Skip to content

Commit b6b0f61

Browse files
Merge pull request #190 from ven-k/vkb/mechanical-refactor
Refactors Mechanical module with `@mtkmodel`
2 parents a2ecca0 + 21df461 commit b6b0f61

File tree

24 files changed

+667
-678
lines changed

24 files changed

+667
-678
lines changed

docs/src/connectors/connections.md

+21-21
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,8 +134,8 @@ 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)
138-
@named body = TV.Mass(m = 1, v_0 = 1)
137+
@named damping = TV.Damper(d = 1, flange_a.v = 1)
138+
@named body = TV.Mass(m = 1, v = 1)
139139
@named ground = TV.Fixed()
140140
141141
eqs = [connect(damping.flange_a, body.flange)
@@ -167,8 +167,8 @@ 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)
171-
@named body = TP.Mass(m = 1, v_0 = 1)
170+
@named damping = TP.Damper(d = 1, va = 1, vb = 0.0)
171+
@named body = TP.Mass(m = 1, v = 1)
172172
@named ground = TP.Fixed(s_0 = 0)
173173
174174
eqs = [connect(damping.flange_a, body.flange)
@@ -197,10 +197,10 @@ plot(p1, p2, p3)
197197

198198
The question then arises, can the position be plotted when using the Mechanical Translational Domain based on the Velocity Across variable? Yes, we can! There are 2 solutions:
199199

200-
1. the `Mass` component will add the position variable when the `s_0` parameter is used to set an initial position. Otherwise, the component does not track the position.
200+
1. the `Mass` component will add the position variable when the `s` parameter is used to set an initial position. Otherwise, the component does not track the position.
201201

202202
```julia
203-
@named body = TV.Mass(m = 1, v_0 = 1, s_0 = 0)
203+
@named body = TV.Mass(m = 1, v = 1, s = 0)
204204
```
205205

206206
2. implement a `PositionSensor`
@@ -220,31 +220,31 @@ 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, flange_a.v = 1)
227+
@named dp = TP.Damper(d = 1, va = 1, vb = 0.0, 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, flange_a__v = 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

241241
#### Mass
242242

243-
For both position- and velocity-based domains, we set the mass `m=1` and initial velocity `v_0=1`. Like the damper, the position domain requires the position initial conditions set as well.
243+
For both position- and velocity-based domains, we set the mass `m=1` and initial velocity `v=1`. Like the damper, the position domain requires the position initial conditions set as well.
244244

245245
```@example connections
246-
@named bv = TV.Mass(m = 1, v_0 = 1)
247-
@named bp = TP.Mass(m = 1, v_0 = 1, s_0 = 3)
246+
@named bv = TV.Mass(m = 1, v = 1)
247+
@named bp = TP.Mass(m = 1, v = 1, s = 3)
248248
nothing # hide
249249
```
250250

src/Hydraulic/IsothermalCompressible/components.jl

+1-1
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/MultiBody2D/components.jl

+23-22
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
@component function Link(; name, m, l, I, g, x1_0 = 0, y1_0 = 0)
2-
pars = @parameters begin
3-
m = m
4-
l = l
5-
I = I
6-
g = g
7-
x1_0 = x1_0
8-
y1_0 = y1_0
1+
@mtkmodel Link begin
2+
@parameters begin
3+
m
4+
l
5+
I
6+
g
7+
x1_0 = 0.0
8+
y1_0 = 0.0
99
end
1010

11-
vars = @variables begin
11+
@variables begin
1212
(A(t) = 0), [state_priority = 10]
1313
(dA(t) = 0), [state_priority = 10]
1414
(ddA(t) = 0), [state_priority = 10]
@@ -40,13 +40,16 @@
4040
ddy_cm(t) = 0
4141
end
4242

43-
@named TX1 = MechanicalPort()
44-
@named TY1 = MechanicalPort()
43+
@components begin
44+
TX1 = MechanicalPort()
45+
TY1 = MechanicalPort()
4546

46-
@named TX2 = MechanicalPort()
47-
@named TY2 = MechanicalPort()
47+
TX2 = MechanicalPort()
48+
TY2 = MechanicalPort()
49+
end
4850

49-
eqs = [D(A) ~ dA
51+
@equations begin
52+
D(A) ~ dA
5053
D(dA) ~ ddA
5154
D(x1) ~ dx1
5255
D(y1) ~ dy1
@@ -57,17 +60,17 @@
5760
D(y_cm) ~ dy_cm
5861
D(dy_cm) ~ ddy_cm
5962

60-
# x forces
63+
# x forces
6164
m * ddx_cm ~ fx1 + fx2
6265

63-
# y forces
66+
# y forces
6467
m * ddy_cm ~ m * g + fy1 + fy2
6568

66-
# torques
69+
# torques
6770
I * ddA ~ -fy1 * (x2 - x1) / 2 + fy2 * (x2 - x1) / 2 + fx1 * (y2 - y1) / 2 -
6871
fx2 * (y2 - y1) / 2
6972

70-
# geometry
73+
# geometry
7174
x2 ~ l * cos(A) + x1
7275
y2 ~ l * sin(A) + y1
7376
x_cm ~ l * cos(A) / 2 + x1
@@ -79,8 +82,6 @@
7982
TX2.f ~ fx2
8083
TX2.v ~ dx2
8184
TY2.f ~ fy2
82-
TY2.v ~ dy2]
83-
84-
return ODESystem(eqs, t, vars, pars; name = name, systems = [TX1, TY1, TX2, TY2],
85-
defaults = [TX1.v => 0, TY1.v => 0, TX2.v => 0, TY2.v => 0])
85+
TY2.v ~ dy2
86+
end
8687
end

0 commit comments

Comments
 (0)