xref: /petsc/src/ts/interface/ts.c (revision a6772fa2bbc4e572600b24dd7daf9f5313d00bac)
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:
1254d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
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
149b3d3934dSBarry Smith .  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
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];
1706991f827SBarry Smith   const char             *defaultType;
1716991f827SBarry Smith   char                   typeName[256];
172bdad233fSMatthew Knepley 
173bdad233fSMatthew Knepley   PetscFunctionBegin;
1740700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1753194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
1766991f827SBarry Smith   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
1776991f827SBarry Smith   else defaultType = TSEULER;
1786991f827SBarry Smith 
1796991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
1806991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
1816991f827SBarry Smith   if (opt) {
1826991f827SBarry Smith     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
1836991f827SBarry Smith   } else {
1846991f827SBarry Smith     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
1856991f827SBarry Smith   }
186bdad233fSMatthew Knepley 
187bdad233fSMatthew Knepley   /* Handle generic TS options */
1880298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1890298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1900298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
19131748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
19231748224SBarry Smith   if (flg) {
19331748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
19431748224SBarry Smith   }
19549354f04SShri Abhyankar   ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr);
19649354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1970298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);CHKERRQ(ierr);
1980298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1990298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
2000298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
2010298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
202bdad233fSMatthew Knepley 
20356f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
20456f85f32SBarry Smith   {
20556f85f32SBarry Smith   PetscBool set;
20656f85f32SBarry Smith   flg  = PETSC_FALSE;
20756f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
20856f85f32SBarry Smith   if (set) {
20956f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
21056f85f32SBarry Smith   }
21156f85f32SBarry Smith   }
21256f85f32SBarry Smith #endif
21356f85f32SBarry Smith 
214bdad233fSMatthew Knepley   /* Monitor options */
215fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
216fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
217fde5950dSBarry Smith   ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr);
218fde5950dSBarry Smith 
2195180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2205180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
2215180491cSLisandro Dalcin 
2224f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
223b3603a34SBarry Smith   if (opt) {
2240b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2253923b477SBarry Smith     PetscInt       howoften = 1;
226b3603a34SBarry Smith 
2270298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2286ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2294f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
230bdad233fSMatthew Knepley   }
2316ba87a44SLisandro Dalcin 
2324f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
233ef20d060SBarry Smith   if (opt) {
2340b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2353923b477SBarry Smith     PetscInt       howoften = 1;
236ef20d060SBarry Smith 
2370298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
2386ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2394f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
240ef20d060SBarry Smith   }
2416934998bSLisandro Dalcin 
2426934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2436934998bSLisandro Dalcin   if (opt) {
2446934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2456934998bSLisandro Dalcin     PetscInt       howoften = 1;
2466934998bSLisandro Dalcin 
2476934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2486ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2496934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2506934998bSLisandro Dalcin   }
251201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
252201da799SBarry Smith   if (opt) {
253201da799SBarry Smith     TSMonitorLGCtx ctx;
254201da799SBarry Smith     PetscInt       howoften = 1;
255201da799SBarry Smith 
2560298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2576ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
258201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
259201da799SBarry Smith   }
260201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
261201da799SBarry Smith   if (opt) {
262201da799SBarry Smith     TSMonitorLGCtx ctx;
263201da799SBarry Smith     PetscInt       howoften = 1;
264201da799SBarry Smith 
2650298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2666ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
267201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
268201da799SBarry Smith   }
2698189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2708189c53fSBarry Smith   if (opt) {
2718189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2728189c53fSBarry Smith     PetscInt          howoften = 1;
2738189c53fSBarry Smith 
2740298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
2756934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2768189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2778189c53fSBarry Smith   }
278ef20d060SBarry Smith   opt  = PETSC_FALSE;
2790dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
280a7cc72afSBarry Smith   if (opt) {
28183a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
28283a4ac43SBarry Smith     PetscInt         howoften = 1;
283a80ad3e0SBarry Smith 
2840298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2856934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
28683a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
287bdad233fSMatthew Knepley   }
288fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2899110b2e7SHong Zhang   ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr);
2909110b2e7SHong Zhang   if (opt) {
2919110b2e7SHong Zhang     TSMonitorDrawCtx ctx;
2929110b2e7SHong Zhang     PetscInt         howoften = 1;
2939110b2e7SHong Zhang 
2949110b2e7SHong Zhang     ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr);
2956934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2969110b2e7SHong Zhang     ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2979110b2e7SHong Zhang   }
2989110b2e7SHong Zhang   opt  = PETSC_FALSE;
2992d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
3002d5ee99bSBarry Smith   if (opt) {
3012d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
3022d5ee99bSBarry Smith     PetscReal        bounds[4];
3032d5ee99bSBarry Smith     PetscInt         n = 4;
3042d5ee99bSBarry Smith     PetscDraw        draw;
3056934998bSLisandro Dalcin     PetscDrawAxis    axis;
3062d5ee99bSBarry Smith 
3072d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
3082d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
3096934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
3102d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
3116934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
3126934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
3136934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
3142d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3152d5ee99bSBarry Smith   }
3162d5ee99bSBarry Smith   opt  = PETSC_FALSE;
3170dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
3183a471f94SBarry Smith   if (opt) {
31983a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
32083a4ac43SBarry Smith     PetscInt         howoften = 1;
3213a471f94SBarry Smith 
3220298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
3236934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
32483a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3253a471f94SBarry Smith   }
326fde5950dSBarry Smith 
327ed81e22dSJed Brown   opt  = PETSC_FALSE;
32891b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
329ed81e22dSJed Brown   if (flg) {
330ed81e22dSJed Brown     const char *ptr,*ptr2;
331ed81e22dSJed Brown     char       *filetemplate;
332ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
333ed81e22dSJed Brown     /* Do some cursory validation of the input. */
334ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
335ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
336ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
337ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
338ce94432eSBarry Smith       if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts");
339ed81e22dSJed Brown       if (ptr2) break;
340ed81e22dSJed Brown     }
341ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
342ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
343ed81e22dSJed Brown   }
344bdad233fSMatthew Knepley 
345d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
346d1212d36SBarry Smith   if (flg) {
347d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
348d1212d36SBarry Smith     int                  ray = 0;
349d1212d36SBarry Smith     DMDADirection        ddir;
350d1212d36SBarry Smith     DM                   da;
351d1212d36SBarry Smith     PetscMPIInt          rank;
352d1212d36SBarry Smith 
353ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
354d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
355d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
356ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
357d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
358d1212d36SBarry Smith 
359d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
360b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
361d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
362d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
363ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
364d1212d36SBarry Smith     if (!rank) {
365d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
366d1212d36SBarry Smith     }
36751b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
368d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
369d1212d36SBarry Smith   }
37051b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
37151b4a12fSMatthew G. Knepley   if (flg) {
37251b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
37351b4a12fSMatthew G. Knepley     int                 ray = 0;
37451b4a12fSMatthew G. Knepley     DMDADirection       ddir;
37551b4a12fSMatthew G. Knepley     DM                  da;
37651b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
377d1212d36SBarry Smith 
37851b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
37951b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
38051b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
38151b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
38251b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3831c3436cfSJed Brown 
38451b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
385b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
38651b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
38751b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
38851b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
38951b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
39051b4a12fSMatthew G. Knepley   }
391a7a1495cSBarry Smith 
392b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
393b3d3934dSBarry Smith   if (opt) {
394b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
395b3d3934dSBarry Smith 
396b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
397b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
398b3d3934dSBarry Smith   }
399b3d3934dSBarry Smith 
400847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
401847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
402847ff0e1SMatthew G. Knepley   if (flg) {
403847ff0e1SMatthew G. Knepley     DM   dm;
404847ff0e1SMatthew G. Knepley     DMTS tdm;
405847ff0e1SMatthew G. Knepley 
406847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
407847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
408847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
409847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
410847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
411847ff0e1SMatthew G. Knepley   }
412847ff0e1SMatthew G. Knepley 
413ee346746SBarry Smith   if (ts->adapt) {
414ee346746SBarry Smith     ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
415ee346746SBarry Smith   }
416d763cef2SBarry Smith 
417d763cef2SBarry Smith   /* Handle specific TS options */
418d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4196991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
420d763cef2SBarry Smith   }
421fbc52257SHong Zhang 
42268bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4234f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
42468bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
42568bece0bSHong Zhang   if (tflg) {
42668bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
42768bece0bSHong Zhang   }
4284f122a70SLisandro Dalcin   tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE;
42968bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr);
43068bece0bSHong Zhang   if (flg) {
43168bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
43268bece0bSHong Zhang     ts->adjoint_solve = tflg;
43368bece0bSHong Zhang   }
434d763cef2SBarry Smith 
435d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4360633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
437fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
438d763cef2SBarry Smith 
4394f122a70SLisandro Dalcin   if (ts->trajectory) {
4404f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
441d763cef2SBarry Smith   }
44268bece0bSHong Zhang 
4434f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4444f122a70SLisandro Dalcin   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);}
4454f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
446d763cef2SBarry Smith   PetscFunctionReturn(0);
447d763cef2SBarry Smith }
448d763cef2SBarry Smith 
449d763cef2SBarry Smith #undef __FUNCT__
450bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory"
451d2daff3dSHong Zhang /*@
452bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
453d2daff3dSHong Zhang 
454d2daff3dSHong Zhang    Collective on TS
455d2daff3dSHong Zhang 
456d2daff3dSHong Zhang    Input Parameters:
457bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
458bc952696SBarry Smith 
45968bece0bSHong Zhang Note: This routine should be called after all TS options have been set
460d2daff3dSHong Zhang 
461d2daff3dSHong Zhang    Level: intermediate
462d2daff3dSHong Zhang 
463bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve()
464d2daff3dSHong Zhang 
465bc952696SBarry Smith .keywords: TS, set, checkpoint,
466d2daff3dSHong Zhang @*/
467bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
468d2daff3dSHong Zhang {
469bc952696SBarry Smith   PetscErrorCode ierr;
470bc952696SBarry Smith 
471d2daff3dSHong Zhang   PetscFunctionBegin;
472d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
473bc952696SBarry Smith   if (!ts->trajectory) {
474bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
475972caf09SHong Zhang     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
476bc952696SBarry Smith   }
477d2daff3dSHong Zhang   PetscFunctionReturn(0);
478d2daff3dSHong Zhang }
479d2daff3dSHong Zhang 
480a7a1495cSBarry Smith #undef __FUNCT__
481a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian"
482a7a1495cSBarry Smith /*@
483a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
484a7a1495cSBarry Smith       set with TSSetRHSJacobian().
485a7a1495cSBarry Smith 
486a7a1495cSBarry Smith    Collective on TS and Vec
487a7a1495cSBarry Smith 
488a7a1495cSBarry Smith    Input Parameters:
489316643e7SJed Brown +  ts - the TS context
490a7a1495cSBarry Smith .  t - current timestep
4910910c330SBarry Smith -  U - input vector
492a7a1495cSBarry Smith 
493a7a1495cSBarry Smith    Output Parameters:
494a7a1495cSBarry Smith +  A - Jacobian matrix
495a7a1495cSBarry Smith .  B - optional preconditioning matrix
496a7a1495cSBarry Smith -  flag - flag indicating matrix structure
497a7a1495cSBarry Smith 
498a7a1495cSBarry Smith    Notes:
499a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
500a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
501a7a1495cSBarry Smith 
50294b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
503a7a1495cSBarry Smith    flag parameter.
504a7a1495cSBarry Smith 
505a7a1495cSBarry Smith    Level: developer
506a7a1495cSBarry Smith 
507a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
508a7a1495cSBarry Smith 
50994b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
510a7a1495cSBarry Smith @*/
511d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
512a7a1495cSBarry Smith {
513dfbe8321SBarry Smith   PetscErrorCode ierr;
514270bf2e7SJed Brown   PetscObjectState Ustate;
51524989b8cSPeter Brune   DM             dm;
516942e3340SBarry Smith   DMTS           tsdm;
51724989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
51824989b8cSPeter Brune   void           *ctx;
51924989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
520b2df71adSDebojyoti Ghosh   TSRHSFunction  rhsfunction;
521a7a1495cSBarry Smith 
522a7a1495cSBarry Smith   PetscFunctionBegin;
5230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5240910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5250910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
52624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
527942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
52824989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5290298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
530b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
53159e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
532b2df71adSDebojyoti Ghosh   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
5330e4ef248SJed Brown     PetscFunctionReturn(0);
534f8ede8e7SJed Brown   }
535d90be118SSean Farley 
536ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
537d90be118SSean Farley 
538e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
53994ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
54094ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
54194ab13aaSBarry Smith     if (A != B) {
54294ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
54394ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
544e1244c69SJed Brown     }
545e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
546e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
547e1244c69SJed Brown   }
548e1244c69SJed Brown 
54924989b8cSPeter Brune   if (rhsjacobianfunc) {
5506cd88445SBarry Smith     PetscBool missing;
55194ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
552a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
553d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
554a7a1495cSBarry Smith     PetscStackPop;
55594ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
5566cd88445SBarry Smith     if (A) {
5576cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
5586cd88445SBarry 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");
5596cd88445SBarry Smith     }
5606cd88445SBarry Smith     if (B && B != A) {
5616cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
5626cd88445SBarry 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");
5636cd88445SBarry Smith     }
564ef66eb69SBarry Smith   } else {
56594ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
56694ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
567ef66eb69SBarry Smith   }
5680e4ef248SJed Brown   ts->rhsjacobian.time       = t;
5690910c330SBarry Smith   ts->rhsjacobian.X          = U;
57059e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
571a7a1495cSBarry Smith   PetscFunctionReturn(0);
572a7a1495cSBarry Smith }
573a7a1495cSBarry Smith 
5744a2ae208SSatish Balay #undef __FUNCT__
5754a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
576316643e7SJed Brown /*@
577d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
578d763cef2SBarry Smith 
579316643e7SJed Brown    Collective on TS and Vec
580316643e7SJed Brown 
581316643e7SJed Brown    Input Parameters:
582316643e7SJed Brown +  ts - the TS context
583316643e7SJed Brown .  t - current time
5840910c330SBarry Smith -  U - state vector
585316643e7SJed Brown 
586316643e7SJed Brown    Output Parameter:
587316643e7SJed Brown .  y - right hand side
588316643e7SJed Brown 
589316643e7SJed Brown    Note:
590316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
591316643e7SJed Brown    is used internally within the nonlinear solvers.
592316643e7SJed Brown 
593316643e7SJed Brown    Level: developer
594316643e7SJed Brown 
595316643e7SJed Brown .keywords: TS, compute
596316643e7SJed Brown 
597316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
598316643e7SJed Brown @*/
5990910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
600d763cef2SBarry Smith {
601dfbe8321SBarry Smith   PetscErrorCode ierr;
60224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
60324989b8cSPeter Brune   TSIFunction    ifunction;
60424989b8cSPeter Brune   void           *ctx;
60524989b8cSPeter Brune   DM             dm;
60624989b8cSPeter Brune 
607d763cef2SBarry Smith   PetscFunctionBegin;
6080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6090910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6100700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
61124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
61224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6130298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
614d763cef2SBarry Smith 
615ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
616d763cef2SBarry Smith 
6170910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
61824989b8cSPeter Brune   if (rhsfunction) {
619d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6200910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
621d763cef2SBarry Smith     PetscStackPop;
622214bc6a2SJed Brown   } else {
623214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
624b2cd27e8SJed Brown   }
62544a41b28SSean Farley 
6260910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
627d763cef2SBarry Smith   PetscFunctionReturn(0);
628d763cef2SBarry Smith }
629d763cef2SBarry Smith 
6304a2ae208SSatish Balay #undef __FUNCT__
631ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
632ef20d060SBarry Smith /*@
633ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
634ef20d060SBarry Smith 
635ef20d060SBarry Smith    Collective on TS and Vec
636ef20d060SBarry Smith 
637ef20d060SBarry Smith    Input Parameters:
638ef20d060SBarry Smith +  ts - the TS context
639ef20d060SBarry Smith -  t - current time
640ef20d060SBarry Smith 
641ef20d060SBarry Smith    Output Parameter:
6420910c330SBarry Smith .  U - the solution
643ef20d060SBarry Smith 
644ef20d060SBarry Smith    Note:
645ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
646ef20d060SBarry Smith    is used internally within the nonlinear solvers.
647ef20d060SBarry Smith 
648ef20d060SBarry Smith    Level: developer
649ef20d060SBarry Smith 
650ef20d060SBarry Smith .keywords: TS, compute
651ef20d060SBarry Smith 
652abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
653ef20d060SBarry Smith @*/
6540910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
655ef20d060SBarry Smith {
656ef20d060SBarry Smith   PetscErrorCode     ierr;
657ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
658ef20d060SBarry Smith   void               *ctx;
659ef20d060SBarry Smith   DM                 dm;
660ef20d060SBarry Smith 
661ef20d060SBarry Smith   PetscFunctionBegin;
662ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6630910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
664ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
665ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
666ef20d060SBarry Smith 
667ef20d060SBarry Smith   if (solutionfunction) {
6689b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6690910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
670ef20d060SBarry Smith     PetscStackPop;
671ef20d060SBarry Smith   }
672ef20d060SBarry Smith   PetscFunctionReturn(0);
673ef20d060SBarry Smith }
6749b7cd975SBarry Smith #undef __FUNCT__
6759b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
6769b7cd975SBarry Smith /*@
6779b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6789b7cd975SBarry Smith 
6799b7cd975SBarry Smith    Collective on TS and Vec
6809b7cd975SBarry Smith 
6819b7cd975SBarry Smith    Input Parameters:
6829b7cd975SBarry Smith +  ts - the TS context
6839b7cd975SBarry Smith -  t - current time
6849b7cd975SBarry Smith 
6859b7cd975SBarry Smith    Output Parameter:
6869b7cd975SBarry Smith .  U - the function value
6879b7cd975SBarry Smith 
6889b7cd975SBarry Smith    Note:
6899b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
6909b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6919b7cd975SBarry Smith 
6929b7cd975SBarry Smith    Level: developer
6939b7cd975SBarry Smith 
6949b7cd975SBarry Smith .keywords: TS, compute
6959b7cd975SBarry Smith 
6969b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
6979b7cd975SBarry Smith @*/
6989b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
6999b7cd975SBarry Smith {
7009b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7019b7cd975SBarry Smith   void               *ctx;
7029b7cd975SBarry Smith   DM                 dm;
7039b7cd975SBarry Smith 
7049b7cd975SBarry Smith   PetscFunctionBegin;
7059b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7069b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7079b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7089b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7099b7cd975SBarry Smith 
7109b7cd975SBarry Smith   if (forcing) {
7119b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7129b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7139b7cd975SBarry Smith     PetscStackPop;
7149b7cd975SBarry Smith   }
7159b7cd975SBarry Smith   PetscFunctionReturn(0);
7169b7cd975SBarry Smith }
717ef20d060SBarry Smith 
718ef20d060SBarry Smith #undef __FUNCT__
719214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
720214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
721214bc6a2SJed Brown {
7222dd45cf8SJed Brown   Vec            F;
723214bc6a2SJed Brown   PetscErrorCode ierr;
724214bc6a2SJed Brown 
725214bc6a2SJed Brown   PetscFunctionBegin;
7260298fd71SBarry Smith   *Frhs = NULL;
7270298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
728214bc6a2SJed Brown   if (!ts->Frhs) {
7292dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
730214bc6a2SJed Brown   }
731214bc6a2SJed Brown   *Frhs = ts->Frhs;
732214bc6a2SJed Brown   PetscFunctionReturn(0);
733214bc6a2SJed Brown }
734214bc6a2SJed Brown 
735214bc6a2SJed Brown #undef __FUNCT__
736214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
737214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
738214bc6a2SJed Brown {
739214bc6a2SJed Brown   Mat            A,B;
7402dd45cf8SJed Brown   PetscErrorCode ierr;
741214bc6a2SJed Brown 
742214bc6a2SJed Brown   PetscFunctionBegin;
743c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
744c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
7450298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
746214bc6a2SJed Brown   if (Arhs) {
747214bc6a2SJed Brown     if (!ts->Arhs) {
748214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
749214bc6a2SJed Brown     }
750214bc6a2SJed Brown     *Arhs = ts->Arhs;
751214bc6a2SJed Brown   }
752214bc6a2SJed Brown   if (Brhs) {
753214bc6a2SJed Brown     if (!ts->Brhs) {
754bdb70873SJed Brown       if (A != B) {
755214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
756bdb70873SJed Brown       } else {
757bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
75851699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
759bdb70873SJed Brown       }
760214bc6a2SJed Brown     }
761214bc6a2SJed Brown     *Brhs = ts->Brhs;
762214bc6a2SJed Brown   }
763214bc6a2SJed Brown   PetscFunctionReturn(0);
764214bc6a2SJed Brown }
765214bc6a2SJed Brown 
766214bc6a2SJed Brown #undef __FUNCT__
767316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
768316643e7SJed Brown /*@
7690910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
770316643e7SJed Brown 
771316643e7SJed Brown    Collective on TS and Vec
772316643e7SJed Brown 
773316643e7SJed Brown    Input Parameters:
774316643e7SJed Brown +  ts - the TS context
775316643e7SJed Brown .  t - current time
7760910c330SBarry Smith .  U - state vector
7770910c330SBarry Smith .  Udot - time derivative of state vector
778214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
779316643e7SJed Brown 
780316643e7SJed Brown    Output Parameter:
781316643e7SJed Brown .  Y - right hand side
782316643e7SJed Brown 
783316643e7SJed Brown    Note:
784316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
785316643e7SJed Brown    is used internally within the nonlinear solvers.
786316643e7SJed Brown 
787316643e7SJed Brown    If the user did did not write their equations in implicit form, this
788316643e7SJed Brown    function recasts them in implicit form.
789316643e7SJed Brown 
790316643e7SJed Brown    Level: developer
791316643e7SJed Brown 
792316643e7SJed Brown .keywords: TS, compute
793316643e7SJed Brown 
794316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
795316643e7SJed Brown @*/
7960910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
797316643e7SJed Brown {
798316643e7SJed Brown   PetscErrorCode ierr;
79924989b8cSPeter Brune   TSIFunction    ifunction;
80024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
80124989b8cSPeter Brune   void           *ctx;
80224989b8cSPeter Brune   DM             dm;
803316643e7SJed Brown 
804316643e7SJed Brown   PetscFunctionBegin;
8050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8060910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8070910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8080700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
809316643e7SJed Brown 
81024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
81124989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8120298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
81324989b8cSPeter Brune 
814ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
815d90be118SSean Farley 
8160910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
81724989b8cSPeter Brune   if (ifunction) {
818316643e7SJed Brown     PetscStackPush("TS user implicit function");
8190910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
820316643e7SJed Brown     PetscStackPop;
821214bc6a2SJed Brown   }
822214bc6a2SJed Brown   if (imex) {
82324989b8cSPeter Brune     if (!ifunction) {
8240910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8252dd45cf8SJed Brown     }
82624989b8cSPeter Brune   } else if (rhsfunction) {
82724989b8cSPeter Brune     if (ifunction) {
828214bc6a2SJed Brown       Vec Frhs;
829214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8300910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
831214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8322dd45cf8SJed Brown     } else {
8330910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8340910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
835316643e7SJed Brown     }
8364a6899ffSJed Brown   }
8370910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
838316643e7SJed Brown   PetscFunctionReturn(0);
839316643e7SJed Brown }
840316643e7SJed Brown 
841316643e7SJed Brown #undef __FUNCT__
842316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
843316643e7SJed Brown /*@
844316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
845316643e7SJed Brown 
846316643e7SJed Brown    Collective on TS and Vec
847316643e7SJed Brown 
848316643e7SJed Brown    Input
849316643e7SJed Brown       Input Parameters:
850316643e7SJed Brown +  ts - the TS context
851316643e7SJed Brown .  t - current timestep
8520910c330SBarry Smith .  U - state vector
8530910c330SBarry Smith .  Udot - time derivative of state vector
854214bc6a2SJed Brown .  shift - shift to apply, see note below
855214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
856316643e7SJed Brown 
857316643e7SJed Brown    Output Parameters:
858316643e7SJed Brown +  A - Jacobian matrix
859316643e7SJed Brown .  B - optional preconditioning matrix
860316643e7SJed Brown -  flag - flag indicating matrix structure
861316643e7SJed Brown 
862316643e7SJed Brown    Notes:
8630910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
864316643e7SJed Brown 
8650910c330SBarry Smith    dF/dU + shift*dF/dUdot
866316643e7SJed Brown 
867316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
868316643e7SJed Brown    is used internally within the nonlinear solvers.
869316643e7SJed Brown 
870316643e7SJed Brown    Level: developer
871316643e7SJed Brown 
872316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
873316643e7SJed Brown 
874316643e7SJed Brown .seealso:  TSSetIJacobian()
87563495f91SJed Brown @*/
876d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
877316643e7SJed Brown {
878316643e7SJed Brown   PetscErrorCode ierr;
87924989b8cSPeter Brune   TSIJacobian    ijacobian;
88024989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
88124989b8cSPeter Brune   DM             dm;
88224989b8cSPeter Brune   void           *ctx;
883316643e7SJed Brown 
884316643e7SJed Brown   PetscFunctionBegin;
8850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8860910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8870910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
888316643e7SJed Brown   PetscValidPointer(A,6);
88994ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
890316643e7SJed Brown   PetscValidPointer(B,7);
89194ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
89224989b8cSPeter Brune 
89324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
89424989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
8950298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
89624989b8cSPeter Brune 
897ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
898316643e7SJed Brown 
89994ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
90024989b8cSPeter Brune   if (ijacobian) {
9016cd88445SBarry Smith     PetscBool missing;
902316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
903d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
904316643e7SJed Brown     PetscStackPop;
9056cd88445SBarry Smith     if (A) {
9066cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9076cd88445SBarry 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");
9086cd88445SBarry Smith     }
9096cd88445SBarry Smith     if (B && B != A) {
9106cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9116cd88445SBarry 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");
9126cd88445SBarry Smith     }
9134a6899ffSJed Brown   }
914214bc6a2SJed Brown   if (imex) {
915b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
91694ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
91794ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
91894ab13aaSBarry Smith       if (A != B) {
91994ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
92094ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
921214bc6a2SJed Brown       }
922214bc6a2SJed Brown     }
923214bc6a2SJed Brown   } else {
924e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
925e1244c69SJed Brown     if (rhsjacobian) {
926bd373395SBarry Smith       if (ijacobian) {
927214bc6a2SJed Brown         ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
928bd373395SBarry Smith       } else {
929bd373395SBarry Smith         ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr);
930214bc6a2SJed Brown       }
931d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
932e1244c69SJed Brown     }
93394ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
934e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
935e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
93694ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
93794ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
93894ab13aaSBarry Smith       if (A != B) {
93994ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
94094ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
941316643e7SJed Brown       }
942e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
943e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
944e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
94594ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
94694ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
94794ab13aaSBarry Smith         if (A != B) {
94894ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
94994ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
950214bc6a2SJed Brown         }
951316643e7SJed Brown       }
95294ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
95394ab13aaSBarry Smith       if (A != B) {
95494ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
955316643e7SJed Brown       }
956316643e7SJed Brown     }
957316643e7SJed Brown   }
95894ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
959316643e7SJed Brown   PetscFunctionReturn(0);
960316643e7SJed Brown }
961316643e7SJed Brown 
962316643e7SJed Brown #undef __FUNCT__
9634a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
964d763cef2SBarry Smith /*@C
965d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
966b5abc632SBarry Smith     where U_t = G(t,u).
967d763cef2SBarry Smith 
9683f9fe445SBarry Smith     Logically Collective on TS
969d763cef2SBarry Smith 
970d763cef2SBarry Smith     Input Parameters:
971d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
9720298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
973d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
974d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
9750298fd71SBarry Smith           function evaluation routine (may be NULL)
976d763cef2SBarry Smith 
977d763cef2SBarry Smith     Calling sequence of func:
97887828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
979d763cef2SBarry Smith 
980d763cef2SBarry Smith +   t - current timestep
981d763cef2SBarry Smith .   u - input vector
982d763cef2SBarry Smith .   F - function vector
983d763cef2SBarry Smith -   ctx - [optional] user-defined function context
984d763cef2SBarry Smith 
985d763cef2SBarry Smith     Level: beginner
986d763cef2SBarry Smith 
9872bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
9882bbac0d3SBarry Smith 
989d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
990d763cef2SBarry Smith 
991ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
992d763cef2SBarry Smith @*/
993089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
994d763cef2SBarry Smith {
995089b2837SJed Brown   PetscErrorCode ierr;
996089b2837SJed Brown   SNES           snes;
9970298fd71SBarry Smith   Vec            ralloc = NULL;
99824989b8cSPeter Brune   DM             dm;
999d763cef2SBarry Smith 
1000089b2837SJed Brown   PetscFunctionBegin;
10010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1002ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
100324989b8cSPeter Brune 
100424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
100524989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1006089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1007e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1008e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1009e856ceecSJed Brown     r = ralloc;
1010e856ceecSJed Brown   }
1011089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1012e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1013d763cef2SBarry Smith   PetscFunctionReturn(0);
1014d763cef2SBarry Smith }
1015d763cef2SBarry Smith 
10164a2ae208SSatish Balay #undef __FUNCT__
1017ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
1018ef20d060SBarry Smith /*@C
1019abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1020ef20d060SBarry Smith 
1021ef20d060SBarry Smith     Logically Collective on TS
1022ef20d060SBarry Smith 
1023ef20d060SBarry Smith     Input Parameters:
1024ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1025ef20d060SBarry Smith .   f - routine for evaluating the solution
1026ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10270298fd71SBarry Smith           function evaluation routine (may be NULL)
1028ef20d060SBarry Smith 
1029ef20d060SBarry Smith     Calling sequence of func:
1030ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1031ef20d060SBarry Smith 
1032ef20d060SBarry Smith +   t - current timestep
1033ef20d060SBarry Smith .   u - output vector
1034ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1035ef20d060SBarry Smith 
1036abd5a294SJed Brown     Notes:
1037abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1038abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1039abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1040abd5a294SJed Brown 
10414f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1042abd5a294SJed Brown 
1043ef20d060SBarry Smith     Level: beginner
1044ef20d060SBarry Smith 
1045ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1046ef20d060SBarry Smith 
10479b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
1048ef20d060SBarry Smith @*/
1049ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1050ef20d060SBarry Smith {
1051ef20d060SBarry Smith   PetscErrorCode ierr;
1052ef20d060SBarry Smith   DM             dm;
1053ef20d060SBarry Smith 
1054ef20d060SBarry Smith   PetscFunctionBegin;
1055ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1056ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1057ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1058ef20d060SBarry Smith   PetscFunctionReturn(0);
1059ef20d060SBarry Smith }
1060ef20d060SBarry Smith 
1061ef20d060SBarry Smith #undef __FUNCT__
10629b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
10639b7cd975SBarry Smith /*@C
10649b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
10659b7cd975SBarry Smith 
10669b7cd975SBarry Smith     Logically Collective on TS
10679b7cd975SBarry Smith 
10689b7cd975SBarry Smith     Input Parameters:
10699b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
10709b7cd975SBarry Smith .   f - routine for evaluating the forcing function
10719b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
10720298fd71SBarry Smith           function evaluation routine (may be NULL)
10739b7cd975SBarry Smith 
10749b7cd975SBarry Smith     Calling sequence of func:
10759b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
10769b7cd975SBarry Smith 
10779b7cd975SBarry Smith +   t - current timestep
10789b7cd975SBarry Smith .   u - output vector
10799b7cd975SBarry Smith -   ctx - [optional] user-defined function context
10809b7cd975SBarry Smith 
10819b7cd975SBarry Smith     Notes:
10829b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
10839b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
10849b7cd975SBarry Smith 
10859b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
10869b7cd975SBarry Smith 
10879b7cd975SBarry Smith     Level: beginner
10889b7cd975SBarry Smith 
10899b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
10909b7cd975SBarry Smith 
10919b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
10929b7cd975SBarry Smith @*/
10939b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
10949b7cd975SBarry Smith {
10959b7cd975SBarry Smith   PetscErrorCode ierr;
10969b7cd975SBarry Smith   DM             dm;
10979b7cd975SBarry Smith 
10989b7cd975SBarry Smith   PetscFunctionBegin;
10999b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11009b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
11019b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
11029b7cd975SBarry Smith   PetscFunctionReturn(0);
11039b7cd975SBarry Smith }
11049b7cd975SBarry Smith 
11059b7cd975SBarry Smith #undef __FUNCT__
11064a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
1107d763cef2SBarry Smith /*@C
1108f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1109b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1110d763cef2SBarry Smith 
11113f9fe445SBarry Smith    Logically Collective on TS
1112d763cef2SBarry Smith 
1113d763cef2SBarry Smith    Input Parameters:
1114d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1115e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1116e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1117d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1118d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11190298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1120d763cef2SBarry Smith 
1121f7ab8db6SBarry Smith    Calling sequence of f:
1122ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1123d763cef2SBarry Smith 
1124d763cef2SBarry Smith +  t - current timestep
1125d763cef2SBarry Smith .  u - input vector
1126e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1127e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1128d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1129d763cef2SBarry Smith 
11306cd88445SBarry Smith    Notes:
11316cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11326cd88445SBarry Smith 
11336cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1134ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1135d763cef2SBarry Smith 
1136d763cef2SBarry Smith    Level: beginner
1137d763cef2SBarry Smith 
1138d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1139d763cef2SBarry Smith 
1140ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1141d763cef2SBarry Smith 
1142d763cef2SBarry Smith @*/
1143e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1144d763cef2SBarry Smith {
1145277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1146089b2837SJed Brown   SNES           snes;
114724989b8cSPeter Brune   DM             dm;
114824989b8cSPeter Brune   TSIJacobian    ijacobian;
1149277b19d0SLisandro Dalcin 
1150d763cef2SBarry Smith   PetscFunctionBegin;
11510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1152e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1153e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1154e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1155e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1156d763cef2SBarry Smith 
115724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
115824989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1159e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1160e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1161e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1162e1244c69SJed Brown   }
11630298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1164089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11655f659677SPeter Brune   if (!ijacobian) {
1166e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
11670e4ef248SJed Brown   }
1168e5d3d808SBarry Smith   if (Amat) {
1169e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
11700e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1171e5d3d808SBarry Smith     ts->Arhs = Amat;
11720e4ef248SJed Brown   }
1173e5d3d808SBarry Smith   if (Pmat) {
1174e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
11750e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1176e5d3d808SBarry Smith     ts->Brhs = Pmat;
11770e4ef248SJed Brown   }
1178d763cef2SBarry Smith   PetscFunctionReturn(0);
1179d763cef2SBarry Smith }
1180d763cef2SBarry Smith 
1181316643e7SJed Brown 
1182316643e7SJed Brown #undef __FUNCT__
1183316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1184316643e7SJed Brown /*@C
1185b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1186316643e7SJed Brown 
11873f9fe445SBarry Smith    Logically Collective on TS
1188316643e7SJed Brown 
1189316643e7SJed Brown    Input Parameters:
1190316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
11910298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1192316643e7SJed Brown .  f   - the function evaluation routine
11930298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1194316643e7SJed Brown 
1195316643e7SJed Brown    Calling sequence of f:
1196316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1197316643e7SJed Brown 
1198316643e7SJed Brown +  t   - time at step/stage being solved
1199316643e7SJed Brown .  u   - state vector
1200316643e7SJed Brown .  u_t - time derivative of state vector
1201316643e7SJed Brown .  F   - function vector
1202316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1203316643e7SJed Brown 
1204316643e7SJed Brown    Important:
12052bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1206316643e7SJed Brown 
1207316643e7SJed Brown    Level: beginner
1208316643e7SJed Brown 
1209316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1210316643e7SJed Brown 
1211d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1212316643e7SJed Brown @*/
121351699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1214316643e7SJed Brown {
1215089b2837SJed Brown   PetscErrorCode ierr;
1216089b2837SJed Brown   SNES           snes;
121751699248SLisandro Dalcin   Vec            ralloc = NULL;
121824989b8cSPeter Brune   DM             dm;
1219316643e7SJed Brown 
1220316643e7SJed Brown   PetscFunctionBegin;
12210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
122251699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
122324989b8cSPeter Brune 
122424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
122524989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
122624989b8cSPeter Brune 
1227089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
122851699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
122951699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
123051699248SLisandro Dalcin     r  = ralloc;
1231e856ceecSJed Brown   }
123251699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
123351699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1234089b2837SJed Brown   PetscFunctionReturn(0);
1235089b2837SJed Brown }
1236089b2837SJed Brown 
1237089b2837SJed Brown #undef __FUNCT__
1238089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1239089b2837SJed Brown /*@C
1240089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1241089b2837SJed Brown 
1242089b2837SJed Brown    Not Collective
1243089b2837SJed Brown 
1244089b2837SJed Brown    Input Parameter:
1245089b2837SJed Brown .  ts - the TS context
1246089b2837SJed Brown 
1247089b2837SJed Brown    Output Parameter:
12480298fd71SBarry Smith +  r - vector to hold residual (or NULL)
12490298fd71SBarry Smith .  func - the function to compute residual (or NULL)
12500298fd71SBarry Smith -  ctx - the function context (or NULL)
1251089b2837SJed Brown 
1252089b2837SJed Brown    Level: advanced
1253089b2837SJed Brown 
1254089b2837SJed Brown .keywords: TS, nonlinear, get, function
1255089b2837SJed Brown 
1256089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1257089b2837SJed Brown @*/
1258089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1259089b2837SJed Brown {
1260089b2837SJed Brown   PetscErrorCode ierr;
1261089b2837SJed Brown   SNES           snes;
126224989b8cSPeter Brune   DM             dm;
1263089b2837SJed Brown 
1264089b2837SJed Brown   PetscFunctionBegin;
1265089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1266089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12670298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
126824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
126924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1270089b2837SJed Brown   PetscFunctionReturn(0);
1271089b2837SJed Brown }
1272089b2837SJed Brown 
1273089b2837SJed Brown #undef __FUNCT__
1274089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1275089b2837SJed Brown /*@C
1276089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1277089b2837SJed Brown 
1278089b2837SJed Brown    Not Collective
1279089b2837SJed Brown 
1280089b2837SJed Brown    Input Parameter:
1281089b2837SJed Brown .  ts - the TS context
1282089b2837SJed Brown 
1283089b2837SJed Brown    Output Parameter:
12840298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
12850298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
12860298fd71SBarry Smith -  ctx - the function context (or NULL)
1287089b2837SJed Brown 
1288089b2837SJed Brown    Level: advanced
1289089b2837SJed Brown 
1290089b2837SJed Brown .keywords: TS, nonlinear, get, function
1291089b2837SJed Brown 
12922bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1293089b2837SJed Brown @*/
1294089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1295089b2837SJed Brown {
1296089b2837SJed Brown   PetscErrorCode ierr;
1297089b2837SJed Brown   SNES           snes;
129824989b8cSPeter Brune   DM             dm;
1299089b2837SJed Brown 
1300089b2837SJed Brown   PetscFunctionBegin;
1301089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1302089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13030298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
130424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
130524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1306316643e7SJed Brown   PetscFunctionReturn(0);
1307316643e7SJed Brown }
1308316643e7SJed Brown 
1309316643e7SJed Brown #undef __FUNCT__
1310316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1311316643e7SJed Brown /*@C
1312a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1313ae8867d6SBarry Smith         provided with TSSetIFunction().
1314316643e7SJed Brown 
13153f9fe445SBarry Smith    Logically Collective on TS
1316316643e7SJed Brown 
1317316643e7SJed Brown    Input Parameters:
1318316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1319e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1320e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1321316643e7SJed Brown .  f   - the Jacobian evaluation routine
13220298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1323316643e7SJed Brown 
1324316643e7SJed Brown    Calling sequence of f:
1325ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1326316643e7SJed Brown 
1327316643e7SJed Brown +  t    - time at step/stage being solved
13281b4a444bSJed Brown .  U    - state vector
13291b4a444bSJed Brown .  U_t  - time derivative of state vector
1330316643e7SJed Brown .  a    - shift
1331e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1332e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1333316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1334316643e7SJed Brown 
1335316643e7SJed Brown    Notes:
1336e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1337316643e7SJed Brown 
1338895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1339895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1340895c21f2SBarry Smith 
1341a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1342b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1343a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1344a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1345a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1346a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1347a4f0a591SBarry Smith 
13486cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13496cd88445SBarry Smith 
13506cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1351ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1352ca5f011dSBarry Smith 
1353316643e7SJed Brown    Level: beginner
1354316643e7SJed Brown 
1355316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1356316643e7SJed Brown 
1357ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1358316643e7SJed Brown 
1359316643e7SJed Brown @*/
1360e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1361316643e7SJed Brown {
1362316643e7SJed Brown   PetscErrorCode ierr;
1363089b2837SJed Brown   SNES           snes;
136424989b8cSPeter Brune   DM             dm;
1365316643e7SJed Brown 
1366316643e7SJed Brown   PetscFunctionBegin;
13670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1368e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1369e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1370e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1371e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
137224989b8cSPeter Brune 
137324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
137424989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
137524989b8cSPeter Brune 
1376089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1377e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1378316643e7SJed Brown   PetscFunctionReturn(0);
1379316643e7SJed Brown }
1380316643e7SJed Brown 
13814a2ae208SSatish Balay #undef __FUNCT__
1382e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1383e1244c69SJed Brown /*@
1384e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1385e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1386e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1387e1244c69SJed Brown    not been changed by the TS.
1388e1244c69SJed Brown 
1389e1244c69SJed Brown    Logically Collective
1390e1244c69SJed Brown 
1391e1244c69SJed Brown    Input Arguments:
1392e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1393e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1394e1244c69SJed Brown 
1395e1244c69SJed Brown    Level: intermediate
1396e1244c69SJed Brown 
1397e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1398e1244c69SJed Brown @*/
1399e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1400e1244c69SJed Brown {
1401e1244c69SJed Brown   PetscFunctionBegin;
1402e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1403e1244c69SJed Brown   PetscFunctionReturn(0);
1404e1244c69SJed Brown }
1405e1244c69SJed Brown 
1406e1244c69SJed Brown #undef __FUNCT__
140755849f57SBarry Smith #define __FUNCT__ "TSLoad"
140855849f57SBarry Smith /*@C
140955849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
141055849f57SBarry Smith 
141155849f57SBarry Smith   Collective on PetscViewer
141255849f57SBarry Smith 
141355849f57SBarry Smith   Input Parameters:
141455849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
141555849f57SBarry Smith            some related function before a call to TSLoad().
141655849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
141755849f57SBarry Smith 
141855849f57SBarry Smith    Level: intermediate
141955849f57SBarry Smith 
142055849f57SBarry Smith   Notes:
142155849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
142255849f57SBarry Smith 
142355849f57SBarry Smith   Notes for advanced users:
142455849f57SBarry Smith   Most users should not need to know the details of the binary storage
142555849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
142655849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
142755849f57SBarry Smith   format is
142855849f57SBarry Smith .vb
142955849f57SBarry Smith      has not yet been determined
143055849f57SBarry Smith .ve
143155849f57SBarry Smith 
143255849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
143355849f57SBarry Smith @*/
1434f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
143555849f57SBarry Smith {
143655849f57SBarry Smith   PetscErrorCode ierr;
143755849f57SBarry Smith   PetscBool      isbinary;
143855849f57SBarry Smith   PetscInt       classid;
143955849f57SBarry Smith   char           type[256];
14402d53ad75SBarry Smith   DMTS           sdm;
1441ad6bc421SBarry Smith   DM             dm;
144255849f57SBarry Smith 
144355849f57SBarry Smith   PetscFunctionBegin;
1444f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
144555849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
144655849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
144755849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
144855849f57SBarry Smith 
1449060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1450ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1451060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1452f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1453f2c2a1b9SBarry Smith   if (ts->ops->load) {
1454f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1455f2c2a1b9SBarry Smith   }
1456ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1457ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1458ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1459f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1460f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
14612d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14622d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
146355849f57SBarry Smith   PetscFunctionReturn(0);
146455849f57SBarry Smith }
146555849f57SBarry Smith 
14669804daf3SBarry Smith #include <petscdraw.h>
1467e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1468e04113cfSBarry Smith #include <petscviewersaws.h>
1469f05ece33SBarry Smith #endif
147055849f57SBarry Smith #undef __FUNCT__
14714a2ae208SSatish Balay #define __FUNCT__ "TSView"
14727e2c5f70SBarry Smith /*@C
1473d763cef2SBarry Smith     TSView - Prints the TS data structure.
1474d763cef2SBarry Smith 
14754c49b128SBarry Smith     Collective on TS
1476d763cef2SBarry Smith 
1477d763cef2SBarry Smith     Input Parameters:
1478d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1479d763cef2SBarry Smith -   viewer - visualization context
1480d763cef2SBarry Smith 
1481d763cef2SBarry Smith     Options Database Key:
1482d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1483d763cef2SBarry Smith 
1484d763cef2SBarry Smith     Notes:
1485d763cef2SBarry Smith     The available visualization contexts include
1486b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1487b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1488d763cef2SBarry Smith          output where only the first processor opens
1489d763cef2SBarry Smith          the file.  All other processors send their
1490d763cef2SBarry Smith          data to the first processor to print.
1491d763cef2SBarry Smith 
1492d763cef2SBarry Smith     The user can open an alternative visualization context with
1493b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1494d763cef2SBarry Smith 
1495d763cef2SBarry Smith     Level: beginner
1496d763cef2SBarry Smith 
1497d763cef2SBarry Smith .keywords: TS, timestep, view
1498d763cef2SBarry Smith 
1499b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1500d763cef2SBarry Smith @*/
15017087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1502d763cef2SBarry Smith {
1503dfbe8321SBarry Smith   PetscErrorCode ierr;
150419fd82e9SBarry Smith   TSType         type;
15052b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
15062d53ad75SBarry Smith   DMTS           sdm;
1507e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1508536b137fSBarry Smith   PetscBool      issaws;
1509f05ece33SBarry Smith #endif
1510d763cef2SBarry Smith 
1511d763cef2SBarry Smith   PetscFunctionBegin;
15120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15133050cee2SBarry Smith   if (!viewer) {
1514ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
15153050cee2SBarry Smith   }
15160700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1517c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1518fd16b177SBarry Smith 
1519251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1520251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
152155849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
15222b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1523e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1524536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1525f05ece33SBarry Smith #endif
152632077d6dSBarry Smith   if (iascii) {
1527dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
152877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
15297c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1530d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
15315ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1532c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1533d763cef2SBarry Smith     }
15345ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1535193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
15362d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
15372d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1538d52bd9f3SBarry Smith     if (ts->ops->view) {
1539d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1540d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1541d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1542d52bd9f3SBarry Smith     }
15430f5bd95cSBarry Smith   } else if (isstring) {
1544a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1545b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
154655849f57SBarry Smith   } else if (isbinary) {
154755849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
154855849f57SBarry Smith     MPI_Comm    comm;
154955849f57SBarry Smith     PetscMPIInt rank;
155055849f57SBarry Smith     char        type[256];
155155849f57SBarry Smith 
155255849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
155355849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
155455849f57SBarry Smith     if (!rank) {
155555849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
155655849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
155755849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
155855849f57SBarry Smith     }
155955849f57SBarry Smith     if (ts->ops->view) {
156055849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
156155849f57SBarry Smith     }
1562f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1563f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
15642d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
15652d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
15662b0a91c0SBarry Smith   } else if (isdraw) {
15672b0a91c0SBarry Smith     PetscDraw draw;
15682b0a91c0SBarry Smith     char      str[36];
156989fd9fafSBarry Smith     PetscReal x,y,bottom,h;
15702b0a91c0SBarry Smith 
15712b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
15722b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
15732b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
15742b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
157551fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
157689fd9fafSBarry Smith     bottom = y - h;
15772b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
15782b0a91c0SBarry Smith     if (ts->ops->view) {
15792b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
15802b0a91c0SBarry Smith     }
15812b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1582e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1583536b137fSBarry Smith   } else if (issaws) {
1584d45a07a7SBarry Smith     PetscMPIInt rank;
15852657e9d9SBarry Smith     const char  *name;
15862657e9d9SBarry Smith 
15872657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1588d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1589d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1590d45a07a7SBarry Smith       char       dir[1024];
1591d45a07a7SBarry Smith 
1592e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1593a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
15942657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
15952657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
15962657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1597d763cef2SBarry Smith     }
15980acecf5bSBarry Smith     if (ts->ops->view) {
15990acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
16000acecf5bSBarry Smith     }
1601f05ece33SBarry Smith #endif
1602f05ece33SBarry Smith   }
1603f05ece33SBarry Smith 
1604b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1605251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1606b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1607d763cef2SBarry Smith   PetscFunctionReturn(0);
1608d763cef2SBarry Smith }
1609d763cef2SBarry Smith 
1610d763cef2SBarry Smith 
16114a2ae208SSatish Balay #undef __FUNCT__
16124a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1613b07ff414SBarry Smith /*@
1614d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1615d763cef2SBarry Smith    the timesteppers.
1616d763cef2SBarry Smith 
16173f9fe445SBarry Smith    Logically Collective on TS
1618d763cef2SBarry Smith 
1619d763cef2SBarry Smith    Input Parameters:
1620d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1621d763cef2SBarry Smith -  usrP - optional user context
1622d763cef2SBarry Smith 
1623daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
1624daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
1625daf670e6SBarry Smith 
1626d763cef2SBarry Smith    Level: intermediate
1627d763cef2SBarry Smith 
1628d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1629d763cef2SBarry Smith 
1630d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1631d763cef2SBarry Smith @*/
16327087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1633d763cef2SBarry Smith {
1634d763cef2SBarry Smith   PetscFunctionBegin;
16350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1636d763cef2SBarry Smith   ts->user = usrP;
1637d763cef2SBarry Smith   PetscFunctionReturn(0);
1638d763cef2SBarry Smith }
1639d763cef2SBarry Smith 
16404a2ae208SSatish Balay #undef __FUNCT__
16414a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1642b07ff414SBarry Smith /*@
1643d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1644d763cef2SBarry Smith     timestepper.
1645d763cef2SBarry Smith 
1646d763cef2SBarry Smith     Not Collective
1647d763cef2SBarry Smith 
1648d763cef2SBarry Smith     Input Parameter:
1649d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1650d763cef2SBarry Smith 
1651d763cef2SBarry Smith     Output Parameter:
1652d763cef2SBarry Smith .   usrP - user context
1653d763cef2SBarry Smith 
1654daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
1655daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
1656daf670e6SBarry Smith 
1657d763cef2SBarry Smith     Level: intermediate
1658d763cef2SBarry Smith 
1659d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1660d763cef2SBarry Smith 
1661d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1662d763cef2SBarry Smith @*/
1663e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1664d763cef2SBarry Smith {
1665d763cef2SBarry Smith   PetscFunctionBegin;
16660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1667e71120c6SJed Brown   *(void**)usrP = ts->user;
1668d763cef2SBarry Smith   PetscFunctionReturn(0);
1669d763cef2SBarry Smith }
1670d763cef2SBarry Smith 
16714a2ae208SSatish Balay #undef __FUNCT__
16724a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1673d763cef2SBarry Smith /*@
1674b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1675d763cef2SBarry Smith 
1676d763cef2SBarry Smith    Not Collective
1677d763cef2SBarry Smith 
1678d763cef2SBarry Smith    Input Parameter:
1679d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1680d763cef2SBarry Smith 
1681d763cef2SBarry Smith    Output Parameter:
1682b8123daeSJed Brown .  iter - number of steps completed so far
1683d763cef2SBarry Smith 
1684d763cef2SBarry Smith    Level: intermediate
1685d763cef2SBarry Smith 
1686d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
16879be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1688d763cef2SBarry Smith @*/
16897087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1690d763cef2SBarry Smith {
1691d763cef2SBarry Smith   PetscFunctionBegin;
16920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16934482741eSBarry Smith   PetscValidIntPointer(iter,2);
1694d763cef2SBarry Smith   *iter = ts->steps;
1695d763cef2SBarry Smith   PetscFunctionReturn(0);
1696d763cef2SBarry Smith }
1697d763cef2SBarry Smith 
16984a2ae208SSatish Balay #undef __FUNCT__
16994a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1700d763cef2SBarry Smith /*@
1701d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1702d763cef2SBarry Smith    as well as the initial time.
1703d763cef2SBarry Smith 
17043f9fe445SBarry Smith    Logically Collective on TS
1705d763cef2SBarry Smith 
1706d763cef2SBarry Smith    Input Parameters:
1707d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1708d763cef2SBarry Smith .  initial_time - the initial time
1709d763cef2SBarry Smith -  time_step - the size of the timestep
1710d763cef2SBarry Smith 
1711d763cef2SBarry Smith    Level: intermediate
1712d763cef2SBarry Smith 
1713d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1714d763cef2SBarry Smith 
1715d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1716d763cef2SBarry Smith @*/
17177087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1718d763cef2SBarry Smith {
1719e144a568SJed Brown   PetscErrorCode ierr;
1720e144a568SJed Brown 
1721d763cef2SBarry Smith   PetscFunctionBegin;
17220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1723e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1724d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1725d763cef2SBarry Smith   PetscFunctionReturn(0);
1726d763cef2SBarry Smith }
1727d763cef2SBarry Smith 
17284a2ae208SSatish Balay #undef __FUNCT__
17294a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1730d763cef2SBarry Smith /*@
1731d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1732d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1733d763cef2SBarry Smith 
17343f9fe445SBarry Smith    Logically Collective on TS
1735d763cef2SBarry Smith 
1736d763cef2SBarry Smith    Input Parameters:
1737d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1738d763cef2SBarry Smith -  time_step - the size of the timestep
1739d763cef2SBarry Smith 
1740d763cef2SBarry Smith    Level: intermediate
1741d763cef2SBarry Smith 
1742d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1743d763cef2SBarry Smith 
1744d763cef2SBarry Smith .keywords: TS, set, timestep
1745d763cef2SBarry Smith @*/
17467087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1747d763cef2SBarry Smith {
1748d763cef2SBarry Smith   PetscFunctionBegin;
17490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1750c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1751d763cef2SBarry Smith   ts->time_step      = time_step;
175231748224SBarry Smith   ts->time_step_orig = time_step;
1753d763cef2SBarry Smith   PetscFunctionReturn(0);
1754d763cef2SBarry Smith }
1755d763cef2SBarry Smith 
17564a2ae208SSatish Balay #undef __FUNCT__
1757a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1758a43b19c4SJed Brown /*@
175949354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
176049354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
176149354f04SShri Abhyankar      or just return at the final time TS computed.
1762a43b19c4SJed Brown 
1763a43b19c4SJed Brown   Logically Collective on TS
1764a43b19c4SJed Brown 
1765a43b19c4SJed Brown    Input Parameter:
1766a43b19c4SJed Brown +   ts - the time-step context
176749354f04SShri Abhyankar -   eftopt - exact final time option
1768a43b19c4SJed Brown 
1769feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
1770feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
1771feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
1772feed9e9dSBarry Smith 
1773feed9e9dSBarry Smith    Options Database:
1774feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
1775feed9e9dSBarry Smith 
1776ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
1777ee346746SBarry Smith     then the final time you selected.
1778ee346746SBarry Smith 
1779a43b19c4SJed Brown    Level: beginner
1780a43b19c4SJed Brown 
1781a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1782a43b19c4SJed Brown @*/
178349354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1784a43b19c4SJed Brown {
1785a43b19c4SJed Brown   PetscFunctionBegin;
1786a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
178749354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
178849354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1789a43b19c4SJed Brown   PetscFunctionReturn(0);
1790a43b19c4SJed Brown }
1791a43b19c4SJed Brown 
1792a43b19c4SJed Brown #undef __FUNCT__
17934a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1794d763cef2SBarry Smith /*@
1795d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1796d763cef2SBarry Smith 
1797d763cef2SBarry Smith    Not Collective
1798d763cef2SBarry Smith 
1799d763cef2SBarry Smith    Input Parameter:
1800d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1801d763cef2SBarry Smith 
1802d763cef2SBarry Smith    Output Parameter:
1803d763cef2SBarry Smith .  dt - the current timestep size
1804d763cef2SBarry Smith 
1805d763cef2SBarry Smith    Level: intermediate
1806d763cef2SBarry Smith 
1807d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1808d763cef2SBarry Smith 
1809d763cef2SBarry Smith .keywords: TS, get, timestep
1810d763cef2SBarry Smith @*/
18117087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1812d763cef2SBarry Smith {
1813d763cef2SBarry Smith   PetscFunctionBegin;
18140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1815f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1816d763cef2SBarry Smith   *dt = ts->time_step;
1817d763cef2SBarry Smith   PetscFunctionReturn(0);
1818d763cef2SBarry Smith }
1819d763cef2SBarry Smith 
18204a2ae208SSatish Balay #undef __FUNCT__
18214a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1822d8e5e3e6SSatish Balay /*@
1823d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1824d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1825d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1826d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1827d763cef2SBarry Smith 
1828d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1829d763cef2SBarry Smith 
1830d763cef2SBarry Smith    Input Parameter:
1831d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1832d763cef2SBarry Smith 
1833d763cef2SBarry Smith    Output Parameter:
1834d763cef2SBarry Smith .  v - the vector containing the solution
1835d763cef2SBarry Smith 
183663e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
183763e21af5SBarry Smith    final time. It returns the solution at the next timestep.
183863e21af5SBarry Smith 
1839d763cef2SBarry Smith    Level: intermediate
1840d763cef2SBarry Smith 
184163e21af5SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime()
1842d763cef2SBarry Smith 
1843d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1844d763cef2SBarry Smith @*/
18457087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1846d763cef2SBarry Smith {
1847d763cef2SBarry Smith   PetscFunctionBegin;
18480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18494482741eSBarry Smith   PetscValidPointer(v,2);
18508737fe31SLisandro Dalcin   *v = ts->vec_sol;
1851d763cef2SBarry Smith   PetscFunctionReturn(0);
1852d763cef2SBarry Smith }
1853d763cef2SBarry Smith 
1854c235aa8dSHong Zhang #undef __FUNCT__
1855dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients"
1856c235aa8dSHong Zhang /*@
1857dfb21088SHong Zhang    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()
1858c235aa8dSHong Zhang 
1859c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
1860c235aa8dSHong Zhang 
1861c235aa8dSHong Zhang    Input Parameter:
1862c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
1863c235aa8dSHong Zhang 
1864c235aa8dSHong Zhang    Output Parameter:
1865abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
1866abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
1867c235aa8dSHong Zhang 
1868c235aa8dSHong Zhang    Level: intermediate
1869c235aa8dSHong Zhang 
1870c235aa8dSHong Zhang .seealso: TSGetTimeStep()
1871c235aa8dSHong Zhang 
1872c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
1873c235aa8dSHong Zhang @*/
1874dfb21088SHong Zhang PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
1875c235aa8dSHong Zhang {
1876c235aa8dSHong Zhang   PetscFunctionBegin;
1877c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1878abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
1879abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
1880abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
1881c235aa8dSHong Zhang   PetscFunctionReturn(0);
1882c235aa8dSHong Zhang }
1883c235aa8dSHong Zhang 
1884bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
18854a2ae208SSatish Balay #undef __FUNCT__
1886bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1887d8e5e3e6SSatish Balay /*@
1888bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1889d763cef2SBarry Smith 
1890bdad233fSMatthew Knepley   Not collective
1891d763cef2SBarry Smith 
1892bdad233fSMatthew Knepley   Input Parameters:
1893bdad233fSMatthew Knepley + ts   - The TS
1894bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1895d763cef2SBarry Smith .vb
18960910c330SBarry Smith          U_t - A U = 0      (linear)
18970910c330SBarry Smith          U_t - A(t) U = 0   (linear)
18980910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1899d763cef2SBarry Smith .ve
1900d763cef2SBarry Smith 
1901d763cef2SBarry Smith    Level: beginner
1902d763cef2SBarry Smith 
1903bdad233fSMatthew Knepley .keywords: TS, problem type
1904bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1905d763cef2SBarry Smith @*/
19067087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1907a7cc72afSBarry Smith {
19089e2a6581SJed Brown   PetscErrorCode ierr;
19099e2a6581SJed Brown 
1910d763cef2SBarry Smith   PetscFunctionBegin;
19110700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1912bdad233fSMatthew Knepley   ts->problem_type = type;
19139e2a6581SJed Brown   if (type == TS_LINEAR) {
19149e2a6581SJed Brown     SNES snes;
19159e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
19169e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
19179e2a6581SJed Brown   }
1918d763cef2SBarry Smith   PetscFunctionReturn(0);
1919d763cef2SBarry Smith }
1920d763cef2SBarry Smith 
1921bdad233fSMatthew Knepley #undef __FUNCT__
1922bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1923bdad233fSMatthew Knepley /*@C
1924bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1925bdad233fSMatthew Knepley 
1926bdad233fSMatthew Knepley   Not collective
1927bdad233fSMatthew Knepley 
1928bdad233fSMatthew Knepley   Input Parameter:
1929bdad233fSMatthew Knepley . ts   - The TS
1930bdad233fSMatthew Knepley 
1931bdad233fSMatthew Knepley   Output Parameter:
1932bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1933bdad233fSMatthew Knepley .vb
1934089b2837SJed Brown          M U_t = A U
1935089b2837SJed Brown          M(t) U_t = A(t) U
1936b5abc632SBarry Smith          F(t,U,U_t)
1937bdad233fSMatthew Knepley .ve
1938bdad233fSMatthew Knepley 
1939bdad233fSMatthew Knepley    Level: beginner
1940bdad233fSMatthew Knepley 
1941bdad233fSMatthew Knepley .keywords: TS, problem type
1942bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1943bdad233fSMatthew Knepley @*/
19447087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1945a7cc72afSBarry Smith {
1946bdad233fSMatthew Knepley   PetscFunctionBegin;
19470700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
19484482741eSBarry Smith   PetscValidIntPointer(type,2);
1949bdad233fSMatthew Knepley   *type = ts->problem_type;
1950bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1951bdad233fSMatthew Knepley }
1952d763cef2SBarry Smith 
19534a2ae208SSatish Balay #undef __FUNCT__
19544a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1955d763cef2SBarry Smith /*@
1956d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1957d763cef2SBarry Smith    of a timestepper.
1958d763cef2SBarry Smith 
1959d763cef2SBarry Smith    Collective on TS
1960d763cef2SBarry Smith 
1961d763cef2SBarry Smith    Input Parameter:
1962d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1963d763cef2SBarry Smith 
1964d763cef2SBarry Smith    Notes:
1965d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1966d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1967d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1968d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1969d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1970d763cef2SBarry Smith 
1971d763cef2SBarry Smith    Level: advanced
1972d763cef2SBarry Smith 
1973d763cef2SBarry Smith .keywords: TS, timestep, setup
1974d763cef2SBarry Smith 
1975d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1976d763cef2SBarry Smith @*/
19777087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1978d763cef2SBarry Smith {
1979dfbe8321SBarry Smith   PetscErrorCode ierr;
19806c6b9e74SPeter Brune   DM             dm;
19816c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
1982d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
19836c6b9e74SPeter Brune   TSIJacobian    ijac;
19846c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1985d763cef2SBarry Smith 
1986d763cef2SBarry Smith   PetscFunctionBegin;
19870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1988277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1989277b19d0SLisandro Dalcin 
19902c18e0fdSBarry Smith   ts->total_steps = 0;
19917adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
19929596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1993d763cef2SBarry Smith   }
1994277b19d0SLisandro Dalcin 
1995277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1996277b19d0SLisandro Dalcin 
1997e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1998e1244c69SJed Brown     Mat Amat,Pmat;
1999e1244c69SJed Brown     SNES snes;
2000e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2001e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2002e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2003e1244c69SJed Brown      * have displaced the RHS matrix */
2004e1244c69SJed Brown     if (Amat == ts->Arhs) {
2005e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2006e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2007e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2008e1244c69SJed Brown     }
2009e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2010e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2011e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2012e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2013e1244c69SJed Brown     }
2014e1244c69SJed Brown   }
2015277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2016000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2017277b19d0SLisandro Dalcin   }
2018277b19d0SLisandro Dalcin 
2019*a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
20206c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
20216c6b9e74SPeter Brune    */
20226c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
20230298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
20246c6b9e74SPeter Brune   if (!func) {
20256c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
20266c6b9e74SPeter Brune   }
2027*a6772fa2SLisandro 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.
20286c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
20296c6b9e74SPeter Brune    */
20300298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
20310298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
20320298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
20336c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
20346c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
20356c6b9e74SPeter Brune   }
2036277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2037277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2038277b19d0SLisandro Dalcin }
2039277b19d0SLisandro Dalcin 
2040277b19d0SLisandro Dalcin #undef __FUNCT__
2041f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp"
2042f6a906c0SBarry Smith /*@
2043f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
2044f6a906c0SBarry Smith    of an adjoint solver
2045f6a906c0SBarry Smith 
2046f6a906c0SBarry Smith    Collective on TS
2047f6a906c0SBarry Smith 
2048f6a906c0SBarry Smith    Input Parameter:
2049f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
2050f6a906c0SBarry Smith 
2051f6a906c0SBarry Smith    Level: advanced
2052f6a906c0SBarry Smith 
2053f6a906c0SBarry Smith .keywords: TS, timestep, setup
2054f6a906c0SBarry Smith 
2055947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
2056f6a906c0SBarry Smith @*/
2057f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
2058f6a906c0SBarry Smith {
2059f6a906c0SBarry Smith   PetscErrorCode ierr;
2060f6a906c0SBarry Smith 
2061f6a906c0SBarry Smith   PetscFunctionBegin;
2062f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2063f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
2064dfb21088SHong Zhang   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
2065d8151eeaSBarry Smith 
2066947abb85SHong Zhang   if (ts->vec_costintegral) { /* if there is integral in the cost function*/
2067d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2068d8151eeaSBarry Smith     if (ts->vecs_sensip){
2069d8151eeaSBarry Smith       ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2070d8151eeaSBarry Smith     }
2071947abb85SHong Zhang   }
2072d8151eeaSBarry Smith 
207342f2b339SBarry Smith   if (ts->ops->adjointsetup) {
207442f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
2075f6a906c0SBarry Smith   }
2076f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
2077f6a906c0SBarry Smith   PetscFunctionReturn(0);
2078f6a906c0SBarry Smith }
2079f6a906c0SBarry Smith 
2080f6a906c0SBarry Smith #undef __FUNCT__
2081277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
2082277b19d0SLisandro Dalcin /*@
2083277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2084277b19d0SLisandro Dalcin 
2085277b19d0SLisandro Dalcin    Collective on TS
2086277b19d0SLisandro Dalcin 
2087277b19d0SLisandro Dalcin    Input Parameter:
2088277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2089277b19d0SLisandro Dalcin 
2090277b19d0SLisandro Dalcin    Level: beginner
2091277b19d0SLisandro Dalcin 
2092277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2093277b19d0SLisandro Dalcin 
2094277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2095277b19d0SLisandro Dalcin @*/
2096277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2097277b19d0SLisandro Dalcin {
2098277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2099277b19d0SLisandro Dalcin 
2100277b19d0SLisandro Dalcin   PetscFunctionBegin;
2101277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2102b18ea86cSHong Zhang 
2103277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2104277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2105277b19d0SLisandro Dalcin   }
2106277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2107e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2108bbd56ea5SKarl Rupp 
21094e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
21104e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2111214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
21126bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2113e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2114e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
211538637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2116bbd56ea5SKarl Rupp 
2117947abb85SHong Zhang  if (ts->vec_costintegral) {
2118d8151eeaSBarry Smith     ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2119d8151eeaSBarry Smith     if (ts->vecs_drdp){
2120d8151eeaSBarry Smith       ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2121d8151eeaSBarry Smith     }
2122947abb85SHong Zhang   }
2123d8151eeaSBarry Smith   ts->vecs_sensi  = NULL;
2124d8151eeaSBarry Smith   ts->vecs_sensip = NULL;
2125ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
21262c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
212736eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2128277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2129d763cef2SBarry Smith   PetscFunctionReturn(0);
2130d763cef2SBarry Smith }
2131d763cef2SBarry Smith 
21324a2ae208SSatish Balay #undef __FUNCT__
21334a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
2134d8e5e3e6SSatish Balay /*@
2135d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2136d763cef2SBarry Smith    with TSCreate().
2137d763cef2SBarry Smith 
2138d763cef2SBarry Smith    Collective on TS
2139d763cef2SBarry Smith 
2140d763cef2SBarry Smith    Input Parameter:
2141d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2142d763cef2SBarry Smith 
2143d763cef2SBarry Smith    Level: beginner
2144d763cef2SBarry Smith 
2145d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2146d763cef2SBarry Smith 
2147d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2148d763cef2SBarry Smith @*/
21496bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2150d763cef2SBarry Smith {
21516849ba73SBarry Smith   PetscErrorCode ierr;
2152d763cef2SBarry Smith 
2153d763cef2SBarry Smith   PetscFunctionBegin;
21546bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
21556bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
21566bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2157d763cef2SBarry Smith 
21586bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2159277b19d0SLisandro Dalcin 
2160e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2161e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
21626bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
21636d4c513bSLisandro Dalcin 
2164bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2165bc952696SBarry Smith 
216684df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
21676427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
21686427ac75SLisandro Dalcin 
21696bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
21706bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
21716bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
21720dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
21736d4c513bSLisandro Dalcin 
2174a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2175d763cef2SBarry Smith   PetscFunctionReturn(0);
2176d763cef2SBarry Smith }
2177d763cef2SBarry Smith 
21784a2ae208SSatish Balay #undef __FUNCT__
21794a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2180d8e5e3e6SSatish Balay /*@
2181d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2182d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2183d763cef2SBarry Smith 
2184d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2185d763cef2SBarry Smith 
2186d763cef2SBarry Smith    Input Parameter:
2187d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2188d763cef2SBarry Smith 
2189d763cef2SBarry Smith    Output Parameter:
2190d763cef2SBarry Smith .  snes - the nonlinear solver context
2191d763cef2SBarry Smith 
2192d763cef2SBarry Smith    Notes:
2193d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2194d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
219594b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2196d763cef2SBarry Smith 
2197d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
21980298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2199d763cef2SBarry Smith 
2200d763cef2SBarry Smith    Level: beginner
2201d763cef2SBarry Smith 
2202d763cef2SBarry Smith .keywords: timestep, get, SNES
2203d763cef2SBarry Smith @*/
22047087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2205d763cef2SBarry Smith {
2206d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2207d372ba47SLisandro Dalcin 
2208d763cef2SBarry Smith   PetscFunctionBegin;
22090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22104482741eSBarry Smith   PetscValidPointer(snes,2);
2211d372ba47SLisandro Dalcin   if (!ts->snes) {
2212ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
22130298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
22143bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2215d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2216496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
22179e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
22189e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
22199e2a6581SJed Brown     }
2220d372ba47SLisandro Dalcin   }
2221d763cef2SBarry Smith   *snes = ts->snes;
2222d763cef2SBarry Smith   PetscFunctionReturn(0);
2223d763cef2SBarry Smith }
2224d763cef2SBarry Smith 
22254a2ae208SSatish Balay #undef __FUNCT__
2226deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2227deb2cd25SJed Brown /*@
2228deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2229deb2cd25SJed Brown 
2230deb2cd25SJed Brown    Collective
2231deb2cd25SJed Brown 
2232deb2cd25SJed Brown    Input Parameter:
2233deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2234deb2cd25SJed Brown -  snes - the nonlinear solver context
2235deb2cd25SJed Brown 
2236deb2cd25SJed Brown    Notes:
2237deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2238deb2cd25SJed Brown 
2239deb2cd25SJed Brown    Level: developer
2240deb2cd25SJed Brown 
2241deb2cd25SJed Brown .keywords: timestep, set, SNES
2242deb2cd25SJed Brown @*/
2243deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2244deb2cd25SJed Brown {
2245deb2cd25SJed Brown   PetscErrorCode ierr;
2246d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2247deb2cd25SJed Brown 
2248deb2cd25SJed Brown   PetscFunctionBegin;
2249deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2250deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2251deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2252deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2253bbd56ea5SKarl Rupp 
2254deb2cd25SJed Brown   ts->snes = snes;
2255bbd56ea5SKarl Rupp 
22560298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
22570298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2258740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
22590298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2260740132f1SEmil Constantinescu   }
2261deb2cd25SJed Brown   PetscFunctionReturn(0);
2262deb2cd25SJed Brown }
2263deb2cd25SJed Brown 
2264deb2cd25SJed Brown #undef __FUNCT__
226594b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2266d8e5e3e6SSatish Balay /*@
226794b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2268d763cef2SBarry Smith    a TS (timestepper) context.
2269d763cef2SBarry Smith 
227094b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2271d763cef2SBarry Smith 
2272d763cef2SBarry Smith    Input Parameter:
2273d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2274d763cef2SBarry Smith 
2275d763cef2SBarry Smith    Output Parameter:
227694b7f48cSBarry Smith .  ksp - the nonlinear solver context
2277d763cef2SBarry Smith 
2278d763cef2SBarry Smith    Notes:
227994b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2280d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2281d763cef2SBarry Smith    KSP and PC contexts as well.
2282d763cef2SBarry Smith 
228394b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
22840298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2285d763cef2SBarry Smith 
2286d763cef2SBarry Smith    Level: beginner
2287d763cef2SBarry Smith 
228894b7f48cSBarry Smith .keywords: timestep, get, KSP
2289d763cef2SBarry Smith @*/
22907087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2291d763cef2SBarry Smith {
2292d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2293089b2837SJed Brown   SNES           snes;
2294d372ba47SLisandro Dalcin 
2295d763cef2SBarry Smith   PetscFunctionBegin;
22960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22974482741eSBarry Smith   PetscValidPointer(ksp,2);
229817186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2299e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2300089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2301089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2302d763cef2SBarry Smith   PetscFunctionReturn(0);
2303d763cef2SBarry Smith }
2304d763cef2SBarry Smith 
2305d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2306d763cef2SBarry Smith 
23074a2ae208SSatish Balay #undef __FUNCT__
2308adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2309adb62b0dSMatthew Knepley /*@
2310adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2311adb62b0dSMatthew Knepley    maximum time for iteration.
2312adb62b0dSMatthew Knepley 
23133f9fe445SBarry Smith    Not Collective
2314adb62b0dSMatthew Knepley 
2315adb62b0dSMatthew Knepley    Input Parameters:
2316adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
23170298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
23180298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2319adb62b0dSMatthew Knepley 
2320adb62b0dSMatthew Knepley    Level: intermediate
2321adb62b0dSMatthew Knepley 
2322adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2323adb62b0dSMatthew Knepley @*/
23247087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2325adb62b0dSMatthew Knepley {
2326adb62b0dSMatthew Knepley   PetscFunctionBegin;
23270700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2328abc0a331SBarry Smith   if (maxsteps) {
23294482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2330adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2331adb62b0dSMatthew Knepley   }
2332abc0a331SBarry Smith   if (maxtime) {
23334482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2334adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2335adb62b0dSMatthew Knepley   }
2336adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2337adb62b0dSMatthew Knepley }
2338adb62b0dSMatthew Knepley 
2339adb62b0dSMatthew Knepley #undef __FUNCT__
23404a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2341d763cef2SBarry Smith /*@
2342d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2343d763cef2SBarry Smith    maximum time for iteration.
2344d763cef2SBarry Smith 
23453f9fe445SBarry Smith    Logically Collective on TS
2346d763cef2SBarry Smith 
2347d763cef2SBarry Smith    Input Parameters:
2348d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2349d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2350d763cef2SBarry Smith -  maxtime - final time to iterate to
2351d763cef2SBarry Smith 
2352d763cef2SBarry Smith    Options Database Keys:
2353d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
23543bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2355d763cef2SBarry Smith 
2356d763cef2SBarry Smith    Notes:
2357d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2358d763cef2SBarry Smith 
2359d763cef2SBarry Smith    Level: intermediate
2360d763cef2SBarry Smith 
2361d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2362a43b19c4SJed Brown 
2363a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2364d763cef2SBarry Smith @*/
23657087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2366d763cef2SBarry Smith {
2367d763cef2SBarry Smith   PetscFunctionBegin;
23680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2369c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2370c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
237139b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
237239b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2373d763cef2SBarry Smith   PetscFunctionReturn(0);
2374d763cef2SBarry Smith }
2375d763cef2SBarry Smith 
23764a2ae208SSatish Balay #undef __FUNCT__
23774a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2378d763cef2SBarry Smith /*@
2379d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2380d763cef2SBarry Smith    for use by the TS routines.
2381d763cef2SBarry Smith 
23823f9fe445SBarry Smith    Logically Collective on TS and Vec
2383d763cef2SBarry Smith 
2384d763cef2SBarry Smith    Input Parameters:
2385d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
23860910c330SBarry Smith -  u - the solution vector
2387d763cef2SBarry Smith 
2388d763cef2SBarry Smith    Level: beginner
2389d763cef2SBarry Smith 
2390d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2391d763cef2SBarry Smith @*/
23920910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2393d763cef2SBarry Smith {
23948737fe31SLisandro Dalcin   PetscErrorCode ierr;
2395496e6a7aSJed Brown   DM             dm;
23968737fe31SLisandro Dalcin 
2397d763cef2SBarry Smith   PetscFunctionBegin;
23980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23990910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
24000910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
24016bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
24020910c330SBarry Smith   ts->vec_sol = u;
2403bbd56ea5SKarl Rupp 
2404496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
24050910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2406d763cef2SBarry Smith   PetscFunctionReturn(0);
2407d763cef2SBarry Smith }
2408d763cef2SBarry Smith 
2409e74ef692SMatthew Knepley #undef __FUNCT__
241073b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps"
241173b18844SBarry Smith /*@
241273b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
241373b18844SBarry Smith 
241473b18844SBarry Smith    Logically Collective on TS
241573b18844SBarry Smith 
241673b18844SBarry Smith    Input Parameters:
241773b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
241873b18844SBarry Smith .  steps - number of steps to use
241973b18844SBarry Smith 
242073b18844SBarry Smith    Level: intermediate
242173b18844SBarry Smith 
242273b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
242373b18844SBarry Smith           so as to integrate back to less than the original timestep
242473b18844SBarry Smith 
242573b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
242673b18844SBarry Smith 
242773b18844SBarry Smith .seealso: TSSetExactFinalTime()
242873b18844SBarry Smith @*/
242973b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
243073b18844SBarry Smith {
243173b18844SBarry Smith   PetscFunctionBegin;
243273b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
243373b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
243473b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
243573b18844SBarry 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");
243673b18844SBarry Smith   ts->adjoint_max_steps = steps;
243773b18844SBarry Smith   PetscFunctionReturn(0);
243873b18844SBarry Smith }
243973b18844SBarry Smith 
244073b18844SBarry Smith #undef __FUNCT__
2441dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients"
2442c235aa8dSHong Zhang /*@
2443dfb21088SHong 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
24440cf82b59SBarry Smith       for use by the TSAdjoint routines.
2445c235aa8dSHong Zhang 
2446c235aa8dSHong Zhang    Logically Collective on TS and Vec
2447c235aa8dSHong Zhang 
2448c235aa8dSHong Zhang    Input Parameters:
2449c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2450abc2977eSBarry 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
2451abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
2452c235aa8dSHong Zhang 
2453c235aa8dSHong Zhang    Level: beginner
2454c235aa8dSHong Zhang 
2455abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
24560cf82b59SBarry Smith 
2457c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2458c235aa8dSHong Zhang @*/
2459dfb21088SHong Zhang PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
2460c235aa8dSHong Zhang {
2461c235aa8dSHong Zhang   PetscFunctionBegin;
2462c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2463abc2977eSBarry Smith   PetscValidPointer(lambda,2);
2464abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
2465abc2977eSBarry Smith   ts->vecs_sensip = mu;
2466dfb21088SHong 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");
2467abc2977eSBarry Smith   ts->numcost  = numcost;
2468ad8e2604SHong Zhang   PetscFunctionReturn(0);
2469ad8e2604SHong Zhang }
2470ad8e2604SHong Zhang 
2471ad8e2604SHong Zhang #undef __FUNCT__
24725bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian"
2473ad8e2604SHong Zhang /*@C
24740cf82b59SBarry 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.
2475ad8e2604SHong Zhang 
2476ad8e2604SHong Zhang   Logically Collective on TS
2477ad8e2604SHong Zhang 
2478ad8e2604SHong Zhang   Input Parameters:
2479ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2480ad8e2604SHong Zhang - func - The function
2481ad8e2604SHong Zhang 
2482ad8e2604SHong Zhang   Calling sequence of func:
24830cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
248405755b9cSHong Zhang +   t - current timestep
24850cf82b59SBarry Smith .   y - input vector (current ODE solution)
248605755b9cSHong Zhang .   A - output matrix
248705755b9cSHong Zhang -   ctx - [optional] user-defined function context
2488ad8e2604SHong Zhang 
2489ad8e2604SHong Zhang   Level: intermediate
2490ad8e2604SHong Zhang 
24910cf82b59SBarry 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
24920cf82b59SBarry Smith 
2493ad8e2604SHong Zhang .keywords: TS, sensitivity
2494ad8e2604SHong Zhang .seealso:
2495ad8e2604SHong Zhang @*/
24965bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2497ad8e2604SHong Zhang {
2498ad8e2604SHong Zhang   PetscErrorCode ierr;
2499ad8e2604SHong Zhang 
2500ad8e2604SHong Zhang   PetscFunctionBegin;
2501ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2502ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
2503ad8e2604SHong Zhang 
2504ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
2505ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
2506ad8e2604SHong Zhang   if(Amat) {
2507ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
2508ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2509ad8e2604SHong Zhang     ts->Jacp = Amat;
2510ad8e2604SHong Zhang   }
2511ad8e2604SHong Zhang   PetscFunctionReturn(0);
2512ad8e2604SHong Zhang }
2513ad8e2604SHong Zhang 
2514ad8e2604SHong Zhang #undef __FUNCT__
25155bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian"
25160cf82b59SBarry Smith /*@C
25175bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
2518ad8e2604SHong Zhang 
2519ad8e2604SHong Zhang   Collective on TS
2520ad8e2604SHong Zhang 
2521ad8e2604SHong Zhang   Input Parameters:
2522ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
2523ad8e2604SHong Zhang 
2524ad8e2604SHong Zhang   Level: developer
2525ad8e2604SHong Zhang 
2526ad8e2604SHong Zhang .keywords: TS, sensitivity
25275bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
2528ad8e2604SHong Zhang @*/
25295bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
2530ad8e2604SHong Zhang {
2531ad8e2604SHong Zhang   PetscErrorCode ierr;
2532ad8e2604SHong Zhang 
2533ad8e2604SHong Zhang   PetscFunctionBegin;
2534ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2535ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
2536ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
2537ad8e2604SHong Zhang 
2538ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
2539ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
2540ad8e2604SHong Zhang   PetscStackPop;
2541c235aa8dSHong Zhang   PetscFunctionReturn(0);
2542c235aa8dSHong Zhang }
2543c235aa8dSHong Zhang 
2544c235aa8dSHong Zhang #undef __FUNCT__
2545dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand"
25466fd0887fSHong Zhang /*@C
2547dfb21088SHong Zhang     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
25486fd0887fSHong Zhang 
25496fd0887fSHong Zhang     Logically Collective on TS
25506fd0887fSHong Zhang 
25516fd0887fSHong Zhang     Input Parameters:
25526fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
2553abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
25540cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
2555b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
2556b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
2557b1cb36f3SHong Zhang .   fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
2558b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
25596fd0887fSHong Zhang 
25600cf82b59SBarry Smith     Calling sequence of rf:
25610cf82b59SBarry Smith $     rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx);
25626fd0887fSHong Zhang 
25636fd0887fSHong Zhang +   t - current timestep
25640cf82b59SBarry Smith .   y - input vector
25650cf82b59SBarry Smith .   f - function result; one vector entry for each cost function
25666fd0887fSHong Zhang -   ctx - [optional] user-defined function context
25676fd0887fSHong Zhang 
2568b612ec54SBarry Smith    Calling sequence of drdyf:
25690cf82b59SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
2570b612ec54SBarry Smith 
2571b612ec54SBarry Smith    Calling sequence of drdpf:
25720cf82b59SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
2573b612ec54SBarry Smith 
2574b612ec54SBarry Smith     Level: intermediate
25756fd0887fSHong Zhang 
2576abc2977eSBarry Smith     Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions
25770cf82b59SBarry Smith 
25786fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
25796fd0887fSHong Zhang 
2580dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
25816fd0887fSHong Zhang @*/
2582dfb21088SHong Zhang PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
2583d8151eeaSBarry Smith                                                           PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
2584b1cb36f3SHong Zhang                                                           PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),
2585b1cb36f3SHong Zhang                                                           PetscBool fwd,void *ctx)
25866fd0887fSHong Zhang {
25876fd0887fSHong Zhang   PetscErrorCode ierr;
25886fd0887fSHong Zhang 
25896fd0887fSHong Zhang   PetscFunctionBegin;
25906fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2591dfb21088SHong 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()");
2592c72ed514SHong Zhang   if (!ts->numcost) ts->numcost=numcost;
25936fd0887fSHong Zhang 
2594b1cb36f3SHong Zhang   ts->costintegralfwd  = fwd; /* Evaluate the cost integral in forward run if fwd is true */
2595abc2977eSBarry Smith   ierr                 = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
25962c39e106SBarry Smith   ierr                 = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
25970cf82b59SBarry Smith   ts->costintegrand    = rf;
259805755b9cSHong Zhang   ts->costintegrandctx = ctx;
2599b612ec54SBarry Smith   ts->drdyfunction     = drdyf;
2600b612ec54SBarry Smith   ts->drdpfunction     = drdpf;
26016fd0887fSHong Zhang   PetscFunctionReturn(0);
26026fd0887fSHong Zhang }
26036fd0887fSHong Zhang 
26046fd0887fSHong Zhang #undef __FUNCT__
2605dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral"
260636eaed60SHong Zhang /*@
2607dfb21088SHong Zhang    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
260836eaed60SHong Zhang    It is valid to call the routine after a backward run.
260936eaed60SHong Zhang 
261036eaed60SHong Zhang    Not Collective
261136eaed60SHong Zhang 
261236eaed60SHong Zhang    Input Parameter:
261336eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
261436eaed60SHong Zhang 
261536eaed60SHong Zhang    Output Parameter:
26162c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
261736eaed60SHong Zhang 
261836eaed60SHong Zhang    Level: intermediate
261936eaed60SHong Zhang 
2620dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
262136eaed60SHong Zhang 
262236eaed60SHong Zhang .keywords: TS, sensitivity analysis
262336eaed60SHong Zhang @*/
2624dfb21088SHong Zhang PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
262536eaed60SHong Zhang {
262636eaed60SHong Zhang   PetscFunctionBegin;
262736eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
262836eaed60SHong Zhang   PetscValidPointer(v,2);
26292c39e106SBarry Smith   *v = ts->vec_costintegral;
263036eaed60SHong Zhang   PetscFunctionReturn(0);
263136eaed60SHong Zhang }
263236eaed60SHong Zhang 
263336eaed60SHong Zhang #undef __FUNCT__
2634d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand"
26356fd0887fSHong Zhang /*@
26362c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
26376fd0887fSHong Zhang 
26386fd0887fSHong Zhang    Input Parameters:
26396fd0887fSHong Zhang +  ts - the TS context
26406fd0887fSHong Zhang .  t - current time
26410cf82b59SBarry Smith -  y - state vector, i.e. current solution
26426fd0887fSHong Zhang 
26436fd0887fSHong Zhang    Output Parameter:
2644abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
26456fd0887fSHong Zhang 
26466fd0887fSHong Zhang    Note:
26476fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
26486fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
26496fd0887fSHong Zhang 
26506fd0887fSHong Zhang    Level: developer
26516fd0887fSHong Zhang 
26526fd0887fSHong Zhang .keywords: TS, compute
26536fd0887fSHong Zhang 
2654dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
26556fd0887fSHong Zhang @*/
26560cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
26576fd0887fSHong Zhang {
26586fd0887fSHong Zhang   PetscErrorCode ierr;
26596fd0887fSHong Zhang 
26606fd0887fSHong Zhang   PetscFunctionBegin;
26616fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
26620cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
26636fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
26646fd0887fSHong Zhang 
26650cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
2666e4680132SHong Zhang   if (ts->costintegrand) {
2667e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
26680cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
26696fd0887fSHong Zhang     PetscStackPop;
26706fd0887fSHong Zhang   } else {
26716fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
26726fd0887fSHong Zhang   }
26736fd0887fSHong Zhang 
26740cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
26756fd0887fSHong Zhang   PetscFunctionReturn(0);
26766fd0887fSHong Zhang }
26776fd0887fSHong Zhang 
26786fd0887fSHong Zhang #undef __FUNCT__
2679d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction"
268005755b9cSHong Zhang /*@
2681d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
268205755b9cSHong Zhang 
268305755b9cSHong Zhang   Collective on TS
268405755b9cSHong Zhang 
268505755b9cSHong Zhang   Input Parameters:
268605755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
268705755b9cSHong Zhang 
268805755b9cSHong Zhang   Notes:
2689d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
269005755b9cSHong Zhang   so most users would not generally call this routine themselves.
269105755b9cSHong Zhang 
269205755b9cSHong Zhang   Level: developer
269305755b9cSHong Zhang 
269405755b9cSHong Zhang .keywords: TS, sensitivity
2695d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
269605755b9cSHong Zhang @*/
26970cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
269805755b9cSHong Zhang {
269905755b9cSHong Zhang   PetscErrorCode ierr;
270005755b9cSHong Zhang 
270105755b9cSHong Zhang   PetscFunctionBegin;
270205755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27030cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
270405755b9cSHong Zhang 
270505755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
27060cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
270705755b9cSHong Zhang   PetscStackPop;
270805755b9cSHong Zhang   PetscFunctionReturn(0);
270905755b9cSHong Zhang }
271005755b9cSHong Zhang 
271105755b9cSHong Zhang #undef __FUNCT__
2712d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction"
271305755b9cSHong Zhang /*@
2714d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
271505755b9cSHong Zhang 
271605755b9cSHong Zhang   Collective on TS
271705755b9cSHong Zhang 
271805755b9cSHong Zhang   Input Parameters:
271905755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
272005755b9cSHong Zhang 
272105755b9cSHong Zhang   Notes:
272205755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
272305755b9cSHong Zhang   so most users would not generally call this routine themselves.
272405755b9cSHong Zhang 
272505755b9cSHong Zhang   Level: developer
272605755b9cSHong Zhang 
272705755b9cSHong Zhang .keywords: TS, sensitivity
2728d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
272905755b9cSHong Zhang @*/
27300cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
273105755b9cSHong Zhang {
273205755b9cSHong Zhang   PetscErrorCode ierr;
273305755b9cSHong Zhang 
273405755b9cSHong Zhang   PetscFunctionBegin;
273505755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27360cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
273705755b9cSHong Zhang 
273805755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
27390cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
274005755b9cSHong Zhang   PetscStackPop;
274105755b9cSHong Zhang   PetscFunctionReturn(0);
274205755b9cSHong Zhang }
274305755b9cSHong Zhang 
274405755b9cSHong Zhang #undef __FUNCT__
2745e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2746ac226902SBarry Smith /*@C
2747000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
27483f2090d5SJed Brown   called once at the beginning of each time step.
2749000e7ae3SMatthew Knepley 
27503f9fe445SBarry Smith   Logically Collective on TS
2751000e7ae3SMatthew Knepley 
2752000e7ae3SMatthew Knepley   Input Parameters:
2753000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2754000e7ae3SMatthew Knepley - func - The function
2755000e7ae3SMatthew Knepley 
2756000e7ae3SMatthew Knepley   Calling sequence of func:
2757000e7ae3SMatthew Knepley . func (TS ts);
2758000e7ae3SMatthew Knepley 
2759000e7ae3SMatthew Knepley   Level: intermediate
2760000e7ae3SMatthew Knepley 
2761b8123daeSJed Brown   Note:
2762b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2763b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2764b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2765b8123daeSJed Brown 
2766000e7ae3SMatthew Knepley .keywords: TS, timestep
27679be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2768000e7ae3SMatthew Knepley @*/
27697087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2770000e7ae3SMatthew Knepley {
2771000e7ae3SMatthew Knepley   PetscFunctionBegin;
27720700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2773ae60f76fSBarry Smith   ts->prestep = func;
2774000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2775000e7ae3SMatthew Knepley }
2776000e7ae3SMatthew Knepley 
2777e74ef692SMatthew Knepley #undef __FUNCT__
27783f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
277909ee8438SJed Brown /*@
27803f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
27813f2090d5SJed Brown 
27823f2090d5SJed Brown   Collective on TS
27833f2090d5SJed Brown 
27843f2090d5SJed Brown   Input Parameters:
27853f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
27863f2090d5SJed Brown 
27873f2090d5SJed Brown   Notes:
27883f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
27893f2090d5SJed Brown   so most users would not generally call this routine themselves.
27903f2090d5SJed Brown 
27913f2090d5SJed Brown   Level: developer
27923f2090d5SJed Brown 
27933f2090d5SJed Brown .keywords: TS, timestep
27949be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
27953f2090d5SJed Brown @*/
27967087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
27973f2090d5SJed Brown {
27983f2090d5SJed Brown   PetscErrorCode ierr;
27993f2090d5SJed Brown 
28003f2090d5SJed Brown   PetscFunctionBegin;
28010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2802ae60f76fSBarry Smith   if (ts->prestep) {
2803ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2804312ce896SJed Brown   }
28053f2090d5SJed Brown   PetscFunctionReturn(0);
28063f2090d5SJed Brown }
28073f2090d5SJed Brown 
28083f2090d5SJed Brown #undef __FUNCT__
2809b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2810b8123daeSJed Brown /*@C
2811b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2812b8123daeSJed Brown   called once at the beginning of each stage.
2813b8123daeSJed Brown 
2814b8123daeSJed Brown   Logically Collective on TS
2815b8123daeSJed Brown 
2816b8123daeSJed Brown   Input Parameters:
2817b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2818b8123daeSJed Brown - func - The function
2819b8123daeSJed Brown 
2820b8123daeSJed Brown   Calling sequence of func:
2821b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2822b8123daeSJed Brown 
2823b8123daeSJed Brown   Level: intermediate
2824b8123daeSJed Brown 
2825b8123daeSJed Brown   Note:
2826b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2827b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2828b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2829b8123daeSJed Brown 
2830b8123daeSJed Brown .keywords: TS, timestep
28319be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2832b8123daeSJed Brown @*/
2833b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2834b8123daeSJed Brown {
2835b8123daeSJed Brown   PetscFunctionBegin;
2836b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2837ae60f76fSBarry Smith   ts->prestage = func;
2838b8123daeSJed Brown   PetscFunctionReturn(0);
2839b8123daeSJed Brown }
2840b8123daeSJed Brown 
2841b8123daeSJed Brown #undef __FUNCT__
28429be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
28439be3e283SDebojyoti Ghosh /*@C
28449be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
28459be3e283SDebojyoti Ghosh   called once at the end of each stage.
28469be3e283SDebojyoti Ghosh 
28479be3e283SDebojyoti Ghosh   Logically Collective on TS
28489be3e283SDebojyoti Ghosh 
28499be3e283SDebojyoti Ghosh   Input Parameters:
28509be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
28519be3e283SDebojyoti Ghosh - func - The function
28529be3e283SDebojyoti Ghosh 
28539be3e283SDebojyoti Ghosh   Calling sequence of func:
28549be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
28559be3e283SDebojyoti Ghosh 
28569be3e283SDebojyoti Ghosh   Level: intermediate
28579be3e283SDebojyoti Ghosh 
28589be3e283SDebojyoti Ghosh   Note:
28599be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
28609be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
28619be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
28629be3e283SDebojyoti Ghosh 
28639be3e283SDebojyoti Ghosh .keywords: TS, timestep
28649be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
28659be3e283SDebojyoti Ghosh @*/
28669be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
28679be3e283SDebojyoti Ghosh {
28689be3e283SDebojyoti Ghosh   PetscFunctionBegin;
28699be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
28709be3e283SDebojyoti Ghosh   ts->poststage = func;
28719be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
28729be3e283SDebojyoti Ghosh }
28739be3e283SDebojyoti Ghosh 
28749be3e283SDebojyoti Ghosh #undef __FUNCT__
2875b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2876b8123daeSJed Brown /*@
2877b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2878b8123daeSJed Brown 
2879b8123daeSJed Brown   Collective on TS
2880b8123daeSJed Brown 
2881b8123daeSJed Brown   Input Parameters:
2882b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
28839be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2884b8123daeSJed Brown 
2885b8123daeSJed Brown   Notes:
2886b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2887b8123daeSJed Brown   most users would not generally call this routine themselves.
2888b8123daeSJed Brown 
2889b8123daeSJed Brown   Level: developer
2890b8123daeSJed Brown 
2891b8123daeSJed Brown .keywords: TS, timestep
28929be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2893b8123daeSJed Brown @*/
2894b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2895b8123daeSJed Brown {
2896b8123daeSJed Brown   PetscErrorCode ierr;
2897b8123daeSJed Brown 
2898b8123daeSJed Brown   PetscFunctionBegin;
2899b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2900ae60f76fSBarry Smith   if (ts->prestage) {
2901ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2902b8123daeSJed Brown   }
2903b8123daeSJed Brown   PetscFunctionReturn(0);
2904b8123daeSJed Brown }
2905b8123daeSJed Brown 
2906b8123daeSJed Brown #undef __FUNCT__
29079be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
29089be3e283SDebojyoti Ghosh /*@
29099be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
29109be3e283SDebojyoti Ghosh 
29119be3e283SDebojyoti Ghosh   Collective on TS
29129be3e283SDebojyoti Ghosh 
29139be3e283SDebojyoti Ghosh   Input Parameters:
29149be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
29159be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
29169be3e283SDebojyoti Ghosh   stageindex  - Stage number
29179be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
29189be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
29199be3e283SDebojyoti Ghosh 
29209be3e283SDebojyoti Ghosh   Notes:
29219be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
29229be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
29239be3e283SDebojyoti Ghosh 
29249be3e283SDebojyoti Ghosh   Level: developer
29259be3e283SDebojyoti Ghosh 
29269be3e283SDebojyoti Ghosh .keywords: TS, timestep
29279be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
29289be3e283SDebojyoti Ghosh @*/
29299be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
29309be3e283SDebojyoti Ghosh {
29319be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
29329be3e283SDebojyoti Ghosh 
29339be3e283SDebojyoti Ghosh   PetscFunctionBegin;
29349be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29354beae5d8SLisandro Dalcin   if (ts->poststage) {
29369be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
29379be3e283SDebojyoti Ghosh   }
29389be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
29399be3e283SDebojyoti Ghosh }
29409be3e283SDebojyoti Ghosh 
29419be3e283SDebojyoti Ghosh #undef __FUNCT__
2942e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2943ac226902SBarry Smith /*@C
2944000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
29453f2090d5SJed Brown   called once at the end of each time step.
2946000e7ae3SMatthew Knepley 
29473f9fe445SBarry Smith   Logically Collective on TS
2948000e7ae3SMatthew Knepley 
2949000e7ae3SMatthew Knepley   Input Parameters:
2950000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2951000e7ae3SMatthew Knepley - func - The function
2952000e7ae3SMatthew Knepley 
2953000e7ae3SMatthew Knepley   Calling sequence of func:
2954b8123daeSJed Brown $ func (TS ts);
2955000e7ae3SMatthew Knepley 
2956000e7ae3SMatthew Knepley   Level: intermediate
2957000e7ae3SMatthew Knepley 
2958000e7ae3SMatthew Knepley .keywords: TS, timestep
2959b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2960000e7ae3SMatthew Knepley @*/
29617087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2962000e7ae3SMatthew Knepley {
2963000e7ae3SMatthew Knepley   PetscFunctionBegin;
29640700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2965ae60f76fSBarry Smith   ts->poststep = func;
2966000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2967000e7ae3SMatthew Knepley }
2968000e7ae3SMatthew Knepley 
2969e74ef692SMatthew Knepley #undef __FUNCT__
29703f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
297109ee8438SJed Brown /*@
29723f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
29733f2090d5SJed Brown 
29743f2090d5SJed Brown   Collective on TS
29753f2090d5SJed Brown 
29763f2090d5SJed Brown   Input Parameters:
29773f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
29783f2090d5SJed Brown 
29793f2090d5SJed Brown   Notes:
29803f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
29813f2090d5SJed Brown   so most users would not generally call this routine themselves.
29823f2090d5SJed Brown 
29833f2090d5SJed Brown   Level: developer
29843f2090d5SJed Brown 
29853f2090d5SJed Brown .keywords: TS, timestep
29863f2090d5SJed Brown @*/
29877087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
29883f2090d5SJed Brown {
29893f2090d5SJed Brown   PetscErrorCode ierr;
29903f2090d5SJed Brown 
29913f2090d5SJed Brown   PetscFunctionBegin;
29920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2993ae60f76fSBarry Smith   if (ts->poststep) {
2994ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
299572ac3e02SJed Brown   }
29963f2090d5SJed Brown   PetscFunctionReturn(0);
29973f2090d5SJed Brown }
29983f2090d5SJed Brown 
2999d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3000d763cef2SBarry Smith 
30014a2ae208SSatish Balay #undef __FUNCT__
3002a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
3003d763cef2SBarry Smith /*@C
3004a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3005d763cef2SBarry Smith    timestep to display the iteration's  progress.
3006d763cef2SBarry Smith 
30073f9fe445SBarry Smith    Logically Collective on TS
3008d763cef2SBarry Smith 
3009d763cef2SBarry Smith    Input Parameters:
3010d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3011e213d8f1SJed Brown .  monitor - monitoring routine
3012329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
30130298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3014b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
30150298fd71SBarry Smith           (may be NULL)
3016d763cef2SBarry Smith 
3017e213d8f1SJed Brown    Calling sequence of monitor:
30180910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3019d763cef2SBarry Smith 
3020d763cef2SBarry Smith +    ts - the TS context
302163e21af5SBarry 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)
30221f06c33eSBarry Smith .    time - current time
30230910c330SBarry Smith .    u - current iterate
3024d763cef2SBarry Smith -    mctx - [optional] monitoring context
3025d763cef2SBarry Smith 
3026d763cef2SBarry Smith    Notes:
3027d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3028d763cef2SBarry Smith    already has been loaded.
3029d763cef2SBarry Smith 
3030025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
3031025f1a04SBarry Smith 
3032d763cef2SBarry Smith    Level: intermediate
3033d763cef2SBarry Smith 
3034d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3035d763cef2SBarry Smith 
3036a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3037d763cef2SBarry Smith @*/
3038c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3039d763cef2SBarry Smith {
3040d763cef2SBarry Smith   PetscFunctionBegin;
30410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
304217186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3043d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
30448704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3045d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3046d763cef2SBarry Smith   PetscFunctionReturn(0);
3047d763cef2SBarry Smith }
3048d763cef2SBarry Smith 
30494a2ae208SSatish Balay #undef __FUNCT__
3050a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
3051d763cef2SBarry Smith /*@C
3052a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3053d763cef2SBarry Smith 
30543f9fe445SBarry Smith    Logically Collective on TS
3055d763cef2SBarry Smith 
3056d763cef2SBarry Smith    Input Parameters:
3057d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3058d763cef2SBarry Smith 
3059d763cef2SBarry Smith    Notes:
3060d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3061d763cef2SBarry Smith 
3062d763cef2SBarry Smith    Level: intermediate
3063d763cef2SBarry Smith 
3064d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3065d763cef2SBarry Smith 
3066a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3067d763cef2SBarry Smith @*/
30687087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3069d763cef2SBarry Smith {
3070d952e501SBarry Smith   PetscErrorCode ierr;
3071d952e501SBarry Smith   PetscInt       i;
3072d952e501SBarry Smith 
3073d763cef2SBarry Smith   PetscFunctionBegin;
30740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3075d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
30768704b422SBarry Smith     if (ts->monitordestroy[i]) {
30778704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3078d952e501SBarry Smith     }
3079d952e501SBarry Smith   }
3080d763cef2SBarry Smith   ts->numbermonitors = 0;
3081d763cef2SBarry Smith   PetscFunctionReturn(0);
3082d763cef2SBarry Smith }
3083d763cef2SBarry Smith 
30844a2ae208SSatish Balay #undef __FUNCT__
3085a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
3086721cd6eeSBarry Smith /*@C
3087721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
30885516499fSSatish Balay 
30895516499fSSatish Balay    Level: intermediate
309041251cbbSSatish Balay 
30915516499fSSatish Balay .keywords: TS, set, monitor
30925516499fSSatish Balay 
309363e21af5SBarry Smith .seealso:  TSMonitorSet()
309441251cbbSSatish Balay @*/
3095721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3096d763cef2SBarry Smith {
3097dfbe8321SBarry Smith   PetscErrorCode ierr;
3098721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
309941aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3100d132466eSBarry Smith 
3101d763cef2SBarry Smith   PetscFunctionBegin;
31024d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
310341aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
310441aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3105721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
310641aca3d6SBarry Smith   if (iascii) {
3107649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
310863e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
310963e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
311063e21af5SBarry Smith     } else {
31118392e04aSShri 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);
311263e21af5SBarry Smith     }
3113649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
311441aca3d6SBarry Smith   } else if (ibinary) {
311541aca3d6SBarry Smith     PetscMPIInt rank;
311641aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
311741aca3d6SBarry Smith     if (!rank) {
311841aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
311941aca3d6SBarry Smith     } else {
312041aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
312141aca3d6SBarry Smith     }
312241aca3d6SBarry Smith   }
3123721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3124d763cef2SBarry Smith   PetscFunctionReturn(0);
3125d763cef2SBarry Smith }
3126d763cef2SBarry Smith 
31274a2ae208SSatish Balay #undef __FUNCT__
31289110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet"
31299110b2e7SHong Zhang /*@C
31309110b2e7SHong Zhang    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
31319110b2e7SHong Zhang    timestep to display the iteration's  progress.
31329110b2e7SHong Zhang 
31339110b2e7SHong Zhang    Logically Collective on TS
31349110b2e7SHong Zhang 
31359110b2e7SHong Zhang    Input Parameters:
31369110b2e7SHong Zhang +  ts - the TS context obtained from TSCreate()
31379110b2e7SHong Zhang .  adjointmonitor - monitoring routine
31389110b2e7SHong Zhang .  adjointmctx - [optional] user-defined context for private data for the
31399110b2e7SHong Zhang              monitor routine (use NULL if no context is desired)
31409110b2e7SHong Zhang -  adjointmonitordestroy - [optional] routine that frees monitor context
31419110b2e7SHong Zhang           (may be NULL)
31429110b2e7SHong Zhang 
31439110b2e7SHong Zhang    Calling sequence of monitor:
31449110b2e7SHong Zhang $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)
31459110b2e7SHong Zhang 
31469110b2e7SHong Zhang +    ts - the TS context
31479110b2e7SHong 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
31489110b2e7SHong Zhang                                been interpolated to)
31499110b2e7SHong Zhang .    time - current time
31509110b2e7SHong Zhang .    u - current iterate
31519110b2e7SHong Zhang .    numcost - number of cost functionos
31529110b2e7SHong Zhang .    lambda - sensitivities to initial conditions
31539110b2e7SHong Zhang .    mu - sensitivities to parameters
31549110b2e7SHong Zhang -    adjointmctx - [optional] adjoint monitoring context
31559110b2e7SHong Zhang 
31569110b2e7SHong Zhang    Notes:
31579110b2e7SHong Zhang    This routine adds an additional monitor to the list of monitors that
31589110b2e7SHong Zhang    already has been loaded.
31599110b2e7SHong Zhang 
31609110b2e7SHong Zhang    Fortran notes: Only a single monitor function can be set for each TS object
31619110b2e7SHong Zhang 
31629110b2e7SHong Zhang    Level: intermediate
31639110b2e7SHong Zhang 
31649110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
31659110b2e7SHong Zhang 
31669110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel()
31679110b2e7SHong Zhang @*/
31689110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
31699110b2e7SHong Zhang {
31709110b2e7SHong Zhang   PetscFunctionBegin;
31719110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31729110b2e7SHong Zhang   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
31739110b2e7SHong Zhang   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
31749110b2e7SHong Zhang   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
31759110b2e7SHong Zhang   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
31769110b2e7SHong Zhang   PetscFunctionReturn(0);
31779110b2e7SHong Zhang }
31789110b2e7SHong Zhang 
31799110b2e7SHong Zhang #undef __FUNCT__
31809110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel"
31819110b2e7SHong Zhang /*@C
31829110b2e7SHong Zhang    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.
31839110b2e7SHong Zhang 
31849110b2e7SHong Zhang    Logically Collective on TS
31859110b2e7SHong Zhang 
31869110b2e7SHong Zhang    Input Parameters:
31879110b2e7SHong Zhang .  ts - the TS context obtained from TSCreate()
31889110b2e7SHong Zhang 
31899110b2e7SHong Zhang    Notes:
31909110b2e7SHong Zhang    There is no way to remove a single, specific monitor.
31919110b2e7SHong Zhang 
31929110b2e7SHong Zhang    Level: intermediate
31939110b2e7SHong Zhang 
31949110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
31959110b2e7SHong Zhang 
31969110b2e7SHong Zhang .seealso: TSAdjointMonitorSet()
31979110b2e7SHong Zhang @*/
31989110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorCancel(TS ts)
31999110b2e7SHong Zhang {
32009110b2e7SHong Zhang   PetscErrorCode ierr;
32019110b2e7SHong Zhang   PetscInt       i;
32029110b2e7SHong Zhang 
32039110b2e7SHong Zhang   PetscFunctionBegin;
32049110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32059110b2e7SHong Zhang   for (i=0; i<ts->numberadjointmonitors; i++) {
32069110b2e7SHong Zhang     if (ts->adjointmonitordestroy[i]) {
32079110b2e7SHong Zhang       ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
32089110b2e7SHong Zhang     }
32099110b2e7SHong Zhang   }
32109110b2e7SHong Zhang   ts->numberadjointmonitors = 0;
32119110b2e7SHong Zhang   PetscFunctionReturn(0);
32129110b2e7SHong Zhang }
32139110b2e7SHong Zhang 
32149110b2e7SHong Zhang #undef __FUNCT__
3215110eb670SHong Zhang #define __FUNCT__ "TSAdjointMonitorDefault"
3216721cd6eeSBarry Smith /*@C
3217721cd6eeSBarry Smith    TSAdjointMonitorDefault - the default monitor of adjoint computations
3218110eb670SHong Zhang 
3219110eb670SHong Zhang    Level: intermediate
3220110eb670SHong Zhang 
3221110eb670SHong Zhang .keywords: TS, set, monitor
3222110eb670SHong Zhang 
3223110eb670SHong Zhang .seealso: TSAdjointMonitorSet()
3224110eb670SHong Zhang @*/
3225721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
3226110eb670SHong Zhang {
3227110eb670SHong Zhang   PetscErrorCode ierr;
3228721cd6eeSBarry Smith   PetscViewer    viewer = vf->viewer;
3229110eb670SHong Zhang 
3230110eb670SHong Zhang   PetscFunctionBegin;
3231110eb670SHong Zhang   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3232721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3233110eb670SHong Zhang   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3234110eb670SHong 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);
3235110eb670SHong Zhang   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3236721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3237110eb670SHong Zhang   PetscFunctionReturn(0);
3238110eb670SHong Zhang }
3239110eb670SHong Zhang 
3240110eb670SHong Zhang #undef __FUNCT__
3241cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
3242cd652676SJed Brown /*@
3243cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
3244cd652676SJed Brown 
3245cd652676SJed Brown    Logically Collective on TS
3246cd652676SJed Brown 
3247cd652676SJed Brown    Input Argument:
3248cd652676SJed Brown .  ts - time stepping context
3249cd652676SJed Brown 
3250cd652676SJed Brown    Output Argument:
3251cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
3252cd652676SJed Brown 
3253cd652676SJed Brown    Level: intermediate
3254cd652676SJed Brown 
3255cd652676SJed Brown .keywords: TS, set
3256cd652676SJed Brown 
3257cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
3258cd652676SJed Brown @*/
3259cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
3260cd652676SJed Brown {
3261cd652676SJed Brown   PetscFunctionBegin;
3262cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3263cd652676SJed Brown   ts->retain_stages = flg;
3264cd652676SJed Brown   PetscFunctionReturn(0);
3265cd652676SJed Brown }
3266cd652676SJed Brown 
3267cd652676SJed Brown #undef __FUNCT__
3268cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
3269cd652676SJed Brown /*@
3270cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3271cd652676SJed Brown 
3272cd652676SJed Brown    Collective on TS
3273cd652676SJed Brown 
3274cd652676SJed Brown    Input Argument:
3275cd652676SJed Brown +  ts - time stepping context
3276cd652676SJed Brown -  t - time to interpolate to
3277cd652676SJed Brown 
3278cd652676SJed Brown    Output Argument:
32790910c330SBarry Smith .  U - state at given time
3280cd652676SJed Brown 
3281cd652676SJed Brown    Notes:
3282cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
3283cd652676SJed Brown 
3284cd652676SJed Brown    Level: intermediate
3285cd652676SJed Brown 
3286cd652676SJed Brown    Developer Notes:
3287cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3288cd652676SJed Brown 
3289cd652676SJed Brown .keywords: TS, set
3290cd652676SJed Brown 
3291cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
3292cd652676SJed Brown @*/
32930910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3294cd652676SJed Brown {
3295cd652676SJed Brown   PetscErrorCode ierr;
3296cd652676SJed Brown 
3297cd652676SJed Brown   PetscFunctionBegin;
3298cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3299b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
33006712e2f1SBarry Smith   if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Requested time %g not in last time steps [%g,%g]",t,(double)(ts->ptime-ts->time_step_prev),(double)ts->ptime);
3301ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
33020910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3303cd652676SJed Brown   PetscFunctionReturn(0);
3304cd652676SJed Brown }
3305cd652676SJed Brown 
3306cd652676SJed Brown #undef __FUNCT__
33074a2ae208SSatish Balay #define __FUNCT__ "TSStep"
3308d763cef2SBarry Smith /*@
33096d9e5789SSean Farley    TSStep - Steps one time step
3310d763cef2SBarry Smith 
3311d763cef2SBarry Smith    Collective on TS
3312d763cef2SBarry Smith 
3313d763cef2SBarry Smith    Input Parameter:
3314d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3315d763cef2SBarry Smith 
331627829d71SBarry Smith    Level: developer
3317d763cef2SBarry Smith 
3318b8123daeSJed Brown    Notes:
331927829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
332027829d71SBarry Smith 
3321b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3322b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3323b8123daeSJed Brown 
332425cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
332525cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
332625cb2221SBarry Smith 
3327d763cef2SBarry Smith .keywords: TS, timestep, solve
3328d763cef2SBarry Smith 
33299be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3330d763cef2SBarry Smith @*/
3331193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3332d763cef2SBarry Smith {
33332fb8446cSMatthew G. Knepley   DM               dm;
3334dfbe8321SBarry Smith   PetscErrorCode   ierr;
3335fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3336d763cef2SBarry Smith 
3337d763cef2SBarry Smith   PetscFunctionBegin;
33380700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3339fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3340fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3341fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3342fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3343fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3344fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3345302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3346fffbeea8SBarry Smith 
33472fb8446cSMatthew G. Knepley   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
3348d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
334968bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3350d405a339SMatthew Knepley 
3351*a6772fa2SLisandro 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()");
3352*a6772fa2SLisandro Dalcin 
3353362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
335424655328SShri   ts->ptime_prev = ts->ptime;
3355bbd56ea5SKarl Rupp 
3356e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3357d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3358193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3359d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3360bbd56ea5SKarl Rupp 
336124655328SShri   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3362362cd11cSLisandro Dalcin 
3363362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3364cef5090cSJed Brown     if (ts->errorifstepfailed) {
336508c7845fSBarry 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]);
336608c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3367d2daff3dSHong Zhang     }
336808c7845fSBarry Smith   } else if (!ts->reason) {
336908c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
337008c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
337108c7845fSBarry Smith   }
33722c18e0fdSBarry Smith   ts->total_steps++;
33738392e04aSShri Abhyankar   ts->steprollback = PETSC_FALSE;
337408c7845fSBarry Smith   PetscFunctionReturn(0);
337508c7845fSBarry Smith }
337608c7845fSBarry Smith 
337708c7845fSBarry Smith #undef __FUNCT__
337808c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep"
337908c7845fSBarry Smith /*@
3380b957a604SHong Zhang    TSAdjointStep - Steps one time step backward in the adjoint run
338108c7845fSBarry Smith 
338208c7845fSBarry Smith    Collective on TS
338308c7845fSBarry Smith 
338408c7845fSBarry Smith    Input Parameter:
338508c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
338608c7845fSBarry Smith 
33876a4d4014SLisandro Dalcin    Level: intermediate
33886a4d4014SLisandro Dalcin 
3389b957a604SHong Zhang .keywords: TS, adjoint, step
33906a4d4014SLisandro Dalcin 
3391b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve()
33926a4d4014SLisandro Dalcin @*/
339308c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
33946a4d4014SLisandro Dalcin {
339508c7845fSBarry Smith   DM               dm;
33966a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
33976a4d4014SLisandro Dalcin 
33986a4d4014SLisandro Dalcin   PetscFunctionBegin;
33994a2ae208SSatish Balay   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
340008c7845fSBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
340108c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3402d763cef2SBarry Smith 
3403d763cef2SBarry Smith   ts->reason = TS_CONVERGED_ITERATING;
340408c7845fSBarry Smith   ts->ptime_prev = ts->ptime;
340508c7845fSBarry Smith   ierr = DMSetOutputSequenceNumber(dm,ts->steps,ts->ptime);CHKERRQ(ierr);
3406685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr);
3407d763cef2SBarry Smith 
34088d0ad7a8SHong Zhang   ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
340942f2b339SBarry 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);
341042f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
34118d0ad7a8SHong Zhang   ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
3412d763cef2SBarry Smith 
3413cef5090cSJed Brown   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3414cef5090cSJed Brown   ierr = DMSetOutputSequenceNumber(dm,ts->steps,ts->ptime);CHKERRQ(ierr);
3415362cd11cSLisandro Dalcin 
3416362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3417cef5090cSJed Brown     if (ts->errorifstepfailed) {
34186c4ed002SBarry 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]);
34196c4ed002SBarry 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]);
34206c4ed002SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3421cef5090cSJed Brown     }
3422362cd11cSLisandro Dalcin   } else if (!ts->reason) {
342373b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
3424362cd11cSLisandro Dalcin   }
34252c18e0fdSBarry Smith   ts->total_steps--;
3426d763cef2SBarry Smith   PetscFunctionReturn(0);
3427d763cef2SBarry Smith }
3428d763cef2SBarry Smith 
3429d763cef2SBarry Smith #undef __FUNCT__
34307cbde773SLisandro Dalcin #define __FUNCT__ "TSEvaluateWLTE"
34317cbde773SLisandro Dalcin /*@
34327cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
34337cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
34347cbde773SLisandro Dalcin 
34357cbde773SLisandro Dalcin    Collective on TS
34367cbde773SLisandro Dalcin 
34377cbde773SLisandro Dalcin    Input Arguments:
34387cbde773SLisandro Dalcin +  ts - time stepping context
34397cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
34407cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
34417cbde773SLisandro Dalcin 
34427cbde773SLisandro Dalcin    Output Arguments:
34437cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
34447cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
34457cbde773SLisandro Dalcin 
34467cbde773SLisandro Dalcin    Level: advanced
34477cbde773SLisandro Dalcin 
34487cbde773SLisandro Dalcin    Notes:
34497cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
34507cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
34517cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
34527cbde773SLisandro Dalcin 
34537cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
34547cbde773SLisandro Dalcin @*/
34557cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
34567cbde773SLisandro Dalcin {
34577cbde773SLisandro Dalcin   PetscErrorCode ierr;
34587cbde773SLisandro Dalcin 
34597cbde773SLisandro Dalcin   PetscFunctionBegin;
34607cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34617cbde773SLisandro Dalcin   PetscValidType(ts,1);
34627cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
34637cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
34647cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
34657cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
34667cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
34677cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
34687cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
34697cbde773SLisandro Dalcin   PetscFunctionReturn(0);
34707cbde773SLisandro Dalcin }
34717cbde773SLisandro Dalcin 
34727cbde773SLisandro Dalcin #undef __FUNCT__
347305175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
347405175c85SJed Brown /*@
347505175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
347605175c85SJed Brown 
34771c3436cfSJed Brown    Collective on TS
347805175c85SJed Brown 
347905175c85SJed Brown    Input Arguments:
34801c3436cfSJed Brown +  ts - time stepping context
34811c3436cfSJed Brown .  order - desired order of accuracy
34820298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
348305175c85SJed Brown 
348405175c85SJed Brown    Output Arguments:
34850910c330SBarry Smith .  U - state at the end of the current step
348605175c85SJed Brown 
348705175c85SJed Brown    Level: advanced
348805175c85SJed Brown 
3489108c343cSJed Brown    Notes:
3490108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3491108c343cSJed 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.
3492108c343cSJed Brown 
34931c3436cfSJed Brown .seealso: TSStep(), TSAdapt
349405175c85SJed Brown @*/
34950910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
349605175c85SJed Brown {
349705175c85SJed Brown   PetscErrorCode ierr;
349805175c85SJed Brown 
349905175c85SJed Brown   PetscFunctionBegin;
350005175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
350105175c85SJed Brown   PetscValidType(ts,1);
35020910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3503ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
35040910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
350505175c85SJed Brown   PetscFunctionReturn(0);
350605175c85SJed Brown }
350705175c85SJed Brown 
3508b1cb36f3SHong Zhang #undef __FUNCT__
3509b1cb36f3SHong Zhang #define __FUNCT__ "TSForwardCostIntegral"
3510b1cb36f3SHong Zhang /*@
3511b1cb36f3SHong Zhang  TSForwardCostIntegral - Evaluate the cost integral in the forward run.
3512b1cb36f3SHong Zhang 
3513b1cb36f3SHong Zhang  Collective on TS
3514b1cb36f3SHong Zhang 
3515b1cb36f3SHong Zhang  Input Arguments:
3516b1cb36f3SHong Zhang  .  ts - time stepping context
3517b1cb36f3SHong Zhang 
3518b1cb36f3SHong Zhang  Level: advanced
3519b1cb36f3SHong Zhang 
3520b1cb36f3SHong Zhang  Notes:
3521b1cb36f3SHong Zhang  This function cannot be called until TSStep() has been completed.
3522b1cb36f3SHong Zhang 
3523b1cb36f3SHong Zhang  .seealso: TSSolve(), TSAdjointCostIntegral()
3524b1cb36f3SHong Zhang  @*/
3525b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts)
3526b1cb36f3SHong Zhang {
3527b1cb36f3SHong Zhang     PetscErrorCode ierr;
3528b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3529b1cb36f3SHong 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);
3530b1cb36f3SHong Zhang     ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr);
3531b1cb36f3SHong Zhang     PetscFunctionReturn(0);
3532b1cb36f3SHong Zhang }
3533d67e68b7SBarry Smith 
353405175c85SJed Brown #undef __FUNCT__
3535d763cef2SBarry Smith #define __FUNCT__ "TSSolve"
3536d763cef2SBarry Smith /*@
3537d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
3538d763cef2SBarry Smith 
3539d763cef2SBarry Smith    Collective on TS
3540d763cef2SBarry Smith 
3541d763cef2SBarry Smith    Input Parameter:
3542d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
354363e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
354463e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
35455a3a76d0SJed Brown 
3546d763cef2SBarry Smith    Level: beginner
3547d763cef2SBarry Smith 
35485a3a76d0SJed Brown    Notes:
35495a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
35505a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
35515a3a76d0SJed Brown    stepped over the final time.
35525a3a76d0SJed Brown 
3553d763cef2SBarry Smith .keywords: TS, timestep, solve
3554d763cef2SBarry Smith 
355563e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
3556d763cef2SBarry Smith @*/
3557cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
3558d763cef2SBarry Smith {
3559b06615a5SLisandro Dalcin   Vec               solution;
3560d763cef2SBarry Smith   PetscErrorCode    ierr;
3561d763cef2SBarry Smith 
3562d763cef2SBarry Smith   PetscFunctionBegin;
3563d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3564f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3565feed9e9dSBarry Smith 
356649354f04SShri 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 */
3567b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
35680910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3569b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3570b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3571b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
35725a3a76d0SJed Brown     }
35730910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3574bbd56ea5SKarl Rupp   } else if (u) {
35750910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
35765a3a76d0SJed Brown   }
3577b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
357868bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3579*a6772fa2SLisandro Dalcin 
3580*a6772fa2SLisandro 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()");
3581*a6772fa2SLisandro 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");
3582*a6772fa2SLisandro Dalcin 
3583d763cef2SBarry Smith   /* reset time step and iteration counters */
3584193ac0bcSJed Brown   ts->steps             = 0;
35855ef26d82SJed Brown   ts->ksp_its           = 0;
35865ef26d82SJed Brown   ts->snes_its          = 0;
3587c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
3588c610991cSLisandro Dalcin   ts->reject            = 0;
3589193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
3590193ac0bcSJed Brown 
3591ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3592f05ece33SBarry Smith 
3593193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
3594193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
3595*a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3596cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3597*a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
3598193ac0bcSJed Brown   } else {
3599d763cef2SBarry Smith     /* steps the requested number of timesteps. */
3600db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3601db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3602cdf0de3dSShri Abhyankar     ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
36036427ac75SLisandro Dalcin     ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
36046427ac75SLisandro Dalcin 
3605e1a7a14fSJed Brown     while (!ts->reason) {
3606193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
36079687d888SLisandro Dalcin       if (!ts->steprollback) {
36089687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
36099687d888SLisandro Dalcin       }
3610193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
36119687d888SLisandro Dalcin       if (ts->vec_costintegral && ts->costintegralfwd) {
3612b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
3613b1cb36f3SHong Zhang       }
36146427ac75SLisandro Dalcin       ierr = TSEventHandler(ts);CHKERRQ(ierr);
36158392e04aSShri Abhyankar       if (!ts->steprollback) {
3616cdf0de3dSShri Abhyankar         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
36171eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
3618aeb4809dSShri Abhyankar       }
3619193ac0bcSJed Brown     }
362063e21af5SBarry Smith     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
36216427ac75SLisandro Dalcin 
362249354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
36230910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3624cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3625b06615a5SLisandro Dalcin       solution = u;
362663e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
36270574a7fbSJed Brown     } else {
3628ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3629cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3630b06615a5SLisandro Dalcin       solution = ts->vec_sol;
36310574a7fbSJed Brown     }
3632193ac0bcSJed Brown   }
3633d2daff3dSHong Zhang 
3634ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
363563e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
363656f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3637ef222394SBarry Smith   if (ts->adjoint_solve) {
3638ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
36392b0a91c0SBarry Smith   }
3640d763cef2SBarry Smith   PetscFunctionReturn(0);
3641d763cef2SBarry Smith }
3642d763cef2SBarry Smith 
3643d763cef2SBarry Smith #undef __FUNCT__
3644b1cb36f3SHong Zhang #define __FUNCT__ "TSAdjointCostIntegral"
3645b1cb36f3SHong Zhang /*@
3646b1cb36f3SHong Zhang  TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run.
3647b1cb36f3SHong Zhang 
3648b1cb36f3SHong Zhang  Collective on TS
3649b1cb36f3SHong Zhang 
3650b1cb36f3SHong Zhang  Input Arguments:
3651b1cb36f3SHong Zhang  .  ts - time stepping context
3652b1cb36f3SHong Zhang 
3653b1cb36f3SHong Zhang  Level: advanced
3654b1cb36f3SHong Zhang 
3655b1cb36f3SHong Zhang  Notes:
3656b1cb36f3SHong Zhang  This function cannot be called until TSAdjointStep() has been completed.
3657b1cb36f3SHong Zhang 
3658b1cb36f3SHong Zhang  .seealso: TSAdjointSolve(), TSAdjointStep
3659b1cb36f3SHong Zhang  @*/
3660b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts)
3661b1cb36f3SHong Zhang {
3662b1cb36f3SHong Zhang     PetscErrorCode ierr;
3663b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3664b1cb36f3SHong 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);
3665b1cb36f3SHong Zhang     ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr);
3666b1cb36f3SHong Zhang     PetscFunctionReturn(0);
3667b1cb36f3SHong Zhang }
3668b1cb36f3SHong Zhang 
3669b1cb36f3SHong Zhang #undef __FUNCT__
3670c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve"
3671c88f2d44SBarry Smith /*@
3672c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
3673c88f2d44SBarry Smith 
3674c88f2d44SBarry Smith    Collective on TS
3675c88f2d44SBarry Smith 
3676c88f2d44SBarry Smith    Input Parameter:
36772c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
3678c88f2d44SBarry Smith 
3679e87fd094SBarry Smith    Options Database:
3680e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions
3681e87fd094SBarry Smith 
3682c88f2d44SBarry Smith    Level: intermediate
3683c88f2d44SBarry Smith 
3684c88f2d44SBarry Smith    Notes:
3685c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
3686c88f2d44SBarry Smith 
368773b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
368873b18844SBarry Smith 
3689c88f2d44SBarry Smith .keywords: TS, timestep, solve
3690c88f2d44SBarry Smith 
3691b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
3692c88f2d44SBarry Smith @*/
36932c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
3694c88f2d44SBarry Smith {
3695c88f2d44SBarry Smith   PetscErrorCode    ierr;
3696c88f2d44SBarry Smith 
3697c88f2d44SBarry Smith   PetscFunctionBegin;
3698c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3699c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
370068bece0bSHong Zhang 
3701c88f2d44SBarry Smith   /* reset time step and iteration counters */
3702c88f2d44SBarry Smith   ts->steps             = 0;
3703c88f2d44SBarry Smith   ts->ksp_its           = 0;
3704c88f2d44SBarry Smith   ts->snes_its          = 0;
3705c88f2d44SBarry Smith   ts->num_snes_failures = 0;
3706c88f2d44SBarry Smith   ts->reject            = 0;
3707c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
3708c88f2d44SBarry Smith 
370973b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
3710c88f2d44SBarry Smith 
371173b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3712c88f2d44SBarry Smith   while (!ts->reason) {
371355c52731SHong Zhang     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
37149110b2e7SHong Zhang     ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
37156427ac75SLisandro Dalcin     ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr);
3716616946c1SHong Zhang     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
3717b1cb36f3SHong Zhang     if (ts->vec_costintegral && !ts->costintegralfwd) {
3718b1cb36f3SHong Zhang       ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr);
3719b1cb36f3SHong Zhang     }
3720c88f2d44SBarry Smith   }
372126656371SHong Zhang   ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
372226656371SHong Zhang   ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
3723c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
3724e210cd0eSHong Zhang   ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr);
3725685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr);
3726c88f2d44SBarry Smith   PetscFunctionReturn(0);
3727c88f2d44SBarry Smith }
3728c88f2d44SBarry Smith 
3729c88f2d44SBarry Smith #undef __FUNCT__
3730d763cef2SBarry Smith #define __FUNCT__ "TSMonitor"
37317db568b7SBarry Smith /*@C
3732228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3733228d79bcSJed Brown 
3734228d79bcSJed Brown    Collective on TS
3735228d79bcSJed Brown 
3736228d79bcSJed Brown    Input Parameters:
3737228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3738228d79bcSJed Brown .  step - step number that has just completed
3739228d79bcSJed Brown .  ptime - model time of the state
37400910c330SBarry Smith -  u - state at the current model time
3741228d79bcSJed Brown 
3742228d79bcSJed Brown    Notes:
37437db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
37447db568b7SBarry Smith    Users would almost never call this routine directly.
3745228d79bcSJed Brown 
374663e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
374763e21af5SBarry Smith 
37487db568b7SBarry Smith    Level: developer
3749228d79bcSJed Brown 
3750228d79bcSJed Brown .keywords: TS, timestep
3751228d79bcSJed Brown @*/
37520910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3753d763cef2SBarry Smith {
3754d6152f81SLisandro Dalcin   DM             dm;
3755a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3756d6152f81SLisandro Dalcin   PetscErrorCode ierr;
3757d763cef2SBarry Smith 
3758d763cef2SBarry Smith   PetscFunctionBegin;
3759b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3760b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
3761d6152f81SLisandro Dalcin 
3762d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
3763d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
3764d6152f81SLisandro Dalcin 
37655edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3766d763cef2SBarry Smith   for (i=0; i<n; i++) {
37670910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3768d763cef2SBarry Smith   }
37695edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3770d763cef2SBarry Smith   PetscFunctionReturn(0);
3771d763cef2SBarry Smith }
3772d763cef2SBarry Smith 
37739110b2e7SHong Zhang #undef __FUNCT__
37749110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor"
37757db568b7SBarry Smith /*@C
37769110b2e7SHong Zhang    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()
37779110b2e7SHong Zhang 
37789110b2e7SHong Zhang    Collective on TS
37799110b2e7SHong Zhang 
37809110b2e7SHong Zhang    Input Parameters:
37819110b2e7SHong Zhang +  ts - time stepping context obtained from TSCreate()
37829110b2e7SHong Zhang .  step - step number that has just completed
37839110b2e7SHong Zhang .  ptime - model time of the state
37849110b2e7SHong Zhang .  u - state at the current model time
37859110b2e7SHong Zhang .  numcost - number of cost functions (dimension of lambda  or mu)
37869110b2e7SHong Zhang .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
37879110b2e7SHong Zhang -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
37889110b2e7SHong Zhang 
37899110b2e7SHong Zhang    Notes:
37907db568b7SBarry Smith    TSAdjointMonitor() is typically used automatically within the time stepping implementations.
37917db568b7SBarry Smith    Users would almost never call this routine directly.
37929110b2e7SHong Zhang 
37937db568b7SBarry Smith    Level: developer
37949110b2e7SHong Zhang 
37959110b2e7SHong Zhang .keywords: TS, timestep
37969110b2e7SHong Zhang @*/
37979110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
37989110b2e7SHong Zhang {
37999110b2e7SHong Zhang   PetscErrorCode ierr;
38009110b2e7SHong Zhang   PetscInt       i,n = ts->numberadjointmonitors;
38019110b2e7SHong Zhang 
38029110b2e7SHong Zhang   PetscFunctionBegin;
38039110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38049110b2e7SHong Zhang   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
38059110b2e7SHong Zhang   ierr = VecLockPush(u);CHKERRQ(ierr);
38069110b2e7SHong Zhang   for (i=0; i<n; i++) {
38079110b2e7SHong Zhang     ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
38089110b2e7SHong Zhang   }
38099110b2e7SHong Zhang   ierr = VecLockPop(u);CHKERRQ(ierr);
38109110b2e7SHong Zhang   PetscFunctionReturn(0);
38119110b2e7SHong Zhang }
38129110b2e7SHong Zhang 
3813d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
38144a2ae208SSatish Balay #undef __FUNCT__
3815a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
3816d763cef2SBarry Smith /*@C
38177db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
3818a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3819d763cef2SBarry Smith 
3820d763cef2SBarry Smith    Collective on TS
3821d763cef2SBarry Smith 
3822d763cef2SBarry Smith    Input Parameters:
3823d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3824d763cef2SBarry Smith .  label - the title to put in the title bar
38257c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3826a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3827a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3828d763cef2SBarry Smith 
3829d763cef2SBarry Smith    Output Parameter:
38300b039ecaSBarry Smith .  ctx - the context
3831d763cef2SBarry Smith 
3832d763cef2SBarry Smith    Options Database Key:
38334f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
38347db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
38357db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
38367db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
38377db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
3838b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
3839d763cef2SBarry Smith 
3840d763cef2SBarry Smith    Notes:
3841a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3842d763cef2SBarry Smith 
38437db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
38447db568b7SBarry Smith 
38457db568b7SBarry 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
38467db568b7SBarry 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
38477db568b7SBarry Smith    as the first argument.
38487db568b7SBarry Smith 
38497db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
38507db568b7SBarry Smith 
38517db568b7SBarry Smith 
3852d763cef2SBarry Smith    Level: intermediate
3853d763cef2SBarry Smith 
38547db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
3855d763cef2SBarry Smith 
38567db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
38577db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
38587db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
38597db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
38607db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
38617c922b88SBarry Smith 
3862d763cef2SBarry Smith @*/
3863a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3864d763cef2SBarry Smith {
38657f52daa2SLisandro Dalcin   PetscDraw      draw;
3866dfbe8321SBarry Smith   PetscErrorCode ierr;
3867d763cef2SBarry Smith 
3868d763cef2SBarry Smith   PetscFunctionBegin;
3869b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
38707f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
38717f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
38727f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
3873287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
38747f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
3875a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3876d763cef2SBarry Smith   PetscFunctionReturn(0);
3877d763cef2SBarry Smith }
3878d763cef2SBarry Smith 
38794a2ae208SSatish Balay #undef __FUNCT__
38804f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
3881b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3882d763cef2SBarry Smith {
38830b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3884c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3885dfbe8321SBarry Smith   PetscErrorCode ierr;
3886d763cef2SBarry Smith 
3887d763cef2SBarry Smith   PetscFunctionBegin;
388863e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
3889b06615a5SLisandro Dalcin   if (!step) {
3890a9f9c1f6SBarry Smith     PetscDrawAxis axis;
3891a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
38926934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr);
3893a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3894a9f9c1f6SBarry Smith   }
3895c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
38960b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3897b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
38980b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
38996934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
39003923b477SBarry Smith   }
3901d763cef2SBarry Smith   PetscFunctionReturn(0);
3902d763cef2SBarry Smith }
3903d763cef2SBarry Smith 
39044a2ae208SSatish Balay #undef __FUNCT__
3905a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
3906d763cef2SBarry Smith /*@C
3907a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3908a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3909d763cef2SBarry Smith 
39100b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3911d763cef2SBarry Smith 
3912d763cef2SBarry Smith    Input Parameter:
39130b039ecaSBarry Smith .  ctx - the monitor context
3914d763cef2SBarry Smith 
3915d763cef2SBarry Smith    Level: intermediate
3916d763cef2SBarry Smith 
3917d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3918d763cef2SBarry Smith 
39194f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3920d763cef2SBarry Smith @*/
3921a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3922d763cef2SBarry Smith {
3923dfbe8321SBarry Smith   PetscErrorCode ierr;
3924d763cef2SBarry Smith 
3925d763cef2SBarry Smith   PetscFunctionBegin;
39267684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
39277684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
39287684fa3eSBarry Smith   }
39290b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
393031152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
3931387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
3932387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
3933387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
39340b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3935d763cef2SBarry Smith   PetscFunctionReturn(0);
3936d763cef2SBarry Smith }
3937d763cef2SBarry Smith 
39384a2ae208SSatish Balay #undef __FUNCT__
39394a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
3940d763cef2SBarry Smith /*@
3941b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3942d763cef2SBarry Smith 
3943d763cef2SBarry Smith    Not Collective
3944d763cef2SBarry Smith 
3945d763cef2SBarry Smith    Input Parameter:
3946d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3947d763cef2SBarry Smith 
3948d763cef2SBarry Smith    Output Parameter:
394963e21af5SBarry Smith .  t  - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime().
3950d763cef2SBarry Smith 
3951d763cef2SBarry Smith    Level: beginner
3952d763cef2SBarry Smith 
3953b8123daeSJed Brown    Note:
3954b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
39559be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
3956b8123daeSJed Brown 
395763e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime()
3958d763cef2SBarry Smith 
3959d763cef2SBarry Smith .keywords: TS, get, time
3960d763cef2SBarry Smith @*/
39617087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
3962d763cef2SBarry Smith {
3963d763cef2SBarry Smith   PetscFunctionBegin;
39640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3965f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
3966d763cef2SBarry Smith   *t = ts->ptime;
3967d763cef2SBarry Smith   PetscFunctionReturn(0);
3968d763cef2SBarry Smith }
3969d763cef2SBarry Smith 
39704a2ae208SSatish Balay #undef __FUNCT__
3971e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
3972e5e524a1SHong Zhang /*@
3973e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
3974e5e524a1SHong Zhang 
3975e5e524a1SHong Zhang    Not Collective
3976e5e524a1SHong Zhang 
3977e5e524a1SHong Zhang    Input Parameter:
3978e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
3979e5e524a1SHong Zhang 
3980e5e524a1SHong Zhang    Output Parameter:
3981e5e524a1SHong Zhang .  t  - the previous time
3982e5e524a1SHong Zhang 
3983e5e524a1SHong Zhang    Level: beginner
3984e5e524a1SHong Zhang 
3985e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3986e5e524a1SHong Zhang 
3987e5e524a1SHong Zhang .keywords: TS, get, time
3988e5e524a1SHong Zhang @*/
3989e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
3990e5e524a1SHong Zhang {
3991e5e524a1SHong Zhang   PetscFunctionBegin;
3992e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3993e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
3994e5e524a1SHong Zhang   *t = ts->ptime_prev;
3995e5e524a1SHong Zhang   PetscFunctionReturn(0);
3996e5e524a1SHong Zhang }
3997e5e524a1SHong Zhang 
3998e5e524a1SHong Zhang #undef __FUNCT__
39996a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
40006a4d4014SLisandro Dalcin /*@
40016a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
40026a4d4014SLisandro Dalcin 
40033f9fe445SBarry Smith    Logically Collective on TS
40046a4d4014SLisandro Dalcin 
40056a4d4014SLisandro Dalcin    Input Parameters:
40066a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
40076a4d4014SLisandro Dalcin -  time - the time
40086a4d4014SLisandro Dalcin 
40096a4d4014SLisandro Dalcin    Level: intermediate
40106a4d4014SLisandro Dalcin 
40116a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
40126a4d4014SLisandro Dalcin 
40136a4d4014SLisandro Dalcin .keywords: TS, set, time
40146a4d4014SLisandro Dalcin @*/
40157087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
40166a4d4014SLisandro Dalcin {
40176a4d4014SLisandro Dalcin   PetscFunctionBegin;
40180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4019c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
40206a4d4014SLisandro Dalcin   ts->ptime = t;
40216a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
40226a4d4014SLisandro Dalcin }
40236a4d4014SLisandro Dalcin 
40246a4d4014SLisandro Dalcin #undef __FUNCT__
40254a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
4026d763cef2SBarry Smith /*@C
4027d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4028d763cef2SBarry Smith    TS options in the database.
4029d763cef2SBarry Smith 
40303f9fe445SBarry Smith    Logically Collective on TS
4031d763cef2SBarry Smith 
4032d763cef2SBarry Smith    Input Parameter:
4033d763cef2SBarry Smith +  ts     - The TS context
4034d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4035d763cef2SBarry Smith 
4036d763cef2SBarry Smith    Notes:
4037d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4038d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4039d763cef2SBarry Smith    hyphen.
4040d763cef2SBarry Smith 
4041d763cef2SBarry Smith    Level: advanced
4042d763cef2SBarry Smith 
4043d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4044d763cef2SBarry Smith 
4045d763cef2SBarry Smith .seealso: TSSetFromOptions()
4046d763cef2SBarry Smith 
4047d763cef2SBarry Smith @*/
40487087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4049d763cef2SBarry Smith {
4050dfbe8321SBarry Smith   PetscErrorCode ierr;
4051089b2837SJed Brown   SNES           snes;
4052d763cef2SBarry Smith 
4053d763cef2SBarry Smith   PetscFunctionBegin;
40540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4055d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4056089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4057089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4058d763cef2SBarry Smith   PetscFunctionReturn(0);
4059d763cef2SBarry Smith }
4060d763cef2SBarry Smith 
4061d763cef2SBarry Smith 
40624a2ae208SSatish Balay #undef __FUNCT__
40634a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
4064d763cef2SBarry Smith /*@C
4065d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4066d763cef2SBarry Smith    TS options in the database.
4067d763cef2SBarry Smith 
40683f9fe445SBarry Smith    Logically Collective on TS
4069d763cef2SBarry Smith 
4070d763cef2SBarry Smith    Input Parameter:
4071d763cef2SBarry Smith +  ts     - The TS context
4072d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4073d763cef2SBarry Smith 
4074d763cef2SBarry Smith    Notes:
4075d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4076d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4077d763cef2SBarry Smith    hyphen.
4078d763cef2SBarry Smith 
4079d763cef2SBarry Smith    Level: advanced
4080d763cef2SBarry Smith 
4081d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4082d763cef2SBarry Smith 
4083d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4084d763cef2SBarry Smith 
4085d763cef2SBarry Smith @*/
40867087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4087d763cef2SBarry Smith {
4088dfbe8321SBarry Smith   PetscErrorCode ierr;
4089089b2837SJed Brown   SNES           snes;
4090d763cef2SBarry Smith 
4091d763cef2SBarry Smith   PetscFunctionBegin;
40920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4093d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4094089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4095089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4096d763cef2SBarry Smith   PetscFunctionReturn(0);
4097d763cef2SBarry Smith }
4098d763cef2SBarry Smith 
40994a2ae208SSatish Balay #undef __FUNCT__
41004a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
4101d763cef2SBarry Smith /*@C
4102d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4103d763cef2SBarry Smith    TS options in the database.
4104d763cef2SBarry Smith 
4105d763cef2SBarry Smith    Not Collective
4106d763cef2SBarry Smith 
4107d763cef2SBarry Smith    Input Parameter:
4108d763cef2SBarry Smith .  ts - The TS context
4109d763cef2SBarry Smith 
4110d763cef2SBarry Smith    Output Parameter:
4111d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4112d763cef2SBarry Smith 
4113d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
4114d763cef2SBarry Smith    sufficient length to hold the prefix.
4115d763cef2SBarry Smith 
4116d763cef2SBarry Smith    Level: intermediate
4117d763cef2SBarry Smith 
4118d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4119d763cef2SBarry Smith 
4120d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4121d763cef2SBarry Smith @*/
41227087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4123d763cef2SBarry Smith {
4124dfbe8321SBarry Smith   PetscErrorCode ierr;
4125d763cef2SBarry Smith 
4126d763cef2SBarry Smith   PetscFunctionBegin;
41270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41284482741eSBarry Smith   PetscValidPointer(prefix,2);
4129d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4130d763cef2SBarry Smith   PetscFunctionReturn(0);
4131d763cef2SBarry Smith }
4132d763cef2SBarry Smith 
41334a2ae208SSatish Balay #undef __FUNCT__
41344a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
4135d763cef2SBarry Smith /*@C
4136d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4137d763cef2SBarry Smith 
4138d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4139d763cef2SBarry Smith 
4140d763cef2SBarry Smith    Input Parameter:
4141d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4142d763cef2SBarry Smith 
4143d763cef2SBarry Smith    Output Parameters:
4144e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4145e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4146e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4147e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4148d763cef2SBarry Smith 
41490298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
4150d763cef2SBarry Smith 
4151d763cef2SBarry Smith    Level: intermediate
4152d763cef2SBarry Smith 
415326d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
4154d763cef2SBarry Smith 
4155d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4156d763cef2SBarry Smith @*/
4157e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4158d763cef2SBarry Smith {
4159089b2837SJed Brown   PetscErrorCode ierr;
4160089b2837SJed Brown   SNES           snes;
416124989b8cSPeter Brune   DM             dm;
4162089b2837SJed Brown 
4163d763cef2SBarry Smith   PetscFunctionBegin;
4164089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4165e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
416624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
416724989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4168d763cef2SBarry Smith   PetscFunctionReturn(0);
4169d763cef2SBarry Smith }
4170d763cef2SBarry Smith 
41711713a123SBarry Smith #undef __FUNCT__
41722eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
41732eca1d9cSJed Brown /*@C
41742eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
41752eca1d9cSJed Brown 
41762eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
41772eca1d9cSJed Brown 
41782eca1d9cSJed Brown    Input Parameter:
41792eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
41802eca1d9cSJed Brown 
41812eca1d9cSJed Brown    Output Parameters:
4182e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4183e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
41842eca1d9cSJed Brown .  f   - The function to compute the matrices
41852eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
41862eca1d9cSJed Brown 
41870298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
41882eca1d9cSJed Brown 
41892eca1d9cSJed Brown    Level: advanced
41902eca1d9cSJed Brown 
41912eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
41922eca1d9cSJed Brown 
41932eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
41942eca1d9cSJed Brown @*/
4195e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
41962eca1d9cSJed Brown {
4197089b2837SJed Brown   PetscErrorCode ierr;
4198089b2837SJed Brown   SNES           snes;
419924989b8cSPeter Brune   DM             dm;
42000910c330SBarry Smith 
42012eca1d9cSJed Brown   PetscFunctionBegin;
4202089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4203f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4204e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
420524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
420624989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
42072eca1d9cSJed Brown   PetscFunctionReturn(0);
42082eca1d9cSJed Brown }
42092eca1d9cSJed Brown 
42106083293cSBarry Smith 
42112eca1d9cSJed Brown #undef __FUNCT__
421283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
42131713a123SBarry Smith /*@C
421483a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
42151713a123SBarry Smith    VecView() for the solution at each timestep
42161713a123SBarry Smith 
42171713a123SBarry Smith    Collective on TS
42181713a123SBarry Smith 
42191713a123SBarry Smith    Input Parameters:
42201713a123SBarry Smith +  ts - the TS context
42211713a123SBarry Smith .  step - current time-step
4222142b95e3SSatish Balay .  ptime - current time
42230298fd71SBarry Smith -  dummy - either a viewer or NULL
42241713a123SBarry Smith 
422599fdda47SBarry Smith    Options Database:
422699fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
422799fdda47SBarry Smith 
4228387f4636SBarry 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
422999fdda47SBarry Smith        will look bad
423099fdda47SBarry Smith 
42311713a123SBarry Smith    Level: intermediate
42321713a123SBarry Smith 
42331713a123SBarry Smith .keywords: TS,  vector, monitor, view
42341713a123SBarry Smith 
4235a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
42361713a123SBarry Smith @*/
42370910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
42381713a123SBarry Smith {
4239dfbe8321SBarry Smith   PetscErrorCode   ierr;
424083a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4241473a3ab2SBarry Smith   PetscDraw        draw;
42421713a123SBarry Smith 
42431713a123SBarry Smith   PetscFunctionBegin;
42446083293cSBarry Smith   if (!step && ictx->showinitial) {
42456083293cSBarry Smith     if (!ictx->initialsolution) {
42460910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
42471713a123SBarry Smith     }
42480910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
42496083293cSBarry Smith   }
4250b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
42510dcf80beSBarry Smith 
42526083293cSBarry Smith   if (ictx->showinitial) {
42536083293cSBarry Smith     PetscReal pause;
42546083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
42556083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
42566083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
42576083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
42586083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
42596083293cSBarry Smith   }
42600910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4261473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
426251fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4263473a3ab2SBarry Smith     char      time[32];
4264473a3ab2SBarry Smith 
4265473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
426685b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4267473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4268473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
426951fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4270473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4271473a3ab2SBarry Smith   }
4272473a3ab2SBarry Smith 
42736083293cSBarry Smith   if (ictx->showinitial) {
42746083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
42756083293cSBarry Smith   }
42761713a123SBarry Smith   PetscFunctionReturn(0);
42771713a123SBarry Smith }
42781713a123SBarry Smith 
42792d5ee99bSBarry Smith #undef __FUNCT__
42809110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi"
42819110b2e7SHong Zhang /*@C
42829110b2e7SHong Zhang    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
42839110b2e7SHong Zhang    VecView() for the sensitivities to initial states at each timestep
42849110b2e7SHong Zhang 
42859110b2e7SHong Zhang    Collective on TS
42869110b2e7SHong Zhang 
42879110b2e7SHong Zhang    Input Parameters:
42889110b2e7SHong Zhang +  ts - the TS context
42899110b2e7SHong Zhang .  step - current time-step
42909110b2e7SHong Zhang .  ptime - current time
42919110b2e7SHong Zhang .  u - current state
42929110b2e7SHong Zhang .  numcost - number of cost functions
42939110b2e7SHong Zhang .  lambda - sensitivities to initial conditions
42949110b2e7SHong Zhang .  mu - sensitivities to parameters
42959110b2e7SHong Zhang -  dummy - either a viewer or NULL
42969110b2e7SHong Zhang 
42979110b2e7SHong Zhang    Level: intermediate
42989110b2e7SHong Zhang 
42999110b2e7SHong Zhang .keywords: TS,  vector, adjoint, monitor, view
43009110b2e7SHong Zhang 
43019110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
43029110b2e7SHong Zhang @*/
43039110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
43049110b2e7SHong Zhang {
43059110b2e7SHong Zhang   PetscErrorCode   ierr;
43069110b2e7SHong Zhang   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
43079110b2e7SHong Zhang   PetscDraw        draw;
4308a0046e2cSSatish Balay   PetscReal        xl,yl,xr,yr,h;
4309a0046e2cSSatish Balay   char             time[32];
43109110b2e7SHong Zhang 
43119110b2e7SHong Zhang   PetscFunctionBegin;
43129110b2e7SHong Zhang   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
43139110b2e7SHong Zhang 
43149110b2e7SHong Zhang   ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr);
43159110b2e7SHong Zhang   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
43169110b2e7SHong Zhang   ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
43179110b2e7SHong Zhang   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
43189110b2e7SHong Zhang   h    = yl + .95*(yr - yl);
43199110b2e7SHong Zhang   ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
43209110b2e7SHong Zhang   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
43219110b2e7SHong Zhang   PetscFunctionReturn(0);
43229110b2e7SHong Zhang }
43239110b2e7SHong Zhang 
43249110b2e7SHong Zhang #undef __FUNCT__
43252d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
43262d5ee99bSBarry Smith /*@C
43272d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
43282d5ee99bSBarry Smith 
43292d5ee99bSBarry Smith    Collective on TS
43302d5ee99bSBarry Smith 
43312d5ee99bSBarry Smith    Input Parameters:
43322d5ee99bSBarry Smith +  ts - the TS context
43332d5ee99bSBarry Smith .  step - current time-step
43342d5ee99bSBarry Smith .  ptime - current time
43352d5ee99bSBarry Smith -  dummy - either a viewer or NULL
43362d5ee99bSBarry Smith 
43372d5ee99bSBarry Smith    Level: intermediate
43382d5ee99bSBarry Smith 
43392d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
43402d5ee99bSBarry Smith 
43412d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
43422d5ee99bSBarry Smith @*/
43432d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
43442d5ee99bSBarry Smith {
43452d5ee99bSBarry Smith   PetscErrorCode    ierr;
43462d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
43472d5ee99bSBarry Smith   PetscDraw         draw;
43486934998bSLisandro Dalcin   PetscDrawAxis     axis;
43492d5ee99bSBarry Smith   PetscInt          n;
43502d5ee99bSBarry Smith   PetscMPIInt       size;
43516934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
43522d5ee99bSBarry Smith   char              time[32];
43532d5ee99bSBarry Smith   const PetscScalar *U;
43542d5ee99bSBarry Smith 
43552d5ee99bSBarry Smith   PetscFunctionBegin;
43566934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
43576934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
43582d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
43596934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
43602d5ee99bSBarry Smith 
43612d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
43626934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
43636934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
43646934998bSLisandro Dalcin   if (!step) {
43656934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
43666934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
43676934998bSLisandro Dalcin   }
43682d5ee99bSBarry Smith 
43692d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
43706934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
43716934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4372a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
43736934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
43742d5ee99bSBarry Smith 
43756934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
43766934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
43772d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
43784b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
437985b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
43802d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
438151fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
43822d5ee99bSBarry Smith   }
43836934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
43842d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
43856934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
43862d5ee99bSBarry Smith   PetscFunctionReturn(0);
43872d5ee99bSBarry Smith }
43882d5ee99bSBarry Smith 
43891713a123SBarry Smith 
43906c699258SBarry Smith #undef __FUNCT__
439183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
43926083293cSBarry Smith /*@C
439383a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
43946083293cSBarry Smith 
43956083293cSBarry Smith    Collective on TS
43966083293cSBarry Smith 
43976083293cSBarry Smith    Input Parameters:
43986083293cSBarry Smith .    ctx - the monitor context
43996083293cSBarry Smith 
44006083293cSBarry Smith    Level: intermediate
44016083293cSBarry Smith 
44026083293cSBarry Smith .keywords: TS,  vector, monitor, view
44036083293cSBarry Smith 
440483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
44056083293cSBarry Smith @*/
440683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
44076083293cSBarry Smith {
44086083293cSBarry Smith   PetscErrorCode ierr;
44096083293cSBarry Smith 
44106083293cSBarry Smith   PetscFunctionBegin;
441183a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
441283a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
441383a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
44146083293cSBarry Smith   PetscFunctionReturn(0);
44156083293cSBarry Smith }
44166083293cSBarry Smith 
44176083293cSBarry Smith #undef __FUNCT__
441883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
44196083293cSBarry Smith /*@C
442083a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
44216083293cSBarry Smith 
44226083293cSBarry Smith    Collective on TS
44236083293cSBarry Smith 
44246083293cSBarry Smith    Input Parameter:
44256083293cSBarry Smith .    ts - time-step context
44266083293cSBarry Smith 
44276083293cSBarry Smith    Output Patameter:
44286083293cSBarry Smith .    ctx - the monitor context
44296083293cSBarry Smith 
443099fdda47SBarry Smith    Options Database:
443199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
443299fdda47SBarry Smith 
44336083293cSBarry Smith    Level: intermediate
44346083293cSBarry Smith 
44356083293cSBarry Smith .keywords: TS,  vector, monitor, view
44366083293cSBarry Smith 
443783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
44386083293cSBarry Smith @*/
443983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
44406083293cSBarry Smith {
44416083293cSBarry Smith   PetscErrorCode   ierr;
44426083293cSBarry Smith 
44436083293cSBarry Smith   PetscFunctionBegin;
4444b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
444583a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4446e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4447bbd56ea5SKarl Rupp 
444883a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4449473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4450c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4451473a3ab2SBarry Smith 
4452473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4453c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
44546083293cSBarry Smith   PetscFunctionReturn(0);
44556083293cSBarry Smith }
44566083293cSBarry Smith 
44576083293cSBarry Smith #undef __FUNCT__
445883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
44593a471f94SBarry Smith /*@C
446083a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
44613a471f94SBarry Smith    VecView() for the error at each timestep
44623a471f94SBarry Smith 
44633a471f94SBarry Smith    Collective on TS
44643a471f94SBarry Smith 
44653a471f94SBarry Smith    Input Parameters:
44663a471f94SBarry Smith +  ts - the TS context
44673a471f94SBarry Smith .  step - current time-step
44683a471f94SBarry Smith .  ptime - current time
44690298fd71SBarry Smith -  dummy - either a viewer or NULL
44703a471f94SBarry Smith 
44713a471f94SBarry Smith    Level: intermediate
44723a471f94SBarry Smith 
44733a471f94SBarry Smith .keywords: TS,  vector, monitor, view
44743a471f94SBarry Smith 
44753a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
44763a471f94SBarry Smith @*/
44770910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
44783a471f94SBarry Smith {
44793a471f94SBarry Smith   PetscErrorCode   ierr;
448083a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
448183a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
44823a471f94SBarry Smith   Vec              work;
44833a471f94SBarry Smith 
44843a471f94SBarry Smith   PetscFunctionBegin;
4485b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
44860910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
44873a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
44880910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
44893a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
44903a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
44913a471f94SBarry Smith   PetscFunctionReturn(0);
44923a471f94SBarry Smith }
44933a471f94SBarry Smith 
4494af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
44953a471f94SBarry Smith #undef __FUNCT__
44966c699258SBarry Smith #define __FUNCT__ "TSSetDM"
44976c699258SBarry Smith /*@
44986c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
44996c699258SBarry Smith 
45003f9fe445SBarry Smith    Logically Collective on TS and DM
45016c699258SBarry Smith 
45026c699258SBarry Smith    Input Parameters:
45036c699258SBarry Smith +  ts - the preconditioner context
45046c699258SBarry Smith -  dm - the dm
45056c699258SBarry Smith 
45066c699258SBarry Smith    Level: intermediate
45076c699258SBarry Smith 
45086c699258SBarry Smith 
45096c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
45106c699258SBarry Smith @*/
45117087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
45126c699258SBarry Smith {
45136c699258SBarry Smith   PetscErrorCode ierr;
4514089b2837SJed Brown   SNES           snes;
4515942e3340SBarry Smith   DMTS           tsdm;
45166c699258SBarry Smith 
45176c699258SBarry Smith   PetscFunctionBegin;
45180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
451970663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4520942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
45212a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4522942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4523942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
452424989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
452524989b8cSPeter Brune         tsdm->originaldm = dm;
452624989b8cSPeter Brune       }
452724989b8cSPeter Brune     }
45286bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
452924989b8cSPeter Brune   }
45306c699258SBarry Smith   ts->dm = dm;
4531bbd56ea5SKarl Rupp 
4532089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4533089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
45346c699258SBarry Smith   PetscFunctionReturn(0);
45356c699258SBarry Smith }
45366c699258SBarry Smith 
45376c699258SBarry Smith #undef __FUNCT__
45386c699258SBarry Smith #define __FUNCT__ "TSGetDM"
45396c699258SBarry Smith /*@
45406c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
45416c699258SBarry Smith 
45423f9fe445SBarry Smith    Not Collective
45436c699258SBarry Smith 
45446c699258SBarry Smith    Input Parameter:
45456c699258SBarry Smith . ts - the preconditioner context
45466c699258SBarry Smith 
45476c699258SBarry Smith    Output Parameter:
45486c699258SBarry Smith .  dm - the dm
45496c699258SBarry Smith 
45506c699258SBarry Smith    Level: intermediate
45516c699258SBarry Smith 
45526c699258SBarry Smith 
45536c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
45546c699258SBarry Smith @*/
45557087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
45566c699258SBarry Smith {
4557496e6a7aSJed Brown   PetscErrorCode ierr;
4558496e6a7aSJed Brown 
45596c699258SBarry Smith   PetscFunctionBegin;
45600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4561496e6a7aSJed Brown   if (!ts->dm) {
4562ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4563496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4564496e6a7aSJed Brown   }
45656c699258SBarry Smith   *dm = ts->dm;
45666c699258SBarry Smith   PetscFunctionReturn(0);
45676c699258SBarry Smith }
45681713a123SBarry Smith 
45690f5c6efeSJed Brown #undef __FUNCT__
45700f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
45710f5c6efeSJed Brown /*@
45720f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
45730f5c6efeSJed Brown 
45743f9fe445SBarry Smith    Logically Collective on SNES
45750f5c6efeSJed Brown 
45760f5c6efeSJed Brown    Input Parameter:
4577d42a1c89SJed Brown + snes - nonlinear solver
45780910c330SBarry Smith . U - the current state at which to evaluate the residual
4579d42a1c89SJed Brown - ctx - user context, must be a TS
45800f5c6efeSJed Brown 
45810f5c6efeSJed Brown    Output Parameter:
45820f5c6efeSJed Brown . F - the nonlinear residual
45830f5c6efeSJed Brown 
45840f5c6efeSJed Brown    Notes:
45850f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
45860f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
45870f5c6efeSJed Brown 
45880f5c6efeSJed Brown    Level: advanced
45890f5c6efeSJed Brown 
45900f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
45910f5c6efeSJed Brown @*/
45920910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
45930f5c6efeSJed Brown {
45940f5c6efeSJed Brown   TS             ts = (TS)ctx;
45950f5c6efeSJed Brown   PetscErrorCode ierr;
45960f5c6efeSJed Brown 
45970f5c6efeSJed Brown   PetscFunctionBegin;
45980f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
45990910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
46000f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
46010f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
46020910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
46030f5c6efeSJed Brown   PetscFunctionReturn(0);
46040f5c6efeSJed Brown }
46050f5c6efeSJed Brown 
46060f5c6efeSJed Brown #undef __FUNCT__
46070f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
46080f5c6efeSJed Brown /*@
46090f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
46100f5c6efeSJed Brown 
46110f5c6efeSJed Brown    Collective on SNES
46120f5c6efeSJed Brown 
46130f5c6efeSJed Brown    Input Parameter:
46140f5c6efeSJed Brown + snes - nonlinear solver
46150910c330SBarry Smith . U - the current state at which to evaluate the residual
46160f5c6efeSJed Brown - ctx - user context, must be a TS
46170f5c6efeSJed Brown 
46180f5c6efeSJed Brown    Output Parameter:
46190f5c6efeSJed Brown + A - the Jacobian
46200f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
46210f5c6efeSJed Brown - flag - indicates any structure change in the matrix
46220f5c6efeSJed Brown 
46230f5c6efeSJed Brown    Notes:
46240f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
46250f5c6efeSJed Brown 
46260f5c6efeSJed Brown    Level: developer
46270f5c6efeSJed Brown 
46280f5c6efeSJed Brown .seealso: SNESSetJacobian()
46290f5c6efeSJed Brown @*/
4630d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
46310f5c6efeSJed Brown {
46320f5c6efeSJed Brown   TS             ts = (TS)ctx;
46330f5c6efeSJed Brown   PetscErrorCode ierr;
46340f5c6efeSJed Brown 
46350f5c6efeSJed Brown   PetscFunctionBegin;
46360f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
46370910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
46380f5c6efeSJed Brown   PetscValidPointer(A,3);
463994ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
46400f5c6efeSJed Brown   PetscValidPointer(B,4);
464194ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
46420f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4643d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
46440f5c6efeSJed Brown   PetscFunctionReturn(0);
46450f5c6efeSJed Brown }
4646325fc9f4SBarry Smith 
46470e4ef248SJed Brown #undef __FUNCT__
46480e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
46490e4ef248SJed Brown /*@C
46509ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
46510e4ef248SJed Brown 
46520e4ef248SJed Brown    Collective on TS
46530e4ef248SJed Brown 
46540e4ef248SJed Brown    Input Arguments:
46550e4ef248SJed Brown +  ts - time stepping context
46560e4ef248SJed Brown .  t - time at which to evaluate
46570910c330SBarry Smith .  U - state at which to evaluate
46580e4ef248SJed Brown -  ctx - context
46590e4ef248SJed Brown 
46600e4ef248SJed Brown    Output Arguments:
46610e4ef248SJed Brown .  F - right hand side
46620e4ef248SJed Brown 
46630e4ef248SJed Brown    Level: intermediate
46640e4ef248SJed Brown 
46650e4ef248SJed Brown    Notes:
46660e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
46670e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
46680e4ef248SJed Brown 
46690e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
46700e4ef248SJed Brown @*/
46710910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
46720e4ef248SJed Brown {
46730e4ef248SJed Brown   PetscErrorCode ierr;
46740e4ef248SJed Brown   Mat            Arhs,Brhs;
46750e4ef248SJed Brown 
46760e4ef248SJed Brown   PetscFunctionBegin;
46770e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4678d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
46790910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
46800e4ef248SJed Brown   PetscFunctionReturn(0);
46810e4ef248SJed Brown }
46820e4ef248SJed Brown 
46830e4ef248SJed Brown #undef __FUNCT__
46840e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
46850e4ef248SJed Brown /*@C
46860e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
46870e4ef248SJed Brown 
46880e4ef248SJed Brown    Collective on TS
46890e4ef248SJed Brown 
46900e4ef248SJed Brown    Input Arguments:
46910e4ef248SJed Brown +  ts - time stepping context
46920e4ef248SJed Brown .  t - time at which to evaluate
46930910c330SBarry Smith .  U - state at which to evaluate
46940e4ef248SJed Brown -  ctx - context
46950e4ef248SJed Brown 
46960e4ef248SJed Brown    Output Arguments:
46970e4ef248SJed Brown +  A - pointer to operator
46980e4ef248SJed Brown .  B - pointer to preconditioning matrix
46990e4ef248SJed Brown -  flg - matrix structure flag
47000e4ef248SJed Brown 
47010e4ef248SJed Brown    Level: intermediate
47020e4ef248SJed Brown 
47030e4ef248SJed Brown    Notes:
47040e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
47050e4ef248SJed Brown 
47060e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
47070e4ef248SJed Brown @*/
4708d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
47090e4ef248SJed Brown {
47100e4ef248SJed Brown   PetscFunctionBegin;
47110e4ef248SJed Brown   PetscFunctionReturn(0);
47120e4ef248SJed Brown }
47130e4ef248SJed Brown 
47140026cea9SSean Farley #undef __FUNCT__
47150026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
47160026cea9SSean Farley /*@C
47170026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
47180026cea9SSean Farley 
47190026cea9SSean Farley    Collective on TS
47200026cea9SSean Farley 
47210026cea9SSean Farley    Input Arguments:
47220026cea9SSean Farley +  ts - time stepping context
47230026cea9SSean Farley .  t - time at which to evaluate
47240910c330SBarry Smith .  U - state at which to evaluate
47250910c330SBarry Smith .  Udot - time derivative of state vector
47260026cea9SSean Farley -  ctx - context
47270026cea9SSean Farley 
47280026cea9SSean Farley    Output Arguments:
47290026cea9SSean Farley .  F - left hand side
47300026cea9SSean Farley 
47310026cea9SSean Farley    Level: intermediate
47320026cea9SSean Farley 
47330026cea9SSean Farley    Notes:
47340910c330SBarry 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
47350026cea9SSean Farley    user is required to write their own TSComputeIFunction.
47360026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
47370026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
47380026cea9SSean Farley 
47399ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
47409ae8fd06SBarry Smith 
47419ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
47420026cea9SSean Farley @*/
47430910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
47440026cea9SSean Farley {
47450026cea9SSean Farley   PetscErrorCode ierr;
47460026cea9SSean Farley   Mat            A,B;
47470026cea9SSean Farley 
47480026cea9SSean Farley   PetscFunctionBegin;
47490298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4750d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
47510910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
47520026cea9SSean Farley   PetscFunctionReturn(0);
47530026cea9SSean Farley }
47540026cea9SSean Farley 
47550026cea9SSean Farley #undef __FUNCT__
47560026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
47570026cea9SSean Farley /*@C
4758b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
47590026cea9SSean Farley 
47600026cea9SSean Farley    Collective on TS
47610026cea9SSean Farley 
47620026cea9SSean Farley    Input Arguments:
47630026cea9SSean Farley +  ts - time stepping context
47640026cea9SSean Farley .  t - time at which to evaluate
47650910c330SBarry Smith .  U - state at which to evaluate
47660910c330SBarry Smith .  Udot - time derivative of state vector
47670026cea9SSean Farley .  shift - shift to apply
47680026cea9SSean Farley -  ctx - context
47690026cea9SSean Farley 
47700026cea9SSean Farley    Output Arguments:
47710026cea9SSean Farley +  A - pointer to operator
47720026cea9SSean Farley .  B - pointer to preconditioning matrix
47730026cea9SSean Farley -  flg - matrix structure flag
47740026cea9SSean Farley 
4775b41af12eSJed Brown    Level: advanced
47760026cea9SSean Farley 
47770026cea9SSean Farley    Notes:
47780026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
47790026cea9SSean Farley 
4780b41af12eSJed Brown    It is only appropriate for problems of the form
4781b41af12eSJed Brown 
4782b41af12eSJed Brown $     M Udot = F(U,t)
4783b41af12eSJed Brown 
4784b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4785b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4786b41af12eSJed Brown   an implicit operator of the form
4787b41af12eSJed Brown 
4788b41af12eSJed Brown $    shift*M + J
4789b41af12eSJed Brown 
4790b41af12eSJed 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
4791b41af12eSJed Brown   a copy of M or reassemble it when requested.
4792b41af12eSJed Brown 
47930026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
47940026cea9SSean Farley @*/
4795d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
47960026cea9SSean Farley {
4797b41af12eSJed Brown   PetscErrorCode ierr;
4798b41af12eSJed Brown 
47990026cea9SSean Farley   PetscFunctionBegin;
480094ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4801b41af12eSJed Brown   ts->ijacobian.shift = shift;
48020026cea9SSean Farley   PetscFunctionReturn(0);
48030026cea9SSean Farley }
4804b41af12eSJed Brown 
4805e817cc15SEmil Constantinescu #undef __FUNCT__
4806e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
4807e817cc15SEmil Constantinescu /*@
4808e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4809e817cc15SEmil Constantinescu 
4810e817cc15SEmil Constantinescu    Not Collective
4811e817cc15SEmil Constantinescu 
4812e817cc15SEmil Constantinescu    Input Parameter:
4813e817cc15SEmil Constantinescu .  ts - the TS context
4814e817cc15SEmil Constantinescu 
4815e817cc15SEmil Constantinescu    Output Parameter:
48164e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4817e817cc15SEmil Constantinescu 
4818e817cc15SEmil Constantinescu    Level: beginner
4819e817cc15SEmil Constantinescu 
4820e817cc15SEmil Constantinescu .keywords: TS, equation type
4821e817cc15SEmil Constantinescu 
4822e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4823e817cc15SEmil Constantinescu @*/
4824e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4825e817cc15SEmil Constantinescu {
4826e817cc15SEmil Constantinescu   PetscFunctionBegin;
4827e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4828e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4829e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4830e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4831e817cc15SEmil Constantinescu }
4832e817cc15SEmil Constantinescu 
4833e817cc15SEmil Constantinescu #undef __FUNCT__
4834e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
4835e817cc15SEmil Constantinescu /*@
4836e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4837e817cc15SEmil Constantinescu 
4838e817cc15SEmil Constantinescu    Not Collective
4839e817cc15SEmil Constantinescu 
4840e817cc15SEmil Constantinescu    Input Parameter:
4841e817cc15SEmil Constantinescu +  ts - the TS context
48421297b224SEmil Constantinescu -  equation_type - see TSEquationType
4843e817cc15SEmil Constantinescu 
4844e817cc15SEmil Constantinescu    Level: advanced
4845e817cc15SEmil Constantinescu 
4846e817cc15SEmil Constantinescu .keywords: TS, equation type
4847e817cc15SEmil Constantinescu 
4848e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4849e817cc15SEmil Constantinescu @*/
4850e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4851e817cc15SEmil Constantinescu {
4852e817cc15SEmil Constantinescu   PetscFunctionBegin;
4853e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4854e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4855e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4856e817cc15SEmil Constantinescu }
48570026cea9SSean Farley 
48584af1b03aSJed Brown #undef __FUNCT__
48594af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
48604af1b03aSJed Brown /*@
48614af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
48624af1b03aSJed Brown 
48634af1b03aSJed Brown    Not Collective
48644af1b03aSJed Brown 
48654af1b03aSJed Brown    Input Parameter:
48664af1b03aSJed Brown .  ts - the TS context
48674af1b03aSJed Brown 
48684af1b03aSJed Brown    Output Parameter:
48694af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
48704af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
48714af1b03aSJed Brown 
4872487e0bb9SJed Brown    Level: beginner
48734af1b03aSJed Brown 
4874cd652676SJed Brown    Notes:
4875cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
48764af1b03aSJed Brown 
48774af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
48784af1b03aSJed Brown 
48794af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
48804af1b03aSJed Brown @*/
48814af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
48824af1b03aSJed Brown {
48834af1b03aSJed Brown   PetscFunctionBegin;
48844af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48854af1b03aSJed Brown   PetscValidPointer(reason,2);
48864af1b03aSJed Brown   *reason = ts->reason;
48874af1b03aSJed Brown   PetscFunctionReturn(0);
48884af1b03aSJed Brown }
48894af1b03aSJed Brown 
4890fb1732b5SBarry Smith #undef __FUNCT__
4891d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
4892d6ad946cSShri Abhyankar /*@
4893d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4894d6ad946cSShri Abhyankar 
4895d6ad946cSShri Abhyankar    Not Collective
4896d6ad946cSShri Abhyankar 
4897d6ad946cSShri Abhyankar    Input Parameter:
4898d6ad946cSShri Abhyankar +  ts - the TS context
4899d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4900d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4901d6ad946cSShri Abhyankar 
4902f5abba47SShri Abhyankar    Level: advanced
4903d6ad946cSShri Abhyankar 
4904d6ad946cSShri Abhyankar    Notes:
4905d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4906d6ad946cSShri Abhyankar 
4907d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4908d6ad946cSShri Abhyankar 
4909d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4910d6ad946cSShri Abhyankar @*/
4911d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4912d6ad946cSShri Abhyankar {
4913d6ad946cSShri Abhyankar   PetscFunctionBegin;
4914d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4915d6ad946cSShri Abhyankar   ts->reason = reason;
4916d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4917d6ad946cSShri Abhyankar }
4918d6ad946cSShri Abhyankar 
4919d6ad946cSShri Abhyankar #undef __FUNCT__
4920cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
4921cc708dedSBarry Smith /*@
4922cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4923cc708dedSBarry Smith 
4924cc708dedSBarry Smith    Not Collective
4925cc708dedSBarry Smith 
4926cc708dedSBarry Smith    Input Parameter:
4927cc708dedSBarry Smith .  ts - the TS context
4928cc708dedSBarry Smith 
4929cc708dedSBarry Smith    Output Parameter:
493063e21af5SBarry Smith .  ftime - the final time. This time corresponds to the final time set with TSSetDuration()
4931cc708dedSBarry Smith 
4932487e0bb9SJed Brown    Level: beginner
4933cc708dedSBarry Smith 
4934cc708dedSBarry Smith    Notes:
4935cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4936cc708dedSBarry Smith 
4937cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4938cc708dedSBarry Smith 
4939cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4940cc708dedSBarry Smith @*/
4941cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4942cc708dedSBarry Smith {
4943cc708dedSBarry Smith   PetscFunctionBegin;
4944cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4945cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4946cc708dedSBarry Smith   *ftime = ts->solvetime;
4947cc708dedSBarry Smith   PetscFunctionReturn(0);
4948cc708dedSBarry Smith }
4949cc708dedSBarry Smith 
4950cc708dedSBarry Smith #undef __FUNCT__
49512c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps"
49522c18e0fdSBarry Smith /*@
49532c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
49542c18e0fdSBarry Smith 
49552c18e0fdSBarry Smith    Not Collective
49562c18e0fdSBarry Smith 
49572c18e0fdSBarry Smith    Input Parameter:
49582c18e0fdSBarry Smith .  ts - the TS context
49592c18e0fdSBarry Smith 
49602c18e0fdSBarry Smith    Output Parameter:
49612c18e0fdSBarry Smith .  steps - the number of steps
49622c18e0fdSBarry Smith 
49632c18e0fdSBarry Smith    Level: beginner
49642c18e0fdSBarry Smith 
49652c18e0fdSBarry Smith    Notes:
49662c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
49672c18e0fdSBarry Smith 
49682c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
49692c18e0fdSBarry Smith 
49702c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
49712c18e0fdSBarry Smith @*/
49722c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
49732c18e0fdSBarry Smith {
49742c18e0fdSBarry Smith   PetscFunctionBegin;
49752c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49762c18e0fdSBarry Smith   PetscValidPointer(steps,2);
49772c18e0fdSBarry Smith   *steps = ts->total_steps;
49782c18e0fdSBarry Smith   PetscFunctionReturn(0);
49792c18e0fdSBarry Smith }
49802c18e0fdSBarry Smith 
49812c18e0fdSBarry Smith #undef __FUNCT__
49825ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
49839f67acb7SJed Brown /*@
49845ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
49859f67acb7SJed Brown    used by the time integrator.
49869f67acb7SJed Brown 
49879f67acb7SJed Brown    Not Collective
49889f67acb7SJed Brown 
49899f67acb7SJed Brown    Input Parameter:
49909f67acb7SJed Brown .  ts - TS context
49919f67acb7SJed Brown 
49929f67acb7SJed Brown    Output Parameter:
49939f67acb7SJed Brown .  nits - number of nonlinear iterations
49949f67acb7SJed Brown 
49959f67acb7SJed Brown    Notes:
49969f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
49979f67acb7SJed Brown 
49989f67acb7SJed Brown    Level: intermediate
49999f67acb7SJed Brown 
50009f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
50019f67acb7SJed Brown 
50025ef26d82SJed Brown .seealso:  TSGetKSPIterations()
50039f67acb7SJed Brown @*/
50045ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
50059f67acb7SJed Brown {
50069f67acb7SJed Brown   PetscFunctionBegin;
50079f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50089f67acb7SJed Brown   PetscValidIntPointer(nits,2);
50095ef26d82SJed Brown   *nits = ts->snes_its;
50109f67acb7SJed Brown   PetscFunctionReturn(0);
50119f67acb7SJed Brown }
50129f67acb7SJed Brown 
50139f67acb7SJed Brown #undef __FUNCT__
50145ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
50159f67acb7SJed Brown /*@
50165ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
50179f67acb7SJed Brown    used by the time integrator.
50189f67acb7SJed Brown 
50199f67acb7SJed Brown    Not Collective
50209f67acb7SJed Brown 
50219f67acb7SJed Brown    Input Parameter:
50229f67acb7SJed Brown .  ts - TS context
50239f67acb7SJed Brown 
50249f67acb7SJed Brown    Output Parameter:
50259f67acb7SJed Brown .  lits - number of linear iterations
50269f67acb7SJed Brown 
50279f67acb7SJed Brown    Notes:
50289f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
50299f67acb7SJed Brown 
50309f67acb7SJed Brown    Level: intermediate
50319f67acb7SJed Brown 
50329f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
50339f67acb7SJed Brown 
50345ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
50359f67acb7SJed Brown @*/
50365ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
50379f67acb7SJed Brown {
50389f67acb7SJed Brown   PetscFunctionBegin;
50399f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50409f67acb7SJed Brown   PetscValidIntPointer(lits,2);
50415ef26d82SJed Brown   *lits = ts->ksp_its;
50429f67acb7SJed Brown   PetscFunctionReturn(0);
50439f67acb7SJed Brown }
50449f67acb7SJed Brown 
50459f67acb7SJed Brown #undef __FUNCT__
5046cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
5047cef5090cSJed Brown /*@
5048cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5049cef5090cSJed Brown 
5050cef5090cSJed Brown    Not Collective
5051cef5090cSJed Brown 
5052cef5090cSJed Brown    Input Parameter:
5053cef5090cSJed Brown .  ts - TS context
5054cef5090cSJed Brown 
5055cef5090cSJed Brown    Output Parameter:
5056cef5090cSJed Brown .  rejects - number of steps rejected
5057cef5090cSJed Brown 
5058cef5090cSJed Brown    Notes:
5059cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5060cef5090cSJed Brown 
5061cef5090cSJed Brown    Level: intermediate
5062cef5090cSJed Brown 
5063cef5090cSJed Brown .keywords: TS, get, number
5064cef5090cSJed Brown 
50655ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5066cef5090cSJed Brown @*/
5067cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5068cef5090cSJed Brown {
5069cef5090cSJed Brown   PetscFunctionBegin;
5070cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5071cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5072cef5090cSJed Brown   *rejects = ts->reject;
5073cef5090cSJed Brown   PetscFunctionReturn(0);
5074cef5090cSJed Brown }
5075cef5090cSJed Brown 
5076cef5090cSJed Brown #undef __FUNCT__
5077cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
5078cef5090cSJed Brown /*@
5079cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5080cef5090cSJed Brown 
5081cef5090cSJed Brown    Not Collective
5082cef5090cSJed Brown 
5083cef5090cSJed Brown    Input Parameter:
5084cef5090cSJed Brown .  ts - TS context
5085cef5090cSJed Brown 
5086cef5090cSJed Brown    Output Parameter:
5087cef5090cSJed Brown .  fails - number of failed nonlinear solves
5088cef5090cSJed Brown 
5089cef5090cSJed Brown    Notes:
5090cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5091cef5090cSJed Brown 
5092cef5090cSJed Brown    Level: intermediate
5093cef5090cSJed Brown 
5094cef5090cSJed Brown .keywords: TS, get, number
5095cef5090cSJed Brown 
50965ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5097cef5090cSJed Brown @*/
5098cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5099cef5090cSJed Brown {
5100cef5090cSJed Brown   PetscFunctionBegin;
5101cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5102cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5103cef5090cSJed Brown   *fails = ts->num_snes_failures;
5104cef5090cSJed Brown   PetscFunctionReturn(0);
5105cef5090cSJed Brown }
5106cef5090cSJed Brown 
5107cef5090cSJed Brown #undef __FUNCT__
5108cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
5109cef5090cSJed Brown /*@
5110cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5111cef5090cSJed Brown 
5112cef5090cSJed Brown    Not Collective
5113cef5090cSJed Brown 
5114cef5090cSJed Brown    Input Parameter:
5115cef5090cSJed Brown +  ts - TS context
5116cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5117cef5090cSJed Brown 
5118cef5090cSJed Brown    Notes:
5119cef5090cSJed Brown    The counter is reset to zero for each step
5120cef5090cSJed Brown 
5121cef5090cSJed Brown    Options Database Key:
5122cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5123cef5090cSJed Brown 
5124cef5090cSJed Brown    Level: intermediate
5125cef5090cSJed Brown 
5126cef5090cSJed Brown .keywords: TS, set, maximum, number
5127cef5090cSJed Brown 
51285ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5129cef5090cSJed Brown @*/
5130cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5131cef5090cSJed Brown {
5132cef5090cSJed Brown   PetscFunctionBegin;
5133cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5134cef5090cSJed Brown   ts->max_reject = rejects;
5135cef5090cSJed Brown   PetscFunctionReturn(0);
5136cef5090cSJed Brown }
5137cef5090cSJed Brown 
5138cef5090cSJed Brown #undef __FUNCT__
5139cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
5140cef5090cSJed Brown /*@
5141cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5142cef5090cSJed Brown 
5143cef5090cSJed Brown    Not Collective
5144cef5090cSJed Brown 
5145cef5090cSJed Brown    Input Parameter:
5146cef5090cSJed Brown +  ts - TS context
5147cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5148cef5090cSJed Brown 
5149cef5090cSJed Brown    Notes:
5150cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5151cef5090cSJed Brown 
5152cef5090cSJed Brown    Options Database Key:
5153cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5154cef5090cSJed Brown 
5155cef5090cSJed Brown    Level: intermediate
5156cef5090cSJed Brown 
5157cef5090cSJed Brown .keywords: TS, set, maximum, number
5158cef5090cSJed Brown 
51595ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5160cef5090cSJed Brown @*/
5161cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5162cef5090cSJed Brown {
5163cef5090cSJed Brown   PetscFunctionBegin;
5164cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5165cef5090cSJed Brown   ts->max_snes_failures = fails;
5166cef5090cSJed Brown   PetscFunctionReturn(0);
5167cef5090cSJed Brown }
5168cef5090cSJed Brown 
5169cef5090cSJed Brown #undef __FUNCT__
51704e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
5171cef5090cSJed Brown /*@
5172cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5173cef5090cSJed Brown 
5174cef5090cSJed Brown    Not Collective
5175cef5090cSJed Brown 
5176cef5090cSJed Brown    Input Parameter:
5177cef5090cSJed Brown +  ts - TS context
5178cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5179cef5090cSJed Brown 
5180cef5090cSJed Brown    Options Database Key:
5181cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5182cef5090cSJed Brown 
5183cef5090cSJed Brown    Level: intermediate
5184cef5090cSJed Brown 
5185cef5090cSJed Brown .keywords: TS, set, error
5186cef5090cSJed Brown 
51875ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5188cef5090cSJed Brown @*/
5189cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5190cef5090cSJed Brown {
5191cef5090cSJed Brown   PetscFunctionBegin;
5192cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5193cef5090cSJed Brown   ts->errorifstepfailed = err;
5194cef5090cSJed Brown   PetscFunctionReturn(0);
5195cef5090cSJed Brown }
5196cef5090cSJed Brown 
5197cef5090cSJed Brown #undef __FUNCT__
5198fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSolution"
5199fb1732b5SBarry Smith /*@C
5200fde5950dSBarry 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
5201fb1732b5SBarry Smith 
5202fb1732b5SBarry Smith    Collective on TS
5203fb1732b5SBarry Smith 
5204fb1732b5SBarry Smith    Input Parameters:
5205fb1732b5SBarry Smith +  ts - the TS context
5206fb1732b5SBarry Smith .  step - current time-step
5207fb1732b5SBarry Smith .  ptime - current time
52080910c330SBarry Smith .  u - current state
5209721cd6eeSBarry Smith -  vf - viewer and its format
5210fb1732b5SBarry Smith 
5211fb1732b5SBarry Smith    Level: intermediate
5212fb1732b5SBarry Smith 
5213fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5214fb1732b5SBarry Smith 
5215fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5216fb1732b5SBarry Smith @*/
5217721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5218fb1732b5SBarry Smith {
5219fb1732b5SBarry Smith   PetscErrorCode ierr;
5220fb1732b5SBarry Smith 
5221fb1732b5SBarry Smith   PetscFunctionBegin;
5222721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5223721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5224721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5225ed81e22dSJed Brown   PetscFunctionReturn(0);
5226ed81e22dSJed Brown }
5227ed81e22dSJed Brown 
5228ed81e22dSJed Brown #undef __FUNCT__
5229ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
5230ed81e22dSJed Brown /*@C
5231ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5232ed81e22dSJed Brown 
5233ed81e22dSJed Brown    Collective on TS
5234ed81e22dSJed Brown 
5235ed81e22dSJed Brown    Input Parameters:
5236ed81e22dSJed Brown +  ts - the TS context
5237ed81e22dSJed Brown .  step - current time-step
5238ed81e22dSJed Brown .  ptime - current time
52390910c330SBarry Smith .  u - current state
5240ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5241ed81e22dSJed Brown 
5242ed81e22dSJed Brown    Level: intermediate
5243ed81e22dSJed Brown 
5244ed81e22dSJed Brown    Notes:
5245ed81e22dSJed 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.
5246ed81e22dSJed Brown    These are named according to the file name template.
5247ed81e22dSJed Brown 
5248ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5249ed81e22dSJed Brown 
5250ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5251ed81e22dSJed Brown 
5252ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5253ed81e22dSJed Brown @*/
52540910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5255ed81e22dSJed Brown {
5256ed81e22dSJed Brown   PetscErrorCode ierr;
5257ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5258ed81e22dSJed Brown   PetscViewer    viewer;
5259ed81e22dSJed Brown 
5260ed81e22dSJed Brown   PetscFunctionBegin;
526163e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
52628caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5263ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
52640910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5265ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5266ed81e22dSJed Brown   PetscFunctionReturn(0);
5267ed81e22dSJed Brown }
5268ed81e22dSJed Brown 
5269ed81e22dSJed Brown #undef __FUNCT__
5270ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
5271ed81e22dSJed Brown /*@C
5272ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5273ed81e22dSJed Brown 
5274ed81e22dSJed Brown    Collective on TS
5275ed81e22dSJed Brown 
5276ed81e22dSJed Brown    Input Parameters:
5277ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5278ed81e22dSJed Brown 
5279ed81e22dSJed Brown    Level: intermediate
5280ed81e22dSJed Brown 
5281ed81e22dSJed Brown    Note:
5282ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5283ed81e22dSJed Brown 
5284ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5285ed81e22dSJed Brown 
5286ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5287ed81e22dSJed Brown @*/
5288ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5289ed81e22dSJed Brown {
5290ed81e22dSJed Brown   PetscErrorCode ierr;
5291ed81e22dSJed Brown 
5292ed81e22dSJed Brown   PetscFunctionBegin;
5293ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5294fb1732b5SBarry Smith   PetscFunctionReturn(0);
5295fb1732b5SBarry Smith }
5296fb1732b5SBarry Smith 
529784df9cb4SJed Brown #undef __FUNCT__
5298552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
529984df9cb4SJed Brown /*@
5300552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
530184df9cb4SJed Brown 
5302ed81e22dSJed Brown    Collective on TS if controller has not been created yet
530384df9cb4SJed Brown 
530484df9cb4SJed Brown    Input Arguments:
5305ed81e22dSJed Brown .  ts - time stepping context
530684df9cb4SJed Brown 
530784df9cb4SJed Brown    Output Arguments:
5308ed81e22dSJed Brown .  adapt - adaptive controller
530984df9cb4SJed Brown 
531084df9cb4SJed Brown    Level: intermediate
531184df9cb4SJed Brown 
5312ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
531384df9cb4SJed Brown @*/
5314552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
531584df9cb4SJed Brown {
531684df9cb4SJed Brown   PetscErrorCode ierr;
531784df9cb4SJed Brown 
531884df9cb4SJed Brown   PetscFunctionBegin;
531984df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5320bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
532184df9cb4SJed Brown   if (!ts->adapt) {
5322ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
53233bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
53241c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
532584df9cb4SJed Brown   }
5326bec58848SLisandro Dalcin   *adapt = ts->adapt;
532784df9cb4SJed Brown   PetscFunctionReturn(0);
532884df9cb4SJed Brown }
5329d6ebe24aSShri Abhyankar 
5330d6ebe24aSShri Abhyankar #undef __FUNCT__
53311c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
53321c3436cfSJed Brown /*@
53331c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
53341c3436cfSJed Brown 
53351c3436cfSJed Brown    Logically Collective
53361c3436cfSJed Brown 
53371c3436cfSJed Brown    Input Arguments:
53381c3436cfSJed Brown +  ts - time integration context
53391c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
53400298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
53411c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
53420298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
53431c3436cfSJed Brown 
5344a3cdaa26SBarry Smith    Options Database keys:
5345a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5346a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5347a3cdaa26SBarry Smith 
53483ff766beSShri Abhyankar    Notes:
53493ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
53503ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
53513ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
53523ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
53533ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
53543ff766beSShri Abhyankar    differential variables.
53553ff766beSShri Abhyankar 
53561c3436cfSJed Brown    Level: beginner
53571c3436cfSJed Brown 
5358c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
53591c3436cfSJed Brown @*/
53601c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
53611c3436cfSJed Brown {
53621c3436cfSJed Brown   PetscErrorCode ierr;
53631c3436cfSJed Brown 
53641c3436cfSJed Brown   PetscFunctionBegin;
5365c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
53661c3436cfSJed Brown   if (vatol) {
53671c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
53681c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
53691c3436cfSJed Brown     ts->vatol = vatol;
53701c3436cfSJed Brown   }
5371c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
53721c3436cfSJed Brown   if (vrtol) {
53731c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
53741c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
53751c3436cfSJed Brown     ts->vrtol = vrtol;
53761c3436cfSJed Brown   }
53771c3436cfSJed Brown   PetscFunctionReturn(0);
53781c3436cfSJed Brown }
53791c3436cfSJed Brown 
53801c3436cfSJed Brown #undef __FUNCT__
5381c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
5382c5033834SJed Brown /*@
5383c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5384c5033834SJed Brown 
5385c5033834SJed Brown    Logically Collective
5386c5033834SJed Brown 
5387c5033834SJed Brown    Input Arguments:
5388c5033834SJed Brown .  ts - time integration context
5389c5033834SJed Brown 
5390c5033834SJed Brown    Output Arguments:
53910298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
53920298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
53930298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
53940298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5395c5033834SJed Brown 
5396c5033834SJed Brown    Level: beginner
5397c5033834SJed Brown 
5398c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5399c5033834SJed Brown @*/
5400c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5401c5033834SJed Brown {
5402c5033834SJed Brown   PetscFunctionBegin;
5403c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5404c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5405c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5406c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5407c5033834SJed Brown   PetscFunctionReturn(0);
5408c5033834SJed Brown }
5409c5033834SJed Brown 
5410c5033834SJed Brown #undef __FUNCT__
54119c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2"
54129c6b16b5SShri Abhyankar /*@
5413a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
54149c6b16b5SShri Abhyankar 
54159c6b16b5SShri Abhyankar    Collective on TS
54169c6b16b5SShri Abhyankar 
54179c6b16b5SShri Abhyankar    Input Arguments:
54189c6b16b5SShri Abhyankar +  ts - time stepping context
5419a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5420a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
54219c6b16b5SShri Abhyankar 
54229c6b16b5SShri Abhyankar    Output Arguments:
54239c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
54249c6b16b5SShri Abhyankar 
54259c6b16b5SShri Abhyankar    Level: developer
54269c6b16b5SShri Abhyankar 
5427deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
54289c6b16b5SShri Abhyankar @*/
5429a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm)
54309c6b16b5SShri Abhyankar {
54319c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
54329c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
54339c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
54349c6b16b5SShri Abhyankar   PetscReal         sum,gsum;
54359c6b16b5SShri Abhyankar   PetscReal         tol;
54369c6b16b5SShri Abhyankar 
54379c6b16b5SShri Abhyankar   PetscFunctionBegin;
54389c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5439a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5440a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5441a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5442a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5443a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5444a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
5445a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
54469c6b16b5SShri Abhyankar 
54479c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
54489c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
54499c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
54509c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
54519c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
54529c6b16b5SShri Abhyankar   sum  = 0.;
54539c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
54549c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
54559c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54569c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54579c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54589c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54599c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
54609c6b16b5SShri Abhyankar     }
54619c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54629c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54639c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
54649c6b16b5SShri Abhyankar     const PetscScalar *atol;
54659c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54669c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54679c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54689c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
54699c6b16b5SShri Abhyankar     }
54709c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54719c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
54729c6b16b5SShri Abhyankar     const PetscScalar *rtol;
54739c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54749c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54759c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54769c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
54779c6b16b5SShri Abhyankar     }
54789c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54799c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
54809c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54819c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54829c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
54839c6b16b5SShri Abhyankar     }
54849c6b16b5SShri Abhyankar   }
54859c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
54869c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
54879c6b16b5SShri Abhyankar 
5488b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
54899c6b16b5SShri Abhyankar   *norm = PetscSqrtReal(gsum / N);
54909c6b16b5SShri Abhyankar 
54919c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
54929c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
54939c6b16b5SShri Abhyankar }
54949c6b16b5SShri Abhyankar 
54959c6b16b5SShri Abhyankar #undef __FUNCT__
54969c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity"
54979c6b16b5SShri Abhyankar /*@
5498a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
54999c6b16b5SShri Abhyankar 
55009c6b16b5SShri Abhyankar    Collective on TS
55019c6b16b5SShri Abhyankar 
55029c6b16b5SShri Abhyankar    Input Arguments:
55039c6b16b5SShri Abhyankar +  ts - time stepping context
5504a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5505a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
55069c6b16b5SShri Abhyankar 
55079c6b16b5SShri Abhyankar    Output Arguments:
55089c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
55099c6b16b5SShri Abhyankar 
55109c6b16b5SShri Abhyankar    Level: developer
55119c6b16b5SShri Abhyankar 
5512deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
55139c6b16b5SShri Abhyankar @*/
5514a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm)
55159c6b16b5SShri Abhyankar {
55169c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
55179c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart,k;
55189c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
55199c6b16b5SShri Abhyankar   PetscReal         max,gmax;
55209c6b16b5SShri Abhyankar   PetscReal         tol;
55219c6b16b5SShri Abhyankar 
55229c6b16b5SShri Abhyankar   PetscFunctionBegin;
55239c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5524a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5525a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5526a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5527a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5528a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5529a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
5530a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
55319c6b16b5SShri Abhyankar 
55329c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
55339c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
55349c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
55359c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
55369c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
55379c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
55389c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
55399c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55409c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55419c6b16b5SShri Abhyankar     k = 0;
55429c6b16b5SShri Abhyankar     tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
55439c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
55449c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
55459c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55469c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
55479c6b16b5SShri Abhyankar     }
55489c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55499c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55509c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
55519c6b16b5SShri Abhyankar     const PetscScalar *atol;
55529c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55539c6b16b5SShri Abhyankar     k = 0;
55549c6b16b5SShri Abhyankar     tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
55559c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
55569c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
55579c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55589c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
55599c6b16b5SShri Abhyankar     }
55609c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55619c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
55629c6b16b5SShri Abhyankar     const PetscScalar *rtol;
55639c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55649c6b16b5SShri Abhyankar     k = 0;
55659c6b16b5SShri Abhyankar     tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
55669c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
55679c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
55689c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55699c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
55709c6b16b5SShri Abhyankar     }
55719c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55729c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
55739c6b16b5SShri Abhyankar     k = 0;
55749c6b16b5SShri Abhyankar     tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
55759c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
55769c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
55779c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55789c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
55799c6b16b5SShri Abhyankar     }
55809c6b16b5SShri Abhyankar   }
55819c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
55829c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
55839c6b16b5SShri Abhyankar 
5584b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
55859c6b16b5SShri Abhyankar   *norm = gmax;
55869c6b16b5SShri Abhyankar 
55879c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
55889c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
55899c6b16b5SShri Abhyankar }
55909c6b16b5SShri Abhyankar 
55919c6b16b5SShri Abhyankar #undef __FUNCT__
55927619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm"
55931c3436cfSJed Brown /*@
5594a4868fbcSLisandro Dalcin    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors
55951c3436cfSJed Brown 
55961c3436cfSJed Brown    Collective on TS
55971c3436cfSJed Brown 
55981c3436cfSJed Brown    Input Arguments:
55991c3436cfSJed Brown +  ts - time stepping context
5600a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5601a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5602a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
56037619abb3SShri 
56041c3436cfSJed Brown    Output Arguments:
56051c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
56061c3436cfSJed Brown 
5607a4868fbcSLisandro Dalcin 
5608a4868fbcSLisandro Dalcin    Options Database Keys:
5609a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5610a4868fbcSLisandro Dalcin 
56111c3436cfSJed Brown    Level: developer
56121c3436cfSJed Brown 
5613deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
56141c3436cfSJed Brown @*/
5615a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm)
56161c3436cfSJed Brown {
56178beabaa1SBarry Smith   PetscErrorCode ierr;
56181c3436cfSJed Brown 
56191c3436cfSJed Brown   PetscFunctionBegin;
5620a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
5621a4868fbcSLisandro Dalcin     ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr);
5622a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
5623a4868fbcSLisandro Dalcin     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr);
5624a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
56251c3436cfSJed Brown   PetscFunctionReturn(0);
56261c3436cfSJed Brown }
56271c3436cfSJed Brown 
56281c3436cfSJed Brown #undef __FUNCT__
56298d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
56308d59e960SJed Brown /*@
56318d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
56328d59e960SJed Brown 
56338d59e960SJed Brown    Logically Collective on TS
56348d59e960SJed Brown 
56358d59e960SJed Brown    Input Arguments:
56368d59e960SJed Brown +  ts - time stepping context
56378d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
56388d59e960SJed Brown 
56398d59e960SJed Brown    Note:
56408d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
56418d59e960SJed Brown 
56428d59e960SJed Brown    Level: intermediate
56438d59e960SJed Brown 
56448d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
56458d59e960SJed Brown @*/
56468d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
56478d59e960SJed Brown {
56488d59e960SJed Brown   PetscFunctionBegin;
56498d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
56508d59e960SJed Brown   ts->cfltime_local = cfltime;
56518d59e960SJed Brown   ts->cfltime       = -1.;
56528d59e960SJed Brown   PetscFunctionReturn(0);
56538d59e960SJed Brown }
56548d59e960SJed Brown 
56558d59e960SJed Brown #undef __FUNCT__
56568d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
56578d59e960SJed Brown /*@
56588d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
56598d59e960SJed Brown 
56608d59e960SJed Brown    Collective on TS
56618d59e960SJed Brown 
56628d59e960SJed Brown    Input Arguments:
56638d59e960SJed Brown .  ts - time stepping context
56648d59e960SJed Brown 
56658d59e960SJed Brown    Output Arguments:
56668d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
56678d59e960SJed Brown 
56688d59e960SJed Brown    Level: advanced
56698d59e960SJed Brown 
56708d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
56718d59e960SJed Brown @*/
56728d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
56738d59e960SJed Brown {
56748d59e960SJed Brown   PetscErrorCode ierr;
56758d59e960SJed Brown 
56768d59e960SJed Brown   PetscFunctionBegin;
56778d59e960SJed Brown   if (ts->cfltime < 0) {
5678b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
56798d59e960SJed Brown   }
56808d59e960SJed Brown   *cfltime = ts->cfltime;
56818d59e960SJed Brown   PetscFunctionReturn(0);
56828d59e960SJed Brown }
56838d59e960SJed Brown 
56848d59e960SJed Brown #undef __FUNCT__
5685d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
5686d6ebe24aSShri Abhyankar /*@
5687d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5688d6ebe24aSShri Abhyankar 
5689d6ebe24aSShri Abhyankar    Input Parameters:
5690d6ebe24aSShri Abhyankar .  ts   - the TS context.
5691d6ebe24aSShri Abhyankar .  xl   - lower bound.
5692d6ebe24aSShri Abhyankar .  xu   - upper bound.
5693d6ebe24aSShri Abhyankar 
5694d6ebe24aSShri Abhyankar    Notes:
5695d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
5696e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
5697d6ebe24aSShri Abhyankar 
56982bd2b0e6SSatish Balay    Level: advanced
56992bd2b0e6SSatish Balay 
5700d6ebe24aSShri Abhyankar @*/
5701d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5702d6ebe24aSShri Abhyankar {
5703d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
5704d6ebe24aSShri Abhyankar   SNES           snes;
5705d6ebe24aSShri Abhyankar 
5706d6ebe24aSShri Abhyankar   PetscFunctionBegin;
5707d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5708d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
5709d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5710d6ebe24aSShri Abhyankar }
5711d6ebe24aSShri Abhyankar 
5712325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
5713c6db04a5SJed Brown #include <mex.h>
5714325fc9f4SBarry Smith 
5715325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
5716325fc9f4SBarry Smith 
5717325fc9f4SBarry Smith #undef __FUNCT__
5718325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
5719325fc9f4SBarry Smith /*
5720325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
5721325fc9f4SBarry Smith                          TSSetFunctionMatlab().
5722325fc9f4SBarry Smith 
5723325fc9f4SBarry Smith    Collective on TS
5724325fc9f4SBarry Smith 
5725325fc9f4SBarry Smith    Input Parameters:
5726325fc9f4SBarry Smith +  snes - the TS context
57270910c330SBarry Smith -  u - input vector
5728325fc9f4SBarry Smith 
5729325fc9f4SBarry Smith    Output Parameter:
5730325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
5731325fc9f4SBarry Smith 
5732325fc9f4SBarry Smith    Notes:
5733325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
5734325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
5735325fc9f4SBarry Smith    themselves.
5736325fc9f4SBarry Smith 
5737325fc9f4SBarry Smith    Level: developer
5738325fc9f4SBarry Smith 
5739325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5740325fc9f4SBarry Smith 
5741325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5742325fc9f4SBarry Smith */
57430910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
5744325fc9f4SBarry Smith {
5745325fc9f4SBarry Smith   PetscErrorCode  ierr;
5746325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5747325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
5748325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
5749325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
5750325fc9f4SBarry Smith 
5751325fc9f4SBarry Smith   PetscFunctionBegin;
5752325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
57530910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
57540910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
5755325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
57560910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
5757325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
5758325fc9f4SBarry Smith 
5759325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
57600910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
57610910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
57620910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
5763bbd56ea5SKarl Rupp 
5764325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5765325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
5766325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5767325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5768325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
5769325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
5770325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
5771325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
5772325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5773325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5774325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5775325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5776325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5777325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5778325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5779325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5780325fc9f4SBarry Smith   PetscFunctionReturn(0);
5781325fc9f4SBarry Smith }
5782325fc9f4SBarry Smith 
5783325fc9f4SBarry Smith 
5784325fc9f4SBarry Smith #undef __FUNCT__
5785325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
5786325fc9f4SBarry Smith /*
5787325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
5788325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
5789e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
5790325fc9f4SBarry Smith 
5791325fc9f4SBarry Smith    Logically Collective on TS
5792325fc9f4SBarry Smith 
5793325fc9f4SBarry Smith    Input Parameters:
5794325fc9f4SBarry Smith +  ts - the TS context
5795325fc9f4SBarry Smith -  func - function evaluation routine
5796325fc9f4SBarry Smith 
5797325fc9f4SBarry Smith    Calling sequence of func:
57980910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
5799325fc9f4SBarry Smith 
5800325fc9f4SBarry Smith    Level: beginner
5801325fc9f4SBarry Smith 
5802325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5803325fc9f4SBarry Smith 
5804325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5805325fc9f4SBarry Smith */
5806cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
5807325fc9f4SBarry Smith {
5808325fc9f4SBarry Smith   PetscErrorCode  ierr;
5809325fc9f4SBarry Smith   TSMatlabContext *sctx;
5810325fc9f4SBarry Smith 
5811325fc9f4SBarry Smith   PetscFunctionBegin;
5812325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5813325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5814325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5815325fc9f4SBarry Smith   /*
5816325fc9f4SBarry Smith      This should work, but it doesn't
5817325fc9f4SBarry Smith   sctx->ctx = ctx;
5818325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5819325fc9f4SBarry Smith   */
5820325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5821bbd56ea5SKarl Rupp 
58220298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
5823325fc9f4SBarry Smith   PetscFunctionReturn(0);
5824325fc9f4SBarry Smith }
5825325fc9f4SBarry Smith 
5826325fc9f4SBarry Smith #undef __FUNCT__
5827325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
5828325fc9f4SBarry Smith /*
5829325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
5830325fc9f4SBarry Smith                          TSSetJacobianMatlab().
5831325fc9f4SBarry Smith 
5832325fc9f4SBarry Smith    Collective on TS
5833325fc9f4SBarry Smith 
5834325fc9f4SBarry Smith    Input Parameters:
5835cdcf91faSSean Farley +  ts - the TS context
58360910c330SBarry Smith .  u - input vector
5837325fc9f4SBarry Smith .  A, B - the matrices
5838325fc9f4SBarry Smith -  ctx - user context
5839325fc9f4SBarry Smith 
5840325fc9f4SBarry Smith    Level: developer
5841325fc9f4SBarry Smith 
5842325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5843325fc9f4SBarry Smith 
5844325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5845325fc9f4SBarry Smith @*/
5846f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
5847325fc9f4SBarry Smith {
5848325fc9f4SBarry Smith   PetscErrorCode  ierr;
5849325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5850325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
5851325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
5852325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
5853325fc9f4SBarry Smith 
5854325fc9f4SBarry Smith   PetscFunctionBegin;
5855cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
58560910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
5857325fc9f4SBarry Smith 
58580910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
5859325fc9f4SBarry Smith 
5860cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
58610910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
58620910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
58630910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
58640910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
5865bbd56ea5SKarl Rupp 
5866325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5867325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
5868325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5869325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5870325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
5871325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
5872325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
5873325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
5874325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
5875325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
5876325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5877325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5878325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5879325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5880325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5881325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5882325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5883325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
5884325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
5885325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5886325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
5887325fc9f4SBarry Smith   PetscFunctionReturn(0);
5888325fc9f4SBarry Smith }
5889325fc9f4SBarry Smith 
5890325fc9f4SBarry Smith 
5891325fc9f4SBarry Smith #undef __FUNCT__
5892325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
5893325fc9f4SBarry Smith /*
5894325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
5895e3c5b3baSBarry 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
5896325fc9f4SBarry Smith 
5897325fc9f4SBarry Smith    Logically Collective on TS
5898325fc9f4SBarry Smith 
5899325fc9f4SBarry Smith    Input Parameters:
5900cdcf91faSSean Farley +  ts - the TS context
5901325fc9f4SBarry Smith .  A,B - Jacobian matrices
5902325fc9f4SBarry Smith .  func - function evaluation routine
5903325fc9f4SBarry Smith -  ctx - user context
5904325fc9f4SBarry Smith 
5905325fc9f4SBarry Smith    Calling sequence of func:
59060910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
5907325fc9f4SBarry Smith 
5908325fc9f4SBarry Smith 
5909325fc9f4SBarry Smith    Level: developer
5910325fc9f4SBarry Smith 
5911325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5912325fc9f4SBarry Smith 
5913325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5914325fc9f4SBarry Smith */
5915cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
5916325fc9f4SBarry Smith {
5917325fc9f4SBarry Smith   PetscErrorCode  ierr;
5918325fc9f4SBarry Smith   TSMatlabContext *sctx;
5919325fc9f4SBarry Smith 
5920325fc9f4SBarry Smith   PetscFunctionBegin;
5921325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5922325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5923325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5924325fc9f4SBarry Smith   /*
5925325fc9f4SBarry Smith      This should work, but it doesn't
5926325fc9f4SBarry Smith   sctx->ctx = ctx;
5927325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5928325fc9f4SBarry Smith   */
5929325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5930bbd56ea5SKarl Rupp 
5931cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
5932325fc9f4SBarry Smith   PetscFunctionReturn(0);
5933325fc9f4SBarry Smith }
5934325fc9f4SBarry Smith 
5935b5b1a830SBarry Smith #undef __FUNCT__
5936b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
5937b5b1a830SBarry Smith /*
5938b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
5939b5b1a830SBarry Smith 
5940b5b1a830SBarry Smith    Collective on TS
5941b5b1a830SBarry Smith 
5942b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5943b5b1a830SBarry Smith @*/
59440910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
5945b5b1a830SBarry Smith {
5946b5b1a830SBarry Smith   PetscErrorCode  ierr;
5947b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5948a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
5949b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
5950b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
5951b5b1a830SBarry Smith 
5952b5b1a830SBarry Smith   PetscFunctionBegin;
5953cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
59540910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
5955b5b1a830SBarry Smith 
5956cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
59570910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
5958bbd56ea5SKarl Rupp 
5959b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5960b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
5961b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
5962b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
5963b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
5964b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
5965b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
5966b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5967b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
5968b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
5969b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
5970b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
5971b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
5972b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
5973b5b1a830SBarry Smith   PetscFunctionReturn(0);
5974b5b1a830SBarry Smith }
5975b5b1a830SBarry Smith 
5976b5b1a830SBarry Smith 
5977b5b1a830SBarry Smith #undef __FUNCT__
5978b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
5979b5b1a830SBarry Smith /*
5980b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
5981b5b1a830SBarry Smith 
5982b5b1a830SBarry Smith    Level: developer
5983b5b1a830SBarry Smith 
5984b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
5985b5b1a830SBarry Smith 
5986b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5987b5b1a830SBarry Smith */
5988cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
5989b5b1a830SBarry Smith {
5990b5b1a830SBarry Smith   PetscErrorCode  ierr;
5991b5b1a830SBarry Smith   TSMatlabContext *sctx;
5992b5b1a830SBarry Smith 
5993b5b1a830SBarry Smith   PetscFunctionBegin;
5994b5b1a830SBarry Smith   /* currently sctx is memory bleed */
5995b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5996b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5997b5b1a830SBarry Smith   /*
5998b5b1a830SBarry Smith      This should work, but it doesn't
5999b5b1a830SBarry Smith   sctx->ctx = ctx;
6000b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6001b5b1a830SBarry Smith   */
6002b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6003bbd56ea5SKarl Rupp 
60040298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6005b5b1a830SBarry Smith   PetscFunctionReturn(0);
6006b5b1a830SBarry Smith }
6007325fc9f4SBarry Smith #endif
6008b3603a34SBarry Smith 
6009b3603a34SBarry Smith #undef __FUNCT__
60104f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
6011b3603a34SBarry Smith /*@C
60124f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6013b3603a34SBarry Smith        in a time based line graph
6014b3603a34SBarry Smith 
6015b3603a34SBarry Smith    Collective on TS
6016b3603a34SBarry Smith 
6017b3603a34SBarry Smith    Input Parameters:
6018b3603a34SBarry Smith +  ts - the TS context
6019b3603a34SBarry Smith .  step - current time-step
6020b3603a34SBarry Smith .  ptime - current time
60217db568b7SBarry Smith .  u - current solution
60227db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6023b3603a34SBarry Smith 
6024b3d3934dSBarry Smith    Options Database:
60259ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6026b3d3934dSBarry Smith 
6027b3603a34SBarry Smith    Level: intermediate
6028b3603a34SBarry Smith 
60296934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component solutions in a separate window
6030b3603a34SBarry Smith 
6031b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6032b3603a34SBarry Smith 
60337db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
60347db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
60357db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
60367db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6037b3603a34SBarry Smith @*/
60387db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6039b3603a34SBarry Smith {
6040b3603a34SBarry Smith   PetscErrorCode    ierr;
60417db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6042b3603a34SBarry Smith   const PetscScalar *yy;
604380666b62SBarry Smith   Vec               v;
6044b3603a34SBarry Smith 
6045b3603a34SBarry Smith   PetscFunctionBegin;
604663e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
604758ff32f7SBarry Smith   if (!step) {
6048a9f9c1f6SBarry Smith     PetscDrawAxis axis;
60496934998bSLisandro Dalcin     PetscInt      dim;
6050a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6051a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6052387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6053387f4636SBarry Smith       char      **displaynames;
6054387f4636SBarry Smith       PetscBool flg;
6055387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6056387f4636SBarry Smith       ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr);
6057387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6058c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6059387f4636SBarry Smith       if (flg) {
6060a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6061387f4636SBarry Smith       }
6062387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6063387f4636SBarry Smith     }
6064387f4636SBarry Smith     if (ctx->displaynames) {
6065387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6066387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6067387f4636SBarry Smith     } else if (ctx->names) {
60680910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
60690b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6070387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6071b0bc92c6SBarry Smith     } else {
6072b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6073b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6074387f4636SBarry Smith     }
60750b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
607658ff32f7SBarry Smith   }
60776934998bSLisandro Dalcin 
60786934998bSLisandro Dalcin   if (!ctx->transform) v = u;
60796934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
608080666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
60816934998bSLisandro Dalcin   if (ctx->displaynames) {
60826934998bSLisandro Dalcin     PetscInt i;
60836934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
60846934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
60856934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
60866934998bSLisandro Dalcin   } else {
6087e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6088e3efe391SJed Brown     PetscInt  i,n;
60896934998bSLisandro Dalcin     PetscReal *yreal;
609080666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6091785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6092e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
60930b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6094e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6095e3efe391SJed Brown #else
60960b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6097e3efe391SJed Brown #endif
609880666b62SBarry Smith   }
60996934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
61006934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
61016934998bSLisandro Dalcin 
6102b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
61030b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
61046934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
61053923b477SBarry Smith   }
6106b3603a34SBarry Smith   PetscFunctionReturn(0);
6107b3603a34SBarry Smith }
6108b3603a34SBarry Smith 
6109387f4636SBarry Smith 
6110b3603a34SBarry Smith #undef __FUNCT__
611131152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames"
6112b037adc7SBarry Smith /*@C
611331152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6114b037adc7SBarry Smith 
6115b037adc7SBarry Smith    Collective on TS
6116b037adc7SBarry Smith 
6117b037adc7SBarry Smith    Input Parameters:
6118b037adc7SBarry Smith +  ts - the TS context
6119b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6120b037adc7SBarry Smith 
6121b037adc7SBarry Smith    Level: intermediate
6122b037adc7SBarry Smith 
61237db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
61247db568b7SBarry Smith 
6125b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
6126b037adc7SBarry Smith 
6127a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6128b037adc7SBarry Smith @*/
612931152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6130b037adc7SBarry Smith {
6131b037adc7SBarry Smith   PetscErrorCode    ierr;
6132b037adc7SBarry Smith   PetscInt          i;
6133b037adc7SBarry Smith 
6134b037adc7SBarry Smith   PetscFunctionBegin;
6135b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6136b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
61375537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6138b3d3934dSBarry Smith       break;
6139b3d3934dSBarry Smith     }
6140b3d3934dSBarry Smith   }
6141b3d3934dSBarry Smith   PetscFunctionReturn(0);
6142b3d3934dSBarry Smith }
6143b3d3934dSBarry Smith 
6144b3d3934dSBarry Smith #undef __FUNCT__
6145e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames"
6146e673d494SBarry Smith /*@C
6147e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6148e673d494SBarry Smith 
6149e673d494SBarry Smith    Collective on TS
6150e673d494SBarry Smith 
6151e673d494SBarry Smith    Input Parameters:
6152e673d494SBarry Smith +  ts - the TS context
6153e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6154e673d494SBarry Smith 
6155e673d494SBarry Smith    Level: intermediate
6156e673d494SBarry Smith 
6157e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6158e673d494SBarry Smith 
6159a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6160e673d494SBarry Smith @*/
6161e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6162e673d494SBarry Smith {
6163e673d494SBarry Smith   PetscErrorCode    ierr;
6164e673d494SBarry Smith 
6165e673d494SBarry Smith   PetscFunctionBegin;
6166e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6167e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6168e673d494SBarry Smith   PetscFunctionReturn(0);
6169e673d494SBarry Smith }
6170e673d494SBarry Smith 
6171e673d494SBarry Smith #undef __FUNCT__
6172b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames"
6173b3d3934dSBarry Smith /*@C
6174b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6175b3d3934dSBarry Smith 
6176b3d3934dSBarry Smith    Collective on TS
6177b3d3934dSBarry Smith 
6178b3d3934dSBarry Smith    Input Parameter:
6179b3d3934dSBarry Smith .  ts - the TS context
6180b3d3934dSBarry Smith 
6181b3d3934dSBarry Smith    Output Parameter:
6182b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6183b3d3934dSBarry Smith 
6184b3d3934dSBarry Smith    Level: intermediate
6185b3d3934dSBarry Smith 
61867db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
61877db568b7SBarry Smith 
6188b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6189b3d3934dSBarry Smith 
6190b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6191b3d3934dSBarry Smith @*/
6192b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6193b3d3934dSBarry Smith {
6194b3d3934dSBarry Smith   PetscInt       i;
6195b3d3934dSBarry Smith 
6196b3d3934dSBarry Smith   PetscFunctionBegin;
6197b3d3934dSBarry Smith   *names = NULL;
6198b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6199b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
62005537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6201b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6202b3d3934dSBarry Smith       break;
6203387f4636SBarry Smith     }
6204387f4636SBarry Smith   }
6205387f4636SBarry Smith   PetscFunctionReturn(0);
6206387f4636SBarry Smith }
6207387f4636SBarry Smith 
6208387f4636SBarry Smith #undef __FUNCT__
6209a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables"
6210a66092f1SBarry Smith /*@C
6211a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6212a66092f1SBarry Smith 
6213a66092f1SBarry Smith    Collective on TS
6214a66092f1SBarry Smith 
6215a66092f1SBarry Smith    Input Parameters:
6216a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6217a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
6218a66092f1SBarry Smith 
6219a66092f1SBarry Smith    Level: intermediate
6220a66092f1SBarry Smith 
6221a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
6222a66092f1SBarry Smith 
6223a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6224a66092f1SBarry Smith @*/
6225a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6226a66092f1SBarry Smith {
6227a66092f1SBarry Smith   PetscInt          j = 0,k;
6228a66092f1SBarry Smith   PetscErrorCode    ierr;
6229a66092f1SBarry Smith 
6230a66092f1SBarry Smith   PetscFunctionBegin;
6231a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6232a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6233a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6234a66092f1SBarry Smith   while (displaynames[j]) j++;
6235a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6236a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6237a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6238a66092f1SBarry Smith   j = 0;
6239a66092f1SBarry Smith   while (displaynames[j]) {
6240a66092f1SBarry Smith     k = 0;
6241a66092f1SBarry Smith     while (ctx->names[k]) {
6242a66092f1SBarry Smith       PetscBool flg;
6243a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6244a66092f1SBarry Smith       if (flg) {
6245a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6246a66092f1SBarry Smith         break;
6247a66092f1SBarry Smith       }
6248a66092f1SBarry Smith       k++;
6249a66092f1SBarry Smith     }
6250a66092f1SBarry Smith     j++;
6251a66092f1SBarry Smith   }
6252a66092f1SBarry Smith   PetscFunctionReturn(0);
6253a66092f1SBarry Smith }
6254a66092f1SBarry Smith 
6255a66092f1SBarry Smith 
6256a66092f1SBarry Smith #undef __FUNCT__
6257387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables"
6258387f4636SBarry Smith /*@C
6259387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6260387f4636SBarry Smith 
6261387f4636SBarry Smith    Collective on TS
6262387f4636SBarry Smith 
6263387f4636SBarry Smith    Input Parameters:
6264387f4636SBarry Smith +  ts - the TS context
6265387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
6266387f4636SBarry Smith 
62677db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
62687db568b7SBarry Smith 
6269387f4636SBarry Smith    Level: intermediate
6270387f4636SBarry Smith 
6271387f4636SBarry Smith .keywords: TS,  vector, monitor, view
6272387f4636SBarry Smith 
6273387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6274387f4636SBarry Smith @*/
6275387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6276387f4636SBarry Smith {
6277a66092f1SBarry Smith   PetscInt          i;
6278387f4636SBarry Smith   PetscErrorCode    ierr;
6279387f4636SBarry Smith 
6280387f4636SBarry Smith   PetscFunctionBegin;
6281387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6282387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
62835537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6284b3d3934dSBarry Smith       break;
6285b037adc7SBarry Smith     }
6286b037adc7SBarry Smith   }
6287b037adc7SBarry Smith   PetscFunctionReturn(0);
6288b037adc7SBarry Smith }
6289b037adc7SBarry Smith 
6290b037adc7SBarry Smith #undef __FUNCT__
629180666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform"
629280666b62SBarry Smith /*@C
629380666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
629480666b62SBarry Smith 
629580666b62SBarry Smith    Collective on TS
629680666b62SBarry Smith 
629780666b62SBarry Smith    Input Parameters:
629880666b62SBarry Smith +  ts - the TS context
629980666b62SBarry Smith .  transform - the transform function
63007684fa3eSBarry Smith .  destroy - function to destroy the optional context
630180666b62SBarry Smith -  ctx - optional context used by transform function
630280666b62SBarry Smith 
63037db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
63047db568b7SBarry Smith 
630580666b62SBarry Smith    Level: intermediate
630680666b62SBarry Smith 
630780666b62SBarry Smith .keywords: TS,  vector, monitor, view
630880666b62SBarry Smith 
6309a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
631080666b62SBarry Smith @*/
63117684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
631280666b62SBarry Smith {
631380666b62SBarry Smith   PetscInt          i;
6314a66092f1SBarry Smith   PetscErrorCode    ierr;
631580666b62SBarry Smith 
631680666b62SBarry Smith   PetscFunctionBegin;
631780666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
631880666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
63195537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
632080666b62SBarry Smith     }
632180666b62SBarry Smith   }
632280666b62SBarry Smith   PetscFunctionReturn(0);
632380666b62SBarry Smith }
632480666b62SBarry Smith 
632580666b62SBarry Smith #undef __FUNCT__
6326e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform"
6327e673d494SBarry Smith /*@C
6328e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6329e673d494SBarry Smith 
6330e673d494SBarry Smith    Collective on TSLGCtx
6331e673d494SBarry Smith 
6332e673d494SBarry Smith    Input Parameters:
6333e673d494SBarry Smith +  ts - the TS context
6334e673d494SBarry Smith .  transform - the transform function
63357684fa3eSBarry Smith .  destroy - function to destroy the optional context
6336e673d494SBarry Smith -  ctx - optional context used by transform function
6337e673d494SBarry Smith 
6338e673d494SBarry Smith    Level: intermediate
6339e673d494SBarry Smith 
6340e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6341e673d494SBarry Smith 
6342a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6343e673d494SBarry Smith @*/
63447684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6345e673d494SBarry Smith {
6346e673d494SBarry Smith   PetscFunctionBegin;
6347e673d494SBarry Smith   ctx->transform    = transform;
63487684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6349e673d494SBarry Smith   ctx->transformctx = tctx;
6350e673d494SBarry Smith   PetscFunctionReturn(0);
6351e673d494SBarry Smith }
6352e673d494SBarry Smith 
6353b3603a34SBarry Smith #undef __FUNCT__
63544f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
6355ef20d060SBarry Smith /*@C
63564f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
6357ef20d060SBarry Smith        in a time based line graph
6358ef20d060SBarry Smith 
6359ef20d060SBarry Smith    Collective on TS
6360ef20d060SBarry Smith 
6361ef20d060SBarry Smith    Input Parameters:
6362ef20d060SBarry Smith +  ts - the TS context
6363ef20d060SBarry Smith .  step - current time-step
6364ef20d060SBarry Smith .  ptime - current time
63657db568b7SBarry Smith .  u - current solution
63667db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6367ef20d060SBarry Smith 
6368ef20d060SBarry Smith    Level: intermediate
6369ef20d060SBarry Smith 
63706934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component errors in a separate window
6371abd5a294SJed Brown 
6372abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6373abd5a294SJed Brown 
6374abd5a294SJed Brown    Options Database Keys:
63754f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6376ef20d060SBarry Smith 
6377ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
6378ef20d060SBarry Smith 
6379abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6380ef20d060SBarry Smith @*/
63810910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6382ef20d060SBarry Smith {
6383ef20d060SBarry Smith   PetscErrorCode    ierr;
63840b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6385ef20d060SBarry Smith   const PetscScalar *yy;
6386ef20d060SBarry Smith   Vec               y;
6387ef20d060SBarry Smith 
6388ef20d060SBarry Smith   PetscFunctionBegin;
6389a9f9c1f6SBarry Smith   if (!step) {
6390a9f9c1f6SBarry Smith     PetscDrawAxis axis;
63916934998bSLisandro Dalcin     PetscInt      dim;
6392a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
63931ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
63940910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6395a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6396a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6397a9f9c1f6SBarry Smith   }
63980910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6399ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
64000910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6401ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6402e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6403e3efe391SJed Brown   {
6404e3efe391SJed Brown     PetscReal *yreal;
6405e3efe391SJed Brown     PetscInt  i,n;
6406e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6407785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6408e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
64090b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6410e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6411e3efe391SJed Brown   }
6412e3efe391SJed Brown #else
64130b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6414e3efe391SJed Brown #endif
6415ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6416ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6417b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
64180b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
64196934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
64203923b477SBarry Smith   }
6421ef20d060SBarry Smith   PetscFunctionReturn(0);
6422ef20d060SBarry Smith }
6423ef20d060SBarry Smith 
6424201da799SBarry Smith #undef __FUNCT__
6425201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
6426201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6427201da799SBarry Smith {
6428201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6429201da799SBarry Smith   PetscReal      x   = ptime,y;
6430201da799SBarry Smith   PetscErrorCode ierr;
6431201da799SBarry Smith   PetscInt       its;
6432201da799SBarry Smith 
6433201da799SBarry Smith   PetscFunctionBegin;
643463e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6435201da799SBarry Smith   if (!n) {
6436201da799SBarry Smith     PetscDrawAxis axis;
6437201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6438201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
6439201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6440201da799SBarry Smith     ctx->snes_its = 0;
6441201da799SBarry Smith   }
6442201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
6443201da799SBarry Smith   y    = its - ctx->snes_its;
6444201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
64453fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6446201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
64476934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6448201da799SBarry Smith   }
6449201da799SBarry Smith   ctx->snes_its = its;
6450201da799SBarry Smith   PetscFunctionReturn(0);
6451201da799SBarry Smith }
6452201da799SBarry Smith 
6453201da799SBarry Smith #undef __FUNCT__
6454201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
6455201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6456201da799SBarry Smith {
6457201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6458201da799SBarry Smith   PetscReal      x   = ptime,y;
6459201da799SBarry Smith   PetscErrorCode ierr;
6460201da799SBarry Smith   PetscInt       its;
6461201da799SBarry Smith 
6462201da799SBarry Smith   PetscFunctionBegin;
646363e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6464201da799SBarry Smith   if (!n) {
6465201da799SBarry Smith     PetscDrawAxis axis;
6466201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6467201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
6468201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6469201da799SBarry Smith     ctx->ksp_its = 0;
6470201da799SBarry Smith   }
6471201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
6472201da799SBarry Smith   y    = its - ctx->ksp_its;
6473201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
647499fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6475201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
64766934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6477201da799SBarry Smith   }
6478201da799SBarry Smith   ctx->ksp_its = its;
6479201da799SBarry Smith   PetscFunctionReturn(0);
6480201da799SBarry Smith }
6481f9c1d6abSBarry Smith 
6482f9c1d6abSBarry Smith #undef __FUNCT__
6483f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
6484f9c1d6abSBarry Smith /*@
6485f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
6486f9c1d6abSBarry Smith 
6487f9c1d6abSBarry Smith    Collective on TS and Vec
6488f9c1d6abSBarry Smith 
6489f9c1d6abSBarry Smith    Input Parameters:
6490f9c1d6abSBarry Smith +  ts - the TS context
6491f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
6492f9c1d6abSBarry Smith 
6493f9c1d6abSBarry Smith    Output Parameters:
6494f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
6495f9c1d6abSBarry Smith 
6496f9c1d6abSBarry Smith    Level: developer
6497f9c1d6abSBarry Smith 
6498f9c1d6abSBarry Smith .keywords: TS, compute
6499f9c1d6abSBarry Smith 
6500f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
6501f9c1d6abSBarry Smith @*/
6502f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
6503f9c1d6abSBarry Smith {
6504f9c1d6abSBarry Smith   PetscErrorCode ierr;
6505f9c1d6abSBarry Smith 
6506f9c1d6abSBarry Smith   PetscFunctionBegin;
6507f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6508ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
6509f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
6510f9c1d6abSBarry Smith   PetscFunctionReturn(0);
6511f9c1d6abSBarry Smith }
651224655328SShri 
6513b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
6514b3d3934dSBarry Smith #undef __FUNCT__
6515b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate"
6516b3d3934dSBarry Smith /*@C
6517b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
6518b3d3934dSBarry Smith 
6519b3d3934dSBarry Smith    Collective on TS
6520b3d3934dSBarry Smith 
6521b3d3934dSBarry Smith    Input Parameters:
6522b3d3934dSBarry Smith .  ts  - the ODE solver object
6523b3d3934dSBarry Smith 
6524b3d3934dSBarry Smith    Output Parameter:
6525b3d3934dSBarry Smith .  ctx - the context
6526b3d3934dSBarry Smith 
6527b3d3934dSBarry Smith    Level: intermediate
6528b3d3934dSBarry Smith 
6529b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
6530b3d3934dSBarry Smith 
6531b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
6532b3d3934dSBarry Smith 
6533b3d3934dSBarry Smith @*/
6534b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
6535b3d3934dSBarry Smith {
6536b3d3934dSBarry Smith   PetscErrorCode ierr;
6537b3d3934dSBarry Smith 
6538b3d3934dSBarry Smith   PetscFunctionBegin;
6539a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
6540b3d3934dSBarry Smith   PetscFunctionReturn(0);
6541b3d3934dSBarry Smith }
6542b3d3934dSBarry Smith 
6543b3d3934dSBarry Smith #undef __FUNCT__
6544b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope"
6545b3d3934dSBarry Smith /*@C
6546b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
6547b3d3934dSBarry Smith 
6548b3d3934dSBarry Smith    Collective on TS
6549b3d3934dSBarry Smith 
6550b3d3934dSBarry Smith    Input Parameters:
6551b3d3934dSBarry Smith +  ts - the TS context
6552b3d3934dSBarry Smith .  step - current time-step
6553b3d3934dSBarry Smith .  ptime - current time
65547db568b7SBarry Smith .  u  - current solution
65557db568b7SBarry Smith -  dctx - the envelope context
6556b3d3934dSBarry Smith 
6557b3d3934dSBarry Smith    Options Database:
6558b3d3934dSBarry Smith .  -ts_monitor_envelope
6559b3d3934dSBarry Smith 
6560b3d3934dSBarry Smith    Level: intermediate
6561b3d3934dSBarry Smith 
6562b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
6563b3d3934dSBarry Smith 
6564b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6565b3d3934dSBarry Smith 
65667db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
6567b3d3934dSBarry Smith @*/
65687db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6569b3d3934dSBarry Smith {
6570b3d3934dSBarry Smith   PetscErrorCode       ierr;
65717db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
6572b3d3934dSBarry Smith 
6573b3d3934dSBarry Smith   PetscFunctionBegin;
6574b3d3934dSBarry Smith   if (!ctx->max) {
6575b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
6576b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
6577b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
6578b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
6579b3d3934dSBarry Smith   } else {
6580b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
6581b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
6582b3d3934dSBarry Smith   }
6583b3d3934dSBarry Smith   PetscFunctionReturn(0);
6584b3d3934dSBarry Smith }
6585b3d3934dSBarry Smith 
6586b3d3934dSBarry Smith 
6587b3d3934dSBarry Smith #undef __FUNCT__
6588b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds"
6589b3d3934dSBarry Smith /*@C
6590b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
6591b3d3934dSBarry Smith 
6592b3d3934dSBarry Smith    Collective on TS
6593b3d3934dSBarry Smith 
6594b3d3934dSBarry Smith    Input Parameter:
6595b3d3934dSBarry Smith .  ts - the TS context
6596b3d3934dSBarry Smith 
6597b3d3934dSBarry Smith    Output Parameter:
6598b3d3934dSBarry Smith +  max - the maximum values
6599b3d3934dSBarry Smith -  min - the minimum values
6600b3d3934dSBarry Smith 
66017db568b7SBarry Smith    Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
66027db568b7SBarry Smith 
6603b3d3934dSBarry Smith    Level: intermediate
6604b3d3934dSBarry Smith 
6605b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6606b3d3934dSBarry Smith 
6607b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6608b3d3934dSBarry Smith @*/
6609b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
6610b3d3934dSBarry Smith {
6611b3d3934dSBarry Smith   PetscInt i;
6612b3d3934dSBarry Smith 
6613b3d3934dSBarry Smith   PetscFunctionBegin;
6614b3d3934dSBarry Smith   if (max) *max = NULL;
6615b3d3934dSBarry Smith   if (min) *min = NULL;
6616b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6617b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
66185537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
6619b3d3934dSBarry Smith       if (max) *max = ctx->max;
6620b3d3934dSBarry Smith       if (min) *min = ctx->min;
6621b3d3934dSBarry Smith       break;
6622b3d3934dSBarry Smith     }
6623b3d3934dSBarry Smith   }
6624b3d3934dSBarry Smith   PetscFunctionReturn(0);
6625b3d3934dSBarry Smith }
6626b3d3934dSBarry Smith 
6627b3d3934dSBarry Smith #undef __FUNCT__
6628b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy"
6629b3d3934dSBarry Smith /*@C
6630b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
6631b3d3934dSBarry Smith 
6632b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
6633b3d3934dSBarry Smith 
6634b3d3934dSBarry Smith    Input Parameter:
6635b3d3934dSBarry Smith .  ctx - the monitor context
6636b3d3934dSBarry Smith 
6637b3d3934dSBarry Smith    Level: intermediate
6638b3d3934dSBarry Smith 
6639b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
6640b3d3934dSBarry Smith 
66417db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
6642b3d3934dSBarry Smith @*/
6643b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
6644b3d3934dSBarry Smith {
6645b3d3934dSBarry Smith   PetscErrorCode ierr;
6646b3d3934dSBarry Smith 
6647b3d3934dSBarry Smith   PetscFunctionBegin;
6648b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
6649b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
6650b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
6651b3d3934dSBarry Smith   PetscFunctionReturn(0);
6652b3d3934dSBarry Smith }
6653f2dee214SBarry Smith 
665424655328SShri #undef __FUNCT__
665524655328SShri #define __FUNCT__ "TSRollBack"
665624655328SShri /*@
665724655328SShri    TSRollBack - Rolls back one time step
665824655328SShri 
665924655328SShri    Collective on TS
666024655328SShri 
666124655328SShri    Input Parameter:
666224655328SShri .  ts - the TS context obtained from TSCreate()
666324655328SShri 
666424655328SShri    Level: advanced
666524655328SShri 
666624655328SShri .keywords: TS, timestep, rollback
666724655328SShri 
666824655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
666924655328SShri @*/
667024655328SShri PetscErrorCode  TSRollBack(TS ts)
667124655328SShri {
667224655328SShri   PetscErrorCode ierr;
667324655328SShri 
667424655328SShri   PetscFunctionBegin;
667524655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
667624655328SShri 
667724655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
667824655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
667924655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
668024655328SShri   ts->ptime = ts->ptime_prev;
66818392e04aSShri Abhyankar   ts->steprollback = PETSC_TRUE; /* Flag to indicate that the step is rollbacked */
668224655328SShri   PetscFunctionReturn(0);
668324655328SShri }
6684aeb4809dSShri Abhyankar 
6685ff22ae23SHong Zhang #undef __FUNCT__
6686ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
6687ff22ae23SHong Zhang /*@
6688ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
6689ff22ae23SHong Zhang 
6690ff22ae23SHong Zhang    Input Parameter:
6691ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
6692ff22ae23SHong Zhang 
6693ff22ae23SHong Zhang    Level: advanced
6694ff22ae23SHong Zhang 
6695ff22ae23SHong Zhang .keywords: TS, getstages
6696ff22ae23SHong Zhang 
6697ff22ae23SHong Zhang .seealso: TSCreate()
6698ff22ae23SHong Zhang @*/
6699ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns, Vec **Y)
6700ff22ae23SHong Zhang {
6701ff22ae23SHong Zhang   PetscErrorCode ierr;
6702ff22ae23SHong Zhang 
6703ff22ae23SHong Zhang   PetscFunctionBegin;
6704ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
6705ff22ae23SHong Zhang   PetscValidPointer(ns,2);
6706ff22ae23SHong Zhang 
6707ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
6708ff22ae23SHong Zhang   else {
6709ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
6710ff22ae23SHong Zhang   }
6711ff22ae23SHong Zhang   PetscFunctionReturn(0);
6712ff22ae23SHong Zhang }
6713ff22ae23SHong Zhang 
6714847ff0e1SMatthew G. Knepley #undef __FUNCT__
6715847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor"
6716847ff0e1SMatthew G. Knepley /*@C
6717847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
6718847ff0e1SMatthew G. Knepley 
6719847ff0e1SMatthew G. Knepley   Collective on SNES
6720847ff0e1SMatthew G. Knepley 
6721847ff0e1SMatthew G. Knepley   Input Parameters:
6722847ff0e1SMatthew G. Knepley + ts - the TS context
6723847ff0e1SMatthew G. Knepley . t - current timestep
6724847ff0e1SMatthew G. Knepley . U - state vector
6725847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
6726847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
6727847ff0e1SMatthew G. Knepley - ctx - an optional user context
6728847ff0e1SMatthew G. Knepley 
6729847ff0e1SMatthew G. Knepley   Output Parameters:
6730847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
6731847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
6732847ff0e1SMatthew G. Knepley 
6733847ff0e1SMatthew G. Knepley   Level: intermediate
6734847ff0e1SMatthew G. Knepley 
6735847ff0e1SMatthew G. Knepley   Notes:
6736847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
6737847ff0e1SMatthew G. Knepley 
6738847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
6739847ff0e1SMatthew G. Knepley 
6740847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
6741847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
6742847ff0e1SMatthew G. Knepley 
6743847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
6744847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
6745847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
6746847ff0e1SMatthew G. Knepley 
6747847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
6748847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
6749847ff0e1SMatthew G. Knepley @*/
6750847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
6751847ff0e1SMatthew G. Knepley {
6752847ff0e1SMatthew G. Knepley   SNES           snes;
6753847ff0e1SMatthew G. Knepley   MatFDColoring  color;
6754847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
6755847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
6756847ff0e1SMatthew G. Knepley 
6757847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
6758c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
6759847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
6760847ff0e1SMatthew G. Knepley   if (!color) {
6761847ff0e1SMatthew G. Knepley     DM         dm;
6762847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
6763847ff0e1SMatthew G. Knepley 
6764847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
6765847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
6766847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
6767847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
6768847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6769847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6770847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6771847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6772847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6773847ff0e1SMatthew G. Knepley     } else {
6774847ff0e1SMatthew G. Knepley       MatColoring mc;
6775847ff0e1SMatthew G. Knepley 
6776847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
6777847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
6778847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
6779847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
6780847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
6781847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
6782847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6783847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6784847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6785847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6786847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6787847ff0e1SMatthew G. Knepley     }
6788847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
6789847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
6790847ff0e1SMatthew G. Knepley   }
6791847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
6792847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
6793847ff0e1SMatthew G. Knepley   if (J != B) {
6794847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6795847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6796847ff0e1SMatthew G. Knepley   }
6797847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
6798847ff0e1SMatthew G. Knepley }
679993b34091SDebojyoti Ghosh 
680093b34091SDebojyoti Ghosh #undef __FUNCT__
6801cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSSetFunctionDomainError"
6802cb9d8021SPierre Barbier de Reuille /*@
6803cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
6804cb9d8021SPierre Barbier de Reuille 
6805cb9d8021SPierre Barbier de Reuille     Input Parameters:
6806cb9d8021SPierre Barbier de Reuille     ts - the TS context
6807cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
6808cb9d8021SPierre Barbier de Reuille 
6809cb9d8021SPierre Barbier de Reuille     Level: intermediate
6810cb9d8021SPierre Barbier de Reuille 
6811cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
6812cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
6813cb9d8021SPierre Barbier de Reuille @*/
6814cb9d8021SPierre Barbier de Reuille 
6815d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
6816cb9d8021SPierre Barbier de Reuille {
6817cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
6818cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
6819cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
6820cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
6821cb9d8021SPierre Barbier de Reuille }
6822cb9d8021SPierre Barbier de Reuille 
6823cb9d8021SPierre Barbier de Reuille #undef __FUNCT__
6824cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSFunctionDomainError"
6825cb9d8021SPierre Barbier de Reuille /*@
6826cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
6827cb9d8021SPierre Barbier de Reuille 
6828cb9d8021SPierre Barbier de Reuille     Input Parameters:
6829cb9d8021SPierre Barbier de Reuille     ts - the TS context
6830cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
6831d183316bSPierre Barbier de Reuille     Y - state vector to check.
6832cb9d8021SPierre Barbier de Reuille 
6833cb9d8021SPierre Barbier de Reuille     Output Parameter:
6834cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
6835cb9d8021SPierre Barbier de Reuille 
6836cb9d8021SPierre Barbier de Reuille     Note:
6837cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
6838cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
683996a0c994SBarry Smith 
684096a0c994SBarry Smith     Level: advanced
6841cb9d8021SPierre Barbier de Reuille @*/
6842d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
6843cb9d8021SPierre Barbier de Reuille {
6844cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
6845cb9d8021SPierre Barbier de Reuille 
6846cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
6847cb9d8021SPierre Barbier de Reuille 
6848cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6849cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
6850cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
6851d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
6852cb9d8021SPierre Barbier de Reuille   }
6853cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
6854cb9d8021SPierre Barbier de Reuille }
68551ceb14c0SBarry Smith 
68561ceb14c0SBarry Smith #undef  __FUNCT__
6857e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone"
685893b34091SDebojyoti Ghosh /*@C
6859e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
686093b34091SDebojyoti Ghosh 
686193b34091SDebojyoti Ghosh   Collective on MPI_Comm
686293b34091SDebojyoti Ghosh 
686393b34091SDebojyoti Ghosh   Input Parameter:
686493b34091SDebojyoti Ghosh . tsin    - The input TS
686593b34091SDebojyoti Ghosh 
686693b34091SDebojyoti Ghosh   Output Parameter:
6867e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
686893b34091SDebojyoti Ghosh 
68695eca1a21SEmil Constantinescu   Notes:
68705eca1a21SEmil 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.
68715eca1a21SEmil Constantinescu 
6872baa10174SEmil 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);
68735eca1a21SEmil Constantinescu 
68745eca1a21SEmil Constantinescu   Level: developer
687593b34091SDebojyoti Ghosh 
6876e5168f73SEmil Constantinescu .keywords: TS, clone
6877e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
687893b34091SDebojyoti Ghosh @*/
6879baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
688093b34091SDebojyoti Ghosh {
688193b34091SDebojyoti Ghosh   TS             t;
688293b34091SDebojyoti Ghosh   PetscErrorCode ierr;
6883dc846ba4SSatish Balay   SNES           snes_start;
6884dc846ba4SSatish Balay   DM             dm;
6885dc846ba4SSatish Balay   TSType         type;
688693b34091SDebojyoti Ghosh 
688793b34091SDebojyoti Ghosh   PetscFunctionBegin;
688893b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
688993b34091SDebojyoti Ghosh   *tsout = NULL;
689093b34091SDebojyoti Ghosh 
68917a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
689293b34091SDebojyoti Ghosh 
689393b34091SDebojyoti Ghosh   /* General TS description */
689493b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
689593b34091SDebojyoti Ghosh   t->setupcalled       = 0;
689693b34091SDebojyoti Ghosh   t->ksp_its           = 0;
689793b34091SDebojyoti Ghosh   t->snes_its          = 0;
689893b34091SDebojyoti Ghosh   t->nwork             = 0;
689993b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
690093b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
690193b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
690293b34091SDebojyoti Ghosh 
690334561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
690434561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
6905d15a3a53SEmil Constantinescu 
690693b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
690793b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
690893b34091SDebojyoti Ghosh 
690993b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
691051699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
691193b34091SDebojyoti Ghosh 
691293b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
691393b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
691493b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
691593b34091SDebojyoti Ghosh   t->time_step_orig    = tsin->time_step_orig;
691693b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
691793b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
691893b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
691993b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
692093b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
692193b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
692293b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
692393b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
692493b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
692593b34091SDebojyoti Ghosh 
692693b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
692793b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
692893b34091SDebojyoti Ghosh 
692993b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
693093b34091SDebojyoti Ghosh 
693193b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
693293b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
693393b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
693493b34091SDebojyoti Ghosh 
693593b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
693693b34091SDebojyoti Ghosh 
69370d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
69380d4fed19SBarry Smith     PetscInt i;
69390d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
69400d4fed19SBarry Smith     for (i=0; i<10; i++) {
69410d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
69420d4fed19SBarry Smith     }
69430d4fed19SBarry Smith   }
694493b34091SDebojyoti Ghosh   *tsout = t;
694593b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
694693b34091SDebojyoti Ghosh }
6947