xref: /petsc/src/ts/interface/ts.c (revision 41a1d4d2207bbd45cde03cb0ab519a048c5be39d)
163dd3a1aSKris Buschelman 
2af0996ceSBarry Smith #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
41e25c274SJed Brown #include <petscdmda.h>
52d5ee99bSBarry Smith #include <petscviewer.h>
62d5ee99bSBarry Smith #include <petscdraw.h>
7d763cef2SBarry Smith 
8d5ba7fb7SMatthew Knepley /* Logging support */
9d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
108d0ad7a8SHong Zhang PetscLogEvent TS_AdjointStep, TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
11d405a339SMatthew Knepley 
12feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1349354f04SShri Abhyankar 
142d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx {
152d5ee99bSBarry Smith   PetscViewer   viewer;
162d5ee99bSBarry Smith   Vec           initialsolution;
172d5ee99bSBarry Smith   PetscBool     showinitial;
182d5ee99bSBarry Smith   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
192d5ee99bSBarry Smith   PetscBool     showtimestepandtime;
202d5ee99bSBarry Smith };
212d5ee99bSBarry Smith 
22fde5950dSBarry Smith /*@C
23fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
24fde5950dSBarry Smith 
25fde5950dSBarry Smith    Collective on TS
26fde5950dSBarry Smith 
27fde5950dSBarry Smith    Input Parameters:
28fde5950dSBarry Smith +  ts - TS object you wish to monitor
29fde5950dSBarry Smith .  name - the monitor type one is seeking
30fde5950dSBarry Smith .  help - message indicating what monitoring is done
31fde5950dSBarry Smith .  manual - manual page for the monitor
32fde5950dSBarry Smith .  monitor - the monitor function
33fde5950dSBarry Smith -  monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects
34fde5950dSBarry Smith 
35fde5950dSBarry Smith    Level: developer
36fde5950dSBarry Smith 
37fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
38fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
39fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
40fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
41fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
42fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
43fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
44fde5950dSBarry Smith @*/
45721cd6eeSBarry Smith PetscErrorCode  TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
46fde5950dSBarry Smith {
47fde5950dSBarry Smith   PetscErrorCode    ierr;
48fde5950dSBarry Smith   PetscViewer       viewer;
49fde5950dSBarry Smith   PetscViewerFormat format;
50fde5950dSBarry Smith   PetscBool         flg;
51fde5950dSBarry Smith 
52fde5950dSBarry Smith   PetscFunctionBegin;
53fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
54fde5950dSBarry Smith   if (flg) {
55721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
56721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
57721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
58fde5950dSBarry Smith     if (monitorsetup) {
59721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
60fde5950dSBarry Smith     }
61721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
62fde5950dSBarry Smith   }
63fde5950dSBarry Smith   PetscFunctionReturn(0);
64fde5950dSBarry Smith }
65fde5950dSBarry Smith 
66fde5950dSBarry Smith /*@C
67fde5950dSBarry Smith    TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
68fde5950dSBarry Smith 
69fde5950dSBarry Smith    Collective on TS
70fde5950dSBarry Smith 
71fde5950dSBarry Smith    Input Parameters:
72fde5950dSBarry Smith +  ts - TS object you wish to monitor
73fde5950dSBarry Smith .  name - the monitor type one is seeking
74fde5950dSBarry Smith .  help - message indicating what monitoring is done
75fde5950dSBarry Smith .  manual - manual page for the monitor
76fde5950dSBarry Smith .  monitor - the monitor function
77fde5950dSBarry 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
78fde5950dSBarry Smith 
79fde5950dSBarry Smith    Level: developer
80fde5950dSBarry Smith 
81fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
82fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
83fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
84fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
85fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
86fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
87fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
88fde5950dSBarry Smith @*/
89721cd6eeSBarry Smith PetscErrorCode  TSAdjointMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
90fde5950dSBarry Smith {
91fde5950dSBarry Smith   PetscErrorCode    ierr;
92fde5950dSBarry Smith   PetscViewer       viewer;
93fde5950dSBarry Smith   PetscViewerFormat format;
94fde5950dSBarry Smith   PetscBool         flg;
95fde5950dSBarry Smith 
96fde5950dSBarry Smith   PetscFunctionBegin;
97fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
98fde5950dSBarry Smith   if (flg) {
99721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
100721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
101721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
102fde5950dSBarry Smith     if (monitorsetup) {
103721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
104fde5950dSBarry Smith     }
105721cd6eeSBarry Smith     ierr = TSAdjointMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
106fde5950dSBarry Smith   }
107fde5950dSBarry Smith   PetscFunctionReturn(0);
108fde5950dSBarry Smith }
109fde5950dSBarry Smith 
110bdad233fSMatthew Knepley /*@
111bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
112bdad233fSMatthew Knepley 
113bdad233fSMatthew Knepley    Collective on TS
114bdad233fSMatthew Knepley 
115bdad233fSMatthew Knepley    Input Parameter:
116bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
117bdad233fSMatthew Knepley 
118bdad233fSMatthew Knepley    Options Database Keys:
119b6a60446SDebojyoti Ghosh +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE
120ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
1213e4cdcaaSBarry Smith .  -ts_max_steps <maxsteps> - maximum number of time-steps to take
1223e4cdcaaSBarry Smith .  -ts_final_time <time> - maximum time to compute to
1233e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
124a3cdaa26SBarry 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
125a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
126a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
127a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
128a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
129a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
130ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
131847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
132bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
133de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
134de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1356934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
136de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
137de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
138de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
139de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1403e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1413e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
142fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
143e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
144110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
145110eb670SHong Zhang .  -ts_adjoint_monitor - print information at each adjoint time step
14653ea634cSHong Zhang -  -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically
14753ea634cSHong Zhang 
14891b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
149bdad233fSMatthew Knepley 
150bdad233fSMatthew Knepley    Level: beginner
151bdad233fSMatthew Knepley 
152bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
153bdad233fSMatthew Knepley 
154a313700dSBarry Smith .seealso: TSGetType()
155bdad233fSMatthew Knepley @*/
1567087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
157bdad233fSMatthew Knepley {
158bc952696SBarry Smith   PetscBool              opt,flg,tflg;
159dfbe8321SBarry Smith   PetscErrorCode         ierr;
160eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
16131748224SBarry Smith   PetscReal              time_step;
16249354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
163d1212d36SBarry Smith   char                   dir[16];
164cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1656991f827SBarry Smith   const char             *defaultType;
1666991f827SBarry Smith   char                   typeName[256];
167bdad233fSMatthew Knepley 
168bdad233fSMatthew Knepley   PetscFunctionBegin;
1690700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1706991f827SBarry Smith 
1716991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
172cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
173cd11d68dSLisandro Dalcin 
174cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
175cd11d68dSLisandro Dalcin   if (((PetscObject)ts)->type_name)
176cd11d68dSLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
177cd11d68dSLisandro Dalcin   else
178cd11d68dSLisandro Dalcin     defaultType = ifun ? TSBEULER : TSEULER;
1796991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1806991f827SBarry Smith   if (opt) {
1816991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1826991f827SBarry Smith   } else {
1836991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1846991f827SBarry Smith   }
185bdad233fSMatthew Knepley 
186bdad233fSMatthew Knepley   /* Handle generic TS options */
1870298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1880298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1890298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
19031748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
191cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
19249354f04SShri 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);
19349354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1940298fd71SBarry 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);
1950298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1960298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1970298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1980298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
199bdad233fSMatthew Knepley 
20056f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
20156f85f32SBarry Smith   {
20256f85f32SBarry Smith   PetscBool set;
20356f85f32SBarry Smith   flg  = PETSC_FALSE;
20456f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
20556f85f32SBarry Smith   if (set) {
20656f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
20756f85f32SBarry Smith   }
20856f85f32SBarry Smith   }
20956f85f32SBarry Smith #endif
21056f85f32SBarry Smith 
211bdad233fSMatthew Knepley   /* Monitor options */
212cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
213fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
214fde5950dSBarry Smith   ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr);
215fde5950dSBarry Smith 
2165180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2175180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
2185180491cSLisandro Dalcin 
2194f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
220b3603a34SBarry Smith   if (opt) {
2210b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2223923b477SBarry Smith     PetscInt       howoften = 1;
223b3603a34SBarry Smith 
2240298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2256ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2264f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
227bdad233fSMatthew Knepley   }
2286ba87a44SLisandro Dalcin 
2294f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
230ef20d060SBarry Smith   if (opt) {
2310b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2323923b477SBarry Smith     PetscInt       howoften = 1;
233ef20d060SBarry Smith 
2340298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
2356ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2364f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
237ef20d060SBarry Smith   }
2386934998bSLisandro Dalcin 
2396934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2406934998bSLisandro Dalcin   if (opt) {
2416934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2426934998bSLisandro Dalcin     PetscInt       howoften = 1;
2436934998bSLisandro Dalcin 
2446934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2456ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2466934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2476934998bSLisandro Dalcin   }
248201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
249201da799SBarry Smith   if (opt) {
250201da799SBarry Smith     TSMonitorLGCtx ctx;
251201da799SBarry Smith     PetscInt       howoften = 1;
252201da799SBarry Smith 
2530298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2546ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
255201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
256201da799SBarry Smith   }
257201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
258201da799SBarry Smith   if (opt) {
259201da799SBarry Smith     TSMonitorLGCtx ctx;
260201da799SBarry Smith     PetscInt       howoften = 1;
261201da799SBarry Smith 
2620298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2636ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
264201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
265201da799SBarry Smith   }
2668189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2678189c53fSBarry Smith   if (opt) {
2688189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2698189c53fSBarry Smith     PetscInt          howoften = 1;
2708189c53fSBarry Smith 
2710298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
2726934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2738189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2748189c53fSBarry Smith   }
275ef20d060SBarry Smith   opt  = PETSC_FALSE;
2760dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
277a7cc72afSBarry Smith   if (opt) {
27883a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
27983a4ac43SBarry Smith     PetscInt         howoften = 1;
280a80ad3e0SBarry Smith 
2810298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2826934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
28383a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
284bdad233fSMatthew Knepley   }
285fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2869110b2e7SHong Zhang   ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr);
2879110b2e7SHong Zhang   if (opt) {
2889110b2e7SHong Zhang     TSMonitorDrawCtx ctx;
2899110b2e7SHong Zhang     PetscInt         howoften = 1;
2909110b2e7SHong Zhang 
2919110b2e7SHong Zhang     ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr);
2926934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2939110b2e7SHong Zhang     ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2949110b2e7SHong Zhang   }
2959110b2e7SHong Zhang   opt  = PETSC_FALSE;
2962d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2972d5ee99bSBarry Smith   if (opt) {
2982d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2992d5ee99bSBarry Smith     PetscReal        bounds[4];
3002d5ee99bSBarry Smith     PetscInt         n = 4;
3012d5ee99bSBarry Smith     PetscDraw        draw;
3026934998bSLisandro Dalcin     PetscDrawAxis    axis;
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");
3066934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
3072d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
3086934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
3096934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
3106934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
3112d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3122d5ee99bSBarry Smith   }
3132d5ee99bSBarry Smith   opt  = PETSC_FALSE;
3140dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
3153a471f94SBarry Smith   if (opt) {
31683a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
31783a4ac43SBarry Smith     PetscInt         howoften = 1;
3183a471f94SBarry Smith 
3190298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
3206934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
32183a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3223a471f94SBarry Smith   }
323fde5950dSBarry Smith 
324ed81e22dSJed Brown   opt  = PETSC_FALSE;
32591b97e58SBarry 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);
326ed81e22dSJed Brown   if (flg) {
327ed81e22dSJed Brown     const char *ptr,*ptr2;
328ed81e22dSJed Brown     char       *filetemplate;
329ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
330ed81e22dSJed Brown     /* Do some cursory validation of the input. */
331ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
332ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
333ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
334ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
335ce94432eSBarry 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");
336ed81e22dSJed Brown       if (ptr2) break;
337ed81e22dSJed Brown     }
338ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
339ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
340ed81e22dSJed Brown   }
341bdad233fSMatthew Knepley 
342d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
343d1212d36SBarry Smith   if (flg) {
344d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
345d1212d36SBarry Smith     int                  ray = 0;
346d1212d36SBarry Smith     DMDADirection        ddir;
347d1212d36SBarry Smith     DM                   da;
348d1212d36SBarry Smith     PetscMPIInt          rank;
349d1212d36SBarry Smith 
350ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
351d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
352d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
353ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
354d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
355d1212d36SBarry Smith 
356d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
357b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
358d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
359d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
360ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
361d1212d36SBarry Smith     if (!rank) {
362d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
363d1212d36SBarry Smith     }
36451b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
365d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
366d1212d36SBarry Smith   }
36751b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
36851b4a12fSMatthew G. Knepley   if (flg) {
36951b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
37051b4a12fSMatthew G. Knepley     int                 ray = 0;
37151b4a12fSMatthew G. Knepley     DMDADirection       ddir;
37251b4a12fSMatthew G. Knepley     DM                  da;
37351b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
374d1212d36SBarry Smith 
37551b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
37651b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
37751b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
37851b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
37951b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3801c3436cfSJed Brown 
38151b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
382b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
38351b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
38451b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
38551b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
38651b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
38751b4a12fSMatthew G. Knepley   }
388a7a1495cSBarry Smith 
389b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
390b3d3934dSBarry Smith   if (opt) {
391b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
392b3d3934dSBarry Smith 
393b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
394b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
395b3d3934dSBarry Smith   }
396b3d3934dSBarry Smith 
397847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
398847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
399847ff0e1SMatthew G. Knepley   if (flg) {
400847ff0e1SMatthew G. Knepley     DM   dm;
401847ff0e1SMatthew G. Knepley     DMTS tdm;
402847ff0e1SMatthew G. Knepley 
403847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
404847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
405847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
406847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
407847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
408847ff0e1SMatthew G. Knepley   }
409847ff0e1SMatthew G. Knepley 
410ee346746SBarry Smith   if (ts->adapt) {
411ee346746SBarry Smith     ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
412ee346746SBarry Smith   }
413d763cef2SBarry Smith 
414d763cef2SBarry Smith   /* Handle specific TS options */
415d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4166991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
417d763cef2SBarry Smith   }
418fbc52257SHong Zhang 
41968bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4204f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
42168bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
42268bece0bSHong Zhang   if (tflg) {
42368bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
42468bece0bSHong Zhang   }
4254f122a70SLisandro Dalcin   tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE;
42668bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr);
42768bece0bSHong Zhang   if (flg) {
42868bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
42968bece0bSHong Zhang     ts->adjoint_solve = tflg;
43068bece0bSHong Zhang   }
431d763cef2SBarry Smith 
432d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4330633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
434fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
435d763cef2SBarry Smith 
4364f122a70SLisandro Dalcin   if (ts->trajectory) {
4374f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
438d763cef2SBarry Smith   }
43968bece0bSHong Zhang 
4404f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4414f122a70SLisandro Dalcin   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);}
4424f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
443d763cef2SBarry Smith   PetscFunctionReturn(0);
444d763cef2SBarry Smith }
445d763cef2SBarry Smith 
446d2daff3dSHong Zhang /*@
447bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
448d2daff3dSHong Zhang 
449d2daff3dSHong Zhang    Collective on TS
450d2daff3dSHong Zhang 
451d2daff3dSHong Zhang    Input Parameters:
452bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
453bc952696SBarry Smith 
45468bece0bSHong Zhang Note: This routine should be called after all TS options have been set
455d2daff3dSHong Zhang 
456d2daff3dSHong Zhang    Level: intermediate
457d2daff3dSHong Zhang 
458bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve()
459d2daff3dSHong Zhang 
460bc952696SBarry Smith .keywords: TS, set, checkpoint,
461d2daff3dSHong Zhang @*/
462bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
463d2daff3dSHong Zhang {
464bc952696SBarry Smith   PetscErrorCode ierr;
465bc952696SBarry Smith 
466d2daff3dSHong Zhang   PetscFunctionBegin;
467d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
468bc952696SBarry Smith   if (!ts->trajectory) {
469bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
470972caf09SHong Zhang     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
471bc952696SBarry Smith   }
472d2daff3dSHong Zhang   PetscFunctionReturn(0);
473d2daff3dSHong Zhang }
474d2daff3dSHong Zhang 
475a7a1495cSBarry Smith /*@
476a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
477a7a1495cSBarry Smith       set with TSSetRHSJacobian().
478a7a1495cSBarry Smith 
479a7a1495cSBarry Smith    Collective on TS and Vec
480a7a1495cSBarry Smith 
481a7a1495cSBarry Smith    Input Parameters:
482316643e7SJed Brown +  ts - the TS context
483a7a1495cSBarry Smith .  t - current timestep
4840910c330SBarry Smith -  U - input vector
485a7a1495cSBarry Smith 
486a7a1495cSBarry Smith    Output Parameters:
487a7a1495cSBarry Smith +  A - Jacobian matrix
488a7a1495cSBarry Smith .  B - optional preconditioning matrix
489a7a1495cSBarry Smith -  flag - flag indicating matrix structure
490a7a1495cSBarry Smith 
491a7a1495cSBarry Smith    Notes:
492a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
493a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
494a7a1495cSBarry Smith 
49594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
496a7a1495cSBarry Smith    flag parameter.
497a7a1495cSBarry Smith 
498a7a1495cSBarry Smith    Level: developer
499a7a1495cSBarry Smith 
500a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
501a7a1495cSBarry Smith 
50294b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
503a7a1495cSBarry Smith @*/
504d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
505a7a1495cSBarry Smith {
506dfbe8321SBarry Smith   PetscErrorCode ierr;
507270bf2e7SJed Brown   PetscObjectState Ustate;
50824989b8cSPeter Brune   DM             dm;
509942e3340SBarry Smith   DMTS           tsdm;
51024989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
51124989b8cSPeter Brune   void           *ctx;
51224989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
513b2df71adSDebojyoti Ghosh   TSRHSFunction  rhsfunction;
514a7a1495cSBarry Smith 
515a7a1495cSBarry Smith   PetscFunctionBegin;
5160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5170910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5180910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
51924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
520942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
52124989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5220298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
523b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
52459e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
525b2df71adSDebojyoti Ghosh   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
5260e4ef248SJed Brown     PetscFunctionReturn(0);
527f8ede8e7SJed Brown   }
528d90be118SSean Farley 
529ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
530d90be118SSean Farley 
531e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
53294ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
53394ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
53494ab13aaSBarry Smith     if (A != B) {
53594ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
53694ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
537e1244c69SJed Brown     }
538e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
539e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
540e1244c69SJed Brown   }
541e1244c69SJed Brown 
54224989b8cSPeter Brune   if (rhsjacobianfunc) {
5436cd88445SBarry Smith     PetscBool missing;
54494ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
545a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
546d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
547a7a1495cSBarry Smith     PetscStackPop;
54894ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
5496cd88445SBarry Smith     if (A) {
5506cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
5516cd88445SBarry 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");
5526cd88445SBarry Smith     }
5536cd88445SBarry Smith     if (B && B != A) {
5546cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
5556cd88445SBarry 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");
5566cd88445SBarry Smith     }
557ef66eb69SBarry Smith   } else {
55894ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
55994ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
560ef66eb69SBarry Smith   }
5610e4ef248SJed Brown   ts->rhsjacobian.time       = t;
5620910c330SBarry Smith   ts->rhsjacobian.X          = U;
56359e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
564a7a1495cSBarry Smith   PetscFunctionReturn(0);
565a7a1495cSBarry Smith }
566a7a1495cSBarry Smith 
567316643e7SJed Brown /*@
568d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
569d763cef2SBarry Smith 
570316643e7SJed Brown    Collective on TS and Vec
571316643e7SJed Brown 
572316643e7SJed Brown    Input Parameters:
573316643e7SJed Brown +  ts - the TS context
574316643e7SJed Brown .  t - current time
5750910c330SBarry Smith -  U - state vector
576316643e7SJed Brown 
577316643e7SJed Brown    Output Parameter:
578316643e7SJed Brown .  y - right hand side
579316643e7SJed Brown 
580316643e7SJed Brown    Note:
581316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
582316643e7SJed Brown    is used internally within the nonlinear solvers.
583316643e7SJed Brown 
584316643e7SJed Brown    Level: developer
585316643e7SJed Brown 
586316643e7SJed Brown .keywords: TS, compute
587316643e7SJed Brown 
588316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
589316643e7SJed Brown @*/
5900910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
591d763cef2SBarry Smith {
592dfbe8321SBarry Smith   PetscErrorCode ierr;
59324989b8cSPeter Brune   TSRHSFunction  rhsfunction;
59424989b8cSPeter Brune   TSIFunction    ifunction;
59524989b8cSPeter Brune   void           *ctx;
59624989b8cSPeter Brune   DM             dm;
59724989b8cSPeter Brune 
598d763cef2SBarry Smith   PetscFunctionBegin;
5990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6000910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6010700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
60224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
60324989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6040298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
605d763cef2SBarry Smith 
606ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
607d763cef2SBarry Smith 
6080910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
60924989b8cSPeter Brune   if (rhsfunction) {
610d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6110910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
612d763cef2SBarry Smith     PetscStackPop;
613214bc6a2SJed Brown   } else {
614214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
615b2cd27e8SJed Brown   }
61644a41b28SSean Farley 
6170910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
618d763cef2SBarry Smith   PetscFunctionReturn(0);
619d763cef2SBarry Smith }
620d763cef2SBarry Smith 
621ef20d060SBarry Smith /*@
622ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
623ef20d060SBarry Smith 
624ef20d060SBarry Smith    Collective on TS and Vec
625ef20d060SBarry Smith 
626ef20d060SBarry Smith    Input Parameters:
627ef20d060SBarry Smith +  ts - the TS context
628ef20d060SBarry Smith -  t - current time
629ef20d060SBarry Smith 
630ef20d060SBarry Smith    Output Parameter:
6310910c330SBarry Smith .  U - the solution
632ef20d060SBarry Smith 
633ef20d060SBarry Smith    Note:
634ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
635ef20d060SBarry Smith    is used internally within the nonlinear solvers.
636ef20d060SBarry Smith 
637ef20d060SBarry Smith    Level: developer
638ef20d060SBarry Smith 
639ef20d060SBarry Smith .keywords: TS, compute
640ef20d060SBarry Smith 
641abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
642ef20d060SBarry Smith @*/
6430910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
644ef20d060SBarry Smith {
645ef20d060SBarry Smith   PetscErrorCode     ierr;
646ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
647ef20d060SBarry Smith   void               *ctx;
648ef20d060SBarry Smith   DM                 dm;
649ef20d060SBarry Smith 
650ef20d060SBarry Smith   PetscFunctionBegin;
651ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6520910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
653ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
654ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
655ef20d060SBarry Smith 
656ef20d060SBarry Smith   if (solutionfunction) {
6579b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6580910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
659ef20d060SBarry Smith     PetscStackPop;
660ef20d060SBarry Smith   }
661ef20d060SBarry Smith   PetscFunctionReturn(0);
662ef20d060SBarry Smith }
6639b7cd975SBarry Smith /*@
6649b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6659b7cd975SBarry Smith 
6669b7cd975SBarry Smith    Collective on TS and Vec
6679b7cd975SBarry Smith 
6689b7cd975SBarry Smith    Input Parameters:
6699b7cd975SBarry Smith +  ts - the TS context
6709b7cd975SBarry Smith -  t - current time
6719b7cd975SBarry Smith 
6729b7cd975SBarry Smith    Output Parameter:
6739b7cd975SBarry Smith .  U - the function value
6749b7cd975SBarry Smith 
6759b7cd975SBarry Smith    Note:
6769b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
6779b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6789b7cd975SBarry Smith 
6799b7cd975SBarry Smith    Level: developer
6809b7cd975SBarry Smith 
6819b7cd975SBarry Smith .keywords: TS, compute
6829b7cd975SBarry Smith 
6839b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
6849b7cd975SBarry Smith @*/
6859b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
6869b7cd975SBarry Smith {
6879b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
6889b7cd975SBarry Smith   void               *ctx;
6899b7cd975SBarry Smith   DM                 dm;
6909b7cd975SBarry Smith 
6919b7cd975SBarry Smith   PetscFunctionBegin;
6929b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6939b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6949b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
6959b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
6969b7cd975SBarry Smith 
6979b7cd975SBarry Smith   if (forcing) {
6989b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
6999b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7009b7cd975SBarry Smith     PetscStackPop;
7019b7cd975SBarry Smith   }
7029b7cd975SBarry Smith   PetscFunctionReturn(0);
7039b7cd975SBarry Smith }
704ef20d060SBarry Smith 
705214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
706214bc6a2SJed Brown {
7072dd45cf8SJed Brown   Vec            F;
708214bc6a2SJed Brown   PetscErrorCode ierr;
709214bc6a2SJed Brown 
710214bc6a2SJed Brown   PetscFunctionBegin;
7110298fd71SBarry Smith   *Frhs = NULL;
7120298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
713214bc6a2SJed Brown   if (!ts->Frhs) {
7142dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
715214bc6a2SJed Brown   }
716214bc6a2SJed Brown   *Frhs = ts->Frhs;
717214bc6a2SJed Brown   PetscFunctionReturn(0);
718214bc6a2SJed Brown }
719214bc6a2SJed Brown 
720214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
721214bc6a2SJed Brown {
722214bc6a2SJed Brown   Mat            A,B;
7232dd45cf8SJed Brown   PetscErrorCode ierr;
724*41a1d4d2SBarry Smith   TSIJacobian    ijacobian;
725214bc6a2SJed Brown 
726214bc6a2SJed Brown   PetscFunctionBegin;
727c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
728c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
729*41a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
730214bc6a2SJed Brown   if (Arhs) {
731214bc6a2SJed Brown     if (!ts->Arhs) {
732*41a1d4d2SBarry Smith       if (ijacobian) {
733214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
734*41a1d4d2SBarry Smith       } else {
735*41a1d4d2SBarry Smith         ts->Arhs = A;
736*41a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
737*41a1d4d2SBarry Smith       }
738214bc6a2SJed Brown     }
739214bc6a2SJed Brown     *Arhs = ts->Arhs;
740214bc6a2SJed Brown   }
741214bc6a2SJed Brown   if (Brhs) {
742214bc6a2SJed Brown     if (!ts->Brhs) {
743bdb70873SJed Brown       if (A != B) {
744*41a1d4d2SBarry Smith         if (ijacobian) {
745214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
746bdb70873SJed Brown         } else {
747*41a1d4d2SBarry Smith           ts->Brhs = B;
748*41a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
749*41a1d4d2SBarry Smith         }
750*41a1d4d2SBarry Smith       } else {
751bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
75251699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
753bdb70873SJed Brown       }
754214bc6a2SJed Brown     }
755214bc6a2SJed Brown     *Brhs = ts->Brhs;
756214bc6a2SJed Brown   }
757214bc6a2SJed Brown   PetscFunctionReturn(0);
758214bc6a2SJed Brown }
759214bc6a2SJed Brown 
760316643e7SJed Brown /*@
7610910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
762316643e7SJed Brown 
763316643e7SJed Brown    Collective on TS and Vec
764316643e7SJed Brown 
765316643e7SJed Brown    Input Parameters:
766316643e7SJed Brown +  ts - the TS context
767316643e7SJed Brown .  t - current time
7680910c330SBarry Smith .  U - state vector
7690910c330SBarry Smith .  Udot - time derivative of state vector
770214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
771316643e7SJed Brown 
772316643e7SJed Brown    Output Parameter:
773316643e7SJed Brown .  Y - right hand side
774316643e7SJed Brown 
775316643e7SJed Brown    Note:
776316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
777316643e7SJed Brown    is used internally within the nonlinear solvers.
778316643e7SJed Brown 
779316643e7SJed Brown    If the user did did not write their equations in implicit form, this
780316643e7SJed Brown    function recasts them in implicit form.
781316643e7SJed Brown 
782316643e7SJed Brown    Level: developer
783316643e7SJed Brown 
784316643e7SJed Brown .keywords: TS, compute
785316643e7SJed Brown 
786316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
787316643e7SJed Brown @*/
7880910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
789316643e7SJed Brown {
790316643e7SJed Brown   PetscErrorCode ierr;
79124989b8cSPeter Brune   TSIFunction    ifunction;
79224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
79324989b8cSPeter Brune   void           *ctx;
79424989b8cSPeter Brune   DM             dm;
795316643e7SJed Brown 
796316643e7SJed Brown   PetscFunctionBegin;
7970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7980910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7990910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8000700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
801316643e7SJed Brown 
80224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
80324989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8040298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
80524989b8cSPeter Brune 
806ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
807d90be118SSean Farley 
8080910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
80924989b8cSPeter Brune   if (ifunction) {
810316643e7SJed Brown     PetscStackPush("TS user implicit function");
8110910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
812316643e7SJed Brown     PetscStackPop;
813214bc6a2SJed Brown   }
814214bc6a2SJed Brown   if (imex) {
81524989b8cSPeter Brune     if (!ifunction) {
8160910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8172dd45cf8SJed Brown     }
81824989b8cSPeter Brune   } else if (rhsfunction) {
81924989b8cSPeter Brune     if (ifunction) {
820214bc6a2SJed Brown       Vec Frhs;
821214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8220910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
823214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8242dd45cf8SJed Brown     } else {
8250910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8260910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
827316643e7SJed Brown     }
8284a6899ffSJed Brown   }
8290910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
830316643e7SJed Brown   PetscFunctionReturn(0);
831316643e7SJed Brown }
832316643e7SJed Brown 
833316643e7SJed Brown /*@
834316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
835316643e7SJed Brown 
836316643e7SJed Brown    Collective on TS and Vec
837316643e7SJed Brown 
838316643e7SJed Brown    Input
839316643e7SJed Brown       Input Parameters:
840316643e7SJed Brown +  ts - the TS context
841316643e7SJed Brown .  t - current timestep
8420910c330SBarry Smith .  U - state vector
8430910c330SBarry Smith .  Udot - time derivative of state vector
844214bc6a2SJed Brown .  shift - shift to apply, see note below
845214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
846316643e7SJed Brown 
847316643e7SJed Brown    Output Parameters:
848316643e7SJed Brown +  A - Jacobian matrix
849316643e7SJed Brown .  B - optional preconditioning matrix
850316643e7SJed Brown -  flag - flag indicating matrix structure
851316643e7SJed Brown 
852316643e7SJed Brown    Notes:
8530910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
854316643e7SJed Brown 
8550910c330SBarry Smith    dF/dU + shift*dF/dUdot
856316643e7SJed Brown 
857316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
858316643e7SJed Brown    is used internally within the nonlinear solvers.
859316643e7SJed Brown 
860316643e7SJed Brown    Level: developer
861316643e7SJed Brown 
862316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
863316643e7SJed Brown 
864316643e7SJed Brown .seealso:  TSSetIJacobian()
86563495f91SJed Brown @*/
866d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
867316643e7SJed Brown {
868316643e7SJed Brown   PetscErrorCode ierr;
86924989b8cSPeter Brune   TSIJacobian    ijacobian;
87024989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
87124989b8cSPeter Brune   DM             dm;
87224989b8cSPeter Brune   void           *ctx;
873316643e7SJed Brown 
874316643e7SJed Brown   PetscFunctionBegin;
8750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8760910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8770910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
878316643e7SJed Brown   PetscValidPointer(A,6);
87994ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
880316643e7SJed Brown   PetscValidPointer(B,7);
88194ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
88224989b8cSPeter Brune 
88324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
88424989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
8850298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
88624989b8cSPeter Brune 
887ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
888316643e7SJed Brown 
88994ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
89024989b8cSPeter Brune   if (ijacobian) {
8916cd88445SBarry Smith     PetscBool missing;
892316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
893d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
894316643e7SJed Brown     PetscStackPop;
8956cd88445SBarry Smith     if (A) {
8966cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
8976cd88445SBarry 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");
8986cd88445SBarry Smith     }
8996cd88445SBarry Smith     if (B && B != A) {
9006cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9016cd88445SBarry 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");
9026cd88445SBarry Smith     }
9034a6899ffSJed Brown   }
904214bc6a2SJed Brown   if (imex) {
905b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9064c26be97Sstefano_zampini       PetscBool assembled;
90794ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9084c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9094c26be97Sstefano_zampini       if (!assembled) {
9104c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9114c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9124c26be97Sstefano_zampini       }
91394ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
91494ab13aaSBarry Smith       if (A != B) {
91594ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9164c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9174c26be97Sstefano_zampini         if (!assembled) {
9184c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9194c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9204c26be97Sstefano_zampini         }
92194ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
922214bc6a2SJed Brown       }
923214bc6a2SJed Brown     }
924214bc6a2SJed Brown   } else {
925e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
926e1244c69SJed Brown     if (rhsjacobian) {
927214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
928d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
929e1244c69SJed Brown     }
93094ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
931e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
932e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
93394ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
93494ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
93594ab13aaSBarry Smith       if (A != B) {
93694ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
93794ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
938316643e7SJed Brown       }
939e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
940e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
941e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
94294ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
94394ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
94494ab13aaSBarry Smith         if (A != B) {
94594ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
94694ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
947214bc6a2SJed Brown         }
948316643e7SJed Brown       }
94994ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
95094ab13aaSBarry Smith       if (A != B) {
95194ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
952316643e7SJed Brown       }
953316643e7SJed Brown     }
954316643e7SJed Brown   }
95594ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
956316643e7SJed Brown   PetscFunctionReturn(0);
957316643e7SJed Brown }
958316643e7SJed Brown 
959d763cef2SBarry Smith /*@C
960d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
961b5abc632SBarry Smith     where U_t = G(t,u).
962d763cef2SBarry Smith 
9633f9fe445SBarry Smith     Logically Collective on TS
964d763cef2SBarry Smith 
965d763cef2SBarry Smith     Input Parameters:
966d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
9670298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
968d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
969d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
9700298fd71SBarry Smith           function evaluation routine (may be NULL)
971d763cef2SBarry Smith 
972d763cef2SBarry Smith     Calling sequence of func:
97387828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
974d763cef2SBarry Smith 
975d763cef2SBarry Smith +   t - current timestep
976d763cef2SBarry Smith .   u - input vector
977d763cef2SBarry Smith .   F - function vector
978d763cef2SBarry Smith -   ctx - [optional] user-defined function context
979d763cef2SBarry Smith 
980d763cef2SBarry Smith     Level: beginner
981d763cef2SBarry Smith 
9822bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
9832bbac0d3SBarry Smith 
984d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
985d763cef2SBarry Smith 
986ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
987d763cef2SBarry Smith @*/
988089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
989d763cef2SBarry Smith {
990089b2837SJed Brown   PetscErrorCode ierr;
991089b2837SJed Brown   SNES           snes;
9920298fd71SBarry Smith   Vec            ralloc = NULL;
99324989b8cSPeter Brune   DM             dm;
994d763cef2SBarry Smith 
995089b2837SJed Brown   PetscFunctionBegin;
9960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
997ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
99824989b8cSPeter Brune 
99924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
100024989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1001089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1002e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1003e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1004e856ceecSJed Brown     r = ralloc;
1005e856ceecSJed Brown   }
1006089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1007e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1008d763cef2SBarry Smith   PetscFunctionReturn(0);
1009d763cef2SBarry Smith }
1010d763cef2SBarry Smith 
1011ef20d060SBarry Smith /*@C
1012abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1013ef20d060SBarry Smith 
1014ef20d060SBarry Smith     Logically Collective on TS
1015ef20d060SBarry Smith 
1016ef20d060SBarry Smith     Input Parameters:
1017ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1018ef20d060SBarry Smith .   f - routine for evaluating the solution
1019ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10200298fd71SBarry Smith           function evaluation routine (may be NULL)
1021ef20d060SBarry Smith 
1022ef20d060SBarry Smith     Calling sequence of func:
1023ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1024ef20d060SBarry Smith 
1025ef20d060SBarry Smith +   t - current timestep
1026ef20d060SBarry Smith .   u - output vector
1027ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1028ef20d060SBarry Smith 
1029abd5a294SJed Brown     Notes:
1030abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1031abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1032abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1033abd5a294SJed Brown 
10344f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1035abd5a294SJed Brown 
1036ef20d060SBarry Smith     Level: beginner
1037ef20d060SBarry Smith 
1038ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1039ef20d060SBarry Smith 
10409b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
1041ef20d060SBarry Smith @*/
1042ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1043ef20d060SBarry Smith {
1044ef20d060SBarry Smith   PetscErrorCode ierr;
1045ef20d060SBarry Smith   DM             dm;
1046ef20d060SBarry Smith 
1047ef20d060SBarry Smith   PetscFunctionBegin;
1048ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1049ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1050ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1051ef20d060SBarry Smith   PetscFunctionReturn(0);
1052ef20d060SBarry Smith }
1053ef20d060SBarry Smith 
10549b7cd975SBarry Smith /*@C
10559b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
10569b7cd975SBarry Smith 
10579b7cd975SBarry Smith     Logically Collective on TS
10589b7cd975SBarry Smith 
10599b7cd975SBarry Smith     Input Parameters:
10609b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1061e162b725SBarry Smith .   func - routine for evaluating the forcing function
10629b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
10630298fd71SBarry Smith           function evaluation routine (may be NULL)
10649b7cd975SBarry Smith 
10659b7cd975SBarry Smith     Calling sequence of func:
1066e162b725SBarry Smith $     func (TS ts,PetscReal t,Vec f,void *ctx);
10679b7cd975SBarry Smith 
10689b7cd975SBarry Smith +   t - current timestep
1069e162b725SBarry Smith .   f - output vector
10709b7cd975SBarry Smith -   ctx - [optional] user-defined function context
10719b7cd975SBarry Smith 
10729b7cd975SBarry Smith     Notes:
10739b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1074e162b725SBarry Smith     create closed-form solutions with a non-physical forcing term. It allows you to use the Method of Manufactored Solution without directly editing the
1075e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1076e162b725SBarry Smith 
1077e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1078e162b725SBarry Smith 
1079e162b725SBarry Smith     This forcing function does not depend on the solution to the equations, it can only depend on spatial location, time, and possibly parameters, the
1080e162b725SBarry Smith     parameters can be passed in the ctx variable.
10819b7cd975SBarry Smith 
10829b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
10839b7cd975SBarry Smith 
10849b7cd975SBarry Smith     Level: beginner
10859b7cd975SBarry Smith 
10869b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
10879b7cd975SBarry Smith 
10889b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
10899b7cd975SBarry Smith @*/
1090e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
10919b7cd975SBarry Smith {
10929b7cd975SBarry Smith   PetscErrorCode ierr;
10939b7cd975SBarry Smith   DM             dm;
10949b7cd975SBarry Smith 
10959b7cd975SBarry Smith   PetscFunctionBegin;
10969b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10979b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1098e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
10999b7cd975SBarry Smith   PetscFunctionReturn(0);
11009b7cd975SBarry Smith }
11019b7cd975SBarry Smith 
1102d763cef2SBarry Smith /*@C
1103f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1104b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1105d763cef2SBarry Smith 
11063f9fe445SBarry Smith    Logically Collective on TS
1107d763cef2SBarry Smith 
1108d763cef2SBarry Smith    Input Parameters:
1109d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1110e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1111e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1112d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1113d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11140298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1115d763cef2SBarry Smith 
1116f7ab8db6SBarry Smith    Calling sequence of f:
1117ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1118d763cef2SBarry Smith 
1119d763cef2SBarry Smith +  t - current timestep
1120d763cef2SBarry Smith .  u - input vector
1121e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1122e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1123d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1124d763cef2SBarry Smith 
11256cd88445SBarry Smith    Notes:
11266cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11276cd88445SBarry Smith 
11286cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1129ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1130d763cef2SBarry Smith 
1131d763cef2SBarry Smith    Level: beginner
1132d763cef2SBarry Smith 
1133d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1134d763cef2SBarry Smith 
1135ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1136d763cef2SBarry Smith 
1137d763cef2SBarry Smith @*/
1138e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1139d763cef2SBarry Smith {
1140277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1141089b2837SJed Brown   SNES           snes;
114224989b8cSPeter Brune   DM             dm;
114324989b8cSPeter Brune   TSIJacobian    ijacobian;
1144277b19d0SLisandro Dalcin 
1145d763cef2SBarry Smith   PetscFunctionBegin;
11460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1147e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1148e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1149e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1150e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1151d763cef2SBarry Smith 
115224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
115324989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1154e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1155e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1156e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1157e1244c69SJed Brown   }
11580298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1159089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11605f659677SPeter Brune   if (!ijacobian) {
1161e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
11620e4ef248SJed Brown   }
1163e5d3d808SBarry Smith   if (Amat) {
1164e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
11650e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1166e5d3d808SBarry Smith     ts->Arhs = Amat;
11670e4ef248SJed Brown   }
1168e5d3d808SBarry Smith   if (Pmat) {
1169e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
11700e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1171e5d3d808SBarry Smith     ts->Brhs = Pmat;
11720e4ef248SJed Brown   }
1173d763cef2SBarry Smith   PetscFunctionReturn(0);
1174d763cef2SBarry Smith }
1175d763cef2SBarry Smith 
1176316643e7SJed Brown 
1177316643e7SJed Brown /*@C
1178b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1179316643e7SJed Brown 
11803f9fe445SBarry Smith    Logically Collective on TS
1181316643e7SJed Brown 
1182316643e7SJed Brown    Input Parameters:
1183316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
11840298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1185316643e7SJed Brown .  f   - the function evaluation routine
11860298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1187316643e7SJed Brown 
1188316643e7SJed Brown    Calling sequence of f:
1189316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1190316643e7SJed Brown 
1191316643e7SJed Brown +  t   - time at step/stage being solved
1192316643e7SJed Brown .  u   - state vector
1193316643e7SJed Brown .  u_t - time derivative of state vector
1194316643e7SJed Brown .  F   - function vector
1195316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1196316643e7SJed Brown 
1197316643e7SJed Brown    Important:
11982bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1199316643e7SJed Brown 
1200316643e7SJed Brown    Level: beginner
1201316643e7SJed Brown 
1202316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1203316643e7SJed Brown 
1204d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1205316643e7SJed Brown @*/
120651699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1207316643e7SJed Brown {
1208089b2837SJed Brown   PetscErrorCode ierr;
1209089b2837SJed Brown   SNES           snes;
121051699248SLisandro Dalcin   Vec            ralloc = NULL;
121124989b8cSPeter Brune   DM             dm;
1212316643e7SJed Brown 
1213316643e7SJed Brown   PetscFunctionBegin;
12140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
121551699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
121624989b8cSPeter Brune 
121724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
121824989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
121924989b8cSPeter Brune 
1220089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
122151699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
122251699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
122351699248SLisandro Dalcin     r  = ralloc;
1224e856ceecSJed Brown   }
122551699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
122651699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1227089b2837SJed Brown   PetscFunctionReturn(0);
1228089b2837SJed Brown }
1229089b2837SJed Brown 
1230089b2837SJed Brown /*@C
1231089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1232089b2837SJed Brown 
1233089b2837SJed Brown    Not Collective
1234089b2837SJed Brown 
1235089b2837SJed Brown    Input Parameter:
1236089b2837SJed Brown .  ts - the TS context
1237089b2837SJed Brown 
1238089b2837SJed Brown    Output Parameter:
12390298fd71SBarry Smith +  r - vector to hold residual (or NULL)
12400298fd71SBarry Smith .  func - the function to compute residual (or NULL)
12410298fd71SBarry Smith -  ctx - the function context (or NULL)
1242089b2837SJed Brown 
1243089b2837SJed Brown    Level: advanced
1244089b2837SJed Brown 
1245089b2837SJed Brown .keywords: TS, nonlinear, get, function
1246089b2837SJed Brown 
1247089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1248089b2837SJed Brown @*/
1249089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1250089b2837SJed Brown {
1251089b2837SJed Brown   PetscErrorCode ierr;
1252089b2837SJed Brown   SNES           snes;
125324989b8cSPeter Brune   DM             dm;
1254089b2837SJed Brown 
1255089b2837SJed Brown   PetscFunctionBegin;
1256089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1257089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12580298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
125924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
126024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1261089b2837SJed Brown   PetscFunctionReturn(0);
1262089b2837SJed Brown }
1263089b2837SJed Brown 
1264089b2837SJed Brown /*@C
1265089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1266089b2837SJed Brown 
1267089b2837SJed Brown    Not Collective
1268089b2837SJed Brown 
1269089b2837SJed Brown    Input Parameter:
1270089b2837SJed Brown .  ts - the TS context
1271089b2837SJed Brown 
1272089b2837SJed Brown    Output Parameter:
12730298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
12740298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
12750298fd71SBarry Smith -  ctx - the function context (or NULL)
1276089b2837SJed Brown 
1277089b2837SJed Brown    Level: advanced
1278089b2837SJed Brown 
1279089b2837SJed Brown .keywords: TS, nonlinear, get, function
1280089b2837SJed Brown 
12812bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1282089b2837SJed Brown @*/
1283089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1284089b2837SJed Brown {
1285089b2837SJed Brown   PetscErrorCode ierr;
1286089b2837SJed Brown   SNES           snes;
128724989b8cSPeter Brune   DM             dm;
1288089b2837SJed Brown 
1289089b2837SJed Brown   PetscFunctionBegin;
1290089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1291089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12920298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
129324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
129424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1295316643e7SJed Brown   PetscFunctionReturn(0);
1296316643e7SJed Brown }
1297316643e7SJed Brown 
1298316643e7SJed Brown /*@C
1299a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1300ae8867d6SBarry Smith         provided with TSSetIFunction().
1301316643e7SJed Brown 
13023f9fe445SBarry Smith    Logically Collective on TS
1303316643e7SJed Brown 
1304316643e7SJed Brown    Input Parameters:
1305316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1306e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1307e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1308316643e7SJed Brown .  f   - the Jacobian evaluation routine
13090298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1310316643e7SJed Brown 
1311316643e7SJed Brown    Calling sequence of f:
1312ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1313316643e7SJed Brown 
1314316643e7SJed Brown +  t    - time at step/stage being solved
13151b4a444bSJed Brown .  U    - state vector
13161b4a444bSJed Brown .  U_t  - time derivative of state vector
1317316643e7SJed Brown .  a    - shift
1318e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1319e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1320316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1321316643e7SJed Brown 
1322316643e7SJed Brown    Notes:
1323e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1324316643e7SJed Brown 
1325895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1326895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1327895c21f2SBarry Smith 
1328a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1329b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1330a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1331a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1332a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1333a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1334a4f0a591SBarry Smith 
13356cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13366cd88445SBarry Smith 
13376cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1338ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1339ca5f011dSBarry Smith 
1340316643e7SJed Brown    Level: beginner
1341316643e7SJed Brown 
1342316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1343316643e7SJed Brown 
1344ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1345316643e7SJed Brown 
1346316643e7SJed Brown @*/
1347e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1348316643e7SJed Brown {
1349316643e7SJed Brown   PetscErrorCode ierr;
1350089b2837SJed Brown   SNES           snes;
135124989b8cSPeter Brune   DM             dm;
1352316643e7SJed Brown 
1353316643e7SJed Brown   PetscFunctionBegin;
13540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1355e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1356e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1357e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1358e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
135924989b8cSPeter Brune 
136024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
136124989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
136224989b8cSPeter Brune 
1363089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1364e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1365316643e7SJed Brown   PetscFunctionReturn(0);
1366316643e7SJed Brown }
1367316643e7SJed Brown 
1368e1244c69SJed Brown /*@
1369e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1370e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1371e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1372e1244c69SJed Brown    not been changed by the TS.
1373e1244c69SJed Brown 
1374e1244c69SJed Brown    Logically Collective
1375e1244c69SJed Brown 
1376e1244c69SJed Brown    Input Arguments:
1377e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1378e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1379e1244c69SJed Brown 
1380e1244c69SJed Brown    Level: intermediate
1381e1244c69SJed Brown 
1382e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1383e1244c69SJed Brown @*/
1384e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1385e1244c69SJed Brown {
1386e1244c69SJed Brown   PetscFunctionBegin;
1387e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1388e1244c69SJed Brown   PetscFunctionReturn(0);
1389e1244c69SJed Brown }
1390e1244c69SJed Brown 
1391efe9872eSLisandro Dalcin /*@C
1392efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1393efe9872eSLisandro Dalcin 
1394efe9872eSLisandro Dalcin    Logically Collective on TS
1395efe9872eSLisandro Dalcin 
1396efe9872eSLisandro Dalcin    Input Parameters:
1397efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1398efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1399efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1400efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1401efe9872eSLisandro Dalcin 
1402efe9872eSLisandro Dalcin    Calling sequence of fun:
1403efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1404efe9872eSLisandro Dalcin 
1405efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1406efe9872eSLisandro Dalcin .  U    - state vector
1407efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1408efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1409efe9872eSLisandro Dalcin .  F    - function vector
1410efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1411efe9872eSLisandro Dalcin 
1412efe9872eSLisandro Dalcin    Level: beginner
1413efe9872eSLisandro Dalcin 
1414efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1415efe9872eSLisandro Dalcin 
1416efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1417efe9872eSLisandro Dalcin @*/
1418efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1419efe9872eSLisandro Dalcin {
1420efe9872eSLisandro Dalcin   DM             dm;
1421efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1422efe9872eSLisandro Dalcin 
1423efe9872eSLisandro Dalcin   PetscFunctionBegin;
1424efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1425efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1426efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1427efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1428efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1429efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1430efe9872eSLisandro Dalcin }
1431efe9872eSLisandro Dalcin 
1432efe9872eSLisandro Dalcin /*@C
1433efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1434efe9872eSLisandro Dalcin 
1435efe9872eSLisandro Dalcin   Not Collective
1436efe9872eSLisandro Dalcin 
1437efe9872eSLisandro Dalcin   Input Parameter:
1438efe9872eSLisandro Dalcin . ts - the TS context
1439efe9872eSLisandro Dalcin 
1440efe9872eSLisandro Dalcin   Output Parameter:
1441efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1442efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1443efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1444efe9872eSLisandro Dalcin 
1445efe9872eSLisandro Dalcin   Level: advanced
1446efe9872eSLisandro Dalcin 
1447efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1448efe9872eSLisandro Dalcin 
1449efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1450efe9872eSLisandro Dalcin @*/
1451efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1452efe9872eSLisandro Dalcin {
1453efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1454efe9872eSLisandro Dalcin   SNES           snes;
1455efe9872eSLisandro Dalcin   DM             dm;
1456efe9872eSLisandro Dalcin 
1457efe9872eSLisandro Dalcin   PetscFunctionBegin;
1458efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1459efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1460efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1461efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1462efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1463efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1464efe9872eSLisandro Dalcin }
1465efe9872eSLisandro Dalcin 
1466efe9872eSLisandro Dalcin /*@C
1467bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1468efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1469efe9872eSLisandro Dalcin 
1470efe9872eSLisandro Dalcin    Logically Collective on TS
1471efe9872eSLisandro Dalcin 
1472efe9872eSLisandro Dalcin    Input Parameters:
1473efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1474efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1475efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1476efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1477efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1478efe9872eSLisandro Dalcin 
1479efe9872eSLisandro Dalcin    Calling sequence of jac:
1480bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1481efe9872eSLisandro Dalcin 
1482efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1483efe9872eSLisandro Dalcin .  U    - state vector
1484efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1485efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1486efe9872eSLisandro Dalcin .  v    - shift for U_t
1487efe9872eSLisandro Dalcin .  a    - shift for U_tt
1488efe9872eSLisandro Dalcin .  J    - Jacobian of G(U) = F(t,U,W+v*U,W'+a*U), equivalent to dF/dU + v*dF/dU_t  + a*dF/dU_tt
1489efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1490efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1491efe9872eSLisandro Dalcin 
1492efe9872eSLisandro Dalcin    Notes:
1493efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1494efe9872eSLisandro Dalcin 
1495efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1496efe9872eSLisandro Dalcin    the Jacobian of G(U) = F(t,U,W+v*U,W'+a*U) where F(t,U,U_t,U_tt) = 0 is the DAE to be solved.
1497efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1498bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1499efe9872eSLisandro Dalcin 
1500efe9872eSLisandro Dalcin    Level: beginner
1501efe9872eSLisandro Dalcin 
1502efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1503efe9872eSLisandro Dalcin 
1504efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1505efe9872eSLisandro Dalcin @*/
1506efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1507efe9872eSLisandro Dalcin {
1508efe9872eSLisandro Dalcin   DM             dm;
1509efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1510efe9872eSLisandro Dalcin 
1511efe9872eSLisandro Dalcin   PetscFunctionBegin;
1512efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1513efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1514efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1515efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1516efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1517efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1518efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1519efe9872eSLisandro Dalcin }
1520efe9872eSLisandro Dalcin 
1521efe9872eSLisandro Dalcin /*@C
1522efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1523efe9872eSLisandro Dalcin 
1524efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1525efe9872eSLisandro Dalcin 
1526efe9872eSLisandro Dalcin   Input Parameter:
1527efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1528efe9872eSLisandro Dalcin 
1529efe9872eSLisandro Dalcin   Output Parameters:
1530efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1531efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1532efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1533efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1534efe9872eSLisandro Dalcin 
1535efe9872eSLisandro Dalcin   Notes: You can pass in NULL for any return argument you do not need.
1536efe9872eSLisandro Dalcin 
1537efe9872eSLisandro Dalcin   Level: advanced
1538efe9872eSLisandro Dalcin 
1539efe9872eSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
1540efe9872eSLisandro Dalcin 
1541efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1542efe9872eSLisandro Dalcin @*/
1543efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1544efe9872eSLisandro Dalcin {
1545efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1546efe9872eSLisandro Dalcin   SNES           snes;
1547efe9872eSLisandro Dalcin   DM             dm;
1548efe9872eSLisandro Dalcin 
1549efe9872eSLisandro Dalcin   PetscFunctionBegin;
1550efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1551efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1552efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1553efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1554efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1555efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1556efe9872eSLisandro Dalcin }
1557efe9872eSLisandro Dalcin 
1558efe9872eSLisandro Dalcin /*@
1559efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1560efe9872eSLisandro Dalcin 
1561efe9872eSLisandro Dalcin   Collective on TS and Vec
1562efe9872eSLisandro Dalcin 
1563efe9872eSLisandro Dalcin   Input Parameters:
1564efe9872eSLisandro Dalcin + ts - the TS context
1565efe9872eSLisandro Dalcin . t - current time
1566efe9872eSLisandro Dalcin . U - state vector
1567efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1568efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1569efe9872eSLisandro Dalcin 
1570efe9872eSLisandro Dalcin   Output Parameter:
1571efe9872eSLisandro Dalcin . F - the residual vector
1572efe9872eSLisandro Dalcin 
1573efe9872eSLisandro Dalcin   Note:
1574efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1575efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1576efe9872eSLisandro Dalcin 
1577efe9872eSLisandro Dalcin   Level: developer
1578efe9872eSLisandro Dalcin 
1579efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1580efe9872eSLisandro Dalcin 
1581efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1582efe9872eSLisandro Dalcin @*/
1583efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1584efe9872eSLisandro Dalcin {
1585efe9872eSLisandro Dalcin   DM             dm;
1586efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1587efe9872eSLisandro Dalcin   void           *ctx;
1588efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1589efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1590efe9872eSLisandro Dalcin 
1591efe9872eSLisandro Dalcin   PetscFunctionBegin;
1592efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1593efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1594efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1595efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1596efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1597efe9872eSLisandro Dalcin 
1598efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1599efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1600efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1601efe9872eSLisandro Dalcin 
1602efe9872eSLisandro Dalcin   if (!I2Function) {
1603efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1604efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1605efe9872eSLisandro Dalcin   }
1606efe9872eSLisandro Dalcin 
1607efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1608efe9872eSLisandro Dalcin 
1609efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1610efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1611efe9872eSLisandro Dalcin   PetscStackPop;
1612efe9872eSLisandro Dalcin 
1613efe9872eSLisandro Dalcin   if (rhsfunction) {
1614efe9872eSLisandro Dalcin     Vec Frhs;
1615efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1616efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1617efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1618efe9872eSLisandro Dalcin   }
1619efe9872eSLisandro Dalcin 
1620efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1621efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1622efe9872eSLisandro Dalcin }
1623efe9872eSLisandro Dalcin 
1624efe9872eSLisandro Dalcin /*@
1625efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1626efe9872eSLisandro Dalcin 
1627efe9872eSLisandro Dalcin   Collective on TS and Vec
1628efe9872eSLisandro Dalcin 
1629efe9872eSLisandro Dalcin   Input Parameters:
1630efe9872eSLisandro Dalcin + ts - the TS context
1631efe9872eSLisandro Dalcin . t - current timestep
1632efe9872eSLisandro Dalcin . U - state vector
1633efe9872eSLisandro Dalcin . V - time derivative of state vector
1634efe9872eSLisandro Dalcin . A - second time derivative of state vector
1635efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1636efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1637efe9872eSLisandro Dalcin 
1638efe9872eSLisandro Dalcin   Output Parameters:
1639efe9872eSLisandro Dalcin + J - Jacobian matrix
1640efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1641efe9872eSLisandro Dalcin 
1642efe9872eSLisandro Dalcin   Notes:
1643efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1644efe9872eSLisandro Dalcin 
1645efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1646efe9872eSLisandro Dalcin 
1647efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1648efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1649efe9872eSLisandro Dalcin 
1650efe9872eSLisandro Dalcin   Level: developer
1651efe9872eSLisandro Dalcin 
1652efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1653efe9872eSLisandro Dalcin 
1654efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1655efe9872eSLisandro Dalcin @*/
1656efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1657efe9872eSLisandro Dalcin {
1658efe9872eSLisandro Dalcin   DM             dm;
1659efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1660efe9872eSLisandro Dalcin   void           *ctx;
1661efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1662efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1663efe9872eSLisandro Dalcin 
1664efe9872eSLisandro Dalcin   PetscFunctionBegin;
1665efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1666efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1667efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1668efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1669efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1670efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1671efe9872eSLisandro Dalcin 
1672efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1673efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1674efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1675efe9872eSLisandro Dalcin 
1676efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1677efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1678efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1679efe9872eSLisandro Dalcin   }
1680efe9872eSLisandro Dalcin 
1681efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1682efe9872eSLisandro Dalcin 
1683efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1684efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1685efe9872eSLisandro Dalcin   PetscStackPop;
1686efe9872eSLisandro Dalcin 
1687efe9872eSLisandro Dalcin   if (rhsjacobian) {
1688efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1689efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1690efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1691efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1692efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1693efe9872eSLisandro Dalcin   }
1694efe9872eSLisandro Dalcin 
1695efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1696efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1697efe9872eSLisandro Dalcin }
1698efe9872eSLisandro Dalcin 
1699efe9872eSLisandro Dalcin /*@
1700efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1701efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1702efe9872eSLisandro Dalcin 
1703efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1704efe9872eSLisandro Dalcin 
1705efe9872eSLisandro Dalcin    Input Parameters:
1706efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1707efe9872eSLisandro Dalcin .  u - the solution vector
1708efe9872eSLisandro Dalcin -  v - the time derivative vector
1709efe9872eSLisandro Dalcin 
1710efe9872eSLisandro Dalcin    Level: beginner
1711efe9872eSLisandro Dalcin 
1712efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1713efe9872eSLisandro Dalcin @*/
1714efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1715efe9872eSLisandro Dalcin {
1716efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1717efe9872eSLisandro Dalcin 
1718efe9872eSLisandro Dalcin   PetscFunctionBegin;
1719efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1720efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1721efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1722efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1723efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1724efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1725efe9872eSLisandro Dalcin   ts->vec_dot = v;
1726efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1727efe9872eSLisandro Dalcin }
1728efe9872eSLisandro Dalcin 
1729efe9872eSLisandro Dalcin /*@
1730efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1731efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1732efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1733efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1734efe9872eSLisandro Dalcin 
1735efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1736efe9872eSLisandro Dalcin 
1737efe9872eSLisandro Dalcin    Input Parameter:
1738efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1739efe9872eSLisandro Dalcin 
1740efe9872eSLisandro Dalcin    Output Parameter:
1741efe9872eSLisandro Dalcin +  u - the vector containing the solution
1742efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1743efe9872eSLisandro Dalcin 
1744efe9872eSLisandro Dalcin    Level: intermediate
1745efe9872eSLisandro Dalcin 
1746efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1747efe9872eSLisandro Dalcin 
1748efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1749efe9872eSLisandro Dalcin @*/
1750efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1751efe9872eSLisandro Dalcin {
1752efe9872eSLisandro Dalcin   PetscFunctionBegin;
1753efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1754efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1755efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1756efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1757efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1758efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1759efe9872eSLisandro Dalcin }
1760efe9872eSLisandro Dalcin 
176155849f57SBarry Smith /*@C
176255849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
176355849f57SBarry Smith 
176455849f57SBarry Smith   Collective on PetscViewer
176555849f57SBarry Smith 
176655849f57SBarry Smith   Input Parameters:
176755849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
176855849f57SBarry Smith            some related function before a call to TSLoad().
176955849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
177055849f57SBarry Smith 
177155849f57SBarry Smith    Level: intermediate
177255849f57SBarry Smith 
177355849f57SBarry Smith   Notes:
177455849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
177555849f57SBarry Smith 
177655849f57SBarry Smith   Notes for advanced users:
177755849f57SBarry Smith   Most users should not need to know the details of the binary storage
177855849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
177955849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
178055849f57SBarry Smith   format is
178155849f57SBarry Smith .vb
178255849f57SBarry Smith      has not yet been determined
178355849f57SBarry Smith .ve
178455849f57SBarry Smith 
178555849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
178655849f57SBarry Smith @*/
1787f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
178855849f57SBarry Smith {
178955849f57SBarry Smith   PetscErrorCode ierr;
179055849f57SBarry Smith   PetscBool      isbinary;
179155849f57SBarry Smith   PetscInt       classid;
179255849f57SBarry Smith   char           type[256];
17932d53ad75SBarry Smith   DMTS           sdm;
1794ad6bc421SBarry Smith   DM             dm;
179555849f57SBarry Smith 
179655849f57SBarry Smith   PetscFunctionBegin;
1797f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
179855849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
179955849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
180055849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
180155849f57SBarry Smith 
1802060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1803ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1804060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1805f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1806f2c2a1b9SBarry Smith   if (ts->ops->load) {
1807f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1808f2c2a1b9SBarry Smith   }
1809ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1810ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1811ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1812f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1813f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
18142d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18152d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
181655849f57SBarry Smith   PetscFunctionReturn(0);
181755849f57SBarry Smith }
181855849f57SBarry Smith 
18199804daf3SBarry Smith #include <petscdraw.h>
1820e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1821e04113cfSBarry Smith #include <petscviewersaws.h>
1822f05ece33SBarry Smith #endif
18237e2c5f70SBarry Smith /*@C
1824d763cef2SBarry Smith     TSView - Prints the TS data structure.
1825d763cef2SBarry Smith 
18264c49b128SBarry Smith     Collective on TS
1827d763cef2SBarry Smith 
1828d763cef2SBarry Smith     Input Parameters:
1829d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1830d763cef2SBarry Smith -   viewer - visualization context
1831d763cef2SBarry Smith 
1832d763cef2SBarry Smith     Options Database Key:
1833d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1834d763cef2SBarry Smith 
1835d763cef2SBarry Smith     Notes:
1836d763cef2SBarry Smith     The available visualization contexts include
1837b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1838b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1839d763cef2SBarry Smith          output where only the first processor opens
1840d763cef2SBarry Smith          the file.  All other processors send their
1841d763cef2SBarry Smith          data to the first processor to print.
1842d763cef2SBarry Smith 
1843d763cef2SBarry Smith     The user can open an alternative visualization context with
1844b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1845d763cef2SBarry Smith 
1846d763cef2SBarry Smith     Level: beginner
1847d763cef2SBarry Smith 
1848d763cef2SBarry Smith .keywords: TS, timestep, view
1849d763cef2SBarry Smith 
1850b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1851d763cef2SBarry Smith @*/
18527087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1853d763cef2SBarry Smith {
1854dfbe8321SBarry Smith   PetscErrorCode ierr;
185519fd82e9SBarry Smith   TSType         type;
18562b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
18572d53ad75SBarry Smith   DMTS           sdm;
1858e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1859536b137fSBarry Smith   PetscBool      issaws;
1860f05ece33SBarry Smith #endif
1861d763cef2SBarry Smith 
1862d763cef2SBarry Smith   PetscFunctionBegin;
18630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18643050cee2SBarry Smith   if (!viewer) {
1865ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
18663050cee2SBarry Smith   }
18670700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1868c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1869fd16b177SBarry Smith 
1870251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1871251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
187255849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
18732b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1874e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1875536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1876f05ece33SBarry Smith #endif
187732077d6dSBarry Smith   if (iascii) {
1878dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
187977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
18807c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1881d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
18825ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1883c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1884d763cef2SBarry Smith     }
18855ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1886193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1887a0af407cSBarry Smith     if (ts->vrtol) {
1888a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1889a0af407cSBarry Smith     } else {
1890a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
1891a0af407cSBarry Smith     }
1892a0af407cSBarry Smith     if (ts->vatol) {
1893a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
1894a0af407cSBarry Smith     } else {
1895a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
1896a0af407cSBarry Smith     }
18972d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18982d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1899d52bd9f3SBarry Smith     if (ts->ops->view) {
1900d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1901d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1902d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1903d52bd9f3SBarry Smith     }
19040f5bd95cSBarry Smith   } else if (isstring) {
1905a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1906b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
190755849f57SBarry Smith   } else if (isbinary) {
190855849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
190955849f57SBarry Smith     MPI_Comm    comm;
191055849f57SBarry Smith     PetscMPIInt rank;
191155849f57SBarry Smith     char        type[256];
191255849f57SBarry Smith 
191355849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
191455849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
191555849f57SBarry Smith     if (!rank) {
191655849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
191755849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
191855849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
191955849f57SBarry Smith     }
192055849f57SBarry Smith     if (ts->ops->view) {
192155849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
192255849f57SBarry Smith     }
1923f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1924f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
19252d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19262d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19272b0a91c0SBarry Smith   } else if (isdraw) {
19282b0a91c0SBarry Smith     PetscDraw draw;
19292b0a91c0SBarry Smith     char      str[36];
193089fd9fafSBarry Smith     PetscReal x,y,bottom,h;
19312b0a91c0SBarry Smith 
19322b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
19332b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
19342b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
19352b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
193651fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
193789fd9fafSBarry Smith     bottom = y - h;
19382b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
19392b0a91c0SBarry Smith     if (ts->ops->view) {
19402b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
19412b0a91c0SBarry Smith     }
19422b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1943e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1944536b137fSBarry Smith   } else if (issaws) {
1945d45a07a7SBarry Smith     PetscMPIInt rank;
19462657e9d9SBarry Smith     const char  *name;
19472657e9d9SBarry Smith 
19482657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1949d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1950d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1951d45a07a7SBarry Smith       char       dir[1024];
1952d45a07a7SBarry Smith 
1953e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1954a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
19552657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
19562657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
19572657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1958d763cef2SBarry Smith     }
19590acecf5bSBarry Smith     if (ts->ops->view) {
19600acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
19610acecf5bSBarry Smith     }
1962f05ece33SBarry Smith #endif
1963f05ece33SBarry Smith   }
1964f05ece33SBarry Smith 
1965b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1966251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1967b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1968d763cef2SBarry Smith   PetscFunctionReturn(0);
1969d763cef2SBarry Smith }
1970d763cef2SBarry Smith 
1971d763cef2SBarry Smith 
1972b07ff414SBarry Smith /*@
1973d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1974d763cef2SBarry Smith    the timesteppers.
1975d763cef2SBarry Smith 
19763f9fe445SBarry Smith    Logically Collective on TS
1977d763cef2SBarry Smith 
1978d763cef2SBarry Smith    Input Parameters:
1979d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1980d763cef2SBarry Smith -  usrP - optional user context
1981d763cef2SBarry Smith 
1982daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
1983daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
1984daf670e6SBarry Smith 
1985d763cef2SBarry Smith    Level: intermediate
1986d763cef2SBarry Smith 
1987d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1988d763cef2SBarry Smith 
1989d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1990d763cef2SBarry Smith @*/
19917087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1992d763cef2SBarry Smith {
1993d763cef2SBarry Smith   PetscFunctionBegin;
19940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1995d763cef2SBarry Smith   ts->user = usrP;
1996d763cef2SBarry Smith   PetscFunctionReturn(0);
1997d763cef2SBarry Smith }
1998d763cef2SBarry Smith 
1999b07ff414SBarry Smith /*@
2000d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2001d763cef2SBarry Smith     timestepper.
2002d763cef2SBarry Smith 
2003d763cef2SBarry Smith     Not Collective
2004d763cef2SBarry Smith 
2005d763cef2SBarry Smith     Input Parameter:
2006d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2007d763cef2SBarry Smith 
2008d763cef2SBarry Smith     Output Parameter:
2009d763cef2SBarry Smith .   usrP - user context
2010d763cef2SBarry Smith 
2011daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2012daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2013daf670e6SBarry Smith 
2014d763cef2SBarry Smith     Level: intermediate
2015d763cef2SBarry Smith 
2016d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2017d763cef2SBarry Smith 
2018d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2019d763cef2SBarry Smith @*/
2020e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2021d763cef2SBarry Smith {
2022d763cef2SBarry Smith   PetscFunctionBegin;
20230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2024e71120c6SJed Brown   *(void**)usrP = ts->user;
2025d763cef2SBarry Smith   PetscFunctionReturn(0);
2026d763cef2SBarry Smith }
2027d763cef2SBarry Smith 
2028d763cef2SBarry Smith /*@
2029b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
2030d763cef2SBarry Smith 
2031d763cef2SBarry Smith    Not Collective
2032d763cef2SBarry Smith 
2033d763cef2SBarry Smith    Input Parameter:
2034d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2035d763cef2SBarry Smith 
2036d763cef2SBarry Smith    Output Parameter:
2037b8123daeSJed Brown .  iter - number of steps completed so far
2038d763cef2SBarry Smith 
2039d763cef2SBarry Smith    Level: intermediate
2040d763cef2SBarry Smith 
2041d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
20429be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2043d763cef2SBarry Smith @*/
20447087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
2045d763cef2SBarry Smith {
2046d763cef2SBarry Smith   PetscFunctionBegin;
20470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20484482741eSBarry Smith   PetscValidIntPointer(iter,2);
2049d763cef2SBarry Smith   *iter = ts->steps;
2050d763cef2SBarry Smith   PetscFunctionReturn(0);
2051d763cef2SBarry Smith }
2052d763cef2SBarry Smith 
2053d763cef2SBarry Smith /*@
2054d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
2055d763cef2SBarry Smith    as well as the initial time.
2056d763cef2SBarry Smith 
20573f9fe445SBarry Smith    Logically Collective on TS
2058d763cef2SBarry Smith 
2059d763cef2SBarry Smith    Input Parameters:
2060d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2061d763cef2SBarry Smith .  initial_time - the initial time
2062d763cef2SBarry Smith -  time_step - the size of the timestep
2063d763cef2SBarry Smith 
2064d763cef2SBarry Smith    Level: intermediate
2065d763cef2SBarry Smith 
2066d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
2067d763cef2SBarry Smith 
2068d763cef2SBarry Smith .keywords: TS, set, initial, timestep
2069d763cef2SBarry Smith @*/
20707087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
2071d763cef2SBarry Smith {
2072e144a568SJed Brown   PetscErrorCode ierr;
2073e144a568SJed Brown 
2074d763cef2SBarry Smith   PetscFunctionBegin;
20750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2076e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
2077d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
2078d763cef2SBarry Smith   PetscFunctionReturn(0);
2079d763cef2SBarry Smith }
2080d763cef2SBarry Smith 
2081d763cef2SBarry Smith /*@
2082d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2083d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2084d763cef2SBarry Smith 
20853f9fe445SBarry Smith    Logically Collective on TS
2086d763cef2SBarry Smith 
2087d763cef2SBarry Smith    Input Parameters:
2088d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2089d763cef2SBarry Smith -  time_step - the size of the timestep
2090d763cef2SBarry Smith 
2091d763cef2SBarry Smith    Level: intermediate
2092d763cef2SBarry Smith 
2093d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2094d763cef2SBarry Smith 
2095d763cef2SBarry Smith .keywords: TS, set, timestep
2096d763cef2SBarry Smith @*/
20977087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2098d763cef2SBarry Smith {
2099d763cef2SBarry Smith   PetscFunctionBegin;
21000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2101c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2102d763cef2SBarry Smith   ts->time_step = time_step;
2103d763cef2SBarry Smith   PetscFunctionReturn(0);
2104d763cef2SBarry Smith }
2105d763cef2SBarry Smith 
2106a43b19c4SJed Brown /*@
210749354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
210849354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
210949354f04SShri Abhyankar      or just return at the final time TS computed.
2110a43b19c4SJed Brown 
2111a43b19c4SJed Brown   Logically Collective on TS
2112a43b19c4SJed Brown 
2113a43b19c4SJed Brown    Input Parameter:
2114a43b19c4SJed Brown +   ts - the time-step context
211549354f04SShri Abhyankar -   eftopt - exact final time option
2116a43b19c4SJed Brown 
2117feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2118feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2119feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2120feed9e9dSBarry Smith 
2121feed9e9dSBarry Smith    Options Database:
2122feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2123feed9e9dSBarry Smith 
2124ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2125ee346746SBarry Smith     then the final time you selected.
2126ee346746SBarry Smith 
2127a43b19c4SJed Brown    Level: beginner
2128a43b19c4SJed Brown 
2129a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
2130a43b19c4SJed Brown @*/
213149354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2132a43b19c4SJed Brown {
2133a43b19c4SJed Brown   PetscFunctionBegin;
2134a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
213549354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
213649354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2137a43b19c4SJed Brown   PetscFunctionReturn(0);
2138a43b19c4SJed Brown }
2139a43b19c4SJed Brown 
2140d763cef2SBarry Smith /*@
2141d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2142d763cef2SBarry Smith 
2143d763cef2SBarry Smith    Not Collective
2144d763cef2SBarry Smith 
2145d763cef2SBarry Smith    Input Parameter:
2146d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2147d763cef2SBarry Smith 
2148d763cef2SBarry Smith    Output Parameter:
2149d763cef2SBarry Smith .  dt - the current timestep size
2150d763cef2SBarry Smith 
2151d763cef2SBarry Smith    Level: intermediate
2152d763cef2SBarry Smith 
2153d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2154d763cef2SBarry Smith 
2155d763cef2SBarry Smith .keywords: TS, get, timestep
2156d763cef2SBarry Smith @*/
21577087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2158d763cef2SBarry Smith {
2159d763cef2SBarry Smith   PetscFunctionBegin;
21600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2161f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2162d763cef2SBarry Smith   *dt = ts->time_step;
2163d763cef2SBarry Smith   PetscFunctionReturn(0);
2164d763cef2SBarry Smith }
2165d763cef2SBarry Smith 
2166d8e5e3e6SSatish Balay /*@
2167d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2168d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2169d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2170d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2171d763cef2SBarry Smith 
2172d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2173d763cef2SBarry Smith 
2174d763cef2SBarry Smith    Input Parameter:
2175d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2176d763cef2SBarry Smith 
2177d763cef2SBarry Smith    Output Parameter:
2178d763cef2SBarry Smith .  v - the vector containing the solution
2179d763cef2SBarry Smith 
218063e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
218163e21af5SBarry Smith    final time. It returns the solution at the next timestep.
218263e21af5SBarry Smith 
2183d763cef2SBarry Smith    Level: intermediate
2184d763cef2SBarry Smith 
2185b2bf4f3aSDebojyoti Ghosh .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents()
2186d763cef2SBarry Smith 
2187d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2188d763cef2SBarry Smith @*/
21897087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2190d763cef2SBarry Smith {
2191d763cef2SBarry Smith   PetscFunctionBegin;
21920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21934482741eSBarry Smith   PetscValidPointer(v,2);
21948737fe31SLisandro Dalcin   *v = ts->vec_sol;
2195d763cef2SBarry Smith   PetscFunctionReturn(0);
2196d763cef2SBarry Smith }
2197d763cef2SBarry Smith 
219803fe5f5eSDebojyoti Ghosh /*@
2199b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
220003fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2201b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
220203fe5f5eSDebojyoti Ghosh    structure as the solution vector.
220303fe5f5eSDebojyoti Ghosh 
220403fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
220503fe5f5eSDebojyoti Ghosh 
220603fe5f5eSDebojyoti Ghosh    Parameters :
220703fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2208b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2209b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
221003fe5f5eSDebojyoti Ghosh        returned in v.
2211b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
221203fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2213b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
221403fe5f5eSDebojyoti Ghosh 
22154cdd57e5SDebojyoti Ghosh    Level: advanced
221603fe5f5eSDebojyoti Ghosh 
221703fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
221803fe5f5eSDebojyoti Ghosh 
221903fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
222003fe5f5eSDebojyoti Ghosh @*/
2221b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
222203fe5f5eSDebojyoti Ghosh {
222303fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
222403fe5f5eSDebojyoti Ghosh 
222503fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
222603fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2227b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
222803fe5f5eSDebojyoti Ghosh   else {
2229b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
223003fe5f5eSDebojyoti Ghosh   }
223103fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
223203fe5f5eSDebojyoti Ghosh }
223303fe5f5eSDebojyoti Ghosh 
22344cdd57e5SDebojyoti Ghosh /*@
22354cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
22364cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
22374cdd57e5SDebojyoti Ghosh 
22384cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
22394cdd57e5SDebojyoti Ghosh 
22404cdd57e5SDebojyoti Ghosh    Parameters :
22414cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
22424cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
22434cdd57e5SDebojyoti Ghosh 
22444cdd57e5SDebojyoti Ghosh    Level: intermediate
22454cdd57e5SDebojyoti Ghosh 
22464cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
22474cdd57e5SDebojyoti Ghosh 
22484cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
22494cdd57e5SDebojyoti Ghosh @*/
22504cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
22514cdd57e5SDebojyoti Ghosh {
22524cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
22534cdd57e5SDebojyoti Ghosh 
22544cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
22554cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2256f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
22574cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2258f6356ec7SDebojyoti Ghosh   } else {
2259f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2260f6356ec7SDebojyoti Ghosh   }
22614cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
22624cdd57e5SDebojyoti Ghosh }
22634cdd57e5SDebojyoti Ghosh 
22644cdd57e5SDebojyoti Ghosh /*@
22654cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
22664cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
22674cdd57e5SDebojyoti Ghosh 
22684cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
22694cdd57e5SDebojyoti Ghosh 
22709657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
22719657682dSDebojyoti Ghosh 
22724cdd57e5SDebojyoti Ghosh    Parameters :
22734cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2274657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
22754cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
22764cdd57e5SDebojyoti Ghosh 
22774cdd57e5SDebojyoti Ghosh    Level: intermediate
22784cdd57e5SDebojyoti Ghosh 
227957df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
22804cdd57e5SDebojyoti Ghosh 
22814cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
22824cdd57e5SDebojyoti Ghosh @*/
22830a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
22844cdd57e5SDebojyoti Ghosh {
22854cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
22864cdd57e5SDebojyoti Ghosh 
22874cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
22884cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2289f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
22900a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2291f6356ec7SDebojyoti Ghosh   } else {
2292f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2293f6356ec7SDebojyoti Ghosh   }
22944cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
22954cdd57e5SDebojyoti Ghosh }
22964cdd57e5SDebojyoti Ghosh 
229757df6a1bSDebojyoti Ghosh /*@
229857df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
229957df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
230057df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
230157df6a1bSDebojyoti Ghosh 
230257df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
230357df6a1bSDebojyoti Ghosh 
230457df6a1bSDebojyoti Ghosh    Parameters :
230557df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
230657df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
230757df6a1bSDebojyoti Ghosh 
230857df6a1bSDebojyoti Ghosh    Level: intermediate
230957df6a1bSDebojyoti Ghosh 
231057df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
231157df6a1bSDebojyoti Ghosh 
231257df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
231357df6a1bSDebojyoti Ghosh @*/
231457df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
231557df6a1bSDebojyoti Ghosh {
231657df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
231757df6a1bSDebojyoti Ghosh 
231857df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
231957df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23209657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
232157df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
232257df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
232357df6a1bSDebojyoti Ghosh   }
232457df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
232557df6a1bSDebojyoti Ghosh }
232657df6a1bSDebojyoti Ghosh 
2327c235aa8dSHong Zhang /*@
2328dfb21088SHong Zhang    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()
2329c235aa8dSHong Zhang 
2330c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
2331c235aa8dSHong Zhang 
2332c235aa8dSHong Zhang    Input Parameter:
2333c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
2334c235aa8dSHong Zhang 
2335c235aa8dSHong Zhang    Output Parameter:
2336abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
2337abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
2338c235aa8dSHong Zhang 
2339c235aa8dSHong Zhang    Level: intermediate
2340c235aa8dSHong Zhang 
2341c235aa8dSHong Zhang .seealso: TSGetTimeStep()
2342c235aa8dSHong Zhang 
2343c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
2344c235aa8dSHong Zhang @*/
2345dfb21088SHong Zhang PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
2346c235aa8dSHong Zhang {
2347c235aa8dSHong Zhang   PetscFunctionBegin;
2348c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2349abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
2350abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
2351abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
2352c235aa8dSHong Zhang   PetscFunctionReturn(0);
2353c235aa8dSHong Zhang }
2354c235aa8dSHong Zhang 
2355bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2356d8e5e3e6SSatish Balay /*@
2357bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2358d763cef2SBarry Smith 
2359bdad233fSMatthew Knepley   Not collective
2360d763cef2SBarry Smith 
2361bdad233fSMatthew Knepley   Input Parameters:
2362bdad233fSMatthew Knepley + ts   - The TS
2363bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2364d763cef2SBarry Smith .vb
23650910c330SBarry Smith          U_t - A U = 0      (linear)
23660910c330SBarry Smith          U_t - A(t) U = 0   (linear)
23670910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2368d763cef2SBarry Smith .ve
2369d763cef2SBarry Smith 
2370d763cef2SBarry Smith    Level: beginner
2371d763cef2SBarry Smith 
2372bdad233fSMatthew Knepley .keywords: TS, problem type
2373bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2374d763cef2SBarry Smith @*/
23757087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2376a7cc72afSBarry Smith {
23779e2a6581SJed Brown   PetscErrorCode ierr;
23789e2a6581SJed Brown 
2379d763cef2SBarry Smith   PetscFunctionBegin;
23800700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2381bdad233fSMatthew Knepley   ts->problem_type = type;
23829e2a6581SJed Brown   if (type == TS_LINEAR) {
23839e2a6581SJed Brown     SNES snes;
23849e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
23859e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
23869e2a6581SJed Brown   }
2387d763cef2SBarry Smith   PetscFunctionReturn(0);
2388d763cef2SBarry Smith }
2389d763cef2SBarry Smith 
2390bdad233fSMatthew Knepley /*@C
2391bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2392bdad233fSMatthew Knepley 
2393bdad233fSMatthew Knepley   Not collective
2394bdad233fSMatthew Knepley 
2395bdad233fSMatthew Knepley   Input Parameter:
2396bdad233fSMatthew Knepley . ts   - The TS
2397bdad233fSMatthew Knepley 
2398bdad233fSMatthew Knepley   Output Parameter:
2399bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2400bdad233fSMatthew Knepley .vb
2401089b2837SJed Brown          M U_t = A U
2402089b2837SJed Brown          M(t) U_t = A(t) U
2403b5abc632SBarry Smith          F(t,U,U_t)
2404bdad233fSMatthew Knepley .ve
2405bdad233fSMatthew Knepley 
2406bdad233fSMatthew Knepley    Level: beginner
2407bdad233fSMatthew Knepley 
2408bdad233fSMatthew Knepley .keywords: TS, problem type
2409bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2410bdad233fSMatthew Knepley @*/
24117087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2412a7cc72afSBarry Smith {
2413bdad233fSMatthew Knepley   PetscFunctionBegin;
24140700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
24154482741eSBarry Smith   PetscValidIntPointer(type,2);
2416bdad233fSMatthew Knepley   *type = ts->problem_type;
2417bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2418bdad233fSMatthew Knepley }
2419d763cef2SBarry Smith 
2420d763cef2SBarry Smith /*@
2421d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2422d763cef2SBarry Smith    of a timestepper.
2423d763cef2SBarry Smith 
2424d763cef2SBarry Smith    Collective on TS
2425d763cef2SBarry Smith 
2426d763cef2SBarry Smith    Input Parameter:
2427d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2428d763cef2SBarry Smith 
2429d763cef2SBarry Smith    Notes:
2430d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2431d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2432d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
2433d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2434d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
2435d763cef2SBarry Smith 
2436d763cef2SBarry Smith    Level: advanced
2437d763cef2SBarry Smith 
2438d763cef2SBarry Smith .keywords: TS, timestep, setup
2439d763cef2SBarry Smith 
2440d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
2441d763cef2SBarry Smith @*/
24427087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2443d763cef2SBarry Smith {
2444dfbe8321SBarry Smith   PetscErrorCode ierr;
24456c6b9e74SPeter Brune   DM             dm;
24466c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2447d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2448cd11d68dSLisandro Dalcin   TSIFunction    ifun;
24496c6b9e74SPeter Brune   TSIJacobian    ijac;
2450efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
24516c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
2452d763cef2SBarry Smith 
2453d763cef2SBarry Smith   PetscFunctionBegin;
24540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2455277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2456277b19d0SLisandro Dalcin 
24577adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2458cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2459cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2460d763cef2SBarry Smith   }
2461277b19d0SLisandro Dalcin 
2462277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2463277b19d0SLisandro Dalcin 
2464e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
2465e1244c69SJed Brown     Mat Amat,Pmat;
2466e1244c69SJed Brown     SNES snes;
2467e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2468e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2469e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2470e1244c69SJed Brown      * have displaced the RHS matrix */
2471e1244c69SJed Brown     if (Amat == ts->Arhs) {
2472e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2473e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2474e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2475e1244c69SJed Brown     }
2476e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2477e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2478e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2479e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2480e1244c69SJed Brown     }
2481e1244c69SJed Brown   }
2482277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2483000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2484277b19d0SLisandro Dalcin   }
2485277b19d0SLisandro Dalcin 
2486a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
24876c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
24886c6b9e74SPeter Brune    */
24896c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
24900298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
24916c6b9e74SPeter Brune   if (!func) {
24926c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
24936c6b9e74SPeter Brune   }
2494a6772fa2SLisandro Dalcin   /* If the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it.
24956c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
24966c6b9e74SPeter Brune    */
24970298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
24980298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2499efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
25000298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2501efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
25026c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
25036c6b9e74SPeter Brune   }
2504c0517034SDebojyoti Ghosh 
2505c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2506c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2507c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2508c0517034SDebojyoti Ghosh   }
2509c0517034SDebojyoti Ghosh 
2510277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2511277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2512277b19d0SLisandro Dalcin }
2513277b19d0SLisandro Dalcin 
2514f6a906c0SBarry Smith /*@
2515f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
2516f6a906c0SBarry Smith    of an adjoint solver
2517f6a906c0SBarry Smith 
2518f6a906c0SBarry Smith    Collective on TS
2519f6a906c0SBarry Smith 
2520f6a906c0SBarry Smith    Input Parameter:
2521f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
2522f6a906c0SBarry Smith 
2523f6a906c0SBarry Smith    Level: advanced
2524f6a906c0SBarry Smith 
2525f6a906c0SBarry Smith .keywords: TS, timestep, setup
2526f6a906c0SBarry Smith 
2527947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
2528f6a906c0SBarry Smith @*/
2529f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
2530f6a906c0SBarry Smith {
2531f6a906c0SBarry Smith   PetscErrorCode ierr;
2532f6a906c0SBarry Smith 
2533f6a906c0SBarry Smith   PetscFunctionBegin;
2534f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2535f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
2536dfb21088SHong Zhang   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
2537d8151eeaSBarry Smith 
2538947abb85SHong Zhang   if (ts->vec_costintegral) { /* if there is integral in the cost function*/
2539d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2540d8151eeaSBarry Smith     if (ts->vecs_sensip){
2541d8151eeaSBarry Smith       ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2542d8151eeaSBarry Smith     }
2543947abb85SHong Zhang   }
2544d8151eeaSBarry Smith 
254542f2b339SBarry Smith   if (ts->ops->adjointsetup) {
254642f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
2547f6a906c0SBarry Smith   }
2548f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
2549f6a906c0SBarry Smith   PetscFunctionReturn(0);
2550f6a906c0SBarry Smith }
2551f6a906c0SBarry Smith 
2552277b19d0SLisandro Dalcin /*@
2553277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2554277b19d0SLisandro Dalcin 
2555277b19d0SLisandro Dalcin    Collective on TS
2556277b19d0SLisandro Dalcin 
2557277b19d0SLisandro Dalcin    Input Parameter:
2558277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2559277b19d0SLisandro Dalcin 
2560277b19d0SLisandro Dalcin    Level: beginner
2561277b19d0SLisandro Dalcin 
2562277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2563277b19d0SLisandro Dalcin 
2564277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2565277b19d0SLisandro Dalcin @*/
2566277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2567277b19d0SLisandro Dalcin {
2568277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2569277b19d0SLisandro Dalcin 
2570277b19d0SLisandro Dalcin   PetscFunctionBegin;
2571277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2572b18ea86cSHong Zhang 
2573277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2574277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2575277b19d0SLisandro Dalcin   }
2576277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2577e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2578bbd56ea5SKarl Rupp 
25794e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
25804e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2581214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
25826bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2583efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2584e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2585e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
258638637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2587bbd56ea5SKarl Rupp 
2588947abb85SHong Zhang  if (ts->vec_costintegral) {
2589d8151eeaSBarry Smith     ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2590d8151eeaSBarry Smith     if (ts->vecs_drdp){
2591d8151eeaSBarry Smith       ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2592d8151eeaSBarry Smith     }
2593947abb85SHong Zhang   }
2594d8151eeaSBarry Smith   ts->vecs_sensi  = NULL;
2595d8151eeaSBarry Smith   ts->vecs_sensip = NULL;
2596ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
25972c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
259836eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2599277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2600d763cef2SBarry Smith   PetscFunctionReturn(0);
2601d763cef2SBarry Smith }
2602d763cef2SBarry Smith 
2603d8e5e3e6SSatish Balay /*@
2604d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2605d763cef2SBarry Smith    with TSCreate().
2606d763cef2SBarry Smith 
2607d763cef2SBarry Smith    Collective on TS
2608d763cef2SBarry Smith 
2609d763cef2SBarry Smith    Input Parameter:
2610d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2611d763cef2SBarry Smith 
2612d763cef2SBarry Smith    Level: beginner
2613d763cef2SBarry Smith 
2614d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2615d763cef2SBarry Smith 
2616d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2617d763cef2SBarry Smith @*/
26186bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2619d763cef2SBarry Smith {
26206849ba73SBarry Smith   PetscErrorCode ierr;
2621d763cef2SBarry Smith 
2622d763cef2SBarry Smith   PetscFunctionBegin;
26236bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
26246bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
26256bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2626d763cef2SBarry Smith 
26276bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2628277b19d0SLisandro Dalcin 
2629e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2630e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
26316bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
26326d4c513bSLisandro Dalcin 
2633bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2634bc952696SBarry Smith 
263584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
26366427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
26376427ac75SLisandro Dalcin 
26386bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
26396bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
26406bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
26410dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
26426d4c513bSLisandro Dalcin 
2643a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2644d763cef2SBarry Smith   PetscFunctionReturn(0);
2645d763cef2SBarry Smith }
2646d763cef2SBarry Smith 
2647d8e5e3e6SSatish Balay /*@
2648d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2649d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2650d763cef2SBarry Smith 
2651d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2652d763cef2SBarry Smith 
2653d763cef2SBarry Smith    Input Parameter:
2654d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2655d763cef2SBarry Smith 
2656d763cef2SBarry Smith    Output Parameter:
2657d763cef2SBarry Smith .  snes - the nonlinear solver context
2658d763cef2SBarry Smith 
2659d763cef2SBarry Smith    Notes:
2660d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2661d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
266294b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2663d763cef2SBarry Smith 
2664d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
26650298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2666d763cef2SBarry Smith 
2667d763cef2SBarry Smith    Level: beginner
2668d763cef2SBarry Smith 
2669d763cef2SBarry Smith .keywords: timestep, get, SNES
2670d763cef2SBarry Smith @*/
26717087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2672d763cef2SBarry Smith {
2673d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2674d372ba47SLisandro Dalcin 
2675d763cef2SBarry Smith   PetscFunctionBegin;
26760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
26774482741eSBarry Smith   PetscValidPointer(snes,2);
2678d372ba47SLisandro Dalcin   if (!ts->snes) {
2679ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
26800298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
26813bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2682d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2683496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
26849e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
26859e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
26869e2a6581SJed Brown     }
2687d372ba47SLisandro Dalcin   }
2688d763cef2SBarry Smith   *snes = ts->snes;
2689d763cef2SBarry Smith   PetscFunctionReturn(0);
2690d763cef2SBarry Smith }
2691d763cef2SBarry Smith 
2692deb2cd25SJed Brown /*@
2693deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2694deb2cd25SJed Brown 
2695deb2cd25SJed Brown    Collective
2696deb2cd25SJed Brown 
2697deb2cd25SJed Brown    Input Parameter:
2698deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2699deb2cd25SJed Brown -  snes - the nonlinear solver context
2700deb2cd25SJed Brown 
2701deb2cd25SJed Brown    Notes:
2702deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2703deb2cd25SJed Brown 
2704deb2cd25SJed Brown    Level: developer
2705deb2cd25SJed Brown 
2706deb2cd25SJed Brown .keywords: timestep, set, SNES
2707deb2cd25SJed Brown @*/
2708deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2709deb2cd25SJed Brown {
2710deb2cd25SJed Brown   PetscErrorCode ierr;
2711d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2712deb2cd25SJed Brown 
2713deb2cd25SJed Brown   PetscFunctionBegin;
2714deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2715deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2716deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2717deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2718bbd56ea5SKarl Rupp 
2719deb2cd25SJed Brown   ts->snes = snes;
2720bbd56ea5SKarl Rupp 
27210298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27220298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2723740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
27240298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2725740132f1SEmil Constantinescu   }
2726deb2cd25SJed Brown   PetscFunctionReturn(0);
2727deb2cd25SJed Brown }
2728deb2cd25SJed Brown 
2729d8e5e3e6SSatish Balay /*@
273094b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2731d763cef2SBarry Smith    a TS (timestepper) context.
2732d763cef2SBarry Smith 
273394b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2734d763cef2SBarry Smith 
2735d763cef2SBarry Smith    Input Parameter:
2736d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2737d763cef2SBarry Smith 
2738d763cef2SBarry Smith    Output Parameter:
273994b7f48cSBarry Smith .  ksp - the nonlinear solver context
2740d763cef2SBarry Smith 
2741d763cef2SBarry Smith    Notes:
274294b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2743d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2744d763cef2SBarry Smith    KSP and PC contexts as well.
2745d763cef2SBarry Smith 
274694b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
27470298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2748d763cef2SBarry Smith 
2749d763cef2SBarry Smith    Level: beginner
2750d763cef2SBarry Smith 
275194b7f48cSBarry Smith .keywords: timestep, get, KSP
2752d763cef2SBarry Smith @*/
27537087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2754d763cef2SBarry Smith {
2755d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2756089b2837SJed Brown   SNES           snes;
2757d372ba47SLisandro Dalcin 
2758d763cef2SBarry Smith   PetscFunctionBegin;
27590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27604482741eSBarry Smith   PetscValidPointer(ksp,2);
276117186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2762e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2763089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2764089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2765d763cef2SBarry Smith   PetscFunctionReturn(0);
2766d763cef2SBarry Smith }
2767d763cef2SBarry Smith 
2768d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2769d763cef2SBarry Smith 
2770adb62b0dSMatthew Knepley /*@
2771adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2772adb62b0dSMatthew Knepley    maximum time for iteration.
2773adb62b0dSMatthew Knepley 
27743f9fe445SBarry Smith    Not Collective
2775adb62b0dSMatthew Knepley 
2776adb62b0dSMatthew Knepley    Input Parameters:
2777adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
27780298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
27790298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2780adb62b0dSMatthew Knepley 
2781adb62b0dSMatthew Knepley    Level: intermediate
2782adb62b0dSMatthew Knepley 
2783adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2784adb62b0dSMatthew Knepley @*/
27857087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2786adb62b0dSMatthew Knepley {
2787adb62b0dSMatthew Knepley   PetscFunctionBegin;
27880700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2789abc0a331SBarry Smith   if (maxsteps) {
27904482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2791adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2792adb62b0dSMatthew Knepley   }
2793abc0a331SBarry Smith   if (maxtime) {
27944482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2795adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2796adb62b0dSMatthew Knepley   }
2797adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2798adb62b0dSMatthew Knepley }
2799adb62b0dSMatthew Knepley 
2800d763cef2SBarry Smith /*@
2801d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2802d763cef2SBarry Smith    maximum time for iteration.
2803d763cef2SBarry Smith 
28043f9fe445SBarry Smith    Logically Collective on TS
2805d763cef2SBarry Smith 
2806d763cef2SBarry Smith    Input Parameters:
2807d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2808d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2809d763cef2SBarry Smith -  maxtime - final time to iterate to
2810d763cef2SBarry Smith 
2811d763cef2SBarry Smith    Options Database Keys:
2812d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
28133bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2814d763cef2SBarry Smith 
2815d763cef2SBarry Smith    Notes:
2816d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2817d763cef2SBarry Smith 
2818d763cef2SBarry Smith    Level: intermediate
2819d763cef2SBarry Smith 
2820d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2821a43b19c4SJed Brown 
2822a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2823d763cef2SBarry Smith @*/
28247087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2825d763cef2SBarry Smith {
2826d763cef2SBarry Smith   PetscFunctionBegin;
28270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2828c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2829c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
283039b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
283139b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2832d763cef2SBarry Smith   PetscFunctionReturn(0);
2833d763cef2SBarry Smith }
2834d763cef2SBarry Smith 
2835d763cef2SBarry Smith /*@
2836d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2837d763cef2SBarry Smith    for use by the TS routines.
2838d763cef2SBarry Smith 
28393f9fe445SBarry Smith    Logically Collective on TS and Vec
2840d763cef2SBarry Smith 
2841d763cef2SBarry Smith    Input Parameters:
2842d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
28430910c330SBarry Smith -  u - the solution vector
2844d763cef2SBarry Smith 
2845d763cef2SBarry Smith    Level: beginner
2846d763cef2SBarry Smith 
2847d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2848d763cef2SBarry Smith @*/
28490910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2850d763cef2SBarry Smith {
28518737fe31SLisandro Dalcin   PetscErrorCode ierr;
2852496e6a7aSJed Brown   DM             dm;
28538737fe31SLisandro Dalcin 
2854d763cef2SBarry Smith   PetscFunctionBegin;
28550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28560910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
28570910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
28586bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
28590910c330SBarry Smith   ts->vec_sol = u;
2860bbd56ea5SKarl Rupp 
2861496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
28620910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2863d763cef2SBarry Smith   PetscFunctionReturn(0);
2864d763cef2SBarry Smith }
2865d763cef2SBarry Smith 
286673b18844SBarry Smith /*@
286773b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
286873b18844SBarry Smith 
286973b18844SBarry Smith    Logically Collective on TS
287073b18844SBarry Smith 
287173b18844SBarry Smith    Input Parameters:
287273b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
287373b18844SBarry Smith .  steps - number of steps to use
287473b18844SBarry Smith 
287573b18844SBarry Smith    Level: intermediate
287673b18844SBarry Smith 
287773b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
287873b18844SBarry Smith           so as to integrate back to less than the original timestep
287973b18844SBarry Smith 
288073b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
288173b18844SBarry Smith 
288273b18844SBarry Smith .seealso: TSSetExactFinalTime()
288373b18844SBarry Smith @*/
288473b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
288573b18844SBarry Smith {
288673b18844SBarry Smith   PetscFunctionBegin;
288773b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
288873b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
288973b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
289073b18844SBarry 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");
289173b18844SBarry Smith   ts->adjoint_max_steps = steps;
289273b18844SBarry Smith   PetscFunctionReturn(0);
289373b18844SBarry Smith }
289473b18844SBarry Smith 
2895c235aa8dSHong Zhang /*@
2896dfb21088SHong 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
28970cf82b59SBarry Smith       for use by the TSAdjoint routines.
2898c235aa8dSHong Zhang 
2899c235aa8dSHong Zhang    Logically Collective on TS and Vec
2900c235aa8dSHong Zhang 
2901c235aa8dSHong Zhang    Input Parameters:
2902c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2903abc2977eSBarry 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
2904abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
2905c235aa8dSHong Zhang 
2906c235aa8dSHong Zhang    Level: beginner
2907c235aa8dSHong Zhang 
2908abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
29090cf82b59SBarry Smith 
2910c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2911c235aa8dSHong Zhang @*/
2912dfb21088SHong Zhang PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
2913c235aa8dSHong Zhang {
2914c235aa8dSHong Zhang   PetscFunctionBegin;
2915c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2916abc2977eSBarry Smith   PetscValidPointer(lambda,2);
2917abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
2918abc2977eSBarry Smith   ts->vecs_sensip = mu;
2919dfb21088SHong 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");
2920abc2977eSBarry Smith   ts->numcost  = numcost;
2921ad8e2604SHong Zhang   PetscFunctionReturn(0);
2922ad8e2604SHong Zhang }
2923ad8e2604SHong Zhang 
2924ad8e2604SHong Zhang /*@C
29250cf82b59SBarry 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.
2926ad8e2604SHong Zhang 
2927ad8e2604SHong Zhang   Logically Collective on TS
2928ad8e2604SHong Zhang 
2929ad8e2604SHong Zhang   Input Parameters:
2930ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2931ad8e2604SHong Zhang - func - The function
2932ad8e2604SHong Zhang 
2933ad8e2604SHong Zhang   Calling sequence of func:
29340cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
293505755b9cSHong Zhang +   t - current timestep
29360cf82b59SBarry Smith .   y - input vector (current ODE solution)
293705755b9cSHong Zhang .   A - output matrix
293805755b9cSHong Zhang -   ctx - [optional] user-defined function context
2939ad8e2604SHong Zhang 
2940ad8e2604SHong Zhang   Level: intermediate
2941ad8e2604SHong Zhang 
29420cf82b59SBarry 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
29430cf82b59SBarry Smith 
2944ad8e2604SHong Zhang .keywords: TS, sensitivity
2945ad8e2604SHong Zhang .seealso:
2946ad8e2604SHong Zhang @*/
29475bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2948ad8e2604SHong Zhang {
2949ad8e2604SHong Zhang   PetscErrorCode ierr;
2950ad8e2604SHong Zhang 
2951ad8e2604SHong Zhang   PetscFunctionBegin;
2952ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2953ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
2954ad8e2604SHong Zhang 
2955ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
2956ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
2957ad8e2604SHong Zhang   if(Amat) {
2958ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
2959ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2960ad8e2604SHong Zhang     ts->Jacp = Amat;
2961ad8e2604SHong Zhang   }
2962ad8e2604SHong Zhang   PetscFunctionReturn(0);
2963ad8e2604SHong Zhang }
2964ad8e2604SHong Zhang 
29650cf82b59SBarry Smith /*@C
29665bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
2967ad8e2604SHong Zhang 
2968ad8e2604SHong Zhang   Collective on TS
2969ad8e2604SHong Zhang 
2970ad8e2604SHong Zhang   Input Parameters:
2971ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
2972ad8e2604SHong Zhang 
2973ad8e2604SHong Zhang   Level: developer
2974ad8e2604SHong Zhang 
2975ad8e2604SHong Zhang .keywords: TS, sensitivity
29765bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
2977ad8e2604SHong Zhang @*/
29785bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
2979ad8e2604SHong Zhang {
2980ad8e2604SHong Zhang   PetscErrorCode ierr;
2981ad8e2604SHong Zhang 
2982ad8e2604SHong Zhang   PetscFunctionBegin;
2983ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2984ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
2985ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
2986ad8e2604SHong Zhang 
2987ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
2988ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
2989ad8e2604SHong Zhang   PetscStackPop;
2990c235aa8dSHong Zhang   PetscFunctionReturn(0);
2991c235aa8dSHong Zhang }
2992c235aa8dSHong Zhang 
29936fd0887fSHong Zhang /*@C
2994dfb21088SHong Zhang     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
29956fd0887fSHong Zhang 
29966fd0887fSHong Zhang     Logically Collective on TS
29976fd0887fSHong Zhang 
29986fd0887fSHong Zhang     Input Parameters:
29996fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
3000abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
30010cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
3002b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
3003b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
3004feaa1ddbSSatish Balay .   fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
3005b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
30066fd0887fSHong Zhang 
30070cf82b59SBarry Smith     Calling sequence of rf:
30080cf82b59SBarry Smith $     rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx);
30096fd0887fSHong Zhang 
30106fd0887fSHong Zhang +   t - current timestep
30110cf82b59SBarry Smith .   y - input vector
30120cf82b59SBarry Smith .   f - function result; one vector entry for each cost function
30136fd0887fSHong Zhang -   ctx - [optional] user-defined function context
30146fd0887fSHong Zhang 
3015b612ec54SBarry Smith    Calling sequence of drdyf:
30160cf82b59SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
3017b612ec54SBarry Smith 
3018b612ec54SBarry Smith    Calling sequence of drdpf:
30190cf82b59SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
3020b612ec54SBarry Smith 
3021b612ec54SBarry Smith     Level: intermediate
30226fd0887fSHong Zhang 
3023abc2977eSBarry Smith     Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions
30240cf82b59SBarry Smith 
30256fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
30266fd0887fSHong Zhang 
3027dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
30286fd0887fSHong Zhang @*/
3029dfb21088SHong Zhang PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
3030d8151eeaSBarry Smith                                                           PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
3031b1cb36f3SHong Zhang                                                           PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),
3032b1cb36f3SHong Zhang                                                           PetscBool fwd,void *ctx)
30336fd0887fSHong Zhang {
30346fd0887fSHong Zhang   PetscErrorCode ierr;
30356fd0887fSHong Zhang 
30366fd0887fSHong Zhang   PetscFunctionBegin;
30376fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3038dfb21088SHong 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()");
3039c72ed514SHong Zhang   if (!ts->numcost) ts->numcost=numcost;
30406fd0887fSHong Zhang 
3041b1cb36f3SHong Zhang   ts->costintegralfwd  = fwd; /* Evaluate the cost integral in forward run if fwd is true */
3042abc2977eSBarry Smith   ierr                 = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
30432c39e106SBarry Smith   ierr                 = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
30440cf82b59SBarry Smith   ts->costintegrand    = rf;
304505755b9cSHong Zhang   ts->costintegrandctx = ctx;
3046b612ec54SBarry Smith   ts->drdyfunction     = drdyf;
3047b612ec54SBarry Smith   ts->drdpfunction     = drdpf;
30486fd0887fSHong Zhang   PetscFunctionReturn(0);
30496fd0887fSHong Zhang }
30506fd0887fSHong Zhang 
305136eaed60SHong Zhang /*@
3052dfb21088SHong Zhang    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
305336eaed60SHong Zhang    It is valid to call the routine after a backward run.
305436eaed60SHong Zhang 
305536eaed60SHong Zhang    Not Collective
305636eaed60SHong Zhang 
305736eaed60SHong Zhang    Input Parameter:
305836eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
305936eaed60SHong Zhang 
306036eaed60SHong Zhang    Output Parameter:
30612c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
306236eaed60SHong Zhang 
306336eaed60SHong Zhang    Level: intermediate
306436eaed60SHong Zhang 
3065dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
306636eaed60SHong Zhang 
306736eaed60SHong Zhang .keywords: TS, sensitivity analysis
306836eaed60SHong Zhang @*/
3069dfb21088SHong Zhang PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
307036eaed60SHong Zhang {
307136eaed60SHong Zhang   PetscFunctionBegin;
307236eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
307336eaed60SHong Zhang   PetscValidPointer(v,2);
30742c39e106SBarry Smith   *v = ts->vec_costintegral;
307536eaed60SHong Zhang   PetscFunctionReturn(0);
307636eaed60SHong Zhang }
307736eaed60SHong Zhang 
30786fd0887fSHong Zhang /*@
30792c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
30806fd0887fSHong Zhang 
30816fd0887fSHong Zhang    Input Parameters:
30826fd0887fSHong Zhang +  ts - the TS context
30836fd0887fSHong Zhang .  t - current time
30840cf82b59SBarry Smith -  y - state vector, i.e. current solution
30856fd0887fSHong Zhang 
30866fd0887fSHong Zhang    Output Parameter:
3087abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
30886fd0887fSHong Zhang 
30896fd0887fSHong Zhang    Note:
30906fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
30916fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
30926fd0887fSHong Zhang 
30936fd0887fSHong Zhang    Level: developer
30946fd0887fSHong Zhang 
30956fd0887fSHong Zhang .keywords: TS, compute
30966fd0887fSHong Zhang 
3097dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
30986fd0887fSHong Zhang @*/
30990cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
31006fd0887fSHong Zhang {
31016fd0887fSHong Zhang   PetscErrorCode ierr;
31026fd0887fSHong Zhang 
31036fd0887fSHong Zhang   PetscFunctionBegin;
31046fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31050cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
31066fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
31076fd0887fSHong Zhang 
31080cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
3109e4680132SHong Zhang   if (ts->costintegrand) {
3110e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
31110cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
31126fd0887fSHong Zhang     PetscStackPop;
31136fd0887fSHong Zhang   } else {
31146fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
31156fd0887fSHong Zhang   }
31166fd0887fSHong Zhang 
31170cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
31186fd0887fSHong Zhang   PetscFunctionReturn(0);
31196fd0887fSHong Zhang }
31206fd0887fSHong Zhang 
312105755b9cSHong Zhang /*@
3122d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
312305755b9cSHong Zhang 
312405755b9cSHong Zhang   Collective on TS
312505755b9cSHong Zhang 
312605755b9cSHong Zhang   Input Parameters:
312705755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
312805755b9cSHong Zhang 
312905755b9cSHong Zhang   Notes:
3130d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
313105755b9cSHong Zhang   so most users would not generally call this routine themselves.
313205755b9cSHong Zhang 
313305755b9cSHong Zhang   Level: developer
313405755b9cSHong Zhang 
313505755b9cSHong Zhang .keywords: TS, sensitivity
3136d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
313705755b9cSHong Zhang @*/
31380cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
313905755b9cSHong Zhang {
314005755b9cSHong Zhang   PetscErrorCode ierr;
314105755b9cSHong Zhang 
314205755b9cSHong Zhang   PetscFunctionBegin;
314305755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31440cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
314505755b9cSHong Zhang 
314605755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
31470cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
314805755b9cSHong Zhang   PetscStackPop;
314905755b9cSHong Zhang   PetscFunctionReturn(0);
315005755b9cSHong Zhang }
315105755b9cSHong Zhang 
315205755b9cSHong Zhang /*@
3153d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
315405755b9cSHong Zhang 
315505755b9cSHong Zhang   Collective on TS
315605755b9cSHong Zhang 
315705755b9cSHong Zhang   Input Parameters:
315805755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
315905755b9cSHong Zhang 
316005755b9cSHong Zhang   Notes:
316105755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
316205755b9cSHong Zhang   so most users would not generally call this routine themselves.
316305755b9cSHong Zhang 
316405755b9cSHong Zhang   Level: developer
316505755b9cSHong Zhang 
316605755b9cSHong Zhang .keywords: TS, sensitivity
3167d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
316805755b9cSHong Zhang @*/
31690cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
317005755b9cSHong Zhang {
317105755b9cSHong Zhang   PetscErrorCode ierr;
317205755b9cSHong Zhang 
317305755b9cSHong Zhang   PetscFunctionBegin;
317405755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31750cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
317605755b9cSHong Zhang 
317705755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
31780cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
317905755b9cSHong Zhang   PetscStackPop;
318005755b9cSHong Zhang   PetscFunctionReturn(0);
318105755b9cSHong Zhang }
318205755b9cSHong Zhang 
3183ac226902SBarry Smith /*@C
3184000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
31853f2090d5SJed Brown   called once at the beginning of each time step.
3186000e7ae3SMatthew Knepley 
31873f9fe445SBarry Smith   Logically Collective on TS
3188000e7ae3SMatthew Knepley 
3189000e7ae3SMatthew Knepley   Input Parameters:
3190000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3191000e7ae3SMatthew Knepley - func - The function
3192000e7ae3SMatthew Knepley 
3193000e7ae3SMatthew Knepley   Calling sequence of func:
3194000e7ae3SMatthew Knepley . func (TS ts);
3195000e7ae3SMatthew Knepley 
3196000e7ae3SMatthew Knepley   Level: intermediate
3197000e7ae3SMatthew Knepley 
3198000e7ae3SMatthew Knepley .keywords: TS, timestep
31999be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
3200000e7ae3SMatthew Knepley @*/
32017087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3202000e7ae3SMatthew Knepley {
3203000e7ae3SMatthew Knepley   PetscFunctionBegin;
32040700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3205ae60f76fSBarry Smith   ts->prestep = func;
3206000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3207000e7ae3SMatthew Knepley }
3208000e7ae3SMatthew Knepley 
320909ee8438SJed Brown /*@
32103f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
32113f2090d5SJed Brown 
32123f2090d5SJed Brown   Collective on TS
32133f2090d5SJed Brown 
32143f2090d5SJed Brown   Input Parameters:
32153f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32163f2090d5SJed Brown 
32173f2090d5SJed Brown   Notes:
32183f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32193f2090d5SJed Brown   so most users would not generally call this routine themselves.
32203f2090d5SJed Brown 
32213f2090d5SJed Brown   Level: developer
32223f2090d5SJed Brown 
32233f2090d5SJed Brown .keywords: TS, timestep
32249be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32253f2090d5SJed Brown @*/
32267087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32273f2090d5SJed Brown {
32283f2090d5SJed Brown   PetscErrorCode ierr;
32293f2090d5SJed Brown 
32303f2090d5SJed Brown   PetscFunctionBegin;
32310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3232ae60f76fSBarry Smith   if (ts->prestep) {
3233ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
3234312ce896SJed Brown   }
32353f2090d5SJed Brown   PetscFunctionReturn(0);
32363f2090d5SJed Brown }
32373f2090d5SJed Brown 
3238b8123daeSJed Brown /*@C
3239b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3240b8123daeSJed Brown   called once at the beginning of each stage.
3241b8123daeSJed Brown 
3242b8123daeSJed Brown   Logically Collective on TS
3243b8123daeSJed Brown 
3244b8123daeSJed Brown   Input Parameters:
3245b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3246b8123daeSJed Brown - func - The function
3247b8123daeSJed Brown 
3248b8123daeSJed Brown   Calling sequence of func:
3249b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3250b8123daeSJed Brown 
3251b8123daeSJed Brown   Level: intermediate
3252b8123daeSJed Brown 
3253b8123daeSJed Brown   Note:
3254b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
3255b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
3256b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3257b8123daeSJed Brown 
3258b8123daeSJed Brown .keywords: TS, timestep
32599be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3260b8123daeSJed Brown @*/
3261b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3262b8123daeSJed Brown {
3263b8123daeSJed Brown   PetscFunctionBegin;
3264b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3265ae60f76fSBarry Smith   ts->prestage = func;
3266b8123daeSJed Brown   PetscFunctionReturn(0);
3267b8123daeSJed Brown }
3268b8123daeSJed Brown 
32699be3e283SDebojyoti Ghosh /*@C
32709be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
32719be3e283SDebojyoti Ghosh   called once at the end of each stage.
32729be3e283SDebojyoti Ghosh 
32739be3e283SDebojyoti Ghosh   Logically Collective on TS
32749be3e283SDebojyoti Ghosh 
32759be3e283SDebojyoti Ghosh   Input Parameters:
32769be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
32779be3e283SDebojyoti Ghosh - func - The function
32789be3e283SDebojyoti Ghosh 
32799be3e283SDebojyoti Ghosh   Calling sequence of func:
32809be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
32819be3e283SDebojyoti Ghosh 
32829be3e283SDebojyoti Ghosh   Level: intermediate
32839be3e283SDebojyoti Ghosh 
32849be3e283SDebojyoti Ghosh   Note:
32859be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
32869be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
32879be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
32889be3e283SDebojyoti Ghosh 
32899be3e283SDebojyoti Ghosh .keywords: TS, timestep
32909be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
32919be3e283SDebojyoti Ghosh @*/
32929be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
32939be3e283SDebojyoti Ghosh {
32949be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32959be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
32969be3e283SDebojyoti Ghosh   ts->poststage = func;
32979be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32989be3e283SDebojyoti Ghosh }
32999be3e283SDebojyoti Ghosh 
3300c688d042SShri Abhyankar /*@C
3301c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3302c688d042SShri Abhyankar   called once at the end of each step evaluation.
3303c688d042SShri Abhyankar 
3304c688d042SShri Abhyankar   Logically Collective on TS
3305c688d042SShri Abhyankar 
3306c688d042SShri Abhyankar   Input Parameters:
3307c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3308c688d042SShri Abhyankar - func - The function
3309c688d042SShri Abhyankar 
3310c688d042SShri Abhyankar   Calling sequence of func:
3311c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3312c688d042SShri Abhyankar 
3313c688d042SShri Abhyankar   Level: intermediate
3314c688d042SShri Abhyankar 
3315c688d042SShri Abhyankar   Note:
33161785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33171785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3318e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3319e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3320e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3321c688d042SShri Abhyankar 
3322c688d042SShri Abhyankar .keywords: TS, timestep
3323c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3324c688d042SShri Abhyankar @*/
3325c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3326c688d042SShri Abhyankar {
3327c688d042SShri Abhyankar   PetscFunctionBegin;
3328c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3329c688d042SShri Abhyankar   ts->postevaluate = func;
3330c688d042SShri Abhyankar   PetscFunctionReturn(0);
3331c688d042SShri Abhyankar }
3332c688d042SShri Abhyankar 
3333b8123daeSJed Brown /*@
3334b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3335b8123daeSJed Brown 
3336b8123daeSJed Brown   Collective on TS
3337b8123daeSJed Brown 
3338b8123daeSJed Brown   Input Parameters:
3339b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33409be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3341b8123daeSJed Brown 
3342b8123daeSJed Brown   Notes:
3343b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3344b8123daeSJed Brown   most users would not generally call this routine themselves.
3345b8123daeSJed Brown 
3346b8123daeSJed Brown   Level: developer
3347b8123daeSJed Brown 
3348b8123daeSJed Brown .keywords: TS, timestep
33499be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3350b8123daeSJed Brown @*/
3351b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3352b8123daeSJed Brown {
3353b8123daeSJed Brown   PetscErrorCode ierr;
3354b8123daeSJed Brown 
3355b8123daeSJed Brown   PetscFunctionBegin;
3356b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3357ae60f76fSBarry Smith   if (ts->prestage) {
3358ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3359b8123daeSJed Brown   }
3360b8123daeSJed Brown   PetscFunctionReturn(0);
3361b8123daeSJed Brown }
3362b8123daeSJed Brown 
33639be3e283SDebojyoti Ghosh /*@
33649be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
33659be3e283SDebojyoti Ghosh 
33669be3e283SDebojyoti Ghosh   Collective on TS
33679be3e283SDebojyoti Ghosh 
33689be3e283SDebojyoti Ghosh   Input Parameters:
33699be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
33709be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
33719be3e283SDebojyoti Ghosh   stageindex  - Stage number
33729be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
33739be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
33749be3e283SDebojyoti Ghosh 
33759be3e283SDebojyoti Ghosh   Notes:
33769be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
33779be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
33789be3e283SDebojyoti Ghosh 
33799be3e283SDebojyoti Ghosh   Level: developer
33809be3e283SDebojyoti Ghosh 
33819be3e283SDebojyoti Ghosh .keywords: TS, timestep
33829be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
33839be3e283SDebojyoti Ghosh @*/
33849be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
33859be3e283SDebojyoti Ghosh {
33869be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
33879be3e283SDebojyoti Ghosh 
33889be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33899be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33904beae5d8SLisandro Dalcin   if (ts->poststage) {
33919be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
33929be3e283SDebojyoti Ghosh   }
33939be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33949be3e283SDebojyoti Ghosh }
33959be3e283SDebojyoti Ghosh 
3396c688d042SShri Abhyankar /*@
3397c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3398c688d042SShri Abhyankar 
3399c688d042SShri Abhyankar   Collective on TS
3400c688d042SShri Abhyankar 
3401c688d042SShri Abhyankar   Input Parameters:
3402c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3403c688d042SShri Abhyankar 
3404c688d042SShri Abhyankar   Notes:
3405c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3406c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3407c688d042SShri Abhyankar 
3408c688d042SShri Abhyankar   Level: developer
3409c688d042SShri Abhyankar 
3410c688d042SShri Abhyankar .keywords: TS, timestep
3411c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3412c688d042SShri Abhyankar @*/
3413c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3414c688d042SShri Abhyankar {
3415c688d042SShri Abhyankar   PetscErrorCode ierr;
3416c688d042SShri Abhyankar 
3417c688d042SShri Abhyankar   PetscFunctionBegin;
3418c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3419c688d042SShri Abhyankar   if (ts->postevaluate) {
3420c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3421c688d042SShri Abhyankar   }
3422c688d042SShri Abhyankar   PetscFunctionReturn(0);
3423c688d042SShri Abhyankar }
3424c688d042SShri Abhyankar 
3425ac226902SBarry Smith /*@C
3426000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34273f2090d5SJed Brown   called once at the end of each time step.
3428000e7ae3SMatthew Knepley 
34293f9fe445SBarry Smith   Logically Collective on TS
3430000e7ae3SMatthew Knepley 
3431000e7ae3SMatthew Knepley   Input Parameters:
3432000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3433000e7ae3SMatthew Knepley - func - The function
3434000e7ae3SMatthew Knepley 
3435000e7ae3SMatthew Knepley   Calling sequence of func:
3436b8123daeSJed Brown $ func (TS ts);
3437000e7ae3SMatthew Knepley 
34381785ff2aSShri Abhyankar   Notes:
34391785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34401785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34411785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34421785ff2aSShri Abhyankar 
3443000e7ae3SMatthew Knepley   Level: intermediate
3444000e7ae3SMatthew Knepley 
3445000e7ae3SMatthew Knepley .keywords: TS, timestep
34461785ff2aSShri Abhyankar .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
3447000e7ae3SMatthew Knepley @*/
34487087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3449000e7ae3SMatthew Knepley {
3450000e7ae3SMatthew Knepley   PetscFunctionBegin;
34510700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3452ae60f76fSBarry Smith   ts->poststep = func;
3453000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3454000e7ae3SMatthew Knepley }
3455000e7ae3SMatthew Knepley 
345609ee8438SJed Brown /*@
34573f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34583f2090d5SJed Brown 
34593f2090d5SJed Brown   Collective on TS
34603f2090d5SJed Brown 
34613f2090d5SJed Brown   Input Parameters:
34623f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
34633f2090d5SJed Brown 
34643f2090d5SJed Brown   Notes:
34653f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
34663f2090d5SJed Brown   so most users would not generally call this routine themselves.
34673f2090d5SJed Brown 
34683f2090d5SJed Brown   Level: developer
34693f2090d5SJed Brown 
34703f2090d5SJed Brown .keywords: TS, timestep
34713f2090d5SJed Brown @*/
34727087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
34733f2090d5SJed Brown {
34743f2090d5SJed Brown   PetscErrorCode ierr;
34753f2090d5SJed Brown 
34763f2090d5SJed Brown   PetscFunctionBegin;
34770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3478ae60f76fSBarry Smith   if (ts->poststep) {
3479ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
348072ac3e02SJed Brown   }
34813f2090d5SJed Brown   PetscFunctionReturn(0);
34823f2090d5SJed Brown }
34833f2090d5SJed Brown 
3484d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3485d763cef2SBarry Smith 
3486d763cef2SBarry Smith /*@C
3487a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3488d763cef2SBarry Smith    timestep to display the iteration's  progress.
3489d763cef2SBarry Smith 
34903f9fe445SBarry Smith    Logically Collective on TS
3491d763cef2SBarry Smith 
3492d763cef2SBarry Smith    Input Parameters:
3493d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3494e213d8f1SJed Brown .  monitor - monitoring routine
3495329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
34960298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3497b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
34980298fd71SBarry Smith           (may be NULL)
3499d763cef2SBarry Smith 
3500e213d8f1SJed Brown    Calling sequence of monitor:
35010910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3502d763cef2SBarry Smith 
3503d763cef2SBarry Smith +    ts - the TS context
350463e21af5SBarry Smith .    steps - iteration number (after the final time step the monitor routine may be called with a step of -1, this indicates the solution has been interpolated to this time)
35051f06c33eSBarry Smith .    time - current time
35060910c330SBarry Smith .    u - current iterate
3507d763cef2SBarry Smith -    mctx - [optional] monitoring context
3508d763cef2SBarry Smith 
3509d763cef2SBarry Smith    Notes:
3510d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3511d763cef2SBarry Smith    already has been loaded.
3512d763cef2SBarry Smith 
3513025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
3514025f1a04SBarry Smith 
3515d763cef2SBarry Smith    Level: intermediate
3516d763cef2SBarry Smith 
3517d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3518d763cef2SBarry Smith 
3519a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3520d763cef2SBarry Smith @*/
3521c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3522d763cef2SBarry Smith {
352378064530SBarry Smith   PetscErrorCode ierr;
352478064530SBarry Smith   PetscInt       i;
352578064530SBarry Smith   PetscBool      identical;
352678064530SBarry Smith 
3527d763cef2SBarry Smith   PetscFunctionBegin;
35280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
352978064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
353078064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
353178064530SBarry Smith     if (identical) PetscFunctionReturn(0);
353278064530SBarry Smith   }
353317186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3534d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
35358704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3536d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3537d763cef2SBarry Smith   PetscFunctionReturn(0);
3538d763cef2SBarry Smith }
3539d763cef2SBarry Smith 
3540d763cef2SBarry Smith /*@C
3541a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3542d763cef2SBarry Smith 
35433f9fe445SBarry Smith    Logically Collective on TS
3544d763cef2SBarry Smith 
3545d763cef2SBarry Smith    Input Parameters:
3546d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3547d763cef2SBarry Smith 
3548d763cef2SBarry Smith    Notes:
3549d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3550d763cef2SBarry Smith 
3551d763cef2SBarry Smith    Level: intermediate
3552d763cef2SBarry Smith 
3553d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3554d763cef2SBarry Smith 
3555a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3556d763cef2SBarry Smith @*/
35577087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3558d763cef2SBarry Smith {
3559d952e501SBarry Smith   PetscErrorCode ierr;
3560d952e501SBarry Smith   PetscInt       i;
3561d952e501SBarry Smith 
3562d763cef2SBarry Smith   PetscFunctionBegin;
35630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3564d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
35658704b422SBarry Smith     if (ts->monitordestroy[i]) {
35668704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3567d952e501SBarry Smith     }
3568d952e501SBarry Smith   }
3569d763cef2SBarry Smith   ts->numbermonitors = 0;
3570d763cef2SBarry Smith   PetscFunctionReturn(0);
3571d763cef2SBarry Smith }
3572d763cef2SBarry Smith 
3573721cd6eeSBarry Smith /*@C
3574721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
35755516499fSSatish Balay 
35765516499fSSatish Balay    Level: intermediate
357741251cbbSSatish Balay 
35785516499fSSatish Balay .keywords: TS, set, monitor
35795516499fSSatish Balay 
358063e21af5SBarry Smith .seealso:  TSMonitorSet()
358141251cbbSSatish Balay @*/
3582721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3583d763cef2SBarry Smith {
3584dfbe8321SBarry Smith   PetscErrorCode ierr;
3585721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
358641aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3587d132466eSBarry Smith 
3588d763cef2SBarry Smith   PetscFunctionBegin;
35894d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
359041aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
359141aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3592721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
359341aca3d6SBarry Smith   if (iascii) {
3594649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
359563e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
359663e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
359763e21af5SBarry Smith     } else {
35988392e04aSShri 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);
359963e21af5SBarry Smith     }
3600649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
360141aca3d6SBarry Smith   } else if (ibinary) {
360241aca3d6SBarry Smith     PetscMPIInt rank;
360341aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
360441aca3d6SBarry Smith     if (!rank) {
3605450a797fSBarry Smith       PetscBool skipHeader;
3606450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3607450a797fSBarry Smith 
3608450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3609450a797fSBarry Smith       if (!skipHeader) {
3610450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3611450a797fSBarry Smith        }
361241aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
361341aca3d6SBarry Smith     } else {
361441aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
361541aca3d6SBarry Smith     }
361641aca3d6SBarry Smith   }
3617721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3618d763cef2SBarry Smith   PetscFunctionReturn(0);
3619d763cef2SBarry Smith }
3620d763cef2SBarry Smith 
36219110b2e7SHong Zhang /*@C
36229110b2e7SHong Zhang    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
36239110b2e7SHong Zhang    timestep to display the iteration's  progress.
36249110b2e7SHong Zhang 
36259110b2e7SHong Zhang    Logically Collective on TS
36269110b2e7SHong Zhang 
36279110b2e7SHong Zhang    Input Parameters:
36289110b2e7SHong Zhang +  ts - the TS context obtained from TSCreate()
36299110b2e7SHong Zhang .  adjointmonitor - monitoring routine
36309110b2e7SHong Zhang .  adjointmctx - [optional] user-defined context for private data for the
36319110b2e7SHong Zhang              monitor routine (use NULL if no context is desired)
36329110b2e7SHong Zhang -  adjointmonitordestroy - [optional] routine that frees monitor context
36339110b2e7SHong Zhang           (may be NULL)
36349110b2e7SHong Zhang 
36359110b2e7SHong Zhang    Calling sequence of monitor:
36369110b2e7SHong Zhang $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)
36379110b2e7SHong Zhang 
36389110b2e7SHong Zhang +    ts - the TS context
36399110b2e7SHong 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
36409110b2e7SHong Zhang                                been interpolated to)
36419110b2e7SHong Zhang .    time - current time
36429110b2e7SHong Zhang .    u - current iterate
36439110b2e7SHong Zhang .    numcost - number of cost functionos
36449110b2e7SHong Zhang .    lambda - sensitivities to initial conditions
36459110b2e7SHong Zhang .    mu - sensitivities to parameters
36469110b2e7SHong Zhang -    adjointmctx - [optional] adjoint monitoring context
36479110b2e7SHong Zhang 
36489110b2e7SHong Zhang    Notes:
36499110b2e7SHong Zhang    This routine adds an additional monitor to the list of monitors that
36509110b2e7SHong Zhang    already has been loaded.
36519110b2e7SHong Zhang 
36529110b2e7SHong Zhang    Fortran notes: Only a single monitor function can be set for each TS object
36539110b2e7SHong Zhang 
36549110b2e7SHong Zhang    Level: intermediate
36559110b2e7SHong Zhang 
36569110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
36579110b2e7SHong Zhang 
36589110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel()
36599110b2e7SHong Zhang @*/
36609110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
36619110b2e7SHong Zhang {
366278064530SBarry Smith   PetscErrorCode ierr;
366378064530SBarry Smith   PetscInt       i;
366478064530SBarry Smith   PetscBool      identical;
366578064530SBarry Smith 
36669110b2e7SHong Zhang   PetscFunctionBegin;
36679110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
366878064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
366978064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr);
367078064530SBarry Smith     if (identical) PetscFunctionReturn(0);
367178064530SBarry Smith   }
36729110b2e7SHong Zhang   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
36739110b2e7SHong Zhang   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
36749110b2e7SHong Zhang   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
36759110b2e7SHong Zhang   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
36769110b2e7SHong Zhang   PetscFunctionReturn(0);
36779110b2e7SHong Zhang }
36789110b2e7SHong Zhang 
36799110b2e7SHong Zhang /*@C
36809110b2e7SHong Zhang    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.
36819110b2e7SHong Zhang 
36829110b2e7SHong Zhang    Logically Collective on TS
36839110b2e7SHong Zhang 
36849110b2e7SHong Zhang    Input Parameters:
36859110b2e7SHong Zhang .  ts - the TS context obtained from TSCreate()
36869110b2e7SHong Zhang 
36879110b2e7SHong Zhang    Notes:
36889110b2e7SHong Zhang    There is no way to remove a single, specific monitor.
36899110b2e7SHong Zhang 
36909110b2e7SHong Zhang    Level: intermediate
36919110b2e7SHong Zhang 
36929110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
36939110b2e7SHong Zhang 
36949110b2e7SHong Zhang .seealso: TSAdjointMonitorSet()
36959110b2e7SHong Zhang @*/
36969110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorCancel(TS ts)
36979110b2e7SHong Zhang {
36989110b2e7SHong Zhang   PetscErrorCode ierr;
36999110b2e7SHong Zhang   PetscInt       i;
37009110b2e7SHong Zhang 
37019110b2e7SHong Zhang   PetscFunctionBegin;
37029110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37039110b2e7SHong Zhang   for (i=0; i<ts->numberadjointmonitors; i++) {
37049110b2e7SHong Zhang     if (ts->adjointmonitordestroy[i]) {
37059110b2e7SHong Zhang       ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
37069110b2e7SHong Zhang     }
37079110b2e7SHong Zhang   }
37089110b2e7SHong Zhang   ts->numberadjointmonitors = 0;
37099110b2e7SHong Zhang   PetscFunctionReturn(0);
37109110b2e7SHong Zhang }
37119110b2e7SHong Zhang 
3712721cd6eeSBarry Smith /*@C
3713721cd6eeSBarry Smith    TSAdjointMonitorDefault - the default monitor of adjoint computations
3714110eb670SHong Zhang 
3715110eb670SHong Zhang    Level: intermediate
3716110eb670SHong Zhang 
3717110eb670SHong Zhang .keywords: TS, set, monitor
3718110eb670SHong Zhang 
3719110eb670SHong Zhang .seealso: TSAdjointMonitorSet()
3720110eb670SHong Zhang @*/
3721721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
3722110eb670SHong Zhang {
3723110eb670SHong Zhang   PetscErrorCode ierr;
3724721cd6eeSBarry Smith   PetscViewer    viewer = vf->viewer;
3725110eb670SHong Zhang 
3726110eb670SHong Zhang   PetscFunctionBegin;
3727110eb670SHong Zhang   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3728721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3729110eb670SHong Zhang   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3730110eb670SHong 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);
3731110eb670SHong Zhang   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3732721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3733110eb670SHong Zhang   PetscFunctionReturn(0);
3734110eb670SHong Zhang }
3735110eb670SHong Zhang 
3736cd652676SJed Brown /*@
3737cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3738cd652676SJed Brown 
3739cd652676SJed Brown    Collective on TS
3740cd652676SJed Brown 
3741cd652676SJed Brown    Input Argument:
3742cd652676SJed Brown +  ts - time stepping context
3743cd652676SJed Brown -  t - time to interpolate to
3744cd652676SJed Brown 
3745cd652676SJed Brown    Output Argument:
37460910c330SBarry Smith .  U - state at given time
3747cd652676SJed Brown 
3748cd652676SJed Brown    Level: intermediate
3749cd652676SJed Brown 
3750cd652676SJed Brown    Developer Notes:
3751cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3752cd652676SJed Brown 
3753cd652676SJed Brown .keywords: TS, set
3754cd652676SJed Brown 
3755874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3756cd652676SJed Brown @*/
37570910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3758cd652676SJed Brown {
3759cd652676SJed Brown   PetscErrorCode ierr;
3760cd652676SJed Brown 
3761cd652676SJed Brown   PetscFunctionBegin;
3762cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3763b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3764be5899b3SLisandro Dalcin   if (t < ts->ptime_prev || t > ts->ptime) SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Requested time %g not in last time steps [%g,%g]",t,(double)ts->ptime_prev,(double)ts->ptime);
3765ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
37660910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3767cd652676SJed Brown   PetscFunctionReturn(0);
3768cd652676SJed Brown }
3769cd652676SJed Brown 
3770d763cef2SBarry Smith /*@
37716d9e5789SSean Farley    TSStep - Steps one time step
3772d763cef2SBarry Smith 
3773d763cef2SBarry Smith    Collective on TS
3774d763cef2SBarry Smith 
3775d763cef2SBarry Smith    Input Parameter:
3776d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3777d763cef2SBarry Smith 
377827829d71SBarry Smith    Level: developer
3779d763cef2SBarry Smith 
3780b8123daeSJed Brown    Notes:
378127829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
378227829d71SBarry Smith 
3783b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3784b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3785b8123daeSJed Brown 
378625cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
378725cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
378825cb2221SBarry Smith 
3789d763cef2SBarry Smith .keywords: TS, timestep, solve
3790d763cef2SBarry Smith 
37919be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3792d763cef2SBarry Smith @*/
3793193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3794d763cef2SBarry Smith {
3795dfbe8321SBarry Smith   PetscErrorCode   ierr;
3796fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3797be5899b3SLisandro Dalcin   PetscReal        ptime;
3798d763cef2SBarry Smith 
3799d763cef2SBarry Smith   PetscFunctionBegin;
38000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3801fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3802fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3803fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3804fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3805fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3806fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3807302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3808fffbeea8SBarry Smith 
3809d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
381068bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3811d405a339SMatthew Knepley 
3812a6772fa2SLisandro Dalcin   if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSStep()");
3813be5899b3SLisandro Dalcin   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && !ts->adapt) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE");
3814a6772fa2SLisandro Dalcin 
3815be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3816362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3817be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
3818e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3819d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3820193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3821d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3822be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
3823be5899b3SLisandro Dalcin   ts->steps++; ts->total_steps++;
3824be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
382528d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3826362cd11cSLisandro Dalcin 
3827362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3828cef5090cSJed Brown     if (ts->errorifstepfailed) {
382908c7845fSBarry 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]);
383008c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3831d2daff3dSHong Zhang     }
383208c7845fSBarry Smith   } else if (!ts->reason) {
383308c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
383408c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
383508c7845fSBarry Smith   }
383608c7845fSBarry Smith   PetscFunctionReturn(0);
383708c7845fSBarry Smith }
383808c7845fSBarry Smith 
383908c7845fSBarry Smith /*@
3840b957a604SHong Zhang    TSAdjointStep - Steps one time step backward in the adjoint run
384108c7845fSBarry Smith 
384208c7845fSBarry Smith    Collective on TS
384308c7845fSBarry Smith 
384408c7845fSBarry Smith    Input Parameter:
384508c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
384608c7845fSBarry Smith 
38476a4d4014SLisandro Dalcin    Level: intermediate
38486a4d4014SLisandro Dalcin 
3849b957a604SHong Zhang .keywords: TS, adjoint, step
38506a4d4014SLisandro Dalcin 
3851b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve()
38526a4d4014SLisandro Dalcin @*/
385308c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
38546a4d4014SLisandro Dalcin {
385508c7845fSBarry Smith   DM               dm;
38566a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
38576a4d4014SLisandro Dalcin 
38586a4d4014SLisandro Dalcin   PetscFunctionBegin;
38594a2ae208SSatish Balay   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
386008c7845fSBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
386108c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3862d763cef2SBarry Smith 
3863685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr);
3864d763cef2SBarry Smith 
3865be5899b3SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3866be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime;
386742f2b339SBarry 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);
3868be5899b3SLisandro Dalcin   ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
386942f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
38708d0ad7a8SHong Zhang   ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
3871be5899b3SLisandro Dalcin   ts->steps++; ts->total_steps--;
3872362cd11cSLisandro Dalcin 
3873362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3874cef5090cSJed Brown     if (ts->errorifstepfailed) {
38756c4ed002SBarry 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]);
38766c4ed002SBarry 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]);
38776c4ed002SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3878cef5090cSJed Brown     }
3879362cd11cSLisandro Dalcin   } else if (!ts->reason) {
388073b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
3881362cd11cSLisandro Dalcin   }
3882d763cef2SBarry Smith   PetscFunctionReturn(0);
3883d763cef2SBarry Smith }
3884d763cef2SBarry Smith 
38857cbde773SLisandro Dalcin /*@
38867cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
38877cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
38887cbde773SLisandro Dalcin 
38897cbde773SLisandro Dalcin    Collective on TS
38907cbde773SLisandro Dalcin 
38917cbde773SLisandro Dalcin    Input Arguments:
38927cbde773SLisandro Dalcin +  ts - time stepping context
38937cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
38947cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
38957cbde773SLisandro Dalcin 
38967cbde773SLisandro Dalcin    Output Arguments:
38977cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
38987cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
38997cbde773SLisandro Dalcin 
39007cbde773SLisandro Dalcin    Level: advanced
39017cbde773SLisandro Dalcin 
39027cbde773SLisandro Dalcin    Notes:
39037cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
39047cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
39057cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
39067cbde773SLisandro Dalcin 
39077cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
39087cbde773SLisandro Dalcin @*/
39097cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
39107cbde773SLisandro Dalcin {
39117cbde773SLisandro Dalcin   PetscErrorCode ierr;
39127cbde773SLisandro Dalcin 
39137cbde773SLisandro Dalcin   PetscFunctionBegin;
39147cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39157cbde773SLisandro Dalcin   PetscValidType(ts,1);
39167cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
39177cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
39187cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
39197cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
39207cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
39217cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
39227cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
39237cbde773SLisandro Dalcin   PetscFunctionReturn(0);
39247cbde773SLisandro Dalcin }
39257cbde773SLisandro Dalcin 
392605175c85SJed Brown /*@
392705175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
392805175c85SJed Brown 
39291c3436cfSJed Brown    Collective on TS
393005175c85SJed Brown 
393105175c85SJed Brown    Input Arguments:
39321c3436cfSJed Brown +  ts - time stepping context
39331c3436cfSJed Brown .  order - desired order of accuracy
39340298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
393505175c85SJed Brown 
393605175c85SJed Brown    Output Arguments:
39370910c330SBarry Smith .  U - state at the end of the current step
393805175c85SJed Brown 
393905175c85SJed Brown    Level: advanced
394005175c85SJed Brown 
3941108c343cSJed Brown    Notes:
3942108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3943108c343cSJed 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.
3944108c343cSJed Brown 
39451c3436cfSJed Brown .seealso: TSStep(), TSAdapt
394605175c85SJed Brown @*/
39470910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
394805175c85SJed Brown {
394905175c85SJed Brown   PetscErrorCode ierr;
395005175c85SJed Brown 
395105175c85SJed Brown   PetscFunctionBegin;
395205175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
395305175c85SJed Brown   PetscValidType(ts,1);
39540910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3955ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
39560910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
395705175c85SJed Brown   PetscFunctionReturn(0);
395805175c85SJed Brown }
395905175c85SJed Brown 
3960b1cb36f3SHong Zhang /*@
3961b1cb36f3SHong Zhang  TSForwardCostIntegral - Evaluate the cost integral in the forward run.
3962b1cb36f3SHong Zhang 
3963b1cb36f3SHong Zhang  Collective on TS
3964b1cb36f3SHong Zhang 
3965b1cb36f3SHong Zhang  Input Arguments:
3966b1cb36f3SHong Zhang  .  ts - time stepping context
3967b1cb36f3SHong Zhang 
3968b1cb36f3SHong Zhang  Level: advanced
3969b1cb36f3SHong Zhang 
3970b1cb36f3SHong Zhang  Notes:
3971b1cb36f3SHong Zhang  This function cannot be called until TSStep() has been completed.
3972b1cb36f3SHong Zhang 
3973b1cb36f3SHong Zhang  .seealso: TSSolve(), TSAdjointCostIntegral()
3974b1cb36f3SHong Zhang  @*/
3975b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts)
3976b1cb36f3SHong Zhang {
3977b1cb36f3SHong Zhang     PetscErrorCode ierr;
3978b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3979b1cb36f3SHong 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);
3980b1cb36f3SHong Zhang     ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr);
3981b1cb36f3SHong Zhang     PetscFunctionReturn(0);
3982b1cb36f3SHong Zhang }
3983d67e68b7SBarry Smith 
3984d763cef2SBarry Smith /*@
3985d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
3986d763cef2SBarry Smith 
3987d763cef2SBarry Smith    Collective on TS
3988d763cef2SBarry Smith 
3989d763cef2SBarry Smith    Input Parameter:
3990d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
399163e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
399263e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
39935a3a76d0SJed Brown 
3994d763cef2SBarry Smith    Level: beginner
3995d763cef2SBarry Smith 
39965a3a76d0SJed Brown    Notes:
39975a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
39985a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
39995a3a76d0SJed Brown    stepped over the final time.
40005a3a76d0SJed Brown 
4001d763cef2SBarry Smith .keywords: TS, timestep, solve
4002d763cef2SBarry Smith 
400363e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
4004d763cef2SBarry Smith @*/
4005cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
4006d763cef2SBarry Smith {
4007b06615a5SLisandro Dalcin   Vec               solution;
4008d763cef2SBarry Smith   PetscErrorCode    ierr;
4009d763cef2SBarry Smith 
4010d763cef2SBarry Smith   PetscFunctionBegin;
4011d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4012f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4013feed9e9dSBarry Smith 
401449354f04SShri 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 */
4015b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
40160910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4017b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4018b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4019b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
40205a3a76d0SJed Brown     }
40210910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4022bbd56ea5SKarl Rupp   } else if (u) {
40230910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
40245a3a76d0SJed Brown   }
4025b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
402668bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4027a6772fa2SLisandro Dalcin 
4028a6772fa2SLisandro Dalcin   if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSSolve()");
4029a6772fa2SLisandro Dalcin   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && !ts->adapt) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE");
4030a6772fa2SLisandro Dalcin 
4031e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4032e7069c78SShri      restarts the step after an event. Resetting these counters in such a case causes
4033e7069c78SShri      TSTrajectory to incorrectly save the output files
4034e7069c78SShri   */
4035938c94caSShri Abhyankar 
4036193ac0bcSJed Brown   ts->steps             = 0;
40375ef26d82SJed Brown   ts->ksp_its           = 0;
40385ef26d82SJed Brown   ts->snes_its          = 0;
4039c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
4040c610991cSLisandro Dalcin   ts->reject            = 0;
4041193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
4042193ac0bcSJed Brown 
4043ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4044f05ece33SBarry Smith 
4045193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4046193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4047a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4048cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4049a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4050be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4051db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
4052db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4053e7069c78SShri 
4054938c94caSShri Abhyankar     ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40556427ac75SLisandro Dalcin     ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
4056e7069c78SShri 
405728d5b5d6SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
405828d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_TRUE;
40596427ac75SLisandro Dalcin 
4060e1a7a14fSJed Brown     while (!ts->reason) {
4061938c94caSShri Abhyankar       ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40629687d888SLisandro Dalcin       if (!ts->steprollback) {
40639687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
40649687d888SLisandro Dalcin       }
4065193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4066e783b05fSHong Zhang       if (ts->vec_costintegral && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
4067b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
4068b1cb36f3SHong Zhang       }
406910b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4070e783b05fSHong Zhang       ierr = TSEventHandler(ts);CHKERRQ(ierr); /* The right-hand side may be changed due to event. Be careful with Any computation using the RHS information after this point. */
4071e783b05fSHong Zhang       if (!ts->steprollback) {
4072938c94caSShri Abhyankar         ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40731eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4074aeb4809dSShri Abhyankar       }
4075193ac0bcSJed Brown     }
4076938c94caSShri Abhyankar     ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40776427ac75SLisandro Dalcin 
407849354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
40790910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4080cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4081b06615a5SLisandro Dalcin       solution = u;
408263e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
40830574a7fbSJed Brown     } else {
4084ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4085cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4086b06615a5SLisandro Dalcin       solution = ts->vec_sol;
40870574a7fbSJed Brown     }
4088193ac0bcSJed Brown   }
4089d2daff3dSHong Zhang 
4090ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
409163e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
409256f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4093ef222394SBarry Smith   if (ts->adjoint_solve) {
4094ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
40952b0a91c0SBarry Smith   }
4096d763cef2SBarry Smith   PetscFunctionReturn(0);
4097d763cef2SBarry Smith }
4098d763cef2SBarry Smith 
4099b1cb36f3SHong Zhang /*@
4100b1cb36f3SHong Zhang  TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run.
4101b1cb36f3SHong Zhang 
4102b1cb36f3SHong Zhang  Collective on TS
4103b1cb36f3SHong Zhang 
4104b1cb36f3SHong Zhang  Input Arguments:
4105b1cb36f3SHong Zhang  .  ts - time stepping context
4106b1cb36f3SHong Zhang 
4107b1cb36f3SHong Zhang  Level: advanced
4108b1cb36f3SHong Zhang 
4109b1cb36f3SHong Zhang  Notes:
4110b1cb36f3SHong Zhang  This function cannot be called until TSAdjointStep() has been completed.
4111b1cb36f3SHong Zhang 
4112b1cb36f3SHong Zhang  .seealso: TSAdjointSolve(), TSAdjointStep
4113b1cb36f3SHong Zhang  @*/
4114b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts)
4115b1cb36f3SHong Zhang {
4116b1cb36f3SHong Zhang     PetscErrorCode ierr;
4117b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4118b1cb36f3SHong 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);
4119b1cb36f3SHong Zhang     ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr);
4120b1cb36f3SHong Zhang     PetscFunctionReturn(0);
4121b1cb36f3SHong Zhang }
4122b1cb36f3SHong Zhang 
4123c88f2d44SBarry Smith /*@
4124c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
4125c88f2d44SBarry Smith 
4126c88f2d44SBarry Smith    Collective on TS
4127c88f2d44SBarry Smith 
4128c88f2d44SBarry Smith    Input Parameter:
41292c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
4130c88f2d44SBarry Smith 
4131e87fd094SBarry Smith    Options Database:
4132e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions
4133e87fd094SBarry Smith 
4134c88f2d44SBarry Smith    Level: intermediate
4135c88f2d44SBarry Smith 
4136c88f2d44SBarry Smith    Notes:
4137c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
4138c88f2d44SBarry Smith 
413973b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
414073b18844SBarry Smith 
4141c88f2d44SBarry Smith .keywords: TS, timestep, solve
4142c88f2d44SBarry Smith 
4143b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
4144c88f2d44SBarry Smith @*/
41452c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
4146c88f2d44SBarry Smith {
4147c88f2d44SBarry Smith   PetscErrorCode    ierr;
4148c88f2d44SBarry Smith 
4149c88f2d44SBarry Smith   PetscFunctionBegin;
4150c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4151c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
415268bece0bSHong Zhang 
4153c88f2d44SBarry Smith   /* reset time step and iteration counters */
4154c88f2d44SBarry Smith   ts->steps             = 0;
4155c88f2d44SBarry Smith   ts->ksp_its           = 0;
4156c88f2d44SBarry Smith   ts->snes_its          = 0;
4157c88f2d44SBarry Smith   ts->num_snes_failures = 0;
4158c88f2d44SBarry Smith   ts->reject            = 0;
4159c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
4160c88f2d44SBarry Smith 
416173b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
4162c88f2d44SBarry Smith 
416373b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
4164c88f2d44SBarry Smith   while (!ts->reason) {
416555c52731SHong Zhang     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
41669110b2e7SHong Zhang     ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
41676427ac75SLisandro Dalcin     ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr);
4168616946c1SHong Zhang     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
4169b1cb36f3SHong Zhang     if (ts->vec_costintegral && !ts->costintegralfwd) {
4170b1cb36f3SHong Zhang       ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr);
4171b1cb36f3SHong Zhang     }
4172c88f2d44SBarry Smith   }
417326656371SHong Zhang   ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
417426656371SHong Zhang   ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
4175c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
4176e210cd0eSHong Zhang   ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr);
4177685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr);
4178c88f2d44SBarry Smith   PetscFunctionReturn(0);
4179c88f2d44SBarry Smith }
4180c88f2d44SBarry Smith 
41817db568b7SBarry Smith /*@C
4182228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4183228d79bcSJed Brown 
4184228d79bcSJed Brown    Collective on TS
4185228d79bcSJed Brown 
4186228d79bcSJed Brown    Input Parameters:
4187228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4188228d79bcSJed Brown .  step - step number that has just completed
4189228d79bcSJed Brown .  ptime - model time of the state
41900910c330SBarry Smith -  u - state at the current model time
4191228d79bcSJed Brown 
4192228d79bcSJed Brown    Notes:
41937db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
41947db568b7SBarry Smith    Users would almost never call this routine directly.
4195228d79bcSJed Brown 
419663e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
419763e21af5SBarry Smith 
41987db568b7SBarry Smith    Level: developer
4199228d79bcSJed Brown 
4200228d79bcSJed Brown .keywords: TS, timestep
4201228d79bcSJed Brown @*/
42020910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4203d763cef2SBarry Smith {
4204d6152f81SLisandro Dalcin   DM             dm;
4205a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4206d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4207d763cef2SBarry Smith 
4208d763cef2SBarry Smith   PetscFunctionBegin;
4209b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4210b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4211d6152f81SLisandro Dalcin 
4212d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4213d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4214d6152f81SLisandro Dalcin 
42155edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
4216d763cef2SBarry Smith   for (i=0; i<n; i++) {
42170910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4218d763cef2SBarry Smith   }
42195edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
4220d763cef2SBarry Smith   PetscFunctionReturn(0);
4221d763cef2SBarry Smith }
4222d763cef2SBarry Smith 
42237db568b7SBarry Smith /*@C
42249110b2e7SHong Zhang    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()
42259110b2e7SHong Zhang 
42269110b2e7SHong Zhang    Collective on TS
42279110b2e7SHong Zhang 
42289110b2e7SHong Zhang    Input Parameters:
42299110b2e7SHong Zhang +  ts - time stepping context obtained from TSCreate()
42309110b2e7SHong Zhang .  step - step number that has just completed
42319110b2e7SHong Zhang .  ptime - model time of the state
42329110b2e7SHong Zhang .  u - state at the current model time
42339110b2e7SHong Zhang .  numcost - number of cost functions (dimension of lambda  or mu)
42349110b2e7SHong Zhang .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
42359110b2e7SHong Zhang -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
42369110b2e7SHong Zhang 
42379110b2e7SHong Zhang    Notes:
42387db568b7SBarry Smith    TSAdjointMonitor() is typically used automatically within the time stepping implementations.
42397db568b7SBarry Smith    Users would almost never call this routine directly.
42409110b2e7SHong Zhang 
42417db568b7SBarry Smith    Level: developer
42429110b2e7SHong Zhang 
42439110b2e7SHong Zhang .keywords: TS, timestep
42449110b2e7SHong Zhang @*/
42459110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
42469110b2e7SHong Zhang {
42479110b2e7SHong Zhang   PetscErrorCode ierr;
42489110b2e7SHong Zhang   PetscInt       i,n = ts->numberadjointmonitors;
42499110b2e7SHong Zhang 
42509110b2e7SHong Zhang   PetscFunctionBegin;
42519110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42529110b2e7SHong Zhang   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
42539110b2e7SHong Zhang   ierr = VecLockPush(u);CHKERRQ(ierr);
42549110b2e7SHong Zhang   for (i=0; i<n; i++) {
42559110b2e7SHong Zhang     ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
42569110b2e7SHong Zhang   }
42579110b2e7SHong Zhang   ierr = VecLockPop(u);CHKERRQ(ierr);
42589110b2e7SHong Zhang   PetscFunctionReturn(0);
42599110b2e7SHong Zhang }
42609110b2e7SHong Zhang 
4261d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4262d763cef2SBarry Smith /*@C
42637db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4264a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4265d763cef2SBarry Smith 
4266d763cef2SBarry Smith    Collective on TS
4267d763cef2SBarry Smith 
4268d763cef2SBarry Smith    Input Parameters:
4269d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4270d763cef2SBarry Smith .  label - the title to put in the title bar
42717c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4272a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4273a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4274d763cef2SBarry Smith 
4275d763cef2SBarry Smith    Output Parameter:
42760b039ecaSBarry Smith .  ctx - the context
4277d763cef2SBarry Smith 
4278d763cef2SBarry Smith    Options Database Key:
42794f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
42807db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
42817db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
42827db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
42837db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4284b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4285d763cef2SBarry Smith 
4286d763cef2SBarry Smith    Notes:
4287a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4288d763cef2SBarry Smith 
42897db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
42907db568b7SBarry Smith 
42917db568b7SBarry 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
42927db568b7SBarry 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
42937db568b7SBarry Smith    as the first argument.
42947db568b7SBarry Smith 
42957db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
42967db568b7SBarry Smith 
42977db568b7SBarry Smith 
4298d763cef2SBarry Smith    Level: intermediate
4299d763cef2SBarry Smith 
43007db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
4301d763cef2SBarry Smith 
43027db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
43037db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
43047db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
43057db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
43067db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
43077c922b88SBarry Smith 
4308d763cef2SBarry Smith @*/
4309a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4310d763cef2SBarry Smith {
43117f52daa2SLisandro Dalcin   PetscDraw      draw;
4312dfbe8321SBarry Smith   PetscErrorCode ierr;
4313d763cef2SBarry Smith 
4314d763cef2SBarry Smith   PetscFunctionBegin;
4315b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
43167f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43177f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43187f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4319287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
43207f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4321a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4322d763cef2SBarry Smith   PetscFunctionReturn(0);
4323d763cef2SBarry Smith }
4324d763cef2SBarry Smith 
4325b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4326d763cef2SBarry Smith {
43270b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4328c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4329dfbe8321SBarry Smith   PetscErrorCode ierr;
4330d763cef2SBarry Smith 
4331d763cef2SBarry Smith   PetscFunctionBegin;
433263e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4333b06615a5SLisandro Dalcin   if (!step) {
4334a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4335a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
43366934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr);
4337a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4338a9f9c1f6SBarry Smith   }
4339c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
43400b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4341b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
43420b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
43436934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
43443923b477SBarry Smith   }
4345d763cef2SBarry Smith   PetscFunctionReturn(0);
4346d763cef2SBarry Smith }
4347d763cef2SBarry Smith 
4348d763cef2SBarry Smith /*@C
4349a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4350a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4351d763cef2SBarry Smith 
43520b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4353d763cef2SBarry Smith 
4354d763cef2SBarry Smith    Input Parameter:
43550b039ecaSBarry Smith .  ctx - the monitor context
4356d763cef2SBarry Smith 
4357d763cef2SBarry Smith    Level: intermediate
4358d763cef2SBarry Smith 
4359d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
4360d763cef2SBarry Smith 
43614f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4362d763cef2SBarry Smith @*/
4363a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4364d763cef2SBarry Smith {
4365dfbe8321SBarry Smith   PetscErrorCode ierr;
4366d763cef2SBarry Smith 
4367d763cef2SBarry Smith   PetscFunctionBegin;
43687684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
43697684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
43707684fa3eSBarry Smith   }
43710b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
437231152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4373387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4374387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4375387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
43760b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4377d763cef2SBarry Smith   PetscFunctionReturn(0);
4378d763cef2SBarry Smith }
4379d763cef2SBarry Smith 
4380d763cef2SBarry Smith /*@
4381b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4382d763cef2SBarry Smith 
4383d763cef2SBarry Smith    Not Collective
4384d763cef2SBarry Smith 
4385d763cef2SBarry Smith    Input Parameter:
4386d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4387d763cef2SBarry Smith 
4388d763cef2SBarry Smith    Output Parameter:
438963e21af5SBarry Smith .  t  - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime().
4390d763cef2SBarry Smith 
4391d763cef2SBarry Smith    Level: beginner
4392d763cef2SBarry Smith 
4393b8123daeSJed Brown    Note:
4394b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
43959be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4396b8123daeSJed Brown 
439763e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime()
4398d763cef2SBarry Smith 
4399d763cef2SBarry Smith .keywords: TS, get, time
4400d763cef2SBarry Smith @*/
44017087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4402d763cef2SBarry Smith {
4403d763cef2SBarry Smith   PetscFunctionBegin;
44040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4405f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4406d763cef2SBarry Smith   *t = ts->ptime;
4407d763cef2SBarry Smith   PetscFunctionReturn(0);
4408d763cef2SBarry Smith }
4409d763cef2SBarry Smith 
4410e5e524a1SHong Zhang /*@
4411e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4412e5e524a1SHong Zhang 
4413e5e524a1SHong Zhang    Not Collective
4414e5e524a1SHong Zhang 
4415e5e524a1SHong Zhang    Input Parameter:
4416e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4417e5e524a1SHong Zhang 
4418e5e524a1SHong Zhang    Output Parameter:
4419e5e524a1SHong Zhang .  t  - the previous time
4420e5e524a1SHong Zhang 
4421e5e524a1SHong Zhang    Level: beginner
4422e5e524a1SHong Zhang 
4423e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
4424e5e524a1SHong Zhang 
4425e5e524a1SHong Zhang .keywords: TS, get, time
4426e5e524a1SHong Zhang @*/
4427e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4428e5e524a1SHong Zhang {
4429e5e524a1SHong Zhang   PetscFunctionBegin;
4430e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4431e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4432e5e524a1SHong Zhang   *t = ts->ptime_prev;
4433e5e524a1SHong Zhang   PetscFunctionReturn(0);
4434e5e524a1SHong Zhang }
4435e5e524a1SHong Zhang 
44366a4d4014SLisandro Dalcin /*@
44376a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
44386a4d4014SLisandro Dalcin 
44393f9fe445SBarry Smith    Logically Collective on TS
44406a4d4014SLisandro Dalcin 
44416a4d4014SLisandro Dalcin    Input Parameters:
44426a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
44436a4d4014SLisandro Dalcin -  time - the time
44446a4d4014SLisandro Dalcin 
44456a4d4014SLisandro Dalcin    Level: intermediate
44466a4d4014SLisandro Dalcin 
44476a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
44486a4d4014SLisandro Dalcin 
44496a4d4014SLisandro Dalcin .keywords: TS, set, time
44506a4d4014SLisandro Dalcin @*/
44517087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
44526a4d4014SLisandro Dalcin {
44536a4d4014SLisandro Dalcin   PetscFunctionBegin;
44540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4455c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
44566a4d4014SLisandro Dalcin   ts->ptime = t;
44576a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
44586a4d4014SLisandro Dalcin }
44596a4d4014SLisandro Dalcin 
4460d763cef2SBarry Smith /*@C
4461d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4462d763cef2SBarry Smith    TS options in the database.
4463d763cef2SBarry Smith 
44643f9fe445SBarry Smith    Logically Collective on TS
4465d763cef2SBarry Smith 
4466d763cef2SBarry Smith    Input Parameter:
4467d763cef2SBarry Smith +  ts     - The TS context
4468d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4469d763cef2SBarry Smith 
4470d763cef2SBarry Smith    Notes:
4471d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4472d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4473d763cef2SBarry Smith    hyphen.
4474d763cef2SBarry Smith 
4475d763cef2SBarry Smith    Level: advanced
4476d763cef2SBarry Smith 
4477d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4478d763cef2SBarry Smith 
4479d763cef2SBarry Smith .seealso: TSSetFromOptions()
4480d763cef2SBarry Smith 
4481d763cef2SBarry Smith @*/
44827087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4483d763cef2SBarry Smith {
4484dfbe8321SBarry Smith   PetscErrorCode ierr;
4485089b2837SJed Brown   SNES           snes;
4486d763cef2SBarry Smith 
4487d763cef2SBarry Smith   PetscFunctionBegin;
44880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4489d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4490089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4491089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4492d763cef2SBarry Smith   PetscFunctionReturn(0);
4493d763cef2SBarry Smith }
4494d763cef2SBarry Smith 
4495d763cef2SBarry Smith 
4496d763cef2SBarry Smith /*@C
4497d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4498d763cef2SBarry Smith    TS options in the database.
4499d763cef2SBarry Smith 
45003f9fe445SBarry Smith    Logically Collective on TS
4501d763cef2SBarry Smith 
4502d763cef2SBarry Smith    Input Parameter:
4503d763cef2SBarry Smith +  ts     - The TS context
4504d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4505d763cef2SBarry Smith 
4506d763cef2SBarry Smith    Notes:
4507d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4508d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4509d763cef2SBarry Smith    hyphen.
4510d763cef2SBarry Smith 
4511d763cef2SBarry Smith    Level: advanced
4512d763cef2SBarry Smith 
4513d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4514d763cef2SBarry Smith 
4515d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4516d763cef2SBarry Smith 
4517d763cef2SBarry Smith @*/
45187087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4519d763cef2SBarry Smith {
4520dfbe8321SBarry Smith   PetscErrorCode ierr;
4521089b2837SJed Brown   SNES           snes;
4522d763cef2SBarry Smith 
4523d763cef2SBarry Smith   PetscFunctionBegin;
45240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4525d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4526089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4527089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4528d763cef2SBarry Smith   PetscFunctionReturn(0);
4529d763cef2SBarry Smith }
4530d763cef2SBarry Smith 
4531d763cef2SBarry Smith /*@C
4532d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4533d763cef2SBarry Smith    TS options in the database.
4534d763cef2SBarry Smith 
4535d763cef2SBarry Smith    Not Collective
4536d763cef2SBarry Smith 
4537d763cef2SBarry Smith    Input Parameter:
4538d763cef2SBarry Smith .  ts - The TS context
4539d763cef2SBarry Smith 
4540d763cef2SBarry Smith    Output Parameter:
4541d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4542d763cef2SBarry Smith 
4543d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
4544d763cef2SBarry Smith    sufficient length to hold the prefix.
4545d763cef2SBarry Smith 
4546d763cef2SBarry Smith    Level: intermediate
4547d763cef2SBarry Smith 
4548d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4549d763cef2SBarry Smith 
4550d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4551d763cef2SBarry Smith @*/
45527087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4553d763cef2SBarry Smith {
4554dfbe8321SBarry Smith   PetscErrorCode ierr;
4555d763cef2SBarry Smith 
4556d763cef2SBarry Smith   PetscFunctionBegin;
45570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45584482741eSBarry Smith   PetscValidPointer(prefix,2);
4559d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4560d763cef2SBarry Smith   PetscFunctionReturn(0);
4561d763cef2SBarry Smith }
4562d763cef2SBarry Smith 
4563d763cef2SBarry Smith /*@C
4564d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4565d763cef2SBarry Smith 
4566d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4567d763cef2SBarry Smith 
4568d763cef2SBarry Smith    Input Parameter:
4569d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4570d763cef2SBarry Smith 
4571d763cef2SBarry Smith    Output Parameters:
4572e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4573e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4574e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4575e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4576d763cef2SBarry Smith 
45770298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
4578d763cef2SBarry Smith 
4579d763cef2SBarry Smith    Level: intermediate
4580d763cef2SBarry Smith 
458126d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
4582d763cef2SBarry Smith 
4583d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4584d763cef2SBarry Smith @*/
4585e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4586d763cef2SBarry Smith {
4587089b2837SJed Brown   PetscErrorCode ierr;
458824989b8cSPeter Brune   DM             dm;
4589089b2837SJed Brown 
4590d763cef2SBarry Smith   PetscFunctionBegin;
459123a57915SBarry Smith   if (Amat || Pmat) {
459223a57915SBarry Smith     SNES snes;
4593089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
459423a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4595e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
459623a57915SBarry Smith   }
459724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
459824989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4599d763cef2SBarry Smith   PetscFunctionReturn(0);
4600d763cef2SBarry Smith }
4601d763cef2SBarry Smith 
46022eca1d9cSJed Brown /*@C
46032eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
46042eca1d9cSJed Brown 
46052eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
46062eca1d9cSJed Brown 
46072eca1d9cSJed Brown    Input Parameter:
46082eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
46092eca1d9cSJed Brown 
46102eca1d9cSJed Brown    Output Parameters:
4611e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4612e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
46132eca1d9cSJed Brown .  f   - The function to compute the matrices
46142eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
46152eca1d9cSJed Brown 
46160298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
46172eca1d9cSJed Brown 
46182eca1d9cSJed Brown    Level: advanced
46192eca1d9cSJed Brown 
46202eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
46212eca1d9cSJed Brown 
46222eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
46232eca1d9cSJed Brown @*/
4624e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
46252eca1d9cSJed Brown {
4626089b2837SJed Brown   PetscErrorCode ierr;
462724989b8cSPeter Brune   DM             dm;
46280910c330SBarry Smith 
46292eca1d9cSJed Brown   PetscFunctionBegin;
4630c0aab802Sstefano_zampini   if (Amat || Pmat) {
4631c0aab802Sstefano_zampini     SNES snes;
4632089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4633f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4634e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4635c0aab802Sstefano_zampini   }
463624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
463724989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
46382eca1d9cSJed Brown   PetscFunctionReturn(0);
46392eca1d9cSJed Brown }
46402eca1d9cSJed Brown 
46416083293cSBarry Smith 
46421713a123SBarry Smith /*@C
464383a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
46441713a123SBarry Smith    VecView() for the solution at each timestep
46451713a123SBarry Smith 
46461713a123SBarry Smith    Collective on TS
46471713a123SBarry Smith 
46481713a123SBarry Smith    Input Parameters:
46491713a123SBarry Smith +  ts - the TS context
46501713a123SBarry Smith .  step - current time-step
4651142b95e3SSatish Balay .  ptime - current time
46520298fd71SBarry Smith -  dummy - either a viewer or NULL
46531713a123SBarry Smith 
465499fdda47SBarry Smith    Options Database:
465599fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
465699fdda47SBarry Smith 
4657387f4636SBarry 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
465899fdda47SBarry Smith        will look bad
465999fdda47SBarry Smith 
46601713a123SBarry Smith    Level: intermediate
46611713a123SBarry Smith 
46621713a123SBarry Smith .keywords: TS,  vector, monitor, view
46631713a123SBarry Smith 
4664a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
46651713a123SBarry Smith @*/
46660910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46671713a123SBarry Smith {
4668dfbe8321SBarry Smith   PetscErrorCode   ierr;
466983a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4670473a3ab2SBarry Smith   PetscDraw        draw;
46711713a123SBarry Smith 
46721713a123SBarry Smith   PetscFunctionBegin;
46736083293cSBarry Smith   if (!step && ictx->showinitial) {
46746083293cSBarry Smith     if (!ictx->initialsolution) {
46750910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
46761713a123SBarry Smith     }
46770910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
46786083293cSBarry Smith   }
4679b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
46800dcf80beSBarry Smith 
46816083293cSBarry Smith   if (ictx->showinitial) {
46826083293cSBarry Smith     PetscReal pause;
46836083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
46846083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
46856083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
46866083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
46876083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
46886083293cSBarry Smith   }
46890910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4690473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
469151fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4692473a3ab2SBarry Smith     char      time[32];
4693473a3ab2SBarry Smith 
4694473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
469585b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4696473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4697473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
469851fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4699473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4700473a3ab2SBarry Smith   }
4701473a3ab2SBarry Smith 
47026083293cSBarry Smith   if (ictx->showinitial) {
47036083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
47046083293cSBarry Smith   }
47051713a123SBarry Smith   PetscFunctionReturn(0);
47061713a123SBarry Smith }
47071713a123SBarry Smith 
47089110b2e7SHong Zhang /*@C
47099110b2e7SHong Zhang    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
47109110b2e7SHong Zhang    VecView() for the sensitivities to initial states at each timestep
47119110b2e7SHong Zhang 
47129110b2e7SHong Zhang    Collective on TS
47139110b2e7SHong Zhang 
47149110b2e7SHong Zhang    Input Parameters:
47159110b2e7SHong Zhang +  ts - the TS context
47169110b2e7SHong Zhang .  step - current time-step
47179110b2e7SHong Zhang .  ptime - current time
47189110b2e7SHong Zhang .  u - current state
47199110b2e7SHong Zhang .  numcost - number of cost functions
47209110b2e7SHong Zhang .  lambda - sensitivities to initial conditions
47219110b2e7SHong Zhang .  mu - sensitivities to parameters
47229110b2e7SHong Zhang -  dummy - either a viewer or NULL
47239110b2e7SHong Zhang 
47249110b2e7SHong Zhang    Level: intermediate
47259110b2e7SHong Zhang 
47269110b2e7SHong Zhang .keywords: TS,  vector, adjoint, monitor, view
47279110b2e7SHong Zhang 
47289110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
47299110b2e7SHong Zhang @*/
47309110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
47319110b2e7SHong Zhang {
47329110b2e7SHong Zhang   PetscErrorCode   ierr;
47339110b2e7SHong Zhang   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
47349110b2e7SHong Zhang   PetscDraw        draw;
4735a0046e2cSSatish Balay   PetscReal        xl,yl,xr,yr,h;
4736a0046e2cSSatish Balay   char             time[32];
47379110b2e7SHong Zhang 
47389110b2e7SHong Zhang   PetscFunctionBegin;
47399110b2e7SHong Zhang   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
47409110b2e7SHong Zhang 
47419110b2e7SHong Zhang   ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr);
47429110b2e7SHong Zhang   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
47439110b2e7SHong Zhang   ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
47449110b2e7SHong Zhang   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
47459110b2e7SHong Zhang   h    = yl + .95*(yr - yl);
47469110b2e7SHong Zhang   ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
47479110b2e7SHong Zhang   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
47489110b2e7SHong Zhang   PetscFunctionReturn(0);
47499110b2e7SHong Zhang }
47509110b2e7SHong Zhang 
47512d5ee99bSBarry Smith /*@C
47522d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
47532d5ee99bSBarry Smith 
47542d5ee99bSBarry Smith    Collective on TS
47552d5ee99bSBarry Smith 
47562d5ee99bSBarry Smith    Input Parameters:
47572d5ee99bSBarry Smith +  ts - the TS context
47582d5ee99bSBarry Smith .  step - current time-step
47592d5ee99bSBarry Smith .  ptime - current time
47602d5ee99bSBarry Smith -  dummy - either a viewer or NULL
47612d5ee99bSBarry Smith 
47622d5ee99bSBarry Smith    Level: intermediate
47632d5ee99bSBarry Smith 
47642d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
47652d5ee99bSBarry Smith 
47662d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
47672d5ee99bSBarry Smith @*/
47682d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
47692d5ee99bSBarry Smith {
47702d5ee99bSBarry Smith   PetscErrorCode    ierr;
47712d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
47722d5ee99bSBarry Smith   PetscDraw         draw;
47736934998bSLisandro Dalcin   PetscDrawAxis     axis;
47742d5ee99bSBarry Smith   PetscInt          n;
47752d5ee99bSBarry Smith   PetscMPIInt       size;
47766934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
47772d5ee99bSBarry Smith   char              time[32];
47782d5ee99bSBarry Smith   const PetscScalar *U;
47792d5ee99bSBarry Smith 
47802d5ee99bSBarry Smith   PetscFunctionBegin;
47816934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
47826934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
47832d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
47846934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
47852d5ee99bSBarry Smith 
47862d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
47876934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
47886934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
47896934998bSLisandro Dalcin   if (!step) {
47906934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
47916934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
47926934998bSLisandro Dalcin   }
47932d5ee99bSBarry Smith 
47942d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
47956934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
47966934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4797a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
47986934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
47992d5ee99bSBarry Smith 
48006934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
48016934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
48022d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
48034b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
480485b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
48052d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
480651fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
48072d5ee99bSBarry Smith   }
48086934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
48092d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
48106934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
48112d5ee99bSBarry Smith   PetscFunctionReturn(0);
48122d5ee99bSBarry Smith }
48132d5ee99bSBarry Smith 
48141713a123SBarry Smith 
48156083293cSBarry Smith /*@C
481683a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
48176083293cSBarry Smith 
48186083293cSBarry Smith    Collective on TS
48196083293cSBarry Smith 
48206083293cSBarry Smith    Input Parameters:
48216083293cSBarry Smith .    ctx - the monitor context
48226083293cSBarry Smith 
48236083293cSBarry Smith    Level: intermediate
48246083293cSBarry Smith 
48256083293cSBarry Smith .keywords: TS,  vector, monitor, view
48266083293cSBarry Smith 
482783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
48286083293cSBarry Smith @*/
482983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
48306083293cSBarry Smith {
48316083293cSBarry Smith   PetscErrorCode ierr;
48326083293cSBarry Smith 
48336083293cSBarry Smith   PetscFunctionBegin;
483483a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
483583a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
483683a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
48376083293cSBarry Smith   PetscFunctionReturn(0);
48386083293cSBarry Smith }
48396083293cSBarry Smith 
48406083293cSBarry Smith /*@C
484183a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
48426083293cSBarry Smith 
48436083293cSBarry Smith    Collective on TS
48446083293cSBarry Smith 
48456083293cSBarry Smith    Input Parameter:
48466083293cSBarry Smith .    ts - time-step context
48476083293cSBarry Smith 
48486083293cSBarry Smith    Output Patameter:
48496083293cSBarry Smith .    ctx - the monitor context
48506083293cSBarry Smith 
485199fdda47SBarry Smith    Options Database:
485299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
485399fdda47SBarry Smith 
48546083293cSBarry Smith    Level: intermediate
48556083293cSBarry Smith 
48566083293cSBarry Smith .keywords: TS,  vector, monitor, view
48576083293cSBarry Smith 
485883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
48596083293cSBarry Smith @*/
486083a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
48616083293cSBarry Smith {
48626083293cSBarry Smith   PetscErrorCode   ierr;
48636083293cSBarry Smith 
48646083293cSBarry Smith   PetscFunctionBegin;
4865b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
486683a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4867e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4868bbd56ea5SKarl Rupp 
486983a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4870473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4871c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4872473a3ab2SBarry Smith 
4873473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4874c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
48756083293cSBarry Smith   PetscFunctionReturn(0);
48766083293cSBarry Smith }
48776083293cSBarry Smith 
48783a471f94SBarry Smith /*@C
487983a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
48803a471f94SBarry Smith    VecView() for the error at each timestep
48813a471f94SBarry Smith 
48823a471f94SBarry Smith    Collective on TS
48833a471f94SBarry Smith 
48843a471f94SBarry Smith    Input Parameters:
48853a471f94SBarry Smith +  ts - the TS context
48863a471f94SBarry Smith .  step - current time-step
48873a471f94SBarry Smith .  ptime - current time
48880298fd71SBarry Smith -  dummy - either a viewer or NULL
48893a471f94SBarry Smith 
48903a471f94SBarry Smith    Level: intermediate
48913a471f94SBarry Smith 
48923a471f94SBarry Smith .keywords: TS,  vector, monitor, view
48933a471f94SBarry Smith 
48943a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
48953a471f94SBarry Smith @*/
48960910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48973a471f94SBarry Smith {
48983a471f94SBarry Smith   PetscErrorCode   ierr;
489983a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
490083a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
49013a471f94SBarry Smith   Vec              work;
49023a471f94SBarry Smith 
49033a471f94SBarry Smith   PetscFunctionBegin;
4904b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
49050910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
49063a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
49070910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
49083a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
49093a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
49103a471f94SBarry Smith   PetscFunctionReturn(0);
49113a471f94SBarry Smith }
49123a471f94SBarry Smith 
4913af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
49146c699258SBarry Smith /*@
49152a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
49166c699258SBarry Smith 
49173f9fe445SBarry Smith    Logically Collective on TS and DM
49186c699258SBarry Smith 
49196c699258SBarry Smith    Input Parameters:
49202a808120SBarry Smith +  ts - the ODE integrator object
49212a808120SBarry Smith -  dm - the dm, cannot be NULL
49226c699258SBarry Smith 
49236c699258SBarry Smith    Level: intermediate
49246c699258SBarry Smith 
49256c699258SBarry Smith 
49266c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
49276c699258SBarry Smith @*/
49287087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
49296c699258SBarry Smith {
49306c699258SBarry Smith   PetscErrorCode ierr;
4931089b2837SJed Brown   SNES           snes;
4932942e3340SBarry Smith   DMTS           tsdm;
49336c699258SBarry Smith 
49346c699258SBarry Smith   PetscFunctionBegin;
49350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49362a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
493770663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4938942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
49392a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4940942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4941942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
494224989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
494324989b8cSPeter Brune         tsdm->originaldm = dm;
494424989b8cSPeter Brune       }
494524989b8cSPeter Brune     }
49466bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
494724989b8cSPeter Brune   }
49486c699258SBarry Smith   ts->dm = dm;
4949bbd56ea5SKarl Rupp 
4950089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4951089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
49526c699258SBarry Smith   PetscFunctionReturn(0);
49536c699258SBarry Smith }
49546c699258SBarry Smith 
49556c699258SBarry Smith /*@
49566c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
49576c699258SBarry Smith 
49583f9fe445SBarry Smith    Not Collective
49596c699258SBarry Smith 
49606c699258SBarry Smith    Input Parameter:
49616c699258SBarry Smith . ts - the preconditioner context
49626c699258SBarry Smith 
49636c699258SBarry Smith    Output Parameter:
49646c699258SBarry Smith .  dm - the dm
49656c699258SBarry Smith 
49666c699258SBarry Smith    Level: intermediate
49676c699258SBarry Smith 
49686c699258SBarry Smith 
49696c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
49706c699258SBarry Smith @*/
49717087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
49726c699258SBarry Smith {
4973496e6a7aSJed Brown   PetscErrorCode ierr;
4974496e6a7aSJed Brown 
49756c699258SBarry Smith   PetscFunctionBegin;
49760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4977496e6a7aSJed Brown   if (!ts->dm) {
4978ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4979496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4980496e6a7aSJed Brown   }
49816c699258SBarry Smith   *dm = ts->dm;
49826c699258SBarry Smith   PetscFunctionReturn(0);
49836c699258SBarry Smith }
49841713a123SBarry Smith 
49850f5c6efeSJed Brown /*@
49860f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
49870f5c6efeSJed Brown 
49883f9fe445SBarry Smith    Logically Collective on SNES
49890f5c6efeSJed Brown 
49900f5c6efeSJed Brown    Input Parameter:
4991d42a1c89SJed Brown + snes - nonlinear solver
49920910c330SBarry Smith . U - the current state at which to evaluate the residual
4993d42a1c89SJed Brown - ctx - user context, must be a TS
49940f5c6efeSJed Brown 
49950f5c6efeSJed Brown    Output Parameter:
49960f5c6efeSJed Brown . F - the nonlinear residual
49970f5c6efeSJed Brown 
49980f5c6efeSJed Brown    Notes:
49990f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50000f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
50010f5c6efeSJed Brown 
50020f5c6efeSJed Brown    Level: advanced
50030f5c6efeSJed Brown 
50040f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
50050f5c6efeSJed Brown @*/
50060910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
50070f5c6efeSJed Brown {
50080f5c6efeSJed Brown   TS             ts = (TS)ctx;
50090f5c6efeSJed Brown   PetscErrorCode ierr;
50100f5c6efeSJed Brown 
50110f5c6efeSJed Brown   PetscFunctionBegin;
50120f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50130910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50140f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
50150f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
50160910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
50170f5c6efeSJed Brown   PetscFunctionReturn(0);
50180f5c6efeSJed Brown }
50190f5c6efeSJed Brown 
50200f5c6efeSJed Brown /*@
50210f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
50220f5c6efeSJed Brown 
50230f5c6efeSJed Brown    Collective on SNES
50240f5c6efeSJed Brown 
50250f5c6efeSJed Brown    Input Parameter:
50260f5c6efeSJed Brown + snes - nonlinear solver
50270910c330SBarry Smith . U - the current state at which to evaluate the residual
50280f5c6efeSJed Brown - ctx - user context, must be a TS
50290f5c6efeSJed Brown 
50300f5c6efeSJed Brown    Output Parameter:
50310f5c6efeSJed Brown + A - the Jacobian
50320f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
50330f5c6efeSJed Brown - flag - indicates any structure change in the matrix
50340f5c6efeSJed Brown 
50350f5c6efeSJed Brown    Notes:
50360f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50370f5c6efeSJed Brown 
50380f5c6efeSJed Brown    Level: developer
50390f5c6efeSJed Brown 
50400f5c6efeSJed Brown .seealso: SNESSetJacobian()
50410f5c6efeSJed Brown @*/
5042d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
50430f5c6efeSJed Brown {
50440f5c6efeSJed Brown   TS             ts = (TS)ctx;
50450f5c6efeSJed Brown   PetscErrorCode ierr;
50460f5c6efeSJed Brown 
50470f5c6efeSJed Brown   PetscFunctionBegin;
50480f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50490910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50500f5c6efeSJed Brown   PetscValidPointer(A,3);
505194ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
50520f5c6efeSJed Brown   PetscValidPointer(B,4);
505394ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
50540f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5055d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
50560f5c6efeSJed Brown   PetscFunctionReturn(0);
50570f5c6efeSJed Brown }
5058325fc9f4SBarry Smith 
50590e4ef248SJed Brown /*@C
50609ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
50610e4ef248SJed Brown 
50620e4ef248SJed Brown    Collective on TS
50630e4ef248SJed Brown 
50640e4ef248SJed Brown    Input Arguments:
50650e4ef248SJed Brown +  ts - time stepping context
50660e4ef248SJed Brown .  t - time at which to evaluate
50670910c330SBarry Smith .  U - state at which to evaluate
50680e4ef248SJed Brown -  ctx - context
50690e4ef248SJed Brown 
50700e4ef248SJed Brown    Output Arguments:
50710e4ef248SJed Brown .  F - right hand side
50720e4ef248SJed Brown 
50730e4ef248SJed Brown    Level: intermediate
50740e4ef248SJed Brown 
50750e4ef248SJed Brown    Notes:
50760e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
50770e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
50780e4ef248SJed Brown 
50790e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
50800e4ef248SJed Brown @*/
50810910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
50820e4ef248SJed Brown {
50830e4ef248SJed Brown   PetscErrorCode ierr;
50840e4ef248SJed Brown   Mat            Arhs,Brhs;
50850e4ef248SJed Brown 
50860e4ef248SJed Brown   PetscFunctionBegin;
50870e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
5088d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
50890910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
50900e4ef248SJed Brown   PetscFunctionReturn(0);
50910e4ef248SJed Brown }
50920e4ef248SJed Brown 
50930e4ef248SJed Brown /*@C
50940e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
50950e4ef248SJed Brown 
50960e4ef248SJed Brown    Collective on TS
50970e4ef248SJed Brown 
50980e4ef248SJed Brown    Input Arguments:
50990e4ef248SJed Brown +  ts - time stepping context
51000e4ef248SJed Brown .  t - time at which to evaluate
51010910c330SBarry Smith .  U - state at which to evaluate
51020e4ef248SJed Brown -  ctx - context
51030e4ef248SJed Brown 
51040e4ef248SJed Brown    Output Arguments:
51050e4ef248SJed Brown +  A - pointer to operator
51060e4ef248SJed Brown .  B - pointer to preconditioning matrix
51070e4ef248SJed Brown -  flg - matrix structure flag
51080e4ef248SJed Brown 
51090e4ef248SJed Brown    Level: intermediate
51100e4ef248SJed Brown 
51110e4ef248SJed Brown    Notes:
51120e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
51130e4ef248SJed Brown 
51140e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
51150e4ef248SJed Brown @*/
5116d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
51170e4ef248SJed Brown {
51180e4ef248SJed Brown   PetscFunctionBegin;
51190e4ef248SJed Brown   PetscFunctionReturn(0);
51200e4ef248SJed Brown }
51210e4ef248SJed Brown 
51220026cea9SSean Farley /*@C
51230026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
51240026cea9SSean Farley 
51250026cea9SSean Farley    Collective on TS
51260026cea9SSean Farley 
51270026cea9SSean Farley    Input Arguments:
51280026cea9SSean Farley +  ts - time stepping context
51290026cea9SSean Farley .  t - time at which to evaluate
51300910c330SBarry Smith .  U - state at which to evaluate
51310910c330SBarry Smith .  Udot - time derivative of state vector
51320026cea9SSean Farley -  ctx - context
51330026cea9SSean Farley 
51340026cea9SSean Farley    Output Arguments:
51350026cea9SSean Farley .  F - left hand side
51360026cea9SSean Farley 
51370026cea9SSean Farley    Level: intermediate
51380026cea9SSean Farley 
51390026cea9SSean Farley    Notes:
51400910c330SBarry 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
51410026cea9SSean Farley    user is required to write their own TSComputeIFunction.
51420026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
51430026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
51440026cea9SSean Farley 
51459ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
51469ae8fd06SBarry Smith 
51479ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
51480026cea9SSean Farley @*/
51490910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
51500026cea9SSean Farley {
51510026cea9SSean Farley   PetscErrorCode ierr;
51520026cea9SSean Farley   Mat            A,B;
51530026cea9SSean Farley 
51540026cea9SSean Farley   PetscFunctionBegin;
51550298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5156d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
51570910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
51580026cea9SSean Farley   PetscFunctionReturn(0);
51590026cea9SSean Farley }
51600026cea9SSean Farley 
51610026cea9SSean Farley /*@C
5162b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
51630026cea9SSean Farley 
51640026cea9SSean Farley    Collective on TS
51650026cea9SSean Farley 
51660026cea9SSean Farley    Input Arguments:
51670026cea9SSean Farley +  ts - time stepping context
51680026cea9SSean Farley .  t - time at which to evaluate
51690910c330SBarry Smith .  U - state at which to evaluate
51700910c330SBarry Smith .  Udot - time derivative of state vector
51710026cea9SSean Farley .  shift - shift to apply
51720026cea9SSean Farley -  ctx - context
51730026cea9SSean Farley 
51740026cea9SSean Farley    Output Arguments:
51750026cea9SSean Farley +  A - pointer to operator
51760026cea9SSean Farley .  B - pointer to preconditioning matrix
51770026cea9SSean Farley -  flg - matrix structure flag
51780026cea9SSean Farley 
5179b41af12eSJed Brown    Level: advanced
51800026cea9SSean Farley 
51810026cea9SSean Farley    Notes:
51820026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
51830026cea9SSean Farley 
5184b41af12eSJed Brown    It is only appropriate for problems of the form
5185b41af12eSJed Brown 
5186b41af12eSJed Brown $     M Udot = F(U,t)
5187b41af12eSJed Brown 
5188b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5189b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5190b41af12eSJed Brown   an implicit operator of the form
5191b41af12eSJed Brown 
5192b41af12eSJed Brown $    shift*M + J
5193b41af12eSJed Brown 
5194b41af12eSJed 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
5195b41af12eSJed Brown   a copy of M or reassemble it when requested.
5196b41af12eSJed Brown 
51970026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
51980026cea9SSean Farley @*/
5199d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
52000026cea9SSean Farley {
5201b41af12eSJed Brown   PetscErrorCode ierr;
5202b41af12eSJed Brown 
52030026cea9SSean Farley   PetscFunctionBegin;
520494ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5205b41af12eSJed Brown   ts->ijacobian.shift = shift;
52060026cea9SSean Farley   PetscFunctionReturn(0);
52070026cea9SSean Farley }
5208b41af12eSJed Brown 
5209e817cc15SEmil Constantinescu /*@
5210e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5211e817cc15SEmil Constantinescu 
5212e817cc15SEmil Constantinescu    Not Collective
5213e817cc15SEmil Constantinescu 
5214e817cc15SEmil Constantinescu    Input Parameter:
5215e817cc15SEmil Constantinescu .  ts - the TS context
5216e817cc15SEmil Constantinescu 
5217e817cc15SEmil Constantinescu    Output Parameter:
52184e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5219e817cc15SEmil Constantinescu 
5220e817cc15SEmil Constantinescu    Level: beginner
5221e817cc15SEmil Constantinescu 
5222e817cc15SEmil Constantinescu .keywords: TS, equation type
5223e817cc15SEmil Constantinescu 
5224e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5225e817cc15SEmil Constantinescu @*/
5226e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5227e817cc15SEmil Constantinescu {
5228e817cc15SEmil Constantinescu   PetscFunctionBegin;
5229e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5230e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5231e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5232e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5233e817cc15SEmil Constantinescu }
5234e817cc15SEmil Constantinescu 
5235e817cc15SEmil Constantinescu /*@
5236e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5237e817cc15SEmil Constantinescu 
5238e817cc15SEmil Constantinescu    Not Collective
5239e817cc15SEmil Constantinescu 
5240e817cc15SEmil Constantinescu    Input Parameter:
5241e817cc15SEmil Constantinescu +  ts - the TS context
52421297b224SEmil Constantinescu -  equation_type - see TSEquationType
5243e817cc15SEmil Constantinescu 
5244e817cc15SEmil Constantinescu    Level: advanced
5245e817cc15SEmil Constantinescu 
5246e817cc15SEmil Constantinescu .keywords: TS, equation type
5247e817cc15SEmil Constantinescu 
5248e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5249e817cc15SEmil Constantinescu @*/
5250e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5251e817cc15SEmil Constantinescu {
5252e817cc15SEmil Constantinescu   PetscFunctionBegin;
5253e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5254e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5255e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5256e817cc15SEmil Constantinescu }
52570026cea9SSean Farley 
52584af1b03aSJed Brown /*@
52594af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
52604af1b03aSJed Brown 
52614af1b03aSJed Brown    Not Collective
52624af1b03aSJed Brown 
52634af1b03aSJed Brown    Input Parameter:
52644af1b03aSJed Brown .  ts - the TS context
52654af1b03aSJed Brown 
52664af1b03aSJed Brown    Output Parameter:
52674af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
52684af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
52694af1b03aSJed Brown 
5270487e0bb9SJed Brown    Level: beginner
52714af1b03aSJed Brown 
5272cd652676SJed Brown    Notes:
5273cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
52744af1b03aSJed Brown 
52754af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
52764af1b03aSJed Brown 
52774af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
52784af1b03aSJed Brown @*/
52794af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
52804af1b03aSJed Brown {
52814af1b03aSJed Brown   PetscFunctionBegin;
52824af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52834af1b03aSJed Brown   PetscValidPointer(reason,2);
52844af1b03aSJed Brown   *reason = ts->reason;
52854af1b03aSJed Brown   PetscFunctionReturn(0);
52864af1b03aSJed Brown }
52874af1b03aSJed Brown 
5288d6ad946cSShri Abhyankar /*@
5289d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5290d6ad946cSShri Abhyankar 
5291d6ad946cSShri Abhyankar    Not Collective
5292d6ad946cSShri Abhyankar 
5293d6ad946cSShri Abhyankar    Input Parameter:
5294d6ad946cSShri Abhyankar +  ts - the TS context
5295d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5296d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5297d6ad946cSShri Abhyankar 
5298f5abba47SShri Abhyankar    Level: advanced
5299d6ad946cSShri Abhyankar 
5300d6ad946cSShri Abhyankar    Notes:
5301d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
5302d6ad946cSShri Abhyankar 
5303d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
5304d6ad946cSShri Abhyankar 
5305d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5306d6ad946cSShri Abhyankar @*/
5307d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5308d6ad946cSShri Abhyankar {
5309d6ad946cSShri Abhyankar   PetscFunctionBegin;
5310d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5311d6ad946cSShri Abhyankar   ts->reason = reason;
5312d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5313d6ad946cSShri Abhyankar }
5314d6ad946cSShri Abhyankar 
5315cc708dedSBarry Smith /*@
5316cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5317cc708dedSBarry Smith 
5318cc708dedSBarry Smith    Not Collective
5319cc708dedSBarry Smith 
5320cc708dedSBarry Smith    Input Parameter:
5321cc708dedSBarry Smith .  ts - the TS context
5322cc708dedSBarry Smith 
5323cc708dedSBarry Smith    Output Parameter:
532463e21af5SBarry Smith .  ftime - the final time. This time corresponds to the final time set with TSSetDuration()
5325cc708dedSBarry Smith 
5326487e0bb9SJed Brown    Level: beginner
5327cc708dedSBarry Smith 
5328cc708dedSBarry Smith    Notes:
5329cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5330cc708dedSBarry Smith 
5331cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
5332cc708dedSBarry Smith 
5333cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5334cc708dedSBarry Smith @*/
5335cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5336cc708dedSBarry Smith {
5337cc708dedSBarry Smith   PetscFunctionBegin;
5338cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5339cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5340cc708dedSBarry Smith   *ftime = ts->solvetime;
5341cc708dedSBarry Smith   PetscFunctionReturn(0);
5342cc708dedSBarry Smith }
5343cc708dedSBarry Smith 
53442c18e0fdSBarry Smith /*@
53452c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
53462c18e0fdSBarry Smith 
53472c18e0fdSBarry Smith    Not Collective
53482c18e0fdSBarry Smith 
53492c18e0fdSBarry Smith    Input Parameter:
53502c18e0fdSBarry Smith .  ts - the TS context
53512c18e0fdSBarry Smith 
53522c18e0fdSBarry Smith    Output Parameter:
53532c18e0fdSBarry Smith .  steps - the number of steps
53542c18e0fdSBarry Smith 
53552c18e0fdSBarry Smith    Level: beginner
53562c18e0fdSBarry Smith 
53572c18e0fdSBarry Smith    Notes:
53582c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
53592c18e0fdSBarry Smith 
53602c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
53612c18e0fdSBarry Smith 
53622c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
53632c18e0fdSBarry Smith @*/
53642c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
53652c18e0fdSBarry Smith {
53662c18e0fdSBarry Smith   PetscFunctionBegin;
53672c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53682c18e0fdSBarry Smith   PetscValidPointer(steps,2);
53692c18e0fdSBarry Smith   *steps = ts->total_steps;
53702c18e0fdSBarry Smith   PetscFunctionReturn(0);
53712c18e0fdSBarry Smith }
53722c18e0fdSBarry Smith 
53739f67acb7SJed Brown /*@
53745ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
53759f67acb7SJed Brown    used by the time integrator.
53769f67acb7SJed Brown 
53779f67acb7SJed Brown    Not Collective
53789f67acb7SJed Brown 
53799f67acb7SJed Brown    Input Parameter:
53809f67acb7SJed Brown .  ts - TS context
53819f67acb7SJed Brown 
53829f67acb7SJed Brown    Output Parameter:
53839f67acb7SJed Brown .  nits - number of nonlinear iterations
53849f67acb7SJed Brown 
53859f67acb7SJed Brown    Notes:
53869f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53879f67acb7SJed Brown 
53889f67acb7SJed Brown    Level: intermediate
53899f67acb7SJed Brown 
53909f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
53919f67acb7SJed Brown 
53925ef26d82SJed Brown .seealso:  TSGetKSPIterations()
53939f67acb7SJed Brown @*/
53945ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
53959f67acb7SJed Brown {
53969f67acb7SJed Brown   PetscFunctionBegin;
53979f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53989f67acb7SJed Brown   PetscValidIntPointer(nits,2);
53995ef26d82SJed Brown   *nits = ts->snes_its;
54009f67acb7SJed Brown   PetscFunctionReturn(0);
54019f67acb7SJed Brown }
54029f67acb7SJed Brown 
54039f67acb7SJed Brown /*@
54045ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
54059f67acb7SJed Brown    used by the time integrator.
54069f67acb7SJed Brown 
54079f67acb7SJed Brown    Not Collective
54089f67acb7SJed Brown 
54099f67acb7SJed Brown    Input Parameter:
54109f67acb7SJed Brown .  ts - TS context
54119f67acb7SJed Brown 
54129f67acb7SJed Brown    Output Parameter:
54139f67acb7SJed Brown .  lits - number of linear iterations
54149f67acb7SJed Brown 
54159f67acb7SJed Brown    Notes:
54169f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
54179f67acb7SJed Brown 
54189f67acb7SJed Brown    Level: intermediate
54199f67acb7SJed Brown 
54209f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
54219f67acb7SJed Brown 
54225ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
54239f67acb7SJed Brown @*/
54245ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
54259f67acb7SJed Brown {
54269f67acb7SJed Brown   PetscFunctionBegin;
54279f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54289f67acb7SJed Brown   PetscValidIntPointer(lits,2);
54295ef26d82SJed Brown   *lits = ts->ksp_its;
54309f67acb7SJed Brown   PetscFunctionReturn(0);
54319f67acb7SJed Brown }
54329f67acb7SJed Brown 
5433cef5090cSJed Brown /*@
5434cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5435cef5090cSJed Brown 
5436cef5090cSJed Brown    Not Collective
5437cef5090cSJed Brown 
5438cef5090cSJed Brown    Input Parameter:
5439cef5090cSJed Brown .  ts - TS context
5440cef5090cSJed Brown 
5441cef5090cSJed Brown    Output Parameter:
5442cef5090cSJed Brown .  rejects - number of steps rejected
5443cef5090cSJed Brown 
5444cef5090cSJed Brown    Notes:
5445cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5446cef5090cSJed Brown 
5447cef5090cSJed Brown    Level: intermediate
5448cef5090cSJed Brown 
5449cef5090cSJed Brown .keywords: TS, get, number
5450cef5090cSJed Brown 
54515ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5452cef5090cSJed Brown @*/
5453cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5454cef5090cSJed Brown {
5455cef5090cSJed Brown   PetscFunctionBegin;
5456cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5457cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5458cef5090cSJed Brown   *rejects = ts->reject;
5459cef5090cSJed Brown   PetscFunctionReturn(0);
5460cef5090cSJed Brown }
5461cef5090cSJed Brown 
5462cef5090cSJed Brown /*@
5463cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5464cef5090cSJed Brown 
5465cef5090cSJed Brown    Not Collective
5466cef5090cSJed Brown 
5467cef5090cSJed Brown    Input Parameter:
5468cef5090cSJed Brown .  ts - TS context
5469cef5090cSJed Brown 
5470cef5090cSJed Brown    Output Parameter:
5471cef5090cSJed Brown .  fails - number of failed nonlinear solves
5472cef5090cSJed Brown 
5473cef5090cSJed Brown    Notes:
5474cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5475cef5090cSJed Brown 
5476cef5090cSJed Brown    Level: intermediate
5477cef5090cSJed Brown 
5478cef5090cSJed Brown .keywords: TS, get, number
5479cef5090cSJed Brown 
54805ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5481cef5090cSJed Brown @*/
5482cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5483cef5090cSJed Brown {
5484cef5090cSJed Brown   PetscFunctionBegin;
5485cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5486cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5487cef5090cSJed Brown   *fails = ts->num_snes_failures;
5488cef5090cSJed Brown   PetscFunctionReturn(0);
5489cef5090cSJed Brown }
5490cef5090cSJed Brown 
5491cef5090cSJed Brown /*@
5492cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5493cef5090cSJed Brown 
5494cef5090cSJed Brown    Not Collective
5495cef5090cSJed Brown 
5496cef5090cSJed Brown    Input Parameter:
5497cef5090cSJed Brown +  ts - TS context
5498cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5499cef5090cSJed Brown 
5500cef5090cSJed Brown    Notes:
5501cef5090cSJed Brown    The counter is reset to zero for each step
5502cef5090cSJed Brown 
5503cef5090cSJed Brown    Options Database Key:
5504cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5505cef5090cSJed Brown 
5506cef5090cSJed Brown    Level: intermediate
5507cef5090cSJed Brown 
5508cef5090cSJed Brown .keywords: TS, set, maximum, number
5509cef5090cSJed Brown 
55105ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5511cef5090cSJed Brown @*/
5512cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5513cef5090cSJed Brown {
5514cef5090cSJed Brown   PetscFunctionBegin;
5515cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5516cef5090cSJed Brown   ts->max_reject = rejects;
5517cef5090cSJed Brown   PetscFunctionReturn(0);
5518cef5090cSJed Brown }
5519cef5090cSJed Brown 
5520cef5090cSJed Brown /*@
5521cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5522cef5090cSJed Brown 
5523cef5090cSJed Brown    Not Collective
5524cef5090cSJed Brown 
5525cef5090cSJed Brown    Input Parameter:
5526cef5090cSJed Brown +  ts - TS context
5527cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5528cef5090cSJed Brown 
5529cef5090cSJed Brown    Notes:
5530cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5531cef5090cSJed Brown 
5532cef5090cSJed Brown    Options Database Key:
5533cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5534cef5090cSJed Brown 
5535cef5090cSJed Brown    Level: intermediate
5536cef5090cSJed Brown 
5537cef5090cSJed Brown .keywords: TS, set, maximum, number
5538cef5090cSJed Brown 
55395ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5540cef5090cSJed Brown @*/
5541cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5542cef5090cSJed Brown {
5543cef5090cSJed Brown   PetscFunctionBegin;
5544cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5545cef5090cSJed Brown   ts->max_snes_failures = fails;
5546cef5090cSJed Brown   PetscFunctionReturn(0);
5547cef5090cSJed Brown }
5548cef5090cSJed Brown 
5549cef5090cSJed Brown /*@
5550cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5551cef5090cSJed Brown 
5552cef5090cSJed Brown    Not Collective
5553cef5090cSJed Brown 
5554cef5090cSJed Brown    Input Parameter:
5555cef5090cSJed Brown +  ts - TS context
5556cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5557cef5090cSJed Brown 
5558cef5090cSJed Brown    Options Database Key:
5559cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5560cef5090cSJed Brown 
5561cef5090cSJed Brown    Level: intermediate
5562cef5090cSJed Brown 
5563cef5090cSJed Brown .keywords: TS, set, error
5564cef5090cSJed Brown 
55655ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5566cef5090cSJed Brown @*/
5567cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5568cef5090cSJed Brown {
5569cef5090cSJed Brown   PetscFunctionBegin;
5570cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5571cef5090cSJed Brown   ts->errorifstepfailed = err;
5572cef5090cSJed Brown   PetscFunctionReturn(0);
5573cef5090cSJed Brown }
5574cef5090cSJed Brown 
5575fb1732b5SBarry Smith /*@C
5576fde5950dSBarry 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
5577fb1732b5SBarry Smith 
5578fb1732b5SBarry Smith    Collective on TS
5579fb1732b5SBarry Smith 
5580fb1732b5SBarry Smith    Input Parameters:
5581fb1732b5SBarry Smith +  ts - the TS context
5582fb1732b5SBarry Smith .  step - current time-step
5583fb1732b5SBarry Smith .  ptime - current time
55840910c330SBarry Smith .  u - current state
5585721cd6eeSBarry Smith -  vf - viewer and its format
5586fb1732b5SBarry Smith 
5587fb1732b5SBarry Smith    Level: intermediate
5588fb1732b5SBarry Smith 
5589fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5590fb1732b5SBarry Smith 
5591fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5592fb1732b5SBarry Smith @*/
5593721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5594fb1732b5SBarry Smith {
5595fb1732b5SBarry Smith   PetscErrorCode ierr;
5596fb1732b5SBarry Smith 
5597fb1732b5SBarry Smith   PetscFunctionBegin;
5598721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5599721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5600721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5601ed81e22dSJed Brown   PetscFunctionReturn(0);
5602ed81e22dSJed Brown }
5603ed81e22dSJed Brown 
5604ed81e22dSJed Brown /*@C
5605ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5606ed81e22dSJed Brown 
5607ed81e22dSJed Brown    Collective on TS
5608ed81e22dSJed Brown 
5609ed81e22dSJed Brown    Input Parameters:
5610ed81e22dSJed Brown +  ts - the TS context
5611ed81e22dSJed Brown .  step - current time-step
5612ed81e22dSJed Brown .  ptime - current time
56130910c330SBarry Smith .  u - current state
5614ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5615ed81e22dSJed Brown 
5616ed81e22dSJed Brown    Level: intermediate
5617ed81e22dSJed Brown 
5618ed81e22dSJed Brown    Notes:
5619ed81e22dSJed 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.
5620ed81e22dSJed Brown    These are named according to the file name template.
5621ed81e22dSJed Brown 
5622ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5623ed81e22dSJed Brown 
5624ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5625ed81e22dSJed Brown 
5626ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5627ed81e22dSJed Brown @*/
56280910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5629ed81e22dSJed Brown {
5630ed81e22dSJed Brown   PetscErrorCode ierr;
5631ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5632ed81e22dSJed Brown   PetscViewer    viewer;
5633ed81e22dSJed Brown 
5634ed81e22dSJed Brown   PetscFunctionBegin;
563563e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
56368caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5637ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
56380910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5639ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5640ed81e22dSJed Brown   PetscFunctionReturn(0);
5641ed81e22dSJed Brown }
5642ed81e22dSJed Brown 
5643ed81e22dSJed Brown /*@C
5644ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5645ed81e22dSJed Brown 
5646ed81e22dSJed Brown    Collective on TS
5647ed81e22dSJed Brown 
5648ed81e22dSJed Brown    Input Parameters:
5649ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5650ed81e22dSJed Brown 
5651ed81e22dSJed Brown    Level: intermediate
5652ed81e22dSJed Brown 
5653ed81e22dSJed Brown    Note:
5654ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5655ed81e22dSJed Brown 
5656ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5657ed81e22dSJed Brown 
5658ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5659ed81e22dSJed Brown @*/
5660ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5661ed81e22dSJed Brown {
5662ed81e22dSJed Brown   PetscErrorCode ierr;
5663ed81e22dSJed Brown 
5664ed81e22dSJed Brown   PetscFunctionBegin;
5665ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5666fb1732b5SBarry Smith   PetscFunctionReturn(0);
5667fb1732b5SBarry Smith }
5668fb1732b5SBarry Smith 
566984df9cb4SJed Brown /*@
5670552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
567184df9cb4SJed Brown 
5672ed81e22dSJed Brown    Collective on TS if controller has not been created yet
567384df9cb4SJed Brown 
567484df9cb4SJed Brown    Input Arguments:
5675ed81e22dSJed Brown .  ts - time stepping context
567684df9cb4SJed Brown 
567784df9cb4SJed Brown    Output Arguments:
5678ed81e22dSJed Brown .  adapt - adaptive controller
567984df9cb4SJed Brown 
568084df9cb4SJed Brown    Level: intermediate
568184df9cb4SJed Brown 
5682ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
568384df9cb4SJed Brown @*/
5684552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
568584df9cb4SJed Brown {
568684df9cb4SJed Brown   PetscErrorCode ierr;
568784df9cb4SJed Brown 
568884df9cb4SJed Brown   PetscFunctionBegin;
568984df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5690bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
569184df9cb4SJed Brown   if (!ts->adapt) {
5692ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
56933bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
56941c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
569584df9cb4SJed Brown   }
5696bec58848SLisandro Dalcin   *adapt = ts->adapt;
569784df9cb4SJed Brown   PetscFunctionReturn(0);
569884df9cb4SJed Brown }
5699d6ebe24aSShri Abhyankar 
57001c3436cfSJed Brown /*@
57011c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
57021c3436cfSJed Brown 
57031c3436cfSJed Brown    Logically Collective
57041c3436cfSJed Brown 
57051c3436cfSJed Brown    Input Arguments:
57061c3436cfSJed Brown +  ts - time integration context
57071c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
57080298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
57091c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
57100298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
57111c3436cfSJed Brown 
5712a3cdaa26SBarry Smith    Options Database keys:
5713a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5714a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5715a3cdaa26SBarry Smith 
57163ff766beSShri Abhyankar    Notes:
57173ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
57183ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
57193ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
57203ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
57213ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
57223ff766beSShri Abhyankar    differential variables.
57233ff766beSShri Abhyankar 
57241c3436cfSJed Brown    Level: beginner
57251c3436cfSJed Brown 
5726c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
57271c3436cfSJed Brown @*/
57281c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
57291c3436cfSJed Brown {
57301c3436cfSJed Brown   PetscErrorCode ierr;
57311c3436cfSJed Brown 
57321c3436cfSJed Brown   PetscFunctionBegin;
5733c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
57341c3436cfSJed Brown   if (vatol) {
57351c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
57361c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
57371c3436cfSJed Brown     ts->vatol = vatol;
57381c3436cfSJed Brown   }
5739c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
57401c3436cfSJed Brown   if (vrtol) {
57411c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
57421c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
57431c3436cfSJed Brown     ts->vrtol = vrtol;
57441c3436cfSJed Brown   }
57451c3436cfSJed Brown   PetscFunctionReturn(0);
57461c3436cfSJed Brown }
57471c3436cfSJed Brown 
5748c5033834SJed Brown /*@
5749c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5750c5033834SJed Brown 
5751c5033834SJed Brown    Logically Collective
5752c5033834SJed Brown 
5753c5033834SJed Brown    Input Arguments:
5754c5033834SJed Brown .  ts - time integration context
5755c5033834SJed Brown 
5756c5033834SJed Brown    Output Arguments:
57570298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
57580298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
57590298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
57600298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5761c5033834SJed Brown 
5762c5033834SJed Brown    Level: beginner
5763c5033834SJed Brown 
5764c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5765c5033834SJed Brown @*/
5766c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5767c5033834SJed Brown {
5768c5033834SJed Brown   PetscFunctionBegin;
5769c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5770c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5771c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5772c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5773c5033834SJed Brown   PetscFunctionReturn(0);
5774c5033834SJed Brown }
5775c5033834SJed Brown 
57769c6b16b5SShri Abhyankar /*@
5777a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
57789c6b16b5SShri Abhyankar 
57799c6b16b5SShri Abhyankar    Collective on TS
57809c6b16b5SShri Abhyankar 
57819c6b16b5SShri Abhyankar    Input Arguments:
57829c6b16b5SShri Abhyankar +  ts - time stepping context
5783a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5784a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
57859c6b16b5SShri Abhyankar 
57869c6b16b5SShri Abhyankar    Output Arguments:
57877453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57887453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
57897453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57909c6b16b5SShri Abhyankar 
57919c6b16b5SShri Abhyankar    Level: developer
57929c6b16b5SShri Abhyankar 
5793deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
57949c6b16b5SShri Abhyankar @*/
57957453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57969c6b16b5SShri Abhyankar {
57979c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
57989c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
57997453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
58007453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
58019c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
58027453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
58037453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
58047453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
58059c6b16b5SShri Abhyankar 
58069c6b16b5SShri Abhyankar   PetscFunctionBegin;
58079c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5808a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5809a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5810a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5811a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5812a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5813a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
58148a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
58158a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5816a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
58179c6b16b5SShri Abhyankar 
58189c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
58199c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
58209c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
58219c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
58229c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
58237453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
58247453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
58257453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
58269c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
58279c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
58289c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58299c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58309c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
58317453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58327453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
58337453f775SEmil Constantinescu       if(tola>0.){
58347453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58357453f775SEmil Constantinescu         na_loc++;
58367453f775SEmil Constantinescu       }
58377453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58387453f775SEmil Constantinescu       if(tolr>0.){
58397453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58407453f775SEmil Constantinescu         nr_loc++;
58417453f775SEmil Constantinescu       }
58427453f775SEmil Constantinescu       tol=tola+tolr;
58437453f775SEmil Constantinescu       if(tol>0.){
58447453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58457453f775SEmil Constantinescu         n_loc++;
58467453f775SEmil Constantinescu       }
58479c6b16b5SShri Abhyankar     }
58489c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58499c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58509c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
58519c6b16b5SShri Abhyankar     const PetscScalar *atol;
58529c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58539c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
58547453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58557453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
58567453f775SEmil Constantinescu       if(tola>0.){
58577453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58587453f775SEmil Constantinescu         na_loc++;
58597453f775SEmil Constantinescu       }
58607453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58617453f775SEmil Constantinescu       if(tolr>0.){
58627453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58637453f775SEmil Constantinescu         nr_loc++;
58647453f775SEmil Constantinescu       }
58657453f775SEmil Constantinescu       tol=tola+tolr;
58667453f775SEmil Constantinescu       if(tol>0.){
58677453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58687453f775SEmil Constantinescu         n_loc++;
58697453f775SEmil Constantinescu       }
58709c6b16b5SShri Abhyankar     }
58719c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58729c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
58739c6b16b5SShri Abhyankar     const PetscScalar *rtol;
58749c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58759c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
58767453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58777453f775SEmil Constantinescu       tola = ts->atol;
58787453f775SEmil Constantinescu       if(tola>0.){
58797453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58807453f775SEmil Constantinescu         na_loc++;
58817453f775SEmil Constantinescu       }
58827453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58837453f775SEmil Constantinescu       if(tolr>0.){
58847453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58857453f775SEmil Constantinescu         nr_loc++;
58867453f775SEmil Constantinescu       }
58877453f775SEmil Constantinescu       tol=tola+tolr;
58887453f775SEmil Constantinescu       if(tol>0.){
58897453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58907453f775SEmil Constantinescu         n_loc++;
58917453f775SEmil Constantinescu       }
58929c6b16b5SShri Abhyankar     }
58939c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58949c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
58959c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
58967453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58977453f775SEmil Constantinescu      tola = ts->atol;
58987453f775SEmil Constantinescu       if(tola>0.){
58997453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
59007453f775SEmil Constantinescu         na_loc++;
59017453f775SEmil Constantinescu       }
59027453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59037453f775SEmil Constantinescu       if(tolr>0.){
59047453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
59057453f775SEmil Constantinescu         nr_loc++;
59067453f775SEmil Constantinescu       }
59077453f775SEmil Constantinescu       tol=tola+tolr;
59087453f775SEmil Constantinescu       if(tol>0.){
59097453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
59107453f775SEmil Constantinescu         n_loc++;
59117453f775SEmil Constantinescu       }
59129c6b16b5SShri Abhyankar     }
59139c6b16b5SShri Abhyankar   }
59149c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
59159c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
59169c6b16b5SShri Abhyankar 
59177453f775SEmil Constantinescu   err_loc[0] = sum;
59187453f775SEmil Constantinescu   err_loc[1] = suma;
59197453f775SEmil Constantinescu   err_loc[2] = sumr;
59207453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
59217453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
59227453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
59237453f775SEmil Constantinescu 
5924a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
59257453f775SEmil Constantinescu 
59267453f775SEmil Constantinescu   gsum   = err_glb[0];
59277453f775SEmil Constantinescu   gsuma  = err_glb[1];
59287453f775SEmil Constantinescu   gsumr  = err_glb[2];
59297453f775SEmil Constantinescu   n_glb  = err_glb[3];
59307453f775SEmil Constantinescu   na_glb = err_glb[4];
59317453f775SEmil Constantinescu   nr_glb = err_glb[5];
59327453f775SEmil Constantinescu 
5933b1316ef9SEmil Constantinescu   *norm  = 0.;
5934b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5935b1316ef9SEmil Constantinescu   *norma = 0.;
5936b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5937b1316ef9SEmil Constantinescu   *normr = 0.;
5938b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
59399c6b16b5SShri Abhyankar 
59409c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
59417453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
59427453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
59439c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
59449c6b16b5SShri Abhyankar }
59459c6b16b5SShri Abhyankar 
59469c6b16b5SShri Abhyankar /*@
5947a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
59489c6b16b5SShri Abhyankar 
59499c6b16b5SShri Abhyankar    Collective on TS
59509c6b16b5SShri Abhyankar 
59519c6b16b5SShri Abhyankar    Input Arguments:
59529c6b16b5SShri Abhyankar +  ts - time stepping context
5953a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5954a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
59559c6b16b5SShri Abhyankar 
59569c6b16b5SShri Abhyankar    Output Arguments:
59577453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
59587453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
59597453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
59609c6b16b5SShri Abhyankar 
59619c6b16b5SShri Abhyankar    Level: developer
59629c6b16b5SShri Abhyankar 
5963deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
59649c6b16b5SShri Abhyankar @*/
59657453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
59669c6b16b5SShri Abhyankar {
59679c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
59687453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
59699c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
59707453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
59717453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
59727453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
59739c6b16b5SShri Abhyankar 
59749c6b16b5SShri Abhyankar   PetscFunctionBegin;
59759c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5976a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5977a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5978a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5979a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5980a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5981a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
59828a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
59838a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5984a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
59859c6b16b5SShri Abhyankar 
59869c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
59879c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
59889c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
59899c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59909c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59917453f775SEmil Constantinescu 
59927453f775SEmil Constantinescu   max=0.;
59937453f775SEmil Constantinescu   maxa=0.;
59947453f775SEmil Constantinescu   maxr=0.;
59957453f775SEmil Constantinescu 
59967453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
59979c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
59989c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59999c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60007453f775SEmil Constantinescu 
60017453f775SEmil Constantinescu     for (i=0; i<n; i++) {
60027453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60037453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
60047453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60057453f775SEmil Constantinescu       tol  = tola+tolr;
60067453f775SEmil Constantinescu       if(tola>0.){
60077453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60087453f775SEmil Constantinescu       }
60097453f775SEmil Constantinescu       if(tolr>0.){
60107453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60117453f775SEmil Constantinescu       }
60127453f775SEmil Constantinescu       if(tol>0.){
60137453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60147453f775SEmil Constantinescu       }
60159c6b16b5SShri Abhyankar     }
60169c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60179c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60189c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
60199c6b16b5SShri Abhyankar     const PetscScalar *atol;
60209c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60217453f775SEmil Constantinescu     for (i=0; i<n; i++) {
60227453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60237453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
60247453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60257453f775SEmil Constantinescu       tol  = tola+tolr;
60267453f775SEmil Constantinescu       if(tola>0.){
60277453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60287453f775SEmil Constantinescu       }
60297453f775SEmil Constantinescu       if(tolr>0.){
60307453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60317453f775SEmil Constantinescu       }
60327453f775SEmil Constantinescu       if(tol>0.){
60337453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60347453f775SEmil Constantinescu       }
60359c6b16b5SShri Abhyankar     }
60369c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60379c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
60389c6b16b5SShri Abhyankar     const PetscScalar *rtol;
60399c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60407453f775SEmil Constantinescu 
60417453f775SEmil Constantinescu     for (i=0; i<n; i++) {
60427453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60437453f775SEmil Constantinescu       tola = ts->atol;
60447453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60457453f775SEmil Constantinescu       tol  = tola+tolr;
60467453f775SEmil Constantinescu       if(tola>0.){
60477453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60487453f775SEmil Constantinescu       }
60497453f775SEmil Constantinescu       if(tolr>0.){
60507453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60517453f775SEmil Constantinescu       }
60527453f775SEmil Constantinescu       if(tol>0.){
60537453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60547453f775SEmil Constantinescu       }
60559c6b16b5SShri Abhyankar     }
60569c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60579c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
60587453f775SEmil Constantinescu 
60597453f775SEmil Constantinescu     for (i=0; i<n; i++) {
60607453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60617453f775SEmil Constantinescu       tola = ts->atol;
60627453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60637453f775SEmil Constantinescu       tol  = tola+tolr;
60647453f775SEmil Constantinescu       if(tola>0.){
60657453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60667453f775SEmil Constantinescu       }
60677453f775SEmil Constantinescu       if(tolr>0.){
60687453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60697453f775SEmil Constantinescu       }
60707453f775SEmil Constantinescu       if(tol>0.){
60717453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60727453f775SEmil Constantinescu       }
60739c6b16b5SShri Abhyankar     }
60749c6b16b5SShri Abhyankar   }
60759c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60769c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60777453f775SEmil Constantinescu   err_loc[0] = max;
60787453f775SEmil Constantinescu   err_loc[1] = maxa;
60797453f775SEmil Constantinescu   err_loc[2] = maxr;
6080a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60817453f775SEmil Constantinescu   gmax   = err_glb[0];
60827453f775SEmil Constantinescu   gmaxa  = err_glb[1];
60837453f775SEmil Constantinescu   gmaxr  = err_glb[2];
60849c6b16b5SShri Abhyankar 
60859c6b16b5SShri Abhyankar   *norm = gmax;
60867453f775SEmil Constantinescu   *norma = gmaxa;
60877453f775SEmil Constantinescu   *normr = gmaxr;
60889c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60897453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60907453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60919c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
60929c6b16b5SShri Abhyankar }
60939c6b16b5SShri Abhyankar 
60941c3436cfSJed Brown /*@
60958a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
60961c3436cfSJed Brown 
60971c3436cfSJed Brown    Collective on TS
60981c3436cfSJed Brown 
60991c3436cfSJed Brown    Input Arguments:
61001c3436cfSJed Brown +  ts - time stepping context
6101a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6102a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6103a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
61047619abb3SShri 
61051c3436cfSJed Brown    Output Arguments:
61068a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
61078a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
61088a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6109a4868fbcSLisandro Dalcin 
6110a4868fbcSLisandro Dalcin    Options Database Keys:
6111a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6112a4868fbcSLisandro Dalcin 
61131c3436cfSJed Brown    Level: developer
61141c3436cfSJed Brown 
61158a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
61161c3436cfSJed Brown @*/
61177453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61181c3436cfSJed Brown {
61198beabaa1SBarry Smith   PetscErrorCode ierr;
61201c3436cfSJed Brown 
61211c3436cfSJed Brown   PetscFunctionBegin;
6122a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
61237453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6124a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
61257453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6126a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
61271c3436cfSJed Brown   PetscFunctionReturn(0);
61281c3436cfSJed Brown }
61291c3436cfSJed Brown 
61308a175baeSEmil Constantinescu 
61318a175baeSEmil Constantinescu /*@
61328a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
61338a175baeSEmil Constantinescu 
61348a175baeSEmil Constantinescu    Collective on TS
61358a175baeSEmil Constantinescu 
61368a175baeSEmil Constantinescu    Input Arguments:
61378a175baeSEmil Constantinescu +  ts - time stepping context
61388a175baeSEmil Constantinescu .  E - error vector
61398a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
61408a175baeSEmil Constantinescu -  Y - state vector, previous time step
61418a175baeSEmil Constantinescu 
61428a175baeSEmil Constantinescu    Output Arguments:
61438a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
61448a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
61458a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
61468a175baeSEmil Constantinescu 
61478a175baeSEmil Constantinescu    Level: developer
61488a175baeSEmil Constantinescu 
61498a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
61508a175baeSEmil Constantinescu @*/
61518a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61528a175baeSEmil Constantinescu {
61538a175baeSEmil Constantinescu   PetscErrorCode    ierr;
61548a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
61558a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
61568a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
61578a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
61588a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
61598a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
61608a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
61618a175baeSEmil Constantinescu 
61628a175baeSEmil Constantinescu   PetscFunctionBegin;
61638a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
61648a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
61658a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
61668a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
61678a175baeSEmil Constantinescu   PetscValidType(E,2);
61688a175baeSEmil Constantinescu   PetscValidType(U,3);
61698a175baeSEmil Constantinescu   PetscValidType(Y,4);
61708a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
61718a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
61728a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
61738a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
61748a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
61758a175baeSEmil Constantinescu 
61768a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
61778a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
61788a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
61798a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
61808a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
61818a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
61828a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
61838a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
61848a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
61858a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
61868a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
61878a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61888a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61898a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
61908a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61918a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61928a175baeSEmil Constantinescu       if(tola>0.){
61938a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61948a175baeSEmil Constantinescu         na_loc++;
61958a175baeSEmil Constantinescu       }
61968a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61978a175baeSEmil Constantinescu       if(tolr>0.){
61988a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61998a175baeSEmil Constantinescu         nr_loc++;
62008a175baeSEmil Constantinescu       }
62018a175baeSEmil Constantinescu       tol=tola+tolr;
62028a175baeSEmil Constantinescu       if(tol>0.){
62038a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62048a175baeSEmil Constantinescu         n_loc++;
62058a175baeSEmil Constantinescu       }
62068a175baeSEmil Constantinescu     }
62078a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62088a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62098a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
62108a175baeSEmil Constantinescu     const PetscScalar *atol;
62118a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62128a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
62138a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62148a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
62158a175baeSEmil Constantinescu       if(tola>0.){
62168a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62178a175baeSEmil Constantinescu         na_loc++;
62188a175baeSEmil Constantinescu       }
62198a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62208a175baeSEmil Constantinescu       if(tolr>0.){
62218a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62228a175baeSEmil Constantinescu         nr_loc++;
62238a175baeSEmil Constantinescu       }
62248a175baeSEmil Constantinescu       tol=tola+tolr;
62258a175baeSEmil Constantinescu       if(tol>0.){
62268a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62278a175baeSEmil Constantinescu         n_loc++;
62288a175baeSEmil Constantinescu       }
62298a175baeSEmil Constantinescu     }
62308a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62318a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
62328a175baeSEmil Constantinescu     const PetscScalar *rtol;
62338a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62348a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
62358a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62368a175baeSEmil Constantinescu       tola = ts->atol;
62378a175baeSEmil Constantinescu       if(tola>0.){
62388a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62398a175baeSEmil Constantinescu         na_loc++;
62408a175baeSEmil Constantinescu       }
62418a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62428a175baeSEmil Constantinescu       if(tolr>0.){
62438a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62448a175baeSEmil Constantinescu         nr_loc++;
62458a175baeSEmil Constantinescu       }
62468a175baeSEmil Constantinescu       tol=tola+tolr;
62478a175baeSEmil Constantinescu       if(tol>0.){
62488a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62498a175baeSEmil Constantinescu         n_loc++;
62508a175baeSEmil Constantinescu       }
62518a175baeSEmil Constantinescu     }
62528a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62538a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
62548a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
62558a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62568a175baeSEmil Constantinescu      tola = ts->atol;
62578a175baeSEmil Constantinescu       if(tola>0.){
62588a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62598a175baeSEmil Constantinescu         na_loc++;
62608a175baeSEmil Constantinescu       }
62618a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62628a175baeSEmil Constantinescu       if(tolr>0.){
62638a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62648a175baeSEmil Constantinescu         nr_loc++;
62658a175baeSEmil Constantinescu       }
62668a175baeSEmil Constantinescu       tol=tola+tolr;
62678a175baeSEmil Constantinescu       if(tol>0.){
62688a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62698a175baeSEmil Constantinescu         n_loc++;
62708a175baeSEmil Constantinescu       }
62718a175baeSEmil Constantinescu     }
62728a175baeSEmil Constantinescu   }
62738a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
62748a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
62758a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
62768a175baeSEmil Constantinescu 
62778a175baeSEmil Constantinescu   err_loc[0] = sum;
62788a175baeSEmil Constantinescu   err_loc[1] = suma;
62798a175baeSEmil Constantinescu   err_loc[2] = sumr;
62808a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
62818a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
62828a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
62838a175baeSEmil Constantinescu 
6284a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62858a175baeSEmil Constantinescu 
62868a175baeSEmil Constantinescu   gsum   = err_glb[0];
62878a175baeSEmil Constantinescu   gsuma  = err_glb[1];
62888a175baeSEmil Constantinescu   gsumr  = err_glb[2];
62898a175baeSEmil Constantinescu   n_glb  = err_glb[3];
62908a175baeSEmil Constantinescu   na_glb = err_glb[4];
62918a175baeSEmil Constantinescu   nr_glb = err_glb[5];
62928a175baeSEmil Constantinescu 
62938a175baeSEmil Constantinescu   *norm  = 0.;
62948a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
62958a175baeSEmil Constantinescu   *norma = 0.;
62968a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
62978a175baeSEmil Constantinescu   *normr = 0.;
62988a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
62998a175baeSEmil Constantinescu 
63008a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
63018a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
63028a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
63038a175baeSEmil Constantinescu   PetscFunctionReturn(0);
63048a175baeSEmil Constantinescu }
63058a175baeSEmil Constantinescu 
63068a175baeSEmil Constantinescu /*@
63078a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
63088a175baeSEmil Constantinescu    Collective on TS
63098a175baeSEmil Constantinescu 
63108a175baeSEmil Constantinescu    Input Arguments:
63118a175baeSEmil Constantinescu +  ts - time stepping context
63128a175baeSEmil Constantinescu .  E - error vector
63138a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
63148a175baeSEmil Constantinescu -  Y - state vector, previous time step
63158a175baeSEmil Constantinescu 
63168a175baeSEmil Constantinescu    Output Arguments:
63178a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
63188a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
63198a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
63208a175baeSEmil Constantinescu 
63218a175baeSEmil Constantinescu    Level: developer
63228a175baeSEmil Constantinescu 
63238a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
63248a175baeSEmil Constantinescu @*/
63258a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
63268a175baeSEmil Constantinescu {
63278a175baeSEmil Constantinescu   PetscErrorCode    ierr;
63288a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
63298a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
63308a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
63318a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
63328a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
63338a175baeSEmil Constantinescu 
63348a175baeSEmil Constantinescu   PetscFunctionBegin;
63358a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63368a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
63378a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
63388a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
63398a175baeSEmil Constantinescu   PetscValidType(E,2);
63408a175baeSEmil Constantinescu   PetscValidType(U,3);
63418a175baeSEmil Constantinescu   PetscValidType(Y,4);
63428a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
63438a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
63448a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
63458a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
63468a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
63478a175baeSEmil Constantinescu 
63488a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
63498a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
63508a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
63518a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
63528a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
63538a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
63548a175baeSEmil Constantinescu 
63558a175baeSEmil Constantinescu   max=0.;
63568a175baeSEmil Constantinescu   maxa=0.;
63578a175baeSEmil Constantinescu   maxr=0.;
63588a175baeSEmil Constantinescu 
63598a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
63608a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
63618a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63628a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63638a175baeSEmil Constantinescu 
63648a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
63658a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63668a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63678a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63688a175baeSEmil Constantinescu       tol  = tola+tolr;
63698a175baeSEmil Constantinescu       if(tola>0.){
63708a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63718a175baeSEmil Constantinescu       }
63728a175baeSEmil Constantinescu       if(tolr>0.){
63738a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63748a175baeSEmil Constantinescu       }
63758a175baeSEmil Constantinescu       if(tol>0.){
63768a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63778a175baeSEmil Constantinescu       }
63788a175baeSEmil Constantinescu     }
63798a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63808a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63818a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
63828a175baeSEmil Constantinescu     const PetscScalar *atol;
63838a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63848a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
63858a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63868a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63878a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63888a175baeSEmil Constantinescu       tol  = tola+tolr;
63898a175baeSEmil Constantinescu       if(tola>0.){
63908a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63918a175baeSEmil Constantinescu       }
63928a175baeSEmil Constantinescu       if(tolr>0.){
63938a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63948a175baeSEmil Constantinescu       }
63958a175baeSEmil Constantinescu       if(tol>0.){
63968a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63978a175baeSEmil Constantinescu       }
63988a175baeSEmil Constantinescu     }
63998a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64008a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
64018a175baeSEmil Constantinescu     const PetscScalar *rtol;
64028a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64038a175baeSEmil Constantinescu 
64048a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64058a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64068a175baeSEmil Constantinescu       tola = ts->atol;
64078a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64088a175baeSEmil Constantinescu       tol  = tola+tolr;
64098a175baeSEmil Constantinescu       if(tola>0.){
64108a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64118a175baeSEmil Constantinescu       }
64128a175baeSEmil Constantinescu       if(tolr>0.){
64138a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64148a175baeSEmil Constantinescu       }
64158a175baeSEmil Constantinescu       if(tol>0.){
64168a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64178a175baeSEmil Constantinescu       }
64188a175baeSEmil Constantinescu     }
64198a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64208a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
64218a175baeSEmil Constantinescu 
64228a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64238a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64248a175baeSEmil Constantinescu       tola = ts->atol;
64258a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64268a175baeSEmil Constantinescu       tol  = tola+tolr;
64278a175baeSEmil Constantinescu       if(tola>0.){
64288a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64298a175baeSEmil Constantinescu       }
64308a175baeSEmil Constantinescu       if(tolr>0.){
64318a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64328a175baeSEmil Constantinescu       }
64338a175baeSEmil Constantinescu       if(tol>0.){
64348a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64358a175baeSEmil Constantinescu       }
64368a175baeSEmil Constantinescu     }
64378a175baeSEmil Constantinescu   }
64388a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
64398a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
64408a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
64418a175baeSEmil Constantinescu   err_loc[0] = max;
64428a175baeSEmil Constantinescu   err_loc[1] = maxa;
64438a175baeSEmil Constantinescu   err_loc[2] = maxr;
6444a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
64458a175baeSEmil Constantinescu   gmax   = err_glb[0];
64468a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
64478a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
64488a175baeSEmil Constantinescu 
64498a175baeSEmil Constantinescu   *norm = gmax;
64508a175baeSEmil Constantinescu   *norma = gmaxa;
64518a175baeSEmil Constantinescu   *normr = gmaxr;
64528a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
64538a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
64548a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
64558a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64568a175baeSEmil Constantinescu }
64578a175baeSEmil Constantinescu 
64588a175baeSEmil Constantinescu /*@
64598a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
64608a175baeSEmil Constantinescu 
64618a175baeSEmil Constantinescu    Collective on TS
64628a175baeSEmil Constantinescu 
64638a175baeSEmil Constantinescu    Input Arguments:
64648a175baeSEmil Constantinescu +  ts - time stepping context
64658a175baeSEmil Constantinescu .  E - error vector
64668a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
64678a175baeSEmil Constantinescu .  Y - state vector, previous time step
64688a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
64698a175baeSEmil Constantinescu 
64708a175baeSEmil Constantinescu    Output Arguments:
64718a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
64728a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
64738a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
64748a175baeSEmil Constantinescu 
64758a175baeSEmil Constantinescu    Options Database Keys:
64768a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
64778a175baeSEmil Constantinescu 
64788a175baeSEmil Constantinescu    Level: developer
64798a175baeSEmil Constantinescu 
64808a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
64818a175baeSEmil Constantinescu @*/
64828a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
64838a175baeSEmil Constantinescu {
64848a175baeSEmil Constantinescu   PetscErrorCode ierr;
64858a175baeSEmil Constantinescu 
64868a175baeSEmil Constantinescu   PetscFunctionBegin;
64878a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
64888a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64898a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
64908a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64918a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
64928a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64938a175baeSEmil Constantinescu }
64948a175baeSEmil Constantinescu 
64958a175baeSEmil Constantinescu 
64968d59e960SJed Brown /*@
64978d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
64988d59e960SJed Brown 
64998d59e960SJed Brown    Logically Collective on TS
65008d59e960SJed Brown 
65018d59e960SJed Brown    Input Arguments:
65028d59e960SJed Brown +  ts - time stepping context
65038d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
65048d59e960SJed Brown 
65058d59e960SJed Brown    Note:
65068d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
65078d59e960SJed Brown 
65088d59e960SJed Brown    Level: intermediate
65098d59e960SJed Brown 
65108d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
65118d59e960SJed Brown @*/
65128d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
65138d59e960SJed Brown {
65148d59e960SJed Brown   PetscFunctionBegin;
65158d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
65168d59e960SJed Brown   ts->cfltime_local = cfltime;
65178d59e960SJed Brown   ts->cfltime       = -1.;
65188d59e960SJed Brown   PetscFunctionReturn(0);
65198d59e960SJed Brown }
65208d59e960SJed Brown 
65218d59e960SJed Brown /*@
65228d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
65238d59e960SJed Brown 
65248d59e960SJed Brown    Collective on TS
65258d59e960SJed Brown 
65268d59e960SJed Brown    Input Arguments:
65278d59e960SJed Brown .  ts - time stepping context
65288d59e960SJed Brown 
65298d59e960SJed Brown    Output Arguments:
65308d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
65318d59e960SJed Brown 
65328d59e960SJed Brown    Level: advanced
65338d59e960SJed Brown 
65348d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
65358d59e960SJed Brown @*/
65368d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
65378d59e960SJed Brown {
65388d59e960SJed Brown   PetscErrorCode ierr;
65398d59e960SJed Brown 
65408d59e960SJed Brown   PetscFunctionBegin;
65418d59e960SJed Brown   if (ts->cfltime < 0) {
6542b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
65438d59e960SJed Brown   }
65448d59e960SJed Brown   *cfltime = ts->cfltime;
65458d59e960SJed Brown   PetscFunctionReturn(0);
65468d59e960SJed Brown }
65478d59e960SJed Brown 
6548d6ebe24aSShri Abhyankar /*@
6549d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6550d6ebe24aSShri Abhyankar 
6551d6ebe24aSShri Abhyankar    Input Parameters:
6552d6ebe24aSShri Abhyankar .  ts   - the TS context.
6553d6ebe24aSShri Abhyankar .  xl   - lower bound.
6554d6ebe24aSShri Abhyankar .  xu   - upper bound.
6555d6ebe24aSShri Abhyankar 
6556d6ebe24aSShri Abhyankar    Notes:
6557d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6558e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6559d6ebe24aSShri Abhyankar 
65602bd2b0e6SSatish Balay    Level: advanced
65612bd2b0e6SSatish Balay 
6562d6ebe24aSShri Abhyankar @*/
6563d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6564d6ebe24aSShri Abhyankar {
6565d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6566d6ebe24aSShri Abhyankar   SNES           snes;
6567d6ebe24aSShri Abhyankar 
6568d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6569d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6570d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6571d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6572d6ebe24aSShri Abhyankar }
6573d6ebe24aSShri Abhyankar 
6574325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6575c6db04a5SJed Brown #include <mex.h>
6576325fc9f4SBarry Smith 
6577325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6578325fc9f4SBarry Smith 
6579325fc9f4SBarry Smith /*
6580325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6581325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6582325fc9f4SBarry Smith 
6583325fc9f4SBarry Smith    Collective on TS
6584325fc9f4SBarry Smith 
6585325fc9f4SBarry Smith    Input Parameters:
6586325fc9f4SBarry Smith +  snes - the TS context
65870910c330SBarry Smith -  u - input vector
6588325fc9f4SBarry Smith 
6589325fc9f4SBarry Smith    Output Parameter:
6590325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6591325fc9f4SBarry Smith 
6592325fc9f4SBarry Smith    Notes:
6593325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6594325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6595325fc9f4SBarry Smith    themselves.
6596325fc9f4SBarry Smith 
6597325fc9f4SBarry Smith    Level: developer
6598325fc9f4SBarry Smith 
6599325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6600325fc9f4SBarry Smith 
6601325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6602325fc9f4SBarry Smith */
66030910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6604325fc9f4SBarry Smith {
6605325fc9f4SBarry Smith   PetscErrorCode  ierr;
6606325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6607325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6608325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6609325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6610325fc9f4SBarry Smith 
6611325fc9f4SBarry Smith   PetscFunctionBegin;
6612325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
66130910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
66140910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6615325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
66160910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6617325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6618325fc9f4SBarry Smith 
6619325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
66200910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
66210910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
66220910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6623bbd56ea5SKarl Rupp 
6624325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6625325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6626325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6627325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6628325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6629325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6630325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6631325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6632325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6633325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6634325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6635325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6636325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6637325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6638325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6639325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6640325fc9f4SBarry Smith   PetscFunctionReturn(0);
6641325fc9f4SBarry Smith }
6642325fc9f4SBarry Smith 
6643325fc9f4SBarry Smith 
6644325fc9f4SBarry Smith /*
6645325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6646325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6647e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6648325fc9f4SBarry Smith 
6649325fc9f4SBarry Smith    Logically Collective on TS
6650325fc9f4SBarry Smith 
6651325fc9f4SBarry Smith    Input Parameters:
6652325fc9f4SBarry Smith +  ts - the TS context
6653325fc9f4SBarry Smith -  func - function evaluation routine
6654325fc9f4SBarry Smith 
6655325fc9f4SBarry Smith    Calling sequence of func:
66560910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6657325fc9f4SBarry Smith 
6658325fc9f4SBarry Smith    Level: beginner
6659325fc9f4SBarry Smith 
6660325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6661325fc9f4SBarry Smith 
6662325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6663325fc9f4SBarry Smith */
6664cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6665325fc9f4SBarry Smith {
6666325fc9f4SBarry Smith   PetscErrorCode  ierr;
6667325fc9f4SBarry Smith   TSMatlabContext *sctx;
6668325fc9f4SBarry Smith 
6669325fc9f4SBarry Smith   PetscFunctionBegin;
6670325fc9f4SBarry Smith   /* currently sctx is memory bleed */
667195dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6672325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6673325fc9f4SBarry Smith   /*
6674325fc9f4SBarry Smith      This should work, but it doesn't
6675325fc9f4SBarry Smith   sctx->ctx = ctx;
6676325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6677325fc9f4SBarry Smith   */
6678325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6679bbd56ea5SKarl Rupp 
66800298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6681325fc9f4SBarry Smith   PetscFunctionReturn(0);
6682325fc9f4SBarry Smith }
6683325fc9f4SBarry Smith 
6684325fc9f4SBarry Smith /*
6685325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6686325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6687325fc9f4SBarry Smith 
6688325fc9f4SBarry Smith    Collective on TS
6689325fc9f4SBarry Smith 
6690325fc9f4SBarry Smith    Input Parameters:
6691cdcf91faSSean Farley +  ts - the TS context
66920910c330SBarry Smith .  u - input vector
6693325fc9f4SBarry Smith .  A, B - the matrices
6694325fc9f4SBarry Smith -  ctx - user context
6695325fc9f4SBarry Smith 
6696325fc9f4SBarry Smith    Level: developer
6697325fc9f4SBarry Smith 
6698325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6699325fc9f4SBarry Smith 
6700325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6701325fc9f4SBarry Smith @*/
6702f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6703325fc9f4SBarry Smith {
6704325fc9f4SBarry Smith   PetscErrorCode  ierr;
6705325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6706325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6707325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6708325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6709325fc9f4SBarry Smith 
6710325fc9f4SBarry Smith   PetscFunctionBegin;
6711cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
67120910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6713325fc9f4SBarry Smith 
67140910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6715325fc9f4SBarry Smith 
6716cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
67170910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
67180910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
67190910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
67200910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6721bbd56ea5SKarl Rupp 
6722325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6723325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6724325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6725325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6726325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6727325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6728325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6729325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6730325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6731325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6732325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6733325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6734325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6735325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6736325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6737325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6738325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6739325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6740325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6741325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6742325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6743325fc9f4SBarry Smith   PetscFunctionReturn(0);
6744325fc9f4SBarry Smith }
6745325fc9f4SBarry Smith 
6746325fc9f4SBarry Smith 
6747325fc9f4SBarry Smith /*
6748325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6749e3c5b3baSBarry 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
6750325fc9f4SBarry Smith 
6751325fc9f4SBarry Smith    Logically Collective on TS
6752325fc9f4SBarry Smith 
6753325fc9f4SBarry Smith    Input Parameters:
6754cdcf91faSSean Farley +  ts - the TS context
6755325fc9f4SBarry Smith .  A,B - Jacobian matrices
6756325fc9f4SBarry Smith .  func - function evaluation routine
6757325fc9f4SBarry Smith -  ctx - user context
6758325fc9f4SBarry Smith 
6759325fc9f4SBarry Smith    Calling sequence of func:
67600910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6761325fc9f4SBarry Smith 
6762325fc9f4SBarry Smith 
6763325fc9f4SBarry Smith    Level: developer
6764325fc9f4SBarry Smith 
6765325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6766325fc9f4SBarry Smith 
6767325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6768325fc9f4SBarry Smith */
6769cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6770325fc9f4SBarry Smith {
6771325fc9f4SBarry Smith   PetscErrorCode  ierr;
6772325fc9f4SBarry Smith   TSMatlabContext *sctx;
6773325fc9f4SBarry Smith 
6774325fc9f4SBarry Smith   PetscFunctionBegin;
6775325fc9f4SBarry Smith   /* currently sctx is memory bleed */
677695dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6777325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6778325fc9f4SBarry Smith   /*
6779325fc9f4SBarry Smith      This should work, but it doesn't
6780325fc9f4SBarry Smith   sctx->ctx = ctx;
6781325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6782325fc9f4SBarry Smith   */
6783325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6784bbd56ea5SKarl Rupp 
6785cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
6786325fc9f4SBarry Smith   PetscFunctionReturn(0);
6787325fc9f4SBarry Smith }
6788325fc9f4SBarry Smith 
6789b5b1a830SBarry Smith /*
6790b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
6791b5b1a830SBarry Smith 
6792b5b1a830SBarry Smith    Collective on TS
6793b5b1a830SBarry Smith 
6794b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6795b5b1a830SBarry Smith @*/
67960910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6797b5b1a830SBarry Smith {
6798b5b1a830SBarry Smith   PetscErrorCode  ierr;
6799b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6800a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
6801b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
6802b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
6803b5b1a830SBarry Smith 
6804b5b1a830SBarry Smith   PetscFunctionBegin;
6805cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
68060910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
6807b5b1a830SBarry Smith 
6808cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
68090910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
6810bbd56ea5SKarl Rupp 
6811b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6812b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
6813b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
6814b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
6815b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
6816b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
6817b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
6818b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6819b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
6820b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
6821b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
6822b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
6823b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
6824b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
6825b5b1a830SBarry Smith   PetscFunctionReturn(0);
6826b5b1a830SBarry Smith }
6827b5b1a830SBarry Smith 
6828b5b1a830SBarry Smith 
6829b5b1a830SBarry Smith /*
6830b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
6831b5b1a830SBarry Smith 
6832b5b1a830SBarry Smith    Level: developer
6833b5b1a830SBarry Smith 
6834b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
6835b5b1a830SBarry Smith 
6836b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6837b5b1a830SBarry Smith */
6838cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6839b5b1a830SBarry Smith {
6840b5b1a830SBarry Smith   PetscErrorCode  ierr;
6841b5b1a830SBarry Smith   TSMatlabContext *sctx;
6842b5b1a830SBarry Smith 
6843b5b1a830SBarry Smith   PetscFunctionBegin;
6844b5b1a830SBarry Smith   /* currently sctx is memory bleed */
684595dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6846b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6847b5b1a830SBarry Smith   /*
6848b5b1a830SBarry Smith      This should work, but it doesn't
6849b5b1a830SBarry Smith   sctx->ctx = ctx;
6850b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6851b5b1a830SBarry Smith   */
6852b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6853bbd56ea5SKarl Rupp 
68540298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6855b5b1a830SBarry Smith   PetscFunctionReturn(0);
6856b5b1a830SBarry Smith }
6857325fc9f4SBarry Smith #endif
6858b3603a34SBarry Smith 
6859b3603a34SBarry Smith /*@C
68604f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6861b3603a34SBarry Smith        in a time based line graph
6862b3603a34SBarry Smith 
6863b3603a34SBarry Smith    Collective on TS
6864b3603a34SBarry Smith 
6865b3603a34SBarry Smith    Input Parameters:
6866b3603a34SBarry Smith +  ts - the TS context
6867b3603a34SBarry Smith .  step - current time-step
6868b3603a34SBarry Smith .  ptime - current time
68697db568b7SBarry Smith .  u - current solution
68707db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6871b3603a34SBarry Smith 
6872b3d3934dSBarry Smith    Options Database:
68739ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6874b3d3934dSBarry Smith 
6875b3603a34SBarry Smith    Level: intermediate
6876b3603a34SBarry Smith 
68776934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component solutions in a separate window
6878b3603a34SBarry Smith 
6879b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6880b3603a34SBarry Smith 
68817db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
68827db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
68837db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
68847db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6885b3603a34SBarry Smith @*/
68867db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6887b3603a34SBarry Smith {
6888b3603a34SBarry Smith   PetscErrorCode    ierr;
68897db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6890b3603a34SBarry Smith   const PetscScalar *yy;
689180666b62SBarry Smith   Vec               v;
6892b3603a34SBarry Smith 
6893b3603a34SBarry Smith   PetscFunctionBegin;
689463e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
689558ff32f7SBarry Smith   if (!step) {
6896a9f9c1f6SBarry Smith     PetscDrawAxis axis;
68976934998bSLisandro Dalcin     PetscInt      dim;
6898a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6899a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6900bab0a581SBarry Smith     if (!ctx->names) {
6901bab0a581SBarry Smith       PetscBool flg;
6902bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6903bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6904bab0a581SBarry Smith       if (flg) {
6905bab0a581SBarry Smith         PetscInt i,n;
6906bab0a581SBarry Smith         char     **names;
6907bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6908bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6909bab0a581SBarry Smith         for (i=0; i<n; i++) {
6910bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6911bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6912bab0a581SBarry Smith         }
6913bab0a581SBarry Smith         names[n] = NULL;
6914bab0a581SBarry Smith         ctx->names = names;
6915bab0a581SBarry Smith       }
6916bab0a581SBarry Smith     }
6917387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6918387f4636SBarry Smith       char      **displaynames;
6919387f4636SBarry Smith       PetscBool flg;
6920387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
692195dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6922387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6923c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6924387f4636SBarry Smith       if (flg) {
6925a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6926387f4636SBarry Smith       }
6927387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6928387f4636SBarry Smith     }
6929387f4636SBarry Smith     if (ctx->displaynames) {
6930387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6931387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6932387f4636SBarry Smith     } else if (ctx->names) {
69330910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
69340b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6935387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6936b0bc92c6SBarry Smith     } else {
6937b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6938b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6939387f4636SBarry Smith     }
69400b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
694158ff32f7SBarry Smith   }
69426934998bSLisandro Dalcin 
69436934998bSLisandro Dalcin   if (!ctx->transform) v = u;
69446934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
694580666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
69466934998bSLisandro Dalcin   if (ctx->displaynames) {
69476934998bSLisandro Dalcin     PetscInt i;
69486934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
69496934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
69506934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
69516934998bSLisandro Dalcin   } else {
6952e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6953e3efe391SJed Brown     PetscInt  i,n;
69546934998bSLisandro Dalcin     PetscReal *yreal;
695580666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6956785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6957e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
69580b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6959e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6960e3efe391SJed Brown #else
69610b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6962e3efe391SJed Brown #endif
696380666b62SBarry Smith   }
69646934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
69656934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
69666934998bSLisandro Dalcin 
6967b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69680b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69696934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
69703923b477SBarry Smith   }
6971b3603a34SBarry Smith   PetscFunctionReturn(0);
6972b3603a34SBarry Smith }
6973b3603a34SBarry Smith 
6974387f4636SBarry Smith 
6975b037adc7SBarry Smith /*@C
697631152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6977b037adc7SBarry Smith 
6978b037adc7SBarry Smith    Collective on TS
6979b037adc7SBarry Smith 
6980b037adc7SBarry Smith    Input Parameters:
6981b037adc7SBarry Smith +  ts - the TS context
6982b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6983b037adc7SBarry Smith 
6984b037adc7SBarry Smith    Level: intermediate
6985b037adc7SBarry Smith 
69867db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
69877db568b7SBarry Smith 
6988b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
6989b037adc7SBarry Smith 
6990a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6991b037adc7SBarry Smith @*/
699231152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6993b037adc7SBarry Smith {
6994b037adc7SBarry Smith   PetscErrorCode    ierr;
6995b037adc7SBarry Smith   PetscInt          i;
6996b037adc7SBarry Smith 
6997b037adc7SBarry Smith   PetscFunctionBegin;
6998b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6999b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
70005537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
7001b3d3934dSBarry Smith       break;
7002b3d3934dSBarry Smith     }
7003b3d3934dSBarry Smith   }
7004b3d3934dSBarry Smith   PetscFunctionReturn(0);
7005b3d3934dSBarry Smith }
7006b3d3934dSBarry Smith 
7007e673d494SBarry Smith /*@C
7008e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
7009e673d494SBarry Smith 
7010e673d494SBarry Smith    Collective on TS
7011e673d494SBarry Smith 
7012e673d494SBarry Smith    Input Parameters:
7013e673d494SBarry Smith +  ts - the TS context
7014e673d494SBarry Smith -  names - the names of the components, final string must be NULL
7015e673d494SBarry Smith 
7016e673d494SBarry Smith    Level: intermediate
7017e673d494SBarry Smith 
7018e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7019e673d494SBarry Smith 
7020a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
7021e673d494SBarry Smith @*/
7022e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
7023e673d494SBarry Smith {
7024e673d494SBarry Smith   PetscErrorCode    ierr;
7025e673d494SBarry Smith 
7026e673d494SBarry Smith   PetscFunctionBegin;
7027e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
7028e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
7029e673d494SBarry Smith   PetscFunctionReturn(0);
7030e673d494SBarry Smith }
7031e673d494SBarry Smith 
7032b3d3934dSBarry Smith /*@C
7033b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
7034b3d3934dSBarry Smith 
7035b3d3934dSBarry Smith    Collective on TS
7036b3d3934dSBarry Smith 
7037b3d3934dSBarry Smith    Input Parameter:
7038b3d3934dSBarry Smith .  ts - the TS context
7039b3d3934dSBarry Smith 
7040b3d3934dSBarry Smith    Output Parameter:
7041b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
7042b3d3934dSBarry Smith 
7043b3d3934dSBarry Smith    Level: intermediate
7044b3d3934dSBarry Smith 
70457db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
70467db568b7SBarry Smith 
7047b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7048b3d3934dSBarry Smith 
7049b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7050b3d3934dSBarry Smith @*/
7051b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
7052b3d3934dSBarry Smith {
7053b3d3934dSBarry Smith   PetscInt       i;
7054b3d3934dSBarry Smith 
7055b3d3934dSBarry Smith   PetscFunctionBegin;
7056b3d3934dSBarry Smith   *names = NULL;
7057b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7058b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
70595537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
7060b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
7061b3d3934dSBarry Smith       break;
7062387f4636SBarry Smith     }
7063387f4636SBarry Smith   }
7064387f4636SBarry Smith   PetscFunctionReturn(0);
7065387f4636SBarry Smith }
7066387f4636SBarry Smith 
7067a66092f1SBarry Smith /*@C
7068a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
7069a66092f1SBarry Smith 
7070a66092f1SBarry Smith    Collective on TS
7071a66092f1SBarry Smith 
7072a66092f1SBarry Smith    Input Parameters:
7073a66092f1SBarry Smith +  ctx - the TSMonitorLG context
7074a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
7075a66092f1SBarry Smith 
7076a66092f1SBarry Smith    Level: intermediate
7077a66092f1SBarry Smith 
7078a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
7079a66092f1SBarry Smith 
7080a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7081a66092f1SBarry Smith @*/
7082a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
7083a66092f1SBarry Smith {
7084a66092f1SBarry Smith   PetscInt          j = 0,k;
7085a66092f1SBarry Smith   PetscErrorCode    ierr;
7086a66092f1SBarry Smith 
7087a66092f1SBarry Smith   PetscFunctionBegin;
7088a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
7089a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
7090a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
7091a66092f1SBarry Smith   while (displaynames[j]) j++;
7092a66092f1SBarry Smith   ctx->ndisplayvariables = j;
7093a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
7094a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
7095a66092f1SBarry Smith   j = 0;
7096a66092f1SBarry Smith   while (displaynames[j]) {
7097a66092f1SBarry Smith     k = 0;
7098a66092f1SBarry Smith     while (ctx->names[k]) {
7099a66092f1SBarry Smith       PetscBool flg;
7100a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
7101a66092f1SBarry Smith       if (flg) {
7102a66092f1SBarry Smith         ctx->displayvariables[j] = k;
7103a66092f1SBarry Smith         break;
7104a66092f1SBarry Smith       }
7105a66092f1SBarry Smith       k++;
7106a66092f1SBarry Smith     }
7107a66092f1SBarry Smith     j++;
7108a66092f1SBarry Smith   }
7109a66092f1SBarry Smith   PetscFunctionReturn(0);
7110a66092f1SBarry Smith }
7111a66092f1SBarry Smith 
7112a66092f1SBarry Smith 
7113387f4636SBarry Smith /*@C
7114387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
7115387f4636SBarry Smith 
7116387f4636SBarry Smith    Collective on TS
7117387f4636SBarry Smith 
7118387f4636SBarry Smith    Input Parameters:
7119387f4636SBarry Smith +  ts - the TS context
7120387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
7121387f4636SBarry Smith 
71227db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
71237db568b7SBarry Smith 
7124387f4636SBarry Smith    Level: intermediate
7125387f4636SBarry Smith 
7126387f4636SBarry Smith .keywords: TS,  vector, monitor, view
7127387f4636SBarry Smith 
7128387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7129387f4636SBarry Smith @*/
7130387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
7131387f4636SBarry Smith {
7132a66092f1SBarry Smith   PetscInt          i;
7133387f4636SBarry Smith   PetscErrorCode    ierr;
7134387f4636SBarry Smith 
7135387f4636SBarry Smith   PetscFunctionBegin;
7136387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7137387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
71385537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
7139b3d3934dSBarry Smith       break;
7140b037adc7SBarry Smith     }
7141b037adc7SBarry Smith   }
7142b037adc7SBarry Smith   PetscFunctionReturn(0);
7143b037adc7SBarry Smith }
7144b037adc7SBarry Smith 
714580666b62SBarry Smith /*@C
714680666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
714780666b62SBarry Smith 
714880666b62SBarry Smith    Collective on TS
714980666b62SBarry Smith 
715080666b62SBarry Smith    Input Parameters:
715180666b62SBarry Smith +  ts - the TS context
715280666b62SBarry Smith .  transform - the transform function
71537684fa3eSBarry Smith .  destroy - function to destroy the optional context
715480666b62SBarry Smith -  ctx - optional context used by transform function
715580666b62SBarry Smith 
71567db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
71577db568b7SBarry Smith 
715880666b62SBarry Smith    Level: intermediate
715980666b62SBarry Smith 
716080666b62SBarry Smith .keywords: TS,  vector, monitor, view
716180666b62SBarry Smith 
7162a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
716380666b62SBarry Smith @*/
71647684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
716580666b62SBarry Smith {
716680666b62SBarry Smith   PetscInt          i;
7167a66092f1SBarry Smith   PetscErrorCode    ierr;
716880666b62SBarry Smith 
716980666b62SBarry Smith   PetscFunctionBegin;
717080666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
717180666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
71725537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
717380666b62SBarry Smith     }
717480666b62SBarry Smith   }
717580666b62SBarry Smith   PetscFunctionReturn(0);
717680666b62SBarry Smith }
717780666b62SBarry Smith 
7178e673d494SBarry Smith /*@C
7179e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
7180e673d494SBarry Smith 
7181e673d494SBarry Smith    Collective on TSLGCtx
7182e673d494SBarry Smith 
7183e673d494SBarry Smith    Input Parameters:
7184e673d494SBarry Smith +  ts - the TS context
7185e673d494SBarry Smith .  transform - the transform function
71867684fa3eSBarry Smith .  destroy - function to destroy the optional context
7187e673d494SBarry Smith -  ctx - optional context used by transform function
7188e673d494SBarry Smith 
7189e673d494SBarry Smith    Level: intermediate
7190e673d494SBarry Smith 
7191e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7192e673d494SBarry Smith 
7193a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
7194e673d494SBarry Smith @*/
71957684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
7196e673d494SBarry Smith {
7197e673d494SBarry Smith   PetscFunctionBegin;
7198e673d494SBarry Smith   ctx->transform    = transform;
71997684fa3eSBarry Smith   ctx->transformdestroy = destroy;
7200e673d494SBarry Smith   ctx->transformctx = tctx;
7201e673d494SBarry Smith   PetscFunctionReturn(0);
7202e673d494SBarry Smith }
7203e673d494SBarry Smith 
7204ef20d060SBarry Smith /*@C
72054f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
7206ef20d060SBarry Smith        in a time based line graph
7207ef20d060SBarry Smith 
7208ef20d060SBarry Smith    Collective on TS
7209ef20d060SBarry Smith 
7210ef20d060SBarry Smith    Input Parameters:
7211ef20d060SBarry Smith +  ts - the TS context
7212ef20d060SBarry Smith .  step - current time-step
7213ef20d060SBarry Smith .  ptime - current time
72147db568b7SBarry Smith .  u - current solution
72157db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
7216ef20d060SBarry Smith 
7217ef20d060SBarry Smith    Level: intermediate
7218ef20d060SBarry Smith 
72196934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component errors in a separate window
7220abd5a294SJed Brown 
7221abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
7222abd5a294SJed Brown 
7223abd5a294SJed Brown    Options Database Keys:
72244f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
7225ef20d060SBarry Smith 
7226ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
7227ef20d060SBarry Smith 
7228abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
7229ef20d060SBarry Smith @*/
72300910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
7231ef20d060SBarry Smith {
7232ef20d060SBarry Smith   PetscErrorCode    ierr;
72330b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
7234ef20d060SBarry Smith   const PetscScalar *yy;
7235ef20d060SBarry Smith   Vec               y;
7236ef20d060SBarry Smith 
7237ef20d060SBarry Smith   PetscFunctionBegin;
7238a9f9c1f6SBarry Smith   if (!step) {
7239a9f9c1f6SBarry Smith     PetscDrawAxis axis;
72406934998bSLisandro Dalcin     PetscInt      dim;
7241a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
72421ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
72430910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
7244a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7245a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7246a9f9c1f6SBarry Smith   }
72470910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
7248ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
72490910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7250ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
7251e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
7252e3efe391SJed Brown   {
7253e3efe391SJed Brown     PetscReal *yreal;
7254e3efe391SJed Brown     PetscInt  i,n;
7255e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
7256785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
7257e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
72580b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
7259e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
7260e3efe391SJed Brown   }
7261e3efe391SJed Brown #else
72620b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
7263e3efe391SJed Brown #endif
7264ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
7265ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
7266b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
72670b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
72686934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
72693923b477SBarry Smith   }
7270ef20d060SBarry Smith   PetscFunctionReturn(0);
7271ef20d060SBarry Smith }
7272ef20d060SBarry Smith 
7273201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7274201da799SBarry Smith {
7275201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7276201da799SBarry Smith   PetscReal      x   = ptime,y;
7277201da799SBarry Smith   PetscErrorCode ierr;
7278201da799SBarry Smith   PetscInt       its;
7279201da799SBarry Smith 
7280201da799SBarry Smith   PetscFunctionBegin;
728163e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7282201da799SBarry Smith   if (!n) {
7283201da799SBarry Smith     PetscDrawAxis axis;
7284201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7285201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7286201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7287201da799SBarry Smith     ctx->snes_its = 0;
7288201da799SBarry Smith   }
7289201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7290201da799SBarry Smith   y    = its - ctx->snes_its;
7291201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
72923fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7293201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
72946934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7295201da799SBarry Smith   }
7296201da799SBarry Smith   ctx->snes_its = its;
7297201da799SBarry Smith   PetscFunctionReturn(0);
7298201da799SBarry Smith }
7299201da799SBarry Smith 
7300201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7301201da799SBarry Smith {
7302201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7303201da799SBarry Smith   PetscReal      x   = ptime,y;
7304201da799SBarry Smith   PetscErrorCode ierr;
7305201da799SBarry Smith   PetscInt       its;
7306201da799SBarry Smith 
7307201da799SBarry Smith   PetscFunctionBegin;
730863e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7309201da799SBarry Smith   if (!n) {
7310201da799SBarry Smith     PetscDrawAxis axis;
7311201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7312201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7313201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7314201da799SBarry Smith     ctx->ksp_its = 0;
7315201da799SBarry Smith   }
7316201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7317201da799SBarry Smith   y    = its - ctx->ksp_its;
7318201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
731999fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7320201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
73216934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7322201da799SBarry Smith   }
7323201da799SBarry Smith   ctx->ksp_its = its;
7324201da799SBarry Smith   PetscFunctionReturn(0);
7325201da799SBarry Smith }
7326f9c1d6abSBarry Smith 
7327f9c1d6abSBarry Smith /*@
7328f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7329f9c1d6abSBarry Smith 
7330f9c1d6abSBarry Smith    Collective on TS and Vec
7331f9c1d6abSBarry Smith 
7332f9c1d6abSBarry Smith    Input Parameters:
7333f9c1d6abSBarry Smith +  ts - the TS context
7334f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7335f9c1d6abSBarry Smith 
7336f9c1d6abSBarry Smith    Output Parameters:
7337f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7338f9c1d6abSBarry Smith 
7339f9c1d6abSBarry Smith    Level: developer
7340f9c1d6abSBarry Smith 
7341f9c1d6abSBarry Smith .keywords: TS, compute
7342f9c1d6abSBarry Smith 
7343f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7344f9c1d6abSBarry Smith @*/
7345f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7346f9c1d6abSBarry Smith {
7347f9c1d6abSBarry Smith   PetscErrorCode ierr;
7348f9c1d6abSBarry Smith 
7349f9c1d6abSBarry Smith   PetscFunctionBegin;
7350f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7351ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7352f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7353f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7354f9c1d6abSBarry Smith }
735524655328SShri 
7356b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7357b3d3934dSBarry Smith /*@C
7358b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7359b3d3934dSBarry Smith 
7360b3d3934dSBarry Smith    Collective on TS
7361b3d3934dSBarry Smith 
7362b3d3934dSBarry Smith    Input Parameters:
7363b3d3934dSBarry Smith .  ts  - the ODE solver object
7364b3d3934dSBarry Smith 
7365b3d3934dSBarry Smith    Output Parameter:
7366b3d3934dSBarry Smith .  ctx - the context
7367b3d3934dSBarry Smith 
7368b3d3934dSBarry Smith    Level: intermediate
7369b3d3934dSBarry Smith 
7370b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
7371b3d3934dSBarry Smith 
7372b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7373b3d3934dSBarry Smith 
7374b3d3934dSBarry Smith @*/
7375b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7376b3d3934dSBarry Smith {
7377b3d3934dSBarry Smith   PetscErrorCode ierr;
7378b3d3934dSBarry Smith 
7379b3d3934dSBarry Smith   PetscFunctionBegin;
7380a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7381b3d3934dSBarry Smith   PetscFunctionReturn(0);
7382b3d3934dSBarry Smith }
7383b3d3934dSBarry Smith 
7384b3d3934dSBarry Smith /*@C
7385b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7386b3d3934dSBarry Smith 
7387b3d3934dSBarry Smith    Collective on TS
7388b3d3934dSBarry Smith 
7389b3d3934dSBarry Smith    Input Parameters:
7390b3d3934dSBarry Smith +  ts - the TS context
7391b3d3934dSBarry Smith .  step - current time-step
7392b3d3934dSBarry Smith .  ptime - current time
73937db568b7SBarry Smith .  u  - current solution
73947db568b7SBarry Smith -  dctx - the envelope context
7395b3d3934dSBarry Smith 
7396b3d3934dSBarry Smith    Options Database:
7397b3d3934dSBarry Smith .  -ts_monitor_envelope
7398b3d3934dSBarry Smith 
7399b3d3934dSBarry Smith    Level: intermediate
7400b3d3934dSBarry Smith 
7401b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7402b3d3934dSBarry Smith 
7403b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7404b3d3934dSBarry Smith 
74057db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7406b3d3934dSBarry Smith @*/
74077db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7408b3d3934dSBarry Smith {
7409b3d3934dSBarry Smith   PetscErrorCode       ierr;
74107db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7411b3d3934dSBarry Smith 
7412b3d3934dSBarry Smith   PetscFunctionBegin;
7413b3d3934dSBarry Smith   if (!ctx->max) {
7414b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7415b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7416b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7417b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7418b3d3934dSBarry Smith   } else {
7419b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7420b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7421b3d3934dSBarry Smith   }
7422b3d3934dSBarry Smith   PetscFunctionReturn(0);
7423b3d3934dSBarry Smith }
7424b3d3934dSBarry Smith 
7425b3d3934dSBarry Smith 
7426b3d3934dSBarry Smith /*@C
7427b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7428b3d3934dSBarry Smith 
7429b3d3934dSBarry Smith    Collective on TS
7430b3d3934dSBarry Smith 
7431b3d3934dSBarry Smith    Input Parameter:
7432b3d3934dSBarry Smith .  ts - the TS context
7433b3d3934dSBarry Smith 
7434b3d3934dSBarry Smith    Output Parameter:
7435b3d3934dSBarry Smith +  max - the maximum values
7436b3d3934dSBarry Smith -  min - the minimum values
7437b3d3934dSBarry Smith 
74387db568b7SBarry Smith    Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
74397db568b7SBarry Smith 
7440b3d3934dSBarry Smith    Level: intermediate
7441b3d3934dSBarry Smith 
7442b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7443b3d3934dSBarry Smith 
7444b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7445b3d3934dSBarry Smith @*/
7446b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7447b3d3934dSBarry Smith {
7448b3d3934dSBarry Smith   PetscInt i;
7449b3d3934dSBarry Smith 
7450b3d3934dSBarry Smith   PetscFunctionBegin;
7451b3d3934dSBarry Smith   if (max) *max = NULL;
7452b3d3934dSBarry Smith   if (min) *min = NULL;
7453b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7454b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
74555537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7456b3d3934dSBarry Smith       if (max) *max = ctx->max;
7457b3d3934dSBarry Smith       if (min) *min = ctx->min;
7458b3d3934dSBarry Smith       break;
7459b3d3934dSBarry Smith     }
7460b3d3934dSBarry Smith   }
7461b3d3934dSBarry Smith   PetscFunctionReturn(0);
7462b3d3934dSBarry Smith }
7463b3d3934dSBarry Smith 
7464b3d3934dSBarry Smith /*@C
7465b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7466b3d3934dSBarry Smith 
7467b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7468b3d3934dSBarry Smith 
7469b3d3934dSBarry Smith    Input Parameter:
7470b3d3934dSBarry Smith .  ctx - the monitor context
7471b3d3934dSBarry Smith 
7472b3d3934dSBarry Smith    Level: intermediate
7473b3d3934dSBarry Smith 
7474b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7475b3d3934dSBarry Smith 
74767db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7477b3d3934dSBarry Smith @*/
7478b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7479b3d3934dSBarry Smith {
7480b3d3934dSBarry Smith   PetscErrorCode ierr;
7481b3d3934dSBarry Smith 
7482b3d3934dSBarry Smith   PetscFunctionBegin;
7483b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7484b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7485b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7486b3d3934dSBarry Smith   PetscFunctionReturn(0);
7487b3d3934dSBarry Smith }
7488f2dee214SBarry Smith 
748924655328SShri /*@
749024655328SShri    TSRollBack - Rolls back one time step
749124655328SShri 
749224655328SShri    Collective on TS
749324655328SShri 
749424655328SShri    Input Parameter:
749524655328SShri .  ts - the TS context obtained from TSCreate()
749624655328SShri 
749724655328SShri    Level: advanced
749824655328SShri 
749924655328SShri .keywords: TS, timestep, rollback
750024655328SShri 
750124655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
750224655328SShri @*/
750324655328SShri PetscErrorCode  TSRollBack(TS ts)
750424655328SShri {
750524655328SShri   PetscErrorCode ierr;
750624655328SShri 
750724655328SShri   PetscFunctionBegin;
750824655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7509b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
751024655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
751124655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
751224655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
751324655328SShri   ts->ptime = ts->ptime_prev;
7514be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
7515be5899b3SLisandro Dalcin   ts->steps--; ts->total_steps--;
751610b82f12SShri Abhyankar   ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
7517b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
751824655328SShri   PetscFunctionReturn(0);
751924655328SShri }
7520aeb4809dSShri Abhyankar 
7521ff22ae23SHong Zhang /*@
7522ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7523ff22ae23SHong Zhang 
7524ff22ae23SHong Zhang    Input Parameter:
7525ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7526ff22ae23SHong Zhang 
7527ff22ae23SHong Zhang    Level: advanced
7528ff22ae23SHong Zhang 
7529ff22ae23SHong Zhang .keywords: TS, getstages
7530ff22ae23SHong Zhang 
7531ff22ae23SHong Zhang .seealso: TSCreate()
7532ff22ae23SHong Zhang @*/
7533ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7534ff22ae23SHong Zhang {
7535ff22ae23SHong Zhang   PetscErrorCode ierr;
7536ff22ae23SHong Zhang 
7537ff22ae23SHong Zhang   PetscFunctionBegin;
7538ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7539ff22ae23SHong Zhang   PetscValidPointer(ns,2);
7540ff22ae23SHong Zhang 
7541ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
7542ff22ae23SHong Zhang   else {
7543ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7544ff22ae23SHong Zhang   }
7545ff22ae23SHong Zhang   PetscFunctionReturn(0);
7546ff22ae23SHong Zhang }
7547ff22ae23SHong Zhang 
7548847ff0e1SMatthew G. Knepley /*@C
7549847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7550847ff0e1SMatthew G. Knepley 
7551847ff0e1SMatthew G. Knepley   Collective on SNES
7552847ff0e1SMatthew G. Knepley 
7553847ff0e1SMatthew G. Knepley   Input Parameters:
7554847ff0e1SMatthew G. Knepley + ts - the TS context
7555847ff0e1SMatthew G. Knepley . t - current timestep
7556847ff0e1SMatthew G. Knepley . U - state vector
7557847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7558847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7559847ff0e1SMatthew G. Knepley - ctx - an optional user context
7560847ff0e1SMatthew G. Knepley 
7561847ff0e1SMatthew G. Knepley   Output Parameters:
7562847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7563847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7564847ff0e1SMatthew G. Knepley 
7565847ff0e1SMatthew G. Knepley   Level: intermediate
7566847ff0e1SMatthew G. Knepley 
7567847ff0e1SMatthew G. Knepley   Notes:
7568847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7569847ff0e1SMatthew G. Knepley 
7570847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7571847ff0e1SMatthew G. Knepley 
7572847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7573847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7574847ff0e1SMatthew G. Knepley 
7575847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7576847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7577847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7578847ff0e1SMatthew G. Knepley 
7579847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7580847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7581847ff0e1SMatthew G. Knepley @*/
7582847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7583847ff0e1SMatthew G. Knepley {
7584847ff0e1SMatthew G. Knepley   SNES           snes;
7585847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7586847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7587847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7588847ff0e1SMatthew G. Knepley 
7589847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7590c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7591847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7592847ff0e1SMatthew G. Knepley   if (!color) {
7593847ff0e1SMatthew G. Knepley     DM         dm;
7594847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7595847ff0e1SMatthew G. Knepley 
7596847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7597847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7598847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7599847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7600847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7601847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7602847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7603847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7604847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7605847ff0e1SMatthew G. Knepley     } else {
7606847ff0e1SMatthew G. Knepley       MatColoring mc;
7607847ff0e1SMatthew G. Knepley 
7608847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7609847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7610847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7611847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7612847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7613847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7614847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7615847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7616847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7617847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7618847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7619847ff0e1SMatthew G. Knepley     }
7620847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7621847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7622847ff0e1SMatthew G. Knepley   }
7623847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7624847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7625847ff0e1SMatthew G. Knepley   if (J != B) {
7626847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7627847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7628847ff0e1SMatthew G. Knepley   }
7629847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7630847ff0e1SMatthew G. Knepley }
763193b34091SDebojyoti Ghosh 
7632cb9d8021SPierre Barbier de Reuille /*@
7633cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7634cb9d8021SPierre Barbier de Reuille 
7635cb9d8021SPierre Barbier de Reuille     Input Parameters:
7636cb9d8021SPierre Barbier de Reuille     ts - the TS context
7637cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7638cb9d8021SPierre Barbier de Reuille 
7639cb9d8021SPierre Barbier de Reuille     Level: intermediate
7640cb9d8021SPierre Barbier de Reuille 
7641cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7642cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7643cb9d8021SPierre Barbier de Reuille @*/
7644cb9d8021SPierre Barbier de Reuille 
7645d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7646cb9d8021SPierre Barbier de Reuille {
7647cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7648cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7649cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7650cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7651cb9d8021SPierre Barbier de Reuille }
7652cb9d8021SPierre Barbier de Reuille 
7653cb9d8021SPierre Barbier de Reuille /*@
7654cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7655cb9d8021SPierre Barbier de Reuille 
7656cb9d8021SPierre Barbier de Reuille     Input Parameters:
7657cb9d8021SPierre Barbier de Reuille     ts - the TS context
7658cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7659d183316bSPierre Barbier de Reuille     Y - state vector to check.
7660cb9d8021SPierre Barbier de Reuille 
7661cb9d8021SPierre Barbier de Reuille     Output Parameter:
7662cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7663cb9d8021SPierre Barbier de Reuille 
7664cb9d8021SPierre Barbier de Reuille     Note:
7665cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7666cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
766796a0c994SBarry Smith 
766896a0c994SBarry Smith     Level: advanced
7669cb9d8021SPierre Barbier de Reuille @*/
7670d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7671cb9d8021SPierre Barbier de Reuille {
7672cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
7673cb9d8021SPierre Barbier de Reuille 
7674cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7675cb9d8021SPierre Barbier de Reuille 
7676cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7677cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7678cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7679d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7680cb9d8021SPierre Barbier de Reuille   }
7681cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7682cb9d8021SPierre Barbier de Reuille }
76831ceb14c0SBarry Smith 
768493b34091SDebojyoti Ghosh /*@C
7685e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
768693b34091SDebojyoti Ghosh 
768793b34091SDebojyoti Ghosh   Collective on MPI_Comm
768893b34091SDebojyoti Ghosh 
768993b34091SDebojyoti Ghosh   Input Parameter:
769093b34091SDebojyoti Ghosh . tsin    - The input TS
769193b34091SDebojyoti Ghosh 
769293b34091SDebojyoti Ghosh   Output Parameter:
7693e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
769493b34091SDebojyoti Ghosh 
76955eca1a21SEmil Constantinescu   Notes:
76965eca1a21SEmil 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.
76975eca1a21SEmil Constantinescu 
7698baa10174SEmil 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);
76995eca1a21SEmil Constantinescu 
77005eca1a21SEmil Constantinescu   Level: developer
770193b34091SDebojyoti Ghosh 
7702e5168f73SEmil Constantinescu .keywords: TS, clone
7703e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
770493b34091SDebojyoti Ghosh @*/
7705baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
770693b34091SDebojyoti Ghosh {
770793b34091SDebojyoti Ghosh   TS             t;
770893b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7709dc846ba4SSatish Balay   SNES           snes_start;
7710dc846ba4SSatish Balay   DM             dm;
7711dc846ba4SSatish Balay   TSType         type;
771293b34091SDebojyoti Ghosh 
771393b34091SDebojyoti Ghosh   PetscFunctionBegin;
771493b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
771593b34091SDebojyoti Ghosh   *tsout = NULL;
771693b34091SDebojyoti Ghosh 
77177a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
771893b34091SDebojyoti Ghosh 
771993b34091SDebojyoti Ghosh   /* General TS description */
772093b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
772193b34091SDebojyoti Ghosh   t->setupcalled       = 0;
772293b34091SDebojyoti Ghosh   t->ksp_its           = 0;
772393b34091SDebojyoti Ghosh   t->snes_its          = 0;
772493b34091SDebojyoti Ghosh   t->nwork             = 0;
772593b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
772693b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
772793b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
772893b34091SDebojyoti Ghosh 
772934561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
773034561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7731d15a3a53SEmil Constantinescu 
773293b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
773393b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
773493b34091SDebojyoti Ghosh 
773593b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
773651699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
773793b34091SDebojyoti Ghosh 
7738e7069c78SShri   t->trajectory = tsin->trajectory;
7739e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7740e7069c78SShri 
7741e7069c78SShri   t->event = tsin->event;
77426b10a48eSSatish Balay   if (t->event) t->event->refct++;
7743e7069c78SShri 
774493b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
774593b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7746e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
774793b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
774893b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
774993b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
7750e7069c78SShri   t->total_steps       = tsin->total_steps;
775193b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
775293b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
775393b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
775493b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
775593b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
775693b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
775793b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
775893b34091SDebojyoti Ghosh 
775993b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
776093b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
776193b34091SDebojyoti Ghosh 
776293b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
776393b34091SDebojyoti Ghosh 
776493b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
776593b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
776693b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
776793b34091SDebojyoti Ghosh 
776893b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
776993b34091SDebojyoti Ghosh 
77700d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
77710d4fed19SBarry Smith     PetscInt i;
77720d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
77730d4fed19SBarry Smith     for (i=0; i<10; i++) {
77740d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
77750d4fed19SBarry Smith     }
77760d4fed19SBarry Smith   }
777793b34091SDebojyoti Ghosh   *tsout = t;
777893b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
777993b34091SDebojyoti Ghosh }
7780