xref: /petsc/src/ts/interface/ts.c (revision 4d7d938efd3480b430fca57a45eddde7b805a059)
163dd3a1aSKris Buschelman 
2c6db04a5SJed Brown #include <private/tsimpl.h>        /*I "petscts.h"  I*/
3d763cef2SBarry Smith 
4d5ba7fb7SMatthew Knepley /* Logging support */
57087cfbeSBarry Smith PetscClassId  TS_CLASSID;
6166c7f25SBarry Smith PetscLogEvent  TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
7d405a339SMatthew Knepley 
84a2ae208SSatish Balay #undef __FUNCT__
9bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
10bdad233fSMatthew Knepley /*
11bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
12bdad233fSMatthew Knepley 
13bdad233fSMatthew Knepley   Collective on TS
14bdad233fSMatthew Knepley 
15bdad233fSMatthew Knepley   Input Parameter:
16bdad233fSMatthew Knepley . ts - The ts
17bdad233fSMatthew Knepley 
18bdad233fSMatthew Knepley   Level: intermediate
19bdad233fSMatthew Knepley 
20bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
21bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
22bdad233fSMatthew Knepley */
236849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
24bdad233fSMatthew Knepley {
25ace3abfcSBarry Smith   PetscBool      opt;
262fc52814SBarry Smith   const char     *defaultType;
27bdad233fSMatthew Knepley   char           typeName[256];
28dfbe8321SBarry Smith   PetscErrorCode ierr;
29bdad233fSMatthew Knepley 
30bdad233fSMatthew Knepley   PetscFunctionBegin;
317adad957SLisandro Dalcin   if (((PetscObject)ts)->type_name) {
327adad957SLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
33bdad233fSMatthew Knepley   } else {
349596e0b4SJed Brown     defaultType = TSEULER;
35bdad233fSMatthew Knepley   }
36bdad233fSMatthew Knepley 
37cce0b1b2SLisandro Dalcin   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
38bdad233fSMatthew Knepley   ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
39a7cc72afSBarry Smith   if (opt) {
40bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
41bdad233fSMatthew Knepley   } else {
42bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
43bdad233fSMatthew Knepley   }
44bdad233fSMatthew Knepley   PetscFunctionReturn(0);
45bdad233fSMatthew Knepley }
46bdad233fSMatthew Knepley 
47bdad233fSMatthew Knepley #undef __FUNCT__
48bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
49bdad233fSMatthew Knepley /*@
50bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
51bdad233fSMatthew Knepley 
52bdad233fSMatthew Knepley    Collective on TS
53bdad233fSMatthew Knepley 
54bdad233fSMatthew Knepley    Input Parameter:
55bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
56bdad233fSMatthew Knepley 
57bdad233fSMatthew Knepley    Options Database Keys:
584d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
59bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
60bdad233fSMatthew Knepley .  -ts_max_time time - maximum time to compute to
61bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
62bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
63a6570f20SBarry Smith -  -ts_monitor_draw - plot information at each timestep
64bdad233fSMatthew Knepley 
65bdad233fSMatthew Knepley    Level: beginner
66bdad233fSMatthew Knepley 
67bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
68bdad233fSMatthew Knepley 
69a313700dSBarry Smith .seealso: TSGetType()
70bdad233fSMatthew Knepley @*/
717087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
72bdad233fSMatthew Knepley {
73bdad233fSMatthew Knepley   PetscReal      dt;
74ace3abfcSBarry Smith   PetscBool      opt,flg;
75dfbe8321SBarry Smith   PetscErrorCode ierr;
76649052a6SBarry Smith   PetscViewer    monviewer;
77eabae89aSBarry Smith   char           monfilename[PETSC_MAX_PATH_LEN];
78089b2837SJed Brown   SNES           snes;
79bdad233fSMatthew Knepley 
80bdad233fSMatthew Knepley   PetscFunctionBegin;
810700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
827adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)ts)->comm, ((PetscObject)ts)->prefix, "Time step options", "TS");CHKERRQ(ierr);
83bdad233fSMatthew Knepley 
84bdad233fSMatthew Knepley     /* Handle generic TS options */
85bdad233fSMatthew Knepley     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
86bdad233fSMatthew Knepley     ierr = PetscOptionsReal("-ts_max_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
87bdad233fSMatthew Knepley     ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetInitialTime", ts->ptime, &ts->ptime, PETSC_NULL);CHKERRQ(ierr);
88bdad233fSMatthew Knepley     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetInitialTimeStep",ts->initial_time_step,&dt,&opt);CHKERRQ(ierr);
893daf2de8SJed Brown     if (opt) {ierr = TSSetInitialTimeStep(ts,ts->ptime,dt);CHKERRQ(ierr);}
90193ac0bcSJed Brown     ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","",ts->max_snes_failures,&ts->max_snes_failures,PETSC_NULL);CHKERRQ(ierr);
91193ac0bcSJed Brown     ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections","",ts->max_reject,&ts->max_reject,PETSC_NULL);CHKERRQ(ierr);
92193ac0bcSJed Brown     ierr = PetscOptionsBool("-ts_error_if_step_failed","Error if no step succeeds","",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr);
93bdad233fSMatthew Knepley 
94bdad233fSMatthew Knepley     /* Monitor options */
95a6570f20SBarry Smith     ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
96eabae89aSBarry Smith     if (flg) {
97649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr);
98649052a6SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
99bdad233fSMatthew Knepley     }
1005180491cSLisandro Dalcin     ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1015180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1025180491cSLisandro Dalcin 
10390d69ab7SBarry Smith     opt  = PETSC_FALSE;
104acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
105a7cc72afSBarry Smith     if (opt) {
106a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
107bdad233fSMatthew Knepley     }
10890d69ab7SBarry Smith     opt  = PETSC_FALSE;
109acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
110a7cc72afSBarry Smith     if (opt) {
111a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolution,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
112bdad233fSMatthew Knepley     }
113bdad233fSMatthew Knepley 
114bdad233fSMatthew Knepley     /* Handle TS type options */
115bdad233fSMatthew Knepley     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
116bdad233fSMatthew Knepley 
117bdad233fSMatthew Knepley     /* Handle specific TS options */
118abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
119bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
120bdad233fSMatthew Knepley     }
1215d973c19SBarry Smith 
1225d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
1235d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
124bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
125bdad233fSMatthew Knepley 
126089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
127bdad233fSMatthew Knepley   /* Handle subobject options */
128089b2837SJed Brown   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
129089b2837SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
130bdad233fSMatthew Knepley   PetscFunctionReturn(0);
131bdad233fSMatthew Knepley }
132bdad233fSMatthew Knepley 
133bdad233fSMatthew Knepley #undef __FUNCT__
1344a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
135a7a1495cSBarry Smith /*@
1368c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
137a7a1495cSBarry Smith       set with TSSetRHSJacobian().
138a7a1495cSBarry Smith 
139a7a1495cSBarry Smith    Collective on TS and Vec
140a7a1495cSBarry Smith 
141a7a1495cSBarry Smith    Input Parameters:
142316643e7SJed Brown +  ts - the TS context
143a7a1495cSBarry Smith .  t - current timestep
144a7a1495cSBarry Smith -  x - input vector
145a7a1495cSBarry Smith 
146a7a1495cSBarry Smith    Output Parameters:
147a7a1495cSBarry Smith +  A - Jacobian matrix
148a7a1495cSBarry Smith .  B - optional preconditioning matrix
149a7a1495cSBarry Smith -  flag - flag indicating matrix structure
150a7a1495cSBarry Smith 
151a7a1495cSBarry Smith    Notes:
152a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
153a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
154a7a1495cSBarry Smith 
15594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
156a7a1495cSBarry Smith    flag parameter.
157a7a1495cSBarry Smith 
158a7a1495cSBarry Smith    Level: developer
159a7a1495cSBarry Smith 
160a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
161a7a1495cSBarry Smith 
16294b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
163a7a1495cSBarry Smith @*/
1647087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
165a7a1495cSBarry Smith {
166dfbe8321SBarry Smith   PetscErrorCode ierr;
1670e4ef248SJed Brown   PetscInt Xstate;
168a7a1495cSBarry Smith 
169a7a1495cSBarry Smith   PetscFunctionBegin;
1700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1710700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
172c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
1730e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
1740e4ef248SJed Brown   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) {
1750e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
1760e4ef248SJed Brown     PetscFunctionReturn(0);
177f8ede8e7SJed Brown   }
178d90be118SSean Farley 
1793b5f76d0SSean Farley   if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
180d90be118SSean Farley 
1813b5f76d0SSean Farley   if (ts->userops->rhsjacobian) {
182d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
183a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
184a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
1853b5f76d0SSean Farley     ierr = (*ts->userops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr);
186a7a1495cSBarry Smith     PetscStackPop;
187d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
188a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
1890700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
1900700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
191ef66eb69SBarry Smith   } else {
192214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
193214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
194214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
195ef66eb69SBarry Smith   }
1960e4ef248SJed Brown   ts->rhsjacobian.time = t;
1970e4ef248SJed Brown   ts->rhsjacobian.X = X;
1980e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
1990e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
200a7a1495cSBarry Smith   PetscFunctionReturn(0);
201a7a1495cSBarry Smith }
202a7a1495cSBarry Smith 
2034a2ae208SSatish Balay #undef __FUNCT__
2044a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
205316643e7SJed Brown /*@
206d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
207d763cef2SBarry Smith 
208316643e7SJed Brown    Collective on TS and Vec
209316643e7SJed Brown 
210316643e7SJed Brown    Input Parameters:
211316643e7SJed Brown +  ts - the TS context
212316643e7SJed Brown .  t - current time
213316643e7SJed Brown -  x - state vector
214316643e7SJed Brown 
215316643e7SJed Brown    Output Parameter:
216316643e7SJed Brown .  y - right hand side
217316643e7SJed Brown 
218316643e7SJed Brown    Note:
219316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
220316643e7SJed Brown    is used internally within the nonlinear solvers.
221316643e7SJed Brown 
222316643e7SJed Brown    Level: developer
223316643e7SJed Brown 
224316643e7SJed Brown .keywords: TS, compute
225316643e7SJed Brown 
226316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
227316643e7SJed Brown @*/
228dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
229d763cef2SBarry Smith {
230dfbe8321SBarry Smith   PetscErrorCode ierr;
231d763cef2SBarry Smith 
232d763cef2SBarry Smith   PetscFunctionBegin;
2330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2340700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2350700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
236d763cef2SBarry Smith 
2373b5f76d0SSean Farley   if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
238d763cef2SBarry Smith 
239d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
2403b5f76d0SSean Farley   if (ts->userops->rhsfunction) {
241d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
2423b5f76d0SSean Farley     ierr = (*ts->userops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr);
243d763cef2SBarry Smith     PetscStackPop;
244214bc6a2SJed Brown   } else {
245214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
246b2cd27e8SJed Brown   }
24744a41b28SSean Farley 
248d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
249d763cef2SBarry Smith   PetscFunctionReturn(0);
250d763cef2SBarry Smith }
251d763cef2SBarry Smith 
2524a2ae208SSatish Balay #undef __FUNCT__
253214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
254214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
255214bc6a2SJed Brown {
256214bc6a2SJed Brown   PetscErrorCode ierr;
257214bc6a2SJed Brown 
258214bc6a2SJed Brown   PetscFunctionBegin;
259214bc6a2SJed Brown   if (!ts->Frhs) {
260214bc6a2SJed Brown     ierr = VecDuplicate(ts->vec_sol,&ts->Frhs);CHKERRQ(ierr);
261214bc6a2SJed Brown   }
262214bc6a2SJed Brown   *Frhs = ts->Frhs;
263214bc6a2SJed Brown   PetscFunctionReturn(0);
264214bc6a2SJed Brown }
265214bc6a2SJed Brown 
266214bc6a2SJed Brown #undef __FUNCT__
267214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
268214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
269214bc6a2SJed Brown {
270214bc6a2SJed Brown   PetscErrorCode ierr;
271214bc6a2SJed Brown   Mat A,B;
272214bc6a2SJed Brown 
273214bc6a2SJed Brown   PetscFunctionBegin;
274214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
275214bc6a2SJed Brown   if (Arhs) {
276214bc6a2SJed Brown     if (!ts->Arhs) {
277214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
278214bc6a2SJed Brown     }
279214bc6a2SJed Brown     *Arhs = ts->Arhs;
280214bc6a2SJed Brown   }
281214bc6a2SJed Brown   if (Brhs) {
282214bc6a2SJed Brown     if (!ts->Brhs) {
283214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
284214bc6a2SJed Brown     }
285214bc6a2SJed Brown     *Brhs = ts->Brhs;
286214bc6a2SJed Brown   }
287214bc6a2SJed Brown   PetscFunctionReturn(0);
288214bc6a2SJed Brown }
289214bc6a2SJed Brown 
290214bc6a2SJed Brown #undef __FUNCT__
291316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
292316643e7SJed Brown /*@
293316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
294316643e7SJed Brown 
295316643e7SJed Brown    Collective on TS and Vec
296316643e7SJed Brown 
297316643e7SJed Brown    Input Parameters:
298316643e7SJed Brown +  ts - the TS context
299316643e7SJed Brown .  t - current time
300316643e7SJed Brown .  X - state vector
301214bc6a2SJed Brown .  Xdot - time derivative of state vector
302214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
303316643e7SJed Brown 
304316643e7SJed Brown    Output Parameter:
305316643e7SJed Brown .  Y - right hand side
306316643e7SJed Brown 
307316643e7SJed Brown    Note:
308316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
309316643e7SJed Brown    is used internally within the nonlinear solvers.
310316643e7SJed Brown 
311316643e7SJed Brown    If the user did did not write their equations in implicit form, this
312316643e7SJed Brown    function recasts them in implicit form.
313316643e7SJed Brown 
314316643e7SJed Brown    Level: developer
315316643e7SJed Brown 
316316643e7SJed Brown .keywords: TS, compute
317316643e7SJed Brown 
318316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
319316643e7SJed Brown @*/
320214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex)
321316643e7SJed Brown {
322316643e7SJed Brown   PetscErrorCode ierr;
323316643e7SJed Brown 
324316643e7SJed Brown   PetscFunctionBegin;
3250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3260700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3270700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
3280700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
329316643e7SJed Brown 
330d90be118SSean Farley   if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
331d90be118SSean Farley 
332316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
3333b5f76d0SSean Farley   if (ts->userops->ifunction) {
334316643e7SJed Brown     PetscStackPush("TS user implicit function");
3353b5f76d0SSean Farley     ierr = (*ts->userops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr);
336316643e7SJed Brown     PetscStackPop;
337214bc6a2SJed Brown   }
338214bc6a2SJed Brown   if (imex) {
3393b5f76d0SSean Farley     if (!ts->userops->ifunction) {ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);}
340316643e7SJed Brown   } else {
3413b5f76d0SSean Farley     if (!ts->userops->ifunction) {
342089b2837SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr);
343ace68cafSJed Brown       ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr);
344214bc6a2SJed Brown     } else {
345214bc6a2SJed Brown       Vec Frhs;
346214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
347214bc6a2SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr);
348214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
349316643e7SJed Brown     }
3504a6899ffSJed Brown   }
351316643e7SJed Brown   ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
352316643e7SJed Brown   PetscFunctionReturn(0);
353316643e7SJed Brown }
354316643e7SJed Brown 
355316643e7SJed Brown #undef __FUNCT__
356316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
357316643e7SJed Brown /*@
358316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
359316643e7SJed Brown 
360316643e7SJed Brown    Collective on TS and Vec
361316643e7SJed Brown 
362316643e7SJed Brown    Input
363316643e7SJed Brown       Input Parameters:
364316643e7SJed Brown +  ts - the TS context
365316643e7SJed Brown .  t - current timestep
366316643e7SJed Brown .  X - state vector
367316643e7SJed Brown .  Xdot - time derivative of state vector
368214bc6a2SJed Brown .  shift - shift to apply, see note below
369214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
370316643e7SJed Brown 
371316643e7SJed Brown    Output Parameters:
372316643e7SJed Brown +  A - Jacobian matrix
373316643e7SJed Brown .  B - optional preconditioning matrix
374316643e7SJed Brown -  flag - flag indicating matrix structure
375316643e7SJed Brown 
376316643e7SJed Brown    Notes:
377316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
378316643e7SJed Brown 
379316643e7SJed Brown    dF/dX + shift*dF/dXdot
380316643e7SJed Brown 
381316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
382316643e7SJed Brown    is used internally within the nonlinear solvers.
383316643e7SJed Brown 
384316643e7SJed Brown    Level: developer
385316643e7SJed Brown 
386316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
387316643e7SJed Brown 
388316643e7SJed Brown .seealso:  TSSetIJacobian()
38963495f91SJed Brown @*/
390214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
391316643e7SJed Brown {
3920026cea9SSean Farley   PetscInt Xstate, Xdotstate;
393316643e7SJed Brown   PetscErrorCode ierr;
394316643e7SJed Brown 
395316643e7SJed Brown   PetscFunctionBegin;
3960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3970700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3980700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
399316643e7SJed Brown   PetscValidPointer(A,6);
4000700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
401316643e7SJed Brown   PetscValidPointer(B,7);
4020700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
403316643e7SJed Brown   PetscValidPointer(flg,8);
4040026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
4050026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr);
4060026cea9SSean 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))) {
4070026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
4080026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4090026cea9SSean Farley     PetscFunctionReturn(0);
4100026cea9SSean Farley   }
411316643e7SJed Brown 
4123b5f76d0SSean Farley   if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
413316643e7SJed Brown 
4144e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
415316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
4163b5f76d0SSean Farley   if (ts->userops->ijacobian) {
417316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
4183b5f76d0SSean Farley     ierr = (*ts->userops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr);
419316643e7SJed Brown     PetscStackPop;
420214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
421214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
422214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
4234a6899ffSJed Brown   }
424214bc6a2SJed Brown   if (imex) {
4253b5f76d0SSean Farley     if (!ts->userops->ijacobian) {  /* system was written as Xdot = F(t,X) */
426214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
427214bc6a2SJed Brown       ierr = MatShift(*A,1.0);CHKERRQ(ierr);
428214bc6a2SJed Brown       if (*A != *B) {
429214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
430214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
431214bc6a2SJed Brown       }
432214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
433214bc6a2SJed Brown     }
434214bc6a2SJed Brown   } else {
4353b5f76d0SSean Farley     if (!ts->userops->ijacobian) {
436214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr);
437214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
438214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
439316643e7SJed Brown       if (*A != *B) {
440316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
441316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
442316643e7SJed Brown       }
4433b5f76d0SSean Farley     } else if (ts->userops->rhsjacobian) {
444214bc6a2SJed Brown       Mat Arhs,Brhs;
445214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
446214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
447214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
448214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
449214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
450214bc6a2SJed Brown       if (*A != *B) {
451214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
452214bc6a2SJed Brown       }
453214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
454214bc6a2SJed Brown     }
455316643e7SJed Brown   }
4560026cea9SSean Farley 
4570026cea9SSean Farley   ts->ijacobian.time = t;
4580026cea9SSean Farley   ts->ijacobian.X = X;
4590026cea9SSean Farley   ts->ijacobian.Xdot = Xdot;
4600026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr);
4610026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
4620026cea9SSean Farley   ts->ijacobian.shift = shift;
4630026cea9SSean Farley   ts->ijacobian.imex = imex;
4640026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
465316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
466316643e7SJed Brown   PetscFunctionReturn(0);
467316643e7SJed Brown }
468316643e7SJed Brown 
469316643e7SJed Brown #undef __FUNCT__
4704a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
471d763cef2SBarry Smith /*@C
472d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
473d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
474d763cef2SBarry Smith 
4753f9fe445SBarry Smith     Logically Collective on TS
476d763cef2SBarry Smith 
477d763cef2SBarry Smith     Input Parameters:
478d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
479ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
480d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
481d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
482d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
483d763cef2SBarry Smith 
484d763cef2SBarry Smith     Calling sequence of func:
48587828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
486d763cef2SBarry Smith 
487d763cef2SBarry Smith +   t - current timestep
488d763cef2SBarry Smith .   u - input vector
489d763cef2SBarry Smith .   F - function vector
490d763cef2SBarry Smith -   ctx - [optional] user-defined function context
491d763cef2SBarry Smith 
492d763cef2SBarry Smith     Important:
49376f2fa84SHong Zhang     The user MUST call either this routine or TSSetMatrices().
494d763cef2SBarry Smith 
495d763cef2SBarry Smith     Level: beginner
496d763cef2SBarry Smith 
497d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
498d763cef2SBarry Smith 
49976f2fa84SHong Zhang .seealso: TSSetMatrices()
500d763cef2SBarry Smith @*/
501089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
502d763cef2SBarry Smith {
503089b2837SJed Brown   PetscErrorCode ierr;
504089b2837SJed Brown   SNES           snes;
505d763cef2SBarry Smith 
506089b2837SJed Brown   PetscFunctionBegin;
5070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
508ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
5093b5f76d0SSean Farley   if (f)   ts->userops->rhsfunction = f;
5104e684422SJed Brown   if (ctx) ts->funP             = ctx;
511089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
512089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
513d763cef2SBarry Smith   PetscFunctionReturn(0);
514d763cef2SBarry Smith }
515d763cef2SBarry Smith 
5164a2ae208SSatish Balay #undef __FUNCT__
5174a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
518d763cef2SBarry Smith /*@C
519d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
520d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
52176f2fa84SHong Zhang    Use TSSetMatrices() for linear problems.
522d763cef2SBarry Smith 
5233f9fe445SBarry Smith    Logically Collective on TS
524d763cef2SBarry Smith 
525d763cef2SBarry Smith    Input Parameters:
526d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
527d763cef2SBarry Smith .  A   - Jacobian matrix
528d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
529d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
530d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
531d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
532d763cef2SBarry Smith 
533d763cef2SBarry Smith    Calling sequence of func:
53487828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
535d763cef2SBarry Smith 
536d763cef2SBarry Smith +  t - current timestep
537d763cef2SBarry Smith .  u - input vector
538d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
539d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
540d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
54194b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
542d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
543d763cef2SBarry Smith 
544d763cef2SBarry Smith    Notes:
54594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
546d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
547d763cef2SBarry Smith 
548d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
549d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
55056335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
551d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
552d763cef2SBarry Smith    throughout the global iterations.
553d763cef2SBarry Smith 
554d763cef2SBarry Smith    Level: beginner
555d763cef2SBarry Smith 
556d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
557d763cef2SBarry Smith 
558d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(),
55976f2fa84SHong Zhang           SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices()
560d763cef2SBarry Smith 
561d763cef2SBarry Smith @*/
562089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
563d763cef2SBarry Smith {
564277b19d0SLisandro Dalcin   PetscErrorCode ierr;
565089b2837SJed Brown   SNES           snes;
566277b19d0SLisandro Dalcin 
567d763cef2SBarry Smith   PetscFunctionBegin;
5680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
569277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
570277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
571277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
572277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
573d763cef2SBarry Smith 
5743b5f76d0SSean Farley   if (f)   ts->userops->rhsjacobian = f;
575277b19d0SLisandro Dalcin   if (ctx) ts->jacP             = ctx;
576089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
577dabe61deSJed Brown   if (!ts->userops->ijacobian) {
578089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
5790e4ef248SJed Brown   }
5800e4ef248SJed Brown   if (A) {
5810e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
5820e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
5830e4ef248SJed Brown     ts->Arhs = A;
5840e4ef248SJed Brown   }
5850e4ef248SJed Brown   if (B) {
5860e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
5870e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
5880e4ef248SJed Brown     ts->Brhs = B;
5890e4ef248SJed Brown   }
590d763cef2SBarry Smith   PetscFunctionReturn(0);
591d763cef2SBarry Smith }
592d763cef2SBarry Smith 
593316643e7SJed Brown 
594316643e7SJed Brown #undef __FUNCT__
595316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
596316643e7SJed Brown /*@C
597316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
598316643e7SJed Brown 
5993f9fe445SBarry Smith    Logically Collective on TS
600316643e7SJed Brown 
601316643e7SJed Brown    Input Parameters:
602316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
603ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
604316643e7SJed Brown .  f   - the function evaluation routine
605316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
606316643e7SJed Brown 
607316643e7SJed Brown    Calling sequence of f:
608316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
609316643e7SJed Brown 
610316643e7SJed Brown +  t   - time at step/stage being solved
611316643e7SJed Brown .  u   - state vector
612316643e7SJed Brown .  u_t - time derivative of state vector
613316643e7SJed Brown .  F   - function vector
614316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
615316643e7SJed Brown 
616316643e7SJed Brown    Important:
617316643e7SJed Brown    The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices().  This routine must be used when not solving an ODE.
618316643e7SJed Brown 
619316643e7SJed Brown    Level: beginner
620316643e7SJed Brown 
621316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
622316643e7SJed Brown 
623316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian()
624316643e7SJed Brown @*/
625089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
626316643e7SJed Brown {
627089b2837SJed Brown   PetscErrorCode ierr;
628089b2837SJed Brown   SNES           snes;
629316643e7SJed Brown 
630316643e7SJed Brown   PetscFunctionBegin;
6310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
632ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
6333b5f76d0SSean Farley   if (f)   ts->userops->ifunction = f;
634089b2837SJed Brown   if (ctx) ts->funP           = ctx;
635089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
636089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
637089b2837SJed Brown   PetscFunctionReturn(0);
638089b2837SJed Brown }
639089b2837SJed Brown 
640089b2837SJed Brown #undef __FUNCT__
641089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
642089b2837SJed Brown /*@C
643089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
644089b2837SJed Brown 
645089b2837SJed Brown    Not Collective
646089b2837SJed Brown 
647089b2837SJed Brown    Input Parameter:
648089b2837SJed Brown .  ts - the TS context
649089b2837SJed Brown 
650089b2837SJed Brown    Output Parameter:
651089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
652089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
653089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
654089b2837SJed Brown 
655089b2837SJed Brown    Level: advanced
656089b2837SJed Brown 
657089b2837SJed Brown .keywords: TS, nonlinear, get, function
658089b2837SJed Brown 
659089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
660089b2837SJed Brown @*/
661089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
662089b2837SJed Brown {
663089b2837SJed Brown   PetscErrorCode ierr;
664089b2837SJed Brown   SNES snes;
665089b2837SJed Brown 
666089b2837SJed Brown   PetscFunctionBegin;
667089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
668089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
669089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
6703b5f76d0SSean Farley   if (func) *func = ts->userops->ifunction;
671089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
672089b2837SJed Brown   PetscFunctionReturn(0);
673089b2837SJed Brown }
674089b2837SJed Brown 
675089b2837SJed Brown #undef __FUNCT__
676089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
677089b2837SJed Brown /*@C
678089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
679089b2837SJed Brown 
680089b2837SJed Brown    Not Collective
681089b2837SJed Brown 
682089b2837SJed Brown    Input Parameter:
683089b2837SJed Brown .  ts - the TS context
684089b2837SJed Brown 
685089b2837SJed Brown    Output Parameter:
686089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
687089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
688089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
689089b2837SJed Brown 
690089b2837SJed Brown    Level: advanced
691089b2837SJed Brown 
692089b2837SJed Brown .keywords: TS, nonlinear, get, function
693089b2837SJed Brown 
694089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
695089b2837SJed Brown @*/
696089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
697089b2837SJed Brown {
698089b2837SJed Brown   PetscErrorCode ierr;
699089b2837SJed Brown   SNES snes;
700089b2837SJed Brown 
701089b2837SJed Brown   PetscFunctionBegin;
702089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
703089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
704089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7053b5f76d0SSean Farley   if (func) *func = ts->userops->rhsfunction;
706089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
707316643e7SJed Brown   PetscFunctionReturn(0);
708316643e7SJed Brown }
709316643e7SJed Brown 
710316643e7SJed Brown #undef __FUNCT__
711316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
712316643e7SJed Brown /*@C
713a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
714a4f0a591SBarry Smith         you provided with TSSetIFunction().
715316643e7SJed Brown 
7163f9fe445SBarry Smith    Logically Collective on TS
717316643e7SJed Brown 
718316643e7SJed Brown    Input Parameters:
719316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
720316643e7SJed Brown .  A   - Jacobian matrix
721316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
722316643e7SJed Brown .  f   - the Jacobian evaluation routine
723316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
724316643e7SJed Brown 
725316643e7SJed Brown    Calling sequence of f:
7261b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
727316643e7SJed Brown 
728316643e7SJed Brown +  t    - time at step/stage being solved
7291b4a444bSJed Brown .  U    - state vector
7301b4a444bSJed Brown .  U_t  - time derivative of state vector
731316643e7SJed Brown .  a    - shift
7321b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
733316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
734316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
735316643e7SJed Brown           structure (same as flag in KSPSetOperators())
736316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
737316643e7SJed Brown 
738316643e7SJed Brown    Notes:
739316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
740316643e7SJed Brown 
741a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
742a4f0a591SBarry 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.
743a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
744a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
745a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
746a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
747a4f0a591SBarry Smith 
748316643e7SJed Brown    Level: beginner
749316643e7SJed Brown 
750316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
751316643e7SJed Brown 
752316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian()
753316643e7SJed Brown 
754316643e7SJed Brown @*/
7557087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
756316643e7SJed Brown {
757316643e7SJed Brown   PetscErrorCode ierr;
758089b2837SJed Brown   SNES           snes;
759316643e7SJed Brown 
760316643e7SJed Brown   PetscFunctionBegin;
7610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7620700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
7630700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
764316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
765316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
7663b5f76d0SSean Farley   if (f)   ts->userops->ijacobian = f;
767316643e7SJed Brown   if (ctx) ts->jacP           = ctx;
768089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
769089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
770316643e7SJed Brown   PetscFunctionReturn(0);
771316643e7SJed Brown }
772316643e7SJed Brown 
7734a2ae208SSatish Balay #undef __FUNCT__
7744a2ae208SSatish Balay #define __FUNCT__ "TSView"
7757e2c5f70SBarry Smith /*@C
776d763cef2SBarry Smith     TSView - Prints the TS data structure.
777d763cef2SBarry Smith 
7784c49b128SBarry Smith     Collective on TS
779d763cef2SBarry Smith 
780d763cef2SBarry Smith     Input Parameters:
781d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
782d763cef2SBarry Smith -   viewer - visualization context
783d763cef2SBarry Smith 
784d763cef2SBarry Smith     Options Database Key:
785d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
786d763cef2SBarry Smith 
787d763cef2SBarry Smith     Notes:
788d763cef2SBarry Smith     The available visualization contexts include
789b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
790b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
791d763cef2SBarry Smith          output where only the first processor opens
792d763cef2SBarry Smith          the file.  All other processors send their
793d763cef2SBarry Smith          data to the first processor to print.
794d763cef2SBarry Smith 
795d763cef2SBarry Smith     The user can open an alternative visualization context with
796b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
797d763cef2SBarry Smith 
798d763cef2SBarry Smith     Level: beginner
799d763cef2SBarry Smith 
800d763cef2SBarry Smith .keywords: TS, timestep, view
801d763cef2SBarry Smith 
802b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
803d763cef2SBarry Smith @*/
8047087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
805d763cef2SBarry Smith {
806dfbe8321SBarry Smith   PetscErrorCode ierr;
807a313700dSBarry Smith   const TSType   type;
808089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
809d763cef2SBarry Smith 
810d763cef2SBarry Smith   PetscFunctionBegin;
8110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8123050cee2SBarry Smith   if (!viewer) {
8137adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
8143050cee2SBarry Smith   }
8150700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
816c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
817fd16b177SBarry Smith 
8182692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
8192692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
82032077d6dSBarry Smith   if (iascii) {
821317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
822000e7ae3SMatthew Knepley     if (ts->ops->view) {
823b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
824000e7ae3SMatthew Knepley       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
825b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
826d763cef2SBarry Smith     }
82777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
828a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
829d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
83077431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr);
831193ac0bcSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->max_snes_failures);CHKERRQ(ierr);
832d763cef2SBarry Smith     }
83377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr);
834193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
8350f5bd95cSBarry Smith   } else if (isstring) {
836a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
837b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
838d763cef2SBarry Smith   }
839b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
840089b2837SJed Brown   ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
841089b2837SJed Brown   if (!isundials && ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
842b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
843d763cef2SBarry Smith   PetscFunctionReturn(0);
844d763cef2SBarry Smith }
845d763cef2SBarry Smith 
846d763cef2SBarry Smith 
8474a2ae208SSatish Balay #undef __FUNCT__
8484a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
849b07ff414SBarry Smith /*@
850d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
851d763cef2SBarry Smith    the timesteppers.
852d763cef2SBarry Smith 
8533f9fe445SBarry Smith    Logically Collective on TS
854d763cef2SBarry Smith 
855d763cef2SBarry Smith    Input Parameters:
856d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
857d763cef2SBarry Smith -  usrP - optional user context
858d763cef2SBarry Smith 
859d763cef2SBarry Smith    Level: intermediate
860d763cef2SBarry Smith 
861d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
862d763cef2SBarry Smith 
863d763cef2SBarry Smith .seealso: TSGetApplicationContext()
864d763cef2SBarry Smith @*/
8657087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
866d763cef2SBarry Smith {
867d763cef2SBarry Smith   PetscFunctionBegin;
8680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
869d763cef2SBarry Smith   ts->user = usrP;
870d763cef2SBarry Smith   PetscFunctionReturn(0);
871d763cef2SBarry Smith }
872d763cef2SBarry Smith 
8734a2ae208SSatish Balay #undef __FUNCT__
8744a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
875b07ff414SBarry Smith /*@
876d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
877d763cef2SBarry Smith     timestepper.
878d763cef2SBarry Smith 
879d763cef2SBarry Smith     Not Collective
880d763cef2SBarry Smith 
881d763cef2SBarry Smith     Input Parameter:
882d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
883d763cef2SBarry Smith 
884d763cef2SBarry Smith     Output Parameter:
885d763cef2SBarry Smith .   usrP - user context
886d763cef2SBarry Smith 
887d763cef2SBarry Smith     Level: intermediate
888d763cef2SBarry Smith 
889d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
890d763cef2SBarry Smith 
891d763cef2SBarry Smith .seealso: TSSetApplicationContext()
892d763cef2SBarry Smith @*/
893e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
894d763cef2SBarry Smith {
895d763cef2SBarry Smith   PetscFunctionBegin;
8960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
897e71120c6SJed Brown   *(void**)usrP = ts->user;
898d763cef2SBarry Smith   PetscFunctionReturn(0);
899d763cef2SBarry Smith }
900d763cef2SBarry Smith 
9014a2ae208SSatish Balay #undef __FUNCT__
9024a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
903d763cef2SBarry Smith /*@
904d763cef2SBarry Smith    TSGetTimeStepNumber - Gets the current number of timesteps.
905d763cef2SBarry Smith 
906d763cef2SBarry Smith    Not Collective
907d763cef2SBarry Smith 
908d763cef2SBarry Smith    Input Parameter:
909d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
910d763cef2SBarry Smith 
911d763cef2SBarry Smith    Output Parameter:
912d763cef2SBarry Smith .  iter - number steps so far
913d763cef2SBarry Smith 
914d763cef2SBarry Smith    Level: intermediate
915d763cef2SBarry Smith 
916d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
917d763cef2SBarry Smith @*/
9187087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
919d763cef2SBarry Smith {
920d763cef2SBarry Smith   PetscFunctionBegin;
9210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9224482741eSBarry Smith   PetscValidIntPointer(iter,2);
923d763cef2SBarry Smith   *iter = ts->steps;
924d763cef2SBarry Smith   PetscFunctionReturn(0);
925d763cef2SBarry Smith }
926d763cef2SBarry Smith 
9274a2ae208SSatish Balay #undef __FUNCT__
9284a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
929d763cef2SBarry Smith /*@
930d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
931d763cef2SBarry Smith    as well as the initial time.
932d763cef2SBarry Smith 
9333f9fe445SBarry Smith    Logically Collective on TS
934d763cef2SBarry Smith 
935d763cef2SBarry Smith    Input Parameters:
936d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
937d763cef2SBarry Smith .  initial_time - the initial time
938d763cef2SBarry Smith -  time_step - the size of the timestep
939d763cef2SBarry Smith 
940d763cef2SBarry Smith    Level: intermediate
941d763cef2SBarry Smith 
942d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
943d763cef2SBarry Smith 
944d763cef2SBarry Smith .keywords: TS, set, initial, timestep
945d763cef2SBarry Smith @*/
9467087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
947d763cef2SBarry Smith {
948e144a568SJed Brown   PetscErrorCode ierr;
949e144a568SJed Brown 
950d763cef2SBarry Smith   PetscFunctionBegin;
9510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
952e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
953d763cef2SBarry Smith   ts->initial_time_step = time_step;
954d763cef2SBarry Smith   ts->ptime             = initial_time;
955d763cef2SBarry Smith   PetscFunctionReturn(0);
956d763cef2SBarry Smith }
957d763cef2SBarry Smith 
9584a2ae208SSatish Balay #undef __FUNCT__
9594a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
960d763cef2SBarry Smith /*@
961d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
962d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
963d763cef2SBarry Smith 
9643f9fe445SBarry Smith    Logically Collective on TS
965d763cef2SBarry Smith 
966d763cef2SBarry Smith    Input Parameters:
967d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
968d763cef2SBarry Smith -  time_step - the size of the timestep
969d763cef2SBarry Smith 
970d763cef2SBarry Smith    Level: intermediate
971d763cef2SBarry Smith 
972d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
973d763cef2SBarry Smith 
974d763cef2SBarry Smith .keywords: TS, set, timestep
975d763cef2SBarry Smith @*/
9767087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
977d763cef2SBarry Smith {
978d763cef2SBarry Smith   PetscFunctionBegin;
9790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
980c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
981d763cef2SBarry Smith   ts->time_step      = time_step;
982193ac0bcSJed Brown   ts->next_time_step = time_step;
983d763cef2SBarry Smith   PetscFunctionReturn(0);
984d763cef2SBarry Smith }
985d763cef2SBarry Smith 
9864a2ae208SSatish Balay #undef __FUNCT__
9874a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
988d763cef2SBarry Smith /*@
989d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
990d763cef2SBarry Smith 
991d763cef2SBarry Smith    Not Collective
992d763cef2SBarry Smith 
993d763cef2SBarry Smith    Input Parameter:
994d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
995d763cef2SBarry Smith 
996d763cef2SBarry Smith    Output Parameter:
997d763cef2SBarry Smith .  dt - the current timestep size
998d763cef2SBarry Smith 
999d763cef2SBarry Smith    Level: intermediate
1000d763cef2SBarry Smith 
1001d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1002d763cef2SBarry Smith 
1003d763cef2SBarry Smith .keywords: TS, get, timestep
1004d763cef2SBarry Smith @*/
10057087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1006d763cef2SBarry Smith {
1007d763cef2SBarry Smith   PetscFunctionBegin;
10080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10094482741eSBarry Smith   PetscValidDoublePointer(dt,2);
1010d763cef2SBarry Smith   *dt = ts->time_step;
1011d763cef2SBarry Smith   PetscFunctionReturn(0);
1012d763cef2SBarry Smith }
1013d763cef2SBarry Smith 
10144a2ae208SSatish Balay #undef __FUNCT__
10154a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1016d8e5e3e6SSatish Balay /*@
1017d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1018d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1019d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1020d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1021d763cef2SBarry Smith 
1022d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1023d763cef2SBarry Smith 
1024d763cef2SBarry Smith    Input Parameter:
1025d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1026d763cef2SBarry Smith 
1027d763cef2SBarry Smith    Output Parameter:
1028d763cef2SBarry Smith .  v - the vector containing the solution
1029d763cef2SBarry Smith 
1030d763cef2SBarry Smith    Level: intermediate
1031d763cef2SBarry Smith 
1032d763cef2SBarry Smith .seealso: TSGetTimeStep()
1033d763cef2SBarry Smith 
1034d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1035d763cef2SBarry Smith @*/
10367087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1037d763cef2SBarry Smith {
1038d763cef2SBarry Smith   PetscFunctionBegin;
10390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10404482741eSBarry Smith   PetscValidPointer(v,2);
10418737fe31SLisandro Dalcin   *v = ts->vec_sol;
1042d763cef2SBarry Smith   PetscFunctionReturn(0);
1043d763cef2SBarry Smith }
1044d763cef2SBarry Smith 
1045bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
10464a2ae208SSatish Balay #undef __FUNCT__
1047bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1048d8e5e3e6SSatish Balay /*@
1049bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1050d763cef2SBarry Smith 
1051bdad233fSMatthew Knepley   Not collective
1052d763cef2SBarry Smith 
1053bdad233fSMatthew Knepley   Input Parameters:
1054bdad233fSMatthew Knepley + ts   - The TS
1055bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1056d763cef2SBarry Smith .vb
1057d763cef2SBarry Smith          U_t = A U
1058d763cef2SBarry Smith          U_t = A(t) U
1059d763cef2SBarry Smith          U_t = F(t,U)
1060d763cef2SBarry Smith .ve
1061d763cef2SBarry Smith 
1062d763cef2SBarry Smith    Level: beginner
1063d763cef2SBarry Smith 
1064bdad233fSMatthew Knepley .keywords: TS, problem type
1065bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1066d763cef2SBarry Smith @*/
10677087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1068a7cc72afSBarry Smith {
10699e2a6581SJed Brown   PetscErrorCode ierr;
10709e2a6581SJed Brown 
1071d763cef2SBarry Smith   PetscFunctionBegin;
10720700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1073bdad233fSMatthew Knepley   ts->problem_type = type;
10749e2a6581SJed Brown   if (type == TS_LINEAR) {
10759e2a6581SJed Brown     SNES snes;
10769e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10779e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
10789e2a6581SJed Brown   }
1079d763cef2SBarry Smith   PetscFunctionReturn(0);
1080d763cef2SBarry Smith }
1081d763cef2SBarry Smith 
1082bdad233fSMatthew Knepley #undef __FUNCT__
1083bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1084bdad233fSMatthew Knepley /*@C
1085bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1086bdad233fSMatthew Knepley 
1087bdad233fSMatthew Knepley   Not collective
1088bdad233fSMatthew Knepley 
1089bdad233fSMatthew Knepley   Input Parameter:
1090bdad233fSMatthew Knepley . ts   - The TS
1091bdad233fSMatthew Knepley 
1092bdad233fSMatthew Knepley   Output Parameter:
1093bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1094bdad233fSMatthew Knepley .vb
1095089b2837SJed Brown          M U_t = A U
1096089b2837SJed Brown          M(t) U_t = A(t) U
1097bdad233fSMatthew Knepley          U_t = F(t,U)
1098bdad233fSMatthew Knepley .ve
1099bdad233fSMatthew Knepley 
1100bdad233fSMatthew Knepley    Level: beginner
1101bdad233fSMatthew Knepley 
1102bdad233fSMatthew Knepley .keywords: TS, problem type
1103bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1104bdad233fSMatthew Knepley @*/
11057087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1106a7cc72afSBarry Smith {
1107bdad233fSMatthew Knepley   PetscFunctionBegin;
11080700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
11094482741eSBarry Smith   PetscValidIntPointer(type,2);
1110bdad233fSMatthew Knepley   *type = ts->problem_type;
1111bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1112bdad233fSMatthew Knepley }
1113d763cef2SBarry Smith 
11144a2ae208SSatish Balay #undef __FUNCT__
11154a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1116d763cef2SBarry Smith /*@
1117d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1118d763cef2SBarry Smith    of a timestepper.
1119d763cef2SBarry Smith 
1120d763cef2SBarry Smith    Collective on TS
1121d763cef2SBarry Smith 
1122d763cef2SBarry Smith    Input Parameter:
1123d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1124d763cef2SBarry Smith 
1125d763cef2SBarry Smith    Notes:
1126d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1127d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1128d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1129d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1130d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1131d763cef2SBarry Smith 
1132d763cef2SBarry Smith    Level: advanced
1133d763cef2SBarry Smith 
1134d763cef2SBarry Smith .keywords: TS, timestep, setup
1135d763cef2SBarry Smith 
1136d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1137d763cef2SBarry Smith @*/
11387087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1139d763cef2SBarry Smith {
1140dfbe8321SBarry Smith   PetscErrorCode ierr;
1141d763cef2SBarry Smith 
1142d763cef2SBarry Smith   PetscFunctionBegin;
11430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1144277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1145277b19d0SLisandro Dalcin 
11467adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
11479596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1148d763cef2SBarry Smith   }
1149277b19d0SLisandro Dalcin 
1150277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1151277b19d0SLisandro Dalcin 
1152277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1153000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1154277b19d0SLisandro Dalcin   }
1155277b19d0SLisandro Dalcin 
1156277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1157277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1158277b19d0SLisandro Dalcin }
1159277b19d0SLisandro Dalcin 
1160277b19d0SLisandro Dalcin #undef __FUNCT__
1161277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1162277b19d0SLisandro Dalcin /*@
1163277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1164277b19d0SLisandro Dalcin 
1165277b19d0SLisandro Dalcin    Collective on TS
1166277b19d0SLisandro Dalcin 
1167277b19d0SLisandro Dalcin    Input Parameter:
1168277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1169277b19d0SLisandro Dalcin 
1170277b19d0SLisandro Dalcin    Level: beginner
1171277b19d0SLisandro Dalcin 
1172277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1173277b19d0SLisandro Dalcin 
1174277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1175277b19d0SLisandro Dalcin @*/
1176277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1177277b19d0SLisandro Dalcin {
1178277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1179277b19d0SLisandro Dalcin 
1180277b19d0SLisandro Dalcin   PetscFunctionBegin;
1181277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1182277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1183277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1184277b19d0SLisandro Dalcin   }
1185277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
11864e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
11874e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1188214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
11896bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1190277b19d0SLisandro Dalcin   if (ts->work) {ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);}
1191277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1192d763cef2SBarry Smith   PetscFunctionReturn(0);
1193d763cef2SBarry Smith }
1194d763cef2SBarry Smith 
11954a2ae208SSatish Balay #undef __FUNCT__
11964a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1197d8e5e3e6SSatish Balay /*@
1198d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1199d763cef2SBarry Smith    with TSCreate().
1200d763cef2SBarry Smith 
1201d763cef2SBarry Smith    Collective on TS
1202d763cef2SBarry Smith 
1203d763cef2SBarry Smith    Input Parameter:
1204d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1205d763cef2SBarry Smith 
1206d763cef2SBarry Smith    Level: beginner
1207d763cef2SBarry Smith 
1208d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1209d763cef2SBarry Smith 
1210d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1211d763cef2SBarry Smith @*/
12126bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1213d763cef2SBarry Smith {
12146849ba73SBarry Smith   PetscErrorCode ierr;
1215d763cef2SBarry Smith 
1216d763cef2SBarry Smith   PetscFunctionBegin;
12176bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
12186bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
12196bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1220d763cef2SBarry Smith 
12216bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1222277b19d0SLisandro Dalcin 
1223be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
12246bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
12256bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
12266d4c513bSLisandro Dalcin 
12276bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
12286bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
12296bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
12306d4c513bSLisandro Dalcin 
12313b5f76d0SSean Farley   ierr = PetscFree((*ts)->userops);
12323b5f76d0SSean Farley 
1233a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1234d763cef2SBarry Smith   PetscFunctionReturn(0);
1235d763cef2SBarry Smith }
1236d763cef2SBarry Smith 
12374a2ae208SSatish Balay #undef __FUNCT__
12384a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1239d8e5e3e6SSatish Balay /*@
1240d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1241d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1242d763cef2SBarry Smith 
1243d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1244d763cef2SBarry Smith 
1245d763cef2SBarry Smith    Input Parameter:
1246d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1247d763cef2SBarry Smith 
1248d763cef2SBarry Smith    Output Parameter:
1249d763cef2SBarry Smith .  snes - the nonlinear solver context
1250d763cef2SBarry Smith 
1251d763cef2SBarry Smith    Notes:
1252d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1253d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
125494b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1255d763cef2SBarry Smith 
1256d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1257d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1258d763cef2SBarry Smith 
1259d763cef2SBarry Smith    Level: beginner
1260d763cef2SBarry Smith 
1261d763cef2SBarry Smith .keywords: timestep, get, SNES
1262d763cef2SBarry Smith @*/
12637087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1264d763cef2SBarry Smith {
1265d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1266d372ba47SLisandro Dalcin 
1267d763cef2SBarry Smith   PetscFunctionBegin;
12680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12694482741eSBarry Smith   PetscValidPointer(snes,2);
1270d372ba47SLisandro Dalcin   if (!ts->snes) {
1271d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
1272d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1273d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
12749e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
12759e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
12769e2a6581SJed Brown     }
1277d372ba47SLisandro Dalcin   }
1278d763cef2SBarry Smith   *snes = ts->snes;
1279d763cef2SBarry Smith   PetscFunctionReturn(0);
1280d763cef2SBarry Smith }
1281d763cef2SBarry Smith 
12824a2ae208SSatish Balay #undef __FUNCT__
128394b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1284d8e5e3e6SSatish Balay /*@
128594b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1286d763cef2SBarry Smith    a TS (timestepper) context.
1287d763cef2SBarry Smith 
128894b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1289d763cef2SBarry Smith 
1290d763cef2SBarry Smith    Input Parameter:
1291d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1292d763cef2SBarry Smith 
1293d763cef2SBarry Smith    Output Parameter:
129494b7f48cSBarry Smith .  ksp - the nonlinear solver context
1295d763cef2SBarry Smith 
1296d763cef2SBarry Smith    Notes:
129794b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1298d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1299d763cef2SBarry Smith    KSP and PC contexts as well.
1300d763cef2SBarry Smith 
130194b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
130294b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1303d763cef2SBarry Smith 
1304d763cef2SBarry Smith    Level: beginner
1305d763cef2SBarry Smith 
130694b7f48cSBarry Smith .keywords: timestep, get, KSP
1307d763cef2SBarry Smith @*/
13087087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1309d763cef2SBarry Smith {
1310d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1311089b2837SJed Brown   SNES           snes;
1312d372ba47SLisandro Dalcin 
1313d763cef2SBarry Smith   PetscFunctionBegin;
13140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13154482741eSBarry Smith   PetscValidPointer(ksp,2);
131617186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1317e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1318089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1319089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1320d763cef2SBarry Smith   PetscFunctionReturn(0);
1321d763cef2SBarry Smith }
1322d763cef2SBarry Smith 
1323d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1324d763cef2SBarry Smith 
13254a2ae208SSatish Balay #undef __FUNCT__
1326adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1327adb62b0dSMatthew Knepley /*@
1328adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1329adb62b0dSMatthew Knepley    maximum time for iteration.
1330adb62b0dSMatthew Knepley 
13313f9fe445SBarry Smith    Not Collective
1332adb62b0dSMatthew Knepley 
1333adb62b0dSMatthew Knepley    Input Parameters:
1334adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1335adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1336adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1337adb62b0dSMatthew Knepley 
1338adb62b0dSMatthew Knepley    Level: intermediate
1339adb62b0dSMatthew Knepley 
1340adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1341adb62b0dSMatthew Knepley @*/
13427087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1343adb62b0dSMatthew Knepley {
1344adb62b0dSMatthew Knepley   PetscFunctionBegin;
13450700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1346abc0a331SBarry Smith   if (maxsteps) {
13474482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1348adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1349adb62b0dSMatthew Knepley   }
1350abc0a331SBarry Smith   if (maxtime ) {
13514482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1352adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1353adb62b0dSMatthew Knepley   }
1354adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1355adb62b0dSMatthew Knepley }
1356adb62b0dSMatthew Knepley 
1357adb62b0dSMatthew Knepley #undef __FUNCT__
13584a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1359d763cef2SBarry Smith /*@
1360d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1361d763cef2SBarry Smith    maximum time for iteration.
1362d763cef2SBarry Smith 
13633f9fe445SBarry Smith    Logically Collective on TS
1364d763cef2SBarry Smith 
1365d763cef2SBarry Smith    Input Parameters:
1366d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1367d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1368d763cef2SBarry Smith -  maxtime - final time to iterate to
1369d763cef2SBarry Smith 
1370d763cef2SBarry Smith    Options Database Keys:
1371d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
1372d763cef2SBarry Smith .  -ts_max_time <maxtime> - Sets maxtime
1373d763cef2SBarry Smith 
1374d763cef2SBarry Smith    Notes:
1375d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1376d763cef2SBarry Smith 
1377d763cef2SBarry Smith    Level: intermediate
1378d763cef2SBarry Smith 
1379d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1380d763cef2SBarry Smith @*/
13817087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1382d763cef2SBarry Smith {
1383d763cef2SBarry Smith   PetscFunctionBegin;
13840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1385c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1386c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
138739b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
138839b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1389d763cef2SBarry Smith   PetscFunctionReturn(0);
1390d763cef2SBarry Smith }
1391d763cef2SBarry Smith 
13924a2ae208SSatish Balay #undef __FUNCT__
13934a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1394d763cef2SBarry Smith /*@
1395d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1396d763cef2SBarry Smith    for use by the TS routines.
1397d763cef2SBarry Smith 
13983f9fe445SBarry Smith    Logically Collective on TS and Vec
1399d763cef2SBarry Smith 
1400d763cef2SBarry Smith    Input Parameters:
1401d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1402d763cef2SBarry Smith -  x - the solution vector
1403d763cef2SBarry Smith 
1404d763cef2SBarry Smith    Level: beginner
1405d763cef2SBarry Smith 
1406d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1407d763cef2SBarry Smith @*/
14087087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1409d763cef2SBarry Smith {
14108737fe31SLisandro Dalcin   PetscErrorCode ierr;
14118737fe31SLisandro Dalcin 
1412d763cef2SBarry Smith   PetscFunctionBegin;
14130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14140700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
14158737fe31SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
14166bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
14178737fe31SLisandro Dalcin   ts->vec_sol = x;
1418d763cef2SBarry Smith   PetscFunctionReturn(0);
1419d763cef2SBarry Smith }
1420d763cef2SBarry Smith 
1421e74ef692SMatthew Knepley #undef __FUNCT__
1422e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1423ac226902SBarry Smith /*@C
1424000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
14253f2090d5SJed Brown   called once at the beginning of each time step.
1426000e7ae3SMatthew Knepley 
14273f9fe445SBarry Smith   Logically Collective on TS
1428000e7ae3SMatthew Knepley 
1429000e7ae3SMatthew Knepley   Input Parameters:
1430000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1431000e7ae3SMatthew Knepley - func - The function
1432000e7ae3SMatthew Knepley 
1433000e7ae3SMatthew Knepley   Calling sequence of func:
1434000e7ae3SMatthew Knepley . func (TS ts);
1435000e7ae3SMatthew Knepley 
1436000e7ae3SMatthew Knepley   Level: intermediate
1437000e7ae3SMatthew Knepley 
1438000e7ae3SMatthew Knepley .keywords: TS, timestep
1439000e7ae3SMatthew Knepley @*/
14407087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1441000e7ae3SMatthew Knepley {
1442000e7ae3SMatthew Knepley   PetscFunctionBegin;
14430700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1444000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1445000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1446000e7ae3SMatthew Knepley }
1447000e7ae3SMatthew Knepley 
1448e74ef692SMatthew Knepley #undef __FUNCT__
14493f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
14503f2090d5SJed Brown /*@C
14513f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
14523f2090d5SJed Brown 
14533f2090d5SJed Brown   Collective on TS
14543f2090d5SJed Brown 
14553f2090d5SJed Brown   Input Parameters:
14563f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
14573f2090d5SJed Brown 
14583f2090d5SJed Brown   Notes:
14593f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
14603f2090d5SJed Brown   so most users would not generally call this routine themselves.
14613f2090d5SJed Brown 
14623f2090d5SJed Brown   Level: developer
14633f2090d5SJed Brown 
14643f2090d5SJed Brown .keywords: TS, timestep
14653f2090d5SJed Brown @*/
14667087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
14673f2090d5SJed Brown {
14683f2090d5SJed Brown   PetscErrorCode ierr;
14693f2090d5SJed Brown 
14703f2090d5SJed Brown   PetscFunctionBegin;
14710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
147272ac3e02SJed Brown   if (ts->ops->prestep) {
14733f2090d5SJed Brown     PetscStackPush("TS PreStep function");
14743f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
14753f2090d5SJed Brown     PetscStackPop;
1476312ce896SJed Brown   }
14773f2090d5SJed Brown   PetscFunctionReturn(0);
14783f2090d5SJed Brown }
14793f2090d5SJed Brown 
14803f2090d5SJed Brown #undef __FUNCT__
1481e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1482ac226902SBarry Smith /*@C
1483000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
14843f2090d5SJed Brown   called once at the end of each time step.
1485000e7ae3SMatthew Knepley 
14863f9fe445SBarry Smith   Logically Collective on TS
1487000e7ae3SMatthew Knepley 
1488000e7ae3SMatthew Knepley   Input Parameters:
1489000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1490000e7ae3SMatthew Knepley - func - The function
1491000e7ae3SMatthew Knepley 
1492000e7ae3SMatthew Knepley   Calling sequence of func:
1493000e7ae3SMatthew Knepley . func (TS ts);
1494000e7ae3SMatthew Knepley 
1495000e7ae3SMatthew Knepley   Level: intermediate
1496000e7ae3SMatthew Knepley 
1497000e7ae3SMatthew Knepley .keywords: TS, timestep
1498000e7ae3SMatthew Knepley @*/
14997087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1500000e7ae3SMatthew Knepley {
1501000e7ae3SMatthew Knepley   PetscFunctionBegin;
15020700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1503000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1504000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1505000e7ae3SMatthew Knepley }
1506000e7ae3SMatthew Knepley 
1507e74ef692SMatthew Knepley #undef __FUNCT__
15083f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
15093f2090d5SJed Brown /*@C
15103f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
15113f2090d5SJed Brown 
15123f2090d5SJed Brown   Collective on TS
15133f2090d5SJed Brown 
15143f2090d5SJed Brown   Input Parameters:
15153f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
15163f2090d5SJed Brown 
15173f2090d5SJed Brown   Notes:
15183f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
15193f2090d5SJed Brown   so most users would not generally call this routine themselves.
15203f2090d5SJed Brown 
15213f2090d5SJed Brown   Level: developer
15223f2090d5SJed Brown 
15233f2090d5SJed Brown .keywords: TS, timestep
15243f2090d5SJed Brown @*/
15257087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
15263f2090d5SJed Brown {
15273f2090d5SJed Brown   PetscErrorCode ierr;
15283f2090d5SJed Brown 
15293f2090d5SJed Brown   PetscFunctionBegin;
15300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
153172ac3e02SJed Brown   if (ts->ops->poststep) {
15323f2090d5SJed Brown     PetscStackPush("TS PostStep function");
15333f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
15343f2090d5SJed Brown     PetscStackPop;
153572ac3e02SJed Brown   }
15363f2090d5SJed Brown   PetscFunctionReturn(0);
15373f2090d5SJed Brown }
15383f2090d5SJed Brown 
1539d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1540d763cef2SBarry Smith 
15414a2ae208SSatish Balay #undef __FUNCT__
1542a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1543d763cef2SBarry Smith /*@C
1544a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1545d763cef2SBarry Smith    timestep to display the iteration's  progress.
1546d763cef2SBarry Smith 
15473f9fe445SBarry Smith    Logically Collective on TS
1548d763cef2SBarry Smith 
1549d763cef2SBarry Smith    Input Parameters:
1550d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1551d763cef2SBarry Smith .  func - monitoring routine
1552329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1553b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1554b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1555b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1556d763cef2SBarry Smith 
1557d763cef2SBarry Smith    Calling sequence of func:
1558a7cc72afSBarry Smith $    int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1559d763cef2SBarry Smith 
1560d763cef2SBarry Smith +    ts - the TS context
1561d763cef2SBarry Smith .    steps - iteration number
15621f06c33eSBarry Smith .    time - current time
1563d763cef2SBarry Smith .    x - current iterate
1564d763cef2SBarry Smith -    mctx - [optional] monitoring context
1565d763cef2SBarry Smith 
1566d763cef2SBarry Smith    Notes:
1567d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1568d763cef2SBarry Smith    already has been loaded.
1569d763cef2SBarry Smith 
1570025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1571025f1a04SBarry Smith 
1572d763cef2SBarry Smith    Level: intermediate
1573d763cef2SBarry Smith 
1574d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1575d763cef2SBarry Smith 
1576a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1577d763cef2SBarry Smith @*/
1578c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1579d763cef2SBarry Smith {
1580d763cef2SBarry Smith   PetscFunctionBegin;
15810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
158217186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1583d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1584329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1585d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1586d763cef2SBarry Smith   PetscFunctionReturn(0);
1587d763cef2SBarry Smith }
1588d763cef2SBarry Smith 
15894a2ae208SSatish Balay #undef __FUNCT__
1590a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1591d763cef2SBarry Smith /*@C
1592a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1593d763cef2SBarry Smith 
15943f9fe445SBarry Smith    Logically Collective on TS
1595d763cef2SBarry Smith 
1596d763cef2SBarry Smith    Input Parameters:
1597d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1598d763cef2SBarry Smith 
1599d763cef2SBarry Smith    Notes:
1600d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1601d763cef2SBarry Smith 
1602d763cef2SBarry Smith    Level: intermediate
1603d763cef2SBarry Smith 
1604d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1605d763cef2SBarry Smith 
1606a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1607d763cef2SBarry Smith @*/
16087087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1609d763cef2SBarry Smith {
1610d952e501SBarry Smith   PetscErrorCode ierr;
1611d952e501SBarry Smith   PetscInt       i;
1612d952e501SBarry Smith 
1613d763cef2SBarry Smith   PetscFunctionBegin;
16140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1615d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
1616d952e501SBarry Smith     if (ts->mdestroy[i]) {
16173c4aec1bSBarry Smith       ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
1618d952e501SBarry Smith     }
1619d952e501SBarry Smith   }
1620d763cef2SBarry Smith   ts->numbermonitors = 0;
1621d763cef2SBarry Smith   PetscFunctionReturn(0);
1622d763cef2SBarry Smith }
1623d763cef2SBarry Smith 
16244a2ae208SSatish Balay #undef __FUNCT__
1625a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
1626d8e5e3e6SSatish Balay /*@
1627a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
16285516499fSSatish Balay 
16295516499fSSatish Balay    Level: intermediate
163041251cbbSSatish Balay 
16315516499fSSatish Balay .keywords: TS, set, monitor
16325516499fSSatish Balay 
163341251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
163441251cbbSSatish Balay @*/
1635649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
1636d763cef2SBarry Smith {
1637dfbe8321SBarry Smith   PetscErrorCode ierr;
1638649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
1639d132466eSBarry Smith 
1640d763cef2SBarry Smith   PetscFunctionBegin;
1641649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1642649052a6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr);
1643649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1644d763cef2SBarry Smith   PetscFunctionReturn(0);
1645d763cef2SBarry Smith }
1646d763cef2SBarry Smith 
16474a2ae208SSatish Balay #undef __FUNCT__
16484a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1649d763cef2SBarry Smith /*@
1650d763cef2SBarry Smith    TSStep - Steps the requested number of timesteps.
1651d763cef2SBarry Smith 
1652d763cef2SBarry Smith    Collective on TS
1653d763cef2SBarry Smith 
1654d763cef2SBarry Smith    Input Parameter:
1655d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1656d763cef2SBarry Smith 
1657d763cef2SBarry Smith    Output Parameters:
1658d763cef2SBarry Smith +  steps - number of iterations until termination
1659142b95e3SSatish Balay -  ptime - time until termination
1660d763cef2SBarry Smith 
1661d763cef2SBarry Smith    Level: beginner
1662d763cef2SBarry Smith 
1663d763cef2SBarry Smith .keywords: TS, timestep, solve
1664d763cef2SBarry Smith 
1665d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy()
1666d763cef2SBarry Smith @*/
1667193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
1668d763cef2SBarry Smith {
1669dfbe8321SBarry Smith   PetscErrorCode ierr;
1670d763cef2SBarry Smith 
1671d763cef2SBarry Smith   PetscFunctionBegin;
16720700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1673277b19d0SLisandro Dalcin 
1674d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
1675d405a339SMatthew Knepley 
1676d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1677193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
1678d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1679d763cef2SBarry Smith   PetscFunctionReturn(0);
1680d763cef2SBarry Smith }
1681d763cef2SBarry Smith 
16824a2ae208SSatish Balay #undef __FUNCT__
16836a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
16846a4d4014SLisandro Dalcin /*@
16856a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
16866a4d4014SLisandro Dalcin 
16876a4d4014SLisandro Dalcin    Collective on TS
16886a4d4014SLisandro Dalcin 
16896a4d4014SLisandro Dalcin    Input Parameter:
16906a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
16916a4d4014SLisandro Dalcin -  x - the solution vector, or PETSC_NULL if it was set with TSSetSolution()
16926a4d4014SLisandro Dalcin 
16936a4d4014SLisandro Dalcin    Level: beginner
16946a4d4014SLisandro Dalcin 
16956a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
16966a4d4014SLisandro Dalcin 
16976a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
16986a4d4014SLisandro Dalcin @*/
16997087cfbeSBarry Smith PetscErrorCode  TSSolve(TS ts, Vec x)
17006a4d4014SLisandro Dalcin {
1701193ac0bcSJed Brown   PetscInt       i;
1702*4d7d938eSLisandro Dalcin   PetscBool      flg;
1703*4d7d938eSLisandro Dalcin   char           filename[PETSC_MAX_PATH_LEN];
1704*4d7d938eSLisandro Dalcin   PetscViewer    viewer;
17056a4d4014SLisandro Dalcin   PetscErrorCode ierr;
1706f22f69f0SBarry Smith 
17076a4d4014SLisandro Dalcin   PetscFunctionBegin;
17080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1709193ac0bcSJed Brown   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
1710193ac0bcSJed Brown   ierr = TSSetSolution(ts,x); CHKERRQ(ierr);
1711b5d403baSSean Farley   ierr = TSSetUp(ts); CHKERRQ(ierr);
17126a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
1713193ac0bcSJed Brown   ts->steps = 0;
1714193ac0bcSJed Brown   ts->linear_its = 0;
1715193ac0bcSJed Brown   ts->nonlinear_its = 0;
1716193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
1717193ac0bcSJed Brown   ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
1718193ac0bcSJed Brown 
1719193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
1720193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
1721193ac0bcSJed Brown   } else {
1722e1a7a14fSJed Brown     i = 0;
1723e1a7a14fSJed Brown     if (i >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
1724e1a7a14fSJed Brown     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
17256a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
1726e1a7a14fSJed Brown     while (!ts->reason) {
1727193ac0bcSJed Brown       ierr = TSPreStep(ts);CHKERRQ(ierr);
1728193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
1729193ac0bcSJed Brown       if (ts->reason < 0) {
1730193ac0bcSJed Brown         if (ts->errorifstepfailed) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed");
17313daf2de8SJed Brown       } else if (++i >= ts->max_steps) {
1732193ac0bcSJed Brown         ts->reason = TS_CONVERGED_ITS;
1733193ac0bcSJed Brown       } else if (ts->ptime >= ts->max_time) {
1734193ac0bcSJed Brown         ts->reason = TS_CONVERGED_TIME;
1735193ac0bcSJed Brown       }
1736193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
1737193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
1738193ac0bcSJed Brown     }
1739193ac0bcSJed Brown   }
1740*4d7d938eSLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1741*4d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
1742*4d7d938eSLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr);
1743*4d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
1744*4d7d938eSLisandro Dalcin     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1745193ac0bcSJed Brown   }
17466a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
17476a4d4014SLisandro Dalcin }
17486a4d4014SLisandro Dalcin 
17496a4d4014SLisandro Dalcin #undef __FUNCT__
17504a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
1751d763cef2SBarry Smith /*
1752d763cef2SBarry Smith      Runs the user provided monitor routines, if they exists.
1753d763cef2SBarry Smith */
1754a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
1755d763cef2SBarry Smith {
17566849ba73SBarry Smith   PetscErrorCode ierr;
1757a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
1758d763cef2SBarry Smith 
1759d763cef2SBarry Smith   PetscFunctionBegin;
1760d763cef2SBarry Smith   for (i=0; i<n; i++) {
1761142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
1762d763cef2SBarry Smith   }
1763d763cef2SBarry Smith   PetscFunctionReturn(0);
1764d763cef2SBarry Smith }
1765d763cef2SBarry Smith 
1766d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
1767d763cef2SBarry Smith 
17684a2ae208SSatish Balay #undef __FUNCT__
1769a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate"
1770d763cef2SBarry Smith /*@C
1771a6570f20SBarry Smith    TSMonitorLGCreate - Creates a line graph context for use with
1772d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
1773d763cef2SBarry Smith 
1774d763cef2SBarry Smith    Collective on TS
1775d763cef2SBarry Smith 
1776d763cef2SBarry Smith    Input Parameters:
1777d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
1778d763cef2SBarry Smith .  label - the title to put in the title bar
17797c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
1780d763cef2SBarry Smith -  m, n - the screen width and height in pixels
1781d763cef2SBarry Smith 
1782d763cef2SBarry Smith    Output Parameter:
1783d763cef2SBarry Smith .  draw - the drawing context
1784d763cef2SBarry Smith 
1785d763cef2SBarry Smith    Options Database Key:
1786a6570f20SBarry Smith .  -ts_monitor_draw - automatically sets line graph monitor
1787d763cef2SBarry Smith 
1788d763cef2SBarry Smith    Notes:
1789a6570f20SBarry Smith    Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy().
1790d763cef2SBarry Smith 
1791d763cef2SBarry Smith    Level: intermediate
1792d763cef2SBarry Smith 
17937c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
1794d763cef2SBarry Smith 
1795a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet()
17967c922b88SBarry Smith 
1797d763cef2SBarry Smith @*/
17987087cfbeSBarry Smith PetscErrorCode  TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1799d763cef2SBarry Smith {
1800b0a32e0cSBarry Smith   PetscDraw      win;
1801dfbe8321SBarry Smith   PetscErrorCode ierr;
1802d763cef2SBarry Smith 
1803d763cef2SBarry Smith   PetscFunctionBegin;
1804b0a32e0cSBarry Smith   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
1805b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
1806b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
1807b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
1808d763cef2SBarry Smith 
180952e6d16bSBarry Smith   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
1810d763cef2SBarry Smith   PetscFunctionReturn(0);
1811d763cef2SBarry Smith }
1812d763cef2SBarry Smith 
18134a2ae208SSatish Balay #undef __FUNCT__
1814a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG"
1815a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
1816d763cef2SBarry Smith {
1817b0a32e0cSBarry Smith   PetscDrawLG    lg = (PetscDrawLG) monctx;
181887828ca2SBarry Smith   PetscReal      x,y = ptime;
1819dfbe8321SBarry Smith   PetscErrorCode ierr;
1820d763cef2SBarry Smith 
1821d763cef2SBarry Smith   PetscFunctionBegin;
18227c922b88SBarry Smith   if (!monctx) {
18237c922b88SBarry Smith     MPI_Comm    comm;
1824b0a32e0cSBarry Smith     PetscViewer viewer;
18257c922b88SBarry Smith 
18267c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
1827b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
1828b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
18297c922b88SBarry Smith   }
18307c922b88SBarry Smith 
1831b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
183287828ca2SBarry Smith   x = (PetscReal)n;
1833b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1834d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
1835b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1836d763cef2SBarry Smith   }
1837d763cef2SBarry Smith   PetscFunctionReturn(0);
1838d763cef2SBarry Smith }
1839d763cef2SBarry Smith 
18404a2ae208SSatish Balay #undef __FUNCT__
1841a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy"
1842d763cef2SBarry Smith /*@C
1843a6570f20SBarry Smith    TSMonitorLGDestroy - Destroys a line graph context that was created
1844a6570f20SBarry Smith    with TSMonitorLGCreate().
1845d763cef2SBarry Smith 
1846b0a32e0cSBarry Smith    Collective on PetscDrawLG
1847d763cef2SBarry Smith 
1848d763cef2SBarry Smith    Input Parameter:
1849d763cef2SBarry Smith .  draw - the drawing context
1850d763cef2SBarry Smith 
1851d763cef2SBarry Smith    Level: intermediate
1852d763cef2SBarry Smith 
1853d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
1854d763cef2SBarry Smith 
1855a6570f20SBarry Smith .seealso: TSMonitorLGCreate(),  TSMonitorSet(), TSMonitorLG();
1856d763cef2SBarry Smith @*/
18576bf464f9SBarry Smith PetscErrorCode  TSMonitorLGDestroy(PetscDrawLG *drawlg)
1858d763cef2SBarry Smith {
1859b0a32e0cSBarry Smith   PetscDraw      draw;
1860dfbe8321SBarry Smith   PetscErrorCode ierr;
1861d763cef2SBarry Smith 
1862d763cef2SBarry Smith   PetscFunctionBegin;
18636bf464f9SBarry Smith   ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr);
18646bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
1865b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
1866d763cef2SBarry Smith   PetscFunctionReturn(0);
1867d763cef2SBarry Smith }
1868d763cef2SBarry Smith 
18694a2ae208SSatish Balay #undef __FUNCT__
18704a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
1871d763cef2SBarry Smith /*@
1872d763cef2SBarry Smith    TSGetTime - Gets the current time.
1873d763cef2SBarry Smith 
1874d763cef2SBarry Smith    Not Collective
1875d763cef2SBarry Smith 
1876d763cef2SBarry Smith    Input Parameter:
1877d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1878d763cef2SBarry Smith 
1879d763cef2SBarry Smith    Output Parameter:
1880d763cef2SBarry Smith .  t  - the current time
1881d763cef2SBarry Smith 
1882d763cef2SBarry Smith    Level: beginner
1883d763cef2SBarry Smith 
1884d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1885d763cef2SBarry Smith 
1886d763cef2SBarry Smith .keywords: TS, get, time
1887d763cef2SBarry Smith @*/
18887087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
1889d763cef2SBarry Smith {
1890d763cef2SBarry Smith   PetscFunctionBegin;
18910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18924482741eSBarry Smith   PetscValidDoublePointer(t,2);
1893d763cef2SBarry Smith   *t = ts->ptime;
1894d763cef2SBarry Smith   PetscFunctionReturn(0);
1895d763cef2SBarry Smith }
1896d763cef2SBarry Smith 
18974a2ae208SSatish Balay #undef __FUNCT__
18986a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
18996a4d4014SLisandro Dalcin /*@
19006a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
19016a4d4014SLisandro Dalcin 
19023f9fe445SBarry Smith    Logically Collective on TS
19036a4d4014SLisandro Dalcin 
19046a4d4014SLisandro Dalcin    Input Parameters:
19056a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
19066a4d4014SLisandro Dalcin -  time - the time
19076a4d4014SLisandro Dalcin 
19086a4d4014SLisandro Dalcin    Level: intermediate
19096a4d4014SLisandro Dalcin 
19106a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
19116a4d4014SLisandro Dalcin 
19126a4d4014SLisandro Dalcin .keywords: TS, set, time
19136a4d4014SLisandro Dalcin @*/
19147087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
19156a4d4014SLisandro Dalcin {
19166a4d4014SLisandro Dalcin   PetscFunctionBegin;
19170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1918c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
19196a4d4014SLisandro Dalcin   ts->ptime = t;
19206a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
19216a4d4014SLisandro Dalcin }
19226a4d4014SLisandro Dalcin 
19236a4d4014SLisandro Dalcin #undef __FUNCT__
19244a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
1925d763cef2SBarry Smith /*@C
1926d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
1927d763cef2SBarry Smith    TS options in the database.
1928d763cef2SBarry Smith 
19293f9fe445SBarry Smith    Logically Collective on TS
1930d763cef2SBarry Smith 
1931d763cef2SBarry Smith    Input Parameter:
1932d763cef2SBarry Smith +  ts     - The TS context
1933d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
1934d763cef2SBarry Smith 
1935d763cef2SBarry Smith    Notes:
1936d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1937d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
1938d763cef2SBarry Smith    hyphen.
1939d763cef2SBarry Smith 
1940d763cef2SBarry Smith    Level: advanced
1941d763cef2SBarry Smith 
1942d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
1943d763cef2SBarry Smith 
1944d763cef2SBarry Smith .seealso: TSSetFromOptions()
1945d763cef2SBarry Smith 
1946d763cef2SBarry Smith @*/
19477087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
1948d763cef2SBarry Smith {
1949dfbe8321SBarry Smith   PetscErrorCode ierr;
1950089b2837SJed Brown   SNES           snes;
1951d763cef2SBarry Smith 
1952d763cef2SBarry Smith   PetscFunctionBegin;
19530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1954d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
1955089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1956089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
1957d763cef2SBarry Smith   PetscFunctionReturn(0);
1958d763cef2SBarry Smith }
1959d763cef2SBarry Smith 
1960d763cef2SBarry Smith 
19614a2ae208SSatish Balay #undef __FUNCT__
19624a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
1963d763cef2SBarry Smith /*@C
1964d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
1965d763cef2SBarry Smith    TS options in the database.
1966d763cef2SBarry Smith 
19673f9fe445SBarry Smith    Logically Collective on TS
1968d763cef2SBarry Smith 
1969d763cef2SBarry Smith    Input Parameter:
1970d763cef2SBarry Smith +  ts     - The TS context
1971d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
1972d763cef2SBarry Smith 
1973d763cef2SBarry Smith    Notes:
1974d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1975d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
1976d763cef2SBarry Smith    hyphen.
1977d763cef2SBarry Smith 
1978d763cef2SBarry Smith    Level: advanced
1979d763cef2SBarry Smith 
1980d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
1981d763cef2SBarry Smith 
1982d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
1983d763cef2SBarry Smith 
1984d763cef2SBarry Smith @*/
19857087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
1986d763cef2SBarry Smith {
1987dfbe8321SBarry Smith   PetscErrorCode ierr;
1988089b2837SJed Brown   SNES           snes;
1989d763cef2SBarry Smith 
1990d763cef2SBarry Smith   PetscFunctionBegin;
19910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1992d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
1993089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1994089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
1995d763cef2SBarry Smith   PetscFunctionReturn(0);
1996d763cef2SBarry Smith }
1997d763cef2SBarry Smith 
19984a2ae208SSatish Balay #undef __FUNCT__
19994a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2000d763cef2SBarry Smith /*@C
2001d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2002d763cef2SBarry Smith    TS options in the database.
2003d763cef2SBarry Smith 
2004d763cef2SBarry Smith    Not Collective
2005d763cef2SBarry Smith 
2006d763cef2SBarry Smith    Input Parameter:
2007d763cef2SBarry Smith .  ts - The TS context
2008d763cef2SBarry Smith 
2009d763cef2SBarry Smith    Output Parameter:
2010d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2011d763cef2SBarry Smith 
2012d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2013d763cef2SBarry Smith    sufficient length to hold the prefix.
2014d763cef2SBarry Smith 
2015d763cef2SBarry Smith    Level: intermediate
2016d763cef2SBarry Smith 
2017d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2018d763cef2SBarry Smith 
2019d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2020d763cef2SBarry Smith @*/
20217087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2022d763cef2SBarry Smith {
2023dfbe8321SBarry Smith   PetscErrorCode ierr;
2024d763cef2SBarry Smith 
2025d763cef2SBarry Smith   PetscFunctionBegin;
20260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20274482741eSBarry Smith   PetscValidPointer(prefix,2);
2028d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2029d763cef2SBarry Smith   PetscFunctionReturn(0);
2030d763cef2SBarry Smith }
2031d763cef2SBarry Smith 
20324a2ae208SSatish Balay #undef __FUNCT__
20334a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2034d763cef2SBarry Smith /*@C
2035d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2036d763cef2SBarry Smith 
2037d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2038d763cef2SBarry Smith 
2039d763cef2SBarry Smith    Input Parameter:
2040d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2041d763cef2SBarry Smith 
2042d763cef2SBarry Smith    Output Parameters:
2043d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2044d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2045089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2046d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2047d763cef2SBarry Smith 
2048d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2049d763cef2SBarry Smith 
2050d763cef2SBarry Smith    Level: intermediate
2051d763cef2SBarry Smith 
205226d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2053d763cef2SBarry Smith 
2054d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2055d763cef2SBarry Smith @*/
2056089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2057d763cef2SBarry Smith {
2058089b2837SJed Brown   PetscErrorCode ierr;
2059089b2837SJed Brown   SNES           snes;
2060089b2837SJed Brown 
2061d763cef2SBarry Smith   PetscFunctionBegin;
2062089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2063089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
20643b5f76d0SSean Farley   if (func) *func = ts->userops->rhsjacobian;
206526d46c62SHong Zhang   if (ctx) *ctx = ts->jacP;
2066d763cef2SBarry Smith   PetscFunctionReturn(0);
2067d763cef2SBarry Smith }
2068d763cef2SBarry Smith 
20691713a123SBarry Smith #undef __FUNCT__
20702eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
20712eca1d9cSJed Brown /*@C
20722eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
20732eca1d9cSJed Brown 
20742eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
20752eca1d9cSJed Brown 
20762eca1d9cSJed Brown    Input Parameter:
20772eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
20782eca1d9cSJed Brown 
20792eca1d9cSJed Brown    Output Parameters:
20802eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
20812eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
20822eca1d9cSJed Brown .  f   - The function to compute the matrices
20832eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
20842eca1d9cSJed Brown 
20852eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
20862eca1d9cSJed Brown 
20872eca1d9cSJed Brown    Level: advanced
20882eca1d9cSJed Brown 
20892eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
20902eca1d9cSJed Brown 
20912eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
20922eca1d9cSJed Brown @*/
20937087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
20942eca1d9cSJed Brown {
2095089b2837SJed Brown   PetscErrorCode ierr;
2096089b2837SJed Brown   SNES           snes;
2097089b2837SJed Brown 
20982eca1d9cSJed Brown   PetscFunctionBegin;
2099089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2100089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
21013b5f76d0SSean Farley   if (f) *f = ts->userops->ijacobian;
21022eca1d9cSJed Brown   if (ctx) *ctx = ts->jacP;
21032eca1d9cSJed Brown   PetscFunctionReturn(0);
21042eca1d9cSJed Brown }
21052eca1d9cSJed Brown 
21062eca1d9cSJed Brown #undef __FUNCT__
2107a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution"
21081713a123SBarry Smith /*@C
2109a6570f20SBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by calling
21101713a123SBarry Smith    VecView() for the solution at each timestep
21111713a123SBarry Smith 
21121713a123SBarry Smith    Collective on TS
21131713a123SBarry Smith 
21141713a123SBarry Smith    Input Parameters:
21151713a123SBarry Smith +  ts - the TS context
21161713a123SBarry Smith .  step - current time-step
2117142b95e3SSatish Balay .  ptime - current time
21181713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
21191713a123SBarry Smith 
21201713a123SBarry Smith    Level: intermediate
21211713a123SBarry Smith 
21221713a123SBarry Smith .keywords: TS,  vector, monitor, view
21231713a123SBarry Smith 
2124a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
21251713a123SBarry Smith @*/
21267087cfbeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
21271713a123SBarry Smith {
2128dfbe8321SBarry Smith   PetscErrorCode ierr;
21291713a123SBarry Smith   PetscViewer    viewer = (PetscViewer) dummy;
21301713a123SBarry Smith 
21311713a123SBarry Smith   PetscFunctionBegin;
2132a34d58ebSBarry Smith   if (!dummy) {
21337adad957SLisandro Dalcin     viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm);
21341713a123SBarry Smith   }
21351713a123SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
21361713a123SBarry Smith   PetscFunctionReturn(0);
21371713a123SBarry Smith }
21381713a123SBarry Smith 
21391713a123SBarry Smith 
21406c699258SBarry Smith #undef __FUNCT__
21416c699258SBarry Smith #define __FUNCT__ "TSSetDM"
21426c699258SBarry Smith /*@
21436c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
21446c699258SBarry Smith 
21453f9fe445SBarry Smith    Logically Collective on TS and DM
21466c699258SBarry Smith 
21476c699258SBarry Smith    Input Parameters:
21486c699258SBarry Smith +  ts - the preconditioner context
21496c699258SBarry Smith -  dm - the dm
21506c699258SBarry Smith 
21516c699258SBarry Smith    Level: intermediate
21526c699258SBarry Smith 
21536c699258SBarry Smith 
21546c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
21556c699258SBarry Smith @*/
21567087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
21576c699258SBarry Smith {
21586c699258SBarry Smith   PetscErrorCode ierr;
2159089b2837SJed Brown   SNES           snes;
21606c699258SBarry Smith 
21616c699258SBarry Smith   PetscFunctionBegin;
21620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
216370663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
21646bf464f9SBarry Smith   ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
21656c699258SBarry Smith   ts->dm = dm;
2166089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2167089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
21686c699258SBarry Smith   PetscFunctionReturn(0);
21696c699258SBarry Smith }
21706c699258SBarry Smith 
21716c699258SBarry Smith #undef __FUNCT__
21726c699258SBarry Smith #define __FUNCT__ "TSGetDM"
21736c699258SBarry Smith /*@
21746c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
21756c699258SBarry Smith 
21763f9fe445SBarry Smith    Not Collective
21776c699258SBarry Smith 
21786c699258SBarry Smith    Input Parameter:
21796c699258SBarry Smith . ts - the preconditioner context
21806c699258SBarry Smith 
21816c699258SBarry Smith    Output Parameter:
21826c699258SBarry Smith .  dm - the dm
21836c699258SBarry Smith 
21846c699258SBarry Smith    Level: intermediate
21856c699258SBarry Smith 
21866c699258SBarry Smith 
21876c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
21886c699258SBarry Smith @*/
21897087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
21906c699258SBarry Smith {
21916c699258SBarry Smith   PetscFunctionBegin;
21920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21936c699258SBarry Smith   *dm = ts->dm;
21946c699258SBarry Smith   PetscFunctionReturn(0);
21956c699258SBarry Smith }
21961713a123SBarry Smith 
21970f5c6efeSJed Brown #undef __FUNCT__
21980f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
21990f5c6efeSJed Brown /*@
22000f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
22010f5c6efeSJed Brown 
22023f9fe445SBarry Smith    Logically Collective on SNES
22030f5c6efeSJed Brown 
22040f5c6efeSJed Brown    Input Parameter:
2205d42a1c89SJed Brown + snes - nonlinear solver
22060f5c6efeSJed Brown . X - the current state at which to evaluate the residual
2207d42a1c89SJed Brown - ctx - user context, must be a TS
22080f5c6efeSJed Brown 
22090f5c6efeSJed Brown    Output Parameter:
22100f5c6efeSJed Brown . F - the nonlinear residual
22110f5c6efeSJed Brown 
22120f5c6efeSJed Brown    Notes:
22130f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
22140f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
22150f5c6efeSJed Brown 
22160f5c6efeSJed Brown    Level: advanced
22170f5c6efeSJed Brown 
22180f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
22190f5c6efeSJed Brown @*/
22207087cfbeSBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx)
22210f5c6efeSJed Brown {
22220f5c6efeSJed Brown   TS ts = (TS)ctx;
22230f5c6efeSJed Brown   PetscErrorCode ierr;
22240f5c6efeSJed Brown 
22250f5c6efeSJed Brown   PetscFunctionBegin;
22260f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22270f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
22280f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
22290f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
22300f5c6efeSJed Brown   ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr);
22310f5c6efeSJed Brown   PetscFunctionReturn(0);
22320f5c6efeSJed Brown }
22330f5c6efeSJed Brown 
22340f5c6efeSJed Brown #undef __FUNCT__
22350f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
22360f5c6efeSJed Brown /*@
22370f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
22380f5c6efeSJed Brown 
22390f5c6efeSJed Brown    Collective on SNES
22400f5c6efeSJed Brown 
22410f5c6efeSJed Brown    Input Parameter:
22420f5c6efeSJed Brown + snes - nonlinear solver
22430f5c6efeSJed Brown . X - the current state at which to evaluate the residual
22440f5c6efeSJed Brown - ctx - user context, must be a TS
22450f5c6efeSJed Brown 
22460f5c6efeSJed Brown    Output Parameter:
22470f5c6efeSJed Brown + A - the Jacobian
22480f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
22490f5c6efeSJed Brown - flag - indicates any structure change in the matrix
22500f5c6efeSJed Brown 
22510f5c6efeSJed Brown    Notes:
22520f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
22530f5c6efeSJed Brown 
22540f5c6efeSJed Brown    Level: developer
22550f5c6efeSJed Brown 
22560f5c6efeSJed Brown .seealso: SNESSetJacobian()
22570f5c6efeSJed Brown @*/
22587087cfbeSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx)
22590f5c6efeSJed Brown {
22600f5c6efeSJed Brown   TS ts = (TS)ctx;
22610f5c6efeSJed Brown   PetscErrorCode ierr;
22620f5c6efeSJed Brown 
22630f5c6efeSJed Brown   PetscFunctionBegin;
22640f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22650f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
22660f5c6efeSJed Brown   PetscValidPointer(A,3);
22670f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
22680f5c6efeSJed Brown   PetscValidPointer(B,4);
22690f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
22700f5c6efeSJed Brown   PetscValidPointer(flag,5);
22710f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
22720f5c6efeSJed Brown   ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr);
22730f5c6efeSJed Brown   PetscFunctionReturn(0);
22740f5c6efeSJed Brown }
2275325fc9f4SBarry Smith 
22760e4ef248SJed Brown #undef __FUNCT__
22770e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
22780e4ef248SJed Brown /*@C
22790e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
22800e4ef248SJed Brown 
22810e4ef248SJed Brown    Collective on TS
22820e4ef248SJed Brown 
22830e4ef248SJed Brown    Input Arguments:
22840e4ef248SJed Brown +  ts - time stepping context
22850e4ef248SJed Brown .  t - time at which to evaluate
22860e4ef248SJed Brown .  X - state at which to evaluate
22870e4ef248SJed Brown -  ctx - context
22880e4ef248SJed Brown 
22890e4ef248SJed Brown    Output Arguments:
22900e4ef248SJed Brown .  F - right hand side
22910e4ef248SJed Brown 
22920e4ef248SJed Brown    Level: intermediate
22930e4ef248SJed Brown 
22940e4ef248SJed Brown    Notes:
22950e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
22960e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
22970e4ef248SJed Brown 
22980e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
22990e4ef248SJed Brown @*/
23000e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx)
23010e4ef248SJed Brown {
23020e4ef248SJed Brown   PetscErrorCode ierr;
23030e4ef248SJed Brown   Mat Arhs,Brhs;
23040e4ef248SJed Brown   MatStructure flg2;
23050e4ef248SJed Brown 
23060e4ef248SJed Brown   PetscFunctionBegin;
23070e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
23080e4ef248SJed Brown   ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
23090e4ef248SJed Brown   ierr = MatMult(Arhs,X,F);CHKERRQ(ierr);
23100e4ef248SJed Brown   PetscFunctionReturn(0);
23110e4ef248SJed Brown }
23120e4ef248SJed Brown 
23130e4ef248SJed Brown #undef __FUNCT__
23140e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
23150e4ef248SJed Brown /*@C
23160e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
23170e4ef248SJed Brown 
23180e4ef248SJed Brown    Collective on TS
23190e4ef248SJed Brown 
23200e4ef248SJed Brown    Input Arguments:
23210e4ef248SJed Brown +  ts - time stepping context
23220e4ef248SJed Brown .  t - time at which to evaluate
23230e4ef248SJed Brown .  X - state at which to evaluate
23240e4ef248SJed Brown -  ctx - context
23250e4ef248SJed Brown 
23260e4ef248SJed Brown    Output Arguments:
23270e4ef248SJed Brown +  A - pointer to operator
23280e4ef248SJed Brown .  B - pointer to preconditioning matrix
23290e4ef248SJed Brown -  flg - matrix structure flag
23300e4ef248SJed Brown 
23310e4ef248SJed Brown    Level: intermediate
23320e4ef248SJed Brown 
23330e4ef248SJed Brown    Notes:
23340e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
23350e4ef248SJed Brown 
23360e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
23370e4ef248SJed Brown @*/
23380e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx)
23390e4ef248SJed Brown {
23400e4ef248SJed Brown 
23410e4ef248SJed Brown   PetscFunctionBegin;
23420e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
23430e4ef248SJed Brown   PetscFunctionReturn(0);
23440e4ef248SJed Brown }
23450e4ef248SJed Brown 
23460026cea9SSean Farley #undef __FUNCT__
23470026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
23480026cea9SSean Farley /*@C
23490026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
23500026cea9SSean Farley 
23510026cea9SSean Farley    Collective on TS
23520026cea9SSean Farley 
23530026cea9SSean Farley    Input Arguments:
23540026cea9SSean Farley +  ts - time stepping context
23550026cea9SSean Farley .  t - time at which to evaluate
23560026cea9SSean Farley .  X - state at which to evaluate
23570026cea9SSean Farley .  Xdot - time derivative of state vector
23580026cea9SSean Farley -  ctx - context
23590026cea9SSean Farley 
23600026cea9SSean Farley    Output Arguments:
23610026cea9SSean Farley .  F - left hand side
23620026cea9SSean Farley 
23630026cea9SSean Farley    Level: intermediate
23640026cea9SSean Farley 
23650026cea9SSean Farley    Notes:
23660026cea9SSean 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
23670026cea9SSean Farley    user is required to write their own TSComputeIFunction.
23680026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
23690026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
23700026cea9SSean Farley 
23710026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
23720026cea9SSean Farley @*/
23730026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx)
23740026cea9SSean Farley {
23750026cea9SSean Farley   PetscErrorCode ierr;
23760026cea9SSean Farley   Mat A,B;
23770026cea9SSean Farley   MatStructure flg2;
23780026cea9SSean Farley 
23790026cea9SSean Farley   PetscFunctionBegin;
23800026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
23810026cea9SSean Farley   ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
23820026cea9SSean Farley   ierr = MatMult(A,Xdot,F);CHKERRQ(ierr);
23830026cea9SSean Farley   PetscFunctionReturn(0);
23840026cea9SSean Farley }
23850026cea9SSean Farley 
23860026cea9SSean Farley #undef __FUNCT__
23870026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
23880026cea9SSean Farley /*@C
23890026cea9SSean Farley    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
23900026cea9SSean Farley 
23910026cea9SSean Farley    Collective on TS
23920026cea9SSean Farley 
23930026cea9SSean Farley    Input Arguments:
23940026cea9SSean Farley +  ts - time stepping context
23950026cea9SSean Farley .  t - time at which to evaluate
23960026cea9SSean Farley .  X - state at which to evaluate
23970026cea9SSean Farley .  Xdot - time derivative of state vector
23980026cea9SSean Farley .  shift - shift to apply
23990026cea9SSean Farley -  ctx - context
24000026cea9SSean Farley 
24010026cea9SSean Farley    Output Arguments:
24020026cea9SSean Farley +  A - pointer to operator
24030026cea9SSean Farley .  B - pointer to preconditioning matrix
24040026cea9SSean Farley -  flg - matrix structure flag
24050026cea9SSean Farley 
24060026cea9SSean Farley    Level: intermediate
24070026cea9SSean Farley 
24080026cea9SSean Farley    Notes:
24090026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
24100026cea9SSean Farley 
24110026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
24120026cea9SSean Farley @*/
24130026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
24140026cea9SSean Farley {
24150026cea9SSean Farley 
24160026cea9SSean Farley   PetscFunctionBegin;
24170026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
24180026cea9SSean Farley   PetscFunctionReturn(0);
24190026cea9SSean Farley }
24200026cea9SSean Farley 
24214af1b03aSJed Brown 
24224af1b03aSJed Brown #undef __FUNCT__
24234af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
24244af1b03aSJed Brown /*@
24254af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
24264af1b03aSJed Brown 
24274af1b03aSJed Brown    Not Collective
24284af1b03aSJed Brown 
24294af1b03aSJed Brown    Input Parameter:
24304af1b03aSJed Brown .  ts - the TS context
24314af1b03aSJed Brown 
24324af1b03aSJed Brown    Output Parameter:
24334af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
24344af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
24354af1b03aSJed Brown 
24364af1b03aSJed Brown    Level: intermediate
24374af1b03aSJed Brown 
24384af1b03aSJed Brown    Notes: Can only be called after the call to TSSolve() is complete.
24394af1b03aSJed Brown 
24404af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
24414af1b03aSJed Brown 
24424af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
24434af1b03aSJed Brown @*/
24444af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
24454af1b03aSJed Brown {
24464af1b03aSJed Brown   PetscFunctionBegin;
24474af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24484af1b03aSJed Brown   PetscValidPointer(reason,2);
24494af1b03aSJed Brown   *reason = ts->reason;
24504af1b03aSJed Brown   PetscFunctionReturn(0);
24514af1b03aSJed Brown }
24524af1b03aSJed Brown 
2453325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2454c6db04a5SJed Brown #include <mex.h>
2455325fc9f4SBarry Smith 
2456325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
2457325fc9f4SBarry Smith 
2458325fc9f4SBarry Smith #undef __FUNCT__
2459325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
2460325fc9f4SBarry Smith /*
2461325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
2462325fc9f4SBarry Smith                          TSSetFunctionMatlab().
2463325fc9f4SBarry Smith 
2464325fc9f4SBarry Smith    Collective on TS
2465325fc9f4SBarry Smith 
2466325fc9f4SBarry Smith    Input Parameters:
2467325fc9f4SBarry Smith +  snes - the TS context
2468325fc9f4SBarry Smith -  x - input vector
2469325fc9f4SBarry Smith 
2470325fc9f4SBarry Smith    Output Parameter:
2471325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
2472325fc9f4SBarry Smith 
2473325fc9f4SBarry Smith    Notes:
2474325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
2475325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
2476325fc9f4SBarry Smith    themselves.
2477325fc9f4SBarry Smith 
2478325fc9f4SBarry Smith    Level: developer
2479325fc9f4SBarry Smith 
2480325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2481325fc9f4SBarry Smith 
2482325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2483325fc9f4SBarry Smith */
24847087cfbeSBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx)
2485325fc9f4SBarry Smith {
2486325fc9f4SBarry Smith   PetscErrorCode   ierr;
2487325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2488325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
2489325fc9f4SBarry Smith   mxArray	   *plhs[1],*prhs[7];
2490325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
2491325fc9f4SBarry Smith 
2492325fc9f4SBarry Smith   PetscFunctionBegin;
2493325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2494325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2495325fc9f4SBarry Smith   PetscValidHeaderSpecific(xdot,VEC_CLASSID,4);
2496325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
2497325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,x,3);
2498325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
2499325fc9f4SBarry Smith 
2500325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2501325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2502880f3077SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr);
2503325fc9f4SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
2504325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2505325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
2506325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2507325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2508325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
2509325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
2510325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
2511325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
2512325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2513325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2514325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2515325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2516325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2517325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2518325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2519325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2520325fc9f4SBarry Smith   PetscFunctionReturn(0);
2521325fc9f4SBarry Smith }
2522325fc9f4SBarry Smith 
2523325fc9f4SBarry Smith 
2524325fc9f4SBarry Smith #undef __FUNCT__
2525325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
2526325fc9f4SBarry Smith /*
2527325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
2528325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
2529e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
2530325fc9f4SBarry Smith 
2531325fc9f4SBarry Smith    Logically Collective on TS
2532325fc9f4SBarry Smith 
2533325fc9f4SBarry Smith    Input Parameters:
2534325fc9f4SBarry Smith +  ts - the TS context
2535325fc9f4SBarry Smith -  func - function evaluation routine
2536325fc9f4SBarry Smith 
2537325fc9f4SBarry Smith    Calling sequence of func:
2538325fc9f4SBarry Smith $    func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx);
2539325fc9f4SBarry Smith 
2540325fc9f4SBarry Smith    Level: beginner
2541325fc9f4SBarry Smith 
2542325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2543325fc9f4SBarry Smith 
2544325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2545325fc9f4SBarry Smith */
25467087cfbeSBarry Smith PetscErrorCode  TSSetFunctionMatlab(TS snes,const char *func,mxArray *ctx)
2547325fc9f4SBarry Smith {
2548325fc9f4SBarry Smith   PetscErrorCode  ierr;
2549325fc9f4SBarry Smith   TSMatlabContext *sctx;
2550325fc9f4SBarry Smith 
2551325fc9f4SBarry Smith   PetscFunctionBegin;
2552325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2553325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2554325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2555325fc9f4SBarry Smith   /*
2556325fc9f4SBarry Smith      This should work, but it doesn't
2557325fc9f4SBarry Smith   sctx->ctx = ctx;
2558325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2559325fc9f4SBarry Smith   */
2560325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2561325fc9f4SBarry Smith   ierr = TSSetIFunction(snes,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
2562325fc9f4SBarry Smith   PetscFunctionReturn(0);
2563325fc9f4SBarry Smith }
2564325fc9f4SBarry Smith 
2565325fc9f4SBarry Smith #undef __FUNCT__
2566325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
2567325fc9f4SBarry Smith /*
2568325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
2569325fc9f4SBarry Smith                          TSSetJacobianMatlab().
2570325fc9f4SBarry Smith 
2571325fc9f4SBarry Smith    Collective on TS
2572325fc9f4SBarry Smith 
2573325fc9f4SBarry Smith    Input Parameters:
2574325fc9f4SBarry Smith +  snes - the TS context
2575325fc9f4SBarry Smith .  x - input vector
2576325fc9f4SBarry Smith .  A, B - the matrices
2577325fc9f4SBarry Smith -  ctx - user context
2578325fc9f4SBarry Smith 
2579325fc9f4SBarry Smith    Output Parameter:
2580325fc9f4SBarry Smith .  flag - structure of the matrix
2581325fc9f4SBarry Smith 
2582325fc9f4SBarry Smith    Level: developer
2583325fc9f4SBarry Smith 
2584325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2585325fc9f4SBarry Smith 
2586325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2587325fc9f4SBarry Smith @*/
25887087cfbeSBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
2589325fc9f4SBarry Smith {
2590325fc9f4SBarry Smith   PetscErrorCode  ierr;
2591325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2592325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
2593325fc9f4SBarry Smith   mxArray	  *plhs[2],*prhs[9];
2594325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
2595325fc9f4SBarry Smith 
2596325fc9f4SBarry Smith   PetscFunctionBegin;
2597325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2598325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2599325fc9f4SBarry Smith 
2600325fc9f4SBarry Smith   /* call Matlab function in ctx with arguments x and y */
2601325fc9f4SBarry Smith 
2602325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2603325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2604325fc9f4SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr);
2605325fc9f4SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
2606325fc9f4SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
2607325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2608325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
2609325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2610325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2611325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
2612325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
2613325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
2614325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
2615325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
2616325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
2617325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2618325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
2619325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2620325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2621325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2622325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2623325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2624325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2625325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
2626325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
2627325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2628325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
2629325fc9f4SBarry Smith   PetscFunctionReturn(0);
2630325fc9f4SBarry Smith }
2631325fc9f4SBarry Smith 
2632325fc9f4SBarry Smith 
2633325fc9f4SBarry Smith #undef __FUNCT__
2634325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
2635325fc9f4SBarry Smith /*
2636325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
2637e3c5b3baSBarry 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
2638325fc9f4SBarry Smith 
2639325fc9f4SBarry Smith    Logically Collective on TS
2640325fc9f4SBarry Smith 
2641325fc9f4SBarry Smith    Input Parameters:
2642325fc9f4SBarry Smith +  snes - the TS context
2643325fc9f4SBarry Smith .  A,B - Jacobian matrices
2644325fc9f4SBarry Smith .  func - function evaluation routine
2645325fc9f4SBarry Smith -  ctx - user context
2646325fc9f4SBarry Smith 
2647325fc9f4SBarry Smith    Calling sequence of func:
2648325fc9f4SBarry Smith $    flag = func (TS snes,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx);
2649325fc9f4SBarry Smith 
2650325fc9f4SBarry Smith 
2651325fc9f4SBarry Smith    Level: developer
2652325fc9f4SBarry Smith 
2653325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2654325fc9f4SBarry Smith 
2655325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2656325fc9f4SBarry Smith */
26577087cfbeSBarry Smith PetscErrorCode  TSSetJacobianMatlab(TS snes,Mat A,Mat B,const char *func,mxArray *ctx)
2658325fc9f4SBarry Smith {
2659325fc9f4SBarry Smith   PetscErrorCode    ierr;
2660325fc9f4SBarry Smith   TSMatlabContext *sctx;
2661325fc9f4SBarry Smith 
2662325fc9f4SBarry Smith   PetscFunctionBegin;
2663325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2664325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2665325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2666325fc9f4SBarry Smith   /*
2667325fc9f4SBarry Smith      This should work, but it doesn't
2668325fc9f4SBarry Smith   sctx->ctx = ctx;
2669325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2670325fc9f4SBarry Smith   */
2671325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2672325fc9f4SBarry Smith   ierr = TSSetIJacobian(snes,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
2673325fc9f4SBarry Smith   PetscFunctionReturn(0);
2674325fc9f4SBarry Smith }
2675325fc9f4SBarry Smith 
2676b5b1a830SBarry Smith #undef __FUNCT__
2677b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
2678b5b1a830SBarry Smith /*
2679b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
2680b5b1a830SBarry Smith 
2681b5b1a830SBarry Smith    Collective on TS
2682b5b1a830SBarry Smith 
2683b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2684b5b1a830SBarry Smith @*/
26857087cfbeSBarry Smith PetscErrorCode  TSMonitor_Matlab(TS snes,PetscInt it, PetscReal time,Vec x, void *ctx)
2686b5b1a830SBarry Smith {
2687b5b1a830SBarry Smith   PetscErrorCode  ierr;
2688b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2689a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
2690b5b1a830SBarry Smith   mxArray	  *plhs[1],*prhs[6];
2691b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
2692b5b1a830SBarry Smith 
2693b5b1a830SBarry Smith   PetscFunctionBegin;
2694b5b1a830SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2695b5b1a830SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,4);
2696b5b1a830SBarry Smith 
2697b5b1a830SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2698b5b1a830SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2699b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2700b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
2701b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
2702b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
2703b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
2704b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
2705b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
2706b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2707b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
2708b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
2709b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
2710b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
2711b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
2712b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
2713b5b1a830SBarry Smith   PetscFunctionReturn(0);
2714b5b1a830SBarry Smith }
2715b5b1a830SBarry Smith 
2716b5b1a830SBarry Smith 
2717b5b1a830SBarry Smith #undef __FUNCT__
2718b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
2719b5b1a830SBarry Smith /*
2720b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
2721b5b1a830SBarry Smith 
2722b5b1a830SBarry Smith    Level: developer
2723b5b1a830SBarry Smith 
2724b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
2725b5b1a830SBarry Smith 
2726b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2727b5b1a830SBarry Smith */
27287087cfbeSBarry Smith PetscErrorCode  TSMonitorSetMatlab(TS snes,const char *func,mxArray *ctx)
2729b5b1a830SBarry Smith {
2730b5b1a830SBarry Smith   PetscErrorCode    ierr;
2731b5b1a830SBarry Smith   TSMatlabContext *sctx;
2732b5b1a830SBarry Smith 
2733b5b1a830SBarry Smith   PetscFunctionBegin;
2734b5b1a830SBarry Smith   /* currently sctx is memory bleed */
2735b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2736b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2737b5b1a830SBarry Smith   /*
2738b5b1a830SBarry Smith      This should work, but it doesn't
2739b5b1a830SBarry Smith   sctx->ctx = ctx;
2740b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2741b5b1a830SBarry Smith   */
2742b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2743b5b1a830SBarry Smith   ierr = TSMonitorSet(snes,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
2744b5b1a830SBarry Smith   PetscFunctionReturn(0);
2745b5b1a830SBarry Smith }
2746b5b1a830SBarry Smith 
2747325fc9f4SBarry Smith #endif
2748