Skip to content

Commit

Permalink
Format and fix AnalysisPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
YingboMa committed Feb 21, 2024
1 parent 02fe980 commit 9a47c71
Show file tree
Hide file tree
Showing 51 changed files with 579 additions and 527 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ V = 1.0
@named ground = Ground()

rc_eqs = [connect(constant.output, source.V)
connect(source.p, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, source.n, ground.g)]
connect(source.p, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, source.n, ground.g)]

@named rc_model = ODESystem(rc_eqs, t,
systems = [resistor, capacitor, constant, source, ground])
Expand Down
6 changes: 3 additions & 3 deletions docs/pages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pages = [
"Custom Components" => "tutorials/custom_component.md",
"Thermal Conduction Model" => "tutorials/thermal_model.md",
"DC Motor with Speed Controller" => "tutorials/dc_motor_pi.md",
"SampledData Component" => "tutorials/input_component.md",
"SampledData Component" => "tutorials/input_component.md"
],
"About Acausal Connections" => "connectors/connections.md",
"API" => [
Expand All @@ -15,6 +15,6 @@ pages = [
"Mechanical Components" => "API/mechanical.md",
"Thermal Components" => "API/thermal.md",
"Hydraulic Components" => "API/hydraulic.md",
"Linear Analysis" => "API/linear_analysis.md",
],
"Linear Analysis" => "API/linear_analysis.md"
]
]
2 changes: 1 addition & 1 deletion docs/src/API/linear_analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ using ModelingToolkitStandardLibrary.Blocks, ModelingToolkit
@named C = Gain(-1) # A P controller
t = ModelingToolkit.get_iv(P)
eqs = [connect(P.output, :plant_output, C.input) # Connect with an automatically created analysis point called :plant_output
connect(C.output, :plant_input, P.input)]
connect(C.output, :plant_input, P.input)]
sys = ODESystem(eqs, t, systems = [P, C], name = :feedback_system)
matrices_S = get_sensitivity(sys, :plant_input)[1] # Compute the matrices of a state-space representation of the (input)sensitivity function.
Expand Down
8 changes: 4 additions & 4 deletions docs/src/connectors/connections.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ using Plots
@named ground = Ground()
eqs = [connect(capacitor.p, resistor.p)
connect(resistor.n, ground.g, capacitor.n)]
connect(resistor.n, ground.g, capacitor.n)]
@named model = ODESystem(eqs, t; systems = [resistor, capacitor, ground])
Expand Down Expand Up @@ -139,7 +139,7 @@ const TV = ModelingToolkitStandardLibrary.Mechanical.Translational
@named ground = TV.Fixed()
eqs = [connect(damping.flange_a, body.flange)
connect(ground.flange, damping.flange_b)]
connect(ground.flange, damping.flange_b)]
@named model = ODESystem(eqs, t; systems = [damping, body, ground])
Expand Down Expand Up @@ -172,7 +172,7 @@ const TP = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition
@named ground = TP.Fixed(s_0 = 0)
eqs = [connect(damping.flange_a, body.flange)
connect(ground.flange, damping.flange_b)]
connect(ground.flange, damping.flange_b)]
@named model = ODESystem(eqs, t; systems = [damping, body, ground])
Expand Down Expand Up @@ -267,7 +267,7 @@ Let's define a quick function to simplify and solve the 2 different systems. Not
```@example connections
function simplify_and_solve(damping, spring, body, ground)
eqs = [connect(spring.flange_a, body.flange, damping.flange_a)
connect(spring.flange_b, damping.flange_b, ground.flange)]
connect(spring.flange_b, damping.flange_b, ground.flange)]
@named model = ODESystem(eqs, t; systems = [ground, body, spring, damping])
Expand Down
18 changes: 9 additions & 9 deletions docs/src/tutorials/custom_component.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function NonlinearResistor(; name, Ga, Gb, Ve)
Gb * (v + Ve) - Ga * Ve,
ifelse(v > Ve,
Gb * (v - Ve) + Ga * Ve,
Ga * v)),
Ga * v))
]
extend(ODESystem(eqs, t, [], pars; name = name), oneport)
end
Expand Down Expand Up @@ -102,14 +102,14 @@ The final model can now be created with the components from the library and the
@named Gnd = Ground()
connections = [connect(L.p, G.p)
connect(G.n, Nr.p)
connect(Nr.n, Gnd.g)
connect(C1.p, G.n)
connect(L.n, Ro.p)
connect(G.p, C2.p)
connect(C1.n, Gnd.g)
connect(C2.n, Gnd.g)
connect(Ro.n, Gnd.g)]
connect(G.n, Nr.p)
connect(Nr.n, Gnd.g)
connect(C1.p, G.n)
connect(L.n, Ro.p)
connect(G.p, C2.p)
connect(C1.n, Gnd.g)
connect(C2.n, Gnd.g)
connect(Ro.n, Gnd.g)]
@named model = ODESystem(connections, t, systems = [L, Ro, G, C1, C2, Nr, Gnd])
nothing # hide
Expand Down
26 changes: 13 additions & 13 deletions docs/src/tutorials/dc_motor_pi.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ The actual model can now be composed.
@named speed_sensor = SpeedSensor()
connections = [connect(fixed.flange, emf.support, friction.flange_b)
connect(emf.flange, friction.flange_a, inertia.flange_a)
connect(inertia.flange_b, load.flange)
connect(inertia.flange_b, speed_sensor.flange)
connect(load_step.output, load.tau)
connect(ref.output, feedback.input1)
connect(speed_sensor.w, :y, feedback.input2)
connect(feedback.output, pi_controller.err_input)
connect(pi_controller.ctr_output, :u, source.V)
connect(source.p, R1.p)
connect(R1.n, L1.p)
connect(L1.n, emf.p)
connect(emf.n, source.n, ground.g)]
connect(emf.flange, friction.flange_a, inertia.flange_a)
connect(inertia.flange_b, load.flange)
connect(inertia.flange_b, speed_sensor.flange)
connect(load_step.output, load.tau)
connect(ref.output, feedback.input1)
connect(speed_sensor.w, :y, feedback.input2)
connect(feedback.output, pi_controller.err_input)
connect(pi_controller.ctr_output, :u, source.V)
connect(source.p, R1.p)
connect(R1.n, L1.p)
connect(L1.n, emf.p)
connect(emf.n, source.n, ground.g)]
@named model = ODESystem(connections, t,
systems = [
Expand All @@ -78,7 +78,7 @@ connections = [connect(fixed.flange, emf.support, friction.flange_b)
load_step,
inertia,
friction,
speed_sensor,
speed_sensor
])
nothing # hide
```
Expand Down
18 changes: 9 additions & 9 deletions docs/src/tutorials/input_component.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ function System(f; name)
pars = @parameters m=10 k=1000 d=1

eqs = [f ~ src.output.u
ddx * 10 ~ k * x + d * dx + f
D(x) ~ dx
D(dx) ~ ddx]
ddx * 10 ~ k * x + d * dx + f
D(x) ~ dx
D(dx) ~ ddx]

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

eqs = [f ~ get_sampled_data(t)
ddx * 10 ~ k * x + d * dx + f
D(x) ~ dx
D(dx) ~ ddx]
ddx * 10 ~ k * x + d * dx + f
D(x) ~ dx
D(dx) ~ ddx]

ODESystem(eqs, t, vars, pars; name)
end
Expand Down Expand Up @@ -115,9 +115,9 @@ function System(; name)
pars = @parameters m=10 k=1000 d=1

eqs = [f ~ src.output.u
ddx * 10 ~ k * x + d * dx + f
D(x) ~ dx
D(dx) ~ ddx]
ddx * 10 ~ k * x + d * dx + f
D(x) ~ dx
D(dx) ~ ddx]

ODESystem(eqs, t, vars, pars; systems = [src], name)
end
Expand Down
6 changes: 3 additions & 3 deletions docs/src/tutorials/rc_circuit.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ V = 1.0
@named ground = Ground()
rc_eqs = [connect(constant.output, source.V)
connect(source.p, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, source.n, ground.g)]
connect(source.p, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, source.n, ground.g)]
@named rc_model = ODESystem(rc_eqs, t,
systems = [resistor, capacitor, constant, source, ground])
Expand Down
2 changes: 1 addition & 1 deletion docs/src/tutorials/thermal_model.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ connections = [
connect(mass1.port, conduction.port_a),
connect(conduction.port_b, mass2.port),
connect(mass1.port, Tsensor1.port),
connect(mass2.port, Tsensor2.port),
connect(mass2.port, Tsensor2.port)
]
@named model = ODESystem(connections, t,
Expand Down
4 changes: 2 additions & 2 deletions src/Blocks/Blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export Log, Log10
include("math.jl")

export Constant, TimeVaryingFunction, Sine, Cosine, ContinuousClock, Ramp, Step, ExpSine,
Square, Triangular, Parameter, SampledData
Square, Triangular, Parameter, SampledData
include("sources.jl")

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

export AnalysisPoint, get_sensitivity, get_comp_sensitivity,
get_looptransfer, open_loop
get_looptransfer, open_loop
include("analysis_points.jl")

end
4 changes: 2 additions & 2 deletions src/Blocks/analysis_points.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using ModelingToolkit: get_eqs, vars, @set!, get_iv

Base.@kwdef mutable struct AnalysisPoint
Base.@kwdef mutable struct AnalysisPoint <: Real
in = nothing
out = nothing
name::Symbol = :nothing
Expand Down Expand Up @@ -420,7 +420,7 @@ function ModelingToolkit.linearization_function(sys::ModelingToolkit.AbstractSys
push!(multiplicities_y, length(yi))
append!(y, yi)
[ap_var(ap.in) .~ yi;
ap_var(ap.out) .~ ap_var(ap.in)], yi
ap_var(ap.out) .~ ap_var(ap.in)], yi
else # loop opening
[ap_var(ap.out) .~ 0;], []
end
Expand Down
20 changes: 10 additions & 10 deletions src/Blocks/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ See also [`LimPID`](@ref)
eqs = [
connect(err_input, addPID.input1),
connect(addPID.output, gainPID.input),
connect(gainPID.output, ctr_output),
connect(gainPID.output, ctr_output)
]
if with_I
push!(eqs, connect(err_input, int.input))
Expand Down Expand Up @@ -347,7 +347,7 @@ The simplified expression above is given without the anti-windup protection.
connect(err_input, addTrack.input1),
connect(gainTrack.output, addTrack.input2),
connect(addTrack.output, int.input),
connect(int.output, addPI.input2),
connect(int.output, addPI.input2)
]
ODESystem(eqs, t, [], []; name = name, systems = sys)
end
Expand Down Expand Up @@ -459,7 +459,7 @@ where the transfer function for the derivative includes additional filtering, se
connect(addP.output, addPID.input1),
connect(addPID.output, gainPID.input),
connect(gainPID.output, limiter.input),
connect(limiter.output, ctr_output),
connect(limiter.output, ctr_output)
]
if with_I
push!(eqs, connect(reference, addI.input1))
Expand Down Expand Up @@ -538,7 +538,7 @@ linearized around the operating point `x₀, u₀`, we have `y0, u0 = h(x₀, u
@named input = RealInput(nin = nu)
@named output = RealOutput(nout = ny)
@variables x(t)[1:nx]=x [
description = "State variables of StateSpace system $name",
description = "State variables of StateSpace system $name"
]
# pars = @parameters A=A B=B C=C D=D # This is buggy
eqs = [ # FIXME: if array equations work
Expand All @@ -547,7 +547,7 @@ linearized around the operating point `x₀, u₀`, we have `y0, u0 = h(x₀, u
for i in 1:nx]..., # cannot use D here
[output.u[j] ~ sum(C[j, i] * x[i] for i in 1:nx) +
sum(D[j, k] * (input.u[k] - u0[k]) for k in 1:nu) + y0[j]
for j in 1:ny]...,
for j in 1:ny]...
]
compose(ODESystem(eqs, t, vcat(x...), [], name = name), [input, output])
end
Expand Down Expand Up @@ -596,11 +596,11 @@ See also [`StateSpace`](@ref) which handles MIMO systems, as well as [ControlSys
@parameters begin
b[1:nb] = b,
[
description = "Numerator coefficients of transfer function (e.g., 2s + 3 is specified as [2,3])",
description = "Numerator coefficients of transfer function (e.g., 2s + 3 is specified as [2,3])"
]
a[1:na] = a,
[
description = "Denominator coefficients of transfer function (e.g., `s² + 2ωs + ω^2` is specified as [1, 2ω, ω^2])",
description = "Denominator coefficients of transfer function (e.g., `s² + 2ωs + ω^2` is specified as [1, 2ω, ω^2])"
]
bb[1:(nbb + nb)] = [zeros(nbb); b]
d = bb[1] / a[1], [description = "Direct feedthrough gain"]
Expand All @@ -627,9 +627,9 @@ See also [`StateSpace`](@ref) which handles MIMO systems, as well as [ControlSys
eqs = [y ~ d * u]
else
eqs = [D(x_scaled[1]) ~ (-a[2:na]'x_scaled + a_end * u) / a[1]
D.(x_scaled[2:nx]) .~ x_scaled[1:(nx - 1)]
y ~ ((bb[2:na] - d * a[2:na])'x_scaled) / a_end + d * u
x .~ x_scaled ./ a_end]
D.(x_scaled[2:nx]) .~ x_scaled[1:(nx - 1)]
y ~ ((bb[2:na] - d * a[2:na])'x_scaled) / a_end + d * u
x .~ x_scaled ./ a_end]
end
push!(eqs, input.u ~ u)
push!(eqs, output.u ~ y)
Expand Down
4 changes: 2 additions & 2 deletions src/Blocks/nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ Limit the range of a signal.
@named siso = SISO(u_start = m, y_start = m) # Default signals to center of saturation to minimize risk of saturation while linearizing etc.
@unpack u, y = siso
pars = @parameters y_max=y_max [description = "Maximum allowed output of Limiter $name"] y_min=y_min [
description = "Minimum allowed output of Limiter $name",
description = "Minimum allowed output of Limiter $name"
]
eqs = [
y ~ _clamp(u, y_min, y_max),
y ~ _clamp(u, y_min, y_max)
]
extend(ODESystem(eqs, t, [], pars; name = name), siso)
end
Expand Down
Loading

0 comments on commit 9a47c71

Please sign in to comment.