Add coupling output restart and APIs

This commit is contained in:
Codex
2026-07-04 09:59:52 +08:00
parent 5be6b178bc
commit 5d9933d014
22 changed files with 2636 additions and 89 deletions
+199
View File
@@ -0,0 +1,199 @@
program test_coupling_output_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_set_output_directory, sph_write_output
use sph_coupling_interface, only: sph_coupling_apply_particle_forces
implicit none
integer :: ierr
call check_coupling_output_fields()
contains
subroutine check_coupling_output_fields()
integer :: indices(1)
real(rk) :: forces(SPH_NDIM,1)
call delete_output_pair(80)
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for coupling output')
call sph_read_inp('tests/data/coupling_shell_region_080.inp', ierr)
call assert_ok(ierr, 'read failed for coupling output')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for coupling output')
indices(1) = 1
forces(:,1) = (/3.0_rk, 4.0_rk, 5.0_rk/)
call sph_coupling_apply_particle_forces(2, 1, indices, forces, ierr)
call assert_ok(ierr, 'coupling force import failed for output')
call sph_set_output_directory('build-codex-unix', ierr)
call assert_ok(ierr, 'coupling output directory failed')
call sph_write_output(80, 0.0_rk, ierr)
call assert_ok(ierr, 'coupling output write failed')
call check_particle_csv('build-codex-unix/sphlite_00000080.csv')
call check_summary_csv('build-codex-unix/sphlite_00000080_summary.csv')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for coupling output')
end subroutine check_coupling_output_fields
subroutine check_particle_csv(filename)
character(len=*), intent(in) :: filename
character(len=1024) :: header
integer :: unit
integer :: ios
integer :: id
integer :: phase
integer :: status
integer :: mat
integer :: sec
integer :: failure
integer :: reference_neighbors
integer :: correction_status
integer :: motion_flag
integer :: region_id
real(rk) :: pos(3)
real(rk) :: vel(3)
real(rk) :: scalar_values(4)
real(rk) :: explosive_values(6)
real(rk) :: stress_values(6)
real(rk) :: solid_values(5)
real(rk) :: blast_values(9)
real(rk) :: coupling_force(3)
unit = 84
open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('coupling particle CSV open failed')
read(unit, '(A)', iostat=ios) header
if (ios /= 0) call fail('coupling particle CSV header read failed')
if (index(header, 'coupling_force_x,coupling_force_y,coupling_force_z') <= 0) then
call fail('coupling force particle CSV columns missing')
end if
if (index(header, 'coupling_motion_flag,coupling_region_id') <= 0) then
call fail('coupling motion particle CSV columns missing')
end if
read(unit, *, iostat=ios) id, phase, status, mat, sec, pos, vel, &
scalar_values, explosive_values, stress_values, solid_values, &
blast_values, coupling_force, failure, reference_neighbors, &
correction_status, motion_flag, region_id
close(unit=unit)
if (ios /= 0) call fail('coupling particle CSV row read failed')
if (id /= 1) call fail('coupling output id mismatch')
if (maxval(abs(coupling_force - (/3.0_rk, 4.0_rk, 5.0_rk/))) > &
1.0e-12_rk) call fail('coupling output force mismatch')
if (region_id <= 0) call fail('coupling output region id missing')
if (motion_flag /= 0) call fail('coupling output motion flag default mismatch')
end subroutine check_particle_csv
subroutine check_summary_csv(filename)
character(len=*), intent(in) :: filename
call assert_summary_int(filename, 'coupling_regions', 2)
call assert_summary_int(filename, 'coupling_particles', 3)
call assert_summary_int(filename, 'coupling_facets', 6)
call assert_summary_present(filename, 'coupling_force_x')
call assert_summary_present(filename, 'coupling_force_y')
call assert_summary_present(filename, 'coupling_force_z')
call assert_summary_present(filename, 'coupling_impulse_x')
call assert_summary_present(filename, 'coupling_impulse_y')
call assert_summary_present(filename, 'coupling_impulse_z')
call assert_summary_present(filename, 'coupling_work')
call assert_summary_present(filename, 'coupling_energy_exchange')
call assert_summary_int(filename, 'coupling_nonfinite_imports', 0)
call assert_summary_int(filename, 'coupling_stale_loads', 0)
end subroutine check_summary_csv
subroutine assert_summary_present(filename, metric)
character(len=*), intent(in) :: filename
character(len=*), intent(in) :: metric
character(len=256) :: line
integer :: unit
integer :: ios
logical :: found
found = .false.
unit = 85
open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('coupling summary CSV open failed')
do
read(unit, '(A)', iostat=ios) line
if (ios /= 0) exit
if (index(line, trim(metric) // ',') == 1) found = .true.
end do
close(unit=unit)
if (.not. found) call fail('coupling summary metric missing')
end subroutine assert_summary_present
subroutine assert_summary_int(filename, metric, expected)
character(len=*), intent(in) :: filename
character(len=*), intent(in) :: metric
integer, intent(in) :: expected
integer :: actual
call read_summary_int(filename, metric, actual)
if (actual /= expected) call fail('coupling summary integer mismatch')
end subroutine assert_summary_int
subroutine read_summary_int(filename, metric, value)
character(len=*), intent(in) :: filename
character(len=*), intent(in) :: metric
integer, intent(out) :: value
character(len=256) :: line
integer :: unit
integer :: ios
integer :: start
unit = 86
open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('coupling summary integer open failed')
do
read(unit, '(A)', iostat=ios) line
if (ios /= 0) exit
if (index(line, trim(metric) // ',') == 1) then
start = len_trim(metric) + 2
read(line(start:), *, iostat=ios) value
close(unit=unit)
if (ios /= 0) call fail('coupling summary integer parse failed')
return
end if
end do
close(unit=unit)
call fail('coupling summary integer missing')
end subroutine read_summary_int
subroutine delete_output_pair(step_value)
integer, intent(in) :: step_value
character(len=256) :: filename
write(filename, '("build-codex-unix/sphlite_",I8.8,".csv")') step_value
call delete_file_if_exists(filename)
write(filename, '("build-codex-unix/sphlite_",I8.8,"_summary.csv")') step_value
call delete_file_if_exists(filename)
end subroutine delete_output_pair
subroutine delete_file_if_exists(filename)
character(len=*), intent(in) :: filename
logical :: exists
integer :: ios
inquire(file=trim(filename), exist=exists)
if (.not. exists) return
open(unit=87, file=trim(filename), status='old', iostat=ios)
if (ios == 0) close(unit=87, status='delete')
end subroutine delete_file_if_exists
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_coupling_output_080