From cd9a040cad4201a062e007cc3a0aebaa414154c3 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 2 Jul 2026 21:01:33 +0800 Subject: [PATCH] Extend water output and restart state --- src/interface/sph_api.f90 | 1 + src/output/sph_output_csv.f90 | 46 ++++++++++++++++++-- src/output/sph_restart.f90 | 81 ++++++++++++++++++++++++++++++++--- todo.md | 40 ++++++++--------- 4 files changed, 139 insertions(+), 29 deletions(-) diff --git a/src/interface/sph_api.f90 b/src/interface/sph_api.f90 index b162f60..b998fe6 100644 --- a/src/interface/sph_api.f90 +++ b/src/interface/sph_api.f90 @@ -268,6 +268,7 @@ contains call sph_api_require_initialized(ierr) if (ierr /= SPH_SUCCESS) return call sph_restart_read(filename, ierr) + if (ierr == SPH_SUCCESS) call sph_api_state_set_model_built(.true.) end subroutine sph_read_restart subroutine sph_get_input_warning_count(count, ierr) diff --git a/src/output/sph_output_csv.f90 b/src/output/sph_output_csv.f90 index 4b69d1a..5d78d9b 100644 --- a/src/output/sph_output_csv.f90 +++ b/src/output/sph_output_csv.f90 @@ -3,7 +3,9 @@ module sph_output_csv use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, SPH_ERROR_IO, sph_error_set use sph_control, only: output_directory, output_interval, output_enabled use sph_database, only: npart, particle_id_ext, particle_phase, particle_status, & - material_id, section_id, x, v, rho, pressure, temperature + material_id, section_id, x, v, rho, pressure, temperature, h, & + water_mass, water_momentum, water_max_pressure, water_min_density, & + water_max_density, water_dt_cfl, water_dt_accel, water_dt_viscous implicit none integer, parameter :: SPH_OUTPUT_UNIT = 41 @@ -53,12 +55,12 @@ contains end if write(SPH_OUTPUT_UNIT, '(A)') & - 'id,phase,status,material,section,x,y,z,vx,vy,vz,rho,pressure,temperature' + 'id,phase,status,material,section,x,y,z,vx,vy,vz,rho,pressure,temperature,h' do i = 1, npart - write(SPH_OUTPUT_UNIT, '(5(I0,","),6(ES24.16,","),3(ES24.16,:,","))') & + write(SPH_OUTPUT_UNIT, '(5(I0,","),6(ES24.16,","),4(ES24.16,:,","))') & particle_id_ext(i), particle_phase(i), particle_status(i), & material_id(i), section_id(i), x(1,i), x(2,i), x(3,i), & - v(1,i), v(2,i), v(3,i), rho(i), pressure(i), temperature(i) + v(1,i), v(2,i), v(3,i), rho(i), pressure(i), temperature(i), h(i) end do close(unit=SPH_OUTPUT_UNIT) end subroutine sph_output_write_csv @@ -71,6 +73,42 @@ contains write(filename, '(A,"/sphlite_",I8.8,".csv")') trim(output_directory), step call sph_output_write_csv(filename, ierr) + if (ierr /= SPH_SUCCESS) return + write(filename, '(A,"/sphlite_",I8.8,"_summary.csv")') trim(output_directory), step + call sph_output_write_summary(filename, step, time, ierr) end subroutine sph_output_write_step + subroutine sph_output_write_summary(filename, step, time, ierr) + character(len=*), intent(in) :: filename + integer, intent(in) :: step + real(rk), intent(in) :: time + integer, intent(out) :: ierr + integer :: ios + real(rk) :: dtcrit + + ierr = SPH_SUCCESS + dtcrit = water_dt_cfl + if (water_dt_accel > 0.0_rk .and. water_dt_accel < dtcrit) dtcrit = water_dt_accel + if (water_dt_viscous > 0.0_rk .and. water_dt_viscous < dtcrit) dtcrit = water_dt_viscous + open(unit=SPH_OUTPUT_UNIT, file=trim(filename), status='unknown', & + action='write', iostat=ios) + if (ios /= 0) then + call sph_error_set(SPH_ERROR_IO, 'failed to open summary output file') + ierr = SPH_ERROR_IO + return + end if + write(SPH_OUTPUT_UNIT, '(A)') 'metric,value' + write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'step', step + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'time', time + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'water_mass', water_mass + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'water_momentum_x', water_momentum(1) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'water_momentum_y', water_momentum(2) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'water_momentum_z', water_momentum(3) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'max_pressure', water_max_pressure + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'min_density', water_min_density + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'max_density', water_max_density + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'dtcrit', dtcrit + close(unit=SPH_OUTPUT_UNIT) + end subroutine sph_output_write_summary + end module sph_output_csv diff --git a/src/output/sph_restart.f90 b/src/output/sph_restart.f90 index 1521cb2..168a1d7 100644 --- a/src/output/sph_restart.f90 +++ b/src/output/sph_restart.f90 @@ -1,10 +1,19 @@ module sph_restart use sph_precision, only: rk, SPH_VERSION_MAJOR, SPH_VERSION_MINOR, SPH_VERSION_PATCH use sph_error, only: SPH_SUCCESS, SPH_ERROR_IO, SPH_ERROR_INPUT, sph_error_set - use sph_control, only: current_time, current_step, output_interval, output_directory + use sph_control, only: current_time, current_step, output_interval, output_directory, & + output_enabled, safety_factor use sph_database, only: npart, sph_db_allocate, particle_id_ext, particle_phase, & particle_status, material_id, section_id, x0, x, v, a, vhalf, mass, rho, & - volume, pressure, internal_energy, temperature, h + volume, pressure, internal_energy, temperature, h, water_particle_count, & + water_mass, water_momentum, water_max_pressure, water_min_density, & + water_max_density, water_pressure_impulse, water_dt_cfl, water_dt_accel, & + water_dt_viscous + 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, & + sph_water_update_diagnostics + use sph_neighbor, only: neighbor_cell_size, neighbor_cell_dims, neighbor_num_cells, & + sph_neighbor_rebuild implicit none integer, parameter :: SPH_RESTART_UNIT = 43 @@ -27,10 +36,18 @@ contains end if write(SPH_RESTART_UNIT, '(A,1X,I0,1X,I0,1X,I0,1X,A)') 'SPHLITE_RESTART', & - SPH_VERSION_MAJOR, SPH_VERSION_MINOR, SPH_VERSION_PATCH, 'STATE_0_1' + SPH_VERSION_MAJOR, SPH_VERSION_MINOR, SPH_VERSION_PATCH, 'STATE_0_2' write(SPH_RESTART_UNIT, *) current_time, current_step, npart - write(SPH_RESTART_UNIT, *) output_interval + write(SPH_RESTART_UNIT, *) output_interval, output_enabled, safety_factor write(SPH_RESTART_UNIT, '(A)') trim(output_directory) + write(SPH_RESTART_UNIT, *) water_rho0, water_c0, water_gamma, water_p0, water_b + write(SPH_RESTART_UNIT, *) water_visc_alpha, water_visc_beta, water_visc_eta2 + write(SPH_RESTART_UNIT, *) water_body_force(:) + write(SPH_RESTART_UNIT, *) neighbor_cell_size, neighbor_cell_dims(:), neighbor_num_cells + write(SPH_RESTART_UNIT, *) water_particle_count, water_mass + write(SPH_RESTART_UNIT, *) water_momentum(:), water_max_pressure, & + water_min_density, water_max_density, water_pressure_impulse + write(SPH_RESTART_UNIT, *) water_dt_cfl, water_dt_accel, water_dt_viscous do i = 1, npart write(SPH_RESTART_UNIT, *) particle_id_ext(i), particle_phase(i), & particle_status(i), material_id(i), section_id(i) @@ -77,7 +94,7 @@ contains close(unit=SPH_RESTART_UNIT) return end if - read(SPH_RESTART_UNIT, *, iostat=ios) output_interval + read(SPH_RESTART_UNIT, *, iostat=ios) output_interval, output_enabled, safety_factor if (ios /= 0) then call sph_error_set(SPH_ERROR_INPUT, 'invalid restart output interval') ierr = SPH_ERROR_INPUT @@ -91,6 +108,56 @@ contains close(unit=SPH_RESTART_UNIT) return end if + read(SPH_RESTART_UNIT, *, iostat=ios) water_rho0, water_c0, water_gamma, water_p0, water_b + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Water EOS state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + read(SPH_RESTART_UNIT, *, iostat=ios) water_visc_alpha, water_visc_beta, water_visc_eta2 + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Water viscosity state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + read(SPH_RESTART_UNIT, *, iostat=ios) water_body_force(:) + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart body force state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + read(SPH_RESTART_UNIT, *, iostat=ios) neighbor_cell_size, neighbor_cell_dims(:), neighbor_num_cells + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart neighbor state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + read(SPH_RESTART_UNIT, *, iostat=ios) water_particle_count, water_mass + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Water summary state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + read(SPH_RESTART_UNIT, *, iostat=ios) water_momentum(:), water_max_pressure, & + water_min_density, water_max_density, water_pressure_impulse + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Water diagnostic state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + read(SPH_RESTART_UNIT, *, iostat=ios) water_dt_cfl, water_dt_accel, water_dt_viscous + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Water dt state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if call sph_db_allocate(count, ierr) if (ierr /= SPH_SUCCESS) then close(unit=SPH_RESTART_UNIT) @@ -112,6 +179,10 @@ contains call sph_error_set(SPH_ERROR_INPUT, 'restart file ended unexpectedly') ierr = SPH_ERROR_INPUT end if + if (ierr == SPH_SUCCESS) then + call sph_neighbor_rebuild(ierr) + if (ierr == SPH_SUCCESS) call sph_water_update_diagnostics(ierr) + end if close(unit=SPH_RESTART_UNIT) end subroutine sph_restart_read diff --git a/todo.md b/todo.md index 9e222bc..c2b60b5 100644 --- a/todo.md +++ b/todo.md @@ -279,26 +279,26 @@ - [x] 确认 CSV 输出包含 rho 字段。 - [x] 确认 CSV 输出包含 pressure 字段。 - [x] 确认 CSV 输出包含 temperature 字段。 -- [ ] 确认 CSV 输出包含 h 字段。 -- [ ] 增加 Water summary 输出或诊断写出入口。 -- [ ] 输出 step 诊断。 -- [ ] 输出 time 诊断。 -- [ ] 输出 water mass 诊断。 -- [ ] 输出 water momentum 诊断。 -- [ ] 输出 max pressure 诊断。 -- [ ] 输出 min/max density 诊断。 -- [ ] 输出 dtcrit 诊断。 -- [ ] 扩展 restart 头部状态版本到 0.2。 -- [ ] restart 写入 Water EOS 参数。 -- [ ] restart 写入人工黏性参数。 -- [ ] restart 写入 body force 参数。 -- [ ] restart 写入密度状态。 -- [ ] restart 写入压力状态。 -- [ ] restart 写入 `h` 状态。 -- [ ] restart 写入速度、加速度和半步速度。 -- [ ] restart 写入邻域搜索控制参数。 -- [ ] restart 写入 Water 诊断状态。 -- [ ] restart 读入后重建邻域。 +- [x] 确认 CSV 输出包含 h 字段。 +- [x] 增加 Water summary 输出或诊断写出入口。 +- [x] 输出 step 诊断。 +- [x] 输出 time 诊断。 +- [x] 输出 water mass 诊断。 +- [x] 输出 water momentum 诊断。 +- [x] 输出 max pressure 诊断。 +- [x] 输出 min/max density 诊断。 +- [x] 输出 dtcrit 诊断。 +- [x] 扩展 restart 头部状态版本到 0.2。 +- [x] restart 写入 Water EOS 参数。 +- [x] restart 写入人工黏性参数。 +- [x] restart 写入 body force 参数。 +- [x] restart 写入密度状态。 +- [x] restart 写入压力状态。 +- [x] restart 写入 `h` 状态。 +- [x] restart 写入速度、加速度和半步速度。 +- [x] restart 写入邻域搜索控制参数。 +- [x] restart 写入 Water 诊断状态。 +- [x] restart 读入后重建邻域。 - [ ] restart 读入后继续执行 Water 显式步。 - [ ] 增加 0.2.0 output 测试。 - [ ] 增加 0.2.0 restart 测试。