xref: /petsc/src/ts/interface/ts.c (revision 0da9a4f0a449d38ef004cd40281e9c355707fca0)
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);
83a43b19c4SJed Brown     /* Handle TS type options */
84a43b19c4SJed Brown     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
85bdad233fSMatthew Knepley 
86bdad233fSMatthew Knepley     /* Handle generic TS options */
87bdad233fSMatthew Knepley     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
88bdad233fSMatthew Knepley     ierr = PetscOptionsReal("-ts_max_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
89bdad233fSMatthew Knepley     ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetInitialTime", ts->ptime, &ts->ptime, PETSC_NULL);CHKERRQ(ierr);
90bdad233fSMatthew Knepley     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetInitialTimeStep",ts->initial_time_step,&dt,&opt);CHKERRQ(ierr);
913daf2de8SJed Brown     if (opt) {ierr = TSSetInitialTimeStep(ts,ts->ptime,dt);CHKERRQ(ierr);}
92a43b19c4SJed Brown     opt = ts->exact_final_time == PETSC_DECIDE ? PETSC_FALSE : (PetscBool)ts->exact_final_time;
93a43b19c4SJed Brown     ierr = PetscOptionsBool("-ts_exact_final_time","Interpolate output to stop exactly at the final time","TSSetExactFinalTime",opt,&opt,&flg);CHKERRQ(ierr);
94a43b19c4SJed Brown     if (flg) {ierr = TSSetExactFinalTime(ts,flg);CHKERRQ(ierr);}
95193ac0bcSJed 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);
96193ac0bcSJed Brown     ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections","",ts->max_reject,&ts->max_reject,PETSC_NULL);CHKERRQ(ierr);
97193ac0bcSJed Brown     ierr = PetscOptionsBool("-ts_error_if_step_failed","Error if no step succeeds","",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr);
98bdad233fSMatthew Knepley 
99bdad233fSMatthew Knepley     /* Monitor options */
100a6570f20SBarry Smith     ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
101eabae89aSBarry Smith     if (flg) {
102649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr);
103649052a6SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
104bdad233fSMatthew Knepley     }
1055180491cSLisandro Dalcin     ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1065180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1075180491cSLisandro Dalcin 
10890d69ab7SBarry Smith     opt  = PETSC_FALSE;
109acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
110a7cc72afSBarry Smith     if (opt) {
111a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
112bdad233fSMatthew Knepley     }
11390d69ab7SBarry Smith     opt  = PETSC_FALSE;
114acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
115a7cc72afSBarry Smith     if (opt) {
116a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolution,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
117bdad233fSMatthew Knepley     }
118bdad233fSMatthew Knepley 
119bdad233fSMatthew Knepley     /* Handle specific TS options */
120abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
121bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
122bdad233fSMatthew Knepley     }
1235d973c19SBarry Smith 
1245d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
1255d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
126bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
127bdad233fSMatthew Knepley 
128089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
129bdad233fSMatthew Knepley   /* Handle subobject options */
130089b2837SJed Brown   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
131089b2837SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
132bdad233fSMatthew Knepley   PetscFunctionReturn(0);
133bdad233fSMatthew Knepley }
134bdad233fSMatthew Knepley 
135bdad233fSMatthew Knepley #undef __FUNCT__
136cdcf91faSSean Farley #undef __FUNCT__
1374a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
138a7a1495cSBarry Smith /*@
1398c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
140a7a1495cSBarry Smith       set with TSSetRHSJacobian().
141a7a1495cSBarry Smith 
142a7a1495cSBarry Smith    Collective on TS and Vec
143a7a1495cSBarry Smith 
144a7a1495cSBarry Smith    Input Parameters:
145316643e7SJed Brown +  ts - the TS context
146a7a1495cSBarry Smith .  t - current timestep
147a7a1495cSBarry Smith -  x - input vector
148a7a1495cSBarry Smith 
149a7a1495cSBarry Smith    Output Parameters:
150a7a1495cSBarry Smith +  A - Jacobian matrix
151a7a1495cSBarry Smith .  B - optional preconditioning matrix
152a7a1495cSBarry Smith -  flag - flag indicating matrix structure
153a7a1495cSBarry Smith 
154a7a1495cSBarry Smith    Notes:
155a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
156a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
157a7a1495cSBarry Smith 
15894b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
159a7a1495cSBarry Smith    flag parameter.
160a7a1495cSBarry Smith 
161a7a1495cSBarry Smith    Level: developer
162a7a1495cSBarry Smith 
163a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
164a7a1495cSBarry Smith 
16594b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
166a7a1495cSBarry Smith @*/
1677087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
168a7a1495cSBarry Smith {
169dfbe8321SBarry Smith   PetscErrorCode ierr;
1700e4ef248SJed Brown   PetscInt Xstate;
171a7a1495cSBarry Smith 
172a7a1495cSBarry Smith   PetscFunctionBegin;
1730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1740700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
175c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
1760e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
1770e4ef248SJed Brown   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) {
1780e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
1790e4ef248SJed Brown     PetscFunctionReturn(0);
180f8ede8e7SJed Brown   }
181d90be118SSean Farley 
1823b5f76d0SSean Farley   if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
183d90be118SSean Farley 
1843b5f76d0SSean Farley   if (ts->userops->rhsjacobian) {
185d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
186a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
187a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
1883b5f76d0SSean Farley     ierr = (*ts->userops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr);
189a7a1495cSBarry Smith     PetscStackPop;
190d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
191a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
1920700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
1930700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
194ef66eb69SBarry Smith   } else {
195214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
196214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
197214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
198ef66eb69SBarry Smith   }
1990e4ef248SJed Brown   ts->rhsjacobian.time = t;
2000e4ef248SJed Brown   ts->rhsjacobian.X = X;
2010e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
2020e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
203a7a1495cSBarry Smith   PetscFunctionReturn(0);
204a7a1495cSBarry Smith }
205a7a1495cSBarry Smith 
2064a2ae208SSatish Balay #undef __FUNCT__
2074a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
208316643e7SJed Brown /*@
209d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
210d763cef2SBarry Smith 
211316643e7SJed Brown    Collective on TS and Vec
212316643e7SJed Brown 
213316643e7SJed Brown    Input Parameters:
214316643e7SJed Brown +  ts - the TS context
215316643e7SJed Brown .  t - current time
216316643e7SJed Brown -  x - state vector
217316643e7SJed Brown 
218316643e7SJed Brown    Output Parameter:
219316643e7SJed Brown .  y - right hand side
220316643e7SJed Brown 
221316643e7SJed Brown    Note:
222316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
223316643e7SJed Brown    is used internally within the nonlinear solvers.
224316643e7SJed Brown 
225316643e7SJed Brown    Level: developer
226316643e7SJed Brown 
227316643e7SJed Brown .keywords: TS, compute
228316643e7SJed Brown 
229316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
230316643e7SJed Brown @*/
231dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
232d763cef2SBarry Smith {
233dfbe8321SBarry Smith   PetscErrorCode ierr;
234d763cef2SBarry Smith 
235d763cef2SBarry Smith   PetscFunctionBegin;
2360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2370700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2380700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
239d763cef2SBarry Smith 
2403b5f76d0SSean Farley   if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
241d763cef2SBarry Smith 
242d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
2433b5f76d0SSean Farley   if (ts->userops->rhsfunction) {
244d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
2453b5f76d0SSean Farley     ierr = (*ts->userops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr);
246d763cef2SBarry Smith     PetscStackPop;
247214bc6a2SJed Brown   } else {
248214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
249b2cd27e8SJed Brown   }
25044a41b28SSean Farley 
251d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
252d763cef2SBarry Smith   PetscFunctionReturn(0);
253d763cef2SBarry Smith }
254d763cef2SBarry Smith 
2554a2ae208SSatish Balay #undef __FUNCT__
256214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
257214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
258214bc6a2SJed Brown {
2592dd45cf8SJed Brown   Vec            F;
260214bc6a2SJed Brown   PetscErrorCode ierr;
261214bc6a2SJed Brown 
262214bc6a2SJed Brown   PetscFunctionBegin;
263*0da9a4f0SJed Brown   *Frhs = PETSC_NULL;
2642dd45cf8SJed Brown   ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
265214bc6a2SJed Brown   if (!ts->Frhs) {
2662dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
267214bc6a2SJed Brown   }
268214bc6a2SJed Brown   *Frhs = ts->Frhs;
269214bc6a2SJed Brown   PetscFunctionReturn(0);
270214bc6a2SJed Brown }
271214bc6a2SJed Brown 
272214bc6a2SJed Brown #undef __FUNCT__
273214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
274214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
275214bc6a2SJed Brown {
276214bc6a2SJed Brown   Mat            A,B;
2772dd45cf8SJed Brown   PetscErrorCode ierr;
278214bc6a2SJed Brown 
279214bc6a2SJed Brown   PetscFunctionBegin;
280214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
281214bc6a2SJed Brown   if (Arhs) {
282214bc6a2SJed Brown     if (!ts->Arhs) {
283214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
284214bc6a2SJed Brown     }
285214bc6a2SJed Brown     *Arhs = ts->Arhs;
286214bc6a2SJed Brown   }
287214bc6a2SJed Brown   if (Brhs) {
288214bc6a2SJed Brown     if (!ts->Brhs) {
289214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
290214bc6a2SJed Brown     }
291214bc6a2SJed Brown     *Brhs = ts->Brhs;
292214bc6a2SJed Brown   }
293214bc6a2SJed Brown   PetscFunctionReturn(0);
294214bc6a2SJed Brown }
295214bc6a2SJed Brown 
296214bc6a2SJed Brown #undef __FUNCT__
297316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
298316643e7SJed Brown /*@
299316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
300316643e7SJed Brown 
301316643e7SJed Brown    Collective on TS and Vec
302316643e7SJed Brown 
303316643e7SJed Brown    Input Parameters:
304316643e7SJed Brown +  ts - the TS context
305316643e7SJed Brown .  t - current time
306316643e7SJed Brown .  X - state vector
307214bc6a2SJed Brown .  Xdot - time derivative of state vector
308214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
309316643e7SJed Brown 
310316643e7SJed Brown    Output Parameter:
311316643e7SJed Brown .  Y - right hand side
312316643e7SJed Brown 
313316643e7SJed Brown    Note:
314316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
315316643e7SJed Brown    is used internally within the nonlinear solvers.
316316643e7SJed Brown 
317316643e7SJed Brown    If the user did did not write their equations in implicit form, this
318316643e7SJed Brown    function recasts them in implicit form.
319316643e7SJed Brown 
320316643e7SJed Brown    Level: developer
321316643e7SJed Brown 
322316643e7SJed Brown .keywords: TS, compute
323316643e7SJed Brown 
324316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
325316643e7SJed Brown @*/
326214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex)
327316643e7SJed Brown {
328316643e7SJed Brown   PetscErrorCode ierr;
329316643e7SJed Brown 
330316643e7SJed Brown   PetscFunctionBegin;
3310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3320700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3330700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
3340700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
335316643e7SJed Brown 
336d90be118SSean Farley   if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
337d90be118SSean Farley 
338316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
3393b5f76d0SSean Farley   if (ts->userops->ifunction) {
340316643e7SJed Brown     PetscStackPush("TS user implicit function");
3413b5f76d0SSean Farley     ierr = (*ts->userops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr);
342316643e7SJed Brown     PetscStackPop;
343214bc6a2SJed Brown   }
344214bc6a2SJed Brown   if (imex) {
3453b5f76d0SSean Farley     if (!ts->userops->ifunction) {
3462dd45cf8SJed Brown       ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);
3472dd45cf8SJed Brown     }
3482dd45cf8SJed Brown   } else if (ts->userops->rhsfunction) {
3492dd45cf8SJed Brown     if (ts->userops->ifunction) {
350214bc6a2SJed Brown       Vec Frhs;
351214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
352214bc6a2SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr);
353214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
3542dd45cf8SJed Brown     } else {
3552dd45cf8SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr);
3562dd45cf8SJed Brown       ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr);
357316643e7SJed Brown     }
3584a6899ffSJed Brown   }
359316643e7SJed Brown   ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
360316643e7SJed Brown   PetscFunctionReturn(0);
361316643e7SJed Brown }
362316643e7SJed Brown 
363316643e7SJed Brown #undef __FUNCT__
364316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
365316643e7SJed Brown /*@
366316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
367316643e7SJed Brown 
368316643e7SJed Brown    Collective on TS and Vec
369316643e7SJed Brown 
370316643e7SJed Brown    Input
371316643e7SJed Brown       Input Parameters:
372316643e7SJed Brown +  ts - the TS context
373316643e7SJed Brown .  t - current timestep
374316643e7SJed Brown .  X - state vector
375316643e7SJed Brown .  Xdot - time derivative of state vector
376214bc6a2SJed Brown .  shift - shift to apply, see note below
377214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
378316643e7SJed Brown 
379316643e7SJed Brown    Output Parameters:
380316643e7SJed Brown +  A - Jacobian matrix
381316643e7SJed Brown .  B - optional preconditioning matrix
382316643e7SJed Brown -  flag - flag indicating matrix structure
383316643e7SJed Brown 
384316643e7SJed Brown    Notes:
385316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
386316643e7SJed Brown 
387316643e7SJed Brown    dF/dX + shift*dF/dXdot
388316643e7SJed Brown 
389316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
390316643e7SJed Brown    is used internally within the nonlinear solvers.
391316643e7SJed Brown 
392316643e7SJed Brown    Level: developer
393316643e7SJed Brown 
394316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
395316643e7SJed Brown 
396316643e7SJed Brown .seealso:  TSSetIJacobian()
39763495f91SJed Brown @*/
398214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
399316643e7SJed Brown {
4000026cea9SSean Farley   PetscInt Xstate, Xdotstate;
401316643e7SJed Brown   PetscErrorCode ierr;
402316643e7SJed Brown 
403316643e7SJed Brown   PetscFunctionBegin;
4040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4050700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4060700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
407316643e7SJed Brown   PetscValidPointer(A,6);
4080700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
409316643e7SJed Brown   PetscValidPointer(B,7);
4100700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
411316643e7SJed Brown   PetscValidPointer(flg,8);
4120026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
4130026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr);
4140026cea9SSean 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))) {
4150026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
4160026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4170026cea9SSean Farley     PetscFunctionReturn(0);
4180026cea9SSean Farley   }
419316643e7SJed Brown 
4203b5f76d0SSean Farley   if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
421316643e7SJed Brown 
4224e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
423316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
4243b5f76d0SSean Farley   if (ts->userops->ijacobian) {
4252dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
426316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
4273b5f76d0SSean Farley     ierr = (*ts->userops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr);
428316643e7SJed Brown     PetscStackPop;
429214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
430214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
431214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
4324a6899ffSJed Brown   }
433214bc6a2SJed Brown   if (imex) {
4343b5f76d0SSean Farley     if (!ts->userops->ijacobian) {  /* system was written as Xdot = F(t,X) */
435214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
4362dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
437214bc6a2SJed Brown       if (*A != *B) {
438214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
439214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
440214bc6a2SJed Brown       }
441214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
442214bc6a2SJed Brown     }
443214bc6a2SJed Brown   } else {
4443b5f76d0SSean Farley     if (!ts->userops->ijacobian) {
445214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr);
446214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
447214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
448316643e7SJed Brown       if (*A != *B) {
449316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
450316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
451316643e7SJed Brown       }
4523b5f76d0SSean Farley     } else if (ts->userops->rhsjacobian) {
453214bc6a2SJed Brown       Mat Arhs,Brhs;
454214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
455214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
456214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
457214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
458214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
459214bc6a2SJed Brown       if (*A != *B) {
460214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
461214bc6a2SJed Brown       }
462214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
463214bc6a2SJed Brown     }
464316643e7SJed Brown   }
4650026cea9SSean Farley 
4660026cea9SSean Farley   ts->ijacobian.time = t;
4670026cea9SSean Farley   ts->ijacobian.X = X;
4680026cea9SSean Farley   ts->ijacobian.Xdot = Xdot;
4690026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr);
4700026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
4710026cea9SSean Farley   ts->ijacobian.shift = shift;
4720026cea9SSean Farley   ts->ijacobian.imex = imex;
4730026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
474316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
475316643e7SJed Brown   PetscFunctionReturn(0);
476316643e7SJed Brown }
477316643e7SJed Brown 
478316643e7SJed Brown #undef __FUNCT__
4794a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
480d763cef2SBarry Smith /*@C
481d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
482d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
483d763cef2SBarry Smith 
4843f9fe445SBarry Smith     Logically Collective on TS
485d763cef2SBarry Smith 
486d763cef2SBarry Smith     Input Parameters:
487d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
488ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
489d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
490d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
491d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
492d763cef2SBarry Smith 
493d763cef2SBarry Smith     Calling sequence of func:
49487828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
495d763cef2SBarry Smith 
496d763cef2SBarry Smith +   t - current timestep
497d763cef2SBarry Smith .   u - input vector
498d763cef2SBarry Smith .   F - function vector
499d763cef2SBarry Smith -   ctx - [optional] user-defined function context
500d763cef2SBarry Smith 
501d763cef2SBarry Smith     Important:
50276f2fa84SHong Zhang     The user MUST call either this routine or TSSetMatrices().
503d763cef2SBarry Smith 
504d763cef2SBarry Smith     Level: beginner
505d763cef2SBarry Smith 
506d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
507d763cef2SBarry Smith 
50876f2fa84SHong Zhang .seealso: TSSetMatrices()
509d763cef2SBarry Smith @*/
510089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
511d763cef2SBarry Smith {
512089b2837SJed Brown   PetscErrorCode ierr;
513089b2837SJed Brown   SNES           snes;
514d763cef2SBarry Smith 
515089b2837SJed Brown   PetscFunctionBegin;
5160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
517ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
5183b5f76d0SSean Farley   if (f)   ts->userops->rhsfunction = f;
5194e684422SJed Brown   if (ctx) ts->funP                 = ctx;
520089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
521089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
522d763cef2SBarry Smith   PetscFunctionReturn(0);
523d763cef2SBarry Smith }
524d763cef2SBarry Smith 
5254a2ae208SSatish Balay #undef __FUNCT__
5264a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
527d763cef2SBarry Smith /*@C
528d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
529d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
53076f2fa84SHong Zhang    Use TSSetMatrices() for linear problems.
531d763cef2SBarry Smith 
5323f9fe445SBarry Smith    Logically Collective on TS
533d763cef2SBarry Smith 
534d763cef2SBarry Smith    Input Parameters:
535d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
536d763cef2SBarry Smith .  A   - Jacobian matrix
537d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
538d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
539d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
540d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
541d763cef2SBarry Smith 
542d763cef2SBarry Smith    Calling sequence of func:
54387828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
544d763cef2SBarry Smith 
545d763cef2SBarry Smith +  t - current timestep
546d763cef2SBarry Smith .  u - input vector
547d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
548d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
549d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
55094b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
551d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
552d763cef2SBarry Smith 
553d763cef2SBarry Smith    Notes:
55494b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
555d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
556d763cef2SBarry Smith 
557d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
558d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
55956335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
560d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
561d763cef2SBarry Smith    throughout the global iterations.
562d763cef2SBarry Smith 
563d763cef2SBarry Smith    Level: beginner
564d763cef2SBarry Smith 
565d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
566d763cef2SBarry Smith 
567d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(),
56876f2fa84SHong Zhang           SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices()
569d763cef2SBarry Smith 
570d763cef2SBarry Smith @*/
571089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
572d763cef2SBarry Smith {
573277b19d0SLisandro Dalcin   PetscErrorCode ierr;
574089b2837SJed Brown   SNES           snes;
575277b19d0SLisandro Dalcin 
576d763cef2SBarry Smith   PetscFunctionBegin;
5770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
578277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
579277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
580277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
581277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
582d763cef2SBarry Smith 
5833b5f76d0SSean Farley   if (f)   ts->userops->rhsjacobian = f;
584277b19d0SLisandro Dalcin   if (ctx) ts->jacP                 = ctx;
585089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
586dabe61deSJed Brown   if (!ts->userops->ijacobian) {
587089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
5880e4ef248SJed Brown   }
5890e4ef248SJed Brown   if (A) {
5900e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
5910e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
5920e4ef248SJed Brown     ts->Arhs = A;
5930e4ef248SJed Brown   }
5940e4ef248SJed Brown   if (B) {
5950e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
5960e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
5970e4ef248SJed Brown     ts->Brhs = B;
5980e4ef248SJed Brown   }
599d763cef2SBarry Smith   PetscFunctionReturn(0);
600d763cef2SBarry Smith }
601d763cef2SBarry Smith 
602316643e7SJed Brown 
603316643e7SJed Brown #undef __FUNCT__
604316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
605316643e7SJed Brown /*@C
606316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
607316643e7SJed Brown 
6083f9fe445SBarry Smith    Logically Collective on TS
609316643e7SJed Brown 
610316643e7SJed Brown    Input Parameters:
611316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
612ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
613316643e7SJed Brown .  f   - the function evaluation routine
614316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
615316643e7SJed Brown 
616316643e7SJed Brown    Calling sequence of f:
617316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
618316643e7SJed Brown 
619316643e7SJed Brown +  t   - time at step/stage being solved
620316643e7SJed Brown .  u   - state vector
621316643e7SJed Brown .  u_t - time derivative of state vector
622316643e7SJed Brown .  F   - function vector
623316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
624316643e7SJed Brown 
625316643e7SJed Brown    Important:
626316643e7SJed Brown    The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices().  This routine must be used when not solving an ODE.
627316643e7SJed Brown 
628316643e7SJed Brown    Level: beginner
629316643e7SJed Brown 
630316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
631316643e7SJed Brown 
632316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian()
633316643e7SJed Brown @*/
634089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
635316643e7SJed Brown {
636089b2837SJed Brown   PetscErrorCode ierr;
637089b2837SJed Brown   SNES           snes;
638316643e7SJed Brown 
639316643e7SJed Brown   PetscFunctionBegin;
6400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
641ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
6423b5f76d0SSean Farley   if (f)   ts->userops->ifunction = f;
643089b2837SJed Brown   if (ctx) ts->funP           = ctx;
644089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
645089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
646089b2837SJed Brown   PetscFunctionReturn(0);
647089b2837SJed Brown }
648089b2837SJed Brown 
649089b2837SJed Brown #undef __FUNCT__
650089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
651089b2837SJed Brown /*@C
652089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
653089b2837SJed Brown 
654089b2837SJed Brown    Not Collective
655089b2837SJed Brown 
656089b2837SJed Brown    Input Parameter:
657089b2837SJed Brown .  ts - the TS context
658089b2837SJed Brown 
659089b2837SJed Brown    Output Parameter:
660089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
661089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
662089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
663089b2837SJed Brown 
664089b2837SJed Brown    Level: advanced
665089b2837SJed Brown 
666089b2837SJed Brown .keywords: TS, nonlinear, get, function
667089b2837SJed Brown 
668089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
669089b2837SJed Brown @*/
670089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
671089b2837SJed Brown {
672089b2837SJed Brown   PetscErrorCode ierr;
673089b2837SJed Brown   SNES snes;
674089b2837SJed Brown 
675089b2837SJed Brown   PetscFunctionBegin;
676089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
677089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
678089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
6793b5f76d0SSean Farley   if (func) *func = ts->userops->ifunction;
680089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
681089b2837SJed Brown   PetscFunctionReturn(0);
682089b2837SJed Brown }
683089b2837SJed Brown 
684089b2837SJed Brown #undef __FUNCT__
685089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
686089b2837SJed Brown /*@C
687089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
688089b2837SJed Brown 
689089b2837SJed Brown    Not Collective
690089b2837SJed Brown 
691089b2837SJed Brown    Input Parameter:
692089b2837SJed Brown .  ts - the TS context
693089b2837SJed Brown 
694089b2837SJed Brown    Output Parameter:
695089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
696089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
697089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
698089b2837SJed Brown 
699089b2837SJed Brown    Level: advanced
700089b2837SJed Brown 
701089b2837SJed Brown .keywords: TS, nonlinear, get, function
702089b2837SJed Brown 
703089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
704089b2837SJed Brown @*/
705089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
706089b2837SJed Brown {
707089b2837SJed Brown   PetscErrorCode ierr;
708089b2837SJed Brown   SNES snes;
709089b2837SJed Brown 
710089b2837SJed Brown   PetscFunctionBegin;
711089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
712089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
713089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7143b5f76d0SSean Farley   if (func) *func = ts->userops->rhsfunction;
715089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
716316643e7SJed Brown   PetscFunctionReturn(0);
717316643e7SJed Brown }
718316643e7SJed Brown 
719316643e7SJed Brown #undef __FUNCT__
720316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
721316643e7SJed Brown /*@C
722a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
723a4f0a591SBarry Smith         you provided with TSSetIFunction().
724316643e7SJed Brown 
7253f9fe445SBarry Smith    Logically Collective on TS
726316643e7SJed Brown 
727316643e7SJed Brown    Input Parameters:
728316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
729316643e7SJed Brown .  A   - Jacobian matrix
730316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
731316643e7SJed Brown .  f   - the Jacobian evaluation routine
732316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
733316643e7SJed Brown 
734316643e7SJed Brown    Calling sequence of f:
7351b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
736316643e7SJed Brown 
737316643e7SJed Brown +  t    - time at step/stage being solved
7381b4a444bSJed Brown .  U    - state vector
7391b4a444bSJed Brown .  U_t  - time derivative of state vector
740316643e7SJed Brown .  a    - shift
7411b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
742316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
743316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
744316643e7SJed Brown           structure (same as flag in KSPSetOperators())
745316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
746316643e7SJed Brown 
747316643e7SJed Brown    Notes:
748316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
749316643e7SJed Brown 
750a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
751a4f0a591SBarry 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.
752a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
753a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
754a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
755a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
756a4f0a591SBarry Smith 
757316643e7SJed Brown    Level: beginner
758316643e7SJed Brown 
759316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
760316643e7SJed Brown 
761316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian()
762316643e7SJed Brown 
763316643e7SJed Brown @*/
7647087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
765316643e7SJed Brown {
766316643e7SJed Brown   PetscErrorCode ierr;
767089b2837SJed Brown   SNES           snes;
768316643e7SJed Brown 
769316643e7SJed Brown   PetscFunctionBegin;
7700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7710700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
7720700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
773316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
774316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
7753b5f76d0SSean Farley   if (f)   ts->userops->ijacobian = f;
776316643e7SJed Brown   if (ctx) ts->jacP           = ctx;
777089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
778089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
779316643e7SJed Brown   PetscFunctionReturn(0);
780316643e7SJed Brown }
781316643e7SJed Brown 
7824a2ae208SSatish Balay #undef __FUNCT__
7834a2ae208SSatish Balay #define __FUNCT__ "TSView"
7847e2c5f70SBarry Smith /*@C
785d763cef2SBarry Smith     TSView - Prints the TS data structure.
786d763cef2SBarry Smith 
7874c49b128SBarry Smith     Collective on TS
788d763cef2SBarry Smith 
789d763cef2SBarry Smith     Input Parameters:
790d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
791d763cef2SBarry Smith -   viewer - visualization context
792d763cef2SBarry Smith 
793d763cef2SBarry Smith     Options Database Key:
794d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
795d763cef2SBarry Smith 
796d763cef2SBarry Smith     Notes:
797d763cef2SBarry Smith     The available visualization contexts include
798b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
799b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
800d763cef2SBarry Smith          output where only the first processor opens
801d763cef2SBarry Smith          the file.  All other processors send their
802d763cef2SBarry Smith          data to the first processor to print.
803d763cef2SBarry Smith 
804d763cef2SBarry Smith     The user can open an alternative visualization context with
805b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
806d763cef2SBarry Smith 
807d763cef2SBarry Smith     Level: beginner
808d763cef2SBarry Smith 
809d763cef2SBarry Smith .keywords: TS, timestep, view
810d763cef2SBarry Smith 
811b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
812d763cef2SBarry Smith @*/
8137087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
814d763cef2SBarry Smith {
815dfbe8321SBarry Smith   PetscErrorCode ierr;
816a313700dSBarry Smith   const TSType   type;
817089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
818d763cef2SBarry Smith 
819d763cef2SBarry Smith   PetscFunctionBegin;
8200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8213050cee2SBarry Smith   if (!viewer) {
8227adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
8233050cee2SBarry Smith   }
8240700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
825c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
826fd16b177SBarry Smith 
8272692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
8282692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
82932077d6dSBarry Smith   if (iascii) {
830317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
831000e7ae3SMatthew Knepley     if (ts->ops->view) {
832b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
833000e7ae3SMatthew Knepley       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
834b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
835d763cef2SBarry Smith     }
83677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
837a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
838d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
83977431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr);
840193ac0bcSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->max_snes_failures);CHKERRQ(ierr);
841d763cef2SBarry Smith     }
84277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr);
843193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
8440f5bd95cSBarry Smith   } else if (isstring) {
845a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
846b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
847d763cef2SBarry Smith   }
848b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
849089b2837SJed Brown   ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
850089b2837SJed Brown   if (!isundials && ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
851b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
852d763cef2SBarry Smith   PetscFunctionReturn(0);
853d763cef2SBarry Smith }
854d763cef2SBarry Smith 
855d763cef2SBarry Smith 
8564a2ae208SSatish Balay #undef __FUNCT__
8574a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
858b07ff414SBarry Smith /*@
859d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
860d763cef2SBarry Smith    the timesteppers.
861d763cef2SBarry Smith 
8623f9fe445SBarry Smith    Logically Collective on TS
863d763cef2SBarry Smith 
864d763cef2SBarry Smith    Input Parameters:
865d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
866d763cef2SBarry Smith -  usrP - optional user context
867d763cef2SBarry Smith 
868d763cef2SBarry Smith    Level: intermediate
869d763cef2SBarry Smith 
870d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
871d763cef2SBarry Smith 
872d763cef2SBarry Smith .seealso: TSGetApplicationContext()
873d763cef2SBarry Smith @*/
8747087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
875d763cef2SBarry Smith {
876d763cef2SBarry Smith   PetscFunctionBegin;
8770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
878d763cef2SBarry Smith   ts->user = usrP;
879d763cef2SBarry Smith   PetscFunctionReturn(0);
880d763cef2SBarry Smith }
881d763cef2SBarry Smith 
8824a2ae208SSatish Balay #undef __FUNCT__
8834a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
884b07ff414SBarry Smith /*@
885d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
886d763cef2SBarry Smith     timestepper.
887d763cef2SBarry Smith 
888d763cef2SBarry Smith     Not Collective
889d763cef2SBarry Smith 
890d763cef2SBarry Smith     Input Parameter:
891d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
892d763cef2SBarry Smith 
893d763cef2SBarry Smith     Output Parameter:
894d763cef2SBarry Smith .   usrP - user context
895d763cef2SBarry Smith 
896d763cef2SBarry Smith     Level: intermediate
897d763cef2SBarry Smith 
898d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
899d763cef2SBarry Smith 
900d763cef2SBarry Smith .seealso: TSSetApplicationContext()
901d763cef2SBarry Smith @*/
902e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
903d763cef2SBarry Smith {
904d763cef2SBarry Smith   PetscFunctionBegin;
9050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
906e71120c6SJed Brown   *(void**)usrP = ts->user;
907d763cef2SBarry Smith   PetscFunctionReturn(0);
908d763cef2SBarry Smith }
909d763cef2SBarry Smith 
9104a2ae208SSatish Balay #undef __FUNCT__
9114a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
912d763cef2SBarry Smith /*@
913d763cef2SBarry Smith    TSGetTimeStepNumber - Gets the current number of timesteps.
914d763cef2SBarry Smith 
915d763cef2SBarry Smith    Not Collective
916d763cef2SBarry Smith 
917d763cef2SBarry Smith    Input Parameter:
918d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
919d763cef2SBarry Smith 
920d763cef2SBarry Smith    Output Parameter:
921d763cef2SBarry Smith .  iter - number steps so far
922d763cef2SBarry Smith 
923d763cef2SBarry Smith    Level: intermediate
924d763cef2SBarry Smith 
925d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
926d763cef2SBarry Smith @*/
9277087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
928d763cef2SBarry Smith {
929d763cef2SBarry Smith   PetscFunctionBegin;
9300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9314482741eSBarry Smith   PetscValidIntPointer(iter,2);
932d763cef2SBarry Smith   *iter = ts->steps;
933d763cef2SBarry Smith   PetscFunctionReturn(0);
934d763cef2SBarry Smith }
935d763cef2SBarry Smith 
9364a2ae208SSatish Balay #undef __FUNCT__
9374a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
938d763cef2SBarry Smith /*@
939d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
940d763cef2SBarry Smith    as well as the initial time.
941d763cef2SBarry Smith 
9423f9fe445SBarry Smith    Logically Collective on TS
943d763cef2SBarry Smith 
944d763cef2SBarry Smith    Input Parameters:
945d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
946d763cef2SBarry Smith .  initial_time - the initial time
947d763cef2SBarry Smith -  time_step - the size of the timestep
948d763cef2SBarry Smith 
949d763cef2SBarry Smith    Level: intermediate
950d763cef2SBarry Smith 
951d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
952d763cef2SBarry Smith 
953d763cef2SBarry Smith .keywords: TS, set, initial, timestep
954d763cef2SBarry Smith @*/
9557087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
956d763cef2SBarry Smith {
957e144a568SJed Brown   PetscErrorCode ierr;
958e144a568SJed Brown 
959d763cef2SBarry Smith   PetscFunctionBegin;
9600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
961e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
962d763cef2SBarry Smith   ts->initial_time_step = time_step;
963d763cef2SBarry Smith   ts->ptime             = initial_time;
964d763cef2SBarry Smith   PetscFunctionReturn(0);
965d763cef2SBarry Smith }
966d763cef2SBarry Smith 
9674a2ae208SSatish Balay #undef __FUNCT__
9684a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
969d763cef2SBarry Smith /*@
970d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
971d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
972d763cef2SBarry Smith 
9733f9fe445SBarry Smith    Logically Collective on TS
974d763cef2SBarry Smith 
975d763cef2SBarry Smith    Input Parameters:
976d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
977d763cef2SBarry Smith -  time_step - the size of the timestep
978d763cef2SBarry Smith 
979d763cef2SBarry Smith    Level: intermediate
980d763cef2SBarry Smith 
981d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
982d763cef2SBarry Smith 
983d763cef2SBarry Smith .keywords: TS, set, timestep
984d763cef2SBarry Smith @*/
9857087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
986d763cef2SBarry Smith {
987d763cef2SBarry Smith   PetscFunctionBegin;
9880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
989c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
990d763cef2SBarry Smith   ts->time_step      = time_step;
991193ac0bcSJed Brown   ts->next_time_step = time_step;
992d763cef2SBarry Smith   PetscFunctionReturn(0);
993d763cef2SBarry Smith }
994d763cef2SBarry Smith 
9954a2ae208SSatish Balay #undef __FUNCT__
996a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
997a43b19c4SJed Brown /*@
998a43b19c4SJed Brown    TSSetExactFinalTime - Determines whether to interpolate solution to the
999a43b19c4SJed Brown       exact final time requested by the user or just returns it at the final time
1000a43b19c4SJed Brown       it computed.
1001a43b19c4SJed Brown 
1002a43b19c4SJed Brown   Logically Collective on TS
1003a43b19c4SJed Brown 
1004a43b19c4SJed Brown    Input Parameter:
1005a43b19c4SJed Brown +   ts - the time-step context
1006a43b19c4SJed Brown -   ft - PETSC_TRUE if interpolates, else PETSC_FALSE
1007a43b19c4SJed Brown 
1008a43b19c4SJed Brown    Level: beginner
1009a43b19c4SJed Brown 
1010a43b19c4SJed Brown .seealso: TSSetDuration()
1011a43b19c4SJed Brown @*/
1012a43b19c4SJed Brown PetscErrorCode  TSSetExactFinalTime(TS ts,PetscBool flg)
1013a43b19c4SJed Brown {
1014a43b19c4SJed Brown 
1015a43b19c4SJed Brown   PetscFunctionBegin;
1016a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1017a43b19c4SJed Brown   ts->exact_final_time = flg;
1018a43b19c4SJed Brown   PetscFunctionReturn(0);
1019a43b19c4SJed Brown }
1020a43b19c4SJed Brown 
1021a43b19c4SJed Brown #undef __FUNCT__
10224a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1023d763cef2SBarry Smith /*@
1024d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1025d763cef2SBarry Smith 
1026d763cef2SBarry Smith    Not Collective
1027d763cef2SBarry Smith 
1028d763cef2SBarry Smith    Input Parameter:
1029d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1030d763cef2SBarry Smith 
1031d763cef2SBarry Smith    Output Parameter:
1032d763cef2SBarry Smith .  dt - the current timestep size
1033d763cef2SBarry Smith 
1034d763cef2SBarry Smith    Level: intermediate
1035d763cef2SBarry Smith 
1036d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1037d763cef2SBarry Smith 
1038d763cef2SBarry Smith .keywords: TS, get, timestep
1039d763cef2SBarry Smith @*/
10407087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1041d763cef2SBarry Smith {
1042d763cef2SBarry Smith   PetscFunctionBegin;
10430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10444482741eSBarry Smith   PetscValidDoublePointer(dt,2);
1045d763cef2SBarry Smith   *dt = ts->time_step;
1046d763cef2SBarry Smith   PetscFunctionReturn(0);
1047d763cef2SBarry Smith }
1048d763cef2SBarry Smith 
10494a2ae208SSatish Balay #undef __FUNCT__
10504a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1051d8e5e3e6SSatish Balay /*@
1052d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1053d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1054d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1055d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1056d763cef2SBarry Smith 
1057d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1058d763cef2SBarry Smith 
1059d763cef2SBarry Smith    Input Parameter:
1060d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1061d763cef2SBarry Smith 
1062d763cef2SBarry Smith    Output Parameter:
1063d763cef2SBarry Smith .  v - the vector containing the solution
1064d763cef2SBarry Smith 
1065d763cef2SBarry Smith    Level: intermediate
1066d763cef2SBarry Smith 
1067d763cef2SBarry Smith .seealso: TSGetTimeStep()
1068d763cef2SBarry Smith 
1069d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1070d763cef2SBarry Smith @*/
10717087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1072d763cef2SBarry Smith {
1073d763cef2SBarry Smith   PetscFunctionBegin;
10740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10754482741eSBarry Smith   PetscValidPointer(v,2);
10768737fe31SLisandro Dalcin   *v = ts->vec_sol;
1077d763cef2SBarry Smith   PetscFunctionReturn(0);
1078d763cef2SBarry Smith }
1079d763cef2SBarry Smith 
1080bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
10814a2ae208SSatish Balay #undef __FUNCT__
1082bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1083d8e5e3e6SSatish Balay /*@
1084bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1085d763cef2SBarry Smith 
1086bdad233fSMatthew Knepley   Not collective
1087d763cef2SBarry Smith 
1088bdad233fSMatthew Knepley   Input Parameters:
1089bdad233fSMatthew Knepley + ts   - The TS
1090bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1091d763cef2SBarry Smith .vb
1092d763cef2SBarry Smith          U_t = A U
1093d763cef2SBarry Smith          U_t = A(t) U
1094d763cef2SBarry Smith          U_t = F(t,U)
1095d763cef2SBarry Smith .ve
1096d763cef2SBarry Smith 
1097d763cef2SBarry Smith    Level: beginner
1098d763cef2SBarry Smith 
1099bdad233fSMatthew Knepley .keywords: TS, problem type
1100bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1101d763cef2SBarry Smith @*/
11027087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1103a7cc72afSBarry Smith {
11049e2a6581SJed Brown   PetscErrorCode ierr;
11059e2a6581SJed Brown 
1106d763cef2SBarry Smith   PetscFunctionBegin;
11070700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1108bdad233fSMatthew Knepley   ts->problem_type = type;
11099e2a6581SJed Brown   if (type == TS_LINEAR) {
11109e2a6581SJed Brown     SNES snes;
11119e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11129e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
11139e2a6581SJed Brown   }
1114d763cef2SBarry Smith   PetscFunctionReturn(0);
1115d763cef2SBarry Smith }
1116d763cef2SBarry Smith 
1117bdad233fSMatthew Knepley #undef __FUNCT__
1118bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1119bdad233fSMatthew Knepley /*@C
1120bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1121bdad233fSMatthew Knepley 
1122bdad233fSMatthew Knepley   Not collective
1123bdad233fSMatthew Knepley 
1124bdad233fSMatthew Knepley   Input Parameter:
1125bdad233fSMatthew Knepley . ts   - The TS
1126bdad233fSMatthew Knepley 
1127bdad233fSMatthew Knepley   Output Parameter:
1128bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1129bdad233fSMatthew Knepley .vb
1130089b2837SJed Brown          M U_t = A U
1131089b2837SJed Brown          M(t) U_t = A(t) U
1132bdad233fSMatthew Knepley          U_t = F(t,U)
1133bdad233fSMatthew Knepley .ve
1134bdad233fSMatthew Knepley 
1135bdad233fSMatthew Knepley    Level: beginner
1136bdad233fSMatthew Knepley 
1137bdad233fSMatthew Knepley .keywords: TS, problem type
1138bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1139bdad233fSMatthew Knepley @*/
11407087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1141a7cc72afSBarry Smith {
1142bdad233fSMatthew Knepley   PetscFunctionBegin;
11430700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
11444482741eSBarry Smith   PetscValidIntPointer(type,2);
1145bdad233fSMatthew Knepley   *type = ts->problem_type;
1146bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1147bdad233fSMatthew Knepley }
1148d763cef2SBarry Smith 
11494a2ae208SSatish Balay #undef __FUNCT__
11504a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1151d763cef2SBarry Smith /*@
1152d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1153d763cef2SBarry Smith    of a timestepper.
1154d763cef2SBarry Smith 
1155d763cef2SBarry Smith    Collective on TS
1156d763cef2SBarry Smith 
1157d763cef2SBarry Smith    Input Parameter:
1158d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1159d763cef2SBarry Smith 
1160d763cef2SBarry Smith    Notes:
1161d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1162d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1163d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1164d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1165d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1166d763cef2SBarry Smith 
1167d763cef2SBarry Smith    Level: advanced
1168d763cef2SBarry Smith 
1169d763cef2SBarry Smith .keywords: TS, timestep, setup
1170d763cef2SBarry Smith 
1171d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1172d763cef2SBarry Smith @*/
11737087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1174d763cef2SBarry Smith {
1175dfbe8321SBarry Smith   PetscErrorCode ierr;
1176d763cef2SBarry Smith 
1177d763cef2SBarry Smith   PetscFunctionBegin;
11780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1179277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1180277b19d0SLisandro Dalcin 
11817adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
11829596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1183d763cef2SBarry Smith   }
1184a43b19c4SJed Brown   if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE;
1185277b19d0SLisandro Dalcin 
1186277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1187277b19d0SLisandro Dalcin 
1188277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1189000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1190277b19d0SLisandro Dalcin   }
1191277b19d0SLisandro Dalcin 
1192277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1193277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1194277b19d0SLisandro Dalcin }
1195277b19d0SLisandro Dalcin 
1196277b19d0SLisandro Dalcin #undef __FUNCT__
1197277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1198277b19d0SLisandro Dalcin /*@
1199277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1200277b19d0SLisandro Dalcin 
1201277b19d0SLisandro Dalcin    Collective on TS
1202277b19d0SLisandro Dalcin 
1203277b19d0SLisandro Dalcin    Input Parameter:
1204277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1205277b19d0SLisandro Dalcin 
1206277b19d0SLisandro Dalcin    Level: beginner
1207277b19d0SLisandro Dalcin 
1208277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1209277b19d0SLisandro Dalcin 
1210277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1211277b19d0SLisandro Dalcin @*/
1212277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1213277b19d0SLisandro Dalcin {
1214277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1215277b19d0SLisandro Dalcin 
1216277b19d0SLisandro Dalcin   PetscFunctionBegin;
1217277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1218277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1219277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1220277b19d0SLisandro Dalcin   }
1221277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
12224e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
12234e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1224214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
12256bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
122638637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1227277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1228d763cef2SBarry Smith   PetscFunctionReturn(0);
1229d763cef2SBarry Smith }
1230d763cef2SBarry Smith 
12314a2ae208SSatish Balay #undef __FUNCT__
12324a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1233d8e5e3e6SSatish Balay /*@
1234d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1235d763cef2SBarry Smith    with TSCreate().
1236d763cef2SBarry Smith 
1237d763cef2SBarry Smith    Collective on TS
1238d763cef2SBarry Smith 
1239d763cef2SBarry Smith    Input Parameter:
1240d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1241d763cef2SBarry Smith 
1242d763cef2SBarry Smith    Level: beginner
1243d763cef2SBarry Smith 
1244d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1245d763cef2SBarry Smith 
1246d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1247d763cef2SBarry Smith @*/
12486bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1249d763cef2SBarry Smith {
12506849ba73SBarry Smith   PetscErrorCode ierr;
1251d763cef2SBarry Smith 
1252d763cef2SBarry Smith   PetscFunctionBegin;
12536bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
12546bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
12556bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1256d763cef2SBarry Smith 
12576bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1258277b19d0SLisandro Dalcin 
1259be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
12606bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
12616bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
12626d4c513bSLisandro Dalcin 
12636bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
12646bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
12656bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
12666d4c513bSLisandro Dalcin 
12673b5f76d0SSean Farley   ierr = PetscFree((*ts)->userops);
12683b5f76d0SSean Farley 
1269a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1270d763cef2SBarry Smith   PetscFunctionReturn(0);
1271d763cef2SBarry Smith }
1272d763cef2SBarry Smith 
12734a2ae208SSatish Balay #undef __FUNCT__
12744a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1275d8e5e3e6SSatish Balay /*@
1276d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1277d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1278d763cef2SBarry Smith 
1279d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1280d763cef2SBarry Smith 
1281d763cef2SBarry Smith    Input Parameter:
1282d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1283d763cef2SBarry Smith 
1284d763cef2SBarry Smith    Output Parameter:
1285d763cef2SBarry Smith .  snes - the nonlinear solver context
1286d763cef2SBarry Smith 
1287d763cef2SBarry Smith    Notes:
1288d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1289d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
129094b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1291d763cef2SBarry Smith 
1292d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1293d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1294d763cef2SBarry Smith 
1295d763cef2SBarry Smith    Level: beginner
1296d763cef2SBarry Smith 
1297d763cef2SBarry Smith .keywords: timestep, get, SNES
1298d763cef2SBarry Smith @*/
12997087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1300d763cef2SBarry Smith {
1301d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1302d372ba47SLisandro Dalcin 
1303d763cef2SBarry Smith   PetscFunctionBegin;
13040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13054482741eSBarry Smith   PetscValidPointer(snes,2);
1306d372ba47SLisandro Dalcin   if (!ts->snes) {
1307d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
1308d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1309d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
13109e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
13119e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
13129e2a6581SJed Brown     }
1313d372ba47SLisandro Dalcin   }
1314d763cef2SBarry Smith   *snes = ts->snes;
1315d763cef2SBarry Smith   PetscFunctionReturn(0);
1316d763cef2SBarry Smith }
1317d763cef2SBarry Smith 
13184a2ae208SSatish Balay #undef __FUNCT__
131994b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1320d8e5e3e6SSatish Balay /*@
132194b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1322d763cef2SBarry Smith    a TS (timestepper) context.
1323d763cef2SBarry Smith 
132494b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1325d763cef2SBarry Smith 
1326d763cef2SBarry Smith    Input Parameter:
1327d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1328d763cef2SBarry Smith 
1329d763cef2SBarry Smith    Output Parameter:
133094b7f48cSBarry Smith .  ksp - the nonlinear solver context
1331d763cef2SBarry Smith 
1332d763cef2SBarry Smith    Notes:
133394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1334d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1335d763cef2SBarry Smith    KSP and PC contexts as well.
1336d763cef2SBarry Smith 
133794b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
133894b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1339d763cef2SBarry Smith 
1340d763cef2SBarry Smith    Level: beginner
1341d763cef2SBarry Smith 
134294b7f48cSBarry Smith .keywords: timestep, get, KSP
1343d763cef2SBarry Smith @*/
13447087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1345d763cef2SBarry Smith {
1346d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1347089b2837SJed Brown   SNES           snes;
1348d372ba47SLisandro Dalcin 
1349d763cef2SBarry Smith   PetscFunctionBegin;
13500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13514482741eSBarry Smith   PetscValidPointer(ksp,2);
135217186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1353e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1354089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1355089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1356d763cef2SBarry Smith   PetscFunctionReturn(0);
1357d763cef2SBarry Smith }
1358d763cef2SBarry Smith 
1359d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1360d763cef2SBarry Smith 
13614a2ae208SSatish Balay #undef __FUNCT__
1362adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1363adb62b0dSMatthew Knepley /*@
1364adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1365adb62b0dSMatthew Knepley    maximum time for iteration.
1366adb62b0dSMatthew Knepley 
13673f9fe445SBarry Smith    Not Collective
1368adb62b0dSMatthew Knepley 
1369adb62b0dSMatthew Knepley    Input Parameters:
1370adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1371adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1372adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1373adb62b0dSMatthew Knepley 
1374adb62b0dSMatthew Knepley    Level: intermediate
1375adb62b0dSMatthew Knepley 
1376adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1377adb62b0dSMatthew Knepley @*/
13787087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1379adb62b0dSMatthew Knepley {
1380adb62b0dSMatthew Knepley   PetscFunctionBegin;
13810700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1382abc0a331SBarry Smith   if (maxsteps) {
13834482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1384adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1385adb62b0dSMatthew Knepley   }
1386abc0a331SBarry Smith   if (maxtime ) {
13874482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1388adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1389adb62b0dSMatthew Knepley   }
1390adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1391adb62b0dSMatthew Knepley }
1392adb62b0dSMatthew Knepley 
1393adb62b0dSMatthew Knepley #undef __FUNCT__
13944a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1395d763cef2SBarry Smith /*@
1396d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1397d763cef2SBarry Smith    maximum time for iteration.
1398d763cef2SBarry Smith 
13993f9fe445SBarry Smith    Logically Collective on TS
1400d763cef2SBarry Smith 
1401d763cef2SBarry Smith    Input Parameters:
1402d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1403d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1404d763cef2SBarry Smith -  maxtime - final time to iterate to
1405d763cef2SBarry Smith 
1406d763cef2SBarry Smith    Options Database Keys:
1407d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
1408d763cef2SBarry Smith .  -ts_max_time <maxtime> - Sets maxtime
1409d763cef2SBarry Smith 
1410d763cef2SBarry Smith    Notes:
1411d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1412d763cef2SBarry Smith 
1413d763cef2SBarry Smith    Level: intermediate
1414d763cef2SBarry Smith 
1415d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1416a43b19c4SJed Brown 
1417a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1418d763cef2SBarry Smith @*/
14197087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1420d763cef2SBarry Smith {
1421d763cef2SBarry Smith   PetscFunctionBegin;
14220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1423c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1424c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
142539b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
142639b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1427d763cef2SBarry Smith   PetscFunctionReturn(0);
1428d763cef2SBarry Smith }
1429d763cef2SBarry Smith 
14304a2ae208SSatish Balay #undef __FUNCT__
14314a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1432d763cef2SBarry Smith /*@
1433d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1434d763cef2SBarry Smith    for use by the TS routines.
1435d763cef2SBarry Smith 
14363f9fe445SBarry Smith    Logically Collective on TS and Vec
1437d763cef2SBarry Smith 
1438d763cef2SBarry Smith    Input Parameters:
1439d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1440d763cef2SBarry Smith -  x - the solution vector
1441d763cef2SBarry Smith 
1442d763cef2SBarry Smith    Level: beginner
1443d763cef2SBarry Smith 
1444d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1445d763cef2SBarry Smith @*/
14467087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1447d763cef2SBarry Smith {
14488737fe31SLisandro Dalcin   PetscErrorCode ierr;
14498737fe31SLisandro Dalcin 
1450d763cef2SBarry Smith   PetscFunctionBegin;
14510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14520700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
14538737fe31SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
14546bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
14558737fe31SLisandro Dalcin   ts->vec_sol = x;
1456d763cef2SBarry Smith   PetscFunctionReturn(0);
1457d763cef2SBarry Smith }
1458d763cef2SBarry Smith 
1459e74ef692SMatthew Knepley #undef __FUNCT__
1460e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1461ac226902SBarry Smith /*@C
1462000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
14633f2090d5SJed Brown   called once at the beginning of each time step.
1464000e7ae3SMatthew Knepley 
14653f9fe445SBarry Smith   Logically Collective on TS
1466000e7ae3SMatthew Knepley 
1467000e7ae3SMatthew Knepley   Input Parameters:
1468000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1469000e7ae3SMatthew Knepley - func - The function
1470000e7ae3SMatthew Knepley 
1471000e7ae3SMatthew Knepley   Calling sequence of func:
1472000e7ae3SMatthew Knepley . func (TS ts);
1473000e7ae3SMatthew Knepley 
1474000e7ae3SMatthew Knepley   Level: intermediate
1475000e7ae3SMatthew Knepley 
1476000e7ae3SMatthew Knepley .keywords: TS, timestep
1477000e7ae3SMatthew Knepley @*/
14787087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1479000e7ae3SMatthew Knepley {
1480000e7ae3SMatthew Knepley   PetscFunctionBegin;
14810700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1482000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1483000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1484000e7ae3SMatthew Knepley }
1485000e7ae3SMatthew Knepley 
1486e74ef692SMatthew Knepley #undef __FUNCT__
14873f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
14883f2090d5SJed Brown /*@C
14893f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
14903f2090d5SJed Brown 
14913f2090d5SJed Brown   Collective on TS
14923f2090d5SJed Brown 
14933f2090d5SJed Brown   Input Parameters:
14943f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
14953f2090d5SJed Brown 
14963f2090d5SJed Brown   Notes:
14973f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
14983f2090d5SJed Brown   so most users would not generally call this routine themselves.
14993f2090d5SJed Brown 
15003f2090d5SJed Brown   Level: developer
15013f2090d5SJed Brown 
15023f2090d5SJed Brown .keywords: TS, timestep
15033f2090d5SJed Brown @*/
15047087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
15053f2090d5SJed Brown {
15063f2090d5SJed Brown   PetscErrorCode ierr;
15073f2090d5SJed Brown 
15083f2090d5SJed Brown   PetscFunctionBegin;
15090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
151072ac3e02SJed Brown   if (ts->ops->prestep) {
15113f2090d5SJed Brown     PetscStackPush("TS PreStep function");
15123f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
15133f2090d5SJed Brown     PetscStackPop;
1514312ce896SJed Brown   }
15153f2090d5SJed Brown   PetscFunctionReturn(0);
15163f2090d5SJed Brown }
15173f2090d5SJed Brown 
15183f2090d5SJed Brown #undef __FUNCT__
1519e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1520ac226902SBarry Smith /*@C
1521000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
15223f2090d5SJed Brown   called once at the end of each time step.
1523000e7ae3SMatthew Knepley 
15243f9fe445SBarry Smith   Logically Collective on TS
1525000e7ae3SMatthew Knepley 
1526000e7ae3SMatthew Knepley   Input Parameters:
1527000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1528000e7ae3SMatthew Knepley - func - The function
1529000e7ae3SMatthew Knepley 
1530000e7ae3SMatthew Knepley   Calling sequence of func:
1531000e7ae3SMatthew Knepley . func (TS ts);
1532000e7ae3SMatthew Knepley 
1533000e7ae3SMatthew Knepley   Level: intermediate
1534000e7ae3SMatthew Knepley 
1535000e7ae3SMatthew Knepley .keywords: TS, timestep
1536000e7ae3SMatthew Knepley @*/
15377087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1538000e7ae3SMatthew Knepley {
1539000e7ae3SMatthew Knepley   PetscFunctionBegin;
15400700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1541000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1542000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1543000e7ae3SMatthew Knepley }
1544000e7ae3SMatthew Knepley 
1545e74ef692SMatthew Knepley #undef __FUNCT__
15463f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
15473f2090d5SJed Brown /*@C
15483f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
15493f2090d5SJed Brown 
15503f2090d5SJed Brown   Collective on TS
15513f2090d5SJed Brown 
15523f2090d5SJed Brown   Input Parameters:
15533f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
15543f2090d5SJed Brown 
15553f2090d5SJed Brown   Notes:
15563f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
15573f2090d5SJed Brown   so most users would not generally call this routine themselves.
15583f2090d5SJed Brown 
15593f2090d5SJed Brown   Level: developer
15603f2090d5SJed Brown 
15613f2090d5SJed Brown .keywords: TS, timestep
15623f2090d5SJed Brown @*/
15637087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
15643f2090d5SJed Brown {
15653f2090d5SJed Brown   PetscErrorCode ierr;
15663f2090d5SJed Brown 
15673f2090d5SJed Brown   PetscFunctionBegin;
15680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
156972ac3e02SJed Brown   if (ts->ops->poststep) {
15703f2090d5SJed Brown     PetscStackPush("TS PostStep function");
15713f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
15723f2090d5SJed Brown     PetscStackPop;
157372ac3e02SJed Brown   }
15743f2090d5SJed Brown   PetscFunctionReturn(0);
15753f2090d5SJed Brown }
15763f2090d5SJed Brown 
1577d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1578d763cef2SBarry Smith 
15794a2ae208SSatish Balay #undef __FUNCT__
1580a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1581d763cef2SBarry Smith /*@C
1582a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1583d763cef2SBarry Smith    timestep to display the iteration's  progress.
1584d763cef2SBarry Smith 
15853f9fe445SBarry Smith    Logically Collective on TS
1586d763cef2SBarry Smith 
1587d763cef2SBarry Smith    Input Parameters:
1588d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1589d763cef2SBarry Smith .  func - monitoring routine
1590329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1591b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1592b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1593b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1594d763cef2SBarry Smith 
1595d763cef2SBarry Smith    Calling sequence of func:
1596a7cc72afSBarry Smith $    int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1597d763cef2SBarry Smith 
1598d763cef2SBarry Smith +    ts - the TS context
1599d763cef2SBarry Smith .    steps - iteration number
16001f06c33eSBarry Smith .    time - current time
1601d763cef2SBarry Smith .    x - current iterate
1602d763cef2SBarry Smith -    mctx - [optional] monitoring context
1603d763cef2SBarry Smith 
1604d763cef2SBarry Smith    Notes:
1605d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1606d763cef2SBarry Smith    already has been loaded.
1607d763cef2SBarry Smith 
1608025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1609025f1a04SBarry Smith 
1610d763cef2SBarry Smith    Level: intermediate
1611d763cef2SBarry Smith 
1612d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1613d763cef2SBarry Smith 
1614a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1615d763cef2SBarry Smith @*/
1616c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1617d763cef2SBarry Smith {
1618d763cef2SBarry Smith   PetscFunctionBegin;
16190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
162017186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1621d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1622329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1623d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1624d763cef2SBarry Smith   PetscFunctionReturn(0);
1625d763cef2SBarry Smith }
1626d763cef2SBarry Smith 
16274a2ae208SSatish Balay #undef __FUNCT__
1628a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1629d763cef2SBarry Smith /*@C
1630a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1631d763cef2SBarry Smith 
16323f9fe445SBarry Smith    Logically Collective on TS
1633d763cef2SBarry Smith 
1634d763cef2SBarry Smith    Input Parameters:
1635d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1636d763cef2SBarry Smith 
1637d763cef2SBarry Smith    Notes:
1638d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1639d763cef2SBarry Smith 
1640d763cef2SBarry Smith    Level: intermediate
1641d763cef2SBarry Smith 
1642d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1643d763cef2SBarry Smith 
1644a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1645d763cef2SBarry Smith @*/
16467087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1647d763cef2SBarry Smith {
1648d952e501SBarry Smith   PetscErrorCode ierr;
1649d952e501SBarry Smith   PetscInt       i;
1650d952e501SBarry Smith 
1651d763cef2SBarry Smith   PetscFunctionBegin;
16520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1653d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
1654d952e501SBarry Smith     if (ts->mdestroy[i]) {
16553c4aec1bSBarry Smith       ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
1656d952e501SBarry Smith     }
1657d952e501SBarry Smith   }
1658d763cef2SBarry Smith   ts->numbermonitors = 0;
1659d763cef2SBarry Smith   PetscFunctionReturn(0);
1660d763cef2SBarry Smith }
1661d763cef2SBarry Smith 
16624a2ae208SSatish Balay #undef __FUNCT__
1663a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
1664d8e5e3e6SSatish Balay /*@
1665a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
16665516499fSSatish Balay 
16675516499fSSatish Balay    Level: intermediate
166841251cbbSSatish Balay 
16695516499fSSatish Balay .keywords: TS, set, monitor
16705516499fSSatish Balay 
167141251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
167241251cbbSSatish Balay @*/
1673649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
1674d763cef2SBarry Smith {
1675dfbe8321SBarry Smith   PetscErrorCode ierr;
1676649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
1677d132466eSBarry Smith 
1678d763cef2SBarry Smith   PetscFunctionBegin;
1679649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1680649052a6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr);
1681649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1682d763cef2SBarry Smith   PetscFunctionReturn(0);
1683d763cef2SBarry Smith }
1684d763cef2SBarry Smith 
16854a2ae208SSatish Balay #undef __FUNCT__
1686cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
1687cd652676SJed Brown /*@
1688cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
1689cd652676SJed Brown 
1690cd652676SJed Brown    Logically Collective on TS
1691cd652676SJed Brown 
1692cd652676SJed Brown    Input Argument:
1693cd652676SJed Brown .  ts - time stepping context
1694cd652676SJed Brown 
1695cd652676SJed Brown    Output Argument:
1696cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
1697cd652676SJed Brown 
1698cd652676SJed Brown    Level: intermediate
1699cd652676SJed Brown 
1700cd652676SJed Brown .keywords: TS, set
1701cd652676SJed Brown 
1702cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
1703cd652676SJed Brown @*/
1704cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
1705cd652676SJed Brown {
1706cd652676SJed Brown 
1707cd652676SJed Brown   PetscFunctionBegin;
1708cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1709cd652676SJed Brown   ts->retain_stages = flg;
1710cd652676SJed Brown   PetscFunctionReturn(0);
1711cd652676SJed Brown }
1712cd652676SJed Brown 
1713cd652676SJed Brown #undef __FUNCT__
1714cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
1715cd652676SJed Brown /*@
1716cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
1717cd652676SJed Brown 
1718cd652676SJed Brown    Collective on TS
1719cd652676SJed Brown 
1720cd652676SJed Brown    Input Argument:
1721cd652676SJed Brown +  ts - time stepping context
1722cd652676SJed Brown -  t - time to interpolate to
1723cd652676SJed Brown 
1724cd652676SJed Brown    Output Argument:
1725cd652676SJed Brown .  X - state at given time
1726cd652676SJed Brown 
1727cd652676SJed Brown    Notes:
1728cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
1729cd652676SJed Brown 
1730cd652676SJed Brown    Level: intermediate
1731cd652676SJed Brown 
1732cd652676SJed Brown    Developer Notes:
1733cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
1734cd652676SJed Brown 
1735cd652676SJed Brown .keywords: TS, set
1736cd652676SJed Brown 
1737cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
1738cd652676SJed Brown @*/
1739cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X)
1740cd652676SJed Brown {
1741cd652676SJed Brown   PetscErrorCode ierr;
1742cd652676SJed Brown 
1743cd652676SJed Brown   PetscFunctionBegin;
1744cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1745cd652676SJed 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);
1746cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
1747cd652676SJed Brown   ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr);
1748cd652676SJed Brown   PetscFunctionReturn(0);
1749cd652676SJed Brown }
1750cd652676SJed Brown 
1751cd652676SJed Brown #undef __FUNCT__
17524a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1753d763cef2SBarry Smith /*@
1754d763cef2SBarry Smith    TSStep - Steps the requested number of timesteps.
1755d763cef2SBarry Smith 
1756d763cef2SBarry Smith    Collective on TS
1757d763cef2SBarry Smith 
1758d763cef2SBarry Smith    Input Parameter:
1759d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1760d763cef2SBarry Smith 
1761d763cef2SBarry Smith    Output Parameters:
1762d763cef2SBarry Smith +  steps - number of iterations until termination
1763142b95e3SSatish Balay -  ptime - time until termination
1764d763cef2SBarry Smith 
1765d763cef2SBarry Smith    Level: beginner
1766d763cef2SBarry Smith 
1767d763cef2SBarry Smith .keywords: TS, timestep, solve
1768d763cef2SBarry Smith 
1769d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy()
1770d763cef2SBarry Smith @*/
1771193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
1772d763cef2SBarry Smith {
1773dfbe8321SBarry Smith   PetscErrorCode ierr;
1774d763cef2SBarry Smith 
1775d763cef2SBarry Smith   PetscFunctionBegin;
17760700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1777277b19d0SLisandro Dalcin 
1778d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
1779d405a339SMatthew Knepley 
1780d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1781193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
1782d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1783d763cef2SBarry Smith   PetscFunctionReturn(0);
1784d763cef2SBarry Smith }
1785d763cef2SBarry Smith 
17864a2ae208SSatish Balay #undef __FUNCT__
17876a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
17886a4d4014SLisandro Dalcin /*@
17896a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
17906a4d4014SLisandro Dalcin 
17916a4d4014SLisandro Dalcin    Collective on TS
17926a4d4014SLisandro Dalcin 
17936a4d4014SLisandro Dalcin    Input Parameter:
17946a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
17956a4d4014SLisandro Dalcin -  x - the solution vector, or PETSC_NULL if it was set with TSSetSolution()
17966a4d4014SLisandro Dalcin 
17975a3a76d0SJed Brown    Output Parameter:
17985a3a76d0SJed Brown .  ftime - time of the state vector x upon completion
17995a3a76d0SJed Brown 
18006a4d4014SLisandro Dalcin    Level: beginner
18016a4d4014SLisandro Dalcin 
18025a3a76d0SJed Brown    Notes:
18035a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
18045a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
18055a3a76d0SJed Brown    stepped over the final time.
18065a3a76d0SJed Brown 
18076a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
18086a4d4014SLisandro Dalcin 
18096a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
18106a4d4014SLisandro Dalcin @*/
18115a3a76d0SJed Brown PetscErrorCode TSSolve(TS ts,Vec x,PetscReal *ftime)
18126a4d4014SLisandro Dalcin {
1813193ac0bcSJed Brown   PetscInt       i;
18144d7d938eSLisandro Dalcin   PetscBool      flg;
18154d7d938eSLisandro Dalcin   char           filename[PETSC_MAX_PATH_LEN];
18164d7d938eSLisandro Dalcin   PetscViewer    viewer;
18176a4d4014SLisandro Dalcin   PetscErrorCode ierr;
1818f22f69f0SBarry Smith 
18196a4d4014SLisandro Dalcin   PetscFunctionBegin;
18200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1821193ac0bcSJed Brown   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
18225a3a76d0SJed Brown   if (ts->exact_final_time) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
18235a3a76d0SJed Brown     if (!ts->vec_sol || x == ts->vec_sol) {
18245a3a76d0SJed Brown       Vec y;
18255a3a76d0SJed Brown       ierr = VecDuplicate(x,&y);CHKERRQ(ierr);
18265a3a76d0SJed Brown       ierr = VecCopy(x,y);CHKERRQ(ierr);
18275a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
18285a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
18295a3a76d0SJed Brown     } else {
18305a3a76d0SJed Brown       ierr = VecCopy(x,ts->vec_sol);CHKERRQ(ierr);
18315a3a76d0SJed Brown     }
18325a3a76d0SJed Brown   } else {
1833193ac0bcSJed Brown     ierr = TSSetSolution(ts,x);CHKERRQ(ierr);
18345a3a76d0SJed Brown   }
1835b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
18366a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
1837193ac0bcSJed Brown   ts->steps = 0;
1838193ac0bcSJed Brown   ts->linear_its = 0;
1839193ac0bcSJed Brown   ts->nonlinear_its = 0;
1840193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
1841193ac0bcSJed Brown   ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
1842193ac0bcSJed Brown 
1843193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
1844193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
18455a3a76d0SJed Brown     ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
18465a3a76d0SJed Brown     if (*ftime) *ftime = ts->ptime;
1847193ac0bcSJed Brown   } else {
1848e1a7a14fSJed Brown     i = 0;
1849e1a7a14fSJed Brown     if (i >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
1850e1a7a14fSJed Brown     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
18516a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
1852e1a7a14fSJed Brown     while (!ts->reason) {
1853193ac0bcSJed Brown       ierr = TSPreStep(ts);CHKERRQ(ierr);
1854193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
1855193ac0bcSJed Brown       if (ts->reason < 0) {
1856193ac0bcSJed Brown         if (ts->errorifstepfailed) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed");
18573daf2de8SJed Brown       } else if (++i >= ts->max_steps) {
1858193ac0bcSJed Brown         ts->reason = TS_CONVERGED_ITS;
1859193ac0bcSJed Brown       } else if (ts->ptime >= ts->max_time) {
1860193ac0bcSJed Brown         ts->reason = TS_CONVERGED_TIME;
1861193ac0bcSJed Brown       }
1862193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
1863193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
1864193ac0bcSJed Brown     }
186569abe88aSJed Brown     if (ts->exact_final_time && ts->ptime >= ts->max_time) {
1866a43b19c4SJed Brown       ierr = TSInterpolate(ts,ts->max_time,x);CHKERRQ(ierr);
18675a3a76d0SJed Brown       if (ftime) *ftime = ts->max_time;
18680574a7fbSJed Brown     } else {
18690574a7fbSJed Brown       if (x != ts->vec_sol) {
18700574a7fbSJed Brown         ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
18710574a7fbSJed Brown       }
18720574a7fbSJed Brown       if (ftime) *ftime = ts->ptime;
18730574a7fbSJed Brown     }
1874193ac0bcSJed Brown   }
18754d7d938eSLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
18764d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
18774d7d938eSLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr);
18784d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
18794d7d938eSLisandro Dalcin     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1880193ac0bcSJed Brown   }
18816a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
18826a4d4014SLisandro Dalcin }
18836a4d4014SLisandro Dalcin 
18846a4d4014SLisandro Dalcin #undef __FUNCT__
18854a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
1886d763cef2SBarry Smith /*
1887d763cef2SBarry Smith      Runs the user provided monitor routines, if they exists.
1888d763cef2SBarry Smith */
1889a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
1890d763cef2SBarry Smith {
18916849ba73SBarry Smith   PetscErrorCode ierr;
1892a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
1893d763cef2SBarry Smith 
1894d763cef2SBarry Smith   PetscFunctionBegin;
1895d763cef2SBarry Smith   for (i=0; i<n; i++) {
1896142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
1897d763cef2SBarry Smith   }
1898d763cef2SBarry Smith   PetscFunctionReturn(0);
1899d763cef2SBarry Smith }
1900d763cef2SBarry Smith 
1901d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
1902d763cef2SBarry Smith 
19034a2ae208SSatish Balay #undef __FUNCT__
1904a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate"
1905d763cef2SBarry Smith /*@C
1906a6570f20SBarry Smith    TSMonitorLGCreate - Creates a line graph context for use with
1907d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
1908d763cef2SBarry Smith 
1909d763cef2SBarry Smith    Collective on TS
1910d763cef2SBarry Smith 
1911d763cef2SBarry Smith    Input Parameters:
1912d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
1913d763cef2SBarry Smith .  label - the title to put in the title bar
19147c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
1915d763cef2SBarry Smith -  m, n - the screen width and height in pixels
1916d763cef2SBarry Smith 
1917d763cef2SBarry Smith    Output Parameter:
1918d763cef2SBarry Smith .  draw - the drawing context
1919d763cef2SBarry Smith 
1920d763cef2SBarry Smith    Options Database Key:
1921a6570f20SBarry Smith .  -ts_monitor_draw - automatically sets line graph monitor
1922d763cef2SBarry Smith 
1923d763cef2SBarry Smith    Notes:
1924a6570f20SBarry Smith    Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy().
1925d763cef2SBarry Smith 
1926d763cef2SBarry Smith    Level: intermediate
1927d763cef2SBarry Smith 
19287c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
1929d763cef2SBarry Smith 
1930a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet()
19317c922b88SBarry Smith 
1932d763cef2SBarry Smith @*/
19337087cfbeSBarry Smith PetscErrorCode  TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1934d763cef2SBarry Smith {
1935b0a32e0cSBarry Smith   PetscDraw      win;
1936dfbe8321SBarry Smith   PetscErrorCode ierr;
1937d763cef2SBarry Smith 
1938d763cef2SBarry Smith   PetscFunctionBegin;
1939b0a32e0cSBarry Smith   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
1940b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
1941b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
1942b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
1943d763cef2SBarry Smith 
194452e6d16bSBarry Smith   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
1945d763cef2SBarry Smith   PetscFunctionReturn(0);
1946d763cef2SBarry Smith }
1947d763cef2SBarry Smith 
19484a2ae208SSatish Balay #undef __FUNCT__
1949a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG"
1950a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
1951d763cef2SBarry Smith {
1952b0a32e0cSBarry Smith   PetscDrawLG    lg = (PetscDrawLG) monctx;
195387828ca2SBarry Smith   PetscReal      x,y = ptime;
1954dfbe8321SBarry Smith   PetscErrorCode ierr;
1955d763cef2SBarry Smith 
1956d763cef2SBarry Smith   PetscFunctionBegin;
19577c922b88SBarry Smith   if (!monctx) {
19587c922b88SBarry Smith     MPI_Comm    comm;
1959b0a32e0cSBarry Smith     PetscViewer viewer;
19607c922b88SBarry Smith 
19617c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
1962b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
1963b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
19647c922b88SBarry Smith   }
19657c922b88SBarry Smith 
1966b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
196787828ca2SBarry Smith   x = (PetscReal)n;
1968b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1969d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
1970b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1971d763cef2SBarry Smith   }
1972d763cef2SBarry Smith   PetscFunctionReturn(0);
1973d763cef2SBarry Smith }
1974d763cef2SBarry Smith 
19754a2ae208SSatish Balay #undef __FUNCT__
1976a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy"
1977d763cef2SBarry Smith /*@C
1978a6570f20SBarry Smith    TSMonitorLGDestroy - Destroys a line graph context that was created
1979a6570f20SBarry Smith    with TSMonitorLGCreate().
1980d763cef2SBarry Smith 
1981b0a32e0cSBarry Smith    Collective on PetscDrawLG
1982d763cef2SBarry Smith 
1983d763cef2SBarry Smith    Input Parameter:
1984d763cef2SBarry Smith .  draw - the drawing context
1985d763cef2SBarry Smith 
1986d763cef2SBarry Smith    Level: intermediate
1987d763cef2SBarry Smith 
1988d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
1989d763cef2SBarry Smith 
1990a6570f20SBarry Smith .seealso: TSMonitorLGCreate(),  TSMonitorSet(), TSMonitorLG();
1991d763cef2SBarry Smith @*/
19926bf464f9SBarry Smith PetscErrorCode  TSMonitorLGDestroy(PetscDrawLG *drawlg)
1993d763cef2SBarry Smith {
1994b0a32e0cSBarry Smith   PetscDraw      draw;
1995dfbe8321SBarry Smith   PetscErrorCode ierr;
1996d763cef2SBarry Smith 
1997d763cef2SBarry Smith   PetscFunctionBegin;
19986bf464f9SBarry Smith   ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr);
19996bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
2000b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
2001d763cef2SBarry Smith   PetscFunctionReturn(0);
2002d763cef2SBarry Smith }
2003d763cef2SBarry Smith 
20044a2ae208SSatish Balay #undef __FUNCT__
20054a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2006d763cef2SBarry Smith /*@
2007d763cef2SBarry Smith    TSGetTime - Gets the current time.
2008d763cef2SBarry Smith 
2009d763cef2SBarry Smith    Not Collective
2010d763cef2SBarry Smith 
2011d763cef2SBarry Smith    Input Parameter:
2012d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2013d763cef2SBarry Smith 
2014d763cef2SBarry Smith    Output Parameter:
2015d763cef2SBarry Smith .  t  - the current time
2016d763cef2SBarry Smith 
2017d763cef2SBarry Smith    Level: beginner
2018d763cef2SBarry Smith 
2019d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2020d763cef2SBarry Smith 
2021d763cef2SBarry Smith .keywords: TS, get, time
2022d763cef2SBarry Smith @*/
20237087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
2024d763cef2SBarry Smith {
2025d763cef2SBarry Smith   PetscFunctionBegin;
20260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20274482741eSBarry Smith   PetscValidDoublePointer(t,2);
2028d763cef2SBarry Smith   *t = ts->ptime;
2029d763cef2SBarry Smith   PetscFunctionReturn(0);
2030d763cef2SBarry Smith }
2031d763cef2SBarry Smith 
20324a2ae208SSatish Balay #undef __FUNCT__
20336a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
20346a4d4014SLisandro Dalcin /*@
20356a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
20366a4d4014SLisandro Dalcin 
20373f9fe445SBarry Smith    Logically Collective on TS
20386a4d4014SLisandro Dalcin 
20396a4d4014SLisandro Dalcin    Input Parameters:
20406a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
20416a4d4014SLisandro Dalcin -  time - the time
20426a4d4014SLisandro Dalcin 
20436a4d4014SLisandro Dalcin    Level: intermediate
20446a4d4014SLisandro Dalcin 
20456a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
20466a4d4014SLisandro Dalcin 
20476a4d4014SLisandro Dalcin .keywords: TS, set, time
20486a4d4014SLisandro Dalcin @*/
20497087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
20506a4d4014SLisandro Dalcin {
20516a4d4014SLisandro Dalcin   PetscFunctionBegin;
20520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2053c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
20546a4d4014SLisandro Dalcin   ts->ptime = t;
20556a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
20566a4d4014SLisandro Dalcin }
20576a4d4014SLisandro Dalcin 
20586a4d4014SLisandro Dalcin #undef __FUNCT__
20594a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2060d763cef2SBarry Smith /*@C
2061d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2062d763cef2SBarry Smith    TS options in the database.
2063d763cef2SBarry Smith 
20643f9fe445SBarry Smith    Logically Collective on TS
2065d763cef2SBarry Smith 
2066d763cef2SBarry Smith    Input Parameter:
2067d763cef2SBarry Smith +  ts     - The TS context
2068d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2069d763cef2SBarry Smith 
2070d763cef2SBarry Smith    Notes:
2071d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2072d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2073d763cef2SBarry Smith    hyphen.
2074d763cef2SBarry Smith 
2075d763cef2SBarry Smith    Level: advanced
2076d763cef2SBarry Smith 
2077d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2078d763cef2SBarry Smith 
2079d763cef2SBarry Smith .seealso: TSSetFromOptions()
2080d763cef2SBarry Smith 
2081d763cef2SBarry Smith @*/
20827087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2083d763cef2SBarry Smith {
2084dfbe8321SBarry Smith   PetscErrorCode ierr;
2085089b2837SJed Brown   SNES           snes;
2086d763cef2SBarry Smith 
2087d763cef2SBarry Smith   PetscFunctionBegin;
20880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2089d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2090089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2091089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2092d763cef2SBarry Smith   PetscFunctionReturn(0);
2093d763cef2SBarry Smith }
2094d763cef2SBarry Smith 
2095d763cef2SBarry Smith 
20964a2ae208SSatish Balay #undef __FUNCT__
20974a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2098d763cef2SBarry Smith /*@C
2099d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2100d763cef2SBarry Smith    TS options in the database.
2101d763cef2SBarry Smith 
21023f9fe445SBarry Smith    Logically Collective on TS
2103d763cef2SBarry Smith 
2104d763cef2SBarry Smith    Input Parameter:
2105d763cef2SBarry Smith +  ts     - The TS context
2106d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2107d763cef2SBarry Smith 
2108d763cef2SBarry Smith    Notes:
2109d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2110d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2111d763cef2SBarry Smith    hyphen.
2112d763cef2SBarry Smith 
2113d763cef2SBarry Smith    Level: advanced
2114d763cef2SBarry Smith 
2115d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2116d763cef2SBarry Smith 
2117d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2118d763cef2SBarry Smith 
2119d763cef2SBarry Smith @*/
21207087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2121d763cef2SBarry Smith {
2122dfbe8321SBarry Smith   PetscErrorCode ierr;
2123089b2837SJed Brown   SNES           snes;
2124d763cef2SBarry Smith 
2125d763cef2SBarry Smith   PetscFunctionBegin;
21260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2127d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2128089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2129089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2130d763cef2SBarry Smith   PetscFunctionReturn(0);
2131d763cef2SBarry Smith }
2132d763cef2SBarry Smith 
21334a2ae208SSatish Balay #undef __FUNCT__
21344a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2135d763cef2SBarry Smith /*@C
2136d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2137d763cef2SBarry Smith    TS options in the database.
2138d763cef2SBarry Smith 
2139d763cef2SBarry Smith    Not Collective
2140d763cef2SBarry Smith 
2141d763cef2SBarry Smith    Input Parameter:
2142d763cef2SBarry Smith .  ts - The TS context
2143d763cef2SBarry Smith 
2144d763cef2SBarry Smith    Output Parameter:
2145d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2146d763cef2SBarry Smith 
2147d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2148d763cef2SBarry Smith    sufficient length to hold the prefix.
2149d763cef2SBarry Smith 
2150d763cef2SBarry Smith    Level: intermediate
2151d763cef2SBarry Smith 
2152d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2153d763cef2SBarry Smith 
2154d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2155d763cef2SBarry Smith @*/
21567087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2157d763cef2SBarry Smith {
2158dfbe8321SBarry Smith   PetscErrorCode ierr;
2159d763cef2SBarry Smith 
2160d763cef2SBarry Smith   PetscFunctionBegin;
21610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21624482741eSBarry Smith   PetscValidPointer(prefix,2);
2163d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2164d763cef2SBarry Smith   PetscFunctionReturn(0);
2165d763cef2SBarry Smith }
2166d763cef2SBarry Smith 
21674a2ae208SSatish Balay #undef __FUNCT__
21684a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2169d763cef2SBarry Smith /*@C
2170d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2171d763cef2SBarry Smith 
2172d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2173d763cef2SBarry Smith 
2174d763cef2SBarry Smith    Input Parameter:
2175d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2176d763cef2SBarry Smith 
2177d763cef2SBarry Smith    Output Parameters:
2178d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2179d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2180089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2181d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2182d763cef2SBarry Smith 
2183d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2184d763cef2SBarry Smith 
2185d763cef2SBarry Smith    Level: intermediate
2186d763cef2SBarry Smith 
218726d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2188d763cef2SBarry Smith 
2189d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2190d763cef2SBarry Smith @*/
2191089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2192d763cef2SBarry Smith {
2193089b2837SJed Brown   PetscErrorCode ierr;
2194089b2837SJed Brown   SNES           snes;
2195089b2837SJed Brown 
2196d763cef2SBarry Smith   PetscFunctionBegin;
2197089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2198089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
21993b5f76d0SSean Farley   if (func) *func = ts->userops->rhsjacobian;
220026d46c62SHong Zhang   if (ctx) *ctx = ts->jacP;
2201d763cef2SBarry Smith   PetscFunctionReturn(0);
2202d763cef2SBarry Smith }
2203d763cef2SBarry Smith 
22041713a123SBarry Smith #undef __FUNCT__
22052eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
22062eca1d9cSJed Brown /*@C
22072eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
22082eca1d9cSJed Brown 
22092eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
22102eca1d9cSJed Brown 
22112eca1d9cSJed Brown    Input Parameter:
22122eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
22132eca1d9cSJed Brown 
22142eca1d9cSJed Brown    Output Parameters:
22152eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
22162eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
22172eca1d9cSJed Brown .  f   - The function to compute the matrices
22182eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
22192eca1d9cSJed Brown 
22202eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
22212eca1d9cSJed Brown 
22222eca1d9cSJed Brown    Level: advanced
22232eca1d9cSJed Brown 
22242eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
22252eca1d9cSJed Brown 
22262eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
22272eca1d9cSJed Brown @*/
22287087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
22292eca1d9cSJed Brown {
2230089b2837SJed Brown   PetscErrorCode ierr;
2231089b2837SJed Brown   SNES           snes;
2232089b2837SJed Brown 
22332eca1d9cSJed Brown   PetscFunctionBegin;
2234089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2235089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
22363b5f76d0SSean Farley   if (f) *f = ts->userops->ijacobian;
22372eca1d9cSJed Brown   if (ctx) *ctx = ts->jacP;
22382eca1d9cSJed Brown   PetscFunctionReturn(0);
22392eca1d9cSJed Brown }
22402eca1d9cSJed Brown 
22412eca1d9cSJed Brown #undef __FUNCT__
2242a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution"
22431713a123SBarry Smith /*@C
2244a6570f20SBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by calling
22451713a123SBarry Smith    VecView() for the solution at each timestep
22461713a123SBarry Smith 
22471713a123SBarry Smith    Collective on TS
22481713a123SBarry Smith 
22491713a123SBarry Smith    Input Parameters:
22501713a123SBarry Smith +  ts - the TS context
22511713a123SBarry Smith .  step - current time-step
2252142b95e3SSatish Balay .  ptime - current time
22531713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
22541713a123SBarry Smith 
22551713a123SBarry Smith    Level: intermediate
22561713a123SBarry Smith 
22571713a123SBarry Smith .keywords: TS,  vector, monitor, view
22581713a123SBarry Smith 
2259a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
22601713a123SBarry Smith @*/
22617087cfbeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
22621713a123SBarry Smith {
2263dfbe8321SBarry Smith   PetscErrorCode ierr;
22641713a123SBarry Smith   PetscViewer    viewer = (PetscViewer) dummy;
22651713a123SBarry Smith 
22661713a123SBarry Smith   PetscFunctionBegin;
2267a34d58ebSBarry Smith   if (!dummy) {
22687adad957SLisandro Dalcin     viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm);
22691713a123SBarry Smith   }
22701713a123SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
22711713a123SBarry Smith   PetscFunctionReturn(0);
22721713a123SBarry Smith }
22731713a123SBarry Smith 
22741713a123SBarry Smith 
22756c699258SBarry Smith #undef __FUNCT__
22766c699258SBarry Smith #define __FUNCT__ "TSSetDM"
22776c699258SBarry Smith /*@
22786c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
22796c699258SBarry Smith 
22803f9fe445SBarry Smith    Logically Collective on TS and DM
22816c699258SBarry Smith 
22826c699258SBarry Smith    Input Parameters:
22836c699258SBarry Smith +  ts - the preconditioner context
22846c699258SBarry Smith -  dm - the dm
22856c699258SBarry Smith 
22866c699258SBarry Smith    Level: intermediate
22876c699258SBarry Smith 
22886c699258SBarry Smith 
22896c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
22906c699258SBarry Smith @*/
22917087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
22926c699258SBarry Smith {
22936c699258SBarry Smith   PetscErrorCode ierr;
2294089b2837SJed Brown   SNES           snes;
22956c699258SBarry Smith 
22966c699258SBarry Smith   PetscFunctionBegin;
22970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
229870663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
22996bf464f9SBarry Smith   ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
23006c699258SBarry Smith   ts->dm = dm;
2301089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2302089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
23036c699258SBarry Smith   PetscFunctionReturn(0);
23046c699258SBarry Smith }
23056c699258SBarry Smith 
23066c699258SBarry Smith #undef __FUNCT__
23076c699258SBarry Smith #define __FUNCT__ "TSGetDM"
23086c699258SBarry Smith /*@
23096c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
23106c699258SBarry Smith 
23113f9fe445SBarry Smith    Not Collective
23126c699258SBarry Smith 
23136c699258SBarry Smith    Input Parameter:
23146c699258SBarry Smith . ts - the preconditioner context
23156c699258SBarry Smith 
23166c699258SBarry Smith    Output Parameter:
23176c699258SBarry Smith .  dm - the dm
23186c699258SBarry Smith 
23196c699258SBarry Smith    Level: intermediate
23206c699258SBarry Smith 
23216c699258SBarry Smith 
23226c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
23236c699258SBarry Smith @*/
23247087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
23256c699258SBarry Smith {
23266c699258SBarry Smith   PetscFunctionBegin;
23270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23286c699258SBarry Smith   *dm = ts->dm;
23296c699258SBarry Smith   PetscFunctionReturn(0);
23306c699258SBarry Smith }
23311713a123SBarry Smith 
23320f5c6efeSJed Brown #undef __FUNCT__
23330f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
23340f5c6efeSJed Brown /*@
23350f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
23360f5c6efeSJed Brown 
23373f9fe445SBarry Smith    Logically Collective on SNES
23380f5c6efeSJed Brown 
23390f5c6efeSJed Brown    Input Parameter:
2340d42a1c89SJed Brown + snes - nonlinear solver
23410f5c6efeSJed Brown . X - the current state at which to evaluate the residual
2342d42a1c89SJed Brown - ctx - user context, must be a TS
23430f5c6efeSJed Brown 
23440f5c6efeSJed Brown    Output Parameter:
23450f5c6efeSJed Brown . F - the nonlinear residual
23460f5c6efeSJed Brown 
23470f5c6efeSJed Brown    Notes:
23480f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
23490f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
23500f5c6efeSJed Brown 
23510f5c6efeSJed Brown    Level: advanced
23520f5c6efeSJed Brown 
23530f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
23540f5c6efeSJed Brown @*/
23557087cfbeSBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx)
23560f5c6efeSJed Brown {
23570f5c6efeSJed Brown   TS ts = (TS)ctx;
23580f5c6efeSJed Brown   PetscErrorCode ierr;
23590f5c6efeSJed Brown 
23600f5c6efeSJed Brown   PetscFunctionBegin;
23610f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23620f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
23630f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
23640f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
23650f5c6efeSJed Brown   ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr);
23660f5c6efeSJed Brown   PetscFunctionReturn(0);
23670f5c6efeSJed Brown }
23680f5c6efeSJed Brown 
23690f5c6efeSJed Brown #undef __FUNCT__
23700f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
23710f5c6efeSJed Brown /*@
23720f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
23730f5c6efeSJed Brown 
23740f5c6efeSJed Brown    Collective on SNES
23750f5c6efeSJed Brown 
23760f5c6efeSJed Brown    Input Parameter:
23770f5c6efeSJed Brown + snes - nonlinear solver
23780f5c6efeSJed Brown . X - the current state at which to evaluate the residual
23790f5c6efeSJed Brown - ctx - user context, must be a TS
23800f5c6efeSJed Brown 
23810f5c6efeSJed Brown    Output Parameter:
23820f5c6efeSJed Brown + A - the Jacobian
23830f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
23840f5c6efeSJed Brown - flag - indicates any structure change in the matrix
23850f5c6efeSJed Brown 
23860f5c6efeSJed Brown    Notes:
23870f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
23880f5c6efeSJed Brown 
23890f5c6efeSJed Brown    Level: developer
23900f5c6efeSJed Brown 
23910f5c6efeSJed Brown .seealso: SNESSetJacobian()
23920f5c6efeSJed Brown @*/
23937087cfbeSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx)
23940f5c6efeSJed Brown {
23950f5c6efeSJed Brown   TS ts = (TS)ctx;
23960f5c6efeSJed Brown   PetscErrorCode ierr;
23970f5c6efeSJed Brown 
23980f5c6efeSJed Brown   PetscFunctionBegin;
23990f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24000f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
24010f5c6efeSJed Brown   PetscValidPointer(A,3);
24020f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
24030f5c6efeSJed Brown   PetscValidPointer(B,4);
24040f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
24050f5c6efeSJed Brown   PetscValidPointer(flag,5);
24060f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
24070f5c6efeSJed Brown   ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr);
24080f5c6efeSJed Brown   PetscFunctionReturn(0);
24090f5c6efeSJed Brown }
2410325fc9f4SBarry Smith 
24110e4ef248SJed Brown #undef __FUNCT__
24120e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
24130e4ef248SJed Brown /*@C
24140e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
24150e4ef248SJed Brown 
24160e4ef248SJed Brown    Collective on TS
24170e4ef248SJed Brown 
24180e4ef248SJed Brown    Input Arguments:
24190e4ef248SJed Brown +  ts - time stepping context
24200e4ef248SJed Brown .  t - time at which to evaluate
24210e4ef248SJed Brown .  X - state at which to evaluate
24220e4ef248SJed Brown -  ctx - context
24230e4ef248SJed Brown 
24240e4ef248SJed Brown    Output Arguments:
24250e4ef248SJed Brown .  F - right hand side
24260e4ef248SJed Brown 
24270e4ef248SJed Brown    Level: intermediate
24280e4ef248SJed Brown 
24290e4ef248SJed Brown    Notes:
24300e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
24310e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
24320e4ef248SJed Brown 
24330e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
24340e4ef248SJed Brown @*/
24350e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx)
24360e4ef248SJed Brown {
24370e4ef248SJed Brown   PetscErrorCode ierr;
24380e4ef248SJed Brown   Mat Arhs,Brhs;
24390e4ef248SJed Brown   MatStructure flg2;
24400e4ef248SJed Brown 
24410e4ef248SJed Brown   PetscFunctionBegin;
24420e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
24430e4ef248SJed Brown   ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
24440e4ef248SJed Brown   ierr = MatMult(Arhs,X,F);CHKERRQ(ierr);
24450e4ef248SJed Brown   PetscFunctionReturn(0);
24460e4ef248SJed Brown }
24470e4ef248SJed Brown 
24480e4ef248SJed Brown #undef __FUNCT__
24490e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
24500e4ef248SJed Brown /*@C
24510e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
24520e4ef248SJed Brown 
24530e4ef248SJed Brown    Collective on TS
24540e4ef248SJed Brown 
24550e4ef248SJed Brown    Input Arguments:
24560e4ef248SJed Brown +  ts - time stepping context
24570e4ef248SJed Brown .  t - time at which to evaluate
24580e4ef248SJed Brown .  X - state at which to evaluate
24590e4ef248SJed Brown -  ctx - context
24600e4ef248SJed Brown 
24610e4ef248SJed Brown    Output Arguments:
24620e4ef248SJed Brown +  A - pointer to operator
24630e4ef248SJed Brown .  B - pointer to preconditioning matrix
24640e4ef248SJed Brown -  flg - matrix structure flag
24650e4ef248SJed Brown 
24660e4ef248SJed Brown    Level: intermediate
24670e4ef248SJed Brown 
24680e4ef248SJed Brown    Notes:
24690e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
24700e4ef248SJed Brown 
24710e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
24720e4ef248SJed Brown @*/
24730e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx)
24740e4ef248SJed Brown {
24750e4ef248SJed Brown 
24760e4ef248SJed Brown   PetscFunctionBegin;
24770e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
24780e4ef248SJed Brown   PetscFunctionReturn(0);
24790e4ef248SJed Brown }
24800e4ef248SJed Brown 
24810026cea9SSean Farley #undef __FUNCT__
24820026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
24830026cea9SSean Farley /*@C
24840026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
24850026cea9SSean Farley 
24860026cea9SSean Farley    Collective on TS
24870026cea9SSean Farley 
24880026cea9SSean Farley    Input Arguments:
24890026cea9SSean Farley +  ts - time stepping context
24900026cea9SSean Farley .  t - time at which to evaluate
24910026cea9SSean Farley .  X - state at which to evaluate
24920026cea9SSean Farley .  Xdot - time derivative of state vector
24930026cea9SSean Farley -  ctx - context
24940026cea9SSean Farley 
24950026cea9SSean Farley    Output Arguments:
24960026cea9SSean Farley .  F - left hand side
24970026cea9SSean Farley 
24980026cea9SSean Farley    Level: intermediate
24990026cea9SSean Farley 
25000026cea9SSean Farley    Notes:
25010026cea9SSean 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
25020026cea9SSean Farley    user is required to write their own TSComputeIFunction.
25030026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
25040026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
25050026cea9SSean Farley 
25060026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
25070026cea9SSean Farley @*/
25080026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx)
25090026cea9SSean Farley {
25100026cea9SSean Farley   PetscErrorCode ierr;
25110026cea9SSean Farley   Mat A,B;
25120026cea9SSean Farley   MatStructure flg2;
25130026cea9SSean Farley 
25140026cea9SSean Farley   PetscFunctionBegin;
25150026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
25160026cea9SSean Farley   ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
25170026cea9SSean Farley   ierr = MatMult(A,Xdot,F);CHKERRQ(ierr);
25180026cea9SSean Farley   PetscFunctionReturn(0);
25190026cea9SSean Farley }
25200026cea9SSean Farley 
25210026cea9SSean Farley #undef __FUNCT__
25220026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
25230026cea9SSean Farley /*@C
25240026cea9SSean Farley    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
25250026cea9SSean Farley 
25260026cea9SSean Farley    Collective on TS
25270026cea9SSean Farley 
25280026cea9SSean Farley    Input Arguments:
25290026cea9SSean Farley +  ts - time stepping context
25300026cea9SSean Farley .  t - time at which to evaluate
25310026cea9SSean Farley .  X - state at which to evaluate
25320026cea9SSean Farley .  Xdot - time derivative of state vector
25330026cea9SSean Farley .  shift - shift to apply
25340026cea9SSean Farley -  ctx - context
25350026cea9SSean Farley 
25360026cea9SSean Farley    Output Arguments:
25370026cea9SSean Farley +  A - pointer to operator
25380026cea9SSean Farley .  B - pointer to preconditioning matrix
25390026cea9SSean Farley -  flg - matrix structure flag
25400026cea9SSean Farley 
25410026cea9SSean Farley    Level: intermediate
25420026cea9SSean Farley 
25430026cea9SSean Farley    Notes:
25440026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
25450026cea9SSean Farley 
25460026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
25470026cea9SSean Farley @*/
25480026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
25490026cea9SSean Farley {
25500026cea9SSean Farley 
25510026cea9SSean Farley   PetscFunctionBegin;
25520026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
25530026cea9SSean Farley   PetscFunctionReturn(0);
25540026cea9SSean Farley }
25550026cea9SSean Farley 
25564af1b03aSJed Brown 
25574af1b03aSJed Brown #undef __FUNCT__
25584af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
25594af1b03aSJed Brown /*@
25604af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
25614af1b03aSJed Brown 
25624af1b03aSJed Brown    Not Collective
25634af1b03aSJed Brown 
25644af1b03aSJed Brown    Input Parameter:
25654af1b03aSJed Brown .  ts - the TS context
25664af1b03aSJed Brown 
25674af1b03aSJed Brown    Output Parameter:
25684af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
25694af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
25704af1b03aSJed Brown 
25714af1b03aSJed Brown    Level: intermediate
25724af1b03aSJed Brown 
2573cd652676SJed Brown    Notes:
2574cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
25754af1b03aSJed Brown 
25764af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
25774af1b03aSJed Brown 
25784af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
25794af1b03aSJed Brown @*/
25804af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
25814af1b03aSJed Brown {
25824af1b03aSJed Brown   PetscFunctionBegin;
25834af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25844af1b03aSJed Brown   PetscValidPointer(reason,2);
25854af1b03aSJed Brown   *reason = ts->reason;
25864af1b03aSJed Brown   PetscFunctionReturn(0);
25874af1b03aSJed Brown }
25884af1b03aSJed Brown 
2589325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2590c6db04a5SJed Brown #include <mex.h>
2591325fc9f4SBarry Smith 
2592325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
2593325fc9f4SBarry Smith 
2594325fc9f4SBarry Smith #undef __FUNCT__
2595325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
2596325fc9f4SBarry Smith /*
2597325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
2598325fc9f4SBarry Smith                          TSSetFunctionMatlab().
2599325fc9f4SBarry Smith 
2600325fc9f4SBarry Smith    Collective on TS
2601325fc9f4SBarry Smith 
2602325fc9f4SBarry Smith    Input Parameters:
2603325fc9f4SBarry Smith +  snes - the TS context
2604325fc9f4SBarry Smith -  x - input vector
2605325fc9f4SBarry Smith 
2606325fc9f4SBarry Smith    Output Parameter:
2607325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
2608325fc9f4SBarry Smith 
2609325fc9f4SBarry Smith    Notes:
2610325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
2611325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
2612325fc9f4SBarry Smith    themselves.
2613325fc9f4SBarry Smith 
2614325fc9f4SBarry Smith    Level: developer
2615325fc9f4SBarry Smith 
2616325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2617325fc9f4SBarry Smith 
2618325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2619325fc9f4SBarry Smith */
26207087cfbeSBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx)
2621325fc9f4SBarry Smith {
2622325fc9f4SBarry Smith   PetscErrorCode   ierr;
2623325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2624325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
2625325fc9f4SBarry Smith   mxArray          *plhs[1],*prhs[7];
2626325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
2627325fc9f4SBarry Smith 
2628325fc9f4SBarry Smith   PetscFunctionBegin;
2629325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2630325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2631325fc9f4SBarry Smith   PetscValidHeaderSpecific(xdot,VEC_CLASSID,4);
2632325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
2633325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,x,3);
2634325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
2635325fc9f4SBarry Smith 
2636325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2637325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2638880f3077SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr);
2639325fc9f4SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
2640325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2641325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
2642325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2643325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2644325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
2645325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
2646325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
2647325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
2648325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2649325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2650325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2651325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2652325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2653325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2654325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2655325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2656325fc9f4SBarry Smith   PetscFunctionReturn(0);
2657325fc9f4SBarry Smith }
2658325fc9f4SBarry Smith 
2659325fc9f4SBarry Smith 
2660325fc9f4SBarry Smith #undef __FUNCT__
2661325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
2662325fc9f4SBarry Smith /*
2663325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
2664325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
2665e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
2666325fc9f4SBarry Smith 
2667325fc9f4SBarry Smith    Logically Collective on TS
2668325fc9f4SBarry Smith 
2669325fc9f4SBarry Smith    Input Parameters:
2670325fc9f4SBarry Smith +  ts - the TS context
2671325fc9f4SBarry Smith -  func - function evaluation routine
2672325fc9f4SBarry Smith 
2673325fc9f4SBarry Smith    Calling sequence of func:
2674325fc9f4SBarry Smith $    func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx);
2675325fc9f4SBarry Smith 
2676325fc9f4SBarry Smith    Level: beginner
2677325fc9f4SBarry Smith 
2678325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2679325fc9f4SBarry Smith 
2680325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2681325fc9f4SBarry Smith */
2682cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
2683325fc9f4SBarry Smith {
2684325fc9f4SBarry Smith   PetscErrorCode  ierr;
2685325fc9f4SBarry Smith   TSMatlabContext *sctx;
2686325fc9f4SBarry Smith 
2687325fc9f4SBarry Smith   PetscFunctionBegin;
2688325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2689325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2690325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2691325fc9f4SBarry Smith   /*
2692325fc9f4SBarry Smith      This should work, but it doesn't
2693325fc9f4SBarry Smith   sctx->ctx = ctx;
2694325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2695325fc9f4SBarry Smith   */
2696325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2697cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
2698325fc9f4SBarry Smith   PetscFunctionReturn(0);
2699325fc9f4SBarry Smith }
2700325fc9f4SBarry Smith 
2701325fc9f4SBarry Smith #undef __FUNCT__
2702325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
2703325fc9f4SBarry Smith /*
2704325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
2705325fc9f4SBarry Smith                          TSSetJacobianMatlab().
2706325fc9f4SBarry Smith 
2707325fc9f4SBarry Smith    Collective on TS
2708325fc9f4SBarry Smith 
2709325fc9f4SBarry Smith    Input Parameters:
2710cdcf91faSSean Farley +  ts - the TS context
2711325fc9f4SBarry Smith .  x - input vector
2712325fc9f4SBarry Smith .  A, B - the matrices
2713325fc9f4SBarry Smith -  ctx - user context
2714325fc9f4SBarry Smith 
2715325fc9f4SBarry Smith    Output Parameter:
2716325fc9f4SBarry Smith .  flag - structure of the matrix
2717325fc9f4SBarry Smith 
2718325fc9f4SBarry Smith    Level: developer
2719325fc9f4SBarry Smith 
2720325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2721325fc9f4SBarry Smith 
2722325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2723325fc9f4SBarry Smith @*/
2724cdcf91faSSean Farley PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
2725325fc9f4SBarry Smith {
2726325fc9f4SBarry Smith   PetscErrorCode  ierr;
2727325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2728325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
2729325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
2730325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
2731325fc9f4SBarry Smith 
2732325fc9f4SBarry Smith   PetscFunctionBegin;
2733cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2734325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2735325fc9f4SBarry Smith 
2736325fc9f4SBarry Smith   /* call Matlab function in ctx with arguments x and y */
2737325fc9f4SBarry Smith 
2738cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
2739325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2740325fc9f4SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr);
2741325fc9f4SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
2742325fc9f4SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
2743325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2744325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
2745325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2746325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2747325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
2748325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
2749325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
2750325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
2751325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
2752325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
2753325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2754325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
2755325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2756325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2757325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2758325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2759325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2760325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2761325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
2762325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
2763325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2764325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
2765325fc9f4SBarry Smith   PetscFunctionReturn(0);
2766325fc9f4SBarry Smith }
2767325fc9f4SBarry Smith 
2768325fc9f4SBarry Smith 
2769325fc9f4SBarry Smith #undef __FUNCT__
2770325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
2771325fc9f4SBarry Smith /*
2772325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
2773e3c5b3baSBarry 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
2774325fc9f4SBarry Smith 
2775325fc9f4SBarry Smith    Logically Collective on TS
2776325fc9f4SBarry Smith 
2777325fc9f4SBarry Smith    Input Parameters:
2778cdcf91faSSean Farley +  ts - the TS context
2779325fc9f4SBarry Smith .  A,B - Jacobian matrices
2780325fc9f4SBarry Smith .  func - function evaluation routine
2781325fc9f4SBarry Smith -  ctx - user context
2782325fc9f4SBarry Smith 
2783325fc9f4SBarry Smith    Calling sequence of func:
2784cdcf91faSSean Farley $    flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx);
2785325fc9f4SBarry Smith 
2786325fc9f4SBarry Smith 
2787325fc9f4SBarry Smith    Level: developer
2788325fc9f4SBarry Smith 
2789325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2790325fc9f4SBarry Smith 
2791325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2792325fc9f4SBarry Smith */
2793cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
2794325fc9f4SBarry Smith {
2795325fc9f4SBarry Smith   PetscErrorCode    ierr;
2796325fc9f4SBarry Smith   TSMatlabContext *sctx;
2797325fc9f4SBarry Smith 
2798325fc9f4SBarry Smith   PetscFunctionBegin;
2799325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2800325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2801325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2802325fc9f4SBarry Smith   /*
2803325fc9f4SBarry Smith      This should work, but it doesn't
2804325fc9f4SBarry Smith   sctx->ctx = ctx;
2805325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2806325fc9f4SBarry Smith   */
2807325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2808cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
2809325fc9f4SBarry Smith   PetscFunctionReturn(0);
2810325fc9f4SBarry Smith }
2811325fc9f4SBarry Smith 
2812b5b1a830SBarry Smith #undef __FUNCT__
2813b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
2814b5b1a830SBarry Smith /*
2815b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
2816b5b1a830SBarry Smith 
2817b5b1a830SBarry Smith    Collective on TS
2818b5b1a830SBarry Smith 
2819b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2820b5b1a830SBarry Smith @*/
2821cdcf91faSSean Farley PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx)
2822b5b1a830SBarry Smith {
2823b5b1a830SBarry Smith   PetscErrorCode  ierr;
2824b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2825a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
2826b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
2827b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
2828b5b1a830SBarry Smith 
2829b5b1a830SBarry Smith   PetscFunctionBegin;
2830cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2831b5b1a830SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,4);
2832b5b1a830SBarry Smith 
2833cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
2834b5b1a830SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2835b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2836b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
2837b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
2838b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
2839b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
2840b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
2841b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
2842b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2843b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
2844b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
2845b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
2846b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
2847b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
2848b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
2849b5b1a830SBarry Smith   PetscFunctionReturn(0);
2850b5b1a830SBarry Smith }
2851b5b1a830SBarry Smith 
2852b5b1a830SBarry Smith 
2853b5b1a830SBarry Smith #undef __FUNCT__
2854b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
2855b5b1a830SBarry Smith /*
2856b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
2857b5b1a830SBarry Smith 
2858b5b1a830SBarry Smith    Level: developer
2859b5b1a830SBarry Smith 
2860b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
2861b5b1a830SBarry Smith 
2862b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2863b5b1a830SBarry Smith */
2864cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
2865b5b1a830SBarry Smith {
2866b5b1a830SBarry Smith   PetscErrorCode    ierr;
2867b5b1a830SBarry Smith   TSMatlabContext *sctx;
2868b5b1a830SBarry Smith 
2869b5b1a830SBarry Smith   PetscFunctionBegin;
2870b5b1a830SBarry Smith   /* currently sctx is memory bleed */
2871b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2872b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2873b5b1a830SBarry Smith   /*
2874b5b1a830SBarry Smith      This should work, but it doesn't
2875b5b1a830SBarry Smith   sctx->ctx = ctx;
2876b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2877b5b1a830SBarry Smith   */
2878b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2879cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
2880b5b1a830SBarry Smith   PetscFunctionReturn(0);
2881b5b1a830SBarry Smith }
2882b5b1a830SBarry Smith 
2883325fc9f4SBarry Smith #endif
2884