Skip to content

Commit 8abd6d7

Browse files
authored
Merge pull request #261 from SciML/myb/ap
Define `isconnection` on AnalysisPoint
2 parents f5e0548 + cc61d77 commit 8abd6d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+717
-655
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ V = 1.0
6060
@named ground = Ground()
6161

6262
rc_eqs = [connect(constant.output, source.V)
63-
connect(source.p, resistor.p)
64-
connect(resistor.n, capacitor.p)
65-
connect(capacitor.n, source.n, ground.g)]
63+
connect(source.p, resistor.p)
64+
connect(resistor.n, capacitor.p)
65+
connect(capacitor.n, source.n, ground.g)]
6666

6767
@named rc_model = ODESystem(rc_eqs, t,
6868
systems = [resistor, capacitor, constant, source, ground])

docs/pages.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pages = [
55
"Custom Components" => "tutorials/custom_component.md",
66
"Thermal Conduction Model" => "tutorials/thermal_model.md",
77
"DC Motor with Speed Controller" => "tutorials/dc_motor_pi.md",
8-
"SampledData Component" => "tutorials/input_component.md",
8+
"SampledData Component" => "tutorials/input_component.md"
99
],
1010
"About Acausal Connections" => "connectors/connections.md",
1111
"API" => [
@@ -15,6 +15,6 @@ pages = [
1515
"Mechanical Components" => "API/mechanical.md",
1616
"Thermal Components" => "API/thermal.md",
1717
"Hydraulic Components" => "API/hydraulic.md",
18-
"Linear Analysis" => "API/linear_analysis.md",
19-
],
18+
"Linear Analysis" => "API/linear_analysis.md"
19+
]
2020
]

docs/src/API/linear_analysis.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ using ModelingToolkitStandardLibrary.Blocks, ModelingToolkit
6060
@named C = Gain(-1) # A P controller
6161
t = ModelingToolkit.get_iv(P)
6262
eqs = [connect(P.output, :plant_output, C.input) # Connect with an automatically created analysis point called :plant_output
63-
connect(C.output, :plant_input, P.input)]
63+
connect(C.output, :plant_input, P.input)]
6464
sys = ODESystem(eqs, t, systems = [P, C], name = :feedback_system)
6565
6666
matrices_S = get_sensitivity(sys, :plant_input)[1] # Compute the matrices of a state-space representation of the (input)sensitivity function.

docs/src/connectors/connections.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ using Plots
9999
@named ground = Ground()
100100
101101
eqs = [connect(capacitor.p, resistor.p)
102-
connect(resistor.n, ground.g, capacitor.n)]
102+
connect(resistor.n, ground.g, capacitor.n)]
103103
104104
@named model = ODESystem(eqs, t; systems = [resistor, capacitor, ground])
105105
@@ -139,7 +139,7 @@ const TV = ModelingToolkitStandardLibrary.Mechanical.Translational
139139
@named ground = TV.Fixed()
140140
141141
eqs = [connect(damping.flange_a, body.flange)
142-
connect(ground.flange, damping.flange_b)]
142+
connect(ground.flange, damping.flange_b)]
143143
144144
@named model = ODESystem(eqs, t; systems = [damping, body, ground])
145145
@@ -172,7 +172,7 @@ const TP = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition
172172
@named ground = TP.Fixed(s_0 = 0)
173173
174174
eqs = [connect(damping.flange_a, body.flange)
175-
connect(ground.flange, damping.flange_b)]
175+
connect(ground.flange, damping.flange_b)]
176176
177177
@named model = ODESystem(eqs, t; systems = [damping, body, ground])
178178
@@ -267,7 +267,7 @@ Let's define a quick function to simplify and solve the 2 different systems. Not
267267
```@example connections
268268
function simplify_and_solve(damping, spring, body, ground)
269269
eqs = [connect(spring.flange_a, body.flange, damping.flange_a)
270-
connect(spring.flange_b, damping.flange_b, ground.flange)]
270+
connect(spring.flange_b, damping.flange_b, ground.flange)]
271271
272272
@named model = ODESystem(eqs, t; systems = [ground, body, spring, damping])
273273

docs/src/tutorials/custom_component.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ function NonlinearResistor(; name, Ga, Gb, Ve)
4343
pars = @parameters Ga=Ga Gb=Gb Ve=Ve
4444
eqs = [
4545
i ~ ifelse(v < -Ve,
46-
Gb * (v + Ve) - Ga * Ve,
47-
ifelse(v > Ve,
48-
Gb * (v - Ve) + Ga * Ve,
49-
Ga * v)),
46+
Gb * (v + Ve) - Ga * Ve,
47+
ifelse(v > Ve,
48+
Gb * (v - Ve) + Ga * Ve,
49+
Ga * v))
5050
]
5151
extend(ODESystem(eqs, t, [], pars; name = name), oneport)
5252
end
@@ -102,14 +102,14 @@ The final model can now be created with the components from the library and the
102102
@named Gnd = Ground()
103103
104104
connections = [connect(L.p, G.p)
105-
connect(G.n, Nr.p)
106-
connect(Nr.n, Gnd.g)
107-
connect(C1.p, G.n)
108-
connect(L.n, Ro.p)
109-
connect(G.p, C2.p)
110-
connect(C1.n, Gnd.g)
111-
connect(C2.n, Gnd.g)
112-
connect(Ro.n, Gnd.g)]
105+
connect(G.n, Nr.p)
106+
connect(Nr.n, Gnd.g)
107+
connect(C1.p, G.n)
108+
connect(L.n, Ro.p)
109+
connect(G.p, C2.p)
110+
connect(C1.n, Gnd.g)
111+
connect(C2.n, Gnd.g)
112+
connect(Ro.n, Gnd.g)]
113113
114114
@named model = ODESystem(connections, t, systems = [L, Ro, G, C1, C2, Nr, Gnd])
115115
nothing # hide

docs/src/tutorials/dc_motor_pi.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ The actual model can now be composed.
5050
@named speed_sensor = SpeedSensor()
5151
5252
connections = [connect(fixed.flange, emf.support, friction.flange_b)
53-
connect(emf.flange, friction.flange_a, inertia.flange_a)
54-
connect(inertia.flange_b, load.flange)
55-
connect(inertia.flange_b, speed_sensor.flange)
56-
connect(load_step.output, load.tau)
57-
connect(ref.output, feedback.input1)
58-
connect(speed_sensor.w, :y, feedback.input2)
59-
connect(feedback.output, pi_controller.err_input)
60-
connect(pi_controller.ctr_output, :u, source.V)
61-
connect(source.p, R1.p)
62-
connect(R1.n, L1.p)
63-
connect(L1.n, emf.p)
64-
connect(emf.n, source.n, ground.g)]
53+
connect(emf.flange, friction.flange_a, inertia.flange_a)
54+
connect(inertia.flange_b, load.flange)
55+
connect(inertia.flange_b, speed_sensor.flange)
56+
connect(load_step.output, load.tau)
57+
connect(ref.output, feedback.input1)
58+
connect(speed_sensor.w, :y, feedback.input2)
59+
connect(feedback.output, pi_controller.err_input)
60+
connect(pi_controller.ctr_output, :u, source.V)
61+
connect(source.p, R1.p)
62+
connect(R1.n, L1.p)
63+
connect(L1.n, emf.p)
64+
connect(emf.n, source.n, ground.g)]
6565
6666
@named model = ODESystem(connections, t,
6767
systems = [
@@ -78,7 +78,7 @@ connections = [connect(fixed.flange, emf.support, friction.flange_b)
7878
load_step,
7979
inertia,
8080
friction,
81-
speed_sensor,
81+
speed_sensor
8282
])
8383
nothing # hide
8484
```

docs/src/tutorials/input_component.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ function System(f; name)
2828
pars = @parameters m=10 k=1000 d=1
2929

3030
eqs = [f ~ src.output.u
31-
ddx * 10 ~ k * x + d * dx + f
32-
D(x) ~ dx
33-
D(dx) ~ ddx]
31+
ddx * 10 ~ k * x + d * dx + f
32+
D(x) ~ dx
33+
D(dx) ~ ddx]
3434

3535
ODESystem(eqs, t, vars, pars; systems = [src], name)
3636
end
@@ -74,9 +74,9 @@ function System(; name)
7474
pars = @parameters m=10 k=1000 d=1
7575

7676
eqs = [f ~ get_sampled_data(t)
77-
ddx * 10 ~ k * x + d * dx + f
78-
D(x) ~ dx
79-
D(dx) ~ ddx]
77+
ddx * 10 ~ k * x + d * dx + f
78+
D(x) ~ dx
79+
D(dx) ~ ddx]
8080

8181
ODESystem(eqs, t, vars, pars; name)
8282
end
@@ -115,9 +115,9 @@ function System(; name)
115115
pars = @parameters m=10 k=1000 d=1
116116

117117
eqs = [f ~ src.output.u
118-
ddx * 10 ~ k * x + d * dx + f
119-
D(x) ~ dx
120-
D(dx) ~ ddx]
118+
ddx * 10 ~ k * x + d * dx + f
119+
D(x) ~ dx
120+
D(dx) ~ ddx]
121121

122122
ODESystem(eqs, t, vars, pars; systems = [src], name)
123123
end

docs/src/tutorials/rc_circuit.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ V = 1.0
2222
@named ground = Ground()
2323
2424
rc_eqs = [connect(constant.output, source.V)
25-
connect(source.p, resistor.p)
26-
connect(resistor.n, capacitor.p)
27-
connect(capacitor.n, source.n, ground.g)]
25+
connect(source.p, resistor.p)
26+
connect(resistor.n, capacitor.p)
27+
connect(capacitor.n, source.n, ground.g)]
2828
2929
@named rc_model = ODESystem(rc_eqs, t,
3030
systems = [resistor, capacitor, constant, source, ground])

docs/src/tutorials/thermal_model.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ connections = [
2323
connect(mass1.port, conduction.port_a),
2424
connect(conduction.port_b, mass2.port),
2525
connect(mass1.port, Tsensor1.port),
26-
connect(mass2.port, Tsensor2.port),
26+
connect(mass2.port, Tsensor2.port)
2727
]
2828
2929
@named model = ODESystem(connections, t,

src/Blocks/Blocks.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export Log, Log10
1919
include("math.jl")
2020

2121
export Constant, TimeVaryingFunction, Sine, Cosine, ContinuousClock, Ramp, Step, ExpSine,
22-
Square, Triangular, Parameter, SampledData
22+
Square, Triangular, Parameter, SampledData
2323
include("sources.jl")
2424

2525
export Limiter, DeadZone, SlewRateLimiter
@@ -30,7 +30,7 @@ export PI, LimPI, PID, LimPID
3030
include("continuous.jl")
3131

3232
export AnalysisPoint, get_sensitivity, get_comp_sensitivity,
33-
get_looptransfer, open_loop
33+
get_looptransfer, open_loop
3434
include("analysis_points.jl")
3535

3636
end

src/Blocks/analysis_points.jl

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ Base.@kwdef mutable struct AnalysisPoint
55
out = nothing
66
name::Symbol = :nothing
77
end
8+
Base.broadcastable(x::AnalysisPoint) = Ref(x)
9+
if Base.isdefined(ModelingToolkit, :isconnection)
10+
ModelingToolkit.isconnection(::AnalysisPoint) = true
11+
end
812

913
Base.nameof(ap::AnalysisPoint) = ap.name
14+
function Base.hash(ap::AnalysisPoint, seed::UInt)
15+
h1 = hash(ap.in, seed)
16+
h2 = hash(ap.out, h1)
17+
h3 = hash(ap.name, h2)
18+
h3 (0xd29cdc51aa6562d4 % UInt)
19+
end
1020

1121
function ap_var(sys)
1222
if hasproperty(sys, :u)
@@ -255,7 +265,7 @@ end
255265
const SymOrVec = Union{Symbol, Vector{Symbol}}
256266

257267
function get_sensitivity_function(sys, ap_name::SymOrVec; loop_openings = nothing,
258-
kwargs...)
268+
kwargs...)
259269
find = namespaced_ap_match(ap_name, loop_openings)
260270
t = get_iv(sys)
261271
aps = []
@@ -284,7 +294,7 @@ function get_sensitivity_function(sys, ap_name::SymOrVec; loop_openings = nothin
284294
end
285295

286296
function get_comp_sensitivity_function(sys, ap_name::SymOrVec; loop_openings = nothing,
287-
kwargs...)
297+
kwargs...)
288298
find = namespaced_ap_match(ap_name, loop_openings)
289299
t = get_iv(sys)
290300
aps = []
@@ -313,7 +323,7 @@ function get_comp_sensitivity_function(sys, ap_name::SymOrVec; loop_openings = n
313323
end
314324

315325
function get_looptransfer_function(sys, ap_name::SymOrVec; loop_openings = nothing,
316-
kwargs...)
326+
kwargs...)
317327
find = namespaced_ap_match(ap_name, loop_openings)
318328
t = get_iv(sys)
319329
aps = []
@@ -380,9 +390,9 @@ function open_loop(sys, ap_name::Symbol; ground_input = false, kwargs...)
380390
end
381391

382392
function ModelingToolkit.linearization_function(sys::ModelingToolkit.AbstractSystem,
383-
input_name::SymOrVec, output_name;
384-
loop_openings = nothing,
385-
kwargs...)
393+
input_name::SymOrVec, output_name;
394+
loop_openings = nothing,
395+
kwargs...)
386396
t = get_iv(sys)
387397
@variables u(t)=0 [input = true]
388398
names = [input_name;]
@@ -417,7 +427,7 @@ function ModelingToolkit.linearization_function(sys::ModelingToolkit.AbstractSys
417427
push!(multiplicities_y, length(yi))
418428
append!(y, yi)
419429
[ap_var(ap.in) .~ yi;
420-
ap_var(ap.out) .~ ap_var(ap.in)], yi
430+
ap_var(ap.out) .~ ap_var(ap.in)], yi
421431
else # loop opening
422432
[ap_var(ap.out) .~ 0;], []
423433
end
@@ -444,19 +454,19 @@ for f in [:get_sensitivity, :get_comp_sensitivity, :get_looptransfer, :open_loop
444454
end
445455

446456
function ModelingToolkit.linearize(sys, input::AnalysisPoint, output::AnalysisPoint;
447-
kwargs...)
457+
kwargs...)
448458
ModelingToolkit.linearize(sys, nameof(input), nameof(output); kwargs...)
449459
end
450460

451461
# Methods above are implemented in terms of linearization_function, the method below creates wrappers for linearize
452462
for f in [:get_sensitivity, :get_comp_sensitivity, :get_looptransfer]
453463
@eval function $f(sys,
454-
ap,
455-
args...;
456-
loop_openings = nothing,
457-
op = Dict(),
458-
p = DiffEqBase.NullParameters(),
459-
kwargs...)
464+
ap,
465+
args...;
466+
loop_openings = nothing,
467+
op = Dict(),
468+
p = DiffEqBase.NullParameters(),
469+
kwargs...)
460470
lin_fun, ssys = $(Symbol(string(f) * "_function"))(sys, ap, args...; op, p,
461471
loop_openings,
462472
kwargs...)
@@ -472,7 +482,7 @@ Linearize a system between two analysis points. To get a loop-transfer function,
472482
The output is allowed to be either an analysis-point name, or a vector of symbolic variables like the standard interface to `linearize`. The input must be an analysis-point name.
473483
"""
474484
function ModelingToolkit.linearize(sys, input_name::SymOrVec, output_name;
475-
loop_openings = nothing, kwargs...)
485+
loop_openings = nothing, kwargs...)
476486
lin_fun, ssys = linearization_function(sys, input_name, output_name; loop_openings,
477487
kwargs...)
478488
ModelingToolkit.linearize(ssys, lin_fun; kwargs...), ssys

0 commit comments

Comments
 (0)