ModelingToolkit: Different ways to access array of variables

Hello,

Let’s consider a general function that aims at being a reusable pin/connector of arrays of variables.

using ModelingToolkit

function arrayConnection(;name, discretizations = 4)

	@parameters t
	@variables x[1:discretizations](t)

	ODESystem(Equation[], t, x, []; name=name)

end

Define two instances of this general connection:

@named connection1 = arrayConnection()
@named connection2 = arrayConnection()

Because of the issue mentioned in https://github.com/SciML/ModelingToolkit.jl/issues/908, it is not possible to access the variables directly with their name. One needs to use either unicode variables or states. However, the output is different. For instance,

eqs1 = [connection1.states[1] ~ connection2.states[1]]

leads to:

1-element Array{Equation,1}:
 x₁(t) ~ x₁(t)

which is a trivial solution, while:

eqs2 = [connection1.x₁ ~ connection2.x₁]

generates the equality one could expect:

1-element Array{Equation,1}:
 connection1₊x₁(t) ~ connection2₊x₁(t)

At this point, I have two questions:

  1. I guess the answer is yes, but just to confirm. Are these two equations actually different within ModelingToolkit, i.e. eqs1 != eqs2?

  2. Assume one wants a general function to connect two, or more, of these connections, e.g.:

function connect(ps...)

	discretizations = Int(length(ps[1].states) )

	equations = [
		         [0 ~ sum(p -> p.states[i], ps) for i in 1:discretizations]
		        ]

	return equations

end

This code would lead to trivial equalities as those as in eqs1, which do not add any information to the system and do not actually connect both connectors. Is there a way to loop over the unicode variables so the subscript is i and the connections resembles that in eqs2?

yes, one is namespaced while the other isn’t.

Array variables are being overhauled.

This whole issue will go away.

1 Like

I see that these branches are merged now @ChrisRackauckas.

You also mentioned in this link, Sub plus (₊) instead of dot in the dot notation in equations displayed by ModelingToolkit - #4 by zdenek_hurak, that these changed were going to be released ASAP. Are these changes already out?

Is there a way in Julia to get notifications of releases/changes in particular packages?

Yes, the symbolic array changes are already out in the wild.

2 Likes