|
| 1 | +""" |
| 2 | +test_pendulum_damping.py |
| 3 | +
|
| 4 | +Physics ground-truth test for Wire pendulum damping in py_crane. |
| 5 | +
|
| 6 | +Validates that a freely swinging pendulum (zero crane acceleration) damps its |
| 7 | +amplitude at the rate predicted by the Q-factor definition: |
| 8 | +
|
| 9 | + t_half = 2 * Q * ln(2) / omega_n |
| 10 | +
|
| 11 | +For default parameters (L=10 m, Q=50), amplitude should halve after ~70 simulation |
| 12 | +steps at dt=1.0 s. The buggy implementation (boom.py line 629 pre-fix) produces |
| 13 | +half-amplitude at ~35 steps — a factor-of-2 error that this test detects. |
| 14 | +""" |
| 15 | + |
| 16 | +import logging |
| 17 | +import math |
| 18 | + |
| 19 | +import matplotlib.pyplot as plt |
| 20 | +import numpy as np |
| 21 | +import pytest |
| 22 | + |
| 23 | +from py_crane.boom import Wire |
| 24 | +from py_crane.crane import Crane |
| 25 | + |
| 26 | +logger = logging.getLogger(__name__) |
| 27 | +logger.setLevel(logging.INFO) |
| 28 | +np.set_printoptions(precision=4, suppress=True) |
| 29 | + |
| 30 | + |
| 31 | +def build_test_crane(length: float = 10.0, q_factor: float = 50.0) -> Crane: |
| 32 | + """Build a minimal crane with a single pendulum wire for physics testing.""" |
| 33 | + crane = Crane() |
| 34 | + crane.add_boom( |
| 35 | + "pedestal", |
| 36 | + description="Fixed support", |
| 37 | + mass=100.0, |
| 38 | + boom=(length, 0.0, 0.0), |
| 39 | + ) |
| 40 | + crane.add_boom( |
| 41 | + "wire", |
| 42 | + description="Pendulum wire under test", |
| 43 | + mass=1.0, |
| 44 | + mass_center=1.0, |
| 45 | + boom=(length, np.pi, 0.0), |
| 46 | + q_factor=q_factor, |
| 47 | + ) |
| 48 | + crane.calc_statics_dynamics(None) |
| 49 | + return crane |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize( |
| 53 | + "length,q_factor", |
| 54 | + [ |
| 55 | + (10.0, 50.0), # default crane parameters |
| 56 | + (5.0, 30.0), # shorter wire, lower Q |
| 57 | + (20.0, 80.0), # longer wire, higher Q |
| 58 | + ], |
| 59 | +) |
| 60 | +def test_free_decay_half_amplitude(length: float, q_factor: float) -> None: |
| 61 | + """ |
| 62 | + Free-decay test: pendulum with zero crane acceleration should halve its |
| 63 | + angular velocity amplitude in t_half = 2*Q*ln2/omega_n seconds. |
| 64 | +
|
| 65 | + This test is the physics ground truth for boom.py line 629. It catches the |
| 66 | + known bug where `v *= 1 - dt/damping_time` was used instead of |
| 67 | + `v *= exp(-dt / (2 * damping_time))`, which caused the pendulum to damp |
| 68 | + approximately 2x faster than the Q-factor definition requires. |
| 69 | +
|
| 70 | + Parameters |
| 71 | + ---------- |
| 72 | + length : float |
| 73 | + Wire length in metres. |
| 74 | + q_factor : float |
| 75 | + Quality factor Q (energy stored / energy lost per radian). |
| 76 | + """ |
| 77 | + # --- Setup --- |
| 78 | + dt = 1.0 # simulation timestep (seconds) |
| 79 | + g = 9.81 # gravity (m/s²) |
| 80 | + start_speed = 1.0 # initial load velocity (m/s) -> theta_dot = start_speed / length |
| 81 | + |
| 82 | + crane = build_test_crane(length=length, q_factor=q_factor) |
| 83 | + wire = crane.boom_by_name("wire") |
| 84 | + assert isinstance(wire, Wire), "Not only Boom" |
| 85 | + |
| 86 | + # Kick the pendulum: set initial load velocity, leave crane stationary |
| 87 | + wire.cm_v[0] = start_speed |
| 88 | + crane.d_velocity = np.zeros(3) # zero crane acceleration throughout |
| 89 | + |
| 90 | + # --- Analytically derived expected half-amplitude step --- |
| 91 | + omega_n = math.sqrt(g / length) |
| 92 | + # gamma = omega_n / (2*Q) is the amplitude decay rate |
| 93 | + # t_half = ln(2) / gamma = 2*Q*ln(2) / omega_n |
| 94 | + t_half_expected = 2.0 * q_factor * math.log(2.0) / omega_n |
| 95 | + tolerance = 0.15 # ±15% — wide enough for discrete peak-detection error (~5%), |
| 96 | + # narrow enough to catch the factor-of-2 bug (~100% off) |
| 97 | + |
| 98 | + # Run for 3× the expected half-amplitude time to ensure we observe the crossing |
| 99 | + n_steps = int(3 * t_half_expected / dt) + 1 |
| 100 | + |
| 101 | + # --- Simulate free decay --- |
| 102 | + theta_dot_peaks: list[tuple[int, float]] = [] |
| 103 | + theta_dots: list[float] = [] |
| 104 | + |
| 105 | + for step in range(n_steps): |
| 106 | + crane.do_step(step * dt, dt) |
| 107 | + theta_dot = abs(wire.cm_v[0]) / wire.length |
| 108 | + theta_dots.append(theta_dot) |
| 109 | + |
| 110 | + # Detect local maxima (oscillation peaks) |
| 111 | + if step >= 2: |
| 112 | + prev, curr, _ = theta_dots[-3], theta_dots[-2], theta_dots[-1] |
| 113 | + if curr >= prev and curr >= theta_dots[-1]: |
| 114 | + theta_dot_peaks.append((step - 1, curr)) |
| 115 | + |
| 116 | + assert len(theta_dot_peaks) >= 3, ( |
| 117 | + f"Too few oscillation peaks detected ({len(theta_dot_peaks)}) — " |
| 118 | + f"check that the pendulum is actually oscillating. " |
| 119 | + f"L={length}, Q={q_factor}" |
| 120 | + ) |
| 121 | + |
| 122 | + # --- Find step at which peak amplitude first drops below half of initial peak --- |
| 123 | + initial_peak_amplitude = theta_dot_peaks[0][1] |
| 124 | + half_amplitude = initial_peak_amplitude / 2.0 |
| 125 | + |
| 126 | + half_amplitude_step = None |
| 127 | + for step_i, peak_v in theta_dot_peaks: |
| 128 | + if peak_v < half_amplitude: |
| 129 | + half_amplitude_step = step_i |
| 130 | + break |
| 131 | + |
| 132 | + assert half_amplitude_step is not None, ( |
| 133 | + f"Amplitude never halved within {n_steps} steps. " |
| 134 | + f"L={length}, Q={q_factor}, expected at step ~{t_half_expected:.0f}" |
| 135 | + ) |
| 136 | + |
| 137 | + # --- Assert within tolerance of analytical prediction --- |
| 138 | + lower = t_half_expected * (1.0 - tolerance) |
| 139 | + upper = t_half_expected * (1.0 + tolerance) |
| 140 | + |
| 141 | + assert lower <= half_amplitude_step <= upper, ( |
| 142 | + f"Pendulum amplitude halved at step {half_amplitude_step}, " |
| 143 | + f"but expected between {lower:.0f} and {upper:.0f} steps " |
| 144 | + f"(analytical: {t_half_expected:.1f} steps = 2*Q*ln2/omega_n). " |
| 145 | + f"L={length}, Q={q_factor}. " |
| 146 | + f"If half-amplitude step is ~{t_half_expected / 2:.0f}, " |
| 147 | + f"boom.py line 629 is using the energy decay constant instead of the " |
| 148 | + f"amplitude decay constant — see bug report." |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +def test_free_decay_default_parameters(*, show: bool) -> None: |
| 153 | + """ |
| 154 | + Regression test for default crane parameters (L=10 m, Q=50). |
| 155 | +
|
| 156 | + For these parameters, amplitude should halve after approximately 70 simulation |
| 157 | + steps (dt=1.0 s). This is the exact configuration in which the original bug |
| 158 | + was detected — the buggy code produced half-amplitude at step ~35. |
| 159 | +
|
| 160 | + Empirically measured (correct physics): step 67. |
| 161 | + Analytical prediction: 70.0 steps. |
| 162 | + """ |
| 163 | + # This is a dedicated regression test for the specific configuration |
| 164 | + # used in crane-controller's reward_comparison.md — do not change parameters. |
| 165 | + L = 10.0 |
| 166 | + Q = 50.0 # 50.0 |
| 167 | + dt = 1.0 |
| 168 | + g = 9.81 |
| 169 | + |
| 170 | + crane = build_test_crane(length=L, q_factor=Q) |
| 171 | + wire = crane.boom_by_name("wire") |
| 172 | + assert isinstance(wire, Wire), "Not only Boom" |
| 173 | + wire.cm_v[0] = 1.0 # 1 m/s initial load velocity |
| 174 | + crane.d_velocity = np.zeros(3) |
| 175 | + |
| 176 | + theta_dot_peaks: list[tuple[int, float]] = [] |
| 177 | + theta_dots: list[float] = [] |
| 178 | + times: list[float] = [0.0] |
| 179 | + speeds: list[float] = [wire.cm_v[0]] |
| 180 | + |
| 181 | + for step in range(210): # 3× expected half-amplitude time of 70 steps |
| 182 | + crane.do_step(step * dt, dt) |
| 183 | + times.append(step * dt) |
| 184 | + speeds.append(wire.cm_v[0]) |
| 185 | + theta_dot = abs(wire.cm_v[0]) / wire.length |
| 186 | + theta_dots.append(theta_dot) |
| 187 | + if step >= 2: |
| 188 | + prev, curr = theta_dots[-3], theta_dots[-2] |
| 189 | + if curr >= prev and curr >= theta_dots[-1]: |
| 190 | + theta_dot_peaks.append((step - 1, curr)) |
| 191 | + |
| 192 | + if show: |
| 193 | + _, ax = plt.subplots(1) |
| 194 | + ax.plot(times, speeds, label="speed") |
| 195 | + ax.plot(times, [np.exp(-t / wire.damping_time) for t in times], label="damping") |
| 196 | + plt.title(f"Damping time: {wire.damping_time}, Q: {wire.q_factor}") |
| 197 | + plt.show() |
| 198 | + |
| 199 | + initial_peak = theta_dot_peaks[0][1] |
| 200 | + half_amplitude_step = next((s for s, v in theta_dot_peaks if v < initial_peak / 2), None) |
| 201 | + |
| 202 | + omega_n = math.sqrt(g / L) |
| 203 | + expected = 2.0 * Q * math.log(2.0) / omega_n # 70.0 steps |
| 204 | + |
| 205 | + assert half_amplitude_step is not None, "Amplitude never halved — check damping implementation" |
| 206 | + |
| 207 | + assert abs(half_amplitude_step - expected) / expected < 0.15, ( |
| 208 | + f"Half-amplitude at step {half_amplitude_step}, expected ~{expected:.0f} " |
| 209 | + f"(±15%). " |
| 210 | + f"Buggy boom.py produces ~35 steps (energy constant used on amplitude). " |
| 211 | + f"Correct implementation produces ~70 steps (amplitude constant exp(-dt/(2*tau)))." |
| 212 | + ) |
| 213 | + |
| 214 | + |
| 215 | +if __name__ == "__main__": |
| 216 | + retcode = pytest.main(["-rA", "-v", "--rootdir", "../", "--show", "False", __file__]) |
| 217 | + assert retcode == 0, f"Non-zero return code {retcode}" |
| 218 | + test_free_decay_default_parameters(show=True) |
| 219 | + test_free_decay_half_amplitude(10, 50) # default crane parameters |
| 220 | + test_free_decay_half_amplitude(5.0, 30.0) # shorter wire, lower Q |
| 221 | + test_free_decay_half_amplitude(20.0, 80.0) # longer wire, higher Q |
0 commit comments