API reference
Algorithms
PETScDiffEq.TSRK — Type
TSRK(subtype = "5dp", petsc_options = String[]; comm = MPI.COMM_SELF, dm = nothing)Explicit Runge-Kutta from PETSc's TSRK. subtype is a PETSc TSRKType without its prefix, such as "3bs", "5dp", "5f" or "5bs".
Adapts on its embedded error estimate, so reltol and abstol apply. PETSc gives "1fe", "2b", "3" and "4" no such estimate, so those step at the dt you give and warn if you pass a tolerance. Being explicit it never forms a Jacobian and ignores an ODEFunction's jac, and it cannot carry a mass matrix.
petsc_options are command-line style tokens passed to PETSc for this solve, for example ["-ts_adapt_type", "none"]. They are parsed after the options this package sets, so they win.
A comm other than MPI.COMM_SELF runs the solve distributed over it, with u0 holding this rank's rows; see the MPI section of the documentation. A dm, a DMDA, DMStag or DMPlex from PETSc.jl, runs it on the DM's communicator and gives f the state ghosted from the DM, as that section describes.
PETScDiffEq.TSRosW — Type
TSRosW(subtype = "ra34pw2", petsc_options = String[]; autodiff = AutoForwardDiff(), comm = MPI.COMM_SELF, dm = nothing)Rosenbrock-W from PETSc's TSROSW. subtype is a PETSc TSRosWType without its prefix, such as "2m", "ra34pw2" or "r34prw".
Adapts on its embedded error estimate, except for "theta1" and "theta2", which PETSc gives none, so they step at the dt you give and warn if you pass a tolerance. Linearly implicit, so it uses an ODEFunction's jac, and it accepts a mass matrix.
Without a jac the Jacobian comes from autodiff: ForwardDiff by default, colouring a sparse jac_prototype, or AutoFiniteDiff() to have PETSc difference the step's own equations, colouring a sparse prototype too. For a complex state f has to be holomorphic, and one that is not is refused under ForwardDiff.
PETSc's implementation assumes a right-hand side that does not depend on t. When it does, "2p", "2m", "ra3pw", "ra34pw2", "r34prw" and "assp3p3s1c" keep their order, and every other type, including all the fourth-order ones, converges at first order with or without a jac. Carrying t as an extra state whose derivative is 1 restores their order.
On a linear right-hand side that does not depend on t, "ra3pw"'s embedded error estimate is zero with an exact Jacobian and far too small with a differenced one, so an adaptive solve reports success with an error well above the tolerance. Step it with a fixed dt on such problems.
"assp3p3s1c" needs a Jacobian, which AutoFiniteDiff() does not give it, and cannot take a mass matrix, which PETSc leaves out of its explicit first stage. "lassp3p4s2c", "llssp3p4s2c" and "ark3" are refused. They end on an explicit stage: without a Jacobian PETSc stops and asks for one, and with one it does not restore its Jacobian lag after that stage, so an adaptive solve fails within its first two steps and a fixed-step solve diverges.
A comm other than MPI.COMM_SELF runs the solve distributed over it, as for TSRK. There autodiff defaults to AutoFiniteDiff(), AutoForwardDiff() colours the whole pattern of a sparse jac_prototype holding this rank's rows with global columns, and a jac fills those rows; see the MPI section of the documentation. With a dm the Jacobian is the DM's own matrix, with the pattern of its stencil, which a jac fills through PETSc's matrix API from the ghosted u. Without one autodiff defaults there to AutoFiniteDiff(), PETSc colouring the matrix and differencing f into it, and on a DMDA AutoForwardDiff() fills it from f run on dual numbers, seeded by the same colours.
PETScDiffEq.TSImplicit — Type
TSImplicit(subtype = "beuler"; order = nothing, autodiff = AutoForwardDiff(), comm = MPI.COMM_SELF, dm = nothing)
TSImplicit(subtype, theta; ...)
TSImplicit(subtype, [theta,] petsc_options; ...)Fully implicit methods from PETSc: "beuler", "cn", "theta" and "bdf". The types TSGeneric refuses, such as "alpha2" or "discgrad", are refused here too. theta sets the parameter of the theta method, where 0.5 is Crank-Nicolson and 1.0 is backward Euler.
order sets the BDF order, 1 through 6. PETSc's own default is 2, which on a stiff problem can cost an order of magnitude in steps against a higher-order method, so raise it when comparing against one.
Only "bdf" carries an embedded error estimate and adapts; the others step at the dt you give and warn if you pass a tolerance. All of them use an ODEFunction's jac and accept a mass matrix, which makes a singular mass matrix an index-1 differential-algebraic problem.
Without a jac the Jacobian comes from autodiff: ForwardDiff by default, colouring a sparse jac_prototype, or AutoFiniteDiff() to have PETSc difference the step's own equations, colouring a sparse prototype too. For a complex state f has to be holomorphic, since PETSc's Newton iteration takes a complex Jacobian, and one that is not is refused under ForwardDiff.
A comm other than MPI.COMM_SELF runs the solve distributed over it, as for TSRK. There autodiff defaults to AutoFiniteDiff(), AutoForwardDiff() colours the whole pattern of a sparse jac_prototype holding this rank's rows with global columns, and a jac fills those rows; see the MPI section of the documentation. With a dm the Jacobian is the DM's own matrix, with the pattern of its stencil, which a jac fills through PETSc's matrix API from the ghosted u. Without one autodiff defaults there to AutoFiniteDiff(), PETSc colouring the matrix and differencing f into it, and on a DMDA AutoForwardDiff() fills it from f run on dual numbers, seeded by the same colours.
PETScDiffEq.TSIRK — Type
TSIRK(nstages = 3, petsc_options = String[]; autodiff = AutoForwardDiff(), comm = MPI.COMM_SELF, dm = nothing)Gauss-Legendre implicit Runge-Kutta from PETSc's TSIRK, of order 2 * nstages: one stage is the implicit midpoint rule at order 2, two stages give order 4 and three give order 6. Measured at each of those.
Fixed step: PETSc gives this family no embedded error estimate, so it steps at the dt you give and warns if you pass a tolerance.
Needs a Jacobian, from the ODEFunction's jac or from autodiff, and refuses AutoFiniteDiff() rather than letting PETSc fail, since it solves all stages as one coupled system whose matrix it cannot build from finite differences. That coupled matrix is a Kronecker product with the Jacobian, which has no LU factorisation, so this algorithm defaults to -pc_type pbjacobi unless your own petsc_options set a -pc_type. So does "irk" picked by TSGeneric or by -ts_type.
A wrong Jacobian is not caught here. Where the other implicit families fail to converge, this one reports success and returns a wrong answer, so check a hand-written jac against a solve without one before trusting it.
A mass matrix is rejected. PETSc's coupled-stage matrix assumes dF/du̇ = I, and with a non-identity mass matrix the answer drifts further from the true one as dt shrinks instead of failing, which is worse than an error.
A comm other than MPI.COMM_SELF runs the solve distributed over it, as for TSRK. There it needs a jac or autodiff = AutoForwardDiff(), with a sparse jac_prototype holding this rank's rows with global columns. Any split of the state works: PETSc lays out its stage vector by its own even split, so on any other the solve runs on that one and moves the state to and from each rank's block around every call to f and jac; see the MPI section of the documentation. A dm runs it as well, with autodiff defaulting to AutoForwardDiff(), which needs a DMDA; on a DMStag or a DMPlex give it a jac. PETSc also strides the stage vector by the state's block size, which only a block size of 1 gets right, so the solve runs on a plain vector, on PETSc's split when the DM's is another, with the DM's matrix copied to that split at every Jacobian.
PETScDiffEq.TSARKIMEX — Type
TSARKIMEX(subtype = "3", petsc_options = String[]; autodiff = AutoForwardDiff(), comm = MPI.COMM_SELF, dm = nothing)Additive Runge-Kutta IMEX from PETSc's TSARKIMEX. subtype is a PETSc TSARKIMEXType without its prefix, such as "2e", "3", "4" or "5".
Takes a SplitODEProblem whose f1 is integrated implicitly and whose f2 is integrated explicitly, and uses f1's Jacobian when the problem carries one. Without one, f1's Jacobian comes from autodiff, as for TSImplicit. A plain ODEProblem is treated as fully implicit with the explicit part left at zero, which is PETSc's own default. Adapts on its embedded error estimate, except for "prssp2", "ars443" and "bpr3", which PETSc gives none, so they step at the dt you give and warn if you pass a tolerance.
"ars122" needs a SplitODEProblem: it has an explicit first stage and is not stiffly accurate, so PETSc cannot evaluate its first-stage slope when the whole problem is implicit.
"bpr3" is refused on a SplitODEProblem, where it converges at first order. On a plain ODEProblem PETSc does not use its explicit tableau, and it keeps order 3.
PETSc's DIRK tableaux, such as "es213sal", have no explicit part, so they take a SplitODEProblem's f2 implicitly along with f1.
A comm other than MPI.COMM_SELF runs the solve distributed over it, as for TSRK. There autodiff defaults to AutoFiniteDiff(), AutoForwardDiff() colours the whole pattern of a sparse jac_prototype holding this rank's rows with global columns, and a jac fills those rows; see the MPI section of the documentation. With a dm the Jacobian is the DM's own matrix, with the pattern of its stencil, which a jac fills through PETSc's matrix API from the ghosted u. Without one autodiff defaults there to AutoFiniteDiff(), PETSc colouring the matrix and differencing f into it, and on a DMDA AutoForwardDiff() fills it from f run on dual numbers, seeded by the same colours.
PETScDiffEq.TSDAE — Type
TSDAE(subtype = "bdf", petsc_options = String[]; order = nothing, autodiff = AutoForwardDiff(), comm = MPI.COMM_SELF, dm = nothing)Fully implicit methods applied to a DAEProblem, whose residual G(t, u, u') = 0 is exactly the form PETSc's IFunction takes. subtype is "beuler", "cn", "theta" or "bdf", and the types TSGeneric refuses are refused here too. There is no theta argument as TSImplicit has: "theta" runs PETSc's default of 0.5, and petsc_options such as ["-ts_theta_theta", "1.0"] set another.
A DAEFunction's jac(J, du, u, p, gamma, t) is gamma * dG/du' + dG/du, which is what PETSc's IJacobian wants whole, so it is passed straight through and gamma is PETSc's shift. Without one the Jacobian comes from autodiff, as for TSImplicit.
order sets the BDF order, 1 through 6, and carries the same warning as TSImplicit: PETSc's own default is 2.
Only "bdf" adapts; the others step at the dt you give. PETSc derives the initial derivative itself, so du0 is used only to check and solve for a consistent start, as initializealg asks; see the DAE initialization section of the documentation.
A comm other than MPI.COMM_SELF runs the solve distributed over it, as for TSRK. There autodiff defaults to AutoFiniteDiff(), AutoForwardDiff() colours the whole pattern of a sparse jac_prototype holding this rank's rows with global columns, and a jac fills those rows; see the MPI section of the documentation. With a dm the Jacobian is the DM's own matrix, with the pattern of its stencil, which a jac fills through PETSc's matrix API from the ghosted u, or which PETSc colours and differences f into when there is none, with autodiff then at its default there, AutoFiniteDiff().
PETScDiffEq.TSMPRK — Type
TSMPRK(slow, subtype = "p2", petsc_options = String[]; comm = MPI.COMM_SELF, dm = nothing)
TSMPRK(slow, medium, subtype = "2a23", petsc_options = String[]; comm = MPI.COMM_SELF, dm = nothing)PETSc's multirate partitioned Runge-Kutta. slow lists the indices of the state that are integrated with the outer step; everything else is advanced on a smaller one. Both parts come from the problem's own f, which is evaluated in full and then read row by row, so nothing beyond the index list is asked of the caller.
Passing a medium set as well splits the state three ways, which is what PETSc's "2a23" and "2a33" want. Whatever neither set names is the fast part.
Explicit, so a mass matrix is refused and a jac is ignored. Without a medium set subtype is one of "2a22", "2a32", "p2" or "p3"; with one it is "2a23" or "2a33".
The fast part is stepped on a shorter clock, so the step this tolerates is larger than an explicit single-rate method's: on u' = [-u1, -100u2] with only the first component slow, "p3" holds to dt = 0.0425 against TSRK("5dp")'s 0.03. The ratio is fixed by the tableau rather than by the stiffness, so a separation much wider than that is not something these methods can absorb.
A comm other than MPI.COMM_SELF runs the solve distributed over it, as for TSRK. There slow and medium index this rank's rows and may be empty on some ranks, as long as some rank names a slow row and, for "2a23" and "2a33", a medium one. A dm is refused.
PETScDiffEq.TSBasicSymplectic — Type
TSBasicSymplectic(subtype = "velverlet", petsc_options = String[]; comm = MPI.COMM_SELF, dm = nothing)PETSc's basic symplectic integrators, TSBASICSYMPLECTIC, for a DynamicalODEProblem or a SecondOrderODEProblem. subtype is "sieuler" (or "1"), semi-implicit Euler of order 1, "velverlet" (or "2"), velocity Verlet of order 2, "3", Ruth's method of order 3, or "4", Forest and Ruth's method of order 4.
PETSc steps the two parts in turn, the velocity v with f1(dv, v, u, p, t) at the current positions and the position u with f2(du, v, u, p, t) at the current velocities, so, as for OrdinaryDiffEq's symplectic methods, f1 must not depend on v nor f2 on u. On such a separable problem the energy error stays bounded over long times instead of drifting. f1 is given the time of the positions it is evaluated at, so a force that depends on t keeps the method's order.
Fixed step: these methods have no error estimate, so they step at the dt you give and warn if you pass a tolerance. Being explicit they ignore a jac and refuse a mass matrix.
A comm other than MPI.COMM_SELF runs the solve distributed over it: the initial v and u hold this rank's blocks of the velocity and the position, and f1 and f2 see only those blocks and are collective, as f is for TSRK; see the MPI section of the documentation. A dm runs it on a PETSc DM: v and u are each the block of the grid this rank owns, and f1, and a DynamicalODEProblem's f2, get both of them ghosted through the DM, as f gets u for TSRK.
PETScDiffEq.TSAlpha2 — Type
TSAlpha2(petsc_options = String[]; radius = nothing, autodiff = AutoForwardDiff(), comm = MPI.COMM_SELF, dm = nothing)PETSc's generalized-alpha method for second-order systems, TSALPHA2, for a SecondOrderODEProblemu'' = f(u', u, p, t). Implicit and of order 2. radius is the spectral radius at an infinite step, from 0 to 1: PETSc's default, 1, damps nothing, and a smaller one damps the highest frequencies more.
Adapts on PETSc's error estimate for the method, which PETSc leaves off unless asked, so reltol and abstol apply. PETSc weighs the velocity and the position with one tolerance per component of the position, so a vector tolerance on the state [v; u] takes the smaller of each velocity and position pair. adaptive = false steps at the dt you give, unless petsc_options pick an adaptive -ts_adapt_type, which wins. PETSc forms the velocity and the acceleration from the step's change in position, so an adaptive solve ends with Unstable when a component's change falls below the position's floating point spacing while the velocity it would carry exceeds the tolerance. A tight tolerance such as 1e-9 in Float32 does this, and so can the automatic first step of a Float32 body starting from rest at default tolerances; pass dt there.
Each step solves for the position, with the Jacobian of f in u and u'. That comes from a jac, which is the Jacobian of the first-order system [v; u]' = [f(v, u, p, t); v], 2n by 2n in the order of the state, as OrdinaryDiffEq's implicit methods take it, or without one from autodiff, as for TSImplicit. A sparse jac_prototype of that system keeps the n by n matrix PETSc factors sparse: a jac fills the prototype's structure, and without one the Jacobian is coloured, by SparseMatrixColorings under AD or by PETSc under AutoFiniteDiff, as for TSImplicit. A mass matrix is refused, and so is -snes_type ksponly, since PETSc forms each step's velocity while evaluating the residual, which ksponly never does at its solution. Until SciMLBase's DynamicalODEFunction can be rebuilt with a jac_prototype, solve and init fail on one before reaching this package, so give such a problem to SciMLBase.__solve or SciMLBase.__init.
A reversed tspan is supported. PETSc only steps forward, so the solve runs in s = -t on w(s) = u(-s), whose velocity is -u' and whose acceleration is f(-w', w, p, -s). The states, the saved velocities, jac and the callbacks stay those of the problem as written.
A comm other than MPI.COMM_SELF runs the solve distributed over it, as for TSBasicSymplectic, with this rank's velocity and position of the same length. There autodiff defaults to AutoFiniteDiff(), and a jac or AutoForwardDiff() fills this rank's rows of a sparse jac_prototype of the first-order system, whose columns number the whole [v; u]; see the MPI section of the documentation. A dm runs it on a PETSc DM, which lays out the position, the velocity and the acceleration alike: f gets u' and u ghosted, and PETSc colours the DM's matrix for the matrix it factors, so autodiff stays at its default there, AutoFiniteDiff(), and a jac is refused.
PETScDiffEq.TSGeneric — Type
TSGeneric(ts_type, petsc_options = String[]; explicit = false, autodiff = AutoForwardDiff(), comm = MPI.COMM_SELF, dm = nothing)Any other PETSc TSType by name. An implicit one such as "alpha" works with the default; an explicit one such as "euler" or "ssp" needs explicit = true, since PETSc then wants the right-hand side rather than the implicit residual. The constructor refuses a type given the wrong explicit. An explicit type also ignores a jac and rejects a mass matrix. An implicit one without a jac gets its Jacobian from autodiff, as for TSImplicit.
A type another constructor covers, such as "bdf" or "beuler", is treated as that constructor treats it. Whether any other type adapts is not known here, so it needs dt and gets no tolerance warning. Only "euler" and "alpha" have been run through this package's own convergence tests. "alpha" refuses -snes_type ksponly, as TSAlpha2 does.
"discgrad", "eimex", "mimex" and "mprk" are refused: each is driven through a PETSc setup call this package does not make, and without it they crash or integrate to zero rather than saying anything. "alpha2" and "basicsymplectic" are refused too, since they need a second-order or partitioned problem; use TSAlpha2 or TSBasicSymplectic. PETSc runs "glle" only as a whole solve, with no single step and no stop between steps. So init refuses it, and so does a solve with a callback, tstops, d_discontinuities, isoutofdomain, unstable_check, an adaptive dtmin or a Float32 clock.
A comm other than MPI.COMM_SELF runs an explicit type as TSRK runs, and an implicit "beuler", "cn", "theta", "bdf", "rosw", "arkimex", "irk", "alpha" or "dirk" as TSImplicit runs, with autodiff defaulting to AutoFiniteDiff(). Other implicit types are refused there: "glle"'s step control follows the round-off of the distributed linear solve, so it takes other steps than a serial solve and ends with another error, larger or smaller. A dm runs an explicit type as it runs TSRK and the same implicit types, "irk" as TSIRK runs and the others as TSImplicit runs.
Integrator
PETScDiffEq.PETScIntegrator — Type
PETScIntegratorThe integrator SciMLBase.init returns for a PETSc TS algorithm. Step it with step!, run it to the end with solve!, stop it early with terminate! and restart it with reinit!. Between steps u, uprev, t, tprev and dt are readable, integ(t), integ(t, Val{1}), integ(t; idxs) and integ(out, t) give the state or its slope inside the step just taken, and add_tstop! schedules a time to land on exactly. integ.ps[sym] gets and sets parameters by symbol, and a write is a derivative_discontinuity!(integ, true), so the next step restarts the method and initializes a DAE again. Assigning u or t takes effect at the next step, as set_u! and set_t! do.
These are in the types PETSc steps in. The clock, and so t, dt and the saved times, is Float32 for a Float32 or ComplexF32 state with a Float32 span and Float64 otherwise, whatever the span's type. The state is the problem's own, except that a whole-number one is stepped in Float64 and a single-precision one with a Float64 span in double precision, while the solution it saves stays in single. For a DynamicalODEProblem or SecondOrderODEProblem the state is an ArrayPartition of the velocity and the position, as in OrdinaryDiffEq. An array-shaped u0 gives u, uprev and every state read from the integrator as an Array of its size, and a u0 of another array type, such as a ComponentArray, gives them as similar(u0) makes them. For an out-of-place problem with a static u0 (an SVector or SMatrix) and StaticArrays loaded, f, jac, unstable_check, isoutofdomain and the saved states sol.u get the state in that type. f and jac get it in PETSc's working precision; sol.u keeps the element type of u0. The integrator's own u and uprev stay the mutable similar(u0), since it updates them in place.
iter counts every step attempted, accepted, rejected or failed, as maxiters does, and stats is the running sol.stats. opts carries the options OrdinaryDiffEq's integrator exposes where PETSc has them. Assigning maxiters, save_everystep, save_start, save_end, save_discretes, unstable_check, isoutofdomain, dtmin or adaptive takes effect at once, a fixed step going on at the step PETSc proposed last. adaptive raises an ArgumentError for TSAlpha2, a TSGeneric type PETScDiffEq does not know, or options that pick -ts_adapt_type. tstops and saveat are the live queues keyed by tdir * t, with saveat keeping the times already passed; unstable_check and isoutofdomain are nothing unless given; dense, save_idxs, calck, internalnorm, callback and force_dtmin are fixed at init.
Repeated solves
PETScDiffEq.reusing — Function
PETScDiffEq.reusing(f)Call f(), letting each solve made meanwhile start again on the PETSc objects of an earlier solve instead of building and destroying its own, when the two differ only in u0, p or tspan. Results are the same as without it. An EnsembleProblem solved with EnsembleSerial() or EnsembleThreads() does this already; use it around a loop of solves such as a parameter sweep or a fit:
sols = PETScDiffEq.reusing() do
[solve(remake(prob; p = p), TSRosW()) for p in ps]
endThe objects are freed when the outermost reusing returns. Solves made by other tasks while f runs may reuse them too. A solve is made as before when it has a callback, tstops, isoutofdomain or a Float32 clock, runs over a communicator or a DM, has a state that is not a real Array, uses a method reinit! cannot reuse, or passes an option whose PETSc state outlives a solve, as -snes_lag_jacobian, -ts_time_span and -ts_monitor do; one whose f, mass matrix, Jacobian sparsity, algorithm or keywords differ from every kept solve's starts anew.
Sensitivity algorithm
PETScDiffEq.PETScAdjoint — Type
PETScAdjoint(; petsc_options = String[])PETSc's own discrete adjoint, TSAdjointSolve, as a sensealg for SciMLSensitivity's adjoint_sensitivities:
using PETScDiffEq, SciMLSensitivity
sol = solve(prob, TSRK("4"); dt = 0.01, adaptive = false)
du0, dp = adjoint_sensitivities(
sol, TSRK("4"); sensealg = PETScAdjoint(),
t = ts, dgdu_discrete = dg!, dt = 0.01, adaptive = false,
)The result is the gradient of the solution PETSc computes at these steps, not of the exact solution, so it agrees with finite differences of the same fixed-step solve. PETSc runs the forward solve again, saving a trajectory, and then the adjoint. Only the problem is taken from sol, so the keywords that set the steps (dt, adaptive, abstol, reltol, dtmin, dtmax, maxiters, tstops, d_discontinuities) must be repeated exactly as solve was given them; otherwise the gradient belongs to a different discretization and nothing says so. Saving keywords are ignored, and callback and save_idxs are refused, whether given here or to the problem.
Supported: an ODEProblem without a mass matrix, in place or out of place, solved with TSRK of any subtype, TSImplicit("beuler"), TSImplicit("cn"), TSImplicit("theta") with its theta, in its midpoint form or with -ts_theta_endpoint, or TSARKIMEX, in either time direction. The ODEFunction's jac and paramjac are used when given, and otherwise built as the forward solve builds a missing jac: with the algorithm's autodiff, and with ForwardDiff for a TSRK. Under AutoFiniteDiff() both have to be given, since PETSc's own differences never reach its adjoint. A sparse jac_prototype is used. paramjac takes the form jac does, paramjac(pJ, u, p, t) in place or paramjac(u, p, t) out of place, with a row per state and a column per entry of p, which therefore has to be a vector of real numbers. A hand-written jac or paramjac goes into the gradient unchecked, so a wrong one gives a wrong gradient without an error; compare it against a gradient computed without it.
TSARKIMEX takes a SplitODEProblem as well. The implicit part's jac and paramjac are the problem's, which a SplitODEProblem takes from f1, and the explicit part's are those of f2's own ODEFunction; each is built by automatic differentiation when missing. PETSc's ARKIMEX adjoint has no quadrature, so an integral cost is refused, and -ts_arkimex_fully_implicit is refused on a SplitODEProblem, since the adjoint would still take f2 explicitly.
A DynamicalODEProblem or SecondOrderODEProblem is differentiated as the first-order system on the flat [v; u] that these methods step. Its jac and paramjac, when given, are that system's, taking the state as an ArrayPartition(v, u) as the forward solve's jac does, and are otherwise built from f1 and f2. The cost functions are handed the state as an ArrayPartition and write its derivative into one, and du0 comes back as one. PETSc has no adjoint for TSBasicSymplectic or TSAlpha2, so those are refused. A u0 of another vector type, such as a ComponentArray, is handled the same way, with its own type in place of the ArrayPartition.
The adjoint runs in PETSc's double real build. A Float32 problem is solved there in Float64, so jac, paramjac and the cost functions are handed Float64 states, and du0 and dp come back as Float32 where u0 and p are. Its cost times are matched to the steps at single precision, so the times its own solve saved are accepted with dt repeated as that solve was given it, and at fixed steps or with tstops it takes that solve's steps to single precision. A complex state is refused.
Costs are discrete, integral or both. At each t[i], dgdu_discrete(out, u, p, t, i) writes the cost's derivative with respect to the state and dgdp_discrete(out, u, p, t, i), if given, its direct derivative with respect to p; no_start = true leaves out t[1]. PETSc's adjoint has no derivative of interpolation, so every cost time has to be the end of a step. With fixed steps it must be a time the solve steps to, such as tspan[1] plus a multiple of dt. An adaptive solve takes cost times anywhere in tspan: they are given to PETSc as its time span, TSSetTimeSpan, and its step-size controller ends a step on each one. Those steps are this run's own. solve with the same tolerances takes others and interpolates to its saveat times, so the gradient is that of the cost on the states this run steps to, which differ from the ones solve saves by the error of the two solves. It treats the accepted step sizes as constants rather than differentiating the controller.
tstops and d_discontinuities are stepped to as solve steps to them in its own loop: a step ends on each stop, the next one starts at the size that loop resumes with, and a step after a discontinuity starts one floating-point step past it. The run then takes exactly the steps of solve given the same keywords, and with fixed steps a stop can be a cost time. An adaptive run takes its cost times as stops too, in place of the time span, so its steps are those of solve with the cost times added to tstops, and solve's own when they are among them already; the gradient is then that of the states solve saves.
An integral cost, the integral of g(u, p, t) from tspan[1] to tspan[2], is given as g or as its derivatives dgdu_continuous(out, u, p, t) and dgdp_continuous(out, u, p, t). PETSc sums it with the method's own quadrature, through a quadrature TS whose right-hand side Jacobians are the derivatives of g, so the gradient is that of the sum of dt * b[i] * g over the stages of a TSRK, of dt * g at the end of each backward Euler step, of the trapezoidal sum for Crank-Nicolson, and for the theta method of dt * g at its stage, at t + theta * dt, or in its endpoint form of dt * ((1 - theta) * g(t) + theta * g(t + dt)). Where dgdu_continuous is left out, g is differentiated with respect to u and p as a missing jac is, with ForwardDiff or the algorithm's autodiff; where it is given and dgdp_continuous is not, the direct derivative with respect to p is taken as zero, as SciMLSensitivity's adjoints take it. Under AutoFiniteDiff() the derivatives have to be given. Given together with discrete terms, the two gradients are added.
petsc_options apply to this adjoint's own run and are parsed after the algorithm's. The trajectory is kept in memory, every stage of every step; -ts_trajectory_solution_only 1 keeps only the states and recomputes each step during the adjoint, and -ts_trajectory_type basic writes one file per step to the working directory instead. For an adaptive solve the memory trajectory reserves 8 bytes for each of maxiters steps before starting, 8 MB at the default and 8 GB at maxiters = 10^9. Without a limit, at maxiters = Inf or typemax(Int), it reserves them for 10000 steps and doubles that as they fill. PETSc's time span holds one more copy of the state for each cost time inside tspan. TSImplicit and TSARKIMEX solve transposed linear systems with the same linear solver as their Newton steps. A dense Jacobian is factored directly; with a sparse one the default GMRES leaves the gradient off by up to about its relative tolerance of 1e-5 while the forward states are far closer; pass -ksp_type preonly -pc_type lu, or a tighter -ksp_rtol, when that matters. The options stay in force while the adjoint runs, so those PETSc reads only then, such as -ts_trajectory_view and -ts_adjoint_view_solution, take effect; options set globally, for example through PETSC_OPTIONS, do not reach the run.
With an algorithm whose comm is not MPI.COMM_SELF, the adjoint runs distributed as the solve does, for the same methods on an ODEProblem and for TSARKIMEX on a SplitODEProblem. jac then fills this rank's rows of a sparse jac_prototype with global columns, and paramjac fills this rank's rows; both are collective like f. A missing one is built with ForwardDiff and no other backend: jac from the sparse jac_prototype, coloured as a whole as the solve does under autodiff = AutoForwardDiff(), and paramjac by seeding p, every rank calling f once per chunk of parameters. f then runs on dual numbers, in u for jac, which its halo exchange has to carry, and in p for paramjac. An implicit method's autodiff defaults to AutoFiniteDiff() on such a comm, so pass autodiff = AutoForwardDiff() to have either built. A SplitODEProblem takes the same of f2's ODEFunction, whose sparse jac_prototype of this rank's rows is needed with or without a jac. dgdu_discrete gets this rank's rows of the state and writes their derivative, and dgdp_discrete gives this rank's share of the direct derivative, which the ranks add up. An integral cost is split the same way: g gets this rank's rows and gives its share of the integrand, dgdu_continuous writes the derivative of the whole integrand with respect to those rows, and dgdp_continuous gives this rank's share of its direct derivative. A missing one is taken of this rank's share of g with respect to its own rows and p, so a g whose share reads other ranks' rows needs dgdu_continuous. du0 holds this rank's rows, and dp is the whole gradient on every rank. The cost times, no_start, the length of p, whether there is an integral cost and whether dgdp_discrete, jac and paramjac are given must agree across the ranks. A DynamicalODEProblem or SecondOrderODEProblem runs there too: its jac_prototype holds this rank's v rows and then its u rows, with the columns of the whole [v; u] as in its solve, paramjac fills the same rows, a missing jac or paramjac is built as above, and the costs and du0 are this rank's ArrayPartition(v, u).
With a dm, the adjoint runs on the DM as the solve does, for the same methods and costs, and on a DM of a single rank for a SplitODEProblem with TSARKIMEX too. The ODEFunction's jac(J, u, p, t) then fills the DM's matrix from the ghosted u as in an implicit solve, an explicit method included, whose own solve ignores it, and its paramjac(pJ, u, p, t) gets u ghosted too and fills this rank's rows of pJ in the DM's order, a column per entry of p; PETScDiffEq.reshape_local_array(view(pJ, :, k), dm) indexes column k by grid point. A SplitODEProblem takes f2's as well. On a DMDA a missing one is built with ForwardDiff and no other backend: jac as the solve builds it under autodiff = AutoForwardDiff(), by the DM's colours on the ghosted array of dual numbers, and paramjac by seeding p while u is the ghosted array of plain numbers, every rank calling f once per chunk of parameters. An implicit method's autodiff defaults to AutoFiniteDiff() with a dm, whose colouring only approximates the Jacobian, so pass autodiff = AutoForwardDiff() to have either built; TSRK builds them as it is. On a DMStag or a DMPlex both have to be given. The costs, du0 and dp follow the distributed convention, with the DM's block of the state as this rank's rows: the cost functions get that block as solve saves it, not the ghosted array. The dm has to belong to PETSc's double real build.
Returns (du0, dp'), where dp is nothing when p is nothing or SciMLBase.NullParameters().
Given to solve as its sensealg, it is also what Zygote, or another reverse-mode package that takes its rules from ChainRules, differentiates that solve with:
using Zygote
loss(p) = sum(abs2, Array(solve(prob, alg; p, saveat = ts, sensealg = PETScAdjoint())) .- data)
Zygote.gradient(loss, p)solve returns what it returns without the sensealg. The pullback runs the adjoint above with the keywords solve was given, the times it saved as cost times and the loss's derivative with respect to each saved state as dgdu_discrete, and gives the gradients with respect to u0 and p. Saved times the loss does not depend on are left out, and save_idxs is taken. With fixed steps the saved times have to be step ends. In an adaptive solve the saved states are interpolated while the adjoint's run steps to their times, so the gradient is off by about the error of the solve, and the times of steps saved without saveat are held fixed. With the saveat times also given as tstops, both runs end a step on each and the gradient is that of the saved states. A loss that calls sol(t) or reads sol.prob is refused, since only the saved states carry a derivative, and so are a Float32 problem, a DynamicalODEProblem or SecondOrderODEProblem, a comm other than MPI.COMM_SELF, a dm, and Enzyme, ReverseDiff, Tracker and Mooncake, which it has not been verified with.
DM grids
PETScDiffEq.reshape_local_array — Function
PETScDiffEq.reshape_local_array(x, dm)This rank's part of a vector on the DMDA, DMStag or DMPlex dm, indexed by grid or mesh point. x can be the ghosted array f receives or the block of the state the rank owns, such as du, u0 or a saved state, told apart by their lengths, and a shares its memory.
On a DMDA it is a[c, i] on a 1-D grid and a[c, i, j] on a 2-D one, where c runs over the degrees of freedom at each point. This is PETSc.jl's reshape_local_array, which PETSc.jl 0.4 calls reshapelocalarray and pads to three grid axes.
On a DMStag it is a[loc, c, i], a[loc, c, i, j] or a[loc, c, i, j, k], component c at location loc of element (i, j, k), where loc is a LibPETSc.DMStagStencilLocation such as DMSTAG_ELEMENT, DMSTAG_LEFT or DMSTAG_DOWN_LEFT, and c and the elements count from 1, where PETSc counts from 0. On the ghosted array this is the slot DMStagGetLocationSlot gives within the element. A point on an element's upper side, such as DMSTAG_RIGHT or DMSTAG_UP_RIGHT, is the lower one of the next element, so on a grid of N elements a[DMSTAG_RIGHT, c, N] and a[DMSTAG_LEFT, c, N + 1] are both the last vertex. axes(a, d) is the range of elements along grid axis d whose points a holds. On the ghosted array it takes in the ghost elements, and on the owned block of the last rank along an axis that is not periodic it ends at element N + 1, which holds only the points on that boundary. Reading or writing any other point throws an ArgumentError, unless @inbounds skips the check.
On a DMPlex it is a[c, p], component c of mesh point p, where p is PETSc's own point number, counted from 0, as DMPlexGetDepthStratum, DMPlexGetHeightStratum, DMPlexGetCone and DMPlexGetSupport give it, and c counts from 1 over the degrees of freedom the DM's local section gives the point. The ghosted array holds every point of this rank's part of the mesh, the owned block only the points the rank owns, and checkbounds(Bool, a, c, p) tells whether a holds component c of p. The ghosted array is laid out by the local section and the owned block by the global section, which PETSc builds collectively the first time it is asked for, as a solve does on every rank before it calls f. Any other index throws an ArgumentError, unless @inbounds skips the check of c. The section has to be point-major, without a permutation or constrained degrees of freedom.
PETScDiffEq.set_stencil_values! — Function
set_stencil_values!(J, rows, cols, vals; add = false)Write a block of the DM's matrix J, as a jac gets it in a solve with a dm, through PETSc's MatSetValuesStencil. rows and cols are grid indices, or vectors or tuples of them, in the numbering PETScDiffEq.reshape_local_array uses: (c, i) on a 1-D grid, (c, i, j) on a 2-D one and (c, i, j, k) on a 3-D one, as tuples or CartesianIndexes, global and 1-based, with c the degree of freedom at the point. vals[r, s] is the entry at rows[r] and cols[s], a vector or tuple when either side is a single index and a number when both are. The rows are this rank's own and the columns lie within its ghost region; PETSc drops an index past a ghosted edge, which has no global entry, and refuses an entry outside the DM's stencil. add = true adds to the entries instead of setting them, and PETSc wants a PETSc.assemble!(J) between a set and an add. It allocates nothing once warm, so with tuples for cols and vals a jac can call it at every grid point without allocating.
On a DMStag's matrix the indices are points (loc, c, i), (loc, c, i, j) or (loc, c, i, j, k), as reshape_local_array indexes a DMStag, and the block goes through DMStagMatSetValuesStencil. A point outside this rank's ghosted region throws an ArgumentError, and one past a ghosted edge, which has no global entry, is dropped.
On a DMPlex's matrix the indices are points (c, p), component c of mesh point p, as reshape_local_array indexes a DMPlex, and the block goes through MatSetValues at the global indices the DM's global section gives them, a ghost point's included. A point outside this rank's part of the mesh throws an ArgumentError, and PETSc refuses an entry outside the pattern DMCreateMatrix gives from the DM's adjacency.