Skip to contents

DAEAdjointProblem(sol, sensealg, alg, ...) builds the backward adjoint problem of the solution of a DAEProblem() F(du, u, p, t) = 0, the augmented adjoint of Cao, Li, Petzold and Serban (2003) for z = (w, lambda) with w = F_du' lambda (and the parameter gradient for InterpolatingAdjoint()):

Usage

DAEAdjointProblem(
  sol,
  sensealg = InterpolatingAdjoint(),
  alg = NULL,
  t = NULL,
  dgdu_discrete = NULL,
  dgdp_discrete = NULL,
  dgdu_continuous = NULL,
  dgdp_continuous = NULL,
  g = NULL,
  callback = NULL,
  no_start = FALSE,
  abstol = 1e-06,
  reltol = 0.001
)

Arguments

sol

The dense solution of a DAEProblem.

sensealg

InterpolatingAdjoint() (without checkpointing), QuadratureAdjoint(), GaussAdjoint(), GaussKronrodAdjoint() or SundialsAdjoint() (solved as InterpolatingAdjoint).

alg

The algorithm of the backward solve; the forward algorithm when NULL.

t

Times of the discrete cost.

dgdu_discrete, dgdp_discrete

Functions (u, p, t, i) returning the gradient of the ith discrete cost term.

dgdu_continuous, dgdp_continuous

Functions (u, p, t) returning the gradients of the continuous cost.

g

The continuous cost g(u, p, t).

callback

Must be NULL: callbacks are not supported (see solve_gradient()).

no_start

Leave out the discrete cost at the initial time.

abstol, reltol

Tolerances of the backward solve.

Value

An ODEProblem or DAEProblem with elements alg and finish (a function of the backward solution returning list(du0, dp)).

Details

w_j' = (F_u' lambda - g_u)_j and 0 = w_j - (F_du' lambda)_j for the differential variables, 0 = w_j and 0 = (F_u' lambda - g_u)_j for the algebraic ones, solved backward from z(T) = 0. With an ODE algorithm (a mass matrix method such as Rodas5P() or FBDF()) it is a singular mass matrix ODEProblem, with a DAE algorithm (DFBDF(), the default when the forward solve used one) a DAEProblem. Discrete costs jump w by their gradient, corrected so that the jump is consistent when the cost involves algebraic variables (index 1) or the DAE is Hessenberg index 2. Then dG/du0 = w(t0) and dG/dp = int (g_p - lambda' F_p) dt (the state of InterpolatingAdjoint(), or a quadrature for QuadratureAdjoint(), GaussAdjoint() and GaussKronrodAdjoint()).

adjoint_sensitivities() uses it for DAE solutions. The forward solution must be dense; callbacks are not supported here, but solve_gradient() differentiates a DAEProblem with events through its semi-explicit form u_d' = w, 0 = F(w, u, p, t). The Jacobians of the residual come from the System the problem was built from (symbolically) or from the problem's Jacobian and central differences; du(t) is the derivative of the forward interpolant (by central differences of the step interpolant).

Examples

f <- function(du, u, p, t) c(du[1] + p[1] * u[1] - u[2], u[2] - p[2] * u[1])
prob <- DAEProblem(f, du0 = c(-0.5, 0), u0 = c(1, 0.5), tspan = c(0, 1), p = c(1, 0.5),
                   differential_vars = c(TRUE, FALSE))
sol <- solve(prob, DFBDF(), abstol = 1e-10, reltol = 1e-10)
# G = u1(1) = exp(-(p1 - p2)): dG/dp = (-1, 1) exp(-0.5)
adjoint_sensitivities(sol, DFBDF(), t = 1, dgdu_discrete = function(u, p, t, i) c(1, 0),
                      abstol = 1e-10, reltol = 1e-10)$dp / exp(-0.5)
#> [1] -1  1