xref: /petsc/src/ts/interface/ts.c (revision 4e68442233cded47fbbcb28b3d3c8c6b0e666d07)
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    Level: developer
204a7a1495cSBarry Smith 
205a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
206a7a1495cSBarry Smith 
20794b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
208a7a1495cSBarry Smith @*/
2097087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
210a7a1495cSBarry Smith {
211dfbe8321SBarry Smith   PetscErrorCode ierr;
212a7a1495cSBarry Smith 
213a7a1495cSBarry Smith   PetscFunctionBegin;
2140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2150700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
216c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
217000e7ae3SMatthew Knepley   if (ts->ops->rhsjacobian) {
218d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
219a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
220a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
221000e7ae3SMatthew Knepley     ierr = (*ts->ops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr);
222a7a1495cSBarry Smith     PetscStackPop;
223d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
224a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
2250700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
2260700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
227ef66eb69SBarry Smith   } else {
228ef66eb69SBarry Smith     ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
229ef66eb69SBarry Smith     ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
230ef66eb69SBarry Smith     if (*A != *B) {
231ef66eb69SBarry Smith       ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
232ef66eb69SBarry Smith       ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
233ef66eb69SBarry Smith     }
234089b2837SJed Brown     *flg = ts->rhsmatstructure;
235ef66eb69SBarry Smith   }
236a7a1495cSBarry Smith   PetscFunctionReturn(0);
237a7a1495cSBarry Smith }
238a7a1495cSBarry Smith 
2394a2ae208SSatish Balay #undef __FUNCT__
2404a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
241316643e7SJed Brown /*@
242d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
243d763cef2SBarry Smith 
244316643e7SJed Brown    Collective on TS and Vec
245316643e7SJed Brown 
246316643e7SJed Brown    Input Parameters:
247316643e7SJed Brown +  ts - the TS context
248316643e7SJed Brown .  t - current time
249316643e7SJed Brown -  x - state vector
250316643e7SJed Brown 
251316643e7SJed Brown    Output Parameter:
252316643e7SJed Brown .  y - right hand side
253316643e7SJed Brown 
254316643e7SJed Brown    Note:
255316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
256316643e7SJed Brown    is used internally within the nonlinear solvers.
257316643e7SJed Brown 
258316643e7SJed Brown    If the user did not provide a function but merely a matrix,
259d763cef2SBarry Smith    this routine applies the matrix.
260316643e7SJed Brown 
261316643e7SJed Brown    Level: developer
262316643e7SJed Brown 
263316643e7SJed Brown .keywords: TS, compute
264316643e7SJed Brown 
265316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
266316643e7SJed Brown @*/
267dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
268d763cef2SBarry Smith {
269dfbe8321SBarry Smith   PetscErrorCode ierr;
270d763cef2SBarry Smith 
271d763cef2SBarry Smith   PetscFunctionBegin;
2720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2730700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2740700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
275d763cef2SBarry Smith 
276d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
277000e7ae3SMatthew Knepley   if (ts->ops->rhsfunction) {
278d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
279000e7ae3SMatthew Knepley     ierr = (*ts->ops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr);
280d763cef2SBarry Smith     PetscStackPop;
281b2cd27e8SJed Brown   } else if (ts->problem_type == TS_LINEAR) {
282089b2837SJed Brown     Mat A,B;
283089b2837SJed Brown     ierr = TSGetMatrices(ts,&A,&B,PETSC_NULL, PETSC_NULL,PETSC_NULL,PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
284b2cd27e8SJed Brown     if (ts->ops->rhsmatrix) {
285089b2837SJed Brown       ts->rhsmatstructure = DIFFERENT_NONZERO_PATTERN;
286d763cef2SBarry Smith       PetscStackPush("TS user right-hand-side matrix function");
287089b2837SJed Brown       ierr = (*ts->ops->rhsmatrix)(ts,t,&A,&B,&ts->rhsmatstructure,ts->jacP);CHKERRQ(ierr);
288d763cef2SBarry Smith       PetscStackPop;
289089b2837SJed Brown       /* call TSSetMatrices() in case the user changed the pointers */
290*4e684422SJed Brown       ierr = TSSetMatrices(ts,A,B,PETSC_NULL, PETSC_NULL,PETSC_NULL,PETSC_NULL,DIFFERENT_NONZERO_PATTERN,PETSC_NULL);CHKERRQ(ierr);
291b2cd27e8SJed Brown     }
292089b2837SJed Brown     ierr = MatMult(A,x,y);CHKERRQ(ierr);
293d16dab79SJed Brown   } else SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"No RHS provided, must call TSSetRHSFunction() or TSSetMatrices()");
294d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
295d763cef2SBarry Smith   PetscFunctionReturn(0);
296d763cef2SBarry Smith }
297d763cef2SBarry Smith 
2984a2ae208SSatish Balay #undef __FUNCT__
299316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
300316643e7SJed Brown /*@
301316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
302316643e7SJed Brown 
303316643e7SJed Brown    Collective on TS and Vec
304316643e7SJed Brown 
305316643e7SJed Brown    Input Parameters:
306316643e7SJed Brown +  ts - the TS context
307316643e7SJed Brown .  t - current time
308316643e7SJed Brown .  X - state vector
309316643e7SJed Brown -  Xdot - time derivative of state vector
310316643e7SJed Brown 
311316643e7SJed Brown    Output Parameter:
312316643e7SJed Brown .  Y - right hand side
313316643e7SJed Brown 
314316643e7SJed Brown    Note:
315316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
316316643e7SJed Brown    is used internally within the nonlinear solvers.
317316643e7SJed Brown 
318316643e7SJed Brown    If the user did did not write their equations in implicit form, this
319316643e7SJed Brown    function recasts them in implicit form.
320316643e7SJed Brown 
321316643e7SJed Brown    Level: developer
322316643e7SJed Brown 
323316643e7SJed Brown .keywords: TS, compute
324316643e7SJed Brown 
325316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
326316643e7SJed Brown @*/
327316643e7SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y)
328316643e7SJed Brown {
329316643e7SJed Brown   PetscErrorCode ierr;
330316643e7SJed Brown 
331316643e7SJed Brown   PetscFunctionBegin;
3320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3330700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3340700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
3350700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
336316643e7SJed Brown 
337316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
338316643e7SJed Brown   if (ts->ops->ifunction) {
339316643e7SJed Brown     PetscStackPush("TS user implicit function");
340316643e7SJed Brown     ierr = (*ts->ops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr);
341316643e7SJed Brown     PetscStackPop;
342316643e7SJed Brown   } else {
343089b2837SJed Brown     ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr);
3444a6899ffSJed Brown     /* Convert to implicit form: F(X,Xdot) = Alhs * Xdot - Frhs(X) */
3454a6899ffSJed Brown     if (ts->ops->lhsmatrix) {
346089b2837SJed Brown       ts->lhsmatstructure = DIFFERENT_NONZERO_PATTERN;
3474a6899ffSJed Brown       PetscStackPush("TS user left-hand-side matrix function");
348089b2837SJed Brown       ierr = (*ts->ops->lhsmatrix)(ts,t,&ts->Alhs,&ts->Blhs,&ts->lhsmatstructure,ts->jacP);CHKERRQ(ierr);
3494a6899ffSJed Brown       PetscStackPop;
3504a6899ffSJed Brown       ierr = VecScale(Y,-1.);CHKERRQ(ierr);
3514a6899ffSJed Brown       ierr = MatMultAdd(ts->Alhs,Xdot,Y,Y);CHKERRQ(ierr);
3524a6899ffSJed Brown     } else {
353ace68cafSJed Brown       ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr);
354316643e7SJed Brown     }
3554a6899ffSJed Brown   }
356316643e7SJed Brown   ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
357316643e7SJed Brown   PetscFunctionReturn(0);
358316643e7SJed Brown }
359316643e7SJed Brown 
360316643e7SJed Brown #undef __FUNCT__
361316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
362316643e7SJed Brown /*@
363316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
364316643e7SJed Brown 
365316643e7SJed Brown    Collective on TS and Vec
366316643e7SJed Brown 
367316643e7SJed Brown    Input
368316643e7SJed Brown       Input Parameters:
369316643e7SJed Brown +  ts - the TS context
370316643e7SJed Brown .  t - current timestep
371316643e7SJed Brown .  X - state vector
372316643e7SJed Brown .  Xdot - time derivative of state vector
373316643e7SJed Brown -  shift - shift to apply, see note below
374316643e7SJed Brown 
375316643e7SJed Brown    Output Parameters:
376316643e7SJed Brown +  A - Jacobian matrix
377316643e7SJed Brown .  B - optional preconditioning matrix
378316643e7SJed Brown -  flag - flag indicating matrix structure
379316643e7SJed Brown 
380316643e7SJed Brown    Notes:
381316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
382316643e7SJed Brown 
383316643e7SJed Brown    dF/dX + shift*dF/dXdot
384316643e7SJed Brown 
385316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
386316643e7SJed Brown    is used internally within the nonlinear solvers.
387316643e7SJed Brown 
388316643e7SJed Brown    Level: developer
389316643e7SJed Brown 
390316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
391316643e7SJed Brown 
392316643e7SJed Brown .seealso:  TSSetIJacobian()
39363495f91SJed Brown @*/
3947087cfbeSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg)
395316643e7SJed Brown {
396316643e7SJed Brown   PetscErrorCode ierr;
397316643e7SJed Brown 
398316643e7SJed Brown   PetscFunctionBegin;
3990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4000700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4010700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
402316643e7SJed Brown   PetscValidPointer(A,6);
4030700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
404316643e7SJed Brown   PetscValidPointer(B,7);
4050700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
406316643e7SJed Brown   PetscValidPointer(flg,8);
407316643e7SJed Brown 
408*4e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
409316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
410316643e7SJed Brown   if (ts->ops->ijacobian) {
411316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
412316643e7SJed Brown     ierr = (*ts->ops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr);
413316643e7SJed Brown     PetscStackPop;
414316643e7SJed Brown   } else {
415*4e684422SJed Brown     ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr);
416316643e7SJed Brown 
417316643e7SJed Brown     /* Convert to implicit form */
418316643e7SJed Brown     /* inefficient because these operations will normally traverse all matrix elements twice */
419316643e7SJed Brown     ierr = MatScale(*A,-1);CHKERRQ(ierr);
4204a6899ffSJed Brown     if (ts->Alhs) {
4214a6899ffSJed Brown       ierr = MatAXPY(*A,shift,ts->Alhs,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
4224a6899ffSJed Brown     } else {
423316643e7SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
4244a6899ffSJed Brown     }
425316643e7SJed Brown     if (*A != *B) {
426316643e7SJed Brown       ierr = MatScale(*B,-1);CHKERRQ(ierr);
427316643e7SJed Brown       ierr = MatShift(*B,shift);CHKERRQ(ierr);
428316643e7SJed Brown     }
429316643e7SJed Brown   }
430316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
431316643e7SJed Brown   PetscFunctionReturn(0);
432316643e7SJed Brown }
433316643e7SJed Brown 
434316643e7SJed Brown #undef __FUNCT__
4354a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
436d763cef2SBarry Smith /*@C
437d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
438d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
439d763cef2SBarry Smith 
4403f9fe445SBarry Smith     Logically Collective on TS
441d763cef2SBarry Smith 
442d763cef2SBarry Smith     Input Parameters:
443d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
444ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
445d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
446d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
447d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
448d763cef2SBarry Smith 
449d763cef2SBarry Smith     Calling sequence of func:
45087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
451d763cef2SBarry Smith 
452d763cef2SBarry Smith +   t - current timestep
453d763cef2SBarry Smith .   u - input vector
454d763cef2SBarry Smith .   F - function vector
455d763cef2SBarry Smith -   ctx - [optional] user-defined function context
456d763cef2SBarry Smith 
457d763cef2SBarry Smith     Important:
45876f2fa84SHong Zhang     The user MUST call either this routine or TSSetMatrices().
459d763cef2SBarry Smith 
460d763cef2SBarry Smith     Level: beginner
461d763cef2SBarry Smith 
462d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
463d763cef2SBarry Smith 
46476f2fa84SHong Zhang .seealso: TSSetMatrices()
465d763cef2SBarry Smith @*/
466089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
467d763cef2SBarry Smith {
468089b2837SJed Brown   PetscErrorCode ierr;
469089b2837SJed Brown   SNES           snes;
470d763cef2SBarry Smith 
471089b2837SJed Brown   PetscFunctionBegin;
4720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
473ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
474*4e684422SJed Brown   if (f)   ts->ops->rhsfunction = f;
475*4e684422SJed Brown   if (ctx) ts->funP             = ctx;
476089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
477089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
478d763cef2SBarry Smith   PetscFunctionReturn(0);
479d763cef2SBarry Smith }
480d763cef2SBarry Smith 
4814a2ae208SSatish Balay #undef __FUNCT__
48295f0b562SHong Zhang #define __FUNCT__ "TSSetMatrices"
48395f0b562SHong Zhang /*@C
48495f0b562SHong Zhang    TSSetMatrices - Sets the functions to compute the matrices Alhs and Arhs,
48595f0b562SHong Zhang    where Alhs(t) U_t = Arhs(t) U.
48695f0b562SHong Zhang 
4873f9fe445SBarry Smith    Logically Collective on TS
48895f0b562SHong Zhang 
48995f0b562SHong Zhang    Input Parameters:
49095f0b562SHong Zhang +  ts   - the TS context obtained from TSCreate()
49195f0b562SHong Zhang .  Arhs - matrix
492089b2837SJed Brown .  Brhs - preconditioning matrix
493089b2837SJed Brown .  frhs - the matrix evaluation routine for Arhs and Brhs; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran)
49495f0b562SHong Zhang           if Arhs is not a function of t.
49595f0b562SHong Zhang .  Alhs - matrix or PETSC_NULL if Alhs is an indentity matrix.
496089b2837SJed Brown .  Blhs - preconditioning matrix or PETSC_NULL if Blhs is an identity matrix
49795f0b562SHong Zhang .  flhs - the matrix evaluation routine for Alhs; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran)
49895f0b562SHong Zhang           if Alhs is not a function of t.
49995f0b562SHong Zhang .  flag - flag indicating information about the matrix structure of Arhs and Alhs.
50095f0b562SHong Zhang           The available options are
50195f0b562SHong Zhang             SAME_NONZERO_PATTERN - Alhs has the same nonzero structure as Arhs
50295f0b562SHong Zhang             DIFFERENT_NONZERO_PATTERN - Alhs has different nonzero structure as Arhs
50395f0b562SHong Zhang -  ctx  - [optional] user-defined context for private data for the
50495f0b562SHong Zhang           matrix evaluation routine (may be PETSC_NULL)
50595f0b562SHong Zhang 
50695f0b562SHong Zhang    Calling sequence of func:
50795f0b562SHong Zhang $     func(TS ts,PetscReal t,Mat *A,Mat *B,PetscInt *flag,void *ctx);
50895f0b562SHong Zhang 
50995f0b562SHong Zhang +  t - current timestep
51095f0b562SHong Zhang .  A - matrix A, where U_t = A(t) U
51195f0b562SHong Zhang .  B - preconditioner matrix, usually the same as A
51295f0b562SHong Zhang .  flag - flag indicating information about the preconditioner matrix
51395f0b562SHong Zhang           structure (same as flag in KSPSetOperators())
51495f0b562SHong Zhang -  ctx - [optional] user-defined context for matrix evaluation routine
51595f0b562SHong Zhang 
51695f0b562SHong Zhang    Notes:
51795f0b562SHong Zhang    The routine func() takes Mat* as the matrix arguments rather than Mat.
51895f0b562SHong Zhang    This allows the matrix evaluation routine to replace Arhs or Alhs with a
51995f0b562SHong Zhang    completely new new matrix structure (not just different matrix elements)
52095f0b562SHong Zhang    when appropriate, for instance, if the nonzero structure is changing
52195f0b562SHong Zhang    throughout the global iterations.
52295f0b562SHong Zhang 
52395f0b562SHong Zhang    Important:
52495f0b562SHong Zhang    The user MUST call either this routine or TSSetRHSFunction().
52595f0b562SHong Zhang 
52695f0b562SHong Zhang    Level: beginner
52795f0b562SHong Zhang 
52895f0b562SHong Zhang .keywords: TS, timestep, set, matrix
52995f0b562SHong Zhang 
53095f0b562SHong Zhang .seealso: TSSetRHSFunction()
53195f0b562SHong Zhang @*/
532089b2837SJed 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)
53395f0b562SHong Zhang {
534277b19d0SLisandro Dalcin   PetscErrorCode ierr;
535089b2837SJed Brown   SNES           snes;
536277b19d0SLisandro Dalcin 
53795f0b562SHong Zhang   PetscFunctionBegin;
5380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
539*4e684422SJed Brown   if (ts->problem_type != TS_LINEAR) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Only for linear problems");
540277b19d0SLisandro Dalcin   if (frhs) ts->ops->rhsmatrix = frhs;
541277b19d0SLisandro Dalcin   if (flhs) ts->ops->lhsmatrix = flhs;
542277b19d0SLisandro Dalcin   if (ctx)  ts->jacP           = ctx;
543*4e684422SJed Brown   if (Arhs) {
544*4e684422SJed Brown     PetscValidHeaderSpecific(Arhs,MAT_CLASSID,2);
545*4e684422SJed Brown     PetscCheckSameComm(ts,1,Arhs,2);
546*4e684422SJed Brown     ierr = PetscObjectReference((PetscObject)Arhs);CHKERRQ(ierr);
547*4e684422SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
548*4e684422SJed Brown     ts->Arhs = Arhs;
549*4e684422SJed Brown   }
550*4e684422SJed Brown   if (Brhs) {
551*4e684422SJed Brown     PetscValidHeaderSpecific(Brhs,MAT_CLASSID,3);
552*4e684422SJed Brown     PetscCheckSameComm(ts,1,Brhs,3);
553*4e684422SJed Brown     ierr = PetscObjectReference((PetscObject)Brhs);CHKERRQ(ierr);
554*4e684422SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
555*4e684422SJed Brown     ts->Brhs = Brhs;
556*4e684422SJed Brown   }
55792af4f6aSHong Zhang   if (Alhs) {
558*4e684422SJed Brown     PetscValidHeaderSpecific(Alhs,MAT_CLASSID,5);
559*4e684422SJed Brown     PetscCheckSameComm(ts,1,Alhs,5);
560277b19d0SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)Alhs);CHKERRQ(ierr);
5616bf464f9SBarry Smith     ierr = MatDestroy(&ts->Alhs);CHKERRQ(ierr);
56295f0b562SHong Zhang     ts->Alhs = Alhs;
56392af4f6aSHong Zhang   }
564089b2837SJed Brown   if (Blhs) {
565*4e684422SJed Brown     PetscValidHeaderSpecific(Blhs,MAT_CLASSID,6);
566*4e684422SJed Brown     PetscCheckSameComm(ts,1,Blhs,6);
567089b2837SJed Brown     ierr = PetscObjectReference((PetscObject)Blhs);CHKERRQ(ierr);
568089b2837SJed Brown     ierr = MatDestroy(&ts->Blhs);CHKERRQ(ierr);
569089b2837SJed Brown     ts->Blhs = Blhs;
570089b2837SJed Brown   }
571089b2837SJed Brown   ts->rhsmatstructure = flag;
572089b2837SJed Brown   ts->lhsmatstructure = flag;
573*4e684422SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
574*4e684422SJed Brown   ierr = SNESSetFunction(snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
575*4e684422SJed Brown   if (frhs) {
576*4e684422SJed Brown     /* The RHS matrix is recomputed every step so we can share it with SNES and shift it in-place inside of TSComputeIJacobian() */
577*4e684422SJed Brown     ierr = SNESSetJacobian(snes,Arhs,Brhs,SNESTSFormJacobian,ts);CHKERRQ(ierr);
578*4e684422SJed Brown   }
57995f0b562SHong Zhang   PetscFunctionReturn(0);
58095f0b562SHong Zhang }
581d763cef2SBarry Smith 
582aa644b49SHong Zhang #undef __FUNCT__
583cda39b92SHong Zhang #define __FUNCT__ "TSGetMatrices"
584cda39b92SHong Zhang /*@C
585cda39b92SHong Zhang    TSGetMatrices - Returns the matrices Arhs and Alhs at the present timestep,
586cda39b92SHong Zhang    where Alhs(t) U_t = Arhs(t) U.
587cda39b92SHong Zhang 
588cda39b92SHong Zhang    Not Collective, but parallel objects are returned if TS is parallel
589cda39b92SHong Zhang 
590cda39b92SHong Zhang    Input Parameter:
591cda39b92SHong Zhang .  ts  - The TS context obtained from TSCreate()
592cda39b92SHong Zhang 
593cda39b92SHong Zhang    Output Parameters:
594cda39b92SHong Zhang +  Arhs - The right-hand side matrix
595cda39b92SHong Zhang .  Alhs - The left-hand side matrix
596cda39b92SHong Zhang -  ctx - User-defined context for matrix evaluation routine
597cda39b92SHong Zhang 
598cda39b92SHong Zhang    Notes: You can pass in PETSC_NULL for any return argument you do not need.
599cda39b92SHong Zhang 
600cda39b92SHong Zhang    Level: intermediate
601cda39b92SHong Zhang 
602cda39b92SHong Zhang .seealso: TSSetMatrices(), TSGetTimeStep(), TSGetTime(), TSGetTimeStepNumber(), TSGetRHSJacobian()
603cda39b92SHong Zhang 
604cda39b92SHong Zhang .keywords: TS, timestep, get, matrix
605cda39b92SHong Zhang 
606cda39b92SHong Zhang @*/
607089b2837SJed Brown PetscErrorCode  TSGetMatrices(TS ts,Mat *Arhs,Mat *Brhs,TSMatrix *frhs,Mat *Alhs,Mat *Blhs,TSMatrix *flhs,void **ctx)
608cda39b92SHong Zhang {
609089b2837SJed Brown   PetscErrorCode ierr;
610089b2837SJed Brown   SNES           snes;
611089b2837SJed Brown 
612cda39b92SHong Zhang   PetscFunctionBegin;
6130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
614*4e684422SJed Brown   if (ts->problem_type != TS_LINEAR) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Only for linear problems");
615*4e684422SJed Brown   if (Arhs) *Arhs = ts->Arhs;
616*4e684422SJed Brown   if (Brhs) *Brhs = ts->Brhs;
617089b2837SJed Brown   if (frhs) *frhs = ts->ops->rhsmatrix;
618cda39b92SHong Zhang   if (Alhs) *Alhs = ts->Alhs;
619089b2837SJed Brown   if (Blhs) *Blhs = ts->Blhs;
620089b2837SJed Brown   if (flhs) *flhs = ts->ops->lhsmatrix;
621cda39b92SHong Zhang   if (ctx)  *ctx = ts->jacP;
622cda39b92SHong Zhang   PetscFunctionReturn(0);
623cda39b92SHong Zhang }
624cda39b92SHong Zhang 
625cda39b92SHong Zhang #undef __FUNCT__
6264a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
627d763cef2SBarry Smith /*@C
628d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
629d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
63076f2fa84SHong Zhang    Use TSSetMatrices() for linear problems.
631d763cef2SBarry Smith 
6323f9fe445SBarry Smith    Logically Collective on TS
633d763cef2SBarry Smith 
634d763cef2SBarry Smith    Input Parameters:
635d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
636d763cef2SBarry Smith .  A   - Jacobian matrix
637d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
638d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
639d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
640d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
641d763cef2SBarry Smith 
642d763cef2SBarry Smith    Calling sequence of func:
64387828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
644d763cef2SBarry Smith 
645d763cef2SBarry Smith +  t - current timestep
646d763cef2SBarry Smith .  u - input vector
647d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
648d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
649d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
65094b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
651d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
652d763cef2SBarry Smith 
653d763cef2SBarry Smith    Notes:
65494b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
655d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
656d763cef2SBarry Smith 
657d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
658d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
65956335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
660d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
661d763cef2SBarry Smith    throughout the global iterations.
662d763cef2SBarry Smith 
663d763cef2SBarry Smith    Level: beginner
664d763cef2SBarry Smith 
665d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
666d763cef2SBarry Smith 
667d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(),
66876f2fa84SHong Zhang           SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices()
669d763cef2SBarry Smith 
670d763cef2SBarry Smith @*/
671089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
672d763cef2SBarry Smith {
673277b19d0SLisandro Dalcin   PetscErrorCode ierr;
674089b2837SJed Brown   SNES           snes;
675277b19d0SLisandro Dalcin 
676d763cef2SBarry Smith   PetscFunctionBegin;
6770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
678277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
679277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
680277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
681277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
68217186662SBarry Smith   if (ts->problem_type != TS_NONLINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not for linear problems; use TSSetMatrices()");
683d763cef2SBarry Smith 
684277b19d0SLisandro Dalcin   if (f)   ts->ops->rhsjacobian = f;
685277b19d0SLisandro Dalcin   if (ctx) ts->jacP             = ctx;
686089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
687089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
688d763cef2SBarry Smith   PetscFunctionReturn(0);
689d763cef2SBarry Smith }
690d763cef2SBarry Smith 
691316643e7SJed Brown 
692316643e7SJed Brown #undef __FUNCT__
693316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
694316643e7SJed Brown /*@C
695316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
696316643e7SJed Brown 
6973f9fe445SBarry Smith    Logically Collective on TS
698316643e7SJed Brown 
699316643e7SJed Brown    Input Parameters:
700316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
701ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
702316643e7SJed Brown .  f   - the function evaluation routine
703316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
704316643e7SJed Brown 
705316643e7SJed Brown    Calling sequence of f:
706316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
707316643e7SJed Brown 
708316643e7SJed Brown +  t   - time at step/stage being solved
709316643e7SJed Brown .  u   - state vector
710316643e7SJed Brown .  u_t - time derivative of state vector
711316643e7SJed Brown .  F   - function vector
712316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
713316643e7SJed Brown 
714316643e7SJed Brown    Important:
715316643e7SJed Brown    The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices().  This routine must be used when not solving an ODE.
716316643e7SJed Brown 
717316643e7SJed Brown    Level: beginner
718316643e7SJed Brown 
719316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
720316643e7SJed Brown 
721316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian()
722316643e7SJed Brown @*/
723089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
724316643e7SJed Brown {
725089b2837SJed Brown   PetscErrorCode ierr;
726089b2837SJed Brown   SNES           snes;
727316643e7SJed Brown 
728316643e7SJed Brown   PetscFunctionBegin;
7290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
730ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
731089b2837SJed Brown   if (f)   ts->ops->ifunction = f;
732089b2837SJed Brown   if (ctx) ts->funP           = ctx;
733089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
734089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
735089b2837SJed Brown   PetscFunctionReturn(0);
736089b2837SJed Brown }
737089b2837SJed Brown 
738089b2837SJed Brown #undef __FUNCT__
739089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
740089b2837SJed Brown /*@C
741089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
742089b2837SJed Brown 
743089b2837SJed Brown    Not Collective
744089b2837SJed Brown 
745089b2837SJed Brown    Input Parameter:
746089b2837SJed Brown .  ts - the TS context
747089b2837SJed Brown 
748089b2837SJed Brown    Output Parameter:
749089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
750089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
751089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
752089b2837SJed Brown 
753089b2837SJed Brown    Level: advanced
754089b2837SJed Brown 
755089b2837SJed Brown .keywords: TS, nonlinear, get, function
756089b2837SJed Brown 
757089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
758089b2837SJed Brown @*/
759089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
760089b2837SJed Brown {
761089b2837SJed Brown   PetscErrorCode ierr;
762089b2837SJed Brown   SNES snes;
763089b2837SJed Brown 
764089b2837SJed Brown   PetscFunctionBegin;
765089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
766089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
767089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
768089b2837SJed Brown   if (func) *func = ts->ops->ifunction;
769089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
770089b2837SJed Brown   PetscFunctionReturn(0);
771089b2837SJed Brown }
772089b2837SJed Brown 
773089b2837SJed Brown #undef __FUNCT__
774089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
775089b2837SJed Brown /*@C
776089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
777089b2837SJed Brown 
778089b2837SJed Brown    Not Collective
779089b2837SJed Brown 
780089b2837SJed Brown    Input Parameter:
781089b2837SJed Brown .  ts - the TS context
782089b2837SJed Brown 
783089b2837SJed Brown    Output Parameter:
784089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
785089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
786089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
787089b2837SJed Brown 
788089b2837SJed Brown    Level: advanced
789089b2837SJed Brown 
790089b2837SJed Brown .keywords: TS, nonlinear, get, function
791089b2837SJed Brown 
792089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
793089b2837SJed Brown @*/
794089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
795089b2837SJed Brown {
796089b2837SJed Brown   PetscErrorCode ierr;
797089b2837SJed Brown   SNES snes;
798089b2837SJed Brown 
799089b2837SJed Brown   PetscFunctionBegin;
800089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
801089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
802089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
803089b2837SJed Brown   if (func) *func = ts->ops->rhsfunction;
804089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
805316643e7SJed Brown   PetscFunctionReturn(0);
806316643e7SJed Brown }
807316643e7SJed Brown 
808316643e7SJed Brown #undef __FUNCT__
809316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
810316643e7SJed Brown /*@C
811a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
812a4f0a591SBarry Smith         you provided with TSSetIFunction().
813316643e7SJed Brown 
8143f9fe445SBarry Smith    Logically Collective on TS
815316643e7SJed Brown 
816316643e7SJed Brown    Input Parameters:
817316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
818316643e7SJed Brown .  A   - Jacobian matrix
819316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
820316643e7SJed Brown .  f   - the Jacobian evaluation routine
821316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
822316643e7SJed Brown 
823316643e7SJed Brown    Calling sequence of f:
8241b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
825316643e7SJed Brown 
826316643e7SJed Brown +  t    - time at step/stage being solved
8271b4a444bSJed Brown .  U    - state vector
8281b4a444bSJed Brown .  U_t  - time derivative of state vector
829316643e7SJed Brown .  a    - shift
8301b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
831316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
832316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
833316643e7SJed Brown           structure (same as flag in KSPSetOperators())
834316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
835316643e7SJed Brown 
836316643e7SJed Brown    Notes:
837316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
838316643e7SJed Brown 
839a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
840a4f0a591SBarry 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.
841a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
842a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
843a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
844a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
845a4f0a591SBarry Smith 
846316643e7SJed Brown    Level: beginner
847316643e7SJed Brown 
848316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
849316643e7SJed Brown 
850316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian()
851316643e7SJed Brown 
852316643e7SJed Brown @*/
8537087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
854316643e7SJed Brown {
855316643e7SJed Brown   PetscErrorCode ierr;
856089b2837SJed Brown   SNES           snes;
857316643e7SJed Brown 
858316643e7SJed Brown   PetscFunctionBegin;
8590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8600700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
8610700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
862316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
863316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
864316643e7SJed Brown   if (f)   ts->ops->ijacobian = f;
865316643e7SJed Brown   if (ctx) ts->jacP           = ctx;
866089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
867089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
868316643e7SJed Brown   PetscFunctionReturn(0);
869316643e7SJed Brown }
870316643e7SJed Brown 
8714a2ae208SSatish Balay #undef __FUNCT__
8724a2ae208SSatish Balay #define __FUNCT__ "TSView"
8737e2c5f70SBarry Smith /*@C
874d763cef2SBarry Smith     TSView - Prints the TS data structure.
875d763cef2SBarry Smith 
8764c49b128SBarry Smith     Collective on TS
877d763cef2SBarry Smith 
878d763cef2SBarry Smith     Input Parameters:
879d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
880d763cef2SBarry Smith -   viewer - visualization context
881d763cef2SBarry Smith 
882d763cef2SBarry Smith     Options Database Key:
883d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
884d763cef2SBarry Smith 
885d763cef2SBarry Smith     Notes:
886d763cef2SBarry Smith     The available visualization contexts include
887b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
888b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
889d763cef2SBarry Smith          output where only the first processor opens
890d763cef2SBarry Smith          the file.  All other processors send their
891d763cef2SBarry Smith          data to the first processor to print.
892d763cef2SBarry Smith 
893d763cef2SBarry Smith     The user can open an alternative visualization context with
894b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
895d763cef2SBarry Smith 
896d763cef2SBarry Smith     Level: beginner
897d763cef2SBarry Smith 
898d763cef2SBarry Smith .keywords: TS, timestep, view
899d763cef2SBarry Smith 
900b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
901d763cef2SBarry Smith @*/
9027087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
903d763cef2SBarry Smith {
904dfbe8321SBarry Smith   PetscErrorCode ierr;
905a313700dSBarry Smith   const TSType   type;
906089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
907d763cef2SBarry Smith 
908d763cef2SBarry Smith   PetscFunctionBegin;
9090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9103050cee2SBarry Smith   if (!viewer) {
9117adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
9123050cee2SBarry Smith   }
9130700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
914c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
915fd16b177SBarry Smith 
9162692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
9172692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
91832077d6dSBarry Smith   if (iascii) {
919317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
920000e7ae3SMatthew Knepley     if (ts->ops->view) {
921b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
922000e7ae3SMatthew Knepley       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
923b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
924d763cef2SBarry Smith     }
92577431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
926a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
927d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
92877431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr);
929d763cef2SBarry Smith     }
93077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr);
9310f5bd95cSBarry Smith   } else if (isstring) {
932a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
933b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
934d763cef2SBarry Smith   }
935b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
936089b2837SJed Brown   ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
937089b2837SJed Brown   if (!isundials && ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
938b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
939d763cef2SBarry Smith   PetscFunctionReturn(0);
940d763cef2SBarry Smith }
941d763cef2SBarry Smith 
942d763cef2SBarry Smith 
9434a2ae208SSatish Balay #undef __FUNCT__
9444a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
945b07ff414SBarry Smith /*@
946d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
947d763cef2SBarry Smith    the timesteppers.
948d763cef2SBarry Smith 
9493f9fe445SBarry Smith    Logically Collective on TS
950d763cef2SBarry Smith 
951d763cef2SBarry Smith    Input Parameters:
952d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
953d763cef2SBarry Smith -  usrP - optional user context
954d763cef2SBarry Smith 
955d763cef2SBarry Smith    Level: intermediate
956d763cef2SBarry Smith 
957d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
958d763cef2SBarry Smith 
959d763cef2SBarry Smith .seealso: TSGetApplicationContext()
960d763cef2SBarry Smith @*/
9617087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
962d763cef2SBarry Smith {
963d763cef2SBarry Smith   PetscFunctionBegin;
9640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
965d763cef2SBarry Smith   ts->user = usrP;
966d763cef2SBarry Smith   PetscFunctionReturn(0);
967d763cef2SBarry Smith }
968d763cef2SBarry Smith 
9694a2ae208SSatish Balay #undef __FUNCT__
9704a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
971b07ff414SBarry Smith /*@
972d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
973d763cef2SBarry Smith     timestepper.
974d763cef2SBarry Smith 
975d763cef2SBarry Smith     Not Collective
976d763cef2SBarry Smith 
977d763cef2SBarry Smith     Input Parameter:
978d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
979d763cef2SBarry Smith 
980d763cef2SBarry Smith     Output Parameter:
981d763cef2SBarry Smith .   usrP - user context
982d763cef2SBarry Smith 
983d763cef2SBarry Smith     Level: intermediate
984d763cef2SBarry Smith 
985d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
986d763cef2SBarry Smith 
987d763cef2SBarry Smith .seealso: TSSetApplicationContext()
988d763cef2SBarry Smith @*/
989e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
990d763cef2SBarry Smith {
991d763cef2SBarry Smith   PetscFunctionBegin;
9920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
993e71120c6SJed Brown   *(void**)usrP = ts->user;
994d763cef2SBarry Smith   PetscFunctionReturn(0);
995d763cef2SBarry Smith }
996d763cef2SBarry Smith 
9974a2ae208SSatish Balay #undef __FUNCT__
9984a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
999d763cef2SBarry Smith /*@
1000d763cef2SBarry Smith    TSGetTimeStepNumber - Gets the current number of timesteps.
1001d763cef2SBarry Smith 
1002d763cef2SBarry Smith    Not Collective
1003d763cef2SBarry Smith 
1004d763cef2SBarry Smith    Input Parameter:
1005d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1006d763cef2SBarry Smith 
1007d763cef2SBarry Smith    Output Parameter:
1008d763cef2SBarry Smith .  iter - number steps so far
1009d763cef2SBarry Smith 
1010d763cef2SBarry Smith    Level: intermediate
1011d763cef2SBarry Smith 
1012d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1013d763cef2SBarry Smith @*/
10147087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
1015d763cef2SBarry Smith {
1016d763cef2SBarry Smith   PetscFunctionBegin;
10170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10184482741eSBarry Smith   PetscValidIntPointer(iter,2);
1019d763cef2SBarry Smith   *iter = ts->steps;
1020d763cef2SBarry Smith   PetscFunctionReturn(0);
1021d763cef2SBarry Smith }
1022d763cef2SBarry Smith 
10234a2ae208SSatish Balay #undef __FUNCT__
10244a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1025d763cef2SBarry Smith /*@
1026d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1027d763cef2SBarry Smith    as well as the initial time.
1028d763cef2SBarry Smith 
10293f9fe445SBarry Smith    Logically Collective on TS
1030d763cef2SBarry Smith 
1031d763cef2SBarry Smith    Input Parameters:
1032d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1033d763cef2SBarry Smith .  initial_time - the initial time
1034d763cef2SBarry Smith -  time_step - the size of the timestep
1035d763cef2SBarry Smith 
1036d763cef2SBarry Smith    Level: intermediate
1037d763cef2SBarry Smith 
1038d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1039d763cef2SBarry Smith 
1040d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1041d763cef2SBarry Smith @*/
10427087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1043d763cef2SBarry Smith {
1044d763cef2SBarry Smith   PetscFunctionBegin;
10450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1046d763cef2SBarry Smith   ts->time_step         = time_step;
1047d763cef2SBarry Smith   ts->initial_time_step = time_step;
1048d763cef2SBarry Smith   ts->ptime             = initial_time;
1049d763cef2SBarry Smith   PetscFunctionReturn(0);
1050d763cef2SBarry Smith }
1051d763cef2SBarry Smith 
10524a2ae208SSatish Balay #undef __FUNCT__
10534a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1054d763cef2SBarry Smith /*@
1055d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1056d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1057d763cef2SBarry Smith 
10583f9fe445SBarry Smith    Logically Collective on TS
1059d763cef2SBarry Smith 
1060d763cef2SBarry Smith    Input Parameters:
1061d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1062d763cef2SBarry Smith -  time_step - the size of the timestep
1063d763cef2SBarry Smith 
1064d763cef2SBarry Smith    Level: intermediate
1065d763cef2SBarry Smith 
1066d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1067d763cef2SBarry Smith 
1068d763cef2SBarry Smith .keywords: TS, set, timestep
1069d763cef2SBarry Smith @*/
10707087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1071d763cef2SBarry Smith {
1072d763cef2SBarry Smith   PetscFunctionBegin;
10730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1074c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1075d763cef2SBarry Smith   ts->time_step = time_step;
1076d763cef2SBarry Smith   PetscFunctionReturn(0);
1077d763cef2SBarry Smith }
1078d763cef2SBarry Smith 
10794a2ae208SSatish Balay #undef __FUNCT__
10804a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1081d763cef2SBarry Smith /*@
1082d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1083d763cef2SBarry Smith 
1084d763cef2SBarry Smith    Not Collective
1085d763cef2SBarry Smith 
1086d763cef2SBarry Smith    Input Parameter:
1087d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1088d763cef2SBarry Smith 
1089d763cef2SBarry Smith    Output Parameter:
1090d763cef2SBarry Smith .  dt - the current timestep size
1091d763cef2SBarry Smith 
1092d763cef2SBarry Smith    Level: intermediate
1093d763cef2SBarry Smith 
1094d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1095d763cef2SBarry Smith 
1096d763cef2SBarry Smith .keywords: TS, get, timestep
1097d763cef2SBarry Smith @*/
10987087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1099d763cef2SBarry Smith {
1100d763cef2SBarry Smith   PetscFunctionBegin;
11010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11024482741eSBarry Smith   PetscValidDoublePointer(dt,2);
1103d763cef2SBarry Smith   *dt = ts->time_step;
1104d763cef2SBarry Smith   PetscFunctionReturn(0);
1105d763cef2SBarry Smith }
1106d763cef2SBarry Smith 
11074a2ae208SSatish Balay #undef __FUNCT__
11084a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1109d8e5e3e6SSatish Balay /*@
1110d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1111d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1112d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1113d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1114d763cef2SBarry Smith 
1115d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1116d763cef2SBarry Smith 
1117d763cef2SBarry Smith    Input Parameter:
1118d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1119d763cef2SBarry Smith 
1120d763cef2SBarry Smith    Output Parameter:
1121d763cef2SBarry Smith .  v - the vector containing the solution
1122d763cef2SBarry Smith 
1123d763cef2SBarry Smith    Level: intermediate
1124d763cef2SBarry Smith 
1125d763cef2SBarry Smith .seealso: TSGetTimeStep()
1126d763cef2SBarry Smith 
1127d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1128d763cef2SBarry Smith @*/
11297087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1130d763cef2SBarry Smith {
1131d763cef2SBarry Smith   PetscFunctionBegin;
11320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11334482741eSBarry Smith   PetscValidPointer(v,2);
11348737fe31SLisandro Dalcin   *v = ts->vec_sol;
1135d763cef2SBarry Smith   PetscFunctionReturn(0);
1136d763cef2SBarry Smith }
1137d763cef2SBarry Smith 
1138bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
11394a2ae208SSatish Balay #undef __FUNCT__
1140bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1141d8e5e3e6SSatish Balay /*@
1142bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1143d763cef2SBarry Smith 
1144bdad233fSMatthew Knepley   Not collective
1145d763cef2SBarry Smith 
1146bdad233fSMatthew Knepley   Input Parameters:
1147bdad233fSMatthew Knepley + ts   - The TS
1148bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1149d763cef2SBarry Smith .vb
1150d763cef2SBarry Smith          U_t = A U
1151d763cef2SBarry Smith          U_t = A(t) U
1152d763cef2SBarry Smith          U_t = F(t,U)
1153d763cef2SBarry Smith .ve
1154d763cef2SBarry Smith 
1155d763cef2SBarry Smith    Level: beginner
1156d763cef2SBarry Smith 
1157bdad233fSMatthew Knepley .keywords: TS, problem type
1158bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1159d763cef2SBarry Smith @*/
11607087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1161a7cc72afSBarry Smith {
11629e2a6581SJed Brown   PetscErrorCode ierr;
11639e2a6581SJed Brown 
1164d763cef2SBarry Smith   PetscFunctionBegin;
11650700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1166bdad233fSMatthew Knepley   ts->problem_type = type;
11679e2a6581SJed Brown   if (type == TS_LINEAR) {
11689e2a6581SJed Brown     SNES snes;
11699e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11709e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
11719e2a6581SJed Brown   }
1172d763cef2SBarry Smith   PetscFunctionReturn(0);
1173d763cef2SBarry Smith }
1174d763cef2SBarry Smith 
1175bdad233fSMatthew Knepley #undef __FUNCT__
1176bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1177bdad233fSMatthew Knepley /*@C
1178bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1179bdad233fSMatthew Knepley 
1180bdad233fSMatthew Knepley   Not collective
1181bdad233fSMatthew Knepley 
1182bdad233fSMatthew Knepley   Input Parameter:
1183bdad233fSMatthew Knepley . ts   - The TS
1184bdad233fSMatthew Knepley 
1185bdad233fSMatthew Knepley   Output Parameter:
1186bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1187bdad233fSMatthew Knepley .vb
1188089b2837SJed Brown          M U_t = A U
1189089b2837SJed Brown          M(t) U_t = A(t) U
1190bdad233fSMatthew Knepley          U_t = F(t,U)
1191bdad233fSMatthew Knepley .ve
1192bdad233fSMatthew Knepley 
1193bdad233fSMatthew Knepley    Level: beginner
1194bdad233fSMatthew Knepley 
1195bdad233fSMatthew Knepley .keywords: TS, problem type
1196bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1197bdad233fSMatthew Knepley @*/
11987087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1199a7cc72afSBarry Smith {
1200bdad233fSMatthew Knepley   PetscFunctionBegin;
12010700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
12024482741eSBarry Smith   PetscValidIntPointer(type,2);
1203bdad233fSMatthew Knepley   *type = ts->problem_type;
1204bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1205bdad233fSMatthew Knepley }
1206d763cef2SBarry Smith 
12074a2ae208SSatish Balay #undef __FUNCT__
12084a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1209d763cef2SBarry Smith /*@
1210d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1211d763cef2SBarry Smith    of a timestepper.
1212d763cef2SBarry Smith 
1213d763cef2SBarry Smith    Collective on TS
1214d763cef2SBarry Smith 
1215d763cef2SBarry Smith    Input Parameter:
1216d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1217d763cef2SBarry Smith 
1218d763cef2SBarry Smith    Notes:
1219d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1220d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1221d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1222d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1223d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1224d763cef2SBarry Smith 
1225d763cef2SBarry Smith    Level: advanced
1226d763cef2SBarry Smith 
1227d763cef2SBarry Smith .keywords: TS, timestep, setup
1228d763cef2SBarry Smith 
1229d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1230d763cef2SBarry Smith @*/
12317087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1232d763cef2SBarry Smith {
1233dfbe8321SBarry Smith   PetscErrorCode ierr;
1234d763cef2SBarry Smith 
1235d763cef2SBarry Smith   PetscFunctionBegin;
12360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1237277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1238277b19d0SLisandro Dalcin 
12397adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
12409596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1241d763cef2SBarry Smith   }
1242277b19d0SLisandro Dalcin 
1243277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1244277b19d0SLisandro Dalcin 
1245277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1246000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1247277b19d0SLisandro Dalcin   }
1248277b19d0SLisandro Dalcin 
1249277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1250277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1251277b19d0SLisandro Dalcin }
1252277b19d0SLisandro Dalcin 
1253277b19d0SLisandro Dalcin #undef __FUNCT__
1254277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1255277b19d0SLisandro Dalcin /*@
1256277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1257277b19d0SLisandro Dalcin 
1258277b19d0SLisandro Dalcin    Collective on TS
1259277b19d0SLisandro Dalcin 
1260277b19d0SLisandro Dalcin    Input Parameter:
1261277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1262277b19d0SLisandro Dalcin 
1263277b19d0SLisandro Dalcin    Level: beginner
1264277b19d0SLisandro Dalcin 
1265277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1266277b19d0SLisandro Dalcin 
1267277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1268277b19d0SLisandro Dalcin @*/
1269277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1270277b19d0SLisandro Dalcin {
1271277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1272277b19d0SLisandro Dalcin 
1273277b19d0SLisandro Dalcin   PetscFunctionBegin;
1274277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1275277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1276277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1277277b19d0SLisandro Dalcin   }
1278277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1279*4e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1280*4e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
12816bf464f9SBarry Smith   ierr = MatDestroy(&ts->Alhs);CHKERRQ(ierr);
1282089b2837SJed Brown   ierr = MatDestroy(&ts->Blhs);CHKERRQ(ierr);
12836bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1284277b19d0SLisandro Dalcin   if (ts->work) {ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);}
1285277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1286d763cef2SBarry Smith   PetscFunctionReturn(0);
1287d763cef2SBarry Smith }
1288d763cef2SBarry Smith 
12894a2ae208SSatish Balay #undef __FUNCT__
12904a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1291d8e5e3e6SSatish Balay /*@
1292d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1293d763cef2SBarry Smith    with TSCreate().
1294d763cef2SBarry Smith 
1295d763cef2SBarry Smith    Collective on TS
1296d763cef2SBarry Smith 
1297d763cef2SBarry Smith    Input Parameter:
1298d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1299d763cef2SBarry Smith 
1300d763cef2SBarry Smith    Level: beginner
1301d763cef2SBarry Smith 
1302d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1303d763cef2SBarry Smith 
1304d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1305d763cef2SBarry Smith @*/
13066bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1307d763cef2SBarry Smith {
13086849ba73SBarry Smith   PetscErrorCode ierr;
1309d763cef2SBarry Smith 
1310d763cef2SBarry Smith   PetscFunctionBegin;
13116bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
13126bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
13136bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1314d763cef2SBarry Smith 
13156bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1316277b19d0SLisandro Dalcin 
1317be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
13186bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
13196bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
13206d4c513bSLisandro Dalcin 
13216bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
13226bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
13236bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
13246d4c513bSLisandro Dalcin 
1325a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1326d763cef2SBarry Smith   PetscFunctionReturn(0);
1327d763cef2SBarry Smith }
1328d763cef2SBarry Smith 
13294a2ae208SSatish Balay #undef __FUNCT__
13304a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1331d8e5e3e6SSatish Balay /*@
1332d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1333d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1334d763cef2SBarry Smith 
1335d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1336d763cef2SBarry Smith 
1337d763cef2SBarry Smith    Input Parameter:
1338d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1339d763cef2SBarry Smith 
1340d763cef2SBarry Smith    Output Parameter:
1341d763cef2SBarry Smith .  snes - the nonlinear solver context
1342d763cef2SBarry Smith 
1343d763cef2SBarry Smith    Notes:
1344d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1345d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
134694b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1347d763cef2SBarry Smith 
1348d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1349d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1350d763cef2SBarry Smith 
1351d763cef2SBarry Smith    Level: beginner
1352d763cef2SBarry Smith 
1353d763cef2SBarry Smith .keywords: timestep, get, SNES
1354d763cef2SBarry Smith @*/
13557087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1356d763cef2SBarry Smith {
1357d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1358d372ba47SLisandro Dalcin 
1359d763cef2SBarry Smith   PetscFunctionBegin;
13600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13614482741eSBarry Smith   PetscValidPointer(snes,2);
1362d372ba47SLisandro Dalcin   if (!ts->snes) {
1363d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
1364d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1365d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
13669e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
13679e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
13689e2a6581SJed Brown     }
1369d372ba47SLisandro Dalcin   }
1370d763cef2SBarry Smith   *snes = ts->snes;
1371d763cef2SBarry Smith   PetscFunctionReturn(0);
1372d763cef2SBarry Smith }
1373d763cef2SBarry Smith 
13744a2ae208SSatish Balay #undef __FUNCT__
137594b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1376d8e5e3e6SSatish Balay /*@
137794b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1378d763cef2SBarry Smith    a TS (timestepper) context.
1379d763cef2SBarry Smith 
138094b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1381d763cef2SBarry Smith 
1382d763cef2SBarry Smith    Input Parameter:
1383d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1384d763cef2SBarry Smith 
1385d763cef2SBarry Smith    Output Parameter:
138694b7f48cSBarry Smith .  ksp - the nonlinear solver context
1387d763cef2SBarry Smith 
1388d763cef2SBarry Smith    Notes:
138994b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1390d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1391d763cef2SBarry Smith    KSP and PC contexts as well.
1392d763cef2SBarry Smith 
139394b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
139494b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1395d763cef2SBarry Smith 
1396d763cef2SBarry Smith    Level: beginner
1397d763cef2SBarry Smith 
139894b7f48cSBarry Smith .keywords: timestep, get, KSP
1399d763cef2SBarry Smith @*/
14007087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1401d763cef2SBarry Smith {
1402d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1403089b2837SJed Brown   SNES           snes;
1404d372ba47SLisandro Dalcin 
1405d763cef2SBarry Smith   PetscFunctionBegin;
14060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14074482741eSBarry Smith   PetscValidPointer(ksp,2);
140817186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1409e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1410089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1411089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1412d763cef2SBarry Smith   PetscFunctionReturn(0);
1413d763cef2SBarry Smith }
1414d763cef2SBarry Smith 
1415d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1416d763cef2SBarry Smith 
14174a2ae208SSatish Balay #undef __FUNCT__
1418adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1419adb62b0dSMatthew Knepley /*@
1420adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1421adb62b0dSMatthew Knepley    maximum time for iteration.
1422adb62b0dSMatthew Knepley 
14233f9fe445SBarry Smith    Not Collective
1424adb62b0dSMatthew Knepley 
1425adb62b0dSMatthew Knepley    Input Parameters:
1426adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1427adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1428adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1429adb62b0dSMatthew Knepley 
1430adb62b0dSMatthew Knepley    Level: intermediate
1431adb62b0dSMatthew Knepley 
1432adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1433adb62b0dSMatthew Knepley @*/
14347087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1435adb62b0dSMatthew Knepley {
1436adb62b0dSMatthew Knepley   PetscFunctionBegin;
14370700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1438abc0a331SBarry Smith   if (maxsteps) {
14394482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1440adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1441adb62b0dSMatthew Knepley   }
1442abc0a331SBarry Smith   if (maxtime ) {
14434482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1444adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1445adb62b0dSMatthew Knepley   }
1446adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1447adb62b0dSMatthew Knepley }
1448adb62b0dSMatthew Knepley 
1449adb62b0dSMatthew Knepley #undef __FUNCT__
14504a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1451d763cef2SBarry Smith /*@
1452d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1453d763cef2SBarry Smith    maximum time for iteration.
1454d763cef2SBarry Smith 
14553f9fe445SBarry Smith    Logically Collective on TS
1456d763cef2SBarry Smith 
1457d763cef2SBarry Smith    Input Parameters:
1458d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1459d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1460d763cef2SBarry Smith -  maxtime - final time to iterate to
1461d763cef2SBarry Smith 
1462d763cef2SBarry Smith    Options Database Keys:
1463d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
1464d763cef2SBarry Smith .  -ts_max_time <maxtime> - Sets maxtime
1465d763cef2SBarry Smith 
1466d763cef2SBarry Smith    Notes:
1467d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1468d763cef2SBarry Smith 
1469d763cef2SBarry Smith    Level: intermediate
1470d763cef2SBarry Smith 
1471d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1472d763cef2SBarry Smith @*/
14737087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1474d763cef2SBarry Smith {
1475d763cef2SBarry Smith   PetscFunctionBegin;
14760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1477c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1478c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
1479d763cef2SBarry Smith   ts->max_steps = maxsteps;
1480d763cef2SBarry Smith   ts->max_time  = maxtime;
1481d763cef2SBarry Smith   PetscFunctionReturn(0);
1482d763cef2SBarry Smith }
1483d763cef2SBarry Smith 
14844a2ae208SSatish Balay #undef __FUNCT__
14854a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1486d763cef2SBarry Smith /*@
1487d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1488d763cef2SBarry Smith    for use by the TS routines.
1489d763cef2SBarry Smith 
14903f9fe445SBarry Smith    Logically Collective on TS and Vec
1491d763cef2SBarry Smith 
1492d763cef2SBarry Smith    Input Parameters:
1493d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1494d763cef2SBarry Smith -  x - the solution vector
1495d763cef2SBarry Smith 
1496d763cef2SBarry Smith    Level: beginner
1497d763cef2SBarry Smith 
1498d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1499d763cef2SBarry Smith @*/
15007087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1501d763cef2SBarry Smith {
15028737fe31SLisandro Dalcin   PetscErrorCode ierr;
15038737fe31SLisandro Dalcin 
1504d763cef2SBarry Smith   PetscFunctionBegin;
15050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15060700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
15078737fe31SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
15086bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
15098737fe31SLisandro Dalcin   ts->vec_sol = x;
1510d763cef2SBarry Smith   PetscFunctionReturn(0);
1511d763cef2SBarry Smith }
1512d763cef2SBarry Smith 
1513e74ef692SMatthew Knepley #undef __FUNCT__
1514e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1515ac226902SBarry Smith /*@C
1516000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
15173f2090d5SJed Brown   called once at the beginning of each time step.
1518000e7ae3SMatthew Knepley 
15193f9fe445SBarry Smith   Logically Collective on TS
1520000e7ae3SMatthew Knepley 
1521000e7ae3SMatthew Knepley   Input Parameters:
1522000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1523000e7ae3SMatthew Knepley - func - The function
1524000e7ae3SMatthew Knepley 
1525000e7ae3SMatthew Knepley   Calling sequence of func:
1526000e7ae3SMatthew Knepley . func (TS ts);
1527000e7ae3SMatthew Knepley 
1528000e7ae3SMatthew Knepley   Level: intermediate
1529000e7ae3SMatthew Knepley 
1530000e7ae3SMatthew Knepley .keywords: TS, timestep
1531000e7ae3SMatthew Knepley @*/
15327087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1533000e7ae3SMatthew Knepley {
1534000e7ae3SMatthew Knepley   PetscFunctionBegin;
15350700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1536000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1537000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1538000e7ae3SMatthew Knepley }
1539000e7ae3SMatthew Knepley 
1540e74ef692SMatthew Knepley #undef __FUNCT__
15413f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
15423f2090d5SJed Brown /*@C
15433f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
15443f2090d5SJed Brown 
15453f2090d5SJed Brown   Collective on TS
15463f2090d5SJed Brown 
15473f2090d5SJed Brown   Input Parameters:
15483f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
15493f2090d5SJed Brown 
15503f2090d5SJed Brown   Notes:
15513f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
15523f2090d5SJed Brown   so most users would not generally call this routine themselves.
15533f2090d5SJed Brown 
15543f2090d5SJed Brown   Level: developer
15553f2090d5SJed Brown 
15563f2090d5SJed Brown .keywords: TS, timestep
15573f2090d5SJed Brown @*/
15587087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
15593f2090d5SJed Brown {
15603f2090d5SJed Brown   PetscErrorCode ierr;
15613f2090d5SJed Brown 
15623f2090d5SJed Brown   PetscFunctionBegin;
15630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
156472ac3e02SJed Brown   if (ts->ops->prestep) {
15653f2090d5SJed Brown     PetscStackPush("TS PreStep function");
15663f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
15673f2090d5SJed Brown     PetscStackPop;
1568312ce896SJed Brown   }
15693f2090d5SJed Brown   PetscFunctionReturn(0);
15703f2090d5SJed Brown }
15713f2090d5SJed Brown 
15723f2090d5SJed Brown #undef __FUNCT__
1573e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1574ac226902SBarry Smith /*@C
1575000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
15763f2090d5SJed Brown   called once at the end of each time step.
1577000e7ae3SMatthew Knepley 
15783f9fe445SBarry Smith   Logically Collective on TS
1579000e7ae3SMatthew Knepley 
1580000e7ae3SMatthew Knepley   Input Parameters:
1581000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1582000e7ae3SMatthew Knepley - func - The function
1583000e7ae3SMatthew Knepley 
1584000e7ae3SMatthew Knepley   Calling sequence of func:
1585000e7ae3SMatthew Knepley . func (TS ts);
1586000e7ae3SMatthew Knepley 
1587000e7ae3SMatthew Knepley   Level: intermediate
1588000e7ae3SMatthew Knepley 
1589000e7ae3SMatthew Knepley .keywords: TS, timestep
1590000e7ae3SMatthew Knepley @*/
15917087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1592000e7ae3SMatthew Knepley {
1593000e7ae3SMatthew Knepley   PetscFunctionBegin;
15940700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1595000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1596000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1597000e7ae3SMatthew Knepley }
1598000e7ae3SMatthew Knepley 
1599e74ef692SMatthew Knepley #undef __FUNCT__
16003f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
16013f2090d5SJed Brown /*@C
16023f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
16033f2090d5SJed Brown 
16043f2090d5SJed Brown   Collective on TS
16053f2090d5SJed Brown 
16063f2090d5SJed Brown   Input Parameters:
16073f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
16083f2090d5SJed Brown 
16093f2090d5SJed Brown   Notes:
16103f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
16113f2090d5SJed Brown   so most users would not generally call this routine themselves.
16123f2090d5SJed Brown 
16133f2090d5SJed Brown   Level: developer
16143f2090d5SJed Brown 
16153f2090d5SJed Brown .keywords: TS, timestep
16163f2090d5SJed Brown @*/
16177087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
16183f2090d5SJed Brown {
16193f2090d5SJed Brown   PetscErrorCode ierr;
16203f2090d5SJed Brown 
16213f2090d5SJed Brown   PetscFunctionBegin;
16220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
162372ac3e02SJed Brown   if (ts->ops->poststep) {
16243f2090d5SJed Brown     PetscStackPush("TS PostStep function");
16253f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
16263f2090d5SJed Brown     PetscStackPop;
162772ac3e02SJed Brown   }
16283f2090d5SJed Brown   PetscFunctionReturn(0);
16293f2090d5SJed Brown }
16303f2090d5SJed Brown 
1631d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1632d763cef2SBarry Smith 
16334a2ae208SSatish Balay #undef __FUNCT__
1634a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1635d763cef2SBarry Smith /*@C
1636a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1637d763cef2SBarry Smith    timestep to display the iteration's  progress.
1638d763cef2SBarry Smith 
16393f9fe445SBarry Smith    Logically Collective on TS
1640d763cef2SBarry Smith 
1641d763cef2SBarry Smith    Input Parameters:
1642d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1643d763cef2SBarry Smith .  func - monitoring routine
1644329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1645b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1646b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1647b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1648d763cef2SBarry Smith 
1649d763cef2SBarry Smith    Calling sequence of func:
1650a7cc72afSBarry Smith $    int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1651d763cef2SBarry Smith 
1652d763cef2SBarry Smith +    ts - the TS context
1653d763cef2SBarry Smith .    steps - iteration number
16541f06c33eSBarry Smith .    time - current time
1655d763cef2SBarry Smith .    x - current iterate
1656d763cef2SBarry Smith -    mctx - [optional] monitoring context
1657d763cef2SBarry Smith 
1658d763cef2SBarry Smith    Notes:
1659d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1660d763cef2SBarry Smith    already has been loaded.
1661d763cef2SBarry Smith 
1662025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1663025f1a04SBarry Smith 
1664d763cef2SBarry Smith    Level: intermediate
1665d763cef2SBarry Smith 
1666d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1667d763cef2SBarry Smith 
1668a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1669d763cef2SBarry Smith @*/
1670c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1671d763cef2SBarry Smith {
1672d763cef2SBarry Smith   PetscFunctionBegin;
16730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
167417186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1675d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1676329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1677d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1678d763cef2SBarry Smith   PetscFunctionReturn(0);
1679d763cef2SBarry Smith }
1680d763cef2SBarry Smith 
16814a2ae208SSatish Balay #undef __FUNCT__
1682a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1683d763cef2SBarry Smith /*@C
1684a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1685d763cef2SBarry Smith 
16863f9fe445SBarry Smith    Logically Collective on TS
1687d763cef2SBarry Smith 
1688d763cef2SBarry Smith    Input Parameters:
1689d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1690d763cef2SBarry Smith 
1691d763cef2SBarry Smith    Notes:
1692d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1693d763cef2SBarry Smith 
1694d763cef2SBarry Smith    Level: intermediate
1695d763cef2SBarry Smith 
1696d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1697d763cef2SBarry Smith 
1698a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1699d763cef2SBarry Smith @*/
17007087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1701d763cef2SBarry Smith {
1702d952e501SBarry Smith   PetscErrorCode ierr;
1703d952e501SBarry Smith   PetscInt       i;
1704d952e501SBarry Smith 
1705d763cef2SBarry Smith   PetscFunctionBegin;
17060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1707d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
1708d952e501SBarry Smith     if (ts->mdestroy[i]) {
17093c4aec1bSBarry Smith       ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
1710d952e501SBarry Smith     }
1711d952e501SBarry Smith   }
1712d763cef2SBarry Smith   ts->numbermonitors = 0;
1713d763cef2SBarry Smith   PetscFunctionReturn(0);
1714d763cef2SBarry Smith }
1715d763cef2SBarry Smith 
17164a2ae208SSatish Balay #undef __FUNCT__
1717a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
1718d8e5e3e6SSatish Balay /*@
1719a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
17205516499fSSatish Balay 
17215516499fSSatish Balay    Level: intermediate
172241251cbbSSatish Balay 
17235516499fSSatish Balay .keywords: TS, set, monitor
17245516499fSSatish Balay 
172541251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
172641251cbbSSatish Balay @*/
1727649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
1728d763cef2SBarry Smith {
1729dfbe8321SBarry Smith   PetscErrorCode ierr;
1730649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
1731d132466eSBarry Smith 
1732d763cef2SBarry Smith   PetscFunctionBegin;
1733649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1734649052a6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr);
1735649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1736d763cef2SBarry Smith   PetscFunctionReturn(0);
1737d763cef2SBarry Smith }
1738d763cef2SBarry Smith 
17394a2ae208SSatish Balay #undef __FUNCT__
17404a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1741d763cef2SBarry Smith /*@
1742d763cef2SBarry Smith    TSStep - Steps the requested number of timesteps.
1743d763cef2SBarry Smith 
1744d763cef2SBarry Smith    Collective on TS
1745d763cef2SBarry Smith 
1746d763cef2SBarry Smith    Input Parameter:
1747d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1748d763cef2SBarry Smith 
1749d763cef2SBarry Smith    Output Parameters:
1750d763cef2SBarry Smith +  steps - number of iterations until termination
1751142b95e3SSatish Balay -  ptime - time until termination
1752d763cef2SBarry Smith 
1753d763cef2SBarry Smith    Level: beginner
1754d763cef2SBarry Smith 
1755d763cef2SBarry Smith .keywords: TS, timestep, solve
1756d763cef2SBarry Smith 
1757d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy()
1758d763cef2SBarry Smith @*/
17597087cfbeSBarry Smith PetscErrorCode  TSStep(TS ts,PetscInt *steps,PetscReal *ptime)
1760d763cef2SBarry Smith {
1761dfbe8321SBarry Smith   PetscErrorCode ierr;
1762d763cef2SBarry Smith 
1763d763cef2SBarry Smith   PetscFunctionBegin;
17640700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1765277b19d0SLisandro Dalcin 
1766d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
1767d405a339SMatthew Knepley 
1768d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1769000e7ae3SMatthew Knepley   ierr = (*ts->ops->step)(ts, steps, ptime);CHKERRQ(ierr);
1770d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1771d405a339SMatthew Knepley 
17724bb05414SBarry Smith   if (!PetscPreLoadingOn) {
17737adad957SLisandro Dalcin     ierr = TSViewFromOptions(ts,((PetscObject)ts)->name);CHKERRQ(ierr);
1774d405a339SMatthew Knepley   }
1775d763cef2SBarry Smith   PetscFunctionReturn(0);
1776d763cef2SBarry Smith }
1777d763cef2SBarry Smith 
17784a2ae208SSatish Balay #undef __FUNCT__
17796a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
17806a4d4014SLisandro Dalcin /*@
17816a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
17826a4d4014SLisandro Dalcin 
17836a4d4014SLisandro Dalcin    Collective on TS
17846a4d4014SLisandro Dalcin 
17856a4d4014SLisandro Dalcin    Input Parameter:
17866a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
17876a4d4014SLisandro Dalcin -  x - the solution vector, or PETSC_NULL if it was set with TSSetSolution()
17886a4d4014SLisandro Dalcin 
17896a4d4014SLisandro Dalcin    Level: beginner
17906a4d4014SLisandro Dalcin 
17916a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
17926a4d4014SLisandro Dalcin 
17936a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
17946a4d4014SLisandro Dalcin @*/
17957087cfbeSBarry Smith PetscErrorCode  TSSolve(TS ts, Vec x)
17966a4d4014SLisandro Dalcin {
17976a4d4014SLisandro Dalcin   PetscInt       steps;
17986a4d4014SLisandro Dalcin   PetscReal      ptime;
17996a4d4014SLisandro Dalcin   PetscErrorCode ierr;
1800f22f69f0SBarry Smith 
18016a4d4014SLisandro Dalcin   PetscFunctionBegin;
18020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18036a4d4014SLisandro Dalcin   /* set solution vector if provided */
18046a4d4014SLisandro Dalcin   if (x) { ierr = TSSetSolution(ts, x); CHKERRQ(ierr); }
18056a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
18066a4d4014SLisandro Dalcin   ts->steps = 0; ts->linear_its = 0; ts->nonlinear_its = 0;
18076a4d4014SLisandro Dalcin   /* steps the requested number of timesteps. */
18086a4d4014SLisandro Dalcin   ierr = TSStep(ts, &steps, &ptime);CHKERRQ(ierr);
18096a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
18106a4d4014SLisandro Dalcin }
18116a4d4014SLisandro Dalcin 
18126a4d4014SLisandro Dalcin #undef __FUNCT__
18134a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
1814d763cef2SBarry Smith /*
1815d763cef2SBarry Smith      Runs the user provided monitor routines, if they exists.
1816d763cef2SBarry Smith */
1817a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
1818d763cef2SBarry Smith {
18196849ba73SBarry Smith   PetscErrorCode ierr;
1820a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
1821d763cef2SBarry Smith 
1822d763cef2SBarry Smith   PetscFunctionBegin;
1823d763cef2SBarry Smith   for (i=0; i<n; i++) {
1824142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
1825d763cef2SBarry Smith   }
1826d763cef2SBarry Smith   PetscFunctionReturn(0);
1827d763cef2SBarry Smith }
1828d763cef2SBarry Smith 
1829d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
1830d763cef2SBarry Smith 
18314a2ae208SSatish Balay #undef __FUNCT__
1832a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate"
1833d763cef2SBarry Smith /*@C
1834a6570f20SBarry Smith    TSMonitorLGCreate - Creates a line graph context for use with
1835d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
1836d763cef2SBarry Smith 
1837d763cef2SBarry Smith    Collective on TS
1838d763cef2SBarry Smith 
1839d763cef2SBarry Smith    Input Parameters:
1840d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
1841d763cef2SBarry Smith .  label - the title to put in the title bar
18427c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
1843d763cef2SBarry Smith -  m, n - the screen width and height in pixels
1844d763cef2SBarry Smith 
1845d763cef2SBarry Smith    Output Parameter:
1846d763cef2SBarry Smith .  draw - the drawing context
1847d763cef2SBarry Smith 
1848d763cef2SBarry Smith    Options Database Key:
1849a6570f20SBarry Smith .  -ts_monitor_draw - automatically sets line graph monitor
1850d763cef2SBarry Smith 
1851d763cef2SBarry Smith    Notes:
1852a6570f20SBarry Smith    Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy().
1853d763cef2SBarry Smith 
1854d763cef2SBarry Smith    Level: intermediate
1855d763cef2SBarry Smith 
18567c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
1857d763cef2SBarry Smith 
1858a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet()
18597c922b88SBarry Smith 
1860d763cef2SBarry Smith @*/
18617087cfbeSBarry Smith PetscErrorCode  TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1862d763cef2SBarry Smith {
1863b0a32e0cSBarry Smith   PetscDraw      win;
1864dfbe8321SBarry Smith   PetscErrorCode ierr;
1865d763cef2SBarry Smith 
1866d763cef2SBarry Smith   PetscFunctionBegin;
1867b0a32e0cSBarry Smith   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
1868b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
1869b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
1870b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
1871d763cef2SBarry Smith 
187252e6d16bSBarry Smith   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
1873d763cef2SBarry Smith   PetscFunctionReturn(0);
1874d763cef2SBarry Smith }
1875d763cef2SBarry Smith 
18764a2ae208SSatish Balay #undef __FUNCT__
1877a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG"
1878a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
1879d763cef2SBarry Smith {
1880b0a32e0cSBarry Smith   PetscDrawLG    lg = (PetscDrawLG) monctx;
188187828ca2SBarry Smith   PetscReal      x,y = ptime;
1882dfbe8321SBarry Smith   PetscErrorCode ierr;
1883d763cef2SBarry Smith 
1884d763cef2SBarry Smith   PetscFunctionBegin;
18857c922b88SBarry Smith   if (!monctx) {
18867c922b88SBarry Smith     MPI_Comm    comm;
1887b0a32e0cSBarry Smith     PetscViewer viewer;
18887c922b88SBarry Smith 
18897c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
1890b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
1891b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
18927c922b88SBarry Smith   }
18937c922b88SBarry Smith 
1894b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
189587828ca2SBarry Smith   x = (PetscReal)n;
1896b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1897d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
1898b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1899d763cef2SBarry Smith   }
1900d763cef2SBarry Smith   PetscFunctionReturn(0);
1901d763cef2SBarry Smith }
1902d763cef2SBarry Smith 
19034a2ae208SSatish Balay #undef __FUNCT__
1904a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy"
1905d763cef2SBarry Smith /*@C
1906a6570f20SBarry Smith    TSMonitorLGDestroy - Destroys a line graph context that was created
1907a6570f20SBarry Smith    with TSMonitorLGCreate().
1908d763cef2SBarry Smith 
1909b0a32e0cSBarry Smith    Collective on PetscDrawLG
1910d763cef2SBarry Smith 
1911d763cef2SBarry Smith    Input Parameter:
1912d763cef2SBarry Smith .  draw - the drawing context
1913d763cef2SBarry Smith 
1914d763cef2SBarry Smith    Level: intermediate
1915d763cef2SBarry Smith 
1916d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
1917d763cef2SBarry Smith 
1918a6570f20SBarry Smith .seealso: TSMonitorLGCreate(),  TSMonitorSet(), TSMonitorLG();
1919d763cef2SBarry Smith @*/
19206bf464f9SBarry Smith PetscErrorCode  TSMonitorLGDestroy(PetscDrawLG *drawlg)
1921d763cef2SBarry Smith {
1922b0a32e0cSBarry Smith   PetscDraw      draw;
1923dfbe8321SBarry Smith   PetscErrorCode ierr;
1924d763cef2SBarry Smith 
1925d763cef2SBarry Smith   PetscFunctionBegin;
19266bf464f9SBarry Smith   ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr);
19276bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
1928b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
1929d763cef2SBarry Smith   PetscFunctionReturn(0);
1930d763cef2SBarry Smith }
1931d763cef2SBarry Smith 
19324a2ae208SSatish Balay #undef __FUNCT__
19334a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
1934d763cef2SBarry Smith /*@
1935d763cef2SBarry Smith    TSGetTime - Gets the current time.
1936d763cef2SBarry Smith 
1937d763cef2SBarry Smith    Not Collective
1938d763cef2SBarry Smith 
1939d763cef2SBarry Smith    Input Parameter:
1940d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1941d763cef2SBarry Smith 
1942d763cef2SBarry Smith    Output Parameter:
1943d763cef2SBarry Smith .  t  - the current time
1944d763cef2SBarry Smith 
1945d763cef2SBarry Smith    Level: beginner
1946d763cef2SBarry Smith 
1947d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1948d763cef2SBarry Smith 
1949d763cef2SBarry Smith .keywords: TS, get, time
1950d763cef2SBarry Smith @*/
19517087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
1952d763cef2SBarry Smith {
1953d763cef2SBarry Smith   PetscFunctionBegin;
19540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19554482741eSBarry Smith   PetscValidDoublePointer(t,2);
1956d763cef2SBarry Smith   *t = ts->ptime;
1957d763cef2SBarry Smith   PetscFunctionReturn(0);
1958d763cef2SBarry Smith }
1959d763cef2SBarry Smith 
19604a2ae208SSatish Balay #undef __FUNCT__
19616a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
19626a4d4014SLisandro Dalcin /*@
19636a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
19646a4d4014SLisandro Dalcin 
19653f9fe445SBarry Smith    Logically Collective on TS
19666a4d4014SLisandro Dalcin 
19676a4d4014SLisandro Dalcin    Input Parameters:
19686a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
19696a4d4014SLisandro Dalcin -  time - the time
19706a4d4014SLisandro Dalcin 
19716a4d4014SLisandro Dalcin    Level: intermediate
19726a4d4014SLisandro Dalcin 
19736a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
19746a4d4014SLisandro Dalcin 
19756a4d4014SLisandro Dalcin .keywords: TS, set, time
19766a4d4014SLisandro Dalcin @*/
19777087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
19786a4d4014SLisandro Dalcin {
19796a4d4014SLisandro Dalcin   PetscFunctionBegin;
19800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1981c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
19826a4d4014SLisandro Dalcin   ts->ptime = t;
19836a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
19846a4d4014SLisandro Dalcin }
19856a4d4014SLisandro Dalcin 
19866a4d4014SLisandro Dalcin #undef __FUNCT__
19874a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
1988d763cef2SBarry Smith /*@C
1989d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
1990d763cef2SBarry Smith    TS options in the database.
1991d763cef2SBarry Smith 
19923f9fe445SBarry Smith    Logically Collective on TS
1993d763cef2SBarry Smith 
1994d763cef2SBarry Smith    Input Parameter:
1995d763cef2SBarry Smith +  ts     - The TS context
1996d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
1997d763cef2SBarry Smith 
1998d763cef2SBarry Smith    Notes:
1999d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2000d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2001d763cef2SBarry Smith    hyphen.
2002d763cef2SBarry Smith 
2003d763cef2SBarry Smith    Level: advanced
2004d763cef2SBarry Smith 
2005d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2006d763cef2SBarry Smith 
2007d763cef2SBarry Smith .seealso: TSSetFromOptions()
2008d763cef2SBarry Smith 
2009d763cef2SBarry Smith @*/
20107087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2011d763cef2SBarry Smith {
2012dfbe8321SBarry Smith   PetscErrorCode ierr;
2013089b2837SJed Brown   SNES           snes;
2014d763cef2SBarry Smith 
2015d763cef2SBarry Smith   PetscFunctionBegin;
20160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2017d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2018089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2019089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2020d763cef2SBarry Smith   PetscFunctionReturn(0);
2021d763cef2SBarry Smith }
2022d763cef2SBarry Smith 
2023d763cef2SBarry Smith 
20244a2ae208SSatish Balay #undef __FUNCT__
20254a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2026d763cef2SBarry Smith /*@C
2027d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2028d763cef2SBarry Smith    TS options in the database.
2029d763cef2SBarry Smith 
20303f9fe445SBarry Smith    Logically Collective on TS
2031d763cef2SBarry Smith 
2032d763cef2SBarry Smith    Input Parameter:
2033d763cef2SBarry Smith +  ts     - The TS context
2034d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2035d763cef2SBarry Smith 
2036d763cef2SBarry Smith    Notes:
2037d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2038d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2039d763cef2SBarry Smith    hyphen.
2040d763cef2SBarry Smith 
2041d763cef2SBarry Smith    Level: advanced
2042d763cef2SBarry Smith 
2043d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2044d763cef2SBarry Smith 
2045d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2046d763cef2SBarry Smith 
2047d763cef2SBarry Smith @*/
20487087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2049d763cef2SBarry Smith {
2050dfbe8321SBarry Smith   PetscErrorCode ierr;
2051089b2837SJed Brown   SNES           snes;
2052d763cef2SBarry Smith 
2053d763cef2SBarry Smith   PetscFunctionBegin;
20540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2055d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2056089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2057089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2058d763cef2SBarry Smith   PetscFunctionReturn(0);
2059d763cef2SBarry Smith }
2060d763cef2SBarry Smith 
20614a2ae208SSatish Balay #undef __FUNCT__
20624a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2063d763cef2SBarry Smith /*@C
2064d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2065d763cef2SBarry Smith    TS options in the database.
2066d763cef2SBarry Smith 
2067d763cef2SBarry Smith    Not Collective
2068d763cef2SBarry Smith 
2069d763cef2SBarry Smith    Input Parameter:
2070d763cef2SBarry Smith .  ts - The TS context
2071d763cef2SBarry Smith 
2072d763cef2SBarry Smith    Output Parameter:
2073d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2074d763cef2SBarry Smith 
2075d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2076d763cef2SBarry Smith    sufficient length to hold the prefix.
2077d763cef2SBarry Smith 
2078d763cef2SBarry Smith    Level: intermediate
2079d763cef2SBarry Smith 
2080d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2081d763cef2SBarry Smith 
2082d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2083d763cef2SBarry Smith @*/
20847087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2085d763cef2SBarry Smith {
2086dfbe8321SBarry Smith   PetscErrorCode ierr;
2087d763cef2SBarry Smith 
2088d763cef2SBarry Smith   PetscFunctionBegin;
20890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20904482741eSBarry Smith   PetscValidPointer(prefix,2);
2091d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2092d763cef2SBarry Smith   PetscFunctionReturn(0);
2093d763cef2SBarry Smith }
2094d763cef2SBarry Smith 
20954a2ae208SSatish Balay #undef __FUNCT__
20964a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2097d763cef2SBarry Smith /*@C
2098d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2099d763cef2SBarry Smith 
2100d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2101d763cef2SBarry Smith 
2102d763cef2SBarry Smith    Input Parameter:
2103d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2104d763cef2SBarry Smith 
2105d763cef2SBarry Smith    Output Parameters:
2106d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2107d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2108089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2109d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2110d763cef2SBarry Smith 
2111d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2112d763cef2SBarry Smith 
2113d763cef2SBarry Smith    Level: intermediate
2114d763cef2SBarry Smith 
211526d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2116d763cef2SBarry Smith 
2117d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2118d763cef2SBarry Smith @*/
2119089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2120d763cef2SBarry Smith {
2121089b2837SJed Brown   PetscErrorCode ierr;
2122089b2837SJed Brown   SNES           snes;
2123089b2837SJed Brown 
2124d763cef2SBarry Smith   PetscFunctionBegin;
2125089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2126089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
2127089b2837SJed Brown   if (func) *func = ts->ops->rhsjacobian;
212826d46c62SHong Zhang   if (ctx) *ctx = ts->jacP;
2129d763cef2SBarry Smith   PetscFunctionReturn(0);
2130d763cef2SBarry Smith }
2131d763cef2SBarry Smith 
21321713a123SBarry Smith #undef __FUNCT__
21332eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
21342eca1d9cSJed Brown /*@C
21352eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
21362eca1d9cSJed Brown 
21372eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
21382eca1d9cSJed Brown 
21392eca1d9cSJed Brown    Input Parameter:
21402eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
21412eca1d9cSJed Brown 
21422eca1d9cSJed Brown    Output Parameters:
21432eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
21442eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
21452eca1d9cSJed Brown .  f   - The function to compute the matrices
21462eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
21472eca1d9cSJed Brown 
21482eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
21492eca1d9cSJed Brown 
21502eca1d9cSJed Brown    Level: advanced
21512eca1d9cSJed Brown 
21522eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
21532eca1d9cSJed Brown 
21542eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
21552eca1d9cSJed Brown @*/
21567087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
21572eca1d9cSJed Brown {
2158089b2837SJed Brown   PetscErrorCode ierr;
2159089b2837SJed Brown   SNES           snes;
2160089b2837SJed Brown 
21612eca1d9cSJed Brown   PetscFunctionBegin;
2162089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2163089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
21642eca1d9cSJed Brown   if (f) *f = ts->ops->ijacobian;
21652eca1d9cSJed Brown   if (ctx) *ctx = ts->jacP;
21662eca1d9cSJed Brown   PetscFunctionReturn(0);
21672eca1d9cSJed Brown }
21682eca1d9cSJed Brown 
21692eca1d9cSJed Brown #undef __FUNCT__
2170a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution"
21711713a123SBarry Smith /*@C
2172a6570f20SBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by calling
21731713a123SBarry Smith    VecView() for the solution at each timestep
21741713a123SBarry Smith 
21751713a123SBarry Smith    Collective on TS
21761713a123SBarry Smith 
21771713a123SBarry Smith    Input Parameters:
21781713a123SBarry Smith +  ts - the TS context
21791713a123SBarry Smith .  step - current time-step
2180142b95e3SSatish Balay .  ptime - current time
21811713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
21821713a123SBarry Smith 
21831713a123SBarry Smith    Level: intermediate
21841713a123SBarry Smith 
21851713a123SBarry Smith .keywords: TS,  vector, monitor, view
21861713a123SBarry Smith 
2187a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
21881713a123SBarry Smith @*/
21897087cfbeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
21901713a123SBarry Smith {
2191dfbe8321SBarry Smith   PetscErrorCode ierr;
21921713a123SBarry Smith   PetscViewer    viewer = (PetscViewer) dummy;
21931713a123SBarry Smith 
21941713a123SBarry Smith   PetscFunctionBegin;
2195a34d58ebSBarry Smith   if (!dummy) {
21967adad957SLisandro Dalcin     viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm);
21971713a123SBarry Smith   }
21981713a123SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
21991713a123SBarry Smith   PetscFunctionReturn(0);
22001713a123SBarry Smith }
22011713a123SBarry Smith 
22021713a123SBarry Smith 
22036c699258SBarry Smith #undef __FUNCT__
22046c699258SBarry Smith #define __FUNCT__ "TSSetDM"
22056c699258SBarry Smith /*@
22066c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
22076c699258SBarry Smith 
22083f9fe445SBarry Smith    Logically Collective on TS and DM
22096c699258SBarry Smith 
22106c699258SBarry Smith    Input Parameters:
22116c699258SBarry Smith +  ts - the preconditioner context
22126c699258SBarry Smith -  dm - the dm
22136c699258SBarry Smith 
22146c699258SBarry Smith    Level: intermediate
22156c699258SBarry Smith 
22166c699258SBarry Smith 
22176c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
22186c699258SBarry Smith @*/
22197087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
22206c699258SBarry Smith {
22216c699258SBarry Smith   PetscErrorCode ierr;
2222089b2837SJed Brown   SNES           snes;
22236c699258SBarry Smith 
22246c699258SBarry Smith   PetscFunctionBegin;
22250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
222670663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
22276bf464f9SBarry Smith   ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
22286c699258SBarry Smith   ts->dm = dm;
2229089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2230089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
22316c699258SBarry Smith   PetscFunctionReturn(0);
22326c699258SBarry Smith }
22336c699258SBarry Smith 
22346c699258SBarry Smith #undef __FUNCT__
22356c699258SBarry Smith #define __FUNCT__ "TSGetDM"
22366c699258SBarry Smith /*@
22376c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
22386c699258SBarry Smith 
22393f9fe445SBarry Smith    Not Collective
22406c699258SBarry Smith 
22416c699258SBarry Smith    Input Parameter:
22426c699258SBarry Smith . ts - the preconditioner context
22436c699258SBarry Smith 
22446c699258SBarry Smith    Output Parameter:
22456c699258SBarry Smith .  dm - the dm
22466c699258SBarry Smith 
22476c699258SBarry Smith    Level: intermediate
22486c699258SBarry Smith 
22496c699258SBarry Smith 
22506c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
22516c699258SBarry Smith @*/
22527087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
22536c699258SBarry Smith {
22546c699258SBarry Smith   PetscFunctionBegin;
22550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22566c699258SBarry Smith   *dm = ts->dm;
22576c699258SBarry Smith   PetscFunctionReturn(0);
22586c699258SBarry Smith }
22591713a123SBarry Smith 
22600f5c6efeSJed Brown #undef __FUNCT__
22610f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
22620f5c6efeSJed Brown /*@
22630f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
22640f5c6efeSJed Brown 
22653f9fe445SBarry Smith    Logically Collective on SNES
22660f5c6efeSJed Brown 
22670f5c6efeSJed Brown    Input Parameter:
2268d42a1c89SJed Brown + snes - nonlinear solver
22690f5c6efeSJed Brown . X - the current state at which to evaluate the residual
2270d42a1c89SJed Brown - ctx - user context, must be a TS
22710f5c6efeSJed Brown 
22720f5c6efeSJed Brown    Output Parameter:
22730f5c6efeSJed Brown . F - the nonlinear residual
22740f5c6efeSJed Brown 
22750f5c6efeSJed Brown    Notes:
22760f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
22770f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
22780f5c6efeSJed Brown 
22790f5c6efeSJed Brown    Level: advanced
22800f5c6efeSJed Brown 
22810f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
22820f5c6efeSJed Brown @*/
22837087cfbeSBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx)
22840f5c6efeSJed Brown {
22850f5c6efeSJed Brown   TS ts = (TS)ctx;
22860f5c6efeSJed Brown   PetscErrorCode ierr;
22870f5c6efeSJed Brown 
22880f5c6efeSJed Brown   PetscFunctionBegin;
22890f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22900f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
22910f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
22920f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
22930f5c6efeSJed Brown   ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr);
22940f5c6efeSJed Brown   PetscFunctionReturn(0);
22950f5c6efeSJed Brown }
22960f5c6efeSJed Brown 
22970f5c6efeSJed Brown #undef __FUNCT__
22980f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
22990f5c6efeSJed Brown /*@
23000f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
23010f5c6efeSJed Brown 
23020f5c6efeSJed Brown    Collective on SNES
23030f5c6efeSJed Brown 
23040f5c6efeSJed Brown    Input Parameter:
23050f5c6efeSJed Brown + snes - nonlinear solver
23060f5c6efeSJed Brown . X - the current state at which to evaluate the residual
23070f5c6efeSJed Brown - ctx - user context, must be a TS
23080f5c6efeSJed Brown 
23090f5c6efeSJed Brown    Output Parameter:
23100f5c6efeSJed Brown + A - the Jacobian
23110f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
23120f5c6efeSJed Brown - flag - indicates any structure change in the matrix
23130f5c6efeSJed Brown 
23140f5c6efeSJed Brown    Notes:
23150f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
23160f5c6efeSJed Brown 
23170f5c6efeSJed Brown    Level: developer
23180f5c6efeSJed Brown 
23190f5c6efeSJed Brown .seealso: SNESSetJacobian()
23200f5c6efeSJed Brown @*/
23217087cfbeSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx)
23220f5c6efeSJed Brown {
23230f5c6efeSJed Brown   TS ts = (TS)ctx;
23240f5c6efeSJed Brown   PetscErrorCode ierr;
23250f5c6efeSJed Brown 
23260f5c6efeSJed Brown   PetscFunctionBegin;
23270f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23280f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
23290f5c6efeSJed Brown   PetscValidPointer(A,3);
23300f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
23310f5c6efeSJed Brown   PetscValidPointer(B,4);
23320f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
23330f5c6efeSJed Brown   PetscValidPointer(flag,5);
23340f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
23350f5c6efeSJed Brown   ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr);
23360f5c6efeSJed Brown   PetscFunctionReturn(0);
23370f5c6efeSJed Brown }
2338325fc9f4SBarry Smith 
2339325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2340c6db04a5SJed Brown #include <mex.h>
2341325fc9f4SBarry Smith 
2342325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
2343325fc9f4SBarry Smith 
2344325fc9f4SBarry Smith #undef __FUNCT__
2345325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
2346325fc9f4SBarry Smith /*
2347325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
2348325fc9f4SBarry Smith                          TSSetFunctionMatlab().
2349325fc9f4SBarry Smith 
2350325fc9f4SBarry Smith    Collective on TS
2351325fc9f4SBarry Smith 
2352325fc9f4SBarry Smith    Input Parameters:
2353325fc9f4SBarry Smith +  snes - the TS context
2354325fc9f4SBarry Smith -  x - input vector
2355325fc9f4SBarry Smith 
2356325fc9f4SBarry Smith    Output Parameter:
2357325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
2358325fc9f4SBarry Smith 
2359325fc9f4SBarry Smith    Notes:
2360325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
2361325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
2362325fc9f4SBarry Smith    themselves.
2363325fc9f4SBarry Smith 
2364325fc9f4SBarry Smith    Level: developer
2365325fc9f4SBarry Smith 
2366325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2367325fc9f4SBarry Smith 
2368325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2369325fc9f4SBarry Smith */
23707087cfbeSBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx)
2371325fc9f4SBarry Smith {
2372325fc9f4SBarry Smith   PetscErrorCode   ierr;
2373325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2374325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
2375325fc9f4SBarry Smith   mxArray	   *plhs[1],*prhs[7];
2376325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
2377325fc9f4SBarry Smith 
2378325fc9f4SBarry Smith   PetscFunctionBegin;
2379325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2380325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2381325fc9f4SBarry Smith   PetscValidHeaderSpecific(xdot,VEC_CLASSID,4);
2382325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
2383325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,x,3);
2384325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
2385325fc9f4SBarry Smith 
2386325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2387325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2388880f3077SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr);
2389325fc9f4SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
2390325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2391325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
2392325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2393325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2394325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
2395325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
2396325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
2397325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
2398325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2399325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2400325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2401325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2402325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2403325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2404325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2405325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2406325fc9f4SBarry Smith   PetscFunctionReturn(0);
2407325fc9f4SBarry Smith }
2408325fc9f4SBarry Smith 
2409325fc9f4SBarry Smith 
2410325fc9f4SBarry Smith #undef __FUNCT__
2411325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
2412325fc9f4SBarry Smith /*
2413325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
2414325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
2415e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
2416325fc9f4SBarry Smith 
2417325fc9f4SBarry Smith    Logically Collective on TS
2418325fc9f4SBarry Smith 
2419325fc9f4SBarry Smith    Input Parameters:
2420325fc9f4SBarry Smith +  ts - the TS context
2421325fc9f4SBarry Smith -  func - function evaluation routine
2422325fc9f4SBarry Smith 
2423325fc9f4SBarry Smith    Calling sequence of func:
2424325fc9f4SBarry Smith $    func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx);
2425325fc9f4SBarry Smith 
2426325fc9f4SBarry Smith    Level: beginner
2427325fc9f4SBarry Smith 
2428325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2429325fc9f4SBarry Smith 
2430325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2431325fc9f4SBarry Smith */
24327087cfbeSBarry Smith PetscErrorCode  TSSetFunctionMatlab(TS snes,const char *func,mxArray *ctx)
2433325fc9f4SBarry Smith {
2434325fc9f4SBarry Smith   PetscErrorCode  ierr;
2435325fc9f4SBarry Smith   TSMatlabContext *sctx;
2436325fc9f4SBarry Smith 
2437325fc9f4SBarry Smith   PetscFunctionBegin;
2438325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2439325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2440325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2441325fc9f4SBarry Smith   /*
2442325fc9f4SBarry Smith      This should work, but it doesn't
2443325fc9f4SBarry Smith   sctx->ctx = ctx;
2444325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2445325fc9f4SBarry Smith   */
2446325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2447325fc9f4SBarry Smith   ierr = TSSetIFunction(snes,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
2448325fc9f4SBarry Smith   PetscFunctionReturn(0);
2449325fc9f4SBarry Smith }
2450325fc9f4SBarry Smith 
2451325fc9f4SBarry Smith #undef __FUNCT__
2452325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
2453325fc9f4SBarry Smith /*
2454325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
2455325fc9f4SBarry Smith                          TSSetJacobianMatlab().
2456325fc9f4SBarry Smith 
2457325fc9f4SBarry Smith    Collective on TS
2458325fc9f4SBarry Smith 
2459325fc9f4SBarry Smith    Input Parameters:
2460325fc9f4SBarry Smith +  snes - the TS context
2461325fc9f4SBarry Smith .  x - input vector
2462325fc9f4SBarry Smith .  A, B - the matrices
2463325fc9f4SBarry Smith -  ctx - user context
2464325fc9f4SBarry Smith 
2465325fc9f4SBarry Smith    Output Parameter:
2466325fc9f4SBarry Smith .  flag - structure of the matrix
2467325fc9f4SBarry Smith 
2468325fc9f4SBarry Smith    Level: developer
2469325fc9f4SBarry Smith 
2470325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2471325fc9f4SBarry Smith 
2472325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2473325fc9f4SBarry Smith @*/
24747087cfbeSBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
2475325fc9f4SBarry Smith {
2476325fc9f4SBarry Smith   PetscErrorCode  ierr;
2477325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2478325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
2479325fc9f4SBarry Smith   mxArray	  *plhs[2],*prhs[9];
2480325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
2481325fc9f4SBarry Smith 
2482325fc9f4SBarry Smith   PetscFunctionBegin;
2483325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2484325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2485325fc9f4SBarry Smith 
2486325fc9f4SBarry Smith   /* call Matlab function in ctx with arguments x and y */
2487325fc9f4SBarry Smith 
2488325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2489325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2490325fc9f4SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr);
2491325fc9f4SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
2492325fc9f4SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
2493325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2494325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
2495325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2496325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2497325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
2498325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
2499325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
2500325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
2501325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
2502325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
2503325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2504325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
2505325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2506325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2507325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2508325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2509325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2510325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2511325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
2512325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
2513325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2514325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
2515325fc9f4SBarry Smith   PetscFunctionReturn(0);
2516325fc9f4SBarry Smith }
2517325fc9f4SBarry Smith 
2518325fc9f4SBarry Smith 
2519325fc9f4SBarry Smith #undef __FUNCT__
2520325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
2521325fc9f4SBarry Smith /*
2522325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
2523e3c5b3baSBarry 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
2524325fc9f4SBarry Smith 
2525325fc9f4SBarry Smith    Logically Collective on TS
2526325fc9f4SBarry Smith 
2527325fc9f4SBarry Smith    Input Parameters:
2528325fc9f4SBarry Smith +  snes - the TS context
2529325fc9f4SBarry Smith .  A,B - Jacobian matrices
2530325fc9f4SBarry Smith .  func - function evaluation routine
2531325fc9f4SBarry Smith -  ctx - user context
2532325fc9f4SBarry Smith 
2533325fc9f4SBarry Smith    Calling sequence of func:
2534325fc9f4SBarry Smith $    flag = func (TS snes,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx);
2535325fc9f4SBarry Smith 
2536325fc9f4SBarry Smith 
2537325fc9f4SBarry Smith    Level: developer
2538325fc9f4SBarry Smith 
2539325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2540325fc9f4SBarry Smith 
2541325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2542325fc9f4SBarry Smith */
25437087cfbeSBarry Smith PetscErrorCode  TSSetJacobianMatlab(TS snes,Mat A,Mat B,const char *func,mxArray *ctx)
2544325fc9f4SBarry Smith {
2545325fc9f4SBarry Smith   PetscErrorCode    ierr;
2546325fc9f4SBarry Smith   TSMatlabContext *sctx;
2547325fc9f4SBarry Smith 
2548325fc9f4SBarry Smith   PetscFunctionBegin;
2549325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2550325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2551325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2552325fc9f4SBarry Smith   /*
2553325fc9f4SBarry Smith      This should work, but it doesn't
2554325fc9f4SBarry Smith   sctx->ctx = ctx;
2555325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2556325fc9f4SBarry Smith   */
2557325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2558325fc9f4SBarry Smith   ierr = TSSetIJacobian(snes,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
2559325fc9f4SBarry Smith   PetscFunctionReturn(0);
2560325fc9f4SBarry Smith }
2561325fc9f4SBarry Smith 
2562b5b1a830SBarry Smith #undef __FUNCT__
2563b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
2564b5b1a830SBarry Smith /*
2565b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
2566b5b1a830SBarry Smith 
2567b5b1a830SBarry Smith    Collective on TS
2568b5b1a830SBarry Smith 
2569b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2570b5b1a830SBarry Smith @*/
25717087cfbeSBarry Smith PetscErrorCode  TSMonitor_Matlab(TS snes,PetscInt it, PetscReal time,Vec x, void *ctx)
2572b5b1a830SBarry Smith {
2573b5b1a830SBarry Smith   PetscErrorCode  ierr;
2574b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2575a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
2576b5b1a830SBarry Smith   mxArray	  *plhs[1],*prhs[6];
2577b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
2578b5b1a830SBarry Smith 
2579b5b1a830SBarry Smith   PetscFunctionBegin;
2580b5b1a830SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2581b5b1a830SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,4);
2582b5b1a830SBarry Smith 
2583b5b1a830SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2584b5b1a830SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2585b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2586b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
2587b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
2588b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
2589b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
2590b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
2591b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
2592b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2593b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
2594b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
2595b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
2596b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
2597b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
2598b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
2599b5b1a830SBarry Smith   PetscFunctionReturn(0);
2600b5b1a830SBarry Smith }
2601b5b1a830SBarry Smith 
2602b5b1a830SBarry Smith 
2603b5b1a830SBarry Smith #undef __FUNCT__
2604b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
2605b5b1a830SBarry Smith /*
2606b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
2607b5b1a830SBarry Smith 
2608b5b1a830SBarry Smith    Level: developer
2609b5b1a830SBarry Smith 
2610b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
2611b5b1a830SBarry Smith 
2612b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2613b5b1a830SBarry Smith */
26147087cfbeSBarry Smith PetscErrorCode  TSMonitorSetMatlab(TS snes,const char *func,mxArray *ctx)
2615b5b1a830SBarry Smith {
2616b5b1a830SBarry Smith   PetscErrorCode    ierr;
2617b5b1a830SBarry Smith   TSMatlabContext *sctx;
2618b5b1a830SBarry Smith 
2619b5b1a830SBarry Smith   PetscFunctionBegin;
2620b5b1a830SBarry Smith   /* currently sctx is memory bleed */
2621b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2622b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2623b5b1a830SBarry Smith   /*
2624b5b1a830SBarry Smith      This should work, but it doesn't
2625b5b1a830SBarry Smith   sctx->ctx = ctx;
2626b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2627b5b1a830SBarry Smith   */
2628b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2629b5b1a830SBarry Smith   ierr = TSMonitorSet(snes,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
2630b5b1a830SBarry Smith   PetscFunctionReturn(0);
2631b5b1a830SBarry Smith }
2632b5b1a830SBarry Smith 
2633325fc9f4SBarry Smith #endif
2634