Skip to content

Commit ef096b9

Browse files
author
Brad Carman
committed
fixed connections.md
1 parent 45a9f3a commit ef096b9

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

docs/src/connectors/connections.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ using ModelingToolkitStandardLibrary
136136
const TV = ModelingToolkitStandardLibrary.Mechanical.Translational
137137
138138
systems = @named begin
139-
damping = TV.Damper(d = 1, flange_a.v = 1)
140-
body = TV.Mass(m = 1, v = 1)
139+
damping = TV.Damper(d = 1)
140+
body = TV.Mass(m = 1)
141141
ground = TV.Fixed()
142142
end
143143
@@ -155,7 +155,7 @@ nothing # hide
155155
As expected, we have a similar solution…
156156

157157
```@example connections
158-
prob = ODEProblem(sys, [], (0, 10.0), [])
158+
prob = ODEProblem(sys, [], (0, 10.0), []; initialization_eqs=[sys.body.s ~ 0, sys.body.v ~ 1])
159159
sol_v = solve(prob)
160160
161161
p1 = plot(sol_v, idxs = [body.v])
@@ -228,7 +228,7 @@ In this problem, we have a mass, spring, and damper which are connected to a fix
228228
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`.
229229

230230
```@example connections
231-
@named dv = TV.Damper(d = 1, flange_a.v = 1)
231+
@named dv = TV.Damper(d = 1)
232232
@named dp = TP.Damper(d = 1, va = 1, vb = 0.0, flange_a__s = 3, flange_b__s = 1)
233233
nothing # hide
234234
```
@@ -238,7 +238,7 @@ nothing # hide
238238
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`.
239239

240240
```@example connections
241-
@named sv = TV.Spring(k = 1, flange_a__v = 1, delta_s = 1)
241+
@named sv = TV.Spring(k = 1)
242242
@named sp = TP.Spring(k = 1, flange_a__s = 3, flange_b__s = 1, l = 1)
243243
nothing # hide
244244
```
@@ -248,7 +248,7 @@ nothing # hide
248248
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.
249249

250250
```@example connections
251-
@named bv = TV.Mass(m = 1, v = 1)
251+
@named bv = TV.Mass(m = 1)
252252
@named bp = TP.Mass(m = 1, v = 1, s = 3)
253253
nothing # hide
254254
```
@@ -270,7 +270,7 @@ As can be seen, the position-based domain requires more initial condition inform
270270
Let's define a quick function to simplify and solve the 2 different systems. Note, we will solve with a fixed time step and a set tolerance to compare the numerical differences.
271271

272272
```@example connections
273-
function simplify_and_solve(damping, spring, body, ground)
273+
function simplify_and_solve(damping, spring, body, ground; initialization_eqs = Equation[])
274274
eqs = [connect(spring.flange_a, body.flange, damping.flange_a)
275275
connect(spring.flange_b, damping.flange_b, ground.flange)]
276276
@@ -280,8 +280,8 @@ function simplify_and_solve(damping, spring, body, ground)
280280
281281
println.(full_equations(sys))
282282
283-
prob = ODEProblem(sys, [], (0, 10.0), [])
284-
sol = solve(prob; dt = 0.1, adaptive = false, reltol = 1e-9, abstol = 1e-9)
283+
prob = ODEProblem(sys, [], (0, 10.0), []; initialization_eqs)
284+
sol = solve(prob; abstol=1e-9, reltol=1e-9)
285285
286286
return sol
287287
end
@@ -291,7 +291,12 @@ nothing # hide
291291
Now let's solve the velocity domain model
292292

293293
```@example connections
294-
solv = simplify_and_solve(dv, sv, bv, gv);
294+
initialization_eqs = [
295+
bv.s ~ 3
296+
bv.v ~ 1
297+
sv.delta_s ~ 1
298+
]
299+
solv = simplify_and_solve(dv, sv, bv, gv; initialization_eqs);
295300
nothing # hide
296301
```
297302

@@ -346,12 +351,11 @@ By definition, the spring stretch is
346351
\Delta s = s - s_{b_0} - l
347352
```
348353

349-
Which means both systems are actually solving the same exact system. We can plot the numerical difference between the 2 systems and see the result is negligible.
354+
Which means both systems are actually solving the same exact system. We can plot the numerical difference between the 2 systems and see the result is negligible (much less than the tolerance of 1e-9).
350355

351356
```@example connections
352357
plot(title = "numerical difference: vel. vs. pos. domain", xlabel = "time [s]",
353358
ylabel = "solv[bv.v] .- solp[bp.v]")
354359
time = 0:0.1:10
355360
plot!(time, (solv(time)[bv.v] .- solp(time)[bp.v]), label = "")
356-
Plots.ylims!(-1e-15, 1e-15)
357361
```

src/Mechanical/Translational/components.jl

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ Sliding mass with inertia
6565
@named flange = MechanicalPort()
6666

6767
vars = @variables begin
68-
s(t)
69-
v(t)
70-
f(t)
68+
s(t), [guess=0]
69+
v(t), [guess=0]
70+
f(t), [guess=0]
7171
end
7272

7373
eqs = [flange.v ~ v
@@ -98,22 +98,21 @@ Linear 1D translational spring
9898
- `flange_a`: 1-dim. translational flange on one side of spring
9999
- `flange_b`: 1-dim. translational flange on opposite side of spring
100100
"""
101-
@component function Spring(; name, k, delta_s = 0.0, flange_a__v = 0.0, flange_b__v = 0.0)
102-
Spring(REL; name, k, delta_s, flange_a__v, flange_b__v)
101+
@component function Spring(; name, k)
102+
Spring(REL; name, k)
103103
end # default
104104

105-
@component function Spring(::Val{:relative}; name, k, delta_s = 0.0, flange_a__v = 0.0,
106-
flange_b__v = 0.0)
105+
@component function Spring(::Val{:relative}; name, k)
107106
pars = @parameters begin
108107
k = k
109108
end
110109
vars = @variables begin
111-
delta_s(t) = delta_s
112-
f(t)
110+
delta_s(t), [guess = 0]
111+
f(t), [guess=0]
113112
end
114113

115-
@named flange_a = MechanicalPort(; v = flange_a__v)
116-
@named flange_b = MechanicalPort(; v = flange_b__v)
114+
@named flange_a = MechanicalPort()
115+
@named flange_b = MechanicalPort()
117116

118117
eqs = [D(delta_s) ~ flange_a.v - flange_b.v
119118
f ~ k * delta_s
@@ -125,20 +124,19 @@ end # default
125124
end
126125

127126
const ABS = Val(:absolute)
128-
@component function Spring(::Val{:absolute}; name, k, sa = 0, sb = 0, flange_a__v = 0.0,
129-
flange_b__v = 0.0, l = 0)
127+
@component function Spring(::Val{:absolute}; name, k, l = 0)
130128
pars = @parameters begin
131129
k = k
132130
l = l
133131
end
134132
vars = @variables begin
135-
sa(t) = sa
136-
sb(t) = sb
137-
f(t)
133+
sa(t), [guess=0]
134+
sb(t), [guess=0]
135+
f(t), [guess=0]
138136
end
139137

140-
@named flange_a = MechanicalPort(; v = flange_a__v)
141-
@named flange_b = MechanicalPort(; v = flange_b__v)
138+
@named flange_a = MechanicalPort()
139+
@named flange_b = MechanicalPort()
142140

143141
eqs = [D(sa) ~ flange_a.v
144142
D(sb) ~ flange_b.v
@@ -169,8 +167,8 @@ Linear 1D translational damper
169167
d
170168
end
171169
@variables begin
172-
v(t)
173-
f(t)
170+
v(t), [guess=0]
171+
f(t), [guess=0]
174172
end
175173

176174
@components begin

0 commit comments

Comments
 (0)