181 lines
6.6 KiB
Fortran
181 lines
6.6 KiB
Fortran
program test_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_run_explicit, sph_set_output_directory, sph_set_output_interval, &
|
|
sph_write_output
|
|
implicit none
|
|
|
|
integer :: ierr
|
|
|
|
call check_no_auto_output_when_disabled()
|
|
call check_manual_output_fields()
|
|
call check_auto_output_interval()
|
|
|
|
contains
|
|
|
|
subroutine setup_model()
|
|
call sph_initialize(ierr)
|
|
call assert_ok(ierr, 'initialize failed')
|
|
call sph_read_inp('tests/data/water_minimal_020.inp', ierr)
|
|
call assert_ok(ierr, 'read input failed')
|
|
call sph_build_model(ierr)
|
|
call assert_ok(ierr, 'build model failed')
|
|
end subroutine setup_model
|
|
|
|
subroutine check_no_auto_output_when_disabled()
|
|
call delete_output_pair(1)
|
|
call delete_output_pair(2)
|
|
call setup_model()
|
|
call sph_run_explicit(ierr)
|
|
call assert_ok(ierr, 'run without output failed')
|
|
call assert_missing('build-codex-unix/sphlite_00000001.csv', &
|
|
'disabled output wrote particle CSV')
|
|
call assert_missing('build-codex-unix/sphlite_00000001_summary.csv', &
|
|
'disabled output wrote summary CSV')
|
|
call sph_finalize(ierr)
|
|
call assert_ok(ierr, 'finalize no-output failed')
|
|
end subroutine check_no_auto_output_when_disabled
|
|
|
|
subroutine check_manual_output_fields()
|
|
call delete_output_pair(0)
|
|
call setup_model()
|
|
call sph_set_output_directory('build-codex-unix', ierr)
|
|
call assert_ok(ierr, 'set output directory failed')
|
|
call sph_set_output_interval(1, ierr)
|
|
call assert_ok(ierr, 'set output interval failed')
|
|
call sph_write_output(0, 0.0_rk, ierr)
|
|
call assert_ok(ierr, 'write output failed')
|
|
call assert_exists('build-codex-unix/sphlite_00000000.csv', &
|
|
'particle CSV output was not written')
|
|
call assert_exists('build-codex-unix/sphlite_00000000_summary.csv', &
|
|
'summary CSV output was not written')
|
|
call check_particle_header('build-codex-unix/sphlite_00000000.csv')
|
|
call check_summary_metrics('build-codex-unix/sphlite_00000000_summary.csv')
|
|
call sph_finalize(ierr)
|
|
call assert_ok(ierr, 'finalize manual output failed')
|
|
end subroutine check_manual_output_fields
|
|
|
|
subroutine check_auto_output_interval()
|
|
call delete_output_pair(1)
|
|
call delete_output_pair(2)
|
|
call setup_model()
|
|
call sph_set_output_directory('build-codex-unix', ierr)
|
|
call assert_ok(ierr, 'set auto output directory failed')
|
|
call sph_set_output_interval(1, ierr)
|
|
call assert_ok(ierr, 'set auto output interval failed')
|
|
call sph_run_explicit(ierr)
|
|
call assert_ok(ierr, 'run with output failed')
|
|
call assert_exists('build-codex-unix/sphlite_00000001.csv', &
|
|
'auto particle CSV was not written')
|
|
call assert_exists('build-codex-unix/sphlite_00000001_summary.csv', &
|
|
'auto summary CSV was not written')
|
|
call assert_exists('build-codex-unix/sphlite_00000002.csv', &
|
|
'auto final particle CSV was not written')
|
|
call check_summary_metrics('build-codex-unix/sphlite_00000002_summary.csv')
|
|
call sph_finalize(ierr)
|
|
call assert_ok(ierr, 'finalize auto output failed')
|
|
end subroutine check_auto_output_interval
|
|
|
|
subroutine check_particle_header(filename)
|
|
character(len=*), intent(in) :: filename
|
|
character(len=256) :: header
|
|
integer :: ios
|
|
|
|
open(unit=71, file=trim(filename), status='old', action='read', iostat=ios)
|
|
if (ios /= 0) call fail('particle CSV open failed')
|
|
read(71, '(A)', iostat=ios) header
|
|
close(unit=71)
|
|
if (ios /= 0) call fail('particle CSV header missing')
|
|
if (trim(header) /= &
|
|
'id,phase,status,material,section,x,y,z,vx,vy,vz,rho,pressure,temperature,h') then
|
|
call fail('particle CSV header mismatch')
|
|
end if
|
|
end subroutine check_particle_header
|
|
|
|
subroutine check_summary_metrics(filename)
|
|
character(len=*), intent(in) :: filename
|
|
|
|
call assert_summary_metric(filename, 'step')
|
|
call assert_summary_metric(filename, 'time')
|
|
call assert_summary_metric(filename, 'water_mass')
|
|
call assert_summary_metric(filename, 'water_momentum_x')
|
|
call assert_summary_metric(filename, 'water_momentum_y')
|
|
call assert_summary_metric(filename, 'water_momentum_z')
|
|
call assert_summary_metric(filename, 'max_pressure')
|
|
end subroutine check_summary_metrics
|
|
|
|
subroutine assert_summary_metric(filename, metric)
|
|
character(len=*), intent(in) :: filename
|
|
character(len=*), intent(in) :: metric
|
|
character(len=256) :: line
|
|
integer :: ios
|
|
logical :: found
|
|
|
|
found = .false.
|
|
open(unit=72, file=trim(filename), status='old', action='read', iostat=ios)
|
|
if (ios /= 0) call fail('summary CSV open failed')
|
|
do
|
|
read(72, '(A)', iostat=ios) line
|
|
if (ios /= 0) exit
|
|
if (index(line, trim(metric) // ',') == 1) found = .true.
|
|
end do
|
|
close(unit=72)
|
|
if (.not. found) call fail('summary CSV metric missing')
|
|
end subroutine assert_summary_metric
|
|
|
|
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=73, file=trim(filename), status='old', iostat=ios)
|
|
if (ios == 0) close(unit=73, status='delete')
|
|
end subroutine delete_file_if_exists
|
|
|
|
subroutine assert_exists(filename, message)
|
|
character(len=*), intent(in) :: filename
|
|
character(len=*), intent(in) :: message
|
|
logical :: exists
|
|
|
|
inquire(file=trim(filename), exist=exists)
|
|
if (.not. exists) call fail(message)
|
|
end subroutine assert_exists
|
|
|
|
subroutine assert_missing(filename, message)
|
|
character(len=*), intent(in) :: filename
|
|
character(len=*), intent(in) :: message
|
|
logical :: exists
|
|
|
|
inquire(file=trim(filename), exist=exists)
|
|
if (exists) call fail(message)
|
|
end subroutine assert_missing
|
|
|
|
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_output
|