xref: /petsc/src/ts/interface/ts.c (revision 2dd45cf8f5b8626736788fc0e5c63f59921a1474)
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);
893daf2de8SJed Brown     if (opt) {ierr = TSSetInitialTimeStep(ts,ts->ptime,dt);CHKERRQ(ierr);}
90193ac0bcSJed Brown     ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","",ts->max_snes_failures,&ts->max_snes_failures,PETSC_NULL);CHKERRQ(ierr);
91193ac0bcSJed Brown     ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections","",ts->max_reject,&ts->max_reject,PETSC_NULL);CHKERRQ(ierr);
92193ac0bcSJed Brown     ierr = PetscOptionsBool("-ts_error_if_step_failed","Error if no step succeeds","",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr);
93bdad233fSMatthew Knepley 
94bdad233fSMatthew Knepley     /* Monitor options */
95a6570f20SBarry Smith     ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
96eabae89aSBarry Smith     if (flg) {
97649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr);
98649052a6SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
99bdad233fSMatthew Knepley     }
1005180491cSLisandro Dalcin     ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1015180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1025180491cSLisandro Dalcin 
10390d69ab7SBarry Smith     opt  = PETSC_FALSE;
104acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
105a7cc72afSBarry Smith     if (opt) {
106a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
107bdad233fSMatthew Knepley     }
10890d69ab7SBarry Smith     opt  = PETSC_FALSE;
109acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
110a7cc72afSBarry Smith     if (opt) {
111a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolution,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
112bdad233fSMatthew Knepley     }
113bdad233fSMatthew Knepley 
114bdad233fSMatthew Knepley     /* Handle TS type options */
115bdad233fSMatthew Knepley     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
116bdad233fSMatthew Knepley 
117bdad233fSMatthew Knepley     /* Handle specific TS options */
118abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
119bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
120bdad233fSMatthew Knepley     }
1215d973c19SBarry Smith 
1225d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
1235d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
124bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
125bdad233fSMatthew Knepley 
126089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
127bdad233fSMatthew Knepley   /* Handle subobject options */
128089b2837SJed Brown   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
129089b2837SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
130bdad233fSMatthew Knepley   PetscFunctionReturn(0);
131bdad233fSMatthew Knepley }
132bdad233fSMatthew Knepley 
133bdad233fSMatthew Knepley #undef __FUNCT__
134cdcf91faSSean Farley #undef __FUNCT__
1354a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
136a7a1495cSBarry Smith /*@
1378c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
138a7a1495cSBarry Smith       set with TSSetRHSJacobian().
139a7a1495cSBarry Smith 
140a7a1495cSBarry Smith    Collective on TS and Vec
141a7a1495cSBarry Smith 
142a7a1495cSBarry Smith    Input Parameters:
143316643e7SJed Brown +  ts - the TS context
144a7a1495cSBarry Smith .  t - current timestep
145a7a1495cSBarry Smith -  x - input vector
146a7a1495cSBarry Smith 
147a7a1495cSBarry Smith    Output Parameters:
148a7a1495cSBarry Smith +  A - Jacobian matrix
149a7a1495cSBarry Smith .  B - optional preconditioning matrix
150a7a1495cSBarry Smith -  flag - flag indicating matrix structure
151a7a1495cSBarry Smith 
152a7a1495cSBarry Smith    Notes:
153a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
154a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
155a7a1495cSBarry Smith 
15694b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
157a7a1495cSBarry Smith    flag parameter.
158a7a1495cSBarry Smith 
159a7a1495cSBarry Smith    Level: developer
160a7a1495cSBarry Smith 
161a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
162a7a1495cSBarry Smith 
16394b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
164a7a1495cSBarry Smith @*/
1657087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
166a7a1495cSBarry Smith {
167dfbe8321SBarry Smith   PetscErrorCode ierr;
1680e4ef248SJed Brown   PetscInt Xstate;
169a7a1495cSBarry Smith 
170a7a1495cSBarry Smith   PetscFunctionBegin;
1710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1720700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
173c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
1740e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
1750e4ef248SJed Brown   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) {
1760e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
1770e4ef248SJed Brown     PetscFunctionReturn(0);
178f8ede8e7SJed Brown   }
179d90be118SSean Farley 
1803b5f76d0SSean Farley   if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
181d90be118SSean Farley 
1823b5f76d0SSean Farley   if (ts->userops->rhsjacobian) {
183d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
184a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
185a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
1863b5f76d0SSean Farley     ierr = (*ts->userops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr);
187a7a1495cSBarry Smith     PetscStackPop;
188d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
189a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
1900700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
1910700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
192ef66eb69SBarry Smith   } else {
193214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
194214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
195214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
196ef66eb69SBarry Smith   }
1970e4ef248SJed Brown   ts->rhsjacobian.time = t;
1980e4ef248SJed Brown   ts->rhsjacobian.X = X;
1990e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
2000e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
201a7a1495cSBarry Smith   PetscFunctionReturn(0);
202a7a1495cSBarry Smith }
203a7a1495cSBarry Smith 
2044a2ae208SSatish Balay #undef __FUNCT__
2054a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
206316643e7SJed Brown /*@
207d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
208d763cef2SBarry Smith 
209316643e7SJed Brown    Collective on TS and Vec
210316643e7SJed Brown 
211316643e7SJed Brown    Input Parameters:
212316643e7SJed Brown +  ts - the TS context
213316643e7SJed Brown .  t - current time
214316643e7SJed Brown -  x - state vector
215316643e7SJed Brown 
216316643e7SJed Brown    Output Parameter:
217316643e7SJed Brown .  y - right hand side
218316643e7SJed Brown 
219316643e7SJed Brown    Note:
220316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
221316643e7SJed Brown    is used internally within the nonlinear solvers.
222316643e7SJed Brown 
223316643e7SJed Brown    Level: developer
224316643e7SJed Brown 
225316643e7SJed Brown .keywords: TS, compute
226316643e7SJed Brown 
227316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
228316643e7SJed Brown @*/
229dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
230d763cef2SBarry Smith {
231dfbe8321SBarry Smith   PetscErrorCode ierr;
232d763cef2SBarry Smith 
233d763cef2SBarry Smith   PetscFunctionBegin;
2340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2350700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2360700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
237d763cef2SBarry Smith 
2383b5f76d0SSean Farley   if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
239d763cef2SBarry Smith 
240d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
2413b5f76d0SSean Farley   if (ts->userops->rhsfunction) {
242d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
2433b5f76d0SSean Farley     ierr = (*ts->userops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr);
244d763cef2SBarry Smith     PetscStackPop;
245214bc6a2SJed Brown   } else {
246214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
247b2cd27e8SJed Brown   }
24844a41b28SSean Farley 
249d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
250d763cef2SBarry Smith   PetscFunctionReturn(0);
251d763cef2SBarry Smith }
252d763cef2SBarry Smith 
2534a2ae208SSatish Balay #undef __FUNCT__
254214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
255214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
256214bc6a2SJed Brown {
257*2dd45cf8SJed Brown   Vec            F;
258214bc6a2SJed Brown   PetscErrorCode ierr;
259214bc6a2SJed Brown 
260214bc6a2SJed Brown   PetscFunctionBegin;
261*2dd45cf8SJed Brown   ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
262214bc6a2SJed Brown   if (!ts->Frhs) {
263*2dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
264214bc6a2SJed Brown   }
265214bc6a2SJed Brown   *Frhs = ts->Frhs;
266214bc6a2SJed Brown   PetscFunctionReturn(0);
267214bc6a2SJed Brown }
268214bc6a2SJed Brown 
269214bc6a2SJed Brown #undef __FUNCT__
270214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
271214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
272214bc6a2SJed Brown {
273214bc6a2SJed Brown   Mat            A,B;
274*2dd45cf8SJed Brown   PetscErrorCode ierr;
275214bc6a2SJed Brown 
276214bc6a2SJed Brown   PetscFunctionBegin;
277214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
278214bc6a2SJed Brown   if (Arhs) {
279214bc6a2SJed Brown     if (!ts->Arhs) {
280214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
281214bc6a2SJed Brown     }
282214bc6a2SJed Brown     *Arhs = ts->Arhs;
283214bc6a2SJed Brown   }
284214bc6a2SJed Brown   if (Brhs) {
285214bc6a2SJed Brown     if (!ts->Brhs) {
286214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
287214bc6a2SJed Brown     }
288214bc6a2SJed Brown     *Brhs = ts->Brhs;
289214bc6a2SJed Brown   }
290214bc6a2SJed Brown   PetscFunctionReturn(0);
291214bc6a2SJed Brown }
292214bc6a2SJed Brown 
293214bc6a2SJed Brown #undef __FUNCT__
294316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
295316643e7SJed Brown /*@
296316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
297316643e7SJed Brown 
298316643e7SJed Brown    Collective on TS and Vec
299316643e7SJed Brown 
300316643e7SJed Brown    Input Parameters:
301316643e7SJed Brown +  ts - the TS context
302316643e7SJed Brown .  t - current time
303316643e7SJed Brown .  X - state vector
304214bc6a2SJed Brown .  Xdot - time derivative of state vector
305214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
306316643e7SJed Brown 
307316643e7SJed Brown    Output Parameter:
308316643e7SJed Brown .  Y - right hand side
309316643e7SJed Brown 
310316643e7SJed Brown    Note:
311316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
312316643e7SJed Brown    is used internally within the nonlinear solvers.
313316643e7SJed Brown 
314316643e7SJed Brown    If the user did did not write their equations in implicit form, this
315316643e7SJed Brown    function recasts them in implicit form.
316316643e7SJed Brown 
317316643e7SJed Brown    Level: developer
318316643e7SJed Brown 
319316643e7SJed Brown .keywords: TS, compute
320316643e7SJed Brown 
321316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
322316643e7SJed Brown @*/
323214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex)
324316643e7SJed Brown {
325316643e7SJed Brown   PetscErrorCode ierr;
326316643e7SJed Brown 
327316643e7SJed Brown   PetscFunctionBegin;
3280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3290700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3300700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
3310700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
332316643e7SJed Brown 
333d90be118SSean Farley   if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
334d90be118SSean Farley 
335316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
3363b5f76d0SSean Farley   if (ts->userops->ifunction) {
337316643e7SJed Brown     PetscStackPush("TS user implicit function");
3383b5f76d0SSean Farley     ierr = (*ts->userops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr);
339316643e7SJed Brown     PetscStackPop;
340214bc6a2SJed Brown   }
341214bc6a2SJed Brown   if (imex) {
3423b5f76d0SSean Farley     if (!ts->userops->ifunction) {
343*2dd45cf8SJed Brown       ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);
344*2dd45cf8SJed Brown     }
345*2dd45cf8SJed Brown   } else if (ts->userops->rhsfunction) {
346*2dd45cf8SJed Brown     if (ts->userops->ifunction) {
347214bc6a2SJed Brown       Vec Frhs;
348214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
349214bc6a2SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr);
350214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
351*2dd45cf8SJed Brown     } else {
352*2dd45cf8SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr);
353*2dd45cf8SJed 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
373214bc6a2SJed Brown .  shift - shift to apply, see note below
374214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
375316643e7SJed Brown 
376316643e7SJed Brown    Output Parameters:
377316643e7SJed Brown +  A - Jacobian matrix
378316643e7SJed Brown .  B - optional preconditioning matrix
379316643e7SJed Brown -  flag - flag indicating matrix structure
380316643e7SJed Brown 
381316643e7SJed Brown    Notes:
382316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
383316643e7SJed Brown 
384316643e7SJed Brown    dF/dX + shift*dF/dXdot
385316643e7SJed Brown 
386316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
387316643e7SJed Brown    is used internally within the nonlinear solvers.
388316643e7SJed Brown 
389316643e7SJed Brown    Level: developer
390316643e7SJed Brown 
391316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
392316643e7SJed Brown 
393316643e7SJed Brown .seealso:  TSSetIJacobian()
39463495f91SJed Brown @*/
395214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
396316643e7SJed Brown {
3970026cea9SSean Farley   PetscInt Xstate, Xdotstate;
398316643e7SJed Brown   PetscErrorCode ierr;
399316643e7SJed Brown 
400316643e7SJed Brown   PetscFunctionBegin;
4010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4020700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4030700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
404316643e7SJed Brown   PetscValidPointer(A,6);
4050700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
406316643e7SJed Brown   PetscValidPointer(B,7);
4070700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
408316643e7SJed Brown   PetscValidPointer(flg,8);
4090026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
4100026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr);
4110026cea9SSean Farley   if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == X && ts->ijacobian.Xstate == Xstate && ts->ijacobian.Xdot == Xdot && ts->ijacobian.Xdotstate == Xdotstate && ts->ijacobian.imex == imex))) {
4120026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
4130026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4140026cea9SSean Farley     PetscFunctionReturn(0);
4150026cea9SSean Farley   }
416316643e7SJed Brown 
4173b5f76d0SSean Farley   if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
418316643e7SJed Brown 
4194e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
420316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
4213b5f76d0SSean Farley   if (ts->userops->ijacobian) {
422*2dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
423316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
4243b5f76d0SSean Farley     ierr = (*ts->userops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr);
425316643e7SJed Brown     PetscStackPop;
426214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
427214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
428214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
4294a6899ffSJed Brown   }
430214bc6a2SJed Brown   if (imex) {
4313b5f76d0SSean Farley     if (!ts->userops->ijacobian) {  /* system was written as Xdot = F(t,X) */
432214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
433*2dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
434214bc6a2SJed Brown       if (*A != *B) {
435214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
436214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
437214bc6a2SJed Brown       }
438214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
439214bc6a2SJed Brown     }
440214bc6a2SJed Brown   } else {
4413b5f76d0SSean Farley     if (!ts->userops->ijacobian) {
442214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr);
443214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
444214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
445316643e7SJed Brown       if (*A != *B) {
446316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
447316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
448316643e7SJed Brown       }
4493b5f76d0SSean Farley     } else if (ts->userops->rhsjacobian) {
450214bc6a2SJed Brown       Mat Arhs,Brhs;
451214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
452214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
453214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
454214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
455214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
456214bc6a2SJed Brown       if (*A != *B) {
457214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
458214bc6a2SJed Brown       }
459214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
460214bc6a2SJed Brown     }
461316643e7SJed Brown   }
4620026cea9SSean Farley 
4630026cea9SSean Farley   ts->ijacobian.time = t;
4640026cea9SSean Farley   ts->ijacobian.X = X;
4650026cea9SSean Farley   ts->ijacobian.Xdot = Xdot;
4660026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr);
4670026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
4680026cea9SSean Farley   ts->ijacobian.shift = shift;
4690026cea9SSean Farley   ts->ijacobian.imex = imex;
4700026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
471316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
472316643e7SJed Brown   PetscFunctionReturn(0);
473316643e7SJed Brown }
474316643e7SJed Brown 
475316643e7SJed Brown #undef __FUNCT__
4764a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
477d763cef2SBarry Smith /*@C
478d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
479d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
480d763cef2SBarry Smith 
4813f9fe445SBarry Smith     Logically Collective on TS
482d763cef2SBarry Smith 
483d763cef2SBarry Smith     Input Parameters:
484d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
485ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
486d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
487d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
488d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
489d763cef2SBarry Smith 
490d763cef2SBarry Smith     Calling sequence of func:
49187828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
492d763cef2SBarry Smith 
493d763cef2SBarry Smith +   t - current timestep
494d763cef2SBarry Smith .   u - input vector
495d763cef2SBarry Smith .   F - function vector
496d763cef2SBarry Smith -   ctx - [optional] user-defined function context
497d763cef2SBarry Smith 
498d763cef2SBarry Smith     Important:
49976f2fa84SHong Zhang     The user MUST call either this routine or TSSetMatrices().
500d763cef2SBarry Smith 
501d763cef2SBarry Smith     Level: beginner
502d763cef2SBarry Smith 
503d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
504d763cef2SBarry Smith 
50576f2fa84SHong Zhang .seealso: TSSetMatrices()
506d763cef2SBarry Smith @*/
507089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
508d763cef2SBarry Smith {
509089b2837SJed Brown   PetscErrorCode ierr;
510089b2837SJed Brown   SNES           snes;
511d763cef2SBarry Smith 
512089b2837SJed Brown   PetscFunctionBegin;
5130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
514ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
5153b5f76d0SSean Farley   if (f)   ts->userops->rhsfunction = f;
5164e684422SJed Brown   if (ctx) ts->funP                 = ctx;
517089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
518089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
519d763cef2SBarry Smith   PetscFunctionReturn(0);
520d763cef2SBarry Smith }
521d763cef2SBarry Smith 
5224a2ae208SSatish Balay #undef __FUNCT__
5234a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
524d763cef2SBarry Smith /*@C
525d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
526d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
52776f2fa84SHong Zhang    Use TSSetMatrices() for linear problems.
528d763cef2SBarry Smith 
5293f9fe445SBarry Smith    Logically Collective on TS
530d763cef2SBarry Smith 
531d763cef2SBarry Smith    Input Parameters:
532d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
533d763cef2SBarry Smith .  A   - Jacobian matrix
534d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
535d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
536d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
537d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
538d763cef2SBarry Smith 
539d763cef2SBarry Smith    Calling sequence of func:
54087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
541d763cef2SBarry Smith 
542d763cef2SBarry Smith +  t - current timestep
543d763cef2SBarry Smith .  u - input vector
544d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
545d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
546d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
54794b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
548d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
549d763cef2SBarry Smith 
550d763cef2SBarry Smith    Notes:
55194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
552d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
553d763cef2SBarry Smith 
554d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
555d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
55656335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
557d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
558d763cef2SBarry Smith    throughout the global iterations.
559d763cef2SBarry Smith 
560d763cef2SBarry Smith    Level: beginner
561d763cef2SBarry Smith 
562d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
563d763cef2SBarry Smith 
564d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(),
56576f2fa84SHong Zhang           SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices()
566d763cef2SBarry Smith 
567d763cef2SBarry Smith @*/
568089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
569d763cef2SBarry Smith {
570277b19d0SLisandro Dalcin   PetscErrorCode ierr;
571089b2837SJed Brown   SNES           snes;
572277b19d0SLisandro Dalcin 
573d763cef2SBarry Smith   PetscFunctionBegin;
5740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
575277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
576277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
577277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
578277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
579d763cef2SBarry Smith 
5803b5f76d0SSean Farley   if (f)   ts->userops->rhsjacobian = f;
581277b19d0SLisandro Dalcin   if (ctx) ts->jacP                 = ctx;
582089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
583dabe61deSJed Brown   if (!ts->userops->ijacobian) {
584089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
5850e4ef248SJed Brown   }
5860e4ef248SJed Brown   if (A) {
5870e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
5880e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
5890e4ef248SJed Brown     ts->Arhs = A;
5900e4ef248SJed Brown   }
5910e4ef248SJed Brown   if (B) {
5920e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
5930e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
5940e4ef248SJed Brown     ts->Brhs = B;
5950e4ef248SJed Brown   }
596d763cef2SBarry Smith   PetscFunctionReturn(0);
597d763cef2SBarry Smith }
598d763cef2SBarry Smith 
599316643e7SJed Brown 
600316643e7SJed Brown #undef __FUNCT__
601316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
602316643e7SJed Brown /*@C
603316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
604316643e7SJed Brown 
6053f9fe445SBarry Smith    Logically Collective on TS
606316643e7SJed Brown 
607316643e7SJed Brown    Input Parameters:
608316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
609ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
610316643e7SJed Brown .  f   - the function evaluation routine
611316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
612316643e7SJed Brown 
613316643e7SJed Brown    Calling sequence of f:
614316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
615316643e7SJed Brown 
616316643e7SJed Brown +  t   - time at step/stage being solved
617316643e7SJed Brown .  u   - state vector
618316643e7SJed Brown .  u_t - time derivative of state vector
619316643e7SJed Brown .  F   - function vector
620316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
621316643e7SJed Brown 
622316643e7SJed Brown    Important:
623316643e7SJed Brown    The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices().  This routine must be used when not solving an ODE.
624316643e7SJed Brown 
625316643e7SJed Brown    Level: beginner
626316643e7SJed Brown 
627316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
628316643e7SJed Brown 
629316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian()
630316643e7SJed Brown @*/
631089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
632316643e7SJed Brown {
633089b2837SJed Brown   PetscErrorCode ierr;
634089b2837SJed Brown   SNES           snes;
635316643e7SJed Brown 
636316643e7SJed Brown   PetscFunctionBegin;
6370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
638ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
6393b5f76d0SSean Farley   if (f)   ts->userops->ifunction = f;
640089b2837SJed Brown   if (ctx) ts->funP           = ctx;
641089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
642089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
643089b2837SJed Brown   PetscFunctionReturn(0);
644089b2837SJed Brown }
645089b2837SJed Brown 
646089b2837SJed Brown #undef __FUNCT__
647089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
648089b2837SJed Brown /*@C
649089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
650089b2837SJed Brown 
651089b2837SJed Brown    Not Collective
652089b2837SJed Brown 
653089b2837SJed Brown    Input Parameter:
654089b2837SJed Brown .  ts - the TS context
655089b2837SJed Brown 
656089b2837SJed Brown    Output Parameter:
657089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
658089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
659089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
660089b2837SJed Brown 
661089b2837SJed Brown    Level: advanced
662089b2837SJed Brown 
663089b2837SJed Brown .keywords: TS, nonlinear, get, function
664089b2837SJed Brown 
665089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
666089b2837SJed Brown @*/
667089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
668089b2837SJed Brown {
669089b2837SJed Brown   PetscErrorCode ierr;
670089b2837SJed Brown   SNES snes;
671089b2837SJed Brown 
672089b2837SJed Brown   PetscFunctionBegin;
673089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
674089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
675089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
6763b5f76d0SSean Farley   if (func) *func = ts->userops->ifunction;
677089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
678089b2837SJed Brown   PetscFunctionReturn(0);
679089b2837SJed Brown }
680089b2837SJed Brown 
681089b2837SJed Brown #undef __FUNCT__
682089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
683089b2837SJed Brown /*@C
684089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
685089b2837SJed Brown 
686089b2837SJed Brown    Not Collective
687089b2837SJed Brown 
688089b2837SJed Brown    Input Parameter:
689089b2837SJed Brown .  ts - the TS context
690089b2837SJed Brown 
691089b2837SJed Brown    Output Parameter:
692089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
693089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
694089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
695089b2837SJed Brown 
696089b2837SJed Brown    Level: advanced
697089b2837SJed Brown 
698089b2837SJed Brown .keywords: TS, nonlinear, get, function
699089b2837SJed Brown 
700089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
701089b2837SJed Brown @*/
702089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
703089b2837SJed Brown {
704089b2837SJed Brown   PetscErrorCode ierr;
705089b2837SJed Brown   SNES snes;
706089b2837SJed Brown 
707089b2837SJed Brown   PetscFunctionBegin;
708089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
709089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
710089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7113b5f76d0SSean Farley   if (func) *func = ts->userops->rhsfunction;
712089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
713316643e7SJed Brown   PetscFunctionReturn(0);
714316643e7SJed Brown }
715316643e7SJed Brown 
716316643e7SJed Brown #undef __FUNCT__
717316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
718316643e7SJed Brown /*@C
719a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
720a4f0a591SBarry Smith         you provided with TSSetIFunction().
721316643e7SJed Brown 
7223f9fe445SBarry Smith    Logically Collective on TS
723316643e7SJed Brown 
724316643e7SJed Brown    Input Parameters:
725316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
726316643e7SJed Brown .  A   - Jacobian matrix
727316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
728316643e7SJed Brown .  f   - the Jacobian evaluation routine
729316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
730316643e7SJed Brown 
731316643e7SJed Brown    Calling sequence of f:
7321b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
733316643e7SJed Brown 
734316643e7SJed Brown +  t    - time at step/stage being solved
7351b4a444bSJed Brown .  U    - state vector
7361b4a444bSJed Brown .  U_t  - time derivative of state vector
737316643e7SJed Brown .  a    - shift
7381b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
739316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
740316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
741316643e7SJed Brown           structure (same as flag in KSPSetOperators())
742316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
743316643e7SJed Brown 
744316643e7SJed Brown    Notes:
745316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
746316643e7SJed Brown 
747a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
748a4f0a591SBarry 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.
749a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
750a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
751a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
752a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
753a4f0a591SBarry Smith 
754316643e7SJed Brown    Level: beginner
755316643e7SJed Brown 
756316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
757316643e7SJed Brown 
758316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian()
759316643e7SJed Brown 
760316643e7SJed Brown @*/
7617087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
762316643e7SJed Brown {
763316643e7SJed Brown   PetscErrorCode ierr;
764089b2837SJed Brown   SNES           snes;
765316643e7SJed Brown 
766316643e7SJed Brown   PetscFunctionBegin;
7670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7680700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
7690700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
770316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
771316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
7723b5f76d0SSean Farley   if (f)   ts->userops->ijacobian = f;
773316643e7SJed Brown   if (ctx) ts->jacP           = ctx;
774089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
775089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
776316643e7SJed Brown   PetscFunctionReturn(0);
777316643e7SJed Brown }
778316643e7SJed Brown 
7794a2ae208SSatish Balay #undef __FUNCT__
7804a2ae208SSatish Balay #define __FUNCT__ "TSView"
7817e2c5f70SBarry Smith /*@C
782d763cef2SBarry Smith     TSView - Prints the TS data structure.
783d763cef2SBarry Smith 
7844c49b128SBarry Smith     Collective on TS
785d763cef2SBarry Smith 
786d763cef2SBarry Smith     Input Parameters:
787d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
788d763cef2SBarry Smith -   viewer - visualization context
789d763cef2SBarry Smith 
790d763cef2SBarry Smith     Options Database Key:
791d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
792d763cef2SBarry Smith 
793d763cef2SBarry Smith     Notes:
794d763cef2SBarry Smith     The available visualization contexts include
795b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
796b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
797d763cef2SBarry Smith          output where only the first processor opens
798d763cef2SBarry Smith          the file.  All other processors send their
799d763cef2SBarry Smith          data to the first processor to print.
800d763cef2SBarry Smith 
801d763cef2SBarry Smith     The user can open an alternative visualization context with
802b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
803d763cef2SBarry Smith 
804d763cef2SBarry Smith     Level: beginner
805d763cef2SBarry Smith 
806d763cef2SBarry Smith .keywords: TS, timestep, view
807d763cef2SBarry Smith 
808b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
809d763cef2SBarry Smith @*/
8107087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
811d763cef2SBarry Smith {
812dfbe8321SBarry Smith   PetscErrorCode ierr;
813a313700dSBarry Smith   const TSType   type;
814089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
815d763cef2SBarry Smith 
816d763cef2SBarry Smith   PetscFunctionBegin;
8170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8183050cee2SBarry Smith   if (!viewer) {
8197adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
8203050cee2SBarry Smith   }
8210700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
822c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
823fd16b177SBarry Smith 
8242692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
8252692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
82632077d6dSBarry Smith   if (iascii) {
827317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
828000e7ae3SMatthew Knepley     if (ts->ops->view) {
829b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
830000e7ae3SMatthew Knepley       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
831b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
832d763cef2SBarry Smith     }
83377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
834a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
835d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
83677431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr);
837193ac0bcSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->max_snes_failures);CHKERRQ(ierr);
838d763cef2SBarry Smith     }
83977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr);
840193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
8410f5bd95cSBarry Smith   } else if (isstring) {
842a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
843b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
844d763cef2SBarry Smith   }
845b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
846089b2837SJed Brown   ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
847089b2837SJed Brown   if (!isundials && ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
848b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
849d763cef2SBarry Smith   PetscFunctionReturn(0);
850d763cef2SBarry Smith }
851d763cef2SBarry Smith 
852d763cef2SBarry Smith 
8534a2ae208SSatish Balay #undef __FUNCT__
8544a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
855b07ff414SBarry Smith /*@
856d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
857d763cef2SBarry Smith    the timesteppers.
858d763cef2SBarry Smith 
8593f9fe445SBarry Smith    Logically Collective on TS
860d763cef2SBarry Smith 
861d763cef2SBarry Smith    Input Parameters:
862d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
863d763cef2SBarry Smith -  usrP - optional user context
864d763cef2SBarry Smith 
865d763cef2SBarry Smith    Level: intermediate
866d763cef2SBarry Smith 
867d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
868d763cef2SBarry Smith 
869d763cef2SBarry Smith .seealso: TSGetApplicationContext()
870d763cef2SBarry Smith @*/
8717087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
872d763cef2SBarry Smith {
873d763cef2SBarry Smith   PetscFunctionBegin;
8740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
875d763cef2SBarry Smith   ts->user = usrP;
876d763cef2SBarry Smith   PetscFunctionReturn(0);
877d763cef2SBarry Smith }
878d763cef2SBarry Smith 
8794a2ae208SSatish Balay #undef __FUNCT__
8804a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
881b07ff414SBarry Smith /*@
882d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
883d763cef2SBarry Smith     timestepper.
884d763cef2SBarry Smith 
885d763cef2SBarry Smith     Not Collective
886d763cef2SBarry Smith 
887d763cef2SBarry Smith     Input Parameter:
888d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
889d763cef2SBarry Smith 
890d763cef2SBarry Smith     Output Parameter:
891d763cef2SBarry Smith .   usrP - user context
892d763cef2SBarry Smith 
893d763cef2SBarry Smith     Level: intermediate
894d763cef2SBarry Smith 
895d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
896d763cef2SBarry Smith 
897d763cef2SBarry Smith .seealso: TSSetApplicationContext()
898d763cef2SBarry Smith @*/
899e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
900d763cef2SBarry Smith {
901d763cef2SBarry Smith   PetscFunctionBegin;
9020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
903e71120c6SJed Brown   *(void**)usrP = ts->user;
904d763cef2SBarry Smith   PetscFunctionReturn(0);
905d763cef2SBarry Smith }
906d763cef2SBarry Smith 
9074a2ae208SSatish Balay #undef __FUNCT__
9084a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
909d763cef2SBarry Smith /*@
910d763cef2SBarry Smith    TSGetTimeStepNumber - Gets the current number of timesteps.
911d763cef2SBarry Smith 
912d763cef2SBarry Smith    Not Collective
913d763cef2SBarry Smith 
914d763cef2SBarry Smith    Input Parameter:
915d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
916d763cef2SBarry Smith 
917d763cef2SBarry Smith    Output Parameter:
918d763cef2SBarry Smith .  iter - number steps so far
919d763cef2SBarry Smith 
920d763cef2SBarry Smith    Level: intermediate
921d763cef2SBarry Smith 
922d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
923d763cef2SBarry Smith @*/
9247087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
925d763cef2SBarry Smith {
926d763cef2SBarry Smith   PetscFunctionBegin;
9270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9284482741eSBarry Smith   PetscValidIntPointer(iter,2);
929d763cef2SBarry Smith   *iter = ts->steps;
930d763cef2SBarry Smith   PetscFunctionReturn(0);
931d763cef2SBarry Smith }
932d763cef2SBarry Smith 
9334a2ae208SSatish Balay #undef __FUNCT__
9344a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
935d763cef2SBarry Smith /*@
936d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
937d763cef2SBarry Smith    as well as the initial time.
938d763cef2SBarry Smith 
9393f9fe445SBarry Smith    Logically Collective on TS
940d763cef2SBarry Smith 
941d763cef2SBarry Smith    Input Parameters:
942d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
943d763cef2SBarry Smith .  initial_time - the initial time
944d763cef2SBarry Smith -  time_step - the size of the timestep
945d763cef2SBarry Smith 
946d763cef2SBarry Smith    Level: intermediate
947d763cef2SBarry Smith 
948d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
949d763cef2SBarry Smith 
950d763cef2SBarry Smith .keywords: TS, set, initial, timestep
951d763cef2SBarry Smith @*/
9527087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
953d763cef2SBarry Smith {
954e144a568SJed Brown   PetscErrorCode ierr;
955e144a568SJed Brown 
956d763cef2SBarry Smith   PetscFunctionBegin;
9570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
958e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
959d763cef2SBarry Smith   ts->initial_time_step = time_step;
960d763cef2SBarry Smith   ts->ptime             = initial_time;
961d763cef2SBarry Smith   PetscFunctionReturn(0);
962d763cef2SBarry Smith }
963d763cef2SBarry Smith 
9644a2ae208SSatish Balay #undef __FUNCT__
9654a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
966d763cef2SBarry Smith /*@
967d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
968d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
969d763cef2SBarry Smith 
9703f9fe445SBarry Smith    Logically Collective on TS
971d763cef2SBarry Smith 
972d763cef2SBarry Smith    Input Parameters:
973d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
974d763cef2SBarry Smith -  time_step - the size of the timestep
975d763cef2SBarry Smith 
976d763cef2SBarry Smith    Level: intermediate
977d763cef2SBarry Smith 
978d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
979d763cef2SBarry Smith 
980d763cef2SBarry Smith .keywords: TS, set, timestep
981d763cef2SBarry Smith @*/
9827087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
983d763cef2SBarry Smith {
984d763cef2SBarry Smith   PetscFunctionBegin;
9850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
986c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
987d763cef2SBarry Smith   ts->time_step      = time_step;
988193ac0bcSJed Brown   ts->next_time_step = time_step;
989d763cef2SBarry Smith   PetscFunctionReturn(0);
990d763cef2SBarry Smith }
991d763cef2SBarry Smith 
9924a2ae208SSatish Balay #undef __FUNCT__
9934a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
994d763cef2SBarry Smith /*@
995d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
996d763cef2SBarry Smith 
997d763cef2SBarry Smith    Not Collective
998d763cef2SBarry Smith 
999d763cef2SBarry Smith    Input Parameter:
1000d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1001d763cef2SBarry Smith 
1002d763cef2SBarry Smith    Output Parameter:
1003d763cef2SBarry Smith .  dt - the current timestep size
1004d763cef2SBarry Smith 
1005d763cef2SBarry Smith    Level: intermediate
1006d763cef2SBarry Smith 
1007d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1008d763cef2SBarry Smith 
1009d763cef2SBarry Smith .keywords: TS, get, timestep
1010d763cef2SBarry Smith @*/
10117087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1012d763cef2SBarry Smith {
1013d763cef2SBarry Smith   PetscFunctionBegin;
10140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10154482741eSBarry Smith   PetscValidDoublePointer(dt,2);
1016d763cef2SBarry Smith   *dt = ts->time_step;
1017d763cef2SBarry Smith   PetscFunctionReturn(0);
1018d763cef2SBarry Smith }
1019d763cef2SBarry Smith 
10204a2ae208SSatish Balay #undef __FUNCT__
10214a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1022d8e5e3e6SSatish Balay /*@
1023d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1024d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1025d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1026d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1027d763cef2SBarry Smith 
1028d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1029d763cef2SBarry Smith 
1030d763cef2SBarry Smith    Input Parameter:
1031d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1032d763cef2SBarry Smith 
1033d763cef2SBarry Smith    Output Parameter:
1034d763cef2SBarry Smith .  v - the vector containing the solution
1035d763cef2SBarry Smith 
1036d763cef2SBarry Smith    Level: intermediate
1037d763cef2SBarry Smith 
1038d763cef2SBarry Smith .seealso: TSGetTimeStep()
1039d763cef2SBarry Smith 
1040d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1041d763cef2SBarry Smith @*/
10427087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1043d763cef2SBarry Smith {
1044d763cef2SBarry Smith   PetscFunctionBegin;
10450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10464482741eSBarry Smith   PetscValidPointer(v,2);
10478737fe31SLisandro Dalcin   *v = ts->vec_sol;
1048d763cef2SBarry Smith   PetscFunctionReturn(0);
1049d763cef2SBarry Smith }
1050d763cef2SBarry Smith 
1051bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
10524a2ae208SSatish Balay #undef __FUNCT__
1053bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1054d8e5e3e6SSatish Balay /*@
1055bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1056d763cef2SBarry Smith 
1057bdad233fSMatthew Knepley   Not collective
1058d763cef2SBarry Smith 
1059bdad233fSMatthew Knepley   Input Parameters:
1060bdad233fSMatthew Knepley + ts   - The TS
1061bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1062d763cef2SBarry Smith .vb
1063d763cef2SBarry Smith          U_t = A U
1064d763cef2SBarry Smith          U_t = A(t) U
1065d763cef2SBarry Smith          U_t = F(t,U)
1066d763cef2SBarry Smith .ve
1067d763cef2SBarry Smith 
1068d763cef2SBarry Smith    Level: beginner
1069d763cef2SBarry Smith 
1070bdad233fSMatthew Knepley .keywords: TS, problem type
1071bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1072d763cef2SBarry Smith @*/
10737087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1074a7cc72afSBarry Smith {
10759e2a6581SJed Brown   PetscErrorCode ierr;
10769e2a6581SJed Brown 
1077d763cef2SBarry Smith   PetscFunctionBegin;
10780700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1079bdad233fSMatthew Knepley   ts->problem_type = type;
10809e2a6581SJed Brown   if (type == TS_LINEAR) {
10819e2a6581SJed Brown     SNES snes;
10829e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10839e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
10849e2a6581SJed Brown   }
1085d763cef2SBarry Smith   PetscFunctionReturn(0);
1086d763cef2SBarry Smith }
1087d763cef2SBarry Smith 
1088bdad233fSMatthew Knepley #undef __FUNCT__
1089bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1090bdad233fSMatthew Knepley /*@C
1091bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1092bdad233fSMatthew Knepley 
1093bdad233fSMatthew Knepley   Not collective
1094bdad233fSMatthew Knepley 
1095bdad233fSMatthew Knepley   Input Parameter:
1096bdad233fSMatthew Knepley . ts   - The TS
1097bdad233fSMatthew Knepley 
1098bdad233fSMatthew Knepley   Output Parameter:
1099bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1100bdad233fSMatthew Knepley .vb
1101089b2837SJed Brown          M U_t = A U
1102089b2837SJed Brown          M(t) U_t = A(t) U
1103bdad233fSMatthew Knepley          U_t = F(t,U)
1104bdad233fSMatthew Knepley .ve
1105bdad233fSMatthew Knepley 
1106bdad233fSMatthew Knepley    Level: beginner
1107bdad233fSMatthew Knepley 
1108bdad233fSMatthew Knepley .keywords: TS, problem type
1109bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1110bdad233fSMatthew Knepley @*/
11117087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1112a7cc72afSBarry Smith {
1113bdad233fSMatthew Knepley   PetscFunctionBegin;
11140700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
11154482741eSBarry Smith   PetscValidIntPointer(type,2);
1116bdad233fSMatthew Knepley   *type = ts->problem_type;
1117bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1118bdad233fSMatthew Knepley }
1119d763cef2SBarry Smith 
11204a2ae208SSatish Balay #undef __FUNCT__
11214a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1122d763cef2SBarry Smith /*@
1123d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1124d763cef2SBarry Smith    of a timestepper.
1125d763cef2SBarry Smith 
1126d763cef2SBarry Smith    Collective on TS
1127d763cef2SBarry Smith 
1128d763cef2SBarry Smith    Input Parameter:
1129d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1130d763cef2SBarry Smith 
1131d763cef2SBarry Smith    Notes:
1132d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1133d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1134d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1135d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1136d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1137d763cef2SBarry Smith 
1138d763cef2SBarry Smith    Level: advanced
1139d763cef2SBarry Smith 
1140d763cef2SBarry Smith .keywords: TS, timestep, setup
1141d763cef2SBarry Smith 
1142d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1143d763cef2SBarry Smith @*/
11447087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1145d763cef2SBarry Smith {
1146dfbe8321SBarry Smith   PetscErrorCode ierr;
1147d763cef2SBarry Smith 
1148d763cef2SBarry Smith   PetscFunctionBegin;
11490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1150277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1151277b19d0SLisandro Dalcin 
11527adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
11539596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1154d763cef2SBarry Smith   }
1155277b19d0SLisandro Dalcin 
1156277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1157277b19d0SLisandro Dalcin 
1158277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1159000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1160277b19d0SLisandro Dalcin   }
1161277b19d0SLisandro Dalcin 
1162277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1163277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1164277b19d0SLisandro Dalcin }
1165277b19d0SLisandro Dalcin 
1166277b19d0SLisandro Dalcin #undef __FUNCT__
1167277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1168277b19d0SLisandro Dalcin /*@
1169277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1170277b19d0SLisandro Dalcin 
1171277b19d0SLisandro Dalcin    Collective on TS
1172277b19d0SLisandro Dalcin 
1173277b19d0SLisandro Dalcin    Input Parameter:
1174277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1175277b19d0SLisandro Dalcin 
1176277b19d0SLisandro Dalcin    Level: beginner
1177277b19d0SLisandro Dalcin 
1178277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1179277b19d0SLisandro Dalcin 
1180277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1181277b19d0SLisandro Dalcin @*/
1182277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1183277b19d0SLisandro Dalcin {
1184277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1185277b19d0SLisandro Dalcin 
1186277b19d0SLisandro Dalcin   PetscFunctionBegin;
1187277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1188277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1189277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1190277b19d0SLisandro Dalcin   }
1191277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
11924e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
11934e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1194214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
11956bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1196277b19d0SLisandro Dalcin   if (ts->work) {ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);}
1197277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1198d763cef2SBarry Smith   PetscFunctionReturn(0);
1199d763cef2SBarry Smith }
1200d763cef2SBarry Smith 
12014a2ae208SSatish Balay #undef __FUNCT__
12024a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1203d8e5e3e6SSatish Balay /*@
1204d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1205d763cef2SBarry Smith    with TSCreate().
1206d763cef2SBarry Smith 
1207d763cef2SBarry Smith    Collective on TS
1208d763cef2SBarry Smith 
1209d763cef2SBarry Smith    Input Parameter:
1210d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1211d763cef2SBarry Smith 
1212d763cef2SBarry Smith    Level: beginner
1213d763cef2SBarry Smith 
1214d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1215d763cef2SBarry Smith 
1216d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1217d763cef2SBarry Smith @*/
12186bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1219d763cef2SBarry Smith {
12206849ba73SBarry Smith   PetscErrorCode ierr;
1221d763cef2SBarry Smith 
1222d763cef2SBarry Smith   PetscFunctionBegin;
12236bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
12246bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
12256bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1226d763cef2SBarry Smith 
12276bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1228277b19d0SLisandro Dalcin 
1229be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
12306bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
12316bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
12326d4c513bSLisandro Dalcin 
12336bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
12346bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
12356bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
12366d4c513bSLisandro Dalcin 
12373b5f76d0SSean Farley   ierr = PetscFree((*ts)->userops);
12383b5f76d0SSean Farley 
1239a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1240d763cef2SBarry Smith   PetscFunctionReturn(0);
1241d763cef2SBarry Smith }
1242d763cef2SBarry Smith 
12434a2ae208SSatish Balay #undef __FUNCT__
12444a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1245d8e5e3e6SSatish Balay /*@
1246d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1247d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1248d763cef2SBarry Smith 
1249d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1250d763cef2SBarry Smith 
1251d763cef2SBarry Smith    Input Parameter:
1252d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1253d763cef2SBarry Smith 
1254d763cef2SBarry Smith    Output Parameter:
1255d763cef2SBarry Smith .  snes - the nonlinear solver context
1256d763cef2SBarry Smith 
1257d763cef2SBarry Smith    Notes:
1258d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1259d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
126094b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1261d763cef2SBarry Smith 
1262d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1263d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1264d763cef2SBarry Smith 
1265d763cef2SBarry Smith    Level: beginner
1266d763cef2SBarry Smith 
1267d763cef2SBarry Smith .keywords: timestep, get, SNES
1268d763cef2SBarry Smith @*/
12697087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1270d763cef2SBarry Smith {
1271d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1272d372ba47SLisandro Dalcin 
1273d763cef2SBarry Smith   PetscFunctionBegin;
12740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12754482741eSBarry Smith   PetscValidPointer(snes,2);
1276d372ba47SLisandro Dalcin   if (!ts->snes) {
1277d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
1278d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1279d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
12809e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
12819e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
12829e2a6581SJed Brown     }
1283d372ba47SLisandro Dalcin   }
1284d763cef2SBarry Smith   *snes = ts->snes;
1285d763cef2SBarry Smith   PetscFunctionReturn(0);
1286d763cef2SBarry Smith }
1287d763cef2SBarry Smith 
12884a2ae208SSatish Balay #undef __FUNCT__
128994b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1290d8e5e3e6SSatish Balay /*@
129194b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1292d763cef2SBarry Smith    a TS (timestepper) context.
1293d763cef2SBarry Smith 
129494b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1295d763cef2SBarry Smith 
1296d763cef2SBarry Smith    Input Parameter:
1297d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1298d763cef2SBarry Smith 
1299d763cef2SBarry Smith    Output Parameter:
130094b7f48cSBarry Smith .  ksp - the nonlinear solver context
1301d763cef2SBarry Smith 
1302d763cef2SBarry Smith    Notes:
130394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1304d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1305d763cef2SBarry Smith    KSP and PC contexts as well.
1306d763cef2SBarry Smith 
130794b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
130894b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1309d763cef2SBarry Smith 
1310d763cef2SBarry Smith    Level: beginner
1311d763cef2SBarry Smith 
131294b7f48cSBarry Smith .keywords: timestep, get, KSP
1313d763cef2SBarry Smith @*/
13147087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1315d763cef2SBarry Smith {
1316d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1317089b2837SJed Brown   SNES           snes;
1318d372ba47SLisandro Dalcin 
1319d763cef2SBarry Smith   PetscFunctionBegin;
13200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13214482741eSBarry Smith   PetscValidPointer(ksp,2);
132217186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1323e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1324089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1325089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1326d763cef2SBarry Smith   PetscFunctionReturn(0);
1327d763cef2SBarry Smith }
1328d763cef2SBarry Smith 
1329d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1330d763cef2SBarry Smith 
13314a2ae208SSatish Balay #undef __FUNCT__
1332adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1333adb62b0dSMatthew Knepley /*@
1334adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1335adb62b0dSMatthew Knepley    maximum time for iteration.
1336adb62b0dSMatthew Knepley 
13373f9fe445SBarry Smith    Not Collective
1338adb62b0dSMatthew Knepley 
1339adb62b0dSMatthew Knepley    Input Parameters:
1340adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1341adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1342adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1343adb62b0dSMatthew Knepley 
1344adb62b0dSMatthew Knepley    Level: intermediate
1345adb62b0dSMatthew Knepley 
1346adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1347adb62b0dSMatthew Knepley @*/
13487087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1349adb62b0dSMatthew Knepley {
1350adb62b0dSMatthew Knepley   PetscFunctionBegin;
13510700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1352abc0a331SBarry Smith   if (maxsteps) {
13534482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1354adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1355adb62b0dSMatthew Knepley   }
1356abc0a331SBarry Smith   if (maxtime ) {
13574482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1358adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1359adb62b0dSMatthew Knepley   }
1360adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1361adb62b0dSMatthew Knepley }
1362adb62b0dSMatthew Knepley 
1363adb62b0dSMatthew Knepley #undef __FUNCT__
13644a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1365d763cef2SBarry Smith /*@
1366d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1367d763cef2SBarry Smith    maximum time for iteration.
1368d763cef2SBarry Smith 
13693f9fe445SBarry Smith    Logically Collective on TS
1370d763cef2SBarry Smith 
1371d763cef2SBarry Smith    Input Parameters:
1372d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1373d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1374d763cef2SBarry Smith -  maxtime - final time to iterate to
1375d763cef2SBarry Smith 
1376d763cef2SBarry Smith    Options Database Keys:
1377d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
1378d763cef2SBarry Smith .  -ts_max_time <maxtime> - Sets maxtime
1379d763cef2SBarry Smith 
1380d763cef2SBarry Smith    Notes:
1381d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1382d763cef2SBarry Smith 
1383d763cef2SBarry Smith    Level: intermediate
1384d763cef2SBarry Smith 
1385d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1386d763cef2SBarry Smith @*/
13877087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1388d763cef2SBarry Smith {
1389d763cef2SBarry Smith   PetscFunctionBegin;
13900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1391c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1392c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
139339b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
139439b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1395d763cef2SBarry Smith   PetscFunctionReturn(0);
1396d763cef2SBarry Smith }
1397d763cef2SBarry Smith 
13984a2ae208SSatish Balay #undef __FUNCT__
13994a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1400d763cef2SBarry Smith /*@
1401d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1402d763cef2SBarry Smith    for use by the TS routines.
1403d763cef2SBarry Smith 
14043f9fe445SBarry Smith    Logically Collective on TS and Vec
1405d763cef2SBarry Smith 
1406d763cef2SBarry Smith    Input Parameters:
1407d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1408d763cef2SBarry Smith -  x - the solution vector
1409d763cef2SBarry Smith 
1410d763cef2SBarry Smith    Level: beginner
1411d763cef2SBarry Smith 
1412d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1413d763cef2SBarry Smith @*/
14147087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1415d763cef2SBarry Smith {
14168737fe31SLisandro Dalcin   PetscErrorCode ierr;
14178737fe31SLisandro Dalcin 
1418d763cef2SBarry Smith   PetscFunctionBegin;
14190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14200700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
14218737fe31SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
14226bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
14238737fe31SLisandro Dalcin   ts->vec_sol = x;
1424d763cef2SBarry Smith   PetscFunctionReturn(0);
1425d763cef2SBarry Smith }
1426d763cef2SBarry Smith 
1427e74ef692SMatthew Knepley #undef __FUNCT__
1428e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1429ac226902SBarry Smith /*@C
1430000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
14313f2090d5SJed Brown   called once at the beginning of each time step.
1432000e7ae3SMatthew Knepley 
14333f9fe445SBarry Smith   Logically Collective on TS
1434000e7ae3SMatthew Knepley 
1435000e7ae3SMatthew Knepley   Input Parameters:
1436000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1437000e7ae3SMatthew Knepley - func - The function
1438000e7ae3SMatthew Knepley 
1439000e7ae3SMatthew Knepley   Calling sequence of func:
1440000e7ae3SMatthew Knepley . func (TS ts);
1441000e7ae3SMatthew Knepley 
1442000e7ae3SMatthew Knepley   Level: intermediate
1443000e7ae3SMatthew Knepley 
1444000e7ae3SMatthew Knepley .keywords: TS, timestep
1445000e7ae3SMatthew Knepley @*/
14467087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1447000e7ae3SMatthew Knepley {
1448000e7ae3SMatthew Knepley   PetscFunctionBegin;
14490700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1450000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1451000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1452000e7ae3SMatthew Knepley }
1453000e7ae3SMatthew Knepley 
1454e74ef692SMatthew Knepley #undef __FUNCT__
14553f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
14563f2090d5SJed Brown /*@C
14573f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
14583f2090d5SJed Brown 
14593f2090d5SJed Brown   Collective on TS
14603f2090d5SJed Brown 
14613f2090d5SJed Brown   Input Parameters:
14623f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
14633f2090d5SJed Brown 
14643f2090d5SJed Brown   Notes:
14653f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
14663f2090d5SJed Brown   so most users would not generally call this routine themselves.
14673f2090d5SJed Brown 
14683f2090d5SJed Brown   Level: developer
14693f2090d5SJed Brown 
14703f2090d5SJed Brown .keywords: TS, timestep
14713f2090d5SJed Brown @*/
14727087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
14733f2090d5SJed Brown {
14743f2090d5SJed Brown   PetscErrorCode ierr;
14753f2090d5SJed Brown 
14763f2090d5SJed Brown   PetscFunctionBegin;
14770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
147872ac3e02SJed Brown   if (ts->ops->prestep) {
14793f2090d5SJed Brown     PetscStackPush("TS PreStep function");
14803f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
14813f2090d5SJed Brown     PetscStackPop;
1482312ce896SJed Brown   }
14833f2090d5SJed Brown   PetscFunctionReturn(0);
14843f2090d5SJed Brown }
14853f2090d5SJed Brown 
14863f2090d5SJed Brown #undef __FUNCT__
1487e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1488ac226902SBarry Smith /*@C
1489000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
14903f2090d5SJed Brown   called once at the end of each time step.
1491000e7ae3SMatthew Knepley 
14923f9fe445SBarry Smith   Logically Collective on TS
1493000e7ae3SMatthew Knepley 
1494000e7ae3SMatthew Knepley   Input Parameters:
1495000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1496000e7ae3SMatthew Knepley - func - The function
1497000e7ae3SMatthew Knepley 
1498000e7ae3SMatthew Knepley   Calling sequence of func:
1499000e7ae3SMatthew Knepley . func (TS ts);
1500000e7ae3SMatthew Knepley 
1501000e7ae3SMatthew Knepley   Level: intermediate
1502000e7ae3SMatthew Knepley 
1503000e7ae3SMatthew Knepley .keywords: TS, timestep
1504000e7ae3SMatthew Knepley @*/
15057087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1506000e7ae3SMatthew Knepley {
1507000e7ae3SMatthew Knepley   PetscFunctionBegin;
15080700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1509000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1510000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1511000e7ae3SMatthew Knepley }
1512000e7ae3SMatthew Knepley 
1513e74ef692SMatthew Knepley #undef __FUNCT__
15143f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
15153f2090d5SJed Brown /*@C
15163f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
15173f2090d5SJed Brown 
15183f2090d5SJed Brown   Collective on TS
15193f2090d5SJed Brown 
15203f2090d5SJed Brown   Input Parameters:
15213f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
15223f2090d5SJed Brown 
15233f2090d5SJed Brown   Notes:
15243f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
15253f2090d5SJed Brown   so most users would not generally call this routine themselves.
15263f2090d5SJed Brown 
15273f2090d5SJed Brown   Level: developer
15283f2090d5SJed Brown 
15293f2090d5SJed Brown .keywords: TS, timestep
15303f2090d5SJed Brown @*/
15317087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
15323f2090d5SJed Brown {
15333f2090d5SJed Brown   PetscErrorCode ierr;
15343f2090d5SJed Brown 
15353f2090d5SJed Brown   PetscFunctionBegin;
15360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
153772ac3e02SJed Brown   if (ts->ops->poststep) {
15383f2090d5SJed Brown     PetscStackPush("TS PostStep function");
15393f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
15403f2090d5SJed Brown     PetscStackPop;
154172ac3e02SJed Brown   }
15423f2090d5SJed Brown   PetscFunctionReturn(0);
15433f2090d5SJed Brown }
15443f2090d5SJed Brown 
1545d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1546d763cef2SBarry Smith 
15474a2ae208SSatish Balay #undef __FUNCT__
1548a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1549d763cef2SBarry Smith /*@C
1550a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1551d763cef2SBarry Smith    timestep to display the iteration's  progress.
1552d763cef2SBarry Smith 
15533f9fe445SBarry Smith    Logically Collective on TS
1554d763cef2SBarry Smith 
1555d763cef2SBarry Smith    Input Parameters:
1556d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1557d763cef2SBarry Smith .  func - monitoring routine
1558329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1559b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1560b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1561b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1562d763cef2SBarry Smith 
1563d763cef2SBarry Smith    Calling sequence of func:
1564a7cc72afSBarry Smith $    int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1565d763cef2SBarry Smith 
1566d763cef2SBarry Smith +    ts - the TS context
1567d763cef2SBarry Smith .    steps - iteration number
15681f06c33eSBarry Smith .    time - current time
1569d763cef2SBarry Smith .    x - current iterate
1570d763cef2SBarry Smith -    mctx - [optional] monitoring context
1571d763cef2SBarry Smith 
1572d763cef2SBarry Smith    Notes:
1573d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1574d763cef2SBarry Smith    already has been loaded.
1575d763cef2SBarry Smith 
1576025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1577025f1a04SBarry Smith 
1578d763cef2SBarry Smith    Level: intermediate
1579d763cef2SBarry Smith 
1580d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1581d763cef2SBarry Smith 
1582a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1583d763cef2SBarry Smith @*/
1584c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1585d763cef2SBarry Smith {
1586d763cef2SBarry Smith   PetscFunctionBegin;
15870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
158817186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1589d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1590329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1591d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1592d763cef2SBarry Smith   PetscFunctionReturn(0);
1593d763cef2SBarry Smith }
1594d763cef2SBarry Smith 
15954a2ae208SSatish Balay #undef __FUNCT__
1596a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1597d763cef2SBarry Smith /*@C
1598a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1599d763cef2SBarry Smith 
16003f9fe445SBarry Smith    Logically Collective on TS
1601d763cef2SBarry Smith 
1602d763cef2SBarry Smith    Input Parameters:
1603d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1604d763cef2SBarry Smith 
1605d763cef2SBarry Smith    Notes:
1606d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1607d763cef2SBarry Smith 
1608d763cef2SBarry Smith    Level: intermediate
1609d763cef2SBarry Smith 
1610d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1611d763cef2SBarry Smith 
1612a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1613d763cef2SBarry Smith @*/
16147087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1615d763cef2SBarry Smith {
1616d952e501SBarry Smith   PetscErrorCode ierr;
1617d952e501SBarry Smith   PetscInt       i;
1618d952e501SBarry Smith 
1619d763cef2SBarry Smith   PetscFunctionBegin;
16200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1621d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
1622d952e501SBarry Smith     if (ts->mdestroy[i]) {
16233c4aec1bSBarry Smith       ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
1624d952e501SBarry Smith     }
1625d952e501SBarry Smith   }
1626d763cef2SBarry Smith   ts->numbermonitors = 0;
1627d763cef2SBarry Smith   PetscFunctionReturn(0);
1628d763cef2SBarry Smith }
1629d763cef2SBarry Smith 
16304a2ae208SSatish Balay #undef __FUNCT__
1631a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
1632d8e5e3e6SSatish Balay /*@
1633a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
16345516499fSSatish Balay 
16355516499fSSatish Balay    Level: intermediate
163641251cbbSSatish Balay 
16375516499fSSatish Balay .keywords: TS, set, monitor
16385516499fSSatish Balay 
163941251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
164041251cbbSSatish Balay @*/
1641649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
1642d763cef2SBarry Smith {
1643dfbe8321SBarry Smith   PetscErrorCode ierr;
1644649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
1645d132466eSBarry Smith 
1646d763cef2SBarry Smith   PetscFunctionBegin;
1647649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1648649052a6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr);
1649649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1650d763cef2SBarry Smith   PetscFunctionReturn(0);
1651d763cef2SBarry Smith }
1652d763cef2SBarry Smith 
16534a2ae208SSatish Balay #undef __FUNCT__
1654cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
1655cd652676SJed Brown /*@
1656cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
1657cd652676SJed Brown 
1658cd652676SJed Brown    Logically Collective on TS
1659cd652676SJed Brown 
1660cd652676SJed Brown    Input Argument:
1661cd652676SJed Brown .  ts - time stepping context
1662cd652676SJed Brown 
1663cd652676SJed Brown    Output Argument:
1664cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
1665cd652676SJed Brown 
1666cd652676SJed Brown    Level: intermediate
1667cd652676SJed Brown 
1668cd652676SJed Brown .keywords: TS, set
1669cd652676SJed Brown 
1670cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
1671cd652676SJed Brown @*/
1672cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
1673cd652676SJed Brown {
1674cd652676SJed Brown 
1675cd652676SJed Brown   PetscFunctionBegin;
1676cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1677cd652676SJed Brown   ts->retain_stages = flg;
1678cd652676SJed Brown   PetscFunctionReturn(0);
1679cd652676SJed Brown }
1680cd652676SJed Brown 
1681cd652676SJed Brown #undef __FUNCT__
1682cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
1683cd652676SJed Brown /*@
1684cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
1685cd652676SJed Brown 
1686cd652676SJed Brown    Collective on TS
1687cd652676SJed Brown 
1688cd652676SJed Brown    Input Argument:
1689cd652676SJed Brown +  ts - time stepping context
1690cd652676SJed Brown -  t - time to interpolate to
1691cd652676SJed Brown 
1692cd652676SJed Brown    Output Argument:
1693cd652676SJed Brown .  X - state at given time
1694cd652676SJed Brown 
1695cd652676SJed Brown    Notes:
1696cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
1697cd652676SJed Brown 
1698cd652676SJed Brown    Level: intermediate
1699cd652676SJed Brown 
1700cd652676SJed Brown    Developer Notes:
1701cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
1702cd652676SJed Brown 
1703cd652676SJed Brown .keywords: TS, set
1704cd652676SJed Brown 
1705cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
1706cd652676SJed Brown @*/
1707cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X)
1708cd652676SJed Brown {
1709cd652676SJed Brown   PetscErrorCode ierr;
1710cd652676SJed Brown 
1711cd652676SJed Brown   PetscFunctionBegin;
1712cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1713cd652676SJed Brown   if (t < ts->ptime - ts->time_step || ts->ptime < t) SETERRQ3(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step,ts->ptime);
1714cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
1715cd652676SJed Brown   ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr);
1716cd652676SJed Brown   PetscFunctionReturn(0);
1717cd652676SJed Brown }
1718cd652676SJed Brown 
1719cd652676SJed Brown #undef __FUNCT__
17204a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1721d763cef2SBarry Smith /*@
1722d763cef2SBarry Smith    TSStep - Steps the requested number of timesteps.
1723d763cef2SBarry Smith 
1724d763cef2SBarry Smith    Collective on TS
1725d763cef2SBarry Smith 
1726d763cef2SBarry Smith    Input Parameter:
1727d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1728d763cef2SBarry Smith 
1729d763cef2SBarry Smith    Output Parameters:
1730d763cef2SBarry Smith +  steps - number of iterations until termination
1731142b95e3SSatish Balay -  ptime - time until termination
1732d763cef2SBarry Smith 
1733d763cef2SBarry Smith    Level: beginner
1734d763cef2SBarry Smith 
1735d763cef2SBarry Smith .keywords: TS, timestep, solve
1736d763cef2SBarry Smith 
1737d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy()
1738d763cef2SBarry Smith @*/
1739193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
1740d763cef2SBarry Smith {
1741dfbe8321SBarry Smith   PetscErrorCode ierr;
1742d763cef2SBarry Smith 
1743d763cef2SBarry Smith   PetscFunctionBegin;
17440700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1745277b19d0SLisandro Dalcin 
1746d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
1747d405a339SMatthew Knepley 
1748d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1749193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
1750d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1751d763cef2SBarry Smith   PetscFunctionReturn(0);
1752d763cef2SBarry Smith }
1753d763cef2SBarry Smith 
17544a2ae208SSatish Balay #undef __FUNCT__
17556a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
17566a4d4014SLisandro Dalcin /*@
17576a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
17586a4d4014SLisandro Dalcin 
17596a4d4014SLisandro Dalcin    Collective on TS
17606a4d4014SLisandro Dalcin 
17616a4d4014SLisandro Dalcin    Input Parameter:
17626a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
17636a4d4014SLisandro Dalcin -  x - the solution vector, or PETSC_NULL if it was set with TSSetSolution()
17646a4d4014SLisandro Dalcin 
17656a4d4014SLisandro Dalcin    Level: beginner
17666a4d4014SLisandro Dalcin 
17676a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
17686a4d4014SLisandro Dalcin 
17696a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
17706a4d4014SLisandro Dalcin @*/
17717087cfbeSBarry Smith PetscErrorCode  TSSolve(TS ts, Vec x)
17726a4d4014SLisandro Dalcin {
1773193ac0bcSJed Brown   PetscInt       i;
17744d7d938eSLisandro Dalcin   PetscBool      flg;
17754d7d938eSLisandro Dalcin   char           filename[PETSC_MAX_PATH_LEN];
17764d7d938eSLisandro Dalcin   PetscViewer    viewer;
17776a4d4014SLisandro Dalcin   PetscErrorCode ierr;
1778f22f69f0SBarry Smith 
17796a4d4014SLisandro Dalcin   PetscFunctionBegin;
17800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1781193ac0bcSJed Brown   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
1782193ac0bcSJed Brown   ierr = TSSetSolution(ts,x); CHKERRQ(ierr);
1783b5d403baSSean Farley   ierr = TSSetUp(ts); CHKERRQ(ierr);
17846a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
1785193ac0bcSJed Brown   ts->steps = 0;
1786193ac0bcSJed Brown   ts->linear_its = 0;
1787193ac0bcSJed Brown   ts->nonlinear_its = 0;
1788193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
1789193ac0bcSJed Brown   ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
1790193ac0bcSJed Brown 
1791193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
1792193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
1793193ac0bcSJed Brown   } else {
1794e1a7a14fSJed Brown     i = 0;
1795e1a7a14fSJed Brown     if (i >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
1796e1a7a14fSJed Brown     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
17976a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
1798e1a7a14fSJed Brown     while (!ts->reason) {
1799193ac0bcSJed Brown       ierr = TSPreStep(ts);CHKERRQ(ierr);
1800193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
1801193ac0bcSJed Brown       if (ts->reason < 0) {
1802193ac0bcSJed Brown         if (ts->errorifstepfailed) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed");
18033daf2de8SJed Brown       } else if (++i >= ts->max_steps) {
1804193ac0bcSJed Brown         ts->reason = TS_CONVERGED_ITS;
1805193ac0bcSJed Brown       } else if (ts->ptime >= ts->max_time) {
1806193ac0bcSJed Brown         ts->reason = TS_CONVERGED_TIME;
1807193ac0bcSJed Brown       }
1808193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
1809193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
1810193ac0bcSJed Brown     }
1811193ac0bcSJed Brown   }
18124d7d938eSLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
18134d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
18144d7d938eSLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr);
18154d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
18164d7d938eSLisandro Dalcin     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1817193ac0bcSJed Brown   }
18186a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
18196a4d4014SLisandro Dalcin }
18206a4d4014SLisandro Dalcin 
18216a4d4014SLisandro Dalcin #undef __FUNCT__
18224a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
1823d763cef2SBarry Smith /*
1824d763cef2SBarry Smith      Runs the user provided monitor routines, if they exists.
1825d763cef2SBarry Smith */
1826a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
1827d763cef2SBarry Smith {
18286849ba73SBarry Smith   PetscErrorCode ierr;
1829a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
1830d763cef2SBarry Smith 
1831d763cef2SBarry Smith   PetscFunctionBegin;
1832d763cef2SBarry Smith   for (i=0; i<n; i++) {
1833142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
1834d763cef2SBarry Smith   }
1835d763cef2SBarry Smith   PetscFunctionReturn(0);
1836d763cef2SBarry Smith }
1837d763cef2SBarry Smith 
1838d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
1839d763cef2SBarry Smith 
18404a2ae208SSatish Balay #undef __FUNCT__
1841a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate"
1842d763cef2SBarry Smith /*@C
1843a6570f20SBarry Smith    TSMonitorLGCreate - Creates a line graph context for use with
1844d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
1845d763cef2SBarry Smith 
1846d763cef2SBarry Smith    Collective on TS
1847d763cef2SBarry Smith 
1848d763cef2SBarry Smith    Input Parameters:
1849d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
1850d763cef2SBarry Smith .  label - the title to put in the title bar
18517c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
1852d763cef2SBarry Smith -  m, n - the screen width and height in pixels
1853d763cef2SBarry Smith 
1854d763cef2SBarry Smith    Output Parameter:
1855d763cef2SBarry Smith .  draw - the drawing context
1856d763cef2SBarry Smith 
1857d763cef2SBarry Smith    Options Database Key:
1858a6570f20SBarry Smith .  -ts_monitor_draw - automatically sets line graph monitor
1859d763cef2SBarry Smith 
1860d763cef2SBarry Smith    Notes:
1861a6570f20SBarry Smith    Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy().
1862d763cef2SBarry Smith 
1863d763cef2SBarry Smith    Level: intermediate
1864d763cef2SBarry Smith 
18657c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
1866d763cef2SBarry Smith 
1867a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet()
18687c922b88SBarry Smith 
1869d763cef2SBarry Smith @*/
18707087cfbeSBarry Smith PetscErrorCode  TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1871d763cef2SBarry Smith {
1872b0a32e0cSBarry Smith   PetscDraw      win;
1873dfbe8321SBarry Smith   PetscErrorCode ierr;
1874d763cef2SBarry Smith 
1875d763cef2SBarry Smith   PetscFunctionBegin;
1876b0a32e0cSBarry Smith   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
1877b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
1878b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
1879b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
1880d763cef2SBarry Smith 
188152e6d16bSBarry Smith   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
1882d763cef2SBarry Smith   PetscFunctionReturn(0);
1883d763cef2SBarry Smith }
1884d763cef2SBarry Smith 
18854a2ae208SSatish Balay #undef __FUNCT__
1886a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG"
1887a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
1888d763cef2SBarry Smith {
1889b0a32e0cSBarry Smith   PetscDrawLG    lg = (PetscDrawLG) monctx;
189087828ca2SBarry Smith   PetscReal      x,y = ptime;
1891dfbe8321SBarry Smith   PetscErrorCode ierr;
1892d763cef2SBarry Smith 
1893d763cef2SBarry Smith   PetscFunctionBegin;
18947c922b88SBarry Smith   if (!monctx) {
18957c922b88SBarry Smith     MPI_Comm    comm;
1896b0a32e0cSBarry Smith     PetscViewer viewer;
18977c922b88SBarry Smith 
18987c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
1899b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
1900b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
19017c922b88SBarry Smith   }
19027c922b88SBarry Smith 
1903b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
190487828ca2SBarry Smith   x = (PetscReal)n;
1905b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1906d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
1907b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1908d763cef2SBarry Smith   }
1909d763cef2SBarry Smith   PetscFunctionReturn(0);
1910d763cef2SBarry Smith }
1911d763cef2SBarry Smith 
19124a2ae208SSatish Balay #undef __FUNCT__
1913a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy"
1914d763cef2SBarry Smith /*@C
1915a6570f20SBarry Smith    TSMonitorLGDestroy - Destroys a line graph context that was created
1916a6570f20SBarry Smith    with TSMonitorLGCreate().
1917d763cef2SBarry Smith 
1918b0a32e0cSBarry Smith    Collective on PetscDrawLG
1919d763cef2SBarry Smith 
1920d763cef2SBarry Smith    Input Parameter:
1921d763cef2SBarry Smith .  draw - the drawing context
1922d763cef2SBarry Smith 
1923d763cef2SBarry Smith    Level: intermediate
1924d763cef2SBarry Smith 
1925d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
1926d763cef2SBarry Smith 
1927a6570f20SBarry Smith .seealso: TSMonitorLGCreate(),  TSMonitorSet(), TSMonitorLG();
1928d763cef2SBarry Smith @*/
19296bf464f9SBarry Smith PetscErrorCode  TSMonitorLGDestroy(PetscDrawLG *drawlg)
1930d763cef2SBarry Smith {
1931b0a32e0cSBarry Smith   PetscDraw      draw;
1932dfbe8321SBarry Smith   PetscErrorCode ierr;
1933d763cef2SBarry Smith 
1934d763cef2SBarry Smith   PetscFunctionBegin;
19356bf464f9SBarry Smith   ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr);
19366bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
1937b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
1938d763cef2SBarry Smith   PetscFunctionReturn(0);
1939d763cef2SBarry Smith }
1940d763cef2SBarry Smith 
19414a2ae208SSatish Balay #undef __FUNCT__
19424a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
1943d763cef2SBarry Smith /*@
1944d763cef2SBarry Smith    TSGetTime - Gets the current time.
1945d763cef2SBarry Smith 
1946d763cef2SBarry Smith    Not Collective
1947d763cef2SBarry Smith 
1948d763cef2SBarry Smith    Input Parameter:
1949d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1950d763cef2SBarry Smith 
1951d763cef2SBarry Smith    Output Parameter:
1952d763cef2SBarry Smith .  t  - the current time
1953d763cef2SBarry Smith 
1954d763cef2SBarry Smith    Level: beginner
1955d763cef2SBarry Smith 
1956d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1957d763cef2SBarry Smith 
1958d763cef2SBarry Smith .keywords: TS, get, time
1959d763cef2SBarry Smith @*/
19607087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
1961d763cef2SBarry Smith {
1962d763cef2SBarry Smith   PetscFunctionBegin;
19630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19644482741eSBarry Smith   PetscValidDoublePointer(t,2);
1965d763cef2SBarry Smith   *t = ts->ptime;
1966d763cef2SBarry Smith   PetscFunctionReturn(0);
1967d763cef2SBarry Smith }
1968d763cef2SBarry Smith 
19694a2ae208SSatish Balay #undef __FUNCT__
19706a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
19716a4d4014SLisandro Dalcin /*@
19726a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
19736a4d4014SLisandro Dalcin 
19743f9fe445SBarry Smith    Logically Collective on TS
19756a4d4014SLisandro Dalcin 
19766a4d4014SLisandro Dalcin    Input Parameters:
19776a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
19786a4d4014SLisandro Dalcin -  time - the time
19796a4d4014SLisandro Dalcin 
19806a4d4014SLisandro Dalcin    Level: intermediate
19816a4d4014SLisandro Dalcin 
19826a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
19836a4d4014SLisandro Dalcin 
19846a4d4014SLisandro Dalcin .keywords: TS, set, time
19856a4d4014SLisandro Dalcin @*/
19867087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
19876a4d4014SLisandro Dalcin {
19886a4d4014SLisandro Dalcin   PetscFunctionBegin;
19890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1990c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
19916a4d4014SLisandro Dalcin   ts->ptime = t;
19926a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
19936a4d4014SLisandro Dalcin }
19946a4d4014SLisandro Dalcin 
19956a4d4014SLisandro Dalcin #undef __FUNCT__
19964a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
1997d763cef2SBarry Smith /*@C
1998d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
1999d763cef2SBarry Smith    TS options in the database.
2000d763cef2SBarry Smith 
20013f9fe445SBarry Smith    Logically Collective on TS
2002d763cef2SBarry Smith 
2003d763cef2SBarry Smith    Input Parameter:
2004d763cef2SBarry Smith +  ts     - The TS context
2005d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2006d763cef2SBarry Smith 
2007d763cef2SBarry Smith    Notes:
2008d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2009d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2010d763cef2SBarry Smith    hyphen.
2011d763cef2SBarry Smith 
2012d763cef2SBarry Smith    Level: advanced
2013d763cef2SBarry Smith 
2014d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2015d763cef2SBarry Smith 
2016d763cef2SBarry Smith .seealso: TSSetFromOptions()
2017d763cef2SBarry Smith 
2018d763cef2SBarry Smith @*/
20197087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2020d763cef2SBarry Smith {
2021dfbe8321SBarry Smith   PetscErrorCode ierr;
2022089b2837SJed Brown   SNES           snes;
2023d763cef2SBarry Smith 
2024d763cef2SBarry Smith   PetscFunctionBegin;
20250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2026d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2027089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2028089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2029d763cef2SBarry Smith   PetscFunctionReturn(0);
2030d763cef2SBarry Smith }
2031d763cef2SBarry Smith 
2032d763cef2SBarry Smith 
20334a2ae208SSatish Balay #undef __FUNCT__
20344a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2035d763cef2SBarry Smith /*@C
2036d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2037d763cef2SBarry Smith    TS options in the database.
2038d763cef2SBarry Smith 
20393f9fe445SBarry Smith    Logically Collective on TS
2040d763cef2SBarry Smith 
2041d763cef2SBarry Smith    Input Parameter:
2042d763cef2SBarry Smith +  ts     - The TS context
2043d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2044d763cef2SBarry Smith 
2045d763cef2SBarry Smith    Notes:
2046d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2047d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2048d763cef2SBarry Smith    hyphen.
2049d763cef2SBarry Smith 
2050d763cef2SBarry Smith    Level: advanced
2051d763cef2SBarry Smith 
2052d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2053d763cef2SBarry Smith 
2054d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2055d763cef2SBarry Smith 
2056d763cef2SBarry Smith @*/
20577087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2058d763cef2SBarry Smith {
2059dfbe8321SBarry Smith   PetscErrorCode ierr;
2060089b2837SJed Brown   SNES           snes;
2061d763cef2SBarry Smith 
2062d763cef2SBarry Smith   PetscFunctionBegin;
20630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2064d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2065089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2066089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2067d763cef2SBarry Smith   PetscFunctionReturn(0);
2068d763cef2SBarry Smith }
2069d763cef2SBarry Smith 
20704a2ae208SSatish Balay #undef __FUNCT__
20714a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2072d763cef2SBarry Smith /*@C
2073d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2074d763cef2SBarry Smith    TS options in the database.
2075d763cef2SBarry Smith 
2076d763cef2SBarry Smith    Not Collective
2077d763cef2SBarry Smith 
2078d763cef2SBarry Smith    Input Parameter:
2079d763cef2SBarry Smith .  ts - The TS context
2080d763cef2SBarry Smith 
2081d763cef2SBarry Smith    Output Parameter:
2082d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2083d763cef2SBarry Smith 
2084d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2085d763cef2SBarry Smith    sufficient length to hold the prefix.
2086d763cef2SBarry Smith 
2087d763cef2SBarry Smith    Level: intermediate
2088d763cef2SBarry Smith 
2089d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2090d763cef2SBarry Smith 
2091d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2092d763cef2SBarry Smith @*/
20937087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2094d763cef2SBarry Smith {
2095dfbe8321SBarry Smith   PetscErrorCode ierr;
2096d763cef2SBarry Smith 
2097d763cef2SBarry Smith   PetscFunctionBegin;
20980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20994482741eSBarry Smith   PetscValidPointer(prefix,2);
2100d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2101d763cef2SBarry Smith   PetscFunctionReturn(0);
2102d763cef2SBarry Smith }
2103d763cef2SBarry Smith 
21044a2ae208SSatish Balay #undef __FUNCT__
21054a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2106d763cef2SBarry Smith /*@C
2107d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2108d763cef2SBarry Smith 
2109d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2110d763cef2SBarry Smith 
2111d763cef2SBarry Smith    Input Parameter:
2112d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2113d763cef2SBarry Smith 
2114d763cef2SBarry Smith    Output Parameters:
2115d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2116d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2117089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2118d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2119d763cef2SBarry Smith 
2120d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2121d763cef2SBarry Smith 
2122d763cef2SBarry Smith    Level: intermediate
2123d763cef2SBarry Smith 
212426d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2125d763cef2SBarry Smith 
2126d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2127d763cef2SBarry Smith @*/
2128089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2129d763cef2SBarry Smith {
2130089b2837SJed Brown   PetscErrorCode ierr;
2131089b2837SJed Brown   SNES           snes;
2132089b2837SJed Brown 
2133d763cef2SBarry Smith   PetscFunctionBegin;
2134089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2135089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
21363b5f76d0SSean Farley   if (func) *func = ts->userops->rhsjacobian;
213726d46c62SHong Zhang   if (ctx) *ctx = ts->jacP;
2138d763cef2SBarry Smith   PetscFunctionReturn(0);
2139d763cef2SBarry Smith }
2140d763cef2SBarry Smith 
21411713a123SBarry Smith #undef __FUNCT__
21422eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
21432eca1d9cSJed Brown /*@C
21442eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
21452eca1d9cSJed Brown 
21462eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
21472eca1d9cSJed Brown 
21482eca1d9cSJed Brown    Input Parameter:
21492eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
21502eca1d9cSJed Brown 
21512eca1d9cSJed Brown    Output Parameters:
21522eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
21532eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
21542eca1d9cSJed Brown .  f   - The function to compute the matrices
21552eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
21562eca1d9cSJed Brown 
21572eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
21582eca1d9cSJed Brown 
21592eca1d9cSJed Brown    Level: advanced
21602eca1d9cSJed Brown 
21612eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
21622eca1d9cSJed Brown 
21632eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
21642eca1d9cSJed Brown @*/
21657087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
21662eca1d9cSJed Brown {
2167089b2837SJed Brown   PetscErrorCode ierr;
2168089b2837SJed Brown   SNES           snes;
2169089b2837SJed Brown 
21702eca1d9cSJed Brown   PetscFunctionBegin;
2171089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2172089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
21733b5f76d0SSean Farley   if (f) *f = ts->userops->ijacobian;
21742eca1d9cSJed Brown   if (ctx) *ctx = ts->jacP;
21752eca1d9cSJed Brown   PetscFunctionReturn(0);
21762eca1d9cSJed Brown }
21772eca1d9cSJed Brown 
21782eca1d9cSJed Brown #undef __FUNCT__
2179a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution"
21801713a123SBarry Smith /*@C
2181a6570f20SBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by calling
21821713a123SBarry Smith    VecView() for the solution at each timestep
21831713a123SBarry Smith 
21841713a123SBarry Smith    Collective on TS
21851713a123SBarry Smith 
21861713a123SBarry Smith    Input Parameters:
21871713a123SBarry Smith +  ts - the TS context
21881713a123SBarry Smith .  step - current time-step
2189142b95e3SSatish Balay .  ptime - current time
21901713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
21911713a123SBarry Smith 
21921713a123SBarry Smith    Level: intermediate
21931713a123SBarry Smith 
21941713a123SBarry Smith .keywords: TS,  vector, monitor, view
21951713a123SBarry Smith 
2196a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
21971713a123SBarry Smith @*/
21987087cfbeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
21991713a123SBarry Smith {
2200dfbe8321SBarry Smith   PetscErrorCode ierr;
22011713a123SBarry Smith   PetscViewer    viewer = (PetscViewer) dummy;
22021713a123SBarry Smith 
22031713a123SBarry Smith   PetscFunctionBegin;
2204a34d58ebSBarry Smith   if (!dummy) {
22057adad957SLisandro Dalcin     viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm);
22061713a123SBarry Smith   }
22071713a123SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
22081713a123SBarry Smith   PetscFunctionReturn(0);
22091713a123SBarry Smith }
22101713a123SBarry Smith 
22111713a123SBarry Smith 
22126c699258SBarry Smith #undef __FUNCT__
22136c699258SBarry Smith #define __FUNCT__ "TSSetDM"
22146c699258SBarry Smith /*@
22156c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
22166c699258SBarry Smith 
22173f9fe445SBarry Smith    Logically Collective on TS and DM
22186c699258SBarry Smith 
22196c699258SBarry Smith    Input Parameters:
22206c699258SBarry Smith +  ts - the preconditioner context
22216c699258SBarry Smith -  dm - the dm
22226c699258SBarry Smith 
22236c699258SBarry Smith    Level: intermediate
22246c699258SBarry Smith 
22256c699258SBarry Smith 
22266c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
22276c699258SBarry Smith @*/
22287087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
22296c699258SBarry Smith {
22306c699258SBarry Smith   PetscErrorCode ierr;
2231089b2837SJed Brown   SNES           snes;
22326c699258SBarry Smith 
22336c699258SBarry Smith   PetscFunctionBegin;
22340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
223570663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
22366bf464f9SBarry Smith   ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
22376c699258SBarry Smith   ts->dm = dm;
2238089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2239089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
22406c699258SBarry Smith   PetscFunctionReturn(0);
22416c699258SBarry Smith }
22426c699258SBarry Smith 
22436c699258SBarry Smith #undef __FUNCT__
22446c699258SBarry Smith #define __FUNCT__ "TSGetDM"
22456c699258SBarry Smith /*@
22466c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
22476c699258SBarry Smith 
22483f9fe445SBarry Smith    Not Collective
22496c699258SBarry Smith 
22506c699258SBarry Smith    Input Parameter:
22516c699258SBarry Smith . ts - the preconditioner context
22526c699258SBarry Smith 
22536c699258SBarry Smith    Output Parameter:
22546c699258SBarry Smith .  dm - the dm
22556c699258SBarry Smith 
22566c699258SBarry Smith    Level: intermediate
22576c699258SBarry Smith 
22586c699258SBarry Smith 
22596c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
22606c699258SBarry Smith @*/
22617087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
22626c699258SBarry Smith {
22636c699258SBarry Smith   PetscFunctionBegin;
22640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22656c699258SBarry Smith   *dm = ts->dm;
22666c699258SBarry Smith   PetscFunctionReturn(0);
22676c699258SBarry Smith }
22681713a123SBarry Smith 
22690f5c6efeSJed Brown #undef __FUNCT__
22700f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
22710f5c6efeSJed Brown /*@
22720f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
22730f5c6efeSJed Brown 
22743f9fe445SBarry Smith    Logically Collective on SNES
22750f5c6efeSJed Brown 
22760f5c6efeSJed Brown    Input Parameter:
2277d42a1c89SJed Brown + snes - nonlinear solver
22780f5c6efeSJed Brown . X - the current state at which to evaluate the residual
2279d42a1c89SJed Brown - ctx - user context, must be a TS
22800f5c6efeSJed Brown 
22810f5c6efeSJed Brown    Output Parameter:
22820f5c6efeSJed Brown . F - the nonlinear residual
22830f5c6efeSJed Brown 
22840f5c6efeSJed Brown    Notes:
22850f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
22860f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
22870f5c6efeSJed Brown 
22880f5c6efeSJed Brown    Level: advanced
22890f5c6efeSJed Brown 
22900f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
22910f5c6efeSJed Brown @*/
22927087cfbeSBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx)
22930f5c6efeSJed Brown {
22940f5c6efeSJed Brown   TS ts = (TS)ctx;
22950f5c6efeSJed Brown   PetscErrorCode ierr;
22960f5c6efeSJed Brown 
22970f5c6efeSJed Brown   PetscFunctionBegin;
22980f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22990f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
23000f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
23010f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
23020f5c6efeSJed Brown   ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr);
23030f5c6efeSJed Brown   PetscFunctionReturn(0);
23040f5c6efeSJed Brown }
23050f5c6efeSJed Brown 
23060f5c6efeSJed Brown #undef __FUNCT__
23070f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
23080f5c6efeSJed Brown /*@
23090f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
23100f5c6efeSJed Brown 
23110f5c6efeSJed Brown    Collective on SNES
23120f5c6efeSJed Brown 
23130f5c6efeSJed Brown    Input Parameter:
23140f5c6efeSJed Brown + snes - nonlinear solver
23150f5c6efeSJed Brown . X - the current state at which to evaluate the residual
23160f5c6efeSJed Brown - ctx - user context, must be a TS
23170f5c6efeSJed Brown 
23180f5c6efeSJed Brown    Output Parameter:
23190f5c6efeSJed Brown + A - the Jacobian
23200f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
23210f5c6efeSJed Brown - flag - indicates any structure change in the matrix
23220f5c6efeSJed Brown 
23230f5c6efeSJed Brown    Notes:
23240f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
23250f5c6efeSJed Brown 
23260f5c6efeSJed Brown    Level: developer
23270f5c6efeSJed Brown 
23280f5c6efeSJed Brown .seealso: SNESSetJacobian()
23290f5c6efeSJed Brown @*/
23307087cfbeSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx)
23310f5c6efeSJed Brown {
23320f5c6efeSJed Brown   TS ts = (TS)ctx;
23330f5c6efeSJed Brown   PetscErrorCode ierr;
23340f5c6efeSJed Brown 
23350f5c6efeSJed Brown   PetscFunctionBegin;
23360f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23370f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
23380f5c6efeSJed Brown   PetscValidPointer(A,3);
23390f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
23400f5c6efeSJed Brown   PetscValidPointer(B,4);
23410f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
23420f5c6efeSJed Brown   PetscValidPointer(flag,5);
23430f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
23440f5c6efeSJed Brown   ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr);
23450f5c6efeSJed Brown   PetscFunctionReturn(0);
23460f5c6efeSJed Brown }
2347325fc9f4SBarry Smith 
23480e4ef248SJed Brown #undef __FUNCT__
23490e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
23500e4ef248SJed Brown /*@C
23510e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
23520e4ef248SJed Brown 
23530e4ef248SJed Brown    Collective on TS
23540e4ef248SJed Brown 
23550e4ef248SJed Brown    Input Arguments:
23560e4ef248SJed Brown +  ts - time stepping context
23570e4ef248SJed Brown .  t - time at which to evaluate
23580e4ef248SJed Brown .  X - state at which to evaluate
23590e4ef248SJed Brown -  ctx - context
23600e4ef248SJed Brown 
23610e4ef248SJed Brown    Output Arguments:
23620e4ef248SJed Brown .  F - right hand side
23630e4ef248SJed Brown 
23640e4ef248SJed Brown    Level: intermediate
23650e4ef248SJed Brown 
23660e4ef248SJed Brown    Notes:
23670e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
23680e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
23690e4ef248SJed Brown 
23700e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
23710e4ef248SJed Brown @*/
23720e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx)
23730e4ef248SJed Brown {
23740e4ef248SJed Brown   PetscErrorCode ierr;
23750e4ef248SJed Brown   Mat Arhs,Brhs;
23760e4ef248SJed Brown   MatStructure flg2;
23770e4ef248SJed Brown 
23780e4ef248SJed Brown   PetscFunctionBegin;
23790e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
23800e4ef248SJed Brown   ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
23810e4ef248SJed Brown   ierr = MatMult(Arhs,X,F);CHKERRQ(ierr);
23820e4ef248SJed Brown   PetscFunctionReturn(0);
23830e4ef248SJed Brown }
23840e4ef248SJed Brown 
23850e4ef248SJed Brown #undef __FUNCT__
23860e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
23870e4ef248SJed Brown /*@C
23880e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
23890e4ef248SJed Brown 
23900e4ef248SJed Brown    Collective on TS
23910e4ef248SJed Brown 
23920e4ef248SJed Brown    Input Arguments:
23930e4ef248SJed Brown +  ts - time stepping context
23940e4ef248SJed Brown .  t - time at which to evaluate
23950e4ef248SJed Brown .  X - state at which to evaluate
23960e4ef248SJed Brown -  ctx - context
23970e4ef248SJed Brown 
23980e4ef248SJed Brown    Output Arguments:
23990e4ef248SJed Brown +  A - pointer to operator
24000e4ef248SJed Brown .  B - pointer to preconditioning matrix
24010e4ef248SJed Brown -  flg - matrix structure flag
24020e4ef248SJed Brown 
24030e4ef248SJed Brown    Level: intermediate
24040e4ef248SJed Brown 
24050e4ef248SJed Brown    Notes:
24060e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
24070e4ef248SJed Brown 
24080e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
24090e4ef248SJed Brown @*/
24100e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx)
24110e4ef248SJed Brown {
24120e4ef248SJed Brown 
24130e4ef248SJed Brown   PetscFunctionBegin;
24140e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
24150e4ef248SJed Brown   PetscFunctionReturn(0);
24160e4ef248SJed Brown }
24170e4ef248SJed Brown 
24180026cea9SSean Farley #undef __FUNCT__
24190026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
24200026cea9SSean Farley /*@C
24210026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
24220026cea9SSean Farley 
24230026cea9SSean Farley    Collective on TS
24240026cea9SSean Farley 
24250026cea9SSean Farley    Input Arguments:
24260026cea9SSean Farley +  ts - time stepping context
24270026cea9SSean Farley .  t - time at which to evaluate
24280026cea9SSean Farley .  X - state at which to evaluate
24290026cea9SSean Farley .  Xdot - time derivative of state vector
24300026cea9SSean Farley -  ctx - context
24310026cea9SSean Farley 
24320026cea9SSean Farley    Output Arguments:
24330026cea9SSean Farley .  F - left hand side
24340026cea9SSean Farley 
24350026cea9SSean Farley    Level: intermediate
24360026cea9SSean Farley 
24370026cea9SSean Farley    Notes:
24380026cea9SSean Farley    The assumption here is that the left hand side is of the form A*Xdot (and not A*Xdot + B*X). For other cases, the
24390026cea9SSean Farley    user is required to write their own TSComputeIFunction.
24400026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
24410026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
24420026cea9SSean Farley 
24430026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
24440026cea9SSean Farley @*/
24450026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx)
24460026cea9SSean Farley {
24470026cea9SSean Farley   PetscErrorCode ierr;
24480026cea9SSean Farley   Mat A,B;
24490026cea9SSean Farley   MatStructure flg2;
24500026cea9SSean Farley 
24510026cea9SSean Farley   PetscFunctionBegin;
24520026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
24530026cea9SSean Farley   ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
24540026cea9SSean Farley   ierr = MatMult(A,Xdot,F);CHKERRQ(ierr);
24550026cea9SSean Farley   PetscFunctionReturn(0);
24560026cea9SSean Farley }
24570026cea9SSean Farley 
24580026cea9SSean Farley #undef __FUNCT__
24590026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
24600026cea9SSean Farley /*@C
24610026cea9SSean Farley    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
24620026cea9SSean Farley 
24630026cea9SSean Farley    Collective on TS
24640026cea9SSean Farley 
24650026cea9SSean Farley    Input Arguments:
24660026cea9SSean Farley +  ts - time stepping context
24670026cea9SSean Farley .  t - time at which to evaluate
24680026cea9SSean Farley .  X - state at which to evaluate
24690026cea9SSean Farley .  Xdot - time derivative of state vector
24700026cea9SSean Farley .  shift - shift to apply
24710026cea9SSean Farley -  ctx - context
24720026cea9SSean Farley 
24730026cea9SSean Farley    Output Arguments:
24740026cea9SSean Farley +  A - pointer to operator
24750026cea9SSean Farley .  B - pointer to preconditioning matrix
24760026cea9SSean Farley -  flg - matrix structure flag
24770026cea9SSean Farley 
24780026cea9SSean Farley    Level: intermediate
24790026cea9SSean Farley 
24800026cea9SSean Farley    Notes:
24810026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
24820026cea9SSean Farley 
24830026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
24840026cea9SSean Farley @*/
24850026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
24860026cea9SSean Farley {
24870026cea9SSean Farley 
24880026cea9SSean Farley   PetscFunctionBegin;
24890026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
24900026cea9SSean Farley   PetscFunctionReturn(0);
24910026cea9SSean Farley }
24920026cea9SSean Farley 
24934af1b03aSJed Brown 
24944af1b03aSJed Brown #undef __FUNCT__
24954af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
24964af1b03aSJed Brown /*@
24974af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
24984af1b03aSJed Brown 
24994af1b03aSJed Brown    Not Collective
25004af1b03aSJed Brown 
25014af1b03aSJed Brown    Input Parameter:
25024af1b03aSJed Brown .  ts - the TS context
25034af1b03aSJed Brown 
25044af1b03aSJed Brown    Output Parameter:
25054af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
25064af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
25074af1b03aSJed Brown 
25084af1b03aSJed Brown    Level: intermediate
25094af1b03aSJed Brown 
2510cd652676SJed Brown    Notes:
2511cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
25124af1b03aSJed Brown 
25134af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
25144af1b03aSJed Brown 
25154af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
25164af1b03aSJed Brown @*/
25174af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
25184af1b03aSJed Brown {
25194af1b03aSJed Brown   PetscFunctionBegin;
25204af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25214af1b03aSJed Brown   PetscValidPointer(reason,2);
25224af1b03aSJed Brown   *reason = ts->reason;
25234af1b03aSJed Brown   PetscFunctionReturn(0);
25244af1b03aSJed Brown }
25254af1b03aSJed Brown 
2526325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2527c6db04a5SJed Brown #include <mex.h>
2528325fc9f4SBarry Smith 
2529325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
2530325fc9f4SBarry Smith 
2531325fc9f4SBarry Smith #undef __FUNCT__
2532325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
2533325fc9f4SBarry Smith /*
2534325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
2535325fc9f4SBarry Smith                          TSSetFunctionMatlab().
2536325fc9f4SBarry Smith 
2537325fc9f4SBarry Smith    Collective on TS
2538325fc9f4SBarry Smith 
2539325fc9f4SBarry Smith    Input Parameters:
2540325fc9f4SBarry Smith +  snes - the TS context
2541325fc9f4SBarry Smith -  x - input vector
2542325fc9f4SBarry Smith 
2543325fc9f4SBarry Smith    Output Parameter:
2544325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
2545325fc9f4SBarry Smith 
2546325fc9f4SBarry Smith    Notes:
2547325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
2548325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
2549325fc9f4SBarry Smith    themselves.
2550325fc9f4SBarry Smith 
2551325fc9f4SBarry Smith    Level: developer
2552325fc9f4SBarry Smith 
2553325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2554325fc9f4SBarry Smith 
2555325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2556325fc9f4SBarry Smith */
25577087cfbeSBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx)
2558325fc9f4SBarry Smith {
2559325fc9f4SBarry Smith   PetscErrorCode   ierr;
2560325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2561325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
2562325fc9f4SBarry Smith   mxArray          *plhs[1],*prhs[7];
2563325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
2564325fc9f4SBarry Smith 
2565325fc9f4SBarry Smith   PetscFunctionBegin;
2566325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2567325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2568325fc9f4SBarry Smith   PetscValidHeaderSpecific(xdot,VEC_CLASSID,4);
2569325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
2570325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,x,3);
2571325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
2572325fc9f4SBarry Smith 
2573325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2574325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2575880f3077SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr);
2576325fc9f4SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
2577325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2578325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
2579325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2580325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2581325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
2582325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
2583325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
2584325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
2585325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2586325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2587325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2588325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2589325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2590325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2591325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2592325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2593325fc9f4SBarry Smith   PetscFunctionReturn(0);
2594325fc9f4SBarry Smith }
2595325fc9f4SBarry Smith 
2596325fc9f4SBarry Smith 
2597325fc9f4SBarry Smith #undef __FUNCT__
2598325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
2599325fc9f4SBarry Smith /*
2600325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
2601325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
2602e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
2603325fc9f4SBarry Smith 
2604325fc9f4SBarry Smith    Logically Collective on TS
2605325fc9f4SBarry Smith 
2606325fc9f4SBarry Smith    Input Parameters:
2607325fc9f4SBarry Smith +  ts - the TS context
2608325fc9f4SBarry Smith -  func - function evaluation routine
2609325fc9f4SBarry Smith 
2610325fc9f4SBarry Smith    Calling sequence of func:
2611325fc9f4SBarry Smith $    func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx);
2612325fc9f4SBarry Smith 
2613325fc9f4SBarry Smith    Level: beginner
2614325fc9f4SBarry Smith 
2615325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2616325fc9f4SBarry Smith 
2617325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2618325fc9f4SBarry Smith */
2619cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
2620325fc9f4SBarry Smith {
2621325fc9f4SBarry Smith   PetscErrorCode  ierr;
2622325fc9f4SBarry Smith   TSMatlabContext *sctx;
2623325fc9f4SBarry Smith 
2624325fc9f4SBarry Smith   PetscFunctionBegin;
2625325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2626325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2627325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2628325fc9f4SBarry Smith   /*
2629325fc9f4SBarry Smith      This should work, but it doesn't
2630325fc9f4SBarry Smith   sctx->ctx = ctx;
2631325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2632325fc9f4SBarry Smith   */
2633325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2634cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
2635325fc9f4SBarry Smith   PetscFunctionReturn(0);
2636325fc9f4SBarry Smith }
2637325fc9f4SBarry Smith 
2638325fc9f4SBarry Smith #undef __FUNCT__
2639325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
2640325fc9f4SBarry Smith /*
2641325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
2642325fc9f4SBarry Smith                          TSSetJacobianMatlab().
2643325fc9f4SBarry Smith 
2644325fc9f4SBarry Smith    Collective on TS
2645325fc9f4SBarry Smith 
2646325fc9f4SBarry Smith    Input Parameters:
2647cdcf91faSSean Farley +  ts - the TS context
2648325fc9f4SBarry Smith .  x - input vector
2649325fc9f4SBarry Smith .  A, B - the matrices
2650325fc9f4SBarry Smith -  ctx - user context
2651325fc9f4SBarry Smith 
2652325fc9f4SBarry Smith    Output Parameter:
2653325fc9f4SBarry Smith .  flag - structure of the matrix
2654325fc9f4SBarry Smith 
2655325fc9f4SBarry Smith    Level: developer
2656325fc9f4SBarry Smith 
2657325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2658325fc9f4SBarry Smith 
2659325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2660325fc9f4SBarry Smith @*/
2661cdcf91faSSean Farley PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
2662325fc9f4SBarry Smith {
2663325fc9f4SBarry Smith   PetscErrorCode  ierr;
2664325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2665325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
2666325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
2667325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
2668325fc9f4SBarry Smith 
2669325fc9f4SBarry Smith   PetscFunctionBegin;
2670cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2671325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2672325fc9f4SBarry Smith 
2673325fc9f4SBarry Smith   /* call Matlab function in ctx with arguments x and y */
2674325fc9f4SBarry Smith 
2675cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
2676325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2677325fc9f4SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr);
2678325fc9f4SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
2679325fc9f4SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
2680325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2681325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
2682325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2683325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2684325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
2685325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
2686325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
2687325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
2688325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
2689325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
2690325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2691325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
2692325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2693325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2694325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2695325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2696325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2697325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2698325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
2699325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
2700325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2701325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
2702325fc9f4SBarry Smith   PetscFunctionReturn(0);
2703325fc9f4SBarry Smith }
2704325fc9f4SBarry Smith 
2705325fc9f4SBarry Smith 
2706325fc9f4SBarry Smith #undef __FUNCT__
2707325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
2708325fc9f4SBarry Smith /*
2709325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
2710e3c5b3baSBarry 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
2711325fc9f4SBarry Smith 
2712325fc9f4SBarry Smith    Logically Collective on TS
2713325fc9f4SBarry Smith 
2714325fc9f4SBarry Smith    Input Parameters:
2715cdcf91faSSean Farley +  ts - the TS context
2716325fc9f4SBarry Smith .  A,B - Jacobian matrices
2717325fc9f4SBarry Smith .  func - function evaluation routine
2718325fc9f4SBarry Smith -  ctx - user context
2719325fc9f4SBarry Smith 
2720325fc9f4SBarry Smith    Calling sequence of func:
2721cdcf91faSSean Farley $    flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx);
2722325fc9f4SBarry Smith 
2723325fc9f4SBarry Smith 
2724325fc9f4SBarry Smith    Level: developer
2725325fc9f4SBarry Smith 
2726325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2727325fc9f4SBarry Smith 
2728325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2729325fc9f4SBarry Smith */
2730cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
2731325fc9f4SBarry Smith {
2732325fc9f4SBarry Smith   PetscErrorCode    ierr;
2733325fc9f4SBarry Smith   TSMatlabContext *sctx;
2734325fc9f4SBarry Smith 
2735325fc9f4SBarry Smith   PetscFunctionBegin;
2736325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2737325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2738325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2739325fc9f4SBarry Smith   /*
2740325fc9f4SBarry Smith      This should work, but it doesn't
2741325fc9f4SBarry Smith   sctx->ctx = ctx;
2742325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2743325fc9f4SBarry Smith   */
2744325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2745cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
2746325fc9f4SBarry Smith   PetscFunctionReturn(0);
2747325fc9f4SBarry Smith }
2748325fc9f4SBarry Smith 
2749b5b1a830SBarry Smith #undef __FUNCT__
2750b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
2751b5b1a830SBarry Smith /*
2752b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
2753b5b1a830SBarry Smith 
2754b5b1a830SBarry Smith    Collective on TS
2755b5b1a830SBarry Smith 
2756b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2757b5b1a830SBarry Smith @*/
2758cdcf91faSSean Farley PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx)
2759b5b1a830SBarry Smith {
2760b5b1a830SBarry Smith   PetscErrorCode  ierr;
2761b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2762a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
2763b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
2764b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
2765b5b1a830SBarry Smith 
2766b5b1a830SBarry Smith   PetscFunctionBegin;
2767cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2768b5b1a830SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,4);
2769b5b1a830SBarry Smith 
2770cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
2771b5b1a830SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2772b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2773b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
2774b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
2775b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
2776b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
2777b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
2778b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
2779b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2780b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
2781b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
2782b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
2783b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
2784b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
2785b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
2786b5b1a830SBarry Smith   PetscFunctionReturn(0);
2787b5b1a830SBarry Smith }
2788b5b1a830SBarry Smith 
2789b5b1a830SBarry Smith 
2790b5b1a830SBarry Smith #undef __FUNCT__
2791b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
2792b5b1a830SBarry Smith /*
2793b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
2794b5b1a830SBarry Smith 
2795b5b1a830SBarry Smith    Level: developer
2796b5b1a830SBarry Smith 
2797b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
2798b5b1a830SBarry Smith 
2799b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2800b5b1a830SBarry Smith */
2801cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
2802b5b1a830SBarry Smith {
2803b5b1a830SBarry Smith   PetscErrorCode    ierr;
2804b5b1a830SBarry Smith   TSMatlabContext *sctx;
2805b5b1a830SBarry Smith 
2806b5b1a830SBarry Smith   PetscFunctionBegin;
2807b5b1a830SBarry Smith   /* currently sctx is memory bleed */
2808b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2809b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2810b5b1a830SBarry Smith   /*
2811b5b1a830SBarry Smith      This should work, but it doesn't
2812b5b1a830SBarry Smith   sctx->ctx = ctx;
2813b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2814b5b1a830SBarry Smith   */
2815b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2816cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
2817b5b1a830SBarry Smith   PetscFunctionReturn(0);
2818b5b1a830SBarry Smith }
2819b5b1a830SBarry Smith 
2820325fc9f4SBarry Smith #endif
2821