Skip to content

Commit a739b22

Browse files
Merge pull request #274 from ven-k/vkb/guess
Rotational: default values are now guesses + fix tests
2 parents 42e051c + 3a7ccfb commit a739b22

File tree

14 files changed

+433
-412
lines changed

14 files changed

+433
-412
lines changed

.typos.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[default.extend-words]
22
Nd = "Nd"
33
nin = "nin"
4-
coul = "coul"
4+
coul = "coul"
5+
isconnection = "isconnection"

docs/src/tutorials/dc_motor_pi.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ so that it can be represented as a system of `ODEs` (ordinary differential equat
8888

8989
```@example dc_motor_pi
9090
sys = structural_simplify(model)
91-
prob = ODEProblem(sys, [], (0, 6.0))
91+
prob = ODEProblem(sys, unknowns(sys) .=> 0.0, (0, 6.0))
9292
sol = solve(prob, Rodas4())
9393
9494
p1 = Plots.plot(sol.t, sol[inertia.w], ylabel = "Angular Vel. in rad/s",
@@ -118,9 +118,11 @@ T(s) &= \dfrac{P(s)C(s)}{I + P(s)C(s)}
118118

119119
```@example dc_motor_pi
120120
using ControlSystemsBase
121-
matrices_S, simplified_sys = Blocks.get_sensitivity(model, :y)
121+
matrices_S, simplified_sys = Blocks.get_sensitivity(
122+
model, :y, op = Dict(unknowns(sys) .=> 0.0))
122123
So = ss(matrices_S...) |> minreal # The output-sensitivity function as a StateSpace system
123-
matrices_T, simplified_sys = Blocks.get_comp_sensitivity(model, :y)
124+
matrices_T, simplified_sys = Blocks.get_comp_sensitivity(
125+
model, :y, op = Dict(inertia.phi => 0.0, inertia.w => 0.0))
124126
To = ss(matrices_T...)# The output complementary sensitivity function as a StateSpace system
125127
bodeplot([So, To], label = ["S" "T"], plot_title = "Sensitivity functions",
126128
plotphase = false)
@@ -129,7 +131,8 @@ bodeplot([So, To], label = ["S" "T"], plot_title = "Sensitivity functions",
129131
Similarly, we may compute the loop-transfer function and plot its Nyquist curve
130132

131133
```@example dc_motor_pi
132-
matrices_L, simplified_sys = Blocks.get_looptransfer(model, :y)
134+
matrices_L, simplified_sys = Blocks.get_looptransfer(
135+
model, :y, op = Dict(unknowns(sys) .=> 0.0))
133136
L = -ss(matrices_L...) # The loop-transfer function as a StateSpace system. The negative sign is to negate the built-in negative feedback
134137
Ms, ωMs = hinfnorm(So) # Compute the peak of the sensitivity function to draw a circle in the Nyquist plot
135138
nyquistplot(L, label = "\$L(s)\$", ylims = (-2.5, 0.5), xlims = (-1.2, 0.1),

src/Blocks/continuous.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Initial value of integrator state ``x`` can be set with `x`
1212
# Parameters:
1313
1414
- `k`: Gain of integrator
15+
16+
# Unknowns:
17+
18+
- `x`: State of Integrator. Defaults to 0.0.
1519
"""
1620
@mtkmodel Integrator begin
1721
@extend u, y = siso = SISO()
@@ -26,6 +30,7 @@ Initial value of integrator state ``x`` can be set with `x`
2630
y ~ x
2731
end
2832
end
33+
2934
"""
3035
Derivative(; name, k = 1, T, x = 0.0)
3136
@@ -49,6 +54,10 @@ A smaller `T` leads to a more ideal approximation of the derivative.
4954
- `k`: Gain
5055
- `T`: [s] Time constants (T>0 required; T=0 is ideal derivative block)
5156
57+
# Unknowns:
58+
59+
- `x`: Unknown of Derivative. Defaults to 0.0.
60+
5261
# Connectors:
5362
5463
- `input`
@@ -160,8 +169,8 @@ Initial value of the state `x` can be set with `x`, and of derivative state `xd`
160169
@mtkmodel SecondOrder begin
161170
@extend u, y = siso = SISO()
162171
@variables begin
163-
x(t) = 0.0, [description = "State of SecondOrder filter"]
164-
xd(t) = 0.0, [description = "Derivative state of SecondOrder filter"]
172+
x(t), [description = "State of SecondOrder filter", guess = 0.0]
173+
xd(t), [description = "Derivative state of SecondOrder filter", guess = 0.0]
165174
end
166175
@parameters begin
167176
k = 1.0, [description = "Gain"]

src/Blocks/math.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Output difference between reference input (input1) and feedback input (input2).
9999
end
100100

101101
"""
102-
Add(; name, k1 = 1, k2 = 1)
102+
Add(; name, k1 = 1.0, k2 = 1.0)
103103
104104
Output the sum of the two scalar inputs.
105105
@@ -121,16 +121,16 @@ Output the sum of the two scalar inputs.
121121
output = RealOutput()
122122
end
123123
@parameters begin
124-
k1 = 1, [description = "Gain of Add input1"]
125-
k2 = 1, [description = "Gain of Add input2"]
124+
k1 = 1.0, [description = "Gain of Add input1"]
125+
k2 = 1.0, [description = "Gain of Add input2"]
126126
end
127127
@equations begin
128128
output.u ~ k1 * input1.u + k2 * input2.u
129129
end
130130
end
131131

132132
"""
133-
Add(; name, k1 = 1, k2 = 1, k3 = 1)
133+
Add(; name, k1 = 1.0, k2 = 1.0, k3 = 1.0)
134134
135135
Output the sum of the three scalar inputs.
136136
@@ -155,9 +155,9 @@ Output the sum of the three scalar inputs.
155155
output = RealOutput()
156156
end
157157
@parameters begin
158-
k1 = 1, [description = "Gain of Add input1"]
159-
k2 = 1, [description = "Gain of Add input2"]
160-
k3 = 1, [description = "Gain of Add input3"]
158+
k1 = 1.0, [description = "Gain of Add input1"]
159+
k2 = 1.0, [description = "Gain of Add input2"]
160+
k3 = 1.0, [description = "Gain of Add input3"]
161161
end
162162
@equations begin
163163
output.u ~ k1 * input1.u + k2 * input2.u + k3 * input3.u

src/Mechanical/Rotational/components.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ end
5555
@symcheck J > 0 || throw(ArgumentError("Expected `J` to be positive"))
5656
end
5757
@variables begin
58-
phi(t) = 0.0, [description = "Absolute rotation angle"]
59-
w(t) = 0.0, [description = "Absolute angular velocity"]
60-
a(t) = 0.0, [description = "Absolute angular acceleration"]
58+
phi(t), [description = "Absolute rotation angle", guess = 0.0]
59+
w(t), [description = "Absolute angular velocity", guess = 0.0]
60+
a(t), [description = "Absolute angular acceleration", guess = 0.0]
6161
end
6262
@equations begin
6363
phi ~ flange_a.phi
@@ -86,7 +86,7 @@ Linear 1D rotational spring
8686
# Parameters:
8787
8888
- `c`: [`N.m/rad`] Spring constant
89-
- `phi_rel0`: [`rad`] Unstretched spring angle
89+
- `phi_rel0`: [`rad`] Unstretched spring angle. Defaults to 0.0.
9090
"""
9191
@mtkmodel Spring begin
9292
@extend phi_rel, tau = partial_comp = PartialCompliant()
@@ -156,7 +156,7 @@ Linear 1D rotational spring and damper
156156
157157
- `d`: [`N.m.s/rad`] Damping constant
158158
- `c`: [`N.m/rad`] Spring constant
159-
- `phi_rel0`: [`rad`] Unstretched spring angle
159+
- `phi_rel0`: [`rad`] Unstretched spring angle. Defaults to 0.0
160160
"""
161161
@mtkmodel SpringDamper begin
162162
@extend phi_rel, w_rel, tau = partial_comp = PartialCompliantWithRelativeStates()
@@ -206,8 +206,10 @@ This element characterizes any type of gear box which is fixed in the ground and
206206
ratio, [description = "Transmission ratio"]
207207
end
208208
@variables begin
209-
phi_a(t) = 0.0, [description = "Relative angle between shaft a and the support"]
210-
phi_b(t) = 0.0, [description = "Relative angle between shaft b and the support"]
209+
phi_a(t),
210+
[description = "Relative angle between shaft a and the support", guess = 0.0]
211+
phi_b(t),
212+
[description = "Relative angle between shaft b and the support", guess = 0.0]
211213
end
212214
@equations begin
213215
phi_a ~ flange_a.phi - phi_support

src/Mechanical/Rotational/sensors.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Ideal sensor to measure the relative angular velocity
8181
w_rel = RealOutput()
8282
end
8383
@variables begin
84-
phi_rel(t) = 0.0
84+
phi_rel(t), [guess = 0.0]
8585
end
8686
@equations begin
8787
0 ~ flange_a.tau + flange_b.tau

src/Mechanical/Rotational/sources.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Forced movement of a flange according to a reference angular velocity signal
103103
@named partial_element = PartialElementaryOneFlangeAndSupport2(use_support = use_support)
104104
@unpack flange, phi_support = partial_element
105105
@named w_ref = RealInput()
106-
@variables phi(t)=0.0 w(t)=0.0 a(t)=0.0
106+
@variables phi(t) [guess = 0.0] w(t) [guess = 0.0] a(t) [guess = 0.0]
107107
eqs = [phi ~ flange.phi - phi_support
108108
D(phi) ~ w]
109109
if exact

src/Mechanical/Rotational/utils.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ Partial model for the compliant connection of two rotational 1-dim. shaft flange
7171
flange_b = Flange()
7272
end
7373
@variables begin
74-
phi_rel(t) = 0.0, [description = "Relative rotation angle between flanges"]
75-
tau(t) = 0.0, [description = "Torque between flanges"]
74+
phi_rel(t), [description = "Relative rotation angle between flanges", guess = 0.0]
75+
tau(t), [description = "Torque between flanges", guess = 0.0]
7676
end
7777
@equations begin
7878
phi_rel ~ flange_b.phi - flange_a.phi
@@ -104,10 +104,11 @@ Partial model for the compliant connection of two rotational 1-dim. shaft flange
104104
flange_b = Flange()
105105
end
106106
@variables begin
107-
phi_rel(t) = 0.0, [description = "Relative rotation angle between flanges"]
108-
w_rel(t) = 0.0, [description = "Relative angular velocity between flanges"]
109-
a_rel(t) = 0.0, [description = "Relative angular acceleration between flanges"]
110-
tau(t) = 0.0, [description = "Torque between flanges"]
107+
phi_rel(t), [description = "Relative rotation angle between flanges", guess = 0.0]
108+
w_rel(t), [description = "Relative angular velocity between flanges", guess = 0.0]
109+
a_rel(t),
110+
[description = "Relative angular acceleration between flanges", guess = 0.0]
111+
tau(t), [description = "Torque between flanges", guess = 0.0]
111112
end
112113
@equations begin
113114
phi_rel ~ flange_b.phi - flange_a.phi
@@ -138,7 +139,8 @@ Partial model for a component with one rotational 1-dim. shaft flange and a supp
138139
@component function PartialElementaryOneFlangeAndSupport2(; name, use_support = false)
139140
@named flange = Flange()
140141
sys = [flange]
141-
@variables phi_support(t)=0.0 [description = "Absolute angle of support flange"]
142+
@variables phi_support(t) [
143+
description = "Absolute angle of support flange", guess = 0.0]
142144
if use_support
143145
@named support = Support()
144146
eqs = [support.phi ~ phi_support
@@ -173,7 +175,8 @@ Partial model for a component with two rotational 1-dim. shaft flanges and a sup
173175
@named flange_a = Flange()
174176
@named flange_b = Flange()
175177
sys = [flange_a, flange_b]
176-
@variables phi_support(t)=0.0 [description = "Absolute angle of support flange"]
178+
@variables phi_support(t) [
179+
description = "Absolute angle of support flange", guess = 0.0]
177180
if use_support
178181
@named support = Support()
179182
eqs = [support.phi ~ phi_support

test/Blocks/continuous.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ end
8282
@named pt2 = SecondOrder(; k = k, w = w, d = d)
8383
@named iosys = ODESystem(connect(c.output, pt2.input), t, systems = [pt2, c])
8484
sys = structural_simplify(iosys)
85-
prob = ODEProblem(sys, Pair[], (0.0, 100.0))
85+
prob = ODEProblem(sys, [unknowns(sys) .=> 0.0...; pt2.xd => 0.0], (0.0, 100.0))
8686
sol = solve(prob, Rodas4())
8787
@test sol.retcode == Success
8888
@test sol[pt2.output.u]pt2_func.(sol.t, k, w, d) atol=1e-3

test/Blocks/nonlinear.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ using OrdinaryDiffEq: ReturnCode.Success
3939
systems = [source, lim, int])
4040
sys = structural_simplify(iosys)
4141

42-
prob = ODEProblem(sys, Pair[], (0.0, 10.0))
42+
prob = ODEProblem(sys, unknowns(sys) .=> 0.0, (0.0, 10.0))
4343

4444
sol = solve(prob, Rodas4())
4545
@test sol.retcode == Success

0 commit comments

Comments
 (0)