Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ODEProblem method for type DifferentialEquation #56

Closed
oameye opened this issue Oct 11, 2022 · 3 comments · Fixed by #300
Closed

ODEProblem method for type DifferentialEquation #56

oameye opened this issue Oct 11, 2022 · 3 comments · Fixed by #300
Labels
good first issue Good for newcomers new feature New feature or request

Comments

@oameye
Copy link
Member

oameye commented Oct 11, 2022

To look at the time evolution for the type HarmonicEquation you define a method for the function ODEProblem which transform the HarmonicEquation into the DifferentialEquations.ODEProblem type.

image

It would be nice to have the same for the DifferentialEquation type.

@jkosata jkosata added the new feature New feature or request label Oct 11, 2022
@jkosata
Copy link
Member

jkosata commented Oct 11, 2022

We discussed this with @jdelpino at some point. One difficulty is that DifferentialEquations.jl assumes you break down an Nth order ODE into N first order ODEs, which is probably hard to automate. It does however have a method to parse a 2nd order ODE. We could have this functionality for orders 1 and 2 only, which covers all known use cases so far.

@oameye
Copy link
Member Author

oameye commented Oct 11, 2022

ModelingToolkit.jl has a function for breaking down an Nth order ODE into N first order ODEs.
image

@oameye
Copy link
Member Author

oameye commented Oct 23, 2022

Adding ModelingToolkit.jl to our dependencies would extend our package loading time by ~ 5sec on my AMD Ryzen 5 5600X processor
image
image

However, examining the ModelingToolkit.jl package, it seems that we do not need to add ModelingToolkit.jl as a dependency. The ode_order_lowering function only seem to use functions in the Symbolics.jl package

"""
$(TYPEDSIGNATURES)

Takes a Nth order ODESystem and returns a new ODESystem written in first order
form by defining new variables which represent the N-1 derivatives.
"""
function ode_order_lowering(sys::ODESystem)
    iv = get_iv(sys)
    eqs_lowered, new_vars = ode_order_lowering(equations(sys), iv, states(sys))
    @set! sys.eqs = eqs_lowered
    @set! sys.states = new_vars
    return sys
end

function ode_order_lowering(eqs, iv, states)
    var_order = OrderedDict{Any, Int}()
    D = Differential(iv)
    diff_eqs = Equation[]
    diff_vars = []
    alge_eqs = Equation[]

    for (i, eq) in enumerate(eqs)
        if !isdiffeq(eq)
            push!(alge_eqs, eq)
        else
            var, maxorder = var_from_nested_derivative(eq.lhs)
            maxorder > get(var_order, var, 1) && (var_order[var] = maxorder)
            var′ = lower_varname(var, iv, maxorder - 1)
            rhs′ = diff2term(eq.rhs)
            push!(diff_vars, var′)
            push!(diff_eqs, D(var′) ~ rhs′)
        end
    end

    for (var, order) in var_order
        for o in (order - 1):-1:1
            lvar = lower_varname(var, iv, o - 1)
            rvar = lower_varname(var, iv, o)
            push!(diff_vars, lvar)

            rhs = rvar
            eq = Differential(iv)(lvar) ~ rhs
            push!(diff_eqs, eq)
        end
    end

    # we want to order the equations and variables to be `(diff, alge)`
    return (vcat(diff_eqs, alge_eqs), vcat(diff_vars, setdiff(states, diff_vars)))
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers new feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants