Skip to content

Commit 0493bcd

Browse files
Fixed build issue.
1 parent b7772cb commit 0493bcd

File tree

2 files changed

+41
-44
lines changed

2 files changed

+41
-44
lines changed

src/main/java/com/thealgorithms/physics/SimplePendulumRK4.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
/**
44
* Simulates a simple pendulum using the Runge-Kutta 4th order method.
55
* The pendulum is modeled with the nonlinear differential equation.
6+
*
7+
* @author [Yash Rajput](https://github.com/the-yash-rajput)
68
*/
79
public final class SimplePendulumRK4 {
810

@@ -71,8 +73,8 @@ public double[] stepRK4(double[] state, double dt) {
7173

7274
double[] k4 = derivatives(s4);
7375

74-
double thetaNext = state[0] + (dt / 6.0) * (k1[0] + 2 * k2[0] + 2 * k3[0] + k4[0]);
75-
double omegaNext = state[1] + (dt / 6.0) * (k1[1] + 2 * k2[1] + 2 * k3[1] + k4[1]);
76+
double thetaNext = state[0] + dt / 6.0 * (k1[0] + 2 * k2[0] + 2 * k3[0] + k4[0]);
77+
double omegaNext = state[1] + dt / 6.0 * (k1[1] + 2 * k2[1] + 2 * k3[1] + k4[1]);
7678

7779
return new double[] {thetaNext, omegaNext};
7880
}

src/test/java/com/thealgorithms/physics/SimplePendulumRK4Test.java

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package com.thealgorithms.physics;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4-
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
import static org.junit.jupiter.api.Assertions.assertNotNull;
6-
import static org.junit.jupiter.api.Assertions.assertThrows;
7-
import static org.junit.jupiter.api.Assertions.assertTrue;
8-
3+
import org.junit.jupiter.api.Assertions;
94
import org.junit.jupiter.api.DisplayName;
105
import org.junit.jupiter.api.Test;
116

@@ -21,35 +16,35 @@ class SimplePendulumRK4Test {
2116
@DisplayName("Test constructor creates valid pendulum")
2217
void testConstructor() {
2318
SimplePendulumRK4 pendulum = new SimplePendulumRK4(1.5, 9.81);
24-
assertNotNull(pendulum);
25-
assertEquals(1.5, pendulum.getLength(), EPSILON);
26-
assertEquals(9.81, pendulum.getGravity(), EPSILON);
19+
Assertions.assertNotNull(pendulum);
20+
Assertions.assertEquals(1.5, pendulum.getLength(), EPSILON);
21+
Assertions.assertEquals(9.81, pendulum.getGravity(), EPSILON);
2722
}
2823

2924
@Test
3025
@DisplayName("Test constructor rejects negative length")
3126
void testConstructorNegativeLength() {
32-
assertThrows(IllegalArgumentException.class, () -> { new SimplePendulumRK4(-1.0, 9.81); });
27+
Assertions.assertThrows(IllegalArgumentException.class, () -> { new SimplePendulumRK4(-1.0, 9.81); });
3328
}
3429

3530
@Test
3631
@DisplayName("Test constructor rejects negative gravity")
3732
void testConstructorNegativeGravity() {
38-
assertThrows(IllegalArgumentException.class, () -> { new SimplePendulumRK4(1.0, -9.81); });
33+
Assertions.assertThrows(IllegalArgumentException.class, () -> { new SimplePendulumRK4(1.0, -9.81); });
3934
}
4035

4136
@Test
4237
@DisplayName("Test constructor rejects zero length")
4338
void testConstructorZeroLength() {
44-
assertThrows(IllegalArgumentException.class, () -> { new SimplePendulumRK4(0.0, 9.81); });
39+
Assertions.assertThrows(IllegalArgumentException.class, () -> { new SimplePendulumRK4(0.0, 9.81); });
4540
}
4641

4742
@Test
4843
@DisplayName("Test getters return correct values")
4944
void testGetters() {
5045
SimplePendulumRK4 pendulum = new SimplePendulumRK4(2.5, 10.0);
51-
assertEquals(2.5, pendulum.getLength(), EPSILON);
52-
assertEquals(10.0, pendulum.getGravity(), EPSILON);
46+
Assertions.assertEquals(2.5, pendulum.getLength(), EPSILON);
47+
Assertions.assertEquals(10.0, pendulum.getGravity(), EPSILON);
5348
}
5449

5550
@Test
@@ -59,8 +54,8 @@ void testSingleStep() {
5954
double[] state = {0.1, 0.0};
6055
double[] newState = pendulum.stepRK4(state, 0.01);
6156

62-
assertNotNull(newState);
63-
assertEquals(2, newState.length);
57+
Assertions.assertNotNull(newState);
58+
Assertions.assertEquals(2, newState.length);
6459
}
6560

6661
@Test
@@ -73,8 +68,8 @@ void testEquilibrium() {
7368
state = pendulum.stepRK4(state, 0.01);
7469
}
7570

76-
assertEquals(0.0, state[0], EPSILON, "Theta should remain at equilibrium");
77-
assertEquals(0.0, state[1], EPSILON, "Omega should remain zero");
71+
Assertions.assertEquals(0.0, state[0], EPSILON, "Theta should remain at equilibrium");
72+
Assertions.assertEquals(0.0, state[1], EPSILON, "Omega should remain zero");
7873
}
7974

8075
@Test
@@ -94,7 +89,7 @@ void testSmallAngleOscillation() {
9489

9590
// After one period, should return close to initial position
9691
double error = Math.abs(finalTheta - initialAngle) / Math.abs(initialAngle);
97-
assertTrue(error < 0.05, "Small angle approximation error should be < 5%");
92+
Assertions.assertTrue(error < 0.05, "Small angle approximation error should be < 5%");
9893
}
9994

10095
@Test
@@ -113,12 +108,12 @@ void testLargeAngleOscillation() {
113108
minTheta = Math.min(minTheta, s[0]);
114109
}
115110

116-
assertTrue(maxTheta > 0, "Should have positive excursions");
117-
assertTrue(minTheta < 0, "Should have negative excursions");
111+
Assertions.assertTrue(maxTheta > 0, "Should have positive excursions");
112+
Assertions.assertTrue(minTheta < 0, "Should have negative excursions");
118113

119114
// Check symmetry
120115
double asymmetry = Math.abs((maxTheta + minTheta) / maxTheta);
121-
assertTrue(asymmetry < 0.1, "Oscillation should be symmetric");
116+
Assertions.assertTrue(asymmetry < 0.1, "Oscillation should be symmetric");
122117
}
123118

124119
@Test
@@ -136,7 +131,7 @@ void testEnergyConservationSmallAngle() {
136131
double finalEnergy = pendulum.calculateEnergy(state);
137132
double drift = Math.abs(finalEnergy - initialEnergy) / initialEnergy;
138133

139-
assertTrue(drift < ENERGY_DRIFT_TOLERANCE, "Energy drift should be < 0.1%, got: " + (drift * 100) + "%");
134+
Assertions.assertTrue(drift < ENERGY_DRIFT_TOLERANCE, "Energy drift should be < 0.1%, got: " + (drift * 100) + "%");
140135
}
141136

142137
@Test
@@ -154,7 +149,7 @@ void testEnergyConservationLargeAngle() {
154149
double finalEnergy = pendulum.calculateEnergy(state);
155150
double drift = Math.abs(finalEnergy - initialEnergy) / initialEnergy;
156151

157-
assertTrue(drift < ENERGY_DRIFT_TOLERANCE, "Energy drift should be < 0.1%, got: " + (drift * 100) + "%");
152+
Assertions.assertTrue(drift < ENERGY_DRIFT_TOLERANCE, "Energy drift should be < 0.1%, got: " + (drift * 100) + "%");
158153
}
159154

160155
@Test
@@ -166,8 +161,8 @@ void testSimulate() {
166161

167162
double[][] trajectory = pendulum.simulate(initialState, 0.01, steps);
168163

169-
assertEquals(steps + 1, trajectory.length, "Trajectory should have steps + 1 entries");
170-
assertArrayEquals(initialState, trajectory[0], EPSILON, "First entry should match initial state");
164+
Assertions.assertEquals(steps + 1, trajectory.length, "Trajectory should have steps + 1 entries");
165+
Assertions.assertArrayEquals(initialState, trajectory[0], EPSILON, "First entry should match initial state");
171166

172167
// Verify state changes over time
173168
boolean changed = false;
@@ -177,7 +172,7 @@ void testSimulate() {
177172
break;
178173
}
179174
}
180-
assertTrue(changed, "Simulation should progress from initial state");
175+
Assertions.assertTrue(changed, "Simulation should progress from initial state");
181176
}
182177

183178
@Test
@@ -186,7 +181,7 @@ void testEnergyAtEquilibrium() {
186181
SimplePendulumRK4 pendulum = new SimplePendulumRK4(1.0, 9.81);
187182
double[] state = {0.0, 0.0};
188183
double energy = pendulum.calculateEnergy(state);
189-
assertEquals(0.0, energy, EPSILON, "Energy at equilibrium should be zero");
184+
Assertions.assertEquals(0.0, energy, EPSILON, "Energy at equilibrium should be zero");
190185
}
191186

192187
@Test
@@ -195,7 +190,7 @@ void testEnergyAtMaxAngle() {
195190
SimplePendulumRK4 pendulum = new SimplePendulumRK4(1.0, 9.81);
196191
double[] state = {Math.PI / 2, 0.0};
197192
double energy = pendulum.calculateEnergy(state);
198-
assertTrue(energy > 0, "Energy should be positive at max angle");
193+
Assertions.assertTrue(energy > 0, "Energy should be positive at max angle");
199194
}
200195

201196
@Test
@@ -204,28 +199,28 @@ void testEnergyWithVelocity() {
204199
SimplePendulumRK4 pendulum = new SimplePendulumRK4(1.0, 9.81);
205200
double[] state = {0.0, 1.0};
206201
double energy = pendulum.calculateEnergy(state);
207-
assertTrue(energy > 0, "Energy should be positive with velocity");
202+
Assertions.assertTrue(energy > 0, "Energy should be positive with velocity");
208203
}
209204

210205
@Test
211206
@DisplayName("Test stepRK4 rejects null state")
212207
void testStepRejectsNullState() {
213208
SimplePendulumRK4 pendulum = new SimplePendulumRK4(1.0, 9.81);
214-
assertThrows(IllegalArgumentException.class, () -> { pendulum.stepRK4(null, 0.01); });
209+
Assertions.assertThrows(IllegalArgumentException.class, () -> { pendulum.stepRK4(null, 0.01); });
215210
}
216211

217212
@Test
218213
@DisplayName("Test stepRK4 rejects invalid state length")
219214
void testStepRejectsInvalidStateLength() {
220215
SimplePendulumRK4 pendulum = new SimplePendulumRK4(1.0, 9.81);
221-
assertThrows(IllegalArgumentException.class, () -> { pendulum.stepRK4(new double[] {0.1}, 0.01); });
216+
Assertions.assertThrows(IllegalArgumentException.class, () -> { pendulum.stepRK4(new double[] {0.1}, 0.01); });
222217
}
223218

224219
@Test
225220
@DisplayName("Test stepRK4 rejects negative time step")
226221
void testStepRejectsNegativeTimeStep() {
227222
SimplePendulumRK4 pendulum = new SimplePendulumRK4(1.0, 9.81);
228-
assertThrows(IllegalArgumentException.class, () -> { pendulum.stepRK4(new double[] {0.1, 0.2}, -0.01); });
223+
Assertions.assertThrows(IllegalArgumentException.class, () -> { pendulum.stepRK4(new double[] {0.1, 0.2}, -0.01); });
229224
}
230225

231226
@Test
@@ -235,9 +230,9 @@ void testExtremeLargeAngle() {
235230
double[] state = {Math.toRadians(179.0), 0.0};
236231
double[] result = pendulum.stepRK4(state, 0.01);
237232

238-
assertNotNull(result);
239-
assertTrue(Double.isFinite(result[0]), "Should handle large angles without NaN");
240-
assertTrue(Double.isFinite(result[1]), "Should handle large angles without NaN");
233+
Assertions.assertNotNull(result);
234+
Assertions.assertTrue(Double.isFinite(result[0]), "Should handle large angles without NaN");
235+
Assertions.assertTrue(Double.isFinite(result[1]), "Should handle large angles without NaN");
241236
}
242237

243238
@Test
@@ -247,9 +242,9 @@ void testExtremeHighVelocity() {
247242
double[] state = {0.0, 10.0};
248243
double[] result = pendulum.stepRK4(state, 0.01);
249244

250-
assertNotNull(result);
251-
assertTrue(Double.isFinite(result[0]), "Should handle high velocity without NaN");
252-
assertTrue(Double.isFinite(result[1]), "Should handle high velocity without NaN");
245+
Assertions.assertNotNull(result);
246+
Assertions.assertTrue(Double.isFinite(result[0]), "Should handle high velocity without NaN");
247+
Assertions.assertTrue(Double.isFinite(result[1]), "Should handle high velocity without NaN");
253248
}
254249

255250
@Test
@@ -259,8 +254,8 @@ void testExtremeSmallTimeStep() {
259254
double[] state = {Math.toRadians(10.0), 0.0};
260255
double[] result = pendulum.stepRK4(state, 1e-6);
261256

262-
assertNotNull(result);
263-
assertTrue(Double.isFinite(result[0]), "Should handle small time steps without NaN");
264-
assertTrue(Double.isFinite(result[1]), "Should handle small time steps without NaN");
257+
Assertions.assertNotNull(result);
258+
Assertions.assertTrue(Double.isFinite(result[0]), "Should handle small time steps without NaN");
259+
Assertions.assertTrue(Double.isFinite(result[1]), "Should handle small time steps without NaN");
265260
}
266261
}

0 commit comments

Comments
 (0)