xref: /petsc/src/ts/interface/ts.c (revision 01c7e25b8ddc92510c65d14ec7e5a894e24a4fff)
163dd3a1aSKris Buschelman 
2c6db04a5SJed Brown #include <private/tsimpl.h>        /*I "petscts.h"  I*/
3d763cef2SBarry Smith 
4d5ba7fb7SMatthew Knepley /* Logging support */
57087cfbeSBarry Smith PetscClassId  TS_CLASSID;
6166c7f25SBarry Smith PetscLogEvent  TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
7d405a339SMatthew Knepley 
84a2ae208SSatish Balay #undef __FUNCT__
9bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
10bdad233fSMatthew Knepley /*
11bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
12bdad233fSMatthew Knepley 
13bdad233fSMatthew Knepley   Collective on TS
14bdad233fSMatthew Knepley 
15bdad233fSMatthew Knepley   Input Parameter:
16bdad233fSMatthew Knepley . ts - The ts
17bdad233fSMatthew Knepley 
18bdad233fSMatthew Knepley   Level: intermediate
19bdad233fSMatthew Knepley 
20bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
21bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
22bdad233fSMatthew Knepley */
236849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
24bdad233fSMatthew Knepley {
25ace3abfcSBarry Smith   PetscBool      opt;
262fc52814SBarry Smith   const char     *defaultType;
27bdad233fSMatthew Knepley   char           typeName[256];
28dfbe8321SBarry Smith   PetscErrorCode ierr;
29bdad233fSMatthew Knepley 
30bdad233fSMatthew Knepley   PetscFunctionBegin;
317adad957SLisandro Dalcin   if (((PetscObject)ts)->type_name) {
327adad957SLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
33bdad233fSMatthew Knepley   } else {
349596e0b4SJed Brown     defaultType = TSEULER;
35bdad233fSMatthew Knepley   }
36bdad233fSMatthew Knepley 
37cce0b1b2SLisandro Dalcin   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
38bdad233fSMatthew Knepley   ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
39a7cc72afSBarry Smith   if (opt) {
40bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
41bdad233fSMatthew Knepley   } else {
42bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
43bdad233fSMatthew Knepley   }
44bdad233fSMatthew Knepley   PetscFunctionReturn(0);
45bdad233fSMatthew Knepley }
46bdad233fSMatthew Knepley 
47bdad233fSMatthew Knepley #undef __FUNCT__
48bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
49bdad233fSMatthew Knepley /*@
50bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
51bdad233fSMatthew Knepley 
52bdad233fSMatthew Knepley    Collective on TS
53bdad233fSMatthew Knepley 
54bdad233fSMatthew Knepley    Input Parameter:
55bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
56bdad233fSMatthew Knepley 
57bdad233fSMatthew Knepley    Options Database Keys:
584d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
59bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
60bdad233fSMatthew Knepley .  -ts_max_time time - maximum time to compute to
61bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
62bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
63a6570f20SBarry Smith -  -ts_monitor_draw - plot information at each timestep
64bdad233fSMatthew Knepley 
65bdad233fSMatthew Knepley    Level: beginner
66bdad233fSMatthew Knepley 
67bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
68bdad233fSMatthew Knepley 
69a313700dSBarry Smith .seealso: TSGetType()
70bdad233fSMatthew Knepley @*/
717087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
72bdad233fSMatthew Knepley {
73ace3abfcSBarry Smith   PetscBool      opt,flg;
74dfbe8321SBarry Smith   PetscErrorCode ierr;
75649052a6SBarry Smith   PetscViewer    monviewer;
76eabae89aSBarry Smith   char           monfilename[PETSC_MAX_PATH_LEN];
77089b2837SJed Brown   SNES           snes;
78bdad233fSMatthew Knepley 
79bdad233fSMatthew Knepley   PetscFunctionBegin;
800700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
813194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
82a43b19c4SJed Brown     /* Handle TS type options */
83a43b19c4SJed Brown     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
84bdad233fSMatthew Knepley 
85bdad233fSMatthew Knepley     /* Handle generic TS options */
86bdad233fSMatthew Knepley     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
87bdad233fSMatthew Knepley     ierr = PetscOptionsReal("-ts_max_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
88d8cd7023SBarry Smith     ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr);
89d8cd7023SBarry Smith     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&ts->time_step,PETSC_NULL);CHKERRQ(ierr);
90a43b19c4SJed Brown     opt = ts->exact_final_time == PETSC_DECIDE ? PETSC_FALSE : (PetscBool)ts->exact_final_time;
91a43b19c4SJed Brown     ierr = PetscOptionsBool("-ts_exact_final_time","Interpolate output to stop exactly at the final time","TSSetExactFinalTime",opt,&opt,&flg);CHKERRQ(ierr);
92461e00b3SSean Farley     if (flg) {ierr = TSSetExactFinalTime(ts,opt);CHKERRQ(ierr);}
93193ac0bcSJed Brown     ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","",ts->max_snes_failures,&ts->max_snes_failures,PETSC_NULL);CHKERRQ(ierr);
94193ac0bcSJed Brown     ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections","",ts->max_reject,&ts->max_reject,PETSC_NULL);CHKERRQ(ierr);
95193ac0bcSJed Brown     ierr = PetscOptionsBool("-ts_error_if_step_failed","Error if no step succeeds","",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr);
96bdad233fSMatthew Knepley 
97bdad233fSMatthew Knepley     /* Monitor options */
98a6570f20SBarry Smith     ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
99eabae89aSBarry Smith     if (flg) {
100649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr);
101649052a6SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
102bdad233fSMatthew Knepley     }
1035180491cSLisandro Dalcin     ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1045180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1055180491cSLisandro Dalcin 
10690d69ab7SBarry Smith     opt  = PETSC_FALSE;
107acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
108a7cc72afSBarry Smith     if (opt) {
109a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
110bdad233fSMatthew Knepley     }
11190d69ab7SBarry Smith     opt  = PETSC_FALSE;
112acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
113a7cc72afSBarry Smith     if (opt) {
1146083293cSBarry Smith       void *ctx;
1156083293cSBarry Smith       ierr = TSMonitorSolutionCreate(ts,PETSC_NULL,&ctx);CHKERRQ(ierr);
1166083293cSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolution,ctx,TSMonitorSolutionDestroy);CHKERRQ(ierr);
117bdad233fSMatthew Knepley     }
118fb1732b5SBarry Smith     opt  = PETSC_FALSE;
119fb1732b5SBarry Smith     ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
120fb1732b5SBarry Smith     if (flg) {
121fb1732b5SBarry Smith       PetscViewer ctx;
122fb1732b5SBarry Smith       if (monfilename[0]) {
123fb1732b5SBarry Smith         ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
124fb1732b5SBarry Smith       } else {
125fb1732b5SBarry Smith         ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm);
126fb1732b5SBarry Smith       }
127fb1732b5SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
128fb1732b5SBarry Smith     }
129bdad233fSMatthew Knepley 
130d52bd9f3SBarry Smith     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
131d52bd9f3SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
132d52bd9f3SBarry Smith 
133bdad233fSMatthew Knepley     /* Handle specific TS options */
134abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
135bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
136bdad233fSMatthew Knepley     }
1375d973c19SBarry Smith 
1385d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
1395d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
140bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
141bdad233fSMatthew Knepley   PetscFunctionReturn(0);
142bdad233fSMatthew Knepley }
143bdad233fSMatthew Knepley 
144bdad233fSMatthew Knepley #undef __FUNCT__
145cdcf91faSSean Farley #undef __FUNCT__
1464a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
147a7a1495cSBarry Smith /*@
1488c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
149a7a1495cSBarry Smith       set with TSSetRHSJacobian().
150a7a1495cSBarry Smith 
151a7a1495cSBarry Smith    Collective on TS and Vec
152a7a1495cSBarry Smith 
153a7a1495cSBarry Smith    Input Parameters:
154316643e7SJed Brown +  ts - the TS context
155a7a1495cSBarry Smith .  t - current timestep
156a7a1495cSBarry Smith -  x - input vector
157a7a1495cSBarry Smith 
158a7a1495cSBarry Smith    Output Parameters:
159a7a1495cSBarry Smith +  A - Jacobian matrix
160a7a1495cSBarry Smith .  B - optional preconditioning matrix
161a7a1495cSBarry Smith -  flag - flag indicating matrix structure
162a7a1495cSBarry Smith 
163a7a1495cSBarry Smith    Notes:
164a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
165a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
166a7a1495cSBarry Smith 
16794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
168a7a1495cSBarry Smith    flag parameter.
169a7a1495cSBarry Smith 
170a7a1495cSBarry Smith    Level: developer
171a7a1495cSBarry Smith 
172a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
173a7a1495cSBarry Smith 
17494b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
175a7a1495cSBarry Smith @*/
1767087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
177a7a1495cSBarry Smith {
178dfbe8321SBarry Smith   PetscErrorCode ierr;
1790e4ef248SJed Brown   PetscInt Xstate;
180a7a1495cSBarry Smith 
181a7a1495cSBarry Smith   PetscFunctionBegin;
1820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1830700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
184c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
1850e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
1860e4ef248SJed Brown   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) {
1870e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
1880e4ef248SJed Brown     PetscFunctionReturn(0);
189f8ede8e7SJed Brown   }
190d90be118SSean Farley 
1913b5f76d0SSean Farley   if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
192d90be118SSean Farley 
1933b5f76d0SSean Farley   if (ts->userops->rhsjacobian) {
194d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
195a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
196a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
1973b5f76d0SSean Farley     ierr = (*ts->userops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr);
198a7a1495cSBarry Smith     PetscStackPop;
199d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
200a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
2010700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
2020700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
203ef66eb69SBarry Smith   } else {
204214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
205214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
206214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
207ef66eb69SBarry Smith   }
2080e4ef248SJed Brown   ts->rhsjacobian.time = t;
2090e4ef248SJed Brown   ts->rhsjacobian.X = X;
2100e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
2110e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
212a7a1495cSBarry Smith   PetscFunctionReturn(0);
213a7a1495cSBarry Smith }
214a7a1495cSBarry Smith 
2154a2ae208SSatish Balay #undef __FUNCT__
2164a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
217316643e7SJed Brown /*@
218d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
219d763cef2SBarry Smith 
220316643e7SJed Brown    Collective on TS and Vec
221316643e7SJed Brown 
222316643e7SJed Brown    Input Parameters:
223316643e7SJed Brown +  ts - the TS context
224316643e7SJed Brown .  t - current time
225316643e7SJed Brown -  x - state vector
226316643e7SJed Brown 
227316643e7SJed Brown    Output Parameter:
228316643e7SJed Brown .  y - right hand side
229316643e7SJed Brown 
230316643e7SJed Brown    Note:
231316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
232316643e7SJed Brown    is used internally within the nonlinear solvers.
233316643e7SJed Brown 
234316643e7SJed Brown    Level: developer
235316643e7SJed Brown 
236316643e7SJed Brown .keywords: TS, compute
237316643e7SJed Brown 
238316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
239316643e7SJed Brown @*/
240dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
241d763cef2SBarry Smith {
242dfbe8321SBarry Smith   PetscErrorCode ierr;
243d763cef2SBarry Smith 
244d763cef2SBarry Smith   PetscFunctionBegin;
2450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2460700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2470700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
248d763cef2SBarry Smith 
2493b5f76d0SSean Farley   if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
250d763cef2SBarry Smith 
251d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
2523b5f76d0SSean Farley   if (ts->userops->rhsfunction) {
253d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
2543b5f76d0SSean Farley     ierr = (*ts->userops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr);
255d763cef2SBarry Smith     PetscStackPop;
256214bc6a2SJed Brown   } else {
257214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
258b2cd27e8SJed Brown   }
25944a41b28SSean Farley 
260d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
261d763cef2SBarry Smith   PetscFunctionReturn(0);
262d763cef2SBarry Smith }
263d763cef2SBarry Smith 
2644a2ae208SSatish Balay #undef __FUNCT__
265214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
266214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
267214bc6a2SJed Brown {
2682dd45cf8SJed Brown   Vec            F;
269214bc6a2SJed Brown   PetscErrorCode ierr;
270214bc6a2SJed Brown 
271214bc6a2SJed Brown   PetscFunctionBegin;
2720da9a4f0SJed Brown   *Frhs = PETSC_NULL;
2732dd45cf8SJed Brown   ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
274214bc6a2SJed Brown   if (!ts->Frhs) {
2752dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
276214bc6a2SJed Brown   }
277214bc6a2SJed Brown   *Frhs = ts->Frhs;
278214bc6a2SJed Brown   PetscFunctionReturn(0);
279214bc6a2SJed Brown }
280214bc6a2SJed Brown 
281214bc6a2SJed Brown #undef __FUNCT__
282214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
283214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
284214bc6a2SJed Brown {
285214bc6a2SJed Brown   Mat            A,B;
2862dd45cf8SJed Brown   PetscErrorCode ierr;
287214bc6a2SJed Brown 
288214bc6a2SJed Brown   PetscFunctionBegin;
289214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
290214bc6a2SJed Brown   if (Arhs) {
291214bc6a2SJed Brown     if (!ts->Arhs) {
292214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
293214bc6a2SJed Brown     }
294214bc6a2SJed Brown     *Arhs = ts->Arhs;
295214bc6a2SJed Brown   }
296214bc6a2SJed Brown   if (Brhs) {
297214bc6a2SJed Brown     if (!ts->Brhs) {
298214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
299214bc6a2SJed Brown     }
300214bc6a2SJed Brown     *Brhs = ts->Brhs;
301214bc6a2SJed Brown   }
302214bc6a2SJed Brown   PetscFunctionReturn(0);
303214bc6a2SJed Brown }
304214bc6a2SJed Brown 
305214bc6a2SJed Brown #undef __FUNCT__
306316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
307316643e7SJed Brown /*@
308316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
309316643e7SJed Brown 
310316643e7SJed Brown    Collective on TS and Vec
311316643e7SJed Brown 
312316643e7SJed Brown    Input Parameters:
313316643e7SJed Brown +  ts - the TS context
314316643e7SJed Brown .  t - current time
315316643e7SJed Brown .  X - state vector
316214bc6a2SJed Brown .  Xdot - time derivative of state vector
317214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
318316643e7SJed Brown 
319316643e7SJed Brown    Output Parameter:
320316643e7SJed Brown .  Y - right hand side
321316643e7SJed Brown 
322316643e7SJed Brown    Note:
323316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
324316643e7SJed Brown    is used internally within the nonlinear solvers.
325316643e7SJed Brown 
326316643e7SJed Brown    If the user did did not write their equations in implicit form, this
327316643e7SJed Brown    function recasts them in implicit form.
328316643e7SJed Brown 
329316643e7SJed Brown    Level: developer
330316643e7SJed Brown 
331316643e7SJed Brown .keywords: TS, compute
332316643e7SJed Brown 
333316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
334316643e7SJed Brown @*/
335214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex)
336316643e7SJed Brown {
337316643e7SJed Brown   PetscErrorCode ierr;
338316643e7SJed Brown 
339316643e7SJed Brown   PetscFunctionBegin;
3400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3410700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3420700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
3430700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
344316643e7SJed Brown 
345d90be118SSean Farley   if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
346d90be118SSean Farley 
347316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
3483b5f76d0SSean Farley   if (ts->userops->ifunction) {
349316643e7SJed Brown     PetscStackPush("TS user implicit function");
3503b5f76d0SSean Farley     ierr = (*ts->userops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr);
351316643e7SJed Brown     PetscStackPop;
352214bc6a2SJed Brown   }
353214bc6a2SJed Brown   if (imex) {
3543b5f76d0SSean Farley     if (!ts->userops->ifunction) {
3552dd45cf8SJed Brown       ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);
3562dd45cf8SJed Brown     }
3572dd45cf8SJed Brown   } else if (ts->userops->rhsfunction) {
3582dd45cf8SJed Brown     if (ts->userops->ifunction) {
359214bc6a2SJed Brown       Vec Frhs;
360214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
361214bc6a2SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr);
362214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
3632dd45cf8SJed Brown     } else {
3642dd45cf8SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr);
3652dd45cf8SJed Brown       ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr);
366316643e7SJed Brown     }
3674a6899ffSJed Brown   }
368316643e7SJed Brown   ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
369316643e7SJed Brown   PetscFunctionReturn(0);
370316643e7SJed Brown }
371316643e7SJed Brown 
372316643e7SJed Brown #undef __FUNCT__
373316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
374316643e7SJed Brown /*@
375316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
376316643e7SJed Brown 
377316643e7SJed Brown    Collective on TS and Vec
378316643e7SJed Brown 
379316643e7SJed Brown    Input
380316643e7SJed Brown       Input Parameters:
381316643e7SJed Brown +  ts - the TS context
382316643e7SJed Brown .  t - current timestep
383316643e7SJed Brown .  X - state vector
384316643e7SJed Brown .  Xdot - time derivative of state vector
385214bc6a2SJed Brown .  shift - shift to apply, see note below
386214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
387316643e7SJed Brown 
388316643e7SJed Brown    Output Parameters:
389316643e7SJed Brown +  A - Jacobian matrix
390316643e7SJed Brown .  B - optional preconditioning matrix
391316643e7SJed Brown -  flag - flag indicating matrix structure
392316643e7SJed Brown 
393316643e7SJed Brown    Notes:
394316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
395316643e7SJed Brown 
396316643e7SJed Brown    dF/dX + shift*dF/dXdot
397316643e7SJed Brown 
398316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
399316643e7SJed Brown    is used internally within the nonlinear solvers.
400316643e7SJed Brown 
401316643e7SJed Brown    Level: developer
402316643e7SJed Brown 
403316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
404316643e7SJed Brown 
405316643e7SJed Brown .seealso:  TSSetIJacobian()
40663495f91SJed Brown @*/
407214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
408316643e7SJed Brown {
4090026cea9SSean Farley   PetscInt Xstate, Xdotstate;
410316643e7SJed Brown   PetscErrorCode ierr;
411316643e7SJed Brown 
412316643e7SJed Brown   PetscFunctionBegin;
4130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4140700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4150700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
416316643e7SJed Brown   PetscValidPointer(A,6);
4170700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
418316643e7SJed Brown   PetscValidPointer(B,7);
4190700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
420316643e7SJed Brown   PetscValidPointer(flg,8);
4210026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
4220026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr);
4230026cea9SSean Farley   if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == X && ts->ijacobian.Xstate == Xstate && ts->ijacobian.Xdot == Xdot && ts->ijacobian.Xdotstate == Xdotstate && ts->ijacobian.imex == imex))) {
4240026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
4250026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4260026cea9SSean Farley     PetscFunctionReturn(0);
4270026cea9SSean Farley   }
428316643e7SJed Brown 
4293b5f76d0SSean Farley   if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
430316643e7SJed Brown 
4314e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
432316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
4333b5f76d0SSean Farley   if (ts->userops->ijacobian) {
4342dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
435316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
4363b5f76d0SSean Farley     ierr = (*ts->userops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr);
437316643e7SJed Brown     PetscStackPop;
438214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
439214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
440214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
4414a6899ffSJed Brown   }
442214bc6a2SJed Brown   if (imex) {
4433b5f76d0SSean Farley     if (!ts->userops->ijacobian) {  /* system was written as Xdot = F(t,X) */
444214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
4452dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
446214bc6a2SJed Brown       if (*A != *B) {
447214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
448214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
449214bc6a2SJed Brown       }
450214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
451214bc6a2SJed Brown     }
452214bc6a2SJed Brown   } else {
4533b5f76d0SSean Farley     if (!ts->userops->ijacobian) {
454214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr);
455214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
456214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
457316643e7SJed Brown       if (*A != *B) {
458316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
459316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
460316643e7SJed Brown       }
4613b5f76d0SSean Farley     } else if (ts->userops->rhsjacobian) {
462214bc6a2SJed Brown       Mat Arhs,Brhs;
463214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
464214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
465214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
466214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
467214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
468214bc6a2SJed Brown       if (*A != *B) {
469214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
470214bc6a2SJed Brown       }
471214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
472214bc6a2SJed Brown     }
473316643e7SJed Brown   }
4740026cea9SSean Farley 
4750026cea9SSean Farley   ts->ijacobian.time = t;
4760026cea9SSean Farley   ts->ijacobian.X = X;
4770026cea9SSean Farley   ts->ijacobian.Xdot = Xdot;
4780026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr);
4790026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
4800026cea9SSean Farley   ts->ijacobian.shift = shift;
4810026cea9SSean Farley   ts->ijacobian.imex = imex;
4820026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
483316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
484316643e7SJed Brown   PetscFunctionReturn(0);
485316643e7SJed Brown }
486316643e7SJed Brown 
487316643e7SJed Brown #undef __FUNCT__
4884a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
489d763cef2SBarry Smith /*@C
490d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
491d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
492d763cef2SBarry Smith 
4933f9fe445SBarry Smith     Logically Collective on TS
494d763cef2SBarry Smith 
495d763cef2SBarry Smith     Input Parameters:
496d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
497ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
498d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
499d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
500d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
501d763cef2SBarry Smith 
502d763cef2SBarry Smith     Calling sequence of func:
50387828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
504d763cef2SBarry Smith 
505d763cef2SBarry Smith +   t - current timestep
506d763cef2SBarry Smith .   u - input vector
507d763cef2SBarry Smith .   F - function vector
508d763cef2SBarry Smith -   ctx - [optional] user-defined function context
509d763cef2SBarry Smith 
510d763cef2SBarry Smith     Important:
51176f2fa84SHong Zhang     The user MUST call either this routine or TSSetMatrices().
512d763cef2SBarry Smith 
513d763cef2SBarry Smith     Level: beginner
514d763cef2SBarry Smith 
515d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
516d763cef2SBarry Smith 
51776f2fa84SHong Zhang .seealso: TSSetMatrices()
518d763cef2SBarry Smith @*/
519089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
520d763cef2SBarry Smith {
521089b2837SJed Brown   PetscErrorCode ierr;
522089b2837SJed Brown   SNES           snes;
523d763cef2SBarry Smith 
524089b2837SJed Brown   PetscFunctionBegin;
5250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
526ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
5273b5f76d0SSean Farley   if (f)   ts->userops->rhsfunction = f;
5284e684422SJed Brown   if (ctx) ts->funP                 = ctx;
529089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
530089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
531d763cef2SBarry Smith   PetscFunctionReturn(0);
532d763cef2SBarry Smith }
533d763cef2SBarry Smith 
5344a2ae208SSatish Balay #undef __FUNCT__
5354a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
536d763cef2SBarry Smith /*@C
537d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
538d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
53976f2fa84SHong Zhang    Use TSSetMatrices() for linear problems.
540d763cef2SBarry Smith 
5413f9fe445SBarry Smith    Logically Collective on TS
542d763cef2SBarry Smith 
543d763cef2SBarry Smith    Input Parameters:
544d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
545d763cef2SBarry Smith .  A   - Jacobian matrix
546d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
547d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
548d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
549d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
550d763cef2SBarry Smith 
551d763cef2SBarry Smith    Calling sequence of func:
55287828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
553d763cef2SBarry Smith 
554d763cef2SBarry Smith +  t - current timestep
555d763cef2SBarry Smith .  u - input vector
556d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
557d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
558d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
55994b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
560d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
561d763cef2SBarry Smith 
562d763cef2SBarry Smith    Notes:
56394b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
564d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
565d763cef2SBarry Smith 
566d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
567d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
56856335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
569d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
570d763cef2SBarry Smith    throughout the global iterations.
571d763cef2SBarry Smith 
572d763cef2SBarry Smith    Level: beginner
573d763cef2SBarry Smith 
574d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
575d763cef2SBarry Smith 
576d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(),
57776f2fa84SHong Zhang           SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices()
578d763cef2SBarry Smith 
579d763cef2SBarry Smith @*/
580089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
581d763cef2SBarry Smith {
582277b19d0SLisandro Dalcin   PetscErrorCode ierr;
583089b2837SJed Brown   SNES           snes;
584277b19d0SLisandro Dalcin 
585d763cef2SBarry Smith   PetscFunctionBegin;
5860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
587277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
588277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
589277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
590277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
591d763cef2SBarry Smith 
5923b5f76d0SSean Farley   if (f)   ts->userops->rhsjacobian = f;
593277b19d0SLisandro Dalcin   if (ctx) ts->jacP                 = ctx;
594089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
595dabe61deSJed Brown   if (!ts->userops->ijacobian) {
596089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
5970e4ef248SJed Brown   }
5980e4ef248SJed Brown   if (A) {
5990e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
6000e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
6010e4ef248SJed Brown     ts->Arhs = A;
6020e4ef248SJed Brown   }
6030e4ef248SJed Brown   if (B) {
6040e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
6050e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
6060e4ef248SJed Brown     ts->Brhs = B;
6070e4ef248SJed Brown   }
608d763cef2SBarry Smith   PetscFunctionReturn(0);
609d763cef2SBarry Smith }
610d763cef2SBarry Smith 
611316643e7SJed Brown 
612316643e7SJed Brown #undef __FUNCT__
613316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
614316643e7SJed Brown /*@C
615316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
616316643e7SJed Brown 
6173f9fe445SBarry Smith    Logically Collective on TS
618316643e7SJed Brown 
619316643e7SJed Brown    Input Parameters:
620316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
621ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
622316643e7SJed Brown .  f   - the function evaluation routine
623316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
624316643e7SJed Brown 
625316643e7SJed Brown    Calling sequence of f:
626316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
627316643e7SJed Brown 
628316643e7SJed Brown +  t   - time at step/stage being solved
629316643e7SJed Brown .  u   - state vector
630316643e7SJed Brown .  u_t - time derivative of state vector
631316643e7SJed Brown .  F   - function vector
632316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
633316643e7SJed Brown 
634316643e7SJed Brown    Important:
635316643e7SJed Brown    The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices().  This routine must be used when not solving an ODE.
636316643e7SJed Brown 
637316643e7SJed Brown    Level: beginner
638316643e7SJed Brown 
639316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
640316643e7SJed Brown 
641316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian()
642316643e7SJed Brown @*/
643089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
644316643e7SJed Brown {
645089b2837SJed Brown   PetscErrorCode ierr;
646089b2837SJed Brown   SNES           snes;
647316643e7SJed Brown 
648316643e7SJed Brown   PetscFunctionBegin;
6490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
650ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
6513b5f76d0SSean Farley   if (f)   ts->userops->ifunction = f;
652089b2837SJed Brown   if (ctx) ts->funP           = ctx;
653089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
654089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
655089b2837SJed Brown   PetscFunctionReturn(0);
656089b2837SJed Brown }
657089b2837SJed Brown 
658089b2837SJed Brown #undef __FUNCT__
659089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
660089b2837SJed Brown /*@C
661089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
662089b2837SJed Brown 
663089b2837SJed Brown    Not Collective
664089b2837SJed Brown 
665089b2837SJed Brown    Input Parameter:
666089b2837SJed Brown .  ts - the TS context
667089b2837SJed Brown 
668089b2837SJed Brown    Output Parameter:
669089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
670089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
671089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
672089b2837SJed Brown 
673089b2837SJed Brown    Level: advanced
674089b2837SJed Brown 
675089b2837SJed Brown .keywords: TS, nonlinear, get, function
676089b2837SJed Brown 
677089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
678089b2837SJed Brown @*/
679089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
680089b2837SJed Brown {
681089b2837SJed Brown   PetscErrorCode ierr;
682089b2837SJed Brown   SNES snes;
683089b2837SJed Brown 
684089b2837SJed Brown   PetscFunctionBegin;
685089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
686089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
687089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
6883b5f76d0SSean Farley   if (func) *func = ts->userops->ifunction;
689089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
690089b2837SJed Brown   PetscFunctionReturn(0);
691089b2837SJed Brown }
692089b2837SJed Brown 
693089b2837SJed Brown #undef __FUNCT__
694089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
695089b2837SJed Brown /*@C
696089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
697089b2837SJed Brown 
698089b2837SJed Brown    Not Collective
699089b2837SJed Brown 
700089b2837SJed Brown    Input Parameter:
701089b2837SJed Brown .  ts - the TS context
702089b2837SJed Brown 
703089b2837SJed Brown    Output Parameter:
704089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
705089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
706089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
707089b2837SJed Brown 
708089b2837SJed Brown    Level: advanced
709089b2837SJed Brown 
710089b2837SJed Brown .keywords: TS, nonlinear, get, function
711089b2837SJed Brown 
712089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
713089b2837SJed Brown @*/
714089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
715089b2837SJed Brown {
716089b2837SJed Brown   PetscErrorCode ierr;
717089b2837SJed Brown   SNES snes;
718089b2837SJed Brown 
719089b2837SJed Brown   PetscFunctionBegin;
720089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
721089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
722089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7233b5f76d0SSean Farley   if (func) *func = ts->userops->rhsfunction;
724089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
725316643e7SJed Brown   PetscFunctionReturn(0);
726316643e7SJed Brown }
727316643e7SJed Brown 
728316643e7SJed Brown #undef __FUNCT__
729316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
730316643e7SJed Brown /*@C
731a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
732a4f0a591SBarry Smith         you provided with TSSetIFunction().
733316643e7SJed Brown 
7343f9fe445SBarry Smith    Logically Collective on TS
735316643e7SJed Brown 
736316643e7SJed Brown    Input Parameters:
737316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
738316643e7SJed Brown .  A   - Jacobian matrix
739316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
740316643e7SJed Brown .  f   - the Jacobian evaluation routine
741316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
742316643e7SJed Brown 
743316643e7SJed Brown    Calling sequence of f:
7441b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
745316643e7SJed Brown 
746316643e7SJed Brown +  t    - time at step/stage being solved
7471b4a444bSJed Brown .  U    - state vector
7481b4a444bSJed Brown .  U_t  - time derivative of state vector
749316643e7SJed Brown .  a    - shift
7501b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
751316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
752316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
753316643e7SJed Brown           structure (same as flag in KSPSetOperators())
754316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
755316643e7SJed Brown 
756316643e7SJed Brown    Notes:
757316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
758316643e7SJed Brown 
759a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
760a4f0a591SBarry Smith    the Jacobian of G(U) = F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
761a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
762a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
763a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
764a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
765a4f0a591SBarry Smith 
766316643e7SJed Brown    Level: beginner
767316643e7SJed Brown 
768316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
769316643e7SJed Brown 
770316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian()
771316643e7SJed Brown 
772316643e7SJed Brown @*/
7737087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
774316643e7SJed Brown {
775316643e7SJed Brown   PetscErrorCode ierr;
776089b2837SJed Brown   SNES           snes;
777316643e7SJed Brown 
778316643e7SJed Brown   PetscFunctionBegin;
7790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7800700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
7810700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
782316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
783316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
7843b5f76d0SSean Farley   if (f)   ts->userops->ijacobian = f;
785316643e7SJed Brown   if (ctx) ts->jacP           = ctx;
786089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
787089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
788316643e7SJed Brown   PetscFunctionReturn(0);
789316643e7SJed Brown }
790316643e7SJed Brown 
7914a2ae208SSatish Balay #undef __FUNCT__
7924a2ae208SSatish Balay #define __FUNCT__ "TSView"
7937e2c5f70SBarry Smith /*@C
794d763cef2SBarry Smith     TSView - Prints the TS data structure.
795d763cef2SBarry Smith 
7964c49b128SBarry Smith     Collective on TS
797d763cef2SBarry Smith 
798d763cef2SBarry Smith     Input Parameters:
799d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
800d763cef2SBarry Smith -   viewer - visualization context
801d763cef2SBarry Smith 
802d763cef2SBarry Smith     Options Database Key:
803d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
804d763cef2SBarry Smith 
805d763cef2SBarry Smith     Notes:
806d763cef2SBarry Smith     The available visualization contexts include
807b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
808b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
809d763cef2SBarry Smith          output where only the first processor opens
810d763cef2SBarry Smith          the file.  All other processors send their
811d763cef2SBarry Smith          data to the first processor to print.
812d763cef2SBarry Smith 
813d763cef2SBarry Smith     The user can open an alternative visualization context with
814b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
815d763cef2SBarry Smith 
816d763cef2SBarry Smith     Level: beginner
817d763cef2SBarry Smith 
818d763cef2SBarry Smith .keywords: TS, timestep, view
819d763cef2SBarry Smith 
820b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
821d763cef2SBarry Smith @*/
8227087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
823d763cef2SBarry Smith {
824dfbe8321SBarry Smith   PetscErrorCode ierr;
825a313700dSBarry Smith   const TSType   type;
826089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
827d763cef2SBarry Smith 
828d763cef2SBarry Smith   PetscFunctionBegin;
8290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8303050cee2SBarry Smith   if (!viewer) {
8317adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
8323050cee2SBarry Smith   }
8330700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
834c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
835fd16b177SBarry Smith 
8362692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
8372692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
83832077d6dSBarry Smith   if (iascii) {
839317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
84077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
841a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
842d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
84377431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr);
844c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
845d763cef2SBarry Smith     }
84677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr);
847193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
848d52bd9f3SBarry Smith     if (ts->ops->view) {
849d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
850d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
851d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
852d52bd9f3SBarry Smith     }
8530f5bd95cSBarry Smith   } else if (isstring) {
854a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
855b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
856d763cef2SBarry Smith   }
857b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
858089b2837SJed Brown   ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
859b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
860d763cef2SBarry Smith   PetscFunctionReturn(0);
861d763cef2SBarry Smith }
862d763cef2SBarry Smith 
863d763cef2SBarry Smith 
8644a2ae208SSatish Balay #undef __FUNCT__
8654a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
866b07ff414SBarry Smith /*@
867d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
868d763cef2SBarry Smith    the timesteppers.
869d763cef2SBarry Smith 
8703f9fe445SBarry Smith    Logically Collective on TS
871d763cef2SBarry Smith 
872d763cef2SBarry Smith    Input Parameters:
873d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
874d763cef2SBarry Smith -  usrP - optional user context
875d763cef2SBarry Smith 
876d763cef2SBarry Smith    Level: intermediate
877d763cef2SBarry Smith 
878d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
879d763cef2SBarry Smith 
880d763cef2SBarry Smith .seealso: TSGetApplicationContext()
881d763cef2SBarry Smith @*/
8827087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
883d763cef2SBarry Smith {
884d763cef2SBarry Smith   PetscFunctionBegin;
8850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
886d763cef2SBarry Smith   ts->user = usrP;
887d763cef2SBarry Smith   PetscFunctionReturn(0);
888d763cef2SBarry Smith }
889d763cef2SBarry Smith 
8904a2ae208SSatish Balay #undef __FUNCT__
8914a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
892b07ff414SBarry Smith /*@
893d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
894d763cef2SBarry Smith     timestepper.
895d763cef2SBarry Smith 
896d763cef2SBarry Smith     Not Collective
897d763cef2SBarry Smith 
898d763cef2SBarry Smith     Input Parameter:
899d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
900d763cef2SBarry Smith 
901d763cef2SBarry Smith     Output Parameter:
902d763cef2SBarry Smith .   usrP - user context
903d763cef2SBarry Smith 
904d763cef2SBarry Smith     Level: intermediate
905d763cef2SBarry Smith 
906d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
907d763cef2SBarry Smith 
908d763cef2SBarry Smith .seealso: TSSetApplicationContext()
909d763cef2SBarry Smith @*/
910e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
911d763cef2SBarry Smith {
912d763cef2SBarry Smith   PetscFunctionBegin;
9130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
914e71120c6SJed Brown   *(void**)usrP = ts->user;
915d763cef2SBarry Smith   PetscFunctionReturn(0);
916d763cef2SBarry Smith }
917d763cef2SBarry Smith 
9184a2ae208SSatish Balay #undef __FUNCT__
9194a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
920d763cef2SBarry Smith /*@
921d763cef2SBarry Smith    TSGetTimeStepNumber - Gets the current number of timesteps.
922d763cef2SBarry Smith 
923d763cef2SBarry Smith    Not Collective
924d763cef2SBarry Smith 
925d763cef2SBarry Smith    Input Parameter:
926d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
927d763cef2SBarry Smith 
928d763cef2SBarry Smith    Output Parameter:
929d763cef2SBarry Smith .  iter - number steps so far
930d763cef2SBarry Smith 
931d763cef2SBarry Smith    Level: intermediate
932d763cef2SBarry Smith 
933d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
934d763cef2SBarry Smith @*/
9357087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
936d763cef2SBarry Smith {
937d763cef2SBarry Smith   PetscFunctionBegin;
9380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9394482741eSBarry Smith   PetscValidIntPointer(iter,2);
940d763cef2SBarry Smith   *iter = ts->steps;
941d763cef2SBarry Smith   PetscFunctionReturn(0);
942d763cef2SBarry Smith }
943d763cef2SBarry Smith 
9444a2ae208SSatish Balay #undef __FUNCT__
9454a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
946d763cef2SBarry Smith /*@
947d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
948d763cef2SBarry Smith    as well as the initial time.
949d763cef2SBarry Smith 
9503f9fe445SBarry Smith    Logically Collective on TS
951d763cef2SBarry Smith 
952d763cef2SBarry Smith    Input Parameters:
953d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
954d763cef2SBarry Smith .  initial_time - the initial time
955d763cef2SBarry Smith -  time_step - the size of the timestep
956d763cef2SBarry Smith 
957d763cef2SBarry Smith    Level: intermediate
958d763cef2SBarry Smith 
959d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
960d763cef2SBarry Smith 
961d763cef2SBarry Smith .keywords: TS, set, initial, timestep
962d763cef2SBarry Smith @*/
9637087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
964d763cef2SBarry Smith {
965e144a568SJed Brown   PetscErrorCode ierr;
966e144a568SJed Brown 
967d763cef2SBarry Smith   PetscFunctionBegin;
9680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
969e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
970d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
971d763cef2SBarry Smith   PetscFunctionReturn(0);
972d763cef2SBarry Smith }
973d763cef2SBarry Smith 
9744a2ae208SSatish Balay #undef __FUNCT__
9754a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
976d763cef2SBarry Smith /*@
977d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
978d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
979d763cef2SBarry Smith 
9803f9fe445SBarry Smith    Logically Collective on TS
981d763cef2SBarry Smith 
982d763cef2SBarry Smith    Input Parameters:
983d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
984d763cef2SBarry Smith -  time_step - the size of the timestep
985d763cef2SBarry Smith 
986d763cef2SBarry Smith    Level: intermediate
987d763cef2SBarry Smith 
988d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
989d763cef2SBarry Smith 
990d763cef2SBarry Smith .keywords: TS, set, timestep
991d763cef2SBarry Smith @*/
9927087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
993d763cef2SBarry Smith {
994d763cef2SBarry Smith   PetscFunctionBegin;
9950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
996c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
997d763cef2SBarry Smith   ts->time_step = time_step;
998d763cef2SBarry Smith   PetscFunctionReturn(0);
999d763cef2SBarry Smith }
1000d763cef2SBarry Smith 
10014a2ae208SSatish Balay #undef __FUNCT__
1002a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1003a43b19c4SJed Brown /*@
1004a43b19c4SJed Brown    TSSetExactFinalTime - Determines whether to interpolate solution to the
1005a43b19c4SJed Brown       exact final time requested by the user or just returns it at the final time
1006a43b19c4SJed Brown       it computed.
1007a43b19c4SJed Brown 
1008a43b19c4SJed Brown   Logically Collective on TS
1009a43b19c4SJed Brown 
1010a43b19c4SJed Brown    Input Parameter:
1011a43b19c4SJed Brown +   ts - the time-step context
1012a43b19c4SJed Brown -   ft - PETSC_TRUE if interpolates, else PETSC_FALSE
1013a43b19c4SJed Brown 
1014a43b19c4SJed Brown    Level: beginner
1015a43b19c4SJed Brown 
1016a43b19c4SJed Brown .seealso: TSSetDuration()
1017a43b19c4SJed Brown @*/
1018a43b19c4SJed Brown PetscErrorCode  TSSetExactFinalTime(TS ts,PetscBool flg)
1019a43b19c4SJed Brown {
1020a43b19c4SJed Brown 
1021a43b19c4SJed Brown   PetscFunctionBegin;
1022a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1023d8cd7023SBarry Smith   PetscValidLogicalCollectiveBool(ts,flg,2);
1024a43b19c4SJed Brown   ts->exact_final_time = flg;
1025a43b19c4SJed Brown   PetscFunctionReturn(0);
1026a43b19c4SJed Brown }
1027a43b19c4SJed Brown 
1028a43b19c4SJed Brown #undef __FUNCT__
10294a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1030d763cef2SBarry Smith /*@
1031d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1032d763cef2SBarry Smith 
1033d763cef2SBarry Smith    Not Collective
1034d763cef2SBarry Smith 
1035d763cef2SBarry Smith    Input Parameter:
1036d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1037d763cef2SBarry Smith 
1038d763cef2SBarry Smith    Output Parameter:
1039d763cef2SBarry Smith .  dt - the current timestep size
1040d763cef2SBarry Smith 
1041d763cef2SBarry Smith    Level: intermediate
1042d763cef2SBarry Smith 
1043d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1044d763cef2SBarry Smith 
1045d763cef2SBarry Smith .keywords: TS, get, timestep
1046d763cef2SBarry Smith @*/
10477087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1048d763cef2SBarry Smith {
1049d763cef2SBarry Smith   PetscFunctionBegin;
10500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10514482741eSBarry Smith   PetscValidDoublePointer(dt,2);
1052d763cef2SBarry Smith   *dt = ts->time_step;
1053d763cef2SBarry Smith   PetscFunctionReturn(0);
1054d763cef2SBarry Smith }
1055d763cef2SBarry Smith 
10564a2ae208SSatish Balay #undef __FUNCT__
10574a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1058d8e5e3e6SSatish Balay /*@
1059d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1060d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1061d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1062d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1063d763cef2SBarry Smith 
1064d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1065d763cef2SBarry Smith 
1066d763cef2SBarry Smith    Input Parameter:
1067d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1068d763cef2SBarry Smith 
1069d763cef2SBarry Smith    Output Parameter:
1070d763cef2SBarry Smith .  v - the vector containing the solution
1071d763cef2SBarry Smith 
1072d763cef2SBarry Smith    Level: intermediate
1073d763cef2SBarry Smith 
1074d763cef2SBarry Smith .seealso: TSGetTimeStep()
1075d763cef2SBarry Smith 
1076d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1077d763cef2SBarry Smith @*/
10787087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1079d763cef2SBarry Smith {
1080d763cef2SBarry Smith   PetscFunctionBegin;
10810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10824482741eSBarry Smith   PetscValidPointer(v,2);
10838737fe31SLisandro Dalcin   *v = ts->vec_sol;
1084d763cef2SBarry Smith   PetscFunctionReturn(0);
1085d763cef2SBarry Smith }
1086d763cef2SBarry Smith 
1087bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
10884a2ae208SSatish Balay #undef __FUNCT__
1089bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1090d8e5e3e6SSatish Balay /*@
1091bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1092d763cef2SBarry Smith 
1093bdad233fSMatthew Knepley   Not collective
1094d763cef2SBarry Smith 
1095bdad233fSMatthew Knepley   Input Parameters:
1096bdad233fSMatthew Knepley + ts   - The TS
1097bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1098d763cef2SBarry Smith .vb
1099d763cef2SBarry Smith          U_t = A U
1100d763cef2SBarry Smith          U_t = A(t) U
1101d763cef2SBarry Smith          U_t = F(t,U)
1102d763cef2SBarry Smith .ve
1103d763cef2SBarry Smith 
1104d763cef2SBarry Smith    Level: beginner
1105d763cef2SBarry Smith 
1106bdad233fSMatthew Knepley .keywords: TS, problem type
1107bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1108d763cef2SBarry Smith @*/
11097087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1110a7cc72afSBarry Smith {
11119e2a6581SJed Brown   PetscErrorCode ierr;
11129e2a6581SJed Brown 
1113d763cef2SBarry Smith   PetscFunctionBegin;
11140700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1115bdad233fSMatthew Knepley   ts->problem_type = type;
11169e2a6581SJed Brown   if (type == TS_LINEAR) {
11179e2a6581SJed Brown     SNES snes;
11189e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11199e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
11209e2a6581SJed Brown   }
1121d763cef2SBarry Smith   PetscFunctionReturn(0);
1122d763cef2SBarry Smith }
1123d763cef2SBarry Smith 
1124bdad233fSMatthew Knepley #undef __FUNCT__
1125bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1126bdad233fSMatthew Knepley /*@C
1127bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1128bdad233fSMatthew Knepley 
1129bdad233fSMatthew Knepley   Not collective
1130bdad233fSMatthew Knepley 
1131bdad233fSMatthew Knepley   Input Parameter:
1132bdad233fSMatthew Knepley . ts   - The TS
1133bdad233fSMatthew Knepley 
1134bdad233fSMatthew Knepley   Output Parameter:
1135bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1136bdad233fSMatthew Knepley .vb
1137089b2837SJed Brown          M U_t = A U
1138089b2837SJed Brown          M(t) U_t = A(t) U
1139bdad233fSMatthew Knepley          U_t = F(t,U)
1140bdad233fSMatthew Knepley .ve
1141bdad233fSMatthew Knepley 
1142bdad233fSMatthew Knepley    Level: beginner
1143bdad233fSMatthew Knepley 
1144bdad233fSMatthew Knepley .keywords: TS, problem type
1145bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1146bdad233fSMatthew Knepley @*/
11477087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1148a7cc72afSBarry Smith {
1149bdad233fSMatthew Knepley   PetscFunctionBegin;
11500700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
11514482741eSBarry Smith   PetscValidIntPointer(type,2);
1152bdad233fSMatthew Knepley   *type = ts->problem_type;
1153bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1154bdad233fSMatthew Knepley }
1155d763cef2SBarry Smith 
11564a2ae208SSatish Balay #undef __FUNCT__
11574a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1158d763cef2SBarry Smith /*@
1159d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1160d763cef2SBarry Smith    of a timestepper.
1161d763cef2SBarry Smith 
1162d763cef2SBarry Smith    Collective on TS
1163d763cef2SBarry Smith 
1164d763cef2SBarry Smith    Input Parameter:
1165d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1166d763cef2SBarry Smith 
1167d763cef2SBarry Smith    Notes:
1168d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1169d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1170d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1171d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1172d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1173d763cef2SBarry Smith 
1174d763cef2SBarry Smith    Level: advanced
1175d763cef2SBarry Smith 
1176d763cef2SBarry Smith .keywords: TS, timestep, setup
1177d763cef2SBarry Smith 
1178d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1179d763cef2SBarry Smith @*/
11807087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1181d763cef2SBarry Smith {
1182dfbe8321SBarry Smith   PetscErrorCode ierr;
1183d763cef2SBarry Smith 
1184d763cef2SBarry Smith   PetscFunctionBegin;
11850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1186277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1187277b19d0SLisandro Dalcin 
11887adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
11899596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1190d763cef2SBarry Smith   }
1191a43b19c4SJed Brown   if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE;
1192277b19d0SLisandro Dalcin 
1193277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1194277b19d0SLisandro Dalcin 
1195277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1196000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1197277b19d0SLisandro Dalcin   }
1198277b19d0SLisandro Dalcin 
1199277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1200277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1201277b19d0SLisandro Dalcin }
1202277b19d0SLisandro Dalcin 
1203277b19d0SLisandro Dalcin #undef __FUNCT__
1204277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1205277b19d0SLisandro Dalcin /*@
1206277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1207277b19d0SLisandro Dalcin 
1208277b19d0SLisandro Dalcin    Collective on TS
1209277b19d0SLisandro Dalcin 
1210277b19d0SLisandro Dalcin    Input Parameter:
1211277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1212277b19d0SLisandro Dalcin 
1213277b19d0SLisandro Dalcin    Level: beginner
1214277b19d0SLisandro Dalcin 
1215277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1216277b19d0SLisandro Dalcin 
1217277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1218277b19d0SLisandro Dalcin @*/
1219277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1220277b19d0SLisandro Dalcin {
1221277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1222277b19d0SLisandro Dalcin 
1223277b19d0SLisandro Dalcin   PetscFunctionBegin;
1224277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1225277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1226277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1227277b19d0SLisandro Dalcin   }
1228277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
12294e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
12304e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1231214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
12326bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
123338637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1234277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1235d763cef2SBarry Smith   PetscFunctionReturn(0);
1236d763cef2SBarry Smith }
1237d763cef2SBarry Smith 
12384a2ae208SSatish Balay #undef __FUNCT__
12394a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1240d8e5e3e6SSatish Balay /*@
1241d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1242d763cef2SBarry Smith    with TSCreate().
1243d763cef2SBarry Smith 
1244d763cef2SBarry Smith    Collective on TS
1245d763cef2SBarry Smith 
1246d763cef2SBarry Smith    Input Parameter:
1247d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1248d763cef2SBarry Smith 
1249d763cef2SBarry Smith    Level: beginner
1250d763cef2SBarry Smith 
1251d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1252d763cef2SBarry Smith 
1253d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1254d763cef2SBarry Smith @*/
12556bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1256d763cef2SBarry Smith {
12576849ba73SBarry Smith   PetscErrorCode ierr;
1258d763cef2SBarry Smith 
1259d763cef2SBarry Smith   PetscFunctionBegin;
12606bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
12616bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
12626bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1263d763cef2SBarry Smith 
12646bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1265277b19d0SLisandro Dalcin 
1266be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
12676bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
12686bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
12696d4c513bSLisandro Dalcin 
12706bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
12716bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
12726bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
12736d4c513bSLisandro Dalcin 
12743b5f76d0SSean Farley   ierr = PetscFree((*ts)->userops);
12753b5f76d0SSean Farley 
1276a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1277d763cef2SBarry Smith   PetscFunctionReturn(0);
1278d763cef2SBarry Smith }
1279d763cef2SBarry Smith 
12804a2ae208SSatish Balay #undef __FUNCT__
12814a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1282d8e5e3e6SSatish Balay /*@
1283d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1284d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1285d763cef2SBarry Smith 
1286d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1287d763cef2SBarry Smith 
1288d763cef2SBarry Smith    Input Parameter:
1289d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1290d763cef2SBarry Smith 
1291d763cef2SBarry Smith    Output Parameter:
1292d763cef2SBarry Smith .  snes - the nonlinear solver context
1293d763cef2SBarry Smith 
1294d763cef2SBarry Smith    Notes:
1295d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1296d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
129794b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1298d763cef2SBarry Smith 
1299d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1300d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1301d763cef2SBarry Smith 
1302d763cef2SBarry Smith    Level: beginner
1303d763cef2SBarry Smith 
1304d763cef2SBarry Smith .keywords: timestep, get, SNES
1305d763cef2SBarry Smith @*/
13067087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1307d763cef2SBarry Smith {
1308d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1309d372ba47SLisandro Dalcin 
1310d763cef2SBarry Smith   PetscFunctionBegin;
13110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13124482741eSBarry Smith   PetscValidPointer(snes,2);
1313d372ba47SLisandro Dalcin   if (!ts->snes) {
1314d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
1315d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1316d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
13179e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
13189e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
13199e2a6581SJed Brown     }
1320d372ba47SLisandro Dalcin   }
1321d763cef2SBarry Smith   *snes = ts->snes;
1322d763cef2SBarry Smith   PetscFunctionReturn(0);
1323d763cef2SBarry Smith }
1324d763cef2SBarry Smith 
13254a2ae208SSatish Balay #undef __FUNCT__
132694b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1327d8e5e3e6SSatish Balay /*@
132894b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1329d763cef2SBarry Smith    a TS (timestepper) context.
1330d763cef2SBarry Smith 
133194b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1332d763cef2SBarry Smith 
1333d763cef2SBarry Smith    Input Parameter:
1334d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1335d763cef2SBarry Smith 
1336d763cef2SBarry Smith    Output Parameter:
133794b7f48cSBarry Smith .  ksp - the nonlinear solver context
1338d763cef2SBarry Smith 
1339d763cef2SBarry Smith    Notes:
134094b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1341d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1342d763cef2SBarry Smith    KSP and PC contexts as well.
1343d763cef2SBarry Smith 
134494b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
134594b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1346d763cef2SBarry Smith 
1347d763cef2SBarry Smith    Level: beginner
1348d763cef2SBarry Smith 
134994b7f48cSBarry Smith .keywords: timestep, get, KSP
1350d763cef2SBarry Smith @*/
13517087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1352d763cef2SBarry Smith {
1353d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1354089b2837SJed Brown   SNES           snes;
1355d372ba47SLisandro Dalcin 
1356d763cef2SBarry Smith   PetscFunctionBegin;
13570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13584482741eSBarry Smith   PetscValidPointer(ksp,2);
135917186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1360e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1361089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1362089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1363d763cef2SBarry Smith   PetscFunctionReturn(0);
1364d763cef2SBarry Smith }
1365d763cef2SBarry Smith 
1366d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1367d763cef2SBarry Smith 
13684a2ae208SSatish Balay #undef __FUNCT__
1369adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1370adb62b0dSMatthew Knepley /*@
1371adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1372adb62b0dSMatthew Knepley    maximum time for iteration.
1373adb62b0dSMatthew Knepley 
13743f9fe445SBarry Smith    Not Collective
1375adb62b0dSMatthew Knepley 
1376adb62b0dSMatthew Knepley    Input Parameters:
1377adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1378adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1379adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1380adb62b0dSMatthew Knepley 
1381adb62b0dSMatthew Knepley    Level: intermediate
1382adb62b0dSMatthew Knepley 
1383adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1384adb62b0dSMatthew Knepley @*/
13857087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1386adb62b0dSMatthew Knepley {
1387adb62b0dSMatthew Knepley   PetscFunctionBegin;
13880700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1389abc0a331SBarry Smith   if (maxsteps) {
13904482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1391adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1392adb62b0dSMatthew Knepley   }
1393abc0a331SBarry Smith   if (maxtime) {
13944482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1395adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1396adb62b0dSMatthew Knepley   }
1397adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1398adb62b0dSMatthew Knepley }
1399adb62b0dSMatthew Knepley 
1400adb62b0dSMatthew Knepley #undef __FUNCT__
14014a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1402d763cef2SBarry Smith /*@
1403d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1404d763cef2SBarry Smith    maximum time for iteration.
1405d763cef2SBarry Smith 
14063f9fe445SBarry Smith    Logically Collective on TS
1407d763cef2SBarry Smith 
1408d763cef2SBarry Smith    Input Parameters:
1409d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1410d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1411d763cef2SBarry Smith -  maxtime - final time to iterate to
1412d763cef2SBarry Smith 
1413d763cef2SBarry Smith    Options Database Keys:
1414d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
1415d763cef2SBarry Smith .  -ts_max_time <maxtime> - Sets maxtime
1416d763cef2SBarry Smith 
1417d763cef2SBarry Smith    Notes:
1418d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1419d763cef2SBarry Smith 
1420d763cef2SBarry Smith    Level: intermediate
1421d763cef2SBarry Smith 
1422d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1423a43b19c4SJed Brown 
1424a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1425d763cef2SBarry Smith @*/
14267087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1427d763cef2SBarry Smith {
1428d763cef2SBarry Smith   PetscFunctionBegin;
14290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1430c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1431c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
143239b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
143339b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1434d763cef2SBarry Smith   PetscFunctionReturn(0);
1435d763cef2SBarry Smith }
1436d763cef2SBarry Smith 
14374a2ae208SSatish Balay #undef __FUNCT__
14384a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1439d763cef2SBarry Smith /*@
1440d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1441d763cef2SBarry Smith    for use by the TS routines.
1442d763cef2SBarry Smith 
14433f9fe445SBarry Smith    Logically Collective on TS and Vec
1444d763cef2SBarry Smith 
1445d763cef2SBarry Smith    Input Parameters:
1446d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1447d763cef2SBarry Smith -  x - the solution vector
1448d763cef2SBarry Smith 
1449d763cef2SBarry Smith    Level: beginner
1450d763cef2SBarry Smith 
1451d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1452d763cef2SBarry Smith @*/
14537087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1454d763cef2SBarry Smith {
14558737fe31SLisandro Dalcin   PetscErrorCode ierr;
14568737fe31SLisandro Dalcin 
1457d763cef2SBarry Smith   PetscFunctionBegin;
14580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14590700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
14608737fe31SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
14616bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
14628737fe31SLisandro Dalcin   ts->vec_sol = x;
1463d763cef2SBarry Smith   PetscFunctionReturn(0);
1464d763cef2SBarry Smith }
1465d763cef2SBarry Smith 
1466e74ef692SMatthew Knepley #undef __FUNCT__
1467e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1468ac226902SBarry Smith /*@C
1469000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
14703f2090d5SJed Brown   called once at the beginning of each time step.
1471000e7ae3SMatthew Knepley 
14723f9fe445SBarry Smith   Logically Collective on TS
1473000e7ae3SMatthew Knepley 
1474000e7ae3SMatthew Knepley   Input Parameters:
1475000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1476000e7ae3SMatthew Knepley - func - The function
1477000e7ae3SMatthew Knepley 
1478000e7ae3SMatthew Knepley   Calling sequence of func:
1479000e7ae3SMatthew Knepley . func (TS ts);
1480000e7ae3SMatthew Knepley 
1481000e7ae3SMatthew Knepley   Level: intermediate
1482000e7ae3SMatthew Knepley 
1483000e7ae3SMatthew Knepley .keywords: TS, timestep
1484000e7ae3SMatthew Knepley @*/
14857087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1486000e7ae3SMatthew Knepley {
1487000e7ae3SMatthew Knepley   PetscFunctionBegin;
14880700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1489000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1490000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1491000e7ae3SMatthew Knepley }
1492000e7ae3SMatthew Knepley 
1493e74ef692SMatthew Knepley #undef __FUNCT__
14943f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
149509ee8438SJed Brown /*@
14963f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
14973f2090d5SJed Brown 
14983f2090d5SJed Brown   Collective on TS
14993f2090d5SJed Brown 
15003f2090d5SJed Brown   Input Parameters:
15013f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
15023f2090d5SJed Brown 
15033f2090d5SJed Brown   Notes:
15043f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
15053f2090d5SJed Brown   so most users would not generally call this routine themselves.
15063f2090d5SJed Brown 
15073f2090d5SJed Brown   Level: developer
15083f2090d5SJed Brown 
15093f2090d5SJed Brown .keywords: TS, timestep
15103f2090d5SJed Brown @*/
15117087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
15123f2090d5SJed Brown {
15133f2090d5SJed Brown   PetscErrorCode ierr;
15143f2090d5SJed Brown 
15153f2090d5SJed Brown   PetscFunctionBegin;
15160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
151772ac3e02SJed Brown   if (ts->ops->prestep) {
15183f2090d5SJed Brown     PetscStackPush("TS PreStep function");
15193f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
15203f2090d5SJed Brown     PetscStackPop;
1521312ce896SJed Brown   }
15223f2090d5SJed Brown   PetscFunctionReturn(0);
15233f2090d5SJed Brown }
15243f2090d5SJed Brown 
15253f2090d5SJed Brown #undef __FUNCT__
1526e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1527ac226902SBarry Smith /*@C
1528000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
15293f2090d5SJed Brown   called once at the end of each time step.
1530000e7ae3SMatthew Knepley 
15313f9fe445SBarry Smith   Logically Collective on TS
1532000e7ae3SMatthew Knepley 
1533000e7ae3SMatthew Knepley   Input Parameters:
1534000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1535000e7ae3SMatthew Knepley - func - The function
1536000e7ae3SMatthew Knepley 
1537000e7ae3SMatthew Knepley   Calling sequence of func:
1538000e7ae3SMatthew Knepley . func (TS ts);
1539000e7ae3SMatthew Knepley 
1540000e7ae3SMatthew Knepley   Level: intermediate
1541000e7ae3SMatthew Knepley 
1542000e7ae3SMatthew Knepley .keywords: TS, timestep
1543000e7ae3SMatthew Knepley @*/
15447087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1545000e7ae3SMatthew Knepley {
1546000e7ae3SMatthew Knepley   PetscFunctionBegin;
15470700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1548000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1549000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1550000e7ae3SMatthew Knepley }
1551000e7ae3SMatthew Knepley 
1552e74ef692SMatthew Knepley #undef __FUNCT__
15533f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
155409ee8438SJed Brown /*@
15553f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
15563f2090d5SJed Brown 
15573f2090d5SJed Brown   Collective on TS
15583f2090d5SJed Brown 
15593f2090d5SJed Brown   Input Parameters:
15603f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
15613f2090d5SJed Brown 
15623f2090d5SJed Brown   Notes:
15633f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
15643f2090d5SJed Brown   so most users would not generally call this routine themselves.
15653f2090d5SJed Brown 
15663f2090d5SJed Brown   Level: developer
15673f2090d5SJed Brown 
15683f2090d5SJed Brown .keywords: TS, timestep
15693f2090d5SJed Brown @*/
15707087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
15713f2090d5SJed Brown {
15723f2090d5SJed Brown   PetscErrorCode ierr;
15733f2090d5SJed Brown 
15743f2090d5SJed Brown   PetscFunctionBegin;
15750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
157672ac3e02SJed Brown   if (ts->ops->poststep) {
15773f2090d5SJed Brown     PetscStackPush("TS PostStep function");
15783f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
15793f2090d5SJed Brown     PetscStackPop;
158072ac3e02SJed Brown   }
15813f2090d5SJed Brown   PetscFunctionReturn(0);
15823f2090d5SJed Brown }
15833f2090d5SJed Brown 
1584d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1585d763cef2SBarry Smith 
15864a2ae208SSatish Balay #undef __FUNCT__
1587a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1588d763cef2SBarry Smith /*@C
1589a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1590d763cef2SBarry Smith    timestep to display the iteration's  progress.
1591d763cef2SBarry Smith 
15923f9fe445SBarry Smith    Logically Collective on TS
1593d763cef2SBarry Smith 
1594d763cef2SBarry Smith    Input Parameters:
1595d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1596e213d8f1SJed Brown .  monitor - monitoring routine
1597329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1598b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1599b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1600b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1601d763cef2SBarry Smith 
1602e213d8f1SJed Brown    Calling sequence of monitor:
1603e213d8f1SJed Brown $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1604d763cef2SBarry Smith 
1605d763cef2SBarry Smith +    ts - the TS context
1606d763cef2SBarry Smith .    steps - iteration number
16071f06c33eSBarry Smith .    time - current time
1608d763cef2SBarry Smith .    x - current iterate
1609d763cef2SBarry Smith -    mctx - [optional] monitoring context
1610d763cef2SBarry Smith 
1611d763cef2SBarry Smith    Notes:
1612d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1613d763cef2SBarry Smith    already has been loaded.
1614d763cef2SBarry Smith 
1615025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1616025f1a04SBarry Smith 
1617d763cef2SBarry Smith    Level: intermediate
1618d763cef2SBarry Smith 
1619d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1620d763cef2SBarry Smith 
1621a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1622d763cef2SBarry Smith @*/
1623c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1624d763cef2SBarry Smith {
1625d763cef2SBarry Smith   PetscFunctionBegin;
16260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
162717186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1628d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1629329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1630d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1631d763cef2SBarry Smith   PetscFunctionReturn(0);
1632d763cef2SBarry Smith }
1633d763cef2SBarry Smith 
16344a2ae208SSatish Balay #undef __FUNCT__
1635a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1636d763cef2SBarry Smith /*@C
1637a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1638d763cef2SBarry Smith 
16393f9fe445SBarry Smith    Logically Collective on TS
1640d763cef2SBarry Smith 
1641d763cef2SBarry Smith    Input Parameters:
1642d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1643d763cef2SBarry Smith 
1644d763cef2SBarry Smith    Notes:
1645d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1646d763cef2SBarry Smith 
1647d763cef2SBarry Smith    Level: intermediate
1648d763cef2SBarry Smith 
1649d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1650d763cef2SBarry Smith 
1651a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1652d763cef2SBarry Smith @*/
16537087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1654d763cef2SBarry Smith {
1655d952e501SBarry Smith   PetscErrorCode ierr;
1656d952e501SBarry Smith   PetscInt       i;
1657d952e501SBarry Smith 
1658d763cef2SBarry Smith   PetscFunctionBegin;
16590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1660d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
1661d952e501SBarry Smith     if (ts->mdestroy[i]) {
16623c4aec1bSBarry Smith       ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
1663d952e501SBarry Smith     }
1664d952e501SBarry Smith   }
1665d763cef2SBarry Smith   ts->numbermonitors = 0;
1666d763cef2SBarry Smith   PetscFunctionReturn(0);
1667d763cef2SBarry Smith }
1668d763cef2SBarry Smith 
16694a2ae208SSatish Balay #undef __FUNCT__
1670a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
1671d8e5e3e6SSatish Balay /*@
1672a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
16735516499fSSatish Balay 
16745516499fSSatish Balay    Level: intermediate
167541251cbbSSatish Balay 
16765516499fSSatish Balay .keywords: TS, set, monitor
16775516499fSSatish Balay 
167841251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
167941251cbbSSatish Balay @*/
1680649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
1681d763cef2SBarry Smith {
1682dfbe8321SBarry Smith   PetscErrorCode ierr;
1683649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
1684d132466eSBarry Smith 
1685d763cef2SBarry Smith   PetscFunctionBegin;
1686649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1687971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
1688649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1689d763cef2SBarry Smith   PetscFunctionReturn(0);
1690d763cef2SBarry Smith }
1691d763cef2SBarry Smith 
16924a2ae208SSatish Balay #undef __FUNCT__
1693cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
1694cd652676SJed Brown /*@
1695cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
1696cd652676SJed Brown 
1697cd652676SJed Brown    Logically Collective on TS
1698cd652676SJed Brown 
1699cd652676SJed Brown    Input Argument:
1700cd652676SJed Brown .  ts - time stepping context
1701cd652676SJed Brown 
1702cd652676SJed Brown    Output Argument:
1703cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
1704cd652676SJed Brown 
1705cd652676SJed Brown    Level: intermediate
1706cd652676SJed Brown 
1707cd652676SJed Brown .keywords: TS, set
1708cd652676SJed Brown 
1709cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
1710cd652676SJed Brown @*/
1711cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
1712cd652676SJed Brown {
1713cd652676SJed Brown 
1714cd652676SJed Brown   PetscFunctionBegin;
1715cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1716cd652676SJed Brown   ts->retain_stages = flg;
1717cd652676SJed Brown   PetscFunctionReturn(0);
1718cd652676SJed Brown }
1719cd652676SJed Brown 
1720cd652676SJed Brown #undef __FUNCT__
1721cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
1722cd652676SJed Brown /*@
1723cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
1724cd652676SJed Brown 
1725cd652676SJed Brown    Collective on TS
1726cd652676SJed Brown 
1727cd652676SJed Brown    Input Argument:
1728cd652676SJed Brown +  ts - time stepping context
1729cd652676SJed Brown -  t - time to interpolate to
1730cd652676SJed Brown 
1731cd652676SJed Brown    Output Argument:
1732cd652676SJed Brown .  X - state at given time
1733cd652676SJed Brown 
1734cd652676SJed Brown    Notes:
1735cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
1736cd652676SJed Brown 
1737cd652676SJed Brown    Level: intermediate
1738cd652676SJed Brown 
1739cd652676SJed Brown    Developer Notes:
1740cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
1741cd652676SJed Brown 
1742cd652676SJed Brown .keywords: TS, set
1743cd652676SJed Brown 
1744cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
1745cd652676SJed Brown @*/
1746cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X)
1747cd652676SJed Brown {
1748cd652676SJed Brown   PetscErrorCode ierr;
1749cd652676SJed Brown 
1750cd652676SJed Brown   PetscFunctionBegin;
1751cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1752d8cd7023SBarry Smith   if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step_prev,ts->ptime);
1753cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
1754cd652676SJed Brown   ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr);
1755cd652676SJed Brown   PetscFunctionReturn(0);
1756cd652676SJed Brown }
1757cd652676SJed Brown 
1758cd652676SJed Brown #undef __FUNCT__
17594a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1760d763cef2SBarry Smith /*@
17616d9e5789SSean Farley    TSStep - Steps one time step
1762d763cef2SBarry Smith 
1763d763cef2SBarry Smith    Collective on TS
1764d763cef2SBarry Smith 
1765d763cef2SBarry Smith    Input Parameter:
1766d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1767d763cef2SBarry Smith 
17686d9e5789SSean Farley    Level: intermediate
1769d763cef2SBarry Smith 
1770d763cef2SBarry Smith .keywords: TS, timestep, solve
1771d763cef2SBarry Smith 
17726d9e5789SSean Farley .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve()
1773d763cef2SBarry Smith @*/
1774193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
1775d763cef2SBarry Smith {
1776362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
1777dfbe8321SBarry Smith   PetscErrorCode ierr;
1778d763cef2SBarry Smith 
1779d763cef2SBarry Smith   PetscFunctionBegin;
17800700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1781d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
1782d405a339SMatthew Knepley 
1783362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
1784362cd11cSLisandro Dalcin 
1785362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
1786d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
1787193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
1788d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
1789362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
1790362cd11cSLisandro Dalcin 
1791362cd11cSLisandro Dalcin   if (ts->reason < 0) {
1792f1b97656SJed Brown     if (ts->errorifstepfailed) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
1793362cd11cSLisandro Dalcin   } else if (!ts->reason) {
1794362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
1795362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
1796362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
1797362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
1798362cd11cSLisandro Dalcin   }
1799362cd11cSLisandro Dalcin 
1800d763cef2SBarry Smith   PetscFunctionReturn(0);
1801d763cef2SBarry Smith }
1802d763cef2SBarry Smith 
18034a2ae208SSatish Balay #undef __FUNCT__
18046a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
18056a4d4014SLisandro Dalcin /*@
18066a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
18076a4d4014SLisandro Dalcin 
18086a4d4014SLisandro Dalcin    Collective on TS
18096a4d4014SLisandro Dalcin 
18106a4d4014SLisandro Dalcin    Input Parameter:
18116a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1812362cd11cSLisandro Dalcin -  x - the solution vector
18136a4d4014SLisandro Dalcin 
18145a3a76d0SJed Brown    Output Parameter:
18155a3a76d0SJed Brown .  ftime - time of the state vector x upon completion
18165a3a76d0SJed Brown 
18176a4d4014SLisandro Dalcin    Level: beginner
18186a4d4014SLisandro Dalcin 
18195a3a76d0SJed Brown    Notes:
18205a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
18215a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
18225a3a76d0SJed Brown    stepped over the final time.
18235a3a76d0SJed Brown 
18246a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
18256a4d4014SLisandro Dalcin 
18266a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
18276a4d4014SLisandro Dalcin @*/
18285a3a76d0SJed Brown PetscErrorCode TSSolve(TS ts,Vec x,PetscReal *ftime)
18296a4d4014SLisandro Dalcin {
18304d7d938eSLisandro Dalcin   PetscBool      flg;
18314d7d938eSLisandro Dalcin   char           filename[PETSC_MAX_PATH_LEN];
18324d7d938eSLisandro Dalcin   PetscViewer    viewer;
18336a4d4014SLisandro Dalcin   PetscErrorCode ierr;
1834f22f69f0SBarry Smith 
18356a4d4014SLisandro Dalcin   PetscFunctionBegin;
18360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1837193ac0bcSJed Brown   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
18385a3a76d0SJed Brown   if (ts->exact_final_time) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
18395a3a76d0SJed Brown     if (!ts->vec_sol || x == ts->vec_sol) {
18405a3a76d0SJed Brown       Vec y;
18415a3a76d0SJed Brown       ierr = VecDuplicate(x,&y);CHKERRQ(ierr);
18425a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
18435a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
18445a3a76d0SJed Brown     }
1845362cd11cSLisandro Dalcin     ierr = VecCopy(x,ts->vec_sol);CHKERRQ(ierr);
18465a3a76d0SJed Brown   } else {
1847193ac0bcSJed Brown     ierr = TSSetSolution(ts,x);CHKERRQ(ierr);
18485a3a76d0SJed Brown   }
1849b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
18506a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
1851193ac0bcSJed Brown   ts->steps = 0;
1852193ac0bcSJed Brown   ts->linear_its = 0;
1853193ac0bcSJed Brown   ts->nonlinear_its = 0;
1854c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
1855c610991cSLisandro Dalcin   ts->reject = 0;
1856193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
1857193ac0bcSJed Brown 
1858193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
1859193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
18605a3a76d0SJed Brown     ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
1861a5b82c69SSean Farley     if (ftime) *ftime = ts->ptime;
1862193ac0bcSJed Brown   } else {
18636a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
1864362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
1865362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
1866362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
1867362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
1868362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
1869e1a7a14fSJed Brown     while (!ts->reason) {
1870193ac0bcSJed Brown       ierr = TSPreStep(ts);CHKERRQ(ierr);
1871193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
1872193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
1873193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
1874193ac0bcSJed Brown     }
1875362cd11cSLisandro Dalcin     if (ts->exact_final_time && ts->ptime > ts->max_time) {
1876a43b19c4SJed Brown       ierr = TSInterpolate(ts,ts->max_time,x);CHKERRQ(ierr);
18775a3a76d0SJed Brown       if (ftime) *ftime = ts->max_time;
18780574a7fbSJed Brown     } else {
18790574a7fbSJed Brown       ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
18800574a7fbSJed Brown       if (ftime) *ftime = ts->ptime;
18810574a7fbSJed Brown     }
1882193ac0bcSJed Brown   }
18834d7d938eSLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
18844d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
18854d7d938eSLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr);
18864d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
18874d7d938eSLisandro Dalcin     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1888193ac0bcSJed Brown   }
18896a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
18906a4d4014SLisandro Dalcin }
18916a4d4014SLisandro Dalcin 
18926a4d4014SLisandro Dalcin #undef __FUNCT__
18934a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
1894228d79bcSJed Brown /*@
1895228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
1896228d79bcSJed Brown 
1897228d79bcSJed Brown    Collective on TS
1898228d79bcSJed Brown 
1899228d79bcSJed Brown    Input Parameters:
1900228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
1901228d79bcSJed Brown .  step - step number that has just completed
1902228d79bcSJed Brown .  ptime - model time of the state
1903228d79bcSJed Brown -  x - state at the current model time
1904228d79bcSJed Brown 
1905228d79bcSJed Brown    Notes:
1906228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
1907228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
1908228d79bcSJed Brown 
1909228d79bcSJed Brown    Level: advanced
1910228d79bcSJed Brown 
1911228d79bcSJed Brown .keywords: TS, timestep
1912228d79bcSJed Brown @*/
1913a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
1914d763cef2SBarry Smith {
19156849ba73SBarry Smith   PetscErrorCode ierr;
1916a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
1917d763cef2SBarry Smith 
1918d763cef2SBarry Smith   PetscFunctionBegin;
1919d763cef2SBarry Smith   for (i=0; i<n; i++) {
1920142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
1921d763cef2SBarry Smith   }
1922d763cef2SBarry Smith   PetscFunctionReturn(0);
1923d763cef2SBarry Smith }
1924d763cef2SBarry Smith 
1925d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
1926d763cef2SBarry Smith 
19274a2ae208SSatish Balay #undef __FUNCT__
1928a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate"
1929d763cef2SBarry Smith /*@C
1930a6570f20SBarry Smith    TSMonitorLGCreate - Creates a line graph context for use with
1931d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
1932d763cef2SBarry Smith 
1933d763cef2SBarry Smith    Collective on TS
1934d763cef2SBarry Smith 
1935d763cef2SBarry Smith    Input Parameters:
1936d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
1937d763cef2SBarry Smith .  label - the title to put in the title bar
19387c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
1939d763cef2SBarry Smith -  m, n - the screen width and height in pixels
1940d763cef2SBarry Smith 
1941d763cef2SBarry Smith    Output Parameter:
1942d763cef2SBarry Smith .  draw - the drawing context
1943d763cef2SBarry Smith 
1944d763cef2SBarry Smith    Options Database Key:
1945a6570f20SBarry Smith .  -ts_monitor_draw - automatically sets line graph monitor
1946d763cef2SBarry Smith 
1947d763cef2SBarry Smith    Notes:
1948a6570f20SBarry Smith    Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy().
1949d763cef2SBarry Smith 
1950d763cef2SBarry Smith    Level: intermediate
1951d763cef2SBarry Smith 
19527c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
1953d763cef2SBarry Smith 
1954a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet()
19557c922b88SBarry Smith 
1956d763cef2SBarry Smith @*/
19577087cfbeSBarry Smith PetscErrorCode  TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1958d763cef2SBarry Smith {
1959b0a32e0cSBarry Smith   PetscDraw      win;
1960dfbe8321SBarry Smith   PetscErrorCode ierr;
1961d763cef2SBarry Smith 
1962d763cef2SBarry Smith   PetscFunctionBegin;
1963b0a32e0cSBarry Smith   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
1964b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
1965b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
1966b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
1967d763cef2SBarry Smith 
196852e6d16bSBarry Smith   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
1969d763cef2SBarry Smith   PetscFunctionReturn(0);
1970d763cef2SBarry Smith }
1971d763cef2SBarry Smith 
19724a2ae208SSatish Balay #undef __FUNCT__
1973a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG"
1974a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
1975d763cef2SBarry Smith {
1976b0a32e0cSBarry Smith   PetscDrawLG    lg = (PetscDrawLG) monctx;
197787828ca2SBarry Smith   PetscReal      x,y = ptime;
1978dfbe8321SBarry Smith   PetscErrorCode ierr;
1979d763cef2SBarry Smith 
1980d763cef2SBarry Smith   PetscFunctionBegin;
19817c922b88SBarry Smith   if (!monctx) {
19827c922b88SBarry Smith     MPI_Comm    comm;
1983b0a32e0cSBarry Smith     PetscViewer viewer;
19847c922b88SBarry Smith 
19857c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
1986b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
1987b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
19887c922b88SBarry Smith   }
19897c922b88SBarry Smith 
1990b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
199187828ca2SBarry Smith   x = (PetscReal)n;
1992b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1993d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
1994b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1995d763cef2SBarry Smith   }
1996d763cef2SBarry Smith   PetscFunctionReturn(0);
1997d763cef2SBarry Smith }
1998d763cef2SBarry Smith 
19994a2ae208SSatish Balay #undef __FUNCT__
2000a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy"
2001d763cef2SBarry Smith /*@C
2002a6570f20SBarry Smith    TSMonitorLGDestroy - Destroys a line graph context that was created
2003a6570f20SBarry Smith    with TSMonitorLGCreate().
2004d763cef2SBarry Smith 
2005b0a32e0cSBarry Smith    Collective on PetscDrawLG
2006d763cef2SBarry Smith 
2007d763cef2SBarry Smith    Input Parameter:
2008d763cef2SBarry Smith .  draw - the drawing context
2009d763cef2SBarry Smith 
2010d763cef2SBarry Smith    Level: intermediate
2011d763cef2SBarry Smith 
2012d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2013d763cef2SBarry Smith 
2014a6570f20SBarry Smith .seealso: TSMonitorLGCreate(),  TSMonitorSet(), TSMonitorLG();
2015d763cef2SBarry Smith @*/
20166bf464f9SBarry Smith PetscErrorCode  TSMonitorLGDestroy(PetscDrawLG *drawlg)
2017d763cef2SBarry Smith {
2018b0a32e0cSBarry Smith   PetscDraw      draw;
2019dfbe8321SBarry Smith   PetscErrorCode ierr;
2020d763cef2SBarry Smith 
2021d763cef2SBarry Smith   PetscFunctionBegin;
20226bf464f9SBarry Smith   ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr);
20236bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
2024b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
2025d763cef2SBarry Smith   PetscFunctionReturn(0);
2026d763cef2SBarry Smith }
2027d763cef2SBarry Smith 
20284a2ae208SSatish Balay #undef __FUNCT__
20294a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2030d763cef2SBarry Smith /*@
2031d763cef2SBarry Smith    TSGetTime - Gets the current time.
2032d763cef2SBarry Smith 
2033d763cef2SBarry Smith    Not Collective
2034d763cef2SBarry Smith 
2035d763cef2SBarry Smith    Input Parameter:
2036d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2037d763cef2SBarry Smith 
2038d763cef2SBarry Smith    Output Parameter:
2039d763cef2SBarry Smith .  t  - the current time
2040d763cef2SBarry Smith 
2041d763cef2SBarry Smith    Level: beginner
2042d763cef2SBarry Smith 
2043d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2044d763cef2SBarry Smith 
2045d763cef2SBarry Smith .keywords: TS, get, time
2046d763cef2SBarry Smith @*/
20477087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
2048d763cef2SBarry Smith {
2049d763cef2SBarry Smith   PetscFunctionBegin;
20500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20514482741eSBarry Smith   PetscValidDoublePointer(t,2);
2052d763cef2SBarry Smith   *t = ts->ptime;
2053d763cef2SBarry Smith   PetscFunctionReturn(0);
2054d763cef2SBarry Smith }
2055d763cef2SBarry Smith 
20564a2ae208SSatish Balay #undef __FUNCT__
20576a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
20586a4d4014SLisandro Dalcin /*@
20596a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
20606a4d4014SLisandro Dalcin 
20613f9fe445SBarry Smith    Logically Collective on TS
20626a4d4014SLisandro Dalcin 
20636a4d4014SLisandro Dalcin    Input Parameters:
20646a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
20656a4d4014SLisandro Dalcin -  time - the time
20666a4d4014SLisandro Dalcin 
20676a4d4014SLisandro Dalcin    Level: intermediate
20686a4d4014SLisandro Dalcin 
20696a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
20706a4d4014SLisandro Dalcin 
20716a4d4014SLisandro Dalcin .keywords: TS, set, time
20726a4d4014SLisandro Dalcin @*/
20737087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
20746a4d4014SLisandro Dalcin {
20756a4d4014SLisandro Dalcin   PetscFunctionBegin;
20760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2077c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
20786a4d4014SLisandro Dalcin   ts->ptime = t;
20796a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
20806a4d4014SLisandro Dalcin }
20816a4d4014SLisandro Dalcin 
20826a4d4014SLisandro Dalcin #undef __FUNCT__
20834a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2084d763cef2SBarry Smith /*@C
2085d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2086d763cef2SBarry Smith    TS options in the database.
2087d763cef2SBarry Smith 
20883f9fe445SBarry Smith    Logically Collective on TS
2089d763cef2SBarry Smith 
2090d763cef2SBarry Smith    Input Parameter:
2091d763cef2SBarry Smith +  ts     - The TS context
2092d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2093d763cef2SBarry Smith 
2094d763cef2SBarry Smith    Notes:
2095d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2096d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2097d763cef2SBarry Smith    hyphen.
2098d763cef2SBarry Smith 
2099d763cef2SBarry Smith    Level: advanced
2100d763cef2SBarry Smith 
2101d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2102d763cef2SBarry Smith 
2103d763cef2SBarry Smith .seealso: TSSetFromOptions()
2104d763cef2SBarry Smith 
2105d763cef2SBarry Smith @*/
21067087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2107d763cef2SBarry Smith {
2108dfbe8321SBarry Smith   PetscErrorCode ierr;
2109089b2837SJed Brown   SNES           snes;
2110d763cef2SBarry Smith 
2111d763cef2SBarry Smith   PetscFunctionBegin;
21120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2113d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2114089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2115089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2116d763cef2SBarry Smith   PetscFunctionReturn(0);
2117d763cef2SBarry Smith }
2118d763cef2SBarry Smith 
2119d763cef2SBarry Smith 
21204a2ae208SSatish Balay #undef __FUNCT__
21214a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2122d763cef2SBarry Smith /*@C
2123d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2124d763cef2SBarry Smith    TS options in the database.
2125d763cef2SBarry Smith 
21263f9fe445SBarry Smith    Logically Collective on TS
2127d763cef2SBarry Smith 
2128d763cef2SBarry Smith    Input Parameter:
2129d763cef2SBarry Smith +  ts     - The TS context
2130d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2131d763cef2SBarry Smith 
2132d763cef2SBarry Smith    Notes:
2133d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2134d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2135d763cef2SBarry Smith    hyphen.
2136d763cef2SBarry Smith 
2137d763cef2SBarry Smith    Level: advanced
2138d763cef2SBarry Smith 
2139d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2140d763cef2SBarry Smith 
2141d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2142d763cef2SBarry Smith 
2143d763cef2SBarry Smith @*/
21447087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2145d763cef2SBarry Smith {
2146dfbe8321SBarry Smith   PetscErrorCode ierr;
2147089b2837SJed Brown   SNES           snes;
2148d763cef2SBarry Smith 
2149d763cef2SBarry Smith   PetscFunctionBegin;
21500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2151d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2152089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2153089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2154d763cef2SBarry Smith   PetscFunctionReturn(0);
2155d763cef2SBarry Smith }
2156d763cef2SBarry Smith 
21574a2ae208SSatish Balay #undef __FUNCT__
21584a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2159d763cef2SBarry Smith /*@C
2160d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2161d763cef2SBarry Smith    TS options in the database.
2162d763cef2SBarry Smith 
2163d763cef2SBarry Smith    Not Collective
2164d763cef2SBarry Smith 
2165d763cef2SBarry Smith    Input Parameter:
2166d763cef2SBarry Smith .  ts - The TS context
2167d763cef2SBarry Smith 
2168d763cef2SBarry Smith    Output Parameter:
2169d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2170d763cef2SBarry Smith 
2171d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2172d763cef2SBarry Smith    sufficient length to hold the prefix.
2173d763cef2SBarry Smith 
2174d763cef2SBarry Smith    Level: intermediate
2175d763cef2SBarry Smith 
2176d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2177d763cef2SBarry Smith 
2178d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2179d763cef2SBarry Smith @*/
21807087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2181d763cef2SBarry Smith {
2182dfbe8321SBarry Smith   PetscErrorCode ierr;
2183d763cef2SBarry Smith 
2184d763cef2SBarry Smith   PetscFunctionBegin;
21850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21864482741eSBarry Smith   PetscValidPointer(prefix,2);
2187d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2188d763cef2SBarry Smith   PetscFunctionReturn(0);
2189d763cef2SBarry Smith }
2190d763cef2SBarry Smith 
21914a2ae208SSatish Balay #undef __FUNCT__
21924a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2193d763cef2SBarry Smith /*@C
2194d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2195d763cef2SBarry Smith 
2196d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2197d763cef2SBarry Smith 
2198d763cef2SBarry Smith    Input Parameter:
2199d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2200d763cef2SBarry Smith 
2201d763cef2SBarry Smith    Output Parameters:
2202d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2203d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2204089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2205d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2206d763cef2SBarry Smith 
2207d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2208d763cef2SBarry Smith 
2209d763cef2SBarry Smith    Level: intermediate
2210d763cef2SBarry Smith 
221126d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2212d763cef2SBarry Smith 
2213d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2214d763cef2SBarry Smith @*/
2215089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2216d763cef2SBarry Smith {
2217089b2837SJed Brown   PetscErrorCode ierr;
2218089b2837SJed Brown   SNES           snes;
2219089b2837SJed Brown 
2220d763cef2SBarry Smith   PetscFunctionBegin;
2221089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2222089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
22233b5f76d0SSean Farley   if (func) *func = ts->userops->rhsjacobian;
222426d46c62SHong Zhang   if (ctx) *ctx = ts->jacP;
2225d763cef2SBarry Smith   PetscFunctionReturn(0);
2226d763cef2SBarry Smith }
2227d763cef2SBarry Smith 
22281713a123SBarry Smith #undef __FUNCT__
22292eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
22302eca1d9cSJed Brown /*@C
22312eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
22322eca1d9cSJed Brown 
22332eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
22342eca1d9cSJed Brown 
22352eca1d9cSJed Brown    Input Parameter:
22362eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
22372eca1d9cSJed Brown 
22382eca1d9cSJed Brown    Output Parameters:
22392eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
22402eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
22412eca1d9cSJed Brown .  f   - The function to compute the matrices
22422eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
22432eca1d9cSJed Brown 
22442eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
22452eca1d9cSJed Brown 
22462eca1d9cSJed Brown    Level: advanced
22472eca1d9cSJed Brown 
22482eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
22492eca1d9cSJed Brown 
22502eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
22512eca1d9cSJed Brown @*/
22527087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
22532eca1d9cSJed Brown {
2254089b2837SJed Brown   PetscErrorCode ierr;
2255089b2837SJed Brown   SNES           snes;
2256089b2837SJed Brown 
22572eca1d9cSJed Brown   PetscFunctionBegin;
2258089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2259089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
22603b5f76d0SSean Farley   if (f) *f = ts->userops->ijacobian;
22612eca1d9cSJed Brown   if (ctx) *ctx = ts->jacP;
22622eca1d9cSJed Brown   PetscFunctionReturn(0);
22632eca1d9cSJed Brown }
22642eca1d9cSJed Brown 
22656083293cSBarry Smith typedef struct {
22666083293cSBarry Smith   PetscViewer viewer;
22676083293cSBarry Smith   Vec         initialsolution;
22686083293cSBarry Smith   PetscBool   showinitial;
22696083293cSBarry Smith } TSMonitorSolutionCtx;
22706083293cSBarry Smith 
22712eca1d9cSJed Brown #undef __FUNCT__
2272a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution"
22731713a123SBarry Smith /*@C
2274a6570f20SBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by calling
22751713a123SBarry Smith    VecView() for the solution at each timestep
22761713a123SBarry Smith 
22771713a123SBarry Smith    Collective on TS
22781713a123SBarry Smith 
22791713a123SBarry Smith    Input Parameters:
22801713a123SBarry Smith +  ts - the TS context
22811713a123SBarry Smith .  step - current time-step
2282142b95e3SSatish Balay .  ptime - current time
22831713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
22841713a123SBarry Smith 
22851713a123SBarry Smith    Level: intermediate
22861713a123SBarry Smith 
22871713a123SBarry Smith .keywords: TS,  vector, monitor, view
22881713a123SBarry Smith 
2289a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
22901713a123SBarry Smith @*/
22917087cfbeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
22921713a123SBarry Smith {
2293dfbe8321SBarry Smith   PetscErrorCode       ierr;
22946083293cSBarry Smith   TSMonitorSolutionCtx *ictx = (TSMonitorSolutionCtx*)dummy;
22951713a123SBarry Smith 
22961713a123SBarry Smith   PetscFunctionBegin;
22976083293cSBarry Smith   if (!step && ictx->showinitial) {
22986083293cSBarry Smith     if (!ictx->initialsolution) {
22996083293cSBarry Smith       ierr = VecDuplicate(x,&ictx->initialsolution);CHKERRQ(ierr);
23001713a123SBarry Smith     }
23016083293cSBarry Smith     ierr = VecCopy(x,ictx->initialsolution);CHKERRQ(ierr);
23026083293cSBarry Smith   }
23036083293cSBarry Smith   if (ictx->showinitial) {
23046083293cSBarry Smith     PetscReal pause;
23056083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
23066083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
23076083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
23086083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
23096083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
23106083293cSBarry Smith   }
23116083293cSBarry Smith   ierr = VecView(x,ictx->viewer);CHKERRQ(ierr);
23126083293cSBarry Smith   if (ictx->showinitial) {
23136083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
23146083293cSBarry Smith   }
23151713a123SBarry Smith   PetscFunctionReturn(0);
23161713a123SBarry Smith }
23171713a123SBarry Smith 
23181713a123SBarry Smith 
23196c699258SBarry Smith #undef __FUNCT__
23206083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionDestroy"
23216083293cSBarry Smith /*@C
23226083293cSBarry Smith    TSMonitorSolutionDestroy - Destroys the monitor context for TSMonitorSolution
23236083293cSBarry Smith 
23246083293cSBarry Smith    Collective on TS
23256083293cSBarry Smith 
23266083293cSBarry Smith    Input Parameters:
23276083293cSBarry Smith .    ctx - the monitor context
23286083293cSBarry Smith 
23296083293cSBarry Smith    Level: intermediate
23306083293cSBarry Smith 
23316083293cSBarry Smith .keywords: TS,  vector, monitor, view
23326083293cSBarry Smith 
23336083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution()
23346083293cSBarry Smith @*/
23356083293cSBarry Smith PetscErrorCode  TSMonitorSolutionDestroy(void **ctx)
23366083293cSBarry Smith {
23376083293cSBarry Smith   PetscErrorCode       ierr;
23386083293cSBarry Smith   TSMonitorSolutionCtx *ictx = *(TSMonitorSolutionCtx**)ctx;
23396083293cSBarry Smith 
23406083293cSBarry Smith   PetscFunctionBegin;
23416083293cSBarry Smith   ierr = PetscViewerDestroy(&ictx->viewer);CHKERRQ(ierr);
23426083293cSBarry Smith   ierr = VecDestroy(&ictx->initialsolution);CHKERRQ(ierr);
23436083293cSBarry Smith   ierr = PetscFree(ictx);CHKERRQ(ierr);
23446083293cSBarry Smith   PetscFunctionReturn(0);
23456083293cSBarry Smith }
23466083293cSBarry Smith 
23476083293cSBarry Smith #undef __FUNCT__
23486083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionCreate"
23496083293cSBarry Smith /*@C
23506083293cSBarry Smith    TSMonitorSolutionCreate - Creates the monitor context for TSMonitorSolution
23516083293cSBarry Smith 
23526083293cSBarry Smith    Collective on TS
23536083293cSBarry Smith 
23546083293cSBarry Smith    Input Parameter:
23556083293cSBarry Smith .    ts - time-step context
23566083293cSBarry Smith 
23576083293cSBarry Smith    Output Patameter:
23586083293cSBarry Smith .    ctx - the monitor context
23596083293cSBarry Smith 
23606083293cSBarry Smith    Level: intermediate
23616083293cSBarry Smith 
23626083293cSBarry Smith .keywords: TS,  vector, monitor, view
23636083293cSBarry Smith 
23646083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution()
23656083293cSBarry Smith @*/
23666083293cSBarry Smith PetscErrorCode  TSMonitorSolutionCreate(TS ts,PetscViewer viewer,void **ctx)
23676083293cSBarry Smith {
23686083293cSBarry Smith   PetscErrorCode       ierr;
23696083293cSBarry Smith   TSMonitorSolutionCtx *ictx;
23706083293cSBarry Smith 
23716083293cSBarry Smith   PetscFunctionBegin;
23726083293cSBarry Smith   ierr = PetscNew(TSMonitorSolutionCtx,&ictx);CHKERRQ(ierr);
23736083293cSBarry Smith   *ctx = (void*)ictx;
23746083293cSBarry Smith   if (!viewer) {
23756083293cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm);
23766083293cSBarry Smith   }
23776083293cSBarry Smith   ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr);
23786083293cSBarry Smith   ictx->viewer      = viewer;
23796083293cSBarry Smith   ictx->showinitial = PETSC_FALSE;
23806083293cSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix,"-ts_monitor_solution_initial",&ictx->showinitial,PETSC_NULL);CHKERRQ(ierr);
23816083293cSBarry Smith   PetscFunctionReturn(0);
23826083293cSBarry Smith }
23836083293cSBarry Smith 
23846083293cSBarry Smith #undef __FUNCT__
23856c699258SBarry Smith #define __FUNCT__ "TSSetDM"
23866c699258SBarry Smith /*@
23876c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
23886c699258SBarry Smith 
23893f9fe445SBarry Smith    Logically Collective on TS and DM
23906c699258SBarry Smith 
23916c699258SBarry Smith    Input Parameters:
23926c699258SBarry Smith +  ts - the preconditioner context
23936c699258SBarry Smith -  dm - the dm
23946c699258SBarry Smith 
23956c699258SBarry Smith    Level: intermediate
23966c699258SBarry Smith 
23976c699258SBarry Smith 
23986c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
23996c699258SBarry Smith @*/
24007087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
24016c699258SBarry Smith {
24026c699258SBarry Smith   PetscErrorCode ierr;
2403089b2837SJed Brown   SNES           snes;
24046c699258SBarry Smith 
24056c699258SBarry Smith   PetscFunctionBegin;
24060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
240770663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
24086bf464f9SBarry Smith   ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
24096c699258SBarry Smith   ts->dm = dm;
2410089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2411089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
24126c699258SBarry Smith   PetscFunctionReturn(0);
24136c699258SBarry Smith }
24146c699258SBarry Smith 
24156c699258SBarry Smith #undef __FUNCT__
24166c699258SBarry Smith #define __FUNCT__ "TSGetDM"
24176c699258SBarry Smith /*@
24186c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
24196c699258SBarry Smith 
24203f9fe445SBarry Smith    Not Collective
24216c699258SBarry Smith 
24226c699258SBarry Smith    Input Parameter:
24236c699258SBarry Smith . ts - the preconditioner context
24246c699258SBarry Smith 
24256c699258SBarry Smith    Output Parameter:
24266c699258SBarry Smith .  dm - the dm
24276c699258SBarry Smith 
24286c699258SBarry Smith    Level: intermediate
24296c699258SBarry Smith 
24306c699258SBarry Smith 
24316c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
24326c699258SBarry Smith @*/
24337087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
24346c699258SBarry Smith {
24356c699258SBarry Smith   PetscFunctionBegin;
24360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24376c699258SBarry Smith   *dm = ts->dm;
24386c699258SBarry Smith   PetscFunctionReturn(0);
24396c699258SBarry Smith }
24401713a123SBarry Smith 
24410f5c6efeSJed Brown #undef __FUNCT__
24420f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
24430f5c6efeSJed Brown /*@
24440f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
24450f5c6efeSJed Brown 
24463f9fe445SBarry Smith    Logically Collective on SNES
24470f5c6efeSJed Brown 
24480f5c6efeSJed Brown    Input Parameter:
2449d42a1c89SJed Brown + snes - nonlinear solver
24500f5c6efeSJed Brown . X - the current state at which to evaluate the residual
2451d42a1c89SJed Brown - ctx - user context, must be a TS
24520f5c6efeSJed Brown 
24530f5c6efeSJed Brown    Output Parameter:
24540f5c6efeSJed Brown . F - the nonlinear residual
24550f5c6efeSJed Brown 
24560f5c6efeSJed Brown    Notes:
24570f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
24580f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
24590f5c6efeSJed Brown 
24600f5c6efeSJed Brown    Level: advanced
24610f5c6efeSJed Brown 
24620f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
24630f5c6efeSJed Brown @*/
24647087cfbeSBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx)
24650f5c6efeSJed Brown {
24660f5c6efeSJed Brown   TS ts = (TS)ctx;
24670f5c6efeSJed Brown   PetscErrorCode ierr;
24680f5c6efeSJed Brown 
24690f5c6efeSJed Brown   PetscFunctionBegin;
24700f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24710f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
24720f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
24730f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
24740f5c6efeSJed Brown   ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr);
24750f5c6efeSJed Brown   PetscFunctionReturn(0);
24760f5c6efeSJed Brown }
24770f5c6efeSJed Brown 
24780f5c6efeSJed Brown #undef __FUNCT__
24790f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
24800f5c6efeSJed Brown /*@
24810f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
24820f5c6efeSJed Brown 
24830f5c6efeSJed Brown    Collective on SNES
24840f5c6efeSJed Brown 
24850f5c6efeSJed Brown    Input Parameter:
24860f5c6efeSJed Brown + snes - nonlinear solver
24870f5c6efeSJed Brown . X - the current state at which to evaluate the residual
24880f5c6efeSJed Brown - ctx - user context, must be a TS
24890f5c6efeSJed Brown 
24900f5c6efeSJed Brown    Output Parameter:
24910f5c6efeSJed Brown + A - the Jacobian
24920f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
24930f5c6efeSJed Brown - flag - indicates any structure change in the matrix
24940f5c6efeSJed Brown 
24950f5c6efeSJed Brown    Notes:
24960f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
24970f5c6efeSJed Brown 
24980f5c6efeSJed Brown    Level: developer
24990f5c6efeSJed Brown 
25000f5c6efeSJed Brown .seealso: SNESSetJacobian()
25010f5c6efeSJed Brown @*/
25027087cfbeSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx)
25030f5c6efeSJed Brown {
25040f5c6efeSJed Brown   TS ts = (TS)ctx;
25050f5c6efeSJed Brown   PetscErrorCode ierr;
25060f5c6efeSJed Brown 
25070f5c6efeSJed Brown   PetscFunctionBegin;
25080f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25090f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
25100f5c6efeSJed Brown   PetscValidPointer(A,3);
25110f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
25120f5c6efeSJed Brown   PetscValidPointer(B,4);
25130f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
25140f5c6efeSJed Brown   PetscValidPointer(flag,5);
25150f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
25160f5c6efeSJed Brown   ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr);
25170f5c6efeSJed Brown   PetscFunctionReturn(0);
25180f5c6efeSJed Brown }
2519325fc9f4SBarry Smith 
25200e4ef248SJed Brown #undef __FUNCT__
25210e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
25220e4ef248SJed Brown /*@C
25230e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
25240e4ef248SJed Brown 
25250e4ef248SJed Brown    Collective on TS
25260e4ef248SJed Brown 
25270e4ef248SJed Brown    Input Arguments:
25280e4ef248SJed Brown +  ts - time stepping context
25290e4ef248SJed Brown .  t - time at which to evaluate
25300e4ef248SJed Brown .  X - state at which to evaluate
25310e4ef248SJed Brown -  ctx - context
25320e4ef248SJed Brown 
25330e4ef248SJed Brown    Output Arguments:
25340e4ef248SJed Brown .  F - right hand side
25350e4ef248SJed Brown 
25360e4ef248SJed Brown    Level: intermediate
25370e4ef248SJed Brown 
25380e4ef248SJed Brown    Notes:
25390e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
25400e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
25410e4ef248SJed Brown 
25420e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
25430e4ef248SJed Brown @*/
25440e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx)
25450e4ef248SJed Brown {
25460e4ef248SJed Brown   PetscErrorCode ierr;
25470e4ef248SJed Brown   Mat Arhs,Brhs;
25480e4ef248SJed Brown   MatStructure flg2;
25490e4ef248SJed Brown 
25500e4ef248SJed Brown   PetscFunctionBegin;
25510e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
25520e4ef248SJed Brown   ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
25530e4ef248SJed Brown   ierr = MatMult(Arhs,X,F);CHKERRQ(ierr);
25540e4ef248SJed Brown   PetscFunctionReturn(0);
25550e4ef248SJed Brown }
25560e4ef248SJed Brown 
25570e4ef248SJed Brown #undef __FUNCT__
25580e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
25590e4ef248SJed Brown /*@C
25600e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
25610e4ef248SJed Brown 
25620e4ef248SJed Brown    Collective on TS
25630e4ef248SJed Brown 
25640e4ef248SJed Brown    Input Arguments:
25650e4ef248SJed Brown +  ts - time stepping context
25660e4ef248SJed Brown .  t - time at which to evaluate
25670e4ef248SJed Brown .  X - state at which to evaluate
25680e4ef248SJed Brown -  ctx - context
25690e4ef248SJed Brown 
25700e4ef248SJed Brown    Output Arguments:
25710e4ef248SJed Brown +  A - pointer to operator
25720e4ef248SJed Brown .  B - pointer to preconditioning matrix
25730e4ef248SJed Brown -  flg - matrix structure flag
25740e4ef248SJed Brown 
25750e4ef248SJed Brown    Level: intermediate
25760e4ef248SJed Brown 
25770e4ef248SJed Brown    Notes:
25780e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
25790e4ef248SJed Brown 
25800e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
25810e4ef248SJed Brown @*/
25820e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx)
25830e4ef248SJed Brown {
25840e4ef248SJed Brown 
25850e4ef248SJed Brown   PetscFunctionBegin;
25860e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
25870e4ef248SJed Brown   PetscFunctionReturn(0);
25880e4ef248SJed Brown }
25890e4ef248SJed Brown 
25900026cea9SSean Farley #undef __FUNCT__
25910026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
25920026cea9SSean Farley /*@C
25930026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
25940026cea9SSean Farley 
25950026cea9SSean Farley    Collective on TS
25960026cea9SSean Farley 
25970026cea9SSean Farley    Input Arguments:
25980026cea9SSean Farley +  ts - time stepping context
25990026cea9SSean Farley .  t - time at which to evaluate
26000026cea9SSean Farley .  X - state at which to evaluate
26010026cea9SSean Farley .  Xdot - time derivative of state vector
26020026cea9SSean Farley -  ctx - context
26030026cea9SSean Farley 
26040026cea9SSean Farley    Output Arguments:
26050026cea9SSean Farley .  F - left hand side
26060026cea9SSean Farley 
26070026cea9SSean Farley    Level: intermediate
26080026cea9SSean Farley 
26090026cea9SSean Farley    Notes:
26100026cea9SSean Farley    The assumption here is that the left hand side is of the form A*Xdot (and not A*Xdot + B*X). For other cases, the
26110026cea9SSean Farley    user is required to write their own TSComputeIFunction.
26120026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
26130026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
26140026cea9SSean Farley 
26150026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
26160026cea9SSean Farley @*/
26170026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx)
26180026cea9SSean Farley {
26190026cea9SSean Farley   PetscErrorCode ierr;
26200026cea9SSean Farley   Mat A,B;
26210026cea9SSean Farley   MatStructure flg2;
26220026cea9SSean Farley 
26230026cea9SSean Farley   PetscFunctionBegin;
26240026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
26250026cea9SSean Farley   ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
26260026cea9SSean Farley   ierr = MatMult(A,Xdot,F);CHKERRQ(ierr);
26270026cea9SSean Farley   PetscFunctionReturn(0);
26280026cea9SSean Farley }
26290026cea9SSean Farley 
26300026cea9SSean Farley #undef __FUNCT__
26310026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
26320026cea9SSean Farley /*@C
26330026cea9SSean Farley    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
26340026cea9SSean Farley 
26350026cea9SSean Farley    Collective on TS
26360026cea9SSean Farley 
26370026cea9SSean Farley    Input Arguments:
26380026cea9SSean Farley +  ts - time stepping context
26390026cea9SSean Farley .  t - time at which to evaluate
26400026cea9SSean Farley .  X - state at which to evaluate
26410026cea9SSean Farley .  Xdot - time derivative of state vector
26420026cea9SSean Farley .  shift - shift to apply
26430026cea9SSean Farley -  ctx - context
26440026cea9SSean Farley 
26450026cea9SSean Farley    Output Arguments:
26460026cea9SSean Farley +  A - pointer to operator
26470026cea9SSean Farley .  B - pointer to preconditioning matrix
26480026cea9SSean Farley -  flg - matrix structure flag
26490026cea9SSean Farley 
26500026cea9SSean Farley    Level: intermediate
26510026cea9SSean Farley 
26520026cea9SSean Farley    Notes:
26530026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
26540026cea9SSean Farley 
26550026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
26560026cea9SSean Farley @*/
26570026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
26580026cea9SSean Farley {
26590026cea9SSean Farley 
26600026cea9SSean Farley   PetscFunctionBegin;
26610026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
26620026cea9SSean Farley   PetscFunctionReturn(0);
26630026cea9SSean Farley }
26640026cea9SSean Farley 
26654af1b03aSJed Brown 
26664af1b03aSJed Brown #undef __FUNCT__
26674af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
26684af1b03aSJed Brown /*@
26694af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
26704af1b03aSJed Brown 
26714af1b03aSJed Brown    Not Collective
26724af1b03aSJed Brown 
26734af1b03aSJed Brown    Input Parameter:
26744af1b03aSJed Brown .  ts - the TS context
26754af1b03aSJed Brown 
26764af1b03aSJed Brown    Output Parameter:
26774af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
26784af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
26794af1b03aSJed Brown 
26804af1b03aSJed Brown    Level: intermediate
26814af1b03aSJed Brown 
2682cd652676SJed Brown    Notes:
2683cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
26844af1b03aSJed Brown 
26854af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
26864af1b03aSJed Brown 
26874af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
26884af1b03aSJed Brown @*/
26894af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
26904af1b03aSJed Brown {
26914af1b03aSJed Brown   PetscFunctionBegin;
26924af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
26934af1b03aSJed Brown   PetscValidPointer(reason,2);
26944af1b03aSJed Brown   *reason = ts->reason;
26954af1b03aSJed Brown   PetscFunctionReturn(0);
26964af1b03aSJed Brown }
26974af1b03aSJed Brown 
2698fb1732b5SBarry Smith #undef __FUNCT__
26999f67acb7SJed Brown #define __FUNCT__ "TSGetNonlinearSolveIterations"
27009f67acb7SJed Brown /*@
2701*01c7e25bSJed Brown    TSGetNonlinearSolveIterations - Gets the total number of linear iterations
27029f67acb7SJed Brown    used by the time integrator.
27039f67acb7SJed Brown 
27049f67acb7SJed Brown    Not Collective
27059f67acb7SJed Brown 
27069f67acb7SJed Brown    Input Parameter:
27079f67acb7SJed Brown .  ts - TS context
27089f67acb7SJed Brown 
27099f67acb7SJed Brown    Output Parameter:
27109f67acb7SJed Brown .  nits - number of nonlinear iterations
27119f67acb7SJed Brown 
27129f67acb7SJed Brown    Notes:
27139f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
27149f67acb7SJed Brown 
27159f67acb7SJed Brown    Level: intermediate
27169f67acb7SJed Brown 
27179f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
27189f67acb7SJed Brown 
27199f67acb7SJed Brown .seealso:  TSGetLinearSolveIterations()
27209f67acb7SJed Brown @*/
27219f67acb7SJed Brown PetscErrorCode TSGetNonlinearSolveIterations(TS ts,PetscInt *nits)
27229f67acb7SJed Brown {
27239f67acb7SJed Brown   PetscFunctionBegin;
27249f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27259f67acb7SJed Brown   PetscValidIntPointer(nits,2);
27269f67acb7SJed Brown   *nits = ts->nonlinear_its;
27279f67acb7SJed Brown   PetscFunctionReturn(0);
27289f67acb7SJed Brown }
27299f67acb7SJed Brown 
27309f67acb7SJed Brown #undef __FUNCT__
27319f67acb7SJed Brown #define __FUNCT__ "TSGetLinearSolveIterations"
27329f67acb7SJed Brown /*@
27339f67acb7SJed Brown    TSGetLinearSolveIterations - Gets the total number of linear iterations
27349f67acb7SJed Brown    used by the time integrator.
27359f67acb7SJed Brown 
27369f67acb7SJed Brown    Not Collective
27379f67acb7SJed Brown 
27389f67acb7SJed Brown    Input Parameter:
27399f67acb7SJed Brown .  ts - TS context
27409f67acb7SJed Brown 
27419f67acb7SJed Brown    Output Parameter:
27429f67acb7SJed Brown .  lits - number of linear iterations
27439f67acb7SJed Brown 
27449f67acb7SJed Brown    Notes:
27459f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
27469f67acb7SJed Brown 
27479f67acb7SJed Brown    Level: intermediate
27489f67acb7SJed Brown 
27499f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
27509f67acb7SJed Brown 
27519f67acb7SJed Brown .seealso:  TSGetNonlinearSolveIterations(), SNESGetLinearSolveIterations()
27529f67acb7SJed Brown @*/
27539f67acb7SJed Brown PetscErrorCode TSGetLinearSolveIterations(TS ts,PetscInt *lits)
27549f67acb7SJed Brown {
27559f67acb7SJed Brown   PetscFunctionBegin;
27569f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27579f67acb7SJed Brown   PetscValidIntPointer(lits,2);
27589f67acb7SJed Brown   *lits = ts->linear_its;
27599f67acb7SJed Brown   PetscFunctionReturn(0);
27609f67acb7SJed Brown }
27619f67acb7SJed Brown 
27629f67acb7SJed Brown #undef __FUNCT__
2763fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
2764fb1732b5SBarry Smith /*@C
2765fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
2766fb1732b5SBarry Smith 
2767fb1732b5SBarry Smith    Collective on TS
2768fb1732b5SBarry Smith 
2769fb1732b5SBarry Smith    Input Parameters:
2770fb1732b5SBarry Smith +  ts - the TS context
2771fb1732b5SBarry Smith .  step - current time-step
2772fb1732b5SBarry Smith .  ptime - current time
2773fb1732b5SBarry Smith -  viewer - binary viewer
2774fb1732b5SBarry Smith 
2775fb1732b5SBarry Smith    Level: intermediate
2776fb1732b5SBarry Smith 
2777fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
2778fb1732b5SBarry Smith 
2779fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
2780fb1732b5SBarry Smith @*/
2781fb1732b5SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
2782fb1732b5SBarry Smith {
2783fb1732b5SBarry Smith   PetscErrorCode       ierr;
2784fb1732b5SBarry Smith   PetscViewer          viewer = (PetscViewer)dummy;
2785fb1732b5SBarry Smith 
2786fb1732b5SBarry Smith   PetscFunctionBegin;
2787fb1732b5SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
2788fb1732b5SBarry Smith   PetscFunctionReturn(0);
2789fb1732b5SBarry Smith }
2790fb1732b5SBarry Smith 
2791d6ebe24aSShri Abhyankar 
2792d6ebe24aSShri Abhyankar #undef __FUNCT__
2793d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
2794d6ebe24aSShri Abhyankar /*@
2795d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
2796d6ebe24aSShri Abhyankar 
2797d6ebe24aSShri Abhyankar    Input Parameters:
2798d6ebe24aSShri Abhyankar .  ts   - the TS context.
2799d6ebe24aSShri Abhyankar .  xl   - lower bound.
2800d6ebe24aSShri Abhyankar .  xu   - upper bound.
2801d6ebe24aSShri Abhyankar 
2802d6ebe24aSShri Abhyankar    Notes:
2803d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
2804d6ebe24aSShri Abhyankar    SNES_VI_INF and SNES_VI_NINF respectively during SNESSetUp().
2805d6ebe24aSShri Abhyankar 
28062bd2b0e6SSatish Balay    Level: advanced
28072bd2b0e6SSatish Balay 
2808d6ebe24aSShri Abhyankar @*/
2809d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
2810d6ebe24aSShri Abhyankar {
2811d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
2812d6ebe24aSShri Abhyankar   SNES           snes;
2813d6ebe24aSShri Abhyankar 
2814d6ebe24aSShri Abhyankar   PetscFunctionBegin;
2815d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2816d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
2817d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
2818d6ebe24aSShri Abhyankar }
2819d6ebe24aSShri Abhyankar 
2820325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2821c6db04a5SJed Brown #include <mex.h>
2822325fc9f4SBarry Smith 
2823325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
2824325fc9f4SBarry Smith 
2825325fc9f4SBarry Smith #undef __FUNCT__
2826325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
2827325fc9f4SBarry Smith /*
2828325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
2829325fc9f4SBarry Smith                          TSSetFunctionMatlab().
2830325fc9f4SBarry Smith 
2831325fc9f4SBarry Smith    Collective on TS
2832325fc9f4SBarry Smith 
2833325fc9f4SBarry Smith    Input Parameters:
2834325fc9f4SBarry Smith +  snes - the TS context
2835325fc9f4SBarry Smith -  x - input vector
2836325fc9f4SBarry Smith 
2837325fc9f4SBarry Smith    Output Parameter:
2838325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
2839325fc9f4SBarry Smith 
2840325fc9f4SBarry Smith    Notes:
2841325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
2842325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
2843325fc9f4SBarry Smith    themselves.
2844325fc9f4SBarry Smith 
2845325fc9f4SBarry Smith    Level: developer
2846325fc9f4SBarry Smith 
2847325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2848325fc9f4SBarry Smith 
2849325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2850325fc9f4SBarry Smith */
28517087cfbeSBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx)
2852325fc9f4SBarry Smith {
2853325fc9f4SBarry Smith   PetscErrorCode   ierr;
2854325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2855325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
2856325fc9f4SBarry Smith   mxArray          *plhs[1],*prhs[7];
2857325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
2858325fc9f4SBarry Smith 
2859325fc9f4SBarry Smith   PetscFunctionBegin;
2860325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
2861325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2862325fc9f4SBarry Smith   PetscValidHeaderSpecific(xdot,VEC_CLASSID,4);
2863325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
2864325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,x,3);
2865325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
2866325fc9f4SBarry Smith 
2867325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
2868325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2869880f3077SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr);
2870325fc9f4SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
2871325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2872325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
2873325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2874325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2875325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
2876325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
2877325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
2878325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
2879325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2880325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2881325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2882325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2883325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2884325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2885325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2886325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2887325fc9f4SBarry Smith   PetscFunctionReturn(0);
2888325fc9f4SBarry Smith }
2889325fc9f4SBarry Smith 
2890325fc9f4SBarry Smith 
2891325fc9f4SBarry Smith #undef __FUNCT__
2892325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
2893325fc9f4SBarry Smith /*
2894325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
2895325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
2896e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
2897325fc9f4SBarry Smith 
2898325fc9f4SBarry Smith    Logically Collective on TS
2899325fc9f4SBarry Smith 
2900325fc9f4SBarry Smith    Input Parameters:
2901325fc9f4SBarry Smith +  ts - the TS context
2902325fc9f4SBarry Smith -  func - function evaluation routine
2903325fc9f4SBarry Smith 
2904325fc9f4SBarry Smith    Calling sequence of func:
2905325fc9f4SBarry Smith $    func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx);
2906325fc9f4SBarry Smith 
2907325fc9f4SBarry Smith    Level: beginner
2908325fc9f4SBarry Smith 
2909325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
2910325fc9f4SBarry Smith 
2911325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
2912325fc9f4SBarry Smith */
2913cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
2914325fc9f4SBarry Smith {
2915325fc9f4SBarry Smith   PetscErrorCode  ierr;
2916325fc9f4SBarry Smith   TSMatlabContext *sctx;
2917325fc9f4SBarry Smith 
2918325fc9f4SBarry Smith   PetscFunctionBegin;
2919325fc9f4SBarry Smith   /* currently sctx is memory bleed */
2920325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
2921325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
2922325fc9f4SBarry Smith   /*
2923325fc9f4SBarry Smith      This should work, but it doesn't
2924325fc9f4SBarry Smith   sctx->ctx = ctx;
2925325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
2926325fc9f4SBarry Smith   */
2927325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
2928cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
2929325fc9f4SBarry Smith   PetscFunctionReturn(0);
2930325fc9f4SBarry Smith }
2931325fc9f4SBarry Smith 
2932325fc9f4SBarry Smith #undef __FUNCT__
2933325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
2934325fc9f4SBarry Smith /*
2935325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
2936325fc9f4SBarry Smith                          TSSetJacobianMatlab().
2937325fc9f4SBarry Smith 
2938325fc9f4SBarry Smith    Collective on TS
2939325fc9f4SBarry Smith 
2940325fc9f4SBarry Smith    Input Parameters:
2941cdcf91faSSean Farley +  ts - the TS context
2942325fc9f4SBarry Smith .  x - input vector
2943325fc9f4SBarry Smith .  A, B - the matrices
2944325fc9f4SBarry Smith -  ctx - user context
2945325fc9f4SBarry Smith 
2946325fc9f4SBarry Smith    Output Parameter:
2947325fc9f4SBarry Smith .  flag - structure of the matrix
2948325fc9f4SBarry Smith 
2949325fc9f4SBarry Smith    Level: developer
2950325fc9f4SBarry Smith 
2951325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
2952325fc9f4SBarry Smith 
2953325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
2954325fc9f4SBarry Smith @*/
2955cdcf91faSSean Farley PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
2956325fc9f4SBarry Smith {
2957325fc9f4SBarry Smith   PetscErrorCode  ierr;
2958325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
2959325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
2960325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
2961325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
2962325fc9f4SBarry Smith 
2963325fc9f4SBarry Smith   PetscFunctionBegin;
2964cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2965325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2966325fc9f4SBarry Smith 
2967325fc9f4SBarry Smith   /* call Matlab function in ctx with arguments x and y */
2968325fc9f4SBarry Smith 
2969cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
2970325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
2971325fc9f4SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr);
2972325fc9f4SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
2973325fc9f4SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
2974325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
2975325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
2976325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
2977325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
2978325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
2979325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
2980325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
2981325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
2982325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
2983325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
2984325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
2985325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
2986325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
2987325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
2988325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
2989325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
2990325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
2991325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
2992325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
2993325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
2994325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
2995325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
2996325fc9f4SBarry Smith   PetscFunctionReturn(0);
2997325fc9f4SBarry Smith }
2998325fc9f4SBarry Smith 
2999325fc9f4SBarry Smith 
3000325fc9f4SBarry Smith #undef __FUNCT__
3001325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
3002325fc9f4SBarry Smith /*
3003325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
3004e3c5b3baSBarry 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
3005325fc9f4SBarry Smith 
3006325fc9f4SBarry Smith    Logically Collective on TS
3007325fc9f4SBarry Smith 
3008325fc9f4SBarry Smith    Input Parameters:
3009cdcf91faSSean Farley +  ts - the TS context
3010325fc9f4SBarry Smith .  A,B - Jacobian matrices
3011325fc9f4SBarry Smith .  func - function evaluation routine
3012325fc9f4SBarry Smith -  ctx - user context
3013325fc9f4SBarry Smith 
3014325fc9f4SBarry Smith    Calling sequence of func:
3015cdcf91faSSean Farley $    flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx);
3016325fc9f4SBarry Smith 
3017325fc9f4SBarry Smith 
3018325fc9f4SBarry Smith    Level: developer
3019325fc9f4SBarry Smith 
3020325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3021325fc9f4SBarry Smith 
3022325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3023325fc9f4SBarry Smith */
3024cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
3025325fc9f4SBarry Smith {
3026325fc9f4SBarry Smith   PetscErrorCode    ierr;
3027325fc9f4SBarry Smith   TSMatlabContext *sctx;
3028325fc9f4SBarry Smith 
3029325fc9f4SBarry Smith   PetscFunctionBegin;
3030325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3031325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3032325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3033325fc9f4SBarry Smith   /*
3034325fc9f4SBarry Smith      This should work, but it doesn't
3035325fc9f4SBarry Smith   sctx->ctx = ctx;
3036325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3037325fc9f4SBarry Smith   */
3038325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3039cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
3040325fc9f4SBarry Smith   PetscFunctionReturn(0);
3041325fc9f4SBarry Smith }
3042325fc9f4SBarry Smith 
3043b5b1a830SBarry Smith #undef __FUNCT__
3044b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
3045b5b1a830SBarry Smith /*
3046b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
3047b5b1a830SBarry Smith 
3048b5b1a830SBarry Smith    Collective on TS
3049b5b1a830SBarry Smith 
3050b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3051b5b1a830SBarry Smith @*/
3052cdcf91faSSean Farley PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx)
3053b5b1a830SBarry Smith {
3054b5b1a830SBarry Smith   PetscErrorCode  ierr;
3055b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3056a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
3057b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
3058b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
3059b5b1a830SBarry Smith 
3060b5b1a830SBarry Smith   PetscFunctionBegin;
3061cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3062b5b1a830SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,4);
3063b5b1a830SBarry Smith 
3064cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
3065b5b1a830SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3066b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3067b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
3068b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
3069b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
3070b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
3071b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
3072b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
3073b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3074b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
3075b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
3076b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
3077b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
3078b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
3079b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
3080b5b1a830SBarry Smith   PetscFunctionReturn(0);
3081b5b1a830SBarry Smith }
3082b5b1a830SBarry Smith 
3083b5b1a830SBarry Smith 
3084b5b1a830SBarry Smith #undef __FUNCT__
3085b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
3086b5b1a830SBarry Smith /*
3087b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
3088b5b1a830SBarry Smith 
3089b5b1a830SBarry Smith    Level: developer
3090b5b1a830SBarry Smith 
3091b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
3092b5b1a830SBarry Smith 
3093b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3094b5b1a830SBarry Smith */
3095cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
3096b5b1a830SBarry Smith {
3097b5b1a830SBarry Smith   PetscErrorCode    ierr;
3098b5b1a830SBarry Smith   TSMatlabContext *sctx;
3099b5b1a830SBarry Smith 
3100b5b1a830SBarry Smith   PetscFunctionBegin;
3101b5b1a830SBarry Smith   /* currently sctx is memory bleed */
3102b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3103b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3104b5b1a830SBarry Smith   /*
3105b5b1a830SBarry Smith      This should work, but it doesn't
3106b5b1a830SBarry Smith   sctx->ctx = ctx;
3107b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3108b5b1a830SBarry Smith   */
3109b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3110cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
3111b5b1a830SBarry Smith   PetscFunctionReturn(0);
3112b5b1a830SBarry Smith }
3113325fc9f4SBarry Smith #endif
3114