diff --git a/tests/module/test_restart.f90 b/tests/module/test_restart.f90 index 0d95e61..dc75c77 100644 --- a/tests/module/test_restart.f90 +++ b/tests/module/test_restart.f90 @@ -6,7 +6,10 @@ program test_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, & - internal_energy, temperature, h, acc_body, SPH_PHASE_WATER, SPH_STATUS_ACTIVE + internal_energy, temperature, h, acc_body, neighbor_count, & + water_particle_count, water_mass, water_momentum, water_max_pressure, & + water_min_density, water_max_density, water_dt_cfl, water_dt_accel, & + water_dt_viscous, 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 @@ -37,6 +40,7 @@ program test_restart call sph_read_restart('build-codex-unix/restart_test.tmp', ierr) call assert_ok(ierr, 'read restart failed') call assert_restart_identity() + call assert_restart_read_state() call assert_repeat_summary() call sph_get_current_time(time, step, ierr) call assert_ok(ierr, 'time after restart failed') @@ -52,6 +56,7 @@ program test_restart end if call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed') + call check_invalid_restart_error() contains @@ -94,6 +99,19 @@ contains end if end subroutine assert_restart_identity + subroutine assert_restart_read_state() + if (particle_phase(1) /= SPH_PHASE_WATER) call fail('restart read phase mismatch') + if (particle_status(1) /= SPH_STATUS_ACTIVE) call fail('restart read status mismatch') + if (abs(rho(1) - 1000.0_rk) > 1.0e-6_rk) call fail('restart read rho mismatch') + if (abs(h(1) - 0.15_rk) > 1.0e-12_rk) call fail('restart read h mismatch') + if (water_rho0 <= 0.0_rk .or. water_c0 <= 0.0_rk) call fail('restart read EOS mismatch') + if (water_visc_alpha < 0.0_rk .or. water_visc_eta2 <= 0.0_rk) then + call fail('restart read viscosity mismatch') + end if + if (maxval(abs(water_body_force)) > 1.0e-12_rk) call fail('restart read body force mismatch') + if (maxval(neighbor_count) <= 0) call fail('restart did not rebuild neighbors') + end subroutine assert_restart_read_state + subroutine assert_restart_file_content(expected_time, expected_step) real(rk), intent(in) :: expected_time integer, intent(in) :: expected_step @@ -119,7 +137,7 @@ contains real(rk) :: visc_values(3) real(rk) :: body_values(3) real(rk) :: neighbor_values(5) - real(rk) :: water_count_value + integer :: file_water_count real(rk) :: water_mass_value real(rk) :: water_diag_values(7) real(rk) :: dt_values(3) @@ -172,12 +190,29 @@ contains 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 + read(74, *, iostat=ios) file_water_count, water_mass_value if (ios /= 0) call fail('restart water summary read failed') + if (file_water_count /= water_particle_count) call fail('restart water count mismatch') + if (abs(water_mass_value - water_mass) > 1.0e-12_rk) call fail('restart water mass mismatch') read(74, *, iostat=ios) water_diag_values if (ios /= 0) call fail('restart water diagnostic read failed') + if (maxval(abs(water_diag_values(1:3) - water_momentum)) > 1.0e-12_rk) then + call fail('restart water momentum mismatch') + end if + if (abs(water_diag_values(4) - water_max_pressure) > 1.0e-12_rk) then + call fail('restart max pressure mismatch') + end if + if (abs(water_diag_values(5) - water_min_density) > 1.0e-12_rk) then + call fail('restart min density mismatch') + end if + if (abs(water_diag_values(6) - water_max_density) > 1.0e-12_rk) then + call fail('restart max density mismatch') + end if read(74, *, iostat=ios) dt_values if (ios /= 0) call fail('restart water dt read failed') + if (abs(dt_values(1) - water_dt_cfl) > 1.0e-12_rk) call fail('restart dt cfl mismatch') + if (abs(dt_values(2) - water_dt_accel) > 1.0e-12_rk) call fail('restart dt accel mismatch') + if (abs(dt_values(3) - water_dt_viscous) > 1.0e-12_rk) call fail('restart dt viscous mismatch') 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') @@ -206,6 +241,22 @@ contains if (abs(scalar_values(7) - h(1)) > 1.0e-12_rk) call fail('restart h mismatch') end subroutine assert_restart_file_content + subroutine check_invalid_restart_error() + integer :: ios + + open(unit=75, file='build-codex-unix/restart_invalid.tmp', & + status='unknown', action='write', iostat=ios) + if (ios /= 0) call fail('invalid restart fixture open failed') + write(75, '(A)') 'NOT_A_RESTART' + close(unit=75) + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize invalid restart failed') + call sph_read_restart('build-codex-unix/restart_invalid.tmp', ierr) + if (ierr == SPH_SUCCESS) call fail('invalid restart did not fail') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize invalid restart failed') + end subroutine check_invalid_restart_error + subroutine assert_repeat_summary() real(rk) :: mass_a real(rk) :: mass_b diff --git a/tests/module/test_solver_water.f90 b/tests/module/test_solver_water.f90 index e05d9d7..dbcc72e 100644 --- a/tests/module/test_solver_water.f90 +++ b/tests/module/test_solver_water.f90 @@ -4,7 +4,9 @@ program test_solver_water use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model, & sph_advance_explicit, sph_run_explicit, sph_get_current_time, & sph_estimate_stable_dt, sph_rebuild_neighbors, sph_get_neighbor_stats, & - sph_get_water_summary, sph_get_water_dt_limits + sph_get_particle_count, sph_get_particle_state_capacity, sph_get_particle_state, & + sph_set_output_directory, sph_write_output, sph_write_restart, sph_read_restart, & + sph_get_last_error, sph_get_water_summary, sph_get_water_dt_limits use sph_database, only: rho, v, h, a, water_dt_cfl, water_dt_accel, & water_dt_viscous use sph_control, only: dt_default, end_time, max_steps, safety_factor, & @@ -26,6 +28,7 @@ program test_solver_water call check_no_water_dt_strategy() call check_bad_h_dt_error() call check_fortran_api_paths() + call check_fortran_api_compatibility() contains @@ -274,6 +277,86 @@ contains call assert_ok(ierr, 'finalize API paths failed') end subroutine check_fortran_api_paths + subroutine check_fortran_api_compatibility() + integer :: count + integer :: ndim + integer :: actual + integer :: code + integer :: max_neighbors + integer :: ids(8) + integer :: phase(8) + integer :: status(8) + integer :: mat(8) + integer :: sec(8) + real(rk) :: average_neighbors + real(rk) :: x_out(3,8) + real(rk) :: v_out(3,8) + real(rk) :: a_out(3,8) + real(rk) :: rho_out(8) + real(rk) :: pressure_out(8) + real(rk) :: energy_out(8) + real(rk) :: temp_out(8) + real(rk) :: h_out(8) + real(rk) :: dtcrit + real(rk) :: total_mass + real(rk) :: momentum(3) + real(rk) :: max_pressure + real(rk) :: min_density + real(rk) :: max_density + character(len=256) :: message + + call sph_initialize(ierr) + call assert_ok(ierr, 'compat initialize failed') + call sph_read_inp('tests/data/water_minimal_020.inp', ierr) + call assert_ok(ierr, 'compat read failed') + call sph_build_model(ierr) + call assert_ok(ierr, 'compat build failed') + call sph_get_particle_count(count, ierr) + call assert_ok(ierr, 'compat particle count failed') + if (count /= 4) call fail('compat particle count mismatch') + call sph_get_particle_state_capacity(count, ndim, ierr) + call assert_ok(ierr, 'compat state capacity failed') + if (count /= 4 .or. ndim /= 3) call fail('compat state capacity mismatch') + call sph_get_particle_state(8, ids, phase, status, mat, sec, x_out, v_out, & + a_out, rho_out, pressure_out, energy_out, temp_out, h_out, actual, ierr) + call assert_ok(ierr, 'compat particle state failed') + if (actual /= 4 .or. ids(1) /= 1) call fail('compat particle state mismatch') + call sph_rebuild_neighbors(ierr) + call assert_ok(ierr, 'compat neighbor rebuild failed') + call sph_get_neighbor_stats(max_neighbors, average_neighbors, ierr) + call assert_ok(ierr, 'compat neighbor stats failed') + if (max_neighbors <= 0 .or. average_neighbors <= 0.0_rk) call fail('compat neighbor mismatch') + call sph_estimate_stable_dt(dtcrit, ierr) + call assert_ok(ierr, 'compat stable dt failed') + if (dtcrit <= 0.0_rk) call fail('compat stable dt mismatch') + call sph_advance_explicit(1.0e-6_rk, ierr) + call assert_ok(ierr, 'compat advance failed') + call sph_run_explicit(ierr) + call assert_ok(ierr, 'compat run failed') + call sph_get_current_time(time, step, ierr) + call assert_ok(ierr, 'compat current time failed') + if (step <= 0 .or. time <= 0.0_rk) call fail('compat current time mismatch') + call sph_get_water_summary(total_mass, momentum, max_pressure, & + min_density, max_density, ierr) + call assert_ok(ierr, 'compat water summary failed') + if (total_mass <= 0.0_rk .or. min_density <= 0.0_rk) call fail('compat summary mismatch') + call sph_set_output_directory('build-codex-unix', ierr) + call assert_ok(ierr, 'compat output directory failed') + call sph_write_output(88, time, ierr) + call assert_ok(ierr, 'compat write output failed') + call sph_write_restart('build-codex-unix/restart_api_compat.tmp', ierr) + call assert_ok(ierr, 'compat write restart failed') + call sph_read_restart('build-codex-unix/restart_api_compat.tmp', ierr) + call assert_ok(ierr, 'compat read restart failed') + call sph_advance_explicit(-1.0e-6_rk, ierr) + if (ierr == SPH_SUCCESS) call fail('compat error path did not fail') + call sph_get_last_error(code, message, ierr) + call assert_ok(ierr, 'compat get last error failed') + if (code == 0 .or. len_trim(message) <= 0) call fail('compat error query mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'compat finalize failed') + end subroutine check_fortran_api_compatibility + subroutine assert_ok(ierr_value, message) integer, intent(in) :: ierr_value character(len=*), intent(in) :: message diff --git a/todo.md b/todo.md index 7a76643..e8b9cf9 100644 --- a/todo.md +++ b/todo.md @@ -282,29 +282,29 @@ - [x] 确认 restart 写入 pressure。 - [x] 确认 restart 写入 h。 - [x] 确认 restart 写入 v、a、vhalf。 -- [ ] 确认 restart 写入 Water 诊断状态。 -- [ ] 确认 restart 读回 current time。 -- [ ] 确认 restart 读回 current step。 -- [ ] 确认 restart 读回 Water 参数。 -- [ ] 确认 restart 读回粒子状态。 -- [ ] 确认 restart 读回后重建邻域。 -- [ ] 确认 restart 读回后继续 Water 显式步。 -- [ ] 加固非法 restart 文件错误路径。 +- [x] 确认 restart 写入 Water 诊断状态。 +- [x] 确认 restart 读回 current time。 +- [x] 确认 restart 读回 current step。 +- [x] 确认 restart 读回 Water 参数。 +- [x] 确认 restart 读回粒子状态。 +- [x] 确认 restart 读回后重建邻域。 +- [x] 确认 restart 读回后继续 Water 显式步。 +- [x] 加固非法 restart 文件错误路径。 ## 阶段 N:Fortran API 加固 -- [ ] 确认生命周期 API 兼容 0.2.0。 -- [ ] 确认 `.inp` 读取 API 兼容 0.2.0。 -- [ ] 确认 build model API 兼容 0.2.0。 -- [ ] 确认 stable dt API 兼容 0.2.0。 -- [ ] 确认 advance/run explicit API 兼容 0.2.0。 -- [ ] 确认 current time API 兼容 0.2.0。 -- [ ] 确认 particle count/state API 兼容 0.2.0。 -- [ ] 确认 output/restart API 兼容 0.2.0。 -- [ ] 确认 error query API 兼容 0.2.0。 -- [ ] 确认 neighbor rebuild API 兼容 0.2.0。 -- [ ] 确认 neighbor stats API 兼容 0.2.0。 -- [ ] 确认 water summary API 兼容 0.2.0。 +- [x] 确认生命周期 API 兼容 0.2.0。 +- [x] 确认 `.inp` 读取 API 兼容 0.2.0。 +- [x] 确认 build model API 兼容 0.2.0。 +- [x] 确认 stable dt API 兼容 0.2.0。 +- [x] 确认 advance/run explicit API 兼容 0.2.0。 +- [x] 确认 current time API 兼容 0.2.0。 +- [x] 确认 particle count/state API 兼容 0.2.0。 +- [x] 确认 output/restart API 兼容 0.2.0。 +- [x] 确认 error query API 兼容 0.2.0。 +- [x] 确认 neighbor rebuild API 兼容 0.2.0。 +- [x] 确认 neighbor stats API 兼容 0.2.0。 +- [x] 确认 water summary API 兼容 0.2.0。 - [ ] 确认 water dt limits API 兼容 0.2.0。 - [ ] 加固新增 API 未初始化错误路径。 - [ ] 加固新增 API 未 build 错误路径。