Skip to content

Commit ebbcfee

Browse files
committed
Add Modelica compatible generate_connection_equations
1 parent acadb7e commit ebbcfee

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/systems/connectors.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,42 @@ end
359359
{<load.n.v, outside>, <load.resistor.n.v, inside>}
360360
=#
361361

362+
function Base.merge(csets::AbstractVector{<:ConnectionSet})
363+
mcsets = ConnectionSet[]
364+
# FIXME: this is O(m n^3)
365+
for cset in csets
366+
idx = findfirst(mcset->any(s->any(z->isequal(z, s), cset.set), mcset.set), mcsets)
367+
if idx === nothing
368+
push!(mcsets, cset)
369+
else
370+
union!(mcsets[idx].set, cset.set)
371+
end
372+
end
373+
mcsets
374+
end
375+
376+
function generate_connection_equations(csets::AbstractVector{<:ConnectionSet})
377+
eqs = Equation[]
378+
for cset in csets
379+
vtype = get_connection_type(cset.set[1][1])
380+
vtype === Stream && continue
381+
if vtype === Flow
382+
rhs = 0
383+
for (v, isouter) in cset.set
384+
rhs += isouter ? -v : v
385+
end
386+
push!(eqs, 0 ~ rhs)
387+
else # Equality
388+
base = cset.set[1][1]
389+
for i in 2:length(cset.set)
390+
v = cset.set[i][1]
391+
push!(eqs, base ~ v)
392+
end
393+
end
394+
end
395+
eqs
396+
end
397+
362398
function expand_connections(sys::AbstractSystem; debug=false, tol=1e-10,
363399
rename=Ref{Union{Nothing,Tuple{Symbol,Int}}}(nothing), stream_connects=[])
364400
subsys = get_systems(sys)

0 commit comments

Comments
 (0)