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.43k stars 209 forks source link

`@discrete_events` or `@continuous_events` not working with acausal modeling framework #2849

Closed joseph03choi closed 3 months ago

joseph03choi commented 4 months ago

@discrete_events or @continuous_events can not "see" connector variables from outside components or its own component variables. Example component:

@mtkmodel PIcontroller begin
    @components begin
        readingsIn = Stream()
        signalOut = ValveSignal()
    end
    @variables begin
        I_err(t) = 0
    end
    @parameters begin
        c_BSP = 0.32
        K_b = 0.0
        K_c = 9.0
        K_I = 0.18
        s = 0 # No initial signal to open valve
    end
    @equations begin
        D(I_err) ~ c_BSP - readingsIn.c_B
        signalOut.s ~ s
    end
    @discrete_events begin
        ((mod(t-200,30) == 0) & (t >= 30)) => [s ~ K_b + K_c*(c_BSP - readingsIn.c_B) + K_I*I_err]
    end
end

I'm able to connect all my components, use @mtkbuild, and create an ODEProblem(), but when I get to solve(), I get ERROR: UndefVarError: picontroller₊readingsIn₊c_B not defined. I’ve isolated @discrete_events as the issue here because whenever I replace readingsIn.c_B in the @discrete_events block with some number, everything is able to run properly (although I get the wrong solution of course).

Similarly, @discrete_events or @continuous_events can not see its own component variables unless it has a derivative term. For example:

@mtkmodel PIcontroller begin
    @components begin
        readingsIn = Stream()
        signalOut = ValveSignal()
    end
    @variables begin
        I_err(t) = 0
        B(t) # Trying to use component variable
    end
    @parameters begin
        c_BSP = 0.32
        K_b = 0.0
        K_c = 9.0
        K_I = 0.18
        s = 0 # No initial signal to open valve
    end
    @equations begin
        B ~ readingsIn.c_B
        D(I_err) ~ c_BSP - B
        signalOut.s ~ s
    end
    @discrete_events begin
        ((mod(t-200,30) == 0) & (t >= 30)) => [s ~ K_b + K_c*(c_BSP - B) + K_I*I_err]
    end
end

@discrete_events can't see the variable B, and I get ERROR: UndefVarError: picontroller₊B not defined. @discrete_events can only see B if I do something like D(B) ~ 0, but then I can’t do B ~ readingsIn.c_B because @mtkbuild will tell me I have too many equations.

AayushSabharwal commented 3 months ago

Please provide a complete MWE. There isn't enough code here to reproduce the problem (e.g. PIcontroller references Stream and ValveSignal which aren't defined here)

ChrisRackauckas commented 3 months ago

Closing unless code shows up that allows for reproducing. Indeed I have no idea how to get the error that is being referenced here.