Skip to content

Commit 8fa4c92

Browse files
committed
test: add rod-sphere contact tests for collision scenarios
1 parent 510d4f4 commit 8fa4c92

File tree

3 files changed

+88
-6
lines changed

3 files changed

+88
-6
lines changed

tests/test_contact_classes.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,92 @@ def test_contact_rod_sphere_with_collision_with_k_without_nu_and_friction(
547547
atol=1e-6,
548548
)
549549

550+
def test_contact_rod_sphere_without_collision(
551+
self,
552+
):
553+
"Testing Rod Sphere Contact wrapper without Collision"
554+
555+
mock_rod = MockRod()
556+
mock_sphere = MockSphere()
557+
rod_sphere_contact = RodSphereContact(k=1.0, nu=0.0)
558+
559+
# Setting sphere position such that there is no collision
560+
mock_sphere.position_collection = np.array([[100], [200], [300]])
561+
562+
mock_rod_external_forces_before_execution = mock_rod.external_forces.copy()
563+
mock_sphere_external_forces_before_execution = (
564+
mock_sphere.external_forces.copy()
565+
)
566+
mock_sphere_external_torques_before_execution = (
567+
mock_sphere.external_torques.copy()
568+
)
569+
570+
rod_sphere_contact.apply_contact(mock_rod, mock_sphere)
571+
572+
assert_allclose(
573+
mock_rod.external_forces, mock_rod_external_forces_before_execution
574+
)
575+
assert_allclose(
576+
mock_sphere.external_forces,
577+
mock_sphere_external_forces_before_execution,
578+
)
579+
assert_allclose(
580+
mock_sphere.external_torques,
581+
mock_sphere_external_torques_before_execution,
582+
)
583+
584+
def test_contact_rod_sphere_with_collision_with_nu_without_k(
585+
self,
586+
):
587+
"Testing Rod Sphere Contact wrapper with Collision with nu without k"
588+
589+
mock_rod = MockRod()
590+
mock_rod.velocity_collection = np.array([[-1, 0, 0], [-1, 0, 0], [-1, 0, 0]])
591+
592+
mock_sphere = MockSphere()
593+
mock_sphere.velocity_collection = np.array([[1], [0], [0]])
594+
595+
rod_sphere_contact = RodSphereContact(k=0.0, nu=1.0)
596+
rod_sphere_contact.apply_contact(mock_rod, mock_sphere)
597+
598+
assert_allclose(
599+
mock_sphere.external_forces, np.array([[-1.5], [0], [0]]), atol=1e-6
600+
)
601+
assert_allclose(
602+
mock_sphere.external_torques, np.array([[0], [0], [0]]), atol=1e-6
603+
)
604+
assert_allclose(
605+
mock_rod.external_forces,
606+
np.array([[0.5, 1.0, 0], [0, 0, 0], [0, 0, 0]]),
607+
atol=1e-6,
608+
)
609+
610+
def test_contact_rod_sphere_with_collision_with_k_and_nu(
611+
self,
612+
):
613+
"Testing Rod Sphere Contact wrapper with Collision with k and nu"
614+
615+
mock_rod = MockRod()
616+
mock_rod.velocity_collection = np.array([[-1, 0, 0], [-1, 0, 0], [-1, 0, 0]])
617+
618+
mock_sphere = MockSphere()
619+
mock_sphere.velocity_collection = np.array([[1], [0], [0]])
620+
621+
rod_sphere_contact = RodSphereContact(k=1.0, nu=1.0)
622+
rod_sphere_contact.apply_contact(mock_rod, mock_sphere)
623+
624+
assert_allclose(
625+
mock_sphere.external_forces, np.array([[-2.0], [0], [0]]), atol=1e-6
626+
)
627+
assert_allclose(
628+
mock_sphere.external_torques, np.array([[0], [0], [0]]), atol=1e-6
629+
)
630+
assert_allclose(
631+
mock_rod.external_forces,
632+
np.array([[0.666666, 1.333333, 0], [0, 0, 0], [0, 0, 0]]),
633+
atol=1e-6,
634+
)
635+
550636

551637
class TestRodPlaneContact:
552638
def initializer(

tests/test_contact_functions.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ def test_calculate_contact_forces_rod_sphere_with_k_without_nu_and_friction(
624624

625625
"initializing sphere parameters"
626626
sphere = MockSphere()
627-
x_sph = sphere.position[..., 0] - sphere.radius * sphere.director[2, :, 0]
627+
x_sph = sphere.position[..., 0]
628628

629629
"initializing constants"
630630
"""
@@ -640,16 +640,11 @@ def test_calculate_contact_forces_rod_sphere_with_k_without_nu_and_friction(
640640
_calculate_contact_forces_rod_sphere(
641641
rod_element_position,
642642
rod.lengths * rod.tangents,
643-
sphere.position[..., 0],
644643
x_sph,
645-
sphere.radius * sphere.director[2, :, 0],
646644
rod.radius + sphere.radius,
647645
rod.lengths + sphere.radius * 2,
648-
rod.internal_forces,
649646
rod.external_forces,
650647
sphere.external_forces,
651-
sphere.external_torques,
652-
sphere.director[:, :, 0],
653648
rod.velocity_collection,
654649
sphere.velocity_collection,
655650
k,

tests/test_contact_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
_clip,
1313
_out_of_bounds,
1414
_find_min_dist,
15+
_find_min_dist_cylinder_sphere,
1516
_aabbs_not_intersecting,
1617
_prune_using_aabbs_rod_cylinder,
1718
_prune_using_aabbs_rod_rod,

0 commit comments

Comments
 (0)