xref: /petsc/src/ts/interface/ts.c (revision e74ef6923b2c0251c8aab3a0671bbb2dfd6aaf97)
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 
44a2ae208SSatish Balay #undef __FUNCT__
54a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
6a7a1495cSBarry Smith /*@
78c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
8a7a1495cSBarry Smith       set with TSSetRHSJacobian().
9a7a1495cSBarry Smith 
10a7a1495cSBarry Smith    Collective on TS and Vec
11a7a1495cSBarry Smith 
12a7a1495cSBarry Smith    Input Parameters:
13a7a1495cSBarry Smith +  ts - the SNES context
14a7a1495cSBarry Smith .  t - current timestep
15a7a1495cSBarry Smith -  x - input vector
16a7a1495cSBarry Smith 
17a7a1495cSBarry Smith    Output Parameters:
18a7a1495cSBarry Smith +  A - Jacobian matrix
19a7a1495cSBarry Smith .  B - optional preconditioning matrix
20a7a1495cSBarry Smith -  flag - flag indicating matrix structure
21a7a1495cSBarry Smith 
22a7a1495cSBarry Smith    Notes:
23a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
24a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
25a7a1495cSBarry Smith 
26a7a1495cSBarry Smith    See SLESSetOperators() for important information about setting the
27a7a1495cSBarry Smith    flag parameter.
28a7a1495cSBarry Smith 
29a7a1495cSBarry Smith    TSComputeJacobian() is valid only for TS_NONLINEAR
30a7a1495cSBarry Smith 
31a7a1495cSBarry Smith    Level: developer
32a7a1495cSBarry Smith 
33a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
34a7a1495cSBarry Smith 
35a7a1495cSBarry Smith .seealso:  TSSetRHSJacobian(), SLESSetOperators()
36a7a1495cSBarry Smith @*/
3787828ca2SBarry Smith int TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
38a7a1495cSBarry Smith {
39a7a1495cSBarry Smith   int ierr;
40a7a1495cSBarry Smith 
41a7a1495cSBarry Smith   PetscFunctionBegin;
42a7a1495cSBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
43a7a1495cSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE);
44a7a1495cSBarry Smith   PetscCheckSameComm(ts,X);
45a7a1495cSBarry Smith   if (ts->problem_type != TS_NONLINEAR) {
4629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For TS_NONLINEAR only");
47a7a1495cSBarry Smith   }
48000e7ae3SMatthew Knepley   if (ts->ops->rhsjacobian) {
49b0a32e0cSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
50a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
51a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
52000e7ae3SMatthew Knepley     ierr = (*ts->ops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr);
53a7a1495cSBarry Smith     PetscStackPop;
54b0a32e0cSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
55a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
56a7a1495cSBarry Smith     PetscValidHeaderSpecific(*A,MAT_COOKIE);
57a7a1495cSBarry Smith     PetscValidHeaderSpecific(*B,MAT_COOKIE);
58ef66eb69SBarry Smith   } else {
59ef66eb69SBarry Smith     ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
60ef66eb69SBarry Smith     ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
61ef66eb69SBarry Smith     if (*A != *B) {
62ef66eb69SBarry Smith       ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
63ef66eb69SBarry Smith       ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
64ef66eb69SBarry Smith     }
65ef66eb69SBarry Smith   }
66a7a1495cSBarry Smith   PetscFunctionReturn(0);
67a7a1495cSBarry Smith }
68a7a1495cSBarry Smith 
694a2ae208SSatish Balay #undef __FUNCT__
704a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
71d763cef2SBarry Smith /*
72d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
73d763cef2SBarry Smith 
74d763cef2SBarry Smith    Note: If the user did not provide a function but merely a matrix,
75d763cef2SBarry Smith    this routine applies the matrix.
76d763cef2SBarry Smith */
7787828ca2SBarry Smith int TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
78d763cef2SBarry Smith {
79d763cef2SBarry Smith   int ierr;
80d763cef2SBarry Smith 
81d763cef2SBarry Smith   PetscFunctionBegin;
82d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
837c922b88SBarry Smith   PetscValidHeader(x);
847c922b88SBarry Smith   PetscValidHeader(y);
85d763cef2SBarry Smith 
86b0a32e0cSBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
87000e7ae3SMatthew Knepley   if (ts->ops->rhsfunction) {
88d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
89000e7ae3SMatthew Knepley     ierr = (*ts->ops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr);
90d763cef2SBarry Smith     PetscStackPop;
917c922b88SBarry Smith   } else {
92000e7ae3SMatthew Knepley     if (ts->ops->rhsmatrix) { /* assemble matrix for this timestep */
93d763cef2SBarry Smith       MatStructure flg;
94d763cef2SBarry Smith       PetscStackPush("TS user right-hand-side matrix function");
95000e7ae3SMatthew Knepley       ierr = (*ts->ops->rhsmatrix)(ts,t,&ts->A,&ts->B,&flg,ts->jacP);CHKERRQ(ierr);
96d763cef2SBarry Smith       PetscStackPop;
97d763cef2SBarry Smith     }
98d763cef2SBarry Smith     ierr = MatMult(ts->A,x,y);CHKERRQ(ierr);
997c922b88SBarry Smith   }
100d763cef2SBarry Smith 
101d763cef2SBarry Smith   /* apply user-provided boundary conditions (only needed if these are time dependent) */
102d763cef2SBarry Smith   ierr = TSComputeRHSBoundaryConditions(ts,t,y);CHKERRQ(ierr);
103b0a32e0cSBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
104d763cef2SBarry Smith 
105d763cef2SBarry Smith   PetscFunctionReturn(0);
106d763cef2SBarry Smith }
107d763cef2SBarry Smith 
1084a2ae208SSatish Balay #undef __FUNCT__
1094a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
110d763cef2SBarry Smith /*@C
111d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
112d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
113d763cef2SBarry Smith 
114d763cef2SBarry Smith     Collective on TS
115d763cef2SBarry Smith 
116d763cef2SBarry Smith     Input Parameters:
117d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
118d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
119d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
120d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
121d763cef2SBarry Smith 
122d763cef2SBarry Smith     Calling sequence of func:
12387828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
124d763cef2SBarry Smith 
125d763cef2SBarry Smith +   t - current timestep
126d763cef2SBarry Smith .   u - input vector
127d763cef2SBarry Smith .   F - function vector
128d763cef2SBarry Smith -   ctx - [optional] user-defined function context
129d763cef2SBarry Smith 
130d763cef2SBarry Smith     Important:
131d763cef2SBarry Smith     The user MUST call either this routine or TSSetRHSMatrix().
132d763cef2SBarry Smith 
133d763cef2SBarry Smith     Level: beginner
134d763cef2SBarry Smith 
135d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
136d763cef2SBarry Smith 
137d763cef2SBarry Smith .seealso: TSSetRHSMatrix()
138d763cef2SBarry Smith @*/
13987828ca2SBarry Smith int TSSetRHSFunction(TS ts,int (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
140d763cef2SBarry Smith {
141d763cef2SBarry Smith   PetscFunctionBegin;
142d763cef2SBarry Smith 
143d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
144d763cef2SBarry Smith   if (ts->problem_type == TS_LINEAR) {
14529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Cannot set function for linear problem");
146d763cef2SBarry Smith   }
147000e7ae3SMatthew Knepley   ts->ops->rhsfunction = f;
148d763cef2SBarry Smith   ts->funP             = ctx;
149d763cef2SBarry Smith   PetscFunctionReturn(0);
150d763cef2SBarry Smith }
151d763cef2SBarry Smith 
1524a2ae208SSatish Balay #undef __FUNCT__
1534a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSMatrix"
154d763cef2SBarry Smith /*@C
155d763cef2SBarry Smith    TSSetRHSMatrix - Sets the function to compute the matrix A, where U_t = A(t) U.
156d763cef2SBarry Smith    Also sets the location to store A.
157d763cef2SBarry Smith 
158d763cef2SBarry Smith    Collective on TS
159d763cef2SBarry Smith 
160d763cef2SBarry Smith    Input Parameters:
161d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
162d763cef2SBarry Smith .  A   - matrix
163d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
164d763cef2SBarry Smith .  f   - the matrix evaluation routine; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran)
165d763cef2SBarry Smith          if A is not a function of t.
166d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
167d763cef2SBarry Smith           matrix evaluation routine (may be PETSC_NULL)
168d763cef2SBarry Smith 
169d763cef2SBarry Smith    Calling sequence of func:
17087828ca2SBarry Smith $     func (TS ts,PetscReal t,Mat *A,Mat *B,int *flag,void *ctx);
171d763cef2SBarry Smith 
172d763cef2SBarry Smith +  t - current timestep
173d763cef2SBarry Smith .  A - matrix A, where U_t = A(t) U
174d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
175d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
176d763cef2SBarry Smith           structure (same as flag in SLESSetOperators())
177d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
178d763cef2SBarry Smith 
179d763cef2SBarry Smith    Notes:
180d763cef2SBarry Smith    See SLESSetOperators() for important information about setting the flag
181d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
182d763cef2SBarry Smith 
183d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
184d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
185d763cef2SBarry Smith    completely new new matrix structure (not just different matrix elements)
186d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
187d763cef2SBarry Smith    throughout the global iterations.
188d763cef2SBarry Smith 
189d763cef2SBarry Smith    Important:
190d763cef2SBarry Smith    The user MUST call either this routine or TSSetRHSFunction().
191d763cef2SBarry Smith 
192d763cef2SBarry Smith    Level: beginner
193d763cef2SBarry Smith 
194d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, matrix
195d763cef2SBarry Smith 
196d763cef2SBarry Smith .seealso: TSSetRHSFunction()
197d763cef2SBarry Smith @*/
19887828ca2SBarry Smith int TSSetRHSMatrix(TS ts,Mat A,Mat B,int (*f)(TS,PetscReal,Mat*,Mat*,MatStructure*,void*),void *ctx)
199d763cef2SBarry Smith {
200d763cef2SBarry Smith   PetscFunctionBegin;
201d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
202184914b5SBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE);
203184914b5SBarry Smith   PetscValidHeaderSpecific(B,MAT_COOKIE);
204184914b5SBarry Smith   PetscCheckSameComm(ts,A);
205184914b5SBarry Smith   PetscCheckSameComm(ts,B);
206d763cef2SBarry Smith   if (ts->problem_type == TS_NONLINEAR) {
20729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Not for nonlinear problems; use TSSetRHSJacobian()");
208d763cef2SBarry Smith   }
209d763cef2SBarry Smith 
210000e7ae3SMatthew Knepley   ts->ops->rhsmatrix = f;
211d763cef2SBarry Smith   ts->jacP           = ctx;
212d763cef2SBarry Smith   ts->A              = A;
213d763cef2SBarry Smith   ts->B              = B;
214d763cef2SBarry Smith 
215d763cef2SBarry Smith   PetscFunctionReturn(0);
216d763cef2SBarry Smith }
217d763cef2SBarry Smith 
2184a2ae208SSatish Balay #undef __FUNCT__
2194a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
220d763cef2SBarry Smith /*@C
221d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
222d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
223d763cef2SBarry Smith 
224d763cef2SBarry Smith    Collective on TS
225d763cef2SBarry Smith 
226d763cef2SBarry Smith    Input Parameters:
227d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
228d763cef2SBarry Smith .  A   - Jacobian matrix
229d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
230d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
231d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
232d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
233d763cef2SBarry Smith 
234d763cef2SBarry Smith    Calling sequence of func:
23587828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
236d763cef2SBarry Smith 
237d763cef2SBarry Smith +  t - current timestep
238d763cef2SBarry Smith .  u - input vector
239d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
240d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
241d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
242d763cef2SBarry Smith           structure (same as flag in SLESSetOperators())
243d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
244d763cef2SBarry Smith 
245d763cef2SBarry Smith    Notes:
246d763cef2SBarry Smith    See SLESSetOperators() for important information about setting the flag
247d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
248d763cef2SBarry Smith 
249d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
250d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
251d763cef2SBarry Smith    completely new new matrix structure (not just different matrix elements)
252d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
253d763cef2SBarry Smith    throughout the global iterations.
254d763cef2SBarry Smith 
255d763cef2SBarry Smith    Level: beginner
256d763cef2SBarry Smith 
257d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
258d763cef2SBarry Smith 
259d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(),
260d763cef2SBarry Smith           SNESDefaultComputeJacobianColor()
261d763cef2SBarry Smith 
262d763cef2SBarry Smith @*/
26387828ca2SBarry Smith int TSSetRHSJacobian(TS ts,Mat A,Mat B,int (*f)(TS,PetscReal,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
264d763cef2SBarry Smith {
265d763cef2SBarry Smith   PetscFunctionBegin;
266d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
267184914b5SBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE);
268184914b5SBarry Smith   PetscValidHeaderSpecific(B,MAT_COOKIE);
269184914b5SBarry Smith   PetscCheckSameComm(ts,A);
270184914b5SBarry Smith   PetscCheckSameComm(ts,B);
271d763cef2SBarry Smith   if (ts->problem_type != TS_NONLINEAR) {
27229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Not for linear problems; use TSSetRHSMatrix()");
273d763cef2SBarry Smith   }
274d763cef2SBarry Smith 
275000e7ae3SMatthew Knepley   ts->ops->rhsjacobian = f;
276d763cef2SBarry Smith   ts->jacP             = ctx;
277d763cef2SBarry Smith   ts->A                = A;
278d763cef2SBarry Smith   ts->B                = B;
279d763cef2SBarry Smith   PetscFunctionReturn(0);
280d763cef2SBarry Smith }
281d763cef2SBarry Smith 
2824a2ae208SSatish Balay #undef __FUNCT__
2834a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSBoundaryConditions"
284d763cef2SBarry Smith /*
285d763cef2SBarry Smith    TSComputeRHSBoundaryConditions - Evaluates the boundary condition function.
286d763cef2SBarry Smith 
287d763cef2SBarry Smith    Note: If the user did not provide a function but merely a matrix,
288d763cef2SBarry Smith    this routine applies the matrix.
289d763cef2SBarry Smith */
29087828ca2SBarry Smith int TSComputeRHSBoundaryConditions(TS ts,PetscReal t,Vec x)
291d763cef2SBarry Smith {
292d763cef2SBarry Smith   int ierr;
293d763cef2SBarry Smith 
294d763cef2SBarry Smith   PetscFunctionBegin;
295d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
296d763cef2SBarry Smith   PetscValidHeader(x);
297184914b5SBarry Smith   PetscCheckSameComm(ts,x);
298d763cef2SBarry Smith 
299000e7ae3SMatthew Knepley   if (ts->ops->rhsbc) {
300d763cef2SBarry Smith     PetscStackPush("TS user boundary condition function");
301000e7ae3SMatthew Knepley     ierr = (*ts->ops->rhsbc)(ts,t,x,ts->bcP);CHKERRQ(ierr);
302d763cef2SBarry Smith     PetscStackPop;
303d763cef2SBarry Smith     PetscFunctionReturn(0);
304d763cef2SBarry Smith   }
305d763cef2SBarry Smith 
306d763cef2SBarry Smith   PetscFunctionReturn(0);
307d763cef2SBarry Smith }
308d763cef2SBarry Smith 
3094a2ae208SSatish Balay #undef __FUNCT__
3104a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSBoundaryConditions"
311d763cef2SBarry Smith /*@C
312d763cef2SBarry Smith     TSSetRHSBoundaryConditions - Sets the routine for evaluating the function,
313d763cef2SBarry Smith     boundary conditions for the function F.
314d763cef2SBarry Smith 
315d763cef2SBarry Smith     Collective on TS
316d763cef2SBarry Smith 
317d763cef2SBarry Smith     Input Parameters:
318d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
319d763cef2SBarry Smith .   f - routine for evaluating the boundary condition function
320d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
321d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
322d763cef2SBarry Smith 
323d763cef2SBarry Smith     Calling sequence of func:
32487828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec F,void *ctx);
325d763cef2SBarry Smith 
326d763cef2SBarry Smith +   t - current timestep
327d763cef2SBarry Smith .   F - function vector
328d763cef2SBarry Smith -   ctx - [optional] user-defined function context
329d763cef2SBarry Smith 
330d763cef2SBarry Smith     Level: intermediate
331d763cef2SBarry Smith 
332d763cef2SBarry Smith .keywords: TS, timestep, set, boundary conditions, function
333d763cef2SBarry Smith @*/
33487828ca2SBarry Smith int TSSetRHSBoundaryConditions(TS ts,int (*f)(TS,PetscReal,Vec,void*),void *ctx)
335d763cef2SBarry Smith {
336d763cef2SBarry Smith   PetscFunctionBegin;
337d763cef2SBarry Smith 
338d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
339d763cef2SBarry Smith   if (ts->problem_type != TS_LINEAR) {
34029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For linear problems only");
341d763cef2SBarry Smith   }
342000e7ae3SMatthew Knepley   ts->ops->rhsbc = f;
343d763cef2SBarry Smith   ts->bcP        = ctx;
344d763cef2SBarry Smith   PetscFunctionReturn(0);
345d763cef2SBarry Smith }
346d763cef2SBarry Smith 
3474a2ae208SSatish Balay #undef __FUNCT__
3484a2ae208SSatish Balay #define __FUNCT__ "TSView"
3497e2c5f70SBarry Smith /*@C
350d763cef2SBarry Smith     TSView - Prints the TS data structure.
351d763cef2SBarry Smith 
3524c49b128SBarry Smith     Collective on TS
353d763cef2SBarry Smith 
354d763cef2SBarry Smith     Input Parameters:
355d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
356d763cef2SBarry Smith -   viewer - visualization context
357d763cef2SBarry Smith 
358d763cef2SBarry Smith     Options Database Key:
359d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
360d763cef2SBarry Smith 
361d763cef2SBarry Smith     Notes:
362d763cef2SBarry Smith     The available visualization contexts include
363b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
364b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
365d763cef2SBarry Smith          output where only the first processor opens
366d763cef2SBarry Smith          the file.  All other processors send their
367d763cef2SBarry Smith          data to the first processor to print.
368d763cef2SBarry Smith 
369d763cef2SBarry Smith     The user can open an alternative visualization context with
370b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
371d763cef2SBarry Smith 
372d763cef2SBarry Smith     Level: beginner
373d763cef2SBarry Smith 
374d763cef2SBarry Smith .keywords: TS, timestep, view
375d763cef2SBarry Smith 
376b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
377d763cef2SBarry Smith @*/
378b0a32e0cSBarry Smith int TSView(TS ts,PetscViewer viewer)
379d763cef2SBarry Smith {
380d763cef2SBarry Smith   int        ierr;
381454a90a3SBarry Smith   char       *type;
3826831982aSBarry Smith   PetscTruth isascii,isstring;
383d763cef2SBarry Smith 
384d763cef2SBarry Smith   PetscFunctionBegin;
385d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
386b0a32e0cSBarry Smith   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(ts->comm);
387b0a32e0cSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE);
3886831982aSBarry Smith   PetscCheckSameComm(ts,viewer);
389fd16b177SBarry Smith 
390b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
391b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
3920f5bd95cSBarry Smith   if (isascii) {
393b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"TS Object:\n");CHKERRQ(ierr);
394454a90a3SBarry Smith     ierr = TSGetType(ts,(TSType *)&type);CHKERRQ(ierr);
395454a90a3SBarry Smith     if (type) {
396b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
397184914b5SBarry Smith     } else {
398b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not yet set\n");CHKERRQ(ierr);
399184914b5SBarry Smith     }
400000e7ae3SMatthew Knepley     if (ts->ops->view) {
401b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
402000e7ae3SMatthew Knepley       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
403b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
404d763cef2SBarry Smith     }
405b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%d\n",ts->max_steps);CHKERRQ(ierr);
406b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",ts->max_time);CHKERRQ(ierr);
407d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
408b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%d\n",ts->nonlinear_its);CHKERRQ(ierr);
409d763cef2SBarry Smith     }
410b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%d\n",ts->linear_its);CHKERRQ(ierr);
4110f5bd95cSBarry Smith   } else if (isstring) {
412454a90a3SBarry Smith     ierr = TSGetType(ts,(TSType *)&type);CHKERRQ(ierr);
413b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
414d763cef2SBarry Smith   }
415b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
416d763cef2SBarry Smith   if (ts->sles) {ierr = SLESView(ts->sles,viewer);CHKERRQ(ierr);}
417d763cef2SBarry Smith   if (ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
418b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
419d763cef2SBarry Smith   PetscFunctionReturn(0);
420d763cef2SBarry Smith }
421d763cef2SBarry Smith 
422d763cef2SBarry Smith 
4234a2ae208SSatish Balay #undef __FUNCT__
4244a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
425d763cef2SBarry Smith /*@C
426d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
427d763cef2SBarry Smith    the timesteppers.
428d763cef2SBarry Smith 
429d763cef2SBarry Smith    Collective on TS
430d763cef2SBarry Smith 
431d763cef2SBarry Smith    Input Parameters:
432d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
433d763cef2SBarry Smith -  usrP - optional user context
434d763cef2SBarry Smith 
435d763cef2SBarry Smith    Level: intermediate
436d763cef2SBarry Smith 
437d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
438d763cef2SBarry Smith 
439d763cef2SBarry Smith .seealso: TSGetApplicationContext()
440d763cef2SBarry Smith @*/
441d763cef2SBarry Smith int TSSetApplicationContext(TS ts,void *usrP)
442d763cef2SBarry Smith {
443d763cef2SBarry Smith   PetscFunctionBegin;
444d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
445d763cef2SBarry Smith   ts->user = usrP;
446d763cef2SBarry Smith   PetscFunctionReturn(0);
447d763cef2SBarry Smith }
448d763cef2SBarry Smith 
4494a2ae208SSatish Balay #undef __FUNCT__
4504a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
451d763cef2SBarry Smith /*@C
452d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
453d763cef2SBarry Smith     timestepper.
454d763cef2SBarry Smith 
455d763cef2SBarry Smith     Not Collective
456d763cef2SBarry Smith 
457d763cef2SBarry Smith     Input Parameter:
458d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
459d763cef2SBarry Smith 
460d763cef2SBarry Smith     Output Parameter:
461d763cef2SBarry Smith .   usrP - user context
462d763cef2SBarry Smith 
463d763cef2SBarry Smith     Level: intermediate
464d763cef2SBarry Smith 
465d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
466d763cef2SBarry Smith 
467d763cef2SBarry Smith .seealso: TSSetApplicationContext()
468d763cef2SBarry Smith @*/
469d763cef2SBarry Smith int TSGetApplicationContext(TS ts,void **usrP)
470d763cef2SBarry Smith {
471d763cef2SBarry Smith   PetscFunctionBegin;
472d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
473d763cef2SBarry Smith   *usrP = ts->user;
474d763cef2SBarry Smith   PetscFunctionReturn(0);
475d763cef2SBarry Smith }
476d763cef2SBarry Smith 
4774a2ae208SSatish Balay #undef __FUNCT__
4784a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
479d763cef2SBarry Smith /*@
480d763cef2SBarry Smith    TSGetTimeStepNumber - Gets the current number of timesteps.
481d763cef2SBarry Smith 
482d763cef2SBarry Smith    Not Collective
483d763cef2SBarry Smith 
484d763cef2SBarry Smith    Input Parameter:
485d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
486d763cef2SBarry Smith 
487d763cef2SBarry Smith    Output Parameter:
488d763cef2SBarry Smith .  iter - number steps so far
489d763cef2SBarry Smith 
490d763cef2SBarry Smith    Level: intermediate
491d763cef2SBarry Smith 
492d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
493d763cef2SBarry Smith @*/
494d763cef2SBarry Smith int TSGetTimeStepNumber(TS ts,int* iter)
495d763cef2SBarry Smith {
496d763cef2SBarry Smith   PetscFunctionBegin;
497d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
498d763cef2SBarry Smith   *iter = ts->steps;
499d763cef2SBarry Smith   PetscFunctionReturn(0);
500d763cef2SBarry Smith }
501d763cef2SBarry Smith 
5024a2ae208SSatish Balay #undef __FUNCT__
5034a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
504d763cef2SBarry Smith /*@
505d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
506d763cef2SBarry Smith    as well as the initial time.
507d763cef2SBarry Smith 
508d763cef2SBarry Smith    Collective on TS
509d763cef2SBarry Smith 
510d763cef2SBarry Smith    Input Parameters:
511d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
512d763cef2SBarry Smith .  initial_time - the initial time
513d763cef2SBarry Smith -  time_step - the size of the timestep
514d763cef2SBarry Smith 
515d763cef2SBarry Smith    Level: intermediate
516d763cef2SBarry Smith 
517d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
518d763cef2SBarry Smith 
519d763cef2SBarry Smith .keywords: TS, set, initial, timestep
520d763cef2SBarry Smith @*/
52187828ca2SBarry Smith int TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
522d763cef2SBarry Smith {
523d763cef2SBarry Smith   PetscFunctionBegin;
524d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
525d763cef2SBarry Smith   ts->time_step         = time_step;
526d763cef2SBarry Smith   ts->initial_time_step = time_step;
527d763cef2SBarry Smith   ts->ptime             = initial_time;
528d763cef2SBarry Smith   PetscFunctionReturn(0);
529d763cef2SBarry Smith }
530d763cef2SBarry Smith 
5314a2ae208SSatish Balay #undef __FUNCT__
5324a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
533d763cef2SBarry Smith /*@
534d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
535d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
536d763cef2SBarry Smith 
537d763cef2SBarry Smith    Collective on TS
538d763cef2SBarry Smith 
539d763cef2SBarry Smith    Input Parameters:
540d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
541d763cef2SBarry Smith -  time_step - the size of the timestep
542d763cef2SBarry Smith 
543d763cef2SBarry Smith    Level: intermediate
544d763cef2SBarry Smith 
545d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
546d763cef2SBarry Smith 
547d763cef2SBarry Smith .keywords: TS, set, timestep
548d763cef2SBarry Smith @*/
54987828ca2SBarry Smith int TSSetTimeStep(TS ts,PetscReal time_step)
550d763cef2SBarry Smith {
551d763cef2SBarry Smith   PetscFunctionBegin;
552d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
553d763cef2SBarry Smith   ts->time_step = time_step;
554d763cef2SBarry Smith   PetscFunctionReturn(0);
555d763cef2SBarry Smith }
556d763cef2SBarry Smith 
5574a2ae208SSatish Balay #undef __FUNCT__
5584a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
559d763cef2SBarry Smith /*@
560d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
561d763cef2SBarry Smith 
562d763cef2SBarry Smith    Not Collective
563d763cef2SBarry Smith 
564d763cef2SBarry Smith    Input Parameter:
565d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
566d763cef2SBarry Smith 
567d763cef2SBarry Smith    Output Parameter:
568d763cef2SBarry Smith .  dt - the current timestep size
569d763cef2SBarry Smith 
570d763cef2SBarry Smith    Level: intermediate
571d763cef2SBarry Smith 
572d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
573d763cef2SBarry Smith 
574d763cef2SBarry Smith .keywords: TS, get, timestep
575d763cef2SBarry Smith @*/
57687828ca2SBarry Smith int TSGetTimeStep(TS ts,PetscReal* dt)
577d763cef2SBarry Smith {
578d763cef2SBarry Smith   PetscFunctionBegin;
579d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
580d763cef2SBarry Smith   *dt = ts->time_step;
581d763cef2SBarry Smith   PetscFunctionReturn(0);
582d763cef2SBarry Smith }
583d763cef2SBarry Smith 
5844a2ae208SSatish Balay #undef __FUNCT__
5854a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
586d763cef2SBarry Smith /*@C
587d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
588d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
589d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
590d763cef2SBarry Smith    the solution at the next timestep has been calculated.
591d763cef2SBarry Smith 
592d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
593d763cef2SBarry Smith 
594d763cef2SBarry Smith    Input Parameter:
595d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
596d763cef2SBarry Smith 
597d763cef2SBarry Smith    Output Parameter:
598d763cef2SBarry Smith .  v - the vector containing the solution
599d763cef2SBarry Smith 
600d763cef2SBarry Smith    Level: intermediate
601d763cef2SBarry Smith 
602d763cef2SBarry Smith .seealso: TSGetTimeStep()
603d763cef2SBarry Smith 
604d763cef2SBarry Smith .keywords: TS, timestep, get, solution
605d763cef2SBarry Smith @*/
606d763cef2SBarry Smith int TSGetSolution(TS ts,Vec *v)
607d763cef2SBarry Smith {
608d763cef2SBarry Smith   PetscFunctionBegin;
609d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
610d763cef2SBarry Smith   *v = ts->vec_sol_always;
611d763cef2SBarry Smith   PetscFunctionReturn(0);
612d763cef2SBarry Smith }
613d763cef2SBarry Smith 
6144a2ae208SSatish Balay #undef __FUNCT__
6154a2ae208SSatish Balay #define __FUNCT__ "TSPublish_Petsc"
616454a90a3SBarry Smith static int TSPublish_Petsc(PetscObject obj)
617d763cef2SBarry Smith {
618aa482453SBarry Smith #if defined(PETSC_HAVE_AMS)
619454a90a3SBarry Smith   TS   v = (TS) obj;
620d763cef2SBarry Smith   int  ierr;
62143d6d2cbSBarry Smith #endif
622d763cef2SBarry Smith 
623d763cef2SBarry Smith   PetscFunctionBegin;
624d763cef2SBarry Smith 
62543d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS)
626d763cef2SBarry Smith   /* if it is already published then return */
627d763cef2SBarry Smith   if (v->amem >=0) PetscFunctionReturn(0);
628d763cef2SBarry Smith 
629454a90a3SBarry Smith   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
630d763cef2SBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Step",&v->steps,1,AMS_INT,AMS_READ,
631d763cef2SBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
632d763cef2SBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Time",&v->ptime,1,AMS_DOUBLE,AMS_READ,
633d763cef2SBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
634d763cef2SBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"CurrentTimeStep",&v->time_step,1,
635d763cef2SBarry Smith                                AMS_DOUBLE,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
636454a90a3SBarry Smith   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
637d763cef2SBarry Smith #endif
638d763cef2SBarry Smith   PetscFunctionReturn(0);
639d763cef2SBarry Smith }
640d763cef2SBarry Smith 
641d763cef2SBarry Smith /* -----------------------------------------------------------*/
642d763cef2SBarry Smith 
6434a2ae208SSatish Balay #undef __FUNCT__
6444a2ae208SSatish Balay #define __FUNCT__ "TSCreate"
645d763cef2SBarry Smith /*@C
646d763cef2SBarry Smith    TSCreate - Creates a timestepper context.
647d763cef2SBarry Smith 
648d763cef2SBarry Smith    Collective on MPI_Comm
649d763cef2SBarry Smith 
650d763cef2SBarry Smith    Input Parameter:
651d763cef2SBarry Smith +  comm - MPI communicator
652d763cef2SBarry Smith -  type - One of  TS_LINEAR,TS_NONLINEAR
653d763cef2SBarry Smith    where these types refer to problems of the forms
654d763cef2SBarry Smith .vb
655d763cef2SBarry Smith          U_t = A U
656d763cef2SBarry Smith          U_t = A(t) U
657d763cef2SBarry Smith          U_t = F(t,U)
658d763cef2SBarry Smith .ve
659d763cef2SBarry Smith 
660d763cef2SBarry Smith    Output Parameter:
661000e7ae3SMatthew Knepley .  ts - the new TS context
662d763cef2SBarry Smith 
663d763cef2SBarry Smith    Level: beginner
664d763cef2SBarry Smith 
665d763cef2SBarry Smith .keywords: TS, timestep, create, context
666d763cef2SBarry Smith 
667435da068SBarry Smith .seealso: TSSetUp(), TSStep(), TSDestroy(), TSProblemType, TS
668d763cef2SBarry Smith @*/
669000e7ae3SMatthew Knepley int TSCreate(MPI_Comm comm, TSProblemType problemtype, TS *ts)
670d763cef2SBarry Smith {
671000e7ae3SMatthew Knepley   TS  t;
672000e7ae3SMatthew Knepley   int ierr;
673d763cef2SBarry Smith 
674d763cef2SBarry Smith   PetscFunctionBegin;
675000e7ae3SMatthew Knepley   *ts = PETSC_NULL;
676000e7ae3SMatthew Knepley   PetscValidPointer(ts);
677000e7ae3SMatthew Knepley   PetscHeaderCreate(t, _p_TS, struct _TSOps, TS_COOKIE, -1, "TS", comm, TSDestroy, TSView);
678000e7ae3SMatthew Knepley   PetscLogObjectCreate(t);
679000e7ae3SMatthew Knepley   PetscLogObjectMemory(t, sizeof(struct _p_TS));
680000e7ae3SMatthew Knepley   ierr = PetscMemzero(t->ops, sizeof(struct _TSOps));                                                     CHKERRQ(ierr);
681000e7ae3SMatthew Knepley   t->bops->publish      = TSPublish_Petsc;
682000e7ae3SMatthew Knepley   t->type_name          = PETSC_NULL;
683d763cef2SBarry Smith 
684000e7ae3SMatthew Knepley   t->problem_type       = problemtype;
685000e7ae3SMatthew Knepley   t->vec_sol            = PETSC_NULL;
686000e7ae3SMatthew Knepley   t->vec_sol_always     = PETSC_NULL;
687000e7ae3SMatthew Knepley   t->numbermonitors     = 0;
688000e7ae3SMatthew Knepley   t->isGTS              = PETSC_FALSE;
6897f5a67d6SMatthew Knepley   t->isExplicit         = PETSC_NULL;
690000e7ae3SMatthew Knepley   t->Iindex             = PETSC_NULL;
691000e7ae3SMatthew Knepley   t->sles               = PETSC_NULL;
692000e7ae3SMatthew Knepley   t->A                  = PETSC_NULL;
693000e7ae3SMatthew Knepley   t->B                  = PETSC_NULL;
694000e7ae3SMatthew Knepley   t->snes               = PETSC_NULL;
695000e7ae3SMatthew Knepley   t->funP               = PETSC_NULL;
696000e7ae3SMatthew Knepley   t->jacP               = PETSC_NULL;
697000e7ae3SMatthew Knepley   t->setupcalled        = 0;
698000e7ae3SMatthew Knepley   t->data               = PETSC_NULL;
699000e7ae3SMatthew Knepley   t->user               = PETSC_NULL;
700000e7ae3SMatthew Knepley   t->max_steps          = 5000;
701000e7ae3SMatthew Knepley   t->max_time           = 5.0;
702000e7ae3SMatthew Knepley   t->time_step          = .1;
703000e7ae3SMatthew Knepley   t->time_step_old      = t->time_step;
704000e7ae3SMatthew Knepley   t->initial_time_step  = t->time_step;
705000e7ae3SMatthew Knepley   t->steps              = 0;
706000e7ae3SMatthew Knepley   t->ptime              = 0.0;
707000e7ae3SMatthew Knepley   t->linear_its         = 0;
708000e7ae3SMatthew Knepley   t->nonlinear_its      = 0;
709000e7ae3SMatthew Knepley   t->work               = PETSC_NULL;
710000e7ae3SMatthew Knepley   t->nwork              = 0;
711000e7ae3SMatthew Knepley   t->ops->applymatrixbc = TSDefaultSystemMatrixBC;
712000e7ae3SMatthew Knepley   t->ops->applyrhsbc    = TSDefaultRhsBC;
713000e7ae3SMatthew Knepley   t->ops->applysolbc    = TSDefaultSolutionBC;
714000e7ae3SMatthew Knepley   t->ops->prestep       = TSDefaultPreStep;
715000e7ae3SMatthew Knepley   t->ops->update        = TSDefaultUpdate;
716000e7ae3SMatthew Knepley   t->ops->poststep      = TSDefaultPostStep;
717000e7ae3SMatthew Knepley   t->ops->reform        = PETSC_NULL;
718000e7ae3SMatthew Knepley   t->ops->reallocate    = PETSC_NULL;
719000e7ae3SMatthew Knepley   t->ops->serialize     = PETSC_NULL;
720000e7ae3SMatthew Knepley 
721000e7ae3SMatthew Knepley   *ts = t;
722d763cef2SBarry Smith   PetscFunctionReturn(0);
723d763cef2SBarry Smith }
724d763cef2SBarry Smith 
725d763cef2SBarry Smith /* ----- Routines to initialize and destroy a timestepper ---- */
726d763cef2SBarry Smith 
7274a2ae208SSatish Balay #undef __FUNCT__
7284a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
729d763cef2SBarry Smith /*@
730d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
731d763cef2SBarry Smith    of a timestepper.
732d763cef2SBarry Smith 
733d763cef2SBarry Smith    Collective on TS
734d763cef2SBarry Smith 
735d763cef2SBarry Smith    Input Parameter:
736d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
737d763cef2SBarry Smith 
738d763cef2SBarry Smith    Notes:
739d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
740d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
741d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
742d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
743d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
744d763cef2SBarry Smith 
745d763cef2SBarry Smith    Level: advanced
746d763cef2SBarry Smith 
747d763cef2SBarry Smith .keywords: TS, timestep, setup
748d763cef2SBarry Smith 
749d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
750d763cef2SBarry Smith @*/
751d763cef2SBarry Smith int TSSetUp(TS ts)
752d763cef2SBarry Smith {
753d763cef2SBarry Smith   int ierr;
754d763cef2SBarry Smith 
755d763cef2SBarry Smith   PetscFunctionBegin;
756d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
75729bbc08cSBarry Smith   if (!ts->vec_sol) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
758d763cef2SBarry Smith   if (!ts->type_name) {
759d763cef2SBarry Smith     ierr = TSSetType(ts,TS_EULER);CHKERRQ(ierr);
760d763cef2SBarry Smith   }
761000e7ae3SMatthew Knepley   ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
762d763cef2SBarry Smith   ts->setupcalled = 1;
763d763cef2SBarry Smith   PetscFunctionReturn(0);
764d763cef2SBarry Smith }
765d763cef2SBarry Smith 
7664a2ae208SSatish Balay #undef __FUNCT__
7674a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
768d763cef2SBarry Smith /*@C
769d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
770d763cef2SBarry Smith    with TSCreate().
771d763cef2SBarry Smith 
772d763cef2SBarry Smith    Collective on TS
773d763cef2SBarry Smith 
774d763cef2SBarry Smith    Input Parameter:
775d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
776d763cef2SBarry Smith 
777d763cef2SBarry Smith    Level: beginner
778d763cef2SBarry Smith 
779d763cef2SBarry Smith .keywords: TS, timestepper, destroy
780d763cef2SBarry Smith 
781d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
782d763cef2SBarry Smith @*/
783d763cef2SBarry Smith int TSDestroy(TS ts)
784d763cef2SBarry Smith {
785329f5518SBarry Smith   int ierr,i;
786d763cef2SBarry Smith 
787d763cef2SBarry Smith   PetscFunctionBegin;
788d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
789d763cef2SBarry Smith   if (--ts->refct > 0) PetscFunctionReturn(0);
790d763cef2SBarry Smith 
791be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
7920f5bd95cSBarry Smith   ierr = PetscObjectDepublish(ts);CHKERRQ(ierr);
793be0abb6dSBarry Smith 
794d763cef2SBarry Smith   if (ts->sles) {ierr = SLESDestroy(ts->sles);CHKERRQ(ierr);}
795d763cef2SBarry Smith   if (ts->snes) {ierr = SNESDestroy(ts->snes);CHKERRQ(ierr);}
796000e7ae3SMatthew Knepley   ierr = (*(ts)->ops->destroy)(ts);CHKERRQ(ierr);
797329f5518SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
798329f5518SBarry Smith     if (ts->mdestroy[i]) {
799329f5518SBarry Smith       ierr = (*ts->mdestroy[i])(ts->monitorcontext[i]);CHKERRQ(ierr);
800329f5518SBarry Smith     }
801329f5518SBarry Smith   }
802b0a32e0cSBarry Smith   PetscLogObjectDestroy((PetscObject)ts);
803d763cef2SBarry Smith   PetscHeaderDestroy((PetscObject)ts);
804d763cef2SBarry Smith   PetscFunctionReturn(0);
805d763cef2SBarry Smith }
806d763cef2SBarry Smith 
8074a2ae208SSatish Balay #undef __FUNCT__
8084a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
809d763cef2SBarry Smith /*@C
810d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
811d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
812d763cef2SBarry Smith 
813d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
814d763cef2SBarry Smith 
815d763cef2SBarry Smith    Input Parameter:
816d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
817d763cef2SBarry Smith 
818d763cef2SBarry Smith    Output Parameter:
819d763cef2SBarry Smith .  snes - the nonlinear solver context
820d763cef2SBarry Smith 
821d763cef2SBarry Smith    Notes:
822d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
823d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
824d763cef2SBarry Smith    SLES, KSP, and PC contexts as well.
825d763cef2SBarry Smith 
826d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
827d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
828d763cef2SBarry Smith 
829d763cef2SBarry Smith    Level: beginner
830d763cef2SBarry Smith 
831d763cef2SBarry Smith .keywords: timestep, get, SNES
832d763cef2SBarry Smith @*/
833d763cef2SBarry Smith int TSGetSNES(TS ts,SNES *snes)
834d763cef2SBarry Smith {
835d763cef2SBarry Smith   PetscFunctionBegin;
836d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
83729bbc08cSBarry Smith   if (ts->problem_type == TS_LINEAR) SETERRQ(PETSC_ERR_ARG_WRONG,"Nonlinear only; use TSGetSLES()");
838d763cef2SBarry Smith   *snes = ts->snes;
839d763cef2SBarry Smith   PetscFunctionReturn(0);
840d763cef2SBarry Smith }
841d763cef2SBarry Smith 
8424a2ae208SSatish Balay #undef __FUNCT__
8434a2ae208SSatish Balay #define __FUNCT__ "TSGetSLES"
844d763cef2SBarry Smith /*@C
845d763cef2SBarry Smith    TSGetSLES - Returns the SLES (linear solver) associated with
846d763cef2SBarry Smith    a TS (timestepper) context.
847d763cef2SBarry Smith 
848d763cef2SBarry Smith    Not Collective, but SLES is parallel if TS is parallel
849d763cef2SBarry Smith 
850d763cef2SBarry Smith    Input Parameter:
851d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
852d763cef2SBarry Smith 
853d763cef2SBarry Smith    Output Parameter:
854d763cef2SBarry Smith .  sles - the nonlinear solver context
855d763cef2SBarry Smith 
856d763cef2SBarry Smith    Notes:
857d763cef2SBarry Smith    The user can then directly manipulate the SLES context to set various
858d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
859d763cef2SBarry Smith    KSP and PC contexts as well.
860d763cef2SBarry Smith 
861d763cef2SBarry Smith    TSGetSLES() does not work for integrators that do not use SLES;
862d763cef2SBarry Smith    in this case TSGetSLES() returns PETSC_NULL in sles.
863d763cef2SBarry Smith 
864d763cef2SBarry Smith    Level: beginner
865d763cef2SBarry Smith 
866d763cef2SBarry Smith .keywords: timestep, get, SLES
867d763cef2SBarry Smith @*/
868d763cef2SBarry Smith int TSGetSLES(TS ts,SLES *sles)
869d763cef2SBarry Smith {
870d763cef2SBarry Smith   PetscFunctionBegin;
871d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
87229bbc08cSBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
873d763cef2SBarry Smith   *sles = ts->sles;
874d763cef2SBarry Smith   PetscFunctionReturn(0);
875d763cef2SBarry Smith }
876d763cef2SBarry Smith 
877d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
878d763cef2SBarry Smith 
8794a2ae208SSatish Balay #undef __FUNCT__
8804a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
881d763cef2SBarry Smith /*@
882d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
883d763cef2SBarry Smith    maximum time for iteration.
884d763cef2SBarry Smith 
885d763cef2SBarry Smith    Collective on TS
886d763cef2SBarry Smith 
887d763cef2SBarry Smith    Input Parameters:
888d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
889d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
890d763cef2SBarry Smith -  maxtime - final time to iterate to
891d763cef2SBarry Smith 
892d763cef2SBarry Smith    Options Database Keys:
893d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
894d763cef2SBarry Smith .  -ts_max_time <maxtime> - Sets maxtime
895d763cef2SBarry Smith 
896d763cef2SBarry Smith    Notes:
897d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
898d763cef2SBarry Smith 
899d763cef2SBarry Smith    Level: intermediate
900d763cef2SBarry Smith 
901d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
902d763cef2SBarry Smith @*/
90387828ca2SBarry Smith int TSSetDuration(TS ts,int maxsteps,PetscReal maxtime)
904d763cef2SBarry Smith {
905d763cef2SBarry Smith   PetscFunctionBegin;
906d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
907d763cef2SBarry Smith   ts->max_steps = maxsteps;
908d763cef2SBarry Smith   ts->max_time  = maxtime;
909d763cef2SBarry Smith   PetscFunctionReturn(0);
910d763cef2SBarry Smith }
911d763cef2SBarry Smith 
9124a2ae208SSatish Balay #undef __FUNCT__
9134a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
914d763cef2SBarry Smith /*@
915d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
916d763cef2SBarry Smith    for use by the TS routines.
917d763cef2SBarry Smith 
918d763cef2SBarry Smith    Collective on TS and Vec
919d763cef2SBarry Smith 
920d763cef2SBarry Smith    Input Parameters:
921d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
922d763cef2SBarry Smith -  x - the solution vector
923d763cef2SBarry Smith 
924d763cef2SBarry Smith    Level: beginner
925d763cef2SBarry Smith 
926d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
927d763cef2SBarry Smith @*/
928d763cef2SBarry Smith int TSSetSolution(TS ts,Vec x)
929d763cef2SBarry Smith {
930d763cef2SBarry Smith   PetscFunctionBegin;
931d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
932d763cef2SBarry Smith   ts->vec_sol        = ts->vec_sol_always = x;
933d763cef2SBarry Smith   PetscFunctionReturn(0);
934d763cef2SBarry Smith }
935d763cef2SBarry Smith 
936*e74ef692SMatthew Knepley #undef __FUNCT__
937*e74ef692SMatthew Knepley #define __FUNCT__ "TSSetRhsBC"
938000e7ae3SMatthew Knepley /*@
939000e7ae3SMatthew Knepley   TSSetRhsBC - Sets the function which applies boundary conditions
940000e7ae3SMatthew Knepley   to the Rhs of each system.
941000e7ae3SMatthew Knepley 
942000e7ae3SMatthew Knepley   Collective on TS
943000e7ae3SMatthew Knepley 
944000e7ae3SMatthew Knepley   Input Parameters:
945000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
946000e7ae3SMatthew Knepley - func - The function
947000e7ae3SMatthew Knepley 
948000e7ae3SMatthew Knepley   Calling sequence of func:
949000e7ae3SMatthew Knepley . func (TS ts, Vec rhs, void *ctx);
950000e7ae3SMatthew Knepley 
951000e7ae3SMatthew Knepley + rhs - The current rhs vector
952000e7ae3SMatthew Knepley - ctx - The user-context
953000e7ae3SMatthew Knepley 
954000e7ae3SMatthew Knepley   Level: intermediate
955000e7ae3SMatthew Knepley 
956000e7ae3SMatthew Knepley .keywords: TS, Rhs, boundary conditions
957000e7ae3SMatthew Knepley @*/
958000e7ae3SMatthew Knepley int TSSetRhsBC(TS ts, int (*func)(TS, Vec, void *))
959000e7ae3SMatthew Knepley {
960000e7ae3SMatthew Knepley   PetscFunctionBegin;
961000e7ae3SMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
962000e7ae3SMatthew Knepley   ts->ops->applyrhsbc = func;
963000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
964000e7ae3SMatthew Knepley }
965000e7ae3SMatthew Knepley 
966*e74ef692SMatthew Knepley #undef __FUNCT__
967*e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultRhsBC"
968000e7ae3SMatthew Knepley /*@
969000e7ae3SMatthew Knepley   TSDefaultRhsBC - The default boundary condition function which does nothing.
970000e7ae3SMatthew Knepley 
971000e7ae3SMatthew Knepley   Collective on TS
972000e7ae3SMatthew Knepley 
973000e7ae3SMatthew Knepley   Input Parameters:
974000e7ae3SMatthew Knepley + ts  - The TS context obtained from TSCreate()
975000e7ae3SMatthew Knepley . rhs - The Rhs
976000e7ae3SMatthew Knepley - ctx - The user-context
977000e7ae3SMatthew Knepley 
978000e7ae3SMatthew Knepley   Level: developer
979000e7ae3SMatthew Knepley 
980000e7ae3SMatthew Knepley .keywords: TS, Rhs, boundary conditions
981000e7ae3SMatthew Knepley @*/
982000e7ae3SMatthew Knepley int TSDefaultRhsBC(TS ts,  Vec rhs, void *ctx)
983000e7ae3SMatthew Knepley {
984000e7ae3SMatthew Knepley   PetscFunctionBegin;
985000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
986000e7ae3SMatthew Knepley }
987000e7ae3SMatthew Knepley 
988*e74ef692SMatthew Knepley #undef __FUNCT__
989*e74ef692SMatthew Knepley #define __FUNCT__ "TSSetSystemMatrixBC"
990000e7ae3SMatthew Knepley /*@
991000e7ae3SMatthew Knepley   TSSetSystemMatrixBC - Sets the function which applies boundary conditions
992000e7ae3SMatthew Knepley   to the system matrix and preconditioner of each system.
993000e7ae3SMatthew Knepley 
994000e7ae3SMatthew Knepley   Collective on TS
995000e7ae3SMatthew Knepley 
996000e7ae3SMatthew Knepley   Input Parameters:
997000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
998000e7ae3SMatthew Knepley - func - The function
999000e7ae3SMatthew Knepley 
1000000e7ae3SMatthew Knepley   Calling sequence of func:
1001000e7ae3SMatthew Knepley . func (TS ts, Mat A, Mat B, void *ctx);
1002000e7ae3SMatthew Knepley 
1003000e7ae3SMatthew Knepley + A   - The current system matrix
1004000e7ae3SMatthew Knepley . B   - The current preconditioner
1005000e7ae3SMatthew Knepley - ctx - The user-context
1006000e7ae3SMatthew Knepley 
1007000e7ae3SMatthew Knepley   Level: intermediate
1008000e7ae3SMatthew Knepley 
1009000e7ae3SMatthew Knepley .keywords: TS, System matrix, boundary conditions
1010000e7ae3SMatthew Knepley @*/
1011000e7ae3SMatthew Knepley int TSSetSystemMatrixBC(TS ts, int (*func)(TS, Mat, Mat, void *))
1012000e7ae3SMatthew Knepley {
1013000e7ae3SMatthew Knepley   PetscFunctionBegin;
1014000e7ae3SMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
1015000e7ae3SMatthew Knepley   ts->ops->applymatrixbc = func;
1016000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1017000e7ae3SMatthew Knepley }
1018000e7ae3SMatthew Knepley 
1019*e74ef692SMatthew Knepley #undef __FUNCT__
1020*e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultSystemMatrixBC"
1021000e7ae3SMatthew Knepley /*@
1022000e7ae3SMatthew Knepley   TSDefaultSystemMatrixBC - The default boundary condition function which
1023000e7ae3SMatthew Knepley   does nothing.
1024000e7ae3SMatthew Knepley 
1025000e7ae3SMatthew Knepley   Collective on TS
1026000e7ae3SMatthew Knepley 
1027000e7ae3SMatthew Knepley   Input Parameters:
1028000e7ae3SMatthew Knepley + ts  - The TS context obtained from TSCreate()
1029000e7ae3SMatthew Knepley . A   - The system matrix
1030000e7ae3SMatthew Knepley . B   - The preconditioner
1031000e7ae3SMatthew Knepley - ctx - The user-context
1032000e7ae3SMatthew Knepley 
1033000e7ae3SMatthew Knepley   Level: developer
1034000e7ae3SMatthew Knepley 
1035000e7ae3SMatthew Knepley .keywords: TS, System matrix, boundary conditions
1036000e7ae3SMatthew Knepley @*/
1037000e7ae3SMatthew Knepley int TSDefaultSystemMatrixBC(TS ts, Mat A, Mat B, void *ctx)
1038000e7ae3SMatthew Knepley {
1039000e7ae3SMatthew Knepley   PetscFunctionBegin;
1040000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1041000e7ae3SMatthew Knepley }
1042000e7ae3SMatthew Knepley 
1043*e74ef692SMatthew Knepley #undef __FUNCT__
1044*e74ef692SMatthew Knepley #define __FUNCT__ "TSSetSolutionBC"
1045000e7ae3SMatthew Knepley /*@
1046000e7ae3SMatthew Knepley   TSSetSolutionBC - Sets the function which applies boundary conditions
1047000e7ae3SMatthew Knepley   to the solution of each system. This is necessary in nonlinear systems
1048000e7ae3SMatthew Knepley   which time dependent boundary conditions.
1049000e7ae3SMatthew Knepley 
1050000e7ae3SMatthew Knepley   Collective on TS
1051000e7ae3SMatthew Knepley 
1052000e7ae3SMatthew Knepley   Input Parameters:
1053000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1054000e7ae3SMatthew Knepley - func - The function
1055000e7ae3SMatthew Knepley 
1056000e7ae3SMatthew Knepley   Calling sequence of func:
1057000e7ae3SMatthew Knepley . func (TS ts, Vec rsol, void *ctx);
1058000e7ae3SMatthew Knepley 
1059000e7ae3SMatthew Knepley + sol - The current solution vector
1060000e7ae3SMatthew Knepley - ctx - The user-context
1061000e7ae3SMatthew Knepley 
1062000e7ae3SMatthew Knepley   Level: intermediate
1063000e7ae3SMatthew Knepley 
1064000e7ae3SMatthew Knepley .keywords: TS, solution, boundary conditions
1065000e7ae3SMatthew Knepley @*/
1066000e7ae3SMatthew Knepley int TSSetSolutionBC(TS ts, int (*func)(TS, Vec, void *))
1067000e7ae3SMatthew Knepley {
1068000e7ae3SMatthew Knepley   PetscFunctionBegin;
1069000e7ae3SMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
1070000e7ae3SMatthew Knepley   ts->ops->applysolbc = func;
1071000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1072000e7ae3SMatthew Knepley }
1073000e7ae3SMatthew Knepley 
1074*e74ef692SMatthew Knepley #undef __FUNCT__
1075*e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultSolutionBC"
1076000e7ae3SMatthew Knepley /*@
1077000e7ae3SMatthew Knepley   TSDefaultSolutionBC - The default boundary condition function which
1078000e7ae3SMatthew Knepley   does nothing.
1079000e7ae3SMatthew Knepley 
1080000e7ae3SMatthew Knepley   Collective on TS
1081000e7ae3SMatthew Knepley 
1082000e7ae3SMatthew Knepley   Input Parameters:
1083000e7ae3SMatthew Knepley + ts  - The TS context obtained from TSCreate()
1084000e7ae3SMatthew Knepley . sol - The solution
1085000e7ae3SMatthew Knepley - ctx - The user-context
1086000e7ae3SMatthew Knepley 
1087000e7ae3SMatthew Knepley   Level: developer
1088000e7ae3SMatthew Knepley 
1089000e7ae3SMatthew Knepley .keywords: TS, solution, boundary conditions
1090000e7ae3SMatthew Knepley @*/
1091000e7ae3SMatthew Knepley int TSDefaultSolutionBC(TS ts, Vec sol, void *ctx)
1092000e7ae3SMatthew Knepley {
1093000e7ae3SMatthew Knepley   PetscFunctionBegin;
1094000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1095000e7ae3SMatthew Knepley }
1096000e7ae3SMatthew Knepley 
1097*e74ef692SMatthew Knepley #undef __FUNCT__
1098*e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1099000e7ae3SMatthew Knepley /*@
1100000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
1101000e7ae3SMatthew Knepley   called once at the beginning of time stepping.
1102000e7ae3SMatthew Knepley 
1103000e7ae3SMatthew Knepley   Collective on TS
1104000e7ae3SMatthew Knepley 
1105000e7ae3SMatthew Knepley   Input Parameters:
1106000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1107000e7ae3SMatthew Knepley - func - The function
1108000e7ae3SMatthew Knepley 
1109000e7ae3SMatthew Knepley   Calling sequence of func:
1110000e7ae3SMatthew Knepley . func (TS ts);
1111000e7ae3SMatthew Knepley 
1112000e7ae3SMatthew Knepley   Level: intermediate
1113000e7ae3SMatthew Knepley 
1114000e7ae3SMatthew Knepley .keywords: TS, timestep
1115000e7ae3SMatthew Knepley @*/
1116000e7ae3SMatthew Knepley int TSSetPreStep(TS ts, int (*func)(TS))
1117000e7ae3SMatthew Knepley {
1118000e7ae3SMatthew Knepley   PetscFunctionBegin;
1119000e7ae3SMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
1120000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1121000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1122000e7ae3SMatthew Knepley }
1123000e7ae3SMatthew Knepley 
1124*e74ef692SMatthew Knepley #undef __FUNCT__
1125*e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultPreStep"
1126000e7ae3SMatthew Knepley /*@
1127000e7ae3SMatthew Knepley   TSDefaultPreStep - The default pre-stepping function which does nothing.
1128000e7ae3SMatthew Knepley 
1129000e7ae3SMatthew Knepley   Collective on TS
1130000e7ae3SMatthew Knepley 
1131000e7ae3SMatthew Knepley   Input Parameters:
1132000e7ae3SMatthew Knepley . ts  - The TS context obtained from TSCreate()
1133000e7ae3SMatthew Knepley 
1134000e7ae3SMatthew Knepley   Level: developer
1135000e7ae3SMatthew Knepley 
1136000e7ae3SMatthew Knepley .keywords: TS, timestep
1137000e7ae3SMatthew Knepley @*/
1138000e7ae3SMatthew Knepley int TSDefaultPreStep(TS ts)
1139000e7ae3SMatthew Knepley {
1140000e7ae3SMatthew Knepley   PetscFunctionBegin;
1141000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1142000e7ae3SMatthew Knepley }
1143000e7ae3SMatthew Knepley 
1144*e74ef692SMatthew Knepley #undef __FUNCT__
1145*e74ef692SMatthew Knepley #define __FUNCT__ "TSSetUpdate"
1146000e7ae3SMatthew Knepley /*@
1147000e7ae3SMatthew Knepley   TSSetUpdate - Sets the general-purpose update function called
1148000e7ae3SMatthew Knepley   at the beginning of every time step. This function can change
1149000e7ae3SMatthew Knepley   the time step.
1150000e7ae3SMatthew Knepley 
1151000e7ae3SMatthew Knepley   Collective on TS
1152000e7ae3SMatthew Knepley 
1153000e7ae3SMatthew Knepley   Input Parameters:
1154000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1155000e7ae3SMatthew Knepley - func - The function
1156000e7ae3SMatthew Knepley 
1157000e7ae3SMatthew Knepley   Calling sequence of func:
1158000e7ae3SMatthew Knepley . func (TS ts, double t, double *dt);
1159000e7ae3SMatthew Knepley 
1160000e7ae3SMatthew Knepley + t   - The current time
1161000e7ae3SMatthew Knepley - dt  - The current time step
1162000e7ae3SMatthew Knepley 
1163000e7ae3SMatthew Knepley   Level: intermediate
1164000e7ae3SMatthew Knepley 
1165000e7ae3SMatthew Knepley .keywords: TS, update, timestep
1166000e7ae3SMatthew Knepley @*/
1167000e7ae3SMatthew Knepley int TSSetUpdate(TS ts, int (*func)(TS, double, double *))
1168000e7ae3SMatthew Knepley {
1169000e7ae3SMatthew Knepley   PetscFunctionBegin;
1170000e7ae3SMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
1171000e7ae3SMatthew Knepley   ts->ops->update = func;
1172000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1173000e7ae3SMatthew Knepley }
1174000e7ae3SMatthew Knepley 
1175*e74ef692SMatthew Knepley #undef __FUNCT__
1176*e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultUpdate"
1177000e7ae3SMatthew Knepley /*@
1178000e7ae3SMatthew Knepley   TSDefaultUpdate - The default update function which does nothing.
1179000e7ae3SMatthew Knepley 
1180000e7ae3SMatthew Knepley   Collective on TS
1181000e7ae3SMatthew Knepley 
1182000e7ae3SMatthew Knepley   Input Parameters:
1183000e7ae3SMatthew Knepley + ts  - The TS context obtained from TSCreate()
1184000e7ae3SMatthew Knepley - t   - The current time
1185000e7ae3SMatthew Knepley 
1186000e7ae3SMatthew Knepley   Output Parameters:
1187000e7ae3SMatthew Knepley . dt  - The current time step
1188000e7ae3SMatthew Knepley 
1189000e7ae3SMatthew Knepley   Level: developer
1190000e7ae3SMatthew Knepley 
1191000e7ae3SMatthew Knepley .keywords: TS, update, timestep
1192000e7ae3SMatthew Knepley @*/
1193000e7ae3SMatthew Knepley int TSDefaultUpdate(TS ts, double t, double *dt)
1194000e7ae3SMatthew Knepley {
1195000e7ae3SMatthew Knepley   PetscFunctionBegin;
1196000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1197000e7ae3SMatthew Knepley }
1198000e7ae3SMatthew Knepley 
1199*e74ef692SMatthew Knepley #undef __FUNCT__
1200*e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1201000e7ae3SMatthew Knepley /*@
1202000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
1203000e7ae3SMatthew Knepley   called once at the end of time stepping.
1204000e7ae3SMatthew Knepley 
1205000e7ae3SMatthew Knepley   Collective on TS
1206000e7ae3SMatthew Knepley 
1207000e7ae3SMatthew Knepley   Input Parameters:
1208000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1209000e7ae3SMatthew Knepley - func - The function
1210000e7ae3SMatthew Knepley 
1211000e7ae3SMatthew Knepley   Calling sequence of func:
1212000e7ae3SMatthew Knepley . func (TS ts);
1213000e7ae3SMatthew Knepley 
1214000e7ae3SMatthew Knepley   Level: intermediate
1215000e7ae3SMatthew Knepley 
1216000e7ae3SMatthew Knepley .keywords: TS, timestep
1217000e7ae3SMatthew Knepley @*/
1218000e7ae3SMatthew Knepley int TSSetPostStep(TS ts, int (*func)(TS))
1219000e7ae3SMatthew Knepley {
1220000e7ae3SMatthew Knepley   PetscFunctionBegin;
1221000e7ae3SMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
1222000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1223000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1224000e7ae3SMatthew Knepley }
1225000e7ae3SMatthew Knepley 
1226*e74ef692SMatthew Knepley #undef __FUNCT__
1227*e74ef692SMatthew Knepley #define __FUNCT__ "TSDefaultPostStep"
1228000e7ae3SMatthew Knepley /*@
1229000e7ae3SMatthew Knepley   TSDefaultPostStep - The default post-stepping function which does nothing.
1230000e7ae3SMatthew Knepley 
1231000e7ae3SMatthew Knepley   Collective on TS
1232000e7ae3SMatthew Knepley 
1233000e7ae3SMatthew Knepley   Input Parameters:
1234000e7ae3SMatthew Knepley . ts  - The TS context obtained from TSCreate()
1235000e7ae3SMatthew Knepley 
1236000e7ae3SMatthew Knepley   Level: developer
1237000e7ae3SMatthew Knepley 
1238000e7ae3SMatthew Knepley .keywords: TS, timestep
1239000e7ae3SMatthew Knepley @*/
1240000e7ae3SMatthew Knepley int TSDefaultPostStep(TS ts)
1241000e7ae3SMatthew Knepley {
1242000e7ae3SMatthew Knepley   PetscFunctionBegin;
1243000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1244000e7ae3SMatthew Knepley }
1245000e7ae3SMatthew Knepley 
1246d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1247d763cef2SBarry Smith 
12484a2ae208SSatish Balay #undef __FUNCT__
12494a2ae208SSatish Balay #define __FUNCT__ "TSSetMonitor"
1250d763cef2SBarry Smith /*@C
1251d763cef2SBarry Smith    TSSetMonitor - Sets an ADDITIONAL function that is to be used at every
1252d763cef2SBarry Smith    timestep to display the iteration's  progress.
1253d763cef2SBarry Smith 
1254d763cef2SBarry Smith    Collective on TS
1255d763cef2SBarry Smith 
1256d763cef2SBarry Smith    Input Parameters:
1257d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1258d763cef2SBarry Smith .  func - monitoring routine
1259329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1260b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1261b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1262b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1263d763cef2SBarry Smith 
1264d763cef2SBarry Smith    Calling sequence of func:
126587828ca2SBarry Smith $    int func(TS ts,int steps,PetscReal time,Vec x,void *mctx)
1266d763cef2SBarry Smith 
1267d763cef2SBarry Smith +    ts - the TS context
1268d763cef2SBarry Smith .    steps - iteration number
12691f06c33eSBarry Smith .    time - current time
1270d763cef2SBarry Smith .    x - current iterate
1271d763cef2SBarry Smith -    mctx - [optional] monitoring context
1272d763cef2SBarry Smith 
1273d763cef2SBarry Smith    Notes:
1274d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1275d763cef2SBarry Smith    already has been loaded.
1276d763cef2SBarry Smith 
1277d763cef2SBarry Smith    Level: intermediate
1278d763cef2SBarry Smith 
1279d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1280d763cef2SBarry Smith 
1281d763cef2SBarry Smith .seealso: TSDefaultMonitor(), TSClearMonitor()
1282d763cef2SBarry Smith @*/
128387828ca2SBarry Smith int TSSetMonitor(TS ts,int (*monitor)(TS,int,PetscReal,Vec,void*),void *mctx,int (*mdestroy)(void*))
1284d763cef2SBarry Smith {
1285d763cef2SBarry Smith   PetscFunctionBegin;
1286d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1287d763cef2SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) {
128829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1289d763cef2SBarry Smith   }
1290d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1291329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1292d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1293d763cef2SBarry Smith   PetscFunctionReturn(0);
1294d763cef2SBarry Smith }
1295d763cef2SBarry Smith 
12964a2ae208SSatish Balay #undef __FUNCT__
12974a2ae208SSatish Balay #define __FUNCT__ "TSClearMonitor"
1298d763cef2SBarry Smith /*@C
1299d763cef2SBarry Smith    TSClearMonitor - Clears all the monitors that have been set on a time-step object.
1300d763cef2SBarry Smith 
1301d763cef2SBarry Smith    Collective on TS
1302d763cef2SBarry Smith 
1303d763cef2SBarry Smith    Input Parameters:
1304d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1305d763cef2SBarry Smith 
1306d763cef2SBarry Smith    Notes:
1307d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1308d763cef2SBarry Smith 
1309d763cef2SBarry Smith    Level: intermediate
1310d763cef2SBarry Smith 
1311d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1312d763cef2SBarry Smith 
1313d763cef2SBarry Smith .seealso: TSDefaultMonitor(), TSSetMonitor()
1314d763cef2SBarry Smith @*/
1315d763cef2SBarry Smith int TSClearMonitor(TS ts)
1316d763cef2SBarry Smith {
1317d763cef2SBarry Smith   PetscFunctionBegin;
1318d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1319d763cef2SBarry Smith   ts->numbermonitors = 0;
1320d763cef2SBarry Smith   PetscFunctionReturn(0);
1321d763cef2SBarry Smith }
1322d763cef2SBarry Smith 
13234a2ae208SSatish Balay #undef __FUNCT__
13244a2ae208SSatish Balay #define __FUNCT__ "TSDefaultMonitor"
132587828ca2SBarry Smith int TSDefaultMonitor(TS ts,int step,PetscReal ptime,Vec v,void *ctx)
1326d763cef2SBarry Smith {
1327d132466eSBarry Smith   int ierr;
1328d132466eSBarry Smith 
1329d763cef2SBarry Smith   PetscFunctionBegin;
1330142b95e3SSatish Balay   ierr = PetscPrintf(ts->comm,"timestep %d dt %g time %g\n",step,ts->time_step,ptime);CHKERRQ(ierr);
1331d763cef2SBarry Smith   PetscFunctionReturn(0);
1332d763cef2SBarry Smith }
1333d763cef2SBarry Smith 
13344a2ae208SSatish Balay #undef __FUNCT__
13354a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1336d763cef2SBarry Smith /*@
1337d763cef2SBarry Smith    TSStep - Steps the requested number of timesteps.
1338d763cef2SBarry Smith 
1339d763cef2SBarry Smith    Collective on TS
1340d763cef2SBarry Smith 
1341d763cef2SBarry Smith    Input Parameter:
1342d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1343d763cef2SBarry Smith 
1344d763cef2SBarry Smith    Output Parameters:
1345d763cef2SBarry Smith +  steps - number of iterations until termination
1346142b95e3SSatish Balay -  ptime - time until termination
1347d763cef2SBarry Smith 
1348d763cef2SBarry Smith    Level: beginner
1349d763cef2SBarry Smith 
1350d763cef2SBarry Smith .keywords: TS, timestep, solve
1351d763cef2SBarry Smith 
1352d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy()
1353d763cef2SBarry Smith @*/
135487828ca2SBarry Smith int TSStep(TS ts,int *steps,PetscReal *ptime)
1355d763cef2SBarry Smith {
1356f1af5d2fSBarry Smith   int        ierr;
1357f1af5d2fSBarry Smith   PetscTruth flg;
1358d763cef2SBarry Smith 
1359d763cef2SBarry Smith   PetscFunctionBegin;
1360d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1361d763cef2SBarry Smith   if (!ts->setupcalled) {ierr = TSSetUp(ts);CHKERRQ(ierr);}
1362b0a32e0cSBarry Smith   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
1363000e7ae3SMatthew Knepley   ierr = (*ts->ops->step)(ts,steps,ptime);CHKERRQ(ierr);
1364b0a32e0cSBarry Smith   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
1365b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(ts->prefix,"-ts_view",&flg);CHKERRQ(ierr);
1366b5758dffSBarry Smith   if (flg  && !PetscPreLoadingOn) {ierr = TSView(ts,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);}
1367d763cef2SBarry Smith   PetscFunctionReturn(0);
1368d763cef2SBarry Smith }
1369d763cef2SBarry Smith 
13704a2ae208SSatish Balay #undef __FUNCT__
13714a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
1372d763cef2SBarry Smith /*
1373d763cef2SBarry Smith      Runs the user provided monitor routines, if they exists.
1374d763cef2SBarry Smith */
137587828ca2SBarry Smith int TSMonitor(TS ts,int step,PetscReal ptime,Vec x)
1376d763cef2SBarry Smith {
1377d763cef2SBarry Smith   int i,ierr,n = ts->numbermonitors;
1378d763cef2SBarry Smith 
1379d763cef2SBarry Smith   PetscFunctionBegin;
1380d763cef2SBarry Smith   for (i=0; i<n; i++) {
1381142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
1382d763cef2SBarry Smith   }
1383d763cef2SBarry Smith   PetscFunctionReturn(0);
1384d763cef2SBarry Smith }
1385d763cef2SBarry Smith 
1386d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
1387d763cef2SBarry Smith 
13884a2ae208SSatish Balay #undef __FUNCT__
13894a2ae208SSatish Balay #define __FUNCT__ "TSLGMonitorCreate"
1390d763cef2SBarry Smith /*@C
1391d763cef2SBarry Smith    TSLGMonitorCreate - Creates a line graph context for use with
1392d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
1393d763cef2SBarry Smith 
1394d763cef2SBarry Smith    Collective on TS
1395d763cef2SBarry Smith 
1396d763cef2SBarry Smith    Input Parameters:
1397d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
1398d763cef2SBarry Smith .  label - the title to put in the title bar
13997c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
1400d763cef2SBarry Smith -  m, n - the screen width and height in pixels
1401d763cef2SBarry Smith 
1402d763cef2SBarry Smith    Output Parameter:
1403d763cef2SBarry Smith .  draw - the drawing context
1404d763cef2SBarry Smith 
1405d763cef2SBarry Smith    Options Database Key:
1406d763cef2SBarry Smith .  -ts_xmonitor - automatically sets line graph monitor
1407d763cef2SBarry Smith 
1408d763cef2SBarry Smith    Notes:
1409b0a32e0cSBarry Smith    Use TSLGMonitorDestroy() to destroy this line graph, not PetscDrawLGDestroy().
1410d763cef2SBarry Smith 
1411d763cef2SBarry Smith    Level: intermediate
1412d763cef2SBarry Smith 
14137c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
1414d763cef2SBarry Smith 
1415d763cef2SBarry Smith .seealso: TSLGMonitorDestroy(), TSSetMonitor()
14167c922b88SBarry Smith 
1417d763cef2SBarry Smith @*/
1418b0a32e0cSBarry Smith int TSLGMonitorCreate(char *host,char *label,int x,int y,int m,int n,PetscDrawLG *draw)
1419d763cef2SBarry Smith {
1420b0a32e0cSBarry Smith   PetscDraw win;
1421d763cef2SBarry Smith   int       ierr;
1422d763cef2SBarry Smith 
1423d763cef2SBarry Smith   PetscFunctionBegin;
1424b0a32e0cSBarry Smith   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
1425b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
1426b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
1427b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
1428d763cef2SBarry Smith 
1429b0a32e0cSBarry Smith   PetscLogObjectParent(*draw,win);
1430d763cef2SBarry Smith   PetscFunctionReturn(0);
1431d763cef2SBarry Smith }
1432d763cef2SBarry Smith 
14334a2ae208SSatish Balay #undef __FUNCT__
14344a2ae208SSatish Balay #define __FUNCT__ "TSLGMonitor"
143587828ca2SBarry Smith int TSLGMonitor(TS ts,int n,PetscReal ptime,Vec v,void *monctx)
1436d763cef2SBarry Smith {
1437b0a32e0cSBarry Smith   PetscDrawLG lg = (PetscDrawLG) monctx;
143887828ca2SBarry Smith   PetscReal      x,y = ptime;
1439d763cef2SBarry Smith   int         ierr;
1440d763cef2SBarry Smith 
1441d763cef2SBarry Smith   PetscFunctionBegin;
14427c922b88SBarry Smith   if (!monctx) {
14437c922b88SBarry Smith     MPI_Comm    comm;
1444b0a32e0cSBarry Smith     PetscViewer viewer;
14457c922b88SBarry Smith 
14467c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
1447b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
1448b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
14497c922b88SBarry Smith   }
14507c922b88SBarry Smith 
1451b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
145287828ca2SBarry Smith   x = (PetscReal)n;
1453b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1454d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
1455b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1456d763cef2SBarry Smith   }
1457d763cef2SBarry Smith   PetscFunctionReturn(0);
1458d763cef2SBarry Smith }
1459d763cef2SBarry Smith 
14604a2ae208SSatish Balay #undef __FUNCT__
14614a2ae208SSatish Balay #define __FUNCT__ "TSLGMonitorDestroy"
1462d763cef2SBarry Smith /*@C
1463d763cef2SBarry Smith    TSLGMonitorDestroy - Destroys a line graph context that was created
1464d763cef2SBarry Smith    with TSLGMonitorCreate().
1465d763cef2SBarry Smith 
1466b0a32e0cSBarry Smith    Collective on PetscDrawLG
1467d763cef2SBarry Smith 
1468d763cef2SBarry Smith    Input Parameter:
1469d763cef2SBarry Smith .  draw - the drawing context
1470d763cef2SBarry Smith 
1471d763cef2SBarry Smith    Level: intermediate
1472d763cef2SBarry Smith 
1473d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
1474d763cef2SBarry Smith 
1475d763cef2SBarry Smith .seealso: TSLGMonitorCreate(),  TSSetMonitor(), TSLGMonitor();
1476d763cef2SBarry Smith @*/
1477b0a32e0cSBarry Smith int TSLGMonitorDestroy(PetscDrawLG drawlg)
1478d763cef2SBarry Smith {
1479b0a32e0cSBarry Smith   PetscDraw draw;
1480d763cef2SBarry Smith   int       ierr;
1481d763cef2SBarry Smith 
1482d763cef2SBarry Smith   PetscFunctionBegin;
1483b0a32e0cSBarry Smith   ierr = PetscDrawLGGetDraw(drawlg,&draw);CHKERRQ(ierr);
1484b0a32e0cSBarry Smith   ierr = PetscDrawDestroy(draw);CHKERRQ(ierr);
1485b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
1486d763cef2SBarry Smith   PetscFunctionReturn(0);
1487d763cef2SBarry Smith }
1488d763cef2SBarry Smith 
14894a2ae208SSatish Balay #undef __FUNCT__
14904a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
1491d763cef2SBarry Smith /*@
1492d763cef2SBarry Smith    TSGetTime - Gets the current time.
1493d763cef2SBarry Smith 
1494d763cef2SBarry Smith    Not Collective
1495d763cef2SBarry Smith 
1496d763cef2SBarry Smith    Input Parameter:
1497d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1498d763cef2SBarry Smith 
1499d763cef2SBarry Smith    Output Parameter:
1500d763cef2SBarry Smith .  t  - the current time
1501d763cef2SBarry Smith 
1502d763cef2SBarry Smith    Contributed by: Matthew Knepley
1503d763cef2SBarry Smith 
1504d763cef2SBarry Smith    Level: beginner
1505d763cef2SBarry Smith 
1506d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1507d763cef2SBarry Smith 
1508d763cef2SBarry Smith .keywords: TS, get, time
1509d763cef2SBarry Smith @*/
151087828ca2SBarry Smith int TSGetTime(TS ts,PetscReal* t)
1511d763cef2SBarry Smith {
1512d763cef2SBarry Smith   PetscFunctionBegin;
1513d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1514d763cef2SBarry Smith   *t = ts->ptime;
1515d763cef2SBarry Smith   PetscFunctionReturn(0);
1516d763cef2SBarry Smith }
1517d763cef2SBarry Smith 
15184a2ae208SSatish Balay #undef __FUNCT__
15194a2ae208SSatish Balay #define __FUNCT__ "TSGetProblemType"
1520d763cef2SBarry Smith /*@C
1521d763cef2SBarry Smith    TSGetProblemType - Returns the problem type of a TS (timestepper) context.
1522d763cef2SBarry Smith 
1523d763cef2SBarry Smith    Not Collective
1524d763cef2SBarry Smith 
1525d763cef2SBarry Smith    Input Parameter:
1526d763cef2SBarry Smith .  ts   - The TS context obtained from TSCreate()
1527d763cef2SBarry Smith 
1528d763cef2SBarry Smith    Output Parameter:
1529d763cef2SBarry Smith .  type - The problem type, TS_LINEAR or TS_NONLINEAR
1530d763cef2SBarry Smith 
1531d763cef2SBarry Smith    Level: intermediate
1532d763cef2SBarry Smith 
1533d763cef2SBarry Smith    Contributed by: Matthew Knepley
1534d763cef2SBarry Smith 
1535d763cef2SBarry Smith .keywords: ts, get, type
1536d763cef2SBarry Smith 
1537d763cef2SBarry Smith @*/
1538d763cef2SBarry Smith int TSGetProblemType(TS ts,TSProblemType *type)
1539d763cef2SBarry Smith {
1540d763cef2SBarry Smith   PetscFunctionBegin;
1541d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1542d763cef2SBarry Smith   *type = ts->problem_type;
1543d763cef2SBarry Smith   PetscFunctionReturn(0);
1544d763cef2SBarry Smith }
1545d763cef2SBarry Smith 
15464a2ae208SSatish Balay #undef __FUNCT__
15474a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
1548d763cef2SBarry Smith /*@C
1549d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
1550d763cef2SBarry Smith    TS options in the database.
1551d763cef2SBarry Smith 
1552d763cef2SBarry Smith    Collective on TS
1553d763cef2SBarry Smith 
1554d763cef2SBarry Smith    Input Parameter:
1555d763cef2SBarry Smith +  ts     - The TS context
1556d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
1557d763cef2SBarry Smith 
1558d763cef2SBarry Smith    Notes:
1559d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1560d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
1561d763cef2SBarry Smith    hyphen.
1562d763cef2SBarry Smith 
1563d763cef2SBarry Smith    Contributed by: Matthew Knepley
1564d763cef2SBarry Smith 
1565d763cef2SBarry Smith    Level: advanced
1566d763cef2SBarry Smith 
1567d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
1568d763cef2SBarry Smith 
1569d763cef2SBarry Smith .seealso: TSSetFromOptions()
1570d763cef2SBarry Smith 
1571d763cef2SBarry Smith @*/
1572d763cef2SBarry Smith int TSSetOptionsPrefix(TS ts,char *prefix)
1573d763cef2SBarry Smith {
1574d763cef2SBarry Smith   int ierr;
1575d763cef2SBarry Smith 
1576d763cef2SBarry Smith   PetscFunctionBegin;
1577d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1578d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
1579d763cef2SBarry Smith   switch(ts->problem_type) {
1580d763cef2SBarry Smith     case TS_NONLINEAR:
1581d763cef2SBarry Smith       ierr = SNESSetOptionsPrefix(ts->snes,prefix);CHKERRQ(ierr);
1582d763cef2SBarry Smith       break;
1583d763cef2SBarry Smith     case TS_LINEAR:
1584d763cef2SBarry Smith       ierr = SLESSetOptionsPrefix(ts->sles,prefix);CHKERRQ(ierr);
1585d763cef2SBarry Smith       break;
1586d763cef2SBarry Smith   }
1587d763cef2SBarry Smith   PetscFunctionReturn(0);
1588d763cef2SBarry Smith }
1589d763cef2SBarry Smith 
1590d763cef2SBarry Smith 
15914a2ae208SSatish Balay #undef __FUNCT__
15924a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
1593d763cef2SBarry Smith /*@C
1594d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
1595d763cef2SBarry Smith    TS options in the database.
1596d763cef2SBarry Smith 
1597d763cef2SBarry Smith    Collective on TS
1598d763cef2SBarry Smith 
1599d763cef2SBarry Smith    Input Parameter:
1600d763cef2SBarry Smith +  ts     - The TS context
1601d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
1602d763cef2SBarry Smith 
1603d763cef2SBarry Smith    Notes:
1604d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1605d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
1606d763cef2SBarry Smith    hyphen.
1607d763cef2SBarry Smith 
1608d763cef2SBarry Smith    Contributed by: Matthew Knepley
1609d763cef2SBarry Smith 
1610d763cef2SBarry Smith    Level: advanced
1611d763cef2SBarry Smith 
1612d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
1613d763cef2SBarry Smith 
1614d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
1615d763cef2SBarry Smith 
1616d763cef2SBarry Smith @*/
1617d763cef2SBarry Smith int TSAppendOptionsPrefix(TS ts,char *prefix)
1618d763cef2SBarry Smith {
1619d763cef2SBarry Smith   int ierr;
1620d763cef2SBarry Smith 
1621d763cef2SBarry Smith   PetscFunctionBegin;
1622d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1623d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
1624d763cef2SBarry Smith   switch(ts->problem_type) {
1625d763cef2SBarry Smith     case TS_NONLINEAR:
1626d763cef2SBarry Smith       ierr = SNESAppendOptionsPrefix(ts->snes,prefix);CHKERRQ(ierr);
1627d763cef2SBarry Smith       break;
1628d763cef2SBarry Smith     case TS_LINEAR:
1629d763cef2SBarry Smith       ierr = SLESAppendOptionsPrefix(ts->sles,prefix);CHKERRQ(ierr);
1630d763cef2SBarry Smith       break;
1631d763cef2SBarry Smith   }
1632d763cef2SBarry Smith   PetscFunctionReturn(0);
1633d763cef2SBarry Smith }
1634d763cef2SBarry Smith 
16354a2ae208SSatish Balay #undef __FUNCT__
16364a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
1637d763cef2SBarry Smith /*@C
1638d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
1639d763cef2SBarry Smith    TS options in the database.
1640d763cef2SBarry Smith 
1641d763cef2SBarry Smith    Not Collective
1642d763cef2SBarry Smith 
1643d763cef2SBarry Smith    Input Parameter:
1644d763cef2SBarry Smith .  ts - The TS context
1645d763cef2SBarry Smith 
1646d763cef2SBarry Smith    Output Parameter:
1647d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
1648d763cef2SBarry Smith 
1649d763cef2SBarry Smith    Contributed by: Matthew Knepley
1650d763cef2SBarry Smith 
1651d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
1652d763cef2SBarry Smith    sufficient length to hold the prefix.
1653d763cef2SBarry Smith 
1654d763cef2SBarry Smith    Level: intermediate
1655d763cef2SBarry Smith 
1656d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
1657d763cef2SBarry Smith 
1658d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
1659d763cef2SBarry Smith @*/
1660d763cef2SBarry Smith int TSGetOptionsPrefix(TS ts,char **prefix)
1661d763cef2SBarry Smith {
1662d763cef2SBarry Smith   int ierr;
1663d763cef2SBarry Smith 
1664d763cef2SBarry Smith   PetscFunctionBegin;
1665d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1666d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
1667d763cef2SBarry Smith   PetscFunctionReturn(0);
1668d763cef2SBarry Smith }
1669d763cef2SBarry Smith 
16704a2ae208SSatish Balay #undef __FUNCT__
16714a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSMatrix"
1672d763cef2SBarry Smith /*@C
1673d763cef2SBarry Smith    TSGetRHSMatrix - Returns the matrix A at the present timestep.
1674d763cef2SBarry Smith 
1675d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
1676d763cef2SBarry Smith 
1677d763cef2SBarry Smith    Input Parameter:
1678d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
1679d763cef2SBarry Smith 
1680d763cef2SBarry Smith    Output Parameters:
1681d763cef2SBarry Smith +  A   - The matrix A, where U_t = A(t) U
1682d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as A
1683d763cef2SBarry Smith -  ctx - User-defined context for matrix evaluation routine
1684d763cef2SBarry Smith 
1685d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
1686d763cef2SBarry Smith 
1687d763cef2SBarry Smith    Contributed by: Matthew Knepley
1688d763cef2SBarry Smith 
1689d763cef2SBarry Smith    Level: intermediate
1690d763cef2SBarry Smith 
1691d763cef2SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetTimeStepNumber(), TSGetRHSJacobian()
1692d763cef2SBarry Smith 
1693d763cef2SBarry Smith .keywords: TS, timestep, get, matrix
1694d763cef2SBarry Smith 
1695d763cef2SBarry Smith @*/
1696d763cef2SBarry Smith int TSGetRHSMatrix(TS ts,Mat *A,Mat *M,void **ctx)
1697d763cef2SBarry Smith {
1698d763cef2SBarry Smith   PetscFunctionBegin;
1699d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
1700d763cef2SBarry Smith   if (A)   *A = ts->A;
1701d763cef2SBarry Smith   if (M)   *M = ts->B;
1702d763cef2SBarry Smith   if (ctx) *ctx = ts->jacP;
1703d763cef2SBarry Smith   PetscFunctionReturn(0);
1704d763cef2SBarry Smith }
1705d763cef2SBarry Smith 
17064a2ae208SSatish Balay #undef __FUNCT__
17074a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
1708d763cef2SBarry Smith /*@C
1709d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
1710d763cef2SBarry Smith 
1711d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
1712d763cef2SBarry Smith 
1713d763cef2SBarry Smith    Input Parameter:
1714d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
1715d763cef2SBarry Smith 
1716d763cef2SBarry Smith    Output Parameters:
1717d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
1718d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
1719d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine
1720d763cef2SBarry Smith 
1721d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
1722d763cef2SBarry Smith 
1723d763cef2SBarry Smith    Contributed by: Matthew Knepley
1724d763cef2SBarry Smith 
1725d763cef2SBarry Smith    Level: intermediate
1726d763cef2SBarry Smith 
1727d763cef2SBarry Smith .seealso: TSGetTimeStep(), TSGetRHSMatrix(), TSGetTime(), TSGetTimeStepNumber()
1728d763cef2SBarry Smith 
1729d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
1730d763cef2SBarry Smith @*/
1731d763cef2SBarry Smith int TSGetRHSJacobian(TS ts,Mat *J,Mat *M,void **ctx)
1732d763cef2SBarry Smith {
1733d763cef2SBarry Smith   int ierr;
1734d763cef2SBarry Smith 
1735d763cef2SBarry Smith   PetscFunctionBegin;
1736d763cef2SBarry Smith   ierr = TSGetRHSMatrix(ts,J,M,ctx);CHKERRQ(ierr);
1737d763cef2SBarry Smith   PetscFunctionReturn(0);
1738d763cef2SBarry Smith }
1739d763cef2SBarry Smith 
1740d763cef2SBarry Smith /*MC
1741f1af5d2fSBarry Smith    TSRegisterDynamic - Adds a method to the timestepping solver package.
1742d763cef2SBarry Smith 
1743d763cef2SBarry Smith    Synopsis:
17443a7fca6bSBarry Smith    int TSRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(TS))
1745d763cef2SBarry Smith 
1746d763cef2SBarry Smith    Not collective
1747d763cef2SBarry Smith 
1748d763cef2SBarry Smith    Input Parameters:
1749d763cef2SBarry Smith +  name_solver - name of a new user-defined solver
1750d763cef2SBarry Smith .  path - path (either absolute or relative) the library containing this solver
1751d763cef2SBarry Smith .  name_create - name of routine to create method context
1752d763cef2SBarry Smith -  routine_create - routine to create method context
1753d763cef2SBarry Smith 
1754d763cef2SBarry Smith    Notes:
1755f1af5d2fSBarry Smith    TSRegisterDynamic() may be called multiple times to add several user-defined solvers.
1756d763cef2SBarry Smith 
1757d763cef2SBarry Smith    If dynamic libraries are used, then the fourth input argument (routine_create)
1758d763cef2SBarry Smith    is ignored.
1759d763cef2SBarry Smith 
1760d763cef2SBarry Smith    Sample usage:
1761d763cef2SBarry Smith .vb
1762f1af5d2fSBarry Smith    TSRegisterDynamic("my_solver",/home/username/my_lib/lib/libO/solaris/mylib.a,
1763d763cef2SBarry Smith               "MySolverCreate",MySolverCreate);
1764d763cef2SBarry Smith .ve
1765d763cef2SBarry Smith 
1766d763cef2SBarry Smith    Then, your solver can be chosen with the procedural interface via
1767d763cef2SBarry Smith $     TSSetType(ts,"my_solver")
1768d763cef2SBarry Smith    or at runtime via the option
1769d763cef2SBarry Smith $     -ts_type my_solver
1770d763cef2SBarry Smith 
1771d763cef2SBarry Smith    Level: advanced
1772d763cef2SBarry Smith 
17732aeb12f1SSatish Balay    Environmental variables such as ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, ${BOPT},
17745ba88a07SLois Curfman McInnes    and others of the form ${any_environmental_variable} occuring in pathname will be
17755ba88a07SLois Curfman McInnes    replaced with appropriate values.
1776d763cef2SBarry Smith 
1777d763cef2SBarry Smith .keywords: TS, register
1778d763cef2SBarry Smith 
1779d763cef2SBarry Smith .seealso: TSRegisterAll(), TSRegisterDestroy()
1780d763cef2SBarry Smith M*/
1781d763cef2SBarry Smith 
17824a2ae208SSatish Balay #undef __FUNCT__
17834a2ae208SSatish Balay #define __FUNCT__ "TSRegister"
1784000e7ae3SMatthew Knepley int TSRegister(const char sname[], const char path[], const char name[], int (*function)(TS))
1785d763cef2SBarry Smith {
1786d763cef2SBarry Smith   char fullname[256];
1787549d3d68SSatish Balay   int  ierr;
1788d763cef2SBarry Smith 
1789d763cef2SBarry Smith   PetscFunctionBegin;
1790b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
17911713a123SBarry Smith   ierr = PetscFListAdd(&TSList,sname,fullname,(void (*)())function);CHKERRQ(ierr);
1792d763cef2SBarry Smith   PetscFunctionReturn(0);
1793d763cef2SBarry Smith }
17941713a123SBarry Smith 
17951713a123SBarry Smith #undef __FUNCT__
17961713a123SBarry Smith #define __FUNCT__ "TSVecViewMonitor"
17971713a123SBarry Smith /*@C
17981713a123SBarry Smith    TSVecViewMonitor - Monitors progress of the TS solvers by calling
17991713a123SBarry Smith    VecView() for the solution at each timestep
18001713a123SBarry Smith 
18011713a123SBarry Smith    Collective on TS
18021713a123SBarry Smith 
18031713a123SBarry Smith    Input Parameters:
18041713a123SBarry Smith +  ts - the TS context
18051713a123SBarry Smith .  step - current time-step
1806142b95e3SSatish Balay .  ptime - current time
18071713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
18081713a123SBarry Smith 
18091713a123SBarry Smith    Level: intermediate
18101713a123SBarry Smith 
18111713a123SBarry Smith .keywords: TS,  vector, monitor, view
18121713a123SBarry Smith 
18131713a123SBarry Smith .seealso: TSSetMonitor(), TSDefaultMonitor(), VecView()
18141713a123SBarry Smith @*/
1815142b95e3SSatish Balay int TSVecViewMonitor(TS ts,int step,PetscReal ptime,Vec x,void *dummy)
18161713a123SBarry Smith {
18171713a123SBarry Smith   int         ierr;
18181713a123SBarry Smith   PetscViewer viewer = (PetscViewer) dummy;
18191713a123SBarry Smith 
18201713a123SBarry Smith   PetscFunctionBegin;
18211713a123SBarry Smith   if (!viewer) {
18221713a123SBarry Smith     MPI_Comm comm;
18231713a123SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
18241713a123SBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
18251713a123SBarry Smith   }
18261713a123SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
18271713a123SBarry Smith   PetscFunctionReturn(0);
18281713a123SBarry Smith }
18291713a123SBarry Smith 
18301713a123SBarry Smith 
18311713a123SBarry Smith 
1832