Skip to content

Solving a system with initial condition of the form $f(0) = g(0)$ #1911

@natema

Description

@natema

In some cases when solving a model we have a differential-algebraic system of equations where two variables and have an initial condition of the form .
We created a thread in the Julia forum to ask how to input such initial condition in ModelingToolkit, and it was suggested to open an issue here.

Here's a MWE which returns an error:

using ModelingToolkit, DifferentialEquations

@variables t
D = Differential(t)

function system_f(; name)
    @variables f(t) g(t)
    eqs = [D(f) ~ g - f]
    ODESystem(eqs; name)
end

function system_g(; name)
    @variables g(t)
    eqs = [g ~ t]
    ODESystem(eqs; name)
end

@named f = system_f()
@named g = system_g()

connected_eqs = [
    f.g ~ g.g,
]

@named _model = ODESystem(connected_eqs, t)
@named model = compose(_model, [f, g])

model = structural_simplify(model)
prob = ODEProblem(model, [f.f => g.g], (0.0, 10.0))
sol = solve(prob)

If we replace the last two lines in the above block with the following ones, the code works, but the solution is clearly a workaround that could be improved:

pre_prob = ODEProblem(model, [f.f=>42,g.g=>42], (0.0, 10.0)) 
pre_sol = solve(pre_prob)
prob = ODEProblem(model, [f.f=>pre_sol[g.g, 1],g.g=>pre_sol[g.g, 1]], (0.0, 10.0))
sol = solve(prob)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions