-
Notifications
You must be signed in to change notification settings - Fork 107
/
test_rigid_body_memory_block_validity.py
327 lines (277 loc) · 10.3 KB
/
test_rigid_body_memory_block_validity.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import numpy as np
from numpy.testing import assert_allclose
from elastica.memory_block.memory_block_rigid_body import MemoryBlockRigidBody
import pytest
from elastica.utils import Tolerance
class MockRigidBody:
def __init__(self):
self.radius = np.random.randn()
self.length = np.random.randn()
self.density = np.random.randn()
self.volume = np.random.randn()
self.mass = np.random.randn()
self.position_collection = np.random.randn(3, 1)
self.velocity_collection = np.random.randn(3, 1)
self.acceleration_collection = np.random.randn(3, 1)
self.omega_collection = np.random.randn(3, 1)
self.alpha_collection = np.random.randn(3, 1)
self.director_collection = np.random.randn(3, 3, 1)
self.external_forces = np.random.randn(3, 1)
self.external_torques = np.random.randn(3, 1)
self.mass_second_moment_of_inertia = np.random.randn(3, 3, 1)
self.inv_mass_second_moment_of_inertia = np.random.randn(3, 3, 1)
@pytest.mark.parametrize("n_rods", [1, 2, 5, 6])
def test_block_structure_scalar_validity(n_rods):
"""
This function is testing validity of scalars. It is been tested that
for scalar variables, if the block structure memory and values are
set correctly and rods that belong to rod structure share the memory
with block structure.
Parameters
----------
n_rods
Returns
-------
"""
world_bodies = [MockRigidBody() for _ in range(n_rods)]
block_structure = MemoryBlockRigidBody(
world_bodies, [i for i in range(len(world_bodies))]
)
for i in range(n_rods):
# radius
assert np.shares_memory(block_structure.radius, world_bodies[i].radius)
assert np.shares_memory(
block_structure.scalar_dofs_in_rigid_bodies, world_bodies[i].radius
)
assert_allclose(
block_structure.radius[i : i + 1],
world_bodies[i].radius,
atol=Tolerance.atol(),
)
# length
assert np.shares_memory(block_structure.length, world_bodies[i].length)
assert np.shares_memory(
block_structure.scalar_dofs_in_rigid_bodies, world_bodies[i].length
)
assert_allclose(
block_structure.length[i : i + 1],
world_bodies[i].length,
atol=Tolerance.atol(),
)
# density
assert np.shares_memory(block_structure.density, world_bodies[i].density)
assert np.shares_memory(
block_structure.scalar_dofs_in_rigid_bodies, world_bodies[i].density
)
assert_allclose(
block_structure.density[i : i + 1],
world_bodies[i].density,
atol=Tolerance.atol(),
)
# volume
assert np.shares_memory(block_structure.volume, world_bodies[i].volume)
assert np.shares_memory(
block_structure.scalar_dofs_in_rigid_bodies, world_bodies[i].volume
)
assert_allclose(
block_structure.volume[i : i + 1],
world_bodies[i].volume,
atol=Tolerance.atol(),
)
# mass
assert np.shares_memory(block_structure.mass, world_bodies[i].mass)
assert np.shares_memory(
block_structure.scalar_dofs_in_rigid_bodies, world_bodies[i].mass
)
assert_allclose(
block_structure.mass[i : i + 1],
world_bodies[i].mass,
atol=Tolerance.atol(),
)
@pytest.mark.parametrize("n_rods", [1, 2, 5, 6])
def test_block_structure_vectors_validity(n_rods):
"""
This function is testing validity of vectors. It is been
tested that for vector variables, if the block structure memory
and values are set correctly and rods that belong to rod structure
share the memory with block structure.
Parameters
----------
n_rods
Returns
-------
"""
world_bodies = [MockRigidBody() for _ in range(n_rods)]
block_structure = MemoryBlockRigidBody(
world_bodies, [i for i in range(len(world_bodies))]
)
for i in range(n_rods):
# position collection
assert np.shares_memory(
block_structure.position_collection, world_bodies[i].position_collection
)
assert np.shares_memory(
block_structure.vector_dofs_in_rigid_bodies,
world_bodies[i].position_collection,
)
assert_allclose(
block_structure.position_collection[..., i : i + 1],
world_bodies[i].position_collection,
)
# external forces
assert np.shares_memory(
block_structure.external_forces, world_bodies[i].external_forces
)
assert np.shares_memory(
block_structure.vector_dofs_in_rigid_bodies, world_bodies[i].external_forces
)
assert_allclose(
block_structure.external_forces[..., i : i + 1],
world_bodies[i].external_forces,
)
# external torques
assert np.shares_memory(
block_structure.external_torques, world_bodies[i].external_torques
)
assert np.shares_memory(
block_structure.vector_dofs_in_rigid_bodies,
world_bodies[i].external_torques,
)
assert_allclose(
block_structure.external_torques[..., i : i + 1],
world_bodies[i].external_torques,
)
@pytest.mark.parametrize("n_rods", [1, 2, 5, 6])
def test_block_structure_matrix_validity(n_rods):
"""
This function is testing validity of matrices. It is been
tested that for matrix variables, if the block structure memory
and values are set correctly and rods that belong to rod structure
share the memory with block structure.
Parameters
----------
n_rods
Returns
-------
"""
world_bodies = [MockRigidBody() for _ in range(n_rods)]
block_structure = MemoryBlockRigidBody(
world_bodies, [i for i in range(len(world_bodies))]
)
for i in range(n_rods):
# director collection
assert np.shares_memory(
block_structure.director_collection, world_bodies[i].director_collection
)
assert np.shares_memory(
block_structure.matrix_dofs_in_rigid_bodies,
world_bodies[i].director_collection,
)
assert_allclose(
block_structure.director_collection[..., i : i + 1],
world_bodies[i].director_collection,
)
# mass second moment of inertia
assert np.shares_memory(
block_structure.mass_second_moment_of_inertia,
world_bodies[i].mass_second_moment_of_inertia,
)
assert np.shares_memory(
block_structure.matrix_dofs_in_rigid_bodies,
world_bodies[i].mass_second_moment_of_inertia,
)
assert_allclose(
block_structure.mass_second_moment_of_inertia[..., i : i + 1],
world_bodies[i].mass_second_moment_of_inertia,
)
# inv mass second moment of inertia
assert np.shares_memory(
block_structure.inv_mass_second_moment_of_inertia,
world_bodies[i].inv_mass_second_moment_of_inertia,
)
assert np.shares_memory(
block_structure.matrix_dofs_in_rigid_bodies,
world_bodies[i].inv_mass_second_moment_of_inertia,
)
assert_allclose(
block_structure.inv_mass_second_moment_of_inertia[..., i : i + 1],
world_bodies[i].inv_mass_second_moment_of_inertia,
)
@pytest.mark.parametrize("n_rods", [1, 2, 5, 6])
def test_block_structure_symplectic_stepper_variables_validity(n_rods):
"""
This function is testing validity of vectors. It is been
tested that for vector variables, if the block structure memory
and values are set correctly and rods that belong to rod structure
share the memory with block structure.
Parameters
----------
n_rods
Returns
-------
"""
world_bodies = [MockRigidBody() for _ in range(n_rods)]
block_structure = MemoryBlockRigidBody(
world_bodies, [i for i in range(len(world_bodies))]
)
for i in range(n_rods):
# velocity collection
assert np.shares_memory(
block_structure.velocity_collection, world_bodies[i].velocity_collection
)
assert np.shares_memory(
block_structure.rate_collection, world_bodies[i].velocity_collection
)
assert_allclose(
block_structure.velocity_collection[..., i : i + 1],
world_bodies[i].velocity_collection,
)
# omega collection
assert np.shares_memory(
block_structure.omega_collection, world_bodies[i].omega_collection
)
assert np.shares_memory(
block_structure.rate_collection, world_bodies[i].omega_collection
)
assert_allclose(
block_structure.omega_collection[..., i : i + 1],
world_bodies[i].omega_collection,
)
# acceleration collection
assert np.shares_memory(
block_structure.acceleration_collection,
world_bodies[i].acceleration_collection,
)
assert np.shares_memory(
block_structure.rate_collection, world_bodies[i].acceleration_collection
)
assert_allclose(
block_structure.acceleration_collection[..., i : i + 1],
world_bodies[i].acceleration_collection,
)
# alpha collection
assert np.shares_memory(
block_structure.alpha_collection, world_bodies[i].alpha_collection
)
assert np.shares_memory(
block_structure.rate_collection, world_bodies[i].alpha_collection
)
assert_allclose(
block_structure.alpha_collection[..., i : i + 1],
world_bodies[i].alpha_collection,
)
# Validity of the rate collection array
assert np.shares_memory(
block_structure.rate_collection, block_structure.v_w_collection
)
assert np.shares_memory(
block_structure.rate_collection, block_structure.dvdt_dwdt_collection
)
assert block_structure.v_w_collection.shape == (2, 3 * block_structure.n_nodes)
assert block_structure.dvdt_dwdt_collection.shape == (
2,
3 * block_structure.n_nodes,
)
if __name__ == "__main__":
from pytest import main
main([__file__])