Files
CCLite/tests/unit/test_broad_phase.f90
2026-07-05 23:58:34 +08:00

108 lines
4.9 KiB
Fortran

program test_broad_phase
use iso_c_binding, only: c_double
use ccl_broad_phase, only: CCL_Broad_Phase_Stats_Type, CCL_Candidate_Pair_Type, &
CCL_Hash_Grid_Type, ccl_broad_phase_stats_reset, ccl_hash_grid_init, &
ccl_hash_grid_insert_facet, ccl_hash_grid_query_particle
use ccl_errors, only: CCL_ERROR_INPUT_INVALID_VALUE, CCL_SUCCESS
use ccl_geometry, only: CCL_AABB_Type, ccl_aabb_init, ccl_aabb_expand_point, ccl_particle_aabb
implicit none
integer :: ierr
integer :: candidate_count
type(CCL_Hash_Grid_Type) :: grid
type(CCL_Broad_Phase_Stats_Type) :: stats
type(CCL_AABB_Type) :: facet_box
type(CCL_AABB_Type) :: particle_box
type(CCL_Candidate_Pair_Type) :: candidates(4)
call ccl_hash_grid_init(grid, 1.0_c_double, 16, ierr)
call require_equal(ierr, CCL_SUCCESS, "grid init")
call ccl_broad_phase_stats_reset(stats)
call make_box([0.0_c_double, 0.0_c_double, 0.0_c_double], &
[1.0_c_double, 1.0_c_double, 0.0_c_double], facet_box)
call ccl_hash_grid_insert_facet(grid, 1, facet_box, stats, ierr)
call require_equal(ierr, CCL_SUCCESS, "facet insert")
call require_true(stats%facet_insert_count > 0, "facet insertion counted")
call require_true(stats%grid_cell_count > 0, "grid cells counted")
call ccl_particle_aabb([0.5_c_double, 0.5_c_double, 0.0_c_double], 0.1_c_double, particle_box, ierr)
call require_equal(ierr, CCL_SUCCESS, "particle AABB near facet")
candidate_count = 0
call ccl_hash_grid_query_particle(grid, 1, particle_box, candidates, candidate_count, 4, stats, ierr)
call require_equal(ierr, CCL_SUCCESS, "near particle query")
call require_equal(candidate_count, 1, "near particle candidate count")
call require_equal(candidates(1)%particle_index, 1, "candidate particle id")
call require_equal(candidates(1)%facet_index, 1, "candidate facet id")
call ccl_particle_aabb([5.0_c_double, 5.0_c_double, 0.0_c_double], 0.1_c_double, particle_box, ierr)
call require_equal(ierr, CCL_SUCCESS, "particle AABB far")
candidate_count = 0
call ccl_hash_grid_query_particle(grid, 2, particle_box, candidates, candidate_count, 4, stats, ierr)
call require_equal(ierr, CCL_SUCCESS, "far particle query")
call require_equal(candidate_count, 0, "far particle has no candidate")
call ccl_hash_grid_init(grid, 1.0_c_double, 16, ierr)
call require_equal(ierr, CCL_SUCCESS, "boundary grid init")
call ccl_broad_phase_stats_reset(stats)
call make_box([0.9_c_double, 0.0_c_double, 0.0_c_double], &
[1.1_c_double, 0.2_c_double, 0.0_c_double], facet_box)
call ccl_hash_grid_insert_facet(grid, 3, facet_box, stats, ierr)
call require_equal(ierr, CCL_SUCCESS, "boundary facet insert")
call ccl_particle_aabb([1.0_c_double, 0.1_c_double, 0.0_c_double], 0.05_c_double, particle_box, ierr)
candidate_count = 0
call ccl_hash_grid_query_particle(grid, 3, particle_box, candidates, candidate_count, 4, stats, ierr)
call require_equal(ierr, CCL_SUCCESS, "boundary particle query")
call require_equal(candidate_count, 1, "boundary candidate deduplicated")
call require_true(stats%duplicate_candidate_count >= 1, "duplicate candidate counted")
call ccl_hash_grid_init(grid, 1.0_c_double, 16, ierr)
call ccl_broad_phase_stats_reset(stats)
call make_box([0.0_c_double, 0.0_c_double, 0.0_c_double], &
[0.2_c_double, 0.2_c_double, 0.0_c_double], facet_box)
call ccl_hash_grid_insert_facet(grid, 4, facet_box, stats, ierr)
call require_equal(ierr, CCL_SUCCESS, "overflow facet insert")
call ccl_particle_aabb([0.1_c_double, 0.1_c_double, 0.0_c_double], 0.05_c_double, particle_box, ierr)
candidate_count = 0
call ccl_hash_grid_query_particle(grid, 4, particle_box, candidates, candidate_count, 0, stats, ierr)
call require_equal(ierr, CCL_ERROR_INPUT_INVALID_VALUE, "candidate overflow rejected")
call require_equal(stats%overflow_count, 1, "candidate overflow counted")
contains
subroutine make_box(min_corner, max_corner, box)
real(c_double), intent(in) :: min_corner(3)
real(c_double), intent(in) :: max_corner(3)
type(CCL_AABB_Type), intent(out) :: box
integer :: local_ierr
call ccl_aabb_init(box)
call ccl_aabb_expand_point(box, min_corner, local_ierr)
call require_equal(local_ierr, CCL_SUCCESS, "box min point")
call ccl_aabb_expand_point(box, max_corner, local_ierr)
call require_equal(local_ierr, CCL_SUCCESS, "box max point")
end subroutine make_box
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_true(condition, message)
logical, intent(in) :: condition
character(len=*), intent(in) :: message
if (.not. condition) then
print '(a)', trim(message)
stop 1
end if
end subroutine require_true
end program test_broad_phase