xref: /petsc/src/ts/interface/ts.c (revision e193eaaf9b0fedba4e985fff87fadc3f92b562af)
1af0996ceSBarry Smith #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
2496e6a7aSJed Brown #include <petscdmshell.h>
31e25c274SJed Brown #include <petscdmda.h>
42d5ee99bSBarry Smith #include <petscviewer.h>
52d5ee99bSBarry Smith #include <petscdraw.h>
6d763cef2SBarry Smith 
7d5ba7fb7SMatthew Knepley /* Logging support */
8d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
98d0ad7a8SHong Zhang PetscLogEvent TS_AdjointStep, TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
10d405a339SMatthew Knepley 
11feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1249354f04SShri Abhyankar 
132d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx {
142d5ee99bSBarry Smith   PetscViewer   viewer;
152d5ee99bSBarry Smith   Vec           initialsolution;
162d5ee99bSBarry Smith   PetscBool     showinitial;
172d5ee99bSBarry Smith   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
182d5ee99bSBarry Smith   PetscBool     showtimestepandtime;
192d5ee99bSBarry Smith };
202d5ee99bSBarry Smith 
21fde5950dSBarry Smith /*@C
22fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
23fde5950dSBarry Smith 
24fde5950dSBarry Smith    Collective on TS
25fde5950dSBarry Smith 
26fde5950dSBarry Smith    Input Parameters:
27fde5950dSBarry Smith +  ts - TS object you wish to monitor
28fde5950dSBarry Smith .  name - the monitor type one is seeking
29fde5950dSBarry Smith .  help - message indicating what monitoring is done
30fde5950dSBarry Smith .  manual - manual page for the monitor
31fde5950dSBarry Smith .  monitor - the monitor function
32fde5950dSBarry 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
33fde5950dSBarry Smith 
34fde5950dSBarry Smith    Level: developer
35fde5950dSBarry Smith 
36fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
37fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
38fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
39fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
40fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
41fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
42fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
43fde5950dSBarry Smith @*/
44721cd6eeSBarry 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*))
45fde5950dSBarry Smith {
46fde5950dSBarry Smith   PetscErrorCode    ierr;
47fde5950dSBarry Smith   PetscViewer       viewer;
48fde5950dSBarry Smith   PetscViewerFormat format;
49fde5950dSBarry Smith   PetscBool         flg;
50fde5950dSBarry Smith 
51fde5950dSBarry Smith   PetscFunctionBegin;
52fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
53fde5950dSBarry Smith   if (flg) {
54721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
55721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
56721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
57fde5950dSBarry Smith     if (monitorsetup) {
58721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
59fde5950dSBarry Smith     }
60721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
61fde5950dSBarry Smith   }
62fde5950dSBarry Smith   PetscFunctionReturn(0);
63fde5950dSBarry Smith }
64fde5950dSBarry Smith 
65fde5950dSBarry Smith /*@C
66e875c4f7SBarry Smith    TSAdjointMonitorSensi - monitors the first lambda sensitivity
67287c2655SBarry Smith 
68287c2655SBarry Smith    Level: intermediate
69287c2655SBarry Smith 
70287c2655SBarry Smith .keywords: TS, set, monitor
71287c2655SBarry Smith 
72287c2655SBarry Smith .seealso: TSAdjointMonitorSet()
73287c2655SBarry Smith @*/
74f4c7b390SBarry Smith PetscErrorCode TSAdjointMonitorSensi(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
75287c2655SBarry Smith {
76287c2655SBarry Smith   PetscErrorCode ierr;
77287c2655SBarry Smith   PetscViewer    viewer = vf->viewer;
78287c2655SBarry Smith 
79287c2655SBarry Smith   PetscFunctionBegin;
80287c2655SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
81287c2655SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
82287c2655SBarry Smith   ierr = VecView(lambda[0],viewer);CHKERRQ(ierr);
83287c2655SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
84287c2655SBarry Smith   PetscFunctionReturn(0);
85287c2655SBarry Smith }
86287c2655SBarry Smith 
87287c2655SBarry Smith /*@C
88fde5950dSBarry Smith    TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
89fde5950dSBarry Smith 
90fde5950dSBarry Smith    Collective on TS
91fde5950dSBarry Smith 
92fde5950dSBarry Smith    Input Parameters:
93fde5950dSBarry Smith +  ts - TS object you wish to monitor
94fde5950dSBarry Smith .  name - the monitor type one is seeking
95fde5950dSBarry Smith .  help - message indicating what monitoring is done
96fde5950dSBarry Smith .  manual - manual page for the monitor
97fde5950dSBarry Smith .  monitor - the monitor function
98fde5950dSBarry 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
99fde5950dSBarry Smith 
100fde5950dSBarry Smith    Level: developer
101fde5950dSBarry Smith 
102fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
103fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
104fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
105fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
106fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
107fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
108fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
109fde5950dSBarry Smith @*/
110721cd6eeSBarry 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*))
111fde5950dSBarry Smith {
112fde5950dSBarry Smith   PetscErrorCode    ierr;
113fde5950dSBarry Smith   PetscViewer       viewer;
114fde5950dSBarry Smith   PetscViewerFormat format;
115fde5950dSBarry Smith   PetscBool         flg;
116fde5950dSBarry Smith 
117fde5950dSBarry Smith   PetscFunctionBegin;
118fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
119fde5950dSBarry Smith   if (flg) {
120721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
121721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
122721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
123fde5950dSBarry Smith     if (monitorsetup) {
124721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
125fde5950dSBarry Smith     }
126721cd6eeSBarry Smith     ierr = TSAdjointMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
127fde5950dSBarry Smith   }
128fde5950dSBarry Smith   PetscFunctionReturn(0);
129fde5950dSBarry Smith }
130fde5950dSBarry Smith 
1312ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
1322ffb9264SLisandro Dalcin {
1332ffb9264SLisandro Dalcin   PetscErrorCode ierr;
1342ffb9264SLisandro Dalcin 
1352ffb9264SLisandro Dalcin   PetscFunctionBegin;
136b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
137b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
1382ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
1392ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
1402ffb9264SLisandro Dalcin   }
1412ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
1422ffb9264SLisandro Dalcin }
1432ffb9264SLisandro Dalcin 
144bdad233fSMatthew Knepley /*@
145bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
146bdad233fSMatthew Knepley 
147bdad233fSMatthew Knepley    Collective on TS
148bdad233fSMatthew Knepley 
149bdad233fSMatthew Knepley    Input Parameter:
150bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
151bdad233fSMatthew Knepley 
152bdad233fSMatthew Knepley    Options Database Keys:
153b6a60446SDebojyoti Ghosh +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE
154ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
155ef85077eSLisandro Dalcin .  -ts_max_time <time> - maximum time to compute to
156ef85077eSLisandro Dalcin .  -ts_max_steps <steps> - maximum number of time-steps to take
157ef85077eSLisandro Dalcin .  -ts_init_time <time> - initial time to start computation
158ef85077eSLisandro Dalcin .  -ts_final_time <time> - final time to compute to
1593e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
160a3cdaa26SBarry 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
161a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
162a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
163a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
164a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
165a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
166ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
167847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
168bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
169de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
170de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1716934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
172de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
173de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
174de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
175de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1763e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1773e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
178fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
179e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
180110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
181110eb670SHong Zhang .  -ts_adjoint_monitor - print information at each adjoint time step
18253ea634cSHong Zhang -  -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically
18353ea634cSHong Zhang 
18491b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
185bdad233fSMatthew Knepley 
186bdad233fSMatthew Knepley    Level: beginner
187bdad233fSMatthew Knepley 
188bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
189bdad233fSMatthew Knepley 
190a313700dSBarry Smith .seealso: TSGetType()
191bdad233fSMatthew Knepley @*/
1927087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
193bdad233fSMatthew Knepley {
194bc952696SBarry Smith   PetscBool              opt,flg,tflg;
195dfbe8321SBarry Smith   PetscErrorCode         ierr;
196eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
19731748224SBarry Smith   PetscReal              time_step;
19849354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
199d1212d36SBarry Smith   char                   dir[16];
200cd11d68dSLisandro Dalcin   TSIFunction            ifun;
2016991f827SBarry Smith   const char             *defaultType;
2026991f827SBarry Smith   char                   typeName[256];
203bdad233fSMatthew Knepley 
204bdad233fSMatthew Knepley   PetscFunctionBegin;
2050700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2066991f827SBarry Smith 
2076991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
208cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
209cd11d68dSLisandro Dalcin 
210cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
211cd11d68dSLisandro Dalcin   if (((PetscObject)ts)->type_name)
212cd11d68dSLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
213cd11d68dSLisandro Dalcin   else
214cd11d68dSLisandro Dalcin     defaultType = ifun ? TSBEULER : TSEULER;
2156991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
2166991f827SBarry Smith   if (opt) {
2176991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
2186991f827SBarry Smith   } else {
2196991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
2206991f827SBarry Smith   }
221bdad233fSMatthew Knepley 
222bdad233fSMatthew Knepley   /* Handle generic TS options */
223ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
22419eac22cSLisandro Dalcin   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
2250298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
226ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_final_time","Final time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
22731748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
228cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
22949354f04SShri 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);
23049354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
2310298fd71SBarry 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);
2320298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
2330298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
2340298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
2350298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
236bdad233fSMatthew Knepley 
23756f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
23856f85f32SBarry Smith   {
23956f85f32SBarry Smith   PetscBool set;
24056f85f32SBarry Smith   flg  = PETSC_FALSE;
24156f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
24256f85f32SBarry Smith   if (set) {
24356f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
24456f85f32SBarry Smith   }
24556f85f32SBarry Smith   }
24656f85f32SBarry Smith #endif
24756f85f32SBarry Smith 
248bdad233fSMatthew Knepley   /* Monitor options */
249cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
250fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
251fde5950dSBarry Smith   ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr);
252e875c4f7SBarry Smith   ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor_sensi","Monitor sensitivity in the adjoint computation","TSAdjointMonitorSensi",TSAdjointMonitorSensi,NULL);CHKERRQ(ierr);
253fde5950dSBarry Smith 
2545180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2555180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
2565180491cSLisandro Dalcin 
2574f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
258b3603a34SBarry Smith   if (opt) {
2590b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2603923b477SBarry Smith     PetscInt       howoften = 1;
261b3603a34SBarry Smith 
2620298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2636ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2644f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
265bdad233fSMatthew Knepley   }
2666ba87a44SLisandro Dalcin 
2674f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
268ef20d060SBarry Smith   if (opt) {
2690b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2703923b477SBarry Smith     PetscInt       howoften = 1;
271ef20d060SBarry Smith 
2720298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
2736ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2744f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
275ef20d060SBarry Smith   }
2766934998bSLisandro Dalcin 
2776934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2786934998bSLisandro Dalcin   if (opt) {
2796934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2806934998bSLisandro Dalcin     PetscInt       howoften = 1;
2816934998bSLisandro Dalcin 
2826934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2836ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2846934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2856934998bSLisandro Dalcin   }
286201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
287201da799SBarry Smith   if (opt) {
288201da799SBarry Smith     TSMonitorLGCtx ctx;
289201da799SBarry Smith     PetscInt       howoften = 1;
290201da799SBarry Smith 
2910298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2926ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
293201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
294201da799SBarry Smith   }
295201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
296201da799SBarry Smith   if (opt) {
297201da799SBarry Smith     TSMonitorLGCtx ctx;
298201da799SBarry Smith     PetscInt       howoften = 1;
299201da799SBarry Smith 
3000298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
3016ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
302201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
303201da799SBarry Smith   }
3048189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
3058189c53fSBarry Smith   if (opt) {
3068189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
3078189c53fSBarry Smith     PetscInt          howoften = 1;
3088189c53fSBarry Smith 
3090298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
3106934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3118189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
3128189c53fSBarry Smith   }
313ef20d060SBarry Smith   opt  = PETSC_FALSE;
3140dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
315a7cc72afSBarry Smith   if (opt) {
31683a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
31783a4ac43SBarry Smith     PetscInt         howoften = 1;
318a80ad3e0SBarry Smith 
3190298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
3206934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
32183a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
322bdad233fSMatthew Knepley   }
323fb1732b5SBarry Smith   opt  = PETSC_FALSE;
3249110b2e7SHong Zhang   ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr);
3259110b2e7SHong Zhang   if (opt) {
3269110b2e7SHong Zhang     TSMonitorDrawCtx ctx;
3279110b2e7SHong Zhang     PetscInt         howoften = 1;
3289110b2e7SHong Zhang 
3299110b2e7SHong Zhang     ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr);
3306934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3319110b2e7SHong Zhang     ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3329110b2e7SHong Zhang   }
3339110b2e7SHong Zhang   opt  = PETSC_FALSE;
3342d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
3352d5ee99bSBarry Smith   if (opt) {
3362d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
3372d5ee99bSBarry Smith     PetscReal        bounds[4];
3382d5ee99bSBarry Smith     PetscInt         n = 4;
3392d5ee99bSBarry Smith     PetscDraw        draw;
3406934998bSLisandro Dalcin     PetscDrawAxis    axis;
3412d5ee99bSBarry Smith 
3422d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
3432d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
3446934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
3452d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
3466934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
3476934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
3486934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
3492d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3502d5ee99bSBarry Smith   }
3512d5ee99bSBarry Smith   opt  = PETSC_FALSE;
3520dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
3533a471f94SBarry Smith   if (opt) {
35483a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
35583a4ac43SBarry Smith     PetscInt         howoften = 1;
3563a471f94SBarry Smith 
3570298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
3586934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
35983a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3603a471f94SBarry Smith   }
361fde5950dSBarry Smith 
362ed81e22dSJed Brown   opt  = PETSC_FALSE;
36391b97e58SBarry 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);
364ed81e22dSJed Brown   if (flg) {
365ed81e22dSJed Brown     const char *ptr,*ptr2;
366ed81e22dSJed Brown     char       *filetemplate;
367ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
368ed81e22dSJed Brown     /* Do some cursory validation of the input. */
369ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
370ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
371ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
372ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
373ce94432eSBarry 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");
374ed81e22dSJed Brown       if (ptr2) break;
375ed81e22dSJed Brown     }
376ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
377ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
378ed81e22dSJed Brown   }
379bdad233fSMatthew Knepley 
380d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
381d1212d36SBarry Smith   if (flg) {
382d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
383d1212d36SBarry Smith     int                  ray = 0;
384d1212d36SBarry Smith     DMDADirection        ddir;
385d1212d36SBarry Smith     DM                   da;
386d1212d36SBarry Smith     PetscMPIInt          rank;
387d1212d36SBarry Smith 
388ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
389d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
390d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
391ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
392d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
393d1212d36SBarry Smith 
394d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
395b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
396d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
397d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
398ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
399d1212d36SBarry Smith     if (!rank) {
400d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
401d1212d36SBarry Smith     }
40251b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
403d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
404d1212d36SBarry Smith   }
40551b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
40651b4a12fSMatthew G. Knepley   if (flg) {
40751b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
40851b4a12fSMatthew G. Knepley     int                 ray = 0;
40951b4a12fSMatthew G. Knepley     DMDADirection       ddir;
41051b4a12fSMatthew G. Knepley     DM                  da;
41151b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
412d1212d36SBarry Smith 
41351b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
41451b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
41551b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
41651b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
41751b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
4181c3436cfSJed Brown 
41951b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
420b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
42151b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
42251b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
42351b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
42451b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
42551b4a12fSMatthew G. Knepley   }
426a7a1495cSBarry Smith 
427b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
428b3d3934dSBarry Smith   if (opt) {
429b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
430b3d3934dSBarry Smith 
431b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
432b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
433b3d3934dSBarry Smith   }
434b3d3934dSBarry Smith 
435847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
436847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
437847ff0e1SMatthew G. Knepley   if (flg) {
438847ff0e1SMatthew G. Knepley     DM   dm;
439847ff0e1SMatthew G. Knepley     DMTS tdm;
440847ff0e1SMatthew G. Knepley 
441847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
442847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
443847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
444847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
445847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
446847ff0e1SMatthew G. Knepley   }
447847ff0e1SMatthew G. Knepley 
448d763cef2SBarry Smith   /* Handle specific TS options */
449d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4506991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
451d763cef2SBarry Smith   }
452fbc52257SHong Zhang 
453a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
454a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
455a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
456a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
457a7bdc993SLisandro Dalcin 
45868bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4594f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
46068bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
46168bece0bSHong Zhang   if (tflg) {
46268bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
46368bece0bSHong Zhang   }
4644f122a70SLisandro Dalcin   tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE;
46568bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr);
46668bece0bSHong Zhang   if (flg) {
46768bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
46868bece0bSHong Zhang     ts->adjoint_solve = tflg;
46968bece0bSHong Zhang   }
470d763cef2SBarry Smith 
471d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4720633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
473fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
474d763cef2SBarry Smith 
4754f122a70SLisandro Dalcin   if (ts->trajectory) {
4764f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
477d763cef2SBarry Smith   }
47868bece0bSHong Zhang 
4794f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4804f122a70SLisandro Dalcin   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);}
4814f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
482d763cef2SBarry Smith   PetscFunctionReturn(0);
483d763cef2SBarry Smith }
484d763cef2SBarry Smith 
485d2daff3dSHong Zhang /*@
48678fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
48778fbdcc8SBarry Smith 
48878fbdcc8SBarry Smith    Collective on TS
48978fbdcc8SBarry Smith 
49078fbdcc8SBarry Smith    Input Parameters:
49178fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
49278fbdcc8SBarry Smith 
49378fbdcc8SBarry Smith    Output Parameters;
49478fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
49578fbdcc8SBarry Smith 
49678fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
49778fbdcc8SBarry Smith 
49878fbdcc8SBarry Smith    Level: advanced
49978fbdcc8SBarry Smith 
50078fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
50178fbdcc8SBarry Smith 
50278fbdcc8SBarry Smith .keywords: TS, set, checkpoint,
50378fbdcc8SBarry Smith @*/
50478fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
50578fbdcc8SBarry Smith {
50678fbdcc8SBarry Smith   PetscFunctionBegin;
50778fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50878fbdcc8SBarry Smith   *tr = ts->trajectory;
50978fbdcc8SBarry Smith   PetscFunctionReturn(0);
51078fbdcc8SBarry Smith }
51178fbdcc8SBarry Smith 
51278fbdcc8SBarry Smith /*@
513bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
514d2daff3dSHong Zhang 
515d2daff3dSHong Zhang    Collective on TS
516d2daff3dSHong Zhang 
517d2daff3dSHong Zhang    Input Parameters:
518bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
519bc952696SBarry Smith 
52078fbdcc8SBarry Smith    Options Database:
52178fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
52278fbdcc8SBarry Smith -  -ts_trajectory_type type
52378fbdcc8SBarry Smith 
52468bece0bSHong Zhang Note: This routine should be called after all TS options have been set
525d2daff3dSHong Zhang 
52678fbdcc8SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/bin/PetscBinaryIOTrajectory.py and
52778fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
52878fbdcc8SBarry Smith 
529d2daff3dSHong Zhang    Level: intermediate
530d2daff3dSHong Zhang 
53178fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectoryType, TSSetTrajectoryType()
532d2daff3dSHong Zhang 
533bc952696SBarry Smith .keywords: TS, set, checkpoint,
534d2daff3dSHong Zhang @*/
535bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
536d2daff3dSHong Zhang {
537bc952696SBarry Smith   PetscErrorCode ierr;
538bc952696SBarry Smith 
539d2daff3dSHong Zhang   PetscFunctionBegin;
540d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
541bc952696SBarry Smith   if (!ts->trajectory) {
542bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
543972caf09SHong Zhang     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
544bc952696SBarry Smith   }
545d2daff3dSHong Zhang   PetscFunctionReturn(0);
546d2daff3dSHong Zhang }
547d2daff3dSHong Zhang 
548a7a1495cSBarry Smith /*@
549a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
550a7a1495cSBarry Smith       set with TSSetRHSJacobian().
551a7a1495cSBarry Smith 
552a7a1495cSBarry Smith    Collective on TS and Vec
553a7a1495cSBarry Smith 
554a7a1495cSBarry Smith    Input Parameters:
555316643e7SJed Brown +  ts - the TS context
556a7a1495cSBarry Smith .  t - current timestep
5570910c330SBarry Smith -  U - input vector
558a7a1495cSBarry Smith 
559a7a1495cSBarry Smith    Output Parameters:
560a7a1495cSBarry Smith +  A - Jacobian matrix
561a7a1495cSBarry Smith .  B - optional preconditioning matrix
562a7a1495cSBarry Smith -  flag - flag indicating matrix structure
563a7a1495cSBarry Smith 
564a7a1495cSBarry Smith    Notes:
565a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
566a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
567a7a1495cSBarry Smith 
56894b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
569a7a1495cSBarry Smith    flag parameter.
570a7a1495cSBarry Smith 
571a7a1495cSBarry Smith    Level: developer
572a7a1495cSBarry Smith 
573a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
574a7a1495cSBarry Smith 
57594b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
576a7a1495cSBarry Smith @*/
577d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
578a7a1495cSBarry Smith {
579dfbe8321SBarry Smith   PetscErrorCode   ierr;
580270bf2e7SJed Brown   PetscObjectState Ustate;
5816c1e1eecSBarry Smith   PetscObjectId    Uid;
58224989b8cSPeter Brune   DM               dm;
583942e3340SBarry Smith   DMTS             tsdm;
58424989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
58524989b8cSPeter Brune   void             *ctx;
58624989b8cSPeter Brune   TSIJacobian      ijacobianfunc;
587b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
588a7a1495cSBarry Smith 
589a7a1495cSBarry Smith   PetscFunctionBegin;
5900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5910910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5920910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
59324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
594942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
59524989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5960298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
597b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
59859e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5996c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
6006c1e1eecSBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
6010e4ef248SJed Brown     PetscFunctionReturn(0);
602f8ede8e7SJed Brown   }
603d90be118SSean Farley 
604ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
605d90be118SSean Farley 
606e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
60794ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
60894ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
60994ab13aaSBarry Smith     if (A != B) {
61094ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
61194ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
612e1244c69SJed Brown     }
613e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
614e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
615e1244c69SJed Brown   }
616e1244c69SJed Brown 
61724989b8cSPeter Brune   if (rhsjacobianfunc) {
6186cd88445SBarry Smith     PetscBool missing;
61994ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
620a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
621d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
622a7a1495cSBarry Smith     PetscStackPop;
62394ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
6246cd88445SBarry Smith     if (A) {
6256cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
6266cd88445SBarry 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");
6276cd88445SBarry Smith     }
6286cd88445SBarry Smith     if (B && B != A) {
6296cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
6306cd88445SBarry 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");
6316cd88445SBarry Smith     }
632ef66eb69SBarry Smith   } else {
63394ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
63494ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
635ef66eb69SBarry Smith   }
6360e4ef248SJed Brown   ts->rhsjacobian.time       = t;
6377f79407eSBarry Smith   ierr                       = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
63859e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
639a7a1495cSBarry Smith   PetscFunctionReturn(0);
640a7a1495cSBarry Smith }
641a7a1495cSBarry Smith 
642316643e7SJed Brown /*@
643d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
644d763cef2SBarry Smith 
645316643e7SJed Brown    Collective on TS and Vec
646316643e7SJed Brown 
647316643e7SJed Brown    Input Parameters:
648316643e7SJed Brown +  ts - the TS context
649316643e7SJed Brown .  t - current time
6500910c330SBarry Smith -  U - state vector
651316643e7SJed Brown 
652316643e7SJed Brown    Output Parameter:
653316643e7SJed Brown .  y - right hand side
654316643e7SJed Brown 
655316643e7SJed Brown    Note:
656316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
657316643e7SJed Brown    is used internally within the nonlinear solvers.
658316643e7SJed Brown 
659316643e7SJed Brown    Level: developer
660316643e7SJed Brown 
661316643e7SJed Brown .keywords: TS, compute
662316643e7SJed Brown 
663316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
664316643e7SJed Brown @*/
6650910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
666d763cef2SBarry Smith {
667dfbe8321SBarry Smith   PetscErrorCode ierr;
66824989b8cSPeter Brune   TSRHSFunction  rhsfunction;
66924989b8cSPeter Brune   TSIFunction    ifunction;
67024989b8cSPeter Brune   void           *ctx;
67124989b8cSPeter Brune   DM             dm;
67224989b8cSPeter Brune 
673d763cef2SBarry Smith   PetscFunctionBegin;
6740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6750910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6760700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
67724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
67824989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6790298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
680d763cef2SBarry Smith 
681ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
682d763cef2SBarry Smith 
6830910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
68424989b8cSPeter Brune   if (rhsfunction) {
685d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6860910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
687d763cef2SBarry Smith     PetscStackPop;
688214bc6a2SJed Brown   } else {
689214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
690b2cd27e8SJed Brown   }
69144a41b28SSean Farley 
6920910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
693d763cef2SBarry Smith   PetscFunctionReturn(0);
694d763cef2SBarry Smith }
695d763cef2SBarry Smith 
696ef20d060SBarry Smith /*@
697ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
698ef20d060SBarry Smith 
699ef20d060SBarry Smith    Collective on TS and Vec
700ef20d060SBarry Smith 
701ef20d060SBarry Smith    Input Parameters:
702ef20d060SBarry Smith +  ts - the TS context
703ef20d060SBarry Smith -  t - current time
704ef20d060SBarry Smith 
705ef20d060SBarry Smith    Output Parameter:
7060910c330SBarry Smith .  U - the solution
707ef20d060SBarry Smith 
708ef20d060SBarry Smith    Note:
709ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
710ef20d060SBarry Smith    is used internally within the nonlinear solvers.
711ef20d060SBarry Smith 
712ef20d060SBarry Smith    Level: developer
713ef20d060SBarry Smith 
714ef20d060SBarry Smith .keywords: TS, compute
715ef20d060SBarry Smith 
716abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
717ef20d060SBarry Smith @*/
7180910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
719ef20d060SBarry Smith {
720ef20d060SBarry Smith   PetscErrorCode     ierr;
721ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
722ef20d060SBarry Smith   void               *ctx;
723ef20d060SBarry Smith   DM                 dm;
724ef20d060SBarry Smith 
725ef20d060SBarry Smith   PetscFunctionBegin;
726ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7270910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
728ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
729ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
730ef20d060SBarry Smith 
731ef20d060SBarry Smith   if (solutionfunction) {
7329b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7330910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
734ef20d060SBarry Smith     PetscStackPop;
735ef20d060SBarry Smith   }
736ef20d060SBarry Smith   PetscFunctionReturn(0);
737ef20d060SBarry Smith }
7389b7cd975SBarry Smith /*@
7399b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7409b7cd975SBarry Smith 
7419b7cd975SBarry Smith    Collective on TS and Vec
7429b7cd975SBarry Smith 
7439b7cd975SBarry Smith    Input Parameters:
7449b7cd975SBarry Smith +  ts - the TS context
7459b7cd975SBarry Smith -  t - current time
7469b7cd975SBarry Smith 
7479b7cd975SBarry Smith    Output Parameter:
7489b7cd975SBarry Smith .  U - the function value
7499b7cd975SBarry Smith 
7509b7cd975SBarry Smith    Note:
7519b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7529b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7539b7cd975SBarry Smith 
7549b7cd975SBarry Smith    Level: developer
7559b7cd975SBarry Smith 
7569b7cd975SBarry Smith .keywords: TS, compute
7579b7cd975SBarry Smith 
7589b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7599b7cd975SBarry Smith @*/
7609b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7619b7cd975SBarry Smith {
7629b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7639b7cd975SBarry Smith   void               *ctx;
7649b7cd975SBarry Smith   DM                 dm;
7659b7cd975SBarry Smith 
7669b7cd975SBarry Smith   PetscFunctionBegin;
7679b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7689b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7699b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7709b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7719b7cd975SBarry Smith 
7729b7cd975SBarry Smith   if (forcing) {
7739b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7749b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7759b7cd975SBarry Smith     PetscStackPop;
7769b7cd975SBarry Smith   }
7779b7cd975SBarry Smith   PetscFunctionReturn(0);
7789b7cd975SBarry Smith }
779ef20d060SBarry Smith 
780214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
781214bc6a2SJed Brown {
7822dd45cf8SJed Brown   Vec            F;
783214bc6a2SJed Brown   PetscErrorCode ierr;
784214bc6a2SJed Brown 
785214bc6a2SJed Brown   PetscFunctionBegin;
7860298fd71SBarry Smith   *Frhs = NULL;
7870298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
788214bc6a2SJed Brown   if (!ts->Frhs) {
7892dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
790214bc6a2SJed Brown   }
791214bc6a2SJed Brown   *Frhs = ts->Frhs;
792214bc6a2SJed Brown   PetscFunctionReturn(0);
793214bc6a2SJed Brown }
794214bc6a2SJed Brown 
795214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
796214bc6a2SJed Brown {
797214bc6a2SJed Brown   Mat            A,B;
7982dd45cf8SJed Brown   PetscErrorCode ierr;
79941a1d4d2SBarry Smith   TSIJacobian    ijacobian;
800214bc6a2SJed Brown 
801214bc6a2SJed Brown   PetscFunctionBegin;
802c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
803c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
80441a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
805214bc6a2SJed Brown   if (Arhs) {
806214bc6a2SJed Brown     if (!ts->Arhs) {
80741a1d4d2SBarry Smith       if (ijacobian) {
808214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
80941a1d4d2SBarry Smith       } else {
81041a1d4d2SBarry Smith         ts->Arhs = A;
81141a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
81241a1d4d2SBarry Smith       }
8133565c898SBarry Smith     } else {
8143565c898SBarry Smith       PetscBool flg;
8153565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
8163565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
8173565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
8183565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
8193565c898SBarry Smith         ts->Arhs = A;
8203565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8213565c898SBarry Smith       }
822214bc6a2SJed Brown     }
823214bc6a2SJed Brown     *Arhs = ts->Arhs;
824214bc6a2SJed Brown   }
825214bc6a2SJed Brown   if (Brhs) {
826214bc6a2SJed Brown     if (!ts->Brhs) {
827bdb70873SJed Brown       if (A != B) {
82841a1d4d2SBarry Smith         if (ijacobian) {
829214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
830bdb70873SJed Brown         } else {
83141a1d4d2SBarry Smith           ts->Brhs = B;
83241a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
83341a1d4d2SBarry Smith         }
83441a1d4d2SBarry Smith       } else {
835bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
83651699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
837bdb70873SJed Brown       }
838214bc6a2SJed Brown     }
839214bc6a2SJed Brown     *Brhs = ts->Brhs;
840214bc6a2SJed Brown   }
841214bc6a2SJed Brown   PetscFunctionReturn(0);
842214bc6a2SJed Brown }
843214bc6a2SJed Brown 
844316643e7SJed Brown /*@
8450910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
846316643e7SJed Brown 
847316643e7SJed Brown    Collective on TS and Vec
848316643e7SJed Brown 
849316643e7SJed Brown    Input Parameters:
850316643e7SJed Brown +  ts - the TS context
851316643e7SJed Brown .  t - current time
8520910c330SBarry Smith .  U - state vector
8530910c330SBarry Smith .  Udot - time derivative of state vector
854214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
855316643e7SJed Brown 
856316643e7SJed Brown    Output Parameter:
857316643e7SJed Brown .  Y - right hand side
858316643e7SJed Brown 
859316643e7SJed Brown    Note:
860316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
861316643e7SJed Brown    is used internally within the nonlinear solvers.
862316643e7SJed Brown 
863316643e7SJed Brown    If the user did did not write their equations in implicit form, this
864316643e7SJed Brown    function recasts them in implicit form.
865316643e7SJed Brown 
866316643e7SJed Brown    Level: developer
867316643e7SJed Brown 
868316643e7SJed Brown .keywords: TS, compute
869316643e7SJed Brown 
870316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
871316643e7SJed Brown @*/
8720910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
873316643e7SJed Brown {
874316643e7SJed Brown   PetscErrorCode ierr;
87524989b8cSPeter Brune   TSIFunction    ifunction;
87624989b8cSPeter Brune   TSRHSFunction  rhsfunction;
87724989b8cSPeter Brune   void           *ctx;
87824989b8cSPeter Brune   DM             dm;
879316643e7SJed Brown 
880316643e7SJed Brown   PetscFunctionBegin;
8810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8820910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8830910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8840700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
885316643e7SJed Brown 
88624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
88724989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8880298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
88924989b8cSPeter Brune 
890ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
891d90be118SSean Farley 
8920910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
89324989b8cSPeter Brune   if (ifunction) {
894316643e7SJed Brown     PetscStackPush("TS user implicit function");
8950910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
896316643e7SJed Brown     PetscStackPop;
897214bc6a2SJed Brown   }
898214bc6a2SJed Brown   if (imex) {
89924989b8cSPeter Brune     if (!ifunction) {
9000910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
9012dd45cf8SJed Brown     }
90224989b8cSPeter Brune   } else if (rhsfunction) {
90324989b8cSPeter Brune     if (ifunction) {
904214bc6a2SJed Brown       Vec Frhs;
905214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
9060910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
907214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
9082dd45cf8SJed Brown     } else {
9090910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
9100910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
911316643e7SJed Brown     }
9124a6899ffSJed Brown   }
9130910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
914316643e7SJed Brown   PetscFunctionReturn(0);
915316643e7SJed Brown }
916316643e7SJed Brown 
917316643e7SJed Brown /*@
918316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
919316643e7SJed Brown 
920316643e7SJed Brown    Collective on TS and Vec
921316643e7SJed Brown 
922316643e7SJed Brown    Input
923316643e7SJed Brown       Input Parameters:
924316643e7SJed Brown +  ts - the TS context
925316643e7SJed Brown .  t - current timestep
9260910c330SBarry Smith .  U - state vector
9270910c330SBarry Smith .  Udot - time derivative of state vector
928214bc6a2SJed Brown .  shift - shift to apply, see note below
929214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
930316643e7SJed Brown 
931316643e7SJed Brown    Output Parameters:
932316643e7SJed Brown +  A - Jacobian matrix
9333565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
934316643e7SJed Brown 
935316643e7SJed Brown    Notes:
9360910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
937316643e7SJed Brown 
9380910c330SBarry Smith    dF/dU + shift*dF/dUdot
939316643e7SJed Brown 
940316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
941316643e7SJed Brown    is used internally within the nonlinear solvers.
942316643e7SJed Brown 
943316643e7SJed Brown    Level: developer
944316643e7SJed Brown 
945316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
946316643e7SJed Brown 
947316643e7SJed Brown .seealso:  TSSetIJacobian()
94863495f91SJed Brown @*/
949d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
950316643e7SJed Brown {
951316643e7SJed Brown   PetscErrorCode ierr;
95224989b8cSPeter Brune   TSIJacobian    ijacobian;
95324989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
95424989b8cSPeter Brune   DM             dm;
95524989b8cSPeter Brune   void           *ctx;
956316643e7SJed Brown 
957316643e7SJed Brown   PetscFunctionBegin;
9580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9590910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9600910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
961316643e7SJed Brown   PetscValidPointer(A,6);
96294ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
963316643e7SJed Brown   PetscValidPointer(B,7);
96494ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
96524989b8cSPeter Brune 
96624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
96724989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9680298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
96924989b8cSPeter Brune 
970ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
971316643e7SJed Brown 
97294ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
97324989b8cSPeter Brune   if (ijacobian) {
9746cd88445SBarry Smith     PetscBool missing;
975316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
976d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
977316643e7SJed Brown     PetscStackPop;
9786cd88445SBarry Smith     ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9796cd88445SBarry 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");
9803565c898SBarry Smith     if (B != A) {
9816cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9826cd88445SBarry 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");
9836cd88445SBarry Smith     }
9844a6899ffSJed Brown   }
985214bc6a2SJed Brown   if (imex) {
986b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9874c26be97Sstefano_zampini       PetscBool assembled;
98894ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9894c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9904c26be97Sstefano_zampini       if (!assembled) {
9914c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9924c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9934c26be97Sstefano_zampini       }
99494ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
99594ab13aaSBarry Smith       if (A != B) {
99694ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9974c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9984c26be97Sstefano_zampini         if (!assembled) {
9994c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10004c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10014c26be97Sstefano_zampini         }
100294ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1003214bc6a2SJed Brown       }
1004214bc6a2SJed Brown     }
1005214bc6a2SJed Brown   } else {
1006e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
1007e1244c69SJed Brown     if (rhsjacobian) {
1008214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
1009d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
1010e1244c69SJed Brown     }
101194ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
10123565c898SBarry Smith       PetscBool flg;
1013e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
1014e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
10153565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10163565c898SBarry Smith       /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10173565c898SBarry Smith       if (!flg) {
101894ab13aaSBarry Smith         ierr = MatScale(A,-1);CHKERRQ(ierr);
101994ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
10203565c898SBarry Smith       }
102194ab13aaSBarry Smith       if (A != B) {
102294ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
102394ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1024316643e7SJed Brown       }
1025e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1026e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1027e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
102894ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
102994ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
103094ab13aaSBarry Smith         if (A != B) {
103194ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
103294ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1033214bc6a2SJed Brown         }
1034316643e7SJed Brown       }
103594ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
103694ab13aaSBarry Smith       if (A != B) {
103794ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
1038316643e7SJed Brown       }
1039316643e7SJed Brown     }
1040316643e7SJed Brown   }
104194ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1042316643e7SJed Brown   PetscFunctionReturn(0);
1043316643e7SJed Brown }
1044316643e7SJed Brown 
1045d763cef2SBarry Smith /*@C
1046d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1047b5abc632SBarry Smith     where U_t = G(t,u).
1048d763cef2SBarry Smith 
10493f9fe445SBarry Smith     Logically Collective on TS
1050d763cef2SBarry Smith 
1051d763cef2SBarry Smith     Input Parameters:
1052d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10530298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1054d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1055d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10560298fd71SBarry Smith           function evaluation routine (may be NULL)
1057d763cef2SBarry Smith 
1058d763cef2SBarry Smith     Calling sequence of func:
105987828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1060d763cef2SBarry Smith 
1061d763cef2SBarry Smith +   t - current timestep
1062d763cef2SBarry Smith .   u - input vector
1063d763cef2SBarry Smith .   F - function vector
1064d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1065d763cef2SBarry Smith 
1066d763cef2SBarry Smith     Level: beginner
1067d763cef2SBarry Smith 
10682bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10692bbac0d3SBarry Smith 
1070d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1071d763cef2SBarry Smith 
1072ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1073d763cef2SBarry Smith @*/
1074089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1075d763cef2SBarry Smith {
1076089b2837SJed Brown   PetscErrorCode ierr;
1077089b2837SJed Brown   SNES           snes;
10780298fd71SBarry Smith   Vec            ralloc = NULL;
107924989b8cSPeter Brune   DM             dm;
1080d763cef2SBarry Smith 
1081089b2837SJed Brown   PetscFunctionBegin;
10820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1083ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
108424989b8cSPeter Brune 
108524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
108624989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1087089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1088e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1089e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1090e856ceecSJed Brown     r = ralloc;
1091e856ceecSJed Brown   }
1092089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1093e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1094d763cef2SBarry Smith   PetscFunctionReturn(0);
1095d763cef2SBarry Smith }
1096d763cef2SBarry Smith 
1097ef20d060SBarry Smith /*@C
1098abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1099ef20d060SBarry Smith 
1100ef20d060SBarry Smith     Logically Collective on TS
1101ef20d060SBarry Smith 
1102ef20d060SBarry Smith     Input Parameters:
1103ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1104ef20d060SBarry Smith .   f - routine for evaluating the solution
1105ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11060298fd71SBarry Smith           function evaluation routine (may be NULL)
1107ef20d060SBarry Smith 
1108ef20d060SBarry Smith     Calling sequence of func:
1109ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1110ef20d060SBarry Smith 
1111ef20d060SBarry Smith +   t - current timestep
1112ef20d060SBarry Smith .   u - output vector
1113ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1114ef20d060SBarry Smith 
1115abd5a294SJed Brown     Notes:
1116abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1117abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1118abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1119abd5a294SJed Brown 
11204f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1121abd5a294SJed Brown 
1122ef20d060SBarry Smith     Level: beginner
1123ef20d060SBarry Smith 
1124ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1125ef20d060SBarry Smith 
11269b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
1127ef20d060SBarry Smith @*/
1128ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1129ef20d060SBarry Smith {
1130ef20d060SBarry Smith   PetscErrorCode ierr;
1131ef20d060SBarry Smith   DM             dm;
1132ef20d060SBarry Smith 
1133ef20d060SBarry Smith   PetscFunctionBegin;
1134ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1135ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1136ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1137ef20d060SBarry Smith   PetscFunctionReturn(0);
1138ef20d060SBarry Smith }
1139ef20d060SBarry Smith 
11409b7cd975SBarry Smith /*@C
11419b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11429b7cd975SBarry Smith 
11439b7cd975SBarry Smith     Logically Collective on TS
11449b7cd975SBarry Smith 
11459b7cd975SBarry Smith     Input Parameters:
11469b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1147e162b725SBarry Smith .   func - routine for evaluating the forcing function
11489b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11490298fd71SBarry Smith           function evaluation routine (may be NULL)
11509b7cd975SBarry Smith 
11519b7cd975SBarry Smith     Calling sequence of func:
1152e162b725SBarry Smith $     func (TS ts,PetscReal t,Vec f,void *ctx);
11539b7cd975SBarry Smith 
11549b7cd975SBarry Smith +   t - current timestep
1155e162b725SBarry Smith .   f - output vector
11569b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11579b7cd975SBarry Smith 
11589b7cd975SBarry Smith     Notes:
11599b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1160e162b725SBarry Smith     create closed-form solutions with a non-physical forcing term. It allows you to use the Method of Manufactored Solution without directly editing the
1161e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1162e162b725SBarry Smith 
1163e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1164e162b725SBarry Smith 
1165e162b725SBarry Smith     This forcing function does not depend on the solution to the equations, it can only depend on spatial location, time, and possibly parameters, the
1166e162b725SBarry Smith     parameters can be passed in the ctx variable.
11679b7cd975SBarry Smith 
11689b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11699b7cd975SBarry Smith 
11709b7cd975SBarry Smith     Level: beginner
11719b7cd975SBarry Smith 
11729b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
11739b7cd975SBarry Smith 
11749b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11759b7cd975SBarry Smith @*/
1176e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11779b7cd975SBarry Smith {
11789b7cd975SBarry Smith   PetscErrorCode ierr;
11799b7cd975SBarry Smith   DM             dm;
11809b7cd975SBarry Smith 
11819b7cd975SBarry Smith   PetscFunctionBegin;
11829b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11839b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1184e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11859b7cd975SBarry Smith   PetscFunctionReturn(0);
11869b7cd975SBarry Smith }
11879b7cd975SBarry Smith 
1188d763cef2SBarry Smith /*@C
1189f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1190b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1191d763cef2SBarry Smith 
11923f9fe445SBarry Smith    Logically Collective on TS
1193d763cef2SBarry Smith 
1194d763cef2SBarry Smith    Input Parameters:
1195d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1196e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1197e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1198d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1199d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
12000298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1201d763cef2SBarry Smith 
1202f7ab8db6SBarry Smith    Calling sequence of f:
1203ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1204d763cef2SBarry Smith 
1205d763cef2SBarry Smith +  t - current timestep
1206d763cef2SBarry Smith .  u - input vector
1207e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1208e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1209d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1210d763cef2SBarry Smith 
12116cd88445SBarry Smith    Notes:
12126cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12136cd88445SBarry Smith 
12146cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1215ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1216d763cef2SBarry Smith 
1217d763cef2SBarry Smith    Level: beginner
1218d763cef2SBarry Smith 
1219d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1220d763cef2SBarry Smith 
1221ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1222d763cef2SBarry Smith 
1223d763cef2SBarry Smith @*/
1224e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1225d763cef2SBarry Smith {
1226277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1227089b2837SJed Brown   SNES           snes;
122824989b8cSPeter Brune   DM             dm;
122924989b8cSPeter Brune   TSIJacobian    ijacobian;
1230277b19d0SLisandro Dalcin 
1231d763cef2SBarry Smith   PetscFunctionBegin;
12320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1233e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1234e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1235e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1236e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1237d763cef2SBarry Smith 
123824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
123924989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1240e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1241e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1242e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1243e1244c69SJed Brown   }
12440298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1245089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12465f659677SPeter Brune   if (!ijacobian) {
1247e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12480e4ef248SJed Brown   }
1249e5d3d808SBarry Smith   if (Amat) {
1250e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12510e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1252e5d3d808SBarry Smith     ts->Arhs = Amat;
12530e4ef248SJed Brown   }
1254e5d3d808SBarry Smith   if (Pmat) {
1255e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12560e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1257e5d3d808SBarry Smith     ts->Brhs = Pmat;
12580e4ef248SJed Brown   }
1259d763cef2SBarry Smith   PetscFunctionReturn(0);
1260d763cef2SBarry Smith }
1261d763cef2SBarry Smith 
1262316643e7SJed Brown /*@C
1263b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1264316643e7SJed Brown 
12653f9fe445SBarry Smith    Logically Collective on TS
1266316643e7SJed Brown 
1267316643e7SJed Brown    Input Parameters:
1268316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12690298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1270316643e7SJed Brown .  f   - the function evaluation routine
12710298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1272316643e7SJed Brown 
1273316643e7SJed Brown    Calling sequence of f:
1274316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1275316643e7SJed Brown 
1276316643e7SJed Brown +  t   - time at step/stage being solved
1277316643e7SJed Brown .  u   - state vector
1278316643e7SJed Brown .  u_t - time derivative of state vector
1279316643e7SJed Brown .  F   - function vector
1280316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1281316643e7SJed Brown 
1282316643e7SJed Brown    Important:
12832bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1284316643e7SJed Brown 
1285316643e7SJed Brown    Level: beginner
1286316643e7SJed Brown 
1287316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1288316643e7SJed Brown 
1289d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1290316643e7SJed Brown @*/
129151699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1292316643e7SJed Brown {
1293089b2837SJed Brown   PetscErrorCode ierr;
1294089b2837SJed Brown   SNES           snes;
129551699248SLisandro Dalcin   Vec            ralloc = NULL;
129624989b8cSPeter Brune   DM             dm;
1297316643e7SJed Brown 
1298316643e7SJed Brown   PetscFunctionBegin;
12990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
130051699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
130124989b8cSPeter Brune 
130224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
130324989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
130424989b8cSPeter Brune 
1305089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
130651699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
130751699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
130851699248SLisandro Dalcin     r  = ralloc;
1309e856ceecSJed Brown   }
131051699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
131151699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1312089b2837SJed Brown   PetscFunctionReturn(0);
1313089b2837SJed Brown }
1314089b2837SJed Brown 
1315089b2837SJed Brown /*@C
1316089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1317089b2837SJed Brown 
1318089b2837SJed Brown    Not Collective
1319089b2837SJed Brown 
1320089b2837SJed Brown    Input Parameter:
1321089b2837SJed Brown .  ts - the TS context
1322089b2837SJed Brown 
1323089b2837SJed Brown    Output Parameter:
13240298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13250298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13260298fd71SBarry Smith -  ctx - the function context (or NULL)
1327089b2837SJed Brown 
1328089b2837SJed Brown    Level: advanced
1329089b2837SJed Brown 
1330089b2837SJed Brown .keywords: TS, nonlinear, get, function
1331089b2837SJed Brown 
1332089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1333089b2837SJed Brown @*/
1334089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1335089b2837SJed Brown {
1336089b2837SJed Brown   PetscErrorCode ierr;
1337089b2837SJed Brown   SNES           snes;
133824989b8cSPeter Brune   DM             dm;
1339089b2837SJed Brown 
1340089b2837SJed Brown   PetscFunctionBegin;
1341089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1342089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13430298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
134424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
134524989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1346089b2837SJed Brown   PetscFunctionReturn(0);
1347089b2837SJed Brown }
1348089b2837SJed Brown 
1349089b2837SJed Brown /*@C
1350089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1351089b2837SJed Brown 
1352089b2837SJed Brown    Not Collective
1353089b2837SJed Brown 
1354089b2837SJed Brown    Input Parameter:
1355089b2837SJed Brown .  ts - the TS context
1356089b2837SJed Brown 
1357089b2837SJed Brown    Output Parameter:
13580298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13590298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13600298fd71SBarry Smith -  ctx - the function context (or NULL)
1361089b2837SJed Brown 
1362089b2837SJed Brown    Level: advanced
1363089b2837SJed Brown 
1364089b2837SJed Brown .keywords: TS, nonlinear, get, function
1365089b2837SJed Brown 
13662bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1367089b2837SJed Brown @*/
1368089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1369089b2837SJed Brown {
1370089b2837SJed Brown   PetscErrorCode ierr;
1371089b2837SJed Brown   SNES           snes;
137224989b8cSPeter Brune   DM             dm;
1373089b2837SJed Brown 
1374089b2837SJed Brown   PetscFunctionBegin;
1375089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1376089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13770298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
137824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
137924989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1380316643e7SJed Brown   PetscFunctionReturn(0);
1381316643e7SJed Brown }
1382316643e7SJed Brown 
1383316643e7SJed Brown /*@C
1384a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1385ae8867d6SBarry Smith         provided with TSSetIFunction().
1386316643e7SJed Brown 
13873f9fe445SBarry Smith    Logically Collective on TS
1388316643e7SJed Brown 
1389316643e7SJed Brown    Input Parameters:
1390316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1391e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1392e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1393316643e7SJed Brown .  f   - the Jacobian evaluation routine
13940298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1395316643e7SJed Brown 
1396316643e7SJed Brown    Calling sequence of f:
1397ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1398316643e7SJed Brown 
1399316643e7SJed Brown +  t    - time at step/stage being solved
14001b4a444bSJed Brown .  U    - state vector
14011b4a444bSJed Brown .  U_t  - time derivative of state vector
1402316643e7SJed Brown .  a    - shift
1403e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1404e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1405316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1406316643e7SJed Brown 
1407316643e7SJed Brown    Notes:
1408e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1409316643e7SJed Brown 
1410895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1411895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1412895c21f2SBarry Smith 
1413a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1414b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1415a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1416a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1417a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1418a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1419a4f0a591SBarry Smith 
14206cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14216cd88445SBarry Smith 
14226cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1423ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1424ca5f011dSBarry Smith 
1425316643e7SJed Brown    Level: beginner
1426316643e7SJed Brown 
1427316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1428316643e7SJed Brown 
1429ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1430316643e7SJed Brown 
1431316643e7SJed Brown @*/
1432e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1433316643e7SJed Brown {
1434316643e7SJed Brown   PetscErrorCode ierr;
1435089b2837SJed Brown   SNES           snes;
143624989b8cSPeter Brune   DM             dm;
1437316643e7SJed Brown 
1438316643e7SJed Brown   PetscFunctionBegin;
14390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1440e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1441e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1442e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1443e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
144424989b8cSPeter Brune 
144524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
144624989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
144724989b8cSPeter Brune 
1448089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1449e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1450316643e7SJed Brown   PetscFunctionReturn(0);
1451316643e7SJed Brown }
1452316643e7SJed Brown 
1453e1244c69SJed Brown /*@
1454e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1455e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1456e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1457e1244c69SJed Brown    not been changed by the TS.
1458e1244c69SJed Brown 
1459e1244c69SJed Brown    Logically Collective
1460e1244c69SJed Brown 
1461e1244c69SJed Brown    Input Arguments:
1462e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1463e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1464e1244c69SJed Brown 
1465e1244c69SJed Brown    Level: intermediate
1466e1244c69SJed Brown 
1467e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1468e1244c69SJed Brown @*/
1469e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1470e1244c69SJed Brown {
1471e1244c69SJed Brown   PetscFunctionBegin;
1472e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1473e1244c69SJed Brown   PetscFunctionReturn(0);
1474e1244c69SJed Brown }
1475e1244c69SJed Brown 
1476efe9872eSLisandro Dalcin /*@C
1477efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1478efe9872eSLisandro Dalcin 
1479efe9872eSLisandro Dalcin    Logically Collective on TS
1480efe9872eSLisandro Dalcin 
1481efe9872eSLisandro Dalcin    Input Parameters:
1482efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1483efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1484efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1485efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1486efe9872eSLisandro Dalcin 
1487efe9872eSLisandro Dalcin    Calling sequence of fun:
1488efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1489efe9872eSLisandro Dalcin 
1490efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1491efe9872eSLisandro Dalcin .  U    - state vector
1492efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1493efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1494efe9872eSLisandro Dalcin .  F    - function vector
1495efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1496efe9872eSLisandro Dalcin 
1497efe9872eSLisandro Dalcin    Level: beginner
1498efe9872eSLisandro Dalcin 
1499efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1500efe9872eSLisandro Dalcin 
1501efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1502efe9872eSLisandro Dalcin @*/
1503efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1504efe9872eSLisandro Dalcin {
1505efe9872eSLisandro Dalcin   DM             dm;
1506efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1507efe9872eSLisandro Dalcin 
1508efe9872eSLisandro Dalcin   PetscFunctionBegin;
1509efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1510efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1511efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1512efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1513efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1514efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1515efe9872eSLisandro Dalcin }
1516efe9872eSLisandro Dalcin 
1517efe9872eSLisandro Dalcin /*@C
1518efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1519efe9872eSLisandro Dalcin 
1520efe9872eSLisandro Dalcin   Not Collective
1521efe9872eSLisandro Dalcin 
1522efe9872eSLisandro Dalcin   Input Parameter:
1523efe9872eSLisandro Dalcin . ts - the TS context
1524efe9872eSLisandro Dalcin 
1525efe9872eSLisandro Dalcin   Output Parameter:
1526efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1527efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1528efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1529efe9872eSLisandro Dalcin 
1530efe9872eSLisandro Dalcin   Level: advanced
1531efe9872eSLisandro Dalcin 
1532efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1533efe9872eSLisandro Dalcin 
1534efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1535efe9872eSLisandro Dalcin @*/
1536efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1537efe9872eSLisandro Dalcin {
1538efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1539efe9872eSLisandro Dalcin   SNES           snes;
1540efe9872eSLisandro Dalcin   DM             dm;
1541efe9872eSLisandro Dalcin 
1542efe9872eSLisandro Dalcin   PetscFunctionBegin;
1543efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1544efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1545efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1546efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1547efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1548efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1549efe9872eSLisandro Dalcin }
1550efe9872eSLisandro Dalcin 
1551efe9872eSLisandro Dalcin /*@C
1552bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1553efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1554efe9872eSLisandro Dalcin 
1555efe9872eSLisandro Dalcin    Logically Collective on TS
1556efe9872eSLisandro Dalcin 
1557efe9872eSLisandro Dalcin    Input Parameters:
1558efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1559efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1560efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1561efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1562efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1563efe9872eSLisandro Dalcin 
1564efe9872eSLisandro Dalcin    Calling sequence of jac:
1565bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1566efe9872eSLisandro Dalcin 
1567efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1568efe9872eSLisandro Dalcin .  U    - state vector
1569efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1570efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1571efe9872eSLisandro Dalcin .  v    - shift for U_t
1572efe9872eSLisandro Dalcin .  a    - shift for U_tt
1573efe9872eSLisandro Dalcin .  J    - Jacobian of G(U) = F(t,U,W+v*U,W'+a*U), equivalent to dF/dU + v*dF/dU_t  + a*dF/dU_tt
1574efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1575efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1576efe9872eSLisandro Dalcin 
1577efe9872eSLisandro Dalcin    Notes:
1578efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1579efe9872eSLisandro Dalcin 
1580efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1581efe9872eSLisandro Dalcin    the Jacobian of G(U) = F(t,U,W+v*U,W'+a*U) where F(t,U,U_t,U_tt) = 0 is the DAE to be solved.
1582efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1583bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1584efe9872eSLisandro Dalcin 
1585efe9872eSLisandro Dalcin    Level: beginner
1586efe9872eSLisandro Dalcin 
1587efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1588efe9872eSLisandro Dalcin 
1589efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1590efe9872eSLisandro Dalcin @*/
1591efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1592efe9872eSLisandro Dalcin {
1593efe9872eSLisandro Dalcin   DM             dm;
1594efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1595efe9872eSLisandro Dalcin 
1596efe9872eSLisandro Dalcin   PetscFunctionBegin;
1597efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1598efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1599efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1600efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1601efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1602efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1603efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1604efe9872eSLisandro Dalcin }
1605efe9872eSLisandro Dalcin 
1606efe9872eSLisandro Dalcin /*@C
1607efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1608efe9872eSLisandro Dalcin 
1609efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1610efe9872eSLisandro Dalcin 
1611efe9872eSLisandro Dalcin   Input Parameter:
1612efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1613efe9872eSLisandro Dalcin 
1614efe9872eSLisandro Dalcin   Output Parameters:
1615efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1616efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1617efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1618efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1619efe9872eSLisandro Dalcin 
1620efe9872eSLisandro Dalcin   Notes: You can pass in NULL for any return argument you do not need.
1621efe9872eSLisandro Dalcin 
1622efe9872eSLisandro Dalcin   Level: advanced
1623efe9872eSLisandro Dalcin 
162480275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
1625efe9872eSLisandro Dalcin 
1626efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1627efe9872eSLisandro Dalcin @*/
1628efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1629efe9872eSLisandro Dalcin {
1630efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1631efe9872eSLisandro Dalcin   SNES           snes;
1632efe9872eSLisandro Dalcin   DM             dm;
1633efe9872eSLisandro Dalcin 
1634efe9872eSLisandro Dalcin   PetscFunctionBegin;
1635efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1636efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1637efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1638efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1639efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1640efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1641efe9872eSLisandro Dalcin }
1642efe9872eSLisandro Dalcin 
1643efe9872eSLisandro Dalcin /*@
1644efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1645efe9872eSLisandro Dalcin 
1646efe9872eSLisandro Dalcin   Collective on TS and Vec
1647efe9872eSLisandro Dalcin 
1648efe9872eSLisandro Dalcin   Input Parameters:
1649efe9872eSLisandro Dalcin + ts - the TS context
1650efe9872eSLisandro Dalcin . t - current time
1651efe9872eSLisandro Dalcin . U - state vector
1652efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1653efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1654efe9872eSLisandro Dalcin 
1655efe9872eSLisandro Dalcin   Output Parameter:
1656efe9872eSLisandro Dalcin . F - the residual vector
1657efe9872eSLisandro Dalcin 
1658efe9872eSLisandro Dalcin   Note:
1659efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1660efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1661efe9872eSLisandro Dalcin 
1662efe9872eSLisandro Dalcin   Level: developer
1663efe9872eSLisandro Dalcin 
1664efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1665efe9872eSLisandro Dalcin 
1666efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1667efe9872eSLisandro Dalcin @*/
1668efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1669efe9872eSLisandro Dalcin {
1670efe9872eSLisandro Dalcin   DM             dm;
1671efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1672efe9872eSLisandro Dalcin   void           *ctx;
1673efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1674efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1675efe9872eSLisandro Dalcin 
1676efe9872eSLisandro Dalcin   PetscFunctionBegin;
1677efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1678efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1679efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1680efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1681efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1682efe9872eSLisandro Dalcin 
1683efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1684efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1685efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1686efe9872eSLisandro Dalcin 
1687efe9872eSLisandro Dalcin   if (!I2Function) {
1688efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1689efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1690efe9872eSLisandro Dalcin   }
1691efe9872eSLisandro Dalcin 
1692efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1693efe9872eSLisandro Dalcin 
1694efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1695efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1696efe9872eSLisandro Dalcin   PetscStackPop;
1697efe9872eSLisandro Dalcin 
1698efe9872eSLisandro Dalcin   if (rhsfunction) {
1699efe9872eSLisandro Dalcin     Vec Frhs;
1700efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1701efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1702efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1703efe9872eSLisandro Dalcin   }
1704efe9872eSLisandro Dalcin 
1705efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1706efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1707efe9872eSLisandro Dalcin }
1708efe9872eSLisandro Dalcin 
1709efe9872eSLisandro Dalcin /*@
1710efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1711efe9872eSLisandro Dalcin 
1712efe9872eSLisandro Dalcin   Collective on TS and Vec
1713efe9872eSLisandro Dalcin 
1714efe9872eSLisandro Dalcin   Input Parameters:
1715efe9872eSLisandro Dalcin + ts - the TS context
1716efe9872eSLisandro Dalcin . t - current timestep
1717efe9872eSLisandro Dalcin . U - state vector
1718efe9872eSLisandro Dalcin . V - time derivative of state vector
1719efe9872eSLisandro Dalcin . A - second time derivative of state vector
1720efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1721efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1722efe9872eSLisandro Dalcin 
1723efe9872eSLisandro Dalcin   Output Parameters:
1724efe9872eSLisandro Dalcin + J - Jacobian matrix
1725efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1726efe9872eSLisandro Dalcin 
1727efe9872eSLisandro Dalcin   Notes:
1728efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1729efe9872eSLisandro Dalcin 
1730efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1731efe9872eSLisandro Dalcin 
1732efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1733efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1734efe9872eSLisandro Dalcin 
1735efe9872eSLisandro Dalcin   Level: developer
1736efe9872eSLisandro Dalcin 
1737efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1738efe9872eSLisandro Dalcin 
1739efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1740efe9872eSLisandro Dalcin @*/
1741efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1742efe9872eSLisandro Dalcin {
1743efe9872eSLisandro Dalcin   DM             dm;
1744efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1745efe9872eSLisandro Dalcin   void           *ctx;
1746efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1747efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1748efe9872eSLisandro Dalcin 
1749efe9872eSLisandro Dalcin   PetscFunctionBegin;
1750efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1751efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1752efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1753efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1754efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1755efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1756efe9872eSLisandro Dalcin 
1757efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1758efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1759efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1760efe9872eSLisandro Dalcin 
1761efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1762efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1763efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1764efe9872eSLisandro Dalcin   }
1765efe9872eSLisandro Dalcin 
1766efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1767efe9872eSLisandro Dalcin 
1768efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1769efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1770efe9872eSLisandro Dalcin   PetscStackPop;
1771efe9872eSLisandro Dalcin 
1772efe9872eSLisandro Dalcin   if (rhsjacobian) {
1773efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1774efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1775efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1776efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1777efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1778efe9872eSLisandro Dalcin   }
1779efe9872eSLisandro Dalcin 
1780efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1781efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1782efe9872eSLisandro Dalcin }
1783efe9872eSLisandro Dalcin 
1784efe9872eSLisandro Dalcin /*@
1785efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1786efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1787efe9872eSLisandro Dalcin 
1788efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1789efe9872eSLisandro Dalcin 
1790efe9872eSLisandro Dalcin    Input Parameters:
1791efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1792efe9872eSLisandro Dalcin .  u - the solution vector
1793efe9872eSLisandro Dalcin -  v - the time derivative vector
1794efe9872eSLisandro Dalcin 
1795efe9872eSLisandro Dalcin    Level: beginner
1796efe9872eSLisandro Dalcin 
1797efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1798efe9872eSLisandro Dalcin @*/
1799efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1800efe9872eSLisandro Dalcin {
1801efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1802efe9872eSLisandro Dalcin 
1803efe9872eSLisandro Dalcin   PetscFunctionBegin;
1804efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1805efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1806efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1807efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1808efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1809efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1810efe9872eSLisandro Dalcin   ts->vec_dot = v;
1811efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1812efe9872eSLisandro Dalcin }
1813efe9872eSLisandro Dalcin 
1814efe9872eSLisandro Dalcin /*@
1815efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1816efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1817efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1818efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1819efe9872eSLisandro Dalcin 
1820efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1821efe9872eSLisandro Dalcin 
1822efe9872eSLisandro Dalcin    Input Parameter:
1823efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1824efe9872eSLisandro Dalcin 
1825efe9872eSLisandro Dalcin    Output Parameter:
1826efe9872eSLisandro Dalcin +  u - the vector containing the solution
1827efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1828efe9872eSLisandro Dalcin 
1829efe9872eSLisandro Dalcin    Level: intermediate
1830efe9872eSLisandro Dalcin 
1831efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1832efe9872eSLisandro Dalcin 
1833efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1834efe9872eSLisandro Dalcin @*/
1835efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1836efe9872eSLisandro Dalcin {
1837efe9872eSLisandro Dalcin   PetscFunctionBegin;
1838efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1839efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1840efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1841efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1842efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1843efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1844efe9872eSLisandro Dalcin }
1845efe9872eSLisandro Dalcin 
184655849f57SBarry Smith /*@C
184755849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
184855849f57SBarry Smith 
184955849f57SBarry Smith   Collective on PetscViewer
185055849f57SBarry Smith 
185155849f57SBarry Smith   Input Parameters:
185255849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
185355849f57SBarry Smith            some related function before a call to TSLoad().
185455849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
185555849f57SBarry Smith 
185655849f57SBarry Smith    Level: intermediate
185755849f57SBarry Smith 
185855849f57SBarry Smith   Notes:
185955849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
186055849f57SBarry Smith 
186155849f57SBarry Smith   Notes for advanced users:
186255849f57SBarry Smith   Most users should not need to know the details of the binary storage
186355849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
186455849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
186555849f57SBarry Smith   format is
186655849f57SBarry Smith .vb
186755849f57SBarry Smith      has not yet been determined
186855849f57SBarry Smith .ve
186955849f57SBarry Smith 
187055849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
187155849f57SBarry Smith @*/
1872f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
187355849f57SBarry Smith {
187455849f57SBarry Smith   PetscErrorCode ierr;
187555849f57SBarry Smith   PetscBool      isbinary;
187655849f57SBarry Smith   PetscInt       classid;
187755849f57SBarry Smith   char           type[256];
18782d53ad75SBarry Smith   DMTS           sdm;
1879ad6bc421SBarry Smith   DM             dm;
188055849f57SBarry Smith 
188155849f57SBarry Smith   PetscFunctionBegin;
1882f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
188355849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
188455849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
188555849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
188655849f57SBarry Smith 
1887060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1888ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1889060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1890f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1891f2c2a1b9SBarry Smith   if (ts->ops->load) {
1892f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1893f2c2a1b9SBarry Smith   }
1894ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1895ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1896ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1897f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1898f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
18992d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19002d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
190155849f57SBarry Smith   PetscFunctionReturn(0);
190255849f57SBarry Smith }
190355849f57SBarry Smith 
19049804daf3SBarry Smith #include <petscdraw.h>
1905e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1906e04113cfSBarry Smith #include <petscviewersaws.h>
1907f05ece33SBarry Smith #endif
19087e2c5f70SBarry Smith /*@C
1909d763cef2SBarry Smith     TSView - Prints the TS data structure.
1910d763cef2SBarry Smith 
19114c49b128SBarry Smith     Collective on TS
1912d763cef2SBarry Smith 
1913d763cef2SBarry Smith     Input Parameters:
1914d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1915d763cef2SBarry Smith -   viewer - visualization context
1916d763cef2SBarry Smith 
1917d763cef2SBarry Smith     Options Database Key:
1918d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1919d763cef2SBarry Smith 
1920d763cef2SBarry Smith     Notes:
1921d763cef2SBarry Smith     The available visualization contexts include
1922b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1923b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1924d763cef2SBarry Smith          output where only the first processor opens
1925d763cef2SBarry Smith          the file.  All other processors send their
1926d763cef2SBarry Smith          data to the first processor to print.
1927d763cef2SBarry Smith 
1928d763cef2SBarry Smith     The user can open an alternative visualization context with
1929b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1930d763cef2SBarry Smith 
1931d763cef2SBarry Smith     Level: beginner
1932d763cef2SBarry Smith 
1933d763cef2SBarry Smith .keywords: TS, timestep, view
1934d763cef2SBarry Smith 
1935b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1936d763cef2SBarry Smith @*/
19377087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1938d763cef2SBarry Smith {
1939dfbe8321SBarry Smith   PetscErrorCode ierr;
194019fd82e9SBarry Smith   TSType         type;
19412b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
19422d53ad75SBarry Smith   DMTS           sdm;
1943e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1944536b137fSBarry Smith   PetscBool      issaws;
1945f05ece33SBarry Smith #endif
1946d763cef2SBarry Smith 
1947d763cef2SBarry Smith   PetscFunctionBegin;
19480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19493050cee2SBarry Smith   if (!viewer) {
1950ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
19513050cee2SBarry Smith   }
19520700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1953c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1954fd16b177SBarry Smith 
1955251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1956251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
195755849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19582b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1959e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1960536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1961f05ece33SBarry Smith #endif
196232077d6dSBarry Smith   if (iascii) {
1963dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
1964efd4aadfSBarry Smith     if (ts->ops->view) {
1965efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1966efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1967efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1968efd4aadfSBarry Smith     }
1969ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
197077431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1971ef85077eSLisandro Dalcin     }
1972ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
19737c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1974ef85077eSLisandro Dalcin     }
1975efd4aadfSBarry Smith     if (ts->usessnes) {
1976efd4aadfSBarry Smith       PetscBool lin;
1977d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
19785ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1979d763cef2SBarry Smith       }
19805ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1981efd4aadfSBarry Smith       ierr = PetscObjectTypeCompare((PetscObject)ts->snes,SNESKSPONLY,&lin);CHKERRQ(ierr);
1982efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
1983efd4aadfSBarry Smith     }
1984193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1985a0af407cSBarry Smith     if (ts->vrtol) {
1986a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1987a0af407cSBarry Smith     } else {
1988a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
1989a0af407cSBarry Smith     }
1990a0af407cSBarry Smith     if (ts->vatol) {
1991a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
1992a0af407cSBarry Smith     } else {
1993a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
1994a0af407cSBarry Smith     }
1995efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
1996efd4aadfSBarry Smith     if (ts->snes && ts->usessnes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
19972d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19982d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19990f5bd95cSBarry Smith   } else if (isstring) {
2000a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
2001b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
200255849f57SBarry Smith   } else if (isbinary) {
200355849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
200455849f57SBarry Smith     MPI_Comm    comm;
200555849f57SBarry Smith     PetscMPIInt rank;
200655849f57SBarry Smith     char        type[256];
200755849f57SBarry Smith 
200855849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
200955849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
201055849f57SBarry Smith     if (!rank) {
201155849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
201255849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
201355849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
201455849f57SBarry Smith     }
201555849f57SBarry Smith     if (ts->ops->view) {
201655849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
201755849f57SBarry Smith     }
2018efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2019f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2020f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
20212d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20222d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
20232b0a91c0SBarry Smith   } else if (isdraw) {
20242b0a91c0SBarry Smith     PetscDraw draw;
20252b0a91c0SBarry Smith     char      str[36];
202689fd9fafSBarry Smith     PetscReal x,y,bottom,h;
20272b0a91c0SBarry Smith 
20282b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
20292b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
20302b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
20312b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
203251fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
203389fd9fafSBarry Smith     bottom = y - h;
20342b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
20352b0a91c0SBarry Smith     if (ts->ops->view) {
20362b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20372b0a91c0SBarry Smith     }
2038efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2039efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
20402b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2041e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2042536b137fSBarry Smith   } else if (issaws) {
2043d45a07a7SBarry Smith     PetscMPIInt rank;
20442657e9d9SBarry Smith     const char  *name;
20452657e9d9SBarry Smith 
20462657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2047d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2048d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2049d45a07a7SBarry Smith       char       dir[1024];
2050d45a07a7SBarry Smith 
2051e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2052a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
20532657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
20542657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
20552657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2056d763cef2SBarry Smith     }
20570acecf5bSBarry Smith     if (ts->ops->view) {
20580acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20590acecf5bSBarry Smith     }
2060f05ece33SBarry Smith #endif
2061f05ece33SBarry Smith   }
2062f05ece33SBarry Smith 
2063b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2064251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2065b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2066d763cef2SBarry Smith   PetscFunctionReturn(0);
2067d763cef2SBarry Smith }
2068d763cef2SBarry Smith 
2069b07ff414SBarry Smith /*@
2070d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2071d763cef2SBarry Smith    the timesteppers.
2072d763cef2SBarry Smith 
20733f9fe445SBarry Smith    Logically Collective on TS
2074d763cef2SBarry Smith 
2075d763cef2SBarry Smith    Input Parameters:
2076d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2077d763cef2SBarry Smith -  usrP - optional user context
2078d763cef2SBarry Smith 
2079daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2080daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2081daf670e6SBarry Smith 
2082d763cef2SBarry Smith    Level: intermediate
2083d763cef2SBarry Smith 
2084d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
2085d763cef2SBarry Smith 
2086d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2087d763cef2SBarry Smith @*/
20887087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2089d763cef2SBarry Smith {
2090d763cef2SBarry Smith   PetscFunctionBegin;
20910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2092d763cef2SBarry Smith   ts->user = usrP;
2093d763cef2SBarry Smith   PetscFunctionReturn(0);
2094d763cef2SBarry Smith }
2095d763cef2SBarry Smith 
2096b07ff414SBarry Smith /*@
2097d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2098d763cef2SBarry Smith     timestepper.
2099d763cef2SBarry Smith 
2100d763cef2SBarry Smith     Not Collective
2101d763cef2SBarry Smith 
2102d763cef2SBarry Smith     Input Parameter:
2103d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2104d763cef2SBarry Smith 
2105d763cef2SBarry Smith     Output Parameter:
2106d763cef2SBarry Smith .   usrP - user context
2107d763cef2SBarry Smith 
2108daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2109daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2110daf670e6SBarry Smith 
2111d763cef2SBarry Smith     Level: intermediate
2112d763cef2SBarry Smith 
2113d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2114d763cef2SBarry Smith 
2115d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2116d763cef2SBarry Smith @*/
2117e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2118d763cef2SBarry Smith {
2119d763cef2SBarry Smith   PetscFunctionBegin;
21200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2121e71120c6SJed Brown   *(void**)usrP = ts->user;
2122d763cef2SBarry Smith   PetscFunctionReturn(0);
2123d763cef2SBarry Smith }
2124d763cef2SBarry Smith 
2125d763cef2SBarry Smith /*@
212680275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2127d763cef2SBarry Smith 
2128d763cef2SBarry Smith    Not Collective
2129d763cef2SBarry Smith 
2130d763cef2SBarry Smith    Input Parameter:
2131d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2132d763cef2SBarry Smith 
2133d763cef2SBarry Smith    Output Parameter:
213480275a0aSLisandro Dalcin .  steps - number of steps completed so far
2135d763cef2SBarry Smith 
2136d763cef2SBarry Smith    Level: intermediate
2137d763cef2SBarry Smith 
2138d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
21399be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2140d763cef2SBarry Smith @*/
214180275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2142d763cef2SBarry Smith {
2143d763cef2SBarry Smith   PetscFunctionBegin;
21440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
214580275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
214680275a0aSLisandro Dalcin   *steps = ts->steps;
214780275a0aSLisandro Dalcin   PetscFunctionReturn(0);
214880275a0aSLisandro Dalcin }
214980275a0aSLisandro Dalcin 
215080275a0aSLisandro Dalcin /*@
215180275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
215280275a0aSLisandro Dalcin 
215380275a0aSLisandro Dalcin    Logically Collective on TS
215480275a0aSLisandro Dalcin 
215580275a0aSLisandro Dalcin    Input Parameters:
215680275a0aSLisandro Dalcin +  ts - the TS context
215780275a0aSLisandro Dalcin -  steps - number of steps completed so far
215880275a0aSLisandro Dalcin 
215980275a0aSLisandro Dalcin    Notes:
216080275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
216180275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
216280275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
216380275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
216480275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
216580275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
216680275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
216780275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
216880275a0aSLisandro Dalcin 
216980275a0aSLisandro Dalcin    Level: advanced
217080275a0aSLisandro Dalcin 
217180275a0aSLisandro Dalcin .keywords: TS, timestep, set, iteration, number
217280275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
217380275a0aSLisandro Dalcin @*/
217480275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
217580275a0aSLisandro Dalcin {
217680275a0aSLisandro Dalcin   PetscFunctionBegin;
217780275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
217880275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
217980275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
218080275a0aSLisandro Dalcin   ts->steps = steps;
2181d763cef2SBarry Smith   PetscFunctionReturn(0);
2182d763cef2SBarry Smith }
2183d763cef2SBarry Smith 
2184d763cef2SBarry Smith /*@
2185d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2186d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2187d763cef2SBarry Smith 
21883f9fe445SBarry Smith    Logically Collective on TS
2189d763cef2SBarry Smith 
2190d763cef2SBarry Smith    Input Parameters:
2191d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2192d763cef2SBarry Smith -  time_step - the size of the timestep
2193d763cef2SBarry Smith 
2194d763cef2SBarry Smith    Level: intermediate
2195d763cef2SBarry Smith 
2196aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2197d763cef2SBarry Smith 
2198d763cef2SBarry Smith .keywords: TS, set, timestep
2199d763cef2SBarry Smith @*/
22007087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2201d763cef2SBarry Smith {
2202d763cef2SBarry Smith   PetscFunctionBegin;
22030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2204c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2205d763cef2SBarry Smith   ts->time_step = time_step;
2206d763cef2SBarry Smith   PetscFunctionReturn(0);
2207d763cef2SBarry Smith }
2208d763cef2SBarry Smith 
2209a43b19c4SJed Brown /*@
221049354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
221149354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
221249354f04SShri Abhyankar      or just return at the final time TS computed.
2213a43b19c4SJed Brown 
2214a43b19c4SJed Brown   Logically Collective on TS
2215a43b19c4SJed Brown 
2216a43b19c4SJed Brown    Input Parameter:
2217a43b19c4SJed Brown +   ts - the time-step context
221849354f04SShri Abhyankar -   eftopt - exact final time option
2219a43b19c4SJed Brown 
2220feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2221feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2222feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2223feed9e9dSBarry Smith 
2224feed9e9dSBarry Smith    Options Database:
2225feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2226feed9e9dSBarry Smith 
2227ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2228ee346746SBarry Smith     then the final time you selected.
2229ee346746SBarry Smith 
2230a43b19c4SJed Brown    Level: beginner
2231a43b19c4SJed Brown 
2232f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2233a43b19c4SJed Brown @*/
223449354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2235a43b19c4SJed Brown {
2236a43b19c4SJed Brown   PetscFunctionBegin;
2237a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
223849354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
223949354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2240a43b19c4SJed Brown   PetscFunctionReturn(0);
2241a43b19c4SJed Brown }
2242a43b19c4SJed Brown 
2243d763cef2SBarry Smith /*@
2244f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2245f6953c82SLisandro Dalcin 
2246f6953c82SLisandro Dalcin    Not Collective
2247f6953c82SLisandro Dalcin 
2248f6953c82SLisandro Dalcin    Input Parameter:
2249f6953c82SLisandro Dalcin .  ts - the TS context
2250f6953c82SLisandro Dalcin 
2251f6953c82SLisandro Dalcin    Output Parameter:
2252f6953c82SLisandro Dalcin .  eftopt - exact final time option
2253f6953c82SLisandro Dalcin 
2254f6953c82SLisandro Dalcin    Level: beginner
2255f6953c82SLisandro Dalcin 
2256f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2257f6953c82SLisandro Dalcin @*/
2258f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2259f6953c82SLisandro Dalcin {
2260f6953c82SLisandro Dalcin   PetscFunctionBegin;
2261f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2262f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2263f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2264f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2265f6953c82SLisandro Dalcin }
2266f6953c82SLisandro Dalcin 
2267f6953c82SLisandro Dalcin /*@
2268d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2269d763cef2SBarry Smith 
2270d763cef2SBarry Smith    Not Collective
2271d763cef2SBarry Smith 
2272d763cef2SBarry Smith    Input Parameter:
2273d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2274d763cef2SBarry Smith 
2275d763cef2SBarry Smith    Output Parameter:
2276d763cef2SBarry Smith .  dt - the current timestep size
2277d763cef2SBarry Smith 
2278d763cef2SBarry Smith    Level: intermediate
2279d763cef2SBarry Smith 
2280aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2281d763cef2SBarry Smith 
2282d763cef2SBarry Smith .keywords: TS, get, timestep
2283d763cef2SBarry Smith @*/
22847087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2285d763cef2SBarry Smith {
2286d763cef2SBarry Smith   PetscFunctionBegin;
22870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2288f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2289d763cef2SBarry Smith   *dt = ts->time_step;
2290d763cef2SBarry Smith   PetscFunctionReturn(0);
2291d763cef2SBarry Smith }
2292d763cef2SBarry Smith 
2293d8e5e3e6SSatish Balay /*@
2294d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2295d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2296d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2297d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2298d763cef2SBarry Smith 
2299d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2300d763cef2SBarry Smith 
2301d763cef2SBarry Smith    Input Parameter:
2302d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2303d763cef2SBarry Smith 
2304d763cef2SBarry Smith    Output Parameter:
2305d763cef2SBarry Smith .  v - the vector containing the solution
2306d763cef2SBarry Smith 
230763e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
230863e21af5SBarry Smith    final time. It returns the solution at the next timestep.
230963e21af5SBarry Smith 
2310d763cef2SBarry Smith    Level: intermediate
2311d763cef2SBarry Smith 
2312b2bf4f3aSDebojyoti Ghosh .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents()
2313d763cef2SBarry Smith 
2314d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2315d763cef2SBarry Smith @*/
23167087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2317d763cef2SBarry Smith {
2318d763cef2SBarry Smith   PetscFunctionBegin;
23190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23204482741eSBarry Smith   PetscValidPointer(v,2);
23218737fe31SLisandro Dalcin   *v = ts->vec_sol;
2322d763cef2SBarry Smith   PetscFunctionReturn(0);
2323d763cef2SBarry Smith }
2324d763cef2SBarry Smith 
232503fe5f5eSDebojyoti Ghosh /*@
2326b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
232703fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2328b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
232903fe5f5eSDebojyoti Ghosh    structure as the solution vector.
233003fe5f5eSDebojyoti Ghosh 
233103fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
233203fe5f5eSDebojyoti Ghosh 
233303fe5f5eSDebojyoti Ghosh    Parameters :
233403fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2335b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2336b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
233703fe5f5eSDebojyoti Ghosh        returned in v.
2338b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
233903fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2340b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
234103fe5f5eSDebojyoti Ghosh 
23424cdd57e5SDebojyoti Ghosh    Level: advanced
234303fe5f5eSDebojyoti Ghosh 
234403fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
234503fe5f5eSDebojyoti Ghosh 
234603fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
234703fe5f5eSDebojyoti Ghosh @*/
2348b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
234903fe5f5eSDebojyoti Ghosh {
235003fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
235103fe5f5eSDebojyoti Ghosh 
235203fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
235303fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2354b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
235503fe5f5eSDebojyoti Ghosh   else {
2356b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
235703fe5f5eSDebojyoti Ghosh   }
235803fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
235903fe5f5eSDebojyoti Ghosh }
236003fe5f5eSDebojyoti Ghosh 
23614cdd57e5SDebojyoti Ghosh /*@
23624cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
23634cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
23644cdd57e5SDebojyoti Ghosh 
23654cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23664cdd57e5SDebojyoti Ghosh 
23674cdd57e5SDebojyoti Ghosh    Parameters :
23684cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
23694cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
23704cdd57e5SDebojyoti Ghosh 
23714cdd57e5SDebojyoti Ghosh    Level: intermediate
23724cdd57e5SDebojyoti Ghosh 
23734cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
23744cdd57e5SDebojyoti Ghosh 
23754cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
23764cdd57e5SDebojyoti Ghosh @*/
23774cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
23784cdd57e5SDebojyoti Ghosh {
23794cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23804cdd57e5SDebojyoti Ghosh 
23814cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23824cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2383f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
23844cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2385f6356ec7SDebojyoti Ghosh   } else {
2386f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2387f6356ec7SDebojyoti Ghosh   }
23884cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23894cdd57e5SDebojyoti Ghosh }
23904cdd57e5SDebojyoti Ghosh 
23914cdd57e5SDebojyoti Ghosh /*@
23924cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
23934cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
23944cdd57e5SDebojyoti Ghosh 
23954cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23964cdd57e5SDebojyoti Ghosh 
23979657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
23989657682dSDebojyoti Ghosh 
23994cdd57e5SDebojyoti Ghosh    Parameters :
24004cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2401657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
24024cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
24034cdd57e5SDebojyoti Ghosh 
24044cdd57e5SDebojyoti Ghosh    Level: intermediate
24054cdd57e5SDebojyoti Ghosh 
240657df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
24074cdd57e5SDebojyoti Ghosh 
24084cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
24094cdd57e5SDebojyoti Ghosh @*/
24100a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
24114cdd57e5SDebojyoti Ghosh {
24124cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24134cdd57e5SDebojyoti Ghosh 
24144cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24154cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2416f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
24170a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2418f6356ec7SDebojyoti Ghosh   } else {
2419f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2420f6356ec7SDebojyoti Ghosh   }
24214cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24224cdd57e5SDebojyoti Ghosh }
24234cdd57e5SDebojyoti Ghosh 
242457df6a1bSDebojyoti Ghosh /*@
242557df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
242657df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
242757df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
242857df6a1bSDebojyoti Ghosh 
242957df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
243057df6a1bSDebojyoti Ghosh 
243157df6a1bSDebojyoti Ghosh    Parameters :
243257df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
243357df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
243457df6a1bSDebojyoti Ghosh 
243557df6a1bSDebojyoti Ghosh    Level: intermediate
243657df6a1bSDebojyoti Ghosh 
243757df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
243857df6a1bSDebojyoti Ghosh 
243957df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
244057df6a1bSDebojyoti Ghosh @*/
244157df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
244257df6a1bSDebojyoti Ghosh {
244357df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
244457df6a1bSDebojyoti Ghosh 
244557df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
244657df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24479657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
244857df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
244957df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
245057df6a1bSDebojyoti Ghosh   }
245157df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
245257df6a1bSDebojyoti Ghosh }
245357df6a1bSDebojyoti Ghosh 
2454c235aa8dSHong Zhang /*@
2455dfb21088SHong Zhang    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()
2456c235aa8dSHong Zhang 
2457c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
2458c235aa8dSHong Zhang 
2459c235aa8dSHong Zhang    Input Parameter:
2460c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
2461c235aa8dSHong Zhang 
2462c235aa8dSHong Zhang    Output Parameter:
2463abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
2464abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
2465c235aa8dSHong Zhang 
2466c235aa8dSHong Zhang    Level: intermediate
2467c235aa8dSHong Zhang 
2468c235aa8dSHong Zhang .seealso: TSGetTimeStep()
2469c235aa8dSHong Zhang 
2470c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
2471c235aa8dSHong Zhang @*/
2472dfb21088SHong Zhang PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
2473c235aa8dSHong Zhang {
2474c235aa8dSHong Zhang   PetscFunctionBegin;
2475c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2476abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
2477abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
2478abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
2479c235aa8dSHong Zhang   PetscFunctionReturn(0);
2480c235aa8dSHong Zhang }
2481c235aa8dSHong Zhang 
2482bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2483d8e5e3e6SSatish Balay /*@
2484bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2485d763cef2SBarry Smith 
2486bdad233fSMatthew Knepley   Not collective
2487d763cef2SBarry Smith 
2488bdad233fSMatthew Knepley   Input Parameters:
2489bdad233fSMatthew Knepley + ts   - The TS
2490bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2491d763cef2SBarry Smith .vb
24920910c330SBarry Smith          U_t - A U = 0      (linear)
24930910c330SBarry Smith          U_t - A(t) U = 0   (linear)
24940910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2495d763cef2SBarry Smith .ve
2496d763cef2SBarry Smith 
2497d763cef2SBarry Smith    Level: beginner
2498d763cef2SBarry Smith 
2499bdad233fSMatthew Knepley .keywords: TS, problem type
2500bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2501d763cef2SBarry Smith @*/
25027087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2503a7cc72afSBarry Smith {
25049e2a6581SJed Brown   PetscErrorCode ierr;
25059e2a6581SJed Brown 
2506d763cef2SBarry Smith   PetscFunctionBegin;
25070700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2508bdad233fSMatthew Knepley   ts->problem_type = type;
25099e2a6581SJed Brown   if (type == TS_LINEAR) {
25109e2a6581SJed Brown     SNES snes;
25119e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
25129e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
25139e2a6581SJed Brown   }
2514d763cef2SBarry Smith   PetscFunctionReturn(0);
2515d763cef2SBarry Smith }
2516d763cef2SBarry Smith 
2517bdad233fSMatthew Knepley /*@C
2518bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2519bdad233fSMatthew Knepley 
2520bdad233fSMatthew Knepley   Not collective
2521bdad233fSMatthew Knepley 
2522bdad233fSMatthew Knepley   Input Parameter:
2523bdad233fSMatthew Knepley . ts   - The TS
2524bdad233fSMatthew Knepley 
2525bdad233fSMatthew Knepley   Output Parameter:
2526bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2527bdad233fSMatthew Knepley .vb
2528089b2837SJed Brown          M U_t = A U
2529089b2837SJed Brown          M(t) U_t = A(t) U
2530b5abc632SBarry Smith          F(t,U,U_t)
2531bdad233fSMatthew Knepley .ve
2532bdad233fSMatthew Knepley 
2533bdad233fSMatthew Knepley    Level: beginner
2534bdad233fSMatthew Knepley 
2535bdad233fSMatthew Knepley .keywords: TS, problem type
2536bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2537bdad233fSMatthew Knepley @*/
25387087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2539a7cc72afSBarry Smith {
2540bdad233fSMatthew Knepley   PetscFunctionBegin;
25410700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
25424482741eSBarry Smith   PetscValidIntPointer(type,2);
2543bdad233fSMatthew Knepley   *type = ts->problem_type;
2544bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2545bdad233fSMatthew Knepley }
2546d763cef2SBarry Smith 
2547d763cef2SBarry Smith /*@
2548d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2549d763cef2SBarry Smith    of a timestepper.
2550d763cef2SBarry Smith 
2551d763cef2SBarry Smith    Collective on TS
2552d763cef2SBarry Smith 
2553d763cef2SBarry Smith    Input Parameter:
2554d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2555d763cef2SBarry Smith 
2556d763cef2SBarry Smith    Notes:
2557d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2558d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2559d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
2560d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2561d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
2562d763cef2SBarry Smith 
2563d763cef2SBarry Smith    Level: advanced
2564d763cef2SBarry Smith 
2565d763cef2SBarry Smith .keywords: TS, timestep, setup
2566d763cef2SBarry Smith 
2567d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
2568d763cef2SBarry Smith @*/
25697087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2570d763cef2SBarry Smith {
2571dfbe8321SBarry Smith   PetscErrorCode ierr;
25726c6b9e74SPeter Brune   DM             dm;
25736c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2574d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2575cd11d68dSLisandro Dalcin   TSIFunction    ifun;
25766c6b9e74SPeter Brune   TSIJacobian    ijac;
2577efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
25786c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
25792ffb9264SLisandro Dalcin   PetscBool      isnone;
2580d763cef2SBarry Smith 
2581d763cef2SBarry Smith   PetscFunctionBegin;
25820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2583277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2584277b19d0SLisandro Dalcin 
25857adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2586cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2587cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2588d763cef2SBarry Smith   }
2589277b19d0SLisandro Dalcin 
2590277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2591277b19d0SLisandro Dalcin 
2592e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
2593e1244c69SJed Brown     Mat Amat,Pmat;
2594e1244c69SJed Brown     SNES snes;
2595e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2596e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2597e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2598e1244c69SJed Brown      * have displaced the RHS matrix */
2599e1244c69SJed Brown     if (Amat == ts->Arhs) {
2600abc0d4abSBarry Smith       /* we need to copy the values of the matrix because for the constant Jacobian case the user will never set the numerical values in this new location */
2601abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2602e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2603e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2604e1244c69SJed Brown     }
2605e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2606abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2607e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2608e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2609e1244c69SJed Brown     }
2610e1244c69SJed Brown   }
26112ffb9264SLisandro Dalcin 
26122ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
26132ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
26142ffb9264SLisandro Dalcin 
2615277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2616000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2617277b19d0SLisandro Dalcin   }
2618277b19d0SLisandro Dalcin 
26192ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
26202ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
26212ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
26222ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
26232ffb9264SLisandro Dalcin 
2624a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
26256c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
26266c6b9e74SPeter Brune    */
26276c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
26280298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
26296c6b9e74SPeter Brune   if (!func) {
26306c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
26316c6b9e74SPeter Brune   }
2632a6772fa2SLisandro 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.
26336c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
26346c6b9e74SPeter Brune    */
26350298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
26360298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2637efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
26380298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2639efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
26406c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
26416c6b9e74SPeter Brune   }
2642c0517034SDebojyoti Ghosh 
2643c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2644c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2645c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2646c0517034SDebojyoti Ghosh   }
2647c0517034SDebojyoti Ghosh 
2648277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2649277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2650277b19d0SLisandro Dalcin }
2651277b19d0SLisandro Dalcin 
2652f6a906c0SBarry Smith /*@
2653f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
2654f6a906c0SBarry Smith    of an adjoint solver
2655f6a906c0SBarry Smith 
2656f6a906c0SBarry Smith    Collective on TS
2657f6a906c0SBarry Smith 
2658f6a906c0SBarry Smith    Input Parameter:
2659f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
2660f6a906c0SBarry Smith 
2661f6a906c0SBarry Smith    Level: advanced
2662f6a906c0SBarry Smith 
2663f6a906c0SBarry Smith .keywords: TS, timestep, setup
2664f6a906c0SBarry Smith 
2665947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
2666f6a906c0SBarry Smith @*/
2667f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
2668f6a906c0SBarry Smith {
2669f6a906c0SBarry Smith   PetscErrorCode ierr;
2670f6a906c0SBarry Smith 
2671f6a906c0SBarry Smith   PetscFunctionBegin;
2672f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2673f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
267423706b9aSHong Zhang   if (!ts->vecs_sensi) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
2675c5767687SHong Zhang   if (ts->vecs_sensip && !ts->Jacp) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetRHSJacobian() first");
2676d8151eeaSBarry Smith 
2677947abb85SHong Zhang   if (ts->vec_costintegral) { /* if there is integral in the cost function */
2678d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2679d8151eeaSBarry Smith     if (ts->vecs_sensip){
2680d8151eeaSBarry Smith       ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2681d8151eeaSBarry Smith     }
2682947abb85SHong Zhang   }
2683d8151eeaSBarry Smith 
268442f2b339SBarry Smith   if (ts->ops->adjointsetup) {
268542f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
2686f6a906c0SBarry Smith   }
2687f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
2688f6a906c0SBarry Smith   PetscFunctionReturn(0);
2689f6a906c0SBarry Smith }
2690f6a906c0SBarry Smith 
2691277b19d0SLisandro Dalcin /*@
2692277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2693277b19d0SLisandro Dalcin 
2694277b19d0SLisandro Dalcin    Collective on TS
2695277b19d0SLisandro Dalcin 
2696277b19d0SLisandro Dalcin    Input Parameter:
2697277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2698277b19d0SLisandro Dalcin 
2699277b19d0SLisandro Dalcin    Level: beginner
2700277b19d0SLisandro Dalcin 
2701277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2702277b19d0SLisandro Dalcin 
2703277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2704277b19d0SLisandro Dalcin @*/
2705277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2706277b19d0SLisandro Dalcin {
2707277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2708277b19d0SLisandro Dalcin 
2709277b19d0SLisandro Dalcin   PetscFunctionBegin;
2710277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2711b18ea86cSHong Zhang 
2712277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2713277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2714277b19d0SLisandro Dalcin   }
2715277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2716e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2717bbd56ea5SKarl Rupp 
27184e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
27194e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2720214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
27216bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2722efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2723e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2724e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
272538637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2726bbd56ea5SKarl Rupp 
2727d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2728d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2729715f1b00SHong Zhang 
2730ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
27312c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
273236eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2733715f1b00SHong Zhang 
2734022c081aSHong Zhang   ierr = PetscFree(ts->vecs_fwdsensipacked);CHKERRQ(ierr);
2735022c081aSHong Zhang 
2736277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2737d763cef2SBarry Smith   PetscFunctionReturn(0);
2738d763cef2SBarry Smith }
2739d763cef2SBarry Smith 
2740d8e5e3e6SSatish Balay /*@
2741d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2742d763cef2SBarry Smith    with TSCreate().
2743d763cef2SBarry Smith 
2744d763cef2SBarry Smith    Collective on TS
2745d763cef2SBarry Smith 
2746d763cef2SBarry Smith    Input Parameter:
2747d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2748d763cef2SBarry Smith 
2749d763cef2SBarry Smith    Level: beginner
2750d763cef2SBarry Smith 
2751d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2752d763cef2SBarry Smith 
2753d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2754d763cef2SBarry Smith @*/
27556bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2756d763cef2SBarry Smith {
27576849ba73SBarry Smith   PetscErrorCode ierr;
2758d763cef2SBarry Smith 
2759d763cef2SBarry Smith   PetscFunctionBegin;
27606bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
27616bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
27626bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2763d763cef2SBarry Smith 
27646bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2765277b19d0SLisandro Dalcin 
2766e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2767e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
27686bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
27696d4c513bSLisandro Dalcin 
2770bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2771bc952696SBarry Smith 
277284df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
27736427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
27746427ac75SLisandro Dalcin 
27756bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
27766bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
27776bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
27780dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
27796d4c513bSLisandro Dalcin 
2780a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2781d763cef2SBarry Smith   PetscFunctionReturn(0);
2782d763cef2SBarry Smith }
2783d763cef2SBarry Smith 
2784d8e5e3e6SSatish Balay /*@
2785d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2786d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2787d763cef2SBarry Smith 
2788d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2789d763cef2SBarry Smith 
2790d763cef2SBarry Smith    Input Parameter:
2791d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2792d763cef2SBarry Smith 
2793d763cef2SBarry Smith    Output Parameter:
2794d763cef2SBarry Smith .  snes - the nonlinear solver context
2795d763cef2SBarry Smith 
2796d763cef2SBarry Smith    Notes:
2797d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2798d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
279994b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2800d763cef2SBarry Smith 
2801d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
28020298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2803d763cef2SBarry Smith 
2804d763cef2SBarry Smith    Level: beginner
2805d763cef2SBarry Smith 
2806d763cef2SBarry Smith .keywords: timestep, get, SNES
2807d763cef2SBarry Smith @*/
28087087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2809d763cef2SBarry Smith {
2810d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2811d372ba47SLisandro Dalcin 
2812d763cef2SBarry Smith   PetscFunctionBegin;
28130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28144482741eSBarry Smith   PetscValidPointer(snes,2);
2815d372ba47SLisandro Dalcin   if (!ts->snes) {
2816ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
28170298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28183bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2819d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2820496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
28219e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
28229e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
28239e2a6581SJed Brown     }
2824d372ba47SLisandro Dalcin   }
2825d763cef2SBarry Smith   *snes = ts->snes;
2826d763cef2SBarry Smith   PetscFunctionReturn(0);
2827d763cef2SBarry Smith }
2828d763cef2SBarry Smith 
2829deb2cd25SJed Brown /*@
2830deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2831deb2cd25SJed Brown 
2832deb2cd25SJed Brown    Collective
2833deb2cd25SJed Brown 
2834deb2cd25SJed Brown    Input Parameter:
2835deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2836deb2cd25SJed Brown -  snes - the nonlinear solver context
2837deb2cd25SJed Brown 
2838deb2cd25SJed Brown    Notes:
2839deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2840deb2cd25SJed Brown 
2841deb2cd25SJed Brown    Level: developer
2842deb2cd25SJed Brown 
2843deb2cd25SJed Brown .keywords: timestep, set, SNES
2844deb2cd25SJed Brown @*/
2845deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2846deb2cd25SJed Brown {
2847deb2cd25SJed Brown   PetscErrorCode ierr;
2848d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2849deb2cd25SJed Brown 
2850deb2cd25SJed Brown   PetscFunctionBegin;
2851deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2852deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2853deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2854deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2855bbd56ea5SKarl Rupp 
2856deb2cd25SJed Brown   ts->snes = snes;
2857bbd56ea5SKarl Rupp 
28580298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28590298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2860740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
28610298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2862740132f1SEmil Constantinescu   }
2863deb2cd25SJed Brown   PetscFunctionReturn(0);
2864deb2cd25SJed Brown }
2865deb2cd25SJed Brown 
2866d8e5e3e6SSatish Balay /*@
286794b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2868d763cef2SBarry Smith    a TS (timestepper) context.
2869d763cef2SBarry Smith 
287094b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2871d763cef2SBarry Smith 
2872d763cef2SBarry Smith    Input Parameter:
2873d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2874d763cef2SBarry Smith 
2875d763cef2SBarry Smith    Output Parameter:
287694b7f48cSBarry Smith .  ksp - the nonlinear solver context
2877d763cef2SBarry Smith 
2878d763cef2SBarry Smith    Notes:
287994b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2880d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2881d763cef2SBarry Smith    KSP and PC contexts as well.
2882d763cef2SBarry Smith 
288394b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
28840298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2885d763cef2SBarry Smith 
2886d763cef2SBarry Smith    Level: beginner
2887d763cef2SBarry Smith 
288894b7f48cSBarry Smith .keywords: timestep, get, KSP
2889d763cef2SBarry Smith @*/
28907087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2891d763cef2SBarry Smith {
2892d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2893089b2837SJed Brown   SNES           snes;
2894d372ba47SLisandro Dalcin 
2895d763cef2SBarry Smith   PetscFunctionBegin;
28960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28974482741eSBarry Smith   PetscValidPointer(ksp,2);
289817186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2899e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2900089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2901089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2902d763cef2SBarry Smith   PetscFunctionReturn(0);
2903d763cef2SBarry Smith }
2904d763cef2SBarry Smith 
2905d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2906d763cef2SBarry Smith 
2907adb62b0dSMatthew Knepley /*@
2908618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2909618ce8baSLisandro Dalcin 
2910618ce8baSLisandro Dalcin    Logically Collective on TS
2911618ce8baSLisandro Dalcin 
2912618ce8baSLisandro Dalcin    Input Parameters:
2913618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2914618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2915618ce8baSLisandro Dalcin 
2916618ce8baSLisandro Dalcin    Options Database Keys:
2917618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2918618ce8baSLisandro Dalcin 
2919618ce8baSLisandro Dalcin    Notes:
2920618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2921618ce8baSLisandro Dalcin 
2922618ce8baSLisandro Dalcin    Level: intermediate
2923618ce8baSLisandro Dalcin 
2924618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, steps
2925618ce8baSLisandro Dalcin 
2926618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2927618ce8baSLisandro Dalcin @*/
2928618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2929618ce8baSLisandro Dalcin {
2930618ce8baSLisandro Dalcin   PetscFunctionBegin;
2931618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2932618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2933618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2934618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2935618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2936618ce8baSLisandro Dalcin }
2937618ce8baSLisandro Dalcin 
2938618ce8baSLisandro Dalcin /*@
2939618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2940618ce8baSLisandro Dalcin 
2941618ce8baSLisandro Dalcin    Not Collective
2942618ce8baSLisandro Dalcin 
2943618ce8baSLisandro Dalcin    Input Parameters:
2944618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2945618ce8baSLisandro Dalcin 
2946618ce8baSLisandro Dalcin    Output Parameter:
2947618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2948618ce8baSLisandro Dalcin 
2949618ce8baSLisandro Dalcin    Level: advanced
2950618ce8baSLisandro Dalcin 
2951618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, steps
2952618ce8baSLisandro Dalcin 
2953618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2954618ce8baSLisandro Dalcin @*/
2955618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2956618ce8baSLisandro Dalcin {
2957618ce8baSLisandro Dalcin   PetscFunctionBegin;
2958618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2959618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
2960618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
2961618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2962618ce8baSLisandro Dalcin }
2963618ce8baSLisandro Dalcin 
2964618ce8baSLisandro Dalcin /*@
2965618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
2966618ce8baSLisandro Dalcin 
2967618ce8baSLisandro Dalcin    Logically Collective on TS
2968618ce8baSLisandro Dalcin 
2969618ce8baSLisandro Dalcin    Input Parameters:
2970618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2971618ce8baSLisandro Dalcin -  maxtime - final time to step to
2972618ce8baSLisandro Dalcin 
2973618ce8baSLisandro Dalcin    Options Database Keys:
2974ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
2975618ce8baSLisandro Dalcin 
2976618ce8baSLisandro Dalcin    Notes:
2977618ce8baSLisandro Dalcin    The default maximum time is 5.0
2978618ce8baSLisandro Dalcin 
2979618ce8baSLisandro Dalcin    Level: intermediate
2980618ce8baSLisandro Dalcin 
2981618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, time
2982618ce8baSLisandro Dalcin 
2983618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
2984618ce8baSLisandro Dalcin @*/
2985618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
2986618ce8baSLisandro Dalcin {
2987618ce8baSLisandro Dalcin   PetscFunctionBegin;
2988618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2989618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
2990618ce8baSLisandro Dalcin   ts->max_time = maxtime;
2991618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2992618ce8baSLisandro Dalcin }
2993618ce8baSLisandro Dalcin 
2994618ce8baSLisandro Dalcin /*@
2995618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
2996618ce8baSLisandro Dalcin 
2997618ce8baSLisandro Dalcin    Not Collective
2998618ce8baSLisandro Dalcin 
2999618ce8baSLisandro Dalcin    Input Parameters:
3000618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3001618ce8baSLisandro Dalcin 
3002618ce8baSLisandro Dalcin    Output Parameter:
3003618ce8baSLisandro Dalcin .  maxtime - final time to step to
3004618ce8baSLisandro Dalcin 
3005618ce8baSLisandro Dalcin    Level: advanced
3006618ce8baSLisandro Dalcin 
3007618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, time
3008618ce8baSLisandro Dalcin 
3009618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3010618ce8baSLisandro Dalcin @*/
3011618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3012618ce8baSLisandro Dalcin {
3013618ce8baSLisandro Dalcin   PetscFunctionBegin;
3014618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3015618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3016618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3017618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3018618ce8baSLisandro Dalcin }
3019618ce8baSLisandro Dalcin 
3020618ce8baSLisandro Dalcin /*@
3021aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3022aaa6c58dSLisandro Dalcin @*/
3023aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3024aaa6c58dSLisandro Dalcin {
3025aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3026aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3027aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3028aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3029aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3030aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3031aaa6c58dSLisandro Dalcin }
3032aaa6c58dSLisandro Dalcin 
3033aaa6c58dSLisandro Dalcin /*@
303419eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3035adb62b0dSMatthew Knepley @*/
30367087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3037adb62b0dSMatthew Knepley {
3038adb62b0dSMatthew Knepley   PetscFunctionBegin;
30390700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3040abc0a331SBarry Smith   if (maxsteps) {
30414482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3042adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3043adb62b0dSMatthew Knepley   }
3044abc0a331SBarry Smith   if (maxtime) {
30454482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3046adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3047adb62b0dSMatthew Knepley   }
3048adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3049adb62b0dSMatthew Knepley }
3050adb62b0dSMatthew Knepley 
3051d763cef2SBarry Smith /*@
305219eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3053d763cef2SBarry Smith @*/
30547087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3055d763cef2SBarry Smith {
3056d763cef2SBarry Smith   PetscFunctionBegin;
30570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3058c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3059c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
306039b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
306139b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3062d763cef2SBarry Smith   PetscFunctionReturn(0);
3063d763cef2SBarry Smith }
3064d763cef2SBarry Smith 
3065d763cef2SBarry Smith /*@
30665c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
30675c5f5948SLisandro Dalcin @*/
3068*e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
30695c5f5948SLisandro Dalcin 
307019eac22cSLisandro Dalcin /*@
3071d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3072d763cef2SBarry Smith    for use by the TS routines.
3073d763cef2SBarry Smith 
30743f9fe445SBarry Smith    Logically Collective on TS and Vec
3075d763cef2SBarry Smith 
3076d763cef2SBarry Smith    Input Parameters:
3077d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
30780910c330SBarry Smith -  u - the solution vector
3079d763cef2SBarry Smith 
3080d763cef2SBarry Smith    Level: beginner
3081d763cef2SBarry Smith 
3082715f1b00SHong Zhang .keywords: TS, timestep, set, solution, initial values
3083d763cef2SBarry Smith @*/
30840910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3085d763cef2SBarry Smith {
30868737fe31SLisandro Dalcin   PetscErrorCode ierr;
3087496e6a7aSJed Brown   DM             dm;
30888737fe31SLisandro Dalcin 
3089d763cef2SBarry Smith   PetscFunctionBegin;
30900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30910910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
30920910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
30936bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
30940910c330SBarry Smith   ts->vec_sol = u;
3095bbd56ea5SKarl Rupp 
3096496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
30970910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3098d763cef2SBarry Smith   PetscFunctionReturn(0);
3099d763cef2SBarry Smith }
3100d763cef2SBarry Smith 
310173b18844SBarry Smith /*@
310273b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
310373b18844SBarry Smith 
310473b18844SBarry Smith    Logically Collective on TS
310573b18844SBarry Smith 
310673b18844SBarry Smith    Input Parameters:
310773b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
310873b18844SBarry Smith .  steps - number of steps to use
310973b18844SBarry Smith 
311073b18844SBarry Smith    Level: intermediate
311173b18844SBarry Smith 
311273b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
311373b18844SBarry Smith           so as to integrate back to less than the original timestep
311473b18844SBarry Smith 
311573b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
311673b18844SBarry Smith 
311773b18844SBarry Smith .seealso: TSSetExactFinalTime()
311873b18844SBarry Smith @*/
311973b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
312073b18844SBarry Smith {
312173b18844SBarry Smith   PetscFunctionBegin;
312273b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
312373b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
312473b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
31252808aa04SLisandro Dalcin   if (steps > ts->steps) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back more than the total number of forward steps");
312673b18844SBarry Smith   ts->adjoint_max_steps = steps;
312773b18844SBarry Smith   PetscFunctionReturn(0);
312873b18844SBarry Smith }
312973b18844SBarry Smith 
3130c235aa8dSHong Zhang /*@
3131715f1b00SHong Zhang    TSSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial values and w.r.t. the problem parameters
31320cf82b59SBarry Smith       for use by the TSAdjoint routines.
3133c235aa8dSHong Zhang 
3134c235aa8dSHong Zhang    Logically Collective on TS and Vec
3135c235aa8dSHong Zhang 
3136c235aa8dSHong Zhang    Input Parameters:
3137c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
3138abc2977eSBarry 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
3139abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
3140c235aa8dSHong Zhang 
3141c235aa8dSHong Zhang    Level: beginner
3142c235aa8dSHong Zhang 
3143abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
31440cf82b59SBarry Smith 
3145715f1b00SHong Zhang .keywords: TS, timestep, set, sensitivity, initial values
3146c235aa8dSHong Zhang @*/
3147dfb21088SHong Zhang PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
3148c235aa8dSHong Zhang {
3149c235aa8dSHong Zhang   PetscFunctionBegin;
3150c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3151abc2977eSBarry Smith   PetscValidPointer(lambda,2);
3152abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
3153abc2977eSBarry Smith   ts->vecs_sensip = mu;
3154dfb21088SHong 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");
3155abc2977eSBarry Smith   ts->numcost  = numcost;
3156ad8e2604SHong Zhang   PetscFunctionReturn(0);
3157ad8e2604SHong Zhang }
3158ad8e2604SHong Zhang 
3159ad8e2604SHong Zhang /*@C
31600cf82b59SBarry 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.
3161ad8e2604SHong Zhang 
3162ad8e2604SHong Zhang   Logically Collective on TS
3163ad8e2604SHong Zhang 
3164ad8e2604SHong Zhang   Input Parameters:
3165ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
3166ad8e2604SHong Zhang - func - The function
3167ad8e2604SHong Zhang 
3168ad8e2604SHong Zhang   Calling sequence of func:
31690cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
317005755b9cSHong Zhang +   t - current timestep
31710cf82b59SBarry Smith .   y - input vector (current ODE solution)
317205755b9cSHong Zhang .   A - output matrix
317305755b9cSHong Zhang -   ctx - [optional] user-defined function context
3174ad8e2604SHong Zhang 
3175ad8e2604SHong Zhang   Level: intermediate
3176ad8e2604SHong Zhang 
31770cf82b59SBarry 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
31780cf82b59SBarry Smith 
3179ad8e2604SHong Zhang .keywords: TS, sensitivity
3180ad8e2604SHong Zhang .seealso:
3181ad8e2604SHong Zhang @*/
31825bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
3183ad8e2604SHong Zhang {
3184ad8e2604SHong Zhang   PetscErrorCode ierr;
3185ad8e2604SHong Zhang 
3186ad8e2604SHong Zhang   PetscFunctionBegin;
3187ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
318823706b9aSHong Zhang   PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
3189ad8e2604SHong Zhang 
3190ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
3191ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
3192ad8e2604SHong Zhang   if(Amat) {
3193ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
3194ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
3195ad8e2604SHong Zhang     ts->Jacp = Amat;
3196ad8e2604SHong Zhang   }
3197ad8e2604SHong Zhang   PetscFunctionReturn(0);
3198ad8e2604SHong Zhang }
3199ad8e2604SHong Zhang 
32000cf82b59SBarry Smith /*@C
32015bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
3202ad8e2604SHong Zhang 
3203ad8e2604SHong Zhang   Collective on TS
3204ad8e2604SHong Zhang 
3205ad8e2604SHong Zhang   Input Parameters:
3206ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
3207ad8e2604SHong Zhang 
3208ad8e2604SHong Zhang   Level: developer
3209ad8e2604SHong Zhang 
3210ad8e2604SHong Zhang .keywords: TS, sensitivity
32115bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
3212ad8e2604SHong Zhang @*/
32135bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
3214ad8e2604SHong Zhang {
3215ad8e2604SHong Zhang   PetscErrorCode ierr;
3216ad8e2604SHong Zhang 
3217ad8e2604SHong Zhang   PetscFunctionBegin;
3218ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3219ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3220ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
3221ad8e2604SHong Zhang 
3222ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
3223ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
3224ad8e2604SHong Zhang   PetscStackPop;
3225c235aa8dSHong Zhang   PetscFunctionReturn(0);
3226c235aa8dSHong Zhang }
3227c235aa8dSHong Zhang 
32286fd0887fSHong Zhang /*@C
3229dfb21088SHong Zhang     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
32306fd0887fSHong Zhang 
32316fd0887fSHong Zhang     Logically Collective on TS
32326fd0887fSHong Zhang 
32336fd0887fSHong Zhang     Input Parameters:
32346fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
3235abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
32363d1d12c6SHong Zhang .   costintegral - vector that stores the integral values
32370cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
3238b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
3239b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
3240feaa1ddbSSatish Balay .   fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
3241b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
32426fd0887fSHong Zhang 
32430cf82b59SBarry Smith     Calling sequence of rf:
32443d1d12c6SHong Zhang $   PetscErrorCode rf(TS ts,PetscReal t,Vec y,Vec f,void *ctx);
32456fd0887fSHong Zhang 
3246b612ec54SBarry Smith     Calling sequence of drdyf:
32470cf82b59SBarry Smith $   PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
3248b612ec54SBarry Smith 
3249b612ec54SBarry Smith     Calling sequence of drdpf:
32500cf82b59SBarry Smith $   PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
3251b612ec54SBarry Smith 
3252b612ec54SBarry Smith     Level: intermediate
32536fd0887fSHong Zhang 
3254715f1b00SHong Zhang     Notes: For optimization there is usually a single cost function (numcost = 1). For sensitivities there may be multiple cost functions
32550cf82b59SBarry Smith 
32566fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
32576fd0887fSHong Zhang 
3258dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
32596fd0887fSHong Zhang @*/
32603d1d12c6SHong Zhang PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost,Vec costintegral,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
3261d8151eeaSBarry Smith                                                           PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
3262b1cb36f3SHong Zhang                                                           PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),
3263b1cb36f3SHong Zhang                                                           PetscBool fwd,void *ctx)
32646fd0887fSHong Zhang {
32656fd0887fSHong Zhang   PetscErrorCode ierr;
32666fd0887fSHong Zhang 
32676fd0887fSHong Zhang   PetscFunctionBegin;
32686fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32693d1d12c6SHong Zhang   if (costintegral) PetscValidHeaderSpecific(costintegral,VEC_CLASSID,3);
3270715f1b00SHong 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() or TSForwardSetIntegralGradients()");
3271c72ed514SHong Zhang   if (!ts->numcost) ts->numcost=numcost;
32726fd0887fSHong Zhang 
32733d1d12c6SHong Zhang   if (costintegral) {
32743d1d12c6SHong Zhang     ierr = PetscObjectReference((PetscObject)costintegral);CHKERRQ(ierr);
32753d1d12c6SHong Zhang     ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
32763d1d12c6SHong Zhang     ts->vec_costintegral = costintegral;
3277afe7b317SHong Zhang   } else {
3278afe7b317SHong Zhang     if (!ts->vec_costintegral) { /* Create a seq vec if user does not provide one */
3279afe7b317SHong Zhang       ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
3280afe7b317SHong Zhang     } else {
3281afe7b317SHong Zhang       ierr = VecSet(ts->vec_costintegral,0.0);CHKERRQ(ierr);
3282afe7b317SHong Zhang     }
3283afe7b317SHong Zhang   }
3284afe7b317SHong Zhang   if (!ts->vec_costintegrand) {
32852c39e106SBarry Smith     ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
3286afe7b317SHong Zhang   } else {
3287afe7b317SHong Zhang     ierr = VecSet(ts->vec_costintegrand,0.0);CHKERRQ(ierr);
3288afe7b317SHong Zhang   }
3289afe7b317SHong Zhang   ts->costintegralfwd  = fwd; /* Evaluate the cost integral in forward run if fwd is true */
32900cf82b59SBarry Smith   ts->costintegrand    = rf;
329105755b9cSHong Zhang   ts->costintegrandctx = ctx;
3292b612ec54SBarry Smith   ts->drdyfunction     = drdyf;
3293b612ec54SBarry Smith   ts->drdpfunction     = drdpf;
32946fd0887fSHong Zhang   PetscFunctionReturn(0);
32956fd0887fSHong Zhang }
32966fd0887fSHong Zhang 
329736eaed60SHong Zhang /*@
3298dfb21088SHong Zhang    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
329936eaed60SHong Zhang    It is valid to call the routine after a backward run.
330036eaed60SHong Zhang 
330136eaed60SHong Zhang    Not Collective
330236eaed60SHong Zhang 
330336eaed60SHong Zhang    Input Parameter:
330436eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
330536eaed60SHong Zhang 
330636eaed60SHong Zhang    Output Parameter:
33072c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
330836eaed60SHong Zhang 
330936eaed60SHong Zhang    Level: intermediate
331036eaed60SHong Zhang 
3311dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
331236eaed60SHong Zhang 
331336eaed60SHong Zhang .keywords: TS, sensitivity analysis
331436eaed60SHong Zhang @*/
3315dfb21088SHong Zhang PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
331636eaed60SHong Zhang {
331736eaed60SHong Zhang   PetscFunctionBegin;
331836eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
331936eaed60SHong Zhang   PetscValidPointer(v,2);
33202c39e106SBarry Smith   *v = ts->vec_costintegral;
332136eaed60SHong Zhang   PetscFunctionReturn(0);
332236eaed60SHong Zhang }
332336eaed60SHong Zhang 
33246fd0887fSHong Zhang /*@
3325022c081aSHong Zhang    TSComputeCostIntegrand - Evaluates the integral function in the cost functions.
33266fd0887fSHong Zhang 
33276fd0887fSHong Zhang    Input Parameters:
33286fd0887fSHong Zhang +  ts - the TS context
33296fd0887fSHong Zhang .  t - current time
33300cf82b59SBarry Smith -  y - state vector, i.e. current solution
33316fd0887fSHong Zhang 
33326fd0887fSHong Zhang    Output Parameter:
3333abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
33346fd0887fSHong Zhang 
33356fd0887fSHong Zhang    Note:
33366fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
33376fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
33386fd0887fSHong Zhang 
33396fd0887fSHong Zhang    Level: developer
33406fd0887fSHong Zhang 
33416fd0887fSHong Zhang .keywords: TS, compute
33426fd0887fSHong Zhang 
3343dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
33446fd0887fSHong Zhang @*/
3345022c081aSHong Zhang PetscErrorCode TSComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
33466fd0887fSHong Zhang {
33476fd0887fSHong Zhang   PetscErrorCode ierr;
33486fd0887fSHong Zhang 
33496fd0887fSHong Zhang   PetscFunctionBegin;
33506fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33510cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
33526fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
33536fd0887fSHong Zhang 
33540cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
3355e4680132SHong Zhang   if (ts->costintegrand) {
3356e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
33570cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
33586fd0887fSHong Zhang     PetscStackPop;
33596fd0887fSHong Zhang   } else {
33606fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
33616fd0887fSHong Zhang   }
33626fd0887fSHong Zhang 
33630cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
33646fd0887fSHong Zhang   PetscFunctionReturn(0);
33656fd0887fSHong Zhang }
33666fd0887fSHong Zhang 
336705755b9cSHong Zhang /*@
3368d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
336905755b9cSHong Zhang 
337005755b9cSHong Zhang   Collective on TS
337105755b9cSHong Zhang 
337205755b9cSHong Zhang   Input Parameters:
337305755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
337405755b9cSHong Zhang 
337505755b9cSHong Zhang   Notes:
3376d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
337705755b9cSHong Zhang   so most users would not generally call this routine themselves.
337805755b9cSHong Zhang 
337905755b9cSHong Zhang   Level: developer
338005755b9cSHong Zhang 
338105755b9cSHong Zhang .keywords: TS, sensitivity
3382d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
338305755b9cSHong Zhang @*/
33840cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
338505755b9cSHong Zhang {
338605755b9cSHong Zhang   PetscErrorCode ierr;
338705755b9cSHong Zhang 
338805755b9cSHong Zhang   PetscFunctionBegin;
338905755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33900cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
339105755b9cSHong Zhang 
339205755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
33930cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
339405755b9cSHong Zhang   PetscStackPop;
339505755b9cSHong Zhang   PetscFunctionReturn(0);
339605755b9cSHong Zhang }
339705755b9cSHong Zhang 
339805755b9cSHong Zhang /*@
3399d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
340005755b9cSHong Zhang 
340105755b9cSHong Zhang   Collective on TS
340205755b9cSHong Zhang 
340305755b9cSHong Zhang   Input Parameters:
340405755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
340505755b9cSHong Zhang 
340605755b9cSHong Zhang   Notes:
340705755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
340805755b9cSHong Zhang   so most users would not generally call this routine themselves.
340905755b9cSHong Zhang 
341005755b9cSHong Zhang   Level: developer
341105755b9cSHong Zhang 
341205755b9cSHong Zhang .keywords: TS, sensitivity
3413d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
341405755b9cSHong Zhang @*/
34150cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
341605755b9cSHong Zhang {
341705755b9cSHong Zhang   PetscErrorCode ierr;
341805755b9cSHong Zhang 
341905755b9cSHong Zhang   PetscFunctionBegin;
342005755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34210cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
342205755b9cSHong Zhang 
342305755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
34240cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
342505755b9cSHong Zhang   PetscStackPop;
342605755b9cSHong Zhang   PetscFunctionReturn(0);
342705755b9cSHong Zhang }
342805755b9cSHong Zhang 
3429ac226902SBarry Smith /*@C
3430000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
34313f2090d5SJed Brown   called once at the beginning of each time step.
3432000e7ae3SMatthew Knepley 
34333f9fe445SBarry Smith   Logically Collective on TS
3434000e7ae3SMatthew Knepley 
3435000e7ae3SMatthew Knepley   Input Parameters:
3436000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3437000e7ae3SMatthew Knepley - func - The function
3438000e7ae3SMatthew Knepley 
3439000e7ae3SMatthew Knepley   Calling sequence of func:
3440000e7ae3SMatthew Knepley . func (TS ts);
3441000e7ae3SMatthew Knepley 
3442000e7ae3SMatthew Knepley   Level: intermediate
3443000e7ae3SMatthew Knepley 
3444000e7ae3SMatthew Knepley .keywords: TS, timestep
34459be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
3446000e7ae3SMatthew Knepley @*/
34477087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3448000e7ae3SMatthew Knepley {
3449000e7ae3SMatthew Knepley   PetscFunctionBegin;
34500700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3451ae60f76fSBarry Smith   ts->prestep = func;
3452000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3453000e7ae3SMatthew Knepley }
3454000e7ae3SMatthew Knepley 
345509ee8438SJed Brown /*@
34563f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
34573f2090d5SJed Brown 
34583f2090d5SJed Brown   Collective on TS
34593f2090d5SJed Brown 
34603f2090d5SJed Brown   Input Parameters:
34613f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
34623f2090d5SJed Brown 
34633f2090d5SJed Brown   Notes:
34643f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
34653f2090d5SJed Brown   so most users would not generally call this routine themselves.
34663f2090d5SJed Brown 
34673f2090d5SJed Brown   Level: developer
34683f2090d5SJed Brown 
34693f2090d5SJed Brown .keywords: TS, timestep
34709be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
34713f2090d5SJed Brown @*/
34727087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
34733f2090d5SJed Brown {
34743f2090d5SJed Brown   PetscErrorCode ierr;
34753f2090d5SJed Brown 
34763f2090d5SJed Brown   PetscFunctionBegin;
34770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3478ae60f76fSBarry Smith   if (ts->prestep) {
3479ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
3480312ce896SJed Brown   }
34813f2090d5SJed Brown   PetscFunctionReturn(0);
34823f2090d5SJed Brown }
34833f2090d5SJed Brown 
3484b8123daeSJed Brown /*@C
3485b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3486b8123daeSJed Brown   called once at the beginning of each stage.
3487b8123daeSJed Brown 
3488b8123daeSJed Brown   Logically Collective on TS
3489b8123daeSJed Brown 
3490b8123daeSJed Brown   Input Parameters:
3491b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3492b8123daeSJed Brown - func - The function
3493b8123daeSJed Brown 
3494b8123daeSJed Brown   Calling sequence of func:
3495b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3496b8123daeSJed Brown 
3497b8123daeSJed Brown   Level: intermediate
3498b8123daeSJed Brown 
3499b8123daeSJed Brown   Note:
3500b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
350180275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3502b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3503b8123daeSJed Brown 
3504b8123daeSJed Brown .keywords: TS, timestep
35059be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3506b8123daeSJed Brown @*/
3507b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3508b8123daeSJed Brown {
3509b8123daeSJed Brown   PetscFunctionBegin;
3510b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3511ae60f76fSBarry Smith   ts->prestage = func;
3512b8123daeSJed Brown   PetscFunctionReturn(0);
3513b8123daeSJed Brown }
3514b8123daeSJed Brown 
35159be3e283SDebojyoti Ghosh /*@C
35169be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
35179be3e283SDebojyoti Ghosh   called once at the end of each stage.
35189be3e283SDebojyoti Ghosh 
35199be3e283SDebojyoti Ghosh   Logically Collective on TS
35209be3e283SDebojyoti Ghosh 
35219be3e283SDebojyoti Ghosh   Input Parameters:
35229be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
35239be3e283SDebojyoti Ghosh - func - The function
35249be3e283SDebojyoti Ghosh 
35259be3e283SDebojyoti Ghosh   Calling sequence of func:
35269be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
35279be3e283SDebojyoti Ghosh 
35289be3e283SDebojyoti Ghosh   Level: intermediate
35299be3e283SDebojyoti Ghosh 
35309be3e283SDebojyoti Ghosh   Note:
35319be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
353280275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
35339be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
35349be3e283SDebojyoti Ghosh 
35359be3e283SDebojyoti Ghosh .keywords: TS, timestep
35369be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
35379be3e283SDebojyoti Ghosh @*/
35389be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
35399be3e283SDebojyoti Ghosh {
35409be3e283SDebojyoti Ghosh   PetscFunctionBegin;
35419be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
35429be3e283SDebojyoti Ghosh   ts->poststage = func;
35439be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
35449be3e283SDebojyoti Ghosh }
35459be3e283SDebojyoti Ghosh 
3546c688d042SShri Abhyankar /*@C
3547c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3548c688d042SShri Abhyankar   called once at the end of each step evaluation.
3549c688d042SShri Abhyankar 
3550c688d042SShri Abhyankar   Logically Collective on TS
3551c688d042SShri Abhyankar 
3552c688d042SShri Abhyankar   Input Parameters:
3553c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3554c688d042SShri Abhyankar - func - The function
3555c688d042SShri Abhyankar 
3556c688d042SShri Abhyankar   Calling sequence of func:
3557c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3558c688d042SShri Abhyankar 
3559c688d042SShri Abhyankar   Level: intermediate
3560c688d042SShri Abhyankar 
3561c688d042SShri Abhyankar   Note:
35621785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
35631785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3564e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3565e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3566e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3567c688d042SShri Abhyankar 
3568c688d042SShri Abhyankar .keywords: TS, timestep
3569c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3570c688d042SShri Abhyankar @*/
3571c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3572c688d042SShri Abhyankar {
3573c688d042SShri Abhyankar   PetscFunctionBegin;
3574c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3575c688d042SShri Abhyankar   ts->postevaluate = func;
3576c688d042SShri Abhyankar   PetscFunctionReturn(0);
3577c688d042SShri Abhyankar }
3578c688d042SShri Abhyankar 
3579b8123daeSJed Brown /*@
3580b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3581b8123daeSJed Brown 
3582b8123daeSJed Brown   Collective on TS
3583b8123daeSJed Brown 
3584b8123daeSJed Brown   Input Parameters:
3585b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
35869be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3587b8123daeSJed Brown 
3588b8123daeSJed Brown   Notes:
3589b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3590b8123daeSJed Brown   most users would not generally call this routine themselves.
3591b8123daeSJed Brown 
3592b8123daeSJed Brown   Level: developer
3593b8123daeSJed Brown 
3594b8123daeSJed Brown .keywords: TS, timestep
35959be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3596b8123daeSJed Brown @*/
3597b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3598b8123daeSJed Brown {
3599b8123daeSJed Brown   PetscErrorCode ierr;
3600b8123daeSJed Brown 
3601b8123daeSJed Brown   PetscFunctionBegin;
3602b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3603ae60f76fSBarry Smith   if (ts->prestage) {
3604ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3605b8123daeSJed Brown   }
3606b8123daeSJed Brown   PetscFunctionReturn(0);
3607b8123daeSJed Brown }
3608b8123daeSJed Brown 
36099be3e283SDebojyoti Ghosh /*@
36109be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
36119be3e283SDebojyoti Ghosh 
36129be3e283SDebojyoti Ghosh   Collective on TS
36139be3e283SDebojyoti Ghosh 
36149be3e283SDebojyoti Ghosh   Input Parameters:
36159be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
36169be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
36179be3e283SDebojyoti Ghosh   stageindex  - Stage number
36189be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
36199be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
36209be3e283SDebojyoti Ghosh 
36219be3e283SDebojyoti Ghosh   Notes:
36229be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
36239be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
36249be3e283SDebojyoti Ghosh 
36259be3e283SDebojyoti Ghosh   Level: developer
36269be3e283SDebojyoti Ghosh 
36279be3e283SDebojyoti Ghosh .keywords: TS, timestep
36289be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
36299be3e283SDebojyoti Ghosh @*/
36309be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
36319be3e283SDebojyoti Ghosh {
36329be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
36339be3e283SDebojyoti Ghosh 
36349be3e283SDebojyoti Ghosh   PetscFunctionBegin;
36359be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36364beae5d8SLisandro Dalcin   if (ts->poststage) {
36379be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
36389be3e283SDebojyoti Ghosh   }
36399be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
36409be3e283SDebojyoti Ghosh }
36419be3e283SDebojyoti Ghosh 
3642c688d042SShri Abhyankar /*@
3643c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3644c688d042SShri Abhyankar 
3645c688d042SShri Abhyankar   Collective on TS
3646c688d042SShri Abhyankar 
3647c688d042SShri Abhyankar   Input Parameters:
3648c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3649c688d042SShri Abhyankar 
3650c688d042SShri Abhyankar   Notes:
3651c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3652c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3653c688d042SShri Abhyankar 
3654c688d042SShri Abhyankar   Level: developer
3655c688d042SShri Abhyankar 
3656c688d042SShri Abhyankar .keywords: TS, timestep
3657c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3658c688d042SShri Abhyankar @*/
3659c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3660c688d042SShri Abhyankar {
3661c688d042SShri Abhyankar   PetscErrorCode ierr;
3662c688d042SShri Abhyankar 
3663c688d042SShri Abhyankar   PetscFunctionBegin;
3664c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3665c688d042SShri Abhyankar   if (ts->postevaluate) {
3666c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3667c688d042SShri Abhyankar   }
3668c688d042SShri Abhyankar   PetscFunctionReturn(0);
3669c688d042SShri Abhyankar }
3670c688d042SShri Abhyankar 
3671ac226902SBarry Smith /*@C
3672000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
36733f2090d5SJed Brown   called once at the end of each time step.
3674000e7ae3SMatthew Knepley 
36753f9fe445SBarry Smith   Logically Collective on TS
3676000e7ae3SMatthew Knepley 
3677000e7ae3SMatthew Knepley   Input Parameters:
3678000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3679000e7ae3SMatthew Knepley - func - The function
3680000e7ae3SMatthew Knepley 
3681000e7ae3SMatthew Knepley   Calling sequence of func:
3682b8123daeSJed Brown $ func (TS ts);
3683000e7ae3SMatthew Knepley 
36841785ff2aSShri Abhyankar   Notes:
36851785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
36861785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
36871785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
36881785ff2aSShri Abhyankar 
3689000e7ae3SMatthew Knepley   Level: intermediate
3690000e7ae3SMatthew Knepley 
3691000e7ae3SMatthew Knepley .keywords: TS, timestep
369280275a0aSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime()
3693000e7ae3SMatthew Knepley @*/
36947087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3695000e7ae3SMatthew Knepley {
3696000e7ae3SMatthew Knepley   PetscFunctionBegin;
36970700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3698ae60f76fSBarry Smith   ts->poststep = func;
3699000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3700000e7ae3SMatthew Knepley }
3701000e7ae3SMatthew Knepley 
370209ee8438SJed Brown /*@
37033f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
37043f2090d5SJed Brown 
37053f2090d5SJed Brown   Collective on TS
37063f2090d5SJed Brown 
37073f2090d5SJed Brown   Input Parameters:
37083f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
37093f2090d5SJed Brown 
37103f2090d5SJed Brown   Notes:
37113f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
37123f2090d5SJed Brown   so most users would not generally call this routine themselves.
37133f2090d5SJed Brown 
37143f2090d5SJed Brown   Level: developer
37153f2090d5SJed Brown 
37163f2090d5SJed Brown .keywords: TS, timestep
37173f2090d5SJed Brown @*/
37187087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
37193f2090d5SJed Brown {
37203f2090d5SJed Brown   PetscErrorCode ierr;
37213f2090d5SJed Brown 
37223f2090d5SJed Brown   PetscFunctionBegin;
37230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3724ae60f76fSBarry Smith   if (ts->poststep) {
3725ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
372672ac3e02SJed Brown   }
37273f2090d5SJed Brown   PetscFunctionReturn(0);
37283f2090d5SJed Brown }
37293f2090d5SJed Brown 
3730d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3731d763cef2SBarry Smith 
3732d763cef2SBarry Smith /*@C
3733a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3734d763cef2SBarry Smith    timestep to display the iteration's  progress.
3735d763cef2SBarry Smith 
37363f9fe445SBarry Smith    Logically Collective on TS
3737d763cef2SBarry Smith 
3738d763cef2SBarry Smith    Input Parameters:
3739d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3740e213d8f1SJed Brown .  monitor - monitoring routine
3741329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
37420298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3743b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
37440298fd71SBarry Smith           (may be NULL)
3745d763cef2SBarry Smith 
3746e213d8f1SJed Brown    Calling sequence of monitor:
37470910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3748d763cef2SBarry Smith 
3749d763cef2SBarry Smith +    ts - the TS context
375063e21af5SBarry 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)
37511f06c33eSBarry Smith .    time - current time
37520910c330SBarry Smith .    u - current iterate
3753d763cef2SBarry Smith -    mctx - [optional] monitoring context
3754d763cef2SBarry Smith 
3755d763cef2SBarry Smith    Notes:
3756d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3757d763cef2SBarry Smith    already has been loaded.
3758d763cef2SBarry Smith 
3759025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
3760025f1a04SBarry Smith 
3761d763cef2SBarry Smith    Level: intermediate
3762d763cef2SBarry Smith 
3763d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3764d763cef2SBarry Smith 
3765a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3766d763cef2SBarry Smith @*/
3767c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3768d763cef2SBarry Smith {
376978064530SBarry Smith   PetscErrorCode ierr;
377078064530SBarry Smith   PetscInt       i;
377178064530SBarry Smith   PetscBool      identical;
377278064530SBarry Smith 
3773d763cef2SBarry Smith   PetscFunctionBegin;
37740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
377578064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
377678064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
377778064530SBarry Smith     if (identical) PetscFunctionReturn(0);
377878064530SBarry Smith   }
377917186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3780d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
37818704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3782d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3783d763cef2SBarry Smith   PetscFunctionReturn(0);
3784d763cef2SBarry Smith }
3785d763cef2SBarry Smith 
3786d763cef2SBarry Smith /*@C
3787a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3788d763cef2SBarry Smith 
37893f9fe445SBarry Smith    Logically Collective on TS
3790d763cef2SBarry Smith 
3791d763cef2SBarry Smith    Input Parameters:
3792d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3793d763cef2SBarry Smith 
3794d763cef2SBarry Smith    Notes:
3795d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3796d763cef2SBarry Smith 
3797d763cef2SBarry Smith    Level: intermediate
3798d763cef2SBarry Smith 
3799d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3800d763cef2SBarry Smith 
3801a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3802d763cef2SBarry Smith @*/
38037087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3804d763cef2SBarry Smith {
3805d952e501SBarry Smith   PetscErrorCode ierr;
3806d952e501SBarry Smith   PetscInt       i;
3807d952e501SBarry Smith 
3808d763cef2SBarry Smith   PetscFunctionBegin;
38090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3810d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
38118704b422SBarry Smith     if (ts->monitordestroy[i]) {
38128704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3813d952e501SBarry Smith     }
3814d952e501SBarry Smith   }
3815d763cef2SBarry Smith   ts->numbermonitors = 0;
3816d763cef2SBarry Smith   PetscFunctionReturn(0);
3817d763cef2SBarry Smith }
3818d763cef2SBarry Smith 
3819721cd6eeSBarry Smith /*@C
3820721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
38215516499fSSatish Balay 
38225516499fSSatish Balay    Level: intermediate
382341251cbbSSatish Balay 
38245516499fSSatish Balay .keywords: TS, set, monitor
38255516499fSSatish Balay 
382663e21af5SBarry Smith .seealso:  TSMonitorSet()
382741251cbbSSatish Balay @*/
3828721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3829d763cef2SBarry Smith {
3830dfbe8321SBarry Smith   PetscErrorCode ierr;
3831721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
383241aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3833d132466eSBarry Smith 
3834d763cef2SBarry Smith   PetscFunctionBegin;
38354d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
383641aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
383741aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3838721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
383941aca3d6SBarry Smith   if (iascii) {
3840649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
384163e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
384263e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
384363e21af5SBarry Smith     } else {
38448392e04aSShri 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);
384563e21af5SBarry Smith     }
3846649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
384741aca3d6SBarry Smith   } else if (ibinary) {
384841aca3d6SBarry Smith     PetscMPIInt rank;
384941aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
385041aca3d6SBarry Smith     if (!rank) {
3851450a797fSBarry Smith       PetscBool skipHeader;
3852450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3853450a797fSBarry Smith 
3854450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3855450a797fSBarry Smith       if (!skipHeader) {
3856450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3857450a797fSBarry Smith        }
385841aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
385941aca3d6SBarry Smith     } else {
386041aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
386141aca3d6SBarry Smith     }
386241aca3d6SBarry Smith   }
3863721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3864d763cef2SBarry Smith   PetscFunctionReturn(0);
3865d763cef2SBarry Smith }
3866d763cef2SBarry Smith 
38679110b2e7SHong Zhang /*@C
38689110b2e7SHong Zhang    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
38699110b2e7SHong Zhang    timestep to display the iteration's  progress.
38709110b2e7SHong Zhang 
38719110b2e7SHong Zhang    Logically Collective on TS
38729110b2e7SHong Zhang 
38739110b2e7SHong Zhang    Input Parameters:
38749110b2e7SHong Zhang +  ts - the TS context obtained from TSCreate()
38759110b2e7SHong Zhang .  adjointmonitor - monitoring routine
38769110b2e7SHong Zhang .  adjointmctx - [optional] user-defined context for private data for the
38779110b2e7SHong Zhang              monitor routine (use NULL if no context is desired)
38789110b2e7SHong Zhang -  adjointmonitordestroy - [optional] routine that frees monitor context
38799110b2e7SHong Zhang           (may be NULL)
38809110b2e7SHong Zhang 
38819110b2e7SHong Zhang    Calling sequence of monitor:
38829110b2e7SHong Zhang $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)
38839110b2e7SHong Zhang 
38849110b2e7SHong Zhang +    ts - the TS context
38859110b2e7SHong 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
38869110b2e7SHong Zhang                                been interpolated to)
38879110b2e7SHong Zhang .    time - current time
38889110b2e7SHong Zhang .    u - current iterate
38899110b2e7SHong Zhang .    numcost - number of cost functionos
38909110b2e7SHong Zhang .    lambda - sensitivities to initial conditions
38919110b2e7SHong Zhang .    mu - sensitivities to parameters
38929110b2e7SHong Zhang -    adjointmctx - [optional] adjoint monitoring context
38939110b2e7SHong Zhang 
38949110b2e7SHong Zhang    Notes:
38959110b2e7SHong Zhang    This routine adds an additional monitor to the list of monitors that
38969110b2e7SHong Zhang    already has been loaded.
38979110b2e7SHong Zhang 
38989110b2e7SHong Zhang    Fortran notes: Only a single monitor function can be set for each TS object
38999110b2e7SHong Zhang 
39009110b2e7SHong Zhang    Level: intermediate
39019110b2e7SHong Zhang 
39029110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
39039110b2e7SHong Zhang 
39049110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel()
39059110b2e7SHong Zhang @*/
39069110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
39079110b2e7SHong Zhang {
390878064530SBarry Smith   PetscErrorCode ierr;
390978064530SBarry Smith   PetscInt       i;
391078064530SBarry Smith   PetscBool      identical;
391178064530SBarry Smith 
39129110b2e7SHong Zhang   PetscFunctionBegin;
39139110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
391478064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
391578064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr);
391678064530SBarry Smith     if (identical) PetscFunctionReturn(0);
391778064530SBarry Smith   }
39189110b2e7SHong Zhang   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
39199110b2e7SHong Zhang   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
39209110b2e7SHong Zhang   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
39219110b2e7SHong Zhang   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
39229110b2e7SHong Zhang   PetscFunctionReturn(0);
39239110b2e7SHong Zhang }
39249110b2e7SHong Zhang 
39259110b2e7SHong Zhang /*@C
39269110b2e7SHong Zhang    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.
39279110b2e7SHong Zhang 
39289110b2e7SHong Zhang    Logically Collective on TS
39299110b2e7SHong Zhang 
39309110b2e7SHong Zhang    Input Parameters:
39319110b2e7SHong Zhang .  ts - the TS context obtained from TSCreate()
39329110b2e7SHong Zhang 
39339110b2e7SHong Zhang    Notes:
39349110b2e7SHong Zhang    There is no way to remove a single, specific monitor.
39359110b2e7SHong Zhang 
39369110b2e7SHong Zhang    Level: intermediate
39379110b2e7SHong Zhang 
39389110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
39399110b2e7SHong Zhang 
39409110b2e7SHong Zhang .seealso: TSAdjointMonitorSet()
39419110b2e7SHong Zhang @*/
39429110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorCancel(TS ts)
39439110b2e7SHong Zhang {
39449110b2e7SHong Zhang   PetscErrorCode ierr;
39459110b2e7SHong Zhang   PetscInt       i;
39469110b2e7SHong Zhang 
39479110b2e7SHong Zhang   PetscFunctionBegin;
39489110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39499110b2e7SHong Zhang   for (i=0; i<ts->numberadjointmonitors; i++) {
39509110b2e7SHong Zhang     if (ts->adjointmonitordestroy[i]) {
39519110b2e7SHong Zhang       ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
39529110b2e7SHong Zhang     }
39539110b2e7SHong Zhang   }
39549110b2e7SHong Zhang   ts->numberadjointmonitors = 0;
39559110b2e7SHong Zhang   PetscFunctionReturn(0);
39569110b2e7SHong Zhang }
39579110b2e7SHong Zhang 
3958721cd6eeSBarry Smith /*@C
3959721cd6eeSBarry Smith    TSAdjointMonitorDefault - the default monitor of adjoint computations
3960110eb670SHong Zhang 
3961110eb670SHong Zhang    Level: intermediate
3962110eb670SHong Zhang 
3963110eb670SHong Zhang .keywords: TS, set, monitor
3964110eb670SHong Zhang 
3965110eb670SHong Zhang .seealso: TSAdjointMonitorSet()
3966110eb670SHong Zhang @*/
3967721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
3968110eb670SHong Zhang {
3969110eb670SHong Zhang   PetscErrorCode ierr;
3970721cd6eeSBarry Smith   PetscViewer    viewer = vf->viewer;
3971110eb670SHong Zhang 
3972110eb670SHong Zhang   PetscFunctionBegin;
3973110eb670SHong Zhang   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3974721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3975110eb670SHong Zhang   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3976110eb670SHong 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);
3977110eb670SHong Zhang   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3978721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3979110eb670SHong Zhang   PetscFunctionReturn(0);
3980110eb670SHong Zhang }
3981110eb670SHong Zhang 
3982cd652676SJed Brown /*@
3983cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3984cd652676SJed Brown 
3985cd652676SJed Brown    Collective on TS
3986cd652676SJed Brown 
3987cd652676SJed Brown    Input Argument:
3988cd652676SJed Brown +  ts - time stepping context
3989cd652676SJed Brown -  t - time to interpolate to
3990cd652676SJed Brown 
3991cd652676SJed Brown    Output Argument:
39920910c330SBarry Smith .  U - state at given time
3993cd652676SJed Brown 
3994cd652676SJed Brown    Level: intermediate
3995cd652676SJed Brown 
3996cd652676SJed Brown    Developer Notes:
3997cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3998cd652676SJed Brown 
3999cd652676SJed Brown .keywords: TS, set
4000cd652676SJed Brown 
4001874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
4002cd652676SJed Brown @*/
40030910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
4004cd652676SJed Brown {
4005cd652676SJed Brown   PetscErrorCode ierr;
4006cd652676SJed Brown 
4007cd652676SJed Brown   PetscFunctionBegin;
4008cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4009b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4010be5899b3SLisandro Dalcin   if (t < ts->ptime_prev || t > ts->ptime) SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Requested time %g not in last time steps [%g,%g]",t,(double)ts->ptime_prev,(double)ts->ptime);
4011ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
40120910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
4013cd652676SJed Brown   PetscFunctionReturn(0);
4014cd652676SJed Brown }
4015cd652676SJed Brown 
4016d763cef2SBarry Smith /*@
40176d9e5789SSean Farley    TSStep - Steps one time step
4018d763cef2SBarry Smith 
4019d763cef2SBarry Smith    Collective on TS
4020d763cef2SBarry Smith 
4021d763cef2SBarry Smith    Input Parameter:
4022d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4023d763cef2SBarry Smith 
402427829d71SBarry Smith    Level: developer
4025d763cef2SBarry Smith 
4026b8123daeSJed Brown    Notes:
402727829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
402827829d71SBarry Smith 
4029b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
4030b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
4031b8123daeSJed Brown 
403219eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
403319eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
403425cb2221SBarry Smith 
4035d763cef2SBarry Smith .keywords: TS, timestep, solve
4036d763cef2SBarry Smith 
40379be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
4038d763cef2SBarry Smith @*/
4039193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
4040d763cef2SBarry Smith {
4041dfbe8321SBarry Smith   PetscErrorCode   ierr;
4042fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
4043be5899b3SLisandro Dalcin   PetscReal        ptime;
4044d763cef2SBarry Smith 
4045d763cef2SBarry Smith   PetscFunctionBegin;
40460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4047fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
4048fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
4049fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
4050fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
4051fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
4052fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
4053302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
4054fffbeea8SBarry Smith 
4055d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
405668bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4057d405a339SMatthew Knepley 
4058ef85077eSLisandro Dalcin   if (ts->max_time >= PETSC_MAX_REAL && ts->max_steps == PETSC_MAX_INT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>");
4059a6772fa2SLisandro 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()");
4060be5899b3SLisandro 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");
4061a6772fa2SLisandro Dalcin 
4062be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
4063be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
40642808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
4065e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
4066d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
4067193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
4068d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
4069be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
40702808aa04SLisandro Dalcin   ts->steps++;
4071be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
407228d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
4073362cd11cSLisandro Dalcin 
4074362cd11cSLisandro Dalcin   if (ts->reason < 0) {
4075cef5090cSJed Brown     if (ts->errorifstepfailed) {
407608c7845fSBarry 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]);
407708c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
4078d2daff3dSHong Zhang     }
407908c7845fSBarry Smith   } else if (!ts->reason) {
408008c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
408108c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
408208c7845fSBarry Smith   }
408308c7845fSBarry Smith   PetscFunctionReturn(0);
408408c7845fSBarry Smith }
408508c7845fSBarry Smith 
408608c7845fSBarry Smith /*@
4087b957a604SHong Zhang    TSAdjointStep - Steps one time step backward in the adjoint run
408808c7845fSBarry Smith 
408908c7845fSBarry Smith    Collective on TS
409008c7845fSBarry Smith 
409108c7845fSBarry Smith    Input Parameter:
409208c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
409308c7845fSBarry Smith 
40946a4d4014SLisandro Dalcin    Level: intermediate
40956a4d4014SLisandro Dalcin 
4096b957a604SHong Zhang .keywords: TS, adjoint, step
40976a4d4014SLisandro Dalcin 
4098b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve()
40996a4d4014SLisandro Dalcin @*/
410008c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
41016a4d4014SLisandro Dalcin {
410208c7845fSBarry Smith   DM               dm;
41036a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
41046a4d4014SLisandro Dalcin 
41056a4d4014SLisandro Dalcin   PetscFunctionBegin;
41064a2ae208SSatish Balay   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
410708c7845fSBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
410808c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
4109d763cef2SBarry Smith 
4110685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr);
4111d763cef2SBarry Smith 
4112be5899b3SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
4113be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime;
411442f2b339SBarry 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);
4115be5899b3SLisandro Dalcin   ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
411642f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
41178d0ad7a8SHong Zhang   ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
41182808aa04SLisandro Dalcin   ts->adjoint_steps++; ts->steps--;
4119362cd11cSLisandro Dalcin 
4120362cd11cSLisandro Dalcin   if (ts->reason < 0) {
4121cef5090cSJed Brown     if (ts->errorifstepfailed) {
41226c4ed002SBarry 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]);
41236c4ed002SBarry 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]);
41246c4ed002SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
4125cef5090cSJed Brown     }
4126362cd11cSLisandro Dalcin   } else if (!ts->reason) {
41272808aa04SLisandro Dalcin     if (ts->adjoint_steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
4128362cd11cSLisandro Dalcin   }
4129d763cef2SBarry Smith   PetscFunctionReturn(0);
4130d763cef2SBarry Smith }
4131d763cef2SBarry Smith 
41327cbde773SLisandro Dalcin /*@
41337cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
41347cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
41357cbde773SLisandro Dalcin 
41367cbde773SLisandro Dalcin    Collective on TS
41377cbde773SLisandro Dalcin 
41387cbde773SLisandro Dalcin    Input Arguments:
41397cbde773SLisandro Dalcin +  ts - time stepping context
41407cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
41417cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
41427cbde773SLisandro Dalcin 
41437cbde773SLisandro Dalcin    Output Arguments:
41447cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
41457cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
41467cbde773SLisandro Dalcin 
41477cbde773SLisandro Dalcin    Level: advanced
41487cbde773SLisandro Dalcin 
41497cbde773SLisandro Dalcin    Notes:
41507cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
41517cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
41527cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
41537cbde773SLisandro Dalcin 
41547cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
41557cbde773SLisandro Dalcin @*/
41567cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
41577cbde773SLisandro Dalcin {
41587cbde773SLisandro Dalcin   PetscErrorCode ierr;
41597cbde773SLisandro Dalcin 
41607cbde773SLisandro Dalcin   PetscFunctionBegin;
41617cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41627cbde773SLisandro Dalcin   PetscValidType(ts,1);
41637cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
41647cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
41657cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
41667cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
41677cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
41687cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
41697cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
41707cbde773SLisandro Dalcin   PetscFunctionReturn(0);
41717cbde773SLisandro Dalcin }
41727cbde773SLisandro Dalcin 
417305175c85SJed Brown /*@
417405175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
417505175c85SJed Brown 
41761c3436cfSJed Brown    Collective on TS
417705175c85SJed Brown 
417805175c85SJed Brown    Input Arguments:
41791c3436cfSJed Brown +  ts - time stepping context
41801c3436cfSJed Brown .  order - desired order of accuracy
41810298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
418205175c85SJed Brown 
418305175c85SJed Brown    Output Arguments:
41840910c330SBarry Smith .  U - state at the end of the current step
418505175c85SJed Brown 
418605175c85SJed Brown    Level: advanced
418705175c85SJed Brown 
4188108c343cSJed Brown    Notes:
4189108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
4190108c343cSJed 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.
4191108c343cSJed Brown 
41921c3436cfSJed Brown .seealso: TSStep(), TSAdapt
419305175c85SJed Brown @*/
41940910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
419505175c85SJed Brown {
419605175c85SJed Brown   PetscErrorCode ierr;
419705175c85SJed Brown 
419805175c85SJed Brown   PetscFunctionBegin;
419905175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
420005175c85SJed Brown   PetscValidType(ts,1);
42010910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4202ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
42030910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
420405175c85SJed Brown   PetscFunctionReturn(0);
420505175c85SJed Brown }
420605175c85SJed Brown 
4207b1cb36f3SHong Zhang /*@
4208b1cb36f3SHong Zhang    TSForwardCostIntegral - Evaluate the cost integral in the forward run.
4209b1cb36f3SHong Zhang 
4210b1cb36f3SHong Zhang    Collective on TS
4211b1cb36f3SHong Zhang 
4212b1cb36f3SHong Zhang    Input Arguments:
4213b1cb36f3SHong Zhang .  ts - time stepping context
4214b1cb36f3SHong Zhang 
4215b1cb36f3SHong Zhang    Level: advanced
4216b1cb36f3SHong Zhang 
4217b1cb36f3SHong Zhang    Notes:
4218b1cb36f3SHong Zhang    This function cannot be called until TSStep() has been completed.
4219b1cb36f3SHong Zhang 
4220b1cb36f3SHong Zhang .seealso: TSSolve(), TSAdjointCostIntegral()
4221b1cb36f3SHong Zhang @*/
4222b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts)
4223b1cb36f3SHong Zhang {
4224b1cb36f3SHong Zhang   PetscErrorCode ierr;
4225b1cb36f3SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4226b1cb36f3SHong 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);
4227b1cb36f3SHong Zhang   ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr);
4228b1cb36f3SHong Zhang   PetscFunctionReturn(0);
4229b1cb36f3SHong Zhang }
4230d67e68b7SBarry Smith 
4231d763cef2SBarry Smith /*@
4232d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
4233d763cef2SBarry Smith 
4234d763cef2SBarry Smith    Collective on TS
4235d763cef2SBarry Smith 
4236d763cef2SBarry Smith    Input Parameter:
4237d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
423863e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
423963e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
42405a3a76d0SJed Brown 
4241d763cef2SBarry Smith    Level: beginner
4242d763cef2SBarry Smith 
42435a3a76d0SJed Brown    Notes:
42445a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
42455a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
42465a3a76d0SJed Brown    stepped over the final time.
42475a3a76d0SJed Brown 
4248d763cef2SBarry Smith .keywords: TS, timestep, solve
4249d763cef2SBarry Smith 
425063e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
4251d763cef2SBarry Smith @*/
4252cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
4253d763cef2SBarry Smith {
4254b06615a5SLisandro Dalcin   Vec               solution;
4255d763cef2SBarry Smith   PetscErrorCode    ierr;
4256d763cef2SBarry Smith 
4257d763cef2SBarry Smith   PetscFunctionBegin;
4258d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4259f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4260feed9e9dSBarry Smith 
426149354f04SShri 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 */
4262b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
42630910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4264b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4265b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4266b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
42675a3a76d0SJed Brown     }
42680910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4269715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
4270bbd56ea5SKarl Rupp   } else if (u) {
42710910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
42725a3a76d0SJed Brown   }
4273b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
427468bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4275a6772fa2SLisandro Dalcin 
4276ef85077eSLisandro Dalcin   if (ts->max_time >= PETSC_MAX_REAL && ts->max_steps == PETSC_MAX_INT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>");
4277a6772fa2SLisandro 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()");
4278a6772fa2SLisandro 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");
4279a6772fa2SLisandro Dalcin 
4280715f1b00SHong Zhang   if (ts->forward_solve) {
4281715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
4282715f1b00SHong Zhang   }
4283715f1b00SHong Zhang 
4284e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4285715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
4286e7069c78SShri      TSTrajectory to incorrectly save the output files
4287e7069c78SShri   */
4288715f1b00SHong Zhang   /* reset time step and iteration counters */
42892808aa04SLisandro Dalcin 
42902808aa04SLisandro Dalcin   if (!ts->steps) {
42915ef26d82SJed Brown     ts->ksp_its           = 0;
42925ef26d82SJed Brown     ts->snes_its          = 0;
4293c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
4294c610991cSLisandro Dalcin     ts->reject            = 0;
42952808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
42962808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
42972808aa04SLisandro Dalcin   }
4298193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
4299193ac0bcSJed Brown 
4300ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4301f05ece33SBarry Smith 
4302193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4303193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4304a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4305cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4306a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4307be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4308db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4309db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4310e7069c78SShri 
43112808aa04SLisandro Dalcin     if (!ts->steps) {
43122808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
43136427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
43142808aa04SLisandro Dalcin     }
43156427ac75SLisandro Dalcin 
4316e1a7a14fSJed Brown     while (!ts->reason) {
43172808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
43189687d888SLisandro Dalcin       if (!ts->steprollback) {
43199687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
43209687d888SLisandro Dalcin       }
4321193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4322e783b05fSHong Zhang       if (ts->vec_costintegral && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
4323b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
4324b1cb36f3SHong Zhang       }
4325715f1b00SHong Zhang       if (!ts->steprollback && ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
4326715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
4327715f1b00SHong Zhang       }
432810b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4329e783b05fSHong Zhang       ierr = TSEventHandler(ts);CHKERRQ(ierr); /* The right-hand side may be changed due to event. Be careful with Any computation using the RHS information after this point. */
4330e783b05fSHong Zhang       if (!ts->steprollback) {
43312808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
43321eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4333aeb4809dSShri Abhyankar       }
4334193ac0bcSJed Brown     }
43352808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
43366427ac75SLisandro Dalcin 
433749354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
43380910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4339cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4340b06615a5SLisandro Dalcin       solution = u;
434163e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
43420574a7fbSJed Brown     } else {
4343ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4344cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4345b06615a5SLisandro Dalcin       solution = ts->vec_sol;
43460574a7fbSJed Brown     }
4347193ac0bcSJed Brown   }
4348d2daff3dSHong Zhang 
4349ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
435063e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
435156f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4352ef222394SBarry Smith   if (ts->adjoint_solve) {
4353ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
43542b0a91c0SBarry Smith   }
4355d763cef2SBarry Smith   PetscFunctionReturn(0);
4356d763cef2SBarry Smith }
4357d763cef2SBarry Smith 
4358b1cb36f3SHong Zhang /*@
4359b1cb36f3SHong Zhang  TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run.
4360b1cb36f3SHong Zhang 
4361b1cb36f3SHong Zhang  Collective on TS
4362b1cb36f3SHong Zhang 
4363b1cb36f3SHong Zhang  Input Arguments:
4364b1cb36f3SHong Zhang  .  ts - time stepping context
4365b1cb36f3SHong Zhang 
4366b1cb36f3SHong Zhang  Level: advanced
4367b1cb36f3SHong Zhang 
4368b1cb36f3SHong Zhang  Notes:
4369b1cb36f3SHong Zhang  This function cannot be called until TSAdjointStep() has been completed.
4370b1cb36f3SHong Zhang 
4371b1cb36f3SHong Zhang  .seealso: TSAdjointSolve(), TSAdjointStep
4372b1cb36f3SHong Zhang  @*/
4373b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts)
4374b1cb36f3SHong Zhang {
4375b1cb36f3SHong Zhang     PetscErrorCode ierr;
4376b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4377b1cb36f3SHong 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);
4378b1cb36f3SHong Zhang     ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr);
4379b1cb36f3SHong Zhang     PetscFunctionReturn(0);
4380b1cb36f3SHong Zhang }
4381b1cb36f3SHong Zhang 
4382c88f2d44SBarry Smith /*@
4383c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
4384c88f2d44SBarry Smith 
4385c88f2d44SBarry Smith    Collective on TS
4386c88f2d44SBarry Smith 
4387c88f2d44SBarry Smith    Input Parameter:
43882c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
4389c88f2d44SBarry Smith 
4390e87fd094SBarry Smith    Options Database:
4391715f1b00SHong Zhang . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial values
4392e87fd094SBarry Smith 
4393c88f2d44SBarry Smith    Level: intermediate
4394c88f2d44SBarry Smith 
4395c88f2d44SBarry Smith    Notes:
4396c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
4397c88f2d44SBarry Smith 
439873b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
439973b18844SBarry Smith 
4400c88f2d44SBarry Smith .keywords: TS, timestep, solve
4401c88f2d44SBarry Smith 
4402b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
4403c88f2d44SBarry Smith @*/
44042c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
4405c88f2d44SBarry Smith {
4406c88f2d44SBarry Smith   PetscErrorCode    ierr;
4407c88f2d44SBarry Smith 
4408c88f2d44SBarry Smith   PetscFunctionBegin;
4409c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4410c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
441168bece0bSHong Zhang 
4412c88f2d44SBarry Smith   /* reset time step and iteration counters */
44132808aa04SLisandro Dalcin   ts->adjoint_steps     = 0;
4414c88f2d44SBarry Smith   ts->ksp_its           = 0;
4415c88f2d44SBarry Smith   ts->snes_its          = 0;
4416c88f2d44SBarry Smith   ts->num_snes_failures = 0;
4417c88f2d44SBarry Smith   ts->reject            = 0;
4418c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
4419c88f2d44SBarry Smith 
44202808aa04SLisandro Dalcin   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->steps;
44212808aa04SLisandro Dalcin   if (ts->adjoint_steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
4422c88f2d44SBarry Smith 
4423c88f2d44SBarry Smith   while (!ts->reason) {
44242808aa04SLisandro Dalcin     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->steps,&ts->ptime);CHKERRQ(ierr);
44252808aa04SLisandro Dalcin     ierr = TSAdjointMonitor(ts,ts->steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
44266427ac75SLisandro Dalcin     ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr);
4427616946c1SHong Zhang     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
4428b1cb36f3SHong Zhang     if (ts->vec_costintegral && !ts->costintegralfwd) {
4429b1cb36f3SHong Zhang       ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr);
4430b1cb36f3SHong Zhang     }
4431c88f2d44SBarry Smith   }
44322808aa04SLisandro Dalcin   ierr = TSTrajectoryGet(ts->trajectory,ts,ts->steps,&ts->ptime);CHKERRQ(ierr);
44332808aa04SLisandro Dalcin   ierr = TSAdjointMonitor(ts,ts->steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
4434c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
4435e210cd0eSHong Zhang   ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr);
4436685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr);
4437c88f2d44SBarry Smith   PetscFunctionReturn(0);
4438c88f2d44SBarry Smith }
4439c88f2d44SBarry Smith 
44407db568b7SBarry Smith /*@C
4441228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4442228d79bcSJed Brown 
4443228d79bcSJed Brown    Collective on TS
4444228d79bcSJed Brown 
4445228d79bcSJed Brown    Input Parameters:
4446228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4447228d79bcSJed Brown .  step - step number that has just completed
4448228d79bcSJed Brown .  ptime - model time of the state
44490910c330SBarry Smith -  u - state at the current model time
4450228d79bcSJed Brown 
4451228d79bcSJed Brown    Notes:
44527db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
44537db568b7SBarry Smith    Users would almost never call this routine directly.
4454228d79bcSJed Brown 
445563e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
445663e21af5SBarry Smith 
44577db568b7SBarry Smith    Level: developer
4458228d79bcSJed Brown 
4459228d79bcSJed Brown .keywords: TS, timestep
4460228d79bcSJed Brown @*/
44610910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4462d763cef2SBarry Smith {
4463d6152f81SLisandro Dalcin   DM             dm;
4464a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4465d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4466d763cef2SBarry Smith 
4467d763cef2SBarry Smith   PetscFunctionBegin;
4468b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4469b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4470d6152f81SLisandro Dalcin 
4471d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4472d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4473d6152f81SLisandro Dalcin 
44745edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
4475d763cef2SBarry Smith   for (i=0; i<n; i++) {
44760910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4477d763cef2SBarry Smith   }
44785edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
4479d763cef2SBarry Smith   PetscFunctionReturn(0);
4480d763cef2SBarry Smith }
4481d763cef2SBarry Smith 
44827db568b7SBarry Smith /*@C
44839110b2e7SHong Zhang    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()
44849110b2e7SHong Zhang 
44859110b2e7SHong Zhang    Collective on TS
44869110b2e7SHong Zhang 
44879110b2e7SHong Zhang    Input Parameters:
44889110b2e7SHong Zhang +  ts - time stepping context obtained from TSCreate()
44899110b2e7SHong Zhang .  step - step number that has just completed
44909110b2e7SHong Zhang .  ptime - model time of the state
44919110b2e7SHong Zhang .  u - state at the current model time
44929110b2e7SHong Zhang .  numcost - number of cost functions (dimension of lambda  or mu)
44939110b2e7SHong Zhang .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
44949110b2e7SHong Zhang -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
44959110b2e7SHong Zhang 
44969110b2e7SHong Zhang    Notes:
44977db568b7SBarry Smith    TSAdjointMonitor() is typically used automatically within the time stepping implementations.
44987db568b7SBarry Smith    Users would almost never call this routine directly.
44999110b2e7SHong Zhang 
45007db568b7SBarry Smith    Level: developer
45019110b2e7SHong Zhang 
45029110b2e7SHong Zhang .keywords: TS, timestep
45039110b2e7SHong Zhang @*/
45049110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
45059110b2e7SHong Zhang {
45069110b2e7SHong Zhang   PetscErrorCode ierr;
45079110b2e7SHong Zhang   PetscInt       i,n = ts->numberadjointmonitors;
45089110b2e7SHong Zhang 
45099110b2e7SHong Zhang   PetscFunctionBegin;
45109110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45119110b2e7SHong Zhang   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
45129110b2e7SHong Zhang   ierr = VecLockPush(u);CHKERRQ(ierr);
45139110b2e7SHong Zhang   for (i=0; i<n; i++) {
45149110b2e7SHong Zhang     ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
45159110b2e7SHong Zhang   }
45169110b2e7SHong Zhang   ierr = VecLockPop(u);CHKERRQ(ierr);
45179110b2e7SHong Zhang   PetscFunctionReturn(0);
45189110b2e7SHong Zhang }
45199110b2e7SHong Zhang 
4520d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4521d763cef2SBarry Smith /*@C
45227db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4523a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4524d763cef2SBarry Smith 
4525d763cef2SBarry Smith    Collective on TS
4526d763cef2SBarry Smith 
4527d763cef2SBarry Smith    Input Parameters:
4528d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4529d763cef2SBarry Smith .  label - the title to put in the title bar
45307c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4531a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4532a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4533d763cef2SBarry Smith 
4534d763cef2SBarry Smith    Output Parameter:
45350b039ecaSBarry Smith .  ctx - the context
4536d763cef2SBarry Smith 
4537d763cef2SBarry Smith    Options Database Key:
45384f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
45397db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
45407db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
45417db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
45427db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4543b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4544d763cef2SBarry Smith 
4545d763cef2SBarry Smith    Notes:
4546a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4547d763cef2SBarry Smith 
45487db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
45497db568b7SBarry Smith 
45507db568b7SBarry 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
45517db568b7SBarry 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
45527db568b7SBarry Smith    as the first argument.
45537db568b7SBarry Smith 
45547db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
45557db568b7SBarry Smith 
4556d763cef2SBarry Smith    Level: intermediate
4557d763cef2SBarry Smith 
45587db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
4559d763cef2SBarry Smith 
45607db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
45617db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
45627db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
45637db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
45647db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
45657c922b88SBarry Smith 
4566d763cef2SBarry Smith @*/
4567a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4568d763cef2SBarry Smith {
45697f52daa2SLisandro Dalcin   PetscDraw      draw;
4570dfbe8321SBarry Smith   PetscErrorCode ierr;
4571d763cef2SBarry Smith 
4572d763cef2SBarry Smith   PetscFunctionBegin;
4573b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
45747f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
45757f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
45767f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4577287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
45787f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4579a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4580d763cef2SBarry Smith   PetscFunctionReturn(0);
4581d763cef2SBarry Smith }
4582d763cef2SBarry Smith 
4583b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4584d763cef2SBarry Smith {
45850b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4586c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4587dfbe8321SBarry Smith   PetscErrorCode ierr;
4588d763cef2SBarry Smith 
4589d763cef2SBarry Smith   PetscFunctionBegin;
459063e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4591b06615a5SLisandro Dalcin   if (!step) {
4592a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4593a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
45946934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr);
4595a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4596a9f9c1f6SBarry Smith   }
4597c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
459808763aebSBarry Smith   y =  PetscLog10Real(y);
45990b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4600b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
46010b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
46026934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
46033923b477SBarry Smith   }
4604d763cef2SBarry Smith   PetscFunctionReturn(0);
4605d763cef2SBarry Smith }
4606d763cef2SBarry Smith 
4607d763cef2SBarry Smith /*@C
4608a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4609a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4610d763cef2SBarry Smith 
46110b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4612d763cef2SBarry Smith 
4613d763cef2SBarry Smith    Input Parameter:
46140b039ecaSBarry Smith .  ctx - the monitor context
4615d763cef2SBarry Smith 
4616d763cef2SBarry Smith    Level: intermediate
4617d763cef2SBarry Smith 
4618d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
4619d763cef2SBarry Smith 
46204f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4621d763cef2SBarry Smith @*/
4622a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4623d763cef2SBarry Smith {
4624dfbe8321SBarry Smith   PetscErrorCode ierr;
4625d763cef2SBarry Smith 
4626d763cef2SBarry Smith   PetscFunctionBegin;
46277684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
46287684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
46297684fa3eSBarry Smith   }
46300b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
463131152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4632387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4633387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4634387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
46350b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4636d763cef2SBarry Smith   PetscFunctionReturn(0);
4637d763cef2SBarry Smith }
4638d763cef2SBarry Smith 
4639d763cef2SBarry Smith /*@
4640b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4641d763cef2SBarry Smith 
4642d763cef2SBarry Smith    Not Collective
4643d763cef2SBarry Smith 
4644d763cef2SBarry Smith    Input Parameter:
4645d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4646d763cef2SBarry Smith 
4647d763cef2SBarry Smith    Output Parameter:
464819eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4649d763cef2SBarry Smith 
4650d763cef2SBarry Smith    Level: beginner
4651d763cef2SBarry Smith 
4652b8123daeSJed Brown    Note:
4653b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
46549be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4655b8123daeSJed Brown 
4656aaa6c58dSLisandro Dalcin .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep()
4657d763cef2SBarry Smith 
4658d763cef2SBarry Smith .keywords: TS, get, time
4659d763cef2SBarry Smith @*/
46607087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4661d763cef2SBarry Smith {
4662d763cef2SBarry Smith   PetscFunctionBegin;
46630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4664f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4665d763cef2SBarry Smith   *t = ts->ptime;
4666d763cef2SBarry Smith   PetscFunctionReturn(0);
4667d763cef2SBarry Smith }
4668d763cef2SBarry Smith 
4669e5e524a1SHong Zhang /*@
4670e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4671e5e524a1SHong Zhang 
4672e5e524a1SHong Zhang    Not Collective
4673e5e524a1SHong Zhang 
4674e5e524a1SHong Zhang    Input Parameter:
4675e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4676e5e524a1SHong Zhang 
4677e5e524a1SHong Zhang    Output Parameter:
4678e5e524a1SHong Zhang .  t  - the previous time
4679e5e524a1SHong Zhang 
4680e5e524a1SHong Zhang    Level: beginner
4681e5e524a1SHong Zhang 
4682aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4683e5e524a1SHong Zhang 
4684e5e524a1SHong Zhang .keywords: TS, get, time
4685e5e524a1SHong Zhang @*/
4686e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4687e5e524a1SHong Zhang {
4688e5e524a1SHong Zhang   PetscFunctionBegin;
4689e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4690e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4691e5e524a1SHong Zhang   *t = ts->ptime_prev;
4692e5e524a1SHong Zhang   PetscFunctionReturn(0);
4693e5e524a1SHong Zhang }
4694e5e524a1SHong Zhang 
46956a4d4014SLisandro Dalcin /*@
46966a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
46976a4d4014SLisandro Dalcin 
46983f9fe445SBarry Smith    Logically Collective on TS
46996a4d4014SLisandro Dalcin 
47006a4d4014SLisandro Dalcin    Input Parameters:
47016a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
47026a4d4014SLisandro Dalcin -  time - the time
47036a4d4014SLisandro Dalcin 
47046a4d4014SLisandro Dalcin    Level: intermediate
47056a4d4014SLisandro Dalcin 
470619eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
47076a4d4014SLisandro Dalcin 
47086a4d4014SLisandro Dalcin .keywords: TS, set, time
47096a4d4014SLisandro Dalcin @*/
47107087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
47116a4d4014SLisandro Dalcin {
47126a4d4014SLisandro Dalcin   PetscFunctionBegin;
47130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4714c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
47156a4d4014SLisandro Dalcin   ts->ptime = t;
47166a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
47176a4d4014SLisandro Dalcin }
47186a4d4014SLisandro Dalcin 
4719d763cef2SBarry Smith /*@C
4720d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4721d763cef2SBarry Smith    TS options in the database.
4722d763cef2SBarry Smith 
47233f9fe445SBarry Smith    Logically Collective on TS
4724d763cef2SBarry Smith 
4725d763cef2SBarry Smith    Input Parameter:
4726d763cef2SBarry Smith +  ts     - The TS context
4727d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4728d763cef2SBarry Smith 
4729d763cef2SBarry Smith    Notes:
4730d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4731d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4732d763cef2SBarry Smith    hyphen.
4733d763cef2SBarry Smith 
4734d763cef2SBarry Smith    Level: advanced
4735d763cef2SBarry Smith 
4736d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4737d763cef2SBarry Smith 
4738d763cef2SBarry Smith .seealso: TSSetFromOptions()
4739d763cef2SBarry Smith 
4740d763cef2SBarry Smith @*/
47417087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4742d763cef2SBarry Smith {
4743dfbe8321SBarry Smith   PetscErrorCode ierr;
4744089b2837SJed Brown   SNES           snes;
4745d763cef2SBarry Smith 
4746d763cef2SBarry Smith   PetscFunctionBegin;
47470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4748d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4749089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4750089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4751d763cef2SBarry Smith   PetscFunctionReturn(0);
4752d763cef2SBarry Smith }
4753d763cef2SBarry Smith 
4754d763cef2SBarry Smith /*@C
4755d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4756d763cef2SBarry Smith    TS options in the database.
4757d763cef2SBarry Smith 
47583f9fe445SBarry Smith    Logically Collective on TS
4759d763cef2SBarry Smith 
4760d763cef2SBarry Smith    Input Parameter:
4761d763cef2SBarry Smith +  ts     - The TS context
4762d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4763d763cef2SBarry Smith 
4764d763cef2SBarry Smith    Notes:
4765d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4766d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4767d763cef2SBarry Smith    hyphen.
4768d763cef2SBarry Smith 
4769d763cef2SBarry Smith    Level: advanced
4770d763cef2SBarry Smith 
4771d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4772d763cef2SBarry Smith 
4773d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4774d763cef2SBarry Smith 
4775d763cef2SBarry Smith @*/
47767087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4777d763cef2SBarry Smith {
4778dfbe8321SBarry Smith   PetscErrorCode ierr;
4779089b2837SJed Brown   SNES           snes;
4780d763cef2SBarry Smith 
4781d763cef2SBarry Smith   PetscFunctionBegin;
47820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4783d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4784089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4785089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4786d763cef2SBarry Smith   PetscFunctionReturn(0);
4787d763cef2SBarry Smith }
4788d763cef2SBarry Smith 
4789d763cef2SBarry Smith /*@C
4790d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4791d763cef2SBarry Smith    TS options in the database.
4792d763cef2SBarry Smith 
4793d763cef2SBarry Smith    Not Collective
4794d763cef2SBarry Smith 
4795d763cef2SBarry Smith    Input Parameter:
4796d763cef2SBarry Smith .  ts - The TS context
4797d763cef2SBarry Smith 
4798d763cef2SBarry Smith    Output Parameter:
4799d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4800d763cef2SBarry Smith 
4801d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
4802d763cef2SBarry Smith    sufficient length to hold the prefix.
4803d763cef2SBarry Smith 
4804d763cef2SBarry Smith    Level: intermediate
4805d763cef2SBarry Smith 
4806d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4807d763cef2SBarry Smith 
4808d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4809d763cef2SBarry Smith @*/
48107087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4811d763cef2SBarry Smith {
4812dfbe8321SBarry Smith   PetscErrorCode ierr;
4813d763cef2SBarry Smith 
4814d763cef2SBarry Smith   PetscFunctionBegin;
48150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48164482741eSBarry Smith   PetscValidPointer(prefix,2);
4817d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4818d763cef2SBarry Smith   PetscFunctionReturn(0);
4819d763cef2SBarry Smith }
4820d763cef2SBarry Smith 
4821d763cef2SBarry Smith /*@C
4822d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4823d763cef2SBarry Smith 
4824d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4825d763cef2SBarry Smith 
4826d763cef2SBarry Smith    Input Parameter:
4827d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4828d763cef2SBarry Smith 
4829d763cef2SBarry Smith    Output Parameters:
4830e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4831e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4832e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4833e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4834d763cef2SBarry Smith 
48350298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
4836d763cef2SBarry Smith 
4837d763cef2SBarry Smith    Level: intermediate
4838d763cef2SBarry Smith 
483980275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4840d763cef2SBarry Smith 
4841d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4842d763cef2SBarry Smith @*/
4843e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4844d763cef2SBarry Smith {
4845089b2837SJed Brown   PetscErrorCode ierr;
484624989b8cSPeter Brune   DM             dm;
4847089b2837SJed Brown 
4848d763cef2SBarry Smith   PetscFunctionBegin;
484923a57915SBarry Smith   if (Amat || Pmat) {
485023a57915SBarry Smith     SNES snes;
4851089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
485223a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4853e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
485423a57915SBarry Smith   }
485524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
485624989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4857d763cef2SBarry Smith   PetscFunctionReturn(0);
4858d763cef2SBarry Smith }
4859d763cef2SBarry Smith 
48602eca1d9cSJed Brown /*@C
48612eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
48622eca1d9cSJed Brown 
48632eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
48642eca1d9cSJed Brown 
48652eca1d9cSJed Brown    Input Parameter:
48662eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
48672eca1d9cSJed Brown 
48682eca1d9cSJed Brown    Output Parameters:
4869e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4870e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
48712eca1d9cSJed Brown .  f   - The function to compute the matrices
48722eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
48732eca1d9cSJed Brown 
48740298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
48752eca1d9cSJed Brown 
48762eca1d9cSJed Brown    Level: advanced
48772eca1d9cSJed Brown 
487880275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
48792eca1d9cSJed Brown 
48802eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
48812eca1d9cSJed Brown @*/
4882e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
48832eca1d9cSJed Brown {
4884089b2837SJed Brown   PetscErrorCode ierr;
488524989b8cSPeter Brune   DM             dm;
48860910c330SBarry Smith 
48872eca1d9cSJed Brown   PetscFunctionBegin;
4888c0aab802Sstefano_zampini   if (Amat || Pmat) {
4889c0aab802Sstefano_zampini     SNES snes;
4890089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4891f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4892e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4893c0aab802Sstefano_zampini   }
489424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
489524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
48962eca1d9cSJed Brown   PetscFunctionReturn(0);
48972eca1d9cSJed Brown }
48982eca1d9cSJed Brown 
48991713a123SBarry Smith /*@C
490083a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
49011713a123SBarry Smith    VecView() for the solution at each timestep
49021713a123SBarry Smith 
49031713a123SBarry Smith    Collective on TS
49041713a123SBarry Smith 
49051713a123SBarry Smith    Input Parameters:
49061713a123SBarry Smith +  ts - the TS context
49071713a123SBarry Smith .  step - current time-step
4908142b95e3SSatish Balay .  ptime - current time
49090298fd71SBarry Smith -  dummy - either a viewer or NULL
49101713a123SBarry Smith 
491199fdda47SBarry Smith    Options Database:
491299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
491399fdda47SBarry Smith 
4914387f4636SBarry 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
491599fdda47SBarry Smith        will look bad
491699fdda47SBarry Smith 
49171713a123SBarry Smith    Level: intermediate
49181713a123SBarry Smith 
49191713a123SBarry Smith .keywords: TS,  vector, monitor, view
49201713a123SBarry Smith 
4921a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
49221713a123SBarry Smith @*/
49230910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
49241713a123SBarry Smith {
4925dfbe8321SBarry Smith   PetscErrorCode   ierr;
492683a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4927473a3ab2SBarry Smith   PetscDraw        draw;
49281713a123SBarry Smith 
49291713a123SBarry Smith   PetscFunctionBegin;
49306083293cSBarry Smith   if (!step && ictx->showinitial) {
49316083293cSBarry Smith     if (!ictx->initialsolution) {
49320910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
49331713a123SBarry Smith     }
49340910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
49356083293cSBarry Smith   }
4936b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
49370dcf80beSBarry Smith 
49386083293cSBarry Smith   if (ictx->showinitial) {
49396083293cSBarry Smith     PetscReal pause;
49406083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
49416083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
49426083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
49436083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
49446083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
49456083293cSBarry Smith   }
49460910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4947473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
494851fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4949473a3ab2SBarry Smith     char      time[32];
4950473a3ab2SBarry Smith 
4951473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
495285b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4953473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4954473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
495551fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4956473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4957473a3ab2SBarry Smith   }
4958473a3ab2SBarry Smith 
49596083293cSBarry Smith   if (ictx->showinitial) {
49606083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
49616083293cSBarry Smith   }
49621713a123SBarry Smith   PetscFunctionReturn(0);
49631713a123SBarry Smith }
49641713a123SBarry Smith 
49659110b2e7SHong Zhang /*@C
49669110b2e7SHong Zhang    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
49679110b2e7SHong Zhang    VecView() for the sensitivities to initial states at each timestep
49689110b2e7SHong Zhang 
49699110b2e7SHong Zhang    Collective on TS
49709110b2e7SHong Zhang 
49719110b2e7SHong Zhang    Input Parameters:
49729110b2e7SHong Zhang +  ts - the TS context
49739110b2e7SHong Zhang .  step - current time-step
49749110b2e7SHong Zhang .  ptime - current time
49759110b2e7SHong Zhang .  u - current state
49769110b2e7SHong Zhang .  numcost - number of cost functions
49779110b2e7SHong Zhang .  lambda - sensitivities to initial conditions
49789110b2e7SHong Zhang .  mu - sensitivities to parameters
49799110b2e7SHong Zhang -  dummy - either a viewer or NULL
49809110b2e7SHong Zhang 
49819110b2e7SHong Zhang    Level: intermediate
49829110b2e7SHong Zhang 
49839110b2e7SHong Zhang .keywords: TS,  vector, adjoint, monitor, view
49849110b2e7SHong Zhang 
49859110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
49869110b2e7SHong Zhang @*/
49879110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
49889110b2e7SHong Zhang {
49899110b2e7SHong Zhang   PetscErrorCode   ierr;
49909110b2e7SHong Zhang   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
49919110b2e7SHong Zhang   PetscDraw        draw;
4992a0046e2cSSatish Balay   PetscReal        xl,yl,xr,yr,h;
4993a0046e2cSSatish Balay   char             time[32];
49949110b2e7SHong Zhang 
49959110b2e7SHong Zhang   PetscFunctionBegin;
49969110b2e7SHong Zhang   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
49979110b2e7SHong Zhang 
49989110b2e7SHong Zhang   ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr);
49999110b2e7SHong Zhang   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
50009110b2e7SHong Zhang   ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
50019110b2e7SHong Zhang   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
50029110b2e7SHong Zhang   h    = yl + .95*(yr - yl);
50039110b2e7SHong Zhang   ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
50049110b2e7SHong Zhang   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
50059110b2e7SHong Zhang   PetscFunctionReturn(0);
50069110b2e7SHong Zhang }
50079110b2e7SHong Zhang 
50082d5ee99bSBarry Smith /*@C
50092d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
50102d5ee99bSBarry Smith 
50112d5ee99bSBarry Smith    Collective on TS
50122d5ee99bSBarry Smith 
50132d5ee99bSBarry Smith    Input Parameters:
50142d5ee99bSBarry Smith +  ts - the TS context
50152d5ee99bSBarry Smith .  step - current time-step
50162d5ee99bSBarry Smith .  ptime - current time
50172d5ee99bSBarry Smith -  dummy - either a viewer or NULL
50182d5ee99bSBarry Smith 
50192d5ee99bSBarry Smith    Level: intermediate
50202d5ee99bSBarry Smith 
50212d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
50222d5ee99bSBarry Smith 
50232d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
50242d5ee99bSBarry Smith @*/
50252d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
50262d5ee99bSBarry Smith {
50272d5ee99bSBarry Smith   PetscErrorCode    ierr;
50282d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
50292d5ee99bSBarry Smith   PetscDraw         draw;
50306934998bSLisandro Dalcin   PetscDrawAxis     axis;
50312d5ee99bSBarry Smith   PetscInt          n;
50322d5ee99bSBarry Smith   PetscMPIInt       size;
50336934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
50342d5ee99bSBarry Smith   char              time[32];
50352d5ee99bSBarry Smith   const PetscScalar *U;
50362d5ee99bSBarry Smith 
50372d5ee99bSBarry Smith   PetscFunctionBegin;
50386934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
50396934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
50402d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
50416934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
50422d5ee99bSBarry Smith 
50432d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
50446934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
50456934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
50466934998bSLisandro Dalcin   if (!step) {
50476934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
50486934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
50496934998bSLisandro Dalcin   }
50502d5ee99bSBarry Smith 
50512d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
50526934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
50536934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
5054a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
50556934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
50562d5ee99bSBarry Smith 
50576934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
50586934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
50592d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
50604b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
506185b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
50622d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
506351fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
50642d5ee99bSBarry Smith   }
50656934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
50662d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
506756d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
50686934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
50692d5ee99bSBarry Smith   PetscFunctionReturn(0);
50702d5ee99bSBarry Smith }
50712d5ee99bSBarry Smith 
50726083293cSBarry Smith /*@C
507383a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
50746083293cSBarry Smith 
50756083293cSBarry Smith    Collective on TS
50766083293cSBarry Smith 
50776083293cSBarry Smith    Input Parameters:
50786083293cSBarry Smith .    ctx - the monitor context
50796083293cSBarry Smith 
50806083293cSBarry Smith    Level: intermediate
50816083293cSBarry Smith 
50826083293cSBarry Smith .keywords: TS,  vector, monitor, view
50836083293cSBarry Smith 
508483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
50856083293cSBarry Smith @*/
508683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
50876083293cSBarry Smith {
50886083293cSBarry Smith   PetscErrorCode ierr;
50896083293cSBarry Smith 
50906083293cSBarry Smith   PetscFunctionBegin;
509183a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
509283a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
509383a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
50946083293cSBarry Smith   PetscFunctionReturn(0);
50956083293cSBarry Smith }
50966083293cSBarry Smith 
50976083293cSBarry Smith /*@C
509883a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
50996083293cSBarry Smith 
51006083293cSBarry Smith    Collective on TS
51016083293cSBarry Smith 
51026083293cSBarry Smith    Input Parameter:
51036083293cSBarry Smith .    ts - time-step context
51046083293cSBarry Smith 
51056083293cSBarry Smith    Output Patameter:
51066083293cSBarry Smith .    ctx - the monitor context
51076083293cSBarry Smith 
510899fdda47SBarry Smith    Options Database:
510999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
511099fdda47SBarry Smith 
51116083293cSBarry Smith    Level: intermediate
51126083293cSBarry Smith 
51136083293cSBarry Smith .keywords: TS,  vector, monitor, view
51146083293cSBarry Smith 
511583a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
51166083293cSBarry Smith @*/
511783a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
51186083293cSBarry Smith {
51196083293cSBarry Smith   PetscErrorCode   ierr;
51206083293cSBarry Smith 
51216083293cSBarry Smith   PetscFunctionBegin;
5122b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
512383a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
5124e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
5125bbd56ea5SKarl Rupp 
512683a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
5127473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
5128c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
5129473a3ab2SBarry Smith 
5130473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
5131c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
51326083293cSBarry Smith   PetscFunctionReturn(0);
51336083293cSBarry Smith }
51346083293cSBarry Smith 
51353a471f94SBarry Smith /*@C
513683a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
51373a471f94SBarry Smith    VecView() for the error at each timestep
51383a471f94SBarry Smith 
51393a471f94SBarry Smith    Collective on TS
51403a471f94SBarry Smith 
51413a471f94SBarry Smith    Input Parameters:
51423a471f94SBarry Smith +  ts - the TS context
51433a471f94SBarry Smith .  step - current time-step
51443a471f94SBarry Smith .  ptime - current time
51450298fd71SBarry Smith -  dummy - either a viewer or NULL
51463a471f94SBarry Smith 
51473a471f94SBarry Smith    Level: intermediate
51483a471f94SBarry Smith 
51493a471f94SBarry Smith .keywords: TS,  vector, monitor, view
51503a471f94SBarry Smith 
51513a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
51523a471f94SBarry Smith @*/
51530910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
51543a471f94SBarry Smith {
51553a471f94SBarry Smith   PetscErrorCode   ierr;
515683a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
515783a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
51583a471f94SBarry Smith   Vec              work;
51593a471f94SBarry Smith 
51603a471f94SBarry Smith   PetscFunctionBegin;
5161b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
51620910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
51633a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
51640910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
51653a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
51663a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
51673a471f94SBarry Smith   PetscFunctionReturn(0);
51683a471f94SBarry Smith }
51693a471f94SBarry Smith 
5170af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
51716c699258SBarry Smith /*@
51722a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
51736c699258SBarry Smith 
51743f9fe445SBarry Smith    Logically Collective on TS and DM
51756c699258SBarry Smith 
51766c699258SBarry Smith    Input Parameters:
51772a808120SBarry Smith +  ts - the ODE integrator object
51782a808120SBarry Smith -  dm - the dm, cannot be NULL
51796c699258SBarry Smith 
51806c699258SBarry Smith    Level: intermediate
51816c699258SBarry Smith 
51826c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
51836c699258SBarry Smith @*/
51847087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
51856c699258SBarry Smith {
51866c699258SBarry Smith   PetscErrorCode ierr;
5187089b2837SJed Brown   SNES           snes;
5188942e3340SBarry Smith   DMTS           tsdm;
51896c699258SBarry Smith 
51906c699258SBarry Smith   PetscFunctionBegin;
51910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
51922a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
519370663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
5194942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
51952a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
5196942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
5197942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
519824989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
519924989b8cSPeter Brune         tsdm->originaldm = dm;
520024989b8cSPeter Brune       }
520124989b8cSPeter Brune     }
52026bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
520324989b8cSPeter Brune   }
52046c699258SBarry Smith   ts->dm = dm;
5205bbd56ea5SKarl Rupp 
5206089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5207089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
52086c699258SBarry Smith   PetscFunctionReturn(0);
52096c699258SBarry Smith }
52106c699258SBarry Smith 
52116c699258SBarry Smith /*@
52126c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
52136c699258SBarry Smith 
52143f9fe445SBarry Smith    Not Collective
52156c699258SBarry Smith 
52166c699258SBarry Smith    Input Parameter:
52176c699258SBarry Smith . ts - the preconditioner context
52186c699258SBarry Smith 
52196c699258SBarry Smith    Output Parameter:
52206c699258SBarry Smith .  dm - the dm
52216c699258SBarry Smith 
52226c699258SBarry Smith    Level: intermediate
52236c699258SBarry Smith 
52246c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
52256c699258SBarry Smith @*/
52267087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
52276c699258SBarry Smith {
5228496e6a7aSJed Brown   PetscErrorCode ierr;
5229496e6a7aSJed Brown 
52306c699258SBarry Smith   PetscFunctionBegin;
52310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5232496e6a7aSJed Brown   if (!ts->dm) {
5233ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
5234496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
5235496e6a7aSJed Brown   }
52366c699258SBarry Smith   *dm = ts->dm;
52376c699258SBarry Smith   PetscFunctionReturn(0);
52386c699258SBarry Smith }
52391713a123SBarry Smith 
52400f5c6efeSJed Brown /*@
52410f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
52420f5c6efeSJed Brown 
52433f9fe445SBarry Smith    Logically Collective on SNES
52440f5c6efeSJed Brown 
52450f5c6efeSJed Brown    Input Parameter:
5246d42a1c89SJed Brown + snes - nonlinear solver
52470910c330SBarry Smith . U - the current state at which to evaluate the residual
5248d42a1c89SJed Brown - ctx - user context, must be a TS
52490f5c6efeSJed Brown 
52500f5c6efeSJed Brown    Output Parameter:
52510f5c6efeSJed Brown . F - the nonlinear residual
52520f5c6efeSJed Brown 
52530f5c6efeSJed Brown    Notes:
52540f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
52550f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
52560f5c6efeSJed Brown 
52570f5c6efeSJed Brown    Level: advanced
52580f5c6efeSJed Brown 
52590f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
52600f5c6efeSJed Brown @*/
52610910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
52620f5c6efeSJed Brown {
52630f5c6efeSJed Brown   TS             ts = (TS)ctx;
52640f5c6efeSJed Brown   PetscErrorCode ierr;
52650f5c6efeSJed Brown 
52660f5c6efeSJed Brown   PetscFunctionBegin;
52670f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
52680910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
52690f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
52700f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
52710910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
52720f5c6efeSJed Brown   PetscFunctionReturn(0);
52730f5c6efeSJed Brown }
52740f5c6efeSJed Brown 
52750f5c6efeSJed Brown /*@
52760f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
52770f5c6efeSJed Brown 
52780f5c6efeSJed Brown    Collective on SNES
52790f5c6efeSJed Brown 
52800f5c6efeSJed Brown    Input Parameter:
52810f5c6efeSJed Brown + snes - nonlinear solver
52820910c330SBarry Smith . U - the current state at which to evaluate the residual
52830f5c6efeSJed Brown - ctx - user context, must be a TS
52840f5c6efeSJed Brown 
52850f5c6efeSJed Brown    Output Parameter:
52860f5c6efeSJed Brown + A - the Jacobian
52870f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
52880f5c6efeSJed Brown - flag - indicates any structure change in the matrix
52890f5c6efeSJed Brown 
52900f5c6efeSJed Brown    Notes:
52910f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
52920f5c6efeSJed Brown 
52930f5c6efeSJed Brown    Level: developer
52940f5c6efeSJed Brown 
52950f5c6efeSJed Brown .seealso: SNESSetJacobian()
52960f5c6efeSJed Brown @*/
5297d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
52980f5c6efeSJed Brown {
52990f5c6efeSJed Brown   TS             ts = (TS)ctx;
53000f5c6efeSJed Brown   PetscErrorCode ierr;
53010f5c6efeSJed Brown 
53020f5c6efeSJed Brown   PetscFunctionBegin;
53030f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
53040910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
53050f5c6efeSJed Brown   PetscValidPointer(A,3);
530694ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
53070f5c6efeSJed Brown   PetscValidPointer(B,4);
530894ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
53090f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5310d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
53110f5c6efeSJed Brown   PetscFunctionReturn(0);
53120f5c6efeSJed Brown }
5313325fc9f4SBarry Smith 
53140e4ef248SJed Brown /*@C
53159ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
53160e4ef248SJed Brown 
53170e4ef248SJed Brown    Collective on TS
53180e4ef248SJed Brown 
53190e4ef248SJed Brown    Input Arguments:
53200e4ef248SJed Brown +  ts - time stepping context
53210e4ef248SJed Brown .  t - time at which to evaluate
53220910c330SBarry Smith .  U - state at which to evaluate
53230e4ef248SJed Brown -  ctx - context
53240e4ef248SJed Brown 
53250e4ef248SJed Brown    Output Arguments:
53260e4ef248SJed Brown .  F - right hand side
53270e4ef248SJed Brown 
53280e4ef248SJed Brown    Level: intermediate
53290e4ef248SJed Brown 
53300e4ef248SJed Brown    Notes:
53310e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
53320e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
53330e4ef248SJed Brown 
53340e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
53350e4ef248SJed Brown @*/
53360910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
53370e4ef248SJed Brown {
53380e4ef248SJed Brown   PetscErrorCode ierr;
53390e4ef248SJed Brown   Mat            Arhs,Brhs;
53400e4ef248SJed Brown 
53410e4ef248SJed Brown   PetscFunctionBegin;
53420e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
5343d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
53440910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
53450e4ef248SJed Brown   PetscFunctionReturn(0);
53460e4ef248SJed Brown }
53470e4ef248SJed Brown 
53480e4ef248SJed Brown /*@C
53490e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
53500e4ef248SJed Brown 
53510e4ef248SJed Brown    Collective on TS
53520e4ef248SJed Brown 
53530e4ef248SJed Brown    Input Arguments:
53540e4ef248SJed Brown +  ts - time stepping context
53550e4ef248SJed Brown .  t - time at which to evaluate
53560910c330SBarry Smith .  U - state at which to evaluate
53570e4ef248SJed Brown -  ctx - context
53580e4ef248SJed Brown 
53590e4ef248SJed Brown    Output Arguments:
53600e4ef248SJed Brown +  A - pointer to operator
53610e4ef248SJed Brown .  B - pointer to preconditioning matrix
53620e4ef248SJed Brown -  flg - matrix structure flag
53630e4ef248SJed Brown 
53640e4ef248SJed Brown    Level: intermediate
53650e4ef248SJed Brown 
53660e4ef248SJed Brown    Notes:
53670e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
53680e4ef248SJed Brown 
53690e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
53700e4ef248SJed Brown @*/
5371d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
53720e4ef248SJed Brown {
53730e4ef248SJed Brown   PetscFunctionBegin;
53740e4ef248SJed Brown   PetscFunctionReturn(0);
53750e4ef248SJed Brown }
53760e4ef248SJed Brown 
53770026cea9SSean Farley /*@C
53780026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
53790026cea9SSean Farley 
53800026cea9SSean Farley    Collective on TS
53810026cea9SSean Farley 
53820026cea9SSean Farley    Input Arguments:
53830026cea9SSean Farley +  ts - time stepping context
53840026cea9SSean Farley .  t - time at which to evaluate
53850910c330SBarry Smith .  U - state at which to evaluate
53860910c330SBarry Smith .  Udot - time derivative of state vector
53870026cea9SSean Farley -  ctx - context
53880026cea9SSean Farley 
53890026cea9SSean Farley    Output Arguments:
53900026cea9SSean Farley .  F - left hand side
53910026cea9SSean Farley 
53920026cea9SSean Farley    Level: intermediate
53930026cea9SSean Farley 
53940026cea9SSean Farley    Notes:
53950910c330SBarry 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
53960026cea9SSean Farley    user is required to write their own TSComputeIFunction.
53970026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
53980026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
53990026cea9SSean Farley 
54009ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
54019ae8fd06SBarry Smith 
54029ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
54030026cea9SSean Farley @*/
54040910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
54050026cea9SSean Farley {
54060026cea9SSean Farley   PetscErrorCode ierr;
54070026cea9SSean Farley   Mat            A,B;
54080026cea9SSean Farley 
54090026cea9SSean Farley   PetscFunctionBegin;
54100298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5411d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
54120910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
54130026cea9SSean Farley   PetscFunctionReturn(0);
54140026cea9SSean Farley }
54150026cea9SSean Farley 
54160026cea9SSean Farley /*@C
5417b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
54180026cea9SSean Farley 
54190026cea9SSean Farley    Collective on TS
54200026cea9SSean Farley 
54210026cea9SSean Farley    Input Arguments:
54220026cea9SSean Farley +  ts - time stepping context
54230026cea9SSean Farley .  t - time at which to evaluate
54240910c330SBarry Smith .  U - state at which to evaluate
54250910c330SBarry Smith .  Udot - time derivative of state vector
54260026cea9SSean Farley .  shift - shift to apply
54270026cea9SSean Farley -  ctx - context
54280026cea9SSean Farley 
54290026cea9SSean Farley    Output Arguments:
54300026cea9SSean Farley +  A - pointer to operator
54310026cea9SSean Farley .  B - pointer to preconditioning matrix
54320026cea9SSean Farley -  flg - matrix structure flag
54330026cea9SSean Farley 
5434b41af12eSJed Brown    Level: advanced
54350026cea9SSean Farley 
54360026cea9SSean Farley    Notes:
54370026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
54380026cea9SSean Farley 
5439b41af12eSJed Brown    It is only appropriate for problems of the form
5440b41af12eSJed Brown 
5441b41af12eSJed Brown $     M Udot = F(U,t)
5442b41af12eSJed Brown 
5443b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5444b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5445b41af12eSJed Brown   an implicit operator of the form
5446b41af12eSJed Brown 
5447b41af12eSJed Brown $    shift*M + J
5448b41af12eSJed Brown 
5449b41af12eSJed 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
5450b41af12eSJed Brown   a copy of M or reassemble it when requested.
5451b41af12eSJed Brown 
54520026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
54530026cea9SSean Farley @*/
5454d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
54550026cea9SSean Farley {
5456b41af12eSJed Brown   PetscErrorCode ierr;
5457b41af12eSJed Brown 
54580026cea9SSean Farley   PetscFunctionBegin;
545994ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5460b41af12eSJed Brown   ts->ijacobian.shift = shift;
54610026cea9SSean Farley   PetscFunctionReturn(0);
54620026cea9SSean Farley }
5463b41af12eSJed Brown 
5464e817cc15SEmil Constantinescu /*@
5465e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5466e817cc15SEmil Constantinescu 
5467e817cc15SEmil Constantinescu    Not Collective
5468e817cc15SEmil Constantinescu 
5469e817cc15SEmil Constantinescu    Input Parameter:
5470e817cc15SEmil Constantinescu .  ts - the TS context
5471e817cc15SEmil Constantinescu 
5472e817cc15SEmil Constantinescu    Output Parameter:
54734e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5474e817cc15SEmil Constantinescu 
5475e817cc15SEmil Constantinescu    Level: beginner
5476e817cc15SEmil Constantinescu 
5477e817cc15SEmil Constantinescu .keywords: TS, equation type
5478e817cc15SEmil Constantinescu 
5479e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5480e817cc15SEmil Constantinescu @*/
5481e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5482e817cc15SEmil Constantinescu {
5483e817cc15SEmil Constantinescu   PetscFunctionBegin;
5484e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5485e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5486e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5487e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5488e817cc15SEmil Constantinescu }
5489e817cc15SEmil Constantinescu 
5490e817cc15SEmil Constantinescu /*@
5491e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5492e817cc15SEmil Constantinescu 
5493e817cc15SEmil Constantinescu    Not Collective
5494e817cc15SEmil Constantinescu 
5495e817cc15SEmil Constantinescu    Input Parameter:
5496e817cc15SEmil Constantinescu +  ts - the TS context
54971297b224SEmil Constantinescu -  equation_type - see TSEquationType
5498e817cc15SEmil Constantinescu 
5499e817cc15SEmil Constantinescu    Level: advanced
5500e817cc15SEmil Constantinescu 
5501e817cc15SEmil Constantinescu .keywords: TS, equation type
5502e817cc15SEmil Constantinescu 
5503e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5504e817cc15SEmil Constantinescu @*/
5505e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5506e817cc15SEmil Constantinescu {
5507e817cc15SEmil Constantinescu   PetscFunctionBegin;
5508e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5509e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5510e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5511e817cc15SEmil Constantinescu }
55120026cea9SSean Farley 
55134af1b03aSJed Brown /*@
55144af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
55154af1b03aSJed Brown 
55164af1b03aSJed Brown    Not Collective
55174af1b03aSJed Brown 
55184af1b03aSJed Brown    Input Parameter:
55194af1b03aSJed Brown .  ts - the TS context
55204af1b03aSJed Brown 
55214af1b03aSJed Brown    Output Parameter:
55224af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
55234af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
55244af1b03aSJed Brown 
5525487e0bb9SJed Brown    Level: beginner
55264af1b03aSJed Brown 
5527cd652676SJed Brown    Notes:
5528cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
55294af1b03aSJed Brown 
55304af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
55314af1b03aSJed Brown 
55324af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
55334af1b03aSJed Brown @*/
55344af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
55354af1b03aSJed Brown {
55364af1b03aSJed Brown   PetscFunctionBegin;
55374af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
55384af1b03aSJed Brown   PetscValidPointer(reason,2);
55394af1b03aSJed Brown   *reason = ts->reason;
55404af1b03aSJed Brown   PetscFunctionReturn(0);
55414af1b03aSJed Brown }
55424af1b03aSJed Brown 
5543d6ad946cSShri Abhyankar /*@
5544d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5545d6ad946cSShri Abhyankar 
5546d6ad946cSShri Abhyankar    Not Collective
5547d6ad946cSShri Abhyankar 
5548d6ad946cSShri Abhyankar    Input Parameter:
5549d6ad946cSShri Abhyankar +  ts - the TS context
5550d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5551d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5552d6ad946cSShri Abhyankar 
5553f5abba47SShri Abhyankar    Level: advanced
5554d6ad946cSShri Abhyankar 
5555d6ad946cSShri Abhyankar    Notes:
5556d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
5557d6ad946cSShri Abhyankar 
5558d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
5559d6ad946cSShri Abhyankar 
5560d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5561d6ad946cSShri Abhyankar @*/
5562d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5563d6ad946cSShri Abhyankar {
5564d6ad946cSShri Abhyankar   PetscFunctionBegin;
5565d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5566d6ad946cSShri Abhyankar   ts->reason = reason;
5567d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5568d6ad946cSShri Abhyankar }
5569d6ad946cSShri Abhyankar 
5570cc708dedSBarry Smith /*@
5571cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5572cc708dedSBarry Smith 
5573cc708dedSBarry Smith    Not Collective
5574cc708dedSBarry Smith 
5575cc708dedSBarry Smith    Input Parameter:
5576cc708dedSBarry Smith .  ts - the TS context
5577cc708dedSBarry Smith 
5578cc708dedSBarry Smith    Output Parameter:
557919eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5580cc708dedSBarry Smith 
5581487e0bb9SJed Brown    Level: beginner
5582cc708dedSBarry Smith 
5583cc708dedSBarry Smith    Notes:
5584cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5585cc708dedSBarry Smith 
5586cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
5587cc708dedSBarry Smith 
5588cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5589cc708dedSBarry Smith @*/
5590cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5591cc708dedSBarry Smith {
5592cc708dedSBarry Smith   PetscFunctionBegin;
5593cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5594cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5595cc708dedSBarry Smith   *ftime = ts->solvetime;
5596cc708dedSBarry Smith   PetscFunctionReturn(0);
5597cc708dedSBarry Smith }
5598cc708dedSBarry Smith 
55992c18e0fdSBarry Smith /*@
56005ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
56019f67acb7SJed Brown    used by the time integrator.
56029f67acb7SJed Brown 
56039f67acb7SJed Brown    Not Collective
56049f67acb7SJed Brown 
56059f67acb7SJed Brown    Input Parameter:
56069f67acb7SJed Brown .  ts - TS context
56079f67acb7SJed Brown 
56089f67acb7SJed Brown    Output Parameter:
56099f67acb7SJed Brown .  nits - number of nonlinear iterations
56109f67acb7SJed Brown 
56119f67acb7SJed Brown    Notes:
56129f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
56139f67acb7SJed Brown 
56149f67acb7SJed Brown    Level: intermediate
56159f67acb7SJed Brown 
56169f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
56179f67acb7SJed Brown 
56185ef26d82SJed Brown .seealso:  TSGetKSPIterations()
56199f67acb7SJed Brown @*/
56205ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
56219f67acb7SJed Brown {
56229f67acb7SJed Brown   PetscFunctionBegin;
56239f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
56249f67acb7SJed Brown   PetscValidIntPointer(nits,2);
56255ef26d82SJed Brown   *nits = ts->snes_its;
56269f67acb7SJed Brown   PetscFunctionReturn(0);
56279f67acb7SJed Brown }
56289f67acb7SJed Brown 
56299f67acb7SJed Brown /*@
56305ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
56319f67acb7SJed Brown    used by the time integrator.
56329f67acb7SJed Brown 
56339f67acb7SJed Brown    Not Collective
56349f67acb7SJed Brown 
56359f67acb7SJed Brown    Input Parameter:
56369f67acb7SJed Brown .  ts - TS context
56379f67acb7SJed Brown 
56389f67acb7SJed Brown    Output Parameter:
56399f67acb7SJed Brown .  lits - number of linear iterations
56409f67acb7SJed Brown 
56419f67acb7SJed Brown    Notes:
56429f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
56439f67acb7SJed Brown 
56449f67acb7SJed Brown    Level: intermediate
56459f67acb7SJed Brown 
56469f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
56479f67acb7SJed Brown 
56485ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
56499f67acb7SJed Brown @*/
56505ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
56519f67acb7SJed Brown {
56529f67acb7SJed Brown   PetscFunctionBegin;
56539f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
56549f67acb7SJed Brown   PetscValidIntPointer(lits,2);
56555ef26d82SJed Brown   *lits = ts->ksp_its;
56569f67acb7SJed Brown   PetscFunctionReturn(0);
56579f67acb7SJed Brown }
56589f67acb7SJed Brown 
5659cef5090cSJed Brown /*@
5660cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5661cef5090cSJed Brown 
5662cef5090cSJed Brown    Not Collective
5663cef5090cSJed Brown 
5664cef5090cSJed Brown    Input Parameter:
5665cef5090cSJed Brown .  ts - TS context
5666cef5090cSJed Brown 
5667cef5090cSJed Brown    Output Parameter:
5668cef5090cSJed Brown .  rejects - number of steps rejected
5669cef5090cSJed Brown 
5670cef5090cSJed Brown    Notes:
5671cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5672cef5090cSJed Brown 
5673cef5090cSJed Brown    Level: intermediate
5674cef5090cSJed Brown 
5675cef5090cSJed Brown .keywords: TS, get, number
5676cef5090cSJed Brown 
56775ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5678cef5090cSJed Brown @*/
5679cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5680cef5090cSJed Brown {
5681cef5090cSJed Brown   PetscFunctionBegin;
5682cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5683cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5684cef5090cSJed Brown   *rejects = ts->reject;
5685cef5090cSJed Brown   PetscFunctionReturn(0);
5686cef5090cSJed Brown }
5687cef5090cSJed Brown 
5688cef5090cSJed Brown /*@
5689cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5690cef5090cSJed Brown 
5691cef5090cSJed Brown    Not Collective
5692cef5090cSJed Brown 
5693cef5090cSJed Brown    Input Parameter:
5694cef5090cSJed Brown .  ts - TS context
5695cef5090cSJed Brown 
5696cef5090cSJed Brown    Output Parameter:
5697cef5090cSJed Brown .  fails - number of failed nonlinear solves
5698cef5090cSJed Brown 
5699cef5090cSJed Brown    Notes:
5700cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5701cef5090cSJed Brown 
5702cef5090cSJed Brown    Level: intermediate
5703cef5090cSJed Brown 
5704cef5090cSJed Brown .keywords: TS, get, number
5705cef5090cSJed Brown 
57065ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5707cef5090cSJed Brown @*/
5708cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5709cef5090cSJed Brown {
5710cef5090cSJed Brown   PetscFunctionBegin;
5711cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5712cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5713cef5090cSJed Brown   *fails = ts->num_snes_failures;
5714cef5090cSJed Brown   PetscFunctionReturn(0);
5715cef5090cSJed Brown }
5716cef5090cSJed Brown 
5717cef5090cSJed Brown /*@
5718cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5719cef5090cSJed Brown 
5720cef5090cSJed Brown    Not Collective
5721cef5090cSJed Brown 
5722cef5090cSJed Brown    Input Parameter:
5723cef5090cSJed Brown +  ts - TS context
5724cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5725cef5090cSJed Brown 
5726cef5090cSJed Brown    Notes:
5727cef5090cSJed Brown    The counter is reset to zero for each step
5728cef5090cSJed Brown 
5729cef5090cSJed Brown    Options Database Key:
5730cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5731cef5090cSJed Brown 
5732cef5090cSJed Brown    Level: intermediate
5733cef5090cSJed Brown 
5734cef5090cSJed Brown .keywords: TS, set, maximum, number
5735cef5090cSJed Brown 
57365ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5737cef5090cSJed Brown @*/
5738cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5739cef5090cSJed Brown {
5740cef5090cSJed Brown   PetscFunctionBegin;
5741cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5742cef5090cSJed Brown   ts->max_reject = rejects;
5743cef5090cSJed Brown   PetscFunctionReturn(0);
5744cef5090cSJed Brown }
5745cef5090cSJed Brown 
5746cef5090cSJed Brown /*@
5747cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5748cef5090cSJed Brown 
5749cef5090cSJed Brown    Not Collective
5750cef5090cSJed Brown 
5751cef5090cSJed Brown    Input Parameter:
5752cef5090cSJed Brown +  ts - TS context
5753cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5754cef5090cSJed Brown 
5755cef5090cSJed Brown    Notes:
5756cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5757cef5090cSJed Brown 
5758cef5090cSJed Brown    Options Database Key:
5759cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5760cef5090cSJed Brown 
5761cef5090cSJed Brown    Level: intermediate
5762cef5090cSJed Brown 
5763cef5090cSJed Brown .keywords: TS, set, maximum, number
5764cef5090cSJed Brown 
57655ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5766cef5090cSJed Brown @*/
5767cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5768cef5090cSJed Brown {
5769cef5090cSJed Brown   PetscFunctionBegin;
5770cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5771cef5090cSJed Brown   ts->max_snes_failures = fails;
5772cef5090cSJed Brown   PetscFunctionReturn(0);
5773cef5090cSJed Brown }
5774cef5090cSJed Brown 
5775cef5090cSJed Brown /*@
5776cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5777cef5090cSJed Brown 
5778cef5090cSJed Brown    Not Collective
5779cef5090cSJed Brown 
5780cef5090cSJed Brown    Input Parameter:
5781cef5090cSJed Brown +  ts - TS context
5782cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5783cef5090cSJed Brown 
5784cef5090cSJed Brown    Options Database Key:
5785cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5786cef5090cSJed Brown 
5787cef5090cSJed Brown    Level: intermediate
5788cef5090cSJed Brown 
5789cef5090cSJed Brown .keywords: TS, set, error
5790cef5090cSJed Brown 
57915ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5792cef5090cSJed Brown @*/
5793cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5794cef5090cSJed Brown {
5795cef5090cSJed Brown   PetscFunctionBegin;
5796cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5797cef5090cSJed Brown   ts->errorifstepfailed = err;
5798cef5090cSJed Brown   PetscFunctionReturn(0);
5799cef5090cSJed Brown }
5800cef5090cSJed Brown 
5801fb1732b5SBarry Smith /*@C
5802fde5950dSBarry 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
5803fb1732b5SBarry Smith 
5804fb1732b5SBarry Smith    Collective on TS
5805fb1732b5SBarry Smith 
5806fb1732b5SBarry Smith    Input Parameters:
5807fb1732b5SBarry Smith +  ts - the TS context
5808fb1732b5SBarry Smith .  step - current time-step
5809fb1732b5SBarry Smith .  ptime - current time
58100910c330SBarry Smith .  u - current state
5811721cd6eeSBarry Smith -  vf - viewer and its format
5812fb1732b5SBarry Smith 
5813fb1732b5SBarry Smith    Level: intermediate
5814fb1732b5SBarry Smith 
5815fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5816fb1732b5SBarry Smith 
5817fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5818fb1732b5SBarry Smith @*/
5819721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5820fb1732b5SBarry Smith {
5821fb1732b5SBarry Smith   PetscErrorCode ierr;
5822fb1732b5SBarry Smith 
5823fb1732b5SBarry Smith   PetscFunctionBegin;
5824721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5825721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5826721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5827ed81e22dSJed Brown   PetscFunctionReturn(0);
5828ed81e22dSJed Brown }
5829ed81e22dSJed Brown 
5830ed81e22dSJed Brown /*@C
5831ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5832ed81e22dSJed Brown 
5833ed81e22dSJed Brown    Collective on TS
5834ed81e22dSJed Brown 
5835ed81e22dSJed Brown    Input Parameters:
5836ed81e22dSJed Brown +  ts - the TS context
5837ed81e22dSJed Brown .  step - current time-step
5838ed81e22dSJed Brown .  ptime - current time
58390910c330SBarry Smith .  u - current state
5840ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5841ed81e22dSJed Brown 
5842ed81e22dSJed Brown    Level: intermediate
5843ed81e22dSJed Brown 
5844ed81e22dSJed Brown    Notes:
5845ed81e22dSJed 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.
5846ed81e22dSJed Brown    These are named according to the file name template.
5847ed81e22dSJed Brown 
5848ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5849ed81e22dSJed Brown 
5850ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5851ed81e22dSJed Brown 
5852ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5853ed81e22dSJed Brown @*/
58540910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5855ed81e22dSJed Brown {
5856ed81e22dSJed Brown   PetscErrorCode ierr;
5857ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5858ed81e22dSJed Brown   PetscViewer    viewer;
5859ed81e22dSJed Brown 
5860ed81e22dSJed Brown   PetscFunctionBegin;
586163e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
58628caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5863ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
58640910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5865ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5866ed81e22dSJed Brown   PetscFunctionReturn(0);
5867ed81e22dSJed Brown }
5868ed81e22dSJed Brown 
5869ed81e22dSJed Brown /*@C
5870ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5871ed81e22dSJed Brown 
5872ed81e22dSJed Brown    Collective on TS
5873ed81e22dSJed Brown 
5874ed81e22dSJed Brown    Input Parameters:
5875ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5876ed81e22dSJed Brown 
5877ed81e22dSJed Brown    Level: intermediate
5878ed81e22dSJed Brown 
5879ed81e22dSJed Brown    Note:
5880ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5881ed81e22dSJed Brown 
5882ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5883ed81e22dSJed Brown 
5884ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5885ed81e22dSJed Brown @*/
5886ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5887ed81e22dSJed Brown {
5888ed81e22dSJed Brown   PetscErrorCode ierr;
5889ed81e22dSJed Brown 
5890ed81e22dSJed Brown   PetscFunctionBegin;
5891ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5892fb1732b5SBarry Smith   PetscFunctionReturn(0);
5893fb1732b5SBarry Smith }
5894fb1732b5SBarry Smith 
589584df9cb4SJed Brown /*@
5896552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
589784df9cb4SJed Brown 
5898ed81e22dSJed Brown    Collective on TS if controller has not been created yet
589984df9cb4SJed Brown 
590084df9cb4SJed Brown    Input Arguments:
5901ed81e22dSJed Brown .  ts - time stepping context
590284df9cb4SJed Brown 
590384df9cb4SJed Brown    Output Arguments:
5904ed81e22dSJed Brown .  adapt - adaptive controller
590584df9cb4SJed Brown 
590684df9cb4SJed Brown    Level: intermediate
590784df9cb4SJed Brown 
5908ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
590984df9cb4SJed Brown @*/
5910552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
591184df9cb4SJed Brown {
591284df9cb4SJed Brown   PetscErrorCode ierr;
591384df9cb4SJed Brown 
591484df9cb4SJed Brown   PetscFunctionBegin;
591584df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5916bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
591784df9cb4SJed Brown   if (!ts->adapt) {
5918ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
59193bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
59201c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
592184df9cb4SJed Brown   }
5922bec58848SLisandro Dalcin   *adapt = ts->adapt;
592384df9cb4SJed Brown   PetscFunctionReturn(0);
592484df9cb4SJed Brown }
5925d6ebe24aSShri Abhyankar 
59261c3436cfSJed Brown /*@
59271c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
59281c3436cfSJed Brown 
59291c3436cfSJed Brown    Logically Collective
59301c3436cfSJed Brown 
59311c3436cfSJed Brown    Input Arguments:
59321c3436cfSJed Brown +  ts - time integration context
59331c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
59340298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
59351c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
59360298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
59371c3436cfSJed Brown 
5938a3cdaa26SBarry Smith    Options Database keys:
5939a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5940a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5941a3cdaa26SBarry Smith 
59423ff766beSShri Abhyankar    Notes:
59433ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
59443ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
59453ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
59463ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
59473ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
59483ff766beSShri Abhyankar    differential variables.
59493ff766beSShri Abhyankar 
59501c3436cfSJed Brown    Level: beginner
59511c3436cfSJed Brown 
5952c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
59531c3436cfSJed Brown @*/
59541c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
59551c3436cfSJed Brown {
59561c3436cfSJed Brown   PetscErrorCode ierr;
59571c3436cfSJed Brown 
59581c3436cfSJed Brown   PetscFunctionBegin;
5959c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
59601c3436cfSJed Brown   if (vatol) {
59611c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
59621c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
59631c3436cfSJed Brown     ts->vatol = vatol;
59641c3436cfSJed Brown   }
5965c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
59661c3436cfSJed Brown   if (vrtol) {
59671c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
59681c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
59691c3436cfSJed Brown     ts->vrtol = vrtol;
59701c3436cfSJed Brown   }
59711c3436cfSJed Brown   PetscFunctionReturn(0);
59721c3436cfSJed Brown }
59731c3436cfSJed Brown 
5974c5033834SJed Brown /*@
5975c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5976c5033834SJed Brown 
5977c5033834SJed Brown    Logically Collective
5978c5033834SJed Brown 
5979c5033834SJed Brown    Input Arguments:
5980c5033834SJed Brown .  ts - time integration context
5981c5033834SJed Brown 
5982c5033834SJed Brown    Output Arguments:
59830298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
59840298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
59850298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
59860298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5987c5033834SJed Brown 
5988c5033834SJed Brown    Level: beginner
5989c5033834SJed Brown 
5990c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5991c5033834SJed Brown @*/
5992c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5993c5033834SJed Brown {
5994c5033834SJed Brown   PetscFunctionBegin;
5995c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5996c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5997c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5998c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5999c5033834SJed Brown   PetscFunctionReturn(0);
6000c5033834SJed Brown }
6001c5033834SJed Brown 
60029c6b16b5SShri Abhyankar /*@
6003a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
60049c6b16b5SShri Abhyankar 
60059c6b16b5SShri Abhyankar    Collective on TS
60069c6b16b5SShri Abhyankar 
60079c6b16b5SShri Abhyankar    Input Arguments:
60089c6b16b5SShri Abhyankar +  ts - time stepping context
6009a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6010a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
60119c6b16b5SShri Abhyankar 
60129c6b16b5SShri Abhyankar    Output Arguments:
60137453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
60147453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
60157453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
60169c6b16b5SShri Abhyankar 
60179c6b16b5SShri Abhyankar    Level: developer
60189c6b16b5SShri Abhyankar 
6019deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
60209c6b16b5SShri Abhyankar @*/
60217453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60229c6b16b5SShri Abhyankar {
60239c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
60249c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
60257453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
60267453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
60279c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
60287453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
60297453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
60307453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
60319c6b16b5SShri Abhyankar 
60329c6b16b5SShri Abhyankar   PetscFunctionBegin;
60339c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6034a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
6035a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
6036a4868fbcSLisandro Dalcin   PetscValidType(U,2);
6037a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
6038a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
6039a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
60408a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
60418a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
6042a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
60439c6b16b5SShri Abhyankar 
60449c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
60459c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
60469c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
60479c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
60489c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
60497453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
60507453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
60517453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
60529c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
60539c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
60549c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60559c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60569c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
60577453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60587453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
60597453f775SEmil Constantinescu       if(tola>0.){
60607453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
60617453f775SEmil Constantinescu         na_loc++;
60627453f775SEmil Constantinescu       }
60637453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60647453f775SEmil Constantinescu       if(tolr>0.){
60657453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
60667453f775SEmil Constantinescu         nr_loc++;
60677453f775SEmil Constantinescu       }
60687453f775SEmil Constantinescu       tol=tola+tolr;
60697453f775SEmil Constantinescu       if(tol>0.){
60707453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
60717453f775SEmil Constantinescu         n_loc++;
60727453f775SEmil Constantinescu       }
60739c6b16b5SShri Abhyankar     }
60749c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60759c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60769c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
60779c6b16b5SShri Abhyankar     const PetscScalar *atol;
60789c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60799c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
60807453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60817453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
60827453f775SEmil Constantinescu       if(tola>0.){
60837453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
60847453f775SEmil Constantinescu         na_loc++;
60857453f775SEmil Constantinescu       }
60867453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60877453f775SEmil Constantinescu       if(tolr>0.){
60887453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
60897453f775SEmil Constantinescu         nr_loc++;
60907453f775SEmil Constantinescu       }
60917453f775SEmil Constantinescu       tol=tola+tolr;
60927453f775SEmil Constantinescu       if(tol>0.){
60937453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
60947453f775SEmil Constantinescu         n_loc++;
60957453f775SEmil Constantinescu       }
60969c6b16b5SShri Abhyankar     }
60979c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60989c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
60999c6b16b5SShri Abhyankar     const PetscScalar *rtol;
61009c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61019c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
61027453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
61037453f775SEmil Constantinescu       tola = ts->atol;
61047453f775SEmil Constantinescu       if(tola>0.){
61057453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
61067453f775SEmil Constantinescu         na_loc++;
61077453f775SEmil Constantinescu       }
61087453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61097453f775SEmil Constantinescu       if(tolr>0.){
61107453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
61117453f775SEmil Constantinescu         nr_loc++;
61127453f775SEmil Constantinescu       }
61137453f775SEmil Constantinescu       tol=tola+tolr;
61147453f775SEmil Constantinescu       if(tol>0.){
61157453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
61167453f775SEmil Constantinescu         n_loc++;
61177453f775SEmil Constantinescu       }
61189c6b16b5SShri Abhyankar     }
61199c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61209c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
61219c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
61227453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
61237453f775SEmil Constantinescu      tola = ts->atol;
61247453f775SEmil Constantinescu       if(tola>0.){
61257453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
61267453f775SEmil Constantinescu         na_loc++;
61277453f775SEmil Constantinescu       }
61287453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61297453f775SEmil Constantinescu       if(tolr>0.){
61307453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
61317453f775SEmil Constantinescu         nr_loc++;
61327453f775SEmil Constantinescu       }
61337453f775SEmil Constantinescu       tol=tola+tolr;
61347453f775SEmil Constantinescu       if(tol>0.){
61357453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
61367453f775SEmil Constantinescu         n_loc++;
61377453f775SEmil Constantinescu       }
61389c6b16b5SShri Abhyankar     }
61399c6b16b5SShri Abhyankar   }
61409c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
61419c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
61429c6b16b5SShri Abhyankar 
61437453f775SEmil Constantinescu   err_loc[0] = sum;
61447453f775SEmil Constantinescu   err_loc[1] = suma;
61457453f775SEmil Constantinescu   err_loc[2] = sumr;
61467453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
61477453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
61487453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
61497453f775SEmil Constantinescu 
6150a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61517453f775SEmil Constantinescu 
61527453f775SEmil Constantinescu   gsum   = err_glb[0];
61537453f775SEmil Constantinescu   gsuma  = err_glb[1];
61547453f775SEmil Constantinescu   gsumr  = err_glb[2];
61557453f775SEmil Constantinescu   n_glb  = err_glb[3];
61567453f775SEmil Constantinescu   na_glb = err_glb[4];
61577453f775SEmil Constantinescu   nr_glb = err_glb[5];
61587453f775SEmil Constantinescu 
6159b1316ef9SEmil Constantinescu   *norm  = 0.;
6160b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
6161b1316ef9SEmil Constantinescu   *norma = 0.;
6162b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
6163b1316ef9SEmil Constantinescu   *normr = 0.;
6164b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
61659c6b16b5SShri Abhyankar 
61669c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
61677453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
61687453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
61699c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
61709c6b16b5SShri Abhyankar }
61719c6b16b5SShri Abhyankar 
61729c6b16b5SShri Abhyankar /*@
6173a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
61749c6b16b5SShri Abhyankar 
61759c6b16b5SShri Abhyankar    Collective on TS
61769c6b16b5SShri Abhyankar 
61779c6b16b5SShri Abhyankar    Input Arguments:
61789c6b16b5SShri Abhyankar +  ts - time stepping context
6179a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6180a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
61819c6b16b5SShri Abhyankar 
61829c6b16b5SShri Abhyankar    Output Arguments:
61837453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
61847453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
61857453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
61869c6b16b5SShri Abhyankar 
61879c6b16b5SShri Abhyankar    Level: developer
61889c6b16b5SShri Abhyankar 
6189deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
61909c6b16b5SShri Abhyankar @*/
61917453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61929c6b16b5SShri Abhyankar {
61939c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
61947453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
61959c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
61967453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
61977453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
61987453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
61999c6b16b5SShri Abhyankar 
62009c6b16b5SShri Abhyankar   PetscFunctionBegin;
62019c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6202a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
6203a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
6204a4868fbcSLisandro Dalcin   PetscValidType(U,2);
6205a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
6206a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
6207a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
62088a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
62098a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
6210a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
62119c6b16b5SShri Abhyankar 
62129c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
62139c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
62149c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
62159c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
62169c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
62177453f775SEmil Constantinescu 
62187453f775SEmil Constantinescu   max=0.;
62197453f775SEmil Constantinescu   maxa=0.;
62207453f775SEmil Constantinescu   maxr=0.;
62217453f775SEmil Constantinescu 
62227453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
62239c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
62249c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62259c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62267453f775SEmil Constantinescu 
62277453f775SEmil Constantinescu     for (i=0; i<n; i++) {
62287453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
62297453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
62307453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62317453f775SEmil Constantinescu       tol  = tola+tolr;
62327453f775SEmil Constantinescu       if(tola>0.){
62337453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
62347453f775SEmil Constantinescu       }
62357453f775SEmil Constantinescu       if(tolr>0.){
62367453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
62377453f775SEmil Constantinescu       }
62387453f775SEmil Constantinescu       if(tol>0.){
62397453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
62407453f775SEmil Constantinescu       }
62419c6b16b5SShri Abhyankar     }
62429c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62439c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62449c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
62459c6b16b5SShri Abhyankar     const PetscScalar *atol;
62469c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62477453f775SEmil Constantinescu     for (i=0; i<n; i++) {
62487453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
62497453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
62507453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62517453f775SEmil Constantinescu       tol  = tola+tolr;
62527453f775SEmil Constantinescu       if(tola>0.){
62537453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
62547453f775SEmil Constantinescu       }
62557453f775SEmil Constantinescu       if(tolr>0.){
62567453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
62577453f775SEmil Constantinescu       }
62587453f775SEmil Constantinescu       if(tol>0.){
62597453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
62607453f775SEmil Constantinescu       }
62619c6b16b5SShri Abhyankar     }
62629c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62639c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
62649c6b16b5SShri Abhyankar     const PetscScalar *rtol;
62659c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62667453f775SEmil Constantinescu 
62677453f775SEmil Constantinescu     for (i=0; i<n; i++) {
62687453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
62697453f775SEmil Constantinescu       tola = ts->atol;
62707453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62717453f775SEmil Constantinescu       tol  = tola+tolr;
62727453f775SEmil Constantinescu       if(tola>0.){
62737453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
62747453f775SEmil Constantinescu       }
62757453f775SEmil Constantinescu       if(tolr>0.){
62767453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
62777453f775SEmil Constantinescu       }
62787453f775SEmil Constantinescu       if(tol>0.){
62797453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
62807453f775SEmil Constantinescu       }
62819c6b16b5SShri Abhyankar     }
62829c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62839c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
62847453f775SEmil Constantinescu 
62857453f775SEmil Constantinescu     for (i=0; i<n; i++) {
62867453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
62877453f775SEmil Constantinescu       tola = ts->atol;
62887453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62897453f775SEmil Constantinescu       tol  = tola+tolr;
62907453f775SEmil Constantinescu       if(tola>0.){
62917453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
62927453f775SEmil Constantinescu       }
62937453f775SEmil Constantinescu       if(tolr>0.){
62947453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
62957453f775SEmil Constantinescu       }
62967453f775SEmil Constantinescu       if(tol>0.){
62977453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
62987453f775SEmil Constantinescu       }
62999c6b16b5SShri Abhyankar     }
63009c6b16b5SShri Abhyankar   }
63019c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
63029c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
63037453f775SEmil Constantinescu   err_loc[0] = max;
63047453f775SEmil Constantinescu   err_loc[1] = maxa;
63057453f775SEmil Constantinescu   err_loc[2] = maxr;
6306a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
63077453f775SEmil Constantinescu   gmax   = err_glb[0];
63087453f775SEmil Constantinescu   gmaxa  = err_glb[1];
63097453f775SEmil Constantinescu   gmaxr  = err_glb[2];
63109c6b16b5SShri Abhyankar 
63119c6b16b5SShri Abhyankar   *norm = gmax;
63127453f775SEmil Constantinescu   *norma = gmaxa;
63137453f775SEmil Constantinescu   *normr = gmaxr;
63149c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
63157453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
63167453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
63179c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
63189c6b16b5SShri Abhyankar }
63199c6b16b5SShri Abhyankar 
63201c3436cfSJed Brown /*@
63218a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
63221c3436cfSJed Brown 
63231c3436cfSJed Brown    Collective on TS
63241c3436cfSJed Brown 
63251c3436cfSJed Brown    Input Arguments:
63261c3436cfSJed Brown +  ts - time stepping context
6327a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6328a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6329a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
63307619abb3SShri 
63311c3436cfSJed Brown    Output Arguments:
63328a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
63338a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
63348a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6335a4868fbcSLisandro Dalcin 
6336a4868fbcSLisandro Dalcin    Options Database Keys:
6337a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6338a4868fbcSLisandro Dalcin 
63391c3436cfSJed Brown    Level: developer
63401c3436cfSJed Brown 
63418a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
63421c3436cfSJed Brown @*/
63437453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
63441c3436cfSJed Brown {
63458beabaa1SBarry Smith   PetscErrorCode ierr;
63461c3436cfSJed Brown 
63471c3436cfSJed Brown   PetscFunctionBegin;
6348a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
63497453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6350a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
63517453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6352a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
63531c3436cfSJed Brown   PetscFunctionReturn(0);
63541c3436cfSJed Brown }
63551c3436cfSJed Brown 
63568a175baeSEmil Constantinescu 
63578a175baeSEmil Constantinescu /*@
63588a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
63598a175baeSEmil Constantinescu 
63608a175baeSEmil Constantinescu    Collective on TS
63618a175baeSEmil Constantinescu 
63628a175baeSEmil Constantinescu    Input Arguments:
63638a175baeSEmil Constantinescu +  ts - time stepping context
63648a175baeSEmil Constantinescu .  E - error vector
63658a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
63668a175baeSEmil Constantinescu -  Y - state vector, previous time step
63678a175baeSEmil Constantinescu 
63688a175baeSEmil Constantinescu    Output Arguments:
63698a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
63708a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
63718a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
63728a175baeSEmil Constantinescu 
63738a175baeSEmil Constantinescu    Level: developer
63748a175baeSEmil Constantinescu 
63758a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
63768a175baeSEmil Constantinescu @*/
63778a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
63788a175baeSEmil Constantinescu {
63798a175baeSEmil Constantinescu   PetscErrorCode    ierr;
63808a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
63818a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
63828a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
63838a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
63848a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
63858a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
63868a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
63878a175baeSEmil Constantinescu 
63888a175baeSEmil Constantinescu   PetscFunctionBegin;
63898a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63908a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
63918a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
63928a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
63938a175baeSEmil Constantinescu   PetscValidType(E,2);
63948a175baeSEmil Constantinescu   PetscValidType(U,3);
63958a175baeSEmil Constantinescu   PetscValidType(Y,4);
63968a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
63978a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
63988a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
63998a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
64008a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
64018a175baeSEmil Constantinescu 
64028a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
64038a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
64048a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
64058a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
64068a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
64078a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
64088a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
64098a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
64108a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
64118a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
64128a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
64138a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64148a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64158a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64168a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64178a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
64188a175baeSEmil Constantinescu       if(tola>0.){
64198a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
64208a175baeSEmil Constantinescu         na_loc++;
64218a175baeSEmil Constantinescu       }
64228a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64238a175baeSEmil Constantinescu       if(tolr>0.){
64248a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
64258a175baeSEmil Constantinescu         nr_loc++;
64268a175baeSEmil Constantinescu       }
64278a175baeSEmil Constantinescu       tol=tola+tolr;
64288a175baeSEmil Constantinescu       if(tol>0.){
64298a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
64308a175baeSEmil Constantinescu         n_loc++;
64318a175baeSEmil Constantinescu       }
64328a175baeSEmil Constantinescu     }
64338a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64348a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64358a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
64368a175baeSEmil Constantinescu     const PetscScalar *atol;
64378a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64388a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64398a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64408a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
64418a175baeSEmil Constantinescu       if(tola>0.){
64428a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
64438a175baeSEmil Constantinescu         na_loc++;
64448a175baeSEmil Constantinescu       }
64458a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64468a175baeSEmil Constantinescu       if(tolr>0.){
64478a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
64488a175baeSEmil Constantinescu         nr_loc++;
64498a175baeSEmil Constantinescu       }
64508a175baeSEmil Constantinescu       tol=tola+tolr;
64518a175baeSEmil Constantinescu       if(tol>0.){
64528a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
64538a175baeSEmil Constantinescu         n_loc++;
64548a175baeSEmil Constantinescu       }
64558a175baeSEmil Constantinescu     }
64568a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64578a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
64588a175baeSEmil Constantinescu     const PetscScalar *rtol;
64598a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64608a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64618a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64628a175baeSEmil Constantinescu       tola = ts->atol;
64638a175baeSEmil Constantinescu       if(tola>0.){
64648a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
64658a175baeSEmil Constantinescu         na_loc++;
64668a175baeSEmil Constantinescu       }
64678a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64688a175baeSEmil Constantinescu       if(tolr>0.){
64698a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
64708a175baeSEmil Constantinescu         nr_loc++;
64718a175baeSEmil Constantinescu       }
64728a175baeSEmil Constantinescu       tol=tola+tolr;
64738a175baeSEmil Constantinescu       if(tol>0.){
64748a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
64758a175baeSEmil Constantinescu         n_loc++;
64768a175baeSEmil Constantinescu       }
64778a175baeSEmil Constantinescu     }
64788a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64798a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
64808a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64818a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64828a175baeSEmil Constantinescu      tola = ts->atol;
64838a175baeSEmil Constantinescu       if(tola>0.){
64848a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
64858a175baeSEmil Constantinescu         na_loc++;
64868a175baeSEmil Constantinescu       }
64878a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64888a175baeSEmil Constantinescu       if(tolr>0.){
64898a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
64908a175baeSEmil Constantinescu         nr_loc++;
64918a175baeSEmil Constantinescu       }
64928a175baeSEmil Constantinescu       tol=tola+tolr;
64938a175baeSEmil Constantinescu       if(tol>0.){
64948a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
64958a175baeSEmil Constantinescu         n_loc++;
64968a175baeSEmil Constantinescu       }
64978a175baeSEmil Constantinescu     }
64988a175baeSEmil Constantinescu   }
64998a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
65008a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
65018a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
65028a175baeSEmil Constantinescu 
65038a175baeSEmil Constantinescu   err_loc[0] = sum;
65048a175baeSEmil Constantinescu   err_loc[1] = suma;
65058a175baeSEmil Constantinescu   err_loc[2] = sumr;
65068a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
65078a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
65088a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
65098a175baeSEmil Constantinescu 
6510a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
65118a175baeSEmil Constantinescu 
65128a175baeSEmil Constantinescu   gsum   = err_glb[0];
65138a175baeSEmil Constantinescu   gsuma  = err_glb[1];
65148a175baeSEmil Constantinescu   gsumr  = err_glb[2];
65158a175baeSEmil Constantinescu   n_glb  = err_glb[3];
65168a175baeSEmil Constantinescu   na_glb = err_glb[4];
65178a175baeSEmil Constantinescu   nr_glb = err_glb[5];
65188a175baeSEmil Constantinescu 
65198a175baeSEmil Constantinescu   *norm  = 0.;
65208a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
65218a175baeSEmil Constantinescu   *norma = 0.;
65228a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
65238a175baeSEmil Constantinescu   *normr = 0.;
65248a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
65258a175baeSEmil Constantinescu 
65268a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
65278a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
65288a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
65298a175baeSEmil Constantinescu   PetscFunctionReturn(0);
65308a175baeSEmil Constantinescu }
65318a175baeSEmil Constantinescu 
65328a175baeSEmil Constantinescu /*@
65338a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
65348a175baeSEmil Constantinescu    Collective on TS
65358a175baeSEmil Constantinescu 
65368a175baeSEmil Constantinescu    Input Arguments:
65378a175baeSEmil Constantinescu +  ts - time stepping context
65388a175baeSEmil Constantinescu .  E - error vector
65398a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
65408a175baeSEmil Constantinescu -  Y - state vector, previous time step
65418a175baeSEmil Constantinescu 
65428a175baeSEmil Constantinescu    Output Arguments:
65438a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
65448a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
65458a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
65468a175baeSEmil Constantinescu 
65478a175baeSEmil Constantinescu    Level: developer
65488a175baeSEmil Constantinescu 
65498a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
65508a175baeSEmil Constantinescu @*/
65518a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
65528a175baeSEmil Constantinescu {
65538a175baeSEmil Constantinescu   PetscErrorCode    ierr;
65548a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
65558a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
65568a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
65578a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
65588a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
65598a175baeSEmil Constantinescu 
65608a175baeSEmil Constantinescu   PetscFunctionBegin;
65618a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
65628a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
65638a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
65648a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
65658a175baeSEmil Constantinescu   PetscValidType(E,2);
65668a175baeSEmil Constantinescu   PetscValidType(U,3);
65678a175baeSEmil Constantinescu   PetscValidType(Y,4);
65688a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
65698a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
65708a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
65718a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
65728a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
65738a175baeSEmil Constantinescu 
65748a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
65758a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
65768a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
65778a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
65788a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
65798a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
65808a175baeSEmil Constantinescu 
65818a175baeSEmil Constantinescu   max=0.;
65828a175baeSEmil Constantinescu   maxa=0.;
65838a175baeSEmil Constantinescu   maxr=0.;
65848a175baeSEmil Constantinescu 
65858a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
65868a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
65878a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
65888a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
65898a175baeSEmil Constantinescu 
65908a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
65918a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
65928a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
65938a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
65948a175baeSEmil Constantinescu       tol  = tola+tolr;
65958a175baeSEmil Constantinescu       if(tola>0.){
65968a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
65978a175baeSEmil Constantinescu       }
65988a175baeSEmil Constantinescu       if(tolr>0.){
65998a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
66008a175baeSEmil Constantinescu       }
66018a175baeSEmil Constantinescu       if(tol>0.){
66028a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
66038a175baeSEmil Constantinescu       }
66048a175baeSEmil Constantinescu     }
66058a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
66068a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
66078a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
66088a175baeSEmil Constantinescu     const PetscScalar *atol;
66098a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
66108a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
66118a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
66128a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
66138a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
66148a175baeSEmil Constantinescu       tol  = tola+tolr;
66158a175baeSEmil Constantinescu       if(tola>0.){
66168a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
66178a175baeSEmil Constantinescu       }
66188a175baeSEmil Constantinescu       if(tolr>0.){
66198a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
66208a175baeSEmil Constantinescu       }
66218a175baeSEmil Constantinescu       if(tol>0.){
66228a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
66238a175baeSEmil Constantinescu       }
66248a175baeSEmil Constantinescu     }
66258a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
66268a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
66278a175baeSEmil Constantinescu     const PetscScalar *rtol;
66288a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
66298a175baeSEmil Constantinescu 
66308a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
66318a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
66328a175baeSEmil Constantinescu       tola = ts->atol;
66338a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
66348a175baeSEmil Constantinescu       tol  = tola+tolr;
66358a175baeSEmil Constantinescu       if(tola>0.){
66368a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
66378a175baeSEmil Constantinescu       }
66388a175baeSEmil Constantinescu       if(tolr>0.){
66398a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
66408a175baeSEmil Constantinescu       }
66418a175baeSEmil Constantinescu       if(tol>0.){
66428a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
66438a175baeSEmil Constantinescu       }
66448a175baeSEmil Constantinescu     }
66458a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
66468a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
66478a175baeSEmil Constantinescu 
66488a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
66498a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
66508a175baeSEmil Constantinescu       tola = ts->atol;
66518a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
66528a175baeSEmil Constantinescu       tol  = tola+tolr;
66538a175baeSEmil Constantinescu       if(tola>0.){
66548a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
66558a175baeSEmil Constantinescu       }
66568a175baeSEmil Constantinescu       if(tolr>0.){
66578a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
66588a175baeSEmil Constantinescu       }
66598a175baeSEmil Constantinescu       if(tol>0.){
66608a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
66618a175baeSEmil Constantinescu       }
66628a175baeSEmil Constantinescu     }
66638a175baeSEmil Constantinescu   }
66648a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
66658a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
66668a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
66678a175baeSEmil Constantinescu   err_loc[0] = max;
66688a175baeSEmil Constantinescu   err_loc[1] = maxa;
66698a175baeSEmil Constantinescu   err_loc[2] = maxr;
6670a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
66718a175baeSEmil Constantinescu   gmax   = err_glb[0];
66728a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
66738a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
66748a175baeSEmil Constantinescu 
66758a175baeSEmil Constantinescu   *norm = gmax;
66768a175baeSEmil Constantinescu   *norma = gmaxa;
66778a175baeSEmil Constantinescu   *normr = gmaxr;
66788a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
66798a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
66808a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
66818a175baeSEmil Constantinescu   PetscFunctionReturn(0);
66828a175baeSEmil Constantinescu }
66838a175baeSEmil Constantinescu 
66848a175baeSEmil Constantinescu /*@
66858a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
66868a175baeSEmil Constantinescu 
66878a175baeSEmil Constantinescu    Collective on TS
66888a175baeSEmil Constantinescu 
66898a175baeSEmil Constantinescu    Input Arguments:
66908a175baeSEmil Constantinescu +  ts - time stepping context
66918a175baeSEmil Constantinescu .  E - error vector
66928a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
66938a175baeSEmil Constantinescu .  Y - state vector, previous time step
66948a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
66958a175baeSEmil Constantinescu 
66968a175baeSEmil Constantinescu    Output Arguments:
66978a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
66988a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
66998a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
67008a175baeSEmil Constantinescu 
67018a175baeSEmil Constantinescu    Options Database Keys:
67028a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
67038a175baeSEmil Constantinescu 
67048a175baeSEmil Constantinescu    Level: developer
67058a175baeSEmil Constantinescu 
67068a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
67078a175baeSEmil Constantinescu @*/
67088a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
67098a175baeSEmil Constantinescu {
67108a175baeSEmil Constantinescu   PetscErrorCode ierr;
67118a175baeSEmil Constantinescu 
67128a175baeSEmil Constantinescu   PetscFunctionBegin;
67138a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
67148a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
67158a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
67168a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
67178a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
67188a175baeSEmil Constantinescu   PetscFunctionReturn(0);
67198a175baeSEmil Constantinescu }
67208a175baeSEmil Constantinescu 
67218a175baeSEmil Constantinescu 
67228d59e960SJed Brown /*@
67238d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
67248d59e960SJed Brown 
67258d59e960SJed Brown    Logically Collective on TS
67268d59e960SJed Brown 
67278d59e960SJed Brown    Input Arguments:
67288d59e960SJed Brown +  ts - time stepping context
67298d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
67308d59e960SJed Brown 
67318d59e960SJed Brown    Note:
67328d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
67338d59e960SJed Brown 
67348d59e960SJed Brown    Level: intermediate
67358d59e960SJed Brown 
67368d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
67378d59e960SJed Brown @*/
67388d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
67398d59e960SJed Brown {
67408d59e960SJed Brown   PetscFunctionBegin;
67418d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
67428d59e960SJed Brown   ts->cfltime_local = cfltime;
67438d59e960SJed Brown   ts->cfltime       = -1.;
67448d59e960SJed Brown   PetscFunctionReturn(0);
67458d59e960SJed Brown }
67468d59e960SJed Brown 
67478d59e960SJed Brown /*@
67488d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
67498d59e960SJed Brown 
67508d59e960SJed Brown    Collective on TS
67518d59e960SJed Brown 
67528d59e960SJed Brown    Input Arguments:
67538d59e960SJed Brown .  ts - time stepping context
67548d59e960SJed Brown 
67558d59e960SJed Brown    Output Arguments:
67568d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
67578d59e960SJed Brown 
67588d59e960SJed Brown    Level: advanced
67598d59e960SJed Brown 
67608d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
67618d59e960SJed Brown @*/
67628d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
67638d59e960SJed Brown {
67648d59e960SJed Brown   PetscErrorCode ierr;
67658d59e960SJed Brown 
67668d59e960SJed Brown   PetscFunctionBegin;
67678d59e960SJed Brown   if (ts->cfltime < 0) {
6768b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
67698d59e960SJed Brown   }
67708d59e960SJed Brown   *cfltime = ts->cfltime;
67718d59e960SJed Brown   PetscFunctionReturn(0);
67728d59e960SJed Brown }
67738d59e960SJed Brown 
6774d6ebe24aSShri Abhyankar /*@
6775d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6776d6ebe24aSShri Abhyankar 
6777d6ebe24aSShri Abhyankar    Input Parameters:
6778d6ebe24aSShri Abhyankar .  ts   - the TS context.
6779d6ebe24aSShri Abhyankar .  xl   - lower bound.
6780d6ebe24aSShri Abhyankar .  xu   - upper bound.
6781d6ebe24aSShri Abhyankar 
6782d6ebe24aSShri Abhyankar    Notes:
6783d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6784e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6785d6ebe24aSShri Abhyankar 
67862bd2b0e6SSatish Balay    Level: advanced
67872bd2b0e6SSatish Balay 
6788d6ebe24aSShri Abhyankar @*/
6789d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6790d6ebe24aSShri Abhyankar {
6791d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6792d6ebe24aSShri Abhyankar   SNES           snes;
6793d6ebe24aSShri Abhyankar 
6794d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6795d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6796d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6797d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6798d6ebe24aSShri Abhyankar }
6799d6ebe24aSShri Abhyankar 
6800325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6801c6db04a5SJed Brown #include <mex.h>
6802325fc9f4SBarry Smith 
6803325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6804325fc9f4SBarry Smith 
6805325fc9f4SBarry Smith /*
6806325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6807325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6808325fc9f4SBarry Smith 
6809325fc9f4SBarry Smith    Collective on TS
6810325fc9f4SBarry Smith 
6811325fc9f4SBarry Smith    Input Parameters:
6812325fc9f4SBarry Smith +  snes - the TS context
68130910c330SBarry Smith -  u - input vector
6814325fc9f4SBarry Smith 
6815325fc9f4SBarry Smith    Output Parameter:
6816325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6817325fc9f4SBarry Smith 
6818325fc9f4SBarry Smith    Notes:
6819325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6820325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6821325fc9f4SBarry Smith    themselves.
6822325fc9f4SBarry Smith 
6823325fc9f4SBarry Smith    Level: developer
6824325fc9f4SBarry Smith 
6825325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6826325fc9f4SBarry Smith 
6827325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6828325fc9f4SBarry Smith */
68290910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6830325fc9f4SBarry Smith {
6831325fc9f4SBarry Smith   PetscErrorCode  ierr;
6832325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6833325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6834325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6835325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6836325fc9f4SBarry Smith 
6837325fc9f4SBarry Smith   PetscFunctionBegin;
6838325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
68390910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
68400910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6841325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
68420910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6843325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6844325fc9f4SBarry Smith 
6845325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
68460910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
68470910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
68480910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6849bbd56ea5SKarl Rupp 
6850325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6851325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6852325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6853325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6854325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6855325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6856325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6857325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6858325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6859325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6860325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6861325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6862325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6863325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6864325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6865325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6866325fc9f4SBarry Smith   PetscFunctionReturn(0);
6867325fc9f4SBarry Smith }
6868325fc9f4SBarry Smith 
6869325fc9f4SBarry Smith /*
6870325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6871325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6872e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6873325fc9f4SBarry Smith 
6874325fc9f4SBarry Smith    Logically Collective on TS
6875325fc9f4SBarry Smith 
6876325fc9f4SBarry Smith    Input Parameters:
6877325fc9f4SBarry Smith +  ts - the TS context
6878325fc9f4SBarry Smith -  func - function evaluation routine
6879325fc9f4SBarry Smith 
6880325fc9f4SBarry Smith    Calling sequence of func:
68810910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6882325fc9f4SBarry Smith 
6883325fc9f4SBarry Smith    Level: beginner
6884325fc9f4SBarry Smith 
6885325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6886325fc9f4SBarry Smith 
6887325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6888325fc9f4SBarry Smith */
6889cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6890325fc9f4SBarry Smith {
6891325fc9f4SBarry Smith   PetscErrorCode  ierr;
6892325fc9f4SBarry Smith   TSMatlabContext *sctx;
6893325fc9f4SBarry Smith 
6894325fc9f4SBarry Smith   PetscFunctionBegin;
6895325fc9f4SBarry Smith   /* currently sctx is memory bleed */
689695dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6897325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6898325fc9f4SBarry Smith   /*
6899325fc9f4SBarry Smith      This should work, but it doesn't
6900325fc9f4SBarry Smith   sctx->ctx = ctx;
6901325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6902325fc9f4SBarry Smith   */
6903325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6904bbd56ea5SKarl Rupp 
69050298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6906325fc9f4SBarry Smith   PetscFunctionReturn(0);
6907325fc9f4SBarry Smith }
6908325fc9f4SBarry Smith 
6909325fc9f4SBarry Smith /*
6910325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6911325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6912325fc9f4SBarry Smith 
6913325fc9f4SBarry Smith    Collective on TS
6914325fc9f4SBarry Smith 
6915325fc9f4SBarry Smith    Input Parameters:
6916cdcf91faSSean Farley +  ts - the TS context
69170910c330SBarry Smith .  u - input vector
6918325fc9f4SBarry Smith .  A, B - the matrices
6919325fc9f4SBarry Smith -  ctx - user context
6920325fc9f4SBarry Smith 
6921325fc9f4SBarry Smith    Level: developer
6922325fc9f4SBarry Smith 
6923325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6924325fc9f4SBarry Smith 
6925325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6926325fc9f4SBarry Smith @*/
6927f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6928325fc9f4SBarry Smith {
6929325fc9f4SBarry Smith   PetscErrorCode  ierr;
6930325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6931325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6932325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6933325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6934325fc9f4SBarry Smith 
6935325fc9f4SBarry Smith   PetscFunctionBegin;
6936cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
69370910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6938325fc9f4SBarry Smith 
69390910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6940325fc9f4SBarry Smith 
6941cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
69420910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
69430910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
69440910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
69450910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6946bbd56ea5SKarl Rupp 
6947325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6948325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6949325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6950325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6951325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6952325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6953325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6954325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6955325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6956325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6957325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6958325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6959325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6960325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6961325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6962325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6963325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6964325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6965325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6966325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6967325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6968325fc9f4SBarry Smith   PetscFunctionReturn(0);
6969325fc9f4SBarry Smith }
6970325fc9f4SBarry Smith 
6971325fc9f4SBarry Smith /*
6972325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6973e3c5b3baSBarry 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
6974325fc9f4SBarry Smith 
6975325fc9f4SBarry Smith    Logically Collective on TS
6976325fc9f4SBarry Smith 
6977325fc9f4SBarry Smith    Input Parameters:
6978cdcf91faSSean Farley +  ts - the TS context
6979325fc9f4SBarry Smith .  A,B - Jacobian matrices
6980325fc9f4SBarry Smith .  func - function evaluation routine
6981325fc9f4SBarry Smith -  ctx - user context
6982325fc9f4SBarry Smith 
6983325fc9f4SBarry Smith    Calling sequence of func:
69840910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6985325fc9f4SBarry Smith 
6986325fc9f4SBarry Smith    Level: developer
6987325fc9f4SBarry Smith 
6988325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6989325fc9f4SBarry Smith 
6990325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6991325fc9f4SBarry Smith */
6992cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6993325fc9f4SBarry Smith {
6994325fc9f4SBarry Smith   PetscErrorCode  ierr;
6995325fc9f4SBarry Smith   TSMatlabContext *sctx;
6996325fc9f4SBarry Smith 
6997325fc9f4SBarry Smith   PetscFunctionBegin;
6998325fc9f4SBarry Smith   /* currently sctx is memory bleed */
699995dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
7000325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
7001325fc9f4SBarry Smith   /*
7002325fc9f4SBarry Smith      This should work, but it doesn't
7003325fc9f4SBarry Smith   sctx->ctx = ctx;
7004325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
7005325fc9f4SBarry Smith   */
7006325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
7007bbd56ea5SKarl Rupp 
7008cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
7009325fc9f4SBarry Smith   PetscFunctionReturn(0);
7010325fc9f4SBarry Smith }
7011325fc9f4SBarry Smith 
7012b5b1a830SBarry Smith /*
7013b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
7014b5b1a830SBarry Smith 
7015b5b1a830SBarry Smith    Collective on TS
7016b5b1a830SBarry Smith 
7017b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
7018b5b1a830SBarry Smith @*/
70190910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
7020b5b1a830SBarry Smith {
7021b5b1a830SBarry Smith   PetscErrorCode  ierr;
7022b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
7023a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
7024b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
7025b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
7026b5b1a830SBarry Smith 
7027b5b1a830SBarry Smith   PetscFunctionBegin;
7028cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
70290910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
7030b5b1a830SBarry Smith 
7031cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
70320910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
7033bbd56ea5SKarl Rupp 
7034b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
7035b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
7036b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
7037b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
7038b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
7039b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
7040b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
7041b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
7042b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
7043b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
7044b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
7045b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
7046b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
7047b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
7048b5b1a830SBarry Smith   PetscFunctionReturn(0);
7049b5b1a830SBarry Smith }
7050b5b1a830SBarry Smith 
7051b5b1a830SBarry Smith /*
7052b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
7053b5b1a830SBarry Smith 
7054b5b1a830SBarry Smith    Level: developer
7055b5b1a830SBarry Smith 
7056b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
7057b5b1a830SBarry Smith 
7058b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
7059b5b1a830SBarry Smith */
7060cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
7061b5b1a830SBarry Smith {
7062b5b1a830SBarry Smith   PetscErrorCode  ierr;
7063b5b1a830SBarry Smith   TSMatlabContext *sctx;
7064b5b1a830SBarry Smith 
7065b5b1a830SBarry Smith   PetscFunctionBegin;
7066b5b1a830SBarry Smith   /* currently sctx is memory bleed */
706795dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
7068b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
7069b5b1a830SBarry Smith   /*
7070b5b1a830SBarry Smith      This should work, but it doesn't
7071b5b1a830SBarry Smith   sctx->ctx = ctx;
7072b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
7073b5b1a830SBarry Smith   */
7074b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
7075bbd56ea5SKarl Rupp 
70760298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
7077b5b1a830SBarry Smith   PetscFunctionReturn(0);
7078b5b1a830SBarry Smith }
7079325fc9f4SBarry Smith #endif
7080b3603a34SBarry Smith 
7081b3603a34SBarry Smith /*@C
70824f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
7083b3603a34SBarry Smith        in a time based line graph
7084b3603a34SBarry Smith 
7085b3603a34SBarry Smith    Collective on TS
7086b3603a34SBarry Smith 
7087b3603a34SBarry Smith    Input Parameters:
7088b3603a34SBarry Smith +  ts - the TS context
7089b3603a34SBarry Smith .  step - current time-step
7090b3603a34SBarry Smith .  ptime - current time
70917db568b7SBarry Smith .  u - current solution
70927db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
7093b3603a34SBarry Smith 
7094b3d3934dSBarry Smith    Options Database:
70959ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
7096b3d3934dSBarry Smith 
7097b3603a34SBarry Smith    Level: intermediate
7098b3603a34SBarry Smith 
70996934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component solutions in a separate window
7100b3603a34SBarry Smith 
7101b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
7102b3603a34SBarry Smith 
71037db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
71047db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
71057db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
71067db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
7107b3603a34SBarry Smith @*/
71087db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7109b3603a34SBarry Smith {
7110b3603a34SBarry Smith   PetscErrorCode    ierr;
71117db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
7112b3603a34SBarry Smith   const PetscScalar *yy;
711380666b62SBarry Smith   Vec               v;
7114b3603a34SBarry Smith 
7115b3603a34SBarry Smith   PetscFunctionBegin;
711663e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
711758ff32f7SBarry Smith   if (!step) {
7118a9f9c1f6SBarry Smith     PetscDrawAxis axis;
71196934998bSLisandro Dalcin     PetscInt      dim;
7120a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7121a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
7122bab0a581SBarry Smith     if (!ctx->names) {
7123bab0a581SBarry Smith       PetscBool flg;
7124bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
7125bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
7126bab0a581SBarry Smith       if (flg) {
7127bab0a581SBarry Smith         PetscInt i,n;
7128bab0a581SBarry Smith         char     **names;
7129bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
7130bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
7131bab0a581SBarry Smith         for (i=0; i<n; i++) {
7132bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
7133bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
7134bab0a581SBarry Smith         }
7135bab0a581SBarry Smith         names[n] = NULL;
7136bab0a581SBarry Smith         ctx->names = names;
7137bab0a581SBarry Smith       }
7138bab0a581SBarry Smith     }
7139387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
7140387f4636SBarry Smith       char      **displaynames;
7141387f4636SBarry Smith       PetscBool flg;
7142387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
714395dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
7144387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
7145c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
7146387f4636SBarry Smith       if (flg) {
7147a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
7148387f4636SBarry Smith       }
7149387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
7150387f4636SBarry Smith     }
7151387f4636SBarry Smith     if (ctx->displaynames) {
7152387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
7153387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
7154387f4636SBarry Smith     } else if (ctx->names) {
71550910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
71560b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7157387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
7158b0bc92c6SBarry Smith     } else {
7159b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
7160b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7161387f4636SBarry Smith     }
71620b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
716358ff32f7SBarry Smith   }
71646934998bSLisandro Dalcin 
71656934998bSLisandro Dalcin   if (!ctx->transform) v = u;
71666934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
716780666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
71686934998bSLisandro Dalcin   if (ctx->displaynames) {
71696934998bSLisandro Dalcin     PetscInt i;
71706934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
71716934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
71726934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
71736934998bSLisandro Dalcin   } else {
7174e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
7175e3efe391SJed Brown     PetscInt  i,n;
71766934998bSLisandro Dalcin     PetscReal *yreal;
717780666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
7178785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
7179e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
71800b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
7181e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
7182e3efe391SJed Brown #else
71830b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
7184e3efe391SJed Brown #endif
718580666b62SBarry Smith   }
71866934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
71876934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
71886934998bSLisandro Dalcin 
7189b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
71900b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
71916934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
71923923b477SBarry Smith   }
7193b3603a34SBarry Smith   PetscFunctionReturn(0);
7194b3603a34SBarry Smith }
7195b3603a34SBarry Smith 
7196b037adc7SBarry Smith /*@C
719731152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
7198b037adc7SBarry Smith 
7199b037adc7SBarry Smith    Collective on TS
7200b037adc7SBarry Smith 
7201b037adc7SBarry Smith    Input Parameters:
7202b037adc7SBarry Smith +  ts - the TS context
7203b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
7204b037adc7SBarry Smith 
7205b037adc7SBarry Smith    Level: intermediate
7206b037adc7SBarry Smith 
72077db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
72087db568b7SBarry Smith 
7209b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
7210b037adc7SBarry Smith 
7211a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
7212b037adc7SBarry Smith @*/
721331152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
7214b037adc7SBarry Smith {
7215b037adc7SBarry Smith   PetscErrorCode    ierr;
7216b037adc7SBarry Smith   PetscInt          i;
7217b037adc7SBarry Smith 
7218b037adc7SBarry Smith   PetscFunctionBegin;
7219b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7220b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
72215537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
7222b3d3934dSBarry Smith       break;
7223b3d3934dSBarry Smith     }
7224b3d3934dSBarry Smith   }
7225b3d3934dSBarry Smith   PetscFunctionReturn(0);
7226b3d3934dSBarry Smith }
7227b3d3934dSBarry Smith 
7228e673d494SBarry Smith /*@C
7229e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
7230e673d494SBarry Smith 
7231e673d494SBarry Smith    Collective on TS
7232e673d494SBarry Smith 
7233e673d494SBarry Smith    Input Parameters:
7234e673d494SBarry Smith +  ts - the TS context
7235e673d494SBarry Smith -  names - the names of the components, final string must be NULL
7236e673d494SBarry Smith 
7237e673d494SBarry Smith    Level: intermediate
7238e673d494SBarry Smith 
7239e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7240e673d494SBarry Smith 
7241a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
7242e673d494SBarry Smith @*/
7243e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
7244e673d494SBarry Smith {
7245e673d494SBarry Smith   PetscErrorCode    ierr;
7246e673d494SBarry Smith 
7247e673d494SBarry Smith   PetscFunctionBegin;
7248e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
7249e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
7250e673d494SBarry Smith   PetscFunctionReturn(0);
7251e673d494SBarry Smith }
7252e673d494SBarry Smith 
7253b3d3934dSBarry Smith /*@C
7254b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
7255b3d3934dSBarry Smith 
7256b3d3934dSBarry Smith    Collective on TS
7257b3d3934dSBarry Smith 
7258b3d3934dSBarry Smith    Input Parameter:
7259b3d3934dSBarry Smith .  ts - the TS context
7260b3d3934dSBarry Smith 
7261b3d3934dSBarry Smith    Output Parameter:
7262b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
7263b3d3934dSBarry Smith 
7264b3d3934dSBarry Smith    Level: intermediate
7265b3d3934dSBarry Smith 
72667db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
72677db568b7SBarry Smith 
7268b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7269b3d3934dSBarry Smith 
7270b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7271b3d3934dSBarry Smith @*/
7272b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
7273b3d3934dSBarry Smith {
7274b3d3934dSBarry Smith   PetscInt       i;
7275b3d3934dSBarry Smith 
7276b3d3934dSBarry Smith   PetscFunctionBegin;
7277b3d3934dSBarry Smith   *names = NULL;
7278b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7279b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
72805537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
7281b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
7282b3d3934dSBarry Smith       break;
7283387f4636SBarry Smith     }
7284387f4636SBarry Smith   }
7285387f4636SBarry Smith   PetscFunctionReturn(0);
7286387f4636SBarry Smith }
7287387f4636SBarry Smith 
7288a66092f1SBarry Smith /*@C
7289a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
7290a66092f1SBarry Smith 
7291a66092f1SBarry Smith    Collective on TS
7292a66092f1SBarry Smith 
7293a66092f1SBarry Smith    Input Parameters:
7294a66092f1SBarry Smith +  ctx - the TSMonitorLG context
7295a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
7296a66092f1SBarry Smith 
7297a66092f1SBarry Smith    Level: intermediate
7298a66092f1SBarry Smith 
7299a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
7300a66092f1SBarry Smith 
7301a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7302a66092f1SBarry Smith @*/
7303a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
7304a66092f1SBarry Smith {
7305a66092f1SBarry Smith   PetscInt          j = 0,k;
7306a66092f1SBarry Smith   PetscErrorCode    ierr;
7307a66092f1SBarry Smith 
7308a66092f1SBarry Smith   PetscFunctionBegin;
7309a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
7310a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
7311a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
7312a66092f1SBarry Smith   while (displaynames[j]) j++;
7313a66092f1SBarry Smith   ctx->ndisplayvariables = j;
7314a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
7315a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
7316a66092f1SBarry Smith   j = 0;
7317a66092f1SBarry Smith   while (displaynames[j]) {
7318a66092f1SBarry Smith     k = 0;
7319a66092f1SBarry Smith     while (ctx->names[k]) {
7320a66092f1SBarry Smith       PetscBool flg;
7321a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
7322a66092f1SBarry Smith       if (flg) {
7323a66092f1SBarry Smith         ctx->displayvariables[j] = k;
7324a66092f1SBarry Smith         break;
7325a66092f1SBarry Smith       }
7326a66092f1SBarry Smith       k++;
7327a66092f1SBarry Smith     }
7328a66092f1SBarry Smith     j++;
7329a66092f1SBarry Smith   }
7330a66092f1SBarry Smith   PetscFunctionReturn(0);
7331a66092f1SBarry Smith }
7332a66092f1SBarry Smith 
7333387f4636SBarry Smith /*@C
7334387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
7335387f4636SBarry Smith 
7336387f4636SBarry Smith    Collective on TS
7337387f4636SBarry Smith 
7338387f4636SBarry Smith    Input Parameters:
7339387f4636SBarry Smith +  ts - the TS context
7340387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
7341387f4636SBarry Smith 
73427db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
73437db568b7SBarry Smith 
7344387f4636SBarry Smith    Level: intermediate
7345387f4636SBarry Smith 
7346387f4636SBarry Smith .keywords: TS,  vector, monitor, view
7347387f4636SBarry Smith 
7348387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7349387f4636SBarry Smith @*/
7350387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
7351387f4636SBarry Smith {
7352a66092f1SBarry Smith   PetscInt          i;
7353387f4636SBarry Smith   PetscErrorCode    ierr;
7354387f4636SBarry Smith 
7355387f4636SBarry Smith   PetscFunctionBegin;
7356387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7357387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
73585537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
7359b3d3934dSBarry Smith       break;
7360b037adc7SBarry Smith     }
7361b037adc7SBarry Smith   }
7362b037adc7SBarry Smith   PetscFunctionReturn(0);
7363b037adc7SBarry Smith }
7364b037adc7SBarry Smith 
736580666b62SBarry Smith /*@C
736680666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
736780666b62SBarry Smith 
736880666b62SBarry Smith    Collective on TS
736980666b62SBarry Smith 
737080666b62SBarry Smith    Input Parameters:
737180666b62SBarry Smith +  ts - the TS context
737280666b62SBarry Smith .  transform - the transform function
73737684fa3eSBarry Smith .  destroy - function to destroy the optional context
737480666b62SBarry Smith -  ctx - optional context used by transform function
737580666b62SBarry Smith 
73767db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
73777db568b7SBarry Smith 
737880666b62SBarry Smith    Level: intermediate
737980666b62SBarry Smith 
738080666b62SBarry Smith .keywords: TS,  vector, monitor, view
738180666b62SBarry Smith 
7382a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
738380666b62SBarry Smith @*/
73847684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
738580666b62SBarry Smith {
738680666b62SBarry Smith   PetscInt          i;
7387a66092f1SBarry Smith   PetscErrorCode    ierr;
738880666b62SBarry Smith 
738980666b62SBarry Smith   PetscFunctionBegin;
739080666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
739180666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
73925537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
739380666b62SBarry Smith     }
739480666b62SBarry Smith   }
739580666b62SBarry Smith   PetscFunctionReturn(0);
739680666b62SBarry Smith }
739780666b62SBarry Smith 
7398e673d494SBarry Smith /*@C
7399e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
7400e673d494SBarry Smith 
7401e673d494SBarry Smith    Collective on TSLGCtx
7402e673d494SBarry Smith 
7403e673d494SBarry Smith    Input Parameters:
7404e673d494SBarry Smith +  ts - the TS context
7405e673d494SBarry Smith .  transform - the transform function
74067684fa3eSBarry Smith .  destroy - function to destroy the optional context
7407e673d494SBarry Smith -  ctx - optional context used by transform function
7408e673d494SBarry Smith 
7409e673d494SBarry Smith    Level: intermediate
7410e673d494SBarry Smith 
7411e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7412e673d494SBarry Smith 
7413a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
7414e673d494SBarry Smith @*/
74157684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
7416e673d494SBarry Smith {
7417e673d494SBarry Smith   PetscFunctionBegin;
7418e673d494SBarry Smith   ctx->transform    = transform;
74197684fa3eSBarry Smith   ctx->transformdestroy = destroy;
7420e673d494SBarry Smith   ctx->transformctx = tctx;
7421e673d494SBarry Smith   PetscFunctionReturn(0);
7422e673d494SBarry Smith }
7423e673d494SBarry Smith 
7424ef20d060SBarry Smith /*@C
74254f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
7426ef20d060SBarry Smith        in a time based line graph
7427ef20d060SBarry Smith 
7428ef20d060SBarry Smith    Collective on TS
7429ef20d060SBarry Smith 
7430ef20d060SBarry Smith    Input Parameters:
7431ef20d060SBarry Smith +  ts - the TS context
7432ef20d060SBarry Smith .  step - current time-step
7433ef20d060SBarry Smith .  ptime - current time
74347db568b7SBarry Smith .  u - current solution
74357db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
7436ef20d060SBarry Smith 
7437ef20d060SBarry Smith    Level: intermediate
7438ef20d060SBarry Smith 
74396934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component errors in a separate window
7440abd5a294SJed Brown 
7441abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
7442abd5a294SJed Brown 
7443abd5a294SJed Brown    Options Database Keys:
74444f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
7445ef20d060SBarry Smith 
7446ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
7447ef20d060SBarry Smith 
7448abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
7449ef20d060SBarry Smith @*/
74500910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
7451ef20d060SBarry Smith {
7452ef20d060SBarry Smith   PetscErrorCode    ierr;
74530b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
7454ef20d060SBarry Smith   const PetscScalar *yy;
7455ef20d060SBarry Smith   Vec               y;
7456ef20d060SBarry Smith 
7457ef20d060SBarry Smith   PetscFunctionBegin;
7458a9f9c1f6SBarry Smith   if (!step) {
7459a9f9c1f6SBarry Smith     PetscDrawAxis axis;
74606934998bSLisandro Dalcin     PetscInt      dim;
7461a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
74621ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
74630910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
7464a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7465a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7466a9f9c1f6SBarry Smith   }
74670910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
7468ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
74690910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7470ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
7471e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
7472e3efe391SJed Brown   {
7473e3efe391SJed Brown     PetscReal *yreal;
7474e3efe391SJed Brown     PetscInt  i,n;
7475e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
7476785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
7477e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
74780b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
7479e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
7480e3efe391SJed Brown   }
7481e3efe391SJed Brown #else
74820b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
7483e3efe391SJed Brown #endif
7484ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
7485ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
7486b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
74870b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
74886934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
74893923b477SBarry Smith   }
7490ef20d060SBarry Smith   PetscFunctionReturn(0);
7491ef20d060SBarry Smith }
7492ef20d060SBarry Smith 
7493201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7494201da799SBarry Smith {
7495201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7496201da799SBarry Smith   PetscReal      x   = ptime,y;
7497201da799SBarry Smith   PetscErrorCode ierr;
7498201da799SBarry Smith   PetscInt       its;
7499201da799SBarry Smith 
7500201da799SBarry Smith   PetscFunctionBegin;
750163e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7502201da799SBarry Smith   if (!n) {
7503201da799SBarry Smith     PetscDrawAxis axis;
7504201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7505201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7506201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7507201da799SBarry Smith     ctx->snes_its = 0;
7508201da799SBarry Smith   }
7509201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7510201da799SBarry Smith   y    = its - ctx->snes_its;
7511201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
75123fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7513201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
75146934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7515201da799SBarry Smith   }
7516201da799SBarry Smith   ctx->snes_its = its;
7517201da799SBarry Smith   PetscFunctionReturn(0);
7518201da799SBarry Smith }
7519201da799SBarry Smith 
7520201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7521201da799SBarry Smith {
7522201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7523201da799SBarry Smith   PetscReal      x   = ptime,y;
7524201da799SBarry Smith   PetscErrorCode ierr;
7525201da799SBarry Smith   PetscInt       its;
7526201da799SBarry Smith 
7527201da799SBarry Smith   PetscFunctionBegin;
752863e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7529201da799SBarry Smith   if (!n) {
7530201da799SBarry Smith     PetscDrawAxis axis;
7531201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7532201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7533201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7534201da799SBarry Smith     ctx->ksp_its = 0;
7535201da799SBarry Smith   }
7536201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7537201da799SBarry Smith   y    = its - ctx->ksp_its;
7538201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
753999fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7540201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
75416934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7542201da799SBarry Smith   }
7543201da799SBarry Smith   ctx->ksp_its = its;
7544201da799SBarry Smith   PetscFunctionReturn(0);
7545201da799SBarry Smith }
7546f9c1d6abSBarry Smith 
7547f9c1d6abSBarry Smith /*@
7548f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7549f9c1d6abSBarry Smith 
7550f9c1d6abSBarry Smith    Collective on TS and Vec
7551f9c1d6abSBarry Smith 
7552f9c1d6abSBarry Smith    Input Parameters:
7553f9c1d6abSBarry Smith +  ts - the TS context
7554f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7555f9c1d6abSBarry Smith 
7556f9c1d6abSBarry Smith    Output Parameters:
7557f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7558f9c1d6abSBarry Smith 
7559f9c1d6abSBarry Smith    Level: developer
7560f9c1d6abSBarry Smith 
7561f9c1d6abSBarry Smith .keywords: TS, compute
7562f9c1d6abSBarry Smith 
7563f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7564f9c1d6abSBarry Smith @*/
7565f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7566f9c1d6abSBarry Smith {
7567f9c1d6abSBarry Smith   PetscErrorCode ierr;
7568f9c1d6abSBarry Smith 
7569f9c1d6abSBarry Smith   PetscFunctionBegin;
7570f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7571ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7572f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7573f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7574f9c1d6abSBarry Smith }
757524655328SShri 
7576b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7577b3d3934dSBarry Smith /*@C
7578b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7579b3d3934dSBarry Smith 
7580b3d3934dSBarry Smith    Collective on TS
7581b3d3934dSBarry Smith 
7582b3d3934dSBarry Smith    Input Parameters:
7583b3d3934dSBarry Smith .  ts  - the ODE solver object
7584b3d3934dSBarry Smith 
7585b3d3934dSBarry Smith    Output Parameter:
7586b3d3934dSBarry Smith .  ctx - the context
7587b3d3934dSBarry Smith 
7588b3d3934dSBarry Smith    Level: intermediate
7589b3d3934dSBarry Smith 
7590b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
7591b3d3934dSBarry Smith 
7592b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7593b3d3934dSBarry Smith 
7594b3d3934dSBarry Smith @*/
7595b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7596b3d3934dSBarry Smith {
7597b3d3934dSBarry Smith   PetscErrorCode ierr;
7598b3d3934dSBarry Smith 
7599b3d3934dSBarry Smith   PetscFunctionBegin;
7600a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7601b3d3934dSBarry Smith   PetscFunctionReturn(0);
7602b3d3934dSBarry Smith }
7603b3d3934dSBarry Smith 
7604b3d3934dSBarry Smith /*@C
7605b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7606b3d3934dSBarry Smith 
7607b3d3934dSBarry Smith    Collective on TS
7608b3d3934dSBarry Smith 
7609b3d3934dSBarry Smith    Input Parameters:
7610b3d3934dSBarry Smith +  ts - the TS context
7611b3d3934dSBarry Smith .  step - current time-step
7612b3d3934dSBarry Smith .  ptime - current time
76137db568b7SBarry Smith .  u  - current solution
76147db568b7SBarry Smith -  dctx - the envelope context
7615b3d3934dSBarry Smith 
7616b3d3934dSBarry Smith    Options Database:
7617b3d3934dSBarry Smith .  -ts_monitor_envelope
7618b3d3934dSBarry Smith 
7619b3d3934dSBarry Smith    Level: intermediate
7620b3d3934dSBarry Smith 
7621b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7622b3d3934dSBarry Smith 
7623b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7624b3d3934dSBarry Smith 
76257db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7626b3d3934dSBarry Smith @*/
76277db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7628b3d3934dSBarry Smith {
7629b3d3934dSBarry Smith   PetscErrorCode       ierr;
76307db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7631b3d3934dSBarry Smith 
7632b3d3934dSBarry Smith   PetscFunctionBegin;
7633b3d3934dSBarry Smith   if (!ctx->max) {
7634b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7635b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7636b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7637b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7638b3d3934dSBarry Smith   } else {
7639b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7640b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7641b3d3934dSBarry Smith   }
7642b3d3934dSBarry Smith   PetscFunctionReturn(0);
7643b3d3934dSBarry Smith }
7644b3d3934dSBarry Smith 
7645b3d3934dSBarry Smith /*@C
7646b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7647b3d3934dSBarry Smith 
7648b3d3934dSBarry Smith    Collective on TS
7649b3d3934dSBarry Smith 
7650b3d3934dSBarry Smith    Input Parameter:
7651b3d3934dSBarry Smith .  ts - the TS context
7652b3d3934dSBarry Smith 
7653b3d3934dSBarry Smith    Output Parameter:
7654b3d3934dSBarry Smith +  max - the maximum values
7655b3d3934dSBarry Smith -  min - the minimum values
7656b3d3934dSBarry Smith 
76577db568b7SBarry Smith    Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
76587db568b7SBarry Smith 
7659b3d3934dSBarry Smith    Level: intermediate
7660b3d3934dSBarry Smith 
7661b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7662b3d3934dSBarry Smith 
7663b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7664b3d3934dSBarry Smith @*/
7665b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7666b3d3934dSBarry Smith {
7667b3d3934dSBarry Smith   PetscInt i;
7668b3d3934dSBarry Smith 
7669b3d3934dSBarry Smith   PetscFunctionBegin;
7670b3d3934dSBarry Smith   if (max) *max = NULL;
7671b3d3934dSBarry Smith   if (min) *min = NULL;
7672b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7673b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
76745537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7675b3d3934dSBarry Smith       if (max) *max = ctx->max;
7676b3d3934dSBarry Smith       if (min) *min = ctx->min;
7677b3d3934dSBarry Smith       break;
7678b3d3934dSBarry Smith     }
7679b3d3934dSBarry Smith   }
7680b3d3934dSBarry Smith   PetscFunctionReturn(0);
7681b3d3934dSBarry Smith }
7682b3d3934dSBarry Smith 
7683b3d3934dSBarry Smith /*@C
7684b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7685b3d3934dSBarry Smith 
7686b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7687b3d3934dSBarry Smith 
7688b3d3934dSBarry Smith    Input Parameter:
7689b3d3934dSBarry Smith .  ctx - the monitor context
7690b3d3934dSBarry Smith 
7691b3d3934dSBarry Smith    Level: intermediate
7692b3d3934dSBarry Smith 
7693b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7694b3d3934dSBarry Smith 
76957db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7696b3d3934dSBarry Smith @*/
7697b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7698b3d3934dSBarry Smith {
7699b3d3934dSBarry Smith   PetscErrorCode ierr;
7700b3d3934dSBarry Smith 
7701b3d3934dSBarry Smith   PetscFunctionBegin;
7702b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7703b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7704b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7705b3d3934dSBarry Smith   PetscFunctionReturn(0);
7706b3d3934dSBarry Smith }
7707f2dee214SBarry Smith 
770824655328SShri /*@
770924655328SShri    TSRollBack - Rolls back one time step
771024655328SShri 
771124655328SShri    Collective on TS
771224655328SShri 
771324655328SShri    Input Parameter:
771424655328SShri .  ts - the TS context obtained from TSCreate()
771524655328SShri 
771624655328SShri    Level: advanced
771724655328SShri 
771824655328SShri .keywords: TS, timestep, rollback
771924655328SShri 
772024655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
772124655328SShri @*/
772224655328SShri PetscErrorCode  TSRollBack(TS ts)
772324655328SShri {
772424655328SShri   PetscErrorCode ierr;
772524655328SShri 
772624655328SShri   PetscFunctionBegin;
772724655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7728b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
772924655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
773024655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
773124655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
773224655328SShri   ts->ptime = ts->ptime_prev;
7733be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
77342808aa04SLisandro Dalcin   ts->steps--;
773510b82f12SShri Abhyankar   ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
7736b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
773724655328SShri   PetscFunctionReturn(0);
773824655328SShri }
7739aeb4809dSShri Abhyankar 
7740ff22ae23SHong Zhang /*@
7741ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7742ff22ae23SHong Zhang 
7743ff22ae23SHong Zhang    Input Parameter:
7744ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7745ff22ae23SHong Zhang 
7746ff22ae23SHong Zhang    Level: advanced
7747ff22ae23SHong Zhang 
7748ff22ae23SHong Zhang .keywords: TS, getstages
7749ff22ae23SHong Zhang 
7750ff22ae23SHong Zhang .seealso: TSCreate()
7751ff22ae23SHong Zhang @*/
7752ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7753ff22ae23SHong Zhang {
7754ff22ae23SHong Zhang   PetscErrorCode ierr;
7755ff22ae23SHong Zhang 
7756ff22ae23SHong Zhang   PetscFunctionBegin;
7757ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7758ff22ae23SHong Zhang   PetscValidPointer(ns,2);
7759ff22ae23SHong Zhang 
7760ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
7761ff22ae23SHong Zhang   else {
7762ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7763ff22ae23SHong Zhang   }
7764ff22ae23SHong Zhang   PetscFunctionReturn(0);
7765ff22ae23SHong Zhang }
7766ff22ae23SHong Zhang 
7767847ff0e1SMatthew G. Knepley /*@C
7768847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7769847ff0e1SMatthew G. Knepley 
7770847ff0e1SMatthew G. Knepley   Collective on SNES
7771847ff0e1SMatthew G. Knepley 
7772847ff0e1SMatthew G. Knepley   Input Parameters:
7773847ff0e1SMatthew G. Knepley + ts - the TS context
7774847ff0e1SMatthew G. Knepley . t - current timestep
7775847ff0e1SMatthew G. Knepley . U - state vector
7776847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7777847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7778847ff0e1SMatthew G. Knepley - ctx - an optional user context
7779847ff0e1SMatthew G. Knepley 
7780847ff0e1SMatthew G. Knepley   Output Parameters:
7781847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7782847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7783847ff0e1SMatthew G. Knepley 
7784847ff0e1SMatthew G. Knepley   Level: intermediate
7785847ff0e1SMatthew G. Knepley 
7786847ff0e1SMatthew G. Knepley   Notes:
7787847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7788847ff0e1SMatthew G. Knepley 
7789847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7790847ff0e1SMatthew G. Knepley 
7791847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7792847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7793847ff0e1SMatthew G. Knepley 
7794847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7795847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7796847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7797847ff0e1SMatthew G. Knepley 
7798847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7799847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7800847ff0e1SMatthew G. Knepley @*/
7801847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7802847ff0e1SMatthew G. Knepley {
7803847ff0e1SMatthew G. Knepley   SNES           snes;
7804847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7805847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7806847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7807847ff0e1SMatthew G. Knepley 
7808847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7809c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7810847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7811847ff0e1SMatthew G. Knepley   if (!color) {
7812847ff0e1SMatthew G. Knepley     DM         dm;
7813847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7814847ff0e1SMatthew G. Knepley 
7815847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7816847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7817847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7818847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7819847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7820847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7821847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7822847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7823847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7824847ff0e1SMatthew G. Knepley     } else {
7825847ff0e1SMatthew G. Knepley       MatColoring mc;
7826847ff0e1SMatthew G. Knepley 
7827847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7828847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7829847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7830847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7831847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7832847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7833847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7834847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7835847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7836847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7837847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7838847ff0e1SMatthew G. Knepley     }
7839847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7840847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7841847ff0e1SMatthew G. Knepley   }
7842847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7843847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7844847ff0e1SMatthew G. Knepley   if (J != B) {
7845847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7846847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7847847ff0e1SMatthew G. Knepley   }
7848847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7849847ff0e1SMatthew G. Knepley }
785093b34091SDebojyoti Ghosh 
7851cb9d8021SPierre Barbier de Reuille /*@
7852cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7853cb9d8021SPierre Barbier de Reuille 
7854cb9d8021SPierre Barbier de Reuille     Input Parameters:
7855cb9d8021SPierre Barbier de Reuille     ts - the TS context
7856cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7857cb9d8021SPierre Barbier de Reuille 
7858cb9d8021SPierre Barbier de Reuille     Level: intermediate
7859cb9d8021SPierre Barbier de Reuille 
7860cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7861cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7862cb9d8021SPierre Barbier de Reuille @*/
7863cb9d8021SPierre Barbier de Reuille 
7864d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7865cb9d8021SPierre Barbier de Reuille {
7866cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7867cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7868cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7869cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7870cb9d8021SPierre Barbier de Reuille }
7871cb9d8021SPierre Barbier de Reuille 
7872cb9d8021SPierre Barbier de Reuille /*@
7873cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7874cb9d8021SPierre Barbier de Reuille 
7875cb9d8021SPierre Barbier de Reuille     Input Parameters:
7876cb9d8021SPierre Barbier de Reuille     ts - the TS context
7877cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7878d183316bSPierre Barbier de Reuille     Y - state vector to check.
7879cb9d8021SPierre Barbier de Reuille 
7880cb9d8021SPierre Barbier de Reuille     Output Parameter:
7881cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7882cb9d8021SPierre Barbier de Reuille 
7883cb9d8021SPierre Barbier de Reuille     Note:
7884cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7885cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
788696a0c994SBarry Smith 
788796a0c994SBarry Smith     Level: advanced
7888cb9d8021SPierre Barbier de Reuille @*/
7889d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7890cb9d8021SPierre Barbier de Reuille {
7891cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
7892cb9d8021SPierre Barbier de Reuille 
7893cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7894cb9d8021SPierre Barbier de Reuille 
7895cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7896cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7897cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7898d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7899cb9d8021SPierre Barbier de Reuille   }
7900cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7901cb9d8021SPierre Barbier de Reuille }
79021ceb14c0SBarry Smith 
790393b34091SDebojyoti Ghosh /*@C
7904e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
790593b34091SDebojyoti Ghosh 
790693b34091SDebojyoti Ghosh   Collective on MPI_Comm
790793b34091SDebojyoti Ghosh 
790893b34091SDebojyoti Ghosh   Input Parameter:
790993b34091SDebojyoti Ghosh . tsin    - The input TS
791093b34091SDebojyoti Ghosh 
791193b34091SDebojyoti Ghosh   Output Parameter:
7912e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
791393b34091SDebojyoti Ghosh 
79145eca1a21SEmil Constantinescu   Notes:
79155eca1a21SEmil 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.
79165eca1a21SEmil Constantinescu 
7917baa10174SEmil 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);
79185eca1a21SEmil Constantinescu 
79195eca1a21SEmil Constantinescu   Level: developer
792093b34091SDebojyoti Ghosh 
7921e5168f73SEmil Constantinescu .keywords: TS, clone
7922e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
792393b34091SDebojyoti Ghosh @*/
7924baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
792593b34091SDebojyoti Ghosh {
792693b34091SDebojyoti Ghosh   TS             t;
792793b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7928dc846ba4SSatish Balay   SNES           snes_start;
7929dc846ba4SSatish Balay   DM             dm;
7930dc846ba4SSatish Balay   TSType         type;
793193b34091SDebojyoti Ghosh 
793293b34091SDebojyoti Ghosh   PetscFunctionBegin;
793393b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
793493b34091SDebojyoti Ghosh   *tsout = NULL;
793593b34091SDebojyoti Ghosh 
79367a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
793793b34091SDebojyoti Ghosh 
793893b34091SDebojyoti Ghosh   /* General TS description */
793993b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
794093b34091SDebojyoti Ghosh   t->setupcalled       = 0;
794193b34091SDebojyoti Ghosh   t->ksp_its           = 0;
794293b34091SDebojyoti Ghosh   t->snes_its          = 0;
794393b34091SDebojyoti Ghosh   t->nwork             = 0;
794493b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
794593b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
794693b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
794793b34091SDebojyoti Ghosh 
794834561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
794934561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7950d15a3a53SEmil Constantinescu 
795193b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
795293b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
795393b34091SDebojyoti Ghosh 
795493b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
795551699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
795693b34091SDebojyoti Ghosh 
7957e7069c78SShri   t->trajectory = tsin->trajectory;
7958e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7959e7069c78SShri 
7960e7069c78SShri   t->event = tsin->event;
79616b10a48eSSatish Balay   if (t->event) t->event->refct++;
7962e7069c78SShri 
796393b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
796493b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7965e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
796693b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
796793b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
796893b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
796993b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
797093b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
797193b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
797293b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
797393b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
797493b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
797593b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
797693b34091SDebojyoti Ghosh 
797793b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
797893b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
797993b34091SDebojyoti Ghosh 
798093b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
798193b34091SDebojyoti Ghosh 
798293b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
798393b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
798493b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
798593b34091SDebojyoti Ghosh 
798693b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
798793b34091SDebojyoti Ghosh 
79880d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
79890d4fed19SBarry Smith     PetscInt i;
79900d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
79910d4fed19SBarry Smith     for (i=0; i<10; i++) {
79920d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
79930d4fed19SBarry Smith     }
79940d4fed19SBarry Smith   }
799593b34091SDebojyoti Ghosh   *tsout = t;
799693b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
799793b34091SDebojyoti Ghosh }
7998