xref: /petsc/src/ts/interface/ts.c (revision c134de8d2530e1d0e12b3d536a25fd1369966ab4)
173f4d377SMatthew Knepley /* $Id: ts.c,v 1.43 2001/09/07 20:12:01 bsmith Exp $ */
2e090d566SSatish Balay #include "src/ts/tsimpl.h"        /*I "petscts.h"  I*/
3d763cef2SBarry Smith 
4d405a339SMatthew Knepley int TSEvents[TS_MAX_EVENTS];
5d405a339SMatthew Knepley 
64a2ae208SSatish Balay #undef __FUNCT__
74a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
8a7a1495cSBarry Smith /*@
98c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
10a7a1495cSBarry Smith       set with TSSetRHSJacobian().
11a7a1495cSBarry Smith 
12a7a1495cSBarry Smith    Collective on TS and Vec
13a7a1495cSBarry Smith 
14a7a1495cSBarry Smith    Input Parameters:
15a7a1495cSBarry Smith +  ts - the SNES context
16a7a1495cSBarry Smith .  t - current timestep
17a7a1495cSBarry Smith -  x - input vector
18a7a1495cSBarry Smith 
19a7a1495cSBarry Smith    Output Parameters:
20a7a1495cSBarry Smith +  A - Jacobian matrix
21a7a1495cSBarry Smith .  B - optional preconditioning matrix
22a7a1495cSBarry Smith -  flag - flag indicating matrix structure
23a7a1495cSBarry Smith 
24a7a1495cSBarry Smith    Notes:
25a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
26a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
27a7a1495cSBarry Smith 
28a7a1495cSBarry Smith    See SLESSetOperators() for important information about setting the
29a7a1495cSBarry Smith    flag parameter.
30a7a1495cSBarry Smith 
31a7a1495cSBarry Smith    TSComputeJacobian() is valid only for TS_NONLINEAR
32a7a1495cSBarry Smith 
33a7a1495cSBarry Smith    Level: developer
34a7a1495cSBarry Smith 
35a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
36a7a1495cSBarry Smith 
37a7a1495cSBarry Smith .seealso:  TSSetRHSJacobian(), SLESSetOperators()
38a7a1495cSBarry Smith @*/
3987828ca2SBarry Smith int TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
40a7a1495cSBarry Smith {
41a7a1495cSBarry Smith   int ierr;
42a7a1495cSBarry Smith 
43a7a1495cSBarry Smith   PetscFunctionBegin;
44a7a1495cSBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
45a7a1495cSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE);
46a7a1495cSBarry Smith   PetscCheckSameComm(ts,X);
47a7a1495cSBarry Smith   if (ts->problem_type != TS_NONLINEAR) {
4829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For TS_NONLINEAR only");
49a7a1495cSBarry Smith   }
50000e7ae3SMatthew Knepley   if (ts->ops->rhsjacobian) {
51b0a32e0cSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
52a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
53a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
54000e7ae3SMatthew Knepley     ierr = (*ts->ops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr);
55a7a1495cSBarry Smith     PetscStackPop;
56b0a32e0cSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
57a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
58a7a1495cSBarry Smith     PetscValidHeaderSpecific(*A,MAT_COOKIE);
59a7a1495cSBarry Smith     PetscValidHeaderSpecific(*B,MAT_COOKIE);
60ef66eb69SBarry Smith   } else {
61ef66eb69SBarry Smith     ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
62ef66eb69SBarry Smith     ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
63ef66eb69SBarry Smith     if (*A != *B) {
64ef66eb69SBarry Smith       ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
65ef66eb69SBarry Smith       ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
66ef66eb69SBarry Smith     }
67ef66eb69SBarry Smith   }
68a7a1495cSBarry Smith   PetscFunctionReturn(0);
69a7a1495cSBarry Smith }
70a7a1495cSBarry Smith 
714a2ae208SSatish Balay #undef __FUNCT__
724a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
73d763cef2SBarry Smith /*
74d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
75d763cef2SBarry Smith 
76d763cef2SBarry Smith    Note: If the user did not provide a function but merely a matrix,
77d763cef2SBarry Smith    this routine applies the matrix.
78d763cef2SBarry Smith */
7987828ca2SBarry Smith int TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
80d763cef2SBarry Smith {
81d763cef2SBarry Smith   int ierr;
82d763cef2SBarry Smith 
83d763cef2SBarry Smith   PetscFunctionBegin;
84d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
857c922b88SBarry Smith   PetscValidHeader(x);
867c922b88SBarry Smith   PetscValidHeader(y);
87d763cef2SBarry Smith 
88b0a32e0cSBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
89000e7ae3SMatthew Knepley   if (ts->ops->rhsfunction) {
90d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
91000e7ae3SMatthew Knepley     ierr = (*ts->ops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr);
92d763cef2SBarry Smith     PetscStackPop;
937c922b88SBarry Smith   } else {
94000e7ae3SMatthew Knepley     if (ts->ops->rhsmatrix) { /* assemble matrix for this timestep */
95d763cef2SBarry Smith       MatStructure flg;
96d763cef2SBarry Smith       PetscStackPush("TS user right-hand-side matrix function");
97000e7ae3SMatthew Knepley       ierr = (*ts->ops->rhsmatrix)(ts,t,&ts->A,&ts->B,&flg,ts->jacP);CHKERRQ(ierr);
98d763cef2SBarry Smith       PetscStackPop;
99d763cef2SBarry Smith     }
100d763cef2SBarry Smith     ierr = MatMult(ts->A,x,y);CHKERRQ(ierr);
1017c922b88SBarry Smith   }
102d763cef2SBarry Smith 
103d763cef2SBarry Smith   /* apply user-provided boundary conditions (only needed if these are time dependent) */
104d763cef2SBarry Smith   ierr = TSComputeRHSBoundaryConditions(ts,t,y);CHKERRQ(ierr);
105b0a32e0cSBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
106d763cef2SBarry Smith 
107d763cef2SBarry Smith   PetscFunctionReturn(0);
108d763cef2SBarry Smith }
109d763cef2SBarry Smith 
1104a2ae208SSatish Balay #undef __FUNCT__
1114a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
112d763cef2SBarry Smith /*@C
113d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
114d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
115d763cef2SBarry Smith 
116d763cef2SBarry Smith     Collective on TS
117d763cef2SBarry Smith 
118d763cef2SBarry Smith     Input Parameters:
119d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
120d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
121d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
122d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
123d763cef2SBarry Smith 
124d763cef2SBarry Smith     Calling sequence of func:
12587828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
126d763cef2SBarry Smith 
127d763cef2SBarry Smith +   t - current timestep
128d763cef2SBarry Smith .   u - input vector
129d763cef2SBarry Smith .   F - function vector
130d763cef2SBarry Smith -   ctx - [optional] user-defined function context
131d763cef2SBarry Smith 
132d763cef2SBarry Smith     Important:
133d763cef2SBarry Smith     The user MUST call either this routine or TSSetRHSMatrix().
134d763cef2SBarry Smith 
135d763cef2SBarry Smith     Level: beginner
136d763cef2SBarry Smith 
137d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
138d763cef2SBarry Smith 
139d763cef2SBarry Smith .seealso: TSSetRHSMatrix()
140d763cef2SBarry Smith @*/
14187828ca2SBarry Smith int TSSetRHSFunction(TS ts,int (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
142d763cef2SBarry Smith {
143d763cef2SBarry Smith   PetscFunctionBegin;
144d763cef2SBarry Smith 
145d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
146d763cef2SBarry Smith   if (ts->problem_type == TS_LINEAR) {
14729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Cannot set function for linear problem");
148d763cef2SBarry Smith   }
149000e7ae3SMatthew Knepley   ts->ops->rhsfunction = f;
150d763cef2SBarry Smith   ts->funP             = ctx;
151d763cef2SBarry Smith   PetscFunctionReturn(0);
152d763cef2SBarry Smith }
153d763cef2SBarry Smith 
1544a2ae208SSatish Balay #undef __FUNCT__
1554a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSMatrix"
156d763cef2SBarry Smith /*@C
157d763cef2SBarry Smith    TSSetRHSMatrix - Sets the function to compute the matrix A, where U_t = A(t) U.
158d763cef2SBarry Smith    Also sets the location to store A.
159d763cef2SBarry Smith 
160d763cef2SBarry Smith    Collective on TS
161d763cef2SBarry Smith 
162d763cef2SBarry Smith    Input Parameters:
163d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
164d763cef2SBarry Smith .  A   - matrix
165d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
166d763cef2SBarry Smith .  f   - the matrix evaluation routine; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran)
167d763cef2SBarry Smith          if A is not a function of t.
168d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
169d763cef2SBarry Smith           matrix evaluation routine (may be PETSC_NULL)
170d763cef2SBarry Smith 
171d763cef2SBarry Smith    Calling sequence of func:
17287828ca2SBarry Smith $     func (TS ts,PetscReal t,Mat *A,Mat *B,int *flag,void *ctx);
173d763cef2SBarry Smith 
174d763cef2SBarry Smith +  t - current timestep
175d763cef2SBarry Smith .  A - matrix A, where U_t = A(t) U
176d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
177d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
178d763cef2SBarry Smith           structure (same as flag in SLESSetOperators())
179d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
180d763cef2SBarry Smith 
181d763cef2SBarry Smith    Notes:
182d763cef2SBarry Smith    See SLESSetOperators() for important information about setting the flag
183d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
184d763cef2SBarry Smith 
185d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
186d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
187d763cef2SBarry Smith    completely new new matrix structure (not just different matrix elements)
188d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
189d763cef2SBarry Smith    throughout the global iterations.
190d763cef2SBarry Smith 
191d763cef2SBarry Smith    Important:
192d763cef2SBarry Smith    The user MUST call either this routine or TSSetRHSFunction().
193d763cef2SBarry Smith 
194d763cef2SBarry Smith    Level: beginner
195d763cef2SBarry Smith 
196d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, matrix
197d763cef2SBarry Smith 
198d763cef2SBarry Smith .seealso: TSSetRHSFunction()
199d763cef2SBarry Smith @*/
20087828ca2SBarry Smith int TSSetRHSMatrix(TS ts,Mat A,Mat B,int (*f)(TS,PetscReal,Mat*,Mat*,MatStructure*,void*),void *ctx)
201d763cef2SBarry Smith {
202d763cef2SBarry Smith   PetscFunctionBegin;
203d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
204184914b5SBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE);
205184914b5SBarry Smith   PetscValidHeaderSpecific(B,MAT_COOKIE);
206184914b5SBarry Smith   PetscCheckSameComm(ts,A);
207184914b5SBarry Smith   PetscCheckSameComm(ts,B);
208d763cef2SBarry Smith   if (ts->problem_type == TS_NONLINEAR) {
20929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Not for nonlinear problems; use TSSetRHSJacobian()");
210d763cef2SBarry Smith   }
211d763cef2SBarry Smith 
212000e7ae3SMatthew Knepley   ts->ops->rhsmatrix = f;
213d763cef2SBarry Smith   ts->jacP           = ctx;
214d763cef2SBarry Smith   ts->A              = A;
215d763cef2SBarry Smith   ts->B              = B;
216d763cef2SBarry Smith 
217d763cef2SBarry Smith   PetscFunctionReturn(0);
218d763cef2SBarry Smith }
219d763cef2SBarry Smith 
2204a2ae208SSatish Balay #undef __FUNCT__
2214a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
222d763cef2SBarry Smith /*@C
223d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
224d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
225d763cef2SBarry Smith 
226d763cef2SBarry Smith    Collective on TS
227d763cef2SBarry Smith 
228d763cef2SBarry Smith    Input Parameters:
229d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
230d763cef2SBarry Smith .  A   - Jacobian matrix
231d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
232d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
233d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
234d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
235d763cef2SBarry Smith 
236d763cef2SBarry Smith    Calling sequence of func:
23787828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
238d763cef2SBarry Smith 
239d763cef2SBarry Smith +  t - current timestep
240d763cef2SBarry Smith .  u - input vector
241d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
242d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
243d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
244d763cef2SBarry Smith           structure (same as flag in SLESSetOperators())
245d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
246d763cef2SBarry Smith 
247d763cef2SBarry Smith    Notes:
248d763cef2SBarry Smith    See SLESSetOperators() for important information about setting the flag
249d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
250d763cef2SBarry Smith 
251d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
252d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
253d763cef2SBarry Smith    completely new new matrix structure (not just different matrix elements)
254d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
255d763cef2SBarry Smith    throughout the global iterations.
256d763cef2SBarry Smith 
257d763cef2SBarry Smith    Level: beginner
258d763cef2SBarry Smith 
259d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
260d763cef2SBarry Smith 
261d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(),
262d763cef2SBarry Smith           SNESDefaultComputeJacobianColor()
263d763cef2SBarry Smith 
264d763cef2SBarry Smith @*/
26587828ca2SBarry Smith int TSSetRHSJacobian(TS ts,Mat A,Mat B,int (*f)(TS,PetscReal,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
266d763cef2SBarry Smith {
267d763cef2SBarry Smith   PetscFunctionBegin;
268d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
269184914b5SBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE);
270184914b5SBarry Smith   PetscValidHeaderSpecific(B,MAT_COOKIE);
271184914b5SBarry Smith   PetscCheckSameComm(ts,A);
272184914b5SBarry Smith   PetscCheckSameComm(ts,B);
273d763cef2SBarry Smith   if (ts->problem_type != TS_NONLINEAR) {
27429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Not for linear problems; use TSSetRHSMatrix()");
275d763cef2SBarry Smith   }
276d763cef2SBarry Smith 
277000e7ae3SMatthew Knepley   ts->ops->rhsjacobian = f;
278d763cef2SBarry Smith   ts->jacP             = ctx;
279d763cef2SBarry Smith   ts->A                = A;
280d763cef2SBarry Smith   ts->B                = B;
281d763cef2SBarry Smith   PetscFunctionReturn(0);
282d763cef2SBarry Smith }
283d763cef2SBarry Smith 
2844a2ae208SSatish Balay #undef __FUNCT__
2854a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSBoundaryConditions"
286d763cef2SBarry Smith /*
287d763cef2SBarry Smith    TSComputeRHSBoundaryConditions - Evaluates the boundary condition function.
288d763cef2SBarry Smith 
289d763cef2SBarry Smith    Note: If the user did not provide a function but merely a matrix,
290d763cef2SBarry Smith    this routine applies the matrix.
291d763cef2SBarry Smith */
29287828ca2SBarry Smith int TSComputeRHSBoundaryConditions(TS ts,PetscReal t,Vec x)
293d763cef2SBarry Smith {
294d763cef2SBarry Smith   int ierr;
295d763cef2SBarry Smith 
296d763cef2SBarry Smith   PetscFunctionBegin;
297d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
298d763cef2SBarry Smith   PetscValidHeader(x);
299184914b5SBarry Smith   PetscCheckSameComm(ts,x);
300d763cef2SBarry Smith 
301000e7ae3SMatthew Knepley   if (ts->ops->rhsbc) {
302d763cef2SBarry Smith     PetscStackPush("TS user boundary condition function");
303000e7ae3SMatthew Knepley     ierr = (*ts->ops->rhsbc)(ts,t,x,ts->bcP);CHKERRQ(ierr);
304d763cef2SBarry Smith     PetscStackPop;
305d763cef2SBarry Smith     PetscFunctionReturn(0);
306d763cef2SBarry Smith   }
307d763cef2SBarry Smith 
308d763cef2SBarry Smith   PetscFunctionReturn(0);
309d763cef2SBarry Smith }
310d763cef2SBarry Smith 
3114a2ae208SSatish Balay #undef __FUNCT__
3124a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSBoundaryConditions"
313d763cef2SBarry Smith /*@C
314d763cef2SBarry Smith     TSSetRHSBoundaryConditions - Sets the routine for evaluating the function,
315d763cef2SBarry Smith     boundary conditions for the function F.
316d763cef2SBarry Smith 
317d763cef2SBarry Smith     Collective on TS
318d763cef2SBarry Smith 
319d763cef2SBarry Smith     Input Parameters:
320d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
321d763cef2SBarry Smith .   f - routine for evaluating the boundary condition function
322d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
323d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
324d763cef2SBarry Smith 
325d763cef2SBarry Smith     Calling sequence of func:
32687828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec F,void *ctx);
327d763cef2SBarry Smith 
328d763cef2SBarry Smith +   t - current timestep
329d763cef2SBarry Smith .   F - function vector
330d763cef2SBarry Smith -   ctx - [optional] user-defined function context
331d763cef2SBarry Smith 
332d763cef2SBarry Smith     Level: intermediate
333d763cef2SBarry Smith 
334d763cef2SBarry Smith .keywords: TS, timestep, set, boundary conditions, function
335d763cef2SBarry Smith @*/
33687828ca2SBarry Smith int TSSetRHSBoundaryConditions(TS ts,int (*f)(TS,PetscReal,Vec,void*),void *ctx)
337d763cef2SBarry Smith {
338d763cef2SBarry Smith   PetscFunctionBegin;
339d763cef2SBarry Smith 
340d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
341d763cef2SBarry Smith   if (ts->problem_type != TS_LINEAR) {
34229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For linear problems only");
343d763cef2SBarry Smith   }
344000e7ae3SMatthew Knepley   ts->ops->rhsbc = f;
345d763cef2SBarry Smith   ts->bcP        = ctx;
346d763cef2SBarry Smith   PetscFunctionReturn(0);
347d763cef2SBarry Smith }
348d763cef2SBarry Smith 
3494a2ae208SSatish Balay #undef __FUNCT__
3504a2ae208SSatish Balay #define __FUNCT__ "TSView"
3517e2c5f70SBarry Smith /*@C
352d763cef2SBarry Smith     TSView - Prints the TS data structure.
353d763cef2SBarry Smith 
3544c49b128SBarry Smith     Collective on TS
355d763cef2SBarry Smith 
356d763cef2SBarry Smith     Input Parameters:
357d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
358d763cef2SBarry Smith -   viewer - visualization context
359d763cef2SBarry Smith 
360d763cef2SBarry Smith     Options Database Key:
361d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
362d763cef2SBarry Smith 
363d763cef2SBarry Smith     Notes:
364d763cef2SBarry Smith     The available visualization contexts include
365b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
366b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
367d763cef2SBarry Smith          output where only the first processor opens
368d763cef2SBarry Smith          the file.  All other processors send their
369d763cef2SBarry Smith          data to the first processor to print.
370d763cef2SBarry Smith 
371d763cef2SBarry Smith     The user can open an alternative visualization context with
372b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
373d763cef2SBarry Smith 
374d763cef2SBarry Smith     Level: beginner
375d763cef2SBarry Smith 
376d763cef2SBarry Smith .keywords: TS, timestep, view
377d763cef2SBarry Smith 
378b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
379d763cef2SBarry Smith @*/
380b0a32e0cSBarry Smith int TSView(TS ts,PetscViewer viewer)
381d763cef2SBarry Smith {
382d763cef2SBarry Smith   int        ierr;
383454a90a3SBarry Smith   char       *type;
3846831982aSBarry Smith   PetscTruth isascii,isstring;
385d763cef2SBarry Smith 
386d763cef2SBarry Smith   PetscFunctionBegin;
387d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
388b0a32e0cSBarry Smith   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(ts->comm);
389b0a32e0cSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE);
3906831982aSBarry Smith   PetscCheckSameComm(ts,viewer);
391fd16b177SBarry Smith 
392b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
393b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
3940f5bd95cSBarry Smith   if (isascii) {
395b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"TS Object:\n");CHKERRQ(ierr);
396454a90a3SBarry Smith     ierr = TSGetType(ts,(TSType *)&type);CHKERRQ(ierr);
397454a90a3SBarry Smith     if (type) {
398b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
399184914b5SBarry Smith     } else {
400b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not yet set\n");CHKERRQ(ierr);
401184914b5SBarry Smith     }
402000e7ae3SMatthew Knepley     if (ts->ops->view) {
403b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
404000e7ae3SMatthew Knepley       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
405b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
406d763cef2SBarry Smith     }
407b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%d\n",ts->max_steps);CHKERRQ(ierr);
408b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",ts->max_time);CHKERRQ(ierr);
409d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
410b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%d\n",ts->nonlinear_its);CHKERRQ(ierr);
411d763cef2SBarry Smith     }
412b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%d\n",ts->linear_its);CHKERRQ(ierr);
4130f5bd95cSBarry Smith   } else if (isstring) {
414454a90a3SBarry Smith     ierr = TSGetType(ts,(TSType *)&type);CHKERRQ(ierr);
415b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
416d763cef2SBarry Smith   }
417b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
418d763cef2SBarry Smith   if (ts->sles) {ierr = SLESView(ts->sles,viewer);CHKERRQ(ierr);}
419d763cef2SBarry Smith   if (ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
420b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
421d763cef2SBarry Smith   PetscFunctionReturn(0);
422d763cef2SBarry Smith }
423d763cef2SBarry Smith 
424d763cef2SBarry Smith 
4254a2ae208SSatish Balay #undef __FUNCT__
4264a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
427d763cef2SBarry Smith /*@C
428d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
429d763cef2SBarry Smith    the timesteppers.
430d763cef2SBarry Smith 
431d763cef2SBarry Smith    Collective on TS
432d763cef2SBarry Smith 
433d763cef2SBarry Smith    Input Parameters:
434d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
435d763cef2SBarry Smith -  usrP - optional user context
436d763cef2SBarry Smith 
437d763cef2SBarry Smith    Level: intermediate
438d763cef2SBarry Smith 
439d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
440d763cef2SBarry Smith 
441d763cef2SBarry Smith .seealso: TSGetApplicationContext()
442d763cef2SBarry Smith @*/
443d763cef2SBarry Smith int TSSetApplicationContext(TS ts,void *usrP)
444d763cef2SBarry Smith {
445d763cef2SBarry Smith   PetscFunctionBegin;
446d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
447d763cef2SBarry Smith   ts->user = usrP;
448d763cef2SBarry Smith   PetscFunctionReturn(0);
449d763cef2SBarry Smith }
450d763cef2SBarry Smith 
4514a2ae208SSatish Balay #undef __FUNCT__
4524a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
453d763cef2SBarry Smith /*@C
454d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
455d763cef2SBarry Smith     timestepper.
456d763cef2SBarry Smith 
457d763cef2SBarry Smith     Not Collective
458d763cef2SBarry Smith 
459d763cef2SBarry Smith     Input Parameter:
460d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
461d763cef2SBarry Smith 
462d763cef2SBarry Smith     Output Parameter:
463d763cef2SBarry Smith .   usrP - user context
464d763cef2SBarry Smith 
465d763cef2SBarry Smith     Level: intermediate
466d763cef2SBarry Smith 
467d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
468d763cef2SBarry Smith 
469d763cef2SBarry Smith .seealso: TSSetApplicationContext()
470d763cef2SBarry Smith @*/
471d763cef2SBarry Smith int TSGetApplicationContext(TS ts,void **usrP)
472d763cef2SBarry Smith {
473d763cef2SBarry Smith   PetscFunctionBegin;
474d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
475d763cef2SBarry Smith   *usrP = ts->user;
476d763cef2SBarry Smith   PetscFunctionReturn(0);
477d763cef2SBarry Smith }
478d763cef2SBarry Smith 
4794a2ae208SSatish Balay #undef __FUNCT__
4804a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
481d763cef2SBarry Smith /*@
482d763cef2SBarry Smith    TSGetTimeStepNumber - Gets the current number of timesteps.
483d763cef2SBarry Smith 
484d763cef2SBarry Smith    Not Collective
485d763cef2SBarry Smith 
486d763cef2SBarry Smith    Input Parameter:
487d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
488d763cef2SBarry Smith 
489d763cef2SBarry Smith    Output Parameter:
490d763cef2SBarry Smith .  iter - number steps so far
491d763cef2SBarry Smith 
492d763cef2SBarry Smith    Level: intermediate
493d763cef2SBarry Smith 
494d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
495d763cef2SBarry Smith @*/
496d763cef2SBarry Smith int TSGetTimeStepNumber(TS ts,int* iter)
497d763cef2SBarry Smith {
498d763cef2SBarry Smith   PetscFunctionBegin;
499d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
500d763cef2SBarry Smith   *iter = ts->steps;
501d763cef2SBarry Smith   PetscFunctionReturn(0);
502d763cef2SBarry Smith }
503d763cef2SBarry Smith 
5044a2ae208SSatish Balay #undef __FUNCT__
5054a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
506d763cef2SBarry Smith /*@
507d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
508d763cef2SBarry Smith    as well as the initial time.
509d763cef2SBarry Smith 
510d763cef2SBarry Smith    Collective on TS
511d763cef2SBarry Smith 
512d763cef2SBarry Smith    Input Parameters:
513d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
514d763cef2SBarry Smith .  initial_time - the initial time
515d763cef2SBarry Smith -  time_step - the size of the timestep
516d763cef2SBarry Smith 
517d763cef2SBarry Smith    Level: intermediate
518d763cef2SBarry Smith 
519d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
520d763cef2SBarry Smith 
521d763cef2SBarry Smith .keywords: TS, set, initial, timestep
522d763cef2SBarry Smith @*/
52387828ca2SBarry Smith int TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
524d763cef2SBarry Smith {
525d763cef2SBarry Smith   PetscFunctionBegin;
526d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
527d763cef2SBarry Smith   ts->time_step         = time_step;
528d763cef2SBarry Smith   ts->initial_time_step = time_step;
529d763cef2SBarry Smith   ts->ptime             = initial_time;
530d763cef2SBarry Smith   PetscFunctionReturn(0);
531d763cef2SBarry Smith }
532d763cef2SBarry Smith 
5334a2ae208SSatish Balay #undef __FUNCT__
5344a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
535d763cef2SBarry Smith /*@
536d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
537d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
538d763cef2SBarry Smith 
539d763cef2SBarry Smith    Collective on TS
540d763cef2SBarry Smith 
541d763cef2SBarry Smith    Input Parameters:
542d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
543d763cef2SBarry Smith -  time_step - the size of the timestep
544d763cef2SBarry Smith 
545d763cef2SBarry Smith    Level: intermediate
546d763cef2SBarry Smith 
547d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
548d763cef2SBarry Smith 
549d763cef2SBarry Smith .keywords: TS, set, timestep
550d763cef2SBarry Smith @*/
55187828ca2SBarry Smith int TSSetTimeStep(TS ts,PetscReal time_step)
552d763cef2SBarry Smith {
553d763cef2SBarry Smith   PetscFunctionBegin;
554d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
555d763cef2SBarry Smith   ts->time_step = time_step;
556d763cef2SBarry Smith   PetscFunctionReturn(0);
557d763cef2SBarry Smith }
558d763cef2SBarry Smith 
5594a2ae208SSatish Balay #undef __FUNCT__
5604a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
561d763cef2SBarry Smith /*@
562d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
563d763cef2SBarry Smith 
564d763cef2SBarry Smith    Not Collective
565d763cef2SBarry Smith 
566d763cef2SBarry Smith    Input Parameter:
567d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
568d763cef2SBarry Smith 
569d763cef2SBarry Smith    Output Parameter:
570d763cef2SBarry Smith .  dt - the current timestep size
571d763cef2SBarry Smith 
572d763cef2SBarry Smith    Level: intermediate
573d763cef2SBarry Smith 
574d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
575d763cef2SBarry Smith 
576d763cef2SBarry Smith .keywords: TS, get, timestep
577d763cef2SBarry Smith @*/
57887828ca2SBarry Smith int TSGetTimeStep(TS ts,PetscReal* dt)
579d763cef2SBarry Smith {
580d763cef2SBarry Smith   PetscFunctionBegin;
581d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
582d763cef2SBarry Smith   *dt = ts->time_step;
583d763cef2SBarry Smith   PetscFunctionReturn(0);
584d763cef2SBarry Smith }
585d763cef2SBarry Smith 
5864a2ae208SSatish Balay #undef __FUNCT__
5874a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
588d763cef2SBarry Smith /*@C
589d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
590d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
591d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
592d763cef2SBarry Smith    the solution at the next timestep has been calculated.
593d763cef2SBarry Smith 
594d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
595d763cef2SBarry Smith 
596d763cef2SBarry Smith    Input Parameter:
597d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
598d763cef2SBarry Smith 
599d763cef2SBarry Smith    Output Parameter:
600d763cef2SBarry Smith .  v - the vector containing the solution
601d763cef2SBarry Smith 
602d763cef2SBarry Smith    Level: intermediate
603d763cef2SBarry Smith 
604d763cef2SBarry Smith .seealso: TSGetTimeStep()
605d763cef2SBarry Smith 
606d763cef2SBarry Smith .keywords: TS, timestep, get, solution
607d763cef2SBarry Smith @*/
608d763cef2SBarry Smith int TSGetSolution(TS ts,Vec *v)
609d763cef2SBarry Smith {
610d763cef2SBarry Smith   PetscFunctionBegin;
611d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
612d763cef2SBarry Smith   *v = ts->vec_sol_always;
613d763cef2SBarry Smith   PetscFunctionReturn(0);
614d763cef2SBarry Smith }
615d763cef2SBarry Smith 
6164a2ae208SSatish Balay #undef __FUNCT__
6174a2ae208SSatish Balay #define __FUNCT__ "TSPublish_Petsc"
618454a90a3SBarry Smith static int TSPublish_Petsc(PetscObject obj)
619d763cef2SBarry Smith {
620aa482453SBarry Smith #if defined(PETSC_HAVE_AMS)
621454a90a3SBarry Smith   TS   v = (TS) obj;
622d763cef2SBarry Smith   int  ierr;
62343d6d2cbSBarry Smith #endif
624d763cef2SBarry Smith 
625d763cef2SBarry Smith   PetscFunctionBegin;
626d763cef2SBarry Smith 
62743d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS)
628d763cef2SBarry Smith   /* if it is already published then return */
629d763cef2SBarry Smith   if (v->amem >=0) PetscFunctionReturn(0);
630d763cef2SBarry Smith 
631454a90a3SBarry Smith   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
632d763cef2SBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Step",&v->steps,1,AMS_INT,AMS_READ,
633d763cef2SBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
634d763cef2SBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Time",&v->ptime,1,AMS_DOUBLE,AMS_READ,
635d763cef2SBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
636d763cef2SBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"CurrentTimeStep",&v->time_step,1,
637d763cef2SBarry Smith                                AMS_DOUBLE,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
638454a90a3SBarry Smith   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
639d763cef2SBarry Smith #endif
640d763cef2SBarry Smith   PetscFunctionReturn(0);
641d763cef2SBarry Smith }
642d763cef2SBarry Smith 
643d763cef2SBarry Smith /* -----------------------------------------------------------*/
644d763cef2SBarry Smith 
6454a2ae208SSatish Balay #undef __FUNCT__
6464a2ae208SSatish Balay #define __FUNCT__ "TSCreate"
647d763cef2SBarry Smith /*@C
648d763cef2SBarry Smith    TSCreate - Creates a timestepper context.
649d763cef2SBarry Smith 
650d763cef2SBarry Smith    Collective on MPI_Comm
651d763cef2SBarry Smith 
652d763cef2SBarry Smith    Input Parameter:
653d763cef2SBarry Smith +  comm - MPI communicator
654d763cef2SBarry Smith -  type - One of  TS_LINEAR,TS_NONLINEAR
655d763cef2SBarry Smith    where these types refer to problems of the forms
656d763cef2SBarry Smith .vb
657d763cef2SBarry Smith          U_t = A U
658d763cef2SBarry Smith          U_t = A(t) U
659d763cef2SBarry Smith          U_t = F(t,U)
660d763cef2SBarry Smith .ve
661d763cef2SBarry Smith 
662d763cef2SBarry Smith    Output Parameter:
663000e7ae3SMatthew Knepley .  ts - the new TS context
664d763cef2SBarry Smith 
665d763cef2SBarry Smith    Level: beginner
666d763cef2SBarry Smith 
667d763cef2SBarry Smith .keywords: TS, timestep, create, context
668d763cef2SBarry Smith 
669435da068SBarry Smith .seealso: TSSetUp(), TSStep(), TSDestroy(), TSProblemType, TS
670d763cef2SBarry Smith @*/
671000e7ae3SMatthew Knepley int TSCreate(MPI_Comm comm, TSProblemType problemtype, TS *ts)
672d763cef2SBarry Smith {
673000e7ae3SMatthew Knepley   TS  t;
674000e7ae3SMatthew Knepley   int ierr;
675d763cef2SBarry Smith 
676d763cef2SBarry Smith   PetscFunctionBegin;
677000e7ae3SMatthew Knepley   *ts = PETSC_NULL;
678000e7ae3SMatthew Knepley   PetscValidPointer(ts);
679000e7ae3SMatthew Knepley   PetscHeaderCreate(t, _p_TS, struct _TSOps, TS_COOKIE, -1, "TS", comm, TSDestroy, TSView);
680000e7ae3SMatthew Knepley   PetscLogObjectCreate(t);
681000e7ae3SMatthew Knepley   PetscLogObjectMemory(t, sizeof(struct _p_TS));
682000e7ae3SMatthew Knepley   ierr = PetscMemzero(t->ops, sizeof(struct _TSOps));                                                     CHKERRQ(ierr);
683000e7ae3SMatthew Knepley   t->bops->publish      = TSPublish_Petsc;
684000e7ae3SMatthew Knepley   t->type_name          = PETSC_NULL;
685d763cef2SBarry Smith 
686000e7ae3SMatthew Knepley   t->problem_type       = problemtype;
687000e7ae3SMatthew Knepley   t->vec_sol            = PETSC_NULL;
688000e7ae3SMatthew Knepley   t->vec_sol_always     = PETSC_NULL;
689000e7ae3SMatthew Knepley   t->numbermonitors     = 0;
690000e7ae3SMatthew Knepley   t->isGTS              = PETSC_FALSE;
6917f5a67d6SMatthew Knepley   t->isExplicit         = PETSC_NULL;
692000e7ae3SMatthew Knepley   t->Iindex             = PETSC_NULL;
693000e7ae3SMatthew Knepley   t->sles               = PETSC_NULL;
694000e7ae3SMatthew Knepley   t->A                  = PETSC_NULL;
695000e7ae3SMatthew Knepley   t->B                  = PETSC_NULL;
696000e7ae3SMatthew Knepley   t->snes               = PETSC_NULL;
697000e7ae3SMatthew Knepley   t->funP               = PETSC_NULL;
698000e7ae3SMatthew Knepley   t->jacP               = PETSC_NULL;
699000e7ae3SMatthew Knepley   t->setupcalled        = 0;
700000e7ae3SMatthew Knepley   t->data               = PETSC_NULL;
701000e7ae3SMatthew Knepley   t->user               = PETSC_NULL;
702000e7ae3SMatthew Knepley   t->max_steps          = 5000;
703000e7ae3SMatthew Knepley   t->max_time           = 5.0;
704000e7ae3SMatthew Knepley   t->time_step          = .1;
705000e7ae3SMatthew Knepley   t->time_step_old      = t->time_step;
706000e7ae3SMatthew Knepley   t->initial_time_step  = t->time_step;
707000e7ae3SMatthew Knepley   t->steps              = 0;
708000e7ae3SMatthew Knepley   t->ptime              = 0.0;
709000e7ae3SMatthew Knepley   t->linear_its         = 0;
710000e7ae3SMatthew Knepley   t->nonlinear_its      = 0;
711000e7ae3SMatthew Knepley   t->work               = PETSC_NULL;
712000e7ae3SMatthew Knepley   t->nwork              = 0;
713000e7ae3SMatthew Knepley   t->ops->applymatrixbc = TSDefaultSystemMatrixBC;
714000e7ae3SMatthew Knepley   t->ops->applyrhsbc    = TSDefaultRhsBC;
715000e7ae3SMatthew Knepley   t->ops->applysolbc    = TSDefaultSolutionBC;
716000e7ae3SMatthew Knepley   t->ops->prestep       = TSDefaultPreStep;
717000e7ae3SMatthew Knepley   t->ops->update        = TSDefaultUpdate;
718000e7ae3SMatthew Knepley   t->ops->poststep      = TSDefaultPostStep;
719000e7ae3SMatthew Knepley   t->ops->reform        = PETSC_NULL;
720000e7ae3SMatthew Knepley   t->ops->reallocate    = PETSC_NULL;
721000e7ae3SMatthew Knepley   t->ops->serialize     = PETSC_NULL;
722000e7ae3SMatthew Knepley 
723000e7ae3SMatthew Knepley   *ts = t;
724d763cef2SBarry Smith   PetscFunctionReturn(0);
725d763cef2SBarry Smith }
726d763cef2SBarry Smith 
727d763cef2SBarry Smith /* ----- Routines to initialize and destroy a timestepper ---- */
728d763cef2SBarry Smith 
7294a2ae208SSatish Balay #undef __FUNCT__
7304a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
731d763cef2SBarry Smith /*@
732d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
733d763cef2SBarry Smith    of a timestepper.
734d763cef2SBarry Smith 
735d763cef2SBarry Smith    Collective on TS
736d763cef2SBarry Smith 
737d763cef2SBarry Smith    Input Parameter:
738d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
739d763cef2SBarry Smith 
740d763cef2SBarry Smith    Notes:
741d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
742d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
743d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
744d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
745d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
746d763cef2SBarry Smith 
747d763cef2SBarry Smith    Level: advanced
748d763cef2SBarry Smith 
749d763cef2SBarry Smith .keywords: TS, timestep, setup
750d763cef2SBarry Smith 
751d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
752d763cef2SBarry Smith @*/
753d763cef2SBarry Smith int TSSetUp(TS ts)
754d763cef2SBarry Smith {
755d763cef2SBarry Smith   int ierr;
756d763cef2SBarry Smith 
757d763cef2SBarry Smith   PetscFunctionBegin;
758d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
75929bbc08cSBarry Smith   if (!ts->vec_sol) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
760d763cef2SBarry Smith   if (!ts->type_name) {
761d763cef2SBarry Smith     ierr = TSSetType(ts,TS_EULER);CHKERRQ(ierr);
762d763cef2SBarry Smith   }
763000e7ae3SMatthew Knepley   ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
764d763cef2SBarry Smith   ts->setupcalled = 1;
765d763cef2SBarry Smith   PetscFunctionReturn(0);
766d763cef2SBarry Smith }
767d763cef2SBarry Smith 
7684a2ae208SSatish Balay #undef __FUNCT__
7694a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
770d763cef2SBarry Smith /*@C
771d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
772d763cef2SBarry Smith    with TSCreate().
773d763cef2SBarry Smith 
774d763cef2SBarry Smith    Collective on TS
775d763cef2SBarry Smith 
776d763cef2SBarry Smith    Input Parameter:
777d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
778d763cef2SBarry Smith 
779d763cef2SBarry Smith    Level: beginner
780d763cef2SBarry Smith 
781d763cef2SBarry Smith .keywords: TS, timestepper, destroy
782d763cef2SBarry Smith 
783d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
784d763cef2SBarry Smith @*/
785d763cef2SBarry Smith int TSDestroy(TS ts)
786d763cef2SBarry Smith {
787329f5518SBarry Smith   int ierr,i;
788d763cef2SBarry Smith 
789d763cef2SBarry Smith   PetscFunctionBegin;
790d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
791d763cef2SBarry Smith   if (--ts->refct > 0) PetscFunctionReturn(0);
792d763cef2SBarry Smith 
793be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
7940f5bd95cSBarry Smith   ierr = PetscObjectDepublish(ts);CHKERRQ(ierr);
795be0abb6dSBarry Smith 
796d763cef2SBarry Smith   if (ts->sles) {ierr = SLESDestroy(ts->sles);CHKERRQ(ierr);}
797d763cef2SBarry Smith   if (ts->snes) {ierr = SNESDestroy(ts->snes);CHKERRQ(ierr);}
798000e7ae3SMatthew Knepley   ierr = (*(ts)->ops->destroy)(ts);CHKERRQ(ierr);
799329f5518SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
800329f5518SBarry Smith     if (ts->mdestroy[i]) {
801329f5518SBarry Smith       ierr = (*ts->mdestroy[i])(ts->monitorcontext[i]);CHKERRQ(ierr);
802329f5518SBarry Smith     }
803329f5518SBarry Smith   }
804b0a32e0cSBarry Smith   PetscLogObjectDestroy((PetscObject)ts);
805d763cef2SBarry Smith   PetscHeaderDestroy((PetscObject)ts);
806d763cef2SBarry Smith   PetscFunctionReturn(0);
807d763cef2SBarry Smith }
808d763cef2SBarry Smith 
8094a2ae208SSatish Balay #undef __FUNCT__
8104a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
811d763cef2SBarry Smith /*@C
812d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
813d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
814d763cef2SBarry Smith 
815d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
816d763cef2SBarry Smith 
817d763cef2SBarry Smith    Input Parameter:
818d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
819d763cef2SBarry Smith 
820d763cef2SBarry Smith    Output Parameter:
821d763cef2SBarry Smith .  snes - the nonlinear solver context
822d763cef2SBarry Smith 
823d763cef2SBarry Smith    Notes:
824d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
825d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
826d763cef2SBarry Smith    SLES, KSP, and PC contexts as well.
827d763cef2SBarry Smith 
828d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
829d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
830d763cef2SBarry Smith 
831d763cef2SBarry Smith    Level: beginner
832d763cef2SBarry Smith 
833d763cef2SBarry Smith .keywords: timestep, get, SNES
834d763cef2SBarry Smith @*/
835d763cef2SBarry Smith int TSGetSNES(TS ts,SNES *snes)
836d763cef2SBarry Smith {
837d763cef2SBarry Smith   PetscFunctionBegin;
838d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
83929bbc08cSBarry Smith   if (ts->problem_type == TS_LINEAR) SETERRQ(PETSC_ERR_ARG_WRONG,"Nonlinear only; use TSGetSLES()");
840d763cef2SBarry Smith   *snes = ts->snes;
841d763cef2SBarry Smith   PetscFunctionReturn(0);
842d763cef2SBarry Smith }
843d763cef2SBarry Smith 
8444a2ae208SSatish Balay #undef __FUNCT__
8454a2ae208SSatish Balay #define __FUNCT__ "TSGetSLES"
846d763cef2SBarry Smith /*@C
847d763cef2SBarry Smith    TSGetSLES - Returns the SLES (linear solver) associated with
848d763cef2SBarry Smith    a TS (timestepper) context.
849d763cef2SBarry Smith 
850d763cef2SBarry Smith    Not Collective, but SLES is parallel if TS is parallel
851d763cef2SBarry Smith 
852d763cef2SBarry Smith    Input Parameter:
853d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
854d763cef2SBarry Smith 
855d763cef2SBarry Smith    Output Parameter:
856d763cef2SBarry Smith .  sles - the nonlinear solver context
857d763cef2SBarry Smith 
858d763cef2SBarry Smith    Notes:
859d763cef2SBarry Smith    The user can then directly manipulate the SLES context to set various
860d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
861d763cef2SBarry Smith    KSP and PC contexts as well.
862d763cef2SBarry Smith 
863d763cef2SBarry Smith    TSGetSLES() does not work for integrators that do not use SLES;
864d763cef2SBarry Smith    in this case TSGetSLES() returns PETSC_NULL in sles.
865d763cef2SBarry Smith 
866d763cef2SBarry Smith    Level: beginner
867d763cef2SBarry Smith 
868d763cef2SBarry Smith .keywords: timestep, get, SLES
869d763cef2SBarry Smith @*/
870d763cef2SBarry Smith int TSGetSLES(TS ts,SLES *sles)
871d763cef2SBarry Smith {
872d763cef2SBarry Smith   PetscFunctionBegin;
873d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
87429bbc08cSBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
875d763cef2SBarry Smith   *sles = ts->sles;
876d763cef2SBarry Smith   PetscFunctionReturn(0);
877d763cef2SBarry Smith }
878d763cef2SBarry Smith 
879d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
880d763cef2SBarry Smith 
8814a2ae208SSatish Balay #undef __FUNCT__
8824a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
883d763cef2SBarry Smith /*@
884d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
885d763cef2SBarry Smith    maximum time for iteration.
886d763cef2SBarry Smith 
887d763cef2SBarry Smith    Collective on TS
888d763cef2SBarry Smith 
889d763cef2SBarry Smith    Input Parameters:
890d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
891d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
892d763cef2SBarry Smith -  maxtime - final time to iterate to
893d763cef2SBarry Smith 
894d763cef2SBarry Smith    Options Database Keys:
895d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
896d763cef2SBarry Smith .  -ts_max_time <maxtime> - Sets maxtime
897d763cef2SBarry Smith 
898d763cef2SBarry Smith    Notes:
899d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
900d763cef2SBarry Smith 
901d763cef2SBarry Smith    Level: intermediate
902d763cef2SBarry Smith 
903d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
904d763cef2SBarry Smith @*/
90587828ca2SBarry Smith int TSSetDuration(TS ts,int maxsteps,PetscReal maxtime)
906d763cef2SBarry Smith {
907d763cef2SBarry Smith   PetscFunctionBegin;
908d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
909d763cef2SBarry Smith   ts->max_steps = maxsteps;
910d763cef2SBarry Smith   ts->max_time  = maxtime;
911d763cef2SBarry Smith   PetscFunctionReturn(0);
912d763cef2SBarry Smith }
913d763cef2SBarry Smith 
9144a2ae208SSatish Balay #undef __FUNCT__
9154a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
916d763cef2SBarry Smith /*@
917d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
918d763cef2SBarry Smith    for use by the TS routines.
919d763cef2SBarry Smith 
920d763cef2SBarry Smith    Collective on TS and Vec
921d763cef2SBarry Smith 
922d763cef2SBarry Smith    Input Parameters:
923d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
924d763cef2SBarry Smith -  x - the solution vector
925d763cef2SBarry Smith 
926d763cef2SBarry Smith    Level: beginner
927d763cef2SBarry Smith 
928d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
929d763cef2SBarry Smith @*/
930d763cef2SBarry Smith int TSSetSolution(TS ts,Vec x)
931d763cef2SBarry Smith {
932d763cef2SBarry Smith   PetscFunctionBegin;
933d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
934d763cef2SBarry Smith   ts->vec_sol        = ts->vec_sol_always = x;
935d763cef2SBarry Smith   PetscFunctionReturn(0);
936d763cef2SBarry Smith }
937d763cef2SBarry Smith 
938e74ef692SMatthew Knepley #undef __FUNCT__
939e74ef692SMatthew Knepley #define __FUNCT__ "TSSetRhsBC"
940000e7ae3SMatthew Knepley /*@
941000e7ae3SMatthew Knepley   TSSetRhsBC - Sets the function which applies boundary conditions
942000e7ae3SMatthew Knepley   to the Rhs of each system.
943000e7ae3SMatthew Knepley 
944000e7ae3SMatthew Knepley   Collective on TS
945000e7ae3SMatthew Knepley 
946000e7ae3SMatthew Knepley   Input Parameters:
947000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
948000e7ae3SMatthew Knepley - func - The function
949000e7ae3SMatthew Knepley 
950000e7ae3SMatthew Knepley   Calling sequence of func:
951000e7ae3SMatthew Knepley . func (TS ts, Vec rhs, void *ctx);
952000e7ae3SMatthew Knepley 
953000e7ae3SMatthew Knepley + rhs - The current rhs vector
954000e7ae3SMatthew Knepley - ctx - The user-context
955000e7ae3SMatthew Knepley 
956000e7ae3SMatthew Knepley   Level: intermediate
957000e7ae3SMatthew Knepley 
958000e7ae3SMatthew Knepley .keywords: TS, Rhs, boundary conditions
959000e7ae3SMatthew Knepley @*/
960000e7ae3SMatthew Knepley int TSSetRhsBC(TS ts, int (*func)(TS, Vec, void *))
961000e7ae3SMatthew Knepley {
962000e7ae3SMatthew Knepley   PetscFunctionBegin;
963000e7ae3SMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
964000e7ae3SMatthew Knepley   ts->ops->applyrhsbc = func;
965000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
966000e7ae3SMatthew Knepley }
967000e7ae3SMatthew Knepley 
968e74ef692SMatthew Knepley #undef __FUNCT__
969e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultRhsBC"
970000e7ae3SMatthew Knepley /*@
971000e7ae3SMatthew Knepley   TSDefaultRhsBC - The default boundary condition function which does nothing.
972000e7ae3SMatthew Knepley 
973000e7ae3SMatthew Knepley   Collective on TS
974000e7ae3SMatthew Knepley 
975000e7ae3SMatthew Knepley   Input Parameters:
976000e7ae3SMatthew Knepley + ts  - The TS context obtained from TSCreate()
977000e7ae3SMatthew Knepley . rhs - The Rhs
978000e7ae3SMatthew Knepley - ctx - The user-context
979000e7ae3SMatthew Knepley 
980000e7ae3SMatthew Knepley   Level: developer
981000e7ae3SMatthew Knepley 
982000e7ae3SMatthew Knepley .keywords: TS, Rhs, boundary conditions
983000e7ae3SMatthew Knepley @*/
984000e7ae3SMatthew Knepley int TSDefaultRhsBC(TS ts,  Vec rhs, void *ctx)
985000e7ae3SMatthew Knepley {
986000e7ae3SMatthew Knepley   PetscFunctionBegin;
987000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
988000e7ae3SMatthew Knepley }
989000e7ae3SMatthew Knepley 
990e74ef692SMatthew Knepley #undef __FUNCT__
991e74ef692SMatthew Knepley #define __FUNCT__ "TSSetSystemMatrixBC"
992000e7ae3SMatthew Knepley /*@
993000e7ae3SMatthew Knepley   TSSetSystemMatrixBC - Sets the function which applies boundary conditions
994000e7ae3SMatthew Knepley   to the system matrix and preconditioner of each system.
995000e7ae3SMatthew Knepley 
996000e7ae3SMatthew Knepley   Collective on TS
997000e7ae3SMatthew Knepley 
998000e7ae3SMatthew Knepley   Input Parameters:
999000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1000000e7ae3SMatthew Knepley - func - The function
1001000e7ae3SMatthew Knepley 
1002000e7ae3SMatthew Knepley   Calling sequence of func:
1003000e7ae3SMatthew Knepley . func (TS ts, Mat A, Mat B, void *ctx);
1004000e7ae3SMatthew Knepley 
1005000e7ae3SMatthew Knepley + A   - The current system matrix
1006000e7ae3SMatthew Knepley . B   - The current preconditioner
1007000e7ae3SMatthew Knepley - ctx - The user-context
1008000e7ae3SMatthew Knepley 
1009000e7ae3SMatthew Knepley   Level: intermediate
1010000e7ae3SMatthew Knepley 
1011000e7ae3SMatthew Knepley .keywords: TS, System matrix, boundary conditions
1012000e7ae3SMatthew Knepley @*/
1013000e7ae3SMatthew Knepley int TSSetSystemMatrixBC(TS ts, int (*func)(TS, Mat, Mat, void *))
1014000e7ae3SMatthew Knepley {
1015000e7ae3SMatthew Knepley   PetscFunctionBegin;
1016000e7ae3SMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
1017000e7ae3SMatthew Knepley   ts->ops->applymatrixbc = func;
1018000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1019000e7ae3SMatthew Knepley }
1020000e7ae3SMatthew Knepley 
1021e74ef692SMatthew Knepley #undef __FUNCT__
1022e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultSystemMatrixBC"
1023000e7ae3SMatthew Knepley /*@
1024000e7ae3SMatthew Knepley   TSDefaultSystemMatrixBC - The default boundary condition function which
1025000e7ae3SMatthew Knepley   does nothing.
1026000e7ae3SMatthew Knepley 
1027000e7ae3SMatthew Knepley   Collective on TS
1028000e7ae3SMatthew Knepley 
1029000e7ae3SMatthew Knepley   Input Parameters:
1030000e7ae3SMatthew Knepley + ts  - The TS context obtained from TSCreate()
1031000e7ae3SMatthew Knepley . A   - The system matrix
1032000e7ae3SMatthew Knepley . B   - The preconditioner
1033000e7ae3SMatthew Knepley - ctx - The user-context
1034000e7ae3SMatthew Knepley 
1035000e7ae3SMatthew Knepley   Level: developer
1036000e7ae3SMatthew Knepley 
1037000e7ae3SMatthew Knepley .keywords: TS, System matrix, boundary conditions
1038000e7ae3SMatthew Knepley @*/
1039000e7ae3SMatthew Knepley int TSDefaultSystemMatrixBC(TS ts, Mat A, Mat B, void *ctx)
1040000e7ae3SMatthew Knepley {
1041000e7ae3SMatthew Knepley   PetscFunctionBegin;
1042000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1043000e7ae3SMatthew Knepley }
1044000e7ae3SMatthew Knepley 
1045e74ef692SMatthew Knepley #undef __FUNCT__
1046e74ef692SMatthew Knepley #define __FUNCT__ "TSSetSolutionBC"
1047000e7ae3SMatthew Knepley /*@
1048000e7ae3SMatthew Knepley   TSSetSolutionBC - Sets the function which applies boundary conditions
1049000e7ae3SMatthew Knepley   to the solution of each system. This is necessary in nonlinear systems
1050000e7ae3SMatthew Knepley   which time dependent boundary conditions.
1051000e7ae3SMatthew Knepley 
1052000e7ae3SMatthew Knepley   Collective on TS
1053000e7ae3SMatthew Knepley 
1054000e7ae3SMatthew Knepley   Input Parameters:
1055000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1056000e7ae3SMatthew Knepley - func - The function
1057000e7ae3SMatthew Knepley 
1058000e7ae3SMatthew Knepley   Calling sequence of func:
1059000e7ae3SMatthew Knepley . func (TS ts, Vec rsol, void *ctx);
1060000e7ae3SMatthew Knepley 
1061000e7ae3SMatthew Knepley + sol - The current solution vector
1062000e7ae3SMatthew Knepley - ctx - The user-context
1063000e7ae3SMatthew Knepley 
1064000e7ae3SMatthew Knepley   Level: intermediate
1065000e7ae3SMatthew Knepley 
1066000e7ae3SMatthew Knepley .keywords: TS, solution, boundary conditions
1067000e7ae3SMatthew Knepley @*/
1068000e7ae3SMatthew Knepley int TSSetSolutionBC(TS ts, int (*func)(TS, Vec, void *))
1069000e7ae3SMatthew Knepley {
1070000e7ae3SMatthew Knepley   PetscFunctionBegin;
1071000e7ae3SMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
1072000e7ae3SMatthew Knepley   ts->ops->applysolbc = func;
1073000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1074000e7ae3SMatthew Knepley }
1075000e7ae3SMatthew Knepley 
1076e74ef692SMatthew Knepley #undef __FUNCT__
1077e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultSolutionBC"
1078000e7ae3SMatthew Knepley /*@
1079000e7ae3SMatthew Knepley   TSDefaultSolutionBC - The default boundary condition function which
1080000e7ae3SMatthew Knepley   does nothing.
1081000e7ae3SMatthew Knepley 
1082000e7ae3SMatthew Knepley   Collective on TS
1083000e7ae3SMatthew Knepley 
1084000e7ae3SMatthew Knepley   Input Parameters:
1085000e7ae3SMatthew Knepley + ts  - The TS context obtained from TSCreate()
1086000e7ae3SMatthew Knepley . sol - The solution
1087000e7ae3SMatthew Knepley - ctx - The user-context
1088000e7ae3SMatthew Knepley 
1089000e7ae3SMatthew Knepley   Level: developer
1090000e7ae3SMatthew Knepley 
1091000e7ae3SMatthew Knepley .keywords: TS, solution, boundary conditions
1092000e7ae3SMatthew Knepley @*/
1093000e7ae3SMatthew Knepley int TSDefaultSolutionBC(TS ts, Vec sol, void *ctx)
1094000e7ae3SMatthew Knepley {
1095000e7ae3SMatthew Knepley   PetscFunctionBegin;
1096000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1097000e7ae3SMatthew Knepley }
1098000e7ae3SMatthew Knepley 
1099e74ef692SMatthew Knepley #undef __FUNCT__
1100e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1101000e7ae3SMatthew Knepley /*@
1102000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
1103000e7ae3SMatthew Knepley   called once at the beginning of time stepping.
1104000e7ae3SMatthew Knepley 
1105000e7ae3SMatthew Knepley   Collective on TS
1106000e7ae3SMatthew Knepley 
1107000e7ae3SMatthew Knepley   Input Parameters:
1108000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1109000e7ae3SMatthew Knepley - func - The function
1110000e7ae3SMatthew Knepley 
1111000e7ae3SMatthew Knepley   Calling sequence of func:
1112000e7ae3SMatthew Knepley . func (TS ts);
1113000e7ae3SMatthew Knepley 
1114000e7ae3SMatthew Knepley   Level: intermediate
1115000e7ae3SMatthew Knepley 
1116000e7ae3SMatthew Knepley .keywords: TS, timestep
1117000e7ae3SMatthew Knepley @*/
1118000e7ae3SMatthew Knepley int TSSetPreStep(TS ts, int (*func)(TS))
1119000e7ae3SMatthew Knepley {
1120000e7ae3SMatthew Knepley   PetscFunctionBegin;
1121000e7ae3SMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
1122000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1123000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1124000e7ae3SMatthew Knepley }
1125000e7ae3SMatthew Knepley 
1126e74ef692SMatthew Knepley #undef __FUNCT__
1127e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultPreStep"
1128000e7ae3SMatthew Knepley /*@
1129000e7ae3SMatthew Knepley   TSDefaultPreStep - The default pre-stepping function which does nothing.
1130000e7ae3SMatthew Knepley 
1131000e7ae3SMatthew Knepley   Collective on TS
1132000e7ae3SMatthew Knepley 
1133000e7ae3SMatthew Knepley   Input Parameters:
1134000e7ae3SMatthew Knepley . ts  - The TS context obtained from TSCreate()
1135000e7ae3SMatthew Knepley 
1136000e7ae3SMatthew Knepley   Level: developer
1137000e7ae3SMatthew Knepley 
1138000e7ae3SMatthew Knepley .keywords: TS, timestep
1139000e7ae3SMatthew Knepley @*/
1140000e7ae3SMatthew Knepley int TSDefaultPreStep(TS ts)
1141000e7ae3SMatthew Knepley {
1142000e7ae3SMatthew Knepley   PetscFunctionBegin;
1143000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1144000e7ae3SMatthew Knepley }
1145000e7ae3SMatthew Knepley 
1146e74ef692SMatthew Knepley #undef __FUNCT__
1147e74ef692SMatthew Knepley #define __FUNCT__ "TSSetUpdate"
1148000e7ae3SMatthew Knepley /*@
1149000e7ae3SMatthew Knepley   TSSetUpdate - Sets the general-purpose update function called
1150000e7ae3SMatthew Knepley   at the beginning of every time step. This function can change
1151000e7ae3SMatthew Knepley   the time step.
1152000e7ae3SMatthew Knepley 
1153000e7ae3SMatthew Knepley   Collective on TS
1154000e7ae3SMatthew Knepley 
1155000e7ae3SMatthew Knepley   Input Parameters:
1156000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1157000e7ae3SMatthew Knepley - func - The function
1158000e7ae3SMatthew Knepley 
1159000e7ae3SMatthew Knepley   Calling sequence of func:
1160000e7ae3SMatthew Knepley . func (TS ts, double t, double *dt);
1161000e7ae3SMatthew Knepley 
1162000e7ae3SMatthew Knepley + t   - The current time
1163000e7ae3SMatthew Knepley - dt  - The current time step
1164000e7ae3SMatthew Knepley 
1165000e7ae3SMatthew Knepley   Level: intermediate
1166000e7ae3SMatthew Knepley 
1167000e7ae3SMatthew Knepley .keywords: TS, update, timestep
1168000e7ae3SMatthew Knepley @*/
1169000e7ae3SMatthew Knepley int TSSetUpdate(TS ts, int (*func)(TS, double, double *))
1170000e7ae3SMatthew Knepley {
1171000e7ae3SMatthew Knepley   PetscFunctionBegin;
1172000e7ae3SMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
1173000e7ae3SMatthew Knepley   ts->ops->update = func;
1174000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1175000e7ae3SMatthew Knepley }
1176000e7ae3SMatthew Knepley 
1177e74ef692SMatthew Knepley #undef __FUNCT__
1178e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultUpdate"
1179000e7ae3SMatthew Knepley /*@
1180000e7ae3SMatthew Knepley   TSDefaultUpdate - The default update function which does nothing.
1181000e7ae3SMatthew Knepley 
1182000e7ae3SMatthew Knepley   Collective on TS
1183000e7ae3SMatthew Knepley 
1184000e7ae3SMatthew Knepley   Input Parameters:
1185000e7ae3SMatthew Knepley + ts  - The TS context obtained from TSCreate()
1186000e7ae3SMatthew Knepley - t   - The current time
1187000e7ae3SMatthew Knepley 
1188000e7ae3SMatthew Knepley   Output Parameters:
1189000e7ae3SMatthew Knepley . dt  - The current time step
1190000e7ae3SMatthew Knepley 
1191000e7ae3SMatthew Knepley   Level: developer
1192000e7ae3SMatthew Knepley 
1193000e7ae3SMatthew Knepley .keywords: TS, update, timestep
1194000e7ae3SMatthew Knepley @*/
1195000e7ae3SMatthew Knepley int TSDefaultUpdate(TS ts, double t, double *dt)
1196000e7ae3SMatthew Knepley {
1197000e7ae3SMatthew Knepley   PetscFunctionBegin;
1198000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1199000e7ae3SMatthew Knepley }
1200000e7ae3SMatthew Knepley 
1201e74ef692SMatthew Knepley #undef __FUNCT__
1202e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1203000e7ae3SMatthew Knepley /*@
1204000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
1205000e7ae3SMatthew Knepley   called once at the end of time stepping.
1206000e7ae3SMatthew Knepley 
1207000e7ae3SMatthew Knepley   Collective on TS
1208000e7ae3SMatthew Knepley 
1209000e7ae3SMatthew Knepley   Input Parameters:
1210000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1211000e7ae3SMatthew Knepley - func - The function
1212000e7ae3SMatthew Knepley 
1213000e7ae3SMatthew Knepley   Calling sequence of func:
1214000e7ae3SMatthew Knepley . func (TS ts);
1215000e7ae3SMatthew Knepley 
1216000e7ae3SMatthew Knepley   Level: intermediate
1217000e7ae3SMatthew Knepley 
1218000e7ae3SMatthew Knepley .keywords: TS, timestep
1219000e7ae3SMatthew Knepley @*/
1220000e7ae3SMatthew Knepley int TSSetPostStep(TS ts, int (*func)(TS))
1221000e7ae3SMatthew Knepley {
1222000e7ae3SMatthew Knepley   PetscFunctionBegin;
1223000e7ae3SMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
1224000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1225000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1226000e7ae3SMatthew Knepley }
1227000e7ae3SMatthew Knepley 
1228e74ef692SMatthew Knepley #undef __FUNCT__
1229e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultPostStep"
1230000e7ae3SMatthew Knepley /*@
1231000e7ae3SMatthew Knepley   TSDefaultPostStep - The default post-stepping function which does nothing.
1232000e7ae3SMatthew Knepley 
1233000e7ae3SMatthew Knepley   Collective on TS
1234000e7ae3SMatthew Knepley 
1235000e7ae3SMatthew Knepley   Input Parameters:
1236000e7ae3SMatthew Knepley . ts  - The TS context obtained from TSCreate()
1237000e7ae3SMatthew Knepley 
1238000e7ae3SMatthew Knepley   Level: developer
1239000e7ae3SMatthew Knepley 
1240000e7ae3SMatthew Knepley .keywords: TS, timestep
1241000e7ae3SMatthew Knepley @*/
1242000e7ae3SMatthew Knepley int TSDefaultPostStep(TS ts)
1243000e7ae3SMatthew Knepley {
1244000e7ae3SMatthew Knepley   PetscFunctionBegin;
1245000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1246000e7ae3SMatthew Knepley }
1247000e7ae3SMatthew Knepley 
1248d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1249d763cef2SBarry Smith 
12504a2ae208SSatish Balay #undef __FUNCT__
12514a2ae208SSatish Balay #define __FUNCT__ "TSSetMonitor"
1252d763cef2SBarry Smith /*@C
1253d763cef2SBarry Smith    TSSetMonitor - Sets an ADDITIONAL function that is to be used at every
1254d763cef2SBarry Smith    timestep to display the iteration's  progress.
1255d763cef2SBarry Smith 
1256d763cef2SBarry Smith    Collective on TS
1257d763cef2SBarry Smith 
1258d763cef2SBarry Smith    Input Parameters:
1259d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1260d763cef2SBarry Smith .  func - monitoring routine
1261329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1262b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1263b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1264b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1265d763cef2SBarry Smith 
1266d763cef2SBarry Smith    Calling sequence of func:
126787828ca2SBarry Smith $    int func(TS ts,int steps,PetscReal time,Vec x,void *mctx)
1268d763cef2SBarry Smith 
1269d763cef2SBarry Smith +    ts - the TS context
1270d763cef2SBarry Smith .    steps - iteration number
12711f06c33eSBarry Smith .    time - current time
1272d763cef2SBarry Smith .    x - current iterate
1273d763cef2SBarry Smith -    mctx - [optional] monitoring context
1274d763cef2SBarry Smith 
1275d763cef2SBarry Smith    Notes:
1276d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1277d763cef2SBarry Smith    already has been loaded.
1278d763cef2SBarry Smith 
1279d763cef2SBarry Smith    Level: intermediate
1280d763cef2SBarry Smith 
1281d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1282d763cef2SBarry Smith 
1283d763cef2SBarry Smith .seealso: TSDefaultMonitor(), TSClearMonitor()
1284d763cef2SBarry Smith @*/
128587828ca2SBarry Smith int TSSetMonitor(TS ts,int (*monitor)(TS,int,PetscReal,Vec,void*),void *mctx,int (*mdestroy)(void*))
1286d763cef2SBarry Smith {
1287d763cef2SBarry Smith   PetscFunctionBegin;
1288d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1289d763cef2SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) {
129029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1291d763cef2SBarry Smith   }
1292d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1293329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1294d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1295d763cef2SBarry Smith   PetscFunctionReturn(0);
1296d763cef2SBarry Smith }
1297d763cef2SBarry Smith 
12984a2ae208SSatish Balay #undef __FUNCT__
12994a2ae208SSatish Balay #define __FUNCT__ "TSClearMonitor"
1300d763cef2SBarry Smith /*@C
1301d763cef2SBarry Smith    TSClearMonitor - Clears all the monitors that have been set on a time-step object.
1302d763cef2SBarry Smith 
1303d763cef2SBarry Smith    Collective on TS
1304d763cef2SBarry Smith 
1305d763cef2SBarry Smith    Input Parameters:
1306d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1307d763cef2SBarry Smith 
1308d763cef2SBarry Smith    Notes:
1309d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1310d763cef2SBarry Smith 
1311d763cef2SBarry Smith    Level: intermediate
1312d763cef2SBarry Smith 
1313d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1314d763cef2SBarry Smith 
1315d763cef2SBarry Smith .seealso: TSDefaultMonitor(), TSSetMonitor()
1316d763cef2SBarry Smith @*/
1317d763cef2SBarry Smith int TSClearMonitor(TS ts)
1318d763cef2SBarry Smith {
1319d763cef2SBarry Smith   PetscFunctionBegin;
1320d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1321d763cef2SBarry Smith   ts->numbermonitors = 0;
1322d763cef2SBarry Smith   PetscFunctionReturn(0);
1323d763cef2SBarry Smith }
1324d763cef2SBarry Smith 
13254a2ae208SSatish Balay #undef __FUNCT__
13264a2ae208SSatish Balay #define __FUNCT__ "TSDefaultMonitor"
132787828ca2SBarry Smith int TSDefaultMonitor(TS ts,int step,PetscReal ptime,Vec v,void *ctx)
1328d763cef2SBarry Smith {
1329d132466eSBarry Smith   int ierr;
1330d132466eSBarry Smith 
1331d763cef2SBarry Smith   PetscFunctionBegin;
1332142b95e3SSatish Balay   ierr = PetscPrintf(ts->comm,"timestep %d dt %g time %g\n",step,ts->time_step,ptime);CHKERRQ(ierr);
1333d763cef2SBarry Smith   PetscFunctionReturn(0);
1334d763cef2SBarry Smith }
1335d763cef2SBarry Smith 
13364a2ae208SSatish Balay #undef __FUNCT__
13374a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1338d763cef2SBarry Smith /*@
1339d763cef2SBarry Smith    TSStep - Steps the requested number of timesteps.
1340d763cef2SBarry Smith 
1341d763cef2SBarry Smith    Collective on TS
1342d763cef2SBarry Smith 
1343d763cef2SBarry Smith    Input Parameter:
1344d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1345d763cef2SBarry Smith 
1346d763cef2SBarry Smith    Output Parameters:
1347d763cef2SBarry Smith +  steps - number of iterations until termination
1348142b95e3SSatish Balay -  ptime - time until termination
1349d763cef2SBarry Smith 
1350d763cef2SBarry Smith    Level: beginner
1351d763cef2SBarry Smith 
1352d763cef2SBarry Smith .keywords: TS, timestep, solve
1353d763cef2SBarry Smith 
1354d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy()
1355d763cef2SBarry Smith @*/
135687828ca2SBarry Smith int TSStep(TS ts,int *steps,PetscReal *ptime)
1357d763cef2SBarry Smith {
1358d405a339SMatthew Knepley   PetscTruth opt;
1359f1af5d2fSBarry Smith   int        ierr;
1360d763cef2SBarry Smith 
1361d763cef2SBarry Smith   PetscFunctionBegin;
1362d763cef2SBarry Smith   PetscValidHeaderSpecific(ts, TS_COOKIE);
1363d405a339SMatthew Knepley   if (!ts->setupcalled) {
1364d405a339SMatthew Knepley     ierr = TSSetUp(ts);                                                                                   CHKERRQ(ierr);
1365d405a339SMatthew Knepley   }
1366d405a339SMatthew Knepley 
1367d405a339SMatthew Knepley   TSLogEventBegin(TS_Step, ts, 0, 0, 0);
1368d405a339SMatthew Knepley   ierr = (*ts->ops->prestep)(ts);                                                                         CHKERRQ(ierr);
1369000e7ae3SMatthew Knepley   ierr = (*ts->ops->step)(ts, steps, ptime);                                                              CHKERRQ(ierr);
1370d405a339SMatthew Knepley   ierr = (*ts->ops->poststep)(ts);                                                                        CHKERRQ(ierr);
1371d405a339SMatthew Knepley   TSLogEventEnd(TS_Step, ts, 0, 0, 0);
1372d405a339SMatthew Knepley 
1373d405a339SMatthew Knepley   ierr = PetscOptionsHasName(ts->prefix, "-ts_view", &opt);                                               CHKERRQ(ierr);
1374d405a339SMatthew Knepley   if ((opt == PETSC_TRUE) && !PetscPreLoadingOn) {
1375d405a339SMatthew Knepley     ierr = TSView(ts, PETSC_VIEWER_STDOUT_WORLD);                                                         CHKERRQ(ierr);
1376d405a339SMatthew Knepley   }
1377d763cef2SBarry Smith   PetscFunctionReturn(0);
1378d763cef2SBarry Smith }
1379d763cef2SBarry Smith 
13804a2ae208SSatish Balay #undef __FUNCT__
13814a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
1382d763cef2SBarry Smith /*
1383d763cef2SBarry Smith      Runs the user provided monitor routines, if they exists.
1384d763cef2SBarry Smith */
138587828ca2SBarry Smith int TSMonitor(TS ts,int step,PetscReal ptime,Vec x)
1386d763cef2SBarry Smith {
1387d763cef2SBarry Smith   int i,ierr,n = ts->numbermonitors;
1388d763cef2SBarry Smith 
1389d763cef2SBarry Smith   PetscFunctionBegin;
1390d763cef2SBarry Smith   for (i=0; i<n; i++) {
1391142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
1392d763cef2SBarry Smith   }
1393d763cef2SBarry Smith   PetscFunctionReturn(0);
1394d763cef2SBarry Smith }
1395d763cef2SBarry Smith 
1396d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
1397d763cef2SBarry Smith 
13984a2ae208SSatish Balay #undef __FUNCT__
13994a2ae208SSatish Balay #define __FUNCT__ "TSLGMonitorCreate"
1400d763cef2SBarry Smith /*@C
1401d763cef2SBarry Smith    TSLGMonitorCreate - Creates a line graph context for use with
1402d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
1403d763cef2SBarry Smith 
1404d763cef2SBarry Smith    Collective on TS
1405d763cef2SBarry Smith 
1406d763cef2SBarry Smith    Input Parameters:
1407d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
1408d763cef2SBarry Smith .  label - the title to put in the title bar
14097c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
1410d763cef2SBarry Smith -  m, n - the screen width and height in pixels
1411d763cef2SBarry Smith 
1412d763cef2SBarry Smith    Output Parameter:
1413d763cef2SBarry Smith .  draw - the drawing context
1414d763cef2SBarry Smith 
1415d763cef2SBarry Smith    Options Database Key:
1416d763cef2SBarry Smith .  -ts_xmonitor - automatically sets line graph monitor
1417d763cef2SBarry Smith 
1418d763cef2SBarry Smith    Notes:
1419b0a32e0cSBarry Smith    Use TSLGMonitorDestroy() to destroy this line graph, not PetscDrawLGDestroy().
1420d763cef2SBarry Smith 
1421d763cef2SBarry Smith    Level: intermediate
1422d763cef2SBarry Smith 
14237c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
1424d763cef2SBarry Smith 
1425d763cef2SBarry Smith .seealso: TSLGMonitorDestroy(), TSSetMonitor()
14267c922b88SBarry Smith 
1427d763cef2SBarry Smith @*/
1428b0a32e0cSBarry Smith int TSLGMonitorCreate(char *host,char *label,int x,int y,int m,int n,PetscDrawLG *draw)
1429d763cef2SBarry Smith {
1430b0a32e0cSBarry Smith   PetscDraw win;
1431d763cef2SBarry Smith   int       ierr;
1432d763cef2SBarry Smith 
1433d763cef2SBarry Smith   PetscFunctionBegin;
1434b0a32e0cSBarry Smith   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
1435b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
1436b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
1437b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
1438d763cef2SBarry Smith 
1439b0a32e0cSBarry Smith   PetscLogObjectParent(*draw,win);
1440d763cef2SBarry Smith   PetscFunctionReturn(0);
1441d763cef2SBarry Smith }
1442d763cef2SBarry Smith 
14434a2ae208SSatish Balay #undef __FUNCT__
14444a2ae208SSatish Balay #define __FUNCT__ "TSLGMonitor"
144587828ca2SBarry Smith int TSLGMonitor(TS ts,int n,PetscReal ptime,Vec v,void *monctx)
1446d763cef2SBarry Smith {
1447b0a32e0cSBarry Smith   PetscDrawLG lg = (PetscDrawLG) monctx;
144887828ca2SBarry Smith   PetscReal      x,y = ptime;
1449d763cef2SBarry Smith   int         ierr;
1450d763cef2SBarry Smith 
1451d763cef2SBarry Smith   PetscFunctionBegin;
14527c922b88SBarry Smith   if (!monctx) {
14537c922b88SBarry Smith     MPI_Comm    comm;
1454b0a32e0cSBarry Smith     PetscViewer viewer;
14557c922b88SBarry Smith 
14567c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
1457b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
1458b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
14597c922b88SBarry Smith   }
14607c922b88SBarry Smith 
1461b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
146287828ca2SBarry Smith   x = (PetscReal)n;
1463b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1464d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
1465b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1466d763cef2SBarry Smith   }
1467d763cef2SBarry Smith   PetscFunctionReturn(0);
1468d763cef2SBarry Smith }
1469d763cef2SBarry Smith 
14704a2ae208SSatish Balay #undef __FUNCT__
14714a2ae208SSatish Balay #define __FUNCT__ "TSLGMonitorDestroy"
1472d763cef2SBarry Smith /*@C
1473d763cef2SBarry Smith    TSLGMonitorDestroy - Destroys a line graph context that was created
1474d763cef2SBarry Smith    with TSLGMonitorCreate().
1475d763cef2SBarry Smith 
1476b0a32e0cSBarry Smith    Collective on PetscDrawLG
1477d763cef2SBarry Smith 
1478d763cef2SBarry Smith    Input Parameter:
1479d763cef2SBarry Smith .  draw - the drawing context
1480d763cef2SBarry Smith 
1481d763cef2SBarry Smith    Level: intermediate
1482d763cef2SBarry Smith 
1483d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
1484d763cef2SBarry Smith 
1485d763cef2SBarry Smith .seealso: TSLGMonitorCreate(),  TSSetMonitor(), TSLGMonitor();
1486d763cef2SBarry Smith @*/
1487b0a32e0cSBarry Smith int TSLGMonitorDestroy(PetscDrawLG drawlg)
1488d763cef2SBarry Smith {
1489b0a32e0cSBarry Smith   PetscDraw draw;
1490d763cef2SBarry Smith   int       ierr;
1491d763cef2SBarry Smith 
1492d763cef2SBarry Smith   PetscFunctionBegin;
1493b0a32e0cSBarry Smith   ierr = PetscDrawLGGetDraw(drawlg,&draw);CHKERRQ(ierr);
1494b0a32e0cSBarry Smith   ierr = PetscDrawDestroy(draw);CHKERRQ(ierr);
1495b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
1496d763cef2SBarry Smith   PetscFunctionReturn(0);
1497d763cef2SBarry Smith }
1498d763cef2SBarry Smith 
14994a2ae208SSatish Balay #undef __FUNCT__
15004a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
1501d763cef2SBarry Smith /*@
1502d763cef2SBarry Smith    TSGetTime - Gets the current time.
1503d763cef2SBarry Smith 
1504d763cef2SBarry Smith    Not Collective
1505d763cef2SBarry Smith 
1506d763cef2SBarry Smith    Input Parameter:
1507d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1508d763cef2SBarry Smith 
1509d763cef2SBarry Smith    Output Parameter:
1510d763cef2SBarry Smith .  t  - the current time
1511d763cef2SBarry Smith 
1512d763cef2SBarry Smith    Contributed by: Matthew Knepley
1513d763cef2SBarry Smith 
1514d763cef2SBarry Smith    Level: beginner
1515d763cef2SBarry Smith 
1516d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1517d763cef2SBarry Smith 
1518d763cef2SBarry Smith .keywords: TS, get, time
1519d763cef2SBarry Smith @*/
152087828ca2SBarry Smith int TSGetTime(TS ts,PetscReal* t)
1521d763cef2SBarry Smith {
1522d763cef2SBarry Smith   PetscFunctionBegin;
1523d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1524d763cef2SBarry Smith   *t = ts->ptime;
1525d763cef2SBarry Smith   PetscFunctionReturn(0);
1526d763cef2SBarry Smith }
1527d763cef2SBarry Smith 
15284a2ae208SSatish Balay #undef __FUNCT__
15294a2ae208SSatish Balay #define __FUNCT__ "TSGetProblemType"
1530d763cef2SBarry Smith /*@C
1531d763cef2SBarry Smith    TSGetProblemType - Returns the problem type of a TS (timestepper) context.
1532d763cef2SBarry Smith 
1533d763cef2SBarry Smith    Not Collective
1534d763cef2SBarry Smith 
1535d763cef2SBarry Smith    Input Parameter:
1536d763cef2SBarry Smith .  ts   - The TS context obtained from TSCreate()
1537d763cef2SBarry Smith 
1538d763cef2SBarry Smith    Output Parameter:
1539d763cef2SBarry Smith .  type - The problem type, TS_LINEAR or TS_NONLINEAR
1540d763cef2SBarry Smith 
1541d763cef2SBarry Smith    Level: intermediate
1542d763cef2SBarry Smith 
1543d763cef2SBarry Smith    Contributed by: Matthew Knepley
1544d763cef2SBarry Smith 
1545d763cef2SBarry Smith .keywords: ts, get, type
1546d763cef2SBarry Smith 
1547d763cef2SBarry Smith @*/
1548d763cef2SBarry Smith int TSGetProblemType(TS ts,TSProblemType *type)
1549d763cef2SBarry Smith {
1550d763cef2SBarry Smith   PetscFunctionBegin;
1551d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1552d763cef2SBarry Smith   *type = ts->problem_type;
1553d763cef2SBarry Smith   PetscFunctionReturn(0);
1554d763cef2SBarry Smith }
1555d763cef2SBarry Smith 
15564a2ae208SSatish Balay #undef __FUNCT__
15574a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
1558d763cef2SBarry Smith /*@C
1559d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
1560d763cef2SBarry Smith    TS options in the database.
1561d763cef2SBarry Smith 
1562d763cef2SBarry Smith    Collective on TS
1563d763cef2SBarry Smith 
1564d763cef2SBarry Smith    Input Parameter:
1565d763cef2SBarry Smith +  ts     - The TS context
1566d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
1567d763cef2SBarry Smith 
1568d763cef2SBarry Smith    Notes:
1569d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1570d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
1571d763cef2SBarry Smith    hyphen.
1572d763cef2SBarry Smith 
1573d763cef2SBarry Smith    Contributed by: Matthew Knepley
1574d763cef2SBarry Smith 
1575d763cef2SBarry Smith    Level: advanced
1576d763cef2SBarry Smith 
1577d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
1578d763cef2SBarry Smith 
1579d763cef2SBarry Smith .seealso: TSSetFromOptions()
1580d763cef2SBarry Smith 
1581d763cef2SBarry Smith @*/
1582d763cef2SBarry Smith int TSSetOptionsPrefix(TS ts,char *prefix)
1583d763cef2SBarry Smith {
1584d763cef2SBarry Smith   int ierr;
1585d763cef2SBarry Smith 
1586d763cef2SBarry Smith   PetscFunctionBegin;
1587d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1588d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
1589d763cef2SBarry Smith   switch(ts->problem_type) {
1590d763cef2SBarry Smith     case TS_NONLINEAR:
1591d763cef2SBarry Smith       ierr = SNESSetOptionsPrefix(ts->snes,prefix);CHKERRQ(ierr);
1592d763cef2SBarry Smith       break;
1593d763cef2SBarry Smith     case TS_LINEAR:
1594d763cef2SBarry Smith       ierr = SLESSetOptionsPrefix(ts->sles,prefix);CHKERRQ(ierr);
1595d763cef2SBarry Smith       break;
1596d763cef2SBarry Smith   }
1597d763cef2SBarry Smith   PetscFunctionReturn(0);
1598d763cef2SBarry Smith }
1599d763cef2SBarry Smith 
1600d763cef2SBarry Smith 
16014a2ae208SSatish Balay #undef __FUNCT__
16024a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
1603d763cef2SBarry Smith /*@C
1604d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
1605d763cef2SBarry Smith    TS options in the database.
1606d763cef2SBarry Smith 
1607d763cef2SBarry Smith    Collective on TS
1608d763cef2SBarry Smith 
1609d763cef2SBarry Smith    Input Parameter:
1610d763cef2SBarry Smith +  ts     - The TS context
1611d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
1612d763cef2SBarry Smith 
1613d763cef2SBarry Smith    Notes:
1614d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1615d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
1616d763cef2SBarry Smith    hyphen.
1617d763cef2SBarry Smith 
1618d763cef2SBarry Smith    Contributed by: Matthew Knepley
1619d763cef2SBarry Smith 
1620d763cef2SBarry Smith    Level: advanced
1621d763cef2SBarry Smith 
1622d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
1623d763cef2SBarry Smith 
1624d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
1625d763cef2SBarry Smith 
1626d763cef2SBarry Smith @*/
1627d763cef2SBarry Smith int TSAppendOptionsPrefix(TS ts,char *prefix)
1628d763cef2SBarry Smith {
1629d763cef2SBarry Smith   int ierr;
1630d763cef2SBarry Smith 
1631d763cef2SBarry Smith   PetscFunctionBegin;
1632d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1633d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
1634d763cef2SBarry Smith   switch(ts->problem_type) {
1635d763cef2SBarry Smith     case TS_NONLINEAR:
1636d763cef2SBarry Smith       ierr = SNESAppendOptionsPrefix(ts->snes,prefix);CHKERRQ(ierr);
1637d763cef2SBarry Smith       break;
1638d763cef2SBarry Smith     case TS_LINEAR:
1639d763cef2SBarry Smith       ierr = SLESAppendOptionsPrefix(ts->sles,prefix);CHKERRQ(ierr);
1640d763cef2SBarry Smith       break;
1641d763cef2SBarry Smith   }
1642d763cef2SBarry Smith   PetscFunctionReturn(0);
1643d763cef2SBarry Smith }
1644d763cef2SBarry Smith 
16454a2ae208SSatish Balay #undef __FUNCT__
16464a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
1647d763cef2SBarry Smith /*@C
1648d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
1649d763cef2SBarry Smith    TS options in the database.
1650d763cef2SBarry Smith 
1651d763cef2SBarry Smith    Not Collective
1652d763cef2SBarry Smith 
1653d763cef2SBarry Smith    Input Parameter:
1654d763cef2SBarry Smith .  ts - The TS context
1655d763cef2SBarry Smith 
1656d763cef2SBarry Smith    Output Parameter:
1657d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
1658d763cef2SBarry Smith 
1659d763cef2SBarry Smith    Contributed by: Matthew Knepley
1660d763cef2SBarry Smith 
1661d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
1662d763cef2SBarry Smith    sufficient length to hold the prefix.
1663d763cef2SBarry Smith 
1664d763cef2SBarry Smith    Level: intermediate
1665d763cef2SBarry Smith 
1666d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
1667d763cef2SBarry Smith 
1668d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
1669d763cef2SBarry Smith @*/
1670d763cef2SBarry Smith int TSGetOptionsPrefix(TS ts,char **prefix)
1671d763cef2SBarry Smith {
1672d763cef2SBarry Smith   int ierr;
1673d763cef2SBarry Smith 
1674d763cef2SBarry Smith   PetscFunctionBegin;
1675d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1676d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
1677d763cef2SBarry Smith   PetscFunctionReturn(0);
1678d763cef2SBarry Smith }
1679d763cef2SBarry Smith 
16804a2ae208SSatish Balay #undef __FUNCT__
16814a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSMatrix"
1682d763cef2SBarry Smith /*@C
1683d763cef2SBarry Smith    TSGetRHSMatrix - Returns the matrix A at the present timestep.
1684d763cef2SBarry Smith 
1685d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
1686d763cef2SBarry Smith 
1687d763cef2SBarry Smith    Input Parameter:
1688d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
1689d763cef2SBarry Smith 
1690d763cef2SBarry Smith    Output Parameters:
1691d763cef2SBarry Smith +  A   - The matrix A, where U_t = A(t) U
1692d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as A
1693d763cef2SBarry Smith -  ctx - User-defined context for matrix evaluation routine
1694d763cef2SBarry Smith 
1695d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
1696d763cef2SBarry Smith 
1697d763cef2SBarry Smith    Contributed by: Matthew Knepley
1698d763cef2SBarry Smith 
1699d763cef2SBarry Smith    Level: intermediate
1700d763cef2SBarry Smith 
1701d763cef2SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetTimeStepNumber(), TSGetRHSJacobian()
1702d763cef2SBarry Smith 
1703d763cef2SBarry Smith .keywords: TS, timestep, get, matrix
1704d763cef2SBarry Smith 
1705d763cef2SBarry Smith @*/
1706d763cef2SBarry Smith int TSGetRHSMatrix(TS ts,Mat *A,Mat *M,void **ctx)
1707d763cef2SBarry Smith {
1708d763cef2SBarry Smith   PetscFunctionBegin;
1709d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1710d763cef2SBarry Smith   if (A)   *A = ts->A;
1711d763cef2SBarry Smith   if (M)   *M = ts->B;
1712d763cef2SBarry Smith   if (ctx) *ctx = ts->jacP;
1713d763cef2SBarry Smith   PetscFunctionReturn(0);
1714d763cef2SBarry Smith }
1715d763cef2SBarry Smith 
17164a2ae208SSatish Balay #undef __FUNCT__
17174a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
1718d763cef2SBarry Smith /*@C
1719d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
1720d763cef2SBarry Smith 
1721d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
1722d763cef2SBarry Smith 
1723d763cef2SBarry Smith    Input Parameter:
1724d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
1725d763cef2SBarry Smith 
1726d763cef2SBarry Smith    Output Parameters:
1727d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
1728d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
1729d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine
1730d763cef2SBarry Smith 
1731d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
1732d763cef2SBarry Smith 
1733d763cef2SBarry Smith    Contributed by: Matthew Knepley
1734d763cef2SBarry Smith 
1735d763cef2SBarry Smith    Level: intermediate
1736d763cef2SBarry Smith 
1737d763cef2SBarry Smith .seealso: TSGetTimeStep(), TSGetRHSMatrix(), TSGetTime(), TSGetTimeStepNumber()
1738d763cef2SBarry Smith 
1739d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
1740d763cef2SBarry Smith @*/
1741d763cef2SBarry Smith int TSGetRHSJacobian(TS ts,Mat *J,Mat *M,void **ctx)
1742d763cef2SBarry Smith {
1743d763cef2SBarry Smith   int ierr;
1744d763cef2SBarry Smith 
1745d763cef2SBarry Smith   PetscFunctionBegin;
1746d763cef2SBarry Smith   ierr = TSGetRHSMatrix(ts,J,M,ctx);CHKERRQ(ierr);
1747d763cef2SBarry Smith   PetscFunctionReturn(0);
1748d763cef2SBarry Smith }
1749d763cef2SBarry Smith 
1750d763cef2SBarry Smith /*MC
1751f1af5d2fSBarry Smith    TSRegisterDynamic - Adds a method to the timestepping solver package.
1752d763cef2SBarry Smith 
1753d763cef2SBarry Smith    Synopsis:
17543a7fca6bSBarry Smith    int TSRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(TS))
1755d763cef2SBarry Smith 
1756d763cef2SBarry Smith    Not collective
1757d763cef2SBarry Smith 
1758d763cef2SBarry Smith    Input Parameters:
1759d763cef2SBarry Smith +  name_solver - name of a new user-defined solver
1760d763cef2SBarry Smith .  path - path (either absolute or relative) the library containing this solver
1761d763cef2SBarry Smith .  name_create - name of routine to create method context
1762d763cef2SBarry Smith -  routine_create - routine to create method context
1763d763cef2SBarry Smith 
1764d763cef2SBarry Smith    Notes:
1765f1af5d2fSBarry Smith    TSRegisterDynamic() may be called multiple times to add several user-defined solvers.
1766d763cef2SBarry Smith 
1767d763cef2SBarry Smith    If dynamic libraries are used, then the fourth input argument (routine_create)
1768d763cef2SBarry Smith    is ignored.
1769d763cef2SBarry Smith 
1770d763cef2SBarry Smith    Sample usage:
1771d763cef2SBarry Smith .vb
1772f1af5d2fSBarry Smith    TSRegisterDynamic("my_solver",/home/username/my_lib/lib/libO/solaris/mylib.a,
1773d763cef2SBarry Smith               "MySolverCreate",MySolverCreate);
1774d763cef2SBarry Smith .ve
1775d763cef2SBarry Smith 
1776d763cef2SBarry Smith    Then, your solver can be chosen with the procedural interface via
1777d763cef2SBarry Smith $     TSSetType(ts,"my_solver")
1778d763cef2SBarry Smith    or at runtime via the option
1779d763cef2SBarry Smith $     -ts_type my_solver
1780d763cef2SBarry Smith 
1781d763cef2SBarry Smith    Level: advanced
1782d763cef2SBarry Smith 
17832aeb12f1SSatish Balay    Environmental variables such as ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, ${BOPT},
17845ba88a07SLois Curfman McInnes    and others of the form ${any_environmental_variable} occuring in pathname will be
17855ba88a07SLois Curfman McInnes    replaced with appropriate values.
1786d763cef2SBarry Smith 
1787d763cef2SBarry Smith .keywords: TS, register
1788d763cef2SBarry Smith 
1789d763cef2SBarry Smith .seealso: TSRegisterAll(), TSRegisterDestroy()
1790d763cef2SBarry Smith M*/
1791d763cef2SBarry Smith 
17924a2ae208SSatish Balay #undef __FUNCT__
17934a2ae208SSatish Balay #define __FUNCT__ "TSRegister"
1794000e7ae3SMatthew Knepley int TSRegister(const char sname[], const char path[], const char name[], int (*function)(TS))
1795d763cef2SBarry Smith {
1796d763cef2SBarry Smith   char fullname[256];
1797549d3d68SSatish Balay   int  ierr;
1798d763cef2SBarry Smith 
1799d763cef2SBarry Smith   PetscFunctionBegin;
1800b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
1801*c134de8dSSatish Balay   ierr = PetscFListAdd(&TSList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
1802d763cef2SBarry Smith   PetscFunctionReturn(0);
1803d763cef2SBarry Smith }
18041713a123SBarry Smith 
18051713a123SBarry Smith #undef __FUNCT__
18061713a123SBarry Smith #define __FUNCT__ "TSVecViewMonitor"
18071713a123SBarry Smith /*@C
18081713a123SBarry Smith    TSVecViewMonitor - Monitors progress of the TS solvers by calling
18091713a123SBarry Smith    VecView() for the solution at each timestep
18101713a123SBarry Smith 
18111713a123SBarry Smith    Collective on TS
18121713a123SBarry Smith 
18131713a123SBarry Smith    Input Parameters:
18141713a123SBarry Smith +  ts - the TS context
18151713a123SBarry Smith .  step - current time-step
1816142b95e3SSatish Balay .  ptime - current time
18171713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
18181713a123SBarry Smith 
18191713a123SBarry Smith    Level: intermediate
18201713a123SBarry Smith 
18211713a123SBarry Smith .keywords: TS,  vector, monitor, view
18221713a123SBarry Smith 
18231713a123SBarry Smith .seealso: TSSetMonitor(), TSDefaultMonitor(), VecView()
18241713a123SBarry Smith @*/
1825142b95e3SSatish Balay int TSVecViewMonitor(TS ts,int step,PetscReal ptime,Vec x,void *dummy)
18261713a123SBarry Smith {
18271713a123SBarry Smith   int         ierr;
18281713a123SBarry Smith   PetscViewer viewer = (PetscViewer) dummy;
18291713a123SBarry Smith 
18301713a123SBarry Smith   PetscFunctionBegin;
18311713a123SBarry Smith   if (!viewer) {
18321713a123SBarry Smith     MPI_Comm comm;
18331713a123SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
18341713a123SBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
18351713a123SBarry Smith   }
18361713a123SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
18371713a123SBarry Smith   PetscFunctionReturn(0);
18381713a123SBarry Smith }
18391713a123SBarry Smith 
18401713a123SBarry Smith 
18411713a123SBarry Smith 
1842