Skip to content

Commit 177aa93

Browse files
refactor: move Connection and connect here instead of Symbolics
1 parent 56b0c5e commit 177aa93

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

src/ModelingToolkit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ using RuntimeGeneratedFunctions: drop_expr
7272
using Symbolics: degree
7373
using Symbolics: _parse_vars, value, @derivatives, get_variables,
7474
exprs_occur_in, symbolic_linear_solve, build_expr, unwrap, wrap,
75-
VariableSource, getname, variable, Connection, connect,
75+
VariableSource, getname, variable,
7676
NAMESPACE_SEPARATOR, set_scalar_metadata, setdefaultval,
7777
initial_state, transition, activeState, entry, hasnode,
7878
ticksInState, timeInState, fixpoint_sub, fast_substitute,

src/systems/analysis_points.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function renamespace(sys, ap::AnalysisPoint)
176176
end
177177

178178
# create analysis points via `connect`
179-
function Symbolics.connect(in, ap::AnalysisPoint, outs...; verbose = true)
179+
function connect(in, ap::AnalysisPoint, outs...; verbose = true)
180180
return AnalysisPoint() ~ AnalysisPoint(in, ap.name, collect(outs); verbose)
181181
end
182182

@@ -217,11 +217,11 @@ typically is not (unless the model is an inverse model).
217217
- `verbose`: Warn if an input is connected to an output (reverse causality). Silence this
218218
warning if you are analyzing an inverse model.
219219
"""
220-
function Symbolics.connect(in::AbstractSystem, name::Symbol, out, outs...; verbose = true)
220+
function connect(in::AbstractSystem, name::Symbol, out, outs...; verbose = true)
221221
return AnalysisPoint() ~ AnalysisPoint(in, name, [out; collect(outs)]; verbose)
222222
end
223223

224-
function Symbolics.connect(
224+
function connect(
225225
in::ConnectableSymbolicT, name::Symbol, out::ConnectableSymbolicT,
226226
outs::ConnectableSymbolicT...; verbose = true)
227227
allvars = (in, out, outs...)

src/systems/connectors.jl

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,48 @@
11
using Symbolics: StateMachineOperator
2+
3+
"""
4+
$(TYPEDEF)
5+
6+
Struct used to represent a connection equation. A connection equation is an `Equation`
7+
where the LHS is an empty `Connection(nothing)` and the RHS is a `Connection` containing
8+
the connected connectors.
9+
10+
For special types of connections, the LHS `Connection` can contain relevant metadata.
11+
"""
12+
struct Connection
13+
systems::Any
14+
end
15+
16+
Base.broadcastable(x::Connection) = Ref(x)
17+
Connection() = Connection(nothing)
18+
Base.hash(c::Connection, seed::UInt) = hash(c.systems, (0xc80093537bdc1311 % UInt) seed)
19+
Symbolics.hide_lhs(_::Connection) = true
20+
21+
"""
22+
$(TYPEDSIGNATURES)
23+
24+
Connect multiple connectors created via `@connector`. All connected connectors
25+
must be unique.
26+
"""
27+
function connect(sys1::AbstractSystem, sys2::AbstractSystem, syss::AbstractSystem...)
28+
syss = (sys1, sys2, syss...)
29+
length(unique(nameof, syss)) == length(syss) || error("connect takes distinct systems!")
30+
Equation(Connection(), Connection(syss)) # the RHS are connected systems
31+
end
32+
33+
function Base.show(io::IO, c::Connection)
34+
print(io, "connect(")
35+
if c.systems isa AbstractArray || c.systems isa Tuple
36+
n = length(c.systems)
37+
for (i, s) in enumerate(c.systems)
38+
str = join(split(string(nameof(s)), NAMESPACE_SEPARATOR), '.')
39+
print(io, str)
40+
i != n && print(io, ", ")
41+
end
42+
end
43+
print(io, ")")
44+
end
45+
246
isconnection(_) = false
347
isconnection(_::Connection) = true
448
"""
@@ -188,7 +232,7 @@ var1 ~ var3
188232
# ...
189233
```
190234
"""
191-
function Symbolics.connect(var1::ConnectableSymbolicT, var2::ConnectableSymbolicT,
235+
function connect(var1::ConnectableSymbolicT, var2::ConnectableSymbolicT,
192236
vars::ConnectableSymbolicT...)
193237
allvars = (var1, var2, vars...)
194238
validate_causal_variables_connection(allvars)

src/systems/systemstructure.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using DataStructures
2-
using Symbolics: linear_expansion, unwrap, Connection
2+
using Symbolics: linear_expansion, unwrap
33
using SymbolicUtils: iscall, operation, arguments, Symbolic
44
using SymbolicUtils: quick_cancel, maketerm
55
using ..ModelingToolkit
66
import ..ModelingToolkit: isdiffeq, var_from_nested_derivative, vars!, flatten,
77
value, InvalidSystemException, isdifferential, _iszero,
8-
isparameter,
8+
isparameter, Connection,
99
independent_variables, SparseMatrixCLIL, AbstractSystem,
1010
equations, isirreducible, input_timedomain, TimeDomain,
1111
InferredTimeDomain,

src/variables.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ potential is equated at a junction.
102102
For more information, refer to the [Connection semantics](@ref connect_semantics) section
103103
of the docs.
104104
105-
See also: [`Symbolics.connect`](@ref), [`@connector`](@ref), [`Flow`](@ref),
105+
See also: [`connect`](@ref), [`@connector`](@ref), [`Flow`](@ref),
106106
[`Stream`](@ref).
107107
"""
108108
struct Equality <: AbstractConnectType end # Equality connection
@@ -118,7 +118,7 @@ for current flowing in and out of the function).
118118
For more information, refer to the [Connection semantics](@ref connect_semantics) section
119119
of the docs.
120120
121-
See also: [`Symbolics.connect`](@ref), [`@connector`](@ref), [`Equality`](@ref),
121+
See also: [`connect`](@ref), [`@connector`](@ref), [`Equality`](@ref),
122122
[`Stream`](@ref).
123123
"""
124124
struct Flow <: AbstractConnectType end # sum to 0
@@ -132,7 +132,7 @@ the variable is part of a special stream connector.
132132
For more information, refer to the [Connection semantics](@ref connect_semantics) section
133133
of the docs.
134134
135-
See also: [`Symbolics.connect`](@ref), [`@connector`](@ref), [`Equality`](@ref),
135+
See also: [`connect`](@ref), [`@connector`](@ref), [`Equality`](@ref),
136136
[`Flow`](@ref).
137137
"""
138138
struct Stream <: AbstractConnectType end # special stream connector

0 commit comments

Comments
 (0)