Skip to content

Commit

Permalink
test: add test for observable functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DhairyaLGandhi committed May 12, 2024
1 parent a69d087 commit 785b052
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/downstream/observables_autodiff.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using ModelingToolkit, OrdinaryDiffEq
using Zygote

@parameters σ ρ β
@variables x(t) y(t) z(t) w(t)

eqs = [D(D(x)) ~ σ * (y - x),
D(y) ~ x *- z) - y,
D(z) ~ x * y - β * z,
w ~ x + y + z + 2 * β]

@mtkbuild sys = ODESystem(eqs, t)

u0 = [D(x) => 2.0,
x => 1.0,
y => 0.0,
z => 0.0]

p ==> 28.0,
ρ => 10.0,
β => 8 / 3]

tspan = (0.0, 100.0)
prob = ODEProblem(sys, u0, tspan, p, jac = true)
sol = solve(prob, Tsit5())

@testset "AutoDiff Observable Functions" begin
gs, = gradient(sol) do sol
sum(sol[sys.w])
end
du_ = [0., 1., 1., 1.]
du = [du_ for _ = sol.u]
@test du == gs.u
end

0 comments on commit 785b052

Please sign in to comment.