xref: /petsc/src/ts/interface/ts.c (revision e162b725996043a8153e61ea764133a87320a815)
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;
724214bc6a2SJed Brown 
725214bc6a2SJed Brown   PetscFunctionBegin;
726c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
727c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
7280298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
729214bc6a2SJed Brown   if (Arhs) {
730214bc6a2SJed Brown     if (!ts->Arhs) {
731214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
732214bc6a2SJed Brown     }
733214bc6a2SJed Brown     *Arhs = ts->Arhs;
734214bc6a2SJed Brown   }
735214bc6a2SJed Brown   if (Brhs) {
736214bc6a2SJed Brown     if (!ts->Brhs) {
737bdb70873SJed Brown       if (A != B) {
738214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
739bdb70873SJed Brown       } else {
740bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
74151699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
742bdb70873SJed Brown       }
743214bc6a2SJed Brown     }
744214bc6a2SJed Brown     *Brhs = ts->Brhs;
745214bc6a2SJed Brown   }
746214bc6a2SJed Brown   PetscFunctionReturn(0);
747214bc6a2SJed Brown }
748214bc6a2SJed Brown 
749316643e7SJed Brown /*@
7500910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
751316643e7SJed Brown 
752316643e7SJed Brown    Collective on TS and Vec
753316643e7SJed Brown 
754316643e7SJed Brown    Input Parameters:
755316643e7SJed Brown +  ts - the TS context
756316643e7SJed Brown .  t - current time
7570910c330SBarry Smith .  U - state vector
7580910c330SBarry Smith .  Udot - time derivative of state vector
759214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
760316643e7SJed Brown 
761316643e7SJed Brown    Output Parameter:
762316643e7SJed Brown .  Y - right hand side
763316643e7SJed Brown 
764316643e7SJed Brown    Note:
765316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
766316643e7SJed Brown    is used internally within the nonlinear solvers.
767316643e7SJed Brown 
768316643e7SJed Brown    If the user did did not write their equations in implicit form, this
769316643e7SJed Brown    function recasts them in implicit form.
770316643e7SJed Brown 
771316643e7SJed Brown    Level: developer
772316643e7SJed Brown 
773316643e7SJed Brown .keywords: TS, compute
774316643e7SJed Brown 
775316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
776316643e7SJed Brown @*/
7770910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
778316643e7SJed Brown {
779316643e7SJed Brown   PetscErrorCode ierr;
78024989b8cSPeter Brune   TSIFunction    ifunction;
78124989b8cSPeter Brune   TSRHSFunction  rhsfunction;
78224989b8cSPeter Brune   void           *ctx;
78324989b8cSPeter Brune   DM             dm;
784316643e7SJed Brown 
785316643e7SJed Brown   PetscFunctionBegin;
7860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7870910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7880910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
7890700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
790316643e7SJed Brown 
79124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
79224989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
7930298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
79424989b8cSPeter Brune 
795ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
796d90be118SSean Farley 
7970910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
79824989b8cSPeter Brune   if (ifunction) {
799316643e7SJed Brown     PetscStackPush("TS user implicit function");
8000910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
801316643e7SJed Brown     PetscStackPop;
802214bc6a2SJed Brown   }
803214bc6a2SJed Brown   if (imex) {
80424989b8cSPeter Brune     if (!ifunction) {
8050910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8062dd45cf8SJed Brown     }
80724989b8cSPeter Brune   } else if (rhsfunction) {
80824989b8cSPeter Brune     if (ifunction) {
809214bc6a2SJed Brown       Vec Frhs;
810214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8110910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
812214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8132dd45cf8SJed Brown     } else {
8140910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8150910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
816316643e7SJed Brown     }
8174a6899ffSJed Brown   }
8180910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
819316643e7SJed Brown   PetscFunctionReturn(0);
820316643e7SJed Brown }
821316643e7SJed Brown 
822316643e7SJed Brown /*@
823316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
824316643e7SJed Brown 
825316643e7SJed Brown    Collective on TS and Vec
826316643e7SJed Brown 
827316643e7SJed Brown    Input
828316643e7SJed Brown       Input Parameters:
829316643e7SJed Brown +  ts - the TS context
830316643e7SJed Brown .  t - current timestep
8310910c330SBarry Smith .  U - state vector
8320910c330SBarry Smith .  Udot - time derivative of state vector
833214bc6a2SJed Brown .  shift - shift to apply, see note below
834214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
835316643e7SJed Brown 
836316643e7SJed Brown    Output Parameters:
837316643e7SJed Brown +  A - Jacobian matrix
838316643e7SJed Brown .  B - optional preconditioning matrix
839316643e7SJed Brown -  flag - flag indicating matrix structure
840316643e7SJed Brown 
841316643e7SJed Brown    Notes:
8420910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
843316643e7SJed Brown 
8440910c330SBarry Smith    dF/dU + shift*dF/dUdot
845316643e7SJed Brown 
846316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
847316643e7SJed Brown    is used internally within the nonlinear solvers.
848316643e7SJed Brown 
849316643e7SJed Brown    Level: developer
850316643e7SJed Brown 
851316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
852316643e7SJed Brown 
853316643e7SJed Brown .seealso:  TSSetIJacobian()
85463495f91SJed Brown @*/
855d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
856316643e7SJed Brown {
857316643e7SJed Brown   PetscErrorCode ierr;
85824989b8cSPeter Brune   TSIJacobian    ijacobian;
85924989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
86024989b8cSPeter Brune   DM             dm;
86124989b8cSPeter Brune   void           *ctx;
862316643e7SJed Brown 
863316643e7SJed Brown   PetscFunctionBegin;
8640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8650910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8660910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
867316643e7SJed Brown   PetscValidPointer(A,6);
86894ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
869316643e7SJed Brown   PetscValidPointer(B,7);
87094ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
87124989b8cSPeter Brune 
87224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
87324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
8740298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
87524989b8cSPeter Brune 
876ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
877316643e7SJed Brown 
87894ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
87924989b8cSPeter Brune   if (ijacobian) {
8806cd88445SBarry Smith     PetscBool missing;
881316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
882d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
883316643e7SJed Brown     PetscStackPop;
8846cd88445SBarry Smith     if (A) {
8856cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
8866cd88445SBarry 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");
8876cd88445SBarry Smith     }
8886cd88445SBarry Smith     if (B && B != A) {
8896cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
8906cd88445SBarry 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");
8916cd88445SBarry Smith     }
8924a6899ffSJed Brown   }
893214bc6a2SJed Brown   if (imex) {
894b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
8954c26be97Sstefano_zampini       PetscBool assembled;
89694ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
8974c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
8984c26be97Sstefano_zampini       if (!assembled) {
8994c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9004c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9014c26be97Sstefano_zampini       }
90294ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
90394ab13aaSBarry Smith       if (A != B) {
90494ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9054c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9064c26be97Sstefano_zampini         if (!assembled) {
9074c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9084c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9094c26be97Sstefano_zampini         }
91094ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
911214bc6a2SJed Brown       }
912214bc6a2SJed Brown     }
913214bc6a2SJed Brown   } else {
914e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
915e1244c69SJed Brown     if (rhsjacobian) {
916214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
917d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
918e1244c69SJed Brown     }
91994ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
920e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
921e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
92294ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
92394ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
92494ab13aaSBarry Smith       if (A != B) {
92594ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
92694ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
927316643e7SJed Brown       }
928e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
929e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
930e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
93194ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
93294ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
93394ab13aaSBarry Smith         if (A != B) {
93494ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
93594ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
936214bc6a2SJed Brown         }
937316643e7SJed Brown       }
93894ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
93994ab13aaSBarry Smith       if (A != B) {
94094ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
941316643e7SJed Brown       }
942316643e7SJed Brown     }
943316643e7SJed Brown   }
94494ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
945316643e7SJed Brown   PetscFunctionReturn(0);
946316643e7SJed Brown }
947316643e7SJed Brown 
948d763cef2SBarry Smith /*@C
949d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
950b5abc632SBarry Smith     where U_t = G(t,u).
951d763cef2SBarry Smith 
9523f9fe445SBarry Smith     Logically Collective on TS
953d763cef2SBarry Smith 
954d763cef2SBarry Smith     Input Parameters:
955d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
9560298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
957d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
958d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
9590298fd71SBarry Smith           function evaluation routine (may be NULL)
960d763cef2SBarry Smith 
961d763cef2SBarry Smith     Calling sequence of func:
96287828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
963d763cef2SBarry Smith 
964d763cef2SBarry Smith +   t - current timestep
965d763cef2SBarry Smith .   u - input vector
966d763cef2SBarry Smith .   F - function vector
967d763cef2SBarry Smith -   ctx - [optional] user-defined function context
968d763cef2SBarry Smith 
969d763cef2SBarry Smith     Level: beginner
970d763cef2SBarry Smith 
9712bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
9722bbac0d3SBarry Smith 
973d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
974d763cef2SBarry Smith 
975ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
976d763cef2SBarry Smith @*/
977089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
978d763cef2SBarry Smith {
979089b2837SJed Brown   PetscErrorCode ierr;
980089b2837SJed Brown   SNES           snes;
9810298fd71SBarry Smith   Vec            ralloc = NULL;
98224989b8cSPeter Brune   DM             dm;
983d763cef2SBarry Smith 
984089b2837SJed Brown   PetscFunctionBegin;
9850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
986ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
98724989b8cSPeter Brune 
98824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
98924989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
990089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
991e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
992e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
993e856ceecSJed Brown     r = ralloc;
994e856ceecSJed Brown   }
995089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
996e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
997d763cef2SBarry Smith   PetscFunctionReturn(0);
998d763cef2SBarry Smith }
999d763cef2SBarry Smith 
1000ef20d060SBarry Smith /*@C
1001abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1002ef20d060SBarry Smith 
1003ef20d060SBarry Smith     Logically Collective on TS
1004ef20d060SBarry Smith 
1005ef20d060SBarry Smith     Input Parameters:
1006ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1007ef20d060SBarry Smith .   f - routine for evaluating the solution
1008ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10090298fd71SBarry Smith           function evaluation routine (may be NULL)
1010ef20d060SBarry Smith 
1011ef20d060SBarry Smith     Calling sequence of func:
1012ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1013ef20d060SBarry Smith 
1014ef20d060SBarry Smith +   t - current timestep
1015ef20d060SBarry Smith .   u - output vector
1016ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1017ef20d060SBarry Smith 
1018abd5a294SJed Brown     Notes:
1019abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1020abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1021abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1022abd5a294SJed Brown 
10234f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1024abd5a294SJed Brown 
1025ef20d060SBarry Smith     Level: beginner
1026ef20d060SBarry Smith 
1027ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1028ef20d060SBarry Smith 
10299b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
1030ef20d060SBarry Smith @*/
1031ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1032ef20d060SBarry Smith {
1033ef20d060SBarry Smith   PetscErrorCode ierr;
1034ef20d060SBarry Smith   DM             dm;
1035ef20d060SBarry Smith 
1036ef20d060SBarry Smith   PetscFunctionBegin;
1037ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1038ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1039ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1040ef20d060SBarry Smith   PetscFunctionReturn(0);
1041ef20d060SBarry Smith }
1042ef20d060SBarry Smith 
10439b7cd975SBarry Smith /*@C
10449b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
10459b7cd975SBarry Smith 
10469b7cd975SBarry Smith     Logically Collective on TS
10479b7cd975SBarry Smith 
10489b7cd975SBarry Smith     Input Parameters:
10499b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1050*e162b725SBarry Smith .   func - routine for evaluating the forcing function
10519b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
10520298fd71SBarry Smith           function evaluation routine (may be NULL)
10539b7cd975SBarry Smith 
10549b7cd975SBarry Smith     Calling sequence of func:
1055*e162b725SBarry Smith $     func (TS ts,PetscReal t,Vec f,void *ctx);
10569b7cd975SBarry Smith 
10579b7cd975SBarry Smith +   t - current timestep
1058*e162b725SBarry Smith .   f - output vector
10599b7cd975SBarry Smith -   ctx - [optional] user-defined function context
10609b7cd975SBarry Smith 
10619b7cd975SBarry Smith     Notes:
10629b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1063*e162b725SBarry 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
1064*e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1065*e162b725SBarry Smith 
1066*e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1067*e162b725SBarry Smith 
1068*e162b725SBarry 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
1069*e162b725SBarry Smith     parameters can be passed in the ctx variable.
10709b7cd975SBarry Smith 
10719b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
10729b7cd975SBarry Smith 
10739b7cd975SBarry Smith     Level: beginner
10749b7cd975SBarry Smith 
10759b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
10769b7cd975SBarry Smith 
10779b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
10789b7cd975SBarry Smith @*/
1079*e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
10809b7cd975SBarry Smith {
10819b7cd975SBarry Smith   PetscErrorCode ierr;
10829b7cd975SBarry Smith   DM             dm;
10839b7cd975SBarry Smith 
10849b7cd975SBarry Smith   PetscFunctionBegin;
10859b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10869b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1087*e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
10889b7cd975SBarry Smith   PetscFunctionReturn(0);
10899b7cd975SBarry Smith }
10909b7cd975SBarry Smith 
1091d763cef2SBarry Smith /*@C
1092f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1093b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1094d763cef2SBarry Smith 
10953f9fe445SBarry Smith    Logically Collective on TS
1096d763cef2SBarry Smith 
1097d763cef2SBarry Smith    Input Parameters:
1098d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1099e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1100e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1101d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1102d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11030298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1104d763cef2SBarry Smith 
1105f7ab8db6SBarry Smith    Calling sequence of f:
1106ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1107d763cef2SBarry Smith 
1108d763cef2SBarry Smith +  t - current timestep
1109d763cef2SBarry Smith .  u - input vector
1110e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1111e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1112d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1113d763cef2SBarry Smith 
11146cd88445SBarry Smith    Notes:
11156cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11166cd88445SBarry Smith 
11176cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1118ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1119d763cef2SBarry Smith 
1120d763cef2SBarry Smith    Level: beginner
1121d763cef2SBarry Smith 
1122d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1123d763cef2SBarry Smith 
1124ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1125d763cef2SBarry Smith 
1126d763cef2SBarry Smith @*/
1127e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1128d763cef2SBarry Smith {
1129277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1130089b2837SJed Brown   SNES           snes;
113124989b8cSPeter Brune   DM             dm;
113224989b8cSPeter Brune   TSIJacobian    ijacobian;
1133277b19d0SLisandro Dalcin 
1134d763cef2SBarry Smith   PetscFunctionBegin;
11350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1136e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1137e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1138e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1139e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1140d763cef2SBarry Smith 
114124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
114224989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1143e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1144e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1145e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1146e1244c69SJed Brown   }
11470298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1148089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11495f659677SPeter Brune   if (!ijacobian) {
1150e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
11510e4ef248SJed Brown   }
1152e5d3d808SBarry Smith   if (Amat) {
1153e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
11540e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1155e5d3d808SBarry Smith     ts->Arhs = Amat;
11560e4ef248SJed Brown   }
1157e5d3d808SBarry Smith   if (Pmat) {
1158e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
11590e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1160e5d3d808SBarry Smith     ts->Brhs = Pmat;
11610e4ef248SJed Brown   }
1162d763cef2SBarry Smith   PetscFunctionReturn(0);
1163d763cef2SBarry Smith }
1164d763cef2SBarry Smith 
1165316643e7SJed Brown 
1166316643e7SJed Brown /*@C
1167b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1168316643e7SJed Brown 
11693f9fe445SBarry Smith    Logically Collective on TS
1170316643e7SJed Brown 
1171316643e7SJed Brown    Input Parameters:
1172316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
11730298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1174316643e7SJed Brown .  f   - the function evaluation routine
11750298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1176316643e7SJed Brown 
1177316643e7SJed Brown    Calling sequence of f:
1178316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1179316643e7SJed Brown 
1180316643e7SJed Brown +  t   - time at step/stage being solved
1181316643e7SJed Brown .  u   - state vector
1182316643e7SJed Brown .  u_t - time derivative of state vector
1183316643e7SJed Brown .  F   - function vector
1184316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1185316643e7SJed Brown 
1186316643e7SJed Brown    Important:
11872bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1188316643e7SJed Brown 
1189316643e7SJed Brown    Level: beginner
1190316643e7SJed Brown 
1191316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1192316643e7SJed Brown 
1193d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1194316643e7SJed Brown @*/
119551699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1196316643e7SJed Brown {
1197089b2837SJed Brown   PetscErrorCode ierr;
1198089b2837SJed Brown   SNES           snes;
119951699248SLisandro Dalcin   Vec            ralloc = NULL;
120024989b8cSPeter Brune   DM             dm;
1201316643e7SJed Brown 
1202316643e7SJed Brown   PetscFunctionBegin;
12030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
120451699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
120524989b8cSPeter Brune 
120624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
120724989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
120824989b8cSPeter Brune 
1209089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
121051699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
121151699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
121251699248SLisandro Dalcin     r  = ralloc;
1213e856ceecSJed Brown   }
121451699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
121551699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1216089b2837SJed Brown   PetscFunctionReturn(0);
1217089b2837SJed Brown }
1218089b2837SJed Brown 
1219089b2837SJed Brown /*@C
1220089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1221089b2837SJed Brown 
1222089b2837SJed Brown    Not Collective
1223089b2837SJed Brown 
1224089b2837SJed Brown    Input Parameter:
1225089b2837SJed Brown .  ts - the TS context
1226089b2837SJed Brown 
1227089b2837SJed Brown    Output Parameter:
12280298fd71SBarry Smith +  r - vector to hold residual (or NULL)
12290298fd71SBarry Smith .  func - the function to compute residual (or NULL)
12300298fd71SBarry Smith -  ctx - the function context (or NULL)
1231089b2837SJed Brown 
1232089b2837SJed Brown    Level: advanced
1233089b2837SJed Brown 
1234089b2837SJed Brown .keywords: TS, nonlinear, get, function
1235089b2837SJed Brown 
1236089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1237089b2837SJed Brown @*/
1238089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1239089b2837SJed Brown {
1240089b2837SJed Brown   PetscErrorCode ierr;
1241089b2837SJed Brown   SNES           snes;
124224989b8cSPeter Brune   DM             dm;
1243089b2837SJed Brown 
1244089b2837SJed Brown   PetscFunctionBegin;
1245089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1246089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12470298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
124824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
124924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1250089b2837SJed Brown   PetscFunctionReturn(0);
1251089b2837SJed Brown }
1252089b2837SJed Brown 
1253089b2837SJed Brown /*@C
1254089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1255089b2837SJed Brown 
1256089b2837SJed Brown    Not Collective
1257089b2837SJed Brown 
1258089b2837SJed Brown    Input Parameter:
1259089b2837SJed Brown .  ts - the TS context
1260089b2837SJed Brown 
1261089b2837SJed Brown    Output Parameter:
12620298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
12630298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
12640298fd71SBarry Smith -  ctx - the function context (or NULL)
1265089b2837SJed Brown 
1266089b2837SJed Brown    Level: advanced
1267089b2837SJed Brown 
1268089b2837SJed Brown .keywords: TS, nonlinear, get, function
1269089b2837SJed Brown 
12702bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1271089b2837SJed Brown @*/
1272089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1273089b2837SJed Brown {
1274089b2837SJed Brown   PetscErrorCode ierr;
1275089b2837SJed Brown   SNES           snes;
127624989b8cSPeter Brune   DM             dm;
1277089b2837SJed Brown 
1278089b2837SJed Brown   PetscFunctionBegin;
1279089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1280089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12810298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
128224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
128324989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1284316643e7SJed Brown   PetscFunctionReturn(0);
1285316643e7SJed Brown }
1286316643e7SJed Brown 
1287316643e7SJed Brown /*@C
1288a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1289ae8867d6SBarry Smith         provided with TSSetIFunction().
1290316643e7SJed Brown 
12913f9fe445SBarry Smith    Logically Collective on TS
1292316643e7SJed Brown 
1293316643e7SJed Brown    Input Parameters:
1294316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1295e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1296e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1297316643e7SJed Brown .  f   - the Jacobian evaluation routine
12980298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1299316643e7SJed Brown 
1300316643e7SJed Brown    Calling sequence of f:
1301ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1302316643e7SJed Brown 
1303316643e7SJed Brown +  t    - time at step/stage being solved
13041b4a444bSJed Brown .  U    - state vector
13051b4a444bSJed Brown .  U_t  - time derivative of state vector
1306316643e7SJed Brown .  a    - shift
1307e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1308e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1309316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1310316643e7SJed Brown 
1311316643e7SJed Brown    Notes:
1312e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1313316643e7SJed Brown 
1314895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1315895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1316895c21f2SBarry Smith 
1317a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1318b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1319a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1320a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1321a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1322a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1323a4f0a591SBarry Smith 
13246cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13256cd88445SBarry Smith 
13266cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1327ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1328ca5f011dSBarry Smith 
1329316643e7SJed Brown    Level: beginner
1330316643e7SJed Brown 
1331316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1332316643e7SJed Brown 
1333ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1334316643e7SJed Brown 
1335316643e7SJed Brown @*/
1336e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1337316643e7SJed Brown {
1338316643e7SJed Brown   PetscErrorCode ierr;
1339089b2837SJed Brown   SNES           snes;
134024989b8cSPeter Brune   DM             dm;
1341316643e7SJed Brown 
1342316643e7SJed Brown   PetscFunctionBegin;
13430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1344e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1345e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1346e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1347e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
134824989b8cSPeter Brune 
134924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
135024989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
135124989b8cSPeter Brune 
1352089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1353e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1354316643e7SJed Brown   PetscFunctionReturn(0);
1355316643e7SJed Brown }
1356316643e7SJed Brown 
1357e1244c69SJed Brown /*@
1358e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1359e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1360e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1361e1244c69SJed Brown    not been changed by the TS.
1362e1244c69SJed Brown 
1363e1244c69SJed Brown    Logically Collective
1364e1244c69SJed Brown 
1365e1244c69SJed Brown    Input Arguments:
1366e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1367e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1368e1244c69SJed Brown 
1369e1244c69SJed Brown    Level: intermediate
1370e1244c69SJed Brown 
1371e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1372e1244c69SJed Brown @*/
1373e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1374e1244c69SJed Brown {
1375e1244c69SJed Brown   PetscFunctionBegin;
1376e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1377e1244c69SJed Brown   PetscFunctionReturn(0);
1378e1244c69SJed Brown }
1379e1244c69SJed Brown 
1380efe9872eSLisandro Dalcin /*@C
1381efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1382efe9872eSLisandro Dalcin 
1383efe9872eSLisandro Dalcin    Logically Collective on TS
1384efe9872eSLisandro Dalcin 
1385efe9872eSLisandro Dalcin    Input Parameters:
1386efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1387efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1388efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1389efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1390efe9872eSLisandro Dalcin 
1391efe9872eSLisandro Dalcin    Calling sequence of fun:
1392efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1393efe9872eSLisandro Dalcin 
1394efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1395efe9872eSLisandro Dalcin .  U    - state vector
1396efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1397efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1398efe9872eSLisandro Dalcin .  F    - function vector
1399efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1400efe9872eSLisandro Dalcin 
1401efe9872eSLisandro Dalcin    Level: beginner
1402efe9872eSLisandro Dalcin 
1403efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1404efe9872eSLisandro Dalcin 
1405efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1406efe9872eSLisandro Dalcin @*/
1407efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1408efe9872eSLisandro Dalcin {
1409efe9872eSLisandro Dalcin   DM             dm;
1410efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1411efe9872eSLisandro Dalcin 
1412efe9872eSLisandro Dalcin   PetscFunctionBegin;
1413efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1414efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1415efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1416efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1417efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1418efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1419efe9872eSLisandro Dalcin }
1420efe9872eSLisandro Dalcin 
1421efe9872eSLisandro Dalcin /*@C
1422efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1423efe9872eSLisandro Dalcin 
1424efe9872eSLisandro Dalcin   Not Collective
1425efe9872eSLisandro Dalcin 
1426efe9872eSLisandro Dalcin   Input Parameter:
1427efe9872eSLisandro Dalcin . ts - the TS context
1428efe9872eSLisandro Dalcin 
1429efe9872eSLisandro Dalcin   Output Parameter:
1430efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1431efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1432efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1433efe9872eSLisandro Dalcin 
1434efe9872eSLisandro Dalcin   Level: advanced
1435efe9872eSLisandro Dalcin 
1436efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1437efe9872eSLisandro Dalcin 
1438efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1439efe9872eSLisandro Dalcin @*/
1440efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1441efe9872eSLisandro Dalcin {
1442efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1443efe9872eSLisandro Dalcin   SNES           snes;
1444efe9872eSLisandro Dalcin   DM             dm;
1445efe9872eSLisandro Dalcin 
1446efe9872eSLisandro Dalcin   PetscFunctionBegin;
1447efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1448efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1449efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1450efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1451efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1452efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1453efe9872eSLisandro Dalcin }
1454efe9872eSLisandro Dalcin 
1455efe9872eSLisandro Dalcin /*@C
1456bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1457efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1458efe9872eSLisandro Dalcin 
1459efe9872eSLisandro Dalcin    Logically Collective on TS
1460efe9872eSLisandro Dalcin 
1461efe9872eSLisandro Dalcin    Input Parameters:
1462efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1463efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1464efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1465efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1466efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1467efe9872eSLisandro Dalcin 
1468efe9872eSLisandro Dalcin    Calling sequence of jac:
1469bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1470efe9872eSLisandro Dalcin 
1471efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1472efe9872eSLisandro Dalcin .  U    - state vector
1473efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1474efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1475efe9872eSLisandro Dalcin .  v    - shift for U_t
1476efe9872eSLisandro Dalcin .  a    - shift for U_tt
1477efe9872eSLisandro 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
1478efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1479efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1480efe9872eSLisandro Dalcin 
1481efe9872eSLisandro Dalcin    Notes:
1482efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1483efe9872eSLisandro Dalcin 
1484efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1485efe9872eSLisandro 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.
1486efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1487bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1488efe9872eSLisandro Dalcin 
1489efe9872eSLisandro Dalcin    Level: beginner
1490efe9872eSLisandro Dalcin 
1491efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1492efe9872eSLisandro Dalcin 
1493efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1494efe9872eSLisandro Dalcin @*/
1495efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1496efe9872eSLisandro Dalcin {
1497efe9872eSLisandro Dalcin   DM             dm;
1498efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1499efe9872eSLisandro Dalcin 
1500efe9872eSLisandro Dalcin   PetscFunctionBegin;
1501efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1502efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1503efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1504efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1505efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1506efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1507efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1508efe9872eSLisandro Dalcin }
1509efe9872eSLisandro Dalcin 
1510efe9872eSLisandro Dalcin /*@C
1511efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1512efe9872eSLisandro Dalcin 
1513efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1514efe9872eSLisandro Dalcin 
1515efe9872eSLisandro Dalcin   Input Parameter:
1516efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1517efe9872eSLisandro Dalcin 
1518efe9872eSLisandro Dalcin   Output Parameters:
1519efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1520efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1521efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1522efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1523efe9872eSLisandro Dalcin 
1524efe9872eSLisandro Dalcin   Notes: You can pass in NULL for any return argument you do not need.
1525efe9872eSLisandro Dalcin 
1526efe9872eSLisandro Dalcin   Level: advanced
1527efe9872eSLisandro Dalcin 
1528efe9872eSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
1529efe9872eSLisandro Dalcin 
1530efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1531efe9872eSLisandro Dalcin @*/
1532efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1533efe9872eSLisandro Dalcin {
1534efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1535efe9872eSLisandro Dalcin   SNES           snes;
1536efe9872eSLisandro Dalcin   DM             dm;
1537efe9872eSLisandro Dalcin 
1538efe9872eSLisandro Dalcin   PetscFunctionBegin;
1539efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1540efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1541efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1542efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1543efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1544efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1545efe9872eSLisandro Dalcin }
1546efe9872eSLisandro Dalcin 
1547efe9872eSLisandro Dalcin /*@
1548efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1549efe9872eSLisandro Dalcin 
1550efe9872eSLisandro Dalcin   Collective on TS and Vec
1551efe9872eSLisandro Dalcin 
1552efe9872eSLisandro Dalcin   Input Parameters:
1553efe9872eSLisandro Dalcin + ts - the TS context
1554efe9872eSLisandro Dalcin . t - current time
1555efe9872eSLisandro Dalcin . U - state vector
1556efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1557efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1558efe9872eSLisandro Dalcin 
1559efe9872eSLisandro Dalcin   Output Parameter:
1560efe9872eSLisandro Dalcin . F - the residual vector
1561efe9872eSLisandro Dalcin 
1562efe9872eSLisandro Dalcin   Note:
1563efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1564efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1565efe9872eSLisandro Dalcin 
1566efe9872eSLisandro Dalcin   Level: developer
1567efe9872eSLisandro Dalcin 
1568efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1569efe9872eSLisandro Dalcin 
1570efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1571efe9872eSLisandro Dalcin @*/
1572efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1573efe9872eSLisandro Dalcin {
1574efe9872eSLisandro Dalcin   DM             dm;
1575efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1576efe9872eSLisandro Dalcin   void           *ctx;
1577efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1578efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1579efe9872eSLisandro Dalcin 
1580efe9872eSLisandro Dalcin   PetscFunctionBegin;
1581efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1582efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1583efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1584efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1585efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1586efe9872eSLisandro Dalcin 
1587efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1588efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1589efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1590efe9872eSLisandro Dalcin 
1591efe9872eSLisandro Dalcin   if (!I2Function) {
1592efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1593efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1594efe9872eSLisandro Dalcin   }
1595efe9872eSLisandro Dalcin 
1596efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1597efe9872eSLisandro Dalcin 
1598efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1599efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1600efe9872eSLisandro Dalcin   PetscStackPop;
1601efe9872eSLisandro Dalcin 
1602efe9872eSLisandro Dalcin   if (rhsfunction) {
1603efe9872eSLisandro Dalcin     Vec Frhs;
1604efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1605efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1606efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1607efe9872eSLisandro Dalcin   }
1608efe9872eSLisandro Dalcin 
1609efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1610efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1611efe9872eSLisandro Dalcin }
1612efe9872eSLisandro Dalcin 
1613efe9872eSLisandro Dalcin /*@
1614efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1615efe9872eSLisandro Dalcin 
1616efe9872eSLisandro Dalcin   Collective on TS and Vec
1617efe9872eSLisandro Dalcin 
1618efe9872eSLisandro Dalcin   Input Parameters:
1619efe9872eSLisandro Dalcin + ts - the TS context
1620efe9872eSLisandro Dalcin . t - current timestep
1621efe9872eSLisandro Dalcin . U - state vector
1622efe9872eSLisandro Dalcin . V - time derivative of state vector
1623efe9872eSLisandro Dalcin . A - second time derivative of state vector
1624efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1625efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1626efe9872eSLisandro Dalcin 
1627efe9872eSLisandro Dalcin   Output Parameters:
1628efe9872eSLisandro Dalcin + J - Jacobian matrix
1629efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1630efe9872eSLisandro Dalcin 
1631efe9872eSLisandro Dalcin   Notes:
1632efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1633efe9872eSLisandro Dalcin 
1634efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1635efe9872eSLisandro Dalcin 
1636efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1637efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1638efe9872eSLisandro Dalcin 
1639efe9872eSLisandro Dalcin   Level: developer
1640efe9872eSLisandro Dalcin 
1641efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1642efe9872eSLisandro Dalcin 
1643efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1644efe9872eSLisandro Dalcin @*/
1645efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1646efe9872eSLisandro Dalcin {
1647efe9872eSLisandro Dalcin   DM             dm;
1648efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1649efe9872eSLisandro Dalcin   void           *ctx;
1650efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1651efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1652efe9872eSLisandro Dalcin 
1653efe9872eSLisandro Dalcin   PetscFunctionBegin;
1654efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1655efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1656efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1657efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1658efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1659efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1660efe9872eSLisandro Dalcin 
1661efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1662efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1663efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1664efe9872eSLisandro Dalcin 
1665efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1666efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1667efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1668efe9872eSLisandro Dalcin   }
1669efe9872eSLisandro Dalcin 
1670efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1671efe9872eSLisandro Dalcin 
1672efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1673efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1674efe9872eSLisandro Dalcin   PetscStackPop;
1675efe9872eSLisandro Dalcin 
1676efe9872eSLisandro Dalcin   if (rhsjacobian) {
1677efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1678efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1679efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1680efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1681efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1682efe9872eSLisandro Dalcin   }
1683efe9872eSLisandro Dalcin 
1684efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1685efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1686efe9872eSLisandro Dalcin }
1687efe9872eSLisandro Dalcin 
1688efe9872eSLisandro Dalcin /*@
1689efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1690efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1691efe9872eSLisandro Dalcin 
1692efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1693efe9872eSLisandro Dalcin 
1694efe9872eSLisandro Dalcin    Input Parameters:
1695efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1696efe9872eSLisandro Dalcin .  u - the solution vector
1697efe9872eSLisandro Dalcin -  v - the time derivative vector
1698efe9872eSLisandro Dalcin 
1699efe9872eSLisandro Dalcin    Level: beginner
1700efe9872eSLisandro Dalcin 
1701efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1702efe9872eSLisandro Dalcin @*/
1703efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1704efe9872eSLisandro Dalcin {
1705efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1706efe9872eSLisandro Dalcin 
1707efe9872eSLisandro Dalcin   PetscFunctionBegin;
1708efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1709efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1710efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1711efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1712efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1713efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1714efe9872eSLisandro Dalcin   ts->vec_dot = v;
1715efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1716efe9872eSLisandro Dalcin }
1717efe9872eSLisandro Dalcin 
1718efe9872eSLisandro Dalcin /*@
1719efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1720efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1721efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1722efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1723efe9872eSLisandro Dalcin 
1724efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1725efe9872eSLisandro Dalcin 
1726efe9872eSLisandro Dalcin    Input Parameter:
1727efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1728efe9872eSLisandro Dalcin 
1729efe9872eSLisandro Dalcin    Output Parameter:
1730efe9872eSLisandro Dalcin +  u - the vector containing the solution
1731efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1732efe9872eSLisandro Dalcin 
1733efe9872eSLisandro Dalcin    Level: intermediate
1734efe9872eSLisandro Dalcin 
1735efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1736efe9872eSLisandro Dalcin 
1737efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1738efe9872eSLisandro Dalcin @*/
1739efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1740efe9872eSLisandro Dalcin {
1741efe9872eSLisandro Dalcin   PetscFunctionBegin;
1742efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1743efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1744efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1745efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1746efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1747efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1748efe9872eSLisandro Dalcin }
1749efe9872eSLisandro Dalcin 
175055849f57SBarry Smith /*@C
175155849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
175255849f57SBarry Smith 
175355849f57SBarry Smith   Collective on PetscViewer
175455849f57SBarry Smith 
175555849f57SBarry Smith   Input Parameters:
175655849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
175755849f57SBarry Smith            some related function before a call to TSLoad().
175855849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
175955849f57SBarry Smith 
176055849f57SBarry Smith    Level: intermediate
176155849f57SBarry Smith 
176255849f57SBarry Smith   Notes:
176355849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
176455849f57SBarry Smith 
176555849f57SBarry Smith   Notes for advanced users:
176655849f57SBarry Smith   Most users should not need to know the details of the binary storage
176755849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
176855849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
176955849f57SBarry Smith   format is
177055849f57SBarry Smith .vb
177155849f57SBarry Smith      has not yet been determined
177255849f57SBarry Smith .ve
177355849f57SBarry Smith 
177455849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
177555849f57SBarry Smith @*/
1776f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
177755849f57SBarry Smith {
177855849f57SBarry Smith   PetscErrorCode ierr;
177955849f57SBarry Smith   PetscBool      isbinary;
178055849f57SBarry Smith   PetscInt       classid;
178155849f57SBarry Smith   char           type[256];
17822d53ad75SBarry Smith   DMTS           sdm;
1783ad6bc421SBarry Smith   DM             dm;
178455849f57SBarry Smith 
178555849f57SBarry Smith   PetscFunctionBegin;
1786f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
178755849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
178855849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
178955849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
179055849f57SBarry Smith 
1791060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1792ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1793060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1794f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1795f2c2a1b9SBarry Smith   if (ts->ops->load) {
1796f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1797f2c2a1b9SBarry Smith   }
1798ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1799ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1800ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1801f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1802f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
18032d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18042d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
180555849f57SBarry Smith   PetscFunctionReturn(0);
180655849f57SBarry Smith }
180755849f57SBarry Smith 
18089804daf3SBarry Smith #include <petscdraw.h>
1809e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1810e04113cfSBarry Smith #include <petscviewersaws.h>
1811f05ece33SBarry Smith #endif
18127e2c5f70SBarry Smith /*@C
1813d763cef2SBarry Smith     TSView - Prints the TS data structure.
1814d763cef2SBarry Smith 
18154c49b128SBarry Smith     Collective on TS
1816d763cef2SBarry Smith 
1817d763cef2SBarry Smith     Input Parameters:
1818d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1819d763cef2SBarry Smith -   viewer - visualization context
1820d763cef2SBarry Smith 
1821d763cef2SBarry Smith     Options Database Key:
1822d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1823d763cef2SBarry Smith 
1824d763cef2SBarry Smith     Notes:
1825d763cef2SBarry Smith     The available visualization contexts include
1826b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1827b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1828d763cef2SBarry Smith          output where only the first processor opens
1829d763cef2SBarry Smith          the file.  All other processors send their
1830d763cef2SBarry Smith          data to the first processor to print.
1831d763cef2SBarry Smith 
1832d763cef2SBarry Smith     The user can open an alternative visualization context with
1833b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1834d763cef2SBarry Smith 
1835d763cef2SBarry Smith     Level: beginner
1836d763cef2SBarry Smith 
1837d763cef2SBarry Smith .keywords: TS, timestep, view
1838d763cef2SBarry Smith 
1839b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1840d763cef2SBarry Smith @*/
18417087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1842d763cef2SBarry Smith {
1843dfbe8321SBarry Smith   PetscErrorCode ierr;
184419fd82e9SBarry Smith   TSType         type;
18452b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
18462d53ad75SBarry Smith   DMTS           sdm;
1847e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1848536b137fSBarry Smith   PetscBool      issaws;
1849f05ece33SBarry Smith #endif
1850d763cef2SBarry Smith 
1851d763cef2SBarry Smith   PetscFunctionBegin;
18520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18533050cee2SBarry Smith   if (!viewer) {
1854ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
18553050cee2SBarry Smith   }
18560700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1857c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1858fd16b177SBarry Smith 
1859251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1860251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
186155849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
18622b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1863e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1864536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1865f05ece33SBarry Smith #endif
186632077d6dSBarry Smith   if (iascii) {
1867dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
186877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
18697c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1870d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
18715ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1872c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1873d763cef2SBarry Smith     }
18745ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1875193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1876a0af407cSBarry Smith     if (ts->vrtol) {
1877a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1878a0af407cSBarry Smith     } else {
1879a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
1880a0af407cSBarry Smith     }
1881a0af407cSBarry Smith     if (ts->vatol) {
1882a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
1883a0af407cSBarry Smith     } else {
1884a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
1885a0af407cSBarry Smith     }
18862d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18872d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1888d52bd9f3SBarry Smith     if (ts->ops->view) {
1889d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1890d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1891d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1892d52bd9f3SBarry Smith     }
18930f5bd95cSBarry Smith   } else if (isstring) {
1894a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1895b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
189655849f57SBarry Smith   } else if (isbinary) {
189755849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
189855849f57SBarry Smith     MPI_Comm    comm;
189955849f57SBarry Smith     PetscMPIInt rank;
190055849f57SBarry Smith     char        type[256];
190155849f57SBarry Smith 
190255849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
190355849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
190455849f57SBarry Smith     if (!rank) {
190555849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
190655849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
190755849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
190855849f57SBarry Smith     }
190955849f57SBarry Smith     if (ts->ops->view) {
191055849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
191155849f57SBarry Smith     }
1912f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1913f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
19142d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19152d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19162b0a91c0SBarry Smith   } else if (isdraw) {
19172b0a91c0SBarry Smith     PetscDraw draw;
19182b0a91c0SBarry Smith     char      str[36];
191989fd9fafSBarry Smith     PetscReal x,y,bottom,h;
19202b0a91c0SBarry Smith 
19212b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
19222b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
19232b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
19242b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
192551fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
192689fd9fafSBarry Smith     bottom = y - h;
19272b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
19282b0a91c0SBarry Smith     if (ts->ops->view) {
19292b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
19302b0a91c0SBarry Smith     }
19312b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1932e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1933536b137fSBarry Smith   } else if (issaws) {
1934d45a07a7SBarry Smith     PetscMPIInt rank;
19352657e9d9SBarry Smith     const char  *name;
19362657e9d9SBarry Smith 
19372657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1938d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1939d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1940d45a07a7SBarry Smith       char       dir[1024];
1941d45a07a7SBarry Smith 
1942e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1943a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
19442657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
19452657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
19462657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1947d763cef2SBarry Smith     }
19480acecf5bSBarry Smith     if (ts->ops->view) {
19490acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
19500acecf5bSBarry Smith     }
1951f05ece33SBarry Smith #endif
1952f05ece33SBarry Smith   }
1953f05ece33SBarry Smith 
1954b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1955251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1956b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1957d763cef2SBarry Smith   PetscFunctionReturn(0);
1958d763cef2SBarry Smith }
1959d763cef2SBarry Smith 
1960d763cef2SBarry Smith 
1961b07ff414SBarry Smith /*@
1962d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1963d763cef2SBarry Smith    the timesteppers.
1964d763cef2SBarry Smith 
19653f9fe445SBarry Smith    Logically Collective on TS
1966d763cef2SBarry Smith 
1967d763cef2SBarry Smith    Input Parameters:
1968d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1969d763cef2SBarry Smith -  usrP - optional user context
1970d763cef2SBarry Smith 
1971daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
1972daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
1973daf670e6SBarry Smith 
1974d763cef2SBarry Smith    Level: intermediate
1975d763cef2SBarry Smith 
1976d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1977d763cef2SBarry Smith 
1978d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1979d763cef2SBarry Smith @*/
19807087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1981d763cef2SBarry Smith {
1982d763cef2SBarry Smith   PetscFunctionBegin;
19830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1984d763cef2SBarry Smith   ts->user = usrP;
1985d763cef2SBarry Smith   PetscFunctionReturn(0);
1986d763cef2SBarry Smith }
1987d763cef2SBarry Smith 
1988b07ff414SBarry Smith /*@
1989d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1990d763cef2SBarry Smith     timestepper.
1991d763cef2SBarry Smith 
1992d763cef2SBarry Smith     Not Collective
1993d763cef2SBarry Smith 
1994d763cef2SBarry Smith     Input Parameter:
1995d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1996d763cef2SBarry Smith 
1997d763cef2SBarry Smith     Output Parameter:
1998d763cef2SBarry Smith .   usrP - user context
1999d763cef2SBarry Smith 
2000daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2001daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2002daf670e6SBarry Smith 
2003d763cef2SBarry Smith     Level: intermediate
2004d763cef2SBarry Smith 
2005d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2006d763cef2SBarry Smith 
2007d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2008d763cef2SBarry Smith @*/
2009e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2010d763cef2SBarry Smith {
2011d763cef2SBarry Smith   PetscFunctionBegin;
20120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2013e71120c6SJed Brown   *(void**)usrP = ts->user;
2014d763cef2SBarry Smith   PetscFunctionReturn(0);
2015d763cef2SBarry Smith }
2016d763cef2SBarry Smith 
2017d763cef2SBarry Smith /*@
2018b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
2019d763cef2SBarry Smith 
2020d763cef2SBarry Smith    Not Collective
2021d763cef2SBarry Smith 
2022d763cef2SBarry Smith    Input Parameter:
2023d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2024d763cef2SBarry Smith 
2025d763cef2SBarry Smith    Output Parameter:
2026b8123daeSJed Brown .  iter - number of steps completed so far
2027d763cef2SBarry Smith 
2028d763cef2SBarry Smith    Level: intermediate
2029d763cef2SBarry Smith 
2030d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
20319be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2032d763cef2SBarry Smith @*/
20337087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
2034d763cef2SBarry Smith {
2035d763cef2SBarry Smith   PetscFunctionBegin;
20360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20374482741eSBarry Smith   PetscValidIntPointer(iter,2);
2038d763cef2SBarry Smith   *iter = ts->steps;
2039d763cef2SBarry Smith   PetscFunctionReturn(0);
2040d763cef2SBarry Smith }
2041d763cef2SBarry Smith 
2042d763cef2SBarry Smith /*@
2043d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
2044d763cef2SBarry Smith    as well as the initial time.
2045d763cef2SBarry Smith 
20463f9fe445SBarry Smith    Logically Collective on TS
2047d763cef2SBarry Smith 
2048d763cef2SBarry Smith    Input Parameters:
2049d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2050d763cef2SBarry Smith .  initial_time - the initial time
2051d763cef2SBarry Smith -  time_step - the size of the timestep
2052d763cef2SBarry Smith 
2053d763cef2SBarry Smith    Level: intermediate
2054d763cef2SBarry Smith 
2055d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
2056d763cef2SBarry Smith 
2057d763cef2SBarry Smith .keywords: TS, set, initial, timestep
2058d763cef2SBarry Smith @*/
20597087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
2060d763cef2SBarry Smith {
2061e144a568SJed Brown   PetscErrorCode ierr;
2062e144a568SJed Brown 
2063d763cef2SBarry Smith   PetscFunctionBegin;
20640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2065e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
2066d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
2067d763cef2SBarry Smith   PetscFunctionReturn(0);
2068d763cef2SBarry Smith }
2069d763cef2SBarry Smith 
2070d763cef2SBarry Smith /*@
2071d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2072d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2073d763cef2SBarry Smith 
20743f9fe445SBarry Smith    Logically Collective on TS
2075d763cef2SBarry Smith 
2076d763cef2SBarry Smith    Input Parameters:
2077d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2078d763cef2SBarry Smith -  time_step - the size of the timestep
2079d763cef2SBarry Smith 
2080d763cef2SBarry Smith    Level: intermediate
2081d763cef2SBarry Smith 
2082d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2083d763cef2SBarry Smith 
2084d763cef2SBarry Smith .keywords: TS, set, timestep
2085d763cef2SBarry Smith @*/
20867087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2087d763cef2SBarry Smith {
2088d763cef2SBarry Smith   PetscFunctionBegin;
20890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2090c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2091d763cef2SBarry Smith   ts->time_step = time_step;
2092d763cef2SBarry Smith   PetscFunctionReturn(0);
2093d763cef2SBarry Smith }
2094d763cef2SBarry Smith 
2095a43b19c4SJed Brown /*@
209649354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
209749354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
209849354f04SShri Abhyankar      or just return at the final time TS computed.
2099a43b19c4SJed Brown 
2100a43b19c4SJed Brown   Logically Collective on TS
2101a43b19c4SJed Brown 
2102a43b19c4SJed Brown    Input Parameter:
2103a43b19c4SJed Brown +   ts - the time-step context
210449354f04SShri Abhyankar -   eftopt - exact final time option
2105a43b19c4SJed Brown 
2106feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2107feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2108feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2109feed9e9dSBarry Smith 
2110feed9e9dSBarry Smith    Options Database:
2111feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2112feed9e9dSBarry Smith 
2113ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2114ee346746SBarry Smith     then the final time you selected.
2115ee346746SBarry Smith 
2116a43b19c4SJed Brown    Level: beginner
2117a43b19c4SJed Brown 
2118a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
2119a43b19c4SJed Brown @*/
212049354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2121a43b19c4SJed Brown {
2122a43b19c4SJed Brown   PetscFunctionBegin;
2123a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
212449354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
212549354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2126a43b19c4SJed Brown   PetscFunctionReturn(0);
2127a43b19c4SJed Brown }
2128a43b19c4SJed Brown 
2129d763cef2SBarry Smith /*@
2130d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2131d763cef2SBarry Smith 
2132d763cef2SBarry Smith    Not Collective
2133d763cef2SBarry Smith 
2134d763cef2SBarry Smith    Input Parameter:
2135d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2136d763cef2SBarry Smith 
2137d763cef2SBarry Smith    Output Parameter:
2138d763cef2SBarry Smith .  dt - the current timestep size
2139d763cef2SBarry Smith 
2140d763cef2SBarry Smith    Level: intermediate
2141d763cef2SBarry Smith 
2142d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2143d763cef2SBarry Smith 
2144d763cef2SBarry Smith .keywords: TS, get, timestep
2145d763cef2SBarry Smith @*/
21467087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2147d763cef2SBarry Smith {
2148d763cef2SBarry Smith   PetscFunctionBegin;
21490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2150f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2151d763cef2SBarry Smith   *dt = ts->time_step;
2152d763cef2SBarry Smith   PetscFunctionReturn(0);
2153d763cef2SBarry Smith }
2154d763cef2SBarry Smith 
2155d8e5e3e6SSatish Balay /*@
2156d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2157d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2158d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2159d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2160d763cef2SBarry Smith 
2161d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2162d763cef2SBarry Smith 
2163d763cef2SBarry Smith    Input Parameter:
2164d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2165d763cef2SBarry Smith 
2166d763cef2SBarry Smith    Output Parameter:
2167d763cef2SBarry Smith .  v - the vector containing the solution
2168d763cef2SBarry Smith 
216963e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
217063e21af5SBarry Smith    final time. It returns the solution at the next timestep.
217163e21af5SBarry Smith 
2172d763cef2SBarry Smith    Level: intermediate
2173d763cef2SBarry Smith 
2174b2bf4f3aSDebojyoti Ghosh .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents()
2175d763cef2SBarry Smith 
2176d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2177d763cef2SBarry Smith @*/
21787087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2179d763cef2SBarry Smith {
2180d763cef2SBarry Smith   PetscFunctionBegin;
21810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21824482741eSBarry Smith   PetscValidPointer(v,2);
21838737fe31SLisandro Dalcin   *v = ts->vec_sol;
2184d763cef2SBarry Smith   PetscFunctionReturn(0);
2185d763cef2SBarry Smith }
2186d763cef2SBarry Smith 
218703fe5f5eSDebojyoti Ghosh /*@
2188b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
218903fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2190b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
219103fe5f5eSDebojyoti Ghosh    structure as the solution vector.
219203fe5f5eSDebojyoti Ghosh 
219303fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
219403fe5f5eSDebojyoti Ghosh 
219503fe5f5eSDebojyoti Ghosh    Parameters :
219603fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2197b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2198b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
219903fe5f5eSDebojyoti Ghosh        returned in v.
2200b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
220103fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2202b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
220303fe5f5eSDebojyoti Ghosh 
22044cdd57e5SDebojyoti Ghosh    Level: advanced
220503fe5f5eSDebojyoti Ghosh 
220603fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
220703fe5f5eSDebojyoti Ghosh 
220803fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
220903fe5f5eSDebojyoti Ghosh @*/
2210b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
221103fe5f5eSDebojyoti Ghosh {
221203fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
221303fe5f5eSDebojyoti Ghosh 
221403fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
221503fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2216b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
221703fe5f5eSDebojyoti Ghosh   else {
2218b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
221903fe5f5eSDebojyoti Ghosh   }
222003fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
222103fe5f5eSDebojyoti Ghosh }
222203fe5f5eSDebojyoti Ghosh 
22234cdd57e5SDebojyoti Ghosh /*@
22244cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
22254cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
22264cdd57e5SDebojyoti Ghosh 
22274cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
22284cdd57e5SDebojyoti Ghosh 
22294cdd57e5SDebojyoti Ghosh    Parameters :
22304cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
22314cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
22324cdd57e5SDebojyoti Ghosh 
22334cdd57e5SDebojyoti Ghosh    Level: intermediate
22344cdd57e5SDebojyoti Ghosh 
22354cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
22364cdd57e5SDebojyoti Ghosh 
22374cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
22384cdd57e5SDebojyoti Ghosh @*/
22394cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
22404cdd57e5SDebojyoti Ghosh {
22414cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
22424cdd57e5SDebojyoti Ghosh 
22434cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
22444cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2245f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
22464cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2247f6356ec7SDebojyoti Ghosh   } else {
2248f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2249f6356ec7SDebojyoti Ghosh   }
22504cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
22514cdd57e5SDebojyoti Ghosh }
22524cdd57e5SDebojyoti Ghosh 
22534cdd57e5SDebojyoti Ghosh /*@
22544cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
22554cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
22564cdd57e5SDebojyoti Ghosh 
22574cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
22584cdd57e5SDebojyoti Ghosh 
22599657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
22609657682dSDebojyoti Ghosh 
22614cdd57e5SDebojyoti Ghosh    Parameters :
22624cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2263657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
22644cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
22654cdd57e5SDebojyoti Ghosh 
22664cdd57e5SDebojyoti Ghosh    Level: intermediate
22674cdd57e5SDebojyoti Ghosh 
226857df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
22694cdd57e5SDebojyoti Ghosh 
22704cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
22714cdd57e5SDebojyoti Ghosh @*/
22720a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
22734cdd57e5SDebojyoti Ghosh {
22744cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
22754cdd57e5SDebojyoti Ghosh 
22764cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
22774cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2278f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
22790a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2280f6356ec7SDebojyoti Ghosh   } else {
2281f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2282f6356ec7SDebojyoti Ghosh   }
22834cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
22844cdd57e5SDebojyoti Ghosh }
22854cdd57e5SDebojyoti Ghosh 
228657df6a1bSDebojyoti Ghosh /*@
228757df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
228857df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
228957df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
229057df6a1bSDebojyoti Ghosh 
229157df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
229257df6a1bSDebojyoti Ghosh 
229357df6a1bSDebojyoti Ghosh    Parameters :
229457df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
229557df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
229657df6a1bSDebojyoti Ghosh 
229757df6a1bSDebojyoti Ghosh    Level: intermediate
229857df6a1bSDebojyoti Ghosh 
229957df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
230057df6a1bSDebojyoti Ghosh 
230157df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
230257df6a1bSDebojyoti Ghosh @*/
230357df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
230457df6a1bSDebojyoti Ghosh {
230557df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
230657df6a1bSDebojyoti Ghosh 
230757df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
230857df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23099657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
231057df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
231157df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
231257df6a1bSDebojyoti Ghosh   }
231357df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
231457df6a1bSDebojyoti Ghosh }
231557df6a1bSDebojyoti Ghosh 
2316c235aa8dSHong Zhang /*@
2317dfb21088SHong Zhang    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()
2318c235aa8dSHong Zhang 
2319c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
2320c235aa8dSHong Zhang 
2321c235aa8dSHong Zhang    Input Parameter:
2322c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
2323c235aa8dSHong Zhang 
2324c235aa8dSHong Zhang    Output Parameter:
2325abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
2326abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
2327c235aa8dSHong Zhang 
2328c235aa8dSHong Zhang    Level: intermediate
2329c235aa8dSHong Zhang 
2330c235aa8dSHong Zhang .seealso: TSGetTimeStep()
2331c235aa8dSHong Zhang 
2332c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
2333c235aa8dSHong Zhang @*/
2334dfb21088SHong Zhang PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
2335c235aa8dSHong Zhang {
2336c235aa8dSHong Zhang   PetscFunctionBegin;
2337c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2338abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
2339abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
2340abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
2341c235aa8dSHong Zhang   PetscFunctionReturn(0);
2342c235aa8dSHong Zhang }
2343c235aa8dSHong Zhang 
2344bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2345d8e5e3e6SSatish Balay /*@
2346bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2347d763cef2SBarry Smith 
2348bdad233fSMatthew Knepley   Not collective
2349d763cef2SBarry Smith 
2350bdad233fSMatthew Knepley   Input Parameters:
2351bdad233fSMatthew Knepley + ts   - The TS
2352bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2353d763cef2SBarry Smith .vb
23540910c330SBarry Smith          U_t - A U = 0      (linear)
23550910c330SBarry Smith          U_t - A(t) U = 0   (linear)
23560910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2357d763cef2SBarry Smith .ve
2358d763cef2SBarry Smith 
2359d763cef2SBarry Smith    Level: beginner
2360d763cef2SBarry Smith 
2361bdad233fSMatthew Knepley .keywords: TS, problem type
2362bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2363d763cef2SBarry Smith @*/
23647087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2365a7cc72afSBarry Smith {
23669e2a6581SJed Brown   PetscErrorCode ierr;
23679e2a6581SJed Brown 
2368d763cef2SBarry Smith   PetscFunctionBegin;
23690700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2370bdad233fSMatthew Knepley   ts->problem_type = type;
23719e2a6581SJed Brown   if (type == TS_LINEAR) {
23729e2a6581SJed Brown     SNES snes;
23739e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
23749e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
23759e2a6581SJed Brown   }
2376d763cef2SBarry Smith   PetscFunctionReturn(0);
2377d763cef2SBarry Smith }
2378d763cef2SBarry Smith 
2379bdad233fSMatthew Knepley /*@C
2380bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2381bdad233fSMatthew Knepley 
2382bdad233fSMatthew Knepley   Not collective
2383bdad233fSMatthew Knepley 
2384bdad233fSMatthew Knepley   Input Parameter:
2385bdad233fSMatthew Knepley . ts   - The TS
2386bdad233fSMatthew Knepley 
2387bdad233fSMatthew Knepley   Output Parameter:
2388bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2389bdad233fSMatthew Knepley .vb
2390089b2837SJed Brown          M U_t = A U
2391089b2837SJed Brown          M(t) U_t = A(t) U
2392b5abc632SBarry Smith          F(t,U,U_t)
2393bdad233fSMatthew Knepley .ve
2394bdad233fSMatthew Knepley 
2395bdad233fSMatthew Knepley    Level: beginner
2396bdad233fSMatthew Knepley 
2397bdad233fSMatthew Knepley .keywords: TS, problem type
2398bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2399bdad233fSMatthew Knepley @*/
24007087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2401a7cc72afSBarry Smith {
2402bdad233fSMatthew Knepley   PetscFunctionBegin;
24030700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
24044482741eSBarry Smith   PetscValidIntPointer(type,2);
2405bdad233fSMatthew Knepley   *type = ts->problem_type;
2406bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2407bdad233fSMatthew Knepley }
2408d763cef2SBarry Smith 
2409d763cef2SBarry Smith /*@
2410d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2411d763cef2SBarry Smith    of a timestepper.
2412d763cef2SBarry Smith 
2413d763cef2SBarry Smith    Collective on TS
2414d763cef2SBarry Smith 
2415d763cef2SBarry Smith    Input Parameter:
2416d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2417d763cef2SBarry Smith 
2418d763cef2SBarry Smith    Notes:
2419d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2420d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2421d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
2422d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2423d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
2424d763cef2SBarry Smith 
2425d763cef2SBarry Smith    Level: advanced
2426d763cef2SBarry Smith 
2427d763cef2SBarry Smith .keywords: TS, timestep, setup
2428d763cef2SBarry Smith 
2429d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
2430d763cef2SBarry Smith @*/
24317087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2432d763cef2SBarry Smith {
2433dfbe8321SBarry Smith   PetscErrorCode ierr;
24346c6b9e74SPeter Brune   DM             dm;
24356c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2436d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2437cd11d68dSLisandro Dalcin   TSIFunction    ifun;
24386c6b9e74SPeter Brune   TSIJacobian    ijac;
2439efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
24406c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
2441d763cef2SBarry Smith 
2442d763cef2SBarry Smith   PetscFunctionBegin;
24430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2444277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2445277b19d0SLisandro Dalcin 
24467adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2447cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2448cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2449d763cef2SBarry Smith   }
2450277b19d0SLisandro Dalcin 
2451277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2452277b19d0SLisandro Dalcin 
2453e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
2454e1244c69SJed Brown     Mat Amat,Pmat;
2455e1244c69SJed Brown     SNES snes;
2456e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2457e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2458e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2459e1244c69SJed Brown      * have displaced the RHS matrix */
2460e1244c69SJed Brown     if (Amat == ts->Arhs) {
2461e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2462e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2463e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2464e1244c69SJed Brown     }
2465e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2466e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2467e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2468e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2469e1244c69SJed Brown     }
2470e1244c69SJed Brown   }
2471277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2472000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2473277b19d0SLisandro Dalcin   }
2474277b19d0SLisandro Dalcin 
2475a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
24766c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
24776c6b9e74SPeter Brune    */
24786c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
24790298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
24806c6b9e74SPeter Brune   if (!func) {
24816c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
24826c6b9e74SPeter Brune   }
2483a6772fa2SLisandro 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.
24846c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
24856c6b9e74SPeter Brune    */
24860298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
24870298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2488efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
24890298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2490efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
24916c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
24926c6b9e74SPeter Brune   }
2493c0517034SDebojyoti Ghosh 
2494c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2495c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2496c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2497c0517034SDebojyoti Ghosh   }
2498c0517034SDebojyoti Ghosh 
2499277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2500277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2501277b19d0SLisandro Dalcin }
2502277b19d0SLisandro Dalcin 
2503f6a906c0SBarry Smith /*@
2504f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
2505f6a906c0SBarry Smith    of an adjoint solver
2506f6a906c0SBarry Smith 
2507f6a906c0SBarry Smith    Collective on TS
2508f6a906c0SBarry Smith 
2509f6a906c0SBarry Smith    Input Parameter:
2510f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
2511f6a906c0SBarry Smith 
2512f6a906c0SBarry Smith    Level: advanced
2513f6a906c0SBarry Smith 
2514f6a906c0SBarry Smith .keywords: TS, timestep, setup
2515f6a906c0SBarry Smith 
2516947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
2517f6a906c0SBarry Smith @*/
2518f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
2519f6a906c0SBarry Smith {
2520f6a906c0SBarry Smith   PetscErrorCode ierr;
2521f6a906c0SBarry Smith 
2522f6a906c0SBarry Smith   PetscFunctionBegin;
2523f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2524f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
2525dfb21088SHong Zhang   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
2526d8151eeaSBarry Smith 
2527947abb85SHong Zhang   if (ts->vec_costintegral) { /* if there is integral in the cost function*/
2528d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2529d8151eeaSBarry Smith     if (ts->vecs_sensip){
2530d8151eeaSBarry Smith       ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2531d8151eeaSBarry Smith     }
2532947abb85SHong Zhang   }
2533d8151eeaSBarry Smith 
253442f2b339SBarry Smith   if (ts->ops->adjointsetup) {
253542f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
2536f6a906c0SBarry Smith   }
2537f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
2538f6a906c0SBarry Smith   PetscFunctionReturn(0);
2539f6a906c0SBarry Smith }
2540f6a906c0SBarry Smith 
2541277b19d0SLisandro Dalcin /*@
2542277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2543277b19d0SLisandro Dalcin 
2544277b19d0SLisandro Dalcin    Collective on TS
2545277b19d0SLisandro Dalcin 
2546277b19d0SLisandro Dalcin    Input Parameter:
2547277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2548277b19d0SLisandro Dalcin 
2549277b19d0SLisandro Dalcin    Level: beginner
2550277b19d0SLisandro Dalcin 
2551277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2552277b19d0SLisandro Dalcin 
2553277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2554277b19d0SLisandro Dalcin @*/
2555277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2556277b19d0SLisandro Dalcin {
2557277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2558277b19d0SLisandro Dalcin 
2559277b19d0SLisandro Dalcin   PetscFunctionBegin;
2560277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2561b18ea86cSHong Zhang 
2562277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2563277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2564277b19d0SLisandro Dalcin   }
2565277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2566e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2567bbd56ea5SKarl Rupp 
25684e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
25694e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2570214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
25716bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2572efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2573e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2574e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
257538637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2576bbd56ea5SKarl Rupp 
2577947abb85SHong Zhang  if (ts->vec_costintegral) {
2578d8151eeaSBarry Smith     ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2579d8151eeaSBarry Smith     if (ts->vecs_drdp){
2580d8151eeaSBarry Smith       ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2581d8151eeaSBarry Smith     }
2582947abb85SHong Zhang   }
2583d8151eeaSBarry Smith   ts->vecs_sensi  = NULL;
2584d8151eeaSBarry Smith   ts->vecs_sensip = NULL;
2585ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
25862c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
258736eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2588277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2589d763cef2SBarry Smith   PetscFunctionReturn(0);
2590d763cef2SBarry Smith }
2591d763cef2SBarry Smith 
2592d8e5e3e6SSatish Balay /*@
2593d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2594d763cef2SBarry Smith    with TSCreate().
2595d763cef2SBarry Smith 
2596d763cef2SBarry Smith    Collective on TS
2597d763cef2SBarry Smith 
2598d763cef2SBarry Smith    Input Parameter:
2599d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2600d763cef2SBarry Smith 
2601d763cef2SBarry Smith    Level: beginner
2602d763cef2SBarry Smith 
2603d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2604d763cef2SBarry Smith 
2605d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2606d763cef2SBarry Smith @*/
26076bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2608d763cef2SBarry Smith {
26096849ba73SBarry Smith   PetscErrorCode ierr;
2610d763cef2SBarry Smith 
2611d763cef2SBarry Smith   PetscFunctionBegin;
26126bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
26136bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
26146bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2615d763cef2SBarry Smith 
26166bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2617277b19d0SLisandro Dalcin 
2618e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2619e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
26206bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
26216d4c513bSLisandro Dalcin 
2622bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2623bc952696SBarry Smith 
262484df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
26256427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
26266427ac75SLisandro Dalcin 
26276bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
26286bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
26296bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
26300dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
26316d4c513bSLisandro Dalcin 
2632a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2633d763cef2SBarry Smith   PetscFunctionReturn(0);
2634d763cef2SBarry Smith }
2635d763cef2SBarry Smith 
2636d8e5e3e6SSatish Balay /*@
2637d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2638d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2639d763cef2SBarry Smith 
2640d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2641d763cef2SBarry Smith 
2642d763cef2SBarry Smith    Input Parameter:
2643d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2644d763cef2SBarry Smith 
2645d763cef2SBarry Smith    Output Parameter:
2646d763cef2SBarry Smith .  snes - the nonlinear solver context
2647d763cef2SBarry Smith 
2648d763cef2SBarry Smith    Notes:
2649d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2650d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
265194b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2652d763cef2SBarry Smith 
2653d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
26540298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2655d763cef2SBarry Smith 
2656d763cef2SBarry Smith    Level: beginner
2657d763cef2SBarry Smith 
2658d763cef2SBarry Smith .keywords: timestep, get, SNES
2659d763cef2SBarry Smith @*/
26607087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2661d763cef2SBarry Smith {
2662d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2663d372ba47SLisandro Dalcin 
2664d763cef2SBarry Smith   PetscFunctionBegin;
26650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
26664482741eSBarry Smith   PetscValidPointer(snes,2);
2667d372ba47SLisandro Dalcin   if (!ts->snes) {
2668ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
26690298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
26703bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2671d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2672496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
26739e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
26749e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
26759e2a6581SJed Brown     }
2676d372ba47SLisandro Dalcin   }
2677d763cef2SBarry Smith   *snes = ts->snes;
2678d763cef2SBarry Smith   PetscFunctionReturn(0);
2679d763cef2SBarry Smith }
2680d763cef2SBarry Smith 
2681deb2cd25SJed Brown /*@
2682deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2683deb2cd25SJed Brown 
2684deb2cd25SJed Brown    Collective
2685deb2cd25SJed Brown 
2686deb2cd25SJed Brown    Input Parameter:
2687deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2688deb2cd25SJed Brown -  snes - the nonlinear solver context
2689deb2cd25SJed Brown 
2690deb2cd25SJed Brown    Notes:
2691deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2692deb2cd25SJed Brown 
2693deb2cd25SJed Brown    Level: developer
2694deb2cd25SJed Brown 
2695deb2cd25SJed Brown .keywords: timestep, set, SNES
2696deb2cd25SJed Brown @*/
2697deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2698deb2cd25SJed Brown {
2699deb2cd25SJed Brown   PetscErrorCode ierr;
2700d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2701deb2cd25SJed Brown 
2702deb2cd25SJed Brown   PetscFunctionBegin;
2703deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2704deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2705deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2706deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2707bbd56ea5SKarl Rupp 
2708deb2cd25SJed Brown   ts->snes = snes;
2709bbd56ea5SKarl Rupp 
27100298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27110298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2712740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
27130298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2714740132f1SEmil Constantinescu   }
2715deb2cd25SJed Brown   PetscFunctionReturn(0);
2716deb2cd25SJed Brown }
2717deb2cd25SJed Brown 
2718d8e5e3e6SSatish Balay /*@
271994b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2720d763cef2SBarry Smith    a TS (timestepper) context.
2721d763cef2SBarry Smith 
272294b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2723d763cef2SBarry Smith 
2724d763cef2SBarry Smith    Input Parameter:
2725d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2726d763cef2SBarry Smith 
2727d763cef2SBarry Smith    Output Parameter:
272894b7f48cSBarry Smith .  ksp - the nonlinear solver context
2729d763cef2SBarry Smith 
2730d763cef2SBarry Smith    Notes:
273194b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2732d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2733d763cef2SBarry Smith    KSP and PC contexts as well.
2734d763cef2SBarry Smith 
273594b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
27360298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2737d763cef2SBarry Smith 
2738d763cef2SBarry Smith    Level: beginner
2739d763cef2SBarry Smith 
274094b7f48cSBarry Smith .keywords: timestep, get, KSP
2741d763cef2SBarry Smith @*/
27427087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2743d763cef2SBarry Smith {
2744d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2745089b2837SJed Brown   SNES           snes;
2746d372ba47SLisandro Dalcin 
2747d763cef2SBarry Smith   PetscFunctionBegin;
27480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27494482741eSBarry Smith   PetscValidPointer(ksp,2);
275017186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2751e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2752089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2753089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2754d763cef2SBarry Smith   PetscFunctionReturn(0);
2755d763cef2SBarry Smith }
2756d763cef2SBarry Smith 
2757d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2758d763cef2SBarry Smith 
2759adb62b0dSMatthew Knepley /*@
2760adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2761adb62b0dSMatthew Knepley    maximum time for iteration.
2762adb62b0dSMatthew Knepley 
27633f9fe445SBarry Smith    Not Collective
2764adb62b0dSMatthew Knepley 
2765adb62b0dSMatthew Knepley    Input Parameters:
2766adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
27670298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
27680298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2769adb62b0dSMatthew Knepley 
2770adb62b0dSMatthew Knepley    Level: intermediate
2771adb62b0dSMatthew Knepley 
2772adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2773adb62b0dSMatthew Knepley @*/
27747087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2775adb62b0dSMatthew Knepley {
2776adb62b0dSMatthew Knepley   PetscFunctionBegin;
27770700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2778abc0a331SBarry Smith   if (maxsteps) {
27794482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2780adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2781adb62b0dSMatthew Knepley   }
2782abc0a331SBarry Smith   if (maxtime) {
27834482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2784adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2785adb62b0dSMatthew Knepley   }
2786adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2787adb62b0dSMatthew Knepley }
2788adb62b0dSMatthew Knepley 
2789d763cef2SBarry Smith /*@
2790d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2791d763cef2SBarry Smith    maximum time for iteration.
2792d763cef2SBarry Smith 
27933f9fe445SBarry Smith    Logically Collective on TS
2794d763cef2SBarry Smith 
2795d763cef2SBarry Smith    Input Parameters:
2796d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2797d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2798d763cef2SBarry Smith -  maxtime - final time to iterate to
2799d763cef2SBarry Smith 
2800d763cef2SBarry Smith    Options Database Keys:
2801d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
28023bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2803d763cef2SBarry Smith 
2804d763cef2SBarry Smith    Notes:
2805d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2806d763cef2SBarry Smith 
2807d763cef2SBarry Smith    Level: intermediate
2808d763cef2SBarry Smith 
2809d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2810a43b19c4SJed Brown 
2811a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2812d763cef2SBarry Smith @*/
28137087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2814d763cef2SBarry Smith {
2815d763cef2SBarry Smith   PetscFunctionBegin;
28160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2817c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2818c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
281939b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
282039b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2821d763cef2SBarry Smith   PetscFunctionReturn(0);
2822d763cef2SBarry Smith }
2823d763cef2SBarry Smith 
2824d763cef2SBarry Smith /*@
2825d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2826d763cef2SBarry Smith    for use by the TS routines.
2827d763cef2SBarry Smith 
28283f9fe445SBarry Smith    Logically Collective on TS and Vec
2829d763cef2SBarry Smith 
2830d763cef2SBarry Smith    Input Parameters:
2831d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
28320910c330SBarry Smith -  u - the solution vector
2833d763cef2SBarry Smith 
2834d763cef2SBarry Smith    Level: beginner
2835d763cef2SBarry Smith 
2836d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2837d763cef2SBarry Smith @*/
28380910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2839d763cef2SBarry Smith {
28408737fe31SLisandro Dalcin   PetscErrorCode ierr;
2841496e6a7aSJed Brown   DM             dm;
28428737fe31SLisandro Dalcin 
2843d763cef2SBarry Smith   PetscFunctionBegin;
28440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28450910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
28460910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
28476bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
28480910c330SBarry Smith   ts->vec_sol = u;
2849bbd56ea5SKarl Rupp 
2850496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
28510910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2852d763cef2SBarry Smith   PetscFunctionReturn(0);
2853d763cef2SBarry Smith }
2854d763cef2SBarry Smith 
285573b18844SBarry Smith /*@
285673b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
285773b18844SBarry Smith 
285873b18844SBarry Smith    Logically Collective on TS
285973b18844SBarry Smith 
286073b18844SBarry Smith    Input Parameters:
286173b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
286273b18844SBarry Smith .  steps - number of steps to use
286373b18844SBarry Smith 
286473b18844SBarry Smith    Level: intermediate
286573b18844SBarry Smith 
286673b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
286773b18844SBarry Smith           so as to integrate back to less than the original timestep
286873b18844SBarry Smith 
286973b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
287073b18844SBarry Smith 
287173b18844SBarry Smith .seealso: TSSetExactFinalTime()
287273b18844SBarry Smith @*/
287373b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
287473b18844SBarry Smith {
287573b18844SBarry Smith   PetscFunctionBegin;
287673b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
287773b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
287873b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
287973b18844SBarry 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");
288073b18844SBarry Smith   ts->adjoint_max_steps = steps;
288173b18844SBarry Smith   PetscFunctionReturn(0);
288273b18844SBarry Smith }
288373b18844SBarry Smith 
2884c235aa8dSHong Zhang /*@
2885dfb21088SHong 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
28860cf82b59SBarry Smith       for use by the TSAdjoint routines.
2887c235aa8dSHong Zhang 
2888c235aa8dSHong Zhang    Logically Collective on TS and Vec
2889c235aa8dSHong Zhang 
2890c235aa8dSHong Zhang    Input Parameters:
2891c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2892abc2977eSBarry 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
2893abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
2894c235aa8dSHong Zhang 
2895c235aa8dSHong Zhang    Level: beginner
2896c235aa8dSHong Zhang 
2897abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
28980cf82b59SBarry Smith 
2899c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2900c235aa8dSHong Zhang @*/
2901dfb21088SHong Zhang PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
2902c235aa8dSHong Zhang {
2903c235aa8dSHong Zhang   PetscFunctionBegin;
2904c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2905abc2977eSBarry Smith   PetscValidPointer(lambda,2);
2906abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
2907abc2977eSBarry Smith   ts->vecs_sensip = mu;
2908dfb21088SHong 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");
2909abc2977eSBarry Smith   ts->numcost  = numcost;
2910ad8e2604SHong Zhang   PetscFunctionReturn(0);
2911ad8e2604SHong Zhang }
2912ad8e2604SHong Zhang 
2913ad8e2604SHong Zhang /*@C
29140cf82b59SBarry 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.
2915ad8e2604SHong Zhang 
2916ad8e2604SHong Zhang   Logically Collective on TS
2917ad8e2604SHong Zhang 
2918ad8e2604SHong Zhang   Input Parameters:
2919ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2920ad8e2604SHong Zhang - func - The function
2921ad8e2604SHong Zhang 
2922ad8e2604SHong Zhang   Calling sequence of func:
29230cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
292405755b9cSHong Zhang +   t - current timestep
29250cf82b59SBarry Smith .   y - input vector (current ODE solution)
292605755b9cSHong Zhang .   A - output matrix
292705755b9cSHong Zhang -   ctx - [optional] user-defined function context
2928ad8e2604SHong Zhang 
2929ad8e2604SHong Zhang   Level: intermediate
2930ad8e2604SHong Zhang 
29310cf82b59SBarry 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
29320cf82b59SBarry Smith 
2933ad8e2604SHong Zhang .keywords: TS, sensitivity
2934ad8e2604SHong Zhang .seealso:
2935ad8e2604SHong Zhang @*/
29365bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2937ad8e2604SHong Zhang {
2938ad8e2604SHong Zhang   PetscErrorCode ierr;
2939ad8e2604SHong Zhang 
2940ad8e2604SHong Zhang   PetscFunctionBegin;
2941ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2942ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
2943ad8e2604SHong Zhang 
2944ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
2945ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
2946ad8e2604SHong Zhang   if(Amat) {
2947ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
2948ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2949ad8e2604SHong Zhang     ts->Jacp = Amat;
2950ad8e2604SHong Zhang   }
2951ad8e2604SHong Zhang   PetscFunctionReturn(0);
2952ad8e2604SHong Zhang }
2953ad8e2604SHong Zhang 
29540cf82b59SBarry Smith /*@C
29555bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
2956ad8e2604SHong Zhang 
2957ad8e2604SHong Zhang   Collective on TS
2958ad8e2604SHong Zhang 
2959ad8e2604SHong Zhang   Input Parameters:
2960ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
2961ad8e2604SHong Zhang 
2962ad8e2604SHong Zhang   Level: developer
2963ad8e2604SHong Zhang 
2964ad8e2604SHong Zhang .keywords: TS, sensitivity
29655bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
2966ad8e2604SHong Zhang @*/
29675bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
2968ad8e2604SHong Zhang {
2969ad8e2604SHong Zhang   PetscErrorCode ierr;
2970ad8e2604SHong Zhang 
2971ad8e2604SHong Zhang   PetscFunctionBegin;
2972ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2973ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
2974ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
2975ad8e2604SHong Zhang 
2976ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
2977ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
2978ad8e2604SHong Zhang   PetscStackPop;
2979c235aa8dSHong Zhang   PetscFunctionReturn(0);
2980c235aa8dSHong Zhang }
2981c235aa8dSHong Zhang 
29826fd0887fSHong Zhang /*@C
2983dfb21088SHong Zhang     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
29846fd0887fSHong Zhang 
29856fd0887fSHong Zhang     Logically Collective on TS
29866fd0887fSHong Zhang 
29876fd0887fSHong Zhang     Input Parameters:
29886fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
2989abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
29900cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
2991b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
2992b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
2993feaa1ddbSSatish Balay .   fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
2994b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
29956fd0887fSHong Zhang 
29960cf82b59SBarry Smith     Calling sequence of rf:
29970cf82b59SBarry Smith $     rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx);
29986fd0887fSHong Zhang 
29996fd0887fSHong Zhang +   t - current timestep
30000cf82b59SBarry Smith .   y - input vector
30010cf82b59SBarry Smith .   f - function result; one vector entry for each cost function
30026fd0887fSHong Zhang -   ctx - [optional] user-defined function context
30036fd0887fSHong Zhang 
3004b612ec54SBarry Smith    Calling sequence of drdyf:
30050cf82b59SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
3006b612ec54SBarry Smith 
3007b612ec54SBarry Smith    Calling sequence of drdpf:
30080cf82b59SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
3009b612ec54SBarry Smith 
3010b612ec54SBarry Smith     Level: intermediate
30116fd0887fSHong Zhang 
3012abc2977eSBarry Smith     Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions
30130cf82b59SBarry Smith 
30146fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
30156fd0887fSHong Zhang 
3016dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
30176fd0887fSHong Zhang @*/
3018dfb21088SHong Zhang PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
3019d8151eeaSBarry Smith                                                           PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
3020b1cb36f3SHong Zhang                                                           PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),
3021b1cb36f3SHong Zhang                                                           PetscBool fwd,void *ctx)
30226fd0887fSHong Zhang {
30236fd0887fSHong Zhang   PetscErrorCode ierr;
30246fd0887fSHong Zhang 
30256fd0887fSHong Zhang   PetscFunctionBegin;
30266fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3027dfb21088SHong 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()");
3028c72ed514SHong Zhang   if (!ts->numcost) ts->numcost=numcost;
30296fd0887fSHong Zhang 
3030b1cb36f3SHong Zhang   ts->costintegralfwd  = fwd; /* Evaluate the cost integral in forward run if fwd is true */
3031abc2977eSBarry Smith   ierr                 = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
30322c39e106SBarry Smith   ierr                 = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
30330cf82b59SBarry Smith   ts->costintegrand    = rf;
303405755b9cSHong Zhang   ts->costintegrandctx = ctx;
3035b612ec54SBarry Smith   ts->drdyfunction     = drdyf;
3036b612ec54SBarry Smith   ts->drdpfunction     = drdpf;
30376fd0887fSHong Zhang   PetscFunctionReturn(0);
30386fd0887fSHong Zhang }
30396fd0887fSHong Zhang 
304036eaed60SHong Zhang /*@
3041dfb21088SHong Zhang    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
304236eaed60SHong Zhang    It is valid to call the routine after a backward run.
304336eaed60SHong Zhang 
304436eaed60SHong Zhang    Not Collective
304536eaed60SHong Zhang 
304636eaed60SHong Zhang    Input Parameter:
304736eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
304836eaed60SHong Zhang 
304936eaed60SHong Zhang    Output Parameter:
30502c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
305136eaed60SHong Zhang 
305236eaed60SHong Zhang    Level: intermediate
305336eaed60SHong Zhang 
3054dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
305536eaed60SHong Zhang 
305636eaed60SHong Zhang .keywords: TS, sensitivity analysis
305736eaed60SHong Zhang @*/
3058dfb21088SHong Zhang PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
305936eaed60SHong Zhang {
306036eaed60SHong Zhang   PetscFunctionBegin;
306136eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
306236eaed60SHong Zhang   PetscValidPointer(v,2);
30632c39e106SBarry Smith   *v = ts->vec_costintegral;
306436eaed60SHong Zhang   PetscFunctionReturn(0);
306536eaed60SHong Zhang }
306636eaed60SHong Zhang 
30676fd0887fSHong Zhang /*@
30682c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
30696fd0887fSHong Zhang 
30706fd0887fSHong Zhang    Input Parameters:
30716fd0887fSHong Zhang +  ts - the TS context
30726fd0887fSHong Zhang .  t - current time
30730cf82b59SBarry Smith -  y - state vector, i.e. current solution
30746fd0887fSHong Zhang 
30756fd0887fSHong Zhang    Output Parameter:
3076abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
30776fd0887fSHong Zhang 
30786fd0887fSHong Zhang    Note:
30796fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
30806fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
30816fd0887fSHong Zhang 
30826fd0887fSHong Zhang    Level: developer
30836fd0887fSHong Zhang 
30846fd0887fSHong Zhang .keywords: TS, compute
30856fd0887fSHong Zhang 
3086dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
30876fd0887fSHong Zhang @*/
30880cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
30896fd0887fSHong Zhang {
30906fd0887fSHong Zhang   PetscErrorCode ierr;
30916fd0887fSHong Zhang 
30926fd0887fSHong Zhang   PetscFunctionBegin;
30936fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30940cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
30956fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
30966fd0887fSHong Zhang 
30970cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
3098e4680132SHong Zhang   if (ts->costintegrand) {
3099e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
31000cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
31016fd0887fSHong Zhang     PetscStackPop;
31026fd0887fSHong Zhang   } else {
31036fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
31046fd0887fSHong Zhang   }
31056fd0887fSHong Zhang 
31060cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
31076fd0887fSHong Zhang   PetscFunctionReturn(0);
31086fd0887fSHong Zhang }
31096fd0887fSHong Zhang 
311005755b9cSHong Zhang /*@
3111d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
311205755b9cSHong Zhang 
311305755b9cSHong Zhang   Collective on TS
311405755b9cSHong Zhang 
311505755b9cSHong Zhang   Input Parameters:
311605755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
311705755b9cSHong Zhang 
311805755b9cSHong Zhang   Notes:
3119d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
312005755b9cSHong Zhang   so most users would not generally call this routine themselves.
312105755b9cSHong Zhang 
312205755b9cSHong Zhang   Level: developer
312305755b9cSHong Zhang 
312405755b9cSHong Zhang .keywords: TS, sensitivity
3125d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
312605755b9cSHong Zhang @*/
31270cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
312805755b9cSHong Zhang {
312905755b9cSHong Zhang   PetscErrorCode ierr;
313005755b9cSHong Zhang 
313105755b9cSHong Zhang   PetscFunctionBegin;
313205755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31330cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
313405755b9cSHong Zhang 
313505755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
31360cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
313705755b9cSHong Zhang   PetscStackPop;
313805755b9cSHong Zhang   PetscFunctionReturn(0);
313905755b9cSHong Zhang }
314005755b9cSHong Zhang 
314105755b9cSHong Zhang /*@
3142d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
314305755b9cSHong Zhang 
314405755b9cSHong Zhang   Collective on TS
314505755b9cSHong Zhang 
314605755b9cSHong Zhang   Input Parameters:
314705755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
314805755b9cSHong Zhang 
314905755b9cSHong Zhang   Notes:
315005755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
315105755b9cSHong Zhang   so most users would not generally call this routine themselves.
315205755b9cSHong Zhang 
315305755b9cSHong Zhang   Level: developer
315405755b9cSHong Zhang 
315505755b9cSHong Zhang .keywords: TS, sensitivity
3156d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
315705755b9cSHong Zhang @*/
31580cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
315905755b9cSHong Zhang {
316005755b9cSHong Zhang   PetscErrorCode ierr;
316105755b9cSHong Zhang 
316205755b9cSHong Zhang   PetscFunctionBegin;
316305755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31640cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
316505755b9cSHong Zhang 
316605755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
31670cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
316805755b9cSHong Zhang   PetscStackPop;
316905755b9cSHong Zhang   PetscFunctionReturn(0);
317005755b9cSHong Zhang }
317105755b9cSHong Zhang 
3172ac226902SBarry Smith /*@C
3173000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
31743f2090d5SJed Brown   called once at the beginning of each time step.
3175000e7ae3SMatthew Knepley 
31763f9fe445SBarry Smith   Logically Collective on TS
3177000e7ae3SMatthew Knepley 
3178000e7ae3SMatthew Knepley   Input Parameters:
3179000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3180000e7ae3SMatthew Knepley - func - The function
3181000e7ae3SMatthew Knepley 
3182000e7ae3SMatthew Knepley   Calling sequence of func:
3183000e7ae3SMatthew Knepley . func (TS ts);
3184000e7ae3SMatthew Knepley 
3185000e7ae3SMatthew Knepley   Level: intermediate
3186000e7ae3SMatthew Knepley 
3187b8123daeSJed Brown   Note:
3188b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
3189b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
3190b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
3191b8123daeSJed Brown 
3192000e7ae3SMatthew Knepley .keywords: TS, timestep
31939be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
3194000e7ae3SMatthew Knepley @*/
31957087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3196000e7ae3SMatthew Knepley {
3197000e7ae3SMatthew Knepley   PetscFunctionBegin;
31980700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3199ae60f76fSBarry Smith   ts->prestep = func;
3200000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3201000e7ae3SMatthew Knepley }
3202000e7ae3SMatthew Knepley 
320309ee8438SJed Brown /*@
32043f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
32053f2090d5SJed Brown 
32063f2090d5SJed Brown   Collective on TS
32073f2090d5SJed Brown 
32083f2090d5SJed Brown   Input Parameters:
32093f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32103f2090d5SJed Brown 
32113f2090d5SJed Brown   Notes:
32123f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32133f2090d5SJed Brown   so most users would not generally call this routine themselves.
32143f2090d5SJed Brown 
32153f2090d5SJed Brown   Level: developer
32163f2090d5SJed Brown 
32173f2090d5SJed Brown .keywords: TS, timestep
32189be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32193f2090d5SJed Brown @*/
32207087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32213f2090d5SJed Brown {
32223f2090d5SJed Brown   PetscErrorCode ierr;
32233f2090d5SJed Brown 
32243f2090d5SJed Brown   PetscFunctionBegin;
32250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3226ae60f76fSBarry Smith   if (ts->prestep) {
3227ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
3228312ce896SJed Brown   }
32293f2090d5SJed Brown   PetscFunctionReturn(0);
32303f2090d5SJed Brown }
32313f2090d5SJed Brown 
3232b8123daeSJed Brown /*@C
3233b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3234b8123daeSJed Brown   called once at the beginning of each stage.
3235b8123daeSJed Brown 
3236b8123daeSJed Brown   Logically Collective on TS
3237b8123daeSJed Brown 
3238b8123daeSJed Brown   Input Parameters:
3239b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3240b8123daeSJed Brown - func - The function
3241b8123daeSJed Brown 
3242b8123daeSJed Brown   Calling sequence of func:
3243b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3244b8123daeSJed Brown 
3245b8123daeSJed Brown   Level: intermediate
3246b8123daeSJed Brown 
3247b8123daeSJed Brown   Note:
3248b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
3249b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
3250b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3251b8123daeSJed Brown 
3252b8123daeSJed Brown .keywords: TS, timestep
32539be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3254b8123daeSJed Brown @*/
3255b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3256b8123daeSJed Brown {
3257b8123daeSJed Brown   PetscFunctionBegin;
3258b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3259ae60f76fSBarry Smith   ts->prestage = func;
3260b8123daeSJed Brown   PetscFunctionReturn(0);
3261b8123daeSJed Brown }
3262b8123daeSJed Brown 
32639be3e283SDebojyoti Ghosh /*@C
32649be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
32659be3e283SDebojyoti Ghosh   called once at the end of each stage.
32669be3e283SDebojyoti Ghosh 
32679be3e283SDebojyoti Ghosh   Logically Collective on TS
32689be3e283SDebojyoti Ghosh 
32699be3e283SDebojyoti Ghosh   Input Parameters:
32709be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
32719be3e283SDebojyoti Ghosh - func - The function
32729be3e283SDebojyoti Ghosh 
32739be3e283SDebojyoti Ghosh   Calling sequence of func:
32749be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
32759be3e283SDebojyoti Ghosh 
32769be3e283SDebojyoti Ghosh   Level: intermediate
32779be3e283SDebojyoti Ghosh 
32789be3e283SDebojyoti Ghosh   Note:
32799be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
32809be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
32819be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
32829be3e283SDebojyoti Ghosh 
32839be3e283SDebojyoti Ghosh .keywords: TS, timestep
32849be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
32859be3e283SDebojyoti Ghosh @*/
32869be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
32879be3e283SDebojyoti Ghosh {
32889be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32899be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
32909be3e283SDebojyoti Ghosh   ts->poststage = func;
32919be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32929be3e283SDebojyoti Ghosh }
32939be3e283SDebojyoti Ghosh 
3294c688d042SShri Abhyankar /*@C
3295c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3296c688d042SShri Abhyankar   called once at the end of each step evaluation.
3297c688d042SShri Abhyankar 
3298c688d042SShri Abhyankar   Logically Collective on TS
3299c688d042SShri Abhyankar 
3300c688d042SShri Abhyankar   Input Parameters:
3301c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3302c688d042SShri Abhyankar - func - The function
3303c688d042SShri Abhyankar 
3304c688d042SShri Abhyankar   Calling sequence of func:
3305c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3306c688d042SShri Abhyankar 
3307c688d042SShri Abhyankar   Level: intermediate
3308c688d042SShri Abhyankar 
3309c688d042SShri Abhyankar   Note:
33101785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33111785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3312e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3313e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3314e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3315c688d042SShri Abhyankar 
3316c688d042SShri Abhyankar .keywords: TS, timestep
3317c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3318c688d042SShri Abhyankar @*/
3319c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3320c688d042SShri Abhyankar {
3321c688d042SShri Abhyankar   PetscFunctionBegin;
3322c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3323c688d042SShri Abhyankar   ts->postevaluate = func;
3324c688d042SShri Abhyankar   PetscFunctionReturn(0);
3325c688d042SShri Abhyankar }
3326c688d042SShri Abhyankar 
3327b8123daeSJed Brown /*@
3328b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3329b8123daeSJed Brown 
3330b8123daeSJed Brown   Collective on TS
3331b8123daeSJed Brown 
3332b8123daeSJed Brown   Input Parameters:
3333b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33349be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3335b8123daeSJed Brown 
3336b8123daeSJed Brown   Notes:
3337b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3338b8123daeSJed Brown   most users would not generally call this routine themselves.
3339b8123daeSJed Brown 
3340b8123daeSJed Brown   Level: developer
3341b8123daeSJed Brown 
3342b8123daeSJed Brown .keywords: TS, timestep
33439be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3344b8123daeSJed Brown @*/
3345b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3346b8123daeSJed Brown {
3347b8123daeSJed Brown   PetscErrorCode ierr;
3348b8123daeSJed Brown 
3349b8123daeSJed Brown   PetscFunctionBegin;
3350b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3351ae60f76fSBarry Smith   if (ts->prestage) {
3352ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3353b8123daeSJed Brown   }
3354b8123daeSJed Brown   PetscFunctionReturn(0);
3355b8123daeSJed Brown }
3356b8123daeSJed Brown 
33579be3e283SDebojyoti Ghosh /*@
33589be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
33599be3e283SDebojyoti Ghosh 
33609be3e283SDebojyoti Ghosh   Collective on TS
33619be3e283SDebojyoti Ghosh 
33629be3e283SDebojyoti Ghosh   Input Parameters:
33639be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
33649be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
33659be3e283SDebojyoti Ghosh   stageindex  - Stage number
33669be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
33679be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
33689be3e283SDebojyoti Ghosh 
33699be3e283SDebojyoti Ghosh   Notes:
33709be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
33719be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
33729be3e283SDebojyoti Ghosh 
33739be3e283SDebojyoti Ghosh   Level: developer
33749be3e283SDebojyoti Ghosh 
33759be3e283SDebojyoti Ghosh .keywords: TS, timestep
33769be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
33779be3e283SDebojyoti Ghosh @*/
33789be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
33799be3e283SDebojyoti Ghosh {
33809be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
33819be3e283SDebojyoti Ghosh 
33829be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33839be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33844beae5d8SLisandro Dalcin   if (ts->poststage) {
33859be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
33869be3e283SDebojyoti Ghosh   }
33879be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33889be3e283SDebojyoti Ghosh }
33899be3e283SDebojyoti Ghosh 
3390c688d042SShri Abhyankar /*@
3391c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3392c688d042SShri Abhyankar 
3393c688d042SShri Abhyankar   Collective on TS
3394c688d042SShri Abhyankar 
3395c688d042SShri Abhyankar   Input Parameters:
3396c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3397c688d042SShri Abhyankar 
3398c688d042SShri Abhyankar   Notes:
3399c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3400c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3401c688d042SShri Abhyankar 
3402c688d042SShri Abhyankar   Level: developer
3403c688d042SShri Abhyankar 
3404c688d042SShri Abhyankar .keywords: TS, timestep
3405c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3406c688d042SShri Abhyankar @*/
3407c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3408c688d042SShri Abhyankar {
3409c688d042SShri Abhyankar   PetscErrorCode ierr;
3410c688d042SShri Abhyankar 
3411c688d042SShri Abhyankar   PetscFunctionBegin;
3412c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3413c688d042SShri Abhyankar   if (ts->postevaluate) {
3414c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3415c688d042SShri Abhyankar   }
3416c688d042SShri Abhyankar   PetscFunctionReturn(0);
3417c688d042SShri Abhyankar }
3418c688d042SShri Abhyankar 
3419ac226902SBarry Smith /*@C
3420000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34213f2090d5SJed Brown   called once at the end of each time step.
3422000e7ae3SMatthew Knepley 
34233f9fe445SBarry Smith   Logically Collective on TS
3424000e7ae3SMatthew Knepley 
3425000e7ae3SMatthew Knepley   Input Parameters:
3426000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3427000e7ae3SMatthew Knepley - func - The function
3428000e7ae3SMatthew Knepley 
3429000e7ae3SMatthew Knepley   Calling sequence of func:
3430b8123daeSJed Brown $ func (TS ts);
3431000e7ae3SMatthew Knepley 
34321785ff2aSShri Abhyankar   Notes:
34331785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34341785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34351785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34361785ff2aSShri Abhyankar 
3437000e7ae3SMatthew Knepley   Level: intermediate
3438000e7ae3SMatthew Knepley 
3439000e7ae3SMatthew Knepley .keywords: TS, timestep
34401785ff2aSShri Abhyankar .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
3441000e7ae3SMatthew Knepley @*/
34427087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3443000e7ae3SMatthew Knepley {
3444000e7ae3SMatthew Knepley   PetscFunctionBegin;
34450700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3446ae60f76fSBarry Smith   ts->poststep = func;
3447000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3448000e7ae3SMatthew Knepley }
3449000e7ae3SMatthew Knepley 
345009ee8438SJed Brown /*@
34513f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34523f2090d5SJed Brown 
34533f2090d5SJed Brown   Collective on TS
34543f2090d5SJed Brown 
34553f2090d5SJed Brown   Input Parameters:
34563f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
34573f2090d5SJed Brown 
34583f2090d5SJed Brown   Notes:
34593f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
34603f2090d5SJed Brown   so most users would not generally call this routine themselves.
34613f2090d5SJed Brown 
34623f2090d5SJed Brown   Level: developer
34633f2090d5SJed Brown 
34643f2090d5SJed Brown .keywords: TS, timestep
34653f2090d5SJed Brown @*/
34667087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
34673f2090d5SJed Brown {
34683f2090d5SJed Brown   PetscErrorCode ierr;
34693f2090d5SJed Brown 
34703f2090d5SJed Brown   PetscFunctionBegin;
34710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3472ae60f76fSBarry Smith   if (ts->poststep) {
3473ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
347472ac3e02SJed Brown   }
34753f2090d5SJed Brown   PetscFunctionReturn(0);
34763f2090d5SJed Brown }
34773f2090d5SJed Brown 
3478d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3479d763cef2SBarry Smith 
3480d763cef2SBarry Smith /*@C
3481a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3482d763cef2SBarry Smith    timestep to display the iteration's  progress.
3483d763cef2SBarry Smith 
34843f9fe445SBarry Smith    Logically Collective on TS
3485d763cef2SBarry Smith 
3486d763cef2SBarry Smith    Input Parameters:
3487d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3488e213d8f1SJed Brown .  monitor - monitoring routine
3489329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
34900298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3491b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
34920298fd71SBarry Smith           (may be NULL)
3493d763cef2SBarry Smith 
3494e213d8f1SJed Brown    Calling sequence of monitor:
34950910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3496d763cef2SBarry Smith 
3497d763cef2SBarry Smith +    ts - the TS context
349863e21af5SBarry 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)
34991f06c33eSBarry Smith .    time - current time
35000910c330SBarry Smith .    u - current iterate
3501d763cef2SBarry Smith -    mctx - [optional] monitoring context
3502d763cef2SBarry Smith 
3503d763cef2SBarry Smith    Notes:
3504d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3505d763cef2SBarry Smith    already has been loaded.
3506d763cef2SBarry Smith 
3507025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
3508025f1a04SBarry Smith 
3509d763cef2SBarry Smith    Level: intermediate
3510d763cef2SBarry Smith 
3511d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3512d763cef2SBarry Smith 
3513a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3514d763cef2SBarry Smith @*/
3515c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3516d763cef2SBarry Smith {
351778064530SBarry Smith   PetscErrorCode ierr;
351878064530SBarry Smith   PetscInt       i;
351978064530SBarry Smith   PetscBool      identical;
352078064530SBarry Smith 
3521d763cef2SBarry Smith   PetscFunctionBegin;
35220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
352378064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
352478064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
352578064530SBarry Smith     if (identical) PetscFunctionReturn(0);
352678064530SBarry Smith   }
352717186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3528d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
35298704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3530d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3531d763cef2SBarry Smith   PetscFunctionReturn(0);
3532d763cef2SBarry Smith }
3533d763cef2SBarry Smith 
3534d763cef2SBarry Smith /*@C
3535a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3536d763cef2SBarry Smith 
35373f9fe445SBarry Smith    Logically Collective on TS
3538d763cef2SBarry Smith 
3539d763cef2SBarry Smith    Input Parameters:
3540d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3541d763cef2SBarry Smith 
3542d763cef2SBarry Smith    Notes:
3543d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3544d763cef2SBarry Smith 
3545d763cef2SBarry Smith    Level: intermediate
3546d763cef2SBarry Smith 
3547d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3548d763cef2SBarry Smith 
3549a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3550d763cef2SBarry Smith @*/
35517087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3552d763cef2SBarry Smith {
3553d952e501SBarry Smith   PetscErrorCode ierr;
3554d952e501SBarry Smith   PetscInt       i;
3555d952e501SBarry Smith 
3556d763cef2SBarry Smith   PetscFunctionBegin;
35570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3558d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
35598704b422SBarry Smith     if (ts->monitordestroy[i]) {
35608704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3561d952e501SBarry Smith     }
3562d952e501SBarry Smith   }
3563d763cef2SBarry Smith   ts->numbermonitors = 0;
3564d763cef2SBarry Smith   PetscFunctionReturn(0);
3565d763cef2SBarry Smith }
3566d763cef2SBarry Smith 
3567721cd6eeSBarry Smith /*@C
3568721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
35695516499fSSatish Balay 
35705516499fSSatish Balay    Level: intermediate
357141251cbbSSatish Balay 
35725516499fSSatish Balay .keywords: TS, set, monitor
35735516499fSSatish Balay 
357463e21af5SBarry Smith .seealso:  TSMonitorSet()
357541251cbbSSatish Balay @*/
3576721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3577d763cef2SBarry Smith {
3578dfbe8321SBarry Smith   PetscErrorCode ierr;
3579721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
358041aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3581d132466eSBarry Smith 
3582d763cef2SBarry Smith   PetscFunctionBegin;
35834d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
358441aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
358541aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3586721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
358741aca3d6SBarry Smith   if (iascii) {
3588649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
358963e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
359063e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
359163e21af5SBarry Smith     } else {
35928392e04aSShri 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);
359363e21af5SBarry Smith     }
3594649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
359541aca3d6SBarry Smith   } else if (ibinary) {
359641aca3d6SBarry Smith     PetscMPIInt rank;
359741aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
359841aca3d6SBarry Smith     if (!rank) {
3599450a797fSBarry Smith       PetscBool skipHeader;
3600450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3601450a797fSBarry Smith 
3602450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3603450a797fSBarry Smith       if (!skipHeader) {
3604450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3605450a797fSBarry Smith        }
360641aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
360741aca3d6SBarry Smith     } else {
360841aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
360941aca3d6SBarry Smith     }
361041aca3d6SBarry Smith   }
3611721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3612d763cef2SBarry Smith   PetscFunctionReturn(0);
3613d763cef2SBarry Smith }
3614d763cef2SBarry Smith 
36159110b2e7SHong Zhang /*@C
36169110b2e7SHong Zhang    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
36179110b2e7SHong Zhang    timestep to display the iteration's  progress.
36189110b2e7SHong Zhang 
36199110b2e7SHong Zhang    Logically Collective on TS
36209110b2e7SHong Zhang 
36219110b2e7SHong Zhang    Input Parameters:
36229110b2e7SHong Zhang +  ts - the TS context obtained from TSCreate()
36239110b2e7SHong Zhang .  adjointmonitor - monitoring routine
36249110b2e7SHong Zhang .  adjointmctx - [optional] user-defined context for private data for the
36259110b2e7SHong Zhang              monitor routine (use NULL if no context is desired)
36269110b2e7SHong Zhang -  adjointmonitordestroy - [optional] routine that frees monitor context
36279110b2e7SHong Zhang           (may be NULL)
36289110b2e7SHong Zhang 
36299110b2e7SHong Zhang    Calling sequence of monitor:
36309110b2e7SHong Zhang $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)
36319110b2e7SHong Zhang 
36329110b2e7SHong Zhang +    ts - the TS context
36339110b2e7SHong 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
36349110b2e7SHong Zhang                                been interpolated to)
36359110b2e7SHong Zhang .    time - current time
36369110b2e7SHong Zhang .    u - current iterate
36379110b2e7SHong Zhang .    numcost - number of cost functionos
36389110b2e7SHong Zhang .    lambda - sensitivities to initial conditions
36399110b2e7SHong Zhang .    mu - sensitivities to parameters
36409110b2e7SHong Zhang -    adjointmctx - [optional] adjoint monitoring context
36419110b2e7SHong Zhang 
36429110b2e7SHong Zhang    Notes:
36439110b2e7SHong Zhang    This routine adds an additional monitor to the list of monitors that
36449110b2e7SHong Zhang    already has been loaded.
36459110b2e7SHong Zhang 
36469110b2e7SHong Zhang    Fortran notes: Only a single monitor function can be set for each TS object
36479110b2e7SHong Zhang 
36489110b2e7SHong Zhang    Level: intermediate
36499110b2e7SHong Zhang 
36509110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
36519110b2e7SHong Zhang 
36529110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel()
36539110b2e7SHong Zhang @*/
36549110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
36559110b2e7SHong Zhang {
365678064530SBarry Smith   PetscErrorCode ierr;
365778064530SBarry Smith   PetscInt       i;
365878064530SBarry Smith   PetscBool      identical;
365978064530SBarry Smith 
36609110b2e7SHong Zhang   PetscFunctionBegin;
36619110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
366278064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
366378064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr);
366478064530SBarry Smith     if (identical) PetscFunctionReturn(0);
366578064530SBarry Smith   }
36669110b2e7SHong Zhang   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
36679110b2e7SHong Zhang   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
36689110b2e7SHong Zhang   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
36699110b2e7SHong Zhang   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
36709110b2e7SHong Zhang   PetscFunctionReturn(0);
36719110b2e7SHong Zhang }
36729110b2e7SHong Zhang 
36739110b2e7SHong Zhang /*@C
36749110b2e7SHong Zhang    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.
36759110b2e7SHong Zhang 
36769110b2e7SHong Zhang    Logically Collective on TS
36779110b2e7SHong Zhang 
36789110b2e7SHong Zhang    Input Parameters:
36799110b2e7SHong Zhang .  ts - the TS context obtained from TSCreate()
36809110b2e7SHong Zhang 
36819110b2e7SHong Zhang    Notes:
36829110b2e7SHong Zhang    There is no way to remove a single, specific monitor.
36839110b2e7SHong Zhang 
36849110b2e7SHong Zhang    Level: intermediate
36859110b2e7SHong Zhang 
36869110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
36879110b2e7SHong Zhang 
36889110b2e7SHong Zhang .seealso: TSAdjointMonitorSet()
36899110b2e7SHong Zhang @*/
36909110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorCancel(TS ts)
36919110b2e7SHong Zhang {
36929110b2e7SHong Zhang   PetscErrorCode ierr;
36939110b2e7SHong Zhang   PetscInt       i;
36949110b2e7SHong Zhang 
36959110b2e7SHong Zhang   PetscFunctionBegin;
36969110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36979110b2e7SHong Zhang   for (i=0; i<ts->numberadjointmonitors; i++) {
36989110b2e7SHong Zhang     if (ts->adjointmonitordestroy[i]) {
36999110b2e7SHong Zhang       ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
37009110b2e7SHong Zhang     }
37019110b2e7SHong Zhang   }
37029110b2e7SHong Zhang   ts->numberadjointmonitors = 0;
37039110b2e7SHong Zhang   PetscFunctionReturn(0);
37049110b2e7SHong Zhang }
37059110b2e7SHong Zhang 
3706721cd6eeSBarry Smith /*@C
3707721cd6eeSBarry Smith    TSAdjointMonitorDefault - the default monitor of adjoint computations
3708110eb670SHong Zhang 
3709110eb670SHong Zhang    Level: intermediate
3710110eb670SHong Zhang 
3711110eb670SHong Zhang .keywords: TS, set, monitor
3712110eb670SHong Zhang 
3713110eb670SHong Zhang .seealso: TSAdjointMonitorSet()
3714110eb670SHong Zhang @*/
3715721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
3716110eb670SHong Zhang {
3717110eb670SHong Zhang   PetscErrorCode ierr;
3718721cd6eeSBarry Smith   PetscViewer    viewer = vf->viewer;
3719110eb670SHong Zhang 
3720110eb670SHong Zhang   PetscFunctionBegin;
3721110eb670SHong Zhang   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3722721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3723110eb670SHong Zhang   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3724110eb670SHong 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);
3725110eb670SHong Zhang   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3726721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3727110eb670SHong Zhang   PetscFunctionReturn(0);
3728110eb670SHong Zhang }
3729110eb670SHong Zhang 
3730cd652676SJed Brown /*@
3731cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3732cd652676SJed Brown 
3733cd652676SJed Brown    Collective on TS
3734cd652676SJed Brown 
3735cd652676SJed Brown    Input Argument:
3736cd652676SJed Brown +  ts - time stepping context
3737cd652676SJed Brown -  t - time to interpolate to
3738cd652676SJed Brown 
3739cd652676SJed Brown    Output Argument:
37400910c330SBarry Smith .  U - state at given time
3741cd652676SJed Brown 
3742cd652676SJed Brown    Level: intermediate
3743cd652676SJed Brown 
3744cd652676SJed Brown    Developer Notes:
3745cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3746cd652676SJed Brown 
3747cd652676SJed Brown .keywords: TS, set
3748cd652676SJed Brown 
3749874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3750cd652676SJed Brown @*/
37510910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3752cd652676SJed Brown {
3753cd652676SJed Brown   PetscErrorCode ierr;
3754cd652676SJed Brown 
3755cd652676SJed Brown   PetscFunctionBegin;
3756cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3757b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3758be5899b3SLisandro 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);
3759ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
37600910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3761cd652676SJed Brown   PetscFunctionReturn(0);
3762cd652676SJed Brown }
3763cd652676SJed Brown 
3764d763cef2SBarry Smith /*@
37656d9e5789SSean Farley    TSStep - Steps one time step
3766d763cef2SBarry Smith 
3767d763cef2SBarry Smith    Collective on TS
3768d763cef2SBarry Smith 
3769d763cef2SBarry Smith    Input Parameter:
3770d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3771d763cef2SBarry Smith 
377227829d71SBarry Smith    Level: developer
3773d763cef2SBarry Smith 
3774b8123daeSJed Brown    Notes:
377527829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
377627829d71SBarry Smith 
3777b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3778b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3779b8123daeSJed Brown 
378025cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
378125cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
378225cb2221SBarry Smith 
3783d763cef2SBarry Smith .keywords: TS, timestep, solve
3784d763cef2SBarry Smith 
37859be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3786d763cef2SBarry Smith @*/
3787193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3788d763cef2SBarry Smith {
3789dfbe8321SBarry Smith   PetscErrorCode   ierr;
3790fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3791be5899b3SLisandro Dalcin   PetscReal        ptime;
3792d763cef2SBarry Smith 
3793d763cef2SBarry Smith   PetscFunctionBegin;
37940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3795fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3796fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3797fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3798fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3799fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3800fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3801302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3802fffbeea8SBarry Smith 
3803d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
380468bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3805d405a339SMatthew Knepley 
3806a6772fa2SLisandro 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()");
3807be5899b3SLisandro 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");
3808a6772fa2SLisandro Dalcin 
3809be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3810362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3811be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
3812e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3813d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3814193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3815d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3816be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
3817be5899b3SLisandro Dalcin   ts->steps++; ts->total_steps++;
3818be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
381928d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3820362cd11cSLisandro Dalcin 
3821362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3822cef5090cSJed Brown     if (ts->errorifstepfailed) {
382308c7845fSBarry 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]);
382408c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3825d2daff3dSHong Zhang     }
382608c7845fSBarry Smith   } else if (!ts->reason) {
382708c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
382808c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
382908c7845fSBarry Smith   }
383008c7845fSBarry Smith   PetscFunctionReturn(0);
383108c7845fSBarry Smith }
383208c7845fSBarry Smith 
383308c7845fSBarry Smith /*@
3834b957a604SHong Zhang    TSAdjointStep - Steps one time step backward in the adjoint run
383508c7845fSBarry Smith 
383608c7845fSBarry Smith    Collective on TS
383708c7845fSBarry Smith 
383808c7845fSBarry Smith    Input Parameter:
383908c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
384008c7845fSBarry Smith 
38416a4d4014SLisandro Dalcin    Level: intermediate
38426a4d4014SLisandro Dalcin 
3843b957a604SHong Zhang .keywords: TS, adjoint, step
38446a4d4014SLisandro Dalcin 
3845b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve()
38466a4d4014SLisandro Dalcin @*/
384708c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
38486a4d4014SLisandro Dalcin {
384908c7845fSBarry Smith   DM               dm;
38506a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
38516a4d4014SLisandro Dalcin 
38526a4d4014SLisandro Dalcin   PetscFunctionBegin;
38534a2ae208SSatish Balay   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
385408c7845fSBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
385508c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3856d763cef2SBarry Smith 
3857685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr);
3858d763cef2SBarry Smith 
3859be5899b3SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3860be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime;
386142f2b339SBarry 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);
3862be5899b3SLisandro Dalcin   ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
386342f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
38648d0ad7a8SHong Zhang   ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
3865be5899b3SLisandro Dalcin   ts->steps++; ts->total_steps--;
3866362cd11cSLisandro Dalcin 
3867362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3868cef5090cSJed Brown     if (ts->errorifstepfailed) {
38696c4ed002SBarry 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]);
38706c4ed002SBarry 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]);
38716c4ed002SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3872cef5090cSJed Brown     }
3873362cd11cSLisandro Dalcin   } else if (!ts->reason) {
387473b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
3875362cd11cSLisandro Dalcin   }
3876d763cef2SBarry Smith   PetscFunctionReturn(0);
3877d763cef2SBarry Smith }
3878d763cef2SBarry Smith 
38797cbde773SLisandro Dalcin /*@
38807cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
38817cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
38827cbde773SLisandro Dalcin 
38837cbde773SLisandro Dalcin    Collective on TS
38847cbde773SLisandro Dalcin 
38857cbde773SLisandro Dalcin    Input Arguments:
38867cbde773SLisandro Dalcin +  ts - time stepping context
38877cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
38887cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
38897cbde773SLisandro Dalcin 
38907cbde773SLisandro Dalcin    Output Arguments:
38917cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
38927cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
38937cbde773SLisandro Dalcin 
38947cbde773SLisandro Dalcin    Level: advanced
38957cbde773SLisandro Dalcin 
38967cbde773SLisandro Dalcin    Notes:
38977cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
38987cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
38997cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
39007cbde773SLisandro Dalcin 
39017cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
39027cbde773SLisandro Dalcin @*/
39037cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
39047cbde773SLisandro Dalcin {
39057cbde773SLisandro Dalcin   PetscErrorCode ierr;
39067cbde773SLisandro Dalcin 
39077cbde773SLisandro Dalcin   PetscFunctionBegin;
39087cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39097cbde773SLisandro Dalcin   PetscValidType(ts,1);
39107cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
39117cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
39127cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
39137cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
39147cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
39157cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
39167cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
39177cbde773SLisandro Dalcin   PetscFunctionReturn(0);
39187cbde773SLisandro Dalcin }
39197cbde773SLisandro Dalcin 
392005175c85SJed Brown /*@
392105175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
392205175c85SJed Brown 
39231c3436cfSJed Brown    Collective on TS
392405175c85SJed Brown 
392505175c85SJed Brown    Input Arguments:
39261c3436cfSJed Brown +  ts - time stepping context
39271c3436cfSJed Brown .  order - desired order of accuracy
39280298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
392905175c85SJed Brown 
393005175c85SJed Brown    Output Arguments:
39310910c330SBarry Smith .  U - state at the end of the current step
393205175c85SJed Brown 
393305175c85SJed Brown    Level: advanced
393405175c85SJed Brown 
3935108c343cSJed Brown    Notes:
3936108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3937108c343cSJed 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.
3938108c343cSJed Brown 
39391c3436cfSJed Brown .seealso: TSStep(), TSAdapt
394005175c85SJed Brown @*/
39410910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
394205175c85SJed Brown {
394305175c85SJed Brown   PetscErrorCode ierr;
394405175c85SJed Brown 
394505175c85SJed Brown   PetscFunctionBegin;
394605175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
394705175c85SJed Brown   PetscValidType(ts,1);
39480910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3949ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
39500910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
395105175c85SJed Brown   PetscFunctionReturn(0);
395205175c85SJed Brown }
395305175c85SJed Brown 
3954b1cb36f3SHong Zhang /*@
3955b1cb36f3SHong Zhang  TSForwardCostIntegral - Evaluate the cost integral in the forward run.
3956b1cb36f3SHong Zhang 
3957b1cb36f3SHong Zhang  Collective on TS
3958b1cb36f3SHong Zhang 
3959b1cb36f3SHong Zhang  Input Arguments:
3960b1cb36f3SHong Zhang  .  ts - time stepping context
3961b1cb36f3SHong Zhang 
3962b1cb36f3SHong Zhang  Level: advanced
3963b1cb36f3SHong Zhang 
3964b1cb36f3SHong Zhang  Notes:
3965b1cb36f3SHong Zhang  This function cannot be called until TSStep() has been completed.
3966b1cb36f3SHong Zhang 
3967b1cb36f3SHong Zhang  .seealso: TSSolve(), TSAdjointCostIntegral()
3968b1cb36f3SHong Zhang  @*/
3969b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts)
3970b1cb36f3SHong Zhang {
3971b1cb36f3SHong Zhang     PetscErrorCode ierr;
3972b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3973b1cb36f3SHong 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);
3974b1cb36f3SHong Zhang     ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr);
3975b1cb36f3SHong Zhang     PetscFunctionReturn(0);
3976b1cb36f3SHong Zhang }
3977d67e68b7SBarry Smith 
3978d763cef2SBarry Smith /*@
3979d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
3980d763cef2SBarry Smith 
3981d763cef2SBarry Smith    Collective on TS
3982d763cef2SBarry Smith 
3983d763cef2SBarry Smith    Input Parameter:
3984d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
398563e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
398663e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
39875a3a76d0SJed Brown 
3988d763cef2SBarry Smith    Level: beginner
3989d763cef2SBarry Smith 
39905a3a76d0SJed Brown    Notes:
39915a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
39925a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
39935a3a76d0SJed Brown    stepped over the final time.
39945a3a76d0SJed Brown 
3995d763cef2SBarry Smith .keywords: TS, timestep, solve
3996d763cef2SBarry Smith 
399763e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
3998d763cef2SBarry Smith @*/
3999cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
4000d763cef2SBarry Smith {
4001b06615a5SLisandro Dalcin   Vec               solution;
4002d763cef2SBarry Smith   PetscErrorCode    ierr;
4003d763cef2SBarry Smith 
4004d763cef2SBarry Smith   PetscFunctionBegin;
4005d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4006f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4007feed9e9dSBarry Smith 
400849354f04SShri 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 */
4009b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
40100910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4011b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4012b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4013b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
40145a3a76d0SJed Brown     }
40150910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4016bbd56ea5SKarl Rupp   } else if (u) {
40170910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
40185a3a76d0SJed Brown   }
4019b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
402068bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4021a6772fa2SLisandro Dalcin 
4022a6772fa2SLisandro 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()");
4023a6772fa2SLisandro 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");
4024a6772fa2SLisandro Dalcin 
4025e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4026e7069c78SShri      restarts the step after an event. Resetting these counters in such a case causes
4027e7069c78SShri      TSTrajectory to incorrectly save the output files
4028e7069c78SShri   */
4029938c94caSShri Abhyankar 
4030193ac0bcSJed Brown   ts->steps             = 0;
40315ef26d82SJed Brown   ts->ksp_its           = 0;
40325ef26d82SJed Brown   ts->snes_its          = 0;
4033c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
4034c610991cSLisandro Dalcin   ts->reject            = 0;
4035193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
4036193ac0bcSJed Brown 
4037ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4038f05ece33SBarry Smith 
4039193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4040193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4041a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4042cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4043a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4044be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4045db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
4046db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4047e7069c78SShri 
4048938c94caSShri Abhyankar     ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40496427ac75SLisandro Dalcin     ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
4050e7069c78SShri 
405128d5b5d6SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
405228d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_TRUE;
40536427ac75SLisandro Dalcin 
4054e1a7a14fSJed Brown     while (!ts->reason) {
4055938c94caSShri Abhyankar       ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40569687d888SLisandro Dalcin       if (!ts->steprollback) {
40579687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
40589687d888SLisandro Dalcin       }
4059193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4060e783b05fSHong 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. */
4061b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
4062b1cb36f3SHong Zhang       }
406310b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4064e783b05fSHong 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. */
4065e783b05fSHong Zhang       if (!ts->steprollback) {
4066938c94caSShri Abhyankar         ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40671eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4068aeb4809dSShri Abhyankar       }
4069193ac0bcSJed Brown     }
4070938c94caSShri Abhyankar     ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40716427ac75SLisandro Dalcin 
407249354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
40730910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4074cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4075b06615a5SLisandro Dalcin       solution = u;
407663e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
40770574a7fbSJed Brown     } else {
4078ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4079cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4080b06615a5SLisandro Dalcin       solution = ts->vec_sol;
40810574a7fbSJed Brown     }
4082193ac0bcSJed Brown   }
4083d2daff3dSHong Zhang 
4084ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
408563e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
408656f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4087ef222394SBarry Smith   if (ts->adjoint_solve) {
4088ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
40892b0a91c0SBarry Smith   }
4090d763cef2SBarry Smith   PetscFunctionReturn(0);
4091d763cef2SBarry Smith }
4092d763cef2SBarry Smith 
4093b1cb36f3SHong Zhang /*@
4094b1cb36f3SHong Zhang  TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run.
4095b1cb36f3SHong Zhang 
4096b1cb36f3SHong Zhang  Collective on TS
4097b1cb36f3SHong Zhang 
4098b1cb36f3SHong Zhang  Input Arguments:
4099b1cb36f3SHong Zhang  .  ts - time stepping context
4100b1cb36f3SHong Zhang 
4101b1cb36f3SHong Zhang  Level: advanced
4102b1cb36f3SHong Zhang 
4103b1cb36f3SHong Zhang  Notes:
4104b1cb36f3SHong Zhang  This function cannot be called until TSAdjointStep() has been completed.
4105b1cb36f3SHong Zhang 
4106b1cb36f3SHong Zhang  .seealso: TSAdjointSolve(), TSAdjointStep
4107b1cb36f3SHong Zhang  @*/
4108b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts)
4109b1cb36f3SHong Zhang {
4110b1cb36f3SHong Zhang     PetscErrorCode ierr;
4111b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4112b1cb36f3SHong 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);
4113b1cb36f3SHong Zhang     ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr);
4114b1cb36f3SHong Zhang     PetscFunctionReturn(0);
4115b1cb36f3SHong Zhang }
4116b1cb36f3SHong Zhang 
4117c88f2d44SBarry Smith /*@
4118c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
4119c88f2d44SBarry Smith 
4120c88f2d44SBarry Smith    Collective on TS
4121c88f2d44SBarry Smith 
4122c88f2d44SBarry Smith    Input Parameter:
41232c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
4124c88f2d44SBarry Smith 
4125e87fd094SBarry Smith    Options Database:
4126e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions
4127e87fd094SBarry Smith 
4128c88f2d44SBarry Smith    Level: intermediate
4129c88f2d44SBarry Smith 
4130c88f2d44SBarry Smith    Notes:
4131c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
4132c88f2d44SBarry Smith 
413373b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
413473b18844SBarry Smith 
4135c88f2d44SBarry Smith .keywords: TS, timestep, solve
4136c88f2d44SBarry Smith 
4137b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
4138c88f2d44SBarry Smith @*/
41392c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
4140c88f2d44SBarry Smith {
4141c88f2d44SBarry Smith   PetscErrorCode    ierr;
4142c88f2d44SBarry Smith 
4143c88f2d44SBarry Smith   PetscFunctionBegin;
4144c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4145c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
414668bece0bSHong Zhang 
4147c88f2d44SBarry Smith   /* reset time step and iteration counters */
4148c88f2d44SBarry Smith   ts->steps             = 0;
4149c88f2d44SBarry Smith   ts->ksp_its           = 0;
4150c88f2d44SBarry Smith   ts->snes_its          = 0;
4151c88f2d44SBarry Smith   ts->num_snes_failures = 0;
4152c88f2d44SBarry Smith   ts->reject            = 0;
4153c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
4154c88f2d44SBarry Smith 
415573b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
4156c88f2d44SBarry Smith 
415773b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
4158c88f2d44SBarry Smith   while (!ts->reason) {
415955c52731SHong Zhang     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
41609110b2e7SHong Zhang     ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
41616427ac75SLisandro Dalcin     ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr);
4162616946c1SHong Zhang     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
4163b1cb36f3SHong Zhang     if (ts->vec_costintegral && !ts->costintegralfwd) {
4164b1cb36f3SHong Zhang       ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr);
4165b1cb36f3SHong Zhang     }
4166c88f2d44SBarry Smith   }
416726656371SHong Zhang   ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
416826656371SHong Zhang   ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
4169c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
4170e210cd0eSHong Zhang   ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr);
4171685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr);
4172c88f2d44SBarry Smith   PetscFunctionReturn(0);
4173c88f2d44SBarry Smith }
4174c88f2d44SBarry Smith 
41757db568b7SBarry Smith /*@C
4176228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4177228d79bcSJed Brown 
4178228d79bcSJed Brown    Collective on TS
4179228d79bcSJed Brown 
4180228d79bcSJed Brown    Input Parameters:
4181228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4182228d79bcSJed Brown .  step - step number that has just completed
4183228d79bcSJed Brown .  ptime - model time of the state
41840910c330SBarry Smith -  u - state at the current model time
4185228d79bcSJed Brown 
4186228d79bcSJed Brown    Notes:
41877db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
41887db568b7SBarry Smith    Users would almost never call this routine directly.
4189228d79bcSJed Brown 
419063e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
419163e21af5SBarry Smith 
41927db568b7SBarry Smith    Level: developer
4193228d79bcSJed Brown 
4194228d79bcSJed Brown .keywords: TS, timestep
4195228d79bcSJed Brown @*/
41960910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4197d763cef2SBarry Smith {
4198d6152f81SLisandro Dalcin   DM             dm;
4199a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4200d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4201d763cef2SBarry Smith 
4202d763cef2SBarry Smith   PetscFunctionBegin;
4203b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4204b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4205d6152f81SLisandro Dalcin 
4206d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4207d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4208d6152f81SLisandro Dalcin 
42095edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
4210d763cef2SBarry Smith   for (i=0; i<n; i++) {
42110910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4212d763cef2SBarry Smith   }
42135edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
4214d763cef2SBarry Smith   PetscFunctionReturn(0);
4215d763cef2SBarry Smith }
4216d763cef2SBarry Smith 
42177db568b7SBarry Smith /*@C
42189110b2e7SHong Zhang    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()
42199110b2e7SHong Zhang 
42209110b2e7SHong Zhang    Collective on TS
42219110b2e7SHong Zhang 
42229110b2e7SHong Zhang    Input Parameters:
42239110b2e7SHong Zhang +  ts - time stepping context obtained from TSCreate()
42249110b2e7SHong Zhang .  step - step number that has just completed
42259110b2e7SHong Zhang .  ptime - model time of the state
42269110b2e7SHong Zhang .  u - state at the current model time
42279110b2e7SHong Zhang .  numcost - number of cost functions (dimension of lambda  or mu)
42289110b2e7SHong Zhang .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
42299110b2e7SHong Zhang -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
42309110b2e7SHong Zhang 
42319110b2e7SHong Zhang    Notes:
42327db568b7SBarry Smith    TSAdjointMonitor() is typically used automatically within the time stepping implementations.
42337db568b7SBarry Smith    Users would almost never call this routine directly.
42349110b2e7SHong Zhang 
42357db568b7SBarry Smith    Level: developer
42369110b2e7SHong Zhang 
42379110b2e7SHong Zhang .keywords: TS, timestep
42389110b2e7SHong Zhang @*/
42399110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
42409110b2e7SHong Zhang {
42419110b2e7SHong Zhang   PetscErrorCode ierr;
42429110b2e7SHong Zhang   PetscInt       i,n = ts->numberadjointmonitors;
42439110b2e7SHong Zhang 
42449110b2e7SHong Zhang   PetscFunctionBegin;
42459110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42469110b2e7SHong Zhang   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
42479110b2e7SHong Zhang   ierr = VecLockPush(u);CHKERRQ(ierr);
42489110b2e7SHong Zhang   for (i=0; i<n; i++) {
42499110b2e7SHong Zhang     ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
42509110b2e7SHong Zhang   }
42519110b2e7SHong Zhang   ierr = VecLockPop(u);CHKERRQ(ierr);
42529110b2e7SHong Zhang   PetscFunctionReturn(0);
42539110b2e7SHong Zhang }
42549110b2e7SHong Zhang 
4255d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4256d763cef2SBarry Smith /*@C
42577db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4258a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4259d763cef2SBarry Smith 
4260d763cef2SBarry Smith    Collective on TS
4261d763cef2SBarry Smith 
4262d763cef2SBarry Smith    Input Parameters:
4263d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4264d763cef2SBarry Smith .  label - the title to put in the title bar
42657c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4266a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4267a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4268d763cef2SBarry Smith 
4269d763cef2SBarry Smith    Output Parameter:
42700b039ecaSBarry Smith .  ctx - the context
4271d763cef2SBarry Smith 
4272d763cef2SBarry Smith    Options Database Key:
42734f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
42747db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
42757db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
42767db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
42777db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4278b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4279d763cef2SBarry Smith 
4280d763cef2SBarry Smith    Notes:
4281a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4282d763cef2SBarry Smith 
42837db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
42847db568b7SBarry Smith 
42857db568b7SBarry 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
42867db568b7SBarry 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
42877db568b7SBarry Smith    as the first argument.
42887db568b7SBarry Smith 
42897db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
42907db568b7SBarry Smith 
42917db568b7SBarry Smith 
4292d763cef2SBarry Smith    Level: intermediate
4293d763cef2SBarry Smith 
42947db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
4295d763cef2SBarry Smith 
42967db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
42977db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
42987db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
42997db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
43007db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
43017c922b88SBarry Smith 
4302d763cef2SBarry Smith @*/
4303a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4304d763cef2SBarry Smith {
43057f52daa2SLisandro Dalcin   PetscDraw      draw;
4306dfbe8321SBarry Smith   PetscErrorCode ierr;
4307d763cef2SBarry Smith 
4308d763cef2SBarry Smith   PetscFunctionBegin;
4309b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
43107f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43117f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43127f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4313287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
43147f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4315a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4316d763cef2SBarry Smith   PetscFunctionReturn(0);
4317d763cef2SBarry Smith }
4318d763cef2SBarry Smith 
4319b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4320d763cef2SBarry Smith {
43210b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4322c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4323dfbe8321SBarry Smith   PetscErrorCode ierr;
4324d763cef2SBarry Smith 
4325d763cef2SBarry Smith   PetscFunctionBegin;
432663e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4327b06615a5SLisandro Dalcin   if (!step) {
4328a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4329a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
43306934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr);
4331a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4332a9f9c1f6SBarry Smith   }
4333c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
43340b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4335b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
43360b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
43376934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
43383923b477SBarry Smith   }
4339d763cef2SBarry Smith   PetscFunctionReturn(0);
4340d763cef2SBarry Smith }
4341d763cef2SBarry Smith 
4342d763cef2SBarry Smith /*@C
4343a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4344a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4345d763cef2SBarry Smith 
43460b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4347d763cef2SBarry Smith 
4348d763cef2SBarry Smith    Input Parameter:
43490b039ecaSBarry Smith .  ctx - the monitor context
4350d763cef2SBarry Smith 
4351d763cef2SBarry Smith    Level: intermediate
4352d763cef2SBarry Smith 
4353d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
4354d763cef2SBarry Smith 
43554f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4356d763cef2SBarry Smith @*/
4357a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4358d763cef2SBarry Smith {
4359dfbe8321SBarry Smith   PetscErrorCode ierr;
4360d763cef2SBarry Smith 
4361d763cef2SBarry Smith   PetscFunctionBegin;
43627684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
43637684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
43647684fa3eSBarry Smith   }
43650b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
436631152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4367387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4368387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4369387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
43700b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4371d763cef2SBarry Smith   PetscFunctionReturn(0);
4372d763cef2SBarry Smith }
4373d763cef2SBarry Smith 
4374d763cef2SBarry Smith /*@
4375b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4376d763cef2SBarry Smith 
4377d763cef2SBarry Smith    Not Collective
4378d763cef2SBarry Smith 
4379d763cef2SBarry Smith    Input Parameter:
4380d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4381d763cef2SBarry Smith 
4382d763cef2SBarry Smith    Output Parameter:
438363e21af5SBarry Smith .  t  - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime().
4384d763cef2SBarry Smith 
4385d763cef2SBarry Smith    Level: beginner
4386d763cef2SBarry Smith 
4387b8123daeSJed Brown    Note:
4388b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
43899be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4390b8123daeSJed Brown 
439163e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime()
4392d763cef2SBarry Smith 
4393d763cef2SBarry Smith .keywords: TS, get, time
4394d763cef2SBarry Smith @*/
43957087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4396d763cef2SBarry Smith {
4397d763cef2SBarry Smith   PetscFunctionBegin;
43980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4399f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4400d763cef2SBarry Smith   *t = ts->ptime;
4401d763cef2SBarry Smith   PetscFunctionReturn(0);
4402d763cef2SBarry Smith }
4403d763cef2SBarry Smith 
4404e5e524a1SHong Zhang /*@
4405e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4406e5e524a1SHong Zhang 
4407e5e524a1SHong Zhang    Not Collective
4408e5e524a1SHong Zhang 
4409e5e524a1SHong Zhang    Input Parameter:
4410e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4411e5e524a1SHong Zhang 
4412e5e524a1SHong Zhang    Output Parameter:
4413e5e524a1SHong Zhang .  t  - the previous time
4414e5e524a1SHong Zhang 
4415e5e524a1SHong Zhang    Level: beginner
4416e5e524a1SHong Zhang 
4417e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
4418e5e524a1SHong Zhang 
4419e5e524a1SHong Zhang .keywords: TS, get, time
4420e5e524a1SHong Zhang @*/
4421e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4422e5e524a1SHong Zhang {
4423e5e524a1SHong Zhang   PetscFunctionBegin;
4424e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4425e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4426e5e524a1SHong Zhang   *t = ts->ptime_prev;
4427e5e524a1SHong Zhang   PetscFunctionReturn(0);
4428e5e524a1SHong Zhang }
4429e5e524a1SHong Zhang 
44306a4d4014SLisandro Dalcin /*@
44316a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
44326a4d4014SLisandro Dalcin 
44333f9fe445SBarry Smith    Logically Collective on TS
44346a4d4014SLisandro Dalcin 
44356a4d4014SLisandro Dalcin    Input Parameters:
44366a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
44376a4d4014SLisandro Dalcin -  time - the time
44386a4d4014SLisandro Dalcin 
44396a4d4014SLisandro Dalcin    Level: intermediate
44406a4d4014SLisandro Dalcin 
44416a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
44426a4d4014SLisandro Dalcin 
44436a4d4014SLisandro Dalcin .keywords: TS, set, time
44446a4d4014SLisandro Dalcin @*/
44457087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
44466a4d4014SLisandro Dalcin {
44476a4d4014SLisandro Dalcin   PetscFunctionBegin;
44480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4449c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
44506a4d4014SLisandro Dalcin   ts->ptime = t;
44516a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
44526a4d4014SLisandro Dalcin }
44536a4d4014SLisandro Dalcin 
4454d763cef2SBarry Smith /*@C
4455d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4456d763cef2SBarry Smith    TS options in the database.
4457d763cef2SBarry Smith 
44583f9fe445SBarry Smith    Logically Collective on TS
4459d763cef2SBarry Smith 
4460d763cef2SBarry Smith    Input Parameter:
4461d763cef2SBarry Smith +  ts     - The TS context
4462d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4463d763cef2SBarry Smith 
4464d763cef2SBarry Smith    Notes:
4465d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4466d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4467d763cef2SBarry Smith    hyphen.
4468d763cef2SBarry Smith 
4469d763cef2SBarry Smith    Level: advanced
4470d763cef2SBarry Smith 
4471d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4472d763cef2SBarry Smith 
4473d763cef2SBarry Smith .seealso: TSSetFromOptions()
4474d763cef2SBarry Smith 
4475d763cef2SBarry Smith @*/
44767087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4477d763cef2SBarry Smith {
4478dfbe8321SBarry Smith   PetscErrorCode ierr;
4479089b2837SJed Brown   SNES           snes;
4480d763cef2SBarry Smith 
4481d763cef2SBarry Smith   PetscFunctionBegin;
44820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4483d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4484089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4485089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4486d763cef2SBarry Smith   PetscFunctionReturn(0);
4487d763cef2SBarry Smith }
4488d763cef2SBarry Smith 
4489d763cef2SBarry Smith 
4490d763cef2SBarry Smith /*@C
4491d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4492d763cef2SBarry Smith    TS options in the database.
4493d763cef2SBarry Smith 
44943f9fe445SBarry Smith    Logically Collective on TS
4495d763cef2SBarry Smith 
4496d763cef2SBarry Smith    Input Parameter:
4497d763cef2SBarry Smith +  ts     - The TS context
4498d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4499d763cef2SBarry Smith 
4500d763cef2SBarry Smith    Notes:
4501d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4502d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4503d763cef2SBarry Smith    hyphen.
4504d763cef2SBarry Smith 
4505d763cef2SBarry Smith    Level: advanced
4506d763cef2SBarry Smith 
4507d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4508d763cef2SBarry Smith 
4509d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4510d763cef2SBarry Smith 
4511d763cef2SBarry Smith @*/
45127087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4513d763cef2SBarry Smith {
4514dfbe8321SBarry Smith   PetscErrorCode ierr;
4515089b2837SJed Brown   SNES           snes;
4516d763cef2SBarry Smith 
4517d763cef2SBarry Smith   PetscFunctionBegin;
45180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4519d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4520089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4521089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4522d763cef2SBarry Smith   PetscFunctionReturn(0);
4523d763cef2SBarry Smith }
4524d763cef2SBarry Smith 
4525d763cef2SBarry Smith /*@C
4526d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4527d763cef2SBarry Smith    TS options in the database.
4528d763cef2SBarry Smith 
4529d763cef2SBarry Smith    Not Collective
4530d763cef2SBarry Smith 
4531d763cef2SBarry Smith    Input Parameter:
4532d763cef2SBarry Smith .  ts - The TS context
4533d763cef2SBarry Smith 
4534d763cef2SBarry Smith    Output Parameter:
4535d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4536d763cef2SBarry Smith 
4537d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
4538d763cef2SBarry Smith    sufficient length to hold the prefix.
4539d763cef2SBarry Smith 
4540d763cef2SBarry Smith    Level: intermediate
4541d763cef2SBarry Smith 
4542d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4543d763cef2SBarry Smith 
4544d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4545d763cef2SBarry Smith @*/
45467087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4547d763cef2SBarry Smith {
4548dfbe8321SBarry Smith   PetscErrorCode ierr;
4549d763cef2SBarry Smith 
4550d763cef2SBarry Smith   PetscFunctionBegin;
45510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45524482741eSBarry Smith   PetscValidPointer(prefix,2);
4553d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4554d763cef2SBarry Smith   PetscFunctionReturn(0);
4555d763cef2SBarry Smith }
4556d763cef2SBarry Smith 
4557d763cef2SBarry Smith /*@C
4558d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4559d763cef2SBarry Smith 
4560d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4561d763cef2SBarry Smith 
4562d763cef2SBarry Smith    Input Parameter:
4563d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4564d763cef2SBarry Smith 
4565d763cef2SBarry Smith    Output Parameters:
4566e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4567e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4568e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4569e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4570d763cef2SBarry Smith 
45710298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
4572d763cef2SBarry Smith 
4573d763cef2SBarry Smith    Level: intermediate
4574d763cef2SBarry Smith 
457526d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
4576d763cef2SBarry Smith 
4577d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4578d763cef2SBarry Smith @*/
4579e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4580d763cef2SBarry Smith {
4581089b2837SJed Brown   PetscErrorCode ierr;
4582089b2837SJed Brown   SNES           snes;
458324989b8cSPeter Brune   DM             dm;
4584089b2837SJed Brown 
4585d763cef2SBarry Smith   PetscFunctionBegin;
4586089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4587e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
458824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
458924989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4590d763cef2SBarry Smith   PetscFunctionReturn(0);
4591d763cef2SBarry Smith }
4592d763cef2SBarry Smith 
45932eca1d9cSJed Brown /*@C
45942eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
45952eca1d9cSJed Brown 
45962eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
45972eca1d9cSJed Brown 
45982eca1d9cSJed Brown    Input Parameter:
45992eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
46002eca1d9cSJed Brown 
46012eca1d9cSJed Brown    Output Parameters:
4602e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4603e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
46042eca1d9cSJed Brown .  f   - The function to compute the matrices
46052eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
46062eca1d9cSJed Brown 
46070298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
46082eca1d9cSJed Brown 
46092eca1d9cSJed Brown    Level: advanced
46102eca1d9cSJed Brown 
46112eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
46122eca1d9cSJed Brown 
46132eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
46142eca1d9cSJed Brown @*/
4615e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
46162eca1d9cSJed Brown {
4617089b2837SJed Brown   PetscErrorCode ierr;
4618089b2837SJed Brown   SNES           snes;
461924989b8cSPeter Brune   DM             dm;
46200910c330SBarry Smith 
46212eca1d9cSJed Brown   PetscFunctionBegin;
4622089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4623f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4624e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
462524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
462624989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
46272eca1d9cSJed Brown   PetscFunctionReturn(0);
46282eca1d9cSJed Brown }
46292eca1d9cSJed Brown 
46306083293cSBarry Smith 
46311713a123SBarry Smith /*@C
463283a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
46331713a123SBarry Smith    VecView() for the solution at each timestep
46341713a123SBarry Smith 
46351713a123SBarry Smith    Collective on TS
46361713a123SBarry Smith 
46371713a123SBarry Smith    Input Parameters:
46381713a123SBarry Smith +  ts - the TS context
46391713a123SBarry Smith .  step - current time-step
4640142b95e3SSatish Balay .  ptime - current time
46410298fd71SBarry Smith -  dummy - either a viewer or NULL
46421713a123SBarry Smith 
464399fdda47SBarry Smith    Options Database:
464499fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
464599fdda47SBarry Smith 
4646387f4636SBarry 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
464799fdda47SBarry Smith        will look bad
464899fdda47SBarry Smith 
46491713a123SBarry Smith    Level: intermediate
46501713a123SBarry Smith 
46511713a123SBarry Smith .keywords: TS,  vector, monitor, view
46521713a123SBarry Smith 
4653a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
46541713a123SBarry Smith @*/
46550910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46561713a123SBarry Smith {
4657dfbe8321SBarry Smith   PetscErrorCode   ierr;
465883a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4659473a3ab2SBarry Smith   PetscDraw        draw;
46601713a123SBarry Smith 
46611713a123SBarry Smith   PetscFunctionBegin;
46626083293cSBarry Smith   if (!step && ictx->showinitial) {
46636083293cSBarry Smith     if (!ictx->initialsolution) {
46640910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
46651713a123SBarry Smith     }
46660910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
46676083293cSBarry Smith   }
4668b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
46690dcf80beSBarry Smith 
46706083293cSBarry Smith   if (ictx->showinitial) {
46716083293cSBarry Smith     PetscReal pause;
46726083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
46736083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
46746083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
46756083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
46766083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
46776083293cSBarry Smith   }
46780910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4679473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
468051fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4681473a3ab2SBarry Smith     char      time[32];
4682473a3ab2SBarry Smith 
4683473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
468485b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4685473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4686473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
468751fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4688473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4689473a3ab2SBarry Smith   }
4690473a3ab2SBarry Smith 
46916083293cSBarry Smith   if (ictx->showinitial) {
46926083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
46936083293cSBarry Smith   }
46941713a123SBarry Smith   PetscFunctionReturn(0);
46951713a123SBarry Smith }
46961713a123SBarry Smith 
46979110b2e7SHong Zhang /*@C
46989110b2e7SHong Zhang    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
46999110b2e7SHong Zhang    VecView() for the sensitivities to initial states at each timestep
47009110b2e7SHong Zhang 
47019110b2e7SHong Zhang    Collective on TS
47029110b2e7SHong Zhang 
47039110b2e7SHong Zhang    Input Parameters:
47049110b2e7SHong Zhang +  ts - the TS context
47059110b2e7SHong Zhang .  step - current time-step
47069110b2e7SHong Zhang .  ptime - current time
47079110b2e7SHong Zhang .  u - current state
47089110b2e7SHong Zhang .  numcost - number of cost functions
47099110b2e7SHong Zhang .  lambda - sensitivities to initial conditions
47109110b2e7SHong Zhang .  mu - sensitivities to parameters
47119110b2e7SHong Zhang -  dummy - either a viewer or NULL
47129110b2e7SHong Zhang 
47139110b2e7SHong Zhang    Level: intermediate
47149110b2e7SHong Zhang 
47159110b2e7SHong Zhang .keywords: TS,  vector, adjoint, monitor, view
47169110b2e7SHong Zhang 
47179110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
47189110b2e7SHong Zhang @*/
47199110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
47209110b2e7SHong Zhang {
47219110b2e7SHong Zhang   PetscErrorCode   ierr;
47229110b2e7SHong Zhang   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
47239110b2e7SHong Zhang   PetscDraw        draw;
4724a0046e2cSSatish Balay   PetscReal        xl,yl,xr,yr,h;
4725a0046e2cSSatish Balay   char             time[32];
47269110b2e7SHong Zhang 
47279110b2e7SHong Zhang   PetscFunctionBegin;
47289110b2e7SHong Zhang   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
47299110b2e7SHong Zhang 
47309110b2e7SHong Zhang   ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr);
47319110b2e7SHong Zhang   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
47329110b2e7SHong Zhang   ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
47339110b2e7SHong Zhang   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
47349110b2e7SHong Zhang   h    = yl + .95*(yr - yl);
47359110b2e7SHong Zhang   ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
47369110b2e7SHong Zhang   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
47379110b2e7SHong Zhang   PetscFunctionReturn(0);
47389110b2e7SHong Zhang }
47399110b2e7SHong Zhang 
47402d5ee99bSBarry Smith /*@C
47412d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
47422d5ee99bSBarry Smith 
47432d5ee99bSBarry Smith    Collective on TS
47442d5ee99bSBarry Smith 
47452d5ee99bSBarry Smith    Input Parameters:
47462d5ee99bSBarry Smith +  ts - the TS context
47472d5ee99bSBarry Smith .  step - current time-step
47482d5ee99bSBarry Smith .  ptime - current time
47492d5ee99bSBarry Smith -  dummy - either a viewer or NULL
47502d5ee99bSBarry Smith 
47512d5ee99bSBarry Smith    Level: intermediate
47522d5ee99bSBarry Smith 
47532d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
47542d5ee99bSBarry Smith 
47552d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
47562d5ee99bSBarry Smith @*/
47572d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
47582d5ee99bSBarry Smith {
47592d5ee99bSBarry Smith   PetscErrorCode    ierr;
47602d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
47612d5ee99bSBarry Smith   PetscDraw         draw;
47626934998bSLisandro Dalcin   PetscDrawAxis     axis;
47632d5ee99bSBarry Smith   PetscInt          n;
47642d5ee99bSBarry Smith   PetscMPIInt       size;
47656934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
47662d5ee99bSBarry Smith   char              time[32];
47672d5ee99bSBarry Smith   const PetscScalar *U;
47682d5ee99bSBarry Smith 
47692d5ee99bSBarry Smith   PetscFunctionBegin;
47706934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
47716934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
47722d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
47736934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
47742d5ee99bSBarry Smith 
47752d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
47766934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
47776934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
47786934998bSLisandro Dalcin   if (!step) {
47796934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
47806934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
47816934998bSLisandro Dalcin   }
47822d5ee99bSBarry Smith 
47832d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
47846934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
47856934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4786a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
47876934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
47882d5ee99bSBarry Smith 
47896934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
47906934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
47912d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
47924b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
479385b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
47942d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
479551fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
47962d5ee99bSBarry Smith   }
47976934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
47982d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
47996934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
48002d5ee99bSBarry Smith   PetscFunctionReturn(0);
48012d5ee99bSBarry Smith }
48022d5ee99bSBarry Smith 
48031713a123SBarry Smith 
48046083293cSBarry Smith /*@C
480583a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
48066083293cSBarry Smith 
48076083293cSBarry Smith    Collective on TS
48086083293cSBarry Smith 
48096083293cSBarry Smith    Input Parameters:
48106083293cSBarry Smith .    ctx - the monitor context
48116083293cSBarry Smith 
48126083293cSBarry Smith    Level: intermediate
48136083293cSBarry Smith 
48146083293cSBarry Smith .keywords: TS,  vector, monitor, view
48156083293cSBarry Smith 
481683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
48176083293cSBarry Smith @*/
481883a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
48196083293cSBarry Smith {
48206083293cSBarry Smith   PetscErrorCode ierr;
48216083293cSBarry Smith 
48226083293cSBarry Smith   PetscFunctionBegin;
482383a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
482483a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
482583a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
48266083293cSBarry Smith   PetscFunctionReturn(0);
48276083293cSBarry Smith }
48286083293cSBarry Smith 
48296083293cSBarry Smith /*@C
483083a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
48316083293cSBarry Smith 
48326083293cSBarry Smith    Collective on TS
48336083293cSBarry Smith 
48346083293cSBarry Smith    Input Parameter:
48356083293cSBarry Smith .    ts - time-step context
48366083293cSBarry Smith 
48376083293cSBarry Smith    Output Patameter:
48386083293cSBarry Smith .    ctx - the monitor context
48396083293cSBarry Smith 
484099fdda47SBarry Smith    Options Database:
484199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
484299fdda47SBarry Smith 
48436083293cSBarry Smith    Level: intermediate
48446083293cSBarry Smith 
48456083293cSBarry Smith .keywords: TS,  vector, monitor, view
48466083293cSBarry Smith 
484783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
48486083293cSBarry Smith @*/
484983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
48506083293cSBarry Smith {
48516083293cSBarry Smith   PetscErrorCode   ierr;
48526083293cSBarry Smith 
48536083293cSBarry Smith   PetscFunctionBegin;
4854b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
485583a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4856e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4857bbd56ea5SKarl Rupp 
485883a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4859473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4860c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4861473a3ab2SBarry Smith 
4862473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4863c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
48646083293cSBarry Smith   PetscFunctionReturn(0);
48656083293cSBarry Smith }
48666083293cSBarry Smith 
48673a471f94SBarry Smith /*@C
486883a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
48693a471f94SBarry Smith    VecView() for the error at each timestep
48703a471f94SBarry Smith 
48713a471f94SBarry Smith    Collective on TS
48723a471f94SBarry Smith 
48733a471f94SBarry Smith    Input Parameters:
48743a471f94SBarry Smith +  ts - the TS context
48753a471f94SBarry Smith .  step - current time-step
48763a471f94SBarry Smith .  ptime - current time
48770298fd71SBarry Smith -  dummy - either a viewer or NULL
48783a471f94SBarry Smith 
48793a471f94SBarry Smith    Level: intermediate
48803a471f94SBarry Smith 
48813a471f94SBarry Smith .keywords: TS,  vector, monitor, view
48823a471f94SBarry Smith 
48833a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
48843a471f94SBarry Smith @*/
48850910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48863a471f94SBarry Smith {
48873a471f94SBarry Smith   PetscErrorCode   ierr;
488883a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
488983a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
48903a471f94SBarry Smith   Vec              work;
48913a471f94SBarry Smith 
48923a471f94SBarry Smith   PetscFunctionBegin;
4893b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48940910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
48953a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
48960910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
48973a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
48983a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
48993a471f94SBarry Smith   PetscFunctionReturn(0);
49003a471f94SBarry Smith }
49013a471f94SBarry Smith 
4902af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
49036c699258SBarry Smith /*@
49042a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
49056c699258SBarry Smith 
49063f9fe445SBarry Smith    Logically Collective on TS and DM
49076c699258SBarry Smith 
49086c699258SBarry Smith    Input Parameters:
49092a808120SBarry Smith +  ts - the ODE integrator object
49102a808120SBarry Smith -  dm - the dm, cannot be NULL
49116c699258SBarry Smith 
49126c699258SBarry Smith    Level: intermediate
49136c699258SBarry Smith 
49146c699258SBarry Smith 
49156c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
49166c699258SBarry Smith @*/
49177087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
49186c699258SBarry Smith {
49196c699258SBarry Smith   PetscErrorCode ierr;
4920089b2837SJed Brown   SNES           snes;
4921942e3340SBarry Smith   DMTS           tsdm;
49226c699258SBarry Smith 
49236c699258SBarry Smith   PetscFunctionBegin;
49240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49252a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
492670663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4927942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
49282a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4929942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4930942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
493124989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
493224989b8cSPeter Brune         tsdm->originaldm = dm;
493324989b8cSPeter Brune       }
493424989b8cSPeter Brune     }
49356bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
493624989b8cSPeter Brune   }
49376c699258SBarry Smith   ts->dm = dm;
4938bbd56ea5SKarl Rupp 
4939089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4940089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
49416c699258SBarry Smith   PetscFunctionReturn(0);
49426c699258SBarry Smith }
49436c699258SBarry Smith 
49446c699258SBarry Smith /*@
49456c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
49466c699258SBarry Smith 
49473f9fe445SBarry Smith    Not Collective
49486c699258SBarry Smith 
49496c699258SBarry Smith    Input Parameter:
49506c699258SBarry Smith . ts - the preconditioner context
49516c699258SBarry Smith 
49526c699258SBarry Smith    Output Parameter:
49536c699258SBarry Smith .  dm - the dm
49546c699258SBarry Smith 
49556c699258SBarry Smith    Level: intermediate
49566c699258SBarry Smith 
49576c699258SBarry Smith 
49586c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
49596c699258SBarry Smith @*/
49607087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
49616c699258SBarry Smith {
4962496e6a7aSJed Brown   PetscErrorCode ierr;
4963496e6a7aSJed Brown 
49646c699258SBarry Smith   PetscFunctionBegin;
49650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4966496e6a7aSJed Brown   if (!ts->dm) {
4967ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4968496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4969496e6a7aSJed Brown   }
49706c699258SBarry Smith   *dm = ts->dm;
49716c699258SBarry Smith   PetscFunctionReturn(0);
49726c699258SBarry Smith }
49731713a123SBarry Smith 
49740f5c6efeSJed Brown /*@
49750f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
49760f5c6efeSJed Brown 
49773f9fe445SBarry Smith    Logically Collective on SNES
49780f5c6efeSJed Brown 
49790f5c6efeSJed Brown    Input Parameter:
4980d42a1c89SJed Brown + snes - nonlinear solver
49810910c330SBarry Smith . U - the current state at which to evaluate the residual
4982d42a1c89SJed Brown - ctx - user context, must be a TS
49830f5c6efeSJed Brown 
49840f5c6efeSJed Brown    Output Parameter:
49850f5c6efeSJed Brown . F - the nonlinear residual
49860f5c6efeSJed Brown 
49870f5c6efeSJed Brown    Notes:
49880f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
49890f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
49900f5c6efeSJed Brown 
49910f5c6efeSJed Brown    Level: advanced
49920f5c6efeSJed Brown 
49930f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
49940f5c6efeSJed Brown @*/
49950910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
49960f5c6efeSJed Brown {
49970f5c6efeSJed Brown   TS             ts = (TS)ctx;
49980f5c6efeSJed Brown   PetscErrorCode ierr;
49990f5c6efeSJed Brown 
50000f5c6efeSJed Brown   PetscFunctionBegin;
50010f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50020910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50030f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
50040f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
50050910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
50060f5c6efeSJed Brown   PetscFunctionReturn(0);
50070f5c6efeSJed Brown }
50080f5c6efeSJed Brown 
50090f5c6efeSJed Brown /*@
50100f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
50110f5c6efeSJed Brown 
50120f5c6efeSJed Brown    Collective on SNES
50130f5c6efeSJed Brown 
50140f5c6efeSJed Brown    Input Parameter:
50150f5c6efeSJed Brown + snes - nonlinear solver
50160910c330SBarry Smith . U - the current state at which to evaluate the residual
50170f5c6efeSJed Brown - ctx - user context, must be a TS
50180f5c6efeSJed Brown 
50190f5c6efeSJed Brown    Output Parameter:
50200f5c6efeSJed Brown + A - the Jacobian
50210f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
50220f5c6efeSJed Brown - flag - indicates any structure change in the matrix
50230f5c6efeSJed Brown 
50240f5c6efeSJed Brown    Notes:
50250f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50260f5c6efeSJed Brown 
50270f5c6efeSJed Brown    Level: developer
50280f5c6efeSJed Brown 
50290f5c6efeSJed Brown .seealso: SNESSetJacobian()
50300f5c6efeSJed Brown @*/
5031d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
50320f5c6efeSJed Brown {
50330f5c6efeSJed Brown   TS             ts = (TS)ctx;
50340f5c6efeSJed Brown   PetscErrorCode ierr;
50350f5c6efeSJed Brown 
50360f5c6efeSJed Brown   PetscFunctionBegin;
50370f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50380910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50390f5c6efeSJed Brown   PetscValidPointer(A,3);
504094ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
50410f5c6efeSJed Brown   PetscValidPointer(B,4);
504294ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
50430f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5044d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
50450f5c6efeSJed Brown   PetscFunctionReturn(0);
50460f5c6efeSJed Brown }
5047325fc9f4SBarry Smith 
50480e4ef248SJed Brown /*@C
50499ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
50500e4ef248SJed Brown 
50510e4ef248SJed Brown    Collective on TS
50520e4ef248SJed Brown 
50530e4ef248SJed Brown    Input Arguments:
50540e4ef248SJed Brown +  ts - time stepping context
50550e4ef248SJed Brown .  t - time at which to evaluate
50560910c330SBarry Smith .  U - state at which to evaluate
50570e4ef248SJed Brown -  ctx - context
50580e4ef248SJed Brown 
50590e4ef248SJed Brown    Output Arguments:
50600e4ef248SJed Brown .  F - right hand side
50610e4ef248SJed Brown 
50620e4ef248SJed Brown    Level: intermediate
50630e4ef248SJed Brown 
50640e4ef248SJed Brown    Notes:
50650e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
50660e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
50670e4ef248SJed Brown 
50680e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
50690e4ef248SJed Brown @*/
50700910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
50710e4ef248SJed Brown {
50720e4ef248SJed Brown   PetscErrorCode ierr;
50730e4ef248SJed Brown   Mat            Arhs,Brhs;
50740e4ef248SJed Brown 
50750e4ef248SJed Brown   PetscFunctionBegin;
50760e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
5077d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
50780910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
50790e4ef248SJed Brown   PetscFunctionReturn(0);
50800e4ef248SJed Brown }
50810e4ef248SJed Brown 
50820e4ef248SJed Brown /*@C
50830e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
50840e4ef248SJed Brown 
50850e4ef248SJed Brown    Collective on TS
50860e4ef248SJed Brown 
50870e4ef248SJed Brown    Input Arguments:
50880e4ef248SJed Brown +  ts - time stepping context
50890e4ef248SJed Brown .  t - time at which to evaluate
50900910c330SBarry Smith .  U - state at which to evaluate
50910e4ef248SJed Brown -  ctx - context
50920e4ef248SJed Brown 
50930e4ef248SJed Brown    Output Arguments:
50940e4ef248SJed Brown +  A - pointer to operator
50950e4ef248SJed Brown .  B - pointer to preconditioning matrix
50960e4ef248SJed Brown -  flg - matrix structure flag
50970e4ef248SJed Brown 
50980e4ef248SJed Brown    Level: intermediate
50990e4ef248SJed Brown 
51000e4ef248SJed Brown    Notes:
51010e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
51020e4ef248SJed Brown 
51030e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
51040e4ef248SJed Brown @*/
5105d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
51060e4ef248SJed Brown {
51070e4ef248SJed Brown   PetscFunctionBegin;
51080e4ef248SJed Brown   PetscFunctionReturn(0);
51090e4ef248SJed Brown }
51100e4ef248SJed Brown 
51110026cea9SSean Farley /*@C
51120026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
51130026cea9SSean Farley 
51140026cea9SSean Farley    Collective on TS
51150026cea9SSean Farley 
51160026cea9SSean Farley    Input Arguments:
51170026cea9SSean Farley +  ts - time stepping context
51180026cea9SSean Farley .  t - time at which to evaluate
51190910c330SBarry Smith .  U - state at which to evaluate
51200910c330SBarry Smith .  Udot - time derivative of state vector
51210026cea9SSean Farley -  ctx - context
51220026cea9SSean Farley 
51230026cea9SSean Farley    Output Arguments:
51240026cea9SSean Farley .  F - left hand side
51250026cea9SSean Farley 
51260026cea9SSean Farley    Level: intermediate
51270026cea9SSean Farley 
51280026cea9SSean Farley    Notes:
51290910c330SBarry 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
51300026cea9SSean Farley    user is required to write their own TSComputeIFunction.
51310026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
51320026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
51330026cea9SSean Farley 
51349ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
51359ae8fd06SBarry Smith 
51369ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
51370026cea9SSean Farley @*/
51380910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
51390026cea9SSean Farley {
51400026cea9SSean Farley   PetscErrorCode ierr;
51410026cea9SSean Farley   Mat            A,B;
51420026cea9SSean Farley 
51430026cea9SSean Farley   PetscFunctionBegin;
51440298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5145d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
51460910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
51470026cea9SSean Farley   PetscFunctionReturn(0);
51480026cea9SSean Farley }
51490026cea9SSean Farley 
51500026cea9SSean Farley /*@C
5151b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
51520026cea9SSean Farley 
51530026cea9SSean Farley    Collective on TS
51540026cea9SSean Farley 
51550026cea9SSean Farley    Input Arguments:
51560026cea9SSean Farley +  ts - time stepping context
51570026cea9SSean Farley .  t - time at which to evaluate
51580910c330SBarry Smith .  U - state at which to evaluate
51590910c330SBarry Smith .  Udot - time derivative of state vector
51600026cea9SSean Farley .  shift - shift to apply
51610026cea9SSean Farley -  ctx - context
51620026cea9SSean Farley 
51630026cea9SSean Farley    Output Arguments:
51640026cea9SSean Farley +  A - pointer to operator
51650026cea9SSean Farley .  B - pointer to preconditioning matrix
51660026cea9SSean Farley -  flg - matrix structure flag
51670026cea9SSean Farley 
5168b41af12eSJed Brown    Level: advanced
51690026cea9SSean Farley 
51700026cea9SSean Farley    Notes:
51710026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
51720026cea9SSean Farley 
5173b41af12eSJed Brown    It is only appropriate for problems of the form
5174b41af12eSJed Brown 
5175b41af12eSJed Brown $     M Udot = F(U,t)
5176b41af12eSJed Brown 
5177b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5178b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5179b41af12eSJed Brown   an implicit operator of the form
5180b41af12eSJed Brown 
5181b41af12eSJed Brown $    shift*M + J
5182b41af12eSJed Brown 
5183b41af12eSJed 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
5184b41af12eSJed Brown   a copy of M or reassemble it when requested.
5185b41af12eSJed Brown 
51860026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
51870026cea9SSean Farley @*/
5188d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
51890026cea9SSean Farley {
5190b41af12eSJed Brown   PetscErrorCode ierr;
5191b41af12eSJed Brown 
51920026cea9SSean Farley   PetscFunctionBegin;
519394ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5194b41af12eSJed Brown   ts->ijacobian.shift = shift;
51950026cea9SSean Farley   PetscFunctionReturn(0);
51960026cea9SSean Farley }
5197b41af12eSJed Brown 
5198e817cc15SEmil Constantinescu /*@
5199e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5200e817cc15SEmil Constantinescu 
5201e817cc15SEmil Constantinescu    Not Collective
5202e817cc15SEmil Constantinescu 
5203e817cc15SEmil Constantinescu    Input Parameter:
5204e817cc15SEmil Constantinescu .  ts - the TS context
5205e817cc15SEmil Constantinescu 
5206e817cc15SEmil Constantinescu    Output Parameter:
52074e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5208e817cc15SEmil Constantinescu 
5209e817cc15SEmil Constantinescu    Level: beginner
5210e817cc15SEmil Constantinescu 
5211e817cc15SEmil Constantinescu .keywords: TS, equation type
5212e817cc15SEmil Constantinescu 
5213e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5214e817cc15SEmil Constantinescu @*/
5215e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5216e817cc15SEmil Constantinescu {
5217e817cc15SEmil Constantinescu   PetscFunctionBegin;
5218e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5219e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5220e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5221e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5222e817cc15SEmil Constantinescu }
5223e817cc15SEmil Constantinescu 
5224e817cc15SEmil Constantinescu /*@
5225e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5226e817cc15SEmil Constantinescu 
5227e817cc15SEmil Constantinescu    Not Collective
5228e817cc15SEmil Constantinescu 
5229e817cc15SEmil Constantinescu    Input Parameter:
5230e817cc15SEmil Constantinescu +  ts - the TS context
52311297b224SEmil Constantinescu -  equation_type - see TSEquationType
5232e817cc15SEmil Constantinescu 
5233e817cc15SEmil Constantinescu    Level: advanced
5234e817cc15SEmil Constantinescu 
5235e817cc15SEmil Constantinescu .keywords: TS, equation type
5236e817cc15SEmil Constantinescu 
5237e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5238e817cc15SEmil Constantinescu @*/
5239e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5240e817cc15SEmil Constantinescu {
5241e817cc15SEmil Constantinescu   PetscFunctionBegin;
5242e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5243e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5244e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5245e817cc15SEmil Constantinescu }
52460026cea9SSean Farley 
52474af1b03aSJed Brown /*@
52484af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
52494af1b03aSJed Brown 
52504af1b03aSJed Brown    Not Collective
52514af1b03aSJed Brown 
52524af1b03aSJed Brown    Input Parameter:
52534af1b03aSJed Brown .  ts - the TS context
52544af1b03aSJed Brown 
52554af1b03aSJed Brown    Output Parameter:
52564af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
52574af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
52584af1b03aSJed Brown 
5259487e0bb9SJed Brown    Level: beginner
52604af1b03aSJed Brown 
5261cd652676SJed Brown    Notes:
5262cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
52634af1b03aSJed Brown 
52644af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
52654af1b03aSJed Brown 
52664af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
52674af1b03aSJed Brown @*/
52684af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
52694af1b03aSJed Brown {
52704af1b03aSJed Brown   PetscFunctionBegin;
52714af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52724af1b03aSJed Brown   PetscValidPointer(reason,2);
52734af1b03aSJed Brown   *reason = ts->reason;
52744af1b03aSJed Brown   PetscFunctionReturn(0);
52754af1b03aSJed Brown }
52764af1b03aSJed Brown 
5277d6ad946cSShri Abhyankar /*@
5278d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5279d6ad946cSShri Abhyankar 
5280d6ad946cSShri Abhyankar    Not Collective
5281d6ad946cSShri Abhyankar 
5282d6ad946cSShri Abhyankar    Input Parameter:
5283d6ad946cSShri Abhyankar +  ts - the TS context
5284d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5285d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5286d6ad946cSShri Abhyankar 
5287f5abba47SShri Abhyankar    Level: advanced
5288d6ad946cSShri Abhyankar 
5289d6ad946cSShri Abhyankar    Notes:
5290d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
5291d6ad946cSShri Abhyankar 
5292d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
5293d6ad946cSShri Abhyankar 
5294d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5295d6ad946cSShri Abhyankar @*/
5296d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5297d6ad946cSShri Abhyankar {
5298d6ad946cSShri Abhyankar   PetscFunctionBegin;
5299d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5300d6ad946cSShri Abhyankar   ts->reason = reason;
5301d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5302d6ad946cSShri Abhyankar }
5303d6ad946cSShri Abhyankar 
5304cc708dedSBarry Smith /*@
5305cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5306cc708dedSBarry Smith 
5307cc708dedSBarry Smith    Not Collective
5308cc708dedSBarry Smith 
5309cc708dedSBarry Smith    Input Parameter:
5310cc708dedSBarry Smith .  ts - the TS context
5311cc708dedSBarry Smith 
5312cc708dedSBarry Smith    Output Parameter:
531363e21af5SBarry Smith .  ftime - the final time. This time corresponds to the final time set with TSSetDuration()
5314cc708dedSBarry Smith 
5315487e0bb9SJed Brown    Level: beginner
5316cc708dedSBarry Smith 
5317cc708dedSBarry Smith    Notes:
5318cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5319cc708dedSBarry Smith 
5320cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
5321cc708dedSBarry Smith 
5322cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5323cc708dedSBarry Smith @*/
5324cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5325cc708dedSBarry Smith {
5326cc708dedSBarry Smith   PetscFunctionBegin;
5327cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5328cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5329cc708dedSBarry Smith   *ftime = ts->solvetime;
5330cc708dedSBarry Smith   PetscFunctionReturn(0);
5331cc708dedSBarry Smith }
5332cc708dedSBarry Smith 
53332c18e0fdSBarry Smith /*@
53342c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
53352c18e0fdSBarry Smith 
53362c18e0fdSBarry Smith    Not Collective
53372c18e0fdSBarry Smith 
53382c18e0fdSBarry Smith    Input Parameter:
53392c18e0fdSBarry Smith .  ts - the TS context
53402c18e0fdSBarry Smith 
53412c18e0fdSBarry Smith    Output Parameter:
53422c18e0fdSBarry Smith .  steps - the number of steps
53432c18e0fdSBarry Smith 
53442c18e0fdSBarry Smith    Level: beginner
53452c18e0fdSBarry Smith 
53462c18e0fdSBarry Smith    Notes:
53472c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
53482c18e0fdSBarry Smith 
53492c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
53502c18e0fdSBarry Smith 
53512c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
53522c18e0fdSBarry Smith @*/
53532c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
53542c18e0fdSBarry Smith {
53552c18e0fdSBarry Smith   PetscFunctionBegin;
53562c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53572c18e0fdSBarry Smith   PetscValidPointer(steps,2);
53582c18e0fdSBarry Smith   *steps = ts->total_steps;
53592c18e0fdSBarry Smith   PetscFunctionReturn(0);
53602c18e0fdSBarry Smith }
53612c18e0fdSBarry Smith 
53629f67acb7SJed Brown /*@
53635ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
53649f67acb7SJed Brown    used by the time integrator.
53659f67acb7SJed Brown 
53669f67acb7SJed Brown    Not Collective
53679f67acb7SJed Brown 
53689f67acb7SJed Brown    Input Parameter:
53699f67acb7SJed Brown .  ts - TS context
53709f67acb7SJed Brown 
53719f67acb7SJed Brown    Output Parameter:
53729f67acb7SJed Brown .  nits - number of nonlinear iterations
53739f67acb7SJed Brown 
53749f67acb7SJed Brown    Notes:
53759f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53769f67acb7SJed Brown 
53779f67acb7SJed Brown    Level: intermediate
53789f67acb7SJed Brown 
53799f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
53809f67acb7SJed Brown 
53815ef26d82SJed Brown .seealso:  TSGetKSPIterations()
53829f67acb7SJed Brown @*/
53835ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
53849f67acb7SJed Brown {
53859f67acb7SJed Brown   PetscFunctionBegin;
53869f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53879f67acb7SJed Brown   PetscValidIntPointer(nits,2);
53885ef26d82SJed Brown   *nits = ts->snes_its;
53899f67acb7SJed Brown   PetscFunctionReturn(0);
53909f67acb7SJed Brown }
53919f67acb7SJed Brown 
53929f67acb7SJed Brown /*@
53935ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
53949f67acb7SJed Brown    used by the time integrator.
53959f67acb7SJed Brown 
53969f67acb7SJed Brown    Not Collective
53979f67acb7SJed Brown 
53989f67acb7SJed Brown    Input Parameter:
53999f67acb7SJed Brown .  ts - TS context
54009f67acb7SJed Brown 
54019f67acb7SJed Brown    Output Parameter:
54029f67acb7SJed Brown .  lits - number of linear iterations
54039f67acb7SJed Brown 
54049f67acb7SJed Brown    Notes:
54059f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
54069f67acb7SJed Brown 
54079f67acb7SJed Brown    Level: intermediate
54089f67acb7SJed Brown 
54099f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
54109f67acb7SJed Brown 
54115ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
54129f67acb7SJed Brown @*/
54135ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
54149f67acb7SJed Brown {
54159f67acb7SJed Brown   PetscFunctionBegin;
54169f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54179f67acb7SJed Brown   PetscValidIntPointer(lits,2);
54185ef26d82SJed Brown   *lits = ts->ksp_its;
54199f67acb7SJed Brown   PetscFunctionReturn(0);
54209f67acb7SJed Brown }
54219f67acb7SJed Brown 
5422cef5090cSJed Brown /*@
5423cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5424cef5090cSJed Brown 
5425cef5090cSJed Brown    Not Collective
5426cef5090cSJed Brown 
5427cef5090cSJed Brown    Input Parameter:
5428cef5090cSJed Brown .  ts - TS context
5429cef5090cSJed Brown 
5430cef5090cSJed Brown    Output Parameter:
5431cef5090cSJed Brown .  rejects - number of steps rejected
5432cef5090cSJed Brown 
5433cef5090cSJed Brown    Notes:
5434cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5435cef5090cSJed Brown 
5436cef5090cSJed Brown    Level: intermediate
5437cef5090cSJed Brown 
5438cef5090cSJed Brown .keywords: TS, get, number
5439cef5090cSJed Brown 
54405ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5441cef5090cSJed Brown @*/
5442cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5443cef5090cSJed Brown {
5444cef5090cSJed Brown   PetscFunctionBegin;
5445cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5446cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5447cef5090cSJed Brown   *rejects = ts->reject;
5448cef5090cSJed Brown   PetscFunctionReturn(0);
5449cef5090cSJed Brown }
5450cef5090cSJed Brown 
5451cef5090cSJed Brown /*@
5452cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5453cef5090cSJed Brown 
5454cef5090cSJed Brown    Not Collective
5455cef5090cSJed Brown 
5456cef5090cSJed Brown    Input Parameter:
5457cef5090cSJed Brown .  ts - TS context
5458cef5090cSJed Brown 
5459cef5090cSJed Brown    Output Parameter:
5460cef5090cSJed Brown .  fails - number of failed nonlinear solves
5461cef5090cSJed Brown 
5462cef5090cSJed Brown    Notes:
5463cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5464cef5090cSJed Brown 
5465cef5090cSJed Brown    Level: intermediate
5466cef5090cSJed Brown 
5467cef5090cSJed Brown .keywords: TS, get, number
5468cef5090cSJed Brown 
54695ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5470cef5090cSJed Brown @*/
5471cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5472cef5090cSJed Brown {
5473cef5090cSJed Brown   PetscFunctionBegin;
5474cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5475cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5476cef5090cSJed Brown   *fails = ts->num_snes_failures;
5477cef5090cSJed Brown   PetscFunctionReturn(0);
5478cef5090cSJed Brown }
5479cef5090cSJed Brown 
5480cef5090cSJed Brown /*@
5481cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5482cef5090cSJed Brown 
5483cef5090cSJed Brown    Not Collective
5484cef5090cSJed Brown 
5485cef5090cSJed Brown    Input Parameter:
5486cef5090cSJed Brown +  ts - TS context
5487cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5488cef5090cSJed Brown 
5489cef5090cSJed Brown    Notes:
5490cef5090cSJed Brown    The counter is reset to zero for each step
5491cef5090cSJed Brown 
5492cef5090cSJed Brown    Options Database Key:
5493cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5494cef5090cSJed Brown 
5495cef5090cSJed Brown    Level: intermediate
5496cef5090cSJed Brown 
5497cef5090cSJed Brown .keywords: TS, set, maximum, number
5498cef5090cSJed Brown 
54995ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5500cef5090cSJed Brown @*/
5501cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5502cef5090cSJed Brown {
5503cef5090cSJed Brown   PetscFunctionBegin;
5504cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5505cef5090cSJed Brown   ts->max_reject = rejects;
5506cef5090cSJed Brown   PetscFunctionReturn(0);
5507cef5090cSJed Brown }
5508cef5090cSJed Brown 
5509cef5090cSJed Brown /*@
5510cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5511cef5090cSJed Brown 
5512cef5090cSJed Brown    Not Collective
5513cef5090cSJed Brown 
5514cef5090cSJed Brown    Input Parameter:
5515cef5090cSJed Brown +  ts - TS context
5516cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5517cef5090cSJed Brown 
5518cef5090cSJed Brown    Notes:
5519cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5520cef5090cSJed Brown 
5521cef5090cSJed Brown    Options Database Key:
5522cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5523cef5090cSJed Brown 
5524cef5090cSJed Brown    Level: intermediate
5525cef5090cSJed Brown 
5526cef5090cSJed Brown .keywords: TS, set, maximum, number
5527cef5090cSJed Brown 
55285ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5529cef5090cSJed Brown @*/
5530cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5531cef5090cSJed Brown {
5532cef5090cSJed Brown   PetscFunctionBegin;
5533cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5534cef5090cSJed Brown   ts->max_snes_failures = fails;
5535cef5090cSJed Brown   PetscFunctionReturn(0);
5536cef5090cSJed Brown }
5537cef5090cSJed Brown 
5538cef5090cSJed Brown /*@
5539cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5540cef5090cSJed Brown 
5541cef5090cSJed Brown    Not Collective
5542cef5090cSJed Brown 
5543cef5090cSJed Brown    Input Parameter:
5544cef5090cSJed Brown +  ts - TS context
5545cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5546cef5090cSJed Brown 
5547cef5090cSJed Brown    Options Database Key:
5548cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5549cef5090cSJed Brown 
5550cef5090cSJed Brown    Level: intermediate
5551cef5090cSJed Brown 
5552cef5090cSJed Brown .keywords: TS, set, error
5553cef5090cSJed Brown 
55545ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5555cef5090cSJed Brown @*/
5556cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5557cef5090cSJed Brown {
5558cef5090cSJed Brown   PetscFunctionBegin;
5559cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5560cef5090cSJed Brown   ts->errorifstepfailed = err;
5561cef5090cSJed Brown   PetscFunctionReturn(0);
5562cef5090cSJed Brown }
5563cef5090cSJed Brown 
5564fb1732b5SBarry Smith /*@C
5565fde5950dSBarry 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
5566fb1732b5SBarry Smith 
5567fb1732b5SBarry Smith    Collective on TS
5568fb1732b5SBarry Smith 
5569fb1732b5SBarry Smith    Input Parameters:
5570fb1732b5SBarry Smith +  ts - the TS context
5571fb1732b5SBarry Smith .  step - current time-step
5572fb1732b5SBarry Smith .  ptime - current time
55730910c330SBarry Smith .  u - current state
5574721cd6eeSBarry Smith -  vf - viewer and its format
5575fb1732b5SBarry Smith 
5576fb1732b5SBarry Smith    Level: intermediate
5577fb1732b5SBarry Smith 
5578fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5579fb1732b5SBarry Smith 
5580fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5581fb1732b5SBarry Smith @*/
5582721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5583fb1732b5SBarry Smith {
5584fb1732b5SBarry Smith   PetscErrorCode ierr;
5585fb1732b5SBarry Smith 
5586fb1732b5SBarry Smith   PetscFunctionBegin;
5587721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5588721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5589721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5590ed81e22dSJed Brown   PetscFunctionReturn(0);
5591ed81e22dSJed Brown }
5592ed81e22dSJed Brown 
5593ed81e22dSJed Brown /*@C
5594ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5595ed81e22dSJed Brown 
5596ed81e22dSJed Brown    Collective on TS
5597ed81e22dSJed Brown 
5598ed81e22dSJed Brown    Input Parameters:
5599ed81e22dSJed Brown +  ts - the TS context
5600ed81e22dSJed Brown .  step - current time-step
5601ed81e22dSJed Brown .  ptime - current time
56020910c330SBarry Smith .  u - current state
5603ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5604ed81e22dSJed Brown 
5605ed81e22dSJed Brown    Level: intermediate
5606ed81e22dSJed Brown 
5607ed81e22dSJed Brown    Notes:
5608ed81e22dSJed 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.
5609ed81e22dSJed Brown    These are named according to the file name template.
5610ed81e22dSJed Brown 
5611ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5612ed81e22dSJed Brown 
5613ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5614ed81e22dSJed Brown 
5615ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5616ed81e22dSJed Brown @*/
56170910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5618ed81e22dSJed Brown {
5619ed81e22dSJed Brown   PetscErrorCode ierr;
5620ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5621ed81e22dSJed Brown   PetscViewer    viewer;
5622ed81e22dSJed Brown 
5623ed81e22dSJed Brown   PetscFunctionBegin;
562463e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
56258caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5626ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
56270910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5628ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5629ed81e22dSJed Brown   PetscFunctionReturn(0);
5630ed81e22dSJed Brown }
5631ed81e22dSJed Brown 
5632ed81e22dSJed Brown /*@C
5633ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5634ed81e22dSJed Brown 
5635ed81e22dSJed Brown    Collective on TS
5636ed81e22dSJed Brown 
5637ed81e22dSJed Brown    Input Parameters:
5638ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5639ed81e22dSJed Brown 
5640ed81e22dSJed Brown    Level: intermediate
5641ed81e22dSJed Brown 
5642ed81e22dSJed Brown    Note:
5643ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5644ed81e22dSJed Brown 
5645ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5646ed81e22dSJed Brown 
5647ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5648ed81e22dSJed Brown @*/
5649ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5650ed81e22dSJed Brown {
5651ed81e22dSJed Brown   PetscErrorCode ierr;
5652ed81e22dSJed Brown 
5653ed81e22dSJed Brown   PetscFunctionBegin;
5654ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5655fb1732b5SBarry Smith   PetscFunctionReturn(0);
5656fb1732b5SBarry Smith }
5657fb1732b5SBarry Smith 
565884df9cb4SJed Brown /*@
5659552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
566084df9cb4SJed Brown 
5661ed81e22dSJed Brown    Collective on TS if controller has not been created yet
566284df9cb4SJed Brown 
566384df9cb4SJed Brown    Input Arguments:
5664ed81e22dSJed Brown .  ts - time stepping context
566584df9cb4SJed Brown 
566684df9cb4SJed Brown    Output Arguments:
5667ed81e22dSJed Brown .  adapt - adaptive controller
566884df9cb4SJed Brown 
566984df9cb4SJed Brown    Level: intermediate
567084df9cb4SJed Brown 
5671ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
567284df9cb4SJed Brown @*/
5673552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
567484df9cb4SJed Brown {
567584df9cb4SJed Brown   PetscErrorCode ierr;
567684df9cb4SJed Brown 
567784df9cb4SJed Brown   PetscFunctionBegin;
567884df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5679bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
568084df9cb4SJed Brown   if (!ts->adapt) {
5681ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
56823bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
56831c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
568484df9cb4SJed Brown   }
5685bec58848SLisandro Dalcin   *adapt = ts->adapt;
568684df9cb4SJed Brown   PetscFunctionReturn(0);
568784df9cb4SJed Brown }
5688d6ebe24aSShri Abhyankar 
56891c3436cfSJed Brown /*@
56901c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
56911c3436cfSJed Brown 
56921c3436cfSJed Brown    Logically Collective
56931c3436cfSJed Brown 
56941c3436cfSJed Brown    Input Arguments:
56951c3436cfSJed Brown +  ts - time integration context
56961c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
56970298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
56981c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
56990298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
57001c3436cfSJed Brown 
5701a3cdaa26SBarry Smith    Options Database keys:
5702a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5703a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5704a3cdaa26SBarry Smith 
57053ff766beSShri Abhyankar    Notes:
57063ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
57073ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
57083ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
57093ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
57103ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
57113ff766beSShri Abhyankar    differential variables.
57123ff766beSShri Abhyankar 
57131c3436cfSJed Brown    Level: beginner
57141c3436cfSJed Brown 
5715c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
57161c3436cfSJed Brown @*/
57171c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
57181c3436cfSJed Brown {
57191c3436cfSJed Brown   PetscErrorCode ierr;
57201c3436cfSJed Brown 
57211c3436cfSJed Brown   PetscFunctionBegin;
5722c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
57231c3436cfSJed Brown   if (vatol) {
57241c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
57251c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
57261c3436cfSJed Brown     ts->vatol = vatol;
57271c3436cfSJed Brown   }
5728c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
57291c3436cfSJed Brown   if (vrtol) {
57301c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
57311c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
57321c3436cfSJed Brown     ts->vrtol = vrtol;
57331c3436cfSJed Brown   }
57341c3436cfSJed Brown   PetscFunctionReturn(0);
57351c3436cfSJed Brown }
57361c3436cfSJed Brown 
5737c5033834SJed Brown /*@
5738c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5739c5033834SJed Brown 
5740c5033834SJed Brown    Logically Collective
5741c5033834SJed Brown 
5742c5033834SJed Brown    Input Arguments:
5743c5033834SJed Brown .  ts - time integration context
5744c5033834SJed Brown 
5745c5033834SJed Brown    Output Arguments:
57460298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
57470298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
57480298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
57490298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5750c5033834SJed Brown 
5751c5033834SJed Brown    Level: beginner
5752c5033834SJed Brown 
5753c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5754c5033834SJed Brown @*/
5755c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5756c5033834SJed Brown {
5757c5033834SJed Brown   PetscFunctionBegin;
5758c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5759c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5760c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5761c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5762c5033834SJed Brown   PetscFunctionReturn(0);
5763c5033834SJed Brown }
5764c5033834SJed Brown 
57659c6b16b5SShri Abhyankar /*@
5766a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
57679c6b16b5SShri Abhyankar 
57689c6b16b5SShri Abhyankar    Collective on TS
57699c6b16b5SShri Abhyankar 
57709c6b16b5SShri Abhyankar    Input Arguments:
57719c6b16b5SShri Abhyankar +  ts - time stepping context
5772a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5773a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
57749c6b16b5SShri Abhyankar 
57759c6b16b5SShri Abhyankar    Output Arguments:
57767453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57777453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
57787453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57799c6b16b5SShri Abhyankar 
57809c6b16b5SShri Abhyankar    Level: developer
57819c6b16b5SShri Abhyankar 
5782deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
57839c6b16b5SShri Abhyankar @*/
57847453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57859c6b16b5SShri Abhyankar {
57869c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
57879c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
57887453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
57897453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
57909c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
57917453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
57927453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
57937453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
57949c6b16b5SShri Abhyankar 
57959c6b16b5SShri Abhyankar   PetscFunctionBegin;
57969c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5797a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5798a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5799a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5800a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5801a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5802a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
58038a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
58048a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5805a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
58069c6b16b5SShri Abhyankar 
58079c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
58089c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
58099c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
58109c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
58119c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
58127453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
58137453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
58147453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
58159c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
58169c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
58179c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58189c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58199c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
58207453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58217453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
58227453f775SEmil Constantinescu       if(tola>0.){
58237453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58247453f775SEmil Constantinescu         na_loc++;
58257453f775SEmil Constantinescu       }
58267453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58277453f775SEmil Constantinescu       if(tolr>0.){
58287453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58297453f775SEmil Constantinescu         nr_loc++;
58307453f775SEmil Constantinescu       }
58317453f775SEmil Constantinescu       tol=tola+tolr;
58327453f775SEmil Constantinescu       if(tol>0.){
58337453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58347453f775SEmil Constantinescu         n_loc++;
58357453f775SEmil Constantinescu       }
58369c6b16b5SShri Abhyankar     }
58379c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58389c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58399c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
58409c6b16b5SShri Abhyankar     const PetscScalar *atol;
58419c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58429c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
58437453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58447453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
58457453f775SEmil Constantinescu       if(tola>0.){
58467453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58477453f775SEmil Constantinescu         na_loc++;
58487453f775SEmil Constantinescu       }
58497453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58507453f775SEmil Constantinescu       if(tolr>0.){
58517453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58527453f775SEmil Constantinescu         nr_loc++;
58537453f775SEmil Constantinescu       }
58547453f775SEmil Constantinescu       tol=tola+tolr;
58557453f775SEmil Constantinescu       if(tol>0.){
58567453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58577453f775SEmil Constantinescu         n_loc++;
58587453f775SEmil Constantinescu       }
58599c6b16b5SShri Abhyankar     }
58609c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58619c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
58629c6b16b5SShri Abhyankar     const PetscScalar *rtol;
58639c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58649c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
58657453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58667453f775SEmil Constantinescu       tola = ts->atol;
58677453f775SEmil Constantinescu       if(tola>0.){
58687453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58697453f775SEmil Constantinescu         na_loc++;
58707453f775SEmil Constantinescu       }
58717453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58727453f775SEmil Constantinescu       if(tolr>0.){
58737453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58747453f775SEmil Constantinescu         nr_loc++;
58757453f775SEmil Constantinescu       }
58767453f775SEmil Constantinescu       tol=tola+tolr;
58777453f775SEmil Constantinescu       if(tol>0.){
58787453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58797453f775SEmil Constantinescu         n_loc++;
58807453f775SEmil Constantinescu       }
58819c6b16b5SShri Abhyankar     }
58829c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58839c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
58849c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
58857453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58867453f775SEmil Constantinescu      tola = ts->atol;
58877453f775SEmil Constantinescu       if(tola>0.){
58887453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58897453f775SEmil Constantinescu         na_loc++;
58907453f775SEmil Constantinescu       }
58917453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58927453f775SEmil Constantinescu       if(tolr>0.){
58937453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58947453f775SEmil Constantinescu         nr_loc++;
58957453f775SEmil Constantinescu       }
58967453f775SEmil Constantinescu       tol=tola+tolr;
58977453f775SEmil Constantinescu       if(tol>0.){
58987453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58997453f775SEmil Constantinescu         n_loc++;
59007453f775SEmil Constantinescu       }
59019c6b16b5SShri Abhyankar     }
59029c6b16b5SShri Abhyankar   }
59039c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
59049c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
59059c6b16b5SShri Abhyankar 
59067453f775SEmil Constantinescu   err_loc[0] = sum;
59077453f775SEmil Constantinescu   err_loc[1] = suma;
59087453f775SEmil Constantinescu   err_loc[2] = sumr;
59097453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
59107453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
59117453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
59127453f775SEmil Constantinescu 
5913a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
59147453f775SEmil Constantinescu 
59157453f775SEmil Constantinescu   gsum   = err_glb[0];
59167453f775SEmil Constantinescu   gsuma  = err_glb[1];
59177453f775SEmil Constantinescu   gsumr  = err_glb[2];
59187453f775SEmil Constantinescu   n_glb  = err_glb[3];
59197453f775SEmil Constantinescu   na_glb = err_glb[4];
59207453f775SEmil Constantinescu   nr_glb = err_glb[5];
59217453f775SEmil Constantinescu 
5922b1316ef9SEmil Constantinescu   *norm  = 0.;
5923b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5924b1316ef9SEmil Constantinescu   *norma = 0.;
5925b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5926b1316ef9SEmil Constantinescu   *normr = 0.;
5927b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
59289c6b16b5SShri Abhyankar 
59299c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
59307453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
59317453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
59329c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
59339c6b16b5SShri Abhyankar }
59349c6b16b5SShri Abhyankar 
59359c6b16b5SShri Abhyankar /*@
5936a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
59379c6b16b5SShri Abhyankar 
59389c6b16b5SShri Abhyankar    Collective on TS
59399c6b16b5SShri Abhyankar 
59409c6b16b5SShri Abhyankar    Input Arguments:
59419c6b16b5SShri Abhyankar +  ts - time stepping context
5942a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5943a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
59449c6b16b5SShri Abhyankar 
59459c6b16b5SShri Abhyankar    Output Arguments:
59467453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
59477453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
59487453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
59499c6b16b5SShri Abhyankar 
59509c6b16b5SShri Abhyankar    Level: developer
59519c6b16b5SShri Abhyankar 
5952deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
59539c6b16b5SShri Abhyankar @*/
59547453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
59559c6b16b5SShri Abhyankar {
59569c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
59577453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
59589c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
59597453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
59607453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
59617453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
59629c6b16b5SShri Abhyankar 
59639c6b16b5SShri Abhyankar   PetscFunctionBegin;
59649c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5965a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5966a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5967a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5968a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5969a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5970a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
59718a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
59728a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5973a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
59749c6b16b5SShri Abhyankar 
59759c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
59769c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
59779c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
59789c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59799c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59807453f775SEmil Constantinescu 
59817453f775SEmil Constantinescu   max=0.;
59827453f775SEmil Constantinescu   maxa=0.;
59837453f775SEmil Constantinescu   maxr=0.;
59847453f775SEmil Constantinescu 
59857453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
59869c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
59879c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59889c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59897453f775SEmil Constantinescu 
59907453f775SEmil Constantinescu     for (i=0; i<n; i++) {
59917453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59927453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59937453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59947453f775SEmil Constantinescu       tol  = tola+tolr;
59957453f775SEmil Constantinescu       if(tola>0.){
59967453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59977453f775SEmil Constantinescu       }
59987453f775SEmil Constantinescu       if(tolr>0.){
59997453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60007453f775SEmil Constantinescu       }
60017453f775SEmil Constantinescu       if(tol>0.){
60027453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60037453f775SEmil Constantinescu       }
60049c6b16b5SShri Abhyankar     }
60059c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60069c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60079c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
60089c6b16b5SShri Abhyankar     const PetscScalar *atol;
60099c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60107453f775SEmil Constantinescu     for (i=0; i<n; i++) {
60117453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60127453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
60137453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60147453f775SEmil Constantinescu       tol  = tola+tolr;
60157453f775SEmil Constantinescu       if(tola>0.){
60167453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60177453f775SEmil Constantinescu       }
60187453f775SEmil Constantinescu       if(tolr>0.){
60197453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60207453f775SEmil Constantinescu       }
60217453f775SEmil Constantinescu       if(tol>0.){
60227453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60237453f775SEmil Constantinescu       }
60249c6b16b5SShri Abhyankar     }
60259c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60269c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
60279c6b16b5SShri Abhyankar     const PetscScalar *rtol;
60289c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60297453f775SEmil Constantinescu 
60307453f775SEmil Constantinescu     for (i=0; i<n; i++) {
60317453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60327453f775SEmil Constantinescu       tola = ts->atol;
60337453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60347453f775SEmil Constantinescu       tol  = tola+tolr;
60357453f775SEmil Constantinescu       if(tola>0.){
60367453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60377453f775SEmil Constantinescu       }
60387453f775SEmil Constantinescu       if(tolr>0.){
60397453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60407453f775SEmil Constantinescu       }
60417453f775SEmil Constantinescu       if(tol>0.){
60427453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60437453f775SEmil Constantinescu       }
60449c6b16b5SShri Abhyankar     }
60459c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60469c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
60477453f775SEmil Constantinescu 
60487453f775SEmil Constantinescu     for (i=0; i<n; i++) {
60497453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60507453f775SEmil Constantinescu       tola = ts->atol;
60517453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60527453f775SEmil Constantinescu       tol  = tola+tolr;
60537453f775SEmil Constantinescu       if(tola>0.){
60547453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60557453f775SEmil Constantinescu       }
60567453f775SEmil Constantinescu       if(tolr>0.){
60577453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60587453f775SEmil Constantinescu       }
60597453f775SEmil Constantinescu       if(tol>0.){
60607453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60617453f775SEmil Constantinescu       }
60629c6b16b5SShri Abhyankar     }
60639c6b16b5SShri Abhyankar   }
60649c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60659c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60667453f775SEmil Constantinescu   err_loc[0] = max;
60677453f775SEmil Constantinescu   err_loc[1] = maxa;
60687453f775SEmil Constantinescu   err_loc[2] = maxr;
6069a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60707453f775SEmil Constantinescu   gmax   = err_glb[0];
60717453f775SEmil Constantinescu   gmaxa  = err_glb[1];
60727453f775SEmil Constantinescu   gmaxr  = err_glb[2];
60739c6b16b5SShri Abhyankar 
60749c6b16b5SShri Abhyankar   *norm = gmax;
60757453f775SEmil Constantinescu   *norma = gmaxa;
60767453f775SEmil Constantinescu   *normr = gmaxr;
60779c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60787453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60797453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60809c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
60819c6b16b5SShri Abhyankar }
60829c6b16b5SShri Abhyankar 
60831c3436cfSJed Brown /*@
60848a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
60851c3436cfSJed Brown 
60861c3436cfSJed Brown    Collective on TS
60871c3436cfSJed Brown 
60881c3436cfSJed Brown    Input Arguments:
60891c3436cfSJed Brown +  ts - time stepping context
6090a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6091a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6092a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
60937619abb3SShri 
60941c3436cfSJed Brown    Output Arguments:
60958a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
60968a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
60978a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6098a4868fbcSLisandro Dalcin 
6099a4868fbcSLisandro Dalcin    Options Database Keys:
6100a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6101a4868fbcSLisandro Dalcin 
61021c3436cfSJed Brown    Level: developer
61031c3436cfSJed Brown 
61048a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
61051c3436cfSJed Brown @*/
61067453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61071c3436cfSJed Brown {
61088beabaa1SBarry Smith   PetscErrorCode ierr;
61091c3436cfSJed Brown 
61101c3436cfSJed Brown   PetscFunctionBegin;
6111a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
61127453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6113a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
61147453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6115a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
61161c3436cfSJed Brown   PetscFunctionReturn(0);
61171c3436cfSJed Brown }
61181c3436cfSJed Brown 
61198a175baeSEmil Constantinescu 
61208a175baeSEmil Constantinescu /*@
61218a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
61228a175baeSEmil Constantinescu 
61238a175baeSEmil Constantinescu    Collective on TS
61248a175baeSEmil Constantinescu 
61258a175baeSEmil Constantinescu    Input Arguments:
61268a175baeSEmil Constantinescu +  ts - time stepping context
61278a175baeSEmil Constantinescu .  E - error vector
61288a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
61298a175baeSEmil Constantinescu -  Y - state vector, previous time step
61308a175baeSEmil Constantinescu 
61318a175baeSEmil Constantinescu    Output Arguments:
61328a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
61338a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
61348a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
61358a175baeSEmil Constantinescu 
61368a175baeSEmil Constantinescu    Level: developer
61378a175baeSEmil Constantinescu 
61388a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
61398a175baeSEmil Constantinescu @*/
61408a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61418a175baeSEmil Constantinescu {
61428a175baeSEmil Constantinescu   PetscErrorCode    ierr;
61438a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
61448a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
61458a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
61468a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
61478a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
61488a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
61498a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
61508a175baeSEmil Constantinescu 
61518a175baeSEmil Constantinescu   PetscFunctionBegin;
61528a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
61538a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
61548a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
61558a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
61568a175baeSEmil Constantinescu   PetscValidType(E,2);
61578a175baeSEmil Constantinescu   PetscValidType(U,3);
61588a175baeSEmil Constantinescu   PetscValidType(Y,4);
61598a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
61608a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
61618a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
61628a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
61638a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
61648a175baeSEmil Constantinescu 
61658a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
61668a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
61678a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
61688a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
61698a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
61708a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
61718a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
61728a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
61738a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
61748a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
61758a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
61768a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61778a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61788a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
61798a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61808a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61818a175baeSEmil Constantinescu       if(tola>0.){
61828a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61838a175baeSEmil Constantinescu         na_loc++;
61848a175baeSEmil Constantinescu       }
61858a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61868a175baeSEmil Constantinescu       if(tolr>0.){
61878a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61888a175baeSEmil Constantinescu         nr_loc++;
61898a175baeSEmil Constantinescu       }
61908a175baeSEmil Constantinescu       tol=tola+tolr;
61918a175baeSEmil Constantinescu       if(tol>0.){
61928a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61938a175baeSEmil Constantinescu         n_loc++;
61948a175baeSEmil Constantinescu       }
61958a175baeSEmil Constantinescu     }
61968a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61978a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61988a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
61998a175baeSEmil Constantinescu     const PetscScalar *atol;
62008a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62018a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
62028a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62038a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
62048a175baeSEmil Constantinescu       if(tola>0.){
62058a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62068a175baeSEmil Constantinescu         na_loc++;
62078a175baeSEmil Constantinescu       }
62088a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62098a175baeSEmil Constantinescu       if(tolr>0.){
62108a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62118a175baeSEmil Constantinescu         nr_loc++;
62128a175baeSEmil Constantinescu       }
62138a175baeSEmil Constantinescu       tol=tola+tolr;
62148a175baeSEmil Constantinescu       if(tol>0.){
62158a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62168a175baeSEmil Constantinescu         n_loc++;
62178a175baeSEmil Constantinescu       }
62188a175baeSEmil Constantinescu     }
62198a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62208a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
62218a175baeSEmil Constantinescu     const PetscScalar *rtol;
62228a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62238a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
62248a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62258a175baeSEmil Constantinescu       tola = ts->atol;
62268a175baeSEmil Constantinescu       if(tola>0.){
62278a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62288a175baeSEmil Constantinescu         na_loc++;
62298a175baeSEmil Constantinescu       }
62308a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62318a175baeSEmil Constantinescu       if(tolr>0.){
62328a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62338a175baeSEmil Constantinescu         nr_loc++;
62348a175baeSEmil Constantinescu       }
62358a175baeSEmil Constantinescu       tol=tola+tolr;
62368a175baeSEmil Constantinescu       if(tol>0.){
62378a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62388a175baeSEmil Constantinescu         n_loc++;
62398a175baeSEmil Constantinescu       }
62408a175baeSEmil Constantinescu     }
62418a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62428a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
62438a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
62448a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62458a175baeSEmil Constantinescu      tola = ts->atol;
62468a175baeSEmil Constantinescu       if(tola>0.){
62478a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62488a175baeSEmil Constantinescu         na_loc++;
62498a175baeSEmil Constantinescu       }
62508a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62518a175baeSEmil Constantinescu       if(tolr>0.){
62528a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62538a175baeSEmil Constantinescu         nr_loc++;
62548a175baeSEmil Constantinescu       }
62558a175baeSEmil Constantinescu       tol=tola+tolr;
62568a175baeSEmil Constantinescu       if(tol>0.){
62578a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62588a175baeSEmil Constantinescu         n_loc++;
62598a175baeSEmil Constantinescu       }
62608a175baeSEmil Constantinescu     }
62618a175baeSEmil Constantinescu   }
62628a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
62638a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
62648a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
62658a175baeSEmil Constantinescu 
62668a175baeSEmil Constantinescu   err_loc[0] = sum;
62678a175baeSEmil Constantinescu   err_loc[1] = suma;
62688a175baeSEmil Constantinescu   err_loc[2] = sumr;
62698a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
62708a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
62718a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
62728a175baeSEmil Constantinescu 
6273a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62748a175baeSEmil Constantinescu 
62758a175baeSEmil Constantinescu   gsum   = err_glb[0];
62768a175baeSEmil Constantinescu   gsuma  = err_glb[1];
62778a175baeSEmil Constantinescu   gsumr  = err_glb[2];
62788a175baeSEmil Constantinescu   n_glb  = err_glb[3];
62798a175baeSEmil Constantinescu   na_glb = err_glb[4];
62808a175baeSEmil Constantinescu   nr_glb = err_glb[5];
62818a175baeSEmil Constantinescu 
62828a175baeSEmil Constantinescu   *norm  = 0.;
62838a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
62848a175baeSEmil Constantinescu   *norma = 0.;
62858a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
62868a175baeSEmil Constantinescu   *normr = 0.;
62878a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
62888a175baeSEmil Constantinescu 
62898a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
62908a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
62918a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
62928a175baeSEmil Constantinescu   PetscFunctionReturn(0);
62938a175baeSEmil Constantinescu }
62948a175baeSEmil Constantinescu 
62958a175baeSEmil Constantinescu /*@
62968a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
62978a175baeSEmil Constantinescu    Collective on TS
62988a175baeSEmil Constantinescu 
62998a175baeSEmil Constantinescu    Input Arguments:
63008a175baeSEmil Constantinescu +  ts - time stepping context
63018a175baeSEmil Constantinescu .  E - error vector
63028a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
63038a175baeSEmil Constantinescu -  Y - state vector, previous time step
63048a175baeSEmil Constantinescu 
63058a175baeSEmil Constantinescu    Output Arguments:
63068a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
63078a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
63088a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
63098a175baeSEmil Constantinescu 
63108a175baeSEmil Constantinescu    Level: developer
63118a175baeSEmil Constantinescu 
63128a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
63138a175baeSEmil Constantinescu @*/
63148a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
63158a175baeSEmil Constantinescu {
63168a175baeSEmil Constantinescu   PetscErrorCode    ierr;
63178a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
63188a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
63198a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
63208a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
63218a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
63228a175baeSEmil Constantinescu 
63238a175baeSEmil Constantinescu   PetscFunctionBegin;
63248a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63258a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
63268a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
63278a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
63288a175baeSEmil Constantinescu   PetscValidType(E,2);
63298a175baeSEmil Constantinescu   PetscValidType(U,3);
63308a175baeSEmil Constantinescu   PetscValidType(Y,4);
63318a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
63328a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
63338a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
63348a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
63358a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
63368a175baeSEmil Constantinescu 
63378a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
63388a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
63398a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
63408a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
63418a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
63428a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
63438a175baeSEmil Constantinescu 
63448a175baeSEmil Constantinescu   max=0.;
63458a175baeSEmil Constantinescu   maxa=0.;
63468a175baeSEmil Constantinescu   maxr=0.;
63478a175baeSEmil Constantinescu 
63488a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
63498a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
63508a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63518a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63528a175baeSEmil Constantinescu 
63538a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
63548a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63558a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63568a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63578a175baeSEmil Constantinescu       tol  = tola+tolr;
63588a175baeSEmil Constantinescu       if(tola>0.){
63598a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63608a175baeSEmil Constantinescu       }
63618a175baeSEmil Constantinescu       if(tolr>0.){
63628a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63638a175baeSEmil Constantinescu       }
63648a175baeSEmil Constantinescu       if(tol>0.){
63658a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63668a175baeSEmil Constantinescu       }
63678a175baeSEmil Constantinescu     }
63688a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63698a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63708a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
63718a175baeSEmil Constantinescu     const PetscScalar *atol;
63728a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63738a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
63748a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63758a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63768a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63778a175baeSEmil Constantinescu       tol  = tola+tolr;
63788a175baeSEmil Constantinescu       if(tola>0.){
63798a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63808a175baeSEmil Constantinescu       }
63818a175baeSEmil Constantinescu       if(tolr>0.){
63828a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63838a175baeSEmil Constantinescu       }
63848a175baeSEmil Constantinescu       if(tol>0.){
63858a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63868a175baeSEmil Constantinescu       }
63878a175baeSEmil Constantinescu     }
63888a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63898a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
63908a175baeSEmil Constantinescu     const PetscScalar *rtol;
63918a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63928a175baeSEmil Constantinescu 
63938a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
63948a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63958a175baeSEmil Constantinescu       tola = ts->atol;
63968a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63978a175baeSEmil Constantinescu       tol  = tola+tolr;
63988a175baeSEmil Constantinescu       if(tola>0.){
63998a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64008a175baeSEmil Constantinescu       }
64018a175baeSEmil Constantinescu       if(tolr>0.){
64028a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64038a175baeSEmil Constantinescu       }
64048a175baeSEmil Constantinescu       if(tol>0.){
64058a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64068a175baeSEmil Constantinescu       }
64078a175baeSEmil Constantinescu     }
64088a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64098a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
64108a175baeSEmil Constantinescu 
64118a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64128a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64138a175baeSEmil Constantinescu       tola = ts->atol;
64148a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64158a175baeSEmil Constantinescu       tol  = tola+tolr;
64168a175baeSEmil Constantinescu       if(tola>0.){
64178a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64188a175baeSEmil Constantinescu       }
64198a175baeSEmil Constantinescu       if(tolr>0.){
64208a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64218a175baeSEmil Constantinescu       }
64228a175baeSEmil Constantinescu       if(tol>0.){
64238a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64248a175baeSEmil Constantinescu       }
64258a175baeSEmil Constantinescu     }
64268a175baeSEmil Constantinescu   }
64278a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
64288a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
64298a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
64308a175baeSEmil Constantinescu   err_loc[0] = max;
64318a175baeSEmil Constantinescu   err_loc[1] = maxa;
64328a175baeSEmil Constantinescu   err_loc[2] = maxr;
6433a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
64348a175baeSEmil Constantinescu   gmax   = err_glb[0];
64358a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
64368a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
64378a175baeSEmil Constantinescu 
64388a175baeSEmil Constantinescu   *norm = gmax;
64398a175baeSEmil Constantinescu   *norma = gmaxa;
64408a175baeSEmil Constantinescu   *normr = gmaxr;
64418a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
64428a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
64438a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
64448a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64458a175baeSEmil Constantinescu }
64468a175baeSEmil Constantinescu 
64478a175baeSEmil Constantinescu /*@
64488a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
64498a175baeSEmil Constantinescu 
64508a175baeSEmil Constantinescu    Collective on TS
64518a175baeSEmil Constantinescu 
64528a175baeSEmil Constantinescu    Input Arguments:
64538a175baeSEmil Constantinescu +  ts - time stepping context
64548a175baeSEmil Constantinescu .  E - error vector
64558a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
64568a175baeSEmil Constantinescu .  Y - state vector, previous time step
64578a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
64588a175baeSEmil Constantinescu 
64598a175baeSEmil Constantinescu    Output Arguments:
64608a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
64618a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
64628a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
64638a175baeSEmil Constantinescu 
64648a175baeSEmil Constantinescu    Options Database Keys:
64658a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
64668a175baeSEmil Constantinescu 
64678a175baeSEmil Constantinescu    Level: developer
64688a175baeSEmil Constantinescu 
64698a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
64708a175baeSEmil Constantinescu @*/
64718a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
64728a175baeSEmil Constantinescu {
64738a175baeSEmil Constantinescu   PetscErrorCode ierr;
64748a175baeSEmil Constantinescu 
64758a175baeSEmil Constantinescu   PetscFunctionBegin;
64768a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
64778a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64788a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
64798a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64808a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
64818a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64828a175baeSEmil Constantinescu }
64838a175baeSEmil Constantinescu 
64848a175baeSEmil Constantinescu 
64858d59e960SJed Brown /*@
64868d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
64878d59e960SJed Brown 
64888d59e960SJed Brown    Logically Collective on TS
64898d59e960SJed Brown 
64908d59e960SJed Brown    Input Arguments:
64918d59e960SJed Brown +  ts - time stepping context
64928d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
64938d59e960SJed Brown 
64948d59e960SJed Brown    Note:
64958d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
64968d59e960SJed Brown 
64978d59e960SJed Brown    Level: intermediate
64988d59e960SJed Brown 
64998d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
65008d59e960SJed Brown @*/
65018d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
65028d59e960SJed Brown {
65038d59e960SJed Brown   PetscFunctionBegin;
65048d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
65058d59e960SJed Brown   ts->cfltime_local = cfltime;
65068d59e960SJed Brown   ts->cfltime       = -1.;
65078d59e960SJed Brown   PetscFunctionReturn(0);
65088d59e960SJed Brown }
65098d59e960SJed Brown 
65108d59e960SJed Brown /*@
65118d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
65128d59e960SJed Brown 
65138d59e960SJed Brown    Collective on TS
65148d59e960SJed Brown 
65158d59e960SJed Brown    Input Arguments:
65168d59e960SJed Brown .  ts - time stepping context
65178d59e960SJed Brown 
65188d59e960SJed Brown    Output Arguments:
65198d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
65208d59e960SJed Brown 
65218d59e960SJed Brown    Level: advanced
65228d59e960SJed Brown 
65238d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
65248d59e960SJed Brown @*/
65258d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
65268d59e960SJed Brown {
65278d59e960SJed Brown   PetscErrorCode ierr;
65288d59e960SJed Brown 
65298d59e960SJed Brown   PetscFunctionBegin;
65308d59e960SJed Brown   if (ts->cfltime < 0) {
6531b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
65328d59e960SJed Brown   }
65338d59e960SJed Brown   *cfltime = ts->cfltime;
65348d59e960SJed Brown   PetscFunctionReturn(0);
65358d59e960SJed Brown }
65368d59e960SJed Brown 
6537d6ebe24aSShri Abhyankar /*@
6538d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6539d6ebe24aSShri Abhyankar 
6540d6ebe24aSShri Abhyankar    Input Parameters:
6541d6ebe24aSShri Abhyankar .  ts   - the TS context.
6542d6ebe24aSShri Abhyankar .  xl   - lower bound.
6543d6ebe24aSShri Abhyankar .  xu   - upper bound.
6544d6ebe24aSShri Abhyankar 
6545d6ebe24aSShri Abhyankar    Notes:
6546d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6547e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6548d6ebe24aSShri Abhyankar 
65492bd2b0e6SSatish Balay    Level: advanced
65502bd2b0e6SSatish Balay 
6551d6ebe24aSShri Abhyankar @*/
6552d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6553d6ebe24aSShri Abhyankar {
6554d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6555d6ebe24aSShri Abhyankar   SNES           snes;
6556d6ebe24aSShri Abhyankar 
6557d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6558d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6559d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6560d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6561d6ebe24aSShri Abhyankar }
6562d6ebe24aSShri Abhyankar 
6563325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6564c6db04a5SJed Brown #include <mex.h>
6565325fc9f4SBarry Smith 
6566325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6567325fc9f4SBarry Smith 
6568325fc9f4SBarry Smith /*
6569325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6570325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6571325fc9f4SBarry Smith 
6572325fc9f4SBarry Smith    Collective on TS
6573325fc9f4SBarry Smith 
6574325fc9f4SBarry Smith    Input Parameters:
6575325fc9f4SBarry Smith +  snes - the TS context
65760910c330SBarry Smith -  u - input vector
6577325fc9f4SBarry Smith 
6578325fc9f4SBarry Smith    Output Parameter:
6579325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6580325fc9f4SBarry Smith 
6581325fc9f4SBarry Smith    Notes:
6582325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6583325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6584325fc9f4SBarry Smith    themselves.
6585325fc9f4SBarry Smith 
6586325fc9f4SBarry Smith    Level: developer
6587325fc9f4SBarry Smith 
6588325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6589325fc9f4SBarry Smith 
6590325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6591325fc9f4SBarry Smith */
65920910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6593325fc9f4SBarry Smith {
6594325fc9f4SBarry Smith   PetscErrorCode  ierr;
6595325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6596325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6597325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6598325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6599325fc9f4SBarry Smith 
6600325fc9f4SBarry Smith   PetscFunctionBegin;
6601325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
66020910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
66030910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6604325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
66050910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6606325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6607325fc9f4SBarry Smith 
6608325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
66090910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
66100910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
66110910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6612bbd56ea5SKarl Rupp 
6613325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6614325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6615325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6616325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6617325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6618325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6619325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6620325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6621325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6622325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6623325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6624325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6625325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6626325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6627325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6628325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6629325fc9f4SBarry Smith   PetscFunctionReturn(0);
6630325fc9f4SBarry Smith }
6631325fc9f4SBarry Smith 
6632325fc9f4SBarry Smith 
6633325fc9f4SBarry Smith /*
6634325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6635325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6636e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6637325fc9f4SBarry Smith 
6638325fc9f4SBarry Smith    Logically Collective on TS
6639325fc9f4SBarry Smith 
6640325fc9f4SBarry Smith    Input Parameters:
6641325fc9f4SBarry Smith +  ts - the TS context
6642325fc9f4SBarry Smith -  func - function evaluation routine
6643325fc9f4SBarry Smith 
6644325fc9f4SBarry Smith    Calling sequence of func:
66450910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6646325fc9f4SBarry Smith 
6647325fc9f4SBarry Smith    Level: beginner
6648325fc9f4SBarry Smith 
6649325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6650325fc9f4SBarry Smith 
6651325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6652325fc9f4SBarry Smith */
6653cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6654325fc9f4SBarry Smith {
6655325fc9f4SBarry Smith   PetscErrorCode  ierr;
6656325fc9f4SBarry Smith   TSMatlabContext *sctx;
6657325fc9f4SBarry Smith 
6658325fc9f4SBarry Smith   PetscFunctionBegin;
6659325fc9f4SBarry Smith   /* currently sctx is memory bleed */
666095dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6661325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6662325fc9f4SBarry Smith   /*
6663325fc9f4SBarry Smith      This should work, but it doesn't
6664325fc9f4SBarry Smith   sctx->ctx = ctx;
6665325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6666325fc9f4SBarry Smith   */
6667325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6668bbd56ea5SKarl Rupp 
66690298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6670325fc9f4SBarry Smith   PetscFunctionReturn(0);
6671325fc9f4SBarry Smith }
6672325fc9f4SBarry Smith 
6673325fc9f4SBarry Smith /*
6674325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6675325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6676325fc9f4SBarry Smith 
6677325fc9f4SBarry Smith    Collective on TS
6678325fc9f4SBarry Smith 
6679325fc9f4SBarry Smith    Input Parameters:
6680cdcf91faSSean Farley +  ts - the TS context
66810910c330SBarry Smith .  u - input vector
6682325fc9f4SBarry Smith .  A, B - the matrices
6683325fc9f4SBarry Smith -  ctx - user context
6684325fc9f4SBarry Smith 
6685325fc9f4SBarry Smith    Level: developer
6686325fc9f4SBarry Smith 
6687325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6688325fc9f4SBarry Smith 
6689325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6690325fc9f4SBarry Smith @*/
6691f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6692325fc9f4SBarry Smith {
6693325fc9f4SBarry Smith   PetscErrorCode  ierr;
6694325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6695325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6696325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6697325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6698325fc9f4SBarry Smith 
6699325fc9f4SBarry Smith   PetscFunctionBegin;
6700cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
67010910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6702325fc9f4SBarry Smith 
67030910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6704325fc9f4SBarry Smith 
6705cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
67060910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
67070910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
67080910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
67090910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6710bbd56ea5SKarl Rupp 
6711325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6712325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6713325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6714325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6715325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6716325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6717325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6718325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6719325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6720325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6721325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6722325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6723325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6724325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6725325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6726325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6727325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6728325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6729325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6730325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6731325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6732325fc9f4SBarry Smith   PetscFunctionReturn(0);
6733325fc9f4SBarry Smith }
6734325fc9f4SBarry Smith 
6735325fc9f4SBarry Smith 
6736325fc9f4SBarry Smith /*
6737325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6738e3c5b3baSBarry 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
6739325fc9f4SBarry Smith 
6740325fc9f4SBarry Smith    Logically Collective on TS
6741325fc9f4SBarry Smith 
6742325fc9f4SBarry Smith    Input Parameters:
6743cdcf91faSSean Farley +  ts - the TS context
6744325fc9f4SBarry Smith .  A,B - Jacobian matrices
6745325fc9f4SBarry Smith .  func - function evaluation routine
6746325fc9f4SBarry Smith -  ctx - user context
6747325fc9f4SBarry Smith 
6748325fc9f4SBarry Smith    Calling sequence of func:
67490910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6750325fc9f4SBarry Smith 
6751325fc9f4SBarry Smith 
6752325fc9f4SBarry Smith    Level: developer
6753325fc9f4SBarry Smith 
6754325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6755325fc9f4SBarry Smith 
6756325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6757325fc9f4SBarry Smith */
6758cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6759325fc9f4SBarry Smith {
6760325fc9f4SBarry Smith   PetscErrorCode  ierr;
6761325fc9f4SBarry Smith   TSMatlabContext *sctx;
6762325fc9f4SBarry Smith 
6763325fc9f4SBarry Smith   PetscFunctionBegin;
6764325fc9f4SBarry Smith   /* currently sctx is memory bleed */
676595dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6766325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6767325fc9f4SBarry Smith   /*
6768325fc9f4SBarry Smith      This should work, but it doesn't
6769325fc9f4SBarry Smith   sctx->ctx = ctx;
6770325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6771325fc9f4SBarry Smith   */
6772325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6773bbd56ea5SKarl Rupp 
6774cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
6775325fc9f4SBarry Smith   PetscFunctionReturn(0);
6776325fc9f4SBarry Smith }
6777325fc9f4SBarry Smith 
6778b5b1a830SBarry Smith /*
6779b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
6780b5b1a830SBarry Smith 
6781b5b1a830SBarry Smith    Collective on TS
6782b5b1a830SBarry Smith 
6783b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6784b5b1a830SBarry Smith @*/
67850910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6786b5b1a830SBarry Smith {
6787b5b1a830SBarry Smith   PetscErrorCode  ierr;
6788b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6789a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
6790b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
6791b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
6792b5b1a830SBarry Smith 
6793b5b1a830SBarry Smith   PetscFunctionBegin;
6794cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
67950910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
6796b5b1a830SBarry Smith 
6797cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
67980910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
6799bbd56ea5SKarl Rupp 
6800b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6801b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
6802b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
6803b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
6804b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
6805b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
6806b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
6807b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6808b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
6809b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
6810b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
6811b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
6812b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
6813b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
6814b5b1a830SBarry Smith   PetscFunctionReturn(0);
6815b5b1a830SBarry Smith }
6816b5b1a830SBarry Smith 
6817b5b1a830SBarry Smith 
6818b5b1a830SBarry Smith /*
6819b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
6820b5b1a830SBarry Smith 
6821b5b1a830SBarry Smith    Level: developer
6822b5b1a830SBarry Smith 
6823b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
6824b5b1a830SBarry Smith 
6825b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6826b5b1a830SBarry Smith */
6827cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6828b5b1a830SBarry Smith {
6829b5b1a830SBarry Smith   PetscErrorCode  ierr;
6830b5b1a830SBarry Smith   TSMatlabContext *sctx;
6831b5b1a830SBarry Smith 
6832b5b1a830SBarry Smith   PetscFunctionBegin;
6833b5b1a830SBarry Smith   /* currently sctx is memory bleed */
683495dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6835b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6836b5b1a830SBarry Smith   /*
6837b5b1a830SBarry Smith      This should work, but it doesn't
6838b5b1a830SBarry Smith   sctx->ctx = ctx;
6839b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6840b5b1a830SBarry Smith   */
6841b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6842bbd56ea5SKarl Rupp 
68430298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6844b5b1a830SBarry Smith   PetscFunctionReturn(0);
6845b5b1a830SBarry Smith }
6846325fc9f4SBarry Smith #endif
6847b3603a34SBarry Smith 
6848b3603a34SBarry Smith /*@C
68494f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6850b3603a34SBarry Smith        in a time based line graph
6851b3603a34SBarry Smith 
6852b3603a34SBarry Smith    Collective on TS
6853b3603a34SBarry Smith 
6854b3603a34SBarry Smith    Input Parameters:
6855b3603a34SBarry Smith +  ts - the TS context
6856b3603a34SBarry Smith .  step - current time-step
6857b3603a34SBarry Smith .  ptime - current time
68587db568b7SBarry Smith .  u - current solution
68597db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6860b3603a34SBarry Smith 
6861b3d3934dSBarry Smith    Options Database:
68629ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6863b3d3934dSBarry Smith 
6864b3603a34SBarry Smith    Level: intermediate
6865b3603a34SBarry Smith 
68666934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component solutions in a separate window
6867b3603a34SBarry Smith 
6868b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6869b3603a34SBarry Smith 
68707db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
68717db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
68727db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
68737db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6874b3603a34SBarry Smith @*/
68757db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6876b3603a34SBarry Smith {
6877b3603a34SBarry Smith   PetscErrorCode    ierr;
68787db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6879b3603a34SBarry Smith   const PetscScalar *yy;
688080666b62SBarry Smith   Vec               v;
6881b3603a34SBarry Smith 
6882b3603a34SBarry Smith   PetscFunctionBegin;
688363e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
688458ff32f7SBarry Smith   if (!step) {
6885a9f9c1f6SBarry Smith     PetscDrawAxis axis;
68866934998bSLisandro Dalcin     PetscInt      dim;
6887a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6888a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6889bab0a581SBarry Smith     if (!ctx->names) {
6890bab0a581SBarry Smith       PetscBool flg;
6891bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6892bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6893bab0a581SBarry Smith       if (flg) {
6894bab0a581SBarry Smith         PetscInt i,n;
6895bab0a581SBarry Smith         char     **names;
6896bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6897bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6898bab0a581SBarry Smith         for (i=0; i<n; i++) {
6899bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6900bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6901bab0a581SBarry Smith         }
6902bab0a581SBarry Smith         names[n] = NULL;
6903bab0a581SBarry Smith         ctx->names = names;
6904bab0a581SBarry Smith       }
6905bab0a581SBarry Smith     }
6906387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6907387f4636SBarry Smith       char      **displaynames;
6908387f4636SBarry Smith       PetscBool flg;
6909387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
691095dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6911387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6912c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6913387f4636SBarry Smith       if (flg) {
6914a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6915387f4636SBarry Smith       }
6916387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6917387f4636SBarry Smith     }
6918387f4636SBarry Smith     if (ctx->displaynames) {
6919387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6920387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6921387f4636SBarry Smith     } else if (ctx->names) {
69220910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
69230b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6924387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6925b0bc92c6SBarry Smith     } else {
6926b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6927b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6928387f4636SBarry Smith     }
69290b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
693058ff32f7SBarry Smith   }
69316934998bSLisandro Dalcin 
69326934998bSLisandro Dalcin   if (!ctx->transform) v = u;
69336934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
693480666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
69356934998bSLisandro Dalcin   if (ctx->displaynames) {
69366934998bSLisandro Dalcin     PetscInt i;
69376934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
69386934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
69396934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
69406934998bSLisandro Dalcin   } else {
6941e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6942e3efe391SJed Brown     PetscInt  i,n;
69436934998bSLisandro Dalcin     PetscReal *yreal;
694480666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6945785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6946e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
69470b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6948e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6949e3efe391SJed Brown #else
69500b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6951e3efe391SJed Brown #endif
695280666b62SBarry Smith   }
69536934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
69546934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
69556934998bSLisandro Dalcin 
6956b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69570b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69586934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
69593923b477SBarry Smith   }
6960b3603a34SBarry Smith   PetscFunctionReturn(0);
6961b3603a34SBarry Smith }
6962b3603a34SBarry Smith 
6963387f4636SBarry Smith 
6964b037adc7SBarry Smith /*@C
696531152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6966b037adc7SBarry Smith 
6967b037adc7SBarry Smith    Collective on TS
6968b037adc7SBarry Smith 
6969b037adc7SBarry Smith    Input Parameters:
6970b037adc7SBarry Smith +  ts - the TS context
6971b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6972b037adc7SBarry Smith 
6973b037adc7SBarry Smith    Level: intermediate
6974b037adc7SBarry Smith 
69757db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
69767db568b7SBarry Smith 
6977b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
6978b037adc7SBarry Smith 
6979a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6980b037adc7SBarry Smith @*/
698131152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6982b037adc7SBarry Smith {
6983b037adc7SBarry Smith   PetscErrorCode    ierr;
6984b037adc7SBarry Smith   PetscInt          i;
6985b037adc7SBarry Smith 
6986b037adc7SBarry Smith   PetscFunctionBegin;
6987b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6988b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
69895537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6990b3d3934dSBarry Smith       break;
6991b3d3934dSBarry Smith     }
6992b3d3934dSBarry Smith   }
6993b3d3934dSBarry Smith   PetscFunctionReturn(0);
6994b3d3934dSBarry Smith }
6995b3d3934dSBarry Smith 
6996e673d494SBarry Smith /*@C
6997e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6998e673d494SBarry Smith 
6999e673d494SBarry Smith    Collective on TS
7000e673d494SBarry Smith 
7001e673d494SBarry Smith    Input Parameters:
7002e673d494SBarry Smith +  ts - the TS context
7003e673d494SBarry Smith -  names - the names of the components, final string must be NULL
7004e673d494SBarry Smith 
7005e673d494SBarry Smith    Level: intermediate
7006e673d494SBarry Smith 
7007e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7008e673d494SBarry Smith 
7009a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
7010e673d494SBarry Smith @*/
7011e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
7012e673d494SBarry Smith {
7013e673d494SBarry Smith   PetscErrorCode    ierr;
7014e673d494SBarry Smith 
7015e673d494SBarry Smith   PetscFunctionBegin;
7016e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
7017e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
7018e673d494SBarry Smith   PetscFunctionReturn(0);
7019e673d494SBarry Smith }
7020e673d494SBarry Smith 
7021b3d3934dSBarry Smith /*@C
7022b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
7023b3d3934dSBarry Smith 
7024b3d3934dSBarry Smith    Collective on TS
7025b3d3934dSBarry Smith 
7026b3d3934dSBarry Smith    Input Parameter:
7027b3d3934dSBarry Smith .  ts - the TS context
7028b3d3934dSBarry Smith 
7029b3d3934dSBarry Smith    Output Parameter:
7030b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
7031b3d3934dSBarry Smith 
7032b3d3934dSBarry Smith    Level: intermediate
7033b3d3934dSBarry Smith 
70347db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
70357db568b7SBarry Smith 
7036b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7037b3d3934dSBarry Smith 
7038b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7039b3d3934dSBarry Smith @*/
7040b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
7041b3d3934dSBarry Smith {
7042b3d3934dSBarry Smith   PetscInt       i;
7043b3d3934dSBarry Smith 
7044b3d3934dSBarry Smith   PetscFunctionBegin;
7045b3d3934dSBarry Smith   *names = NULL;
7046b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7047b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
70485537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
7049b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
7050b3d3934dSBarry Smith       break;
7051387f4636SBarry Smith     }
7052387f4636SBarry Smith   }
7053387f4636SBarry Smith   PetscFunctionReturn(0);
7054387f4636SBarry Smith }
7055387f4636SBarry Smith 
7056a66092f1SBarry Smith /*@C
7057a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
7058a66092f1SBarry Smith 
7059a66092f1SBarry Smith    Collective on TS
7060a66092f1SBarry Smith 
7061a66092f1SBarry Smith    Input Parameters:
7062a66092f1SBarry Smith +  ctx - the TSMonitorLG context
7063a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
7064a66092f1SBarry Smith 
7065a66092f1SBarry Smith    Level: intermediate
7066a66092f1SBarry Smith 
7067a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
7068a66092f1SBarry Smith 
7069a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7070a66092f1SBarry Smith @*/
7071a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
7072a66092f1SBarry Smith {
7073a66092f1SBarry Smith   PetscInt          j = 0,k;
7074a66092f1SBarry Smith   PetscErrorCode    ierr;
7075a66092f1SBarry Smith 
7076a66092f1SBarry Smith   PetscFunctionBegin;
7077a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
7078a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
7079a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
7080a66092f1SBarry Smith   while (displaynames[j]) j++;
7081a66092f1SBarry Smith   ctx->ndisplayvariables = j;
7082a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
7083a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
7084a66092f1SBarry Smith   j = 0;
7085a66092f1SBarry Smith   while (displaynames[j]) {
7086a66092f1SBarry Smith     k = 0;
7087a66092f1SBarry Smith     while (ctx->names[k]) {
7088a66092f1SBarry Smith       PetscBool flg;
7089a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
7090a66092f1SBarry Smith       if (flg) {
7091a66092f1SBarry Smith         ctx->displayvariables[j] = k;
7092a66092f1SBarry Smith         break;
7093a66092f1SBarry Smith       }
7094a66092f1SBarry Smith       k++;
7095a66092f1SBarry Smith     }
7096a66092f1SBarry Smith     j++;
7097a66092f1SBarry Smith   }
7098a66092f1SBarry Smith   PetscFunctionReturn(0);
7099a66092f1SBarry Smith }
7100a66092f1SBarry Smith 
7101a66092f1SBarry Smith 
7102387f4636SBarry Smith /*@C
7103387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
7104387f4636SBarry Smith 
7105387f4636SBarry Smith    Collective on TS
7106387f4636SBarry Smith 
7107387f4636SBarry Smith    Input Parameters:
7108387f4636SBarry Smith +  ts - the TS context
7109387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
7110387f4636SBarry Smith 
71117db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
71127db568b7SBarry Smith 
7113387f4636SBarry Smith    Level: intermediate
7114387f4636SBarry Smith 
7115387f4636SBarry Smith .keywords: TS,  vector, monitor, view
7116387f4636SBarry Smith 
7117387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7118387f4636SBarry Smith @*/
7119387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
7120387f4636SBarry Smith {
7121a66092f1SBarry Smith   PetscInt          i;
7122387f4636SBarry Smith   PetscErrorCode    ierr;
7123387f4636SBarry Smith 
7124387f4636SBarry Smith   PetscFunctionBegin;
7125387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7126387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
71275537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
7128b3d3934dSBarry Smith       break;
7129b037adc7SBarry Smith     }
7130b037adc7SBarry Smith   }
7131b037adc7SBarry Smith   PetscFunctionReturn(0);
7132b037adc7SBarry Smith }
7133b037adc7SBarry Smith 
713480666b62SBarry Smith /*@C
713580666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
713680666b62SBarry Smith 
713780666b62SBarry Smith    Collective on TS
713880666b62SBarry Smith 
713980666b62SBarry Smith    Input Parameters:
714080666b62SBarry Smith +  ts - the TS context
714180666b62SBarry Smith .  transform - the transform function
71427684fa3eSBarry Smith .  destroy - function to destroy the optional context
714380666b62SBarry Smith -  ctx - optional context used by transform function
714480666b62SBarry Smith 
71457db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
71467db568b7SBarry Smith 
714780666b62SBarry Smith    Level: intermediate
714880666b62SBarry Smith 
714980666b62SBarry Smith .keywords: TS,  vector, monitor, view
715080666b62SBarry Smith 
7151a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
715280666b62SBarry Smith @*/
71537684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
715480666b62SBarry Smith {
715580666b62SBarry Smith   PetscInt          i;
7156a66092f1SBarry Smith   PetscErrorCode    ierr;
715780666b62SBarry Smith 
715880666b62SBarry Smith   PetscFunctionBegin;
715980666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
716080666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
71615537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
716280666b62SBarry Smith     }
716380666b62SBarry Smith   }
716480666b62SBarry Smith   PetscFunctionReturn(0);
716580666b62SBarry Smith }
716680666b62SBarry Smith 
7167e673d494SBarry Smith /*@C
7168e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
7169e673d494SBarry Smith 
7170e673d494SBarry Smith    Collective on TSLGCtx
7171e673d494SBarry Smith 
7172e673d494SBarry Smith    Input Parameters:
7173e673d494SBarry Smith +  ts - the TS context
7174e673d494SBarry Smith .  transform - the transform function
71757684fa3eSBarry Smith .  destroy - function to destroy the optional context
7176e673d494SBarry Smith -  ctx - optional context used by transform function
7177e673d494SBarry Smith 
7178e673d494SBarry Smith    Level: intermediate
7179e673d494SBarry Smith 
7180e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7181e673d494SBarry Smith 
7182a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
7183e673d494SBarry Smith @*/
71847684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
7185e673d494SBarry Smith {
7186e673d494SBarry Smith   PetscFunctionBegin;
7187e673d494SBarry Smith   ctx->transform    = transform;
71887684fa3eSBarry Smith   ctx->transformdestroy = destroy;
7189e673d494SBarry Smith   ctx->transformctx = tctx;
7190e673d494SBarry Smith   PetscFunctionReturn(0);
7191e673d494SBarry Smith }
7192e673d494SBarry Smith 
7193ef20d060SBarry Smith /*@C
71944f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
7195ef20d060SBarry Smith        in a time based line graph
7196ef20d060SBarry Smith 
7197ef20d060SBarry Smith    Collective on TS
7198ef20d060SBarry Smith 
7199ef20d060SBarry Smith    Input Parameters:
7200ef20d060SBarry Smith +  ts - the TS context
7201ef20d060SBarry Smith .  step - current time-step
7202ef20d060SBarry Smith .  ptime - current time
72037db568b7SBarry Smith .  u - current solution
72047db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
7205ef20d060SBarry Smith 
7206ef20d060SBarry Smith    Level: intermediate
7207ef20d060SBarry Smith 
72086934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component errors in a separate window
7209abd5a294SJed Brown 
7210abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
7211abd5a294SJed Brown 
7212abd5a294SJed Brown    Options Database Keys:
72134f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
7214ef20d060SBarry Smith 
7215ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
7216ef20d060SBarry Smith 
7217abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
7218ef20d060SBarry Smith @*/
72190910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
7220ef20d060SBarry Smith {
7221ef20d060SBarry Smith   PetscErrorCode    ierr;
72220b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
7223ef20d060SBarry Smith   const PetscScalar *yy;
7224ef20d060SBarry Smith   Vec               y;
7225ef20d060SBarry Smith 
7226ef20d060SBarry Smith   PetscFunctionBegin;
7227a9f9c1f6SBarry Smith   if (!step) {
7228a9f9c1f6SBarry Smith     PetscDrawAxis axis;
72296934998bSLisandro Dalcin     PetscInt      dim;
7230a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
72311ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
72320910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
7233a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7234a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7235a9f9c1f6SBarry Smith   }
72360910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
7237ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
72380910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7239ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
7240e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
7241e3efe391SJed Brown   {
7242e3efe391SJed Brown     PetscReal *yreal;
7243e3efe391SJed Brown     PetscInt  i,n;
7244e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
7245785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
7246e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
72470b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
7248e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
7249e3efe391SJed Brown   }
7250e3efe391SJed Brown #else
72510b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
7252e3efe391SJed Brown #endif
7253ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
7254ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
7255b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
72560b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
72576934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
72583923b477SBarry Smith   }
7259ef20d060SBarry Smith   PetscFunctionReturn(0);
7260ef20d060SBarry Smith }
7261ef20d060SBarry Smith 
7262201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7263201da799SBarry Smith {
7264201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7265201da799SBarry Smith   PetscReal      x   = ptime,y;
7266201da799SBarry Smith   PetscErrorCode ierr;
7267201da799SBarry Smith   PetscInt       its;
7268201da799SBarry Smith 
7269201da799SBarry Smith   PetscFunctionBegin;
727063e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7271201da799SBarry Smith   if (!n) {
7272201da799SBarry Smith     PetscDrawAxis axis;
7273201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7274201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7275201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7276201da799SBarry Smith     ctx->snes_its = 0;
7277201da799SBarry Smith   }
7278201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7279201da799SBarry Smith   y    = its - ctx->snes_its;
7280201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
72813fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7282201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
72836934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7284201da799SBarry Smith   }
7285201da799SBarry Smith   ctx->snes_its = its;
7286201da799SBarry Smith   PetscFunctionReturn(0);
7287201da799SBarry Smith }
7288201da799SBarry Smith 
7289201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7290201da799SBarry Smith {
7291201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7292201da799SBarry Smith   PetscReal      x   = ptime,y;
7293201da799SBarry Smith   PetscErrorCode ierr;
7294201da799SBarry Smith   PetscInt       its;
7295201da799SBarry Smith 
7296201da799SBarry Smith   PetscFunctionBegin;
729763e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7298201da799SBarry Smith   if (!n) {
7299201da799SBarry Smith     PetscDrawAxis axis;
7300201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7301201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7302201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7303201da799SBarry Smith     ctx->ksp_its = 0;
7304201da799SBarry Smith   }
7305201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7306201da799SBarry Smith   y    = its - ctx->ksp_its;
7307201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
730899fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7309201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
73106934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7311201da799SBarry Smith   }
7312201da799SBarry Smith   ctx->ksp_its = its;
7313201da799SBarry Smith   PetscFunctionReturn(0);
7314201da799SBarry Smith }
7315f9c1d6abSBarry Smith 
7316f9c1d6abSBarry Smith /*@
7317f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7318f9c1d6abSBarry Smith 
7319f9c1d6abSBarry Smith    Collective on TS and Vec
7320f9c1d6abSBarry Smith 
7321f9c1d6abSBarry Smith    Input Parameters:
7322f9c1d6abSBarry Smith +  ts - the TS context
7323f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7324f9c1d6abSBarry Smith 
7325f9c1d6abSBarry Smith    Output Parameters:
7326f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7327f9c1d6abSBarry Smith 
7328f9c1d6abSBarry Smith    Level: developer
7329f9c1d6abSBarry Smith 
7330f9c1d6abSBarry Smith .keywords: TS, compute
7331f9c1d6abSBarry Smith 
7332f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7333f9c1d6abSBarry Smith @*/
7334f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7335f9c1d6abSBarry Smith {
7336f9c1d6abSBarry Smith   PetscErrorCode ierr;
7337f9c1d6abSBarry Smith 
7338f9c1d6abSBarry Smith   PetscFunctionBegin;
7339f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7340ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7341f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7342f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7343f9c1d6abSBarry Smith }
734424655328SShri 
7345b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7346b3d3934dSBarry Smith /*@C
7347b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7348b3d3934dSBarry Smith 
7349b3d3934dSBarry Smith    Collective on TS
7350b3d3934dSBarry Smith 
7351b3d3934dSBarry Smith    Input Parameters:
7352b3d3934dSBarry Smith .  ts  - the ODE solver object
7353b3d3934dSBarry Smith 
7354b3d3934dSBarry Smith    Output Parameter:
7355b3d3934dSBarry Smith .  ctx - the context
7356b3d3934dSBarry Smith 
7357b3d3934dSBarry Smith    Level: intermediate
7358b3d3934dSBarry Smith 
7359b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
7360b3d3934dSBarry Smith 
7361b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7362b3d3934dSBarry Smith 
7363b3d3934dSBarry Smith @*/
7364b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7365b3d3934dSBarry Smith {
7366b3d3934dSBarry Smith   PetscErrorCode ierr;
7367b3d3934dSBarry Smith 
7368b3d3934dSBarry Smith   PetscFunctionBegin;
7369a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7370b3d3934dSBarry Smith   PetscFunctionReturn(0);
7371b3d3934dSBarry Smith }
7372b3d3934dSBarry Smith 
7373b3d3934dSBarry Smith /*@C
7374b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7375b3d3934dSBarry Smith 
7376b3d3934dSBarry Smith    Collective on TS
7377b3d3934dSBarry Smith 
7378b3d3934dSBarry Smith    Input Parameters:
7379b3d3934dSBarry Smith +  ts - the TS context
7380b3d3934dSBarry Smith .  step - current time-step
7381b3d3934dSBarry Smith .  ptime - current time
73827db568b7SBarry Smith .  u  - current solution
73837db568b7SBarry Smith -  dctx - the envelope context
7384b3d3934dSBarry Smith 
7385b3d3934dSBarry Smith    Options Database:
7386b3d3934dSBarry Smith .  -ts_monitor_envelope
7387b3d3934dSBarry Smith 
7388b3d3934dSBarry Smith    Level: intermediate
7389b3d3934dSBarry Smith 
7390b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7391b3d3934dSBarry Smith 
7392b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7393b3d3934dSBarry Smith 
73947db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7395b3d3934dSBarry Smith @*/
73967db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7397b3d3934dSBarry Smith {
7398b3d3934dSBarry Smith   PetscErrorCode       ierr;
73997db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7400b3d3934dSBarry Smith 
7401b3d3934dSBarry Smith   PetscFunctionBegin;
7402b3d3934dSBarry Smith   if (!ctx->max) {
7403b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7404b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7405b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7406b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7407b3d3934dSBarry Smith   } else {
7408b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7409b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7410b3d3934dSBarry Smith   }
7411b3d3934dSBarry Smith   PetscFunctionReturn(0);
7412b3d3934dSBarry Smith }
7413b3d3934dSBarry Smith 
7414b3d3934dSBarry Smith 
7415b3d3934dSBarry Smith /*@C
7416b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7417b3d3934dSBarry Smith 
7418b3d3934dSBarry Smith    Collective on TS
7419b3d3934dSBarry Smith 
7420b3d3934dSBarry Smith    Input Parameter:
7421b3d3934dSBarry Smith .  ts - the TS context
7422b3d3934dSBarry Smith 
7423b3d3934dSBarry Smith    Output Parameter:
7424b3d3934dSBarry Smith +  max - the maximum values
7425b3d3934dSBarry Smith -  min - the minimum values
7426b3d3934dSBarry Smith 
74277db568b7SBarry Smith    Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
74287db568b7SBarry Smith 
7429b3d3934dSBarry Smith    Level: intermediate
7430b3d3934dSBarry Smith 
7431b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7432b3d3934dSBarry Smith 
7433b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7434b3d3934dSBarry Smith @*/
7435b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7436b3d3934dSBarry Smith {
7437b3d3934dSBarry Smith   PetscInt i;
7438b3d3934dSBarry Smith 
7439b3d3934dSBarry Smith   PetscFunctionBegin;
7440b3d3934dSBarry Smith   if (max) *max = NULL;
7441b3d3934dSBarry Smith   if (min) *min = NULL;
7442b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7443b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
74445537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7445b3d3934dSBarry Smith       if (max) *max = ctx->max;
7446b3d3934dSBarry Smith       if (min) *min = ctx->min;
7447b3d3934dSBarry Smith       break;
7448b3d3934dSBarry Smith     }
7449b3d3934dSBarry Smith   }
7450b3d3934dSBarry Smith   PetscFunctionReturn(0);
7451b3d3934dSBarry Smith }
7452b3d3934dSBarry Smith 
7453b3d3934dSBarry Smith /*@C
7454b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7455b3d3934dSBarry Smith 
7456b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7457b3d3934dSBarry Smith 
7458b3d3934dSBarry Smith    Input Parameter:
7459b3d3934dSBarry Smith .  ctx - the monitor context
7460b3d3934dSBarry Smith 
7461b3d3934dSBarry Smith    Level: intermediate
7462b3d3934dSBarry Smith 
7463b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7464b3d3934dSBarry Smith 
74657db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7466b3d3934dSBarry Smith @*/
7467b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7468b3d3934dSBarry Smith {
7469b3d3934dSBarry Smith   PetscErrorCode ierr;
7470b3d3934dSBarry Smith 
7471b3d3934dSBarry Smith   PetscFunctionBegin;
7472b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7473b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7474b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7475b3d3934dSBarry Smith   PetscFunctionReturn(0);
7476b3d3934dSBarry Smith }
7477f2dee214SBarry Smith 
747824655328SShri /*@
747924655328SShri    TSRollBack - Rolls back one time step
748024655328SShri 
748124655328SShri    Collective on TS
748224655328SShri 
748324655328SShri    Input Parameter:
748424655328SShri .  ts - the TS context obtained from TSCreate()
748524655328SShri 
748624655328SShri    Level: advanced
748724655328SShri 
748824655328SShri .keywords: TS, timestep, rollback
748924655328SShri 
749024655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
749124655328SShri @*/
749224655328SShri PetscErrorCode  TSRollBack(TS ts)
749324655328SShri {
749424655328SShri   PetscErrorCode ierr;
749524655328SShri 
749624655328SShri   PetscFunctionBegin;
749724655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7498b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
749924655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
750024655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
750124655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
750224655328SShri   ts->ptime = ts->ptime_prev;
7503be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
7504be5899b3SLisandro Dalcin   ts->steps--; ts->total_steps--;
750510b82f12SShri Abhyankar   ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
7506b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
750724655328SShri   PetscFunctionReturn(0);
750824655328SShri }
7509aeb4809dSShri Abhyankar 
7510ff22ae23SHong Zhang /*@
7511ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7512ff22ae23SHong Zhang 
7513ff22ae23SHong Zhang    Input Parameter:
7514ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7515ff22ae23SHong Zhang 
7516ff22ae23SHong Zhang    Level: advanced
7517ff22ae23SHong Zhang 
7518ff22ae23SHong Zhang .keywords: TS, getstages
7519ff22ae23SHong Zhang 
7520ff22ae23SHong Zhang .seealso: TSCreate()
7521ff22ae23SHong Zhang @*/
7522ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7523ff22ae23SHong Zhang {
7524ff22ae23SHong Zhang   PetscErrorCode ierr;
7525ff22ae23SHong Zhang 
7526ff22ae23SHong Zhang   PetscFunctionBegin;
7527ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7528ff22ae23SHong Zhang   PetscValidPointer(ns,2);
7529ff22ae23SHong Zhang 
7530ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
7531ff22ae23SHong Zhang   else {
7532ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7533ff22ae23SHong Zhang   }
7534ff22ae23SHong Zhang   PetscFunctionReturn(0);
7535ff22ae23SHong Zhang }
7536ff22ae23SHong Zhang 
7537847ff0e1SMatthew G. Knepley /*@C
7538847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7539847ff0e1SMatthew G. Knepley 
7540847ff0e1SMatthew G. Knepley   Collective on SNES
7541847ff0e1SMatthew G. Knepley 
7542847ff0e1SMatthew G. Knepley   Input Parameters:
7543847ff0e1SMatthew G. Knepley + ts - the TS context
7544847ff0e1SMatthew G. Knepley . t - current timestep
7545847ff0e1SMatthew G. Knepley . U - state vector
7546847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7547847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7548847ff0e1SMatthew G. Knepley - ctx - an optional user context
7549847ff0e1SMatthew G. Knepley 
7550847ff0e1SMatthew G. Knepley   Output Parameters:
7551847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7552847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7553847ff0e1SMatthew G. Knepley 
7554847ff0e1SMatthew G. Knepley   Level: intermediate
7555847ff0e1SMatthew G. Knepley 
7556847ff0e1SMatthew G. Knepley   Notes:
7557847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7558847ff0e1SMatthew G. Knepley 
7559847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7560847ff0e1SMatthew G. Knepley 
7561847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7562847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7563847ff0e1SMatthew G. Knepley 
7564847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7565847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7566847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7567847ff0e1SMatthew G. Knepley 
7568847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7569847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7570847ff0e1SMatthew G. Knepley @*/
7571847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7572847ff0e1SMatthew G. Knepley {
7573847ff0e1SMatthew G. Knepley   SNES           snes;
7574847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7575847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7576847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7577847ff0e1SMatthew G. Knepley 
7578847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7579c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7580847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7581847ff0e1SMatthew G. Knepley   if (!color) {
7582847ff0e1SMatthew G. Knepley     DM         dm;
7583847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7584847ff0e1SMatthew G. Knepley 
7585847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7586847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7587847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7588847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7589847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7590847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7591847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7592847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7593847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7594847ff0e1SMatthew G. Knepley     } else {
7595847ff0e1SMatthew G. Knepley       MatColoring mc;
7596847ff0e1SMatthew G. Knepley 
7597847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7598847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7599847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7600847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7601847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7602847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7603847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7604847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7605847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7606847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7607847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7608847ff0e1SMatthew G. Knepley     }
7609847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7610847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7611847ff0e1SMatthew G. Knepley   }
7612847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7613847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7614847ff0e1SMatthew G. Knepley   if (J != B) {
7615847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7616847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7617847ff0e1SMatthew G. Knepley   }
7618847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7619847ff0e1SMatthew G. Knepley }
762093b34091SDebojyoti Ghosh 
7621cb9d8021SPierre Barbier de Reuille /*@
7622cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7623cb9d8021SPierre Barbier de Reuille 
7624cb9d8021SPierre Barbier de Reuille     Input Parameters:
7625cb9d8021SPierre Barbier de Reuille     ts - the TS context
7626cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7627cb9d8021SPierre Barbier de Reuille 
7628cb9d8021SPierre Barbier de Reuille     Level: intermediate
7629cb9d8021SPierre Barbier de Reuille 
7630cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7631cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7632cb9d8021SPierre Barbier de Reuille @*/
7633cb9d8021SPierre Barbier de Reuille 
7634d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7635cb9d8021SPierre Barbier de Reuille {
7636cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7637cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7638cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7639cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7640cb9d8021SPierre Barbier de Reuille }
7641cb9d8021SPierre Barbier de Reuille 
7642cb9d8021SPierre Barbier de Reuille /*@
7643cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7644cb9d8021SPierre Barbier de Reuille 
7645cb9d8021SPierre Barbier de Reuille     Input Parameters:
7646cb9d8021SPierre Barbier de Reuille     ts - the TS context
7647cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7648d183316bSPierre Barbier de Reuille     Y - state vector to check.
7649cb9d8021SPierre Barbier de Reuille 
7650cb9d8021SPierre Barbier de Reuille     Output Parameter:
7651cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7652cb9d8021SPierre Barbier de Reuille 
7653cb9d8021SPierre Barbier de Reuille     Note:
7654cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7655cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
765696a0c994SBarry Smith 
765796a0c994SBarry Smith     Level: advanced
7658cb9d8021SPierre Barbier de Reuille @*/
7659d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7660cb9d8021SPierre Barbier de Reuille {
7661cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
7662cb9d8021SPierre Barbier de Reuille 
7663cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7664cb9d8021SPierre Barbier de Reuille 
7665cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7666cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7667cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7668d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7669cb9d8021SPierre Barbier de Reuille   }
7670cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7671cb9d8021SPierre Barbier de Reuille }
76721ceb14c0SBarry Smith 
767393b34091SDebojyoti Ghosh /*@C
7674e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
767593b34091SDebojyoti Ghosh 
767693b34091SDebojyoti Ghosh   Collective on MPI_Comm
767793b34091SDebojyoti Ghosh 
767893b34091SDebojyoti Ghosh   Input Parameter:
767993b34091SDebojyoti Ghosh . tsin    - The input TS
768093b34091SDebojyoti Ghosh 
768193b34091SDebojyoti Ghosh   Output Parameter:
7682e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
768393b34091SDebojyoti Ghosh 
76845eca1a21SEmil Constantinescu   Notes:
76855eca1a21SEmil 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.
76865eca1a21SEmil Constantinescu 
7687baa10174SEmil 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);
76885eca1a21SEmil Constantinescu 
76895eca1a21SEmil Constantinescu   Level: developer
769093b34091SDebojyoti Ghosh 
7691e5168f73SEmil Constantinescu .keywords: TS, clone
7692e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
769393b34091SDebojyoti Ghosh @*/
7694baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
769593b34091SDebojyoti Ghosh {
769693b34091SDebojyoti Ghosh   TS             t;
769793b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7698dc846ba4SSatish Balay   SNES           snes_start;
7699dc846ba4SSatish Balay   DM             dm;
7700dc846ba4SSatish Balay   TSType         type;
770193b34091SDebojyoti Ghosh 
770293b34091SDebojyoti Ghosh   PetscFunctionBegin;
770393b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
770493b34091SDebojyoti Ghosh   *tsout = NULL;
770593b34091SDebojyoti Ghosh 
77067a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
770793b34091SDebojyoti Ghosh 
770893b34091SDebojyoti Ghosh   /* General TS description */
770993b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
771093b34091SDebojyoti Ghosh   t->setupcalled       = 0;
771193b34091SDebojyoti Ghosh   t->ksp_its           = 0;
771293b34091SDebojyoti Ghosh   t->snes_its          = 0;
771393b34091SDebojyoti Ghosh   t->nwork             = 0;
771493b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
771593b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
771693b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
771793b34091SDebojyoti Ghosh 
771834561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
771934561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7720d15a3a53SEmil Constantinescu 
772193b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
772293b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
772393b34091SDebojyoti Ghosh 
772493b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
772551699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
772693b34091SDebojyoti Ghosh 
7727e7069c78SShri   t->trajectory = tsin->trajectory;
7728e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7729e7069c78SShri 
7730e7069c78SShri   t->event = tsin->event;
77316b10a48eSSatish Balay   if (t->event) t->event->refct++;
7732e7069c78SShri 
773393b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
773493b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7735e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
773693b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
773793b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
773893b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
7739e7069c78SShri   t->total_steps       = tsin->total_steps;
774093b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
774193b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
774293b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
774393b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
774493b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
774593b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
774693b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
774793b34091SDebojyoti Ghosh 
774893b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
774993b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
775093b34091SDebojyoti Ghosh 
775193b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
775293b34091SDebojyoti Ghosh 
775393b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
775493b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
775593b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
775693b34091SDebojyoti Ghosh 
775793b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
775893b34091SDebojyoti Ghosh 
77590d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
77600d4fed19SBarry Smith     PetscInt i;
77610d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
77620d4fed19SBarry Smith     for (i=0; i<10; i++) {
77630d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
77640d4fed19SBarry Smith     }
77650d4fed19SBarry Smith   }
776693b34091SDebojyoti Ghosh   *tsout = t;
776793b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
776893b34091SDebojyoti Ghosh }
7769