diff --git a/CMakeLists.txt b/CMakeLists.txt index 78b6acf..795a144 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,8 @@ if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") #define SPHLITE_FC_sphft_get_gas_dt_limits sphft_get_gas_dt_limits_ #define SPHLITE_FC_sphft_get_gas_extended_summary sphft_get_gas_extended_summary_ #define SPHLITE_FC_sphft_get_gas_source_summary sphft_get_gas_source_summary_ +#define SPHLITE_FC_sphft_set_ignition_point sphft_set_ignition_point_ +#define SPHLITE_FC_sphft_get_explosive_summary sphft_get_explosive_summary_ #endif ") endif() diff --git a/Python/sphlite_debug.py b/Python/sphlite_debug.py index eac5cad..dedb7d2 100644 --- a/Python/sphlite_debug.py +++ b/Python/sphlite_debug.py @@ -120,6 +120,23 @@ class SPHLite: ] self.lib.sphlite_get_gas_source_summary.restype = ctypes.c_int + self.lib.sphlite_set_ignition_point.argtypes = [ + ctypes.c_double, + ctypes.c_double, + ctypes.c_double, + ctypes.c_double, + ] + self.lib.sphlite_set_ignition_point.restype = ctypes.c_int + + self.lib.sphlite_get_explosive_summary.argtypes = [ + ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ] + self.lib.sphlite_get_explosive_summary.restype = ctypes.c_int + self.lib.sphlite_get_particle_state.argtypes = [ ctypes.c_int, ctypes.POINTER(ctypes.c_int), @@ -384,6 +401,38 @@ class SPHLite: "gas_source_energy": float(energy_source.value), } + def set_ignition_point(self, x: float, y: float, z: float, time: float) -> None: + self._check(self.lib.sphlite_set_ignition_point( + float(x), + float(y), + float(z), + float(time), + )) + + def explosive_summary(self) -> dict[str, int | float | list[float]]: + particle_count = ctypes.c_int(0) + reacted_count = ctypes.c_int(0) + lambda_type = ctypes.c_double * 3 + released_type = ctypes.c_double * 5 + time_type = ctypes.c_double * 2 + lambda_range = lambda_type() + released_totals = released_type() + detonation_range = time_type() + self._check(self.lib.sphlite_get_explosive_summary( + ctypes.byref(particle_count), + ctypes.byref(reacted_count), + lambda_range, + released_totals, + detonation_range, + )) + return { + "explosive_particles": int(particle_count.value), + "explosive_reacted_particles": int(reacted_count.value), + "explosive_lambda_range": list(lambda_range), + "explosive_released_totals": list(released_totals), + "explosive_detonation_time_range": list(detonation_range), + } + def inspect(self, filename: str | Path) -> dict[str, int | float | list[float]]: self.read_inp(filename) self.build_model() @@ -398,6 +447,7 @@ class SPHLite: summary.update(self.gas_dt_limits()) summary.update(self.gas_extended_summary()) summary.update(self.gas_source_summary()) + summary.update(self.explosive_summary()) return summary def run(self, filename: str | Path) -> dict[str, int | float | list[float]]: @@ -411,4 +461,5 @@ class SPHLite: summary.update(self.gas_dt_limits()) summary.update(self.gas_extended_summary()) summary.update(self.gas_source_summary()) + summary.update(self.explosive_summary()) return summary diff --git a/include/sphlite.hpp b/include/sphlite.hpp index c9c0def..1d2c283 100644 --- a/include/sphlite.hpp +++ b/include/sphlite.hpp @@ -96,6 +96,14 @@ struct GasSourceSummary { double energy_source; }; +struct ExplosiveSummary { + int particle_count; + int reacted_count; + double lambda_range[3]; + double released_totals[5]; + double detonation_time_range[2]; +}; + class Error : public std::runtime_error { public: explicit Error(const std::string &message) : std::runtime_error(message) {} @@ -240,6 +248,21 @@ inline GasSourceSummary gas_source_summary() return summary; } +inline void set_ignition_point(double x, double y, double z, double time) +{ + check(sphlite_set_ignition_point(x, y, z, time)); +} + +inline ExplosiveSummary explosive_summary() +{ + ExplosiveSummary summary = { + 0, 0, {0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 0.0}}; + check(sphlite_get_explosive_summary(&summary.particle_count, + &summary.reacted_count, summary.lambda_range, summary.released_totals, + summary.detonation_time_range)); + return summary; +} + inline void set_output_directory(const std::string &path) { check(sphlite_set_output_directory(path.c_str())); diff --git a/include/sphlite_c_api.h b/include/sphlite_c_api.h index a056232..ea2ce78 100644 --- a/include/sphlite_c_api.h +++ b/include/sphlite_c_api.h @@ -88,6 +88,12 @@ SPHLITE_API int sphlite_get_gas_extended_summary(double *mass, SPHLITE_API int sphlite_get_gas_source_summary(double *mass_source, double *momentum_source, double *energy_source); +SPHLITE_API int sphlite_set_ignition_point(double x, double y, double z, double time); +SPHLITE_API int sphlite_get_explosive_summary(int *particle_count, + int *reacted_count, + double *lambda_range, + double *released_totals, + double *detonation_time_range); #ifdef __cplusplus } diff --git a/src/app/sph_main.c b/src/app/sph_main.c index d93a298..351ee35 100644 --- a/src/app/sph_main.c +++ b/src/app/sph_main.c @@ -106,6 +106,9 @@ int main(int argc, char **argv) int warnings = 0; int water_particles = 0; int gas_particles = 0; + int explosive_phase_particles = 0; + int explosive_particles = 0; + int explosive_reacted_particles = 0; double time = 0.0; double water_mass = 0.0; double water_momentum[3] = {0.0, 0.0, 0.0}; @@ -129,6 +132,9 @@ int main(int argc, char **argv) double gas_source_mass = 0.0; double gas_source_momentum[3] = {0.0, 0.0, 0.0}; double gas_source_energy = 0.0; + double explosive_lambda_range[3] = {0.0, 0.0, 0.0}; + double explosive_released_totals[5] = {0.0, 0.0, 0.0, 0.0, 0.0}; + double explosive_detonation_time_range[2] = {0.0, 0.0}; int run_mode = 0; int check_mode = 0; const char *input = NULL; @@ -258,6 +264,13 @@ int main(int argc, char **argv) (void)sphlite_finalize(); return 1; } + ierr = sphlite_count_phase_particles(count, SPHLITE_PHASE_EXPLOSIVE, + &explosive_phase_particles); + if (ierr != 0) { + sphlite_print_error(); + (void)sphlite_finalize(); + return 1; + } ierr = sphlite_get_water_summary(&water_mass, water_momentum, &max_pressure, &min_density, &max_density); if (ierr != 0) { @@ -305,6 +318,14 @@ int main(int argc, char **argv) (void)sphlite_finalize(); return 1; } + ierr = sphlite_get_explosive_summary(&explosive_particles, + &explosive_reacted_particles, explosive_lambda_range, + explosive_released_totals, explosive_detonation_time_range); + if (ierr != 0) { + sphlite_print_error(); + (void)sphlite_finalize(); + return 1; + } printf("version: %d.%d.%d\n", major, minor, patch); printf("input: %s\n", input); @@ -340,6 +361,19 @@ int main(int argc, char **argv) printf("gas_source_momentum_x: %.16e\n", gas_source_momentum[0]); printf("gas_source_momentum_y: %.16e\n", gas_source_momentum[1]); printf("gas_source_momentum_z: %.16e\n", gas_source_momentum[2]); + printf("explosive_phase_particles: %d\n", explosive_phase_particles); + printf("explosive_particles: %d\n", explosive_particles); + printf("explosive_reacted_particles: %d\n", explosive_reacted_particles); + printf("explosive_lambda_min: %.16e\n", explosive_lambda_range[0]); + printf("explosive_lambda_max: %.16e\n", explosive_lambda_range[1]); + printf("explosive_lambda_mean: %.16e\n", explosive_lambda_range[2]); + printf("explosive_released_mass: %.16e\n", explosive_released_totals[0]); + printf("explosive_released_energy: %.16e\n", explosive_released_totals[1]); + printf("explosive_released_momentum_x: %.16e\n", explosive_released_totals[2]); + printf("explosive_released_momentum_y: %.16e\n", explosive_released_totals[3]); + printf("explosive_released_momentum_z: %.16e\n", explosive_released_totals[4]); + printf("explosive_detonation_time_min: %.16e\n", explosive_detonation_time_range[0]); + printf("explosive_detonation_time_max: %.16e\n", explosive_detonation_time_range[1]); printf("status: ok\n"); ierr = sphlite_finalize(); diff --git a/src/explosive/sph_explosive.f90 b/src/explosive/sph_explosive.f90 index 88b0aa0..20c5245 100644 --- a/src/explosive/sph_explosive.f90 +++ b/src/explosive/sph_explosive.f90 @@ -501,7 +501,12 @@ contains ierr = SPH_SUCCESS product_id = explosive_product_material_id(explosive_index) - if (product_id < 1 .or. product_id > material_count) then + if (product_id < 1 .or. product_id > SPH_MAX_MATERIALS) then + call sph_error_set(SPH_ERROR_INPUT, 'Explosive product material is invalid') + ierr = SPH_ERROR_INPUT + return + end if + if (material_count > 0 .and. product_id > material_count) then call sph_error_set(SPH_ERROR_INPUT, 'Explosive product material is invalid') ierr = SPH_ERROR_INPUT return @@ -535,6 +540,78 @@ contains explosive_dt_reaction = dt_reaction end subroutine sph_explosive_estimate_dt_limit + subroutine sph_explosive_get_summary(particle_count, reacted_count, & + lambda_range, released_totals, detonation_time_range, ierr) + integer, intent(out) :: particle_count + integer, intent(out) :: reacted_count + real(rk), intent(out) :: lambda_range(3) + real(rk), intent(out) :: released_totals(5) + real(rk), intent(out) :: detonation_time_range(2) + integer, intent(out) :: ierr + integer :: i + logical :: first_value + + ierr = SPH_SUCCESS + particle_count = 0 + reacted_count = 0 + lambda_range = 0.0_rk + released_totals = 0.0_rk + detonation_time_range = 0.0_rk + first_value = .true. + if (.not. allocated(explosive_reaction_progress)) return + do i = 1, npart + if (.not. sph_explosive_is_tracked_particle(i)) cycle + particle_count = particle_count + 1 + if (explosive_reaction_progress(i) > 0.0_rk) reacted_count = reacted_count + 1 + lambda_range(3) = lambda_range(3) + explosive_reaction_progress(i) + released_totals(1) = released_totals(1) + explosive_released_mass(i) + released_totals(2) = released_totals(2) + explosive_released_energy(i) + released_totals(3:5) = released_totals(3:5) + explosive_released_momentum(:,i) + if (first_value) then + lambda_range(1) = explosive_reaction_progress(i) + lambda_range(2) = explosive_reaction_progress(i) + detonation_time_range(1) = explosive_detonation_time(i) + detonation_time_range(2) = explosive_detonation_time(i) + first_value = .false. + else + if (explosive_reaction_progress(i) < lambda_range(1)) then + lambda_range(1) = explosive_reaction_progress(i) + end if + if (explosive_reaction_progress(i) > lambda_range(2)) then + lambda_range(2) = explosive_reaction_progress(i) + end if + if (explosive_detonation_time(i) < detonation_time_range(1)) then + detonation_time_range(1) = explosive_detonation_time(i) + end if + if (explosive_detonation_time(i) > detonation_time_range(2)) then + detonation_time_range(2) = explosive_detonation_time(i) + end if + end if + end do + if (particle_count > 0) lambda_range(3) = & + lambda_range(3) / real(particle_count, rk) + end subroutine sph_explosive_get_summary + + subroutine sph_explosive_set_default_ignition_point(ignition_x, & + ignition_time, ierr) + real(rk), intent(in) :: ignition_x(SPH_NDIM) + real(rk), intent(in) :: ignition_time + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + if (explosive_material_count <= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'no Explosive material is available') + ierr = SPH_ERROR_INPUT + return + end if + call sph_explosive_set_ignition(explosive_material_name(1), ignition_x, & + ignition_time, ierr) + if (ierr /= SPH_SUCCESS) return + if (allocated(explosive_detonation_time)) then + call sph_explosive_compute_detonation_times(ierr) + end if + end subroutine sph_explosive_set_default_ignition_point + subroutine sph_explosive_get_detonation_time_range(time_min, time_max, ierr) real(rk), intent(out) :: time_min real(rk), intent(out) :: time_max @@ -731,6 +808,21 @@ contains sph_explosive_is_active_particle = .true. end function sph_explosive_is_active_particle + logical function sph_explosive_is_tracked_particle(index) + integer, intent(in) :: index + + sph_explosive_is_tracked_particle = .false. + if (index < 1 .or. index > npart) return + if (.not. allocated(explosive_ignition_status)) return + if (explosive_ignition_status(index) == 1) then + sph_explosive_is_tracked_particle = .true. + else if (allocated(explosive_reaction_progress)) then + if (explosive_reaction_progress(index) > 0.0_rk) then + sph_explosive_is_tracked_particle = .true. + end if + end if + end function sph_explosive_is_tracked_particle + logical function sph_explosive_is_finite(value) real(rk), intent(in) :: value diff --git a/src/interface/sph_api.f90 b/src/interface/sph_api.f90 index 6de88cd..30eee16 100644 --- a/src/interface/sph_api.f90 +++ b/src/interface/sph_api.f90 @@ -24,7 +24,8 @@ module sph_api sph_gas_get_summary, sph_gas_get_extended_summary, & sph_gas_get_source_summary, sph_gas_get_bubble_metrics, & sph_gas_estimate_dt_limits - use sph_explosive, only: sph_explosive_initialize, sph_explosive_reset + use sph_explosive, only: sph_explosive_initialize, sph_explosive_reset, & + sph_explosive_get_summary, sph_explosive_set_default_ignition_point use sph_solid, only: sph_solid_initialize, sph_solid_reset use sph_shell, only: sph_shell_initialize, sph_shell_reset use sph_api_state, only: sph_api_initialized, sph_api_model_built, & @@ -439,6 +440,42 @@ contains energy_source, ierr) end subroutine sph_get_gas_source_summary + subroutine sph_set_ignition_point(x_value, y_value, z_value, time_value, ierr) + real(rk), intent(in) :: x_value + real(rk), intent(in) :: y_value + real(rk), intent(in) :: z_value + real(rk), intent(in) :: time_value + integer, intent(out) :: ierr + real(rk) :: ignition_x(3) + + call sph_api_require_initialized(ierr) + if (ierr /= SPH_SUCCESS) return + ignition_x = (/x_value, y_value, z_value/) + call sph_explosive_set_default_ignition_point(ignition_x, time_value, ierr) + end subroutine sph_set_ignition_point + + subroutine sph_get_explosive_summary(particle_count, reacted_count, & + lambda_range, released_totals, detonation_time_range, ierr) + integer, intent(out) :: particle_count + integer, intent(out) :: reacted_count + real(rk), intent(out) :: lambda_range(3) + real(rk), intent(out) :: released_totals(5) + real(rk), intent(out) :: detonation_time_range(2) + integer, intent(out) :: ierr + + particle_count = 0 + reacted_count = 0 + lambda_range = 0.0_rk + released_totals = 0.0_rk + detonation_time_range = 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_explosive_get_summary(particle_count, reacted_count, & + lambda_range, released_totals, detonation_time_range, ierr) + end subroutine sph_get_explosive_summary + subroutine sph_api_require_initialized(ierr) integer, intent(out) :: ierr @@ -720,6 +757,35 @@ subroutine sphft_get_gas_source_summary(mass_source, momentum_source, & energy_source, ierr) end subroutine sphft_get_gas_source_summary +subroutine sphft_set_ignition_point(x_value, y_value, z_value, time_value, ierr) + use sph_api, only: sph_set_ignition_point + use sph_precision, only: rk + implicit none + real(rk), intent(in) :: x_value + real(rk), intent(in) :: y_value + real(rk), intent(in) :: z_value + real(rk), intent(in) :: time_value + integer, intent(out) :: ierr + + call sph_set_ignition_point(x_value, y_value, z_value, time_value, ierr) +end subroutine sphft_set_ignition_point + +subroutine sphft_get_explosive_summary(particle_count, reacted_count, & + lambda_range, released_totals, detonation_time_range, ierr) + use sph_api, only: sph_get_explosive_summary + use sph_precision, only: rk + implicit none + integer, intent(out) :: particle_count + integer, intent(out) :: reacted_count + real(rk), intent(out) :: lambda_range(3) + real(rk), intent(out) :: released_totals(5) + real(rk), intent(out) :: detonation_time_range(2) + integer, intent(out) :: ierr + + call sph_get_explosive_summary(particle_count, reacted_count, & + lambda_range, released_totals, detonation_time_range, ierr) +end subroutine sphft_get_explosive_summary + 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 1b501a1..29d8c44 100644 --- a/src/interface/sph_c_api.c +++ b/src/interface/sph_c_api.c @@ -86,6 +86,16 @@ extern void SPHLITE_FC_sphft_get_gas_source_summary( double *momentum_source, double *energy_source, int *ierr); +extern void SPHLITE_FC_sphft_set_ignition_point( + double *x_value, double *y_value, double *z_value, double *time_value, + int *ierr); +extern void SPHLITE_FC_sphft_get_explosive_summary( + int *particle_count, + int *reacted_count, + double *lambda_range, + double *released_totals, + double *detonation_time_range, + int *ierr); static int sphlite_call_path( const char *path, @@ -402,3 +412,27 @@ int sphlite_get_gas_source_summary(double *mass_source, energy_source, &ierr); return ierr; } + +int sphlite_set_ignition_point(double x, double y, double z, double time) +{ + int ierr = 0; + SPHLITE_FC_sphft_set_ignition_point(&x, &y, &z, &time, &ierr); + return ierr; +} + +int sphlite_get_explosive_summary(int *particle_count, + int *reacted_count, + double *lambda_range, + double *released_totals, + double *detonation_time_range) +{ + int ierr = 0; + if (particle_count == NULL || reacted_count == NULL || + lambda_range == NULL || released_totals == NULL || + detonation_time_range == NULL) { + return -1; + } + SPHLITE_FC_sphft_get_explosive_summary(particle_count, reacted_count, + lambda_range, released_totals, detonation_time_range, &ierr); + return ierr; +} diff --git a/src/output/sph_output_csv.f90 b/src/output/sph_output_csv.f90 index 906ab18..7fc40b5 100644 --- a/src/output/sph_output_csv.f90 +++ b/src/output/sph_output_csv.f90 @@ -11,6 +11,9 @@ module sph_output_csv gas_equivalent_radius, gas_radius_rate, gas_dt_cfl, gas_dt_accel, & gas_dtcrit use sph_gas, only: sph_gas_get_source_summary + use sph_explosive, only: sph_explosive_get_summary, & + explosive_detonation_time, explosive_reaction_progress, & + explosive_released_mass, explosive_released_energy implicit none integer, parameter :: SPH_OUTPUT_UNIT = 41 @@ -49,6 +52,10 @@ contains integer, intent(out) :: ierr integer :: ios integer :: i + real(rk) :: explosive_td + real(rk) :: explosive_lambda + real(rk) :: explosive_mass + real(rk) :: explosive_energy ierr = SPH_SUCCESS open(unit=SPH_OUTPUT_UNIT, file=trim(filename), status='unknown', & @@ -59,13 +66,25 @@ contains return end if - write(SPH_OUTPUT_UNIT, '(A)') & - 'id,phase,status,material,section,x,y,z,vx,vy,vz,rho,pressure,temperature,h' + write(SPH_OUTPUT_UNIT, '(A)') 'id,phase,status,material,section,x,y,z,' // & + 'vx,vy,vz,rho,pressure,temperature,h,explosive_detonation_time,' // & + 'explosive_lambda,explosive_released_mass,explosive_released_energy' do i = 1, npart - write(SPH_OUTPUT_UNIT, '(5(I0,","),6(ES24.16,","),4(ES24.16,:,","))') & + explosive_td = 0.0_rk + explosive_lambda = 0.0_rk + explosive_mass = 0.0_rk + explosive_energy = 0.0_rk + if (allocated(explosive_detonation_time)) explosive_td = explosive_detonation_time(i) + if (allocated(explosive_reaction_progress)) explosive_lambda = & + explosive_reaction_progress(i) + if (allocated(explosive_released_mass)) explosive_mass = explosive_released_mass(i) + if (allocated(explosive_released_energy)) explosive_energy = & + explosive_released_energy(i) + write(SPH_OUTPUT_UNIT, '(5(I0,","),13(ES24.16,","),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), h(i) + v(1,i), v(2,i), v(3,i), rho(i), pressure(i), temperature(i), h(i), & + explosive_td, explosive_lambda, explosive_mass, explosive_energy end do close(unit=SPH_OUTPUT_UNIT) end subroutine sph_output_write_csv @@ -94,6 +113,11 @@ contains real(rk) :: gas_source_mass real(rk) :: gas_source_energy real(rk) :: gas_source_momentum(3) + integer :: explosive_particles + integer :: explosive_reacted_particles + real(rk) :: explosive_lambda_range(3) + real(rk) :: explosive_released_totals(5) + real(rk) :: explosive_time_range(2) ierr = SPH_SUCCESS gas_source_mass = 0.0_rk @@ -101,6 +125,9 @@ contains gas_source_momentum = 0.0_rk call sph_gas_get_source_summary(gas_source_mass, gas_source_momentum, & gas_source_energy, local_ierr) + call sph_explosive_get_summary(explosive_particles, & + explosive_reacted_particles, explosive_lambda_range, & + explosive_released_totals, explosive_time_range, local_ierr) 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 @@ -145,6 +172,29 @@ contains write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'gas_source_momentum_x', gas_source_momentum(1) write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'gas_source_momentum_y', gas_source_momentum(2) write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'gas_source_momentum_z', gas_source_momentum(3) + write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'explosive_particles', explosive_particles + write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'explosive_reacted_particles', & + explosive_reacted_particles + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'explosive_lambda_min', & + explosive_lambda_range(1) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'explosive_lambda_max', & + explosive_lambda_range(2) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'explosive_lambda_mean', & + explosive_lambda_range(3) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'explosive_released_mass', & + explosive_released_totals(1) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'explosive_released_energy', & + explosive_released_totals(2) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'explosive_released_momentum_x', & + explosive_released_totals(3) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'explosive_released_momentum_y', & + explosive_released_totals(4) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'explosive_released_momentum_z', & + explosive_released_totals(5) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'explosive_detonation_time_min', & + explosive_time_range(1) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'explosive_detonation_time_max', & + explosive_time_range(2) close(unit=SPH_OUTPUT_UNIT) end subroutine sph_output_write_summary diff --git a/src/output/sph_restart.f90 b/src/output/sph_restart.f90 index 46505e5..2ee380b 100644 --- a/src/output/sph_restart.f90 +++ b/src/output/sph_restart.f90 @@ -33,6 +33,18 @@ module sph_restart sph_gas_rebuild_legacy_restart_state, sph_gas_update_diagnostics use sph_gas_source, only: gas_mass_source, gas_energy_source, & gas_momentum_source + use sph_explosive, only: explosive_material_count, explosive_material_id, & + explosive_material_seen, explosive_material_name, explosive_control_defined, & + explosive_product_material_id, explosive_product_material_name, & + explosive_density0, explosive_detonation_velocity, & + explosive_energy_per_mass, explosive_burn_time, & + explosive_conversion_mode, explosive_ignition_count, & + explosive_ignition_material_id, explosive_ignition_x, & + explosive_ignition_time, explosive_ignition_status, & + explosive_detonation_time, explosive_reaction_progress, & + explosive_released_mass, explosive_released_energy, & + explosive_released_momentum, explosive_dt_reaction, & + sph_explosive_allocate_state, sph_explosive_clear_material_table use sph_neighbor, only: neighbor_cell_size, neighbor_cell_dims, neighbor_num_cells, & sph_neighbor_rebuild implicit none @@ -60,7 +72,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_3_1' + SPH_VERSION_MAJOR, SPH_VERSION_MINOR, SPH_VERSION_PATCH, 'STATE_0_4' 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) @@ -107,6 +119,12 @@ contains write(SPH_RESTART_UNIT, *) gas_mass_source(i), gas_energy_source(i), & gas_momentum_source(:,i) end do + call sph_explosive_allocate_state(ierr) + if (ierr /= SPH_SUCCESS) then + close(unit=SPH_RESTART_UNIT) + return + end if + call sph_restart_write_explosive_state() close(unit=SPH_RESTART_UNIT) end subroutine sph_restart_write @@ -123,10 +141,12 @@ contains integer :: i logical :: has_gas_table logical :: has_gas_sources + logical :: has_explosive_state ierr = SPH_SUCCESS has_gas_table = .false. has_gas_sources = .false. + has_explosive_state = .false. open(unit=SPH_RESTART_UNIT, file=trim(filename), status='old', & action='read', iostat=ios) if (ios /= 0) then @@ -220,7 +240,8 @@ contains return end if if (trim(state_version) == 'STATE_0_3' .or. & - trim(state_version) == 'STATE_0_3_1') then + trim(state_version) == 'STATE_0_3_1' .or. & + trim(state_version) == 'STATE_0_4') 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 @@ -277,7 +298,8 @@ contains return end if gas_bubble_history_valid = gas_particle_count > 0 - if (trim(state_version) == 'STATE_0_3_1') then + if (trim(state_version) == 'STATE_0_3_1' .or. & + trim(state_version) == 'STATE_0_4') then has_gas_table = .true. has_gas_sources = .true. call sph_restart_read_gas_material_table(ios) @@ -288,6 +310,7 @@ contains return end if end if + if (trim(state_version) == 'STATE_0_4') has_explosive_state = .true. else if (trim(state_version) /= 'STATE_0_2') then call sph_error_set(SPH_ERROR_INPUT, 'unsupported restart state version') ierr = SPH_ERROR_INPUT @@ -321,6 +344,15 @@ contains else if (ierr == SPH_SUCCESS) then call sph_gas_clear_sources(ierr) end if + if (ierr == SPH_SUCCESS .and. has_explosive_state) then + call sph_restart_read_explosive_state(ios) + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Explosive state') + ierr = SPH_ERROR_INPUT + end if + else if (ierr == SPH_SUCCESS) then + call sph_restart_clear_explosive_state(ierr) + end if if (ierr == SPH_SUCCESS) then call sph_neighbor_rebuild(ierr) if (ierr == SPH_SUCCESS) call sph_water_update_diagnostics(ierr) @@ -375,4 +407,93 @@ contains end do end subroutine sph_restart_read_gas_sources + subroutine sph_restart_write_explosive_state() + integer :: i + + write(SPH_RESTART_UNIT, *) explosive_material_count + do i = 1, explosive_material_count + write(SPH_RESTART_UNIT, *) explosive_material_id(i), & + trim(explosive_material_name(i)), explosive_control_defined(i), & + explosive_product_material_id(i), & + trim(explosive_product_material_name(i)), explosive_conversion_mode(i) + write(SPH_RESTART_UNIT, *) explosive_density0(i), & + explosive_detonation_velocity(i), explosive_energy_per_mass(i), & + explosive_burn_time(i) + end do + write(SPH_RESTART_UNIT, *) explosive_ignition_count + do i = 1, explosive_ignition_count + write(SPH_RESTART_UNIT, *) explosive_ignition_material_id(i), & + explosive_ignition_x(:,i), explosive_ignition_time(i) + end do + write(SPH_RESTART_UNIT, *) explosive_dt_reaction + do i = 1, npart + write(SPH_RESTART_UNIT, *) explosive_ignition_status(i), & + explosive_detonation_time(i), explosive_reaction_progress(i), & + explosive_released_mass(i), explosive_released_energy(i), & + explosive_released_momentum(:,i) + end do + end subroutine sph_restart_write_explosive_state + + subroutine sph_restart_read_explosive_state(ios) + integer, intent(out) :: ios + integer :: i + integer :: table_count + integer :: local_ierr + + ios = 0 + call sph_explosive_clear_material_table() + read(SPH_RESTART_UNIT, *, iostat=ios) table_count + if (ios /= 0) return + if (table_count < 0 .or. table_count > SPH_MAX_MATERIALS) then + ios = 1 + return + end if + explosive_material_count = table_count + explosive_material_seen = explosive_material_count > 0 + do i = 1, explosive_material_count + read(SPH_RESTART_UNIT, *, iostat=ios) explosive_material_id(i), & + explosive_material_name(i), explosive_control_defined(i), & + explosive_product_material_id(i), explosive_product_material_name(i), & + explosive_conversion_mode(i) + if (ios /= 0) return + read(SPH_RESTART_UNIT, *, iostat=ios) explosive_density0(i), & + explosive_detonation_velocity(i), explosive_energy_per_mass(i), & + explosive_burn_time(i) + if (ios /= 0) return + end do + read(SPH_RESTART_UNIT, *, iostat=ios) explosive_ignition_count + if (ios /= 0) return + if (explosive_ignition_count < 0 .or. & + explosive_ignition_count > SPH_MAX_MATERIALS) then + ios = 1 + return + end if + do i = 1, explosive_ignition_count + read(SPH_RESTART_UNIT, *, iostat=ios) explosive_ignition_material_id(i), & + explosive_ignition_x(:,i), explosive_ignition_time(i) + if (ios /= 0) return + end do + read(SPH_RESTART_UNIT, *, iostat=ios) explosive_dt_reaction + if (ios /= 0) return + call sph_explosive_allocate_state(local_ierr) + if (local_ierr /= SPH_SUCCESS) then + ios = 1 + return + end if + do i = 1, npart + read(SPH_RESTART_UNIT, *, iostat=ios) explosive_ignition_status(i), & + explosive_detonation_time(i), explosive_reaction_progress(i), & + explosive_released_mass(i), explosive_released_energy(i), & + explosive_released_momentum(:,i) + if (ios /= 0) return + end do + end subroutine sph_restart_read_explosive_state + + subroutine sph_restart_clear_explosive_state(ierr) + integer, intent(out) :: ierr + + call sph_explosive_clear_material_table() + call sph_explosive_allocate_state(ierr) + end subroutine sph_restart_clear_explosive_state + end module sph_restart diff --git a/tests/interface/test_c_api.c b/tests/interface/test_c_api.c index 5f89dd0..c6365b3 100644 --- a/tests/interface/test_c_api.c +++ b/tests/interface/test_c_api.c @@ -55,6 +55,11 @@ int main(void) double gas_source_mass = 0.0; double gas_source_momentum[3] = {0.0, 0.0, 0.0}; double gas_source_energy = 0.0; + int explosive_particle_count = 0; + int explosive_reacted_count = 0; + double explosive_lambda_range[3] = {0.0, 0.0, 0.0}; + double explosive_released_totals[5] = {0.0, 0.0, 0.0, 0.0, 0.0}; + double explosive_detonation_range[2] = {0.0, 0.0}; if (SPHLITE_PHASE_SOLID != 4 || SPHLITE_PHASE_GAS != 2 || SPHLITE_STATUS_ACTIVE != 0) { @@ -108,6 +113,16 @@ int main(void) &gas_source_energy) != -1) { return fail("null gas source momentum pointer did not return -1"); } + if (sphlite_get_explosive_summary(NULL, &explosive_reacted_count, + explosive_lambda_range, explosive_released_totals, + explosive_detonation_range) != -1) { + return fail("null explosive summary particle pointer did not return -1"); + } + if (sphlite_get_explosive_summary(&explosive_particle_count, + &explosive_reacted_count, NULL, explosive_released_totals, + explosive_detonation_range) != -1) { + return fail("null explosive summary lambda 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"); @@ -242,5 +257,30 @@ int main(void) } ierr = sphlite_finalize(); if (ierr != 0) return fail("finalize gas failed"); + + ierr = sphlite_initialize(); + if (ierr != 0) return fail("initialize explosive failed"); + snprintf(path, sizeof(path), "%s/explosive_conversion_phase_040.inp", + SPHLITE_TEST_DATA_DIR); + ierr = sphlite_read_inp(path); + if (ierr != 0) return fail("read explosive input failed"); + ierr = sphlite_set_ignition_point(0.0, 0.0, 0.0, 0.0); + if (ierr != 0) return fail("set explosive ignition failed"); + ierr = sphlite_build_model(); + if (ierr != 0) return fail("build explosive model failed"); + ierr = sphlite_run_explicit(); + if (ierr != 0) return fail("run explosive model failed"); + ierr = sphlite_get_explosive_summary(&explosive_particle_count, + &explosive_reacted_count, explosive_lambda_range, + explosive_released_totals, explosive_detonation_range); + if (ierr != 0) return fail("explosive summary query failed"); + if (explosive_particle_count <= 0 || explosive_reacted_count <= 0) { + return fail("explosive summary count mismatch"); + } + if (explosive_lambda_range[1] <= 0.0 || explosive_released_totals[1] <= 0.0) { + return fail("explosive summary content mismatch"); + } + ierr = sphlite_finalize(); + if (ierr != 0) return fail("finalize explosive failed"); return 0; } diff --git a/tests/interface/test_cpp_api.cpp b/tests/interface/test_cpp_api.cpp index 6f40489..5fcc83d 100644 --- a/tests/interface/test_cpp_api.cpp +++ b/tests/interface/test_cpp_api.cpp @@ -78,5 +78,21 @@ int main() } gas_session.close(); + sphlite::Session explosive_session; + std::string explosive_input = + std::string(SPHLITE_TEST_DATA_DIR) + "/explosive_conversion_phase_040.inp"; + sphlite::read_inp(explosive_input); + sphlite::set_ignition_point(0.0, 0.0, 0.0, 0.0); + sphlite::build_model(); + sphlite::run_explicit(); + sphlite::ExplosiveSummary explosive = sphlite::explosive_summary(); + if (explosive.particle_count <= 0 || explosive.reacted_count <= 0) { + return 1; + } + if (explosive.lambda_range[1] <= 0.0 || explosive.released_totals[1] <= 0.0) { + return 1; + } + explosive_session.close(); + return 0; } diff --git a/tests/module/test_output.f90 b/tests/module/test_output.f90 index 7174661..eebb69d 100644 --- a/tests/module/test_output.f90 +++ b/tests/module/test_output.f90 @@ -114,8 +114,9 @@ contains 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 + if (trim(header) /= 'id,phase,status,material,section,x,y,z,' // & + 'vx,vy,vz,rho,pressure,temperature,h,explosive_detonation_time,' // & + 'explosive_lambda,explosive_released_mass,explosive_released_energy') then call fail('particle CSV header mismatch') end if end subroutine check_particle_header @@ -144,6 +145,9 @@ contains call assert_summary_metric(filename, 'gas_equivalent_radius') call assert_summary_metric(filename, 'gas_radius_rate') call assert_summary_metric(filename, 'gas_dtcrit') + call assert_summary_metric(filename, 'explosive_particles') + call assert_summary_metric(filename, 'explosive_lambda_mean') + call assert_summary_metric(filename, 'explosive_released_energy') end subroutine check_summary_metrics subroutine assert_summary_metric(filename, metric) diff --git a/tests/module/test_restart.f90 b/tests/module/test_restart.f90 index 837bf5e..a90256c 100644 --- a/tests/module/test_restart.f90 +++ b/tests/module/test_restart.f90 @@ -143,6 +143,7 @@ contains real(rk) :: dt_values(3) integer :: gas_eos_type_value integer :: gas_count_value + integer :: gas_table_count real(rk) :: gas_eos_values(10) real(rk) :: gas_control_values(3) real(rk) :: gas_body_values(3) @@ -167,7 +168,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_3') call fail('restart state version mismatch') + if (trim(state_version) /= 'STATE_0_4') 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') @@ -236,6 +237,9 @@ contains 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) gas_table_count + if (ios /= 0) call fail('restart Gas material table count read failed') + if (gas_table_count /= 0) call fail('restart Water-only Gas table 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') diff --git a/todo.md b/todo.md index 9e51f87..dc293cb 100644 --- a/todo.md +++ b/todo.md @@ -102,26 +102,26 @@ ## 阶段 E:Summary、Output、Restart、API、C++、Python 与 CLI -- [ ] 增加 Explosive summary 内部统计过程。 -- [ ] 统计 explosive 粒子总数和已反应粒子数。 -- [ ] 统计 `lambda` 最小值、最大值和平均值。 -- [ ] 统计累计释放质量、能量和三向动量。 -- [ ] 统计爆轰到达时间最小值和最大值。 -- [ ] 将 Explosive summary 字段写入 summary CSV。 -- [ ] 将 Explosive 关键粒子状态写入粒子 CSV 输出。 -- [ ] 将 0.4.0 restart 状态版本更新为 `STATE_0_4`。 -- [ ] 将 Explosive material 表写入 0.4.0 restart。 -- [ ] 将 product Gas material 映射写入 0.4.0 restart。 -- [ ] 将点火记录写入 0.4.0 restart。 -- [ ] 将每粒子爆轰到达时间写入 0.4.0 restart。 -- [ ] 将每粒子反应进度和累计释放量写入 0.4.0 restart。 -- [ ] 从 0.4.0 restart 读回 Explosive 状态并继续显式步。 -- [ ] 从 0.3.1 restart 读回时初始化空 Explosive 状态。 -- [ ] 增加 `sph_get_explosive_summary` Fortran API。 -- [ ] 增加 `sph_set_ignition_point` Fortran API。 -- [ ] 在 C ABI 中追加点火点设置和 Explosive summary 查询接口。 -- [ ] 在 C++ wrapper 和 Python ctypes 中增加 Explosive 薄包装。 -- [ ] 在 CLI inspect/run 中增加 Explosive 字段检查。 +- [x] 增加 Explosive summary 内部统计过程。 +- [x] 统计 explosive 粒子总数和已反应粒子数。 +- [x] 统计 `lambda` 最小值、最大值和平均值。 +- [x] 统计累计释放质量、能量和三向动量。 +- [x] 统计爆轰到达时间最小值和最大值。 +- [x] 将 Explosive summary 字段写入 summary CSV。 +- [x] 将 Explosive 关键粒子状态写入粒子 CSV 输出。 +- [x] 将 0.4.0 restart 状态版本更新为 `STATE_0_4`。 +- [x] 将 Explosive material 表写入 0.4.0 restart。 +- [x] 将 product Gas material 映射写入 0.4.0 restart。 +- [x] 将点火记录写入 0.4.0 restart。 +- [x] 将每粒子爆轰到达时间写入 0.4.0 restart。 +- [x] 将每粒子反应进度和累计释放量写入 0.4.0 restart。 +- [x] 从 0.4.0 restart 读回 Explosive 状态并继续显式步。 +- [x] 从 0.3.1 restart 读回时初始化空 Explosive 状态。 +- [x] 增加 `sph_get_explosive_summary` Fortran API。 +- [x] 增加 `sph_set_ignition_point` Fortran API。 +- [x] 在 C ABI 中追加点火点设置和 Explosive summary 查询接口。 +- [x] 在 C++ wrapper 和 Python ctypes 中增加 Explosive 薄包装。 +- [x] 在 CLI inspect/run 中增加 Explosive 字段检查。 ## 阶段 F:测试接入、回归、文档同步与发布门禁 diff --git a/tools/testing/test_cli.py b/tools/testing/test_cli.py index 9db65b2..031b681 100644 --- a/tools/testing/test_cli.py +++ b/tools/testing/test_cli.py @@ -82,6 +82,16 @@ def main() -> int: "gas_dtcrit", "gas_source_mass", "gas_source_energy", + "explosive_phase_particles", + "explosive_particles", + "explosive_reacted_particles", + "explosive_lambda_min", + "explosive_lambda_max", + "explosive_lambda_mean", + "explosive_released_mass", + "explosive_released_energy", + "explosive_detonation_time_min", + "explosive_detonation_time_max", "status", ): require_field(inspect.stdout, field) @@ -110,6 +120,8 @@ def main() -> int: raise AssertionError("CLI dtcrit mismatch") if int(inspect_fields["gas_particles"]) != 0: raise AssertionError("CLI water inspect gas particle count mismatch") + if int(inspect_fields["explosive_particles"]) != 0: + raise AssertionError("CLI water inspect explosive particle count mismatch") if inspect_fields["status"] != "ok": raise AssertionError("CLI status field mismatch") @@ -130,6 +142,22 @@ def main() -> int: raise AssertionError("CLI gas extended mass mismatch") if float(gas_fields["gas_source_mass"]) != 0.0: raise AssertionError("CLI gas source default mismatch") + if int(gas_fields["explosive_particles"]) != 0: + raise AssertionError("CLI gas inspect explosive particle count mismatch") + + explosive_model = str(Path(args.input).parent / "explosive_conversion_phase_040.inp") + explosive_run = run_command([exe, explosive_model, "--run"]) + explosive_fields = read_fields(explosive_run.stdout) + if int(explosive_fields["explosive_phase_particles"]) <= 0: + raise AssertionError("CLI explosive phase particle count mismatch") + if int(explosive_fields["explosive_particles"]) <= 0: + raise AssertionError("CLI explosive tracked particle count mismatch") + if int(explosive_fields["explosive_reacted_particles"]) <= 0: + raise AssertionError("CLI explosive reacted particle count mismatch") + if float(explosive_fields["explosive_lambda_max"]) <= 0.0: + raise AssertionError("CLI explosive lambda mismatch") + if float(explosive_fields["explosive_released_energy"]) <= 0.0: + raise AssertionError("CLI explosive released energy mismatch") check = run_command([exe, model, "--check"]) if "mode: check" not in check.stdout: diff --git a/tools/testing/test_python_debug.py b/tools/testing/test_python_debug.py index 02349d4..48f1ca1 100644 --- a/tools/testing/test_python_debug.py +++ b/tools/testing/test_python_debug.py @@ -31,6 +31,10 @@ def main() -> int: raise AssertionError("Python wrapper is missing Gas C ABI diagnostics") if not hasattr(solver.lib, "sphlite_get_gas_extended_summary"): raise AssertionError("Python wrapper is missing Gas extended C ABI diagnostics") + if not hasattr(solver.lib, "sphlite_get_explosive_summary"): + raise AssertionError("Python wrapper is missing Explosive C ABI diagnostics") + if not hasattr(solver.lib, "sphlite_set_ignition_point"): + raise AssertionError("Python wrapper is missing Explosive ignition C ABI") if solver.version() != (0, 4, 0): raise AssertionError("Python version mismatch") @@ -53,6 +57,8 @@ def main() -> int: raise AssertionError("inspect helper did not include Gas diagnostics") if "gas_source_mass" not in solid_summary or "gas_density_range" not in solid_summary: raise AssertionError("inspect helper did not include Gas extended diagnostics") + if "explosive_particles" not in solid_summary or "explosive_lambda_range" not in solid_summary: + raise AssertionError("inspect helper did not include Explosive diagnostics") solver.set_output_directory(root / "build-codex-unix") solver.write_output(0, 0.0) restart = root / "build-codex-unix" / "python_restart.tmp" @@ -112,6 +118,26 @@ def main() -> int: finally: gas.finalize() + explosive = SPHLite(args.library) + explosive.initialize() + try: + explosive_input = root / "tests" / "data" / "explosive_conversion_phase_040.inp" + explosive.read_inp(explosive_input) + explosive.set_ignition_point(0.0, 0.0, 0.0, 0.0) + explosive.build_model() + explosive.run_explicit() + explosive_summary = explosive.explosive_summary() + if explosive_summary["explosive_particles"] <= 0: + raise AssertionError("explosive summary particle count mismatch") + if explosive_summary["explosive_reacted_particles"] <= 0: + raise AssertionError("explosive summary reacted count mismatch") + if explosive_summary["explosive_lambda_range"][1] <= 0.0: + raise AssertionError("explosive summary lambda mismatch") + if explosive_summary["explosive_released_totals"][1] <= 0.0: + raise AssertionError("explosive summary energy mismatch") + finally: + explosive.finalize() + bad_water = SPHLite(args.library) bad_water.initialize() try: