From bb0248ba807c87345add40048e3c59f3829a205e Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 3 Jul 2026 09:47:27 +0800 Subject: [PATCH] Add Gas restart and C ABI diagnostics --- CMakeLists.txt | 9 +++ include/sphlite_c_api.h | 10 ++++ src/gas/sph_gas.f90 | 29 ++++++++++ src/interface/sph_api.f90 | 90 ++++++++++++++++++++++++++++- src/interface/sph_c_api.c | 53 +++++++++++++++++ src/output/sph_restart.f90 | 86 ++++++++++++++++++++++++++- tests/interface/test_c_api.c | 58 ++++++++++++++++++- tests/module/test_gas_restart.f90 | 96 +++++++++++++++++++++++++++++++ tests/module/test_restart.f90 | 25 +++++++- tests/module/test_solver_gas.f90 | 21 ++++++- todo.md | 40 ++++++------- 11 files changed, 490 insertions(+), 27 deletions(-) create mode 100644 tests/module/test_gas_restart.f90 diff --git a/CMakeLists.txt b/CMakeLists.txt index 84cda7c..fe51546 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,9 @@ if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") #define SPHLITE_FC_sphft_read_restart_chars sphft_read_restart_chars_ #define SPHLITE_FC_sphft_get_water_summary sphft_get_water_summary_ #define SPHLITE_FC_sphft_get_water_dt_limits sphft_get_water_dt_limits_ +#define SPHLITE_FC_sphft_get_gas_summary sphft_get_gas_summary_ +#define SPHLITE_FC_sphft_get_gas_bubble_metrics sphft_get_gas_bubble_metrics_ +#define SPHLITE_FC_sphft_get_gas_dt_limits sphft_get_gas_dt_limits_ #endif ") endif() @@ -219,6 +222,12 @@ set_target_properties(test_gas_output PROPERTIES Fortran_MODULE_DIRECTORY "${SPH add_test(NAME gas_output COMMAND test_gas_output) set_tests_properties(gas_output PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_gas_restart tests/module/test_gas_restart.f90) +target_link_libraries(test_gas_restart PRIVATE sphlite_core) +set_target_properties(test_gas_restart PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME gas_restart COMMAND test_gas_restart) +set_tests_properties(gas_restart PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_executable(test_water_density tests/module/test_water_density.f90) target_link_libraries(test_water_density PRIVATE sphlite_core) set_target_properties(test_water_density PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") diff --git a/include/sphlite_c_api.h b/include/sphlite_c_api.h index cb9d050..ceffd87 100644 --- a/include/sphlite_c_api.h +++ b/include/sphlite_c_api.h @@ -71,6 +71,16 @@ SPHLITE_API int sphlite_get_water_summary(double *mass, SPHLITE_API int sphlite_get_water_dt_limits(double *dt_cfl, double *dt_accel, double *dt_viscous); +SPHLITE_API int sphlite_get_gas_summary(double *volume, + double *center, + double *mean_pressure, + double *max_pressure); +SPHLITE_API int sphlite_get_gas_bubble_metrics(double *equivalent_radius, + double *radius_rate, + double *center); +SPHLITE_API int sphlite_get_gas_dt_limits(double *dt_cfl, + double *dt_accel, + double *dtcrit); #ifdef __cplusplus } diff --git a/src/gas/sph_gas.f90 b/src/gas/sph_gas.f90 index 4e7c15e..e8fd17c 100644 --- a/src/gas/sph_gas.f90 +++ b/src/gas/sph_gas.f90 @@ -565,6 +565,35 @@ contains gas_dtcrit = dt_limit end subroutine sph_gas_estimate_dt_limits + subroutine sph_gas_get_summary(volume_out, center_out, mean_pressure_out, & + max_pressure_out, ierr) + real(rk), intent(out) :: volume_out + real(rk), intent(out) :: center_out(SPH_NDIM) + real(rk), intent(out) :: mean_pressure_out + real(rk), intent(out) :: max_pressure_out + integer, intent(out) :: ierr + + call sph_gas_update_diagnostics(ierr) + if (ierr /= SPH_SUCCESS) return + volume_out = gas_volume + center_out = gas_center + mean_pressure_out = gas_mean_pressure + max_pressure_out = gas_max_pressure + end subroutine sph_gas_get_summary + + subroutine sph_gas_get_bubble_metrics(equivalent_radius_out, radius_rate_out, & + center_out, ierr) + real(rk), intent(out) :: equivalent_radius_out + real(rk), intent(out) :: radius_rate_out + real(rk), intent(out) :: center_out(SPH_NDIM) + integer, intent(out) :: ierr + + equivalent_radius_out = gas_equivalent_radius + radius_rate_out = gas_radius_rate + center_out = gas_center + ierr = SPH_SUCCESS + end subroutine sph_gas_get_bubble_metrics + real(rk) function sph_gas_sound_speed(i) integer, intent(in) :: i real(rk) :: pressure_scale diff --git a/src/interface/sph_api.f90 b/src/interface/sph_api.f90 index 778d886..15d5fe4 100644 --- a/src/interface/sph_api.f90 +++ b/src/interface/sph_api.f90 @@ -20,7 +20,8 @@ module sph_api use sph_material, only: sph_material_initialize, sph_material_reset use sph_water, only: sph_water_initialize, sph_water_reset, & sph_water_get_summary, sph_water_estimate_dt_limits - use sph_gas, only: sph_gas_initialize, sph_gas_reset + use sph_gas, only: sph_gas_initialize, sph_gas_reset, & + sph_gas_get_summary, sph_gas_get_bubble_metrics, sph_gas_estimate_dt_limits use sph_explosive, only: sph_explosive_initialize, sph_explosive_reset use sph_solid, only: sph_solid_initialize, sph_solid_reset use sph_shell, only: sph_shell_initialize, sph_shell_reset @@ -348,6 +349,56 @@ contains call sph_water_estimate_dt_limits(dt_cfl, dt_accel, dt_viscous, ierr) end subroutine sph_get_water_dt_limits + subroutine sph_get_gas_summary(volume, center, mean_pressure, max_pressure, ierr) + real(rk), intent(out) :: volume + real(rk), intent(out) :: center(3) + real(rk), intent(out) :: mean_pressure + real(rk), intent(out) :: max_pressure + integer, intent(out) :: ierr + + volume = 0.0_rk + center = 0.0_rk + mean_pressure = 0.0_rk + max_pressure = 0.0_rk + call sph_api_require_initialized(ierr) + if (ierr /= SPH_SUCCESS) return + call sph_api_require_model_built(ierr) + if (ierr /= SPH_SUCCESS) return + call sph_gas_get_summary(volume, center, mean_pressure, max_pressure, ierr) + end subroutine sph_get_gas_summary + + subroutine sph_get_gas_bubble_metrics(equivalent_radius, radius_rate, center, ierr) + real(rk), intent(out) :: equivalent_radius + real(rk), intent(out) :: radius_rate + real(rk), intent(out) :: center(3) + integer, intent(out) :: ierr + + equivalent_radius = 0.0_rk + radius_rate = 0.0_rk + center = 0.0_rk + call sph_api_require_initialized(ierr) + if (ierr /= SPH_SUCCESS) return + call sph_api_require_model_built(ierr) + if (ierr /= SPH_SUCCESS) return + call sph_gas_get_bubble_metrics(equivalent_radius, radius_rate, center, ierr) + end subroutine sph_get_gas_bubble_metrics + + subroutine sph_get_gas_dt_limits(dt_cfl, dt_accel, dtcrit, ierr) + real(rk), intent(out) :: dt_cfl + real(rk), intent(out) :: dt_accel + real(rk), intent(out) :: dtcrit + integer, intent(out) :: ierr + + dt_cfl = 0.0_rk + dt_accel = 0.0_rk + dtcrit = 0.0_rk + call sph_api_require_initialized(ierr) + if (ierr /= SPH_SUCCESS) return + call sph_api_require_model_built(ierr) + if (ierr /= SPH_SUCCESS) return + call sph_gas_estimate_dt_limits(dt_cfl, dt_accel, dtcrit, ierr) + end subroutine sph_get_gas_dt_limits + subroutine sph_api_require_initialized(ierr) integer, intent(out) :: ierr @@ -563,6 +614,43 @@ subroutine sphft_get_water_dt_limits(dt_cfl, dt_accel, dt_viscous, ierr) call sph_get_water_dt_limits(dt_cfl, dt_accel, dt_viscous, ierr) end subroutine sphft_get_water_dt_limits +subroutine sphft_get_gas_summary(volume, center, mean_pressure, max_pressure, ierr) + use sph_api, only: sph_get_gas_summary + use sph_precision, only: rk + implicit none + real(rk), intent(out) :: volume + real(rk), intent(out) :: center(3) + real(rk), intent(out) :: mean_pressure + real(rk), intent(out) :: max_pressure + integer, intent(out) :: ierr + + call sph_get_gas_summary(volume, center, mean_pressure, max_pressure, ierr) +end subroutine sphft_get_gas_summary + +subroutine sphft_get_gas_bubble_metrics(equivalent_radius, radius_rate, center, ierr) + use sph_api, only: sph_get_gas_bubble_metrics + use sph_precision, only: rk + implicit none + real(rk), intent(out) :: equivalent_radius + real(rk), intent(out) :: radius_rate + real(rk), intent(out) :: center(3) + integer, intent(out) :: ierr + + call sph_get_gas_bubble_metrics(equivalent_radius, radius_rate, center, ierr) +end subroutine sphft_get_gas_bubble_metrics + +subroutine sphft_get_gas_dt_limits(dt_cfl, dt_accel, dtcrit, ierr) + use sph_api, only: sph_get_gas_dt_limits + use sph_precision, only: rk + implicit none + real(rk), intent(out) :: dt_cfl + real(rk), intent(out) :: dt_accel + real(rk), intent(out) :: dtcrit + integer, intent(out) :: ierr + + call sph_get_gas_dt_limits(dt_cfl, dt_accel, dtcrit, ierr) +end subroutine sphft_get_gas_dt_limits + subroutine sphft_set_output_directory_chars(chars, count, ierr) use sph_api, only: sph_set_output_directory implicit none diff --git a/src/interface/sph_c_api.c b/src/interface/sph_c_api.c index 0b165f9..ff047f3 100644 --- a/src/interface/sph_c_api.c +++ b/src/interface/sph_c_api.c @@ -62,6 +62,19 @@ extern void SPHLITE_FC_sphft_get_water_summary( int *ierr); extern void SPHLITE_FC_sphft_get_water_dt_limits( double *dt_cfl, double *dt_accel, double *dt_viscous, int *ierr); +extern void SPHLITE_FC_sphft_get_gas_summary( + double *volume, + double *center, + double *mean_pressure, + double *max_pressure, + int *ierr); +extern void SPHLITE_FC_sphft_get_gas_bubble_metrics( + double *equivalent_radius, + double *radius_rate, + double *center, + int *ierr); +extern void SPHLITE_FC_sphft_get_gas_dt_limits( + double *dt_cfl, double *dt_accel, double *dtcrit, int *ierr); static int sphlite_call_path( const char *path, @@ -310,3 +323,43 @@ int sphlite_get_water_dt_limits(double *dt_cfl, SPHLITE_FC_sphft_get_water_dt_limits(dt_cfl, dt_accel, dt_viscous, &ierr); return ierr; } + +int sphlite_get_gas_summary(double *volume, + double *center, + double *mean_pressure, + double *max_pressure) +{ + int ierr = 0; + if (volume == NULL || center == NULL || mean_pressure == NULL || + max_pressure == NULL) { + return -1; + } + SPHLITE_FC_sphft_get_gas_summary(volume, center, mean_pressure, + max_pressure, &ierr); + return ierr; +} + +int sphlite_get_gas_bubble_metrics(double *equivalent_radius, + double *radius_rate, + double *center) +{ + int ierr = 0; + if (equivalent_radius == NULL || radius_rate == NULL || center == NULL) { + return -1; + } + SPHLITE_FC_sphft_get_gas_bubble_metrics(equivalent_radius, radius_rate, + center, &ierr); + return ierr; +} + +int sphlite_get_gas_dt_limits(double *dt_cfl, + double *dt_accel, + double *dtcrit) +{ + int ierr = 0; + if (dt_cfl == NULL || dt_accel == NULL || dtcrit == NULL) { + return -1; + } + SPHLITE_FC_sphft_get_gas_dt_limits(dt_cfl, dt_accel, dtcrit, &ierr); + return ierr; +} diff --git a/src/output/sph_restart.f90 b/src/output/sph_restart.f90 index 168a1d7..f8b8443 100644 --- a/src/output/sph_restart.f90 +++ b/src/output/sph_restart.f90 @@ -8,10 +8,18 @@ module sph_restart 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 + water_dt_viscous, gas_particle_count, gas_mass, gas_volume, gas_momentum, & + gas_center, gas_mean_pressure, gas_max_pressure, gas_min_density, & + gas_max_density, gas_equivalent_radius, gas_previous_equivalent_radius, & + gas_radius_rate, gas_previous_center, gas_center_shift, gas_dt_cfl, & + gas_dt_accel, gas_dtcrit 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_gas, only: gas_eos_type, gas_rho0, gas_gamma, gas_e0, gas_p0, & + gas_jwl_a, gas_jwl_b, gas_jwl_r1, gas_jwl_r2, gas_jwl_omega, & + gas_pinf, gas_default_h, gas_default_mass, gas_default_volume, & + gas_body_force, gas_bubble_history_valid use sph_neighbor, only: neighbor_cell_size, neighbor_cell_dims, neighbor_num_cells, & sph_neighbor_rebuild implicit none @@ -36,7 +44,7 @@ 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_2' + SPH_VERSION_MAJOR, SPH_VERSION_MINOR, SPH_VERSION_PATCH, 'STATE_0_3' write(SPH_RESTART_UNIT, *) current_time, current_step, npart write(SPH_RESTART_UNIT, *) output_interval, output_enabled, safety_factor write(SPH_RESTART_UNIT, '(A)') trim(output_directory) @@ -48,6 +56,16 @@ contains 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 + write(SPH_RESTART_UNIT, *) gas_eos_type, gas_rho0, gas_gamma, gas_e0, gas_p0, & + gas_jwl_a, gas_jwl_b, gas_jwl_r1, gas_jwl_r2, gas_jwl_omega, gas_pinf + write(SPH_RESTART_UNIT, *) gas_default_h, gas_default_mass, gas_default_volume + write(SPH_RESTART_UNIT, *) gas_body_force(:) + write(SPH_RESTART_UNIT, *) gas_particle_count, gas_mass, gas_volume + write(SPH_RESTART_UNIT, *) gas_momentum(:), gas_center(:), gas_mean_pressure, & + gas_max_pressure, gas_min_density, gas_max_density + write(SPH_RESTART_UNIT, *) gas_equivalent_radius, gas_previous_equivalent_radius, & + gas_radius_rate, gas_previous_center(:), gas_center_shift + write(SPH_RESTART_UNIT, *) gas_dt_cfl, gas_dt_accel, gas_dtcrit do i = 1, npart write(SPH_RESTART_UNIT, *) particle_id_ext(i), particle_phase(i), & particle_status(i), material_id(i), section_id(i) @@ -163,7 +181,69 @@ contains close(unit=SPH_RESTART_UNIT) return end if - + if (trim(state_version) == 'STATE_0_3') then + read(SPH_RESTART_UNIT, *, iostat=ios) gas_eos_type, gas_rho0, gas_gamma, & + gas_e0, gas_p0, gas_jwl_a, gas_jwl_b, gas_jwl_r1, gas_jwl_r2, & + gas_jwl_omega, gas_pinf + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Gas EOS state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + read(SPH_RESTART_UNIT, *, iostat=ios) gas_default_h, gas_default_mass, & + gas_default_volume + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Gas particle control state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + read(SPH_RESTART_UNIT, *, iostat=ios) gas_body_force(:) + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Gas body force state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + read(SPH_RESTART_UNIT, *, iostat=ios) gas_particle_count, gas_mass, gas_volume + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Gas summary state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + read(SPH_RESTART_UNIT, *, iostat=ios) gas_momentum(:), gas_center(:), & + gas_mean_pressure, gas_max_pressure, gas_min_density, gas_max_density + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Gas diagnostic state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + read(SPH_RESTART_UNIT, *, iostat=ios) gas_equivalent_radius, & + gas_previous_equivalent_radius, gas_radius_rate, gas_previous_center(:), & + gas_center_shift + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Gas bubble state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + read(SPH_RESTART_UNIT, *, iostat=ios) gas_dt_cfl, gas_dt_accel, gas_dtcrit + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Gas dt state') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if + gas_bubble_history_valid = gas_particle_count > 0 + else if (trim(state_version) /= 'STATE_0_2') then + call sph_error_set(SPH_ERROR_INPUT, 'unsupported restart state version') + ierr = SPH_ERROR_INPUT + close(unit=SPH_RESTART_UNIT) + return + end if do i = 1, npart read(SPH_RESTART_UNIT, *, iostat=ios) particle_id_ext(i), particle_phase(i), & particle_status(i), material_id(i), section_id(i) diff --git a/tests/interface/test_c_api.c b/tests/interface/test_c_api.c index 56837be..bc364c4 100644 --- a/tests/interface/test_c_api.c +++ b/tests/interface/test_c_api.c @@ -41,8 +41,16 @@ int main(void) double dt_cfl = 0.0; double dt_accel = 0.0; double dt_viscous = 0.0; + double gas_volume = 0.0; + double gas_center[3] = {0.0, 0.0, 0.0}; + double gas_mean_pressure = 0.0; + double gas_max_pressure = 0.0; + double gas_radius = 0.0; + double gas_radius_rate = 0.0; + double gas_dtcrit = 0.0; - if (SPHLITE_PHASE_SOLID != 4 || SPHLITE_STATUS_ACTIVE != 0) { + if (SPHLITE_PHASE_SOLID != 4 || SPHLITE_PHASE_GAS != 2 || + SPHLITE_STATUS_ACTIVE != 0) { return fail("C constants are not locked"); } if (sphlite_get_particle_count(NULL) != -1) { @@ -68,6 +76,27 @@ int main(void) if (sphlite_get_water_dt_limits(&dt_cfl, &dt_accel, NULL) != -1) { return fail("null water dt viscous pointer did not return -1"); } + if (sphlite_get_gas_summary(NULL, gas_center, &gas_mean_pressure, + &gas_max_pressure) != -1) { + return fail("null gas summary volume pointer did not return -1"); + } + if (sphlite_get_gas_summary(&gas_volume, NULL, &gas_mean_pressure, + &gas_max_pressure) != -1) { + return fail("null gas summary center pointer did not return -1"); + } + if (sphlite_get_gas_bubble_metrics(NULL, &gas_radius_rate, gas_center) != -1) { + return fail("null gas radius pointer did not return -1"); + } + if (sphlite_get_gas_bubble_metrics(&gas_radius, &gas_radius_rate, NULL) != -1) { + return fail("null gas bubble center pointer did not return -1"); + } + if (sphlite_get_gas_dt_limits(NULL, &dt_accel, &gas_dtcrit) != -1) { + return fail("null gas dt cfl pointer did not return -1"); + } + if (sphlite_get_gas_summary(&gas_volume, gas_center, &gas_mean_pressure, + &gas_max_pressure) != 2) { + return fail("uninitialized gas summary did not return Fortran state error"); + } ierr = sphlite_initialize(); if (ierr != 0) return fail("initialize failed"); @@ -75,6 +104,11 @@ int main(void) ierr = sphlite_get_water_summary(&water_mass, water_momentum, &max_pressure, &min_density, &max_density); if (ierr != 2) return fail("unbuilt water summary did not return Fortran state error"); + ierr = sphlite_get_gas_summary(&gas_volume, gas_center, &gas_mean_pressure, + &gas_max_pressure); + if (ierr != 2) return fail("unbuilt gas summary did not return Fortran state error"); + ierr = sphlite_get_gas_dt_limits(&dt_cfl, &dt_accel, &gas_dtcrit); + if (ierr != 2) return fail("unbuilt gas dt did not return Fortran state error"); ierr = sphlite_get_version(&major, &minor, &patch); if (ierr != 0) return fail("get_version failed"); @@ -159,5 +193,27 @@ int main(void) ierr = sphlite_finalize(); if (ierr != 0) return fail("finalize water failed"); + + ierr = sphlite_initialize(); + if (ierr != 0) return fail("initialize gas failed"); + snprintf(path, sizeof(path), "%s/gas_ideal_minimal_030.inp", SPHLITE_TEST_DATA_DIR); + ierr = sphlite_read_inp(path); + if (ierr != 0) return fail("read gas input failed"); + ierr = sphlite_build_model(); + if (ierr != 0) return fail("build gas model failed"); + ierr = sphlite_get_gas_summary(&gas_volume, gas_center, &gas_mean_pressure, + &gas_max_pressure); + if (ierr != 0) return fail("gas summary query failed"); + if (gas_volume <= 0.0 || gas_max_pressure <= 0.0) { + return fail("gas summary content mismatch"); + } + ierr = sphlite_get_gas_bubble_metrics(&gas_radius, &gas_radius_rate, gas_center); + if (ierr != 0) return fail("gas bubble query failed"); + if (gas_radius <= 0.0) return fail("gas bubble content mismatch"); + ierr = sphlite_get_gas_dt_limits(&dt_cfl, &dt_accel, &gas_dtcrit); + if (ierr != 0) return fail("gas dt query failed"); + if (dt_cfl <= 0.0 || gas_dtcrit <= 0.0) return fail("gas dt content mismatch"); + ierr = sphlite_finalize(); + if (ierr != 0) return fail("finalize gas failed"); return 0; } diff --git a/tests/module/test_gas_restart.f90 b/tests/module/test_gas_restart.f90 new file mode 100644 index 0000000..efba7dc --- /dev/null +++ b/tests/module/test_gas_restart.f90 @@ -0,0 +1,96 @@ +program test_gas_restart + 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_advance_explicit, sph_write_restart, sph_read_restart, & + sph_get_gas_summary, sph_get_gas_bubble_metrics, sph_get_gas_dt_limits + use sph_database, only: particle_phase, gas_volume, gas_center, & + gas_equivalent_radius, gas_previous_equivalent_radius, gas_radius_rate, & + gas_dtcrit, SPH_PHASE_GAS + use sph_gas, only: gas_eos_type, gas_default_h, gas_default_mass, & + gas_default_volume, gas_body_force + implicit none + + integer :: ierr + integer :: eos_type_before + real(rk) :: volume_before + real(rk) :: center_before(3) + real(rk) :: radius_before + real(rk) :: previous_radius_before + real(rk) :: radius_rate_before + real(rk) :: dtcrit_before + real(rk) :: api_volume + real(rk) :: api_center(3) + real(rk) :: api_mean_pressure + real(rk) :: api_max_pressure + real(rk) :: api_radius + real(rk) :: api_radius_rate + real(rk) :: api_dt_cfl + real(rk) :: api_dt_accel + real(rk) :: api_dtcrit + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Gas restart') + call sph_read_inp('tests/data/gas_ideal_minimal_030.inp', ierr) + call assert_ok(ierr, 'Gas restart input read failed') + call sph_build_model(ierr) + call assert_ok(ierr, 'Gas restart build failed') + call sph_advance_explicit(1.0e-6_rk, ierr) + call assert_ok(ierr, 'Gas restart advance failed') + eos_type_before = gas_eos_type + volume_before = gas_volume + center_before = gas_center + radius_before = gas_equivalent_radius + previous_radius_before = gas_previous_equivalent_radius + radius_rate_before = gas_radius_rate + dtcrit_before = gas_dtcrit + if (gas_default_h <= 0.0_rk .or. gas_default_mass <= 0.0_rk .or. & + gas_default_volume <= 0.0_rk) call fail('Gas restart defaults missing') + if (maxval(abs(gas_body_force)) > 1.0e-12_rk) call fail('Gas body force restart mismatch') + call sph_write_restart('build-codex-unix/restart_gas.tmp', ierr) + call assert_ok(ierr, 'Gas restart write failed') + call sph_read_restart('build-codex-unix/restart_gas.tmp', ierr) + call assert_ok(ierr, 'Gas restart read failed') + if (particle_phase(1) /= SPH_PHASE_GAS) call fail('Gas restart phase mismatch') + if (gas_eos_type /= eos_type_before) call fail('Gas restart EOS type mismatch') + if (abs(gas_volume - volume_before) > 1.0e-12_rk) call fail('Gas restart volume mismatch') + if (maxval(abs(gas_center - center_before)) > 1.0e-12_rk) call fail('Gas restart center mismatch') + if (abs(gas_equivalent_radius - radius_before) > 1.0e-12_rk) then + call fail('Gas restart equivalent radius mismatch') + end if + if (abs(gas_previous_equivalent_radius - previous_radius_before) > 1.0e-12_rk) then + call fail('Gas restart previous radius mismatch') + end if + if (abs(gas_radius_rate - radius_rate_before) > 1.0e-12_rk) call fail('Gas restart radius rate mismatch') + if (abs(gas_dtcrit - dtcrit_before) > 1.0e-12_rk) call fail('Gas restart dtcrit mismatch') + call sph_get_gas_summary(api_volume, api_center, api_mean_pressure, api_max_pressure, ierr) + call assert_ok(ierr, 'Gas summary API after restart failed') + if (api_volume <= 0.0_rk .or. api_max_pressure <= 0.0_rk) call fail('Gas API summary mismatch') + call sph_get_gas_bubble_metrics(api_radius, api_radius_rate, api_center, ierr) + call assert_ok(ierr, 'Gas bubble API after restart failed') + if (api_radius <= 0.0_rk) call fail('Gas API bubble mismatch') + call sph_get_gas_dt_limits(api_dt_cfl, api_dt_accel, api_dtcrit, ierr) + call assert_ok(ierr, 'Gas dt API after restart failed') + if (api_dt_cfl <= 0.0_rk .or. api_dtcrit <= 0.0_rk) call fail('Gas API dt mismatch') + call sph_advance_explicit(1.0e-6_rk, ierr) + call assert_ok(ierr, 'Gas restart continue step failed') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Gas restart') + +contains + + 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_gas_restart diff --git a/tests/module/test_restart.f90 b/tests/module/test_restart.f90 index dc75c77..837bf5e 100644 --- a/tests/module/test_restart.f90 +++ b/tests/module/test_restart.f90 @@ -141,6 +141,15 @@ contains real(rk) :: water_mass_value real(rk) :: water_diag_values(7) real(rk) :: dt_values(3) + integer :: gas_eos_type_value + integer :: gas_count_value + real(rk) :: gas_eos_values(10) + real(rk) :: gas_control_values(3) + real(rk) :: gas_body_values(3) + real(rk) :: gas_summary_values(2) + real(rk) :: gas_diag_values(10) + real(rk) :: gas_bubble_values(6) + real(rk) :: gas_dt_values(3) real(rk) :: rx0(3) real(rk) :: rx(3) real(rk) :: rv(3) @@ -158,7 +167,7 @@ contains 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') + if (trim(state_version) /= 'STATE_0_3') 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') @@ -213,6 +222,20 @@ contains 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) gas_eos_type_value, gas_eos_values + if (ios /= 0) call fail('restart Gas EOS read failed') + read(74, *, iostat=ios) gas_control_values + if (ios /= 0) call fail('restart Gas control read failed') + read(74, *, iostat=ios) gas_body_values + if (ios /= 0) call fail('restart Gas body force read failed') + read(74, *, iostat=ios) gas_count_value, gas_summary_values + if (ios /= 0) call fail('restart Gas summary read failed') + read(74, *, iostat=ios) gas_diag_values + if (ios /= 0) call fail('restart Gas diagnostics read failed') + read(74, *, iostat=ios) gas_bubble_values + if (ios /= 0) call fail('restart Gas bubble read failed') + read(74, *, iostat=ios) gas_dt_values + if (ios /= 0) call fail('restart Gas 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') diff --git a/tests/module/test_solver_gas.f90 b/tests/module/test_solver_gas.f90 index 6f2d81c..d254a04 100644 --- a/tests/module/test_solver_gas.f90 +++ b/tests/module/test_solver_gas.f90 @@ -2,7 +2,8 @@ program test_solver_gas 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_advance_explicit, sph_estimate_stable_dt, sph_get_current_time + sph_advance_explicit, sph_estimate_stable_dt, sph_get_current_time, & + sph_get_gas_summary, sph_get_gas_bubble_metrics, sph_get_gas_dt_limits use sph_database, only: x, v, a, rho, pressure, gas_dtcrit, gas_particle_count, & water_mass implicit none @@ -19,6 +20,15 @@ contains subroutine check_gas_single_step() real(rk) :: dtcrit real(rk) :: x_before(3) + real(rk) :: api_volume + real(rk) :: api_center(3) + real(rk) :: api_mean_pressure + real(rk) :: api_max_pressure + real(rk) :: api_radius + real(rk) :: api_radius_rate + real(rk) :: api_dt_cfl + real(rk) :: api_dt_accel + real(rk) :: api_dtcrit call sph_initialize(ierr) call assert_ok(ierr, 'initialize failed for Gas solver') @@ -32,6 +42,15 @@ contains call assert_ok(ierr, 'Gas dt estimate failed') if (dtcrit <= 0.0_rk) call fail('Gas dtcrit is not positive') if (gas_dtcrit <= 0.0_rk) call fail('Gas dt diagnostic is not positive') + call sph_get_gas_summary(api_volume, api_center, api_mean_pressure, api_max_pressure, ierr) + call assert_ok(ierr, 'Gas summary API failed') + if (api_volume <= 0.0_rk .or. api_max_pressure <= 0.0_rk) call fail('Gas summary API mismatch') + call sph_get_gas_bubble_metrics(api_radius, api_radius_rate, api_center, ierr) + call assert_ok(ierr, 'Gas bubble API failed') + if (api_radius <= 0.0_rk) call fail('Gas bubble API mismatch') + call sph_get_gas_dt_limits(api_dt_cfl, api_dt_accel, api_dtcrit, ierr) + call assert_ok(ierr, 'Gas dt API failed') + if (api_dt_cfl <= 0.0_rk .or. api_dtcrit <= 0.0_rk) call fail('Gas dt API mismatch') call sph_advance_explicit(1.0e-6_rk, ierr) call assert_ok(ierr, 'Gas explicit advance failed') call sph_get_current_time(time, step, ierr) diff --git a/todo.md b/todo.md index f64ccac..a5ee69e 100644 --- a/todo.md +++ b/todo.md @@ -166,29 +166,29 @@ ## 阶段 M:Gas Restart -- [ ] 更新 restart 状态版本策略。 -- [ ] 写入 Gas EOS 类型和参数。 -- [ ] 写入 Gas 默认粒子控制参数。 -- [ ] 写入 Gas 外部体力。 -- [ ] 写入 Gas summary 诊断状态。 -- [ ] 写入 bubble metrics 历史状态。 -- [ ] 写入 Gas 稳定时间步诊断。 -- [ ] 读回 Gas restart 后重建邻域。 -- [ ] 读回 Gas restart 后继续显式步。 -- [ ] 增加 `gas_restart` 测试目标并接入 CTest。 +- [x] 更新 restart 状态版本策略。 +- [x] 写入 Gas EOS 类型和参数。 +- [x] 写入 Gas 默认粒子控制参数。 +- [x] 写入 Gas 外部体力。 +- [x] 写入 Gas summary 诊断状态。 +- [x] 写入 bubble metrics 历史状态。 +- [x] 写入 Gas 稳定时间步诊断。 +- [x] 读回 Gas restart 后重建邻域。 +- [x] 读回 Gas restart 后继续显式步。 +- [x] 增加 `gas_restart` 测试目标并接入 CTest。 ## 阶段 N:Fortran API 与 C ABI -- [ ] 增加 Fortran `sph_get_gas_summary`。 -- [ ] 增加 Fortran `sph_get_gas_bubble_metrics`。 -- [ ] 增加 Fortran `sph_get_gas_dt_limits`。 -- [ ] 增加 C ABI `sphlite_get_gas_summary`。 -- [ ] 增加 C ABI `sphlite_get_gas_bubble_metrics`。 -- [ ] 增加 C ABI `sphlite_get_gas_dt_limits`。 -- [ ] 为 Gas C ABI 增加空指针错误测试。 -- [ ] 为 Gas C ABI 增加未初始化错误测试。 -- [ ] 为 Gas C ABI 增加未 build 错误测试。 -- [ ] 更新 C API 测试目标以覆盖 Gas 诊断。 +- [x] 增加 Fortran `sph_get_gas_summary`。 +- [x] 增加 Fortran `sph_get_gas_bubble_metrics`。 +- [x] 增加 Fortran `sph_get_gas_dt_limits`。 +- [x] 增加 C ABI `sphlite_get_gas_summary`。 +- [x] 增加 C ABI `sphlite_get_gas_bubble_metrics`。 +- [x] 增加 C ABI `sphlite_get_gas_dt_limits`。 +- [x] 为 Gas C ABI 增加空指针错误测试。 +- [x] 为 Gas C ABI 增加未初始化错误测试。 +- [x] 为 Gas C ABI 增加未 build 错误测试。 +- [x] 更新 C API 测试目标以覆盖 Gas 诊断。 ## 阶段 O:C++、Python 和 CLI