From d14ea370e59441ea6e227200df73398b7f55aebd Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 3 Jul 2026 15:55:37 +0800 Subject: [PATCH] Add Solid output and restart --- CMakeLists.txt | 19 ++ src/input/sph_input_reader.f90 | 4 +- src/output/sph_output_csv.f90 | 59 +++- src/output/sph_restart.f90 | 102 +++++-- src/solid/sph_solid_dynamics.f90 | 9 + src/solid/sph_solid_restart.f90 | 155 ++++++++++ tests/module/test_explosive_restart_041.f90 | 2 +- tests/module/test_output.f90 | 18 +- tests/module/test_restart.f90 | 2 +- tests/module/test_solid_output.f90 | 271 ++++++++++++++++++ .../module/test_solid_restart_041_compat.f90 | 100 +++++++ tests/module/test_solid_restart_050.f90 | 211 ++++++++++++++ todo.md | 54 ++-- 13 files changed, 951 insertions(+), 55 deletions(-) create mode 100644 src/solid/sph_solid_restart.f90 create mode 100644 tests/module/test_solid_output.f90 create mode 100644 tests/module/test_solid_restart_041_compat.f90 create mode 100644 tests/module/test_solid_restart_050.f90 diff --git a/CMakeLists.txt b/CMakeLists.txt index d2c5324..fa6f848 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,6 +88,7 @@ set(SPHLITE_CORE_SOURCES src/solid/sph_solid_material.f90 src/solid/sph_solid.f90 src/solid/sph_solid_dynamics.f90 + src/solid/sph_solid_restart.f90 src/shell/sph_shell.f90 src/interface/sph_api_state.f90 src/interface/sph_api.f90 @@ -226,6 +227,24 @@ set_target_properties(test_solid_explicit_smoke PROPERTIES Fortran_MODULE_DIRECT add_test(NAME solid_explicit_smoke COMMAND test_solid_explicit_smoke) set_tests_properties(solid_explicit_smoke PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_solid_output tests/module/test_solid_output.f90) +target_link_libraries(test_solid_output PRIVATE sphlite_core) +set_target_properties(test_solid_output PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME solid_output COMMAND test_solid_output) +set_tests_properties(solid_output PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_solid_restart_050 tests/module/test_solid_restart_050.f90) +target_link_libraries(test_solid_restart_050 PRIVATE sphlite_core) +set_target_properties(test_solid_restart_050 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME solid_restart_050 COMMAND test_solid_restart_050) +set_tests_properties(solid_restart_050 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_solid_restart_041_compat tests/module/test_solid_restart_041_compat.f90) +target_link_libraries(test_solid_restart_041_compat PRIVATE sphlite_core) +set_target_properties(test_solid_restart_041_compat PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME solid_restart_041_compat COMMAND test_solid_restart_041_compat) +set_tests_properties(solid_restart_041_compat PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_executable(test_c_api tests/interface/test_c_api.c) target_include_directories(test_c_api PRIVATE include) target_compile_definitions(test_c_api PRIVATE diff --git a/src/input/sph_input_reader.f90 b/src/input/sph_input_reader.f90 index e3c0579..03b0be4 100644 --- a/src/input/sph_input_reader.f90 +++ b/src/input/sph_input_reader.f90 @@ -23,7 +23,7 @@ module sph_input_reader sph_solid_set_johnson_cook, sph_solid_set_failure, & sph_solid_set_control, sph_solid_build_state, & sph_solid_count_unbound_materials - use sph_solid_dynamics, only: sph_solid_set_body_force + use sph_solid_dynamics, only: sph_solid_set_body_force, sph_solid_update_diagnostics use sph_neighbor, only: sph_neighbor_rebuild use sph_inp_defs, only: SPH_INPUT_LINE_LEN, SPH_INPUT_FIELD_LEN, & SPH_INPUT_MAX_FIELDS, SPH_KEY_NODE, SPH_KEY_ELEMENT, SPH_KEY_NSET, & @@ -124,6 +124,8 @@ contains input_warning_count = input_warning_count + explosive_unbound_count call sph_solid_count_unbound_materials(solid_unbound_count) input_warning_count = input_warning_count + solid_unbound_count + call sph_solid_update_diagnostics(ierr) + if (ierr /= SPH_SUCCESS) return call sph_neighbor_rebuild(ierr) end subroutine sph_input_build_model diff --git a/src/output/sph_output_csv.f90 b/src/output/sph_output_csv.f90 index fdc8aee..f35f88f 100644 --- a/src/output/sph_output_csv.f90 +++ b/src/output/sph_output_csv.f90 @@ -9,7 +9,11 @@ module sph_output_csv gas_particle_count, gas_mass, gas_volume, gas_center, gas_mean_pressure, & gas_max_pressure, gas_min_density, gas_max_density, gas_momentum, & gas_equivalent_radius, gas_radius_rate, gas_dt_cfl, gas_dt_accel, & - gas_dtcrit + gas_dtcrit, solid_particle_count, solid_failed_count, solid_mass, & + solid_momentum, solid_kinetic_energy, solid_internal_energy_total, & + solid_max_von_mises, solid_min_plastic_strain, solid_max_plastic_strain, & + solid_min_damage, solid_max_damage, solid_min_softening, & + solid_max_softening, solid_dt_cfl, solid_dt_accel, solid_dtcrit use sph_gas, only: sph_gas_get_source_summary use sph_explosive_state, only: & explosive_detonation_time, explosive_reaction_progress, & @@ -20,6 +24,8 @@ module sph_output_csv explosive_source_momentum_total use sph_explosive, only: sph_explosive_get_summary, & sph_explosive_get_release_balance + use sph_solid_state, only: solid_stress, solid_plastic_strain, solid_damage, & + solid_softening, solid_failure_status implicit none integer, parameter :: SPH_OUTPUT_UNIT = 41 @@ -64,6 +70,11 @@ contains real(rk) :: explosive_remaining real(rk) :: explosive_mass real(rk) :: explosive_energy + real(rk) :: solid_stress_value(6) + real(rk) :: solid_plastic + real(rk) :: solid_damage_value + real(rk) :: solid_softening_value + integer :: solid_failure ierr = SPH_SUCCESS open(unit=SPH_OUTPUT_UNIT, file=trim(filename), status='unknown', & @@ -77,7 +88,10 @@ contains 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_initial_mass,explosive_remaining_mass,' // & - 'explosive_released_mass,explosive_released_energy' + 'explosive_released_mass,explosive_released_energy,' // & + 'solid_stress_xx,solid_stress_yy,solid_stress_zz,solid_stress_xy,' // & + 'solid_stress_yz,solid_stress_zx,solid_plastic_strain,solid_damage,' // & + 'solid_softening,solid_failure_status' do i = 1, npart explosive_td = 0.0_rk explosive_lambda = 0.0_rk @@ -85,6 +99,11 @@ contains explosive_remaining = 0.0_rk explosive_mass = 0.0_rk explosive_energy = 0.0_rk + solid_stress_value = 0.0_rk + solid_plastic = 0.0_rk + solid_damage_value = 0.0_rk + solid_softening_value = 0.0_rk + solid_failure = 0 if (allocated(explosive_detonation_time)) explosive_td = explosive_detonation_time(i) if (allocated(explosive_reaction_progress)) explosive_lambda = & explosive_reaction_progress(i) @@ -95,12 +114,19 @@ contains 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,","),15(ES24.16,","),ES24.16)') & + if (allocated(solid_stress)) solid_stress_value = solid_stress(:,i) + if (allocated(solid_plastic_strain)) solid_plastic = solid_plastic_strain(i) + if (allocated(solid_damage)) solid_damage_value = solid_damage(i) + if (allocated(solid_softening)) solid_softening_value = solid_softening(i) + if (allocated(solid_failure_status)) solid_failure = solid_failure_status(i) + write(SPH_OUTPUT_UNIT, '(5(I0,","),25(ES24.16,","),I0)') & 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), & explosive_td, explosive_lambda, explosive_initial, & - explosive_remaining, explosive_mass, explosive_energy + explosive_remaining, explosive_mass, explosive_energy, & + solid_stress_value(:), solid_plastic, solid_damage_value, & + solid_softening_value, solid_failure end do close(unit=SPH_OUTPUT_UNIT) end subroutine sph_output_write_csv @@ -238,7 +264,32 @@ contains explosive_source_momentum_total(2) write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'explosive_source_momentum_z', & explosive_source_momentum_total(3) + call sph_output_write_solid_summary() close(unit=SPH_OUTPUT_UNIT) end subroutine sph_output_write_summary + subroutine sph_output_write_solid_summary() + write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'solid_particles', solid_particle_count + write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'solid_failed_particles', solid_failed_count + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_mass', solid_mass + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_momentum_x', solid_momentum(1) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_momentum_y', solid_momentum(2) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_momentum_z', solid_momentum(3) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_kinetic_energy', solid_kinetic_energy + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_internal_energy', & + solid_internal_energy_total + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_max_von_mises', solid_max_von_mises + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_plastic_strain_min', & + solid_min_plastic_strain + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_plastic_strain_max', & + solid_max_plastic_strain + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_damage_min', solid_min_damage + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_damage_max', solid_max_damage + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_softening_min', solid_min_softening + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_softening_max', solid_max_softening + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_dt_cfl', solid_dt_cfl + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_dt_accel', solid_dt_accel + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'solid_dtcrit', solid_dtcrit + end subroutine sph_output_write_solid_summary + end module sph_output_csv diff --git a/src/output/sph_restart.f90 b/src/output/sph_restart.f90 index 41464a3..48de6c8 100644 --- a/src/output/sph_restart.f90 +++ b/src/output/sph_restart.f90 @@ -13,7 +13,12 @@ module sph_restart 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 + gas_dt_accel, gas_dtcrit, solid_particle_count, solid_failed_count, & + solid_mass, solid_momentum, solid_kinetic_energy, & + solid_internal_energy_total, solid_max_von_mises, & + solid_min_plastic_strain, solid_max_plastic_strain, solid_min_damage, & + solid_max_damage, solid_min_softening, solid_max_softening, & + solid_dt_cfl, solid_dt_accel, solid_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 @@ -53,6 +58,9 @@ module sph_restart use sph_explosive, only: & sph_explosive_allocate_state, sph_explosive_clear_material_table, & sph_explosive_update_release_balance + use sph_solid_restart, only: sph_solid_restart_write, sph_solid_restart_read, & + sph_solid_restart_clear + use sph_solid_dynamics, only: sph_solid_update_diagnostics use sph_neighbor, only: neighbor_cell_size, neighbor_cell_dims, neighbor_num_cells, & sph_neighbor_rebuild implicit none @@ -80,7 +88,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_4_1' + SPH_VERSION_MAJOR, SPH_VERSION_MINOR, SPH_VERSION_PATCH, 'STATE_0_5' 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) @@ -133,6 +141,7 @@ contains return end if call sph_restart_write_explosive_state() + call sph_solid_restart_write(SPH_RESTART_UNIT) close(unit=SPH_RESTART_UNIT) end subroutine sph_restart_write @@ -150,11 +159,13 @@ contains logical :: has_gas_table logical :: has_gas_sources logical :: has_explosive_state + logical :: has_solid_state ierr = SPH_SUCCESS has_gas_table = .false. has_gas_sources = .false. has_explosive_state = .false. + has_solid_state = .false. open(unit=SPH_RESTART_UNIT, file=trim(filename), status='old', & action='read', iostat=ios) if (ios /= 0) then @@ -250,7 +261,8 @@ contains if (trim(state_version) == 'STATE_0_3' .or. & trim(state_version) == 'STATE_0_3_1' .or. & trim(state_version) == 'STATE_0_4' .or. & - trim(state_version) == 'STATE_0_4_1') then + trim(state_version) == 'STATE_0_4_1' .or. & + trim(state_version) == 'STATE_0_5') 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 @@ -309,7 +321,8 @@ contains gas_bubble_history_valid = gas_particle_count > 0 if (trim(state_version) == 'STATE_0_3_1' .or. & trim(state_version) == 'STATE_0_4' .or. & - trim(state_version) == 'STATE_0_4_1') then + trim(state_version) == 'STATE_0_4_1' .or. & + trim(state_version) == 'STATE_0_5') then has_gas_table = .true. has_gas_sources = .true. call sph_restart_read_gas_material_table(ios) @@ -321,7 +334,9 @@ contains end if end if if (trim(state_version) == 'STATE_0_4' .or. & - trim(state_version) == 'STATE_0_4_1') has_explosive_state = .true. + trim(state_version) == 'STATE_0_4_1' .or. & + trim(state_version) == 'STATE_0_5') has_explosive_state = .true. + if (trim(state_version) == 'STATE_0_5') has_solid_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 @@ -358,16 +373,25 @@ contains if (ierr == SPH_SUCCESS .and. has_explosive_state) then call sph_restart_read_explosive_state(state_version, 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 .and. has_solid_state) then + call sph_solid_restart_read(SPH_RESTART_UNIT, ios) + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Solid state') + ierr = SPH_ERROR_INPUT + end if + else if (ierr == SPH_SUCCESS) then + call sph_solid_restart_clear(ierr) + end if if (ierr == SPH_SUCCESS) then call sph_neighbor_rebuild(ierr) if (ierr == SPH_SUCCESS) call sph_water_update_diagnostics(ierr) if (ierr == SPH_SUCCESS) call sph_gas_update_diagnostics(ierr) + if (ierr == SPH_SUCCESS) call sph_solid_update_diagnostics(ierr) end if close(unit=SPH_RESTART_UNIT) end subroutine sph_restart_read @@ -461,11 +485,16 @@ contains logical :: has_041_state ios = 0 - has_041_state = trim(state_version) == 'STATE_0_4_1' + has_041_state = trim(state_version) == 'STATE_0_4_1' .or. & + trim(state_version) == 'STATE_0_5' call sph_explosive_clear_material_table() read(SPH_RESTART_UNIT, *, iostat=ios) table_count - if (ios /= 0) return + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Explosive material count') + return + end if if (table_count < 0 .or. table_count > SPH_MAX_MATERIALS) then + call sph_error_set(SPH_ERROR_INPUT, 'restart Explosive material count is out of range') ios = 1 return end if @@ -476,44 +505,76 @@ contains 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 + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Explosive material row') + return + end if 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 + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Explosive control row') + return + end if end do read(SPH_RESTART_UNIT, *, iostat=ios) explosive_ignition_count - if (ios /= 0) return + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Explosive ignition count') + return + end if if (explosive_ignition_count < 0 .or. & explosive_ignition_count > SPH_MAX_MATERIALS) then + call sph_error_set(SPH_ERROR_INPUT, 'restart Explosive ignition count is out of range') 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 + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Explosive ignition row') + return + end if end do read(SPH_RESTART_UNIT, *, iostat=ios) explosive_dt_reaction - if (ios /= 0) return + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Explosive dt state') + return + end if call sph_explosive_allocate_state(local_ierr) if (local_ierr /= SPH_SUCCESS) then + call sph_error_set(SPH_ERROR_INPUT, 'failed to allocate restart Explosive state') ios = 1 return end if if (has_041_state) then read(SPH_RESTART_UNIT, *, iostat=ios) explosive_converted_count, & explosive_source_target_count - if (ios /= 0) return + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Explosive conversion counts') + return + end if read(SPH_RESTART_UNIT, *, iostat=ios) explosive_source_mass_total, & explosive_source_energy_total, explosive_source_momentum_total(:) - if (ios /= 0) return + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Explosive source totals') + return + end if read(SPH_RESTART_UNIT, *, iostat=ios) explosive_release_mass_balance(:) - if (ios /= 0) return + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Explosive mass balance') + return + end if read(SPH_RESTART_UNIT, *, iostat=ios) explosive_release_energy_balance(:) - if (ios /= 0) return + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Explosive energy balance') + return + end if read(SPH_RESTART_UNIT, *, iostat=ios) explosive_release_momentum_balance(:) - if (ios /= 0) return + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Explosive momentum balance') + return + end if end if do i = 1, npart if (has_041_state) then @@ -529,7 +590,10 @@ contains explosive_released_momentum(:,i) if (ios == 0) call sph_restart_seed_explosive_041_defaults(i) end if - if (ios /= 0) return + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Explosive particle state') + return + end if end do call sph_explosive_update_release_balance() end subroutine sph_restart_read_explosive_state diff --git a/src/solid/sph_solid_dynamics.f90 b/src/solid/sph_solid_dynamics.f90 index 26157d3..f490065 100644 --- a/src/solid/sph_solid_dynamics.f90 +++ b/src/solid/sph_solid_dynamics.f90 @@ -129,10 +129,19 @@ contains logical :: first_active real(rk) :: speed2 real(rk) :: vm_value + real(rk) :: saved_dt_cfl + real(rk) :: saved_dt_accel + real(rk) :: saved_dtcrit ierr = SPH_SUCCESS + saved_dt_cfl = solid_dt_cfl + saved_dt_accel = solid_dt_accel + saved_dtcrit = solid_dtcrit call sph_db_clear_solid_diagnostics() if (.not. allocated(solid_stress)) return + solid_dt_cfl = saved_dt_cfl + solid_dt_accel = saved_dt_accel + solid_dtcrit = saved_dtcrit first_active = .true. do i = 1, npart if (sph_solid_is_failed(i)) solid_failed_count = solid_failed_count + 1 diff --git a/src/solid/sph_solid_restart.f90 b/src/solid/sph_solid_restart.f90 new file mode 100644 index 0000000..f2b4544 --- /dev/null +++ b/src/solid/sph_solid_restart.f90 @@ -0,0 +1,155 @@ +module sph_solid_restart + use sph_precision, only: rk + use sph_error, only: SPH_SUCCESS + use sph_database, only: npart, SPH_MAX_MATERIALS, solid_particle_count, & + solid_failed_count, solid_mass, solid_momentum, solid_kinetic_energy, & + solid_internal_energy_total, solid_max_von_mises, & + solid_min_plastic_strain, solid_max_plastic_strain, solid_min_damage, & + solid_max_damage, solid_min_softening, solid_max_softening, & + solid_dt_cfl, solid_dt_accel, solid_dtcrit + use sph_solid, only: solid_material_seen, solid_material_count, & + solid_material_ids, solid_material_names, solid_elastic_defined, & + solid_johnson_cook_defined, solid_failure_defined, solid_control_defined, & + solid_elastic_e, solid_elastic_nu, solid_jc_a, solid_jc_b, solid_jc_n, & + solid_jc_c, solid_jc_m, solid_jc_epsdot0, solid_jc_troom, & + solid_jc_tmelt, solid_failure_d, solid_softening_type, & + solid_softening_width, sph_solid_allocate_state, & + sph_solid_deallocate_state, sph_solid_clear_material_table + use sph_solid_state, only: solid_density, solid_shear_modulus, & + solid_bulk_modulus, solid_wave_speed, solid_temperature_rise_factor, & + solid_body_force, solid_stress, solid_strain, solid_plastic_strain, & + solid_plastic_strain_rate, solid_temperature, solid_damage, & + solid_softening, solid_internal_energy, solid_reference_volume, & + solid_deformation_gradient, solid_correction_matrix, solid_acc_internal, & + solid_failure_status + use sph_solid_dynamics, only: sph_solid_update_diagnostics + implicit none + +contains + + subroutine sph_solid_restart_write(unit) + integer, intent(in) :: unit + integer :: i + + write(unit, *) solid_material_seen, solid_material_count + do i = 1, solid_material_count + write(unit, *) solid_material_ids(i), trim(solid_material_names(i)), & + solid_elastic_defined(i), solid_johnson_cook_defined(i), & + solid_failure_defined(i), solid_control_defined(i) + write(unit, *) solid_elastic_e(i), solid_elastic_nu(i) + write(unit, *) solid_jc_a(i), solid_jc_b(i), solid_jc_n(i), & + solid_jc_c(i), solid_jc_m(i), solid_jc_epsdot0(i), & + solid_jc_troom(i), solid_jc_tmelt(i) + write(unit, *) solid_failure_d(:,i) + write(unit, *) trim(solid_softening_type(i)), solid_softening_width(i) + write(unit, *) solid_density(i), solid_shear_modulus(i), & + solid_bulk_modulus(i), solid_wave_speed(i), solid_temperature_rise_factor(i) + end do + write(unit, *) solid_body_force(:) + write(unit, *) solid_particle_count, solid_failed_count, solid_mass + write(unit, *) solid_momentum(:), solid_kinetic_energy, & + solid_internal_energy_total, solid_max_von_mises + write(unit, *) solid_min_plastic_strain, solid_max_plastic_strain, & + solid_min_damage, solid_max_damage, solid_min_softening, solid_max_softening + write(unit, *) solid_dt_cfl, solid_dt_accel, solid_dtcrit + write(unit, *) allocated(solid_stress) + if (.not. allocated(solid_stress)) return + do i = 1, npart + write(unit, *) solid_stress(:,i) + write(unit, *) solid_strain(:,i) + write(unit, *) solid_plastic_strain(i), solid_plastic_strain_rate(i), & + solid_temperature(i), solid_damage(i), solid_softening(i), & + solid_internal_energy(i), solid_reference_volume(i), solid_failure_status(i) + write(unit, *) solid_deformation_gradient(:,:,i) + write(unit, *) solid_correction_matrix(:,:,i) + write(unit, *) solid_acc_internal(:,i) + end do + end subroutine sph_solid_restart_write + + subroutine sph_solid_restart_read(unit, ios) + integer, intent(in) :: unit + integer, intent(out) :: ios + integer :: i + integer :: table_count + integer :: local_ierr + logical :: has_state + + ios = 0 + call sph_solid_clear_material_table() + read(unit, *, iostat=ios) solid_material_seen, table_count + if (ios /= 0) return + if (table_count < 0 .or. table_count > SPH_MAX_MATERIALS) then + ios = 1 + return + end if + solid_material_count = table_count + do i = 1, solid_material_count + read(unit, *, iostat=ios) solid_material_ids(i), solid_material_names(i), & + solid_elastic_defined(i), solid_johnson_cook_defined(i), & + solid_failure_defined(i), solid_control_defined(i) + if (ios /= 0) return + read(unit, *, iostat=ios) solid_elastic_e(i), solid_elastic_nu(i) + if (ios /= 0) return + read(unit, *, iostat=ios) solid_jc_a(i), solid_jc_b(i), solid_jc_n(i), & + solid_jc_c(i), solid_jc_m(i), solid_jc_epsdot0(i), & + solid_jc_troom(i), solid_jc_tmelt(i) + if (ios /= 0) return + read(unit, *, iostat=ios) solid_failure_d(:,i) + if (ios /= 0) return + read(unit, *, iostat=ios) solid_softening_type(i), solid_softening_width(i) + if (ios /= 0) return + read(unit, *, iostat=ios) solid_density(i), solid_shear_modulus(i), & + solid_bulk_modulus(i), solid_wave_speed(i), solid_temperature_rise_factor(i) + if (ios /= 0) return + end do + read(unit, *, iostat=ios) solid_body_force(:) + if (ios /= 0) return + read(unit, *, iostat=ios) solid_particle_count, solid_failed_count, solid_mass + if (ios /= 0) return + read(unit, *, iostat=ios) solid_momentum(:), solid_kinetic_energy, & + solid_internal_energy_total, solid_max_von_mises + if (ios /= 0) return + read(unit, *, iostat=ios) solid_min_plastic_strain, solid_max_plastic_strain, & + solid_min_damage, solid_max_damage, solid_min_softening, solid_max_softening + if (ios /= 0) return + read(unit, *, iostat=ios) solid_dt_cfl, solid_dt_accel, solid_dtcrit + if (ios /= 0) return + read(unit, *, iostat=ios) has_state + if (ios /= 0) return + if (.not. has_state) then + call sph_solid_deallocate_state() + return + end if + call sph_solid_allocate_state(local_ierr) + if (local_ierr /= SPH_SUCCESS) then + ios = 1 + return + end if + do i = 1, npart + read(unit, *, iostat=ios) solid_stress(:,i) + if (ios /= 0) return + read(unit, *, iostat=ios) solid_strain(:,i) + if (ios /= 0) return + read(unit, *, iostat=ios) solid_plastic_strain(i), & + solid_plastic_strain_rate(i), solid_temperature(i), solid_damage(i), & + solid_softening(i), solid_internal_energy(i), solid_reference_volume(i), & + solid_failure_status(i) + if (ios /= 0) return + read(unit, *, iostat=ios) solid_deformation_gradient(:,:,i) + if (ios /= 0) return + read(unit, *, iostat=ios) solid_correction_matrix(:,:,i) + if (ios /= 0) return + read(unit, *, iostat=ios) solid_acc_internal(:,i) + if (ios /= 0) return + end do + end subroutine sph_solid_restart_read + + subroutine sph_solid_restart_clear(ierr) + integer, intent(out) :: ierr + + call sph_solid_clear_material_table() + call sph_solid_deallocate_state() + call sph_solid_update_diagnostics(ierr) + end subroutine sph_solid_restart_clear + +end module sph_solid_restart diff --git a/tests/module/test_explosive_restart_041.f90 b/tests/module/test_explosive_restart_041.f90 index 0216056..7670cf0 100644 --- a/tests/module/test_explosive_restart_041.f90 +++ b/tests/module/test_explosive_restart_041.f90 @@ -67,7 +67,7 @@ contains close(91) if (ios /= 0) call fail('restart header read failed') if (trim(tag) /= 'SPHLITE_RESTART') call fail('restart tag mismatch') - if (trim(state) /= 'STATE_0_4_1') call fail('restart state version mismatch') + if (trim(state) /= 'STATE_0_5') call fail('restart state version mismatch') end subroutine assert_restart_header subroutine assert_ok(ierr_value, message) diff --git a/tests/module/test_output.f90 b/tests/module/test_output.f90 index b51f8b1..8ef1bcd 100644 --- a/tests/module/test_output.f90 +++ b/tests/module/test_output.f90 @@ -106,7 +106,7 @@ contains subroutine check_particle_header(filename) character(len=*), intent(in) :: filename - character(len=256) :: header + character(len=512) :: header integer :: ios open(unit=71, file=trim(filename), status='old', action='read', iostat=ios) @@ -117,7 +117,10 @@ contains if (trim(header) /= 'id,phase,status,material,section,x,y,z,' // & 'vx,vy,vz,rho,pressure,temperature,h,explosive_detonation_time,' // & 'explosive_lambda,explosive_initial_mass,explosive_remaining_mass,' // & - 'explosive_released_mass,explosive_released_energy') then + 'explosive_released_mass,explosive_released_energy,' // & + 'solid_stress_xx,solid_stress_yy,solid_stress_zz,solid_stress_xy,' // & + 'solid_stress_yz,solid_stress_zx,solid_plastic_strain,solid_damage,' // & + 'solid_softening,solid_failure_status') then call fail('particle CSV header mismatch') end if end subroutine check_particle_header @@ -156,6 +159,17 @@ contains call assert_summary_metric(filename, 'explosive_converted_particles') call assert_summary_metric(filename, 'explosive_source_particles') call assert_summary_metric(filename, 'explosive_source_mass_total') + call assert_summary_metric(filename, 'solid_particles') + call assert_summary_metric(filename, 'solid_failed_particles') + call assert_summary_metric(filename, 'solid_mass') + call assert_summary_metric(filename, 'solid_momentum_x') + call assert_summary_metric(filename, 'solid_kinetic_energy') + call assert_summary_metric(filename, 'solid_internal_energy') + call assert_summary_metric(filename, 'solid_max_von_mises') + call assert_summary_metric(filename, 'solid_plastic_strain_min') + call assert_summary_metric(filename, 'solid_damage_max') + call assert_summary_metric(filename, 'solid_softening_min') + call assert_summary_metric(filename, 'solid_dtcrit') 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 e0fdced..f8d9b9e 100644 --- a/tests/module/test_restart.f90 +++ b/tests/module/test_restart.f90 @@ -168,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_4_1') call fail('restart state version mismatch') + if (trim(state_version) /= 'STATE_0_5') 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') diff --git a/tests/module/test_solid_output.f90 b/tests/module/test_solid_output.f90 new file mode 100644 index 0000000..9f25f03 --- /dev/null +++ b/tests/module/test_solid_output.f90 @@ -0,0 +1,271 @@ +program test_solid_output + 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_estimate_stable_dt, sph_set_output_directory, sph_write_output + use sph_database, only: v, solid_particle_count, solid_failed_count, & + solid_dtcrit, SPH_STATUS_FAILED + use sph_solid, only: sph_solid_mark_particle_failed + use sph_solid_state, only: solid_stress, solid_plastic_strain, solid_damage, & + solid_softening, solid_internal_energy, solid_failure_status + use sph_solid_dynamics, only: sph_solid_update_diagnostics + implicit none + + integer :: ierr + + call check_solid_output_fields() + +contains + + subroutine check_solid_output_fields() + real(rk) :: dtcrit + + call delete_output_pair(50) + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Solid output') + call sph_read_inp('tests/data/solid_body_force_050.inp', ierr) + call assert_ok(ierr, 'read failed for Solid output') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for Solid output') + call seed_solid_state() + call sph_solid_mark_particle_failed(2, ierr) + call assert_ok(ierr, 'mark Solid failed particle failed') + call sph_estimate_stable_dt(dtcrit, ierr) + call assert_ok(ierr, 'Solid output dt estimate failed') + call sph_solid_update_diagnostics(ierr) + call assert_ok(ierr, 'Solid output diagnostics failed') + call sph_set_output_directory('build-codex-unix', ierr) + call assert_ok(ierr, 'Solid output directory failed') + call sph_write_output(50, 0.0_rk, ierr) + call assert_ok(ierr, 'Solid output write failed') + call check_particle_csv('build-codex-unix/sphlite_00000050.csv') + call check_summary_csv('build-codex-unix/sphlite_00000050_summary.csv') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Solid output') + end subroutine check_solid_output_fields + + subroutine seed_solid_state() + v(:,1) = (/2.0_rk, 3.0_rk, 4.0_rk/) + solid_stress(:,1) = (/1.0_rk, 2.0_rk, 3.0_rk, & + 4.0_rk, 5.0_rk, 6.0_rk/) + solid_plastic_strain(1) = 0.25_rk + solid_damage(1) = 0.40_rk + solid_softening(1) = 0.60_rk + solid_internal_energy(1) = 2.0_rk + solid_failure_status(1) = 0 + end subroutine seed_solid_state + + subroutine check_particle_csv(filename) + character(len=*), intent(in) :: filename + character(len=512) :: header + integer :: unit + integer :: ios + integer :: id + integer :: phase + integer :: status + integer :: mat + integer :: sec + integer :: failure + real(rk) :: pos(3) + real(rk) :: vel(3) + real(rk) :: scalar_values(4) + real(rk) :: explosive_values(6) + real(rk) :: stress_values(6) + real(rk) :: plastic + real(rk) :: damage + real(rk) :: softening + + unit = 84 + open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('Solid particle CSV open failed') + read(unit, '(A)', iostat=ios) header + if (ios /= 0) call fail('Solid particle CSV header read failed') + if (trim(header) /= 'id,phase,status,material,section,x,y,z,' // & + 'vx,vy,vz,rho,pressure,temperature,h,explosive_detonation_time,' // & + 'explosive_lambda,explosive_initial_mass,explosive_remaining_mass,' // & + 'explosive_released_mass,explosive_released_energy,' // & + 'solid_stress_xx,solid_stress_yy,solid_stress_zz,solid_stress_xy,' // & + 'solid_stress_yz,solid_stress_zx,solid_plastic_strain,solid_damage,' // & + 'solid_softening,solid_failure_status') then + call fail('Solid particle CSV header mismatch') + end if + read(unit, *, iostat=ios) id, phase, status, mat, sec, pos, vel, & + scalar_values, explosive_values, stress_values, plastic, damage, & + softening, failure + close(unit=unit) + if (ios /= 0) call fail('Solid particle CSV row read failed') + if (id /= 1) call fail('Solid particle CSV id mismatch') + if (maxval(abs(vel - (/2.0_rk, 3.0_rk, 4.0_rk/))) > 1.0e-12_rk) then + call fail('Solid particle CSV velocity mismatch') + end if + if (maxval(abs(stress_values - solid_stress(:,1))) > 1.0e-12_rk) then + call fail('Solid particle CSV stress mismatch') + end if + if (abs(plastic - solid_plastic_strain(1)) > 1.0e-12_rk) then + call fail('Solid particle CSV plastic strain mismatch') + end if + if (abs(damage - solid_damage(1)) > 1.0e-12_rk) then + call fail('Solid particle CSV damage mismatch') + end if + if (abs(softening - solid_softening(1)) > 1.0e-12_rk) then + call fail('Solid particle CSV softening mismatch') + end if + if (failure /= solid_failure_status(1)) call fail('Solid particle CSV failure mismatch') + end subroutine check_particle_csv + + subroutine check_summary_csv(filename) + character(len=*), intent(in) :: filename + + call assert_summary_int(filename, 'solid_particles', solid_particle_count) + call assert_summary_int(filename, 'solid_failed_particles', solid_failed_count) + if (solid_particle_count /= 1) call fail('Solid summary active count mismatch') + if (solid_failed_count /= 1) call fail('Solid summary failed count mismatch') + call assert_summary_present(filename, 'solid_mass') + call assert_summary_present(filename, 'solid_momentum_x') + call assert_summary_present(filename, 'solid_momentum_y') + call assert_summary_present(filename, 'solid_momentum_z') + call assert_summary_positive(filename, 'solid_kinetic_energy') + call assert_summary_positive(filename, 'solid_internal_energy') + call assert_summary_positive(filename, 'solid_max_von_mises') + call assert_summary_present(filename, 'solid_plastic_strain_min') + call assert_summary_present(filename, 'solid_plastic_strain_max') + call assert_summary_present(filename, 'solid_damage_min') + call assert_summary_present(filename, 'solid_damage_max') + call assert_summary_present(filename, 'solid_softening_min') + call assert_summary_present(filename, 'solid_softening_max') + call assert_summary_positive(filename, 'solid_dt_cfl') + call assert_summary_positive(filename, 'solid_dt_accel') + call assert_summary_positive(filename, 'solid_dtcrit') + if (solid_dtcrit <= 0.0_rk) call fail('Solid dtcrit diagnostic missing') + end subroutine check_summary_csv + + subroutine assert_summary_present(filename, metric) + character(len=*), intent(in) :: filename + character(len=*), intent(in) :: metric + character(len=256) :: line + integer :: unit + integer :: ios + logical :: found + + found = .false. + unit = 85 + open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('Solid summary CSV open failed') + do + read(unit, '(A)', iostat=ios) line + if (ios /= 0) exit + if (index(line, trim(metric) // ',') == 1) found = .true. + end do + close(unit=unit) + if (.not. found) call fail('Solid summary metric missing') + end subroutine assert_summary_present + + subroutine assert_summary_int(filename, metric, expected) + character(len=*), intent(in) :: filename + character(len=*), intent(in) :: metric + integer, intent(in) :: expected + integer :: actual + + call read_summary_int(filename, metric, actual) + if (actual /= expected) call fail('Solid summary integer mismatch') + end subroutine assert_summary_int + + subroutine assert_summary_positive(filename, metric) + character(len=*), intent(in) :: filename + character(len=*), intent(in) :: metric + real(rk) :: value + + call read_summary_real(filename, metric, value) + if (value <= 0.0_rk) call fail('Solid summary positive metric missing') + end subroutine assert_summary_positive + + subroutine read_summary_int(filename, metric, value) + character(len=*), intent(in) :: filename + character(len=*), intent(in) :: metric + integer, intent(out) :: value + character(len=256) :: line + integer :: unit + integer :: ios + integer :: start + + unit = 86 + open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('Solid summary integer open failed') + do + read(unit, '(A)', iostat=ios) line + if (ios /= 0) exit + if (index(line, trim(metric) // ',') == 1) then + start = len_trim(metric) + 2 + read(line(start:), *, iostat=ios) value + close(unit=unit) + if (ios /= 0) call fail('Solid summary integer parse failed') + return + end if + end do + close(unit=unit) + call fail('Solid summary integer missing') + end subroutine read_summary_int + + subroutine read_summary_real(filename, metric, value) + character(len=*), intent(in) :: filename + character(len=*), intent(in) :: metric + real(rk), intent(out) :: value + character(len=256) :: line + integer :: unit + integer :: ios + integer :: start + + unit = 87 + open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('Solid summary real open failed') + do + read(unit, '(A)', iostat=ios) line + if (ios /= 0) exit + if (index(line, trim(metric) // ',') == 1) then + start = len_trim(metric) + 2 + read(line(start:), *, iostat=ios) value + close(unit=unit) + if (ios /= 0) call fail('Solid summary real parse failed') + return + end if + end do + close(unit=unit) + call fail('Solid summary real missing') + end subroutine read_summary_real + + subroutine delete_output_pair(step_value) + integer, intent(in) :: step_value + character(len=256) :: filename + + write(filename, '("build-codex-unix/sphlite_",I8.8,".csv")') step_value + call delete_file_if_exists(filename) + write(filename, '("build-codex-unix/sphlite_",I8.8,"_summary.csv")') step_value + call delete_file_if_exists(filename) + end subroutine delete_output_pair + + subroutine delete_file_if_exists(filename) + character(len=*), intent(in) :: filename + logical :: exists + integer :: ios + + inquire(file=trim(filename), exist=exists) + if (.not. exists) return + open(unit=88, file=trim(filename), status='old', iostat=ios) + if (ios == 0) close(unit=88, status='delete') + end subroutine delete_file_if_exists + + 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_solid_output diff --git a/tests/module/test_solid_restart_041_compat.f90 b/tests/module/test_solid_restart_041_compat.f90 new file mode 100644 index 0000000..f1cb55e --- /dev/null +++ b/tests/module/test_solid_restart_041_compat.f90 @@ -0,0 +1,100 @@ +program test_solid_restart_041_compat + 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_write_restart, sph_read_restart, sph_advance_explicit + use sph_database, only: solid_particle_count, solid_failed_count + use sph_solid, only: solid_material_count + use sph_solid_state, only: solid_stress + implicit none + + integer :: ierr + + call check_041_restart_clears_solid_state() + +contains + + subroutine check_041_restart_clears_solid_state() + call write_current_water_restart('build-codex-unix/restart_water_for_041.tmp') + call write_041_compat_restart('build-codex-unix/restart_water_for_041.tmp', & + 'build-codex-unix/restart_solid_041_compat.tmp') + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Solid 0.4.1 compat') + call sph_read_inp('tests/data/solid_elastic_minimal_050.inp', ierr) + call assert_ok(ierr, 'read Solid seed failed for 0.4.1 compat') + call sph_build_model(ierr) + call assert_ok(ierr, 'build Solid seed failed for 0.4.1 compat') + if (.not. allocated(solid_stress)) call fail('Solid seed state was not allocated') + call sph_read_restart('build-codex-unix/restart_solid_041_compat.tmp', ierr) + call assert_ok(ierr, 'read 0.4.1 restart failed for Solid compat') + if (solid_material_count /= 0) call fail('0.4.1 restart left Solid material table') + if (allocated(solid_stress)) call fail('0.4.1 restart left Solid particle state') + if (solid_particle_count /= 0) call fail('0.4.1 restart left Solid active count') + if (solid_failed_count /= 0) call fail('0.4.1 restart left Solid failed count') + call sph_advance_explicit(1.0e-7_rk, ierr) + call assert_ok(ierr, 'continue 0.4.1 restart failed for Solid compat') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Solid 0.4.1 compat') + end subroutine check_041_restart_clears_solid_state + + subroutine write_current_water_restart(filename) + character(len=*), intent(in) :: filename + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize water fixture failed') + call sph_read_inp('tests/data/water_minimal_020.inp', ierr) + call assert_ok(ierr, 'read water fixture failed') + call sph_build_model(ierr) + call assert_ok(ierr, 'build water fixture failed') + call sph_write_restart(filename, ierr) + call assert_ok(ierr, 'write water fixture failed') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize water fixture failed') + end subroutine write_current_water_restart + + subroutine write_041_compat_restart(source_filename, target_filename) + character(len=*), intent(in) :: source_filename + character(len=*), intent(in) :: target_filename + character(len=512) :: lines(256) + character(len=512) :: line + integer :: line_count + integer :: i + integer :: ios + + line_count = 0 + open(unit=94, file=trim(source_filename), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('open source restart for 0.4.1 compat failed') + do + read(94, '(A)', iostat=ios) line + if (ios /= 0) exit + line_count = line_count + 1 + if (line_count > size(lines)) call fail('0.4.1 compat fixture is too large') + lines(line_count) = line + end do + close(unit=94) + if (line_count <= 7) call fail('0.4.1 compat fixture is too short') + lines(1) = 'SPHLITE_RESTART 0 4 1 STATE_0_4_1' + open(unit=95, file=trim(target_filename), status='unknown', action='write', & + iostat=ios) + if (ios /= 0) call fail('open target restart for 0.4.1 compat failed') + do i = 1, line_count - 7 + write(95, '(A)') trim(lines(i)) + end do + close(unit=95) + end subroutine write_041_compat_restart + + 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_solid_restart_041_compat diff --git a/tests/module/test_solid_restart_050.f90 b/tests/module/test_solid_restart_050.f90 new file mode 100644 index 0000000..99f1285 --- /dev/null +++ b/tests/module/test_solid_restart_050.f90 @@ -0,0 +1,211 @@ +program test_solid_restart_050 + 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_estimate_stable_dt, sph_advance_explicit, sph_write_restart, & + sph_read_restart + use sph_database, only: particle_phase, particle_status, solid_particle_count, & + solid_failed_count, solid_dtcrit, SPH_PHASE_SOLID_FAILED, SPH_STATUS_FAILED + use sph_solid, only: solid_material_count, solid_material_names, & + solid_elastic_e, solid_elastic_nu, solid_johnson_cook_defined, & + solid_jc_a, solid_failure_defined, solid_failure_d, solid_softening_width, & + sph_solid_mark_particle_failed + use sph_solid_state, only: solid_body_force, solid_stress, solid_strain, & + solid_plastic_strain, solid_plastic_strain_rate, solid_temperature, & + solid_damage, solid_softening, solid_internal_energy, & + solid_reference_volume, solid_deformation_gradient, & + solid_correction_matrix, solid_acc_internal, solid_failure_status + use sph_solid_dynamics, only: sph_solid_update_diagnostics + implicit none + + integer :: ierr + + call check_solid_restart_round_trip() + +contains + + subroutine check_solid_restart_round_trip() + real(rk) :: expected_stress(6) + real(rk) :: expected_strain(6) + real(rk) :: expected_deformation(3,3) + real(rk) :: expected_correction(3,3) + real(rk) :: expected_acc_internal(3) + real(rk) :: expected_dtcrit + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Solid restart') + call sph_read_inp('tests/data/solid_restart_050.inp', ierr) + call assert_ok(ierr, 'read failed for Solid restart') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for Solid restart') + call seed_solid_restart_state(expected_stress, expected_strain, & + expected_deformation, expected_correction, expected_acc_internal) + call sph_estimate_stable_dt(expected_dtcrit, ierr) + call assert_ok(ierr, 'Solid restart dt estimate failed') + expected_dtcrit = solid_dtcrit + call sph_solid_update_diagnostics(ierr) + call assert_ok(ierr, 'Solid restart diagnostics failed') + call sph_write_restart('build-codex-unix/restart_solid_050.tmp', ierr) + call assert_ok(ierr, 'Solid restart write failed') + call scramble_solid_state() + call sph_read_restart('build-codex-unix/restart_solid_050.tmp', ierr) + call assert_ok(ierr, 'Solid restart read failed') + call assert_material_table_restored() + call assert_particle_state_restored(expected_stress, expected_strain, & + expected_deformation, expected_correction, expected_acc_internal) + if (solid_particle_count /= 3) call fail('Solid restart active count mismatch') + if (solid_failed_count /= 1) call fail('Solid restart failed count mismatch') + if (abs(solid_dtcrit - expected_dtcrit) > 1.0e-12_rk) then + call fail('Solid restart dtcrit mismatch') + end if + call sph_advance_explicit(1.0e-8_rk, ierr) + call assert_ok(ierr, 'Solid restart continuation failed') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Solid restart') + end subroutine check_solid_restart_round_trip + + subroutine seed_solid_restart_state(expected_stress, expected_strain, & + expected_deformation, expected_correction, expected_acc_internal) + real(rk), intent(out) :: expected_stress(6) + real(rk), intent(out) :: expected_strain(6) + real(rk), intent(out) :: expected_deformation(3,3) + real(rk), intent(out) :: expected_correction(3,3) + real(rk), intent(out) :: expected_acc_internal(3) + + expected_stress = (/11.0_rk, 22.0_rk, 33.0_rk, & + 4.0_rk, 5.0_rk, 6.0_rk/) + expected_strain = (/0.01_rk, 0.02_rk, 0.03_rk, & + 0.004_rk, 0.005_rk, 0.006_rk/) + expected_deformation = reshape((/1.10_rk, 0.01_rk, 0.02_rk, & + 0.03_rk, 1.20_rk, 0.04_rk, 0.05_rk, 0.06_rk, 1.30_rk/), (/3,3/)) + expected_correction = reshape((/0.90_rk, 0.07_rk, 0.08_rk, & + 0.09_rk, 0.80_rk, 0.10_rk, 0.11_rk, 0.12_rk, 0.70_rk/), (/3,3/)) + expected_acc_internal = (/1.1_rk, 2.2_rk, 3.3_rk/) + solid_body_force = (/0.10_rk, 0.20_rk, 0.30_rk/) + solid_stress(:,1) = expected_stress + solid_strain(:,1) = expected_strain + solid_plastic_strain(1) = 0.125_rk + solid_plastic_strain_rate(1) = 2.5_rk + solid_temperature(1) = 310.0_rk + solid_damage(1) = 0.35_rk + solid_softening(1) = 0.65_rk + solid_internal_energy(1) = 9.5_rk + solid_reference_volume(1) = 0.00123_rk + solid_deformation_gradient(:,:,1) = expected_deformation + solid_correction_matrix(:,:,1) = expected_correction + solid_acc_internal(:,1) = expected_acc_internal + solid_failure_status(1) = 0 + call sph_solid_mark_particle_failed(4, ierr) + call assert_ok(ierr, 'mark Solid failed particle for restart failed') + end subroutine seed_solid_restart_state + + subroutine scramble_solid_state() + solid_body_force = -999.0_rk + solid_stress = -999.0_rk + solid_strain = -999.0_rk + solid_plastic_strain = -999.0_rk + solid_plastic_strain_rate = -999.0_rk + solid_temperature = -999.0_rk + solid_damage = -999.0_rk + solid_softening = -999.0_rk + solid_internal_energy = -999.0_rk + solid_reference_volume = -999.0_rk + solid_deformation_gradient = -999.0_rk + solid_correction_matrix = -999.0_rk + solid_acc_internal = -999.0_rk + solid_failure_status = -999 + end subroutine scramble_solid_state + + subroutine assert_material_table_restored() + if (solid_material_count /= 1) call fail('Solid restart material count mismatch') + if (trim(solid_material_names(1)) /= 'STEEL_RESTART') then + call fail('Solid restart material name mismatch') + end if + if (abs(solid_elastic_e(1) - 210.0e9_rk) > 1.0e-3_rk) then + call fail('Solid restart elastic modulus mismatch') + end if + if (abs(solid_elastic_nu(1) - 0.30_rk) > 1.0e-12_rk) then + call fail('Solid restart Poisson ratio mismatch') + end if + if (.not. solid_johnson_cook_defined(1)) then + call fail('Solid restart Johnson-Cook flag missing') + end if + if (abs(solid_jc_a(1) - 792.0e6_rk) > 1.0e-3_rk) then + call fail('Solid restart Johnson-Cook A mismatch') + end if + if (.not. solid_failure_defined(1)) call fail('Solid restart failure flag missing') + if (abs(solid_failure_d(1,1) - 0.05_rk) > 1.0e-12_rk) then + call fail('Solid restart failure parameter mismatch') + end if + if (abs(solid_softening_width(1) - 0.25_rk) > 1.0e-12_rk) then + call fail('Solid restart softening width mismatch') + end if + end subroutine assert_material_table_restored + + subroutine assert_particle_state_restored(expected_stress, expected_strain, & + expected_deformation, expected_correction, expected_acc_internal) + real(rk), intent(in) :: expected_stress(6) + real(rk), intent(in) :: expected_strain(6) + real(rk), intent(in) :: expected_deformation(3,3) + real(rk), intent(in) :: expected_correction(3,3) + real(rk), intent(in) :: expected_acc_internal(3) + + if (maxval(abs(solid_body_force - (/0.10_rk, 0.20_rk, 0.30_rk/))) > & + 1.0e-12_rk) call fail('Solid restart body force mismatch') + if (maxval(abs(solid_stress(:,1) - expected_stress)) > 1.0e-12_rk) then + call fail('Solid restart stress mismatch') + end if + if (maxval(abs(solid_strain(:,1) - expected_strain)) > 1.0e-12_rk) then + call fail('Solid restart strain mismatch') + end if + if (abs(solid_plastic_strain(1) - 0.125_rk) > 1.0e-12_rk) then + call fail('Solid restart plastic strain mismatch') + end if + if (abs(solid_plastic_strain_rate(1) - 2.5_rk) > 1.0e-12_rk) then + call fail('Solid restart plastic rate mismatch') + end if + if (abs(solid_temperature(1) - 310.0_rk) > 1.0e-12_rk) then + call fail('Solid restart temperature mismatch') + end if + if (abs(solid_damage(1) - 0.35_rk) > 1.0e-12_rk) then + call fail('Solid restart damage mismatch') + end if + if (abs(solid_softening(1) - 0.65_rk) > 1.0e-12_rk) then + call fail('Solid restart softening mismatch') + end if + if (abs(solid_internal_energy(1) - 9.5_rk) > 1.0e-12_rk) then + call fail('Solid restart internal energy mismatch') + end if + if (abs(solid_reference_volume(1) - 0.00123_rk) > 1.0e-12_rk) then + call fail('Solid restart reference volume mismatch') + end if + if (maxval(abs(solid_deformation_gradient(:,:,1) - expected_deformation)) > & + 1.0e-12_rk) call fail('Solid restart deformation mismatch') + if (maxval(abs(solid_correction_matrix(:,:,1) - expected_correction)) > & + 1.0e-12_rk) call fail('Solid restart correction mismatch') + if (maxval(abs(solid_acc_internal(:,1) - expected_acc_internal)) > & + 1.0e-12_rk) call fail('Solid restart internal acceleration mismatch') + if (solid_failure_status(4) /= SPH_STATUS_FAILED) then + call fail('Solid restart failure status mismatch') + end if + if (particle_status(4) /= SPH_STATUS_FAILED) call fail('Solid restart status mismatch') + if (particle_phase(4) /= SPH_PHASE_SOLID_FAILED) then + call fail('Solid restart failed phase mismatch') + end if + end subroutine assert_particle_state_restored + + 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_solid_restart_050 diff --git a/todo.md b/todo.md index 5d38ebe..d5bc372 100644 --- a/todo.md +++ b/todo.md @@ -147,33 +147,33 @@ ## 阶段 F:输出、restart 与模块级持久化 -- [ ] 在 summary CSV 中追加 Solid active/failed 粒子数字段。 -- [ ] 在 summary CSV 中追加 Solid 质量和动量字段。 -- [ ] 在 summary CSV 中追加 Solid 动能和内能字段。 -- [ ] 在 summary CSV 中追加 Solid 最大 von Mises 应力字段。 -- [ ] 在 summary CSV 中追加 Solid 塑性应变范围字段。 -- [ ] 在 summary CSV 中追加 Solid damage 范围字段。 -- [ ] 在 summary CSV 中追加 Solid softening 和 dt 字段。 -- [ ] 在粒子 CSV 中追加 Solid stress 关键字段。 -- [ ] 在粒子 CSV 中追加 Solid plastic strain 字段。 -- [ ] 在粒子 CSV 中追加 Solid damage 字段。 -- [ ] 在粒子 CSV 中追加 Solid softening 字段。 -- [ ] 在粒子 CSV 中追加 Solid failure status 字段。 -- [ ] 更新 output 测试以锁定 0.5.0 CSV header。 -- [ ] 新增 `solid_output` 测试并接入 CTest。 -- [ ] 将 restart 状态版本更新为 `STATE_0_5`。 -- [ ] 将 Solid material 表写入 0.5.0 restart。 -- [ ] 将 Solid per-particle 状态写入 0.5.0 restart。 -- [ ] 将 Solid summary 和 dt 诊断写入或可恢复于 0.5.0 restart。 -- [ ] 从 0.5.0 restart 读回 Solid material 表。 -- [ ] 从 0.5.0 restart 读回 Solid per-particle 状态。 -- [ ] 从 0.5.0 restart 读回后继续显式步。 -- [ ] 从 `STATE_0_4_1` restart 读回时初始化空 Solid 状态。 -- [ ] 从 `STATE_0_4` restart 读回时初始化空 Solid 状态。 -- [ ] 从 `STATE_0_3_1` 或更早兼容状态读回时初始化空 Solid 状态。 -- [ ] 确认 restart 后不会重复初始化 Solid 应力、塑性应变、damage 或 softening。 -- [ ] 新增 `solid_restart_050` 测试并接入 CTest。 -- [ ] 新增 `solid_restart_041_compat` 测试并接入 CTest。 +- [x] 在 summary CSV 中追加 Solid active/failed 粒子数字段。 +- [x] 在 summary CSV 中追加 Solid 质量和动量字段。 +- [x] 在 summary CSV 中追加 Solid 动能和内能字段。 +- [x] 在 summary CSV 中追加 Solid 最大 von Mises 应力字段。 +- [x] 在 summary CSV 中追加 Solid 塑性应变范围字段。 +- [x] 在 summary CSV 中追加 Solid damage 范围字段。 +- [x] 在 summary CSV 中追加 Solid softening 和 dt 字段。 +- [x] 在粒子 CSV 中追加 Solid stress 关键字段。 +- [x] 在粒子 CSV 中追加 Solid plastic strain 字段。 +- [x] 在粒子 CSV 中追加 Solid damage 字段。 +- [x] 在粒子 CSV 中追加 Solid softening 字段。 +- [x] 在粒子 CSV 中追加 Solid failure status 字段。 +- [x] 更新 output 测试以锁定 0.5.0 CSV header。 +- [x] 新增 `solid_output` 测试并接入 CTest。 +- [x] 将 restart 状态版本更新为 `STATE_0_5`。 +- [x] 将 Solid material 表写入 0.5.0 restart。 +- [x] 将 Solid per-particle 状态写入 0.5.0 restart。 +- [x] 将 Solid summary 和 dt 诊断写入或可恢复于 0.5.0 restart。 +- [x] 从 0.5.0 restart 读回 Solid material 表。 +- [x] 从 0.5.0 restart 读回 Solid per-particle 状态。 +- [x] 从 0.5.0 restart 读回后继续显式步。 +- [x] 从 `STATE_0_4_1` restart 读回时初始化空 Solid 状态。 +- [x] 从 `STATE_0_4` restart 读回时初始化空 Solid 状态。 +- [x] 从 `STATE_0_3_1` 或更早兼容状态读回时初始化空 Solid 状态。 +- [x] 确认 restart 后不会重复初始化 Solid 应力、塑性应变、damage 或 softening。 +- [x] 新增 `solid_restart_050` 测试并接入 CTest。 +- [x] 新增 `solid_restart_041_compat` 测试并接入 CTest。 ## 阶段 G:Fortran API、C ABI、C++、Python 与 CLI