|
| 1 | +"""Utilities related to inertia tensor transformations. |
| 2 | +
|
| 3 | +This module centralizes dynamic helpers for applying the parallel axis |
| 4 | + theorem (PAT). It lives inside ``rocketpy.mathutils`` so that functionality |
| 5 | + depending on :class:`rocketpy.mathutils.function.Function` does not leak into |
| 6 | + generic utility modules such as ``rocketpy.tools``. |
| 7 | +""" |
| 8 | + |
| 9 | +from rocketpy.mathutils.function import Function |
| 10 | +from rocketpy.mathutils.vector_matrix import Vector |
| 11 | + |
| 12 | + |
| 13 | +def _pat_dynamic_helper(com_inertia_moment, mass, distance_vec_3d, axes_term_lambda): |
| 14 | + """Apply the PAT to inertia moments, supporting static and dynamic inputs.""" |
| 15 | + |
| 16 | + is_dynamic = ( |
| 17 | + isinstance(com_inertia_moment, Function) |
| 18 | + or isinstance(mass, Function) |
| 19 | + or isinstance(distance_vec_3d, Function) |
| 20 | + ) |
| 21 | + |
| 22 | + def get_val(arg, t): |
| 23 | + return arg(t) if isinstance(arg, Function) else arg |
| 24 | + |
| 25 | + if not is_dynamic: |
| 26 | + d_vec = Vector(distance_vec_3d) |
| 27 | + mass_term = mass * axes_term_lambda(d_vec) |
| 28 | + return com_inertia_moment + mass_term |
| 29 | + |
| 30 | + def new_source(t): |
| 31 | + d_vec_t = get_val(distance_vec_3d, t) |
| 32 | + mass_t = get_val(mass, t) |
| 33 | + inertia_t = get_val(com_inertia_moment, t) |
| 34 | + mass_term = mass_t * axes_term_lambda(d_vec_t) |
| 35 | + return inertia_t + mass_term |
| 36 | + |
| 37 | + return Function(new_source, inputs="t", outputs="Inertia (kg*m^2)") |
| 38 | + |
| 39 | + |
| 40 | +def _pat_dynamic_product_helper( |
| 41 | + com_inertia_product, mass, distance_vec_3d, product_term_lambda |
| 42 | +): |
| 43 | + """Apply the PAT to inertia products, supporting static and dynamic inputs.""" |
| 44 | + |
| 45 | + is_dynamic = ( |
| 46 | + isinstance(com_inertia_product, Function) |
| 47 | + or isinstance(mass, Function) |
| 48 | + or isinstance(distance_vec_3d, Function) |
| 49 | + ) |
| 50 | + |
| 51 | + def get_val(arg, t): |
| 52 | + return arg(t) if isinstance(arg, Function) else arg |
| 53 | + |
| 54 | + if not is_dynamic: |
| 55 | + d_vec = Vector(distance_vec_3d) |
| 56 | + mass_term = mass * product_term_lambda(d_vec) |
| 57 | + return com_inertia_product + mass_term |
| 58 | + |
| 59 | + def new_source(t): |
| 60 | + d_vec_t = get_val(distance_vec_3d, t) |
| 61 | + mass_t = get_val(mass, t) |
| 62 | + inertia_t = get_val(com_inertia_product, t) |
| 63 | + mass_term = mass_t * product_term_lambda(d_vec_t) |
| 64 | + return inertia_t + mass_term |
| 65 | + |
| 66 | + return Function(new_source, inputs="t", outputs="Inertia (kg*m^2)") |
| 67 | + |
| 68 | + |
| 69 | +# --- Public functions for the Parallel Axis Theorem --- |
| 70 | + |
| 71 | + |
| 72 | +def parallel_axis_theorem_I11(com_inertia_moment, mass, distance_vec_3d): |
| 73 | + """Apply PAT to the I11 inertia term. |
| 74 | +
|
| 75 | + Parameters |
| 76 | + ---------- |
| 77 | + com_inertia_moment : float or Function |
| 78 | + Inertia moment relative to the component center of mass. |
| 79 | + mass : float or Function |
| 80 | + Mass of the component. If a Function, it must map time to mass. |
| 81 | + distance_vec_3d : array-like or Function |
| 82 | + Displacement vector from the component COM to the reference COM. |
| 83 | +
|
| 84 | + Returns |
| 85 | + ------- |
| 86 | + float or Function |
| 87 | + Updated I11 value referenced to the new axis. |
| 88 | + """ |
| 89 | + |
| 90 | + return _pat_dynamic_helper( |
| 91 | + com_inertia_moment, mass, distance_vec_3d, lambda d_vec: d_vec.y**2 + d_vec.z**2 |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def parallel_axis_theorem_I22(com_inertia_moment, mass, distance_vec_3d): |
| 96 | + """Apply PAT to the I22 inertia term. |
| 97 | +
|
| 98 | + Parameters |
| 99 | + ---------- |
| 100 | + com_inertia_moment : float or Function |
| 101 | + Inertia moment relative to the component center of mass. |
| 102 | + mass : float or Function |
| 103 | + Mass of the component. If a Function, it must map time to mass. |
| 104 | + distance_vec_3d : array-like or Function |
| 105 | + Displacement vector from the component COM to the reference COM. |
| 106 | +
|
| 107 | + Returns |
| 108 | + ------- |
| 109 | + float or Function |
| 110 | + Updated I22 value referenced to the new axis. |
| 111 | + """ |
| 112 | + |
| 113 | + return _pat_dynamic_helper( |
| 114 | + com_inertia_moment, mass, distance_vec_3d, lambda d_vec: d_vec.x**2 + d_vec.z**2 |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +def parallel_axis_theorem_I33(com_inertia_moment, mass, distance_vec_3d): |
| 119 | + """Apply PAT to the I33 inertia term. |
| 120 | +
|
| 121 | + Parameters |
| 122 | + ---------- |
| 123 | + com_inertia_moment : float or Function |
| 124 | + Inertia moment relative to the component center of mass. |
| 125 | + mass : float or Function |
| 126 | + Mass of the component. If a Function, it must map time to mass. |
| 127 | + distance_vec_3d : array-like or Function |
| 128 | + Displacement vector from the component COM to the reference COM. |
| 129 | +
|
| 130 | + Returns |
| 131 | + ------- |
| 132 | + float or Function |
| 133 | + Updated I33 value referenced to the new axis. |
| 134 | + """ |
| 135 | + |
| 136 | + return _pat_dynamic_helper( |
| 137 | + com_inertia_moment, mass, distance_vec_3d, lambda d_vec: d_vec.x**2 + d_vec.y**2 |
| 138 | + ) |
| 139 | + |
| 140 | + |
| 141 | +def parallel_axis_theorem_I12(com_inertia_product, mass, distance_vec_3d): |
| 142 | + """Apply PAT to the I12 inertia product. |
| 143 | +
|
| 144 | + Parameters |
| 145 | + ---------- |
| 146 | + com_inertia_product : float or Function |
| 147 | + Product of inertia relative to the component center of mass. |
| 148 | + mass : float or Function |
| 149 | + Mass of the component. If a Function, it must map time to mass. |
| 150 | + distance_vec_3d : array-like or Function |
| 151 | + Displacement vector from the component COM to the reference COM. |
| 152 | +
|
| 153 | + Returns |
| 154 | + ------- |
| 155 | + float or Function |
| 156 | + Updated I12 value referenced to the new axis. |
| 157 | + """ |
| 158 | + |
| 159 | + return _pat_dynamic_product_helper( |
| 160 | + com_inertia_product, mass, distance_vec_3d, lambda d_vec: d_vec.x * d_vec.y |
| 161 | + ) |
| 162 | + |
| 163 | + |
| 164 | +def parallel_axis_theorem_I13(com_inertia_product, mass, distance_vec_3d): |
| 165 | + """Apply PAT to the I13 inertia product. |
| 166 | +
|
| 167 | + Parameters |
| 168 | + ---------- |
| 169 | + com_inertia_product : float or Function |
| 170 | + Product of inertia relative to the component center of mass. |
| 171 | + mass : float or Function |
| 172 | + Mass of the component. If a Function, it must map time to mass. |
| 173 | + distance_vec_3d : array-like or Function |
| 174 | + Displacement vector from the component COM to the reference COM. |
| 175 | +
|
| 176 | + Returns |
| 177 | + ------- |
| 178 | + float or Function |
| 179 | + Updated I13 value referenced to the new axis. |
| 180 | + """ |
| 181 | + |
| 182 | + return _pat_dynamic_product_helper( |
| 183 | + com_inertia_product, mass, distance_vec_3d, lambda d_vec: d_vec.x * d_vec.z |
| 184 | + ) |
| 185 | + |
| 186 | + |
| 187 | +def parallel_axis_theorem_I23(com_inertia_product, mass, distance_vec_3d): |
| 188 | + """Apply PAT to the I23 inertia product. |
| 189 | +
|
| 190 | + Parameters |
| 191 | + ---------- |
| 192 | + com_inertia_product : float or Function |
| 193 | + Product of inertia relative to the component center of mass. |
| 194 | + mass : float or Function |
| 195 | + Mass of the component. If a Function, it must map time to mass. |
| 196 | + distance_vec_3d : array-like or Function |
| 197 | + Displacement vector from the component COM to the reference COM. |
| 198 | +
|
| 199 | + Returns |
| 200 | + ------- |
| 201 | + float or Function |
| 202 | + Updated I23 value referenced to the new axis. |
| 203 | + """ |
| 204 | + |
| 205 | + return _pat_dynamic_product_helper( |
| 206 | + com_inertia_product, mass, distance_vec_3d, lambda d_vec: d_vec.y * d_vec.z |
| 207 | + ) |
| 208 | + |
| 209 | + |
| 210 | +__all__ = [ |
| 211 | + "parallel_axis_theorem_I11", |
| 212 | + "parallel_axis_theorem_I22", |
| 213 | + "parallel_axis_theorem_I33", |
| 214 | + "parallel_axis_theorem_I12", |
| 215 | + "parallel_axis_theorem_I13", |
| 216 | + "parallel_axis_theorem_I23", |
| 217 | +] |
0 commit comments