# Automatic Differentiation of the flow of an ode : DifferentiationInterface, SciMLSensitivity

**URL:** https://discourse.julialang.org/t/automatic-differentiation-of-the-flow-of-an-ode-differentiationinterface-scimlsensitivity/135730
**Category:** Numerics
**Created:** [February 19, 2026, 2:25pm UTC](https://discourse.julialang.org/t/automatic-differentiation-of-the-flow-of-an-ode-differentiationinterface-scimlsensitivity/135730 "2026-02-19T14:25:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![joseph-gergaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joseph-gergaud/32/220870_2.png) [@joseph-gergaud](https://discourse.julialang.org/u/joseph-gergaud)
#### Post date: [February 19, 2026, 2:25pm UTC](https://discourse.julialang.org/t/automatic-differentiation-of-the-flow-of-an-ode-differentiationinterface-scimlsensitivity/135730/1 "2026-02-19T14:25:04Z")

</div>

When I use the backend Zygote for the automatic differentiation with DifferentiationInterface for the flow of an ordinary differentiation equation, I have an error :

Here is the code :

```julia
using Pkg
Pkg.activate(".")
#Pkg.add("SciMLSensitivity")
#Pkg.add("BenchmarkTools")
#Pkg.add("Enzyme")
#Pkg.add("Mooncake")
#println(pwd())
#println(Pkg.status())
using Markdown
using LinearAlgebra
using Test
using DifferentiationInterface

using ForwardDiff: ForwardDiff
using Enzyme: Enzyme
using Mooncake: Mooncake
using Zygote: Zygote

using OrdinaryDiffEq
using SciMLSensitivity

#include("./fun_examples.jl")
#include("../src/CTDiffFlow.jl")
#using .CTDiffFlow

# Automatic differentiation on the flow
# derivatives with respect to x0
function build_∂x0_flow(rhs::Function,t0::Real,x0::Vector{<:Real},tf::Real, λ::Vector{<:Real}; backend = AutoForwardDiff())

    # Jacobian matrix
    function ∂x0_flow(t0::Real,x0::Vector{<:Real}, tf::Real, λ::Vector{<:Real};ode_kwargs...)
        function _flow(x0)
            ivp = ODEProblem(rhs, x0, (t0,tf), λ)
            algo = get(ode_kwargs, :alg, Tsit5())
            sol = solve(ivp, alg=algo; ode_kwargs...)
            return sol.u[end]
        end
        return jacobian(_flow,backend,x0)
     end
    return ∂x0_flow
end

function main()
    tol_error = 2*eps()
    # Backends
    # Problems with Enzyme
    #Backend = (AutoEnzyme(), AutoForwardDiff(), AutoMooncake(), AutoZygote())
    Backends = (AutoForwardDiff(), AutoMooncake(), AutoZygote())
    reltol = 1.e-3;
    abstol = 1.e-6
    tol_error = 10*max(reltol,abstol)
    #
    # Initial value problem
    λ = [1.0, 2]
    A(λ) = [λ[1] 0 ; 0 λ[2]]
    fun_lin1(x,λ,t) = A(λ)*x
    t0 = 0.0; tf = 1.0;
    x0 = [1., 2.]
    # jacobien of the tlow
    sol_∂xO_flow(tf,λ) = exp(tf*A(λ))

    # Test of automatic differentiation
    for backend in Backends
      println("backend = ", backend)
      #∂x0_flow = CTDiffFlow.build_∂x0_flow(fun_lin1, t0, x0, tf, λ; backend = backend)
      ∂x0_flow = build_∂x0_flow(fun_lin1, t0, x0, tf, λ; backend = backend)
      #println("∂x0_flow = ", ∂x0_flow(t0, x0, tf, λ; reltol=reltol, abstol=abstol))
      #println("sol_∂xO_flow(tf,λ) = ", sol_∂xO_flow(tf,λ))
      #println(@test isapprox(sol_∂xO_flow(tf,λ), ∂x0_flow(t0, x0, tf, λ; reltol=reltol, abstol=abstol), atol=tol_error))
      @test isapprox(sol_∂xO_flow(tf,λ), ∂x0_flow(t0, x0, tf, λ; reltol=reltol, abstol=abstol), atol=tol_error)
    end
  end

  main()

```

and the results :

Activating project at `~/Control/CTDiffFlow.jl`  
backend = AutoForwardDiff()  
backend = AutoMooncake()  
backend = AutoZygote()  
Test Failed at /Users/gergaud/Control/CTDiffFlow.jl/test/test\_diff\_auto\_flow.jl:70  
Expression: isapprox(sol\_∂xO\_flow(tf, λ), ∂x0\_flow(t0, x0, tf, λ; reltol = reltol, abstol = abstol), atol = tol\_error)  
Evaluated: isapprox([2.718281828459045 0.0; 0.0 7.38905609893065], [2.718281813973846 0.0; 2.718281813973846 7.389041757987686]; atol = 0.01)

ERROR: There was an error during testing

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [February 20, 2026, 6:23am UTC](https://discourse.julialang.org/t/automatic-differentiation-of-the-flow-of-an-ode-differentiationinterface-scimlsensitivity/135730/2 "2026-02-20T06:23:04Z")

</div>

It looks like it was a rule issue, fixed in [fix: remove ODEProblem constructor rrules causing Zygote Jacobian accumulation by ChrisRackauckas-Claude · Pull Request #1237 · SciML/SciMLBase.jl · GitHub](https://github.com/SciML/SciMLBase.jl/pull/1237)
