Skip to content

Commit 8e631b7

Browse files
committed
refactor: update the arglist of non-dsl components to match the style of dsl components
- this will allow upgradation of those without breaking again
1 parent eed7303 commit 8e631b7

File tree

8 files changed

+134
-165
lines changed

8 files changed

+134
-165
lines changed

docs/src/connectors/connections.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The idea behind the selection of the **through** variable is that it should be a
1818
```math
1919
\begin{aligned}
2020
\partial {\color{blue}{across}} / \partial t \cdot c_1 = {\color{green}{through}} \\
21-
{\color{green}{through}} \cdot c_2 = {\color{blue}{across}}
21+
{\color{green}{through}} \cdot c_2 = {\color{blue}{across}}
2222
\end{aligned}
2323
```
2424

@@ -29,7 +29,7 @@ For the Electrical domain, the across variable is *voltage* and the through vari
2929
- Energy Dissipation:
3030

3131
```math
32-
\partial {\color{blue}{voltage}} / \partial t \cdot capacitance = {\color{green}{current}}
32+
\partial {\color{blue}{voltage}} / \partial t \cdot capacitance = {\color{green}{current}}
3333
```
3434

3535
- Flow:
@@ -45,13 +45,13 @@ For the translation domain, choosing *velocity* for the across variable and *for
4545
- Energy Dissipation:
4646

4747
```math
48-
\partial {\color{blue}{velocity}} / \partial t \cdot mass = {\color{green}{force}}
48+
\partial {\color{blue}{velocity}} / \partial t \cdot mass = {\color{green}{force}}
4949
```
5050

5151
- Flow:
5252

5353
```math
54-
{\color{green}{force}} \cdot (1/damping) = {\color{blue}{velocity}}
54+
{\color{green}{force}} \cdot (1/damping) = {\color{blue}{velocity}}
5555
```
5656

5757
The diagram here shows the similarity of problems in different physical domains.
@@ -65,13 +65,13 @@ Now, if we choose *position* for the across variable, a similar relationship can
6565
- Energy Dissipation:
6666

6767
```math
68-
\partial^2 {\color{blue}{position}} / \partial t^2 \cdot mass = {\color{green}{force}}
68+
\partial^2 {\color{blue}{position}} / \partial t^2 \cdot mass = {\color{green}{force}}
6969
```
7070

7171
- Flow:
7272

7373
```math
74-
{\color{green}{force}} \cdot (1/damping) = \partial {\color{blue}{position}} / \partial t
74+
{\color{green}{force}} \cdot (1/damping) = \partial {\color{blue}{position}} / \partial t
7575
```
7676

7777
As can be seen, we must now establish a higher order derivative to define the Energy Dissipation and Flow equations, requiring an extra equation, as will be shown in the example below.
@@ -134,7 +134,7 @@ Now using the Translational library based on velocity, we can see the same relat
134134
using ModelingToolkitStandardLibrary
135135
const TV = ModelingToolkitStandardLibrary.Mechanical.Translational
136136
137-
@named damping = TV.Damper(d = 1, v_a_0 = 1)
137+
@named damping = TV.Damper(d = 1, va = 1)
138138
@named body = TV.Mass(m = 1, v_0 = 1)
139139
@named ground = TV.Fixed()
140140
@@ -167,7 +167,7 @@ Now, let's consider the position-based approach. We can build the same model wi
167167
```@example connections
168168
const TP = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition
169169
170-
@named damping = TP.Damper(d = 1, v_a_0 = 1)
170+
@named damping = TP.Damper(d = 1, va = 1)
171171
@named body = TP.Mass(m = 1, v_0 = 1)
172172
@named ground = TP.Fixed(s_0 = 0)
173173
@@ -220,21 +220,21 @@ In this problem, we have a mass, spring, and damper which are connected to a fix
220220

221221
#### Damper
222222

223-
The damper will connect the flange/flange 1 (`flange_a`) to the mass, and flange/flange 2 (`flange_b`) to the fixed point. For both position- and velocity-based domains, we set the damping constant `d=1` and `v_a_0=1` and leave the default for `v_b_0` at 0. For the position domain, we also need to set the initial positions for `flange_a` and `flange_b`.
223+
The damper will connect the flange/flange 1 (`flange_a`) to the mass, and flange/flange 2 (`flange_b`) to the fixed point. For both position- and velocity-based domains, we set the damping constant `d=1` and `va=1` and leave the default for `v_b_0` at 0. For the position domain, we also need to set the initial positions for `flange_a` and `flange_b`.
224224

225225
```@example connections
226-
@named dv = TV.Damper(d = 1, v_a_0 = 1)
227-
@named dp = TP.Damper(d = 1, v_a_0 = 1, s_a_0 = 3, s_b_0 = 1)
226+
@named dv = TV.Damper(d = 1, va = 1)
227+
@named dp = TP.Damper(d = 1, va = 1, flange_a__s = 3, flange_b__s = 1)
228228
nothing # hide
229229
```
230230

231231
#### Spring
232232

233-
The spring will connect the flange/flange 1 (`flange_a`) to the mass, and flange/flange 2 (`flange_b`) to the fixed point. For both position- and velocity-based domains, we set the spring constant `k=1`. The velocity domain then requires the initial velocity `v_a_0` and initial spring stretch `delta_s_0`. The position domain instead needs the initial positions for `flange_a` and `flange_b` and the natural spring length `l`.
233+
The spring will connect the flange/flange 1 (`flange_a`) to the mass, and flange/flange 2 (`flange_b`) to the fixed point. For both position- and velocity-based domains, we set the spring constant `k=1`. The velocity domain then requires the initial velocity `va` and initial spring stretch `delta_s`. The position domain instead needs the initial positions for `flange_a` and `flange_b` and the natural spring length `l`.
234234

235235
```@example connections
236-
@named sv = TV.Spring(k = 1, v_a_0 = 1, delta_s_0 = 1)
237-
@named sp = TP.Spring(k = 1, s_a_0 = 3, s_b_0 = 1, l = 1)
236+
@named sv = TV.Spring(k = 1, va = 1, delta_s = 1)
237+
@named sp = TP.Spring(k = 1, flange_a__s = 3, flange_b__s = 1, l = 1)
238238
nothing # hide
239239
```
240240

src/Hydraulic/IsothermalCompressible/components.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ dm ────► │ │ area
506506

507507
ports = @named begin
508508
port = HydraulicPort(; p_int)
509-
flange = MechanicalPort(; f_int = p_int * area)
509+
flange = MechanicalPort(; f = p_int * area)
510510
damper = ValveBase(; p_a_int = p_int, p_b_int = p_int, area_int = 1, Cd,
511511
Cd_reverse, minimum_area)
512512
end

src/Mechanical/Translational/components.jl

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,22 @@ Sliding mass with inertia
5858
5959
- `flange`: 1-dim. translational flange
6060
"""
61-
@component function Mass(; name, v_0 = 0.0, m, s_0 = nothing, g = nothing)
61+
@component function Mass(; name, v = 0.0, m, s = nothing, g = nothing)
6262
pars = @parameters begin
6363
m = m
64-
v_0 = v_0
6564
end
65+
@named flange = MechanicalPort(; v = v)
66+
6667
vars = @variables begin
67-
v(t) = v_0
68+
v(t) = v
6869
f(t) = 0
6970
end
7071

71-
@named flange = MechanicalPort()
72-
7372
eqs = [flange.v ~ v
7473
flange.f ~ f]
7574

7675
# gravity option
77-
if !isnothing(g)
76+
if g !== nothing
7877
@parameters g = g
7978
push!(pars, g)
8079
push!(eqs, D(v) ~ f / m + g)
@@ -83,99 +82,86 @@ Sliding mass with inertia
8382
end
8483

8584
# position option
86-
if !isnothing(s_0)
87-
@parameters s_0 = s_0
88-
push!(pars, s_0)
89-
90-
@variables s(t) = s_0
85+
if s !== nothing
86+
@variables s(t) = s
9187
push!(vars, s)
9288

9389
push!(eqs, D(s) ~ v)
9490
end
9591

96-
return compose(ODESystem(eqs, t, vars, pars; name = name, defaults = [flange.v => v_0]),
92+
return compose(ODESystem(eqs, t, vars, pars; name = name),
9793
flange)
9894
end
9995

10096
const REL = Val(:relative)
10197

10298
"""
103-
Spring(; name, k, delta_s_0 = 0.0, v_a_0=0.0, v_b_0=0.0)
99+
Spring(; name, k, delta_s = 0.0, va=0.0, v_b_0=0.0)
104100
105101
Linear 1D translational spring
106102
107103
# Parameters:
108104
109105
- `k`: [N/m] Spring constant
110-
- `delta_s_0`: initial spring stretch
111-
- `v_a_0`: [m/s] Initial value of absolute linear velocity at flange_a (default 0 m/s)
106+
- `delta_s`: initial spring stretch
107+
- `va`: [m/s] Initial value of absolute linear velocity at flange_a (default 0 m/s)
112108
- `v_b_0`: [m/s] Initial value of absolute linear velocity at flange_b (default 0 m/s)
113109
114110
# Connectors:
115111
116112
- `flange_a`: 1-dim. translational flange on one side of spring
117113
- `flange_b`: 1-dim. translational flange on opposite side of spring
118114
"""
119-
@component function Spring(; name, k, delta_s_0 = 0.0, v_a_0 = 0.0, v_b_0 = 0.0)
120-
Spring(REL; name, k, delta_s_0, v_a_0, v_b_0)
115+
@component function Spring(; name, k, delta_s = 0.0, flange_a__v = 0.0, flange_b__v = 0.0)
116+
Spring(REL; name, k, delta_s, flange_a__v, flange_b__v)
121117
end # default
122118

123-
@component function Spring(::Val{:relative}; name, k, delta_s_0 = 0.0, v_a_0 = 0.0,
124-
v_b_0 = 0.0)
119+
@component function Spring(::Val{:relative}; name, k, delta_s = 0.0, flange_a__v = 0.0,
120+
flange_b__v = 0.0)
125121
pars = @parameters begin
126122
k = k
127-
delta_s_0 = delta_s_0
128-
v_a_0 = v_a_0
129-
v_b_0 = v_b_0
130123
end
131124
vars = @variables begin
132-
delta_s(t) = delta_s_0
125+
delta_s(t) = delta_s
133126
f(t) = 0
134127
end
135128

136-
@named flange_a = MechanicalPort()
137-
@named flange_b = MechanicalPort()
129+
@named flange_a = MechanicalPort(; v = flange_a__v)
130+
@named flange_b = MechanicalPort(; v = flange_b__v)
138131

139132
eqs = [D(delta_s) ~ flange_a.v - flange_b.v
140133
f ~ k * delta_s
141134
flange_a.f ~ +f
142135
flange_b.f ~ -f]
143-
return compose(ODESystem(eqs, t, vars, pars; name = name,
144-
defaults = [flange_a.v => v_a_0, flange_b.v => v_b_0]),
136+
return compose(ODESystem(eqs, t, vars, pars; name = name),
145137
flange_a,
146-
flange_b) #flange_a.f => +k*delta_s_0, flange_b.f => -k*delta_s_0
138+
flange_b) #flange_a.f => +k*delta_s, flange_b.f => -k*delta_s
147139
end
148140

149141
const ABS = Val(:absolute)
150-
@component function Spring(::Val{:absolute}; name, k, s_a_0 = 0, s_b_0 = 0, v_a_0 = 0.0,
151-
v_b_0 = 0.0,
152-
l = 0)
142+
@component function Spring(::Val{:absolute}; name, k, sa = 0, sb = 0, flange_a__v = 0.0,
143+
flange_b__v = 0.0, l = 0)
153144
pars = @parameters begin
154145
k = k
155-
s_a_0 = s_a_0
156-
s_b_0 = s_b_0
157-
v_a_0 = v_a_0
158-
v_b_0 = v_b_0
159146
l = l
160147
end
161148
vars = @variables begin
162-
s1(t) = s_a_0
163-
s2(t) = s_b_0
149+
sa(t) = sa
150+
sb(t) = sb
164151
f(t) = 0
165152
end
166153

167-
@named flange_a = MechanicalPort()
168-
@named flange_b = MechanicalPort()
154+
@named flange_a = MechanicalPort(; v = flange_a__v)
155+
@named flange_b = MechanicalPort(; v = flange_b__v)
169156

170-
eqs = [D(s1) ~ flange_a.v
171-
D(s2) ~ flange_b.v
172-
f ~ k * (s1 - s2 - l) #delta_s
157+
eqs = [D(sa) ~ flange_a.v
158+
D(sb) ~ flange_b.v
159+
f ~ k * (sa - sb - l) #delta_s
173160
flange_a.f ~ +f
174161
flange_b.f ~ -f]
175-
return compose(ODESystem(eqs, t, vars, pars; name = name,
176-
defaults = [flange_a.v => v_a_0, flange_b.v => v_b_0]),
162+
return compose(ODESystem(eqs, t, vars, pars; name = name,),
177163
flange_a,
178-
flange_b) #, flange_a.f => k * (s_a_0 - s_b_0 - l)
164+
flange_b) #, flange_a.f => k * (flange_a__s - flange_b__s - l)
179165
end
180166

181167
"""
@@ -213,3 +199,9 @@ Linear 1D translational damper
213199
flange_b.f ~ -f
214200
end
215201
end
202+
203+
204+
#=
205+
206+
207+
=#

src/Mechanical/Translational/sources.jl

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,53 +29,49 @@ Linear 1D position input source
2929
- `flange`: 1-dim. translational flange
3030
- `s`: real input. `s.u_start` accepts initial value and defaults to 0.0.
3131
"""
32-
@mtkmodel Position begin
33-
@parameters begin
34-
solves_force = false
35-
end
36-
@components begin
37-
flange = MechanicalPort(; v = 0.0)
38-
s = RealInput(; u_start = 0.0)
39-
end
40-
@variables begin
41-
x(t)
42-
end
43-
@equations begin
44-
D(x) ~ flange.v
45-
s.u ~ x
46-
!getdefault(solves_force) && 0 ~ flange.f
32+
@component function Position(; solves_force = true, s__u_start = 0, name)
33+
systems = @named begin
34+
flange = MechanicalPort()
35+
s = RealInput()
4736
end
37+
38+
vars = @variables x(t)
39+
40+
eqs = [D(x) ~ flange.v
41+
s.u ~ x]
42+
43+
!solves_force && push!(eqs, 0 ~ flange.f)
44+
45+
ODESystem(eqs, t, vars, []; name, systems, defaults = [flange.v => 0, s.u => s__u_start])
4846
end
4947

50-
@mtkmodel Velocity begin
51-
@parameters begin
52-
solves_force = false
53-
end
54-
@components begin
48+
@component function Velocity(; solves_force = true, name)
49+
systems = @named begin
5550
flange = MechanicalPort()
5651
v = RealInput()
5752
end
58-
@equations begin
59-
v.u ~ flange.v
60-
getdefault(solves_force) && 0 ~ flange.f
61-
end
53+
54+
eqs = [
55+
v.u ~ flange.v,
56+
]
57+
58+
!solves_force && push!(eqs, 0 ~ flange.f)
59+
60+
ODESystem(eqs, t, [], []; name, systems, defaults = [flange.v => 0])
6261
end
6362

64-
@mtkmodel Acceleration begin
65-
@parameters begin
66-
solves_force = false
67-
end
68-
@components begin
69-
flange = MechanicalPort(; v = 0.0)
70-
a = RealInput()
71-
end
72-
@variables begin
73-
v(t) = 0
63+
@component function Acceleration(solves_force = true; s__u_start = 0, name)
64+
systems = @named begin
65+
flange = MechanicalPort()
66+
a = RealInput(; u_start = s__u_start)
7467
end
7568

76-
@equations begin
77-
v ~ flange.v
78-
D(v) ~ a.u
79-
!getdefault(solves_force) && 0 ~ flange.f
80-
end
81-
end
69+
vars = @variables v(t) = 0
70+
71+
eqs = [v ~ flange.v
72+
D(v) ~ a.u]
73+
74+
!solves_force && push!(eqs, 0 ~ flange.f)
75+
76+
ODESystem(eqs, t, vars, []; name, systems, defaults = [flange.v => 0])
77+
end

0 commit comments

Comments
 (0)