diff --git a/CMakeLists.txt b/CMakeLists.txt index 2366eb8..2e18c8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(CCL_COMMON_SOURCES src/common/ccl_log.f90 src/geometry/ccl_geometry.f90 src/search/ccl_broad_phase.f90 + src/contact/ccl_contact_narrow.f90 src/diagnostics/ccl_diagnostics.f90 src/case/ccl_string_utils.f90 src/case/ccl_case_types.f90 @@ -99,6 +100,11 @@ add_executable(test_broad_phase ) target_link_libraries(test_broad_phase PRIVATE ccl_common) +add_executable(test_contact_narrow + tests/unit/test_contact_narrow.f90 +) +target_link_libraries(test_contact_narrow PRIVATE ccl_common) + add_test(NAME smoke_cclite_version COMMAND cclite --version) add_test(NAME smoke_cclite_help COMMAND cclite --help) add_test(NAME smoke_mock_sph_worker_version COMMAND mock_sph_worker --version) @@ -148,6 +154,7 @@ add_test(NAME data_block_semantics COMMAND test_data_block) add_test(NAME V020_interface_view_and_load_buffer COMMAND test_interface_view) add_test(NAME V020_geometry_primitives COMMAND test_geometry_primitives) add_test(NAME V020_broad_phase_search COMMAND test_broad_phase) +add_test(NAME V020_narrow_phase_contact COMMAND test_contact_narrow) add_test(NAME dry_run_one_step COMMAND cclite dry-run ${CMAKE_SOURCE_DIR}/tests/data/v010_one_step.inp ) diff --git a/src/contact/ccl_contact_narrow.f90 b/src/contact/ccl_contact_narrow.f90 new file mode 100644 index 0000000..2c8eeeb --- /dev/null +++ b/src/contact/ccl_contact_narrow.f90 @@ -0,0 +1,181 @@ +module ccl_contact_narrow + use iso_c_binding, only: c_double, c_int32_t, c_int64_t + use ccl_broad_phase, only: CCL_Candidate_Pair_Type + use ccl_errors, only: CCL_ERROR_INPUT_INVALID_VALUE, CCL_SUCCESS, CCL_ERROR_SYSTEM_INTERNAL + use ccl_geometry, only: CCL_Geometry_Diagnostics_Type, ccl_point_quad_closest, & + ccl_point_triangle_closest, ccl_vec3_dot, ccl_vec3_normalize + use ccl_interface_view, only: CCL_FEM_Facet_View_Type, CCL_SPH_Particle_View_Type + implicit none + + integer, parameter :: CCL_CONTACT_MAX_NODES = 8 + + type :: CCL_Contact_Record_Type + integer(c_int32_t) :: step_id = 0_c_int32_t + integer(c_int32_t) :: pair_id = 0_c_int32_t + integer :: particle_index = 0 + integer :: facet_index = 0 + integer(c_int64_t) :: sph_external_id = 0_c_int64_t + integer(c_int64_t) :: facet_id = 0_c_int64_t + integer(c_int32_t) :: node_count = 0_c_int32_t + real(c_double) :: closest_point(3) = 0.0_c_double + real(c_double) :: shape_weights(CCL_CONTACT_MAX_NODES) = 0.0_c_double + real(c_double) :: normal(3) = 0.0_c_double + real(c_double) :: gap = 0.0_c_double + real(c_double) :: contact_distance = 0.0_c_double + real(c_double) :: penetration = 0.0_c_double + end type CCL_Contact_Record_Type + + type :: CCL_Narrow_Phase_Stats_Type + integer :: candidate_count = 0 + integer :: contact_count = 0 + integer :: skipped_separated_count = 0 + integer :: skipped_degenerate_count = 0 + integer :: skipped_invalid_count = 0 + end type CCL_Narrow_Phase_Stats_Type + +contains + + subroutine ccl_narrow_phase_stats_reset(stats) + type(CCL_Narrow_Phase_Stats_Type), intent(out) :: stats + + stats%candidate_count = 0 + stats%contact_count = 0 + stats%skipped_separated_count = 0 + stats%skipped_degenerate_count = 0 + stats%skipped_invalid_count = 0 + end subroutine ccl_narrow_phase_stats_reset + + subroutine ccl_narrow_phase_run(step_id, pair_id, particles, particle_count, facets, facet_count, & + candidates, candidate_count, contacts, contact_count, max_contacts, stats, ierr) + integer(c_int32_t), intent(in) :: step_id + integer(c_int32_t), intent(in) :: pair_id + type(CCL_SPH_Particle_View_Type), intent(in) :: particles(:) + integer, intent(in) :: particle_count + type(CCL_FEM_Facet_View_Type), intent(in) :: facets(:) + integer, intent(in) :: facet_count + type(CCL_Candidate_Pair_Type), intent(in) :: candidates(:) + integer, intent(in) :: candidate_count + type(CCL_Contact_Record_Type), intent(inout) :: contacts(:) + integer, intent(out) :: contact_count + integer, intent(in) :: max_contacts + type(CCL_Narrow_Phase_Stats_Type), intent(inout) :: stats + integer, intent(out) :: ierr + integer :: i + integer :: local_ierr + type(CCL_Contact_Record_Type) :: contact + + ierr = CCL_SUCCESS + contact_count = 0 + stats%candidate_count = candidate_count + if (candidate_count < 0 .or. max_contacts < 0 .or. max_contacts > size(contacts)) then + ierr = CCL_ERROR_INPUT_INVALID_VALUE + return + end if + + do i = 1, candidate_count + if (candidates(i)%particle_index < 1 .or. candidates(i)%particle_index > particle_count .or. & + candidates(i)%facet_index < 1 .or. candidates(i)%facet_index > facet_count) then + stats%skipped_invalid_count = stats%skipped_invalid_count + 1 + cycle + end if + + call ccl_narrow_phase_candidate(step_id, pair_id, particles(candidates(i)%particle_index), & + facets(candidates(i)%facet_index), candidates(i), contact, local_ierr) + if (local_ierr == CCL_ERROR_INPUT_INVALID_VALUE) then + stats%skipped_degenerate_count = stats%skipped_degenerate_count + 1 + cycle + end if + if (local_ierr /= CCL_SUCCESS) then + ierr = local_ierr + return + end if + if (contact%penetration <= 0.0_c_double) then + stats%skipped_separated_count = stats%skipped_separated_count + 1 + cycle + end if + if (contact_count >= max_contacts) then + ierr = CCL_ERROR_SYSTEM_INTERNAL + return + end if + contact_count = contact_count + 1 + contacts(contact_count) = contact + end do + stats%contact_count = contact_count + end subroutine ccl_narrow_phase_run + + subroutine ccl_narrow_phase_candidate(step_id, pair_id, particle, facet, candidate, contact, ierr) + integer(c_int32_t), intent(in) :: step_id + integer(c_int32_t), intent(in) :: pair_id + type(CCL_SPH_Particle_View_Type), intent(in) :: particle + type(CCL_FEM_Facet_View_Type), intent(in) :: facet + type(CCL_Candidate_Pair_Type), intent(in) :: candidate + type(CCL_Contact_Record_Type), intent(out) :: contact + integer, intent(out) :: ierr + type(CCL_Geometry_Diagnostics_Type) :: geom_diag + real(c_double) :: normal(3) + real(c_double) :: tri_weights(3) + real(c_double) :: quad_x(3, 4) + real(c_double) :: quad_weights(4) + real(c_double) :: distance2 + + ierr = CCL_SUCCESS + contact = CCL_Contact_Record_Type() + call ccl_vec3_normalize(facet%normal, normal, ierr, geom_diag) + if (ierr /= CCL_SUCCESS) return + + select case (facet%node_count) + case (3_c_int32_t) + call ccl_point_triangle_closest(particle%x, facet%x(:, 1), facet%x(:, 2), facet%x(:, 3), & + contact%closest_point, tri_weights, distance2, ierr, geom_diag) + if (ierr /= CCL_SUCCESS) return + contact%shape_weights(1:3) = tri_weights + case (4_c_int32_t) + quad_x = facet%x(:, 1:4) + call ccl_point_quad_closest(particle%x, quad_x, contact%closest_point, quad_weights, & + distance2, ierr, geom_diag) + if (ierr /= CCL_SUCCESS) return + contact%shape_weights(1:4) = quad_weights + case default + ierr = CCL_ERROR_INPUT_INVALID_VALUE + return + end select + + contact%step_id = step_id + contact%pair_id = pair_id + contact%particle_index = candidate%particle_index + contact%facet_index = candidate%facet_index + contact%sph_external_id = particle%external_id + contact%facet_id = facet%facet_id + contact%node_count = facet%node_count + contact%normal = normal + contact%gap = ccl_vec3_dot(particle%x - contact%closest_point, normal) + contact%contact_distance = particle%contact_radius + max(0.0_c_double, facet%thickness) * 0.5_c_double + contact%penetration = contact%contact_distance - contact%gap + end subroutine ccl_narrow_phase_candidate + + subroutine ccl_contact_report_write_csv(filename, contacts, contact_count, ierr) + character(len=*), intent(in) :: filename + type(CCL_Contact_Record_Type), intent(in) :: contacts(:) + integer, intent(in) :: contact_count + integer, intent(out) :: ierr + integer :: unit_id + integer :: ios + integer :: i + + ierr = CCL_SUCCESS + open(newunit=unit_id, file=trim(filename), status="replace", action="write", iostat=ios) + if (ios /= 0) then + ierr = CCL_ERROR_INPUT_INVALID_VALUE + return + end if + write(unit_id, '(a)') "step_id,pair_id,sph_id,facet_id,gap,penetration,normal_x,normal_y,normal_z,node_count" + do i = 1, contact_count + write(unit_id, '(i0,",",i0,",",i0,",",i0,",",es16.8,",",es16.8,",",es16.8,",",es16.8,",",es16.8,",",i0)') & + contacts(i)%step_id, contacts(i)%pair_id, contacts(i)%sph_external_id, contacts(i)%facet_id, & + contacts(i)%gap, contacts(i)%penetration, contacts(i)%normal(1), contacts(i)%normal(2), & + contacts(i)%normal(3), contacts(i)%node_count + end do + close(unit_id) + end subroutine ccl_contact_report_write_csv + +end module ccl_contact_narrow diff --git a/tests/unit/test_contact_narrow.f90 b/tests/unit/test_contact_narrow.f90 new file mode 100644 index 0000000..893610a --- /dev/null +++ b/tests/unit/test_contact_narrow.f90 @@ -0,0 +1,147 @@ +program test_contact_narrow + use iso_c_binding, only: c_double, c_int32_t, c_int64_t + use ccl_broad_phase, only: CCL_Candidate_Pair_Type + use ccl_contact_narrow, only: CCL_Contact_Record_Type, CCL_Narrow_Phase_Stats_Type, & + ccl_contact_report_write_csv, ccl_narrow_phase_run, ccl_narrow_phase_stats_reset + use ccl_errors, only: CCL_SUCCESS + use ccl_interface_view, only: CCL_FEM_Facet_View_Type, CCL_SPH_Particle_View_Type + implicit none + + integer :: ierr + integer :: contact_count + type(CCL_SPH_Particle_View_Type) :: particles(2) + type(CCL_FEM_Facet_View_Type) :: facets(2) + type(CCL_Candidate_Pair_Type) :: candidates(2) + type(CCL_Contact_Record_Type) :: contacts(4) + type(CCL_Narrow_Phase_Stats_Type) :: stats + + call make_particle(particles(1), 101_c_int64_t, [0.25_c_double, 0.25_c_double, 0.10_c_double], & + 0.20_c_double) + call make_particle(particles(2), 102_c_int64_t, [0.25_c_double, 0.25_c_double, 1.00_c_double], & + 0.20_c_double) + call make_quad_facet(facets(1), 201_c_int64_t) + call make_degenerate_facet(facets(2), 202_c_int64_t) + + candidates(1)%particle_index = 1 + candidates(1)%facet_index = 1 + call ccl_narrow_phase_stats_reset(stats) + call ccl_narrow_phase_run(3_c_int32_t, 7_c_int32_t, particles, 2, facets, 2, candidates, 1, & + contacts, contact_count, 4, stats, ierr) + call require_equal(ierr, CCL_SUCCESS, "penetrating candidate accepted") + call require_equal(contact_count, 1, "one contact generated") + call require_equal(stats%contact_count, 1, "contact count tracked") + call require_real_close(contacts(1)%gap, 0.10_c_double, "gap") + call require_real_close(contacts(1)%penetration, 0.10_c_double, "penetration") + call require_vector_close(contacts(1)%normal, [0.0_c_double, 0.0_c_double, 1.0_c_double], "normal") + call require_real_close(sum(contacts(1)%shape_weights(1:4)), 1.0_c_double, "shape weight sum") + + candidates(1)%particle_index = 2 + candidates(1)%facet_index = 1 + call ccl_narrow_phase_stats_reset(stats) + call ccl_narrow_phase_run(3_c_int32_t, 7_c_int32_t, particles, 2, facets, 2, candidates, 1, & + contacts, contact_count, 4, stats, ierr) + call require_equal(ierr, CCL_SUCCESS, "separated candidate accepted") + call require_equal(contact_count, 0, "no contact for separated particle") + call require_equal(stats%skipped_separated_count, 1, "separated skip counted") + + candidates(1)%particle_index = 1 + candidates(1)%facet_index = 2 + call ccl_narrow_phase_stats_reset(stats) + call ccl_narrow_phase_run(3_c_int32_t, 7_c_int32_t, particles, 2, facets, 2, candidates, 1, & + contacts, contact_count, 4, stats, ierr) + call require_equal(ierr, CCL_SUCCESS, "degenerate candidate skipped") + call require_equal(contact_count, 0, "no contact for degenerate facet") + call require_equal(stats%skipped_degenerate_count, 1, "degenerate skip counted") + + candidates(1)%particle_index = 1 + candidates(1)%facet_index = 1 + call ccl_narrow_phase_stats_reset(stats) + call ccl_narrow_phase_run(3_c_int32_t, 7_c_int32_t, particles, 2, facets, 2, candidates, 1, & + contacts, contact_count, 4, stats, ierr) + call require_equal(ierr, CCL_SUCCESS, "contact before report") + call ccl_contact_report_write_csv("v020_contact_report_unit.csv", contacts, contact_count, ierr) + call require_equal(ierr, CCL_SUCCESS, "contact report csv written") + +contains + + subroutine make_particle(particle, external_id, x, radius) + type(CCL_SPH_Particle_View_Type), intent(out) :: particle + integer(c_int64_t), intent(in) :: external_id + real(c_double), intent(in) :: x(3) + real(c_double), intent(in) :: radius + + particle = CCL_SPH_Particle_View_Type() + particle%external_id = external_id + particle%internal_index = int(external_id - 100_c_int64_t, c_int32_t) + particle%status = 1_c_int32_t + particle%valid_flags = 1_c_int32_t + particle%contact_radius = radius + particle%mass = 1.0_c_double + particle%x = x + particle%v = 0.0_c_double + end subroutine make_particle + + subroutine make_quad_facet(facet, facet_id) + type(CCL_FEM_Facet_View_Type), intent(out) :: facet + integer(c_int64_t), intent(in) :: facet_id + + facet = CCL_FEM_Facet_View_Type() + facet%facet_id = facet_id + facet%node_count = 4_c_int32_t + facet%node_ids(1:4) = [201_c_int64_t, 202_c_int64_t, 203_c_int64_t, 204_c_int64_t] + facet%area = 1.0_c_double + facet%thickness = 0.0_c_double + facet%normal = [0.0_c_double, 0.0_c_double, 1.0_c_double] + facet%x(:, 1) = [0.0_c_double, 0.0_c_double, 0.0_c_double] + facet%x(:, 2) = [1.0_c_double, 0.0_c_double, 0.0_c_double] + facet%x(:, 3) = [1.0_c_double, 1.0_c_double, 0.0_c_double] + facet%x(:, 4) = [0.0_c_double, 1.0_c_double, 0.0_c_double] + facet%v = 0.0_c_double + facet%valid_flags = 1_c_int32_t + end subroutine make_quad_facet + + subroutine make_degenerate_facet(facet, facet_id) + type(CCL_FEM_Facet_View_Type), intent(out) :: facet + integer(c_int64_t), intent(in) :: facet_id + + call make_quad_facet(facet, facet_id) + facet%x(:, 1) = [0.0_c_double, 0.0_c_double, 0.0_c_double] + facet%x(:, 2) = [1.0_c_double, 0.0_c_double, 0.0_c_double] + facet%x(:, 3) = [2.0_c_double, 0.0_c_double, 0.0_c_double] + facet%x(:, 4) = [3.0_c_double, 0.0_c_double, 0.0_c_double] + end subroutine make_degenerate_facet + + subroutine require_equal(actual, expected, message) + integer, intent(in) :: actual + integer, intent(in) :: expected + character(len=*), intent(in) :: message + + if (actual /= expected) then + print '(a,": expected ",i0," got ",i0)', trim(message), expected, actual + stop 1 + end if + end subroutine require_equal + + subroutine require_real_close(actual, expected, message) + real(c_double), intent(in) :: actual + real(c_double), intent(in) :: expected + character(len=*), intent(in) :: message + + if (abs(actual - expected) > 1.0e-10_c_double) then + print '(a,": expected ",es12.4," got ",es12.4)', trim(message), expected, actual + stop 1 + end if + end subroutine require_real_close + + subroutine require_vector_close(actual, expected, message) + real(c_double), intent(in) :: actual(3) + real(c_double), intent(in) :: expected(3) + character(len=*), intent(in) :: message + + if (maxval(abs(actual - expected)) > 1.0e-10_c_double) then + print '(a,": vector mismatch")', trim(message) + stop 1 + end if + end subroutine require_vector_close + +end program test_contact_narrow diff --git a/todo.md b/todo.md index 36da489..0b96177 100644 --- a/todo.md +++ b/todo.md @@ -227,37 +227,37 @@ 目标:从 candidate pair 生成真实接触对,输出 gap、penetration、normal 和 shape weight。 -- [ ] T0205-01 定义 contact pair record。 +- [x] T0205-01 定义 contact pair record。 完成判据:包含 step、pair id、SPH id、facet id、gap、penetration、normal 和 shape weight。 -- [ ] T0205-02 实现 candidate 窄相循环。 +- [x] T0205-02 实现 candidate 窄相循环。 完成判据:每个宽相候选最多生成一条窄相 contact record。 -- [ ] T0205-03 计算 facet 最近点。 +- [x] T0205-03 计算 facet 最近点。 完成判据:三角形 facet 使用 point-triangle,四边形 facet 使用 split triangle。 -- [ ] T0205-04 计算局部接触法向。 +- [x] T0205-04 计算局部接触法向。 完成判据:法向方向与 04 号文档约定一致,并可在诊断中复核。 -- [ ] T0205-05 计算法向间隙 `g`。 +- [x] T0205-05 计算法向间隙 `g`。 完成判据:粒子中心到 facet 最近点的符号距离可复现。 -- [ ] T0205-06 计算有效接触距离。 +- [x] T0205-06 计算有效接触距离。 完成判据:SPH `contact_radius` 与 facet 厚度或容差的组合规则明确。 -- [ ] T0205-07 计算穿透量 `penetration`。 +- [x] T0205-07 计算穿透量 `penetration`。 完成判据:满足 `penetration = contact_distance - g`,非穿透时不生成接触力。 -- [ ] T0205-08 保存 FEM 节点 shape weight。 +- [x] T0205-08 保存 FEM 节点 shape weight。 完成判据:后续节点力分配可直接使用 contact record 中的权重。 -- [ ] T0205-09 记录窄相跳过原因。 +- [x] T0205-09 记录窄相跳过原因。 完成判据:远离、退化、非法法向和非有限输入有独立计数。 -- [ ] T0205-10 输出基础 `contact_report.csv`。 +- [x] T0205-10 输出基础 `contact_report.csv`。 完成判据:包含 step、pair、粒子、facet、gap、penetration、normal 和 node_count 字段。 -- [ ] T0205-11 增加窄相 contact 单元测试。 +- [x] T0205-11 增加窄相 contact 单元测试。 完成判据:覆盖无接触、穿透接触、法向方向、shape weight 和退化 facet。 # 8. 0.2.6 Penalty Force 与节点分配