xref: /petsc/src/ts/interface/ts.c (revision 24989b8cf3e165ebaa37f4b2d30151131f137c58)
163dd3a1aSKris Buschelman 
2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
4d763cef2SBarry Smith 
5d5ba7fb7SMatthew Knepley /* Logging support */
67087cfbeSBarry Smith PetscClassId  TS_CLASSID;
7166c7f25SBarry Smith PetscLogEvent  TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
8d405a339SMatthew Knepley 
94a2ae208SSatish Balay #undef __FUNCT__
10bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
11bdad233fSMatthew Knepley /*
12bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
13bdad233fSMatthew Knepley 
14bdad233fSMatthew Knepley   Collective on TS
15bdad233fSMatthew Knepley 
16bdad233fSMatthew Knepley   Input Parameter:
17bdad233fSMatthew Knepley . ts - The ts
18bdad233fSMatthew Knepley 
19bdad233fSMatthew Knepley   Level: intermediate
20bdad233fSMatthew Knepley 
21bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
22bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
23bdad233fSMatthew Knepley */
246849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
25bdad233fSMatthew Knepley {
26ace3abfcSBarry Smith   PetscBool      opt;
272fc52814SBarry Smith   const char     *defaultType;
28bdad233fSMatthew Knepley   char           typeName[256];
29dfbe8321SBarry Smith   PetscErrorCode ierr;
30bdad233fSMatthew Knepley 
31bdad233fSMatthew Knepley   PetscFunctionBegin;
327adad957SLisandro Dalcin   if (((PetscObject)ts)->type_name) {
337adad957SLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
34bdad233fSMatthew Knepley   } else {
359596e0b4SJed Brown     defaultType = TSEULER;
36bdad233fSMatthew Knepley   }
37bdad233fSMatthew Knepley 
38cce0b1b2SLisandro Dalcin   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
39bdad233fSMatthew Knepley   ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
40a7cc72afSBarry Smith   if (opt) {
41bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
42bdad233fSMatthew Knepley   } else {
43bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
44bdad233fSMatthew Knepley   }
45bdad233fSMatthew Knepley   PetscFunctionReturn(0);
46bdad233fSMatthew Knepley }
47bdad233fSMatthew Knepley 
48bdad233fSMatthew Knepley #undef __FUNCT__
49bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
50bdad233fSMatthew Knepley /*@
51bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
52bdad233fSMatthew Knepley 
53bdad233fSMatthew Knepley    Collective on TS
54bdad233fSMatthew Knepley 
55bdad233fSMatthew Knepley    Input Parameter:
56bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
57bdad233fSMatthew Knepley 
58bdad233fSMatthew Knepley    Options Database Keys:
594d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
60bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
613bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
62bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
63bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
64a6570f20SBarry Smith -  -ts_monitor_draw - plot information at each timestep
65bdad233fSMatthew Knepley 
66bdad233fSMatthew Knepley    Level: beginner
67bdad233fSMatthew Knepley 
68bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
69bdad233fSMatthew Knepley 
70a313700dSBarry Smith .seealso: TSGetType()
71bdad233fSMatthew Knepley @*/
727087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
73bdad233fSMatthew Knepley {
74ace3abfcSBarry Smith   PetscBool      opt,flg;
75dfbe8321SBarry Smith   PetscErrorCode ierr;
76649052a6SBarry Smith   PetscViewer    monviewer;
77eabae89aSBarry Smith   char           monfilename[PETSC_MAX_PATH_LEN];
78089b2837SJed Brown   SNES           snes;
791c3436cfSJed Brown   TSAdapt        adapt;
80bdad233fSMatthew Knepley 
81bdad233fSMatthew Knepley   PetscFunctionBegin;
820700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
833194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
84a43b19c4SJed Brown     /* Handle TS type options */
85a43b19c4SJed Brown     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
86bdad233fSMatthew Knepley 
87bdad233fSMatthew Knepley     /* Handle generic TS options */
88bdad233fSMatthew Knepley     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
893bca7d26SBarry Smith     ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
90d8cd7023SBarry Smith     ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr);
91d8cd7023SBarry Smith     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&ts->time_step,PETSC_NULL);CHKERRQ(ierr);
92a43b19c4SJed Brown     opt = ts->exact_final_time == PETSC_DECIDE ? PETSC_FALSE : (PetscBool)ts->exact_final_time;
93a43b19c4SJed Brown     ierr = PetscOptionsBool("-ts_exact_final_time","Interpolate output to stop exactly at the final time","TSSetExactFinalTime",opt,&opt,&flg);CHKERRQ(ierr);
94461e00b3SSean Farley     if (flg) {ierr = TSSetExactFinalTime(ts,opt);CHKERRQ(ierr);}
95cef5090cSJed Brown     ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,PETSC_NULL);CHKERRQ(ierr);
96cef5090cSJed Brown     ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,PETSC_NULL);CHKERRQ(ierr);
97cef5090cSJed Brown     ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr);
981c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr);
991c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr);
100bdad233fSMatthew Knepley 
101bdad233fSMatthew Knepley     /* Monitor options */
102a6570f20SBarry Smith     ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
103eabae89aSBarry Smith     if (flg) {
104649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr);
105649052a6SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
106bdad233fSMatthew Knepley     }
1075180491cSLisandro Dalcin     ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1085180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1095180491cSLisandro Dalcin 
11090d69ab7SBarry Smith     opt  = PETSC_FALSE;
111acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
112a7cc72afSBarry Smith     if (opt) {
113a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
114bdad233fSMatthew Knepley     }
11590d69ab7SBarry Smith     opt  = PETSC_FALSE;
116acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
117a7cc72afSBarry Smith     if (opt) {
1186083293cSBarry Smith       void *ctx;
1196083293cSBarry Smith       ierr = TSMonitorSolutionCreate(ts,PETSC_NULL,&ctx);CHKERRQ(ierr);
1206083293cSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolution,ctx,TSMonitorSolutionDestroy);CHKERRQ(ierr);
121bdad233fSMatthew Knepley     }
122fb1732b5SBarry Smith     opt  = PETSC_FALSE;
123fb1732b5SBarry Smith     ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
124fb1732b5SBarry Smith     if (flg) {
125fb1732b5SBarry Smith       PetscViewer ctx;
126fb1732b5SBarry Smith       if (monfilename[0]) {
127fb1732b5SBarry Smith         ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
128fb1732b5SBarry Smith       } else {
129fb1732b5SBarry Smith         ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm);
130fb1732b5SBarry Smith       }
131fb1732b5SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
132fb1732b5SBarry Smith     }
133ed81e22dSJed Brown     opt  = PETSC_FALSE;
134ed81e22dSJed Brown     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);
135ed81e22dSJed Brown     if (flg) {
136ed81e22dSJed Brown       const char *ptr,*ptr2;
137ed81e22dSJed Brown       char *filetemplate;
138ed81e22dSJed Brown       if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
139ed81e22dSJed Brown       /* Do some cursory validation of the input. */
140ed81e22dSJed Brown       ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
141ed81e22dSJed Brown       if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
142ed81e22dSJed Brown       for (ptr++ ; ptr && *ptr; ptr++) {
143ed81e22dSJed Brown         ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
144ed81e22dSJed Brown         if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts");
145ed81e22dSJed Brown         if (ptr2) break;
146ed81e22dSJed Brown       }
147ed81e22dSJed Brown       ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
148ed81e22dSJed Brown       ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
149ed81e22dSJed Brown     }
150bdad233fSMatthew Knepley 
1511c3436cfSJed Brown     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
1521c3436cfSJed Brown     ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
1531c3436cfSJed Brown 
154d52bd9f3SBarry Smith     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
155d52bd9f3SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
156d52bd9f3SBarry Smith 
157bdad233fSMatthew Knepley     /* Handle specific TS options */
158abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
159bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
160bdad233fSMatthew Knepley     }
1615d973c19SBarry Smith 
1625d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
1635d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
164bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
165bdad233fSMatthew Knepley   PetscFunctionReturn(0);
166bdad233fSMatthew Knepley }
167bdad233fSMatthew Knepley 
168bdad233fSMatthew Knepley #undef __FUNCT__
169cdcf91faSSean Farley #undef __FUNCT__
1704a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
171a7a1495cSBarry Smith /*@
1728c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
173a7a1495cSBarry Smith       set with TSSetRHSJacobian().
174a7a1495cSBarry Smith 
175a7a1495cSBarry Smith    Collective on TS and Vec
176a7a1495cSBarry Smith 
177a7a1495cSBarry Smith    Input Parameters:
178316643e7SJed Brown +  ts - the TS context
179a7a1495cSBarry Smith .  t - current timestep
180a7a1495cSBarry Smith -  x - input vector
181a7a1495cSBarry Smith 
182a7a1495cSBarry Smith    Output Parameters:
183a7a1495cSBarry Smith +  A - Jacobian matrix
184a7a1495cSBarry Smith .  B - optional preconditioning matrix
185a7a1495cSBarry Smith -  flag - flag indicating matrix structure
186a7a1495cSBarry Smith 
187a7a1495cSBarry Smith    Notes:
188a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
189a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
190a7a1495cSBarry Smith 
19194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
192a7a1495cSBarry Smith    flag parameter.
193a7a1495cSBarry Smith 
194a7a1495cSBarry Smith    Level: developer
195a7a1495cSBarry Smith 
196a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
197a7a1495cSBarry Smith 
19894b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
199a7a1495cSBarry Smith @*/
2007087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
201a7a1495cSBarry Smith {
202dfbe8321SBarry Smith   PetscErrorCode ierr;
2030e4ef248SJed Brown   PetscInt       Xstate;
204*24989b8cSPeter Brune   DM             dm;
205*24989b8cSPeter Brune   TSDM           tsdm;
206*24989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
207*24989b8cSPeter Brune   void           *ctx;
208*24989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
209a7a1495cSBarry Smith 
210a7a1495cSBarry Smith   PetscFunctionBegin;
2110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2120700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
213c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
214*24989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
215*24989b8cSPeter Brune   ierr = DMTSGetContext(dm,&tsdm);CHKERRQ(ierr);
216*24989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
217*24989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr);
2180e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
2190e4ef248SJed Brown   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) {
2200e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
2210e4ef248SJed Brown     PetscFunctionReturn(0);
222f8ede8e7SJed Brown   }
223d90be118SSean Farley 
224*24989b8cSPeter Brune   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
225d90be118SSean Farley 
226*24989b8cSPeter Brune   if (rhsjacobianfunc) {
227d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
228a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
229a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
230*24989b8cSPeter Brune     ierr = (*rhsjacobianfunc)(ts,t,X,A,B,flg,ctx);CHKERRQ(ierr);
231a7a1495cSBarry Smith     PetscStackPop;
232d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
233a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
2340700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
2350700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
236ef66eb69SBarry Smith   } else {
237214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
238214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
239214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
240ef66eb69SBarry Smith   }
2410e4ef248SJed Brown   ts->rhsjacobian.time = t;
2420e4ef248SJed Brown   ts->rhsjacobian.X = X;
2430e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
2440e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
245a7a1495cSBarry Smith   PetscFunctionReturn(0);
246a7a1495cSBarry Smith }
247a7a1495cSBarry Smith 
2484a2ae208SSatish Balay #undef __FUNCT__
2494a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
250316643e7SJed Brown /*@
251d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
252d763cef2SBarry Smith 
253316643e7SJed Brown    Collective on TS and Vec
254316643e7SJed Brown 
255316643e7SJed Brown    Input Parameters:
256316643e7SJed Brown +  ts - the TS context
257316643e7SJed Brown .  t - current time
258316643e7SJed Brown -  x - state vector
259316643e7SJed Brown 
260316643e7SJed Brown    Output Parameter:
261316643e7SJed Brown .  y - right hand side
262316643e7SJed Brown 
263316643e7SJed Brown    Note:
264316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
265316643e7SJed Brown    is used internally within the nonlinear solvers.
266316643e7SJed Brown 
267316643e7SJed Brown    Level: developer
268316643e7SJed Brown 
269316643e7SJed Brown .keywords: TS, compute
270316643e7SJed Brown 
271316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
272316643e7SJed Brown @*/
273dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
274d763cef2SBarry Smith {
275dfbe8321SBarry Smith   PetscErrorCode ierr;
276d763cef2SBarry Smith 
277*24989b8cSPeter Brune   TSRHSFunction rhsfunction;
278*24989b8cSPeter Brune   TSIFunction   ifunction;
279*24989b8cSPeter Brune   void          *ctx;
280*24989b8cSPeter Brune   DM            dm;
281*24989b8cSPeter Brune 
282d763cef2SBarry Smith   PetscFunctionBegin;
2830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2840700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2850700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
286*24989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
287*24989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
288*24989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr);
289d763cef2SBarry Smith 
290*24989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
291d763cef2SBarry Smith 
292d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
293*24989b8cSPeter Brune   if (rhsfunction) {
294d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
295*24989b8cSPeter Brune     ierr = (*rhsfunction)(ts,t,x,y,ctx);CHKERRQ(ierr);
296d763cef2SBarry Smith     PetscStackPop;
297214bc6a2SJed Brown   } else {
298214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
299b2cd27e8SJed Brown   }
30044a41b28SSean Farley 
301d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
302d763cef2SBarry Smith   PetscFunctionReturn(0);
303d763cef2SBarry Smith }
304d763cef2SBarry Smith 
3054a2ae208SSatish Balay #undef __FUNCT__
306214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
307214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
308214bc6a2SJed Brown {
3092dd45cf8SJed Brown   Vec            F;
310214bc6a2SJed Brown   PetscErrorCode ierr;
311214bc6a2SJed Brown 
312214bc6a2SJed Brown   PetscFunctionBegin;
3130da9a4f0SJed Brown   *Frhs = PETSC_NULL;
3142dd45cf8SJed Brown   ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
315214bc6a2SJed Brown   if (!ts->Frhs) {
3162dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
317214bc6a2SJed Brown   }
318214bc6a2SJed Brown   *Frhs = ts->Frhs;
319214bc6a2SJed Brown   PetscFunctionReturn(0);
320214bc6a2SJed Brown }
321214bc6a2SJed Brown 
322214bc6a2SJed Brown #undef __FUNCT__
323214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
324214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
325214bc6a2SJed Brown {
326214bc6a2SJed Brown   Mat            A,B;
3272dd45cf8SJed Brown   PetscErrorCode ierr;
328214bc6a2SJed Brown 
329214bc6a2SJed Brown   PetscFunctionBegin;
330214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
331214bc6a2SJed Brown   if (Arhs) {
332214bc6a2SJed Brown     if (!ts->Arhs) {
333214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
334214bc6a2SJed Brown     }
335214bc6a2SJed Brown     *Arhs = ts->Arhs;
336214bc6a2SJed Brown   }
337214bc6a2SJed Brown   if (Brhs) {
338214bc6a2SJed Brown     if (!ts->Brhs) {
339214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
340214bc6a2SJed Brown     }
341214bc6a2SJed Brown     *Brhs = ts->Brhs;
342214bc6a2SJed Brown   }
343214bc6a2SJed Brown   PetscFunctionReturn(0);
344214bc6a2SJed Brown }
345214bc6a2SJed Brown 
346214bc6a2SJed Brown #undef __FUNCT__
347316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
348316643e7SJed Brown /*@
349316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
350316643e7SJed Brown 
351316643e7SJed Brown    Collective on TS and Vec
352316643e7SJed Brown 
353316643e7SJed Brown    Input Parameters:
354316643e7SJed Brown +  ts - the TS context
355316643e7SJed Brown .  t - current time
356316643e7SJed Brown .  X - state vector
357214bc6a2SJed Brown .  Xdot - time derivative of state vector
358214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
359316643e7SJed Brown 
360316643e7SJed Brown    Output Parameter:
361316643e7SJed Brown .  Y - right hand side
362316643e7SJed Brown 
363316643e7SJed Brown    Note:
364316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
365316643e7SJed Brown    is used internally within the nonlinear solvers.
366316643e7SJed Brown 
367316643e7SJed Brown    If the user did did not write their equations in implicit form, this
368316643e7SJed Brown    function recasts them in implicit form.
369316643e7SJed Brown 
370316643e7SJed Brown    Level: developer
371316643e7SJed Brown 
372316643e7SJed Brown .keywords: TS, compute
373316643e7SJed Brown 
374316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
375316643e7SJed Brown @*/
376214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex)
377316643e7SJed Brown {
378316643e7SJed Brown   PetscErrorCode ierr;
379*24989b8cSPeter Brune   TSIFunction    ifunction;
380*24989b8cSPeter Brune   TSRHSFunction  rhsfunction;
381*24989b8cSPeter Brune   void           *ctx;
382*24989b8cSPeter Brune   DM             dm;
383316643e7SJed Brown 
384316643e7SJed Brown   PetscFunctionBegin;
3850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3860700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3870700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
3880700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
389316643e7SJed Brown 
390*24989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
391*24989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
392*24989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr);
393*24989b8cSPeter Brune 
394*24989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
395d90be118SSean Farley 
396316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
397*24989b8cSPeter Brune   if (ifunction) {
398316643e7SJed Brown     PetscStackPush("TS user implicit function");
399*24989b8cSPeter Brune     ierr = (*ifunction)(ts,t,X,Xdot,Y,ctx);CHKERRQ(ierr);
400316643e7SJed Brown     PetscStackPop;
401214bc6a2SJed Brown   }
402214bc6a2SJed Brown   if (imex) {
403*24989b8cSPeter Brune     if (!ifunction) {
4042dd45cf8SJed Brown       ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);
4052dd45cf8SJed Brown     }
406*24989b8cSPeter Brune   } else if (rhsfunction) {
407*24989b8cSPeter Brune     if (ifunction) {
408214bc6a2SJed Brown       Vec Frhs;
409214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
410214bc6a2SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr);
411214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
4122dd45cf8SJed Brown     } else {
4132dd45cf8SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr);
4142dd45cf8SJed Brown       ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr);
415316643e7SJed Brown     }
4164a6899ffSJed Brown   }
417316643e7SJed Brown   ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
418316643e7SJed Brown   PetscFunctionReturn(0);
419316643e7SJed Brown }
420316643e7SJed Brown 
421316643e7SJed Brown #undef __FUNCT__
422316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
423316643e7SJed Brown /*@
424316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
425316643e7SJed Brown 
426316643e7SJed Brown    Collective on TS and Vec
427316643e7SJed Brown 
428316643e7SJed Brown    Input
429316643e7SJed Brown       Input Parameters:
430316643e7SJed Brown +  ts - the TS context
431316643e7SJed Brown .  t - current timestep
432316643e7SJed Brown .  X - state vector
433316643e7SJed Brown .  Xdot - time derivative of state vector
434214bc6a2SJed Brown .  shift - shift to apply, see note below
435214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
436316643e7SJed Brown 
437316643e7SJed Brown    Output Parameters:
438316643e7SJed Brown +  A - Jacobian matrix
439316643e7SJed Brown .  B - optional preconditioning matrix
440316643e7SJed Brown -  flag - flag indicating matrix structure
441316643e7SJed Brown 
442316643e7SJed Brown    Notes:
443316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
444316643e7SJed Brown 
445316643e7SJed Brown    dF/dX + shift*dF/dXdot
446316643e7SJed Brown 
447316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
448316643e7SJed Brown    is used internally within the nonlinear solvers.
449316643e7SJed Brown 
450316643e7SJed Brown    Level: developer
451316643e7SJed Brown 
452316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
453316643e7SJed Brown 
454316643e7SJed Brown .seealso:  TSSetIJacobian()
45563495f91SJed Brown @*/
456214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
457316643e7SJed Brown {
4580026cea9SSean Farley   PetscInt Xstate, Xdotstate;
459316643e7SJed Brown   PetscErrorCode ierr;
460*24989b8cSPeter Brune   TSIJacobian    ijacobian;
461*24989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
462*24989b8cSPeter Brune   DM             dm;
463*24989b8cSPeter Brune   void           *ctx;
464316643e7SJed Brown 
465316643e7SJed Brown   PetscFunctionBegin;
466*24989b8cSPeter Brune 
4670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4680700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4690700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
470316643e7SJed Brown   PetscValidPointer(A,6);
4710700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
472316643e7SJed Brown   PetscValidPointer(B,7);
4730700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
474316643e7SJed Brown   PetscValidPointer(flg,8);
475*24989b8cSPeter Brune 
476*24989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
477*24989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
478*24989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr);
479*24989b8cSPeter Brune 
4800026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
4810026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr);
4820026cea9SSean Farley   if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == X && ts->ijacobian.Xstate == Xstate && ts->ijacobian.Xdot == Xdot && ts->ijacobian.Xdotstate == Xdotstate && ts->ijacobian.imex == imex))) {
4830026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
4840026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4850026cea9SSean Farley     PetscFunctionReturn(0);
4860026cea9SSean Farley   }
487316643e7SJed Brown 
488*24989b8cSPeter Brune   if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
489316643e7SJed Brown 
4904e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
491316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
492*24989b8cSPeter Brune   if (ijacobian) {
4932dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
494316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
495*24989b8cSPeter Brune     ierr = (*ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ctx);CHKERRQ(ierr);
496316643e7SJed Brown     PetscStackPop;
497214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
498214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
499214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
5004a6899ffSJed Brown   }
501214bc6a2SJed Brown   if (imex) {
502*24989b8cSPeter Brune     if (!ijacobian) {  /* system was written as Xdot = F(t,X) */
503214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
5042dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
505214bc6a2SJed Brown       if (*A != *B) {
506214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
507214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
508214bc6a2SJed Brown       }
509214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
510214bc6a2SJed Brown     }
511214bc6a2SJed Brown   } else {
512*24989b8cSPeter Brune     if (!ijacobian) {
513214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr);
514214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
515214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
516316643e7SJed Brown       if (*A != *B) {
517316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
518316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
519316643e7SJed Brown       }
520*24989b8cSPeter Brune     } else if (rhsjacobian) {
521214bc6a2SJed Brown       Mat Arhs,Brhs;
522214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
523214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
524214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
525214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
526214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
527214bc6a2SJed Brown       if (*A != *B) {
528214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
529214bc6a2SJed Brown       }
530214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
531214bc6a2SJed Brown     }
532316643e7SJed Brown   }
5330026cea9SSean Farley 
5340026cea9SSean Farley   ts->ijacobian.time = t;
5350026cea9SSean Farley   ts->ijacobian.X = X;
5360026cea9SSean Farley   ts->ijacobian.Xdot = Xdot;
5370026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr);
5380026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
5390026cea9SSean Farley   ts->ijacobian.shift = shift;
5400026cea9SSean Farley   ts->ijacobian.imex = imex;
5410026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
542316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
543316643e7SJed Brown   PetscFunctionReturn(0);
544316643e7SJed Brown }
545316643e7SJed Brown 
546316643e7SJed Brown #undef __FUNCT__
5474a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
548d763cef2SBarry Smith /*@C
549d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
550d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
551d763cef2SBarry Smith 
5523f9fe445SBarry Smith     Logically Collective on TS
553d763cef2SBarry Smith 
554d763cef2SBarry Smith     Input Parameters:
555d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
556ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
557d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
558d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
559d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
560d763cef2SBarry Smith 
561d763cef2SBarry Smith     Calling sequence of func:
56287828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
563d763cef2SBarry Smith 
564d763cef2SBarry Smith +   t - current timestep
565d763cef2SBarry Smith .   u - input vector
566d763cef2SBarry Smith .   F - function vector
567d763cef2SBarry Smith -   ctx - [optional] user-defined function context
568d763cef2SBarry Smith 
569d763cef2SBarry Smith     Level: beginner
570d763cef2SBarry Smith 
571d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
572d763cef2SBarry Smith 
573d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
574d763cef2SBarry Smith @*/
575089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
576d763cef2SBarry Smith {
577089b2837SJed Brown   PetscErrorCode ierr;
578089b2837SJed Brown   SNES           snes;
579e856ceecSJed Brown   Vec            ralloc = PETSC_NULL;
580*24989b8cSPeter Brune   DM             dm;
581d763cef2SBarry Smith 
582089b2837SJed Brown   PetscFunctionBegin;
5830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
584ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
585*24989b8cSPeter Brune 
586*24989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
587*24989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
588089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
589e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
590e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
591e856ceecSJed Brown     r = ralloc;
592e856ceecSJed Brown   }
593089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
594e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
595d763cef2SBarry Smith   PetscFunctionReturn(0);
596d763cef2SBarry Smith }
597d763cef2SBarry Smith 
5984a2ae208SSatish Balay #undef __FUNCT__
5994a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
600d763cef2SBarry Smith /*@C
601d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
602d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
603d763cef2SBarry Smith 
6043f9fe445SBarry Smith    Logically Collective on TS
605d763cef2SBarry Smith 
606d763cef2SBarry Smith    Input Parameters:
607d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
608d763cef2SBarry Smith .  A   - Jacobian matrix
609d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
610d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
611d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
612d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
613d763cef2SBarry Smith 
614d763cef2SBarry Smith    Calling sequence of func:
61587828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
616d763cef2SBarry Smith 
617d763cef2SBarry Smith +  t - current timestep
618d763cef2SBarry Smith .  u - input vector
619d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
620d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
621d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
62294b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
623d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
624d763cef2SBarry Smith 
625d763cef2SBarry Smith    Notes:
62694b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
627d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
628d763cef2SBarry Smith 
629d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
630d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
63156335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
632d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
633d763cef2SBarry Smith    throughout the global iterations.
634d763cef2SBarry Smith 
635d763cef2SBarry Smith    Level: beginner
636d763cef2SBarry Smith 
637d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
638d763cef2SBarry Smith 
639ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
640d763cef2SBarry Smith 
641d763cef2SBarry Smith @*/
642089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
643d763cef2SBarry Smith {
644277b19d0SLisandro Dalcin   PetscErrorCode ierr;
645089b2837SJed Brown   SNES           snes;
646*24989b8cSPeter Brune   DM             dm;
647*24989b8cSPeter Brune   TSIJacobian    ijacobian;
648277b19d0SLisandro Dalcin 
649d763cef2SBarry Smith   PetscFunctionBegin;
6500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
651277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
652277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
653277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
654277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
655d763cef2SBarry Smith 
656*24989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
657*24989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
658*24989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr);
659*24989b8cSPeter Brune 
660089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
661*24989b8cSPeter Brune   if (ijacobian) {
662089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
6630e4ef248SJed Brown   }
6640e4ef248SJed Brown   if (A) {
6650e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
6660e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
6670e4ef248SJed Brown     ts->Arhs = A;
6680e4ef248SJed Brown   }
6690e4ef248SJed Brown   if (B) {
6700e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
6710e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
6720e4ef248SJed Brown     ts->Brhs = B;
6730e4ef248SJed Brown   }
674d763cef2SBarry Smith   PetscFunctionReturn(0);
675d763cef2SBarry Smith }
676d763cef2SBarry Smith 
677316643e7SJed Brown 
678316643e7SJed Brown #undef __FUNCT__
679316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
680316643e7SJed Brown /*@C
681316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
682316643e7SJed Brown 
6833f9fe445SBarry Smith    Logically Collective on TS
684316643e7SJed Brown 
685316643e7SJed Brown    Input Parameters:
686316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
687ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
688316643e7SJed Brown .  f   - the function evaluation routine
689316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
690316643e7SJed Brown 
691316643e7SJed Brown    Calling sequence of f:
692316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
693316643e7SJed Brown 
694316643e7SJed Brown +  t   - time at step/stage being solved
695316643e7SJed Brown .  u   - state vector
696316643e7SJed Brown .  u_t - time derivative of state vector
697316643e7SJed Brown .  F   - function vector
698316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
699316643e7SJed Brown 
700316643e7SJed Brown    Important:
701d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
702316643e7SJed Brown 
703316643e7SJed Brown    Level: beginner
704316643e7SJed Brown 
705316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
706316643e7SJed Brown 
707d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
708316643e7SJed Brown @*/
709089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
710316643e7SJed Brown {
711089b2837SJed Brown   PetscErrorCode ierr;
712089b2837SJed Brown   SNES           snes;
713e856ceecSJed Brown   Vec            resalloc = PETSC_NULL;
714*24989b8cSPeter Brune   DM             dm;
715316643e7SJed Brown 
716316643e7SJed Brown   PetscFunctionBegin;
7170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
718ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
719*24989b8cSPeter Brune 
720*24989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
721*24989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
722*24989b8cSPeter Brune 
723089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
724e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
725e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
726e856ceecSJed Brown     res = resalloc;
727e856ceecSJed Brown   }
728089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
729e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
730*24989b8cSPeter Brune 
731089b2837SJed Brown   PetscFunctionReturn(0);
732089b2837SJed Brown }
733089b2837SJed Brown 
734089b2837SJed Brown #undef __FUNCT__
735089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
736089b2837SJed Brown /*@C
737089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
738089b2837SJed Brown 
739089b2837SJed Brown    Not Collective
740089b2837SJed Brown 
741089b2837SJed Brown    Input Parameter:
742089b2837SJed Brown .  ts - the TS context
743089b2837SJed Brown 
744089b2837SJed Brown    Output Parameter:
745089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
746089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
747089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
748089b2837SJed Brown 
749089b2837SJed Brown    Level: advanced
750089b2837SJed Brown 
751089b2837SJed Brown .keywords: TS, nonlinear, get, function
752089b2837SJed Brown 
753089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
754089b2837SJed Brown @*/
755089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
756089b2837SJed Brown {
757089b2837SJed Brown   PetscErrorCode ierr;
758089b2837SJed Brown   SNES snes;
759*24989b8cSPeter Brune   DM   dm;
760089b2837SJed Brown 
761089b2837SJed Brown   PetscFunctionBegin;
762089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
763089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
764089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
765*24989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
766*24989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
767089b2837SJed Brown   PetscFunctionReturn(0);
768089b2837SJed Brown }
769089b2837SJed Brown 
770089b2837SJed Brown #undef __FUNCT__
771089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
772089b2837SJed Brown /*@C
773089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
774089b2837SJed Brown 
775089b2837SJed Brown    Not Collective
776089b2837SJed Brown 
777089b2837SJed Brown    Input Parameter:
778089b2837SJed Brown .  ts - the TS context
779089b2837SJed Brown 
780089b2837SJed Brown    Output Parameter:
781089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
782089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
783089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
784089b2837SJed Brown 
785089b2837SJed Brown    Level: advanced
786089b2837SJed Brown 
787089b2837SJed Brown .keywords: TS, nonlinear, get, function
788089b2837SJed Brown 
789089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
790089b2837SJed Brown @*/
791089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
792089b2837SJed Brown {
793089b2837SJed Brown   PetscErrorCode ierr;
794089b2837SJed Brown   SNES snes;
795*24989b8cSPeter Brune   DM   dm;
796089b2837SJed Brown 
797089b2837SJed Brown   PetscFunctionBegin;
798089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
799089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
800089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
801*24989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
802*24989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
803316643e7SJed Brown   PetscFunctionReturn(0);
804316643e7SJed Brown }
805316643e7SJed Brown 
806316643e7SJed Brown #undef __FUNCT__
807316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
808316643e7SJed Brown /*@C
809a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
810a4f0a591SBarry Smith         you provided with TSSetIFunction().
811316643e7SJed Brown 
8123f9fe445SBarry Smith    Logically Collective on TS
813316643e7SJed Brown 
814316643e7SJed Brown    Input Parameters:
815316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
816316643e7SJed Brown .  A   - Jacobian matrix
817316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
818316643e7SJed Brown .  f   - the Jacobian evaluation routine
819316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
820316643e7SJed Brown 
821316643e7SJed Brown    Calling sequence of f:
8221b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
823316643e7SJed Brown 
824316643e7SJed Brown +  t    - time at step/stage being solved
8251b4a444bSJed Brown .  U    - state vector
8261b4a444bSJed Brown .  U_t  - time derivative of state vector
827316643e7SJed Brown .  a    - shift
8281b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
829316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
830316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
831316643e7SJed Brown           structure (same as flag in KSPSetOperators())
832316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
833316643e7SJed Brown 
834316643e7SJed Brown    Notes:
835316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
836316643e7SJed Brown 
837a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
838a4f0a591SBarry Smith    the Jacobian of G(U) = F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
839a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
840a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
841a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
842a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
843a4f0a591SBarry Smith 
844316643e7SJed Brown    Level: beginner
845316643e7SJed Brown 
846316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
847316643e7SJed Brown 
848ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
849316643e7SJed Brown 
850316643e7SJed Brown @*/
8517087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
852316643e7SJed Brown {
853316643e7SJed Brown   PetscErrorCode ierr;
854089b2837SJed Brown   SNES           snes;
855*24989b8cSPeter Brune   DM             dm;
856316643e7SJed Brown 
857316643e7SJed Brown   PetscFunctionBegin;
8580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8590700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
8600700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
861316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
862316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
863*24989b8cSPeter Brune 
864*24989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
865*24989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
866*24989b8cSPeter Brune 
867089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
868089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
869316643e7SJed Brown   PetscFunctionReturn(0);
870316643e7SJed Brown }
871316643e7SJed Brown 
8724a2ae208SSatish Balay #undef __FUNCT__
8734a2ae208SSatish Balay #define __FUNCT__ "TSView"
8747e2c5f70SBarry Smith /*@C
875d763cef2SBarry Smith     TSView - Prints the TS data structure.
876d763cef2SBarry Smith 
8774c49b128SBarry Smith     Collective on TS
878d763cef2SBarry Smith 
879d763cef2SBarry Smith     Input Parameters:
880d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
881d763cef2SBarry Smith -   viewer - visualization context
882d763cef2SBarry Smith 
883d763cef2SBarry Smith     Options Database Key:
884d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
885d763cef2SBarry Smith 
886d763cef2SBarry Smith     Notes:
887d763cef2SBarry Smith     The available visualization contexts include
888b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
889b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
890d763cef2SBarry Smith          output where only the first processor opens
891d763cef2SBarry Smith          the file.  All other processors send their
892d763cef2SBarry Smith          data to the first processor to print.
893d763cef2SBarry Smith 
894d763cef2SBarry Smith     The user can open an alternative visualization context with
895b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
896d763cef2SBarry Smith 
897d763cef2SBarry Smith     Level: beginner
898d763cef2SBarry Smith 
899d763cef2SBarry Smith .keywords: TS, timestep, view
900d763cef2SBarry Smith 
901b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
902d763cef2SBarry Smith @*/
9037087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
904d763cef2SBarry Smith {
905dfbe8321SBarry Smith   PetscErrorCode ierr;
906a313700dSBarry Smith   const TSType   type;
907089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
908d763cef2SBarry Smith 
909d763cef2SBarry Smith   PetscFunctionBegin;
9100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9113050cee2SBarry Smith   if (!viewer) {
9127adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
9133050cee2SBarry Smith   }
9140700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
915c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
916fd16b177SBarry Smith 
917251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
918251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
91932077d6dSBarry Smith   if (iascii) {
920317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
92177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
922a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
923d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
9245ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
925c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
926d763cef2SBarry Smith     }
9275ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
928193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
929d52bd9f3SBarry Smith     if (ts->ops->view) {
930d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
931d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
932d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
933d52bd9f3SBarry Smith     }
9340f5bd95cSBarry Smith   } else if (isstring) {
935a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
936b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
937d763cef2SBarry Smith   }
938b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
939251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
940b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
941d763cef2SBarry Smith   PetscFunctionReturn(0);
942d763cef2SBarry Smith }
943d763cef2SBarry Smith 
944d763cef2SBarry Smith 
9454a2ae208SSatish Balay #undef __FUNCT__
9464a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
947b07ff414SBarry Smith /*@
948d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
949d763cef2SBarry Smith    the timesteppers.
950d763cef2SBarry Smith 
9513f9fe445SBarry Smith    Logically Collective on TS
952d763cef2SBarry Smith 
953d763cef2SBarry Smith    Input Parameters:
954d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
955d763cef2SBarry Smith -  usrP - optional user context
956d763cef2SBarry Smith 
957d763cef2SBarry Smith    Level: intermediate
958d763cef2SBarry Smith 
959d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
960d763cef2SBarry Smith 
961d763cef2SBarry Smith .seealso: TSGetApplicationContext()
962d763cef2SBarry Smith @*/
9637087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
964d763cef2SBarry Smith {
965d763cef2SBarry Smith   PetscFunctionBegin;
9660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
967d763cef2SBarry Smith   ts->user = usrP;
968d763cef2SBarry Smith   PetscFunctionReturn(0);
969d763cef2SBarry Smith }
970d763cef2SBarry Smith 
9714a2ae208SSatish Balay #undef __FUNCT__
9724a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
973b07ff414SBarry Smith /*@
974d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
975d763cef2SBarry Smith     timestepper.
976d763cef2SBarry Smith 
977d763cef2SBarry Smith     Not Collective
978d763cef2SBarry Smith 
979d763cef2SBarry Smith     Input Parameter:
980d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
981d763cef2SBarry Smith 
982d763cef2SBarry Smith     Output Parameter:
983d763cef2SBarry Smith .   usrP - user context
984d763cef2SBarry Smith 
985d763cef2SBarry Smith     Level: intermediate
986d763cef2SBarry Smith 
987d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
988d763cef2SBarry Smith 
989d763cef2SBarry Smith .seealso: TSSetApplicationContext()
990d763cef2SBarry Smith @*/
991e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
992d763cef2SBarry Smith {
993d763cef2SBarry Smith   PetscFunctionBegin;
9940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
995e71120c6SJed Brown   *(void**)usrP = ts->user;
996d763cef2SBarry Smith   PetscFunctionReturn(0);
997d763cef2SBarry Smith }
998d763cef2SBarry Smith 
9994a2ae208SSatish Balay #undef __FUNCT__
10004a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1001d763cef2SBarry Smith /*@
1002d763cef2SBarry Smith    TSGetTimeStepNumber - Gets the current number of timesteps.
1003d763cef2SBarry Smith 
1004d763cef2SBarry Smith    Not Collective
1005d763cef2SBarry Smith 
1006d763cef2SBarry Smith    Input Parameter:
1007d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1008d763cef2SBarry Smith 
1009d763cef2SBarry Smith    Output Parameter:
1010d763cef2SBarry Smith .  iter - number steps so far
1011d763cef2SBarry Smith 
1012d763cef2SBarry Smith    Level: intermediate
1013d763cef2SBarry Smith 
1014d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1015d763cef2SBarry Smith @*/
10167087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
1017d763cef2SBarry Smith {
1018d763cef2SBarry Smith   PetscFunctionBegin;
10190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10204482741eSBarry Smith   PetscValidIntPointer(iter,2);
1021d763cef2SBarry Smith   *iter = ts->steps;
1022d763cef2SBarry Smith   PetscFunctionReturn(0);
1023d763cef2SBarry Smith }
1024d763cef2SBarry Smith 
10254a2ae208SSatish Balay #undef __FUNCT__
10264a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1027d763cef2SBarry Smith /*@
1028d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1029d763cef2SBarry Smith    as well as the initial time.
1030d763cef2SBarry Smith 
10313f9fe445SBarry Smith    Logically Collective on TS
1032d763cef2SBarry Smith 
1033d763cef2SBarry Smith    Input Parameters:
1034d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1035d763cef2SBarry Smith .  initial_time - the initial time
1036d763cef2SBarry Smith -  time_step - the size of the timestep
1037d763cef2SBarry Smith 
1038d763cef2SBarry Smith    Level: intermediate
1039d763cef2SBarry Smith 
1040d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1041d763cef2SBarry Smith 
1042d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1043d763cef2SBarry Smith @*/
10447087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1045d763cef2SBarry Smith {
1046e144a568SJed Brown   PetscErrorCode ierr;
1047e144a568SJed Brown 
1048d763cef2SBarry Smith   PetscFunctionBegin;
10490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1050e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1051d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1052d763cef2SBarry Smith   PetscFunctionReturn(0);
1053d763cef2SBarry Smith }
1054d763cef2SBarry Smith 
10554a2ae208SSatish Balay #undef __FUNCT__
10564a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1057d763cef2SBarry Smith /*@
1058d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1059d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1060d763cef2SBarry Smith 
10613f9fe445SBarry Smith    Logically Collective on TS
1062d763cef2SBarry Smith 
1063d763cef2SBarry Smith    Input Parameters:
1064d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1065d763cef2SBarry Smith -  time_step - the size of the timestep
1066d763cef2SBarry Smith 
1067d763cef2SBarry Smith    Level: intermediate
1068d763cef2SBarry Smith 
1069d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1070d763cef2SBarry Smith 
1071d763cef2SBarry Smith .keywords: TS, set, timestep
1072d763cef2SBarry Smith @*/
10737087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1074d763cef2SBarry Smith {
1075d763cef2SBarry Smith   PetscFunctionBegin;
10760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1077c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1078d763cef2SBarry Smith   ts->time_step = time_step;
1079d763cef2SBarry Smith   PetscFunctionReturn(0);
1080d763cef2SBarry Smith }
1081d763cef2SBarry Smith 
10824a2ae208SSatish Balay #undef __FUNCT__
1083a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1084a43b19c4SJed Brown /*@
1085a43b19c4SJed Brown    TSSetExactFinalTime - Determines whether to interpolate solution to the
1086a43b19c4SJed Brown       exact final time requested by the user or just returns it at the final time
1087a43b19c4SJed Brown       it computed.
1088a43b19c4SJed Brown 
1089a43b19c4SJed Brown   Logically Collective on TS
1090a43b19c4SJed Brown 
1091a43b19c4SJed Brown    Input Parameter:
1092a43b19c4SJed Brown +   ts - the time-step context
1093a43b19c4SJed Brown -   ft - PETSC_TRUE if interpolates, else PETSC_FALSE
1094a43b19c4SJed Brown 
1095a43b19c4SJed Brown    Level: beginner
1096a43b19c4SJed Brown 
1097a43b19c4SJed Brown .seealso: TSSetDuration()
1098a43b19c4SJed Brown @*/
1099a43b19c4SJed Brown PetscErrorCode  TSSetExactFinalTime(TS ts,PetscBool flg)
1100a43b19c4SJed Brown {
1101a43b19c4SJed Brown 
1102a43b19c4SJed Brown   PetscFunctionBegin;
1103a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1104d8cd7023SBarry Smith   PetscValidLogicalCollectiveBool(ts,flg,2);
1105a43b19c4SJed Brown   ts->exact_final_time = flg;
1106a43b19c4SJed Brown   PetscFunctionReturn(0);
1107a43b19c4SJed Brown }
1108a43b19c4SJed Brown 
1109a43b19c4SJed Brown #undef __FUNCT__
11104a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1111d763cef2SBarry Smith /*@
1112d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1113d763cef2SBarry Smith 
1114d763cef2SBarry Smith    Not Collective
1115d763cef2SBarry Smith 
1116d763cef2SBarry Smith    Input Parameter:
1117d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1118d763cef2SBarry Smith 
1119d763cef2SBarry Smith    Output Parameter:
1120d763cef2SBarry Smith .  dt - the current timestep size
1121d763cef2SBarry Smith 
1122d763cef2SBarry Smith    Level: intermediate
1123d763cef2SBarry Smith 
1124d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1125d763cef2SBarry Smith 
1126d763cef2SBarry Smith .keywords: TS, get, timestep
1127d763cef2SBarry Smith @*/
11287087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1129d763cef2SBarry Smith {
1130d763cef2SBarry Smith   PetscFunctionBegin;
11310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1132f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1133d763cef2SBarry Smith   *dt = ts->time_step;
1134d763cef2SBarry Smith   PetscFunctionReturn(0);
1135d763cef2SBarry Smith }
1136d763cef2SBarry Smith 
11374a2ae208SSatish Balay #undef __FUNCT__
11384a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1139d8e5e3e6SSatish Balay /*@
1140d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1141d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1142d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1143d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1144d763cef2SBarry Smith 
1145d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1146d763cef2SBarry Smith 
1147d763cef2SBarry Smith    Input Parameter:
1148d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1149d763cef2SBarry Smith 
1150d763cef2SBarry Smith    Output Parameter:
1151d763cef2SBarry Smith .  v - the vector containing the solution
1152d763cef2SBarry Smith 
1153d763cef2SBarry Smith    Level: intermediate
1154d763cef2SBarry Smith 
1155d763cef2SBarry Smith .seealso: TSGetTimeStep()
1156d763cef2SBarry Smith 
1157d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1158d763cef2SBarry Smith @*/
11597087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1160d763cef2SBarry Smith {
1161d763cef2SBarry Smith   PetscFunctionBegin;
11620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11634482741eSBarry Smith   PetscValidPointer(v,2);
11648737fe31SLisandro Dalcin   *v = ts->vec_sol;
1165d763cef2SBarry Smith   PetscFunctionReturn(0);
1166d763cef2SBarry Smith }
1167d763cef2SBarry Smith 
1168bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
11694a2ae208SSatish Balay #undef __FUNCT__
1170bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1171d8e5e3e6SSatish Balay /*@
1172bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1173d763cef2SBarry Smith 
1174bdad233fSMatthew Knepley   Not collective
1175d763cef2SBarry Smith 
1176bdad233fSMatthew Knepley   Input Parameters:
1177bdad233fSMatthew Knepley + ts   - The TS
1178bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1179d763cef2SBarry Smith .vb
1180d763cef2SBarry Smith          U_t = A U
1181d763cef2SBarry Smith          U_t = A(t) U
1182d763cef2SBarry Smith          U_t = F(t,U)
1183d763cef2SBarry Smith .ve
1184d763cef2SBarry Smith 
1185d763cef2SBarry Smith    Level: beginner
1186d763cef2SBarry Smith 
1187bdad233fSMatthew Knepley .keywords: TS, problem type
1188bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1189d763cef2SBarry Smith @*/
11907087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1191a7cc72afSBarry Smith {
11929e2a6581SJed Brown   PetscErrorCode ierr;
11939e2a6581SJed Brown 
1194d763cef2SBarry Smith   PetscFunctionBegin;
11950700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1196bdad233fSMatthew Knepley   ts->problem_type = type;
11979e2a6581SJed Brown   if (type == TS_LINEAR) {
11989e2a6581SJed Brown     SNES snes;
11999e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12009e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
12019e2a6581SJed Brown   }
1202d763cef2SBarry Smith   PetscFunctionReturn(0);
1203d763cef2SBarry Smith }
1204d763cef2SBarry Smith 
1205bdad233fSMatthew Knepley #undef __FUNCT__
1206bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1207bdad233fSMatthew Knepley /*@C
1208bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1209bdad233fSMatthew Knepley 
1210bdad233fSMatthew Knepley   Not collective
1211bdad233fSMatthew Knepley 
1212bdad233fSMatthew Knepley   Input Parameter:
1213bdad233fSMatthew Knepley . ts   - The TS
1214bdad233fSMatthew Knepley 
1215bdad233fSMatthew Knepley   Output Parameter:
1216bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1217bdad233fSMatthew Knepley .vb
1218089b2837SJed Brown          M U_t = A U
1219089b2837SJed Brown          M(t) U_t = A(t) U
1220bdad233fSMatthew Knepley          U_t = F(t,U)
1221bdad233fSMatthew Knepley .ve
1222bdad233fSMatthew Knepley 
1223bdad233fSMatthew Knepley    Level: beginner
1224bdad233fSMatthew Knepley 
1225bdad233fSMatthew Knepley .keywords: TS, problem type
1226bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1227bdad233fSMatthew Knepley @*/
12287087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1229a7cc72afSBarry Smith {
1230bdad233fSMatthew Knepley   PetscFunctionBegin;
12310700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
12324482741eSBarry Smith   PetscValidIntPointer(type,2);
1233bdad233fSMatthew Knepley   *type = ts->problem_type;
1234bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1235bdad233fSMatthew Knepley }
1236d763cef2SBarry Smith 
12374a2ae208SSatish Balay #undef __FUNCT__
12384a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1239d763cef2SBarry Smith /*@
1240d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1241d763cef2SBarry Smith    of a timestepper.
1242d763cef2SBarry Smith 
1243d763cef2SBarry Smith    Collective on TS
1244d763cef2SBarry Smith 
1245d763cef2SBarry Smith    Input Parameter:
1246d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1247d763cef2SBarry Smith 
1248d763cef2SBarry Smith    Notes:
1249d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1250d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1251d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1252d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1253d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1254d763cef2SBarry Smith 
1255d763cef2SBarry Smith    Level: advanced
1256d763cef2SBarry Smith 
1257d763cef2SBarry Smith .keywords: TS, timestep, setup
1258d763cef2SBarry Smith 
1259d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1260d763cef2SBarry Smith @*/
12617087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1262d763cef2SBarry Smith {
1263dfbe8321SBarry Smith   PetscErrorCode ierr;
1264d763cef2SBarry Smith 
1265d763cef2SBarry Smith   PetscFunctionBegin;
12660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1267277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1268277b19d0SLisandro Dalcin 
12697adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
12709596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1271d763cef2SBarry Smith   }
1272a43b19c4SJed Brown   if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE;
1273277b19d0SLisandro Dalcin 
1274277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1275277b19d0SLisandro Dalcin 
12761c3436cfSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
12771c3436cfSJed Brown 
1278277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1279000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1280277b19d0SLisandro Dalcin   }
1281277b19d0SLisandro Dalcin 
1282277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1283277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1284277b19d0SLisandro Dalcin }
1285277b19d0SLisandro Dalcin 
1286277b19d0SLisandro Dalcin #undef __FUNCT__
1287277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1288277b19d0SLisandro Dalcin /*@
1289277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1290277b19d0SLisandro Dalcin 
1291277b19d0SLisandro Dalcin    Collective on TS
1292277b19d0SLisandro Dalcin 
1293277b19d0SLisandro Dalcin    Input Parameter:
1294277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1295277b19d0SLisandro Dalcin 
1296277b19d0SLisandro Dalcin    Level: beginner
1297277b19d0SLisandro Dalcin 
1298277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1299277b19d0SLisandro Dalcin 
1300277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1301277b19d0SLisandro Dalcin @*/
1302277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1303277b19d0SLisandro Dalcin {
1304277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1305277b19d0SLisandro Dalcin 
1306277b19d0SLisandro Dalcin   PetscFunctionBegin;
1307277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1308277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1309277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1310277b19d0SLisandro Dalcin   }
1311277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
13124e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
13134e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1314214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
13156bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1316e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1317e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
131838637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1319277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1320d763cef2SBarry Smith   PetscFunctionReturn(0);
1321d763cef2SBarry Smith }
1322d763cef2SBarry Smith 
13234a2ae208SSatish Balay #undef __FUNCT__
13244a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1325d8e5e3e6SSatish Balay /*@
1326d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1327d763cef2SBarry Smith    with TSCreate().
1328d763cef2SBarry Smith 
1329d763cef2SBarry Smith    Collective on TS
1330d763cef2SBarry Smith 
1331d763cef2SBarry Smith    Input Parameter:
1332d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1333d763cef2SBarry Smith 
1334d763cef2SBarry Smith    Level: beginner
1335d763cef2SBarry Smith 
1336d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1337d763cef2SBarry Smith 
1338d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1339d763cef2SBarry Smith @*/
13406bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1341d763cef2SBarry Smith {
13426849ba73SBarry Smith   PetscErrorCode ierr;
1343d763cef2SBarry Smith 
1344d763cef2SBarry Smith   PetscFunctionBegin;
13456bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
13466bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
13476bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1348d763cef2SBarry Smith 
13496bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1350277b19d0SLisandro Dalcin 
1351be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
13526bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
13536bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
13546d4c513bSLisandro Dalcin 
135584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
13566bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
13576bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
13586bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
13596d4c513bSLisandro Dalcin 
1360a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1361d763cef2SBarry Smith   PetscFunctionReturn(0);
1362d763cef2SBarry Smith }
1363d763cef2SBarry Smith 
13644a2ae208SSatish Balay #undef __FUNCT__
13654a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1366d8e5e3e6SSatish Balay /*@
1367d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1368d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1369d763cef2SBarry Smith 
1370d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1371d763cef2SBarry Smith 
1372d763cef2SBarry Smith    Input Parameter:
1373d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1374d763cef2SBarry Smith 
1375d763cef2SBarry Smith    Output Parameter:
1376d763cef2SBarry Smith .  snes - the nonlinear solver context
1377d763cef2SBarry Smith 
1378d763cef2SBarry Smith    Notes:
1379d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1380d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
138194b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1382d763cef2SBarry Smith 
1383d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1384d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1385d763cef2SBarry Smith 
1386d763cef2SBarry Smith    Level: beginner
1387d763cef2SBarry Smith 
1388d763cef2SBarry Smith .keywords: timestep, get, SNES
1389d763cef2SBarry Smith @*/
13907087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1391d763cef2SBarry Smith {
1392d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1393d372ba47SLisandro Dalcin 
1394d763cef2SBarry Smith   PetscFunctionBegin;
13950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13964482741eSBarry Smith   PetscValidPointer(snes,2);
1397d372ba47SLisandro Dalcin   if (!ts->snes) {
1398d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
1399d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1400d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1401496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
14029e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
14039e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
14049e2a6581SJed Brown     }
1405d372ba47SLisandro Dalcin   }
1406d763cef2SBarry Smith   *snes = ts->snes;
1407d763cef2SBarry Smith   PetscFunctionReturn(0);
1408d763cef2SBarry Smith }
1409d763cef2SBarry Smith 
14104a2ae208SSatish Balay #undef __FUNCT__
141194b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1412d8e5e3e6SSatish Balay /*@
141394b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1414d763cef2SBarry Smith    a TS (timestepper) context.
1415d763cef2SBarry Smith 
141694b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1417d763cef2SBarry Smith 
1418d763cef2SBarry Smith    Input Parameter:
1419d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1420d763cef2SBarry Smith 
1421d763cef2SBarry Smith    Output Parameter:
142294b7f48cSBarry Smith .  ksp - the nonlinear solver context
1423d763cef2SBarry Smith 
1424d763cef2SBarry Smith    Notes:
142594b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1426d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1427d763cef2SBarry Smith    KSP and PC contexts as well.
1428d763cef2SBarry Smith 
142994b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
143094b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1431d763cef2SBarry Smith 
1432d763cef2SBarry Smith    Level: beginner
1433d763cef2SBarry Smith 
143494b7f48cSBarry Smith .keywords: timestep, get, KSP
1435d763cef2SBarry Smith @*/
14367087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1437d763cef2SBarry Smith {
1438d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1439089b2837SJed Brown   SNES           snes;
1440d372ba47SLisandro Dalcin 
1441d763cef2SBarry Smith   PetscFunctionBegin;
14420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14434482741eSBarry Smith   PetscValidPointer(ksp,2);
144417186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1445e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1446089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1447089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1448d763cef2SBarry Smith   PetscFunctionReturn(0);
1449d763cef2SBarry Smith }
1450d763cef2SBarry Smith 
1451d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1452d763cef2SBarry Smith 
14534a2ae208SSatish Balay #undef __FUNCT__
1454adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1455adb62b0dSMatthew Knepley /*@
1456adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1457adb62b0dSMatthew Knepley    maximum time for iteration.
1458adb62b0dSMatthew Knepley 
14593f9fe445SBarry Smith    Not Collective
1460adb62b0dSMatthew Knepley 
1461adb62b0dSMatthew Knepley    Input Parameters:
1462adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1463adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1464adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1465adb62b0dSMatthew Knepley 
1466adb62b0dSMatthew Knepley    Level: intermediate
1467adb62b0dSMatthew Knepley 
1468adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1469adb62b0dSMatthew Knepley @*/
14707087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1471adb62b0dSMatthew Knepley {
1472adb62b0dSMatthew Knepley   PetscFunctionBegin;
14730700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1474abc0a331SBarry Smith   if (maxsteps) {
14754482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1476adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1477adb62b0dSMatthew Knepley   }
1478abc0a331SBarry Smith   if (maxtime) {
14794482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1480adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1481adb62b0dSMatthew Knepley   }
1482adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1483adb62b0dSMatthew Knepley }
1484adb62b0dSMatthew Knepley 
1485adb62b0dSMatthew Knepley #undef __FUNCT__
14864a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1487d763cef2SBarry Smith /*@
1488d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1489d763cef2SBarry Smith    maximum time for iteration.
1490d763cef2SBarry Smith 
14913f9fe445SBarry Smith    Logically Collective on TS
1492d763cef2SBarry Smith 
1493d763cef2SBarry Smith    Input Parameters:
1494d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1495d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1496d763cef2SBarry Smith -  maxtime - final time to iterate to
1497d763cef2SBarry Smith 
1498d763cef2SBarry Smith    Options Database Keys:
1499d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
15003bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1501d763cef2SBarry Smith 
1502d763cef2SBarry Smith    Notes:
1503d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1504d763cef2SBarry Smith 
1505d763cef2SBarry Smith    Level: intermediate
1506d763cef2SBarry Smith 
1507d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1508a43b19c4SJed Brown 
1509a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1510d763cef2SBarry Smith @*/
15117087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1512d763cef2SBarry Smith {
1513d763cef2SBarry Smith   PetscFunctionBegin;
15140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1515c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1516c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
151739b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
151839b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1519d763cef2SBarry Smith   PetscFunctionReturn(0);
1520d763cef2SBarry Smith }
1521d763cef2SBarry Smith 
15224a2ae208SSatish Balay #undef __FUNCT__
15234a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1524d763cef2SBarry Smith /*@
1525d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1526d763cef2SBarry Smith    for use by the TS routines.
1527d763cef2SBarry Smith 
15283f9fe445SBarry Smith    Logically Collective on TS and Vec
1529d763cef2SBarry Smith 
1530d763cef2SBarry Smith    Input Parameters:
1531d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1532d763cef2SBarry Smith -  x - the solution vector
1533d763cef2SBarry Smith 
1534d763cef2SBarry Smith    Level: beginner
1535d763cef2SBarry Smith 
1536d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1537d763cef2SBarry Smith @*/
15387087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1539d763cef2SBarry Smith {
15408737fe31SLisandro Dalcin   PetscErrorCode ierr;
1541496e6a7aSJed Brown   DM             dm;
15428737fe31SLisandro Dalcin 
1543d763cef2SBarry Smith   PetscFunctionBegin;
15440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15450700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
15468737fe31SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
15476bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
15488737fe31SLisandro Dalcin   ts->vec_sol = x;
1549496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1550496e6a7aSJed Brown   ierr = DMShellSetGlobalVector(dm,x);CHKERRQ(ierr);
1551d763cef2SBarry Smith   PetscFunctionReturn(0);
1552d763cef2SBarry Smith }
1553d763cef2SBarry Smith 
1554e74ef692SMatthew Knepley #undef __FUNCT__
1555e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1556ac226902SBarry Smith /*@C
1557000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
15583f2090d5SJed Brown   called once at the beginning of each time step.
1559000e7ae3SMatthew Knepley 
15603f9fe445SBarry Smith   Logically Collective on TS
1561000e7ae3SMatthew Knepley 
1562000e7ae3SMatthew Knepley   Input Parameters:
1563000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1564000e7ae3SMatthew Knepley - func - The function
1565000e7ae3SMatthew Knepley 
1566000e7ae3SMatthew Knepley   Calling sequence of func:
1567000e7ae3SMatthew Knepley . func (TS ts);
1568000e7ae3SMatthew Knepley 
1569000e7ae3SMatthew Knepley   Level: intermediate
1570000e7ae3SMatthew Knepley 
1571000e7ae3SMatthew Knepley .keywords: TS, timestep
1572000e7ae3SMatthew Knepley @*/
15737087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1574000e7ae3SMatthew Knepley {
1575000e7ae3SMatthew Knepley   PetscFunctionBegin;
15760700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1577000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1578000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1579000e7ae3SMatthew Knepley }
1580000e7ae3SMatthew Knepley 
1581e74ef692SMatthew Knepley #undef __FUNCT__
15823f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
158309ee8438SJed Brown /*@
15843f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
15853f2090d5SJed Brown 
15863f2090d5SJed Brown   Collective on TS
15873f2090d5SJed Brown 
15883f2090d5SJed Brown   Input Parameters:
15893f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
15903f2090d5SJed Brown 
15913f2090d5SJed Brown   Notes:
15923f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
15933f2090d5SJed Brown   so most users would not generally call this routine themselves.
15943f2090d5SJed Brown 
15953f2090d5SJed Brown   Level: developer
15963f2090d5SJed Brown 
15973f2090d5SJed Brown .keywords: TS, timestep
15983f2090d5SJed Brown @*/
15997087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
16003f2090d5SJed Brown {
16013f2090d5SJed Brown   PetscErrorCode ierr;
16023f2090d5SJed Brown 
16033f2090d5SJed Brown   PetscFunctionBegin;
16040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
160572ac3e02SJed Brown   if (ts->ops->prestep) {
16063f2090d5SJed Brown     PetscStackPush("TS PreStep function");
16073f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
16083f2090d5SJed Brown     PetscStackPop;
1609312ce896SJed Brown   }
16103f2090d5SJed Brown   PetscFunctionReturn(0);
16113f2090d5SJed Brown }
16123f2090d5SJed Brown 
16133f2090d5SJed Brown #undef __FUNCT__
1614e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1615ac226902SBarry Smith /*@C
1616000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
16173f2090d5SJed Brown   called once at the end of each time step.
1618000e7ae3SMatthew Knepley 
16193f9fe445SBarry Smith   Logically Collective on TS
1620000e7ae3SMatthew Knepley 
1621000e7ae3SMatthew Knepley   Input Parameters:
1622000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1623000e7ae3SMatthew Knepley - func - The function
1624000e7ae3SMatthew Knepley 
1625000e7ae3SMatthew Knepley   Calling sequence of func:
1626000e7ae3SMatthew Knepley . func (TS ts);
1627000e7ae3SMatthew Knepley 
1628000e7ae3SMatthew Knepley   Level: intermediate
1629000e7ae3SMatthew Knepley 
1630000e7ae3SMatthew Knepley .keywords: TS, timestep
1631000e7ae3SMatthew Knepley @*/
16327087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1633000e7ae3SMatthew Knepley {
1634000e7ae3SMatthew Knepley   PetscFunctionBegin;
16350700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1636000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1637000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1638000e7ae3SMatthew Knepley }
1639000e7ae3SMatthew Knepley 
1640e74ef692SMatthew Knepley #undef __FUNCT__
16413f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
164209ee8438SJed Brown /*@
16433f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
16443f2090d5SJed Brown 
16453f2090d5SJed Brown   Collective on TS
16463f2090d5SJed Brown 
16473f2090d5SJed Brown   Input Parameters:
16483f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
16493f2090d5SJed Brown 
16503f2090d5SJed Brown   Notes:
16513f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
16523f2090d5SJed Brown   so most users would not generally call this routine themselves.
16533f2090d5SJed Brown 
16543f2090d5SJed Brown   Level: developer
16553f2090d5SJed Brown 
16563f2090d5SJed Brown .keywords: TS, timestep
16573f2090d5SJed Brown @*/
16587087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
16593f2090d5SJed Brown {
16603f2090d5SJed Brown   PetscErrorCode ierr;
16613f2090d5SJed Brown 
16623f2090d5SJed Brown   PetscFunctionBegin;
16630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
166472ac3e02SJed Brown   if (ts->ops->poststep) {
16653f2090d5SJed Brown     PetscStackPush("TS PostStep function");
16663f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
16673f2090d5SJed Brown     PetscStackPop;
166872ac3e02SJed Brown   }
16693f2090d5SJed Brown   PetscFunctionReturn(0);
16703f2090d5SJed Brown }
16713f2090d5SJed Brown 
1672d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1673d763cef2SBarry Smith 
16744a2ae208SSatish Balay #undef __FUNCT__
1675a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1676d763cef2SBarry Smith /*@C
1677a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1678d763cef2SBarry Smith    timestep to display the iteration's  progress.
1679d763cef2SBarry Smith 
16803f9fe445SBarry Smith    Logically Collective on TS
1681d763cef2SBarry Smith 
1682d763cef2SBarry Smith    Input Parameters:
1683d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1684e213d8f1SJed Brown .  monitor - monitoring routine
1685329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1686b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1687b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1688b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1689d763cef2SBarry Smith 
1690e213d8f1SJed Brown    Calling sequence of monitor:
1691e213d8f1SJed Brown $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1692d763cef2SBarry Smith 
1693d763cef2SBarry Smith +    ts - the TS context
1694d763cef2SBarry Smith .    steps - iteration number
16951f06c33eSBarry Smith .    time - current time
1696d763cef2SBarry Smith .    x - current iterate
1697d763cef2SBarry Smith -    mctx - [optional] monitoring context
1698d763cef2SBarry Smith 
1699d763cef2SBarry Smith    Notes:
1700d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1701d763cef2SBarry Smith    already has been loaded.
1702d763cef2SBarry Smith 
1703025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1704025f1a04SBarry Smith 
1705d763cef2SBarry Smith    Level: intermediate
1706d763cef2SBarry Smith 
1707d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1708d763cef2SBarry Smith 
1709a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1710d763cef2SBarry Smith @*/
1711c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1712d763cef2SBarry Smith {
1713d763cef2SBarry Smith   PetscFunctionBegin;
17140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
171517186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1716d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1717329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1718d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1719d763cef2SBarry Smith   PetscFunctionReturn(0);
1720d763cef2SBarry Smith }
1721d763cef2SBarry Smith 
17224a2ae208SSatish Balay #undef __FUNCT__
1723a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1724d763cef2SBarry Smith /*@C
1725a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1726d763cef2SBarry Smith 
17273f9fe445SBarry Smith    Logically Collective on TS
1728d763cef2SBarry Smith 
1729d763cef2SBarry Smith    Input Parameters:
1730d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1731d763cef2SBarry Smith 
1732d763cef2SBarry Smith    Notes:
1733d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1734d763cef2SBarry Smith 
1735d763cef2SBarry Smith    Level: intermediate
1736d763cef2SBarry Smith 
1737d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1738d763cef2SBarry Smith 
1739a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1740d763cef2SBarry Smith @*/
17417087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1742d763cef2SBarry Smith {
1743d952e501SBarry Smith   PetscErrorCode ierr;
1744d952e501SBarry Smith   PetscInt       i;
1745d952e501SBarry Smith 
1746d763cef2SBarry Smith   PetscFunctionBegin;
17470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1748d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
1749d952e501SBarry Smith     if (ts->mdestroy[i]) {
17503c4aec1bSBarry Smith       ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
1751d952e501SBarry Smith     }
1752d952e501SBarry Smith   }
1753d763cef2SBarry Smith   ts->numbermonitors = 0;
1754d763cef2SBarry Smith   PetscFunctionReturn(0);
1755d763cef2SBarry Smith }
1756d763cef2SBarry Smith 
17574a2ae208SSatish Balay #undef __FUNCT__
1758a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
1759d8e5e3e6SSatish Balay /*@
1760a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
17615516499fSSatish Balay 
17625516499fSSatish Balay    Level: intermediate
176341251cbbSSatish Balay 
17645516499fSSatish Balay .keywords: TS, set, monitor
17655516499fSSatish Balay 
176641251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
176741251cbbSSatish Balay @*/
1768649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
1769d763cef2SBarry Smith {
1770dfbe8321SBarry Smith   PetscErrorCode ierr;
1771649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
1772d132466eSBarry Smith 
1773d763cef2SBarry Smith   PetscFunctionBegin;
1774649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1775971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
1776649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1777d763cef2SBarry Smith   PetscFunctionReturn(0);
1778d763cef2SBarry Smith }
1779d763cef2SBarry Smith 
17804a2ae208SSatish Balay #undef __FUNCT__
1781cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
1782cd652676SJed Brown /*@
1783cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
1784cd652676SJed Brown 
1785cd652676SJed Brown    Logically Collective on TS
1786cd652676SJed Brown 
1787cd652676SJed Brown    Input Argument:
1788cd652676SJed Brown .  ts - time stepping context
1789cd652676SJed Brown 
1790cd652676SJed Brown    Output Argument:
1791cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
1792cd652676SJed Brown 
1793cd652676SJed Brown    Level: intermediate
1794cd652676SJed Brown 
1795cd652676SJed Brown .keywords: TS, set
1796cd652676SJed Brown 
1797cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
1798cd652676SJed Brown @*/
1799cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
1800cd652676SJed Brown {
1801cd652676SJed Brown 
1802cd652676SJed Brown   PetscFunctionBegin;
1803cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1804cd652676SJed Brown   ts->retain_stages = flg;
1805cd652676SJed Brown   PetscFunctionReturn(0);
1806cd652676SJed Brown }
1807cd652676SJed Brown 
1808cd652676SJed Brown #undef __FUNCT__
1809cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
1810cd652676SJed Brown /*@
1811cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
1812cd652676SJed Brown 
1813cd652676SJed Brown    Collective on TS
1814cd652676SJed Brown 
1815cd652676SJed Brown    Input Argument:
1816cd652676SJed Brown +  ts - time stepping context
1817cd652676SJed Brown -  t - time to interpolate to
1818cd652676SJed Brown 
1819cd652676SJed Brown    Output Argument:
1820cd652676SJed Brown .  X - state at given time
1821cd652676SJed Brown 
1822cd652676SJed Brown    Notes:
1823cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
1824cd652676SJed Brown 
1825cd652676SJed Brown    Level: intermediate
1826cd652676SJed Brown 
1827cd652676SJed Brown    Developer Notes:
1828cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
1829cd652676SJed Brown 
1830cd652676SJed Brown .keywords: TS, set
1831cd652676SJed Brown 
1832cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
1833cd652676SJed Brown @*/
1834cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X)
1835cd652676SJed Brown {
1836cd652676SJed Brown   PetscErrorCode ierr;
1837cd652676SJed Brown 
1838cd652676SJed Brown   PetscFunctionBegin;
1839cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1840d8cd7023SBarry Smith   if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step_prev,ts->ptime);
1841cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
1842cd652676SJed Brown   ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr);
1843cd652676SJed Brown   PetscFunctionReturn(0);
1844cd652676SJed Brown }
1845cd652676SJed Brown 
1846cd652676SJed Brown #undef __FUNCT__
18474a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1848d763cef2SBarry Smith /*@
18496d9e5789SSean Farley    TSStep - Steps one time step
1850d763cef2SBarry Smith 
1851d763cef2SBarry Smith    Collective on TS
1852d763cef2SBarry Smith 
1853d763cef2SBarry Smith    Input Parameter:
1854d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1855d763cef2SBarry Smith 
18566d9e5789SSean Farley    Level: intermediate
1857d763cef2SBarry Smith 
1858d763cef2SBarry Smith .keywords: TS, timestep, solve
1859d763cef2SBarry Smith 
18606d9e5789SSean Farley .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve()
1861d763cef2SBarry Smith @*/
1862193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
1863d763cef2SBarry Smith {
1864362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
1865dfbe8321SBarry Smith   PetscErrorCode ierr;
1866d763cef2SBarry Smith 
1867d763cef2SBarry Smith   PetscFunctionBegin;
18680700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1869d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
1870d405a339SMatthew Knepley 
1871362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
1872362cd11cSLisandro Dalcin 
1873362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
1874d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
1875193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
1876d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
1877362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
1878362cd11cSLisandro Dalcin 
1879362cd11cSLisandro Dalcin   if (ts->reason < 0) {
1880cef5090cSJed Brown     if (ts->errorifstepfailed) {
1881cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
1882cef5090cSJed Brown         SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
1883cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
1884cef5090cSJed Brown     }
1885362cd11cSLisandro Dalcin   } else if (!ts->reason) {
1886362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
1887362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
1888362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
1889362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
1890362cd11cSLisandro Dalcin   }
1891362cd11cSLisandro Dalcin 
1892d763cef2SBarry Smith   PetscFunctionReturn(0);
1893d763cef2SBarry Smith }
1894d763cef2SBarry Smith 
18954a2ae208SSatish Balay #undef __FUNCT__
189605175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
189705175c85SJed Brown /*@
189805175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
189905175c85SJed Brown 
19001c3436cfSJed Brown    Collective on TS
190105175c85SJed Brown 
190205175c85SJed Brown    Input Arguments:
19031c3436cfSJed Brown +  ts - time stepping context
19041c3436cfSJed Brown .  order - desired order of accuracy
19051c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
190605175c85SJed Brown 
190705175c85SJed Brown    Output Arguments:
19081c3436cfSJed Brown .  X - state at the end of the current step
190905175c85SJed Brown 
191005175c85SJed Brown    Level: advanced
191105175c85SJed Brown 
1912108c343cSJed Brown    Notes:
1913108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
1914108c343cSJed 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.
1915108c343cSJed Brown 
19161c3436cfSJed Brown .seealso: TSStep(), TSAdapt
191705175c85SJed Brown @*/
19181c3436cfSJed Brown PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec X,PetscBool *done)
191905175c85SJed Brown {
192005175c85SJed Brown   PetscErrorCode ierr;
192105175c85SJed Brown 
192205175c85SJed Brown   PetscFunctionBegin;
192305175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
192405175c85SJed Brown   PetscValidType(ts,1);
192505175c85SJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
19261c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
19271c3436cfSJed Brown   ierr = (*ts->ops->evaluatestep)(ts,order,X,done);CHKERRQ(ierr);
192805175c85SJed Brown   PetscFunctionReturn(0);
192905175c85SJed Brown }
193005175c85SJed Brown 
193105175c85SJed Brown #undef __FUNCT__
19326a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
19336a4d4014SLisandro Dalcin /*@
19346a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
19356a4d4014SLisandro Dalcin 
19366a4d4014SLisandro Dalcin    Collective on TS
19376a4d4014SLisandro Dalcin 
19386a4d4014SLisandro Dalcin    Input Parameter:
19396a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1940362cd11cSLisandro Dalcin -  x - the solution vector
19416a4d4014SLisandro Dalcin 
19425a3a76d0SJed Brown    Output Parameter:
19435a3a76d0SJed Brown .  ftime - time of the state vector x upon completion
19445a3a76d0SJed Brown 
19456a4d4014SLisandro Dalcin    Level: beginner
19466a4d4014SLisandro Dalcin 
19475a3a76d0SJed Brown    Notes:
19485a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
19495a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
19505a3a76d0SJed Brown    stepped over the final time.
19515a3a76d0SJed Brown 
19526a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
19536a4d4014SLisandro Dalcin 
19546a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
19556a4d4014SLisandro Dalcin @*/
19565a3a76d0SJed Brown PetscErrorCode TSSolve(TS ts,Vec x,PetscReal *ftime)
19576a4d4014SLisandro Dalcin {
19584d7d938eSLisandro Dalcin   PetscBool      flg;
19594d7d938eSLisandro Dalcin   char           filename[PETSC_MAX_PATH_LEN];
19604d7d938eSLisandro Dalcin   PetscViewer    viewer;
19616a4d4014SLisandro Dalcin   PetscErrorCode ierr;
1962f22f69f0SBarry Smith 
19636a4d4014SLisandro Dalcin   PetscFunctionBegin;
19640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1965193ac0bcSJed Brown   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
19665a3a76d0SJed Brown   if (ts->exact_final_time) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
19675a3a76d0SJed Brown     if (!ts->vec_sol || x == ts->vec_sol) {
19685a3a76d0SJed Brown       Vec y;
19695a3a76d0SJed Brown       ierr = VecDuplicate(x,&y);CHKERRQ(ierr);
19705a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
19715a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
19725a3a76d0SJed Brown     }
1973362cd11cSLisandro Dalcin     ierr = VecCopy(x,ts->vec_sol);CHKERRQ(ierr);
19745a3a76d0SJed Brown   } else {
1975193ac0bcSJed Brown     ierr = TSSetSolution(ts,x);CHKERRQ(ierr);
19765a3a76d0SJed Brown   }
1977b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
19786a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
1979193ac0bcSJed Brown   ts->steps = 0;
19805ef26d82SJed Brown   ts->ksp_its = 0;
19815ef26d82SJed Brown   ts->snes_its = 0;
1982c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
1983c610991cSLisandro Dalcin   ts->reject = 0;
1984193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
1985193ac0bcSJed Brown 
1986193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
1987193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
19885a3a76d0SJed Brown     ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
1989a5b82c69SSean Farley     if (ftime) *ftime = ts->ptime;
1990193ac0bcSJed Brown   } else {
19916a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
1992362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
1993362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
1994362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
1995362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
1996362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
1997e1a7a14fSJed Brown     while (!ts->reason) {
1998193ac0bcSJed Brown       ierr = TSPreStep(ts);CHKERRQ(ierr);
1999193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2000193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2001193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2002193ac0bcSJed Brown     }
2003362cd11cSLisandro Dalcin     if (ts->exact_final_time && ts->ptime > ts->max_time) {
2004a43b19c4SJed Brown       ierr = TSInterpolate(ts,ts->max_time,x);CHKERRQ(ierr);
20055a3a76d0SJed Brown       if (ftime) *ftime = ts->max_time;
20060574a7fbSJed Brown     } else {
20070574a7fbSJed Brown       ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
20080574a7fbSJed Brown       if (ftime) *ftime = ts->ptime;
20090574a7fbSJed Brown     }
2010193ac0bcSJed Brown   }
20114d7d938eSLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
20124d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
20134d7d938eSLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr);
20144d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
20154d7d938eSLisandro Dalcin     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2016193ac0bcSJed Brown   }
20176a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
20186a4d4014SLisandro Dalcin }
20196a4d4014SLisandro Dalcin 
20206a4d4014SLisandro Dalcin #undef __FUNCT__
20214a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2022228d79bcSJed Brown /*@
2023228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2024228d79bcSJed Brown 
2025228d79bcSJed Brown    Collective on TS
2026228d79bcSJed Brown 
2027228d79bcSJed Brown    Input Parameters:
2028228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2029228d79bcSJed Brown .  step - step number that has just completed
2030228d79bcSJed Brown .  ptime - model time of the state
2031228d79bcSJed Brown -  x - state at the current model time
2032228d79bcSJed Brown 
2033228d79bcSJed Brown    Notes:
2034228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2035228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2036228d79bcSJed Brown 
2037228d79bcSJed Brown    Level: advanced
2038228d79bcSJed Brown 
2039228d79bcSJed Brown .keywords: TS, timestep
2040228d79bcSJed Brown @*/
2041a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
2042d763cef2SBarry Smith {
20436849ba73SBarry Smith   PetscErrorCode ierr;
2044a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2045d763cef2SBarry Smith 
2046d763cef2SBarry Smith   PetscFunctionBegin;
2047d763cef2SBarry Smith   for (i=0; i<n; i++) {
2048142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
2049d763cef2SBarry Smith   }
2050d763cef2SBarry Smith   PetscFunctionReturn(0);
2051d763cef2SBarry Smith }
2052d763cef2SBarry Smith 
2053d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
2054d763cef2SBarry Smith 
20554a2ae208SSatish Balay #undef __FUNCT__
2056a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate"
2057d763cef2SBarry Smith /*@C
2058a6570f20SBarry Smith    TSMonitorLGCreate - Creates a line graph context for use with
2059d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
2060d763cef2SBarry Smith 
2061d763cef2SBarry Smith    Collective on TS
2062d763cef2SBarry Smith 
2063d763cef2SBarry Smith    Input Parameters:
2064d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2065d763cef2SBarry Smith .  label - the title to put in the title bar
20667c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2067d763cef2SBarry Smith -  m, n - the screen width and height in pixels
2068d763cef2SBarry Smith 
2069d763cef2SBarry Smith    Output Parameter:
2070d763cef2SBarry Smith .  draw - the drawing context
2071d763cef2SBarry Smith 
2072d763cef2SBarry Smith    Options Database Key:
2073a6570f20SBarry Smith .  -ts_monitor_draw - automatically sets line graph monitor
2074d763cef2SBarry Smith 
2075d763cef2SBarry Smith    Notes:
2076a6570f20SBarry Smith    Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy().
2077d763cef2SBarry Smith 
2078d763cef2SBarry Smith    Level: intermediate
2079d763cef2SBarry Smith 
20807c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2081d763cef2SBarry Smith 
2082a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet()
20837c922b88SBarry Smith 
2084d763cef2SBarry Smith @*/
20857087cfbeSBarry Smith PetscErrorCode  TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2086d763cef2SBarry Smith {
2087b0a32e0cSBarry Smith   PetscDraw      win;
2088dfbe8321SBarry Smith   PetscErrorCode ierr;
2089d763cef2SBarry Smith 
2090d763cef2SBarry Smith   PetscFunctionBegin;
2091b0a32e0cSBarry Smith   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
2092b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
2093b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
2094b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
2095d763cef2SBarry Smith 
209652e6d16bSBarry Smith   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
2097d763cef2SBarry Smith   PetscFunctionReturn(0);
2098d763cef2SBarry Smith }
2099d763cef2SBarry Smith 
21004a2ae208SSatish Balay #undef __FUNCT__
2101a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG"
2102a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2103d763cef2SBarry Smith {
2104b0a32e0cSBarry Smith   PetscDrawLG    lg = (PetscDrawLG) monctx;
210587828ca2SBarry Smith   PetscReal      x,y = ptime;
2106dfbe8321SBarry Smith   PetscErrorCode ierr;
2107d763cef2SBarry Smith 
2108d763cef2SBarry Smith   PetscFunctionBegin;
21097c922b88SBarry Smith   if (!monctx) {
21107c922b88SBarry Smith     MPI_Comm    comm;
2111b0a32e0cSBarry Smith     PetscViewer viewer;
21127c922b88SBarry Smith 
21137c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
2114b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
2115b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
21167c922b88SBarry Smith   }
21177c922b88SBarry Smith 
2118b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
211987828ca2SBarry Smith   x = (PetscReal)n;
2120b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2121d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
2122b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2123d763cef2SBarry Smith   }
2124d763cef2SBarry Smith   PetscFunctionReturn(0);
2125d763cef2SBarry Smith }
2126d763cef2SBarry Smith 
21274a2ae208SSatish Balay #undef __FUNCT__
2128a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy"
2129d763cef2SBarry Smith /*@C
2130a6570f20SBarry Smith    TSMonitorLGDestroy - Destroys a line graph context that was created
2131a6570f20SBarry Smith    with TSMonitorLGCreate().
2132d763cef2SBarry Smith 
2133b0a32e0cSBarry Smith    Collective on PetscDrawLG
2134d763cef2SBarry Smith 
2135d763cef2SBarry Smith    Input Parameter:
2136d763cef2SBarry Smith .  draw - the drawing context
2137d763cef2SBarry Smith 
2138d763cef2SBarry Smith    Level: intermediate
2139d763cef2SBarry Smith 
2140d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2141d763cef2SBarry Smith 
2142a6570f20SBarry Smith .seealso: TSMonitorLGCreate(),  TSMonitorSet(), TSMonitorLG();
2143d763cef2SBarry Smith @*/
21446bf464f9SBarry Smith PetscErrorCode  TSMonitorLGDestroy(PetscDrawLG *drawlg)
2145d763cef2SBarry Smith {
2146b0a32e0cSBarry Smith   PetscDraw      draw;
2147dfbe8321SBarry Smith   PetscErrorCode ierr;
2148d763cef2SBarry Smith 
2149d763cef2SBarry Smith   PetscFunctionBegin;
21506bf464f9SBarry Smith   ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr);
21516bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
2152b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
2153d763cef2SBarry Smith   PetscFunctionReturn(0);
2154d763cef2SBarry Smith }
2155d763cef2SBarry Smith 
21564a2ae208SSatish Balay #undef __FUNCT__
21574a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2158d763cef2SBarry Smith /*@
2159d763cef2SBarry Smith    TSGetTime - Gets the current time.
2160d763cef2SBarry Smith 
2161d763cef2SBarry Smith    Not Collective
2162d763cef2SBarry Smith 
2163d763cef2SBarry Smith    Input Parameter:
2164d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2165d763cef2SBarry Smith 
2166d763cef2SBarry Smith    Output Parameter:
2167d763cef2SBarry Smith .  t  - the current time
2168d763cef2SBarry Smith 
2169d763cef2SBarry Smith    Level: beginner
2170d763cef2SBarry Smith 
2171d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2172d763cef2SBarry Smith 
2173d763cef2SBarry Smith .keywords: TS, get, time
2174d763cef2SBarry Smith @*/
21757087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
2176d763cef2SBarry Smith {
2177d763cef2SBarry Smith   PetscFunctionBegin;
21780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2179f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2180d763cef2SBarry Smith   *t = ts->ptime;
2181d763cef2SBarry Smith   PetscFunctionReturn(0);
2182d763cef2SBarry Smith }
2183d763cef2SBarry Smith 
21844a2ae208SSatish Balay #undef __FUNCT__
21856a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
21866a4d4014SLisandro Dalcin /*@
21876a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
21886a4d4014SLisandro Dalcin 
21893f9fe445SBarry Smith    Logically Collective on TS
21906a4d4014SLisandro Dalcin 
21916a4d4014SLisandro Dalcin    Input Parameters:
21926a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
21936a4d4014SLisandro Dalcin -  time - the time
21946a4d4014SLisandro Dalcin 
21956a4d4014SLisandro Dalcin    Level: intermediate
21966a4d4014SLisandro Dalcin 
21976a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
21986a4d4014SLisandro Dalcin 
21996a4d4014SLisandro Dalcin .keywords: TS, set, time
22006a4d4014SLisandro Dalcin @*/
22017087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
22026a4d4014SLisandro Dalcin {
22036a4d4014SLisandro Dalcin   PetscFunctionBegin;
22040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2205c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
22066a4d4014SLisandro Dalcin   ts->ptime = t;
22076a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
22086a4d4014SLisandro Dalcin }
22096a4d4014SLisandro Dalcin 
22106a4d4014SLisandro Dalcin #undef __FUNCT__
22114a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2212d763cef2SBarry Smith /*@C
2213d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2214d763cef2SBarry Smith    TS options in the database.
2215d763cef2SBarry Smith 
22163f9fe445SBarry Smith    Logically Collective on TS
2217d763cef2SBarry Smith 
2218d763cef2SBarry Smith    Input Parameter:
2219d763cef2SBarry Smith +  ts     - The TS context
2220d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2221d763cef2SBarry Smith 
2222d763cef2SBarry Smith    Notes:
2223d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2224d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2225d763cef2SBarry Smith    hyphen.
2226d763cef2SBarry Smith 
2227d763cef2SBarry Smith    Level: advanced
2228d763cef2SBarry Smith 
2229d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2230d763cef2SBarry Smith 
2231d763cef2SBarry Smith .seealso: TSSetFromOptions()
2232d763cef2SBarry Smith 
2233d763cef2SBarry Smith @*/
22347087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2235d763cef2SBarry Smith {
2236dfbe8321SBarry Smith   PetscErrorCode ierr;
2237089b2837SJed Brown   SNES           snes;
2238d763cef2SBarry Smith 
2239d763cef2SBarry Smith   PetscFunctionBegin;
22400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2241d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2242089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2243089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2244d763cef2SBarry Smith   PetscFunctionReturn(0);
2245d763cef2SBarry Smith }
2246d763cef2SBarry Smith 
2247d763cef2SBarry Smith 
22484a2ae208SSatish Balay #undef __FUNCT__
22494a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2250d763cef2SBarry Smith /*@C
2251d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2252d763cef2SBarry Smith    TS options in the database.
2253d763cef2SBarry Smith 
22543f9fe445SBarry Smith    Logically Collective on TS
2255d763cef2SBarry Smith 
2256d763cef2SBarry Smith    Input Parameter:
2257d763cef2SBarry Smith +  ts     - The TS context
2258d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2259d763cef2SBarry Smith 
2260d763cef2SBarry Smith    Notes:
2261d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2262d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2263d763cef2SBarry Smith    hyphen.
2264d763cef2SBarry Smith 
2265d763cef2SBarry Smith    Level: advanced
2266d763cef2SBarry Smith 
2267d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2268d763cef2SBarry Smith 
2269d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2270d763cef2SBarry Smith 
2271d763cef2SBarry Smith @*/
22727087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2273d763cef2SBarry Smith {
2274dfbe8321SBarry Smith   PetscErrorCode ierr;
2275089b2837SJed Brown   SNES           snes;
2276d763cef2SBarry Smith 
2277d763cef2SBarry Smith   PetscFunctionBegin;
22780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2279d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2280089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2281089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2282d763cef2SBarry Smith   PetscFunctionReturn(0);
2283d763cef2SBarry Smith }
2284d763cef2SBarry Smith 
22854a2ae208SSatish Balay #undef __FUNCT__
22864a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2287d763cef2SBarry Smith /*@C
2288d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2289d763cef2SBarry Smith    TS options in the database.
2290d763cef2SBarry Smith 
2291d763cef2SBarry Smith    Not Collective
2292d763cef2SBarry Smith 
2293d763cef2SBarry Smith    Input Parameter:
2294d763cef2SBarry Smith .  ts - The TS context
2295d763cef2SBarry Smith 
2296d763cef2SBarry Smith    Output Parameter:
2297d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2298d763cef2SBarry Smith 
2299d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2300d763cef2SBarry Smith    sufficient length to hold the prefix.
2301d763cef2SBarry Smith 
2302d763cef2SBarry Smith    Level: intermediate
2303d763cef2SBarry Smith 
2304d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2305d763cef2SBarry Smith 
2306d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2307d763cef2SBarry Smith @*/
23087087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2309d763cef2SBarry Smith {
2310dfbe8321SBarry Smith   PetscErrorCode ierr;
2311d763cef2SBarry Smith 
2312d763cef2SBarry Smith   PetscFunctionBegin;
23130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23144482741eSBarry Smith   PetscValidPointer(prefix,2);
2315d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2316d763cef2SBarry Smith   PetscFunctionReturn(0);
2317d763cef2SBarry Smith }
2318d763cef2SBarry Smith 
23194a2ae208SSatish Balay #undef __FUNCT__
23204a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2321d763cef2SBarry Smith /*@C
2322d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2323d763cef2SBarry Smith 
2324d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2325d763cef2SBarry Smith 
2326d763cef2SBarry Smith    Input Parameter:
2327d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2328d763cef2SBarry Smith 
2329d763cef2SBarry Smith    Output Parameters:
2330d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2331d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2332089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2333d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2334d763cef2SBarry Smith 
2335d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2336d763cef2SBarry Smith 
2337d763cef2SBarry Smith    Level: intermediate
2338d763cef2SBarry Smith 
233926d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2340d763cef2SBarry Smith 
2341d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2342d763cef2SBarry Smith @*/
2343089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2344d763cef2SBarry Smith {
2345089b2837SJed Brown   PetscErrorCode ierr;
2346089b2837SJed Brown   SNES           snes;
2347*24989b8cSPeter Brune   DM             dm;
2348089b2837SJed Brown 
2349d763cef2SBarry Smith   PetscFunctionBegin;
2350089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2351089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
2352*24989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
2353*24989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2354d763cef2SBarry Smith   PetscFunctionReturn(0);
2355d763cef2SBarry Smith }
2356d763cef2SBarry Smith 
23571713a123SBarry Smith #undef __FUNCT__
23582eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
23592eca1d9cSJed Brown /*@C
23602eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
23612eca1d9cSJed Brown 
23622eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
23632eca1d9cSJed Brown 
23642eca1d9cSJed Brown    Input Parameter:
23652eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
23662eca1d9cSJed Brown 
23672eca1d9cSJed Brown    Output Parameters:
23682eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
23692eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
23702eca1d9cSJed Brown .  f   - The function to compute the matrices
23712eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
23722eca1d9cSJed Brown 
23732eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
23742eca1d9cSJed Brown 
23752eca1d9cSJed Brown    Level: advanced
23762eca1d9cSJed Brown 
23772eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
23782eca1d9cSJed Brown 
23792eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
23802eca1d9cSJed Brown @*/
23817087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
23822eca1d9cSJed Brown {
2383089b2837SJed Brown   PetscErrorCode ierr;
2384089b2837SJed Brown   SNES           snes;
2385*24989b8cSPeter Brune   DM             dm;
23862eca1d9cSJed Brown   PetscFunctionBegin;
2387089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2388089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
2389*24989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
2390*24989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
23912eca1d9cSJed Brown   PetscFunctionReturn(0);
23922eca1d9cSJed Brown }
23932eca1d9cSJed Brown 
23946083293cSBarry Smith typedef struct {
23956083293cSBarry Smith   PetscViewer viewer;
23966083293cSBarry Smith   Vec         initialsolution;
23976083293cSBarry Smith   PetscBool   showinitial;
23986083293cSBarry Smith } TSMonitorSolutionCtx;
23996083293cSBarry Smith 
24002eca1d9cSJed Brown #undef __FUNCT__
2401a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution"
24021713a123SBarry Smith /*@C
2403a6570f20SBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by calling
24041713a123SBarry Smith    VecView() for the solution at each timestep
24051713a123SBarry Smith 
24061713a123SBarry Smith    Collective on TS
24071713a123SBarry Smith 
24081713a123SBarry Smith    Input Parameters:
24091713a123SBarry Smith +  ts - the TS context
24101713a123SBarry Smith .  step - current time-step
2411142b95e3SSatish Balay .  ptime - current time
24121713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
24131713a123SBarry Smith 
24141713a123SBarry Smith    Level: intermediate
24151713a123SBarry Smith 
24161713a123SBarry Smith .keywords: TS,  vector, monitor, view
24171713a123SBarry Smith 
2418a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
24191713a123SBarry Smith @*/
24207087cfbeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
24211713a123SBarry Smith {
2422dfbe8321SBarry Smith   PetscErrorCode       ierr;
24236083293cSBarry Smith   TSMonitorSolutionCtx *ictx = (TSMonitorSolutionCtx*)dummy;
24241713a123SBarry Smith 
24251713a123SBarry Smith   PetscFunctionBegin;
24266083293cSBarry Smith   if (!step && ictx->showinitial) {
24276083293cSBarry Smith     if (!ictx->initialsolution) {
24286083293cSBarry Smith       ierr = VecDuplicate(x,&ictx->initialsolution);CHKERRQ(ierr);
24291713a123SBarry Smith     }
24306083293cSBarry Smith     ierr = VecCopy(x,ictx->initialsolution);CHKERRQ(ierr);
24316083293cSBarry Smith   }
24326083293cSBarry Smith   if (ictx->showinitial) {
24336083293cSBarry Smith     PetscReal pause;
24346083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
24356083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
24366083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
24376083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
24386083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
24396083293cSBarry Smith   }
24406083293cSBarry Smith   ierr = VecView(x,ictx->viewer);CHKERRQ(ierr);
24416083293cSBarry Smith   if (ictx->showinitial) {
24426083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
24436083293cSBarry Smith   }
24441713a123SBarry Smith   PetscFunctionReturn(0);
24451713a123SBarry Smith }
24461713a123SBarry Smith 
24471713a123SBarry Smith 
24486c699258SBarry Smith #undef __FUNCT__
24496083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionDestroy"
24506083293cSBarry Smith /*@C
24516083293cSBarry Smith    TSMonitorSolutionDestroy - Destroys the monitor context for TSMonitorSolution
24526083293cSBarry Smith 
24536083293cSBarry Smith    Collective on TS
24546083293cSBarry Smith 
24556083293cSBarry Smith    Input Parameters:
24566083293cSBarry Smith .    ctx - the monitor context
24576083293cSBarry Smith 
24586083293cSBarry Smith    Level: intermediate
24596083293cSBarry Smith 
24606083293cSBarry Smith .keywords: TS,  vector, monitor, view
24616083293cSBarry Smith 
24626083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution()
24636083293cSBarry Smith @*/
24646083293cSBarry Smith PetscErrorCode  TSMonitorSolutionDestroy(void **ctx)
24656083293cSBarry Smith {
24666083293cSBarry Smith   PetscErrorCode       ierr;
24676083293cSBarry Smith   TSMonitorSolutionCtx *ictx = *(TSMonitorSolutionCtx**)ctx;
24686083293cSBarry Smith 
24696083293cSBarry Smith   PetscFunctionBegin;
24706083293cSBarry Smith   ierr = PetscViewerDestroy(&ictx->viewer);CHKERRQ(ierr);
24716083293cSBarry Smith   ierr = VecDestroy(&ictx->initialsolution);CHKERRQ(ierr);
24726083293cSBarry Smith   ierr = PetscFree(ictx);CHKERRQ(ierr);
24736083293cSBarry Smith   PetscFunctionReturn(0);
24746083293cSBarry Smith }
24756083293cSBarry Smith 
24766083293cSBarry Smith #undef __FUNCT__
24776083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionCreate"
24786083293cSBarry Smith /*@C
24796083293cSBarry Smith    TSMonitorSolutionCreate - Creates the monitor context for TSMonitorSolution
24806083293cSBarry Smith 
24816083293cSBarry Smith    Collective on TS
24826083293cSBarry Smith 
24836083293cSBarry Smith    Input Parameter:
24846083293cSBarry Smith .    ts - time-step context
24856083293cSBarry Smith 
24866083293cSBarry Smith    Output Patameter:
24876083293cSBarry Smith .    ctx - the monitor context
24886083293cSBarry Smith 
24896083293cSBarry Smith    Level: intermediate
24906083293cSBarry Smith 
24916083293cSBarry Smith .keywords: TS,  vector, monitor, view
24926083293cSBarry Smith 
24936083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution()
24946083293cSBarry Smith @*/
24956083293cSBarry Smith PetscErrorCode  TSMonitorSolutionCreate(TS ts,PetscViewer viewer,void **ctx)
24966083293cSBarry Smith {
24976083293cSBarry Smith   PetscErrorCode       ierr;
24986083293cSBarry Smith   TSMonitorSolutionCtx *ictx;
24996083293cSBarry Smith 
25006083293cSBarry Smith   PetscFunctionBegin;
25016083293cSBarry Smith   ierr = PetscNew(TSMonitorSolutionCtx,&ictx);CHKERRQ(ierr);
25026083293cSBarry Smith   *ctx = (void*)ictx;
25036083293cSBarry Smith   if (!viewer) {
25046083293cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm);
25056083293cSBarry Smith   }
25066083293cSBarry Smith   ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr);
25076083293cSBarry Smith   ictx->viewer      = viewer;
25086083293cSBarry Smith   ictx->showinitial = PETSC_FALSE;
25096083293cSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix,"-ts_monitor_solution_initial",&ictx->showinitial,PETSC_NULL);CHKERRQ(ierr);
25106083293cSBarry Smith   PetscFunctionReturn(0);
25116083293cSBarry Smith }
25126083293cSBarry Smith 
25136083293cSBarry Smith #undef __FUNCT__
25146c699258SBarry Smith #define __FUNCT__ "TSSetDM"
25156c699258SBarry Smith /*@
25166c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
25176c699258SBarry Smith 
25183f9fe445SBarry Smith    Logically Collective on TS and DM
25196c699258SBarry Smith 
25206c699258SBarry Smith    Input Parameters:
25216c699258SBarry Smith +  ts - the preconditioner context
25226c699258SBarry Smith -  dm - the dm
25236c699258SBarry Smith 
25246c699258SBarry Smith    Level: intermediate
25256c699258SBarry Smith 
25266c699258SBarry Smith 
25276c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
25286c699258SBarry Smith @*/
25297087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
25306c699258SBarry Smith {
25316c699258SBarry Smith   PetscErrorCode ierr;
2532089b2837SJed Brown   SNES           snes;
2533*24989b8cSPeter Brune   TSDM           tsdm;
25346c699258SBarry Smith 
25356c699258SBarry Smith   PetscFunctionBegin;
25360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
253770663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
2538*24989b8cSPeter Brune   if (ts->dm) {               /* Move the TSDM context over to the new DM unless the new DM already has one */
2539*24989b8cSPeter Brune     PetscContainer oldcontainer,container;
2540*24989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)ts->dm,"TSDM",(PetscObject*)&oldcontainer);CHKERRQ(ierr);
2541*24989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)dm,"TSDM",(PetscObject*)&container);CHKERRQ(ierr);
2542*24989b8cSPeter Brune     if (oldcontainer && !container) {
2543*24989b8cSPeter Brune       ierr = DMTSCopyContext(ts->dm,dm);CHKERRQ(ierr);
2544*24989b8cSPeter Brune       ierr = DMTSGetContext(ts->dm,&tsdm);CHKERRQ(ierr);
2545*24989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
2546*24989b8cSPeter Brune         tsdm->originaldm = dm;
2547*24989b8cSPeter Brune       }
2548*24989b8cSPeter Brune     }
25496bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
2550*24989b8cSPeter Brune   }
25516c699258SBarry Smith   ts->dm = dm;
2552089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2553089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
25546c699258SBarry Smith   PetscFunctionReturn(0);
25556c699258SBarry Smith }
25566c699258SBarry Smith 
25576c699258SBarry Smith #undef __FUNCT__
25586c699258SBarry Smith #define __FUNCT__ "TSGetDM"
25596c699258SBarry Smith /*@
25606c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
25616c699258SBarry Smith 
25623f9fe445SBarry Smith    Not Collective
25636c699258SBarry Smith 
25646c699258SBarry Smith    Input Parameter:
25656c699258SBarry Smith . ts - the preconditioner context
25666c699258SBarry Smith 
25676c699258SBarry Smith    Output Parameter:
25686c699258SBarry Smith .  dm - the dm
25696c699258SBarry Smith 
25706c699258SBarry Smith    Level: intermediate
25716c699258SBarry Smith 
25726c699258SBarry Smith 
25736c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
25746c699258SBarry Smith @*/
25757087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
25766c699258SBarry Smith {
2577496e6a7aSJed Brown   PetscErrorCode ierr;
2578496e6a7aSJed Brown 
25796c699258SBarry Smith   PetscFunctionBegin;
25800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2581496e6a7aSJed Brown   if (!ts->dm) {
2582496e6a7aSJed Brown     ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr);
2583496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
2584496e6a7aSJed Brown   }
25856c699258SBarry Smith   *dm = ts->dm;
25866c699258SBarry Smith   PetscFunctionReturn(0);
25876c699258SBarry Smith }
25881713a123SBarry Smith 
25890f5c6efeSJed Brown #undef __FUNCT__
25900f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
25910f5c6efeSJed Brown /*@
25920f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
25930f5c6efeSJed Brown 
25943f9fe445SBarry Smith    Logically Collective on SNES
25950f5c6efeSJed Brown 
25960f5c6efeSJed Brown    Input Parameter:
2597d42a1c89SJed Brown + snes - nonlinear solver
25980f5c6efeSJed Brown . X - the current state at which to evaluate the residual
2599d42a1c89SJed Brown - ctx - user context, must be a TS
26000f5c6efeSJed Brown 
26010f5c6efeSJed Brown    Output Parameter:
26020f5c6efeSJed Brown . F - the nonlinear residual
26030f5c6efeSJed Brown 
26040f5c6efeSJed Brown    Notes:
26050f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
26060f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
26070f5c6efeSJed Brown 
26080f5c6efeSJed Brown    Level: advanced
26090f5c6efeSJed Brown 
26100f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
26110f5c6efeSJed Brown @*/
26127087cfbeSBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx)
26130f5c6efeSJed Brown {
26140f5c6efeSJed Brown   TS ts = (TS)ctx;
26150f5c6efeSJed Brown   PetscErrorCode ierr;
26160f5c6efeSJed Brown 
26170f5c6efeSJed Brown   PetscFunctionBegin;
26180f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
26190f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
26200f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
26210f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
26220f5c6efeSJed Brown   ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr);
26230f5c6efeSJed Brown   PetscFunctionReturn(0);
26240f5c6efeSJed Brown }
26250f5c6efeSJed Brown 
26260f5c6efeSJed Brown #undef __FUNCT__
26270f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
26280f5c6efeSJed Brown /*@
26290f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
26300f5c6efeSJed Brown 
26310f5c6efeSJed Brown    Collective on SNES
26320f5c6efeSJed Brown 
26330f5c6efeSJed Brown    Input Parameter:
26340f5c6efeSJed Brown + snes - nonlinear solver
26350f5c6efeSJed Brown . X - the current state at which to evaluate the residual
26360f5c6efeSJed Brown - ctx - user context, must be a TS
26370f5c6efeSJed Brown 
26380f5c6efeSJed Brown    Output Parameter:
26390f5c6efeSJed Brown + A - the Jacobian
26400f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
26410f5c6efeSJed Brown - flag - indicates any structure change in the matrix
26420f5c6efeSJed Brown 
26430f5c6efeSJed Brown    Notes:
26440f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
26450f5c6efeSJed Brown 
26460f5c6efeSJed Brown    Level: developer
26470f5c6efeSJed Brown 
26480f5c6efeSJed Brown .seealso: SNESSetJacobian()
26490f5c6efeSJed Brown @*/
26507087cfbeSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx)
26510f5c6efeSJed Brown {
26520f5c6efeSJed Brown   TS ts = (TS)ctx;
26530f5c6efeSJed Brown   PetscErrorCode ierr;
26540f5c6efeSJed Brown 
26550f5c6efeSJed Brown   PetscFunctionBegin;
26560f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
26570f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
26580f5c6efeSJed Brown   PetscValidPointer(A,3);
26590f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
26600f5c6efeSJed Brown   PetscValidPointer(B,4);
26610f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
26620f5c6efeSJed Brown   PetscValidPointer(flag,5);
26630f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
26640f5c6efeSJed Brown   ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr);
26650f5c6efeSJed Brown   PetscFunctionReturn(0);
26660f5c6efeSJed Brown }
2667325fc9f4SBarry Smith 
26680e4ef248SJed Brown #undef __FUNCT__
26690e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
26700e4ef248SJed Brown /*@C
26710e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
26720e4ef248SJed Brown 
26730e4ef248SJed Brown    Collective on TS
26740e4ef248SJed Brown 
26750e4ef248SJed Brown    Input Arguments:
26760e4ef248SJed Brown +  ts - time stepping context
26770e4ef248SJed Brown .  t - time at which to evaluate
26780e4ef248SJed Brown .  X - state at which to evaluate
26790e4ef248SJed Brown -  ctx - context
26800e4ef248SJed Brown 
26810e4ef248SJed Brown    Output Arguments:
26820e4ef248SJed Brown .  F - right hand side
26830e4ef248SJed Brown 
26840e4ef248SJed Brown    Level: intermediate
26850e4ef248SJed Brown 
26860e4ef248SJed Brown    Notes:
26870e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
26880e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
26890e4ef248SJed Brown 
26900e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
26910e4ef248SJed Brown @*/
26920e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx)
26930e4ef248SJed Brown {
26940e4ef248SJed Brown   PetscErrorCode ierr;
26950e4ef248SJed Brown   Mat Arhs,Brhs;
26960e4ef248SJed Brown   MatStructure flg2;
26970e4ef248SJed Brown 
26980e4ef248SJed Brown   PetscFunctionBegin;
26990e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
27000e4ef248SJed Brown   ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
27010e4ef248SJed Brown   ierr = MatMult(Arhs,X,F);CHKERRQ(ierr);
27020e4ef248SJed Brown   PetscFunctionReturn(0);
27030e4ef248SJed Brown }
27040e4ef248SJed Brown 
27050e4ef248SJed Brown #undef __FUNCT__
27060e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
27070e4ef248SJed Brown /*@C
27080e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
27090e4ef248SJed Brown 
27100e4ef248SJed Brown    Collective on TS
27110e4ef248SJed Brown 
27120e4ef248SJed Brown    Input Arguments:
27130e4ef248SJed Brown +  ts - time stepping context
27140e4ef248SJed Brown .  t - time at which to evaluate
27150e4ef248SJed Brown .  X - state at which to evaluate
27160e4ef248SJed Brown -  ctx - context
27170e4ef248SJed Brown 
27180e4ef248SJed Brown    Output Arguments:
27190e4ef248SJed Brown +  A - pointer to operator
27200e4ef248SJed Brown .  B - pointer to preconditioning matrix
27210e4ef248SJed Brown -  flg - matrix structure flag
27220e4ef248SJed Brown 
27230e4ef248SJed Brown    Level: intermediate
27240e4ef248SJed Brown 
27250e4ef248SJed Brown    Notes:
27260e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
27270e4ef248SJed Brown 
27280e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
27290e4ef248SJed Brown @*/
27300e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx)
27310e4ef248SJed Brown {
27320e4ef248SJed Brown 
27330e4ef248SJed Brown   PetscFunctionBegin;
27340e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
27350e4ef248SJed Brown   PetscFunctionReturn(0);
27360e4ef248SJed Brown }
27370e4ef248SJed Brown 
27380026cea9SSean Farley #undef __FUNCT__
27390026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
27400026cea9SSean Farley /*@C
27410026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
27420026cea9SSean Farley 
27430026cea9SSean Farley    Collective on TS
27440026cea9SSean Farley 
27450026cea9SSean Farley    Input Arguments:
27460026cea9SSean Farley +  ts - time stepping context
27470026cea9SSean Farley .  t - time at which to evaluate
27480026cea9SSean Farley .  X - state at which to evaluate
27490026cea9SSean Farley .  Xdot - time derivative of state vector
27500026cea9SSean Farley -  ctx - context
27510026cea9SSean Farley 
27520026cea9SSean Farley    Output Arguments:
27530026cea9SSean Farley .  F - left hand side
27540026cea9SSean Farley 
27550026cea9SSean Farley    Level: intermediate
27560026cea9SSean Farley 
27570026cea9SSean Farley    Notes:
27580026cea9SSean Farley    The assumption here is that the left hand side is of the form A*Xdot (and not A*Xdot + B*X). For other cases, the
27590026cea9SSean Farley    user is required to write their own TSComputeIFunction.
27600026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
27610026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
27620026cea9SSean Farley 
27630026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
27640026cea9SSean Farley @*/
27650026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx)
27660026cea9SSean Farley {
27670026cea9SSean Farley   PetscErrorCode ierr;
27680026cea9SSean Farley   Mat A,B;
27690026cea9SSean Farley   MatStructure flg2;
27700026cea9SSean Farley 
27710026cea9SSean Farley   PetscFunctionBegin;
27720026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
27730026cea9SSean Farley   ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
27740026cea9SSean Farley   ierr = MatMult(A,Xdot,F);CHKERRQ(ierr);
27750026cea9SSean Farley   PetscFunctionReturn(0);
27760026cea9SSean Farley }
27770026cea9SSean Farley 
27780026cea9SSean Farley #undef __FUNCT__
27790026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
27800026cea9SSean Farley /*@C
278124e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
27820026cea9SSean Farley 
27830026cea9SSean Farley    Collective on TS
27840026cea9SSean Farley 
27850026cea9SSean Farley    Input Arguments:
27860026cea9SSean Farley +  ts - time stepping context
27870026cea9SSean Farley .  t - time at which to evaluate
27880026cea9SSean Farley .  X - state at which to evaluate
27890026cea9SSean Farley .  Xdot - time derivative of state vector
27900026cea9SSean Farley .  shift - shift to apply
27910026cea9SSean Farley -  ctx - context
27920026cea9SSean Farley 
27930026cea9SSean Farley    Output Arguments:
27940026cea9SSean Farley +  A - pointer to operator
27950026cea9SSean Farley .  B - pointer to preconditioning matrix
27960026cea9SSean Farley -  flg - matrix structure flag
27970026cea9SSean Farley 
27980026cea9SSean Farley    Level: intermediate
27990026cea9SSean Farley 
28000026cea9SSean Farley    Notes:
28010026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
28020026cea9SSean Farley 
28030026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
28040026cea9SSean Farley @*/
28050026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
28060026cea9SSean Farley {
28070026cea9SSean Farley 
28080026cea9SSean Farley   PetscFunctionBegin;
28090026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
28100026cea9SSean Farley   PetscFunctionReturn(0);
28110026cea9SSean Farley }
28120026cea9SSean Farley 
28134af1b03aSJed Brown 
28144af1b03aSJed Brown #undef __FUNCT__
28154af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
28164af1b03aSJed Brown /*@
28174af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
28184af1b03aSJed Brown 
28194af1b03aSJed Brown    Not Collective
28204af1b03aSJed Brown 
28214af1b03aSJed Brown    Input Parameter:
28224af1b03aSJed Brown .  ts - the TS context
28234af1b03aSJed Brown 
28244af1b03aSJed Brown    Output Parameter:
28254af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
28264af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
28274af1b03aSJed Brown 
28284af1b03aSJed Brown    Level: intermediate
28294af1b03aSJed Brown 
2830cd652676SJed Brown    Notes:
2831cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
28324af1b03aSJed Brown 
28334af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
28344af1b03aSJed Brown 
28354af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
28364af1b03aSJed Brown @*/
28374af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
28384af1b03aSJed Brown {
28394af1b03aSJed Brown   PetscFunctionBegin;
28404af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28414af1b03aSJed Brown   PetscValidPointer(reason,2);
28424af1b03aSJed Brown   *reason = ts->reason;
28434af1b03aSJed Brown   PetscFunctionReturn(0);
28444af1b03aSJed Brown }
28454af1b03aSJed Brown 
2846fb1732b5SBarry Smith #undef __FUNCT__
28475ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
28489f67acb7SJed Brown /*@
28495ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
28509f67acb7SJed Brown    used by the time integrator.
28519f67acb7SJed Brown 
28529f67acb7SJed Brown    Not Collective
28539f67acb7SJed Brown 
28549f67acb7SJed Brown    Input Parameter:
28559f67acb7SJed Brown .  ts - TS context
28569f67acb7SJed Brown 
28579f67acb7SJed Brown    Output Parameter:
28589f67acb7SJed Brown .  nits - number of nonlinear iterations
28599f67acb7SJed Brown 
28609f67acb7SJed Brown    Notes:
28619f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
28629f67acb7SJed Brown 
28639f67acb7SJed Brown    Level: intermediate
28649f67acb7SJed Brown 
28659f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
28669f67acb7SJed Brown 
28675ef26d82SJed Brown .seealso:  TSGetKSPIterations()
28689f67acb7SJed Brown @*/
28695ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
28709f67acb7SJed Brown {
28719f67acb7SJed Brown   PetscFunctionBegin;
28729f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28739f67acb7SJed Brown   PetscValidIntPointer(nits,2);
28745ef26d82SJed Brown   *nits = ts->snes_its;
28759f67acb7SJed Brown   PetscFunctionReturn(0);
28769f67acb7SJed Brown }
28779f67acb7SJed Brown 
28789f67acb7SJed Brown #undef __FUNCT__
28795ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
28809f67acb7SJed Brown /*@
28815ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
28829f67acb7SJed Brown    used by the time integrator.
28839f67acb7SJed Brown 
28849f67acb7SJed Brown    Not Collective
28859f67acb7SJed Brown 
28869f67acb7SJed Brown    Input Parameter:
28879f67acb7SJed Brown .  ts - TS context
28889f67acb7SJed Brown 
28899f67acb7SJed Brown    Output Parameter:
28909f67acb7SJed Brown .  lits - number of linear iterations
28919f67acb7SJed Brown 
28929f67acb7SJed Brown    Notes:
28939f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
28949f67acb7SJed Brown 
28959f67acb7SJed Brown    Level: intermediate
28969f67acb7SJed Brown 
28979f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
28989f67acb7SJed Brown 
28995ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
29009f67acb7SJed Brown @*/
29015ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
29029f67acb7SJed Brown {
29039f67acb7SJed Brown   PetscFunctionBegin;
29049f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29059f67acb7SJed Brown   PetscValidIntPointer(lits,2);
29065ef26d82SJed Brown   *lits = ts->ksp_its;
29079f67acb7SJed Brown   PetscFunctionReturn(0);
29089f67acb7SJed Brown }
29099f67acb7SJed Brown 
29109f67acb7SJed Brown #undef __FUNCT__
2911cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
2912cef5090cSJed Brown /*@
2913cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
2914cef5090cSJed Brown 
2915cef5090cSJed Brown    Not Collective
2916cef5090cSJed Brown 
2917cef5090cSJed Brown    Input Parameter:
2918cef5090cSJed Brown .  ts - TS context
2919cef5090cSJed Brown 
2920cef5090cSJed Brown    Output Parameter:
2921cef5090cSJed Brown .  rejects - number of steps rejected
2922cef5090cSJed Brown 
2923cef5090cSJed Brown    Notes:
2924cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
2925cef5090cSJed Brown 
2926cef5090cSJed Brown    Level: intermediate
2927cef5090cSJed Brown 
2928cef5090cSJed Brown .keywords: TS, get, number
2929cef5090cSJed Brown 
29305ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
2931cef5090cSJed Brown @*/
2932cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
2933cef5090cSJed Brown {
2934cef5090cSJed Brown   PetscFunctionBegin;
2935cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2936cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
2937cef5090cSJed Brown   *rejects = ts->reject;
2938cef5090cSJed Brown   PetscFunctionReturn(0);
2939cef5090cSJed Brown }
2940cef5090cSJed Brown 
2941cef5090cSJed Brown #undef __FUNCT__
2942cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
2943cef5090cSJed Brown /*@
2944cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
2945cef5090cSJed Brown 
2946cef5090cSJed Brown    Not Collective
2947cef5090cSJed Brown 
2948cef5090cSJed Brown    Input Parameter:
2949cef5090cSJed Brown .  ts - TS context
2950cef5090cSJed Brown 
2951cef5090cSJed Brown    Output Parameter:
2952cef5090cSJed Brown .  fails - number of failed nonlinear solves
2953cef5090cSJed Brown 
2954cef5090cSJed Brown    Notes:
2955cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
2956cef5090cSJed Brown 
2957cef5090cSJed Brown    Level: intermediate
2958cef5090cSJed Brown 
2959cef5090cSJed Brown .keywords: TS, get, number
2960cef5090cSJed Brown 
29615ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
2962cef5090cSJed Brown @*/
2963cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
2964cef5090cSJed Brown {
2965cef5090cSJed Brown   PetscFunctionBegin;
2966cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2967cef5090cSJed Brown   PetscValidIntPointer(fails,2);
2968cef5090cSJed Brown   *fails = ts->num_snes_failures;
2969cef5090cSJed Brown   PetscFunctionReturn(0);
2970cef5090cSJed Brown }
2971cef5090cSJed Brown 
2972cef5090cSJed Brown #undef __FUNCT__
2973cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
2974cef5090cSJed Brown /*@
2975cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
2976cef5090cSJed Brown 
2977cef5090cSJed Brown    Not Collective
2978cef5090cSJed Brown 
2979cef5090cSJed Brown    Input Parameter:
2980cef5090cSJed Brown +  ts - TS context
2981cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
2982cef5090cSJed Brown 
2983cef5090cSJed Brown    Notes:
2984cef5090cSJed Brown    The counter is reset to zero for each step
2985cef5090cSJed Brown 
2986cef5090cSJed Brown    Options Database Key:
2987cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
2988cef5090cSJed Brown 
2989cef5090cSJed Brown    Level: intermediate
2990cef5090cSJed Brown 
2991cef5090cSJed Brown .keywords: TS, set, maximum, number
2992cef5090cSJed Brown 
29935ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
2994cef5090cSJed Brown @*/
2995cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
2996cef5090cSJed Brown {
2997cef5090cSJed Brown   PetscFunctionBegin;
2998cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2999cef5090cSJed Brown   ts->max_reject = rejects;
3000cef5090cSJed Brown   PetscFunctionReturn(0);
3001cef5090cSJed Brown }
3002cef5090cSJed Brown 
3003cef5090cSJed Brown #undef __FUNCT__
3004cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3005cef5090cSJed Brown /*@
3006cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3007cef5090cSJed Brown 
3008cef5090cSJed Brown    Not Collective
3009cef5090cSJed Brown 
3010cef5090cSJed Brown    Input Parameter:
3011cef5090cSJed Brown +  ts - TS context
3012cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3013cef5090cSJed Brown 
3014cef5090cSJed Brown    Notes:
3015cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3016cef5090cSJed Brown 
3017cef5090cSJed Brown    Options Database Key:
3018cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3019cef5090cSJed Brown 
3020cef5090cSJed Brown    Level: intermediate
3021cef5090cSJed Brown 
3022cef5090cSJed Brown .keywords: TS, set, maximum, number
3023cef5090cSJed Brown 
30245ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3025cef5090cSJed Brown @*/
3026cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3027cef5090cSJed Brown {
3028cef5090cSJed Brown   PetscFunctionBegin;
3029cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3030cef5090cSJed Brown   ts->max_snes_failures = fails;
3031cef5090cSJed Brown   PetscFunctionReturn(0);
3032cef5090cSJed Brown }
3033cef5090cSJed Brown 
3034cef5090cSJed Brown #undef __FUNCT__
3035cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3036cef5090cSJed Brown /*@
3037cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3038cef5090cSJed Brown 
3039cef5090cSJed Brown    Not Collective
3040cef5090cSJed Brown 
3041cef5090cSJed Brown    Input Parameter:
3042cef5090cSJed Brown +  ts - TS context
3043cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3044cef5090cSJed Brown 
3045cef5090cSJed Brown    Options Database Key:
3046cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3047cef5090cSJed Brown 
3048cef5090cSJed Brown    Level: intermediate
3049cef5090cSJed Brown 
3050cef5090cSJed Brown .keywords: TS, set, error
3051cef5090cSJed Brown 
30525ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3053cef5090cSJed Brown @*/
3054cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3055cef5090cSJed Brown {
3056cef5090cSJed Brown   PetscFunctionBegin;
3057cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3058cef5090cSJed Brown   ts->errorifstepfailed = err;
3059cef5090cSJed Brown   PetscFunctionReturn(0);
3060cef5090cSJed Brown }
3061cef5090cSJed Brown 
3062cef5090cSJed Brown #undef __FUNCT__
3063fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3064fb1732b5SBarry Smith /*@C
3065fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3066fb1732b5SBarry Smith 
3067fb1732b5SBarry Smith    Collective on TS
3068fb1732b5SBarry Smith 
3069fb1732b5SBarry Smith    Input Parameters:
3070fb1732b5SBarry Smith +  ts - the TS context
3071fb1732b5SBarry Smith .  step - current time-step
3072fb1732b5SBarry Smith .  ptime - current time
3073ed81e22dSJed Brown .  x - current state
3074fb1732b5SBarry Smith -  viewer - binary viewer
3075fb1732b5SBarry Smith 
3076fb1732b5SBarry Smith    Level: intermediate
3077fb1732b5SBarry Smith 
3078fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3079fb1732b5SBarry Smith 
3080fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3081fb1732b5SBarry Smith @*/
3082ed81e22dSJed Brown PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec x,void *viewer)
3083fb1732b5SBarry Smith {
3084fb1732b5SBarry Smith   PetscErrorCode       ierr;
3085ed81e22dSJed Brown   PetscViewer          v = (PetscViewer)viewer;
3086fb1732b5SBarry Smith 
3087fb1732b5SBarry Smith   PetscFunctionBegin;
3088ed81e22dSJed Brown   ierr = VecView(x,v);CHKERRQ(ierr);
3089ed81e22dSJed Brown   PetscFunctionReturn(0);
3090ed81e22dSJed Brown }
3091ed81e22dSJed Brown 
3092ed81e22dSJed Brown #undef __FUNCT__
3093ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3094ed81e22dSJed Brown /*@C
3095ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3096ed81e22dSJed Brown 
3097ed81e22dSJed Brown    Collective on TS
3098ed81e22dSJed Brown 
3099ed81e22dSJed Brown    Input Parameters:
3100ed81e22dSJed Brown +  ts - the TS context
3101ed81e22dSJed Brown .  step - current time-step
3102ed81e22dSJed Brown .  ptime - current time
3103ed81e22dSJed Brown .  x - current state
3104ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3105ed81e22dSJed Brown 
3106ed81e22dSJed Brown    Level: intermediate
3107ed81e22dSJed Brown 
3108ed81e22dSJed Brown    Notes:
3109ed81e22dSJed 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.
3110ed81e22dSJed Brown    These are named according to the file name template.
3111ed81e22dSJed Brown 
3112ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3113ed81e22dSJed Brown 
3114ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3115ed81e22dSJed Brown 
3116ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3117ed81e22dSJed Brown @*/
3118ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec x,void *filenametemplate)
3119ed81e22dSJed Brown {
3120ed81e22dSJed Brown   PetscErrorCode ierr;
3121ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3122ed81e22dSJed Brown   PetscViewer    viewer;
3123ed81e22dSJed Brown 
3124ed81e22dSJed Brown   PetscFunctionBegin;
3125ed81e22dSJed Brown   ierr = PetscSNPrintf(filename,sizeof filename,(const char*)filenametemplate,step);CHKERRQ(ierr);
3126ed81e22dSJed Brown   ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
3127fb1732b5SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
3128ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3129ed81e22dSJed Brown   PetscFunctionReturn(0);
3130ed81e22dSJed Brown }
3131ed81e22dSJed Brown 
3132ed81e22dSJed Brown #undef __FUNCT__
3133ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3134ed81e22dSJed Brown /*@C
3135ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3136ed81e22dSJed Brown 
3137ed81e22dSJed Brown    Collective on TS
3138ed81e22dSJed Brown 
3139ed81e22dSJed Brown    Input Parameters:
3140ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3141ed81e22dSJed Brown 
3142ed81e22dSJed Brown    Level: intermediate
3143ed81e22dSJed Brown 
3144ed81e22dSJed Brown    Note:
3145ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3146ed81e22dSJed Brown 
3147ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3148ed81e22dSJed Brown 
3149ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3150ed81e22dSJed Brown @*/
3151ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3152ed81e22dSJed Brown {
3153ed81e22dSJed Brown   PetscErrorCode ierr;
3154ed81e22dSJed Brown 
3155ed81e22dSJed Brown   PetscFunctionBegin;
3156ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3157fb1732b5SBarry Smith   PetscFunctionReturn(0);
3158fb1732b5SBarry Smith }
3159fb1732b5SBarry Smith 
316084df9cb4SJed Brown #undef __FUNCT__
316184df9cb4SJed Brown #define __FUNCT__ "TSGetAdapt"
316284df9cb4SJed Brown /*@
316384df9cb4SJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
316484df9cb4SJed Brown 
3165ed81e22dSJed Brown    Collective on TS if controller has not been created yet
316684df9cb4SJed Brown 
316784df9cb4SJed Brown    Input Arguments:
3168ed81e22dSJed Brown .  ts - time stepping context
316984df9cb4SJed Brown 
317084df9cb4SJed Brown    Output Arguments:
3171ed81e22dSJed Brown .  adapt - adaptive controller
317284df9cb4SJed Brown 
317384df9cb4SJed Brown    Level: intermediate
317484df9cb4SJed Brown 
3175ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
317684df9cb4SJed Brown @*/
317784df9cb4SJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
317884df9cb4SJed Brown {
317984df9cb4SJed Brown   PetscErrorCode ierr;
318084df9cb4SJed Brown 
318184df9cb4SJed Brown   PetscFunctionBegin;
318284df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
318384df9cb4SJed Brown   PetscValidPointer(adapt,2);
318484df9cb4SJed Brown   if (!ts->adapt) {
318584df9cb4SJed Brown     ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr);
31861c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
31871c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
318884df9cb4SJed Brown   }
318984df9cb4SJed Brown   *adapt = ts->adapt;
319084df9cb4SJed Brown   PetscFunctionReturn(0);
319184df9cb4SJed Brown }
3192d6ebe24aSShri Abhyankar 
3193d6ebe24aSShri Abhyankar #undef __FUNCT__
31941c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
31951c3436cfSJed Brown /*@
31961c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
31971c3436cfSJed Brown 
31981c3436cfSJed Brown    Logically Collective
31991c3436cfSJed Brown 
32001c3436cfSJed Brown    Input Arguments:
32011c3436cfSJed Brown +  ts - time integration context
32021c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
32031c3436cfSJed Brown .  vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present
32041c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
32051c3436cfSJed Brown -  vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present
32061c3436cfSJed Brown 
32071c3436cfSJed Brown    Level: beginner
32081c3436cfSJed Brown 
3209c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
32101c3436cfSJed Brown @*/
32111c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
32121c3436cfSJed Brown {
32131c3436cfSJed Brown   PetscErrorCode ierr;
32141c3436cfSJed Brown 
32151c3436cfSJed Brown   PetscFunctionBegin;
3216c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
32171c3436cfSJed Brown   if (vatol) {
32181c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
32191c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
32201c3436cfSJed Brown     ts->vatol = vatol;
32211c3436cfSJed Brown   }
3222c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
32231c3436cfSJed Brown   if (vrtol) {
32241c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
32251c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
32261c3436cfSJed Brown     ts->vrtol = vrtol;
32271c3436cfSJed Brown   }
32281c3436cfSJed Brown   PetscFunctionReturn(0);
32291c3436cfSJed Brown }
32301c3436cfSJed Brown 
32311c3436cfSJed Brown #undef __FUNCT__
3232c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3233c5033834SJed Brown /*@
3234c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3235c5033834SJed Brown 
3236c5033834SJed Brown    Logically Collective
3237c5033834SJed Brown 
3238c5033834SJed Brown    Input Arguments:
3239c5033834SJed Brown .  ts - time integration context
3240c5033834SJed Brown 
3241c5033834SJed Brown    Output Arguments:
3242c5033834SJed Brown +  atol - scalar absolute tolerances, PETSC_NULL to ignore
3243c5033834SJed Brown .  vatol - vector of absolute tolerances, PETSC_NULL to ignore
3244c5033834SJed Brown .  rtol - scalar relative tolerances, PETSC_NULL to ignore
3245c5033834SJed Brown -  vrtol - vector of relative tolerances, PETSC_NULL to ignore
3246c5033834SJed Brown 
3247c5033834SJed Brown    Level: beginner
3248c5033834SJed Brown 
3249c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3250c5033834SJed Brown @*/
3251c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3252c5033834SJed Brown {
3253c5033834SJed Brown 
3254c5033834SJed Brown   PetscFunctionBegin;
3255c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3256c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3257c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3258c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3259c5033834SJed Brown   PetscFunctionReturn(0);
3260c5033834SJed Brown }
3261c5033834SJed Brown 
3262c5033834SJed Brown #undef __FUNCT__
32631c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
32641c3436cfSJed Brown /*@
32651c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
32661c3436cfSJed Brown 
32671c3436cfSJed Brown    Collective on TS
32681c3436cfSJed Brown 
32691c3436cfSJed Brown    Input Arguments:
32701c3436cfSJed Brown +  ts - time stepping context
32711c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
32721c3436cfSJed Brown 
32731c3436cfSJed Brown    Output Arguments:
32741c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
32751c3436cfSJed Brown 
32761c3436cfSJed Brown    Level: developer
32771c3436cfSJed Brown 
32781c3436cfSJed Brown .seealso: TSSetTolerances()
32791c3436cfSJed Brown @*/
32801c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
32811c3436cfSJed Brown {
32821c3436cfSJed Brown   PetscErrorCode ierr;
32831c3436cfSJed Brown   PetscInt i,n,N;
32841c3436cfSJed Brown   const PetscScalar *x,*y;
32851c3436cfSJed Brown   Vec X;
32861c3436cfSJed Brown   PetscReal sum,gsum;
32871c3436cfSJed Brown 
32881c3436cfSJed Brown   PetscFunctionBegin;
32891c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32901c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
32911c3436cfSJed Brown   PetscValidPointer(norm,3);
32921c3436cfSJed Brown   X = ts->vec_sol;
32931c3436cfSJed Brown   PetscCheckSameTypeAndComm(X,1,Y,2);
32941c3436cfSJed Brown   if (X == Y) SETERRQ(((PetscObject)X)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
32951c3436cfSJed Brown 
32961c3436cfSJed Brown   ierr = VecGetSize(X,&N);CHKERRQ(ierr);
32971c3436cfSJed Brown   ierr = VecGetLocalSize(X,&n);CHKERRQ(ierr);
32981c3436cfSJed Brown   ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr);
32991c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
33001c3436cfSJed Brown   sum = 0.;
3301ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
3302ceefc113SJed Brown     const PetscScalar *atol,*rtol;
3303ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3304ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3305ceefc113SJed Brown     for (i=0; i<n; i++) {
3306ceefc113SJed Brown       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3307ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3308ceefc113SJed Brown     }
3309ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3310ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3311ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
3312ceefc113SJed Brown     const PetscScalar *atol;
3313ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3314ceefc113SJed Brown     for (i=0; i<n; i++) {
3315ceefc113SJed Brown       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3316ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3317ceefc113SJed Brown     }
3318ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3319ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
3320ceefc113SJed Brown     const PetscScalar *rtol;
3321ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3322ceefc113SJed Brown     for (i=0; i<n; i++) {
3323ceefc113SJed Brown       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3324ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3325ceefc113SJed Brown     }
3326ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3327ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
33281c3436cfSJed Brown     for (i=0; i<n; i++) {
33291c3436cfSJed Brown       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
33301c3436cfSJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
33311c3436cfSJed Brown     }
3332ceefc113SJed Brown   }
33331c3436cfSJed Brown   ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr);
33341c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
33351c3436cfSJed Brown 
33361c3436cfSJed Brown   ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr);
33371c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
33381c3436cfSJed Brown   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
33391c3436cfSJed Brown   PetscFunctionReturn(0);
33401c3436cfSJed Brown }
33411c3436cfSJed Brown 
33421c3436cfSJed Brown #undef __FUNCT__
33438d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
33448d59e960SJed Brown /*@
33458d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
33468d59e960SJed Brown 
33478d59e960SJed Brown    Logically Collective on TS
33488d59e960SJed Brown 
33498d59e960SJed Brown    Input Arguments:
33508d59e960SJed Brown +  ts - time stepping context
33518d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
33528d59e960SJed Brown 
33538d59e960SJed Brown    Note:
33548d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
33558d59e960SJed Brown 
33568d59e960SJed Brown    Level: intermediate
33578d59e960SJed Brown 
33588d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
33598d59e960SJed Brown @*/
33608d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
33618d59e960SJed Brown {
33628d59e960SJed Brown 
33638d59e960SJed Brown   PetscFunctionBegin;
33648d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33658d59e960SJed Brown   ts->cfltime_local = cfltime;
33668d59e960SJed Brown   ts->cfltime = -1.;
33678d59e960SJed Brown   PetscFunctionReturn(0);
33688d59e960SJed Brown }
33698d59e960SJed Brown 
33708d59e960SJed Brown #undef __FUNCT__
33718d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
33728d59e960SJed Brown /*@
33738d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
33748d59e960SJed Brown 
33758d59e960SJed Brown    Collective on TS
33768d59e960SJed Brown 
33778d59e960SJed Brown    Input Arguments:
33788d59e960SJed Brown .  ts - time stepping context
33798d59e960SJed Brown 
33808d59e960SJed Brown    Output Arguments:
33818d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
33828d59e960SJed Brown 
33838d59e960SJed Brown    Level: advanced
33848d59e960SJed Brown 
33858d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
33868d59e960SJed Brown @*/
33878d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
33888d59e960SJed Brown {
33898d59e960SJed Brown   PetscErrorCode ierr;
33908d59e960SJed Brown 
33918d59e960SJed Brown   PetscFunctionBegin;
33928d59e960SJed Brown   if (ts->cfltime < 0) {
33938d59e960SJed Brown     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr);
33948d59e960SJed Brown   }
33958d59e960SJed Brown   *cfltime = ts->cfltime;
33968d59e960SJed Brown   PetscFunctionReturn(0);
33978d59e960SJed Brown }
33988d59e960SJed Brown 
33998d59e960SJed Brown #undef __FUNCT__
3400d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
3401d6ebe24aSShri Abhyankar /*@
3402d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
3403d6ebe24aSShri Abhyankar 
3404d6ebe24aSShri Abhyankar    Input Parameters:
3405d6ebe24aSShri Abhyankar .  ts   - the TS context.
3406d6ebe24aSShri Abhyankar .  xl   - lower bound.
3407d6ebe24aSShri Abhyankar .  xu   - upper bound.
3408d6ebe24aSShri Abhyankar 
3409d6ebe24aSShri Abhyankar    Notes:
3410d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
341133c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
3412d6ebe24aSShri Abhyankar 
34132bd2b0e6SSatish Balay    Level: advanced
34142bd2b0e6SSatish Balay 
3415d6ebe24aSShri Abhyankar @*/
3416d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
3417d6ebe24aSShri Abhyankar {
3418d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
3419d6ebe24aSShri Abhyankar   SNES           snes;
3420d6ebe24aSShri Abhyankar 
3421d6ebe24aSShri Abhyankar   PetscFunctionBegin;
3422d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3423d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
3424d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
3425d6ebe24aSShri Abhyankar }
3426d6ebe24aSShri Abhyankar 
3427325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3428c6db04a5SJed Brown #include <mex.h>
3429325fc9f4SBarry Smith 
3430325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
3431325fc9f4SBarry Smith 
3432325fc9f4SBarry Smith #undef __FUNCT__
3433325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
3434325fc9f4SBarry Smith /*
3435325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
3436325fc9f4SBarry Smith                          TSSetFunctionMatlab().
3437325fc9f4SBarry Smith 
3438325fc9f4SBarry Smith    Collective on TS
3439325fc9f4SBarry Smith 
3440325fc9f4SBarry Smith    Input Parameters:
3441325fc9f4SBarry Smith +  snes - the TS context
3442325fc9f4SBarry Smith -  x - input vector
3443325fc9f4SBarry Smith 
3444325fc9f4SBarry Smith    Output Parameter:
3445325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
3446325fc9f4SBarry Smith 
3447325fc9f4SBarry Smith    Notes:
3448325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
3449325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
3450325fc9f4SBarry Smith    themselves.
3451325fc9f4SBarry Smith 
3452325fc9f4SBarry Smith    Level: developer
3453325fc9f4SBarry Smith 
3454325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3455325fc9f4SBarry Smith 
3456325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3457325fc9f4SBarry Smith */
34587087cfbeSBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx)
3459325fc9f4SBarry Smith {
3460325fc9f4SBarry Smith   PetscErrorCode   ierr;
3461325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3462325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
3463325fc9f4SBarry Smith   mxArray          *plhs[1],*prhs[7];
3464325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
3465325fc9f4SBarry Smith 
3466325fc9f4SBarry Smith   PetscFunctionBegin;
3467325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
3468325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3469325fc9f4SBarry Smith   PetscValidHeaderSpecific(xdot,VEC_CLASSID,4);
3470325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
3471325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,x,3);
3472325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
3473325fc9f4SBarry Smith 
3474325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3475325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3476880f3077SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr);
3477325fc9f4SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
3478325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3479325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
3480325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3481325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3482325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
3483325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
3484325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
3485325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
3486325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3487325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3488325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3489325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3490325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3491325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3492325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3493325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3494325fc9f4SBarry Smith   PetscFunctionReturn(0);
3495325fc9f4SBarry Smith }
3496325fc9f4SBarry Smith 
3497325fc9f4SBarry Smith 
3498325fc9f4SBarry Smith #undef __FUNCT__
3499325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
3500325fc9f4SBarry Smith /*
3501325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
3502325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
3503e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
3504325fc9f4SBarry Smith 
3505325fc9f4SBarry Smith    Logically Collective on TS
3506325fc9f4SBarry Smith 
3507325fc9f4SBarry Smith    Input Parameters:
3508325fc9f4SBarry Smith +  ts - the TS context
3509325fc9f4SBarry Smith -  func - function evaluation routine
3510325fc9f4SBarry Smith 
3511325fc9f4SBarry Smith    Calling sequence of func:
3512325fc9f4SBarry Smith $    func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx);
3513325fc9f4SBarry Smith 
3514325fc9f4SBarry Smith    Level: beginner
3515325fc9f4SBarry Smith 
3516325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3517325fc9f4SBarry Smith 
3518325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3519325fc9f4SBarry Smith */
3520cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
3521325fc9f4SBarry Smith {
3522325fc9f4SBarry Smith   PetscErrorCode  ierr;
3523325fc9f4SBarry Smith   TSMatlabContext *sctx;
3524325fc9f4SBarry Smith 
3525325fc9f4SBarry Smith   PetscFunctionBegin;
3526325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3527325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3528325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3529325fc9f4SBarry Smith   /*
3530325fc9f4SBarry Smith      This should work, but it doesn't
3531325fc9f4SBarry Smith   sctx->ctx = ctx;
3532325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3533325fc9f4SBarry Smith   */
3534325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3535cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
3536325fc9f4SBarry Smith   PetscFunctionReturn(0);
3537325fc9f4SBarry Smith }
3538325fc9f4SBarry Smith 
3539325fc9f4SBarry Smith #undef __FUNCT__
3540325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
3541325fc9f4SBarry Smith /*
3542325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
3543325fc9f4SBarry Smith                          TSSetJacobianMatlab().
3544325fc9f4SBarry Smith 
3545325fc9f4SBarry Smith    Collective on TS
3546325fc9f4SBarry Smith 
3547325fc9f4SBarry Smith    Input Parameters:
3548cdcf91faSSean Farley +  ts - the TS context
3549325fc9f4SBarry Smith .  x - input vector
3550325fc9f4SBarry Smith .  A, B - the matrices
3551325fc9f4SBarry Smith -  ctx - user context
3552325fc9f4SBarry Smith 
3553325fc9f4SBarry Smith    Output Parameter:
3554325fc9f4SBarry Smith .  flag - structure of the matrix
3555325fc9f4SBarry Smith 
3556325fc9f4SBarry Smith    Level: developer
3557325fc9f4SBarry Smith 
3558325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3559325fc9f4SBarry Smith 
3560325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3561325fc9f4SBarry Smith @*/
3562cdcf91faSSean Farley PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
3563325fc9f4SBarry Smith {
3564325fc9f4SBarry Smith   PetscErrorCode  ierr;
3565325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3566325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
3567325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
3568325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
3569325fc9f4SBarry Smith 
3570325fc9f4SBarry Smith   PetscFunctionBegin;
3571cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3572325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3573325fc9f4SBarry Smith 
3574325fc9f4SBarry Smith   /* call Matlab function in ctx with arguments x and y */
3575325fc9f4SBarry Smith 
3576cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
3577325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3578325fc9f4SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr);
3579325fc9f4SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
3580325fc9f4SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
3581325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3582325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
3583325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3584325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3585325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
3586325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
3587325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
3588325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
3589325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
3590325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
3591325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3592325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
3593325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3594325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3595325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3596325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3597325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3598325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3599325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
3600325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
3601325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3602325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
3603325fc9f4SBarry Smith   PetscFunctionReturn(0);
3604325fc9f4SBarry Smith }
3605325fc9f4SBarry Smith 
3606325fc9f4SBarry Smith 
3607325fc9f4SBarry Smith #undef __FUNCT__
3608325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
3609325fc9f4SBarry Smith /*
3610325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
3611e3c5b3baSBarry 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
3612325fc9f4SBarry Smith 
3613325fc9f4SBarry Smith    Logically Collective on TS
3614325fc9f4SBarry Smith 
3615325fc9f4SBarry Smith    Input Parameters:
3616cdcf91faSSean Farley +  ts - the TS context
3617325fc9f4SBarry Smith .  A,B - Jacobian matrices
3618325fc9f4SBarry Smith .  func - function evaluation routine
3619325fc9f4SBarry Smith -  ctx - user context
3620325fc9f4SBarry Smith 
3621325fc9f4SBarry Smith    Calling sequence of func:
3622cdcf91faSSean Farley $    flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx);
3623325fc9f4SBarry Smith 
3624325fc9f4SBarry Smith 
3625325fc9f4SBarry Smith    Level: developer
3626325fc9f4SBarry Smith 
3627325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3628325fc9f4SBarry Smith 
3629325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3630325fc9f4SBarry Smith */
3631cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
3632325fc9f4SBarry Smith {
3633325fc9f4SBarry Smith   PetscErrorCode    ierr;
3634325fc9f4SBarry Smith   TSMatlabContext *sctx;
3635325fc9f4SBarry Smith 
3636325fc9f4SBarry Smith   PetscFunctionBegin;
3637325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3638325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3639325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3640325fc9f4SBarry Smith   /*
3641325fc9f4SBarry Smith      This should work, but it doesn't
3642325fc9f4SBarry Smith   sctx->ctx = ctx;
3643325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3644325fc9f4SBarry Smith   */
3645325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3646cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
3647325fc9f4SBarry Smith   PetscFunctionReturn(0);
3648325fc9f4SBarry Smith }
3649325fc9f4SBarry Smith 
3650b5b1a830SBarry Smith #undef __FUNCT__
3651b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
3652b5b1a830SBarry Smith /*
3653b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
3654b5b1a830SBarry Smith 
3655b5b1a830SBarry Smith    Collective on TS
3656b5b1a830SBarry Smith 
3657b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3658b5b1a830SBarry Smith @*/
3659cdcf91faSSean Farley PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx)
3660b5b1a830SBarry Smith {
3661b5b1a830SBarry Smith   PetscErrorCode  ierr;
3662b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3663a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
3664b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
3665b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
3666b5b1a830SBarry Smith 
3667b5b1a830SBarry Smith   PetscFunctionBegin;
3668cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3669b5b1a830SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,4);
3670b5b1a830SBarry Smith 
3671cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
3672b5b1a830SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3673b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3674b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
3675b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
3676b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
3677b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
3678b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
3679b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
3680b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3681b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
3682b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
3683b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
3684b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
3685b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
3686b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
3687b5b1a830SBarry Smith   PetscFunctionReturn(0);
3688b5b1a830SBarry Smith }
3689b5b1a830SBarry Smith 
3690b5b1a830SBarry Smith 
3691b5b1a830SBarry Smith #undef __FUNCT__
3692b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
3693b5b1a830SBarry Smith /*
3694b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
3695b5b1a830SBarry Smith 
3696b5b1a830SBarry Smith    Level: developer
3697b5b1a830SBarry Smith 
3698b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
3699b5b1a830SBarry Smith 
3700b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3701b5b1a830SBarry Smith */
3702cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
3703b5b1a830SBarry Smith {
3704b5b1a830SBarry Smith   PetscErrorCode    ierr;
3705b5b1a830SBarry Smith   TSMatlabContext *sctx;
3706b5b1a830SBarry Smith 
3707b5b1a830SBarry Smith   PetscFunctionBegin;
3708b5b1a830SBarry Smith   /* currently sctx is memory bleed */
3709b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3710b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3711b5b1a830SBarry Smith   /*
3712b5b1a830SBarry Smith      This should work, but it doesn't
3713b5b1a830SBarry Smith   sctx->ctx = ctx;
3714b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3715b5b1a830SBarry Smith   */
3716b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3717cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
3718b5b1a830SBarry Smith   PetscFunctionReturn(0);
3719b5b1a830SBarry Smith }
3720325fc9f4SBarry Smith #endif
3721