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 use sph_database, only: x, v, rho, pressure implicit none integer :: ierr call check_no_auto_output_when_disabled() call check_manual_output_fields() call check_auto_output_interval() call check_output_directory_errors() 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() real(rk) :: x_before(3) real(rk) :: v_before(3) real(rk) :: rho_before real(rk) :: pressure_before call delete_output_pair(0) call setup_model() x_before = x(:,1) v_before = v(:,1) rho_before = rho(1) pressure_before = pressure(1) 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') if (maxval(abs(x(:,1) - x_before)) > 1.0e-12_rk) call fail('output changed position') if (maxval(abs(v(:,1) - v_before)) > 1.0e-12_rk) call fail('output changed velocity') if (abs(rho(1) - rho_before) > 1.0e-12_rk) call fail('output changed density') if (abs(pressure(1) - pressure_before) > 1.0e-12_rk) call fail('output changed pressure') 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_output_directory_errors() call setup_model() call sph_set_output_directory('', ierr) if (ierr == SPH_SUCCESS) call fail('empty output directory did not fail') call sph_set_output_directory('build-codex-unix/missing-output-dir', ierr) call assert_ok(ierr, 'set missing output directory failed') call sph_write_output(99, 0.0_rk, ierr) if (ierr == SPH_SUCCESS) call fail('missing output directory did not fail') call sph_finalize(ierr) call assert_ok(ierr, 'finalize output errors failed') end subroutine check_output_directory_errors 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,explosive_detonation_time,' // & 'explosive_lambda,explosive_initial_mass,explosive_remaining_mass,' // & 'explosive_released_mass,explosive_released_energy') 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') call assert_summary_metric(filename, 'min_density') call assert_summary_metric(filename, 'max_density') call assert_summary_metric(filename, 'dtcrit') call assert_summary_metric(filename, 'gas_particles') call assert_summary_metric(filename, 'gas_mass') call assert_summary_metric(filename, 'gas_volume') call assert_summary_metric(filename, 'gas_center_x') call assert_summary_metric(filename, 'gas_center_y') call assert_summary_metric(filename, 'gas_center_z') call assert_summary_metric(filename, 'gas_mean_pressure') call assert_summary_metric(filename, 'gas_max_pressure') call assert_summary_metric(filename, 'gas_equivalent_radius') call assert_summary_metric(filename, 'gas_radius_rate') call assert_summary_metric(filename, 'gas_dtcrit') call assert_summary_metric(filename, 'explosive_particles') call assert_summary_metric(filename, 'explosive_lambda_mean') call assert_summary_metric(filename, 'explosive_released_energy') call assert_summary_metric(filename, 'explosive_initial_mass') call assert_summary_metric(filename, 'explosive_remaining_mass') call assert_summary_metric(filename, 'explosive_release_mass_error') call assert_summary_metric(filename, 'explosive_release_energy_error') call assert_summary_metric(filename, 'explosive_converted_particles') call assert_summary_metric(filename, 'explosive_source_particles') call assert_summary_metric(filename, 'explosive_source_mass_total') 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