Harden output metadata and restart serialization checks

This commit is contained in:
Codex
2026-07-02 23:36:00 +08:00
parent b9299f14c6
commit f66768f261
4 changed files with 182 additions and 22 deletions
+30
View File
@@ -4,6 +4,7 @@ program test_output
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
@@ -11,6 +12,7 @@ program test_output
call check_no_auto_output_when_disabled()
call check_manual_output_fields()
call check_auto_output_interval()
call check_output_directory_errors()
contains
@@ -38,8 +40,17 @@ contains
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)
@@ -52,6 +63,10 @@ contains
'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
@@ -77,6 +92,18 @@ contains
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
@@ -103,6 +130,9 @@ contains
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')
end subroutine check_summary_metrics
subroutine assert_summary_metric(filename, metric)
+118 -2
View File
@@ -1,12 +1,15 @@
program test_restart
use sph_precision, only: rk
use sph_precision, only: rk, SPH_VERSION_MAJOR, SPH_VERSION_MINOR, SPH_VERSION_PATCH
use sph_error, only: SPH_SUCCESS
use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model, &
sph_run_explicit, sph_get_current_time, sph_write_restart, sph_read_restart, &
sph_advance_explicit, sph_get_water_summary
use sph_database, only: particle_id_ext, particle_phase, particle_status, &
material_id, section_id, x0, x, v, a, vhalf, mass, rho, volume, pressure, &
temperature, h, acc_body, SPH_PHASE_WATER, SPH_STATUS_ACTIVE
internal_energy, temperature, h, acc_body, SPH_PHASE_WATER, SPH_STATUS_ACTIVE
use sph_control, only: output_interval, output_enabled, safety_factor
use sph_water, only: water_rho0, water_c0, water_gamma, water_p0, water_b, &
water_visc_alpha, water_visc_beta, water_visc_eta2, water_body_force
implicit none
integer :: ierr
@@ -30,6 +33,7 @@ program test_restart
call assert_ok(ierr, 'time before restart failed')
call sph_write_restart('build-codex-unix/restart_test.tmp', ierr)
call assert_ok(ierr, 'write restart failed')
call assert_restart_file_content(written_time, written_step)
call sph_read_restart('build-codex-unix/restart_test.tmp', ierr)
call assert_ok(ierr, 'read restart failed')
call assert_restart_identity()
@@ -90,6 +94,118 @@ contains
end if
end subroutine assert_restart_identity
subroutine assert_restart_file_content(expected_time, expected_step)
real(rk), intent(in) :: expected_time
integer, intent(in) :: expected_step
character(len=64) :: tag
character(len=32) :: state_version
character(len=256) :: directory
integer :: major
integer :: minor
integer :: patch
integer :: count
integer :: file_step
integer :: interval
integer :: pid
integer :: phase
integer :: status
integer :: mat
integer :: sec
integer :: ios
logical :: enabled
real(rk) :: file_time
real(rk) :: file_safety
real(rk) :: eos_values(5)
real(rk) :: visc_values(3)
real(rk) :: body_values(3)
real(rk) :: neighbor_values(5)
real(rk) :: water_count_value
real(rk) :: water_mass_value
real(rk) :: water_diag_values(7)
real(rk) :: dt_values(3)
real(rk) :: rx0(3)
real(rk) :: rx(3)
real(rk) :: rv(3)
real(rk) :: ra(3)
real(rk) :: rvhalf(3)
real(rk) :: scalar_values(7)
open(unit=74, file='build-codex-unix/restart_test.tmp', status='old', &
action='read', iostat=ios)
if (ios /= 0) call fail('restart file open failed')
read(74, *, iostat=ios) tag, major, minor, patch, state_version
if (ios /= 0) call fail('restart header read failed')
if (trim(tag) /= 'SPHLITE_RESTART') call fail('restart header tag mismatch')
if (major /= SPH_VERSION_MAJOR .or. minor /= SPH_VERSION_MINOR) then
call fail('restart version mismatch')
end if
if (patch /= SPH_VERSION_PATCH) call fail('restart patch mismatch')
if (trim(state_version) /= 'STATE_0_2') call fail('restart state version mismatch')
read(74, *, iostat=ios) file_time, file_step, count
if (ios /= 0) call fail('restart time state read failed')
if (abs(file_time - expected_time) > 1.0e-12_rk) call fail('restart time write mismatch')
if (file_step /= expected_step) call fail('restart step write mismatch')
if (count /= 4) call fail('restart particle count write mismatch')
read(74, *, iostat=ios) interval, enabled, file_safety
if (ios /= 0) call fail('restart output state read failed')
if (interval /= output_interval) call fail('restart output interval mismatch')
if (enabled .neqv. output_enabled) call fail('restart output enabled mismatch')
if (abs(file_safety - safety_factor) > 1.0e-12_rk) call fail('restart safety mismatch')
read(74, '(A)', iostat=ios) directory
if (ios /= 0 .or. len_trim(directory) <= 0) call fail('restart output directory missing')
read(74, *, iostat=ios) eos_values
if (ios /= 0) call fail('restart EOS state read failed')
if (abs(eos_values(1) - water_rho0) > 1.0e-12_rk) call fail('restart rho0 mismatch')
if (abs(eos_values(2) - water_c0) > 1.0e-12_rk) call fail('restart c0 mismatch')
if (abs(eos_values(3) - water_gamma) > 1.0e-12_rk) call fail('restart gamma mismatch')
if (abs(eos_values(4) - water_p0) > 1.0e-12_rk) call fail('restart p0 mismatch')
if (abs(eos_values(5) - water_b) > 1.0e-6_rk) call fail('restart B mismatch')
read(74, *, iostat=ios) visc_values
if (ios /= 0) call fail('restart viscosity state read failed')
if (abs(visc_values(1) - water_visc_alpha) > 1.0e-12_rk) call fail('restart alpha mismatch')
if (abs(visc_values(2) - water_visc_beta) > 1.0e-12_rk) call fail('restart beta mismatch')
if (abs(visc_values(3) - water_visc_eta2) > 1.0e-12_rk) call fail('restart eta2 mismatch')
read(74, *, iostat=ios) body_values
if (ios /= 0) call fail('restart body force state read failed')
if (maxval(abs(body_values - water_body_force)) > 1.0e-12_rk) then
call fail('restart body force mismatch')
end if
read(74, *, iostat=ios) neighbor_values
if (ios /= 0) call fail('restart neighbor state read failed')
read(74, *, iostat=ios) water_count_value, water_mass_value
if (ios /= 0) call fail('restart water summary read failed')
read(74, *, iostat=ios) water_diag_values
if (ios /= 0) call fail('restart water diagnostic read failed')
read(74, *, iostat=ios) dt_values
if (ios /= 0) call fail('restart water dt read failed')
read(74, *, iostat=ios) pid, phase, status, mat, sec
if (ios /= 0) call fail('restart particle ids read failed')
if (pid /= particle_id_ext(1)) call fail('restart particle id mismatch')
if (phase /= particle_phase(1)) call fail('restart particle phase mismatch')
if (status /= particle_status(1)) call fail('restart particle status mismatch')
if (mat /= material_id(1)) call fail('restart particle material mismatch')
if (sec /= section_id(1)) call fail('restart particle section mismatch')
read(74, *, iostat=ios) rx0, rx, rv, ra, rvhalf
if (ios /= 0) call fail('restart particle vector read failed')
if (maxval(abs(rx0 - x0(:,1))) > 1.0e-12_rk) call fail('restart x0 mismatch')
if (maxval(abs(rx - x(:,1))) > 1.0e-12_rk) call fail('restart x mismatch')
if (maxval(abs(rv - v(:,1))) > 1.0e-12_rk) call fail('restart velocity mismatch')
if (maxval(abs(ra - a(:,1))) > 1.0e-12_rk) call fail('restart acceleration mismatch')
if (maxval(abs(rvhalf - vhalf(:,1))) > 1.0e-12_rk) call fail('restart vhalf mismatch')
read(74, *, iostat=ios) scalar_values
close(unit=74)
if (ios /= 0) call fail('restart particle scalar read failed')
if (abs(scalar_values(1) - mass(1)) > 1.0e-12_rk) call fail('restart mass mismatch')
if (abs(scalar_values(2) - rho(1)) > 1.0e-12_rk) call fail('restart rho mismatch')
if (abs(scalar_values(3) - volume(1)) > 1.0e-12_rk) call fail('restart volume mismatch')
if (abs(scalar_values(4) - pressure(1)) > 1.0e-12_rk) call fail('restart pressure mismatch')
if (abs(scalar_values(5) - internal_energy(1)) > 1.0e-12_rk) then
call fail('restart internal energy mismatch')
end if
if (abs(scalar_values(6) - temperature(1)) > 1.0e-12_rk) call fail('restart temp mismatch')
if (abs(scalar_values(7) - h(1)) > 1.0e-12_rk) call fail('restart h mismatch')
end subroutine assert_restart_file_content
subroutine assert_repeat_summary()
real(rk) :: mass_a
real(rk) :: mass_b
+20 -20
View File
@@ -259,29 +259,29 @@
- [x] 确认 summary CSV 包含 water mass 字段。
- [x] 确认 summary CSV 包含 water momentum 字段。
- [x] 确认 summary CSV 包含 max pressure 字段。
- [ ] 确认 summary CSV 包含 min/max density 字段。
- [ ] 确认 summary CSV 包含 dtcrit 字段。
- [ ] 加固输出目录不存在时的处理。
- [ ] 加固非法输出目录错误路径。
- [ ] 确认手动 `sph_write_output` 不改变物理状态。
- [ ] 确认 CLI `--run --output` 使用实际 step/time。
- [x] 确认 summary CSV 包含 min/max density 字段。
- [x] 确认 summary CSV 包含 dtcrit 字段。
- [x] 加固输出目录不存在时的处理。
- [x] 加固非法输出目录错误路径。
- [x] 确认手动 `sph_write_output` 不改变物理状态。
- [x] 确认 CLI `--run --output` 使用实际 step/time。
## 阶段 M:Restart 加固
- [ ] 确认 restart header 可识别。
- [ ] 确认 restart state version 记录 0.2.1 策略。
- [ ] 确认 restart 写入 current time。
- [ ] 确认 restart 写入 current step。
- [ ] 确认 restart 写入 output control。
- [ ] 确认 restart 写入 safety factor。
- [ ] 确认 restart 写入 Water EOS 参数。
- [ ] 确认 restart 写入 viscosity 参数。
- [ ] 确认 restart 写入 body force 参数。
- [ ] 确认 restart 写入粒子通用状态。
- [ ] 确认 restart 写入 rho。
- [ ] 确认 restart 写入 pressure。
- [ ] 确认 restart 写入 h。
- [ ] 确认 restart 写入 v、a、vhalf。
- [x] 确认 restart header 可识别。
- [x] 确认 restart state version 记录 0.2.1 策略。
- [x] 确认 restart 写入 current time。
- [x] 确认 restart 写入 current step。
- [x] 确认 restart 写入 output control。
- [x] 确认 restart 写入 safety factor。
- [x] 确认 restart 写入 Water EOS 参数。
- [x] 确认 restart 写入 viscosity 参数。
- [x] 确认 restart 写入 body force 参数。
- [x] 确认 restart 写入粒子通用状态。
- [x] 确认 restart 写入 rho。
- [x] 确认 restart 写入 pressure。
- [x] 确认 restart 写入 h。
- [x] 确认 restart 写入 v、a、vhalf。
- [ ] 确认 restart 写入 Water 诊断状态。
- [ ] 确认 restart 读回 current time。
- [ ] 确认 restart 读回 current step。
+14
View File
@@ -28,6 +28,15 @@ def read_fields(output: str) -> dict[str, str]:
return fields
def read_summary(path: Path) -> dict[str, str]:
metrics: dict[str, str] = {}
for line in path.read_text(encoding="utf-8").splitlines()[1:]:
if "," in line:
name, value = line.split(",", 1)
metrics[name.strip()] = value.strip()
return metrics
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--exe", required=True)
@@ -87,6 +96,11 @@ def main() -> int:
summary_file = Path(output_dir) / f"sphlite_{output_step:08d}_summary.csv"
if not output_file.exists() or not summary_file.exists():
raise AssertionError("CLI output files were not written")
summary = read_summary(summary_file)
if int(summary["step"]) != output_step:
raise AssertionError("CLI summary step does not match actual step")
if abs(float(summary["time"]) - float(run_fields["time"])) > 1.0e-12:
raise AssertionError("CLI summary time does not match actual time")
bad = run_command([exe, bad_model, "--check"], expect_ok=False)
require_field(bad.stderr, "error_code")