xref: /petsc/src/ts/interface/ts.c (revision 1b4a444bf524ae5d0f57f86c65e48a4b30d654de)
163dd3a1aSKris Buschelman #define PETSCTS_DLL
263dd3a1aSKris Buschelman 
37c4f633dSBarry Smith #include "private/tsimpl.h"        /*I "petscts.h"  I*/
4d763cef2SBarry Smith 
5d5ba7fb7SMatthew Knepley /* Logging support */
67087cfbeSBarry Smith PetscClassId  TS_CLASSID;
7166c7f25SBarry Smith PetscLogEvent  TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
8d405a339SMatthew Knepley 
94a2ae208SSatish Balay #undef __FUNCT__
10bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
11bdad233fSMatthew Knepley /*
12bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
13bdad233fSMatthew Knepley 
14bdad233fSMatthew Knepley   Collective on TS
15bdad233fSMatthew Knepley 
16bdad233fSMatthew Knepley   Input Parameter:
17bdad233fSMatthew Knepley . ts - The ts
18bdad233fSMatthew Knepley 
19bdad233fSMatthew Knepley   Level: intermediate
20bdad233fSMatthew Knepley 
21bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
22bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
23bdad233fSMatthew Knepley */
246849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
25bdad233fSMatthew Knepley {
26ace3abfcSBarry Smith   PetscBool      opt;
272fc52814SBarry Smith   const char     *defaultType;
28bdad233fSMatthew Knepley   char           typeName[256];
29dfbe8321SBarry Smith   PetscErrorCode ierr;
30bdad233fSMatthew Knepley 
31bdad233fSMatthew Knepley   PetscFunctionBegin;
327adad957SLisandro Dalcin   if (((PetscObject)ts)->type_name) {
337adad957SLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
34bdad233fSMatthew Knepley   } else {
359596e0b4SJed Brown     defaultType = TSEULER;
36bdad233fSMatthew Knepley   }
37bdad233fSMatthew Knepley 
38cce0b1b2SLisandro Dalcin   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
39bdad233fSMatthew Knepley   ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
40a7cc72afSBarry Smith   if (opt) {
41bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
42bdad233fSMatthew Knepley   } else {
43bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
44bdad233fSMatthew Knepley   }
45bdad233fSMatthew Knepley   PetscFunctionReturn(0);
46bdad233fSMatthew Knepley }
47bdad233fSMatthew Knepley 
48bdad233fSMatthew Knepley #undef __FUNCT__
49bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
50bdad233fSMatthew Knepley /*@
51bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
52bdad233fSMatthew Knepley 
53bdad233fSMatthew Knepley    Collective on TS
54bdad233fSMatthew Knepley 
55bdad233fSMatthew Knepley    Input Parameter:
56bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
57bdad233fSMatthew Knepley 
58bdad233fSMatthew Knepley    Options Database Keys:
594d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
60bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
61bdad233fSMatthew Knepley .  -ts_max_time time - maximum time to compute to
62bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
63bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
64a6570f20SBarry Smith -  -ts_monitor_draw - plot information at each timestep
65bdad233fSMatthew Knepley 
66bdad233fSMatthew Knepley    Level: beginner
67bdad233fSMatthew Knepley 
68bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
69bdad233fSMatthew Knepley 
70a313700dSBarry Smith .seealso: TSGetType()
71bdad233fSMatthew Knepley @*/
727087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
73bdad233fSMatthew Knepley {
74bdad233fSMatthew Knepley   PetscReal               dt;
75ace3abfcSBarry Smith   PetscBool               opt,flg;
76dfbe8321SBarry Smith   PetscErrorCode          ierr;
77a34d58ebSBarry Smith   PetscViewerASCIIMonitor monviewer;
78eabae89aSBarry Smith   char                    monfilename[PETSC_MAX_PATH_LEN];
79bdad233fSMatthew Knepley 
80bdad233fSMatthew Knepley   PetscFunctionBegin;
810700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
827adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)ts)->comm, ((PetscObject)ts)->prefix, "Time step options", "TS");CHKERRQ(ierr);
83bdad233fSMatthew Knepley 
84bdad233fSMatthew Knepley     /* Handle generic TS options */
85bdad233fSMatthew Knepley     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
86bdad233fSMatthew Knepley     ierr = PetscOptionsReal("-ts_max_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
87bdad233fSMatthew Knepley     ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetInitialTime", ts->ptime, &ts->ptime, PETSC_NULL);CHKERRQ(ierr);
88bdad233fSMatthew Knepley     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetInitialTimeStep",ts->initial_time_step,&dt,&opt);CHKERRQ(ierr);
89a7cc72afSBarry Smith     if (opt) {
90bdad233fSMatthew Knepley       ts->initial_time_step = ts->time_step = dt;
91bdad233fSMatthew Knepley     }
92bdad233fSMatthew Knepley 
93bdad233fSMatthew Knepley     /* Monitor options */
94a6570f20SBarry Smith     ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
95eabae89aSBarry Smith     if (flg) {
96050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)ts)->comm,monfilename,((PetscObject)ts)->tablevel,&monviewer);CHKERRQ(ierr);
97a34d58ebSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
98bdad233fSMatthew Knepley     }
9990d69ab7SBarry Smith     opt  = PETSC_FALSE;
100acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
101a7cc72afSBarry Smith     if (opt) {
102a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
103bdad233fSMatthew Knepley     }
10490d69ab7SBarry Smith     opt  = PETSC_FALSE;
105acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
106a7cc72afSBarry Smith     if (opt) {
107a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolution,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
108bdad233fSMatthew Knepley     }
109bdad233fSMatthew Knepley 
110bdad233fSMatthew Knepley     /* Handle TS type options */
111bdad233fSMatthew Knepley     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
112bdad233fSMatthew Knepley 
113bdad233fSMatthew Knepley     /* Handle specific TS options */
114abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
115bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
116bdad233fSMatthew Knepley     }
1175d973c19SBarry Smith 
1185d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
1195d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
120bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
121bdad233fSMatthew Knepley 
122bdad233fSMatthew Knepley   /* Handle subobject options */
123bdad233fSMatthew Knepley   switch(ts->problem_type) {
124156fc9a6SMatthew Knepley     /* Should check for implicit/explicit */
125bdad233fSMatthew Knepley   case TS_LINEAR:
126abc0a331SBarry Smith     if (ts->ksp) {
1278beb423aSHong Zhang       ierr = KSPSetOperators(ts->ksp,ts->Arhs,ts->B,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
12894b7f48cSBarry Smith       ierr = KSPSetFromOptions(ts->ksp);CHKERRQ(ierr);
129156fc9a6SMatthew Knepley     }
130bdad233fSMatthew Knepley     break;
131bdad233fSMatthew Knepley   case TS_NONLINEAR:
132abc0a331SBarry Smith     if (ts->snes) {
1337c236d22SBarry Smith       /* this is a bit of a hack, but it gets the matrix information into SNES earlier
1347c236d22SBarry Smith          so that SNES and KSP have more information to pick reasonable defaults
1357c0b301bSJed Brown          before they allow users to set options
1367c0b301bSJed Brown        * If ts->A has been set at this point, we are probably using the implicit form
1377c0b301bSJed Brown          and Arhs will never be used. */
1387c0b301bSJed Brown       ierr = SNESSetJacobian(ts->snes,ts->A?ts->A:ts->Arhs,ts->B,0,ts);CHKERRQ(ierr);
139bdad233fSMatthew Knepley       ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
140156fc9a6SMatthew Knepley     }
141bdad233fSMatthew Knepley     break;
142bdad233fSMatthew Knepley   default:
143e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid problem type: %d", (int)ts->problem_type);
144bdad233fSMatthew Knepley   }
145bdad233fSMatthew Knepley 
146bdad233fSMatthew Knepley   PetscFunctionReturn(0);
147bdad233fSMatthew Knepley }
148bdad233fSMatthew Knepley 
149bdad233fSMatthew Knepley #undef  __FUNCT__
150bdad233fSMatthew Knepley #define __FUNCT__ "TSViewFromOptions"
151bdad233fSMatthew Knepley /*@
152bdad233fSMatthew Knepley   TSViewFromOptions - This function visualizes the ts based upon user options.
153bdad233fSMatthew Knepley 
154bdad233fSMatthew Knepley   Collective on TS
155bdad233fSMatthew Knepley 
156bdad233fSMatthew Knepley   Input Parameter:
157bdad233fSMatthew Knepley . ts - The ts
158bdad233fSMatthew Knepley 
159bdad233fSMatthew Knepley   Level: intermediate
160bdad233fSMatthew Knepley 
161bdad233fSMatthew Knepley .keywords: TS, view, options, database
162bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSView()
163bdad233fSMatthew Knepley @*/
1647087cfbeSBarry Smith PetscErrorCode  TSViewFromOptions(TS ts,const char title[])
165bdad233fSMatthew Knepley {
166bdad233fSMatthew Knepley   PetscViewer    viewer;
167bdad233fSMatthew Knepley   PetscDraw      draw;
168ace3abfcSBarry Smith   PetscBool      opt = PETSC_FALSE;
169e10c49a3SBarry Smith   char           fileName[PETSC_MAX_PATH_LEN];
170dfbe8321SBarry Smith   PetscErrorCode ierr;
171bdad233fSMatthew Knepley 
172bdad233fSMatthew Knepley   PetscFunctionBegin;
1737adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix, "-ts_view", fileName, PETSC_MAX_PATH_LEN, &opt);CHKERRQ(ierr);
174eabae89aSBarry Smith   if (opt && !PetscPreLoadingOn) {
1757adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,fileName,&viewer);CHKERRQ(ierr);
176bdad233fSMatthew Knepley     ierr = TSView(ts, viewer);CHKERRQ(ierr);
177bdad233fSMatthew Knepley     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
178bdad233fSMatthew Knepley   }
1798e83347fSKai Germaschewski   opt = PETSC_FALSE;
180acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix, "-ts_view_draw", &opt,PETSC_NULL);CHKERRQ(ierr);
181a7cc72afSBarry Smith   if (opt) {
1827adad957SLisandro Dalcin     ierr = PetscViewerDrawOpen(((PetscObject)ts)->comm, 0, 0, 0, 0, 300, 300, &viewer);CHKERRQ(ierr);
183bdad233fSMatthew Knepley     ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
184a7cc72afSBarry Smith     if (title) {
1851836bdbcSSatish Balay       ierr = PetscDrawSetTitle(draw, (char *)title);CHKERRQ(ierr);
186bdad233fSMatthew Knepley     } else {
187bdad233fSMatthew Knepley       ierr = PetscObjectName((PetscObject)ts);CHKERRQ(ierr);
1887adad957SLisandro Dalcin       ierr = PetscDrawSetTitle(draw, ((PetscObject)ts)->name);CHKERRQ(ierr);
189bdad233fSMatthew Knepley     }
190bdad233fSMatthew Knepley     ierr = TSView(ts, viewer);CHKERRQ(ierr);
191bdad233fSMatthew Knepley     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
192bdad233fSMatthew Knepley     ierr = PetscDrawPause(draw);CHKERRQ(ierr);
193bdad233fSMatthew Knepley     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
194bdad233fSMatthew Knepley   }
195bdad233fSMatthew Knepley   PetscFunctionReturn(0);
196bdad233fSMatthew Knepley }
197bdad233fSMatthew Knepley 
198bdad233fSMatthew Knepley #undef __FUNCT__
1994a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
200a7a1495cSBarry Smith /*@
2018c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
202a7a1495cSBarry Smith       set with TSSetRHSJacobian().
203a7a1495cSBarry Smith 
204a7a1495cSBarry Smith    Collective on TS and Vec
205a7a1495cSBarry Smith 
206a7a1495cSBarry Smith    Input Parameters:
207316643e7SJed Brown +  ts - the TS context
208a7a1495cSBarry Smith .  t - current timestep
209a7a1495cSBarry Smith -  x - input vector
210a7a1495cSBarry Smith 
211a7a1495cSBarry Smith    Output Parameters:
212a7a1495cSBarry Smith +  A - Jacobian matrix
213a7a1495cSBarry Smith .  B - optional preconditioning matrix
214a7a1495cSBarry Smith -  flag - flag indicating matrix structure
215a7a1495cSBarry Smith 
216a7a1495cSBarry Smith    Notes:
217a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
218a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
219a7a1495cSBarry Smith 
22094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
221a7a1495cSBarry Smith    flag parameter.
222a7a1495cSBarry Smith 
223a7a1495cSBarry Smith    TSComputeJacobian() is valid only for TS_NONLINEAR
224a7a1495cSBarry Smith 
225a7a1495cSBarry Smith    Level: developer
226a7a1495cSBarry Smith 
227a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
228a7a1495cSBarry Smith 
22994b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
230a7a1495cSBarry Smith @*/
2317087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
232a7a1495cSBarry Smith {
233dfbe8321SBarry Smith   PetscErrorCode ierr;
234a7a1495cSBarry Smith 
235a7a1495cSBarry Smith   PetscFunctionBegin;
2360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2370700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
238c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
23917186662SBarry Smith   if (ts->problem_type != TS_NONLINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"For TS_NONLINEAR only");
240000e7ae3SMatthew Knepley   if (ts->ops->rhsjacobian) {
241d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
242a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
243a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
244000e7ae3SMatthew Knepley     ierr = (*ts->ops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr);
245a7a1495cSBarry Smith     PetscStackPop;
246d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
247a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
2480700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
2490700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
250ef66eb69SBarry Smith   } else {
251ef66eb69SBarry Smith     ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
252ef66eb69SBarry Smith     ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
253ef66eb69SBarry Smith     if (*A != *B) {
254ef66eb69SBarry Smith       ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
255ef66eb69SBarry Smith       ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
256ef66eb69SBarry Smith     }
257ef66eb69SBarry Smith   }
258a7a1495cSBarry Smith   PetscFunctionReturn(0);
259a7a1495cSBarry Smith }
260a7a1495cSBarry Smith 
2614a2ae208SSatish Balay #undef __FUNCT__
2624a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
263316643e7SJed Brown /*@
264d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
265d763cef2SBarry Smith 
266316643e7SJed Brown    Collective on TS and Vec
267316643e7SJed Brown 
268316643e7SJed Brown    Input Parameters:
269316643e7SJed Brown +  ts - the TS context
270316643e7SJed Brown .  t - current time
271316643e7SJed Brown -  x - state vector
272316643e7SJed Brown 
273316643e7SJed Brown    Output Parameter:
274316643e7SJed Brown .  y - right hand side
275316643e7SJed Brown 
276316643e7SJed Brown    Note:
277316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
278316643e7SJed Brown    is used internally within the nonlinear solvers.
279316643e7SJed Brown 
280316643e7SJed Brown    If the user did not provide a function but merely a matrix,
281d763cef2SBarry Smith    this routine applies the matrix.
282316643e7SJed Brown 
283316643e7SJed Brown    Level: developer
284316643e7SJed Brown 
285316643e7SJed Brown .keywords: TS, compute
286316643e7SJed Brown 
287316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
288316643e7SJed Brown @*/
289dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
290d763cef2SBarry Smith {
291dfbe8321SBarry Smith   PetscErrorCode ierr;
292d763cef2SBarry Smith 
293d763cef2SBarry Smith   PetscFunctionBegin;
2940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2950700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2960700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
297d763cef2SBarry Smith 
298d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
299000e7ae3SMatthew Knepley   if (ts->ops->rhsfunction) {
300d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
301000e7ae3SMatthew Knepley     ierr = (*ts->ops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr);
302d763cef2SBarry Smith     PetscStackPop;
303d16dab79SJed Brown   } else if (ts->Arhs) {
304000e7ae3SMatthew Knepley     if (ts->ops->rhsmatrix) { /* assemble matrix for this timestep */
305d763cef2SBarry Smith       MatStructure flg;
306d763cef2SBarry Smith       PetscStackPush("TS user right-hand-side matrix function");
3078beb423aSHong Zhang       ierr = (*ts->ops->rhsmatrix)(ts,t,&ts->Arhs,&ts->B,&flg,ts->jacP);CHKERRQ(ierr);
308d763cef2SBarry Smith       PetscStackPop;
309d763cef2SBarry Smith     }
3108beb423aSHong Zhang     ierr = MatMult(ts->Arhs,x,y);CHKERRQ(ierr);
311d16dab79SJed Brown   } else SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"No RHS provided, must call TSSetRHSFunction() or TSSetMatrices()");
312d763cef2SBarry Smith 
313d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
314d763cef2SBarry Smith 
315d763cef2SBarry Smith   PetscFunctionReturn(0);
316d763cef2SBarry Smith }
317d763cef2SBarry Smith 
3184a2ae208SSatish Balay #undef __FUNCT__
319316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
320316643e7SJed Brown /*@
321316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
322316643e7SJed Brown 
323316643e7SJed Brown    Collective on TS and Vec
324316643e7SJed Brown 
325316643e7SJed Brown    Input Parameters:
326316643e7SJed Brown +  ts - the TS context
327316643e7SJed Brown .  t - current time
328316643e7SJed Brown .  X - state vector
329316643e7SJed Brown -  Xdot - time derivative of state vector
330316643e7SJed Brown 
331316643e7SJed Brown    Output Parameter:
332316643e7SJed Brown .  Y - right hand side
333316643e7SJed Brown 
334316643e7SJed Brown    Note:
335316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
336316643e7SJed Brown    is used internally within the nonlinear solvers.
337316643e7SJed Brown 
338316643e7SJed Brown    If the user did did not write their equations in implicit form, this
339316643e7SJed Brown    function recasts them in implicit form.
340316643e7SJed Brown 
341316643e7SJed Brown    Level: developer
342316643e7SJed Brown 
343316643e7SJed Brown .keywords: TS, compute
344316643e7SJed Brown 
345316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
346316643e7SJed Brown @*/
347316643e7SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y)
348316643e7SJed Brown {
349316643e7SJed Brown   PetscErrorCode ierr;
350316643e7SJed Brown 
351316643e7SJed Brown   PetscFunctionBegin;
3520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3530700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3540700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
3550700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
356316643e7SJed Brown 
357316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
358316643e7SJed Brown   if (ts->ops->ifunction) {
359316643e7SJed Brown     PetscStackPush("TS user implicit function");
360316643e7SJed Brown     ierr = (*ts->ops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr);
361316643e7SJed Brown     PetscStackPop;
362316643e7SJed Brown   } else {
363316643e7SJed Brown     if (ts->ops->rhsfunction) {
364316643e7SJed Brown       PetscStackPush("TS user right-hand-side function");
365316643e7SJed Brown       ierr = (*ts->ops->rhsfunction)(ts,t,X,Y,ts->funP);CHKERRQ(ierr);
366316643e7SJed Brown       PetscStackPop;
367316643e7SJed Brown     } else {
368316643e7SJed Brown       if (ts->ops->rhsmatrix) { /* assemble matrix for this timestep */
369316643e7SJed Brown         MatStructure flg;
3704a6899ffSJed Brown         /* Note: flg is not being used.
3714a6899ffSJed Brown            For it to be useful, we'd have to cache it and then apply it in TSComputeIJacobian.
3724a6899ffSJed Brown         */
373316643e7SJed Brown         PetscStackPush("TS user right-hand-side matrix function");
374316643e7SJed Brown         ierr = (*ts->ops->rhsmatrix)(ts,t,&ts->Arhs,&ts->B,&flg,ts->jacP);CHKERRQ(ierr);
375316643e7SJed Brown         PetscStackPop;
376316643e7SJed Brown       }
377316643e7SJed Brown       ierr = MatMult(ts->Arhs,X,Y);CHKERRQ(ierr);
378316643e7SJed Brown     }
379316643e7SJed Brown 
3804a6899ffSJed Brown     /* Convert to implicit form: F(X,Xdot) = Alhs * Xdot - Frhs(X) */
3814a6899ffSJed Brown     if (ts->Alhs) {
3824a6899ffSJed Brown       if (ts->ops->lhsmatrix) {
3834a6899ffSJed Brown         MatStructure flg;
3844a6899ffSJed Brown         PetscStackPush("TS user left-hand-side matrix function");
3854a6899ffSJed Brown         ierr = (*ts->ops->lhsmatrix)(ts,t,&ts->Alhs,PETSC_NULL,&flg,ts->jacP);CHKERRQ(ierr);
3864a6899ffSJed Brown         PetscStackPop;
3874a6899ffSJed Brown       }
3884a6899ffSJed Brown       ierr = VecScale(Y,-1.);CHKERRQ(ierr);
3894a6899ffSJed Brown       ierr = MatMultAdd(ts->Alhs,Xdot,Y,Y);CHKERRQ(ierr);
3904a6899ffSJed Brown     } else {
391ace68cafSJed Brown       ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr);
392316643e7SJed Brown     }
3934a6899ffSJed Brown   }
394316643e7SJed Brown   ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
395316643e7SJed Brown   PetscFunctionReturn(0);
396316643e7SJed Brown }
397316643e7SJed Brown 
398316643e7SJed Brown #undef __FUNCT__
399316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
400316643e7SJed Brown /*@
401316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
402316643e7SJed Brown 
403316643e7SJed Brown    Collective on TS and Vec
404316643e7SJed Brown 
405316643e7SJed Brown    Input
406316643e7SJed Brown       Input Parameters:
407316643e7SJed Brown +  ts - the TS context
408316643e7SJed Brown .  t - current timestep
409316643e7SJed Brown .  X - state vector
410316643e7SJed Brown .  Xdot - time derivative of state vector
411316643e7SJed Brown -  shift - shift to apply, see note below
412316643e7SJed Brown 
413316643e7SJed Brown    Output Parameters:
414316643e7SJed Brown +  A - Jacobian matrix
415316643e7SJed Brown .  B - optional preconditioning matrix
416316643e7SJed Brown -  flag - flag indicating matrix structure
417316643e7SJed Brown 
418316643e7SJed Brown    Notes:
419316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
420316643e7SJed Brown 
421316643e7SJed Brown    dF/dX + shift*dF/dXdot
422316643e7SJed Brown 
423316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
424316643e7SJed Brown    is used internally within the nonlinear solvers.
425316643e7SJed Brown 
426316643e7SJed Brown    TSComputeIJacobian() is valid only for TS_NONLINEAR
427316643e7SJed Brown 
428316643e7SJed Brown    Level: developer
429316643e7SJed Brown 
430316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
431316643e7SJed Brown 
432316643e7SJed Brown .seealso:  TSSetIJacobian()
43363495f91SJed Brown @*/
4347087cfbeSBarry Smith PetscErrorCode  TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg)
435316643e7SJed Brown {
436316643e7SJed Brown   PetscErrorCode ierr;
437316643e7SJed Brown 
438316643e7SJed Brown   PetscFunctionBegin;
4390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4400700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4410700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
442316643e7SJed Brown   PetscValidPointer(A,6);
4430700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
444316643e7SJed Brown   PetscValidPointer(B,7);
4450700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
446316643e7SJed Brown   PetscValidPointer(flg,8);
447316643e7SJed Brown 
4484a6899ffSJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case it we're solving a linear problem in which case it wouldn't get initialized below. */
449316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
450316643e7SJed Brown   if (ts->ops->ijacobian) {
451316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
452316643e7SJed Brown     ierr = (*ts->ops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr);
453316643e7SJed Brown     PetscStackPop;
454316643e7SJed Brown   } else {
455316643e7SJed Brown     if (ts->ops->rhsjacobian) {
456316643e7SJed Brown       PetscStackPush("TS user right-hand-side Jacobian");
457316643e7SJed Brown       ierr = (*ts->ops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr);
458316643e7SJed Brown       PetscStackPop;
459316643e7SJed Brown     } else {
460316643e7SJed Brown       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
461316643e7SJed Brown       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
462316643e7SJed Brown       if (*A != *B) {
463316643e7SJed Brown         ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
464316643e7SJed Brown         ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
465316643e7SJed Brown       }
466316643e7SJed Brown     }
467316643e7SJed Brown 
468316643e7SJed Brown     /* Convert to implicit form */
469316643e7SJed Brown     /* inefficient because these operations will normally traverse all matrix elements twice */
470316643e7SJed Brown     ierr = MatScale(*A,-1);CHKERRQ(ierr);
4714a6899ffSJed Brown     if (ts->Alhs) {
4724a6899ffSJed Brown       ierr = MatAXPY(*A,shift,ts->Alhs,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
4734a6899ffSJed Brown     } else {
474316643e7SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
4754a6899ffSJed Brown     }
476316643e7SJed Brown     if (*A != *B) {
477316643e7SJed Brown       ierr = MatScale(*B,-1);CHKERRQ(ierr);
478316643e7SJed Brown       ierr = MatShift(*B,shift);CHKERRQ(ierr);
479316643e7SJed Brown     }
480316643e7SJed Brown   }
481316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
482316643e7SJed Brown   PetscFunctionReturn(0);
483316643e7SJed Brown }
484316643e7SJed Brown 
485316643e7SJed Brown #undef __FUNCT__
4864a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
487d763cef2SBarry Smith /*@C
488d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
489d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
490d763cef2SBarry Smith 
4913f9fe445SBarry Smith     Logically Collective on TS
492d763cef2SBarry Smith 
493d763cef2SBarry Smith     Input Parameters:
494d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
495d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
496d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
497d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
498d763cef2SBarry Smith 
499d763cef2SBarry Smith     Calling sequence of func:
50087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
501d763cef2SBarry Smith 
502d763cef2SBarry Smith +   t - current timestep
503d763cef2SBarry Smith .   u - input vector
504d763cef2SBarry Smith .   F - function vector
505d763cef2SBarry Smith -   ctx - [optional] user-defined function context
506d763cef2SBarry Smith 
507d763cef2SBarry Smith     Important:
50876f2fa84SHong Zhang     The user MUST call either this routine or TSSetMatrices().
509d763cef2SBarry Smith 
510d763cef2SBarry Smith     Level: beginner
511d763cef2SBarry Smith 
512d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
513d763cef2SBarry Smith 
51476f2fa84SHong Zhang .seealso: TSSetMatrices()
515d763cef2SBarry Smith @*/
5167087cfbeSBarry Smith PetscErrorCode  TSSetRHSFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
517d763cef2SBarry Smith {
518d763cef2SBarry Smith   PetscFunctionBegin;
519d763cef2SBarry Smith 
5200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52117186662SBarry Smith   if (ts->problem_type == TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot set function for linear problem");
522000e7ae3SMatthew Knepley   ts->ops->rhsfunction = f;
523d763cef2SBarry Smith   ts->funP             = ctx;
524d763cef2SBarry Smith   PetscFunctionReturn(0);
525d763cef2SBarry Smith }
526d763cef2SBarry Smith 
5274a2ae208SSatish Balay #undef __FUNCT__
52895f0b562SHong Zhang #define __FUNCT__ "TSSetMatrices"
52995f0b562SHong Zhang /*@C
53095f0b562SHong Zhang    TSSetMatrices - Sets the functions to compute the matrices Alhs and Arhs,
53195f0b562SHong Zhang    where Alhs(t) U_t = Arhs(t) U.
53295f0b562SHong Zhang 
5333f9fe445SBarry Smith    Logically Collective on TS
53495f0b562SHong Zhang 
53595f0b562SHong Zhang    Input Parameters:
53695f0b562SHong Zhang +  ts   - the TS context obtained from TSCreate()
53795f0b562SHong Zhang .  Arhs - matrix
53895f0b562SHong Zhang .  frhs - the matrix evaluation routine for Arhs; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran)
53995f0b562SHong Zhang           if Arhs is not a function of t.
54095f0b562SHong Zhang .  Alhs - matrix or PETSC_NULL if Alhs is an indentity matrix.
54195f0b562SHong Zhang .  flhs - the matrix evaluation routine for Alhs; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran)
54295f0b562SHong Zhang           if Alhs is not a function of t.
54395f0b562SHong Zhang .  flag - flag indicating information about the matrix structure of Arhs and Alhs.
54495f0b562SHong Zhang           The available options are
54595f0b562SHong Zhang             SAME_NONZERO_PATTERN - Alhs has the same nonzero structure as Arhs
54695f0b562SHong Zhang             DIFFERENT_NONZERO_PATTERN - Alhs has different nonzero structure as Arhs
54795f0b562SHong Zhang -  ctx  - [optional] user-defined context for private data for the
54895f0b562SHong Zhang           matrix evaluation routine (may be PETSC_NULL)
54995f0b562SHong Zhang 
55095f0b562SHong Zhang    Calling sequence of func:
55195f0b562SHong Zhang $     func(TS ts,PetscReal t,Mat *A,Mat *B,PetscInt *flag,void *ctx);
55295f0b562SHong Zhang 
55395f0b562SHong Zhang +  t - current timestep
55495f0b562SHong Zhang .  A - matrix A, where U_t = A(t) U
55595f0b562SHong Zhang .  B - preconditioner matrix, usually the same as A
55695f0b562SHong Zhang .  flag - flag indicating information about the preconditioner matrix
55795f0b562SHong Zhang           structure (same as flag in KSPSetOperators())
55895f0b562SHong Zhang -  ctx - [optional] user-defined context for matrix evaluation routine
55995f0b562SHong Zhang 
56095f0b562SHong Zhang    Notes:
56195f0b562SHong Zhang    The routine func() takes Mat* as the matrix arguments rather than Mat.
56295f0b562SHong Zhang    This allows the matrix evaluation routine to replace Arhs or Alhs with a
56395f0b562SHong Zhang    completely new new matrix structure (not just different matrix elements)
56495f0b562SHong Zhang    when appropriate, for instance, if the nonzero structure is changing
56595f0b562SHong Zhang    throughout the global iterations.
56695f0b562SHong Zhang 
56795f0b562SHong Zhang    Important:
56895f0b562SHong Zhang    The user MUST call either this routine or TSSetRHSFunction().
56995f0b562SHong Zhang 
57095f0b562SHong Zhang    Level: beginner
57195f0b562SHong Zhang 
57295f0b562SHong Zhang .keywords: TS, timestep, set, matrix
57395f0b562SHong Zhang 
57495f0b562SHong Zhang .seealso: TSSetRHSFunction()
57595f0b562SHong Zhang @*/
5767087cfbeSBarry Smith PetscErrorCode  TSSetMatrices(TS ts,Mat Arhs,PetscErrorCode (*frhs)(TS,PetscReal,Mat*,Mat*,MatStructure*,void*),Mat Alhs,PetscErrorCode (*flhs)(TS,PetscReal,Mat*,Mat*,MatStructure*,void*),MatStructure flag,void *ctx)
57795f0b562SHong Zhang {
57895f0b562SHong Zhang   PetscFunctionBegin;
5790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
58092af4f6aSHong Zhang   if (Arhs){
5810700a824SBarry Smith     PetscValidHeaderSpecific(Arhs,MAT_CLASSID,2);
58295f0b562SHong Zhang     PetscCheckSameComm(ts,1,Arhs,2);
58395f0b562SHong Zhang     ts->Arhs           = Arhs;
58492af4f6aSHong Zhang     ts->ops->rhsmatrix = frhs;
58592af4f6aSHong Zhang   }
58692af4f6aSHong Zhang   if (Alhs){
5870700a824SBarry Smith     PetscValidHeaderSpecific(Alhs,MAT_CLASSID,4);
58892af4f6aSHong Zhang     PetscCheckSameComm(ts,1,Alhs,4);
58995f0b562SHong Zhang     ts->Alhs           = Alhs;
59092af4f6aSHong Zhang     ts->ops->lhsmatrix = flhs;
59192af4f6aSHong Zhang   }
59292af4f6aSHong Zhang 
59392af4f6aSHong Zhang   ts->jacP           = ctx;
59495f0b562SHong Zhang   ts->matflg         = flag;
59595f0b562SHong Zhang   PetscFunctionReturn(0);
59695f0b562SHong Zhang }
597d763cef2SBarry Smith 
598aa644b49SHong Zhang #undef __FUNCT__
599cda39b92SHong Zhang #define __FUNCT__ "TSGetMatrices"
600cda39b92SHong Zhang /*@C
601cda39b92SHong Zhang    TSGetMatrices - Returns the matrices Arhs and Alhs at the present timestep,
602cda39b92SHong Zhang    where Alhs(t) U_t = Arhs(t) U.
603cda39b92SHong Zhang 
604cda39b92SHong Zhang    Not Collective, but parallel objects are returned if TS is parallel
605cda39b92SHong Zhang 
606cda39b92SHong Zhang    Input Parameter:
607cda39b92SHong Zhang .  ts  - The TS context obtained from TSCreate()
608cda39b92SHong Zhang 
609cda39b92SHong Zhang    Output Parameters:
610cda39b92SHong Zhang +  Arhs - The right-hand side matrix
611cda39b92SHong Zhang .  Alhs - The left-hand side matrix
612cda39b92SHong Zhang -  ctx - User-defined context for matrix evaluation routine
613cda39b92SHong Zhang 
614cda39b92SHong Zhang    Notes: You can pass in PETSC_NULL for any return argument you do not need.
615cda39b92SHong Zhang 
616cda39b92SHong Zhang    Level: intermediate
617cda39b92SHong Zhang 
618cda39b92SHong Zhang .seealso: TSSetMatrices(), TSGetTimeStep(), TSGetTime(), TSGetTimeStepNumber(), TSGetRHSJacobian()
619cda39b92SHong Zhang 
620cda39b92SHong Zhang .keywords: TS, timestep, get, matrix
621cda39b92SHong Zhang 
622cda39b92SHong Zhang @*/
6237087cfbeSBarry Smith PetscErrorCode  TSGetMatrices(TS ts,Mat *Arhs,Mat *Alhs,void **ctx)
624cda39b92SHong Zhang {
625cda39b92SHong Zhang   PetscFunctionBegin;
6260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
627cda39b92SHong Zhang   if (Arhs) *Arhs = ts->Arhs;
628cda39b92SHong Zhang   if (Alhs) *Alhs = ts->Alhs;
629cda39b92SHong Zhang   if (ctx)  *ctx = ts->jacP;
630cda39b92SHong Zhang   PetscFunctionReturn(0);
631cda39b92SHong Zhang }
632cda39b92SHong Zhang 
633cda39b92SHong Zhang #undef __FUNCT__
6344a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
635d763cef2SBarry Smith /*@C
636d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
637d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
63876f2fa84SHong Zhang    Use TSSetMatrices() for linear problems.
639d763cef2SBarry Smith 
6403f9fe445SBarry Smith    Logically Collective on TS
641d763cef2SBarry Smith 
642d763cef2SBarry Smith    Input Parameters:
643d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
644d763cef2SBarry Smith .  A   - Jacobian matrix
645d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
646d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
647d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
648d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
649d763cef2SBarry Smith 
650d763cef2SBarry Smith    Calling sequence of func:
65187828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
652d763cef2SBarry Smith 
653d763cef2SBarry Smith +  t - current timestep
654d763cef2SBarry Smith .  u - input vector
655d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
656d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
657d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
65894b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
659d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
660d763cef2SBarry Smith 
661d763cef2SBarry Smith    Notes:
66294b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
663d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
664d763cef2SBarry Smith 
665d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
666d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
66756335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
668d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
669d763cef2SBarry Smith    throughout the global iterations.
670d763cef2SBarry Smith 
671d763cef2SBarry Smith    Level: beginner
672d763cef2SBarry Smith 
673d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
674d763cef2SBarry Smith 
675d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(),
67676f2fa84SHong Zhang           SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices()
677d763cef2SBarry Smith 
678d763cef2SBarry Smith @*/
6797087cfbeSBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,PetscErrorCode (*f)(TS,PetscReal,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
680d763cef2SBarry Smith {
681d763cef2SBarry Smith   PetscFunctionBegin;
6820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6830700a824SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,2);
6840700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,3);
685c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,A,2);
686c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,B,3);
68717186662SBarry Smith   if (ts->problem_type != TS_NONLINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not for linear problems; use TSSetMatrices()");
688d763cef2SBarry Smith 
689000e7ae3SMatthew Knepley   ts->ops->rhsjacobian = f;
690d763cef2SBarry Smith   ts->jacP             = ctx;
6918beb423aSHong Zhang   ts->Arhs             = A;
692d763cef2SBarry Smith   ts->B                = B;
693d763cef2SBarry Smith   PetscFunctionReturn(0);
694d763cef2SBarry Smith }
695d763cef2SBarry Smith 
696316643e7SJed Brown 
697316643e7SJed Brown #undef __FUNCT__
698316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
699316643e7SJed Brown /*@C
700316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
701316643e7SJed Brown 
7023f9fe445SBarry Smith    Logically Collective on TS
703316643e7SJed Brown 
704316643e7SJed Brown    Input Parameters:
705316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
706316643e7SJed Brown .  f   - the function evaluation routine
707316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
708316643e7SJed Brown 
709316643e7SJed Brown    Calling sequence of f:
710316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
711316643e7SJed Brown 
712316643e7SJed Brown +  t   - time at step/stage being solved
713316643e7SJed Brown .  u   - state vector
714316643e7SJed Brown .  u_t - time derivative of state vector
715316643e7SJed Brown .  F   - function vector
716316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
717316643e7SJed Brown 
718316643e7SJed Brown    Important:
719316643e7SJed Brown    The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices().  This routine must be used when not solving an ODE.
720316643e7SJed Brown 
721316643e7SJed Brown    Level: beginner
722316643e7SJed Brown 
723316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
724316643e7SJed Brown 
725316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian()
726316643e7SJed Brown @*/
7277087cfbeSBarry Smith PetscErrorCode  TSSetIFunction(TS ts,TSIFunction f,void *ctx)
728316643e7SJed Brown {
729316643e7SJed Brown 
730316643e7SJed Brown   PetscFunctionBegin;
7310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
732316643e7SJed Brown   ts->ops->ifunction = f;
733316643e7SJed Brown   ts->funP           = ctx;
734316643e7SJed Brown   PetscFunctionReturn(0);
735316643e7SJed Brown }
736316643e7SJed Brown 
737316643e7SJed Brown #undef __FUNCT__
738316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
739316643e7SJed Brown /*@C
740*1b4a444bSJed Brown    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t which is
741*1b4a444bSJed Brown    the Jacobian of G(U) = F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
742*1b4a444bSJed Brown    The time integrator internally approximates U_t by W+a*U where the positive "shift"
743*1b4a444bSJed Brown    a and vector W depend on the integration method, step size, and past states.
744316643e7SJed Brown 
7453f9fe445SBarry Smith    Logically Collective on TS
746316643e7SJed Brown 
747316643e7SJed Brown    Input Parameters:
748316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
749316643e7SJed Brown .  A   - Jacobian matrix
750316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
751316643e7SJed Brown .  f   - the Jacobian evaluation routine
752316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
753316643e7SJed Brown 
754316643e7SJed Brown    Calling sequence of f:
755*1b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
756316643e7SJed Brown 
757316643e7SJed Brown +  t    - time at step/stage being solved
758*1b4a444bSJed Brown .  U    - state vector
759*1b4a444bSJed Brown .  U_t  - time derivative of state vector
760316643e7SJed Brown .  a    - shift
761*1b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
762316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
763316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
764316643e7SJed Brown           structure (same as flag in KSPSetOperators())
765316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
766316643e7SJed Brown 
767316643e7SJed Brown    Notes:
768316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
769316643e7SJed Brown 
770316643e7SJed Brown    Level: beginner
771316643e7SJed Brown 
772316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
773316643e7SJed Brown 
774316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian()
775316643e7SJed Brown 
776316643e7SJed Brown @*/
7777087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
778316643e7SJed Brown {
779316643e7SJed Brown   PetscErrorCode ierr;
780316643e7SJed Brown 
781316643e7SJed Brown   PetscFunctionBegin;
7820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7830700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
7840700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
785316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
786316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
787316643e7SJed Brown   if (f)   ts->ops->ijacobian = f;
788316643e7SJed Brown   if (ctx) ts->jacP             = ctx;
789316643e7SJed Brown   if (A) {
790316643e7SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
791316643e7SJed Brown     if (ts->A) {ierr = MatDestroy(ts->A);CHKERRQ(ierr);}
792316643e7SJed Brown     ts->A = A;
793316643e7SJed Brown   }
794dc3f620dSJed Brown #if 0
795dc3f620dSJed Brown   /* The sane and consistent alternative */
796316643e7SJed Brown   if (B) {
797316643e7SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
798316643e7SJed Brown     if (ts->B) {ierr = MatDestroy(ts->B);CHKERRQ(ierr);}
799316643e7SJed Brown     ts->B = B;
800316643e7SJed Brown   }
801dc3f620dSJed Brown #else
802dc3f620dSJed Brown   /* Don't reference B because TSDestroy() doesn't destroy it.  These ownership semantics are awkward and inconsistent. */
803dc3f620dSJed Brown   if (B) ts->B = B;
804dc3f620dSJed Brown #endif
805316643e7SJed Brown   PetscFunctionReturn(0);
806316643e7SJed Brown }
807316643e7SJed Brown 
8084a2ae208SSatish Balay #undef __FUNCT__
8094a2ae208SSatish Balay #define __FUNCT__ "TSView"
8107e2c5f70SBarry Smith /*@C
811d763cef2SBarry Smith     TSView - Prints the TS data structure.
812d763cef2SBarry Smith 
8134c49b128SBarry Smith     Collective on TS
814d763cef2SBarry Smith 
815d763cef2SBarry Smith     Input Parameters:
816d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
817d763cef2SBarry Smith -   viewer - visualization context
818d763cef2SBarry Smith 
819d763cef2SBarry Smith     Options Database Key:
820d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
821d763cef2SBarry Smith 
822d763cef2SBarry Smith     Notes:
823d763cef2SBarry Smith     The available visualization contexts include
824b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
825b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
826d763cef2SBarry Smith          output where only the first processor opens
827d763cef2SBarry Smith          the file.  All other processors send their
828d763cef2SBarry Smith          data to the first processor to print.
829d763cef2SBarry Smith 
830d763cef2SBarry Smith     The user can open an alternative visualization context with
831b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
832d763cef2SBarry Smith 
833d763cef2SBarry Smith     Level: beginner
834d763cef2SBarry Smith 
835d763cef2SBarry Smith .keywords: TS, timestep, view
836d763cef2SBarry Smith 
837b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
838d763cef2SBarry Smith @*/
8397087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
840d763cef2SBarry Smith {
841dfbe8321SBarry Smith   PetscErrorCode ierr;
842a313700dSBarry Smith   const TSType   type;
843ace3abfcSBarry Smith   PetscBool      iascii,isstring;
844d763cef2SBarry Smith 
845d763cef2SBarry Smith   PetscFunctionBegin;
8460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8473050cee2SBarry Smith   if (!viewer) {
8487adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
8493050cee2SBarry Smith   }
8500700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
851c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
852fd16b177SBarry Smith 
8532692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
8542692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
85532077d6dSBarry Smith   if (iascii) {
856317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
857000e7ae3SMatthew Knepley     if (ts->ops->view) {
858b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
859000e7ae3SMatthew Knepley       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
860b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
861d763cef2SBarry Smith     }
86277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
863a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
864d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
86577431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr);
866d763cef2SBarry Smith     }
86777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr);
8680f5bd95cSBarry Smith   } else if (isstring) {
869a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
870b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
871d763cef2SBarry Smith   }
872b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
87394b7f48cSBarry Smith   if (ts->ksp) {ierr = KSPView(ts->ksp,viewer);CHKERRQ(ierr);}
874d763cef2SBarry Smith   if (ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
875b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
876d763cef2SBarry Smith   PetscFunctionReturn(0);
877d763cef2SBarry Smith }
878d763cef2SBarry Smith 
879d763cef2SBarry Smith 
8804a2ae208SSatish Balay #undef __FUNCT__
8814a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
882d763cef2SBarry Smith /*@C
883d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
884d763cef2SBarry Smith    the timesteppers.
885d763cef2SBarry Smith 
8863f9fe445SBarry Smith    Logically Collective on TS
887d763cef2SBarry Smith 
888d763cef2SBarry Smith    Input Parameters:
889d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
890d763cef2SBarry Smith -  usrP - optional user context
891d763cef2SBarry Smith 
892d763cef2SBarry Smith    Level: intermediate
893d763cef2SBarry Smith 
894d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
895d763cef2SBarry Smith 
896d763cef2SBarry Smith .seealso: TSGetApplicationContext()
897d763cef2SBarry Smith @*/
8987087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
899d763cef2SBarry Smith {
900d763cef2SBarry Smith   PetscFunctionBegin;
9010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
902d763cef2SBarry Smith   ts->user = usrP;
903d763cef2SBarry Smith   PetscFunctionReturn(0);
904d763cef2SBarry Smith }
905d763cef2SBarry Smith 
9064a2ae208SSatish Balay #undef __FUNCT__
9074a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
908d763cef2SBarry Smith /*@C
909d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
910d763cef2SBarry Smith     timestepper.
911d763cef2SBarry Smith 
912d763cef2SBarry Smith     Not Collective
913d763cef2SBarry Smith 
914d763cef2SBarry Smith     Input Parameter:
915d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
916d763cef2SBarry Smith 
917d763cef2SBarry Smith     Output Parameter:
918d763cef2SBarry Smith .   usrP - user context
919d763cef2SBarry Smith 
920d763cef2SBarry Smith     Level: intermediate
921d763cef2SBarry Smith 
922d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
923d763cef2SBarry Smith 
924d763cef2SBarry Smith .seealso: TSSetApplicationContext()
925d763cef2SBarry Smith @*/
9267087cfbeSBarry Smith PetscErrorCode  TSGetApplicationContext(TS ts,void **usrP)
927d763cef2SBarry Smith {
928d763cef2SBarry Smith   PetscFunctionBegin;
9290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
930d763cef2SBarry Smith   *usrP = ts->user;
931d763cef2SBarry Smith   PetscFunctionReturn(0);
932d763cef2SBarry Smith }
933d763cef2SBarry Smith 
9344a2ae208SSatish Balay #undef __FUNCT__
9354a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
936d763cef2SBarry Smith /*@
937d763cef2SBarry Smith    TSGetTimeStepNumber - Gets the current number of timesteps.
938d763cef2SBarry Smith 
939d763cef2SBarry Smith    Not Collective
940d763cef2SBarry Smith 
941d763cef2SBarry Smith    Input Parameter:
942d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
943d763cef2SBarry Smith 
944d763cef2SBarry Smith    Output Parameter:
945d763cef2SBarry Smith .  iter - number steps so far
946d763cef2SBarry Smith 
947d763cef2SBarry Smith    Level: intermediate
948d763cef2SBarry Smith 
949d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
950d763cef2SBarry Smith @*/
9517087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
952d763cef2SBarry Smith {
953d763cef2SBarry Smith   PetscFunctionBegin;
9540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9554482741eSBarry Smith   PetscValidIntPointer(iter,2);
956d763cef2SBarry Smith   *iter = ts->steps;
957d763cef2SBarry Smith   PetscFunctionReturn(0);
958d763cef2SBarry Smith }
959d763cef2SBarry Smith 
9604a2ae208SSatish Balay #undef __FUNCT__
9614a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
962d763cef2SBarry Smith /*@
963d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
964d763cef2SBarry Smith    as well as the initial time.
965d763cef2SBarry Smith 
9663f9fe445SBarry Smith    Logically Collective on TS
967d763cef2SBarry Smith 
968d763cef2SBarry Smith    Input Parameters:
969d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
970d763cef2SBarry Smith .  initial_time - the initial time
971d763cef2SBarry Smith -  time_step - the size of the timestep
972d763cef2SBarry Smith 
973d763cef2SBarry Smith    Level: intermediate
974d763cef2SBarry Smith 
975d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
976d763cef2SBarry Smith 
977d763cef2SBarry Smith .keywords: TS, set, initial, timestep
978d763cef2SBarry Smith @*/
9797087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
980d763cef2SBarry Smith {
981d763cef2SBarry Smith   PetscFunctionBegin;
9820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
983d763cef2SBarry Smith   ts->time_step         = time_step;
984d763cef2SBarry Smith   ts->initial_time_step = time_step;
985d763cef2SBarry Smith   ts->ptime             = initial_time;
986d763cef2SBarry Smith   PetscFunctionReturn(0);
987d763cef2SBarry Smith }
988d763cef2SBarry Smith 
9894a2ae208SSatish Balay #undef __FUNCT__
9904a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
991d763cef2SBarry Smith /*@
992d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
993d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
994d763cef2SBarry Smith 
9953f9fe445SBarry Smith    Logically Collective on TS
996d763cef2SBarry Smith 
997d763cef2SBarry Smith    Input Parameters:
998d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
999d763cef2SBarry Smith -  time_step - the size of the timestep
1000d763cef2SBarry Smith 
1001d763cef2SBarry Smith    Level: intermediate
1002d763cef2SBarry Smith 
1003d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1004d763cef2SBarry Smith 
1005d763cef2SBarry Smith .keywords: TS, set, timestep
1006d763cef2SBarry Smith @*/
10077087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1008d763cef2SBarry Smith {
1009d763cef2SBarry Smith   PetscFunctionBegin;
10100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1011c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1012d763cef2SBarry Smith   ts->time_step = time_step;
1013d763cef2SBarry Smith   PetscFunctionReturn(0);
1014d763cef2SBarry Smith }
1015d763cef2SBarry Smith 
10164a2ae208SSatish Balay #undef __FUNCT__
10174a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1018d763cef2SBarry Smith /*@
1019d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1020d763cef2SBarry Smith 
1021d763cef2SBarry Smith    Not Collective
1022d763cef2SBarry Smith 
1023d763cef2SBarry Smith    Input Parameter:
1024d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1025d763cef2SBarry Smith 
1026d763cef2SBarry Smith    Output Parameter:
1027d763cef2SBarry Smith .  dt - the current timestep size
1028d763cef2SBarry Smith 
1029d763cef2SBarry Smith    Level: intermediate
1030d763cef2SBarry Smith 
1031d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1032d763cef2SBarry Smith 
1033d763cef2SBarry Smith .keywords: TS, get, timestep
1034d763cef2SBarry Smith @*/
10357087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1036d763cef2SBarry Smith {
1037d763cef2SBarry Smith   PetscFunctionBegin;
10380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10394482741eSBarry Smith   PetscValidDoublePointer(dt,2);
1040d763cef2SBarry Smith   *dt = ts->time_step;
1041d763cef2SBarry Smith   PetscFunctionReturn(0);
1042d763cef2SBarry Smith }
1043d763cef2SBarry Smith 
10444a2ae208SSatish Balay #undef __FUNCT__
10454a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1046d8e5e3e6SSatish Balay /*@
1047d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1048d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1049d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1050d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1051d763cef2SBarry Smith 
1052d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1053d763cef2SBarry Smith 
1054d763cef2SBarry Smith    Input Parameter:
1055d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1056d763cef2SBarry Smith 
1057d763cef2SBarry Smith    Output Parameter:
1058d763cef2SBarry Smith .  v - the vector containing the solution
1059d763cef2SBarry Smith 
1060d763cef2SBarry Smith    Level: intermediate
1061d763cef2SBarry Smith 
1062d763cef2SBarry Smith .seealso: TSGetTimeStep()
1063d763cef2SBarry Smith 
1064d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1065d763cef2SBarry Smith @*/
10667087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1067d763cef2SBarry Smith {
1068d763cef2SBarry Smith   PetscFunctionBegin;
10690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10704482741eSBarry Smith   PetscValidPointer(v,2);
1071d763cef2SBarry Smith   *v = ts->vec_sol_always;
1072d763cef2SBarry Smith   PetscFunctionReturn(0);
1073d763cef2SBarry Smith }
1074d763cef2SBarry Smith 
1075bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
10764a2ae208SSatish Balay #undef __FUNCT__
1077bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1078d8e5e3e6SSatish Balay /*@
1079bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1080d763cef2SBarry Smith 
1081bdad233fSMatthew Knepley   Not collective
1082d763cef2SBarry Smith 
1083bdad233fSMatthew Knepley   Input Parameters:
1084bdad233fSMatthew Knepley + ts   - The TS
1085bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1086d763cef2SBarry Smith .vb
1087d763cef2SBarry Smith          U_t = A U
1088d763cef2SBarry Smith          U_t = A(t) U
1089d763cef2SBarry Smith          U_t = F(t,U)
1090d763cef2SBarry Smith .ve
1091d763cef2SBarry Smith 
1092d763cef2SBarry Smith    Level: beginner
1093d763cef2SBarry Smith 
1094bdad233fSMatthew Knepley .keywords: TS, problem type
1095bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1096d763cef2SBarry Smith @*/
10977087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1098a7cc72afSBarry Smith {
1099d763cef2SBarry Smith   PetscFunctionBegin;
11000700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1101bdad233fSMatthew Knepley   ts->problem_type = type;
1102d763cef2SBarry Smith   PetscFunctionReturn(0);
1103d763cef2SBarry Smith }
1104d763cef2SBarry Smith 
1105bdad233fSMatthew Knepley #undef __FUNCT__
1106bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1107bdad233fSMatthew Knepley /*@C
1108bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1109bdad233fSMatthew Knepley 
1110bdad233fSMatthew Knepley   Not collective
1111bdad233fSMatthew Knepley 
1112bdad233fSMatthew Knepley   Input Parameter:
1113bdad233fSMatthew Knepley . ts   - The TS
1114bdad233fSMatthew Knepley 
1115bdad233fSMatthew Knepley   Output Parameter:
1116bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1117bdad233fSMatthew Knepley .vb
1118bdad233fSMatthew Knepley          U_t = A U
1119bdad233fSMatthew Knepley          U_t = A(t) U
1120bdad233fSMatthew Knepley          U_t = F(t,U)
1121bdad233fSMatthew Knepley .ve
1122bdad233fSMatthew Knepley 
1123bdad233fSMatthew Knepley    Level: beginner
1124bdad233fSMatthew Knepley 
1125bdad233fSMatthew Knepley .keywords: TS, problem type
1126bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1127bdad233fSMatthew Knepley @*/
11287087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1129a7cc72afSBarry Smith {
1130bdad233fSMatthew Knepley   PetscFunctionBegin;
11310700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
11324482741eSBarry Smith   PetscValidIntPointer(type,2);
1133bdad233fSMatthew Knepley   *type = ts->problem_type;
1134bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1135bdad233fSMatthew Knepley }
1136d763cef2SBarry Smith 
11374a2ae208SSatish Balay #undef __FUNCT__
11384a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1139d763cef2SBarry Smith /*@
1140d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1141d763cef2SBarry Smith    of a timestepper.
1142d763cef2SBarry Smith 
1143d763cef2SBarry Smith    Collective on TS
1144d763cef2SBarry Smith 
1145d763cef2SBarry Smith    Input Parameter:
1146d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1147d763cef2SBarry Smith 
1148d763cef2SBarry Smith    Notes:
1149d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1150d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1151d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1152d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1153d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1154d763cef2SBarry Smith 
1155d763cef2SBarry Smith    Level: advanced
1156d763cef2SBarry Smith 
1157d763cef2SBarry Smith .keywords: TS, timestep, setup
1158d763cef2SBarry Smith 
1159d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1160d763cef2SBarry Smith @*/
11617087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1162d763cef2SBarry Smith {
1163dfbe8321SBarry Smith   PetscErrorCode ierr;
1164d763cef2SBarry Smith 
1165d763cef2SBarry Smith   PetscFunctionBegin;
11660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1167e32f2f54SBarry Smith   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
11687adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
11699596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1170d763cef2SBarry Smith   }
1171000e7ae3SMatthew Knepley   ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1172d763cef2SBarry Smith   ts->setupcalled = 1;
1173d763cef2SBarry Smith   PetscFunctionReturn(0);
1174d763cef2SBarry Smith }
1175d763cef2SBarry Smith 
11764a2ae208SSatish Balay #undef __FUNCT__
11774a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1178d8e5e3e6SSatish Balay /*@
1179d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1180d763cef2SBarry Smith    with TSCreate().
1181d763cef2SBarry Smith 
1182d763cef2SBarry Smith    Collective on TS
1183d763cef2SBarry Smith 
1184d763cef2SBarry Smith    Input Parameter:
1185d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1186d763cef2SBarry Smith 
1187d763cef2SBarry Smith    Level: beginner
1188d763cef2SBarry Smith 
1189d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1190d763cef2SBarry Smith 
1191d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1192d763cef2SBarry Smith @*/
11937087cfbeSBarry Smith PetscErrorCode  TSDestroy(TS ts)
1194d763cef2SBarry Smith {
11956849ba73SBarry Smith   PetscErrorCode ierr;
1196d763cef2SBarry Smith 
1197d763cef2SBarry Smith   PetscFunctionBegin;
11980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11997adad957SLisandro Dalcin   if (--((PetscObject)ts)->refct > 0) PetscFunctionReturn(0);
1200d763cef2SBarry Smith 
1201be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
12020f5bd95cSBarry Smith   ierr = PetscObjectDepublish(ts);CHKERRQ(ierr);
12036c699258SBarry Smith 
12046c699258SBarry Smith   if (ts->dm) {ierr = DMDestroy(ts->dm);CHKERRQ(ierr);}
1205cb9801acSJed Brown   if (ts->A) {ierr = MatDestroy(ts->A);CHKERRQ(ierr);}
120694b7f48cSBarry Smith   if (ts->ksp) {ierr = KSPDestroy(ts->ksp);CHKERRQ(ierr);}
1207d763cef2SBarry Smith   if (ts->snes) {ierr = SNESDestroy(ts->snes);CHKERRQ(ierr);}
12081e3347e8SBarry Smith   if (ts->ops->destroy) {ierr = (*(ts)->ops->destroy)(ts);CHKERRQ(ierr);}
1209a6570f20SBarry Smith   ierr = TSMonitorCancel(ts);CHKERRQ(ierr);
1210a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1211d763cef2SBarry Smith   PetscFunctionReturn(0);
1212d763cef2SBarry Smith }
1213d763cef2SBarry Smith 
12144a2ae208SSatish Balay #undef __FUNCT__
12154a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1216d8e5e3e6SSatish Balay /*@
1217d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1218d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1219d763cef2SBarry Smith 
1220d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1221d763cef2SBarry Smith 
1222d763cef2SBarry Smith    Input Parameter:
1223d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1224d763cef2SBarry Smith 
1225d763cef2SBarry Smith    Output Parameter:
1226d763cef2SBarry Smith .  snes - the nonlinear solver context
1227d763cef2SBarry Smith 
1228d763cef2SBarry Smith    Notes:
1229d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1230d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
123194b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1232d763cef2SBarry Smith 
1233d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1234d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1235d763cef2SBarry Smith 
1236d763cef2SBarry Smith    Level: beginner
1237d763cef2SBarry Smith 
1238d763cef2SBarry Smith .keywords: timestep, get, SNES
1239d763cef2SBarry Smith @*/
12407087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1241d763cef2SBarry Smith {
1242d763cef2SBarry Smith   PetscFunctionBegin;
12430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12444482741eSBarry Smith   PetscValidPointer(snes,2);
124517186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"SNES is not created yet. Call TSSetType() first");
1246e32f2f54SBarry Smith   if (ts->problem_type == TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Nonlinear only; use TSGetKSP()");
1247d763cef2SBarry Smith   *snes = ts->snes;
1248d763cef2SBarry Smith   PetscFunctionReturn(0);
1249d763cef2SBarry Smith }
1250d763cef2SBarry Smith 
12514a2ae208SSatish Balay #undef __FUNCT__
125294b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1253d8e5e3e6SSatish Balay /*@
125494b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1255d763cef2SBarry Smith    a TS (timestepper) context.
1256d763cef2SBarry Smith 
125794b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1258d763cef2SBarry Smith 
1259d763cef2SBarry Smith    Input Parameter:
1260d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1261d763cef2SBarry Smith 
1262d763cef2SBarry Smith    Output Parameter:
126394b7f48cSBarry Smith .  ksp - the nonlinear solver context
1264d763cef2SBarry Smith 
1265d763cef2SBarry Smith    Notes:
126694b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1267d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1268d763cef2SBarry Smith    KSP and PC contexts as well.
1269d763cef2SBarry Smith 
127094b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
127194b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1272d763cef2SBarry Smith 
1273d763cef2SBarry Smith    Level: beginner
1274d763cef2SBarry Smith 
127594b7f48cSBarry Smith .keywords: timestep, get, KSP
1276d763cef2SBarry Smith @*/
12777087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1278d763cef2SBarry Smith {
1279d763cef2SBarry Smith   PetscFunctionBegin;
12800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12814482741eSBarry Smith   PetscValidPointer(ksp,2);
128217186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1283e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
128494b7f48cSBarry Smith   *ksp = ts->ksp;
1285d763cef2SBarry Smith   PetscFunctionReturn(0);
1286d763cef2SBarry Smith }
1287d763cef2SBarry Smith 
1288d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1289d763cef2SBarry Smith 
12904a2ae208SSatish Balay #undef __FUNCT__
1291adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1292adb62b0dSMatthew Knepley /*@
1293adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1294adb62b0dSMatthew Knepley    maximum time for iteration.
1295adb62b0dSMatthew Knepley 
12963f9fe445SBarry Smith    Not Collective
1297adb62b0dSMatthew Knepley 
1298adb62b0dSMatthew Knepley    Input Parameters:
1299adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1300adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1301adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1302adb62b0dSMatthew Knepley 
1303adb62b0dSMatthew Knepley    Level: intermediate
1304adb62b0dSMatthew Knepley 
1305adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1306adb62b0dSMatthew Knepley @*/
13077087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1308adb62b0dSMatthew Knepley {
1309adb62b0dSMatthew Knepley   PetscFunctionBegin;
13100700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1311abc0a331SBarry Smith   if (maxsteps) {
13124482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1313adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1314adb62b0dSMatthew Knepley   }
1315abc0a331SBarry Smith   if (maxtime ) {
13164482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1317adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1318adb62b0dSMatthew Knepley   }
1319adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1320adb62b0dSMatthew Knepley }
1321adb62b0dSMatthew Knepley 
1322adb62b0dSMatthew Knepley #undef __FUNCT__
13234a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1324d763cef2SBarry Smith /*@
1325d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1326d763cef2SBarry Smith    maximum time for iteration.
1327d763cef2SBarry Smith 
13283f9fe445SBarry Smith    Logically Collective on TS
1329d763cef2SBarry Smith 
1330d763cef2SBarry Smith    Input Parameters:
1331d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1332d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1333d763cef2SBarry Smith -  maxtime - final time to iterate to
1334d763cef2SBarry Smith 
1335d763cef2SBarry Smith    Options Database Keys:
1336d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
1337d763cef2SBarry Smith .  -ts_max_time <maxtime> - Sets maxtime
1338d763cef2SBarry Smith 
1339d763cef2SBarry Smith    Notes:
1340d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1341d763cef2SBarry Smith 
1342d763cef2SBarry Smith    Level: intermediate
1343d763cef2SBarry Smith 
1344d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1345d763cef2SBarry Smith @*/
13467087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1347d763cef2SBarry Smith {
1348d763cef2SBarry Smith   PetscFunctionBegin;
13490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1350c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1351c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
1352d763cef2SBarry Smith   ts->max_steps = maxsteps;
1353d763cef2SBarry Smith   ts->max_time  = maxtime;
1354d763cef2SBarry Smith   PetscFunctionReturn(0);
1355d763cef2SBarry Smith }
1356d763cef2SBarry Smith 
13574a2ae208SSatish Balay #undef __FUNCT__
13584a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1359d763cef2SBarry Smith /*@
1360d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1361d763cef2SBarry Smith    for use by the TS routines.
1362d763cef2SBarry Smith 
13633f9fe445SBarry Smith    Logically Collective on TS and Vec
1364d763cef2SBarry Smith 
1365d763cef2SBarry Smith    Input Parameters:
1366d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1367d763cef2SBarry Smith -  x - the solution vector
1368d763cef2SBarry Smith 
1369d763cef2SBarry Smith    Level: beginner
1370d763cef2SBarry Smith 
1371d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1372d763cef2SBarry Smith @*/
13737087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1374d763cef2SBarry Smith {
1375d763cef2SBarry Smith   PetscFunctionBegin;
13760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13770700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
1378d763cef2SBarry Smith   ts->vec_sol        = ts->vec_sol_always = x;
1379d763cef2SBarry Smith   PetscFunctionReturn(0);
1380d763cef2SBarry Smith }
1381d763cef2SBarry Smith 
1382e74ef692SMatthew Knepley #undef __FUNCT__
1383e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1384ac226902SBarry Smith /*@C
1385000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
13863f2090d5SJed Brown   called once at the beginning of each time step.
1387000e7ae3SMatthew Knepley 
13883f9fe445SBarry Smith   Logically Collective on TS
1389000e7ae3SMatthew Knepley 
1390000e7ae3SMatthew Knepley   Input Parameters:
1391000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1392000e7ae3SMatthew Knepley - func - The function
1393000e7ae3SMatthew Knepley 
1394000e7ae3SMatthew Knepley   Calling sequence of func:
1395000e7ae3SMatthew Knepley . func (TS ts);
1396000e7ae3SMatthew Knepley 
1397000e7ae3SMatthew Knepley   Level: intermediate
1398000e7ae3SMatthew Knepley 
1399000e7ae3SMatthew Knepley .keywords: TS, timestep
1400000e7ae3SMatthew Knepley @*/
14017087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1402000e7ae3SMatthew Knepley {
1403000e7ae3SMatthew Knepley   PetscFunctionBegin;
14040700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1405000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1406000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1407000e7ae3SMatthew Knepley }
1408000e7ae3SMatthew Knepley 
1409e74ef692SMatthew Knepley #undef __FUNCT__
14103f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
14113f2090d5SJed Brown /*@C
14123f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
14133f2090d5SJed Brown 
14143f2090d5SJed Brown   Collective on TS
14153f2090d5SJed Brown 
14163f2090d5SJed Brown   Input Parameters:
14173f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
14183f2090d5SJed Brown 
14193f2090d5SJed Brown   Notes:
14203f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
14213f2090d5SJed Brown   so most users would not generally call this routine themselves.
14223f2090d5SJed Brown 
14233f2090d5SJed Brown   Level: developer
14243f2090d5SJed Brown 
14253f2090d5SJed Brown .keywords: TS, timestep
14263f2090d5SJed Brown @*/
14277087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
14283f2090d5SJed Brown {
14293f2090d5SJed Brown   PetscErrorCode ierr;
14303f2090d5SJed Brown 
14313f2090d5SJed Brown   PetscFunctionBegin;
14320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
143372ac3e02SJed Brown   if (ts->ops->prestep) {
14343f2090d5SJed Brown     PetscStackPush("TS PreStep function");
14353f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
14363f2090d5SJed Brown     PetscStackPop;
1437312ce896SJed Brown   }
14383f2090d5SJed Brown   PetscFunctionReturn(0);
14393f2090d5SJed Brown }
14403f2090d5SJed Brown 
14413f2090d5SJed Brown #undef __FUNCT__
1442e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultPreStep"
1443000e7ae3SMatthew Knepley /*@
1444000e7ae3SMatthew Knepley   TSDefaultPreStep - The default pre-stepping function which does nothing.
1445000e7ae3SMatthew Knepley 
1446000e7ae3SMatthew Knepley   Collective on TS
1447000e7ae3SMatthew Knepley 
1448000e7ae3SMatthew Knepley   Input Parameters:
1449000e7ae3SMatthew Knepley . ts  - The TS context obtained from TSCreate()
1450000e7ae3SMatthew Knepley 
1451000e7ae3SMatthew Knepley   Level: developer
1452000e7ae3SMatthew Knepley 
1453000e7ae3SMatthew Knepley .keywords: TS, timestep
1454000e7ae3SMatthew Knepley @*/
14557087cfbeSBarry Smith PetscErrorCode  TSDefaultPreStep(TS ts)
1456000e7ae3SMatthew Knepley {
1457000e7ae3SMatthew Knepley   PetscFunctionBegin;
1458000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1459000e7ae3SMatthew Knepley }
1460000e7ae3SMatthew Knepley 
1461e74ef692SMatthew Knepley #undef __FUNCT__
1462e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1463ac226902SBarry Smith /*@C
1464000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
14653f2090d5SJed Brown   called once at the end of each time step.
1466000e7ae3SMatthew Knepley 
14673f9fe445SBarry Smith   Logically Collective on TS
1468000e7ae3SMatthew Knepley 
1469000e7ae3SMatthew Knepley   Input Parameters:
1470000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1471000e7ae3SMatthew Knepley - func - The function
1472000e7ae3SMatthew Knepley 
1473000e7ae3SMatthew Knepley   Calling sequence of func:
1474000e7ae3SMatthew Knepley . func (TS ts);
1475000e7ae3SMatthew Knepley 
1476000e7ae3SMatthew Knepley   Level: intermediate
1477000e7ae3SMatthew Knepley 
1478000e7ae3SMatthew Knepley .keywords: TS, timestep
1479000e7ae3SMatthew Knepley @*/
14807087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1481000e7ae3SMatthew Knepley {
1482000e7ae3SMatthew Knepley   PetscFunctionBegin;
14830700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1484000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1485000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1486000e7ae3SMatthew Knepley }
1487000e7ae3SMatthew Knepley 
1488e74ef692SMatthew Knepley #undef __FUNCT__
14893f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
14903f2090d5SJed Brown /*@C
14913f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
14923f2090d5SJed Brown 
14933f2090d5SJed Brown   Collective on TS
14943f2090d5SJed Brown 
14953f2090d5SJed Brown   Input Parameters:
14963f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
14973f2090d5SJed Brown 
14983f2090d5SJed Brown   Notes:
14993f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
15003f2090d5SJed Brown   so most users would not generally call this routine themselves.
15013f2090d5SJed Brown 
15023f2090d5SJed Brown   Level: developer
15033f2090d5SJed Brown 
15043f2090d5SJed Brown .keywords: TS, timestep
15053f2090d5SJed Brown @*/
15067087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
15073f2090d5SJed Brown {
15083f2090d5SJed Brown   PetscErrorCode ierr;
15093f2090d5SJed Brown 
15103f2090d5SJed Brown   PetscFunctionBegin;
15110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
151272ac3e02SJed Brown   if (ts->ops->poststep) {
15133f2090d5SJed Brown     PetscStackPush("TS PostStep function");
15143f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
15153f2090d5SJed Brown     PetscStackPop;
151672ac3e02SJed Brown   }
15173f2090d5SJed Brown   PetscFunctionReturn(0);
15183f2090d5SJed Brown }
15193f2090d5SJed Brown 
15203f2090d5SJed Brown #undef __FUNCT__
1521e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultPostStep"
1522000e7ae3SMatthew Knepley /*@
1523000e7ae3SMatthew Knepley   TSDefaultPostStep - The default post-stepping function which does nothing.
1524000e7ae3SMatthew Knepley 
1525000e7ae3SMatthew Knepley   Collective on TS
1526000e7ae3SMatthew Knepley 
1527000e7ae3SMatthew Knepley   Input Parameters:
1528000e7ae3SMatthew Knepley . ts  - The TS context obtained from TSCreate()
1529000e7ae3SMatthew Knepley 
1530000e7ae3SMatthew Knepley   Level: developer
1531000e7ae3SMatthew Knepley 
1532000e7ae3SMatthew Knepley .keywords: TS, timestep
1533000e7ae3SMatthew Knepley @*/
15347087cfbeSBarry Smith PetscErrorCode  TSDefaultPostStep(TS ts)
1535000e7ae3SMatthew Knepley {
1536000e7ae3SMatthew Knepley   PetscFunctionBegin;
1537000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1538000e7ae3SMatthew Knepley }
1539000e7ae3SMatthew Knepley 
1540d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1541d763cef2SBarry Smith 
15424a2ae208SSatish Balay #undef __FUNCT__
1543a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1544d763cef2SBarry Smith /*@C
1545a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1546d763cef2SBarry Smith    timestep to display the iteration's  progress.
1547d763cef2SBarry Smith 
15483f9fe445SBarry Smith    Logically Collective on TS
1549d763cef2SBarry Smith 
1550d763cef2SBarry Smith    Input Parameters:
1551d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1552d763cef2SBarry Smith .  func - monitoring routine
1553329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1554b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1555b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1556b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1557d763cef2SBarry Smith 
1558d763cef2SBarry Smith    Calling sequence of func:
1559a7cc72afSBarry Smith $    int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1560d763cef2SBarry Smith 
1561d763cef2SBarry Smith +    ts - the TS context
1562d763cef2SBarry Smith .    steps - iteration number
15631f06c33eSBarry Smith .    time - current time
1564d763cef2SBarry Smith .    x - current iterate
1565d763cef2SBarry Smith -    mctx - [optional] monitoring context
1566d763cef2SBarry Smith 
1567d763cef2SBarry Smith    Notes:
1568d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1569d763cef2SBarry Smith    already has been loaded.
1570d763cef2SBarry Smith 
1571025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1572025f1a04SBarry Smith 
1573d763cef2SBarry Smith    Level: intermediate
1574d763cef2SBarry Smith 
1575d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1576d763cef2SBarry Smith 
1577a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1578d763cef2SBarry Smith @*/
15797087cfbeSBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void*))
1580d763cef2SBarry Smith {
1581d763cef2SBarry Smith   PetscFunctionBegin;
15820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
158317186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1584d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1585329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1586d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1587d763cef2SBarry Smith   PetscFunctionReturn(0);
1588d763cef2SBarry Smith }
1589d763cef2SBarry Smith 
15904a2ae208SSatish Balay #undef __FUNCT__
1591a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1592d763cef2SBarry Smith /*@C
1593a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1594d763cef2SBarry Smith 
15953f9fe445SBarry Smith    Logically Collective on TS
1596d763cef2SBarry Smith 
1597d763cef2SBarry Smith    Input Parameters:
1598d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1599d763cef2SBarry Smith 
1600d763cef2SBarry Smith    Notes:
1601d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1602d763cef2SBarry Smith 
1603d763cef2SBarry Smith    Level: intermediate
1604d763cef2SBarry Smith 
1605d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1606d763cef2SBarry Smith 
1607a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1608d763cef2SBarry Smith @*/
16097087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1610d763cef2SBarry Smith {
1611d952e501SBarry Smith   PetscErrorCode ierr;
1612d952e501SBarry Smith   PetscInt       i;
1613d952e501SBarry Smith 
1614d763cef2SBarry Smith   PetscFunctionBegin;
16150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1616d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
1617d952e501SBarry Smith     if (ts->mdestroy[i]) {
1618d952e501SBarry Smith       ierr = (*ts->mdestroy[i])(ts->monitorcontext[i]);CHKERRQ(ierr);
1619d952e501SBarry Smith     }
1620d952e501SBarry Smith   }
1621d763cef2SBarry Smith   ts->numbermonitors = 0;
1622d763cef2SBarry Smith   PetscFunctionReturn(0);
1623d763cef2SBarry Smith }
1624d763cef2SBarry Smith 
16254a2ae208SSatish Balay #undef __FUNCT__
1626a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
1627d8e5e3e6SSatish Balay /*@
1628a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
16295516499fSSatish Balay 
16305516499fSSatish Balay    Level: intermediate
163141251cbbSSatish Balay 
16325516499fSSatish Balay .keywords: TS, set, monitor
16335516499fSSatish Balay 
163441251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
163541251cbbSSatish Balay @*/
1636a6570f20SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *ctx)
1637d763cef2SBarry Smith {
1638dfbe8321SBarry Smith   PetscErrorCode          ierr;
1639a34d58ebSBarry Smith   PetscViewerASCIIMonitor viewer = (PetscViewerASCIIMonitor)ctx;
1640d132466eSBarry Smith 
1641d763cef2SBarry Smith   PetscFunctionBegin;
1642a34d58ebSBarry Smith   if (!ctx) {
16437adad957SLisandro Dalcin     ierr = PetscViewerASCIIMonitorCreate(((PetscObject)ts)->comm,"stdout",0,&viewer);CHKERRQ(ierr);
1644a34d58ebSBarry Smith   }
1645f22f69f0SBarry Smith   ierr = PetscViewerASCIIMonitorPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr);
1646a34d58ebSBarry Smith   if (!ctx) {
1647a34d58ebSBarry Smith     ierr = PetscViewerASCIIMonitorDestroy(viewer);CHKERRQ(ierr);
1648a34d58ebSBarry Smith   }
1649d763cef2SBarry Smith   PetscFunctionReturn(0);
1650d763cef2SBarry Smith }
1651d763cef2SBarry Smith 
16524a2ae208SSatish Balay #undef __FUNCT__
16534a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1654d763cef2SBarry Smith /*@
1655d763cef2SBarry Smith    TSStep - Steps the requested number of timesteps.
1656d763cef2SBarry Smith 
1657d763cef2SBarry Smith    Collective on TS
1658d763cef2SBarry Smith 
1659d763cef2SBarry Smith    Input Parameter:
1660d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1661d763cef2SBarry Smith 
1662d763cef2SBarry Smith    Output Parameters:
1663d763cef2SBarry Smith +  steps - number of iterations until termination
1664142b95e3SSatish Balay -  ptime - time until termination
1665d763cef2SBarry Smith 
1666d763cef2SBarry Smith    Level: beginner
1667d763cef2SBarry Smith 
1668d763cef2SBarry Smith .keywords: TS, timestep, solve
1669d763cef2SBarry Smith 
1670d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy()
1671d763cef2SBarry Smith @*/
16727087cfbeSBarry Smith PetscErrorCode  TSStep(TS ts,PetscInt *steps,PetscReal *ptime)
1673d763cef2SBarry Smith {
1674dfbe8321SBarry Smith   PetscErrorCode ierr;
1675d763cef2SBarry Smith 
1676d763cef2SBarry Smith   PetscFunctionBegin;
16770700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1678d405a339SMatthew Knepley   if (!ts->setupcalled) {
1679d405a339SMatthew Knepley     ierr = TSSetUp(ts);CHKERRQ(ierr);
1680d405a339SMatthew Knepley   }
1681d405a339SMatthew Knepley 
1682d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1683000e7ae3SMatthew Knepley   ierr = (*ts->ops->step)(ts, steps, ptime);CHKERRQ(ierr);
1684d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr);
1685d405a339SMatthew Knepley 
16864bb05414SBarry Smith   if (!PetscPreLoadingOn) {
16877adad957SLisandro Dalcin     ierr = TSViewFromOptions(ts,((PetscObject)ts)->name);CHKERRQ(ierr);
1688d405a339SMatthew Knepley   }
1689d763cef2SBarry Smith   PetscFunctionReturn(0);
1690d763cef2SBarry Smith }
1691d763cef2SBarry Smith 
16924a2ae208SSatish Balay #undef __FUNCT__
16936a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
16946a4d4014SLisandro Dalcin /*@
16956a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
16966a4d4014SLisandro Dalcin 
16976a4d4014SLisandro Dalcin    Collective on TS
16986a4d4014SLisandro Dalcin 
16996a4d4014SLisandro Dalcin    Input Parameter:
17006a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
17016a4d4014SLisandro Dalcin -  x - the solution vector, or PETSC_NULL if it was set with TSSetSolution()
17026a4d4014SLisandro Dalcin 
17036a4d4014SLisandro Dalcin    Level: beginner
17046a4d4014SLisandro Dalcin 
17056a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
17066a4d4014SLisandro Dalcin 
17076a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
17086a4d4014SLisandro Dalcin @*/
17097087cfbeSBarry Smith PetscErrorCode  TSSolve(TS ts, Vec x)
17106a4d4014SLisandro Dalcin {
17116a4d4014SLisandro Dalcin   PetscInt       steps;
17126a4d4014SLisandro Dalcin   PetscReal      ptime;
17136a4d4014SLisandro Dalcin   PetscErrorCode ierr;
1714f22f69f0SBarry Smith 
17156a4d4014SLisandro Dalcin   PetscFunctionBegin;
17160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17176a4d4014SLisandro Dalcin   /* set solution vector if provided */
17186a4d4014SLisandro Dalcin   if (x) { ierr = TSSetSolution(ts, x); CHKERRQ(ierr); }
17196a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
17206a4d4014SLisandro Dalcin   ts->steps = 0; ts->linear_its = 0; ts->nonlinear_its = 0;
17216a4d4014SLisandro Dalcin   /* steps the requested number of timesteps. */
17226a4d4014SLisandro Dalcin   ierr = TSStep(ts, &steps, &ptime);CHKERRQ(ierr);
17236a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
17246a4d4014SLisandro Dalcin }
17256a4d4014SLisandro Dalcin 
17266a4d4014SLisandro Dalcin #undef __FUNCT__
17274a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
1728d763cef2SBarry Smith /*
1729d763cef2SBarry Smith      Runs the user provided monitor routines, if they exists.
1730d763cef2SBarry Smith */
1731a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
1732d763cef2SBarry Smith {
17336849ba73SBarry Smith   PetscErrorCode ierr;
1734a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
1735d763cef2SBarry Smith 
1736d763cef2SBarry Smith   PetscFunctionBegin;
1737d763cef2SBarry Smith   for (i=0; i<n; i++) {
1738142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
1739d763cef2SBarry Smith   }
1740d763cef2SBarry Smith   PetscFunctionReturn(0);
1741d763cef2SBarry Smith }
1742d763cef2SBarry Smith 
1743d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
1744d763cef2SBarry Smith 
17454a2ae208SSatish Balay #undef __FUNCT__
1746a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate"
1747d763cef2SBarry Smith /*@C
1748a6570f20SBarry Smith    TSMonitorLGCreate - Creates a line graph context for use with
1749d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
1750d763cef2SBarry Smith 
1751d763cef2SBarry Smith    Collective on TS
1752d763cef2SBarry Smith 
1753d763cef2SBarry Smith    Input Parameters:
1754d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
1755d763cef2SBarry Smith .  label - the title to put in the title bar
17567c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
1757d763cef2SBarry Smith -  m, n - the screen width and height in pixels
1758d763cef2SBarry Smith 
1759d763cef2SBarry Smith    Output Parameter:
1760d763cef2SBarry Smith .  draw - the drawing context
1761d763cef2SBarry Smith 
1762d763cef2SBarry Smith    Options Database Key:
1763a6570f20SBarry Smith .  -ts_monitor_draw - automatically sets line graph monitor
1764d763cef2SBarry Smith 
1765d763cef2SBarry Smith    Notes:
1766a6570f20SBarry Smith    Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy().
1767d763cef2SBarry Smith 
1768d763cef2SBarry Smith    Level: intermediate
1769d763cef2SBarry Smith 
17707c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
1771d763cef2SBarry Smith 
1772a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet()
17737c922b88SBarry Smith 
1774d763cef2SBarry Smith @*/
17757087cfbeSBarry Smith PetscErrorCode  TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1776d763cef2SBarry Smith {
1777b0a32e0cSBarry Smith   PetscDraw      win;
1778dfbe8321SBarry Smith   PetscErrorCode ierr;
1779d763cef2SBarry Smith 
1780d763cef2SBarry Smith   PetscFunctionBegin;
1781b0a32e0cSBarry Smith   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
1782b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
1783b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
1784b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
1785d763cef2SBarry Smith 
178652e6d16bSBarry Smith   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
1787d763cef2SBarry Smith   PetscFunctionReturn(0);
1788d763cef2SBarry Smith }
1789d763cef2SBarry Smith 
17904a2ae208SSatish Balay #undef __FUNCT__
1791a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG"
1792a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
1793d763cef2SBarry Smith {
1794b0a32e0cSBarry Smith   PetscDrawLG    lg = (PetscDrawLG) monctx;
179587828ca2SBarry Smith   PetscReal      x,y = ptime;
1796dfbe8321SBarry Smith   PetscErrorCode ierr;
1797d763cef2SBarry Smith 
1798d763cef2SBarry Smith   PetscFunctionBegin;
17997c922b88SBarry Smith   if (!monctx) {
18007c922b88SBarry Smith     MPI_Comm    comm;
1801b0a32e0cSBarry Smith     PetscViewer viewer;
18027c922b88SBarry Smith 
18037c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
1804b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
1805b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
18067c922b88SBarry Smith   }
18077c922b88SBarry Smith 
1808b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
180987828ca2SBarry Smith   x = (PetscReal)n;
1810b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1811d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
1812b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1813d763cef2SBarry Smith   }
1814d763cef2SBarry Smith   PetscFunctionReturn(0);
1815d763cef2SBarry Smith }
1816d763cef2SBarry Smith 
18174a2ae208SSatish Balay #undef __FUNCT__
1818a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy"
1819d763cef2SBarry Smith /*@C
1820a6570f20SBarry Smith    TSMonitorLGDestroy - Destroys a line graph context that was created
1821a6570f20SBarry Smith    with TSMonitorLGCreate().
1822d763cef2SBarry Smith 
1823b0a32e0cSBarry Smith    Collective on PetscDrawLG
1824d763cef2SBarry Smith 
1825d763cef2SBarry Smith    Input Parameter:
1826d763cef2SBarry Smith .  draw - the drawing context
1827d763cef2SBarry Smith 
1828d763cef2SBarry Smith    Level: intermediate
1829d763cef2SBarry Smith 
1830d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
1831d763cef2SBarry Smith 
1832a6570f20SBarry Smith .seealso: TSMonitorLGCreate(),  TSMonitorSet(), TSMonitorLG();
1833d763cef2SBarry Smith @*/
18347087cfbeSBarry Smith PetscErrorCode  TSMonitorLGDestroy(PetscDrawLG drawlg)
1835d763cef2SBarry Smith {
1836b0a32e0cSBarry Smith   PetscDraw      draw;
1837dfbe8321SBarry Smith   PetscErrorCode ierr;
1838d763cef2SBarry Smith 
1839d763cef2SBarry Smith   PetscFunctionBegin;
1840b0a32e0cSBarry Smith   ierr = PetscDrawLGGetDraw(drawlg,&draw);CHKERRQ(ierr);
1841b0a32e0cSBarry Smith   ierr = PetscDrawDestroy(draw);CHKERRQ(ierr);
1842b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
1843d763cef2SBarry Smith   PetscFunctionReturn(0);
1844d763cef2SBarry Smith }
1845d763cef2SBarry Smith 
18464a2ae208SSatish Balay #undef __FUNCT__
18474a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
1848d763cef2SBarry Smith /*@
1849d763cef2SBarry Smith    TSGetTime - Gets the current time.
1850d763cef2SBarry Smith 
1851d763cef2SBarry Smith    Not Collective
1852d763cef2SBarry Smith 
1853d763cef2SBarry Smith    Input Parameter:
1854d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1855d763cef2SBarry Smith 
1856d763cef2SBarry Smith    Output Parameter:
1857d763cef2SBarry Smith .  t  - the current time
1858d763cef2SBarry Smith 
1859d763cef2SBarry Smith    Level: beginner
1860d763cef2SBarry Smith 
1861d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1862d763cef2SBarry Smith 
1863d763cef2SBarry Smith .keywords: TS, get, time
1864d763cef2SBarry Smith @*/
18657087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
1866d763cef2SBarry Smith {
1867d763cef2SBarry Smith   PetscFunctionBegin;
18680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18694482741eSBarry Smith   PetscValidDoublePointer(t,2);
1870d763cef2SBarry Smith   *t = ts->ptime;
1871d763cef2SBarry Smith   PetscFunctionReturn(0);
1872d763cef2SBarry Smith }
1873d763cef2SBarry Smith 
18744a2ae208SSatish Balay #undef __FUNCT__
18756a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
18766a4d4014SLisandro Dalcin /*@
18776a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
18786a4d4014SLisandro Dalcin 
18793f9fe445SBarry Smith    Logically Collective on TS
18806a4d4014SLisandro Dalcin 
18816a4d4014SLisandro Dalcin    Input Parameters:
18826a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
18836a4d4014SLisandro Dalcin -  time - the time
18846a4d4014SLisandro Dalcin 
18856a4d4014SLisandro Dalcin    Level: intermediate
18866a4d4014SLisandro Dalcin 
18876a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
18886a4d4014SLisandro Dalcin 
18896a4d4014SLisandro Dalcin .keywords: TS, set, time
18906a4d4014SLisandro Dalcin @*/
18917087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
18926a4d4014SLisandro Dalcin {
18936a4d4014SLisandro Dalcin   PetscFunctionBegin;
18940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1895c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
18966a4d4014SLisandro Dalcin   ts->ptime = t;
18976a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
18986a4d4014SLisandro Dalcin }
18996a4d4014SLisandro Dalcin 
19006a4d4014SLisandro Dalcin #undef __FUNCT__
19014a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
1902d763cef2SBarry Smith /*@C
1903d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
1904d763cef2SBarry Smith    TS options in the database.
1905d763cef2SBarry Smith 
19063f9fe445SBarry Smith    Logically Collective on TS
1907d763cef2SBarry Smith 
1908d763cef2SBarry Smith    Input Parameter:
1909d763cef2SBarry Smith +  ts     - The TS context
1910d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
1911d763cef2SBarry Smith 
1912d763cef2SBarry Smith    Notes:
1913d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1914d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
1915d763cef2SBarry Smith    hyphen.
1916d763cef2SBarry Smith 
1917d763cef2SBarry Smith    Level: advanced
1918d763cef2SBarry Smith 
1919d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
1920d763cef2SBarry Smith 
1921d763cef2SBarry Smith .seealso: TSSetFromOptions()
1922d763cef2SBarry Smith 
1923d763cef2SBarry Smith @*/
19247087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
1925d763cef2SBarry Smith {
1926dfbe8321SBarry Smith   PetscErrorCode ierr;
1927d763cef2SBarry Smith 
1928d763cef2SBarry Smith   PetscFunctionBegin;
19290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1930d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
1931d763cef2SBarry Smith   switch(ts->problem_type) {
1932d763cef2SBarry Smith     case TS_NONLINEAR:
193359580b9cSBarry Smith       if (ts->snes) {
1934d763cef2SBarry Smith         ierr = SNESSetOptionsPrefix(ts->snes,prefix);CHKERRQ(ierr);
193559580b9cSBarry Smith       }
1936d763cef2SBarry Smith       break;
1937d763cef2SBarry Smith     case TS_LINEAR:
193859580b9cSBarry Smith       if (ts->ksp) {
193994b7f48cSBarry Smith         ierr = KSPSetOptionsPrefix(ts->ksp,prefix);CHKERRQ(ierr);
194059580b9cSBarry Smith       }
1941d763cef2SBarry Smith       break;
1942d763cef2SBarry Smith   }
1943d763cef2SBarry Smith   PetscFunctionReturn(0);
1944d763cef2SBarry Smith }
1945d763cef2SBarry Smith 
1946d763cef2SBarry Smith 
19474a2ae208SSatish Balay #undef __FUNCT__
19484a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
1949d763cef2SBarry Smith /*@C
1950d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
1951d763cef2SBarry Smith    TS options in the database.
1952d763cef2SBarry Smith 
19533f9fe445SBarry Smith    Logically Collective on TS
1954d763cef2SBarry Smith 
1955d763cef2SBarry Smith    Input Parameter:
1956d763cef2SBarry Smith +  ts     - The TS context
1957d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
1958d763cef2SBarry Smith 
1959d763cef2SBarry Smith    Notes:
1960d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1961d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
1962d763cef2SBarry Smith    hyphen.
1963d763cef2SBarry Smith 
1964d763cef2SBarry Smith    Level: advanced
1965d763cef2SBarry Smith 
1966d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
1967d763cef2SBarry Smith 
1968d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
1969d763cef2SBarry Smith 
1970d763cef2SBarry Smith @*/
19717087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
1972d763cef2SBarry Smith {
1973dfbe8321SBarry Smith   PetscErrorCode ierr;
1974d763cef2SBarry Smith 
1975d763cef2SBarry Smith   PetscFunctionBegin;
19760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1977d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
1978d763cef2SBarry Smith   switch(ts->problem_type) {
1979d763cef2SBarry Smith     case TS_NONLINEAR:
19801ac94b3bSBarry Smith       if (ts->snes) {
1981d763cef2SBarry Smith         ierr = SNESAppendOptionsPrefix(ts->snes,prefix);CHKERRQ(ierr);
19821ac94b3bSBarry Smith       }
1983d763cef2SBarry Smith       break;
1984d763cef2SBarry Smith     case TS_LINEAR:
19851ac94b3bSBarry Smith       if (ts->ksp) {
198694b7f48cSBarry Smith         ierr = KSPAppendOptionsPrefix(ts->ksp,prefix);CHKERRQ(ierr);
19871ac94b3bSBarry Smith       }
1988d763cef2SBarry Smith       break;
1989d763cef2SBarry Smith   }
1990d763cef2SBarry Smith   PetscFunctionReturn(0);
1991d763cef2SBarry Smith }
1992d763cef2SBarry Smith 
19934a2ae208SSatish Balay #undef __FUNCT__
19944a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
1995d763cef2SBarry Smith /*@C
1996d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
1997d763cef2SBarry Smith    TS options in the database.
1998d763cef2SBarry Smith 
1999d763cef2SBarry Smith    Not Collective
2000d763cef2SBarry Smith 
2001d763cef2SBarry Smith    Input Parameter:
2002d763cef2SBarry Smith .  ts - The TS context
2003d763cef2SBarry Smith 
2004d763cef2SBarry Smith    Output Parameter:
2005d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2006d763cef2SBarry Smith 
2007d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2008d763cef2SBarry Smith    sufficient length to hold the prefix.
2009d763cef2SBarry Smith 
2010d763cef2SBarry Smith    Level: intermediate
2011d763cef2SBarry Smith 
2012d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2013d763cef2SBarry Smith 
2014d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2015d763cef2SBarry Smith @*/
20167087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2017d763cef2SBarry Smith {
2018dfbe8321SBarry Smith   PetscErrorCode ierr;
2019d763cef2SBarry Smith 
2020d763cef2SBarry Smith   PetscFunctionBegin;
20210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20224482741eSBarry Smith   PetscValidPointer(prefix,2);
2023d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2024d763cef2SBarry Smith   PetscFunctionReturn(0);
2025d763cef2SBarry Smith }
2026d763cef2SBarry Smith 
20274a2ae208SSatish Balay #undef __FUNCT__
20284a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2029d763cef2SBarry Smith /*@C
2030d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2031d763cef2SBarry Smith 
2032d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2033d763cef2SBarry Smith 
2034d763cef2SBarry Smith    Input Parameter:
2035d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2036d763cef2SBarry Smith 
2037d763cef2SBarry Smith    Output Parameters:
2038d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2039d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2040d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine
2041d763cef2SBarry Smith 
2042d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2043d763cef2SBarry Smith 
2044d763cef2SBarry Smith    Level: intermediate
2045d763cef2SBarry Smith 
204626d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2047d763cef2SBarry Smith 
2048d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2049d763cef2SBarry Smith @*/
20507087cfbeSBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,void **ctx)
2051d763cef2SBarry Smith {
2052d763cef2SBarry Smith   PetscFunctionBegin;
205326d46c62SHong Zhang   if (J) *J = ts->Arhs;
205426d46c62SHong Zhang   if (M) *M = ts->B;
205526d46c62SHong Zhang   if (ctx) *ctx = ts->jacP;
2056d763cef2SBarry Smith   PetscFunctionReturn(0);
2057d763cef2SBarry Smith }
2058d763cef2SBarry Smith 
20591713a123SBarry Smith #undef __FUNCT__
20602eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
20612eca1d9cSJed Brown /*@C
20622eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
20632eca1d9cSJed Brown 
20642eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
20652eca1d9cSJed Brown 
20662eca1d9cSJed Brown    Input Parameter:
20672eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
20682eca1d9cSJed Brown 
20692eca1d9cSJed Brown    Output Parameters:
20702eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
20712eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
20722eca1d9cSJed Brown .  f   - The function to compute the matrices
20732eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
20742eca1d9cSJed Brown 
20752eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
20762eca1d9cSJed Brown 
20772eca1d9cSJed Brown    Level: advanced
20782eca1d9cSJed Brown 
20792eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
20802eca1d9cSJed Brown 
20812eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
20822eca1d9cSJed Brown @*/
20837087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
20842eca1d9cSJed Brown {
20852eca1d9cSJed Brown   PetscFunctionBegin;
20862eca1d9cSJed Brown   if (A) *A = ts->A;
20872eca1d9cSJed Brown   if (B) *B = ts->B;
20882eca1d9cSJed Brown   if (f) *f = ts->ops->ijacobian;
20892eca1d9cSJed Brown   if (ctx) *ctx = ts->jacP;
20902eca1d9cSJed Brown   PetscFunctionReturn(0);
20912eca1d9cSJed Brown }
20922eca1d9cSJed Brown 
20932eca1d9cSJed Brown #undef __FUNCT__
2094a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution"
20951713a123SBarry Smith /*@C
2096a6570f20SBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by calling
20971713a123SBarry Smith    VecView() for the solution at each timestep
20981713a123SBarry Smith 
20991713a123SBarry Smith    Collective on TS
21001713a123SBarry Smith 
21011713a123SBarry Smith    Input Parameters:
21021713a123SBarry Smith +  ts - the TS context
21031713a123SBarry Smith .  step - current time-step
2104142b95e3SSatish Balay .  ptime - current time
21051713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
21061713a123SBarry Smith 
21071713a123SBarry Smith    Level: intermediate
21081713a123SBarry Smith 
21091713a123SBarry Smith .keywords: TS,  vector, monitor, view
21101713a123SBarry Smith 
2111a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
21121713a123SBarry Smith @*/
21137087cfbeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
21141713a123SBarry Smith {
2115dfbe8321SBarry Smith   PetscErrorCode ierr;
21161713a123SBarry Smith   PetscViewer    viewer = (PetscViewer) dummy;
21171713a123SBarry Smith 
21181713a123SBarry Smith   PetscFunctionBegin;
2119a34d58ebSBarry Smith   if (!dummy) {
21207adad957SLisandro Dalcin     viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm);
21211713a123SBarry Smith   }
21221713a123SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
21231713a123SBarry Smith   PetscFunctionReturn(0);
21241713a123SBarry Smith }
21251713a123SBarry Smith 
21261713a123SBarry Smith 
21276c699258SBarry Smith #undef __FUNCT__
21286c699258SBarry Smith #define __FUNCT__ "TSSetDM"
21296c699258SBarry Smith /*@
21306c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
21316c699258SBarry Smith 
21323f9fe445SBarry Smith    Logically Collective on TS and DM
21336c699258SBarry Smith 
21346c699258SBarry Smith    Input Parameters:
21356c699258SBarry Smith +  ts - the preconditioner context
21366c699258SBarry Smith -  dm - the dm
21376c699258SBarry Smith 
21386c699258SBarry Smith    Level: intermediate
21396c699258SBarry Smith 
21406c699258SBarry Smith 
21416c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
21426c699258SBarry Smith @*/
21437087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
21446c699258SBarry Smith {
21456c699258SBarry Smith   PetscErrorCode ierr;
21466c699258SBarry Smith 
21476c699258SBarry Smith   PetscFunctionBegin;
21480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
214970663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
21506c699258SBarry Smith   if (ts->dm) {ierr = DMDestroy(ts->dm);CHKERRQ(ierr);}
21516c699258SBarry Smith   ts->dm = dm;
215270663e4aSLisandro Dalcin   if (ts->snes) {
215370663e4aSLisandro Dalcin     ierr = SNESSetDM(ts->snes,dm);CHKERRQ(ierr);
215470663e4aSLisandro Dalcin   }
215570663e4aSLisandro Dalcin   if (ts->ksp) {
215670663e4aSLisandro Dalcin     ierr = KSPSetDM(ts->ksp,dm);CHKERRQ(ierr);
215770663e4aSLisandro Dalcin     ierr = KSPSetDMActive(ts->ksp,PETSC_FALSE);CHKERRQ(ierr);
215870663e4aSLisandro Dalcin   }
21596c699258SBarry Smith   PetscFunctionReturn(0);
21606c699258SBarry Smith }
21616c699258SBarry Smith 
21626c699258SBarry Smith #undef __FUNCT__
21636c699258SBarry Smith #define __FUNCT__ "TSGetDM"
21646c699258SBarry Smith /*@
21656c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
21666c699258SBarry Smith 
21673f9fe445SBarry Smith    Not Collective
21686c699258SBarry Smith 
21696c699258SBarry Smith    Input Parameter:
21706c699258SBarry Smith . ts - the preconditioner context
21716c699258SBarry Smith 
21726c699258SBarry Smith    Output Parameter:
21736c699258SBarry Smith .  dm - the dm
21746c699258SBarry Smith 
21756c699258SBarry Smith    Level: intermediate
21766c699258SBarry Smith 
21776c699258SBarry Smith 
21786c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
21796c699258SBarry Smith @*/
21807087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
21816c699258SBarry Smith {
21826c699258SBarry Smith   PetscFunctionBegin;
21830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21846c699258SBarry Smith   *dm = ts->dm;
21856c699258SBarry Smith   PetscFunctionReturn(0);
21866c699258SBarry Smith }
21871713a123SBarry Smith 
21880f5c6efeSJed Brown #undef __FUNCT__
21890f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
21900f5c6efeSJed Brown /*@
21910f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
21920f5c6efeSJed Brown 
21933f9fe445SBarry Smith    Logically Collective on SNES
21940f5c6efeSJed Brown 
21950f5c6efeSJed Brown    Input Parameter:
2196d42a1c89SJed Brown + snes - nonlinear solver
21970f5c6efeSJed Brown . X - the current state at which to evaluate the residual
2198d42a1c89SJed Brown - ctx - user context, must be a TS
21990f5c6efeSJed Brown 
22000f5c6efeSJed Brown    Output Parameter:
22010f5c6efeSJed Brown . F - the nonlinear residual
22020f5c6efeSJed Brown 
22030f5c6efeSJed Brown    Notes:
22040f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
22050f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
22060f5c6efeSJed Brown 
22070f5c6efeSJed Brown    Level: advanced
22080f5c6efeSJed Brown 
22090f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
22100f5c6efeSJed Brown @*/
22117087cfbeSBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx)
22120f5c6efeSJed Brown {
22130f5c6efeSJed Brown   TS ts = (TS)ctx;
22140f5c6efeSJed Brown   PetscErrorCode ierr;
22150f5c6efeSJed Brown 
22160f5c6efeSJed Brown   PetscFunctionBegin;
22170f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22180f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
22190f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
22200f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
22210f5c6efeSJed Brown   ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr);
22220f5c6efeSJed Brown   PetscFunctionReturn(0);
22230f5c6efeSJed Brown }
22240f5c6efeSJed Brown 
22250f5c6efeSJed Brown #undef __FUNCT__
22260f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
22270f5c6efeSJed Brown /*@
22280f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
22290f5c6efeSJed Brown 
22300f5c6efeSJed Brown    Collective on SNES
22310f5c6efeSJed Brown 
22320f5c6efeSJed Brown    Input Parameter:
22330f5c6efeSJed Brown + snes - nonlinear solver
22340f5c6efeSJed Brown . X - the current state at which to evaluate the residual
22350f5c6efeSJed Brown - ctx - user context, must be a TS
22360f5c6efeSJed Brown 
22370f5c6efeSJed Brown    Output Parameter:
22380f5c6efeSJed Brown + A - the Jacobian
22390f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
22400f5c6efeSJed Brown - flag - indicates any structure change in the matrix
22410f5c6efeSJed Brown 
22420f5c6efeSJed Brown    Notes:
22430f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
22440f5c6efeSJed Brown 
22450f5c6efeSJed Brown    Level: developer
22460f5c6efeSJed Brown 
22470f5c6efeSJed Brown .seealso: SNESSetJacobian()
22480f5c6efeSJed Brown @*/
22497087cfbeSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx)
22500f5c6efeSJed Brown {
22510f5c6efeSJed Brown   TS ts = (TS)ctx;
22520f5c6efeSJed Brown   PetscErrorCode ierr;
22530f5c6efeSJed Brown 
22540f5c6efeSJed Brown   PetscFunctionBegin;
22550f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22560f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
22570f5c6efeSJed Brown   PetscValidPointer(A,3);
22580f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
22590f5c6efeSJed Brown   PetscValidPointer(B,4);
22600f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
22610f5c6efeSJed Brown   PetscValidPointer(flag,5);
22620f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
22630f5c6efeSJed Brown   ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr);
22640f5c6efeSJed Brown   PetscFunctionReturn(0);
22650f5c6efeSJed Brown }
2266325fc9f4SBarry Smith 
2267325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2268325fc9f4SBarry Smith #include "mex.h"
2269325fc9f4SBarry Smith 
2270325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
2271325fc9f4SBarry Smith 
2272325fc9f4SBarry Smith #undef __FUNCT__
2273325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
2274325fc9f4SBarry Smith /*
2275325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
2276325fc9f4SBarry Smith                          TSSetFunctionMatlab().
2277325fc9f4SBarry Smith 
2278325fc9f4SBarry Smith    Collective on TS
2279325fc9f4SBarry Smith 
2280325fc9f4SBarry Smith    Input Parameters:
2281325fc9f4SBarry Smith +  snes - the TS context
2282325fc9f4SBarry Smith -  x - input vector
2283325fc9f4SBarry Smith 
2284325fc9f4SBarry Smith    Output Parameter:
2285325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
2286325fc9f4SBarry Smith 
2287325fc9f4SBarry Smith    Notes:
2288325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
2289325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
2290325fc9f4SBarry Smith    themselves.
2291325fc9f4SBarry Smith 
2292325fc9f4SBarry Smith    Level: developer
2293325fc9f4SBarry Smith 
2294325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2295325fc9f4SBarry Smith 
2296325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2297325fc9f4SBarry Smith */
22987087cfbeSBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx)
2299325fc9f4SBarry Smith {
2300325fc9f4SBarry Smith   PetscErrorCode   ierr;
2301325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2302325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
2303325fc9f4SBarry Smith   mxArray	   *plhs[1],*prhs[7];
2304325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
2305325fc9f4SBarry Smith 
2306325fc9f4SBarry Smith   PetscFunctionBegin;
2307325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2308325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2309325fc9f4SBarry Smith   PetscValidHeaderSpecific(xdot,VEC_CLASSID,4);
2310325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
2311325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,x,3);
2312325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
2313325fc9f4SBarry Smith 
2314325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2315325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2316880f3077SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr);
2317325fc9f4SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
2318325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2319325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
2320325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2321325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2322325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
2323325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
2324325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
2325325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
2326325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2327325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2328325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2329325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2330325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2331325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2332325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2333325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2334325fc9f4SBarry Smith   PetscFunctionReturn(0);
2335325fc9f4SBarry Smith }
2336325fc9f4SBarry Smith 
2337325fc9f4SBarry Smith 
2338325fc9f4SBarry Smith #undef __FUNCT__
2339325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
2340325fc9f4SBarry Smith /*
2341325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
2342325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
2343e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
2344325fc9f4SBarry Smith 
2345325fc9f4SBarry Smith    Logically Collective on TS
2346325fc9f4SBarry Smith 
2347325fc9f4SBarry Smith    Input Parameters:
2348325fc9f4SBarry Smith +  ts - the TS context
2349325fc9f4SBarry Smith -  func - function evaluation routine
2350325fc9f4SBarry Smith 
2351325fc9f4SBarry Smith    Calling sequence of func:
2352325fc9f4SBarry Smith $    func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx);
2353325fc9f4SBarry Smith 
2354325fc9f4SBarry Smith    Level: beginner
2355325fc9f4SBarry Smith 
2356325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2357325fc9f4SBarry Smith 
2358325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2359325fc9f4SBarry Smith */
23607087cfbeSBarry Smith PetscErrorCode  TSSetFunctionMatlab(TS snes,const char *func,mxArray *ctx)
2361325fc9f4SBarry Smith {
2362325fc9f4SBarry Smith   PetscErrorCode  ierr;
2363325fc9f4SBarry Smith   TSMatlabContext *sctx;
2364325fc9f4SBarry Smith 
2365325fc9f4SBarry Smith   PetscFunctionBegin;
2366325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2367325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2368325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2369325fc9f4SBarry Smith   /*
2370325fc9f4SBarry Smith      This should work, but it doesn't
2371325fc9f4SBarry Smith   sctx->ctx = ctx;
2372325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2373325fc9f4SBarry Smith   */
2374325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2375325fc9f4SBarry Smith   ierr = TSSetIFunction(snes,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
2376325fc9f4SBarry Smith   PetscFunctionReturn(0);
2377325fc9f4SBarry Smith }
2378325fc9f4SBarry Smith 
2379325fc9f4SBarry Smith #undef __FUNCT__
2380325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
2381325fc9f4SBarry Smith /*
2382325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
2383325fc9f4SBarry Smith                          TSSetJacobianMatlab().
2384325fc9f4SBarry Smith 
2385325fc9f4SBarry Smith    Collective on TS
2386325fc9f4SBarry Smith 
2387325fc9f4SBarry Smith    Input Parameters:
2388325fc9f4SBarry Smith +  snes - the TS context
2389325fc9f4SBarry Smith .  x - input vector
2390325fc9f4SBarry Smith .  A, B - the matrices
2391325fc9f4SBarry Smith -  ctx - user context
2392325fc9f4SBarry Smith 
2393325fc9f4SBarry Smith    Output Parameter:
2394325fc9f4SBarry Smith .  flag - structure of the matrix
2395325fc9f4SBarry Smith 
2396325fc9f4SBarry Smith    Level: developer
2397325fc9f4SBarry Smith 
2398325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2399325fc9f4SBarry Smith 
2400325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2401325fc9f4SBarry Smith @*/
24027087cfbeSBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
2403325fc9f4SBarry Smith {
2404325fc9f4SBarry Smith   PetscErrorCode  ierr;
2405325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2406325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
2407325fc9f4SBarry Smith   mxArray	  *plhs[2],*prhs[9];
2408325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
2409325fc9f4SBarry Smith 
2410325fc9f4SBarry Smith   PetscFunctionBegin;
2411325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2412325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2413325fc9f4SBarry Smith 
2414325fc9f4SBarry Smith   /* call Matlab function in ctx with arguments x and y */
2415325fc9f4SBarry Smith 
2416325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2417325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2418325fc9f4SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr);
2419325fc9f4SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
2420325fc9f4SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
2421325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2422325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
2423325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2424325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2425325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
2426325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
2427325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
2428325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
2429325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
2430325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
2431325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2432325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
2433325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2434325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2435325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2436325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2437325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2438325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2439325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
2440325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
2441325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2442325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
2443325fc9f4SBarry Smith   PetscFunctionReturn(0);
2444325fc9f4SBarry Smith }
2445325fc9f4SBarry Smith 
2446325fc9f4SBarry Smith 
2447325fc9f4SBarry Smith #undef __FUNCT__
2448325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
2449325fc9f4SBarry Smith /*
2450325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
2451e3c5b3baSBarry 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
2452325fc9f4SBarry Smith 
2453325fc9f4SBarry Smith    Logically Collective on TS
2454325fc9f4SBarry Smith 
2455325fc9f4SBarry Smith    Input Parameters:
2456325fc9f4SBarry Smith +  snes - the TS context
2457325fc9f4SBarry Smith .  A,B - Jacobian matrices
2458325fc9f4SBarry Smith .  func - function evaluation routine
2459325fc9f4SBarry Smith -  ctx - user context
2460325fc9f4SBarry Smith 
2461325fc9f4SBarry Smith    Calling sequence of func:
2462325fc9f4SBarry Smith $    flag = func (TS snes,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx);
2463325fc9f4SBarry Smith 
2464325fc9f4SBarry Smith 
2465325fc9f4SBarry Smith    Level: developer
2466325fc9f4SBarry Smith 
2467325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2468325fc9f4SBarry Smith 
2469325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2470325fc9f4SBarry Smith */
24717087cfbeSBarry Smith PetscErrorCode  TSSetJacobianMatlab(TS snes,Mat A,Mat B,const char *func,mxArray *ctx)
2472325fc9f4SBarry Smith {
2473325fc9f4SBarry Smith   PetscErrorCode    ierr;
2474325fc9f4SBarry Smith   TSMatlabContext *sctx;
2475325fc9f4SBarry Smith 
2476325fc9f4SBarry Smith   PetscFunctionBegin;
2477325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2478325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2479325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2480325fc9f4SBarry Smith   /*
2481325fc9f4SBarry Smith      This should work, but it doesn't
2482325fc9f4SBarry Smith   sctx->ctx = ctx;
2483325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2484325fc9f4SBarry Smith   */
2485325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2486325fc9f4SBarry Smith   ierr = TSSetIJacobian(snes,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
2487325fc9f4SBarry Smith   PetscFunctionReturn(0);
2488325fc9f4SBarry Smith }
2489325fc9f4SBarry Smith 
2490b5b1a830SBarry Smith #undef __FUNCT__
2491b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
2492b5b1a830SBarry Smith /*
2493b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
2494b5b1a830SBarry Smith 
2495b5b1a830SBarry Smith    Collective on TS
2496b5b1a830SBarry Smith 
2497b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2498b5b1a830SBarry Smith @*/
24997087cfbeSBarry Smith PetscErrorCode  TSMonitor_Matlab(TS snes,PetscInt it, PetscReal time,Vec x, void *ctx)
2500b5b1a830SBarry Smith {
2501b5b1a830SBarry Smith   PetscErrorCode  ierr;
2502b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2503a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
2504b5b1a830SBarry Smith   mxArray	  *plhs[1],*prhs[6];
2505b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
2506b5b1a830SBarry Smith 
2507b5b1a830SBarry Smith   PetscFunctionBegin;
2508b5b1a830SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2509b5b1a830SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,4);
2510b5b1a830SBarry Smith 
2511b5b1a830SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2512b5b1a830SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2513b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2514b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
2515b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
2516b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
2517b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
2518b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
2519b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
2520b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2521b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
2522b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
2523b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
2524b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
2525b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
2526b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
2527b5b1a830SBarry Smith   PetscFunctionReturn(0);
2528b5b1a830SBarry Smith }
2529b5b1a830SBarry Smith 
2530b5b1a830SBarry Smith 
2531b5b1a830SBarry Smith #undef __FUNCT__
2532b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
2533b5b1a830SBarry Smith /*
2534b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
2535b5b1a830SBarry Smith 
2536b5b1a830SBarry Smith    Level: developer
2537b5b1a830SBarry Smith 
2538b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
2539b5b1a830SBarry Smith 
2540b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2541b5b1a830SBarry Smith */
25427087cfbeSBarry Smith PetscErrorCode  TSMonitorSetMatlab(TS snes,const char *func,mxArray *ctx)
2543b5b1a830SBarry Smith {
2544b5b1a830SBarry Smith   PetscErrorCode    ierr;
2545b5b1a830SBarry Smith   TSMatlabContext *sctx;
2546b5b1a830SBarry Smith 
2547b5b1a830SBarry Smith   PetscFunctionBegin;
2548b5b1a830SBarry Smith   /* currently sctx is memory bleed */
2549b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2550b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2551b5b1a830SBarry Smith   /*
2552b5b1a830SBarry Smith      This should work, but it doesn't
2553b5b1a830SBarry Smith   sctx->ctx = ctx;
2554b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2555b5b1a830SBarry Smith   */
2556b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2557b5b1a830SBarry Smith   ierr = TSMonitorSet(snes,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
2558b5b1a830SBarry Smith   PetscFunctionReturn(0);
2559b5b1a830SBarry Smith }
2560b5b1a830SBarry Smith 
2561325fc9f4SBarry Smith #endif
2562