-
-
Notifications
You must be signed in to change notification settings - Fork 208
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
Array of components #2674
Comments
@ven-k is the array form documented? |
Yes it's the examples here: https://docs.sciml.ai/ModelingToolkit/stable/basics/MTKModel_Connector/#What-can-an-MTK-Model-definition-have? The relevant bits (from ModelC example): @mtkmodel ModelC begin
@structural_parameters begin
N = 2
end
:
@components begin
model_a = ModelA(; k_array)
model_array_a = [ModelA(; k = i) for i in 1:N]
model_array_b = for i in 1:N
k = i^2
ModelA(; k)
end
end
:
end For |
Thanks for answer! Useful, but it only addresses part of my question, and misses the most important part (marked with red):
Creation of vectors:
The example you refer to illustrates how a vector of models, `model_array_a` and `model_array_b` can be created. I missed that. Yingbo has used a third way, e.g., `model_array_c[1:N] = ModelA()` in his presentations. Perhaps this third way is only suitable when all model instantiations are equal...
Reference to vector elements:
You write:
The relevant bits (from ModelC example):
@mtkmodel ModelC begin
@structural_parameters begin
N = 2
end
:
@components begin
model_a = ModelA(; k_array)
model_array_a = [ModelA(; k = i) for i in 1:N]
model_array_b = for i in 1:N
k = i^2
ModelA(; k)
end
end
:
end
For @nAmed model_c = ModelC() ,the subcomponents can be then accessed as model_c.model_array_a_1, model_c.model_array_a_2 and so on.
This is useful. However, it doesn't answer my question, which was: how do I address the elements of, say, model_a within the ModelC definition. In other words, suppose I add:
...
@components begin
model_a = ModelA(; k_array)
model_array_a = [ModelA(; k = i) for i in 1:N]
model_array_b = for i in 1:N
k = i^2
ModelA(; k)
end
end
@equations begin
for i in 1:N
model_array_a[i].k ~ 3
end
end
:
end
OK – not a perfect example since `k` is a parameter; in my case it would be a variable.
What I have inserted in red above does not work. Should it be `model_array_a_i` instead? Or something else?
BR,
Bernt
…________________________________
From: Christopher Rackauckas ***@***.***>
Sent: Monday, April 29, 2024 8:04 AM
To: SciML/ModelingToolkit.jl ***@***.***>
Cc: Bernt Lie ***@***.***>; Author ***@***.***>
Subject: Re: [SciML/ModelingToolkit.jl] Array of components (Issue #2674)
Closed #2674<#2674> as completed.
—
Reply to this email directly, view it on GitHub<#2674 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AM5VM72TFKURHHYMTHC6PTLY7XPFLAVCNFSM6AAAAABG4EHJVWVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJSGYZTOOJSGQZTCMQ>.
You are receiving this because you authored the thread.
T
|
To make it simpler, consider my slightly modified code from the documentation, where my changes are highlighted in *red*:
using ModelingToolkit
using ModelingToolkit: t
@mtkmodel ModelA begin
@parameters begin
k
k_array[1:2]
end
end
@mtkmodel ModelB begin
@parameters begin
p1 = 1.0, [description = "Parameter of ModelB"]
p2 = 1.0, [description = "Parameter of ModelB"]
end
end
@mtkmodel ModelC begin
# @ICON "https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png"
@Constants begin
c::Int = 1, [description = "Example constant."]
end
@structural_parameters begin
f = sin
N = 2
end
begin
v_var = 1.0
end
@variables begin
v(t) = v_var
v_array(t)[1:2, 1:3]
v_for_defaults(t)
end
@extend ModelB(; p1)
@components begin
model_a = ModelA(; k_array = [1, 2])
model_array_a = [ModelA(; k = i) for i in 1:N]
model_array_b = for i in 1:N
k = i^2
ModelA(; k)
end
end
@equations begin
model_a.k ~ f(v)
model_array_a[1:N].k ~ f(v)
end
@defaults begin
v_for_defaults => 2.0
end
end
;
The addition of model_array_a[1:N].k ~ f(v) causes the error message below [same error message if I write .~ ]::
@mtkbuild modelc = ModelC()
type Array has no field k Stacktrace: [1] getproperty @ .\Base.jl:37<https://untitled+.vscode-resource.vscode-cdn.net/Base.jl:37> [inlined] [2] #__ModelC__#74 @ C:\Users\Bernt\.julia\packages\UnPack\EkESO\src\UnPack.jl:1396 [inlined] [3] __ModelC__(; name::Symbol, v_array::Nothing, p1::Nothing, N::Int64, model_array_a__k::Nothing, f::typeof(sin), v::Nothing, model_a__k_array::Nothing, v_for_defaults::Nothing, model_array_b__k::Nothing) @ Main .\none:0<https://untitled+.vscode-resource.vscode-cdn.net/none:0> [4] (::ModelingToolkit.Model{typeof(__ModelC__), Dict{Symbol, Any}})(; ***@***.***{name::Symbol}) @ ModelingToolkit C:\Users\Bernt\.julia\packages\ModelingToolkit\QP8aH\src\systems\model_parsing.jl:25 [5] top-level scope @ C:\Users\Bernt\.julia\packages\ModelingToolkit\QP8aH\src\systems\abstractsystem.jl:1396
…________________________________
From: Bernt Lie ***@***.***>
Sent: Monday, April 29, 2024 10:25 AM
To: SciML/ModelingToolkit.jl ***@***.***>; SciML/ModelingToolkit.jl ***@***.***>
Cc: Author ***@***.***>
Subject: Re: [SciML/ModelingToolkit.jl] Array of components (Issue #2674)
Thanks for answer! Useful, but it only addresses part of my question, and misses the most important part (marked with red):
Creation of vectors:
The example you refer to illustrates how a vector of models, `model_array_a` and `model_array_b` can be created. I missed that. Yingbo has used a third way, e.g., `model_array_c[1:N] = ModelA()` in his presentations. Perhaps this third way is only suitable when all model instantiations are equal...
Reference to vector elements:
You write:
The relevant bits (from ModelC example):
@mtkmodel ModelC begin
@structural_parameters begin
N = 2
end
:
@components begin
model_a = ModelA(; k_array)
model_array_a = [ModelA(; k = i) for i in 1:N]
model_array_b = for i in 1:N
k = i^2
ModelA(; k)
end
end
:
end
For @nAmed model_c = ModelC() ,the subcomponents can be then accessed as model_c.model_array_a_1, model_c.model_array_a_2 and so on.
This is useful. However, it doesn't answer my question, which was: how do I address the elements of, say, model_a within the ModelC definition. In other words, suppose I add:
...
@components begin
model_a = ModelA(; k_array)
model_array_a = [ModelA(; k = i) for i in 1:N]
model_array_b = for i in 1:N
k = i^2
ModelA(; k)
end
end
@equations begin
for i in 1:N
model_array_a[i].k ~ 3
end
end
:
end
OK – not a perfect example since `k` is a parameter; in my case it would be a variable.
What I have inserted in red above does not work. Should it be `model_array_a_i` instead? Or something else?
BR,
Bernt
________________________________
From: Christopher Rackauckas ***@***.***>
Sent: Monday, April 29, 2024 8:04 AM
To: SciML/ModelingToolkit.jl ***@***.***>
Cc: Bernt Lie ***@***.***>; Author ***@***.***>
Subject: Re: [SciML/ModelingToolkit.jl] Array of components (Issue #2674)
Closed #2674<#2674> as completed.
—
Reply to this email directly, view it on GitHub<#2674 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AM5VM72TFKURHHYMTHC6PTLY7XPFLAVCNFSM6AAAAABG4EHJVWVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJSGYZTOOJSGQZTCMQ>.
You are receiving this because you authored the thread.
T
|
(My color coding disappeared when transferring my e-mail to the GitHub system...) |
How can I iterate over that array of components? Eg. I can write connect(model_c.model_array_a_1, other_component)
connect(model_c.model_array_a_2, other_component)
connect(model_c.model_array_a_3, other_component)
connect(model_c.model_array_a_4, other_component) but that's terrible. Can I somehow write a for loop? |
|
This does not work for me. MWE: using ModelingToolkit # v9.48.0
using ModelingToolkit: t_nounits as t
@mtkmodel MyComponent begin
@variables begin
x(t)
end
@equations begin
x ~ 10
end
end
@mtkmodel Moo begin
@components begin
some_comp = MyComponent()
comp_array = [MyComponent() for i in 1:4]
end
end
@named moo = Moo()
moo.some_comp # works
moo.comp_array_1 # works
moo.comp_array # ArgumentError: System moo: variable comp_array does not exist This is the same problem highlighted in @B-LIE's comment above (#2674 (comment)) EDIT: first version accidentally used |
The component-array is splat before adding them as subsystems. So Note that |
Sounds good! Is there any reason why we couldn't have |
ODESystem constructor doesn't accept Vector{ODESystem} (for subsystem) as input. Also, @mtkmodel Moo begin
@components begin
some_comp = MyComponent()
comp_array = [MyComponent() for i in 1:4]
end
@equations begin
[connect(comp_array[i], some_comp) for i in 1:4]
end
end
comp_array = [getproperty(moo, Symbol(:comp_array_, i)) for i in 1:4] Maybe there can be a helper function to access it. But I fear it will have many non-obvious failing edge cases. If I can come up with a reliable one, I'll add. |
That looks like a complicated change indeed. For context, I wanted to have a component with N connectors. In that case, the using ModelingToolkit
using ModelingToolkit: t_nounits as t
@mtkmodel MyConnector begin
@variables begin
x(t)
end
end
@mtkmodel MilkTank begin
@components begin
connections = [MyConnector() for i in 1:4]
end
end
@mtkmodel Farm begin
@components begin
m1 = MilkTank()
m2 = MilkTank()
end
@equations begin
# (sorry for the meaningless example)
[connect(co1, co2) for (co1, co2) in zip(m1.connections, m2.connections)]...
end
end
@named farm = Farm() # System m1: variable connections does not exist But on further investigation, stream connectors look like a better solution to my problem, so I don't need multiple connectors. Thank you for the input! |
Is there an example of how to use arrays of components in the documentation❓
From a presentation, I've seen the the syntax:
I don't know if this is a standard notation or not. I've tried to use it, but have problems when I add equations where the instances have fields, such...
would not work.
However,
would work.
The text was updated successfully, but these errors were encountered: