xref: /petsc/src/ts/interface/ts.c (revision 39b7ec4ba34fe3c579d4c02bb5bbaf8bab544d96)
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     }
10090d69ab7SBarry Smith     opt  = PETSC_FALSE;
101acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
102a7cc72afSBarry Smith     if (opt) {
103a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
104bdad233fSMatthew Knepley     }
10590d69ab7SBarry Smith     opt  = PETSC_FALSE;
106acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
107a7cc72afSBarry Smith     if (opt) {
108a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolution,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
109bdad233fSMatthew Knepley     }
110bdad233fSMatthew Knepley 
111bdad233fSMatthew Knepley     /* Handle TS type options */
112bdad233fSMatthew Knepley     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
113bdad233fSMatthew Knepley 
114bdad233fSMatthew Knepley     /* Handle specific TS options */
115abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
116bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
117bdad233fSMatthew Knepley     }
1185d973c19SBarry Smith 
1195d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
1205d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
121bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
122bdad233fSMatthew Knepley 
123089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
124bdad233fSMatthew Knepley   /* Handle subobject options */
125089b2837SJed Brown   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
126089b2837SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
127bdad233fSMatthew Knepley   PetscFunctionReturn(0);
128bdad233fSMatthew Knepley }
129bdad233fSMatthew Knepley 
130bdad233fSMatthew Knepley #undef  __FUNCT__
131bdad233fSMatthew Knepley #define __FUNCT__ "TSViewFromOptions"
132bdad233fSMatthew Knepley /*@
133bdad233fSMatthew Knepley   TSViewFromOptions - This function visualizes the ts based upon user options.
134bdad233fSMatthew Knepley 
135bdad233fSMatthew Knepley   Collective on TS
136bdad233fSMatthew Knepley 
137bdad233fSMatthew Knepley   Input Parameter:
138bdad233fSMatthew Knepley . ts - The ts
139bdad233fSMatthew Knepley 
140bdad233fSMatthew Knepley   Level: intermediate
141bdad233fSMatthew Knepley 
142bdad233fSMatthew Knepley .keywords: TS, view, options, database
143bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSView()
144bdad233fSMatthew Knepley @*/
1457087cfbeSBarry Smith PetscErrorCode  TSViewFromOptions(TS ts,const char title[])
146bdad233fSMatthew Knepley {
147bdad233fSMatthew Knepley   PetscViewer    viewer;
148bdad233fSMatthew Knepley   PetscDraw      draw;
149ace3abfcSBarry Smith   PetscBool      opt = PETSC_FALSE;
150e10c49a3SBarry Smith   char           fileName[PETSC_MAX_PATH_LEN];
151dfbe8321SBarry Smith   PetscErrorCode ierr;
152bdad233fSMatthew Knepley 
153bdad233fSMatthew Knepley   PetscFunctionBegin;
1547adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix, "-ts_view", fileName, PETSC_MAX_PATH_LEN, &opt);CHKERRQ(ierr);
155eabae89aSBarry Smith   if (opt && !PetscPreLoadingOn) {
1567adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,fileName,&viewer);CHKERRQ(ierr);
157bdad233fSMatthew Knepley     ierr = TSView(ts, viewer);CHKERRQ(ierr);
1586bf464f9SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
159bdad233fSMatthew Knepley   }
1608e83347fSKai Germaschewski   opt = PETSC_FALSE;
161acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix, "-ts_view_draw", &opt,PETSC_NULL);CHKERRQ(ierr);
162a7cc72afSBarry Smith   if (opt) {
1637adad957SLisandro Dalcin     ierr = PetscViewerDrawOpen(((PetscObject)ts)->comm, 0, 0, 0, 0, 300, 300, &viewer);CHKERRQ(ierr);
164bdad233fSMatthew Knepley     ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
165a7cc72afSBarry Smith     if (title) {
1661836bdbcSSatish Balay       ierr = PetscDrawSetTitle(draw, (char *)title);CHKERRQ(ierr);
167bdad233fSMatthew Knepley     } else {
168bdad233fSMatthew Knepley       ierr = PetscObjectName((PetscObject)ts);CHKERRQ(ierr);
1697adad957SLisandro Dalcin       ierr = PetscDrawSetTitle(draw, ((PetscObject)ts)->name);CHKERRQ(ierr);
170bdad233fSMatthew Knepley     }
171bdad233fSMatthew Knepley     ierr = TSView(ts, viewer);CHKERRQ(ierr);
172bdad233fSMatthew Knepley     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
173bdad233fSMatthew Knepley     ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1746bf464f9SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
175bdad233fSMatthew Knepley   }
176bdad233fSMatthew Knepley   PetscFunctionReturn(0);
177bdad233fSMatthew Knepley }
178bdad233fSMatthew Knepley 
179bdad233fSMatthew Knepley #undef __FUNCT__
1804a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
181a7a1495cSBarry Smith /*@
1828c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
183a7a1495cSBarry Smith       set with TSSetRHSJacobian().
184a7a1495cSBarry Smith 
185a7a1495cSBarry Smith    Collective on TS and Vec
186a7a1495cSBarry Smith 
187a7a1495cSBarry Smith    Input Parameters:
188316643e7SJed Brown +  ts - the TS context
189a7a1495cSBarry Smith .  t - current timestep
190a7a1495cSBarry Smith -  x - input vector
191a7a1495cSBarry Smith 
192a7a1495cSBarry Smith    Output Parameters:
193a7a1495cSBarry Smith +  A - Jacobian matrix
194a7a1495cSBarry Smith .  B - optional preconditioning matrix
195a7a1495cSBarry Smith -  flag - flag indicating matrix structure
196a7a1495cSBarry Smith 
197a7a1495cSBarry Smith    Notes:
198a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
199a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
200a7a1495cSBarry Smith 
20194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
202a7a1495cSBarry Smith    flag parameter.
203a7a1495cSBarry Smith 
204a7a1495cSBarry Smith    Level: developer
205a7a1495cSBarry Smith 
206a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
207a7a1495cSBarry Smith 
20894b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
209a7a1495cSBarry Smith @*/
2107087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
211a7a1495cSBarry Smith {
212dfbe8321SBarry Smith   PetscErrorCode ierr;
2130e4ef248SJed Brown   PetscInt Xstate;
214a7a1495cSBarry Smith 
215a7a1495cSBarry Smith   PetscFunctionBegin;
2160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2170700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
218c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
2190e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
2200e4ef248SJed Brown   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) {
2210e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
2220e4ef248SJed Brown     PetscFunctionReturn(0);
223f8ede8e7SJed Brown   }
224d90be118SSean Farley 
2253b5f76d0SSean Farley   if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
226d90be118SSean Farley 
2273b5f76d0SSean Farley   if (ts->userops->rhsjacobian) {
228d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
229a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
230a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
2313b5f76d0SSean Farley     ierr = (*ts->userops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr);
232a7a1495cSBarry Smith     PetscStackPop;
233d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
234a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
2350700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
2360700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
237ef66eb69SBarry Smith   } else {
238214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
239214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
240214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
241ef66eb69SBarry Smith   }
2420e4ef248SJed Brown   ts->rhsjacobian.time = t;
2430e4ef248SJed Brown   ts->rhsjacobian.X = X;
2440e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
2450e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
246a7a1495cSBarry Smith   PetscFunctionReturn(0);
247a7a1495cSBarry Smith }
248a7a1495cSBarry Smith 
2494a2ae208SSatish Balay #undef __FUNCT__
2504a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
251316643e7SJed Brown /*@
252d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
253d763cef2SBarry Smith 
254316643e7SJed Brown    Collective on TS and Vec
255316643e7SJed Brown 
256316643e7SJed Brown    Input Parameters:
257316643e7SJed Brown +  ts - the TS context
258316643e7SJed Brown .  t - current time
259316643e7SJed Brown -  x - state vector
260316643e7SJed Brown 
261316643e7SJed Brown    Output Parameter:
262316643e7SJed Brown .  y - right hand side
263316643e7SJed Brown 
264316643e7SJed Brown    Note:
265316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
266316643e7SJed Brown    is used internally within the nonlinear solvers.
267316643e7SJed Brown 
268316643e7SJed Brown    Level: developer
269316643e7SJed Brown 
270316643e7SJed Brown .keywords: TS, compute
271316643e7SJed Brown 
272316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
273316643e7SJed Brown @*/
274dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
275d763cef2SBarry Smith {
276dfbe8321SBarry Smith   PetscErrorCode ierr;
277d763cef2SBarry Smith 
278d763cef2SBarry Smith   PetscFunctionBegin;
2790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2800700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2810700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
282d763cef2SBarry Smith 
2833b5f76d0SSean Farley   if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
284d763cef2SBarry Smith 
285d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
2863b5f76d0SSean Farley   if (ts->userops->rhsfunction) {
287d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
2883b5f76d0SSean Farley     ierr = (*ts->userops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr);
289d763cef2SBarry Smith     PetscStackPop;
290214bc6a2SJed Brown   } else {
291214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
292b2cd27e8SJed Brown   }
29344a41b28SSean Farley 
294d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
295d763cef2SBarry Smith   PetscFunctionReturn(0);
296d763cef2SBarry Smith }
297d763cef2SBarry Smith 
2984a2ae208SSatish Balay #undef __FUNCT__
299214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
300214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
301214bc6a2SJed Brown {
302214bc6a2SJed Brown   PetscErrorCode ierr;
303214bc6a2SJed Brown 
304214bc6a2SJed Brown   PetscFunctionBegin;
305214bc6a2SJed Brown   if (!ts->Frhs) {
306214bc6a2SJed Brown     ierr = VecDuplicate(ts->vec_sol,&ts->Frhs);CHKERRQ(ierr);
307214bc6a2SJed Brown   }
308214bc6a2SJed Brown   *Frhs = ts->Frhs;
309214bc6a2SJed Brown   PetscFunctionReturn(0);
310214bc6a2SJed Brown }
311214bc6a2SJed Brown 
312214bc6a2SJed Brown #undef __FUNCT__
313214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
314214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
315214bc6a2SJed Brown {
316214bc6a2SJed Brown   PetscErrorCode ierr;
317214bc6a2SJed Brown   Mat A,B;
318214bc6a2SJed Brown 
319214bc6a2SJed Brown   PetscFunctionBegin;
320214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
321214bc6a2SJed Brown   if (Arhs) {
322214bc6a2SJed Brown     if (!ts->Arhs) {
323214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
324214bc6a2SJed Brown     }
325214bc6a2SJed Brown     *Arhs = ts->Arhs;
326214bc6a2SJed Brown   }
327214bc6a2SJed Brown   if (Brhs) {
328214bc6a2SJed Brown     if (!ts->Brhs) {
329214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
330214bc6a2SJed Brown     }
331214bc6a2SJed Brown     *Brhs = ts->Brhs;
332214bc6a2SJed Brown   }
333214bc6a2SJed Brown   PetscFunctionReturn(0);
334214bc6a2SJed Brown }
335214bc6a2SJed Brown 
336214bc6a2SJed Brown #undef __FUNCT__
337316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
338316643e7SJed Brown /*@
339316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
340316643e7SJed Brown 
341316643e7SJed Brown    Collective on TS and Vec
342316643e7SJed Brown 
343316643e7SJed Brown    Input Parameters:
344316643e7SJed Brown +  ts - the TS context
345316643e7SJed Brown .  t - current time
346316643e7SJed Brown .  X - state vector
347214bc6a2SJed Brown .  Xdot - time derivative of state vector
348214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
349316643e7SJed Brown 
350316643e7SJed Brown    Output Parameter:
351316643e7SJed Brown .  Y - right hand side
352316643e7SJed Brown 
353316643e7SJed Brown    Note:
354316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
355316643e7SJed Brown    is used internally within the nonlinear solvers.
356316643e7SJed Brown 
357316643e7SJed Brown    If the user did did not write their equations in implicit form, this
358316643e7SJed Brown    function recasts them in implicit form.
359316643e7SJed Brown 
360316643e7SJed Brown    Level: developer
361316643e7SJed Brown 
362316643e7SJed Brown .keywords: TS, compute
363316643e7SJed Brown 
364316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
365316643e7SJed Brown @*/
366214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex)
367316643e7SJed Brown {
368316643e7SJed Brown   PetscErrorCode ierr;
369316643e7SJed Brown 
370316643e7SJed Brown   PetscFunctionBegin;
3710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3720700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3730700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
3740700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
375316643e7SJed Brown 
376d90be118SSean Farley   if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
377d90be118SSean Farley 
378316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
3793b5f76d0SSean Farley   if (ts->userops->ifunction) {
380316643e7SJed Brown     PetscStackPush("TS user implicit function");
3813b5f76d0SSean Farley     ierr = (*ts->userops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr);
382316643e7SJed Brown     PetscStackPop;
383214bc6a2SJed Brown   }
384214bc6a2SJed Brown   if (imex) {
3853b5f76d0SSean Farley     if (!ts->userops->ifunction) {ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);}
386316643e7SJed Brown   } else {
3873b5f76d0SSean Farley     if (!ts->userops->ifunction) {
388089b2837SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr);
389ace68cafSJed Brown       ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr);
390214bc6a2SJed Brown     } else {
391214bc6a2SJed Brown       Vec Frhs;
392214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
393214bc6a2SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr);
394214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
395316643e7SJed Brown     }
3964a6899ffSJed Brown   }
397316643e7SJed Brown   ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
398316643e7SJed Brown   PetscFunctionReturn(0);
399316643e7SJed Brown }
400316643e7SJed Brown 
401316643e7SJed Brown #undef __FUNCT__
402316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
403316643e7SJed Brown /*@
404316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
405316643e7SJed Brown 
406316643e7SJed Brown    Collective on TS and Vec
407316643e7SJed Brown 
408316643e7SJed Brown    Input
409316643e7SJed Brown       Input Parameters:
410316643e7SJed Brown +  ts - the TS context
411316643e7SJed Brown .  t - current timestep
412316643e7SJed Brown .  X - state vector
413316643e7SJed Brown .  Xdot - time derivative of state vector
414214bc6a2SJed Brown .  shift - shift to apply, see note below
415214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
416316643e7SJed Brown 
417316643e7SJed Brown    Output Parameters:
418316643e7SJed Brown +  A - Jacobian matrix
419316643e7SJed Brown .  B - optional preconditioning matrix
420316643e7SJed Brown -  flag - flag indicating matrix structure
421316643e7SJed Brown 
422316643e7SJed Brown    Notes:
423316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
424316643e7SJed Brown 
425316643e7SJed Brown    dF/dX + shift*dF/dXdot
426316643e7SJed Brown 
427316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
428316643e7SJed Brown    is used internally within the nonlinear solvers.
429316643e7SJed Brown 
430316643e7SJed Brown    Level: developer
431316643e7SJed Brown 
432316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
433316643e7SJed Brown 
434316643e7SJed Brown .seealso:  TSSetIJacobian()
43563495f91SJed Brown @*/
436214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
437316643e7SJed Brown {
4380026cea9SSean Farley   PetscInt Xstate, Xdotstate;
439316643e7SJed Brown   PetscErrorCode ierr;
440316643e7SJed Brown 
441316643e7SJed Brown   PetscFunctionBegin;
4420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4430700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4440700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
445316643e7SJed Brown   PetscValidPointer(A,6);
4460700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
447316643e7SJed Brown   PetscValidPointer(B,7);
4480700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
449316643e7SJed Brown   PetscValidPointer(flg,8);
4500026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
4510026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr);
4520026cea9SSean 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))) {
4530026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
4540026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4550026cea9SSean Farley     PetscFunctionReturn(0);
4560026cea9SSean Farley   }
457316643e7SJed Brown 
4583b5f76d0SSean Farley   if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
459316643e7SJed Brown 
4604e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
461316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
4623b5f76d0SSean Farley   if (ts->userops->ijacobian) {
463316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
4643b5f76d0SSean Farley     ierr = (*ts->userops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr);
465316643e7SJed Brown     PetscStackPop;
466214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
467214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
468214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
4694a6899ffSJed Brown   }
470214bc6a2SJed Brown   if (imex) {
4713b5f76d0SSean Farley     if (!ts->userops->ijacobian) {  /* system was written as Xdot = F(t,X) */
472214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
473214bc6a2SJed Brown       ierr = MatShift(*A,1.0);CHKERRQ(ierr);
474214bc6a2SJed Brown       if (*A != *B) {
475214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
476214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
477214bc6a2SJed Brown       }
478214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
479214bc6a2SJed Brown     }
480214bc6a2SJed Brown   } else {
4813b5f76d0SSean Farley     if (!ts->userops->ijacobian) {
482214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr);
483214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
484214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
485316643e7SJed Brown       if (*A != *B) {
486316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
487316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
488316643e7SJed Brown       }
4893b5f76d0SSean Farley     } else if (ts->userops->rhsjacobian) {
490214bc6a2SJed Brown       Mat Arhs,Brhs;
491214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
492214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
493214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
494214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
495214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
496214bc6a2SJed Brown       if (*A != *B) {
497214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
498214bc6a2SJed Brown       }
499214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
500214bc6a2SJed Brown     }
501316643e7SJed Brown   }
5020026cea9SSean Farley 
5030026cea9SSean Farley   ts->ijacobian.time = t;
5040026cea9SSean Farley   ts->ijacobian.X = X;
5050026cea9SSean Farley   ts->ijacobian.Xdot = Xdot;
5060026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr);
5070026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
5080026cea9SSean Farley   ts->ijacobian.shift = shift;
5090026cea9SSean Farley   ts->ijacobian.imex = imex;
5100026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
511316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
512316643e7SJed Brown   PetscFunctionReturn(0);
513316643e7SJed Brown }
514316643e7SJed Brown 
515316643e7SJed Brown #undef __FUNCT__
5164a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
517d763cef2SBarry Smith /*@C
518d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
519d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
520d763cef2SBarry Smith 
5213f9fe445SBarry Smith     Logically Collective on TS
522d763cef2SBarry Smith 
523d763cef2SBarry Smith     Input Parameters:
524d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
525ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
526d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
527d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
528d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
529d763cef2SBarry Smith 
530d763cef2SBarry Smith     Calling sequence of func:
53187828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
532d763cef2SBarry Smith 
533d763cef2SBarry Smith +   t - current timestep
534d763cef2SBarry Smith .   u - input vector
535d763cef2SBarry Smith .   F - function vector
536d763cef2SBarry Smith -   ctx - [optional] user-defined function context
537d763cef2SBarry Smith 
538d763cef2SBarry Smith     Important:
53976f2fa84SHong Zhang     The user MUST call either this routine or TSSetMatrices().
540d763cef2SBarry Smith 
541d763cef2SBarry Smith     Level: beginner
542d763cef2SBarry Smith 
543d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
544d763cef2SBarry Smith 
54576f2fa84SHong Zhang .seealso: TSSetMatrices()
546d763cef2SBarry Smith @*/
547089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
548d763cef2SBarry Smith {
549089b2837SJed Brown   PetscErrorCode ierr;
550089b2837SJed Brown   SNES           snes;
551d763cef2SBarry Smith 
552089b2837SJed Brown   PetscFunctionBegin;
5530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
554ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
5553b5f76d0SSean Farley   if (f)   ts->userops->rhsfunction = f;
5564e684422SJed Brown   if (ctx) ts->funP             = ctx;
557089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
558089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
559d763cef2SBarry Smith   PetscFunctionReturn(0);
560d763cef2SBarry Smith }
561d763cef2SBarry Smith 
5624a2ae208SSatish Balay #undef __FUNCT__
5634a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
564d763cef2SBarry Smith /*@C
565d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
566d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
56776f2fa84SHong Zhang    Use TSSetMatrices() for linear problems.
568d763cef2SBarry Smith 
5693f9fe445SBarry Smith    Logically Collective on TS
570d763cef2SBarry Smith 
571d763cef2SBarry Smith    Input Parameters:
572d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
573d763cef2SBarry Smith .  A   - Jacobian matrix
574d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
575d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
576d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
577d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
578d763cef2SBarry Smith 
579d763cef2SBarry Smith    Calling sequence of func:
58087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
581d763cef2SBarry Smith 
582d763cef2SBarry Smith +  t - current timestep
583d763cef2SBarry Smith .  u - input vector
584d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
585d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
586d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
58794b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
588d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
589d763cef2SBarry Smith 
590d763cef2SBarry Smith    Notes:
59194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
592d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
593d763cef2SBarry Smith 
594d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
595d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
59656335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
597d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
598d763cef2SBarry Smith    throughout the global iterations.
599d763cef2SBarry Smith 
600d763cef2SBarry Smith    Level: beginner
601d763cef2SBarry Smith 
602d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
603d763cef2SBarry Smith 
604d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(),
60576f2fa84SHong Zhang           SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices()
606d763cef2SBarry Smith 
607d763cef2SBarry Smith @*/
608089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
609d763cef2SBarry Smith {
610277b19d0SLisandro Dalcin   PetscErrorCode ierr;
611089b2837SJed Brown   SNES           snes;
612277b19d0SLisandro Dalcin 
613d763cef2SBarry Smith   PetscFunctionBegin;
6140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
615277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
616277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
617277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
618277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
619d763cef2SBarry Smith 
6203b5f76d0SSean Farley   if (f)   ts->userops->rhsjacobian = f;
621277b19d0SLisandro Dalcin   if (ctx) ts->jacP             = ctx;
622089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
623dabe61deSJed Brown   if (!ts->userops->ijacobian) {
624089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
6250e4ef248SJed Brown   }
6260e4ef248SJed Brown   if (A) {
6270e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
6280e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
6290e4ef248SJed Brown     ts->Arhs = A;
6300e4ef248SJed Brown   }
6310e4ef248SJed Brown   if (B) {
6320e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
6330e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
6340e4ef248SJed Brown     ts->Brhs = B;
6350e4ef248SJed Brown   }
636d763cef2SBarry Smith   PetscFunctionReturn(0);
637d763cef2SBarry Smith }
638d763cef2SBarry Smith 
639316643e7SJed Brown 
640316643e7SJed Brown #undef __FUNCT__
641316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
642316643e7SJed Brown /*@C
643316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
644316643e7SJed Brown 
6453f9fe445SBarry Smith    Logically Collective on TS
646316643e7SJed Brown 
647316643e7SJed Brown    Input Parameters:
648316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
649ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
650316643e7SJed Brown .  f   - the function evaluation routine
651316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
652316643e7SJed Brown 
653316643e7SJed Brown    Calling sequence of f:
654316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
655316643e7SJed Brown 
656316643e7SJed Brown +  t   - time at step/stage being solved
657316643e7SJed Brown .  u   - state vector
658316643e7SJed Brown .  u_t - time derivative of state vector
659316643e7SJed Brown .  F   - function vector
660316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
661316643e7SJed Brown 
662316643e7SJed Brown    Important:
663316643e7SJed Brown    The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices().  This routine must be used when not solving an ODE.
664316643e7SJed Brown 
665316643e7SJed Brown    Level: beginner
666316643e7SJed Brown 
667316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
668316643e7SJed Brown 
669316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian()
670316643e7SJed Brown @*/
671089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
672316643e7SJed Brown {
673089b2837SJed Brown   PetscErrorCode ierr;
674089b2837SJed Brown   SNES           snes;
675316643e7SJed Brown 
676316643e7SJed Brown   PetscFunctionBegin;
6770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
678ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
6793b5f76d0SSean Farley   if (f)   ts->userops->ifunction = f;
680089b2837SJed Brown   if (ctx) ts->funP           = ctx;
681089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
682089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
683089b2837SJed Brown   PetscFunctionReturn(0);
684089b2837SJed Brown }
685089b2837SJed Brown 
686089b2837SJed Brown #undef __FUNCT__
687089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
688089b2837SJed Brown /*@C
689089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
690089b2837SJed Brown 
691089b2837SJed Brown    Not Collective
692089b2837SJed Brown 
693089b2837SJed Brown    Input Parameter:
694089b2837SJed Brown .  ts - the TS context
695089b2837SJed Brown 
696089b2837SJed Brown    Output Parameter:
697089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
698089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
699089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
700089b2837SJed Brown 
701089b2837SJed Brown    Level: advanced
702089b2837SJed Brown 
703089b2837SJed Brown .keywords: TS, nonlinear, get, function
704089b2837SJed Brown 
705089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
706089b2837SJed Brown @*/
707089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
708089b2837SJed Brown {
709089b2837SJed Brown   PetscErrorCode ierr;
710089b2837SJed Brown   SNES snes;
711089b2837SJed Brown 
712089b2837SJed Brown   PetscFunctionBegin;
713089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
714089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
715089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7163b5f76d0SSean Farley   if (func) *func = ts->userops->ifunction;
717089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
718089b2837SJed Brown   PetscFunctionReturn(0);
719089b2837SJed Brown }
720089b2837SJed Brown 
721089b2837SJed Brown #undef __FUNCT__
722089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
723089b2837SJed Brown /*@C
724089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
725089b2837SJed Brown 
726089b2837SJed Brown    Not Collective
727089b2837SJed Brown 
728089b2837SJed Brown    Input Parameter:
729089b2837SJed Brown .  ts - the TS context
730089b2837SJed Brown 
731089b2837SJed Brown    Output Parameter:
732089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
733089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
734089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
735089b2837SJed Brown 
736089b2837SJed Brown    Level: advanced
737089b2837SJed Brown 
738089b2837SJed Brown .keywords: TS, nonlinear, get, function
739089b2837SJed Brown 
740089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
741089b2837SJed Brown @*/
742089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
743089b2837SJed Brown {
744089b2837SJed Brown   PetscErrorCode ierr;
745089b2837SJed Brown   SNES snes;
746089b2837SJed Brown 
747089b2837SJed Brown   PetscFunctionBegin;
748089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
749089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
750089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7513b5f76d0SSean Farley   if (func) *func = ts->userops->rhsfunction;
752089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
753316643e7SJed Brown   PetscFunctionReturn(0);
754316643e7SJed Brown }
755316643e7SJed Brown 
756316643e7SJed Brown #undef __FUNCT__
757316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
758316643e7SJed Brown /*@C
759a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
760a4f0a591SBarry Smith         you provided with TSSetIFunction().
761316643e7SJed Brown 
7623f9fe445SBarry Smith    Logically Collective on TS
763316643e7SJed Brown 
764316643e7SJed Brown    Input Parameters:
765316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
766316643e7SJed Brown .  A   - Jacobian matrix
767316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
768316643e7SJed Brown .  f   - the Jacobian evaluation routine
769316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
770316643e7SJed Brown 
771316643e7SJed Brown    Calling sequence of f:
7721b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
773316643e7SJed Brown 
774316643e7SJed Brown +  t    - time at step/stage being solved
7751b4a444bSJed Brown .  U    - state vector
7761b4a444bSJed Brown .  U_t  - time derivative of state vector
777316643e7SJed Brown .  a    - shift
7781b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
779316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
780316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
781316643e7SJed Brown           structure (same as flag in KSPSetOperators())
782316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
783316643e7SJed Brown 
784316643e7SJed Brown    Notes:
785316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
786316643e7SJed Brown 
787a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
788a4f0a591SBarry 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.
789a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
790a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
791a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
792a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
793a4f0a591SBarry Smith 
794316643e7SJed Brown    Level: beginner
795316643e7SJed Brown 
796316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
797316643e7SJed Brown 
798316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian()
799316643e7SJed Brown 
800316643e7SJed Brown @*/
8017087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
802316643e7SJed Brown {
803316643e7SJed Brown   PetscErrorCode ierr;
804089b2837SJed Brown   SNES           snes;
805316643e7SJed Brown 
806316643e7SJed Brown   PetscFunctionBegin;
8070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8080700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
8090700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
810316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
811316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
8123b5f76d0SSean Farley   if (f)   ts->userops->ijacobian = f;
813316643e7SJed Brown   if (ctx) ts->jacP           = ctx;
814089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
815089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
816316643e7SJed Brown   PetscFunctionReturn(0);
817316643e7SJed Brown }
818316643e7SJed Brown 
8194a2ae208SSatish Balay #undef __FUNCT__
8204a2ae208SSatish Balay #define __FUNCT__ "TSView"
8217e2c5f70SBarry Smith /*@C
822d763cef2SBarry Smith     TSView - Prints the TS data structure.
823d763cef2SBarry Smith 
8244c49b128SBarry Smith     Collective on TS
825d763cef2SBarry Smith 
826d763cef2SBarry Smith     Input Parameters:
827d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
828d763cef2SBarry Smith -   viewer - visualization context
829d763cef2SBarry Smith 
830d763cef2SBarry Smith     Options Database Key:
831d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
832d763cef2SBarry Smith 
833d763cef2SBarry Smith     Notes:
834d763cef2SBarry Smith     The available visualization contexts include
835b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
836b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
837d763cef2SBarry Smith          output where only the first processor opens
838d763cef2SBarry Smith          the file.  All other processors send their
839d763cef2SBarry Smith          data to the first processor to print.
840d763cef2SBarry Smith 
841d763cef2SBarry Smith     The user can open an alternative visualization context with
842b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
843d763cef2SBarry Smith 
844d763cef2SBarry Smith     Level: beginner
845d763cef2SBarry Smith 
846d763cef2SBarry Smith .keywords: TS, timestep, view
847d763cef2SBarry Smith 
848b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
849d763cef2SBarry Smith @*/
8507087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
851d763cef2SBarry Smith {
852dfbe8321SBarry Smith   PetscErrorCode ierr;
853a313700dSBarry Smith   const TSType   type;
854089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
855d763cef2SBarry Smith 
856d763cef2SBarry Smith   PetscFunctionBegin;
8570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8583050cee2SBarry Smith   if (!viewer) {
8597adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
8603050cee2SBarry Smith   }
8610700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
862c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
863fd16b177SBarry Smith 
8642692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
8652692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
86632077d6dSBarry Smith   if (iascii) {
867317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
868000e7ae3SMatthew Knepley     if (ts->ops->view) {
869b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
870000e7ae3SMatthew Knepley       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
871b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
872d763cef2SBarry Smith     }
87377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
874a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
875d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
87677431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr);
877193ac0bcSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->max_snes_failures);CHKERRQ(ierr);
878d763cef2SBarry Smith     }
87977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr);
880193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
8810f5bd95cSBarry Smith   } else if (isstring) {
882a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
883b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
884d763cef2SBarry Smith   }
885b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
886089b2837SJed Brown   ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
887089b2837SJed Brown   if (!isundials && ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
888b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
889d763cef2SBarry Smith   PetscFunctionReturn(0);
890d763cef2SBarry Smith }
891d763cef2SBarry Smith 
892d763cef2SBarry Smith 
8934a2ae208SSatish Balay #undef __FUNCT__
8944a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
895b07ff414SBarry Smith /*@
896d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
897d763cef2SBarry Smith    the timesteppers.
898d763cef2SBarry Smith 
8993f9fe445SBarry Smith    Logically Collective on TS
900d763cef2SBarry Smith 
901d763cef2SBarry Smith    Input Parameters:
902d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
903d763cef2SBarry Smith -  usrP - optional user context
904d763cef2SBarry Smith 
905d763cef2SBarry Smith    Level: intermediate
906d763cef2SBarry Smith 
907d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
908d763cef2SBarry Smith 
909d763cef2SBarry Smith .seealso: TSGetApplicationContext()
910d763cef2SBarry Smith @*/
9117087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
912d763cef2SBarry Smith {
913d763cef2SBarry Smith   PetscFunctionBegin;
9140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
915d763cef2SBarry Smith   ts->user = usrP;
916d763cef2SBarry Smith   PetscFunctionReturn(0);
917d763cef2SBarry Smith }
918d763cef2SBarry Smith 
9194a2ae208SSatish Balay #undef __FUNCT__
9204a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
921b07ff414SBarry Smith /*@
922d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
923d763cef2SBarry Smith     timestepper.
924d763cef2SBarry Smith 
925d763cef2SBarry Smith     Not Collective
926d763cef2SBarry Smith 
927d763cef2SBarry Smith     Input Parameter:
928d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
929d763cef2SBarry Smith 
930d763cef2SBarry Smith     Output Parameter:
931d763cef2SBarry Smith .   usrP - user context
932d763cef2SBarry Smith 
933d763cef2SBarry Smith     Level: intermediate
934d763cef2SBarry Smith 
935d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
936d763cef2SBarry Smith 
937d763cef2SBarry Smith .seealso: TSSetApplicationContext()
938d763cef2SBarry Smith @*/
939e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
940d763cef2SBarry Smith {
941d763cef2SBarry Smith   PetscFunctionBegin;
9420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
943e71120c6SJed Brown   *(void**)usrP = ts->user;
944d763cef2SBarry Smith   PetscFunctionReturn(0);
945d763cef2SBarry Smith }
946d763cef2SBarry Smith 
9474a2ae208SSatish Balay #undef __FUNCT__
9484a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
949d763cef2SBarry Smith /*@
950d763cef2SBarry Smith    TSGetTimeStepNumber - Gets the current number of timesteps.
951d763cef2SBarry Smith 
952d763cef2SBarry Smith    Not Collective
953d763cef2SBarry Smith 
954d763cef2SBarry Smith    Input Parameter:
955d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
956d763cef2SBarry Smith 
957d763cef2SBarry Smith    Output Parameter:
958d763cef2SBarry Smith .  iter - number steps so far
959d763cef2SBarry Smith 
960d763cef2SBarry Smith    Level: intermediate
961d763cef2SBarry Smith 
962d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
963d763cef2SBarry Smith @*/
9647087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
965d763cef2SBarry Smith {
966d763cef2SBarry Smith   PetscFunctionBegin;
9670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9684482741eSBarry Smith   PetscValidIntPointer(iter,2);
969d763cef2SBarry Smith   *iter = ts->steps;
970d763cef2SBarry Smith   PetscFunctionReturn(0);
971d763cef2SBarry Smith }
972d763cef2SBarry Smith 
9734a2ae208SSatish Balay #undef __FUNCT__
9744a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
975d763cef2SBarry Smith /*@
976d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
977d763cef2SBarry Smith    as well as the initial time.
978d763cef2SBarry Smith 
9793f9fe445SBarry Smith    Logically Collective on TS
980d763cef2SBarry Smith 
981d763cef2SBarry Smith    Input Parameters:
982d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
983d763cef2SBarry Smith .  initial_time - the initial time
984d763cef2SBarry Smith -  time_step - the size of the timestep
985d763cef2SBarry Smith 
986d763cef2SBarry Smith    Level: intermediate
987d763cef2SBarry Smith 
988d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
989d763cef2SBarry Smith 
990d763cef2SBarry Smith .keywords: TS, set, initial, timestep
991d763cef2SBarry Smith @*/
9927087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
993d763cef2SBarry Smith {
994e144a568SJed Brown   PetscErrorCode ierr;
995e144a568SJed Brown 
996d763cef2SBarry Smith   PetscFunctionBegin;
9970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
998e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
999d763cef2SBarry Smith   ts->initial_time_step = time_step;
1000d763cef2SBarry Smith   ts->ptime             = initial_time;
1001d763cef2SBarry Smith   PetscFunctionReturn(0);
1002d763cef2SBarry Smith }
1003d763cef2SBarry Smith 
10044a2ae208SSatish Balay #undef __FUNCT__
10054a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1006d763cef2SBarry Smith /*@
1007d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1008d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1009d763cef2SBarry Smith 
10103f9fe445SBarry Smith    Logically Collective on TS
1011d763cef2SBarry Smith 
1012d763cef2SBarry Smith    Input Parameters:
1013d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1014d763cef2SBarry Smith -  time_step - the size of the timestep
1015d763cef2SBarry Smith 
1016d763cef2SBarry Smith    Level: intermediate
1017d763cef2SBarry Smith 
1018d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1019d763cef2SBarry Smith 
1020d763cef2SBarry Smith .keywords: TS, set, timestep
1021d763cef2SBarry Smith @*/
10227087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1023d763cef2SBarry Smith {
1024d763cef2SBarry Smith   PetscFunctionBegin;
10250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1026c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1027d763cef2SBarry Smith   ts->time_step      = time_step;
1028193ac0bcSJed Brown   ts->next_time_step = time_step;
1029d763cef2SBarry Smith   PetscFunctionReturn(0);
1030d763cef2SBarry Smith }
1031d763cef2SBarry Smith 
10324a2ae208SSatish Balay #undef __FUNCT__
10334a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1034d763cef2SBarry Smith /*@
1035d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1036d763cef2SBarry Smith 
1037d763cef2SBarry Smith    Not Collective
1038d763cef2SBarry Smith 
1039d763cef2SBarry Smith    Input Parameter:
1040d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1041d763cef2SBarry Smith 
1042d763cef2SBarry Smith    Output Parameter:
1043d763cef2SBarry Smith .  dt - the current timestep size
1044d763cef2SBarry Smith 
1045d763cef2SBarry Smith    Level: intermediate
1046d763cef2SBarry Smith 
1047d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1048d763cef2SBarry Smith 
1049d763cef2SBarry Smith .keywords: TS, get, timestep
1050d763cef2SBarry Smith @*/
10517087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1052d763cef2SBarry Smith {
1053d763cef2SBarry Smith   PetscFunctionBegin;
10540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10554482741eSBarry Smith   PetscValidDoublePointer(dt,2);
1056d763cef2SBarry Smith   *dt = ts->time_step;
1057d763cef2SBarry Smith   PetscFunctionReturn(0);
1058d763cef2SBarry Smith }
1059d763cef2SBarry Smith 
10604a2ae208SSatish Balay #undef __FUNCT__
10614a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1062d8e5e3e6SSatish Balay /*@
1063d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1064d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1065d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1066d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1067d763cef2SBarry Smith 
1068d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1069d763cef2SBarry Smith 
1070d763cef2SBarry Smith    Input Parameter:
1071d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1072d763cef2SBarry Smith 
1073d763cef2SBarry Smith    Output Parameter:
1074d763cef2SBarry Smith .  v - the vector containing the solution
1075d763cef2SBarry Smith 
1076d763cef2SBarry Smith    Level: intermediate
1077d763cef2SBarry Smith 
1078d763cef2SBarry Smith .seealso: TSGetTimeStep()
1079d763cef2SBarry Smith 
1080d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1081d763cef2SBarry Smith @*/
10827087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1083d763cef2SBarry Smith {
1084d763cef2SBarry Smith   PetscFunctionBegin;
10850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10864482741eSBarry Smith   PetscValidPointer(v,2);
10878737fe31SLisandro Dalcin   *v = ts->vec_sol;
1088d763cef2SBarry Smith   PetscFunctionReturn(0);
1089d763cef2SBarry Smith }
1090d763cef2SBarry Smith 
1091bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
10924a2ae208SSatish Balay #undef __FUNCT__
1093bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1094d8e5e3e6SSatish Balay /*@
1095bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1096d763cef2SBarry Smith 
1097bdad233fSMatthew Knepley   Not collective
1098d763cef2SBarry Smith 
1099bdad233fSMatthew Knepley   Input Parameters:
1100bdad233fSMatthew Knepley + ts   - The TS
1101bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1102d763cef2SBarry Smith .vb
1103d763cef2SBarry Smith          U_t = A U
1104d763cef2SBarry Smith          U_t = A(t) U
1105d763cef2SBarry Smith          U_t = F(t,U)
1106d763cef2SBarry Smith .ve
1107d763cef2SBarry Smith 
1108d763cef2SBarry Smith    Level: beginner
1109d763cef2SBarry Smith 
1110bdad233fSMatthew Knepley .keywords: TS, problem type
1111bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1112d763cef2SBarry Smith @*/
11137087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1114a7cc72afSBarry Smith {
11159e2a6581SJed Brown   PetscErrorCode ierr;
11169e2a6581SJed Brown 
1117d763cef2SBarry Smith   PetscFunctionBegin;
11180700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1119bdad233fSMatthew Knepley   ts->problem_type = type;
11209e2a6581SJed Brown   if (type == TS_LINEAR) {
11219e2a6581SJed Brown     SNES snes;
11229e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11239e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
11249e2a6581SJed Brown   }
1125d763cef2SBarry Smith   PetscFunctionReturn(0);
1126d763cef2SBarry Smith }
1127d763cef2SBarry Smith 
1128bdad233fSMatthew Knepley #undef __FUNCT__
1129bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1130bdad233fSMatthew Knepley /*@C
1131bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1132bdad233fSMatthew Knepley 
1133bdad233fSMatthew Knepley   Not collective
1134bdad233fSMatthew Knepley 
1135bdad233fSMatthew Knepley   Input Parameter:
1136bdad233fSMatthew Knepley . ts   - The TS
1137bdad233fSMatthew Knepley 
1138bdad233fSMatthew Knepley   Output Parameter:
1139bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1140bdad233fSMatthew Knepley .vb
1141089b2837SJed Brown          M U_t = A U
1142089b2837SJed Brown          M(t) U_t = A(t) U
1143bdad233fSMatthew Knepley          U_t = F(t,U)
1144bdad233fSMatthew Knepley .ve
1145bdad233fSMatthew Knepley 
1146bdad233fSMatthew Knepley    Level: beginner
1147bdad233fSMatthew Knepley 
1148bdad233fSMatthew Knepley .keywords: TS, problem type
1149bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1150bdad233fSMatthew Knepley @*/
11517087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1152a7cc72afSBarry Smith {
1153bdad233fSMatthew Knepley   PetscFunctionBegin;
11540700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
11554482741eSBarry Smith   PetscValidIntPointer(type,2);
1156bdad233fSMatthew Knepley   *type = ts->problem_type;
1157bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1158bdad233fSMatthew Knepley }
1159d763cef2SBarry Smith 
11604a2ae208SSatish Balay #undef __FUNCT__
11614a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1162d763cef2SBarry Smith /*@
1163d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1164d763cef2SBarry Smith    of a timestepper.
1165d763cef2SBarry Smith 
1166d763cef2SBarry Smith    Collective on TS
1167d763cef2SBarry Smith 
1168d763cef2SBarry Smith    Input Parameter:
1169d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1170d763cef2SBarry Smith 
1171d763cef2SBarry Smith    Notes:
1172d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1173d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1174d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1175d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1176d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1177d763cef2SBarry Smith 
1178d763cef2SBarry Smith    Level: advanced
1179d763cef2SBarry Smith 
1180d763cef2SBarry Smith .keywords: TS, timestep, setup
1181d763cef2SBarry Smith 
1182d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1183d763cef2SBarry Smith @*/
11847087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1185d763cef2SBarry Smith {
1186dfbe8321SBarry Smith   PetscErrorCode ierr;
1187d763cef2SBarry Smith 
1188d763cef2SBarry Smith   PetscFunctionBegin;
11890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1190277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1191277b19d0SLisandro Dalcin 
11927adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
11939596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1194d763cef2SBarry Smith   }
1195277b19d0SLisandro Dalcin 
1196277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1197277b19d0SLisandro Dalcin 
1198277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1199000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1200277b19d0SLisandro Dalcin   }
1201277b19d0SLisandro Dalcin 
1202277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1203277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1204277b19d0SLisandro Dalcin }
1205277b19d0SLisandro Dalcin 
1206277b19d0SLisandro Dalcin #undef __FUNCT__
1207277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1208277b19d0SLisandro Dalcin /*@
1209277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1210277b19d0SLisandro Dalcin 
1211277b19d0SLisandro Dalcin    Collective on TS
1212277b19d0SLisandro Dalcin 
1213277b19d0SLisandro Dalcin    Input Parameter:
1214277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1215277b19d0SLisandro Dalcin 
1216277b19d0SLisandro Dalcin    Level: beginner
1217277b19d0SLisandro Dalcin 
1218277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1219277b19d0SLisandro Dalcin 
1220277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1221277b19d0SLisandro Dalcin @*/
1222277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1223277b19d0SLisandro Dalcin {
1224277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1225277b19d0SLisandro Dalcin 
1226277b19d0SLisandro Dalcin   PetscFunctionBegin;
1227277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1228277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1229277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1230277b19d0SLisandro Dalcin   }
1231277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
12324e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
12334e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1234214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
12356bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1236277b19d0SLisandro Dalcin   if (ts->work) {ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);}
1237277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1238d763cef2SBarry Smith   PetscFunctionReturn(0);
1239d763cef2SBarry Smith }
1240d763cef2SBarry Smith 
12414a2ae208SSatish Balay #undef __FUNCT__
12424a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1243d8e5e3e6SSatish Balay /*@
1244d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1245d763cef2SBarry Smith    with TSCreate().
1246d763cef2SBarry Smith 
1247d763cef2SBarry Smith    Collective on TS
1248d763cef2SBarry Smith 
1249d763cef2SBarry Smith    Input Parameter:
1250d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1251d763cef2SBarry Smith 
1252d763cef2SBarry Smith    Level: beginner
1253d763cef2SBarry Smith 
1254d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1255d763cef2SBarry Smith 
1256d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1257d763cef2SBarry Smith @*/
12586bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1259d763cef2SBarry Smith {
12606849ba73SBarry Smith   PetscErrorCode ierr;
1261d763cef2SBarry Smith 
1262d763cef2SBarry Smith   PetscFunctionBegin;
12636bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
12646bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
12656bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1266d763cef2SBarry Smith 
12676bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1268277b19d0SLisandro Dalcin 
1269be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
12706bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
12716bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
12726d4c513bSLisandro Dalcin 
12736bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
12746bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
12756bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
12766d4c513bSLisandro Dalcin 
12773b5f76d0SSean Farley   ierr = PetscFree((*ts)->userops);
12783b5f76d0SSean Farley 
1279a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1280d763cef2SBarry Smith   PetscFunctionReturn(0);
1281d763cef2SBarry Smith }
1282d763cef2SBarry Smith 
12834a2ae208SSatish Balay #undef __FUNCT__
12844a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1285d8e5e3e6SSatish Balay /*@
1286d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1287d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1288d763cef2SBarry Smith 
1289d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1290d763cef2SBarry Smith 
1291d763cef2SBarry Smith    Input Parameter:
1292d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1293d763cef2SBarry Smith 
1294d763cef2SBarry Smith    Output Parameter:
1295d763cef2SBarry Smith .  snes - the nonlinear solver context
1296d763cef2SBarry Smith 
1297d763cef2SBarry Smith    Notes:
1298d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1299d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
130094b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1301d763cef2SBarry Smith 
1302d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1303d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1304d763cef2SBarry Smith 
1305d763cef2SBarry Smith    Level: beginner
1306d763cef2SBarry Smith 
1307d763cef2SBarry Smith .keywords: timestep, get, SNES
1308d763cef2SBarry Smith @*/
13097087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1310d763cef2SBarry Smith {
1311d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1312d372ba47SLisandro Dalcin 
1313d763cef2SBarry Smith   PetscFunctionBegin;
13140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13154482741eSBarry Smith   PetscValidPointer(snes,2);
1316d372ba47SLisandro Dalcin   if (!ts->snes) {
1317d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
1318d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1319d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
13209e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
13219e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
13229e2a6581SJed Brown     }
1323d372ba47SLisandro Dalcin   }
1324d763cef2SBarry Smith   *snes = ts->snes;
1325d763cef2SBarry Smith   PetscFunctionReturn(0);
1326d763cef2SBarry Smith }
1327d763cef2SBarry Smith 
13284a2ae208SSatish Balay #undef __FUNCT__
132994b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1330d8e5e3e6SSatish Balay /*@
133194b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1332d763cef2SBarry Smith    a TS (timestepper) context.
1333d763cef2SBarry Smith 
133494b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1335d763cef2SBarry Smith 
1336d763cef2SBarry Smith    Input Parameter:
1337d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1338d763cef2SBarry Smith 
1339d763cef2SBarry Smith    Output Parameter:
134094b7f48cSBarry Smith .  ksp - the nonlinear solver context
1341d763cef2SBarry Smith 
1342d763cef2SBarry Smith    Notes:
134394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1344d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1345d763cef2SBarry Smith    KSP and PC contexts as well.
1346d763cef2SBarry Smith 
134794b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
134894b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1349d763cef2SBarry Smith 
1350d763cef2SBarry Smith    Level: beginner
1351d763cef2SBarry Smith 
135294b7f48cSBarry Smith .keywords: timestep, get, KSP
1353d763cef2SBarry Smith @*/
13547087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1355d763cef2SBarry Smith {
1356d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1357089b2837SJed Brown   SNES           snes;
1358d372ba47SLisandro Dalcin 
1359d763cef2SBarry Smith   PetscFunctionBegin;
13600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13614482741eSBarry Smith   PetscValidPointer(ksp,2);
136217186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1363e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1364089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1365089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1366d763cef2SBarry Smith   PetscFunctionReturn(0);
1367d763cef2SBarry Smith }
1368d763cef2SBarry Smith 
1369d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1370d763cef2SBarry Smith 
13714a2ae208SSatish Balay #undef __FUNCT__
1372adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1373adb62b0dSMatthew Knepley /*@
1374adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1375adb62b0dSMatthew Knepley    maximum time for iteration.
1376adb62b0dSMatthew Knepley 
13773f9fe445SBarry Smith    Not Collective
1378adb62b0dSMatthew Knepley 
1379adb62b0dSMatthew Knepley    Input Parameters:
1380adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1381adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1382adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1383adb62b0dSMatthew Knepley 
1384adb62b0dSMatthew Knepley    Level: intermediate
1385adb62b0dSMatthew Knepley 
1386adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1387adb62b0dSMatthew Knepley @*/
13887087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1389adb62b0dSMatthew Knepley {
1390adb62b0dSMatthew Knepley   PetscFunctionBegin;
13910700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1392abc0a331SBarry Smith   if (maxsteps) {
13934482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1394adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1395adb62b0dSMatthew Knepley   }
1396abc0a331SBarry Smith   if (maxtime ) {
13974482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1398adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1399adb62b0dSMatthew Knepley   }
1400adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1401adb62b0dSMatthew Knepley }
1402adb62b0dSMatthew Knepley 
1403adb62b0dSMatthew Knepley #undef __FUNCT__
14044a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1405d763cef2SBarry Smith /*@
1406d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1407d763cef2SBarry Smith    maximum time for iteration.
1408d763cef2SBarry Smith 
14093f9fe445SBarry Smith    Logically Collective on TS
1410d763cef2SBarry Smith 
1411d763cef2SBarry Smith    Input Parameters:
1412d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1413d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1414d763cef2SBarry Smith -  maxtime - final time to iterate to
1415d763cef2SBarry Smith 
1416d763cef2SBarry Smith    Options Database Keys:
1417d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
1418d763cef2SBarry Smith .  -ts_max_time <maxtime> - Sets maxtime
1419d763cef2SBarry Smith 
1420d763cef2SBarry Smith    Notes:
1421d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1422d763cef2SBarry Smith 
1423d763cef2SBarry Smith    Level: intermediate
1424d763cef2SBarry Smith 
1425d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1426d763cef2SBarry Smith @*/
14277087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1428d763cef2SBarry Smith {
1429d763cef2SBarry Smith   PetscFunctionBegin;
14300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1431c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1432c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
1433*39b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
1434*39b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1435d763cef2SBarry Smith   PetscFunctionReturn(0);
1436d763cef2SBarry Smith }
1437d763cef2SBarry Smith 
14384a2ae208SSatish Balay #undef __FUNCT__
14394a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1440d763cef2SBarry Smith /*@
1441d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1442d763cef2SBarry Smith    for use by the TS routines.
1443d763cef2SBarry Smith 
14443f9fe445SBarry Smith    Logically Collective on TS and Vec
1445d763cef2SBarry Smith 
1446d763cef2SBarry Smith    Input Parameters:
1447d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1448d763cef2SBarry Smith -  x - the solution vector
1449d763cef2SBarry Smith 
1450d763cef2SBarry Smith    Level: beginner
1451d763cef2SBarry Smith 
1452d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1453d763cef2SBarry Smith @*/
14547087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1455d763cef2SBarry Smith {
14568737fe31SLisandro Dalcin   PetscErrorCode ierr;
14578737fe31SLisandro Dalcin 
1458d763cef2SBarry Smith   PetscFunctionBegin;
14590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14600700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
14618737fe31SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
14626bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
14638737fe31SLisandro Dalcin   ts->vec_sol = x;
1464d763cef2SBarry Smith   PetscFunctionReturn(0);
1465d763cef2SBarry Smith }
1466d763cef2SBarry Smith 
1467e74ef692SMatthew Knepley #undef __FUNCT__
1468e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1469ac226902SBarry Smith /*@C
1470000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
14713f2090d5SJed Brown   called once at the beginning of each time step.
1472000e7ae3SMatthew Knepley 
14733f9fe445SBarry Smith   Logically Collective on TS
1474000e7ae3SMatthew Knepley 
1475000e7ae3SMatthew Knepley   Input Parameters:
1476000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1477000e7ae3SMatthew Knepley - func - The function
1478000e7ae3SMatthew Knepley 
1479000e7ae3SMatthew Knepley   Calling sequence of func:
1480000e7ae3SMatthew Knepley . func (TS ts);
1481000e7ae3SMatthew Knepley 
1482000e7ae3SMatthew Knepley   Level: intermediate
1483000e7ae3SMatthew Knepley 
1484000e7ae3SMatthew Knepley .keywords: TS, timestep
1485000e7ae3SMatthew Knepley @*/
14867087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1487000e7ae3SMatthew Knepley {
1488000e7ae3SMatthew Knepley   PetscFunctionBegin;
14890700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1490000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1491000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1492000e7ae3SMatthew Knepley }
1493000e7ae3SMatthew Knepley 
1494e74ef692SMatthew Knepley #undef __FUNCT__
14953f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
14963f2090d5SJed Brown /*@C
14973f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
14983f2090d5SJed Brown 
14993f2090d5SJed Brown   Collective on TS
15003f2090d5SJed Brown 
15013f2090d5SJed Brown   Input Parameters:
15023f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
15033f2090d5SJed Brown 
15043f2090d5SJed Brown   Notes:
15053f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
15063f2090d5SJed Brown   so most users would not generally call this routine themselves.
15073f2090d5SJed Brown 
15083f2090d5SJed Brown   Level: developer
15093f2090d5SJed Brown 
15103f2090d5SJed Brown .keywords: TS, timestep
15113f2090d5SJed Brown @*/
15127087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
15133f2090d5SJed Brown {
15143f2090d5SJed Brown   PetscErrorCode ierr;
15153f2090d5SJed Brown 
15163f2090d5SJed Brown   PetscFunctionBegin;
15170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
151872ac3e02SJed Brown   if (ts->ops->prestep) {
15193f2090d5SJed Brown     PetscStackPush("TS PreStep function");
15203f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
15213f2090d5SJed Brown     PetscStackPop;
1522312ce896SJed Brown   }
15233f2090d5SJed Brown   PetscFunctionReturn(0);
15243f2090d5SJed Brown }
15253f2090d5SJed Brown 
15263f2090d5SJed Brown #undef __FUNCT__
1527e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1528ac226902SBarry Smith /*@C
1529000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
15303f2090d5SJed Brown   called once at the end of each time step.
1531000e7ae3SMatthew Knepley 
15323f9fe445SBarry Smith   Logically Collective on TS
1533000e7ae3SMatthew Knepley 
1534000e7ae3SMatthew Knepley   Input Parameters:
1535000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1536000e7ae3SMatthew Knepley - func - The function
1537000e7ae3SMatthew Knepley 
1538000e7ae3SMatthew Knepley   Calling sequence of func:
1539000e7ae3SMatthew Knepley . func (TS ts);
1540000e7ae3SMatthew Knepley 
1541000e7ae3SMatthew Knepley   Level: intermediate
1542000e7ae3SMatthew Knepley 
1543000e7ae3SMatthew Knepley .keywords: TS, timestep
1544000e7ae3SMatthew Knepley @*/
15457087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1546000e7ae3SMatthew Knepley {
1547000e7ae3SMatthew Knepley   PetscFunctionBegin;
15480700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1549000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1550000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1551000e7ae3SMatthew Knepley }
1552000e7ae3SMatthew Knepley 
1553e74ef692SMatthew Knepley #undef __FUNCT__
15543f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
15553f2090d5SJed Brown /*@C
15563f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
15573f2090d5SJed Brown 
15583f2090d5SJed Brown   Collective on TS
15593f2090d5SJed Brown 
15603f2090d5SJed Brown   Input Parameters:
15613f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
15623f2090d5SJed Brown 
15633f2090d5SJed Brown   Notes:
15643f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
15653f2090d5SJed Brown   so most users would not generally call this routine themselves.
15663f2090d5SJed Brown 
15673f2090d5SJed Brown   Level: developer
15683f2090d5SJed Brown 
15693f2090d5SJed Brown .keywords: TS, timestep
15703f2090d5SJed Brown @*/
15717087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
15723f2090d5SJed Brown {
15733f2090d5SJed Brown   PetscErrorCode ierr;
15743f2090d5SJed Brown 
15753f2090d5SJed Brown   PetscFunctionBegin;
15760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
157772ac3e02SJed Brown   if (ts->ops->poststep) {
15783f2090d5SJed Brown     PetscStackPush("TS PostStep function");
15793f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
15803f2090d5SJed Brown     PetscStackPop;
158172ac3e02SJed Brown   }
15823f2090d5SJed Brown   PetscFunctionReturn(0);
15833f2090d5SJed Brown }
15843f2090d5SJed Brown 
1585d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1586d763cef2SBarry Smith 
15874a2ae208SSatish Balay #undef __FUNCT__
1588a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1589d763cef2SBarry Smith /*@C
1590a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1591d763cef2SBarry Smith    timestep to display the iteration's  progress.
1592d763cef2SBarry Smith 
15933f9fe445SBarry Smith    Logically Collective on TS
1594d763cef2SBarry Smith 
1595d763cef2SBarry Smith    Input Parameters:
1596d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1597d763cef2SBarry Smith .  func - monitoring routine
1598329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1599b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1600b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1601b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1602d763cef2SBarry Smith 
1603d763cef2SBarry Smith    Calling sequence of func:
1604a7cc72afSBarry Smith $    int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1605d763cef2SBarry Smith 
1606d763cef2SBarry Smith +    ts - the TS context
1607d763cef2SBarry Smith .    steps - iteration number
16081f06c33eSBarry Smith .    time - current time
1609d763cef2SBarry Smith .    x - current iterate
1610d763cef2SBarry Smith -    mctx - [optional] monitoring context
1611d763cef2SBarry Smith 
1612d763cef2SBarry Smith    Notes:
1613d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1614d763cef2SBarry Smith    already has been loaded.
1615d763cef2SBarry Smith 
1616025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1617025f1a04SBarry Smith 
1618d763cef2SBarry Smith    Level: intermediate
1619d763cef2SBarry Smith 
1620d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1621d763cef2SBarry Smith 
1622a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1623d763cef2SBarry Smith @*/
1624c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1625d763cef2SBarry Smith {
1626d763cef2SBarry Smith   PetscFunctionBegin;
16270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
162817186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1629d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1630329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1631d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1632d763cef2SBarry Smith   PetscFunctionReturn(0);
1633d763cef2SBarry Smith }
1634d763cef2SBarry Smith 
16354a2ae208SSatish Balay #undef __FUNCT__
1636a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1637d763cef2SBarry Smith /*@C
1638a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1639d763cef2SBarry Smith 
16403f9fe445SBarry Smith    Logically Collective on TS
1641d763cef2SBarry Smith 
1642d763cef2SBarry Smith    Input Parameters:
1643d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1644d763cef2SBarry Smith 
1645d763cef2SBarry Smith    Notes:
1646d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1647d763cef2SBarry Smith 
1648d763cef2SBarry Smith    Level: intermediate
1649d763cef2SBarry Smith 
1650d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1651d763cef2SBarry Smith 
1652a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1653d763cef2SBarry Smith @*/
16547087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1655d763cef2SBarry Smith {
1656d952e501SBarry Smith   PetscErrorCode ierr;
1657d952e501SBarry Smith   PetscInt       i;
1658d952e501SBarry Smith 
1659d763cef2SBarry Smith   PetscFunctionBegin;
16600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1661d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
1662d952e501SBarry Smith     if (ts->mdestroy[i]) {
16633c4aec1bSBarry Smith       ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
1664d952e501SBarry Smith     }
1665d952e501SBarry Smith   }
1666d763cef2SBarry Smith   ts->numbermonitors = 0;
1667d763cef2SBarry Smith   PetscFunctionReturn(0);
1668d763cef2SBarry Smith }
1669d763cef2SBarry Smith 
16704a2ae208SSatish Balay #undef __FUNCT__
1671a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
1672d8e5e3e6SSatish Balay /*@
1673a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
16745516499fSSatish Balay 
16755516499fSSatish Balay    Level: intermediate
167641251cbbSSatish Balay 
16775516499fSSatish Balay .keywords: TS, set, monitor
16785516499fSSatish Balay 
167941251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
168041251cbbSSatish Balay @*/
1681649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
1682d763cef2SBarry Smith {
1683dfbe8321SBarry Smith   PetscErrorCode ierr;
1684649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
1685d132466eSBarry Smith 
1686d763cef2SBarry Smith   PetscFunctionBegin;
1687649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1688649052a6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr);
1689649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1690d763cef2SBarry Smith   PetscFunctionReturn(0);
1691d763cef2SBarry Smith }
1692d763cef2SBarry Smith 
16934a2ae208SSatish Balay #undef __FUNCT__
16944a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1695d763cef2SBarry Smith /*@
1696d763cef2SBarry Smith    TSStep - Steps the requested number of timesteps.
1697d763cef2SBarry Smith 
1698d763cef2SBarry Smith    Collective on TS
1699d763cef2SBarry Smith 
1700d763cef2SBarry Smith    Input Parameter:
1701d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1702d763cef2SBarry Smith 
1703d763cef2SBarry Smith    Output Parameters:
1704d763cef2SBarry Smith +  steps - number of iterations until termination
1705142b95e3SSatish Balay -  ptime - time until termination
1706d763cef2SBarry Smith 
1707d763cef2SBarry Smith    Level: beginner
1708d763cef2SBarry Smith 
1709d763cef2SBarry Smith .keywords: TS, timestep, solve
1710d763cef2SBarry Smith 
1711d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy()
1712d763cef2SBarry Smith @*/
1713193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
1714d763cef2SBarry Smith {
1715dfbe8321SBarry Smith   PetscErrorCode ierr;
1716d763cef2SBarry Smith 
1717d763cef2SBarry Smith   PetscFunctionBegin;
17180700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1719277b19d0SLisandro Dalcin 
1720d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
1721d405a339SMatthew Knepley 
1722d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1723193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
1724d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1725d763cef2SBarry Smith   PetscFunctionReturn(0);
1726d763cef2SBarry Smith }
1727d763cef2SBarry Smith 
17284a2ae208SSatish Balay #undef __FUNCT__
17296a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
17306a4d4014SLisandro Dalcin /*@
17316a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
17326a4d4014SLisandro Dalcin 
17336a4d4014SLisandro Dalcin    Collective on TS
17346a4d4014SLisandro Dalcin 
17356a4d4014SLisandro Dalcin    Input Parameter:
17366a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
17376a4d4014SLisandro Dalcin -  x - the solution vector, or PETSC_NULL if it was set with TSSetSolution()
17386a4d4014SLisandro Dalcin 
17396a4d4014SLisandro Dalcin    Level: beginner
17406a4d4014SLisandro Dalcin 
17416a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
17426a4d4014SLisandro Dalcin 
17436a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
17446a4d4014SLisandro Dalcin @*/
17457087cfbeSBarry Smith PetscErrorCode  TSSolve(TS ts, Vec x)
17466a4d4014SLisandro Dalcin {
1747193ac0bcSJed Brown   PetscInt       i;
17486a4d4014SLisandro Dalcin   PetscErrorCode ierr;
1749f22f69f0SBarry Smith 
17506a4d4014SLisandro Dalcin   PetscFunctionBegin;
17510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1752193ac0bcSJed Brown   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
1753193ac0bcSJed Brown   ierr = TSSetSolution(ts,x); CHKERRQ(ierr);
1754b5d403baSSean Farley   ierr = TSSetUp(ts); CHKERRQ(ierr);
17556a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
1756193ac0bcSJed Brown   ts->steps = 0;
1757193ac0bcSJed Brown   ts->linear_its = 0;
1758193ac0bcSJed Brown   ts->nonlinear_its = 0;
1759193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
1760193ac0bcSJed Brown   ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
1761193ac0bcSJed Brown 
1762193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
1763193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
1764193ac0bcSJed Brown   } else {
1765*39b7ec4bSSean Farley     if (++i >= ts->max_steps) {
1766*39b7ec4bSSean Farley       ts->reason = TS_CONVERGED_ITS;
1767*39b7ec4bSSean Farley     } else if (ts->ptime >= ts->max_time) {
1768*39b7ec4bSSean Farley       ts->reason = TS_CONVERGED_TIME;
1769*39b7ec4bSSean Farley     }
17706a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
17713daf2de8SJed Brown     for (i=0; !ts->reason; ) {
1772193ac0bcSJed Brown       ierr = TSPreStep(ts);CHKERRQ(ierr);
1773193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
1774193ac0bcSJed Brown       if (ts->reason < 0) {
1775193ac0bcSJed Brown         if (ts->errorifstepfailed) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed");
17763daf2de8SJed Brown       } else if (++i >= ts->max_steps) {
1777193ac0bcSJed Brown         ts->reason = TS_CONVERGED_ITS;
1778193ac0bcSJed Brown       } else if (ts->ptime >= ts->max_time) {
1779193ac0bcSJed Brown         ts->reason = TS_CONVERGED_TIME;
1780193ac0bcSJed Brown       }
1781193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
1782193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
1783193ac0bcSJed Brown     }
1784193ac0bcSJed Brown   }
1785193ac0bcSJed Brown   if (!PetscPreLoadingOn) {
1786193ac0bcSJed Brown     ierr = TSViewFromOptions(ts,((PetscObject)ts)->name);CHKERRQ(ierr);
1787193ac0bcSJed Brown   }
17886a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
17896a4d4014SLisandro Dalcin }
17906a4d4014SLisandro Dalcin 
17916a4d4014SLisandro Dalcin #undef __FUNCT__
17924a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
1793d763cef2SBarry Smith /*
1794d763cef2SBarry Smith      Runs the user provided monitor routines, if they exists.
1795d763cef2SBarry Smith */
1796a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
1797d763cef2SBarry Smith {
17986849ba73SBarry Smith   PetscErrorCode ierr;
1799a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
1800d763cef2SBarry Smith 
1801d763cef2SBarry Smith   PetscFunctionBegin;
1802d763cef2SBarry Smith   for (i=0; i<n; i++) {
1803142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
1804d763cef2SBarry Smith   }
1805d763cef2SBarry Smith   PetscFunctionReturn(0);
1806d763cef2SBarry Smith }
1807d763cef2SBarry Smith 
1808d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
1809d763cef2SBarry Smith 
18104a2ae208SSatish Balay #undef __FUNCT__
1811a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate"
1812d763cef2SBarry Smith /*@C
1813a6570f20SBarry Smith    TSMonitorLGCreate - Creates a line graph context for use with
1814d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
1815d763cef2SBarry Smith 
1816d763cef2SBarry Smith    Collective on TS
1817d763cef2SBarry Smith 
1818d763cef2SBarry Smith    Input Parameters:
1819d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
1820d763cef2SBarry Smith .  label - the title to put in the title bar
18217c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
1822d763cef2SBarry Smith -  m, n - the screen width and height in pixels
1823d763cef2SBarry Smith 
1824d763cef2SBarry Smith    Output Parameter:
1825d763cef2SBarry Smith .  draw - the drawing context
1826d763cef2SBarry Smith 
1827d763cef2SBarry Smith    Options Database Key:
1828a6570f20SBarry Smith .  -ts_monitor_draw - automatically sets line graph monitor
1829d763cef2SBarry Smith 
1830d763cef2SBarry Smith    Notes:
1831a6570f20SBarry Smith    Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy().
1832d763cef2SBarry Smith 
1833d763cef2SBarry Smith    Level: intermediate
1834d763cef2SBarry Smith 
18357c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
1836d763cef2SBarry Smith 
1837a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet()
18387c922b88SBarry Smith 
1839d763cef2SBarry Smith @*/
18407087cfbeSBarry Smith PetscErrorCode  TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1841d763cef2SBarry Smith {
1842b0a32e0cSBarry Smith   PetscDraw      win;
1843dfbe8321SBarry Smith   PetscErrorCode ierr;
1844d763cef2SBarry Smith 
1845d763cef2SBarry Smith   PetscFunctionBegin;
1846b0a32e0cSBarry Smith   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
1847b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
1848b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
1849b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
1850d763cef2SBarry Smith 
185152e6d16bSBarry Smith   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
1852d763cef2SBarry Smith   PetscFunctionReturn(0);
1853d763cef2SBarry Smith }
1854d763cef2SBarry Smith 
18554a2ae208SSatish Balay #undef __FUNCT__
1856a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG"
1857a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
1858d763cef2SBarry Smith {
1859b0a32e0cSBarry Smith   PetscDrawLG    lg = (PetscDrawLG) monctx;
186087828ca2SBarry Smith   PetscReal      x,y = ptime;
1861dfbe8321SBarry Smith   PetscErrorCode ierr;
1862d763cef2SBarry Smith 
1863d763cef2SBarry Smith   PetscFunctionBegin;
18647c922b88SBarry Smith   if (!monctx) {
18657c922b88SBarry Smith     MPI_Comm    comm;
1866b0a32e0cSBarry Smith     PetscViewer viewer;
18677c922b88SBarry Smith 
18687c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
1869b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
1870b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
18717c922b88SBarry Smith   }
18727c922b88SBarry Smith 
1873b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
187487828ca2SBarry Smith   x = (PetscReal)n;
1875b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1876d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
1877b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1878d763cef2SBarry Smith   }
1879d763cef2SBarry Smith   PetscFunctionReturn(0);
1880d763cef2SBarry Smith }
1881d763cef2SBarry Smith 
18824a2ae208SSatish Balay #undef __FUNCT__
1883a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy"
1884d763cef2SBarry Smith /*@C
1885a6570f20SBarry Smith    TSMonitorLGDestroy - Destroys a line graph context that was created
1886a6570f20SBarry Smith    with TSMonitorLGCreate().
1887d763cef2SBarry Smith 
1888b0a32e0cSBarry Smith    Collective on PetscDrawLG
1889d763cef2SBarry Smith 
1890d763cef2SBarry Smith    Input Parameter:
1891d763cef2SBarry Smith .  draw - the drawing context
1892d763cef2SBarry Smith 
1893d763cef2SBarry Smith    Level: intermediate
1894d763cef2SBarry Smith 
1895d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
1896d763cef2SBarry Smith 
1897a6570f20SBarry Smith .seealso: TSMonitorLGCreate(),  TSMonitorSet(), TSMonitorLG();
1898d763cef2SBarry Smith @*/
18996bf464f9SBarry Smith PetscErrorCode  TSMonitorLGDestroy(PetscDrawLG *drawlg)
1900d763cef2SBarry Smith {
1901b0a32e0cSBarry Smith   PetscDraw      draw;
1902dfbe8321SBarry Smith   PetscErrorCode ierr;
1903d763cef2SBarry Smith 
1904d763cef2SBarry Smith   PetscFunctionBegin;
19056bf464f9SBarry Smith   ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr);
19066bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
1907b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
1908d763cef2SBarry Smith   PetscFunctionReturn(0);
1909d763cef2SBarry Smith }
1910d763cef2SBarry Smith 
19114a2ae208SSatish Balay #undef __FUNCT__
19124a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
1913d763cef2SBarry Smith /*@
1914d763cef2SBarry Smith    TSGetTime - Gets the current time.
1915d763cef2SBarry Smith 
1916d763cef2SBarry Smith    Not Collective
1917d763cef2SBarry Smith 
1918d763cef2SBarry Smith    Input Parameter:
1919d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1920d763cef2SBarry Smith 
1921d763cef2SBarry Smith    Output Parameter:
1922d763cef2SBarry Smith .  t  - the current time
1923d763cef2SBarry Smith 
1924d763cef2SBarry Smith    Level: beginner
1925d763cef2SBarry Smith 
1926d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1927d763cef2SBarry Smith 
1928d763cef2SBarry Smith .keywords: TS, get, time
1929d763cef2SBarry Smith @*/
19307087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
1931d763cef2SBarry Smith {
1932d763cef2SBarry Smith   PetscFunctionBegin;
19330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19344482741eSBarry Smith   PetscValidDoublePointer(t,2);
1935d763cef2SBarry Smith   *t = ts->ptime;
1936d763cef2SBarry Smith   PetscFunctionReturn(0);
1937d763cef2SBarry Smith }
1938d763cef2SBarry Smith 
19394a2ae208SSatish Balay #undef __FUNCT__
19406a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
19416a4d4014SLisandro Dalcin /*@
19426a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
19436a4d4014SLisandro Dalcin 
19443f9fe445SBarry Smith    Logically Collective on TS
19456a4d4014SLisandro Dalcin 
19466a4d4014SLisandro Dalcin    Input Parameters:
19476a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
19486a4d4014SLisandro Dalcin -  time - the time
19496a4d4014SLisandro Dalcin 
19506a4d4014SLisandro Dalcin    Level: intermediate
19516a4d4014SLisandro Dalcin 
19526a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
19536a4d4014SLisandro Dalcin 
19546a4d4014SLisandro Dalcin .keywords: TS, set, time
19556a4d4014SLisandro Dalcin @*/
19567087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
19576a4d4014SLisandro Dalcin {
19586a4d4014SLisandro Dalcin   PetscFunctionBegin;
19590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1960c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
19616a4d4014SLisandro Dalcin   ts->ptime = t;
19626a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
19636a4d4014SLisandro Dalcin }
19646a4d4014SLisandro Dalcin 
19656a4d4014SLisandro Dalcin #undef __FUNCT__
19664a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
1967d763cef2SBarry Smith /*@C
1968d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
1969d763cef2SBarry Smith    TS options in the database.
1970d763cef2SBarry Smith 
19713f9fe445SBarry Smith    Logically Collective on TS
1972d763cef2SBarry Smith 
1973d763cef2SBarry Smith    Input Parameter:
1974d763cef2SBarry Smith +  ts     - The TS context
1975d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
1976d763cef2SBarry Smith 
1977d763cef2SBarry Smith    Notes:
1978d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1979d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
1980d763cef2SBarry Smith    hyphen.
1981d763cef2SBarry Smith 
1982d763cef2SBarry Smith    Level: advanced
1983d763cef2SBarry Smith 
1984d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
1985d763cef2SBarry Smith 
1986d763cef2SBarry Smith .seealso: TSSetFromOptions()
1987d763cef2SBarry Smith 
1988d763cef2SBarry Smith @*/
19897087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
1990d763cef2SBarry Smith {
1991dfbe8321SBarry Smith   PetscErrorCode ierr;
1992089b2837SJed Brown   SNES           snes;
1993d763cef2SBarry Smith 
1994d763cef2SBarry Smith   PetscFunctionBegin;
19950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1996d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
1997089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1998089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
1999d763cef2SBarry Smith   PetscFunctionReturn(0);
2000d763cef2SBarry Smith }
2001d763cef2SBarry Smith 
2002d763cef2SBarry Smith 
20034a2ae208SSatish Balay #undef __FUNCT__
20044a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2005d763cef2SBarry Smith /*@C
2006d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2007d763cef2SBarry Smith    TS options in the database.
2008d763cef2SBarry Smith 
20093f9fe445SBarry Smith    Logically Collective on TS
2010d763cef2SBarry Smith 
2011d763cef2SBarry Smith    Input Parameter:
2012d763cef2SBarry Smith +  ts     - The TS context
2013d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2014d763cef2SBarry Smith 
2015d763cef2SBarry Smith    Notes:
2016d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2017d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2018d763cef2SBarry Smith    hyphen.
2019d763cef2SBarry Smith 
2020d763cef2SBarry Smith    Level: advanced
2021d763cef2SBarry Smith 
2022d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2023d763cef2SBarry Smith 
2024d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2025d763cef2SBarry Smith 
2026d763cef2SBarry Smith @*/
20277087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2028d763cef2SBarry Smith {
2029dfbe8321SBarry Smith   PetscErrorCode ierr;
2030089b2837SJed Brown   SNES           snes;
2031d763cef2SBarry Smith 
2032d763cef2SBarry Smith   PetscFunctionBegin;
20330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2034d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2035089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2036089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2037d763cef2SBarry Smith   PetscFunctionReturn(0);
2038d763cef2SBarry Smith }
2039d763cef2SBarry Smith 
20404a2ae208SSatish Balay #undef __FUNCT__
20414a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2042d763cef2SBarry Smith /*@C
2043d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2044d763cef2SBarry Smith    TS options in the database.
2045d763cef2SBarry Smith 
2046d763cef2SBarry Smith    Not Collective
2047d763cef2SBarry Smith 
2048d763cef2SBarry Smith    Input Parameter:
2049d763cef2SBarry Smith .  ts - The TS context
2050d763cef2SBarry Smith 
2051d763cef2SBarry Smith    Output Parameter:
2052d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2053d763cef2SBarry Smith 
2054d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2055d763cef2SBarry Smith    sufficient length to hold the prefix.
2056d763cef2SBarry Smith 
2057d763cef2SBarry Smith    Level: intermediate
2058d763cef2SBarry Smith 
2059d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2060d763cef2SBarry Smith 
2061d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2062d763cef2SBarry Smith @*/
20637087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2064d763cef2SBarry Smith {
2065dfbe8321SBarry Smith   PetscErrorCode ierr;
2066d763cef2SBarry Smith 
2067d763cef2SBarry Smith   PetscFunctionBegin;
20680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20694482741eSBarry Smith   PetscValidPointer(prefix,2);
2070d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2071d763cef2SBarry Smith   PetscFunctionReturn(0);
2072d763cef2SBarry Smith }
2073d763cef2SBarry Smith 
20744a2ae208SSatish Balay #undef __FUNCT__
20754a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2076d763cef2SBarry Smith /*@C
2077d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2078d763cef2SBarry Smith 
2079d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2080d763cef2SBarry Smith 
2081d763cef2SBarry Smith    Input Parameter:
2082d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2083d763cef2SBarry Smith 
2084d763cef2SBarry Smith    Output Parameters:
2085d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2086d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2087089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2088d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2089d763cef2SBarry Smith 
2090d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2091d763cef2SBarry Smith 
2092d763cef2SBarry Smith    Level: intermediate
2093d763cef2SBarry Smith 
209426d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2095d763cef2SBarry Smith 
2096d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2097d763cef2SBarry Smith @*/
2098089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2099d763cef2SBarry Smith {
2100089b2837SJed Brown   PetscErrorCode ierr;
2101089b2837SJed Brown   SNES           snes;
2102089b2837SJed Brown 
2103d763cef2SBarry Smith   PetscFunctionBegin;
2104089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2105089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
21063b5f76d0SSean Farley   if (func) *func = ts->userops->rhsjacobian;
210726d46c62SHong Zhang   if (ctx) *ctx = ts->jacP;
2108d763cef2SBarry Smith   PetscFunctionReturn(0);
2109d763cef2SBarry Smith }
2110d763cef2SBarry Smith 
21111713a123SBarry Smith #undef __FUNCT__
21122eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
21132eca1d9cSJed Brown /*@C
21142eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
21152eca1d9cSJed Brown 
21162eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
21172eca1d9cSJed Brown 
21182eca1d9cSJed Brown    Input Parameter:
21192eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
21202eca1d9cSJed Brown 
21212eca1d9cSJed Brown    Output Parameters:
21222eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
21232eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
21242eca1d9cSJed Brown .  f   - The function to compute the matrices
21252eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
21262eca1d9cSJed Brown 
21272eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
21282eca1d9cSJed Brown 
21292eca1d9cSJed Brown    Level: advanced
21302eca1d9cSJed Brown 
21312eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
21322eca1d9cSJed Brown 
21332eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
21342eca1d9cSJed Brown @*/
21357087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
21362eca1d9cSJed Brown {
2137089b2837SJed Brown   PetscErrorCode ierr;
2138089b2837SJed Brown   SNES           snes;
2139089b2837SJed Brown 
21402eca1d9cSJed Brown   PetscFunctionBegin;
2141089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2142089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
21433b5f76d0SSean Farley   if (f) *f = ts->userops->ijacobian;
21442eca1d9cSJed Brown   if (ctx) *ctx = ts->jacP;
21452eca1d9cSJed Brown   PetscFunctionReturn(0);
21462eca1d9cSJed Brown }
21472eca1d9cSJed Brown 
21482eca1d9cSJed Brown #undef __FUNCT__
2149a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution"
21501713a123SBarry Smith /*@C
2151a6570f20SBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by calling
21521713a123SBarry Smith    VecView() for the solution at each timestep
21531713a123SBarry Smith 
21541713a123SBarry Smith    Collective on TS
21551713a123SBarry Smith 
21561713a123SBarry Smith    Input Parameters:
21571713a123SBarry Smith +  ts - the TS context
21581713a123SBarry Smith .  step - current time-step
2159142b95e3SSatish Balay .  ptime - current time
21601713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
21611713a123SBarry Smith 
21621713a123SBarry Smith    Level: intermediate
21631713a123SBarry Smith 
21641713a123SBarry Smith .keywords: TS,  vector, monitor, view
21651713a123SBarry Smith 
2166a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
21671713a123SBarry Smith @*/
21687087cfbeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
21691713a123SBarry Smith {
2170dfbe8321SBarry Smith   PetscErrorCode ierr;
21711713a123SBarry Smith   PetscViewer    viewer = (PetscViewer) dummy;
21721713a123SBarry Smith 
21731713a123SBarry Smith   PetscFunctionBegin;
2174a34d58ebSBarry Smith   if (!dummy) {
21757adad957SLisandro Dalcin     viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm);
21761713a123SBarry Smith   }
21771713a123SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
21781713a123SBarry Smith   PetscFunctionReturn(0);
21791713a123SBarry Smith }
21801713a123SBarry Smith 
21811713a123SBarry Smith 
21826c699258SBarry Smith #undef __FUNCT__
21836c699258SBarry Smith #define __FUNCT__ "TSSetDM"
21846c699258SBarry Smith /*@
21856c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
21866c699258SBarry Smith 
21873f9fe445SBarry Smith    Logically Collective on TS and DM
21886c699258SBarry Smith 
21896c699258SBarry Smith    Input Parameters:
21906c699258SBarry Smith +  ts - the preconditioner context
21916c699258SBarry Smith -  dm - the dm
21926c699258SBarry Smith 
21936c699258SBarry Smith    Level: intermediate
21946c699258SBarry Smith 
21956c699258SBarry Smith 
21966c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
21976c699258SBarry Smith @*/
21987087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
21996c699258SBarry Smith {
22006c699258SBarry Smith   PetscErrorCode ierr;
2201089b2837SJed Brown   SNES           snes;
22026c699258SBarry Smith 
22036c699258SBarry Smith   PetscFunctionBegin;
22040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
220570663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
22066bf464f9SBarry Smith   ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
22076c699258SBarry Smith   ts->dm = dm;
2208089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2209089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
22106c699258SBarry Smith   PetscFunctionReturn(0);
22116c699258SBarry Smith }
22126c699258SBarry Smith 
22136c699258SBarry Smith #undef __FUNCT__
22146c699258SBarry Smith #define __FUNCT__ "TSGetDM"
22156c699258SBarry Smith /*@
22166c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
22176c699258SBarry Smith 
22183f9fe445SBarry Smith    Not Collective
22196c699258SBarry Smith 
22206c699258SBarry Smith    Input Parameter:
22216c699258SBarry Smith . ts - the preconditioner context
22226c699258SBarry Smith 
22236c699258SBarry Smith    Output Parameter:
22246c699258SBarry Smith .  dm - the dm
22256c699258SBarry Smith 
22266c699258SBarry Smith    Level: intermediate
22276c699258SBarry Smith 
22286c699258SBarry Smith 
22296c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
22306c699258SBarry Smith @*/
22317087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
22326c699258SBarry Smith {
22336c699258SBarry Smith   PetscFunctionBegin;
22340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22356c699258SBarry Smith   *dm = ts->dm;
22366c699258SBarry Smith   PetscFunctionReturn(0);
22376c699258SBarry Smith }
22381713a123SBarry Smith 
22390f5c6efeSJed Brown #undef __FUNCT__
22400f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
22410f5c6efeSJed Brown /*@
22420f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
22430f5c6efeSJed Brown 
22443f9fe445SBarry Smith    Logically Collective on SNES
22450f5c6efeSJed Brown 
22460f5c6efeSJed Brown    Input Parameter:
2247d42a1c89SJed Brown + snes - nonlinear solver
22480f5c6efeSJed Brown . X - the current state at which to evaluate the residual
2249d42a1c89SJed Brown - ctx - user context, must be a TS
22500f5c6efeSJed Brown 
22510f5c6efeSJed Brown    Output Parameter:
22520f5c6efeSJed Brown . F - the nonlinear residual
22530f5c6efeSJed Brown 
22540f5c6efeSJed Brown    Notes:
22550f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
22560f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
22570f5c6efeSJed Brown 
22580f5c6efeSJed Brown    Level: advanced
22590f5c6efeSJed Brown 
22600f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
22610f5c6efeSJed Brown @*/
22627087cfbeSBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx)
22630f5c6efeSJed Brown {
22640f5c6efeSJed Brown   TS ts = (TS)ctx;
22650f5c6efeSJed Brown   PetscErrorCode ierr;
22660f5c6efeSJed Brown 
22670f5c6efeSJed Brown   PetscFunctionBegin;
22680f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22690f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
22700f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
22710f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
22720f5c6efeSJed Brown   ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr);
22730f5c6efeSJed Brown   PetscFunctionReturn(0);
22740f5c6efeSJed Brown }
22750f5c6efeSJed Brown 
22760f5c6efeSJed Brown #undef __FUNCT__
22770f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
22780f5c6efeSJed Brown /*@
22790f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
22800f5c6efeSJed Brown 
22810f5c6efeSJed Brown    Collective on SNES
22820f5c6efeSJed Brown 
22830f5c6efeSJed Brown    Input Parameter:
22840f5c6efeSJed Brown + snes - nonlinear solver
22850f5c6efeSJed Brown . X - the current state at which to evaluate the residual
22860f5c6efeSJed Brown - ctx - user context, must be a TS
22870f5c6efeSJed Brown 
22880f5c6efeSJed Brown    Output Parameter:
22890f5c6efeSJed Brown + A - the Jacobian
22900f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
22910f5c6efeSJed Brown - flag - indicates any structure change in the matrix
22920f5c6efeSJed Brown 
22930f5c6efeSJed Brown    Notes:
22940f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
22950f5c6efeSJed Brown 
22960f5c6efeSJed Brown    Level: developer
22970f5c6efeSJed Brown 
22980f5c6efeSJed Brown .seealso: SNESSetJacobian()
22990f5c6efeSJed Brown @*/
23007087cfbeSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx)
23010f5c6efeSJed Brown {
23020f5c6efeSJed Brown   TS ts = (TS)ctx;
23030f5c6efeSJed Brown   PetscErrorCode ierr;
23040f5c6efeSJed Brown 
23050f5c6efeSJed Brown   PetscFunctionBegin;
23060f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23070f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
23080f5c6efeSJed Brown   PetscValidPointer(A,3);
23090f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
23100f5c6efeSJed Brown   PetscValidPointer(B,4);
23110f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
23120f5c6efeSJed Brown   PetscValidPointer(flag,5);
23130f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
23140f5c6efeSJed Brown   ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr);
23150f5c6efeSJed Brown   PetscFunctionReturn(0);
23160f5c6efeSJed Brown }
2317325fc9f4SBarry Smith 
23180e4ef248SJed Brown #undef __FUNCT__
23190e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
23200e4ef248SJed Brown /*@C
23210e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
23220e4ef248SJed Brown 
23230e4ef248SJed Brown    Collective on TS
23240e4ef248SJed Brown 
23250e4ef248SJed Brown    Input Arguments:
23260e4ef248SJed Brown +  ts - time stepping context
23270e4ef248SJed Brown .  t - time at which to evaluate
23280e4ef248SJed Brown .  X - state at which to evaluate
23290e4ef248SJed Brown -  ctx - context
23300e4ef248SJed Brown 
23310e4ef248SJed Brown    Output Arguments:
23320e4ef248SJed Brown .  F - right hand side
23330e4ef248SJed Brown 
23340e4ef248SJed Brown    Level: intermediate
23350e4ef248SJed Brown 
23360e4ef248SJed Brown    Notes:
23370e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
23380e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
23390e4ef248SJed Brown 
23400e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
23410e4ef248SJed Brown @*/
23420e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx)
23430e4ef248SJed Brown {
23440e4ef248SJed Brown   PetscErrorCode ierr;
23450e4ef248SJed Brown   Mat Arhs,Brhs;
23460e4ef248SJed Brown   MatStructure flg2;
23470e4ef248SJed Brown 
23480e4ef248SJed Brown   PetscFunctionBegin;
23490e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
23500e4ef248SJed Brown   ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
23510e4ef248SJed Brown   ierr = MatMult(Arhs,X,F);CHKERRQ(ierr);
23520e4ef248SJed Brown   PetscFunctionReturn(0);
23530e4ef248SJed Brown }
23540e4ef248SJed Brown 
23550e4ef248SJed Brown #undef __FUNCT__
23560e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
23570e4ef248SJed Brown /*@C
23580e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
23590e4ef248SJed Brown 
23600e4ef248SJed Brown    Collective on TS
23610e4ef248SJed Brown 
23620e4ef248SJed Brown    Input Arguments:
23630e4ef248SJed Brown +  ts - time stepping context
23640e4ef248SJed Brown .  t - time at which to evaluate
23650e4ef248SJed Brown .  X - state at which to evaluate
23660e4ef248SJed Brown -  ctx - context
23670e4ef248SJed Brown 
23680e4ef248SJed Brown    Output Arguments:
23690e4ef248SJed Brown +  A - pointer to operator
23700e4ef248SJed Brown .  B - pointer to preconditioning matrix
23710e4ef248SJed Brown -  flg - matrix structure flag
23720e4ef248SJed Brown 
23730e4ef248SJed Brown    Level: intermediate
23740e4ef248SJed Brown 
23750e4ef248SJed Brown    Notes:
23760e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
23770e4ef248SJed Brown 
23780e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
23790e4ef248SJed Brown @*/
23800e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx)
23810e4ef248SJed Brown {
23820e4ef248SJed Brown 
23830e4ef248SJed Brown   PetscFunctionBegin;
23840e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
23850e4ef248SJed Brown   PetscFunctionReturn(0);
23860e4ef248SJed Brown }
23870e4ef248SJed Brown 
23880026cea9SSean Farley #undef __FUNCT__
23890026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
23900026cea9SSean Farley /*@C
23910026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
23920026cea9SSean Farley 
23930026cea9SSean Farley    Collective on TS
23940026cea9SSean Farley 
23950026cea9SSean Farley    Input Arguments:
23960026cea9SSean Farley +  ts - time stepping context
23970026cea9SSean Farley .  t - time at which to evaluate
23980026cea9SSean Farley .  X - state at which to evaluate
23990026cea9SSean Farley .  Xdot - time derivative of state vector
24000026cea9SSean Farley -  ctx - context
24010026cea9SSean Farley 
24020026cea9SSean Farley    Output Arguments:
24030026cea9SSean Farley .  F - left hand side
24040026cea9SSean Farley 
24050026cea9SSean Farley    Level: intermediate
24060026cea9SSean Farley 
24070026cea9SSean Farley    Notes:
24080026cea9SSean 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
24090026cea9SSean Farley    user is required to write their own TSComputeIFunction.
24100026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
24110026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
24120026cea9SSean Farley 
24130026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
24140026cea9SSean Farley @*/
24150026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx)
24160026cea9SSean Farley {
24170026cea9SSean Farley   PetscErrorCode ierr;
24180026cea9SSean Farley   Mat A,B;
24190026cea9SSean Farley   MatStructure flg2;
24200026cea9SSean Farley 
24210026cea9SSean Farley   PetscFunctionBegin;
24220026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
24230026cea9SSean Farley   ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
24240026cea9SSean Farley   ierr = MatMult(A,Xdot,F);CHKERRQ(ierr);
24250026cea9SSean Farley   PetscFunctionReturn(0);
24260026cea9SSean Farley }
24270026cea9SSean Farley 
24280026cea9SSean Farley #undef __FUNCT__
24290026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
24300026cea9SSean Farley /*@C
24310026cea9SSean Farley    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
24320026cea9SSean Farley 
24330026cea9SSean Farley    Collective on TS
24340026cea9SSean Farley 
24350026cea9SSean Farley    Input Arguments:
24360026cea9SSean Farley +  ts - time stepping context
24370026cea9SSean Farley .  t - time at which to evaluate
24380026cea9SSean Farley .  X - state at which to evaluate
24390026cea9SSean Farley .  Xdot - time derivative of state vector
24400026cea9SSean Farley .  shift - shift to apply
24410026cea9SSean Farley -  ctx - context
24420026cea9SSean Farley 
24430026cea9SSean Farley    Output Arguments:
24440026cea9SSean Farley +  A - pointer to operator
24450026cea9SSean Farley .  B - pointer to preconditioning matrix
24460026cea9SSean Farley -  flg - matrix structure flag
24470026cea9SSean Farley 
24480026cea9SSean Farley    Level: intermediate
24490026cea9SSean Farley 
24500026cea9SSean Farley    Notes:
24510026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
24520026cea9SSean Farley 
24530026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
24540026cea9SSean Farley @*/
24550026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
24560026cea9SSean Farley {
24570026cea9SSean Farley 
24580026cea9SSean Farley   PetscFunctionBegin;
24590026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
24600026cea9SSean Farley   PetscFunctionReturn(0);
24610026cea9SSean Farley }
24620026cea9SSean Farley 
2463325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2464c6db04a5SJed Brown #include <mex.h>
2465325fc9f4SBarry Smith 
2466325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
2467325fc9f4SBarry Smith 
2468325fc9f4SBarry Smith #undef __FUNCT__
2469325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
2470325fc9f4SBarry Smith /*
2471325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
2472325fc9f4SBarry Smith                          TSSetFunctionMatlab().
2473325fc9f4SBarry Smith 
2474325fc9f4SBarry Smith    Collective on TS
2475325fc9f4SBarry Smith 
2476325fc9f4SBarry Smith    Input Parameters:
2477325fc9f4SBarry Smith +  snes - the TS context
2478325fc9f4SBarry Smith -  x - input vector
2479325fc9f4SBarry Smith 
2480325fc9f4SBarry Smith    Output Parameter:
2481325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
2482325fc9f4SBarry Smith 
2483325fc9f4SBarry Smith    Notes:
2484325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
2485325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
2486325fc9f4SBarry Smith    themselves.
2487325fc9f4SBarry Smith 
2488325fc9f4SBarry Smith    Level: developer
2489325fc9f4SBarry Smith 
2490325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2491325fc9f4SBarry Smith 
2492325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2493325fc9f4SBarry Smith */
24947087cfbeSBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx)
2495325fc9f4SBarry Smith {
2496325fc9f4SBarry Smith   PetscErrorCode   ierr;
2497325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2498325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
2499325fc9f4SBarry Smith   mxArray	   *plhs[1],*prhs[7];
2500325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
2501325fc9f4SBarry Smith 
2502325fc9f4SBarry Smith   PetscFunctionBegin;
2503325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2504325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2505325fc9f4SBarry Smith   PetscValidHeaderSpecific(xdot,VEC_CLASSID,4);
2506325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
2507325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,x,3);
2508325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
2509325fc9f4SBarry Smith 
2510325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2511325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2512880f3077SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr);
2513325fc9f4SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
2514325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2515325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
2516325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2517325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2518325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
2519325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
2520325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
2521325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
2522325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2523325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2524325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2525325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2526325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2527325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2528325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2529325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2530325fc9f4SBarry Smith   PetscFunctionReturn(0);
2531325fc9f4SBarry Smith }
2532325fc9f4SBarry Smith 
2533325fc9f4SBarry Smith 
2534325fc9f4SBarry Smith #undef __FUNCT__
2535325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
2536325fc9f4SBarry Smith /*
2537325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
2538325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
2539e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
2540325fc9f4SBarry Smith 
2541325fc9f4SBarry Smith    Logically Collective on TS
2542325fc9f4SBarry Smith 
2543325fc9f4SBarry Smith    Input Parameters:
2544325fc9f4SBarry Smith +  ts - the TS context
2545325fc9f4SBarry Smith -  func - function evaluation routine
2546325fc9f4SBarry Smith 
2547325fc9f4SBarry Smith    Calling sequence of func:
2548325fc9f4SBarry Smith $    func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx);
2549325fc9f4SBarry Smith 
2550325fc9f4SBarry Smith    Level: beginner
2551325fc9f4SBarry Smith 
2552325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2553325fc9f4SBarry Smith 
2554325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2555325fc9f4SBarry Smith */
25567087cfbeSBarry Smith PetscErrorCode  TSSetFunctionMatlab(TS snes,const char *func,mxArray *ctx)
2557325fc9f4SBarry Smith {
2558325fc9f4SBarry Smith   PetscErrorCode  ierr;
2559325fc9f4SBarry Smith   TSMatlabContext *sctx;
2560325fc9f4SBarry Smith 
2561325fc9f4SBarry Smith   PetscFunctionBegin;
2562325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2563325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2564325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2565325fc9f4SBarry Smith   /*
2566325fc9f4SBarry Smith      This should work, but it doesn't
2567325fc9f4SBarry Smith   sctx->ctx = ctx;
2568325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2569325fc9f4SBarry Smith   */
2570325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2571325fc9f4SBarry Smith   ierr = TSSetIFunction(snes,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
2572325fc9f4SBarry Smith   PetscFunctionReturn(0);
2573325fc9f4SBarry Smith }
2574325fc9f4SBarry Smith 
2575325fc9f4SBarry Smith #undef __FUNCT__
2576325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
2577325fc9f4SBarry Smith /*
2578325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
2579325fc9f4SBarry Smith                          TSSetJacobianMatlab().
2580325fc9f4SBarry Smith 
2581325fc9f4SBarry Smith    Collective on TS
2582325fc9f4SBarry Smith 
2583325fc9f4SBarry Smith    Input Parameters:
2584325fc9f4SBarry Smith +  snes - the TS context
2585325fc9f4SBarry Smith .  x - input vector
2586325fc9f4SBarry Smith .  A, B - the matrices
2587325fc9f4SBarry Smith -  ctx - user context
2588325fc9f4SBarry Smith 
2589325fc9f4SBarry Smith    Output Parameter:
2590325fc9f4SBarry Smith .  flag - structure of the matrix
2591325fc9f4SBarry Smith 
2592325fc9f4SBarry Smith    Level: developer
2593325fc9f4SBarry Smith 
2594325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2595325fc9f4SBarry Smith 
2596325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2597325fc9f4SBarry Smith @*/
25987087cfbeSBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
2599325fc9f4SBarry Smith {
2600325fc9f4SBarry Smith   PetscErrorCode  ierr;
2601325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2602325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
2603325fc9f4SBarry Smith   mxArray	  *plhs[2],*prhs[9];
2604325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
2605325fc9f4SBarry Smith 
2606325fc9f4SBarry Smith   PetscFunctionBegin;
2607325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2608325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2609325fc9f4SBarry Smith 
2610325fc9f4SBarry Smith   /* call Matlab function in ctx with arguments x and y */
2611325fc9f4SBarry Smith 
2612325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2613325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2614325fc9f4SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr);
2615325fc9f4SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
2616325fc9f4SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
2617325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2618325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
2619325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2620325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2621325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
2622325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
2623325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
2624325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
2625325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
2626325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
2627325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2628325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
2629325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2630325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2631325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2632325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2633325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2634325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2635325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
2636325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
2637325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2638325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
2639325fc9f4SBarry Smith   PetscFunctionReturn(0);
2640325fc9f4SBarry Smith }
2641325fc9f4SBarry Smith 
2642325fc9f4SBarry Smith 
2643325fc9f4SBarry Smith #undef __FUNCT__
2644325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
2645325fc9f4SBarry Smith /*
2646325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
2647e3c5b3baSBarry 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
2648325fc9f4SBarry Smith 
2649325fc9f4SBarry Smith    Logically Collective on TS
2650325fc9f4SBarry Smith 
2651325fc9f4SBarry Smith    Input Parameters:
2652325fc9f4SBarry Smith +  snes - the TS context
2653325fc9f4SBarry Smith .  A,B - Jacobian matrices
2654325fc9f4SBarry Smith .  func - function evaluation routine
2655325fc9f4SBarry Smith -  ctx - user context
2656325fc9f4SBarry Smith 
2657325fc9f4SBarry Smith    Calling sequence of func:
2658325fc9f4SBarry Smith $    flag = func (TS snes,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx);
2659325fc9f4SBarry Smith 
2660325fc9f4SBarry Smith 
2661325fc9f4SBarry Smith    Level: developer
2662325fc9f4SBarry Smith 
2663325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2664325fc9f4SBarry Smith 
2665325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2666325fc9f4SBarry Smith */
26677087cfbeSBarry Smith PetscErrorCode  TSSetJacobianMatlab(TS snes,Mat A,Mat B,const char *func,mxArray *ctx)
2668325fc9f4SBarry Smith {
2669325fc9f4SBarry Smith   PetscErrorCode    ierr;
2670325fc9f4SBarry Smith   TSMatlabContext *sctx;
2671325fc9f4SBarry Smith 
2672325fc9f4SBarry Smith   PetscFunctionBegin;
2673325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2674325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2675325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2676325fc9f4SBarry Smith   /*
2677325fc9f4SBarry Smith      This should work, but it doesn't
2678325fc9f4SBarry Smith   sctx->ctx = ctx;
2679325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2680325fc9f4SBarry Smith   */
2681325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2682325fc9f4SBarry Smith   ierr = TSSetIJacobian(snes,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
2683325fc9f4SBarry Smith   PetscFunctionReturn(0);
2684325fc9f4SBarry Smith }
2685325fc9f4SBarry Smith 
2686b5b1a830SBarry Smith #undef __FUNCT__
2687b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
2688b5b1a830SBarry Smith /*
2689b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
2690b5b1a830SBarry Smith 
2691b5b1a830SBarry Smith    Collective on TS
2692b5b1a830SBarry Smith 
2693b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2694b5b1a830SBarry Smith @*/
26957087cfbeSBarry Smith PetscErrorCode  TSMonitor_Matlab(TS snes,PetscInt it, PetscReal time,Vec x, void *ctx)
2696b5b1a830SBarry Smith {
2697b5b1a830SBarry Smith   PetscErrorCode  ierr;
2698b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2699a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
2700b5b1a830SBarry Smith   mxArray	  *plhs[1],*prhs[6];
2701b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
2702b5b1a830SBarry Smith 
2703b5b1a830SBarry Smith   PetscFunctionBegin;
2704b5b1a830SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2705b5b1a830SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,4);
2706b5b1a830SBarry Smith 
2707b5b1a830SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2708b5b1a830SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2709b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2710b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
2711b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
2712b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
2713b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
2714b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
2715b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
2716b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2717b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
2718b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
2719b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
2720b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
2721b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
2722b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
2723b5b1a830SBarry Smith   PetscFunctionReturn(0);
2724b5b1a830SBarry Smith }
2725b5b1a830SBarry Smith 
2726b5b1a830SBarry Smith 
2727b5b1a830SBarry Smith #undef __FUNCT__
2728b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
2729b5b1a830SBarry Smith /*
2730b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
2731b5b1a830SBarry Smith 
2732b5b1a830SBarry Smith    Level: developer
2733b5b1a830SBarry Smith 
2734b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
2735b5b1a830SBarry Smith 
2736b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2737b5b1a830SBarry Smith */
27387087cfbeSBarry Smith PetscErrorCode  TSMonitorSetMatlab(TS snes,const char *func,mxArray *ctx)
2739b5b1a830SBarry Smith {
2740b5b1a830SBarry Smith   PetscErrorCode    ierr;
2741b5b1a830SBarry Smith   TSMatlabContext *sctx;
2742b5b1a830SBarry Smith 
2743b5b1a830SBarry Smith   PetscFunctionBegin;
2744b5b1a830SBarry Smith   /* currently sctx is memory bleed */
2745b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2746b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2747b5b1a830SBarry Smith   /*
2748b5b1a830SBarry Smith      This should work, but it doesn't
2749b5b1a830SBarry Smith   sctx->ctx = ctx;
2750b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2751b5b1a830SBarry Smith   */
2752b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2753b5b1a830SBarry Smith   ierr = TSMonitorSet(snes,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
2754b5b1a830SBarry Smith   PetscFunctionReturn(0);
2755b5b1a830SBarry Smith }
2756b5b1a830SBarry Smith 
2757325fc9f4SBarry Smith #endif
2758