xref: /petsc/src/ts/interface/ts.c (revision b1cb36f36a6b7dfc48c2470e77f0d724e5408ff8)
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 
1249354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1349354f04SShri Abhyankar 
142d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx {
152d5ee99bSBarry Smith   PetscViewer   viewer;
164b363babSBarry Smith   PetscDrawAxis axis;
172d5ee99bSBarry Smith   Vec           initialsolution;
182d5ee99bSBarry Smith   PetscBool     showinitial;
192d5ee99bSBarry Smith   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
202d5ee99bSBarry Smith   PetscBool     showtimestepandtime;
212d5ee99bSBarry Smith   int           color;
222d5ee99bSBarry Smith };
232d5ee99bSBarry Smith 
24bdad233fSMatthew Knepley #undef __FUNCT__
25fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSetFromOptions"
26fde5950dSBarry Smith /*@C
27fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
28fde5950dSBarry Smith 
29fde5950dSBarry Smith    Collective on TS
30fde5950dSBarry Smith 
31fde5950dSBarry Smith    Input Parameters:
32fde5950dSBarry Smith +  ts - TS object you wish to monitor
33fde5950dSBarry Smith .  name - the monitor type one is seeking
34fde5950dSBarry Smith .  help - message indicating what monitoring is done
35fde5950dSBarry Smith .  manual - manual page for the monitor
36fde5950dSBarry Smith .  monitor - the monitor function
37fde5950dSBarry 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
38fde5950dSBarry Smith 
39fde5950dSBarry Smith    Level: developer
40fde5950dSBarry Smith 
41fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
42fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
43fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
44fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
45fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
46fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
47fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
48fde5950dSBarry Smith @*/
49fde5950dSBarry Smith PetscErrorCode  TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),PetscErrorCode (*monitorsetup)(TS,PetscViewer))
50fde5950dSBarry Smith {
51fde5950dSBarry Smith   PetscErrorCode    ierr;
52fde5950dSBarry Smith   PetscViewer       viewer;
53fde5950dSBarry Smith   PetscViewerFormat format;
54fde5950dSBarry Smith   PetscBool         flg;
55fde5950dSBarry Smith 
56fde5950dSBarry Smith   PetscFunctionBegin;
57fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
58fde5950dSBarry Smith   if (flg) {
596a9046bcSBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
60fde5950dSBarry Smith     if (monitorsetup) {
61fde5950dSBarry Smith       ierr = (*monitorsetup)(ts,viewer);CHKERRQ(ierr);
62fde5950dSBarry Smith     }
63fde5950dSBarry Smith     ierr = TSMonitorSet(ts,monitor,viewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);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 @*/
93fde5950dSBarry Smith PetscErrorCode  TSAdjointMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),PetscErrorCode (*monitorsetup)(TS,PetscViewer))
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) {
1036a9046bcSBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
104fde5950dSBarry Smith     if (monitorsetup) {
105fde5950dSBarry Smith       ierr = (*monitorsetup)(ts,viewer);CHKERRQ(ierr);
106fde5950dSBarry Smith     }
107fde5950dSBarry Smith     ierr = TSAdjointMonitorSet(ts,monitor,viewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
108fde5950dSBarry Smith   }
109fde5950dSBarry Smith   PetscFunctionReturn(0);
110fde5950dSBarry Smith }
111fde5950dSBarry Smith 
112fde5950dSBarry Smith #undef __FUNCT__
113bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
114bdad233fSMatthew Knepley /*@
115bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
116bdad233fSMatthew Knepley 
117bdad233fSMatthew Knepley    Collective on TS
118bdad233fSMatthew Knepley 
119bdad233fSMatthew Knepley    Input Parameter:
120bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
121bdad233fSMatthew Knepley 
122bdad233fSMatthew Knepley    Options Database Keys:
1234d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
124ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
1253e4cdcaaSBarry Smith .  -ts_max_steps <maxsteps> - maximum number of time-steps to take
1263e4cdcaaSBarry Smith .  -ts_final_time <time> - maximum time to compute to
1273e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
128a3cdaa26SBarry 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
129a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
130a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
131a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
132a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
133a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
134ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
135847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
136bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
137de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
138de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
139de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
140de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
141de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
142de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
143de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1443e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1453e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
146fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
147b3d3934dSBarry Smith .  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
148110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
149110eb670SHong Zhang .  -ts_adjoint_monitor - print information at each adjoint time step
15053ea634cSHong Zhang -  -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically
15153ea634cSHong Zhang 
15291b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
153bdad233fSMatthew Knepley 
154bdad233fSMatthew Knepley    Level: beginner
155bdad233fSMatthew Knepley 
156bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
157bdad233fSMatthew Knepley 
158a313700dSBarry Smith .seealso: TSGetType()
159bdad233fSMatthew Knepley @*/
1607087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
161bdad233fSMatthew Knepley {
162bc952696SBarry Smith   PetscBool              opt,flg,tflg;
163dfbe8321SBarry Smith   PetscErrorCode         ierr;
164eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
165089b2837SJed Brown   SNES                   snes;
1661c3436cfSJed Brown   TSAdapt                adapt;
16731748224SBarry Smith   PetscReal              time_step;
16849354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
169d1212d36SBarry Smith   char                   dir[16];
1706991f827SBarry Smith   const char             *defaultType;
1716991f827SBarry Smith   char                   typeName[256];
172bdad233fSMatthew Knepley 
173bdad233fSMatthew Knepley   PetscFunctionBegin;
1740700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1753194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
1766991f827SBarry Smith   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
1776991f827SBarry Smith   else defaultType = TSEULER;
1786991f827SBarry Smith 
1796991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
1806991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
1816991f827SBarry Smith   if (opt) {
1826991f827SBarry Smith     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
1836991f827SBarry Smith   } else {
1846991f827SBarry Smith     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
1856991f827SBarry Smith   }
186bdad233fSMatthew Knepley 
187bdad233fSMatthew Knepley   /* Handle generic TS options */
1880298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1890298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1900298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
19131748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
19231748224SBarry Smith   if (flg) {
19331748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
19431748224SBarry Smith   }
19549354f04SShri 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);
19649354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1970298fd71SBarry 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);
1980298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1990298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
2000298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
2010298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
202bdad233fSMatthew Knepley 
20356f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
20456f85f32SBarry Smith   {
20556f85f32SBarry Smith   PetscBool set;
20656f85f32SBarry Smith   flg  = PETSC_FALSE;
20756f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
20856f85f32SBarry Smith   if (set) {
20956f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
21056f85f32SBarry Smith   }
21156f85f32SBarry Smith   }
21256f85f32SBarry Smith #endif
21356f85f32SBarry Smith 
214bdad233fSMatthew Knepley   /* Monitor options */
215fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
216fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
217fde5950dSBarry Smith   ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr);
218fde5950dSBarry Smith 
2195180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2205180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
2215180491cSLisandro Dalcin 
2224f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
223a7cc72afSBarry Smith   if (opt) {
2240b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2253923b477SBarry Smith     PetscInt       howoften = 1;
226a80ad3e0SBarry Smith 
2270298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2287f52daa2SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2294f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
230b3603a34SBarry Smith   }
2314f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
232b3603a34SBarry Smith   if (opt) {
2330b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2343923b477SBarry Smith     PetscInt       howoften = 1;
235b3603a34SBarry Smith 
2360298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
23722d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
2384f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
239bdad233fSMatthew Knepley   }
2404f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
241ef20d060SBarry Smith   if (opt) {
2420b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2433923b477SBarry Smith     PetscInt       howoften = 1;
244ef20d060SBarry Smith 
2450298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
24622d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
2474f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
248ef20d060SBarry Smith   }
249201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
250201da799SBarry Smith   if (opt) {
251201da799SBarry Smith     TSMonitorLGCtx ctx;
252201da799SBarry Smith     PetscInt       howoften = 1;
253201da799SBarry Smith 
2540298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2557f52daa2SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
256201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
257201da799SBarry Smith   }
258201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
259201da799SBarry Smith   if (opt) {
260201da799SBarry Smith     TSMonitorLGCtx ctx;
261201da799SBarry Smith     PetscInt       howoften = 1;
262201da799SBarry Smith 
2630298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2647f52daa2SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
265201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
266201da799SBarry Smith   }
2678189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2688189c53fSBarry Smith   if (opt) {
2698189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2708189c53fSBarry Smith     PetscInt          howoften = 1;
2718189c53fSBarry Smith 
2720298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
27322d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
2748189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2758189c53fSBarry Smith   }
276ef20d060SBarry Smith   opt  = PETSC_FALSE;
2770dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
278a7cc72afSBarry Smith   if (opt) {
27983a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
28083a4ac43SBarry Smith     PetscInt         howoften = 1;
281a80ad3e0SBarry Smith 
2820298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
283ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
28483a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
285bdad233fSMatthew Knepley   }
286fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2879110b2e7SHong Zhang   ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr);
2889110b2e7SHong Zhang   if (opt) {
2899110b2e7SHong Zhang     TSMonitorDrawCtx ctx;
2909110b2e7SHong Zhang     PetscInt         howoften = 1;
2919110b2e7SHong Zhang 
2929110b2e7SHong Zhang     ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr);
2939110b2e7SHong Zhang     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
2949110b2e7SHong Zhang     ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2959110b2e7SHong Zhang   }
2969110b2e7SHong Zhang   opt  = PETSC_FALSE;
2972d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2982d5ee99bSBarry Smith   if (opt) {
2992d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
3002d5ee99bSBarry Smith     PetscReal        bounds[4];
3012d5ee99bSBarry Smith     PetscInt         n = 4;
3022d5ee99bSBarry Smith     PetscDraw        draw;
3032d5ee99bSBarry Smith 
3042d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
3052d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
3062d5ee99bSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr);
3072d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
3082d5ee99bSBarry Smith     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
3094b363babSBarry Smith     ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr);
3104b363babSBarry Smith     ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
311f54adfc0SBarry Smith     ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
3124b363babSBarry Smith     ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr);
31307995780SPeter Brune     /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */
3142d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3152d5ee99bSBarry Smith   }
3162d5ee99bSBarry Smith   opt  = PETSC_FALSE;
3170dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
3183a471f94SBarry Smith   if (opt) {
31983a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
32083a4ac43SBarry Smith     PetscInt         howoften = 1;
3213a471f94SBarry Smith 
3220298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
323ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
32483a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3253a471f94SBarry Smith   }
326fde5950dSBarry Smith 
327ed81e22dSJed Brown   opt  = PETSC_FALSE;
32891b97e58SBarry 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);
329ed81e22dSJed Brown   if (flg) {
330ed81e22dSJed Brown     const char *ptr,*ptr2;
331ed81e22dSJed Brown     char       *filetemplate;
332ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
333ed81e22dSJed Brown     /* Do some cursory validation of the input. */
334ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
335ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
336ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
337ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
338ce94432eSBarry 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");
339ed81e22dSJed Brown       if (ptr2) break;
340ed81e22dSJed Brown     }
341ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
342ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
343ed81e22dSJed Brown   }
344bdad233fSMatthew Knepley 
345d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
346d1212d36SBarry Smith   if (flg) {
347d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
348d1212d36SBarry Smith     int                  ray = 0;
349d1212d36SBarry Smith     DMDADirection        ddir;
350d1212d36SBarry Smith     DM                   da;
351d1212d36SBarry Smith     PetscMPIInt          rank;
352d1212d36SBarry Smith 
353ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
354d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
355d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
356ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
357d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
358d1212d36SBarry Smith 
359d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
360b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
361d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
362d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
363ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
364d1212d36SBarry Smith     if (!rank) {
365d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
366d1212d36SBarry Smith     }
36751b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
368d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
369d1212d36SBarry Smith   }
37051b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
37151b4a12fSMatthew G. Knepley   if (flg) {
37251b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
37351b4a12fSMatthew G. Knepley     int                 ray = 0;
37451b4a12fSMatthew G. Knepley     DMDADirection       ddir;
37551b4a12fSMatthew G. Knepley     DM                  da;
37651b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
377d1212d36SBarry Smith 
37851b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
37951b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
38051b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
38151b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
38251b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3831c3436cfSJed Brown 
38451b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
385b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
38651b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
38751b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
38851b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
38951b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
39051b4a12fSMatthew G. Knepley   }
391a7a1495cSBarry Smith 
392b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
393b3d3934dSBarry Smith   if (opt) {
394b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
395b3d3934dSBarry Smith 
396b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
397b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
398b3d3934dSBarry Smith   }
399b3d3934dSBarry Smith 
400847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
401847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
402847ff0e1SMatthew G. Knepley   if (flg) {
403847ff0e1SMatthew G. Knepley     DM   dm;
404847ff0e1SMatthew G. Knepley     DMTS tdm;
405847ff0e1SMatthew G. Knepley 
406847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
407847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
408847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
409847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
410847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
411847ff0e1SMatthew G. Knepley   }
412847ff0e1SMatthew G. Knepley 
4137781c08eSBarry Smith   /*
4147781c08eSBarry Smith      This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui
4157781c08eSBarry Smith      will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin()
4167781c08eSBarry Smith   */
417552698daSJed Brown   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
418e55864a3SBarry Smith   ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr);
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 */
42668bece0bSHong Zhang   if (ts->trajectory) tflg = PETSC_TRUE;
42768bece0bSHong Zhang   else tflg = PETSC_FALSE;
42868bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
42968bece0bSHong Zhang   if (tflg) {
43068bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
43168bece0bSHong Zhang   }
43268bece0bSHong Zhang   if (ts->adjoint_solve) tflg = PETSC_TRUE;
43368bece0bSHong Zhang   else tflg = PETSC_FALSE;
43468bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr);
43568bece0bSHong Zhang   if (flg) {
43668bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
43768bece0bSHong Zhang     ts->adjoint_solve = tflg;
43868bece0bSHong Zhang   }
43968bece0bSHong Zhang   if (ts->trajectory) {
44068bece0bSHong Zhang     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
44168bece0bSHong Zhang   }
442d763cef2SBarry Smith 
443d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4440633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
445fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
446d763cef2SBarry Smith 
4476991f827SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4486991f827SBarry Smith   if (snes) {
4496991f827SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
45049c41c37SLisandro Dalcin     ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
451d763cef2SBarry Smith   }
45268bece0bSHong Zhang 
453d763cef2SBarry Smith   PetscFunctionReturn(0);
454d763cef2SBarry Smith }
455d763cef2SBarry Smith 
456d763cef2SBarry Smith #undef __FUNCT__
457bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory"
458d2daff3dSHong Zhang /*@
459bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
460d2daff3dSHong Zhang 
461d2daff3dSHong Zhang    Collective on TS
462d2daff3dSHong Zhang 
463d2daff3dSHong Zhang    Input Parameters:
464bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
465bc952696SBarry Smith 
46668bece0bSHong Zhang Note: This routine should be called after all TS options have been set
467d2daff3dSHong Zhang 
468d2daff3dSHong Zhang    Level: intermediate
469d2daff3dSHong Zhang 
470bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve()
471d2daff3dSHong Zhang 
472bc952696SBarry Smith .keywords: TS, set, checkpoint,
473d2daff3dSHong Zhang @*/
474bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
475d2daff3dSHong Zhang {
476bc952696SBarry Smith   PetscErrorCode ierr;
477bc952696SBarry Smith 
478d2daff3dSHong Zhang   PetscFunctionBegin;
479d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
480bc952696SBarry Smith   if (!ts->trajectory) {
481bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
482972caf09SHong Zhang     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
483bc952696SBarry Smith   }
484d2daff3dSHong Zhang   PetscFunctionReturn(0);
485d2daff3dSHong Zhang }
486d2daff3dSHong Zhang 
487a7a1495cSBarry Smith #undef __FUNCT__
488a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian"
489a7a1495cSBarry Smith /*@
490a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
491a7a1495cSBarry Smith       set with TSSetRHSJacobian().
492a7a1495cSBarry Smith 
493a7a1495cSBarry Smith    Collective on TS and Vec
494a7a1495cSBarry Smith 
495a7a1495cSBarry Smith    Input Parameters:
496316643e7SJed Brown +  ts - the TS context
497a7a1495cSBarry Smith .  t - current timestep
4980910c330SBarry Smith -  U - input vector
499a7a1495cSBarry Smith 
500a7a1495cSBarry Smith    Output Parameters:
501a7a1495cSBarry Smith +  A - Jacobian matrix
502a7a1495cSBarry Smith .  B - optional preconditioning matrix
503a7a1495cSBarry Smith -  flag - flag indicating matrix structure
504a7a1495cSBarry Smith 
505a7a1495cSBarry Smith    Notes:
506a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
507a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
508a7a1495cSBarry Smith 
50994b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
510a7a1495cSBarry Smith    flag parameter.
511a7a1495cSBarry Smith 
512a7a1495cSBarry Smith    Level: developer
513a7a1495cSBarry Smith 
514a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
515a7a1495cSBarry Smith 
51694b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
517a7a1495cSBarry Smith @*/
518d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
519a7a1495cSBarry Smith {
520dfbe8321SBarry Smith   PetscErrorCode ierr;
521270bf2e7SJed Brown   PetscObjectState Ustate;
52224989b8cSPeter Brune   DM             dm;
523942e3340SBarry Smith   DMTS           tsdm;
52424989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
52524989b8cSPeter Brune   void           *ctx;
52624989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
527b2df71adSDebojyoti Ghosh   TSRHSFunction  rhsfunction;
528a7a1495cSBarry Smith 
529a7a1495cSBarry Smith   PetscFunctionBegin;
5300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5310910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5320910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
53324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
534942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
53524989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5360298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
537b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
53859e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
539b2df71adSDebojyoti Ghosh   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
5400e4ef248SJed Brown     PetscFunctionReturn(0);
541f8ede8e7SJed Brown   }
542d90be118SSean Farley 
543ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
544d90be118SSean Farley 
545e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
54694ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
54794ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
54894ab13aaSBarry Smith     if (A != B) {
54994ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
55094ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
551e1244c69SJed Brown     }
552e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
553e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
554e1244c69SJed Brown   }
555e1244c69SJed Brown 
55624989b8cSPeter Brune   if (rhsjacobianfunc) {
5576cd88445SBarry Smith     PetscBool missing;
55894ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
559a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
560d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
561a7a1495cSBarry Smith     PetscStackPop;
56294ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
5636cd88445SBarry Smith     if (A) {
5646cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
5656cd88445SBarry 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");
5666cd88445SBarry Smith     }
5676cd88445SBarry Smith     if (B && B != A) {
5686cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
5696cd88445SBarry 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");
5706cd88445SBarry Smith     }
571ef66eb69SBarry Smith   } else {
57294ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
57394ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
574ef66eb69SBarry Smith   }
5750e4ef248SJed Brown   ts->rhsjacobian.time       = t;
5760910c330SBarry Smith   ts->rhsjacobian.X          = U;
57759e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
578a7a1495cSBarry Smith   PetscFunctionReturn(0);
579a7a1495cSBarry Smith }
580a7a1495cSBarry Smith 
5814a2ae208SSatish Balay #undef __FUNCT__
5824a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
583316643e7SJed Brown /*@
584d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
585d763cef2SBarry Smith 
586316643e7SJed Brown    Collective on TS and Vec
587316643e7SJed Brown 
588316643e7SJed Brown    Input Parameters:
589316643e7SJed Brown +  ts - the TS context
590316643e7SJed Brown .  t - current time
5910910c330SBarry Smith -  U - state vector
592316643e7SJed Brown 
593316643e7SJed Brown    Output Parameter:
594316643e7SJed Brown .  y - right hand side
595316643e7SJed Brown 
596316643e7SJed Brown    Note:
597316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
598316643e7SJed Brown    is used internally within the nonlinear solvers.
599316643e7SJed Brown 
600316643e7SJed Brown    Level: developer
601316643e7SJed Brown 
602316643e7SJed Brown .keywords: TS, compute
603316643e7SJed Brown 
604316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
605316643e7SJed Brown @*/
6060910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
607d763cef2SBarry Smith {
608dfbe8321SBarry Smith   PetscErrorCode ierr;
60924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
61024989b8cSPeter Brune   TSIFunction    ifunction;
61124989b8cSPeter Brune   void           *ctx;
61224989b8cSPeter Brune   DM             dm;
61324989b8cSPeter Brune 
614d763cef2SBarry Smith   PetscFunctionBegin;
6150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6160910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6170700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
61824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
61924989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6200298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
621d763cef2SBarry Smith 
622ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
623d763cef2SBarry Smith 
6240910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
62524989b8cSPeter Brune   if (rhsfunction) {
626d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6270910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
628d763cef2SBarry Smith     PetscStackPop;
629214bc6a2SJed Brown   } else {
630214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
631b2cd27e8SJed Brown   }
63244a41b28SSean Farley 
6330910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
634d763cef2SBarry Smith   PetscFunctionReturn(0);
635d763cef2SBarry Smith }
636d763cef2SBarry Smith 
6374a2ae208SSatish Balay #undef __FUNCT__
638ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
639ef20d060SBarry Smith /*@
640ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
641ef20d060SBarry Smith 
642ef20d060SBarry Smith    Collective on TS and Vec
643ef20d060SBarry Smith 
644ef20d060SBarry Smith    Input Parameters:
645ef20d060SBarry Smith +  ts - the TS context
646ef20d060SBarry Smith -  t - current time
647ef20d060SBarry Smith 
648ef20d060SBarry Smith    Output Parameter:
6490910c330SBarry Smith .  U - the solution
650ef20d060SBarry Smith 
651ef20d060SBarry Smith    Note:
652ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
653ef20d060SBarry Smith    is used internally within the nonlinear solvers.
654ef20d060SBarry Smith 
655ef20d060SBarry Smith    Level: developer
656ef20d060SBarry Smith 
657ef20d060SBarry Smith .keywords: TS, compute
658ef20d060SBarry Smith 
659abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
660ef20d060SBarry Smith @*/
6610910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
662ef20d060SBarry Smith {
663ef20d060SBarry Smith   PetscErrorCode     ierr;
664ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
665ef20d060SBarry Smith   void               *ctx;
666ef20d060SBarry Smith   DM                 dm;
667ef20d060SBarry Smith 
668ef20d060SBarry Smith   PetscFunctionBegin;
669ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6700910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
671ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
672ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
673ef20d060SBarry Smith 
674ef20d060SBarry Smith   if (solutionfunction) {
6759b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6760910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
677ef20d060SBarry Smith     PetscStackPop;
678ef20d060SBarry Smith   }
679ef20d060SBarry Smith   PetscFunctionReturn(0);
680ef20d060SBarry Smith }
6819b7cd975SBarry Smith #undef __FUNCT__
6829b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
6839b7cd975SBarry Smith /*@
6849b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6859b7cd975SBarry Smith 
6869b7cd975SBarry Smith    Collective on TS and Vec
6879b7cd975SBarry Smith 
6889b7cd975SBarry Smith    Input Parameters:
6899b7cd975SBarry Smith +  ts - the TS context
6909b7cd975SBarry Smith -  t - current time
6919b7cd975SBarry Smith 
6929b7cd975SBarry Smith    Output Parameter:
6939b7cd975SBarry Smith .  U - the function value
6949b7cd975SBarry Smith 
6959b7cd975SBarry Smith    Note:
6969b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
6979b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6989b7cd975SBarry Smith 
6999b7cd975SBarry Smith    Level: developer
7009b7cd975SBarry Smith 
7019b7cd975SBarry Smith .keywords: TS, compute
7029b7cd975SBarry Smith 
7039b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7049b7cd975SBarry Smith @*/
7059b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7069b7cd975SBarry Smith {
7079b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7089b7cd975SBarry Smith   void               *ctx;
7099b7cd975SBarry Smith   DM                 dm;
7109b7cd975SBarry Smith 
7119b7cd975SBarry Smith   PetscFunctionBegin;
7129b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7139b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7149b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7159b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7169b7cd975SBarry Smith 
7179b7cd975SBarry Smith   if (forcing) {
7189b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7199b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7209b7cd975SBarry Smith     PetscStackPop;
7219b7cd975SBarry Smith   }
7229b7cd975SBarry Smith   PetscFunctionReturn(0);
7239b7cd975SBarry Smith }
724ef20d060SBarry Smith 
725ef20d060SBarry Smith #undef __FUNCT__
726214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
727214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
728214bc6a2SJed Brown {
7292dd45cf8SJed Brown   Vec            F;
730214bc6a2SJed Brown   PetscErrorCode ierr;
731214bc6a2SJed Brown 
732214bc6a2SJed Brown   PetscFunctionBegin;
7330298fd71SBarry Smith   *Frhs = NULL;
7340298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
735214bc6a2SJed Brown   if (!ts->Frhs) {
7362dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
737214bc6a2SJed Brown   }
738214bc6a2SJed Brown   *Frhs = ts->Frhs;
739214bc6a2SJed Brown   PetscFunctionReturn(0);
740214bc6a2SJed Brown }
741214bc6a2SJed Brown 
742214bc6a2SJed Brown #undef __FUNCT__
743214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
744214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
745214bc6a2SJed Brown {
746214bc6a2SJed Brown   Mat            A,B;
7472dd45cf8SJed Brown   PetscErrorCode ierr;
748214bc6a2SJed Brown 
749214bc6a2SJed Brown   PetscFunctionBegin;
750c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
751c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
7520298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
753214bc6a2SJed Brown   if (Arhs) {
754214bc6a2SJed Brown     if (!ts->Arhs) {
755214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
756214bc6a2SJed Brown     }
757214bc6a2SJed Brown     *Arhs = ts->Arhs;
758214bc6a2SJed Brown   }
759214bc6a2SJed Brown   if (Brhs) {
760214bc6a2SJed Brown     if (!ts->Brhs) {
761bdb70873SJed Brown       if (A != B) {
762214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
763bdb70873SJed Brown       } else {
764bdb70873SJed Brown         ts->Brhs = ts->Arhs;
765bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
766bdb70873SJed Brown       }
767214bc6a2SJed Brown     }
768214bc6a2SJed Brown     *Brhs = ts->Brhs;
769214bc6a2SJed Brown   }
770214bc6a2SJed Brown   PetscFunctionReturn(0);
771214bc6a2SJed Brown }
772214bc6a2SJed Brown 
773214bc6a2SJed Brown #undef __FUNCT__
774316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
775316643e7SJed Brown /*@
7760910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
777316643e7SJed Brown 
778316643e7SJed Brown    Collective on TS and Vec
779316643e7SJed Brown 
780316643e7SJed Brown    Input Parameters:
781316643e7SJed Brown +  ts - the TS context
782316643e7SJed Brown .  t - current time
7830910c330SBarry Smith .  U - state vector
7840910c330SBarry Smith .  Udot - time derivative of state vector
785214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
786316643e7SJed Brown 
787316643e7SJed Brown    Output Parameter:
788316643e7SJed Brown .  Y - right hand side
789316643e7SJed Brown 
790316643e7SJed Brown    Note:
791316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
792316643e7SJed Brown    is used internally within the nonlinear solvers.
793316643e7SJed Brown 
794316643e7SJed Brown    If the user did did not write their equations in implicit form, this
795316643e7SJed Brown    function recasts them in implicit form.
796316643e7SJed Brown 
797316643e7SJed Brown    Level: developer
798316643e7SJed Brown 
799316643e7SJed Brown .keywords: TS, compute
800316643e7SJed Brown 
801316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
802316643e7SJed Brown @*/
8030910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
804316643e7SJed Brown {
805316643e7SJed Brown   PetscErrorCode ierr;
80624989b8cSPeter Brune   TSIFunction    ifunction;
80724989b8cSPeter Brune   TSRHSFunction  rhsfunction;
80824989b8cSPeter Brune   void           *ctx;
80924989b8cSPeter Brune   DM             dm;
810316643e7SJed Brown 
811316643e7SJed Brown   PetscFunctionBegin;
8120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8130910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8140910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8150700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
816316643e7SJed Brown 
81724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
81824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8190298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
82024989b8cSPeter Brune 
821ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
822d90be118SSean Farley 
8230910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
82424989b8cSPeter Brune   if (ifunction) {
825316643e7SJed Brown     PetscStackPush("TS user implicit function");
8260910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
827316643e7SJed Brown     PetscStackPop;
828214bc6a2SJed Brown   }
829214bc6a2SJed Brown   if (imex) {
83024989b8cSPeter Brune     if (!ifunction) {
8310910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8322dd45cf8SJed Brown     }
83324989b8cSPeter Brune   } else if (rhsfunction) {
83424989b8cSPeter Brune     if (ifunction) {
835214bc6a2SJed Brown       Vec Frhs;
836214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8370910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
838214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8392dd45cf8SJed Brown     } else {
8400910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8410910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
842316643e7SJed Brown     }
8434a6899ffSJed Brown   }
8440910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
845316643e7SJed Brown   PetscFunctionReturn(0);
846316643e7SJed Brown }
847316643e7SJed Brown 
848316643e7SJed Brown #undef __FUNCT__
849316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
850316643e7SJed Brown /*@
851316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
852316643e7SJed Brown 
853316643e7SJed Brown    Collective on TS and Vec
854316643e7SJed Brown 
855316643e7SJed Brown    Input
856316643e7SJed Brown       Input Parameters:
857316643e7SJed Brown +  ts - the TS context
858316643e7SJed Brown .  t - current timestep
8590910c330SBarry Smith .  U - state vector
8600910c330SBarry Smith .  Udot - time derivative of state vector
861214bc6a2SJed Brown .  shift - shift to apply, see note below
862214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
863316643e7SJed Brown 
864316643e7SJed Brown    Output Parameters:
865316643e7SJed Brown +  A - Jacobian matrix
866316643e7SJed Brown .  B - optional preconditioning matrix
867316643e7SJed Brown -  flag - flag indicating matrix structure
868316643e7SJed Brown 
869316643e7SJed Brown    Notes:
8700910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
871316643e7SJed Brown 
8720910c330SBarry Smith    dF/dU + shift*dF/dUdot
873316643e7SJed Brown 
874316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
875316643e7SJed Brown    is used internally within the nonlinear solvers.
876316643e7SJed Brown 
877316643e7SJed Brown    Level: developer
878316643e7SJed Brown 
879316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
880316643e7SJed Brown 
881316643e7SJed Brown .seealso:  TSSetIJacobian()
88263495f91SJed Brown @*/
883d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
884316643e7SJed Brown {
885316643e7SJed Brown   PetscErrorCode ierr;
88624989b8cSPeter Brune   TSIJacobian    ijacobian;
88724989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
88824989b8cSPeter Brune   DM             dm;
88924989b8cSPeter Brune   void           *ctx;
890316643e7SJed Brown 
891316643e7SJed Brown   PetscFunctionBegin;
8920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8930910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8940910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
895316643e7SJed Brown   PetscValidPointer(A,6);
89694ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
897316643e7SJed Brown   PetscValidPointer(B,7);
89894ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
89924989b8cSPeter Brune 
90024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
90124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9020298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
90324989b8cSPeter Brune 
904ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
905316643e7SJed Brown 
90694ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
90724989b8cSPeter Brune   if (ijacobian) {
9086cd88445SBarry Smith     PetscBool missing;
909316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
910d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
911316643e7SJed Brown     PetscStackPop;
9126cd88445SBarry Smith     if (A) {
9136cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9146cd88445SBarry 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");
9156cd88445SBarry Smith     }
9166cd88445SBarry Smith     if (B && B != A) {
9176cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9186cd88445SBarry 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");
9196cd88445SBarry Smith     }
9204a6899ffSJed Brown   }
921214bc6a2SJed Brown   if (imex) {
922b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
92394ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
92494ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
92594ab13aaSBarry Smith       if (A != B) {
92694ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
92794ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
928214bc6a2SJed Brown       }
929214bc6a2SJed Brown     }
930214bc6a2SJed Brown   } else {
931e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
932e1244c69SJed Brown     if (rhsjacobian) {
933bd373395SBarry Smith       if (ijacobian) {
934214bc6a2SJed Brown         ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
935bd373395SBarry Smith       } else {
936bd373395SBarry Smith         ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr);
937214bc6a2SJed Brown       }
938d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
939e1244c69SJed Brown     }
94094ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
941e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
942e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
94394ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
94494ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
94594ab13aaSBarry Smith       if (A != B) {
94694ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
94794ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
948316643e7SJed Brown       }
949e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
950e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
951e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
95294ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
95394ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
95494ab13aaSBarry Smith         if (A != B) {
95594ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
95694ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
957214bc6a2SJed Brown         }
958316643e7SJed Brown       }
95994ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
96094ab13aaSBarry Smith       if (A != B) {
96194ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
962316643e7SJed Brown       }
963316643e7SJed Brown     }
964316643e7SJed Brown   }
96594ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
966316643e7SJed Brown   PetscFunctionReturn(0);
967316643e7SJed Brown }
968316643e7SJed Brown 
969316643e7SJed Brown #undef __FUNCT__
9704a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
971d763cef2SBarry Smith /*@C
972d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
973b5abc632SBarry Smith     where U_t = G(t,u).
974d763cef2SBarry Smith 
9753f9fe445SBarry Smith     Logically Collective on TS
976d763cef2SBarry Smith 
977d763cef2SBarry Smith     Input Parameters:
978d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
9790298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
980d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
981d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
9820298fd71SBarry Smith           function evaluation routine (may be NULL)
983d763cef2SBarry Smith 
984d763cef2SBarry Smith     Calling sequence of func:
98587828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
986d763cef2SBarry Smith 
987d763cef2SBarry Smith +   t - current timestep
988d763cef2SBarry Smith .   u - input vector
989d763cef2SBarry Smith .   F - function vector
990d763cef2SBarry Smith -   ctx - [optional] user-defined function context
991d763cef2SBarry Smith 
992d763cef2SBarry Smith     Level: beginner
993d763cef2SBarry Smith 
9942bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
9952bbac0d3SBarry Smith 
996d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
997d763cef2SBarry Smith 
998ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
999d763cef2SBarry Smith @*/
1000089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1001d763cef2SBarry Smith {
1002089b2837SJed Brown   PetscErrorCode ierr;
1003089b2837SJed Brown   SNES           snes;
10040298fd71SBarry Smith   Vec            ralloc = NULL;
100524989b8cSPeter Brune   DM             dm;
1006d763cef2SBarry Smith 
1007089b2837SJed Brown   PetscFunctionBegin;
10080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1009ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
101024989b8cSPeter Brune 
101124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
101224989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1013089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1014e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1015e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1016e856ceecSJed Brown     r    = ralloc;
1017e856ceecSJed Brown   }
1018089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1019e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1020d763cef2SBarry Smith   PetscFunctionReturn(0);
1021d763cef2SBarry Smith }
1022d763cef2SBarry Smith 
10234a2ae208SSatish Balay #undef __FUNCT__
1024ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
1025ef20d060SBarry Smith /*@C
1026abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1027ef20d060SBarry Smith 
1028ef20d060SBarry Smith     Logically Collective on TS
1029ef20d060SBarry Smith 
1030ef20d060SBarry Smith     Input Parameters:
1031ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1032ef20d060SBarry Smith .   f - routine for evaluating the solution
1033ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10340298fd71SBarry Smith           function evaluation routine (may be NULL)
1035ef20d060SBarry Smith 
1036ef20d060SBarry Smith     Calling sequence of func:
1037ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1038ef20d060SBarry Smith 
1039ef20d060SBarry Smith +   t - current timestep
1040ef20d060SBarry Smith .   u - output vector
1041ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1042ef20d060SBarry Smith 
1043abd5a294SJed Brown     Notes:
1044abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1045abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1046abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1047abd5a294SJed Brown 
10484f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1049abd5a294SJed Brown 
1050ef20d060SBarry Smith     Level: beginner
1051ef20d060SBarry Smith 
1052ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1053ef20d060SBarry Smith 
10549b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
1055ef20d060SBarry Smith @*/
1056ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1057ef20d060SBarry Smith {
1058ef20d060SBarry Smith   PetscErrorCode ierr;
1059ef20d060SBarry Smith   DM             dm;
1060ef20d060SBarry Smith 
1061ef20d060SBarry Smith   PetscFunctionBegin;
1062ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1063ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1064ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1065ef20d060SBarry Smith   PetscFunctionReturn(0);
1066ef20d060SBarry Smith }
1067ef20d060SBarry Smith 
1068ef20d060SBarry Smith #undef __FUNCT__
10699b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
10709b7cd975SBarry Smith /*@C
10719b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
10729b7cd975SBarry Smith 
10739b7cd975SBarry Smith     Logically Collective on TS
10749b7cd975SBarry Smith 
10759b7cd975SBarry Smith     Input Parameters:
10769b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
10779b7cd975SBarry Smith .   f - routine for evaluating the forcing function
10789b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
10790298fd71SBarry Smith           function evaluation routine (may be NULL)
10809b7cd975SBarry Smith 
10819b7cd975SBarry Smith     Calling sequence of func:
10829b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
10839b7cd975SBarry Smith 
10849b7cd975SBarry Smith +   t - current timestep
10859b7cd975SBarry Smith .   u - output vector
10869b7cd975SBarry Smith -   ctx - [optional] user-defined function context
10879b7cd975SBarry Smith 
10889b7cd975SBarry Smith     Notes:
10899b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
10909b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
10919b7cd975SBarry Smith 
10929b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
10939b7cd975SBarry Smith 
10949b7cd975SBarry Smith     Level: beginner
10959b7cd975SBarry Smith 
10969b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
10979b7cd975SBarry Smith 
10989b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
10999b7cd975SBarry Smith @*/
11009b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
11019b7cd975SBarry Smith {
11029b7cd975SBarry Smith   PetscErrorCode ierr;
11039b7cd975SBarry Smith   DM             dm;
11049b7cd975SBarry Smith 
11059b7cd975SBarry Smith   PetscFunctionBegin;
11069b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11079b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
11089b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
11099b7cd975SBarry Smith   PetscFunctionReturn(0);
11109b7cd975SBarry Smith }
11119b7cd975SBarry Smith 
11129b7cd975SBarry Smith #undef __FUNCT__
11134a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
1114d763cef2SBarry Smith /*@C
1115f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1116b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1117d763cef2SBarry Smith 
11183f9fe445SBarry Smith    Logically Collective on TS
1119d763cef2SBarry Smith 
1120d763cef2SBarry Smith    Input Parameters:
1121d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1122e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1123e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1124d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1125d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11260298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1127d763cef2SBarry Smith 
1128f7ab8db6SBarry Smith    Calling sequence of f:
1129ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1130d763cef2SBarry Smith 
1131d763cef2SBarry Smith +  t - current timestep
1132d763cef2SBarry Smith .  u - input vector
1133e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1134e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1135d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1136d763cef2SBarry Smith 
11376cd88445SBarry Smith    Notes:
11386cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11396cd88445SBarry Smith 
11406cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1141ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1142d763cef2SBarry Smith 
1143d763cef2SBarry Smith    Level: beginner
1144d763cef2SBarry Smith 
1145d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1146d763cef2SBarry Smith 
1147ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1148d763cef2SBarry Smith 
1149d763cef2SBarry Smith @*/
1150e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1151d763cef2SBarry Smith {
1152277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1153089b2837SJed Brown   SNES           snes;
115424989b8cSPeter Brune   DM             dm;
115524989b8cSPeter Brune   TSIJacobian    ijacobian;
1156277b19d0SLisandro Dalcin 
1157d763cef2SBarry Smith   PetscFunctionBegin;
11580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1159e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1160e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1161e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1162e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1163d763cef2SBarry Smith 
116424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
116524989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1166e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1167e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1168e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1169e1244c69SJed Brown   }
11700298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1171089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11725f659677SPeter Brune   if (!ijacobian) {
1173e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
11740e4ef248SJed Brown   }
1175e5d3d808SBarry Smith   if (Amat) {
1176e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
11770e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1178bbd56ea5SKarl Rupp 
1179e5d3d808SBarry Smith     ts->Arhs = Amat;
11800e4ef248SJed Brown   }
1181e5d3d808SBarry Smith   if (Pmat) {
1182e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
11830e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1184bbd56ea5SKarl Rupp 
1185e5d3d808SBarry Smith     ts->Brhs = Pmat;
11860e4ef248SJed Brown   }
1187d763cef2SBarry Smith   PetscFunctionReturn(0);
1188d763cef2SBarry Smith }
1189d763cef2SBarry Smith 
1190316643e7SJed Brown 
1191316643e7SJed Brown #undef __FUNCT__
1192316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1193316643e7SJed Brown /*@C
1194b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1195316643e7SJed Brown 
11963f9fe445SBarry Smith    Logically Collective on TS
1197316643e7SJed Brown 
1198316643e7SJed Brown    Input Parameters:
1199316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12000298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1201316643e7SJed Brown .  f   - the function evaluation routine
12020298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1203316643e7SJed Brown 
1204316643e7SJed Brown    Calling sequence of f:
1205316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1206316643e7SJed Brown 
1207316643e7SJed Brown +  t   - time at step/stage being solved
1208316643e7SJed Brown .  u   - state vector
1209316643e7SJed Brown .  u_t - time derivative of state vector
1210316643e7SJed Brown .  F   - function vector
1211316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1212316643e7SJed Brown 
1213316643e7SJed Brown    Important:
12142bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1215316643e7SJed Brown 
1216316643e7SJed Brown    Level: beginner
1217316643e7SJed Brown 
1218316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1219316643e7SJed Brown 
1220d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1221316643e7SJed Brown @*/
1222089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1223316643e7SJed Brown {
1224089b2837SJed Brown   PetscErrorCode ierr;
1225089b2837SJed Brown   SNES           snes;
12260298fd71SBarry Smith   Vec            resalloc = NULL;
122724989b8cSPeter Brune   DM             dm;
1228316643e7SJed Brown 
1229316643e7SJed Brown   PetscFunctionBegin;
12300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1231ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
123224989b8cSPeter Brune 
123324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
123424989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
123524989b8cSPeter Brune 
1236089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1237e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1238e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1239e856ceecSJed Brown     res  = resalloc;
1240e856ceecSJed Brown   }
1241089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1242e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1243089b2837SJed Brown   PetscFunctionReturn(0);
1244089b2837SJed Brown }
1245089b2837SJed Brown 
1246089b2837SJed Brown #undef __FUNCT__
1247089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1248089b2837SJed Brown /*@C
1249089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1250089b2837SJed Brown 
1251089b2837SJed Brown    Not Collective
1252089b2837SJed Brown 
1253089b2837SJed Brown    Input Parameter:
1254089b2837SJed Brown .  ts - the TS context
1255089b2837SJed Brown 
1256089b2837SJed Brown    Output Parameter:
12570298fd71SBarry Smith +  r - vector to hold residual (or NULL)
12580298fd71SBarry Smith .  func - the function to compute residual (or NULL)
12590298fd71SBarry Smith -  ctx - the function context (or NULL)
1260089b2837SJed Brown 
1261089b2837SJed Brown    Level: advanced
1262089b2837SJed Brown 
1263089b2837SJed Brown .keywords: TS, nonlinear, get, function
1264089b2837SJed Brown 
1265089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1266089b2837SJed Brown @*/
1267089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1268089b2837SJed Brown {
1269089b2837SJed Brown   PetscErrorCode ierr;
1270089b2837SJed Brown   SNES           snes;
127124989b8cSPeter Brune   DM             dm;
1272089b2837SJed Brown 
1273089b2837SJed Brown   PetscFunctionBegin;
1274089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1275089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12760298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
127724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
127824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1279089b2837SJed Brown   PetscFunctionReturn(0);
1280089b2837SJed Brown }
1281089b2837SJed Brown 
1282089b2837SJed Brown #undef __FUNCT__
1283089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1284089b2837SJed Brown /*@C
1285089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1286089b2837SJed Brown 
1287089b2837SJed Brown    Not Collective
1288089b2837SJed Brown 
1289089b2837SJed Brown    Input Parameter:
1290089b2837SJed Brown .  ts - the TS context
1291089b2837SJed Brown 
1292089b2837SJed Brown    Output Parameter:
12930298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
12940298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
12950298fd71SBarry Smith -  ctx - the function context (or NULL)
1296089b2837SJed Brown 
1297089b2837SJed Brown    Level: advanced
1298089b2837SJed Brown 
1299089b2837SJed Brown .keywords: TS, nonlinear, get, function
1300089b2837SJed Brown 
13012bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1302089b2837SJed Brown @*/
1303089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1304089b2837SJed Brown {
1305089b2837SJed Brown   PetscErrorCode ierr;
1306089b2837SJed Brown   SNES           snes;
130724989b8cSPeter Brune   DM             dm;
1308089b2837SJed Brown 
1309089b2837SJed Brown   PetscFunctionBegin;
1310089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1311089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13120298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
131324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
131424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1315316643e7SJed Brown   PetscFunctionReturn(0);
1316316643e7SJed Brown }
1317316643e7SJed Brown 
1318316643e7SJed Brown #undef __FUNCT__
1319316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1320316643e7SJed Brown /*@C
1321a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1322ae8867d6SBarry Smith         provided with TSSetIFunction().
1323316643e7SJed Brown 
13243f9fe445SBarry Smith    Logically Collective on TS
1325316643e7SJed Brown 
1326316643e7SJed Brown    Input Parameters:
1327316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1328e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1329e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1330316643e7SJed Brown .  f   - the Jacobian evaluation routine
13310298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1332316643e7SJed Brown 
1333316643e7SJed Brown    Calling sequence of f:
1334ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1335316643e7SJed Brown 
1336316643e7SJed Brown +  t    - time at step/stage being solved
13371b4a444bSJed Brown .  U    - state vector
13381b4a444bSJed Brown .  U_t  - time derivative of state vector
1339316643e7SJed Brown .  a    - shift
1340e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1341e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1342316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1343316643e7SJed Brown 
1344316643e7SJed Brown    Notes:
1345e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1346316643e7SJed Brown 
1347895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1348895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1349895c21f2SBarry Smith 
1350a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1351b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1352a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1353a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1354a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1355a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1356a4f0a591SBarry Smith 
13576cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13586cd88445SBarry Smith 
13596cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1360ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1361ca5f011dSBarry Smith 
1362316643e7SJed Brown    Level: beginner
1363316643e7SJed Brown 
1364316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1365316643e7SJed Brown 
1366ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1367316643e7SJed Brown 
1368316643e7SJed Brown @*/
1369e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1370316643e7SJed Brown {
1371316643e7SJed Brown   PetscErrorCode ierr;
1372089b2837SJed Brown   SNES           snes;
137324989b8cSPeter Brune   DM             dm;
1374316643e7SJed Brown 
1375316643e7SJed Brown   PetscFunctionBegin;
13760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1377e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1378e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1379e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1380e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
138124989b8cSPeter Brune 
138224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
138324989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
138424989b8cSPeter Brune 
1385089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1386e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1387316643e7SJed Brown   PetscFunctionReturn(0);
1388316643e7SJed Brown }
1389316643e7SJed Brown 
13904a2ae208SSatish Balay #undef __FUNCT__
1391e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1392e1244c69SJed Brown /*@
1393e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1394e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1395e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1396e1244c69SJed Brown    not been changed by the TS.
1397e1244c69SJed Brown 
1398e1244c69SJed Brown    Logically Collective
1399e1244c69SJed Brown 
1400e1244c69SJed Brown    Input Arguments:
1401e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1402e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1403e1244c69SJed Brown 
1404e1244c69SJed Brown    Level: intermediate
1405e1244c69SJed Brown 
1406e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1407e1244c69SJed Brown @*/
1408e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1409e1244c69SJed Brown {
1410e1244c69SJed Brown   PetscFunctionBegin;
1411e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1412e1244c69SJed Brown   PetscFunctionReturn(0);
1413e1244c69SJed Brown }
1414e1244c69SJed Brown 
1415e1244c69SJed Brown #undef __FUNCT__
141655849f57SBarry Smith #define __FUNCT__ "TSLoad"
141755849f57SBarry Smith /*@C
141855849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
141955849f57SBarry Smith 
142055849f57SBarry Smith   Collective on PetscViewer
142155849f57SBarry Smith 
142255849f57SBarry Smith   Input Parameters:
142355849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
142455849f57SBarry Smith            some related function before a call to TSLoad().
142555849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
142655849f57SBarry Smith 
142755849f57SBarry Smith    Level: intermediate
142855849f57SBarry Smith 
142955849f57SBarry Smith   Notes:
143055849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
143155849f57SBarry Smith 
143255849f57SBarry Smith   Notes for advanced users:
143355849f57SBarry Smith   Most users should not need to know the details of the binary storage
143455849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
143555849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
143655849f57SBarry Smith   format is
143755849f57SBarry Smith .vb
143855849f57SBarry Smith      has not yet been determined
143955849f57SBarry Smith .ve
144055849f57SBarry Smith 
144155849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
144255849f57SBarry Smith @*/
1443f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
144455849f57SBarry Smith {
144555849f57SBarry Smith   PetscErrorCode ierr;
144655849f57SBarry Smith   PetscBool      isbinary;
144755849f57SBarry Smith   PetscInt       classid;
144855849f57SBarry Smith   char           type[256];
14492d53ad75SBarry Smith   DMTS           sdm;
1450ad6bc421SBarry Smith   DM             dm;
145155849f57SBarry Smith 
145255849f57SBarry Smith   PetscFunctionBegin;
1453f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
145455849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
145555849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
145655849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
145755849f57SBarry Smith 
1458060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1459ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1460060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1461f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1462f2c2a1b9SBarry Smith   if (ts->ops->load) {
1463f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1464f2c2a1b9SBarry Smith   }
1465ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1466ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1467ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1468f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1469f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
14702d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14712d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
147255849f57SBarry Smith   PetscFunctionReturn(0);
147355849f57SBarry Smith }
147455849f57SBarry Smith 
14759804daf3SBarry Smith #include <petscdraw.h>
1476e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1477e04113cfSBarry Smith #include <petscviewersaws.h>
1478f05ece33SBarry Smith #endif
147955849f57SBarry Smith #undef __FUNCT__
14804a2ae208SSatish Balay #define __FUNCT__ "TSView"
14817e2c5f70SBarry Smith /*@C
1482d763cef2SBarry Smith     TSView - Prints the TS data structure.
1483d763cef2SBarry Smith 
14844c49b128SBarry Smith     Collective on TS
1485d763cef2SBarry Smith 
1486d763cef2SBarry Smith     Input Parameters:
1487d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1488d763cef2SBarry Smith -   viewer - visualization context
1489d763cef2SBarry Smith 
1490d763cef2SBarry Smith     Options Database Key:
1491d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1492d763cef2SBarry Smith 
1493d763cef2SBarry Smith     Notes:
1494d763cef2SBarry Smith     The available visualization contexts include
1495b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1496b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1497d763cef2SBarry Smith          output where only the first processor opens
1498d763cef2SBarry Smith          the file.  All other processors send their
1499d763cef2SBarry Smith          data to the first processor to print.
1500d763cef2SBarry Smith 
1501d763cef2SBarry Smith     The user can open an alternative visualization context with
1502b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1503d763cef2SBarry Smith 
1504d763cef2SBarry Smith     Level: beginner
1505d763cef2SBarry Smith 
1506d763cef2SBarry Smith .keywords: TS, timestep, view
1507d763cef2SBarry Smith 
1508b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1509d763cef2SBarry Smith @*/
15107087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1511d763cef2SBarry Smith {
1512dfbe8321SBarry Smith   PetscErrorCode ierr;
151319fd82e9SBarry Smith   TSType         type;
15142b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
15152d53ad75SBarry Smith   DMTS           sdm;
1516e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1517536b137fSBarry Smith   PetscBool      issaws;
1518f05ece33SBarry Smith #endif
1519d763cef2SBarry Smith 
1520d763cef2SBarry Smith   PetscFunctionBegin;
15210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15223050cee2SBarry Smith   if (!viewer) {
1523ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
15243050cee2SBarry Smith   }
15250700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1526c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1527fd16b177SBarry Smith 
1528251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1529251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
153055849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
15312b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1532e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1533536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1534f05ece33SBarry Smith #endif
153532077d6dSBarry Smith   if (iascii) {
1536dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
153777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
15387c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1539d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
15405ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1541c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1542d763cef2SBarry Smith     }
15435ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1544193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
15452d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
15462d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1547d52bd9f3SBarry Smith     if (ts->ops->view) {
1548d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1549d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1550d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1551d52bd9f3SBarry Smith     }
15520f5bd95cSBarry Smith   } else if (isstring) {
1553a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1554b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
155555849f57SBarry Smith   } else if (isbinary) {
155655849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
155755849f57SBarry Smith     MPI_Comm    comm;
155855849f57SBarry Smith     PetscMPIInt rank;
155955849f57SBarry Smith     char        type[256];
156055849f57SBarry Smith 
156155849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
156255849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
156355849f57SBarry Smith     if (!rank) {
156455849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
156555849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
156655849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
156755849f57SBarry Smith     }
156855849f57SBarry Smith     if (ts->ops->view) {
156955849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
157055849f57SBarry Smith     }
1571f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1572f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
15732d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
15742d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
15752b0a91c0SBarry Smith   } else if (isdraw) {
15762b0a91c0SBarry Smith     PetscDraw draw;
15772b0a91c0SBarry Smith     char      str[36];
157889fd9fafSBarry Smith     PetscReal x,y,bottom,h;
15792b0a91c0SBarry Smith 
15802b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
15812b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
15822b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
15832b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
158451fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
158589fd9fafSBarry Smith     bottom = y - h;
15862b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
15872b0a91c0SBarry Smith     if (ts->ops->view) {
15882b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
15892b0a91c0SBarry Smith     }
15902b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1591e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1592536b137fSBarry Smith   } else if (issaws) {
1593d45a07a7SBarry Smith     PetscMPIInt rank;
15942657e9d9SBarry Smith     const char  *name;
15952657e9d9SBarry Smith 
15962657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1597d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1598d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1599d45a07a7SBarry Smith       char       dir[1024];
1600d45a07a7SBarry Smith 
1601e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1602a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
16032657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
16042657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
16052657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1606d763cef2SBarry Smith     }
16070acecf5bSBarry Smith     if (ts->ops->view) {
16080acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
16090acecf5bSBarry Smith     }
1610f05ece33SBarry Smith #endif
1611f05ece33SBarry Smith   }
1612f05ece33SBarry Smith 
1613b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1614251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1615b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1616d763cef2SBarry Smith   PetscFunctionReturn(0);
1617d763cef2SBarry Smith }
1618d763cef2SBarry Smith 
1619d763cef2SBarry Smith 
16204a2ae208SSatish Balay #undef __FUNCT__
16214a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1622b07ff414SBarry Smith /*@
1623d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1624d763cef2SBarry Smith    the timesteppers.
1625d763cef2SBarry Smith 
16263f9fe445SBarry Smith    Logically Collective on TS
1627d763cef2SBarry Smith 
1628d763cef2SBarry Smith    Input Parameters:
1629d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1630d763cef2SBarry Smith -  usrP - optional user context
1631d763cef2SBarry Smith 
1632daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
1633daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
1634daf670e6SBarry Smith 
1635d763cef2SBarry Smith    Level: intermediate
1636d763cef2SBarry Smith 
1637d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1638d763cef2SBarry Smith 
1639d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1640d763cef2SBarry Smith @*/
16417087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1642d763cef2SBarry Smith {
1643d763cef2SBarry Smith   PetscFunctionBegin;
16440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1645d763cef2SBarry Smith   ts->user = usrP;
1646d763cef2SBarry Smith   PetscFunctionReturn(0);
1647d763cef2SBarry Smith }
1648d763cef2SBarry Smith 
16494a2ae208SSatish Balay #undef __FUNCT__
16504a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1651b07ff414SBarry Smith /*@
1652d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1653d763cef2SBarry Smith     timestepper.
1654d763cef2SBarry Smith 
1655d763cef2SBarry Smith     Not Collective
1656d763cef2SBarry Smith 
1657d763cef2SBarry Smith     Input Parameter:
1658d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1659d763cef2SBarry Smith 
1660d763cef2SBarry Smith     Output Parameter:
1661d763cef2SBarry Smith .   usrP - user context
1662d763cef2SBarry Smith 
1663daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
1664daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
1665daf670e6SBarry Smith 
1666d763cef2SBarry Smith     Level: intermediate
1667d763cef2SBarry Smith 
1668d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1669d763cef2SBarry Smith 
1670d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1671d763cef2SBarry Smith @*/
1672e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1673d763cef2SBarry Smith {
1674d763cef2SBarry Smith   PetscFunctionBegin;
16750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1676e71120c6SJed Brown   *(void**)usrP = ts->user;
1677d763cef2SBarry Smith   PetscFunctionReturn(0);
1678d763cef2SBarry Smith }
1679d763cef2SBarry Smith 
16804a2ae208SSatish Balay #undef __FUNCT__
16814a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1682d763cef2SBarry Smith /*@
1683b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1684d763cef2SBarry Smith 
1685d763cef2SBarry Smith    Not Collective
1686d763cef2SBarry Smith 
1687d763cef2SBarry Smith    Input Parameter:
1688d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1689d763cef2SBarry Smith 
1690d763cef2SBarry Smith    Output Parameter:
1691b8123daeSJed Brown .  iter - number of steps completed so far
1692d763cef2SBarry Smith 
1693d763cef2SBarry Smith    Level: intermediate
1694d763cef2SBarry Smith 
1695d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
16969be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1697d763cef2SBarry Smith @*/
16987087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1699d763cef2SBarry Smith {
1700d763cef2SBarry Smith   PetscFunctionBegin;
17010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17024482741eSBarry Smith   PetscValidIntPointer(iter,2);
1703d763cef2SBarry Smith   *iter = ts->steps;
1704d763cef2SBarry Smith   PetscFunctionReturn(0);
1705d763cef2SBarry Smith }
1706d763cef2SBarry Smith 
17074a2ae208SSatish Balay #undef __FUNCT__
17084a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1709d763cef2SBarry Smith /*@
1710d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1711d763cef2SBarry Smith    as well as the initial time.
1712d763cef2SBarry Smith 
17133f9fe445SBarry Smith    Logically Collective on TS
1714d763cef2SBarry Smith 
1715d763cef2SBarry Smith    Input Parameters:
1716d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1717d763cef2SBarry Smith .  initial_time - the initial time
1718d763cef2SBarry Smith -  time_step - the size of the timestep
1719d763cef2SBarry Smith 
1720d763cef2SBarry Smith    Level: intermediate
1721d763cef2SBarry Smith 
1722d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1723d763cef2SBarry Smith 
1724d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1725d763cef2SBarry Smith @*/
17267087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1727d763cef2SBarry Smith {
1728e144a568SJed Brown   PetscErrorCode ierr;
1729e144a568SJed Brown 
1730d763cef2SBarry Smith   PetscFunctionBegin;
17310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1732e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1733d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1734d763cef2SBarry Smith   PetscFunctionReturn(0);
1735d763cef2SBarry Smith }
1736d763cef2SBarry Smith 
17374a2ae208SSatish Balay #undef __FUNCT__
17384a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1739d763cef2SBarry Smith /*@
1740d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1741d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1742d763cef2SBarry Smith 
17433f9fe445SBarry Smith    Logically Collective on TS
1744d763cef2SBarry Smith 
1745d763cef2SBarry Smith    Input Parameters:
1746d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1747d763cef2SBarry Smith -  time_step - the size of the timestep
1748d763cef2SBarry Smith 
1749d763cef2SBarry Smith    Level: intermediate
1750d763cef2SBarry Smith 
1751d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1752d763cef2SBarry Smith 
1753d763cef2SBarry Smith .keywords: TS, set, timestep
1754d763cef2SBarry Smith @*/
17557087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1756d763cef2SBarry Smith {
1757d763cef2SBarry Smith   PetscFunctionBegin;
17580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1759c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1760d763cef2SBarry Smith   ts->time_step      = time_step;
176131748224SBarry Smith   ts->time_step_orig = time_step;
1762d763cef2SBarry Smith   PetscFunctionReturn(0);
1763d763cef2SBarry Smith }
1764d763cef2SBarry Smith 
17654a2ae208SSatish Balay #undef __FUNCT__
1766a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1767a43b19c4SJed Brown /*@
176849354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
176949354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
177049354f04SShri Abhyankar      or just return at the final time TS computed.
1771a43b19c4SJed Brown 
1772a43b19c4SJed Brown   Logically Collective on TS
1773a43b19c4SJed Brown 
1774a43b19c4SJed Brown    Input Parameter:
1775a43b19c4SJed Brown +   ts - the time-step context
177649354f04SShri Abhyankar -   eftopt - exact final time option
1777a43b19c4SJed Brown 
1778a43b19c4SJed Brown    Level: beginner
1779a43b19c4SJed Brown 
1780a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1781a43b19c4SJed Brown @*/
178249354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1783a43b19c4SJed Brown {
1784a43b19c4SJed Brown   PetscFunctionBegin;
1785a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
178649354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
178749354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1788a43b19c4SJed Brown   PetscFunctionReturn(0);
1789a43b19c4SJed Brown }
1790a43b19c4SJed Brown 
1791a43b19c4SJed Brown #undef __FUNCT__
17924a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1793d763cef2SBarry Smith /*@
1794d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1795d763cef2SBarry Smith 
1796d763cef2SBarry Smith    Not Collective
1797d763cef2SBarry Smith 
1798d763cef2SBarry Smith    Input Parameter:
1799d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1800d763cef2SBarry Smith 
1801d763cef2SBarry Smith    Output Parameter:
1802d763cef2SBarry Smith .  dt - the current timestep size
1803d763cef2SBarry Smith 
1804d763cef2SBarry Smith    Level: intermediate
1805d763cef2SBarry Smith 
1806d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1807d763cef2SBarry Smith 
1808d763cef2SBarry Smith .keywords: TS, get, timestep
1809d763cef2SBarry Smith @*/
18107087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1811d763cef2SBarry Smith {
1812d763cef2SBarry Smith   PetscFunctionBegin;
18130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1814f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1815d763cef2SBarry Smith   *dt = ts->time_step;
1816d763cef2SBarry Smith   PetscFunctionReturn(0);
1817d763cef2SBarry Smith }
1818d763cef2SBarry Smith 
18194a2ae208SSatish Balay #undef __FUNCT__
18204a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1821d8e5e3e6SSatish Balay /*@
1822d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1823d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1824d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1825d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1826d763cef2SBarry Smith 
1827d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1828d763cef2SBarry Smith 
1829d763cef2SBarry Smith    Input Parameter:
1830d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1831d763cef2SBarry Smith 
1832d763cef2SBarry Smith    Output Parameter:
1833d763cef2SBarry Smith .  v - the vector containing the solution
1834d763cef2SBarry Smith 
1835d763cef2SBarry Smith    Level: intermediate
1836d763cef2SBarry Smith 
1837d763cef2SBarry Smith .seealso: TSGetTimeStep()
1838d763cef2SBarry Smith 
1839d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1840d763cef2SBarry Smith @*/
18417087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1842d763cef2SBarry Smith {
1843d763cef2SBarry Smith   PetscFunctionBegin;
18440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18454482741eSBarry Smith   PetscValidPointer(v,2);
18468737fe31SLisandro Dalcin   *v = ts->vec_sol;
1847d763cef2SBarry Smith   PetscFunctionReturn(0);
1848d763cef2SBarry Smith }
1849d763cef2SBarry Smith 
1850c235aa8dSHong Zhang #undef __FUNCT__
1851dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients"
1852c235aa8dSHong Zhang /*@
1853dfb21088SHong Zhang    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()
1854c235aa8dSHong Zhang 
1855c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
1856c235aa8dSHong Zhang 
1857c235aa8dSHong Zhang    Input Parameter:
1858c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
1859c235aa8dSHong Zhang 
1860c235aa8dSHong Zhang    Output Parameter:
1861abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
1862abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
1863c235aa8dSHong Zhang 
1864c235aa8dSHong Zhang    Level: intermediate
1865c235aa8dSHong Zhang 
1866c235aa8dSHong Zhang .seealso: TSGetTimeStep()
1867c235aa8dSHong Zhang 
1868c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
1869c235aa8dSHong Zhang @*/
1870dfb21088SHong Zhang PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
1871c235aa8dSHong Zhang {
1872c235aa8dSHong Zhang   PetscFunctionBegin;
1873c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1874abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
1875abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
1876abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
1877c235aa8dSHong Zhang   PetscFunctionReturn(0);
1878c235aa8dSHong Zhang }
1879c235aa8dSHong Zhang 
1880bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
18814a2ae208SSatish Balay #undef __FUNCT__
1882bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1883d8e5e3e6SSatish Balay /*@
1884bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1885d763cef2SBarry Smith 
1886bdad233fSMatthew Knepley   Not collective
1887d763cef2SBarry Smith 
1888bdad233fSMatthew Knepley   Input Parameters:
1889bdad233fSMatthew Knepley + ts   - The TS
1890bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1891d763cef2SBarry Smith .vb
18920910c330SBarry Smith          U_t - A U = 0      (linear)
18930910c330SBarry Smith          U_t - A(t) U = 0   (linear)
18940910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1895d763cef2SBarry Smith .ve
1896d763cef2SBarry Smith 
1897d763cef2SBarry Smith    Level: beginner
1898d763cef2SBarry Smith 
1899bdad233fSMatthew Knepley .keywords: TS, problem type
1900bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1901d763cef2SBarry Smith @*/
19027087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1903a7cc72afSBarry Smith {
19049e2a6581SJed Brown   PetscErrorCode ierr;
19059e2a6581SJed Brown 
1906d763cef2SBarry Smith   PetscFunctionBegin;
19070700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1908bdad233fSMatthew Knepley   ts->problem_type = type;
19099e2a6581SJed Brown   if (type == TS_LINEAR) {
19109e2a6581SJed Brown     SNES snes;
19119e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
19129e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
19139e2a6581SJed Brown   }
1914d763cef2SBarry Smith   PetscFunctionReturn(0);
1915d763cef2SBarry Smith }
1916d763cef2SBarry Smith 
1917bdad233fSMatthew Knepley #undef __FUNCT__
1918bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1919bdad233fSMatthew Knepley /*@C
1920bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1921bdad233fSMatthew Knepley 
1922bdad233fSMatthew Knepley   Not collective
1923bdad233fSMatthew Knepley 
1924bdad233fSMatthew Knepley   Input Parameter:
1925bdad233fSMatthew Knepley . ts   - The TS
1926bdad233fSMatthew Knepley 
1927bdad233fSMatthew Knepley   Output Parameter:
1928bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1929bdad233fSMatthew Knepley .vb
1930089b2837SJed Brown          M U_t = A U
1931089b2837SJed Brown          M(t) U_t = A(t) U
1932b5abc632SBarry Smith          F(t,U,U_t)
1933bdad233fSMatthew Knepley .ve
1934bdad233fSMatthew Knepley 
1935bdad233fSMatthew Knepley    Level: beginner
1936bdad233fSMatthew Knepley 
1937bdad233fSMatthew Knepley .keywords: TS, problem type
1938bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1939bdad233fSMatthew Knepley @*/
19407087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1941a7cc72afSBarry Smith {
1942bdad233fSMatthew Knepley   PetscFunctionBegin;
19430700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
19444482741eSBarry Smith   PetscValidIntPointer(type,2);
1945bdad233fSMatthew Knepley   *type = ts->problem_type;
1946bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1947bdad233fSMatthew Knepley }
1948d763cef2SBarry Smith 
19494a2ae208SSatish Balay #undef __FUNCT__
19504a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1951d763cef2SBarry Smith /*@
1952d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1953d763cef2SBarry Smith    of a timestepper.
1954d763cef2SBarry Smith 
1955d763cef2SBarry Smith    Collective on TS
1956d763cef2SBarry Smith 
1957d763cef2SBarry Smith    Input Parameter:
1958d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1959d763cef2SBarry Smith 
1960d763cef2SBarry Smith    Notes:
1961d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1962d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1963d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1964d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1965d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1966d763cef2SBarry Smith 
1967d763cef2SBarry Smith    Level: advanced
1968d763cef2SBarry Smith 
1969d763cef2SBarry Smith .keywords: TS, timestep, setup
1970d763cef2SBarry Smith 
1971d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1972d763cef2SBarry Smith @*/
19737087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1974d763cef2SBarry Smith {
1975dfbe8321SBarry Smith   PetscErrorCode ierr;
19766c6b9e74SPeter Brune   DM             dm;
19776c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
1978d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
19796c6b9e74SPeter Brune   TSIJacobian    ijac;
19806c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1981d763cef2SBarry Smith 
1982d763cef2SBarry Smith   PetscFunctionBegin;
19830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1984277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1985277b19d0SLisandro Dalcin 
19862c18e0fdSBarry Smith   ts->total_steps = 0;
19877adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
19889596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1989d763cef2SBarry Smith   }
1990277b19d0SLisandro Dalcin 
1991277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1992277b19d0SLisandro Dalcin 
19931c3436cfSJed Brown 
1994552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
1995277b19d0SLisandro Dalcin 
1996e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1997e1244c69SJed Brown     Mat Amat,Pmat;
1998e1244c69SJed Brown     SNES snes;
1999e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2000e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2001e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2002e1244c69SJed Brown      * have displaced the RHS matrix */
2003e1244c69SJed Brown     if (Amat == ts->Arhs) {
2004e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2005e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2006e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2007e1244c69SJed Brown     }
2008e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2009e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2010e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2011e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2012e1244c69SJed Brown     }
2013e1244c69SJed Brown   }
2014277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2015000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2016277b19d0SLisandro Dalcin   }
2017277b19d0SLisandro Dalcin 
20186c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
20196c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
20206c6b9e74SPeter Brune    */
20216c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
20220298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
20236c6b9e74SPeter Brune   if (!func) {
20246c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
20256c6b9e74SPeter Brune   }
20266c6b9e74SPeter Brune   /* if the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it.
20276c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
20286c6b9e74SPeter Brune    */
20290298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
20300298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
20310298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
20326c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
20336c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
20346c6b9e74SPeter Brune   }
2035277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2036277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2037277b19d0SLisandro Dalcin }
2038277b19d0SLisandro Dalcin 
2039277b19d0SLisandro Dalcin #undef __FUNCT__
2040f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp"
2041f6a906c0SBarry Smith /*@
2042f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
2043f6a906c0SBarry Smith    of an adjoint solver
2044f6a906c0SBarry Smith 
2045f6a906c0SBarry Smith    Collective on TS
2046f6a906c0SBarry Smith 
2047f6a906c0SBarry Smith    Input Parameter:
2048f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
2049f6a906c0SBarry Smith 
2050f6a906c0SBarry Smith    Level: advanced
2051f6a906c0SBarry Smith 
2052f6a906c0SBarry Smith .keywords: TS, timestep, setup
2053f6a906c0SBarry Smith 
2054947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
2055f6a906c0SBarry Smith @*/
2056f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
2057f6a906c0SBarry Smith {
2058f6a906c0SBarry Smith   PetscErrorCode ierr;
2059f6a906c0SBarry Smith 
2060f6a906c0SBarry Smith   PetscFunctionBegin;
2061f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2062f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
2063dfb21088SHong Zhang   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
2064d8151eeaSBarry Smith 
2065947abb85SHong Zhang   if (ts->vec_costintegral) { /* if there is integral in the cost function*/
2066d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2067d8151eeaSBarry Smith     if (ts->vecs_sensip){
2068d8151eeaSBarry Smith       ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2069d8151eeaSBarry Smith     }
2070947abb85SHong Zhang   }
2071d8151eeaSBarry Smith 
207242f2b339SBarry Smith   if (ts->ops->adjointsetup) {
207342f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
2074f6a906c0SBarry Smith   }
2075f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
2076f6a906c0SBarry Smith   PetscFunctionReturn(0);
2077f6a906c0SBarry Smith }
2078f6a906c0SBarry Smith 
2079f6a906c0SBarry Smith #undef __FUNCT__
2080277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
2081277b19d0SLisandro Dalcin /*@
2082277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2083277b19d0SLisandro Dalcin 
2084277b19d0SLisandro Dalcin    Collective on TS
2085277b19d0SLisandro Dalcin 
2086277b19d0SLisandro Dalcin    Input Parameter:
2087277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2088277b19d0SLisandro Dalcin 
2089277b19d0SLisandro Dalcin    Level: beginner
2090277b19d0SLisandro Dalcin 
2091277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2092277b19d0SLisandro Dalcin 
2093277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2094277b19d0SLisandro Dalcin @*/
2095277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2096277b19d0SLisandro Dalcin {
2097277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2098277b19d0SLisandro Dalcin 
2099277b19d0SLisandro Dalcin   PetscFunctionBegin;
2100277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2101b18ea86cSHong Zhang 
2102277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2103277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2104277b19d0SLisandro Dalcin   }
2105277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2106e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2107bbd56ea5SKarl Rupp 
21084e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
21094e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2110214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
21116bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2112e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2113e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
211438637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2115bbd56ea5SKarl Rupp 
2116947abb85SHong Zhang  if (ts->vec_costintegral) {
2117d8151eeaSBarry Smith     ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2118d8151eeaSBarry Smith     if (ts->vecs_drdp){
2119d8151eeaSBarry Smith       ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2120d8151eeaSBarry Smith     }
2121947abb85SHong Zhang   }
2122d8151eeaSBarry Smith   ts->vecs_sensi  = NULL;
2123d8151eeaSBarry Smith   ts->vecs_sensip = NULL;
2124ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
21252c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
212636eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2127277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2128d763cef2SBarry Smith   PetscFunctionReturn(0);
2129d763cef2SBarry Smith }
2130d763cef2SBarry Smith 
21314a2ae208SSatish Balay #undef __FUNCT__
21324a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
2133d8e5e3e6SSatish Balay /*@
2134d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2135d763cef2SBarry Smith    with TSCreate().
2136d763cef2SBarry Smith 
2137d763cef2SBarry Smith    Collective on TS
2138d763cef2SBarry Smith 
2139d763cef2SBarry Smith    Input Parameter:
2140d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2141d763cef2SBarry Smith 
2142d763cef2SBarry Smith    Level: beginner
2143d763cef2SBarry Smith 
2144d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2145d763cef2SBarry Smith 
2146d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2147d763cef2SBarry Smith @*/
21486bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2149d763cef2SBarry Smith {
21506849ba73SBarry Smith   PetscErrorCode ierr;
2151d763cef2SBarry Smith 
2152d763cef2SBarry Smith   PetscFunctionBegin;
21536bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
21546bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
21556bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2156d763cef2SBarry Smith 
21576bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2158277b19d0SLisandro Dalcin 
2159e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2160e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
21616bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
21626d4c513bSLisandro Dalcin 
2163bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2164bc952696SBarry Smith 
216584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
2166aeb4809dSShri Abhyankar   if ((*ts)->event) {
2167aeb4809dSShri Abhyankar     ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr);
2168aeb4809dSShri Abhyankar   }
21696bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
21706bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
21716bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
21720dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
21736d4c513bSLisandro Dalcin 
2174a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2175d763cef2SBarry Smith   PetscFunctionReturn(0);
2176d763cef2SBarry Smith }
2177d763cef2SBarry Smith 
21784a2ae208SSatish Balay #undef __FUNCT__
21794a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2180d8e5e3e6SSatish Balay /*@
2181d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2182d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2183d763cef2SBarry Smith 
2184d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2185d763cef2SBarry Smith 
2186d763cef2SBarry Smith    Input Parameter:
2187d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2188d763cef2SBarry Smith 
2189d763cef2SBarry Smith    Output Parameter:
2190d763cef2SBarry Smith .  snes - the nonlinear solver context
2191d763cef2SBarry Smith 
2192d763cef2SBarry Smith    Notes:
2193d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2194d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
219594b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2196d763cef2SBarry Smith 
2197d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
21980298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2199d763cef2SBarry Smith 
2200d763cef2SBarry Smith    Level: beginner
2201d763cef2SBarry Smith 
2202d763cef2SBarry Smith .keywords: timestep, get, SNES
2203d763cef2SBarry Smith @*/
22047087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2205d763cef2SBarry Smith {
2206d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2207d372ba47SLisandro Dalcin 
2208d763cef2SBarry Smith   PetscFunctionBegin;
22090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22104482741eSBarry Smith   PetscValidPointer(snes,2);
2211d372ba47SLisandro Dalcin   if (!ts->snes) {
2212ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
22130298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
22143bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2215d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2216496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
22179e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
22189e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
22199e2a6581SJed Brown     }
2220d372ba47SLisandro Dalcin   }
2221d763cef2SBarry Smith   *snes = ts->snes;
2222d763cef2SBarry Smith   PetscFunctionReturn(0);
2223d763cef2SBarry Smith }
2224d763cef2SBarry Smith 
22254a2ae208SSatish Balay #undef __FUNCT__
2226deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2227deb2cd25SJed Brown /*@
2228deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2229deb2cd25SJed Brown 
2230deb2cd25SJed Brown    Collective
2231deb2cd25SJed Brown 
2232deb2cd25SJed Brown    Input Parameter:
2233deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2234deb2cd25SJed Brown -  snes - the nonlinear solver context
2235deb2cd25SJed Brown 
2236deb2cd25SJed Brown    Notes:
2237deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2238deb2cd25SJed Brown 
2239deb2cd25SJed Brown    Level: developer
2240deb2cd25SJed Brown 
2241deb2cd25SJed Brown .keywords: timestep, set, SNES
2242deb2cd25SJed Brown @*/
2243deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2244deb2cd25SJed Brown {
2245deb2cd25SJed Brown   PetscErrorCode ierr;
2246d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2247deb2cd25SJed Brown 
2248deb2cd25SJed Brown   PetscFunctionBegin;
2249deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2250deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2251deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2252deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2253bbd56ea5SKarl Rupp 
2254deb2cd25SJed Brown   ts->snes = snes;
2255bbd56ea5SKarl Rupp 
22560298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
22570298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2258740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
22590298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2260740132f1SEmil Constantinescu   }
2261deb2cd25SJed Brown   PetscFunctionReturn(0);
2262deb2cd25SJed Brown }
2263deb2cd25SJed Brown 
2264deb2cd25SJed Brown #undef __FUNCT__
226594b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2266d8e5e3e6SSatish Balay /*@
226794b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2268d763cef2SBarry Smith    a TS (timestepper) context.
2269d763cef2SBarry Smith 
227094b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2271d763cef2SBarry Smith 
2272d763cef2SBarry Smith    Input Parameter:
2273d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2274d763cef2SBarry Smith 
2275d763cef2SBarry Smith    Output Parameter:
227694b7f48cSBarry Smith .  ksp - the nonlinear solver context
2277d763cef2SBarry Smith 
2278d763cef2SBarry Smith    Notes:
227994b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2280d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2281d763cef2SBarry Smith    KSP and PC contexts as well.
2282d763cef2SBarry Smith 
228394b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
22840298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2285d763cef2SBarry Smith 
2286d763cef2SBarry Smith    Level: beginner
2287d763cef2SBarry Smith 
228894b7f48cSBarry Smith .keywords: timestep, get, KSP
2289d763cef2SBarry Smith @*/
22907087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2291d763cef2SBarry Smith {
2292d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2293089b2837SJed Brown   SNES           snes;
2294d372ba47SLisandro Dalcin 
2295d763cef2SBarry Smith   PetscFunctionBegin;
22960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22974482741eSBarry Smith   PetscValidPointer(ksp,2);
229817186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2299e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2300089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2301089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2302d763cef2SBarry Smith   PetscFunctionReturn(0);
2303d763cef2SBarry Smith }
2304d763cef2SBarry Smith 
2305d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2306d763cef2SBarry Smith 
23074a2ae208SSatish Balay #undef __FUNCT__
2308adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2309adb62b0dSMatthew Knepley /*@
2310adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2311adb62b0dSMatthew Knepley    maximum time for iteration.
2312adb62b0dSMatthew Knepley 
23133f9fe445SBarry Smith    Not Collective
2314adb62b0dSMatthew Knepley 
2315adb62b0dSMatthew Knepley    Input Parameters:
2316adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
23170298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
23180298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2319adb62b0dSMatthew Knepley 
2320adb62b0dSMatthew Knepley    Level: intermediate
2321adb62b0dSMatthew Knepley 
2322adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2323adb62b0dSMatthew Knepley @*/
23247087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2325adb62b0dSMatthew Knepley {
2326adb62b0dSMatthew Knepley   PetscFunctionBegin;
23270700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2328abc0a331SBarry Smith   if (maxsteps) {
23294482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2330adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2331adb62b0dSMatthew Knepley   }
2332abc0a331SBarry Smith   if (maxtime) {
23334482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2334adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2335adb62b0dSMatthew Knepley   }
2336adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2337adb62b0dSMatthew Knepley }
2338adb62b0dSMatthew Knepley 
2339adb62b0dSMatthew Knepley #undef __FUNCT__
23404a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2341d763cef2SBarry Smith /*@
2342d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2343d763cef2SBarry Smith    maximum time for iteration.
2344d763cef2SBarry Smith 
23453f9fe445SBarry Smith    Logically Collective on TS
2346d763cef2SBarry Smith 
2347d763cef2SBarry Smith    Input Parameters:
2348d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2349d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2350d763cef2SBarry Smith -  maxtime - final time to iterate to
2351d763cef2SBarry Smith 
2352d763cef2SBarry Smith    Options Database Keys:
2353d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
23543bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2355d763cef2SBarry Smith 
2356d763cef2SBarry Smith    Notes:
2357d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2358d763cef2SBarry Smith 
2359d763cef2SBarry Smith    Level: intermediate
2360d763cef2SBarry Smith 
2361d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2362a43b19c4SJed Brown 
2363a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2364d763cef2SBarry Smith @*/
23657087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2366d763cef2SBarry Smith {
2367d763cef2SBarry Smith   PetscFunctionBegin;
23680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2369c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2370c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
237139b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
237239b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2373d763cef2SBarry Smith   PetscFunctionReturn(0);
2374d763cef2SBarry Smith }
2375d763cef2SBarry Smith 
23764a2ae208SSatish Balay #undef __FUNCT__
23774a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2378d763cef2SBarry Smith /*@
2379d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2380d763cef2SBarry Smith    for use by the TS routines.
2381d763cef2SBarry Smith 
23823f9fe445SBarry Smith    Logically Collective on TS and Vec
2383d763cef2SBarry Smith 
2384d763cef2SBarry Smith    Input Parameters:
2385d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
23860910c330SBarry Smith -  u - the solution vector
2387d763cef2SBarry Smith 
2388d763cef2SBarry Smith    Level: beginner
2389d763cef2SBarry Smith 
2390d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2391d763cef2SBarry Smith @*/
23920910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2393d763cef2SBarry Smith {
23948737fe31SLisandro Dalcin   PetscErrorCode ierr;
2395496e6a7aSJed Brown   DM             dm;
23968737fe31SLisandro Dalcin 
2397d763cef2SBarry Smith   PetscFunctionBegin;
23980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23990910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
24000910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
24016bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2402bbd56ea5SKarl Rupp 
24030910c330SBarry Smith   ts->vec_sol = u;
2404bbd56ea5SKarl Rupp 
2405496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
24060910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2407d763cef2SBarry Smith   PetscFunctionReturn(0);
2408d763cef2SBarry Smith }
2409d763cef2SBarry Smith 
2410e74ef692SMatthew Knepley #undef __FUNCT__
241173b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps"
241273b18844SBarry Smith /*@
241373b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
241473b18844SBarry Smith 
241573b18844SBarry Smith    Logically Collective on TS
241673b18844SBarry Smith 
241773b18844SBarry Smith    Input Parameters:
241873b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
241973b18844SBarry Smith .  steps - number of steps to use
242073b18844SBarry Smith 
242173b18844SBarry Smith    Level: intermediate
242273b18844SBarry Smith 
242373b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
242473b18844SBarry Smith           so as to integrate back to less than the original timestep
242573b18844SBarry Smith 
242673b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
242773b18844SBarry Smith 
242873b18844SBarry Smith .seealso: TSSetExactFinalTime()
242973b18844SBarry Smith @*/
243073b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
243173b18844SBarry Smith {
243273b18844SBarry Smith   PetscFunctionBegin;
243373b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
243473b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
243573b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
243673b18844SBarry 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");
243773b18844SBarry Smith   ts->adjoint_max_steps = steps;
243873b18844SBarry Smith   PetscFunctionReturn(0);
243973b18844SBarry Smith }
244073b18844SBarry Smith 
244173b18844SBarry Smith #undef __FUNCT__
2442dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients"
2443c235aa8dSHong Zhang /*@
2444dfb21088SHong 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
24450cf82b59SBarry Smith       for use by the TSAdjoint routines.
2446c235aa8dSHong Zhang 
2447c235aa8dSHong Zhang    Logically Collective on TS and Vec
2448c235aa8dSHong Zhang 
2449c235aa8dSHong Zhang    Input Parameters:
2450c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2451abc2977eSBarry 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
2452abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
2453c235aa8dSHong Zhang 
2454c235aa8dSHong Zhang    Level: beginner
2455c235aa8dSHong Zhang 
2456abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
24570cf82b59SBarry Smith 
2458c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2459c235aa8dSHong Zhang @*/
2460dfb21088SHong Zhang PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
2461c235aa8dSHong Zhang {
2462c235aa8dSHong Zhang   PetscFunctionBegin;
2463c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2464abc2977eSBarry Smith   PetscValidPointer(lambda,2);
2465abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
2466abc2977eSBarry Smith   ts->vecs_sensip = mu;
2467dfb21088SHong 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");
2468abc2977eSBarry Smith   ts->numcost  = numcost;
2469ad8e2604SHong Zhang   PetscFunctionReturn(0);
2470ad8e2604SHong Zhang }
2471ad8e2604SHong Zhang 
2472ad8e2604SHong Zhang #undef __FUNCT__
24735bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian"
2474ad8e2604SHong Zhang /*@C
24750cf82b59SBarry 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.
2476ad8e2604SHong Zhang 
2477ad8e2604SHong Zhang   Logically Collective on TS
2478ad8e2604SHong Zhang 
2479ad8e2604SHong Zhang   Input Parameters:
2480ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2481ad8e2604SHong Zhang - func - The function
2482ad8e2604SHong Zhang 
2483ad8e2604SHong Zhang   Calling sequence of func:
24840cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
248505755b9cSHong Zhang +   t - current timestep
24860cf82b59SBarry Smith .   y - input vector (current ODE solution)
248705755b9cSHong Zhang .   A - output matrix
248805755b9cSHong Zhang -   ctx - [optional] user-defined function context
2489ad8e2604SHong Zhang 
2490ad8e2604SHong Zhang   Level: intermediate
2491ad8e2604SHong Zhang 
24920cf82b59SBarry 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
24930cf82b59SBarry Smith 
2494ad8e2604SHong Zhang .keywords: TS, sensitivity
2495ad8e2604SHong Zhang .seealso:
2496ad8e2604SHong Zhang @*/
24975bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2498ad8e2604SHong Zhang {
2499ad8e2604SHong Zhang   PetscErrorCode ierr;
2500ad8e2604SHong Zhang 
2501ad8e2604SHong Zhang   PetscFunctionBegin;
2502ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2503ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
2504ad8e2604SHong Zhang 
2505ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
2506ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
2507ad8e2604SHong Zhang   if(Amat) {
2508ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
2509ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2510ad8e2604SHong Zhang     ts->Jacp = Amat;
2511ad8e2604SHong Zhang   }
2512ad8e2604SHong Zhang   PetscFunctionReturn(0);
2513ad8e2604SHong Zhang }
2514ad8e2604SHong Zhang 
2515ad8e2604SHong Zhang #undef __FUNCT__
25165bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian"
25170cf82b59SBarry Smith /*@C
25185bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
2519ad8e2604SHong Zhang 
2520ad8e2604SHong Zhang   Collective on TS
2521ad8e2604SHong Zhang 
2522ad8e2604SHong Zhang   Input Parameters:
2523ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
2524ad8e2604SHong Zhang 
2525ad8e2604SHong Zhang   Level: developer
2526ad8e2604SHong Zhang 
2527ad8e2604SHong Zhang .keywords: TS, sensitivity
25285bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
2529ad8e2604SHong Zhang @*/
25305bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
2531ad8e2604SHong Zhang {
2532ad8e2604SHong Zhang   PetscErrorCode ierr;
2533ad8e2604SHong Zhang 
2534ad8e2604SHong Zhang   PetscFunctionBegin;
2535ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2536ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
2537ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
2538ad8e2604SHong Zhang 
2539ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
2540ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
2541ad8e2604SHong Zhang   PetscStackPop;
2542c235aa8dSHong Zhang   PetscFunctionReturn(0);
2543c235aa8dSHong Zhang }
2544c235aa8dSHong Zhang 
2545c235aa8dSHong Zhang #undef __FUNCT__
2546dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand"
25476fd0887fSHong Zhang /*@C
2548dfb21088SHong Zhang     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
25496fd0887fSHong Zhang 
25506fd0887fSHong Zhang     Logically Collective on TS
25516fd0887fSHong Zhang 
25526fd0887fSHong Zhang     Input Parameters:
25536fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
2554abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
25550cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
2556b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
2557b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
2558*b1cb36f3SHong Zhang .   fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
2559b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
25606fd0887fSHong Zhang 
25610cf82b59SBarry Smith     Calling sequence of rf:
25620cf82b59SBarry Smith $     rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx);
25636fd0887fSHong Zhang 
25646fd0887fSHong Zhang +   t - current timestep
25650cf82b59SBarry Smith .   y - input vector
25660cf82b59SBarry Smith .   f - function result; one vector entry for each cost function
25676fd0887fSHong Zhang -   ctx - [optional] user-defined function context
25686fd0887fSHong Zhang 
2569b612ec54SBarry Smith    Calling sequence of drdyf:
25700cf82b59SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
2571b612ec54SBarry Smith 
2572b612ec54SBarry Smith    Calling sequence of drdpf:
25730cf82b59SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
2574b612ec54SBarry Smith 
2575b612ec54SBarry Smith     Level: intermediate
25766fd0887fSHong Zhang 
2577abc2977eSBarry Smith     Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions
25780cf82b59SBarry Smith 
25796fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
25806fd0887fSHong Zhang 
2581dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
25826fd0887fSHong Zhang @*/
2583dfb21088SHong Zhang PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
2584d8151eeaSBarry Smith                                                           PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
2585*b1cb36f3SHong Zhang                                                           PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),
2586*b1cb36f3SHong Zhang                                                           PetscBool fwd,void *ctx)
25876fd0887fSHong Zhang {
25886fd0887fSHong Zhang   PetscErrorCode ierr;
25896fd0887fSHong Zhang 
25906fd0887fSHong Zhang   PetscFunctionBegin;
25916fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2592dfb21088SHong 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()");
2593c72ed514SHong Zhang   if (!ts->numcost) ts->numcost=numcost;
25946fd0887fSHong Zhang 
2595*b1cb36f3SHong Zhang   ts->costintegralfwd  = fwd; /* Evaluate the cost integral in forward run if fwd is true */
2596abc2977eSBarry Smith   ierr                 = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
25972c39e106SBarry Smith   ierr                 = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
25980cf82b59SBarry Smith   ts->costintegrand    = rf;
259905755b9cSHong Zhang   ts->costintegrandctx = ctx;
2600b612ec54SBarry Smith   ts->drdyfunction     = drdyf;
2601b612ec54SBarry Smith   ts->drdpfunction     = drdpf;
26026fd0887fSHong Zhang   PetscFunctionReturn(0);
26036fd0887fSHong Zhang }
26046fd0887fSHong Zhang 
26056fd0887fSHong Zhang #undef __FUNCT__
2606dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral"
260736eaed60SHong Zhang /*@
2608dfb21088SHong Zhang    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
260936eaed60SHong Zhang    It is valid to call the routine after a backward run.
261036eaed60SHong Zhang 
261136eaed60SHong Zhang    Not Collective
261236eaed60SHong Zhang 
261336eaed60SHong Zhang    Input Parameter:
261436eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
261536eaed60SHong Zhang 
261636eaed60SHong Zhang    Output Parameter:
26172c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
261836eaed60SHong Zhang 
261936eaed60SHong Zhang    Level: intermediate
262036eaed60SHong Zhang 
2621dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
262236eaed60SHong Zhang 
262336eaed60SHong Zhang .keywords: TS, sensitivity analysis
262436eaed60SHong Zhang @*/
2625dfb21088SHong Zhang PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
262636eaed60SHong Zhang {
262736eaed60SHong Zhang   PetscFunctionBegin;
262836eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
262936eaed60SHong Zhang   PetscValidPointer(v,2);
26302c39e106SBarry Smith   *v = ts->vec_costintegral;
263136eaed60SHong Zhang   PetscFunctionReturn(0);
263236eaed60SHong Zhang }
263336eaed60SHong Zhang 
263436eaed60SHong Zhang #undef __FUNCT__
2635d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand"
26366fd0887fSHong Zhang /*@
26372c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
26386fd0887fSHong Zhang 
26396fd0887fSHong Zhang    Input Parameters:
26406fd0887fSHong Zhang +  ts - the TS context
26416fd0887fSHong Zhang .  t - current time
26420cf82b59SBarry Smith -  y - state vector, i.e. current solution
26436fd0887fSHong Zhang 
26446fd0887fSHong Zhang    Output Parameter:
2645abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
26466fd0887fSHong Zhang 
26476fd0887fSHong Zhang    Note:
26486fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
26496fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
26506fd0887fSHong Zhang 
26516fd0887fSHong Zhang    Level: developer
26526fd0887fSHong Zhang 
26536fd0887fSHong Zhang .keywords: TS, compute
26546fd0887fSHong Zhang 
2655dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
26566fd0887fSHong Zhang @*/
26570cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
26586fd0887fSHong Zhang {
26596fd0887fSHong Zhang   PetscErrorCode ierr;
26606fd0887fSHong Zhang 
26616fd0887fSHong Zhang   PetscFunctionBegin;
26626fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
26630cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
26646fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
26656fd0887fSHong Zhang 
26660cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
2667e4680132SHong Zhang   if (ts->costintegrand) {
2668e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
26690cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
26706fd0887fSHong Zhang     PetscStackPop;
26716fd0887fSHong Zhang   } else {
26726fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
26736fd0887fSHong Zhang   }
26746fd0887fSHong Zhang 
26750cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
26766fd0887fSHong Zhang   PetscFunctionReturn(0);
26776fd0887fSHong Zhang }
26786fd0887fSHong Zhang 
26796fd0887fSHong Zhang #undef __FUNCT__
2680d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction"
268105755b9cSHong Zhang /*@
2682d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
268305755b9cSHong Zhang 
268405755b9cSHong Zhang   Collective on TS
268505755b9cSHong Zhang 
268605755b9cSHong Zhang   Input Parameters:
268705755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
268805755b9cSHong Zhang 
268905755b9cSHong Zhang   Notes:
2690d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
269105755b9cSHong Zhang   so most users would not generally call this routine themselves.
269205755b9cSHong Zhang 
269305755b9cSHong Zhang   Level: developer
269405755b9cSHong Zhang 
269505755b9cSHong Zhang .keywords: TS, sensitivity
2696d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
269705755b9cSHong Zhang @*/
26980cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
269905755b9cSHong Zhang {
270005755b9cSHong Zhang   PetscErrorCode ierr;
270105755b9cSHong Zhang 
270205755b9cSHong Zhang   PetscFunctionBegin;
270305755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27040cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
270505755b9cSHong Zhang 
270605755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
27070cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
270805755b9cSHong Zhang   PetscStackPop;
270905755b9cSHong Zhang   PetscFunctionReturn(0);
271005755b9cSHong Zhang }
271105755b9cSHong Zhang 
271205755b9cSHong Zhang #undef __FUNCT__
2713d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction"
271405755b9cSHong Zhang /*@
2715d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
271605755b9cSHong Zhang 
271705755b9cSHong Zhang   Collective on TS
271805755b9cSHong Zhang 
271905755b9cSHong Zhang   Input Parameters:
272005755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
272105755b9cSHong Zhang 
272205755b9cSHong Zhang   Notes:
272305755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
272405755b9cSHong Zhang   so most users would not generally call this routine themselves.
272505755b9cSHong Zhang 
272605755b9cSHong Zhang   Level: developer
272705755b9cSHong Zhang 
272805755b9cSHong Zhang .keywords: TS, sensitivity
2729d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
273005755b9cSHong Zhang @*/
27310cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
273205755b9cSHong Zhang {
273305755b9cSHong Zhang   PetscErrorCode ierr;
273405755b9cSHong Zhang 
273505755b9cSHong Zhang   PetscFunctionBegin;
273605755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27370cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
273805755b9cSHong Zhang 
273905755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
27400cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
274105755b9cSHong Zhang   PetscStackPop;
274205755b9cSHong Zhang   PetscFunctionReturn(0);
274305755b9cSHong Zhang }
274405755b9cSHong Zhang 
274505755b9cSHong Zhang #undef __FUNCT__
2746e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2747ac226902SBarry Smith /*@C
2748000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
27493f2090d5SJed Brown   called once at the beginning of each time step.
2750000e7ae3SMatthew Knepley 
27513f9fe445SBarry Smith   Logically Collective on TS
2752000e7ae3SMatthew Knepley 
2753000e7ae3SMatthew Knepley   Input Parameters:
2754000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2755000e7ae3SMatthew Knepley - func - The function
2756000e7ae3SMatthew Knepley 
2757000e7ae3SMatthew Knepley   Calling sequence of func:
2758000e7ae3SMatthew Knepley . func (TS ts);
2759000e7ae3SMatthew Knepley 
2760000e7ae3SMatthew Knepley   Level: intermediate
2761000e7ae3SMatthew Knepley 
2762b8123daeSJed Brown   Note:
2763b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2764b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2765b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2766b8123daeSJed Brown 
2767000e7ae3SMatthew Knepley .keywords: TS, timestep
27689be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2769000e7ae3SMatthew Knepley @*/
27707087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2771000e7ae3SMatthew Knepley {
2772000e7ae3SMatthew Knepley   PetscFunctionBegin;
27730700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2774ae60f76fSBarry Smith   ts->prestep = func;
2775000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2776000e7ae3SMatthew Knepley }
2777000e7ae3SMatthew Knepley 
2778e74ef692SMatthew Knepley #undef __FUNCT__
27793f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
278009ee8438SJed Brown /*@
27813f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
27823f2090d5SJed Brown 
27833f2090d5SJed Brown   Collective on TS
27843f2090d5SJed Brown 
27853f2090d5SJed Brown   Input Parameters:
27863f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
27873f2090d5SJed Brown 
27883f2090d5SJed Brown   Notes:
27893f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
27903f2090d5SJed Brown   so most users would not generally call this routine themselves.
27913f2090d5SJed Brown 
27923f2090d5SJed Brown   Level: developer
27933f2090d5SJed Brown 
27943f2090d5SJed Brown .keywords: TS, timestep
27959be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
27963f2090d5SJed Brown @*/
27977087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
27983f2090d5SJed Brown {
27993f2090d5SJed Brown   PetscErrorCode ierr;
28003f2090d5SJed Brown 
28013f2090d5SJed Brown   PetscFunctionBegin;
28020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2803ae60f76fSBarry Smith   if (ts->prestep) {
2804ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2805312ce896SJed Brown   }
28063f2090d5SJed Brown   PetscFunctionReturn(0);
28073f2090d5SJed Brown }
28083f2090d5SJed Brown 
28093f2090d5SJed Brown #undef __FUNCT__
2810b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2811b8123daeSJed Brown /*@C
2812b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2813b8123daeSJed Brown   called once at the beginning of each stage.
2814b8123daeSJed Brown 
2815b8123daeSJed Brown   Logically Collective on TS
2816b8123daeSJed Brown 
2817b8123daeSJed Brown   Input Parameters:
2818b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2819b8123daeSJed Brown - func - The function
2820b8123daeSJed Brown 
2821b8123daeSJed Brown   Calling sequence of func:
2822b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2823b8123daeSJed Brown 
2824b8123daeSJed Brown   Level: intermediate
2825b8123daeSJed Brown 
2826b8123daeSJed Brown   Note:
2827b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2828b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2829b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2830b8123daeSJed Brown 
2831b8123daeSJed Brown .keywords: TS, timestep
28329be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2833b8123daeSJed Brown @*/
2834b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2835b8123daeSJed Brown {
2836b8123daeSJed Brown   PetscFunctionBegin;
2837b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2838ae60f76fSBarry Smith   ts->prestage = func;
2839b8123daeSJed Brown   PetscFunctionReturn(0);
2840b8123daeSJed Brown }
2841b8123daeSJed Brown 
2842b8123daeSJed Brown #undef __FUNCT__
28439be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
28449be3e283SDebojyoti Ghosh /*@C
28459be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
28469be3e283SDebojyoti Ghosh   called once at the end of each stage.
28479be3e283SDebojyoti Ghosh 
28489be3e283SDebojyoti Ghosh   Logically Collective on TS
28499be3e283SDebojyoti Ghosh 
28509be3e283SDebojyoti Ghosh   Input Parameters:
28519be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
28529be3e283SDebojyoti Ghosh - func - The function
28539be3e283SDebojyoti Ghosh 
28549be3e283SDebojyoti Ghosh   Calling sequence of func:
28559be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
28569be3e283SDebojyoti Ghosh 
28579be3e283SDebojyoti Ghosh   Level: intermediate
28589be3e283SDebojyoti Ghosh 
28599be3e283SDebojyoti Ghosh   Note:
28609be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
28619be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
28629be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
28639be3e283SDebojyoti Ghosh 
28649be3e283SDebojyoti Ghosh .keywords: TS, timestep
28659be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
28669be3e283SDebojyoti Ghosh @*/
28679be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
28689be3e283SDebojyoti Ghosh {
28699be3e283SDebojyoti Ghosh   PetscFunctionBegin;
28709be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
28719be3e283SDebojyoti Ghosh   ts->poststage = func;
28729be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
28739be3e283SDebojyoti Ghosh }
28749be3e283SDebojyoti Ghosh 
28759be3e283SDebojyoti Ghosh #undef __FUNCT__
2876b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2877b8123daeSJed Brown /*@
2878b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2879b8123daeSJed Brown 
2880b8123daeSJed Brown   Collective on TS
2881b8123daeSJed Brown 
2882b8123daeSJed Brown   Input Parameters:
2883b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
28849be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2885b8123daeSJed Brown 
2886b8123daeSJed Brown   Notes:
2887b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2888b8123daeSJed Brown   most users would not generally call this routine themselves.
2889b8123daeSJed Brown 
2890b8123daeSJed Brown   Level: developer
2891b8123daeSJed Brown 
2892b8123daeSJed Brown .keywords: TS, timestep
28939be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2894b8123daeSJed Brown @*/
2895b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2896b8123daeSJed Brown {
2897b8123daeSJed Brown   PetscErrorCode ierr;
2898b8123daeSJed Brown 
2899b8123daeSJed Brown   PetscFunctionBegin;
2900b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2901ae60f76fSBarry Smith   if (ts->prestage) {
2902ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2903b8123daeSJed Brown   }
2904b8123daeSJed Brown   PetscFunctionReturn(0);
2905b8123daeSJed Brown }
2906b8123daeSJed Brown 
2907b8123daeSJed Brown #undef __FUNCT__
29089be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
29099be3e283SDebojyoti Ghosh /*@
29109be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
29119be3e283SDebojyoti Ghosh 
29129be3e283SDebojyoti Ghosh   Collective on TS
29139be3e283SDebojyoti Ghosh 
29149be3e283SDebojyoti Ghosh   Input Parameters:
29159be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
29169be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
29179be3e283SDebojyoti Ghosh   stageindex  - Stage number
29189be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
29199be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
29209be3e283SDebojyoti Ghosh 
29219be3e283SDebojyoti Ghosh   Notes:
29229be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
29239be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
29249be3e283SDebojyoti Ghosh 
29259be3e283SDebojyoti Ghosh   Level: developer
29269be3e283SDebojyoti Ghosh 
29279be3e283SDebojyoti Ghosh .keywords: TS, timestep
29289be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
29299be3e283SDebojyoti Ghosh @*/
29309be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
29319be3e283SDebojyoti Ghosh {
29329be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
29339be3e283SDebojyoti Ghosh 
29349be3e283SDebojyoti Ghosh   PetscFunctionBegin;
29359be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29364beae5d8SLisandro Dalcin   if (ts->poststage) {
29379be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
29389be3e283SDebojyoti Ghosh   }
29399be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
29409be3e283SDebojyoti Ghosh }
29419be3e283SDebojyoti Ghosh 
29429be3e283SDebojyoti Ghosh #undef __FUNCT__
2943e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2944ac226902SBarry Smith /*@C
2945000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
29463f2090d5SJed Brown   called once at the end of each time step.
2947000e7ae3SMatthew Knepley 
29483f9fe445SBarry Smith   Logically Collective on TS
2949000e7ae3SMatthew Knepley 
2950000e7ae3SMatthew Knepley   Input Parameters:
2951000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2952000e7ae3SMatthew Knepley - func - The function
2953000e7ae3SMatthew Knepley 
2954000e7ae3SMatthew Knepley   Calling sequence of func:
2955b8123daeSJed Brown $ func (TS ts);
2956000e7ae3SMatthew Knepley 
2957000e7ae3SMatthew Knepley   Level: intermediate
2958000e7ae3SMatthew Knepley 
2959000e7ae3SMatthew Knepley .keywords: TS, timestep
2960b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2961000e7ae3SMatthew Knepley @*/
29627087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2963000e7ae3SMatthew Knepley {
2964000e7ae3SMatthew Knepley   PetscFunctionBegin;
29650700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2966ae60f76fSBarry Smith   ts->poststep = func;
2967000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2968000e7ae3SMatthew Knepley }
2969000e7ae3SMatthew Knepley 
2970e74ef692SMatthew Knepley #undef __FUNCT__
29713f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
297209ee8438SJed Brown /*@
29733f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
29743f2090d5SJed Brown 
29753f2090d5SJed Brown   Collective on TS
29763f2090d5SJed Brown 
29773f2090d5SJed Brown   Input Parameters:
29783f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
29793f2090d5SJed Brown 
29803f2090d5SJed Brown   Notes:
29813f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
29823f2090d5SJed Brown   so most users would not generally call this routine themselves.
29833f2090d5SJed Brown 
29843f2090d5SJed Brown   Level: developer
29853f2090d5SJed Brown 
29863f2090d5SJed Brown .keywords: TS, timestep
29873f2090d5SJed Brown @*/
29887087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
29893f2090d5SJed Brown {
29903f2090d5SJed Brown   PetscErrorCode ierr;
29913f2090d5SJed Brown 
29923f2090d5SJed Brown   PetscFunctionBegin;
29930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2994ae60f76fSBarry Smith   if (ts->poststep) {
2995ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
299672ac3e02SJed Brown   }
29973f2090d5SJed Brown   PetscFunctionReturn(0);
29983f2090d5SJed Brown }
29993f2090d5SJed Brown 
3000d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3001d763cef2SBarry Smith 
30024a2ae208SSatish Balay #undef __FUNCT__
3003a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
3004d763cef2SBarry Smith /*@C
3005a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3006d763cef2SBarry Smith    timestep to display the iteration's  progress.
3007d763cef2SBarry Smith 
30083f9fe445SBarry Smith    Logically Collective on TS
3009d763cef2SBarry Smith 
3010d763cef2SBarry Smith    Input Parameters:
3011d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3012e213d8f1SJed Brown .  monitor - monitoring routine
3013329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
30140298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3015b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
30160298fd71SBarry Smith           (may be NULL)
3017d763cef2SBarry Smith 
3018e213d8f1SJed Brown    Calling sequence of monitor:
30190910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3020d763cef2SBarry Smith 
3021d763cef2SBarry Smith +    ts - the TS context
302288c05cc5SBarry Smith .    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
302388c05cc5SBarry Smith                                been interpolated to)
30241f06c33eSBarry Smith .    time - current time
30250910c330SBarry Smith .    u - current iterate
3026d763cef2SBarry Smith -    mctx - [optional] monitoring context
3027d763cef2SBarry Smith 
3028d763cef2SBarry Smith    Notes:
3029d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3030d763cef2SBarry Smith    already has been loaded.
3031d763cef2SBarry Smith 
3032025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
3033025f1a04SBarry Smith 
3034d763cef2SBarry Smith    Level: intermediate
3035d763cef2SBarry Smith 
3036d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3037d763cef2SBarry Smith 
3038a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3039d763cef2SBarry Smith @*/
3040c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3041d763cef2SBarry Smith {
3042d763cef2SBarry Smith   PetscFunctionBegin;
30430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
304417186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3045d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
30468704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3047d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3048d763cef2SBarry Smith   PetscFunctionReturn(0);
3049d763cef2SBarry Smith }
3050d763cef2SBarry Smith 
30514a2ae208SSatish Balay #undef __FUNCT__
3052a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
3053d763cef2SBarry Smith /*@C
3054a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3055d763cef2SBarry Smith 
30563f9fe445SBarry Smith    Logically Collective on TS
3057d763cef2SBarry Smith 
3058d763cef2SBarry Smith    Input Parameters:
3059d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3060d763cef2SBarry Smith 
3061d763cef2SBarry Smith    Notes:
3062d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3063d763cef2SBarry Smith 
3064d763cef2SBarry Smith    Level: intermediate
3065d763cef2SBarry Smith 
3066d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3067d763cef2SBarry Smith 
3068a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3069d763cef2SBarry Smith @*/
30707087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3071d763cef2SBarry Smith {
3072d952e501SBarry Smith   PetscErrorCode ierr;
3073d952e501SBarry Smith   PetscInt       i;
3074d952e501SBarry Smith 
3075d763cef2SBarry Smith   PetscFunctionBegin;
30760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3077d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
30788704b422SBarry Smith     if (ts->monitordestroy[i]) {
30798704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3080d952e501SBarry Smith     }
3081d952e501SBarry Smith   }
3082d763cef2SBarry Smith   ts->numbermonitors = 0;
3083d763cef2SBarry Smith   PetscFunctionReturn(0);
3084d763cef2SBarry Smith }
3085d763cef2SBarry Smith 
30864a2ae208SSatish Balay #undef __FUNCT__
3087a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
3088d8e5e3e6SSatish Balay /*@
3089a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
30905516499fSSatish Balay 
30915516499fSSatish Balay    Level: intermediate
309241251cbbSSatish Balay 
30935516499fSSatish Balay .keywords: TS, set, monitor
30945516499fSSatish Balay 
309541251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
309641251cbbSSatish Balay @*/
3097649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
3098d763cef2SBarry Smith {
3099dfbe8321SBarry Smith   PetscErrorCode ierr;
31004d4332d5SBarry Smith   PetscViewer    viewer =  (PetscViewer) dummy;
3101d132466eSBarry Smith 
3102d763cef2SBarry Smith   PetscFunctionBegin;
31034d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3104649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
31058392e04aSShri 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);
3106649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3107d763cef2SBarry Smith   PetscFunctionReturn(0);
3108d763cef2SBarry Smith }
3109d763cef2SBarry Smith 
31104a2ae208SSatish Balay #undef __FUNCT__
31119110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet"
31129110b2e7SHong Zhang /*@C
31139110b2e7SHong Zhang    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
31149110b2e7SHong Zhang    timestep to display the iteration's  progress.
31159110b2e7SHong Zhang 
31169110b2e7SHong Zhang    Logically Collective on TS
31179110b2e7SHong Zhang 
31189110b2e7SHong Zhang    Input Parameters:
31199110b2e7SHong Zhang +  ts - the TS context obtained from TSCreate()
31209110b2e7SHong Zhang .  adjointmonitor - monitoring routine
31219110b2e7SHong Zhang .  adjointmctx - [optional] user-defined context for private data for the
31229110b2e7SHong Zhang              monitor routine (use NULL if no context is desired)
31239110b2e7SHong Zhang -  adjointmonitordestroy - [optional] routine that frees monitor context
31249110b2e7SHong Zhang           (may be NULL)
31259110b2e7SHong Zhang 
31269110b2e7SHong Zhang    Calling sequence of monitor:
31279110b2e7SHong Zhang $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)
31289110b2e7SHong Zhang 
31299110b2e7SHong Zhang +    ts - the TS context
31309110b2e7SHong 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
31319110b2e7SHong Zhang                                been interpolated to)
31329110b2e7SHong Zhang .    time - current time
31339110b2e7SHong Zhang .    u - current iterate
31349110b2e7SHong Zhang .    numcost - number of cost functionos
31359110b2e7SHong Zhang .    lambda - sensitivities to initial conditions
31369110b2e7SHong Zhang .    mu - sensitivities to parameters
31379110b2e7SHong Zhang -    adjointmctx - [optional] adjoint monitoring context
31389110b2e7SHong Zhang 
31399110b2e7SHong Zhang    Notes:
31409110b2e7SHong Zhang    This routine adds an additional monitor to the list of monitors that
31419110b2e7SHong Zhang    already has been loaded.
31429110b2e7SHong Zhang 
31439110b2e7SHong Zhang    Fortran notes: Only a single monitor function can be set for each TS object
31449110b2e7SHong Zhang 
31459110b2e7SHong Zhang    Level: intermediate
31469110b2e7SHong Zhang 
31479110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
31489110b2e7SHong Zhang 
31499110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel()
31509110b2e7SHong Zhang @*/
31519110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
31529110b2e7SHong Zhang {
31539110b2e7SHong Zhang   PetscFunctionBegin;
31549110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31559110b2e7SHong Zhang   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
31569110b2e7SHong Zhang   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
31579110b2e7SHong Zhang   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
31589110b2e7SHong Zhang   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
31599110b2e7SHong Zhang   PetscFunctionReturn(0);
31609110b2e7SHong Zhang }
31619110b2e7SHong Zhang 
31629110b2e7SHong Zhang #undef __FUNCT__
31639110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel"
31649110b2e7SHong Zhang /*@C
31659110b2e7SHong Zhang    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.
31669110b2e7SHong Zhang 
31679110b2e7SHong Zhang    Logically Collective on TS
31689110b2e7SHong Zhang 
31699110b2e7SHong Zhang    Input Parameters:
31709110b2e7SHong Zhang .  ts - the TS context obtained from TSCreate()
31719110b2e7SHong Zhang 
31729110b2e7SHong Zhang    Notes:
31739110b2e7SHong Zhang    There is no way to remove a single, specific monitor.
31749110b2e7SHong Zhang 
31759110b2e7SHong Zhang    Level: intermediate
31769110b2e7SHong Zhang 
31779110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
31789110b2e7SHong Zhang 
31799110b2e7SHong Zhang .seealso: TSAdjointMonitorSet()
31809110b2e7SHong Zhang @*/
31819110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorCancel(TS ts)
31829110b2e7SHong Zhang {
31839110b2e7SHong Zhang   PetscErrorCode ierr;
31849110b2e7SHong Zhang   PetscInt       i;
31859110b2e7SHong Zhang 
31869110b2e7SHong Zhang   PetscFunctionBegin;
31879110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31889110b2e7SHong Zhang   for (i=0; i<ts->numberadjointmonitors; i++) {
31899110b2e7SHong Zhang     if (ts->adjointmonitordestroy[i]) {
31909110b2e7SHong Zhang       ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
31919110b2e7SHong Zhang     }
31929110b2e7SHong Zhang   }
31939110b2e7SHong Zhang   ts->numberadjointmonitors = 0;
31949110b2e7SHong Zhang   PetscFunctionReturn(0);
31959110b2e7SHong Zhang }
31969110b2e7SHong Zhang 
31979110b2e7SHong Zhang #undef __FUNCT__
3198110eb670SHong Zhang #define __FUNCT__ "TSAdjointMonitorDefault"
3199110eb670SHong Zhang /*@
3200110eb670SHong Zhang    TSAdjointMonitorDefault - Sets the Default monitor
3201110eb670SHong Zhang 
3202110eb670SHong Zhang    Level: intermediate
3203110eb670SHong Zhang 
3204110eb670SHong Zhang .keywords: TS, set, monitor
3205110eb670SHong Zhang 
3206110eb670SHong Zhang .seealso: TSAdjointMonitorSet()
3207110eb670SHong Zhang @*/
3208110eb670SHong Zhang PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
3209110eb670SHong Zhang {
3210110eb670SHong Zhang   PetscErrorCode ierr;
3211110eb670SHong Zhang   PetscViewer    viewer =  (PetscViewer) dummy;
3212110eb670SHong Zhang 
3213110eb670SHong Zhang   PetscFunctionBegin;
3214110eb670SHong Zhang   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3215110eb670SHong Zhang   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3216110eb670SHong 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);
3217110eb670SHong Zhang   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3218110eb670SHong Zhang   PetscFunctionReturn(0);
3219110eb670SHong Zhang }
3220110eb670SHong Zhang 
3221110eb670SHong Zhang #undef __FUNCT__
3222cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
3223cd652676SJed Brown /*@
3224cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
3225cd652676SJed Brown 
3226cd652676SJed Brown    Logically Collective on TS
3227cd652676SJed Brown 
3228cd652676SJed Brown    Input Argument:
3229cd652676SJed Brown .  ts - time stepping context
3230cd652676SJed Brown 
3231cd652676SJed Brown    Output Argument:
3232cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
3233cd652676SJed Brown 
3234cd652676SJed Brown    Level: intermediate
3235cd652676SJed Brown 
3236cd652676SJed Brown .keywords: TS, set
3237cd652676SJed Brown 
3238cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
3239cd652676SJed Brown @*/
3240cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
3241cd652676SJed Brown {
3242cd652676SJed Brown   PetscFunctionBegin;
3243cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3244cd652676SJed Brown   ts->retain_stages = flg;
3245cd652676SJed Brown   PetscFunctionReturn(0);
3246cd652676SJed Brown }
3247cd652676SJed Brown 
3248cd652676SJed Brown #undef __FUNCT__
3249cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
3250cd652676SJed Brown /*@
3251cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3252cd652676SJed Brown 
3253cd652676SJed Brown    Collective on TS
3254cd652676SJed Brown 
3255cd652676SJed Brown    Input Argument:
3256cd652676SJed Brown +  ts - time stepping context
3257cd652676SJed Brown -  t - time to interpolate to
3258cd652676SJed Brown 
3259cd652676SJed Brown    Output Argument:
32600910c330SBarry Smith .  U - state at given time
3261cd652676SJed Brown 
3262cd652676SJed Brown    Notes:
3263cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
3264cd652676SJed Brown 
3265cd652676SJed Brown    Level: intermediate
3266cd652676SJed Brown 
3267cd652676SJed Brown    Developer Notes:
3268cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3269cd652676SJed Brown 
3270cd652676SJed Brown .keywords: TS, set
3271cd652676SJed Brown 
3272cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
3273cd652676SJed Brown @*/
32740910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3275cd652676SJed Brown {
3276cd652676SJed Brown   PetscErrorCode ierr;
3277cd652676SJed Brown 
3278cd652676SJed Brown   PetscFunctionBegin;
3279cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3280b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
32816712e2f1SBarry 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);
3282ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
32830910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3284cd652676SJed Brown   PetscFunctionReturn(0);
3285cd652676SJed Brown }
3286cd652676SJed Brown 
3287cd652676SJed Brown #undef __FUNCT__
32884a2ae208SSatish Balay #define __FUNCT__ "TSStep"
3289d763cef2SBarry Smith /*@
32906d9e5789SSean Farley    TSStep - Steps one time step
3291d763cef2SBarry Smith 
3292d763cef2SBarry Smith    Collective on TS
3293d763cef2SBarry Smith 
3294d763cef2SBarry Smith    Input Parameter:
3295d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3296d763cef2SBarry Smith 
329727829d71SBarry Smith    Level: developer
3298d763cef2SBarry Smith 
3299b8123daeSJed Brown    Notes:
330027829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
330127829d71SBarry Smith 
3302b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3303b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3304b8123daeSJed Brown 
330525cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
330625cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
330725cb2221SBarry Smith 
3308d763cef2SBarry Smith .keywords: TS, timestep, solve
3309d763cef2SBarry Smith 
33109be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3311d763cef2SBarry Smith @*/
3312193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3313d763cef2SBarry Smith {
33142fb8446cSMatthew G. Knepley   DM               dm;
3315dfbe8321SBarry Smith   PetscErrorCode   ierr;
3316fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3317d763cef2SBarry Smith 
3318d763cef2SBarry Smith   PetscFunctionBegin;
33190700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3320fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3321fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3322fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3323fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3324fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3325fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3326302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3327fffbeea8SBarry Smith 
33282fb8446cSMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3329d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
333068bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3331d405a339SMatthew Knepley 
3332362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
333324655328SShri   ts->ptime_prev = ts->ptime;
3334cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3335bbd56ea5SKarl Rupp 
3336e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3337d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3338193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3339d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3340bbd56ea5SKarl Rupp 
334124655328SShri   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3342cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3343362cd11cSLisandro Dalcin 
3344362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3345cef5090cSJed Brown     if (ts->errorifstepfailed) {
334608c7845fSBarry 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]);
334708c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3348d2daff3dSHong Zhang     }
334908c7845fSBarry Smith   } else if (!ts->reason) {
335008c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
335108c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
335208c7845fSBarry Smith   }
33532c18e0fdSBarry Smith   ts->total_steps++;
33548392e04aSShri Abhyankar   ts->steprollback = PETSC_FALSE;
335508c7845fSBarry Smith   PetscFunctionReturn(0);
335608c7845fSBarry Smith }
335708c7845fSBarry Smith 
335808c7845fSBarry Smith #undef __FUNCT__
335908c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep"
336008c7845fSBarry Smith /*@
3361b957a604SHong Zhang    TSAdjointStep - Steps one time step backward in the adjoint run
336208c7845fSBarry Smith 
336308c7845fSBarry Smith    Collective on TS
336408c7845fSBarry Smith 
336508c7845fSBarry Smith    Input Parameter:
336608c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
336708c7845fSBarry Smith 
33686a4d4014SLisandro Dalcin    Level: intermediate
33696a4d4014SLisandro Dalcin 
3370b957a604SHong Zhang .keywords: TS, adjoint, step
33716a4d4014SLisandro Dalcin 
3372b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve()
33736a4d4014SLisandro Dalcin @*/
337408c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
33756a4d4014SLisandro Dalcin {
337608c7845fSBarry Smith   DM               dm;
33776a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
33786a4d4014SLisandro Dalcin 
33796a4d4014SLisandro Dalcin   PetscFunctionBegin;
33804a2ae208SSatish Balay   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
338108c7845fSBarry Smith   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
338208c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3383d763cef2SBarry Smith 
3384d763cef2SBarry Smith   ts->reason = TS_CONVERGED_ITERATING;
338508c7845fSBarry Smith   ts->ptime_prev = ts->ptime;
338608c7845fSBarry Smith   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3387685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts, "-ts_view_solution");CHKERRQ(ierr);
3388d763cef2SBarry Smith 
33898d0ad7a8SHong Zhang   ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
339042f2b339SBarry 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);
339142f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
33928d0ad7a8SHong Zhang   ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
3393d763cef2SBarry Smith 
3394cef5090cSJed Brown   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3395cef5090cSJed Brown   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3396362cd11cSLisandro Dalcin 
3397362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3398cef5090cSJed Brown     if (ts->errorifstepfailed) {
33996c4ed002SBarry 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]);
34006c4ed002SBarry 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]);
34016c4ed002SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3402cef5090cSJed Brown     }
3403362cd11cSLisandro Dalcin   } else if (!ts->reason) {
340473b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3405db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time)         ts->reason = TS_CONVERGED_TIME;
3406362cd11cSLisandro Dalcin   }
34072c18e0fdSBarry Smith   ts->total_steps--;
3408d763cef2SBarry Smith   PetscFunctionReturn(0);
3409d763cef2SBarry Smith }
3410d763cef2SBarry Smith 
3411d763cef2SBarry Smith #undef __FUNCT__
341205175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
341305175c85SJed Brown /*@
341405175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
341505175c85SJed Brown 
34161c3436cfSJed Brown    Collective on TS
341705175c85SJed Brown 
341805175c85SJed Brown    Input Arguments:
34191c3436cfSJed Brown +  ts - time stepping context
34201c3436cfSJed Brown .  order - desired order of accuracy
34210298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
342205175c85SJed Brown 
342305175c85SJed Brown    Output Arguments:
34240910c330SBarry Smith .  U - state at the end of the current step
342505175c85SJed Brown 
342605175c85SJed Brown    Level: advanced
342705175c85SJed Brown 
3428108c343cSJed Brown    Notes:
3429108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3430108c343cSJed 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.
3431108c343cSJed Brown 
34321c3436cfSJed Brown .seealso: TSStep(), TSAdapt
343305175c85SJed Brown @*/
34340910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
343505175c85SJed Brown {
343605175c85SJed Brown   PetscErrorCode ierr;
343705175c85SJed Brown 
343805175c85SJed Brown   PetscFunctionBegin;
343905175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
344005175c85SJed Brown   PetscValidType(ts,1);
34410910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3442ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
34430910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
344405175c85SJed Brown   PetscFunctionReturn(0);
344505175c85SJed Brown }
344605175c85SJed Brown 
3447*b1cb36f3SHong Zhang #undef __FUNCT__
3448*b1cb36f3SHong Zhang #define __FUNCT__ "TSForwardCostIntegral"
3449*b1cb36f3SHong Zhang /*@
3450*b1cb36f3SHong Zhang  TSForwardCostIntegral - Evaluate the cost integral in the forward run.
3451*b1cb36f3SHong Zhang 
3452*b1cb36f3SHong Zhang  Collective on TS
3453*b1cb36f3SHong Zhang 
3454*b1cb36f3SHong Zhang  Input Arguments:
3455*b1cb36f3SHong Zhang  .  ts - time stepping context
3456*b1cb36f3SHong Zhang 
3457*b1cb36f3SHong Zhang  Level: advanced
3458*b1cb36f3SHong Zhang 
3459*b1cb36f3SHong Zhang  Notes:
3460*b1cb36f3SHong Zhang  This function cannot be called until TSStep() has been completed.
3461*b1cb36f3SHong Zhang 
3462*b1cb36f3SHong Zhang  .seealso: TSSolve(), TSAdjointCostIntegral()
3463*b1cb36f3SHong Zhang  @*/
3464*b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts)
3465*b1cb36f3SHong Zhang {
3466*b1cb36f3SHong Zhang     PetscErrorCode ierr;
3467*b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3468*b1cb36f3SHong 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);
3469*b1cb36f3SHong Zhang     ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr);
3470*b1cb36f3SHong Zhang     PetscFunctionReturn(0);
3471*b1cb36f3SHong Zhang }
3472d67e68b7SBarry Smith 
347305175c85SJed Brown #undef __FUNCT__
3474d763cef2SBarry Smith #define __FUNCT__ "TSSolve"
3475d763cef2SBarry Smith /*@
3476d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
3477d763cef2SBarry Smith 
3478d763cef2SBarry Smith    Collective on TS
3479d763cef2SBarry Smith 
3480d763cef2SBarry Smith    Input Parameter:
3481d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3482cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
34835a3a76d0SJed Brown 
3484d763cef2SBarry Smith    Level: beginner
3485d763cef2SBarry Smith 
34865a3a76d0SJed Brown    Notes:
34875a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
34885a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
34895a3a76d0SJed Brown    stepped over the final time.
34905a3a76d0SJed Brown 
3491d763cef2SBarry Smith .keywords: TS, timestep, solve
3492d763cef2SBarry Smith 
3493d763cef2SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep()
3494d763cef2SBarry Smith @*/
3495cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
3496d763cef2SBarry Smith {
3497b06615a5SLisandro Dalcin   Vec               solution;
3498d763cef2SBarry Smith   PetscErrorCode    ierr;
3499d763cef2SBarry Smith 
3500d763cef2SBarry Smith   PetscFunctionBegin;
3501d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3502f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
350349354f04SShri 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 */
3504b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
35050910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3506b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3507b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3508b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
35095a3a76d0SJed Brown     }
35100910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3511bbd56ea5SKarl Rupp   } else if (u) {
35120910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
35135a3a76d0SJed Brown   }
3514b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
351568bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3516d763cef2SBarry Smith   /* reset time step and iteration counters */
3517193ac0bcSJed Brown   ts->steps             = 0;
35185ef26d82SJed Brown   ts->ksp_its           = 0;
35195ef26d82SJed Brown   ts->snes_its          = 0;
3520c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
3521c610991cSLisandro Dalcin   ts->reject            = 0;
3522193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
3523193ac0bcSJed Brown 
3524ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3525ec8db0baSMatthew G. Knepley   {
3526ec8db0baSMatthew G. Knepley     DM dm;
3527ec8db0baSMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3528ec8db0baSMatthew G. Knepley     ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3529ec8db0baSMatthew G. Knepley   }
3530f05ece33SBarry Smith 
3531193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
3532193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
35330910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
3534cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3535193ac0bcSJed Brown   } else {
3536d763cef2SBarry Smith     /* steps the requested number of timesteps. */
3537db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3538db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3539cdf0de3dSShri Abhyankar     ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
35406fea3669SShri Abhyankar     if(ts->event) {
35416fea3669SShri Abhyankar       ierr = TSEventMonitorInitialize(ts);CHKERRQ(ierr);
35426fea3669SShri Abhyankar     }
3543e1a7a14fSJed Brown     while (!ts->reason) {
3544193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3545193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3546*b1cb36f3SHong Zhang       if (!ts->steprollback && ts->vec_costintegral && ts->costintegralfwd) {
3547*b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
3548*b1cb36f3SHong Zhang       }
3549aeb4809dSShri Abhyankar       if (ts->event) {
3550aeb4809dSShri Abhyankar         ierr = TSEventMonitor(ts);CHKERRQ(ierr);
35511eda64f1SShri Abhyankar       }
35528392e04aSShri Abhyankar       if(!ts->steprollback) {
3553cdf0de3dSShri Abhyankar         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
35541eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
3555aeb4809dSShri Abhyankar       }
3556193ac0bcSJed Brown     }
355749354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
35580910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3559cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3560b06615a5SLisandro Dalcin       solution = u;
35610574a7fbSJed Brown     } else {
3562ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3563cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3564b06615a5SLisandro Dalcin       solution = ts->vec_sol;
35650574a7fbSJed Brown     }
3566b06615a5SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
3567685405a1SBarry Smith     ierr = VecViewFromOptions(solution,(PetscObject) ts,"-ts_view_solution");CHKERRQ(ierr);
3568193ac0bcSJed Brown   }
3569d2daff3dSHong Zhang 
3570ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
3571dc1cb503SEmil Constantinescu   ierr = VecViewFromOptions(ts->vec_sol,NULL,"-ts_view_solution");CHKERRQ(ierr);
357256f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3573ef222394SBarry Smith   if (ts->adjoint_solve) {
3574ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
35752b0a91c0SBarry Smith   }
3576d763cef2SBarry Smith   PetscFunctionReturn(0);
3577d763cef2SBarry Smith }
3578d763cef2SBarry Smith 
3579d763cef2SBarry Smith #undef __FUNCT__
3580*b1cb36f3SHong Zhang #define __FUNCT__ "TSAdjointCostIntegral"
3581*b1cb36f3SHong Zhang /*@
3582*b1cb36f3SHong Zhang  TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run.
3583*b1cb36f3SHong Zhang 
3584*b1cb36f3SHong Zhang  Collective on TS
3585*b1cb36f3SHong Zhang 
3586*b1cb36f3SHong Zhang  Input Arguments:
3587*b1cb36f3SHong Zhang  .  ts - time stepping context
3588*b1cb36f3SHong Zhang 
3589*b1cb36f3SHong Zhang  Level: advanced
3590*b1cb36f3SHong Zhang 
3591*b1cb36f3SHong Zhang  Notes:
3592*b1cb36f3SHong Zhang  This function cannot be called until TSAdjointStep() has been completed.
3593*b1cb36f3SHong Zhang 
3594*b1cb36f3SHong Zhang  .seealso: TSAdjointSolve(), TSAdjointStep
3595*b1cb36f3SHong Zhang  @*/
3596*b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts)
3597*b1cb36f3SHong Zhang {
3598*b1cb36f3SHong Zhang     PetscErrorCode ierr;
3599*b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3600*b1cb36f3SHong 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);
3601*b1cb36f3SHong Zhang     ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr);
3602*b1cb36f3SHong Zhang     PetscFunctionReturn(0);
3603*b1cb36f3SHong Zhang }
3604*b1cb36f3SHong Zhang 
3605*b1cb36f3SHong Zhang #undef __FUNCT__
3606c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve"
3607c88f2d44SBarry Smith /*@
3608c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
3609c88f2d44SBarry Smith 
3610c88f2d44SBarry Smith    Collective on TS
3611c88f2d44SBarry Smith 
3612c88f2d44SBarry Smith    Input Parameter:
36132c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
3614c88f2d44SBarry Smith 
3615e87fd094SBarry Smith    Options Database:
3616e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions
3617e87fd094SBarry Smith 
3618c88f2d44SBarry Smith    Level: intermediate
3619c88f2d44SBarry Smith 
3620c88f2d44SBarry Smith    Notes:
3621c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
3622c88f2d44SBarry Smith 
362373b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
362473b18844SBarry Smith 
3625c88f2d44SBarry Smith .keywords: TS, timestep, solve
3626c88f2d44SBarry Smith 
3627b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
3628c88f2d44SBarry Smith @*/
36292c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
3630c88f2d44SBarry Smith {
3631c88f2d44SBarry Smith   PetscErrorCode    ierr;
3632c88f2d44SBarry Smith 
3633c88f2d44SBarry Smith   PetscFunctionBegin;
3634c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3635c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
363668bece0bSHong Zhang 
3637c88f2d44SBarry Smith   /* reset time step and iteration counters */
3638c88f2d44SBarry Smith   ts->steps             = 0;
3639c88f2d44SBarry Smith   ts->ksp_its           = 0;
3640c88f2d44SBarry Smith   ts->snes_its          = 0;
3641c88f2d44SBarry Smith   ts->num_snes_failures = 0;
3642c88f2d44SBarry Smith   ts->reject            = 0;
3643c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
3644c88f2d44SBarry Smith 
364573b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
3646c88f2d44SBarry Smith 
364773b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3648c88f2d44SBarry Smith   while (!ts->reason) {
364955c52731SHong Zhang     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
36509110b2e7SHong Zhang     ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
3651c88f2d44SBarry Smith     if (ts->event) {
3652d0578d90SShri Abhyankar       ierr = TSAdjointEventMonitor(ts);CHKERRQ(ierr);
3653d0578d90SShri Abhyankar     }
3654616946c1SHong Zhang     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
3655*b1cb36f3SHong Zhang     if (ts->vec_costintegral && !ts->costintegralfwd) {
3656*b1cb36f3SHong Zhang       ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr);
3657*b1cb36f3SHong Zhang     }
3658c88f2d44SBarry Smith   }
365926656371SHong Zhang   ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
366026656371SHong Zhang   ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
3661c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
366239c6b90dSHong Zhang   ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-tstrajectory_view");CHKERRQ(ierr);
3663685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr);
3664c88f2d44SBarry Smith   PetscFunctionReturn(0);
3665c88f2d44SBarry Smith }
3666c88f2d44SBarry Smith 
3667c88f2d44SBarry Smith #undef __FUNCT__
3668d763cef2SBarry Smith #define __FUNCT__ "TSMonitor"
36697db568b7SBarry Smith /*@C
3670228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3671228d79bcSJed Brown 
3672228d79bcSJed Brown    Collective on TS
3673228d79bcSJed Brown 
3674228d79bcSJed Brown    Input Parameters:
3675228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3676228d79bcSJed Brown .  step - step number that has just completed
3677228d79bcSJed Brown .  ptime - model time of the state
36780910c330SBarry Smith -  u - state at the current model time
3679228d79bcSJed Brown 
3680228d79bcSJed Brown    Notes:
36817db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
36827db568b7SBarry Smith    Users would almost never call this routine directly.
3683228d79bcSJed Brown 
36847db568b7SBarry Smith    Level: developer
3685228d79bcSJed Brown 
3686228d79bcSJed Brown .keywords: TS, timestep
3687228d79bcSJed Brown @*/
36880910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3689d763cef2SBarry Smith {
36906849ba73SBarry Smith   PetscErrorCode ierr;
3691a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3692d763cef2SBarry Smith 
3693d763cef2SBarry Smith   PetscFunctionBegin;
3694b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3695b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
36965edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3697d763cef2SBarry Smith   for (i=0; i<n; i++) {
36980910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3699d763cef2SBarry Smith   }
37005edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3701d763cef2SBarry Smith   PetscFunctionReturn(0);
3702d763cef2SBarry Smith }
3703d763cef2SBarry Smith 
37049110b2e7SHong Zhang #undef __FUNCT__
37059110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor"
37067db568b7SBarry Smith /*@C
37079110b2e7SHong Zhang    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()
37089110b2e7SHong Zhang 
37099110b2e7SHong Zhang    Collective on TS
37109110b2e7SHong Zhang 
37119110b2e7SHong Zhang    Input Parameters:
37129110b2e7SHong Zhang +  ts - time stepping context obtained from TSCreate()
37139110b2e7SHong Zhang .  step - step number that has just completed
37149110b2e7SHong Zhang .  ptime - model time of the state
37159110b2e7SHong Zhang .  u - state at the current model time
37169110b2e7SHong Zhang .  numcost - number of cost functions (dimension of lambda  or mu)
37179110b2e7SHong Zhang .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
37189110b2e7SHong Zhang -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
37199110b2e7SHong Zhang 
37209110b2e7SHong Zhang    Notes:
37217db568b7SBarry Smith    TSAdjointMonitor() is typically used automatically within the time stepping implementations.
37227db568b7SBarry Smith    Users would almost never call this routine directly.
37239110b2e7SHong Zhang 
37247db568b7SBarry Smith    Level: developer
37259110b2e7SHong Zhang 
37269110b2e7SHong Zhang .keywords: TS, timestep
37279110b2e7SHong Zhang @*/
37289110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
37299110b2e7SHong Zhang {
37309110b2e7SHong Zhang   PetscErrorCode ierr;
37319110b2e7SHong Zhang   PetscInt       i,n = ts->numberadjointmonitors;
37329110b2e7SHong Zhang 
37339110b2e7SHong Zhang   PetscFunctionBegin;
37349110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37359110b2e7SHong Zhang   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
37369110b2e7SHong Zhang   ierr = VecLockPush(u);CHKERRQ(ierr);
37379110b2e7SHong Zhang   for (i=0; i<n; i++) {
37389110b2e7SHong Zhang     ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
37399110b2e7SHong Zhang   }
37409110b2e7SHong Zhang   ierr = VecLockPop(u);CHKERRQ(ierr);
37419110b2e7SHong Zhang   PetscFunctionReturn(0);
37429110b2e7SHong Zhang }
37439110b2e7SHong Zhang 
3744d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
37454a2ae208SSatish Balay #undef __FUNCT__
3746a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
3747d763cef2SBarry Smith /*@C
37487db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
3749a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3750d763cef2SBarry Smith 
3751d763cef2SBarry Smith    Collective on TS
3752d763cef2SBarry Smith 
3753d763cef2SBarry Smith    Input Parameters:
3754d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3755d763cef2SBarry Smith .  label - the title to put in the title bar
37567c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3757a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3758a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3759d763cef2SBarry Smith 
3760d763cef2SBarry Smith    Output Parameter:
37610b039ecaSBarry Smith .  ctx - the context
3762d763cef2SBarry Smith 
3763d763cef2SBarry Smith    Options Database Key:
37644f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
37657db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
37667db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
37677db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
37687db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
3769b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
3770d763cef2SBarry Smith 
3771d763cef2SBarry Smith    Notes:
3772a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3773d763cef2SBarry Smith 
37747db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
37757db568b7SBarry Smith 
37767db568b7SBarry 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
37777db568b7SBarry 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
37787db568b7SBarry Smith    as the first argument.
37797db568b7SBarry Smith 
37807db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
37817db568b7SBarry Smith 
37827db568b7SBarry Smith 
3783d763cef2SBarry Smith    Level: intermediate
3784d763cef2SBarry Smith 
37857db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
3786d763cef2SBarry Smith 
37877db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
37887db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
37897db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
37907db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
37917db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
37927c922b88SBarry Smith 
3793d763cef2SBarry Smith @*/
3794a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3795d763cef2SBarry Smith {
37967f52daa2SLisandro Dalcin   PetscDraw      draw;
3797dfbe8321SBarry Smith   PetscErrorCode ierr;
3798d763cef2SBarry Smith 
3799d763cef2SBarry Smith   PetscFunctionBegin;
3800b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
38017f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
38027f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
38037f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
3804b6fe0379SLisandro Dalcin   ierr = PetscDrawLGSetUseMarkers((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr);
3805287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
38067f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
3807a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3808d763cef2SBarry Smith   PetscFunctionReturn(0);
3809d763cef2SBarry Smith }
3810d763cef2SBarry Smith 
38114a2ae208SSatish Balay #undef __FUNCT__
38124f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
3813b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3814d763cef2SBarry Smith {
38150b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3816c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3817dfbe8321SBarry Smith   PetscErrorCode ierr;
3818d763cef2SBarry Smith 
3819d763cef2SBarry Smith   PetscFunctionBegin;
3820b06615a5SLisandro Dalcin   if (!step) {
3821a9f9c1f6SBarry Smith     PetscDrawAxis axis;
3822a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
3823a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
3824a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3825a9f9c1f6SBarry Smith   }
3826c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
38270b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3828b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
38290b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
38303923b477SBarry Smith   }
3831d763cef2SBarry Smith   PetscFunctionReturn(0);
3832d763cef2SBarry Smith }
3833d763cef2SBarry Smith 
38344a2ae208SSatish Balay #undef __FUNCT__
3835a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
3836d763cef2SBarry Smith /*@C
3837a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3838a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3839d763cef2SBarry Smith 
38400b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3841d763cef2SBarry Smith 
3842d763cef2SBarry Smith    Input Parameter:
38430b039ecaSBarry Smith .  ctx - the monitor context
3844d763cef2SBarry Smith 
3845d763cef2SBarry Smith    Level: intermediate
3846d763cef2SBarry Smith 
3847d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3848d763cef2SBarry Smith 
38494f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3850d763cef2SBarry Smith @*/
3851a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3852d763cef2SBarry Smith {
3853dfbe8321SBarry Smith   PetscErrorCode ierr;
3854d763cef2SBarry Smith 
3855d763cef2SBarry Smith   PetscFunctionBegin;
38567684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
38577684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
38587684fa3eSBarry Smith   }
38590b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
386031152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
3861387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
3862387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
3863387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
38640b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3865d763cef2SBarry Smith   PetscFunctionReturn(0);
3866d763cef2SBarry Smith }
3867d763cef2SBarry Smith 
38684a2ae208SSatish Balay #undef __FUNCT__
38694a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
3870d763cef2SBarry Smith /*@
3871b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3872d763cef2SBarry Smith 
3873d763cef2SBarry Smith    Not Collective
3874d763cef2SBarry Smith 
3875d763cef2SBarry Smith    Input Parameter:
3876d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3877d763cef2SBarry Smith 
3878d763cef2SBarry Smith    Output Parameter:
3879d763cef2SBarry Smith .  t  - the current time
3880d763cef2SBarry Smith 
3881d763cef2SBarry Smith    Level: beginner
3882d763cef2SBarry Smith 
3883b8123daeSJed Brown    Note:
3884b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
38859be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
3886b8123daeSJed Brown 
3887d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3888d763cef2SBarry Smith 
3889d763cef2SBarry Smith .keywords: TS, get, time
3890d763cef2SBarry Smith @*/
38917087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
3892d763cef2SBarry Smith {
3893d763cef2SBarry Smith   PetscFunctionBegin;
38940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3895f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
3896d763cef2SBarry Smith   *t = ts->ptime;
3897d763cef2SBarry Smith   PetscFunctionReturn(0);
3898d763cef2SBarry Smith }
3899d763cef2SBarry Smith 
39004a2ae208SSatish Balay #undef __FUNCT__
3901e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
3902e5e524a1SHong Zhang /*@
3903e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
3904e5e524a1SHong Zhang 
3905e5e524a1SHong Zhang    Not Collective
3906e5e524a1SHong Zhang 
3907e5e524a1SHong Zhang    Input Parameter:
3908e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
3909e5e524a1SHong Zhang 
3910e5e524a1SHong Zhang    Output Parameter:
3911e5e524a1SHong Zhang .  t  - the previous time
3912e5e524a1SHong Zhang 
3913e5e524a1SHong Zhang    Level: beginner
3914e5e524a1SHong Zhang 
3915e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3916e5e524a1SHong Zhang 
3917e5e524a1SHong Zhang .keywords: TS, get, time
3918e5e524a1SHong Zhang @*/
3919e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
3920e5e524a1SHong Zhang {
3921e5e524a1SHong Zhang   PetscFunctionBegin;
3922e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3923e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
3924e5e524a1SHong Zhang   *t = ts->ptime_prev;
3925e5e524a1SHong Zhang   PetscFunctionReturn(0);
3926e5e524a1SHong Zhang }
3927e5e524a1SHong Zhang 
3928e5e524a1SHong Zhang #undef __FUNCT__
39296a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
39306a4d4014SLisandro Dalcin /*@
39316a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
39326a4d4014SLisandro Dalcin 
39333f9fe445SBarry Smith    Logically Collective on TS
39346a4d4014SLisandro Dalcin 
39356a4d4014SLisandro Dalcin    Input Parameters:
39366a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
39376a4d4014SLisandro Dalcin -  time - the time
39386a4d4014SLisandro Dalcin 
39396a4d4014SLisandro Dalcin    Level: intermediate
39406a4d4014SLisandro Dalcin 
39416a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
39426a4d4014SLisandro Dalcin 
39436a4d4014SLisandro Dalcin .keywords: TS, set, time
39446a4d4014SLisandro Dalcin @*/
39457087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
39466a4d4014SLisandro Dalcin {
39476a4d4014SLisandro Dalcin   PetscFunctionBegin;
39480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3949c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
39506a4d4014SLisandro Dalcin   ts->ptime = t;
39516a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
39526a4d4014SLisandro Dalcin }
39536a4d4014SLisandro Dalcin 
39546a4d4014SLisandro Dalcin #undef __FUNCT__
39554a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
3956d763cef2SBarry Smith /*@C
3957d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
3958d763cef2SBarry Smith    TS options in the database.
3959d763cef2SBarry Smith 
39603f9fe445SBarry Smith    Logically Collective on TS
3961d763cef2SBarry Smith 
3962d763cef2SBarry Smith    Input Parameter:
3963d763cef2SBarry Smith +  ts     - The TS context
3964d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3965d763cef2SBarry Smith 
3966d763cef2SBarry Smith    Notes:
3967d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3968d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3969d763cef2SBarry Smith    hyphen.
3970d763cef2SBarry Smith 
3971d763cef2SBarry Smith    Level: advanced
3972d763cef2SBarry Smith 
3973d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
3974d763cef2SBarry Smith 
3975d763cef2SBarry Smith .seealso: TSSetFromOptions()
3976d763cef2SBarry Smith 
3977d763cef2SBarry Smith @*/
39787087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
3979d763cef2SBarry Smith {
3980dfbe8321SBarry Smith   PetscErrorCode ierr;
3981089b2837SJed Brown   SNES           snes;
3982d763cef2SBarry Smith 
3983d763cef2SBarry Smith   PetscFunctionBegin;
39840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3985d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3986089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3987089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3988d763cef2SBarry Smith   PetscFunctionReturn(0);
3989d763cef2SBarry Smith }
3990d763cef2SBarry Smith 
3991d763cef2SBarry Smith 
39924a2ae208SSatish Balay #undef __FUNCT__
39934a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
3994d763cef2SBarry Smith /*@C
3995d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
3996d763cef2SBarry Smith    TS options in the database.
3997d763cef2SBarry Smith 
39983f9fe445SBarry Smith    Logically Collective on TS
3999d763cef2SBarry Smith 
4000d763cef2SBarry Smith    Input Parameter:
4001d763cef2SBarry Smith +  ts     - The TS context
4002d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4003d763cef2SBarry Smith 
4004d763cef2SBarry Smith    Notes:
4005d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4006d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4007d763cef2SBarry Smith    hyphen.
4008d763cef2SBarry Smith 
4009d763cef2SBarry Smith    Level: advanced
4010d763cef2SBarry Smith 
4011d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4012d763cef2SBarry Smith 
4013d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4014d763cef2SBarry Smith 
4015d763cef2SBarry Smith @*/
40167087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4017d763cef2SBarry Smith {
4018dfbe8321SBarry Smith   PetscErrorCode ierr;
4019089b2837SJed Brown   SNES           snes;
4020d763cef2SBarry Smith 
4021d763cef2SBarry Smith   PetscFunctionBegin;
40220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4023d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4024089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4025089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4026d763cef2SBarry Smith   PetscFunctionReturn(0);
4027d763cef2SBarry Smith }
4028d763cef2SBarry Smith 
40294a2ae208SSatish Balay #undef __FUNCT__
40304a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
4031d763cef2SBarry Smith /*@C
4032d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4033d763cef2SBarry Smith    TS options in the database.
4034d763cef2SBarry Smith 
4035d763cef2SBarry Smith    Not Collective
4036d763cef2SBarry Smith 
4037d763cef2SBarry Smith    Input Parameter:
4038d763cef2SBarry Smith .  ts - The TS context
4039d763cef2SBarry Smith 
4040d763cef2SBarry Smith    Output Parameter:
4041d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4042d763cef2SBarry Smith 
4043d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
4044d763cef2SBarry Smith    sufficient length to hold the prefix.
4045d763cef2SBarry Smith 
4046d763cef2SBarry Smith    Level: intermediate
4047d763cef2SBarry Smith 
4048d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4049d763cef2SBarry Smith 
4050d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4051d763cef2SBarry Smith @*/
40527087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4053d763cef2SBarry Smith {
4054dfbe8321SBarry Smith   PetscErrorCode ierr;
4055d763cef2SBarry Smith 
4056d763cef2SBarry Smith   PetscFunctionBegin;
40570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40584482741eSBarry Smith   PetscValidPointer(prefix,2);
4059d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4060d763cef2SBarry Smith   PetscFunctionReturn(0);
4061d763cef2SBarry Smith }
4062d763cef2SBarry Smith 
40634a2ae208SSatish Balay #undef __FUNCT__
40644a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
4065d763cef2SBarry Smith /*@C
4066d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4067d763cef2SBarry Smith 
4068d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4069d763cef2SBarry Smith 
4070d763cef2SBarry Smith    Input Parameter:
4071d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4072d763cef2SBarry Smith 
4073d763cef2SBarry Smith    Output Parameters:
4074e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4075e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4076e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4077e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4078d763cef2SBarry Smith 
40790298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
4080d763cef2SBarry Smith 
4081d763cef2SBarry Smith    Level: intermediate
4082d763cef2SBarry Smith 
408326d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
4084d763cef2SBarry Smith 
4085d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4086d763cef2SBarry Smith @*/
4087e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4088d763cef2SBarry Smith {
4089089b2837SJed Brown   PetscErrorCode ierr;
4090089b2837SJed Brown   SNES           snes;
409124989b8cSPeter Brune   DM             dm;
4092089b2837SJed Brown 
4093d763cef2SBarry Smith   PetscFunctionBegin;
4094089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4095e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
409624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
409724989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4098d763cef2SBarry Smith   PetscFunctionReturn(0);
4099d763cef2SBarry Smith }
4100d763cef2SBarry Smith 
41011713a123SBarry Smith #undef __FUNCT__
41022eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
41032eca1d9cSJed Brown /*@C
41042eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
41052eca1d9cSJed Brown 
41062eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
41072eca1d9cSJed Brown 
41082eca1d9cSJed Brown    Input Parameter:
41092eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
41102eca1d9cSJed Brown 
41112eca1d9cSJed Brown    Output Parameters:
4112e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4113e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
41142eca1d9cSJed Brown .  f   - The function to compute the matrices
41152eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
41162eca1d9cSJed Brown 
41170298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
41182eca1d9cSJed Brown 
41192eca1d9cSJed Brown    Level: advanced
41202eca1d9cSJed Brown 
41212eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
41222eca1d9cSJed Brown 
41232eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
41242eca1d9cSJed Brown @*/
4125e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
41262eca1d9cSJed Brown {
4127089b2837SJed Brown   PetscErrorCode ierr;
4128089b2837SJed Brown   SNES           snes;
412924989b8cSPeter Brune   DM             dm;
41300910c330SBarry Smith 
41312eca1d9cSJed Brown   PetscFunctionBegin;
4132089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4133f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4134e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
413524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
413624989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
41372eca1d9cSJed Brown   PetscFunctionReturn(0);
41382eca1d9cSJed Brown }
41392eca1d9cSJed Brown 
41406083293cSBarry Smith 
41412eca1d9cSJed Brown #undef __FUNCT__
414283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
41431713a123SBarry Smith /*@C
414483a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
41451713a123SBarry Smith    VecView() for the solution at each timestep
41461713a123SBarry Smith 
41471713a123SBarry Smith    Collective on TS
41481713a123SBarry Smith 
41491713a123SBarry Smith    Input Parameters:
41501713a123SBarry Smith +  ts - the TS context
41511713a123SBarry Smith .  step - current time-step
4152142b95e3SSatish Balay .  ptime - current time
41530298fd71SBarry Smith -  dummy - either a viewer or NULL
41541713a123SBarry Smith 
415599fdda47SBarry Smith    Options Database:
415699fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
415799fdda47SBarry Smith 
4158387f4636SBarry 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
415999fdda47SBarry Smith        will look bad
416099fdda47SBarry Smith 
41611713a123SBarry Smith    Level: intermediate
41621713a123SBarry Smith 
41631713a123SBarry Smith .keywords: TS,  vector, monitor, view
41641713a123SBarry Smith 
4165a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
41661713a123SBarry Smith @*/
41670910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
41681713a123SBarry Smith {
4169dfbe8321SBarry Smith   PetscErrorCode   ierr;
417083a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4171473a3ab2SBarry Smith   PetscDraw        draw;
41721713a123SBarry Smith 
41731713a123SBarry Smith   PetscFunctionBegin;
41746083293cSBarry Smith   if (!step && ictx->showinitial) {
41756083293cSBarry Smith     if (!ictx->initialsolution) {
41760910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
41771713a123SBarry Smith     }
41780910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
41796083293cSBarry Smith   }
4180b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
41810dcf80beSBarry Smith 
41826083293cSBarry Smith   if (ictx->showinitial) {
41836083293cSBarry Smith     PetscReal pause;
41846083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
41856083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
41866083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
41876083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
41886083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
41896083293cSBarry Smith   }
41900910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4191473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
419251fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4193473a3ab2SBarry Smith     char      time[32];
4194473a3ab2SBarry Smith 
4195473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
419685b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4197473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4198473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
419951fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4200473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4201473a3ab2SBarry Smith   }
4202473a3ab2SBarry Smith 
42036083293cSBarry Smith   if (ictx->showinitial) {
42046083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
42056083293cSBarry Smith   }
42061713a123SBarry Smith   PetscFunctionReturn(0);
42071713a123SBarry Smith }
42081713a123SBarry Smith 
42092d5ee99bSBarry Smith #undef __FUNCT__
42109110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi"
42119110b2e7SHong Zhang /*@C
42129110b2e7SHong Zhang    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
42139110b2e7SHong Zhang    VecView() for the sensitivities to initial states at each timestep
42149110b2e7SHong Zhang 
42159110b2e7SHong Zhang    Collective on TS
42169110b2e7SHong Zhang 
42179110b2e7SHong Zhang    Input Parameters:
42189110b2e7SHong Zhang +  ts - the TS context
42199110b2e7SHong Zhang .  step - current time-step
42209110b2e7SHong Zhang .  ptime - current time
42219110b2e7SHong Zhang .  u - current state
42229110b2e7SHong Zhang .  numcost - number of cost functions
42239110b2e7SHong Zhang .  lambda - sensitivities to initial conditions
42249110b2e7SHong Zhang .  mu - sensitivities to parameters
42259110b2e7SHong Zhang -  dummy - either a viewer or NULL
42269110b2e7SHong Zhang 
42279110b2e7SHong Zhang    Level: intermediate
42289110b2e7SHong Zhang 
42299110b2e7SHong Zhang .keywords: TS,  vector, adjoint, monitor, view
42309110b2e7SHong Zhang 
42319110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
42329110b2e7SHong Zhang @*/
42339110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
42349110b2e7SHong Zhang {
42359110b2e7SHong Zhang   PetscErrorCode   ierr;
42369110b2e7SHong Zhang   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
42379110b2e7SHong Zhang   PetscDraw        draw;
4238a0046e2cSSatish Balay   PetscReal        xl,yl,xr,yr,h;
4239a0046e2cSSatish Balay   char             time[32];
42409110b2e7SHong Zhang 
42419110b2e7SHong Zhang   PetscFunctionBegin;
42429110b2e7SHong Zhang   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
42439110b2e7SHong Zhang 
42449110b2e7SHong Zhang   ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr);
42459110b2e7SHong Zhang   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
42469110b2e7SHong Zhang   ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
42479110b2e7SHong Zhang   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
42489110b2e7SHong Zhang   h    = yl + .95*(yr - yl);
42499110b2e7SHong Zhang   ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
42509110b2e7SHong Zhang   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
42519110b2e7SHong Zhang 
42529110b2e7SHong Zhang   PetscFunctionReturn(0);
42539110b2e7SHong Zhang }
42549110b2e7SHong Zhang 
42559110b2e7SHong Zhang #undef __FUNCT__
42562d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
42572d5ee99bSBarry Smith /*@C
42582d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
42592d5ee99bSBarry Smith 
42602d5ee99bSBarry Smith    Collective on TS
42612d5ee99bSBarry Smith 
42622d5ee99bSBarry Smith    Input Parameters:
42632d5ee99bSBarry Smith +  ts - the TS context
42642d5ee99bSBarry Smith .  step - current time-step
42652d5ee99bSBarry Smith .  ptime - current time
42662d5ee99bSBarry Smith -  dummy - either a viewer or NULL
42672d5ee99bSBarry Smith 
42682d5ee99bSBarry Smith    Level: intermediate
42692d5ee99bSBarry Smith 
42702d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
42712d5ee99bSBarry Smith 
42722d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
42732d5ee99bSBarry Smith @*/
42742d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
42752d5ee99bSBarry Smith {
42762d5ee99bSBarry Smith   PetscErrorCode    ierr;
42772d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
42782d5ee99bSBarry Smith   PetscDraw         draw;
42792d5ee99bSBarry Smith   MPI_Comm          comm;
42802d5ee99bSBarry Smith   PetscInt          n;
42812d5ee99bSBarry Smith   PetscMPIInt       size;
428251fa3d41SBarry Smith   PetscReal         xl,yl,xr,yr,h;
42832d5ee99bSBarry Smith   char              time[32];
42842d5ee99bSBarry Smith   const PetscScalar *U;
42852d5ee99bSBarry Smith 
42862d5ee99bSBarry Smith   PetscFunctionBegin;
42872d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
42882d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
42892d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
42902d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
42912d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
42922d5ee99bSBarry Smith 
42932d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
42942d5ee99bSBarry Smith 
42952d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
42964b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
4297ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
4298a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
4299a4494fc1SBarry Smith       PetscFunctionReturn(0);
4300a4494fc1SBarry Smith   }
43012d5ee99bSBarry Smith   if (!step) ictx->color++;
43022d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
43032d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
43042d5ee99bSBarry Smith 
43052d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
43064b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
430785b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
43082d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
430951fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
43102d5ee99bSBarry Smith   }
43112d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
43122d5ee99bSBarry Smith   PetscFunctionReturn(0);
43132d5ee99bSBarry Smith }
43142d5ee99bSBarry Smith 
43151713a123SBarry Smith 
43166c699258SBarry Smith #undef __FUNCT__
431783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
43186083293cSBarry Smith /*@C
431983a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
43206083293cSBarry Smith 
43216083293cSBarry Smith    Collective on TS
43226083293cSBarry Smith 
43236083293cSBarry Smith    Input Parameters:
43246083293cSBarry Smith .    ctx - the monitor context
43256083293cSBarry Smith 
43266083293cSBarry Smith    Level: intermediate
43276083293cSBarry Smith 
43286083293cSBarry Smith .keywords: TS,  vector, monitor, view
43296083293cSBarry Smith 
433083a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
43316083293cSBarry Smith @*/
433283a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
43336083293cSBarry Smith {
43346083293cSBarry Smith   PetscErrorCode ierr;
43356083293cSBarry Smith 
43366083293cSBarry Smith   PetscFunctionBegin;
43374b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
433883a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
433983a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
434083a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
43416083293cSBarry Smith   PetscFunctionReturn(0);
43426083293cSBarry Smith }
43436083293cSBarry Smith 
43446083293cSBarry Smith #undef __FUNCT__
434583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
43466083293cSBarry Smith /*@C
434783a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
43486083293cSBarry Smith 
43496083293cSBarry Smith    Collective on TS
43506083293cSBarry Smith 
43516083293cSBarry Smith    Input Parameter:
43526083293cSBarry Smith .    ts - time-step context
43536083293cSBarry Smith 
43546083293cSBarry Smith    Output Patameter:
43556083293cSBarry Smith .    ctx - the monitor context
43566083293cSBarry Smith 
435799fdda47SBarry Smith    Options Database:
435899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
435999fdda47SBarry Smith 
43606083293cSBarry Smith    Level: intermediate
43616083293cSBarry Smith 
43626083293cSBarry Smith .keywords: TS,  vector, monitor, view
43636083293cSBarry Smith 
436483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
43656083293cSBarry Smith @*/
436683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
43676083293cSBarry Smith {
43686083293cSBarry Smith   PetscErrorCode   ierr;
43696083293cSBarry Smith 
43706083293cSBarry Smith   PetscFunctionBegin;
4371b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
437283a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4373e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4374bbd56ea5SKarl Rupp 
437583a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4376473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4377c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4378473a3ab2SBarry Smith 
4379473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4380c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
43812d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
43826083293cSBarry Smith   PetscFunctionReturn(0);
43836083293cSBarry Smith }
43846083293cSBarry Smith 
43856083293cSBarry Smith #undef __FUNCT__
438683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
43873a471f94SBarry Smith /*@C
438883a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
43893a471f94SBarry Smith    VecView() for the error at each timestep
43903a471f94SBarry Smith 
43913a471f94SBarry Smith    Collective on TS
43923a471f94SBarry Smith 
43933a471f94SBarry Smith    Input Parameters:
43943a471f94SBarry Smith +  ts - the TS context
43953a471f94SBarry Smith .  step - current time-step
43963a471f94SBarry Smith .  ptime - current time
43970298fd71SBarry Smith -  dummy - either a viewer or NULL
43983a471f94SBarry Smith 
43993a471f94SBarry Smith    Level: intermediate
44003a471f94SBarry Smith 
44013a471f94SBarry Smith .keywords: TS,  vector, monitor, view
44023a471f94SBarry Smith 
44033a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
44043a471f94SBarry Smith @*/
44050910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
44063a471f94SBarry Smith {
44073a471f94SBarry Smith   PetscErrorCode   ierr;
440883a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
440983a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
44103a471f94SBarry Smith   Vec              work;
44113a471f94SBarry Smith 
44123a471f94SBarry Smith   PetscFunctionBegin;
4413b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
44140910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
44153a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
44160910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
44173a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
44183a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
44193a471f94SBarry Smith   PetscFunctionReturn(0);
44203a471f94SBarry Smith }
44213a471f94SBarry Smith 
4422af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
44233a471f94SBarry Smith #undef __FUNCT__
44246c699258SBarry Smith #define __FUNCT__ "TSSetDM"
44256c699258SBarry Smith /*@
44266c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
44276c699258SBarry Smith 
44283f9fe445SBarry Smith    Logically Collective on TS and DM
44296c699258SBarry Smith 
44306c699258SBarry Smith    Input Parameters:
44316c699258SBarry Smith +  ts - the preconditioner context
44326c699258SBarry Smith -  dm - the dm
44336c699258SBarry Smith 
44346c699258SBarry Smith    Level: intermediate
44356c699258SBarry Smith 
44366c699258SBarry Smith 
44376c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
44386c699258SBarry Smith @*/
44397087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
44406c699258SBarry Smith {
44416c699258SBarry Smith   PetscErrorCode ierr;
4442089b2837SJed Brown   SNES           snes;
4443942e3340SBarry Smith   DMTS           tsdm;
44446c699258SBarry Smith 
44456c699258SBarry Smith   PetscFunctionBegin;
44460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
444770663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4448942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
44492a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4450942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4451942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
445224989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
445324989b8cSPeter Brune         tsdm->originaldm = dm;
445424989b8cSPeter Brune       }
445524989b8cSPeter Brune     }
44566bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
445724989b8cSPeter Brune   }
44586c699258SBarry Smith   ts->dm = dm;
4459bbd56ea5SKarl Rupp 
4460089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4461089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
44626c699258SBarry Smith   PetscFunctionReturn(0);
44636c699258SBarry Smith }
44646c699258SBarry Smith 
44656c699258SBarry Smith #undef __FUNCT__
44666c699258SBarry Smith #define __FUNCT__ "TSGetDM"
44676c699258SBarry Smith /*@
44686c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
44696c699258SBarry Smith 
44703f9fe445SBarry Smith    Not Collective
44716c699258SBarry Smith 
44726c699258SBarry Smith    Input Parameter:
44736c699258SBarry Smith . ts - the preconditioner context
44746c699258SBarry Smith 
44756c699258SBarry Smith    Output Parameter:
44766c699258SBarry Smith .  dm - the dm
44776c699258SBarry Smith 
44786c699258SBarry Smith    Level: intermediate
44796c699258SBarry Smith 
44806c699258SBarry Smith 
44816c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
44826c699258SBarry Smith @*/
44837087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
44846c699258SBarry Smith {
4485496e6a7aSJed Brown   PetscErrorCode ierr;
4486496e6a7aSJed Brown 
44876c699258SBarry Smith   PetscFunctionBegin;
44880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4489496e6a7aSJed Brown   if (!ts->dm) {
4490ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4491496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4492496e6a7aSJed Brown   }
44936c699258SBarry Smith   *dm = ts->dm;
44946c699258SBarry Smith   PetscFunctionReturn(0);
44956c699258SBarry Smith }
44961713a123SBarry Smith 
44970f5c6efeSJed Brown #undef __FUNCT__
44980f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
44990f5c6efeSJed Brown /*@
45000f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
45010f5c6efeSJed Brown 
45023f9fe445SBarry Smith    Logically Collective on SNES
45030f5c6efeSJed Brown 
45040f5c6efeSJed Brown    Input Parameter:
4505d42a1c89SJed Brown + snes - nonlinear solver
45060910c330SBarry Smith . U - the current state at which to evaluate the residual
4507d42a1c89SJed Brown - ctx - user context, must be a TS
45080f5c6efeSJed Brown 
45090f5c6efeSJed Brown    Output Parameter:
45100f5c6efeSJed Brown . F - the nonlinear residual
45110f5c6efeSJed Brown 
45120f5c6efeSJed Brown    Notes:
45130f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
45140f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
45150f5c6efeSJed Brown 
45160f5c6efeSJed Brown    Level: advanced
45170f5c6efeSJed Brown 
45180f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
45190f5c6efeSJed Brown @*/
45200910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
45210f5c6efeSJed Brown {
45220f5c6efeSJed Brown   TS             ts = (TS)ctx;
45230f5c6efeSJed Brown   PetscErrorCode ierr;
45240f5c6efeSJed Brown 
45250f5c6efeSJed Brown   PetscFunctionBegin;
45260f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
45270910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
45280f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
45290f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
45300910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
45310f5c6efeSJed Brown   PetscFunctionReturn(0);
45320f5c6efeSJed Brown }
45330f5c6efeSJed Brown 
45340f5c6efeSJed Brown #undef __FUNCT__
45350f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
45360f5c6efeSJed Brown /*@
45370f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
45380f5c6efeSJed Brown 
45390f5c6efeSJed Brown    Collective on SNES
45400f5c6efeSJed Brown 
45410f5c6efeSJed Brown    Input Parameter:
45420f5c6efeSJed Brown + snes - nonlinear solver
45430910c330SBarry Smith . U - the current state at which to evaluate the residual
45440f5c6efeSJed Brown - ctx - user context, must be a TS
45450f5c6efeSJed Brown 
45460f5c6efeSJed Brown    Output Parameter:
45470f5c6efeSJed Brown + A - the Jacobian
45480f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
45490f5c6efeSJed Brown - flag - indicates any structure change in the matrix
45500f5c6efeSJed Brown 
45510f5c6efeSJed Brown    Notes:
45520f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
45530f5c6efeSJed Brown 
45540f5c6efeSJed Brown    Level: developer
45550f5c6efeSJed Brown 
45560f5c6efeSJed Brown .seealso: SNESSetJacobian()
45570f5c6efeSJed Brown @*/
4558d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
45590f5c6efeSJed Brown {
45600f5c6efeSJed Brown   TS             ts = (TS)ctx;
45610f5c6efeSJed Brown   PetscErrorCode ierr;
45620f5c6efeSJed Brown 
45630f5c6efeSJed Brown   PetscFunctionBegin;
45640f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
45650910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
45660f5c6efeSJed Brown   PetscValidPointer(A,3);
456794ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
45680f5c6efeSJed Brown   PetscValidPointer(B,4);
456994ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
45700f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4571d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
45720f5c6efeSJed Brown   PetscFunctionReturn(0);
45730f5c6efeSJed Brown }
4574325fc9f4SBarry Smith 
45750e4ef248SJed Brown #undef __FUNCT__
45760e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
45770e4ef248SJed Brown /*@C
45789ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
45790e4ef248SJed Brown 
45800e4ef248SJed Brown    Collective on TS
45810e4ef248SJed Brown 
45820e4ef248SJed Brown    Input Arguments:
45830e4ef248SJed Brown +  ts - time stepping context
45840e4ef248SJed Brown .  t - time at which to evaluate
45850910c330SBarry Smith .  U - state at which to evaluate
45860e4ef248SJed Brown -  ctx - context
45870e4ef248SJed Brown 
45880e4ef248SJed Brown    Output Arguments:
45890e4ef248SJed Brown .  F - right hand side
45900e4ef248SJed Brown 
45910e4ef248SJed Brown    Level: intermediate
45920e4ef248SJed Brown 
45930e4ef248SJed Brown    Notes:
45940e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
45950e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
45960e4ef248SJed Brown 
45970e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
45980e4ef248SJed Brown @*/
45990910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
46000e4ef248SJed Brown {
46010e4ef248SJed Brown   PetscErrorCode ierr;
46020e4ef248SJed Brown   Mat            Arhs,Brhs;
46030e4ef248SJed Brown 
46040e4ef248SJed Brown   PetscFunctionBegin;
46050e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4606d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
46070910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
46080e4ef248SJed Brown   PetscFunctionReturn(0);
46090e4ef248SJed Brown }
46100e4ef248SJed Brown 
46110e4ef248SJed Brown #undef __FUNCT__
46120e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
46130e4ef248SJed Brown /*@C
46140e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
46150e4ef248SJed Brown 
46160e4ef248SJed Brown    Collective on TS
46170e4ef248SJed Brown 
46180e4ef248SJed Brown    Input Arguments:
46190e4ef248SJed Brown +  ts - time stepping context
46200e4ef248SJed Brown .  t - time at which to evaluate
46210910c330SBarry Smith .  U - state at which to evaluate
46220e4ef248SJed Brown -  ctx - context
46230e4ef248SJed Brown 
46240e4ef248SJed Brown    Output Arguments:
46250e4ef248SJed Brown +  A - pointer to operator
46260e4ef248SJed Brown .  B - pointer to preconditioning matrix
46270e4ef248SJed Brown -  flg - matrix structure flag
46280e4ef248SJed Brown 
46290e4ef248SJed Brown    Level: intermediate
46300e4ef248SJed Brown 
46310e4ef248SJed Brown    Notes:
46320e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
46330e4ef248SJed Brown 
46340e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
46350e4ef248SJed Brown @*/
4636d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
46370e4ef248SJed Brown {
46380e4ef248SJed Brown   PetscFunctionBegin;
46390e4ef248SJed Brown   PetscFunctionReturn(0);
46400e4ef248SJed Brown }
46410e4ef248SJed Brown 
46420026cea9SSean Farley #undef __FUNCT__
46430026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
46440026cea9SSean Farley /*@C
46450026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
46460026cea9SSean Farley 
46470026cea9SSean Farley    Collective on TS
46480026cea9SSean Farley 
46490026cea9SSean Farley    Input Arguments:
46500026cea9SSean Farley +  ts - time stepping context
46510026cea9SSean Farley .  t - time at which to evaluate
46520910c330SBarry Smith .  U - state at which to evaluate
46530910c330SBarry Smith .  Udot - time derivative of state vector
46540026cea9SSean Farley -  ctx - context
46550026cea9SSean Farley 
46560026cea9SSean Farley    Output Arguments:
46570026cea9SSean Farley .  F - left hand side
46580026cea9SSean Farley 
46590026cea9SSean Farley    Level: intermediate
46600026cea9SSean Farley 
46610026cea9SSean Farley    Notes:
46620910c330SBarry 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
46630026cea9SSean Farley    user is required to write their own TSComputeIFunction.
46640026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
46650026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
46660026cea9SSean Farley 
46679ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
46689ae8fd06SBarry Smith 
46699ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
46700026cea9SSean Farley @*/
46710910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
46720026cea9SSean Farley {
46730026cea9SSean Farley   PetscErrorCode ierr;
46740026cea9SSean Farley   Mat            A,B;
46750026cea9SSean Farley 
46760026cea9SSean Farley   PetscFunctionBegin;
46770298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4678d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
46790910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
46800026cea9SSean Farley   PetscFunctionReturn(0);
46810026cea9SSean Farley }
46820026cea9SSean Farley 
46830026cea9SSean Farley #undef __FUNCT__
46840026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
46850026cea9SSean Farley /*@C
4686b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
46870026cea9SSean Farley 
46880026cea9SSean Farley    Collective on TS
46890026cea9SSean Farley 
46900026cea9SSean Farley    Input Arguments:
46910026cea9SSean Farley +  ts - time stepping context
46920026cea9SSean Farley .  t - time at which to evaluate
46930910c330SBarry Smith .  U - state at which to evaluate
46940910c330SBarry Smith .  Udot - time derivative of state vector
46950026cea9SSean Farley .  shift - shift to apply
46960026cea9SSean Farley -  ctx - context
46970026cea9SSean Farley 
46980026cea9SSean Farley    Output Arguments:
46990026cea9SSean Farley +  A - pointer to operator
47000026cea9SSean Farley .  B - pointer to preconditioning matrix
47010026cea9SSean Farley -  flg - matrix structure flag
47020026cea9SSean Farley 
4703b41af12eSJed Brown    Level: advanced
47040026cea9SSean Farley 
47050026cea9SSean Farley    Notes:
47060026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
47070026cea9SSean Farley 
4708b41af12eSJed Brown    It is only appropriate for problems of the form
4709b41af12eSJed Brown 
4710b41af12eSJed Brown $     M Udot = F(U,t)
4711b41af12eSJed Brown 
4712b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4713b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4714b41af12eSJed Brown   an implicit operator of the form
4715b41af12eSJed Brown 
4716b41af12eSJed Brown $    shift*M + J
4717b41af12eSJed Brown 
4718b41af12eSJed 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
4719b41af12eSJed Brown   a copy of M or reassemble it when requested.
4720b41af12eSJed Brown 
47210026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
47220026cea9SSean Farley @*/
4723d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
47240026cea9SSean Farley {
4725b41af12eSJed Brown   PetscErrorCode ierr;
4726b41af12eSJed Brown 
47270026cea9SSean Farley   PetscFunctionBegin;
472894ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4729b41af12eSJed Brown   ts->ijacobian.shift = shift;
47300026cea9SSean Farley   PetscFunctionReturn(0);
47310026cea9SSean Farley }
4732b41af12eSJed Brown 
4733e817cc15SEmil Constantinescu #undef __FUNCT__
4734e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
4735e817cc15SEmil Constantinescu /*@
4736e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4737e817cc15SEmil Constantinescu 
4738e817cc15SEmil Constantinescu    Not Collective
4739e817cc15SEmil Constantinescu 
4740e817cc15SEmil Constantinescu    Input Parameter:
4741e817cc15SEmil Constantinescu .  ts - the TS context
4742e817cc15SEmil Constantinescu 
4743e817cc15SEmil Constantinescu    Output Parameter:
47444e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4745e817cc15SEmil Constantinescu 
4746e817cc15SEmil Constantinescu    Level: beginner
4747e817cc15SEmil Constantinescu 
4748e817cc15SEmil Constantinescu .keywords: TS, equation type
4749e817cc15SEmil Constantinescu 
4750e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4751e817cc15SEmil Constantinescu @*/
4752e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4753e817cc15SEmil Constantinescu {
4754e817cc15SEmil Constantinescu   PetscFunctionBegin;
4755e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4756e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4757e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4758e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4759e817cc15SEmil Constantinescu }
4760e817cc15SEmil Constantinescu 
4761e817cc15SEmil Constantinescu #undef __FUNCT__
4762e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
4763e817cc15SEmil Constantinescu /*@
4764e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4765e817cc15SEmil Constantinescu 
4766e817cc15SEmil Constantinescu    Not Collective
4767e817cc15SEmil Constantinescu 
4768e817cc15SEmil Constantinescu    Input Parameter:
4769e817cc15SEmil Constantinescu +  ts - the TS context
47701297b224SEmil Constantinescu -  equation_type - see TSEquationType
4771e817cc15SEmil Constantinescu 
4772e817cc15SEmil Constantinescu    Level: advanced
4773e817cc15SEmil Constantinescu 
4774e817cc15SEmil Constantinescu .keywords: TS, equation type
4775e817cc15SEmil Constantinescu 
4776e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4777e817cc15SEmil Constantinescu @*/
4778e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4779e817cc15SEmil Constantinescu {
4780e817cc15SEmil Constantinescu   PetscFunctionBegin;
4781e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4782e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4783e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4784e817cc15SEmil Constantinescu }
47850026cea9SSean Farley 
47864af1b03aSJed Brown #undef __FUNCT__
47874af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
47884af1b03aSJed Brown /*@
47894af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
47904af1b03aSJed Brown 
47914af1b03aSJed Brown    Not Collective
47924af1b03aSJed Brown 
47934af1b03aSJed Brown    Input Parameter:
47944af1b03aSJed Brown .  ts - the TS context
47954af1b03aSJed Brown 
47964af1b03aSJed Brown    Output Parameter:
47974af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
47984af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
47994af1b03aSJed Brown 
4800487e0bb9SJed Brown    Level: beginner
48014af1b03aSJed Brown 
4802cd652676SJed Brown    Notes:
4803cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
48044af1b03aSJed Brown 
48054af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
48064af1b03aSJed Brown 
48074af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
48084af1b03aSJed Brown @*/
48094af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
48104af1b03aSJed Brown {
48114af1b03aSJed Brown   PetscFunctionBegin;
48124af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48134af1b03aSJed Brown   PetscValidPointer(reason,2);
48144af1b03aSJed Brown   *reason = ts->reason;
48154af1b03aSJed Brown   PetscFunctionReturn(0);
48164af1b03aSJed Brown }
48174af1b03aSJed Brown 
4818fb1732b5SBarry Smith #undef __FUNCT__
4819d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
4820d6ad946cSShri Abhyankar /*@
4821d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4822d6ad946cSShri Abhyankar 
4823d6ad946cSShri Abhyankar    Not Collective
4824d6ad946cSShri Abhyankar 
4825d6ad946cSShri Abhyankar    Input Parameter:
4826d6ad946cSShri Abhyankar +  ts - the TS context
4827d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4828d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4829d6ad946cSShri Abhyankar 
4830f5abba47SShri Abhyankar    Level: advanced
4831d6ad946cSShri Abhyankar 
4832d6ad946cSShri Abhyankar    Notes:
4833d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4834d6ad946cSShri Abhyankar 
4835d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4836d6ad946cSShri Abhyankar 
4837d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4838d6ad946cSShri Abhyankar @*/
4839d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4840d6ad946cSShri Abhyankar {
4841d6ad946cSShri Abhyankar   PetscFunctionBegin;
4842d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4843d6ad946cSShri Abhyankar   ts->reason = reason;
4844d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4845d6ad946cSShri Abhyankar }
4846d6ad946cSShri Abhyankar 
4847d6ad946cSShri Abhyankar #undef __FUNCT__
4848cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
4849cc708dedSBarry Smith /*@
4850cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4851cc708dedSBarry Smith 
4852cc708dedSBarry Smith    Not Collective
4853cc708dedSBarry Smith 
4854cc708dedSBarry Smith    Input Parameter:
4855cc708dedSBarry Smith .  ts - the TS context
4856cc708dedSBarry Smith 
4857cc708dedSBarry Smith    Output Parameter:
4858cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
4859cc708dedSBarry Smith 
4860487e0bb9SJed Brown    Level: beginner
4861cc708dedSBarry Smith 
4862cc708dedSBarry Smith    Notes:
4863cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4864cc708dedSBarry Smith 
4865cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4866cc708dedSBarry Smith 
4867cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4868cc708dedSBarry Smith @*/
4869cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4870cc708dedSBarry Smith {
4871cc708dedSBarry Smith   PetscFunctionBegin;
4872cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4873cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4874cc708dedSBarry Smith   *ftime = ts->solvetime;
4875cc708dedSBarry Smith   PetscFunctionReturn(0);
4876cc708dedSBarry Smith }
4877cc708dedSBarry Smith 
4878cc708dedSBarry Smith #undef __FUNCT__
48792c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps"
48802c18e0fdSBarry Smith /*@
48812c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
48822c18e0fdSBarry Smith 
48832c18e0fdSBarry Smith    Not Collective
48842c18e0fdSBarry Smith 
48852c18e0fdSBarry Smith    Input Parameter:
48862c18e0fdSBarry Smith .  ts - the TS context
48872c18e0fdSBarry Smith 
48882c18e0fdSBarry Smith    Output Parameter:
48892c18e0fdSBarry Smith .  steps - the number of steps
48902c18e0fdSBarry Smith 
48912c18e0fdSBarry Smith    Level: beginner
48922c18e0fdSBarry Smith 
48932c18e0fdSBarry Smith    Notes:
48942c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
48952c18e0fdSBarry Smith 
48962c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
48972c18e0fdSBarry Smith 
48982c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
48992c18e0fdSBarry Smith @*/
49002c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
49012c18e0fdSBarry Smith {
49022c18e0fdSBarry Smith   PetscFunctionBegin;
49032c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49042c18e0fdSBarry Smith   PetscValidPointer(steps,2);
49052c18e0fdSBarry Smith   *steps = ts->total_steps;
49062c18e0fdSBarry Smith   PetscFunctionReturn(0);
49072c18e0fdSBarry Smith }
49082c18e0fdSBarry Smith 
49092c18e0fdSBarry Smith #undef __FUNCT__
49105ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
49119f67acb7SJed Brown /*@
49125ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
49139f67acb7SJed Brown    used by the time integrator.
49149f67acb7SJed Brown 
49159f67acb7SJed Brown    Not Collective
49169f67acb7SJed Brown 
49179f67acb7SJed Brown    Input Parameter:
49189f67acb7SJed Brown .  ts - TS context
49199f67acb7SJed Brown 
49209f67acb7SJed Brown    Output Parameter:
49219f67acb7SJed Brown .  nits - number of nonlinear iterations
49229f67acb7SJed Brown 
49239f67acb7SJed Brown    Notes:
49249f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
49259f67acb7SJed Brown 
49269f67acb7SJed Brown    Level: intermediate
49279f67acb7SJed Brown 
49289f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
49299f67acb7SJed Brown 
49305ef26d82SJed Brown .seealso:  TSGetKSPIterations()
49319f67acb7SJed Brown @*/
49325ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
49339f67acb7SJed Brown {
49349f67acb7SJed Brown   PetscFunctionBegin;
49359f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49369f67acb7SJed Brown   PetscValidIntPointer(nits,2);
49375ef26d82SJed Brown   *nits = ts->snes_its;
49389f67acb7SJed Brown   PetscFunctionReturn(0);
49399f67acb7SJed Brown }
49409f67acb7SJed Brown 
49419f67acb7SJed Brown #undef __FUNCT__
49425ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
49439f67acb7SJed Brown /*@
49445ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
49459f67acb7SJed Brown    used by the time integrator.
49469f67acb7SJed Brown 
49479f67acb7SJed Brown    Not Collective
49489f67acb7SJed Brown 
49499f67acb7SJed Brown    Input Parameter:
49509f67acb7SJed Brown .  ts - TS context
49519f67acb7SJed Brown 
49529f67acb7SJed Brown    Output Parameter:
49539f67acb7SJed Brown .  lits - number of linear iterations
49549f67acb7SJed Brown 
49559f67acb7SJed Brown    Notes:
49569f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
49579f67acb7SJed Brown 
49589f67acb7SJed Brown    Level: intermediate
49599f67acb7SJed Brown 
49609f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
49619f67acb7SJed Brown 
49625ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
49639f67acb7SJed Brown @*/
49645ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
49659f67acb7SJed Brown {
49669f67acb7SJed Brown   PetscFunctionBegin;
49679f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49689f67acb7SJed Brown   PetscValidIntPointer(lits,2);
49695ef26d82SJed Brown   *lits = ts->ksp_its;
49709f67acb7SJed Brown   PetscFunctionReturn(0);
49719f67acb7SJed Brown }
49729f67acb7SJed Brown 
49739f67acb7SJed Brown #undef __FUNCT__
4974cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
4975cef5090cSJed Brown /*@
4976cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4977cef5090cSJed Brown 
4978cef5090cSJed Brown    Not Collective
4979cef5090cSJed Brown 
4980cef5090cSJed Brown    Input Parameter:
4981cef5090cSJed Brown .  ts - TS context
4982cef5090cSJed Brown 
4983cef5090cSJed Brown    Output Parameter:
4984cef5090cSJed Brown .  rejects - number of steps rejected
4985cef5090cSJed Brown 
4986cef5090cSJed Brown    Notes:
4987cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4988cef5090cSJed Brown 
4989cef5090cSJed Brown    Level: intermediate
4990cef5090cSJed Brown 
4991cef5090cSJed Brown .keywords: TS, get, number
4992cef5090cSJed Brown 
49935ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
4994cef5090cSJed Brown @*/
4995cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
4996cef5090cSJed Brown {
4997cef5090cSJed Brown   PetscFunctionBegin;
4998cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4999cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5000cef5090cSJed Brown   *rejects = ts->reject;
5001cef5090cSJed Brown   PetscFunctionReturn(0);
5002cef5090cSJed Brown }
5003cef5090cSJed Brown 
5004cef5090cSJed Brown #undef __FUNCT__
5005cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
5006cef5090cSJed Brown /*@
5007cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5008cef5090cSJed Brown 
5009cef5090cSJed Brown    Not Collective
5010cef5090cSJed Brown 
5011cef5090cSJed Brown    Input Parameter:
5012cef5090cSJed Brown .  ts - TS context
5013cef5090cSJed Brown 
5014cef5090cSJed Brown    Output Parameter:
5015cef5090cSJed Brown .  fails - number of failed nonlinear solves
5016cef5090cSJed Brown 
5017cef5090cSJed Brown    Notes:
5018cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5019cef5090cSJed Brown 
5020cef5090cSJed Brown    Level: intermediate
5021cef5090cSJed Brown 
5022cef5090cSJed Brown .keywords: TS, get, number
5023cef5090cSJed Brown 
50245ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5025cef5090cSJed Brown @*/
5026cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5027cef5090cSJed Brown {
5028cef5090cSJed Brown   PetscFunctionBegin;
5029cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5030cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5031cef5090cSJed Brown   *fails = ts->num_snes_failures;
5032cef5090cSJed Brown   PetscFunctionReturn(0);
5033cef5090cSJed Brown }
5034cef5090cSJed Brown 
5035cef5090cSJed Brown #undef __FUNCT__
5036cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
5037cef5090cSJed Brown /*@
5038cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5039cef5090cSJed Brown 
5040cef5090cSJed Brown    Not Collective
5041cef5090cSJed Brown 
5042cef5090cSJed Brown    Input Parameter:
5043cef5090cSJed Brown +  ts - TS context
5044cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5045cef5090cSJed Brown 
5046cef5090cSJed Brown    Notes:
5047cef5090cSJed Brown    The counter is reset to zero for each step
5048cef5090cSJed Brown 
5049cef5090cSJed Brown    Options Database Key:
5050cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5051cef5090cSJed Brown 
5052cef5090cSJed Brown    Level: intermediate
5053cef5090cSJed Brown 
5054cef5090cSJed Brown .keywords: TS, set, maximum, number
5055cef5090cSJed Brown 
50565ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5057cef5090cSJed Brown @*/
5058cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5059cef5090cSJed Brown {
5060cef5090cSJed Brown   PetscFunctionBegin;
5061cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5062cef5090cSJed Brown   ts->max_reject = rejects;
5063cef5090cSJed Brown   PetscFunctionReturn(0);
5064cef5090cSJed Brown }
5065cef5090cSJed Brown 
5066cef5090cSJed Brown #undef __FUNCT__
5067cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
5068cef5090cSJed Brown /*@
5069cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5070cef5090cSJed Brown 
5071cef5090cSJed Brown    Not Collective
5072cef5090cSJed Brown 
5073cef5090cSJed Brown    Input Parameter:
5074cef5090cSJed Brown +  ts - TS context
5075cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5076cef5090cSJed Brown 
5077cef5090cSJed Brown    Notes:
5078cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5079cef5090cSJed Brown 
5080cef5090cSJed Brown    Options Database Key:
5081cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5082cef5090cSJed Brown 
5083cef5090cSJed Brown    Level: intermediate
5084cef5090cSJed Brown 
5085cef5090cSJed Brown .keywords: TS, set, maximum, number
5086cef5090cSJed Brown 
50875ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5088cef5090cSJed Brown @*/
5089cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5090cef5090cSJed Brown {
5091cef5090cSJed Brown   PetscFunctionBegin;
5092cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5093cef5090cSJed Brown   ts->max_snes_failures = fails;
5094cef5090cSJed Brown   PetscFunctionReturn(0);
5095cef5090cSJed Brown }
5096cef5090cSJed Brown 
5097cef5090cSJed Brown #undef __FUNCT__
50984e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
5099cef5090cSJed Brown /*@
5100cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5101cef5090cSJed Brown 
5102cef5090cSJed Brown    Not Collective
5103cef5090cSJed Brown 
5104cef5090cSJed Brown    Input Parameter:
5105cef5090cSJed Brown +  ts - TS context
5106cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5107cef5090cSJed Brown 
5108cef5090cSJed Brown    Options Database Key:
5109cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5110cef5090cSJed Brown 
5111cef5090cSJed Brown    Level: intermediate
5112cef5090cSJed Brown 
5113cef5090cSJed Brown .keywords: TS, set, error
5114cef5090cSJed Brown 
51155ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5116cef5090cSJed Brown @*/
5117cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5118cef5090cSJed Brown {
5119cef5090cSJed Brown   PetscFunctionBegin;
5120cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5121cef5090cSJed Brown   ts->errorifstepfailed = err;
5122cef5090cSJed Brown   PetscFunctionReturn(0);
5123cef5090cSJed Brown }
5124cef5090cSJed Brown 
5125cef5090cSJed Brown #undef __FUNCT__
5126fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSolution"
5127fb1732b5SBarry Smith /*@C
5128fde5950dSBarry 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
5129fb1732b5SBarry Smith 
5130fb1732b5SBarry Smith    Collective on TS
5131fb1732b5SBarry Smith 
5132fb1732b5SBarry Smith    Input Parameters:
5133fb1732b5SBarry Smith +  ts - the TS context
5134fb1732b5SBarry Smith .  step - current time-step
5135fb1732b5SBarry Smith .  ptime - current time
51360910c330SBarry Smith .  u - current state
5137fb1732b5SBarry Smith -  viewer - binary viewer
5138fb1732b5SBarry Smith 
5139fb1732b5SBarry Smith    Level: intermediate
5140fb1732b5SBarry Smith 
5141fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5142fb1732b5SBarry Smith 
5143fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5144fb1732b5SBarry Smith @*/
5145fde5950dSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
5146fb1732b5SBarry Smith {
5147fb1732b5SBarry Smith   PetscErrorCode ierr;
5148ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
5149fb1732b5SBarry Smith 
5150fb1732b5SBarry Smith   PetscFunctionBegin;
51510910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
5152ed81e22dSJed Brown   PetscFunctionReturn(0);
5153ed81e22dSJed Brown }
5154ed81e22dSJed Brown 
5155ed81e22dSJed Brown #undef __FUNCT__
5156ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
5157ed81e22dSJed Brown /*@C
5158ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5159ed81e22dSJed Brown 
5160ed81e22dSJed Brown    Collective on TS
5161ed81e22dSJed Brown 
5162ed81e22dSJed Brown    Input Parameters:
5163ed81e22dSJed Brown +  ts - the TS context
5164ed81e22dSJed Brown .  step - current time-step
5165ed81e22dSJed Brown .  ptime - current time
51660910c330SBarry Smith .  u - current state
5167ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5168ed81e22dSJed Brown 
5169ed81e22dSJed Brown    Level: intermediate
5170ed81e22dSJed Brown 
5171ed81e22dSJed Brown    Notes:
5172ed81e22dSJed 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.
5173ed81e22dSJed Brown    These are named according to the file name template.
5174ed81e22dSJed Brown 
5175ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5176ed81e22dSJed Brown 
5177ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5178ed81e22dSJed Brown 
5179ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5180ed81e22dSJed Brown @*/
51810910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5182ed81e22dSJed Brown {
5183ed81e22dSJed Brown   PetscErrorCode ierr;
5184ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5185ed81e22dSJed Brown   PetscViewer    viewer;
5186ed81e22dSJed Brown 
5187ed81e22dSJed Brown   PetscFunctionBegin;
51888caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5189ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
51900910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5191ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5192ed81e22dSJed Brown   PetscFunctionReturn(0);
5193ed81e22dSJed Brown }
5194ed81e22dSJed Brown 
5195ed81e22dSJed Brown #undef __FUNCT__
5196ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
5197ed81e22dSJed Brown /*@C
5198ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5199ed81e22dSJed Brown 
5200ed81e22dSJed Brown    Collective on TS
5201ed81e22dSJed Brown 
5202ed81e22dSJed Brown    Input Parameters:
5203ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5204ed81e22dSJed Brown 
5205ed81e22dSJed Brown    Level: intermediate
5206ed81e22dSJed Brown 
5207ed81e22dSJed Brown    Note:
5208ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5209ed81e22dSJed Brown 
5210ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5211ed81e22dSJed Brown 
5212ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5213ed81e22dSJed Brown @*/
5214ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5215ed81e22dSJed Brown {
5216ed81e22dSJed Brown   PetscErrorCode ierr;
5217ed81e22dSJed Brown 
5218ed81e22dSJed Brown   PetscFunctionBegin;
5219ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5220fb1732b5SBarry Smith   PetscFunctionReturn(0);
5221fb1732b5SBarry Smith }
5222fb1732b5SBarry Smith 
522384df9cb4SJed Brown #undef __FUNCT__
5224552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
522584df9cb4SJed Brown /*@
5226552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
522784df9cb4SJed Brown 
5228ed81e22dSJed Brown    Collective on TS if controller has not been created yet
522984df9cb4SJed Brown 
523084df9cb4SJed Brown    Input Arguments:
5231ed81e22dSJed Brown .  ts - time stepping context
523284df9cb4SJed Brown 
523384df9cb4SJed Brown    Output Arguments:
5234ed81e22dSJed Brown .  adapt - adaptive controller
523584df9cb4SJed Brown 
523684df9cb4SJed Brown    Level: intermediate
523784df9cb4SJed Brown 
5238ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
523984df9cb4SJed Brown @*/
5240552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
524184df9cb4SJed Brown {
524284df9cb4SJed Brown   PetscErrorCode ierr;
524384df9cb4SJed Brown 
524484df9cb4SJed Brown   PetscFunctionBegin;
524584df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
524684df9cb4SJed Brown   PetscValidPointer(adapt,2);
524784df9cb4SJed Brown   if (!ts->adapt) {
5248ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
52493bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
52501c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
525184df9cb4SJed Brown   }
525284df9cb4SJed Brown   *adapt = ts->adapt;
525384df9cb4SJed Brown   PetscFunctionReturn(0);
525484df9cb4SJed Brown }
5255d6ebe24aSShri Abhyankar 
5256d6ebe24aSShri Abhyankar #undef __FUNCT__
52571c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
52581c3436cfSJed Brown /*@
52591c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
52601c3436cfSJed Brown 
52611c3436cfSJed Brown    Logically Collective
52621c3436cfSJed Brown 
52631c3436cfSJed Brown    Input Arguments:
52641c3436cfSJed Brown +  ts - time integration context
52651c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
52660298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
52671c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
52680298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
52691c3436cfSJed Brown 
5270a3cdaa26SBarry Smith    Options Database keys:
5271a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5272a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5273a3cdaa26SBarry Smith 
52743ff766beSShri Abhyankar    Notes:
52753ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
52763ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
52773ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
52783ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
52793ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
52803ff766beSShri Abhyankar    differential variables.
52813ff766beSShri Abhyankar 
52821c3436cfSJed Brown    Level: beginner
52831c3436cfSJed Brown 
5284c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
52851c3436cfSJed Brown @*/
52861c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
52871c3436cfSJed Brown {
52881c3436cfSJed Brown   PetscErrorCode ierr;
52891c3436cfSJed Brown 
52901c3436cfSJed Brown   PetscFunctionBegin;
5291c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
52921c3436cfSJed Brown   if (vatol) {
52931c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
52941c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
5295bbd56ea5SKarl Rupp 
52961c3436cfSJed Brown     ts->vatol = vatol;
52971c3436cfSJed Brown   }
5298c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
52991c3436cfSJed Brown   if (vrtol) {
53001c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
53011c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
5302bbd56ea5SKarl Rupp 
53031c3436cfSJed Brown     ts->vrtol = vrtol;
53041c3436cfSJed Brown   }
53051c3436cfSJed Brown   PetscFunctionReturn(0);
53061c3436cfSJed Brown }
53071c3436cfSJed Brown 
53081c3436cfSJed Brown #undef __FUNCT__
5309c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
5310c5033834SJed Brown /*@
5311c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5312c5033834SJed Brown 
5313c5033834SJed Brown    Logically Collective
5314c5033834SJed Brown 
5315c5033834SJed Brown    Input Arguments:
5316c5033834SJed Brown .  ts - time integration context
5317c5033834SJed Brown 
5318c5033834SJed Brown    Output Arguments:
53190298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
53200298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
53210298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
53220298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5323c5033834SJed Brown 
5324c5033834SJed Brown    Level: beginner
5325c5033834SJed Brown 
5326c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5327c5033834SJed Brown @*/
5328c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5329c5033834SJed Brown {
5330c5033834SJed Brown   PetscFunctionBegin;
5331c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5332c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5333c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5334c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5335c5033834SJed Brown   PetscFunctionReturn(0);
5336c5033834SJed Brown }
5337c5033834SJed Brown 
5338c5033834SJed Brown #undef __FUNCT__
53399c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2"
53409c6b16b5SShri Abhyankar /*@
5341a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
53429c6b16b5SShri Abhyankar 
53439c6b16b5SShri Abhyankar    Collective on TS
53449c6b16b5SShri Abhyankar 
53459c6b16b5SShri Abhyankar    Input Arguments:
53469c6b16b5SShri Abhyankar +  ts - time stepping context
5347a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5348a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
53499c6b16b5SShri Abhyankar 
53509c6b16b5SShri Abhyankar    Output Arguments:
53519c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
53529c6b16b5SShri Abhyankar 
53539c6b16b5SShri Abhyankar    Level: developer
53549c6b16b5SShri Abhyankar 
5355deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
53569c6b16b5SShri Abhyankar @*/
5357a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm)
53589c6b16b5SShri Abhyankar {
53599c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
53609c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
53619c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
53629c6b16b5SShri Abhyankar   PetscReal         sum,gsum;
53639c6b16b5SShri Abhyankar   PetscReal         tol;
53649c6b16b5SShri Abhyankar 
53659c6b16b5SShri Abhyankar   PetscFunctionBegin;
53669c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5367a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5368a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5369a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5370a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5371a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5372a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
5373a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
53749c6b16b5SShri Abhyankar 
53759c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
53769c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
53779c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
53789c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
53799c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
53809c6b16b5SShri Abhyankar   sum  = 0.;
53819c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
53829c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
53839c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
53849c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
53859c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
53869c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
53879c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
53889c6b16b5SShri Abhyankar     }
53899c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
53909c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
53919c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
53929c6b16b5SShri Abhyankar     const PetscScalar *atol;
53939c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
53949c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
53959c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
53969c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
53979c6b16b5SShri Abhyankar     }
53989c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
53999c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
54009c6b16b5SShri Abhyankar     const PetscScalar *rtol;
54019c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54029c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54039c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54049c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
54059c6b16b5SShri Abhyankar     }
54069c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54079c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
54089c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54099c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54109c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
54119c6b16b5SShri Abhyankar     }
54129c6b16b5SShri Abhyankar   }
54139c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
54149c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
54159c6b16b5SShri Abhyankar 
5416b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
54179c6b16b5SShri Abhyankar   *norm = PetscSqrtReal(gsum / N);
54189c6b16b5SShri Abhyankar 
54199c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
54209c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
54219c6b16b5SShri Abhyankar }
54229c6b16b5SShri Abhyankar 
54239c6b16b5SShri Abhyankar #undef __FUNCT__
54249c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity"
54259c6b16b5SShri Abhyankar /*@
5426a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
54279c6b16b5SShri Abhyankar 
54289c6b16b5SShri Abhyankar    Collective on TS
54299c6b16b5SShri Abhyankar 
54309c6b16b5SShri Abhyankar    Input Arguments:
54319c6b16b5SShri Abhyankar +  ts - time stepping context
5432a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5433a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
54349c6b16b5SShri Abhyankar 
54359c6b16b5SShri Abhyankar    Output Arguments:
54369c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
54379c6b16b5SShri Abhyankar 
54389c6b16b5SShri Abhyankar    Level: developer
54399c6b16b5SShri Abhyankar 
5440deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
54419c6b16b5SShri Abhyankar @*/
5442a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm)
54439c6b16b5SShri Abhyankar {
54449c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
54459c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart,k;
54469c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
54479c6b16b5SShri Abhyankar   PetscReal         max,gmax;
54489c6b16b5SShri Abhyankar   PetscReal         tol;
54499c6b16b5SShri Abhyankar 
54509c6b16b5SShri Abhyankar   PetscFunctionBegin;
54519c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5452a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5453a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5454a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5455a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5456a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5457a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
5458a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
54599c6b16b5SShri Abhyankar 
54609c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
54619c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
54629c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
54639c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
54649c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
54659c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
54669c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
54679c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54689c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54699c6b16b5SShri Abhyankar     k = 0;
54709c6b16b5SShri Abhyankar     tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
54719c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
54729c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
54739c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54749c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
54759c6b16b5SShri Abhyankar     }
54769c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54779c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54789c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
54799c6b16b5SShri Abhyankar     const PetscScalar *atol;
54809c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54819c6b16b5SShri Abhyankar     k = 0;
54829c6b16b5SShri Abhyankar     tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
54839c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
54849c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
54859c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54869c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
54879c6b16b5SShri Abhyankar     }
54889c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54899c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
54909c6b16b5SShri Abhyankar     const PetscScalar *rtol;
54919c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54929c6b16b5SShri Abhyankar     k = 0;
54939c6b16b5SShri Abhyankar     tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
54949c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
54959c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
54969c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54979c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
54989c6b16b5SShri Abhyankar     }
54999c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55009c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
55019c6b16b5SShri Abhyankar     k = 0;
55029c6b16b5SShri Abhyankar     tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
55039c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
55049c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
55059c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55069c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
55079c6b16b5SShri Abhyankar     }
55089c6b16b5SShri Abhyankar   }
55099c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
55109c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
55119c6b16b5SShri Abhyankar 
5512b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
55139c6b16b5SShri Abhyankar   *norm = gmax;
55149c6b16b5SShri Abhyankar 
55159c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
55169c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
55179c6b16b5SShri Abhyankar }
55189c6b16b5SShri Abhyankar 
55199c6b16b5SShri Abhyankar #undef __FUNCT__
55207619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm"
55211c3436cfSJed Brown /*@
5522a4868fbcSLisandro Dalcin    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors
55231c3436cfSJed Brown 
55241c3436cfSJed Brown    Collective on TS
55251c3436cfSJed Brown 
55261c3436cfSJed Brown    Input Arguments:
55271c3436cfSJed Brown +  ts - time stepping context
5528a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5529a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5530a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
55317619abb3SShri 
55321c3436cfSJed Brown    Output Arguments:
55331c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
55341c3436cfSJed Brown 
5535a4868fbcSLisandro Dalcin 
5536a4868fbcSLisandro Dalcin    Options Database Keys:
5537a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5538a4868fbcSLisandro Dalcin 
55391c3436cfSJed Brown    Level: developer
55401c3436cfSJed Brown 
5541deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
55421c3436cfSJed Brown @*/
5543a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm)
55441c3436cfSJed Brown {
55458beabaa1SBarry Smith   PetscErrorCode ierr;
55461c3436cfSJed Brown 
55471c3436cfSJed Brown   PetscFunctionBegin;
5548a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
5549a4868fbcSLisandro Dalcin     ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr);
5550a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
5551a4868fbcSLisandro Dalcin     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr);
5552a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
55531c3436cfSJed Brown   PetscFunctionReturn(0);
55541c3436cfSJed Brown }
55551c3436cfSJed Brown 
55561c3436cfSJed Brown #undef __FUNCT__
55578d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
55588d59e960SJed Brown /*@
55598d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
55608d59e960SJed Brown 
55618d59e960SJed Brown    Logically Collective on TS
55628d59e960SJed Brown 
55638d59e960SJed Brown    Input Arguments:
55648d59e960SJed Brown +  ts - time stepping context
55658d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
55668d59e960SJed Brown 
55678d59e960SJed Brown    Note:
55688d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
55698d59e960SJed Brown 
55708d59e960SJed Brown    Level: intermediate
55718d59e960SJed Brown 
55728d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
55738d59e960SJed Brown @*/
55748d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
55758d59e960SJed Brown {
55768d59e960SJed Brown   PetscFunctionBegin;
55778d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
55788d59e960SJed Brown   ts->cfltime_local = cfltime;
55798d59e960SJed Brown   ts->cfltime       = -1.;
55808d59e960SJed Brown   PetscFunctionReturn(0);
55818d59e960SJed Brown }
55828d59e960SJed Brown 
55838d59e960SJed Brown #undef __FUNCT__
55848d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
55858d59e960SJed Brown /*@
55868d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
55878d59e960SJed Brown 
55888d59e960SJed Brown    Collective on TS
55898d59e960SJed Brown 
55908d59e960SJed Brown    Input Arguments:
55918d59e960SJed Brown .  ts - time stepping context
55928d59e960SJed Brown 
55938d59e960SJed Brown    Output Arguments:
55948d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
55958d59e960SJed Brown 
55968d59e960SJed Brown    Level: advanced
55978d59e960SJed Brown 
55988d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
55998d59e960SJed Brown @*/
56008d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
56018d59e960SJed Brown {
56028d59e960SJed Brown   PetscErrorCode ierr;
56038d59e960SJed Brown 
56048d59e960SJed Brown   PetscFunctionBegin;
56058d59e960SJed Brown   if (ts->cfltime < 0) {
5606b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
56078d59e960SJed Brown   }
56088d59e960SJed Brown   *cfltime = ts->cfltime;
56098d59e960SJed Brown   PetscFunctionReturn(0);
56108d59e960SJed Brown }
56118d59e960SJed Brown 
56128d59e960SJed Brown #undef __FUNCT__
5613d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
5614d6ebe24aSShri Abhyankar /*@
5615d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5616d6ebe24aSShri Abhyankar 
5617d6ebe24aSShri Abhyankar    Input Parameters:
5618d6ebe24aSShri Abhyankar .  ts   - the TS context.
5619d6ebe24aSShri Abhyankar .  xl   - lower bound.
5620d6ebe24aSShri Abhyankar .  xu   - upper bound.
5621d6ebe24aSShri Abhyankar 
5622d6ebe24aSShri Abhyankar    Notes:
5623d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
5624e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
5625d6ebe24aSShri Abhyankar 
56262bd2b0e6SSatish Balay    Level: advanced
56272bd2b0e6SSatish Balay 
5628d6ebe24aSShri Abhyankar @*/
5629d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5630d6ebe24aSShri Abhyankar {
5631d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
5632d6ebe24aSShri Abhyankar   SNES           snes;
5633d6ebe24aSShri Abhyankar 
5634d6ebe24aSShri Abhyankar   PetscFunctionBegin;
5635d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5636d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
5637d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5638d6ebe24aSShri Abhyankar }
5639d6ebe24aSShri Abhyankar 
5640325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
5641c6db04a5SJed Brown #include <mex.h>
5642325fc9f4SBarry Smith 
5643325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
5644325fc9f4SBarry Smith 
5645325fc9f4SBarry Smith #undef __FUNCT__
5646325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
5647325fc9f4SBarry Smith /*
5648325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
5649325fc9f4SBarry Smith                          TSSetFunctionMatlab().
5650325fc9f4SBarry Smith 
5651325fc9f4SBarry Smith    Collective on TS
5652325fc9f4SBarry Smith 
5653325fc9f4SBarry Smith    Input Parameters:
5654325fc9f4SBarry Smith +  snes - the TS context
56550910c330SBarry Smith -  u - input vector
5656325fc9f4SBarry Smith 
5657325fc9f4SBarry Smith    Output Parameter:
5658325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
5659325fc9f4SBarry Smith 
5660325fc9f4SBarry Smith    Notes:
5661325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
5662325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
5663325fc9f4SBarry Smith    themselves.
5664325fc9f4SBarry Smith 
5665325fc9f4SBarry Smith    Level: developer
5666325fc9f4SBarry Smith 
5667325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5668325fc9f4SBarry Smith 
5669325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5670325fc9f4SBarry Smith */
56710910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
5672325fc9f4SBarry Smith {
5673325fc9f4SBarry Smith   PetscErrorCode  ierr;
5674325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5675325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
5676325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
5677325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
5678325fc9f4SBarry Smith 
5679325fc9f4SBarry Smith   PetscFunctionBegin;
5680325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
56810910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
56820910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
5683325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
56840910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
5685325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
5686325fc9f4SBarry Smith 
5687325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
56880910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
56890910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
56900910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
5691bbd56ea5SKarl Rupp 
5692325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5693325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
5694325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5695325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5696325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
5697325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
5698325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
5699325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
5700325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5701325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5702325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5703325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5704325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5705325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5706325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5707325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5708325fc9f4SBarry Smith   PetscFunctionReturn(0);
5709325fc9f4SBarry Smith }
5710325fc9f4SBarry Smith 
5711325fc9f4SBarry Smith 
5712325fc9f4SBarry Smith #undef __FUNCT__
5713325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
5714325fc9f4SBarry Smith /*
5715325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
5716325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
5717e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
5718325fc9f4SBarry Smith 
5719325fc9f4SBarry Smith    Logically Collective on TS
5720325fc9f4SBarry Smith 
5721325fc9f4SBarry Smith    Input Parameters:
5722325fc9f4SBarry Smith +  ts - the TS context
5723325fc9f4SBarry Smith -  func - function evaluation routine
5724325fc9f4SBarry Smith 
5725325fc9f4SBarry Smith    Calling sequence of func:
57260910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
5727325fc9f4SBarry Smith 
5728325fc9f4SBarry Smith    Level: beginner
5729325fc9f4SBarry Smith 
5730325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5731325fc9f4SBarry Smith 
5732325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5733325fc9f4SBarry Smith */
5734cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
5735325fc9f4SBarry Smith {
5736325fc9f4SBarry Smith   PetscErrorCode  ierr;
5737325fc9f4SBarry Smith   TSMatlabContext *sctx;
5738325fc9f4SBarry Smith 
5739325fc9f4SBarry Smith   PetscFunctionBegin;
5740325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5741325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5742325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5743325fc9f4SBarry Smith   /*
5744325fc9f4SBarry Smith      This should work, but it doesn't
5745325fc9f4SBarry Smith   sctx->ctx = ctx;
5746325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5747325fc9f4SBarry Smith   */
5748325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5749bbd56ea5SKarl Rupp 
57500298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
5751325fc9f4SBarry Smith   PetscFunctionReturn(0);
5752325fc9f4SBarry Smith }
5753325fc9f4SBarry Smith 
5754325fc9f4SBarry Smith #undef __FUNCT__
5755325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
5756325fc9f4SBarry Smith /*
5757325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
5758325fc9f4SBarry Smith                          TSSetJacobianMatlab().
5759325fc9f4SBarry Smith 
5760325fc9f4SBarry Smith    Collective on TS
5761325fc9f4SBarry Smith 
5762325fc9f4SBarry Smith    Input Parameters:
5763cdcf91faSSean Farley +  ts - the TS context
57640910c330SBarry Smith .  u - input vector
5765325fc9f4SBarry Smith .  A, B - the matrices
5766325fc9f4SBarry Smith -  ctx - user context
5767325fc9f4SBarry Smith 
5768325fc9f4SBarry Smith    Level: developer
5769325fc9f4SBarry Smith 
5770325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5771325fc9f4SBarry Smith 
5772325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5773325fc9f4SBarry Smith @*/
5774f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
5775325fc9f4SBarry Smith {
5776325fc9f4SBarry Smith   PetscErrorCode  ierr;
5777325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5778325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
5779325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
5780325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
5781325fc9f4SBarry Smith 
5782325fc9f4SBarry Smith   PetscFunctionBegin;
5783cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57840910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
5785325fc9f4SBarry Smith 
57860910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
5787325fc9f4SBarry Smith 
5788cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
57890910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
57900910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
57910910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
57920910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
5793bbd56ea5SKarl Rupp 
5794325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5795325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
5796325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5797325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5798325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
5799325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
5800325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
5801325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
5802325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
5803325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
5804325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5805325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5806325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5807325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5808325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5809325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5810325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5811325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
5812325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
5813325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5814325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
5815325fc9f4SBarry Smith   PetscFunctionReturn(0);
5816325fc9f4SBarry Smith }
5817325fc9f4SBarry Smith 
5818325fc9f4SBarry Smith 
5819325fc9f4SBarry Smith #undef __FUNCT__
5820325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
5821325fc9f4SBarry Smith /*
5822325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
5823e3c5b3baSBarry 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
5824325fc9f4SBarry Smith 
5825325fc9f4SBarry Smith    Logically Collective on TS
5826325fc9f4SBarry Smith 
5827325fc9f4SBarry Smith    Input Parameters:
5828cdcf91faSSean Farley +  ts - the TS context
5829325fc9f4SBarry Smith .  A,B - Jacobian matrices
5830325fc9f4SBarry Smith .  func - function evaluation routine
5831325fc9f4SBarry Smith -  ctx - user context
5832325fc9f4SBarry Smith 
5833325fc9f4SBarry Smith    Calling sequence of func:
58340910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
5835325fc9f4SBarry Smith 
5836325fc9f4SBarry Smith 
5837325fc9f4SBarry Smith    Level: developer
5838325fc9f4SBarry Smith 
5839325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5840325fc9f4SBarry Smith 
5841325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5842325fc9f4SBarry Smith */
5843cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
5844325fc9f4SBarry Smith {
5845325fc9f4SBarry Smith   PetscErrorCode  ierr;
5846325fc9f4SBarry Smith   TSMatlabContext *sctx;
5847325fc9f4SBarry Smith 
5848325fc9f4SBarry Smith   PetscFunctionBegin;
5849325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5850325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5851325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5852325fc9f4SBarry Smith   /*
5853325fc9f4SBarry Smith      This should work, but it doesn't
5854325fc9f4SBarry Smith   sctx->ctx = ctx;
5855325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5856325fc9f4SBarry Smith   */
5857325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5858bbd56ea5SKarl Rupp 
5859cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
5860325fc9f4SBarry Smith   PetscFunctionReturn(0);
5861325fc9f4SBarry Smith }
5862325fc9f4SBarry Smith 
5863b5b1a830SBarry Smith #undef __FUNCT__
5864b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
5865b5b1a830SBarry Smith /*
5866b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
5867b5b1a830SBarry Smith 
5868b5b1a830SBarry Smith    Collective on TS
5869b5b1a830SBarry Smith 
5870b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5871b5b1a830SBarry Smith @*/
58720910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
5873b5b1a830SBarry Smith {
5874b5b1a830SBarry Smith   PetscErrorCode  ierr;
5875b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5876a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
5877b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
5878b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
5879b5b1a830SBarry Smith 
5880b5b1a830SBarry Smith   PetscFunctionBegin;
5881cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
58820910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
5883b5b1a830SBarry Smith 
5884cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
58850910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
5886bbd56ea5SKarl Rupp 
5887b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5888b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
5889b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
5890b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
5891b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
5892b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
5893b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
5894b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5895b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
5896b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
5897b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
5898b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
5899b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
5900b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
5901b5b1a830SBarry Smith   PetscFunctionReturn(0);
5902b5b1a830SBarry Smith }
5903b5b1a830SBarry Smith 
5904b5b1a830SBarry Smith 
5905b5b1a830SBarry Smith #undef __FUNCT__
5906b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
5907b5b1a830SBarry Smith /*
5908b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
5909b5b1a830SBarry Smith 
5910b5b1a830SBarry Smith    Level: developer
5911b5b1a830SBarry Smith 
5912b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
5913b5b1a830SBarry Smith 
5914b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5915b5b1a830SBarry Smith */
5916cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
5917b5b1a830SBarry Smith {
5918b5b1a830SBarry Smith   PetscErrorCode  ierr;
5919b5b1a830SBarry Smith   TSMatlabContext *sctx;
5920b5b1a830SBarry Smith 
5921b5b1a830SBarry Smith   PetscFunctionBegin;
5922b5b1a830SBarry Smith   /* currently sctx is memory bleed */
5923b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5924b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5925b5b1a830SBarry Smith   /*
5926b5b1a830SBarry Smith      This should work, but it doesn't
5927b5b1a830SBarry Smith   sctx->ctx = ctx;
5928b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5929b5b1a830SBarry Smith   */
5930b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5931bbd56ea5SKarl Rupp 
59320298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
5933b5b1a830SBarry Smith   PetscFunctionReturn(0);
5934b5b1a830SBarry Smith }
5935325fc9f4SBarry Smith #endif
5936b3603a34SBarry Smith 
5937b3603a34SBarry Smith #undef __FUNCT__
59384f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
5939b3603a34SBarry Smith /*@C
59404f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
5941b3603a34SBarry Smith        in a time based line graph
5942b3603a34SBarry Smith 
5943b3603a34SBarry Smith    Collective on TS
5944b3603a34SBarry Smith 
5945b3603a34SBarry Smith    Input Parameters:
5946b3603a34SBarry Smith +  ts - the TS context
5947b3603a34SBarry Smith .  step - current time-step
5948b3603a34SBarry Smith .  ptime - current time
59497db568b7SBarry Smith .  u - current solution
59507db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
5951b3603a34SBarry Smith 
5952b3d3934dSBarry Smith    Options Database:
59539ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
5954b3d3934dSBarry Smith 
5955b3603a34SBarry Smith    Level: intermediate
5956b3603a34SBarry Smith 
59570b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
5958b3603a34SBarry Smith 
5959b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
5960b3603a34SBarry Smith 
59617db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
59627db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
59637db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
59647db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
5965b3603a34SBarry Smith @*/
59667db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
5967b3603a34SBarry Smith {
5968b3603a34SBarry Smith   PetscErrorCode    ierr;
59697db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
5970b3603a34SBarry Smith   const PetscScalar *yy;
597158ff32f7SBarry Smith   PetscInt          dim;
597280666b62SBarry Smith   Vec               v;
5973b3603a34SBarry Smith 
5974b3603a34SBarry Smith   PetscFunctionBegin;
597558ff32f7SBarry Smith   if (!step) {
5976a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5977a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5978a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
5979387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
5980387f4636SBarry Smith       char      **displaynames;
5981387f4636SBarry Smith       PetscBool flg;
5982387f4636SBarry Smith 
5983387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
5984387f4636SBarry Smith       ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr);
5985387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
5986c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
5987387f4636SBarry Smith       if (flg) {
5988a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
5989387f4636SBarry Smith       }
5990387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
5991387f4636SBarry Smith     }
5992387f4636SBarry Smith     if (ctx->displaynames) {
5993387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
5994387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
5995387f4636SBarry Smith     } else if (ctx->names) {
59960910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
59970b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
5998387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
5999b0bc92c6SBarry Smith     } else {
6000b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6001b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6002387f4636SBarry Smith     }
60030b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
600458ff32f7SBarry Smith   }
600580666b62SBarry Smith   if (ctx->transform) {
6006e673d494SBarry Smith     ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);
600780666b62SBarry Smith   } else {
600880666b62SBarry Smith     v = u;
600980666b62SBarry Smith   }
601080666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
6011e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6012e3efe391SJed Brown   {
6013e3efe391SJed Brown     PetscReal *yreal;
6014e3efe391SJed Brown     PetscInt  i,n;
601580666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6016785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6017e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
60180b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6019e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6020e3efe391SJed Brown   }
6021e3efe391SJed Brown #else
6022387f4636SBarry Smith   if (ctx->displaynames) {
6023387f4636SBarry Smith     PetscInt i;
6024387f4636SBarry Smith     for (i=0; i<ctx->ndisplayvariables; i++) {
6025387f4636SBarry Smith       ctx->displayvalues[i] = yy[ctx->displayvariables[i]];
6026387f4636SBarry Smith     }
6027387f4636SBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
6028387f4636SBarry Smith   } else {
60290b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6030387f4636SBarry Smith   }
6031e3efe391SJed Brown #endif
603280666b62SBarry Smith   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
603380666b62SBarry Smith   if (ctx->transform) {
603480666b62SBarry Smith     ierr = VecDestroy(&v);CHKERRQ(ierr);
603580666b62SBarry Smith   }
6036b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
60370b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
60383923b477SBarry Smith   }
6039b3603a34SBarry Smith   PetscFunctionReturn(0);
6040b3603a34SBarry Smith }
6041b3603a34SBarry Smith 
6042387f4636SBarry Smith 
6043b3603a34SBarry Smith #undef __FUNCT__
604431152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames"
6045b037adc7SBarry Smith /*@C
604631152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6047b037adc7SBarry Smith 
6048b037adc7SBarry Smith    Collective on TS
6049b037adc7SBarry Smith 
6050b037adc7SBarry Smith    Input Parameters:
6051b037adc7SBarry Smith +  ts - the TS context
6052b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6053b037adc7SBarry Smith 
6054b037adc7SBarry Smith    Level: intermediate
6055b037adc7SBarry Smith 
60567db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
60577db568b7SBarry Smith 
6058b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
6059b037adc7SBarry Smith 
6060a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6061b037adc7SBarry Smith @*/
606231152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6063b037adc7SBarry Smith {
6064b037adc7SBarry Smith   PetscErrorCode    ierr;
6065b037adc7SBarry Smith   PetscInt          i;
6066b037adc7SBarry Smith 
6067b037adc7SBarry Smith   PetscFunctionBegin;
6068b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6069b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
60705537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6071b3d3934dSBarry Smith       break;
6072b3d3934dSBarry Smith     }
6073b3d3934dSBarry Smith   }
6074b3d3934dSBarry Smith   PetscFunctionReturn(0);
6075b3d3934dSBarry Smith }
6076b3d3934dSBarry Smith 
6077b3d3934dSBarry Smith #undef __FUNCT__
6078e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames"
6079e673d494SBarry Smith /*@C
6080e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6081e673d494SBarry Smith 
6082e673d494SBarry Smith    Collective on TS
6083e673d494SBarry Smith 
6084e673d494SBarry Smith    Input Parameters:
6085e673d494SBarry Smith +  ts - the TS context
6086e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6087e673d494SBarry Smith 
6088e673d494SBarry Smith    Level: intermediate
6089e673d494SBarry Smith 
6090e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6091e673d494SBarry Smith 
6092a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6093e673d494SBarry Smith @*/
6094e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6095e673d494SBarry Smith {
6096e673d494SBarry Smith   PetscErrorCode    ierr;
6097e673d494SBarry Smith 
6098e673d494SBarry Smith   PetscFunctionBegin;
6099e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6100e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6101e673d494SBarry Smith   PetscFunctionReturn(0);
6102e673d494SBarry Smith }
6103e673d494SBarry Smith 
6104e673d494SBarry Smith #undef __FUNCT__
6105b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames"
6106b3d3934dSBarry Smith /*@C
6107b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6108b3d3934dSBarry Smith 
6109b3d3934dSBarry Smith    Collective on TS
6110b3d3934dSBarry Smith 
6111b3d3934dSBarry Smith    Input Parameter:
6112b3d3934dSBarry Smith .  ts - the TS context
6113b3d3934dSBarry Smith 
6114b3d3934dSBarry Smith    Output Parameter:
6115b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6116b3d3934dSBarry Smith 
6117b3d3934dSBarry Smith    Level: intermediate
6118b3d3934dSBarry Smith 
61197db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
61207db568b7SBarry Smith 
6121b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6122b3d3934dSBarry Smith 
6123b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6124b3d3934dSBarry Smith @*/
6125b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6126b3d3934dSBarry Smith {
6127b3d3934dSBarry Smith   PetscInt       i;
6128b3d3934dSBarry Smith 
6129b3d3934dSBarry Smith   PetscFunctionBegin;
6130b3d3934dSBarry Smith   *names = NULL;
6131b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6132b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
61335537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6134b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6135b3d3934dSBarry Smith       break;
6136387f4636SBarry Smith     }
6137387f4636SBarry Smith   }
6138387f4636SBarry Smith   PetscFunctionReturn(0);
6139387f4636SBarry Smith }
6140387f4636SBarry Smith 
6141387f4636SBarry Smith #undef __FUNCT__
6142a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables"
6143a66092f1SBarry Smith /*@C
6144a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6145a66092f1SBarry Smith 
6146a66092f1SBarry Smith    Collective on TS
6147a66092f1SBarry Smith 
6148a66092f1SBarry Smith    Input Parameters:
6149a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6150a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
6151a66092f1SBarry Smith 
6152a66092f1SBarry Smith    Level: intermediate
6153a66092f1SBarry Smith 
6154a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
6155a66092f1SBarry Smith 
6156a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6157a66092f1SBarry Smith @*/
6158a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6159a66092f1SBarry Smith {
6160a66092f1SBarry Smith   PetscInt          j = 0,k;
6161a66092f1SBarry Smith   PetscErrorCode    ierr;
6162a66092f1SBarry Smith 
6163a66092f1SBarry Smith   PetscFunctionBegin;
6164a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6165a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6166a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6167a66092f1SBarry Smith   while (displaynames[j]) j++;
6168a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6169a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6170a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6171a66092f1SBarry Smith   j = 0;
6172a66092f1SBarry Smith   while (displaynames[j]) {
6173a66092f1SBarry Smith     k = 0;
6174a66092f1SBarry Smith     while (ctx->names[k]) {
6175a66092f1SBarry Smith       PetscBool flg;
6176a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6177a66092f1SBarry Smith       if (flg) {
6178a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6179a66092f1SBarry Smith         break;
6180a66092f1SBarry Smith       }
6181a66092f1SBarry Smith       k++;
6182a66092f1SBarry Smith     }
6183a66092f1SBarry Smith     j++;
6184a66092f1SBarry Smith   }
6185a66092f1SBarry Smith   PetscFunctionReturn(0);
6186a66092f1SBarry Smith }
6187a66092f1SBarry Smith 
6188a66092f1SBarry Smith 
6189a66092f1SBarry Smith #undef __FUNCT__
6190387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables"
6191387f4636SBarry Smith /*@C
6192387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6193387f4636SBarry Smith 
6194387f4636SBarry Smith    Collective on TS
6195387f4636SBarry Smith 
6196387f4636SBarry Smith    Input Parameters:
6197387f4636SBarry Smith +  ts - the TS context
6198387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
6199387f4636SBarry Smith 
62007db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
62017db568b7SBarry Smith 
6202387f4636SBarry Smith    Level: intermediate
6203387f4636SBarry Smith 
6204387f4636SBarry Smith .keywords: TS,  vector, monitor, view
6205387f4636SBarry Smith 
6206387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6207387f4636SBarry Smith @*/
6208387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6209387f4636SBarry Smith {
6210a66092f1SBarry Smith   PetscInt          i;
6211387f4636SBarry Smith   PetscErrorCode    ierr;
6212387f4636SBarry Smith 
6213387f4636SBarry Smith   PetscFunctionBegin;
6214387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6215387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
62165537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6217b3d3934dSBarry Smith       break;
6218b037adc7SBarry Smith     }
6219b037adc7SBarry Smith   }
6220b037adc7SBarry Smith   PetscFunctionReturn(0);
6221b037adc7SBarry Smith }
6222b037adc7SBarry Smith 
6223b037adc7SBarry Smith #undef __FUNCT__
622480666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform"
622580666b62SBarry Smith /*@C
622680666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
622780666b62SBarry Smith 
622880666b62SBarry Smith    Collective on TS
622980666b62SBarry Smith 
623080666b62SBarry Smith    Input Parameters:
623180666b62SBarry Smith +  ts - the TS context
623280666b62SBarry Smith .  transform - the transform function
62337684fa3eSBarry Smith .  destroy - function to destroy the optional context
623480666b62SBarry Smith -  ctx - optional context used by transform function
623580666b62SBarry Smith 
62367db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
62377db568b7SBarry Smith 
623880666b62SBarry Smith    Level: intermediate
623980666b62SBarry Smith 
624080666b62SBarry Smith .keywords: TS,  vector, monitor, view
624180666b62SBarry Smith 
6242a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
624380666b62SBarry Smith @*/
62447684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
624580666b62SBarry Smith {
624680666b62SBarry Smith   PetscInt          i;
6247a66092f1SBarry Smith   PetscErrorCode    ierr;
624880666b62SBarry Smith 
624980666b62SBarry Smith   PetscFunctionBegin;
625080666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
625180666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
62525537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
625380666b62SBarry Smith     }
625480666b62SBarry Smith   }
625580666b62SBarry Smith   PetscFunctionReturn(0);
625680666b62SBarry Smith }
625780666b62SBarry Smith 
625880666b62SBarry Smith #undef __FUNCT__
6259e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform"
6260e673d494SBarry Smith /*@C
6261e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6262e673d494SBarry Smith 
6263e673d494SBarry Smith    Collective on TSLGCtx
6264e673d494SBarry Smith 
6265e673d494SBarry Smith    Input Parameters:
6266e673d494SBarry Smith +  ts - the TS context
6267e673d494SBarry Smith .  transform - the transform function
62687684fa3eSBarry Smith .  destroy - function to destroy the optional context
6269e673d494SBarry Smith -  ctx - optional context used by transform function
6270e673d494SBarry Smith 
6271e673d494SBarry Smith    Level: intermediate
6272e673d494SBarry Smith 
6273e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6274e673d494SBarry Smith 
6275a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6276e673d494SBarry Smith @*/
62777684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6278e673d494SBarry Smith {
6279e673d494SBarry Smith   PetscFunctionBegin;
6280e673d494SBarry Smith   ctx->transform    = transform;
62817684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6282e673d494SBarry Smith   ctx->transformctx = tctx;
6283e673d494SBarry Smith   PetscFunctionReturn(0);
6284e673d494SBarry Smith }
6285e673d494SBarry Smith 
6286b3603a34SBarry Smith #undef __FUNCT__
62874f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
6288ef20d060SBarry Smith /*@C
62894f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
6290ef20d060SBarry Smith        in a time based line graph
6291ef20d060SBarry Smith 
6292ef20d060SBarry Smith    Collective on TS
6293ef20d060SBarry Smith 
6294ef20d060SBarry Smith    Input Parameters:
6295ef20d060SBarry Smith +  ts - the TS context
6296ef20d060SBarry Smith .  step - current time-step
6297ef20d060SBarry Smith .  ptime - current time
62987db568b7SBarry Smith .  u - current solution
62997db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6300ef20d060SBarry Smith 
6301ef20d060SBarry Smith    Level: intermediate
6302ef20d060SBarry Smith 
6303abd5a294SJed Brown    Notes:
6304abd5a294SJed Brown    Only for sequential solves.
6305abd5a294SJed Brown 
6306abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6307abd5a294SJed Brown 
6308abd5a294SJed Brown    Options Database Keys:
63094f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6310ef20d060SBarry Smith 
6311ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
6312ef20d060SBarry Smith 
6313abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6314ef20d060SBarry Smith @*/
63150910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6316ef20d060SBarry Smith {
6317ef20d060SBarry Smith   PetscErrorCode    ierr;
63180b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6319ef20d060SBarry Smith   const PetscScalar *yy;
6320ef20d060SBarry Smith   Vec               y;
6321a9f9c1f6SBarry Smith   PetscInt          dim;
6322ef20d060SBarry Smith 
6323ef20d060SBarry Smith   PetscFunctionBegin;
6324a9f9c1f6SBarry Smith   if (!step) {
6325a9f9c1f6SBarry Smith     PetscDrawAxis axis;
6326a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
63271ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
63280910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6329a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6330a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6331a9f9c1f6SBarry Smith   }
63320910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6333ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
63340910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6335ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6336e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6337e3efe391SJed Brown   {
6338e3efe391SJed Brown     PetscReal *yreal;
6339e3efe391SJed Brown     PetscInt  i,n;
6340e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6341785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6342e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
63430b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6344e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6345e3efe391SJed Brown   }
6346e3efe391SJed Brown #else
63470b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6348e3efe391SJed Brown #endif
6349ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6350ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6351b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
63520b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
63533923b477SBarry Smith   }
6354ef20d060SBarry Smith   PetscFunctionReturn(0);
6355ef20d060SBarry Smith }
6356ef20d060SBarry Smith 
6357201da799SBarry Smith #undef __FUNCT__
6358201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
6359201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6360201da799SBarry Smith {
6361201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6362201da799SBarry Smith   PetscReal      x   = ptime,y;
6363201da799SBarry Smith   PetscErrorCode ierr;
6364201da799SBarry Smith   PetscInt       its;
6365201da799SBarry Smith 
6366201da799SBarry Smith   PetscFunctionBegin;
6367201da799SBarry Smith   if (!n) {
6368201da799SBarry Smith     PetscDrawAxis axis;
6369bbd56ea5SKarl Rupp 
6370201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6371201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
6372201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6373bbd56ea5SKarl Rupp 
6374201da799SBarry Smith     ctx->snes_its = 0;
6375201da799SBarry Smith   }
6376201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
6377201da799SBarry Smith   y    = its - ctx->snes_its;
6378201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
63793fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6380201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
6381201da799SBarry Smith   }
6382201da799SBarry Smith   ctx->snes_its = its;
6383201da799SBarry Smith   PetscFunctionReturn(0);
6384201da799SBarry Smith }
6385201da799SBarry Smith 
6386201da799SBarry Smith #undef __FUNCT__
6387201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
6388201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6389201da799SBarry Smith {
6390201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6391201da799SBarry Smith   PetscReal      x   = ptime,y;
6392201da799SBarry Smith   PetscErrorCode ierr;
6393201da799SBarry Smith   PetscInt       its;
6394201da799SBarry Smith 
6395201da799SBarry Smith   PetscFunctionBegin;
6396201da799SBarry Smith   if (!n) {
6397201da799SBarry Smith     PetscDrawAxis axis;
6398bbd56ea5SKarl Rupp 
6399201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6400201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
6401201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6402bbd56ea5SKarl Rupp 
6403201da799SBarry Smith     ctx->ksp_its = 0;
6404201da799SBarry Smith   }
6405201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
6406201da799SBarry Smith   y    = its - ctx->ksp_its;
6407201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
640899fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6409201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
6410201da799SBarry Smith   }
6411201da799SBarry Smith   ctx->ksp_its = its;
6412201da799SBarry Smith   PetscFunctionReturn(0);
6413201da799SBarry Smith }
6414f9c1d6abSBarry Smith 
6415f9c1d6abSBarry Smith #undef __FUNCT__
6416f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
6417f9c1d6abSBarry Smith /*@
6418f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
6419f9c1d6abSBarry Smith 
6420f9c1d6abSBarry Smith    Collective on TS and Vec
6421f9c1d6abSBarry Smith 
6422f9c1d6abSBarry Smith    Input Parameters:
6423f9c1d6abSBarry Smith +  ts - the TS context
6424f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
6425f9c1d6abSBarry Smith 
6426f9c1d6abSBarry Smith    Output Parameters:
6427f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
6428f9c1d6abSBarry Smith 
6429f9c1d6abSBarry Smith    Level: developer
6430f9c1d6abSBarry Smith 
6431f9c1d6abSBarry Smith .keywords: TS, compute
6432f9c1d6abSBarry Smith 
6433f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
6434f9c1d6abSBarry Smith @*/
6435f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
6436f9c1d6abSBarry Smith {
6437f9c1d6abSBarry Smith   PetscErrorCode ierr;
6438f9c1d6abSBarry Smith 
6439f9c1d6abSBarry Smith   PetscFunctionBegin;
6440f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6441ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
6442f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
6443f9c1d6abSBarry Smith   PetscFunctionReturn(0);
6444f9c1d6abSBarry Smith }
644524655328SShri 
6446b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
6447b3d3934dSBarry Smith #undef __FUNCT__
6448b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate"
6449b3d3934dSBarry Smith /*@C
6450b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
6451b3d3934dSBarry Smith 
6452b3d3934dSBarry Smith    Collective on TS
6453b3d3934dSBarry Smith 
6454b3d3934dSBarry Smith    Input Parameters:
6455b3d3934dSBarry Smith .  ts  - the ODE solver object
6456b3d3934dSBarry Smith 
6457b3d3934dSBarry Smith    Output Parameter:
6458b3d3934dSBarry Smith .  ctx - the context
6459b3d3934dSBarry Smith 
6460b3d3934dSBarry Smith    Level: intermediate
6461b3d3934dSBarry Smith 
6462b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
6463b3d3934dSBarry Smith 
6464b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
6465b3d3934dSBarry Smith 
6466b3d3934dSBarry Smith @*/
6467b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
6468b3d3934dSBarry Smith {
6469b3d3934dSBarry Smith   PetscErrorCode ierr;
6470b3d3934dSBarry Smith 
6471b3d3934dSBarry Smith   PetscFunctionBegin;
6472a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
6473b3d3934dSBarry Smith   PetscFunctionReturn(0);
6474b3d3934dSBarry Smith }
6475b3d3934dSBarry Smith 
6476b3d3934dSBarry Smith #undef __FUNCT__
6477b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope"
6478b3d3934dSBarry Smith /*@C
6479b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
6480b3d3934dSBarry Smith 
6481b3d3934dSBarry Smith    Collective on TS
6482b3d3934dSBarry Smith 
6483b3d3934dSBarry Smith    Input Parameters:
6484b3d3934dSBarry Smith +  ts - the TS context
6485b3d3934dSBarry Smith .  step - current time-step
6486b3d3934dSBarry Smith .  ptime - current time
64877db568b7SBarry Smith .  u  - current solution
64887db568b7SBarry Smith -  dctx - the envelope context
6489b3d3934dSBarry Smith 
6490b3d3934dSBarry Smith    Options Database:
6491b3d3934dSBarry Smith .  -ts_monitor_envelope
6492b3d3934dSBarry Smith 
6493b3d3934dSBarry Smith    Level: intermediate
6494b3d3934dSBarry Smith 
6495b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
6496b3d3934dSBarry Smith 
6497b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6498b3d3934dSBarry Smith 
64997db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
6500b3d3934dSBarry Smith @*/
65017db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6502b3d3934dSBarry Smith {
6503b3d3934dSBarry Smith   PetscErrorCode       ierr;
65047db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
6505b3d3934dSBarry Smith 
6506b3d3934dSBarry Smith   PetscFunctionBegin;
6507b3d3934dSBarry Smith   if (!ctx->max) {
6508b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
6509b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
6510b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
6511b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
6512b3d3934dSBarry Smith   } else {
6513b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
6514b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
6515b3d3934dSBarry Smith   }
6516b3d3934dSBarry Smith   PetscFunctionReturn(0);
6517b3d3934dSBarry Smith }
6518b3d3934dSBarry Smith 
6519b3d3934dSBarry Smith 
6520b3d3934dSBarry Smith #undef __FUNCT__
6521b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds"
6522b3d3934dSBarry Smith /*@C
6523b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
6524b3d3934dSBarry Smith 
6525b3d3934dSBarry Smith    Collective on TS
6526b3d3934dSBarry Smith 
6527b3d3934dSBarry Smith    Input Parameter:
6528b3d3934dSBarry Smith .  ts - the TS context
6529b3d3934dSBarry Smith 
6530b3d3934dSBarry Smith    Output Parameter:
6531b3d3934dSBarry Smith +  max - the maximum values
6532b3d3934dSBarry Smith -  min - the minimum values
6533b3d3934dSBarry Smith 
65347db568b7SBarry Smith    Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
65357db568b7SBarry Smith 
6536b3d3934dSBarry Smith    Level: intermediate
6537b3d3934dSBarry Smith 
6538b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6539b3d3934dSBarry Smith 
6540b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6541b3d3934dSBarry Smith @*/
6542b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
6543b3d3934dSBarry Smith {
6544b3d3934dSBarry Smith   PetscInt i;
6545b3d3934dSBarry Smith 
6546b3d3934dSBarry Smith   PetscFunctionBegin;
6547b3d3934dSBarry Smith   if (max) *max = NULL;
6548b3d3934dSBarry Smith   if (min) *min = NULL;
6549b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6550b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
65515537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
6552b3d3934dSBarry Smith       if (max) *max = ctx->max;
6553b3d3934dSBarry Smith       if (min) *min = ctx->min;
6554b3d3934dSBarry Smith       break;
6555b3d3934dSBarry Smith     }
6556b3d3934dSBarry Smith   }
6557b3d3934dSBarry Smith   PetscFunctionReturn(0);
6558b3d3934dSBarry Smith }
6559b3d3934dSBarry Smith 
6560b3d3934dSBarry Smith #undef __FUNCT__
6561b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy"
6562b3d3934dSBarry Smith /*@C
6563b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
6564b3d3934dSBarry Smith 
6565b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
6566b3d3934dSBarry Smith 
6567b3d3934dSBarry Smith    Input Parameter:
6568b3d3934dSBarry Smith .  ctx - the monitor context
6569b3d3934dSBarry Smith 
6570b3d3934dSBarry Smith    Level: intermediate
6571b3d3934dSBarry Smith 
6572b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
6573b3d3934dSBarry Smith 
65747db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
6575b3d3934dSBarry Smith @*/
6576b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
6577b3d3934dSBarry Smith {
6578b3d3934dSBarry Smith   PetscErrorCode ierr;
6579b3d3934dSBarry Smith 
6580b3d3934dSBarry Smith   PetscFunctionBegin;
6581b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
6582b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
6583b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
6584b3d3934dSBarry Smith   PetscFunctionReturn(0);
6585b3d3934dSBarry Smith }
6586f2dee214SBarry Smith 
658724655328SShri #undef __FUNCT__
658824655328SShri #define __FUNCT__ "TSRollBack"
658924655328SShri /*@
659024655328SShri    TSRollBack - Rolls back one time step
659124655328SShri 
659224655328SShri    Collective on TS
659324655328SShri 
659424655328SShri    Input Parameter:
659524655328SShri .  ts - the TS context obtained from TSCreate()
659624655328SShri 
659724655328SShri    Level: advanced
659824655328SShri 
659924655328SShri .keywords: TS, timestep, rollback
660024655328SShri 
660124655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
660224655328SShri @*/
660324655328SShri PetscErrorCode  TSRollBack(TS ts)
660424655328SShri {
660524655328SShri   PetscErrorCode ierr;
660624655328SShri 
660724655328SShri   PetscFunctionBegin;
660824655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
660924655328SShri 
661024655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
661124655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
661224655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
661324655328SShri   ts->ptime = ts->ptime_prev;
66148392e04aSShri Abhyankar   ts->steprollback = PETSC_TRUE; /* Flag to indicate that the step is rollbacked */
661524655328SShri   PetscFunctionReturn(0);
661624655328SShri }
6617aeb4809dSShri Abhyankar 
6618ff22ae23SHong Zhang #undef __FUNCT__
6619ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
6620ff22ae23SHong Zhang /*@
6621ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
6622ff22ae23SHong Zhang 
6623ff22ae23SHong Zhang    Input Parameter:
6624ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
6625ff22ae23SHong Zhang 
6626ff22ae23SHong Zhang    Level: advanced
6627ff22ae23SHong Zhang 
6628ff22ae23SHong Zhang .keywords: TS, getstages
6629ff22ae23SHong Zhang 
6630ff22ae23SHong Zhang .seealso: TSCreate()
6631ff22ae23SHong Zhang @*/
6632ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns, Vec **Y)
6633ff22ae23SHong Zhang {
6634ff22ae23SHong Zhang   PetscErrorCode ierr;
6635ff22ae23SHong Zhang 
6636ff22ae23SHong Zhang   PetscFunctionBegin;
6637ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
6638ff22ae23SHong Zhang   PetscValidPointer(ns,2);
6639ff22ae23SHong Zhang 
6640ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
6641ff22ae23SHong Zhang   else {
6642ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
6643ff22ae23SHong Zhang   }
6644ff22ae23SHong Zhang   PetscFunctionReturn(0);
6645ff22ae23SHong Zhang }
6646ff22ae23SHong Zhang 
6647847ff0e1SMatthew G. Knepley #undef __FUNCT__
6648847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor"
6649847ff0e1SMatthew G. Knepley /*@C
6650847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
6651847ff0e1SMatthew G. Knepley 
6652847ff0e1SMatthew G. Knepley   Collective on SNES
6653847ff0e1SMatthew G. Knepley 
6654847ff0e1SMatthew G. Knepley   Input Parameters:
6655847ff0e1SMatthew G. Knepley + ts - the TS context
6656847ff0e1SMatthew G. Knepley . t - current timestep
6657847ff0e1SMatthew G. Knepley . U - state vector
6658847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
6659847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
6660847ff0e1SMatthew G. Knepley - ctx - an optional user context
6661847ff0e1SMatthew G. Knepley 
6662847ff0e1SMatthew G. Knepley   Output Parameters:
6663847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
6664847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
6665847ff0e1SMatthew G. Knepley 
6666847ff0e1SMatthew G. Knepley   Level: intermediate
6667847ff0e1SMatthew G. Knepley 
6668847ff0e1SMatthew G. Knepley   Notes:
6669847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
6670847ff0e1SMatthew G. Knepley 
6671847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
6672847ff0e1SMatthew G. Knepley 
6673847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
6674847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
6675847ff0e1SMatthew G. Knepley 
6676847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
6677847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
6678847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
6679847ff0e1SMatthew G. Knepley 
6680847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
6681847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
6682847ff0e1SMatthew G. Knepley @*/
6683847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
6684847ff0e1SMatthew G. Knepley {
6685847ff0e1SMatthew G. Knepley   SNES           snes;
6686847ff0e1SMatthew G. Knepley   MatFDColoring  color;
6687847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
6688847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
6689847ff0e1SMatthew G. Knepley 
6690847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
6691c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
6692847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
6693847ff0e1SMatthew G. Knepley   if (!color) {
6694847ff0e1SMatthew G. Knepley     DM         dm;
6695847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
6696847ff0e1SMatthew G. Knepley 
6697847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
6698847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
6699847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
6700847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
6701847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6702847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6703847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6704847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6705847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6706847ff0e1SMatthew G. Knepley     } else {
6707847ff0e1SMatthew G. Knepley       MatColoring mc;
6708847ff0e1SMatthew G. Knepley 
6709847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
6710847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
6711847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
6712847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
6713847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
6714847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
6715847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6716847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6717847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6718847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6719847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6720847ff0e1SMatthew G. Knepley     }
6721847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
6722847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
6723847ff0e1SMatthew G. Knepley   }
6724847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
6725847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
6726847ff0e1SMatthew G. Knepley   if (J != B) {
6727847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6728847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6729847ff0e1SMatthew G. Knepley   }
6730847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
6731847ff0e1SMatthew G. Knepley }
673293b34091SDebojyoti Ghosh 
673393b34091SDebojyoti Ghosh #undef __FUNCT__
6734cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSSetFunctionDomainError"
6735cb9d8021SPierre Barbier de Reuille /*@
6736cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
6737cb9d8021SPierre Barbier de Reuille 
6738cb9d8021SPierre Barbier de Reuille     Input Parameters:
6739cb9d8021SPierre Barbier de Reuille     ts - the TS context
6740cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
6741cb9d8021SPierre Barbier de Reuille 
6742cb9d8021SPierre Barbier de Reuille     Level: intermediate
6743cb9d8021SPierre Barbier de Reuille 
6744cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
6745cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
6746cb9d8021SPierre Barbier de Reuille @*/
6747cb9d8021SPierre Barbier de Reuille 
6748d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
6749cb9d8021SPierre Barbier de Reuille {
6750cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
6751cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
6752cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
6753cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
6754cb9d8021SPierre Barbier de Reuille }
6755cb9d8021SPierre Barbier de Reuille 
6756cb9d8021SPierre Barbier de Reuille #undef __FUNCT__
6757cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSFunctionDomainError"
6758cb9d8021SPierre Barbier de Reuille /*@
6759cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
6760cb9d8021SPierre Barbier de Reuille 
6761cb9d8021SPierre Barbier de Reuille     Input Parameters:
6762cb9d8021SPierre Barbier de Reuille     ts - the TS context
6763cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
6764d183316bSPierre Barbier de Reuille     Y - state vector to check.
6765cb9d8021SPierre Barbier de Reuille 
6766cb9d8021SPierre Barbier de Reuille     Output Parameter:
6767cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
6768cb9d8021SPierre Barbier de Reuille 
6769cb9d8021SPierre Barbier de Reuille     Note:
6770cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
6771cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
677296a0c994SBarry Smith 
677396a0c994SBarry Smith     Level: advanced
6774cb9d8021SPierre Barbier de Reuille @*/
6775d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
6776cb9d8021SPierre Barbier de Reuille {
6777cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
6778cb9d8021SPierre Barbier de Reuille 
6779cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
6780cb9d8021SPierre Barbier de Reuille 
6781cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6782cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
6783cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
6784d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
6785cb9d8021SPierre Barbier de Reuille   }
6786cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
6787cb9d8021SPierre Barbier de Reuille }
67881ceb14c0SBarry Smith 
67891ceb14c0SBarry Smith #undef  __FUNCT__
6790e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone"
679193b34091SDebojyoti Ghosh /*@C
6792e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
679393b34091SDebojyoti Ghosh 
679493b34091SDebojyoti Ghosh   Collective on MPI_Comm
679593b34091SDebojyoti Ghosh 
679693b34091SDebojyoti Ghosh   Input Parameter:
679793b34091SDebojyoti Ghosh . tsin    - The input TS
679893b34091SDebojyoti Ghosh 
679993b34091SDebojyoti Ghosh   Output Parameter:
6800e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
680193b34091SDebojyoti Ghosh 
68025eca1a21SEmil Constantinescu   Notes:
68035eca1a21SEmil 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.
68045eca1a21SEmil Constantinescu 
6805baa10174SEmil 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);
68065eca1a21SEmil Constantinescu 
68075eca1a21SEmil Constantinescu   Level: developer
680893b34091SDebojyoti Ghosh 
6809e5168f73SEmil Constantinescu .keywords: TS, clone
6810e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
681193b34091SDebojyoti Ghosh @*/
6812baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
681393b34091SDebojyoti Ghosh {
681493b34091SDebojyoti Ghosh   TS             t;
681593b34091SDebojyoti Ghosh   PetscErrorCode ierr;
6816dc846ba4SSatish Balay   SNES           snes_start;
6817dc846ba4SSatish Balay   DM             dm;
6818dc846ba4SSatish Balay   TSType         type;
681993b34091SDebojyoti Ghosh 
682093b34091SDebojyoti Ghosh   PetscFunctionBegin;
682193b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
682293b34091SDebojyoti Ghosh   *tsout = NULL;
682393b34091SDebojyoti Ghosh 
68247a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
682593b34091SDebojyoti Ghosh 
682693b34091SDebojyoti Ghosh   /* General TS description */
682793b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
682893b34091SDebojyoti Ghosh   t->setupcalled       = 0;
682993b34091SDebojyoti Ghosh   t->ksp_its           = 0;
683093b34091SDebojyoti Ghosh   t->snes_its          = 0;
683193b34091SDebojyoti Ghosh   t->nwork             = 0;
683293b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
683393b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
683493b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
683593b34091SDebojyoti Ghosh 
683634561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);                   CHKERRQ(ierr);
683734561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);                       CHKERRQ(ierr);
6838d15a3a53SEmil Constantinescu 
683993b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);                             CHKERRQ(ierr);
684093b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);                                 CHKERRQ(ierr);
684193b34091SDebojyoti Ghosh 
684293b34091SDebojyoti Ghosh   t->adapt=tsin->adapt;
684393b34091SDebojyoti Ghosh   PetscObjectReference((PetscObject)t->adapt);
684493b34091SDebojyoti Ghosh 
684593b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
684693b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
684793b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
684893b34091SDebojyoti Ghosh   t->time_step_orig    = tsin->time_step_orig;
684993b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
685093b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
685193b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
685293b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
685393b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
685493b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
685593b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
685693b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
685793b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
685893b34091SDebojyoti Ghosh 
685993b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type); CHKERRQ(ierr);
686093b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);     CHKERRQ(ierr);
686193b34091SDebojyoti Ghosh 
686293b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
686393b34091SDebojyoti Ghosh 
686493b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
686593b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
686693b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
686793b34091SDebojyoti Ghosh 
686893b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
686993b34091SDebojyoti Ghosh 
68700d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
68710d4fed19SBarry Smith     PetscInt i;
68720d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
68730d4fed19SBarry Smith     for (i=0; i<10; i++) {
68740d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
68750d4fed19SBarry Smith     }
68760d4fed19SBarry Smith   }
687793b34091SDebojyoti Ghosh   *tsout = t;
687893b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
687993b34091SDebojyoti Ghosh }
6880