Apply coupling imports in explicit step

This commit is contained in:
Codex
2026-07-04 09:22:46 +08:00
parent 788508a465
commit 5be6b178bc
11 changed files with 1195 additions and 46 deletions
@@ -0,0 +1,236 @@
program test_coupling_diagnostics_080
use sph_precision, only: rk, SPH_NDIM
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_database, only: x, v, mass
use sph_coupling_interface, only: coupling_external_force, &
coupling_motion_flag, SPH_COUPLING_MOTION_DIRECT, &
sph_coupling_import_force_by_region_index, &
sph_coupling_import_force_by_external_id, &
sph_coupling_import_motion_velocity, &
sph_coupling_import_motion_position, &
sph_coupling_get_extended_diagnostics
implicit none
integer :: ierr
call check_structural_diagnostics()
call check_water_force_path()
call check_gas_force_path()
contains
subroutine check_structural_diagnostics()
real(rk), parameter :: dt = 1.0e-7_rk
real(rk) :: force(SPH_NDIM)
real(rk) :: target_velocity(SPH_NDIM)
real(rk) :: target_position(SPH_NDIM)
real(rk) :: total_force(SPH_NDIM)
real(rk) :: total_moment(SPH_NDIM)
real(rk) :: current_impulse(SPH_NDIM)
real(rk) :: cumulative_impulse(SPH_NDIM)
real(rk) :: momentum_exchange(SPH_NDIM)
real(rk) :: current_work
real(rk) :: cumulative_work
real(rk) :: energy_exchange
real(rk) :: max_force
real(rk) :: max_acceleration
real(rk) :: max_velocity_error
real(rk) :: max_position_error
integer :: stale_count
integer :: skipped_count
integer :: nonfinite_count
integer :: imported_force_count
integer :: step_load_consumed
call build_contact_case()
v(:,2) = (/0.0_rk, 0.0_rk, 0.5_rk/)
force = (/0.0_rk, 0.0_rk, 10.0_rk/)
call sph_coupling_import_force_by_region_index('CONTACT_PARTICLES', 2, &
force, ierr)
call assert_ok(ierr, 'diagnostic force import failed')
target_velocity = (/0.0_rk, 1.0_rk, 0.0_rk/)
call sph_coupling_import_motion_velocity('CONTACT_PARTICLES', 1, &
SPH_COUPLING_MOTION_DIRECT, target_velocity, ierr)
call assert_ok(ierr, 'diagnostic velocity import failed')
target_position = (/0.0_rk, 0.0_rk, 0.2_rk/)
call sph_coupling_import_motion_position('CONTACT_PARTICLES', 3, &
SPH_COUPLING_MOTION_DIRECT, target_position, ierr)
call assert_ok(ierr, 'diagnostic position import failed')
call sph_advance_explicit(dt, ierr)
call assert_ok(ierr, 'diagnostic advance failed')
call sph_coupling_get_extended_diagnostics(total_force, total_moment, &
current_impulse, cumulative_impulse, current_work, cumulative_work, &
momentum_exchange, energy_exchange, max_force, max_acceleration, &
max_velocity_error, max_position_error, stale_count, skipped_count, &
nonfinite_count, imported_force_count, step_load_consumed, ierr)
call assert_ok(ierr, 'extended diagnostics failed')
call assert_vector_close(total_force, force, 'current force mismatch')
call assert_close(total_moment(2), -1.0_rk, 'current moment mismatch')
call assert_vector_close(current_impulse, force * dt, &
'current impulse mismatch')
call assert_vector_close(cumulative_impulse, force * dt, &
'cumulative impulse mismatch')
call assert_vector_close(momentum_exchange, force * dt, &
'momentum exchange mismatch')
if (current_work <= 0.0_rk) call fail('current work was not positive')
call assert_close(cumulative_work, current_work, 'cumulative work mismatch')
call assert_close(energy_exchange, current_work, 'energy exchange mismatch')
if (max_force <= 0.0_rk) call fail('max force diagnostic missing')
if (max_acceleration <= 0.0_rk) call fail('max acceleration diagnostic missing')
if (max_velocity_error <= 0.0_rk) call fail('velocity error diagnostic missing')
if (max_position_error <= 0.0_rk) call fail('position error diagnostic missing')
if (stale_count /= 0) call fail('unexpected stale count')
if (skipped_count /= 0) call fail('unexpected skipped count')
if (nonfinite_count /= 0) call fail('unexpected nonfinite count')
if (step_load_consumed /= 1) call fail('step load was not consumed')
if (maxval(abs(coupling_external_force)) > 0.0_rk) then
call fail('force cache was not consumed')
end if
if (maxval(coupling_motion_flag) /= 0) then
call fail('motion cache was not consumed')
end if
call assert_vector_close(v(:,1), target_velocity, &
'target velocity was not enforced')
call assert_vector_close(x(:,3), target_position, &
'target position was not enforced')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed after structural diagnostics')
end subroutine check_structural_diagnostics
subroutine check_water_force_path()
real(rk) :: force(SPH_NDIM)
real(rk) :: total_force(SPH_NDIM)
real(rk) :: total_moment(SPH_NDIM)
real(rk) :: current_impulse(SPH_NDIM)
real(rk) :: cumulative_impulse(SPH_NDIM)
real(rk) :: momentum_exchange(SPH_NDIM)
real(rk) :: current_work
real(rk) :: cumulative_work
real(rk) :: energy_exchange
real(rk) :: max_force
real(rk) :: max_acceleration
real(rk) :: max_velocity_error
real(rk) :: max_position_error
integer :: stale_count
integer :: skipped_count
integer :: nonfinite_count
integer :: imported_force_count
integer :: step_load_consumed
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for Water force path')
call sph_read_inp('tests/data/water_minimal_021.inp', ierr)
call assert_ok(ierr, 'read failed for Water force path')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for Water force path')
force = (/0.0_rk, 0.0_rk, mass(1)/1.0e-6_rk/)
call sph_coupling_import_force_by_external_id(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, 'Water force advance failed')
if (v(3,1) <= 0.0_rk) call fail('Water coupling force did not move particle')
call sph_coupling_get_extended_diagnostics(total_force, total_moment, &
current_impulse, cumulative_impulse, current_work, cumulative_work, &
momentum_exchange, energy_exchange, max_force, max_acceleration, &
max_velocity_error, max_position_error, stale_count, skipped_count, &
nonfinite_count, imported_force_count, step_load_consumed, ierr)
call assert_ok(ierr, 'Water diagnostics failed')
call assert_close(total_force(3), force(3), 'Water force diagnostic mismatch')
if (step_load_consumed /= 1) call fail('Water coupling load not consumed')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for Water force path')
end subroutine check_water_force_path
subroutine check_gas_force_path()
real(rk) :: force(SPH_NDIM)
real(rk) :: total_force(SPH_NDIM)
real(rk) :: total_moment(SPH_NDIM)
real(rk) :: current_impulse(SPH_NDIM)
real(rk) :: cumulative_impulse(SPH_NDIM)
real(rk) :: momentum_exchange(SPH_NDIM)
real(rk) :: current_work
real(rk) :: cumulative_work
real(rk) :: energy_exchange
real(rk) :: max_force
real(rk) :: max_acceleration
real(rk) :: max_velocity_error
real(rk) :: max_position_error
integer :: stale_count
integer :: skipped_count
integer :: nonfinite_count
integer :: imported_force_count
integer :: step_load_consumed
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for Gas force path')
call sph_read_inp('tests/data/gas_air_ideal_031.inp', ierr)
call assert_ok(ierr, 'read failed for Gas force path')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for Gas force path')
force = (/0.0_rk, 0.0_rk, mass(1)/1.0e-6_rk/)
call sph_coupling_import_force_by_external_id(1, force, ierr)
call assert_ok(ierr, 'Gas coupling force import failed')
call sph_advance_explicit(1.0e-6_rk, ierr)
call assert_ok(ierr, 'Gas force advance failed')
if (v(3,1) <= 0.0_rk) call fail('Gas coupling force did not move particle')
call sph_coupling_get_extended_diagnostics(total_force, total_moment, &
current_impulse, cumulative_impulse, current_work, cumulative_work, &
momentum_exchange, energy_exchange, max_force, max_acceleration, &
max_velocity_error, max_position_error, stale_count, skipped_count, &
nonfinite_count, imported_force_count, step_load_consumed, ierr)
call assert_ok(ierr, 'Gas diagnostics failed')
call assert_close(total_force(3), force(3), 'Gas force diagnostic mismatch')
if (step_load_consumed /= 1) call fail('Gas coupling load not consumed')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for Gas force path')
end subroutine check_gas_force_path
subroutine build_contact_case()
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for diagnostics')
call sph_read_inp('tests/data/coupling_region_080.inp', ierr)
call assert_ok(ierr, 'read failed for diagnostics')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for diagnostics')
end subroutine build_contact_case
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) > max(1.0e-10_rk, abs(expected) * 1.0e-8_rk)) then
call fail(message)
end if
end subroutine assert_close
subroutine assert_vector_close(actual, expected, message)
real(rk), intent(in) :: actual(SPH_NDIM)
real(rk), intent(in) :: expected(SPH_NDIM)
character(len=*), intent(in) :: message
integer :: i
do i = 1, SPH_NDIM
call assert_close(actual(i), expected(i), message)
end do
end subroutine assert_vector_close
subroutine fail(message)
character(len=*), intent(in) :: message
write(*, '(A)') trim(message)
stop 1
end subroutine fail
end program test_coupling_diagnostics_080