From ae5144fe86eb58aeb9b9994c1c3e273047141712 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 2 Jul 2026 21:32:29 +0800 Subject: [PATCH] Strengthen water coupling interface placeholders --- .../sph_coupling_interface.f90 | 40 +++++++++- tests/module/test_coupling_interface.f90 | 73 ++++++++++++++++++- todo.md | 40 +++++----- 3 files changed, 129 insertions(+), 24 deletions(-) diff --git a/src/coupling_interface/sph_coupling_interface.f90 b/src/coupling_interface/sph_coupling_interface.f90 index ec1fda6..22a4f7d 100644 --- a/src/coupling_interface/sph_coupling_interface.f90 +++ b/src/coupling_interface/sph_coupling_interface.f90 @@ -1,7 +1,8 @@ module sph_coupling_interface use sph_precision, only: rk, SPH_NDIM use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, sph_error_set - use sph_database, only: npart, particle_id_ext, x, v + use sph_database, only: npart, particle_id_ext, particle_phase, material_id, & + x, v, pressure, mass, acc_body implicit none integer :: coupling_region_count = 0 @@ -49,6 +50,37 @@ contains velocity(:) = v(:,index) end subroutine sph_coupling_export_particle + subroutine sph_coupling_export_particle_state(index, ext_id, phase, material, & + position, velocity, particle_pressure, ierr) + integer, intent(in) :: index + integer, intent(out) :: ext_id + integer, intent(out) :: phase + integer, intent(out) :: material + real(rk), intent(out) :: position(SPH_NDIM) + real(rk), intent(out) :: velocity(SPH_NDIM) + real(rk), intent(out) :: particle_pressure + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + ext_id = 0 + phase = 0 + material = 0 + position = 0.0_rk + velocity = 0.0_rk + particle_pressure = 0.0_rk + if (index < 1 .or. index > npart) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling particle index is out of range') + ierr = SPH_ERROR_ARGUMENT + return + end if + ext_id = particle_id_ext(index) + phase = particle_phase(index) + material = material_id(index) + position(:) = x(:,index) + velocity(:) = v(:,index) + particle_pressure = pressure(index) + end subroutine sph_coupling_export_particle_state + subroutine sph_coupling_get_facet_count(count, ierr) integer, intent(out) :: count integer, intent(out) :: ierr @@ -81,6 +113,12 @@ contains ierr = SPH_ERROR_ARGUMENT return end if + if (mass(index) <= 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling force particle mass must be positive') + ierr = SPH_ERROR_ARGUMENT + return + end if + acc_body(:,index) = acc_body(:,index) + force(:) / mass(index) coupling_work = coupling_work + sum(force) coupling_momentum(:) = coupling_momentum(:) + force(:) end subroutine sph_coupling_import_particle_force diff --git a/tests/module/test_coupling_interface.f90 b/tests/module/test_coupling_interface.f90 index e0bcd54..def9f00 100644 --- a/tests/module/test_coupling_interface.f90 +++ b/tests/module/test_coupling_interface.f90 @@ -1,14 +1,22 @@ program test_coupling_interface 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_database, only: SPH_PHASE_WATER + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model, & + sph_advance_explicit use sph_coupling_interface, only: sph_coupling_get_particle_count, & sph_coupling_export_particle, sph_coupling_get_facet_count, & sph_coupling_export_facet, sph_coupling_import_particle_force, & sph_coupling_import_facet_load, sph_coupling_get_diagnostics, & - coupling_type + sph_coupling_export_particle_state, coupling_type implicit none + call check_legacy_region() + call check_water_runtime_state() + +contains + + subroutine check_legacy_region() integer :: ierr integer :: count integer :: ext_id @@ -51,8 +59,61 @@ program test_coupling_interface call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed') + end subroutine check_legacy_region -contains + subroutine check_water_runtime_state() + integer :: ierr + integer :: count + integer :: ext_id + integer :: phase + integer :: material + real(rk) :: position(3) + real(rk) :: velocity(3) + real(rk) :: particle_pressure + real(rk) :: force(3) + real(rk) :: work + real(rk) :: energy + real(rk) :: momentum(3) + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize water failed') + call sph_read_inp('tests/data/water_minimal_020.inp', ierr) + call assert_ok(ierr, 'read water input failed') + call sph_build_model(ierr) + call assert_ok(ierr, 'build water model failed') + call sph_advance_explicit(1.0e-6_rk, ierr) + call assert_ok(ierr, 'advance water model failed') + + call sph_coupling_get_particle_count(count, ierr) + call assert_ok(ierr, 'water coupling particle count failed') + if (count /= 4) call fail('water coupling particle count mismatch') + + call sph_coupling_export_particle_state(1, ext_id, phase, material, & + position, velocity, particle_pressure, ierr) + call assert_ok(ierr, 'water coupling particle state export failed') + if (ext_id /= 1) call fail('water coupling export id mismatch') + if (phase /= SPH_PHASE_WATER) call fail('water coupling phase mismatch') + if (material <= 0) call fail('water coupling material mismatch') + + force = (/0.0_rk, 0.0_rk, 2.0_rk/) + call sph_coupling_import_particle_force(1, force, ierr) + call assert_ok(ierr, 'water coupling force import failed') + call sph_advance_explicit(1.0e-6_rk, ierr) + call assert_ok(ierr, 'advance after coupling force failed') + call sph_coupling_export_particle_state(1, ext_id, phase, material, & + position, velocity, particle_pressure, ierr) + call assert_ok(ierr, 'water coupling export after force failed') + if (velocity(3) <= 0.0_rk) call fail('coupling force did not affect explicit step') + if (.not. is_finite(particle_pressure)) call fail('water coupling pressure is not finite') + + call sph_coupling_get_diagnostics(work, energy, momentum) + if (work <= 0.0_rk) call fail('coupling work placeholder not updated') + if (momentum(3) <= 0.0_rk) call fail('coupling momentum placeholder not updated') + if (coupling_type /= 0) call fail('water coupling type should remain neutral') + + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize water failed') + end subroutine check_water_runtime_state subroutine assert_ok(ierr_value, message) integer, intent(in) :: ierr_value @@ -68,4 +129,10 @@ contains stop 1 end subroutine fail + logical function is_finite(value) + real(rk), intent(in) :: value + + is_finite = (value == value .and. abs(value) < huge(1.0_rk)) + end function is_finite + end program test_coupling_interface diff --git a/todo.md b/todo.md index d9ce1cd..05230d1 100644 --- a/todo.md +++ b/todo.md @@ -368,32 +368,32 @@ - [x] CLI 错误时打印最近错误消息。 - [x] 确认 CLI 不绕过公共生命周期接口。 - [x] 增加 0.2.0 CLI inspect 测试。 -- [ ] 增加 0.2.0 CLI check 测试。 -- [ ] 增加 0.2.0 CLI run 测试。 -- [ ] 增加 0.2.0 CLI output 测试。 -- [ ] 增加 0.2.0 CLI 错误输入测试。 +- [x] 增加 0.2.0 CLI check 测试。 +- [x] 增加 0.2.0 CLI run 测试。 +- [x] 增加 0.2.0 CLI output 测试。 +- [x] 增加 0.2.0 CLI 错误输入测试。 ## 阶段 P:CouplingInterface 最小加固 -- [ ] 确认 Water 运行后 coupling particle count 可用。 -- [ ] 确认 Water 运行后 coupling particle 导出可用。 -- [ ] 确认 coupling particle 导出包含 pressure。 -- [ ] 确认 coupling particle 导出包含 phase。 -- [ ] 确认 coupling particle 导出包含 material。 -- [ ] 确认外部粒子力导入接口仍可用。 -- [ ] 将外部粒子力导入接入显式步外载占位。 -- [ ] 继续记录 coupling work 占位。 -- [ ] 继续记录 coupling momentum 占位。 -- [ ] 继续记录 coupling energy 占位。 -- [ ] 保持接触耦合类型字段。 -- [ ] 保持连续体耦合类型字段。 -- [ ] 确认接口命名不出现 FEMLite 内部结构。 -- [ ] 确认默认 gate 不运行真实 FEM-SPH 联合测试。 -- [ ] 扩展 coupling_interface 测试覆盖 Water 运行后状态。 +- [x] 确认 Water 运行后 coupling particle count 可用。 +- [x] 确认 Water 运行后 coupling particle 导出可用。 +- [x] 确认 coupling particle 导出包含 pressure。 +- [x] 确认 coupling particle 导出包含 phase。 +- [x] 确认 coupling particle 导出包含 material。 +- [x] 确认外部粒子力导入接口仍可用。 +- [x] 将外部粒子力导入接入显式步外载占位。 +- [x] 继续记录 coupling work 占位。 +- [x] 继续记录 coupling momentum 占位。 +- [x] 继续记录 coupling energy 占位。 +- [x] 保持接触耦合类型字段。 +- [x] 保持连续体耦合类型字段。 +- [x] 确认接口命名不出现 FEMLite 内部结构。 +- [x] 确认默认 gate 不运行真实 FEM-SPH 联合测试。 +- [x] 扩展 coupling_interface 测试覆盖 Water 运行后状态。 ## 阶段 Q:Pressure Wave Smoke -- [ ] 确定 pressure wave smoke 的最小粒子布置。 +- [x] 确定 pressure wave smoke 的最小粒子布置。 - [ ] 确定 pressure wave smoke 的初始压力扰动。 - [ ] 确定 pressure wave smoke 的运行步数。 - [ ] 确定 pressure wave smoke 的 checker 阈值。