|
| 1 | +package com.thealgorithms.physics; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.DisplayName; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +/** |
| 11 | + * Unit tests for {@link DampedOscillator}. |
| 12 | + * |
| 13 | + * <p>Tests focus on: |
| 14 | + * <ul> |
| 15 | + * <li>Constructor validation</li> |
| 16 | + * <li>Analytical displacement for underdamped and overdamped parameterizations</li> |
| 17 | + * <li>Basic numeric integration sanity using explicit Euler for small step sizes</li> |
| 18 | + * <li>Method argument validation (null/invalid inputs)</li> |
| 19 | + * </ul> |
| 20 | + */ |
| 21 | +@DisplayName("DampedOscillator — unit tests") |
| 22 | +public class DampedOscillatorTest { |
| 23 | + |
| 24 | + private static final double TOLERANCE = 1e-3; |
| 25 | + |
| 26 | + @Test |
| 27 | + @DisplayName("Constructor rejects invalid parameters") |
| 28 | + void constructorValidation() { |
| 29 | + assertAll("invalid-constructor-params", |
| 30 | + () |
| 31 | + -> assertThrows(IllegalArgumentException.class, () -> new DampedOscillator(0.0, 0.1), "omega0 == 0 should throw"), |
| 32 | + () -> assertThrows(IllegalArgumentException.class, () -> new DampedOscillator(-1.0, 0.1), "negative omega0 should throw"), () -> assertThrows(IllegalArgumentException.class, () -> new DampedOscillator(1.0, -0.1), "negative gamma should throw")); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + @DisplayName("Analytical displacement matches expected formula for underdamped case") |
| 37 | + void analyticalUnderdamped() { |
| 38 | + double omega0 = 10.0; |
| 39 | + double gamma = 0.5; |
| 40 | + DampedOscillator d = new DampedOscillator(omega0, gamma); |
| 41 | + |
| 42 | + double a = 1.0; |
| 43 | + double phi = 0.2; |
| 44 | + double t = 0.123; |
| 45 | + |
| 46 | + // expected: a * exp(-gamma * t) * cos(omega_d * t + phi) |
| 47 | + double omegaD = Math.sqrt(Math.max(0.0, omega0 * omega0 - gamma * gamma)); |
| 48 | + double expected = a * Math.exp(-gamma * t) * Math.cos(omegaD * t + phi); |
| 49 | + |
| 50 | + double actual = d.displacementAnalytical(a, phi, t); |
| 51 | + assertEquals(expected, actual, 1e-12, "Analytical underdamped displacement should match closed-form value"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @DisplayName("Analytical displacement gracefully handles overdamped parameters (omegaD -> 0)") |
| 56 | + void analyticalOverdamped() { |
| 57 | + double omega0 = 1.0; |
| 58 | + double gamma = 2.0; // gamma > omega0 => omega_d = 0 in our implementation (Math.max) |
| 59 | + DampedOscillator d = new DampedOscillator(omega0, gamma); |
| 60 | + |
| 61 | + double a = 2.0; |
| 62 | + double phi = Math.PI / 4.0; |
| 63 | + double t = 0.5; |
| 64 | + |
| 65 | + // With omegaD forced to 0 by implementation, expected simplifies to: |
| 66 | + double expected = a * Math.exp(-gamma * t) * Math.cos(phi); |
| 67 | + double actual = d.displacementAnalytical(a, phi, t); |
| 68 | + |
| 69 | + assertEquals(expected, actual, 1e-12, "Overdamped handling should reduce to exponential * cos(phase)"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + @DisplayName("Explicit Euler step approximates analytical solution for small dt over short time") |
| 74 | + void eulerApproximatesAnalyticalSmallDt() { |
| 75 | + double omega0 = 10.0; |
| 76 | + double gamma = 0.5; |
| 77 | + DampedOscillator d = new DampedOscillator(omega0, gamma); |
| 78 | + |
| 79 | + double a = 1.0; |
| 80 | + double phi = 0.0; |
| 81 | + |
| 82 | + // initial conditions consistent with amplitude a and zero phase: |
| 83 | + // x(0) = a, v(0) = -a * gamma * cos(phi) + a * omegaD * sin(phi) |
| 84 | + double omegaD = Math.sqrt(Math.max(0.0, omega0 * omega0 - gamma * gamma)); |
| 85 | + double x0 = a * Math.cos(phi); |
| 86 | + double v0 = -a * gamma * Math.cos(phi) - a * omegaD * Math.sin(phi); // small general form |
| 87 | + |
| 88 | + double dt = 1e-4; |
| 89 | + int steps = 1000; // simulate to t = 0.1s |
| 90 | + double tFinal = steps * dt; |
| 91 | + |
| 92 | + double[] state = new double[] {x0, v0}; |
| 93 | + for (int i = 0; i < steps; i++) { |
| 94 | + state = d.stepEuler(state, dt); |
| 95 | + } |
| 96 | + |
| 97 | + double analyticAtT = d.displacementAnalytical(a, phi, tFinal); |
| 98 | + double numericAtT = state[0]; |
| 99 | + |
| 100 | + // Euler is low-order — allow a small tolerance but assert it remains close for small dt + short time. |
| 101 | + assertEquals(analyticAtT, numericAtT, TOLERANCE, String.format("Numeric Euler should approximate analytical solution at t=%.6f (tolerance=%g)", tFinal, TOLERANCE)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + @DisplayName("stepEuler validates inputs and throws on null/invalid dt/state") |
| 106 | + void eulerInputValidation() { |
| 107 | + DampedOscillator d = new DampedOscillator(5.0, 0.1); |
| 108 | + |
| 109 | + assertAll("invalid-stepEuler-args", |
| 110 | + () |
| 111 | + -> assertThrows(IllegalArgumentException.class, () -> d.stepEuler(null, 0.01), "null state should throw"), |
| 112 | + () |
| 113 | + -> assertThrows(IllegalArgumentException.class, () -> d.stepEuler(new double[] {1.0}, 0.01), "state array with invalid length should throw"), |
| 114 | + () -> assertThrows(IllegalArgumentException.class, () -> d.stepEuler(new double[] {1.0, 0.0}, 0.0), "non-positive dt should throw"), () -> assertThrows(IllegalArgumentException.class, () -> d.stepEuler(new double[] {1.0, 0.0}, -1e-3), "negative dt should throw")); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + @DisplayName("Getter methods return configured parameters") |
| 119 | + void gettersReturnConfiguration() { |
| 120 | + double omega0 = Math.PI; |
| 121 | + double gamma = 0.01; |
| 122 | + DampedOscillator d = new DampedOscillator(omega0, gamma); |
| 123 | + |
| 124 | + assertAll("getters", () -> assertEquals(omega0, d.getOmega0(), 0.0, "getOmega0 should return configured omega0"), () -> assertEquals(gamma, d.getGamma(), 0.0, "getGamma should return configured gamma")); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + @DisplayName("Analytical displacement at t=0 returns initial amplitude * cos(phase)") |
| 129 | + void analyticalAtZeroTime() { |
| 130 | + double omega0 = 5.0; |
| 131 | + double gamma = 0.2; |
| 132 | + DampedOscillator d = new DampedOscillator(omega0, gamma); |
| 133 | + |
| 134 | + double a = 2.0; |
| 135 | + double phi = Math.PI / 3.0; |
| 136 | + double t = 0.0; |
| 137 | + |
| 138 | + double expected = a * Math.cos(phi); |
| 139 | + double actual = d.displacementAnalytical(a, phi, t); |
| 140 | + |
| 141 | + assertEquals(expected, actual, 1e-12, "Displacement at t=0 should be a * cos(phase)"); |
| 142 | + } |
| 143 | +} |
0 commit comments