Skip to content

MTK -- "infection" from one model to another? #3110

@B-LIE

Description

@B-LIE

Can one model "inherit" initial values from another❓

I create two models: one with default initial unknown, the other without. The model without default initial unknown gives a warning about under-determined initial values, but gives exactly the same result as the other one. WHY?

using ModelingToolkit, ModelingToolkitStandardLibrary.Blocks
using ModelingToolkit: t_nounits as t, D_nounits as Dt
using OrdinaryDiffEq
using Plots, LaTeXStrings
# 
# 1. Parameters
pars = @parameters begin 
    ρ=1,    [description = "Liquid density"]
    A=5,    [description = "Cross sectional tank area"]
    K=5,    [description = "Effluent valve constant"]
    h_ς=3,  [description = "Scaling level in valve model"]
end
# 2. Dependent variables 
vars = @variables begin
    (m(t)=1.5*ρ*A),     [description = "Liquid mass"]
    md_i(t),            [description = "Influent mass flow rate"]
    md_e(t),            [description = "Effluent mass flow rate"]
    V(t),               [description = "Liquid volume"]
    h(t),               [description = "Liquid level"]
end
# 3. Dependent variables -- without default initial value
vars_0 = @variables begin
    m(t),           [description = "Liquid mass"]
    md_i(t),            [description = "Influent mass flow rate"]
    md_e(t),            [description = "Effluent mass flow rate"]
    V(t),               [description = "Liquid volume"]
    h(t),               [description = "Liquid level"]
end
# 
eqs = [
    Dt(m) ~ md_i-md_e,
    m ~ ρ*V,
    V ~ A*h,
    md_e ~ K*sqrt(h/h_ς)
 ]
# 
@named tank = ODESystem(eqs, t, vars, pars)    # Acausal tank model, i.e., without defined input functions
@named tank_0 = ODESystem(eqs, t, vars_0, pars)    # Acausal tank model, i.e., without defined input functions
# 
@register_symbolic md(t)
# 
eqs_i = [md_i ~ md(t)]

@named tank_i = ODESystem(eqs_i, t)

tank_bal = extend(tank,tank_i)
tank_bal_0 = extend(tank_0,tank_i)

tanksys = structural_simplify(tank_bal)
tanksys_0 = structural_simplify(tank_bal_0)
# 
tspan = (0,10)
md(t) = 2 + 0.1*sin(2pi*t/4)

prob = ODEProblem(tanksys, [], tspan)

Here, creation of prob gives no messages. Next,

julia> prob_0 = ODEProblem(tanksys_0,[],tspan)
┌ Warning: Initialization system is underdetermined. 0 equations for 1 unknowns. Initialization will default to using least squares. To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true
└ @ ModelingToolkit [C:\Users\Bernt_Lie\.julia\packages\ModelingToolkit\OYoU3\src\systems\diffeqs\abstractodesystem.jl:1581](file:///C:/Users/Bernt_Lie/.julia/packages/ModelingToolkit/OYoU3/src/systems/diffeqs/abstractodesystem.jl:1581)

ODEProblem with uType Vector{Float64} and tType Int64. In-place: true
timespan: (0, 10)
u0: 1-element Vector{Float64}:
 0.0

NOTE that u0 seems to be set to 0.

However, when I solve prob_0 (in spite of warning), I get exactly the same result as when solving prob:
image

Is the reason for this apparent "infection" of initial value from one problem (prob) to the other (prob_0) that the variable names are in the global namespace, and thus are the same somehow??

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions