Skip to content

Commit

Permalink
Add methods for creating derivatives.
Browse files Browse the repository at this point in the history
  • Loading branch information
michakraus committed Dec 5, 2024
1 parent 02c617b commit c0d17f8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/EulerLagrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ module EulerLagrange


export HamiltonianSystem
export hamiltonian, hamiltonian_variables
export hamiltonian, hamiltonian_variables, hamiltonian_derivatives

include("hamiltonian.jl")


export LagrangianSystem, DegenerateLagrangianSystem
export lagrangian, lagrangian_variables
export lagrangian, lagrangian_variables, lagrangian_derivatives

include("lagrangian_common.jl")
include("lagrangian.jl")
Expand Down
28 changes: 18 additions & 10 deletions src/hamiltonian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ function substitute_hamiltonian_variables(equs::NamedTuple, q, p)
NamedTuple{keys(equs)}(Tuple(substitute_hamiltonian_variables(eq, q, p) for eq in equs))
end


function hamiltonian_variables(dimension::Int)
t = parameter(:t)
@variables (q(t))[1:dimension]
@variables (p(t))[1:dimension]
return (t,q,p)
end

function hamiltonian_derivatives(t, q, p)
Dt = Differential(t)
Dq = collect(Differential.(q))
Dp = collect(Differential.(p))

return (Dt, Dq, Dp)
end


"""
HamiltonianSystem
"""
Expand All @@ -32,9 +49,7 @@ struct HamiltonianSystem
@variables Q[axes(q,1)]
@variables P[axes(p,1)]

Dt = Differential(t)
Dq = collect(Differential.(q))
Dp = collect(Differential.(p))
Dt, Dq, Dp = hamiltonian_derivatives(t, q, p)

Hs = dosimplify ? simplify(H) : H

Expand Down Expand Up @@ -90,13 +105,6 @@ function Base.show(io::IO, hsys::HamiltonianSystem)
# end
end

function hamiltonian_variables(dimension::Int)
t = parameter(:t)
@variables (q(t))[1:dimension]
@variables (p(t))[1:dimension]
return (t,q,p)
end


function HODE(lsys::HamiltonianSystem; kwargs...)
eqs = functions(lsys)
Expand Down
5 changes: 2 additions & 3 deletions src/lagrangian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ struct LagrangianSystem
@variables F[axes(x, 1)]
@variables Λ[axes(v, 1)]

Dt = Differential(t)
Dx = collect(Differential.(x))
Dv = collect(Differential.(v))
Dt, Dx, Dv = lagrangian_derivatives(t, x, v)

Dz = vcat(Dx,Dv)
= collect(Dt.(x))
= collect(Dt.(p))
Expand Down
8 changes: 8 additions & 0 deletions src/lagrangian_common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@ function lagrangian_variables(dimension::Int)

return (t,x,v)
end

function lagrangian_derivatives(t, x, v)
Dt = Differential(t)
Dx = collect(Differential.(x))
Dv = collect(Differential.(v))

return (Dt, Dx, Dv)
end
5 changes: 2 additions & 3 deletions src/lagrangian_degenerate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ struct DegenerateLagrangianSystem
@variables F[axes(x, 1)]
@variables Λ[axes(v, 1)]

Dt = Differential(t)
Dx = collect(Differential.(x))
Dv = collect(Differential.(v))
Dt, Dx, Dv = lagrangian_derivatives(t, x, v)

= collect(Dt.(x))

K = θ v
Expand Down

0 comments on commit c0d17f8

Please sign in to comment.