xref: /petsc/src/ts/interface/ts.c (revision ca94891dcb5a20bac154bf84d5797a91e2e8c8b5)
163dd3a1aSKris Buschelman 
2c6db04a5SJed Brown #include <private/tsimpl.h>        /*I "petscts.h"  I*/
3d763cef2SBarry Smith 
4d5ba7fb7SMatthew Knepley /* Logging support */
57087cfbeSBarry Smith PetscClassId  TS_CLASSID;
6166c7f25SBarry Smith PetscLogEvent  TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
7d405a339SMatthew Knepley 
84a2ae208SSatish Balay #undef __FUNCT__
9bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
10bdad233fSMatthew Knepley /*
11bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
12bdad233fSMatthew Knepley 
13bdad233fSMatthew Knepley   Collective on TS
14bdad233fSMatthew Knepley 
15bdad233fSMatthew Knepley   Input Parameter:
16bdad233fSMatthew Knepley . ts - The ts
17bdad233fSMatthew Knepley 
18bdad233fSMatthew Knepley   Level: intermediate
19bdad233fSMatthew Knepley 
20bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
21bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
22bdad233fSMatthew Knepley */
236849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
24bdad233fSMatthew Knepley {
25ace3abfcSBarry Smith   PetscBool      opt;
262fc52814SBarry Smith   const char     *defaultType;
27bdad233fSMatthew Knepley   char           typeName[256];
28dfbe8321SBarry Smith   PetscErrorCode ierr;
29bdad233fSMatthew Knepley 
30bdad233fSMatthew Knepley   PetscFunctionBegin;
317adad957SLisandro Dalcin   if (((PetscObject)ts)->type_name) {
327adad957SLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
33bdad233fSMatthew Knepley   } else {
349596e0b4SJed Brown     defaultType = TSEULER;
35bdad233fSMatthew Knepley   }
36bdad233fSMatthew Knepley 
37cce0b1b2SLisandro Dalcin   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
38bdad233fSMatthew Knepley   ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
39a7cc72afSBarry Smith   if (opt) {
40bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
41bdad233fSMatthew Knepley   } else {
42bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
43bdad233fSMatthew Knepley   }
44bdad233fSMatthew Knepley   PetscFunctionReturn(0);
45bdad233fSMatthew Knepley }
46bdad233fSMatthew Knepley 
47bdad233fSMatthew Knepley #undef __FUNCT__
48bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
49bdad233fSMatthew Knepley /*@
50bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
51bdad233fSMatthew Knepley 
52bdad233fSMatthew Knepley    Collective on TS
53bdad233fSMatthew Knepley 
54bdad233fSMatthew Knepley    Input Parameter:
55bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
56bdad233fSMatthew Knepley 
57bdad233fSMatthew Knepley    Options Database Keys:
584d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
59bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
60bdad233fSMatthew Knepley .  -ts_max_time time - maximum time to compute to
61bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
62bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
63a6570f20SBarry Smith -  -ts_monitor_draw - plot information at each timestep
64bdad233fSMatthew Knepley 
65bdad233fSMatthew Knepley    Level: beginner
66bdad233fSMatthew Knepley 
67bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
68bdad233fSMatthew Knepley 
69a313700dSBarry Smith .seealso: TSGetType()
70bdad233fSMatthew Knepley @*/
717087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
72bdad233fSMatthew Knepley {
73bdad233fSMatthew Knepley   PetscReal      dt;
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;
79bdad233fSMatthew Knepley 
80bdad233fSMatthew Knepley   PetscFunctionBegin;
810700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
827adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)ts)->comm, ((PetscObject)ts)->prefix, "Time step options", "TS");CHKERRQ(ierr);
83bdad233fSMatthew Knepley 
84bdad233fSMatthew Knepley     /* Handle generic TS options */
85bdad233fSMatthew Knepley     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
86bdad233fSMatthew Knepley     ierr = PetscOptionsReal("-ts_max_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
87bdad233fSMatthew Knepley     ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetInitialTime", ts->ptime, &ts->ptime, PETSC_NULL);CHKERRQ(ierr);
88bdad233fSMatthew Knepley     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetInitialTimeStep",ts->initial_time_step,&dt,&opt);CHKERRQ(ierr);
89a7cc72afSBarry Smith     if (opt) {
90bdad233fSMatthew Knepley       ts->initial_time_step = ts->time_step = dt;
91bdad233fSMatthew Knepley     }
92bdad233fSMatthew Knepley 
93bdad233fSMatthew Knepley     /* Monitor options */
94a6570f20SBarry Smith     ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
95eabae89aSBarry Smith     if (flg) {
96649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr);
97649052a6SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
98bdad233fSMatthew Knepley     }
9990d69ab7SBarry Smith     opt  = PETSC_FALSE;
100acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
101a7cc72afSBarry Smith     if (opt) {
102a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
103bdad233fSMatthew Knepley     }
10490d69ab7SBarry Smith     opt  = PETSC_FALSE;
105acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
106a7cc72afSBarry Smith     if (opt) {
107a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolution,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
108bdad233fSMatthew Knepley     }
109bdad233fSMatthew Knepley 
110bdad233fSMatthew Knepley     /* Handle TS type options */
111bdad233fSMatthew Knepley     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
112bdad233fSMatthew Knepley 
113bdad233fSMatthew Knepley     /* Handle specific TS options */
114abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
115bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
116bdad233fSMatthew Knepley     }
1175d973c19SBarry Smith 
1185d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
1195d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
120bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
121bdad233fSMatthew Knepley 
122089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
123bdad233fSMatthew Knepley   /* Handle subobject options */
124089b2837SJed Brown   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
125089b2837SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
126bdad233fSMatthew Knepley   PetscFunctionReturn(0);
127bdad233fSMatthew Knepley }
128bdad233fSMatthew Knepley 
129bdad233fSMatthew Knepley #undef  __FUNCT__
130bdad233fSMatthew Knepley #define __FUNCT__ "TSViewFromOptions"
131bdad233fSMatthew Knepley /*@
132bdad233fSMatthew Knepley   TSViewFromOptions - This function visualizes the ts based upon user options.
133bdad233fSMatthew Knepley 
134bdad233fSMatthew Knepley   Collective on TS
135bdad233fSMatthew Knepley 
136bdad233fSMatthew Knepley   Input Parameter:
137bdad233fSMatthew Knepley . ts - The ts
138bdad233fSMatthew Knepley 
139bdad233fSMatthew Knepley   Level: intermediate
140bdad233fSMatthew Knepley 
141bdad233fSMatthew Knepley .keywords: TS, view, options, database
142bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSView()
143bdad233fSMatthew Knepley @*/
1447087cfbeSBarry Smith PetscErrorCode  TSViewFromOptions(TS ts,const char title[])
145bdad233fSMatthew Knepley {
146bdad233fSMatthew Knepley   PetscViewer    viewer;
147bdad233fSMatthew Knepley   PetscDraw      draw;
148ace3abfcSBarry Smith   PetscBool      opt = PETSC_FALSE;
149e10c49a3SBarry Smith   char           fileName[PETSC_MAX_PATH_LEN];
150dfbe8321SBarry Smith   PetscErrorCode ierr;
151bdad233fSMatthew Knepley 
152bdad233fSMatthew Knepley   PetscFunctionBegin;
1537adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix, "-ts_view", fileName, PETSC_MAX_PATH_LEN, &opt);CHKERRQ(ierr);
154eabae89aSBarry Smith   if (opt && !PetscPreLoadingOn) {
1557adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,fileName,&viewer);CHKERRQ(ierr);
156bdad233fSMatthew Knepley     ierr = TSView(ts, viewer);CHKERRQ(ierr);
1576bf464f9SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
158bdad233fSMatthew Knepley   }
1598e83347fSKai Germaschewski   opt = PETSC_FALSE;
160acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix, "-ts_view_draw", &opt,PETSC_NULL);CHKERRQ(ierr);
161a7cc72afSBarry Smith   if (opt) {
1627adad957SLisandro Dalcin     ierr = PetscViewerDrawOpen(((PetscObject)ts)->comm, 0, 0, 0, 0, 300, 300, &viewer);CHKERRQ(ierr);
163bdad233fSMatthew Knepley     ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
164a7cc72afSBarry Smith     if (title) {
1651836bdbcSSatish Balay       ierr = PetscDrawSetTitle(draw, (char *)title);CHKERRQ(ierr);
166bdad233fSMatthew Knepley     } else {
167bdad233fSMatthew Knepley       ierr = PetscObjectName((PetscObject)ts);CHKERRQ(ierr);
1687adad957SLisandro Dalcin       ierr = PetscDrawSetTitle(draw, ((PetscObject)ts)->name);CHKERRQ(ierr);
169bdad233fSMatthew Knepley     }
170bdad233fSMatthew Knepley     ierr = TSView(ts, viewer);CHKERRQ(ierr);
171bdad233fSMatthew Knepley     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
172bdad233fSMatthew Knepley     ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1736bf464f9SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
174bdad233fSMatthew Knepley   }
175bdad233fSMatthew Knepley   PetscFunctionReturn(0);
176bdad233fSMatthew Knepley }
177bdad233fSMatthew Knepley 
178bdad233fSMatthew Knepley #undef __FUNCT__
1794a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
180a7a1495cSBarry Smith /*@
1818c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
182a7a1495cSBarry Smith       set with TSSetRHSJacobian().
183a7a1495cSBarry Smith 
184a7a1495cSBarry Smith    Collective on TS and Vec
185a7a1495cSBarry Smith 
186a7a1495cSBarry Smith    Input Parameters:
187316643e7SJed Brown +  ts - the TS context
188a7a1495cSBarry Smith .  t - current timestep
189a7a1495cSBarry Smith -  x - input vector
190a7a1495cSBarry Smith 
191a7a1495cSBarry Smith    Output Parameters:
192a7a1495cSBarry Smith +  A - Jacobian matrix
193a7a1495cSBarry Smith .  B - optional preconditioning matrix
194a7a1495cSBarry Smith -  flag - flag indicating matrix structure
195a7a1495cSBarry Smith 
196a7a1495cSBarry Smith    Notes:
197a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
198a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
199a7a1495cSBarry Smith 
20094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
201a7a1495cSBarry Smith    flag parameter.
202a7a1495cSBarry Smith 
203a7a1495cSBarry Smith    TSComputeJacobian() is valid only for TS_NONLINEAR
204a7a1495cSBarry Smith 
205a7a1495cSBarry Smith    Level: developer
206a7a1495cSBarry Smith 
207a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
208a7a1495cSBarry Smith 
20994b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
210a7a1495cSBarry Smith @*/
2117087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
212a7a1495cSBarry Smith {
213dfbe8321SBarry Smith   PetscErrorCode ierr;
214a7a1495cSBarry Smith 
215a7a1495cSBarry Smith   PetscFunctionBegin;
2160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2170700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
218c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
21917186662SBarry Smith   if (ts->problem_type != TS_NONLINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"For TS_NONLINEAR only");
220000e7ae3SMatthew Knepley   if (ts->ops->rhsjacobian) {
221d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
222a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
223a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
224000e7ae3SMatthew Knepley     ierr = (*ts->ops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr);
225a7a1495cSBarry Smith     PetscStackPop;
226d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
227a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
2280700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
2290700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
230ef66eb69SBarry Smith   } else {
231ef66eb69SBarry Smith     ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
232ef66eb69SBarry Smith     ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
233ef66eb69SBarry Smith     if (*A != *B) {
234ef66eb69SBarry Smith       ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
235ef66eb69SBarry Smith       ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
236ef66eb69SBarry Smith     }
237089b2837SJed Brown     *flg = ts->rhsmatstructure;
238ef66eb69SBarry Smith   }
239a7a1495cSBarry Smith   PetscFunctionReturn(0);
240a7a1495cSBarry Smith }
241a7a1495cSBarry Smith 
2424a2ae208SSatish Balay #undef __FUNCT__
2434a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
244316643e7SJed Brown /*@
245d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
246d763cef2SBarry Smith 
247316643e7SJed Brown    Collective on TS and Vec
248316643e7SJed Brown 
249316643e7SJed Brown    Input Parameters:
250316643e7SJed Brown +  ts - the TS context
251316643e7SJed Brown .  t - current time
252316643e7SJed Brown -  x - state vector
253316643e7SJed Brown 
254316643e7SJed Brown    Output Parameter:
255316643e7SJed Brown .  y - right hand side
256316643e7SJed Brown 
257316643e7SJed Brown    Note:
258316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
259316643e7SJed Brown    is used internally within the nonlinear solvers.
260316643e7SJed Brown 
261316643e7SJed Brown    If the user did not provide a function but merely a matrix,
262d763cef2SBarry Smith    this routine applies the matrix.
263316643e7SJed Brown 
264316643e7SJed Brown    Level: developer
265316643e7SJed Brown 
266316643e7SJed Brown .keywords: TS, compute
267316643e7SJed Brown 
268316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
269316643e7SJed Brown @*/
270dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
271d763cef2SBarry Smith {
272dfbe8321SBarry Smith   PetscErrorCode ierr;
273d763cef2SBarry Smith 
274d763cef2SBarry Smith   PetscFunctionBegin;
2750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2760700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2770700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
278d763cef2SBarry Smith 
279d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
280000e7ae3SMatthew Knepley   if (ts->ops->rhsfunction) {
281d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
282000e7ae3SMatthew Knepley     ierr = (*ts->ops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr);
283d763cef2SBarry Smith     PetscStackPop;
284089b2837SJed Brown   } else if (ts->ops->rhsmatrix) {
285089b2837SJed Brown     Mat A,B;
286089b2837SJed Brown     ierr = TSGetMatrices(ts,&A,&B,PETSC_NULL, PETSC_NULL,PETSC_NULL,PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
287089b2837SJed Brown     ts->rhsmatstructure = DIFFERENT_NONZERO_PATTERN;
288d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side matrix function");
289089b2837SJed Brown     ierr = (*ts->ops->rhsmatrix)(ts,t,&A,&B,&ts->rhsmatstructure,ts->jacP);CHKERRQ(ierr);
290d763cef2SBarry Smith     PetscStackPop;
291089b2837SJed Brown     /* call TSSetMatrices() in case the user changed the pointers */
292089b2837SJed Brown     ierr = TSSetMatrices(ts,A,B,PETSC_NULL, PETSC_NULL,PETSC_NULL,PETSC_NULL, PETSC_NULL,ts->jacP);CHKERRQ(ierr);
293089b2837SJed Brown     ierr = MatMult(A,x,y);CHKERRQ(ierr);
294d16dab79SJed Brown   } else SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"No RHS provided, must call TSSetRHSFunction() or TSSetMatrices()");
295d763cef2SBarry Smith 
296d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
297d763cef2SBarry Smith 
298d763cef2SBarry Smith   PetscFunctionReturn(0);
299d763cef2SBarry Smith }
300d763cef2SBarry Smith 
3014a2ae208SSatish Balay #undef __FUNCT__
302316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
303316643e7SJed Brown /*@
304316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
305316643e7SJed Brown 
306316643e7SJed Brown    Collective on TS and Vec
307316643e7SJed Brown 
308316643e7SJed Brown    Input Parameters:
309316643e7SJed Brown +  ts - the TS context
310316643e7SJed Brown .  t - current time
311316643e7SJed Brown .  X - state vector
312316643e7SJed Brown -  Xdot - time derivative of state vector
313316643e7SJed Brown 
314316643e7SJed Brown    Output Parameter:
315316643e7SJed Brown .  Y - right hand side
316316643e7SJed Brown 
317316643e7SJed Brown    Note:
318316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
319316643e7SJed Brown    is used internally within the nonlinear solvers.
320316643e7SJed Brown 
321316643e7SJed Brown    If the user did did not write their equations in implicit form, this
322316643e7SJed Brown    function recasts them in implicit form.
323316643e7SJed Brown 
324316643e7SJed Brown    Level: developer
325316643e7SJed Brown 
326316643e7SJed Brown .keywords: TS, compute
327316643e7SJed Brown 
328316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
329316643e7SJed Brown @*/
330316643e7SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y)
331316643e7SJed Brown {
332316643e7SJed Brown   PetscErrorCode ierr;
333316643e7SJed Brown 
334316643e7SJed Brown   PetscFunctionBegin;
3350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3360700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3370700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
3380700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
339316643e7SJed Brown 
340316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
341316643e7SJed Brown   if (ts->ops->ifunction) {
342316643e7SJed Brown     PetscStackPush("TS user implicit function");
343316643e7SJed Brown     ierr = (*ts->ops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr);
344316643e7SJed Brown     PetscStackPop;
345316643e7SJed Brown   } else {
346089b2837SJed Brown     ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr);
3474a6899ffSJed Brown     /* Convert to implicit form: F(X,Xdot) = Alhs * Xdot - Frhs(X) */
3484a6899ffSJed Brown     if (ts->ops->lhsmatrix) {
349089b2837SJed Brown       ts->lhsmatstructure = DIFFERENT_NONZERO_PATTERN;
3504a6899ffSJed Brown       PetscStackPush("TS user left-hand-side matrix function");
351089b2837SJed Brown       ierr = (*ts->ops->lhsmatrix)(ts,t,&ts->Alhs,&ts->Blhs,&ts->lhsmatstructure,ts->jacP);CHKERRQ(ierr);
3524a6899ffSJed Brown       PetscStackPop;
3534a6899ffSJed Brown       ierr = VecScale(Y,-1.);CHKERRQ(ierr);
3544a6899ffSJed Brown       ierr = MatMultAdd(ts->Alhs,Xdot,Y,Y);CHKERRQ(ierr);
3554a6899ffSJed Brown     } else {
356ace68cafSJed Brown       ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr);
357316643e7SJed Brown     }
3584a6899ffSJed Brown   }
359316643e7SJed Brown   ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
360316643e7SJed Brown   PetscFunctionReturn(0);
361316643e7SJed Brown }
362316643e7SJed Brown 
363316643e7SJed Brown #undef __FUNCT__
364316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
365316643e7SJed Brown /*@
366316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
367316643e7SJed Brown 
368316643e7SJed Brown    Collective on TS and Vec
369316643e7SJed Brown 
370316643e7SJed Brown    Input
371316643e7SJed Brown       Input Parameters:
372316643e7SJed Brown +  ts - the TS context
373316643e7SJed Brown .  t - current timestep
374316643e7SJed Brown .  X - state vector
375316643e7SJed Brown .  Xdot - time derivative of state vector
376316643e7SJed Brown -  shift - shift to apply, see note below
377316643e7SJed Brown 
378316643e7SJed Brown    Output Parameters:
379316643e7SJed Brown +  A - Jacobian matrix
380316643e7SJed Brown .  B - optional preconditioning matrix
381316643e7SJed Brown -  flag - flag indicating matrix structure
382316643e7SJed Brown 
383316643e7SJed Brown    Notes:
384316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
385316643e7SJed Brown 
386316643e7SJed Brown    dF/dX + shift*dF/dXdot
387316643e7SJed Brown 
388316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
389316643e7SJed Brown    is used internally within the nonlinear solvers.
390316643e7SJed Brown 
391316643e7SJed Brown    TSComputeIJacobian() is valid only for TS_NONLINEAR
392316643e7SJed Brown 
393316643e7SJed Brown    Level: developer
394316643e7SJed Brown 
395316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
396316643e7SJed Brown 
397316643e7SJed Brown .seealso:  TSSetIJacobian()
39863495f91SJed Brown @*/
3997087cfbeSBarry Smith PetscErrorCode  TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg)
400316643e7SJed Brown {
401316643e7SJed Brown   PetscErrorCode ierr;
402316643e7SJed Brown 
403316643e7SJed Brown   PetscFunctionBegin;
4040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4050700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4060700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
407316643e7SJed Brown   PetscValidPointer(A,6);
4080700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
409316643e7SJed Brown   PetscValidPointer(B,7);
4100700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
411316643e7SJed Brown   PetscValidPointer(flg,8);
412316643e7SJed Brown 
4134a6899ffSJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case it we're solving a linear problem in which case it wouldn't get initialized below. */
414316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
415316643e7SJed Brown   if (ts->ops->ijacobian) {
416316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
417316643e7SJed Brown     ierr = (*ts->ops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr);
418316643e7SJed Brown     PetscStackPop;
419316643e7SJed Brown   } else {
420316643e7SJed Brown     if (ts->ops->rhsjacobian) {
421316643e7SJed Brown       PetscStackPush("TS user right-hand-side Jacobian");
422316643e7SJed Brown       ierr = (*ts->ops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr);
423316643e7SJed Brown       PetscStackPop;
424316643e7SJed Brown     } else {
425316643e7SJed Brown       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
426316643e7SJed Brown       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
427316643e7SJed Brown       if (*A != *B) {
428316643e7SJed Brown         ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
429316643e7SJed Brown         ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
430316643e7SJed Brown       }
431316643e7SJed Brown     }
432316643e7SJed Brown 
433316643e7SJed Brown     /* Convert to implicit form */
434316643e7SJed Brown     /* inefficient because these operations will normally traverse all matrix elements twice */
435316643e7SJed Brown     ierr = MatScale(*A,-1);CHKERRQ(ierr);
4364a6899ffSJed Brown     if (ts->Alhs) {
4374a6899ffSJed Brown       ierr = MatAXPY(*A,shift,ts->Alhs,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
4384a6899ffSJed Brown     } else {
439316643e7SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
4404a6899ffSJed Brown     }
441316643e7SJed Brown     if (*A != *B) {
442316643e7SJed Brown       ierr = MatScale(*B,-1);CHKERRQ(ierr);
443316643e7SJed Brown       ierr = MatShift(*B,shift);CHKERRQ(ierr);
444316643e7SJed Brown     }
445316643e7SJed Brown   }
446316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
447316643e7SJed Brown   PetscFunctionReturn(0);
448316643e7SJed Brown }
449316643e7SJed Brown 
450316643e7SJed Brown #undef __FUNCT__
4514a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
452d763cef2SBarry Smith /*@C
453d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
454d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
455d763cef2SBarry Smith 
4563f9fe445SBarry Smith     Logically Collective on TS
457d763cef2SBarry Smith 
458d763cef2SBarry Smith     Input Parameters:
459d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
460*ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
461d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
462d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
463d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
464d763cef2SBarry Smith 
465d763cef2SBarry Smith     Calling sequence of func:
46687828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
467d763cef2SBarry Smith 
468d763cef2SBarry Smith +   t - current timestep
469d763cef2SBarry Smith .   u - input vector
470d763cef2SBarry Smith .   F - function vector
471d763cef2SBarry Smith -   ctx - [optional] user-defined function context
472d763cef2SBarry Smith 
473d763cef2SBarry Smith     Important:
47476f2fa84SHong Zhang     The user MUST call either this routine or TSSetMatrices().
475d763cef2SBarry Smith 
476d763cef2SBarry Smith     Level: beginner
477d763cef2SBarry Smith 
478d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
479d763cef2SBarry Smith 
48076f2fa84SHong Zhang .seealso: TSSetMatrices()
481d763cef2SBarry Smith @*/
482089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
483d763cef2SBarry Smith {
484089b2837SJed Brown   PetscErrorCode ierr;
485089b2837SJed Brown   SNES           snes;
486d763cef2SBarry Smith 
487089b2837SJed Brown   PetscFunctionBegin;
4880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
489*ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
49017186662SBarry Smith   if (ts->problem_type == TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot set function for linear problem");
491000e7ae3SMatthew Knepley   ts->ops->rhsfunction = f;
492d763cef2SBarry Smith   ts->funP             = ctx;
493089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
494089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
495d763cef2SBarry Smith   PetscFunctionReturn(0);
496d763cef2SBarry Smith }
497d763cef2SBarry Smith 
4984a2ae208SSatish Balay #undef __FUNCT__
49995f0b562SHong Zhang #define __FUNCT__ "TSSetMatrices"
50095f0b562SHong Zhang /*@C
50195f0b562SHong Zhang    TSSetMatrices - Sets the functions to compute the matrices Alhs and Arhs,
50295f0b562SHong Zhang    where Alhs(t) U_t = Arhs(t) U.
50395f0b562SHong Zhang 
5043f9fe445SBarry Smith    Logically Collective on TS
50595f0b562SHong Zhang 
50695f0b562SHong Zhang    Input Parameters:
50795f0b562SHong Zhang +  ts   - the TS context obtained from TSCreate()
50895f0b562SHong Zhang .  Arhs - matrix
509089b2837SJed Brown .  Brhs - preconditioning matrix
510089b2837SJed Brown .  frhs - the matrix evaluation routine for Arhs and Brhs; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran)
51195f0b562SHong Zhang           if Arhs is not a function of t.
51295f0b562SHong Zhang .  Alhs - matrix or PETSC_NULL if Alhs is an indentity matrix.
513089b2837SJed Brown .  Blhs - preconditioning matrix or PETSC_NULL if Blhs is an identity matrix
51495f0b562SHong Zhang .  flhs - the matrix evaluation routine for Alhs; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran)
51595f0b562SHong Zhang           if Alhs is not a function of t.
51695f0b562SHong Zhang .  flag - flag indicating information about the matrix structure of Arhs and Alhs.
51795f0b562SHong Zhang           The available options are
51895f0b562SHong Zhang             SAME_NONZERO_PATTERN - Alhs has the same nonzero structure as Arhs
51995f0b562SHong Zhang             DIFFERENT_NONZERO_PATTERN - Alhs has different nonzero structure as Arhs
52095f0b562SHong Zhang -  ctx  - [optional] user-defined context for private data for the
52195f0b562SHong Zhang           matrix evaluation routine (may be PETSC_NULL)
52295f0b562SHong Zhang 
52395f0b562SHong Zhang    Calling sequence of func:
52495f0b562SHong Zhang $     func(TS ts,PetscReal t,Mat *A,Mat *B,PetscInt *flag,void *ctx);
52595f0b562SHong Zhang 
52695f0b562SHong Zhang +  t - current timestep
52795f0b562SHong Zhang .  A - matrix A, where U_t = A(t) U
52895f0b562SHong Zhang .  B - preconditioner matrix, usually the same as A
52995f0b562SHong Zhang .  flag - flag indicating information about the preconditioner matrix
53095f0b562SHong Zhang           structure (same as flag in KSPSetOperators())
53195f0b562SHong Zhang -  ctx - [optional] user-defined context for matrix evaluation routine
53295f0b562SHong Zhang 
53395f0b562SHong Zhang    Notes:
53495f0b562SHong Zhang    The routine func() takes Mat* as the matrix arguments rather than Mat.
53595f0b562SHong Zhang    This allows the matrix evaluation routine to replace Arhs or Alhs with a
53695f0b562SHong Zhang    completely new new matrix structure (not just different matrix elements)
53795f0b562SHong Zhang    when appropriate, for instance, if the nonzero structure is changing
53895f0b562SHong Zhang    throughout the global iterations.
53995f0b562SHong Zhang 
54095f0b562SHong Zhang    Important:
54195f0b562SHong Zhang    The user MUST call either this routine or TSSetRHSFunction().
54295f0b562SHong Zhang 
54395f0b562SHong Zhang    Level: beginner
54495f0b562SHong Zhang 
54595f0b562SHong Zhang .keywords: TS, timestep, set, matrix
54695f0b562SHong Zhang 
54795f0b562SHong Zhang .seealso: TSSetRHSFunction()
54895f0b562SHong Zhang @*/
549089b2837SJed Brown PetscErrorCode  TSSetMatrices(TS ts,Mat Arhs,Mat Brhs,PetscErrorCode (*frhs)(TS,PetscReal,Mat*,Mat*,MatStructure*,void*),Mat Alhs,Mat Blhs,PetscErrorCode (*flhs)(TS,PetscReal,Mat*,Mat*,MatStructure*,void*),MatStructure flag,void *ctx)
55095f0b562SHong Zhang {
551277b19d0SLisandro Dalcin   PetscErrorCode ierr;
552089b2837SJed Brown   SNES           snes;
553277b19d0SLisandro Dalcin 
55495f0b562SHong Zhang   PetscFunctionBegin;
5550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
556277b19d0SLisandro Dalcin   if (frhs) ts->ops->rhsmatrix = frhs;
557277b19d0SLisandro Dalcin   if (flhs) ts->ops->lhsmatrix = flhs;
558277b19d0SLisandro Dalcin   if (ctx)  ts->jacP           = ctx;
559089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
560089b2837SJed Brown   ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
561089b2837SJed Brown   ierr = SNESSetJacobian(snes,Arhs,Brhs,SNESTSFormJacobian,ts);CHKERRQ(ierr);
56292af4f6aSHong Zhang   if (Alhs) {
5630700a824SBarry Smith     PetscValidHeaderSpecific(Alhs,MAT_CLASSID,4);
56492af4f6aSHong Zhang     PetscCheckSameComm(ts,1,Alhs,4);
565277b19d0SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)Alhs);CHKERRQ(ierr);
5666bf464f9SBarry Smith     ierr = MatDestroy(&ts->Alhs);CHKERRQ(ierr);
56795f0b562SHong Zhang     ts->Alhs = Alhs;
56892af4f6aSHong Zhang   }
569089b2837SJed Brown   if (Blhs) {
570089b2837SJed Brown     PetscValidHeaderSpecific(Blhs,MAT_CLASSID,4);
571089b2837SJed Brown     PetscCheckSameComm(ts,1,Blhs,4);
572089b2837SJed Brown     ierr = PetscObjectReference((PetscObject)Blhs);CHKERRQ(ierr);
573089b2837SJed Brown     ierr = MatDestroy(&ts->Blhs);CHKERRQ(ierr);
574089b2837SJed Brown     ts->Blhs = Blhs;
575089b2837SJed Brown   }
576089b2837SJed Brown   ts->rhsmatstructure = flag;
577089b2837SJed Brown   ts->lhsmatstructure = flag;
57895f0b562SHong Zhang   PetscFunctionReturn(0);
57995f0b562SHong Zhang }
580d763cef2SBarry Smith 
581aa644b49SHong Zhang #undef __FUNCT__
582cda39b92SHong Zhang #define __FUNCT__ "TSGetMatrices"
583cda39b92SHong Zhang /*@C
584cda39b92SHong Zhang    TSGetMatrices - Returns the matrices Arhs and Alhs at the present timestep,
585cda39b92SHong Zhang    where Alhs(t) U_t = Arhs(t) U.
586cda39b92SHong Zhang 
587cda39b92SHong Zhang    Not Collective, but parallel objects are returned if TS is parallel
588cda39b92SHong Zhang 
589cda39b92SHong Zhang    Input Parameter:
590cda39b92SHong Zhang .  ts  - The TS context obtained from TSCreate()
591cda39b92SHong Zhang 
592cda39b92SHong Zhang    Output Parameters:
593cda39b92SHong Zhang +  Arhs - The right-hand side matrix
594cda39b92SHong Zhang .  Alhs - The left-hand side matrix
595cda39b92SHong Zhang -  ctx - User-defined context for matrix evaluation routine
596cda39b92SHong Zhang 
597cda39b92SHong Zhang    Notes: You can pass in PETSC_NULL for any return argument you do not need.
598cda39b92SHong Zhang 
599cda39b92SHong Zhang    Level: intermediate
600cda39b92SHong Zhang 
601cda39b92SHong Zhang .seealso: TSSetMatrices(), TSGetTimeStep(), TSGetTime(), TSGetTimeStepNumber(), TSGetRHSJacobian()
602cda39b92SHong Zhang 
603cda39b92SHong Zhang .keywords: TS, timestep, get, matrix
604cda39b92SHong Zhang 
605cda39b92SHong Zhang @*/
606089b2837SJed Brown PetscErrorCode  TSGetMatrices(TS ts,Mat *Arhs,Mat *Brhs,TSMatrix *frhs,Mat *Alhs,Mat *Blhs,TSMatrix *flhs,void **ctx)
607cda39b92SHong Zhang {
608089b2837SJed Brown   PetscErrorCode ierr;
609089b2837SJed Brown   SNES           snes;
610089b2837SJed Brown 
611cda39b92SHong Zhang   PetscFunctionBegin;
6120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
613089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
614089b2837SJed Brown   ierr = SNESGetJacobian(snes,Arhs,Brhs,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
615089b2837SJed Brown   if (frhs) *frhs = ts->ops->rhsmatrix;
616cda39b92SHong Zhang   if (Alhs) *Alhs = ts->Alhs;
617089b2837SJed Brown   if (Blhs) *Blhs = ts->Blhs;
618089b2837SJed Brown   if (flhs) *flhs = ts->ops->lhsmatrix;
619cda39b92SHong Zhang   if (ctx)  *ctx = ts->jacP;
620cda39b92SHong Zhang   PetscFunctionReturn(0);
621cda39b92SHong Zhang }
622cda39b92SHong Zhang 
623cda39b92SHong Zhang #undef __FUNCT__
6244a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
625d763cef2SBarry Smith /*@C
626d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
627d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
62876f2fa84SHong Zhang    Use TSSetMatrices() for linear problems.
629d763cef2SBarry Smith 
6303f9fe445SBarry Smith    Logically Collective on TS
631d763cef2SBarry Smith 
632d763cef2SBarry Smith    Input Parameters:
633d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
634d763cef2SBarry Smith .  A   - Jacobian matrix
635d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
636d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
637d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
638d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
639d763cef2SBarry Smith 
640d763cef2SBarry Smith    Calling sequence of func:
64187828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
642d763cef2SBarry Smith 
643d763cef2SBarry Smith +  t - current timestep
644d763cef2SBarry Smith .  u - input vector
645d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
646d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
647d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
64894b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
649d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
650d763cef2SBarry Smith 
651d763cef2SBarry Smith    Notes:
65294b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
653d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
654d763cef2SBarry Smith 
655d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
656d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
65756335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
658d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
659d763cef2SBarry Smith    throughout the global iterations.
660d763cef2SBarry Smith 
661d763cef2SBarry Smith    Level: beginner
662d763cef2SBarry Smith 
663d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
664d763cef2SBarry Smith 
665d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(),
66676f2fa84SHong Zhang           SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices()
667d763cef2SBarry Smith 
668d763cef2SBarry Smith @*/
669089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
670d763cef2SBarry Smith {
671277b19d0SLisandro Dalcin   PetscErrorCode ierr;
672089b2837SJed Brown   SNES           snes;
673277b19d0SLisandro Dalcin 
674d763cef2SBarry Smith   PetscFunctionBegin;
6750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
676277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
677277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
678277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
679277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
68017186662SBarry Smith   if (ts->problem_type != TS_NONLINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not for linear problems; use TSSetMatrices()");
681d763cef2SBarry Smith 
682277b19d0SLisandro Dalcin   if (f)   ts->ops->rhsjacobian = f;
683277b19d0SLisandro Dalcin   if (ctx) ts->jacP             = ctx;
684089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
685089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
686d763cef2SBarry Smith   PetscFunctionReturn(0);
687d763cef2SBarry Smith }
688d763cef2SBarry Smith 
689316643e7SJed Brown 
690316643e7SJed Brown #undef __FUNCT__
691316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
692316643e7SJed Brown /*@C
693316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
694316643e7SJed Brown 
6953f9fe445SBarry Smith    Logically Collective on TS
696316643e7SJed Brown 
697316643e7SJed Brown    Input Parameters:
698316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
699*ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
700316643e7SJed Brown .  f   - the function evaluation routine
701316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
702316643e7SJed Brown 
703316643e7SJed Brown    Calling sequence of f:
704316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
705316643e7SJed Brown 
706316643e7SJed Brown +  t   - time at step/stage being solved
707316643e7SJed Brown .  u   - state vector
708316643e7SJed Brown .  u_t - time derivative of state vector
709316643e7SJed Brown .  F   - function vector
710316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
711316643e7SJed Brown 
712316643e7SJed Brown    Important:
713316643e7SJed Brown    The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices().  This routine must be used when not solving an ODE.
714316643e7SJed Brown 
715316643e7SJed Brown    Level: beginner
716316643e7SJed Brown 
717316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
718316643e7SJed Brown 
719316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian()
720316643e7SJed Brown @*/
721089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
722316643e7SJed Brown {
723089b2837SJed Brown   PetscErrorCode ierr;
724089b2837SJed Brown   SNES           snes;
725316643e7SJed Brown 
726316643e7SJed Brown   PetscFunctionBegin;
7270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
728*ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
729089b2837SJed Brown   if (f)   ts->ops->ifunction = f;
730089b2837SJed Brown   if (ctx) ts->funP           = ctx;
731089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
732089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
733089b2837SJed Brown   PetscFunctionReturn(0);
734089b2837SJed Brown }
735089b2837SJed Brown 
736089b2837SJed Brown #undef __FUNCT__
737089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
738089b2837SJed Brown /*@C
739089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
740089b2837SJed Brown 
741089b2837SJed Brown    Not Collective
742089b2837SJed Brown 
743089b2837SJed Brown    Input Parameter:
744089b2837SJed Brown .  ts - the TS context
745089b2837SJed Brown 
746089b2837SJed Brown    Output Parameter:
747089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
748089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
749089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
750089b2837SJed Brown 
751089b2837SJed Brown    Level: advanced
752089b2837SJed Brown 
753089b2837SJed Brown .keywords: TS, nonlinear, get, function
754089b2837SJed Brown 
755089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
756089b2837SJed Brown @*/
757089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
758089b2837SJed Brown {
759089b2837SJed Brown   PetscErrorCode ierr;
760089b2837SJed Brown   SNES snes;
761089b2837SJed Brown 
762089b2837SJed Brown   PetscFunctionBegin;
763089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
764089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
765089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
766089b2837SJed Brown   if (func) *func = ts->ops->ifunction;
767089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
768089b2837SJed Brown   PetscFunctionReturn(0);
769089b2837SJed Brown }
770089b2837SJed Brown 
771089b2837SJed Brown #undef __FUNCT__
772089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
773089b2837SJed Brown /*@C
774089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
775089b2837SJed Brown 
776089b2837SJed Brown    Not Collective
777089b2837SJed Brown 
778089b2837SJed Brown    Input Parameter:
779089b2837SJed Brown .  ts - the TS context
780089b2837SJed Brown 
781089b2837SJed Brown    Output Parameter:
782089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
783089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
784089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
785089b2837SJed Brown 
786089b2837SJed Brown    Level: advanced
787089b2837SJed Brown 
788089b2837SJed Brown .keywords: TS, nonlinear, get, function
789089b2837SJed Brown 
790089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
791089b2837SJed Brown @*/
792089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
793089b2837SJed Brown {
794089b2837SJed Brown   PetscErrorCode ierr;
795089b2837SJed Brown   SNES snes;
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);
801089b2837SJed Brown   if (func) *func = ts->ops->rhsfunction;
802089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
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 
848316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian()
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;
855316643e7SJed Brown 
856316643e7SJed Brown   PetscFunctionBegin;
8570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8580700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
8590700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
860316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
861316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
862316643e7SJed Brown   if (f)   ts->ops->ijacobian = f;
863316643e7SJed Brown   if (ctx) ts->jacP           = ctx;
864089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
865089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
866316643e7SJed Brown   PetscFunctionReturn(0);
867316643e7SJed Brown }
868316643e7SJed Brown 
8694a2ae208SSatish Balay #undef __FUNCT__
8704a2ae208SSatish Balay #define __FUNCT__ "TSView"
8717e2c5f70SBarry Smith /*@C
872d763cef2SBarry Smith     TSView - Prints the TS data structure.
873d763cef2SBarry Smith 
8744c49b128SBarry Smith     Collective on TS
875d763cef2SBarry Smith 
876d763cef2SBarry Smith     Input Parameters:
877d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
878d763cef2SBarry Smith -   viewer - visualization context
879d763cef2SBarry Smith 
880d763cef2SBarry Smith     Options Database Key:
881d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
882d763cef2SBarry Smith 
883d763cef2SBarry Smith     Notes:
884d763cef2SBarry Smith     The available visualization contexts include
885b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
886b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
887d763cef2SBarry Smith          output where only the first processor opens
888d763cef2SBarry Smith          the file.  All other processors send their
889d763cef2SBarry Smith          data to the first processor to print.
890d763cef2SBarry Smith 
891d763cef2SBarry Smith     The user can open an alternative visualization context with
892b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
893d763cef2SBarry Smith 
894d763cef2SBarry Smith     Level: beginner
895d763cef2SBarry Smith 
896d763cef2SBarry Smith .keywords: TS, timestep, view
897d763cef2SBarry Smith 
898b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
899d763cef2SBarry Smith @*/
9007087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
901d763cef2SBarry Smith {
902dfbe8321SBarry Smith   PetscErrorCode ierr;
903a313700dSBarry Smith   const TSType   type;
904089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
905d763cef2SBarry Smith 
906d763cef2SBarry Smith   PetscFunctionBegin;
9070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9083050cee2SBarry Smith   if (!viewer) {
9097adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
9103050cee2SBarry Smith   }
9110700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
912c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
913fd16b177SBarry Smith 
9142692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
9152692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
91632077d6dSBarry Smith   if (iascii) {
917317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
918000e7ae3SMatthew Knepley     if (ts->ops->view) {
919b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
920000e7ae3SMatthew Knepley       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
921b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
922d763cef2SBarry Smith     }
92377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
924a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
925d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
92677431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr);
927d763cef2SBarry Smith     }
92877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr);
9290f5bd95cSBarry Smith   } else if (isstring) {
930a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
931b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
932d763cef2SBarry Smith   }
933b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
934089b2837SJed Brown   ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
935089b2837SJed Brown   if (!isundials && ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
936b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
937d763cef2SBarry Smith   PetscFunctionReturn(0);
938d763cef2SBarry Smith }
939d763cef2SBarry Smith 
940d763cef2SBarry Smith 
9414a2ae208SSatish Balay #undef __FUNCT__
9424a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
943b07ff414SBarry Smith /*@
944d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
945d763cef2SBarry Smith    the timesteppers.
946d763cef2SBarry Smith 
9473f9fe445SBarry Smith    Logically Collective on TS
948d763cef2SBarry Smith 
949d763cef2SBarry Smith    Input Parameters:
950d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
951d763cef2SBarry Smith -  usrP - optional user context
952d763cef2SBarry Smith 
953d763cef2SBarry Smith    Level: intermediate
954d763cef2SBarry Smith 
955d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
956d763cef2SBarry Smith 
957d763cef2SBarry Smith .seealso: TSGetApplicationContext()
958d763cef2SBarry Smith @*/
9597087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
960d763cef2SBarry Smith {
961d763cef2SBarry Smith   PetscFunctionBegin;
9620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
963d763cef2SBarry Smith   ts->user = usrP;
964d763cef2SBarry Smith   PetscFunctionReturn(0);
965d763cef2SBarry Smith }
966d763cef2SBarry Smith 
9674a2ae208SSatish Balay #undef __FUNCT__
9684a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
969b07ff414SBarry Smith /*@
970d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
971d763cef2SBarry Smith     timestepper.
972d763cef2SBarry Smith 
973d763cef2SBarry Smith     Not Collective
974d763cef2SBarry Smith 
975d763cef2SBarry Smith     Input Parameter:
976d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
977d763cef2SBarry Smith 
978d763cef2SBarry Smith     Output Parameter:
979d763cef2SBarry Smith .   usrP - user context
980d763cef2SBarry Smith 
981d763cef2SBarry Smith     Level: intermediate
982d763cef2SBarry Smith 
983d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
984d763cef2SBarry Smith 
985d763cef2SBarry Smith .seealso: TSSetApplicationContext()
986d763cef2SBarry Smith @*/
987e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
988d763cef2SBarry Smith {
989d763cef2SBarry Smith   PetscFunctionBegin;
9900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
991e71120c6SJed Brown   *(void**)usrP = ts->user;
992d763cef2SBarry Smith   PetscFunctionReturn(0);
993d763cef2SBarry Smith }
994d763cef2SBarry Smith 
9954a2ae208SSatish Balay #undef __FUNCT__
9964a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
997d763cef2SBarry Smith /*@
998d763cef2SBarry Smith    TSGetTimeStepNumber - Gets the current number of timesteps.
999d763cef2SBarry Smith 
1000d763cef2SBarry Smith    Not Collective
1001d763cef2SBarry Smith 
1002d763cef2SBarry Smith    Input Parameter:
1003d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1004d763cef2SBarry Smith 
1005d763cef2SBarry Smith    Output Parameter:
1006d763cef2SBarry Smith .  iter - number steps so far
1007d763cef2SBarry Smith 
1008d763cef2SBarry Smith    Level: intermediate
1009d763cef2SBarry Smith 
1010d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1011d763cef2SBarry Smith @*/
10127087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
1013d763cef2SBarry Smith {
1014d763cef2SBarry Smith   PetscFunctionBegin;
10150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10164482741eSBarry Smith   PetscValidIntPointer(iter,2);
1017d763cef2SBarry Smith   *iter = ts->steps;
1018d763cef2SBarry Smith   PetscFunctionReturn(0);
1019d763cef2SBarry Smith }
1020d763cef2SBarry Smith 
10214a2ae208SSatish Balay #undef __FUNCT__
10224a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1023d763cef2SBarry Smith /*@
1024d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1025d763cef2SBarry Smith    as well as the initial time.
1026d763cef2SBarry Smith 
10273f9fe445SBarry Smith    Logically Collective on TS
1028d763cef2SBarry Smith 
1029d763cef2SBarry Smith    Input Parameters:
1030d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1031d763cef2SBarry Smith .  initial_time - the initial time
1032d763cef2SBarry Smith -  time_step - the size of the timestep
1033d763cef2SBarry Smith 
1034d763cef2SBarry Smith    Level: intermediate
1035d763cef2SBarry Smith 
1036d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1037d763cef2SBarry Smith 
1038d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1039d763cef2SBarry Smith @*/
10407087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1041d763cef2SBarry Smith {
1042d763cef2SBarry Smith   PetscFunctionBegin;
10430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1044d763cef2SBarry Smith   ts->time_step         = time_step;
1045d763cef2SBarry Smith   ts->initial_time_step = time_step;
1046d763cef2SBarry Smith   ts->ptime             = initial_time;
1047d763cef2SBarry Smith   PetscFunctionReturn(0);
1048d763cef2SBarry Smith }
1049d763cef2SBarry Smith 
10504a2ae208SSatish Balay #undef __FUNCT__
10514a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1052d763cef2SBarry Smith /*@
1053d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1054d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1055d763cef2SBarry Smith 
10563f9fe445SBarry Smith    Logically Collective on TS
1057d763cef2SBarry Smith 
1058d763cef2SBarry Smith    Input Parameters:
1059d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1060d763cef2SBarry Smith -  time_step - the size of the timestep
1061d763cef2SBarry Smith 
1062d763cef2SBarry Smith    Level: intermediate
1063d763cef2SBarry Smith 
1064d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1065d763cef2SBarry Smith 
1066d763cef2SBarry Smith .keywords: TS, set, timestep
1067d763cef2SBarry Smith @*/
10687087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1069d763cef2SBarry Smith {
1070d763cef2SBarry Smith   PetscFunctionBegin;
10710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1072c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1073d763cef2SBarry Smith   ts->time_step = time_step;
1074d763cef2SBarry Smith   PetscFunctionReturn(0);
1075d763cef2SBarry Smith }
1076d763cef2SBarry Smith 
10774a2ae208SSatish Balay #undef __FUNCT__
10784a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1079d763cef2SBarry Smith /*@
1080d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1081d763cef2SBarry Smith 
1082d763cef2SBarry Smith    Not Collective
1083d763cef2SBarry Smith 
1084d763cef2SBarry Smith    Input Parameter:
1085d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1086d763cef2SBarry Smith 
1087d763cef2SBarry Smith    Output Parameter:
1088d763cef2SBarry Smith .  dt - the current timestep size
1089d763cef2SBarry Smith 
1090d763cef2SBarry Smith    Level: intermediate
1091d763cef2SBarry Smith 
1092d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1093d763cef2SBarry Smith 
1094d763cef2SBarry Smith .keywords: TS, get, timestep
1095d763cef2SBarry Smith @*/
10967087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1097d763cef2SBarry Smith {
1098d763cef2SBarry Smith   PetscFunctionBegin;
10990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11004482741eSBarry Smith   PetscValidDoublePointer(dt,2);
1101d763cef2SBarry Smith   *dt = ts->time_step;
1102d763cef2SBarry Smith   PetscFunctionReturn(0);
1103d763cef2SBarry Smith }
1104d763cef2SBarry Smith 
11054a2ae208SSatish Balay #undef __FUNCT__
11064a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1107d8e5e3e6SSatish Balay /*@
1108d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1109d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1110d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1111d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1112d763cef2SBarry Smith 
1113d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1114d763cef2SBarry Smith 
1115d763cef2SBarry Smith    Input Parameter:
1116d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1117d763cef2SBarry Smith 
1118d763cef2SBarry Smith    Output Parameter:
1119d763cef2SBarry Smith .  v - the vector containing the solution
1120d763cef2SBarry Smith 
1121d763cef2SBarry Smith    Level: intermediate
1122d763cef2SBarry Smith 
1123d763cef2SBarry Smith .seealso: TSGetTimeStep()
1124d763cef2SBarry Smith 
1125d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1126d763cef2SBarry Smith @*/
11277087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1128d763cef2SBarry Smith {
1129d763cef2SBarry Smith   PetscFunctionBegin;
11300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11314482741eSBarry Smith   PetscValidPointer(v,2);
11328737fe31SLisandro Dalcin   *v = ts->vec_sol;
1133d763cef2SBarry Smith   PetscFunctionReturn(0);
1134d763cef2SBarry Smith }
1135d763cef2SBarry Smith 
1136bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
11374a2ae208SSatish Balay #undef __FUNCT__
1138bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1139d8e5e3e6SSatish Balay /*@
1140bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1141d763cef2SBarry Smith 
1142bdad233fSMatthew Knepley   Not collective
1143d763cef2SBarry Smith 
1144bdad233fSMatthew Knepley   Input Parameters:
1145bdad233fSMatthew Knepley + ts   - The TS
1146bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1147d763cef2SBarry Smith .vb
1148d763cef2SBarry Smith          U_t = A U
1149d763cef2SBarry Smith          U_t = A(t) U
1150d763cef2SBarry Smith          U_t = F(t,U)
1151d763cef2SBarry Smith .ve
1152d763cef2SBarry Smith 
1153d763cef2SBarry Smith    Level: beginner
1154d763cef2SBarry Smith 
1155bdad233fSMatthew Knepley .keywords: TS, problem type
1156bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1157d763cef2SBarry Smith @*/
11587087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1159a7cc72afSBarry Smith {
1160d763cef2SBarry Smith   PetscFunctionBegin;
11610700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1162bdad233fSMatthew Knepley   ts->problem_type = type;
1163d763cef2SBarry Smith   PetscFunctionReturn(0);
1164d763cef2SBarry Smith }
1165d763cef2SBarry Smith 
1166bdad233fSMatthew Knepley #undef __FUNCT__
1167bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1168bdad233fSMatthew Knepley /*@C
1169bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1170bdad233fSMatthew Knepley 
1171bdad233fSMatthew Knepley   Not collective
1172bdad233fSMatthew Knepley 
1173bdad233fSMatthew Knepley   Input Parameter:
1174bdad233fSMatthew Knepley . ts   - The TS
1175bdad233fSMatthew Knepley 
1176bdad233fSMatthew Knepley   Output Parameter:
1177bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1178bdad233fSMatthew Knepley .vb
1179089b2837SJed Brown          M U_t = A U
1180089b2837SJed Brown          M(t) U_t = A(t) U
1181bdad233fSMatthew Knepley          U_t = F(t,U)
1182bdad233fSMatthew Knepley .ve
1183bdad233fSMatthew Knepley 
1184bdad233fSMatthew Knepley    Level: beginner
1185bdad233fSMatthew Knepley 
1186bdad233fSMatthew Knepley .keywords: TS, problem type
1187bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1188bdad233fSMatthew Knepley @*/
11897087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1190a7cc72afSBarry Smith {
1191bdad233fSMatthew Knepley   PetscFunctionBegin;
11920700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
11934482741eSBarry Smith   PetscValidIntPointer(type,2);
1194bdad233fSMatthew Knepley   *type = ts->problem_type;
1195bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1196bdad233fSMatthew Knepley }
1197d763cef2SBarry Smith 
11984a2ae208SSatish Balay #undef __FUNCT__
11994a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1200d763cef2SBarry Smith /*@
1201d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1202d763cef2SBarry Smith    of a timestepper.
1203d763cef2SBarry Smith 
1204d763cef2SBarry Smith    Collective on TS
1205d763cef2SBarry Smith 
1206d763cef2SBarry Smith    Input Parameter:
1207d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1208d763cef2SBarry Smith 
1209d763cef2SBarry Smith    Notes:
1210d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1211d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1212d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1213d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1214d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1215d763cef2SBarry Smith 
1216d763cef2SBarry Smith    Level: advanced
1217d763cef2SBarry Smith 
1218d763cef2SBarry Smith .keywords: TS, timestep, setup
1219d763cef2SBarry Smith 
1220d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1221d763cef2SBarry Smith @*/
12227087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1223d763cef2SBarry Smith {
1224dfbe8321SBarry Smith   PetscErrorCode ierr;
1225d763cef2SBarry Smith 
1226d763cef2SBarry Smith   PetscFunctionBegin;
12270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1228277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1229277b19d0SLisandro Dalcin 
12307adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
12319596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1232d763cef2SBarry Smith   }
1233277b19d0SLisandro Dalcin 
1234277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1235277b19d0SLisandro Dalcin 
1236277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1237000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1238277b19d0SLisandro Dalcin   }
1239277b19d0SLisandro Dalcin 
1240277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1241277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1242277b19d0SLisandro Dalcin }
1243277b19d0SLisandro Dalcin 
1244277b19d0SLisandro Dalcin #undef __FUNCT__
1245277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1246277b19d0SLisandro Dalcin /*@
1247277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1248277b19d0SLisandro Dalcin 
1249277b19d0SLisandro Dalcin    Collective on TS
1250277b19d0SLisandro Dalcin 
1251277b19d0SLisandro Dalcin    Input Parameter:
1252277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1253277b19d0SLisandro Dalcin 
1254277b19d0SLisandro Dalcin    Level: beginner
1255277b19d0SLisandro Dalcin 
1256277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1257277b19d0SLisandro Dalcin 
1258277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1259277b19d0SLisandro Dalcin @*/
1260277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1261277b19d0SLisandro Dalcin {
1262277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1263277b19d0SLisandro Dalcin 
1264277b19d0SLisandro Dalcin   PetscFunctionBegin;
1265277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1266277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1267277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1268277b19d0SLisandro Dalcin   }
1269277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
12706bf464f9SBarry Smith   ierr = MatDestroy(&ts->Alhs);CHKERRQ(ierr);
1271089b2837SJed Brown   ierr = MatDestroy(&ts->Blhs);CHKERRQ(ierr);
12726bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1273277b19d0SLisandro Dalcin   if (ts->work) {ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);}
1274277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1275d763cef2SBarry Smith   PetscFunctionReturn(0);
1276d763cef2SBarry Smith }
1277d763cef2SBarry Smith 
12784a2ae208SSatish Balay #undef __FUNCT__
12794a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1280d8e5e3e6SSatish Balay /*@
1281d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1282d763cef2SBarry Smith    with TSCreate().
1283d763cef2SBarry Smith 
1284d763cef2SBarry Smith    Collective on TS
1285d763cef2SBarry Smith 
1286d763cef2SBarry Smith    Input Parameter:
1287d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1288d763cef2SBarry Smith 
1289d763cef2SBarry Smith    Level: beginner
1290d763cef2SBarry Smith 
1291d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1292d763cef2SBarry Smith 
1293d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1294d763cef2SBarry Smith @*/
12956bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1296d763cef2SBarry Smith {
12976849ba73SBarry Smith   PetscErrorCode ierr;
1298d763cef2SBarry Smith 
1299d763cef2SBarry Smith   PetscFunctionBegin;
13006bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
13016bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
13026bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1303d763cef2SBarry Smith 
13046bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1305277b19d0SLisandro Dalcin 
1306be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
13076bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
13086bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
13096d4c513bSLisandro Dalcin 
13106bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
13116bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
13126bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
13136d4c513bSLisandro Dalcin 
1314a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1315d763cef2SBarry Smith   PetscFunctionReturn(0);
1316d763cef2SBarry Smith }
1317d763cef2SBarry Smith 
13184a2ae208SSatish Balay #undef __FUNCT__
13194a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1320d8e5e3e6SSatish Balay /*@
1321d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1322d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1323d763cef2SBarry Smith 
1324d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1325d763cef2SBarry Smith 
1326d763cef2SBarry Smith    Input Parameter:
1327d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1328d763cef2SBarry Smith 
1329d763cef2SBarry Smith    Output Parameter:
1330d763cef2SBarry Smith .  snes - the nonlinear solver context
1331d763cef2SBarry Smith 
1332d763cef2SBarry Smith    Notes:
1333d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1334d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
133594b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1336d763cef2SBarry Smith 
1337d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1338d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1339d763cef2SBarry Smith 
1340d763cef2SBarry Smith    Level: beginner
1341d763cef2SBarry Smith 
1342d763cef2SBarry Smith .keywords: timestep, get, SNES
1343d763cef2SBarry Smith @*/
13447087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1345d763cef2SBarry Smith {
1346d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1347d372ba47SLisandro Dalcin 
1348d763cef2SBarry Smith   PetscFunctionBegin;
13490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13504482741eSBarry Smith   PetscValidPointer(snes,2);
1351d372ba47SLisandro Dalcin   if (ts->problem_type != TS_NONLINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Nonlinear only; use TSGetKSP()");
1352d372ba47SLisandro Dalcin   if (!ts->snes) {
1353d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
1354d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1355d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1356d372ba47SLisandro Dalcin   }
1357d763cef2SBarry Smith   *snes = ts->snes;
1358d763cef2SBarry Smith   PetscFunctionReturn(0);
1359d763cef2SBarry Smith }
1360d763cef2SBarry Smith 
13614a2ae208SSatish Balay #undef __FUNCT__
136294b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1363d8e5e3e6SSatish Balay /*@
136494b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1365d763cef2SBarry Smith    a TS (timestepper) context.
1366d763cef2SBarry Smith 
136794b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1368d763cef2SBarry Smith 
1369d763cef2SBarry Smith    Input Parameter:
1370d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1371d763cef2SBarry Smith 
1372d763cef2SBarry Smith    Output Parameter:
137394b7f48cSBarry Smith .  ksp - the nonlinear solver context
1374d763cef2SBarry Smith 
1375d763cef2SBarry Smith    Notes:
137694b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1377d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1378d763cef2SBarry Smith    KSP and PC contexts as well.
1379d763cef2SBarry Smith 
138094b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
138194b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1382d763cef2SBarry Smith 
1383d763cef2SBarry Smith    Level: beginner
1384d763cef2SBarry Smith 
138594b7f48cSBarry Smith .keywords: timestep, get, KSP
1386d763cef2SBarry Smith @*/
13877087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1388d763cef2SBarry Smith {
1389d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1390089b2837SJed Brown   SNES           snes;
1391d372ba47SLisandro Dalcin 
1392d763cef2SBarry Smith   PetscFunctionBegin;
13930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13944482741eSBarry Smith   PetscValidPointer(ksp,2);
139517186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1396e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1397089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1398089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1399d763cef2SBarry Smith   PetscFunctionReturn(0);
1400d763cef2SBarry Smith }
1401d763cef2SBarry Smith 
1402d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1403d763cef2SBarry Smith 
14044a2ae208SSatish Balay #undef __FUNCT__
1405adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1406adb62b0dSMatthew Knepley /*@
1407adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1408adb62b0dSMatthew Knepley    maximum time for iteration.
1409adb62b0dSMatthew Knepley 
14103f9fe445SBarry Smith    Not Collective
1411adb62b0dSMatthew Knepley 
1412adb62b0dSMatthew Knepley    Input Parameters:
1413adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1414adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1415adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1416adb62b0dSMatthew Knepley 
1417adb62b0dSMatthew Knepley    Level: intermediate
1418adb62b0dSMatthew Knepley 
1419adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1420adb62b0dSMatthew Knepley @*/
14217087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1422adb62b0dSMatthew Knepley {
1423adb62b0dSMatthew Knepley   PetscFunctionBegin;
14240700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1425abc0a331SBarry Smith   if (maxsteps) {
14264482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1427adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1428adb62b0dSMatthew Knepley   }
1429abc0a331SBarry Smith   if (maxtime ) {
14304482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1431adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1432adb62b0dSMatthew Knepley   }
1433adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1434adb62b0dSMatthew Knepley }
1435adb62b0dSMatthew Knepley 
1436adb62b0dSMatthew Knepley #undef __FUNCT__
14374a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1438d763cef2SBarry Smith /*@
1439d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1440d763cef2SBarry Smith    maximum time for iteration.
1441d763cef2SBarry Smith 
14423f9fe445SBarry Smith    Logically Collective on TS
1443d763cef2SBarry Smith 
1444d763cef2SBarry Smith    Input Parameters:
1445d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1446d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1447d763cef2SBarry Smith -  maxtime - final time to iterate to
1448d763cef2SBarry Smith 
1449d763cef2SBarry Smith    Options Database Keys:
1450d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
1451d763cef2SBarry Smith .  -ts_max_time <maxtime> - Sets maxtime
1452d763cef2SBarry Smith 
1453d763cef2SBarry Smith    Notes:
1454d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1455d763cef2SBarry Smith 
1456d763cef2SBarry Smith    Level: intermediate
1457d763cef2SBarry Smith 
1458d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1459d763cef2SBarry Smith @*/
14607087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1461d763cef2SBarry Smith {
1462d763cef2SBarry Smith   PetscFunctionBegin;
14630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1464c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1465c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
1466d763cef2SBarry Smith   ts->max_steps = maxsteps;
1467d763cef2SBarry Smith   ts->max_time  = maxtime;
1468d763cef2SBarry Smith   PetscFunctionReturn(0);
1469d763cef2SBarry Smith }
1470d763cef2SBarry Smith 
14714a2ae208SSatish Balay #undef __FUNCT__
14724a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1473d763cef2SBarry Smith /*@
1474d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1475d763cef2SBarry Smith    for use by the TS routines.
1476d763cef2SBarry Smith 
14773f9fe445SBarry Smith    Logically Collective on TS and Vec
1478d763cef2SBarry Smith 
1479d763cef2SBarry Smith    Input Parameters:
1480d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1481d763cef2SBarry Smith -  x - the solution vector
1482d763cef2SBarry Smith 
1483d763cef2SBarry Smith    Level: beginner
1484d763cef2SBarry Smith 
1485d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1486d763cef2SBarry Smith @*/
14877087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1488d763cef2SBarry Smith {
14898737fe31SLisandro Dalcin   PetscErrorCode ierr;
14908737fe31SLisandro Dalcin 
1491d763cef2SBarry Smith   PetscFunctionBegin;
14920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14930700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
14948737fe31SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
14956bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
14968737fe31SLisandro Dalcin   ts->vec_sol = x;
1497d763cef2SBarry Smith   PetscFunctionReturn(0);
1498d763cef2SBarry Smith }
1499d763cef2SBarry Smith 
1500e74ef692SMatthew Knepley #undef __FUNCT__
1501e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1502ac226902SBarry Smith /*@C
1503000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
15043f2090d5SJed Brown   called once at the beginning of each time step.
1505000e7ae3SMatthew Knepley 
15063f9fe445SBarry Smith   Logically Collective on TS
1507000e7ae3SMatthew Knepley 
1508000e7ae3SMatthew Knepley   Input Parameters:
1509000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1510000e7ae3SMatthew Knepley - func - The function
1511000e7ae3SMatthew Knepley 
1512000e7ae3SMatthew Knepley   Calling sequence of func:
1513000e7ae3SMatthew Knepley . func (TS ts);
1514000e7ae3SMatthew Knepley 
1515000e7ae3SMatthew Knepley   Level: intermediate
1516000e7ae3SMatthew Knepley 
1517000e7ae3SMatthew Knepley .keywords: TS, timestep
1518000e7ae3SMatthew Knepley @*/
15197087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1520000e7ae3SMatthew Knepley {
1521000e7ae3SMatthew Knepley   PetscFunctionBegin;
15220700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1523000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1524000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1525000e7ae3SMatthew Knepley }
1526000e7ae3SMatthew Knepley 
1527e74ef692SMatthew Knepley #undef __FUNCT__
15283f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
15293f2090d5SJed Brown /*@C
15303f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
15313f2090d5SJed Brown 
15323f2090d5SJed Brown   Collective on TS
15333f2090d5SJed Brown 
15343f2090d5SJed Brown   Input Parameters:
15353f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
15363f2090d5SJed Brown 
15373f2090d5SJed Brown   Notes:
15383f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
15393f2090d5SJed Brown   so most users would not generally call this routine themselves.
15403f2090d5SJed Brown 
15413f2090d5SJed Brown   Level: developer
15423f2090d5SJed Brown 
15433f2090d5SJed Brown .keywords: TS, timestep
15443f2090d5SJed Brown @*/
15457087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
15463f2090d5SJed Brown {
15473f2090d5SJed Brown   PetscErrorCode ierr;
15483f2090d5SJed Brown 
15493f2090d5SJed Brown   PetscFunctionBegin;
15500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
155172ac3e02SJed Brown   if (ts->ops->prestep) {
15523f2090d5SJed Brown     PetscStackPush("TS PreStep function");
15533f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
15543f2090d5SJed Brown     PetscStackPop;
1555312ce896SJed Brown   }
15563f2090d5SJed Brown   PetscFunctionReturn(0);
15573f2090d5SJed Brown }
15583f2090d5SJed Brown 
15593f2090d5SJed Brown #undef __FUNCT__
1560e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1561ac226902SBarry Smith /*@C
1562000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
15633f2090d5SJed Brown   called once at the end of each time step.
1564000e7ae3SMatthew Knepley 
15653f9fe445SBarry Smith   Logically Collective on TS
1566000e7ae3SMatthew Knepley 
1567000e7ae3SMatthew Knepley   Input Parameters:
1568000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1569000e7ae3SMatthew Knepley - func - The function
1570000e7ae3SMatthew Knepley 
1571000e7ae3SMatthew Knepley   Calling sequence of func:
1572000e7ae3SMatthew Knepley . func (TS ts);
1573000e7ae3SMatthew Knepley 
1574000e7ae3SMatthew Knepley   Level: intermediate
1575000e7ae3SMatthew Knepley 
1576000e7ae3SMatthew Knepley .keywords: TS, timestep
1577000e7ae3SMatthew Knepley @*/
15787087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1579000e7ae3SMatthew Knepley {
1580000e7ae3SMatthew Knepley   PetscFunctionBegin;
15810700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1582000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1583000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1584000e7ae3SMatthew Knepley }
1585000e7ae3SMatthew Knepley 
1586e74ef692SMatthew Knepley #undef __FUNCT__
15873f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
15883f2090d5SJed Brown /*@C
15893f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
15903f2090d5SJed Brown 
15913f2090d5SJed Brown   Collective on TS
15923f2090d5SJed Brown 
15933f2090d5SJed Brown   Input Parameters:
15943f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
15953f2090d5SJed Brown 
15963f2090d5SJed Brown   Notes:
15973f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
15983f2090d5SJed Brown   so most users would not generally call this routine themselves.
15993f2090d5SJed Brown 
16003f2090d5SJed Brown   Level: developer
16013f2090d5SJed Brown 
16023f2090d5SJed Brown .keywords: TS, timestep
16033f2090d5SJed Brown @*/
16047087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
16053f2090d5SJed Brown {
16063f2090d5SJed Brown   PetscErrorCode ierr;
16073f2090d5SJed Brown 
16083f2090d5SJed Brown   PetscFunctionBegin;
16090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
161072ac3e02SJed Brown   if (ts->ops->poststep) {
16113f2090d5SJed Brown     PetscStackPush("TS PostStep function");
16123f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
16133f2090d5SJed Brown     PetscStackPop;
161472ac3e02SJed Brown   }
16153f2090d5SJed Brown   PetscFunctionReturn(0);
16163f2090d5SJed Brown }
16173f2090d5SJed Brown 
1618d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1619d763cef2SBarry Smith 
16204a2ae208SSatish Balay #undef __FUNCT__
1621a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1622d763cef2SBarry Smith /*@C
1623a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1624d763cef2SBarry Smith    timestep to display the iteration's  progress.
1625d763cef2SBarry Smith 
16263f9fe445SBarry Smith    Logically Collective on TS
1627d763cef2SBarry Smith 
1628d763cef2SBarry Smith    Input Parameters:
1629d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1630d763cef2SBarry Smith .  func - monitoring routine
1631329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1632b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1633b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1634b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1635d763cef2SBarry Smith 
1636d763cef2SBarry Smith    Calling sequence of func:
1637a7cc72afSBarry Smith $    int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1638d763cef2SBarry Smith 
1639d763cef2SBarry Smith +    ts - the TS context
1640d763cef2SBarry Smith .    steps - iteration number
16411f06c33eSBarry Smith .    time - current time
1642d763cef2SBarry Smith .    x - current iterate
1643d763cef2SBarry Smith -    mctx - [optional] monitoring context
1644d763cef2SBarry Smith 
1645d763cef2SBarry Smith    Notes:
1646d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1647d763cef2SBarry Smith    already has been loaded.
1648d763cef2SBarry Smith 
1649025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1650025f1a04SBarry Smith 
1651d763cef2SBarry Smith    Level: intermediate
1652d763cef2SBarry Smith 
1653d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1654d763cef2SBarry Smith 
1655a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1656d763cef2SBarry Smith @*/
1657c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1658d763cef2SBarry Smith {
1659d763cef2SBarry Smith   PetscFunctionBegin;
16600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
166117186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1662d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1663329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1664d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1665d763cef2SBarry Smith   PetscFunctionReturn(0);
1666d763cef2SBarry Smith }
1667d763cef2SBarry Smith 
16684a2ae208SSatish Balay #undef __FUNCT__
1669a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1670d763cef2SBarry Smith /*@C
1671a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1672d763cef2SBarry Smith 
16733f9fe445SBarry Smith    Logically Collective on TS
1674d763cef2SBarry Smith 
1675d763cef2SBarry Smith    Input Parameters:
1676d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1677d763cef2SBarry Smith 
1678d763cef2SBarry Smith    Notes:
1679d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1680d763cef2SBarry Smith 
1681d763cef2SBarry Smith    Level: intermediate
1682d763cef2SBarry Smith 
1683d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1684d763cef2SBarry Smith 
1685a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1686d763cef2SBarry Smith @*/
16877087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1688d763cef2SBarry Smith {
1689d952e501SBarry Smith   PetscErrorCode ierr;
1690d952e501SBarry Smith   PetscInt       i;
1691d952e501SBarry Smith 
1692d763cef2SBarry Smith   PetscFunctionBegin;
16930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1694d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
1695d952e501SBarry Smith     if (ts->mdestroy[i]) {
16963c4aec1bSBarry Smith       ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
1697d952e501SBarry Smith     }
1698d952e501SBarry Smith   }
1699d763cef2SBarry Smith   ts->numbermonitors = 0;
1700d763cef2SBarry Smith   PetscFunctionReturn(0);
1701d763cef2SBarry Smith }
1702d763cef2SBarry Smith 
17034a2ae208SSatish Balay #undef __FUNCT__
1704a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
1705d8e5e3e6SSatish Balay /*@
1706a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
17075516499fSSatish Balay 
17085516499fSSatish Balay    Level: intermediate
170941251cbbSSatish Balay 
17105516499fSSatish Balay .keywords: TS, set, monitor
17115516499fSSatish Balay 
171241251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
171341251cbbSSatish Balay @*/
1714649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
1715d763cef2SBarry Smith {
1716dfbe8321SBarry Smith   PetscErrorCode ierr;
1717649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
1718d132466eSBarry Smith 
1719d763cef2SBarry Smith   PetscFunctionBegin;
1720649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1721649052a6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr);
1722649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1723d763cef2SBarry Smith   PetscFunctionReturn(0);
1724d763cef2SBarry Smith }
1725d763cef2SBarry Smith 
17264a2ae208SSatish Balay #undef __FUNCT__
17274a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1728d763cef2SBarry Smith /*@
1729d763cef2SBarry Smith    TSStep - Steps the requested number of timesteps.
1730d763cef2SBarry Smith 
1731d763cef2SBarry Smith    Collective on TS
1732d763cef2SBarry Smith 
1733d763cef2SBarry Smith    Input Parameter:
1734d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1735d763cef2SBarry Smith 
1736d763cef2SBarry Smith    Output Parameters:
1737d763cef2SBarry Smith +  steps - number of iterations until termination
1738142b95e3SSatish Balay -  ptime - time until termination
1739d763cef2SBarry Smith 
1740d763cef2SBarry Smith    Level: beginner
1741d763cef2SBarry Smith 
1742d763cef2SBarry Smith .keywords: TS, timestep, solve
1743d763cef2SBarry Smith 
1744d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy()
1745d763cef2SBarry Smith @*/
17467087cfbeSBarry Smith PetscErrorCode  TSStep(TS ts,PetscInt *steps,PetscReal *ptime)
1747d763cef2SBarry Smith {
1748dfbe8321SBarry Smith   PetscErrorCode ierr;
1749d763cef2SBarry Smith 
1750d763cef2SBarry Smith   PetscFunctionBegin;
17510700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1752277b19d0SLisandro Dalcin 
1753d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
1754d405a339SMatthew Knepley 
1755d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1756000e7ae3SMatthew Knepley   ierr = (*ts->ops->step)(ts, steps, ptime);CHKERRQ(ierr);
1757d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1758d405a339SMatthew Knepley 
17594bb05414SBarry Smith   if (!PetscPreLoadingOn) {
17607adad957SLisandro Dalcin     ierr = TSViewFromOptions(ts,((PetscObject)ts)->name);CHKERRQ(ierr);
1761d405a339SMatthew Knepley   }
1762d763cef2SBarry Smith   PetscFunctionReturn(0);
1763d763cef2SBarry Smith }
1764d763cef2SBarry Smith 
17654a2ae208SSatish Balay #undef __FUNCT__
17666a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
17676a4d4014SLisandro Dalcin /*@
17686a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
17696a4d4014SLisandro Dalcin 
17706a4d4014SLisandro Dalcin    Collective on TS
17716a4d4014SLisandro Dalcin 
17726a4d4014SLisandro Dalcin    Input Parameter:
17736a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
17746a4d4014SLisandro Dalcin -  x - the solution vector, or PETSC_NULL if it was set with TSSetSolution()
17756a4d4014SLisandro Dalcin 
17766a4d4014SLisandro Dalcin    Level: beginner
17776a4d4014SLisandro Dalcin 
17786a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
17796a4d4014SLisandro Dalcin 
17806a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
17816a4d4014SLisandro Dalcin @*/
17827087cfbeSBarry Smith PetscErrorCode  TSSolve(TS ts, Vec x)
17836a4d4014SLisandro Dalcin {
17846a4d4014SLisandro Dalcin   PetscInt       steps;
17856a4d4014SLisandro Dalcin   PetscReal      ptime;
17866a4d4014SLisandro Dalcin   PetscErrorCode ierr;
1787f22f69f0SBarry Smith 
17886a4d4014SLisandro Dalcin   PetscFunctionBegin;
17890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17906a4d4014SLisandro Dalcin   /* set solution vector if provided */
17916a4d4014SLisandro Dalcin   if (x) { ierr = TSSetSolution(ts, x); CHKERRQ(ierr); }
17926a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
17936a4d4014SLisandro Dalcin   ts->steps = 0; ts->linear_its = 0; ts->nonlinear_its = 0;
17946a4d4014SLisandro Dalcin   /* steps the requested number of timesteps. */
17956a4d4014SLisandro Dalcin   ierr = TSStep(ts, &steps, &ptime);CHKERRQ(ierr);
17966a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
17976a4d4014SLisandro Dalcin }
17986a4d4014SLisandro Dalcin 
17996a4d4014SLisandro Dalcin #undef __FUNCT__
18004a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
1801d763cef2SBarry Smith /*
1802d763cef2SBarry Smith      Runs the user provided monitor routines, if they exists.
1803d763cef2SBarry Smith */
1804a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
1805d763cef2SBarry Smith {
18066849ba73SBarry Smith   PetscErrorCode ierr;
1807a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
1808d763cef2SBarry Smith 
1809d763cef2SBarry Smith   PetscFunctionBegin;
1810d763cef2SBarry Smith   for (i=0; i<n; i++) {
1811142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
1812d763cef2SBarry Smith   }
1813d763cef2SBarry Smith   PetscFunctionReturn(0);
1814d763cef2SBarry Smith }
1815d763cef2SBarry Smith 
1816d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
1817d763cef2SBarry Smith 
18184a2ae208SSatish Balay #undef __FUNCT__
1819a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate"
1820d763cef2SBarry Smith /*@C
1821a6570f20SBarry Smith    TSMonitorLGCreate - Creates a line graph context for use with
1822d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
1823d763cef2SBarry Smith 
1824d763cef2SBarry Smith    Collective on TS
1825d763cef2SBarry Smith 
1826d763cef2SBarry Smith    Input Parameters:
1827d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
1828d763cef2SBarry Smith .  label - the title to put in the title bar
18297c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
1830d763cef2SBarry Smith -  m, n - the screen width and height in pixels
1831d763cef2SBarry Smith 
1832d763cef2SBarry Smith    Output Parameter:
1833d763cef2SBarry Smith .  draw - the drawing context
1834d763cef2SBarry Smith 
1835d763cef2SBarry Smith    Options Database Key:
1836a6570f20SBarry Smith .  -ts_monitor_draw - automatically sets line graph monitor
1837d763cef2SBarry Smith 
1838d763cef2SBarry Smith    Notes:
1839a6570f20SBarry Smith    Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy().
1840d763cef2SBarry Smith 
1841d763cef2SBarry Smith    Level: intermediate
1842d763cef2SBarry Smith 
18437c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
1844d763cef2SBarry Smith 
1845a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet()
18467c922b88SBarry Smith 
1847d763cef2SBarry Smith @*/
18487087cfbeSBarry Smith PetscErrorCode  TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1849d763cef2SBarry Smith {
1850b0a32e0cSBarry Smith   PetscDraw      win;
1851dfbe8321SBarry Smith   PetscErrorCode ierr;
1852d763cef2SBarry Smith 
1853d763cef2SBarry Smith   PetscFunctionBegin;
1854b0a32e0cSBarry Smith   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
1855b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
1856b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
1857b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
1858d763cef2SBarry Smith 
185952e6d16bSBarry Smith   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
1860d763cef2SBarry Smith   PetscFunctionReturn(0);
1861d763cef2SBarry Smith }
1862d763cef2SBarry Smith 
18634a2ae208SSatish Balay #undef __FUNCT__
1864a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG"
1865a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
1866d763cef2SBarry Smith {
1867b0a32e0cSBarry Smith   PetscDrawLG    lg = (PetscDrawLG) monctx;
186887828ca2SBarry Smith   PetscReal      x,y = ptime;
1869dfbe8321SBarry Smith   PetscErrorCode ierr;
1870d763cef2SBarry Smith 
1871d763cef2SBarry Smith   PetscFunctionBegin;
18727c922b88SBarry Smith   if (!monctx) {
18737c922b88SBarry Smith     MPI_Comm    comm;
1874b0a32e0cSBarry Smith     PetscViewer viewer;
18757c922b88SBarry Smith 
18767c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
1877b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
1878b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
18797c922b88SBarry Smith   }
18807c922b88SBarry Smith 
1881b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
188287828ca2SBarry Smith   x = (PetscReal)n;
1883b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1884d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
1885b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1886d763cef2SBarry Smith   }
1887d763cef2SBarry Smith   PetscFunctionReturn(0);
1888d763cef2SBarry Smith }
1889d763cef2SBarry Smith 
18904a2ae208SSatish Balay #undef __FUNCT__
1891a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy"
1892d763cef2SBarry Smith /*@C
1893a6570f20SBarry Smith    TSMonitorLGDestroy - Destroys a line graph context that was created
1894a6570f20SBarry Smith    with TSMonitorLGCreate().
1895d763cef2SBarry Smith 
1896b0a32e0cSBarry Smith    Collective on PetscDrawLG
1897d763cef2SBarry Smith 
1898d763cef2SBarry Smith    Input Parameter:
1899d763cef2SBarry Smith .  draw - the drawing context
1900d763cef2SBarry Smith 
1901d763cef2SBarry Smith    Level: intermediate
1902d763cef2SBarry Smith 
1903d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
1904d763cef2SBarry Smith 
1905a6570f20SBarry Smith .seealso: TSMonitorLGCreate(),  TSMonitorSet(), TSMonitorLG();
1906d763cef2SBarry Smith @*/
19076bf464f9SBarry Smith PetscErrorCode  TSMonitorLGDestroy(PetscDrawLG *drawlg)
1908d763cef2SBarry Smith {
1909b0a32e0cSBarry Smith   PetscDraw      draw;
1910dfbe8321SBarry Smith   PetscErrorCode ierr;
1911d763cef2SBarry Smith 
1912d763cef2SBarry Smith   PetscFunctionBegin;
19136bf464f9SBarry Smith   ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr);
19146bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
1915b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
1916d763cef2SBarry Smith   PetscFunctionReturn(0);
1917d763cef2SBarry Smith }
1918d763cef2SBarry Smith 
19194a2ae208SSatish Balay #undef __FUNCT__
19204a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
1921d763cef2SBarry Smith /*@
1922d763cef2SBarry Smith    TSGetTime - Gets the current time.
1923d763cef2SBarry Smith 
1924d763cef2SBarry Smith    Not Collective
1925d763cef2SBarry Smith 
1926d763cef2SBarry Smith    Input Parameter:
1927d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1928d763cef2SBarry Smith 
1929d763cef2SBarry Smith    Output Parameter:
1930d763cef2SBarry Smith .  t  - the current time
1931d763cef2SBarry Smith 
1932d763cef2SBarry Smith    Level: beginner
1933d763cef2SBarry Smith 
1934d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1935d763cef2SBarry Smith 
1936d763cef2SBarry Smith .keywords: TS, get, time
1937d763cef2SBarry Smith @*/
19387087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
1939d763cef2SBarry Smith {
1940d763cef2SBarry Smith   PetscFunctionBegin;
19410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19424482741eSBarry Smith   PetscValidDoublePointer(t,2);
1943d763cef2SBarry Smith   *t = ts->ptime;
1944d763cef2SBarry Smith   PetscFunctionReturn(0);
1945d763cef2SBarry Smith }
1946d763cef2SBarry Smith 
19474a2ae208SSatish Balay #undef __FUNCT__
19486a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
19496a4d4014SLisandro Dalcin /*@
19506a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
19516a4d4014SLisandro Dalcin 
19523f9fe445SBarry Smith    Logically Collective on TS
19536a4d4014SLisandro Dalcin 
19546a4d4014SLisandro Dalcin    Input Parameters:
19556a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
19566a4d4014SLisandro Dalcin -  time - the time
19576a4d4014SLisandro Dalcin 
19586a4d4014SLisandro Dalcin    Level: intermediate
19596a4d4014SLisandro Dalcin 
19606a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
19616a4d4014SLisandro Dalcin 
19626a4d4014SLisandro Dalcin .keywords: TS, set, time
19636a4d4014SLisandro Dalcin @*/
19647087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
19656a4d4014SLisandro Dalcin {
19666a4d4014SLisandro Dalcin   PetscFunctionBegin;
19670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1968c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
19696a4d4014SLisandro Dalcin   ts->ptime = t;
19706a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
19716a4d4014SLisandro Dalcin }
19726a4d4014SLisandro Dalcin 
19736a4d4014SLisandro Dalcin #undef __FUNCT__
19744a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
1975d763cef2SBarry Smith /*@C
1976d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
1977d763cef2SBarry Smith    TS options in the database.
1978d763cef2SBarry Smith 
19793f9fe445SBarry Smith    Logically Collective on TS
1980d763cef2SBarry Smith 
1981d763cef2SBarry Smith    Input Parameter:
1982d763cef2SBarry Smith +  ts     - The TS context
1983d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
1984d763cef2SBarry Smith 
1985d763cef2SBarry Smith    Notes:
1986d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1987d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
1988d763cef2SBarry Smith    hyphen.
1989d763cef2SBarry Smith 
1990d763cef2SBarry Smith    Level: advanced
1991d763cef2SBarry Smith 
1992d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
1993d763cef2SBarry Smith 
1994d763cef2SBarry Smith .seealso: TSSetFromOptions()
1995d763cef2SBarry Smith 
1996d763cef2SBarry Smith @*/
19977087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
1998d763cef2SBarry Smith {
1999dfbe8321SBarry Smith   PetscErrorCode ierr;
2000089b2837SJed Brown   SNES           snes;
2001d763cef2SBarry Smith 
2002d763cef2SBarry Smith   PetscFunctionBegin;
20030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2004d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2005089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2006089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2007d763cef2SBarry Smith   PetscFunctionReturn(0);
2008d763cef2SBarry Smith }
2009d763cef2SBarry Smith 
2010d763cef2SBarry Smith 
20114a2ae208SSatish Balay #undef __FUNCT__
20124a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2013d763cef2SBarry Smith /*@C
2014d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2015d763cef2SBarry Smith    TS options in the database.
2016d763cef2SBarry Smith 
20173f9fe445SBarry Smith    Logically Collective on TS
2018d763cef2SBarry Smith 
2019d763cef2SBarry Smith    Input Parameter:
2020d763cef2SBarry Smith +  ts     - The TS context
2021d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2022d763cef2SBarry Smith 
2023d763cef2SBarry Smith    Notes:
2024d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2025d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2026d763cef2SBarry Smith    hyphen.
2027d763cef2SBarry Smith 
2028d763cef2SBarry Smith    Level: advanced
2029d763cef2SBarry Smith 
2030d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2031d763cef2SBarry Smith 
2032d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2033d763cef2SBarry Smith 
2034d763cef2SBarry Smith @*/
20357087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2036d763cef2SBarry Smith {
2037dfbe8321SBarry Smith   PetscErrorCode ierr;
2038089b2837SJed Brown   SNES           snes;
2039d763cef2SBarry Smith 
2040d763cef2SBarry Smith   PetscFunctionBegin;
20410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2042d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2043089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2044089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2045d763cef2SBarry Smith   PetscFunctionReturn(0);
2046d763cef2SBarry Smith }
2047d763cef2SBarry Smith 
20484a2ae208SSatish Balay #undef __FUNCT__
20494a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2050d763cef2SBarry Smith /*@C
2051d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2052d763cef2SBarry Smith    TS options in the database.
2053d763cef2SBarry Smith 
2054d763cef2SBarry Smith    Not Collective
2055d763cef2SBarry Smith 
2056d763cef2SBarry Smith    Input Parameter:
2057d763cef2SBarry Smith .  ts - The TS context
2058d763cef2SBarry Smith 
2059d763cef2SBarry Smith    Output Parameter:
2060d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2061d763cef2SBarry Smith 
2062d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2063d763cef2SBarry Smith    sufficient length to hold the prefix.
2064d763cef2SBarry Smith 
2065d763cef2SBarry Smith    Level: intermediate
2066d763cef2SBarry Smith 
2067d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2068d763cef2SBarry Smith 
2069d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2070d763cef2SBarry Smith @*/
20717087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2072d763cef2SBarry Smith {
2073dfbe8321SBarry Smith   PetscErrorCode ierr;
2074d763cef2SBarry Smith 
2075d763cef2SBarry Smith   PetscFunctionBegin;
20760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20774482741eSBarry Smith   PetscValidPointer(prefix,2);
2078d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2079d763cef2SBarry Smith   PetscFunctionReturn(0);
2080d763cef2SBarry Smith }
2081d763cef2SBarry Smith 
20824a2ae208SSatish Balay #undef __FUNCT__
20834a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2084d763cef2SBarry Smith /*@C
2085d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2086d763cef2SBarry Smith 
2087d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2088d763cef2SBarry Smith 
2089d763cef2SBarry Smith    Input Parameter:
2090d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2091d763cef2SBarry Smith 
2092d763cef2SBarry Smith    Output Parameters:
2093d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2094d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2095089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2096d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2097d763cef2SBarry Smith 
2098d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2099d763cef2SBarry Smith 
2100d763cef2SBarry Smith    Level: intermediate
2101d763cef2SBarry Smith 
210226d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2103d763cef2SBarry Smith 
2104d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2105d763cef2SBarry Smith @*/
2106089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2107d763cef2SBarry Smith {
2108089b2837SJed Brown   PetscErrorCode ierr;
2109089b2837SJed Brown   SNES           snes;
2110089b2837SJed Brown 
2111d763cef2SBarry Smith   PetscFunctionBegin;
2112089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2113089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
2114089b2837SJed Brown   if (func) *func = ts->ops->rhsjacobian;
211526d46c62SHong Zhang   if (ctx) *ctx = ts->jacP;
2116d763cef2SBarry Smith   PetscFunctionReturn(0);
2117d763cef2SBarry Smith }
2118d763cef2SBarry Smith 
21191713a123SBarry Smith #undef __FUNCT__
21202eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
21212eca1d9cSJed Brown /*@C
21222eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
21232eca1d9cSJed Brown 
21242eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
21252eca1d9cSJed Brown 
21262eca1d9cSJed Brown    Input Parameter:
21272eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
21282eca1d9cSJed Brown 
21292eca1d9cSJed Brown    Output Parameters:
21302eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
21312eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
21322eca1d9cSJed Brown .  f   - The function to compute the matrices
21332eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
21342eca1d9cSJed Brown 
21352eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
21362eca1d9cSJed Brown 
21372eca1d9cSJed Brown    Level: advanced
21382eca1d9cSJed Brown 
21392eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
21402eca1d9cSJed Brown 
21412eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
21422eca1d9cSJed Brown @*/
21437087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
21442eca1d9cSJed Brown {
2145089b2837SJed Brown   PetscErrorCode ierr;
2146089b2837SJed Brown   SNES           snes;
2147089b2837SJed Brown 
21482eca1d9cSJed Brown   PetscFunctionBegin;
2149089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2150089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
21512eca1d9cSJed Brown   if (f) *f = ts->ops->ijacobian;
21522eca1d9cSJed Brown   if (ctx) *ctx = ts->jacP;
21532eca1d9cSJed Brown   PetscFunctionReturn(0);
21542eca1d9cSJed Brown }
21552eca1d9cSJed Brown 
21562eca1d9cSJed Brown #undef __FUNCT__
2157a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution"
21581713a123SBarry Smith /*@C
2159a6570f20SBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by calling
21601713a123SBarry Smith    VecView() for the solution at each timestep
21611713a123SBarry Smith 
21621713a123SBarry Smith    Collective on TS
21631713a123SBarry Smith 
21641713a123SBarry Smith    Input Parameters:
21651713a123SBarry Smith +  ts - the TS context
21661713a123SBarry Smith .  step - current time-step
2167142b95e3SSatish Balay .  ptime - current time
21681713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
21691713a123SBarry Smith 
21701713a123SBarry Smith    Level: intermediate
21711713a123SBarry Smith 
21721713a123SBarry Smith .keywords: TS,  vector, monitor, view
21731713a123SBarry Smith 
2174a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
21751713a123SBarry Smith @*/
21767087cfbeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
21771713a123SBarry Smith {
2178dfbe8321SBarry Smith   PetscErrorCode ierr;
21791713a123SBarry Smith   PetscViewer    viewer = (PetscViewer) dummy;
21801713a123SBarry Smith 
21811713a123SBarry Smith   PetscFunctionBegin;
2182a34d58ebSBarry Smith   if (!dummy) {
21837adad957SLisandro Dalcin     viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm);
21841713a123SBarry Smith   }
21851713a123SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
21861713a123SBarry Smith   PetscFunctionReturn(0);
21871713a123SBarry Smith }
21881713a123SBarry Smith 
21891713a123SBarry Smith 
21906c699258SBarry Smith #undef __FUNCT__
21916c699258SBarry Smith #define __FUNCT__ "TSSetDM"
21926c699258SBarry Smith /*@
21936c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
21946c699258SBarry Smith 
21953f9fe445SBarry Smith    Logically Collective on TS and DM
21966c699258SBarry Smith 
21976c699258SBarry Smith    Input Parameters:
21986c699258SBarry Smith +  ts - the preconditioner context
21996c699258SBarry Smith -  dm - the dm
22006c699258SBarry Smith 
22016c699258SBarry Smith    Level: intermediate
22026c699258SBarry Smith 
22036c699258SBarry Smith 
22046c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
22056c699258SBarry Smith @*/
22067087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
22076c699258SBarry Smith {
22086c699258SBarry Smith   PetscErrorCode ierr;
2209089b2837SJed Brown   SNES           snes;
22106c699258SBarry Smith 
22116c699258SBarry Smith   PetscFunctionBegin;
22120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
221370663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
22146bf464f9SBarry Smith   ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
22156c699258SBarry Smith   ts->dm = dm;
2216089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2217089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
22186c699258SBarry Smith   PetscFunctionReturn(0);
22196c699258SBarry Smith }
22206c699258SBarry Smith 
22216c699258SBarry Smith #undef __FUNCT__
22226c699258SBarry Smith #define __FUNCT__ "TSGetDM"
22236c699258SBarry Smith /*@
22246c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
22256c699258SBarry Smith 
22263f9fe445SBarry Smith    Not Collective
22276c699258SBarry Smith 
22286c699258SBarry Smith    Input Parameter:
22296c699258SBarry Smith . ts - the preconditioner context
22306c699258SBarry Smith 
22316c699258SBarry Smith    Output Parameter:
22326c699258SBarry Smith .  dm - the dm
22336c699258SBarry Smith 
22346c699258SBarry Smith    Level: intermediate
22356c699258SBarry Smith 
22366c699258SBarry Smith 
22376c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
22386c699258SBarry Smith @*/
22397087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
22406c699258SBarry Smith {
22416c699258SBarry Smith   PetscFunctionBegin;
22420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22436c699258SBarry Smith   *dm = ts->dm;
22446c699258SBarry Smith   PetscFunctionReturn(0);
22456c699258SBarry Smith }
22461713a123SBarry Smith 
22470f5c6efeSJed Brown #undef __FUNCT__
22480f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
22490f5c6efeSJed Brown /*@
22500f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
22510f5c6efeSJed Brown 
22523f9fe445SBarry Smith    Logically Collective on SNES
22530f5c6efeSJed Brown 
22540f5c6efeSJed Brown    Input Parameter:
2255d42a1c89SJed Brown + snes - nonlinear solver
22560f5c6efeSJed Brown . X - the current state at which to evaluate the residual
2257d42a1c89SJed Brown - ctx - user context, must be a TS
22580f5c6efeSJed Brown 
22590f5c6efeSJed Brown    Output Parameter:
22600f5c6efeSJed Brown . F - the nonlinear residual
22610f5c6efeSJed Brown 
22620f5c6efeSJed Brown    Notes:
22630f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
22640f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
22650f5c6efeSJed Brown 
22660f5c6efeSJed Brown    Level: advanced
22670f5c6efeSJed Brown 
22680f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
22690f5c6efeSJed Brown @*/
22707087cfbeSBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx)
22710f5c6efeSJed Brown {
22720f5c6efeSJed Brown   TS ts = (TS)ctx;
22730f5c6efeSJed Brown   PetscErrorCode ierr;
22740f5c6efeSJed Brown 
22750f5c6efeSJed Brown   PetscFunctionBegin;
22760f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22770f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
22780f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
22790f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
22800f5c6efeSJed Brown   ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr);
22810f5c6efeSJed Brown   PetscFunctionReturn(0);
22820f5c6efeSJed Brown }
22830f5c6efeSJed Brown 
22840f5c6efeSJed Brown #undef __FUNCT__
22850f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
22860f5c6efeSJed Brown /*@
22870f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
22880f5c6efeSJed Brown 
22890f5c6efeSJed Brown    Collective on SNES
22900f5c6efeSJed Brown 
22910f5c6efeSJed Brown    Input Parameter:
22920f5c6efeSJed Brown + snes - nonlinear solver
22930f5c6efeSJed Brown . X - the current state at which to evaluate the residual
22940f5c6efeSJed Brown - ctx - user context, must be a TS
22950f5c6efeSJed Brown 
22960f5c6efeSJed Brown    Output Parameter:
22970f5c6efeSJed Brown + A - the Jacobian
22980f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
22990f5c6efeSJed Brown - flag - indicates any structure change in the matrix
23000f5c6efeSJed Brown 
23010f5c6efeSJed Brown    Notes:
23020f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
23030f5c6efeSJed Brown 
23040f5c6efeSJed Brown    Level: developer
23050f5c6efeSJed Brown 
23060f5c6efeSJed Brown .seealso: SNESSetJacobian()
23070f5c6efeSJed Brown @*/
23087087cfbeSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx)
23090f5c6efeSJed Brown {
23100f5c6efeSJed Brown   TS ts = (TS)ctx;
23110f5c6efeSJed Brown   PetscErrorCode ierr;
23120f5c6efeSJed Brown 
23130f5c6efeSJed Brown   PetscFunctionBegin;
23140f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23150f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
23160f5c6efeSJed Brown   PetscValidPointer(A,3);
23170f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
23180f5c6efeSJed Brown   PetscValidPointer(B,4);
23190f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
23200f5c6efeSJed Brown   PetscValidPointer(flag,5);
23210f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
23220f5c6efeSJed Brown   ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr);
23230f5c6efeSJed Brown   PetscFunctionReturn(0);
23240f5c6efeSJed Brown }
2325325fc9f4SBarry Smith 
2326325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2327c6db04a5SJed Brown #include <mex.h>
2328325fc9f4SBarry Smith 
2329325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
2330325fc9f4SBarry Smith 
2331325fc9f4SBarry Smith #undef __FUNCT__
2332325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
2333325fc9f4SBarry Smith /*
2334325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
2335325fc9f4SBarry Smith                          TSSetFunctionMatlab().
2336325fc9f4SBarry Smith 
2337325fc9f4SBarry Smith    Collective on TS
2338325fc9f4SBarry Smith 
2339325fc9f4SBarry Smith    Input Parameters:
2340325fc9f4SBarry Smith +  snes - the TS context
2341325fc9f4SBarry Smith -  x - input vector
2342325fc9f4SBarry Smith 
2343325fc9f4SBarry Smith    Output Parameter:
2344325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
2345325fc9f4SBarry Smith 
2346325fc9f4SBarry Smith    Notes:
2347325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
2348325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
2349325fc9f4SBarry Smith    themselves.
2350325fc9f4SBarry Smith 
2351325fc9f4SBarry Smith    Level: developer
2352325fc9f4SBarry Smith 
2353325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2354325fc9f4SBarry Smith 
2355325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2356325fc9f4SBarry Smith */
23577087cfbeSBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx)
2358325fc9f4SBarry Smith {
2359325fc9f4SBarry Smith   PetscErrorCode   ierr;
2360325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2361325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
2362325fc9f4SBarry Smith   mxArray	   *plhs[1],*prhs[7];
2363325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
2364325fc9f4SBarry Smith 
2365325fc9f4SBarry Smith   PetscFunctionBegin;
2366325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2367325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2368325fc9f4SBarry Smith   PetscValidHeaderSpecific(xdot,VEC_CLASSID,4);
2369325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
2370325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,x,3);
2371325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
2372325fc9f4SBarry Smith 
2373325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2374325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2375880f3077SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr);
2376325fc9f4SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
2377325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2378325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
2379325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2380325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2381325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
2382325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
2383325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
2384325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
2385325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2386325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2387325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2388325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2389325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2390325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2391325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2392325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2393325fc9f4SBarry Smith   PetscFunctionReturn(0);
2394325fc9f4SBarry Smith }
2395325fc9f4SBarry Smith 
2396325fc9f4SBarry Smith 
2397325fc9f4SBarry Smith #undef __FUNCT__
2398325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
2399325fc9f4SBarry Smith /*
2400325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
2401325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
2402e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
2403325fc9f4SBarry Smith 
2404325fc9f4SBarry Smith    Logically Collective on TS
2405325fc9f4SBarry Smith 
2406325fc9f4SBarry Smith    Input Parameters:
2407325fc9f4SBarry Smith +  ts - the TS context
2408325fc9f4SBarry Smith -  func - function evaluation routine
2409325fc9f4SBarry Smith 
2410325fc9f4SBarry Smith    Calling sequence of func:
2411325fc9f4SBarry Smith $    func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx);
2412325fc9f4SBarry Smith 
2413325fc9f4SBarry Smith    Level: beginner
2414325fc9f4SBarry Smith 
2415325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2416325fc9f4SBarry Smith 
2417325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2418325fc9f4SBarry Smith */
24197087cfbeSBarry Smith PetscErrorCode  TSSetFunctionMatlab(TS snes,const char *func,mxArray *ctx)
2420325fc9f4SBarry Smith {
2421325fc9f4SBarry Smith   PetscErrorCode  ierr;
2422325fc9f4SBarry Smith   TSMatlabContext *sctx;
2423325fc9f4SBarry Smith 
2424325fc9f4SBarry Smith   PetscFunctionBegin;
2425325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2426325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2427325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2428325fc9f4SBarry Smith   /*
2429325fc9f4SBarry Smith      This should work, but it doesn't
2430325fc9f4SBarry Smith   sctx->ctx = ctx;
2431325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2432325fc9f4SBarry Smith   */
2433325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2434325fc9f4SBarry Smith   ierr = TSSetIFunction(snes,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
2435325fc9f4SBarry Smith   PetscFunctionReturn(0);
2436325fc9f4SBarry Smith }
2437325fc9f4SBarry Smith 
2438325fc9f4SBarry Smith #undef __FUNCT__
2439325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
2440325fc9f4SBarry Smith /*
2441325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
2442325fc9f4SBarry Smith                          TSSetJacobianMatlab().
2443325fc9f4SBarry Smith 
2444325fc9f4SBarry Smith    Collective on TS
2445325fc9f4SBarry Smith 
2446325fc9f4SBarry Smith    Input Parameters:
2447325fc9f4SBarry Smith +  snes - the TS context
2448325fc9f4SBarry Smith .  x - input vector
2449325fc9f4SBarry Smith .  A, B - the matrices
2450325fc9f4SBarry Smith -  ctx - user context
2451325fc9f4SBarry Smith 
2452325fc9f4SBarry Smith    Output Parameter:
2453325fc9f4SBarry Smith .  flag - structure of the matrix
2454325fc9f4SBarry Smith 
2455325fc9f4SBarry Smith    Level: developer
2456325fc9f4SBarry Smith 
2457325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2458325fc9f4SBarry Smith 
2459325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2460325fc9f4SBarry Smith @*/
24617087cfbeSBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
2462325fc9f4SBarry Smith {
2463325fc9f4SBarry Smith   PetscErrorCode  ierr;
2464325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2465325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
2466325fc9f4SBarry Smith   mxArray	  *plhs[2],*prhs[9];
2467325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
2468325fc9f4SBarry Smith 
2469325fc9f4SBarry Smith   PetscFunctionBegin;
2470325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2471325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2472325fc9f4SBarry Smith 
2473325fc9f4SBarry Smith   /* call Matlab function in ctx with arguments x and y */
2474325fc9f4SBarry Smith 
2475325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2476325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2477325fc9f4SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr);
2478325fc9f4SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
2479325fc9f4SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
2480325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2481325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
2482325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2483325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2484325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
2485325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
2486325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
2487325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
2488325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
2489325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
2490325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2491325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
2492325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2493325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2494325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2495325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2496325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2497325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2498325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
2499325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
2500325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2501325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
2502325fc9f4SBarry Smith   PetscFunctionReturn(0);
2503325fc9f4SBarry Smith }
2504325fc9f4SBarry Smith 
2505325fc9f4SBarry Smith 
2506325fc9f4SBarry Smith #undef __FUNCT__
2507325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
2508325fc9f4SBarry Smith /*
2509325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
2510e3c5b3baSBarry 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
2511325fc9f4SBarry Smith 
2512325fc9f4SBarry Smith    Logically Collective on TS
2513325fc9f4SBarry Smith 
2514325fc9f4SBarry Smith    Input Parameters:
2515325fc9f4SBarry Smith +  snes - the TS context
2516325fc9f4SBarry Smith .  A,B - Jacobian matrices
2517325fc9f4SBarry Smith .  func - function evaluation routine
2518325fc9f4SBarry Smith -  ctx - user context
2519325fc9f4SBarry Smith 
2520325fc9f4SBarry Smith    Calling sequence of func:
2521325fc9f4SBarry Smith $    flag = func (TS snes,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx);
2522325fc9f4SBarry Smith 
2523325fc9f4SBarry Smith 
2524325fc9f4SBarry Smith    Level: developer
2525325fc9f4SBarry Smith 
2526325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2527325fc9f4SBarry Smith 
2528325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2529325fc9f4SBarry Smith */
25307087cfbeSBarry Smith PetscErrorCode  TSSetJacobianMatlab(TS snes,Mat A,Mat B,const char *func,mxArray *ctx)
2531325fc9f4SBarry Smith {
2532325fc9f4SBarry Smith   PetscErrorCode    ierr;
2533325fc9f4SBarry Smith   TSMatlabContext *sctx;
2534325fc9f4SBarry Smith 
2535325fc9f4SBarry Smith   PetscFunctionBegin;
2536325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2537325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2538325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2539325fc9f4SBarry Smith   /*
2540325fc9f4SBarry Smith      This should work, but it doesn't
2541325fc9f4SBarry Smith   sctx->ctx = ctx;
2542325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2543325fc9f4SBarry Smith   */
2544325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2545325fc9f4SBarry Smith   ierr = TSSetIJacobian(snes,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
2546325fc9f4SBarry Smith   PetscFunctionReturn(0);
2547325fc9f4SBarry Smith }
2548325fc9f4SBarry Smith 
2549b5b1a830SBarry Smith #undef __FUNCT__
2550b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
2551b5b1a830SBarry Smith /*
2552b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
2553b5b1a830SBarry Smith 
2554b5b1a830SBarry Smith    Collective on TS
2555b5b1a830SBarry Smith 
2556b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2557b5b1a830SBarry Smith @*/
25587087cfbeSBarry Smith PetscErrorCode  TSMonitor_Matlab(TS snes,PetscInt it, PetscReal time,Vec x, void *ctx)
2559b5b1a830SBarry Smith {
2560b5b1a830SBarry Smith   PetscErrorCode  ierr;
2561b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2562a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
2563b5b1a830SBarry Smith   mxArray	  *plhs[1],*prhs[6];
2564b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
2565b5b1a830SBarry Smith 
2566b5b1a830SBarry Smith   PetscFunctionBegin;
2567b5b1a830SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2568b5b1a830SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,4);
2569b5b1a830SBarry Smith 
2570b5b1a830SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2571b5b1a830SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2572b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2573b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
2574b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
2575b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
2576b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
2577b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
2578b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
2579b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2580b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
2581b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
2582b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
2583b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
2584b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
2585b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
2586b5b1a830SBarry Smith   PetscFunctionReturn(0);
2587b5b1a830SBarry Smith }
2588b5b1a830SBarry Smith 
2589b5b1a830SBarry Smith 
2590b5b1a830SBarry Smith #undef __FUNCT__
2591b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
2592b5b1a830SBarry Smith /*
2593b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
2594b5b1a830SBarry Smith 
2595b5b1a830SBarry Smith    Level: developer
2596b5b1a830SBarry Smith 
2597b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
2598b5b1a830SBarry Smith 
2599b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2600b5b1a830SBarry Smith */
26017087cfbeSBarry Smith PetscErrorCode  TSMonitorSetMatlab(TS snes,const char *func,mxArray *ctx)
2602b5b1a830SBarry Smith {
2603b5b1a830SBarry Smith   PetscErrorCode    ierr;
2604b5b1a830SBarry Smith   TSMatlabContext *sctx;
2605b5b1a830SBarry Smith 
2606b5b1a830SBarry Smith   PetscFunctionBegin;
2607b5b1a830SBarry Smith   /* currently sctx is memory bleed */
2608b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2609b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2610b5b1a830SBarry Smith   /*
2611b5b1a830SBarry Smith      This should work, but it doesn't
2612b5b1a830SBarry Smith   sctx->ctx = ctx;
2613b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2614b5b1a830SBarry Smith   */
2615b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2616b5b1a830SBarry Smith   ierr = TSMonitorSet(snes,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
2617b5b1a830SBarry Smith   PetscFunctionReturn(0);
2618b5b1a830SBarry Smith }
2619b5b1a830SBarry Smith 
2620325fc9f4SBarry Smith #endif
2621