xref: /petsc/src/ts/interface/ts.c (revision efd4aadf157bf1ba2d80c2be092fcf4247860003)
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 
1102ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
1112ffb9264SLisandro Dalcin {
1122ffb9264SLisandro Dalcin   PetscErrorCode ierr;
1132ffb9264SLisandro Dalcin 
1142ffb9264SLisandro Dalcin   PetscFunctionBegin;
115b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
116b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
1172ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
1182ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
1192ffb9264SLisandro Dalcin   }
1202ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
1212ffb9264SLisandro Dalcin }
1222ffb9264SLisandro Dalcin 
123bdad233fSMatthew Knepley /*@
124bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
125bdad233fSMatthew Knepley 
126bdad233fSMatthew Knepley    Collective on TS
127bdad233fSMatthew Knepley 
128bdad233fSMatthew Knepley    Input Parameter:
129bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
130bdad233fSMatthew Knepley 
131bdad233fSMatthew Knepley    Options Database Keys:
132b6a60446SDebojyoti Ghosh +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE
133ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
1343e4cdcaaSBarry Smith .  -ts_max_steps <maxsteps> - maximum number of time-steps to take
1353e4cdcaaSBarry Smith .  -ts_final_time <time> - maximum time to compute to
1363e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
137a3cdaa26SBarry 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
138a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
139a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
140a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
141a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
142a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
143ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
144847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
145bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
146de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
147de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1486934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
149de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
150de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
151de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
152de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1533e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1543e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
155fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
156e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
157110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
158110eb670SHong Zhang .  -ts_adjoint_monitor - print information at each adjoint time step
15953ea634cSHong Zhang -  -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically
16053ea634cSHong Zhang 
16191b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
162bdad233fSMatthew Knepley 
163bdad233fSMatthew Knepley    Level: beginner
164bdad233fSMatthew Knepley 
165bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
166bdad233fSMatthew Knepley 
167a313700dSBarry Smith .seealso: TSGetType()
168bdad233fSMatthew Knepley @*/
1697087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
170bdad233fSMatthew Knepley {
171bc952696SBarry Smith   PetscBool              opt,flg,tflg;
172dfbe8321SBarry Smith   PetscErrorCode         ierr;
173eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
17431748224SBarry Smith   PetscReal              time_step;
17549354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
176d1212d36SBarry Smith   char                   dir[16];
177cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1786991f827SBarry Smith   const char             *defaultType;
1796991f827SBarry Smith   char                   typeName[256];
180bdad233fSMatthew Knepley 
181bdad233fSMatthew Knepley   PetscFunctionBegin;
1820700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1836991f827SBarry Smith 
1846991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
185cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
186cd11d68dSLisandro Dalcin 
187cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
188cd11d68dSLisandro Dalcin   if (((PetscObject)ts)->type_name)
189cd11d68dSLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
190cd11d68dSLisandro Dalcin   else
191cd11d68dSLisandro Dalcin     defaultType = ifun ? TSBEULER : TSEULER;
1926991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1936991f827SBarry Smith   if (opt) {
1946991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1956991f827SBarry Smith   } else {
1966991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1976991f827SBarry Smith   }
198bdad233fSMatthew Knepley 
199bdad233fSMatthew Knepley   /* Handle generic TS options */
2000298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
2010298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
2020298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
20331748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
204cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
20549354f04SShri 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);
20649354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
2070298fd71SBarry 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);
2080298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
2090298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
2100298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
2110298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
212bdad233fSMatthew Knepley 
21356f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
21456f85f32SBarry Smith   {
21556f85f32SBarry Smith   PetscBool set;
21656f85f32SBarry Smith   flg  = PETSC_FALSE;
21756f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
21856f85f32SBarry Smith   if (set) {
21956f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
22056f85f32SBarry Smith   }
22156f85f32SBarry Smith   }
22256f85f32SBarry Smith #endif
22356f85f32SBarry Smith 
224bdad233fSMatthew Knepley   /* Monitor options */
225cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
226fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
227fde5950dSBarry Smith   ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr);
228fde5950dSBarry Smith 
2295180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2305180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
2315180491cSLisandro Dalcin 
2324f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
233b3603a34SBarry Smith   if (opt) {
2340b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2353923b477SBarry Smith     PetscInt       howoften = 1;
236b3603a34SBarry Smith 
2370298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2386ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2394f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
240bdad233fSMatthew Knepley   }
2416ba87a44SLisandro Dalcin 
2424f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
243ef20d060SBarry Smith   if (opt) {
2440b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2453923b477SBarry Smith     PetscInt       howoften = 1;
246ef20d060SBarry Smith 
2470298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
2486ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2494f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
250ef20d060SBarry Smith   }
2516934998bSLisandro Dalcin 
2526934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2536934998bSLisandro Dalcin   if (opt) {
2546934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2556934998bSLisandro Dalcin     PetscInt       howoften = 1;
2566934998bSLisandro Dalcin 
2576934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2586ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2596934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2606934998bSLisandro Dalcin   }
261201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
262201da799SBarry Smith   if (opt) {
263201da799SBarry Smith     TSMonitorLGCtx ctx;
264201da799SBarry Smith     PetscInt       howoften = 1;
265201da799SBarry Smith 
2660298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2676ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
268201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
269201da799SBarry Smith   }
270201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
271201da799SBarry Smith   if (opt) {
272201da799SBarry Smith     TSMonitorLGCtx ctx;
273201da799SBarry Smith     PetscInt       howoften = 1;
274201da799SBarry Smith 
2750298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2766ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
277201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
278201da799SBarry Smith   }
2798189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2808189c53fSBarry Smith   if (opt) {
2818189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2828189c53fSBarry Smith     PetscInt          howoften = 1;
2838189c53fSBarry Smith 
2840298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
2856934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2868189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2878189c53fSBarry Smith   }
288ef20d060SBarry Smith   opt  = PETSC_FALSE;
2890dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
290a7cc72afSBarry Smith   if (opt) {
29183a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
29283a4ac43SBarry Smith     PetscInt         howoften = 1;
293a80ad3e0SBarry Smith 
2940298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2956934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
29683a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
297bdad233fSMatthew Knepley   }
298fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2999110b2e7SHong Zhang   ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr);
3009110b2e7SHong Zhang   if (opt) {
3019110b2e7SHong Zhang     TSMonitorDrawCtx ctx;
3029110b2e7SHong Zhang     PetscInt         howoften = 1;
3039110b2e7SHong Zhang 
3049110b2e7SHong Zhang     ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr);
3056934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3069110b2e7SHong Zhang     ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3079110b2e7SHong Zhang   }
3089110b2e7SHong Zhang   opt  = PETSC_FALSE;
3092d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
3102d5ee99bSBarry Smith   if (opt) {
3112d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
3122d5ee99bSBarry Smith     PetscReal        bounds[4];
3132d5ee99bSBarry Smith     PetscInt         n = 4;
3142d5ee99bSBarry Smith     PetscDraw        draw;
3156934998bSLisandro Dalcin     PetscDrawAxis    axis;
3162d5ee99bSBarry Smith 
3172d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
3182d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
3196934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
3202d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
3216934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
3226934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
3236934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
3242d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3252d5ee99bSBarry Smith   }
3262d5ee99bSBarry Smith   opt  = PETSC_FALSE;
3270dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
3283a471f94SBarry Smith   if (opt) {
32983a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
33083a4ac43SBarry Smith     PetscInt         howoften = 1;
3313a471f94SBarry Smith 
3320298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
3336934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
33483a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3353a471f94SBarry Smith   }
336fde5950dSBarry Smith 
337ed81e22dSJed Brown   opt  = PETSC_FALSE;
33891b97e58SBarry 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);
339ed81e22dSJed Brown   if (flg) {
340ed81e22dSJed Brown     const char *ptr,*ptr2;
341ed81e22dSJed Brown     char       *filetemplate;
342ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
343ed81e22dSJed Brown     /* Do some cursory validation of the input. */
344ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
345ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
346ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
347ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
348ce94432eSBarry 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");
349ed81e22dSJed Brown       if (ptr2) break;
350ed81e22dSJed Brown     }
351ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
352ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
353ed81e22dSJed Brown   }
354bdad233fSMatthew Knepley 
355d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
356d1212d36SBarry Smith   if (flg) {
357d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
358d1212d36SBarry Smith     int                  ray = 0;
359d1212d36SBarry Smith     DMDADirection        ddir;
360d1212d36SBarry Smith     DM                   da;
361d1212d36SBarry Smith     PetscMPIInt          rank;
362d1212d36SBarry Smith 
363ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
364d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
365d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
366ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
367d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
368d1212d36SBarry Smith 
369d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
370b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
371d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
372d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
373ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
374d1212d36SBarry Smith     if (!rank) {
375d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
376d1212d36SBarry Smith     }
37751b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
378d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
379d1212d36SBarry Smith   }
38051b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
38151b4a12fSMatthew G. Knepley   if (flg) {
38251b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
38351b4a12fSMatthew G. Knepley     int                 ray = 0;
38451b4a12fSMatthew G. Knepley     DMDADirection       ddir;
38551b4a12fSMatthew G. Knepley     DM                  da;
38651b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
387d1212d36SBarry Smith 
38851b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
38951b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
39051b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
39151b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
39251b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3931c3436cfSJed Brown 
39451b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
395b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
39651b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
39751b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
39851b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
39951b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
40051b4a12fSMatthew G. Knepley   }
401a7a1495cSBarry Smith 
402b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
403b3d3934dSBarry Smith   if (opt) {
404b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
405b3d3934dSBarry Smith 
406b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
407b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
408b3d3934dSBarry Smith   }
409b3d3934dSBarry Smith 
410847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
411847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
412847ff0e1SMatthew G. Knepley   if (flg) {
413847ff0e1SMatthew G. Knepley     DM   dm;
414847ff0e1SMatthew G. Knepley     DMTS tdm;
415847ff0e1SMatthew G. Knepley 
416847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
417847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
418847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
419847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
420847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
421847ff0e1SMatthew G. Knepley   }
422847ff0e1SMatthew G. Knepley 
423d763cef2SBarry Smith   /* Handle specific TS options */
424d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4256991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
426d763cef2SBarry Smith   }
427fbc52257SHong Zhang 
428a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
429a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
430a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
431a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
432a7bdc993SLisandro Dalcin 
43368bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4344f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
43568bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
43668bece0bSHong Zhang   if (tflg) {
43768bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
43868bece0bSHong Zhang   }
4394f122a70SLisandro Dalcin   tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE;
44068bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr);
44168bece0bSHong Zhang   if (flg) {
44268bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
44368bece0bSHong Zhang     ts->adjoint_solve = tflg;
44468bece0bSHong Zhang   }
445d763cef2SBarry Smith 
446d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4470633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
448fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
449d763cef2SBarry Smith 
4504f122a70SLisandro Dalcin   if (ts->trajectory) {
4514f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
452d763cef2SBarry Smith   }
45368bece0bSHong Zhang 
4544f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4554f122a70SLisandro Dalcin   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);}
4564f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
457d763cef2SBarry Smith   PetscFunctionReturn(0);
458d763cef2SBarry Smith }
459d763cef2SBarry Smith 
460d2daff3dSHong Zhang /*@
46178fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
46278fbdcc8SBarry Smith 
46378fbdcc8SBarry Smith    Collective on TS
46478fbdcc8SBarry Smith 
46578fbdcc8SBarry Smith    Input Parameters:
46678fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
46778fbdcc8SBarry Smith 
46878fbdcc8SBarry Smith    Output Parameters;
46978fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
47078fbdcc8SBarry Smith 
47178fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
47278fbdcc8SBarry Smith 
47378fbdcc8SBarry Smith    Level: advanced
47478fbdcc8SBarry Smith 
47578fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
47678fbdcc8SBarry Smith 
47778fbdcc8SBarry Smith .keywords: TS, set, checkpoint,
47878fbdcc8SBarry Smith @*/
47978fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
48078fbdcc8SBarry Smith {
48178fbdcc8SBarry Smith   PetscFunctionBegin;
48278fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48378fbdcc8SBarry Smith   *tr = ts->trajectory;
48478fbdcc8SBarry Smith   PetscFunctionReturn(0);
48578fbdcc8SBarry Smith }
48678fbdcc8SBarry Smith 
48778fbdcc8SBarry Smith /*@
488bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
489d2daff3dSHong Zhang 
490d2daff3dSHong Zhang    Collective on TS
491d2daff3dSHong Zhang 
492d2daff3dSHong Zhang    Input Parameters:
493bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
494bc952696SBarry Smith 
49578fbdcc8SBarry Smith    Options Database:
49678fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
49778fbdcc8SBarry Smith -  -ts_trajectory_type type
49878fbdcc8SBarry Smith 
49968bece0bSHong Zhang Note: This routine should be called after all TS options have been set
500d2daff3dSHong Zhang 
50178fbdcc8SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/bin/PetscBinaryIOTrajectory.py and
50278fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
50378fbdcc8SBarry Smith 
504d2daff3dSHong Zhang    Level: intermediate
505d2daff3dSHong Zhang 
50678fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectoryType, TSSetTrajectoryType()
507d2daff3dSHong Zhang 
508bc952696SBarry Smith .keywords: TS, set, checkpoint,
509d2daff3dSHong Zhang @*/
510bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
511d2daff3dSHong Zhang {
512bc952696SBarry Smith   PetscErrorCode ierr;
513bc952696SBarry Smith 
514d2daff3dSHong Zhang   PetscFunctionBegin;
515d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
516bc952696SBarry Smith   if (!ts->trajectory) {
517bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
518972caf09SHong Zhang     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
519bc952696SBarry Smith   }
520d2daff3dSHong Zhang   PetscFunctionReturn(0);
521d2daff3dSHong Zhang }
522d2daff3dSHong Zhang 
523a7a1495cSBarry Smith /*@
524a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
525a7a1495cSBarry Smith       set with TSSetRHSJacobian().
526a7a1495cSBarry Smith 
527a7a1495cSBarry Smith    Collective on TS and Vec
528a7a1495cSBarry Smith 
529a7a1495cSBarry Smith    Input Parameters:
530316643e7SJed Brown +  ts - the TS context
531a7a1495cSBarry Smith .  t - current timestep
5320910c330SBarry Smith -  U - input vector
533a7a1495cSBarry Smith 
534a7a1495cSBarry Smith    Output Parameters:
535a7a1495cSBarry Smith +  A - Jacobian matrix
536a7a1495cSBarry Smith .  B - optional preconditioning matrix
537a7a1495cSBarry Smith -  flag - flag indicating matrix structure
538a7a1495cSBarry Smith 
539a7a1495cSBarry Smith    Notes:
540a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
541a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
542a7a1495cSBarry Smith 
54394b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
544a7a1495cSBarry Smith    flag parameter.
545a7a1495cSBarry Smith 
546a7a1495cSBarry Smith    Level: developer
547a7a1495cSBarry Smith 
548a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
549a7a1495cSBarry Smith 
55094b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
551a7a1495cSBarry Smith @*/
552d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
553a7a1495cSBarry Smith {
554dfbe8321SBarry Smith   PetscErrorCode ierr;
555270bf2e7SJed Brown   PetscObjectState Ustate;
55624989b8cSPeter Brune   DM             dm;
557942e3340SBarry Smith   DMTS           tsdm;
55824989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
55924989b8cSPeter Brune   void           *ctx;
56024989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
561b2df71adSDebojyoti Ghosh   TSRHSFunction  rhsfunction;
562a7a1495cSBarry Smith 
563a7a1495cSBarry Smith   PetscFunctionBegin;
5640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5650910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5660910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
56724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
568942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
56924989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5700298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
571b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
57259e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
573b2df71adSDebojyoti Ghosh   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
5740e4ef248SJed Brown     PetscFunctionReturn(0);
575f8ede8e7SJed Brown   }
576d90be118SSean Farley 
577ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
578d90be118SSean Farley 
579e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
58094ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
58194ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
58294ab13aaSBarry Smith     if (A != B) {
58394ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
58494ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
585e1244c69SJed Brown     }
586e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
587e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
588e1244c69SJed Brown   }
589e1244c69SJed Brown 
59024989b8cSPeter Brune   if (rhsjacobianfunc) {
5916cd88445SBarry Smith     PetscBool missing;
59294ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
593a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
594d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
595a7a1495cSBarry Smith     PetscStackPop;
59694ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
5976cd88445SBarry Smith     if (A) {
5986cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
5996cd88445SBarry 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");
6006cd88445SBarry Smith     }
6016cd88445SBarry Smith     if (B && B != A) {
6026cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
6036cd88445SBarry 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");
6046cd88445SBarry Smith     }
605ef66eb69SBarry Smith   } else {
60694ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
60794ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
608ef66eb69SBarry Smith   }
6090e4ef248SJed Brown   ts->rhsjacobian.time       = t;
6100910c330SBarry Smith   ts->rhsjacobian.X          = U;
61159e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
612a7a1495cSBarry Smith   PetscFunctionReturn(0);
613a7a1495cSBarry Smith }
614a7a1495cSBarry Smith 
615316643e7SJed Brown /*@
616d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
617d763cef2SBarry Smith 
618316643e7SJed Brown    Collective on TS and Vec
619316643e7SJed Brown 
620316643e7SJed Brown    Input Parameters:
621316643e7SJed Brown +  ts - the TS context
622316643e7SJed Brown .  t - current time
6230910c330SBarry Smith -  U - state vector
624316643e7SJed Brown 
625316643e7SJed Brown    Output Parameter:
626316643e7SJed Brown .  y - right hand side
627316643e7SJed Brown 
628316643e7SJed Brown    Note:
629316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
630316643e7SJed Brown    is used internally within the nonlinear solvers.
631316643e7SJed Brown 
632316643e7SJed Brown    Level: developer
633316643e7SJed Brown 
634316643e7SJed Brown .keywords: TS, compute
635316643e7SJed Brown 
636316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
637316643e7SJed Brown @*/
6380910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
639d763cef2SBarry Smith {
640dfbe8321SBarry Smith   PetscErrorCode ierr;
64124989b8cSPeter Brune   TSRHSFunction  rhsfunction;
64224989b8cSPeter Brune   TSIFunction    ifunction;
64324989b8cSPeter Brune   void           *ctx;
64424989b8cSPeter Brune   DM             dm;
64524989b8cSPeter Brune 
646d763cef2SBarry Smith   PetscFunctionBegin;
6470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6480910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6490700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
65024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
65124989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6520298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
653d763cef2SBarry Smith 
654ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
655d763cef2SBarry Smith 
6560910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
65724989b8cSPeter Brune   if (rhsfunction) {
658d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6590910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
660d763cef2SBarry Smith     PetscStackPop;
661214bc6a2SJed Brown   } else {
662214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
663b2cd27e8SJed Brown   }
66444a41b28SSean Farley 
6650910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
666d763cef2SBarry Smith   PetscFunctionReturn(0);
667d763cef2SBarry Smith }
668d763cef2SBarry Smith 
669ef20d060SBarry Smith /*@
670ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
671ef20d060SBarry Smith 
672ef20d060SBarry Smith    Collective on TS and Vec
673ef20d060SBarry Smith 
674ef20d060SBarry Smith    Input Parameters:
675ef20d060SBarry Smith +  ts - the TS context
676ef20d060SBarry Smith -  t - current time
677ef20d060SBarry Smith 
678ef20d060SBarry Smith    Output Parameter:
6790910c330SBarry Smith .  U - the solution
680ef20d060SBarry Smith 
681ef20d060SBarry Smith    Note:
682ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
683ef20d060SBarry Smith    is used internally within the nonlinear solvers.
684ef20d060SBarry Smith 
685ef20d060SBarry Smith    Level: developer
686ef20d060SBarry Smith 
687ef20d060SBarry Smith .keywords: TS, compute
688ef20d060SBarry Smith 
689abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
690ef20d060SBarry Smith @*/
6910910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
692ef20d060SBarry Smith {
693ef20d060SBarry Smith   PetscErrorCode     ierr;
694ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
695ef20d060SBarry Smith   void               *ctx;
696ef20d060SBarry Smith   DM                 dm;
697ef20d060SBarry Smith 
698ef20d060SBarry Smith   PetscFunctionBegin;
699ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7000910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
701ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
702ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
703ef20d060SBarry Smith 
704ef20d060SBarry Smith   if (solutionfunction) {
7059b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7060910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
707ef20d060SBarry Smith     PetscStackPop;
708ef20d060SBarry Smith   }
709ef20d060SBarry Smith   PetscFunctionReturn(0);
710ef20d060SBarry Smith }
7119b7cd975SBarry Smith /*@
7129b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7139b7cd975SBarry Smith 
7149b7cd975SBarry Smith    Collective on TS and Vec
7159b7cd975SBarry Smith 
7169b7cd975SBarry Smith    Input Parameters:
7179b7cd975SBarry Smith +  ts - the TS context
7189b7cd975SBarry Smith -  t - current time
7199b7cd975SBarry Smith 
7209b7cd975SBarry Smith    Output Parameter:
7219b7cd975SBarry Smith .  U - the function value
7229b7cd975SBarry Smith 
7239b7cd975SBarry Smith    Note:
7249b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7259b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7269b7cd975SBarry Smith 
7279b7cd975SBarry Smith    Level: developer
7289b7cd975SBarry Smith 
7299b7cd975SBarry Smith .keywords: TS, compute
7309b7cd975SBarry Smith 
7319b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7329b7cd975SBarry Smith @*/
7339b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7349b7cd975SBarry Smith {
7359b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7369b7cd975SBarry Smith   void               *ctx;
7379b7cd975SBarry Smith   DM                 dm;
7389b7cd975SBarry Smith 
7399b7cd975SBarry Smith   PetscFunctionBegin;
7409b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7419b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7429b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7439b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7449b7cd975SBarry Smith 
7459b7cd975SBarry Smith   if (forcing) {
7469b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7479b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7489b7cd975SBarry Smith     PetscStackPop;
7499b7cd975SBarry Smith   }
7509b7cd975SBarry Smith   PetscFunctionReturn(0);
7519b7cd975SBarry Smith }
752ef20d060SBarry Smith 
753214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
754214bc6a2SJed Brown {
7552dd45cf8SJed Brown   Vec            F;
756214bc6a2SJed Brown   PetscErrorCode ierr;
757214bc6a2SJed Brown 
758214bc6a2SJed Brown   PetscFunctionBegin;
7590298fd71SBarry Smith   *Frhs = NULL;
7600298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
761214bc6a2SJed Brown   if (!ts->Frhs) {
7622dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
763214bc6a2SJed Brown   }
764214bc6a2SJed Brown   *Frhs = ts->Frhs;
765214bc6a2SJed Brown   PetscFunctionReturn(0);
766214bc6a2SJed Brown }
767214bc6a2SJed Brown 
768214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
769214bc6a2SJed Brown {
770214bc6a2SJed Brown   Mat            A,B;
7712dd45cf8SJed Brown   PetscErrorCode ierr;
772214bc6a2SJed Brown 
773214bc6a2SJed Brown   PetscFunctionBegin;
774c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
775c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
7760298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
777214bc6a2SJed Brown   if (Arhs) {
778214bc6a2SJed Brown     if (!ts->Arhs) {
779214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
780214bc6a2SJed Brown     }
781214bc6a2SJed Brown     *Arhs = ts->Arhs;
782214bc6a2SJed Brown   }
783214bc6a2SJed Brown   if (Brhs) {
784214bc6a2SJed Brown     if (!ts->Brhs) {
785bdb70873SJed Brown       if (A != B) {
786214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
787bdb70873SJed Brown       } else {
788bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
78951699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
790bdb70873SJed Brown       }
791214bc6a2SJed Brown     }
792214bc6a2SJed Brown     *Brhs = ts->Brhs;
793214bc6a2SJed Brown   }
794214bc6a2SJed Brown   PetscFunctionReturn(0);
795214bc6a2SJed Brown }
796214bc6a2SJed Brown 
797316643e7SJed Brown /*@
7980910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
799316643e7SJed Brown 
800316643e7SJed Brown    Collective on TS and Vec
801316643e7SJed Brown 
802316643e7SJed Brown    Input Parameters:
803316643e7SJed Brown +  ts - the TS context
804316643e7SJed Brown .  t - current time
8050910c330SBarry Smith .  U - state vector
8060910c330SBarry Smith .  Udot - time derivative of state vector
807214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
808316643e7SJed Brown 
809316643e7SJed Brown    Output Parameter:
810316643e7SJed Brown .  Y - right hand side
811316643e7SJed Brown 
812316643e7SJed Brown    Note:
813316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
814316643e7SJed Brown    is used internally within the nonlinear solvers.
815316643e7SJed Brown 
816316643e7SJed Brown    If the user did did not write their equations in implicit form, this
817316643e7SJed Brown    function recasts them in implicit form.
818316643e7SJed Brown 
819316643e7SJed Brown    Level: developer
820316643e7SJed Brown 
821316643e7SJed Brown .keywords: TS, compute
822316643e7SJed Brown 
823316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
824316643e7SJed Brown @*/
8250910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
826316643e7SJed Brown {
827316643e7SJed Brown   PetscErrorCode ierr;
82824989b8cSPeter Brune   TSIFunction    ifunction;
82924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
83024989b8cSPeter Brune   void           *ctx;
83124989b8cSPeter Brune   DM             dm;
832316643e7SJed Brown 
833316643e7SJed Brown   PetscFunctionBegin;
8340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8350910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8360910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8370700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
838316643e7SJed Brown 
83924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
84024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8410298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
84224989b8cSPeter Brune 
843ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
844d90be118SSean Farley 
8450910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
84624989b8cSPeter Brune   if (ifunction) {
847316643e7SJed Brown     PetscStackPush("TS user implicit function");
8480910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
849316643e7SJed Brown     PetscStackPop;
850214bc6a2SJed Brown   }
851214bc6a2SJed Brown   if (imex) {
85224989b8cSPeter Brune     if (!ifunction) {
8530910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8542dd45cf8SJed Brown     }
85524989b8cSPeter Brune   } else if (rhsfunction) {
85624989b8cSPeter Brune     if (ifunction) {
857214bc6a2SJed Brown       Vec Frhs;
858214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8590910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
860214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8612dd45cf8SJed Brown     } else {
8620910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8630910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
864316643e7SJed Brown     }
8654a6899ffSJed Brown   }
8660910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
867316643e7SJed Brown   PetscFunctionReturn(0);
868316643e7SJed Brown }
869316643e7SJed Brown 
870316643e7SJed Brown /*@
871316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
872316643e7SJed Brown 
873316643e7SJed Brown    Collective on TS and Vec
874316643e7SJed Brown 
875316643e7SJed Brown    Input
876316643e7SJed Brown       Input Parameters:
877316643e7SJed Brown +  ts - the TS context
878316643e7SJed Brown .  t - current timestep
8790910c330SBarry Smith .  U - state vector
8800910c330SBarry Smith .  Udot - time derivative of state vector
881214bc6a2SJed Brown .  shift - shift to apply, see note below
882214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
883316643e7SJed Brown 
884316643e7SJed Brown    Output Parameters:
885316643e7SJed Brown +  A - Jacobian matrix
886316643e7SJed Brown .  B - optional preconditioning matrix
887316643e7SJed Brown -  flag - flag indicating matrix structure
888316643e7SJed Brown 
889316643e7SJed Brown    Notes:
8900910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
891316643e7SJed Brown 
8920910c330SBarry Smith    dF/dU + shift*dF/dUdot
893316643e7SJed Brown 
894316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
895316643e7SJed Brown    is used internally within the nonlinear solvers.
896316643e7SJed Brown 
897316643e7SJed Brown    Level: developer
898316643e7SJed Brown 
899316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
900316643e7SJed Brown 
901316643e7SJed Brown .seealso:  TSSetIJacobian()
90263495f91SJed Brown @*/
903d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
904316643e7SJed Brown {
905316643e7SJed Brown   PetscErrorCode ierr;
90624989b8cSPeter Brune   TSIJacobian    ijacobian;
90724989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
90824989b8cSPeter Brune   DM             dm;
90924989b8cSPeter Brune   void           *ctx;
910316643e7SJed Brown 
911316643e7SJed Brown   PetscFunctionBegin;
9120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9130910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9140910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
915316643e7SJed Brown   PetscValidPointer(A,6);
91694ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
917316643e7SJed Brown   PetscValidPointer(B,7);
91894ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
91924989b8cSPeter Brune 
92024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
92124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9220298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
92324989b8cSPeter Brune 
924ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
925316643e7SJed Brown 
92694ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
92724989b8cSPeter Brune   if (ijacobian) {
9286cd88445SBarry Smith     PetscBool missing;
929316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
930d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
931316643e7SJed Brown     PetscStackPop;
9326cd88445SBarry Smith     if (A) {
9336cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9346cd88445SBarry 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");
9356cd88445SBarry Smith     }
9366cd88445SBarry Smith     if (B && B != A) {
9376cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9386cd88445SBarry 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");
9396cd88445SBarry Smith     }
9404a6899ffSJed Brown   }
941214bc6a2SJed Brown   if (imex) {
942b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9434c26be97Sstefano_zampini       PetscBool assembled;
94494ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9454c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9464c26be97Sstefano_zampini       if (!assembled) {
9474c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9484c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9494c26be97Sstefano_zampini       }
95094ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
95194ab13aaSBarry Smith       if (A != B) {
95294ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9534c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9544c26be97Sstefano_zampini         if (!assembled) {
9554c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9564c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9574c26be97Sstefano_zampini         }
95894ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
959214bc6a2SJed Brown       }
960214bc6a2SJed Brown     }
961214bc6a2SJed Brown   } else {
962e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
963e1244c69SJed Brown     if (rhsjacobian) {
964214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
965d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
966e1244c69SJed Brown     }
96794ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
968e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
969e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
97094ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
97194ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
97294ab13aaSBarry Smith       if (A != B) {
97394ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
97494ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
975316643e7SJed Brown       }
976e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
977e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
978e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
97994ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
98094ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
98194ab13aaSBarry Smith         if (A != B) {
98294ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
98394ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
984214bc6a2SJed Brown         }
985316643e7SJed Brown       }
98694ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
98794ab13aaSBarry Smith       if (A != B) {
98894ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
989316643e7SJed Brown       }
990316643e7SJed Brown     }
991316643e7SJed Brown   }
99294ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
993316643e7SJed Brown   PetscFunctionReturn(0);
994316643e7SJed Brown }
995316643e7SJed Brown 
996d763cef2SBarry Smith /*@C
997d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
998b5abc632SBarry Smith     where U_t = G(t,u).
999d763cef2SBarry Smith 
10003f9fe445SBarry Smith     Logically Collective on TS
1001d763cef2SBarry Smith 
1002d763cef2SBarry Smith     Input Parameters:
1003d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10040298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1005d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1006d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10070298fd71SBarry Smith           function evaluation routine (may be NULL)
1008d763cef2SBarry Smith 
1009d763cef2SBarry Smith     Calling sequence of func:
101087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1011d763cef2SBarry Smith 
1012d763cef2SBarry Smith +   t - current timestep
1013d763cef2SBarry Smith .   u - input vector
1014d763cef2SBarry Smith .   F - function vector
1015d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1016d763cef2SBarry Smith 
1017d763cef2SBarry Smith     Level: beginner
1018d763cef2SBarry Smith 
10192bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10202bbac0d3SBarry Smith 
1021d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1022d763cef2SBarry Smith 
1023ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1024d763cef2SBarry Smith @*/
1025089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1026d763cef2SBarry Smith {
1027089b2837SJed Brown   PetscErrorCode ierr;
1028089b2837SJed Brown   SNES           snes;
10290298fd71SBarry Smith   Vec            ralloc = NULL;
103024989b8cSPeter Brune   DM             dm;
1031d763cef2SBarry Smith 
1032089b2837SJed Brown   PetscFunctionBegin;
10330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1034ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
103524989b8cSPeter Brune 
103624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
103724989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1038089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1039e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1040e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1041e856ceecSJed Brown     r = ralloc;
1042e856ceecSJed Brown   }
1043089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1044e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1045d763cef2SBarry Smith   PetscFunctionReturn(0);
1046d763cef2SBarry Smith }
1047d763cef2SBarry Smith 
1048ef20d060SBarry Smith /*@C
1049abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1050ef20d060SBarry Smith 
1051ef20d060SBarry Smith     Logically Collective on TS
1052ef20d060SBarry Smith 
1053ef20d060SBarry Smith     Input Parameters:
1054ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1055ef20d060SBarry Smith .   f - routine for evaluating the solution
1056ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10570298fd71SBarry Smith           function evaluation routine (may be NULL)
1058ef20d060SBarry Smith 
1059ef20d060SBarry Smith     Calling sequence of func:
1060ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1061ef20d060SBarry Smith 
1062ef20d060SBarry Smith +   t - current timestep
1063ef20d060SBarry Smith .   u - output vector
1064ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1065ef20d060SBarry Smith 
1066abd5a294SJed Brown     Notes:
1067abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1068abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1069abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1070abd5a294SJed Brown 
10714f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1072abd5a294SJed Brown 
1073ef20d060SBarry Smith     Level: beginner
1074ef20d060SBarry Smith 
1075ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1076ef20d060SBarry Smith 
10779b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
1078ef20d060SBarry Smith @*/
1079ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1080ef20d060SBarry Smith {
1081ef20d060SBarry Smith   PetscErrorCode ierr;
1082ef20d060SBarry Smith   DM             dm;
1083ef20d060SBarry Smith 
1084ef20d060SBarry Smith   PetscFunctionBegin;
1085ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1086ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1087ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1088ef20d060SBarry Smith   PetscFunctionReturn(0);
1089ef20d060SBarry Smith }
1090ef20d060SBarry Smith 
10919b7cd975SBarry Smith /*@C
10929b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
10939b7cd975SBarry Smith 
10949b7cd975SBarry Smith     Logically Collective on TS
10959b7cd975SBarry Smith 
10969b7cd975SBarry Smith     Input Parameters:
10979b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1098e162b725SBarry Smith .   func - routine for evaluating the forcing function
10999b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11000298fd71SBarry Smith           function evaluation routine (may be NULL)
11019b7cd975SBarry Smith 
11029b7cd975SBarry Smith     Calling sequence of func:
1103e162b725SBarry Smith $     func (TS ts,PetscReal t,Vec f,void *ctx);
11049b7cd975SBarry Smith 
11059b7cd975SBarry Smith +   t - current timestep
1106e162b725SBarry Smith .   f - output vector
11079b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11089b7cd975SBarry Smith 
11099b7cd975SBarry Smith     Notes:
11109b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1111e162b725SBarry 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
1112e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1113e162b725SBarry Smith 
1114e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1115e162b725SBarry Smith 
1116e162b725SBarry 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
1117e162b725SBarry Smith     parameters can be passed in the ctx variable.
11189b7cd975SBarry Smith 
11199b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11209b7cd975SBarry Smith 
11219b7cd975SBarry Smith     Level: beginner
11229b7cd975SBarry Smith 
11239b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
11249b7cd975SBarry Smith 
11259b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11269b7cd975SBarry Smith @*/
1127e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11289b7cd975SBarry Smith {
11299b7cd975SBarry Smith   PetscErrorCode ierr;
11309b7cd975SBarry Smith   DM             dm;
11319b7cd975SBarry Smith 
11329b7cd975SBarry Smith   PetscFunctionBegin;
11339b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11349b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1135e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11369b7cd975SBarry Smith   PetscFunctionReturn(0);
11379b7cd975SBarry Smith }
11389b7cd975SBarry Smith 
1139d763cef2SBarry Smith /*@C
1140f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1141b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1142d763cef2SBarry Smith 
11433f9fe445SBarry Smith    Logically Collective on TS
1144d763cef2SBarry Smith 
1145d763cef2SBarry Smith    Input Parameters:
1146d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1147e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1148e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1149d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1150d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11510298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1152d763cef2SBarry Smith 
1153f7ab8db6SBarry Smith    Calling sequence of f:
1154ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1155d763cef2SBarry Smith 
1156d763cef2SBarry Smith +  t - current timestep
1157d763cef2SBarry Smith .  u - input vector
1158e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1159e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1160d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1161d763cef2SBarry Smith 
11626cd88445SBarry Smith    Notes:
11636cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11646cd88445SBarry Smith 
11656cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1166ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1167d763cef2SBarry Smith 
1168d763cef2SBarry Smith    Level: beginner
1169d763cef2SBarry Smith 
1170d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1171d763cef2SBarry Smith 
1172ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1173d763cef2SBarry Smith 
1174d763cef2SBarry Smith @*/
1175e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1176d763cef2SBarry Smith {
1177277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1178089b2837SJed Brown   SNES           snes;
117924989b8cSPeter Brune   DM             dm;
118024989b8cSPeter Brune   TSIJacobian    ijacobian;
1181277b19d0SLisandro Dalcin 
1182d763cef2SBarry Smith   PetscFunctionBegin;
11830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1184e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1185e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1186e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1187e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1188d763cef2SBarry Smith 
118924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
119024989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1191e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1192e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1193e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1194e1244c69SJed Brown   }
11950298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1196089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11975f659677SPeter Brune   if (!ijacobian) {
1198e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
11990e4ef248SJed Brown   }
1200e5d3d808SBarry Smith   if (Amat) {
1201e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12020e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1203e5d3d808SBarry Smith     ts->Arhs = Amat;
12040e4ef248SJed Brown   }
1205e5d3d808SBarry Smith   if (Pmat) {
1206e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12070e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1208e5d3d808SBarry Smith     ts->Brhs = Pmat;
12090e4ef248SJed Brown   }
1210d763cef2SBarry Smith   PetscFunctionReturn(0);
1211d763cef2SBarry Smith }
1212d763cef2SBarry Smith 
1213316643e7SJed Brown 
1214316643e7SJed Brown /*@C
1215b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1216316643e7SJed Brown 
12173f9fe445SBarry Smith    Logically Collective on TS
1218316643e7SJed Brown 
1219316643e7SJed Brown    Input Parameters:
1220316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12210298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1222316643e7SJed Brown .  f   - the function evaluation routine
12230298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1224316643e7SJed Brown 
1225316643e7SJed Brown    Calling sequence of f:
1226316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1227316643e7SJed Brown 
1228316643e7SJed Brown +  t   - time at step/stage being solved
1229316643e7SJed Brown .  u   - state vector
1230316643e7SJed Brown .  u_t - time derivative of state vector
1231316643e7SJed Brown .  F   - function vector
1232316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1233316643e7SJed Brown 
1234316643e7SJed Brown    Important:
12352bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1236316643e7SJed Brown 
1237316643e7SJed Brown    Level: beginner
1238316643e7SJed Brown 
1239316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1240316643e7SJed Brown 
1241d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1242316643e7SJed Brown @*/
124351699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1244316643e7SJed Brown {
1245089b2837SJed Brown   PetscErrorCode ierr;
1246089b2837SJed Brown   SNES           snes;
124751699248SLisandro Dalcin   Vec            ralloc = NULL;
124824989b8cSPeter Brune   DM             dm;
1249316643e7SJed Brown 
1250316643e7SJed Brown   PetscFunctionBegin;
12510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
125251699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
125324989b8cSPeter Brune 
125424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
125524989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
125624989b8cSPeter Brune 
1257089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
125851699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
125951699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
126051699248SLisandro Dalcin     r  = ralloc;
1261e856ceecSJed Brown   }
126251699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
126351699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1264089b2837SJed Brown   PetscFunctionReturn(0);
1265089b2837SJed Brown }
1266089b2837SJed Brown 
1267089b2837SJed Brown /*@C
1268089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1269089b2837SJed Brown 
1270089b2837SJed Brown    Not Collective
1271089b2837SJed Brown 
1272089b2837SJed Brown    Input Parameter:
1273089b2837SJed Brown .  ts - the TS context
1274089b2837SJed Brown 
1275089b2837SJed Brown    Output Parameter:
12760298fd71SBarry Smith +  r - vector to hold residual (or NULL)
12770298fd71SBarry Smith .  func - the function to compute residual (or NULL)
12780298fd71SBarry Smith -  ctx - the function context (or NULL)
1279089b2837SJed Brown 
1280089b2837SJed Brown    Level: advanced
1281089b2837SJed Brown 
1282089b2837SJed Brown .keywords: TS, nonlinear, get, function
1283089b2837SJed Brown 
1284089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1285089b2837SJed Brown @*/
1286089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1287089b2837SJed Brown {
1288089b2837SJed Brown   PetscErrorCode ierr;
1289089b2837SJed Brown   SNES           snes;
129024989b8cSPeter Brune   DM             dm;
1291089b2837SJed Brown 
1292089b2837SJed Brown   PetscFunctionBegin;
1293089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1294089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12950298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
129624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
129724989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1298089b2837SJed Brown   PetscFunctionReturn(0);
1299089b2837SJed Brown }
1300089b2837SJed Brown 
1301089b2837SJed Brown /*@C
1302089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1303089b2837SJed Brown 
1304089b2837SJed Brown    Not Collective
1305089b2837SJed Brown 
1306089b2837SJed Brown    Input Parameter:
1307089b2837SJed Brown .  ts - the TS context
1308089b2837SJed Brown 
1309089b2837SJed Brown    Output Parameter:
13100298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13110298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13120298fd71SBarry Smith -  ctx - the function context (or NULL)
1313089b2837SJed Brown 
1314089b2837SJed Brown    Level: advanced
1315089b2837SJed Brown 
1316089b2837SJed Brown .keywords: TS, nonlinear, get, function
1317089b2837SJed Brown 
13182bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1319089b2837SJed Brown @*/
1320089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1321089b2837SJed Brown {
1322089b2837SJed Brown   PetscErrorCode ierr;
1323089b2837SJed Brown   SNES           snes;
132424989b8cSPeter Brune   DM             dm;
1325089b2837SJed Brown 
1326089b2837SJed Brown   PetscFunctionBegin;
1327089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1328089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13290298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
133024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
133124989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1332316643e7SJed Brown   PetscFunctionReturn(0);
1333316643e7SJed Brown }
1334316643e7SJed Brown 
1335316643e7SJed Brown /*@C
1336a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1337ae8867d6SBarry Smith         provided with TSSetIFunction().
1338316643e7SJed Brown 
13393f9fe445SBarry Smith    Logically Collective on TS
1340316643e7SJed Brown 
1341316643e7SJed Brown    Input Parameters:
1342316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1343e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1344e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1345316643e7SJed Brown .  f   - the Jacobian evaluation routine
13460298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1347316643e7SJed Brown 
1348316643e7SJed Brown    Calling sequence of f:
1349ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1350316643e7SJed Brown 
1351316643e7SJed Brown +  t    - time at step/stage being solved
13521b4a444bSJed Brown .  U    - state vector
13531b4a444bSJed Brown .  U_t  - time derivative of state vector
1354316643e7SJed Brown .  a    - shift
1355e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1356e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1357316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1358316643e7SJed Brown 
1359316643e7SJed Brown    Notes:
1360e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1361316643e7SJed Brown 
1362895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1363895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1364895c21f2SBarry Smith 
1365a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1366b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1367a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1368a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1369a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1370a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1371a4f0a591SBarry Smith 
13726cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13736cd88445SBarry Smith 
13746cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1375ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1376ca5f011dSBarry Smith 
1377316643e7SJed Brown    Level: beginner
1378316643e7SJed Brown 
1379316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1380316643e7SJed Brown 
1381ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1382316643e7SJed Brown 
1383316643e7SJed Brown @*/
1384e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1385316643e7SJed Brown {
1386316643e7SJed Brown   PetscErrorCode ierr;
1387089b2837SJed Brown   SNES           snes;
138824989b8cSPeter Brune   DM             dm;
1389316643e7SJed Brown 
1390316643e7SJed Brown   PetscFunctionBegin;
13910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1392e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1393e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1394e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1395e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
139624989b8cSPeter Brune 
139724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
139824989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
139924989b8cSPeter Brune 
1400089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1401e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1402316643e7SJed Brown   PetscFunctionReturn(0);
1403316643e7SJed Brown }
1404316643e7SJed Brown 
1405e1244c69SJed Brown /*@
1406e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1407e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1408e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1409e1244c69SJed Brown    not been changed by the TS.
1410e1244c69SJed Brown 
1411e1244c69SJed Brown    Logically Collective
1412e1244c69SJed Brown 
1413e1244c69SJed Brown    Input Arguments:
1414e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1415e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1416e1244c69SJed Brown 
1417e1244c69SJed Brown    Level: intermediate
1418e1244c69SJed Brown 
1419e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1420e1244c69SJed Brown @*/
1421e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1422e1244c69SJed Brown {
1423e1244c69SJed Brown   PetscFunctionBegin;
1424e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1425e1244c69SJed Brown   PetscFunctionReturn(0);
1426e1244c69SJed Brown }
1427e1244c69SJed Brown 
1428efe9872eSLisandro Dalcin /*@C
1429efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1430efe9872eSLisandro Dalcin 
1431efe9872eSLisandro Dalcin    Logically Collective on TS
1432efe9872eSLisandro Dalcin 
1433efe9872eSLisandro Dalcin    Input Parameters:
1434efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1435efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1436efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1437efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1438efe9872eSLisandro Dalcin 
1439efe9872eSLisandro Dalcin    Calling sequence of fun:
1440efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1441efe9872eSLisandro Dalcin 
1442efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1443efe9872eSLisandro Dalcin .  U    - state vector
1444efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1445efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1446efe9872eSLisandro Dalcin .  F    - function vector
1447efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1448efe9872eSLisandro Dalcin 
1449efe9872eSLisandro Dalcin    Level: beginner
1450efe9872eSLisandro Dalcin 
1451efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1452efe9872eSLisandro Dalcin 
1453efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1454efe9872eSLisandro Dalcin @*/
1455efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1456efe9872eSLisandro Dalcin {
1457efe9872eSLisandro Dalcin   DM             dm;
1458efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1459efe9872eSLisandro Dalcin 
1460efe9872eSLisandro Dalcin   PetscFunctionBegin;
1461efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1462efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1463efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1464efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1465efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1466efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1467efe9872eSLisandro Dalcin }
1468efe9872eSLisandro Dalcin 
1469efe9872eSLisandro Dalcin /*@C
1470efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1471efe9872eSLisandro Dalcin 
1472efe9872eSLisandro Dalcin   Not Collective
1473efe9872eSLisandro Dalcin 
1474efe9872eSLisandro Dalcin   Input Parameter:
1475efe9872eSLisandro Dalcin . ts - the TS context
1476efe9872eSLisandro Dalcin 
1477efe9872eSLisandro Dalcin   Output Parameter:
1478efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1479efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1480efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1481efe9872eSLisandro Dalcin 
1482efe9872eSLisandro Dalcin   Level: advanced
1483efe9872eSLisandro Dalcin 
1484efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1485efe9872eSLisandro Dalcin 
1486efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1487efe9872eSLisandro Dalcin @*/
1488efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1489efe9872eSLisandro Dalcin {
1490efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1491efe9872eSLisandro Dalcin   SNES           snes;
1492efe9872eSLisandro Dalcin   DM             dm;
1493efe9872eSLisandro Dalcin 
1494efe9872eSLisandro Dalcin   PetscFunctionBegin;
1495efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1496efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1497efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1498efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1499efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1500efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1501efe9872eSLisandro Dalcin }
1502efe9872eSLisandro Dalcin 
1503efe9872eSLisandro Dalcin /*@C
1504bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1505efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1506efe9872eSLisandro Dalcin 
1507efe9872eSLisandro Dalcin    Logically Collective on TS
1508efe9872eSLisandro Dalcin 
1509efe9872eSLisandro Dalcin    Input Parameters:
1510efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1511efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1512efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1513efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1514efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1515efe9872eSLisandro Dalcin 
1516efe9872eSLisandro Dalcin    Calling sequence of jac:
1517bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1518efe9872eSLisandro Dalcin 
1519efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1520efe9872eSLisandro Dalcin .  U    - state vector
1521efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1522efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1523efe9872eSLisandro Dalcin .  v    - shift for U_t
1524efe9872eSLisandro Dalcin .  a    - shift for U_tt
1525efe9872eSLisandro 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
1526efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1527efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1528efe9872eSLisandro Dalcin 
1529efe9872eSLisandro Dalcin    Notes:
1530efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1531efe9872eSLisandro Dalcin 
1532efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1533efe9872eSLisandro 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.
1534efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1535bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1536efe9872eSLisandro Dalcin 
1537efe9872eSLisandro Dalcin    Level: beginner
1538efe9872eSLisandro Dalcin 
1539efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1540efe9872eSLisandro Dalcin 
1541efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1542efe9872eSLisandro Dalcin @*/
1543efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1544efe9872eSLisandro Dalcin {
1545efe9872eSLisandro Dalcin   DM             dm;
1546efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1547efe9872eSLisandro Dalcin 
1548efe9872eSLisandro Dalcin   PetscFunctionBegin;
1549efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1550efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1551efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1552efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1553efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1554efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1555efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1556efe9872eSLisandro Dalcin }
1557efe9872eSLisandro Dalcin 
1558efe9872eSLisandro Dalcin /*@C
1559efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1560efe9872eSLisandro Dalcin 
1561efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1562efe9872eSLisandro Dalcin 
1563efe9872eSLisandro Dalcin   Input Parameter:
1564efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1565efe9872eSLisandro Dalcin 
1566efe9872eSLisandro Dalcin   Output Parameters:
1567efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1568efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1569efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1570efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1571efe9872eSLisandro Dalcin 
1572efe9872eSLisandro Dalcin   Notes: You can pass in NULL for any return argument you do not need.
1573efe9872eSLisandro Dalcin 
1574efe9872eSLisandro Dalcin   Level: advanced
1575efe9872eSLisandro Dalcin 
1576efe9872eSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
1577efe9872eSLisandro Dalcin 
1578efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1579efe9872eSLisandro Dalcin @*/
1580efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1581efe9872eSLisandro Dalcin {
1582efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1583efe9872eSLisandro Dalcin   SNES           snes;
1584efe9872eSLisandro Dalcin   DM             dm;
1585efe9872eSLisandro Dalcin 
1586efe9872eSLisandro Dalcin   PetscFunctionBegin;
1587efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1588efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1589efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1590efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1591efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1592efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1593efe9872eSLisandro Dalcin }
1594efe9872eSLisandro Dalcin 
1595efe9872eSLisandro Dalcin /*@
1596efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1597efe9872eSLisandro Dalcin 
1598efe9872eSLisandro Dalcin   Collective on TS and Vec
1599efe9872eSLisandro Dalcin 
1600efe9872eSLisandro Dalcin   Input Parameters:
1601efe9872eSLisandro Dalcin + ts - the TS context
1602efe9872eSLisandro Dalcin . t - current time
1603efe9872eSLisandro Dalcin . U - state vector
1604efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1605efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1606efe9872eSLisandro Dalcin 
1607efe9872eSLisandro Dalcin   Output Parameter:
1608efe9872eSLisandro Dalcin . F - the residual vector
1609efe9872eSLisandro Dalcin 
1610efe9872eSLisandro Dalcin   Note:
1611efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1612efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1613efe9872eSLisandro Dalcin 
1614efe9872eSLisandro Dalcin   Level: developer
1615efe9872eSLisandro Dalcin 
1616efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1617efe9872eSLisandro Dalcin 
1618efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1619efe9872eSLisandro Dalcin @*/
1620efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1621efe9872eSLisandro Dalcin {
1622efe9872eSLisandro Dalcin   DM             dm;
1623efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1624efe9872eSLisandro Dalcin   void           *ctx;
1625efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1626efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1627efe9872eSLisandro Dalcin 
1628efe9872eSLisandro Dalcin   PetscFunctionBegin;
1629efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1630efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1631efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1632efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1633efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1634efe9872eSLisandro Dalcin 
1635efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1636efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1637efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1638efe9872eSLisandro Dalcin 
1639efe9872eSLisandro Dalcin   if (!I2Function) {
1640efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1641efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1642efe9872eSLisandro Dalcin   }
1643efe9872eSLisandro Dalcin 
1644efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1645efe9872eSLisandro Dalcin 
1646efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1647efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1648efe9872eSLisandro Dalcin   PetscStackPop;
1649efe9872eSLisandro Dalcin 
1650efe9872eSLisandro Dalcin   if (rhsfunction) {
1651efe9872eSLisandro Dalcin     Vec Frhs;
1652efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1653efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1654efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1655efe9872eSLisandro Dalcin   }
1656efe9872eSLisandro Dalcin 
1657efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1658efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1659efe9872eSLisandro Dalcin }
1660efe9872eSLisandro Dalcin 
1661efe9872eSLisandro Dalcin /*@
1662efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1663efe9872eSLisandro Dalcin 
1664efe9872eSLisandro Dalcin   Collective on TS and Vec
1665efe9872eSLisandro Dalcin 
1666efe9872eSLisandro Dalcin   Input Parameters:
1667efe9872eSLisandro Dalcin + ts - the TS context
1668efe9872eSLisandro Dalcin . t - current timestep
1669efe9872eSLisandro Dalcin . U - state vector
1670efe9872eSLisandro Dalcin . V - time derivative of state vector
1671efe9872eSLisandro Dalcin . A - second time derivative of state vector
1672efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1673efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1674efe9872eSLisandro Dalcin 
1675efe9872eSLisandro Dalcin   Output Parameters:
1676efe9872eSLisandro Dalcin + J - Jacobian matrix
1677efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1678efe9872eSLisandro Dalcin 
1679efe9872eSLisandro Dalcin   Notes:
1680efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1681efe9872eSLisandro Dalcin 
1682efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1683efe9872eSLisandro Dalcin 
1684efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1685efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1686efe9872eSLisandro Dalcin 
1687efe9872eSLisandro Dalcin   Level: developer
1688efe9872eSLisandro Dalcin 
1689efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1690efe9872eSLisandro Dalcin 
1691efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1692efe9872eSLisandro Dalcin @*/
1693efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1694efe9872eSLisandro Dalcin {
1695efe9872eSLisandro Dalcin   DM             dm;
1696efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1697efe9872eSLisandro Dalcin   void           *ctx;
1698efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1699efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1700efe9872eSLisandro Dalcin 
1701efe9872eSLisandro Dalcin   PetscFunctionBegin;
1702efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1703efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1704efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1705efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1706efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1707efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1708efe9872eSLisandro Dalcin 
1709efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1710efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1711efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1712efe9872eSLisandro Dalcin 
1713efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1714efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1715efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1716efe9872eSLisandro Dalcin   }
1717efe9872eSLisandro Dalcin 
1718efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1719efe9872eSLisandro Dalcin 
1720efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1721efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1722efe9872eSLisandro Dalcin   PetscStackPop;
1723efe9872eSLisandro Dalcin 
1724efe9872eSLisandro Dalcin   if (rhsjacobian) {
1725efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1726efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1727efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1728efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1729efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1730efe9872eSLisandro Dalcin   }
1731efe9872eSLisandro Dalcin 
1732efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1733efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1734efe9872eSLisandro Dalcin }
1735efe9872eSLisandro Dalcin 
1736efe9872eSLisandro Dalcin /*@
1737efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1738efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1739efe9872eSLisandro Dalcin 
1740efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1741efe9872eSLisandro Dalcin 
1742efe9872eSLisandro Dalcin    Input Parameters:
1743efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1744efe9872eSLisandro Dalcin .  u - the solution vector
1745efe9872eSLisandro Dalcin -  v - the time derivative vector
1746efe9872eSLisandro Dalcin 
1747efe9872eSLisandro Dalcin    Level: beginner
1748efe9872eSLisandro Dalcin 
1749efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1750efe9872eSLisandro Dalcin @*/
1751efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1752efe9872eSLisandro Dalcin {
1753efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1754efe9872eSLisandro Dalcin 
1755efe9872eSLisandro Dalcin   PetscFunctionBegin;
1756efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1757efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1758efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1759efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1760efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1761efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1762efe9872eSLisandro Dalcin   ts->vec_dot = v;
1763efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1764efe9872eSLisandro Dalcin }
1765efe9872eSLisandro Dalcin 
1766efe9872eSLisandro Dalcin /*@
1767efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1768efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1769efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1770efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1771efe9872eSLisandro Dalcin 
1772efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1773efe9872eSLisandro Dalcin 
1774efe9872eSLisandro Dalcin    Input Parameter:
1775efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1776efe9872eSLisandro Dalcin 
1777efe9872eSLisandro Dalcin    Output Parameter:
1778efe9872eSLisandro Dalcin +  u - the vector containing the solution
1779efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1780efe9872eSLisandro Dalcin 
1781efe9872eSLisandro Dalcin    Level: intermediate
1782efe9872eSLisandro Dalcin 
1783efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1784efe9872eSLisandro Dalcin 
1785efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1786efe9872eSLisandro Dalcin @*/
1787efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1788efe9872eSLisandro Dalcin {
1789efe9872eSLisandro Dalcin   PetscFunctionBegin;
1790efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1791efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1792efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1793efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1794efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1795efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1796efe9872eSLisandro Dalcin }
1797efe9872eSLisandro Dalcin 
179855849f57SBarry Smith /*@C
179955849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
180055849f57SBarry Smith 
180155849f57SBarry Smith   Collective on PetscViewer
180255849f57SBarry Smith 
180355849f57SBarry Smith   Input Parameters:
180455849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
180555849f57SBarry Smith            some related function before a call to TSLoad().
180655849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
180755849f57SBarry Smith 
180855849f57SBarry Smith    Level: intermediate
180955849f57SBarry Smith 
181055849f57SBarry Smith   Notes:
181155849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
181255849f57SBarry Smith 
181355849f57SBarry Smith   Notes for advanced users:
181455849f57SBarry Smith   Most users should not need to know the details of the binary storage
181555849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
181655849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
181755849f57SBarry Smith   format is
181855849f57SBarry Smith .vb
181955849f57SBarry Smith      has not yet been determined
182055849f57SBarry Smith .ve
182155849f57SBarry Smith 
182255849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
182355849f57SBarry Smith @*/
1824f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
182555849f57SBarry Smith {
182655849f57SBarry Smith   PetscErrorCode ierr;
182755849f57SBarry Smith   PetscBool      isbinary;
182855849f57SBarry Smith   PetscInt       classid;
182955849f57SBarry Smith   char           type[256];
18302d53ad75SBarry Smith   DMTS           sdm;
1831ad6bc421SBarry Smith   DM             dm;
183255849f57SBarry Smith 
183355849f57SBarry Smith   PetscFunctionBegin;
1834f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
183555849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
183655849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
183755849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
183855849f57SBarry Smith 
1839060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1840ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1841060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1842f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1843f2c2a1b9SBarry Smith   if (ts->ops->load) {
1844f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1845f2c2a1b9SBarry Smith   }
1846ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1847ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1848ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1849f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1850f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
18512d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18522d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
185355849f57SBarry Smith   PetscFunctionReturn(0);
185455849f57SBarry Smith }
185555849f57SBarry Smith 
18569804daf3SBarry Smith #include <petscdraw.h>
1857e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1858e04113cfSBarry Smith #include <petscviewersaws.h>
1859f05ece33SBarry Smith #endif
18607e2c5f70SBarry Smith /*@C
1861d763cef2SBarry Smith     TSView - Prints the TS data structure.
1862d763cef2SBarry Smith 
18634c49b128SBarry Smith     Collective on TS
1864d763cef2SBarry Smith 
1865d763cef2SBarry Smith     Input Parameters:
1866d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1867d763cef2SBarry Smith -   viewer - visualization context
1868d763cef2SBarry Smith 
1869d763cef2SBarry Smith     Options Database Key:
1870d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1871d763cef2SBarry Smith 
1872d763cef2SBarry Smith     Notes:
1873d763cef2SBarry Smith     The available visualization contexts include
1874b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1875b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1876d763cef2SBarry Smith          output where only the first processor opens
1877d763cef2SBarry Smith          the file.  All other processors send their
1878d763cef2SBarry Smith          data to the first processor to print.
1879d763cef2SBarry Smith 
1880d763cef2SBarry Smith     The user can open an alternative visualization context with
1881b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1882d763cef2SBarry Smith 
1883d763cef2SBarry Smith     Level: beginner
1884d763cef2SBarry Smith 
1885d763cef2SBarry Smith .keywords: TS, timestep, view
1886d763cef2SBarry Smith 
1887b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1888d763cef2SBarry Smith @*/
18897087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1890d763cef2SBarry Smith {
1891dfbe8321SBarry Smith   PetscErrorCode ierr;
189219fd82e9SBarry Smith   TSType         type;
18932b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
18942d53ad75SBarry Smith   DMTS           sdm;
1895e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1896536b137fSBarry Smith   PetscBool      issaws;
1897f05ece33SBarry Smith #endif
1898d763cef2SBarry Smith 
1899d763cef2SBarry Smith   PetscFunctionBegin;
19000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19013050cee2SBarry Smith   if (!viewer) {
1902ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
19033050cee2SBarry Smith   }
19040700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1905c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1906fd16b177SBarry Smith 
1907251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1908251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
190955849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19102b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1911e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1912536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1913f05ece33SBarry Smith #endif
191432077d6dSBarry Smith   if (iascii) {
1915dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
1916*efd4aadfSBarry Smith     if (ts->ops->view) {
1917*efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1918*efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1919*efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1920*efd4aadfSBarry Smith     }
192177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
19227c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1923*efd4aadfSBarry Smith     if (ts->usessnes) {
1924*efd4aadfSBarry Smith       PetscBool lin;
1925d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
19265ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1927d763cef2SBarry Smith       }
19285ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1929*efd4aadfSBarry Smith       ierr = PetscObjectTypeCompare((PetscObject)ts->snes,SNESKSPONLY,&lin);CHKERRQ(ierr);
1930*efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
1931*efd4aadfSBarry Smith     }
1932193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1933a0af407cSBarry Smith     if (ts->vrtol) {
1934a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1935a0af407cSBarry Smith     } else {
1936a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
1937a0af407cSBarry Smith     }
1938a0af407cSBarry Smith     if (ts->vatol) {
1939a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
1940a0af407cSBarry Smith     } else {
1941a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
1942a0af407cSBarry Smith     }
1943*efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
1944*efd4aadfSBarry Smith     if (ts->snes && ts->usessnes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
19452d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19462d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19470f5bd95cSBarry Smith   } else if (isstring) {
1948a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1949b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
195055849f57SBarry Smith   } else if (isbinary) {
195155849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
195255849f57SBarry Smith     MPI_Comm    comm;
195355849f57SBarry Smith     PetscMPIInt rank;
195455849f57SBarry Smith     char        type[256];
195555849f57SBarry Smith 
195655849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
195755849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
195855849f57SBarry Smith     if (!rank) {
195955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
196055849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
196155849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
196255849f57SBarry Smith     }
196355849f57SBarry Smith     if (ts->ops->view) {
196455849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
196555849f57SBarry Smith     }
1966*efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
1967f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1968f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
19692d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19702d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19712b0a91c0SBarry Smith   } else if (isdraw) {
19722b0a91c0SBarry Smith     PetscDraw draw;
19732b0a91c0SBarry Smith     char      str[36];
197489fd9fafSBarry Smith     PetscReal x,y,bottom,h;
19752b0a91c0SBarry Smith 
19762b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
19772b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
19782b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
19792b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
198051fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
198189fd9fafSBarry Smith     bottom = y - h;
19822b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
19832b0a91c0SBarry Smith     if (ts->ops->view) {
19842b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
19852b0a91c0SBarry Smith     }
1986*efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
1987*efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
19882b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1989e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1990536b137fSBarry Smith   } else if (issaws) {
1991d45a07a7SBarry Smith     PetscMPIInt rank;
19922657e9d9SBarry Smith     const char  *name;
19932657e9d9SBarry Smith 
19942657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1995d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1996d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1997d45a07a7SBarry Smith       char       dir[1024];
1998d45a07a7SBarry Smith 
1999e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2000a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
20012657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
20022657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
20032657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2004d763cef2SBarry Smith     }
20050acecf5bSBarry Smith     if (ts->ops->view) {
20060acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20070acecf5bSBarry Smith     }
2008f05ece33SBarry Smith #endif
2009f05ece33SBarry Smith   }
2010f05ece33SBarry Smith 
2011b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2012251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2013b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2014d763cef2SBarry Smith   PetscFunctionReturn(0);
2015d763cef2SBarry Smith }
2016d763cef2SBarry Smith 
2017d763cef2SBarry Smith 
2018b07ff414SBarry Smith /*@
2019d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2020d763cef2SBarry Smith    the timesteppers.
2021d763cef2SBarry Smith 
20223f9fe445SBarry Smith    Logically Collective on TS
2023d763cef2SBarry Smith 
2024d763cef2SBarry Smith    Input Parameters:
2025d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2026d763cef2SBarry Smith -  usrP - optional user context
2027d763cef2SBarry Smith 
2028daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2029daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2030daf670e6SBarry Smith 
2031d763cef2SBarry Smith    Level: intermediate
2032d763cef2SBarry Smith 
2033d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
2034d763cef2SBarry Smith 
2035d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2036d763cef2SBarry Smith @*/
20377087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2038d763cef2SBarry Smith {
2039d763cef2SBarry Smith   PetscFunctionBegin;
20400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2041d763cef2SBarry Smith   ts->user = usrP;
2042d763cef2SBarry Smith   PetscFunctionReturn(0);
2043d763cef2SBarry Smith }
2044d763cef2SBarry Smith 
2045b07ff414SBarry Smith /*@
2046d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2047d763cef2SBarry Smith     timestepper.
2048d763cef2SBarry Smith 
2049d763cef2SBarry Smith     Not Collective
2050d763cef2SBarry Smith 
2051d763cef2SBarry Smith     Input Parameter:
2052d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2053d763cef2SBarry Smith 
2054d763cef2SBarry Smith     Output Parameter:
2055d763cef2SBarry Smith .   usrP - user context
2056d763cef2SBarry Smith 
2057daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2058daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2059daf670e6SBarry Smith 
2060d763cef2SBarry Smith     Level: intermediate
2061d763cef2SBarry Smith 
2062d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2063d763cef2SBarry Smith 
2064d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2065d763cef2SBarry Smith @*/
2066e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2067d763cef2SBarry Smith {
2068d763cef2SBarry Smith   PetscFunctionBegin;
20690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2070e71120c6SJed Brown   *(void**)usrP = ts->user;
2071d763cef2SBarry Smith   PetscFunctionReturn(0);
2072d763cef2SBarry Smith }
2073d763cef2SBarry Smith 
2074d763cef2SBarry Smith /*@
2075b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
2076d763cef2SBarry Smith 
2077d763cef2SBarry Smith    Not Collective
2078d763cef2SBarry Smith 
2079d763cef2SBarry Smith    Input Parameter:
2080d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2081d763cef2SBarry Smith 
2082d763cef2SBarry Smith    Output Parameter:
2083b8123daeSJed Brown .  iter - number of steps completed so far
2084d763cef2SBarry Smith 
2085d763cef2SBarry Smith    Level: intermediate
2086d763cef2SBarry Smith 
2087d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
20889be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2089d763cef2SBarry Smith @*/
20907087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
2091d763cef2SBarry Smith {
2092d763cef2SBarry Smith   PetscFunctionBegin;
20930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20944482741eSBarry Smith   PetscValidIntPointer(iter,2);
2095d763cef2SBarry Smith   *iter = ts->steps;
2096d763cef2SBarry Smith   PetscFunctionReturn(0);
2097d763cef2SBarry Smith }
2098d763cef2SBarry Smith 
2099d763cef2SBarry Smith /*@
2100d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
2101d763cef2SBarry Smith    as well as the initial time.
2102d763cef2SBarry Smith 
21033f9fe445SBarry Smith    Logically Collective on TS
2104d763cef2SBarry Smith 
2105d763cef2SBarry Smith    Input Parameters:
2106d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2107d763cef2SBarry Smith .  initial_time - the initial time
2108d763cef2SBarry Smith -  time_step - the size of the timestep
2109d763cef2SBarry Smith 
2110d763cef2SBarry Smith    Level: intermediate
2111d763cef2SBarry Smith 
2112d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
2113d763cef2SBarry Smith 
2114d763cef2SBarry Smith .keywords: TS, set, initial, timestep
2115d763cef2SBarry Smith @*/
21167087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
2117d763cef2SBarry Smith {
2118e144a568SJed Brown   PetscErrorCode ierr;
2119e144a568SJed Brown 
2120d763cef2SBarry Smith   PetscFunctionBegin;
21210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2122e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
2123d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
2124d763cef2SBarry Smith   PetscFunctionReturn(0);
2125d763cef2SBarry Smith }
2126d763cef2SBarry Smith 
2127d763cef2SBarry Smith /*@
2128d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2129d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2130d763cef2SBarry Smith 
21313f9fe445SBarry Smith    Logically Collective on TS
2132d763cef2SBarry Smith 
2133d763cef2SBarry Smith    Input Parameters:
2134d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2135d763cef2SBarry Smith -  time_step - the size of the timestep
2136d763cef2SBarry Smith 
2137d763cef2SBarry Smith    Level: intermediate
2138d763cef2SBarry Smith 
2139d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2140d763cef2SBarry Smith 
2141d763cef2SBarry Smith .keywords: TS, set, timestep
2142d763cef2SBarry Smith @*/
21437087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2144d763cef2SBarry Smith {
2145d763cef2SBarry Smith   PetscFunctionBegin;
21460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2147c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2148d763cef2SBarry Smith   ts->time_step = time_step;
2149d763cef2SBarry Smith   PetscFunctionReturn(0);
2150d763cef2SBarry Smith }
2151d763cef2SBarry Smith 
2152a43b19c4SJed Brown /*@
215349354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
215449354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
215549354f04SShri Abhyankar      or just return at the final time TS computed.
2156a43b19c4SJed Brown 
2157a43b19c4SJed Brown   Logically Collective on TS
2158a43b19c4SJed Brown 
2159a43b19c4SJed Brown    Input Parameter:
2160a43b19c4SJed Brown +   ts - the time-step context
216149354f04SShri Abhyankar -   eftopt - exact final time option
2162a43b19c4SJed Brown 
2163feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2164feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2165feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2166feed9e9dSBarry Smith 
2167feed9e9dSBarry Smith    Options Database:
2168feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2169feed9e9dSBarry Smith 
2170ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2171ee346746SBarry Smith     then the final time you selected.
2172ee346746SBarry Smith 
2173a43b19c4SJed Brown    Level: beginner
2174a43b19c4SJed Brown 
2175a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
2176a43b19c4SJed Brown @*/
217749354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2178a43b19c4SJed Brown {
2179a43b19c4SJed Brown   PetscFunctionBegin;
2180a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
218149354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
218249354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2183a43b19c4SJed Brown   PetscFunctionReturn(0);
2184a43b19c4SJed Brown }
2185a43b19c4SJed Brown 
2186d763cef2SBarry Smith /*@
2187d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2188d763cef2SBarry Smith 
2189d763cef2SBarry Smith    Not Collective
2190d763cef2SBarry Smith 
2191d763cef2SBarry Smith    Input Parameter:
2192d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2193d763cef2SBarry Smith 
2194d763cef2SBarry Smith    Output Parameter:
2195d763cef2SBarry Smith .  dt - the current timestep size
2196d763cef2SBarry Smith 
2197d763cef2SBarry Smith    Level: intermediate
2198d763cef2SBarry Smith 
2199d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2200d763cef2SBarry Smith 
2201d763cef2SBarry Smith .keywords: TS, get, timestep
2202d763cef2SBarry Smith @*/
22037087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2204d763cef2SBarry Smith {
2205d763cef2SBarry Smith   PetscFunctionBegin;
22060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2207f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2208d763cef2SBarry Smith   *dt = ts->time_step;
2209d763cef2SBarry Smith   PetscFunctionReturn(0);
2210d763cef2SBarry Smith }
2211d763cef2SBarry Smith 
2212d8e5e3e6SSatish Balay /*@
2213d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2214d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2215d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2216d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2217d763cef2SBarry Smith 
2218d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2219d763cef2SBarry Smith 
2220d763cef2SBarry Smith    Input Parameter:
2221d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2222d763cef2SBarry Smith 
2223d763cef2SBarry Smith    Output Parameter:
2224d763cef2SBarry Smith .  v - the vector containing the solution
2225d763cef2SBarry Smith 
222663e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
222763e21af5SBarry Smith    final time. It returns the solution at the next timestep.
222863e21af5SBarry Smith 
2229d763cef2SBarry Smith    Level: intermediate
2230d763cef2SBarry Smith 
2231b2bf4f3aSDebojyoti Ghosh .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents()
2232d763cef2SBarry Smith 
2233d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2234d763cef2SBarry Smith @*/
22357087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2236d763cef2SBarry Smith {
2237d763cef2SBarry Smith   PetscFunctionBegin;
22380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22394482741eSBarry Smith   PetscValidPointer(v,2);
22408737fe31SLisandro Dalcin   *v = ts->vec_sol;
2241d763cef2SBarry Smith   PetscFunctionReturn(0);
2242d763cef2SBarry Smith }
2243d763cef2SBarry Smith 
224403fe5f5eSDebojyoti Ghosh /*@
2245b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
224603fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2247b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
224803fe5f5eSDebojyoti Ghosh    structure as the solution vector.
224903fe5f5eSDebojyoti Ghosh 
225003fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
225103fe5f5eSDebojyoti Ghosh 
225203fe5f5eSDebojyoti Ghosh    Parameters :
225303fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2254b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2255b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
225603fe5f5eSDebojyoti Ghosh        returned in v.
2257b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
225803fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2259b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
226003fe5f5eSDebojyoti Ghosh 
22614cdd57e5SDebojyoti Ghosh    Level: advanced
226203fe5f5eSDebojyoti Ghosh 
226303fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
226403fe5f5eSDebojyoti Ghosh 
226503fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
226603fe5f5eSDebojyoti Ghosh @*/
2267b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
226803fe5f5eSDebojyoti Ghosh {
226903fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
227003fe5f5eSDebojyoti Ghosh 
227103fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
227203fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2273b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
227403fe5f5eSDebojyoti Ghosh   else {
2275b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
227603fe5f5eSDebojyoti Ghosh   }
227703fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
227803fe5f5eSDebojyoti Ghosh }
227903fe5f5eSDebojyoti Ghosh 
22804cdd57e5SDebojyoti Ghosh /*@
22814cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
22824cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
22834cdd57e5SDebojyoti Ghosh 
22844cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
22854cdd57e5SDebojyoti Ghosh 
22864cdd57e5SDebojyoti Ghosh    Parameters :
22874cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
22884cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
22894cdd57e5SDebojyoti Ghosh 
22904cdd57e5SDebojyoti Ghosh    Level: intermediate
22914cdd57e5SDebojyoti Ghosh 
22924cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
22934cdd57e5SDebojyoti Ghosh 
22944cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
22954cdd57e5SDebojyoti Ghosh @*/
22964cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
22974cdd57e5SDebojyoti Ghosh {
22984cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
22994cdd57e5SDebojyoti Ghosh 
23004cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23014cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2302f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
23034cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2304f6356ec7SDebojyoti Ghosh   } else {
2305f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2306f6356ec7SDebojyoti Ghosh   }
23074cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23084cdd57e5SDebojyoti Ghosh }
23094cdd57e5SDebojyoti Ghosh 
23104cdd57e5SDebojyoti Ghosh /*@
23114cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
23124cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
23134cdd57e5SDebojyoti Ghosh 
23144cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23154cdd57e5SDebojyoti Ghosh 
23169657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
23179657682dSDebojyoti Ghosh 
23184cdd57e5SDebojyoti Ghosh    Parameters :
23194cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2320657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
23214cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
23224cdd57e5SDebojyoti Ghosh 
23234cdd57e5SDebojyoti Ghosh    Level: intermediate
23244cdd57e5SDebojyoti Ghosh 
232557df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
23264cdd57e5SDebojyoti Ghosh 
23274cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
23284cdd57e5SDebojyoti Ghosh @*/
23290a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
23304cdd57e5SDebojyoti Ghosh {
23314cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23324cdd57e5SDebojyoti Ghosh 
23334cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23344cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2335f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
23360a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2337f6356ec7SDebojyoti Ghosh   } else {
2338f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2339f6356ec7SDebojyoti Ghosh   }
23404cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23414cdd57e5SDebojyoti Ghosh }
23424cdd57e5SDebojyoti Ghosh 
234357df6a1bSDebojyoti Ghosh /*@
234457df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
234557df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
234657df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
234757df6a1bSDebojyoti Ghosh 
234857df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
234957df6a1bSDebojyoti Ghosh 
235057df6a1bSDebojyoti Ghosh    Parameters :
235157df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
235257df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
235357df6a1bSDebojyoti Ghosh 
235457df6a1bSDebojyoti Ghosh    Level: intermediate
235557df6a1bSDebojyoti Ghosh 
235657df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
235757df6a1bSDebojyoti Ghosh 
235857df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
235957df6a1bSDebojyoti Ghosh @*/
236057df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
236157df6a1bSDebojyoti Ghosh {
236257df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
236357df6a1bSDebojyoti Ghosh 
236457df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
236557df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23669657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
236757df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
236857df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
236957df6a1bSDebojyoti Ghosh   }
237057df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
237157df6a1bSDebojyoti Ghosh }
237257df6a1bSDebojyoti Ghosh 
2373c235aa8dSHong Zhang /*@
2374dfb21088SHong Zhang    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()
2375c235aa8dSHong Zhang 
2376c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
2377c235aa8dSHong Zhang 
2378c235aa8dSHong Zhang    Input Parameter:
2379c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
2380c235aa8dSHong Zhang 
2381c235aa8dSHong Zhang    Output Parameter:
2382abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
2383abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
2384c235aa8dSHong Zhang 
2385c235aa8dSHong Zhang    Level: intermediate
2386c235aa8dSHong Zhang 
2387c235aa8dSHong Zhang .seealso: TSGetTimeStep()
2388c235aa8dSHong Zhang 
2389c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
2390c235aa8dSHong Zhang @*/
2391dfb21088SHong Zhang PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
2392c235aa8dSHong Zhang {
2393c235aa8dSHong Zhang   PetscFunctionBegin;
2394c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2395abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
2396abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
2397abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
2398c235aa8dSHong Zhang   PetscFunctionReturn(0);
2399c235aa8dSHong Zhang }
2400c235aa8dSHong Zhang 
2401bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2402d8e5e3e6SSatish Balay /*@
2403bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2404d763cef2SBarry Smith 
2405bdad233fSMatthew Knepley   Not collective
2406d763cef2SBarry Smith 
2407bdad233fSMatthew Knepley   Input Parameters:
2408bdad233fSMatthew Knepley + ts   - The TS
2409bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2410d763cef2SBarry Smith .vb
24110910c330SBarry Smith          U_t - A U = 0      (linear)
24120910c330SBarry Smith          U_t - A(t) U = 0   (linear)
24130910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2414d763cef2SBarry Smith .ve
2415d763cef2SBarry Smith 
2416d763cef2SBarry Smith    Level: beginner
2417d763cef2SBarry Smith 
2418bdad233fSMatthew Knepley .keywords: TS, problem type
2419bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2420d763cef2SBarry Smith @*/
24217087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2422a7cc72afSBarry Smith {
24239e2a6581SJed Brown   PetscErrorCode ierr;
24249e2a6581SJed Brown 
2425d763cef2SBarry Smith   PetscFunctionBegin;
24260700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2427bdad233fSMatthew Knepley   ts->problem_type = type;
24289e2a6581SJed Brown   if (type == TS_LINEAR) {
24299e2a6581SJed Brown     SNES snes;
24309e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
24319e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
24329e2a6581SJed Brown   }
2433d763cef2SBarry Smith   PetscFunctionReturn(0);
2434d763cef2SBarry Smith }
2435d763cef2SBarry Smith 
2436bdad233fSMatthew Knepley /*@C
2437bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2438bdad233fSMatthew Knepley 
2439bdad233fSMatthew Knepley   Not collective
2440bdad233fSMatthew Knepley 
2441bdad233fSMatthew Knepley   Input Parameter:
2442bdad233fSMatthew Knepley . ts   - The TS
2443bdad233fSMatthew Knepley 
2444bdad233fSMatthew Knepley   Output Parameter:
2445bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2446bdad233fSMatthew Knepley .vb
2447089b2837SJed Brown          M U_t = A U
2448089b2837SJed Brown          M(t) U_t = A(t) U
2449b5abc632SBarry Smith          F(t,U,U_t)
2450bdad233fSMatthew Knepley .ve
2451bdad233fSMatthew Knepley 
2452bdad233fSMatthew Knepley    Level: beginner
2453bdad233fSMatthew Knepley 
2454bdad233fSMatthew Knepley .keywords: TS, problem type
2455bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2456bdad233fSMatthew Knepley @*/
24577087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2458a7cc72afSBarry Smith {
2459bdad233fSMatthew Knepley   PetscFunctionBegin;
24600700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
24614482741eSBarry Smith   PetscValidIntPointer(type,2);
2462bdad233fSMatthew Knepley   *type = ts->problem_type;
2463bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2464bdad233fSMatthew Knepley }
2465d763cef2SBarry Smith 
2466d763cef2SBarry Smith /*@
2467d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2468d763cef2SBarry Smith    of a timestepper.
2469d763cef2SBarry Smith 
2470d763cef2SBarry Smith    Collective on TS
2471d763cef2SBarry Smith 
2472d763cef2SBarry Smith    Input Parameter:
2473d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2474d763cef2SBarry Smith 
2475d763cef2SBarry Smith    Notes:
2476d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2477d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2478d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
2479d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2480d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
2481d763cef2SBarry Smith 
2482d763cef2SBarry Smith    Level: advanced
2483d763cef2SBarry Smith 
2484d763cef2SBarry Smith .keywords: TS, timestep, setup
2485d763cef2SBarry Smith 
2486d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
2487d763cef2SBarry Smith @*/
24887087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2489d763cef2SBarry Smith {
2490dfbe8321SBarry Smith   PetscErrorCode ierr;
24916c6b9e74SPeter Brune   DM             dm;
24926c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2493d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2494cd11d68dSLisandro Dalcin   TSIFunction    ifun;
24956c6b9e74SPeter Brune   TSIJacobian    ijac;
2496efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
24976c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
24982ffb9264SLisandro Dalcin   PetscBool      isnone;
2499d763cef2SBarry Smith 
2500d763cef2SBarry Smith   PetscFunctionBegin;
25010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2502277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2503277b19d0SLisandro Dalcin 
25047adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2505cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2506cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2507d763cef2SBarry Smith   }
2508277b19d0SLisandro Dalcin 
2509277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2510277b19d0SLisandro Dalcin 
2511e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
2512e1244c69SJed Brown     Mat Amat,Pmat;
2513e1244c69SJed Brown     SNES snes;
2514e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2515e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2516e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2517e1244c69SJed Brown      * have displaced the RHS matrix */
2518e1244c69SJed Brown     if (Amat == ts->Arhs) {
2519e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2520e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2521e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2522e1244c69SJed Brown     }
2523e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2524e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2525e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2526e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2527e1244c69SJed Brown     }
2528e1244c69SJed Brown   }
25292ffb9264SLisandro Dalcin 
25302ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
25312ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
25322ffb9264SLisandro Dalcin 
2533277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2534000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2535277b19d0SLisandro Dalcin   }
2536277b19d0SLisandro Dalcin 
25372ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
25382ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
25392ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
25402ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
25412ffb9264SLisandro Dalcin 
2542a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
25436c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
25446c6b9e74SPeter Brune    */
25456c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
25460298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
25476c6b9e74SPeter Brune   if (!func) {
25486c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
25496c6b9e74SPeter Brune   }
2550a6772fa2SLisandro 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.
25516c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
25526c6b9e74SPeter Brune    */
25530298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
25540298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2555efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
25560298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2557efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
25586c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
25596c6b9e74SPeter Brune   }
2560c0517034SDebojyoti Ghosh 
2561c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2562c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2563c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2564c0517034SDebojyoti Ghosh   }
2565c0517034SDebojyoti Ghosh 
2566277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2567277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2568277b19d0SLisandro Dalcin }
2569277b19d0SLisandro Dalcin 
2570f6a906c0SBarry Smith /*@
2571f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
2572f6a906c0SBarry Smith    of an adjoint solver
2573f6a906c0SBarry Smith 
2574f6a906c0SBarry Smith    Collective on TS
2575f6a906c0SBarry Smith 
2576f6a906c0SBarry Smith    Input Parameter:
2577f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
2578f6a906c0SBarry Smith 
2579f6a906c0SBarry Smith    Level: advanced
2580f6a906c0SBarry Smith 
2581f6a906c0SBarry Smith .keywords: TS, timestep, setup
2582f6a906c0SBarry Smith 
2583947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
2584f6a906c0SBarry Smith @*/
2585f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
2586f6a906c0SBarry Smith {
2587f6a906c0SBarry Smith   PetscErrorCode ierr;
2588f6a906c0SBarry Smith 
2589f6a906c0SBarry Smith   PetscFunctionBegin;
2590f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2591f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
2592dfb21088SHong Zhang   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
2593d8151eeaSBarry Smith 
2594947abb85SHong Zhang   if (ts->vec_costintegral) { /* if there is integral in the cost function*/
2595d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2596d8151eeaSBarry Smith     if (ts->vecs_sensip){
2597d8151eeaSBarry Smith       ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2598d8151eeaSBarry Smith     }
2599947abb85SHong Zhang   }
2600d8151eeaSBarry Smith 
260142f2b339SBarry Smith   if (ts->ops->adjointsetup) {
260242f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
2603f6a906c0SBarry Smith   }
2604f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
2605f6a906c0SBarry Smith   PetscFunctionReturn(0);
2606f6a906c0SBarry Smith }
2607f6a906c0SBarry Smith 
2608277b19d0SLisandro Dalcin /*@
2609277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2610277b19d0SLisandro Dalcin 
2611277b19d0SLisandro Dalcin    Collective on TS
2612277b19d0SLisandro Dalcin 
2613277b19d0SLisandro Dalcin    Input Parameter:
2614277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2615277b19d0SLisandro Dalcin 
2616277b19d0SLisandro Dalcin    Level: beginner
2617277b19d0SLisandro Dalcin 
2618277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2619277b19d0SLisandro Dalcin 
2620277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2621277b19d0SLisandro Dalcin @*/
2622277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2623277b19d0SLisandro Dalcin {
2624277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2625277b19d0SLisandro Dalcin 
2626277b19d0SLisandro Dalcin   PetscFunctionBegin;
2627277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2628b18ea86cSHong Zhang 
2629277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2630277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2631277b19d0SLisandro Dalcin   }
2632277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2633e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2634bbd56ea5SKarl Rupp 
26354e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
26364e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2637214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
26386bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2639efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2640e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2641e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
264238637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2643bbd56ea5SKarl Rupp 
2644947abb85SHong Zhang  if (ts->vec_costintegral) {
2645d8151eeaSBarry Smith     ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2646d8151eeaSBarry Smith     if (ts->vecs_drdp){
2647d8151eeaSBarry Smith       ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2648d8151eeaSBarry Smith     }
2649947abb85SHong Zhang   }
2650d8151eeaSBarry Smith   ts->vecs_sensi  = NULL;
2651d8151eeaSBarry Smith   ts->vecs_sensip = NULL;
2652ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
26532c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
265436eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2655277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2656d763cef2SBarry Smith   PetscFunctionReturn(0);
2657d763cef2SBarry Smith }
2658d763cef2SBarry Smith 
2659d8e5e3e6SSatish Balay /*@
2660d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2661d763cef2SBarry Smith    with TSCreate().
2662d763cef2SBarry Smith 
2663d763cef2SBarry Smith    Collective on TS
2664d763cef2SBarry Smith 
2665d763cef2SBarry Smith    Input Parameter:
2666d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2667d763cef2SBarry Smith 
2668d763cef2SBarry Smith    Level: beginner
2669d763cef2SBarry Smith 
2670d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2671d763cef2SBarry Smith 
2672d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2673d763cef2SBarry Smith @*/
26746bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2675d763cef2SBarry Smith {
26766849ba73SBarry Smith   PetscErrorCode ierr;
2677d763cef2SBarry Smith 
2678d763cef2SBarry Smith   PetscFunctionBegin;
26796bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
26806bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
26816bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2682d763cef2SBarry Smith 
26836bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2684277b19d0SLisandro Dalcin 
2685e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2686e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
26876bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
26886d4c513bSLisandro Dalcin 
2689bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2690bc952696SBarry Smith 
269184df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
26926427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
26936427ac75SLisandro Dalcin 
26946bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
26956bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
26966bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
26970dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
26986d4c513bSLisandro Dalcin 
2699a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2700d763cef2SBarry Smith   PetscFunctionReturn(0);
2701d763cef2SBarry Smith }
2702d763cef2SBarry Smith 
2703d8e5e3e6SSatish Balay /*@
2704d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2705d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2706d763cef2SBarry Smith 
2707d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2708d763cef2SBarry Smith 
2709d763cef2SBarry Smith    Input Parameter:
2710d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2711d763cef2SBarry Smith 
2712d763cef2SBarry Smith    Output Parameter:
2713d763cef2SBarry Smith .  snes - the nonlinear solver context
2714d763cef2SBarry Smith 
2715d763cef2SBarry Smith    Notes:
2716d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2717d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
271894b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2719d763cef2SBarry Smith 
2720d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
27210298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2722d763cef2SBarry Smith 
2723d763cef2SBarry Smith    Level: beginner
2724d763cef2SBarry Smith 
2725d763cef2SBarry Smith .keywords: timestep, get, SNES
2726d763cef2SBarry Smith @*/
27277087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2728d763cef2SBarry Smith {
2729d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2730d372ba47SLisandro Dalcin 
2731d763cef2SBarry Smith   PetscFunctionBegin;
27320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27334482741eSBarry Smith   PetscValidPointer(snes,2);
2734d372ba47SLisandro Dalcin   if (!ts->snes) {
2735ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
27360298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27373bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2738d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2739496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
27409e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
27419e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
27429e2a6581SJed Brown     }
2743d372ba47SLisandro Dalcin   }
2744d763cef2SBarry Smith   *snes = ts->snes;
2745d763cef2SBarry Smith   PetscFunctionReturn(0);
2746d763cef2SBarry Smith }
2747d763cef2SBarry Smith 
2748deb2cd25SJed Brown /*@
2749deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2750deb2cd25SJed Brown 
2751deb2cd25SJed Brown    Collective
2752deb2cd25SJed Brown 
2753deb2cd25SJed Brown    Input Parameter:
2754deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2755deb2cd25SJed Brown -  snes - the nonlinear solver context
2756deb2cd25SJed Brown 
2757deb2cd25SJed Brown    Notes:
2758deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2759deb2cd25SJed Brown 
2760deb2cd25SJed Brown    Level: developer
2761deb2cd25SJed Brown 
2762deb2cd25SJed Brown .keywords: timestep, set, SNES
2763deb2cd25SJed Brown @*/
2764deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2765deb2cd25SJed Brown {
2766deb2cd25SJed Brown   PetscErrorCode ierr;
2767d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2768deb2cd25SJed Brown 
2769deb2cd25SJed Brown   PetscFunctionBegin;
2770deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2771deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2772deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2773deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2774bbd56ea5SKarl Rupp 
2775deb2cd25SJed Brown   ts->snes = snes;
2776bbd56ea5SKarl Rupp 
27770298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27780298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2779740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
27800298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2781740132f1SEmil Constantinescu   }
2782deb2cd25SJed Brown   PetscFunctionReturn(0);
2783deb2cd25SJed Brown }
2784deb2cd25SJed Brown 
2785d8e5e3e6SSatish Balay /*@
278694b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2787d763cef2SBarry Smith    a TS (timestepper) context.
2788d763cef2SBarry Smith 
278994b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2790d763cef2SBarry Smith 
2791d763cef2SBarry Smith    Input Parameter:
2792d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2793d763cef2SBarry Smith 
2794d763cef2SBarry Smith    Output Parameter:
279594b7f48cSBarry Smith .  ksp - the nonlinear solver context
2796d763cef2SBarry Smith 
2797d763cef2SBarry Smith    Notes:
279894b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2799d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2800d763cef2SBarry Smith    KSP and PC contexts as well.
2801d763cef2SBarry Smith 
280294b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
28030298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2804d763cef2SBarry Smith 
2805d763cef2SBarry Smith    Level: beginner
2806d763cef2SBarry Smith 
280794b7f48cSBarry Smith .keywords: timestep, get, KSP
2808d763cef2SBarry Smith @*/
28097087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2810d763cef2SBarry Smith {
2811d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2812089b2837SJed Brown   SNES           snes;
2813d372ba47SLisandro Dalcin 
2814d763cef2SBarry Smith   PetscFunctionBegin;
28150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28164482741eSBarry Smith   PetscValidPointer(ksp,2);
281717186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2818e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2819089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2820089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2821d763cef2SBarry Smith   PetscFunctionReturn(0);
2822d763cef2SBarry Smith }
2823d763cef2SBarry Smith 
2824d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2825d763cef2SBarry Smith 
2826adb62b0dSMatthew Knepley /*@
2827adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2828adb62b0dSMatthew Knepley    maximum time for iteration.
2829adb62b0dSMatthew Knepley 
28303f9fe445SBarry Smith    Not Collective
2831adb62b0dSMatthew Knepley 
2832adb62b0dSMatthew Knepley    Input Parameters:
2833adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
28340298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
28350298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2836adb62b0dSMatthew Knepley 
2837adb62b0dSMatthew Knepley    Level: intermediate
2838adb62b0dSMatthew Knepley 
2839adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2840adb62b0dSMatthew Knepley @*/
28417087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2842adb62b0dSMatthew Knepley {
2843adb62b0dSMatthew Knepley   PetscFunctionBegin;
28440700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2845abc0a331SBarry Smith   if (maxsteps) {
28464482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2847adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2848adb62b0dSMatthew Knepley   }
2849abc0a331SBarry Smith   if (maxtime) {
28504482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2851adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2852adb62b0dSMatthew Knepley   }
2853adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2854adb62b0dSMatthew Knepley }
2855adb62b0dSMatthew Knepley 
2856d763cef2SBarry Smith /*@
2857d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2858d763cef2SBarry Smith    maximum time for iteration.
2859d763cef2SBarry Smith 
28603f9fe445SBarry Smith    Logically Collective on TS
2861d763cef2SBarry Smith 
2862d763cef2SBarry Smith    Input Parameters:
2863d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2864d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2865d763cef2SBarry Smith -  maxtime - final time to iterate to
2866d763cef2SBarry Smith 
2867d763cef2SBarry Smith    Options Database Keys:
2868d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
28693bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2870d763cef2SBarry Smith 
2871d763cef2SBarry Smith    Notes:
2872d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2873d763cef2SBarry Smith 
2874d763cef2SBarry Smith    Level: intermediate
2875d763cef2SBarry Smith 
2876d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2877a43b19c4SJed Brown 
2878a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2879d763cef2SBarry Smith @*/
28807087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2881d763cef2SBarry Smith {
2882d763cef2SBarry Smith   PetscFunctionBegin;
28830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2884c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2885c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
288639b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
288739b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2888d763cef2SBarry Smith   PetscFunctionReturn(0);
2889d763cef2SBarry Smith }
2890d763cef2SBarry Smith 
2891d763cef2SBarry Smith /*@
2892d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2893d763cef2SBarry Smith    for use by the TS routines.
2894d763cef2SBarry Smith 
28953f9fe445SBarry Smith    Logically Collective on TS and Vec
2896d763cef2SBarry Smith 
2897d763cef2SBarry Smith    Input Parameters:
2898d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
28990910c330SBarry Smith -  u - the solution vector
2900d763cef2SBarry Smith 
2901d763cef2SBarry Smith    Level: beginner
2902d763cef2SBarry Smith 
2903d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2904d763cef2SBarry Smith @*/
29050910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2906d763cef2SBarry Smith {
29078737fe31SLisandro Dalcin   PetscErrorCode ierr;
2908496e6a7aSJed Brown   DM             dm;
29098737fe31SLisandro Dalcin 
2910d763cef2SBarry Smith   PetscFunctionBegin;
29110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29120910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
29130910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
29146bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
29150910c330SBarry Smith   ts->vec_sol = u;
2916bbd56ea5SKarl Rupp 
2917496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
29180910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2919d763cef2SBarry Smith   PetscFunctionReturn(0);
2920d763cef2SBarry Smith }
2921d763cef2SBarry Smith 
292273b18844SBarry Smith /*@
292373b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
292473b18844SBarry Smith 
292573b18844SBarry Smith    Logically Collective on TS
292673b18844SBarry Smith 
292773b18844SBarry Smith    Input Parameters:
292873b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
292973b18844SBarry Smith .  steps - number of steps to use
293073b18844SBarry Smith 
293173b18844SBarry Smith    Level: intermediate
293273b18844SBarry Smith 
293373b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
293473b18844SBarry Smith           so as to integrate back to less than the original timestep
293573b18844SBarry Smith 
293673b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
293773b18844SBarry Smith 
293873b18844SBarry Smith .seealso: TSSetExactFinalTime()
293973b18844SBarry Smith @*/
294073b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
294173b18844SBarry Smith {
294273b18844SBarry Smith   PetscFunctionBegin;
294373b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
294473b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
294573b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
294673b18844SBarry 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");
294773b18844SBarry Smith   ts->adjoint_max_steps = steps;
294873b18844SBarry Smith   PetscFunctionReturn(0);
294973b18844SBarry Smith }
295073b18844SBarry Smith 
2951c235aa8dSHong Zhang /*@
2952dfb21088SHong 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
29530cf82b59SBarry Smith       for use by the TSAdjoint routines.
2954c235aa8dSHong Zhang 
2955c235aa8dSHong Zhang    Logically Collective on TS and Vec
2956c235aa8dSHong Zhang 
2957c235aa8dSHong Zhang    Input Parameters:
2958c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2959abc2977eSBarry 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
2960abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
2961c235aa8dSHong Zhang 
2962c235aa8dSHong Zhang    Level: beginner
2963c235aa8dSHong Zhang 
2964abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
29650cf82b59SBarry Smith 
2966c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2967c235aa8dSHong Zhang @*/
2968dfb21088SHong Zhang PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
2969c235aa8dSHong Zhang {
2970c235aa8dSHong Zhang   PetscFunctionBegin;
2971c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2972abc2977eSBarry Smith   PetscValidPointer(lambda,2);
2973abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
2974abc2977eSBarry Smith   ts->vecs_sensip = mu;
2975dfb21088SHong 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");
2976abc2977eSBarry Smith   ts->numcost  = numcost;
2977ad8e2604SHong Zhang   PetscFunctionReturn(0);
2978ad8e2604SHong Zhang }
2979ad8e2604SHong Zhang 
2980ad8e2604SHong Zhang /*@C
29810cf82b59SBarry 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.
2982ad8e2604SHong Zhang 
2983ad8e2604SHong Zhang   Logically Collective on TS
2984ad8e2604SHong Zhang 
2985ad8e2604SHong Zhang   Input Parameters:
2986ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2987ad8e2604SHong Zhang - func - The function
2988ad8e2604SHong Zhang 
2989ad8e2604SHong Zhang   Calling sequence of func:
29900cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
299105755b9cSHong Zhang +   t - current timestep
29920cf82b59SBarry Smith .   y - input vector (current ODE solution)
299305755b9cSHong Zhang .   A - output matrix
299405755b9cSHong Zhang -   ctx - [optional] user-defined function context
2995ad8e2604SHong Zhang 
2996ad8e2604SHong Zhang   Level: intermediate
2997ad8e2604SHong Zhang 
29980cf82b59SBarry 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
29990cf82b59SBarry Smith 
3000ad8e2604SHong Zhang .keywords: TS, sensitivity
3001ad8e2604SHong Zhang .seealso:
3002ad8e2604SHong Zhang @*/
30035bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
3004ad8e2604SHong Zhang {
3005ad8e2604SHong Zhang   PetscErrorCode ierr;
3006ad8e2604SHong Zhang 
3007ad8e2604SHong Zhang   PetscFunctionBegin;
3008ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3009ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
3010ad8e2604SHong Zhang 
3011ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
3012ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
3013ad8e2604SHong Zhang   if(Amat) {
3014ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
3015ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
3016ad8e2604SHong Zhang     ts->Jacp = Amat;
3017ad8e2604SHong Zhang   }
3018ad8e2604SHong Zhang   PetscFunctionReturn(0);
3019ad8e2604SHong Zhang }
3020ad8e2604SHong Zhang 
30210cf82b59SBarry Smith /*@C
30225bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
3023ad8e2604SHong Zhang 
3024ad8e2604SHong Zhang   Collective on TS
3025ad8e2604SHong Zhang 
3026ad8e2604SHong Zhang   Input Parameters:
3027ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
3028ad8e2604SHong Zhang 
3029ad8e2604SHong Zhang   Level: developer
3030ad8e2604SHong Zhang 
3031ad8e2604SHong Zhang .keywords: TS, sensitivity
30325bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
3033ad8e2604SHong Zhang @*/
30345bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
3035ad8e2604SHong Zhang {
3036ad8e2604SHong Zhang   PetscErrorCode ierr;
3037ad8e2604SHong Zhang 
3038ad8e2604SHong Zhang   PetscFunctionBegin;
3039ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3040ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3041ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
3042ad8e2604SHong Zhang 
3043ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
3044ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
3045ad8e2604SHong Zhang   PetscStackPop;
3046c235aa8dSHong Zhang   PetscFunctionReturn(0);
3047c235aa8dSHong Zhang }
3048c235aa8dSHong Zhang 
30496fd0887fSHong Zhang /*@C
3050dfb21088SHong Zhang     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
30516fd0887fSHong Zhang 
30526fd0887fSHong Zhang     Logically Collective on TS
30536fd0887fSHong Zhang 
30546fd0887fSHong Zhang     Input Parameters:
30556fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
3056abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
30570cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
3058b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
3059b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
3060feaa1ddbSSatish Balay .   fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
3061b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
30626fd0887fSHong Zhang 
30630cf82b59SBarry Smith     Calling sequence of rf:
30640cf82b59SBarry Smith $     rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx);
30656fd0887fSHong Zhang 
30666fd0887fSHong Zhang +   t - current timestep
30670cf82b59SBarry Smith .   y - input vector
30680cf82b59SBarry Smith .   f - function result; one vector entry for each cost function
30696fd0887fSHong Zhang -   ctx - [optional] user-defined function context
30706fd0887fSHong Zhang 
3071b612ec54SBarry Smith    Calling sequence of drdyf:
30720cf82b59SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
3073b612ec54SBarry Smith 
3074b612ec54SBarry Smith    Calling sequence of drdpf:
30750cf82b59SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
3076b612ec54SBarry Smith 
3077b612ec54SBarry Smith     Level: intermediate
30786fd0887fSHong Zhang 
3079abc2977eSBarry Smith     Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions
30800cf82b59SBarry Smith 
30816fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
30826fd0887fSHong Zhang 
3083dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
30846fd0887fSHong Zhang @*/
3085dfb21088SHong Zhang PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
3086d8151eeaSBarry Smith                                                           PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
3087b1cb36f3SHong Zhang                                                           PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),
3088b1cb36f3SHong Zhang                                                           PetscBool fwd,void *ctx)
30896fd0887fSHong Zhang {
30906fd0887fSHong Zhang   PetscErrorCode ierr;
30916fd0887fSHong Zhang 
30926fd0887fSHong Zhang   PetscFunctionBegin;
30936fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3094dfb21088SHong 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()");
3095c72ed514SHong Zhang   if (!ts->numcost) ts->numcost=numcost;
30966fd0887fSHong Zhang 
3097b1cb36f3SHong Zhang   ts->costintegralfwd  = fwd; /* Evaluate the cost integral in forward run if fwd is true */
3098abc2977eSBarry Smith   ierr                 = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
30992c39e106SBarry Smith   ierr                 = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
31000cf82b59SBarry Smith   ts->costintegrand    = rf;
310105755b9cSHong Zhang   ts->costintegrandctx = ctx;
3102b612ec54SBarry Smith   ts->drdyfunction     = drdyf;
3103b612ec54SBarry Smith   ts->drdpfunction     = drdpf;
31046fd0887fSHong Zhang   PetscFunctionReturn(0);
31056fd0887fSHong Zhang }
31066fd0887fSHong Zhang 
310736eaed60SHong Zhang /*@
3108dfb21088SHong Zhang    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
310936eaed60SHong Zhang    It is valid to call the routine after a backward run.
311036eaed60SHong Zhang 
311136eaed60SHong Zhang    Not Collective
311236eaed60SHong Zhang 
311336eaed60SHong Zhang    Input Parameter:
311436eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
311536eaed60SHong Zhang 
311636eaed60SHong Zhang    Output Parameter:
31172c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
311836eaed60SHong Zhang 
311936eaed60SHong Zhang    Level: intermediate
312036eaed60SHong Zhang 
3121dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
312236eaed60SHong Zhang 
312336eaed60SHong Zhang .keywords: TS, sensitivity analysis
312436eaed60SHong Zhang @*/
3125dfb21088SHong Zhang PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
312636eaed60SHong Zhang {
312736eaed60SHong Zhang   PetscFunctionBegin;
312836eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
312936eaed60SHong Zhang   PetscValidPointer(v,2);
31302c39e106SBarry Smith   *v = ts->vec_costintegral;
313136eaed60SHong Zhang   PetscFunctionReturn(0);
313236eaed60SHong Zhang }
313336eaed60SHong Zhang 
31346fd0887fSHong Zhang /*@
31352c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
31366fd0887fSHong Zhang 
31376fd0887fSHong Zhang    Input Parameters:
31386fd0887fSHong Zhang +  ts - the TS context
31396fd0887fSHong Zhang .  t - current time
31400cf82b59SBarry Smith -  y - state vector, i.e. current solution
31416fd0887fSHong Zhang 
31426fd0887fSHong Zhang    Output Parameter:
3143abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
31446fd0887fSHong Zhang 
31456fd0887fSHong Zhang    Note:
31466fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
31476fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
31486fd0887fSHong Zhang 
31496fd0887fSHong Zhang    Level: developer
31506fd0887fSHong Zhang 
31516fd0887fSHong Zhang .keywords: TS, compute
31526fd0887fSHong Zhang 
3153dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
31546fd0887fSHong Zhang @*/
31550cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
31566fd0887fSHong Zhang {
31576fd0887fSHong Zhang   PetscErrorCode ierr;
31586fd0887fSHong Zhang 
31596fd0887fSHong Zhang   PetscFunctionBegin;
31606fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31610cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
31626fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
31636fd0887fSHong Zhang 
31640cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
3165e4680132SHong Zhang   if (ts->costintegrand) {
3166e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
31670cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
31686fd0887fSHong Zhang     PetscStackPop;
31696fd0887fSHong Zhang   } else {
31706fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
31716fd0887fSHong Zhang   }
31726fd0887fSHong Zhang 
31730cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
31746fd0887fSHong Zhang   PetscFunctionReturn(0);
31756fd0887fSHong Zhang }
31766fd0887fSHong Zhang 
317705755b9cSHong Zhang /*@
3178d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
317905755b9cSHong Zhang 
318005755b9cSHong Zhang   Collective on TS
318105755b9cSHong Zhang 
318205755b9cSHong Zhang   Input Parameters:
318305755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
318405755b9cSHong Zhang 
318505755b9cSHong Zhang   Notes:
3186d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
318705755b9cSHong Zhang   so most users would not generally call this routine themselves.
318805755b9cSHong Zhang 
318905755b9cSHong Zhang   Level: developer
319005755b9cSHong Zhang 
319105755b9cSHong Zhang .keywords: TS, sensitivity
3192d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
319305755b9cSHong Zhang @*/
31940cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
319505755b9cSHong Zhang {
319605755b9cSHong Zhang   PetscErrorCode ierr;
319705755b9cSHong Zhang 
319805755b9cSHong Zhang   PetscFunctionBegin;
319905755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32000cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
320105755b9cSHong Zhang 
320205755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
32030cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
320405755b9cSHong Zhang   PetscStackPop;
320505755b9cSHong Zhang   PetscFunctionReturn(0);
320605755b9cSHong Zhang }
320705755b9cSHong Zhang 
320805755b9cSHong Zhang /*@
3209d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
321005755b9cSHong Zhang 
321105755b9cSHong Zhang   Collective on TS
321205755b9cSHong Zhang 
321305755b9cSHong Zhang   Input Parameters:
321405755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
321505755b9cSHong Zhang 
321605755b9cSHong Zhang   Notes:
321705755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
321805755b9cSHong Zhang   so most users would not generally call this routine themselves.
321905755b9cSHong Zhang 
322005755b9cSHong Zhang   Level: developer
322105755b9cSHong Zhang 
322205755b9cSHong Zhang .keywords: TS, sensitivity
3223d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
322405755b9cSHong Zhang @*/
32250cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
322605755b9cSHong Zhang {
322705755b9cSHong Zhang   PetscErrorCode ierr;
322805755b9cSHong Zhang 
322905755b9cSHong Zhang   PetscFunctionBegin;
323005755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32310cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
323205755b9cSHong Zhang 
323305755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
32340cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
323505755b9cSHong Zhang   PetscStackPop;
323605755b9cSHong Zhang   PetscFunctionReturn(0);
323705755b9cSHong Zhang }
323805755b9cSHong Zhang 
3239ac226902SBarry Smith /*@C
3240000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
32413f2090d5SJed Brown   called once at the beginning of each time step.
3242000e7ae3SMatthew Knepley 
32433f9fe445SBarry Smith   Logically Collective on TS
3244000e7ae3SMatthew Knepley 
3245000e7ae3SMatthew Knepley   Input Parameters:
3246000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3247000e7ae3SMatthew Knepley - func - The function
3248000e7ae3SMatthew Knepley 
3249000e7ae3SMatthew Knepley   Calling sequence of func:
3250000e7ae3SMatthew Knepley . func (TS ts);
3251000e7ae3SMatthew Knepley 
3252000e7ae3SMatthew Knepley   Level: intermediate
3253000e7ae3SMatthew Knepley 
3254000e7ae3SMatthew Knepley .keywords: TS, timestep
32559be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
3256000e7ae3SMatthew Knepley @*/
32577087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3258000e7ae3SMatthew Knepley {
3259000e7ae3SMatthew Knepley   PetscFunctionBegin;
32600700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3261ae60f76fSBarry Smith   ts->prestep = func;
3262000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3263000e7ae3SMatthew Knepley }
3264000e7ae3SMatthew Knepley 
326509ee8438SJed Brown /*@
32663f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
32673f2090d5SJed Brown 
32683f2090d5SJed Brown   Collective on TS
32693f2090d5SJed Brown 
32703f2090d5SJed Brown   Input Parameters:
32713f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32723f2090d5SJed Brown 
32733f2090d5SJed Brown   Notes:
32743f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32753f2090d5SJed Brown   so most users would not generally call this routine themselves.
32763f2090d5SJed Brown 
32773f2090d5SJed Brown   Level: developer
32783f2090d5SJed Brown 
32793f2090d5SJed Brown .keywords: TS, timestep
32809be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32813f2090d5SJed Brown @*/
32827087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32833f2090d5SJed Brown {
32843f2090d5SJed Brown   PetscErrorCode ierr;
32853f2090d5SJed Brown 
32863f2090d5SJed Brown   PetscFunctionBegin;
32870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3288ae60f76fSBarry Smith   if (ts->prestep) {
3289ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
3290312ce896SJed Brown   }
32913f2090d5SJed Brown   PetscFunctionReturn(0);
32923f2090d5SJed Brown }
32933f2090d5SJed Brown 
3294b8123daeSJed Brown /*@C
3295b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3296b8123daeSJed Brown   called once at the beginning of each stage.
3297b8123daeSJed Brown 
3298b8123daeSJed Brown   Logically Collective on TS
3299b8123daeSJed Brown 
3300b8123daeSJed Brown   Input Parameters:
3301b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3302b8123daeSJed Brown - func - The function
3303b8123daeSJed Brown 
3304b8123daeSJed Brown   Calling sequence of func:
3305b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3306b8123daeSJed Brown 
3307b8123daeSJed Brown   Level: intermediate
3308b8123daeSJed Brown 
3309b8123daeSJed Brown   Note:
3310b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
3311b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
3312b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3313b8123daeSJed Brown 
3314b8123daeSJed Brown .keywords: TS, timestep
33159be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3316b8123daeSJed Brown @*/
3317b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3318b8123daeSJed Brown {
3319b8123daeSJed Brown   PetscFunctionBegin;
3320b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3321ae60f76fSBarry Smith   ts->prestage = func;
3322b8123daeSJed Brown   PetscFunctionReturn(0);
3323b8123daeSJed Brown }
3324b8123daeSJed Brown 
33259be3e283SDebojyoti Ghosh /*@C
33269be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
33279be3e283SDebojyoti Ghosh   called once at the end of each stage.
33289be3e283SDebojyoti Ghosh 
33299be3e283SDebojyoti Ghosh   Logically Collective on TS
33309be3e283SDebojyoti Ghosh 
33319be3e283SDebojyoti Ghosh   Input Parameters:
33329be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
33339be3e283SDebojyoti Ghosh - func - The function
33349be3e283SDebojyoti Ghosh 
33359be3e283SDebojyoti Ghosh   Calling sequence of func:
33369be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
33379be3e283SDebojyoti Ghosh 
33389be3e283SDebojyoti Ghosh   Level: intermediate
33399be3e283SDebojyoti Ghosh 
33409be3e283SDebojyoti Ghosh   Note:
33419be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
33429be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
33439be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
33449be3e283SDebojyoti Ghosh 
33459be3e283SDebojyoti Ghosh .keywords: TS, timestep
33469be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
33479be3e283SDebojyoti Ghosh @*/
33489be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
33499be3e283SDebojyoti Ghosh {
33509be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33519be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
33529be3e283SDebojyoti Ghosh   ts->poststage = func;
33539be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33549be3e283SDebojyoti Ghosh }
33559be3e283SDebojyoti Ghosh 
3356c688d042SShri Abhyankar /*@C
3357c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3358c688d042SShri Abhyankar   called once at the end of each step evaluation.
3359c688d042SShri Abhyankar 
3360c688d042SShri Abhyankar   Logically Collective on TS
3361c688d042SShri Abhyankar 
3362c688d042SShri Abhyankar   Input Parameters:
3363c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3364c688d042SShri Abhyankar - func - The function
3365c688d042SShri Abhyankar 
3366c688d042SShri Abhyankar   Calling sequence of func:
3367c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3368c688d042SShri Abhyankar 
3369c688d042SShri Abhyankar   Level: intermediate
3370c688d042SShri Abhyankar 
3371c688d042SShri Abhyankar   Note:
33721785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33731785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3374e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3375e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3376e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3377c688d042SShri Abhyankar 
3378c688d042SShri Abhyankar .keywords: TS, timestep
3379c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3380c688d042SShri Abhyankar @*/
3381c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3382c688d042SShri Abhyankar {
3383c688d042SShri Abhyankar   PetscFunctionBegin;
3384c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3385c688d042SShri Abhyankar   ts->postevaluate = func;
3386c688d042SShri Abhyankar   PetscFunctionReturn(0);
3387c688d042SShri Abhyankar }
3388c688d042SShri Abhyankar 
3389b8123daeSJed Brown /*@
3390b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3391b8123daeSJed Brown 
3392b8123daeSJed Brown   Collective on TS
3393b8123daeSJed Brown 
3394b8123daeSJed Brown   Input Parameters:
3395b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33969be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3397b8123daeSJed Brown 
3398b8123daeSJed Brown   Notes:
3399b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3400b8123daeSJed Brown   most users would not generally call this routine themselves.
3401b8123daeSJed Brown 
3402b8123daeSJed Brown   Level: developer
3403b8123daeSJed Brown 
3404b8123daeSJed Brown .keywords: TS, timestep
34059be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3406b8123daeSJed Brown @*/
3407b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3408b8123daeSJed Brown {
3409b8123daeSJed Brown   PetscErrorCode ierr;
3410b8123daeSJed Brown 
3411b8123daeSJed Brown   PetscFunctionBegin;
3412b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3413ae60f76fSBarry Smith   if (ts->prestage) {
3414ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3415b8123daeSJed Brown   }
3416b8123daeSJed Brown   PetscFunctionReturn(0);
3417b8123daeSJed Brown }
3418b8123daeSJed Brown 
34199be3e283SDebojyoti Ghosh /*@
34209be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
34219be3e283SDebojyoti Ghosh 
34229be3e283SDebojyoti Ghosh   Collective on TS
34239be3e283SDebojyoti Ghosh 
34249be3e283SDebojyoti Ghosh   Input Parameters:
34259be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
34269be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
34279be3e283SDebojyoti Ghosh   stageindex  - Stage number
34289be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
34299be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
34309be3e283SDebojyoti Ghosh 
34319be3e283SDebojyoti Ghosh   Notes:
34329be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
34339be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
34349be3e283SDebojyoti Ghosh 
34359be3e283SDebojyoti Ghosh   Level: developer
34369be3e283SDebojyoti Ghosh 
34379be3e283SDebojyoti Ghosh .keywords: TS, timestep
34389be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
34399be3e283SDebojyoti Ghosh @*/
34409be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
34419be3e283SDebojyoti Ghosh {
34429be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
34439be3e283SDebojyoti Ghosh 
34449be3e283SDebojyoti Ghosh   PetscFunctionBegin;
34459be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34464beae5d8SLisandro Dalcin   if (ts->poststage) {
34479be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
34489be3e283SDebojyoti Ghosh   }
34499be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
34509be3e283SDebojyoti Ghosh }
34519be3e283SDebojyoti Ghosh 
3452c688d042SShri Abhyankar /*@
3453c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3454c688d042SShri Abhyankar 
3455c688d042SShri Abhyankar   Collective on TS
3456c688d042SShri Abhyankar 
3457c688d042SShri Abhyankar   Input Parameters:
3458c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3459c688d042SShri Abhyankar 
3460c688d042SShri Abhyankar   Notes:
3461c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3462c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3463c688d042SShri Abhyankar 
3464c688d042SShri Abhyankar   Level: developer
3465c688d042SShri Abhyankar 
3466c688d042SShri Abhyankar .keywords: TS, timestep
3467c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3468c688d042SShri Abhyankar @*/
3469c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3470c688d042SShri Abhyankar {
3471c688d042SShri Abhyankar   PetscErrorCode ierr;
3472c688d042SShri Abhyankar 
3473c688d042SShri Abhyankar   PetscFunctionBegin;
3474c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3475c688d042SShri Abhyankar   if (ts->postevaluate) {
3476c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3477c688d042SShri Abhyankar   }
3478c688d042SShri Abhyankar   PetscFunctionReturn(0);
3479c688d042SShri Abhyankar }
3480c688d042SShri Abhyankar 
3481ac226902SBarry Smith /*@C
3482000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34833f2090d5SJed Brown   called once at the end of each time step.
3484000e7ae3SMatthew Knepley 
34853f9fe445SBarry Smith   Logically Collective on TS
3486000e7ae3SMatthew Knepley 
3487000e7ae3SMatthew Knepley   Input Parameters:
3488000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3489000e7ae3SMatthew Knepley - func - The function
3490000e7ae3SMatthew Knepley 
3491000e7ae3SMatthew Knepley   Calling sequence of func:
3492b8123daeSJed Brown $ func (TS ts);
3493000e7ae3SMatthew Knepley 
34941785ff2aSShri Abhyankar   Notes:
34951785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34961785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34971785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34981785ff2aSShri Abhyankar 
3499000e7ae3SMatthew Knepley   Level: intermediate
3500000e7ae3SMatthew Knepley 
3501000e7ae3SMatthew Knepley .keywords: TS, timestep
35021785ff2aSShri Abhyankar .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
3503000e7ae3SMatthew Knepley @*/
35047087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3505000e7ae3SMatthew Knepley {
3506000e7ae3SMatthew Knepley   PetscFunctionBegin;
35070700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3508ae60f76fSBarry Smith   ts->poststep = func;
3509000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3510000e7ae3SMatthew Knepley }
3511000e7ae3SMatthew Knepley 
351209ee8438SJed Brown /*@
35133f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
35143f2090d5SJed Brown 
35153f2090d5SJed Brown   Collective on TS
35163f2090d5SJed Brown 
35173f2090d5SJed Brown   Input Parameters:
35183f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
35193f2090d5SJed Brown 
35203f2090d5SJed Brown   Notes:
35213f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
35223f2090d5SJed Brown   so most users would not generally call this routine themselves.
35233f2090d5SJed Brown 
35243f2090d5SJed Brown   Level: developer
35253f2090d5SJed Brown 
35263f2090d5SJed Brown .keywords: TS, timestep
35273f2090d5SJed Brown @*/
35287087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
35293f2090d5SJed Brown {
35303f2090d5SJed Brown   PetscErrorCode ierr;
35313f2090d5SJed Brown 
35323f2090d5SJed Brown   PetscFunctionBegin;
35330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3534ae60f76fSBarry Smith   if (ts->poststep) {
3535ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
353672ac3e02SJed Brown   }
35373f2090d5SJed Brown   PetscFunctionReturn(0);
35383f2090d5SJed Brown }
35393f2090d5SJed Brown 
3540d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3541d763cef2SBarry Smith 
3542d763cef2SBarry Smith /*@C
3543a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3544d763cef2SBarry Smith    timestep to display the iteration's  progress.
3545d763cef2SBarry Smith 
35463f9fe445SBarry Smith    Logically Collective on TS
3547d763cef2SBarry Smith 
3548d763cef2SBarry Smith    Input Parameters:
3549d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3550e213d8f1SJed Brown .  monitor - monitoring routine
3551329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
35520298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3553b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
35540298fd71SBarry Smith           (may be NULL)
3555d763cef2SBarry Smith 
3556e213d8f1SJed Brown    Calling sequence of monitor:
35570910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3558d763cef2SBarry Smith 
3559d763cef2SBarry Smith +    ts - the TS context
356063e21af5SBarry 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)
35611f06c33eSBarry Smith .    time - current time
35620910c330SBarry Smith .    u - current iterate
3563d763cef2SBarry Smith -    mctx - [optional] monitoring context
3564d763cef2SBarry Smith 
3565d763cef2SBarry Smith    Notes:
3566d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3567d763cef2SBarry Smith    already has been loaded.
3568d763cef2SBarry Smith 
3569025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
3570025f1a04SBarry Smith 
3571d763cef2SBarry Smith    Level: intermediate
3572d763cef2SBarry Smith 
3573d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3574d763cef2SBarry Smith 
3575a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3576d763cef2SBarry Smith @*/
3577c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3578d763cef2SBarry Smith {
357978064530SBarry Smith   PetscErrorCode ierr;
358078064530SBarry Smith   PetscInt       i;
358178064530SBarry Smith   PetscBool      identical;
358278064530SBarry Smith 
3583d763cef2SBarry Smith   PetscFunctionBegin;
35840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
358578064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
358678064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
358778064530SBarry Smith     if (identical) PetscFunctionReturn(0);
358878064530SBarry Smith   }
358917186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3590d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
35918704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3592d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3593d763cef2SBarry Smith   PetscFunctionReturn(0);
3594d763cef2SBarry Smith }
3595d763cef2SBarry Smith 
3596d763cef2SBarry Smith /*@C
3597a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3598d763cef2SBarry Smith 
35993f9fe445SBarry Smith    Logically Collective on TS
3600d763cef2SBarry Smith 
3601d763cef2SBarry Smith    Input Parameters:
3602d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3603d763cef2SBarry Smith 
3604d763cef2SBarry Smith    Notes:
3605d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3606d763cef2SBarry Smith 
3607d763cef2SBarry Smith    Level: intermediate
3608d763cef2SBarry Smith 
3609d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3610d763cef2SBarry Smith 
3611a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3612d763cef2SBarry Smith @*/
36137087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3614d763cef2SBarry Smith {
3615d952e501SBarry Smith   PetscErrorCode ierr;
3616d952e501SBarry Smith   PetscInt       i;
3617d952e501SBarry Smith 
3618d763cef2SBarry Smith   PetscFunctionBegin;
36190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3620d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
36218704b422SBarry Smith     if (ts->monitordestroy[i]) {
36228704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3623d952e501SBarry Smith     }
3624d952e501SBarry Smith   }
3625d763cef2SBarry Smith   ts->numbermonitors = 0;
3626d763cef2SBarry Smith   PetscFunctionReturn(0);
3627d763cef2SBarry Smith }
3628d763cef2SBarry Smith 
3629721cd6eeSBarry Smith /*@C
3630721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
36315516499fSSatish Balay 
36325516499fSSatish Balay    Level: intermediate
363341251cbbSSatish Balay 
36345516499fSSatish Balay .keywords: TS, set, monitor
36355516499fSSatish Balay 
363663e21af5SBarry Smith .seealso:  TSMonitorSet()
363741251cbbSSatish Balay @*/
3638721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3639d763cef2SBarry Smith {
3640dfbe8321SBarry Smith   PetscErrorCode ierr;
3641721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
364241aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3643d132466eSBarry Smith 
3644d763cef2SBarry Smith   PetscFunctionBegin;
36454d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
364641aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
364741aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3648721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
364941aca3d6SBarry Smith   if (iascii) {
3650649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
365163e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
365263e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
365363e21af5SBarry Smith     } else {
36548392e04aSShri 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);
365563e21af5SBarry Smith     }
3656649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
365741aca3d6SBarry Smith   } else if (ibinary) {
365841aca3d6SBarry Smith     PetscMPIInt rank;
365941aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
366041aca3d6SBarry Smith     if (!rank) {
3661450a797fSBarry Smith       PetscBool skipHeader;
3662450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3663450a797fSBarry Smith 
3664450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3665450a797fSBarry Smith       if (!skipHeader) {
3666450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3667450a797fSBarry Smith        }
366841aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
366941aca3d6SBarry Smith     } else {
367041aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
367141aca3d6SBarry Smith     }
367241aca3d6SBarry Smith   }
3673721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3674d763cef2SBarry Smith   PetscFunctionReturn(0);
3675d763cef2SBarry Smith }
3676d763cef2SBarry Smith 
36779110b2e7SHong Zhang /*@C
36789110b2e7SHong Zhang    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
36799110b2e7SHong Zhang    timestep to display the iteration's  progress.
36809110b2e7SHong Zhang 
36819110b2e7SHong Zhang    Logically Collective on TS
36829110b2e7SHong Zhang 
36839110b2e7SHong Zhang    Input Parameters:
36849110b2e7SHong Zhang +  ts - the TS context obtained from TSCreate()
36859110b2e7SHong Zhang .  adjointmonitor - monitoring routine
36869110b2e7SHong Zhang .  adjointmctx - [optional] user-defined context for private data for the
36879110b2e7SHong Zhang              monitor routine (use NULL if no context is desired)
36889110b2e7SHong Zhang -  adjointmonitordestroy - [optional] routine that frees monitor context
36899110b2e7SHong Zhang           (may be NULL)
36909110b2e7SHong Zhang 
36919110b2e7SHong Zhang    Calling sequence of monitor:
36929110b2e7SHong Zhang $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)
36939110b2e7SHong Zhang 
36949110b2e7SHong Zhang +    ts - the TS context
36959110b2e7SHong 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
36969110b2e7SHong Zhang                                been interpolated to)
36979110b2e7SHong Zhang .    time - current time
36989110b2e7SHong Zhang .    u - current iterate
36999110b2e7SHong Zhang .    numcost - number of cost functionos
37009110b2e7SHong Zhang .    lambda - sensitivities to initial conditions
37019110b2e7SHong Zhang .    mu - sensitivities to parameters
37029110b2e7SHong Zhang -    adjointmctx - [optional] adjoint monitoring context
37039110b2e7SHong Zhang 
37049110b2e7SHong Zhang    Notes:
37059110b2e7SHong Zhang    This routine adds an additional monitor to the list of monitors that
37069110b2e7SHong Zhang    already has been loaded.
37079110b2e7SHong Zhang 
37089110b2e7SHong Zhang    Fortran notes: Only a single monitor function can be set for each TS object
37099110b2e7SHong Zhang 
37109110b2e7SHong Zhang    Level: intermediate
37119110b2e7SHong Zhang 
37129110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
37139110b2e7SHong Zhang 
37149110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel()
37159110b2e7SHong Zhang @*/
37169110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
37179110b2e7SHong Zhang {
371878064530SBarry Smith   PetscErrorCode ierr;
371978064530SBarry Smith   PetscInt       i;
372078064530SBarry Smith   PetscBool      identical;
372178064530SBarry Smith 
37229110b2e7SHong Zhang   PetscFunctionBegin;
37239110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
372478064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
372578064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr);
372678064530SBarry Smith     if (identical) PetscFunctionReturn(0);
372778064530SBarry Smith   }
37289110b2e7SHong Zhang   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
37299110b2e7SHong Zhang   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
37309110b2e7SHong Zhang   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
37319110b2e7SHong Zhang   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
37329110b2e7SHong Zhang   PetscFunctionReturn(0);
37339110b2e7SHong Zhang }
37349110b2e7SHong Zhang 
37359110b2e7SHong Zhang /*@C
37369110b2e7SHong Zhang    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.
37379110b2e7SHong Zhang 
37389110b2e7SHong Zhang    Logically Collective on TS
37399110b2e7SHong Zhang 
37409110b2e7SHong Zhang    Input Parameters:
37419110b2e7SHong Zhang .  ts - the TS context obtained from TSCreate()
37429110b2e7SHong Zhang 
37439110b2e7SHong Zhang    Notes:
37449110b2e7SHong Zhang    There is no way to remove a single, specific monitor.
37459110b2e7SHong Zhang 
37469110b2e7SHong Zhang    Level: intermediate
37479110b2e7SHong Zhang 
37489110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
37499110b2e7SHong Zhang 
37509110b2e7SHong Zhang .seealso: TSAdjointMonitorSet()
37519110b2e7SHong Zhang @*/
37529110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorCancel(TS ts)
37539110b2e7SHong Zhang {
37549110b2e7SHong Zhang   PetscErrorCode ierr;
37559110b2e7SHong Zhang   PetscInt       i;
37569110b2e7SHong Zhang 
37579110b2e7SHong Zhang   PetscFunctionBegin;
37589110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37599110b2e7SHong Zhang   for (i=0; i<ts->numberadjointmonitors; i++) {
37609110b2e7SHong Zhang     if (ts->adjointmonitordestroy[i]) {
37619110b2e7SHong Zhang       ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
37629110b2e7SHong Zhang     }
37639110b2e7SHong Zhang   }
37649110b2e7SHong Zhang   ts->numberadjointmonitors = 0;
37659110b2e7SHong Zhang   PetscFunctionReturn(0);
37669110b2e7SHong Zhang }
37679110b2e7SHong Zhang 
3768721cd6eeSBarry Smith /*@C
3769721cd6eeSBarry Smith    TSAdjointMonitorDefault - the default monitor of adjoint computations
3770110eb670SHong Zhang 
3771110eb670SHong Zhang    Level: intermediate
3772110eb670SHong Zhang 
3773110eb670SHong Zhang .keywords: TS, set, monitor
3774110eb670SHong Zhang 
3775110eb670SHong Zhang .seealso: TSAdjointMonitorSet()
3776110eb670SHong Zhang @*/
3777721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
3778110eb670SHong Zhang {
3779110eb670SHong Zhang   PetscErrorCode ierr;
3780721cd6eeSBarry Smith   PetscViewer    viewer = vf->viewer;
3781110eb670SHong Zhang 
3782110eb670SHong Zhang   PetscFunctionBegin;
3783110eb670SHong Zhang   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3784721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3785110eb670SHong Zhang   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3786110eb670SHong 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);
3787110eb670SHong Zhang   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3788721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3789110eb670SHong Zhang   PetscFunctionReturn(0);
3790110eb670SHong Zhang }
3791110eb670SHong Zhang 
3792cd652676SJed Brown /*@
3793cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3794cd652676SJed Brown 
3795cd652676SJed Brown    Collective on TS
3796cd652676SJed Brown 
3797cd652676SJed Brown    Input Argument:
3798cd652676SJed Brown +  ts - time stepping context
3799cd652676SJed Brown -  t - time to interpolate to
3800cd652676SJed Brown 
3801cd652676SJed Brown    Output Argument:
38020910c330SBarry Smith .  U - state at given time
3803cd652676SJed Brown 
3804cd652676SJed Brown    Level: intermediate
3805cd652676SJed Brown 
3806cd652676SJed Brown    Developer Notes:
3807cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3808cd652676SJed Brown 
3809cd652676SJed Brown .keywords: TS, set
3810cd652676SJed Brown 
3811874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3812cd652676SJed Brown @*/
38130910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3814cd652676SJed Brown {
3815cd652676SJed Brown   PetscErrorCode ierr;
3816cd652676SJed Brown 
3817cd652676SJed Brown   PetscFunctionBegin;
3818cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3819b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3820be5899b3SLisandro 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);
3821ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
38220910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3823cd652676SJed Brown   PetscFunctionReturn(0);
3824cd652676SJed Brown }
3825cd652676SJed Brown 
3826d763cef2SBarry Smith /*@
38276d9e5789SSean Farley    TSStep - Steps one time step
3828d763cef2SBarry Smith 
3829d763cef2SBarry Smith    Collective on TS
3830d763cef2SBarry Smith 
3831d763cef2SBarry Smith    Input Parameter:
3832d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3833d763cef2SBarry Smith 
383427829d71SBarry Smith    Level: developer
3835d763cef2SBarry Smith 
3836b8123daeSJed Brown    Notes:
383727829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
383827829d71SBarry Smith 
3839b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3840b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3841b8123daeSJed Brown 
384225cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
384325cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
384425cb2221SBarry Smith 
3845d763cef2SBarry Smith .keywords: TS, timestep, solve
3846d763cef2SBarry Smith 
38479be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3848d763cef2SBarry Smith @*/
3849193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3850d763cef2SBarry Smith {
3851dfbe8321SBarry Smith   PetscErrorCode   ierr;
3852fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3853be5899b3SLisandro Dalcin   PetscReal        ptime;
3854d763cef2SBarry Smith 
3855d763cef2SBarry Smith   PetscFunctionBegin;
38560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3857fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3858fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3859fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3860fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3861fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3862fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3863302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3864fffbeea8SBarry Smith 
3865d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
386668bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3867d405a339SMatthew Knepley 
3868a6772fa2SLisandro 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()");
3869be5899b3SLisandro 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");
3870a6772fa2SLisandro Dalcin 
3871be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3872362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3873be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
3874e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3875d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3876193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3877d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3878be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
3879be5899b3SLisandro Dalcin   ts->steps++; ts->total_steps++;
3880be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
388128d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3882362cd11cSLisandro Dalcin 
3883362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3884cef5090cSJed Brown     if (ts->errorifstepfailed) {
388508c7845fSBarry 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]);
388608c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3887d2daff3dSHong Zhang     }
388808c7845fSBarry Smith   } else if (!ts->reason) {
388908c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
389008c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
389108c7845fSBarry Smith   }
389208c7845fSBarry Smith   PetscFunctionReturn(0);
389308c7845fSBarry Smith }
389408c7845fSBarry Smith 
389508c7845fSBarry Smith /*@
3896b957a604SHong Zhang    TSAdjointStep - Steps one time step backward in the adjoint run
389708c7845fSBarry Smith 
389808c7845fSBarry Smith    Collective on TS
389908c7845fSBarry Smith 
390008c7845fSBarry Smith    Input Parameter:
390108c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
390208c7845fSBarry Smith 
39036a4d4014SLisandro Dalcin    Level: intermediate
39046a4d4014SLisandro Dalcin 
3905b957a604SHong Zhang .keywords: TS, adjoint, step
39066a4d4014SLisandro Dalcin 
3907b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve()
39086a4d4014SLisandro Dalcin @*/
390908c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
39106a4d4014SLisandro Dalcin {
391108c7845fSBarry Smith   DM               dm;
39126a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
39136a4d4014SLisandro Dalcin 
39146a4d4014SLisandro Dalcin   PetscFunctionBegin;
39154a2ae208SSatish Balay   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
391608c7845fSBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
391708c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3918d763cef2SBarry Smith 
3919685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr);
3920d763cef2SBarry Smith 
3921be5899b3SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3922be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime;
392342f2b339SBarry 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);
3924be5899b3SLisandro Dalcin   ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
392542f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
39268d0ad7a8SHong Zhang   ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
3927be5899b3SLisandro Dalcin   ts->steps++; ts->total_steps--;
3928362cd11cSLisandro Dalcin 
3929362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3930cef5090cSJed Brown     if (ts->errorifstepfailed) {
39316c4ed002SBarry 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]);
39326c4ed002SBarry 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]);
39336c4ed002SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3934cef5090cSJed Brown     }
3935362cd11cSLisandro Dalcin   } else if (!ts->reason) {
393673b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
3937362cd11cSLisandro Dalcin   }
3938d763cef2SBarry Smith   PetscFunctionReturn(0);
3939d763cef2SBarry Smith }
3940d763cef2SBarry Smith 
39417cbde773SLisandro Dalcin /*@
39427cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
39437cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
39447cbde773SLisandro Dalcin 
39457cbde773SLisandro Dalcin    Collective on TS
39467cbde773SLisandro Dalcin 
39477cbde773SLisandro Dalcin    Input Arguments:
39487cbde773SLisandro Dalcin +  ts - time stepping context
39497cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
39507cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
39517cbde773SLisandro Dalcin 
39527cbde773SLisandro Dalcin    Output Arguments:
39537cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
39547cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
39557cbde773SLisandro Dalcin 
39567cbde773SLisandro Dalcin    Level: advanced
39577cbde773SLisandro Dalcin 
39587cbde773SLisandro Dalcin    Notes:
39597cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
39607cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
39617cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
39627cbde773SLisandro Dalcin 
39637cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
39647cbde773SLisandro Dalcin @*/
39657cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
39667cbde773SLisandro Dalcin {
39677cbde773SLisandro Dalcin   PetscErrorCode ierr;
39687cbde773SLisandro Dalcin 
39697cbde773SLisandro Dalcin   PetscFunctionBegin;
39707cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39717cbde773SLisandro Dalcin   PetscValidType(ts,1);
39727cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
39737cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
39747cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
39757cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
39767cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
39777cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
39787cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
39797cbde773SLisandro Dalcin   PetscFunctionReturn(0);
39807cbde773SLisandro Dalcin }
39817cbde773SLisandro Dalcin 
398205175c85SJed Brown /*@
398305175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
398405175c85SJed Brown 
39851c3436cfSJed Brown    Collective on TS
398605175c85SJed Brown 
398705175c85SJed Brown    Input Arguments:
39881c3436cfSJed Brown +  ts - time stepping context
39891c3436cfSJed Brown .  order - desired order of accuracy
39900298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
399105175c85SJed Brown 
399205175c85SJed Brown    Output Arguments:
39930910c330SBarry Smith .  U - state at the end of the current step
399405175c85SJed Brown 
399505175c85SJed Brown    Level: advanced
399605175c85SJed Brown 
3997108c343cSJed Brown    Notes:
3998108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3999108c343cSJed 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.
4000108c343cSJed Brown 
40011c3436cfSJed Brown .seealso: TSStep(), TSAdapt
400205175c85SJed Brown @*/
40030910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
400405175c85SJed Brown {
400505175c85SJed Brown   PetscErrorCode ierr;
400605175c85SJed Brown 
400705175c85SJed Brown   PetscFunctionBegin;
400805175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
400905175c85SJed Brown   PetscValidType(ts,1);
40100910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4011ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
40120910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
401305175c85SJed Brown   PetscFunctionReturn(0);
401405175c85SJed Brown }
401505175c85SJed Brown 
4016b1cb36f3SHong Zhang /*@
4017b1cb36f3SHong Zhang  TSForwardCostIntegral - Evaluate the cost integral in the forward run.
4018b1cb36f3SHong Zhang 
4019b1cb36f3SHong Zhang  Collective on TS
4020b1cb36f3SHong Zhang 
4021b1cb36f3SHong Zhang  Input Arguments:
4022b1cb36f3SHong Zhang  .  ts - time stepping context
4023b1cb36f3SHong Zhang 
4024b1cb36f3SHong Zhang  Level: advanced
4025b1cb36f3SHong Zhang 
4026b1cb36f3SHong Zhang  Notes:
4027b1cb36f3SHong Zhang  This function cannot be called until TSStep() has been completed.
4028b1cb36f3SHong Zhang 
4029b1cb36f3SHong Zhang  .seealso: TSSolve(), TSAdjointCostIntegral()
4030b1cb36f3SHong Zhang  @*/
4031b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts)
4032b1cb36f3SHong Zhang {
4033b1cb36f3SHong Zhang     PetscErrorCode ierr;
4034b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4035b1cb36f3SHong 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);
4036b1cb36f3SHong Zhang     ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr);
4037b1cb36f3SHong Zhang     PetscFunctionReturn(0);
4038b1cb36f3SHong Zhang }
4039d67e68b7SBarry Smith 
4040d763cef2SBarry Smith /*@
4041d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
4042d763cef2SBarry Smith 
4043d763cef2SBarry Smith    Collective on TS
4044d763cef2SBarry Smith 
4045d763cef2SBarry Smith    Input Parameter:
4046d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
404763e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
404863e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
40495a3a76d0SJed Brown 
4050d763cef2SBarry Smith    Level: beginner
4051d763cef2SBarry Smith 
40525a3a76d0SJed Brown    Notes:
40535a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
40545a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
40555a3a76d0SJed Brown    stepped over the final time.
40565a3a76d0SJed Brown 
4057d763cef2SBarry Smith .keywords: TS, timestep, solve
4058d763cef2SBarry Smith 
405963e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
4060d763cef2SBarry Smith @*/
4061cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
4062d763cef2SBarry Smith {
4063b06615a5SLisandro Dalcin   Vec               solution;
4064d763cef2SBarry Smith   PetscErrorCode    ierr;
4065d763cef2SBarry Smith 
4066d763cef2SBarry Smith   PetscFunctionBegin;
4067d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4068f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4069feed9e9dSBarry Smith 
407049354f04SShri 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 */
4071b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
40720910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4073b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4074b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4075b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
40765a3a76d0SJed Brown     }
40770910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4078bbd56ea5SKarl Rupp   } else if (u) {
40790910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
40805a3a76d0SJed Brown   }
4081b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
408268bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4083a6772fa2SLisandro Dalcin 
4084a6772fa2SLisandro 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()");
4085a6772fa2SLisandro 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");
4086a6772fa2SLisandro Dalcin 
4087e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4088e7069c78SShri      restarts the step after an event. Resetting these counters in such a case causes
4089e7069c78SShri      TSTrajectory to incorrectly save the output files
4090e7069c78SShri   */
4091938c94caSShri Abhyankar 
4092193ac0bcSJed Brown   ts->steps             = 0;
40935ef26d82SJed Brown   ts->ksp_its           = 0;
40945ef26d82SJed Brown   ts->snes_its          = 0;
4095c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
4096c610991cSLisandro Dalcin   ts->reject            = 0;
4097193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
4098193ac0bcSJed Brown 
4099ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4100f05ece33SBarry Smith 
4101193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4102193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4103a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4104cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4105a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4106be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4107db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
4108db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4109e7069c78SShri 
4110938c94caSShri Abhyankar     ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41116427ac75SLisandro Dalcin     ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
4112e7069c78SShri 
411328d5b5d6SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
411428d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_TRUE;
41156427ac75SLisandro Dalcin 
4116e1a7a14fSJed Brown     while (!ts->reason) {
4117938c94caSShri Abhyankar       ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41189687d888SLisandro Dalcin       if (!ts->steprollback) {
41199687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
41209687d888SLisandro Dalcin       }
4121193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4122e783b05fSHong 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. */
4123b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
4124b1cb36f3SHong Zhang       }
412510b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4126e783b05fSHong 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. */
4127e783b05fSHong Zhang       if (!ts->steprollback) {
4128938c94caSShri Abhyankar         ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41291eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4130aeb4809dSShri Abhyankar       }
4131193ac0bcSJed Brown     }
4132938c94caSShri Abhyankar     ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41336427ac75SLisandro Dalcin 
413449354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
41350910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4136cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4137b06615a5SLisandro Dalcin       solution = u;
413863e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
41390574a7fbSJed Brown     } else {
4140ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4141cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4142b06615a5SLisandro Dalcin       solution = ts->vec_sol;
41430574a7fbSJed Brown     }
4144193ac0bcSJed Brown   }
4145d2daff3dSHong Zhang 
4146ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
414763e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
414856f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4149ef222394SBarry Smith   if (ts->adjoint_solve) {
4150ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
41512b0a91c0SBarry Smith   }
4152d763cef2SBarry Smith   PetscFunctionReturn(0);
4153d763cef2SBarry Smith }
4154d763cef2SBarry Smith 
4155b1cb36f3SHong Zhang /*@
4156b1cb36f3SHong Zhang  TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run.
4157b1cb36f3SHong Zhang 
4158b1cb36f3SHong Zhang  Collective on TS
4159b1cb36f3SHong Zhang 
4160b1cb36f3SHong Zhang  Input Arguments:
4161b1cb36f3SHong Zhang  .  ts - time stepping context
4162b1cb36f3SHong Zhang 
4163b1cb36f3SHong Zhang  Level: advanced
4164b1cb36f3SHong Zhang 
4165b1cb36f3SHong Zhang  Notes:
4166b1cb36f3SHong Zhang  This function cannot be called until TSAdjointStep() has been completed.
4167b1cb36f3SHong Zhang 
4168b1cb36f3SHong Zhang  .seealso: TSAdjointSolve(), TSAdjointStep
4169b1cb36f3SHong Zhang  @*/
4170b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts)
4171b1cb36f3SHong Zhang {
4172b1cb36f3SHong Zhang     PetscErrorCode ierr;
4173b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4174b1cb36f3SHong 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);
4175b1cb36f3SHong Zhang     ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr);
4176b1cb36f3SHong Zhang     PetscFunctionReturn(0);
4177b1cb36f3SHong Zhang }
4178b1cb36f3SHong Zhang 
4179c88f2d44SBarry Smith /*@
4180c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
4181c88f2d44SBarry Smith 
4182c88f2d44SBarry Smith    Collective on TS
4183c88f2d44SBarry Smith 
4184c88f2d44SBarry Smith    Input Parameter:
41852c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
4186c88f2d44SBarry Smith 
4187e87fd094SBarry Smith    Options Database:
4188e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions
4189e87fd094SBarry Smith 
4190c88f2d44SBarry Smith    Level: intermediate
4191c88f2d44SBarry Smith 
4192c88f2d44SBarry Smith    Notes:
4193c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
4194c88f2d44SBarry Smith 
419573b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
419673b18844SBarry Smith 
4197c88f2d44SBarry Smith .keywords: TS, timestep, solve
4198c88f2d44SBarry Smith 
4199b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
4200c88f2d44SBarry Smith @*/
42012c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
4202c88f2d44SBarry Smith {
4203c88f2d44SBarry Smith   PetscErrorCode    ierr;
4204c88f2d44SBarry Smith 
4205c88f2d44SBarry Smith   PetscFunctionBegin;
4206c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4207c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
420868bece0bSHong Zhang 
4209c88f2d44SBarry Smith   /* reset time step and iteration counters */
4210c88f2d44SBarry Smith   ts->steps             = 0;
4211c88f2d44SBarry Smith   ts->ksp_its           = 0;
4212c88f2d44SBarry Smith   ts->snes_its          = 0;
4213c88f2d44SBarry Smith   ts->num_snes_failures = 0;
4214c88f2d44SBarry Smith   ts->reject            = 0;
4215c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
4216c88f2d44SBarry Smith 
421773b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
4218c88f2d44SBarry Smith 
421973b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
4220c88f2d44SBarry Smith   while (!ts->reason) {
422155c52731SHong Zhang     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
42229110b2e7SHong Zhang     ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
42236427ac75SLisandro Dalcin     ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr);
4224616946c1SHong Zhang     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
4225b1cb36f3SHong Zhang     if (ts->vec_costintegral && !ts->costintegralfwd) {
4226b1cb36f3SHong Zhang       ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr);
4227b1cb36f3SHong Zhang     }
4228c88f2d44SBarry Smith   }
422926656371SHong Zhang   ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
423026656371SHong Zhang   ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
4231c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
4232e210cd0eSHong Zhang   ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr);
4233685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr);
4234c88f2d44SBarry Smith   PetscFunctionReturn(0);
4235c88f2d44SBarry Smith }
4236c88f2d44SBarry Smith 
42377db568b7SBarry Smith /*@C
4238228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4239228d79bcSJed Brown 
4240228d79bcSJed Brown    Collective on TS
4241228d79bcSJed Brown 
4242228d79bcSJed Brown    Input Parameters:
4243228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4244228d79bcSJed Brown .  step - step number that has just completed
4245228d79bcSJed Brown .  ptime - model time of the state
42460910c330SBarry Smith -  u - state at the current model time
4247228d79bcSJed Brown 
4248228d79bcSJed Brown    Notes:
42497db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
42507db568b7SBarry Smith    Users would almost never call this routine directly.
4251228d79bcSJed Brown 
425263e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
425363e21af5SBarry Smith 
42547db568b7SBarry Smith    Level: developer
4255228d79bcSJed Brown 
4256228d79bcSJed Brown .keywords: TS, timestep
4257228d79bcSJed Brown @*/
42580910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4259d763cef2SBarry Smith {
4260d6152f81SLisandro Dalcin   DM             dm;
4261a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4262d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4263d763cef2SBarry Smith 
4264d763cef2SBarry Smith   PetscFunctionBegin;
4265b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4266b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4267d6152f81SLisandro Dalcin 
4268d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4269d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4270d6152f81SLisandro Dalcin 
42715edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
4272d763cef2SBarry Smith   for (i=0; i<n; i++) {
42730910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4274d763cef2SBarry Smith   }
42755edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
4276d763cef2SBarry Smith   PetscFunctionReturn(0);
4277d763cef2SBarry Smith }
4278d763cef2SBarry Smith 
42797db568b7SBarry Smith /*@C
42809110b2e7SHong Zhang    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()
42819110b2e7SHong Zhang 
42829110b2e7SHong Zhang    Collective on TS
42839110b2e7SHong Zhang 
42849110b2e7SHong Zhang    Input Parameters:
42859110b2e7SHong Zhang +  ts - time stepping context obtained from TSCreate()
42869110b2e7SHong Zhang .  step - step number that has just completed
42879110b2e7SHong Zhang .  ptime - model time of the state
42889110b2e7SHong Zhang .  u - state at the current model time
42899110b2e7SHong Zhang .  numcost - number of cost functions (dimension of lambda  or mu)
42909110b2e7SHong Zhang .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
42919110b2e7SHong Zhang -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
42929110b2e7SHong Zhang 
42939110b2e7SHong Zhang    Notes:
42947db568b7SBarry Smith    TSAdjointMonitor() is typically used automatically within the time stepping implementations.
42957db568b7SBarry Smith    Users would almost never call this routine directly.
42969110b2e7SHong Zhang 
42977db568b7SBarry Smith    Level: developer
42989110b2e7SHong Zhang 
42999110b2e7SHong Zhang .keywords: TS, timestep
43009110b2e7SHong Zhang @*/
43019110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
43029110b2e7SHong Zhang {
43039110b2e7SHong Zhang   PetscErrorCode ierr;
43049110b2e7SHong Zhang   PetscInt       i,n = ts->numberadjointmonitors;
43059110b2e7SHong Zhang 
43069110b2e7SHong Zhang   PetscFunctionBegin;
43079110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43089110b2e7SHong Zhang   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
43099110b2e7SHong Zhang   ierr = VecLockPush(u);CHKERRQ(ierr);
43109110b2e7SHong Zhang   for (i=0; i<n; i++) {
43119110b2e7SHong Zhang     ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
43129110b2e7SHong Zhang   }
43139110b2e7SHong Zhang   ierr = VecLockPop(u);CHKERRQ(ierr);
43149110b2e7SHong Zhang   PetscFunctionReturn(0);
43159110b2e7SHong Zhang }
43169110b2e7SHong Zhang 
4317d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4318d763cef2SBarry Smith /*@C
43197db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4320a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4321d763cef2SBarry Smith 
4322d763cef2SBarry Smith    Collective on TS
4323d763cef2SBarry Smith 
4324d763cef2SBarry Smith    Input Parameters:
4325d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4326d763cef2SBarry Smith .  label - the title to put in the title bar
43277c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4328a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4329a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4330d763cef2SBarry Smith 
4331d763cef2SBarry Smith    Output Parameter:
43320b039ecaSBarry Smith .  ctx - the context
4333d763cef2SBarry Smith 
4334d763cef2SBarry Smith    Options Database Key:
43354f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
43367db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
43377db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
43387db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
43397db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4340b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4341d763cef2SBarry Smith 
4342d763cef2SBarry Smith    Notes:
4343a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4344d763cef2SBarry Smith 
43457db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
43467db568b7SBarry Smith 
43477db568b7SBarry 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
43487db568b7SBarry 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
43497db568b7SBarry Smith    as the first argument.
43507db568b7SBarry Smith 
43517db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
43527db568b7SBarry Smith 
43537db568b7SBarry Smith 
4354d763cef2SBarry Smith    Level: intermediate
4355d763cef2SBarry Smith 
43567db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
4357d763cef2SBarry Smith 
43587db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
43597db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
43607db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
43617db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
43627db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
43637c922b88SBarry Smith 
4364d763cef2SBarry Smith @*/
4365a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4366d763cef2SBarry Smith {
43677f52daa2SLisandro Dalcin   PetscDraw      draw;
4368dfbe8321SBarry Smith   PetscErrorCode ierr;
4369d763cef2SBarry Smith 
4370d763cef2SBarry Smith   PetscFunctionBegin;
4371b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
43727f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43737f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43747f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4375287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
43767f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4377a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4378d763cef2SBarry Smith   PetscFunctionReturn(0);
4379d763cef2SBarry Smith }
4380d763cef2SBarry Smith 
4381b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4382d763cef2SBarry Smith {
43830b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4384c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4385dfbe8321SBarry Smith   PetscErrorCode ierr;
4386d763cef2SBarry Smith 
4387d763cef2SBarry Smith   PetscFunctionBegin;
438863e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4389b06615a5SLisandro Dalcin   if (!step) {
4390a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4391a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
43926934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr);
4393a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4394a9f9c1f6SBarry Smith   }
4395c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
43960b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4397b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
43980b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
43996934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
44003923b477SBarry Smith   }
4401d763cef2SBarry Smith   PetscFunctionReturn(0);
4402d763cef2SBarry Smith }
4403d763cef2SBarry Smith 
4404d763cef2SBarry Smith /*@C
4405a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4406a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4407d763cef2SBarry Smith 
44080b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4409d763cef2SBarry Smith 
4410d763cef2SBarry Smith    Input Parameter:
44110b039ecaSBarry Smith .  ctx - the monitor context
4412d763cef2SBarry Smith 
4413d763cef2SBarry Smith    Level: intermediate
4414d763cef2SBarry Smith 
4415d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
4416d763cef2SBarry Smith 
44174f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4418d763cef2SBarry Smith @*/
4419a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4420d763cef2SBarry Smith {
4421dfbe8321SBarry Smith   PetscErrorCode ierr;
4422d763cef2SBarry Smith 
4423d763cef2SBarry Smith   PetscFunctionBegin;
44247684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
44257684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
44267684fa3eSBarry Smith   }
44270b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
442831152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4429387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4430387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4431387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
44320b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4433d763cef2SBarry Smith   PetscFunctionReturn(0);
4434d763cef2SBarry Smith }
4435d763cef2SBarry Smith 
4436d763cef2SBarry Smith /*@
4437b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4438d763cef2SBarry Smith 
4439d763cef2SBarry Smith    Not Collective
4440d763cef2SBarry Smith 
4441d763cef2SBarry Smith    Input Parameter:
4442d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4443d763cef2SBarry Smith 
4444d763cef2SBarry Smith    Output Parameter:
444563e21af5SBarry Smith .  t  - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime().
4446d763cef2SBarry Smith 
4447d763cef2SBarry Smith    Level: beginner
4448d763cef2SBarry Smith 
4449b8123daeSJed Brown    Note:
4450b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
44519be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4452b8123daeSJed Brown 
445363e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime()
4454d763cef2SBarry Smith 
4455d763cef2SBarry Smith .keywords: TS, get, time
4456d763cef2SBarry Smith @*/
44577087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4458d763cef2SBarry Smith {
4459d763cef2SBarry Smith   PetscFunctionBegin;
44600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4461f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4462d763cef2SBarry Smith   *t = ts->ptime;
4463d763cef2SBarry Smith   PetscFunctionReturn(0);
4464d763cef2SBarry Smith }
4465d763cef2SBarry Smith 
4466e5e524a1SHong Zhang /*@
4467e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4468e5e524a1SHong Zhang 
4469e5e524a1SHong Zhang    Not Collective
4470e5e524a1SHong Zhang 
4471e5e524a1SHong Zhang    Input Parameter:
4472e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4473e5e524a1SHong Zhang 
4474e5e524a1SHong Zhang    Output Parameter:
4475e5e524a1SHong Zhang .  t  - the previous time
4476e5e524a1SHong Zhang 
4477e5e524a1SHong Zhang    Level: beginner
4478e5e524a1SHong Zhang 
4479e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
4480e5e524a1SHong Zhang 
4481e5e524a1SHong Zhang .keywords: TS, get, time
4482e5e524a1SHong Zhang @*/
4483e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4484e5e524a1SHong Zhang {
4485e5e524a1SHong Zhang   PetscFunctionBegin;
4486e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4487e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4488e5e524a1SHong Zhang   *t = ts->ptime_prev;
4489e5e524a1SHong Zhang   PetscFunctionReturn(0);
4490e5e524a1SHong Zhang }
4491e5e524a1SHong Zhang 
44926a4d4014SLisandro Dalcin /*@
44936a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
44946a4d4014SLisandro Dalcin 
44953f9fe445SBarry Smith    Logically Collective on TS
44966a4d4014SLisandro Dalcin 
44976a4d4014SLisandro Dalcin    Input Parameters:
44986a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
44996a4d4014SLisandro Dalcin -  time - the time
45006a4d4014SLisandro Dalcin 
45016a4d4014SLisandro Dalcin    Level: intermediate
45026a4d4014SLisandro Dalcin 
45036a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
45046a4d4014SLisandro Dalcin 
45056a4d4014SLisandro Dalcin .keywords: TS, set, time
45066a4d4014SLisandro Dalcin @*/
45077087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
45086a4d4014SLisandro Dalcin {
45096a4d4014SLisandro Dalcin   PetscFunctionBegin;
45100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4511c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
45126a4d4014SLisandro Dalcin   ts->ptime = t;
45136a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
45146a4d4014SLisandro Dalcin }
45156a4d4014SLisandro Dalcin 
4516d763cef2SBarry Smith /*@C
4517d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4518d763cef2SBarry Smith    TS options in the database.
4519d763cef2SBarry Smith 
45203f9fe445SBarry Smith    Logically Collective on TS
4521d763cef2SBarry Smith 
4522d763cef2SBarry Smith    Input Parameter:
4523d763cef2SBarry Smith +  ts     - The TS context
4524d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4525d763cef2SBarry Smith 
4526d763cef2SBarry Smith    Notes:
4527d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4528d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4529d763cef2SBarry Smith    hyphen.
4530d763cef2SBarry Smith 
4531d763cef2SBarry Smith    Level: advanced
4532d763cef2SBarry Smith 
4533d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4534d763cef2SBarry Smith 
4535d763cef2SBarry Smith .seealso: TSSetFromOptions()
4536d763cef2SBarry Smith 
4537d763cef2SBarry Smith @*/
45387087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4539d763cef2SBarry Smith {
4540dfbe8321SBarry Smith   PetscErrorCode ierr;
4541089b2837SJed Brown   SNES           snes;
4542d763cef2SBarry Smith 
4543d763cef2SBarry Smith   PetscFunctionBegin;
45440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4545d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4546089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4547089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4548d763cef2SBarry Smith   PetscFunctionReturn(0);
4549d763cef2SBarry Smith }
4550d763cef2SBarry Smith 
4551d763cef2SBarry Smith 
4552d763cef2SBarry Smith /*@C
4553d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4554d763cef2SBarry Smith    TS options in the database.
4555d763cef2SBarry Smith 
45563f9fe445SBarry Smith    Logically Collective on TS
4557d763cef2SBarry Smith 
4558d763cef2SBarry Smith    Input Parameter:
4559d763cef2SBarry Smith +  ts     - The TS context
4560d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4561d763cef2SBarry Smith 
4562d763cef2SBarry Smith    Notes:
4563d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4564d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4565d763cef2SBarry Smith    hyphen.
4566d763cef2SBarry Smith 
4567d763cef2SBarry Smith    Level: advanced
4568d763cef2SBarry Smith 
4569d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4570d763cef2SBarry Smith 
4571d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4572d763cef2SBarry Smith 
4573d763cef2SBarry Smith @*/
45747087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4575d763cef2SBarry Smith {
4576dfbe8321SBarry Smith   PetscErrorCode ierr;
4577089b2837SJed Brown   SNES           snes;
4578d763cef2SBarry Smith 
4579d763cef2SBarry Smith   PetscFunctionBegin;
45800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4581d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4582089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4583089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4584d763cef2SBarry Smith   PetscFunctionReturn(0);
4585d763cef2SBarry Smith }
4586d763cef2SBarry Smith 
4587d763cef2SBarry Smith /*@C
4588d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4589d763cef2SBarry Smith    TS options in the database.
4590d763cef2SBarry Smith 
4591d763cef2SBarry Smith    Not Collective
4592d763cef2SBarry Smith 
4593d763cef2SBarry Smith    Input Parameter:
4594d763cef2SBarry Smith .  ts - The TS context
4595d763cef2SBarry Smith 
4596d763cef2SBarry Smith    Output Parameter:
4597d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4598d763cef2SBarry Smith 
4599d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
4600d763cef2SBarry Smith    sufficient length to hold the prefix.
4601d763cef2SBarry Smith 
4602d763cef2SBarry Smith    Level: intermediate
4603d763cef2SBarry Smith 
4604d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4605d763cef2SBarry Smith 
4606d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4607d763cef2SBarry Smith @*/
46087087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4609d763cef2SBarry Smith {
4610dfbe8321SBarry Smith   PetscErrorCode ierr;
4611d763cef2SBarry Smith 
4612d763cef2SBarry Smith   PetscFunctionBegin;
46130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46144482741eSBarry Smith   PetscValidPointer(prefix,2);
4615d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4616d763cef2SBarry Smith   PetscFunctionReturn(0);
4617d763cef2SBarry Smith }
4618d763cef2SBarry Smith 
4619d763cef2SBarry Smith /*@C
4620d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4621d763cef2SBarry Smith 
4622d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4623d763cef2SBarry Smith 
4624d763cef2SBarry Smith    Input Parameter:
4625d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4626d763cef2SBarry Smith 
4627d763cef2SBarry Smith    Output Parameters:
4628e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4629e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4630e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4631e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4632d763cef2SBarry Smith 
46330298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
4634d763cef2SBarry Smith 
4635d763cef2SBarry Smith    Level: intermediate
4636d763cef2SBarry Smith 
463726d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
4638d763cef2SBarry Smith 
4639d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4640d763cef2SBarry Smith @*/
4641e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4642d763cef2SBarry Smith {
4643089b2837SJed Brown   PetscErrorCode ierr;
464424989b8cSPeter Brune   DM             dm;
4645089b2837SJed Brown 
4646d763cef2SBarry Smith   PetscFunctionBegin;
464723a57915SBarry Smith   if (Amat || Pmat) {
464823a57915SBarry Smith     SNES snes;
4649089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
465023a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4651e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
465223a57915SBarry Smith   }
465324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
465424989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4655d763cef2SBarry Smith   PetscFunctionReturn(0);
4656d763cef2SBarry Smith }
4657d763cef2SBarry Smith 
46582eca1d9cSJed Brown /*@C
46592eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
46602eca1d9cSJed Brown 
46612eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
46622eca1d9cSJed Brown 
46632eca1d9cSJed Brown    Input Parameter:
46642eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
46652eca1d9cSJed Brown 
46662eca1d9cSJed Brown    Output Parameters:
4667e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4668e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
46692eca1d9cSJed Brown .  f   - The function to compute the matrices
46702eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
46712eca1d9cSJed Brown 
46720298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
46732eca1d9cSJed Brown 
46742eca1d9cSJed Brown    Level: advanced
46752eca1d9cSJed Brown 
46762eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
46772eca1d9cSJed Brown 
46782eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
46792eca1d9cSJed Brown @*/
4680e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
46812eca1d9cSJed Brown {
4682089b2837SJed Brown   PetscErrorCode ierr;
468324989b8cSPeter Brune   DM             dm;
46840910c330SBarry Smith 
46852eca1d9cSJed Brown   PetscFunctionBegin;
4686c0aab802Sstefano_zampini   if (Amat || Pmat) {
4687c0aab802Sstefano_zampini     SNES snes;
4688089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4689f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4690e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4691c0aab802Sstefano_zampini   }
469224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
469324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
46942eca1d9cSJed Brown   PetscFunctionReturn(0);
46952eca1d9cSJed Brown }
46962eca1d9cSJed Brown 
46976083293cSBarry Smith 
46981713a123SBarry Smith /*@C
469983a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
47001713a123SBarry Smith    VecView() for the solution at each timestep
47011713a123SBarry Smith 
47021713a123SBarry Smith    Collective on TS
47031713a123SBarry Smith 
47041713a123SBarry Smith    Input Parameters:
47051713a123SBarry Smith +  ts - the TS context
47061713a123SBarry Smith .  step - current time-step
4707142b95e3SSatish Balay .  ptime - current time
47080298fd71SBarry Smith -  dummy - either a viewer or NULL
47091713a123SBarry Smith 
471099fdda47SBarry Smith    Options Database:
471199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
471299fdda47SBarry Smith 
4713387f4636SBarry 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
471499fdda47SBarry Smith        will look bad
471599fdda47SBarry Smith 
47161713a123SBarry Smith    Level: intermediate
47171713a123SBarry Smith 
47181713a123SBarry Smith .keywords: TS,  vector, monitor, view
47191713a123SBarry Smith 
4720a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
47211713a123SBarry Smith @*/
47220910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
47231713a123SBarry Smith {
4724dfbe8321SBarry Smith   PetscErrorCode   ierr;
472583a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4726473a3ab2SBarry Smith   PetscDraw        draw;
47271713a123SBarry Smith 
47281713a123SBarry Smith   PetscFunctionBegin;
47296083293cSBarry Smith   if (!step && ictx->showinitial) {
47306083293cSBarry Smith     if (!ictx->initialsolution) {
47310910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
47321713a123SBarry Smith     }
47330910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
47346083293cSBarry Smith   }
4735b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
47360dcf80beSBarry Smith 
47376083293cSBarry Smith   if (ictx->showinitial) {
47386083293cSBarry Smith     PetscReal pause;
47396083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
47406083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
47416083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
47426083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
47436083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
47446083293cSBarry Smith   }
47450910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4746473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
474751fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4748473a3ab2SBarry Smith     char      time[32];
4749473a3ab2SBarry Smith 
4750473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
475185b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4752473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4753473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
475451fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4755473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4756473a3ab2SBarry Smith   }
4757473a3ab2SBarry Smith 
47586083293cSBarry Smith   if (ictx->showinitial) {
47596083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
47606083293cSBarry Smith   }
47611713a123SBarry Smith   PetscFunctionReturn(0);
47621713a123SBarry Smith }
47631713a123SBarry Smith 
47649110b2e7SHong Zhang /*@C
47659110b2e7SHong Zhang    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
47669110b2e7SHong Zhang    VecView() for the sensitivities to initial states at each timestep
47679110b2e7SHong Zhang 
47689110b2e7SHong Zhang    Collective on TS
47699110b2e7SHong Zhang 
47709110b2e7SHong Zhang    Input Parameters:
47719110b2e7SHong Zhang +  ts - the TS context
47729110b2e7SHong Zhang .  step - current time-step
47739110b2e7SHong Zhang .  ptime - current time
47749110b2e7SHong Zhang .  u - current state
47759110b2e7SHong Zhang .  numcost - number of cost functions
47769110b2e7SHong Zhang .  lambda - sensitivities to initial conditions
47779110b2e7SHong Zhang .  mu - sensitivities to parameters
47789110b2e7SHong Zhang -  dummy - either a viewer or NULL
47799110b2e7SHong Zhang 
47809110b2e7SHong Zhang    Level: intermediate
47819110b2e7SHong Zhang 
47829110b2e7SHong Zhang .keywords: TS,  vector, adjoint, monitor, view
47839110b2e7SHong Zhang 
47849110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
47859110b2e7SHong Zhang @*/
47869110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
47879110b2e7SHong Zhang {
47889110b2e7SHong Zhang   PetscErrorCode   ierr;
47899110b2e7SHong Zhang   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
47909110b2e7SHong Zhang   PetscDraw        draw;
4791a0046e2cSSatish Balay   PetscReal        xl,yl,xr,yr,h;
4792a0046e2cSSatish Balay   char             time[32];
47939110b2e7SHong Zhang 
47949110b2e7SHong Zhang   PetscFunctionBegin;
47959110b2e7SHong Zhang   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
47969110b2e7SHong Zhang 
47979110b2e7SHong Zhang   ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr);
47989110b2e7SHong Zhang   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
47999110b2e7SHong Zhang   ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
48009110b2e7SHong Zhang   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
48019110b2e7SHong Zhang   h    = yl + .95*(yr - yl);
48029110b2e7SHong Zhang   ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
48039110b2e7SHong Zhang   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
48049110b2e7SHong Zhang   PetscFunctionReturn(0);
48059110b2e7SHong Zhang }
48069110b2e7SHong Zhang 
48072d5ee99bSBarry Smith /*@C
48082d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
48092d5ee99bSBarry Smith 
48102d5ee99bSBarry Smith    Collective on TS
48112d5ee99bSBarry Smith 
48122d5ee99bSBarry Smith    Input Parameters:
48132d5ee99bSBarry Smith +  ts - the TS context
48142d5ee99bSBarry Smith .  step - current time-step
48152d5ee99bSBarry Smith .  ptime - current time
48162d5ee99bSBarry Smith -  dummy - either a viewer or NULL
48172d5ee99bSBarry Smith 
48182d5ee99bSBarry Smith    Level: intermediate
48192d5ee99bSBarry Smith 
48202d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
48212d5ee99bSBarry Smith 
48222d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
48232d5ee99bSBarry Smith @*/
48242d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48252d5ee99bSBarry Smith {
48262d5ee99bSBarry Smith   PetscErrorCode    ierr;
48272d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
48282d5ee99bSBarry Smith   PetscDraw         draw;
48296934998bSLisandro Dalcin   PetscDrawAxis     axis;
48302d5ee99bSBarry Smith   PetscInt          n;
48312d5ee99bSBarry Smith   PetscMPIInt       size;
48326934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
48332d5ee99bSBarry Smith   char              time[32];
48342d5ee99bSBarry Smith   const PetscScalar *U;
48352d5ee99bSBarry Smith 
48362d5ee99bSBarry Smith   PetscFunctionBegin;
48376934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
48386934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
48392d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
48406934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
48412d5ee99bSBarry Smith 
48422d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
48436934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
48446934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
48456934998bSLisandro Dalcin   if (!step) {
48466934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
48476934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
48486934998bSLisandro Dalcin   }
48492d5ee99bSBarry Smith 
48502d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
48516934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
48526934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4853a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
48546934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
48552d5ee99bSBarry Smith 
48566934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
48576934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
48582d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
48594b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
486085b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
48612d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
486251fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
48632d5ee99bSBarry Smith   }
48646934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
48652d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
486656d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
48676934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
48682d5ee99bSBarry Smith   PetscFunctionReturn(0);
48692d5ee99bSBarry Smith }
48702d5ee99bSBarry Smith 
48711713a123SBarry Smith 
48726083293cSBarry Smith /*@C
487383a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
48746083293cSBarry Smith 
48756083293cSBarry Smith    Collective on TS
48766083293cSBarry Smith 
48776083293cSBarry Smith    Input Parameters:
48786083293cSBarry Smith .    ctx - the monitor context
48796083293cSBarry Smith 
48806083293cSBarry Smith    Level: intermediate
48816083293cSBarry Smith 
48826083293cSBarry Smith .keywords: TS,  vector, monitor, view
48836083293cSBarry Smith 
488483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
48856083293cSBarry Smith @*/
488683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
48876083293cSBarry Smith {
48886083293cSBarry Smith   PetscErrorCode ierr;
48896083293cSBarry Smith 
48906083293cSBarry Smith   PetscFunctionBegin;
489183a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
489283a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
489383a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
48946083293cSBarry Smith   PetscFunctionReturn(0);
48956083293cSBarry Smith }
48966083293cSBarry Smith 
48976083293cSBarry Smith /*@C
489883a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
48996083293cSBarry Smith 
49006083293cSBarry Smith    Collective on TS
49016083293cSBarry Smith 
49026083293cSBarry Smith    Input Parameter:
49036083293cSBarry Smith .    ts - time-step context
49046083293cSBarry Smith 
49056083293cSBarry Smith    Output Patameter:
49066083293cSBarry Smith .    ctx - the monitor context
49076083293cSBarry Smith 
490899fdda47SBarry Smith    Options Database:
490999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
491099fdda47SBarry Smith 
49116083293cSBarry Smith    Level: intermediate
49126083293cSBarry Smith 
49136083293cSBarry Smith .keywords: TS,  vector, monitor, view
49146083293cSBarry Smith 
491583a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
49166083293cSBarry Smith @*/
491783a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
49186083293cSBarry Smith {
49196083293cSBarry Smith   PetscErrorCode   ierr;
49206083293cSBarry Smith 
49216083293cSBarry Smith   PetscFunctionBegin;
4922b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
492383a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4924e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4925bbd56ea5SKarl Rupp 
492683a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4927473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4928c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4929473a3ab2SBarry Smith 
4930473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4931c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
49326083293cSBarry Smith   PetscFunctionReturn(0);
49336083293cSBarry Smith }
49346083293cSBarry Smith 
49353a471f94SBarry Smith /*@C
493683a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
49373a471f94SBarry Smith    VecView() for the error at each timestep
49383a471f94SBarry Smith 
49393a471f94SBarry Smith    Collective on TS
49403a471f94SBarry Smith 
49413a471f94SBarry Smith    Input Parameters:
49423a471f94SBarry Smith +  ts - the TS context
49433a471f94SBarry Smith .  step - current time-step
49443a471f94SBarry Smith .  ptime - current time
49450298fd71SBarry Smith -  dummy - either a viewer or NULL
49463a471f94SBarry Smith 
49473a471f94SBarry Smith    Level: intermediate
49483a471f94SBarry Smith 
49493a471f94SBarry Smith .keywords: TS,  vector, monitor, view
49503a471f94SBarry Smith 
49513a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
49523a471f94SBarry Smith @*/
49530910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
49543a471f94SBarry Smith {
49553a471f94SBarry Smith   PetscErrorCode   ierr;
495683a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
495783a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
49583a471f94SBarry Smith   Vec              work;
49593a471f94SBarry Smith 
49603a471f94SBarry Smith   PetscFunctionBegin;
4961b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
49620910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
49633a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
49640910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
49653a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
49663a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
49673a471f94SBarry Smith   PetscFunctionReturn(0);
49683a471f94SBarry Smith }
49693a471f94SBarry Smith 
4970af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
49716c699258SBarry Smith /*@
49722a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
49736c699258SBarry Smith 
49743f9fe445SBarry Smith    Logically Collective on TS and DM
49756c699258SBarry Smith 
49766c699258SBarry Smith    Input Parameters:
49772a808120SBarry Smith +  ts - the ODE integrator object
49782a808120SBarry Smith -  dm - the dm, cannot be NULL
49796c699258SBarry Smith 
49806c699258SBarry Smith    Level: intermediate
49816c699258SBarry Smith 
49826c699258SBarry Smith 
49836c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
49846c699258SBarry Smith @*/
49857087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
49866c699258SBarry Smith {
49876c699258SBarry Smith   PetscErrorCode ierr;
4988089b2837SJed Brown   SNES           snes;
4989942e3340SBarry Smith   DMTS           tsdm;
49906c699258SBarry Smith 
49916c699258SBarry Smith   PetscFunctionBegin;
49920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49932a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
499470663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4995942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
49962a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4997942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4998942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
499924989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
500024989b8cSPeter Brune         tsdm->originaldm = dm;
500124989b8cSPeter Brune       }
500224989b8cSPeter Brune     }
50036bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
500424989b8cSPeter Brune   }
50056c699258SBarry Smith   ts->dm = dm;
5006bbd56ea5SKarl Rupp 
5007089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5008089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
50096c699258SBarry Smith   PetscFunctionReturn(0);
50106c699258SBarry Smith }
50116c699258SBarry Smith 
50126c699258SBarry Smith /*@
50136c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
50146c699258SBarry Smith 
50153f9fe445SBarry Smith    Not Collective
50166c699258SBarry Smith 
50176c699258SBarry Smith    Input Parameter:
50186c699258SBarry Smith . ts - the preconditioner context
50196c699258SBarry Smith 
50206c699258SBarry Smith    Output Parameter:
50216c699258SBarry Smith .  dm - the dm
50226c699258SBarry Smith 
50236c699258SBarry Smith    Level: intermediate
50246c699258SBarry Smith 
50256c699258SBarry Smith 
50266c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
50276c699258SBarry Smith @*/
50287087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
50296c699258SBarry Smith {
5030496e6a7aSJed Brown   PetscErrorCode ierr;
5031496e6a7aSJed Brown 
50326c699258SBarry Smith   PetscFunctionBegin;
50330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5034496e6a7aSJed Brown   if (!ts->dm) {
5035ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
5036496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
5037496e6a7aSJed Brown   }
50386c699258SBarry Smith   *dm = ts->dm;
50396c699258SBarry Smith   PetscFunctionReturn(0);
50406c699258SBarry Smith }
50411713a123SBarry Smith 
50420f5c6efeSJed Brown /*@
50430f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
50440f5c6efeSJed Brown 
50453f9fe445SBarry Smith    Logically Collective on SNES
50460f5c6efeSJed Brown 
50470f5c6efeSJed Brown    Input Parameter:
5048d42a1c89SJed Brown + snes - nonlinear solver
50490910c330SBarry Smith . U - the current state at which to evaluate the residual
5050d42a1c89SJed Brown - ctx - user context, must be a TS
50510f5c6efeSJed Brown 
50520f5c6efeSJed Brown    Output Parameter:
50530f5c6efeSJed Brown . F - the nonlinear residual
50540f5c6efeSJed Brown 
50550f5c6efeSJed Brown    Notes:
50560f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50570f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
50580f5c6efeSJed Brown 
50590f5c6efeSJed Brown    Level: advanced
50600f5c6efeSJed Brown 
50610f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
50620f5c6efeSJed Brown @*/
50630910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
50640f5c6efeSJed Brown {
50650f5c6efeSJed Brown   TS             ts = (TS)ctx;
50660f5c6efeSJed Brown   PetscErrorCode ierr;
50670f5c6efeSJed Brown 
50680f5c6efeSJed Brown   PetscFunctionBegin;
50690f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50700910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50710f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
50720f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
50730910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
50740f5c6efeSJed Brown   PetscFunctionReturn(0);
50750f5c6efeSJed Brown }
50760f5c6efeSJed Brown 
50770f5c6efeSJed Brown /*@
50780f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
50790f5c6efeSJed Brown 
50800f5c6efeSJed Brown    Collective on SNES
50810f5c6efeSJed Brown 
50820f5c6efeSJed Brown    Input Parameter:
50830f5c6efeSJed Brown + snes - nonlinear solver
50840910c330SBarry Smith . U - the current state at which to evaluate the residual
50850f5c6efeSJed Brown - ctx - user context, must be a TS
50860f5c6efeSJed Brown 
50870f5c6efeSJed Brown    Output Parameter:
50880f5c6efeSJed Brown + A - the Jacobian
50890f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
50900f5c6efeSJed Brown - flag - indicates any structure change in the matrix
50910f5c6efeSJed Brown 
50920f5c6efeSJed Brown    Notes:
50930f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50940f5c6efeSJed Brown 
50950f5c6efeSJed Brown    Level: developer
50960f5c6efeSJed Brown 
50970f5c6efeSJed Brown .seealso: SNESSetJacobian()
50980f5c6efeSJed Brown @*/
5099d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
51000f5c6efeSJed Brown {
51010f5c6efeSJed Brown   TS             ts = (TS)ctx;
51020f5c6efeSJed Brown   PetscErrorCode ierr;
51030f5c6efeSJed Brown 
51040f5c6efeSJed Brown   PetscFunctionBegin;
51050f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
51060910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
51070f5c6efeSJed Brown   PetscValidPointer(A,3);
510894ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
51090f5c6efeSJed Brown   PetscValidPointer(B,4);
511094ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
51110f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5112d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
51130f5c6efeSJed Brown   PetscFunctionReturn(0);
51140f5c6efeSJed Brown }
5115325fc9f4SBarry Smith 
51160e4ef248SJed Brown /*@C
51179ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
51180e4ef248SJed Brown 
51190e4ef248SJed Brown    Collective on TS
51200e4ef248SJed Brown 
51210e4ef248SJed Brown    Input Arguments:
51220e4ef248SJed Brown +  ts - time stepping context
51230e4ef248SJed Brown .  t - time at which to evaluate
51240910c330SBarry Smith .  U - state at which to evaluate
51250e4ef248SJed Brown -  ctx - context
51260e4ef248SJed Brown 
51270e4ef248SJed Brown    Output Arguments:
51280e4ef248SJed Brown .  F - right hand side
51290e4ef248SJed Brown 
51300e4ef248SJed Brown    Level: intermediate
51310e4ef248SJed Brown 
51320e4ef248SJed Brown    Notes:
51330e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
51340e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
51350e4ef248SJed Brown 
51360e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
51370e4ef248SJed Brown @*/
51380910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
51390e4ef248SJed Brown {
51400e4ef248SJed Brown   PetscErrorCode ierr;
51410e4ef248SJed Brown   Mat            Arhs,Brhs;
51420e4ef248SJed Brown 
51430e4ef248SJed Brown   PetscFunctionBegin;
51440e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
5145d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
51460910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
51470e4ef248SJed Brown   PetscFunctionReturn(0);
51480e4ef248SJed Brown }
51490e4ef248SJed Brown 
51500e4ef248SJed Brown /*@C
51510e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
51520e4ef248SJed Brown 
51530e4ef248SJed Brown    Collective on TS
51540e4ef248SJed Brown 
51550e4ef248SJed Brown    Input Arguments:
51560e4ef248SJed Brown +  ts - time stepping context
51570e4ef248SJed Brown .  t - time at which to evaluate
51580910c330SBarry Smith .  U - state at which to evaluate
51590e4ef248SJed Brown -  ctx - context
51600e4ef248SJed Brown 
51610e4ef248SJed Brown    Output Arguments:
51620e4ef248SJed Brown +  A - pointer to operator
51630e4ef248SJed Brown .  B - pointer to preconditioning matrix
51640e4ef248SJed Brown -  flg - matrix structure flag
51650e4ef248SJed Brown 
51660e4ef248SJed Brown    Level: intermediate
51670e4ef248SJed Brown 
51680e4ef248SJed Brown    Notes:
51690e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
51700e4ef248SJed Brown 
51710e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
51720e4ef248SJed Brown @*/
5173d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
51740e4ef248SJed Brown {
51750e4ef248SJed Brown   PetscFunctionBegin;
51760e4ef248SJed Brown   PetscFunctionReturn(0);
51770e4ef248SJed Brown }
51780e4ef248SJed Brown 
51790026cea9SSean Farley /*@C
51800026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
51810026cea9SSean Farley 
51820026cea9SSean Farley    Collective on TS
51830026cea9SSean Farley 
51840026cea9SSean Farley    Input Arguments:
51850026cea9SSean Farley +  ts - time stepping context
51860026cea9SSean Farley .  t - time at which to evaluate
51870910c330SBarry Smith .  U - state at which to evaluate
51880910c330SBarry Smith .  Udot - time derivative of state vector
51890026cea9SSean Farley -  ctx - context
51900026cea9SSean Farley 
51910026cea9SSean Farley    Output Arguments:
51920026cea9SSean Farley .  F - left hand side
51930026cea9SSean Farley 
51940026cea9SSean Farley    Level: intermediate
51950026cea9SSean Farley 
51960026cea9SSean Farley    Notes:
51970910c330SBarry 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
51980026cea9SSean Farley    user is required to write their own TSComputeIFunction.
51990026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
52000026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
52010026cea9SSean Farley 
52029ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
52039ae8fd06SBarry Smith 
52049ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
52050026cea9SSean Farley @*/
52060910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
52070026cea9SSean Farley {
52080026cea9SSean Farley   PetscErrorCode ierr;
52090026cea9SSean Farley   Mat            A,B;
52100026cea9SSean Farley 
52110026cea9SSean Farley   PetscFunctionBegin;
52120298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5213d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
52140910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
52150026cea9SSean Farley   PetscFunctionReturn(0);
52160026cea9SSean Farley }
52170026cea9SSean Farley 
52180026cea9SSean Farley /*@C
5219b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
52200026cea9SSean Farley 
52210026cea9SSean Farley    Collective on TS
52220026cea9SSean Farley 
52230026cea9SSean Farley    Input Arguments:
52240026cea9SSean Farley +  ts - time stepping context
52250026cea9SSean Farley .  t - time at which to evaluate
52260910c330SBarry Smith .  U - state at which to evaluate
52270910c330SBarry Smith .  Udot - time derivative of state vector
52280026cea9SSean Farley .  shift - shift to apply
52290026cea9SSean Farley -  ctx - context
52300026cea9SSean Farley 
52310026cea9SSean Farley    Output Arguments:
52320026cea9SSean Farley +  A - pointer to operator
52330026cea9SSean Farley .  B - pointer to preconditioning matrix
52340026cea9SSean Farley -  flg - matrix structure flag
52350026cea9SSean Farley 
5236b41af12eSJed Brown    Level: advanced
52370026cea9SSean Farley 
52380026cea9SSean Farley    Notes:
52390026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
52400026cea9SSean Farley 
5241b41af12eSJed Brown    It is only appropriate for problems of the form
5242b41af12eSJed Brown 
5243b41af12eSJed Brown $     M Udot = F(U,t)
5244b41af12eSJed Brown 
5245b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5246b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5247b41af12eSJed Brown   an implicit operator of the form
5248b41af12eSJed Brown 
5249b41af12eSJed Brown $    shift*M + J
5250b41af12eSJed Brown 
5251b41af12eSJed 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
5252b41af12eSJed Brown   a copy of M or reassemble it when requested.
5253b41af12eSJed Brown 
52540026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
52550026cea9SSean Farley @*/
5256d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
52570026cea9SSean Farley {
5258b41af12eSJed Brown   PetscErrorCode ierr;
5259b41af12eSJed Brown 
52600026cea9SSean Farley   PetscFunctionBegin;
526194ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5262b41af12eSJed Brown   ts->ijacobian.shift = shift;
52630026cea9SSean Farley   PetscFunctionReturn(0);
52640026cea9SSean Farley }
5265b41af12eSJed Brown 
5266e817cc15SEmil Constantinescu /*@
5267e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5268e817cc15SEmil Constantinescu 
5269e817cc15SEmil Constantinescu    Not Collective
5270e817cc15SEmil Constantinescu 
5271e817cc15SEmil Constantinescu    Input Parameter:
5272e817cc15SEmil Constantinescu .  ts - the TS context
5273e817cc15SEmil Constantinescu 
5274e817cc15SEmil Constantinescu    Output Parameter:
52754e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5276e817cc15SEmil Constantinescu 
5277e817cc15SEmil Constantinescu    Level: beginner
5278e817cc15SEmil Constantinescu 
5279e817cc15SEmil Constantinescu .keywords: TS, equation type
5280e817cc15SEmil Constantinescu 
5281e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5282e817cc15SEmil Constantinescu @*/
5283e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5284e817cc15SEmil Constantinescu {
5285e817cc15SEmil Constantinescu   PetscFunctionBegin;
5286e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5287e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5288e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5289e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5290e817cc15SEmil Constantinescu }
5291e817cc15SEmil Constantinescu 
5292e817cc15SEmil Constantinescu /*@
5293e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5294e817cc15SEmil Constantinescu 
5295e817cc15SEmil Constantinescu    Not Collective
5296e817cc15SEmil Constantinescu 
5297e817cc15SEmil Constantinescu    Input Parameter:
5298e817cc15SEmil Constantinescu +  ts - the TS context
52991297b224SEmil Constantinescu -  equation_type - see TSEquationType
5300e817cc15SEmil Constantinescu 
5301e817cc15SEmil Constantinescu    Level: advanced
5302e817cc15SEmil Constantinescu 
5303e817cc15SEmil Constantinescu .keywords: TS, equation type
5304e817cc15SEmil Constantinescu 
5305e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5306e817cc15SEmil Constantinescu @*/
5307e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5308e817cc15SEmil Constantinescu {
5309e817cc15SEmil Constantinescu   PetscFunctionBegin;
5310e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5311e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5312e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5313e817cc15SEmil Constantinescu }
53140026cea9SSean Farley 
53154af1b03aSJed Brown /*@
53164af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
53174af1b03aSJed Brown 
53184af1b03aSJed Brown    Not Collective
53194af1b03aSJed Brown 
53204af1b03aSJed Brown    Input Parameter:
53214af1b03aSJed Brown .  ts - the TS context
53224af1b03aSJed Brown 
53234af1b03aSJed Brown    Output Parameter:
53244af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
53254af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
53264af1b03aSJed Brown 
5327487e0bb9SJed Brown    Level: beginner
53284af1b03aSJed Brown 
5329cd652676SJed Brown    Notes:
5330cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
53314af1b03aSJed Brown 
53324af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
53334af1b03aSJed Brown 
53344af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
53354af1b03aSJed Brown @*/
53364af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
53374af1b03aSJed Brown {
53384af1b03aSJed Brown   PetscFunctionBegin;
53394af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53404af1b03aSJed Brown   PetscValidPointer(reason,2);
53414af1b03aSJed Brown   *reason = ts->reason;
53424af1b03aSJed Brown   PetscFunctionReturn(0);
53434af1b03aSJed Brown }
53444af1b03aSJed Brown 
5345d6ad946cSShri Abhyankar /*@
5346d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5347d6ad946cSShri Abhyankar 
5348d6ad946cSShri Abhyankar    Not Collective
5349d6ad946cSShri Abhyankar 
5350d6ad946cSShri Abhyankar    Input Parameter:
5351d6ad946cSShri Abhyankar +  ts - the TS context
5352d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5353d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5354d6ad946cSShri Abhyankar 
5355f5abba47SShri Abhyankar    Level: advanced
5356d6ad946cSShri Abhyankar 
5357d6ad946cSShri Abhyankar    Notes:
5358d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
5359d6ad946cSShri Abhyankar 
5360d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
5361d6ad946cSShri Abhyankar 
5362d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5363d6ad946cSShri Abhyankar @*/
5364d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5365d6ad946cSShri Abhyankar {
5366d6ad946cSShri Abhyankar   PetscFunctionBegin;
5367d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5368d6ad946cSShri Abhyankar   ts->reason = reason;
5369d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5370d6ad946cSShri Abhyankar }
5371d6ad946cSShri Abhyankar 
5372cc708dedSBarry Smith /*@
5373cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5374cc708dedSBarry Smith 
5375cc708dedSBarry Smith    Not Collective
5376cc708dedSBarry Smith 
5377cc708dedSBarry Smith    Input Parameter:
5378cc708dedSBarry Smith .  ts - the TS context
5379cc708dedSBarry Smith 
5380cc708dedSBarry Smith    Output Parameter:
538163e21af5SBarry Smith .  ftime - the final time. This time corresponds to the final time set with TSSetDuration()
5382cc708dedSBarry Smith 
5383487e0bb9SJed Brown    Level: beginner
5384cc708dedSBarry Smith 
5385cc708dedSBarry Smith    Notes:
5386cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5387cc708dedSBarry Smith 
5388cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
5389cc708dedSBarry Smith 
5390cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5391cc708dedSBarry Smith @*/
5392cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5393cc708dedSBarry Smith {
5394cc708dedSBarry Smith   PetscFunctionBegin;
5395cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5396cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5397cc708dedSBarry Smith   *ftime = ts->solvetime;
5398cc708dedSBarry Smith   PetscFunctionReturn(0);
5399cc708dedSBarry Smith }
5400cc708dedSBarry Smith 
54012c18e0fdSBarry Smith /*@
54022c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
54032c18e0fdSBarry Smith 
54042c18e0fdSBarry Smith    Not Collective
54052c18e0fdSBarry Smith 
54062c18e0fdSBarry Smith    Input Parameter:
54072c18e0fdSBarry Smith .  ts - the TS context
54082c18e0fdSBarry Smith 
54092c18e0fdSBarry Smith    Output Parameter:
54102c18e0fdSBarry Smith .  steps - the number of steps
54112c18e0fdSBarry Smith 
54122c18e0fdSBarry Smith    Level: beginner
54132c18e0fdSBarry Smith 
54142c18e0fdSBarry Smith    Notes:
54152c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
54162c18e0fdSBarry Smith 
54172c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
54182c18e0fdSBarry Smith 
54192c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
54202c18e0fdSBarry Smith @*/
54212c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
54222c18e0fdSBarry Smith {
54232c18e0fdSBarry Smith   PetscFunctionBegin;
54242c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54252c18e0fdSBarry Smith   PetscValidPointer(steps,2);
54262c18e0fdSBarry Smith   *steps = ts->total_steps;
54272c18e0fdSBarry Smith   PetscFunctionReturn(0);
54282c18e0fdSBarry Smith }
54292c18e0fdSBarry Smith 
54309f67acb7SJed Brown /*@
54315ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
54329f67acb7SJed Brown    used by the time integrator.
54339f67acb7SJed Brown 
54349f67acb7SJed Brown    Not Collective
54359f67acb7SJed Brown 
54369f67acb7SJed Brown    Input Parameter:
54379f67acb7SJed Brown .  ts - TS context
54389f67acb7SJed Brown 
54399f67acb7SJed Brown    Output Parameter:
54409f67acb7SJed Brown .  nits - number of nonlinear iterations
54419f67acb7SJed Brown 
54429f67acb7SJed Brown    Notes:
54439f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
54449f67acb7SJed Brown 
54459f67acb7SJed Brown    Level: intermediate
54469f67acb7SJed Brown 
54479f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
54489f67acb7SJed Brown 
54495ef26d82SJed Brown .seealso:  TSGetKSPIterations()
54509f67acb7SJed Brown @*/
54515ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
54529f67acb7SJed Brown {
54539f67acb7SJed Brown   PetscFunctionBegin;
54549f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54559f67acb7SJed Brown   PetscValidIntPointer(nits,2);
54565ef26d82SJed Brown   *nits = ts->snes_its;
54579f67acb7SJed Brown   PetscFunctionReturn(0);
54589f67acb7SJed Brown }
54599f67acb7SJed Brown 
54609f67acb7SJed Brown /*@
54615ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
54629f67acb7SJed Brown    used by the time integrator.
54639f67acb7SJed Brown 
54649f67acb7SJed Brown    Not Collective
54659f67acb7SJed Brown 
54669f67acb7SJed Brown    Input Parameter:
54679f67acb7SJed Brown .  ts - TS context
54689f67acb7SJed Brown 
54699f67acb7SJed Brown    Output Parameter:
54709f67acb7SJed Brown .  lits - number of linear iterations
54719f67acb7SJed Brown 
54729f67acb7SJed Brown    Notes:
54739f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
54749f67acb7SJed Brown 
54759f67acb7SJed Brown    Level: intermediate
54769f67acb7SJed Brown 
54779f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
54789f67acb7SJed Brown 
54795ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
54809f67acb7SJed Brown @*/
54815ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
54829f67acb7SJed Brown {
54839f67acb7SJed Brown   PetscFunctionBegin;
54849f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54859f67acb7SJed Brown   PetscValidIntPointer(lits,2);
54865ef26d82SJed Brown   *lits = ts->ksp_its;
54879f67acb7SJed Brown   PetscFunctionReturn(0);
54889f67acb7SJed Brown }
54899f67acb7SJed Brown 
5490cef5090cSJed Brown /*@
5491cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5492cef5090cSJed Brown 
5493cef5090cSJed Brown    Not Collective
5494cef5090cSJed Brown 
5495cef5090cSJed Brown    Input Parameter:
5496cef5090cSJed Brown .  ts - TS context
5497cef5090cSJed Brown 
5498cef5090cSJed Brown    Output Parameter:
5499cef5090cSJed Brown .  rejects - number of steps rejected
5500cef5090cSJed Brown 
5501cef5090cSJed Brown    Notes:
5502cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5503cef5090cSJed Brown 
5504cef5090cSJed Brown    Level: intermediate
5505cef5090cSJed Brown 
5506cef5090cSJed Brown .keywords: TS, get, number
5507cef5090cSJed Brown 
55085ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5509cef5090cSJed Brown @*/
5510cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5511cef5090cSJed Brown {
5512cef5090cSJed Brown   PetscFunctionBegin;
5513cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5514cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5515cef5090cSJed Brown   *rejects = ts->reject;
5516cef5090cSJed Brown   PetscFunctionReturn(0);
5517cef5090cSJed Brown }
5518cef5090cSJed Brown 
5519cef5090cSJed Brown /*@
5520cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5521cef5090cSJed Brown 
5522cef5090cSJed Brown    Not Collective
5523cef5090cSJed Brown 
5524cef5090cSJed Brown    Input Parameter:
5525cef5090cSJed Brown .  ts - TS context
5526cef5090cSJed Brown 
5527cef5090cSJed Brown    Output Parameter:
5528cef5090cSJed Brown .  fails - number of failed nonlinear solves
5529cef5090cSJed Brown 
5530cef5090cSJed Brown    Notes:
5531cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5532cef5090cSJed Brown 
5533cef5090cSJed Brown    Level: intermediate
5534cef5090cSJed Brown 
5535cef5090cSJed Brown .keywords: TS, get, number
5536cef5090cSJed Brown 
55375ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5538cef5090cSJed Brown @*/
5539cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5540cef5090cSJed Brown {
5541cef5090cSJed Brown   PetscFunctionBegin;
5542cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5543cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5544cef5090cSJed Brown   *fails = ts->num_snes_failures;
5545cef5090cSJed Brown   PetscFunctionReturn(0);
5546cef5090cSJed Brown }
5547cef5090cSJed Brown 
5548cef5090cSJed Brown /*@
5549cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5550cef5090cSJed Brown 
5551cef5090cSJed Brown    Not Collective
5552cef5090cSJed Brown 
5553cef5090cSJed Brown    Input Parameter:
5554cef5090cSJed Brown +  ts - TS context
5555cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5556cef5090cSJed Brown 
5557cef5090cSJed Brown    Notes:
5558cef5090cSJed Brown    The counter is reset to zero for each step
5559cef5090cSJed Brown 
5560cef5090cSJed Brown    Options Database Key:
5561cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5562cef5090cSJed Brown 
5563cef5090cSJed Brown    Level: intermediate
5564cef5090cSJed Brown 
5565cef5090cSJed Brown .keywords: TS, set, maximum, number
5566cef5090cSJed Brown 
55675ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5568cef5090cSJed Brown @*/
5569cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5570cef5090cSJed Brown {
5571cef5090cSJed Brown   PetscFunctionBegin;
5572cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5573cef5090cSJed Brown   ts->max_reject = rejects;
5574cef5090cSJed Brown   PetscFunctionReturn(0);
5575cef5090cSJed Brown }
5576cef5090cSJed Brown 
5577cef5090cSJed Brown /*@
5578cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5579cef5090cSJed Brown 
5580cef5090cSJed Brown    Not Collective
5581cef5090cSJed Brown 
5582cef5090cSJed Brown    Input Parameter:
5583cef5090cSJed Brown +  ts - TS context
5584cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5585cef5090cSJed Brown 
5586cef5090cSJed Brown    Notes:
5587cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5588cef5090cSJed Brown 
5589cef5090cSJed Brown    Options Database Key:
5590cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5591cef5090cSJed Brown 
5592cef5090cSJed Brown    Level: intermediate
5593cef5090cSJed Brown 
5594cef5090cSJed Brown .keywords: TS, set, maximum, number
5595cef5090cSJed Brown 
55965ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5597cef5090cSJed Brown @*/
5598cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5599cef5090cSJed Brown {
5600cef5090cSJed Brown   PetscFunctionBegin;
5601cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5602cef5090cSJed Brown   ts->max_snes_failures = fails;
5603cef5090cSJed Brown   PetscFunctionReturn(0);
5604cef5090cSJed Brown }
5605cef5090cSJed Brown 
5606cef5090cSJed Brown /*@
5607cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5608cef5090cSJed Brown 
5609cef5090cSJed Brown    Not Collective
5610cef5090cSJed Brown 
5611cef5090cSJed Brown    Input Parameter:
5612cef5090cSJed Brown +  ts - TS context
5613cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5614cef5090cSJed Brown 
5615cef5090cSJed Brown    Options Database Key:
5616cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5617cef5090cSJed Brown 
5618cef5090cSJed Brown    Level: intermediate
5619cef5090cSJed Brown 
5620cef5090cSJed Brown .keywords: TS, set, error
5621cef5090cSJed Brown 
56225ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5623cef5090cSJed Brown @*/
5624cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5625cef5090cSJed Brown {
5626cef5090cSJed Brown   PetscFunctionBegin;
5627cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5628cef5090cSJed Brown   ts->errorifstepfailed = err;
5629cef5090cSJed Brown   PetscFunctionReturn(0);
5630cef5090cSJed Brown }
5631cef5090cSJed Brown 
5632fb1732b5SBarry Smith /*@C
5633fde5950dSBarry 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
5634fb1732b5SBarry Smith 
5635fb1732b5SBarry Smith    Collective on TS
5636fb1732b5SBarry Smith 
5637fb1732b5SBarry Smith    Input Parameters:
5638fb1732b5SBarry Smith +  ts - the TS context
5639fb1732b5SBarry Smith .  step - current time-step
5640fb1732b5SBarry Smith .  ptime - current time
56410910c330SBarry Smith .  u - current state
5642721cd6eeSBarry Smith -  vf - viewer and its format
5643fb1732b5SBarry Smith 
5644fb1732b5SBarry Smith    Level: intermediate
5645fb1732b5SBarry Smith 
5646fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5647fb1732b5SBarry Smith 
5648fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5649fb1732b5SBarry Smith @*/
5650721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5651fb1732b5SBarry Smith {
5652fb1732b5SBarry Smith   PetscErrorCode ierr;
5653fb1732b5SBarry Smith 
5654fb1732b5SBarry Smith   PetscFunctionBegin;
5655721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5656721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5657721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5658ed81e22dSJed Brown   PetscFunctionReturn(0);
5659ed81e22dSJed Brown }
5660ed81e22dSJed Brown 
5661ed81e22dSJed Brown /*@C
5662ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5663ed81e22dSJed Brown 
5664ed81e22dSJed Brown    Collective on TS
5665ed81e22dSJed Brown 
5666ed81e22dSJed Brown    Input Parameters:
5667ed81e22dSJed Brown +  ts - the TS context
5668ed81e22dSJed Brown .  step - current time-step
5669ed81e22dSJed Brown .  ptime - current time
56700910c330SBarry Smith .  u - current state
5671ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5672ed81e22dSJed Brown 
5673ed81e22dSJed Brown    Level: intermediate
5674ed81e22dSJed Brown 
5675ed81e22dSJed Brown    Notes:
5676ed81e22dSJed 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.
5677ed81e22dSJed Brown    These are named according to the file name template.
5678ed81e22dSJed Brown 
5679ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5680ed81e22dSJed Brown 
5681ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5682ed81e22dSJed Brown 
5683ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5684ed81e22dSJed Brown @*/
56850910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5686ed81e22dSJed Brown {
5687ed81e22dSJed Brown   PetscErrorCode ierr;
5688ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5689ed81e22dSJed Brown   PetscViewer    viewer;
5690ed81e22dSJed Brown 
5691ed81e22dSJed Brown   PetscFunctionBegin;
569263e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
56938caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5694ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
56950910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5696ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5697ed81e22dSJed Brown   PetscFunctionReturn(0);
5698ed81e22dSJed Brown }
5699ed81e22dSJed Brown 
5700ed81e22dSJed Brown /*@C
5701ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5702ed81e22dSJed Brown 
5703ed81e22dSJed Brown    Collective on TS
5704ed81e22dSJed Brown 
5705ed81e22dSJed Brown    Input Parameters:
5706ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5707ed81e22dSJed Brown 
5708ed81e22dSJed Brown    Level: intermediate
5709ed81e22dSJed Brown 
5710ed81e22dSJed Brown    Note:
5711ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5712ed81e22dSJed Brown 
5713ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5714ed81e22dSJed Brown 
5715ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5716ed81e22dSJed Brown @*/
5717ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5718ed81e22dSJed Brown {
5719ed81e22dSJed Brown   PetscErrorCode ierr;
5720ed81e22dSJed Brown 
5721ed81e22dSJed Brown   PetscFunctionBegin;
5722ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5723fb1732b5SBarry Smith   PetscFunctionReturn(0);
5724fb1732b5SBarry Smith }
5725fb1732b5SBarry Smith 
572684df9cb4SJed Brown /*@
5727552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
572884df9cb4SJed Brown 
5729ed81e22dSJed Brown    Collective on TS if controller has not been created yet
573084df9cb4SJed Brown 
573184df9cb4SJed Brown    Input Arguments:
5732ed81e22dSJed Brown .  ts - time stepping context
573384df9cb4SJed Brown 
573484df9cb4SJed Brown    Output Arguments:
5735ed81e22dSJed Brown .  adapt - adaptive controller
573684df9cb4SJed Brown 
573784df9cb4SJed Brown    Level: intermediate
573884df9cb4SJed Brown 
5739ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
574084df9cb4SJed Brown @*/
5741552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
574284df9cb4SJed Brown {
574384df9cb4SJed Brown   PetscErrorCode ierr;
574484df9cb4SJed Brown 
574584df9cb4SJed Brown   PetscFunctionBegin;
574684df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5747bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
574884df9cb4SJed Brown   if (!ts->adapt) {
5749ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
57503bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
57511c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
575284df9cb4SJed Brown   }
5753bec58848SLisandro Dalcin   *adapt = ts->adapt;
575484df9cb4SJed Brown   PetscFunctionReturn(0);
575584df9cb4SJed Brown }
5756d6ebe24aSShri Abhyankar 
57571c3436cfSJed Brown /*@
57581c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
57591c3436cfSJed Brown 
57601c3436cfSJed Brown    Logically Collective
57611c3436cfSJed Brown 
57621c3436cfSJed Brown    Input Arguments:
57631c3436cfSJed Brown +  ts - time integration context
57641c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
57650298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
57661c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
57670298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
57681c3436cfSJed Brown 
5769a3cdaa26SBarry Smith    Options Database keys:
5770a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5771a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5772a3cdaa26SBarry Smith 
57733ff766beSShri Abhyankar    Notes:
57743ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
57753ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
57763ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
57773ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
57783ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
57793ff766beSShri Abhyankar    differential variables.
57803ff766beSShri Abhyankar 
57811c3436cfSJed Brown    Level: beginner
57821c3436cfSJed Brown 
5783c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
57841c3436cfSJed Brown @*/
57851c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
57861c3436cfSJed Brown {
57871c3436cfSJed Brown   PetscErrorCode ierr;
57881c3436cfSJed Brown 
57891c3436cfSJed Brown   PetscFunctionBegin;
5790c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
57911c3436cfSJed Brown   if (vatol) {
57921c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
57931c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
57941c3436cfSJed Brown     ts->vatol = vatol;
57951c3436cfSJed Brown   }
5796c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
57971c3436cfSJed Brown   if (vrtol) {
57981c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
57991c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
58001c3436cfSJed Brown     ts->vrtol = vrtol;
58011c3436cfSJed Brown   }
58021c3436cfSJed Brown   PetscFunctionReturn(0);
58031c3436cfSJed Brown }
58041c3436cfSJed Brown 
5805c5033834SJed Brown /*@
5806c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5807c5033834SJed Brown 
5808c5033834SJed Brown    Logically Collective
5809c5033834SJed Brown 
5810c5033834SJed Brown    Input Arguments:
5811c5033834SJed Brown .  ts - time integration context
5812c5033834SJed Brown 
5813c5033834SJed Brown    Output Arguments:
58140298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
58150298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
58160298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
58170298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5818c5033834SJed Brown 
5819c5033834SJed Brown    Level: beginner
5820c5033834SJed Brown 
5821c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5822c5033834SJed Brown @*/
5823c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5824c5033834SJed Brown {
5825c5033834SJed Brown   PetscFunctionBegin;
5826c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5827c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5828c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5829c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5830c5033834SJed Brown   PetscFunctionReturn(0);
5831c5033834SJed Brown }
5832c5033834SJed Brown 
58339c6b16b5SShri Abhyankar /*@
5834a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
58359c6b16b5SShri Abhyankar 
58369c6b16b5SShri Abhyankar    Collective on TS
58379c6b16b5SShri Abhyankar 
58389c6b16b5SShri Abhyankar    Input Arguments:
58399c6b16b5SShri Abhyankar +  ts - time stepping context
5840a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5841a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
58429c6b16b5SShri Abhyankar 
58439c6b16b5SShri Abhyankar    Output Arguments:
58447453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
58457453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
58467453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
58479c6b16b5SShri Abhyankar 
58489c6b16b5SShri Abhyankar    Level: developer
58499c6b16b5SShri Abhyankar 
5850deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
58519c6b16b5SShri Abhyankar @*/
58527453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58539c6b16b5SShri Abhyankar {
58549c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
58559c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
58567453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
58577453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
58589c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
58597453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
58607453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
58617453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
58629c6b16b5SShri Abhyankar 
58639c6b16b5SShri Abhyankar   PetscFunctionBegin;
58649c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5865a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5866a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5867a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5868a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5869a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5870a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
58718a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
58728a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5873a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
58749c6b16b5SShri Abhyankar 
58759c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
58769c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
58779c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
58789c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
58799c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
58807453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
58817453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
58827453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
58839c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
58849c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
58859c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58869c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58879c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
58887453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58897453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
58907453f775SEmil Constantinescu       if(tola>0.){
58917453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58927453f775SEmil Constantinescu         na_loc++;
58937453f775SEmil Constantinescu       }
58947453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58957453f775SEmil Constantinescu       if(tolr>0.){
58967453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58977453f775SEmil Constantinescu         nr_loc++;
58987453f775SEmil Constantinescu       }
58997453f775SEmil Constantinescu       tol=tola+tolr;
59007453f775SEmil Constantinescu       if(tol>0.){
59017453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
59027453f775SEmil Constantinescu         n_loc++;
59037453f775SEmil Constantinescu       }
59049c6b16b5SShri Abhyankar     }
59059c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59069c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59079c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59089c6b16b5SShri Abhyankar     const PetscScalar *atol;
59099c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59109c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
59117453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59127453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59137453f775SEmil Constantinescu       if(tola>0.){
59147453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
59157453f775SEmil Constantinescu         na_loc++;
59167453f775SEmil Constantinescu       }
59177453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59187453f775SEmil Constantinescu       if(tolr>0.){
59197453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
59207453f775SEmil Constantinescu         nr_loc++;
59217453f775SEmil Constantinescu       }
59227453f775SEmil Constantinescu       tol=tola+tolr;
59237453f775SEmil Constantinescu       if(tol>0.){
59247453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
59257453f775SEmil Constantinescu         n_loc++;
59267453f775SEmil Constantinescu       }
59279c6b16b5SShri Abhyankar     }
59289c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59299c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59309c6b16b5SShri Abhyankar     const PetscScalar *rtol;
59319c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59329c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
59337453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59347453f775SEmil Constantinescu       tola = ts->atol;
59357453f775SEmil Constantinescu       if(tola>0.){
59367453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
59377453f775SEmil Constantinescu         na_loc++;
59387453f775SEmil Constantinescu       }
59397453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59407453f775SEmil Constantinescu       if(tolr>0.){
59417453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
59427453f775SEmil Constantinescu         nr_loc++;
59437453f775SEmil Constantinescu       }
59447453f775SEmil Constantinescu       tol=tola+tolr;
59457453f775SEmil Constantinescu       if(tol>0.){
59467453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
59477453f775SEmil Constantinescu         n_loc++;
59487453f775SEmil Constantinescu       }
59499c6b16b5SShri Abhyankar     }
59509c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59519c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
59529c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
59537453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59547453f775SEmil Constantinescu      tola = ts->atol;
59557453f775SEmil Constantinescu       if(tola>0.){
59567453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
59577453f775SEmil Constantinescu         na_loc++;
59587453f775SEmil Constantinescu       }
59597453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59607453f775SEmil Constantinescu       if(tolr>0.){
59617453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
59627453f775SEmil Constantinescu         nr_loc++;
59637453f775SEmil Constantinescu       }
59647453f775SEmil Constantinescu       tol=tola+tolr;
59657453f775SEmil Constantinescu       if(tol>0.){
59667453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
59677453f775SEmil Constantinescu         n_loc++;
59687453f775SEmil Constantinescu       }
59699c6b16b5SShri Abhyankar     }
59709c6b16b5SShri Abhyankar   }
59719c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
59729c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
59739c6b16b5SShri Abhyankar 
59747453f775SEmil Constantinescu   err_loc[0] = sum;
59757453f775SEmil Constantinescu   err_loc[1] = suma;
59767453f775SEmil Constantinescu   err_loc[2] = sumr;
59777453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
59787453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
59797453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
59807453f775SEmil Constantinescu 
5981a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
59827453f775SEmil Constantinescu 
59837453f775SEmil Constantinescu   gsum   = err_glb[0];
59847453f775SEmil Constantinescu   gsuma  = err_glb[1];
59857453f775SEmil Constantinescu   gsumr  = err_glb[2];
59867453f775SEmil Constantinescu   n_glb  = err_glb[3];
59877453f775SEmil Constantinescu   na_glb = err_glb[4];
59887453f775SEmil Constantinescu   nr_glb = err_glb[5];
59897453f775SEmil Constantinescu 
5990b1316ef9SEmil Constantinescu   *norm  = 0.;
5991b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5992b1316ef9SEmil Constantinescu   *norma = 0.;
5993b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5994b1316ef9SEmil Constantinescu   *normr = 0.;
5995b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
59969c6b16b5SShri Abhyankar 
59979c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
59987453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
59997453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60009c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
60019c6b16b5SShri Abhyankar }
60029c6b16b5SShri Abhyankar 
60039c6b16b5SShri Abhyankar /*@
6004a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
60059c6b16b5SShri Abhyankar 
60069c6b16b5SShri Abhyankar    Collective on TS
60079c6b16b5SShri Abhyankar 
60089c6b16b5SShri Abhyankar    Input Arguments:
60099c6b16b5SShri Abhyankar +  ts - time stepping context
6010a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6011a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
60129c6b16b5SShri Abhyankar 
60139c6b16b5SShri Abhyankar    Output Arguments:
60147453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
60157453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
60167453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
60179c6b16b5SShri Abhyankar 
60189c6b16b5SShri Abhyankar    Level: developer
60199c6b16b5SShri Abhyankar 
6020deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
60219c6b16b5SShri Abhyankar @*/
60227453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60239c6b16b5SShri Abhyankar {
60249c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
60257453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
60269c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
60277453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
60287453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
60297453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
60309c6b16b5SShri Abhyankar 
60319c6b16b5SShri Abhyankar   PetscFunctionBegin;
60329c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6033a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
6034a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
6035a4868fbcSLisandro Dalcin   PetscValidType(U,2);
6036a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
6037a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
6038a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
60398a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
60408a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
6041a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
60429c6b16b5SShri Abhyankar 
60439c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
60449c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
60459c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
60469c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
60479c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
60487453f775SEmil Constantinescu 
60497453f775SEmil Constantinescu   max=0.;
60507453f775SEmil Constantinescu   maxa=0.;
60517453f775SEmil Constantinescu   maxr=0.;
60527453f775SEmil Constantinescu 
60537453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
60549c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
60559c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60569c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60577453f775SEmil Constantinescu 
60587453f775SEmil Constantinescu     for (i=0; i<n; i++) {
60597453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60607453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
60617453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60627453f775SEmil Constantinescu       tol  = tola+tolr;
60637453f775SEmil Constantinescu       if(tola>0.){
60647453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60657453f775SEmil Constantinescu       }
60667453f775SEmil Constantinescu       if(tolr>0.){
60677453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60687453f775SEmil Constantinescu       }
60697453f775SEmil Constantinescu       if(tol>0.){
60707453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60717453f775SEmil Constantinescu       }
60729c6b16b5SShri Abhyankar     }
60739c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60749c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60759c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
60769c6b16b5SShri Abhyankar     const PetscScalar *atol;
60779c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60787453f775SEmil Constantinescu     for (i=0; i<n; i++) {
60797453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60807453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
60817453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60827453f775SEmil Constantinescu       tol  = tola+tolr;
60837453f775SEmil Constantinescu       if(tola>0.){
60847453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60857453f775SEmil Constantinescu       }
60867453f775SEmil Constantinescu       if(tolr>0.){
60877453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60887453f775SEmil Constantinescu       }
60897453f775SEmil Constantinescu       if(tol>0.){
60907453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60917453f775SEmil Constantinescu       }
60929c6b16b5SShri Abhyankar     }
60939c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60949c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
60959c6b16b5SShri Abhyankar     const PetscScalar *rtol;
60969c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60977453f775SEmil Constantinescu 
60987453f775SEmil Constantinescu     for (i=0; i<n; i++) {
60997453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
61007453f775SEmil Constantinescu       tola = ts->atol;
61017453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61027453f775SEmil Constantinescu       tol  = tola+tolr;
61037453f775SEmil Constantinescu       if(tola>0.){
61047453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
61057453f775SEmil Constantinescu       }
61067453f775SEmil Constantinescu       if(tolr>0.){
61077453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
61087453f775SEmil Constantinescu       }
61097453f775SEmil Constantinescu       if(tol>0.){
61107453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
61117453f775SEmil Constantinescu       }
61129c6b16b5SShri Abhyankar     }
61139c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61149c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
61157453f775SEmil Constantinescu 
61167453f775SEmil Constantinescu     for (i=0; i<n; i++) {
61177453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
61187453f775SEmil Constantinescu       tola = ts->atol;
61197453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61207453f775SEmil Constantinescu       tol  = tola+tolr;
61217453f775SEmil Constantinescu       if(tola>0.){
61227453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
61237453f775SEmil Constantinescu       }
61247453f775SEmil Constantinescu       if(tolr>0.){
61257453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
61267453f775SEmil Constantinescu       }
61277453f775SEmil Constantinescu       if(tol>0.){
61287453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
61297453f775SEmil Constantinescu       }
61309c6b16b5SShri Abhyankar     }
61319c6b16b5SShri Abhyankar   }
61329c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
61339c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
61347453f775SEmil Constantinescu   err_loc[0] = max;
61357453f775SEmil Constantinescu   err_loc[1] = maxa;
61367453f775SEmil Constantinescu   err_loc[2] = maxr;
6137a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61387453f775SEmil Constantinescu   gmax   = err_glb[0];
61397453f775SEmil Constantinescu   gmaxa  = err_glb[1];
61407453f775SEmil Constantinescu   gmaxr  = err_glb[2];
61419c6b16b5SShri Abhyankar 
61429c6b16b5SShri Abhyankar   *norm = gmax;
61437453f775SEmil Constantinescu   *norma = gmaxa;
61447453f775SEmil Constantinescu   *normr = gmaxr;
61459c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
61467453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
61477453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
61489c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
61499c6b16b5SShri Abhyankar }
61509c6b16b5SShri Abhyankar 
61511c3436cfSJed Brown /*@
61528a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
61531c3436cfSJed Brown 
61541c3436cfSJed Brown    Collective on TS
61551c3436cfSJed Brown 
61561c3436cfSJed Brown    Input Arguments:
61571c3436cfSJed Brown +  ts - time stepping context
6158a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6159a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6160a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
61617619abb3SShri 
61621c3436cfSJed Brown    Output Arguments:
61638a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
61648a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
61658a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6166a4868fbcSLisandro Dalcin 
6167a4868fbcSLisandro Dalcin    Options Database Keys:
6168a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6169a4868fbcSLisandro Dalcin 
61701c3436cfSJed Brown    Level: developer
61711c3436cfSJed Brown 
61728a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
61731c3436cfSJed Brown @*/
61747453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61751c3436cfSJed Brown {
61768beabaa1SBarry Smith   PetscErrorCode ierr;
61771c3436cfSJed Brown 
61781c3436cfSJed Brown   PetscFunctionBegin;
6179a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
61807453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6181a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
61827453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6183a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
61841c3436cfSJed Brown   PetscFunctionReturn(0);
61851c3436cfSJed Brown }
61861c3436cfSJed Brown 
61878a175baeSEmil Constantinescu 
61888a175baeSEmil Constantinescu /*@
61898a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
61908a175baeSEmil Constantinescu 
61918a175baeSEmil Constantinescu    Collective on TS
61928a175baeSEmil Constantinescu 
61938a175baeSEmil Constantinescu    Input Arguments:
61948a175baeSEmil Constantinescu +  ts - time stepping context
61958a175baeSEmil Constantinescu .  E - error vector
61968a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
61978a175baeSEmil Constantinescu -  Y - state vector, previous time step
61988a175baeSEmil Constantinescu 
61998a175baeSEmil Constantinescu    Output Arguments:
62008a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
62018a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
62028a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
62038a175baeSEmil Constantinescu 
62048a175baeSEmil Constantinescu    Level: developer
62058a175baeSEmil Constantinescu 
62068a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
62078a175baeSEmil Constantinescu @*/
62088a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
62098a175baeSEmil Constantinescu {
62108a175baeSEmil Constantinescu   PetscErrorCode    ierr;
62118a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
62128a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
62138a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
62148a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
62158a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
62168a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
62178a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
62188a175baeSEmil Constantinescu 
62198a175baeSEmil Constantinescu   PetscFunctionBegin;
62208a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62218a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
62228a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
62238a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
62248a175baeSEmil Constantinescu   PetscValidType(E,2);
62258a175baeSEmil Constantinescu   PetscValidType(U,3);
62268a175baeSEmil Constantinescu   PetscValidType(Y,4);
62278a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
62288a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
62298a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
62308a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
62318a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
62328a175baeSEmil Constantinescu 
62338a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
62348a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
62358a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
62368a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
62378a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
62388a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
62398a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
62408a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
62418a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
62428a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
62438a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
62448a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62458a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62468a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
62478a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62488a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
62498a175baeSEmil Constantinescu       if(tola>0.){
62508a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62518a175baeSEmil Constantinescu         na_loc++;
62528a175baeSEmil Constantinescu       }
62538a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62548a175baeSEmil Constantinescu       if(tolr>0.){
62558a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62568a175baeSEmil Constantinescu         nr_loc++;
62578a175baeSEmil Constantinescu       }
62588a175baeSEmil Constantinescu       tol=tola+tolr;
62598a175baeSEmil Constantinescu       if(tol>0.){
62608a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62618a175baeSEmil Constantinescu         n_loc++;
62628a175baeSEmil Constantinescu       }
62638a175baeSEmil Constantinescu     }
62648a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62658a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62668a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
62678a175baeSEmil Constantinescu     const PetscScalar *atol;
62688a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62698a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
62708a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62718a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
62728a175baeSEmil Constantinescu       if(tola>0.){
62738a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62748a175baeSEmil Constantinescu         na_loc++;
62758a175baeSEmil Constantinescu       }
62768a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62778a175baeSEmil Constantinescu       if(tolr>0.){
62788a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62798a175baeSEmil Constantinescu         nr_loc++;
62808a175baeSEmil Constantinescu       }
62818a175baeSEmil Constantinescu       tol=tola+tolr;
62828a175baeSEmil Constantinescu       if(tol>0.){
62838a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62848a175baeSEmil Constantinescu         n_loc++;
62858a175baeSEmil Constantinescu       }
62868a175baeSEmil Constantinescu     }
62878a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62888a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
62898a175baeSEmil Constantinescu     const PetscScalar *rtol;
62908a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62918a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
62928a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62938a175baeSEmil Constantinescu       tola = ts->atol;
62948a175baeSEmil Constantinescu       if(tola>0.){
62958a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62968a175baeSEmil Constantinescu         na_loc++;
62978a175baeSEmil Constantinescu       }
62988a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62998a175baeSEmil Constantinescu       if(tolr>0.){
63008a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
63018a175baeSEmil Constantinescu         nr_loc++;
63028a175baeSEmil Constantinescu       }
63038a175baeSEmil Constantinescu       tol=tola+tolr;
63048a175baeSEmil Constantinescu       if(tol>0.){
63058a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
63068a175baeSEmil Constantinescu         n_loc++;
63078a175baeSEmil Constantinescu       }
63088a175baeSEmil Constantinescu     }
63098a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63108a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
63118a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
63128a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63138a175baeSEmil Constantinescu      tola = ts->atol;
63148a175baeSEmil Constantinescu       if(tola>0.){
63158a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
63168a175baeSEmil Constantinescu         na_loc++;
63178a175baeSEmil Constantinescu       }
63188a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63198a175baeSEmil Constantinescu       if(tolr>0.){
63208a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
63218a175baeSEmil Constantinescu         nr_loc++;
63228a175baeSEmil Constantinescu       }
63238a175baeSEmil Constantinescu       tol=tola+tolr;
63248a175baeSEmil Constantinescu       if(tol>0.){
63258a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
63268a175baeSEmil Constantinescu         n_loc++;
63278a175baeSEmil Constantinescu       }
63288a175baeSEmil Constantinescu     }
63298a175baeSEmil Constantinescu   }
63308a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
63318a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
63328a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
63338a175baeSEmil Constantinescu 
63348a175baeSEmil Constantinescu   err_loc[0] = sum;
63358a175baeSEmil Constantinescu   err_loc[1] = suma;
63368a175baeSEmil Constantinescu   err_loc[2] = sumr;
63378a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
63388a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
63398a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
63408a175baeSEmil Constantinescu 
6341a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
63428a175baeSEmil Constantinescu 
63438a175baeSEmil Constantinescu   gsum   = err_glb[0];
63448a175baeSEmil Constantinescu   gsuma  = err_glb[1];
63458a175baeSEmil Constantinescu   gsumr  = err_glb[2];
63468a175baeSEmil Constantinescu   n_glb  = err_glb[3];
63478a175baeSEmil Constantinescu   na_glb = err_glb[4];
63488a175baeSEmil Constantinescu   nr_glb = err_glb[5];
63498a175baeSEmil Constantinescu 
63508a175baeSEmil Constantinescu   *norm  = 0.;
63518a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
63528a175baeSEmil Constantinescu   *norma = 0.;
63538a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
63548a175baeSEmil Constantinescu   *normr = 0.;
63558a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
63568a175baeSEmil Constantinescu 
63578a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
63588a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
63598a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
63608a175baeSEmil Constantinescu   PetscFunctionReturn(0);
63618a175baeSEmil Constantinescu }
63628a175baeSEmil Constantinescu 
63638a175baeSEmil Constantinescu /*@
63648a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
63658a175baeSEmil Constantinescu    Collective on TS
63668a175baeSEmil Constantinescu 
63678a175baeSEmil Constantinescu    Input Arguments:
63688a175baeSEmil Constantinescu +  ts - time stepping context
63698a175baeSEmil Constantinescu .  E - error vector
63708a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
63718a175baeSEmil Constantinescu -  Y - state vector, previous time step
63728a175baeSEmil Constantinescu 
63738a175baeSEmil Constantinescu    Output Arguments:
63748a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
63758a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
63768a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
63778a175baeSEmil Constantinescu 
63788a175baeSEmil Constantinescu    Level: developer
63798a175baeSEmil Constantinescu 
63808a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
63818a175baeSEmil Constantinescu @*/
63828a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
63838a175baeSEmil Constantinescu {
63848a175baeSEmil Constantinescu   PetscErrorCode    ierr;
63858a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
63868a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
63878a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
63888a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
63898a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
63908a175baeSEmil Constantinescu 
63918a175baeSEmil Constantinescu   PetscFunctionBegin;
63928a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63938a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
63948a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
63958a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
63968a175baeSEmil Constantinescu   PetscValidType(E,2);
63978a175baeSEmil Constantinescu   PetscValidType(U,3);
63988a175baeSEmil Constantinescu   PetscValidType(Y,4);
63998a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
64008a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
64018a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
64028a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
64038a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
64048a175baeSEmil Constantinescu 
64058a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
64068a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
64078a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
64088a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
64098a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
64108a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
64118a175baeSEmil Constantinescu 
64128a175baeSEmil Constantinescu   max=0.;
64138a175baeSEmil Constantinescu   maxa=0.;
64148a175baeSEmil Constantinescu   maxr=0.;
64158a175baeSEmil Constantinescu 
64168a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
64178a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
64188a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64198a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64208a175baeSEmil Constantinescu 
64218a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64228a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64238a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
64248a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64258a175baeSEmil Constantinescu       tol  = tola+tolr;
64268a175baeSEmil Constantinescu       if(tola>0.){
64278a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64288a175baeSEmil Constantinescu       }
64298a175baeSEmil Constantinescu       if(tolr>0.){
64308a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64318a175baeSEmil Constantinescu       }
64328a175baeSEmil Constantinescu       if(tol>0.){
64338a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64348a175baeSEmil Constantinescu       }
64358a175baeSEmil Constantinescu     }
64368a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64378a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64388a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
64398a175baeSEmil Constantinescu     const PetscScalar *atol;
64408a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64418a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64428a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64438a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
64448a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64458a175baeSEmil Constantinescu       tol  = tola+tolr;
64468a175baeSEmil Constantinescu       if(tola>0.){
64478a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64488a175baeSEmil Constantinescu       }
64498a175baeSEmil Constantinescu       if(tolr>0.){
64508a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64518a175baeSEmil Constantinescu       }
64528a175baeSEmil Constantinescu       if(tol>0.){
64538a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64548a175baeSEmil Constantinescu       }
64558a175baeSEmil Constantinescu     }
64568a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64578a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
64588a175baeSEmil Constantinescu     const PetscScalar *rtol;
64598a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64608a175baeSEmil Constantinescu 
64618a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64628a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64638a175baeSEmil Constantinescu       tola = ts->atol;
64648a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64658a175baeSEmil Constantinescu       tol  = tola+tolr;
64668a175baeSEmil Constantinescu       if(tola>0.){
64678a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64688a175baeSEmil Constantinescu       }
64698a175baeSEmil Constantinescu       if(tolr>0.){
64708a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64718a175baeSEmil Constantinescu       }
64728a175baeSEmil Constantinescu       if(tol>0.){
64738a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64748a175baeSEmil Constantinescu       }
64758a175baeSEmil Constantinescu     }
64768a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64778a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
64788a175baeSEmil Constantinescu 
64798a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64808a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64818a175baeSEmil Constantinescu       tola = ts->atol;
64828a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64838a175baeSEmil Constantinescu       tol  = tola+tolr;
64848a175baeSEmil Constantinescu       if(tola>0.){
64858a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64868a175baeSEmil Constantinescu       }
64878a175baeSEmil Constantinescu       if(tolr>0.){
64888a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64898a175baeSEmil Constantinescu       }
64908a175baeSEmil Constantinescu       if(tol>0.){
64918a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64928a175baeSEmil Constantinescu       }
64938a175baeSEmil Constantinescu     }
64948a175baeSEmil Constantinescu   }
64958a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
64968a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
64978a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
64988a175baeSEmil Constantinescu   err_loc[0] = max;
64998a175baeSEmil Constantinescu   err_loc[1] = maxa;
65008a175baeSEmil Constantinescu   err_loc[2] = maxr;
6501a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
65028a175baeSEmil Constantinescu   gmax   = err_glb[0];
65038a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
65048a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
65058a175baeSEmil Constantinescu 
65068a175baeSEmil Constantinescu   *norm = gmax;
65078a175baeSEmil Constantinescu   *norma = gmaxa;
65088a175baeSEmil Constantinescu   *normr = gmaxr;
65098a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
65108a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
65118a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
65128a175baeSEmil Constantinescu   PetscFunctionReturn(0);
65138a175baeSEmil Constantinescu }
65148a175baeSEmil Constantinescu 
65158a175baeSEmil Constantinescu /*@
65168a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
65178a175baeSEmil Constantinescu 
65188a175baeSEmil Constantinescu    Collective on TS
65198a175baeSEmil Constantinescu 
65208a175baeSEmil Constantinescu    Input Arguments:
65218a175baeSEmil Constantinescu +  ts - time stepping context
65228a175baeSEmil Constantinescu .  E - error vector
65238a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
65248a175baeSEmil Constantinescu .  Y - state vector, previous time step
65258a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
65268a175baeSEmil Constantinescu 
65278a175baeSEmil Constantinescu    Output Arguments:
65288a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
65298a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
65308a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
65318a175baeSEmil Constantinescu 
65328a175baeSEmil Constantinescu    Options Database Keys:
65338a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
65348a175baeSEmil Constantinescu 
65358a175baeSEmil Constantinescu    Level: developer
65368a175baeSEmil Constantinescu 
65378a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
65388a175baeSEmil Constantinescu @*/
65398a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
65408a175baeSEmil Constantinescu {
65418a175baeSEmil Constantinescu   PetscErrorCode ierr;
65428a175baeSEmil Constantinescu 
65438a175baeSEmil Constantinescu   PetscFunctionBegin;
65448a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
65458a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
65468a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
65478a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
65488a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
65498a175baeSEmil Constantinescu   PetscFunctionReturn(0);
65508a175baeSEmil Constantinescu }
65518a175baeSEmil Constantinescu 
65528a175baeSEmil Constantinescu 
65538d59e960SJed Brown /*@
65548d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
65558d59e960SJed Brown 
65568d59e960SJed Brown    Logically Collective on TS
65578d59e960SJed Brown 
65588d59e960SJed Brown    Input Arguments:
65598d59e960SJed Brown +  ts - time stepping context
65608d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
65618d59e960SJed Brown 
65628d59e960SJed Brown    Note:
65638d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
65648d59e960SJed Brown 
65658d59e960SJed Brown    Level: intermediate
65668d59e960SJed Brown 
65678d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
65688d59e960SJed Brown @*/
65698d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
65708d59e960SJed Brown {
65718d59e960SJed Brown   PetscFunctionBegin;
65728d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
65738d59e960SJed Brown   ts->cfltime_local = cfltime;
65748d59e960SJed Brown   ts->cfltime       = -1.;
65758d59e960SJed Brown   PetscFunctionReturn(0);
65768d59e960SJed Brown }
65778d59e960SJed Brown 
65788d59e960SJed Brown /*@
65798d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
65808d59e960SJed Brown 
65818d59e960SJed Brown    Collective on TS
65828d59e960SJed Brown 
65838d59e960SJed Brown    Input Arguments:
65848d59e960SJed Brown .  ts - time stepping context
65858d59e960SJed Brown 
65868d59e960SJed Brown    Output Arguments:
65878d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
65888d59e960SJed Brown 
65898d59e960SJed Brown    Level: advanced
65908d59e960SJed Brown 
65918d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
65928d59e960SJed Brown @*/
65938d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
65948d59e960SJed Brown {
65958d59e960SJed Brown   PetscErrorCode ierr;
65968d59e960SJed Brown 
65978d59e960SJed Brown   PetscFunctionBegin;
65988d59e960SJed Brown   if (ts->cfltime < 0) {
6599b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
66008d59e960SJed Brown   }
66018d59e960SJed Brown   *cfltime = ts->cfltime;
66028d59e960SJed Brown   PetscFunctionReturn(0);
66038d59e960SJed Brown }
66048d59e960SJed Brown 
6605d6ebe24aSShri Abhyankar /*@
6606d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6607d6ebe24aSShri Abhyankar 
6608d6ebe24aSShri Abhyankar    Input Parameters:
6609d6ebe24aSShri Abhyankar .  ts   - the TS context.
6610d6ebe24aSShri Abhyankar .  xl   - lower bound.
6611d6ebe24aSShri Abhyankar .  xu   - upper bound.
6612d6ebe24aSShri Abhyankar 
6613d6ebe24aSShri Abhyankar    Notes:
6614d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6615e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6616d6ebe24aSShri Abhyankar 
66172bd2b0e6SSatish Balay    Level: advanced
66182bd2b0e6SSatish Balay 
6619d6ebe24aSShri Abhyankar @*/
6620d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6621d6ebe24aSShri Abhyankar {
6622d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6623d6ebe24aSShri Abhyankar   SNES           snes;
6624d6ebe24aSShri Abhyankar 
6625d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6626d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6627d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6628d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6629d6ebe24aSShri Abhyankar }
6630d6ebe24aSShri Abhyankar 
6631325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6632c6db04a5SJed Brown #include <mex.h>
6633325fc9f4SBarry Smith 
6634325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6635325fc9f4SBarry Smith 
6636325fc9f4SBarry Smith /*
6637325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6638325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6639325fc9f4SBarry Smith 
6640325fc9f4SBarry Smith    Collective on TS
6641325fc9f4SBarry Smith 
6642325fc9f4SBarry Smith    Input Parameters:
6643325fc9f4SBarry Smith +  snes - the TS context
66440910c330SBarry Smith -  u - input vector
6645325fc9f4SBarry Smith 
6646325fc9f4SBarry Smith    Output Parameter:
6647325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6648325fc9f4SBarry Smith 
6649325fc9f4SBarry Smith    Notes:
6650325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6651325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6652325fc9f4SBarry Smith    themselves.
6653325fc9f4SBarry Smith 
6654325fc9f4SBarry Smith    Level: developer
6655325fc9f4SBarry Smith 
6656325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6657325fc9f4SBarry Smith 
6658325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6659325fc9f4SBarry Smith */
66600910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6661325fc9f4SBarry Smith {
6662325fc9f4SBarry Smith   PetscErrorCode  ierr;
6663325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6664325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6665325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6666325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6667325fc9f4SBarry Smith 
6668325fc9f4SBarry Smith   PetscFunctionBegin;
6669325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
66700910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
66710910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6672325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
66730910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6674325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6675325fc9f4SBarry Smith 
6676325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
66770910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
66780910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
66790910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6680bbd56ea5SKarl Rupp 
6681325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6682325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6683325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6684325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6685325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6686325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6687325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6688325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6689325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6690325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6691325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6692325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6693325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6694325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6695325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6696325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6697325fc9f4SBarry Smith   PetscFunctionReturn(0);
6698325fc9f4SBarry Smith }
6699325fc9f4SBarry Smith 
6700325fc9f4SBarry Smith 
6701325fc9f4SBarry Smith /*
6702325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6703325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6704e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6705325fc9f4SBarry Smith 
6706325fc9f4SBarry Smith    Logically Collective on TS
6707325fc9f4SBarry Smith 
6708325fc9f4SBarry Smith    Input Parameters:
6709325fc9f4SBarry Smith +  ts - the TS context
6710325fc9f4SBarry Smith -  func - function evaluation routine
6711325fc9f4SBarry Smith 
6712325fc9f4SBarry Smith    Calling sequence of func:
67130910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6714325fc9f4SBarry Smith 
6715325fc9f4SBarry Smith    Level: beginner
6716325fc9f4SBarry Smith 
6717325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6718325fc9f4SBarry Smith 
6719325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6720325fc9f4SBarry Smith */
6721cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6722325fc9f4SBarry Smith {
6723325fc9f4SBarry Smith   PetscErrorCode  ierr;
6724325fc9f4SBarry Smith   TSMatlabContext *sctx;
6725325fc9f4SBarry Smith 
6726325fc9f4SBarry Smith   PetscFunctionBegin;
6727325fc9f4SBarry Smith   /* currently sctx is memory bleed */
672895dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6729325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6730325fc9f4SBarry Smith   /*
6731325fc9f4SBarry Smith      This should work, but it doesn't
6732325fc9f4SBarry Smith   sctx->ctx = ctx;
6733325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6734325fc9f4SBarry Smith   */
6735325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6736bbd56ea5SKarl Rupp 
67370298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6738325fc9f4SBarry Smith   PetscFunctionReturn(0);
6739325fc9f4SBarry Smith }
6740325fc9f4SBarry Smith 
6741325fc9f4SBarry Smith /*
6742325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6743325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6744325fc9f4SBarry Smith 
6745325fc9f4SBarry Smith    Collective on TS
6746325fc9f4SBarry Smith 
6747325fc9f4SBarry Smith    Input Parameters:
6748cdcf91faSSean Farley +  ts - the TS context
67490910c330SBarry Smith .  u - input vector
6750325fc9f4SBarry Smith .  A, B - the matrices
6751325fc9f4SBarry Smith -  ctx - user context
6752325fc9f4SBarry Smith 
6753325fc9f4SBarry Smith    Level: developer
6754325fc9f4SBarry Smith 
6755325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6756325fc9f4SBarry Smith 
6757325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6758325fc9f4SBarry Smith @*/
6759f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6760325fc9f4SBarry Smith {
6761325fc9f4SBarry Smith   PetscErrorCode  ierr;
6762325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6763325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6764325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6765325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6766325fc9f4SBarry Smith 
6767325fc9f4SBarry Smith   PetscFunctionBegin;
6768cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
67690910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6770325fc9f4SBarry Smith 
67710910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6772325fc9f4SBarry Smith 
6773cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
67740910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
67750910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
67760910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
67770910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6778bbd56ea5SKarl Rupp 
6779325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6780325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6781325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6782325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6783325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6784325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6785325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6786325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6787325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6788325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6789325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6790325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6791325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6792325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6793325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6794325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6795325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6796325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6797325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6798325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6799325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6800325fc9f4SBarry Smith   PetscFunctionReturn(0);
6801325fc9f4SBarry Smith }
6802325fc9f4SBarry Smith 
6803325fc9f4SBarry Smith 
6804325fc9f4SBarry Smith /*
6805325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6806e3c5b3baSBarry 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
6807325fc9f4SBarry Smith 
6808325fc9f4SBarry Smith    Logically Collective on TS
6809325fc9f4SBarry Smith 
6810325fc9f4SBarry Smith    Input Parameters:
6811cdcf91faSSean Farley +  ts - the TS context
6812325fc9f4SBarry Smith .  A,B - Jacobian matrices
6813325fc9f4SBarry Smith .  func - function evaluation routine
6814325fc9f4SBarry Smith -  ctx - user context
6815325fc9f4SBarry Smith 
6816325fc9f4SBarry Smith    Calling sequence of func:
68170910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6818325fc9f4SBarry Smith 
6819325fc9f4SBarry Smith 
6820325fc9f4SBarry Smith    Level: developer
6821325fc9f4SBarry Smith 
6822325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6823325fc9f4SBarry Smith 
6824325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6825325fc9f4SBarry Smith */
6826cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6827325fc9f4SBarry Smith {
6828325fc9f4SBarry Smith   PetscErrorCode  ierr;
6829325fc9f4SBarry Smith   TSMatlabContext *sctx;
6830325fc9f4SBarry Smith 
6831325fc9f4SBarry Smith   PetscFunctionBegin;
6832325fc9f4SBarry Smith   /* currently sctx is memory bleed */
683395dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6834325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6835325fc9f4SBarry Smith   /*
6836325fc9f4SBarry Smith      This should work, but it doesn't
6837325fc9f4SBarry Smith   sctx->ctx = ctx;
6838325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6839325fc9f4SBarry Smith   */
6840325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6841bbd56ea5SKarl Rupp 
6842cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
6843325fc9f4SBarry Smith   PetscFunctionReturn(0);
6844325fc9f4SBarry Smith }
6845325fc9f4SBarry Smith 
6846b5b1a830SBarry Smith /*
6847b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
6848b5b1a830SBarry Smith 
6849b5b1a830SBarry Smith    Collective on TS
6850b5b1a830SBarry Smith 
6851b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6852b5b1a830SBarry Smith @*/
68530910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6854b5b1a830SBarry Smith {
6855b5b1a830SBarry Smith   PetscErrorCode  ierr;
6856b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6857a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
6858b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
6859b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
6860b5b1a830SBarry Smith 
6861b5b1a830SBarry Smith   PetscFunctionBegin;
6862cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
68630910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
6864b5b1a830SBarry Smith 
6865cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
68660910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
6867bbd56ea5SKarl Rupp 
6868b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6869b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
6870b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
6871b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
6872b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
6873b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
6874b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
6875b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6876b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
6877b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
6878b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
6879b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
6880b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
6881b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
6882b5b1a830SBarry Smith   PetscFunctionReturn(0);
6883b5b1a830SBarry Smith }
6884b5b1a830SBarry Smith 
6885b5b1a830SBarry Smith 
6886b5b1a830SBarry Smith /*
6887b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
6888b5b1a830SBarry Smith 
6889b5b1a830SBarry Smith    Level: developer
6890b5b1a830SBarry Smith 
6891b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
6892b5b1a830SBarry Smith 
6893b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6894b5b1a830SBarry Smith */
6895cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6896b5b1a830SBarry Smith {
6897b5b1a830SBarry Smith   PetscErrorCode  ierr;
6898b5b1a830SBarry Smith   TSMatlabContext *sctx;
6899b5b1a830SBarry Smith 
6900b5b1a830SBarry Smith   PetscFunctionBegin;
6901b5b1a830SBarry Smith   /* currently sctx is memory bleed */
690295dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6903b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6904b5b1a830SBarry Smith   /*
6905b5b1a830SBarry Smith      This should work, but it doesn't
6906b5b1a830SBarry Smith   sctx->ctx = ctx;
6907b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6908b5b1a830SBarry Smith   */
6909b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6910bbd56ea5SKarl Rupp 
69110298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6912b5b1a830SBarry Smith   PetscFunctionReturn(0);
6913b5b1a830SBarry Smith }
6914325fc9f4SBarry Smith #endif
6915b3603a34SBarry Smith 
6916b3603a34SBarry Smith /*@C
69174f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6918b3603a34SBarry Smith        in a time based line graph
6919b3603a34SBarry Smith 
6920b3603a34SBarry Smith    Collective on TS
6921b3603a34SBarry Smith 
6922b3603a34SBarry Smith    Input Parameters:
6923b3603a34SBarry Smith +  ts - the TS context
6924b3603a34SBarry Smith .  step - current time-step
6925b3603a34SBarry Smith .  ptime - current time
69267db568b7SBarry Smith .  u - current solution
69277db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6928b3603a34SBarry Smith 
6929b3d3934dSBarry Smith    Options Database:
69309ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6931b3d3934dSBarry Smith 
6932b3603a34SBarry Smith    Level: intermediate
6933b3603a34SBarry Smith 
69346934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component solutions in a separate window
6935b3603a34SBarry Smith 
6936b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6937b3603a34SBarry Smith 
69387db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
69397db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
69407db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
69417db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6942b3603a34SBarry Smith @*/
69437db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6944b3603a34SBarry Smith {
6945b3603a34SBarry Smith   PetscErrorCode    ierr;
69467db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6947b3603a34SBarry Smith   const PetscScalar *yy;
694880666b62SBarry Smith   Vec               v;
6949b3603a34SBarry Smith 
6950b3603a34SBarry Smith   PetscFunctionBegin;
695163e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
695258ff32f7SBarry Smith   if (!step) {
6953a9f9c1f6SBarry Smith     PetscDrawAxis axis;
69546934998bSLisandro Dalcin     PetscInt      dim;
6955a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6956a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6957bab0a581SBarry Smith     if (!ctx->names) {
6958bab0a581SBarry Smith       PetscBool flg;
6959bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6960bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6961bab0a581SBarry Smith       if (flg) {
6962bab0a581SBarry Smith         PetscInt i,n;
6963bab0a581SBarry Smith         char     **names;
6964bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6965bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6966bab0a581SBarry Smith         for (i=0; i<n; i++) {
6967bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6968bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6969bab0a581SBarry Smith         }
6970bab0a581SBarry Smith         names[n] = NULL;
6971bab0a581SBarry Smith         ctx->names = names;
6972bab0a581SBarry Smith       }
6973bab0a581SBarry Smith     }
6974387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6975387f4636SBarry Smith       char      **displaynames;
6976387f4636SBarry Smith       PetscBool flg;
6977387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
697895dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6979387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6980c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6981387f4636SBarry Smith       if (flg) {
6982a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6983387f4636SBarry Smith       }
6984387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6985387f4636SBarry Smith     }
6986387f4636SBarry Smith     if (ctx->displaynames) {
6987387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6988387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6989387f4636SBarry Smith     } else if (ctx->names) {
69900910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
69910b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6992387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6993b0bc92c6SBarry Smith     } else {
6994b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6995b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6996387f4636SBarry Smith     }
69970b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
699858ff32f7SBarry Smith   }
69996934998bSLisandro Dalcin 
70006934998bSLisandro Dalcin   if (!ctx->transform) v = u;
70016934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
700280666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
70036934998bSLisandro Dalcin   if (ctx->displaynames) {
70046934998bSLisandro Dalcin     PetscInt i;
70056934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
70066934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
70076934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
70086934998bSLisandro Dalcin   } else {
7009e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
7010e3efe391SJed Brown     PetscInt  i,n;
70116934998bSLisandro Dalcin     PetscReal *yreal;
701280666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
7013785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
7014e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
70150b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
7016e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
7017e3efe391SJed Brown #else
70180b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
7019e3efe391SJed Brown #endif
702080666b62SBarry Smith   }
70216934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
70226934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
70236934998bSLisandro Dalcin 
7024b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
70250b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70266934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
70273923b477SBarry Smith   }
7028b3603a34SBarry Smith   PetscFunctionReturn(0);
7029b3603a34SBarry Smith }
7030b3603a34SBarry Smith 
7031387f4636SBarry Smith 
7032b037adc7SBarry Smith /*@C
703331152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
7034b037adc7SBarry Smith 
7035b037adc7SBarry Smith    Collective on TS
7036b037adc7SBarry Smith 
7037b037adc7SBarry Smith    Input Parameters:
7038b037adc7SBarry Smith +  ts - the TS context
7039b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
7040b037adc7SBarry Smith 
7041b037adc7SBarry Smith    Level: intermediate
7042b037adc7SBarry Smith 
70437db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
70447db568b7SBarry Smith 
7045b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
7046b037adc7SBarry Smith 
7047a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
7048b037adc7SBarry Smith @*/
704931152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
7050b037adc7SBarry Smith {
7051b037adc7SBarry Smith   PetscErrorCode    ierr;
7052b037adc7SBarry Smith   PetscInt          i;
7053b037adc7SBarry Smith 
7054b037adc7SBarry Smith   PetscFunctionBegin;
7055b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7056b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
70575537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
7058b3d3934dSBarry Smith       break;
7059b3d3934dSBarry Smith     }
7060b3d3934dSBarry Smith   }
7061b3d3934dSBarry Smith   PetscFunctionReturn(0);
7062b3d3934dSBarry Smith }
7063b3d3934dSBarry Smith 
7064e673d494SBarry Smith /*@C
7065e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
7066e673d494SBarry Smith 
7067e673d494SBarry Smith    Collective on TS
7068e673d494SBarry Smith 
7069e673d494SBarry Smith    Input Parameters:
7070e673d494SBarry Smith +  ts - the TS context
7071e673d494SBarry Smith -  names - the names of the components, final string must be NULL
7072e673d494SBarry Smith 
7073e673d494SBarry Smith    Level: intermediate
7074e673d494SBarry Smith 
7075e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7076e673d494SBarry Smith 
7077a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
7078e673d494SBarry Smith @*/
7079e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
7080e673d494SBarry Smith {
7081e673d494SBarry Smith   PetscErrorCode    ierr;
7082e673d494SBarry Smith 
7083e673d494SBarry Smith   PetscFunctionBegin;
7084e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
7085e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
7086e673d494SBarry Smith   PetscFunctionReturn(0);
7087e673d494SBarry Smith }
7088e673d494SBarry Smith 
7089b3d3934dSBarry Smith /*@C
7090b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
7091b3d3934dSBarry Smith 
7092b3d3934dSBarry Smith    Collective on TS
7093b3d3934dSBarry Smith 
7094b3d3934dSBarry Smith    Input Parameter:
7095b3d3934dSBarry Smith .  ts - the TS context
7096b3d3934dSBarry Smith 
7097b3d3934dSBarry Smith    Output Parameter:
7098b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
7099b3d3934dSBarry Smith 
7100b3d3934dSBarry Smith    Level: intermediate
7101b3d3934dSBarry Smith 
71027db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
71037db568b7SBarry Smith 
7104b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7105b3d3934dSBarry Smith 
7106b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7107b3d3934dSBarry Smith @*/
7108b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
7109b3d3934dSBarry Smith {
7110b3d3934dSBarry Smith   PetscInt       i;
7111b3d3934dSBarry Smith 
7112b3d3934dSBarry Smith   PetscFunctionBegin;
7113b3d3934dSBarry Smith   *names = NULL;
7114b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7115b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
71165537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
7117b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
7118b3d3934dSBarry Smith       break;
7119387f4636SBarry Smith     }
7120387f4636SBarry Smith   }
7121387f4636SBarry Smith   PetscFunctionReturn(0);
7122387f4636SBarry Smith }
7123387f4636SBarry Smith 
7124a66092f1SBarry Smith /*@C
7125a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
7126a66092f1SBarry Smith 
7127a66092f1SBarry Smith    Collective on TS
7128a66092f1SBarry Smith 
7129a66092f1SBarry Smith    Input Parameters:
7130a66092f1SBarry Smith +  ctx - the TSMonitorLG context
7131a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
7132a66092f1SBarry Smith 
7133a66092f1SBarry Smith    Level: intermediate
7134a66092f1SBarry Smith 
7135a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
7136a66092f1SBarry Smith 
7137a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7138a66092f1SBarry Smith @*/
7139a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
7140a66092f1SBarry Smith {
7141a66092f1SBarry Smith   PetscInt          j = 0,k;
7142a66092f1SBarry Smith   PetscErrorCode    ierr;
7143a66092f1SBarry Smith 
7144a66092f1SBarry Smith   PetscFunctionBegin;
7145a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
7146a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
7147a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
7148a66092f1SBarry Smith   while (displaynames[j]) j++;
7149a66092f1SBarry Smith   ctx->ndisplayvariables = j;
7150a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
7151a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
7152a66092f1SBarry Smith   j = 0;
7153a66092f1SBarry Smith   while (displaynames[j]) {
7154a66092f1SBarry Smith     k = 0;
7155a66092f1SBarry Smith     while (ctx->names[k]) {
7156a66092f1SBarry Smith       PetscBool flg;
7157a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
7158a66092f1SBarry Smith       if (flg) {
7159a66092f1SBarry Smith         ctx->displayvariables[j] = k;
7160a66092f1SBarry Smith         break;
7161a66092f1SBarry Smith       }
7162a66092f1SBarry Smith       k++;
7163a66092f1SBarry Smith     }
7164a66092f1SBarry Smith     j++;
7165a66092f1SBarry Smith   }
7166a66092f1SBarry Smith   PetscFunctionReturn(0);
7167a66092f1SBarry Smith }
7168a66092f1SBarry Smith 
7169a66092f1SBarry Smith 
7170387f4636SBarry Smith /*@C
7171387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
7172387f4636SBarry Smith 
7173387f4636SBarry Smith    Collective on TS
7174387f4636SBarry Smith 
7175387f4636SBarry Smith    Input Parameters:
7176387f4636SBarry Smith +  ts - the TS context
7177387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
7178387f4636SBarry Smith 
71797db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
71807db568b7SBarry Smith 
7181387f4636SBarry Smith    Level: intermediate
7182387f4636SBarry Smith 
7183387f4636SBarry Smith .keywords: TS,  vector, monitor, view
7184387f4636SBarry Smith 
7185387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7186387f4636SBarry Smith @*/
7187387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
7188387f4636SBarry Smith {
7189a66092f1SBarry Smith   PetscInt          i;
7190387f4636SBarry Smith   PetscErrorCode    ierr;
7191387f4636SBarry Smith 
7192387f4636SBarry Smith   PetscFunctionBegin;
7193387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7194387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
71955537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
7196b3d3934dSBarry Smith       break;
7197b037adc7SBarry Smith     }
7198b037adc7SBarry Smith   }
7199b037adc7SBarry Smith   PetscFunctionReturn(0);
7200b037adc7SBarry Smith }
7201b037adc7SBarry Smith 
720280666b62SBarry Smith /*@C
720380666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
720480666b62SBarry Smith 
720580666b62SBarry Smith    Collective on TS
720680666b62SBarry Smith 
720780666b62SBarry Smith    Input Parameters:
720880666b62SBarry Smith +  ts - the TS context
720980666b62SBarry Smith .  transform - the transform function
72107684fa3eSBarry Smith .  destroy - function to destroy the optional context
721180666b62SBarry Smith -  ctx - optional context used by transform function
721280666b62SBarry Smith 
72137db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
72147db568b7SBarry Smith 
721580666b62SBarry Smith    Level: intermediate
721680666b62SBarry Smith 
721780666b62SBarry Smith .keywords: TS,  vector, monitor, view
721880666b62SBarry Smith 
7219a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
722080666b62SBarry Smith @*/
72217684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
722280666b62SBarry Smith {
722380666b62SBarry Smith   PetscInt          i;
7224a66092f1SBarry Smith   PetscErrorCode    ierr;
722580666b62SBarry Smith 
722680666b62SBarry Smith   PetscFunctionBegin;
722780666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
722880666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
72295537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
723080666b62SBarry Smith     }
723180666b62SBarry Smith   }
723280666b62SBarry Smith   PetscFunctionReturn(0);
723380666b62SBarry Smith }
723480666b62SBarry Smith 
7235e673d494SBarry Smith /*@C
7236e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
7237e673d494SBarry Smith 
7238e673d494SBarry Smith    Collective on TSLGCtx
7239e673d494SBarry Smith 
7240e673d494SBarry Smith    Input Parameters:
7241e673d494SBarry Smith +  ts - the TS context
7242e673d494SBarry Smith .  transform - the transform function
72437684fa3eSBarry Smith .  destroy - function to destroy the optional context
7244e673d494SBarry Smith -  ctx - optional context used by transform function
7245e673d494SBarry Smith 
7246e673d494SBarry Smith    Level: intermediate
7247e673d494SBarry Smith 
7248e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7249e673d494SBarry Smith 
7250a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
7251e673d494SBarry Smith @*/
72527684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
7253e673d494SBarry Smith {
7254e673d494SBarry Smith   PetscFunctionBegin;
7255e673d494SBarry Smith   ctx->transform    = transform;
72567684fa3eSBarry Smith   ctx->transformdestroy = destroy;
7257e673d494SBarry Smith   ctx->transformctx = tctx;
7258e673d494SBarry Smith   PetscFunctionReturn(0);
7259e673d494SBarry Smith }
7260e673d494SBarry Smith 
7261ef20d060SBarry Smith /*@C
72624f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
7263ef20d060SBarry Smith        in a time based line graph
7264ef20d060SBarry Smith 
7265ef20d060SBarry Smith    Collective on TS
7266ef20d060SBarry Smith 
7267ef20d060SBarry Smith    Input Parameters:
7268ef20d060SBarry Smith +  ts - the TS context
7269ef20d060SBarry Smith .  step - current time-step
7270ef20d060SBarry Smith .  ptime - current time
72717db568b7SBarry Smith .  u - current solution
72727db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
7273ef20d060SBarry Smith 
7274ef20d060SBarry Smith    Level: intermediate
7275ef20d060SBarry Smith 
72766934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component errors in a separate window
7277abd5a294SJed Brown 
7278abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
7279abd5a294SJed Brown 
7280abd5a294SJed Brown    Options Database Keys:
72814f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
7282ef20d060SBarry Smith 
7283ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
7284ef20d060SBarry Smith 
7285abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
7286ef20d060SBarry Smith @*/
72870910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
7288ef20d060SBarry Smith {
7289ef20d060SBarry Smith   PetscErrorCode    ierr;
72900b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
7291ef20d060SBarry Smith   const PetscScalar *yy;
7292ef20d060SBarry Smith   Vec               y;
7293ef20d060SBarry Smith 
7294ef20d060SBarry Smith   PetscFunctionBegin;
7295a9f9c1f6SBarry Smith   if (!step) {
7296a9f9c1f6SBarry Smith     PetscDrawAxis axis;
72976934998bSLisandro Dalcin     PetscInt      dim;
7298a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
72991ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
73000910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
7301a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7302a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7303a9f9c1f6SBarry Smith   }
73040910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
7305ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
73060910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7307ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
7308e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
7309e3efe391SJed Brown   {
7310e3efe391SJed Brown     PetscReal *yreal;
7311e3efe391SJed Brown     PetscInt  i,n;
7312e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
7313785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
7314e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
73150b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
7316e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
7317e3efe391SJed Brown   }
7318e3efe391SJed Brown #else
73190b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
7320e3efe391SJed Brown #endif
7321ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
7322ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
7323b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
73240b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
73256934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
73263923b477SBarry Smith   }
7327ef20d060SBarry Smith   PetscFunctionReturn(0);
7328ef20d060SBarry Smith }
7329ef20d060SBarry Smith 
7330201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7331201da799SBarry Smith {
7332201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7333201da799SBarry Smith   PetscReal      x   = ptime,y;
7334201da799SBarry Smith   PetscErrorCode ierr;
7335201da799SBarry Smith   PetscInt       its;
7336201da799SBarry Smith 
7337201da799SBarry Smith   PetscFunctionBegin;
733863e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7339201da799SBarry Smith   if (!n) {
7340201da799SBarry Smith     PetscDrawAxis axis;
7341201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7342201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7343201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7344201da799SBarry Smith     ctx->snes_its = 0;
7345201da799SBarry Smith   }
7346201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7347201da799SBarry Smith   y    = its - ctx->snes_its;
7348201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
73493fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7350201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
73516934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7352201da799SBarry Smith   }
7353201da799SBarry Smith   ctx->snes_its = its;
7354201da799SBarry Smith   PetscFunctionReturn(0);
7355201da799SBarry Smith }
7356201da799SBarry Smith 
7357201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7358201da799SBarry Smith {
7359201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7360201da799SBarry Smith   PetscReal      x   = ptime,y;
7361201da799SBarry Smith   PetscErrorCode ierr;
7362201da799SBarry Smith   PetscInt       its;
7363201da799SBarry Smith 
7364201da799SBarry Smith   PetscFunctionBegin;
736563e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7366201da799SBarry Smith   if (!n) {
7367201da799SBarry Smith     PetscDrawAxis axis;
7368201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7369201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7370201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7371201da799SBarry Smith     ctx->ksp_its = 0;
7372201da799SBarry Smith   }
7373201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7374201da799SBarry Smith   y    = its - ctx->ksp_its;
7375201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
737699fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7377201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
73786934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7379201da799SBarry Smith   }
7380201da799SBarry Smith   ctx->ksp_its = its;
7381201da799SBarry Smith   PetscFunctionReturn(0);
7382201da799SBarry Smith }
7383f9c1d6abSBarry Smith 
7384f9c1d6abSBarry Smith /*@
7385f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7386f9c1d6abSBarry Smith 
7387f9c1d6abSBarry Smith    Collective on TS and Vec
7388f9c1d6abSBarry Smith 
7389f9c1d6abSBarry Smith    Input Parameters:
7390f9c1d6abSBarry Smith +  ts - the TS context
7391f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7392f9c1d6abSBarry Smith 
7393f9c1d6abSBarry Smith    Output Parameters:
7394f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7395f9c1d6abSBarry Smith 
7396f9c1d6abSBarry Smith    Level: developer
7397f9c1d6abSBarry Smith 
7398f9c1d6abSBarry Smith .keywords: TS, compute
7399f9c1d6abSBarry Smith 
7400f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7401f9c1d6abSBarry Smith @*/
7402f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7403f9c1d6abSBarry Smith {
7404f9c1d6abSBarry Smith   PetscErrorCode ierr;
7405f9c1d6abSBarry Smith 
7406f9c1d6abSBarry Smith   PetscFunctionBegin;
7407f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7408ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7409f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7410f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7411f9c1d6abSBarry Smith }
741224655328SShri 
7413b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7414b3d3934dSBarry Smith /*@C
7415b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7416b3d3934dSBarry Smith 
7417b3d3934dSBarry Smith    Collective on TS
7418b3d3934dSBarry Smith 
7419b3d3934dSBarry Smith    Input Parameters:
7420b3d3934dSBarry Smith .  ts  - the ODE solver object
7421b3d3934dSBarry Smith 
7422b3d3934dSBarry Smith    Output Parameter:
7423b3d3934dSBarry Smith .  ctx - the context
7424b3d3934dSBarry Smith 
7425b3d3934dSBarry Smith    Level: intermediate
7426b3d3934dSBarry Smith 
7427b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
7428b3d3934dSBarry Smith 
7429b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7430b3d3934dSBarry Smith 
7431b3d3934dSBarry Smith @*/
7432b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7433b3d3934dSBarry Smith {
7434b3d3934dSBarry Smith   PetscErrorCode ierr;
7435b3d3934dSBarry Smith 
7436b3d3934dSBarry Smith   PetscFunctionBegin;
7437a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7438b3d3934dSBarry Smith   PetscFunctionReturn(0);
7439b3d3934dSBarry Smith }
7440b3d3934dSBarry Smith 
7441b3d3934dSBarry Smith /*@C
7442b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7443b3d3934dSBarry Smith 
7444b3d3934dSBarry Smith    Collective on TS
7445b3d3934dSBarry Smith 
7446b3d3934dSBarry Smith    Input Parameters:
7447b3d3934dSBarry Smith +  ts - the TS context
7448b3d3934dSBarry Smith .  step - current time-step
7449b3d3934dSBarry Smith .  ptime - current time
74507db568b7SBarry Smith .  u  - current solution
74517db568b7SBarry Smith -  dctx - the envelope context
7452b3d3934dSBarry Smith 
7453b3d3934dSBarry Smith    Options Database:
7454b3d3934dSBarry Smith .  -ts_monitor_envelope
7455b3d3934dSBarry Smith 
7456b3d3934dSBarry Smith    Level: intermediate
7457b3d3934dSBarry Smith 
7458b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7459b3d3934dSBarry Smith 
7460b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7461b3d3934dSBarry Smith 
74627db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7463b3d3934dSBarry Smith @*/
74647db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7465b3d3934dSBarry Smith {
7466b3d3934dSBarry Smith   PetscErrorCode       ierr;
74677db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7468b3d3934dSBarry Smith 
7469b3d3934dSBarry Smith   PetscFunctionBegin;
7470b3d3934dSBarry Smith   if (!ctx->max) {
7471b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7472b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7473b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7474b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7475b3d3934dSBarry Smith   } else {
7476b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7477b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7478b3d3934dSBarry Smith   }
7479b3d3934dSBarry Smith   PetscFunctionReturn(0);
7480b3d3934dSBarry Smith }
7481b3d3934dSBarry Smith 
7482b3d3934dSBarry Smith 
7483b3d3934dSBarry Smith /*@C
7484b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7485b3d3934dSBarry Smith 
7486b3d3934dSBarry Smith    Collective on TS
7487b3d3934dSBarry Smith 
7488b3d3934dSBarry Smith    Input Parameter:
7489b3d3934dSBarry Smith .  ts - the TS context
7490b3d3934dSBarry Smith 
7491b3d3934dSBarry Smith    Output Parameter:
7492b3d3934dSBarry Smith +  max - the maximum values
7493b3d3934dSBarry Smith -  min - the minimum values
7494b3d3934dSBarry Smith 
74957db568b7SBarry Smith    Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
74967db568b7SBarry Smith 
7497b3d3934dSBarry Smith    Level: intermediate
7498b3d3934dSBarry Smith 
7499b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7500b3d3934dSBarry Smith 
7501b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7502b3d3934dSBarry Smith @*/
7503b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7504b3d3934dSBarry Smith {
7505b3d3934dSBarry Smith   PetscInt i;
7506b3d3934dSBarry Smith 
7507b3d3934dSBarry Smith   PetscFunctionBegin;
7508b3d3934dSBarry Smith   if (max) *max = NULL;
7509b3d3934dSBarry Smith   if (min) *min = NULL;
7510b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7511b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
75125537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7513b3d3934dSBarry Smith       if (max) *max = ctx->max;
7514b3d3934dSBarry Smith       if (min) *min = ctx->min;
7515b3d3934dSBarry Smith       break;
7516b3d3934dSBarry Smith     }
7517b3d3934dSBarry Smith   }
7518b3d3934dSBarry Smith   PetscFunctionReturn(0);
7519b3d3934dSBarry Smith }
7520b3d3934dSBarry Smith 
7521b3d3934dSBarry Smith /*@C
7522b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7523b3d3934dSBarry Smith 
7524b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7525b3d3934dSBarry Smith 
7526b3d3934dSBarry Smith    Input Parameter:
7527b3d3934dSBarry Smith .  ctx - the monitor context
7528b3d3934dSBarry Smith 
7529b3d3934dSBarry Smith    Level: intermediate
7530b3d3934dSBarry Smith 
7531b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7532b3d3934dSBarry Smith 
75337db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7534b3d3934dSBarry Smith @*/
7535b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7536b3d3934dSBarry Smith {
7537b3d3934dSBarry Smith   PetscErrorCode ierr;
7538b3d3934dSBarry Smith 
7539b3d3934dSBarry Smith   PetscFunctionBegin;
7540b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7541b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7542b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7543b3d3934dSBarry Smith   PetscFunctionReturn(0);
7544b3d3934dSBarry Smith }
7545f2dee214SBarry Smith 
754624655328SShri /*@
754724655328SShri    TSRollBack - Rolls back one time step
754824655328SShri 
754924655328SShri    Collective on TS
755024655328SShri 
755124655328SShri    Input Parameter:
755224655328SShri .  ts - the TS context obtained from TSCreate()
755324655328SShri 
755424655328SShri    Level: advanced
755524655328SShri 
755624655328SShri .keywords: TS, timestep, rollback
755724655328SShri 
755824655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
755924655328SShri @*/
756024655328SShri PetscErrorCode  TSRollBack(TS ts)
756124655328SShri {
756224655328SShri   PetscErrorCode ierr;
756324655328SShri 
756424655328SShri   PetscFunctionBegin;
756524655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7566b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
756724655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
756824655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
756924655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
757024655328SShri   ts->ptime = ts->ptime_prev;
7571be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
7572be5899b3SLisandro Dalcin   ts->steps--; ts->total_steps--;
757310b82f12SShri Abhyankar   ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
7574b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
757524655328SShri   PetscFunctionReturn(0);
757624655328SShri }
7577aeb4809dSShri Abhyankar 
7578ff22ae23SHong Zhang /*@
7579ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7580ff22ae23SHong Zhang 
7581ff22ae23SHong Zhang    Input Parameter:
7582ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7583ff22ae23SHong Zhang 
7584ff22ae23SHong Zhang    Level: advanced
7585ff22ae23SHong Zhang 
7586ff22ae23SHong Zhang .keywords: TS, getstages
7587ff22ae23SHong Zhang 
7588ff22ae23SHong Zhang .seealso: TSCreate()
7589ff22ae23SHong Zhang @*/
7590ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7591ff22ae23SHong Zhang {
7592ff22ae23SHong Zhang   PetscErrorCode ierr;
7593ff22ae23SHong Zhang 
7594ff22ae23SHong Zhang   PetscFunctionBegin;
7595ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7596ff22ae23SHong Zhang   PetscValidPointer(ns,2);
7597ff22ae23SHong Zhang 
7598ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
7599ff22ae23SHong Zhang   else {
7600ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7601ff22ae23SHong Zhang   }
7602ff22ae23SHong Zhang   PetscFunctionReturn(0);
7603ff22ae23SHong Zhang }
7604ff22ae23SHong Zhang 
7605847ff0e1SMatthew G. Knepley /*@C
7606847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7607847ff0e1SMatthew G. Knepley 
7608847ff0e1SMatthew G. Knepley   Collective on SNES
7609847ff0e1SMatthew G. Knepley 
7610847ff0e1SMatthew G. Knepley   Input Parameters:
7611847ff0e1SMatthew G. Knepley + ts - the TS context
7612847ff0e1SMatthew G. Knepley . t - current timestep
7613847ff0e1SMatthew G. Knepley . U - state vector
7614847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7615847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7616847ff0e1SMatthew G. Knepley - ctx - an optional user context
7617847ff0e1SMatthew G. Knepley 
7618847ff0e1SMatthew G. Knepley   Output Parameters:
7619847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7620847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7621847ff0e1SMatthew G. Knepley 
7622847ff0e1SMatthew G. Knepley   Level: intermediate
7623847ff0e1SMatthew G. Knepley 
7624847ff0e1SMatthew G. Knepley   Notes:
7625847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7626847ff0e1SMatthew G. Knepley 
7627847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7628847ff0e1SMatthew G. Knepley 
7629847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7630847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7631847ff0e1SMatthew G. Knepley 
7632847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7633847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7634847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7635847ff0e1SMatthew G. Knepley 
7636847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7637847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7638847ff0e1SMatthew G. Knepley @*/
7639847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7640847ff0e1SMatthew G. Knepley {
7641847ff0e1SMatthew G. Knepley   SNES           snes;
7642847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7643847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7644847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7645847ff0e1SMatthew G. Knepley 
7646847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7647c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7648847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7649847ff0e1SMatthew G. Knepley   if (!color) {
7650847ff0e1SMatthew G. Knepley     DM         dm;
7651847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7652847ff0e1SMatthew G. Knepley 
7653847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7654847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7655847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7656847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7657847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7658847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7659847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7660847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7661847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7662847ff0e1SMatthew G. Knepley     } else {
7663847ff0e1SMatthew G. Knepley       MatColoring mc;
7664847ff0e1SMatthew G. Knepley 
7665847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7666847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7667847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7668847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7669847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7670847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7671847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7672847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7673847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7674847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7675847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7676847ff0e1SMatthew G. Knepley     }
7677847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7678847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7679847ff0e1SMatthew G. Knepley   }
7680847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7681847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7682847ff0e1SMatthew G. Knepley   if (J != B) {
7683847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7684847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7685847ff0e1SMatthew G. Knepley   }
7686847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7687847ff0e1SMatthew G. Knepley }
768893b34091SDebojyoti Ghosh 
7689cb9d8021SPierre Barbier de Reuille /*@
7690cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7691cb9d8021SPierre Barbier de Reuille 
7692cb9d8021SPierre Barbier de Reuille     Input Parameters:
7693cb9d8021SPierre Barbier de Reuille     ts - the TS context
7694cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7695cb9d8021SPierre Barbier de Reuille 
7696cb9d8021SPierre Barbier de Reuille     Level: intermediate
7697cb9d8021SPierre Barbier de Reuille 
7698cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7699cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7700cb9d8021SPierre Barbier de Reuille @*/
7701cb9d8021SPierre Barbier de Reuille 
7702d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7703cb9d8021SPierre Barbier de Reuille {
7704cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7705cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7706cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7707cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7708cb9d8021SPierre Barbier de Reuille }
7709cb9d8021SPierre Barbier de Reuille 
7710cb9d8021SPierre Barbier de Reuille /*@
7711cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7712cb9d8021SPierre Barbier de Reuille 
7713cb9d8021SPierre Barbier de Reuille     Input Parameters:
7714cb9d8021SPierre Barbier de Reuille     ts - the TS context
7715cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7716d183316bSPierre Barbier de Reuille     Y - state vector to check.
7717cb9d8021SPierre Barbier de Reuille 
7718cb9d8021SPierre Barbier de Reuille     Output Parameter:
7719cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7720cb9d8021SPierre Barbier de Reuille 
7721cb9d8021SPierre Barbier de Reuille     Note:
7722cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7723cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
772496a0c994SBarry Smith 
772596a0c994SBarry Smith     Level: advanced
7726cb9d8021SPierre Barbier de Reuille @*/
7727d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7728cb9d8021SPierre Barbier de Reuille {
7729cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
7730cb9d8021SPierre Barbier de Reuille 
7731cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7732cb9d8021SPierre Barbier de Reuille 
7733cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7734cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7735cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7736d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7737cb9d8021SPierre Barbier de Reuille   }
7738cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7739cb9d8021SPierre Barbier de Reuille }
77401ceb14c0SBarry Smith 
774193b34091SDebojyoti Ghosh /*@C
7742e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
774393b34091SDebojyoti Ghosh 
774493b34091SDebojyoti Ghosh   Collective on MPI_Comm
774593b34091SDebojyoti Ghosh 
774693b34091SDebojyoti Ghosh   Input Parameter:
774793b34091SDebojyoti Ghosh . tsin    - The input TS
774893b34091SDebojyoti Ghosh 
774993b34091SDebojyoti Ghosh   Output Parameter:
7750e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
775193b34091SDebojyoti Ghosh 
77525eca1a21SEmil Constantinescu   Notes:
77535eca1a21SEmil 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.
77545eca1a21SEmil Constantinescu 
7755baa10174SEmil 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);
77565eca1a21SEmil Constantinescu 
77575eca1a21SEmil Constantinescu   Level: developer
775893b34091SDebojyoti Ghosh 
7759e5168f73SEmil Constantinescu .keywords: TS, clone
7760e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
776193b34091SDebojyoti Ghosh @*/
7762baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
776393b34091SDebojyoti Ghosh {
776493b34091SDebojyoti Ghosh   TS             t;
776593b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7766dc846ba4SSatish Balay   SNES           snes_start;
7767dc846ba4SSatish Balay   DM             dm;
7768dc846ba4SSatish Balay   TSType         type;
776993b34091SDebojyoti Ghosh 
777093b34091SDebojyoti Ghosh   PetscFunctionBegin;
777193b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
777293b34091SDebojyoti Ghosh   *tsout = NULL;
777393b34091SDebojyoti Ghosh 
77747a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
777593b34091SDebojyoti Ghosh 
777693b34091SDebojyoti Ghosh   /* General TS description */
777793b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
777893b34091SDebojyoti Ghosh   t->setupcalled       = 0;
777993b34091SDebojyoti Ghosh   t->ksp_its           = 0;
778093b34091SDebojyoti Ghosh   t->snes_its          = 0;
778193b34091SDebojyoti Ghosh   t->nwork             = 0;
778293b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
778393b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
778493b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
778593b34091SDebojyoti Ghosh 
778634561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
778734561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7788d15a3a53SEmil Constantinescu 
778993b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
779093b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
779193b34091SDebojyoti Ghosh 
779293b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
779351699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
779493b34091SDebojyoti Ghosh 
7795e7069c78SShri   t->trajectory = tsin->trajectory;
7796e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7797e7069c78SShri 
7798e7069c78SShri   t->event = tsin->event;
77996b10a48eSSatish Balay   if (t->event) t->event->refct++;
7800e7069c78SShri 
780193b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
780293b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7803e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
780493b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
780593b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
780693b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
7807e7069c78SShri   t->total_steps       = tsin->total_steps;
780893b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
780993b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
781093b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
781193b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
781293b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
781393b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
781493b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
781593b34091SDebojyoti Ghosh 
781693b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
781793b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
781893b34091SDebojyoti Ghosh 
781993b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
782093b34091SDebojyoti Ghosh 
782193b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
782293b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
782393b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
782493b34091SDebojyoti Ghosh 
782593b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
782693b34091SDebojyoti Ghosh 
78270d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
78280d4fed19SBarry Smith     PetscInt i;
78290d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
78300d4fed19SBarry Smith     for (i=0; i<10; i++) {
78310d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
78320d4fed19SBarry Smith     }
78330d4fed19SBarry Smith   }
783493b34091SDebojyoti Ghosh   *tsout = t;
783593b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
783693b34091SDebojyoti Ghosh }
7837