Files
SPHLite/tests/module/test_blast_output.f90
T
2026-07-04 12:06:47 +08:00

221 lines
7.6 KiB
Fortran

program test_blast_output
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, sph_set_output_directory, &
sph_write_output
implicit none
integer :: ierr
call delete_output_pair(70)
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for Blast output')
call sph_read_inp('tests/data/blast_water_gas_explosive_solid_070.inp', ierr)
call assert_ok(ierr, 'read failed for Blast output')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for Blast output')
call sph_advance_explicit(1.0e-7_rk, ierr)
call assert_ok(ierr, 'advance failed for Blast output')
call sph_set_output_directory('build-codex-unix', ierr)
call assert_ok(ierr, 'set output directory failed for Blast output')
call sph_write_output(70, 1.0e-7_rk, ierr)
call assert_ok(ierr, 'write failed for Blast output')
call check_particle_csv('build-codex-unix/sphlite_00000070.csv')
call check_summary_csv('build-codex-unix/sphlite_00000070_summary.csv')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for Blast output')
contains
subroutine check_particle_csv(filename)
character(len=*), intent(in) :: filename
character(len=512) :: header
integer :: unit
integer :: ios
unit = 94
open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('Blast particle CSV open failed')
read(unit, '(A)', iostat=ios) header
if (ios /= 0) call fail('Blast particle CSV header missing')
if (index(header, 'blast_solid_fx,blast_solid_fy,blast_solid_fz') <= 0) then
call fail('Blast particle CSV Solid force columns missing')
end if
call check_particle_rows(unit)
close(unit=unit)
end subroutine check_particle_csv
subroutine check_particle_rows(unit)
integer, intent(in) :: unit
integer :: ios
integer :: id
real(rk) :: blast_values(9)
do
call read_particle_row(unit, id, blast_values, ios)
if (ios /= 0) exit
if (id == 5) then
if (blast_values(1) <= 0.0_rk) call fail('Blast Solid force not output')
if (maxval(abs(blast_values(4:9))) > 0.0_rk) then
call fail('Blast Solid row polluted Shell load columns')
end if
return
end if
end do
call fail('Blast output target row missing')
end subroutine check_particle_rows
subroutine read_particle_row(unit, id, blast_values, ios)
integer, intent(in) :: unit
integer, intent(out) :: id
real(rk), intent(out) :: blast_values(9)
integer, intent(out) :: ios
integer :: phase
integer :: status
integer :: mat
integer :: sec
integer :: failure
integer :: reference_neighbors
integer :: correction_status
integer :: coupling_contact_flag
integer :: coupling_motion_flag
integer :: coupling_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) :: coupling_force(3)
real(rk) :: coupling_contact_values(2)
read(unit, *, iostat=ios) id, phase, status, mat, sec, pos, vel, &
scalar_values, explosive_values, stress_values, solid_values, &
blast_values, coupling_force, coupling_contact_values, &
coupling_contact_flag, failure, reference_neighbors, &
correction_status, coupling_motion_flag, coupling_region_id
end subroutine read_particle_row
subroutine check_summary_csv(filename)
character(len=*), intent(in) :: filename
call assert_summary_int(filename, 'blast_regions', 1)
call assert_summary_int(filename, 'blast_source_count_1', 1)
call assert_summary_int(filename, 'blast_load_targets_1', 2)
call assert_summary_positive(filename, 'blast_solid_force_x_1')
call assert_summary_positive(filename, 'blast_total_force_x_1')
call assert_summary_positive(filename, 'blast_energy_input_1')
call assert_summary_positive(filename, 'blast_max_acceleration_1')
end subroutine check_summary_csv
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('Blast summary integer mismatch')
end subroutine assert_summary_int
subroutine assert_summary_positive(filename, metric)
character(len=*), intent(in) :: filename
character(len=*), intent(in) :: metric
real(rk) :: value
call read_summary_real(filename, metric, value)
if (value <= 0.0_rk) call fail('Blast summary positive metric missing')
end subroutine assert_summary_positive
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 = 95
open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('Blast 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('Blast summary integer parse failed')
return
end if
end do
close(unit=unit)
call fail('Blast summary integer 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
integer :: start
unit = 96
open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('Blast summary real 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('Blast summary real parse failed')
return
end if
end do
close(unit=unit)
call fail('Blast 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=97, file=trim(filename), status='old', iostat=ios)
if (ios == 0) close(unit=97, 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_blast_output