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