-
Notifications
You must be signed in to change notification settings - Fork 107
/
test_joint.py
380 lines (319 loc) · 10.6 KB
/
test_joint.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
__doc__ = """ Joint between rods test module """
# System imports
from elastica.joint import (
FreeJoint,
HingeJoint,
FixedJoint,
)
from numpy.testing import assert_allclose
from elastica._rotations import _inv_rotate
from elastica.rod.cosserat_rod import CosseratRod
from elastica.utils import Tolerance
import numpy as np
import pytest
from scipy.spatial.transform import Rotation
# TODO: change tests and made them independent of rod, at least assigin hardcoded values for forces and torques
# seed random number generator
rng = np.random.default_rng(0)
def test_freejoint():
# Some rod properties. We need them for constructer, they are not used.
normal = np.array([0.0, 1.0, 0.0])
direction = np.array([0.0, 0.0, 1.0])
base_length = 1
base_radius = 0.2
density = 1
nu = 0.1
# Youngs Modulus [Pa]
youngs_modulus = 1e6
# poisson ratio
poisson_ratio = 0.5
shear_modulus = youngs_modulus / (poisson_ratio + 1.0)
# Origin of the rod
origin1 = np.array([0.0, 0.0, 0.0])
origin2 = np.array([1.0, 0.0, 0.0])
# Number of elements
n = 4
# create rod classes
rod1 = CosseratRod.straight_rod(
n,
origin1,
direction,
normal,
base_length,
base_radius,
density,
nu,
youngs_modulus,
shear_modulus=shear_modulus,
)
rod2 = CosseratRod.straight_rod(
n,
origin2,
direction,
normal,
base_length,
base_radius,
density,
nu,
youngs_modulus,
shear_modulus=shear_modulus,
)
# Stiffness between points
k = 1e8
# Damping between two points
nu = 1
# Rod indexes
rod1_index = -1
rod2_index = 0
# Rod velocity
v1 = np.array([-1, 0, 0]).reshape(3, 1)
v2 = v1 * -1
rod1.velocity_collection = np.tile(v1, (1, n + 1))
rod2.velocity_collection = np.tile(v2, (1, n + 1))
# Compute the free joint forces
distance = (
rod2.position_collection[..., rod2_index]
- rod1.position_collection[..., rod1_index]
)
elasticforce = k * distance
relative_vel = (
rod2.velocity_collection[..., rod2_index]
- rod1.velocity_collection[..., rod1_index]
)
dampingforce = nu * relative_vel
contactforce = elasticforce + dampingforce
frjt = FreeJoint(k, nu)
frjt.apply_forces(rod1, rod1_index, rod2, rod2_index)
frjt.apply_torques(rod1, rod1_index, rod2, rod2_index)
assert_allclose(
rod1.external_forces[..., rod1_index], contactforce, atol=Tolerance.atol()
)
assert_allclose(
rod2.external_forces[..., rod2_index], -1 * contactforce, atol=Tolerance.atol()
)
def test_hingejoint():
# Define the rod for testing
# Some rod properties. We need them for constructer, they are not used.
normal1 = np.array([0.0, 1.0, 0.0])
direction = np.array([1.0, 0.0, 0.0])
normal2 = np.array([0.0, 0.0, 1.0])
direction2 = np.array([1.0 / np.sqrt(2), 1.0 / np.sqrt(2), 0.0])
# direction2 = np.array([0.,1.0,0.])
base_length = 1
base_radius = 0.2
density = 1
nu = 0.1
# Youngs Modulus [Pa]
youngs_modulus = 1e6
# poisson ratio
poisson_ratio = 0.5
shear_modulus = youngs_modulus / (poisson_ratio + 1.0)
# Origin of the rod
origin1 = np.array([0.0, 0.0, 0.0])
origin2 = np.array([1.0, 0.0, 0.0])
# Number of elements
n = 2
# create rod classes
rod1 = CosseratRod.straight_rod(
n,
origin1,
direction,
normal1,
base_length,
base_radius,
density,
nu,
youngs_modulus,
shear_modulus=shear_modulus,
)
rod2 = CosseratRod.straight_rod(
n,
origin2,
direction2,
normal2,
base_length,
base_radius,
density,
nu,
youngs_modulus,
shear_modulus=shear_modulus,
)
# Rod velocity
v1 = np.array([-1, 0, 0]).reshape(3, 1)
v2 = v1 * -1
rod1.velocity_collection = np.tile(v1, (1, n + 1))
rod2.velocity_collection = np.tile(v2, (1, n + 1))
# Stiffness between points
k = 1e8
kt = 1e6
# Damping between two points
nu = 1
# Rod indexes
rod1_index = -1
rod2_index = 0
# Compute the free joint forces
distance = (
rod2.position_collection[..., rod2_index]
- rod1.position_collection[..., rod1_index]
)
elasticforce = k * distance
relative_vel = (
rod2.velocity_collection[..., rod2_index]
- rod1.velocity_collection[..., rod1_index]
)
dampingforce = nu * relative_vel
contactforce = elasticforce + dampingforce
hgjt = HingeJoint(k, nu, kt, normal1)
hgjt.apply_forces(rod1, rod1_index, rod2, rod2_index)
hgjt.apply_torques(rod1, rod1_index, rod2, rod2_index)
assert_allclose(
rod1.external_forces[..., rod1_index], contactforce, atol=Tolerance.atol()
)
assert_allclose(
rod2.external_forces[..., rod2_index], -1 * contactforce, atol=Tolerance.atol()
)
system_two_tangent = rod2.director_collection[2, :, rod2_index]
force_direction = np.dot(system_two_tangent, normal1) * normal1
torque = -kt * np.cross(system_two_tangent, force_direction)
torque_rod1 = -rod1.director_collection[..., rod1_index] @ torque
torque_rod2 = rod2.director_collection[..., rod2_index] @ torque
assert_allclose(
rod1.external_torques[..., rod1_index], torque_rod1, atol=Tolerance.atol()
)
assert_allclose(
rod2.external_torques[..., rod2_index], torque_rod2, atol=Tolerance.atol()
)
rest_euler_angles = [
np.array([0.0, 0.0, 0.0]),
np.array([np.pi / 2, 0.0, 0.0]),
np.array([0.0, np.pi / 2, 0.0]),
np.array([0.0, 0.0, np.pi / 2]),
2 * np.pi * rng.random(size=3),
]
@pytest.mark.parametrize("rest_euler_angle", rest_euler_angles)
def test_fixedjoint(rest_euler_angle):
# Define the rod for testing
# Some rod properties. We need them for constructor, they are not used.
normal1 = np.array([0.0, 1.0, 0.0])
direction = np.array([1.0, 0.0, 0.0])
normal2 = np.array([0.0, 0.0, 1.0])
direction2 = np.array([1.0 / np.sqrt(2), 1.0 / np.sqrt(2), 0.0])
# direction2 = np.array([0.,1.0,0.])
base_length = 1
base_radius = 0.2
density = 1
nu = 0.1
# Youngs Modulus [Pa]
youngs_modulus = 1e6
# poisson ratio
poisson_ratio = 0.5
shear_modulus = youngs_modulus / (poisson_ratio + 1.0)
# Origin of the rod
origin1 = np.array([0.0, 0.0, 0.0])
origin2 = np.array([1.0, 0.0, 0.0])
# Number of elements
n = 2
# create rod classes
rod1 = CosseratRod.straight_rod(
n,
origin1,
direction,
normal1,
base_length,
base_radius,
density,
nu,
youngs_modulus,
shear_modulus=shear_modulus,
)
rod2 = CosseratRod.straight_rod(
n,
origin2,
direction2,
normal2,
base_length,
base_radius,
density,
nu,
youngs_modulus,
shear_modulus=shear_modulus,
)
# Rod velocity
v1 = np.array([-1, 0, 0]).reshape(3, 1)
v2 = v1 * -1
rod1.velocity_collection = np.tile(v1, (1, n + 1))
rod2.velocity_collection = np.tile(v2, (1, n + 1))
# Rod angular velocity
omega1 = 1 / 180 * np.pi * np.array([0, 0, 1]).reshape(3, 1)
omega2 = -omega1
rod1.omega_collection = np.tile(omega1, (1, n + 1))
rod2.omega_collection = np.tile(omega2, (1, n + 1))
# Positional and rotational stiffness between systems
k = 1e8
kt = 1e6
# Positional and rotational damping between systems
nu = 1
nut = 1e2
# Rod indexes
rod1_index = -1
rod2_index = 0
# Compute the free joint forces
distance = (
rod2.position_collection[..., rod2_index]
- rod1.position_collection[..., rod1_index]
)
elasticforce = k * distance
relative_vel = (
rod2.velocity_collection[..., rod2_index]
- rod1.velocity_collection[..., rod1_index]
)
dampingforce = nu * relative_vel
contactforce = elasticforce + dampingforce
rest_rotation_matrix = Rotation.from_euler(
"xyz", rest_euler_angle, degrees=False
).as_matrix()
fxjt = FixedJoint(k, nu, kt, nut, rest_rotation_matrix=rest_rotation_matrix)
fxjt.apply_forces(rod1, rod1_index, rod2, rod2_index)
fxjt.apply_torques(rod1, rod1_index, rod2, rod2_index)
assert_allclose(
rod1.external_forces[..., rod1_index], contactforce, atol=Tolerance.atol()
)
assert_allclose(
rod2.external_forces[..., rod2_index], -1 * contactforce, atol=Tolerance.atol()
)
# collect directors of systems one and two
# note that systems can be either rods or rigid bodies
rod1_director = rod1.director_collection[..., rod1_index]
rod2_director = rod2.director_collection[..., rod2_index]
# rel_rot: C_12 = C_1I @ C_I2
# C_12 is relative rotation matrix from system 1 to system 2
# C_1I is the rotation from system 1 to the inertial frame (i.e. the world frame)
# C_I2 is the rotation from the inertial frame to system 2 frame (inverse of system_two_director)
rel_rot = rod1_director @ rod2_director.T
# error_rot: C_22* = C_21 @ C_12*
# C_22* is rotation matrix from current orientation of system 2 to desired orientation of system 2
# C_21 is the inverse of C_12, which describes the relative (current) rotation from system 1 to system 2
# C_12* is the desired rotation between systems one and two, which is saved in the static_rotation attribute
dev_rot = rel_rot.T @ rest_rotation_matrix
# compute rotation vectors based on C_22*
# scipy implementation
rot_vec = _inv_rotate(np.dstack([np.eye(3), dev_rot.T])).squeeze()
# rotate rotation vector into inertial frame
rot_vec_inertial_frame = rod2_director.T @ rot_vec
# deviation in rotation velocity between system 1 and system 2
# first convert to inertial frame, then take differences
dev_omega = (
rod2_director.T @ rod2.omega_collection[..., rod2_index]
- rod1_director.T @ rod1.omega_collection[..., rod1_index]
)
# we compute the constraining torque using a rotational spring - damper system in the inertial frame
torque = kt * rot_vec_inertial_frame - nut * dev_omega
# The opposite torques will be applied to system one and two after rotating the torques into the local frame
torque_rod1 = -rod1_director @ torque
torque_rod2 = rod2_director @ torque
assert_allclose(
rod1.external_torques[..., rod1_index], torque_rod1, atol=Tolerance.atol()
)
assert_allclose(
rod2.external_torques[..., rod2_index], torque_rod2, atol=Tolerance.atol()
)