Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 5dc2eaa

Browse files
committed
separate 2d and 3d tests
1 parent 0c287e1 commit 5dc2eaa

File tree

3 files changed

+171
-50
lines changed

3 files changed

+171
-50
lines changed

tests/src/tests/friction/friction_data_generator.cpp

Lines changed: 107 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ FrictionData friction_data_generator()
149149

150150
using namespace ipc;
151151

152-
SmoothFrictionData<3> smooth_friction_data_generator()
152+
SmoothFrictionData<3> smooth_friction_data_generator_3d()
153153
{
154154
SmoothFrictionData<3> data;
155155

@@ -347,21 +347,117 @@ SmoothFrictionData<3> smooth_friction_data_generator()
347347
}
348348
// SECTION("point-edge 2D")
349349
// {
350-
// V0.resize(3, 2);
351-
// V0.row(0) << -0.5, d; // point at t=0
352-
// V0.row(1) << -1, 0; // edge vertex 0 at t=0
353-
// V0.row(2) << 1, 0; // edge vertex 1 at t=0
350+
// V0.resize(6, 2);
351+
// V0 << d, 0, // point at t=0
352+
// 0, -1, // edge vertex 0 at t=0
353+
// 0, 1, // edge vertex 1 at t=0
354+
// 2*d, -1,
355+
// 2*d, 1,
356+
// -1, 0;
354357

355358
// V1 = V0;
356-
// // double dy = GENERATE(-1, 1, 1e-1);
357-
// V1.row(0) << 0.5, d; // point at t=1
359+
// Eigen::Vector2d disp;
360+
// disp << 0, 1;
361+
// V1.row(0) += disp.transpose();
362+
// V1.row(1) += disp.transpose();
363+
// V1.row(2) += disp.transpose();
364+
365+
// F.resize(2, 3);
366+
// F << 0, 3, 4,
367+
// 1, 2, 5;
368+
369+
// igl::edges(F, E);
370+
371+
// int e = 0;
372+
// for (; e < E.rows(); e++)
373+
// {
374+
// if (std::min(E(e, 0), E(e, 1)) == 1 &&
375+
// std::max(E(e, 0), E(e, 1)) == 2)
376+
// break;
377+
// }
378+
// assert(e < E.rows());
379+
380+
// CollisionMesh mesh(V0, E, F);
381+
// collisions.collisions.push_back(std::make_shared<SmoothCollisionTemplate<max_vert_2d, Edge2, Point2>>(e, 0, PointEdgeDistanceType::AUTO, mesh, param, dhat, V0));
382+
// }
383+
// SECTION("point-point 2D")
384+
// {
385+
// V0.resize(2, 2);
386+
// V0.row(0) << -1, d; // point 0 at t=0
387+
// V0.row(1) << 1, d; // point 1 at t=0
358388

359-
// E.resize(1, 2);
360-
// E.row(0) << 1, 2;
389+
// V1 = V0;
390+
// // double dy = GENERATE(-1, 1, 1e-1);
391+
// V1.row(0) << 0.5, d; // edge a vertex 0 at t=1
392+
// V1.row(1) << -0.5, d; // edge a vertex 1 at t=1
361393

362-
// collisions.ev_collisions.emplace_back(0, 1);
363-
// collisions.ev_collisions.back().weight_gradient.resize(V0.size());
394+
// collisions.vv_collisions.emplace_back(0, 1);
395+
// collisions.vv_collisions.back().weight_gradient.resize(V0.size());
364396
// }
397+
398+
return data;
399+
}
400+
401+
SmoothFrictionData<2> smooth_friction_data_generator_2d()
402+
{
403+
SmoothFrictionData<2> data;
404+
405+
auto& [V0, V1, E, F, collisions, mu, epsv_times_h, param, barrier_stiffness] =
406+
data;
407+
408+
double &dhat = param.dhat;
409+
collisions.set_are_shape_derivatives_enabled(true);
410+
411+
mu = 1.; // GENERATE(range(0.0, 1.0, 0.2));
412+
#ifdef NDEBUG
413+
epsv_times_h = pow(10, GENERATE(range(-6, 0)));
414+
dhat = pow(10, GENERATE(range(-4, 0)));
415+
barrier_stiffness = pow(10, GENERATE(range(0, 2)));
416+
#else
417+
epsv_times_h = 1.; // pow(10, GENERATE(range(-6, 0, 2)));
418+
dhat = 1e-1; // pow(10, GENERATE(range(-4, 0, 2)));
419+
barrier_stiffness = 1.; // 100;
420+
#endif
421+
422+
param = ParameterType(dhat, 0.8, 0, 1, 0, 2);
423+
const double max_d = dhat * 0.9;
424+
const double min_d = dhat * 0.1;
425+
const double d = GENERATE_COPY(range(min_d, max_d, max_d / 10));
426+
SECTION("point-edge 2D")
427+
{
428+
V0.resize(6, 2);
429+
V0 << d, 0, // point at t=0
430+
0, -1, // edge vertex 0 at t=0
431+
0, 1, // edge vertex 1 at t=0
432+
2*d, -1,
433+
2*d, 1,
434+
-1, 0;
435+
436+
V1 = V0;
437+
Eigen::Vector2d disp;
438+
disp << 0, 1;
439+
V1.row(0) += disp.transpose();
440+
V1.row(3) += disp.transpose();
441+
V1.row(4) += disp.transpose();
442+
443+
F.resize(2, 3);
444+
F << 0, 3, 4,
445+
1, 2, 5;
446+
447+
igl::edges(F, E);
448+
449+
int e = 0;
450+
for (; e < E.rows(); e++)
451+
{
452+
if (std::min(E(e, 0), E(e, 1)) == 1 &&
453+
std::max(E(e, 0), E(e, 1)) == 2)
454+
break;
455+
}
456+
assert(e < E.rows());
457+
458+
CollisionMesh mesh(V0, E, F);
459+
collisions.collisions.push_back(std::make_shared<SmoothCollisionTemplate<max_vert_2d, Edge2, Point2>>(e, 0, PointEdgeDistanceType::AUTO, mesh, param, dhat, V0));
460+
}
365461
// SECTION("point-point 2D")
366462
// {
367463
// V0.resize(2, 2);

tests/src/tests/friction/friction_data_generator.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ struct SmoothFrictionData {
3535
double barrier_stiffness;
3636
};
3737

38-
SmoothFrictionData<3> smooth_friction_data_generator();
38+
SmoothFrictionData<2> smooth_friction_data_generator_2d();
39+
SmoothFrictionData<3> smooth_friction_data_generator_3d();

tests/src/tests/friction/test_force_jacobian.cpp

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,46 @@ void check_smooth_friction_force_jacobian(
404404

405405
///////////////////////////////////////////////////////////////////////////
406406

407+
auto create_smooth_collision = [&](const CollisionMesh &fd_mesh, const Eigen::MatrixXd &fd_lagged_positions) {
408+
SmoothCollisions<dim> fd_collisions;
409+
assert(friction_collisions.size() == 1);
410+
411+
if constexpr (dim == 3)
412+
{
413+
auto cc = friction_collisions[0].smooth_collision_3d;
414+
415+
std::shared_ptr<SmoothCollision<max_vert_3d>> fd_cc;
416+
if (cc->type() == CollisionType::EdgeEdge)
417+
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_3d, Edge3, Edge3>>((*cc)[0], (*cc)[1], PrimitiveDistType<Edge3, Edge3>::type::AUTO, fd_mesh, params, dhat, fd_lagged_positions);
418+
else if (cc->type() == CollisionType::EdgeVertex)
419+
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_3d, Edge3, Point3>>((*cc)[0], (*cc)[1], PrimitiveDistType<Edge3, Point3>::type::AUTO, fd_mesh, params, dhat, fd_lagged_positions);
420+
else if (cc->type() == CollisionType::VertexVertex)
421+
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_3d, Point3, Point3>>((*cc)[0], (*cc)[1], PrimitiveDistType<Point3, Point3>::type::AUTO, fd_mesh, params, dhat, fd_lagged_positions);
422+
else if (cc->type() == CollisionType::FaceVertex)
423+
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_3d, Face, Point3>>((*cc)[0], (*cc)[1], PrimitiveDistType<Face, Point3>::type::AUTO, fd_mesh, params, dhat, fd_lagged_positions);
424+
425+
fd_collisions.collisions.push_back(fd_cc);
426+
}
427+
else
428+
{
429+
auto cc = friction_collisions[0].smooth_collision_2d;
430+
431+
std::shared_ptr<SmoothCollision<max_vert_2d>> fd_cc;
432+
if (cc->type() == CollisionType::EdgeVertex)
433+
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_2d, Edge2, Point2>>((*cc)[0], (*cc)[1], PrimitiveDistType<Edge2, Point2>::type::AUTO, fd_mesh, params, dhat, fd_lagged_positions);
434+
else if (cc->type() == CollisionType::VertexVertex)
435+
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_2d, Point2, Point2>>((*cc)[0], (*cc)[1], PrimitiveDistType<Point2, Point2>::type::AUTO, fd_mesh, params, dhat, fd_lagged_positions);
436+
437+
fd_collisions.collisions.push_back(fd_cc);
438+
}
439+
440+
return fd_collisions;
441+
};
442+
443+
///////////////////////////////////////////////////////////////////////////
444+
407445
// test contact force norm derivative
408-
{
446+
if constexpr (dim == 3) {
409447
const Eigen::MatrixXd lagged_disp = X + Ut;
410448
Eigen::MatrixXd normal_force_jacobian = Eigen::MatrixXd::Zero(friction_collisions.size(), X.size());
411449
for (int i = 0; i < friction_collisions.size(); i++)
@@ -471,23 +509,7 @@ void check_smooth_friction_force_jacobian(
471509

472510
CollisionMesh fd_mesh(fd_X, mesh.edges(), mesh.faces());
473511

474-
SmoothCollisions<dim> fd_collisions;
475-
{
476-
CHECK(friction_collisions.size() == 1);
477-
auto cc = friction_collisions[0].smooth_collision_3d;
478-
479-
std::shared_ptr<SmoothCollision<max_vert_3d>> fd_cc;
480-
if (cc->type() == CollisionType::EdgeEdge)
481-
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_3d, Edge3, Edge3>>((*cc)[0], (*cc)[1], PrimitiveDistType<Edge3, Edge3>::type::AUTO, fd_mesh, params, dhat, fd_lagged_positions);
482-
else if (cc->type() == CollisionType::EdgeVertex)
483-
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_3d, Edge3, Point3>>((*cc)[0], (*cc)[1], PrimitiveDistType<Edge3, Point3>::type::AUTO, fd_mesh, params, dhat, fd_lagged_positions);
484-
else if (cc->type() == CollisionType::VertexVertex)
485-
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_3d, Point3, Point3>>((*cc)[0], (*cc)[1], PrimitiveDistType<Point3, Point3>::type::AUTO, fd_mesh, params, dhat, fd_lagged_positions);
486-
else if (cc->type() == CollisionType::FaceVertex)
487-
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_3d, Face, Point3>>((*cc)[0], (*cc)[1], PrimitiveDistType<Face, Point3>::type::AUTO, fd_mesh, params, dhat, fd_lagged_positions);
488-
489-
fd_collisions.collisions.push_back(fd_cc);
490-
}
512+
SmoothCollisions<dim> fd_collisions = create_smooth_collision(fd_mesh, fd_lagged_positions);
491513

492514
FrictionCollisions fd_friction_collisions;
493515
fd_friction_collisions.build_for_smooth_contact<dim>(
@@ -514,23 +536,7 @@ void check_smooth_friction_force_jacobian(
514536
Eigen::MatrixXd fd_Ut = fd::unflatten(ut, Ut.cols());
515537
Eigen::MatrixXd fd_lagged_positions = X + fd_Ut;
516538

517-
SmoothCollisions<dim> fd_collisions;
518-
{
519-
CHECK(friction_collisions.size() == 1);
520-
auto cc = friction_collisions[0].smooth_collision_3d;
521-
522-
std::shared_ptr<SmoothCollision<max_vert_3d>> fd_cc;
523-
if (cc->type() == CollisionType::EdgeEdge)
524-
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_3d, Edge3, Edge3>>((*cc)[0], (*cc)[1], PrimitiveDistType<Edge3, Edge3>::type::AUTO, mesh, params, dhat, fd_lagged_positions);
525-
else if (cc->type() == CollisionType::EdgeVertex)
526-
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_3d, Edge3, Point3>>((*cc)[0], (*cc)[1], PrimitiveDistType<Edge3, Point3>::type::AUTO, mesh, params, dhat, fd_lagged_positions);
527-
else if (cc->type() == CollisionType::VertexVertex)
528-
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_3d, Point3, Point3>>((*cc)[0], (*cc)[1], PrimitiveDistType<Point3, Point3>::type::AUTO, mesh, params, dhat, fd_lagged_positions);
529-
else if (cc->type() == CollisionType::FaceVertex)
530-
fd_cc = std::make_shared<SmoothCollisionTemplate<max_vert_3d, Face, Point3>>((*cc)[0], (*cc)[1], PrimitiveDistType<Face, Point3>::type::AUTO, mesh, params, dhat, fd_lagged_positions);
531-
532-
fd_collisions.collisions.push_back(fd_cc);
533-
}
539+
SmoothCollisions<dim> fd_collisions = create_smooth_collision(mesh, fd_lagged_positions);
534540

535541
FrictionCollisions fd_friction_collisions;
536542
fd_friction_collisions.build_for_smooth_contact<dim>(
@@ -563,9 +569,28 @@ void check_smooth_friction_force_jacobian(
563569
CHECK((fd_JF_wrt_V - JF_wrt_V).norm() <= 1e-7 * JF_wrt_V.norm());
564570
}
565571

566-
TEST_CASE("Smooth friction force jacobian", "[friction-smooth][force-jacobian]")
572+
TEST_CASE("Smooth friction force jacobian 2D", "[friction-smooth][force-jacobian]")
567573
{
568-
SmoothFrictionData<3> data = smooth_friction_data_generator();
574+
SmoothFrictionData<2> data = smooth_friction_data_generator_2d();
575+
const auto& [V0, V1, E, F, collisions, mu, epsv_times_h, param, barrier_stiffness] =
576+
data;
577+
REQUIRE(collisions.are_shape_derivatives_enabled());
578+
579+
Eigen::MatrixXd X, Ut, U;
580+
X = V0;
581+
Ut = V0 - X;
582+
U = V1 - X;
583+
584+
CollisionMesh mesh(X, E, F);
585+
586+
check_smooth_friction_force_jacobian<2>(
587+
mesh, Ut, U, collisions, mu, epsv_times_h, param, barrier_stiffness,
588+
false);
589+
}
590+
591+
TEST_CASE("Smooth friction force jacobian 3D", "[friction-smooth][force-jacobian]")
592+
{
593+
SmoothFrictionData<3> data = smooth_friction_data_generator_3d();
569594
const auto& [V0, V1, E, F, collisions, mu, epsv_times_h, param, barrier_stiffness] =
570595
data;
571596
REQUIRE(collisions.are_shape_derivatives_enabled());
@@ -576,7 +601,6 @@ TEST_CASE("Smooth friction force jacobian", "[friction-smooth][force-jacobian]")
576601
U = V1 - X;
577602

578603
CollisionMesh mesh(X, E, F);
579-
mesh.init_area_jacobians();
580604

581605
check_smooth_friction_force_jacobian<3>(
582606
mesh, Ut, U, collisions, mu, epsv_times_h, param, barrier_stiffness,

0 commit comments

Comments
 (0)