Files
SPHLite/tests/module/test_coupling_output_081.f90
T
2026-07-04 11:43:00 +08:00

235 lines
8.5 KiB
Fortran

program test_coupling_output_081
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_database, only: SPH_COUPLING_DISTRIBUTION_AREA
use sph_coupling_interface, only: sph_coupling_apply_facet_pressures, &
sph_coupling_apply_contact_state
implicit none
integer :: ierr
call check_coupling_output_081()
contains
subroutine check_coupling_output_081()
integer :: ids(1)
real(rk) :: pressures(1)
real(rk) :: normals(SPH_NDIM,1)
real(rk) :: gaps(1)
real(rk) :: penetrations(1)
real(rk) :: contact_pressures(1)
call delete_output_pair(81)
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for coupling output 081')
call sph_read_inp('tests/data/coupling_load_mode_081.inp', ierr)
call assert_ok(ierr, 'read failed for coupling output 081')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for coupling output 081')
ids(1) = 1
pressures(1) = 12.0_rk
normals(:,1) = (/0.0_rk, 0.0_rk, 1.0_rk/)
gaps(1) = 1.0e-4_rk
penetrations(1) = 2.0e-3_rk
contact_pressures(1) = 9.0_rk
call sph_coupling_apply_facet_pressures(1, 1, ids, pressures, &
SPH_COUPLING_DISTRIBUTION_AREA, 81, ierr)
call assert_ok(ierr, 'pressure import failed for output 081')
call sph_coupling_apply_contact_state(1, 1, ids, normals, gaps, &
penetrations, contact_pressures, 81, ierr)
call assert_ok(ierr, 'contact import failed for output 081')
call sph_set_output_directory('build-codex-unix', ierr)
call assert_ok(ierr, 'output directory failed for output 081')
call sph_write_output(81, 0.0_rk, ierr)
call assert_ok(ierr, 'write failed for output 081')
call check_particle_csv('build-codex-unix/sphlite_00000081.csv')
call check_summary_csv('build-codex-unix/sphlite_00000081_summary.csv')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for coupling output 081')
end subroutine check_coupling_output_081
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 :: trailing_values(5)
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)
real(rk) :: coupling_gap
real(rk) :: coupling_penetration
integer :: contact_flag
unit = 84
open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('coupling output 081 CSV open failed')
read(unit, '(A)', iostat=ios) header
if (ios /= 0) call fail('coupling output 081 header read failed')
if (index(header, 'coupling_gap,coupling_penetration,coupling_contact_flag') <= 0) then
call fail('coupling output 081 contact 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, coupling_gap, coupling_penetration, contact_flag, &
trailing_values
close(unit=unit)
if (ios /= 0) call fail('coupling output 081 row read failed')
if (id /= 1) call fail('coupling output 081 id mismatch')
if (contact_flag /= 1) call fail('coupling output 081 contact flag mismatch')
call assert_close(coupling_gap, 1.0e-4_rk, 'coupling output 081 gap mismatch')
call assert_close(coupling_penetration, 2.0e-3_rk, &
'coupling output 081 penetration mismatch')
end subroutine check_particle_csv
subroutine check_summary_csv(filename)
character(len=*), intent(in) :: filename
call assert_summary_int(filename, 'coupling_active_contacts', 1)
call assert_summary_int(filename, 'coupling_skipped_contacts', 0)
call assert_summary_real(filename, 'coupling_max_pressure', 12.0_rk)
call assert_summary_real(filename, 'coupling_max_penetration', 2.0e-3_rk)
call assert_summary_present(filename, 'coupling_force_balance_error')
call assert_summary_present(filename, 'coupling_moment_balance_error')
end subroutine check_summary_csv
subroutine assert_summary_present(filename, metric)
character(len=*), intent(in) :: filename
character(len=*), intent(in) :: metric
real(rk) :: value
call read_summary_real(filename, metric, value)
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 :: value
call read_summary_int(filename, metric, value)
if (value /= expected) call fail('coupling output 081 summary int mismatch')
end subroutine assert_summary_int
subroutine assert_summary_real(filename, metric, expected)
character(len=*), intent(in) :: filename
character(len=*), intent(in) :: metric
real(rk), intent(in) :: expected
real(rk) :: value
call read_summary_real(filename, metric, value)
call assert_close(value, expected, 'coupling output 081 summary real mismatch')
end subroutine assert_summary_real
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
unit = 85
open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('coupling output 081 summary open failed')
do
read(unit, '(A)', iostat=ios) line
if (ios /= 0) exit
if (index(line, trim(metric) // ',') == 1) then
read(line(len_trim(metric)+2:), *, iostat=ios) value
close(unit=unit)
if (ios /= 0) call fail('coupling output 081 summary int parse failed')
return
end if
end do
close(unit=unit)
call fail('coupling output 081 summary int missing')
end subroutine read_summary_int
subroutine read_summary_real(filename, metric, value)
character(len=*), intent(in) :: filename
character(len=*), intent(in) :: metric
real(rk), intent(out) :: value
character(len=256) :: line
integer :: unit
integer :: ios
unit = 86
open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('coupling output 081 summary real open failed')
do
read(unit, '(A)', iostat=ios) line
if (ios /= 0) exit
if (index(line, trim(metric) // ',') == 1) then
read(line(len_trim(metric)+2:), *, iostat=ios) value
close(unit=unit)
if (ios /= 0) call fail('coupling output 081 summary real parse failed')
return
end if
end do
close(unit=unit)
call fail('coupling output 081 summary real missing')
end subroutine read_summary_real
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 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 fail(message)
character(len=*), intent(in) :: message
write(*, '(A)') trim(message)
stop 1
end subroutine fail
end program test_coupling_output_081