forked from blksail-edu/python-refresher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_physics.py
286 lines (267 loc) · 11.6 KB
/
test_physics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""
Unit Test cases for physics.py
Eben Quenneville
7/13/2023
"""
import unittest
import physics
import numpy as np
class TestPhysics(unittest.TestCase):
def test_calculate_buoyancy(self):
self.assertNotEqual(physics.calculate_buoyancy(1, 1000), 0)
self.assertEqual(physics.calculate_buoyancy(0.1, 1000), 981)
self.assertRaises(TypeError, physics.calculate_buoyancy, "foo", "bar")
self.assertRaises(ValueError, physics.calculate_buoyancy, -100, 1000)
self.assertRaises(ValueError, physics.calculate_buoyancy, 100, -1000)
self.assertRaises(ValueError, physics.calculate_buoyancy, 100, 0)
self.assertRaises(ValueError, physics.calculate_buoyancy, 0, 100)
def test_will_it_float(self):
self.assertEqual(physics.will_it_float(0.1, 50), True)
self.assertEqual(physics.will_it_float(0.1, 1000), False)
self.assertEqual(physics.will_it_float(0.1, 100), None)
self.assertNotEqual(physics.will_it_float(0.1, 1000), True)
self.assertRaises(TypeError, physics.will_it_float, "0.1", "100")
self.assertRaises(ValueError, physics.will_it_float, -0.1, 100)
self.assertRaises(ValueError, physics.will_it_float, 0.1, -100)
self.assertRaises(ValueError, physics.will_it_float, 0.0, 100)
self.assertRaises(ValueError, physics.will_it_float, 0.1, 0.0)
def test_calculate_pressure(self):
self.assertEqual(physics.calculate_pressure(10), 199425)
self.assertNotEqual(physics.calculate_pressure(11), 199425)
self.assertRaises(TypeError, physics.calculate_pressure, "foo")
self.assertRaises(Exception, physics.calculate_pressure, -100)
def test_calculate_acceleration(self):
self.assertEqual(physics.calculate_acceleration(100, 10), 10)
self.assertNotEqual(physics.calculate_acceleration(1000, 10), 10)
self.assertRaises(ValueError, physics.calculate_acceleration, 100, 0)
self.assertRaises(ValueError, physics.calculate_acceleration, 100, -10)
def test_calculate_angular_acceleration(self):
self.assertEqual(physics.calculate_angular_acceleration(10, 1), 10)
self.assertnotEqual(physics.calculate_angular_acceleration(10, 10), 10)
self.assertRaises(ValueError, physics.calculate_angular_acceleration, 100, 0)
self.assertRaises(ValueError, physics.calculate_angular_acceleration, 100, -10)
def test_calculate_torque(self):
self.assertEqual(physics.calculate_torque(10, 0, 1), 0)
self.assertNotEqual(physics.calculate_torque(10, 10, 1), 0)
self.assertEqual(physics.calculate_torque(10, 90, 1), 10)
def test_calculate_moment_of_inertia(self):
self.assertEqual(physics.calculate_moment_of_inertia(10, 1), 10)
self.assertNotEqual(physics.calculate_moment_of_inertia(10, 10), 10)
self.assertRaises(ValueError, physics.calculate_moment_of_inertia, -100, 10)
self.assertRaises(ValueError, physics.calculate_moment_of_inertia, 0, 10)
def test_calculate_auv_acceleration(self):
self.assertTrue(
np.allclose(physics.calculate_auv_acceleration(10, 0), np.array([0.1, 0]))
)
self.assertTrue(
np.allclose(
physics.calculate_auv_acceleration(10, np.pi / 2), np.array([0, 0.1])
)
)
self.assertFalse(
np.allclose(
physics.calculate_auv_acceleration(10, np.pi / 3), np.array([0, 0.1])
)
)
def test_calculate_angular_acceleration(self):
# Test for expected output
self.assertEqual(physics.calculate_auv_angular_acceleration(100, np.pi / 2), 50)
# Confirm we don't always get 50
self.assertNotEqual(
physics.calculate_auv_angular_acceleration(1000, np.pi / 2), 50
)
# Negative moment of inertia should raise an error
with self.assertRaises(ValueError):
physics.calculate_auv_angular_acceleration(1000, np.pi / 2, -10, 1)
def test_calculate_auv2_acceleration(self):
# 0 forces should result in no acceleration
self.assertTrue(
np.allclose(
physics.calculate_auv2_acceleration(
np.array([0, 0, 0, 0]), np.pi / 4, 0.0
),
np.array([0, 0]),
)
)
# Equivalent forces should cancel out
self.assertTrue(
np.allclose(
physics.calculate_auv2_acceleration(
np.array([10, 10, 10, 10]), np.pi / 4, 0.0
),
np.array([0, 0]),
)
)
# If F1 and F2 are positive and equal, then the object should only accelerate in X
self.assertTrue(
np.allclose(
physics.calculate_auv2_acceleration(
np.array([10, 10, 0, 0]), np.pi / 4, 0.0
),
np.array([0.141422, 0]),
)
)
# if alpha is pi/2 rads, then all acceleration should be vertical
self.assertTrue(
np.allclose(
physics.calculate_auv2_acceleration(
np.array([10, 0, 0, 10]), np.pi / 2, 0
),
np.array([0, 0.2]),
)
)
# if all relative acceleration is vertical but theta is pi/2, then all acceleration should become -X
self.assertTrue(
np.allclose(
physics.calculate_auv2_acceleration(
np.array([10, 0, 0, 10]), np.pi / 2, np.pi / 2
),
np.array([-0.2, 0]),
)
)
# if theta is not pi/2, then all relative Y-acceleration should not become -X
self.assertFalse(
np.allclose(
physics.calculate_auv2_acceleration(
np.array([10, 0, 0, 10]), np.pi / 3, np.pi / 2
),
np.array([-0.2, 0]),
)
)
# Check that negative mass or incorrect np.array throws an error
with self.assertRaises(ValueError):
physics.calculate_auv2_acceleration(np.array([0, 0, 0, 0]), 45, 0, -100)
physics.calculate_auv2_acceleration(np.array([[], []]), 45, 0)
physics.calculate_auv2_acceleration([], 45, 0)
def test_calculate_auv2_angular_acceleration(self):
# No force should have no torque
self.assertEqual(
physics.calculate_auv2_angular_acceleration(
np.array([0, 0, 0, 0]), 0, 1, 1
),
0,
)
# If all of the force is perpendicular, than it should all be torque
self.assertAlmostEqual(
physics.calculate_auv2_angular_acceleration(
np.array([10, 0, 0, 0]), np.pi / 2, 1, 1
),
0.1,
)
# Equal forces should cancel out
self.assertEqual(
physics.calculate_auv2_angular_acceleration(
np.array([10, 10, 10, 10]), np.pi / 4, 1, 1
),
0,
)
# Unequal forces should create some torque
self.assertNotEqual(
physics.calculate_auv2_angular_acceleration(
np.array([15, 10, 14, 10]), np.pi / 4, 1, 1
),
0,
)
# Sample case: only T1 is active, exerting a force of 10 Newtons at an angle alpha of pi / 4 with a moment arm of sqrt(2)
self.assertAlmostEqual(
physics.calculate_auv2_angular_acceleration(
np.array([10, 0, 0, 0]), np.pi / 4, 1, 1
),
0.14142135623,
)
# Check that negative distances, and moment of inertia throw errors
with self.assertRaises(ValueError):
physics.calculate_auv2_angular_acceleration(
np.array([0, 0, 0, 0]), 45, -10, 10
)
physics.calculate_auv2_angular_acceleration(
np.array([0, 0, 0, 0]), 45, 10, -10
)
physics.calculate_auv2_angular_acceleration(
np.array([0, 0, 0, 0]), 45, 10, 10, -100
)
physics.calculate_auv2_angular_acceleration(np.array([[], []]), 45, 10, 10)
physics.calculate_auv2_angular_acceleration([], 45, 10, 10)
def test_simulate_auv2_motion(self):
# 0 magnitude forces
(times, x, y, theta, v, omega, a) = physics.simulate_auv2_motion(
np.array([0, 0, 0, 0]), np.pi / 4, 1, 1, 100, 100, 0.1, 0.3, 0, 0, 0
)
np.testing.assert_array_equal(times, np.array([0, 0.1, 0.2]))
np.testing.assert_array_equal(x, np.array([0, 0, 0]))
np.testing.assert_array_equal(y, np.array([0, 0, 0]))
np.testing.assert_array_equal(theta, np.array([0, 0, 0]))
np.testing.assert_array_equal(v, np.array([[0, 0], [0, 0], [0, 0]]))
np.testing.assert_array_equal(omega, np.array([0, 0, 0]))
np.testing.assert_array_equal(a, np.array([[0, 0], [0, 0], [0, 0]]))
# Test with counteracting forces, should only be torque
(times, x, y, theta, v, omega, a) = physics.simulate_auv2_motion(
np.array([10, 0, 10, 0]),
np.pi / 4,
1,
1,
100,
100,
0.1,
0.3,
0,
0,
np.pi / 4,
)
np.testing.assert_array_equal(times, np.array([0, 0.1, 0.2]))
np.testing.assert_array_almost_equal(x, np.array([0, 0, 0]))
np.testing.assert_array_equal(y, np.array([0, 0, 0]))
np.testing.assert_array_almost_equal(
theta, np.array([0.785398, 0.788227, 0.793883])
)
np.testing.assert_array_almost_equal(v, np.array([[0, 0], [0, 0], [0, 0]]))
np.testing.assert_array_almost_equal(omega, np.array([0.0, 0.028284, 0.056569]))
np.testing.assert_array_almost_equal(a, np.array([[0, 0], [0, 0], [0, 0]]))
# Test with all equal forces
(times, x, y, theta, v, omega, a) = physics.simulate_auv2_motion(
np.array([10, 10, 10, 10]),
np.pi / 4,
1,
1,
100,
100,
0.1,
0.3,
0,
0,
0,
)
np.testing.assert_array_equal(times, np.array([0, 0.1, 0.2]))
np.testing.assert_array_almost_equal(x, np.array([0, 0, 0]))
np.testing.assert_array_almost_equal(y, np.array([0, 0, 0]))
np.testing.assert_array_almost_equal(theta, np.array([0, 0, 0]))
np.testing.assert_array_almost_equal(v, np.array([[0, 0], [0, 0], [0, 0]]))
np.testing.assert_array_almost_equal(omega, np.array([0, 0, 0]))
np.testing.assert_array_almost_equal(a, np.array([[0, 0], [0, 0], [0, 0]]))
# Test with just 1 force
(times, x, y, theta, v, omega, a) = physics.simulate_auv2_motion(
np.array([10, 0, 0, 0]),
np.pi / 4,
1,
1,
100,
100,
0.1,
0.3,
0,
0,
0,
)
np.testing.assert_array_almost_equal(times, np.array([0, 0.1, 0.2]))
np.testing.assert_array_almost_equal(x, np.array([0.0, 0.000707, 0.002121]))
np.testing.assert_array_almost_equal(y, np.array([0.0, 0.000707, 0.002121]))
np.testing.assert_array_almost_equal(theta, np.array([0.0, 0.001414, 0.004243]))
np.testing.assert_array_almost_equal(
v, np.array([[0.0, 0.0], [0.007071, 0.007071], [0.014132, 0.014152]])
)
np.testing.assert_array_almost_equal(omega, np.array([0.0, 0.014142, 0.028284]))
np.testing.assert_array_almost_equal(
a, np.array([[0.0, 0.0], [0.070711, 0.070711], [0.070611, 0.070811]])
)
if __name__ == "__main__":
unittest.main()