SciML / ModelingToolkit.jl

An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
https://mtk.sciml.ai/dev/
Other
1.41k stars 203 forks source link

@mtkmodel gives "LoadError: Could not parse 0 of component..." #2415

Open bradcarman opened 7 months ago

bradcarman commented 7 months ago

The following code gives the error LoadError: Could not parse 0 of component vol. Note: `MassVolume() is given to show the working version, when trying to recreate this with @mtkmodel the error is given.

using ModelingToolkitStandardLibrary.Mechanical.Translational
using ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible
using ModelingToolkitStandardLibrary.Blocks

mass_flow_fun(t) = cos(2π*10*t)

function MassVolume(; name)

    pars = @parameters begin
        A = 0.01 #m²
        x₀ = 1.0 #m
        M = 10_000 #kg
        g = 9.807 #m/s²
        amp = 5e-2 #m
        f = 15 #Hz   
        p_int=M*g/A
    end
    vars = []
    systems = @named begin
        mass = Mass(;m=M)
        vol = DynamicVolume(0; p_int, area=A, x_int=x₀, x_max=10*x₀)
        mass_flow = MassFlow(;p_int)
        mass_flow_input = TimeVaryingFunction(;f = mass_flow_fun)
    end

    eqs = [
        connect(mass.flange, vol.flange)
        connect(vol.port, mass_flow.port)
        connect(mass_flow.dm, mass_flow_input.output)
    ]

    return ODESystem(eqs, t, vars, pars; systems, name)
end

@named odesys = MassVolume()

@mtkmodel MassVolumeMTK begin
    @parameters begin
        A = 0.01 #m²
        x₀ = 1.0 #m
        M = 10_000 #kg
        g = 9.807 #m/s²
        amp = 5e-2 #m
        f = 15 #Hz   
        p_int=M*g/A
    end
    @components begin
        mass = Mass(;m=M)
        vol = DynamicVolume(0; p_int, area=A, x_int=x₀, x_max=10*x₀)
        mass_flow = MassFlow(;p_int)
        mass_flow_input = TimeVaryingFunction(;f = mass_flow_fun)
    end
    @equations begin
        connect(mass.flange, vol.flange)
        connect(vol.port, mass_flow.port)
        connect(mass_flow.dm, mass_flow_input.output)
    end
end

Currently using ModelingToolkit v8.75.0 with Julia v1.10.0

ChrisRackauckas commented 7 months ago

vol = DynamicVolume(0; p_int, area=A, x_int=x₀, x_max=10*x₀) that 0 should be a keyword argument?

bradcarman commented 7 months ago

No it's a function argument (N). The function signature is:

@component function DynamicVolume(N, add_inertia = true, reversible = false;
    p_int,
    area,
    x_int = 0,
    x_max,
    x_min = 0,
    x_damp = x_min,
    direction = +1,

    # Tube
    perimeter = 2 * sqrt(area * pi),
    shape_factor = 64,
    head_factor = 1,

    # Valve
    Cd = 1e2,
    Cd_reverse = Cd,
    minimum_area = 0,
    name)