blocksim 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- blocksim/__init__.py +524 -0
- blocksim/_logging.py +125 -0
- blocksim/aerospace.py +186 -0
- blocksim/analysis.py +1143 -0
- blocksim/astro.py +660 -0
- blocksim/atmosphere.py +150 -0
- blocksim/auxiliar.py +218 -0
- blocksim/avionics.py +992 -0
- blocksim/comm.py +237 -0
- blocksim/continuous.py +586 -0
- blocksim/control.py +159 -0
- blocksim/core.py +4522 -0
- blocksim/discontinuities.py +334 -0
- blocksim/discrete.py +833 -0
- blocksim/distributed.py +559 -0
- blocksim/driveline.py +496 -0
- blocksim/electrical.py +1086 -0
- blocksim/f16.py +409 -0
- blocksim/f16_hifi.py +250 -0
- blocksim/fmu.py +505 -0
- blocksim/fusion.py +984 -0
- blocksim/gemini.py +422 -0
- blocksim/hydraulics.py +816 -0
- blocksim/jsbsim.py +1284 -0
- blocksim/manipulator.py +928 -0
- blocksim/math.py +254 -0
- blocksim/mechanics.py +231 -0
- blocksim/power.py +1063 -0
- blocksim/process.py +595 -0
- blocksim/propulsion.py +1219 -0
- blocksim/py.typed +0 -0
- blocksim/radar.py +111 -0
- blocksim/robotics.py +164 -0
- blocksim/rotation.py +154 -0
- blocksim/simulator_claude.py +432 -0
- blocksim/sources.py +1846 -0
- blocksim/structural.py +613 -0
- blocksim/symbolic.py +934 -0
- blocksim/table.py +355 -0
- blocksim/thermal.py +395 -0
- blocksim/thermo.py +657 -0
- blocksim/vehicle.py +1274 -0
- blocksim-0.1.0.dist-info/METADATA +256 -0
- blocksim-0.1.0.dist-info/RECORD +47 -0
- blocksim-0.1.0.dist-info/WHEEL +4 -0
- blocksim-0.1.0.dist-info/licenses/COPYING +674 -0
- blocksim-0.1.0.dist-info/licenses/COPYING.LESSER +165 -0
blocksim/structural.py
ADDED
|
@@ -0,0 +1,613 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Structural dynamics: flexible bodies by modal superposition and by
|
|
3
|
+
finite elements.
|
|
4
|
+
|
|
5
|
+
A flexible body has infinitely many degrees of freedom and you never
|
|
6
|
+
want all of them. Two ways to cut it down live here, and they answer
|
|
7
|
+
different questions.
|
|
8
|
+
|
|
9
|
+
**Modal.** `BeamModes` gives the exact mode shapes and natural
|
|
10
|
+
frequencies of a uniform Euler-Bernoulli beam under the standard
|
|
11
|
+
boundary conditions, from the transcendental roots of the frequency
|
|
12
|
+
equation. `ModalBody` then integrates a handful of those modes as
|
|
13
|
+
independent damped oscillators driven by the forces projected onto them:
|
|
14
|
+
|
|
15
|
+
q_n'' + 2 zeta_n omega_n q_n' + omega_n^2 q_n = phi_n(x_f) F
|
|
16
|
+
|
|
17
|
+
That is two states per mode, whatever the geometry, and it is the right
|
|
18
|
+
model when you know which modes matter -- a wing's first bending and
|
|
19
|
+
torsion, an arm's first link mode -- and want them coupled into a
|
|
20
|
+
control loop.
|
|
21
|
+
|
|
22
|
+
**Finite element.** `FiniteElementBeam` assembles the consistent mass
|
|
23
|
+
and stiffness matrices of a chain of two-node beam elements and hands
|
|
24
|
+
back a state-space. That is 2 states per node rather than 2 per mode, so
|
|
25
|
+
it is the more expensive answer -- but it needs no prior knowledge of
|
|
26
|
+
which modes matter, it takes any boundary condition and any load
|
|
27
|
+
position, and its own eigenvalues *are* the modal answer, which is what
|
|
28
|
+
makes it the thing to check `BeamModes` against.
|
|
29
|
+
|
|
30
|
+
`LumpedChain` is the third, blunter option: n masses and springs in a
|
|
31
|
+
line. It is what a discretized rod is, it converges to the continuous
|
|
32
|
+
answer slowly, and it is the easiest model to reason about when the
|
|
33
|
+
structure is genuinely a chain rather than a beam.
|
|
34
|
+
|
|
35
|
+
SI throughout: metres, newtons, kilograms, Pa for modulus.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
import numpy as np
|
|
39
|
+
|
|
40
|
+
from .core import Block
|
|
41
|
+
|
|
42
|
+
__all__ = [
|
|
43
|
+
"BeamModes",
|
|
44
|
+
"FiniteElementBeam",
|
|
45
|
+
"LumpedChain",
|
|
46
|
+
"ModalBody",
|
|
47
|
+
"rayleigh_damping",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
#: `beta_n L` roots of the frequency equation, per boundary condition.
|
|
51
|
+
#: These are transcendental -- cos(bL)cosh(bL) = -1 for a cantilever --
|
|
52
|
+
#: so they are tabulated rather than derived at run time, to the
|
|
53
|
+
#: precision anyone needs.
|
|
54
|
+
BETA_L = {
|
|
55
|
+
#: clamped one end, free the other
|
|
56
|
+
"cantilever": (1.87510407, 4.69409113, 7.85475744, 10.99554073, 14.13716839),
|
|
57
|
+
#: pinned both ends: exactly n pi
|
|
58
|
+
"simply_supported": tuple(n * np.pi for n in range(1, 6)),
|
|
59
|
+
#: free both ends, rigid-body modes excluded
|
|
60
|
+
"free_free": (4.73004074, 7.85320462, 10.99560784, 14.13716549, 17.27875966),
|
|
61
|
+
#: clamped both ends -- the same roots as free-free, which is not a
|
|
62
|
+
#: coincidence: the two problems are adjoint
|
|
63
|
+
"clamped_clamped": (4.73004074, 7.85320462, 10.99560784, 14.13716549, 17.27875966),
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def rayleigh_damping(omega, alpha=0.0, beta=1e-4):
|
|
68
|
+
"""Modal damping ratios from proportional damping `C = alpha M + beta K`.
|
|
69
|
+
|
|
70
|
+
zeta_n = alpha / (2 omega_n) + beta omega_n / 2
|
|
71
|
+
|
|
72
|
+
The mass term damps low frequencies and the stiffness term high ones,
|
|
73
|
+
which is why the pair is fitted to two measured modes and why a
|
|
74
|
+
single ratio applied to everything is usually the honest alternative.
|
|
75
|
+
"""
|
|
76
|
+
omega = np.asarray(omega, dtype=float)
|
|
77
|
+
safe = np.where(omega > 0.0, omega, 1.0)
|
|
78
|
+
return np.where(omega > 0.0, alpha / (2.0 * safe) + beta * safe / 2.0, 0.0)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
# ==============================================================================
|
|
82
|
+
# analytic beam modes
|
|
83
|
+
# ==============================================================================
|
|
84
|
+
class BeamModes:
|
|
85
|
+
"""Mode shapes and natural frequencies of a uniform beam.
|
|
86
|
+
|
|
87
|
+
Euler-Bernoulli: shear deformation and rotary inertia neglected,
|
|
88
|
+
which is right when the beam is slender (length more than about ten
|
|
89
|
+
times its depth) and wrong in an interesting way when it is not.
|
|
90
|
+
|
|
91
|
+
omega_n = (beta_n L)^2 sqrt(EI / (rho A L^4))
|
|
92
|
+
|
|
93
|
+
`second_moment` is the second moment of area, the `I` of `EI`.
|
|
94
|
+
Shapes are mass-normalized, so `integral rho A phi_n^2 dx = 1` and
|
|
95
|
+
the modal equations come out with unit generalized mass -- which is
|
|
96
|
+
what makes `ModalBody` able to write them as plain oscillators.
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
def __init__(
|
|
100
|
+
self,
|
|
101
|
+
length=1.0,
|
|
102
|
+
E=200e9,
|
|
103
|
+
second_moment=8.33e-10,
|
|
104
|
+
density=7850.0,
|
|
105
|
+
area=1e-4,
|
|
106
|
+
condition="cantilever",
|
|
107
|
+
n_modes=3,
|
|
108
|
+
):
|
|
109
|
+
if condition not in BETA_L:
|
|
110
|
+
raise ValueError(
|
|
111
|
+
f"unknown boundary condition {condition!r}; use one of {sorted(BETA_L)}"
|
|
112
|
+
)
|
|
113
|
+
if not 1 <= n_modes <= len(BETA_L[condition]):
|
|
114
|
+
raise ValueError(
|
|
115
|
+
f"n_modes must be 1..{len(BETA_L[condition])} for {condition}"
|
|
116
|
+
)
|
|
117
|
+
if min(length, E, second_moment, density, area) <= 0.0:
|
|
118
|
+
raise ValueError("every beam property must be positive")
|
|
119
|
+
|
|
120
|
+
self.length = float(length)
|
|
121
|
+
self.E = float(E)
|
|
122
|
+
self.I = float(second_moment)
|
|
123
|
+
self.density = float(density)
|
|
124
|
+
self.area = float(area)
|
|
125
|
+
self.condition = condition
|
|
126
|
+
self.n_modes = int(n_modes)
|
|
127
|
+
self.beta_l = np.array(BETA_L[condition][: self.n_modes])
|
|
128
|
+
self.mass_per_length = self.density * self.area
|
|
129
|
+
|
|
130
|
+
@property
|
|
131
|
+
def frequencies(self):
|
|
132
|
+
"""Natural frequencies, rad/s."""
|
|
133
|
+
stiffness = self.E * self.I
|
|
134
|
+
return self.beta_l**2 * np.sqrt(
|
|
135
|
+
stiffness / (self.mass_per_length * self.length**4)
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
def shape(self, station, mode):
|
|
139
|
+
"""Mass-normalized mode shape `phi_mode(x)` at `station` metres
|
|
140
|
+
from the fixed (or first) end."""
|
|
141
|
+
if not 0 <= mode < self.n_modes:
|
|
142
|
+
raise ValueError(f"mode must be 0..{self.n_modes - 1}")
|
|
143
|
+
xi = np.asarray(station, dtype=float) / self.length
|
|
144
|
+
bl = self.beta_l[mode]
|
|
145
|
+
b = bl * xi
|
|
146
|
+
|
|
147
|
+
if self.condition == "simply_supported":
|
|
148
|
+
raw = np.sin(b)
|
|
149
|
+
elif self.condition == "cantilever":
|
|
150
|
+
sigma = (np.cosh(bl) + np.cos(bl)) / (np.sinh(bl) + np.sin(bl))
|
|
151
|
+
raw = np.cosh(b) - np.cos(b) - sigma * (np.sinh(b) - np.sin(b))
|
|
152
|
+
else: # free-free and clamped-clamped share their roots and form
|
|
153
|
+
sigma = (np.cosh(bl) - np.cos(bl)) / (np.sinh(bl) - np.sin(bl))
|
|
154
|
+
if self.condition == "free_free":
|
|
155
|
+
raw = np.cosh(b) + np.cos(b) - sigma * (np.sinh(b) + np.sin(b))
|
|
156
|
+
else:
|
|
157
|
+
raw = np.cosh(b) - np.cos(b) - sigma * (np.sinh(b) - np.sin(b))
|
|
158
|
+
|
|
159
|
+
return raw / self._normalization(mode)
|
|
160
|
+
|
|
161
|
+
def _normalization(self, mode):
|
|
162
|
+
"""sqrt(integral rho A phi^2 dx) for the raw shape, by
|
|
163
|
+
quadrature -- fine here because the shapes are smooth and this
|
|
164
|
+
runs once."""
|
|
165
|
+
xi = np.linspace(0.0, 1.0, 2001)
|
|
166
|
+
stations = xi * self.length
|
|
167
|
+
bl = self.beta_l[mode]
|
|
168
|
+
b = bl * xi
|
|
169
|
+
|
|
170
|
+
if self.condition == "simply_supported":
|
|
171
|
+
raw = np.sin(b)
|
|
172
|
+
elif self.condition == "cantilever":
|
|
173
|
+
sigma = (np.cosh(bl) + np.cos(bl)) / (np.sinh(bl) + np.sin(bl))
|
|
174
|
+
raw = np.cosh(b) - np.cos(b) - sigma * (np.sinh(b) - np.sin(b))
|
|
175
|
+
elif self.condition == "free_free":
|
|
176
|
+
sigma = (np.cosh(bl) - np.cos(bl)) / (np.sinh(bl) - np.sin(bl))
|
|
177
|
+
raw = np.cosh(b) + np.cos(b) - sigma * (np.sinh(b) + np.sin(b))
|
|
178
|
+
else:
|
|
179
|
+
sigma = (np.cosh(bl) - np.cos(bl)) / (np.sinh(bl) - np.sin(bl))
|
|
180
|
+
raw = np.cosh(b) - np.cos(b) - sigma * (np.sinh(b) - np.sin(b))
|
|
181
|
+
|
|
182
|
+
integral = np.trapezoid(self.mass_per_length * raw**2, stations)
|
|
183
|
+
return np.sqrt(integral)
|
|
184
|
+
|
|
185
|
+
def matrix(self, stations):
|
|
186
|
+
"""`Phi[k, n] = phi_n(stations[k])` -- the projection matrix that
|
|
187
|
+
turns forces at stations into modal forces, and modal
|
|
188
|
+
coordinates back into displacements."""
|
|
189
|
+
stations = np.atleast_1d(np.asarray(stations, dtype=float))
|
|
190
|
+
return np.column_stack([self.shape(stations, n) for n in range(self.n_modes)])
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
# ==============================================================================
|
|
194
|
+
# modal body
|
|
195
|
+
# ==============================================================================
|
|
196
|
+
class ModalBody(Block):
|
|
197
|
+
"""A flexible body as a handful of damped modal oscillators.
|
|
198
|
+
|
|
199
|
+
States
|
|
200
|
+
[q (n_modes), q_dot (n_modes)]
|
|
201
|
+
|
|
202
|
+
Inputs
|
|
203
|
+
forces at the input stations (N)
|
|
204
|
+
|
|
205
|
+
Outputs
|
|
206
|
+
displacements at the output stations (m), then the modal
|
|
207
|
+
coordinates
|
|
208
|
+
|
|
209
|
+
::
|
|
210
|
+
|
|
211
|
+
q_n'' + 2 zeta_n omega_n q_n' + omega_n^2 q_n = sum_k phi_n(x_k) F_k
|
|
212
|
+
w(x) = sum_n phi_n(x) q_n
|
|
213
|
+
|
|
214
|
+
Two states per mode, no matter how big the structure is. What the
|
|
215
|
+
truncation costs is the *static* contribution of the modes left out:
|
|
216
|
+
a load pushing on a station will produce slightly too little
|
|
217
|
+
deflection there, because the high modes that are stiff but not
|
|
218
|
+
absent have been dropped. Keep enough modes that the ones you dropped
|
|
219
|
+
are well above anything driving the structure.
|
|
220
|
+
"""
|
|
221
|
+
|
|
222
|
+
def __init__(
|
|
223
|
+
self, modes, force_stations, output_stations=None, damping=0.01, name=None
|
|
224
|
+
):
|
|
225
|
+
self.modes = modes
|
|
226
|
+
self.force_stations = np.atleast_1d(np.asarray(force_stations, dtype=float))
|
|
227
|
+
self.output_stations = (
|
|
228
|
+
self.force_stations
|
|
229
|
+
if output_stations is None
|
|
230
|
+
else np.atleast_1d(np.asarray(output_stations, dtype=float))
|
|
231
|
+
)
|
|
232
|
+
self.omega = modes.frequencies
|
|
233
|
+
self.zeta = (
|
|
234
|
+
np.full(modes.n_modes, float(damping))
|
|
235
|
+
if np.isscalar(damping)
|
|
236
|
+
else np.asarray(damping, dtype=float)
|
|
237
|
+
)
|
|
238
|
+
if self.zeta.size != modes.n_modes:
|
|
239
|
+
raise ValueError(
|
|
240
|
+
f"damping needs one ratio per mode ({modes.n_modes}), "
|
|
241
|
+
f"got {self.zeta.size}"
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
self.force_matrix = modes.matrix(self.force_stations) # (n_f, n_modes)
|
|
245
|
+
self.output_matrix = modes.matrix(self.output_stations) # (n_o, n_modes)
|
|
246
|
+
|
|
247
|
+
n = modes.n_modes
|
|
248
|
+
super().__init__(
|
|
249
|
+
num_inputs=self.force_stations.size,
|
|
250
|
+
num_outputs=self.output_stations.size + n,
|
|
251
|
+
num_states=2 * n,
|
|
252
|
+
name=name,
|
|
253
|
+
inputs=[(f"F{k}", "N") for k in range(self.force_stations.size)],
|
|
254
|
+
outputs=[(f"w{k}", "m") for k in range(self.output_stations.size)]
|
|
255
|
+
+ [(f"q{n_}", None) for n_ in range(n)],
|
|
256
|
+
states=[(f"q{n_}", None) for n_ in range(n)]
|
|
257
|
+
+ [(f"qd{n_}", None) for n_ in range(n)],
|
|
258
|
+
)
|
|
259
|
+
self.x0 = np.zeros(2 * n)
|
|
260
|
+
|
|
261
|
+
def displacement(self, x):
|
|
262
|
+
"""Physical deflection at the output stations."""
|
|
263
|
+
return self.output_matrix @ x[: self.modes.n_modes]
|
|
264
|
+
|
|
265
|
+
def derivative(self, t, x, u):
|
|
266
|
+
n = self.modes.n_modes
|
|
267
|
+
q, qd = x[:n], x[n:]
|
|
268
|
+
modal_force = self.force_matrix.T @ np.asarray(u, dtype=float)
|
|
269
|
+
acceleration = (
|
|
270
|
+
modal_force - 2.0 * self.zeta * self.omega * qd - self.omega**2 * q
|
|
271
|
+
)
|
|
272
|
+
return np.concatenate([qd, acceleration])
|
|
273
|
+
|
|
274
|
+
def output(self, t, x):
|
|
275
|
+
return np.concatenate([self.displacement(x), x[: self.modes.n_modes]])
|
|
276
|
+
|
|
277
|
+
# ---- symbolic form, for System.fuse() ---------------------------------
|
|
278
|
+
# A modal body is linear: constant modal masses, frequencies and
|
|
279
|
+
# projection matrices, assembled once in __init__. So it can write
|
|
280
|
+
# itself down, and a diagram containing one no longer has to fall
|
|
281
|
+
# back to the per-block path. Nothing here imports sympy -- `x` and
|
|
282
|
+
# `u` arrive as symbols and expressions, and the arithmetic below is
|
|
283
|
+
# ordinary Python operators on them.
|
|
284
|
+
#
|
|
285
|
+
# Zero coefficients are SKIPPED rather than multiplied by zero. The
|
|
286
|
+
# projection matrix of a mode with a node at a station is exactly
|
|
287
|
+
# zero there, and leaving those terms in makes the expression (and
|
|
288
|
+
# the Jacobian differentiated from it) denser than the structure is.
|
|
289
|
+
|
|
290
|
+
def symbolic_derivative(self, t, x, u):
|
|
291
|
+
n = self.modes.n_modes
|
|
292
|
+
q, qd = x[:n], x[n:]
|
|
293
|
+
rows = list(qd)
|
|
294
|
+
n_forces = self.force_matrix.shape[0]
|
|
295
|
+
for i in range(n):
|
|
296
|
+
modal_force = sum(
|
|
297
|
+
float(self.force_matrix[k, i]) * u[k]
|
|
298
|
+
for k in range(n_forces)
|
|
299
|
+
if self.force_matrix[k, i] != 0.0
|
|
300
|
+
)
|
|
301
|
+
zeta, omega = float(self.zeta[i]), float(self.omega[i])
|
|
302
|
+
rows.append(modal_force - 2.0 * zeta * omega * qd[i] - omega**2 * q[i])
|
|
303
|
+
return rows
|
|
304
|
+
|
|
305
|
+
def symbolic_output(self, t, x, u):
|
|
306
|
+
n = self.modes.n_modes
|
|
307
|
+
deflections = [
|
|
308
|
+
sum(
|
|
309
|
+
float(self.output_matrix[k, i]) * x[i]
|
|
310
|
+
for i in range(n)
|
|
311
|
+
if self.output_matrix[k, i] != 0.0
|
|
312
|
+
)
|
|
313
|
+
for k in range(self.output_matrix.shape[0])
|
|
314
|
+
]
|
|
315
|
+
return deflections + list(x[:n])
|
|
316
|
+
|
|
317
|
+
def static_deflection(self, forces):
|
|
318
|
+
"""Where the modes say the body settles under a steady load:
|
|
319
|
+
`sum phi_n phi_n^T F / omega_n^2`. Its shortfall against the
|
|
320
|
+
exact answer is exactly what truncation costs."""
|
|
321
|
+
modal = self.force_matrix.T @ np.asarray(forces, dtype=float)
|
|
322
|
+
return self.output_matrix @ (modal / self.omega**2)
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
# ==============================================================================
|
|
326
|
+
# finite elements
|
|
327
|
+
# ==============================================================================
|
|
328
|
+
class FiniteElementBeam:
|
|
329
|
+
"""A chain of two-node Euler-Bernoulli beam elements.
|
|
330
|
+
|
|
331
|
+
Two degrees of freedom per node -- transverse displacement and
|
|
332
|
+
rotation -- with the consistent mass matrix rather than a lumped
|
|
333
|
+
one, because a lumped mass matrix gets the frequencies wrong from
|
|
334
|
+
below while the consistent one is wrong from above, and the
|
|
335
|
+
consistent one converges faster.
|
|
336
|
+
|
|
337
|
+
Assembles `M` and `K`, applies the boundary condition by striking out
|
|
338
|
+
the constrained degrees of freedom, and exposes `frequencies` (the
|
|
339
|
+
generalized eigenproblem `K phi = omega^2 M phi`) and `state_space`
|
|
340
|
+
for handing to `blocksim.analysis` or wrapping in a `StateSpace`
|
|
341
|
+
block.
|
|
342
|
+
"""
|
|
343
|
+
|
|
344
|
+
def __init__(
|
|
345
|
+
self,
|
|
346
|
+
length=1.0,
|
|
347
|
+
E=200e9,
|
|
348
|
+
second_moment=8.33e-10,
|
|
349
|
+
density=7850.0,
|
|
350
|
+
area=1e-4,
|
|
351
|
+
elements=10,
|
|
352
|
+
condition="cantilever",
|
|
353
|
+
):
|
|
354
|
+
if condition not in BETA_L:
|
|
355
|
+
raise ValueError(f"unknown boundary condition {condition!r}")
|
|
356
|
+
if elements < 1:
|
|
357
|
+
raise ValueError("a beam needs at least one element")
|
|
358
|
+
self.length = float(length)
|
|
359
|
+
self.E = float(E)
|
|
360
|
+
self.I = float(second_moment)
|
|
361
|
+
self.density = float(density)
|
|
362
|
+
self.area = float(area)
|
|
363
|
+
self.elements = int(elements)
|
|
364
|
+
self.condition = condition
|
|
365
|
+
|
|
366
|
+
self.element_length = self.length / self.elements
|
|
367
|
+
self.n_nodes = self.elements + 1
|
|
368
|
+
self.M, self.K = self._assemble()
|
|
369
|
+
self.free = self._free_dofs()
|
|
370
|
+
|
|
371
|
+
def _element_matrices(self):
|
|
372
|
+
le = self.element_length
|
|
373
|
+
stiffness = (
|
|
374
|
+
self.E
|
|
375
|
+
* self.I
|
|
376
|
+
/ le**3
|
|
377
|
+
* np.array(
|
|
378
|
+
[
|
|
379
|
+
[12.0, 6.0 * le, -12.0, 6.0 * le],
|
|
380
|
+
[6.0 * le, 4.0 * le**2, -6.0 * le, 2.0 * le**2],
|
|
381
|
+
[-12.0, -6.0 * le, 12.0, -6.0 * le],
|
|
382
|
+
[6.0 * le, 2.0 * le**2, -6.0 * le, 4.0 * le**2],
|
|
383
|
+
]
|
|
384
|
+
)
|
|
385
|
+
)
|
|
386
|
+
mass = (
|
|
387
|
+
self.density
|
|
388
|
+
* self.area
|
|
389
|
+
* le
|
|
390
|
+
/ 420.0
|
|
391
|
+
* np.array(
|
|
392
|
+
[
|
|
393
|
+
[156.0, 22.0 * le, 54.0, -13.0 * le],
|
|
394
|
+
[22.0 * le, 4.0 * le**2, 13.0 * le, -3.0 * le**2],
|
|
395
|
+
[54.0, 13.0 * le, 156.0, -22.0 * le],
|
|
396
|
+
[-13.0 * le, -3.0 * le**2, -22.0 * le, 4.0 * le**2],
|
|
397
|
+
]
|
|
398
|
+
)
|
|
399
|
+
)
|
|
400
|
+
return mass, stiffness
|
|
401
|
+
|
|
402
|
+
def _assemble(self):
|
|
403
|
+
size = 2 * self.n_nodes
|
|
404
|
+
M = np.zeros((size, size))
|
|
405
|
+
K = np.zeros((size, size))
|
|
406
|
+
me, ke = self._element_matrices()
|
|
407
|
+
for e in range(self.elements):
|
|
408
|
+
dofs = np.arange(2 * e, 2 * e + 4)
|
|
409
|
+
M[np.ix_(dofs, dofs)] += me
|
|
410
|
+
K[np.ix_(dofs, dofs)] += ke
|
|
411
|
+
return M, K
|
|
412
|
+
|
|
413
|
+
def _free_dofs(self):
|
|
414
|
+
"""Which degrees of freedom survive the boundary condition."""
|
|
415
|
+
size = 2 * self.n_nodes
|
|
416
|
+
constrained = set()
|
|
417
|
+
if self.condition == "cantilever":
|
|
418
|
+
constrained |= {0, 1} # displacement and slope at the root
|
|
419
|
+
elif self.condition == "simply_supported":
|
|
420
|
+
constrained |= {0, size - 2} # pinned: displacement only
|
|
421
|
+
elif self.condition == "clamped_clamped":
|
|
422
|
+
constrained |= {0, 1, size - 2, size - 1}
|
|
423
|
+
# free-free constrains nothing, and keeps its two rigid-body modes
|
|
424
|
+
return np.array([d for d in range(size) if d not in constrained])
|
|
425
|
+
|
|
426
|
+
@property
|
|
427
|
+
def frequencies(self):
|
|
428
|
+
"""Natural frequencies of the constrained beam, rad/s, ascending.
|
|
429
|
+
|
|
430
|
+
Rigid-body modes of a free-free beam come out at (numerically)
|
|
431
|
+
zero and are kept, because their being there is the check that
|
|
432
|
+
the assembly is right.
|
|
433
|
+
"""
|
|
434
|
+
from scipy.linalg import eigh
|
|
435
|
+
|
|
436
|
+
free = self.free
|
|
437
|
+
eigenvalues = eigh(
|
|
438
|
+
self.K[np.ix_(free, free)],
|
|
439
|
+
self.M[np.ix_(free, free)],
|
|
440
|
+
eigvals_only=True,
|
|
441
|
+
)
|
|
442
|
+
return np.sqrt(np.clip(eigenvalues, 0.0, None))
|
|
443
|
+
|
|
444
|
+
def mode_shape(self, index):
|
|
445
|
+
"""Nodal displacements of one mode, with the constrained degrees
|
|
446
|
+
of freedom filled back in as zero."""
|
|
447
|
+
from scipy.linalg import eigh
|
|
448
|
+
|
|
449
|
+
free = self.free
|
|
450
|
+
_, vectors = eigh(self.K[np.ix_(free, free)], self.M[np.ix_(free, free)])
|
|
451
|
+
full = np.zeros(2 * self.n_nodes)
|
|
452
|
+
full[free] = vectors[:, index]
|
|
453
|
+
return full[::2] # the displacement degrees of freedom
|
|
454
|
+
|
|
455
|
+
def state_space(self, force_dofs=None, output_dofs=None, damping=0.0):
|
|
456
|
+
"""(A, B, C, D) of `M q'' + C q' + K q = F`, in the free degrees
|
|
457
|
+
of freedom, with `C = damping * M` for a simple proportional
|
|
458
|
+
model."""
|
|
459
|
+
free = self.free
|
|
460
|
+
M = self.M[np.ix_(free, free)]
|
|
461
|
+
K = self.K[np.ix_(free, free)]
|
|
462
|
+
n = free.size
|
|
463
|
+
M_inv = np.linalg.inv(M)
|
|
464
|
+
|
|
465
|
+
A = np.zeros((2 * n, 2 * n))
|
|
466
|
+
A[:n, n:] = np.eye(n)
|
|
467
|
+
A[n:, :n] = -M_inv @ K
|
|
468
|
+
A[n:, n:] = -damping * np.eye(n)
|
|
469
|
+
|
|
470
|
+
force_dofs = [free.size - 2] if force_dofs is None else list(force_dofs)
|
|
471
|
+
output_dofs = force_dofs if output_dofs is None else list(output_dofs)
|
|
472
|
+
|
|
473
|
+
B = np.zeros((2 * n, len(force_dofs)))
|
|
474
|
+
for k, dof in enumerate(force_dofs):
|
|
475
|
+
B[n:, k] = M_inv[:, dof]
|
|
476
|
+
C = np.zeros((len(output_dofs), 2 * n))
|
|
477
|
+
for k, dof in enumerate(output_dofs):
|
|
478
|
+
C[k, dof] = 1.0
|
|
479
|
+
return A, B, C, np.zeros((len(output_dofs), len(force_dofs)))
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
# ==============================================================================
|
|
483
|
+
# lumped chain
|
|
484
|
+
# ==============================================================================
|
|
485
|
+
class LumpedChain(Block):
|
|
486
|
+
"""n masses in a line, joined by springs and dampers.
|
|
487
|
+
|
|
488
|
+
States : [positions (n), velocities (n)]
|
|
489
|
+
Inputs : forces on each mass (N)
|
|
490
|
+
Outputs : positions (n), then velocities (n)
|
|
491
|
+
|
|
492
|
+
`anchored=True` ties the first mass to ground through another spring,
|
|
493
|
+
which is the difference between a structure and a free-floating
|
|
494
|
+
train. Free, the chain has a rigid-body mode at zero frequency and
|
|
495
|
+
conserves momentum; anchored, it does not and it does not.
|
|
496
|
+
|
|
497
|
+
The blunt discretization: it converges to a continuous rod slowly --
|
|
498
|
+
the highest modes are badly wrong at any n -- but it needs no mode
|
|
499
|
+
shapes and it is the honest model when the structure really is a
|
|
500
|
+
chain of masses.
|
|
501
|
+
"""
|
|
502
|
+
|
|
503
|
+
def __init__(
|
|
504
|
+
self,
|
|
505
|
+
masses=3,
|
|
506
|
+
mass=1.0,
|
|
507
|
+
stiffness=1000.0,
|
|
508
|
+
damping=0.5,
|
|
509
|
+
anchored=True,
|
|
510
|
+
x0=None,
|
|
511
|
+
name=None,
|
|
512
|
+
):
|
|
513
|
+
if masses < 1:
|
|
514
|
+
raise ValueError("a chain needs at least one mass")
|
|
515
|
+
self.n = int(masses)
|
|
516
|
+
self.mass = (
|
|
517
|
+
np.full(self.n, float(mass))
|
|
518
|
+
if np.isscalar(mass)
|
|
519
|
+
else np.asarray(mass, dtype=float)
|
|
520
|
+
)
|
|
521
|
+
n_springs = self.n if anchored else self.n - 1
|
|
522
|
+
self.stiffness = (
|
|
523
|
+
np.full(max(n_springs, 0), float(stiffness))
|
|
524
|
+
if np.isscalar(stiffness)
|
|
525
|
+
else np.asarray(stiffness, dtype=float)
|
|
526
|
+
)
|
|
527
|
+
self.damping = (
|
|
528
|
+
np.full(max(n_springs, 0), float(damping))
|
|
529
|
+
if np.isscalar(damping)
|
|
530
|
+
else np.asarray(damping, dtype=float)
|
|
531
|
+
)
|
|
532
|
+
self.anchored = bool(anchored)
|
|
533
|
+
if np.min(self.mass) <= 0.0:
|
|
534
|
+
raise ValueError("every mass must be positive")
|
|
535
|
+
|
|
536
|
+
super().__init__(
|
|
537
|
+
num_inputs=self.n,
|
|
538
|
+
num_outputs=2 * self.n,
|
|
539
|
+
num_states=2 * self.n,
|
|
540
|
+
name=name,
|
|
541
|
+
inputs=[(f"F{k}", "N") for k in range(self.n)],
|
|
542
|
+
outputs=[(f"x{k}", "m") for k in range(self.n)]
|
|
543
|
+
+ [(f"v{k}", "m/s") for k in range(self.n)],
|
|
544
|
+
states=[(f"x{k}", "m") for k in range(self.n)]
|
|
545
|
+
+ [(f"v{k}", "m/s") for k in range(self.n)],
|
|
546
|
+
)
|
|
547
|
+
self.x0 = np.zeros(2 * self.n) if x0 is None else np.asarray(x0, dtype=float)
|
|
548
|
+
|
|
549
|
+
def matrices(self):
|
|
550
|
+
"""(M, K, C) of the chain, as the assembled tridiagonal system."""
|
|
551
|
+
M = np.diag(self.mass)
|
|
552
|
+
K = np.zeros((self.n, self.n))
|
|
553
|
+
C = np.zeros((self.n, self.n))
|
|
554
|
+
offset = 0
|
|
555
|
+
if self.anchored:
|
|
556
|
+
K[0, 0] += self.stiffness[0]
|
|
557
|
+
C[0, 0] += self.damping[0]
|
|
558
|
+
offset = 1
|
|
559
|
+
for j in range(self.n - 1):
|
|
560
|
+
k = self.stiffness[j + offset]
|
|
561
|
+
c = self.damping[j + offset]
|
|
562
|
+
for matrix, value in ((K, k), (C, c)):
|
|
563
|
+
matrix[j, j] += value
|
|
564
|
+
matrix[j + 1, j + 1] += value
|
|
565
|
+
matrix[j, j + 1] -= value
|
|
566
|
+
matrix[j + 1, j] -= value
|
|
567
|
+
return M, K, C
|
|
568
|
+
|
|
569
|
+
@property
|
|
570
|
+
def frequencies(self):
|
|
571
|
+
"""Undamped natural frequencies, rad/s, ascending."""
|
|
572
|
+
from scipy.linalg import eigh
|
|
573
|
+
|
|
574
|
+
M, K, _ = self.matrices()
|
|
575
|
+
return np.sqrt(np.clip(eigh(K, M, eigvals_only=True), 0.0, None))
|
|
576
|
+
|
|
577
|
+
def derivative(self, t, x, u):
|
|
578
|
+
M, K, C = self.matrices()
|
|
579
|
+
position, velocity = x[: self.n], x[self.n :]
|
|
580
|
+
force = np.asarray(u, dtype=float) - K @ position - C @ velocity
|
|
581
|
+
return np.concatenate([velocity, force / self.mass])
|
|
582
|
+
|
|
583
|
+
def output(self, t, x):
|
|
584
|
+
return np.asarray(x, dtype=float)
|
|
585
|
+
|
|
586
|
+
# ---- symbolic form, for System.fuse() ---------------------------------
|
|
587
|
+
# M, K and C are constants of the chain, so this writes down without
|
|
588
|
+
# sympy: `x` and `u` arrive as symbols and expressions and the
|
|
589
|
+
# arithmetic below is ordinary Python operators on them.
|
|
590
|
+
#
|
|
591
|
+
# K and C are TRIDIAGONAL, and the loop skips their zeros rather
|
|
592
|
+
# than emitting `0.0 * x[j]`. For a hundred masses that is the
|
|
593
|
+
# difference between about seven terms per row and a hundred, and
|
|
594
|
+
# the analytic Jacobian is differentiated from these expressions --
|
|
595
|
+
# so a dense expression would mean a dense Jacobian for a structure
|
|
596
|
+
# that only ever couples neighbours.
|
|
597
|
+
|
|
598
|
+
def symbolic_derivative(self, t, x, u):
|
|
599
|
+
_, K, C = self.matrices()
|
|
600
|
+
position, velocity = x[: self.n], x[self.n :]
|
|
601
|
+
rows = list(velocity)
|
|
602
|
+
for i in range(self.n):
|
|
603
|
+
force = u[i]
|
|
604
|
+
for j in range(self.n):
|
|
605
|
+
if K[i, j] != 0.0:
|
|
606
|
+
force = force - float(K[i, j]) * position[j]
|
|
607
|
+
if C[i, j] != 0.0:
|
|
608
|
+
force = force - float(C[i, j]) * velocity[j]
|
|
609
|
+
rows.append(force / float(self.mass[i]))
|
|
610
|
+
return rows
|
|
611
|
+
|
|
612
|
+
def symbolic_output(self, t, x, u):
|
|
613
|
+
return list(x)
|