From b2a9caa54a2b48e3454212a74254258606dd5578 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 3 Jul 2026 22:56:50 +0800 Subject: [PATCH] Add blast source snapshots and target filtering --- CMakeLists.txt | 12 + src/blast/sph_blast.f90 | 318 +++++++++++++++++++- src/input/sph_input_reader.f90 | 4 +- src/solver/sph_solver_explicit.f90 | 5 + tests/module/test_blast_source_snapshot.f90 | 107 +++++++ tests/module/test_blast_target_filter.f90 | 124 ++++++++ todo.md | 60 ++-- 7 files changed, 597 insertions(+), 33 deletions(-) create mode 100644 tests/module/test_blast_source_snapshot.f90 create mode 100644 tests/module/test_blast_target_filter.f90 diff --git a/CMakeLists.txt b/CMakeLists.txt index 1658036..e370c2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -191,6 +191,18 @@ set_target_properties(test_input_blast_errors PROPERTIES Fortran_MODULE_DIRECTOR add_test(NAME input_blast_errors COMMAND test_input_blast_errors) set_tests_properties(input_blast_errors PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_blast_source_snapshot tests/module/test_blast_source_snapshot.f90) +target_link_libraries(test_blast_source_snapshot PRIVATE sphlite_core) +set_target_properties(test_blast_source_snapshot PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME blast_source_snapshot COMMAND test_blast_source_snapshot) +set_tests_properties(blast_source_snapshot PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_blast_target_filter tests/module/test_blast_target_filter.f90) +target_link_libraries(test_blast_target_filter PRIVATE sphlite_core) +set_target_properties(test_blast_target_filter PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME blast_target_filter COMMAND test_blast_target_filter) +set_tests_properties(blast_target_filter PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_executable(test_input_solid_material tests/module/test_input_solid_material.f90) target_link_libraries(test_input_solid_material PRIVATE sphlite_core) set_target_properties(test_input_solid_material PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") diff --git a/src/blast/sph_blast.f90 b/src/blast/sph_blast.f90 index cfb3c0e..8e212da 100644 --- a/src/blast/sph_blast.f90 +++ b/src/blast/sph_blast.f90 @@ -2,8 +2,14 @@ module sph_blast use sph_precision, only: rk, SPH_NDIM use sph_error, only: SPH_SUCCESS, SPH_ERROR_INPUT, SPH_ERROR_DATABASE, & sph_error_set - use sph_database, only: SPH_NAME_LEN, SPH_MAX_REGIONS, sph_db_find_elset, & - sph_db_find_nset + use sph_database, only: SPH_NAME_LEN, SPH_MAX_REGIONS, npart, & + particle_id_ext, particle_phase, particle_status, x, mass, volume, & + pressure, internal_energy, nset_member_count, nset_members, & + elset_member_count, elset_members, element_conn_count, element_conn, & + SPH_PHASE_WATER, SPH_PHASE_GAS, SPH_PHASE_EXPLOSIVE, & + SPH_PHASE_SOLID, SPH_PHASE_SHELL, SPH_STATUS_ACTIVE, & + sph_db_find_elset, sph_db_find_nset, sph_db_find_element + use sph_explosive_state, only: explosive_reaction_progress implicit none integer, parameter :: SPH_BLAST_PHASE_NONE = 0 @@ -30,6 +36,21 @@ module sph_blast real(rk) :: blast_radius(SPH_MAX_REGIONS) = 0.0_rk real(rk) :: blast_load_scale(SPH_MAX_REGIONS) = 0.0_rk real(rk) :: blast_time_window(SPH_MAX_REGIONS) = 0.0_rk + integer :: blast_source_count(SPH_MAX_REGIONS) = 0 + integer :: blast_target_count(SPH_MAX_REGIONS) = 0 + integer :: blast_candidate_pair_count(SPH_MAX_REGIONS) = 0 + integer :: blast_skipped_source_count(SPH_MAX_REGIONS) = 0 + integer :: blast_skipped_target_count(SPH_MAX_REGIONS) = 0 + integer :: blast_gas_count(SPH_MAX_REGIONS) = 0 + integer :: blast_water_count(SPH_MAX_REGIONS) = 0 + integer :: blast_explosive_count(SPH_MAX_REGIONS) = 0 + real(rk) :: blast_source_mass(SPH_MAX_REGIONS) = 0.0_rk + real(rk) :: blast_source_energy(SPH_MAX_REGIONS) = 0.0_rk + real(rk) :: blast_source_pressure_mean(SPH_MAX_REGIONS) = 0.0_rk + real(rk) :: blast_gas_volume(SPH_MAX_REGIONS) = 0.0_rk + real(rk) :: blast_explosive_progress_mean(SPH_MAX_REGIONS) = 0.0_rk + real(rk) :: blast_water_pressure_min(SPH_MAX_REGIONS) = 0.0_rk + real(rk) :: blast_water_pressure_max(SPH_MAX_REGIONS) = 0.0_rk contains @@ -49,8 +70,27 @@ contains blast_radius = 0.0_rk blast_load_scale = 0.0_rk blast_time_window = 0.0_rk + call sph_blast_clear_snapshot() end subroutine sph_blast_reset + subroutine sph_blast_clear_snapshot() + blast_source_count = 0 + blast_target_count = 0 + blast_candidate_pair_count = 0 + blast_skipped_source_count = 0 + blast_skipped_target_count = 0 + blast_gas_count = 0 + blast_water_count = 0 + blast_explosive_count = 0 + blast_source_mass = 0.0_rk + blast_source_energy = 0.0_rk + blast_source_pressure_mean = 0.0_rk + blast_gas_volume = 0.0_rk + blast_explosive_progress_mean = 0.0_rk + blast_water_pressure_min = 0.0_rk + blast_water_pressure_max = 0.0_rk + end subroutine sph_blast_clear_snapshot + subroutine sph_blast_add_region(name, center, radius, source_phase_name, & target_phase_name, target_set_name, scale, time_window, mode_name, ierr) character(len=*), intent(in) :: name @@ -154,6 +194,280 @@ contains end do end subroutine sph_blast_count_unused_regions + subroutine sph_blast_refresh_snapshot(ierr) + integer, intent(out) :: ierr + integer :: region + + ierr = SPH_SUCCESS + call sph_blast_clear_snapshot() + if (npart <= 0) return + do region = 1, blast_region_count + if (.not. blast_region_enabled(region)) cycle + call sph_blast_accumulate_region(region) + end do + end subroutine sph_blast_refresh_snapshot + + subroutine sph_blast_accumulate_region(region) + integer, intent(in) :: region + integer :: i + + do i = 1, npart + if (sph_blast_phase_matches(blast_source_phase(region), & + particle_phase(i))) then + if (sph_blast_source_accepts(region, i)) then + call sph_blast_accumulate_source(region, i) + else + blast_skipped_source_count(region) = & + blast_skipped_source_count(region) + 1 + end if + end if + if (sph_blast_phase_matches(blast_target_phase(region), & + particle_phase(i))) then + if (sph_blast_target_accepts(region, i)) then + call sph_blast_accumulate_target(region, i) + else + blast_skipped_target_count(region) = & + blast_skipped_target_count(region) + 1 + end if + end if + call sph_blast_accumulate_environment(region, i) + end do + if (blast_source_count(region) > 0) then + blast_source_pressure_mean(region) = & + blast_source_pressure_mean(region) / & + real(blast_source_count(region), rk) + end if + if (blast_explosive_count(region) > 0) then + blast_explosive_progress_mean(region) = & + blast_explosive_progress_mean(region) / & + real(blast_explosive_count(region), rk) + end if + call sph_blast_count_candidate_pairs(region) + end subroutine sph_blast_accumulate_region + + subroutine sph_blast_accumulate_source(region, index) + integer, intent(in) :: region + integer, intent(in) :: index + + blast_source_count(region) = blast_source_count(region) + 1 + blast_source_mass(region) = blast_source_mass(region) + mass(index) + blast_source_energy(region) = blast_source_energy(region) + & + mass(index) * internal_energy(index) + blast_source_pressure_mean(region) = blast_source_pressure_mean(region) + & + pressure(index) + end subroutine sph_blast_accumulate_source + + subroutine sph_blast_accumulate_target(region, index) + integer, intent(in) :: region + integer, intent(in) :: index + + blast_target_count(region) = blast_target_count(region) + 1 + end subroutine sph_blast_accumulate_target + + subroutine sph_blast_accumulate_environment(region, index) + integer, intent(in) :: region + integer, intent(in) :: index + + if (.not. sph_blast_is_active(index)) return + if (.not. sph_blast_in_region(region, index)) return + if (particle_phase(index) == SPH_PHASE_GAS) then + blast_gas_count(region) = blast_gas_count(region) + 1 + blast_gas_volume(region) = blast_gas_volume(region) + volume(index) + else if (particle_phase(index) == SPH_PHASE_WATER) then + call sph_blast_accumulate_water(region, index) + else if (particle_phase(index) == SPH_PHASE_EXPLOSIVE) then + call sph_blast_accumulate_explosive(region, index) + end if + end subroutine sph_blast_accumulate_environment + + subroutine sph_blast_accumulate_water(region, index) + integer, intent(in) :: region + integer, intent(in) :: index + + if (blast_water_count(region) == 0) then + blast_water_pressure_min(region) = pressure(index) + blast_water_pressure_max(region) = pressure(index) + else + if (pressure(index) < blast_water_pressure_min(region)) then + blast_water_pressure_min(region) = pressure(index) + end if + if (pressure(index) > blast_water_pressure_max(region)) then + blast_water_pressure_max(region) = pressure(index) + end if + end if + blast_water_count(region) = blast_water_count(region) + 1 + end subroutine sph_blast_accumulate_water + + subroutine sph_blast_accumulate_explosive(region, index) + integer, intent(in) :: region + integer, intent(in) :: index + + if (.not. allocated(explosive_reaction_progress)) return + blast_explosive_count(region) = blast_explosive_count(region) + 1 + blast_explosive_progress_mean(region) = & + blast_explosive_progress_mean(region) + & + explosive_reaction_progress(index) + end subroutine sph_blast_accumulate_explosive + + subroutine sph_blast_count_candidate_pairs(region) + integer, intent(in) :: region + integer :: source + integer :: target + + do source = 1, npart + if (.not. sph_blast_source_accepts(region, source)) cycle + do target = 1, npart + if (.not. sph_blast_target_accepts(region, target)) cycle + if (sph_blast_pair_within(region, source, target)) then + blast_candidate_pair_count(region) = & + blast_candidate_pair_count(region) + 1 + end if + end do + end do + end subroutine sph_blast_count_candidate_pairs + + logical function sph_blast_source_accepts(region, index) + integer, intent(in) :: region + integer, intent(in) :: index + + sph_blast_source_accepts = .false. + if (.not. sph_blast_phase_matches(blast_source_phase(region), & + particle_phase(index))) return + if (.not. sph_blast_is_active(index)) return + if (.not. sph_blast_in_region(region, index)) return + sph_blast_source_accepts = .true. + end function sph_blast_source_accepts + + logical function sph_blast_target_accepts(region, index) + integer, intent(in) :: region + integer, intent(in) :: index + + sph_blast_target_accepts = .false. + if (.not. sph_blast_phase_matches(blast_target_phase(region), & + particle_phase(index))) return + if (.not. sph_blast_is_active(index)) return + if (.not. sph_blast_particle_in_target_set(region, index)) return + if (.not. sph_blast_in_region(region, index)) return + sph_blast_target_accepts = .true. + end function sph_blast_target_accepts + + logical function sph_blast_phase_matches(filter_phase, db_phase) + integer, intent(in) :: filter_phase + integer, intent(in) :: db_phase + + sph_blast_phase_matches = .false. + if (filter_phase == SPH_BLAST_PHASE_ALL) then + sph_blast_phase_matches = .true. + else if (filter_phase == SPH_BLAST_PHASE_WATER) then + sph_blast_phase_matches = db_phase == SPH_PHASE_WATER + else if (filter_phase == SPH_BLAST_PHASE_GAS) then + sph_blast_phase_matches = db_phase == SPH_PHASE_GAS + else if (filter_phase == SPH_BLAST_PHASE_EXPLOSIVE) then + sph_blast_phase_matches = db_phase == SPH_PHASE_EXPLOSIVE + else if (filter_phase == SPH_BLAST_PHASE_SOLID) then + sph_blast_phase_matches = db_phase == SPH_PHASE_SOLID + else if (filter_phase == SPH_BLAST_PHASE_SHELL) then + sph_blast_phase_matches = db_phase == SPH_PHASE_SHELL + else if (filter_phase == SPH_BLAST_PHASE_STRUCTURE) then + sph_blast_phase_matches = db_phase == SPH_PHASE_SOLID .or. & + db_phase == SPH_PHASE_SHELL + end if + end function sph_blast_phase_matches + + logical function sph_blast_is_active(index) + integer, intent(in) :: index + + sph_blast_is_active = .false. + if (index < 1 .or. index > npart) return + if (particle_status(index) /= SPH_STATUS_ACTIVE) return + sph_blast_is_active = .true. + end function sph_blast_is_active + + logical function sph_blast_in_region(region, index) + integer, intent(in) :: region + integer, intent(in) :: index + real(rk) :: offset(SPH_NDIM) + real(rk) :: distance2 + real(rk) :: radius2 + + sph_blast_in_region = .false. + if (region < 1 .or. region > blast_region_count) return + if (index < 1 .or. index > npart) return + offset = x(:,index) - blast_center(:,region) + distance2 = sum(offset * offset) + radius2 = blast_radius(region) * blast_radius(region) + if (distance2 <= radius2) sph_blast_in_region = .true. + end function sph_blast_in_region + + logical function sph_blast_pair_within(region, source, target) + integer, intent(in) :: region + integer, intent(in) :: source + integer, intent(in) :: target + real(rk) :: offset(SPH_NDIM) + real(rk) :: distance2 + real(rk) :: radius2 + + sph_blast_pair_within = .false. + offset = x(:,target) - x(:,source) + distance2 = sum(offset * offset) + radius2 = blast_radius(region) * blast_radius(region) + if (distance2 <= radius2) sph_blast_pair_within = .true. + end function sph_blast_pair_within + + logical function sph_blast_particle_in_target_set(region, index) + integer, intent(in) :: region + integer, intent(in) :: index + integer :: set_index + + sph_blast_particle_in_target_set = .false. + if (trim(sph_blast_upper(blast_target_set_name(region))) == 'NONE') return + set_index = sph_db_find_nset(blast_target_set_name(region)) + if (set_index > 0) then + sph_blast_particle_in_target_set = & + sph_blast_nset_contains_particle(set_index, index) + return + end if + set_index = sph_db_find_elset(blast_target_set_name(region)) + if (set_index > 0) then + sph_blast_particle_in_target_set = & + sph_blast_elset_contains_particle(set_index, index) + end if + end function sph_blast_particle_in_target_set + + logical function sph_blast_nset_contains_particle(set_index, index) + integer, intent(in) :: set_index + integer, intent(in) :: index + integer :: member + + sph_blast_nset_contains_particle = .false. + do member = 1, nset_member_count(set_index) + if (nset_members(member,set_index) == particle_id_ext(index)) then + sph_blast_nset_contains_particle = .true. + return + end if + end do + end function sph_blast_nset_contains_particle + + logical function sph_blast_elset_contains_particle(set_index, index) + integer, intent(in) :: set_index + integer, intent(in) :: index + integer :: member + integer :: elem_index + integer :: conn + + sph_blast_elset_contains_particle = .false. + do member = 1, elset_member_count(set_index) + elem_index = sph_db_find_element(elset_members(member,set_index)) + if (elem_index <= 0) cycle + do conn = 1, element_conn_count(elem_index) + if (element_conn(conn,elem_index) == particle_id_ext(index)) then + sph_blast_elset_contains_particle = .true. + return + end if + end do + end do + end function sph_blast_elset_contains_particle + integer function sph_blast_phase_id(phase_name) character(len=*), intent(in) :: phase_name character(len=SPH_NAME_LEN) :: upper_name diff --git a/src/input/sph_input_reader.f90 b/src/input/sph_input_reader.f90 index 42b7ce4..489efbd 100644 --- a/src/input/sph_input_reader.f90 +++ b/src/input/sph_input_reader.f90 @@ -21,7 +21,7 @@ module sph_input_reader sph_explosive_set_control, sph_explosive_set_ignition, & sph_explosive_build, sph_explosive_count_unbound_materials use sph_blast, only: sph_blast_reset, sph_blast_add_region, & - sph_blast_count_unused_regions + sph_blast_count_unused_regions, sph_blast_refresh_snapshot use sph_solid, only: sph_solid_note_material, sph_solid_set_elastic, & sph_solid_set_johnson_cook, sph_solid_set_failure, & sph_solid_set_control, sph_solid_build_state, & @@ -136,6 +136,8 @@ contains if (ierr /= SPH_SUCCESS) return call sph_explosive_build(ierr) if (ierr /= SPH_SUCCESS) return + call sph_blast_refresh_snapshot(ierr) + if (ierr /= SPH_SUCCESS) return call sph_gas_count_unbound_materials(gas_unbound_count) input_warning_count = input_warning_count + gas_unbound_count call sph_explosive_count_unbound_materials(explosive_unbound_count) diff --git a/src/solver/sph_solver_explicit.f90 b/src/solver/sph_solver_explicit.f90 index 47ec0e4..411a955 100644 --- a/src/solver/sph_solver_explicit.f90 +++ b/src/solver/sph_solver_explicit.f90 @@ -12,6 +12,7 @@ module sph_solver_explicit sph_gas_compute_acceleration, sph_gas_update_diagnostics, & sph_gas_estimate_dt_limits, sph_gas_apply_sources_explicit use sph_explosive, only: sph_explosive_advance, sph_explosive_estimate_dt_limit + use sph_blast, only: sph_blast_refresh_snapshot use sph_solid_dynamics, only: sph_solid_compute_acceleration, & sph_solid_advance_material, sph_solid_update_diagnostics, & sph_solid_estimate_dt_limits @@ -52,6 +53,8 @@ contains if (ierr /= SPH_SUCCESS) return call sph_gas_update_pressure(ierr) if (ierr /= SPH_SUCCESS) return + call sph_blast_refresh_snapshot(ierr) + if (ierr /= SPH_SUCCESS) return call sph_water_compute_density_rate(ierr) if (ierr /= SPH_SUCCESS) return call sph_gas_compute_density_rate(ierr) @@ -70,6 +73,8 @@ contains end do call sph_explosive_advance(dt, current_time + dt, ierr) if (ierr /= SPH_SUCCESS) return + call sph_blast_refresh_snapshot(ierr) + if (ierr /= SPH_SUCCESS) return call sph_gas_apply_sources_explicit(dt, ierr) if (ierr /= SPH_SUCCESS) return call sph_solid_advance_material(dt, ierr) diff --git a/tests/module/test_blast_source_snapshot.f90 b/tests/module/test_blast_source_snapshot.f90 new file mode 100644 index 0000000..e5317f1 --- /dev/null +++ b/tests/module/test_blast_source_snapshot.f90 @@ -0,0 +1,107 @@ +program test_blast_source_snapshot + use sph_precision, only: rk + use sph_error, only: SPH_SUCCESS + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, & + sph_build_model, sph_advance_explicit + use sph_blast, only: blast_source_count, blast_target_count, & + blast_candidate_pair_count, blast_gas_count, blast_water_count, & + blast_explosive_count, blast_source_mass, blast_source_energy, & + blast_source_pressure_mean, blast_gas_volume, & + blast_explosive_progress_mean, blast_water_pressure_min, & + blast_water_pressure_max + implicit none + + integer :: ierr + + call check_initial_snapshot() + call check_no_source_defaults() + call check_explicit_refresh() + +contains + + subroutine check_initial_snapshot() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Blast source snapshot') + call sph_read_inp('tests/data/blast_water_gas_explosive_solid_070.inp', ierr) + call assert_ok(ierr, 'read failed for Blast source snapshot') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for Blast source snapshot') + if (blast_source_count(1) /= 1) call fail('Blast source count mismatch') + if (blast_target_count(1) /= 2) call fail('Blast target count mismatch') + if (blast_candidate_pair_count(1) /= 2) call fail('Blast pair count mismatch') + if (blast_gas_count(1) /= 1) call fail('Blast Gas count mismatch') + if (blast_water_count(1) /= 2) call fail('Blast Water count mismatch') + if (blast_explosive_count(1) /= 1) call fail('Blast Explosive count mismatch') + call assert_close(blast_source_mass(1), 0.02_rk, 'Blast source mass mismatch') + if (blast_source_energy(1) <= 0.0_rk) call fail('Blast source energy missing') + if (blast_source_pressure_mean(1) <= 0.0_rk) call fail('Blast source pressure missing') + call assert_close(blast_gas_volume(1), 1.25e-5_rk, 'Blast Gas volume mismatch') + call assert_close(blast_explosive_progress_mean(1), 0.0_rk, & + 'Blast initial explosive progress mismatch') + call assert_close(blast_water_pressure_min(1), 0.0_rk, & + 'Blast Water pressure min mismatch') + call assert_close(blast_water_pressure_max(1), 0.0_rk, & + 'Blast Water pressure max mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Blast source snapshot') + end subroutine check_initial_snapshot + + subroutine check_no_source_defaults() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for no-source Blast snapshot') + call sph_read_inp('tests/data/blast_solid_only_load_070.inp', ierr) + call assert_ok(ierr, 'read failed for no-source Blast snapshot') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for no-source Blast snapshot') + if (blast_source_count(1) /= 0) call fail('no-source count mismatch') + if (blast_target_count(1) /= 3) call fail('no-source target count mismatch') + if (blast_candidate_pair_count(1) /= 0) call fail('no-source pairs mismatch') + call assert_close(blast_source_mass(1), 0.0_rk, 'no-source mass mismatch') + call assert_close(blast_source_energy(1), 0.0_rk, 'no-source energy mismatch') + call assert_close(blast_source_pressure_mean(1), 0.0_rk, & + 'no-source pressure mismatch') + call assert_close(blast_gas_volume(1), 0.0_rk, 'no-source Gas volume mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for no-source Blast snapshot') + end subroutine check_no_source_defaults + + subroutine check_explicit_refresh() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Blast explicit refresh') + call sph_read_inp('tests/data/blast_water_gas_explosive_solid_070.inp', ierr) + call assert_ok(ierr, 'read failed for Blast explicit refresh') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for Blast explicit refresh') + call sph_advance_explicit(1.0e-7_rk, ierr) + call assert_ok(ierr, 'advance failed for Blast explicit refresh') + if (blast_explosive_progress_mean(1) < 0.99_rk) then + call fail('Blast snapshot was not refreshed after Explosive update') + end if + if (blast_source_count(1) /= 1) call fail('Blast source count changed') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Blast explicit refresh') + end subroutine check_explicit_refresh + + subroutine assert_ok(ierr_value, message) + integer, intent(in) :: ierr_value + character(len=*), intent(in) :: message + + if (ierr_value /= SPH_SUCCESS) call fail(message) + end subroutine assert_ok + + subroutine assert_close(actual, expected, message) + real(rk), intent(in) :: actual + real(rk), intent(in) :: expected + character(len=*), intent(in) :: message + + if (abs(actual - expected) > 1.0e-12_rk) call fail(message) + end subroutine assert_close + + subroutine fail(message) + character(len=*), intent(in) :: message + + write(*, '(A)') trim(message) + stop 1 + end subroutine fail + +end program test_blast_source_snapshot diff --git a/tests/module/test_blast_target_filter.f90 b/tests/module/test_blast_target_filter.f90 new file mode 100644 index 0000000..4a42589 --- /dev/null +++ b/tests/module/test_blast_target_filter.f90 @@ -0,0 +1,124 @@ +program test_blast_target_filter + use sph_precision, only: rk + use sph_error, only: SPH_SUCCESS + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model + use sph_blast, only: sph_blast_refresh_snapshot, blast_target_set_name, & + blast_radius, blast_source_count, blast_target_count, & + blast_candidate_pair_count, blast_skipped_source_count, & + blast_skipped_target_count, blast_region_enabled + implicit none + + integer :: ierr + + call check_target_counts('tests/data/blast_water_gas_explosive_solid_070.inp', 2, 2) + call check_target_counts('tests/data/blast_water_gas_explosive_shell_070.inp', 2, 2) + call check_target_counts('tests/data/blast_water_gas_explosive_solid_shell_070.inp', 4, 4) + call check_set_filter_skip() + call check_radius_filter_skip() + call check_shell_only_no_source() + call check_disabled_region_noop() + +contains + + subroutine check_target_counts(filename, expected_targets, expected_pairs) + character(len=*), intent(in) :: filename + integer, intent(in) :: expected_targets + integer, intent(in) :: expected_pairs + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Blast target filter') + call sph_read_inp(filename, ierr) + call assert_ok(ierr, 'read failed for Blast target filter') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for Blast target filter') + if (blast_source_count(1) /= 1) call fail('Blast target filter source mismatch') + if (blast_target_count(1) /= expected_targets) then + call fail('Blast target filter target mismatch') + end if + if (blast_candidate_pair_count(1) /= expected_pairs) then + call fail('Blast target filter pair mismatch') + end if + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Blast target filter') + end subroutine check_target_counts + + subroutine check_set_filter_skip() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Blast set filter') + call sph_read_inp('tests/data/blast_water_gas_explosive_solid_shell_070.inp', ierr) + call assert_ok(ierr, 'read failed for Blast set filter') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for Blast set filter') + blast_target_set_name(1) = 'SOLID_TARGET' + call sph_blast_refresh_snapshot(ierr) + call assert_ok(ierr, 'refresh failed for Blast set filter') + if (blast_target_count(1) /= 2) call fail('Blast set filter target mismatch') + if (blast_skipped_target_count(1) < 2) call fail('Blast set skip missing') + if (blast_candidate_pair_count(1) /= 2) call fail('Blast set pair mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Blast set filter') + end subroutine check_set_filter_skip + + subroutine check_radius_filter_skip() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Blast radius filter') + call sph_read_inp('tests/data/blast_water_gas_explosive_solid_shell_070.inp', ierr) + call assert_ok(ierr, 'read failed for Blast radius filter') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for Blast radius filter') + blast_radius(1) = 0.05_rk + call sph_blast_refresh_snapshot(ierr) + call assert_ok(ierr, 'refresh failed for Blast radius filter') + if (blast_source_count(1) /= 0) call fail('Blast radius source skip mismatch') + if (blast_target_count(1) /= 0) call fail('Blast radius target skip mismatch') + if (blast_skipped_source_count(1) < 1) call fail('Blast radius source skip missing') + if (blast_skipped_target_count(1) < 4) call fail('Blast radius target skip missing') + if (blast_candidate_pair_count(1) /= 0) call fail('Blast radius pair mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Blast radius filter') + end subroutine check_radius_filter_skip + + subroutine check_shell_only_no_source() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Shell-only Blast filter') + call sph_read_inp('tests/data/blast_shell_only_load_070.inp', ierr) + call assert_ok(ierr, 'read failed for Shell-only Blast filter') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for Shell-only Blast filter') + if (blast_source_count(1) /= 0) call fail('Shell-only source mismatch') + if (blast_target_count(1) /= 3) call fail('Shell-only target mismatch') + if (blast_candidate_pair_count(1) /= 0) call fail('Shell-only pair mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Shell-only Blast filter') + end subroutine check_shell_only_no_source + + subroutine check_disabled_region_noop() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for disabled Blast region') + call sph_read_inp('tests/data/blast_no_structure_smoke_070.inp', ierr) + call assert_ok(ierr, 'read failed for disabled Blast region') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for disabled Blast region') + if (blast_region_enabled(1)) call fail('disabled Blast region unexpectedly enabled') + if (blast_source_count(1) /= 0) call fail('disabled Blast source mismatch') + if (blast_target_count(1) /= 0) call fail('disabled Blast target mismatch') + if (blast_candidate_pair_count(1) /= 0) call fail('disabled Blast pair mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for disabled Blast region') + end subroutine check_disabled_region_noop + + subroutine assert_ok(ierr_value, message) + integer, intent(in) :: ierr_value + character(len=*), intent(in) :: message + + if (ierr_value /= SPH_SUCCESS) call fail(message) + end subroutine assert_ok + + subroutine fail(message) + character(len=*), intent(in) :: message + + write(*, '(A)') trim(message) + stop 1 + end subroutine fail + +end program test_blast_target_filter diff --git a/todo.md b/todo.md index 34258bd..c7b76e8 100644 --- a/todo.md +++ b/todo.md @@ -74,36 +74,36 @@ ## 阶段 C:局部爆炸源项快照和载荷候选筛选 -- [ ] 设计局部爆炸源项快照数据结构。 -- [ ] 设计源粒子到结构粒子的邻域筛选规则。 -- [ ] 设计载荷候选计数诊断。 -- [ ] 设计载荷源相质量诊断。 -- [ ] 设计载荷源相能量诊断。 -- [ ] 设计载荷源相平均压力诊断。 -- [ ] 设计局部爆炸区域内 Gas 体积诊断。 -- [ ] 设计局部爆炸区域内 Explosive 反应进度诊断。 -- [ ] 设计局部爆炸区域内 Water 压力范围诊断。 -- [ ] 设计局部爆炸区域内结构目标数量诊断。 -- [ ] 在显式步前刷新 Water 压力快照。 -- [ ] 在显式步前刷新 Gas 压力快照。 -- [ ] 在 Explosive 更新后刷新 Gas 源项贡献快照。 -- [ ] 在载荷构造前刷新结构目标 active 状态。 -- [ ] 对非 active 源粒子执行跳过规则。 -- [ ] 对 failed 结构粒子执行受控跳过或保留规则。 -- [ ] 对非目标 set 结构粒子执行跳过规则。 -- [ ] 对局部爆炸区域外源粒子执行跳过规则。 -- [ ] 对局部爆炸区域外目标粒子执行跳过规则。 -- [ ] 确认无源粒子时返回安全默认诊断。 -- [ ] 确认无目标粒子时返回安全默认诊断。 -- [ ] 确认 Water-only 模型不进入结构载荷路径。 -- [ ] 确认 Gas-only 模型不进入结构载荷路径。 -- [ ] 确认 Explosive-only 模型不进入结构载荷路径。 -- [ ] 确认 Solid-only 模型不读取 Gas 源项。 -- [ ] 确认 Shell-only 模型不读取 Gas 源项。 -- [ ] 新增 `blast_source_snapshot` 测试。 -- [ ] 新增 `blast_target_filter` 测试。 -- [ ] 将源项快照测试接入 CTest。 -- [ ] 准备阶段 C 本地提交说明。 +- [x] 设计局部爆炸源项快照数据结构。 +- [x] 设计源粒子到结构粒子的邻域筛选规则。 +- [x] 设计载荷候选计数诊断。 +- [x] 设计载荷源相质量诊断。 +- [x] 设计载荷源相能量诊断。 +- [x] 设计载荷源相平均压力诊断。 +- [x] 设计局部爆炸区域内 Gas 体积诊断。 +- [x] 设计局部爆炸区域内 Explosive 反应进度诊断。 +- [x] 设计局部爆炸区域内 Water 压力范围诊断。 +- [x] 设计局部爆炸区域内结构目标数量诊断。 +- [x] 在显式步前刷新 Water 压力快照。 +- [x] 在显式步前刷新 Gas 压力快照。 +- [x] 在 Explosive 更新后刷新 Gas 源项贡献快照。 +- [x] 在载荷构造前刷新结构目标 active 状态。 +- [x] 对非 active 源粒子执行跳过规则。 +- [x] 对 failed 结构粒子执行受控跳过或保留规则。 +- [x] 对非目标 set 结构粒子执行跳过规则。 +- [x] 对局部爆炸区域外源粒子执行跳过规则。 +- [x] 对局部爆炸区域外目标粒子执行跳过规则。 +- [x] 确认无源粒子时返回安全默认诊断。 +- [x] 确认无目标粒子时返回安全默认诊断。 +- [x] 确认 Water-only 模型不进入结构载荷路径。 +- [x] 确认 Gas-only 模型不进入结构载荷路径。 +- [x] 确认 Explosive-only 模型不进入结构载荷路径。 +- [x] 确认 Solid-only 模型不读取 Gas 源项。 +- [x] 确认 Shell-only 模型不读取 Gas 源项。 +- [x] 新增 `blast_source_snapshot` 测试。 +- [x] 新增 `blast_target_filter` 测试。 +- [x] 将源项快照测试接入 CTest。 +- [x] 准备阶段 C 本地提交说明。 ## 阶段 D:结构外载荷缓存、投影和守恒诊断