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

Dropped Variable #3311

Closed
bradcarman opened this issue Jan 10, 2025 · 3 comments · Fixed by #3324
Closed

Dropped Variable #3311

bradcarman opened this issue Jan 10, 2025 · 3 comments · Fixed by #3324
Labels
bug Something isn't working

Comments

@bradcarman
Copy link
Contributor

The following produces the error: ERROR: UndefVarError: vol2₊rho not defined, which as can be seen is defined.
 

using ModelingToolkit
using ModelingToolkit: D_nounits as D, t_nounits as t
import ModelingToolkitStandardLibrary.Blocks as B
import ModelingToolkitStandardLibrary.Mechanical.Translational as T
import ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible as IC
using OrdinaryDiffEq
using Plots


liquid_pressure(rho, rho_0, bulk) = (rho/rho_0 - 1)*bulk
gas_pressure(rho, rho_0, p_gas, rho_gas) = rho * ((0 - p_gas) / (rho_0 - rho_gas))
full_pressure(rho, rho_0, bulk, p_gas, rho_gas) = ifelse( rho >= rho_0, liquid_pressure(rho, rho_0, bulk), gas_pressure(rho, rho_0, p_gas, rho_gas))
    

@component function Volume(;

    #parameters
    area,
    direction = +1,
    x_int,
    name)

    pars = @parameters begin
        area = area
        x_int = x_int
        rho_0 = 1000
        bulk = 1e9
        p_gas = -1000
        rho_gas = 1
    end

    vars = @variables begin
        x(t)=x_int
        dx(t), [guess=0]
        p(t), [guess=0]
        f(t), [guess=0]
        rho(t), [guess=0]
        m(t), [guess=0]
        dm(t), [guess=0]
    end

    systems = @named begin
        port = IC.HydraulicPort()
        flange = T.MechanicalPort()
    end

    eqs = [
        # connectors
        port.p ~ p
        port.dm ~ dm
        flange.v * direction ~ dx
        flange.f * direction ~ -f

        # differentials
        D(x) ~ dx
        D(m) ~ dm

        # physics
            p ~ full_pressure(rho, rho_0, bulk, p_gas, rho_gas)

        f ~ p * area
        m ~ rho * x * area]

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

systems = @named begin
    fluid = IC.HydraulicFluid(; bulk_modulus = 1e9)

    src1 = IC.Pressure(;)
    src2 = IC.Pressure(;)

    vol1 = Volume(;area=0.01, direction = +1, x_int=0.1)
    vol2 = Volume(;area=0.01, direction = +1, x_int=0.1)

    mass = T.Mass(; m = 10)

    sin1 = B.Sine(; frequency = 0.5, amplitude = +0.5e5, offset = 10e5)
    sin2 = B.Sine(; frequency = 0.5, amplitude = -0.5e5, offset = 10e5)
end

eqs = [connect(fluid, src1.port)
        connect(fluid, src2.port)
        connect(src1.port, vol1.port)
        connect(src2.port, vol2.port)
        connect(vol1.flange, mass.flange, vol2.flange)
        connect(src1.p, sin1.output)
        connect(src2.p, sin2.output)]

initialization_eqs = [
    mass.s ~ 0.0
    mass.v ~ 0.0
]

@mtkbuild sys = ODESystem(eqs, t, [], []; systems, initialization_eqs)
prob = ODEProblem(sys, [], (0, 5))
sol = solve(prob) #ERROR: UndefVarError: `vol2₊rho` not defined

The current versions used are...

(Issue953) pkg> st
Status `C:\Work\Packages\Issue953\Project.toml`

  [961ee093] ModelingToolkit v9.60.0
  [16a59e39] ModelingToolkitStandardLibrary v2.19.0

  [1dea7af3] OrdinaryDiffEq v6.90.1
  [91a5bcdd] Plots v1.40.9

@bradcarman bradcarman added the bug Something isn't working label Jan 10, 2025
@ChrisRackauckas
Copy link
Member

@AayushSabharwal was this covered by the hash consing fixes?

@AayushSabharwal
Copy link
Member

It doesn't seem so. I'm yet to figure out if it's hashconsing related or not, but it does seem like it could be.

@AayushSabharwal
Copy link
Member

This is an SCCNonlinearProblem issue. Passing use_scc=false to ODEProblem is a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants