Giter Club home page Giter Club logo

Comments (2)

AayushSabharwal avatar AayushSabharwal commented on August 25, 2024

The problem here is that in MassSpringDamper, there is no @parameters damping = damping like there is for the other parameters. This is because a variable can't have a "double ParentScope" metadata.

When @named wheel = MassSpringDamper(; mass=4*wheel_mass, gravity, damping=wheel_damping, stiffness=wheel_stiffness, initial_position=0.5) is done, wheel_damping gets its SymScope metadata set to ParentScope, indicating that it is a variable from the parent of the system it is being passed to (and thus shouldn't be namespaced). If it is aliased to a new parameter inside MassSpringDamper via @parameters damping = damping, then damping doesn't have the SymScope metadata, so when it is passed to Damper, it gets assigned ParentScope. Without the aliasing,ParentScope can't be applied twice to wheel_damping which is why Damper thinks the variable is from MassSpringDamper and not System.

The long explanation is only to document the error and find a solution. It is definitely not something I'd expect a user to be concerned with, and we should either throw an error in such a scenario or handle it properly. I think DelayParentScope should be used here.

from modelingtoolkit.jl.

baggepinnen avatar baggepinnen commented on August 25, 2024

Here's another example that throws the same error, but I'm not sure if the same explanation as to why applies

using ModelingToolkit
using ModelingToolkitStandardLibrary: Blocks
gravity = 9.81
gain_u1 = 0.89 / 1.4
d0 = 70
d1 = 17
n0 = 55
@named motor_dynamics = Blocks.FirstOrder(T = 0.001)
x0 = [0.85, 1, π/12, π/2]
@parameters t
@variables  u(t)[1:2]=0
u = collect(u)
@variables y(t)=0.85 v(t)=1 ϕ(t)=π/12 ω(t)=π/2
D = Differential(t)
eqs = [
    D(y) ~ v,
    D(v) ~ -gravity + gain_u1 * cos(ϕ)*(motor_dynamics.output.u + gravity/gain_u1),
    D(ϕ) ~ ω,
    D(ω) ~ -d0 * ϕ - d1 * ω + n0 * u[2],
    motor_dynamics.input.u ~ u[1]
]

@named model = ODESystem(eqs, t; systems=[motor_dynamics])

linearize(model, collect(u), [y, v, ϕ, ω])
julia> linearize(model, collect(u), [y, v, ϕ, ω])
ERROR: ArgumentError: v(t) is neither an observed nor an unknown variable.
Stacktrace:
  [1] build_explicit_observed_function(sys::NonlinearSystem, ts::Vector{…}; inputs::Nothing, expression::Bool, output_type::Type, checkbounds::Bool, drop_expr::typeof(RuntimeGeneratedFunctions.drop_expr), ps::Vector{…}, return_inplace::Bool, op::Type, throw::Bool)
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/odesystem.jl:451
  [2] build_explicit_observed_function(sys::NonlinearSystem, ts::Vector{SymbolicUtils.BasicSymbolic{Real}})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/odesystem.jl:378
  [3] observed(sys::NonlinearSystem, sym::Vector{SymbolicUtils.BasicSymbolic{Real}})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/abstractsystem.jl:534
  [4] _getu(sys::NonlinearSystem, ::SymbolicIndexingInterface.NotSymbolic, ::SymbolicIndexingInterface.ScalarSymbolic, sym::Vector{…})
    @ SymbolicIndexingInterface ~/.julia/packages/SymbolicIndexingInterface/A1VUA/src/state_indexing.jl:158
  [5] getu(sys::NonlinearSystem, sym::Vector{SymbolicUtils.BasicSymbolic{Real}})
    @ SymbolicIndexingInterface ~/.julia/packages/SymbolicIndexingInterface/A1VUA/src/state_indexing.jl:32
  [6] linearization_function(sys::ODESystem, inputs::Vector{…}, outputs::Vector{…}; simplify::Bool, initialize::Bool, op::Dict{…}, p::SciMLBase.NullParameters, zero_dummy_der::Bool, initialization_solver_alg::NonlinearSolve.GeneralizedFirstOrderAlgorithm{…}, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/abstractsystem.jl:1965
  [7] linearization_function
    @ ~/.julia/dev/ModelingToolkit/src/systems/abstractsystem.jl:1867 [inlined]
  [8] linearize(sys::ODESystem, inputs::Vector{…}, outputs::Vector{…}; op::Dict{…}, t::Float64, allow_input_derivatives::Bool, zero_dummy_der::Bool, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/abstractsystem.jl:2325
  [9] linearize(sys::ODESystem, inputs::Vector{Num}, outputs::Vector{Num})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/abstractsystem.jl:2321
 [10] top-level scope
    @ REPL[3]:1
Some type information was truncated. Use `show(err)` to see complete types.

from modelingtoolkit.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.