xref: /petsc/src/ts/interface/ts.c (revision e7069c78e74748c563594f592e4924a0e908de35)
163dd3a1aSKris Buschelman 
2af0996ceSBarry Smith #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
41e25c274SJed Brown #include <petscdmda.h>
52d5ee99bSBarry Smith #include <petscviewer.h>
62d5ee99bSBarry Smith #include <petscdraw.h>
7d763cef2SBarry Smith 
8d5ba7fb7SMatthew Knepley /* Logging support */
9d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
108d0ad7a8SHong Zhang PetscLogEvent TS_AdjointStep, TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
11d405a339SMatthew Knepley 
12feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1349354f04SShri Abhyankar 
142d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx {
152d5ee99bSBarry Smith   PetscViewer   viewer;
162d5ee99bSBarry Smith   Vec           initialsolution;
172d5ee99bSBarry Smith   PetscBool     showinitial;
182d5ee99bSBarry Smith   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
192d5ee99bSBarry Smith   PetscBool     showtimestepandtime;
202d5ee99bSBarry Smith };
212d5ee99bSBarry Smith 
22bdad233fSMatthew Knepley #undef __FUNCT__
23fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSetFromOptions"
24fde5950dSBarry Smith /*@C
25fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
26fde5950dSBarry Smith 
27fde5950dSBarry Smith    Collective on TS
28fde5950dSBarry Smith 
29fde5950dSBarry Smith    Input Parameters:
30fde5950dSBarry Smith +  ts - TS object you wish to monitor
31fde5950dSBarry Smith .  name - the monitor type one is seeking
32fde5950dSBarry Smith .  help - message indicating what monitoring is done
33fde5950dSBarry Smith .  manual - manual page for the monitor
34fde5950dSBarry Smith .  monitor - the monitor function
35fde5950dSBarry Smith -  monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects
36fde5950dSBarry Smith 
37fde5950dSBarry Smith    Level: developer
38fde5950dSBarry Smith 
39fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
40fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
41fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
42fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
43fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
44fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
45fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
46fde5950dSBarry Smith @*/
47721cd6eeSBarry Smith PetscErrorCode  TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
48fde5950dSBarry Smith {
49fde5950dSBarry Smith   PetscErrorCode    ierr;
50fde5950dSBarry Smith   PetscViewer       viewer;
51fde5950dSBarry Smith   PetscViewerFormat format;
52fde5950dSBarry Smith   PetscBool         flg;
53fde5950dSBarry Smith 
54fde5950dSBarry Smith   PetscFunctionBegin;
55fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
56fde5950dSBarry Smith   if (flg) {
57721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
58721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
59721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
60fde5950dSBarry Smith     if (monitorsetup) {
61721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
62fde5950dSBarry Smith     }
63721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
64fde5950dSBarry Smith   }
65fde5950dSBarry Smith   PetscFunctionReturn(0);
66fde5950dSBarry Smith }
67fde5950dSBarry Smith 
68fde5950dSBarry Smith #undef __FUNCT__
69fde5950dSBarry Smith #define __FUNCT__ "TSAdjointMonitorSetFromOptions"
70fde5950dSBarry Smith /*@C
71fde5950dSBarry Smith    TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
72fde5950dSBarry Smith 
73fde5950dSBarry Smith    Collective on TS
74fde5950dSBarry Smith 
75fde5950dSBarry Smith    Input Parameters:
76fde5950dSBarry Smith +  ts - TS object you wish to monitor
77fde5950dSBarry Smith .  name - the monitor type one is seeking
78fde5950dSBarry Smith .  help - message indicating what monitoring is done
79fde5950dSBarry Smith .  manual - manual page for the monitor
80fde5950dSBarry Smith .  monitor - the monitor function
81fde5950dSBarry Smith -  monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects
82fde5950dSBarry Smith 
83fde5950dSBarry Smith    Level: developer
84fde5950dSBarry Smith 
85fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
86fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
87fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
88fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
89fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
90fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
91fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
92fde5950dSBarry Smith @*/
93721cd6eeSBarry Smith PetscErrorCode  TSAdjointMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
94fde5950dSBarry Smith {
95fde5950dSBarry Smith   PetscErrorCode    ierr;
96fde5950dSBarry Smith   PetscViewer       viewer;
97fde5950dSBarry Smith   PetscViewerFormat format;
98fde5950dSBarry Smith   PetscBool         flg;
99fde5950dSBarry Smith 
100fde5950dSBarry Smith   PetscFunctionBegin;
101fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
102fde5950dSBarry Smith   if (flg) {
103721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
104721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
105721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
106fde5950dSBarry Smith     if (monitorsetup) {
107721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
108fde5950dSBarry Smith     }
109721cd6eeSBarry Smith     ierr = TSAdjointMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
110fde5950dSBarry Smith   }
111fde5950dSBarry Smith   PetscFunctionReturn(0);
112fde5950dSBarry Smith }
113fde5950dSBarry Smith 
114fde5950dSBarry Smith #undef __FUNCT__
115bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
116bdad233fSMatthew Knepley /*@
117bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
118bdad233fSMatthew Knepley 
119bdad233fSMatthew Knepley    Collective on TS
120bdad233fSMatthew Knepley 
121bdad233fSMatthew Knepley    Input Parameter:
122bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
123bdad233fSMatthew Knepley 
124bdad233fSMatthew Knepley    Options Database Keys:
125b6a60446SDebojyoti Ghosh +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE
126ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
1273e4cdcaaSBarry Smith .  -ts_max_steps <maxsteps> - maximum number of time-steps to take
1283e4cdcaaSBarry Smith .  -ts_final_time <time> - maximum time to compute to
1293e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
130a3cdaa26SBarry Smith .  -ts_exact_final_time <stepover,interpolate,matchstep> whether to stop at the exact given final time and how to compute the solution at that ti,e
131a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
132a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
133a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
134a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
135a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
136ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
137847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
138bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
139de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
140de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1416934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
142de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
143de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
144de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
145de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1463e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1473e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
148fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
149e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
150110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
151110eb670SHong Zhang .  -ts_adjoint_monitor - print information at each adjoint time step
15253ea634cSHong Zhang -  -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically
15353ea634cSHong Zhang 
15491b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
155bdad233fSMatthew Knepley 
156bdad233fSMatthew Knepley    Level: beginner
157bdad233fSMatthew Knepley 
158bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
159bdad233fSMatthew Knepley 
160a313700dSBarry Smith .seealso: TSGetType()
161bdad233fSMatthew Knepley @*/
1627087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
163bdad233fSMatthew Knepley {
164bc952696SBarry Smith   PetscBool              opt,flg,tflg;
165dfbe8321SBarry Smith   PetscErrorCode         ierr;
166eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
16731748224SBarry Smith   PetscReal              time_step;
16849354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
169d1212d36SBarry Smith   char                   dir[16];
170cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1716991f827SBarry Smith   const char             *defaultType;
1726991f827SBarry Smith   char                   typeName[256];
173bdad233fSMatthew Knepley 
174bdad233fSMatthew Knepley   PetscFunctionBegin;
1750700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1766991f827SBarry Smith 
1776991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
178cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
179cd11d68dSLisandro Dalcin 
180cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
181cd11d68dSLisandro Dalcin   if (((PetscObject)ts)->type_name)
182cd11d68dSLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
183cd11d68dSLisandro Dalcin   else
184cd11d68dSLisandro Dalcin     defaultType = ifun ? TSBEULER : TSEULER;
1856991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1866991f827SBarry Smith   if (opt) {
1876991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1886991f827SBarry Smith   } else {
1896991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1906991f827SBarry Smith   }
191bdad233fSMatthew Knepley 
192bdad233fSMatthew Knepley   /* Handle generic TS options */
1930298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1940298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1950298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
19631748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
197cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
19849354f04SShri Abhyankar   ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr);
19949354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
2000298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);CHKERRQ(ierr);
2010298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
2020298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
2030298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
2040298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
205bdad233fSMatthew Knepley 
20656f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
20756f85f32SBarry Smith   {
20856f85f32SBarry Smith   PetscBool set;
20956f85f32SBarry Smith   flg  = PETSC_FALSE;
21056f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
21156f85f32SBarry Smith   if (set) {
21256f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
21356f85f32SBarry Smith   }
21456f85f32SBarry Smith   }
21556f85f32SBarry Smith #endif
21656f85f32SBarry Smith 
217bdad233fSMatthew Knepley   /* Monitor options */
218cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
219fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
220fde5950dSBarry Smith   ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr);
221fde5950dSBarry Smith 
2225180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2235180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
2245180491cSLisandro Dalcin 
2254f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
226b3603a34SBarry Smith   if (opt) {
2270b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2283923b477SBarry Smith     PetscInt       howoften = 1;
229b3603a34SBarry Smith 
2300298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2316ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2324f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
233bdad233fSMatthew Knepley   }
2346ba87a44SLisandro Dalcin 
2354f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
236ef20d060SBarry Smith   if (opt) {
2370b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2383923b477SBarry Smith     PetscInt       howoften = 1;
239ef20d060SBarry Smith 
2400298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
2416ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2424f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
243ef20d060SBarry Smith   }
2446934998bSLisandro Dalcin 
2456934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2466934998bSLisandro Dalcin   if (opt) {
2476934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2486934998bSLisandro Dalcin     PetscInt       howoften = 1;
2496934998bSLisandro Dalcin 
2506934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2516ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2526934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2536934998bSLisandro Dalcin   }
254201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
255201da799SBarry Smith   if (opt) {
256201da799SBarry Smith     TSMonitorLGCtx ctx;
257201da799SBarry Smith     PetscInt       howoften = 1;
258201da799SBarry Smith 
2590298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2606ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
261201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
262201da799SBarry Smith   }
263201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
264201da799SBarry Smith   if (opt) {
265201da799SBarry Smith     TSMonitorLGCtx ctx;
266201da799SBarry Smith     PetscInt       howoften = 1;
267201da799SBarry Smith 
2680298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2696ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
270201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
271201da799SBarry Smith   }
2728189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2738189c53fSBarry Smith   if (opt) {
2748189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2758189c53fSBarry Smith     PetscInt          howoften = 1;
2768189c53fSBarry Smith 
2770298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
2786934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2798189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2808189c53fSBarry Smith   }
281ef20d060SBarry Smith   opt  = PETSC_FALSE;
2820dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
283a7cc72afSBarry Smith   if (opt) {
28483a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
28583a4ac43SBarry Smith     PetscInt         howoften = 1;
286a80ad3e0SBarry Smith 
2870298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2886934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
28983a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
290bdad233fSMatthew Knepley   }
291fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2929110b2e7SHong Zhang   ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr);
2939110b2e7SHong Zhang   if (opt) {
2949110b2e7SHong Zhang     TSMonitorDrawCtx ctx;
2959110b2e7SHong Zhang     PetscInt         howoften = 1;
2969110b2e7SHong Zhang 
2979110b2e7SHong Zhang     ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr);
2986934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2999110b2e7SHong Zhang     ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3009110b2e7SHong Zhang   }
3019110b2e7SHong Zhang   opt  = PETSC_FALSE;
3022d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
3032d5ee99bSBarry Smith   if (opt) {
3042d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
3052d5ee99bSBarry Smith     PetscReal        bounds[4];
3062d5ee99bSBarry Smith     PetscInt         n = 4;
3072d5ee99bSBarry Smith     PetscDraw        draw;
3086934998bSLisandro Dalcin     PetscDrawAxis    axis;
3092d5ee99bSBarry Smith 
3102d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
3112d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
3126934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
3132d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
3146934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
3156934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
3166934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
3172d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3182d5ee99bSBarry Smith   }
3192d5ee99bSBarry Smith   opt  = PETSC_FALSE;
3200dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
3213a471f94SBarry Smith   if (opt) {
32283a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
32383a4ac43SBarry Smith     PetscInt         howoften = 1;
3243a471f94SBarry Smith 
3250298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
3266934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
32783a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3283a471f94SBarry Smith   }
329fde5950dSBarry Smith 
330ed81e22dSJed Brown   opt  = PETSC_FALSE;
33191b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
332ed81e22dSJed Brown   if (flg) {
333ed81e22dSJed Brown     const char *ptr,*ptr2;
334ed81e22dSJed Brown     char       *filetemplate;
335ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
336ed81e22dSJed Brown     /* Do some cursory validation of the input. */
337ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
338ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
339ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
340ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
341ce94432eSBarry Smith       if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts");
342ed81e22dSJed Brown       if (ptr2) break;
343ed81e22dSJed Brown     }
344ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
345ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
346ed81e22dSJed Brown   }
347bdad233fSMatthew Knepley 
348d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
349d1212d36SBarry Smith   if (flg) {
350d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
351d1212d36SBarry Smith     int                  ray = 0;
352d1212d36SBarry Smith     DMDADirection        ddir;
353d1212d36SBarry Smith     DM                   da;
354d1212d36SBarry Smith     PetscMPIInt          rank;
355d1212d36SBarry Smith 
356ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
357d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
358d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
359ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
360d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
361d1212d36SBarry Smith 
362d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
363b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
364d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
365d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
366ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
367d1212d36SBarry Smith     if (!rank) {
368d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
369d1212d36SBarry Smith     }
37051b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
371d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
372d1212d36SBarry Smith   }
37351b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
37451b4a12fSMatthew G. Knepley   if (flg) {
37551b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
37651b4a12fSMatthew G. Knepley     int                 ray = 0;
37751b4a12fSMatthew G. Knepley     DMDADirection       ddir;
37851b4a12fSMatthew G. Knepley     DM                  da;
37951b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
380d1212d36SBarry Smith 
38151b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
38251b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
38351b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
38451b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
38551b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3861c3436cfSJed Brown 
38751b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
388b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
38951b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
39051b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
39151b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
39251b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
39351b4a12fSMatthew G. Knepley   }
394a7a1495cSBarry Smith 
395b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
396b3d3934dSBarry Smith   if (opt) {
397b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
398b3d3934dSBarry Smith 
399b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
400b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
401b3d3934dSBarry Smith   }
402b3d3934dSBarry Smith 
403847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
404847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
405847ff0e1SMatthew G. Knepley   if (flg) {
406847ff0e1SMatthew G. Knepley     DM   dm;
407847ff0e1SMatthew G. Knepley     DMTS tdm;
408847ff0e1SMatthew G. Knepley 
409847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
410847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
411847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
412847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
413847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
414847ff0e1SMatthew G. Knepley   }
415847ff0e1SMatthew G. Knepley 
416ee346746SBarry Smith   if (ts->adapt) {
417ee346746SBarry Smith     ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
418ee346746SBarry Smith   }
419d763cef2SBarry Smith 
420d763cef2SBarry Smith   /* Handle specific TS options */
421d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4226991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
423d763cef2SBarry Smith   }
424fbc52257SHong Zhang 
42568bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4264f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
42768bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
42868bece0bSHong Zhang   if (tflg) {
42968bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
43068bece0bSHong Zhang   }
4314f122a70SLisandro Dalcin   tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE;
43268bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr);
43368bece0bSHong Zhang   if (flg) {
43468bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
43568bece0bSHong Zhang     ts->adjoint_solve = tflg;
43668bece0bSHong Zhang   }
437d763cef2SBarry Smith 
438d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4390633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
440fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
441d763cef2SBarry Smith 
4424f122a70SLisandro Dalcin   if (ts->trajectory) {
4434f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
444d763cef2SBarry Smith   }
44568bece0bSHong Zhang 
4464f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4474f122a70SLisandro Dalcin   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);}
4484f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
449d763cef2SBarry Smith   PetscFunctionReturn(0);
450d763cef2SBarry Smith }
451d763cef2SBarry Smith 
452d763cef2SBarry Smith #undef __FUNCT__
453bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory"
454d2daff3dSHong Zhang /*@
455bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
456d2daff3dSHong Zhang 
457d2daff3dSHong Zhang    Collective on TS
458d2daff3dSHong Zhang 
459d2daff3dSHong Zhang    Input Parameters:
460bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
461bc952696SBarry Smith 
46268bece0bSHong Zhang Note: This routine should be called after all TS options have been set
463d2daff3dSHong Zhang 
464d2daff3dSHong Zhang    Level: intermediate
465d2daff3dSHong Zhang 
466bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve()
467d2daff3dSHong Zhang 
468bc952696SBarry Smith .keywords: TS, set, checkpoint,
469d2daff3dSHong Zhang @*/
470bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
471d2daff3dSHong Zhang {
472bc952696SBarry Smith   PetscErrorCode ierr;
473bc952696SBarry Smith 
474d2daff3dSHong Zhang   PetscFunctionBegin;
475d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
476bc952696SBarry Smith   if (!ts->trajectory) {
477bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
478972caf09SHong Zhang     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
479bc952696SBarry Smith   }
480d2daff3dSHong Zhang   PetscFunctionReturn(0);
481d2daff3dSHong Zhang }
482d2daff3dSHong Zhang 
483a7a1495cSBarry Smith #undef __FUNCT__
484a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian"
485a7a1495cSBarry Smith /*@
486a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
487a7a1495cSBarry Smith       set with TSSetRHSJacobian().
488a7a1495cSBarry Smith 
489a7a1495cSBarry Smith    Collective on TS and Vec
490a7a1495cSBarry Smith 
491a7a1495cSBarry Smith    Input Parameters:
492316643e7SJed Brown +  ts - the TS context
493a7a1495cSBarry Smith .  t - current timestep
4940910c330SBarry Smith -  U - input vector
495a7a1495cSBarry Smith 
496a7a1495cSBarry Smith    Output Parameters:
497a7a1495cSBarry Smith +  A - Jacobian matrix
498a7a1495cSBarry Smith .  B - optional preconditioning matrix
499a7a1495cSBarry Smith -  flag - flag indicating matrix structure
500a7a1495cSBarry Smith 
501a7a1495cSBarry Smith    Notes:
502a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
503a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
504a7a1495cSBarry Smith 
50594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
506a7a1495cSBarry Smith    flag parameter.
507a7a1495cSBarry Smith 
508a7a1495cSBarry Smith    Level: developer
509a7a1495cSBarry Smith 
510a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
511a7a1495cSBarry Smith 
51294b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
513a7a1495cSBarry Smith @*/
514d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
515a7a1495cSBarry Smith {
516dfbe8321SBarry Smith   PetscErrorCode ierr;
517270bf2e7SJed Brown   PetscObjectState Ustate;
51824989b8cSPeter Brune   DM             dm;
519942e3340SBarry Smith   DMTS           tsdm;
52024989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
52124989b8cSPeter Brune   void           *ctx;
52224989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
523b2df71adSDebojyoti Ghosh   TSRHSFunction  rhsfunction;
524a7a1495cSBarry Smith 
525a7a1495cSBarry Smith   PetscFunctionBegin;
5260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5270910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5280910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
52924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
530942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
53124989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5320298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
533b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
53459e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
535b2df71adSDebojyoti Ghosh   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
5360e4ef248SJed Brown     PetscFunctionReturn(0);
537f8ede8e7SJed Brown   }
538d90be118SSean Farley 
539ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
540d90be118SSean Farley 
541e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
54294ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
54394ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
54494ab13aaSBarry Smith     if (A != B) {
54594ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
54694ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
547e1244c69SJed Brown     }
548e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
549e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
550e1244c69SJed Brown   }
551e1244c69SJed Brown 
55224989b8cSPeter Brune   if (rhsjacobianfunc) {
5536cd88445SBarry Smith     PetscBool missing;
55494ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
555a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
556d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
557a7a1495cSBarry Smith     PetscStackPop;
55894ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
5596cd88445SBarry Smith     if (A) {
5606cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
5616cd88445SBarry Smith       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
5626cd88445SBarry Smith     }
5636cd88445SBarry Smith     if (B && B != A) {
5646cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
5656cd88445SBarry Smith       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
5666cd88445SBarry Smith     }
567ef66eb69SBarry Smith   } else {
56894ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
56994ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
570ef66eb69SBarry Smith   }
5710e4ef248SJed Brown   ts->rhsjacobian.time       = t;
5720910c330SBarry Smith   ts->rhsjacobian.X          = U;
57359e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
574a7a1495cSBarry Smith   PetscFunctionReturn(0);
575a7a1495cSBarry Smith }
576a7a1495cSBarry Smith 
5774a2ae208SSatish Balay #undef __FUNCT__
5784a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
579316643e7SJed Brown /*@
580d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
581d763cef2SBarry Smith 
582316643e7SJed Brown    Collective on TS and Vec
583316643e7SJed Brown 
584316643e7SJed Brown    Input Parameters:
585316643e7SJed Brown +  ts - the TS context
586316643e7SJed Brown .  t - current time
5870910c330SBarry Smith -  U - state vector
588316643e7SJed Brown 
589316643e7SJed Brown    Output Parameter:
590316643e7SJed Brown .  y - right hand side
591316643e7SJed Brown 
592316643e7SJed Brown    Note:
593316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
594316643e7SJed Brown    is used internally within the nonlinear solvers.
595316643e7SJed Brown 
596316643e7SJed Brown    Level: developer
597316643e7SJed Brown 
598316643e7SJed Brown .keywords: TS, compute
599316643e7SJed Brown 
600316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
601316643e7SJed Brown @*/
6020910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
603d763cef2SBarry Smith {
604dfbe8321SBarry Smith   PetscErrorCode ierr;
60524989b8cSPeter Brune   TSRHSFunction  rhsfunction;
60624989b8cSPeter Brune   TSIFunction    ifunction;
60724989b8cSPeter Brune   void           *ctx;
60824989b8cSPeter Brune   DM             dm;
60924989b8cSPeter Brune 
610d763cef2SBarry Smith   PetscFunctionBegin;
6110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6120910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6130700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
61424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
61524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6160298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
617d763cef2SBarry Smith 
618ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
619d763cef2SBarry Smith 
6200910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
62124989b8cSPeter Brune   if (rhsfunction) {
622d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6230910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
624d763cef2SBarry Smith     PetscStackPop;
625214bc6a2SJed Brown   } else {
626214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
627b2cd27e8SJed Brown   }
62844a41b28SSean Farley 
6290910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
630d763cef2SBarry Smith   PetscFunctionReturn(0);
631d763cef2SBarry Smith }
632d763cef2SBarry Smith 
6334a2ae208SSatish Balay #undef __FUNCT__
634ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
635ef20d060SBarry Smith /*@
636ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
637ef20d060SBarry Smith 
638ef20d060SBarry Smith    Collective on TS and Vec
639ef20d060SBarry Smith 
640ef20d060SBarry Smith    Input Parameters:
641ef20d060SBarry Smith +  ts - the TS context
642ef20d060SBarry Smith -  t - current time
643ef20d060SBarry Smith 
644ef20d060SBarry Smith    Output Parameter:
6450910c330SBarry Smith .  U - the solution
646ef20d060SBarry Smith 
647ef20d060SBarry Smith    Note:
648ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
649ef20d060SBarry Smith    is used internally within the nonlinear solvers.
650ef20d060SBarry Smith 
651ef20d060SBarry Smith    Level: developer
652ef20d060SBarry Smith 
653ef20d060SBarry Smith .keywords: TS, compute
654ef20d060SBarry Smith 
655abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
656ef20d060SBarry Smith @*/
6570910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
658ef20d060SBarry Smith {
659ef20d060SBarry Smith   PetscErrorCode     ierr;
660ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
661ef20d060SBarry Smith   void               *ctx;
662ef20d060SBarry Smith   DM                 dm;
663ef20d060SBarry Smith 
664ef20d060SBarry Smith   PetscFunctionBegin;
665ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6660910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
667ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
668ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
669ef20d060SBarry Smith 
670ef20d060SBarry Smith   if (solutionfunction) {
6719b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6720910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
673ef20d060SBarry Smith     PetscStackPop;
674ef20d060SBarry Smith   }
675ef20d060SBarry Smith   PetscFunctionReturn(0);
676ef20d060SBarry Smith }
6779b7cd975SBarry Smith #undef __FUNCT__
6789b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
6799b7cd975SBarry Smith /*@
6809b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6819b7cd975SBarry Smith 
6829b7cd975SBarry Smith    Collective on TS and Vec
6839b7cd975SBarry Smith 
6849b7cd975SBarry Smith    Input Parameters:
6859b7cd975SBarry Smith +  ts - the TS context
6869b7cd975SBarry Smith -  t - current time
6879b7cd975SBarry Smith 
6889b7cd975SBarry Smith    Output Parameter:
6899b7cd975SBarry Smith .  U - the function value
6909b7cd975SBarry Smith 
6919b7cd975SBarry Smith    Note:
6929b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
6939b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6949b7cd975SBarry Smith 
6959b7cd975SBarry Smith    Level: developer
6969b7cd975SBarry Smith 
6979b7cd975SBarry Smith .keywords: TS, compute
6989b7cd975SBarry Smith 
6999b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7009b7cd975SBarry Smith @*/
7019b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7029b7cd975SBarry Smith {
7039b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7049b7cd975SBarry Smith   void               *ctx;
7059b7cd975SBarry Smith   DM                 dm;
7069b7cd975SBarry Smith 
7079b7cd975SBarry Smith   PetscFunctionBegin;
7089b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7099b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7109b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7119b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7129b7cd975SBarry Smith 
7139b7cd975SBarry Smith   if (forcing) {
7149b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7159b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7169b7cd975SBarry Smith     PetscStackPop;
7179b7cd975SBarry Smith   }
7189b7cd975SBarry Smith   PetscFunctionReturn(0);
7199b7cd975SBarry Smith }
720ef20d060SBarry Smith 
721ef20d060SBarry Smith #undef __FUNCT__
722214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
723214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
724214bc6a2SJed Brown {
7252dd45cf8SJed Brown   Vec            F;
726214bc6a2SJed Brown   PetscErrorCode ierr;
727214bc6a2SJed Brown 
728214bc6a2SJed Brown   PetscFunctionBegin;
7290298fd71SBarry Smith   *Frhs = NULL;
7300298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
731214bc6a2SJed Brown   if (!ts->Frhs) {
7322dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
733214bc6a2SJed Brown   }
734214bc6a2SJed Brown   *Frhs = ts->Frhs;
735214bc6a2SJed Brown   PetscFunctionReturn(0);
736214bc6a2SJed Brown }
737214bc6a2SJed Brown 
738214bc6a2SJed Brown #undef __FUNCT__
739214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
740214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
741214bc6a2SJed Brown {
742214bc6a2SJed Brown   Mat            A,B;
7432dd45cf8SJed Brown   PetscErrorCode ierr;
744214bc6a2SJed Brown 
745214bc6a2SJed Brown   PetscFunctionBegin;
746c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
747c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
7480298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
749214bc6a2SJed Brown   if (Arhs) {
750214bc6a2SJed Brown     if (!ts->Arhs) {
751214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
752214bc6a2SJed Brown     }
753214bc6a2SJed Brown     *Arhs = ts->Arhs;
754214bc6a2SJed Brown   }
755214bc6a2SJed Brown   if (Brhs) {
756214bc6a2SJed Brown     if (!ts->Brhs) {
757bdb70873SJed Brown       if (A != B) {
758214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
759bdb70873SJed Brown       } else {
760bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
76151699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
762bdb70873SJed Brown       }
763214bc6a2SJed Brown     }
764214bc6a2SJed Brown     *Brhs = ts->Brhs;
765214bc6a2SJed Brown   }
766214bc6a2SJed Brown   PetscFunctionReturn(0);
767214bc6a2SJed Brown }
768214bc6a2SJed Brown 
769214bc6a2SJed Brown #undef __FUNCT__
770316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
771316643e7SJed Brown /*@
7720910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
773316643e7SJed Brown 
774316643e7SJed Brown    Collective on TS and Vec
775316643e7SJed Brown 
776316643e7SJed Brown    Input Parameters:
777316643e7SJed Brown +  ts - the TS context
778316643e7SJed Brown .  t - current time
7790910c330SBarry Smith .  U - state vector
7800910c330SBarry Smith .  Udot - time derivative of state vector
781214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
782316643e7SJed Brown 
783316643e7SJed Brown    Output Parameter:
784316643e7SJed Brown .  Y - right hand side
785316643e7SJed Brown 
786316643e7SJed Brown    Note:
787316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
788316643e7SJed Brown    is used internally within the nonlinear solvers.
789316643e7SJed Brown 
790316643e7SJed Brown    If the user did did not write their equations in implicit form, this
791316643e7SJed Brown    function recasts them in implicit form.
792316643e7SJed Brown 
793316643e7SJed Brown    Level: developer
794316643e7SJed Brown 
795316643e7SJed Brown .keywords: TS, compute
796316643e7SJed Brown 
797316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
798316643e7SJed Brown @*/
7990910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
800316643e7SJed Brown {
801316643e7SJed Brown   PetscErrorCode ierr;
80224989b8cSPeter Brune   TSIFunction    ifunction;
80324989b8cSPeter Brune   TSRHSFunction  rhsfunction;
80424989b8cSPeter Brune   void           *ctx;
80524989b8cSPeter Brune   DM             dm;
806316643e7SJed Brown 
807316643e7SJed Brown   PetscFunctionBegin;
8080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8090910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8100910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8110700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
812316643e7SJed Brown 
81324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
81424989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8150298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
81624989b8cSPeter Brune 
817ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
818d90be118SSean Farley 
8190910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
82024989b8cSPeter Brune   if (ifunction) {
821316643e7SJed Brown     PetscStackPush("TS user implicit function");
8220910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
823316643e7SJed Brown     PetscStackPop;
824214bc6a2SJed Brown   }
825214bc6a2SJed Brown   if (imex) {
82624989b8cSPeter Brune     if (!ifunction) {
8270910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8282dd45cf8SJed Brown     }
82924989b8cSPeter Brune   } else if (rhsfunction) {
83024989b8cSPeter Brune     if (ifunction) {
831214bc6a2SJed Brown       Vec Frhs;
832214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8330910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
834214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8352dd45cf8SJed Brown     } else {
8360910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8370910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
838316643e7SJed Brown     }
8394a6899ffSJed Brown   }
8400910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
841316643e7SJed Brown   PetscFunctionReturn(0);
842316643e7SJed Brown }
843316643e7SJed Brown 
844316643e7SJed Brown #undef __FUNCT__
845316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
846316643e7SJed Brown /*@
847316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
848316643e7SJed Brown 
849316643e7SJed Brown    Collective on TS and Vec
850316643e7SJed Brown 
851316643e7SJed Brown    Input
852316643e7SJed Brown       Input Parameters:
853316643e7SJed Brown +  ts - the TS context
854316643e7SJed Brown .  t - current timestep
8550910c330SBarry Smith .  U - state vector
8560910c330SBarry Smith .  Udot - time derivative of state vector
857214bc6a2SJed Brown .  shift - shift to apply, see note below
858214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
859316643e7SJed Brown 
860316643e7SJed Brown    Output Parameters:
861316643e7SJed Brown +  A - Jacobian matrix
862316643e7SJed Brown .  B - optional preconditioning matrix
863316643e7SJed Brown -  flag - flag indicating matrix structure
864316643e7SJed Brown 
865316643e7SJed Brown    Notes:
8660910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
867316643e7SJed Brown 
8680910c330SBarry Smith    dF/dU + shift*dF/dUdot
869316643e7SJed Brown 
870316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
871316643e7SJed Brown    is used internally within the nonlinear solvers.
872316643e7SJed Brown 
873316643e7SJed Brown    Level: developer
874316643e7SJed Brown 
875316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
876316643e7SJed Brown 
877316643e7SJed Brown .seealso:  TSSetIJacobian()
87863495f91SJed Brown @*/
879d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
880316643e7SJed Brown {
881316643e7SJed Brown   PetscErrorCode ierr;
88224989b8cSPeter Brune   TSIJacobian    ijacobian;
88324989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
88424989b8cSPeter Brune   DM             dm;
88524989b8cSPeter Brune   void           *ctx;
886316643e7SJed Brown 
887316643e7SJed Brown   PetscFunctionBegin;
8880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8890910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8900910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
891316643e7SJed Brown   PetscValidPointer(A,6);
89294ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
893316643e7SJed Brown   PetscValidPointer(B,7);
89494ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
89524989b8cSPeter Brune 
89624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
89724989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
8980298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
89924989b8cSPeter Brune 
900ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
901316643e7SJed Brown 
90294ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
90324989b8cSPeter Brune   if (ijacobian) {
9046cd88445SBarry Smith     PetscBool missing;
905316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
906d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
907316643e7SJed Brown     PetscStackPop;
9086cd88445SBarry Smith     if (A) {
9096cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9106cd88445SBarry Smith       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
9116cd88445SBarry Smith     }
9126cd88445SBarry Smith     if (B && B != A) {
9136cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9146cd88445SBarry Smith       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
9156cd88445SBarry Smith     }
9164a6899ffSJed Brown   }
917214bc6a2SJed Brown   if (imex) {
918b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
91994ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
92094ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
92194ab13aaSBarry Smith       if (A != B) {
92294ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
92394ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
924214bc6a2SJed Brown       }
925214bc6a2SJed Brown     }
926214bc6a2SJed Brown   } else {
927e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
928e1244c69SJed Brown     if (rhsjacobian) {
929214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
930d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
931e1244c69SJed Brown     }
93294ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
933e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
934e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
93594ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
93694ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
93794ab13aaSBarry Smith       if (A != B) {
93894ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
93994ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
940316643e7SJed Brown       }
941e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
942e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
943e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
94494ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
94594ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
94694ab13aaSBarry Smith         if (A != B) {
94794ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
94894ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
949214bc6a2SJed Brown         }
950316643e7SJed Brown       }
95194ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
95294ab13aaSBarry Smith       if (A != B) {
95394ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
954316643e7SJed Brown       }
955316643e7SJed Brown     }
956316643e7SJed Brown   }
95794ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
958316643e7SJed Brown   PetscFunctionReturn(0);
959316643e7SJed Brown }
960316643e7SJed Brown 
961316643e7SJed Brown #undef __FUNCT__
9624a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
963d763cef2SBarry Smith /*@C
964d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
965b5abc632SBarry Smith     where U_t = G(t,u).
966d763cef2SBarry Smith 
9673f9fe445SBarry Smith     Logically Collective on TS
968d763cef2SBarry Smith 
969d763cef2SBarry Smith     Input Parameters:
970d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
9710298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
972d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
973d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
9740298fd71SBarry Smith           function evaluation routine (may be NULL)
975d763cef2SBarry Smith 
976d763cef2SBarry Smith     Calling sequence of func:
97787828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
978d763cef2SBarry Smith 
979d763cef2SBarry Smith +   t - current timestep
980d763cef2SBarry Smith .   u - input vector
981d763cef2SBarry Smith .   F - function vector
982d763cef2SBarry Smith -   ctx - [optional] user-defined function context
983d763cef2SBarry Smith 
984d763cef2SBarry Smith     Level: beginner
985d763cef2SBarry Smith 
9862bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
9872bbac0d3SBarry Smith 
988d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
989d763cef2SBarry Smith 
990ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
991d763cef2SBarry Smith @*/
992089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
993d763cef2SBarry Smith {
994089b2837SJed Brown   PetscErrorCode ierr;
995089b2837SJed Brown   SNES           snes;
9960298fd71SBarry Smith   Vec            ralloc = NULL;
99724989b8cSPeter Brune   DM             dm;
998d763cef2SBarry Smith 
999089b2837SJed Brown   PetscFunctionBegin;
10000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1001ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
100224989b8cSPeter Brune 
100324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
100424989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1005089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1006e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1007e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1008e856ceecSJed Brown     r = ralloc;
1009e856ceecSJed Brown   }
1010089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1011e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1012d763cef2SBarry Smith   PetscFunctionReturn(0);
1013d763cef2SBarry Smith }
1014d763cef2SBarry Smith 
10154a2ae208SSatish Balay #undef __FUNCT__
1016ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
1017ef20d060SBarry Smith /*@C
1018abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1019ef20d060SBarry Smith 
1020ef20d060SBarry Smith     Logically Collective on TS
1021ef20d060SBarry Smith 
1022ef20d060SBarry Smith     Input Parameters:
1023ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1024ef20d060SBarry Smith .   f - routine for evaluating the solution
1025ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10260298fd71SBarry Smith           function evaluation routine (may be NULL)
1027ef20d060SBarry Smith 
1028ef20d060SBarry Smith     Calling sequence of func:
1029ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1030ef20d060SBarry Smith 
1031ef20d060SBarry Smith +   t - current timestep
1032ef20d060SBarry Smith .   u - output vector
1033ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1034ef20d060SBarry Smith 
1035abd5a294SJed Brown     Notes:
1036abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1037abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1038abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1039abd5a294SJed Brown 
10404f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1041abd5a294SJed Brown 
1042ef20d060SBarry Smith     Level: beginner
1043ef20d060SBarry Smith 
1044ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1045ef20d060SBarry Smith 
10469b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
1047ef20d060SBarry Smith @*/
1048ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1049ef20d060SBarry Smith {
1050ef20d060SBarry Smith   PetscErrorCode ierr;
1051ef20d060SBarry Smith   DM             dm;
1052ef20d060SBarry Smith 
1053ef20d060SBarry Smith   PetscFunctionBegin;
1054ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1055ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1056ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1057ef20d060SBarry Smith   PetscFunctionReturn(0);
1058ef20d060SBarry Smith }
1059ef20d060SBarry Smith 
1060ef20d060SBarry Smith #undef __FUNCT__
10619b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
10629b7cd975SBarry Smith /*@C
10639b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
10649b7cd975SBarry Smith 
10659b7cd975SBarry Smith     Logically Collective on TS
10669b7cd975SBarry Smith 
10679b7cd975SBarry Smith     Input Parameters:
10689b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
10699b7cd975SBarry Smith .   f - routine for evaluating the forcing function
10709b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
10710298fd71SBarry Smith           function evaluation routine (may be NULL)
10729b7cd975SBarry Smith 
10739b7cd975SBarry Smith     Calling sequence of func:
10749b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
10759b7cd975SBarry Smith 
10769b7cd975SBarry Smith +   t - current timestep
10779b7cd975SBarry Smith .   u - output vector
10789b7cd975SBarry Smith -   ctx - [optional] user-defined function context
10799b7cd975SBarry Smith 
10809b7cd975SBarry Smith     Notes:
10819b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
10829b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
10839b7cd975SBarry Smith 
10849b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
10859b7cd975SBarry Smith 
10869b7cd975SBarry Smith     Level: beginner
10879b7cd975SBarry Smith 
10889b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
10899b7cd975SBarry Smith 
10909b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
10919b7cd975SBarry Smith @*/
1092d56366bfSLisandro Dalcin PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction f,void *ctx)
10939b7cd975SBarry Smith {
10949b7cd975SBarry Smith   PetscErrorCode ierr;
10959b7cd975SBarry Smith   DM             dm;
10969b7cd975SBarry Smith 
10979b7cd975SBarry Smith   PetscFunctionBegin;
10989b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10999b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
11009b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
11019b7cd975SBarry Smith   PetscFunctionReturn(0);
11029b7cd975SBarry Smith }
11039b7cd975SBarry Smith 
11049b7cd975SBarry Smith #undef __FUNCT__
11054a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
1106d763cef2SBarry Smith /*@C
1107f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1108b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1109d763cef2SBarry Smith 
11103f9fe445SBarry Smith    Logically Collective on TS
1111d763cef2SBarry Smith 
1112d763cef2SBarry Smith    Input Parameters:
1113d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1114e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1115e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1116d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1117d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11180298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1119d763cef2SBarry Smith 
1120f7ab8db6SBarry Smith    Calling sequence of f:
1121ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1122d763cef2SBarry Smith 
1123d763cef2SBarry Smith +  t - current timestep
1124d763cef2SBarry Smith .  u - input vector
1125e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1126e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1127d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1128d763cef2SBarry Smith 
11296cd88445SBarry Smith    Notes:
11306cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11316cd88445SBarry Smith 
11326cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1133ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1134d763cef2SBarry Smith 
1135d763cef2SBarry Smith    Level: beginner
1136d763cef2SBarry Smith 
1137d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1138d763cef2SBarry Smith 
1139ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1140d763cef2SBarry Smith 
1141d763cef2SBarry Smith @*/
1142e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1143d763cef2SBarry Smith {
1144277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1145089b2837SJed Brown   SNES           snes;
114624989b8cSPeter Brune   DM             dm;
114724989b8cSPeter Brune   TSIJacobian    ijacobian;
1148277b19d0SLisandro Dalcin 
1149d763cef2SBarry Smith   PetscFunctionBegin;
11500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1151e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1152e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1153e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1154e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1155d763cef2SBarry Smith 
115624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
115724989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1158e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1159e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1160e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1161e1244c69SJed Brown   }
11620298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1163089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11645f659677SPeter Brune   if (!ijacobian) {
1165e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
11660e4ef248SJed Brown   }
1167e5d3d808SBarry Smith   if (Amat) {
1168e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
11690e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1170e5d3d808SBarry Smith     ts->Arhs = Amat;
11710e4ef248SJed Brown   }
1172e5d3d808SBarry Smith   if (Pmat) {
1173e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
11740e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1175e5d3d808SBarry Smith     ts->Brhs = Pmat;
11760e4ef248SJed Brown   }
1177d763cef2SBarry Smith   PetscFunctionReturn(0);
1178d763cef2SBarry Smith }
1179d763cef2SBarry Smith 
1180316643e7SJed Brown 
1181316643e7SJed Brown #undef __FUNCT__
1182316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1183316643e7SJed Brown /*@C
1184b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1185316643e7SJed Brown 
11863f9fe445SBarry Smith    Logically Collective on TS
1187316643e7SJed Brown 
1188316643e7SJed Brown    Input Parameters:
1189316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
11900298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1191316643e7SJed Brown .  f   - the function evaluation routine
11920298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1193316643e7SJed Brown 
1194316643e7SJed Brown    Calling sequence of f:
1195316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1196316643e7SJed Brown 
1197316643e7SJed Brown +  t   - time at step/stage being solved
1198316643e7SJed Brown .  u   - state vector
1199316643e7SJed Brown .  u_t - time derivative of state vector
1200316643e7SJed Brown .  F   - function vector
1201316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1202316643e7SJed Brown 
1203316643e7SJed Brown    Important:
12042bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1205316643e7SJed Brown 
1206316643e7SJed Brown    Level: beginner
1207316643e7SJed Brown 
1208316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1209316643e7SJed Brown 
1210d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1211316643e7SJed Brown @*/
121251699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1213316643e7SJed Brown {
1214089b2837SJed Brown   PetscErrorCode ierr;
1215089b2837SJed Brown   SNES           snes;
121651699248SLisandro Dalcin   Vec            ralloc = NULL;
121724989b8cSPeter Brune   DM             dm;
1218316643e7SJed Brown 
1219316643e7SJed Brown   PetscFunctionBegin;
12200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
122151699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
122224989b8cSPeter Brune 
122324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
122424989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
122524989b8cSPeter Brune 
1226089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
122751699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
122851699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
122951699248SLisandro Dalcin     r  = ralloc;
1230e856ceecSJed Brown   }
123151699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
123251699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1233089b2837SJed Brown   PetscFunctionReturn(0);
1234089b2837SJed Brown }
1235089b2837SJed Brown 
1236089b2837SJed Brown #undef __FUNCT__
1237089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1238089b2837SJed Brown /*@C
1239089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1240089b2837SJed Brown 
1241089b2837SJed Brown    Not Collective
1242089b2837SJed Brown 
1243089b2837SJed Brown    Input Parameter:
1244089b2837SJed Brown .  ts - the TS context
1245089b2837SJed Brown 
1246089b2837SJed Brown    Output Parameter:
12470298fd71SBarry Smith +  r - vector to hold residual (or NULL)
12480298fd71SBarry Smith .  func - the function to compute residual (or NULL)
12490298fd71SBarry Smith -  ctx - the function context (or NULL)
1250089b2837SJed Brown 
1251089b2837SJed Brown    Level: advanced
1252089b2837SJed Brown 
1253089b2837SJed Brown .keywords: TS, nonlinear, get, function
1254089b2837SJed Brown 
1255089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1256089b2837SJed Brown @*/
1257089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1258089b2837SJed Brown {
1259089b2837SJed Brown   PetscErrorCode ierr;
1260089b2837SJed Brown   SNES           snes;
126124989b8cSPeter Brune   DM             dm;
1262089b2837SJed Brown 
1263089b2837SJed Brown   PetscFunctionBegin;
1264089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1265089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12660298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
126724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
126824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1269089b2837SJed Brown   PetscFunctionReturn(0);
1270089b2837SJed Brown }
1271089b2837SJed Brown 
1272089b2837SJed Brown #undef __FUNCT__
1273089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1274089b2837SJed Brown /*@C
1275089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1276089b2837SJed Brown 
1277089b2837SJed Brown    Not Collective
1278089b2837SJed Brown 
1279089b2837SJed Brown    Input Parameter:
1280089b2837SJed Brown .  ts - the TS context
1281089b2837SJed Brown 
1282089b2837SJed Brown    Output Parameter:
12830298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
12840298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
12850298fd71SBarry Smith -  ctx - the function context (or NULL)
1286089b2837SJed Brown 
1287089b2837SJed Brown    Level: advanced
1288089b2837SJed Brown 
1289089b2837SJed Brown .keywords: TS, nonlinear, get, function
1290089b2837SJed Brown 
12912bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1292089b2837SJed Brown @*/
1293089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1294089b2837SJed Brown {
1295089b2837SJed Brown   PetscErrorCode ierr;
1296089b2837SJed Brown   SNES           snes;
129724989b8cSPeter Brune   DM             dm;
1298089b2837SJed Brown 
1299089b2837SJed Brown   PetscFunctionBegin;
1300089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1301089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13020298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
130324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
130424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1305316643e7SJed Brown   PetscFunctionReturn(0);
1306316643e7SJed Brown }
1307316643e7SJed Brown 
1308316643e7SJed Brown #undef __FUNCT__
1309316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1310316643e7SJed Brown /*@C
1311a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1312ae8867d6SBarry Smith         provided with TSSetIFunction().
1313316643e7SJed Brown 
13143f9fe445SBarry Smith    Logically Collective on TS
1315316643e7SJed Brown 
1316316643e7SJed Brown    Input Parameters:
1317316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1318e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1319e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1320316643e7SJed Brown .  f   - the Jacobian evaluation routine
13210298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1322316643e7SJed Brown 
1323316643e7SJed Brown    Calling sequence of f:
1324ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1325316643e7SJed Brown 
1326316643e7SJed Brown +  t    - time at step/stage being solved
13271b4a444bSJed Brown .  U    - state vector
13281b4a444bSJed Brown .  U_t  - time derivative of state vector
1329316643e7SJed Brown .  a    - shift
1330e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1331e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1332316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1333316643e7SJed Brown 
1334316643e7SJed Brown    Notes:
1335e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1336316643e7SJed Brown 
1337895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1338895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1339895c21f2SBarry Smith 
1340a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1341b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1342a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1343a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1344a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1345a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1346a4f0a591SBarry Smith 
13476cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13486cd88445SBarry Smith 
13496cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1350ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1351ca5f011dSBarry Smith 
1352316643e7SJed Brown    Level: beginner
1353316643e7SJed Brown 
1354316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1355316643e7SJed Brown 
1356ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1357316643e7SJed Brown 
1358316643e7SJed Brown @*/
1359e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1360316643e7SJed Brown {
1361316643e7SJed Brown   PetscErrorCode ierr;
1362089b2837SJed Brown   SNES           snes;
136324989b8cSPeter Brune   DM             dm;
1364316643e7SJed Brown 
1365316643e7SJed Brown   PetscFunctionBegin;
13660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1367e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1368e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1369e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1370e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
137124989b8cSPeter Brune 
137224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
137324989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
137424989b8cSPeter Brune 
1375089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1376e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1377316643e7SJed Brown   PetscFunctionReturn(0);
1378316643e7SJed Brown }
1379316643e7SJed Brown 
13804a2ae208SSatish Balay #undef __FUNCT__
1381e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1382e1244c69SJed Brown /*@
1383e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1384e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1385e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1386e1244c69SJed Brown    not been changed by the TS.
1387e1244c69SJed Brown 
1388e1244c69SJed Brown    Logically Collective
1389e1244c69SJed Brown 
1390e1244c69SJed Brown    Input Arguments:
1391e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1392e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1393e1244c69SJed Brown 
1394e1244c69SJed Brown    Level: intermediate
1395e1244c69SJed Brown 
1396e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1397e1244c69SJed Brown @*/
1398e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1399e1244c69SJed Brown {
1400e1244c69SJed Brown   PetscFunctionBegin;
1401e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1402e1244c69SJed Brown   PetscFunctionReturn(0);
1403e1244c69SJed Brown }
1404e1244c69SJed Brown 
1405e1244c69SJed Brown #undef __FUNCT__
1406efe9872eSLisandro Dalcin #define __FUNCT__ "TSSetI2Function"
1407efe9872eSLisandro Dalcin /*@C
1408efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1409efe9872eSLisandro Dalcin 
1410efe9872eSLisandro Dalcin    Logically Collective on TS
1411efe9872eSLisandro Dalcin 
1412efe9872eSLisandro Dalcin    Input Parameters:
1413efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1414efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1415efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1416efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1417efe9872eSLisandro Dalcin 
1418efe9872eSLisandro Dalcin    Calling sequence of fun:
1419efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1420efe9872eSLisandro Dalcin 
1421efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1422efe9872eSLisandro Dalcin .  U    - state vector
1423efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1424efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1425efe9872eSLisandro Dalcin .  F    - function vector
1426efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1427efe9872eSLisandro Dalcin 
1428efe9872eSLisandro Dalcin    Level: beginner
1429efe9872eSLisandro Dalcin 
1430efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1431efe9872eSLisandro Dalcin 
1432efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1433efe9872eSLisandro Dalcin @*/
1434efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1435efe9872eSLisandro Dalcin {
1436efe9872eSLisandro Dalcin   DM             dm;
1437efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1438efe9872eSLisandro Dalcin 
1439efe9872eSLisandro Dalcin   PetscFunctionBegin;
1440efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1441efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1442efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1443efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1444efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1445efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1446efe9872eSLisandro Dalcin }
1447efe9872eSLisandro Dalcin 
1448efe9872eSLisandro Dalcin #undef __FUNCT__
1449efe9872eSLisandro Dalcin #define __FUNCT__ "TSGetI2Function"
1450efe9872eSLisandro Dalcin /*@C
1451efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1452efe9872eSLisandro Dalcin 
1453efe9872eSLisandro Dalcin   Not Collective
1454efe9872eSLisandro Dalcin 
1455efe9872eSLisandro Dalcin   Input Parameter:
1456efe9872eSLisandro Dalcin . ts - the TS context
1457efe9872eSLisandro Dalcin 
1458efe9872eSLisandro Dalcin   Output Parameter:
1459efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1460efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1461efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1462efe9872eSLisandro Dalcin 
1463efe9872eSLisandro Dalcin   Level: advanced
1464efe9872eSLisandro Dalcin 
1465efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1466efe9872eSLisandro Dalcin 
1467efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1468efe9872eSLisandro Dalcin @*/
1469efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1470efe9872eSLisandro Dalcin {
1471efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1472efe9872eSLisandro Dalcin   SNES           snes;
1473efe9872eSLisandro Dalcin   DM             dm;
1474efe9872eSLisandro Dalcin 
1475efe9872eSLisandro Dalcin   PetscFunctionBegin;
1476efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1477efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1478efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1479efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1480efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1481efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1482efe9872eSLisandro Dalcin }
1483efe9872eSLisandro Dalcin 
1484efe9872eSLisandro Dalcin #undef __FUNCT__
1485efe9872eSLisandro Dalcin #define __FUNCT__ "TSSetI2Jacobian"
1486efe9872eSLisandro Dalcin /*@C
1487bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1488efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1489efe9872eSLisandro Dalcin 
1490efe9872eSLisandro Dalcin    Logically Collective on TS
1491efe9872eSLisandro Dalcin 
1492efe9872eSLisandro Dalcin    Input Parameters:
1493efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1494efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1495efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1496efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1497efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1498efe9872eSLisandro Dalcin 
1499efe9872eSLisandro Dalcin    Calling sequence of jac:
1500bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1501efe9872eSLisandro Dalcin 
1502efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1503efe9872eSLisandro Dalcin .  U    - state vector
1504efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1505efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1506efe9872eSLisandro Dalcin .  v    - shift for U_t
1507efe9872eSLisandro Dalcin .  a    - shift for U_tt
1508efe9872eSLisandro 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
1509efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1510efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1511efe9872eSLisandro Dalcin 
1512efe9872eSLisandro Dalcin    Notes:
1513efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1514efe9872eSLisandro Dalcin 
1515efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1516efe9872eSLisandro 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.
1517efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1518bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1519efe9872eSLisandro Dalcin 
1520efe9872eSLisandro Dalcin    Level: beginner
1521efe9872eSLisandro Dalcin 
1522efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1523efe9872eSLisandro Dalcin 
1524efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1525efe9872eSLisandro Dalcin @*/
1526efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1527efe9872eSLisandro Dalcin {
1528efe9872eSLisandro Dalcin   DM             dm;
1529efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1530efe9872eSLisandro Dalcin 
1531efe9872eSLisandro Dalcin   PetscFunctionBegin;
1532efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1533efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1534efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1535efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1536efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1537efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1538efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1539efe9872eSLisandro Dalcin }
1540efe9872eSLisandro Dalcin 
1541efe9872eSLisandro Dalcin #undef __FUNCT__
1542efe9872eSLisandro Dalcin #define __FUNCT__ "TSGetI2Jacobian"
1543efe9872eSLisandro Dalcin /*@C
1544efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1545efe9872eSLisandro Dalcin 
1546efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1547efe9872eSLisandro Dalcin 
1548efe9872eSLisandro Dalcin   Input Parameter:
1549efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1550efe9872eSLisandro Dalcin 
1551efe9872eSLisandro Dalcin   Output Parameters:
1552efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1553efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1554efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1555efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1556efe9872eSLisandro Dalcin 
1557efe9872eSLisandro Dalcin   Notes: You can pass in NULL for any return argument you do not need.
1558efe9872eSLisandro Dalcin 
1559efe9872eSLisandro Dalcin   Level: advanced
1560efe9872eSLisandro Dalcin 
1561efe9872eSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
1562efe9872eSLisandro Dalcin 
1563efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1564efe9872eSLisandro Dalcin @*/
1565efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1566efe9872eSLisandro Dalcin {
1567efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1568efe9872eSLisandro Dalcin   SNES           snes;
1569efe9872eSLisandro Dalcin   DM             dm;
1570efe9872eSLisandro Dalcin 
1571efe9872eSLisandro Dalcin   PetscFunctionBegin;
1572efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1573efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1574efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1575efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1576efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1577efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1578efe9872eSLisandro Dalcin }
1579efe9872eSLisandro Dalcin 
1580efe9872eSLisandro Dalcin #undef __FUNCT__
1581efe9872eSLisandro Dalcin #define __FUNCT__ "TSComputeI2Function"
1582efe9872eSLisandro Dalcin /*@
1583efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1584efe9872eSLisandro Dalcin 
1585efe9872eSLisandro Dalcin   Collective on TS and Vec
1586efe9872eSLisandro Dalcin 
1587efe9872eSLisandro Dalcin   Input Parameters:
1588efe9872eSLisandro Dalcin + ts - the TS context
1589efe9872eSLisandro Dalcin . t - current time
1590efe9872eSLisandro Dalcin . U - state vector
1591efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1592efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1593efe9872eSLisandro Dalcin 
1594efe9872eSLisandro Dalcin   Output Parameter:
1595efe9872eSLisandro Dalcin . F - the residual vector
1596efe9872eSLisandro Dalcin 
1597efe9872eSLisandro Dalcin   Note:
1598efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1599efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1600efe9872eSLisandro Dalcin 
1601efe9872eSLisandro Dalcin   Level: developer
1602efe9872eSLisandro Dalcin 
1603efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1604efe9872eSLisandro Dalcin 
1605efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1606efe9872eSLisandro Dalcin @*/
1607efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1608efe9872eSLisandro Dalcin {
1609efe9872eSLisandro Dalcin   DM             dm;
1610efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1611efe9872eSLisandro Dalcin   void           *ctx;
1612efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1613efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1614efe9872eSLisandro Dalcin 
1615efe9872eSLisandro Dalcin   PetscFunctionBegin;
1616efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1617efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1618efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1619efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1620efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1621efe9872eSLisandro Dalcin 
1622efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1623efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1624efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1625efe9872eSLisandro Dalcin 
1626efe9872eSLisandro Dalcin   if (!I2Function) {
1627efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1628efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1629efe9872eSLisandro Dalcin   }
1630efe9872eSLisandro Dalcin 
1631efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1632efe9872eSLisandro Dalcin 
1633efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1634efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1635efe9872eSLisandro Dalcin   PetscStackPop;
1636efe9872eSLisandro Dalcin 
1637efe9872eSLisandro Dalcin   if (rhsfunction) {
1638efe9872eSLisandro Dalcin     Vec Frhs;
1639efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1640efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1641efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1642efe9872eSLisandro Dalcin   }
1643efe9872eSLisandro Dalcin 
1644efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1645efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1646efe9872eSLisandro Dalcin }
1647efe9872eSLisandro Dalcin 
1648efe9872eSLisandro Dalcin #undef __FUNCT__
1649efe9872eSLisandro Dalcin #define __FUNCT__ "TSComputeI2Jacobian"
1650efe9872eSLisandro Dalcin /*@
1651efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1652efe9872eSLisandro Dalcin 
1653efe9872eSLisandro Dalcin   Collective on TS and Vec
1654efe9872eSLisandro Dalcin 
1655efe9872eSLisandro Dalcin   Input Parameters:
1656efe9872eSLisandro Dalcin + ts - the TS context
1657efe9872eSLisandro Dalcin . t - current timestep
1658efe9872eSLisandro Dalcin . U - state vector
1659efe9872eSLisandro Dalcin . V - time derivative of state vector
1660efe9872eSLisandro Dalcin . A - second time derivative of state vector
1661efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1662efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1663efe9872eSLisandro Dalcin 
1664efe9872eSLisandro Dalcin   Output Parameters:
1665efe9872eSLisandro Dalcin + J - Jacobian matrix
1666efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1667efe9872eSLisandro Dalcin 
1668efe9872eSLisandro Dalcin   Notes:
1669efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1670efe9872eSLisandro Dalcin 
1671efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1672efe9872eSLisandro Dalcin 
1673efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1674efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1675efe9872eSLisandro Dalcin 
1676efe9872eSLisandro Dalcin   Level: developer
1677efe9872eSLisandro Dalcin 
1678efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1679efe9872eSLisandro Dalcin 
1680efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1681efe9872eSLisandro Dalcin @*/
1682efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1683efe9872eSLisandro Dalcin {
1684efe9872eSLisandro Dalcin   DM             dm;
1685efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1686efe9872eSLisandro Dalcin   void           *ctx;
1687efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1688efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1689efe9872eSLisandro Dalcin 
1690efe9872eSLisandro Dalcin   PetscFunctionBegin;
1691efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1692efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1693efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1694efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1695efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1696efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1697efe9872eSLisandro Dalcin 
1698efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1699efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1700efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1701efe9872eSLisandro Dalcin 
1702efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1703efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1704efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1705efe9872eSLisandro Dalcin   }
1706efe9872eSLisandro Dalcin 
1707efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1708efe9872eSLisandro Dalcin 
1709efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1710efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1711efe9872eSLisandro Dalcin   PetscStackPop;
1712efe9872eSLisandro Dalcin 
1713efe9872eSLisandro Dalcin   if (rhsjacobian) {
1714efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1715efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1716efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1717efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1718efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1719efe9872eSLisandro Dalcin   }
1720efe9872eSLisandro Dalcin 
1721efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1722efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1723efe9872eSLisandro Dalcin }
1724efe9872eSLisandro Dalcin 
1725efe9872eSLisandro Dalcin #undef __FUNCT__
1726efe9872eSLisandro Dalcin #define __FUNCT__ "TS2SetSolution"
1727efe9872eSLisandro Dalcin /*@
1728efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1729efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1730efe9872eSLisandro Dalcin 
1731efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1732efe9872eSLisandro Dalcin 
1733efe9872eSLisandro Dalcin    Input Parameters:
1734efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1735efe9872eSLisandro Dalcin .  u - the solution vector
1736efe9872eSLisandro Dalcin -  v - the time derivative vector
1737efe9872eSLisandro Dalcin 
1738efe9872eSLisandro Dalcin    Level: beginner
1739efe9872eSLisandro Dalcin 
1740efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1741efe9872eSLisandro Dalcin @*/
1742efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1743efe9872eSLisandro Dalcin {
1744efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1745efe9872eSLisandro Dalcin 
1746efe9872eSLisandro Dalcin   PetscFunctionBegin;
1747efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1748efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1749efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1750efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1751efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1752efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1753efe9872eSLisandro Dalcin   ts->vec_dot = v;
1754efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1755efe9872eSLisandro Dalcin }
1756efe9872eSLisandro Dalcin 
1757efe9872eSLisandro Dalcin #undef __FUNCT__
1758efe9872eSLisandro Dalcin #define __FUNCT__ "TS2GetSolution"
1759efe9872eSLisandro Dalcin /*@
1760efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1761efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1762efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1763efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1764efe9872eSLisandro Dalcin 
1765efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1766efe9872eSLisandro Dalcin 
1767efe9872eSLisandro Dalcin    Input Parameter:
1768efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1769efe9872eSLisandro Dalcin 
1770efe9872eSLisandro Dalcin    Output Parameter:
1771efe9872eSLisandro Dalcin +  u - the vector containing the solution
1772efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1773efe9872eSLisandro Dalcin 
1774efe9872eSLisandro Dalcin    Level: intermediate
1775efe9872eSLisandro Dalcin 
1776efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1777efe9872eSLisandro Dalcin 
1778efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1779efe9872eSLisandro Dalcin @*/
1780efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1781efe9872eSLisandro Dalcin {
1782efe9872eSLisandro Dalcin   PetscFunctionBegin;
1783efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1784efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1785efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1786efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1787efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1788efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1789efe9872eSLisandro Dalcin }
1790efe9872eSLisandro Dalcin 
1791efe9872eSLisandro Dalcin #undef __FUNCT__
179255849f57SBarry Smith #define __FUNCT__ "TSLoad"
179355849f57SBarry Smith /*@C
179455849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
179555849f57SBarry Smith 
179655849f57SBarry Smith   Collective on PetscViewer
179755849f57SBarry Smith 
179855849f57SBarry Smith   Input Parameters:
179955849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
180055849f57SBarry Smith            some related function before a call to TSLoad().
180155849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
180255849f57SBarry Smith 
180355849f57SBarry Smith    Level: intermediate
180455849f57SBarry Smith 
180555849f57SBarry Smith   Notes:
180655849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
180755849f57SBarry Smith 
180855849f57SBarry Smith   Notes for advanced users:
180955849f57SBarry Smith   Most users should not need to know the details of the binary storage
181055849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
181155849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
181255849f57SBarry Smith   format is
181355849f57SBarry Smith .vb
181455849f57SBarry Smith      has not yet been determined
181555849f57SBarry Smith .ve
181655849f57SBarry Smith 
181755849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
181855849f57SBarry Smith @*/
1819f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
182055849f57SBarry Smith {
182155849f57SBarry Smith   PetscErrorCode ierr;
182255849f57SBarry Smith   PetscBool      isbinary;
182355849f57SBarry Smith   PetscInt       classid;
182455849f57SBarry Smith   char           type[256];
18252d53ad75SBarry Smith   DMTS           sdm;
1826ad6bc421SBarry Smith   DM             dm;
182755849f57SBarry Smith 
182855849f57SBarry Smith   PetscFunctionBegin;
1829f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
183055849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
183155849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
183255849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
183355849f57SBarry Smith 
1834060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1835ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1836060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1837f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1838f2c2a1b9SBarry Smith   if (ts->ops->load) {
1839f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1840f2c2a1b9SBarry Smith   }
1841ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1842ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1843ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1844f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1845f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
18462d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18472d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
184855849f57SBarry Smith   PetscFunctionReturn(0);
184955849f57SBarry Smith }
185055849f57SBarry Smith 
18519804daf3SBarry Smith #include <petscdraw.h>
1852e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1853e04113cfSBarry Smith #include <petscviewersaws.h>
1854f05ece33SBarry Smith #endif
185555849f57SBarry Smith #undef __FUNCT__
18564a2ae208SSatish Balay #define __FUNCT__ "TSView"
18577e2c5f70SBarry Smith /*@C
1858d763cef2SBarry Smith     TSView - Prints the TS data structure.
1859d763cef2SBarry Smith 
18604c49b128SBarry Smith     Collective on TS
1861d763cef2SBarry Smith 
1862d763cef2SBarry Smith     Input Parameters:
1863d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1864d763cef2SBarry Smith -   viewer - visualization context
1865d763cef2SBarry Smith 
1866d763cef2SBarry Smith     Options Database Key:
1867d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1868d763cef2SBarry Smith 
1869d763cef2SBarry Smith     Notes:
1870d763cef2SBarry Smith     The available visualization contexts include
1871b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1872b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1873d763cef2SBarry Smith          output where only the first processor opens
1874d763cef2SBarry Smith          the file.  All other processors send their
1875d763cef2SBarry Smith          data to the first processor to print.
1876d763cef2SBarry Smith 
1877d763cef2SBarry Smith     The user can open an alternative visualization context with
1878b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1879d763cef2SBarry Smith 
1880d763cef2SBarry Smith     Level: beginner
1881d763cef2SBarry Smith 
1882d763cef2SBarry Smith .keywords: TS, timestep, view
1883d763cef2SBarry Smith 
1884b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1885d763cef2SBarry Smith @*/
18867087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1887d763cef2SBarry Smith {
1888dfbe8321SBarry Smith   PetscErrorCode ierr;
188919fd82e9SBarry Smith   TSType         type;
18902b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
18912d53ad75SBarry Smith   DMTS           sdm;
1892e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1893536b137fSBarry Smith   PetscBool      issaws;
1894f05ece33SBarry Smith #endif
1895d763cef2SBarry Smith 
1896d763cef2SBarry Smith   PetscFunctionBegin;
18970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18983050cee2SBarry Smith   if (!viewer) {
1899ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
19003050cee2SBarry Smith   }
19010700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1902c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1903fd16b177SBarry Smith 
1904251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1905251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
190655849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19072b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1908e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1909536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1910f05ece33SBarry Smith #endif
191132077d6dSBarry Smith   if (iascii) {
1912dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
191377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
19147c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1915d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
19165ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1917c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1918d763cef2SBarry Smith     }
19195ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1920193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1921a0af407cSBarry Smith     if (ts->vrtol) {
1922a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1923a0af407cSBarry Smith     } else {
1924a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
1925a0af407cSBarry Smith     }
1926a0af407cSBarry Smith     if (ts->vatol) {
1927a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
1928a0af407cSBarry Smith     } else {
1929a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
1930a0af407cSBarry Smith     }
19312d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19322d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1933d52bd9f3SBarry Smith     if (ts->ops->view) {
1934d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1935d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1936d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1937d52bd9f3SBarry Smith     }
19380f5bd95cSBarry Smith   } else if (isstring) {
1939a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1940b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
194155849f57SBarry Smith   } else if (isbinary) {
194255849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
194355849f57SBarry Smith     MPI_Comm    comm;
194455849f57SBarry Smith     PetscMPIInt rank;
194555849f57SBarry Smith     char        type[256];
194655849f57SBarry Smith 
194755849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
194855849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
194955849f57SBarry Smith     if (!rank) {
195055849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
195155849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
195255849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
195355849f57SBarry Smith     }
195455849f57SBarry Smith     if (ts->ops->view) {
195555849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
195655849f57SBarry Smith     }
1957f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1958f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
19592d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19602d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19612b0a91c0SBarry Smith   } else if (isdraw) {
19622b0a91c0SBarry Smith     PetscDraw draw;
19632b0a91c0SBarry Smith     char      str[36];
196489fd9fafSBarry Smith     PetscReal x,y,bottom,h;
19652b0a91c0SBarry Smith 
19662b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
19672b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
19682b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
19692b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
197051fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
197189fd9fafSBarry Smith     bottom = y - h;
19722b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
19732b0a91c0SBarry Smith     if (ts->ops->view) {
19742b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
19752b0a91c0SBarry Smith     }
19762b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1977e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1978536b137fSBarry Smith   } else if (issaws) {
1979d45a07a7SBarry Smith     PetscMPIInt rank;
19802657e9d9SBarry Smith     const char  *name;
19812657e9d9SBarry Smith 
19822657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1983d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1984d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1985d45a07a7SBarry Smith       char       dir[1024];
1986d45a07a7SBarry Smith 
1987e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1988a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
19892657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
19902657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
19912657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1992d763cef2SBarry Smith     }
19930acecf5bSBarry Smith     if (ts->ops->view) {
19940acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
19950acecf5bSBarry Smith     }
1996f05ece33SBarry Smith #endif
1997f05ece33SBarry Smith   }
1998f05ece33SBarry Smith 
1999b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2000251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2001b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2002d763cef2SBarry Smith   PetscFunctionReturn(0);
2003d763cef2SBarry Smith }
2004d763cef2SBarry Smith 
2005d763cef2SBarry Smith 
20064a2ae208SSatish Balay #undef __FUNCT__
20074a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
2008b07ff414SBarry Smith /*@
2009d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2010d763cef2SBarry Smith    the timesteppers.
2011d763cef2SBarry Smith 
20123f9fe445SBarry Smith    Logically Collective on TS
2013d763cef2SBarry Smith 
2014d763cef2SBarry Smith    Input Parameters:
2015d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2016d763cef2SBarry Smith -  usrP - optional user context
2017d763cef2SBarry Smith 
2018daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2019daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2020daf670e6SBarry Smith 
2021d763cef2SBarry Smith    Level: intermediate
2022d763cef2SBarry Smith 
2023d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
2024d763cef2SBarry Smith 
2025d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2026d763cef2SBarry Smith @*/
20277087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2028d763cef2SBarry Smith {
2029d763cef2SBarry Smith   PetscFunctionBegin;
20300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2031d763cef2SBarry Smith   ts->user = usrP;
2032d763cef2SBarry Smith   PetscFunctionReturn(0);
2033d763cef2SBarry Smith }
2034d763cef2SBarry Smith 
20354a2ae208SSatish Balay #undef __FUNCT__
20364a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
2037b07ff414SBarry Smith /*@
2038d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2039d763cef2SBarry Smith     timestepper.
2040d763cef2SBarry Smith 
2041d763cef2SBarry Smith     Not Collective
2042d763cef2SBarry Smith 
2043d763cef2SBarry Smith     Input Parameter:
2044d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2045d763cef2SBarry Smith 
2046d763cef2SBarry Smith     Output Parameter:
2047d763cef2SBarry Smith .   usrP - user context
2048d763cef2SBarry Smith 
2049daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2050daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2051daf670e6SBarry Smith 
2052d763cef2SBarry Smith     Level: intermediate
2053d763cef2SBarry Smith 
2054d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2055d763cef2SBarry Smith 
2056d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2057d763cef2SBarry Smith @*/
2058e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2059d763cef2SBarry Smith {
2060d763cef2SBarry Smith   PetscFunctionBegin;
20610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2062e71120c6SJed Brown   *(void**)usrP = ts->user;
2063d763cef2SBarry Smith   PetscFunctionReturn(0);
2064d763cef2SBarry Smith }
2065d763cef2SBarry Smith 
20664a2ae208SSatish Balay #undef __FUNCT__
20674a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
2068d763cef2SBarry Smith /*@
2069b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
2070d763cef2SBarry Smith 
2071d763cef2SBarry Smith    Not Collective
2072d763cef2SBarry Smith 
2073d763cef2SBarry Smith    Input Parameter:
2074d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2075d763cef2SBarry Smith 
2076d763cef2SBarry Smith    Output Parameter:
2077b8123daeSJed Brown .  iter - number of steps completed so far
2078d763cef2SBarry Smith 
2079d763cef2SBarry Smith    Level: intermediate
2080d763cef2SBarry Smith 
2081d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
20829be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2083d763cef2SBarry Smith @*/
20847087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
2085d763cef2SBarry Smith {
2086d763cef2SBarry Smith   PetscFunctionBegin;
20870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20884482741eSBarry Smith   PetscValidIntPointer(iter,2);
2089d763cef2SBarry Smith   *iter = ts->steps;
2090d763cef2SBarry Smith   PetscFunctionReturn(0);
2091d763cef2SBarry Smith }
2092d763cef2SBarry Smith 
20934a2ae208SSatish Balay #undef __FUNCT__
20944a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
2095d763cef2SBarry Smith /*@
2096d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
2097d763cef2SBarry Smith    as well as the initial time.
2098d763cef2SBarry Smith 
20993f9fe445SBarry Smith    Logically Collective on TS
2100d763cef2SBarry Smith 
2101d763cef2SBarry Smith    Input Parameters:
2102d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2103d763cef2SBarry Smith .  initial_time - the initial time
2104d763cef2SBarry Smith -  time_step - the size of the timestep
2105d763cef2SBarry Smith 
2106d763cef2SBarry Smith    Level: intermediate
2107d763cef2SBarry Smith 
2108d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
2109d763cef2SBarry Smith 
2110d763cef2SBarry Smith .keywords: TS, set, initial, timestep
2111d763cef2SBarry Smith @*/
21127087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
2113d763cef2SBarry Smith {
2114e144a568SJed Brown   PetscErrorCode ierr;
2115e144a568SJed Brown 
2116d763cef2SBarry Smith   PetscFunctionBegin;
21170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2118e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
2119d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
2120d763cef2SBarry Smith   PetscFunctionReturn(0);
2121d763cef2SBarry Smith }
2122d763cef2SBarry Smith 
21234a2ae208SSatish Balay #undef __FUNCT__
21244a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
2125d763cef2SBarry Smith /*@
2126d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2127d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2128d763cef2SBarry Smith 
21293f9fe445SBarry Smith    Logically Collective on TS
2130d763cef2SBarry Smith 
2131d763cef2SBarry Smith    Input Parameters:
2132d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2133d763cef2SBarry Smith -  time_step - the size of the timestep
2134d763cef2SBarry Smith 
2135d763cef2SBarry Smith    Level: intermediate
2136d763cef2SBarry Smith 
2137d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2138d763cef2SBarry Smith 
2139d763cef2SBarry Smith .keywords: TS, set, timestep
2140d763cef2SBarry Smith @*/
21417087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2142d763cef2SBarry Smith {
2143d763cef2SBarry Smith   PetscFunctionBegin;
21440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2145c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2146d763cef2SBarry Smith   ts->time_step = time_step;
2147d763cef2SBarry Smith   PetscFunctionReturn(0);
2148d763cef2SBarry Smith }
2149d763cef2SBarry Smith 
21504a2ae208SSatish Balay #undef __FUNCT__
2151a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
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 
2186a43b19c4SJed Brown #undef __FUNCT__
21874a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
2188d763cef2SBarry Smith /*@
2189d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2190d763cef2SBarry Smith 
2191d763cef2SBarry Smith    Not Collective
2192d763cef2SBarry Smith 
2193d763cef2SBarry Smith    Input Parameter:
2194d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2195d763cef2SBarry Smith 
2196d763cef2SBarry Smith    Output Parameter:
2197d763cef2SBarry Smith .  dt - the current timestep size
2198d763cef2SBarry Smith 
2199d763cef2SBarry Smith    Level: intermediate
2200d763cef2SBarry Smith 
2201d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2202d763cef2SBarry Smith 
2203d763cef2SBarry Smith .keywords: TS, get, timestep
2204d763cef2SBarry Smith @*/
22057087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2206d763cef2SBarry Smith {
2207d763cef2SBarry Smith   PetscFunctionBegin;
22080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2209f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2210d763cef2SBarry Smith   *dt = ts->time_step;
2211d763cef2SBarry Smith   PetscFunctionReturn(0);
2212d763cef2SBarry Smith }
2213d763cef2SBarry Smith 
22144a2ae208SSatish Balay #undef __FUNCT__
22154a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
2216d8e5e3e6SSatish Balay /*@
2217d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2218d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2219d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2220d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2221d763cef2SBarry Smith 
2222d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2223d763cef2SBarry Smith 
2224d763cef2SBarry Smith    Input Parameter:
2225d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2226d763cef2SBarry Smith 
2227d763cef2SBarry Smith    Output Parameter:
2228d763cef2SBarry Smith .  v - the vector containing the solution
2229d763cef2SBarry Smith 
223063e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
223163e21af5SBarry Smith    final time. It returns the solution at the next timestep.
223263e21af5SBarry Smith 
2233d763cef2SBarry Smith    Level: intermediate
2234d763cef2SBarry Smith 
2235b2bf4f3aSDebojyoti Ghosh .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents()
2236d763cef2SBarry Smith 
2237d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2238d763cef2SBarry Smith @*/
22397087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2240d763cef2SBarry Smith {
2241d763cef2SBarry Smith   PetscFunctionBegin;
22420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22434482741eSBarry Smith   PetscValidPointer(v,2);
22448737fe31SLisandro Dalcin   *v = ts->vec_sol;
2245d763cef2SBarry Smith   PetscFunctionReturn(0);
2246d763cef2SBarry Smith }
2247d763cef2SBarry Smith 
2248c235aa8dSHong Zhang #undef __FUNCT__
2249b2bf4f3aSDebojyoti Ghosh #define __FUNCT__ "TSGetSolutionComponents"
225003fe5f5eSDebojyoti Ghosh /*@
2251b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
225203fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2253b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
225403fe5f5eSDebojyoti Ghosh    structure as the solution vector.
225503fe5f5eSDebojyoti Ghosh 
225603fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
225703fe5f5eSDebojyoti Ghosh 
225803fe5f5eSDebojyoti Ghosh    Parameters :
225903fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2260b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2261b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
226203fe5f5eSDebojyoti Ghosh        returned in v.
2263b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
226403fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2265b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
226603fe5f5eSDebojyoti Ghosh 
22674cdd57e5SDebojyoti Ghosh    Level: advanced
226803fe5f5eSDebojyoti Ghosh 
226903fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
227003fe5f5eSDebojyoti Ghosh 
227103fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
227203fe5f5eSDebojyoti Ghosh @*/
2273b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
227403fe5f5eSDebojyoti Ghosh {
227503fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
227603fe5f5eSDebojyoti Ghosh 
227703fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
227803fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2279b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
228003fe5f5eSDebojyoti Ghosh   else {
2281b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
228203fe5f5eSDebojyoti Ghosh   }
228303fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
228403fe5f5eSDebojyoti Ghosh }
228503fe5f5eSDebojyoti Ghosh 
228603fe5f5eSDebojyoti Ghosh #undef __FUNCT__
22874cdd57e5SDebojyoti Ghosh #define __FUNCT__ "TSGetAuxSolution"
22884cdd57e5SDebojyoti Ghosh /*@
22894cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
22904cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
22914cdd57e5SDebojyoti Ghosh 
22924cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
22934cdd57e5SDebojyoti Ghosh 
22944cdd57e5SDebojyoti Ghosh    Parameters :
22954cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
22964cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
22974cdd57e5SDebojyoti Ghosh 
22984cdd57e5SDebojyoti Ghosh    Level: intermediate
22994cdd57e5SDebojyoti Ghosh 
23004cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
23014cdd57e5SDebojyoti Ghosh 
23024cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
23034cdd57e5SDebojyoti Ghosh @*/
23044cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
23054cdd57e5SDebojyoti Ghosh {
23064cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23074cdd57e5SDebojyoti Ghosh 
23084cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23094cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2310f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
23114cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2312f6356ec7SDebojyoti Ghosh   } else {
2313f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2314f6356ec7SDebojyoti Ghosh   }
23154cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23164cdd57e5SDebojyoti Ghosh }
23174cdd57e5SDebojyoti Ghosh 
23184cdd57e5SDebojyoti Ghosh #undef __FUNCT__
23194cdd57e5SDebojyoti Ghosh #define __FUNCT__ "TSGetTimeError"
23204cdd57e5SDebojyoti Ghosh /*@
23214cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
23224cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
23234cdd57e5SDebojyoti Ghosh 
23244cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23254cdd57e5SDebojyoti Ghosh 
23269657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
23279657682dSDebojyoti Ghosh 
23284cdd57e5SDebojyoti Ghosh    Parameters :
23294cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2330657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
23314cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
23324cdd57e5SDebojyoti Ghosh 
23334cdd57e5SDebojyoti Ghosh    Level: intermediate
23344cdd57e5SDebojyoti Ghosh 
233557df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
23364cdd57e5SDebojyoti Ghosh 
23374cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
23384cdd57e5SDebojyoti Ghosh @*/
23390a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
23404cdd57e5SDebojyoti Ghosh {
23414cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23424cdd57e5SDebojyoti Ghosh 
23434cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23444cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2345f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
23460a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2347f6356ec7SDebojyoti Ghosh   } else {
2348f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2349f6356ec7SDebojyoti Ghosh   }
23504cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23514cdd57e5SDebojyoti Ghosh }
23524cdd57e5SDebojyoti Ghosh 
23534cdd57e5SDebojyoti Ghosh #undef __FUNCT__
235457df6a1bSDebojyoti Ghosh #define __FUNCT__ "TSSetTimeError"
235557df6a1bSDebojyoti Ghosh /*@
235657df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
235757df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
235857df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
235957df6a1bSDebojyoti Ghosh 
236057df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
236157df6a1bSDebojyoti Ghosh 
236257df6a1bSDebojyoti Ghosh    Parameters :
236357df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
236457df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
236557df6a1bSDebojyoti Ghosh 
236657df6a1bSDebojyoti Ghosh    Level: intermediate
236757df6a1bSDebojyoti Ghosh 
236857df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
236957df6a1bSDebojyoti Ghosh 
237057df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
237157df6a1bSDebojyoti Ghosh @*/
237257df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
237357df6a1bSDebojyoti Ghosh {
237457df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
237557df6a1bSDebojyoti Ghosh 
237657df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
237757df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23789657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
237957df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
238057df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
238157df6a1bSDebojyoti Ghosh   }
238257df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
238357df6a1bSDebojyoti Ghosh }
238457df6a1bSDebojyoti Ghosh 
238557df6a1bSDebojyoti Ghosh #undef __FUNCT__
2386dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients"
2387c235aa8dSHong Zhang /*@
2388dfb21088SHong Zhang    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()
2389c235aa8dSHong Zhang 
2390c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
2391c235aa8dSHong Zhang 
2392c235aa8dSHong Zhang    Input Parameter:
2393c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
2394c235aa8dSHong Zhang 
2395c235aa8dSHong Zhang    Output Parameter:
2396abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
2397abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
2398c235aa8dSHong Zhang 
2399c235aa8dSHong Zhang    Level: intermediate
2400c235aa8dSHong Zhang 
2401c235aa8dSHong Zhang .seealso: TSGetTimeStep()
2402c235aa8dSHong Zhang 
2403c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
2404c235aa8dSHong Zhang @*/
2405dfb21088SHong Zhang PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
2406c235aa8dSHong Zhang {
2407c235aa8dSHong Zhang   PetscFunctionBegin;
2408c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2409abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
2410abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
2411abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
2412c235aa8dSHong Zhang   PetscFunctionReturn(0);
2413c235aa8dSHong Zhang }
2414c235aa8dSHong Zhang 
2415bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
24164a2ae208SSatish Balay #undef __FUNCT__
2417bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
2418d8e5e3e6SSatish Balay /*@
2419bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2420d763cef2SBarry Smith 
2421bdad233fSMatthew Knepley   Not collective
2422d763cef2SBarry Smith 
2423bdad233fSMatthew Knepley   Input Parameters:
2424bdad233fSMatthew Knepley + ts   - The TS
2425bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2426d763cef2SBarry Smith .vb
24270910c330SBarry Smith          U_t - A U = 0      (linear)
24280910c330SBarry Smith          U_t - A(t) U = 0   (linear)
24290910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2430d763cef2SBarry Smith .ve
2431d763cef2SBarry Smith 
2432d763cef2SBarry Smith    Level: beginner
2433d763cef2SBarry Smith 
2434bdad233fSMatthew Knepley .keywords: TS, problem type
2435bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2436d763cef2SBarry Smith @*/
24377087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2438a7cc72afSBarry Smith {
24399e2a6581SJed Brown   PetscErrorCode ierr;
24409e2a6581SJed Brown 
2441d763cef2SBarry Smith   PetscFunctionBegin;
24420700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2443bdad233fSMatthew Knepley   ts->problem_type = type;
24449e2a6581SJed Brown   if (type == TS_LINEAR) {
24459e2a6581SJed Brown     SNES snes;
24469e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
24479e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
24489e2a6581SJed Brown   }
2449d763cef2SBarry Smith   PetscFunctionReturn(0);
2450d763cef2SBarry Smith }
2451d763cef2SBarry Smith 
2452bdad233fSMatthew Knepley #undef __FUNCT__
2453bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
2454bdad233fSMatthew Knepley /*@C
2455bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2456bdad233fSMatthew Knepley 
2457bdad233fSMatthew Knepley   Not collective
2458bdad233fSMatthew Knepley 
2459bdad233fSMatthew Knepley   Input Parameter:
2460bdad233fSMatthew Knepley . ts   - The TS
2461bdad233fSMatthew Knepley 
2462bdad233fSMatthew Knepley   Output Parameter:
2463bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2464bdad233fSMatthew Knepley .vb
2465089b2837SJed Brown          M U_t = A U
2466089b2837SJed Brown          M(t) U_t = A(t) U
2467b5abc632SBarry Smith          F(t,U,U_t)
2468bdad233fSMatthew Knepley .ve
2469bdad233fSMatthew Knepley 
2470bdad233fSMatthew Knepley    Level: beginner
2471bdad233fSMatthew Knepley 
2472bdad233fSMatthew Knepley .keywords: TS, problem type
2473bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2474bdad233fSMatthew Knepley @*/
24757087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2476a7cc72afSBarry Smith {
2477bdad233fSMatthew Knepley   PetscFunctionBegin;
24780700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
24794482741eSBarry Smith   PetscValidIntPointer(type,2);
2480bdad233fSMatthew Knepley   *type = ts->problem_type;
2481bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2482bdad233fSMatthew Knepley }
2483d763cef2SBarry Smith 
24844a2ae208SSatish Balay #undef __FUNCT__
24854a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
2486d763cef2SBarry Smith /*@
2487d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2488d763cef2SBarry Smith    of a timestepper.
2489d763cef2SBarry Smith 
2490d763cef2SBarry Smith    Collective on TS
2491d763cef2SBarry Smith 
2492d763cef2SBarry Smith    Input Parameter:
2493d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2494d763cef2SBarry Smith 
2495d763cef2SBarry Smith    Notes:
2496d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2497d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2498d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
2499d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2500d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
2501d763cef2SBarry Smith 
2502d763cef2SBarry Smith    Level: advanced
2503d763cef2SBarry Smith 
2504d763cef2SBarry Smith .keywords: TS, timestep, setup
2505d763cef2SBarry Smith 
2506d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
2507d763cef2SBarry Smith @*/
25087087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2509d763cef2SBarry Smith {
2510dfbe8321SBarry Smith   PetscErrorCode ierr;
25116c6b9e74SPeter Brune   DM             dm;
25126c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2513d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2514cd11d68dSLisandro Dalcin   TSIFunction    ifun;
25156c6b9e74SPeter Brune   TSIJacobian    ijac;
2516efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
25176c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
2518d763cef2SBarry Smith 
2519d763cef2SBarry Smith   PetscFunctionBegin;
25200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2521277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2522277b19d0SLisandro Dalcin 
25237adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2524cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2525cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2526d763cef2SBarry Smith   }
2527277b19d0SLisandro Dalcin 
2528277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2529277b19d0SLisandro Dalcin 
2530e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
2531e1244c69SJed Brown     Mat Amat,Pmat;
2532e1244c69SJed Brown     SNES snes;
2533e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2534e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2535e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2536e1244c69SJed Brown      * have displaced the RHS matrix */
2537e1244c69SJed Brown     if (Amat == ts->Arhs) {
2538e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2539e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2540e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2541e1244c69SJed Brown     }
2542e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2543e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2544e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2545e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2546e1244c69SJed Brown     }
2547e1244c69SJed Brown   }
2548277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2549000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2550277b19d0SLisandro Dalcin   }
2551277b19d0SLisandro Dalcin 
2552a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
25536c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
25546c6b9e74SPeter Brune    */
25556c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
25560298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
25576c6b9e74SPeter Brune   if (!func) {
25586c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
25596c6b9e74SPeter Brune   }
2560a6772fa2SLisandro 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.
25616c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
25626c6b9e74SPeter Brune    */
25630298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
25640298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2565efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
25660298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2567efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
25686c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
25696c6b9e74SPeter Brune   }
2570c0517034SDebojyoti Ghosh 
2571c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2572c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2573c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2574c0517034SDebojyoti Ghosh   }
2575c0517034SDebojyoti Ghosh 
2576277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2577277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2578277b19d0SLisandro Dalcin }
2579277b19d0SLisandro Dalcin 
2580277b19d0SLisandro Dalcin #undef __FUNCT__
2581f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp"
2582f6a906c0SBarry Smith /*@
2583f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
2584f6a906c0SBarry Smith    of an adjoint solver
2585f6a906c0SBarry Smith 
2586f6a906c0SBarry Smith    Collective on TS
2587f6a906c0SBarry Smith 
2588f6a906c0SBarry Smith    Input Parameter:
2589f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
2590f6a906c0SBarry Smith 
2591f6a906c0SBarry Smith    Level: advanced
2592f6a906c0SBarry Smith 
2593f6a906c0SBarry Smith .keywords: TS, timestep, setup
2594f6a906c0SBarry Smith 
2595947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
2596f6a906c0SBarry Smith @*/
2597f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
2598f6a906c0SBarry Smith {
2599f6a906c0SBarry Smith   PetscErrorCode ierr;
2600f6a906c0SBarry Smith 
2601f6a906c0SBarry Smith   PetscFunctionBegin;
2602f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2603f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
2604dfb21088SHong Zhang   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
2605d8151eeaSBarry Smith 
2606947abb85SHong Zhang   if (ts->vec_costintegral) { /* if there is integral in the cost function*/
2607d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2608d8151eeaSBarry Smith     if (ts->vecs_sensip){
2609d8151eeaSBarry Smith       ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2610d8151eeaSBarry Smith     }
2611947abb85SHong Zhang   }
2612d8151eeaSBarry Smith 
261342f2b339SBarry Smith   if (ts->ops->adjointsetup) {
261442f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
2615f6a906c0SBarry Smith   }
2616f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
2617f6a906c0SBarry Smith   PetscFunctionReturn(0);
2618f6a906c0SBarry Smith }
2619f6a906c0SBarry Smith 
2620f6a906c0SBarry Smith #undef __FUNCT__
2621277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
2622277b19d0SLisandro Dalcin /*@
2623277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2624277b19d0SLisandro Dalcin 
2625277b19d0SLisandro Dalcin    Collective on TS
2626277b19d0SLisandro Dalcin 
2627277b19d0SLisandro Dalcin    Input Parameter:
2628277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2629277b19d0SLisandro Dalcin 
2630277b19d0SLisandro Dalcin    Level: beginner
2631277b19d0SLisandro Dalcin 
2632277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2633277b19d0SLisandro Dalcin 
2634277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2635277b19d0SLisandro Dalcin @*/
2636277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2637277b19d0SLisandro Dalcin {
2638277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2639277b19d0SLisandro Dalcin 
2640277b19d0SLisandro Dalcin   PetscFunctionBegin;
2641277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2642b18ea86cSHong Zhang 
2643277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2644277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2645277b19d0SLisandro Dalcin   }
2646277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2647e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2648bbd56ea5SKarl Rupp 
26494e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
26504e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2651214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
26526bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2653efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2654e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2655e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
265638637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2657bbd56ea5SKarl Rupp 
2658947abb85SHong Zhang  if (ts->vec_costintegral) {
2659d8151eeaSBarry Smith     ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2660d8151eeaSBarry Smith     if (ts->vecs_drdp){
2661d8151eeaSBarry Smith       ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2662d8151eeaSBarry Smith     }
2663947abb85SHong Zhang   }
2664d8151eeaSBarry Smith   ts->vecs_sensi  = NULL;
2665d8151eeaSBarry Smith   ts->vecs_sensip = NULL;
2666ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
26672c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
266836eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2669277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2670d763cef2SBarry Smith   PetscFunctionReturn(0);
2671d763cef2SBarry Smith }
2672d763cef2SBarry Smith 
26734a2ae208SSatish Balay #undef __FUNCT__
26744a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
2675d8e5e3e6SSatish Balay /*@
2676d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2677d763cef2SBarry Smith    with TSCreate().
2678d763cef2SBarry Smith 
2679d763cef2SBarry Smith    Collective on TS
2680d763cef2SBarry Smith 
2681d763cef2SBarry Smith    Input Parameter:
2682d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2683d763cef2SBarry Smith 
2684d763cef2SBarry Smith    Level: beginner
2685d763cef2SBarry Smith 
2686d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2687d763cef2SBarry Smith 
2688d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2689d763cef2SBarry Smith @*/
26906bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2691d763cef2SBarry Smith {
26926849ba73SBarry Smith   PetscErrorCode ierr;
2693d763cef2SBarry Smith 
2694d763cef2SBarry Smith   PetscFunctionBegin;
26956bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
26966bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
26976bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2698d763cef2SBarry Smith 
26996bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2700277b19d0SLisandro Dalcin 
2701e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2702e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
27036bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
27046d4c513bSLisandro Dalcin 
2705bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2706bc952696SBarry Smith 
270784df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
27086427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
27096427ac75SLisandro Dalcin 
27106bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
27116bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
27126bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
27130dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
27146d4c513bSLisandro Dalcin 
2715a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2716d763cef2SBarry Smith   PetscFunctionReturn(0);
2717d763cef2SBarry Smith }
2718d763cef2SBarry Smith 
27194a2ae208SSatish Balay #undef __FUNCT__
27204a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2721d8e5e3e6SSatish Balay /*@
2722d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2723d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2724d763cef2SBarry Smith 
2725d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2726d763cef2SBarry Smith 
2727d763cef2SBarry Smith    Input Parameter:
2728d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2729d763cef2SBarry Smith 
2730d763cef2SBarry Smith    Output Parameter:
2731d763cef2SBarry Smith .  snes - the nonlinear solver context
2732d763cef2SBarry Smith 
2733d763cef2SBarry Smith    Notes:
2734d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2735d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
273694b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2737d763cef2SBarry Smith 
2738d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
27390298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2740d763cef2SBarry Smith 
2741d763cef2SBarry Smith    Level: beginner
2742d763cef2SBarry Smith 
2743d763cef2SBarry Smith .keywords: timestep, get, SNES
2744d763cef2SBarry Smith @*/
27457087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2746d763cef2SBarry Smith {
2747d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2748d372ba47SLisandro Dalcin 
2749d763cef2SBarry Smith   PetscFunctionBegin;
27500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27514482741eSBarry Smith   PetscValidPointer(snes,2);
2752d372ba47SLisandro Dalcin   if (!ts->snes) {
2753ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
27540298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27553bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2756d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2757496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
27589e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
27599e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
27609e2a6581SJed Brown     }
2761d372ba47SLisandro Dalcin   }
2762d763cef2SBarry Smith   *snes = ts->snes;
2763d763cef2SBarry Smith   PetscFunctionReturn(0);
2764d763cef2SBarry Smith }
2765d763cef2SBarry Smith 
27664a2ae208SSatish Balay #undef __FUNCT__
2767deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2768deb2cd25SJed Brown /*@
2769deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2770deb2cd25SJed Brown 
2771deb2cd25SJed Brown    Collective
2772deb2cd25SJed Brown 
2773deb2cd25SJed Brown    Input Parameter:
2774deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2775deb2cd25SJed Brown -  snes - the nonlinear solver context
2776deb2cd25SJed Brown 
2777deb2cd25SJed Brown    Notes:
2778deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2779deb2cd25SJed Brown 
2780deb2cd25SJed Brown    Level: developer
2781deb2cd25SJed Brown 
2782deb2cd25SJed Brown .keywords: timestep, set, SNES
2783deb2cd25SJed Brown @*/
2784deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2785deb2cd25SJed Brown {
2786deb2cd25SJed Brown   PetscErrorCode ierr;
2787d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2788deb2cd25SJed Brown 
2789deb2cd25SJed Brown   PetscFunctionBegin;
2790deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2791deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2792deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2793deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2794bbd56ea5SKarl Rupp 
2795deb2cd25SJed Brown   ts->snes = snes;
2796bbd56ea5SKarl Rupp 
27970298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27980298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2799740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
28000298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2801740132f1SEmil Constantinescu   }
2802deb2cd25SJed Brown   PetscFunctionReturn(0);
2803deb2cd25SJed Brown }
2804deb2cd25SJed Brown 
2805deb2cd25SJed Brown #undef __FUNCT__
280694b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2807d8e5e3e6SSatish Balay /*@
280894b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2809d763cef2SBarry Smith    a TS (timestepper) context.
2810d763cef2SBarry Smith 
281194b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2812d763cef2SBarry Smith 
2813d763cef2SBarry Smith    Input Parameter:
2814d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2815d763cef2SBarry Smith 
2816d763cef2SBarry Smith    Output Parameter:
281794b7f48cSBarry Smith .  ksp - the nonlinear solver context
2818d763cef2SBarry Smith 
2819d763cef2SBarry Smith    Notes:
282094b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2821d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2822d763cef2SBarry Smith    KSP and PC contexts as well.
2823d763cef2SBarry Smith 
282494b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
28250298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2826d763cef2SBarry Smith 
2827d763cef2SBarry Smith    Level: beginner
2828d763cef2SBarry Smith 
282994b7f48cSBarry Smith .keywords: timestep, get, KSP
2830d763cef2SBarry Smith @*/
28317087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2832d763cef2SBarry Smith {
2833d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2834089b2837SJed Brown   SNES           snes;
2835d372ba47SLisandro Dalcin 
2836d763cef2SBarry Smith   PetscFunctionBegin;
28370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28384482741eSBarry Smith   PetscValidPointer(ksp,2);
283917186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2840e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2841089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2842089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2843d763cef2SBarry Smith   PetscFunctionReturn(0);
2844d763cef2SBarry Smith }
2845d763cef2SBarry Smith 
2846d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2847d763cef2SBarry Smith 
28484a2ae208SSatish Balay #undef __FUNCT__
2849adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2850adb62b0dSMatthew Knepley /*@
2851adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2852adb62b0dSMatthew Knepley    maximum time for iteration.
2853adb62b0dSMatthew Knepley 
28543f9fe445SBarry Smith    Not Collective
2855adb62b0dSMatthew Knepley 
2856adb62b0dSMatthew Knepley    Input Parameters:
2857adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
28580298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
28590298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2860adb62b0dSMatthew Knepley 
2861adb62b0dSMatthew Knepley    Level: intermediate
2862adb62b0dSMatthew Knepley 
2863adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2864adb62b0dSMatthew Knepley @*/
28657087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2866adb62b0dSMatthew Knepley {
2867adb62b0dSMatthew Knepley   PetscFunctionBegin;
28680700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2869abc0a331SBarry Smith   if (maxsteps) {
28704482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2871adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2872adb62b0dSMatthew Knepley   }
2873abc0a331SBarry Smith   if (maxtime) {
28744482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2875adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2876adb62b0dSMatthew Knepley   }
2877adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2878adb62b0dSMatthew Knepley }
2879adb62b0dSMatthew Knepley 
2880adb62b0dSMatthew Knepley #undef __FUNCT__
28814a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2882d763cef2SBarry Smith /*@
2883d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2884d763cef2SBarry Smith    maximum time for iteration.
2885d763cef2SBarry Smith 
28863f9fe445SBarry Smith    Logically Collective on TS
2887d763cef2SBarry Smith 
2888d763cef2SBarry Smith    Input Parameters:
2889d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2890d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2891d763cef2SBarry Smith -  maxtime - final time to iterate to
2892d763cef2SBarry Smith 
2893d763cef2SBarry Smith    Options Database Keys:
2894d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
28953bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2896d763cef2SBarry Smith 
2897d763cef2SBarry Smith    Notes:
2898d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2899d763cef2SBarry Smith 
2900d763cef2SBarry Smith    Level: intermediate
2901d763cef2SBarry Smith 
2902d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2903a43b19c4SJed Brown 
2904a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2905d763cef2SBarry Smith @*/
29067087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2907d763cef2SBarry Smith {
2908d763cef2SBarry Smith   PetscFunctionBegin;
29090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2910c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2911c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
291239b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
291339b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2914d763cef2SBarry Smith   PetscFunctionReturn(0);
2915d763cef2SBarry Smith }
2916d763cef2SBarry Smith 
29174a2ae208SSatish Balay #undef __FUNCT__
29184a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2919d763cef2SBarry Smith /*@
2920d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2921d763cef2SBarry Smith    for use by the TS routines.
2922d763cef2SBarry Smith 
29233f9fe445SBarry Smith    Logically Collective on TS and Vec
2924d763cef2SBarry Smith 
2925d763cef2SBarry Smith    Input Parameters:
2926d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
29270910c330SBarry Smith -  u - the solution vector
2928d763cef2SBarry Smith 
2929d763cef2SBarry Smith    Level: beginner
2930d763cef2SBarry Smith 
2931d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2932d763cef2SBarry Smith @*/
29330910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2934d763cef2SBarry Smith {
29358737fe31SLisandro Dalcin   PetscErrorCode ierr;
2936496e6a7aSJed Brown   DM             dm;
29378737fe31SLisandro Dalcin 
2938d763cef2SBarry Smith   PetscFunctionBegin;
29390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29400910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
29410910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
29426bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
29430910c330SBarry Smith   ts->vec_sol = u;
2944bbd56ea5SKarl Rupp 
2945496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
29460910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2947d763cef2SBarry Smith   PetscFunctionReturn(0);
2948d763cef2SBarry Smith }
2949d763cef2SBarry Smith 
2950e74ef692SMatthew Knepley #undef __FUNCT__
295173b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps"
295273b18844SBarry Smith /*@
295373b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
295473b18844SBarry Smith 
295573b18844SBarry Smith    Logically Collective on TS
295673b18844SBarry Smith 
295773b18844SBarry Smith    Input Parameters:
295873b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
295973b18844SBarry Smith .  steps - number of steps to use
296073b18844SBarry Smith 
296173b18844SBarry Smith    Level: intermediate
296273b18844SBarry Smith 
296373b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
296473b18844SBarry Smith           so as to integrate back to less than the original timestep
296573b18844SBarry Smith 
296673b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
296773b18844SBarry Smith 
296873b18844SBarry Smith .seealso: TSSetExactFinalTime()
296973b18844SBarry Smith @*/
297073b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
297173b18844SBarry Smith {
297273b18844SBarry Smith   PetscFunctionBegin;
297373b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
297473b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
297573b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
297673b18844SBarry 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");
297773b18844SBarry Smith   ts->adjoint_max_steps = steps;
297873b18844SBarry Smith   PetscFunctionReturn(0);
297973b18844SBarry Smith }
298073b18844SBarry Smith 
298173b18844SBarry Smith #undef __FUNCT__
2982dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients"
2983c235aa8dSHong Zhang /*@
2984dfb21088SHong 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
29850cf82b59SBarry Smith       for use by the TSAdjoint routines.
2986c235aa8dSHong Zhang 
2987c235aa8dSHong Zhang    Logically Collective on TS and Vec
2988c235aa8dSHong Zhang 
2989c235aa8dSHong Zhang    Input Parameters:
2990c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2991abc2977eSBarry 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
2992abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
2993c235aa8dSHong Zhang 
2994c235aa8dSHong Zhang    Level: beginner
2995c235aa8dSHong Zhang 
2996abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
29970cf82b59SBarry Smith 
2998c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2999c235aa8dSHong Zhang @*/
3000dfb21088SHong Zhang PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
3001c235aa8dSHong Zhang {
3002c235aa8dSHong Zhang   PetscFunctionBegin;
3003c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3004abc2977eSBarry Smith   PetscValidPointer(lambda,2);
3005abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
3006abc2977eSBarry Smith   ts->vecs_sensip = mu;
3007dfb21088SHong 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");
3008abc2977eSBarry Smith   ts->numcost  = numcost;
3009ad8e2604SHong Zhang   PetscFunctionReturn(0);
3010ad8e2604SHong Zhang }
3011ad8e2604SHong Zhang 
3012ad8e2604SHong Zhang #undef __FUNCT__
30135bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian"
3014ad8e2604SHong Zhang /*@C
30150cf82b59SBarry 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.
3016ad8e2604SHong Zhang 
3017ad8e2604SHong Zhang   Logically Collective on TS
3018ad8e2604SHong Zhang 
3019ad8e2604SHong Zhang   Input Parameters:
3020ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
3021ad8e2604SHong Zhang - func - The function
3022ad8e2604SHong Zhang 
3023ad8e2604SHong Zhang   Calling sequence of func:
30240cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
302505755b9cSHong Zhang +   t - current timestep
30260cf82b59SBarry Smith .   y - input vector (current ODE solution)
302705755b9cSHong Zhang .   A - output matrix
302805755b9cSHong Zhang -   ctx - [optional] user-defined function context
3029ad8e2604SHong Zhang 
3030ad8e2604SHong Zhang   Level: intermediate
3031ad8e2604SHong Zhang 
30320cf82b59SBarry 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
30330cf82b59SBarry Smith 
3034ad8e2604SHong Zhang .keywords: TS, sensitivity
3035ad8e2604SHong Zhang .seealso:
3036ad8e2604SHong Zhang @*/
30375bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
3038ad8e2604SHong Zhang {
3039ad8e2604SHong Zhang   PetscErrorCode ierr;
3040ad8e2604SHong Zhang 
3041ad8e2604SHong Zhang   PetscFunctionBegin;
3042ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3043ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
3044ad8e2604SHong Zhang 
3045ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
3046ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
3047ad8e2604SHong Zhang   if(Amat) {
3048ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
3049ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
3050ad8e2604SHong Zhang     ts->Jacp = Amat;
3051ad8e2604SHong Zhang   }
3052ad8e2604SHong Zhang   PetscFunctionReturn(0);
3053ad8e2604SHong Zhang }
3054ad8e2604SHong Zhang 
3055ad8e2604SHong Zhang #undef __FUNCT__
30565bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian"
30570cf82b59SBarry Smith /*@C
30585bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
3059ad8e2604SHong Zhang 
3060ad8e2604SHong Zhang   Collective on TS
3061ad8e2604SHong Zhang 
3062ad8e2604SHong Zhang   Input Parameters:
3063ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
3064ad8e2604SHong Zhang 
3065ad8e2604SHong Zhang   Level: developer
3066ad8e2604SHong Zhang 
3067ad8e2604SHong Zhang .keywords: TS, sensitivity
30685bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
3069ad8e2604SHong Zhang @*/
30705bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
3071ad8e2604SHong Zhang {
3072ad8e2604SHong Zhang   PetscErrorCode ierr;
3073ad8e2604SHong Zhang 
3074ad8e2604SHong Zhang   PetscFunctionBegin;
3075ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3076ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3077ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
3078ad8e2604SHong Zhang 
3079ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
3080ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
3081ad8e2604SHong Zhang   PetscStackPop;
3082c235aa8dSHong Zhang   PetscFunctionReturn(0);
3083c235aa8dSHong Zhang }
3084c235aa8dSHong Zhang 
3085c235aa8dSHong Zhang #undef __FUNCT__
3086dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand"
30876fd0887fSHong Zhang /*@C
3088dfb21088SHong Zhang     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
30896fd0887fSHong Zhang 
30906fd0887fSHong Zhang     Logically Collective on TS
30916fd0887fSHong Zhang 
30926fd0887fSHong Zhang     Input Parameters:
30936fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
3094abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
30950cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
3096b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
3097b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
3098feaa1ddbSSatish Balay .   fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
3099b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
31006fd0887fSHong Zhang 
31010cf82b59SBarry Smith     Calling sequence of rf:
31020cf82b59SBarry Smith $     rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx);
31036fd0887fSHong Zhang 
31046fd0887fSHong Zhang +   t - current timestep
31050cf82b59SBarry Smith .   y - input vector
31060cf82b59SBarry Smith .   f - function result; one vector entry for each cost function
31076fd0887fSHong Zhang -   ctx - [optional] user-defined function context
31086fd0887fSHong Zhang 
3109b612ec54SBarry Smith    Calling sequence of drdyf:
31100cf82b59SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
3111b612ec54SBarry Smith 
3112b612ec54SBarry Smith    Calling sequence of drdpf:
31130cf82b59SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
3114b612ec54SBarry Smith 
3115b612ec54SBarry Smith     Level: intermediate
31166fd0887fSHong Zhang 
3117abc2977eSBarry Smith     Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions
31180cf82b59SBarry Smith 
31196fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
31206fd0887fSHong Zhang 
3121dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
31226fd0887fSHong Zhang @*/
3123dfb21088SHong Zhang PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
3124d8151eeaSBarry Smith                                                           PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
3125b1cb36f3SHong Zhang                                                           PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),
3126b1cb36f3SHong Zhang                                                           PetscBool fwd,void *ctx)
31276fd0887fSHong Zhang {
31286fd0887fSHong Zhang   PetscErrorCode ierr;
31296fd0887fSHong Zhang 
31306fd0887fSHong Zhang   PetscFunctionBegin;
31316fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3132dfb21088SHong 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()");
3133c72ed514SHong Zhang   if (!ts->numcost) ts->numcost=numcost;
31346fd0887fSHong Zhang 
3135b1cb36f3SHong Zhang   ts->costintegralfwd  = fwd; /* Evaluate the cost integral in forward run if fwd is true */
3136abc2977eSBarry Smith   ierr                 = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
31372c39e106SBarry Smith   ierr                 = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
31380cf82b59SBarry Smith   ts->costintegrand    = rf;
313905755b9cSHong Zhang   ts->costintegrandctx = ctx;
3140b612ec54SBarry Smith   ts->drdyfunction     = drdyf;
3141b612ec54SBarry Smith   ts->drdpfunction     = drdpf;
31426fd0887fSHong Zhang   PetscFunctionReturn(0);
31436fd0887fSHong Zhang }
31446fd0887fSHong Zhang 
31456fd0887fSHong Zhang #undef __FUNCT__
3146dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral"
314736eaed60SHong Zhang /*@
3148dfb21088SHong Zhang    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
314936eaed60SHong Zhang    It is valid to call the routine after a backward run.
315036eaed60SHong Zhang 
315136eaed60SHong Zhang    Not Collective
315236eaed60SHong Zhang 
315336eaed60SHong Zhang    Input Parameter:
315436eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
315536eaed60SHong Zhang 
315636eaed60SHong Zhang    Output Parameter:
31572c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
315836eaed60SHong Zhang 
315936eaed60SHong Zhang    Level: intermediate
316036eaed60SHong Zhang 
3161dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
316236eaed60SHong Zhang 
316336eaed60SHong Zhang .keywords: TS, sensitivity analysis
316436eaed60SHong Zhang @*/
3165dfb21088SHong Zhang PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
316636eaed60SHong Zhang {
316736eaed60SHong Zhang   PetscFunctionBegin;
316836eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
316936eaed60SHong Zhang   PetscValidPointer(v,2);
31702c39e106SBarry Smith   *v = ts->vec_costintegral;
317136eaed60SHong Zhang   PetscFunctionReturn(0);
317236eaed60SHong Zhang }
317336eaed60SHong Zhang 
317436eaed60SHong Zhang #undef __FUNCT__
3175d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand"
31766fd0887fSHong Zhang /*@
31772c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
31786fd0887fSHong Zhang 
31796fd0887fSHong Zhang    Input Parameters:
31806fd0887fSHong Zhang +  ts - the TS context
31816fd0887fSHong Zhang .  t - current time
31820cf82b59SBarry Smith -  y - state vector, i.e. current solution
31836fd0887fSHong Zhang 
31846fd0887fSHong Zhang    Output Parameter:
3185abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
31866fd0887fSHong Zhang 
31876fd0887fSHong Zhang    Note:
31886fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
31896fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
31906fd0887fSHong Zhang 
31916fd0887fSHong Zhang    Level: developer
31926fd0887fSHong Zhang 
31936fd0887fSHong Zhang .keywords: TS, compute
31946fd0887fSHong Zhang 
3195dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
31966fd0887fSHong Zhang @*/
31970cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
31986fd0887fSHong Zhang {
31996fd0887fSHong Zhang   PetscErrorCode ierr;
32006fd0887fSHong Zhang 
32016fd0887fSHong Zhang   PetscFunctionBegin;
32026fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32030cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
32046fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
32056fd0887fSHong Zhang 
32060cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
3207e4680132SHong Zhang   if (ts->costintegrand) {
3208e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
32090cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
32106fd0887fSHong Zhang     PetscStackPop;
32116fd0887fSHong Zhang   } else {
32126fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
32136fd0887fSHong Zhang   }
32146fd0887fSHong Zhang 
32150cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
32166fd0887fSHong Zhang   PetscFunctionReturn(0);
32176fd0887fSHong Zhang }
32186fd0887fSHong Zhang 
32196fd0887fSHong Zhang #undef __FUNCT__
3220d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction"
322105755b9cSHong Zhang /*@
3222d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
322305755b9cSHong Zhang 
322405755b9cSHong Zhang   Collective on TS
322505755b9cSHong Zhang 
322605755b9cSHong Zhang   Input Parameters:
322705755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
322805755b9cSHong Zhang 
322905755b9cSHong Zhang   Notes:
3230d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
323105755b9cSHong Zhang   so most users would not generally call this routine themselves.
323205755b9cSHong Zhang 
323305755b9cSHong Zhang   Level: developer
323405755b9cSHong Zhang 
323505755b9cSHong Zhang .keywords: TS, sensitivity
3236d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
323705755b9cSHong Zhang @*/
32380cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
323905755b9cSHong Zhang {
324005755b9cSHong Zhang   PetscErrorCode ierr;
324105755b9cSHong Zhang 
324205755b9cSHong Zhang   PetscFunctionBegin;
324305755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32440cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
324505755b9cSHong Zhang 
324605755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
32470cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
324805755b9cSHong Zhang   PetscStackPop;
324905755b9cSHong Zhang   PetscFunctionReturn(0);
325005755b9cSHong Zhang }
325105755b9cSHong Zhang 
325205755b9cSHong Zhang #undef __FUNCT__
3253d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction"
325405755b9cSHong Zhang /*@
3255d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
325605755b9cSHong Zhang 
325705755b9cSHong Zhang   Collective on TS
325805755b9cSHong Zhang 
325905755b9cSHong Zhang   Input Parameters:
326005755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
326105755b9cSHong Zhang 
326205755b9cSHong Zhang   Notes:
326305755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
326405755b9cSHong Zhang   so most users would not generally call this routine themselves.
326505755b9cSHong Zhang 
326605755b9cSHong Zhang   Level: developer
326705755b9cSHong Zhang 
326805755b9cSHong Zhang .keywords: TS, sensitivity
3269d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
327005755b9cSHong Zhang @*/
32710cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
327205755b9cSHong Zhang {
327305755b9cSHong Zhang   PetscErrorCode ierr;
327405755b9cSHong Zhang 
327505755b9cSHong Zhang   PetscFunctionBegin;
327605755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32770cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
327805755b9cSHong Zhang 
327905755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
32800cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
328105755b9cSHong Zhang   PetscStackPop;
328205755b9cSHong Zhang   PetscFunctionReturn(0);
328305755b9cSHong Zhang }
328405755b9cSHong Zhang 
328505755b9cSHong Zhang #undef __FUNCT__
3286e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
3287ac226902SBarry Smith /*@C
3288000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
32893f2090d5SJed Brown   called once at the beginning of each time step.
3290000e7ae3SMatthew Knepley 
32913f9fe445SBarry Smith   Logically Collective on TS
3292000e7ae3SMatthew Knepley 
3293000e7ae3SMatthew Knepley   Input Parameters:
3294000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3295000e7ae3SMatthew Knepley - func - The function
3296000e7ae3SMatthew Knepley 
3297000e7ae3SMatthew Knepley   Calling sequence of func:
3298000e7ae3SMatthew Knepley . func (TS ts);
3299000e7ae3SMatthew Knepley 
3300000e7ae3SMatthew Knepley   Level: intermediate
3301000e7ae3SMatthew Knepley 
3302b8123daeSJed Brown   Note:
3303b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
3304b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
3305b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
3306b8123daeSJed Brown 
3307000e7ae3SMatthew Knepley .keywords: TS, timestep
33089be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
3309000e7ae3SMatthew Knepley @*/
33107087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3311000e7ae3SMatthew Knepley {
3312000e7ae3SMatthew Knepley   PetscFunctionBegin;
33130700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3314ae60f76fSBarry Smith   ts->prestep = func;
3315000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3316000e7ae3SMatthew Knepley }
3317000e7ae3SMatthew Knepley 
3318e74ef692SMatthew Knepley #undef __FUNCT__
33193f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
332009ee8438SJed Brown /*@
33213f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
33223f2090d5SJed Brown 
33233f2090d5SJed Brown   Collective on TS
33243f2090d5SJed Brown 
33253f2090d5SJed Brown   Input Parameters:
33263f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
33273f2090d5SJed Brown 
33283f2090d5SJed Brown   Notes:
33293f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
33303f2090d5SJed Brown   so most users would not generally call this routine themselves.
33313f2090d5SJed Brown 
33323f2090d5SJed Brown   Level: developer
33333f2090d5SJed Brown 
33343f2090d5SJed Brown .keywords: TS, timestep
33359be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
33363f2090d5SJed Brown @*/
33377087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
33383f2090d5SJed Brown {
33393f2090d5SJed Brown   PetscErrorCode ierr;
33403f2090d5SJed Brown 
33413f2090d5SJed Brown   PetscFunctionBegin;
33420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3343ae60f76fSBarry Smith   if (ts->prestep) {
3344ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
3345312ce896SJed Brown   }
33463f2090d5SJed Brown   PetscFunctionReturn(0);
33473f2090d5SJed Brown }
33483f2090d5SJed Brown 
33493f2090d5SJed Brown #undef __FUNCT__
3350b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
3351b8123daeSJed Brown /*@C
3352b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3353b8123daeSJed Brown   called once at the beginning of each stage.
3354b8123daeSJed Brown 
3355b8123daeSJed Brown   Logically Collective on TS
3356b8123daeSJed Brown 
3357b8123daeSJed Brown   Input Parameters:
3358b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3359b8123daeSJed Brown - func - The function
3360b8123daeSJed Brown 
3361b8123daeSJed Brown   Calling sequence of func:
3362b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3363b8123daeSJed Brown 
3364b8123daeSJed Brown   Level: intermediate
3365b8123daeSJed Brown 
3366b8123daeSJed Brown   Note:
3367b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
3368b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
3369b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3370b8123daeSJed Brown 
3371b8123daeSJed Brown .keywords: TS, timestep
33729be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3373b8123daeSJed Brown @*/
3374b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3375b8123daeSJed Brown {
3376b8123daeSJed Brown   PetscFunctionBegin;
3377b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3378ae60f76fSBarry Smith   ts->prestage = func;
3379b8123daeSJed Brown   PetscFunctionReturn(0);
3380b8123daeSJed Brown }
3381b8123daeSJed Brown 
3382b8123daeSJed Brown #undef __FUNCT__
33839be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
33849be3e283SDebojyoti Ghosh /*@C
33859be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
33869be3e283SDebojyoti Ghosh   called once at the end of each stage.
33879be3e283SDebojyoti Ghosh 
33889be3e283SDebojyoti Ghosh   Logically Collective on TS
33899be3e283SDebojyoti Ghosh 
33909be3e283SDebojyoti Ghosh   Input Parameters:
33919be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
33929be3e283SDebojyoti Ghosh - func - The function
33939be3e283SDebojyoti Ghosh 
33949be3e283SDebojyoti Ghosh   Calling sequence of func:
33959be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
33969be3e283SDebojyoti Ghosh 
33979be3e283SDebojyoti Ghosh   Level: intermediate
33989be3e283SDebojyoti Ghosh 
33999be3e283SDebojyoti Ghosh   Note:
34009be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
34019be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
34029be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
34039be3e283SDebojyoti Ghosh 
34049be3e283SDebojyoti Ghosh .keywords: TS, timestep
34059be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
34069be3e283SDebojyoti Ghosh @*/
34079be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
34089be3e283SDebojyoti Ghosh {
34099be3e283SDebojyoti Ghosh   PetscFunctionBegin;
34109be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
34119be3e283SDebojyoti Ghosh   ts->poststage = func;
34129be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
34139be3e283SDebojyoti Ghosh }
34149be3e283SDebojyoti Ghosh 
34159be3e283SDebojyoti Ghosh #undef __FUNCT__
3416c688d042SShri Abhyankar #define __FUNCT__ "TSSetPostEvaluate"
3417c688d042SShri Abhyankar /*@C
3418c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3419c688d042SShri Abhyankar   called once at the end of each step evaluation.
3420c688d042SShri Abhyankar 
3421c688d042SShri Abhyankar   Logically Collective on TS
3422c688d042SShri Abhyankar 
3423c688d042SShri Abhyankar   Input Parameters:
3424c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3425c688d042SShri Abhyankar - func - The function
3426c688d042SShri Abhyankar 
3427c688d042SShri Abhyankar   Calling sequence of func:
3428c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3429c688d042SShri Abhyankar 
3430c688d042SShri Abhyankar   Level: intermediate
3431c688d042SShri Abhyankar 
3432c688d042SShri Abhyankar   Note:
34331785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
34341785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3435e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3436e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3437e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3438c688d042SShri Abhyankar 
3439c688d042SShri Abhyankar .keywords: TS, timestep
3440c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3441c688d042SShri Abhyankar @*/
3442c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3443c688d042SShri Abhyankar {
3444c688d042SShri Abhyankar   PetscFunctionBegin;
3445c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3446c688d042SShri Abhyankar   ts->postevaluate = func;
3447c688d042SShri Abhyankar   PetscFunctionReturn(0);
3448c688d042SShri Abhyankar }
3449c688d042SShri Abhyankar 
3450c688d042SShri Abhyankar #undef __FUNCT__
3451b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
3452b8123daeSJed Brown /*@
3453b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3454b8123daeSJed Brown 
3455b8123daeSJed Brown   Collective on TS
3456b8123daeSJed Brown 
3457b8123daeSJed Brown   Input Parameters:
3458b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
34599be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3460b8123daeSJed Brown 
3461b8123daeSJed Brown   Notes:
3462b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3463b8123daeSJed Brown   most users would not generally call this routine themselves.
3464b8123daeSJed Brown 
3465b8123daeSJed Brown   Level: developer
3466b8123daeSJed Brown 
3467b8123daeSJed Brown .keywords: TS, timestep
34689be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3469b8123daeSJed Brown @*/
3470b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3471b8123daeSJed Brown {
3472b8123daeSJed Brown   PetscErrorCode ierr;
3473b8123daeSJed Brown 
3474b8123daeSJed Brown   PetscFunctionBegin;
3475b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3476ae60f76fSBarry Smith   if (ts->prestage) {
3477ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3478b8123daeSJed Brown   }
3479b8123daeSJed Brown   PetscFunctionReturn(0);
3480b8123daeSJed Brown }
3481b8123daeSJed Brown 
3482b8123daeSJed Brown #undef __FUNCT__
34839be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
34849be3e283SDebojyoti Ghosh /*@
34859be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
34869be3e283SDebojyoti Ghosh 
34879be3e283SDebojyoti Ghosh   Collective on TS
34889be3e283SDebojyoti Ghosh 
34899be3e283SDebojyoti Ghosh   Input Parameters:
34909be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
34919be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
34929be3e283SDebojyoti Ghosh   stageindex  - Stage number
34939be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
34949be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
34959be3e283SDebojyoti Ghosh 
34969be3e283SDebojyoti Ghosh   Notes:
34979be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
34989be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
34999be3e283SDebojyoti Ghosh 
35009be3e283SDebojyoti Ghosh   Level: developer
35019be3e283SDebojyoti Ghosh 
35029be3e283SDebojyoti Ghosh .keywords: TS, timestep
35039be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
35049be3e283SDebojyoti Ghosh @*/
35059be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
35069be3e283SDebojyoti Ghosh {
35079be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
35089be3e283SDebojyoti Ghosh 
35099be3e283SDebojyoti Ghosh   PetscFunctionBegin;
35109be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35114beae5d8SLisandro Dalcin   if (ts->poststage) {
35129be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
35139be3e283SDebojyoti Ghosh   }
35149be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
35159be3e283SDebojyoti Ghosh }
35169be3e283SDebojyoti Ghosh 
35179be3e283SDebojyoti Ghosh #undef __FUNCT__
3518c688d042SShri Abhyankar #define __FUNCT__ "TSPostEvaluate"
3519c688d042SShri Abhyankar /*@
3520c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3521c688d042SShri Abhyankar 
3522c688d042SShri Abhyankar   Collective on TS
3523c688d042SShri Abhyankar 
3524c688d042SShri Abhyankar   Input Parameters:
3525c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3526c688d042SShri Abhyankar 
3527c688d042SShri Abhyankar   Notes:
3528c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3529c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3530c688d042SShri Abhyankar 
3531c688d042SShri Abhyankar   Level: developer
3532c688d042SShri Abhyankar 
3533c688d042SShri Abhyankar .keywords: TS, timestep
3534c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3535c688d042SShri Abhyankar @*/
3536c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3537c688d042SShri Abhyankar {
3538c688d042SShri Abhyankar   PetscErrorCode ierr;
3539c688d042SShri Abhyankar 
3540c688d042SShri Abhyankar   PetscFunctionBegin;
3541c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3542c688d042SShri Abhyankar   if (ts->postevaluate) {
3543c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3544c688d042SShri Abhyankar   }
3545c688d042SShri Abhyankar   PetscFunctionReturn(0);
3546c688d042SShri Abhyankar }
3547c688d042SShri Abhyankar 
3548c688d042SShri Abhyankar #undef __FUNCT__
3549e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
3550ac226902SBarry Smith /*@C
3551000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
35523f2090d5SJed Brown   called once at the end of each time step.
3553000e7ae3SMatthew Knepley 
35543f9fe445SBarry Smith   Logically Collective on TS
3555000e7ae3SMatthew Knepley 
3556000e7ae3SMatthew Knepley   Input Parameters:
3557000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3558000e7ae3SMatthew Knepley - func - The function
3559000e7ae3SMatthew Knepley 
3560000e7ae3SMatthew Knepley   Calling sequence of func:
3561b8123daeSJed Brown $ func (TS ts);
3562000e7ae3SMatthew Knepley 
35631785ff2aSShri Abhyankar   Notes:
35641785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
35651785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
35661785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
35671785ff2aSShri Abhyankar 
3568000e7ae3SMatthew Knepley   Level: intermediate
3569000e7ae3SMatthew Knepley 
3570000e7ae3SMatthew Knepley .keywords: TS, timestep
35711785ff2aSShri Abhyankar .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
3572000e7ae3SMatthew Knepley @*/
35737087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3574000e7ae3SMatthew Knepley {
3575000e7ae3SMatthew Knepley   PetscFunctionBegin;
35760700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3577ae60f76fSBarry Smith   ts->poststep = func;
3578000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3579000e7ae3SMatthew Knepley }
3580000e7ae3SMatthew Knepley 
3581e74ef692SMatthew Knepley #undef __FUNCT__
35823f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
358309ee8438SJed Brown /*@
35843f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
35853f2090d5SJed Brown 
35863f2090d5SJed Brown   Collective on TS
35873f2090d5SJed Brown 
35883f2090d5SJed Brown   Input Parameters:
35893f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
35903f2090d5SJed Brown 
35913f2090d5SJed Brown   Notes:
35923f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
35933f2090d5SJed Brown   so most users would not generally call this routine themselves.
35943f2090d5SJed Brown 
35953f2090d5SJed Brown   Level: developer
35963f2090d5SJed Brown 
35973f2090d5SJed Brown .keywords: TS, timestep
35983f2090d5SJed Brown @*/
35997087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
36003f2090d5SJed Brown {
36013f2090d5SJed Brown   PetscErrorCode ierr;
36023f2090d5SJed Brown 
36033f2090d5SJed Brown   PetscFunctionBegin;
36040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3605ae60f76fSBarry Smith   if (ts->poststep) {
3606ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
360772ac3e02SJed Brown   }
36083f2090d5SJed Brown   PetscFunctionReturn(0);
36093f2090d5SJed Brown }
36103f2090d5SJed Brown 
3611d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3612d763cef2SBarry Smith 
36134a2ae208SSatish Balay #undef __FUNCT__
3614a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
3615d763cef2SBarry Smith /*@C
3616a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3617d763cef2SBarry Smith    timestep to display the iteration's  progress.
3618d763cef2SBarry Smith 
36193f9fe445SBarry Smith    Logically Collective on TS
3620d763cef2SBarry Smith 
3621d763cef2SBarry Smith    Input Parameters:
3622d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3623e213d8f1SJed Brown .  monitor - monitoring routine
3624329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
36250298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3626b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
36270298fd71SBarry Smith           (may be NULL)
3628d763cef2SBarry Smith 
3629e213d8f1SJed Brown    Calling sequence of monitor:
36300910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3631d763cef2SBarry Smith 
3632d763cef2SBarry Smith +    ts - the TS context
363363e21af5SBarry 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)
36341f06c33eSBarry Smith .    time - current time
36350910c330SBarry Smith .    u - current iterate
3636d763cef2SBarry Smith -    mctx - [optional] monitoring context
3637d763cef2SBarry Smith 
3638d763cef2SBarry Smith    Notes:
3639d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3640d763cef2SBarry Smith    already has been loaded.
3641d763cef2SBarry Smith 
3642025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
3643025f1a04SBarry Smith 
3644d763cef2SBarry Smith    Level: intermediate
3645d763cef2SBarry Smith 
3646d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3647d763cef2SBarry Smith 
3648a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3649d763cef2SBarry Smith @*/
3650c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3651d763cef2SBarry Smith {
365278064530SBarry Smith   PetscErrorCode ierr;
365378064530SBarry Smith   PetscInt       i;
365478064530SBarry Smith   PetscBool      identical;
365578064530SBarry Smith 
3656d763cef2SBarry Smith   PetscFunctionBegin;
36570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
365878064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
365978064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
366078064530SBarry Smith     if (identical) PetscFunctionReturn(0);
366178064530SBarry Smith   }
366217186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3663d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
36648704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3665d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3666d763cef2SBarry Smith   PetscFunctionReturn(0);
3667d763cef2SBarry Smith }
3668d763cef2SBarry Smith 
36694a2ae208SSatish Balay #undef __FUNCT__
3670a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
3671d763cef2SBarry Smith /*@C
3672a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3673d763cef2SBarry Smith 
36743f9fe445SBarry Smith    Logically Collective on TS
3675d763cef2SBarry Smith 
3676d763cef2SBarry Smith    Input Parameters:
3677d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3678d763cef2SBarry Smith 
3679d763cef2SBarry Smith    Notes:
3680d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3681d763cef2SBarry Smith 
3682d763cef2SBarry Smith    Level: intermediate
3683d763cef2SBarry Smith 
3684d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3685d763cef2SBarry Smith 
3686a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3687d763cef2SBarry Smith @*/
36887087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3689d763cef2SBarry Smith {
3690d952e501SBarry Smith   PetscErrorCode ierr;
3691d952e501SBarry Smith   PetscInt       i;
3692d952e501SBarry Smith 
3693d763cef2SBarry Smith   PetscFunctionBegin;
36940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3695d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
36968704b422SBarry Smith     if (ts->monitordestroy[i]) {
36978704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3698d952e501SBarry Smith     }
3699d952e501SBarry Smith   }
3700d763cef2SBarry Smith   ts->numbermonitors = 0;
3701d763cef2SBarry Smith   PetscFunctionReturn(0);
3702d763cef2SBarry Smith }
3703d763cef2SBarry Smith 
37044a2ae208SSatish Balay #undef __FUNCT__
3705a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
3706721cd6eeSBarry Smith /*@C
3707721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
37085516499fSSatish Balay 
37095516499fSSatish Balay    Level: intermediate
371041251cbbSSatish Balay 
37115516499fSSatish Balay .keywords: TS, set, monitor
37125516499fSSatish Balay 
371363e21af5SBarry Smith .seealso:  TSMonitorSet()
371441251cbbSSatish Balay @*/
3715721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3716d763cef2SBarry Smith {
3717dfbe8321SBarry Smith   PetscErrorCode ierr;
3718721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
371941aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3720d132466eSBarry Smith 
3721d763cef2SBarry Smith   PetscFunctionBegin;
37224d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
372341aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
372441aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3725721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
372641aca3d6SBarry Smith   if (iascii) {
3727649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
372863e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
372963e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
373063e21af5SBarry Smith     } else {
37318392e04aSShri 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);
373263e21af5SBarry Smith     }
3733649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
373441aca3d6SBarry Smith   } else if (ibinary) {
373541aca3d6SBarry Smith     PetscMPIInt rank;
373641aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
373741aca3d6SBarry Smith     if (!rank) {
3738450a797fSBarry Smith       PetscBool skipHeader;
3739450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3740450a797fSBarry Smith 
3741450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3742450a797fSBarry Smith       if (!skipHeader) {
3743450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3744450a797fSBarry Smith        }
374541aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
374641aca3d6SBarry Smith     } else {
374741aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
374841aca3d6SBarry Smith     }
374941aca3d6SBarry Smith   }
3750721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3751d763cef2SBarry Smith   PetscFunctionReturn(0);
3752d763cef2SBarry Smith }
3753d763cef2SBarry Smith 
37544a2ae208SSatish Balay #undef __FUNCT__
37559110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet"
37569110b2e7SHong Zhang /*@C
37579110b2e7SHong Zhang    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
37589110b2e7SHong Zhang    timestep to display the iteration's  progress.
37599110b2e7SHong Zhang 
37609110b2e7SHong Zhang    Logically Collective on TS
37619110b2e7SHong Zhang 
37629110b2e7SHong Zhang    Input Parameters:
37639110b2e7SHong Zhang +  ts - the TS context obtained from TSCreate()
37649110b2e7SHong Zhang .  adjointmonitor - monitoring routine
37659110b2e7SHong Zhang .  adjointmctx - [optional] user-defined context for private data for the
37669110b2e7SHong Zhang              monitor routine (use NULL if no context is desired)
37679110b2e7SHong Zhang -  adjointmonitordestroy - [optional] routine that frees monitor context
37689110b2e7SHong Zhang           (may be NULL)
37699110b2e7SHong Zhang 
37709110b2e7SHong Zhang    Calling sequence of monitor:
37719110b2e7SHong Zhang $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)
37729110b2e7SHong Zhang 
37739110b2e7SHong Zhang +    ts - the TS context
37749110b2e7SHong 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
37759110b2e7SHong Zhang                                been interpolated to)
37769110b2e7SHong Zhang .    time - current time
37779110b2e7SHong Zhang .    u - current iterate
37789110b2e7SHong Zhang .    numcost - number of cost functionos
37799110b2e7SHong Zhang .    lambda - sensitivities to initial conditions
37809110b2e7SHong Zhang .    mu - sensitivities to parameters
37819110b2e7SHong Zhang -    adjointmctx - [optional] adjoint monitoring context
37829110b2e7SHong Zhang 
37839110b2e7SHong Zhang    Notes:
37849110b2e7SHong Zhang    This routine adds an additional monitor to the list of monitors that
37859110b2e7SHong Zhang    already has been loaded.
37869110b2e7SHong Zhang 
37879110b2e7SHong Zhang    Fortran notes: Only a single monitor function can be set for each TS object
37889110b2e7SHong Zhang 
37899110b2e7SHong Zhang    Level: intermediate
37909110b2e7SHong Zhang 
37919110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
37929110b2e7SHong Zhang 
37939110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel()
37949110b2e7SHong Zhang @*/
37959110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
37969110b2e7SHong Zhang {
379778064530SBarry Smith   PetscErrorCode ierr;
379878064530SBarry Smith   PetscInt       i;
379978064530SBarry Smith   PetscBool      identical;
380078064530SBarry Smith 
38019110b2e7SHong Zhang   PetscFunctionBegin;
38029110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
380378064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
380478064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr);
380578064530SBarry Smith     if (identical) PetscFunctionReturn(0);
380678064530SBarry Smith   }
38079110b2e7SHong Zhang   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
38089110b2e7SHong Zhang   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
38099110b2e7SHong Zhang   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
38109110b2e7SHong Zhang   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
38119110b2e7SHong Zhang   PetscFunctionReturn(0);
38129110b2e7SHong Zhang }
38139110b2e7SHong Zhang 
38149110b2e7SHong Zhang #undef __FUNCT__
38159110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel"
38169110b2e7SHong Zhang /*@C
38179110b2e7SHong Zhang    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.
38189110b2e7SHong Zhang 
38199110b2e7SHong Zhang    Logically Collective on TS
38209110b2e7SHong Zhang 
38219110b2e7SHong Zhang    Input Parameters:
38229110b2e7SHong Zhang .  ts - the TS context obtained from TSCreate()
38239110b2e7SHong Zhang 
38249110b2e7SHong Zhang    Notes:
38259110b2e7SHong Zhang    There is no way to remove a single, specific monitor.
38269110b2e7SHong Zhang 
38279110b2e7SHong Zhang    Level: intermediate
38289110b2e7SHong Zhang 
38299110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
38309110b2e7SHong Zhang 
38319110b2e7SHong Zhang .seealso: TSAdjointMonitorSet()
38329110b2e7SHong Zhang @*/
38339110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorCancel(TS ts)
38349110b2e7SHong Zhang {
38359110b2e7SHong Zhang   PetscErrorCode ierr;
38369110b2e7SHong Zhang   PetscInt       i;
38379110b2e7SHong Zhang 
38389110b2e7SHong Zhang   PetscFunctionBegin;
38399110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38409110b2e7SHong Zhang   for (i=0; i<ts->numberadjointmonitors; i++) {
38419110b2e7SHong Zhang     if (ts->adjointmonitordestroy[i]) {
38429110b2e7SHong Zhang       ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
38439110b2e7SHong Zhang     }
38449110b2e7SHong Zhang   }
38459110b2e7SHong Zhang   ts->numberadjointmonitors = 0;
38469110b2e7SHong Zhang   PetscFunctionReturn(0);
38479110b2e7SHong Zhang }
38489110b2e7SHong Zhang 
38499110b2e7SHong Zhang #undef __FUNCT__
3850110eb670SHong Zhang #define __FUNCT__ "TSAdjointMonitorDefault"
3851721cd6eeSBarry Smith /*@C
3852721cd6eeSBarry Smith    TSAdjointMonitorDefault - the default monitor of adjoint computations
3853110eb670SHong Zhang 
3854110eb670SHong Zhang    Level: intermediate
3855110eb670SHong Zhang 
3856110eb670SHong Zhang .keywords: TS, set, monitor
3857110eb670SHong Zhang 
3858110eb670SHong Zhang .seealso: TSAdjointMonitorSet()
3859110eb670SHong Zhang @*/
3860721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
3861110eb670SHong Zhang {
3862110eb670SHong Zhang   PetscErrorCode ierr;
3863721cd6eeSBarry Smith   PetscViewer    viewer = vf->viewer;
3864110eb670SHong Zhang 
3865110eb670SHong Zhang   PetscFunctionBegin;
3866110eb670SHong Zhang   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3867721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3868110eb670SHong Zhang   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3869110eb670SHong 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);
3870110eb670SHong Zhang   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3871721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3872110eb670SHong Zhang   PetscFunctionReturn(0);
3873110eb670SHong Zhang }
3874110eb670SHong Zhang 
3875110eb670SHong Zhang #undef __FUNCT__
3876cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
3877cd652676SJed Brown /*@
3878cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3879cd652676SJed Brown 
3880cd652676SJed Brown    Collective on TS
3881cd652676SJed Brown 
3882cd652676SJed Brown    Input Argument:
3883cd652676SJed Brown +  ts - time stepping context
3884cd652676SJed Brown -  t - time to interpolate to
3885cd652676SJed Brown 
3886cd652676SJed Brown    Output Argument:
38870910c330SBarry Smith .  U - state at given time
3888cd652676SJed Brown 
3889cd652676SJed Brown    Level: intermediate
3890cd652676SJed Brown 
3891cd652676SJed Brown    Developer Notes:
3892cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3893cd652676SJed Brown 
3894cd652676SJed Brown .keywords: TS, set
3895cd652676SJed Brown 
3896874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3897cd652676SJed Brown @*/
38980910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3899cd652676SJed Brown {
3900cd652676SJed Brown   PetscErrorCode ierr;
3901cd652676SJed Brown 
3902cd652676SJed Brown   PetscFunctionBegin;
3903cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3904b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3905be5899b3SLisandro 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);
3906ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
39070910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3908cd652676SJed Brown   PetscFunctionReturn(0);
3909cd652676SJed Brown }
3910cd652676SJed Brown 
3911cd652676SJed Brown #undef __FUNCT__
39124a2ae208SSatish Balay #define __FUNCT__ "TSStep"
3913d763cef2SBarry Smith /*@
39146d9e5789SSean Farley    TSStep - Steps one time step
3915d763cef2SBarry Smith 
3916d763cef2SBarry Smith    Collective on TS
3917d763cef2SBarry Smith 
3918d763cef2SBarry Smith    Input Parameter:
3919d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3920d763cef2SBarry Smith 
392127829d71SBarry Smith    Level: developer
3922d763cef2SBarry Smith 
3923b8123daeSJed Brown    Notes:
392427829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
392527829d71SBarry Smith 
3926b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3927b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3928b8123daeSJed Brown 
392925cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
393025cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
393125cb2221SBarry Smith 
3932d763cef2SBarry Smith .keywords: TS, timestep, solve
3933d763cef2SBarry Smith 
39349be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3935d763cef2SBarry Smith @*/
3936193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3937d763cef2SBarry Smith {
3938dfbe8321SBarry Smith   PetscErrorCode   ierr;
3939fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3940be5899b3SLisandro Dalcin   PetscReal        ptime;
3941d763cef2SBarry Smith 
3942d763cef2SBarry Smith   PetscFunctionBegin;
39430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3944fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3945fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3946fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3947fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3948fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3949fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3950302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3951fffbeea8SBarry Smith 
3952d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
395368bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3954d405a339SMatthew Knepley 
3955a6772fa2SLisandro 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()");
3956be5899b3SLisandro 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");
3957a6772fa2SLisandro Dalcin 
3958be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3959362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3960be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
3961e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3962d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3963193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3964d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3965be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
3966be5899b3SLisandro Dalcin   ts->steps++; ts->total_steps++;
3967be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
396828d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3969362cd11cSLisandro Dalcin 
3970362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3971cef5090cSJed Brown     if (ts->errorifstepfailed) {
397208c7845fSBarry 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]);
397308c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3974d2daff3dSHong Zhang     }
397508c7845fSBarry Smith   } else if (!ts->reason) {
397608c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
397708c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
397808c7845fSBarry Smith   }
397908c7845fSBarry Smith   PetscFunctionReturn(0);
398008c7845fSBarry Smith }
398108c7845fSBarry Smith 
398208c7845fSBarry Smith #undef __FUNCT__
398308c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep"
398408c7845fSBarry Smith /*@
3985b957a604SHong Zhang    TSAdjointStep - Steps one time step backward in the adjoint run
398608c7845fSBarry Smith 
398708c7845fSBarry Smith    Collective on TS
398808c7845fSBarry Smith 
398908c7845fSBarry Smith    Input Parameter:
399008c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
399108c7845fSBarry Smith 
39926a4d4014SLisandro Dalcin    Level: intermediate
39936a4d4014SLisandro Dalcin 
3994b957a604SHong Zhang .keywords: TS, adjoint, step
39956a4d4014SLisandro Dalcin 
3996b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve()
39976a4d4014SLisandro Dalcin @*/
399808c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
39996a4d4014SLisandro Dalcin {
400008c7845fSBarry Smith   DM               dm;
40016a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
40026a4d4014SLisandro Dalcin 
40036a4d4014SLisandro Dalcin   PetscFunctionBegin;
40044a2ae208SSatish Balay   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
400508c7845fSBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
400608c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
4007d763cef2SBarry Smith 
4008685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr);
4009d763cef2SBarry Smith 
4010be5899b3SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
4011be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime;
401242f2b339SBarry 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);
4013be5899b3SLisandro Dalcin   ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
401442f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
40158d0ad7a8SHong Zhang   ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
4016be5899b3SLisandro Dalcin   ts->steps++; ts->total_steps--;
4017362cd11cSLisandro Dalcin 
4018362cd11cSLisandro Dalcin   if (ts->reason < 0) {
4019cef5090cSJed Brown     if (ts->errorifstepfailed) {
40206c4ed002SBarry 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]);
40216c4ed002SBarry 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]);
40226c4ed002SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
4023cef5090cSJed Brown     }
4024362cd11cSLisandro Dalcin   } else if (!ts->reason) {
402573b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
4026362cd11cSLisandro Dalcin   }
4027d763cef2SBarry Smith   PetscFunctionReturn(0);
4028d763cef2SBarry Smith }
4029d763cef2SBarry Smith 
4030d763cef2SBarry Smith #undef __FUNCT__
40317cbde773SLisandro Dalcin #define __FUNCT__ "TSEvaluateWLTE"
40327cbde773SLisandro Dalcin /*@
40337cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
40347cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
40357cbde773SLisandro Dalcin 
40367cbde773SLisandro Dalcin    Collective on TS
40377cbde773SLisandro Dalcin 
40387cbde773SLisandro Dalcin    Input Arguments:
40397cbde773SLisandro Dalcin +  ts - time stepping context
40407cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
40417cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
40427cbde773SLisandro Dalcin 
40437cbde773SLisandro Dalcin    Output Arguments:
40447cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
40457cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
40467cbde773SLisandro Dalcin 
40477cbde773SLisandro Dalcin    Level: advanced
40487cbde773SLisandro Dalcin 
40497cbde773SLisandro Dalcin    Notes:
40507cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
40517cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
40527cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
40537cbde773SLisandro Dalcin 
40547cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
40557cbde773SLisandro Dalcin @*/
40567cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
40577cbde773SLisandro Dalcin {
40587cbde773SLisandro Dalcin   PetscErrorCode ierr;
40597cbde773SLisandro Dalcin 
40607cbde773SLisandro Dalcin   PetscFunctionBegin;
40617cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40627cbde773SLisandro Dalcin   PetscValidType(ts,1);
40637cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
40647cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
40657cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
40667cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
40677cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
40687cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
40697cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
40707cbde773SLisandro Dalcin   PetscFunctionReturn(0);
40717cbde773SLisandro Dalcin }
40727cbde773SLisandro Dalcin 
40737cbde773SLisandro Dalcin #undef __FUNCT__
407405175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
407505175c85SJed Brown /*@
407605175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
407705175c85SJed Brown 
40781c3436cfSJed Brown    Collective on TS
407905175c85SJed Brown 
408005175c85SJed Brown    Input Arguments:
40811c3436cfSJed Brown +  ts - time stepping context
40821c3436cfSJed Brown .  order - desired order of accuracy
40830298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
408405175c85SJed Brown 
408505175c85SJed Brown    Output Arguments:
40860910c330SBarry Smith .  U - state at the end of the current step
408705175c85SJed Brown 
408805175c85SJed Brown    Level: advanced
408905175c85SJed Brown 
4090108c343cSJed Brown    Notes:
4091108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
4092108c343cSJed 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.
4093108c343cSJed Brown 
40941c3436cfSJed Brown .seealso: TSStep(), TSAdapt
409505175c85SJed Brown @*/
40960910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
409705175c85SJed Brown {
409805175c85SJed Brown   PetscErrorCode ierr;
409905175c85SJed Brown 
410005175c85SJed Brown   PetscFunctionBegin;
410105175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
410205175c85SJed Brown   PetscValidType(ts,1);
41030910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4104ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
41050910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
410605175c85SJed Brown   PetscFunctionReturn(0);
410705175c85SJed Brown }
410805175c85SJed Brown 
4109b1cb36f3SHong Zhang #undef __FUNCT__
4110b1cb36f3SHong Zhang #define __FUNCT__ "TSForwardCostIntegral"
4111b1cb36f3SHong Zhang /*@
4112b1cb36f3SHong Zhang  TSForwardCostIntegral - Evaluate the cost integral in the forward run.
4113b1cb36f3SHong Zhang 
4114b1cb36f3SHong Zhang  Collective on TS
4115b1cb36f3SHong Zhang 
4116b1cb36f3SHong Zhang  Input Arguments:
4117b1cb36f3SHong Zhang  .  ts - time stepping context
4118b1cb36f3SHong Zhang 
4119b1cb36f3SHong Zhang  Level: advanced
4120b1cb36f3SHong Zhang 
4121b1cb36f3SHong Zhang  Notes:
4122b1cb36f3SHong Zhang  This function cannot be called until TSStep() has been completed.
4123b1cb36f3SHong Zhang 
4124b1cb36f3SHong Zhang  .seealso: TSSolve(), TSAdjointCostIntegral()
4125b1cb36f3SHong Zhang  @*/
4126b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts)
4127b1cb36f3SHong Zhang {
4128b1cb36f3SHong Zhang     PetscErrorCode ierr;
4129b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4130b1cb36f3SHong 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);
4131b1cb36f3SHong Zhang     ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr);
4132b1cb36f3SHong Zhang     PetscFunctionReturn(0);
4133b1cb36f3SHong Zhang }
4134d67e68b7SBarry Smith 
413505175c85SJed Brown #undef __FUNCT__
4136d763cef2SBarry Smith #define __FUNCT__ "TSSolve"
4137d763cef2SBarry Smith /*@
4138d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
4139d763cef2SBarry Smith 
4140d763cef2SBarry Smith    Collective on TS
4141d763cef2SBarry Smith 
4142d763cef2SBarry Smith    Input Parameter:
4143d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
414463e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
414563e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
41465a3a76d0SJed Brown 
4147d763cef2SBarry Smith    Level: beginner
4148d763cef2SBarry Smith 
41495a3a76d0SJed Brown    Notes:
41505a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
41515a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
41525a3a76d0SJed Brown    stepped over the final time.
41535a3a76d0SJed Brown 
4154d763cef2SBarry Smith .keywords: TS, timestep, solve
4155d763cef2SBarry Smith 
415663e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
4157d763cef2SBarry Smith @*/
4158cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
4159d763cef2SBarry Smith {
4160b06615a5SLisandro Dalcin   Vec               solution;
4161d763cef2SBarry Smith   PetscErrorCode    ierr;
4162d763cef2SBarry Smith 
4163d763cef2SBarry Smith   PetscFunctionBegin;
4164d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4165f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4166feed9e9dSBarry Smith 
416749354f04SShri 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 */
4168b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
41690910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4170b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4171b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4172b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
41735a3a76d0SJed Brown     }
41740910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4175bbd56ea5SKarl Rupp   } else if (u) {
41760910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
41775a3a76d0SJed Brown   }
4178b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
417968bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4180a6772fa2SLisandro Dalcin 
4181a6772fa2SLisandro 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()");
4182a6772fa2SLisandro 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");
4183a6772fa2SLisandro Dalcin 
4184*e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4185*e7069c78SShri      restarts the step after an event. Resetting these counters in such a case causes
4186*e7069c78SShri      TSTrajectory to incorrectly save the output files
4187*e7069c78SShri   */
4188*e7069c78SShri   if(!ts->steprestart) {
4189193ac0bcSJed Brown     ts->steps       = 0;
4190*e7069c78SShri     ts->total_steps = 0;
4191*e7069c78SShri   }
41925ef26d82SJed Brown   ts->ksp_its           = 0;
41935ef26d82SJed Brown   ts->snes_its          = 0;
4194c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
4195c610991cSLisandro Dalcin   ts->reject            = 0;
4196193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
4197193ac0bcSJed Brown 
4198ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4199f05ece33SBarry Smith 
4200193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4201193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4202a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4203cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4204a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4205be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4206*e7069c78SShri     if(!ts->steprestart) {
4207db4deed7SKarl Rupp       if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
4208db4deed7SKarl Rupp       else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4209*e7069c78SShri     }
4210*e7069c78SShri 
4211cdf0de3dSShri Abhyankar     ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
42126427ac75SLisandro Dalcin     ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
4213*e7069c78SShri 
421428d5b5d6SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
421528d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_TRUE;
42166427ac75SLisandro Dalcin 
4217e1a7a14fSJed Brown     while (!ts->reason) {
4218193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
42199687d888SLisandro Dalcin       if (!ts->steprollback) {
42209687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
42219687d888SLisandro Dalcin       }
4222193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4223e783b05fSHong 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. */
4224b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
4225b1cb36f3SHong Zhang       }
422610b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4227e783b05fSHong 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. */
4228e783b05fSHong Zhang       if (!ts->steprollback) {
4229cdf0de3dSShri Abhyankar         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
42301eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4231aeb4809dSShri Abhyankar       }
4232193ac0bcSJed Brown     }
423363e21af5SBarry Smith     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
42346427ac75SLisandro Dalcin 
423549354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
42360910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4237cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4238b06615a5SLisandro Dalcin       solution = u;
423963e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
42400574a7fbSJed Brown     } else {
4241ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4242cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4243b06615a5SLisandro Dalcin       solution = ts->vec_sol;
42440574a7fbSJed Brown     }
4245193ac0bcSJed Brown   }
4246d2daff3dSHong Zhang 
4247ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
424863e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
424956f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4250ef222394SBarry Smith   if (ts->adjoint_solve) {
4251ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
42522b0a91c0SBarry Smith   }
4253d763cef2SBarry Smith   PetscFunctionReturn(0);
4254d763cef2SBarry Smith }
4255d763cef2SBarry Smith 
4256d763cef2SBarry Smith #undef __FUNCT__
4257b1cb36f3SHong Zhang #define __FUNCT__ "TSAdjointCostIntegral"
4258b1cb36f3SHong Zhang /*@
4259b1cb36f3SHong Zhang  TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run.
4260b1cb36f3SHong Zhang 
4261b1cb36f3SHong Zhang  Collective on TS
4262b1cb36f3SHong Zhang 
4263b1cb36f3SHong Zhang  Input Arguments:
4264b1cb36f3SHong Zhang  .  ts - time stepping context
4265b1cb36f3SHong Zhang 
4266b1cb36f3SHong Zhang  Level: advanced
4267b1cb36f3SHong Zhang 
4268b1cb36f3SHong Zhang  Notes:
4269b1cb36f3SHong Zhang  This function cannot be called until TSAdjointStep() has been completed.
4270b1cb36f3SHong Zhang 
4271b1cb36f3SHong Zhang  .seealso: TSAdjointSolve(), TSAdjointStep
4272b1cb36f3SHong Zhang  @*/
4273b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts)
4274b1cb36f3SHong Zhang {
4275b1cb36f3SHong Zhang     PetscErrorCode ierr;
4276b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4277b1cb36f3SHong 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);
4278b1cb36f3SHong Zhang     ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr);
4279b1cb36f3SHong Zhang     PetscFunctionReturn(0);
4280b1cb36f3SHong Zhang }
4281b1cb36f3SHong Zhang 
4282b1cb36f3SHong Zhang #undef __FUNCT__
4283c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve"
4284c88f2d44SBarry Smith /*@
4285c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
4286c88f2d44SBarry Smith 
4287c88f2d44SBarry Smith    Collective on TS
4288c88f2d44SBarry Smith 
4289c88f2d44SBarry Smith    Input Parameter:
42902c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
4291c88f2d44SBarry Smith 
4292e87fd094SBarry Smith    Options Database:
4293e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions
4294e87fd094SBarry Smith 
4295c88f2d44SBarry Smith    Level: intermediate
4296c88f2d44SBarry Smith 
4297c88f2d44SBarry Smith    Notes:
4298c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
4299c88f2d44SBarry Smith 
430073b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
430173b18844SBarry Smith 
4302c88f2d44SBarry Smith .keywords: TS, timestep, solve
4303c88f2d44SBarry Smith 
4304b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
4305c88f2d44SBarry Smith @*/
43062c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
4307c88f2d44SBarry Smith {
4308c88f2d44SBarry Smith   PetscErrorCode    ierr;
4309c88f2d44SBarry Smith 
4310c88f2d44SBarry Smith   PetscFunctionBegin;
4311c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4312c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
431368bece0bSHong Zhang 
4314c88f2d44SBarry Smith   /* reset time step and iteration counters */
4315c88f2d44SBarry Smith   ts->steps             = 0;
4316c88f2d44SBarry Smith   ts->ksp_its           = 0;
4317c88f2d44SBarry Smith   ts->snes_its          = 0;
4318c88f2d44SBarry Smith   ts->num_snes_failures = 0;
4319c88f2d44SBarry Smith   ts->reject            = 0;
4320c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
4321c88f2d44SBarry Smith 
432273b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
4323c88f2d44SBarry Smith 
432473b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
4325c88f2d44SBarry Smith   while (!ts->reason) {
432655c52731SHong Zhang     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
43279110b2e7SHong Zhang     ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
43286427ac75SLisandro Dalcin     ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr);
4329616946c1SHong Zhang     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
4330b1cb36f3SHong Zhang     if (ts->vec_costintegral && !ts->costintegralfwd) {
4331b1cb36f3SHong Zhang       ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr);
4332b1cb36f3SHong Zhang     }
4333c88f2d44SBarry Smith   }
433426656371SHong Zhang   ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
433526656371SHong Zhang   ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
4336c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
4337e210cd0eSHong Zhang   ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr);
4338685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr);
4339c88f2d44SBarry Smith   PetscFunctionReturn(0);
4340c88f2d44SBarry Smith }
4341c88f2d44SBarry Smith 
4342c88f2d44SBarry Smith #undef __FUNCT__
4343d763cef2SBarry Smith #define __FUNCT__ "TSMonitor"
43447db568b7SBarry Smith /*@C
4345228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4346228d79bcSJed Brown 
4347228d79bcSJed Brown    Collective on TS
4348228d79bcSJed Brown 
4349228d79bcSJed Brown    Input Parameters:
4350228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4351228d79bcSJed Brown .  step - step number that has just completed
4352228d79bcSJed Brown .  ptime - model time of the state
43530910c330SBarry Smith -  u - state at the current model time
4354228d79bcSJed Brown 
4355228d79bcSJed Brown    Notes:
43567db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
43577db568b7SBarry Smith    Users would almost never call this routine directly.
4358228d79bcSJed Brown 
435963e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
436063e21af5SBarry Smith 
43617db568b7SBarry Smith    Level: developer
4362228d79bcSJed Brown 
4363228d79bcSJed Brown .keywords: TS, timestep
4364228d79bcSJed Brown @*/
43650910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4366d763cef2SBarry Smith {
4367d6152f81SLisandro Dalcin   DM             dm;
4368a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4369d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4370d763cef2SBarry Smith 
4371d763cef2SBarry Smith   PetscFunctionBegin;
4372b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4373b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4374d6152f81SLisandro Dalcin 
4375d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4376d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4377d6152f81SLisandro Dalcin 
43785edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
4379d763cef2SBarry Smith   for (i=0; i<n; i++) {
43800910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4381d763cef2SBarry Smith   }
43825edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
4383d763cef2SBarry Smith   PetscFunctionReturn(0);
4384d763cef2SBarry Smith }
4385d763cef2SBarry Smith 
43869110b2e7SHong Zhang #undef __FUNCT__
43879110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor"
43887db568b7SBarry Smith /*@C
43899110b2e7SHong Zhang    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()
43909110b2e7SHong Zhang 
43919110b2e7SHong Zhang    Collective on TS
43929110b2e7SHong Zhang 
43939110b2e7SHong Zhang    Input Parameters:
43949110b2e7SHong Zhang +  ts - time stepping context obtained from TSCreate()
43959110b2e7SHong Zhang .  step - step number that has just completed
43969110b2e7SHong Zhang .  ptime - model time of the state
43979110b2e7SHong Zhang .  u - state at the current model time
43989110b2e7SHong Zhang .  numcost - number of cost functions (dimension of lambda  or mu)
43999110b2e7SHong Zhang .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
44009110b2e7SHong Zhang -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
44019110b2e7SHong Zhang 
44029110b2e7SHong Zhang    Notes:
44037db568b7SBarry Smith    TSAdjointMonitor() is typically used automatically within the time stepping implementations.
44047db568b7SBarry Smith    Users would almost never call this routine directly.
44059110b2e7SHong Zhang 
44067db568b7SBarry Smith    Level: developer
44079110b2e7SHong Zhang 
44089110b2e7SHong Zhang .keywords: TS, timestep
44099110b2e7SHong Zhang @*/
44109110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
44119110b2e7SHong Zhang {
44129110b2e7SHong Zhang   PetscErrorCode ierr;
44139110b2e7SHong Zhang   PetscInt       i,n = ts->numberadjointmonitors;
44149110b2e7SHong Zhang 
44159110b2e7SHong Zhang   PetscFunctionBegin;
44169110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44179110b2e7SHong Zhang   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
44189110b2e7SHong Zhang   ierr = VecLockPush(u);CHKERRQ(ierr);
44199110b2e7SHong Zhang   for (i=0; i<n; i++) {
44209110b2e7SHong Zhang     ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
44219110b2e7SHong Zhang   }
44229110b2e7SHong Zhang   ierr = VecLockPop(u);CHKERRQ(ierr);
44239110b2e7SHong Zhang   PetscFunctionReturn(0);
44249110b2e7SHong Zhang }
44259110b2e7SHong Zhang 
4426d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
44274a2ae208SSatish Balay #undef __FUNCT__
4428a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
4429d763cef2SBarry Smith /*@C
44307db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4431a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4432d763cef2SBarry Smith 
4433d763cef2SBarry Smith    Collective on TS
4434d763cef2SBarry Smith 
4435d763cef2SBarry Smith    Input Parameters:
4436d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4437d763cef2SBarry Smith .  label - the title to put in the title bar
44387c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4439a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4440a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4441d763cef2SBarry Smith 
4442d763cef2SBarry Smith    Output Parameter:
44430b039ecaSBarry Smith .  ctx - the context
4444d763cef2SBarry Smith 
4445d763cef2SBarry Smith    Options Database Key:
44464f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
44477db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
44487db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
44497db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
44507db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4451b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4452d763cef2SBarry Smith 
4453d763cef2SBarry Smith    Notes:
4454a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4455d763cef2SBarry Smith 
44567db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
44577db568b7SBarry Smith 
44587db568b7SBarry 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
44597db568b7SBarry 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
44607db568b7SBarry Smith    as the first argument.
44617db568b7SBarry Smith 
44627db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
44637db568b7SBarry Smith 
44647db568b7SBarry Smith 
4465d763cef2SBarry Smith    Level: intermediate
4466d763cef2SBarry Smith 
44677db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
4468d763cef2SBarry Smith 
44697db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
44707db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
44717db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
44727db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
44737db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
44747c922b88SBarry Smith 
4475d763cef2SBarry Smith @*/
4476a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4477d763cef2SBarry Smith {
44787f52daa2SLisandro Dalcin   PetscDraw      draw;
4479dfbe8321SBarry Smith   PetscErrorCode ierr;
4480d763cef2SBarry Smith 
4481d763cef2SBarry Smith   PetscFunctionBegin;
4482b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
44837f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
44847f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
44857f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4486287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
44877f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4488a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4489d763cef2SBarry Smith   PetscFunctionReturn(0);
4490d763cef2SBarry Smith }
4491d763cef2SBarry Smith 
44924a2ae208SSatish Balay #undef __FUNCT__
44934f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
4494b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4495d763cef2SBarry Smith {
44960b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4497c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4498dfbe8321SBarry Smith   PetscErrorCode ierr;
4499d763cef2SBarry Smith 
4500d763cef2SBarry Smith   PetscFunctionBegin;
450163e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4502b06615a5SLisandro Dalcin   if (!step) {
4503a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4504a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
45056934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr);
4506a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4507a9f9c1f6SBarry Smith   }
4508c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
45090b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4510b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
45110b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
45126934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
45133923b477SBarry Smith   }
4514d763cef2SBarry Smith   PetscFunctionReturn(0);
4515d763cef2SBarry Smith }
4516d763cef2SBarry Smith 
45174a2ae208SSatish Balay #undef __FUNCT__
4518a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
4519d763cef2SBarry Smith /*@C
4520a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4521a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4522d763cef2SBarry Smith 
45230b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4524d763cef2SBarry Smith 
4525d763cef2SBarry Smith    Input Parameter:
45260b039ecaSBarry Smith .  ctx - the monitor context
4527d763cef2SBarry Smith 
4528d763cef2SBarry Smith    Level: intermediate
4529d763cef2SBarry Smith 
4530d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
4531d763cef2SBarry Smith 
45324f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4533d763cef2SBarry Smith @*/
4534a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4535d763cef2SBarry Smith {
4536dfbe8321SBarry Smith   PetscErrorCode ierr;
4537d763cef2SBarry Smith 
4538d763cef2SBarry Smith   PetscFunctionBegin;
45397684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
45407684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
45417684fa3eSBarry Smith   }
45420b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
454331152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4544387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4545387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4546387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
45470b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4548d763cef2SBarry Smith   PetscFunctionReturn(0);
4549d763cef2SBarry Smith }
4550d763cef2SBarry Smith 
45514a2ae208SSatish Balay #undef __FUNCT__
45524a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
4553d763cef2SBarry Smith /*@
4554b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4555d763cef2SBarry Smith 
4556d763cef2SBarry Smith    Not Collective
4557d763cef2SBarry Smith 
4558d763cef2SBarry Smith    Input Parameter:
4559d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4560d763cef2SBarry Smith 
4561d763cef2SBarry Smith    Output Parameter:
456263e21af5SBarry Smith .  t  - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime().
4563d763cef2SBarry Smith 
4564d763cef2SBarry Smith    Level: beginner
4565d763cef2SBarry Smith 
4566b8123daeSJed Brown    Note:
4567b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
45689be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4569b8123daeSJed Brown 
457063e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime()
4571d763cef2SBarry Smith 
4572d763cef2SBarry Smith .keywords: TS, get, time
4573d763cef2SBarry Smith @*/
45747087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4575d763cef2SBarry Smith {
4576d763cef2SBarry Smith   PetscFunctionBegin;
45770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4578f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4579d763cef2SBarry Smith   *t = ts->ptime;
4580d763cef2SBarry Smith   PetscFunctionReturn(0);
4581d763cef2SBarry Smith }
4582d763cef2SBarry Smith 
45834a2ae208SSatish Balay #undef __FUNCT__
4584e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
4585e5e524a1SHong Zhang /*@
4586e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4587e5e524a1SHong Zhang 
4588e5e524a1SHong Zhang    Not Collective
4589e5e524a1SHong Zhang 
4590e5e524a1SHong Zhang    Input Parameter:
4591e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4592e5e524a1SHong Zhang 
4593e5e524a1SHong Zhang    Output Parameter:
4594e5e524a1SHong Zhang .  t  - the previous time
4595e5e524a1SHong Zhang 
4596e5e524a1SHong Zhang    Level: beginner
4597e5e524a1SHong Zhang 
4598e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
4599e5e524a1SHong Zhang 
4600e5e524a1SHong Zhang .keywords: TS, get, time
4601e5e524a1SHong Zhang @*/
4602e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4603e5e524a1SHong Zhang {
4604e5e524a1SHong Zhang   PetscFunctionBegin;
4605e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4606e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4607e5e524a1SHong Zhang   *t = ts->ptime_prev;
4608e5e524a1SHong Zhang   PetscFunctionReturn(0);
4609e5e524a1SHong Zhang }
4610e5e524a1SHong Zhang 
4611e5e524a1SHong Zhang #undef __FUNCT__
46126a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
46136a4d4014SLisandro Dalcin /*@
46146a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
46156a4d4014SLisandro Dalcin 
46163f9fe445SBarry Smith    Logically Collective on TS
46176a4d4014SLisandro Dalcin 
46186a4d4014SLisandro Dalcin    Input Parameters:
46196a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
46206a4d4014SLisandro Dalcin -  time - the time
46216a4d4014SLisandro Dalcin 
46226a4d4014SLisandro Dalcin    Level: intermediate
46236a4d4014SLisandro Dalcin 
46246a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
46256a4d4014SLisandro Dalcin 
46266a4d4014SLisandro Dalcin .keywords: TS, set, time
46276a4d4014SLisandro Dalcin @*/
46287087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
46296a4d4014SLisandro Dalcin {
46306a4d4014SLisandro Dalcin   PetscFunctionBegin;
46310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4632c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
46336a4d4014SLisandro Dalcin   ts->ptime = t;
46346a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
46356a4d4014SLisandro Dalcin }
46366a4d4014SLisandro Dalcin 
46376a4d4014SLisandro Dalcin #undef __FUNCT__
46384a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
4639d763cef2SBarry Smith /*@C
4640d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4641d763cef2SBarry Smith    TS options in the database.
4642d763cef2SBarry Smith 
46433f9fe445SBarry Smith    Logically Collective on TS
4644d763cef2SBarry Smith 
4645d763cef2SBarry Smith    Input Parameter:
4646d763cef2SBarry Smith +  ts     - The TS context
4647d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4648d763cef2SBarry Smith 
4649d763cef2SBarry Smith    Notes:
4650d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4651d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4652d763cef2SBarry Smith    hyphen.
4653d763cef2SBarry Smith 
4654d763cef2SBarry Smith    Level: advanced
4655d763cef2SBarry Smith 
4656d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4657d763cef2SBarry Smith 
4658d763cef2SBarry Smith .seealso: TSSetFromOptions()
4659d763cef2SBarry Smith 
4660d763cef2SBarry Smith @*/
46617087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4662d763cef2SBarry Smith {
4663dfbe8321SBarry Smith   PetscErrorCode ierr;
4664089b2837SJed Brown   SNES           snes;
4665d763cef2SBarry Smith 
4666d763cef2SBarry Smith   PetscFunctionBegin;
46670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4668d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4669089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4670089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4671d763cef2SBarry Smith   PetscFunctionReturn(0);
4672d763cef2SBarry Smith }
4673d763cef2SBarry Smith 
4674d763cef2SBarry Smith 
46754a2ae208SSatish Balay #undef __FUNCT__
46764a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
4677d763cef2SBarry Smith /*@C
4678d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4679d763cef2SBarry Smith    TS options in the database.
4680d763cef2SBarry Smith 
46813f9fe445SBarry Smith    Logically Collective on TS
4682d763cef2SBarry Smith 
4683d763cef2SBarry Smith    Input Parameter:
4684d763cef2SBarry Smith +  ts     - The TS context
4685d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4686d763cef2SBarry Smith 
4687d763cef2SBarry Smith    Notes:
4688d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4689d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4690d763cef2SBarry Smith    hyphen.
4691d763cef2SBarry Smith 
4692d763cef2SBarry Smith    Level: advanced
4693d763cef2SBarry Smith 
4694d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4695d763cef2SBarry Smith 
4696d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4697d763cef2SBarry Smith 
4698d763cef2SBarry Smith @*/
46997087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4700d763cef2SBarry Smith {
4701dfbe8321SBarry Smith   PetscErrorCode ierr;
4702089b2837SJed Brown   SNES           snes;
4703d763cef2SBarry Smith 
4704d763cef2SBarry Smith   PetscFunctionBegin;
47050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4706d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4707089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4708089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4709d763cef2SBarry Smith   PetscFunctionReturn(0);
4710d763cef2SBarry Smith }
4711d763cef2SBarry Smith 
47124a2ae208SSatish Balay #undef __FUNCT__
47134a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
4714d763cef2SBarry Smith /*@C
4715d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4716d763cef2SBarry Smith    TS options in the database.
4717d763cef2SBarry Smith 
4718d763cef2SBarry Smith    Not Collective
4719d763cef2SBarry Smith 
4720d763cef2SBarry Smith    Input Parameter:
4721d763cef2SBarry Smith .  ts - The TS context
4722d763cef2SBarry Smith 
4723d763cef2SBarry Smith    Output Parameter:
4724d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4725d763cef2SBarry Smith 
4726d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
4727d763cef2SBarry Smith    sufficient length to hold the prefix.
4728d763cef2SBarry Smith 
4729d763cef2SBarry Smith    Level: intermediate
4730d763cef2SBarry Smith 
4731d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4732d763cef2SBarry Smith 
4733d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4734d763cef2SBarry Smith @*/
47357087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4736d763cef2SBarry Smith {
4737dfbe8321SBarry Smith   PetscErrorCode ierr;
4738d763cef2SBarry Smith 
4739d763cef2SBarry Smith   PetscFunctionBegin;
47400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
47414482741eSBarry Smith   PetscValidPointer(prefix,2);
4742d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4743d763cef2SBarry Smith   PetscFunctionReturn(0);
4744d763cef2SBarry Smith }
4745d763cef2SBarry Smith 
47464a2ae208SSatish Balay #undef __FUNCT__
47474a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
4748d763cef2SBarry Smith /*@C
4749d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4750d763cef2SBarry Smith 
4751d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4752d763cef2SBarry Smith 
4753d763cef2SBarry Smith    Input Parameter:
4754d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4755d763cef2SBarry Smith 
4756d763cef2SBarry Smith    Output Parameters:
4757e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4758e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4759e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4760e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4761d763cef2SBarry Smith 
47620298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
4763d763cef2SBarry Smith 
4764d763cef2SBarry Smith    Level: intermediate
4765d763cef2SBarry Smith 
476626d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
4767d763cef2SBarry Smith 
4768d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4769d763cef2SBarry Smith @*/
4770e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4771d763cef2SBarry Smith {
4772089b2837SJed Brown   PetscErrorCode ierr;
4773089b2837SJed Brown   SNES           snes;
477424989b8cSPeter Brune   DM             dm;
4775089b2837SJed Brown 
4776d763cef2SBarry Smith   PetscFunctionBegin;
4777089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4778e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
477924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
478024989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4781d763cef2SBarry Smith   PetscFunctionReturn(0);
4782d763cef2SBarry Smith }
4783d763cef2SBarry Smith 
47841713a123SBarry Smith #undef __FUNCT__
47852eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
47862eca1d9cSJed Brown /*@C
47872eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
47882eca1d9cSJed Brown 
47892eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
47902eca1d9cSJed Brown 
47912eca1d9cSJed Brown    Input Parameter:
47922eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
47932eca1d9cSJed Brown 
47942eca1d9cSJed Brown    Output Parameters:
4795e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4796e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
47972eca1d9cSJed Brown .  f   - The function to compute the matrices
47982eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
47992eca1d9cSJed Brown 
48000298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
48012eca1d9cSJed Brown 
48022eca1d9cSJed Brown    Level: advanced
48032eca1d9cSJed Brown 
48042eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
48052eca1d9cSJed Brown 
48062eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
48072eca1d9cSJed Brown @*/
4808e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
48092eca1d9cSJed Brown {
4810089b2837SJed Brown   PetscErrorCode ierr;
4811089b2837SJed Brown   SNES           snes;
481224989b8cSPeter Brune   DM             dm;
48130910c330SBarry Smith 
48142eca1d9cSJed Brown   PetscFunctionBegin;
4815089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4816f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4817e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
481824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
481924989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
48202eca1d9cSJed Brown   PetscFunctionReturn(0);
48212eca1d9cSJed Brown }
48222eca1d9cSJed Brown 
48236083293cSBarry Smith 
48242eca1d9cSJed Brown #undef __FUNCT__
482583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
48261713a123SBarry Smith /*@C
482783a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
48281713a123SBarry Smith    VecView() for the solution at each timestep
48291713a123SBarry Smith 
48301713a123SBarry Smith    Collective on TS
48311713a123SBarry Smith 
48321713a123SBarry Smith    Input Parameters:
48331713a123SBarry Smith +  ts - the TS context
48341713a123SBarry Smith .  step - current time-step
4835142b95e3SSatish Balay .  ptime - current time
48360298fd71SBarry Smith -  dummy - either a viewer or NULL
48371713a123SBarry Smith 
483899fdda47SBarry Smith    Options Database:
483999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
484099fdda47SBarry Smith 
4841387f4636SBarry 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
484299fdda47SBarry Smith        will look bad
484399fdda47SBarry Smith 
48441713a123SBarry Smith    Level: intermediate
48451713a123SBarry Smith 
48461713a123SBarry Smith .keywords: TS,  vector, monitor, view
48471713a123SBarry Smith 
4848a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
48491713a123SBarry Smith @*/
48500910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48511713a123SBarry Smith {
4852dfbe8321SBarry Smith   PetscErrorCode   ierr;
485383a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4854473a3ab2SBarry Smith   PetscDraw        draw;
48551713a123SBarry Smith 
48561713a123SBarry Smith   PetscFunctionBegin;
48576083293cSBarry Smith   if (!step && ictx->showinitial) {
48586083293cSBarry Smith     if (!ictx->initialsolution) {
48590910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
48601713a123SBarry Smith     }
48610910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
48626083293cSBarry Smith   }
4863b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48640dcf80beSBarry Smith 
48656083293cSBarry Smith   if (ictx->showinitial) {
48666083293cSBarry Smith     PetscReal pause;
48676083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
48686083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
48696083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
48706083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
48716083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
48726083293cSBarry Smith   }
48730910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4874473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
487551fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4876473a3ab2SBarry Smith     char      time[32];
4877473a3ab2SBarry Smith 
4878473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
487985b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4880473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4881473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
488251fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4883473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4884473a3ab2SBarry Smith   }
4885473a3ab2SBarry Smith 
48866083293cSBarry Smith   if (ictx->showinitial) {
48876083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
48886083293cSBarry Smith   }
48891713a123SBarry Smith   PetscFunctionReturn(0);
48901713a123SBarry Smith }
48911713a123SBarry Smith 
48922d5ee99bSBarry Smith #undef __FUNCT__
48939110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi"
48949110b2e7SHong Zhang /*@C
48959110b2e7SHong Zhang    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
48969110b2e7SHong Zhang    VecView() for the sensitivities to initial states at each timestep
48979110b2e7SHong Zhang 
48989110b2e7SHong Zhang    Collective on TS
48999110b2e7SHong Zhang 
49009110b2e7SHong Zhang    Input Parameters:
49019110b2e7SHong Zhang +  ts - the TS context
49029110b2e7SHong Zhang .  step - current time-step
49039110b2e7SHong Zhang .  ptime - current time
49049110b2e7SHong Zhang .  u - current state
49059110b2e7SHong Zhang .  numcost - number of cost functions
49069110b2e7SHong Zhang .  lambda - sensitivities to initial conditions
49079110b2e7SHong Zhang .  mu - sensitivities to parameters
49089110b2e7SHong Zhang -  dummy - either a viewer or NULL
49099110b2e7SHong Zhang 
49109110b2e7SHong Zhang    Level: intermediate
49119110b2e7SHong Zhang 
49129110b2e7SHong Zhang .keywords: TS,  vector, adjoint, monitor, view
49139110b2e7SHong Zhang 
49149110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
49159110b2e7SHong Zhang @*/
49169110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
49179110b2e7SHong Zhang {
49189110b2e7SHong Zhang   PetscErrorCode   ierr;
49199110b2e7SHong Zhang   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
49209110b2e7SHong Zhang   PetscDraw        draw;
4921a0046e2cSSatish Balay   PetscReal        xl,yl,xr,yr,h;
4922a0046e2cSSatish Balay   char             time[32];
49239110b2e7SHong Zhang 
49249110b2e7SHong Zhang   PetscFunctionBegin;
49259110b2e7SHong Zhang   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
49269110b2e7SHong Zhang 
49279110b2e7SHong Zhang   ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr);
49289110b2e7SHong Zhang   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
49299110b2e7SHong Zhang   ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
49309110b2e7SHong Zhang   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
49319110b2e7SHong Zhang   h    = yl + .95*(yr - yl);
49329110b2e7SHong Zhang   ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
49339110b2e7SHong Zhang   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
49349110b2e7SHong Zhang   PetscFunctionReturn(0);
49359110b2e7SHong Zhang }
49369110b2e7SHong Zhang 
49379110b2e7SHong Zhang #undef __FUNCT__
49382d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
49392d5ee99bSBarry Smith /*@C
49402d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
49412d5ee99bSBarry Smith 
49422d5ee99bSBarry Smith    Collective on TS
49432d5ee99bSBarry Smith 
49442d5ee99bSBarry Smith    Input Parameters:
49452d5ee99bSBarry Smith +  ts - the TS context
49462d5ee99bSBarry Smith .  step - current time-step
49472d5ee99bSBarry Smith .  ptime - current time
49482d5ee99bSBarry Smith -  dummy - either a viewer or NULL
49492d5ee99bSBarry Smith 
49502d5ee99bSBarry Smith    Level: intermediate
49512d5ee99bSBarry Smith 
49522d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
49532d5ee99bSBarry Smith 
49542d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
49552d5ee99bSBarry Smith @*/
49562d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
49572d5ee99bSBarry Smith {
49582d5ee99bSBarry Smith   PetscErrorCode    ierr;
49592d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
49602d5ee99bSBarry Smith   PetscDraw         draw;
49616934998bSLisandro Dalcin   PetscDrawAxis     axis;
49622d5ee99bSBarry Smith   PetscInt          n;
49632d5ee99bSBarry Smith   PetscMPIInt       size;
49646934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
49652d5ee99bSBarry Smith   char              time[32];
49662d5ee99bSBarry Smith   const PetscScalar *U;
49672d5ee99bSBarry Smith 
49682d5ee99bSBarry Smith   PetscFunctionBegin;
49696934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
49706934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
49712d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
49726934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
49732d5ee99bSBarry Smith 
49742d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
49756934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
49766934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
49776934998bSLisandro Dalcin   if (!step) {
49786934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
49796934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
49806934998bSLisandro Dalcin   }
49812d5ee99bSBarry Smith 
49822d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
49836934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
49846934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4985a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
49866934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
49872d5ee99bSBarry Smith 
49886934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
49896934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
49902d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
49914b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
499285b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
49932d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
499451fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
49952d5ee99bSBarry Smith   }
49966934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
49972d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
49986934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
49992d5ee99bSBarry Smith   PetscFunctionReturn(0);
50002d5ee99bSBarry Smith }
50012d5ee99bSBarry Smith 
50021713a123SBarry Smith 
50036c699258SBarry Smith #undef __FUNCT__
500483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
50056083293cSBarry Smith /*@C
500683a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
50076083293cSBarry Smith 
50086083293cSBarry Smith    Collective on TS
50096083293cSBarry Smith 
50106083293cSBarry Smith    Input Parameters:
50116083293cSBarry Smith .    ctx - the monitor context
50126083293cSBarry Smith 
50136083293cSBarry Smith    Level: intermediate
50146083293cSBarry Smith 
50156083293cSBarry Smith .keywords: TS,  vector, monitor, view
50166083293cSBarry Smith 
501783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
50186083293cSBarry Smith @*/
501983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
50206083293cSBarry Smith {
50216083293cSBarry Smith   PetscErrorCode ierr;
50226083293cSBarry Smith 
50236083293cSBarry Smith   PetscFunctionBegin;
502483a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
502583a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
502683a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
50276083293cSBarry Smith   PetscFunctionReturn(0);
50286083293cSBarry Smith }
50296083293cSBarry Smith 
50306083293cSBarry Smith #undef __FUNCT__
503183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
50326083293cSBarry Smith /*@C
503383a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
50346083293cSBarry Smith 
50356083293cSBarry Smith    Collective on TS
50366083293cSBarry Smith 
50376083293cSBarry Smith    Input Parameter:
50386083293cSBarry Smith .    ts - time-step context
50396083293cSBarry Smith 
50406083293cSBarry Smith    Output Patameter:
50416083293cSBarry Smith .    ctx - the monitor context
50426083293cSBarry Smith 
504399fdda47SBarry Smith    Options Database:
504499fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
504599fdda47SBarry Smith 
50466083293cSBarry Smith    Level: intermediate
50476083293cSBarry Smith 
50486083293cSBarry Smith .keywords: TS,  vector, monitor, view
50496083293cSBarry Smith 
505083a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
50516083293cSBarry Smith @*/
505283a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
50536083293cSBarry Smith {
50546083293cSBarry Smith   PetscErrorCode   ierr;
50556083293cSBarry Smith 
50566083293cSBarry Smith   PetscFunctionBegin;
5057b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
505883a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
5059e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
5060bbd56ea5SKarl Rupp 
506183a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
5062473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
5063c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
5064473a3ab2SBarry Smith 
5065473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
5066c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
50676083293cSBarry Smith   PetscFunctionReturn(0);
50686083293cSBarry Smith }
50696083293cSBarry Smith 
50706083293cSBarry Smith #undef __FUNCT__
507183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
50723a471f94SBarry Smith /*@C
507383a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
50743a471f94SBarry Smith    VecView() for the error at each timestep
50753a471f94SBarry Smith 
50763a471f94SBarry Smith    Collective on TS
50773a471f94SBarry Smith 
50783a471f94SBarry Smith    Input Parameters:
50793a471f94SBarry Smith +  ts - the TS context
50803a471f94SBarry Smith .  step - current time-step
50813a471f94SBarry Smith .  ptime - current time
50820298fd71SBarry Smith -  dummy - either a viewer or NULL
50833a471f94SBarry Smith 
50843a471f94SBarry Smith    Level: intermediate
50853a471f94SBarry Smith 
50863a471f94SBarry Smith .keywords: TS,  vector, monitor, view
50873a471f94SBarry Smith 
50883a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
50893a471f94SBarry Smith @*/
50900910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
50913a471f94SBarry Smith {
50923a471f94SBarry Smith   PetscErrorCode   ierr;
509383a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
509483a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
50953a471f94SBarry Smith   Vec              work;
50963a471f94SBarry Smith 
50973a471f94SBarry Smith   PetscFunctionBegin;
5098b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
50990910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
51003a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
51010910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
51023a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
51033a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
51043a471f94SBarry Smith   PetscFunctionReturn(0);
51053a471f94SBarry Smith }
51063a471f94SBarry Smith 
5107af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
51083a471f94SBarry Smith #undef __FUNCT__
51096c699258SBarry Smith #define __FUNCT__ "TSSetDM"
51106c699258SBarry Smith /*@
51112a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
51126c699258SBarry Smith 
51133f9fe445SBarry Smith    Logically Collective on TS and DM
51146c699258SBarry Smith 
51156c699258SBarry Smith    Input Parameters:
51162a808120SBarry Smith +  ts - the ODE integrator object
51172a808120SBarry Smith -  dm - the dm, cannot be NULL
51186c699258SBarry Smith 
51196c699258SBarry Smith    Level: intermediate
51206c699258SBarry Smith 
51216c699258SBarry Smith 
51226c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
51236c699258SBarry Smith @*/
51247087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
51256c699258SBarry Smith {
51266c699258SBarry Smith   PetscErrorCode ierr;
5127089b2837SJed Brown   SNES           snes;
5128942e3340SBarry Smith   DMTS           tsdm;
51296c699258SBarry Smith 
51306c699258SBarry Smith   PetscFunctionBegin;
51310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
51322a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
513370663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
5134942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
51352a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
5136942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
5137942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
513824989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
513924989b8cSPeter Brune         tsdm->originaldm = dm;
514024989b8cSPeter Brune       }
514124989b8cSPeter Brune     }
51426bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
514324989b8cSPeter Brune   }
51446c699258SBarry Smith   ts->dm = dm;
5145bbd56ea5SKarl Rupp 
5146089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5147089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
51486c699258SBarry Smith   PetscFunctionReturn(0);
51496c699258SBarry Smith }
51506c699258SBarry Smith 
51516c699258SBarry Smith #undef __FUNCT__
51526c699258SBarry Smith #define __FUNCT__ "TSGetDM"
51536c699258SBarry Smith /*@
51546c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
51556c699258SBarry Smith 
51563f9fe445SBarry Smith    Not Collective
51576c699258SBarry Smith 
51586c699258SBarry Smith    Input Parameter:
51596c699258SBarry Smith . ts - the preconditioner context
51606c699258SBarry Smith 
51616c699258SBarry Smith    Output Parameter:
51626c699258SBarry Smith .  dm - the dm
51636c699258SBarry Smith 
51646c699258SBarry Smith    Level: intermediate
51656c699258SBarry Smith 
51666c699258SBarry Smith 
51676c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
51686c699258SBarry Smith @*/
51697087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
51706c699258SBarry Smith {
5171496e6a7aSJed Brown   PetscErrorCode ierr;
5172496e6a7aSJed Brown 
51736c699258SBarry Smith   PetscFunctionBegin;
51740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5175496e6a7aSJed Brown   if (!ts->dm) {
5176ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
5177496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
5178496e6a7aSJed Brown   }
51796c699258SBarry Smith   *dm = ts->dm;
51806c699258SBarry Smith   PetscFunctionReturn(0);
51816c699258SBarry Smith }
51821713a123SBarry Smith 
51830f5c6efeSJed Brown #undef __FUNCT__
51840f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
51850f5c6efeSJed Brown /*@
51860f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
51870f5c6efeSJed Brown 
51883f9fe445SBarry Smith    Logically Collective on SNES
51890f5c6efeSJed Brown 
51900f5c6efeSJed Brown    Input Parameter:
5191d42a1c89SJed Brown + snes - nonlinear solver
51920910c330SBarry Smith . U - the current state at which to evaluate the residual
5193d42a1c89SJed Brown - ctx - user context, must be a TS
51940f5c6efeSJed Brown 
51950f5c6efeSJed Brown    Output Parameter:
51960f5c6efeSJed Brown . F - the nonlinear residual
51970f5c6efeSJed Brown 
51980f5c6efeSJed Brown    Notes:
51990f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
52000f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
52010f5c6efeSJed Brown 
52020f5c6efeSJed Brown    Level: advanced
52030f5c6efeSJed Brown 
52040f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
52050f5c6efeSJed Brown @*/
52060910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
52070f5c6efeSJed Brown {
52080f5c6efeSJed Brown   TS             ts = (TS)ctx;
52090f5c6efeSJed Brown   PetscErrorCode ierr;
52100f5c6efeSJed Brown 
52110f5c6efeSJed Brown   PetscFunctionBegin;
52120f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
52130910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
52140f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
52150f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
52160910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
52170f5c6efeSJed Brown   PetscFunctionReturn(0);
52180f5c6efeSJed Brown }
52190f5c6efeSJed Brown 
52200f5c6efeSJed Brown #undef __FUNCT__
52210f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
52220f5c6efeSJed Brown /*@
52230f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
52240f5c6efeSJed Brown 
52250f5c6efeSJed Brown    Collective on SNES
52260f5c6efeSJed Brown 
52270f5c6efeSJed Brown    Input Parameter:
52280f5c6efeSJed Brown + snes - nonlinear solver
52290910c330SBarry Smith . U - the current state at which to evaluate the residual
52300f5c6efeSJed Brown - ctx - user context, must be a TS
52310f5c6efeSJed Brown 
52320f5c6efeSJed Brown    Output Parameter:
52330f5c6efeSJed Brown + A - the Jacobian
52340f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
52350f5c6efeSJed Brown - flag - indicates any structure change in the matrix
52360f5c6efeSJed Brown 
52370f5c6efeSJed Brown    Notes:
52380f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
52390f5c6efeSJed Brown 
52400f5c6efeSJed Brown    Level: developer
52410f5c6efeSJed Brown 
52420f5c6efeSJed Brown .seealso: SNESSetJacobian()
52430f5c6efeSJed Brown @*/
5244d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
52450f5c6efeSJed Brown {
52460f5c6efeSJed Brown   TS             ts = (TS)ctx;
52470f5c6efeSJed Brown   PetscErrorCode ierr;
52480f5c6efeSJed Brown 
52490f5c6efeSJed Brown   PetscFunctionBegin;
52500f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
52510910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
52520f5c6efeSJed Brown   PetscValidPointer(A,3);
525394ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
52540f5c6efeSJed Brown   PetscValidPointer(B,4);
525594ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
52560f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5257d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
52580f5c6efeSJed Brown   PetscFunctionReturn(0);
52590f5c6efeSJed Brown }
5260325fc9f4SBarry Smith 
52610e4ef248SJed Brown #undef __FUNCT__
52620e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
52630e4ef248SJed Brown /*@C
52649ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
52650e4ef248SJed Brown 
52660e4ef248SJed Brown    Collective on TS
52670e4ef248SJed Brown 
52680e4ef248SJed Brown    Input Arguments:
52690e4ef248SJed Brown +  ts - time stepping context
52700e4ef248SJed Brown .  t - time at which to evaluate
52710910c330SBarry Smith .  U - state at which to evaluate
52720e4ef248SJed Brown -  ctx - context
52730e4ef248SJed Brown 
52740e4ef248SJed Brown    Output Arguments:
52750e4ef248SJed Brown .  F - right hand side
52760e4ef248SJed Brown 
52770e4ef248SJed Brown    Level: intermediate
52780e4ef248SJed Brown 
52790e4ef248SJed Brown    Notes:
52800e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
52810e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
52820e4ef248SJed Brown 
52830e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
52840e4ef248SJed Brown @*/
52850910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
52860e4ef248SJed Brown {
52870e4ef248SJed Brown   PetscErrorCode ierr;
52880e4ef248SJed Brown   Mat            Arhs,Brhs;
52890e4ef248SJed Brown 
52900e4ef248SJed Brown   PetscFunctionBegin;
52910e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
5292d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
52930910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
52940e4ef248SJed Brown   PetscFunctionReturn(0);
52950e4ef248SJed Brown }
52960e4ef248SJed Brown 
52970e4ef248SJed Brown #undef __FUNCT__
52980e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
52990e4ef248SJed Brown /*@C
53000e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
53010e4ef248SJed Brown 
53020e4ef248SJed Brown    Collective on TS
53030e4ef248SJed Brown 
53040e4ef248SJed Brown    Input Arguments:
53050e4ef248SJed Brown +  ts - time stepping context
53060e4ef248SJed Brown .  t - time at which to evaluate
53070910c330SBarry Smith .  U - state at which to evaluate
53080e4ef248SJed Brown -  ctx - context
53090e4ef248SJed Brown 
53100e4ef248SJed Brown    Output Arguments:
53110e4ef248SJed Brown +  A - pointer to operator
53120e4ef248SJed Brown .  B - pointer to preconditioning matrix
53130e4ef248SJed Brown -  flg - matrix structure flag
53140e4ef248SJed Brown 
53150e4ef248SJed Brown    Level: intermediate
53160e4ef248SJed Brown 
53170e4ef248SJed Brown    Notes:
53180e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
53190e4ef248SJed Brown 
53200e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
53210e4ef248SJed Brown @*/
5322d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
53230e4ef248SJed Brown {
53240e4ef248SJed Brown   PetscFunctionBegin;
53250e4ef248SJed Brown   PetscFunctionReturn(0);
53260e4ef248SJed Brown }
53270e4ef248SJed Brown 
53280026cea9SSean Farley #undef __FUNCT__
53290026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
53300026cea9SSean Farley /*@C
53310026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
53320026cea9SSean Farley 
53330026cea9SSean Farley    Collective on TS
53340026cea9SSean Farley 
53350026cea9SSean Farley    Input Arguments:
53360026cea9SSean Farley +  ts - time stepping context
53370026cea9SSean Farley .  t - time at which to evaluate
53380910c330SBarry Smith .  U - state at which to evaluate
53390910c330SBarry Smith .  Udot - time derivative of state vector
53400026cea9SSean Farley -  ctx - context
53410026cea9SSean Farley 
53420026cea9SSean Farley    Output Arguments:
53430026cea9SSean Farley .  F - left hand side
53440026cea9SSean Farley 
53450026cea9SSean Farley    Level: intermediate
53460026cea9SSean Farley 
53470026cea9SSean Farley    Notes:
53480910c330SBarry 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
53490026cea9SSean Farley    user is required to write their own TSComputeIFunction.
53500026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
53510026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
53520026cea9SSean Farley 
53539ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
53549ae8fd06SBarry Smith 
53559ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
53560026cea9SSean Farley @*/
53570910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
53580026cea9SSean Farley {
53590026cea9SSean Farley   PetscErrorCode ierr;
53600026cea9SSean Farley   Mat            A,B;
53610026cea9SSean Farley 
53620026cea9SSean Farley   PetscFunctionBegin;
53630298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5364d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
53650910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
53660026cea9SSean Farley   PetscFunctionReturn(0);
53670026cea9SSean Farley }
53680026cea9SSean Farley 
53690026cea9SSean Farley #undef __FUNCT__
53700026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
53710026cea9SSean Farley /*@C
5372b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
53730026cea9SSean Farley 
53740026cea9SSean Farley    Collective on TS
53750026cea9SSean Farley 
53760026cea9SSean Farley    Input Arguments:
53770026cea9SSean Farley +  ts - time stepping context
53780026cea9SSean Farley .  t - time at which to evaluate
53790910c330SBarry Smith .  U - state at which to evaluate
53800910c330SBarry Smith .  Udot - time derivative of state vector
53810026cea9SSean Farley .  shift - shift to apply
53820026cea9SSean Farley -  ctx - context
53830026cea9SSean Farley 
53840026cea9SSean Farley    Output Arguments:
53850026cea9SSean Farley +  A - pointer to operator
53860026cea9SSean Farley .  B - pointer to preconditioning matrix
53870026cea9SSean Farley -  flg - matrix structure flag
53880026cea9SSean Farley 
5389b41af12eSJed Brown    Level: advanced
53900026cea9SSean Farley 
53910026cea9SSean Farley    Notes:
53920026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
53930026cea9SSean Farley 
5394b41af12eSJed Brown    It is only appropriate for problems of the form
5395b41af12eSJed Brown 
5396b41af12eSJed Brown $     M Udot = F(U,t)
5397b41af12eSJed Brown 
5398b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5399b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5400b41af12eSJed Brown   an implicit operator of the form
5401b41af12eSJed Brown 
5402b41af12eSJed Brown $    shift*M + J
5403b41af12eSJed Brown 
5404b41af12eSJed 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
5405b41af12eSJed Brown   a copy of M or reassemble it when requested.
5406b41af12eSJed Brown 
54070026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
54080026cea9SSean Farley @*/
5409d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
54100026cea9SSean Farley {
5411b41af12eSJed Brown   PetscErrorCode ierr;
5412b41af12eSJed Brown 
54130026cea9SSean Farley   PetscFunctionBegin;
541494ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5415b41af12eSJed Brown   ts->ijacobian.shift = shift;
54160026cea9SSean Farley   PetscFunctionReturn(0);
54170026cea9SSean Farley }
5418b41af12eSJed Brown 
5419e817cc15SEmil Constantinescu #undef __FUNCT__
5420e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
5421e817cc15SEmil Constantinescu /*@
5422e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5423e817cc15SEmil Constantinescu 
5424e817cc15SEmil Constantinescu    Not Collective
5425e817cc15SEmil Constantinescu 
5426e817cc15SEmil Constantinescu    Input Parameter:
5427e817cc15SEmil Constantinescu .  ts - the TS context
5428e817cc15SEmil Constantinescu 
5429e817cc15SEmil Constantinescu    Output Parameter:
54304e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5431e817cc15SEmil Constantinescu 
5432e817cc15SEmil Constantinescu    Level: beginner
5433e817cc15SEmil Constantinescu 
5434e817cc15SEmil Constantinescu .keywords: TS, equation type
5435e817cc15SEmil Constantinescu 
5436e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5437e817cc15SEmil Constantinescu @*/
5438e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5439e817cc15SEmil Constantinescu {
5440e817cc15SEmil Constantinescu   PetscFunctionBegin;
5441e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5442e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5443e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5444e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5445e817cc15SEmil Constantinescu }
5446e817cc15SEmil Constantinescu 
5447e817cc15SEmil Constantinescu #undef __FUNCT__
5448e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
5449e817cc15SEmil Constantinescu /*@
5450e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5451e817cc15SEmil Constantinescu 
5452e817cc15SEmil Constantinescu    Not Collective
5453e817cc15SEmil Constantinescu 
5454e817cc15SEmil Constantinescu    Input Parameter:
5455e817cc15SEmil Constantinescu +  ts - the TS context
54561297b224SEmil Constantinescu -  equation_type - see TSEquationType
5457e817cc15SEmil Constantinescu 
5458e817cc15SEmil Constantinescu    Level: advanced
5459e817cc15SEmil Constantinescu 
5460e817cc15SEmil Constantinescu .keywords: TS, equation type
5461e817cc15SEmil Constantinescu 
5462e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5463e817cc15SEmil Constantinescu @*/
5464e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5465e817cc15SEmil Constantinescu {
5466e817cc15SEmil Constantinescu   PetscFunctionBegin;
5467e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5468e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5469e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5470e817cc15SEmil Constantinescu }
54710026cea9SSean Farley 
54724af1b03aSJed Brown #undef __FUNCT__
54734af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
54744af1b03aSJed Brown /*@
54754af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
54764af1b03aSJed Brown 
54774af1b03aSJed Brown    Not Collective
54784af1b03aSJed Brown 
54794af1b03aSJed Brown    Input Parameter:
54804af1b03aSJed Brown .  ts - the TS context
54814af1b03aSJed Brown 
54824af1b03aSJed Brown    Output Parameter:
54834af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
54844af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
54854af1b03aSJed Brown 
5486487e0bb9SJed Brown    Level: beginner
54874af1b03aSJed Brown 
5488cd652676SJed Brown    Notes:
5489cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
54904af1b03aSJed Brown 
54914af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
54924af1b03aSJed Brown 
54934af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
54944af1b03aSJed Brown @*/
54954af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
54964af1b03aSJed Brown {
54974af1b03aSJed Brown   PetscFunctionBegin;
54984af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54994af1b03aSJed Brown   PetscValidPointer(reason,2);
55004af1b03aSJed Brown   *reason = ts->reason;
55014af1b03aSJed Brown   PetscFunctionReturn(0);
55024af1b03aSJed Brown }
55034af1b03aSJed Brown 
5504fb1732b5SBarry Smith #undef __FUNCT__
5505d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
5506d6ad946cSShri Abhyankar /*@
5507d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5508d6ad946cSShri Abhyankar 
5509d6ad946cSShri Abhyankar    Not Collective
5510d6ad946cSShri Abhyankar 
5511d6ad946cSShri Abhyankar    Input Parameter:
5512d6ad946cSShri Abhyankar +  ts - the TS context
5513d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5514d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5515d6ad946cSShri Abhyankar 
5516f5abba47SShri Abhyankar    Level: advanced
5517d6ad946cSShri Abhyankar 
5518d6ad946cSShri Abhyankar    Notes:
5519d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
5520d6ad946cSShri Abhyankar 
5521d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
5522d6ad946cSShri Abhyankar 
5523d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5524d6ad946cSShri Abhyankar @*/
5525d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5526d6ad946cSShri Abhyankar {
5527d6ad946cSShri Abhyankar   PetscFunctionBegin;
5528d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5529d6ad946cSShri Abhyankar   ts->reason = reason;
5530d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5531d6ad946cSShri Abhyankar }
5532d6ad946cSShri Abhyankar 
5533d6ad946cSShri Abhyankar #undef __FUNCT__
5534cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
5535cc708dedSBarry Smith /*@
5536cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5537cc708dedSBarry Smith 
5538cc708dedSBarry Smith    Not Collective
5539cc708dedSBarry Smith 
5540cc708dedSBarry Smith    Input Parameter:
5541cc708dedSBarry Smith .  ts - the TS context
5542cc708dedSBarry Smith 
5543cc708dedSBarry Smith    Output Parameter:
554463e21af5SBarry Smith .  ftime - the final time. This time corresponds to the final time set with TSSetDuration()
5545cc708dedSBarry Smith 
5546487e0bb9SJed Brown    Level: beginner
5547cc708dedSBarry Smith 
5548cc708dedSBarry Smith    Notes:
5549cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5550cc708dedSBarry Smith 
5551cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
5552cc708dedSBarry Smith 
5553cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5554cc708dedSBarry Smith @*/
5555cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5556cc708dedSBarry Smith {
5557cc708dedSBarry Smith   PetscFunctionBegin;
5558cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5559cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5560cc708dedSBarry Smith   *ftime = ts->solvetime;
5561cc708dedSBarry Smith   PetscFunctionReturn(0);
5562cc708dedSBarry Smith }
5563cc708dedSBarry Smith 
5564cc708dedSBarry Smith #undef __FUNCT__
55652c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps"
55662c18e0fdSBarry Smith /*@
55672c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
55682c18e0fdSBarry Smith 
55692c18e0fdSBarry Smith    Not Collective
55702c18e0fdSBarry Smith 
55712c18e0fdSBarry Smith    Input Parameter:
55722c18e0fdSBarry Smith .  ts - the TS context
55732c18e0fdSBarry Smith 
55742c18e0fdSBarry Smith    Output Parameter:
55752c18e0fdSBarry Smith .  steps - the number of steps
55762c18e0fdSBarry Smith 
55772c18e0fdSBarry Smith    Level: beginner
55782c18e0fdSBarry Smith 
55792c18e0fdSBarry Smith    Notes:
55802c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
55812c18e0fdSBarry Smith 
55822c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
55832c18e0fdSBarry Smith 
55842c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
55852c18e0fdSBarry Smith @*/
55862c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
55872c18e0fdSBarry Smith {
55882c18e0fdSBarry Smith   PetscFunctionBegin;
55892c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
55902c18e0fdSBarry Smith   PetscValidPointer(steps,2);
55912c18e0fdSBarry Smith   *steps = ts->total_steps;
55922c18e0fdSBarry Smith   PetscFunctionReturn(0);
55932c18e0fdSBarry Smith }
55942c18e0fdSBarry Smith 
55952c18e0fdSBarry Smith #undef __FUNCT__
55965ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
55979f67acb7SJed Brown /*@
55985ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
55999f67acb7SJed Brown    used by the time integrator.
56009f67acb7SJed Brown 
56019f67acb7SJed Brown    Not Collective
56029f67acb7SJed Brown 
56039f67acb7SJed Brown    Input Parameter:
56049f67acb7SJed Brown .  ts - TS context
56059f67acb7SJed Brown 
56069f67acb7SJed Brown    Output Parameter:
56079f67acb7SJed Brown .  nits - number of nonlinear iterations
56089f67acb7SJed Brown 
56099f67acb7SJed Brown    Notes:
56109f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
56119f67acb7SJed Brown 
56129f67acb7SJed Brown    Level: intermediate
56139f67acb7SJed Brown 
56149f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
56159f67acb7SJed Brown 
56165ef26d82SJed Brown .seealso:  TSGetKSPIterations()
56179f67acb7SJed Brown @*/
56185ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
56199f67acb7SJed Brown {
56209f67acb7SJed Brown   PetscFunctionBegin;
56219f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
56229f67acb7SJed Brown   PetscValidIntPointer(nits,2);
56235ef26d82SJed Brown   *nits = ts->snes_its;
56249f67acb7SJed Brown   PetscFunctionReturn(0);
56259f67acb7SJed Brown }
56269f67acb7SJed Brown 
56279f67acb7SJed Brown #undef __FUNCT__
56285ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
56299f67acb7SJed Brown /*@
56305ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
56319f67acb7SJed Brown    used by the time integrator.
56329f67acb7SJed Brown 
56339f67acb7SJed Brown    Not Collective
56349f67acb7SJed Brown 
56359f67acb7SJed Brown    Input Parameter:
56369f67acb7SJed Brown .  ts - TS context
56379f67acb7SJed Brown 
56389f67acb7SJed Brown    Output Parameter:
56399f67acb7SJed Brown .  lits - number of linear iterations
56409f67acb7SJed Brown 
56419f67acb7SJed Brown    Notes:
56429f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
56439f67acb7SJed Brown 
56449f67acb7SJed Brown    Level: intermediate
56459f67acb7SJed Brown 
56469f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
56479f67acb7SJed Brown 
56485ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
56499f67acb7SJed Brown @*/
56505ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
56519f67acb7SJed Brown {
56529f67acb7SJed Brown   PetscFunctionBegin;
56539f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
56549f67acb7SJed Brown   PetscValidIntPointer(lits,2);
56555ef26d82SJed Brown   *lits = ts->ksp_its;
56569f67acb7SJed Brown   PetscFunctionReturn(0);
56579f67acb7SJed Brown }
56589f67acb7SJed Brown 
56599f67acb7SJed Brown #undef __FUNCT__
5660cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
5661cef5090cSJed Brown /*@
5662cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5663cef5090cSJed Brown 
5664cef5090cSJed Brown    Not Collective
5665cef5090cSJed Brown 
5666cef5090cSJed Brown    Input Parameter:
5667cef5090cSJed Brown .  ts - TS context
5668cef5090cSJed Brown 
5669cef5090cSJed Brown    Output Parameter:
5670cef5090cSJed Brown .  rejects - number of steps rejected
5671cef5090cSJed Brown 
5672cef5090cSJed Brown    Notes:
5673cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5674cef5090cSJed Brown 
5675cef5090cSJed Brown    Level: intermediate
5676cef5090cSJed Brown 
5677cef5090cSJed Brown .keywords: TS, get, number
5678cef5090cSJed Brown 
56795ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5680cef5090cSJed Brown @*/
5681cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5682cef5090cSJed Brown {
5683cef5090cSJed Brown   PetscFunctionBegin;
5684cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5685cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5686cef5090cSJed Brown   *rejects = ts->reject;
5687cef5090cSJed Brown   PetscFunctionReturn(0);
5688cef5090cSJed Brown }
5689cef5090cSJed Brown 
5690cef5090cSJed Brown #undef __FUNCT__
5691cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
5692cef5090cSJed Brown /*@
5693cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5694cef5090cSJed Brown 
5695cef5090cSJed Brown    Not Collective
5696cef5090cSJed Brown 
5697cef5090cSJed Brown    Input Parameter:
5698cef5090cSJed Brown .  ts - TS context
5699cef5090cSJed Brown 
5700cef5090cSJed Brown    Output Parameter:
5701cef5090cSJed Brown .  fails - number of failed nonlinear solves
5702cef5090cSJed Brown 
5703cef5090cSJed Brown    Notes:
5704cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5705cef5090cSJed Brown 
5706cef5090cSJed Brown    Level: intermediate
5707cef5090cSJed Brown 
5708cef5090cSJed Brown .keywords: TS, get, number
5709cef5090cSJed Brown 
57105ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5711cef5090cSJed Brown @*/
5712cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5713cef5090cSJed Brown {
5714cef5090cSJed Brown   PetscFunctionBegin;
5715cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5716cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5717cef5090cSJed Brown   *fails = ts->num_snes_failures;
5718cef5090cSJed Brown   PetscFunctionReturn(0);
5719cef5090cSJed Brown }
5720cef5090cSJed Brown 
5721cef5090cSJed Brown #undef __FUNCT__
5722cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
5723cef5090cSJed Brown /*@
5724cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5725cef5090cSJed Brown 
5726cef5090cSJed Brown    Not Collective
5727cef5090cSJed Brown 
5728cef5090cSJed Brown    Input Parameter:
5729cef5090cSJed Brown +  ts - TS context
5730cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5731cef5090cSJed Brown 
5732cef5090cSJed Brown    Notes:
5733cef5090cSJed Brown    The counter is reset to zero for each step
5734cef5090cSJed Brown 
5735cef5090cSJed Brown    Options Database Key:
5736cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5737cef5090cSJed Brown 
5738cef5090cSJed Brown    Level: intermediate
5739cef5090cSJed Brown 
5740cef5090cSJed Brown .keywords: TS, set, maximum, number
5741cef5090cSJed Brown 
57425ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5743cef5090cSJed Brown @*/
5744cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5745cef5090cSJed Brown {
5746cef5090cSJed Brown   PetscFunctionBegin;
5747cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5748cef5090cSJed Brown   ts->max_reject = rejects;
5749cef5090cSJed Brown   PetscFunctionReturn(0);
5750cef5090cSJed Brown }
5751cef5090cSJed Brown 
5752cef5090cSJed Brown #undef __FUNCT__
5753cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
5754cef5090cSJed Brown /*@
5755cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5756cef5090cSJed Brown 
5757cef5090cSJed Brown    Not Collective
5758cef5090cSJed Brown 
5759cef5090cSJed Brown    Input Parameter:
5760cef5090cSJed Brown +  ts - TS context
5761cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5762cef5090cSJed Brown 
5763cef5090cSJed Brown    Notes:
5764cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5765cef5090cSJed Brown 
5766cef5090cSJed Brown    Options Database Key:
5767cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5768cef5090cSJed Brown 
5769cef5090cSJed Brown    Level: intermediate
5770cef5090cSJed Brown 
5771cef5090cSJed Brown .keywords: TS, set, maximum, number
5772cef5090cSJed Brown 
57735ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5774cef5090cSJed Brown @*/
5775cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5776cef5090cSJed Brown {
5777cef5090cSJed Brown   PetscFunctionBegin;
5778cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5779cef5090cSJed Brown   ts->max_snes_failures = fails;
5780cef5090cSJed Brown   PetscFunctionReturn(0);
5781cef5090cSJed Brown }
5782cef5090cSJed Brown 
5783cef5090cSJed Brown #undef __FUNCT__
57844e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
5785cef5090cSJed Brown /*@
5786cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5787cef5090cSJed Brown 
5788cef5090cSJed Brown    Not Collective
5789cef5090cSJed Brown 
5790cef5090cSJed Brown    Input Parameter:
5791cef5090cSJed Brown +  ts - TS context
5792cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5793cef5090cSJed Brown 
5794cef5090cSJed Brown    Options Database Key:
5795cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5796cef5090cSJed Brown 
5797cef5090cSJed Brown    Level: intermediate
5798cef5090cSJed Brown 
5799cef5090cSJed Brown .keywords: TS, set, error
5800cef5090cSJed Brown 
58015ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5802cef5090cSJed Brown @*/
5803cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5804cef5090cSJed Brown {
5805cef5090cSJed Brown   PetscFunctionBegin;
5806cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5807cef5090cSJed Brown   ts->errorifstepfailed = err;
5808cef5090cSJed Brown   PetscFunctionReturn(0);
5809cef5090cSJed Brown }
5810cef5090cSJed Brown 
5811cef5090cSJed Brown #undef __FUNCT__
5812fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSolution"
5813fb1732b5SBarry Smith /*@C
5814fde5950dSBarry 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
5815fb1732b5SBarry Smith 
5816fb1732b5SBarry Smith    Collective on TS
5817fb1732b5SBarry Smith 
5818fb1732b5SBarry Smith    Input Parameters:
5819fb1732b5SBarry Smith +  ts - the TS context
5820fb1732b5SBarry Smith .  step - current time-step
5821fb1732b5SBarry Smith .  ptime - current time
58220910c330SBarry Smith .  u - current state
5823721cd6eeSBarry Smith -  vf - viewer and its format
5824fb1732b5SBarry Smith 
5825fb1732b5SBarry Smith    Level: intermediate
5826fb1732b5SBarry Smith 
5827fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5828fb1732b5SBarry Smith 
5829fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5830fb1732b5SBarry Smith @*/
5831721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5832fb1732b5SBarry Smith {
5833fb1732b5SBarry Smith   PetscErrorCode ierr;
5834fb1732b5SBarry Smith 
5835fb1732b5SBarry Smith   PetscFunctionBegin;
5836721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5837721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5838721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5839ed81e22dSJed Brown   PetscFunctionReturn(0);
5840ed81e22dSJed Brown }
5841ed81e22dSJed Brown 
5842ed81e22dSJed Brown #undef __FUNCT__
5843ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
5844ed81e22dSJed Brown /*@C
5845ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5846ed81e22dSJed Brown 
5847ed81e22dSJed Brown    Collective on TS
5848ed81e22dSJed Brown 
5849ed81e22dSJed Brown    Input Parameters:
5850ed81e22dSJed Brown +  ts - the TS context
5851ed81e22dSJed Brown .  step - current time-step
5852ed81e22dSJed Brown .  ptime - current time
58530910c330SBarry Smith .  u - current state
5854ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5855ed81e22dSJed Brown 
5856ed81e22dSJed Brown    Level: intermediate
5857ed81e22dSJed Brown 
5858ed81e22dSJed Brown    Notes:
5859ed81e22dSJed 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.
5860ed81e22dSJed Brown    These are named according to the file name template.
5861ed81e22dSJed Brown 
5862ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5863ed81e22dSJed Brown 
5864ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5865ed81e22dSJed Brown 
5866ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5867ed81e22dSJed Brown @*/
58680910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5869ed81e22dSJed Brown {
5870ed81e22dSJed Brown   PetscErrorCode ierr;
5871ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5872ed81e22dSJed Brown   PetscViewer    viewer;
5873ed81e22dSJed Brown 
5874ed81e22dSJed Brown   PetscFunctionBegin;
587563e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
58768caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5877ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
58780910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5879ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5880ed81e22dSJed Brown   PetscFunctionReturn(0);
5881ed81e22dSJed Brown }
5882ed81e22dSJed Brown 
5883ed81e22dSJed Brown #undef __FUNCT__
5884ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
5885ed81e22dSJed Brown /*@C
5886ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5887ed81e22dSJed Brown 
5888ed81e22dSJed Brown    Collective on TS
5889ed81e22dSJed Brown 
5890ed81e22dSJed Brown    Input Parameters:
5891ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5892ed81e22dSJed Brown 
5893ed81e22dSJed Brown    Level: intermediate
5894ed81e22dSJed Brown 
5895ed81e22dSJed Brown    Note:
5896ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5897ed81e22dSJed Brown 
5898ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5899ed81e22dSJed Brown 
5900ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5901ed81e22dSJed Brown @*/
5902ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5903ed81e22dSJed Brown {
5904ed81e22dSJed Brown   PetscErrorCode ierr;
5905ed81e22dSJed Brown 
5906ed81e22dSJed Brown   PetscFunctionBegin;
5907ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5908fb1732b5SBarry Smith   PetscFunctionReturn(0);
5909fb1732b5SBarry Smith }
5910fb1732b5SBarry Smith 
591184df9cb4SJed Brown #undef __FUNCT__
5912552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
591384df9cb4SJed Brown /*@
5914552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
591584df9cb4SJed Brown 
5916ed81e22dSJed Brown    Collective on TS if controller has not been created yet
591784df9cb4SJed Brown 
591884df9cb4SJed Brown    Input Arguments:
5919ed81e22dSJed Brown .  ts - time stepping context
592084df9cb4SJed Brown 
592184df9cb4SJed Brown    Output Arguments:
5922ed81e22dSJed Brown .  adapt - adaptive controller
592384df9cb4SJed Brown 
592484df9cb4SJed Brown    Level: intermediate
592584df9cb4SJed Brown 
5926ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
592784df9cb4SJed Brown @*/
5928552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
592984df9cb4SJed Brown {
593084df9cb4SJed Brown   PetscErrorCode ierr;
593184df9cb4SJed Brown 
593284df9cb4SJed Brown   PetscFunctionBegin;
593384df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5934bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
593584df9cb4SJed Brown   if (!ts->adapt) {
5936ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
59373bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
59381c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
593984df9cb4SJed Brown   }
5940bec58848SLisandro Dalcin   *adapt = ts->adapt;
594184df9cb4SJed Brown   PetscFunctionReturn(0);
594284df9cb4SJed Brown }
5943d6ebe24aSShri Abhyankar 
5944d6ebe24aSShri Abhyankar #undef __FUNCT__
59451c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
59461c3436cfSJed Brown /*@
59471c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
59481c3436cfSJed Brown 
59491c3436cfSJed Brown    Logically Collective
59501c3436cfSJed Brown 
59511c3436cfSJed Brown    Input Arguments:
59521c3436cfSJed Brown +  ts - time integration context
59531c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
59540298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
59551c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
59560298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
59571c3436cfSJed Brown 
5958a3cdaa26SBarry Smith    Options Database keys:
5959a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5960a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5961a3cdaa26SBarry Smith 
59623ff766beSShri Abhyankar    Notes:
59633ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
59643ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
59653ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
59663ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
59673ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
59683ff766beSShri Abhyankar    differential variables.
59693ff766beSShri Abhyankar 
59701c3436cfSJed Brown    Level: beginner
59711c3436cfSJed Brown 
5972c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
59731c3436cfSJed Brown @*/
59741c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
59751c3436cfSJed Brown {
59761c3436cfSJed Brown   PetscErrorCode ierr;
59771c3436cfSJed Brown 
59781c3436cfSJed Brown   PetscFunctionBegin;
5979c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
59801c3436cfSJed Brown   if (vatol) {
59811c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
59821c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
59831c3436cfSJed Brown     ts->vatol = vatol;
59841c3436cfSJed Brown   }
5985c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
59861c3436cfSJed Brown   if (vrtol) {
59871c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
59881c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
59891c3436cfSJed Brown     ts->vrtol = vrtol;
59901c3436cfSJed Brown   }
59911c3436cfSJed Brown   PetscFunctionReturn(0);
59921c3436cfSJed Brown }
59931c3436cfSJed Brown 
59941c3436cfSJed Brown #undef __FUNCT__
5995c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
5996c5033834SJed Brown /*@
5997c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5998c5033834SJed Brown 
5999c5033834SJed Brown    Logically Collective
6000c5033834SJed Brown 
6001c5033834SJed Brown    Input Arguments:
6002c5033834SJed Brown .  ts - time integration context
6003c5033834SJed Brown 
6004c5033834SJed Brown    Output Arguments:
60050298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
60060298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
60070298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
60080298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
6009c5033834SJed Brown 
6010c5033834SJed Brown    Level: beginner
6011c5033834SJed Brown 
6012c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
6013c5033834SJed Brown @*/
6014c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
6015c5033834SJed Brown {
6016c5033834SJed Brown   PetscFunctionBegin;
6017c5033834SJed Brown   if (atol)  *atol  = ts->atol;
6018c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
6019c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
6020c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
6021c5033834SJed Brown   PetscFunctionReturn(0);
6022c5033834SJed Brown }
6023c5033834SJed Brown 
6024c5033834SJed Brown #undef __FUNCT__
60259c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2"
60269c6b16b5SShri Abhyankar /*@
6027a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
60289c6b16b5SShri Abhyankar 
60299c6b16b5SShri Abhyankar    Collective on TS
60309c6b16b5SShri Abhyankar 
60319c6b16b5SShri Abhyankar    Input Arguments:
60329c6b16b5SShri Abhyankar +  ts - time stepping context
6033a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6034a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
60359c6b16b5SShri Abhyankar 
60369c6b16b5SShri Abhyankar    Output Arguments:
60377453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
60387453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
60397453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
60409c6b16b5SShri Abhyankar 
60419c6b16b5SShri Abhyankar    Level: developer
60429c6b16b5SShri Abhyankar 
6043deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
60449c6b16b5SShri Abhyankar @*/
60457453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60469c6b16b5SShri Abhyankar {
60479c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
60489c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
60497453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
60507453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
60519c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
60527453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
60537453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
60547453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
60559c6b16b5SShri Abhyankar 
60569c6b16b5SShri Abhyankar   PetscFunctionBegin;
60579c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6058a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
6059a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
6060a4868fbcSLisandro Dalcin   PetscValidType(U,2);
6061a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
6062a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
6063a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
60648a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
60658a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
6066a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
60679c6b16b5SShri Abhyankar 
60689c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
60699c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
60709c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
60719c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
60729c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
60737453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
60747453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
60757453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
60769c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
60779c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
60789c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60799c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60809c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
60817453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60827453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
60837453f775SEmil Constantinescu       if(tola>0.){
60847453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
60857453f775SEmil Constantinescu         na_loc++;
60867453f775SEmil Constantinescu       }
60877453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60887453f775SEmil Constantinescu       if(tolr>0.){
60897453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
60907453f775SEmil Constantinescu         nr_loc++;
60917453f775SEmil Constantinescu       }
60927453f775SEmil Constantinescu       tol=tola+tolr;
60937453f775SEmil Constantinescu       if(tol>0.){
60947453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
60957453f775SEmil Constantinescu         n_loc++;
60967453f775SEmil Constantinescu       }
60979c6b16b5SShri Abhyankar     }
60989c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60999c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61009c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
61019c6b16b5SShri Abhyankar     const PetscScalar *atol;
61029c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61039c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
61047453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
61057453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
61067453f775SEmil Constantinescu       if(tola>0.){
61077453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
61087453f775SEmil Constantinescu         na_loc++;
61097453f775SEmil Constantinescu       }
61107453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61117453f775SEmil Constantinescu       if(tolr>0.){
61127453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
61137453f775SEmil Constantinescu         nr_loc++;
61147453f775SEmil Constantinescu       }
61157453f775SEmil Constantinescu       tol=tola+tolr;
61167453f775SEmil Constantinescu       if(tol>0.){
61177453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
61187453f775SEmil Constantinescu         n_loc++;
61197453f775SEmil Constantinescu       }
61209c6b16b5SShri Abhyankar     }
61219c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61229c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
61239c6b16b5SShri Abhyankar     const PetscScalar *rtol;
61249c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61259c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
61267453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
61277453f775SEmil Constantinescu       tola = ts->atol;
61287453f775SEmil Constantinescu       if(tola>0.){
61297453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
61307453f775SEmil Constantinescu         na_loc++;
61317453f775SEmil Constantinescu       }
61327453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61337453f775SEmil Constantinescu       if(tolr>0.){
61347453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
61357453f775SEmil Constantinescu         nr_loc++;
61367453f775SEmil Constantinescu       }
61377453f775SEmil Constantinescu       tol=tola+tolr;
61387453f775SEmil Constantinescu       if(tol>0.){
61397453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
61407453f775SEmil Constantinescu         n_loc++;
61417453f775SEmil Constantinescu       }
61429c6b16b5SShri Abhyankar     }
61439c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61449c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
61459c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
61467453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
61477453f775SEmil Constantinescu      tola = ts->atol;
61487453f775SEmil Constantinescu       if(tola>0.){
61497453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
61507453f775SEmil Constantinescu         na_loc++;
61517453f775SEmil Constantinescu       }
61527453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61537453f775SEmil Constantinescu       if(tolr>0.){
61547453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
61557453f775SEmil Constantinescu         nr_loc++;
61567453f775SEmil Constantinescu       }
61577453f775SEmil Constantinescu       tol=tola+tolr;
61587453f775SEmil Constantinescu       if(tol>0.){
61597453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
61607453f775SEmil Constantinescu         n_loc++;
61617453f775SEmil Constantinescu       }
61629c6b16b5SShri Abhyankar     }
61639c6b16b5SShri Abhyankar   }
61649c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
61659c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
61669c6b16b5SShri Abhyankar 
61677453f775SEmil Constantinescu   err_loc[0] = sum;
61687453f775SEmil Constantinescu   err_loc[1] = suma;
61697453f775SEmil Constantinescu   err_loc[2] = sumr;
61707453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
61717453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
61727453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
61737453f775SEmil Constantinescu 
6174a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61757453f775SEmil Constantinescu 
61767453f775SEmil Constantinescu   gsum   = err_glb[0];
61777453f775SEmil Constantinescu   gsuma  = err_glb[1];
61787453f775SEmil Constantinescu   gsumr  = err_glb[2];
61797453f775SEmil Constantinescu   n_glb  = err_glb[3];
61807453f775SEmil Constantinescu   na_glb = err_glb[4];
61817453f775SEmil Constantinescu   nr_glb = err_glb[5];
61827453f775SEmil Constantinescu 
6183b1316ef9SEmil Constantinescu   *norm  = 0.;
6184b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
6185b1316ef9SEmil Constantinescu   *norma = 0.;
6186b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
6187b1316ef9SEmil Constantinescu   *normr = 0.;
6188b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
61899c6b16b5SShri Abhyankar 
61909c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
61917453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
61927453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
61939c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
61949c6b16b5SShri Abhyankar }
61959c6b16b5SShri Abhyankar 
61969c6b16b5SShri Abhyankar #undef __FUNCT__
61979c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity"
61989c6b16b5SShri Abhyankar /*@
6199a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
62009c6b16b5SShri Abhyankar 
62019c6b16b5SShri Abhyankar    Collective on TS
62029c6b16b5SShri Abhyankar 
62039c6b16b5SShri Abhyankar    Input Arguments:
62049c6b16b5SShri Abhyankar +  ts - time stepping context
6205a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6206a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
62079c6b16b5SShri Abhyankar 
62089c6b16b5SShri Abhyankar    Output Arguments:
62097453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
62107453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
62117453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
62129c6b16b5SShri Abhyankar 
62139c6b16b5SShri Abhyankar    Level: developer
62149c6b16b5SShri Abhyankar 
6215deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
62169c6b16b5SShri Abhyankar @*/
62177453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
62189c6b16b5SShri Abhyankar {
62199c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
62207453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
62219c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
62227453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
62237453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
62247453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
62259c6b16b5SShri Abhyankar 
62269c6b16b5SShri Abhyankar   PetscFunctionBegin;
62279c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6228a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
6229a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
6230a4868fbcSLisandro Dalcin   PetscValidType(U,2);
6231a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
6232a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
6233a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
62348a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
62358a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
6236a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
62379c6b16b5SShri Abhyankar 
62389c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
62399c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
62409c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
62419c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
62429c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
62437453f775SEmil Constantinescu 
62447453f775SEmil Constantinescu   max=0.;
62457453f775SEmil Constantinescu   maxa=0.;
62467453f775SEmil Constantinescu   maxr=0.;
62477453f775SEmil Constantinescu 
62487453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
62499c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
62509c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62519c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62527453f775SEmil Constantinescu 
62537453f775SEmil Constantinescu     for (i=0; i<n; i++) {
62547453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
62557453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
62567453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62577453f775SEmil Constantinescu       tol  = tola+tolr;
62587453f775SEmil Constantinescu       if(tola>0.){
62597453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
62607453f775SEmil Constantinescu       }
62617453f775SEmil Constantinescu       if(tolr>0.){
62627453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
62637453f775SEmil Constantinescu       }
62647453f775SEmil Constantinescu       if(tol>0.){
62657453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
62667453f775SEmil Constantinescu       }
62679c6b16b5SShri Abhyankar     }
62689c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62699c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62709c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
62719c6b16b5SShri Abhyankar     const PetscScalar *atol;
62729c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62737453f775SEmil Constantinescu     for (i=0; i<n; i++) {
62747453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
62757453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
62767453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62777453f775SEmil Constantinescu       tol  = tola+tolr;
62787453f775SEmil Constantinescu       if(tola>0.){
62797453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
62807453f775SEmil Constantinescu       }
62817453f775SEmil Constantinescu       if(tolr>0.){
62827453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
62837453f775SEmil Constantinescu       }
62847453f775SEmil Constantinescu       if(tol>0.){
62857453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
62867453f775SEmil Constantinescu       }
62879c6b16b5SShri Abhyankar     }
62889c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62899c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
62909c6b16b5SShri Abhyankar     const PetscScalar *rtol;
62919c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62927453f775SEmil Constantinescu 
62937453f775SEmil Constantinescu     for (i=0; i<n; i++) {
62947453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
62957453f775SEmil Constantinescu       tola = ts->atol;
62967453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62977453f775SEmil Constantinescu       tol  = tola+tolr;
62987453f775SEmil Constantinescu       if(tola>0.){
62997453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
63007453f775SEmil Constantinescu       }
63017453f775SEmil Constantinescu       if(tolr>0.){
63027453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
63037453f775SEmil Constantinescu       }
63047453f775SEmil Constantinescu       if(tol>0.){
63057453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
63067453f775SEmil Constantinescu       }
63079c6b16b5SShri Abhyankar     }
63089c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63099c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
63107453f775SEmil Constantinescu 
63117453f775SEmil Constantinescu     for (i=0; i<n; i++) {
63127453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
63137453f775SEmil Constantinescu       tola = ts->atol;
63147453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63157453f775SEmil Constantinescu       tol  = tola+tolr;
63167453f775SEmil Constantinescu       if(tola>0.){
63177453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
63187453f775SEmil Constantinescu       }
63197453f775SEmil Constantinescu       if(tolr>0.){
63207453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
63217453f775SEmil Constantinescu       }
63227453f775SEmil Constantinescu       if(tol>0.){
63237453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
63247453f775SEmil Constantinescu       }
63259c6b16b5SShri Abhyankar     }
63269c6b16b5SShri Abhyankar   }
63279c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
63289c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
63297453f775SEmil Constantinescu   err_loc[0] = max;
63307453f775SEmil Constantinescu   err_loc[1] = maxa;
63317453f775SEmil Constantinescu   err_loc[2] = maxr;
6332a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
63337453f775SEmil Constantinescu   gmax   = err_glb[0];
63347453f775SEmil Constantinescu   gmaxa  = err_glb[1];
63357453f775SEmil Constantinescu   gmaxr  = err_glb[2];
63369c6b16b5SShri Abhyankar 
63379c6b16b5SShri Abhyankar   *norm = gmax;
63387453f775SEmil Constantinescu   *norma = gmaxa;
63397453f775SEmil Constantinescu   *normr = gmaxr;
63409c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
63417453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
63427453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
63439c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
63449c6b16b5SShri Abhyankar }
63459c6b16b5SShri Abhyankar 
63469c6b16b5SShri Abhyankar #undef __FUNCT__
63477619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm"
63481c3436cfSJed Brown /*@
63498a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
63501c3436cfSJed Brown 
63511c3436cfSJed Brown    Collective on TS
63521c3436cfSJed Brown 
63531c3436cfSJed Brown    Input Arguments:
63541c3436cfSJed Brown +  ts - time stepping context
6355a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6356a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6357a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
63587619abb3SShri 
63591c3436cfSJed Brown    Output Arguments:
63608a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
63618a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
63628a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6363a4868fbcSLisandro Dalcin 
6364a4868fbcSLisandro Dalcin    Options Database Keys:
6365a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6366a4868fbcSLisandro Dalcin 
63671c3436cfSJed Brown    Level: developer
63681c3436cfSJed Brown 
63698a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
63701c3436cfSJed Brown @*/
63717453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
63721c3436cfSJed Brown {
63738beabaa1SBarry Smith   PetscErrorCode ierr;
63741c3436cfSJed Brown 
63751c3436cfSJed Brown   PetscFunctionBegin;
6376a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
63777453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6378a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
63797453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6380a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
63811c3436cfSJed Brown   PetscFunctionReturn(0);
63821c3436cfSJed Brown }
63831c3436cfSJed Brown 
63848a175baeSEmil Constantinescu 
63858a175baeSEmil Constantinescu #undef __FUNCT__
63868a175baeSEmil Constantinescu #define __FUNCT__ "TSErrorWeightedENorm2"
63878a175baeSEmil Constantinescu /*@
63888a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
63898a175baeSEmil Constantinescu 
63908a175baeSEmil Constantinescu    Collective on TS
63918a175baeSEmil Constantinescu 
63928a175baeSEmil Constantinescu    Input Arguments:
63938a175baeSEmil Constantinescu +  ts - time stepping context
63948a175baeSEmil Constantinescu .  E - error vector
63958a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
63968a175baeSEmil Constantinescu -  Y - state vector, previous time step
63978a175baeSEmil Constantinescu 
63988a175baeSEmil Constantinescu    Output Arguments:
63998a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
64008a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
64018a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
64028a175baeSEmil Constantinescu 
64038a175baeSEmil Constantinescu    Level: developer
64048a175baeSEmil Constantinescu 
64058a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
64068a175baeSEmil Constantinescu @*/
64078a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
64088a175baeSEmil Constantinescu {
64098a175baeSEmil Constantinescu   PetscErrorCode    ierr;
64108a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
64118a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
64128a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
64138a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
64148a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
64158a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
64168a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
64178a175baeSEmil Constantinescu 
64188a175baeSEmil Constantinescu   PetscFunctionBegin;
64198a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64208a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
64218a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
64228a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
64238a175baeSEmil Constantinescu   PetscValidType(E,2);
64248a175baeSEmil Constantinescu   PetscValidType(U,3);
64258a175baeSEmil Constantinescu   PetscValidType(Y,4);
64268a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
64278a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
64288a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
64298a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
64308a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
64318a175baeSEmil Constantinescu 
64328a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
64338a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
64348a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
64358a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
64368a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
64378a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
64388a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
64398a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
64408a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
64418a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
64428a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
64438a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64448a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64458a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64468a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64478a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
64488a175baeSEmil Constantinescu       if(tola>0.){
64498a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
64508a175baeSEmil Constantinescu         na_loc++;
64518a175baeSEmil Constantinescu       }
64528a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64538a175baeSEmil Constantinescu       if(tolr>0.){
64548a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
64558a175baeSEmil Constantinescu         nr_loc++;
64568a175baeSEmil Constantinescu       }
64578a175baeSEmil Constantinescu       tol=tola+tolr;
64588a175baeSEmil Constantinescu       if(tol>0.){
64598a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
64608a175baeSEmil Constantinescu         n_loc++;
64618a175baeSEmil Constantinescu       }
64628a175baeSEmil Constantinescu     }
64638a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64648a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64658a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
64668a175baeSEmil Constantinescu     const PetscScalar *atol;
64678a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64688a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64698a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64708a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
64718a175baeSEmil Constantinescu       if(tola>0.){
64728a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
64738a175baeSEmil Constantinescu         na_loc++;
64748a175baeSEmil Constantinescu       }
64758a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64768a175baeSEmil Constantinescu       if(tolr>0.){
64778a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
64788a175baeSEmil Constantinescu         nr_loc++;
64798a175baeSEmil Constantinescu       }
64808a175baeSEmil Constantinescu       tol=tola+tolr;
64818a175baeSEmil Constantinescu       if(tol>0.){
64828a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
64838a175baeSEmil Constantinescu         n_loc++;
64848a175baeSEmil Constantinescu       }
64858a175baeSEmil Constantinescu     }
64868a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64878a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
64888a175baeSEmil Constantinescu     const PetscScalar *rtol;
64898a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64908a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64918a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64928a175baeSEmil Constantinescu       tola = ts->atol;
64938a175baeSEmil Constantinescu       if(tola>0.){
64948a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
64958a175baeSEmil Constantinescu         na_loc++;
64968a175baeSEmil Constantinescu       }
64978a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64988a175baeSEmil Constantinescu       if(tolr>0.){
64998a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
65008a175baeSEmil Constantinescu         nr_loc++;
65018a175baeSEmil Constantinescu       }
65028a175baeSEmil Constantinescu       tol=tola+tolr;
65038a175baeSEmil Constantinescu       if(tol>0.){
65048a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
65058a175baeSEmil Constantinescu         n_loc++;
65068a175baeSEmil Constantinescu       }
65078a175baeSEmil Constantinescu     }
65088a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
65098a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
65108a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
65118a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
65128a175baeSEmil Constantinescu      tola = ts->atol;
65138a175baeSEmil Constantinescu       if(tola>0.){
65148a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
65158a175baeSEmil Constantinescu         na_loc++;
65168a175baeSEmil Constantinescu       }
65178a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
65188a175baeSEmil Constantinescu       if(tolr>0.){
65198a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
65208a175baeSEmil Constantinescu         nr_loc++;
65218a175baeSEmil Constantinescu       }
65228a175baeSEmil Constantinescu       tol=tola+tolr;
65238a175baeSEmil Constantinescu       if(tol>0.){
65248a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
65258a175baeSEmil Constantinescu         n_loc++;
65268a175baeSEmil Constantinescu       }
65278a175baeSEmil Constantinescu     }
65288a175baeSEmil Constantinescu   }
65298a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
65308a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
65318a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
65328a175baeSEmil Constantinescu 
65338a175baeSEmil Constantinescu   err_loc[0] = sum;
65348a175baeSEmil Constantinescu   err_loc[1] = suma;
65358a175baeSEmil Constantinescu   err_loc[2] = sumr;
65368a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
65378a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
65388a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
65398a175baeSEmil Constantinescu 
6540a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
65418a175baeSEmil Constantinescu 
65428a175baeSEmil Constantinescu   gsum   = err_glb[0];
65438a175baeSEmil Constantinescu   gsuma  = err_glb[1];
65448a175baeSEmil Constantinescu   gsumr  = err_glb[2];
65458a175baeSEmil Constantinescu   n_glb  = err_glb[3];
65468a175baeSEmil Constantinescu   na_glb = err_glb[4];
65478a175baeSEmil Constantinescu   nr_glb = err_glb[5];
65488a175baeSEmil Constantinescu 
65498a175baeSEmil Constantinescu   *norm  = 0.;
65508a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
65518a175baeSEmil Constantinescu   *norma = 0.;
65528a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
65538a175baeSEmil Constantinescu   *normr = 0.;
65548a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
65558a175baeSEmil Constantinescu 
65568a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
65578a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
65588a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
65598a175baeSEmil Constantinescu   PetscFunctionReturn(0);
65608a175baeSEmil Constantinescu }
65618a175baeSEmil Constantinescu 
65628a175baeSEmil Constantinescu #undef __FUNCT__
65638a175baeSEmil Constantinescu #define __FUNCT__ "TSErrorWeightedENormInfinity"
65648a175baeSEmil Constantinescu /*@
65658a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
65668a175baeSEmil Constantinescu    Collective on TS
65678a175baeSEmil Constantinescu 
65688a175baeSEmil Constantinescu    Input Arguments:
65698a175baeSEmil Constantinescu +  ts - time stepping context
65708a175baeSEmil Constantinescu .  E - error vector
65718a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
65728a175baeSEmil Constantinescu -  Y - state vector, previous time step
65738a175baeSEmil Constantinescu 
65748a175baeSEmil Constantinescu    Output Arguments:
65758a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
65768a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
65778a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
65788a175baeSEmil Constantinescu 
65798a175baeSEmil Constantinescu    Level: developer
65808a175baeSEmil Constantinescu 
65818a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
65828a175baeSEmil Constantinescu @*/
65838a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
65848a175baeSEmil Constantinescu {
65858a175baeSEmil Constantinescu   PetscErrorCode    ierr;
65868a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
65878a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
65888a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
65898a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
65908a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
65918a175baeSEmil Constantinescu 
65928a175baeSEmil Constantinescu   PetscFunctionBegin;
65938a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
65948a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
65958a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
65968a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
65978a175baeSEmil Constantinescu   PetscValidType(E,2);
65988a175baeSEmil Constantinescu   PetscValidType(U,3);
65998a175baeSEmil Constantinescu   PetscValidType(Y,4);
66008a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
66018a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
66028a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
66038a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
66048a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
66058a175baeSEmil Constantinescu 
66068a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
66078a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
66088a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
66098a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
66108a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
66118a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
66128a175baeSEmil Constantinescu 
66138a175baeSEmil Constantinescu   max=0.;
66148a175baeSEmil Constantinescu   maxa=0.;
66158a175baeSEmil Constantinescu   maxr=0.;
66168a175baeSEmil Constantinescu 
66178a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
66188a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
66198a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
66208a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
66218a175baeSEmil Constantinescu 
66228a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
66238a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
66248a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
66258a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
66268a175baeSEmil Constantinescu       tol  = tola+tolr;
66278a175baeSEmil Constantinescu       if(tola>0.){
66288a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
66298a175baeSEmil Constantinescu       }
66308a175baeSEmil Constantinescu       if(tolr>0.){
66318a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
66328a175baeSEmil Constantinescu       }
66338a175baeSEmil Constantinescu       if(tol>0.){
66348a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
66358a175baeSEmil Constantinescu       }
66368a175baeSEmil Constantinescu     }
66378a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
66388a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
66398a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
66408a175baeSEmil Constantinescu     const PetscScalar *atol;
66418a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
66428a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
66438a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
66448a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
66458a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
66468a175baeSEmil Constantinescu       tol  = tola+tolr;
66478a175baeSEmil Constantinescu       if(tola>0.){
66488a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
66498a175baeSEmil Constantinescu       }
66508a175baeSEmil Constantinescu       if(tolr>0.){
66518a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
66528a175baeSEmil Constantinescu       }
66538a175baeSEmil Constantinescu       if(tol>0.){
66548a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
66558a175baeSEmil Constantinescu       }
66568a175baeSEmil Constantinescu     }
66578a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
66588a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
66598a175baeSEmil Constantinescu     const PetscScalar *rtol;
66608a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
66618a175baeSEmil Constantinescu 
66628a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
66638a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
66648a175baeSEmil Constantinescu       tola = ts->atol;
66658a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
66668a175baeSEmil Constantinescu       tol  = tola+tolr;
66678a175baeSEmil Constantinescu       if(tola>0.){
66688a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
66698a175baeSEmil Constantinescu       }
66708a175baeSEmil Constantinescu       if(tolr>0.){
66718a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
66728a175baeSEmil Constantinescu       }
66738a175baeSEmil Constantinescu       if(tol>0.){
66748a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
66758a175baeSEmil Constantinescu       }
66768a175baeSEmil Constantinescu     }
66778a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
66788a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
66798a175baeSEmil Constantinescu 
66808a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
66818a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
66828a175baeSEmil Constantinescu       tola = ts->atol;
66838a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
66848a175baeSEmil Constantinescu       tol  = tola+tolr;
66858a175baeSEmil Constantinescu       if(tola>0.){
66868a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
66878a175baeSEmil Constantinescu       }
66888a175baeSEmil Constantinescu       if(tolr>0.){
66898a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
66908a175baeSEmil Constantinescu       }
66918a175baeSEmil Constantinescu       if(tol>0.){
66928a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
66938a175baeSEmil Constantinescu       }
66948a175baeSEmil Constantinescu     }
66958a175baeSEmil Constantinescu   }
66968a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
66978a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
66988a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
66998a175baeSEmil Constantinescu   err_loc[0] = max;
67008a175baeSEmil Constantinescu   err_loc[1] = maxa;
67018a175baeSEmil Constantinescu   err_loc[2] = maxr;
6702a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
67038a175baeSEmil Constantinescu   gmax   = err_glb[0];
67048a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
67058a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
67068a175baeSEmil Constantinescu 
67078a175baeSEmil Constantinescu   *norm = gmax;
67088a175baeSEmil Constantinescu   *norma = gmaxa;
67098a175baeSEmil Constantinescu   *normr = gmaxr;
67108a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
67118a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
67128a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
67138a175baeSEmil Constantinescu   PetscFunctionReturn(0);
67148a175baeSEmil Constantinescu }
67158a175baeSEmil Constantinescu 
67168a175baeSEmil Constantinescu #undef __FUNCT__
67178a175baeSEmil Constantinescu #define __FUNCT__ "TSErrorWeightedENorm"
67188a175baeSEmil Constantinescu /*@
67198a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
67208a175baeSEmil Constantinescu 
67218a175baeSEmil Constantinescu    Collective on TS
67228a175baeSEmil Constantinescu 
67238a175baeSEmil Constantinescu    Input Arguments:
67248a175baeSEmil Constantinescu +  ts - time stepping context
67258a175baeSEmil Constantinescu .  E - error vector
67268a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
67278a175baeSEmil Constantinescu .  Y - state vector, previous time step
67288a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
67298a175baeSEmil Constantinescu 
67308a175baeSEmil Constantinescu    Output Arguments:
67318a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
67328a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
67338a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
67348a175baeSEmil Constantinescu 
67358a175baeSEmil Constantinescu    Options Database Keys:
67368a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
67378a175baeSEmil Constantinescu 
67388a175baeSEmil Constantinescu    Level: developer
67398a175baeSEmil Constantinescu 
67408a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
67418a175baeSEmil Constantinescu @*/
67428a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
67438a175baeSEmil Constantinescu {
67448a175baeSEmil Constantinescu   PetscErrorCode ierr;
67458a175baeSEmil Constantinescu 
67468a175baeSEmil Constantinescu   PetscFunctionBegin;
67478a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
67488a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
67498a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
67508a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
67518a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
67528a175baeSEmil Constantinescu   PetscFunctionReturn(0);
67538a175baeSEmil Constantinescu }
67548a175baeSEmil Constantinescu 
67558a175baeSEmil Constantinescu 
67561c3436cfSJed Brown #undef __FUNCT__
67578d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
67588d59e960SJed Brown /*@
67598d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
67608d59e960SJed Brown 
67618d59e960SJed Brown    Logically Collective on TS
67628d59e960SJed Brown 
67638d59e960SJed Brown    Input Arguments:
67648d59e960SJed Brown +  ts - time stepping context
67658d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
67668d59e960SJed Brown 
67678d59e960SJed Brown    Note:
67688d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
67698d59e960SJed Brown 
67708d59e960SJed Brown    Level: intermediate
67718d59e960SJed Brown 
67728d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
67738d59e960SJed Brown @*/
67748d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
67758d59e960SJed Brown {
67768d59e960SJed Brown   PetscFunctionBegin;
67778d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
67788d59e960SJed Brown   ts->cfltime_local = cfltime;
67798d59e960SJed Brown   ts->cfltime       = -1.;
67808d59e960SJed Brown   PetscFunctionReturn(0);
67818d59e960SJed Brown }
67828d59e960SJed Brown 
67838d59e960SJed Brown #undef __FUNCT__
67848d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
67858d59e960SJed Brown /*@
67868d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
67878d59e960SJed Brown 
67888d59e960SJed Brown    Collective on TS
67898d59e960SJed Brown 
67908d59e960SJed Brown    Input Arguments:
67918d59e960SJed Brown .  ts - time stepping context
67928d59e960SJed Brown 
67938d59e960SJed Brown    Output Arguments:
67948d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
67958d59e960SJed Brown 
67968d59e960SJed Brown    Level: advanced
67978d59e960SJed Brown 
67988d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
67998d59e960SJed Brown @*/
68008d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
68018d59e960SJed Brown {
68028d59e960SJed Brown   PetscErrorCode ierr;
68038d59e960SJed Brown 
68048d59e960SJed Brown   PetscFunctionBegin;
68058d59e960SJed Brown   if (ts->cfltime < 0) {
6806b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
68078d59e960SJed Brown   }
68088d59e960SJed Brown   *cfltime = ts->cfltime;
68098d59e960SJed Brown   PetscFunctionReturn(0);
68108d59e960SJed Brown }
68118d59e960SJed Brown 
68128d59e960SJed Brown #undef __FUNCT__
6813d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
6814d6ebe24aSShri Abhyankar /*@
6815d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6816d6ebe24aSShri Abhyankar 
6817d6ebe24aSShri Abhyankar    Input Parameters:
6818d6ebe24aSShri Abhyankar .  ts   - the TS context.
6819d6ebe24aSShri Abhyankar .  xl   - lower bound.
6820d6ebe24aSShri Abhyankar .  xu   - upper bound.
6821d6ebe24aSShri Abhyankar 
6822d6ebe24aSShri Abhyankar    Notes:
6823d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6824e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6825d6ebe24aSShri Abhyankar 
68262bd2b0e6SSatish Balay    Level: advanced
68272bd2b0e6SSatish Balay 
6828d6ebe24aSShri Abhyankar @*/
6829d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6830d6ebe24aSShri Abhyankar {
6831d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6832d6ebe24aSShri Abhyankar   SNES           snes;
6833d6ebe24aSShri Abhyankar 
6834d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6835d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6836d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6837d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6838d6ebe24aSShri Abhyankar }
6839d6ebe24aSShri Abhyankar 
6840325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6841c6db04a5SJed Brown #include <mex.h>
6842325fc9f4SBarry Smith 
6843325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6844325fc9f4SBarry Smith 
6845325fc9f4SBarry Smith #undef __FUNCT__
6846325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
6847325fc9f4SBarry Smith /*
6848325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6849325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6850325fc9f4SBarry Smith 
6851325fc9f4SBarry Smith    Collective on TS
6852325fc9f4SBarry Smith 
6853325fc9f4SBarry Smith    Input Parameters:
6854325fc9f4SBarry Smith +  snes - the TS context
68550910c330SBarry Smith -  u - input vector
6856325fc9f4SBarry Smith 
6857325fc9f4SBarry Smith    Output Parameter:
6858325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6859325fc9f4SBarry Smith 
6860325fc9f4SBarry Smith    Notes:
6861325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6862325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6863325fc9f4SBarry Smith    themselves.
6864325fc9f4SBarry Smith 
6865325fc9f4SBarry Smith    Level: developer
6866325fc9f4SBarry Smith 
6867325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6868325fc9f4SBarry Smith 
6869325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6870325fc9f4SBarry Smith */
68710910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6872325fc9f4SBarry Smith {
6873325fc9f4SBarry Smith   PetscErrorCode  ierr;
6874325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6875325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6876325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6877325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6878325fc9f4SBarry Smith 
6879325fc9f4SBarry Smith   PetscFunctionBegin;
6880325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
68810910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
68820910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6883325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
68840910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6885325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6886325fc9f4SBarry Smith 
6887325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
68880910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
68890910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
68900910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6891bbd56ea5SKarl Rupp 
6892325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6893325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6894325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6895325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6896325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6897325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6898325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6899325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6900325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6901325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6902325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6903325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6904325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6905325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6906325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6907325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6908325fc9f4SBarry Smith   PetscFunctionReturn(0);
6909325fc9f4SBarry Smith }
6910325fc9f4SBarry Smith 
6911325fc9f4SBarry Smith 
6912325fc9f4SBarry Smith #undef __FUNCT__
6913325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
6914325fc9f4SBarry Smith /*
6915325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6916325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6917e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6918325fc9f4SBarry Smith 
6919325fc9f4SBarry Smith    Logically Collective on TS
6920325fc9f4SBarry Smith 
6921325fc9f4SBarry Smith    Input Parameters:
6922325fc9f4SBarry Smith +  ts - the TS context
6923325fc9f4SBarry Smith -  func - function evaluation routine
6924325fc9f4SBarry Smith 
6925325fc9f4SBarry Smith    Calling sequence of func:
69260910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6927325fc9f4SBarry Smith 
6928325fc9f4SBarry Smith    Level: beginner
6929325fc9f4SBarry Smith 
6930325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6931325fc9f4SBarry Smith 
6932325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6933325fc9f4SBarry Smith */
6934cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6935325fc9f4SBarry Smith {
6936325fc9f4SBarry Smith   PetscErrorCode  ierr;
6937325fc9f4SBarry Smith   TSMatlabContext *sctx;
6938325fc9f4SBarry Smith 
6939325fc9f4SBarry Smith   PetscFunctionBegin;
6940325fc9f4SBarry Smith   /* currently sctx is memory bleed */
694195dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6942325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6943325fc9f4SBarry Smith   /*
6944325fc9f4SBarry Smith      This should work, but it doesn't
6945325fc9f4SBarry Smith   sctx->ctx = ctx;
6946325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6947325fc9f4SBarry Smith   */
6948325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6949bbd56ea5SKarl Rupp 
69500298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6951325fc9f4SBarry Smith   PetscFunctionReturn(0);
6952325fc9f4SBarry Smith }
6953325fc9f4SBarry Smith 
6954325fc9f4SBarry Smith #undef __FUNCT__
6955325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
6956325fc9f4SBarry Smith /*
6957325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6958325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6959325fc9f4SBarry Smith 
6960325fc9f4SBarry Smith    Collective on TS
6961325fc9f4SBarry Smith 
6962325fc9f4SBarry Smith    Input Parameters:
6963cdcf91faSSean Farley +  ts - the TS context
69640910c330SBarry Smith .  u - input vector
6965325fc9f4SBarry Smith .  A, B - the matrices
6966325fc9f4SBarry Smith -  ctx - user context
6967325fc9f4SBarry Smith 
6968325fc9f4SBarry Smith    Level: developer
6969325fc9f4SBarry Smith 
6970325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6971325fc9f4SBarry Smith 
6972325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6973325fc9f4SBarry Smith @*/
6974f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6975325fc9f4SBarry Smith {
6976325fc9f4SBarry Smith   PetscErrorCode  ierr;
6977325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6978325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6979325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6980325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6981325fc9f4SBarry Smith 
6982325fc9f4SBarry Smith   PetscFunctionBegin;
6983cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
69840910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6985325fc9f4SBarry Smith 
69860910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6987325fc9f4SBarry Smith 
6988cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
69890910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
69900910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
69910910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
69920910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6993bbd56ea5SKarl Rupp 
6994325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6995325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6996325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6997325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6998325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6999325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
7000325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
7001325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
7002325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
7003325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
7004325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
7005325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
7006325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
7007325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
7008325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
7009325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
7010325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
7011325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
7012325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
7013325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
7014325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
7015325fc9f4SBarry Smith   PetscFunctionReturn(0);
7016325fc9f4SBarry Smith }
7017325fc9f4SBarry Smith 
7018325fc9f4SBarry Smith 
7019325fc9f4SBarry Smith #undef __FUNCT__
7020325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
7021325fc9f4SBarry Smith /*
7022325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
7023e3c5b3baSBarry 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
7024325fc9f4SBarry Smith 
7025325fc9f4SBarry Smith    Logically Collective on TS
7026325fc9f4SBarry Smith 
7027325fc9f4SBarry Smith    Input Parameters:
7028cdcf91faSSean Farley +  ts - the TS context
7029325fc9f4SBarry Smith .  A,B - Jacobian matrices
7030325fc9f4SBarry Smith .  func - function evaluation routine
7031325fc9f4SBarry Smith -  ctx - user context
7032325fc9f4SBarry Smith 
7033325fc9f4SBarry Smith    Calling sequence of func:
70340910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
7035325fc9f4SBarry Smith 
7036325fc9f4SBarry Smith 
7037325fc9f4SBarry Smith    Level: developer
7038325fc9f4SBarry Smith 
7039325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
7040325fc9f4SBarry Smith 
7041325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
7042325fc9f4SBarry Smith */
7043cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
7044325fc9f4SBarry Smith {
7045325fc9f4SBarry Smith   PetscErrorCode  ierr;
7046325fc9f4SBarry Smith   TSMatlabContext *sctx;
7047325fc9f4SBarry Smith 
7048325fc9f4SBarry Smith   PetscFunctionBegin;
7049325fc9f4SBarry Smith   /* currently sctx is memory bleed */
705095dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
7051325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
7052325fc9f4SBarry Smith   /*
7053325fc9f4SBarry Smith      This should work, but it doesn't
7054325fc9f4SBarry Smith   sctx->ctx = ctx;
7055325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
7056325fc9f4SBarry Smith   */
7057325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
7058bbd56ea5SKarl Rupp 
7059cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
7060325fc9f4SBarry Smith   PetscFunctionReturn(0);
7061325fc9f4SBarry Smith }
7062325fc9f4SBarry Smith 
7063b5b1a830SBarry Smith #undef __FUNCT__
7064b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
7065b5b1a830SBarry Smith /*
7066b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
7067b5b1a830SBarry Smith 
7068b5b1a830SBarry Smith    Collective on TS
7069b5b1a830SBarry Smith 
7070b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
7071b5b1a830SBarry Smith @*/
70720910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
7073b5b1a830SBarry Smith {
7074b5b1a830SBarry Smith   PetscErrorCode  ierr;
7075b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
7076a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
7077b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
7078b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
7079b5b1a830SBarry Smith 
7080b5b1a830SBarry Smith   PetscFunctionBegin;
7081cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
70820910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
7083b5b1a830SBarry Smith 
7084cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
70850910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
7086bbd56ea5SKarl Rupp 
7087b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
7088b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
7089b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
7090b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
7091b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
7092b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
7093b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
7094b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
7095b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
7096b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
7097b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
7098b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
7099b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
7100b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
7101b5b1a830SBarry Smith   PetscFunctionReturn(0);
7102b5b1a830SBarry Smith }
7103b5b1a830SBarry Smith 
7104b5b1a830SBarry Smith 
7105b5b1a830SBarry Smith #undef __FUNCT__
7106b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
7107b5b1a830SBarry Smith /*
7108b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
7109b5b1a830SBarry Smith 
7110b5b1a830SBarry Smith    Level: developer
7111b5b1a830SBarry Smith 
7112b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
7113b5b1a830SBarry Smith 
7114b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
7115b5b1a830SBarry Smith */
7116cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
7117b5b1a830SBarry Smith {
7118b5b1a830SBarry Smith   PetscErrorCode  ierr;
7119b5b1a830SBarry Smith   TSMatlabContext *sctx;
7120b5b1a830SBarry Smith 
7121b5b1a830SBarry Smith   PetscFunctionBegin;
7122b5b1a830SBarry Smith   /* currently sctx is memory bleed */
712395dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
7124b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
7125b5b1a830SBarry Smith   /*
7126b5b1a830SBarry Smith      This should work, but it doesn't
7127b5b1a830SBarry Smith   sctx->ctx = ctx;
7128b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
7129b5b1a830SBarry Smith   */
7130b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
7131bbd56ea5SKarl Rupp 
71320298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
7133b5b1a830SBarry Smith   PetscFunctionReturn(0);
7134b5b1a830SBarry Smith }
7135325fc9f4SBarry Smith #endif
7136b3603a34SBarry Smith 
7137b3603a34SBarry Smith #undef __FUNCT__
71384f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
7139b3603a34SBarry Smith /*@C
71404f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
7141b3603a34SBarry Smith        in a time based line graph
7142b3603a34SBarry Smith 
7143b3603a34SBarry Smith    Collective on TS
7144b3603a34SBarry Smith 
7145b3603a34SBarry Smith    Input Parameters:
7146b3603a34SBarry Smith +  ts - the TS context
7147b3603a34SBarry Smith .  step - current time-step
7148b3603a34SBarry Smith .  ptime - current time
71497db568b7SBarry Smith .  u - current solution
71507db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
7151b3603a34SBarry Smith 
7152b3d3934dSBarry Smith    Options Database:
71539ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
7154b3d3934dSBarry Smith 
7155b3603a34SBarry Smith    Level: intermediate
7156b3603a34SBarry Smith 
71576934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component solutions in a separate window
7158b3603a34SBarry Smith 
7159b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
7160b3603a34SBarry Smith 
71617db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
71627db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
71637db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
71647db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
7165b3603a34SBarry Smith @*/
71667db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7167b3603a34SBarry Smith {
7168b3603a34SBarry Smith   PetscErrorCode    ierr;
71697db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
7170b3603a34SBarry Smith   const PetscScalar *yy;
717180666b62SBarry Smith   Vec               v;
7172b3603a34SBarry Smith 
7173b3603a34SBarry Smith   PetscFunctionBegin;
717463e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
717558ff32f7SBarry Smith   if (!step) {
7176a9f9c1f6SBarry Smith     PetscDrawAxis axis;
71776934998bSLisandro Dalcin     PetscInt      dim;
7178a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7179a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
7180bab0a581SBarry Smith     if (!ctx->names) {
7181bab0a581SBarry Smith       PetscBool flg;
7182bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
7183bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
7184bab0a581SBarry Smith       if (flg) {
7185bab0a581SBarry Smith         PetscInt i,n;
7186bab0a581SBarry Smith         char     **names;
7187bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
7188bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
7189bab0a581SBarry Smith         for (i=0; i<n; i++) {
7190bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
7191bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
7192bab0a581SBarry Smith         }
7193bab0a581SBarry Smith         names[n] = NULL;
7194bab0a581SBarry Smith         ctx->names = names;
7195bab0a581SBarry Smith       }
7196bab0a581SBarry Smith     }
7197387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
7198387f4636SBarry Smith       char      **displaynames;
7199387f4636SBarry Smith       PetscBool flg;
7200387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
720195dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
7202387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
7203c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
7204387f4636SBarry Smith       if (flg) {
7205a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
7206387f4636SBarry Smith       }
7207387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
7208387f4636SBarry Smith     }
7209387f4636SBarry Smith     if (ctx->displaynames) {
7210387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
7211387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
7212387f4636SBarry Smith     } else if (ctx->names) {
72130910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
72140b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7215387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
7216b0bc92c6SBarry Smith     } else {
7217b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
7218b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7219387f4636SBarry Smith     }
72200b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
722158ff32f7SBarry Smith   }
72226934998bSLisandro Dalcin 
72236934998bSLisandro Dalcin   if (!ctx->transform) v = u;
72246934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
722580666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
72266934998bSLisandro Dalcin   if (ctx->displaynames) {
72276934998bSLisandro Dalcin     PetscInt i;
72286934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
72296934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
72306934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
72316934998bSLisandro Dalcin   } else {
7232e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
7233e3efe391SJed Brown     PetscInt  i,n;
72346934998bSLisandro Dalcin     PetscReal *yreal;
723580666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
7236785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
7237e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
72380b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
7239e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
7240e3efe391SJed Brown #else
72410b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
7242e3efe391SJed Brown #endif
724380666b62SBarry Smith   }
72446934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
72456934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
72466934998bSLisandro Dalcin 
7247b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
72480b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
72496934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
72503923b477SBarry Smith   }
7251b3603a34SBarry Smith   PetscFunctionReturn(0);
7252b3603a34SBarry Smith }
7253b3603a34SBarry Smith 
7254387f4636SBarry Smith 
7255b3603a34SBarry Smith #undef __FUNCT__
725631152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames"
7257b037adc7SBarry Smith /*@C
725831152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
7259b037adc7SBarry Smith 
7260b037adc7SBarry Smith    Collective on TS
7261b037adc7SBarry Smith 
7262b037adc7SBarry Smith    Input Parameters:
7263b037adc7SBarry Smith +  ts - the TS context
7264b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
7265b037adc7SBarry Smith 
7266b037adc7SBarry Smith    Level: intermediate
7267b037adc7SBarry Smith 
72687db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
72697db568b7SBarry Smith 
7270b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
7271b037adc7SBarry Smith 
7272a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
7273b037adc7SBarry Smith @*/
727431152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
7275b037adc7SBarry Smith {
7276b037adc7SBarry Smith   PetscErrorCode    ierr;
7277b037adc7SBarry Smith   PetscInt          i;
7278b037adc7SBarry Smith 
7279b037adc7SBarry Smith   PetscFunctionBegin;
7280b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7281b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
72825537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
7283b3d3934dSBarry Smith       break;
7284b3d3934dSBarry Smith     }
7285b3d3934dSBarry Smith   }
7286b3d3934dSBarry Smith   PetscFunctionReturn(0);
7287b3d3934dSBarry Smith }
7288b3d3934dSBarry Smith 
7289b3d3934dSBarry Smith #undef __FUNCT__
7290e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames"
7291e673d494SBarry Smith /*@C
7292e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
7293e673d494SBarry Smith 
7294e673d494SBarry Smith    Collective on TS
7295e673d494SBarry Smith 
7296e673d494SBarry Smith    Input Parameters:
7297e673d494SBarry Smith +  ts - the TS context
7298e673d494SBarry Smith -  names - the names of the components, final string must be NULL
7299e673d494SBarry Smith 
7300e673d494SBarry Smith    Level: intermediate
7301e673d494SBarry Smith 
7302e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7303e673d494SBarry Smith 
7304a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
7305e673d494SBarry Smith @*/
7306e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
7307e673d494SBarry Smith {
7308e673d494SBarry Smith   PetscErrorCode    ierr;
7309e673d494SBarry Smith 
7310e673d494SBarry Smith   PetscFunctionBegin;
7311e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
7312e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
7313e673d494SBarry Smith   PetscFunctionReturn(0);
7314e673d494SBarry Smith }
7315e673d494SBarry Smith 
7316e673d494SBarry Smith #undef __FUNCT__
7317b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames"
7318b3d3934dSBarry Smith /*@C
7319b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
7320b3d3934dSBarry Smith 
7321b3d3934dSBarry Smith    Collective on TS
7322b3d3934dSBarry Smith 
7323b3d3934dSBarry Smith    Input Parameter:
7324b3d3934dSBarry Smith .  ts - the TS context
7325b3d3934dSBarry Smith 
7326b3d3934dSBarry Smith    Output Parameter:
7327b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
7328b3d3934dSBarry Smith 
7329b3d3934dSBarry Smith    Level: intermediate
7330b3d3934dSBarry Smith 
73317db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
73327db568b7SBarry Smith 
7333b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7334b3d3934dSBarry Smith 
7335b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7336b3d3934dSBarry Smith @*/
7337b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
7338b3d3934dSBarry Smith {
7339b3d3934dSBarry Smith   PetscInt       i;
7340b3d3934dSBarry Smith 
7341b3d3934dSBarry Smith   PetscFunctionBegin;
7342b3d3934dSBarry Smith   *names = NULL;
7343b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7344b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
73455537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
7346b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
7347b3d3934dSBarry Smith       break;
7348387f4636SBarry Smith     }
7349387f4636SBarry Smith   }
7350387f4636SBarry Smith   PetscFunctionReturn(0);
7351387f4636SBarry Smith }
7352387f4636SBarry Smith 
7353387f4636SBarry Smith #undef __FUNCT__
7354a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables"
7355a66092f1SBarry Smith /*@C
7356a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
7357a66092f1SBarry Smith 
7358a66092f1SBarry Smith    Collective on TS
7359a66092f1SBarry Smith 
7360a66092f1SBarry Smith    Input Parameters:
7361a66092f1SBarry Smith +  ctx - the TSMonitorLG context
7362a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
7363a66092f1SBarry Smith 
7364a66092f1SBarry Smith    Level: intermediate
7365a66092f1SBarry Smith 
7366a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
7367a66092f1SBarry Smith 
7368a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7369a66092f1SBarry Smith @*/
7370a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
7371a66092f1SBarry Smith {
7372a66092f1SBarry Smith   PetscInt          j = 0,k;
7373a66092f1SBarry Smith   PetscErrorCode    ierr;
7374a66092f1SBarry Smith 
7375a66092f1SBarry Smith   PetscFunctionBegin;
7376a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
7377a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
7378a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
7379a66092f1SBarry Smith   while (displaynames[j]) j++;
7380a66092f1SBarry Smith   ctx->ndisplayvariables = j;
7381a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
7382a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
7383a66092f1SBarry Smith   j = 0;
7384a66092f1SBarry Smith   while (displaynames[j]) {
7385a66092f1SBarry Smith     k = 0;
7386a66092f1SBarry Smith     while (ctx->names[k]) {
7387a66092f1SBarry Smith       PetscBool flg;
7388a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
7389a66092f1SBarry Smith       if (flg) {
7390a66092f1SBarry Smith         ctx->displayvariables[j] = k;
7391a66092f1SBarry Smith         break;
7392a66092f1SBarry Smith       }
7393a66092f1SBarry Smith       k++;
7394a66092f1SBarry Smith     }
7395a66092f1SBarry Smith     j++;
7396a66092f1SBarry Smith   }
7397a66092f1SBarry Smith   PetscFunctionReturn(0);
7398a66092f1SBarry Smith }
7399a66092f1SBarry Smith 
7400a66092f1SBarry Smith 
7401a66092f1SBarry Smith #undef __FUNCT__
7402387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables"
7403387f4636SBarry Smith /*@C
7404387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
7405387f4636SBarry Smith 
7406387f4636SBarry Smith    Collective on TS
7407387f4636SBarry Smith 
7408387f4636SBarry Smith    Input Parameters:
7409387f4636SBarry Smith +  ts - the TS context
7410387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
7411387f4636SBarry Smith 
74127db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
74137db568b7SBarry Smith 
7414387f4636SBarry Smith    Level: intermediate
7415387f4636SBarry Smith 
7416387f4636SBarry Smith .keywords: TS,  vector, monitor, view
7417387f4636SBarry Smith 
7418387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7419387f4636SBarry Smith @*/
7420387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
7421387f4636SBarry Smith {
7422a66092f1SBarry Smith   PetscInt          i;
7423387f4636SBarry Smith   PetscErrorCode    ierr;
7424387f4636SBarry Smith 
7425387f4636SBarry Smith   PetscFunctionBegin;
7426387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7427387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
74285537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
7429b3d3934dSBarry Smith       break;
7430b037adc7SBarry Smith     }
7431b037adc7SBarry Smith   }
7432b037adc7SBarry Smith   PetscFunctionReturn(0);
7433b037adc7SBarry Smith }
7434b037adc7SBarry Smith 
7435b037adc7SBarry Smith #undef __FUNCT__
743680666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform"
743780666b62SBarry Smith /*@C
743880666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
743980666b62SBarry Smith 
744080666b62SBarry Smith    Collective on TS
744180666b62SBarry Smith 
744280666b62SBarry Smith    Input Parameters:
744380666b62SBarry Smith +  ts - the TS context
744480666b62SBarry Smith .  transform - the transform function
74457684fa3eSBarry Smith .  destroy - function to destroy the optional context
744680666b62SBarry Smith -  ctx - optional context used by transform function
744780666b62SBarry Smith 
74487db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
74497db568b7SBarry Smith 
745080666b62SBarry Smith    Level: intermediate
745180666b62SBarry Smith 
745280666b62SBarry Smith .keywords: TS,  vector, monitor, view
745380666b62SBarry Smith 
7454a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
745580666b62SBarry Smith @*/
74567684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
745780666b62SBarry Smith {
745880666b62SBarry Smith   PetscInt          i;
7459a66092f1SBarry Smith   PetscErrorCode    ierr;
746080666b62SBarry Smith 
746180666b62SBarry Smith   PetscFunctionBegin;
746280666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
746380666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
74645537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
746580666b62SBarry Smith     }
746680666b62SBarry Smith   }
746780666b62SBarry Smith   PetscFunctionReturn(0);
746880666b62SBarry Smith }
746980666b62SBarry Smith 
747080666b62SBarry Smith #undef __FUNCT__
7471e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform"
7472e673d494SBarry Smith /*@C
7473e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
7474e673d494SBarry Smith 
7475e673d494SBarry Smith    Collective on TSLGCtx
7476e673d494SBarry Smith 
7477e673d494SBarry Smith    Input Parameters:
7478e673d494SBarry Smith +  ts - the TS context
7479e673d494SBarry Smith .  transform - the transform function
74807684fa3eSBarry Smith .  destroy - function to destroy the optional context
7481e673d494SBarry Smith -  ctx - optional context used by transform function
7482e673d494SBarry Smith 
7483e673d494SBarry Smith    Level: intermediate
7484e673d494SBarry Smith 
7485e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7486e673d494SBarry Smith 
7487a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
7488e673d494SBarry Smith @*/
74897684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
7490e673d494SBarry Smith {
7491e673d494SBarry Smith   PetscFunctionBegin;
7492e673d494SBarry Smith   ctx->transform    = transform;
74937684fa3eSBarry Smith   ctx->transformdestroy = destroy;
7494e673d494SBarry Smith   ctx->transformctx = tctx;
7495e673d494SBarry Smith   PetscFunctionReturn(0);
7496e673d494SBarry Smith }
7497e673d494SBarry Smith 
7498b3603a34SBarry Smith #undef __FUNCT__
74994f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
7500ef20d060SBarry Smith /*@C
75014f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
7502ef20d060SBarry Smith        in a time based line graph
7503ef20d060SBarry Smith 
7504ef20d060SBarry Smith    Collective on TS
7505ef20d060SBarry Smith 
7506ef20d060SBarry Smith    Input Parameters:
7507ef20d060SBarry Smith +  ts - the TS context
7508ef20d060SBarry Smith .  step - current time-step
7509ef20d060SBarry Smith .  ptime - current time
75107db568b7SBarry Smith .  u - current solution
75117db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
7512ef20d060SBarry Smith 
7513ef20d060SBarry Smith    Level: intermediate
7514ef20d060SBarry Smith 
75156934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component errors in a separate window
7516abd5a294SJed Brown 
7517abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
7518abd5a294SJed Brown 
7519abd5a294SJed Brown    Options Database Keys:
75204f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
7521ef20d060SBarry Smith 
7522ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
7523ef20d060SBarry Smith 
7524abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
7525ef20d060SBarry Smith @*/
75260910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
7527ef20d060SBarry Smith {
7528ef20d060SBarry Smith   PetscErrorCode    ierr;
75290b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
7530ef20d060SBarry Smith   const PetscScalar *yy;
7531ef20d060SBarry Smith   Vec               y;
7532ef20d060SBarry Smith 
7533ef20d060SBarry Smith   PetscFunctionBegin;
7534a9f9c1f6SBarry Smith   if (!step) {
7535a9f9c1f6SBarry Smith     PetscDrawAxis axis;
75366934998bSLisandro Dalcin     PetscInt      dim;
7537a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
75381ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
75390910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
7540a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7541a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7542a9f9c1f6SBarry Smith   }
75430910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
7544ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
75450910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7546ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
7547e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
7548e3efe391SJed Brown   {
7549e3efe391SJed Brown     PetscReal *yreal;
7550e3efe391SJed Brown     PetscInt  i,n;
7551e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
7552785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
7553e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
75540b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
7555e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
7556e3efe391SJed Brown   }
7557e3efe391SJed Brown #else
75580b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
7559e3efe391SJed Brown #endif
7560ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
7561ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
7562b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
75630b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
75646934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
75653923b477SBarry Smith   }
7566ef20d060SBarry Smith   PetscFunctionReturn(0);
7567ef20d060SBarry Smith }
7568ef20d060SBarry Smith 
7569201da799SBarry Smith #undef __FUNCT__
7570201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
7571201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7572201da799SBarry Smith {
7573201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7574201da799SBarry Smith   PetscReal      x   = ptime,y;
7575201da799SBarry Smith   PetscErrorCode ierr;
7576201da799SBarry Smith   PetscInt       its;
7577201da799SBarry Smith 
7578201da799SBarry Smith   PetscFunctionBegin;
757963e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7580201da799SBarry Smith   if (!n) {
7581201da799SBarry Smith     PetscDrawAxis axis;
7582201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7583201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7584201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7585201da799SBarry Smith     ctx->snes_its = 0;
7586201da799SBarry Smith   }
7587201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7588201da799SBarry Smith   y    = its - ctx->snes_its;
7589201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
75903fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7591201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
75926934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7593201da799SBarry Smith   }
7594201da799SBarry Smith   ctx->snes_its = its;
7595201da799SBarry Smith   PetscFunctionReturn(0);
7596201da799SBarry Smith }
7597201da799SBarry Smith 
7598201da799SBarry Smith #undef __FUNCT__
7599201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
7600201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7601201da799SBarry Smith {
7602201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7603201da799SBarry Smith   PetscReal      x   = ptime,y;
7604201da799SBarry Smith   PetscErrorCode ierr;
7605201da799SBarry Smith   PetscInt       its;
7606201da799SBarry Smith 
7607201da799SBarry Smith   PetscFunctionBegin;
760863e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7609201da799SBarry Smith   if (!n) {
7610201da799SBarry Smith     PetscDrawAxis axis;
7611201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7612201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7613201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7614201da799SBarry Smith     ctx->ksp_its = 0;
7615201da799SBarry Smith   }
7616201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7617201da799SBarry Smith   y    = its - ctx->ksp_its;
7618201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
761999fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7620201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
76216934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7622201da799SBarry Smith   }
7623201da799SBarry Smith   ctx->ksp_its = its;
7624201da799SBarry Smith   PetscFunctionReturn(0);
7625201da799SBarry Smith }
7626f9c1d6abSBarry Smith 
7627f9c1d6abSBarry Smith #undef __FUNCT__
7628f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
7629f9c1d6abSBarry Smith /*@
7630f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7631f9c1d6abSBarry Smith 
7632f9c1d6abSBarry Smith    Collective on TS and Vec
7633f9c1d6abSBarry Smith 
7634f9c1d6abSBarry Smith    Input Parameters:
7635f9c1d6abSBarry Smith +  ts - the TS context
7636f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7637f9c1d6abSBarry Smith 
7638f9c1d6abSBarry Smith    Output Parameters:
7639f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7640f9c1d6abSBarry Smith 
7641f9c1d6abSBarry Smith    Level: developer
7642f9c1d6abSBarry Smith 
7643f9c1d6abSBarry Smith .keywords: TS, compute
7644f9c1d6abSBarry Smith 
7645f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7646f9c1d6abSBarry Smith @*/
7647f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7648f9c1d6abSBarry Smith {
7649f9c1d6abSBarry Smith   PetscErrorCode ierr;
7650f9c1d6abSBarry Smith 
7651f9c1d6abSBarry Smith   PetscFunctionBegin;
7652f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7653ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7654f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7655f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7656f9c1d6abSBarry Smith }
765724655328SShri 
7658b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7659b3d3934dSBarry Smith #undef __FUNCT__
7660b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate"
7661b3d3934dSBarry Smith /*@C
7662b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7663b3d3934dSBarry Smith 
7664b3d3934dSBarry Smith    Collective on TS
7665b3d3934dSBarry Smith 
7666b3d3934dSBarry Smith    Input Parameters:
7667b3d3934dSBarry Smith .  ts  - the ODE solver object
7668b3d3934dSBarry Smith 
7669b3d3934dSBarry Smith    Output Parameter:
7670b3d3934dSBarry Smith .  ctx - the context
7671b3d3934dSBarry Smith 
7672b3d3934dSBarry Smith    Level: intermediate
7673b3d3934dSBarry Smith 
7674b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
7675b3d3934dSBarry Smith 
7676b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7677b3d3934dSBarry Smith 
7678b3d3934dSBarry Smith @*/
7679b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7680b3d3934dSBarry Smith {
7681b3d3934dSBarry Smith   PetscErrorCode ierr;
7682b3d3934dSBarry Smith 
7683b3d3934dSBarry Smith   PetscFunctionBegin;
7684a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7685b3d3934dSBarry Smith   PetscFunctionReturn(0);
7686b3d3934dSBarry Smith }
7687b3d3934dSBarry Smith 
7688b3d3934dSBarry Smith #undef __FUNCT__
7689b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope"
7690b3d3934dSBarry Smith /*@C
7691b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7692b3d3934dSBarry Smith 
7693b3d3934dSBarry Smith    Collective on TS
7694b3d3934dSBarry Smith 
7695b3d3934dSBarry Smith    Input Parameters:
7696b3d3934dSBarry Smith +  ts - the TS context
7697b3d3934dSBarry Smith .  step - current time-step
7698b3d3934dSBarry Smith .  ptime - current time
76997db568b7SBarry Smith .  u  - current solution
77007db568b7SBarry Smith -  dctx - the envelope context
7701b3d3934dSBarry Smith 
7702b3d3934dSBarry Smith    Options Database:
7703b3d3934dSBarry Smith .  -ts_monitor_envelope
7704b3d3934dSBarry Smith 
7705b3d3934dSBarry Smith    Level: intermediate
7706b3d3934dSBarry Smith 
7707b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7708b3d3934dSBarry Smith 
7709b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7710b3d3934dSBarry Smith 
77117db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7712b3d3934dSBarry Smith @*/
77137db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7714b3d3934dSBarry Smith {
7715b3d3934dSBarry Smith   PetscErrorCode       ierr;
77167db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7717b3d3934dSBarry Smith 
7718b3d3934dSBarry Smith   PetscFunctionBegin;
7719b3d3934dSBarry Smith   if (!ctx->max) {
7720b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7721b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7722b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7723b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7724b3d3934dSBarry Smith   } else {
7725b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7726b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7727b3d3934dSBarry Smith   }
7728b3d3934dSBarry Smith   PetscFunctionReturn(0);
7729b3d3934dSBarry Smith }
7730b3d3934dSBarry Smith 
7731b3d3934dSBarry Smith 
7732b3d3934dSBarry Smith #undef __FUNCT__
7733b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds"
7734b3d3934dSBarry Smith /*@C
7735b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7736b3d3934dSBarry Smith 
7737b3d3934dSBarry Smith    Collective on TS
7738b3d3934dSBarry Smith 
7739b3d3934dSBarry Smith    Input Parameter:
7740b3d3934dSBarry Smith .  ts - the TS context
7741b3d3934dSBarry Smith 
7742b3d3934dSBarry Smith    Output Parameter:
7743b3d3934dSBarry Smith +  max - the maximum values
7744b3d3934dSBarry Smith -  min - the minimum values
7745b3d3934dSBarry Smith 
77467db568b7SBarry Smith    Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
77477db568b7SBarry Smith 
7748b3d3934dSBarry Smith    Level: intermediate
7749b3d3934dSBarry Smith 
7750b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7751b3d3934dSBarry Smith 
7752b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7753b3d3934dSBarry Smith @*/
7754b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7755b3d3934dSBarry Smith {
7756b3d3934dSBarry Smith   PetscInt i;
7757b3d3934dSBarry Smith 
7758b3d3934dSBarry Smith   PetscFunctionBegin;
7759b3d3934dSBarry Smith   if (max) *max = NULL;
7760b3d3934dSBarry Smith   if (min) *min = NULL;
7761b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7762b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
77635537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7764b3d3934dSBarry Smith       if (max) *max = ctx->max;
7765b3d3934dSBarry Smith       if (min) *min = ctx->min;
7766b3d3934dSBarry Smith       break;
7767b3d3934dSBarry Smith     }
7768b3d3934dSBarry Smith   }
7769b3d3934dSBarry Smith   PetscFunctionReturn(0);
7770b3d3934dSBarry Smith }
7771b3d3934dSBarry Smith 
7772b3d3934dSBarry Smith #undef __FUNCT__
7773b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy"
7774b3d3934dSBarry Smith /*@C
7775b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7776b3d3934dSBarry Smith 
7777b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7778b3d3934dSBarry Smith 
7779b3d3934dSBarry Smith    Input Parameter:
7780b3d3934dSBarry Smith .  ctx - the monitor context
7781b3d3934dSBarry Smith 
7782b3d3934dSBarry Smith    Level: intermediate
7783b3d3934dSBarry Smith 
7784b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7785b3d3934dSBarry Smith 
77867db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7787b3d3934dSBarry Smith @*/
7788b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7789b3d3934dSBarry Smith {
7790b3d3934dSBarry Smith   PetscErrorCode ierr;
7791b3d3934dSBarry Smith 
7792b3d3934dSBarry Smith   PetscFunctionBegin;
7793b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7794b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7795b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7796b3d3934dSBarry Smith   PetscFunctionReturn(0);
7797b3d3934dSBarry Smith }
7798f2dee214SBarry Smith 
779924655328SShri #undef __FUNCT__
780024655328SShri #define __FUNCT__ "TSRollBack"
780124655328SShri /*@
780224655328SShri    TSRollBack - Rolls back one time step
780324655328SShri 
780424655328SShri    Collective on TS
780524655328SShri 
780624655328SShri    Input Parameter:
780724655328SShri .  ts - the TS context obtained from TSCreate()
780824655328SShri 
780924655328SShri    Level: advanced
781024655328SShri 
781124655328SShri .keywords: TS, timestep, rollback
781224655328SShri 
781324655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
781424655328SShri @*/
781524655328SShri PetscErrorCode  TSRollBack(TS ts)
781624655328SShri {
781724655328SShri   PetscErrorCode ierr;
781824655328SShri 
781924655328SShri   PetscFunctionBegin;
782024655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7821b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
782224655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
782324655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
782424655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
782524655328SShri   ts->ptime = ts->ptime_prev;
7826be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
7827be5899b3SLisandro Dalcin   ts->steps--; ts->total_steps--;
782810b82f12SShri Abhyankar   ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
7829b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
783024655328SShri   PetscFunctionReturn(0);
783124655328SShri }
7832aeb4809dSShri Abhyankar 
7833ff22ae23SHong Zhang #undef __FUNCT__
7834ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
7835ff22ae23SHong Zhang /*@
7836ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7837ff22ae23SHong Zhang 
7838ff22ae23SHong Zhang    Input Parameter:
7839ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7840ff22ae23SHong Zhang 
7841ff22ae23SHong Zhang    Level: advanced
7842ff22ae23SHong Zhang 
7843ff22ae23SHong Zhang .keywords: TS, getstages
7844ff22ae23SHong Zhang 
7845ff22ae23SHong Zhang .seealso: TSCreate()
7846ff22ae23SHong Zhang @*/
7847ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7848ff22ae23SHong Zhang {
7849ff22ae23SHong Zhang   PetscErrorCode ierr;
7850ff22ae23SHong Zhang 
7851ff22ae23SHong Zhang   PetscFunctionBegin;
7852ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7853ff22ae23SHong Zhang   PetscValidPointer(ns,2);
7854ff22ae23SHong Zhang 
7855ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
7856ff22ae23SHong Zhang   else {
7857ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7858ff22ae23SHong Zhang   }
7859ff22ae23SHong Zhang   PetscFunctionReturn(0);
7860ff22ae23SHong Zhang }
7861ff22ae23SHong Zhang 
7862847ff0e1SMatthew G. Knepley #undef __FUNCT__
7863847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor"
7864847ff0e1SMatthew G. Knepley /*@C
7865847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7866847ff0e1SMatthew G. Knepley 
7867847ff0e1SMatthew G. Knepley   Collective on SNES
7868847ff0e1SMatthew G. Knepley 
7869847ff0e1SMatthew G. Knepley   Input Parameters:
7870847ff0e1SMatthew G. Knepley + ts - the TS context
7871847ff0e1SMatthew G. Knepley . t - current timestep
7872847ff0e1SMatthew G. Knepley . U - state vector
7873847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7874847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7875847ff0e1SMatthew G. Knepley - ctx - an optional user context
7876847ff0e1SMatthew G. Knepley 
7877847ff0e1SMatthew G. Knepley   Output Parameters:
7878847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7879847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7880847ff0e1SMatthew G. Knepley 
7881847ff0e1SMatthew G. Knepley   Level: intermediate
7882847ff0e1SMatthew G. Knepley 
7883847ff0e1SMatthew G. Knepley   Notes:
7884847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7885847ff0e1SMatthew G. Knepley 
7886847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7887847ff0e1SMatthew G. Knepley 
7888847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7889847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7890847ff0e1SMatthew G. Knepley 
7891847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7892847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7893847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7894847ff0e1SMatthew G. Knepley 
7895847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7896847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7897847ff0e1SMatthew G. Knepley @*/
7898847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7899847ff0e1SMatthew G. Knepley {
7900847ff0e1SMatthew G. Knepley   SNES           snes;
7901847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7902847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7903847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7904847ff0e1SMatthew G. Knepley 
7905847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7906c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7907847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7908847ff0e1SMatthew G. Knepley   if (!color) {
7909847ff0e1SMatthew G. Knepley     DM         dm;
7910847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7911847ff0e1SMatthew G. Knepley 
7912847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7913847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7914847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7915847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7916847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7917847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7918847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7919847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7920847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7921847ff0e1SMatthew G. Knepley     } else {
7922847ff0e1SMatthew G. Knepley       MatColoring mc;
7923847ff0e1SMatthew G. Knepley 
7924847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7925847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7926847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7927847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7928847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7929847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7930847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7931847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7932847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7933847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7934847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7935847ff0e1SMatthew G. Knepley     }
7936847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7937847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7938847ff0e1SMatthew G. Knepley   }
7939847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7940847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7941847ff0e1SMatthew G. Knepley   if (J != B) {
7942847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7943847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7944847ff0e1SMatthew G. Knepley   }
7945847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7946847ff0e1SMatthew G. Knepley }
794793b34091SDebojyoti Ghosh 
794893b34091SDebojyoti Ghosh #undef __FUNCT__
7949cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSSetFunctionDomainError"
7950cb9d8021SPierre Barbier de Reuille /*@
7951cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7952cb9d8021SPierre Barbier de Reuille 
7953cb9d8021SPierre Barbier de Reuille     Input Parameters:
7954cb9d8021SPierre Barbier de Reuille     ts - the TS context
7955cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7956cb9d8021SPierre Barbier de Reuille 
7957cb9d8021SPierre Barbier de Reuille     Level: intermediate
7958cb9d8021SPierre Barbier de Reuille 
7959cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7960cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7961cb9d8021SPierre Barbier de Reuille @*/
7962cb9d8021SPierre Barbier de Reuille 
7963d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7964cb9d8021SPierre Barbier de Reuille {
7965cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7966cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7967cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7968cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7969cb9d8021SPierre Barbier de Reuille }
7970cb9d8021SPierre Barbier de Reuille 
7971cb9d8021SPierre Barbier de Reuille #undef __FUNCT__
7972cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSFunctionDomainError"
7973cb9d8021SPierre Barbier de Reuille /*@
7974cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7975cb9d8021SPierre Barbier de Reuille 
7976cb9d8021SPierre Barbier de Reuille     Input Parameters:
7977cb9d8021SPierre Barbier de Reuille     ts - the TS context
7978cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7979d183316bSPierre Barbier de Reuille     Y - state vector to check.
7980cb9d8021SPierre Barbier de Reuille 
7981cb9d8021SPierre Barbier de Reuille     Output Parameter:
7982cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7983cb9d8021SPierre Barbier de Reuille 
7984cb9d8021SPierre Barbier de Reuille     Note:
7985cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7986cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
798796a0c994SBarry Smith 
798896a0c994SBarry Smith     Level: advanced
7989cb9d8021SPierre Barbier de Reuille @*/
7990d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7991cb9d8021SPierre Barbier de Reuille {
7992cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
7993cb9d8021SPierre Barbier de Reuille 
7994cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7995cb9d8021SPierre Barbier de Reuille 
7996cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7997cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7998cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7999d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
8000cb9d8021SPierre Barbier de Reuille   }
8001cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
8002cb9d8021SPierre Barbier de Reuille }
80031ceb14c0SBarry Smith 
80041ceb14c0SBarry Smith #undef  __FUNCT__
8005e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone"
800693b34091SDebojyoti Ghosh /*@C
8007e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
800893b34091SDebojyoti Ghosh 
800993b34091SDebojyoti Ghosh   Collective on MPI_Comm
801093b34091SDebojyoti Ghosh 
801193b34091SDebojyoti Ghosh   Input Parameter:
801293b34091SDebojyoti Ghosh . tsin    - The input TS
801393b34091SDebojyoti Ghosh 
801493b34091SDebojyoti Ghosh   Output Parameter:
8015e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
801693b34091SDebojyoti Ghosh 
80175eca1a21SEmil Constantinescu   Notes:
80185eca1a21SEmil 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.
80195eca1a21SEmil Constantinescu 
8020baa10174SEmil 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);
80215eca1a21SEmil Constantinescu 
80225eca1a21SEmil Constantinescu   Level: developer
802393b34091SDebojyoti Ghosh 
8024e5168f73SEmil Constantinescu .keywords: TS, clone
8025e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
802693b34091SDebojyoti Ghosh @*/
8027baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
802893b34091SDebojyoti Ghosh {
802993b34091SDebojyoti Ghosh   TS             t;
803093b34091SDebojyoti Ghosh   PetscErrorCode ierr;
8031dc846ba4SSatish Balay   SNES           snes_start;
8032dc846ba4SSatish Balay   DM             dm;
8033dc846ba4SSatish Balay   TSType         type;
803493b34091SDebojyoti Ghosh 
803593b34091SDebojyoti Ghosh   PetscFunctionBegin;
803693b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
803793b34091SDebojyoti Ghosh   *tsout = NULL;
803893b34091SDebojyoti Ghosh 
80397a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
804093b34091SDebojyoti Ghosh 
804193b34091SDebojyoti Ghosh   /* General TS description */
804293b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
804393b34091SDebojyoti Ghosh   t->setupcalled       = 0;
804493b34091SDebojyoti Ghosh   t->ksp_its           = 0;
804593b34091SDebojyoti Ghosh   t->snes_its          = 0;
804693b34091SDebojyoti Ghosh   t->nwork             = 0;
804793b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
804893b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
804993b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
805093b34091SDebojyoti Ghosh 
805134561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
805234561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
8053d15a3a53SEmil Constantinescu 
805493b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
805593b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
805693b34091SDebojyoti Ghosh 
805793b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
805851699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
805993b34091SDebojyoti Ghosh 
8060*e7069c78SShri   t->trajectory = tsin->trajectory;
8061*e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
8062*e7069c78SShri 
8063*e7069c78SShri   t->event = tsin->event;
8064*e7069c78SShri   t->event->refct++;
8065*e7069c78SShri 
806693b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
806793b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
8068*e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
806993b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
807093b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
807193b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
8072*e7069c78SShri   t->total_steps       = tsin->total_steps;
807393b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
807493b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
807593b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
807693b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
807793b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
807893b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
807993b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
808093b34091SDebojyoti Ghosh 
808193b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
808293b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
808393b34091SDebojyoti Ghosh 
808493b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
808593b34091SDebojyoti Ghosh 
808693b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
808793b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
808893b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
808993b34091SDebojyoti Ghosh 
809093b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
809193b34091SDebojyoti Ghosh 
80920d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
80930d4fed19SBarry Smith     PetscInt i;
80940d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
80950d4fed19SBarry Smith     for (i=0; i<10; i++) {
80960d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
80970d4fed19SBarry Smith     }
80980d4fed19SBarry Smith   }
809993b34091SDebojyoti Ghosh   *tsout = t;
810093b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
810193b34091SDebojyoti Ghosh }
8102