Skip to content

Model building fails when using @extend multiple times #3578

@zalanzsiboras

Description

@zalanzsiboras

The bug 🐞

When building a system that is an extension of an extended model MTK throws a MethodError.
Note that the model which is a single extension builds successfully.

Expected behavior

MTK should be able to build models that have been extended (with @extend) multiple times.

Minimal Reproducible Example 👇

using ModelingToolkit
using ModelingToolkit: t_nounits as t

@connector Port begin
    T(t)
    m_dot(t), [connect = Flow]
end

@mtkmodel Element1P begin
    @components begin
        port = Port()
    end
    @variables begin
        m_dot(t)
    end
    @equations begin
        m_dot ~ port.m_dot
    end
end

@mtkmodel ExtendedElement1P begin
    @extend Element1P()
    # There could be more components, parameters, variables and/or equations here.
end

@named ext_system = ExtendedElement1P()
# Builds model without a problem.

 
@mtkmodel TwiceExtendedSystem begin
    @extend ExtendedElement1P()
    # There could be even more components, parameters, variables and/or equations here.
end

# However, if an extended system is extended again the model building fails:
@named twice_ext_system = TwiceExtendedSystem()

Error & Stacktrace ⚠️

ERROR: MethodError: no method matching __ExtendedElement1P__(; name::Symbol, port::ModelingToolkit.NoValue, m_dot::ModelingToolkit.NoValue)
This error has been manually thrown, explicitly, so the method may exist but be intentionally marked as unimplemented.

Closest candidates are:
  __ExtendedElement1P__(; name, m_dot) got unsupported keyword argument "port"
   @ Main C:\Users\ZalánZsiborás\.julia\packages\ModelingToolkit\15RQb\src\systems\model_parsing.jl:151

Stacktrace:
 [1] kwerr(kw::@NamedTuple{name::Symbol, port::ModelingToolkit.NoValue, m_dot::ModelingToolkit.NoValue}, args::Function)
   @ Base .\error.jl:165
 [2] (::ModelingToolkit.Model{…})(; kw::@Kwargs{})
   @ ModelingToolkit ~\.julia\packages\ModelingToolkit\15RQb\src\systems\model_parsing.jl:25
 [3] __TwiceExtendedSystem__(; name::Symbol, port::ModelingToolkit.NoValue, m_dot::ModelingToolkit.NoValue)
   @ Main ~\.julia\packages\ModelingToolkit\15RQb\src\systems\model_parsing.jl:151
 [4] __TwiceExtendedSystem__
   @ ~\.julia\packages\ModelingToolkit\15RQb\src\systems\model_parsing.jl:151 [inlined]
 [5] #_#425
   @ ~\.julia\packages\ModelingToolkit\15RQb\src\systems\model_parsing.jl:25 [inlined]
 [6] top-level scope
   @ ~\.julia\packages\ModelingToolkit\15RQb\src\systems\abstractsystem.jl:2272
Some type information was truncated. Use `show(err)` to see complete types.

Environment:

Julia Version 1.11.5
ModelingToolkit v9.73.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions