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/aerospace.py ADDED
@@ -0,0 +1,186 @@
1
+ """
2
+ Flight dynamics: a rigid body in an atmosphere.
3
+
4
+ Two blocks, and both are thin wrappers over machinery that lives
5
+ elsewhere -- which is the point. `RigidBody6DOF` integrates the standard
6
+ twelve-state aircraft model, taking its attitude kinematics from
7
+ `blocksim.rotation` and its air properties from `blocksim.atmosphere`, so
8
+ there is exactly one quaternion convention and one ISA table in this
9
+ library rather than one per module. That was not always true: three
10
+ atmospheres and three sets of quaternion routines had grown up
11
+ separately and disagreed, and consolidating them is why those two
12
+ modules exist.
13
+
14
+ Where to go instead
15
+ -------------------
16
+ This module is the general rigid body. For a specific aircraft:
17
+
18
+ * `blocksim.f16` is the Stevens & Lewis / NASA TP-1538 F-16, in the
19
+ wind-frame state and imperial units those references use, with an
20
+ explicit note on which parts are exact and which are representative.
21
+ * `blocksim.f16_hifi` replaces its aerodynamics with the full TP-1538
22
+ lookup tables.
23
+ * `blocksim.jsbsim` imports a JSBSim aircraft definition and flies it,
24
+ which is the route to an aircraft somebody else has already validated.
25
+
26
+ Conventions
27
+ -----------
28
+ Quaternions are scalar-first and rotate body to inertial; Euler angles
29
+ are the aerospace 3-2-1 sequence; body axes are x forward, y right, z
30
+ down. `blocksim.rotation` fixes those for the whole library and explains
31
+ why the quaternion is never renormalized inside `derivative()`.
32
+
33
+ `StandardAtmosphere` takes GEOPOTENTIAL altitude, as the layer table it
34
+ reads is written in. Feeding it geometric altitude is a 5% density error
35
+ at the stratopause; see `blocksim.atmosphere` and
36
+ `examples/atmosphere/ex01`, which measures it.
37
+ """
38
+
39
+ import numpy as np
40
+ from .atmosphere import standard_atmosphere
41
+ from .core import Block
42
+ from .rotation import (
43
+ body_to_inertial,
44
+ inertia_and_inverse,
45
+ quaternion_rate,
46
+ )
47
+
48
+ __all__ = ["RigidBody6DOF", "StandardAtmosphere"]
49
+
50
+
51
+ class StandardAtmosphere(Block):
52
+ """The 1976 standard atmosphere as a block.
53
+
54
+ Inputs
55
+ [altitude above sea level, m]
56
+
57
+ Outputs
58
+ [temperature K, pressure Pa, density kg/m^3, speed of
59
+ sound m/s]
60
+
61
+ The column itself is `blocksim.atmosphere.standard_atmosphere`, which
62
+ carries every ISA layer to 51 km. This block used to fit two layers
63
+ and stop at 20 km, and disagreed with the library's other atmosphere
64
+ by 9% in density at 15 km -- the kind of difference that gets blamed
65
+ on the aerodynamics.
66
+
67
+ `dT` offsets the temperature for a hot or cold day, leaving the
68
+ pressure column standard.
69
+ """
70
+
71
+ def __init__(self, dT=0.0, name=None):
72
+ self.dT = float(dT)
73
+ super().__init__(
74
+ num_inputs=1,
75
+ num_outputs=4,
76
+ name=name,
77
+ inputs=[("h", "m")],
78
+ outputs=[("T", "K"), ("P", "Pa"), ("rho", "kg/m^3"), ("a", "m/s")],
79
+ )
80
+
81
+ def output(self, u):
82
+ return np.array(standard_atmosphere(float(u[0]), dT=self.dT))
83
+
84
+
85
+ class RigidBody6DOF(Block):
86
+ """Rigid Body 6 Degrees of Freedom equations of motion block.
87
+ States (13 elements):
88
+
89
+ [0:3] - Position p_xyz (m) in Inertial Frame
90
+ [3:6] - Velocity v_body (m/s) in Body Frame [u, v, w]
91
+ [6:10] - Attitude Quaternion q_wxyz [q0, q1, q2, q3]
92
+ [10:13]- Angular Rates omega_body (rad/s) in Body Frame [p, q, r]
93
+
94
+ Inputs:
95
+
96
+ [0:3] - External forces F_body [Fx, Fy, Fz] (N)
97
+ [3:6] - External moments M_body [Mx, My, Mz] (Nm)
98
+
99
+ Outputs:
100
+
101
+ [0:3] - Position p_xyz (m)
102
+ [3:6] - Body velocity v_body (m/s)
103
+ [6:10] - Quaternion q_wxyz
104
+ [10:13]- Angular rates omega_body (rad/s)
105
+ """
106
+
107
+ def __init__(self, mass=1.0, inertia=None, x0=None, name=None):
108
+ self.mass = float(mass)
109
+ self.I, self.I_inv = inertia_and_inverse(
110
+ np.eye(3) if inertia is None else inertia
111
+ )
112
+
113
+ super().__init__(
114
+ num_inputs=6,
115
+ num_outputs=13,
116
+ num_states=13,
117
+ name=name,
118
+ inputs=[
119
+ ("Fx", "N"),
120
+ ("Fy", "N"),
121
+ ("Fz", "N"),
122
+ ("Mx", "N*m"),
123
+ ("My", "N*m"),
124
+ ("Mz", "N*m"),
125
+ ],
126
+ outputs=[
127
+ ("px", "m"),
128
+ ("py", "m"),
129
+ ("pz", "m"),
130
+ ("u", "m/s"),
131
+ ("v", "m/s"),
132
+ ("w", "m/s"),
133
+ ("q0", None),
134
+ ("q1", None),
135
+ ("q2", None),
136
+ ("q3", None),
137
+ ("p", "rad/s"),
138
+ ("q", "rad/s"),
139
+ ("r", "rad/s"),
140
+ ],
141
+ states=[
142
+ ("px", "m"),
143
+ ("py", "m"),
144
+ ("pz", "m"),
145
+ ("u", "m/s"),
146
+ ("v", "m/s"),
147
+ ("w", "m/s"),
148
+ ("q0", None),
149
+ ("q1", None),
150
+ ("q2", None),
151
+ ("q3", None),
152
+ ("p", "rad/s"),
153
+ ("q", "rad/s"),
154
+ ("r", "rad/s"),
155
+ ],
156
+ )
157
+
158
+ # Default state: [0,0,0, 0,0,0, 1,0,0,0, 0,0,0]
159
+ self.x0 = np.array([0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], dtype=float)
160
+ if x0 is not None:
161
+ self.x0 = np.asarray(x0, dtype=float).copy()
162
+
163
+ def derivative(self, t, x, u):
164
+ v_body = x[3:6]
165
+ q = x[6:10]
166
+ omega = x[10:13]
167
+ F_body, M_body = u[0:3], u[3:6]
168
+
169
+ # The quaternion is NOT renormalized here. It used to be, and
170
+ # that quietly projects the state the integrator is holding, so
171
+ # its error estimate stops describing the trajectory it is on.
172
+ # `output()` normalizes instead, and reports the norm.
173
+ return np.concatenate(
174
+ [
175
+ body_to_inertial(q) @ v_body,
176
+ F_body / self.mass - np.cross(omega, v_body),
177
+ quaternion_rate(q, omega),
178
+ self.I_inv @ (M_body - np.cross(omega, self.I @ omega)),
179
+ ]
180
+ )
181
+
182
+ def output(self, t, x):
183
+ # Normalize quaternion output
184
+ out = x.copy()
185
+ out[6:10] = out[6:10] / np.linalg.norm(out[6:10])
186
+ return out