From b9299f14c69780339505050628bbf27f1b06f006 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 2 Jul 2026 23:28:45 +0800 Subject: [PATCH] Harden output triggers and CSV field coverage --- tests/module/test_output.f90 | 174 ++++++++++++++++++++++++++++++---- tests/module/test_restart.f90 | 6 ++ todo.md | 40 ++++---- 3 files changed, 180 insertions(+), 40 deletions(-) diff --git a/tests/module/test_output.f90 b/tests/module/test_output.f90 index 9cbcf10..3cfd41c 100644 --- a/tests/module/test_output.f90 +++ b/tests/module/test_output.f90 @@ -2,33 +2,167 @@ 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_set_output_directory, sph_set_output_interval, sph_write_output + sph_run_explicit, sph_set_output_directory, sph_set_output_interval, & + sph_write_output implicit none integer :: ierr - logical :: exists - 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') - 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') - inquire(file='build-codex-unix/sphlite_00000000.csv', exist=exists) - if (.not. exists) call fail('particle CSV output was not written') - inquire(file='build-codex-unix/sphlite_00000000_summary.csv', exist=exists) - if (.not. exists) call fail('summary CSV output was not written') - call sph_finalize(ierr) - call assert_ok(ierr, 'finalize failed') + 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 diff --git a/tests/module/test_restart.f90 b/tests/module/test_restart.f90 index 3131840..973be30 100644 --- a/tests/module/test_restart.f90 +++ b/tests/module/test_restart.f90 @@ -40,6 +40,12 @@ program test_restart if (abs(time - written_time) > 1.0e-12_rk) call fail('restart did not restore time') call sph_advance_explicit(1.0e-6_rk, ierr) call assert_ok(ierr, 'advance after restart failed') + call sph_get_current_time(time, step, ierr) + call assert_ok(ierr, 'time after restart advance failed') + if (step /= written_step + 1) call fail('restart advance step mismatch') + if (abs(time - written_time - 1.0e-6_rk) > 1.0e-12_rk) then + call fail('restart advance time mismatch') + end if call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed') diff --git a/todo.md b/todo.md index 236a37a..417f76b 100644 --- a/todo.md +++ b/todo.md @@ -236,29 +236,29 @@ - [x] 加固单步 Water 推进。 - [x] 加固多步 Water 推进。 - [x] 加固 `sph_run_explicit` Water 闭环。 -- [ ] 加固自动输出间隔触发。 -- [ ] 加固未启用输出时不自动写文件。 -- [ ] 加固 restart 后继续显式步。 -- [ ] 加固当前时间和步号查询。 +- [x] 加固自动输出间隔触发。 +- [x] 加固未启用输出时不自动写文件。 +- [x] 加固 restart 后继续显式步。 +- [x] 加固当前时间和步号查询。 ## 阶段 L:Output 加固 -- [ ] 确认粒子 CSV 包含 id 字段。 -- [ ] 确认粒子 CSV 包含 phase 字段。 -- [ ] 确认粒子 CSV 包含 status 字段。 -- [ ] 确认粒子 CSV 包含 material 字段。 -- [ ] 确认粒子 CSV 包含 section 字段。 -- [ ] 确认粒子 CSV 包含坐标字段。 -- [ ] 确认粒子 CSV 包含速度字段。 -- [ ] 确认粒子 CSV 包含 rho 字段。 -- [ ] 确认粒子 CSV 包含 pressure 字段。 -- [ ] 确认粒子 CSV 包含 temperature 字段。 -- [ ] 确认粒子 CSV 包含 h 字段。 -- [ ] 确认 summary CSV 包含 step 字段。 -- [ ] 确认 summary CSV 包含 time 字段。 -- [ ] 确认 summary CSV 包含 water mass 字段。 -- [ ] 确认 summary CSV 包含 water momentum 字段。 -- [ ] 确认 summary CSV 包含 max pressure 字段。 +- [x] 确认粒子 CSV 包含 id 字段。 +- [x] 确认粒子 CSV 包含 phase 字段。 +- [x] 确认粒子 CSV 包含 status 字段。 +- [x] 确认粒子 CSV 包含 material 字段。 +- [x] 确认粒子 CSV 包含 section 字段。 +- [x] 确认粒子 CSV 包含坐标字段。 +- [x] 确认粒子 CSV 包含速度字段。 +- [x] 确认粒子 CSV 包含 rho 字段。 +- [x] 确认粒子 CSV 包含 pressure 字段。 +- [x] 确认粒子 CSV 包含 temperature 字段。 +- [x] 确认粒子 CSV 包含 h 字段。 +- [x] 确认 summary CSV 包含 step 字段。 +- [x] 确认 summary CSV 包含 time 字段。 +- [x] 确认 summary CSV 包含 water mass 字段。 +- [x] 确认 summary CSV 包含 water momentum 字段。 +- [x] 确认 summary CSV 包含 max pressure 字段。 - [ ] 确认 summary CSV 包含 min/max density 字段。 - [ ] 确认 summary CSV 包含 dtcrit 字段。 - [ ] 加固输出目录不存在时的处理。