17f296bb3SBarry Smith(ch_ts)= 27f296bb3SBarry Smith 37f296bb3SBarry Smith# TS: Scalable ODE and DAE Solvers 47f296bb3SBarry Smith 57f296bb3SBarry SmithThe `TS` library provides a framework for the scalable solution of 67f296bb3SBarry SmithODEs and DAEs arising from the discretization of time-dependent PDEs. 77f296bb3SBarry Smith 87f296bb3SBarry Smith**Simple Example:** Consider the PDE 97f296bb3SBarry Smith 107f296bb3SBarry Smith$$ 117f296bb3SBarry Smithu_t = u_{xx} 127f296bb3SBarry Smith$$ 137f296bb3SBarry Smith 147f296bb3SBarry Smithdiscretized with centered finite differences in space yielding the 157f296bb3SBarry Smithsemi-discrete equation 167f296bb3SBarry Smith 177f296bb3SBarry Smith$$ 187f296bb3SBarry Smith\begin{aligned} 197f296bb3SBarry Smith (u_i)_t & = & \frac{u_{i+1} - 2 u_{i} + u_{i-1}}{h^2}, \\ 207f296bb3SBarry Smith u_t & = & \tilde{A} u;\end{aligned} 217f296bb3SBarry Smith$$ 227f296bb3SBarry Smith 237f296bb3SBarry Smithor with piecewise linear finite elements approximation in space 247f296bb3SBarry Smith$u(x,t) \doteq \sum_i \xi_i(t) \phi_i(x)$ yielding the 257f296bb3SBarry Smithsemi-discrete equation 267f296bb3SBarry Smith 277f296bb3SBarry Smith$$ 287f296bb3SBarry SmithB {\xi}'(t) = A \xi(t) 297f296bb3SBarry Smith$$ 307f296bb3SBarry Smith 317f296bb3SBarry SmithNow applying the backward Euler method results in 327f296bb3SBarry Smith 337f296bb3SBarry Smith$$ 347f296bb3SBarry Smith( B - dt^n A ) u^{n+1} = B u^n, 357f296bb3SBarry Smith$$ 367f296bb3SBarry Smith 377f296bb3SBarry Smithin which 387f296bb3SBarry Smith 397f296bb3SBarry Smith$$ 407f296bb3SBarry Smith{u^n}_i = \xi_i(t_n) \doteq u(x_i,t_n), 417f296bb3SBarry Smith$$ 427f296bb3SBarry Smith 437f296bb3SBarry Smith$$ 447f296bb3SBarry Smith{\xi}'(t_{n+1}) \doteq \frac{{u^{n+1}}_i - {u^{n}}_i }{dt^{n}}, 457f296bb3SBarry Smith$$ 467f296bb3SBarry Smith 477f296bb3SBarry Smith$A$ is the stiffness matrix, and $B$ is the identity for 487f296bb3SBarry Smithfinite differences or the mass matrix for the finite element method. 497f296bb3SBarry Smith 507f296bb3SBarry SmithThe PETSc interface for solving time dependent problems assumes the 517f296bb3SBarry Smithproblem is written in the form 527f296bb3SBarry Smith 537f296bb3SBarry Smith$$ 547f296bb3SBarry SmithF(t,u,\dot{u}) = G(t,u), \quad u(t_0) = u_0. 557f296bb3SBarry Smith$$ 567f296bb3SBarry Smith 577f296bb3SBarry SmithIn general, this is a differential algebraic equation (DAE) [^id5]. For 587f296bb3SBarry SmithODE with nontrivial mass matrices such as arise in FEM, the implicit/DAE 597f296bb3SBarry Smithinterface significantly reduces overhead to prepare the system for 607f296bb3SBarry Smithalgebraic solvers (`SNES`/`KSP`) by having the user assemble the 617f296bb3SBarry Smithcorrectly shifted matrix. Therefore this interface is also useful for 627f296bb3SBarry SmithODE systems. 637f296bb3SBarry Smith 647f296bb3SBarry SmithTo solve an ODE or DAE one uses: 657f296bb3SBarry Smith 667f296bb3SBarry Smith- Function $F(t,u,\dot{u})$ 677f296bb3SBarry Smith 687f296bb3SBarry Smith ``` 69*2a8381b2SBarry Smith TSSetIFunction(TS ts, Vec R, PetscErrorCode (*f)(TS, PetscReal, Vec, Vec, Vec, PetscCtx), PetscCtxfunP); 707f296bb3SBarry Smith ``` 717f296bb3SBarry Smith 727f296bb3SBarry Smith The vector `R` is an optional location to store the residual. The 737f296bb3SBarry Smith arguments to the function `f()` are the timestep context, current 747f296bb3SBarry Smith time, input state $u$, input time derivative $\dot{u}$, 757f296bb3SBarry Smith and the (optional) user-provided context `funP`. If 767f296bb3SBarry Smith $F(t,u,\dot{u}) = \dot{u}$ then one need not call this 777f296bb3SBarry Smith function. 787f296bb3SBarry Smith 797f296bb3SBarry Smith- Function $G(t,u)$, if it is nonzero, is provided with the 807f296bb3SBarry Smith function 817f296bb3SBarry Smith 827f296bb3SBarry Smith ``` 83*2a8381b2SBarry Smith TSSetRHSFunction(TS ts, Vec R, PetscErrorCode (*f)(TS, PetscReal, Vec, Vec, PetscCtx), PetscCtxfunP); 847f296bb3SBarry Smith ``` 857f296bb3SBarry Smith 867f296bb3SBarry Smith- Jacobian 877f296bb3SBarry Smith 887f296bb3SBarry Smith 897f296bb3SBarry Smith $\sigma F_{\dot{u}}(t^n,u^n,\dot{u}^n) + F_u(t^n,u^n,\dot{u}^n)$ 907f296bb3SBarry Smith 917f296bb3SBarry Smith If using a fully implicit or semi-implicit (IMEX) method one also 927f296bb3SBarry Smith can provide an appropriate (approximate) Jacobian matrix of 937f296bb3SBarry Smith 947f296bb3SBarry Smith 957f296bb3SBarry Smith $F()$ 967f296bb3SBarry Smith 977f296bb3SBarry Smith . 987f296bb3SBarry Smith 997f296bb3SBarry Smith ``` 100*2a8381b2SBarry Smith TSSetIJacobian(TS ts, Mat A, Mat B, PetscErrorCode (*fjac)(TS, PetscReal, Vec, Vec, PetscReal, Mat, Mat, PetscCtx), PetscCtx jacP); 1017f296bb3SBarry Smith ``` 1027f296bb3SBarry Smith 1037f296bb3SBarry Smith The arguments for the function `fjac()` are the timestep context, 1047f296bb3SBarry Smith current time, input state $u$, input derivative 1057f296bb3SBarry Smith $\dot{u}$, input shift $\sigma$, matrix $A$, 1067addb90fSBarry Smith matrix used to construct the preconditioner $B$, and the (optional) user-provided 1077f296bb3SBarry Smith context `jacP`. 1087f296bb3SBarry Smith 1097f296bb3SBarry Smith The Jacobian needed for the nonlinear system is, by the chain rule, 1107f296bb3SBarry Smith 1117f296bb3SBarry Smith $$ 1127f296bb3SBarry Smith \begin{aligned} 1137f296bb3SBarry Smith \frac{d F}{d u^n} & = & \frac{\partial F}{\partial \dot{u}}|_{u^n} \frac{\partial \dot{u}}{\partial u}|_{u^n} + \frac{\partial F}{\partial u}|_{u^n}.\end{aligned} 1147f296bb3SBarry Smith $$ 1157f296bb3SBarry Smith 1167f296bb3SBarry Smith For any ODE integration method the approximation of $\dot{u}$ 1177f296bb3SBarry Smith is linear in $u^n$ hence 1187f296bb3SBarry Smith $\frac{\partial \dot{u}}{\partial u}|_{u^n} = \sigma$, where 1197f296bb3SBarry Smith the shift $\sigma$ depends on the ODE integrator and time step 1207f296bb3SBarry Smith but not on the function being integrated. Thus 1217f296bb3SBarry Smith 1227f296bb3SBarry Smith $$ 1237f296bb3SBarry Smith \begin{aligned} 1247f296bb3SBarry Smith \frac{d F}{d u^n} & = & \sigma F_{\dot{u}}(t^n,u^n,\dot{u}^n) + F_u(t^n,u^n,\dot{u}^n).\end{aligned} 1257f296bb3SBarry Smith $$ 1267f296bb3SBarry Smith 1277f296bb3SBarry Smith This explains why the user provide Jacobian is in the given form for 1287f296bb3SBarry Smith all integration methods. An equivalent way to derive the formula is 1297f296bb3SBarry Smith to note that 1307f296bb3SBarry Smith 1317f296bb3SBarry Smith $$ 1327f296bb3SBarry Smith F(t^n,u^n,\dot{u}^n) = F(t^n,u^n,w+\sigma*u^n) 1337f296bb3SBarry Smith $$ 1347f296bb3SBarry Smith 1357f296bb3SBarry Smith where $w$ is some linear combination of previous time solutions 1367f296bb3SBarry Smith of $u$ so that 1377f296bb3SBarry Smith 1387f296bb3SBarry Smith $$ 1397f296bb3SBarry Smith \frac{d F}{d u^n} = \sigma F_{\dot{u}}(t^n,u^n,\dot{u}^n) + F_u(t^n,u^n,\dot{u}^n) 1407f296bb3SBarry Smith $$ 1417f296bb3SBarry Smith 1427f296bb3SBarry Smith again by the chain rule. 1437f296bb3SBarry Smith 1447f296bb3SBarry Smith For example, consider backward Euler’s method applied to the ODE 1457f296bb3SBarry Smith $F(t, u, \dot{u}) = \dot{u} - f(t, u)$ with 1467f296bb3SBarry Smith $\dot{u} = (u^n - u^{n-1})/\delta t$ and 1477f296bb3SBarry Smith $\frac{\partial \dot{u}}{\partial u}|_{u^n} = 1/\delta t$ 1487f296bb3SBarry Smith resulting in 1497f296bb3SBarry Smith 1507f296bb3SBarry Smith $$ 1517f296bb3SBarry Smith \begin{aligned} 1527f296bb3SBarry Smith \frac{d F}{d u^n} & = & (1/\delta t)F_{\dot{u}} + F_u(t^n,u^n,\dot{u}^n).\end{aligned} 1537f296bb3SBarry Smith $$ 1547f296bb3SBarry Smith 1557f296bb3SBarry Smith But $F_{\dot{u}} = 1$, in this special case, resulting in the 1567f296bb3SBarry Smith expected Jacobian $I/\delta t - f_u(t,u^n)$. 1577f296bb3SBarry Smith 1587f296bb3SBarry Smith- Jacobian 1597f296bb3SBarry Smith 1607f296bb3SBarry Smith $G_u$ 1617f296bb3SBarry Smith 1627f296bb3SBarry Smith If using a fully implicit method and the function 1637f296bb3SBarry Smith 1647f296bb3SBarry Smith $G()$ 1657f296bb3SBarry Smith 1667f296bb3SBarry Smith is 1677f296bb3SBarry Smith provided, one also can provide an appropriate (approximate) 1687f296bb3SBarry Smith Jacobian matrix of 1697f296bb3SBarry Smith 170*2a8381b2SBarry Smith $G()$. 1717f296bb3SBarry Smith 1727f296bb3SBarry Smith 1737f296bb3SBarry Smith ``` 1747f296bb3SBarry Smith TSSetRHSJacobian(TS ts, Mat A, Mat B, 175*2a8381b2SBarry Smith PetscErrorCode (*fjac)(TS, PetscReal, Vec, Mat, Mat, PetscCtx), PetscCtx jacP); 1767f296bb3SBarry Smith ``` 1777f296bb3SBarry Smith 1787f296bb3SBarry Smith The arguments for the function `fjac()` are the timestep context, 1797f296bb3SBarry Smith current time, input state $u$, matrix $A$, 1807addb90fSBarry Smith matrix used to construct the preconditioner $B$, and the (optional) user-provided 1817f296bb3SBarry Smith context `jacP`. 1827f296bb3SBarry Smith 1837f296bb3SBarry SmithProviding appropriate $F()$ and $G()$ for your problem 1847f296bb3SBarry Smithallows for the easy runtime switching between explicit, semi-implicit 1857f296bb3SBarry Smith(IMEX), and fully implicit methods. 1867f296bb3SBarry Smith 1877f296bb3SBarry Smith(sec_ts_basic)= 1887f296bb3SBarry Smith 1897f296bb3SBarry Smith## Basic TS Options 1907f296bb3SBarry Smith 1917f296bb3SBarry SmithThe user first creates a `TS` object with the command 1927f296bb3SBarry Smith 1937f296bb3SBarry Smith``` 1947f296bb3SBarry Smithint TSCreate(MPI_Comm comm,TS *ts); 1957f296bb3SBarry Smith``` 1967f296bb3SBarry Smith 1977f296bb3SBarry Smith``` 1987f296bb3SBarry Smithint TSSetProblemType(TS ts,TSProblemType problemtype); 1997f296bb3SBarry Smith``` 2007f296bb3SBarry Smith 2017f296bb3SBarry SmithThe `TSProblemType` is one of `TS_LINEAR` or `TS_NONLINEAR`. 2027f296bb3SBarry Smith 2037f296bb3SBarry SmithTo set up `TS` for solving an ODE, one must set the “initial 2047f296bb3SBarry Smithconditions” for the ODE with 2057f296bb3SBarry Smith 2067f296bb3SBarry Smith``` 2077f296bb3SBarry SmithTSSetSolution(TS ts, Vec initialsolution); 2087f296bb3SBarry Smith``` 2097f296bb3SBarry Smith 2107f296bb3SBarry SmithOne can set the solution method with the routine 2117f296bb3SBarry Smith 2127f296bb3SBarry Smith``` 2137f296bb3SBarry SmithTSSetType(TS ts,TSType type); 2147f296bb3SBarry Smith``` 2157f296bb3SBarry Smith 2167f296bb3SBarry SmithSome of the currently supported types are `TSEULER`, `TSRK` (Runge-Kutta), `TSBEULER`, `TSCN` (Crank-Nicolson), `TSTHETA`, `TSGLLE` (generalized linear), and `TSPSEUDO`. 2177f296bb3SBarry SmithThey can also be set with the options database option `-ts_type euler, rk, beuler, cn, theta, gl, pseudo, sundials, eimex, arkimex, rosw`. 2187f296bb3SBarry SmithA list of available methods is given in {any}`integrator_table`. 2197f296bb3SBarry Smith 2207f296bb3SBarry SmithSet the initial time with the command 2217f296bb3SBarry Smith 2227f296bb3SBarry Smith``` 2237f296bb3SBarry SmithTSSetTime(TS ts,PetscReal time); 2247f296bb3SBarry Smith``` 2257f296bb3SBarry Smith 2267f296bb3SBarry SmithOne can change the timestep with the command 2277f296bb3SBarry Smith 2287f296bb3SBarry Smith``` 2297f296bb3SBarry SmithTSSetTimeStep(TS ts,PetscReal dt); 2307f296bb3SBarry Smith``` 2317f296bb3SBarry Smith 2327f296bb3SBarry Smithcan determine the current timestep with the routine 2337f296bb3SBarry Smith 2347f296bb3SBarry Smith``` 2357f296bb3SBarry SmithTSGetTimeStep(TS ts,PetscReal* dt); 2367f296bb3SBarry Smith``` 2377f296bb3SBarry Smith 2387f296bb3SBarry SmithHere, “current” refers to the timestep being used to attempt to promote 2397f296bb3SBarry Smiththe solution form $u^n$ to $u^{n+1}.$ 2407f296bb3SBarry Smith 2417f296bb3SBarry SmithOne sets the total number of timesteps to run or the total time to run 2427f296bb3SBarry Smith(whatever is first) with the commands 2437f296bb3SBarry Smith 2447f296bb3SBarry Smith``` 2457f296bb3SBarry SmithTSSetMaxSteps(TS ts,PetscInt maxsteps); 2467f296bb3SBarry SmithTSSetMaxTime(TS ts,PetscReal maxtime); 2477f296bb3SBarry Smith``` 2487f296bb3SBarry Smith 2497f296bb3SBarry Smithand determines the behavior near the final time with 2507f296bb3SBarry Smith 2517f296bb3SBarry Smith``` 2527f296bb3SBarry SmithTSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt); 2537f296bb3SBarry Smith``` 2547f296bb3SBarry Smith 2557f296bb3SBarry Smithwhere `eftopt` is one of 2567f296bb3SBarry Smith`TS_EXACTFINALTIME_STEPOVER`,`TS_EXACTFINALTIME_INTERPOLATE`, or 2577f296bb3SBarry Smith`TS_EXACTFINALTIME_MATCHSTEP`. One performs the requested number of 2587f296bb3SBarry Smithtime steps with 2597f296bb3SBarry Smith 2607f296bb3SBarry Smith``` 2617f296bb3SBarry SmithTSSolve(TS ts,Vec U); 2627f296bb3SBarry Smith``` 2637f296bb3SBarry Smith 2647f296bb3SBarry SmithThe solve call implicitly sets up the timestep context; this can be done 2657f296bb3SBarry Smithexplicitly with 2667f296bb3SBarry Smith 2677f296bb3SBarry Smith``` 2687f296bb3SBarry SmithTSSetUp(TS ts); 2697f296bb3SBarry Smith``` 2707f296bb3SBarry Smith 2717f296bb3SBarry SmithOne destroys the context with 2727f296bb3SBarry Smith 2737f296bb3SBarry Smith``` 2747f296bb3SBarry SmithTSDestroy(TS *ts); 2757f296bb3SBarry Smith``` 2767f296bb3SBarry Smith 2777f296bb3SBarry Smithand views it with 2787f296bb3SBarry Smith 2797f296bb3SBarry Smith``` 2807f296bb3SBarry SmithTSView(TS ts,PetscViewer viewer); 2817f296bb3SBarry Smith``` 2827f296bb3SBarry Smith 2837f296bb3SBarry SmithIn place of `TSSolve()`, a single step can be taken using 2847f296bb3SBarry Smith 2857f296bb3SBarry Smith``` 2867f296bb3SBarry SmithTSStep(TS ts); 2877f296bb3SBarry Smith``` 2887f296bb3SBarry Smith 2897f296bb3SBarry Smith(sec_imex)= 2907f296bb3SBarry Smith 2917f296bb3SBarry Smith## DAE Formulations 2927f296bb3SBarry Smith 2937f296bb3SBarry SmithYou can find a discussion of DAEs in {cite}`ascherpetzold1998` or [Scholarpedia](http://www.scholarpedia.org/article/Differential-algebraic_equations). In PETSc, TS deals with the semi-discrete form of the equations, so that space has already been discretized. If the DAE depends explicitly on the coordinate $x$, then this will just appear as any other data for the equation, not as an explicit argument. Thus we have 2947f296bb3SBarry Smith 2957f296bb3SBarry Smith$$ 2967f296bb3SBarry SmithF(t, u, \dot{u}) = 0 2977f296bb3SBarry Smith$$ 2987f296bb3SBarry Smith 2997f296bb3SBarry SmithIn this form, only fully implicit solvers are appropriate. However, specialized solvers for restricted forms of DAE are supported by PETSc. Below we consider an ODE which is augmented with algebraic constraints on the variables. 3007f296bb3SBarry Smith 3017f296bb3SBarry Smith### Hessenberg Index-1 DAE 3027f296bb3SBarry Smith 3037f296bb3SBarry Smith> This is a Semi-Explicit Index-1 DAE which has the form 3047f296bb3SBarry Smith 3057f296bb3SBarry Smith$$ 3067f296bb3SBarry Smith\begin{aligned} 3077f296bb3SBarry Smith \dot{u} &= f(t, u, z) \\ 3087f296bb3SBarry Smith 0 &= h(t, u, z) 3097f296bb3SBarry Smith\end{aligned} 3107f296bb3SBarry Smith$$ 3117f296bb3SBarry Smith 3127f296bb3SBarry Smithwhere $z$ is a new constraint variable, and the Jacobian $\frac{dh}{dz}$ is non-singular everywhere. We have suppressed the $x$ dependence since it plays no role here. Using the non-singularity of the Jacobian and the Implicit Function Theorem, we can solve for $z$ in terms of $u$. This means we could, in principle, plug $z(u)$ into the first equation to obtain a simple ODE, even if this is not the numerical process we use. Below we show that this type of DAE can be used with IMEX schemes. 3137f296bb3SBarry Smith 3147f296bb3SBarry Smith### Hessenberg Index-2 DAE 3157f296bb3SBarry Smith 3167f296bb3SBarry Smith> This DAE has the form 3177f296bb3SBarry Smith 3187f296bb3SBarry Smith$$ 3197f296bb3SBarry Smith\begin{aligned} 3207f296bb3SBarry Smith \dot{u} &= f(t, u, z) \\ 3217f296bb3SBarry Smith 0 &= h(t, u) 3227f296bb3SBarry Smith\end{aligned} 3237f296bb3SBarry Smith$$ 3247f296bb3SBarry Smith 3257f296bb3SBarry SmithNotice that the constraint equation $h$ is not a function of the constraint variable $z$. This means that we cannot naively invert as we did in the index-1 case. Our strategy will be to convert this into an index-1 DAE using a time derivative, which loosely corresponds to the idea of an index being the number of derivatives necessary to get back to an ODE. If we differentiate the constraint equation with respect to time, we can use the ODE to simplify it, 3267f296bb3SBarry Smith 3277f296bb3SBarry Smith$$ 3287f296bb3SBarry Smith\begin{aligned} 3297f296bb3SBarry Smith 0 &= \dot{h}(t, u) \\ 3307f296bb3SBarry Smith &= \frac{dh}{du} \dot{u} + \frac{\partial h}{\partial t} \\ 3317f296bb3SBarry Smith &= \frac{dh}{du} f(t, u, z) + \frac{\partial h}{\partial t} 3327f296bb3SBarry Smith\end{aligned} 3337f296bb3SBarry Smith$$ 3347f296bb3SBarry Smith 3357f296bb3SBarry SmithIf the Jacobian $\frac{dh}{du} \frac{df}{dz}$ is non-singular, then we have precisely a semi-explicit index-1 DAE, and we can once again use the PETSc IMEX tools to solve it. A common example of an index-2 DAE is the incompressible Navier-Stokes equations, since the continuity equation $\nabla\cdot u = 0$ does not involve the pressure. Using PETSc IMEX with the above conversion then corresponds to the Segregated Runge-Kutta method applied to this equation {cite}`colomesbadia2016`. 3367f296bb3SBarry Smith 3377f296bb3SBarry Smith## Using Implicit-Explicit (IMEX) Methods 3387f296bb3SBarry Smith 3397f296bb3SBarry SmithFor “stiff” problems or those with multiple time scales $F()$ will 3407f296bb3SBarry Smithbe treated implicitly using a method suitable for stiff problems and 3417f296bb3SBarry Smith$G()$ will be treated explicitly when using an IMEX method like 3427f296bb3SBarry SmithTSARKIMEX. $F()$ is typically linear or weakly nonlinear while 3437f296bb3SBarry Smith$G()$ may have very strong nonlinearities such as arise in 3447f296bb3SBarry Smithnon-oscillatory methods for hyperbolic PDE. The user provides three 3457f296bb3SBarry Smithpieces of information, the APIs for which have been described above. 3467f296bb3SBarry Smith 3477f296bb3SBarry Smith- “Slow” part $G(t,u)$ using `TSSetRHSFunction()`. 3487f296bb3SBarry Smith- “Stiff” part $F(t,u,\dot u)$ using `TSSetIFunction()`. 3497f296bb3SBarry Smith- Jacobian $F_u + \sigma F_{\dot u}$ using `TSSetIJacobian()`. 3507f296bb3SBarry Smith 3517f296bb3SBarry SmithThe user needs to set `TSSetEquationType()` to `TS_EQ_IMPLICIT` or 3527f296bb3SBarry Smithhigher if the problem is implicit; e.g., 3537f296bb3SBarry Smith$F(t,u,\dot u) = M \dot u - f(t,u)$, where $M$ is not the 3547f296bb3SBarry Smithidentity matrix: 3557f296bb3SBarry Smith 3567f296bb3SBarry Smith- the problem is an implicit ODE (defined implicitly through 3577f296bb3SBarry Smith `TSSetIFunction()`) or 3587f296bb3SBarry Smith- a DAE is being solved. 3597f296bb3SBarry Smith 3607f296bb3SBarry SmithAn IMEX problem representation can be made implicit by setting `TSARKIMEXSetFullyImplicit()`. 3617f296bb3SBarry SmithNote that multilevel preconditioners (e.g. `PCMG`), won't work in the fully implicit case; the 3627f296bb3SBarry Smithsame holds true for any other `TS` type requiring a fully implicit formulation in case both 3637f296bb3SBarry SmithJacobians are specified. 3647f296bb3SBarry Smith 3657f296bb3SBarry SmithIn PETSc, DAEs and ODEs are formulated as $F(t,u,\dot{u})=G(t,u)$, where $F()$ is meant to be integrated implicitly and $G()$ explicitly. An IMEX formulation such as $M\dot{u}=f(t,u)+g(t,u)$ requires the user to provide $M^{-1} g(t,u)$ or solve $g(t,u) - M x=0$ in place of $G(t,u)$. General cases such as $F(t,u,\dot{u})=G(t,u)$ are not amenable to IMEX Runge-Kutta, but can be solved by using fully implicit methods. Some use-case examples for `TSARKIMEX` are listed in {numref}`tab_DE_forms` and a list of methods with a summary of their properties is given in {any}`tab_IMEX_RK_PETSc`. 3667f296bb3SBarry Smith 3677f296bb3SBarry Smith```{eval-rst} 3687f296bb3SBarry Smith.. list-table:: Use case examples for ``TSARKIMEX`` 3697f296bb3SBarry Smith :name: tab_DE_forms 3707f296bb3SBarry Smith :widths: 40 40 80 3717f296bb3SBarry Smith 3727f296bb3SBarry Smith * - :math:`\dot{u} = g(t,u)` 3737f296bb3SBarry Smith - nonstiff ODE 3747f296bb3SBarry Smith - :math:`\begin{aligned}F(t,u,\dot{u}) &= \dot{u} \\ G(t,u) &= g(t,u)\end{aligned}` 3757f296bb3SBarry Smith * - :math:`M \dot{u} = g(t,u)` 3767f296bb3SBarry Smith - nonstiff ODE with mass matrix 3777f296bb3SBarry Smith - :math:`\begin{aligned}F(t,u,\dot{u}) &= \dot{u} \\ G(t,u) &= M^{-1} g(t,u)\end{aligned}` 3787f296bb3SBarry Smith * - :math:`\dot{u} = f(t,u)` 3797f296bb3SBarry Smith - stiff ODE 3807f296bb3SBarry Smith - :math:`\begin{aligned}F(t,u,\dot{u}) &= \dot{u} - f(t,u) \\ G(t,u) &= 0\end{aligned}` 3817f296bb3SBarry Smith * - :math:`M \dot{u} = f(t,u)` 3827f296bb3SBarry Smith - stiff ODE with mass matrix 3837f296bb3SBarry Smith - :math:`\begin{aligned}F(t,u,\dot{u}) &= M \dot{u} - f(t,u) \\ G(t,u) &= 0\end{aligned}` 3847f296bb3SBarry Smith * - :math:`\dot{u} = f(t,u) + g(t,u)` 3857f296bb3SBarry Smith - stiff-nonstiff ODE 3867f296bb3SBarry Smith - :math:`\begin{aligned}F(t,u,\dot{u}) &= \dot{u} - f(t,u) \\ G(t,u) &= g(t,u)\end{aligned}` 3877f296bb3SBarry Smith * - :math:`M \dot{u} = f(t,u) + g(t,u)` 3887f296bb3SBarry Smith - stiff-nonstiff ODE with mass matrix 3897f296bb3SBarry Smith - :math:`\begin{aligned}F(t,u,\dot{u}) &= M\dot{u} - f(t,u) \\ G(t,u) &= M^{-1} g(t,u)\end{aligned}` 3907f296bb3SBarry Smith * - :math:`\begin{aligned}\dot{u} &= f(t,u,z) + g(t,u,z)\\0 &= h(t,y,z)\end{aligned}` 3917f296bb3SBarry Smith - semi-explicit index-1 DAE 3927f296bb3SBarry Smith - :math:`\begin{aligned}F(t,u,\dot{u}) &= \begin{pmatrix}\dot{u} - f(t,u,z)\\h(t, u, z)\end{pmatrix}\\G(t,u) &= g(t,u)\end{aligned}` 3937f296bb3SBarry Smith * - :math:`f(t,u,\dot{u})=0` 3947f296bb3SBarry Smith - fully implicit ODE/DAE 3957f296bb3SBarry Smith - :math:`\begin{aligned}F(t,u,\dot{u}) &= f(t,u,\dot{u})\\G(t,u) &= 0\end{aligned}`; the user needs to set ``TSSetEquationType()`` to ``TS_EQ_IMPLICIT`` or higher 3967f296bb3SBarry Smith``` 3977f296bb3SBarry Smith 3987f296bb3SBarry Smith{numref}`tab_IMEX_RK_PETSc` lists of the currently available IMEX Runge-Kutta schemes. For each method, it gives the `-ts_arkimex_type` name, the reference, the total number of stages/implicit stages, the order/stage-order, the implicit stability properties (IM), stiff accuracy (SA), the existence of an embedded scheme, and dense output (DO). 3997f296bb3SBarry Smith 4007f296bb3SBarry Smith```{eval-rst} 4017f296bb3SBarry Smith.. list-table:: IMEX Runge-Kutta schemes 4027f296bb3SBarry Smith :name: tab_IMEX_RK_PETSc 4037f296bb3SBarry Smith :header-rows: 1 4047f296bb3SBarry Smith 4057f296bb3SBarry Smith * - Name 4067f296bb3SBarry Smith - Reference 4077f296bb3SBarry Smith - Stages (IM) 4087f296bb3SBarry Smith - Order (Stage) 4097f296bb3SBarry Smith - IM 4107f296bb3SBarry Smith - SA 4117f296bb3SBarry Smith - Embed 4127f296bb3SBarry Smith - DO 4137f296bb3SBarry Smith - Remarks 4147f296bb3SBarry Smith * - a2 4157f296bb3SBarry Smith - based on CN 4167f296bb3SBarry Smith - 2 (1) 4177f296bb3SBarry Smith - 2 (2) 4187f296bb3SBarry Smith - A-Stable 4197f296bb3SBarry Smith - yes 4207f296bb3SBarry Smith - yes (1) 4217f296bb3SBarry Smith - yes (2) 4227f296bb3SBarry Smith - 4237f296bb3SBarry Smith * - l2 4247f296bb3SBarry Smith - SSP2(2,2,2) :cite:`pareschi_2005` 4257f296bb3SBarry Smith - 2 (2) 4267f296bb3SBarry Smith - 2 (1) 4277f296bb3SBarry Smith - L-Stable 4287f296bb3SBarry Smith - yes 4297f296bb3SBarry Smith - yes (1) 4307f296bb3SBarry Smith - yes (2) 4317f296bb3SBarry Smith - SSP SDIRK 4327f296bb3SBarry Smith * - ars122 4337f296bb3SBarry Smith - ARS122 :cite:`ascher_1997` 4347f296bb3SBarry Smith - 2 (1) 4357f296bb3SBarry Smith - 3 (1) 4367f296bb3SBarry Smith - A-Stable 4377f296bb3SBarry Smith - yes 4387f296bb3SBarry Smith - yes (1) 4397f296bb3SBarry Smith - yes (2) 4407f296bb3SBarry Smith - 4417f296bb3SBarry Smith * - 2c 4427f296bb3SBarry Smith - :cite:`giraldo_2013` 4437f296bb3SBarry Smith - 3 (2) 4447f296bb3SBarry Smith - 2 (2) 4457f296bb3SBarry Smith - L-Stable 4467f296bb3SBarry Smith - yes 4477f296bb3SBarry Smith - yes (1) 4487f296bb3SBarry Smith - yes (2) 4497f296bb3SBarry Smith - SDIRK 4507f296bb3SBarry Smith * - 2d 4517f296bb3SBarry Smith - :cite:`giraldo_2013` 4527f296bb3SBarry Smith - 3 (2) 4537f296bb3SBarry Smith - 2 (2) 4547f296bb3SBarry Smith - L-Stable 4557f296bb3SBarry Smith - yes 4567f296bb3SBarry Smith - yes (1) 4577f296bb3SBarry Smith - yes (2) 4587f296bb3SBarry Smith - SDIRK 4597f296bb3SBarry Smith * - 2e 4607f296bb3SBarry Smith - :cite:`giraldo_2013` 4617f296bb3SBarry Smith - 3 (2) 4627f296bb3SBarry Smith - 2 (2) 4637f296bb3SBarry Smith - L-Stable 4647f296bb3SBarry Smith - yes 4657f296bb3SBarry Smith - yes (1) 4667f296bb3SBarry Smith - yes (2) 4677f296bb3SBarry Smith - SDIRK 4687f296bb3SBarry Smith * - prssp2 4697f296bb3SBarry Smith - PRS(3,3,2) :cite:`pareschi_2005` 4707f296bb3SBarry Smith - 3 (3) 4717f296bb3SBarry Smith - 3 (1) 4727f296bb3SBarry Smith - L-Stable 4737f296bb3SBarry Smith - yes 4747f296bb3SBarry Smith - no 4757f296bb3SBarry Smith - no 4767f296bb3SBarry Smith - SSP 4777f296bb3SBarry Smith * - 3 4787f296bb3SBarry Smith - :cite:`kennedy_2003` 4797f296bb3SBarry Smith - 4 (3) 4807f296bb3SBarry Smith - 3 (2) 4817f296bb3SBarry Smith - L-Stable 4827f296bb3SBarry Smith - yes 4837f296bb3SBarry Smith - yes (2) 4847f296bb3SBarry Smith - yes (2) 4857f296bb3SBarry Smith - SDIRK 4867f296bb3SBarry Smith * - bpr3 4877f296bb3SBarry Smith - :cite:`boscarino_tr2011` 4887f296bb3SBarry Smith - 5 (4) 4897f296bb3SBarry Smith - 3 (2) 4907f296bb3SBarry Smith - L-Stable 4917f296bb3SBarry Smith - yes 4927f296bb3SBarry Smith - no 4937f296bb3SBarry Smith - no 4947f296bb3SBarry Smith - SDIRK 4957f296bb3SBarry Smith * - ars443 4967f296bb3SBarry Smith - :cite:`ascher_1997` 4977f296bb3SBarry Smith - 5 (4) 4987f296bb3SBarry Smith - 3 (1) 4997f296bb3SBarry Smith - L-Stable 5007f296bb3SBarry Smith - yes 5017f296bb3SBarry Smith - no 5027f296bb3SBarry Smith - no 5037f296bb3SBarry Smith - SDIRK 5047f296bb3SBarry Smith * - 4 5057f296bb3SBarry Smith - :cite:`kennedy_2003` 5067f296bb3SBarry Smith - 6 (5) 5077f296bb3SBarry Smith - 4 (2) 5087f296bb3SBarry Smith - L-Stable 5097f296bb3SBarry Smith - yes 5107f296bb3SBarry Smith - yes (3) 5117f296bb3SBarry Smith - yes 5127f296bb3SBarry Smith - SDIRK 5137f296bb3SBarry Smith * - 5 5147f296bb3SBarry Smith - :cite:`kennedy_2003` 5157f296bb3SBarry Smith - 8 (7) 5167f296bb3SBarry Smith - 5 (2) 5177f296bb3SBarry Smith - L-Stable 5187f296bb3SBarry Smith - yes 5197f296bb3SBarry Smith - yes (4) 5207f296bb3SBarry Smith - yes (3) 5217f296bb3SBarry Smith - SDIRK 5227f296bb3SBarry Smith``` 5237f296bb3SBarry Smith 5247f296bb3SBarry SmithROSW are linearized implicit Runge-Kutta methods known as Rosenbrock 5257f296bb3SBarry SmithW-methods. They can accommodate inexact Jacobian matrices in their 5267f296bb3SBarry Smithformulation. A series of methods are available in PETSc are listed in 5277f296bb3SBarry Smith{numref}`tab_IMEX_RosW_PETSc` below. For each method, it gives the reference, the total number of stages and implicit stages, the scheme order and stage order, the implicit stability properties (IM), stiff accuracy (SA), the existence of an embedded scheme, dense output (DO), the capacity to use inexact Jacobian matrices (-W), and high order integration of differential algebraic equations (PDAE). 5287f296bb3SBarry Smith 5297f296bb3SBarry Smith```{eval-rst} 5307f296bb3SBarry Smith.. list-table:: Rosenbrock W-schemes 5317f296bb3SBarry Smith :name: tab_IMEX_RosW_PETSc 5327f296bb3SBarry Smith :header-rows: 1 5337f296bb3SBarry Smith 5347f296bb3SBarry Smith * - TS 5357f296bb3SBarry Smith - Reference 5367f296bb3SBarry Smith - Stages (IM) 5377f296bb3SBarry Smith - Order (Stage) 5387f296bb3SBarry Smith - IM 5397f296bb3SBarry Smith - SA 5407f296bb3SBarry Smith - Embed 5417f296bb3SBarry Smith - DO 5427f296bb3SBarry Smith - -W 5437f296bb3SBarry Smith - PDAE 5447f296bb3SBarry Smith - Remarks 5457f296bb3SBarry Smith * - theta1 5467f296bb3SBarry Smith - classical 5477f296bb3SBarry Smith - 1(1) 5487f296bb3SBarry Smith - 1(1) 5497f296bb3SBarry Smith - L-Stable 5507f296bb3SBarry Smith - - 5517f296bb3SBarry Smith - - 5527f296bb3SBarry Smith - - 5537f296bb3SBarry Smith - - 5547f296bb3SBarry Smith - - 5557f296bb3SBarry Smith - - 5567f296bb3SBarry Smith * - theta2 5577f296bb3SBarry Smith - classical 5587f296bb3SBarry Smith - 1(1) 5597f296bb3SBarry Smith - 2(2) 5607f296bb3SBarry Smith - A-Stable 5617f296bb3SBarry Smith - - 5627f296bb3SBarry Smith - - 5637f296bb3SBarry Smith - - 5647f296bb3SBarry Smith - - 5657f296bb3SBarry Smith - - 5667f296bb3SBarry Smith - - 5677f296bb3SBarry Smith * - 2m 5687f296bb3SBarry Smith - Zoltan 5697f296bb3SBarry Smith - 2(2) 5707f296bb3SBarry Smith - 2(1) 5717f296bb3SBarry Smith - L-Stable 5727f296bb3SBarry Smith - No 5737f296bb3SBarry Smith - Yes(1) 5747f296bb3SBarry Smith - Yes(2) 5757f296bb3SBarry Smith - Yes 5767f296bb3SBarry Smith - No 5777f296bb3SBarry Smith - SSP 5787f296bb3SBarry Smith * - 2p 5797f296bb3SBarry Smith - Zoltan 5807f296bb3SBarry Smith - 2(2) 5817f296bb3SBarry Smith - 2(1) 5827f296bb3SBarry Smith - L-Stable 5837f296bb3SBarry Smith - No 5847f296bb3SBarry Smith - Yes(1) 5857f296bb3SBarry Smith - Yes(2) 5867f296bb3SBarry Smith - Yes 5877f296bb3SBarry Smith - No 5887f296bb3SBarry Smith - SSP 5897f296bb3SBarry Smith * - ra3pw 5907f296bb3SBarry Smith - :cite:`rang_2005` 5917f296bb3SBarry Smith - 3(3) 5927f296bb3SBarry Smith - 3(1) 5937f296bb3SBarry Smith - A-Stable 5947f296bb3SBarry Smith - No 5957f296bb3SBarry Smith - Yes 5967f296bb3SBarry Smith - Yes(2) 5977f296bb3SBarry Smith - No 5987f296bb3SBarry Smith - Yes(3) 5997f296bb3SBarry Smith - - 6007f296bb3SBarry Smith * - ra34pw2 6017f296bb3SBarry Smith - :cite:`rang_2005` 6027f296bb3SBarry Smith - 4(4) 6037f296bb3SBarry Smith - 3(1) 6047f296bb3SBarry Smith - L-Stable 6057f296bb3SBarry Smith - Yes 6067f296bb3SBarry Smith - Yes 6077f296bb3SBarry Smith - Yes(3) 6087f296bb3SBarry Smith - Yes 6097f296bb3SBarry Smith - Yes(3) 6107f296bb3SBarry Smith - - 6117f296bb3SBarry Smith * - rodas3 6127f296bb3SBarry Smith - :cite:`sandu_1997` 6137f296bb3SBarry Smith - 4(4) 6147f296bb3SBarry Smith - 3(1) 6157f296bb3SBarry Smith - L-Stable 6167f296bb3SBarry Smith - Yes 6177f296bb3SBarry Smith - Yes 6187f296bb3SBarry Smith - No 6197f296bb3SBarry Smith - No 6207f296bb3SBarry Smith - Yes 6217f296bb3SBarry Smith - - 6227f296bb3SBarry Smith * - sandu3 6237f296bb3SBarry Smith - :cite:`sandu_1997` 6247f296bb3SBarry Smith - 3(3) 6257f296bb3SBarry Smith - 3(1) 6267f296bb3SBarry Smith - L-Stable 6277f296bb3SBarry Smith - Yes 6287f296bb3SBarry Smith - Yes 6297f296bb3SBarry Smith - Yes(2) 6307f296bb3SBarry Smith - No 6317f296bb3SBarry Smith - No 6327f296bb3SBarry Smith - - 6337f296bb3SBarry Smith * - assp3p3s1c 6347f296bb3SBarry Smith - unpub. 6357f296bb3SBarry Smith - 3(2) 6367f296bb3SBarry Smith - 3(1) 6377f296bb3SBarry Smith - A-Stable 6387f296bb3SBarry Smith - No 6397f296bb3SBarry Smith - Yes 6407f296bb3SBarry Smith - Yes(2) 6417f296bb3SBarry Smith - Yes 6427f296bb3SBarry Smith - No 6437f296bb3SBarry Smith - SSP 6447f296bb3SBarry Smith * - lassp3p4s2c 6457f296bb3SBarry Smith - unpub. 6467f296bb3SBarry Smith - 4(3) 6477f296bb3SBarry Smith - 3(1) 6487f296bb3SBarry Smith - L-Stable 6497f296bb3SBarry Smith - No 6507f296bb3SBarry Smith - Yes 6517f296bb3SBarry Smith - Yes(3) 6527f296bb3SBarry Smith - Yes 6537f296bb3SBarry Smith - No 6547f296bb3SBarry Smith - SSP 6557f296bb3SBarry Smith * - lassp3p4s2c 6567f296bb3SBarry Smith - unpub. 6577f296bb3SBarry Smith - 4(3) 6587f296bb3SBarry Smith - 3(1) 6597f296bb3SBarry Smith - L-Stable 6607f296bb3SBarry Smith - No 6617f296bb3SBarry Smith - Yes 6627f296bb3SBarry Smith - Yes(3) 6637f296bb3SBarry Smith - Yes 6647f296bb3SBarry Smith - No 6657f296bb3SBarry Smith - SSP 6667f296bb3SBarry Smith * - ark3 6677f296bb3SBarry Smith - unpub. 6687f296bb3SBarry Smith - 4(3) 6697f296bb3SBarry Smith - 3(1) 6707f296bb3SBarry Smith - L-Stable 6717f296bb3SBarry Smith - No 6727f296bb3SBarry Smith - Yes 6737f296bb3SBarry Smith - Yes(3) 6747f296bb3SBarry Smith - Yes 6757f296bb3SBarry Smith - No 6767f296bb3SBarry Smith - IMEX-RK 6777f296bb3SBarry Smith``` 6787f296bb3SBarry Smith 6797f296bb3SBarry Smith## IMEX Methods for fast-slow systems 6807f296bb3SBarry Smith 6817f296bb3SBarry SmithConsider a fast-slow ODE system 6827f296bb3SBarry Smith 6837f296bb3SBarry Smith$$ 6847f296bb3SBarry Smith\begin{aligned} 6857f296bb3SBarry Smith\dot{u}^{slow} & = f^{slow}(t, u^{slow},u^{fast}) \\ 6867f296bb3SBarry SmithM \dot{u}^{fast} & = g^{fast}(t, u^{slow},u^{fast}) + f^{fast}(t, u^{slow},u^{fast}) 6877f296bb3SBarry Smith\end{aligned} 6887f296bb3SBarry Smith$$ 6897f296bb3SBarry Smith 6907f296bb3SBarry Smithwhere $u^{slow}$ is the slow component and $u^{fast}$ is the 6917f296bb3SBarry Smithfast component. The fast component can be partitioned additively as 6927f296bb3SBarry Smithdescribed above. Thus we want to treat $f^{slow}()$ and 6937f296bb3SBarry Smith$f^{fast}()$ explicitly and the other terms implicitly when using 6947f296bb3SBarry SmithTSARKIMEX. This is achieved by using the following APIs: 6957f296bb3SBarry Smith 6967f296bb3SBarry Smith- `TSARKIMEXSetFastSlowSplit()` informs PETSc to use ARKIMEX to solve a fast-slow system. 6977f296bb3SBarry Smith- `TSRHSSplitSetIS()` specifies the index set for the slow/fast components. 6987f296bb3SBarry Smith- `TSRHSSplitSetRHSFunction()` specifies the parts to be handled explicitly $f^{slow}()$ and $f^{fast}()$. 6997f296bb3SBarry Smith- `TSRHSSplitSetIFunction()` and `TSRHSSplitSetIJacobian()` specify the implicit part and its Jacobian. 7007f296bb3SBarry Smith 7017f296bb3SBarry SmithNote that this ODE system can also be solved by padding zeros in the implicit part and using the standard IMEX methods. However, one needs to provide the full-dimensional Jacobian whereas only a partial Jacobian is needed for the fast-slow split which is more efficient in storage and speed. 7027f296bb3SBarry Smith 703efa39862SBarry Smith(sec_ts_glee)= 704efa39862SBarry Smith 7057f296bb3SBarry Smith## GLEE methods 7067f296bb3SBarry Smith 7077f296bb3SBarry SmithIn this section, we describe explicit and implicit time stepping methods 7087f296bb3SBarry Smithwith global error estimation that are introduced in 7097f296bb3SBarry Smith{cite}`constantinescu_tr2016b`. The solution vector for a 7107f296bb3SBarry SmithGLEE method is either \[$y$, $\tilde{y}$\] or 7117f296bb3SBarry Smith\[$y$,$\varepsilon$\], where $y$ is the solution, 7127f296bb3SBarry Smith$\tilde{y}$ is the “auxiliary solution,” and $\varepsilon$ 7137f296bb3SBarry Smithis the error. The working vector that `TSGLEE` uses is $Y$ = 7147f296bb3SBarry Smith\[$y$,$\tilde{y}$\], or \[$y$,$\varepsilon$\]. A 7157f296bb3SBarry SmithGLEE method is defined by 7167f296bb3SBarry Smith 7177f296bb3SBarry Smith- $(p,r,s)$: (order, steps, and stages), 7187f296bb3SBarry Smith- $\gamma$: factor representing the global error ratio, 7197f296bb3SBarry Smith- $A, U, B, V$: method coefficients, 7207f296bb3SBarry Smith- $S$: starting method to compute the working vector from the 7217f296bb3SBarry Smith solution (say at the beginning of time integration) so that 7227f296bb3SBarry Smith $Y = Sy$, 7237f296bb3SBarry Smith- $F$: finalizing method to compute the solution from the working 7247f296bb3SBarry Smith vector,$y = FY$. 7257f296bb3SBarry Smith- $F_\text{embed}$: coefficients for computing the auxiliary 7267f296bb3SBarry Smith solution $\tilde{y}$ from the working vector 7277f296bb3SBarry Smith ($\tilde{y} = F_\text{embed} Y$), 7287f296bb3SBarry Smith- $F_\text{error}$: coefficients to compute the estimated error 7297f296bb3SBarry Smith vector from the working vector 7307f296bb3SBarry Smith ($\varepsilon = F_\text{error} Y$). 7317f296bb3SBarry Smith- $S_\text{error}$: coefficients to initialize the auxiliary 7327f296bb3SBarry Smith solution ($\tilde{y}$ or $\varepsilon$) from a specified 7337f296bb3SBarry Smith error vector ($\varepsilon$). It is currently implemented only 7347f296bb3SBarry Smith for $r = 2$. We have $y_\text{aux} = 7357f296bb3SBarry Smith S_{error}[0]*\varepsilon + S_\text{error}[1]*y$, where 7367f296bb3SBarry Smith $y_\text{aux}$ is the 2nd component of the working vector 7377f296bb3SBarry Smith $Y$. 7387f296bb3SBarry Smith 7397f296bb3SBarry SmithThe methods can be described in two mathematically equivalent forms: 7407f296bb3SBarry Smithpropagate two components (“$y\tilde{y}$ form”) and propagating the 7417f296bb3SBarry Smithsolution and its estimated error (“$y\varepsilon$ form”). The two 7427f296bb3SBarry Smithforms are not explicitly specified in `TSGLEE`; rather, the specific 7437f296bb3SBarry Smithvalues of $B, U, S, F, F_{embed}$, and $F_{error}$ 7447f296bb3SBarry Smithcharacterize whether the method is in $y\tilde{y}$ or 7457f296bb3SBarry Smith$y\varepsilon$ form. 7467f296bb3SBarry Smith 7477f296bb3SBarry SmithThe API used by this `TS` method includes: 7487f296bb3SBarry Smith 7497f296bb3SBarry Smith- `TSGetSolutionComponents`: Get all the solution components of the 7507f296bb3SBarry Smith working vector 7517f296bb3SBarry Smith 7527f296bb3SBarry Smith ``` 75393a54799SPierre Jolivet PetscCall(TSGetSolutionComponents(TS, int*, Vec*)) 7547f296bb3SBarry Smith ``` 7557f296bb3SBarry Smith 7567f296bb3SBarry Smith Call with `NULL` as the last argument to get the total number of 7577f296bb3SBarry Smith components in the working vector $Y$ (this is $r$ (not 7587f296bb3SBarry Smith $r-1$)), then call to get the $i$-th solution component. 7597f296bb3SBarry Smith 7607f296bb3SBarry Smith- `TSGetAuxSolution`: Returns the auxiliary solution 7617f296bb3SBarry Smith $\tilde{y}$ (computed as $F_\text{embed} Y$) 7627f296bb3SBarry Smith 7637f296bb3SBarry Smith ``` 76493a54799SPierre Jolivet PetscCall(TSGetAuxSolution(TS, Vec*)) 7657f296bb3SBarry Smith ``` 7667f296bb3SBarry Smith 7677f296bb3SBarry Smith- `TSGetTimeError`: Returns the estimated error vector 7687f296bb3SBarry Smith $\varepsilon$ (computed as $F_\text{error} Y$ if 7697f296bb3SBarry Smith $n=0$ or restores the error estimate at the end of the previous 7707f296bb3SBarry Smith step if $n=-1$) 7717f296bb3SBarry Smith 7727f296bb3SBarry Smith ``` 77393a54799SPierre Jolivet PetscCall(TSGetTimeError(TS, PetscInt n, Vec*)) 7747f296bb3SBarry Smith ``` 7757f296bb3SBarry Smith 7767f296bb3SBarry Smith- `TSSetTimeError`: Initializes the auxiliary solution 7777f296bb3SBarry Smith ($\tilde{y}$ or $\varepsilon$) for a specified initial 7787f296bb3SBarry Smith error. 7797f296bb3SBarry Smith 7807f296bb3SBarry Smith ``` 78193a54799SPierre Jolivet PetscCall(TSSetTimeError(TS, Vec)) 7827f296bb3SBarry Smith ``` 7837f296bb3SBarry Smith 7847f296bb3SBarry SmithThe local error is estimated as $\varepsilon(n+1)-\varepsilon(n)$. 7857f296bb3SBarry SmithThis is to be used in the error control. The error in $y\tilde{y}$ 7867f296bb3SBarry SmithGLEE is 7877f296bb3SBarry Smith$\varepsilon(n) = \frac{1}{1-\gamma} * (\tilde{y}(n) - y(n))$. 7887f296bb3SBarry Smith 7897f296bb3SBarry SmithNote that $y$ and $\tilde{y}$ are reported to `TSAdapt` 7907f296bb3SBarry Smith`basic` (`TSADAPTBASIC`), and thus it computes the local error as 7917f296bb3SBarry Smith$\varepsilon_{loc} = (\tilde{y} - 7927f296bb3SBarry Smithy)$. However, the actual local error is $\varepsilon_{loc} 7937f296bb3SBarry Smith= \varepsilon_{n+1} - \varepsilon_n = \frac{1}{1-\gamma} * [(\tilde{y} - 7947f296bb3SBarry Smithy)_{n+1} - (\tilde{y} - y)_n]$. 7957f296bb3SBarry Smith 7967f296bb3SBarry Smith{numref}`tab_IMEX_GLEE_PETSc` lists currently available GL schemes with global error estimation {cite}`constantinescu_tr2016b`. 7977f296bb3SBarry Smith 7987f296bb3SBarry Smith```{eval-rst} 7997f296bb3SBarry Smith.. list-table:: GL schemes with global error estimation 8007f296bb3SBarry Smith :name: tab_IMEX_GLEE_PETSc 8017f296bb3SBarry Smith :header-rows: 1 8027f296bb3SBarry Smith 8037f296bb3SBarry Smith * - TS 8047f296bb3SBarry Smith - Reference 8057f296bb3SBarry Smith - IM/EX 8067f296bb3SBarry Smith - :math:`(p,r,s)` 8077f296bb3SBarry Smith - :math:`\gamma` 8087f296bb3SBarry Smith - Form 8097f296bb3SBarry Smith - Notes 8107f296bb3SBarry Smith * - ``TSGLEEi1`` 8117f296bb3SBarry Smith - ``BE1`` 8127f296bb3SBarry Smith - IM 8137f296bb3SBarry Smith - :math:`(1,3,2)` 8147f296bb3SBarry Smith - :math:`0.5` 8157f296bb3SBarry Smith - :math:`y\varepsilon` 8167f296bb3SBarry Smith - Based on backward Euler 8177f296bb3SBarry Smith * - ``TSGLEE23`` 8187f296bb3SBarry Smith - ``23`` 8197f296bb3SBarry Smith - EX 8207f296bb3SBarry Smith - :math:`(2,3,2)` 8217f296bb3SBarry Smith - :math:`0` 8227f296bb3SBarry Smith - :math:`y\varepsilon` 8237f296bb3SBarry Smith - 8247f296bb3SBarry Smith * - ``TSGLEE24`` 8257f296bb3SBarry Smith - ``24`` 8267f296bb3SBarry Smith - EX 8277f296bb3SBarry Smith - :math:`(2,4,2)` 8287f296bb3SBarry Smith - :math:`0` 8297f296bb3SBarry Smith - :math:`y\tilde{y}` 8307f296bb3SBarry Smith - 8317f296bb3SBarry Smith * - ``TSGLEE25I`` 8327f296bb3SBarry Smith - ``25i`` 8337f296bb3SBarry Smith - EX 8347f296bb3SBarry Smith - :math:`(2,5,2)` 8357f296bb3SBarry Smith - :math:`0` 8367f296bb3SBarry Smith - :math:`y\tilde{y}` 8377f296bb3SBarry Smith - 8387f296bb3SBarry Smith * - ``TSGLEE35`` 8397f296bb3SBarry Smith - ``35`` 8407f296bb3SBarry Smith - EX 8417f296bb3SBarry Smith - :math:`(3,5,2)` 8427f296bb3SBarry Smith - :math:`0` 8437f296bb3SBarry Smith - :math:`y\tilde{y}` 8447f296bb3SBarry Smith - 8457f296bb3SBarry Smith * - ``TSGLEEEXRK2A`` 8467f296bb3SBarry Smith - ``exrk2a`` 8477f296bb3SBarry Smith - EX 8487f296bb3SBarry Smith - :math:`(2,6,2)` 8497f296bb3SBarry Smith - :math:`0.25` 8507f296bb3SBarry Smith - :math:`y\varepsilon` 8517f296bb3SBarry Smith - 8527f296bb3SBarry Smith * - ``TSGLEERK32G1`` 8537f296bb3SBarry Smith - ``rk32g1`` 8547f296bb3SBarry Smith - EX 8557f296bb3SBarry Smith - :math:`(3,8,2)` 8567f296bb3SBarry Smith - :math:`0` 8577f296bb3SBarry Smith - :math:`y\varepsilon` 8587f296bb3SBarry Smith - 8597f296bb3SBarry Smith * - ``TSGLEERK285EX`` 8607f296bb3SBarry Smith - ``rk285ex`` 8617f296bb3SBarry Smith - EX 8627f296bb3SBarry Smith - :math:`(2,9,2)` 8637f296bb3SBarry Smith - :math:`0.25` 8647f296bb3SBarry Smith - :math:`y\varepsilon` 8657f296bb3SBarry Smith - 8667f296bb3SBarry Smith``` 8677f296bb3SBarry Smith 8687f296bb3SBarry Smith## Using fully implicit methods 8697f296bb3SBarry Smith 8707f296bb3SBarry SmithTo use a fully implicit method like `TSTHETA`, `TSBDF` or `TSDIRK`, either 8717f296bb3SBarry Smithprovide the Jacobian of $F()$ (and $G()$ if $G()$ is 8727f296bb3SBarry Smithprovided) or use a `DM` that provides a coloring so the Jacobian can 8737f296bb3SBarry Smithbe computed efficiently via finite differences. 8747f296bb3SBarry Smith 8757f296bb3SBarry Smith## Using the Explicit Runge-Kutta timestepper with variable timesteps 8767f296bb3SBarry Smith 8777f296bb3SBarry SmithThe explicit Euler and Runge-Kutta methods require the ODE be in the 8787f296bb3SBarry Smithform 8797f296bb3SBarry Smith 8807f296bb3SBarry Smith$$ 8817f296bb3SBarry Smith\dot{u} = G(u,t). 8827f296bb3SBarry Smith$$ 8837f296bb3SBarry Smith 8847f296bb3SBarry SmithThe user can either call `TSSetRHSFunction()` and/or they can call 8857f296bb3SBarry Smith`TSSetIFunction()` (so long as the function provided to 8867f296bb3SBarry Smith`TSSetIFunction()` is equivalent to $\dot{u} + \tilde{F}(t,u)$) 8877f296bb3SBarry Smithbut the Jacobians need not be provided. [^id6] 8887f296bb3SBarry Smith 8897f296bb3SBarry SmithThe Explicit Runge-Kutta timestepper with variable timesteps is an 8907f296bb3SBarry Smithimplementation of the standard Runge-Kutta with an embedded method. The 8917f296bb3SBarry Smitherror in each timestep is calculated using the solutions from the 8927f296bb3SBarry SmithRunge-Kutta method and its embedded method (the 2-norm of the difference 8937f296bb3SBarry Smithis used). The default method is the $3$rd-order Bogacki-Shampine 8947f296bb3SBarry Smithmethod with a $2$nd-order embedded method (`TSRK3BS`). Other 8957f296bb3SBarry Smithavailable methods are the $5$th-order Fehlberg RK scheme with a 8967f296bb3SBarry Smith$4$th-order embedded method (`TSRK5F`), the 8977f296bb3SBarry Smith$5$th-order Dormand-Prince RK scheme with a $4$th-order 8987f296bb3SBarry Smithembedded method (`TSRK5DP`), the $5$th-order Bogacki-Shampine 8997f296bb3SBarry SmithRK scheme with a $4$th-order embedded method (`TSRK5BS`, and 9007f296bb3SBarry Smiththe $6$th-, $7$th, and $8$th-order robust Verner 9017f296bb3SBarry SmithRK schemes with a $5$th-, $6$th, and $7$th-order 9027f296bb3SBarry Smithembedded method, respectively (`TSRK6VR`, `TSRK7VR`, `TSRK8VR`). 9037f296bb3SBarry SmithVariable timesteps cannot be used with RK schemes that do not have an 9047f296bb3SBarry Smithembedded method (`TSRK1FE` - $1$st-order, $1$-stage 9057f296bb3SBarry Smithforward Euler, `TSRK2A` - $2$nd-order, $2$-stage RK 9067f296bb3SBarry Smithscheme, `TSRK3` - $3$rd-order, $3$-stage RK scheme, 9077f296bb3SBarry Smith`TSRK4` - $4$-th order, $4$-stage RK scheme). 9087f296bb3SBarry Smith 9097f296bb3SBarry Smith## Special Cases 9107f296bb3SBarry Smith 9117f296bb3SBarry Smith- $\dot{u} = A u.$ First compute the matrix $A$ then call 9127f296bb3SBarry Smith 9137f296bb3SBarry Smith ``` 9147f296bb3SBarry Smith TSSetProblemType(ts,TS_LINEAR); 9157f296bb3SBarry Smith TSSetRHSFunction(ts,NULL,TSComputeRHSFunctionLinear,NULL); 9167f296bb3SBarry Smith TSSetRHSJacobian(ts,A,A,TSComputeRHSJacobianConstant,NULL); 9177f296bb3SBarry Smith ``` 9187f296bb3SBarry Smith 9197f296bb3SBarry Smith or 9207f296bb3SBarry Smith 9217f296bb3SBarry Smith ``` 9227f296bb3SBarry Smith TSSetProblemType(ts,TS_LINEAR); 9237f296bb3SBarry Smith TSSetIFunction(ts,NULL,TSComputeIFunctionLinear,NULL); 9247f296bb3SBarry Smith TSSetIJacobian(ts,A,A,TSComputeIJacobianConstant,NULL); 9257f296bb3SBarry Smith ``` 9267f296bb3SBarry Smith 9277f296bb3SBarry Smith- $\dot{u} = A(t) u.$ Use 9287f296bb3SBarry Smith 9297f296bb3SBarry Smith ``` 9307f296bb3SBarry Smith TSSetProblemType(ts,TS_LINEAR); 9317f296bb3SBarry Smith TSSetRHSFunction(ts,NULL,TSComputeRHSFunctionLinear,NULL); 9327f296bb3SBarry Smith TSSetRHSJacobian(ts,A,A,YourComputeRHSJacobian, &appctx); 9337f296bb3SBarry Smith ``` 9347f296bb3SBarry Smith 9357f296bb3SBarry Smith where `YourComputeRHSJacobian()` is a function you provide that 9367f296bb3SBarry Smith computes $A$ as a function of time. Or use 9377f296bb3SBarry Smith 9387f296bb3SBarry Smith ``` 9397f296bb3SBarry Smith TSSetProblemType(ts,TS_LINEAR); 9407f296bb3SBarry Smith TSSetIFunction(ts,NULL,TSComputeIFunctionLinear,NULL); 9417f296bb3SBarry Smith TSSetIJacobian(ts,A,A,YourComputeIJacobian, &appctx); 9427f296bb3SBarry Smith ``` 9437f296bb3SBarry Smith 9447f296bb3SBarry Smith## Monitoring and visualizing solutions 9457f296bb3SBarry Smith 9467f296bb3SBarry Smith- `-ts_monitor` - prints the time and timestep at each iteration. 9477f296bb3SBarry Smith- `-ts_adapt_monitor` - prints information about the timestep 9487f296bb3SBarry Smith adaption calculation at each iteration. 9497f296bb3SBarry Smith- `-ts_monitor_lg_timestep` - plots the size of each timestep, 9507f296bb3SBarry Smith `TSMonitorLGTimeStep()`. 9517f296bb3SBarry Smith- `-ts_monitor_lg_solution` - for ODEs with only a few components 9527f296bb3SBarry Smith (not arising from the discretization of a PDE) plots the solution as 9537f296bb3SBarry Smith a function of time, `TSMonitorLGSolution()`. 9547f296bb3SBarry Smith- `-ts_monitor_lg_error` - for ODEs with only a few components plots 9557f296bb3SBarry Smith the error as a function of time, only if `TSSetSolutionFunction()` 9567f296bb3SBarry Smith is provided, `TSMonitorLGError()`. 9577f296bb3SBarry Smith- `-ts_monitor_draw_solution` - plots the solution at each iteration, 9587f296bb3SBarry Smith `TSMonitorDrawSolution()`. 9597f296bb3SBarry Smith- `-ts_monitor_draw_error` - plots the error at each iteration only 9607f296bb3SBarry Smith if `TSSetSolutionFunction()` is provided, 9617f296bb3SBarry Smith `TSMonitorDrawSolution()`. 9627f296bb3SBarry Smith- `-ts_monitor_solution binary[:filename]` - saves the solution at each 9637f296bb3SBarry Smith iteration to a binary file, `TSMonitorSolution()`. Solution viewers work 9647f296bb3SBarry Smith with other time-aware formats, e.g., `-ts_monitor_solution cgns:sol.cgns`, 9657f296bb3SBarry Smith and can output one solution every 10 time steps by adding 9667f296bb3SBarry Smith `-ts_monitor_solution_interval 10`. Use `-ts_monitor_solution_interval -1` 9677f296bb3SBarry Smith to output data only at then end of a time loop. 9687f296bb3SBarry Smith- `-ts_monitor_solution_vtk <filename-%03D.vts>` - saves the solution 9697f296bb3SBarry Smith at each iteration to a file in vtk format, 9707f296bb3SBarry Smith `TSMonitorSolutionVTK()`. 9717f296bb3SBarry Smith 972efa39862SBarry Smith 973efa39862SBarry Smith(sec_ts_error_control)= 974efa39862SBarry Smith 9757f296bb3SBarry Smith## Error control via variable time-stepping 9767f296bb3SBarry Smith 9777f296bb3SBarry SmithMost of the time stepping methods available in PETSc have an error 9787f296bb3SBarry Smithestimation and error control mechanism. This mechanism is implemented by 9797f296bb3SBarry Smithchanging the step size in order to maintain user specified absolute and 9807f296bb3SBarry Smithrelative tolerances. The PETSc object responsible with error control is 9817f296bb3SBarry Smith`TSAdapt`. The available `TSAdapt` types are listed in the following table. 9827f296bb3SBarry Smith 9837f296bb3SBarry Smith```{eval-rst} 9847f296bb3SBarry Smith.. list-table:: ``TSAdapt``: available adaptors 9857f296bb3SBarry Smith :name: tab_adaptors 9867f296bb3SBarry Smith :header-rows: 1 9877f296bb3SBarry Smith 9887f296bb3SBarry Smith * - ID 9897f296bb3SBarry Smith - Name 9907f296bb3SBarry Smith - Notes 9917f296bb3SBarry Smith * - ``TSADAPTNONE`` 9927f296bb3SBarry Smith - ``none`` 9937f296bb3SBarry Smith - no adaptivity 9947f296bb3SBarry Smith * - ``TSADAPTBASIC`` 9957f296bb3SBarry Smith - ``basic`` 9967f296bb3SBarry Smith - the default adaptor 9977f296bb3SBarry Smith * - ``TSADAPTGLEE`` 9987f296bb3SBarry Smith - ``glee`` 9997f296bb3SBarry Smith - extension of the basic adaptor to treat :math:`{\rm Tol}_{\rm A}` and :math:`{\rm Tol}_{\rm R}` as separate criteria. It can also control global errors if the integrator (e.g., ``TSGLEE``) provides this information 10007f296bb3SBarry Smith * - ``TSADAPTDSP`` 10017f296bb3SBarry Smith - ``dsp`` 10027f296bb3SBarry Smith - adaptive controller for time-stepping based on digital signal processing 10037f296bb3SBarry Smith``` 10047f296bb3SBarry Smith 10057f296bb3SBarry SmithWhen using `TSADAPTBASIC` (the default), the user typically provides a 10067f296bb3SBarry Smithdesired absolute ${\rm Tol}_{\rm A}$ or a relative 10077f296bb3SBarry Smith${\rm Tol}_{\rm R}$ error tolerance by invoking 10087f296bb3SBarry Smith`TSSetTolerances()` or at the command line with options `-ts_atol` 10097f296bb3SBarry Smithand `-ts_rtol`. The error estimate is based on the local truncation 10107f296bb3SBarry Smitherror, so for every step the algorithm verifies that the estimated local 10117f296bb3SBarry Smithtruncation error satisfies the tolerances provided by the user and 10127f296bb3SBarry Smithcomputes a new step size to be taken. For multistage methods, the local 10137f296bb3SBarry Smithtruncation is obtained by comparing the solution $y$ to a lower 10147f296bb3SBarry Smithorder $\widehat{p}=p-1$ approximation, $\widehat{y}$, where 10157f296bb3SBarry Smith$p$ is the order of the method and $\widehat{p}$ the order 10167f296bb3SBarry Smithof $\widehat{y}$. 10177f296bb3SBarry Smith 10187f296bb3SBarry SmithThe adaptive controller at step $n$ computes a tolerance level 10197f296bb3SBarry Smith 10207f296bb3SBarry Smith$$ 10217f296bb3SBarry Smith\begin{aligned} 10227f296bb3SBarry SmithTol_n(i)&=&{\rm Tol}_{\rm A}(i) + \max(y_n(i),\widehat{y}_n(i)) {\rm Tol}_{\rm R}(i)\,,\end{aligned} 10237f296bb3SBarry Smith$$ 10247f296bb3SBarry Smith 10257f296bb3SBarry Smithand forms the acceptable error level 10267f296bb3SBarry Smith 10277f296bb3SBarry Smith$$ 10287f296bb3SBarry Smith\begin{aligned} 10297f296bb3SBarry Smith\rm wlte_n&=& \frac{1}{m} \sum_{i=1}^{m}\sqrt{\frac{\left\|y_n(i) 10307f296bb3SBarry Smith -\widehat{y}_n(i)\right\|}{Tol(i)}}\,,\end{aligned} 10317f296bb3SBarry Smith$$ 10327f296bb3SBarry Smith 10337f296bb3SBarry Smithwhere the errors are computed componentwise, $m$ is the dimension 10347f296bb3SBarry Smithof $y$ and `-ts_adapt_wnormtype` is `2` (default). If 10357f296bb3SBarry Smith`-ts_adapt_wnormtype` is `infinity` (max norm), then 10367f296bb3SBarry Smith 10377f296bb3SBarry Smith$$ 10387f296bb3SBarry Smith\begin{aligned} 10397f296bb3SBarry Smith\rm wlte_n&=& \max_{1\dots m}\frac{\left\|y_n(i) 10407f296bb3SBarry Smith -\widehat{y}_n(i)\right\|}{Tol(i)}\,.\end{aligned} 10417f296bb3SBarry Smith$$ 10427f296bb3SBarry Smith 10437f296bb3SBarry SmithThe error tolerances are satisfied when $\rm wlte\le 1.0$. 10447f296bb3SBarry Smith 10457f296bb3SBarry SmithThe next step size is based on this error estimate, and determined by 10467f296bb3SBarry Smith 10477f296bb3SBarry Smith$$ 10487f296bb3SBarry Smith\begin{aligned} 10497f296bb3SBarry Smith \Delta t_{\rm new}(t)&=&\Delta t_{\rm{old}} \min(\alpha_{\max}, 10507f296bb3SBarry Smith \max(\alpha_{\min}, \beta (1/\rm wlte)^\frac{1}{\widehat{p}+1}))\,,\end{aligned} 10517f296bb3SBarry Smith$$ (hnew) 10527f296bb3SBarry Smith 10537f296bb3SBarry Smithwhere $\alpha_{\min}=$`-ts_adapt_clip`[0] and 10547f296bb3SBarry Smith$\alpha_{\max}$=`-ts_adapt_clip`[1] keep the change in 10557f296bb3SBarry Smith$\Delta t$ to within a certain factor, and $\beta<1$ is 10567f296bb3SBarry Smithchosen through `-ts_adapt_safety` so that there is some margin to 10577f296bb3SBarry Smithwhich the tolerances are satisfied and so that the probability of 10587f296bb3SBarry Smithrejection is decreased. 10597f296bb3SBarry Smith 10607f296bb3SBarry SmithThis adaptive controller works in the following way. After completing 10617f296bb3SBarry Smithstep $k$, if $\rm wlte_{k+1} \le 1.0$, then the step is 10627f296bb3SBarry Smithaccepted and the next step is modified according to 10637f296bb3SBarry Smith{eq}`hnew`; otherwise, the step is rejected and retaken 10647f296bb3SBarry Smithwith the step length computed in {eq}`hnew`. 10657f296bb3SBarry Smith 10667f296bb3SBarry Smith## Handling of discontinuities 10677f296bb3SBarry Smith 10687f296bb3SBarry SmithFor problems that involve discontinuous right-hand sides, one can set an 10697f296bb3SBarry Smith“event” function $g(t,u)$ for PETSc to detect and locate the times 10707f296bb3SBarry Smithof discontinuities (zeros of $g(t,u)$). Events can be defined 10717f296bb3SBarry Smiththrough the event monitoring routine 10727f296bb3SBarry Smith 10737f296bb3SBarry Smith``` 1074*2a8381b2SBarry SmithTSSetEventHandler(TS ts,PetscInt nevents,PetscInt *direction,PetscBool *terminate,PetscErrorCode (*indicator)(TS,PetscReal,Vec,PetscScalar*,PetscCtx eventP),PetscErrorCode (*postevent)(TS,PetscInt,PetscInt[],PetscReal,Vec,PetscBool,PetscCtx eventP),PetscCtxeventP); 10757f296bb3SBarry Smith``` 10767f296bb3SBarry Smith 10777f296bb3SBarry SmithHere, `nevents` denotes the number of events, `direction` sets the 10787f296bb3SBarry Smithtype of zero crossing to be detected for an event (+1 for positive 10797f296bb3SBarry Smithzero-crossing, -1 for negative zero-crossing, and 0 for both), 10807f296bb3SBarry Smith`terminate` conveys whether the time-stepping should continue or halt 10817f296bb3SBarry Smithwhen an event is located, `eventmonitor` is a user- defined routine 10827f296bb3SBarry Smiththat specifies the event description, `postevent` is an optional 10837f296bb3SBarry Smithuser-defined routine to take specific actions following an event. 10847f296bb3SBarry Smith 10857f296bb3SBarry SmithThe arguments to `indicator()` are the timestep context, current 10867f296bb3SBarry Smithtime, input state $u$, array of event function value, and the 10877f296bb3SBarry Smith(optional) user-provided context `eventP`. 10887f296bb3SBarry Smith 10897f296bb3SBarry SmithThe arguments to `postevent()` routine are the timestep context, 10907f296bb3SBarry Smithnumber of events occurred, indices of events occurred, current time, input 10917f296bb3SBarry Smithstate $u$, a boolean flag indicating forward solve (1) or adjoint 10927f296bb3SBarry Smithsolve (0), and the (optional) user-provided context `eventP`. 10937f296bb3SBarry Smith 10947f296bb3SBarry Smith(sec_tchem)= 10957f296bb3SBarry Smith 10967f296bb3SBarry Smith## Explicit integrators with finite element mass matrices 10977f296bb3SBarry Smith 10987f296bb3SBarry SmithDiscretized finite element problems often have the form $M \dot u = G(t, u)$ where $M$ is the mass matrix. 10997f296bb3SBarry SmithSuch problems can be solved using `DMTSSetIFunction()` with implicit integrators. 11007f296bb3SBarry SmithWhen $M$ is nonsingular (i.e., the problem is an ODE, not a DAE), explicit integrators can be applied to $\dot u = M^{-1} G(t, u)$ or $\dot u = \hat M^{-1} G(t, u)$, where $\hat M$ is the lumped mass matrix. 11017f296bb3SBarry SmithWhile the true mass matrix generally has a dense inverse and thus must be solved iteratively, the lumped mass matrix is diagonal (e.g., computed via collocated quadrature or row sums of $M$). 11027f296bb3SBarry SmithTo have PETSc create and apply a (lumped) mass matrix automatically, first use `DMTSSetRHSFunction()` to specify $G$ and set a `PetscFE` using `DMAddField()` and `DMCreateDS()`, then call either `DMTSCreateRHSMassMatrix()` or `DMTSCreateRHSMassMatrixLumped()` to automatically create the mass matrix and a `KSP` that will be used to apply $M^{-1}$. 11037f296bb3SBarry SmithThis `KSP` can be customized using the `"mass_"` prefix. 11047f296bb3SBarry Smith 11057f296bb3SBarry Smith(section_sa)= 11067f296bb3SBarry Smith 11077f296bb3SBarry Smith## Performing sensitivity analysis with the TS ODE Solvers 11087f296bb3SBarry Smith 11097f296bb3SBarry SmithThe `TS` library provides a framework based on discrete adjoint models 11107f296bb3SBarry Smithfor sensitivity analysis for ODEs and DAEs. The ODE/DAE solution process 11117f296bb3SBarry Smith(henceforth called the forward run) can be obtained by using either 11127f296bb3SBarry Smithexplicit or implicit solvers in `TS`, depending on the problem 11137f296bb3SBarry Smithproperties. Currently supported method types are `TSRK` (Runge-Kutta) 11147f296bb3SBarry Smithexplicit methods and `TSTHETA` implicit methods, which include 11157f296bb3SBarry Smith`TSBEULER` and `TSCN`. 11167f296bb3SBarry Smith 11177f296bb3SBarry Smith### Using the discrete adjoint methods 11187f296bb3SBarry Smith 11197f296bb3SBarry SmithConsider the ODE/DAE 11207f296bb3SBarry Smith 11217f296bb3SBarry Smith$$ 11227f296bb3SBarry SmithF(t,y,\dot{y},p) = 0, \quad y(t_0)=y_0(p) \quad t_0 \le t \le t_F 11237f296bb3SBarry Smith$$ 11247f296bb3SBarry Smith 11257f296bb3SBarry Smithand the cost function(s) 11267f296bb3SBarry Smith 11277f296bb3SBarry Smith$$ 11287f296bb3SBarry Smith\Psi_i(y_0,p) = \Phi_i(y_F,p) + \int_{t_0}^{t_F} r_i(y(t),p,t)dt \quad i=1,...,n_\text{cost}. 11297f296bb3SBarry Smith$$ 11307f296bb3SBarry Smith 11317f296bb3SBarry SmithThe `TSAdjoint` routines of PETSc provide 11327f296bb3SBarry Smith 11337f296bb3SBarry Smith$$ 11347f296bb3SBarry Smith\frac{\partial \Psi_i}{\partial y_0} = \lambda_i 11357f296bb3SBarry Smith$$ 11367f296bb3SBarry Smith 11377f296bb3SBarry Smithand 11387f296bb3SBarry Smith 11397f296bb3SBarry Smith$$ 11407f296bb3SBarry Smith\frac{\partial \Psi_i}{\partial p} = \mu_i + \lambda_i (\frac{\partial y_0}{\partial p}). 11417f296bb3SBarry Smith$$ 11427f296bb3SBarry Smith 11437f296bb3SBarry SmithTo perform the discrete adjoint sensitivity analysis one first sets up 11447f296bb3SBarry Smiththe `TS` object for a regular forward run but with one extra function 11457f296bb3SBarry Smithcall 11467f296bb3SBarry Smith 11477f296bb3SBarry Smith``` 11487f296bb3SBarry SmithTSSetSaveTrajectory(TS ts), 11497f296bb3SBarry Smith``` 11507f296bb3SBarry Smith 11517f296bb3SBarry Smiththen calls `TSSolve()` in the usual manner. 11527f296bb3SBarry Smith 11537f296bb3SBarry SmithOne must create two arrays of $n_\text{cost}$ vectors 11547f296bb3SBarry Smith$\lambda$ and $\mu$ (if there are no parameters $p$ 11557f296bb3SBarry Smiththen one can use `NULL` for the $\mu$ array.) The 11567f296bb3SBarry Smith$\lambda$ vectors are the same dimension and parallel layout as 11577f296bb3SBarry Smiththe solution vector for the ODE, the $\mu$ vectors are of dimension 11587f296bb3SBarry Smith$p$; when $p$ is small usually all its elements are on the 11597f296bb3SBarry Smithfirst MPI process, while the vectors have no entries on the other 11607f296bb3SBarry Smithprocesses. $\lambda_i$ and $\mu_i$ should be initialized with 11617f296bb3SBarry Smiththe values $d\Phi_i/dy|_{t=t_F}$ and $d\Phi_i/dp|_{t=t_F}$ 11627f296bb3SBarry Smithrespectively. Then one calls 11637f296bb3SBarry Smith 11647f296bb3SBarry Smith``` 11657f296bb3SBarry SmithTSSetCostGradients(TS ts,PetscInt numcost, Vec *lambda,Vec *mu); 11667f296bb3SBarry Smith``` 11677f296bb3SBarry Smith 11687f296bb3SBarry Smithwhere `numcost` denotes $n_\text{cost}$. 11697f296bb3SBarry SmithIf $F()$ is a function of $p$ one needs to also provide the 11707f296bb3SBarry SmithJacobian $-F_p$ with 11717f296bb3SBarry Smith 11727f296bb3SBarry Smith``` 1173*2a8381b2SBarry SmithTSSetRHSJacobianP(TS ts,Mat Amat,PetscErrorCode (*fp)(TS,PetscReal,Vec,Mat,PetscCtx),PetscCtx ctx) 11747f296bb3SBarry Smith``` 11757f296bb3SBarry Smith 11767f296bb3SBarry Smithor 11777f296bb3SBarry Smith 11787f296bb3SBarry Smith``` 1179*2a8381b2SBarry SmithTSSetIJacobianP(TS ts,Mat Amat,PetscErrorCode (*fp)(TS,PetscReal,Vec,Vec,PetscReal,Mat,PetscCtx),PetscCtx ctx) 11807f296bb3SBarry Smith``` 11817f296bb3SBarry Smith 11827f296bb3SBarry Smithor both, depending on which form is used to define the ODE. 11837f296bb3SBarry Smith 11847f296bb3SBarry SmithThe arguments for the function `fp()` are the timestep context, 11857f296bb3SBarry Smithcurrent time, $y$, and the (optional) user-provided context. 11867f296bb3SBarry Smith 11877f296bb3SBarry SmithIf there is an integral term in the cost function, i.e. $r$ is 11887f296bb3SBarry Smithnonzero, it can be transformed into another ODE that is augmented to the 11897f296bb3SBarry Smithoriginal ODE. To evaluate the integral, one needs to create a child 11907f296bb3SBarry Smith`TS` objective by calling 11917f296bb3SBarry Smith 11927f296bb3SBarry Smith``` 11937f296bb3SBarry SmithTSCreateQuadratureTS(TS ts,PetscBool fwd,TS *quadts); 11947f296bb3SBarry Smith``` 11957f296bb3SBarry Smith 11967f296bb3SBarry Smithand provide the ODE RHS function (which evaluates the integrand 11977f296bb3SBarry Smith$r$) with 11987f296bb3SBarry Smith 11997f296bb3SBarry Smith``` 1200*2a8381b2SBarry SmithTSSetRHSFunction(TS quadts,Vec R,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,PetscCtx),PetscCtxctx) 12017f296bb3SBarry Smith``` 12027f296bb3SBarry Smith 12037f296bb3SBarry SmithSimilar to the settings for the original ODE, Jacobians of the integrand 12047f296bb3SBarry Smithcan be provided with 12057f296bb3SBarry Smith 12067f296bb3SBarry Smith``` 1207*2a8381b2SBarry SmithTSSetRHSJacobian(TS quadts,Vec DRDU,Vec DRDU,PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,PetscCtx),PetscCtxctx) 1208*2a8381b2SBarry SmithTSSetRHSJacobianP(TS quadts,Vec DRDU,Vec DRDU,PetscErrorCode (*drdyp)(TS,PetscReal,Vec,Vec*,PetscCtx),PetscCtxctx) 12097f296bb3SBarry Smith``` 12107f296bb3SBarry Smith 12117f296bb3SBarry Smithwhere $\mathrm{drdyf}= dr /dy$, $\mathrm{drdpf} = dr /dp$. 12127f296bb3SBarry SmithSince the integral term is additive to the cost function, its gradient 12137f296bb3SBarry Smithinformation will be included in $\lambda$ and $\mu$. 12147f296bb3SBarry Smith 12157f296bb3SBarry SmithLastly, one starts the backward run by calling 12167f296bb3SBarry Smith 12177f296bb3SBarry Smith``` 12187f296bb3SBarry SmithTSAdjointSolve(TS ts). 12197f296bb3SBarry Smith``` 12207f296bb3SBarry Smith 12217f296bb3SBarry SmithOne can obtain the value of the integral term by calling 12227f296bb3SBarry Smith 12237f296bb3SBarry Smith``` 12247f296bb3SBarry SmithTSGetCostIntegral(TS ts,Vec *q). 12257f296bb3SBarry Smith``` 12267f296bb3SBarry Smith 12277f296bb3SBarry Smithor accessing directly the solution vector used by `quadts`. 12287f296bb3SBarry Smith 12297f296bb3SBarry SmithThe second argument of `TSCreateQuadratureTS()` allows one to choose 12307f296bb3SBarry Smithif the integral term is evaluated in the forward run (inside 12317f296bb3SBarry Smith`TSSolve()`) or in the backward run (inside `TSAdjointSolve()`) when 12327f296bb3SBarry Smith`TSSetCostGradients()` and `TSSetCostIntegrand()` are called before 12337f296bb3SBarry Smith`TSSolve()`. Note that this also allows for evaluating the integral 12347f296bb3SBarry Smithwithout having to use the adjoint solvers. 12357f296bb3SBarry Smith 12367f296bb3SBarry SmithTo provide a better understanding of the use of the adjoint solvers, we 12377f296bb3SBarry Smithintroduce a simple example, corresponding to 12387f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/power_grid/ex3sa.c.html">TS Power Grid Tutorial ex3sa</a>. 12397f296bb3SBarry SmithThe problem is to study dynamic security of power system when there are 12407f296bb3SBarry Smithcredible contingencies such as short-circuits or loss of generators, 12417f296bb3SBarry Smithtransmission lines, or loads. The dynamic security constraints are 12427f296bb3SBarry Smithincorporated as equality constraints in the form of discretized 12437f296bb3SBarry Smithdifferential equations and inequality constraints for bounds on the 12447f296bb3SBarry Smithtrajectory. The governing ODE system is 12457f296bb3SBarry Smith 12467f296bb3SBarry Smith$$ 12477f296bb3SBarry Smith\begin{aligned} 12487f296bb3SBarry Smith \phi' &= &\omega_B (\omega - \omega_S) \\ 12497f296bb3SBarry Smith 2H/\omega_S \, \omega' & =& p_m - p_{max} sin(\phi) -D (\omega - \omega_S), \quad t_0 \leq t \leq t_F,\end{aligned} 12507f296bb3SBarry Smith$$ 12517f296bb3SBarry Smith 12527f296bb3SBarry Smithwhere $\phi$ is the phase angle and $\omega$ is the 12537f296bb3SBarry Smithfrequency. 12547f296bb3SBarry Smith 12557f296bb3SBarry SmithThe initial conditions at time $t_0$ are 12567f296bb3SBarry Smith 12577f296bb3SBarry Smith$$ 12587f296bb3SBarry Smith\begin{aligned} 12597f296bb3SBarry Smith\phi(t_0) &=& \arcsin \left( p_m / p_{max} \right), \\ 12607f296bb3SBarry Smithw(t_0) & =& 1.\end{aligned} 12617f296bb3SBarry Smith$$ 12627f296bb3SBarry Smith 12637f296bb3SBarry Smith$p_{max}$ is a positive number when the system operates normally. 12647f296bb3SBarry SmithAt an event such as fault incidence/removal, $p_{max}$ will change 12657f296bb3SBarry Smithto $0$ temporarily and back to the original value after the fault 12667f296bb3SBarry Smithis fixed. The objective is to maximize $p_m$ subject to the above 12677f296bb3SBarry SmithODE constraints and $\phi<\phi_S$ during all times. To accommodate 12687f296bb3SBarry Smiththe inequality constraint, we want to compute the sensitivity of the 12697f296bb3SBarry Smithcost function 12707f296bb3SBarry Smith 12717f296bb3SBarry Smith$$ 12727f296bb3SBarry Smith\Psi(p_m,\phi) = -p_m + c \int_{t_0}^{t_F} \left( \max(0, \phi - \phi_S ) \right)^2 dt 12737f296bb3SBarry Smith$$ 12747f296bb3SBarry Smith 12757f296bb3SBarry Smithwith respect to the parameter $p_m$. $numcost$ is $1$ 12767f296bb3SBarry Smithsince it is a scalar function. 12777f296bb3SBarry Smith 12787f296bb3SBarry SmithFor ODE solution, PETSc requires user-provided functions to evaluate the 12797f296bb3SBarry Smithsystem $F(t,y,\dot{y},p)$ (set by `TSSetIFunction()` ) and its 12807f296bb3SBarry Smithcorresponding Jacobian $F_y + \sigma F_{\dot y}$ (set by 12817f296bb3SBarry Smith`TSSetIJacobian()`). Note that the solution state $y$ is 12827f296bb3SBarry Smith$[ \phi \; \omega ]^T$ here. For sensitivity analysis, we need to 12837f296bb3SBarry Smithprovide a routine to compute $\mathrm{f}_p=[0 \; 1]^T$ using 12847f296bb3SBarry Smith`TSASetRHSJacobianP()`, and three routines corresponding to the 12857f296bb3SBarry Smithintegrand $r=c \left( \max(0, \phi - \phi_S ) \right)^2$, 12867f296bb3SBarry Smith$r_p = [0 \; 0]^T$ and 12877f296bb3SBarry Smith$r_y= [ 2 c \left( \max(0, \phi - \phi_S ) \right) \; 0]^T$ using 12887f296bb3SBarry Smith`TSSetCostIntegrand()`. 12897f296bb3SBarry Smith 12907f296bb3SBarry SmithIn the adjoint run, $\lambda$ and $\mu$ are initialized as 12917f296bb3SBarry Smith$[ 0 \; 0 ]^T$ and $[-1]$ at the final time $t_F$. 12927f296bb3SBarry SmithAfter `TSAdjointSolve()`, the sensitivity of the cost function w.r.t. 12937f296bb3SBarry Smithinitial conditions is given by the sensitivity variable $\lambda$ 12947f296bb3SBarry Smith(at time $t_0$) directly. And the sensitivity of the cost function 12957f296bb3SBarry Smithw.r.t. the parameter $p_m$ can be computed (by users) as 12967f296bb3SBarry Smith 12977f296bb3SBarry Smith$$ 12987f296bb3SBarry Smith\frac{\mathrm{d} \Psi}{\mathrm{d} p_m} = \mu(t_0) + \lambda(t_0) \frac{\mathrm{d} \left[ \phi(t_0) \; \omega(t_0) \right]^T}{\mathrm{d} p_m} . 12997f296bb3SBarry Smith$$ 13007f296bb3SBarry Smith 13017f296bb3SBarry SmithFor explicit methods where one does not need to provide the Jacobian 13027f296bb3SBarry Smith$F_u$ for the forward solve one still does need it for the 13037f296bb3SBarry Smithbackward solve and thus must call 13047f296bb3SBarry Smith 13057f296bb3SBarry Smith``` 1306*2a8381b2SBarry SmithTSSetRHSJacobian(TS ts,Mat Amat, Mat Pmat,PetscErrorCode (*f)(TS,PetscReal,Vec,Mat,Mat,PetscCtx),PetscCtxfP); 13077f296bb3SBarry Smith``` 13087f296bb3SBarry Smith 13097f296bb3SBarry SmithExamples include: 13107f296bb3SBarry Smith 13117f296bb3SBarry Smith- discrete adjoint sensitivity using explicit and implicit time stepping methods for an ODE problem 13127f296bb3SBarry Smith <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex20adj.c.html">TS Tutorial ex20adj</a>, 13137f296bb3SBarry Smith- an optimization problem using the discrete adjoint models of the ERK (for nonstiff ODEs) 13147f296bb3SBarry Smith and the Theta methods (for stiff DAEs) 13157f296bb3SBarry Smith <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex20opt_ic.c.html">TS Tutorial ex20opt_ic</a> 13167f296bb3SBarry Smith and 13177f296bb3SBarry Smith <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex20opt_p.c.html">TS Tutorial ex20opt_p</a>, 13187f296bb3SBarry Smith- an ODE-constrained optimization using the discrete adjoint models of the 13197f296bb3SBarry Smith Theta methods for cost function with an integral term 13207f296bb3SBarry Smith <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/power_grid/ex3opt.c.html">TS Power Grid Tutorial ex3opt</a>, 13217f296bb3SBarry Smith- discrete adjoint sensitivity using the Crank-Nicolson methods for DAEs with discontinuities 13227f296bb3SBarry Smith <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/power_grid/stability_9bus/ex9busadj.c.html">TS Power Grid Stability Tutorial ex9busadj</a>, 13237f296bb3SBarry Smith- a DAE-constrained optimization problem using the discrete adjoint models of the Crank-Nicolson 13247f296bb3SBarry Smith methods for cost function with an integral term 13257f296bb3SBarry Smith <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/power_grid/stability_9bus/ex9busopt.c.html">TS Power Grid Tutorial ex9busopt</a>, 13267f296bb3SBarry Smith- discrete adjoint sensitivity using the Crank-Nicolson methods for a PDE problem 13277f296bb3SBarry Smith <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/advection-diffusion-reaction/ex5adj.c.html">TS Advection-Diffusion-Reaction Tutorial ex5adj</a>. 13287f296bb3SBarry Smith 13297f296bb3SBarry Smith### Checkpointing 13307f296bb3SBarry Smith 13317f296bb3SBarry SmithThe discrete adjoint model requires the states (and stage values in the 13327f296bb3SBarry Smithcontext of multistage timestepping methods) to evaluate the Jacobian 13337f296bb3SBarry Smithmatrices during the adjoint (backward) run. By default, PETSc stores the 13347f296bb3SBarry Smithwhole trajectory to disk as binary files, each of which contains the 13357f296bb3SBarry Smithinformation for a single time step including state, time, and stage 13367f296bb3SBarry Smithvalues (optional). One can also make PETSc store the trajectory to 13377f296bb3SBarry Smithmemory with the option `-ts_trajectory_type memory`. However, there 13387f296bb3SBarry Smithmight not be sufficient memory capacity especially for large-scale 13397f296bb3SBarry Smithproblems and long-time integration. 13407f296bb3SBarry Smith 13417f296bb3SBarry SmithA so-called checkpointing scheme is needed to solve this problem. The 13427f296bb3SBarry Smithscheme stores checkpoints at selective time steps and recomputes the 13437f296bb3SBarry Smithmissing information. The `revolve` library is used by PETSc 13447f296bb3SBarry Smith`TSTrajectory` to generate an optimal checkpointing schedule that 13457f296bb3SBarry Smithminimizes the recomputations given a limited number of available 13467f296bb3SBarry Smithcheckpoints. One can specify the number of available checkpoints with 13477f296bb3SBarry Smiththe option 13487f296bb3SBarry Smith`-ts_trajectory_max_cps_ram [maximum number of checkpoints in RAM]`. 13497f296bb3SBarry SmithNote that one checkpoint corresponds to one time step. 13507f296bb3SBarry Smith 13517f296bb3SBarry SmithThe `revolve` library also provides an optimal multistage 13527f296bb3SBarry Smithcheckpointing scheme that uses both RAM and disk for storage. This 13537f296bb3SBarry Smithscheme is automatically chosen if one uses both the option 13547f296bb3SBarry Smith`-ts_trajectory_max_cps_ram [maximum number of checkpoints in RAM]` 13557f296bb3SBarry Smithand the option 13567f296bb3SBarry Smith`-ts_trajectory_max_cps_disk [maximum number of checkpoints on disk]`. 13577f296bb3SBarry Smith 13587f296bb3SBarry SmithSome other useful options are listed below. 13597f296bb3SBarry Smith 13607f296bb3SBarry Smith- `-ts_trajectory_view` prints the total number of recomputations, 13617f296bb3SBarry Smith- `-ts_monitor` and `-ts_adjoint_monitor` allow users to monitor 13627f296bb3SBarry Smith the progress of the adjoint work flow, 13637f296bb3SBarry Smith- `-ts_trajectory_type visualization` may be used to save the whole 13647f296bb3SBarry Smith trajectory for visualization. It stores the solution and the time, 13657f296bb3SBarry Smith but no stage values. The binary files generated can be read into 13667f296bb3SBarry Smith MATLAB via the script 13677f296bb3SBarry Smith `$PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m`. 13687f296bb3SBarry Smith 13697f296bb3SBarry Smith(sec_sundials)= 13707f296bb3SBarry Smith 13717f296bb3SBarry Smith## Using Sundials from PETSc 13727f296bb3SBarry Smith 13737f296bb3SBarry SmithSundials is a parallel ODE solver developed by Hindmarsh et al. at LLNL. 13747f296bb3SBarry SmithThe `TS` library provides an interface to use the CVODE component of 13757f296bb3SBarry SmithSundials directly from PETSc. (To configure PETSc to use Sundials, see 13767f296bb3SBarry Smiththe installation guide, `installation/index.htm`.) 13777f296bb3SBarry Smith 13787f296bb3SBarry SmithTo use the Sundials integrators, call 13797f296bb3SBarry Smith 13807f296bb3SBarry Smith``` 13817f296bb3SBarry SmithTSSetType(TS ts,TSType TSSUNDIALS); 13827f296bb3SBarry Smith``` 13837f296bb3SBarry Smith 13847f296bb3SBarry Smithor use the command line option `-ts_type` `sundials`. 13857f296bb3SBarry Smith 13867f296bb3SBarry SmithSundials’ CVODE solver comes with two main integrator families, Adams 13877f296bb3SBarry Smithand BDF (backward differentiation formula). One can select these with 13887f296bb3SBarry Smith 13897f296bb3SBarry Smith``` 13907f296bb3SBarry SmithTSSundialsSetType(TS ts,TSSundialsLmmType [SUNDIALS_ADAMS,SUNDIALS_BDF]); 13917f296bb3SBarry Smith``` 13927f296bb3SBarry Smith 13937f296bb3SBarry Smithor the command line option `-ts_sundials_type <adams,bdf>`. BDF is the 13947f296bb3SBarry Smithdefault. 13957f296bb3SBarry Smith 13967f296bb3SBarry SmithSundials does not use the `SNES` library within PETSc for its 13977f296bb3SBarry Smithnonlinear solvers, so one cannot change the nonlinear solver options via 13987f296bb3SBarry Smith`SNES`. Rather, Sundials uses the preconditioners within the `PC` 13997f296bb3SBarry Smithpackage of PETSc, which can be accessed via 14007f296bb3SBarry Smith 14017f296bb3SBarry Smith``` 14027f296bb3SBarry SmithTSSundialsGetPC(TS ts,PC *pc); 14037f296bb3SBarry Smith``` 14047f296bb3SBarry Smith 14057f296bb3SBarry SmithThe user can then directly set preconditioner options; alternatively, 14067f296bb3SBarry Smiththe usual runtime options can be employed via `-pc_xxx`. 14077f296bb3SBarry Smith 14087f296bb3SBarry SmithFinally, one can set the Sundials tolerances via 14097f296bb3SBarry Smith 14107f296bb3SBarry Smith``` 14117f296bb3SBarry SmithTSSundialsSetTolerance(TS ts,double abs,double rel); 14127f296bb3SBarry Smith``` 14137f296bb3SBarry Smith 14147f296bb3SBarry Smithwhere `abs` denotes the absolute tolerance and `rel` the relative 14157f296bb3SBarry Smithtolerance. 14167f296bb3SBarry Smith 14177f296bb3SBarry SmithOther PETSc-Sundials options include 14187f296bb3SBarry Smith 14197f296bb3SBarry Smith``` 14207f296bb3SBarry SmithTSSundialsSetGramSchmidtType(TS ts,TSSundialsGramSchmidtType type); 14217f296bb3SBarry Smith``` 14227f296bb3SBarry Smith 14237f296bb3SBarry Smithwhere `type` is either `SUNDIALS_MODIFIED_GS` or 14247f296bb3SBarry Smith`SUNDIALS_UNMODIFIED_GS`. This may be set via the options data base 14257f296bb3SBarry Smithwith `-ts_sundials_gramschmidt_type <modifed,unmodified>`. 14267f296bb3SBarry Smith 14277f296bb3SBarry SmithThe routine 14287f296bb3SBarry Smith 14297f296bb3SBarry Smith``` 14307f296bb3SBarry SmithTSSundialsSetMaxl(TS ts,PetscInt restart); 14317f296bb3SBarry Smith``` 14327f296bb3SBarry Smith 14337f296bb3SBarry Smithsets the number of vectors in the Krylov subpspace used by GMRES. This 14347f296bb3SBarry Smithmay be set in the options database with `-ts_sundials_maxl` `maxl`. 14357f296bb3SBarry Smith 14367f296bb3SBarry Smith## Using TChem from PETSc 14377f296bb3SBarry Smith 14387f296bb3SBarry SmithTChem [^id7] is a package originally developed at Sandia National 14397f296bb3SBarry SmithLaboratory that can read in CHEMKIN [^id8] data files and compute the 14407f296bb3SBarry Smithright-hand side function and its Jacobian for a reaction ODE system. To 14417f296bb3SBarry Smithutilize PETSc’s ODE solvers for these systems, first install PETSc with 14427f296bb3SBarry Smiththe additional `configure` option `--download-tchem`. We currently 14437f296bb3SBarry Smithprovide two examples of its use; one for single cell reaction and one 14447f296bb3SBarry Smithfor an “artificial” one dimensional problem with periodic boundary 14457f296bb3SBarry Smithconditions and diffusion of all species. The self-explanatory examples 14467f296bb3SBarry Smithare the 14477f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/extchem.c.html">The TS tutorial extchem</a> 14487f296bb3SBarry Smithand 14497f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/extchemfield.c.html">The TS tutorial extchemfield</a>. 14507f296bb3SBarry Smith 14517f296bb3SBarry Smith[^id5]: If the matrix $F_{\dot{u}}(t) = \partial F 14527f296bb3SBarry Smith / \partial \dot{u}$ is nonsingular then it is an ODE and can be 14537f296bb3SBarry Smith transformed to the standard explicit form, although this 14547f296bb3SBarry Smith transformation may not lead to efficient algorithms. 14557f296bb3SBarry Smith 14567f296bb3SBarry Smith[^id6]: PETSc will automatically translate the function provided to the 14577f296bb3SBarry Smith appropriate form. 14587f296bb3SBarry Smith 14597f296bb3SBarry Smith[^id7]: [bitbucket.org/jedbrown/tchem](https://bitbucket.org/jedbrown/tchem) 14607f296bb3SBarry Smith 14617f296bb3SBarry Smith[^id8]: [en.wikipedia.org/wiki/CHEMKIN](https://en.wikipedia.org/wiki/CHEMKIN) 14627f296bb3SBarry Smith 14637f296bb3SBarry Smith```{raw} html 14647f296bb3SBarry Smith<hr> 14657f296bb3SBarry Smith``` 14667f296bb3SBarry Smith 14677f296bb3SBarry Smith# Solving Steady-State Problems with Pseudo-Timestepping 14687f296bb3SBarry Smith 14697f296bb3SBarry Smith**Simple Example:** `TS` provides a general code for performing pseudo 14707f296bb3SBarry Smithtimestepping with a variable timestep at each physical node point. For 14717f296bb3SBarry Smithexample, instead of directly attacking the steady-state problem 14727f296bb3SBarry Smith 14737f296bb3SBarry Smith$$ 14747f296bb3SBarry SmithG(u) = 0, 14757f296bb3SBarry Smith$$ 14767f296bb3SBarry Smith 14777f296bb3SBarry Smithwe can use pseudo-transient continuation by solving 14787f296bb3SBarry Smith 14797f296bb3SBarry Smith$$ 14807f296bb3SBarry Smithu_t = G(u). 14817f296bb3SBarry Smith$$ 14827f296bb3SBarry Smith 14837f296bb3SBarry SmithUsing time differencing 14847f296bb3SBarry Smith 14857f296bb3SBarry Smith$$ 14867f296bb3SBarry Smithu_t \doteq \frac{{u^{n+1}} - {u^{n}} }{dt^{n}} 14877f296bb3SBarry Smith$$ 14887f296bb3SBarry Smith 14897f296bb3SBarry Smithwith the backward Euler method, we obtain nonlinear equations at a 14907f296bb3SBarry Smithseries of pseudo-timesteps 14917f296bb3SBarry Smith 14927f296bb3SBarry Smith$$ 14937f296bb3SBarry Smith\frac{1}{dt^n} B (u^{n+1} - u^{n} ) = G(u^{n+1}). 14947f296bb3SBarry Smith$$ 14957f296bb3SBarry Smith 14967f296bb3SBarry SmithFor this problem the user must provide $G(u)$, the time steps 14977f296bb3SBarry Smith$dt^{n}$ and the left-hand-side matrix $B$ (or optionally, 14987f296bb3SBarry Smithif the timestep is position independent and $B$ is the identity 14997f296bb3SBarry Smithmatrix, a scalar timestep), as well as optionally the Jacobian of 15007f296bb3SBarry Smith$G(u)$. 15017f296bb3SBarry Smith 15027f296bb3SBarry SmithMore generally, this can be applied to implicit ODE and DAE for which 15037f296bb3SBarry Smiththe transient form is 15047f296bb3SBarry Smith 15057f296bb3SBarry Smith$$ 15067f296bb3SBarry SmithF(u,\dot{u}) = 0. 15077f296bb3SBarry Smith$$ 15087f296bb3SBarry Smith 15097f296bb3SBarry SmithFor solving steady-state problems with pseudo-timestepping one proceeds 15107f296bb3SBarry Smithas follows. 15117f296bb3SBarry Smith 15127f296bb3SBarry Smith- Provide the function `G(u)` with the routine 15137f296bb3SBarry Smith 15147f296bb3SBarry Smith ``` 1515*2a8381b2SBarry Smith TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,PetscCtx),PetscCtxfP); 15167f296bb3SBarry Smith ``` 15177f296bb3SBarry Smith 15187f296bb3SBarry Smith The arguments to the function `f()` are the timestep context, the 15197f296bb3SBarry Smith current time, the input for the function, the output for the function 15207f296bb3SBarry Smith and the (optional) user-provided context variable `fP`. 15217f296bb3SBarry Smith 15227f296bb3SBarry Smith- Provide the (approximate) Jacobian matrix of `G(u)` and a function 15237f296bb3SBarry Smith to compute it at each Newton iteration. This is done with the command 15247f296bb3SBarry Smith 15257f296bb3SBarry Smith ``` 1526*2a8381b2SBarry Smith TSSetRHSJacobian(TS ts,Mat Amat, Mat Pmat,PetscErrorCode (*f)(TS,PetscReal,Vec,Mat,Mat,PetscCtx),PetscCtxfP); 15277f296bb3SBarry Smith ``` 15287f296bb3SBarry Smith 15297f296bb3SBarry Smith The arguments for the function `f()` are the timestep context, the 15307f296bb3SBarry Smith current time, the location where the Jacobian is to be computed, the 15317f296bb3SBarry Smith (approximate) Jacobian matrix, an alternative approximate Jacobian 15327f296bb3SBarry Smith matrix used to construct the preconditioner, and the optional 15337f296bb3SBarry Smith user-provided context, passed in as `fP`. The user must provide the 15347f296bb3SBarry Smith Jacobian as a matrix; thus, if using a matrix-free approach, one must 15357f296bb3SBarry Smith create a `MATSHELL` matrix. 15367f296bb3SBarry Smith 15377f296bb3SBarry SmithIn addition, the user must provide a routine that computes the 15387f296bb3SBarry Smithpseudo-timestep. This is slightly different depending on if one is using 15397f296bb3SBarry Smitha constant timestep over the entire grid, or it varies with location. 15407f296bb3SBarry Smith 15417f296bb3SBarry Smith- For location-independent pseudo-timestepping, one uses the routine 15427f296bb3SBarry Smith 15437f296bb3SBarry Smith ``` 1544*2a8381b2SBarry Smith TSPseudoSetTimeStep(TS ts,PetscInt(*dt)(TS,PetscReal*,PetscCtx),PetscCtx dtctx); 15457f296bb3SBarry Smith ``` 15467f296bb3SBarry Smith 15477f296bb3SBarry Smith The function `dt` is a user-provided function that computes the 15487f296bb3SBarry Smith next pseudo-timestep. As a default one can use 1549*2a8381b2SBarry Smith `TSPseudoTimeStepDefault(TS,PetscReal*,PetscCtx)` for `dt`. This 15507f296bb3SBarry Smith routine updates the pseudo-timestep with one of two strategies: the 15517f296bb3SBarry Smith default 15527f296bb3SBarry Smith 15537f296bb3SBarry Smith $$ 15547f296bb3SBarry Smith dt^{n} = dt_{\mathrm{increment}}*dt^{n-1}*\frac{|| F(u^{n-1}) ||}{|| F(u^{n})||} 15557f296bb3SBarry Smith $$ 15567f296bb3SBarry Smith 15577f296bb3SBarry Smith or, the alternative, 15587f296bb3SBarry Smith 15597f296bb3SBarry Smith $$ 15607f296bb3SBarry Smith dt^{n} = dt_{\mathrm{increment}}*dt^{0}*\frac{|| F(u^{0}) ||}{|| F(u^{n})||} 15617f296bb3SBarry Smith $$ 15627f296bb3SBarry Smith 15637f296bb3SBarry Smith which can be set with the call 15647f296bb3SBarry Smith 15657f296bb3SBarry Smith ``` 15667f296bb3SBarry Smith TSPseudoIncrementDtFromInitialDt(TS ts); 15677f296bb3SBarry Smith ``` 15687f296bb3SBarry Smith 15697f296bb3SBarry Smith or the option `-ts_pseudo_increment_dt_from_initial_dt`. The value 15707f296bb3SBarry Smith $dt_{\mathrm{increment}}$ is by default $1.1$, but can be 15717f296bb3SBarry Smith reset with the call 15727f296bb3SBarry Smith 15737f296bb3SBarry Smith ``` 15747f296bb3SBarry Smith TSPseudoSetTimeStepIncrement(TS ts,PetscReal inc); 15757f296bb3SBarry Smith ``` 15767f296bb3SBarry Smith 15777f296bb3SBarry Smith or the option `-ts_pseudo_increment <inc>`. 15787f296bb3SBarry Smith 15797f296bb3SBarry Smith- For location-dependent pseudo-timestepping, the interface function 15807f296bb3SBarry Smith has not yet been created. 15817f296bb3SBarry Smith 15827f296bb3SBarry Smith```{eval-rst} 15837f296bb3SBarry Smith.. bibliography:: /petsc.bib 15847f296bb3SBarry Smith :filter: docname in docnames 15857f296bb3SBarry Smith 15867f296bb3SBarry Smith``` 1587