From 7c2ddefd1930a9e073f684523dbfaf6753d4c6b9 Mon Sep 17 00:00:00 2001 From: Michael Kraus Date: Thu, 5 Dec 2024 16:49:59 +0100 Subject: [PATCH] Simplify problem constructors and minor cleanups. --- src/hamiltonian.jl | 15 ++------- src/lagrangian.jl | 16 ++-------- src/lagrangian_degenerate.jl | 51 ++++++------------------------- test/lagrangian_lotka_volterra.jl | 7 +++-- 4 files changed, 18 insertions(+), 71 deletions(-) diff --git a/src/hamiltonian.jl b/src/hamiltonian.jl index 53f5b85..038c4bd 100644 --- a/src/hamiltonian.jl +++ b/src/hamiltonian.jl @@ -111,18 +111,7 @@ function HODE(lsys::HamiltonianSystem; kwargs...) HODE(eqs.v, eqs.f, eqs.H; kwargs...) end -function HODEProblem(lsys::HamiltonianSystem, tspan::Tuple, tstep::Real, ics::NamedTuple; kwargs...) +function HODEProblem(lsys::HamiltonianSystem, tspan::Tuple, tstep::Real, ics...; kwargs...) eqs = functions(lsys) - HODEProblem(eqs.v, eqs.f, eqs.H, tspan, tstep, ics; kwargs...) -end - -function HODEProblem(lsys::HamiltonianSystem, tspan::Tuple, tstep::Real, q₀::StateVariable, p₀::StateVariable; kwargs...) - ics = (q = q₀, p = p₀) - HODEProblem(lsys, tspan, tstep, ics; kwargs...) -end - -function HODEProblem(lsys::HamiltonianSystem, tspan::Tuple, tstep::Real, q₀::AbstractArray, p₀::AbstractArray; kwargs...) - _q₀ = StateVariable(q₀) - _p₀ = StateVariable(p₀) - HODEProblem(lsys, tspan, tstep, _q₀, _p₀; kwargs...) + HODEProblem(eqs.v, eqs.f, eqs.H, tspan, tstep, ics...; kwargs...) end diff --git a/src/lagrangian.jl b/src/lagrangian.jl index 724da5c..bfb6536 100644 --- a/src/lagrangian.jl +++ b/src/lagrangian.jl @@ -126,19 +126,7 @@ function LODE(lsys::LagrangianSystem; kwargs...) LODE(eqs.ϑ, eqs.f, eqs.g, eqs.ω, eqs.L; kwargs...) end -function LODEProblem(lsys::LagrangianSystem, tspan::Tuple, tstep::Real, ics::NamedTuple; kwargs...) +function LODEProblem(lsys::LagrangianSystem, tspan::Tuple, tstep::Real, ics...; kwargs...) eqs = functions(lsys) - LODEProblem(eqs.ϑ, eqs.f, eqs.g, eqs.ω, eqs.L, tspan, tstep, ics; kwargs...) -end - -function LODEProblem(lsys::LagrangianSystem, tspan::Tuple, tstep::Real, q₀::StateVariable, p₀::StateVariable, λ₀::AlgebraicVariable; kwargs...) - ics = (q = q₀, p = p₀, λ = λ₀) - LODEProblem(lsys, tspan, tstep, ics; kwargs...) -end - -function LODEProblem(lsys::LagrangianSystem, tspan::Tuple, tstep::Real, q₀::AbstractArray, p₀::AbstractArray, λ₀::AbstractArray = zero(q₀); kwargs...) - _q₀ = StateVariable(q₀) - _p₀ = StateVariable(p₀) - _λ₀ = AlgebraicVariable(λ₀) - LODEProblem(lsys, tspan, tstep, _q₀, _p₀, _λ₀; kwargs...) + LODEProblem(eqs.ϑ, eqs.f, eqs.g, eqs.ω, eqs.L, tspan, tstep, ics...; kwargs...) end diff --git a/src/lagrangian_degenerate.jl b/src/lagrangian_degenerate.jl index e937d57..0e12cc0 100644 --- a/src/lagrangian_degenerate.jl +++ b/src/lagrangian_degenerate.jl @@ -74,7 +74,7 @@ struct DegenerateLagrangianSystem code = ( L = substitute_parameters(build_function(equs_subs.L, t, X, V, params...; nanmath = false), params), - H = substitute_parameters(build_function(equs_subs.H, t, X, V, params...; nanmath = false), params), + H = substitute_parameters(build_function(equs_subs.H, t, X, params...; nanmath = false), params), EL = substitute_parameters(build_function(equs_subs.EL, t, X, V, params...; nanmath = false)[2], params), ∇H = substitute_parameters(build_function(equs_subs.∇H, t, X, V, params...; nanmath = false)[2], params), ẋ = substitute_parameters(build_function(equs_subs.ẋ, t, X, params...; nanmath = false)[2], params), @@ -119,62 +119,29 @@ function ODE(lsys::DegenerateLagrangianSystem; kwargs...) ODE(eqs.ẋ; invariants = (h = eqs.H,), kwargs...) end -function ODEProblem(lsys::DegenerateLagrangianSystem, tspan::Tuple, tstep::Real, ics::NamedTuple; kwargs...) +function ODEProblem(lsys::DegenerateLagrangianSystem, tspan::Tuple, tstep::Real, ics...; kwargs...) eqs = functions(lsys) - ODEProblem(eqs.ẋ, tspan, tstep, ics; invariants = (h = eqs.H,), kwargs...) -end - -function ODEProblem(lsys::DegenerateLagrangianSystem, tspan::Tuple, tstep::Real, q₀::StateVariable; kwargs...) - ODEProblem(lsys, tspan, tstep, (q = q₀, ); kwargs...) -end - -function ODEProblem(lsys::DegenerateLagrangianSystem, tspan::Tuple, tstep::Real, q₀::AbstractArray; kwargs...) - ODEProblem(lsys, tspan, tstep, StateVariable(q₀); kwargs...) + ODEProblem(eqs.ẋ, tspan, tstep, ics...; invariants = (h = eqs.H,), kwargs...) end function LODE(lsys::DegenerateLagrangianSystem; v̄ = functions(lsys).v, f̄ = functions(lsys).f, kwargs...) eqs = functions(lsys) - LODE(eqs.ϑ, eqs.f, eqs.g, eqs.ω, eqs.L; v̄ = v̄, f̄ = f̄, invariants = (h = eqs.H,), kwargs...) + LODE(eqs.ϑ, eqs.f, eqs.g, eqs.ω, eqs.L; v̄ = v̄, f̄ = f̄, invariants = (h = (t,q,v,params) -> eqs.H(t,q,params),), kwargs...) end -function LODEProblem(lsys::DegenerateLagrangianSystem, tspan::Tuple, tstep::Real, ics::NamedTuple; v̄ = functions(lsys).v, f̄ = functions(lsys).f, kwargs...) +function LODEProblem(lsys::DegenerateLagrangianSystem, tspan::Tuple, tstep::Real, ics...; v̄ = functions(lsys).v, f̄ = functions(lsys).f, kwargs...) eqs = functions(lsys) - LODEProblem(eqs.ϑ, eqs.f, eqs.g, eqs.ω, eqs.L, tspan, tstep, ics; v̄ = v̄, f̄ = f̄, invariants = (h = eqs.H,), kwargs...) -end - -function LODEProblem(lsys::DegenerateLagrangianSystem, tspan::Tuple, tstep::Real, q₀::StateVariable, p₀::StateVariable, λ₀::AlgebraicVariable; kwargs...) - ics = (q = q₀, p = p₀, λ = λ₀) - LODEProblem(lsys, tspan, tstep, ics; kwargs...) -end - -function LODEProblem(lsys::DegenerateLagrangianSystem, tspan::Tuple, tstep::Real, q₀::AbstractArray, p₀::AbstractArray, λ₀::AbstractArray = zero(q₀); kwargs...) - _q₀ = StateVariable(q₀) - _p₀ = StateVariable(p₀) - _λ₀ = AlgebraicVariable(λ₀) - LODEProblem(lsys, tspan, tstep, _q₀, _p₀, _λ₀; kwargs...) + LODEProblem(eqs.ϑ, eqs.f, eqs.g, eqs.ω, eqs.L, tspan, tstep, ics...; v̄ = v̄, f̄ = f̄, invariants = (h = (t,q,v,params) -> eqs.H(t,q,params),), kwargs...) end function LDAE(lsys::DegenerateLagrangianSystem; v̄ = functions(lsys).v, f̄ = functions(lsys).f, kwargs...) eqs = functions(lsys) - LDAE(eqs.ϑ, eqs.f, eqs.u, eqs.g, eqs.ϕ, eqs.ū, eqs.ḡ, eqs.ψ, eqs.ω, eqs.L; v̄ = v̄, f̄ = f̄, invariants = (h = eqs.H,), kwargs...) + LDAE(eqs.ϑ, eqs.f, eqs.u, eqs.g, eqs.ϕ, eqs.ū, eqs.ḡ, eqs.ψ, eqs.ω, eqs.L; v̄ = v̄, f̄ = f̄, invariants = (h = (t,q,v,params) -> eqs.H(t,q,params),), kwargs...) end -function LDAEProblem(lsys::DegenerateLagrangianSystem, tspan::Tuple, tstep::Real, ics::NamedTuple; v̄ = functions(lsys).v, f̄ = functions(lsys).f, kwargs...) +function LDAEProblem(lsys::DegenerateLagrangianSystem, tspan::Tuple, tstep::Real, ics...; v̄ = functions(lsys).v, f̄ = functions(lsys).f, kwargs...) eqs = functions(lsys) - LDAEProblem(eqs.ϑ, eqs.f, eqs.u, eqs.g, eqs.ϕ, eqs.ū, eqs.ḡ, eqs.ψ, eqs.ω, eqs.L, tspan, tstep, ics; v̄ = v̄, f̄ = f̄, invariants = (h = eqs.H,), kwargs...) -end - -function LDAEProblem(lsys::DegenerateLagrangianSystem, tspan::Tuple, tstep::Real, q₀::StateVariable, p₀::StateVariable, λ₀::AlgebraicVariable, μ₀::AlgebraicVariable; kwargs...) - ics = (q = q₀, p = p₀, λ = λ₀, μ = μ₀) - LDAEProblem(lsys, tspan, tstep, ics; kwargs...) -end - -function LDAEProblem(lsys::DegenerateLagrangianSystem, tspan::Tuple, tstep::Real, q₀::AbstractArray, p₀::AbstractArray, λ₀::AbstractArray = zero(q₀), μ₀::AbstractArray = zero(λ₀); kwargs...) - _q₀ = StateVariable(q₀) - _p₀ = StateVariable(p₀) - _λ₀ = AlgebraicVariable(λ₀) - _μ₀ = AlgebraicVariable(μ₀) - LDAEProblem(lsys, tspan, tstep, _q₀, _p₀, _λ₀, _μ₀; kwargs...) + LDAEProblem(eqs.ϑ, eqs.f, eqs.u, eqs.g, eqs.ϕ, eqs.ū, eqs.ḡ, eqs.ψ, eqs.ω, eqs.L, tspan, tstep, ics...; v̄ = v̄, f̄ = f̄, invariants = (h = (t,q,v,params) -> eqs.H(t,q,params),), kwargs...) end diff --git a/test/lagrangian_lotka_volterra.jl b/test/lagrangian_lotka_volterra.jl index b2fa610..49aa95f 100644 --- a/test/lagrangian_lotka_volterra.jl +++ b/test/lagrangian_lotka_volterra.jl @@ -37,6 +37,7 @@ end t₀, q₀, v₀ = (0.0, [2.0, 1.0], [0.5, 2.0]) p₀ = zero(v₀) +λ₀ = zero(v₀) tspan = (0.0, 1.0) tstep = 0.1 @@ -55,6 +56,8 @@ params_alt = ( b₂ = 1.0, ) +p̃(p₀, t₀, q₀, v₀, params) + # Symbolic variables and parameters @@ -117,7 +120,7 @@ f̃(ṗ₂, t₀, q₀, v₀, params) @test_nowarn ODEProblem(deg_lag_sys, tspan, tstep, q₀; parameters = params) @test_nowarn LODE(deg_lag_sys) -@test_nowarn LODEProblem(deg_lag_sys, tspan, tstep, q₀, v₀; parameters = params) +@test_nowarn LODEProblem(deg_lag_sys, tspan, tstep, q₀, p₀; parameters = params) @test_nowarn LDAE(deg_lag_sys) -@test_nowarn LDAEProblem(deg_lag_sys, tspan, tstep, q₀, v₀; parameters = params) +@test_nowarn LDAEProblem(deg_lag_sys, tspan, tstep, q₀, p₀, λ₀; parameters = params)