Files
SPHLite/tests/module/test_solid_output.f90
T
2026-07-04 09:59:52 +08:00

320 lines
13 KiB
Fortran

program test_solid_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_estimate_stable_dt, sph_set_output_directory, sph_write_output
use sph_database, only: v, a, solid_particle_count, solid_failed_count, &
solid_dtcrit, SPH_STATUS_FAILED
use sph_solid, only: sph_solid_mark_particle_failed
use sph_solid_state, only: solid_stress, solid_plastic_strain, solid_damage, &
solid_softening, solid_internal_energy, solid_strain_rate_eq, &
solid_reference_neighbor_count, solid_correction_status, &
solid_failure_status
use sph_solid_dynamics, only: sph_solid_update_diagnostics
implicit none
integer :: ierr
call check_solid_output_fields()
contains
subroutine check_solid_output_fields()
real(rk) :: dtcrit
call delete_output_pair(50)
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for Solid output')
call sph_read_inp('tests/data/solid_body_force_050.inp', ierr)
call assert_ok(ierr, 'read failed for Solid output')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for Solid output')
call seed_solid_state()
call sph_solid_mark_particle_failed(2, ierr)
call assert_ok(ierr, 'mark Solid failed particle failed')
call sph_estimate_stable_dt(dtcrit, ierr)
call assert_ok(ierr, 'Solid output dt estimate failed')
call sph_solid_update_diagnostics(ierr)
call assert_ok(ierr, 'Solid output diagnostics failed')
call sph_set_output_directory('build-codex-unix', ierr)
call assert_ok(ierr, 'Solid output directory failed')
call sph_write_output(50, 0.0_rk, ierr)
call assert_ok(ierr, 'Solid output write failed')
call check_particle_csv('build-codex-unix/sphlite_00000050.csv')
call check_summary_csv('build-codex-unix/sphlite_00000050_summary.csv')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for Solid output')
end subroutine check_solid_output_fields
subroutine seed_solid_state()
v(:,1) = (/2.0_rk, 3.0_rk, 4.0_rk/)
solid_stress(:,1) = (/1.0_rk, 2.0_rk, 3.0_rk, &
4.0_rk, 5.0_rk, 6.0_rk/)
solid_plastic_strain(1) = 0.25_rk
solid_damage(1) = 0.40_rk
solid_softening(1) = 0.60_rk
solid_internal_energy(1) = 2.0_rk
solid_strain_rate_eq(1) = 1.25_rk
solid_reference_neighbor_count(1) = 5
solid_correction_status(1) = 2
solid_failure_status(1) = 0
end subroutine seed_solid_state
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
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) :: plastic
real(rk) :: damage
real(rk) :: softening
real(rk) :: strain_rate_eq
real(rk) :: acceleration_norm
real(rk) :: blast_force_values(9)
real(rk) :: coupling_force_values(3)
integer :: reference_neighbors
integer :: correction_status
integer :: motion_flag
integer :: region_id
unit = 84
open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('Solid particle CSV open failed')
read(unit, '(A)', iostat=ios) header
if (ios /= 0) call fail('Solid particle CSV header read failed')
if (trim(header) /= 'id,phase,status,material,section,x,y,z,' // &
'vx,vy,vz,rho,pressure,temperature,h,explosive_detonation_time,' // &
'explosive_lambda,explosive_initial_mass,explosive_remaining_mass,' // &
'explosive_released_mass,explosive_released_energy,' // &
'solid_stress_xx,solid_stress_yy,solid_stress_zz,solid_stress_xy,' // &
'solid_stress_yz,solid_stress_zx,solid_plastic_strain,solid_damage,' // &
'solid_softening,solid_strain_rate_eq,solid_acceleration_norm,' // &
'blast_solid_fx,blast_solid_fy,blast_solid_fz,' // &
'blast_shell_fx,blast_shell_fy,blast_shell_fz,' // &
'blast_shell_mx,blast_shell_my,blast_shell_mz,' // &
'coupling_force_x,coupling_force_y,coupling_force_z,' // &
'solid_failure_status,solid_reference_neighbor_count,' // &
'solid_correction_status,coupling_motion_flag,coupling_region_id') then
call fail('Solid particle CSV header mismatch')
end if
read(unit, *, iostat=ios) id, phase, status, mat, sec, pos, vel, &
scalar_values, explosive_values, stress_values, plastic, damage, &
softening, strain_rate_eq, acceleration_norm, blast_force_values, &
coupling_force_values, failure, reference_neighbors, correction_status, &
motion_flag, region_id
close(unit=unit)
if (ios /= 0) call fail('Solid particle CSV row read failed')
if (id /= 1) call fail('Solid particle CSV id mismatch')
if (maxval(abs(vel - (/2.0_rk, 3.0_rk, 4.0_rk/))) > 1.0e-12_rk) then
call fail('Solid particle CSV velocity mismatch')
end if
if (maxval(abs(stress_values - solid_stress(:,1))) > 1.0e-12_rk) then
call fail('Solid particle CSV stress mismatch')
end if
if (abs(plastic - solid_plastic_strain(1)) > 1.0e-12_rk) then
call fail('Solid particle CSV plastic strain mismatch')
end if
if (abs(damage - solid_damage(1)) > 1.0e-12_rk) then
call fail('Solid particle CSV damage mismatch')
end if
if (abs(softening - solid_softening(1)) > 1.0e-12_rk) then
call fail('Solid particle CSV softening mismatch')
end if
if (abs(strain_rate_eq - solid_strain_rate_eq(1)) > 1.0e-12_rk) then
call fail('Solid particle CSV strain rate mismatch')
end if
if (abs(acceleration_norm - sqrt(sum(a(:,1) * a(:,1)))) > 1.0e-12_rk) then
call fail('Solid particle CSV acceleration mismatch')
end if
if (maxval(abs(blast_force_values)) > 1.0e-12_rk) then
call fail('Solid particle CSV Blast load pollution')
end if
if (maxval(abs(coupling_force_values)) > 1.0e-12_rk) then
call fail('Solid particle CSV coupling load pollution')
end if
if (failure /= solid_failure_status(1)) call fail('Solid particle CSV failure mismatch')
if (reference_neighbors /= solid_reference_neighbor_count(1)) then
call fail('Solid particle CSV reference neighbor mismatch')
end if
if (correction_status /= solid_correction_status(1)) then
call fail('Solid particle CSV correction status mismatch')
end if
if (motion_flag /= 0 .or. region_id /= 0) then
call fail('Solid particle CSV coupling defaults mismatch')
end if
end subroutine check_particle_csv
subroutine check_summary_csv(filename)
character(len=*), intent(in) :: filename
call assert_summary_int(filename, 'solid_particles', solid_particle_count)
call assert_summary_int(filename, 'solid_failed_particles', solid_failed_count)
if (solid_particle_count /= 1) call fail('Solid summary active count mismatch')
if (solid_failed_count /= 1) call fail('Solid summary failed count mismatch')
call assert_summary_present(filename, 'solid_mass')
call assert_summary_present(filename, 'solid_momentum_x')
call assert_summary_present(filename, 'solid_momentum_y')
call assert_summary_present(filename, 'solid_momentum_z')
call assert_summary_positive(filename, 'solid_kinetic_energy')
call assert_summary_positive(filename, 'solid_internal_energy')
call assert_summary_positive(filename, 'solid_max_von_mises')
call assert_summary_present(filename, 'solid_plastic_strain_min')
call assert_summary_present(filename, 'solid_plastic_strain_max')
call assert_summary_present(filename, 'solid_damage_min')
call assert_summary_present(filename, 'solid_damage_max')
call assert_summary_present(filename, 'solid_softening_min')
call assert_summary_present(filename, 'solid_softening_max')
call assert_summary_positive(filename, 'solid_max_strain_rate')
call assert_summary_positive(filename, 'solid_max_acceleration')
call assert_summary_present(filename, 'solid_internal_force_residual')
call assert_summary_int(filename, 'solid_reference_neighbor_min', 5)
call assert_summary_present(filename, 'solid_correction_det_min')
call assert_summary_int(filename, 'solid_correction_status', 2)
call assert_summary_positive(filename, 'solid_dt_cfl')
call assert_summary_positive(filename, 'solid_dt_accel')
call assert_summary_positive(filename, 'solid_dtcrit')
if (solid_dtcrit <= 0.0_rk) call fail('Solid dtcrit diagnostic missing')
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('Solid 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('Solid 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('Solid 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('Solid 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 = 86
open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('Solid 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('Solid summary integer parse failed')
return
end if
end do
close(unit=unit)
call fail('Solid 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 = 87
open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('Solid 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('Solid summary real parse failed')
return
end if
end do
close(unit=unit)
call fail('Solid 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=88, file=trim(filename), status='old', iostat=ios)
if (ios == 0) close(unit=88, 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_solid_output