diff --git a/CMakeLists.txt b/CMakeLists.txt index 196fd72..87493ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,6 +116,12 @@ set_target_properties(test_input PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_ add_test(NAME input COMMAND test_input) set_tests_properties(input PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_input_gas tests/module/test_input_gas.f90) +target_link_libraries(test_input_gas PRIVATE sphlite_core) +set_target_properties(test_input_gas PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME input_gas COMMAND test_input_gas) +set_tests_properties(input_gas 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/database/sph_database.f90 b/src/database/sph_database.f90 index ad553b6..a186e49 100644 --- a/src/database/sph_database.f90 +++ b/src/database/sph_database.f90 @@ -10,6 +10,7 @@ module sph_database integer, parameter :: SPH_MAX_REGIONS = 32 integer, parameter :: SPH_MAX_ELEMENTS = 256 integer, parameter :: SPH_MAX_ELEMENT_NODES = 8 + integer, parameter :: SPH_MAX_SET_MEMBERS = 512 integer, parameter :: SPH_PHASE_WATER = 1 integer, parameter :: SPH_PHASE_GAS = 2 @@ -69,6 +70,24 @@ module sph_database real(rk) :: water_dt_accel = 0.0_rk real(rk) :: water_dt_viscous = 0.0_rk + integer :: gas_particle_count = 0 + real(rk) :: gas_mass = 0.0_rk + real(rk) :: gas_volume = 0.0_rk + real(rk) :: gas_momentum(SPH_NDIM) = 0.0_rk + real(rk) :: gas_center(SPH_NDIM) = 0.0_rk + real(rk) :: gas_mean_pressure = 0.0_rk + real(rk) :: gas_max_pressure = 0.0_rk + real(rk) :: gas_min_density = 0.0_rk + real(rk) :: gas_max_density = 0.0_rk + real(rk) :: gas_equivalent_radius = 0.0_rk + real(rk) :: gas_previous_equivalent_radius = 0.0_rk + real(rk) :: gas_radius_rate = 0.0_rk + real(rk) :: gas_previous_center(SPH_NDIM) = 0.0_rk + real(rk) :: gas_center_shift = 0.0_rk + real(rk) :: gas_dt_cfl = 0.0_rk + real(rk) :: gas_dt_accel = 0.0_rk + real(rk) :: gas_dtcrit = 0.0_rk + integer :: material_count = 0 character(len=SPH_NAME_LEN) :: material_name(SPH_MAX_MATERIALS) character(len=SPH_NAME_LEN) :: material_type(SPH_MAX_MATERIALS) @@ -87,11 +106,13 @@ module sph_database character(len=SPH_NAME_LEN) :: nset_name(SPH_MAX_SETS) integer :: nset_member_count(SPH_MAX_SETS) integer :: nset_first_member(SPH_MAX_SETS) + integer :: nset_members(SPH_MAX_SET_MEMBERS,SPH_MAX_SETS) integer :: elset_count = 0 character(len=SPH_NAME_LEN) :: elset_name(SPH_MAX_SETS) integer :: elset_member_count(SPH_MAX_SETS) integer :: elset_first_member(SPH_MAX_SETS) + integer :: elset_members(SPH_MAX_SET_MEMBERS,SPH_MAX_SETS) integer :: element_count = 0 integer :: element_id_ext(SPH_MAX_ELEMENTS) @@ -174,6 +195,7 @@ contains neighbor_offset(i) = 0 end do call sph_db_clear_water_diagnostics() + call sph_db_clear_gas_diagnostics() end subroutine sph_db_initialize_defaults subroutine sph_db_free() @@ -207,6 +229,7 @@ contains npart = 0 call sph_db_clear_metadata() call sph_db_clear_water_diagnostics() + call sph_db_clear_gas_diagnostics() end subroutine sph_db_free subroutine sph_db_clear_metadata() @@ -226,10 +249,12 @@ contains nset_name = ' ' nset_member_count = 0 nset_first_member = 0 + nset_members = 0 elset_count = 0 elset_name = ' ' elset_member_count = 0 elset_first_member = 0 + elset_members = 0 element_count = 0 element_id_ext = 0 element_type = ' ' @@ -255,6 +280,24 @@ contains water_dt_viscous = 0.0_rk end subroutine sph_db_clear_water_diagnostics + subroutine sph_db_clear_gas_diagnostics() + gas_particle_count = 0 + gas_mass = 0.0_rk + gas_volume = 0.0_rk + gas_momentum = 0.0_rk + gas_center = 0.0_rk + gas_mean_pressure = 0.0_rk + gas_max_pressure = 0.0_rk + gas_min_density = 0.0_rk + gas_max_density = 0.0_rk + gas_equivalent_radius = 0.0_rk + gas_radius_rate = 0.0_rk + gas_center_shift = 0.0_rk + gas_dt_cfl = 0.0_rk + gas_dt_accel = 0.0_rk + gas_dtcrit = 0.0_rk + end subroutine sph_db_clear_gas_diagnostics + subroutine sph_db_allocate_neighbor_storage(max_links, num_cells, ierr) integer, intent(in) :: max_links integer, intent(in) :: num_cells @@ -466,11 +509,13 @@ contains end do end function sph_db_upper - subroutine sph_db_add_nset(name, member_count, first_member, ierr) + subroutine sph_db_add_nset(name, member_count, members, ierr) character(len=*), intent(in) :: name integer, intent(in) :: member_count - integer, intent(in) :: first_member + integer, intent(in) :: members(*) integer, intent(out) :: ierr + integer :: i + integer :: n ierr = SPH_SUCCESS if (nset_count >= SPH_MAX_SETS) then @@ -479,16 +524,26 @@ contains return end if nset_count = nset_count + 1 + n = member_count + if (n < 0) n = 0 + if (n > SPH_MAX_SET_MEMBERS) n = SPH_MAX_SET_MEMBERS nset_name(nset_count) = name - nset_member_count(nset_count) = member_count - nset_first_member(nset_count) = first_member + nset_member_count(nset_count) = n + nset_first_member(nset_count) = 0 + nset_members(:,nset_count) = 0 + do i = 1, n + nset_members(i,nset_count) = members(i) + end do + if (n > 0) nset_first_member(nset_count) = nset_members(1,nset_count) end subroutine sph_db_add_nset - subroutine sph_db_add_elset(name, member_count, first_member, ierr) + subroutine sph_db_add_elset(name, member_count, members, ierr) character(len=*), intent(in) :: name integer, intent(in) :: member_count - integer, intent(in) :: first_member + integer, intent(in) :: members(*) integer, intent(out) :: ierr + integer :: i + integer :: n ierr = SPH_SUCCESS if (elset_count >= SPH_MAX_SETS) then @@ -497,9 +552,17 @@ contains return end if elset_count = elset_count + 1 + n = member_count + if (n < 0) n = 0 + if (n > SPH_MAX_SET_MEMBERS) n = SPH_MAX_SET_MEMBERS elset_name(elset_count) = name - elset_member_count(elset_count) = member_count - elset_first_member(elset_count) = first_member + elset_member_count(elset_count) = n + elset_first_member(elset_count) = 0 + elset_members(:,elset_count) = 0 + do i = 1, n + elset_members(i,elset_count) = members(i) + end do + if (n > 0) elset_first_member(elset_count) = elset_members(1,elset_count) end subroutine sph_db_add_elset subroutine sph_db_add_element(ext_id, etype, conn_count, conn_values, ierr) @@ -546,4 +609,125 @@ contains coupling_region_internal_id(coupling_region_count) = coupling_region_count end subroutine sph_db_add_coupling_region + subroutine sph_db_apply_sections_to_particles(ierr) + integer, intent(out) :: ierr + integer :: sec + integer :: set_index + integer :: member_index + integer :: elem_index + integer :: conn_index + integer :: node_index + + ierr = SPH_SUCCESS + do sec = 1, section_count + set_index = sph_db_find_elset(section_name(sec)) + if (set_index > 0) then + do member_index = 1, elset_member_count(set_index) + elem_index = sph_db_find_element(elset_members(member_index,set_index)) + if (elem_index <= 0) cycle + do conn_index = 1, element_conn_count(elem_index) + node_index = sph_db_find_node(element_conn(conn_index,elem_index)) + if (node_index > 0) call sph_db_assign_particle_section(node_index, sec) + end do + end do + else + set_index = sph_db_find_nset(section_name(sec)) + if (set_index > 0) then + do member_index = 1, nset_member_count(set_index) + node_index = sph_db_find_node(nset_members(member_index,set_index)) + if (node_index > 0) call sph_db_assign_particle_section(node_index, sec) + end do + end if + end if + end do + end subroutine sph_db_apply_sections_to_particles + + subroutine sph_db_assign_particle_section(node_index, sec) + integer, intent(in) :: node_index + integer, intent(in) :: sec + integer :: mat_index + + if (node_index < 1 .or. node_index > npart) return + if (sec < 1 .or. sec > section_count) return + mat_index = section_material_id(sec) + if (mat_index < 1 .or. mat_index > material_count) return + section_id(node_index) = sec + material_id(node_index) = mat_index + particle_phase(node_index) = sph_db_phase_from_material(mat_index) + end subroutine sph_db_assign_particle_section + + integer function sph_db_phase_from_material(mat_index) + integer, intent(in) :: mat_index + character(len=SPH_NAME_LEN) :: mtype + + sph_db_phase_from_material = SPH_PHASE_SOLID + if (mat_index < 1 .or. mat_index > material_count) return + mtype = sph_db_upper(material_type(mat_index)) + if (index(mtype, 'WATER') > 0) then + sph_db_phase_from_material = SPH_PHASE_WATER + else if (index(mtype, 'GAS') > 0) then + sph_db_phase_from_material = SPH_PHASE_GAS + else if (index(mtype, 'EXPLOSIVE') > 0) then + sph_db_phase_from_material = SPH_PHASE_EXPLOSIVE + else if (index(mtype, 'SHELL') > 0) then + sph_db_phase_from_material = SPH_PHASE_SHELL + else if (index(mtype, 'BOUNDARY') > 0) then + sph_db_phase_from_material = SPH_PHASE_BOUNDARY + else + sph_db_phase_from_material = SPH_PHASE_SOLID + end if + end function sph_db_phase_from_material + + integer function sph_db_find_elset(name) + character(len=*), intent(in) :: name + integer :: i + + sph_db_find_elset = 0 + do i = 1, elset_count + if (sph_db_name_equal(elset_name(i), name)) then + sph_db_find_elset = i + return + end if + end do + end function sph_db_find_elset + + integer function sph_db_find_nset(name) + character(len=*), intent(in) :: name + integer :: i + + sph_db_find_nset = 0 + do i = 1, nset_count + if (sph_db_name_equal(nset_name(i), name)) then + sph_db_find_nset = i + return + end if + end do + end function sph_db_find_nset + + integer function sph_db_find_element(ext_id) + integer, intent(in) :: ext_id + integer :: i + + sph_db_find_element = 0 + do i = 1, element_count + if (element_id_ext(i) == ext_id) then + sph_db_find_element = i + return + end if + end do + end function sph_db_find_element + + integer function sph_db_find_node(ext_id) + integer, intent(in) :: ext_id + integer :: i + + sph_db_find_node = 0 + do i = 1, npart + if (particle_id_ext(i) == ext_id) then + sph_db_find_node = i + return + end if + end do + end function sph_db_find_node + end module sph_database diff --git a/src/gas/sph_gas.f90 b/src/gas/sph_gas.f90 index 4c1ad2e..57854b5 100644 --- a/src/gas/sph_gas.f90 +++ b/src/gas/sph_gas.f90 @@ -1,15 +1,643 @@ module sph_gas + use sph_precision, only: rk, SPH_NDIM + use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, SPH_ERROR_INPUT, & + sph_error_set + use sph_database, only: npart, particle_phase, particle_status, material_id, & + material_count, material_name, material_type, material_density, & + section_count, section_material_id, x, v, a, mass, rho, volume, & + pressure, internal_energy, temperature, h, drho_dt, acc_pressure, & + acc_body, neighbor_count, neighbor_offset, neighbor_list, & + gas_particle_count, gas_mass, gas_volume, gas_momentum, gas_center, & + gas_mean_pressure, gas_max_pressure, gas_min_density, gas_max_density, & + gas_equivalent_radius, gas_previous_equivalent_radius, gas_radius_rate, & + gas_previous_center, gas_center_shift, gas_dt_cfl, gas_dt_accel, & + gas_dtcrit, SPH_PHASE_GAS, SPH_STATUS_ACTIVE, sph_db_clear_gas_diagnostics + use sph_particle, only: SPH_KERNEL_DEFAULT, sph_kernel_gradient implicit none + + integer, parameter :: GAS_EOS_NONE = 0 + integer, parameter :: GAS_EOS_IDEAL = 1 + integer, parameter :: GAS_EOS_JWL = 2 + integer, parameter :: GAS_EOS_STIFFENED = 3 + logical :: gas_module_initialized = .false. + logical :: gas_material_seen = .false. + logical :: gas_eos_defined = .false. + logical :: gas_particle_control_defined = .false. + logical :: gas_bubble_history_valid = .false. + character(len=64) :: gas_material_name = 'GAS' + integer :: gas_material_internal_id = 0 + integer :: gas_eos_type = GAS_EOS_NONE + real(rk) :: gas_rho0 = 1.0_rk + real(rk) :: gas_gamma = 1.4_rk + real(rk) :: gas_e0 = 0.0_rk + real(rk) :: gas_p0 = 0.0_rk + real(rk) :: gas_jwl_a = 0.0_rk + real(rk) :: gas_jwl_b = 0.0_rk + real(rk) :: gas_jwl_r1 = 0.0_rk + real(rk) :: gas_jwl_r2 = 0.0_rk + real(rk) :: gas_jwl_omega = 0.0_rk + real(rk) :: gas_pinf = 0.0_rk + real(rk) :: gas_default_h = 1.0_rk + real(rk) :: gas_default_mass = 1.0_rk + real(rk) :: gas_default_volume = 1.0_rk + real(rk) :: gas_body_force(SPH_NDIM) = 0.0_rk contains subroutine sph_gas_initialize() gas_module_initialized = .true. + call sph_gas_reset_state() end subroutine sph_gas_initialize subroutine sph_gas_reset() gas_module_initialized = .false. + call sph_gas_reset_state() end subroutine sph_gas_reset + subroutine sph_gas_reset_state() + gas_material_seen = .false. + gas_eos_defined = .false. + gas_particle_control_defined = .false. + gas_bubble_history_valid = .false. + gas_material_name = 'GAS' + gas_material_internal_id = 0 + gas_eos_type = GAS_EOS_NONE + gas_rho0 = 1.0_rk + gas_gamma = 1.4_rk + gas_e0 = 0.0_rk + gas_p0 = 0.0_rk + gas_jwl_a = 0.0_rk + gas_jwl_b = 0.0_rk + gas_jwl_r1 = 0.0_rk + gas_jwl_r2 = 0.0_rk + gas_jwl_omega = 0.0_rk + gas_pinf = 0.0_rk + gas_default_h = 1.0_rk + gas_default_mass = 1.0_rk + gas_default_volume = 1.0_rk + gas_body_force = 0.0_rk + gas_previous_equivalent_radius = 0.0_rk + gas_previous_center = 0.0_rk + call sph_db_clear_gas_diagnostics() + end subroutine sph_gas_reset_state + + subroutine sph_gas_note_material(name, material_type_in) + character(len=*), intent(in) :: name + character(len=*), intent(in) :: material_type_in + + if (index(sph_gas_upper(material_type_in), 'GAS') > 0) then + gas_material_seen = .true. + gas_material_name = name + gas_material_internal_id = sph_gas_find_material(name) + end if + end subroutine sph_gas_note_material + + subroutine sph_gas_set_eos(eos_type, material_name_in, rho0, gamma, e0, p0, & + a_value, b_value, r1, r2, omega, pinf, ierr) + character(len=*), intent(in) :: eos_type + character(len=*), intent(in) :: material_name_in + real(rk), intent(in) :: rho0 + real(rk), intent(in) :: gamma + real(rk), intent(in) :: e0 + real(rk), intent(in) :: p0 + real(rk), intent(in) :: a_value + real(rk), intent(in) :: b_value + real(rk), intent(in) :: r1 + real(rk), intent(in) :: r2 + real(rk), intent(in) :: omega + real(rk), intent(in) :: pinf + integer, intent(out) :: ierr + character(len=64) :: upper_type + + ierr = SPH_SUCCESS + upper_type = sph_gas_upper(eos_type) + if (len_trim(eos_type) == 0) then + call sph_error_set(SPH_ERROR_INPUT, 'Gas EOS TYPE is required') + ierr = SPH_ERROR_INPUT + return + end if + if (len_trim(material_name_in) == 0) then + call sph_error_set(SPH_ERROR_INPUT, 'Gas EOS MATERIAL is required') + ierr = SPH_ERROR_INPUT + return + end if + if (sph_gas_find_material(material_name_in) <= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'Gas EOS references unknown material') + ierr = SPH_ERROR_INPUT + return + end if + + if (index(upper_type, 'IDEAL_GAS') > 0) then + call sph_gas_set_ideal(material_name_in, gamma, e0, p0, ierr) + else if (index(upper_type, 'JWL') > 0) then + call sph_gas_set_jwl(material_name_in, rho0, a_value, b_value, r1, r2, & + omega, e0, ierr) + else if (index(upper_type, 'STIFFENED_GAS') > 0) then + call sph_gas_set_stiffened(material_name_in, rho0, gamma, e0, pinf, ierr) + else + call sph_error_set(SPH_ERROR_INPUT, 'Gas EOS TYPE is not supported') + ierr = SPH_ERROR_INPUT + end if + end subroutine sph_gas_set_eos + + subroutine sph_gas_set_ideal(material_name_in, gamma, e0, p0, ierr) + character(len=*), intent(in) :: material_name_in + real(rk), intent(in) :: gamma + real(rk), intent(in) :: e0 + real(rk), intent(in) :: p0 + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + if (gamma <= 1.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Ideal gas gamma must be greater than one') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (e0 < 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Ideal gas internal energy must be non-negative') + ierr = SPH_ERROR_ARGUMENT + return + end if + gas_material_seen = .true. + gas_material_name = material_name_in + gas_material_internal_id = sph_gas_find_material(material_name_in) + gas_eos_type = GAS_EOS_IDEAL + gas_gamma = gamma + gas_e0 = e0 + gas_p0 = p0 + gas_eos_defined = .true. + end subroutine sph_gas_set_ideal + + subroutine sph_gas_set_jwl(material_name_in, rho0, a_value, b_value, r1, r2, & + omega, e0, ierr) + character(len=*), intent(in) :: material_name_in + real(rk), intent(in) :: rho0 + real(rk), intent(in) :: a_value + real(rk), intent(in) :: b_value + real(rk), intent(in) :: r1 + real(rk), intent(in) :: r2 + real(rk), intent(in) :: omega + real(rk), intent(in) :: e0 + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + if (rho0 <= 0.0_rk .or. r1 <= 0.0_rk .or. r2 <= 0.0_rk .or. & + omega <= 0.0_rk .or. e0 < 0.0_rk .or. a_value < 0.0_rk .or. & + b_value < 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'JWL gas EOS parameters are invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + gas_material_seen = .true. + gas_material_name = material_name_in + gas_material_internal_id = sph_gas_find_material(material_name_in) + gas_eos_type = GAS_EOS_JWL + gas_rho0 = rho0 + gas_jwl_a = a_value + gas_jwl_b = b_value + gas_jwl_r1 = r1 + gas_jwl_r2 = r2 + gas_jwl_omega = omega + gas_e0 = e0 + gas_eos_defined = .true. + end subroutine sph_gas_set_jwl + + subroutine sph_gas_set_stiffened(material_name_in, rho0, gamma, e0, pinf, ierr) + character(len=*), intent(in) :: material_name_in + real(rk), intent(in) :: rho0 + real(rk), intent(in) :: gamma + real(rk), intent(in) :: e0 + real(rk), intent(in) :: pinf + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + if (rho0 <= 0.0_rk .or. gamma <= 1.0_rk .or. e0 < 0.0_rk .or. & + pinf < 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Stiffened gas EOS parameters are invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + gas_material_seen = .true. + gas_material_name = material_name_in + gas_material_internal_id = sph_gas_find_material(material_name_in) + gas_eos_type = GAS_EOS_STIFFENED + gas_rho0 = rho0 + gas_gamma = gamma + gas_e0 = e0 + gas_pinf = pinf + gas_eos_defined = .true. + end subroutine sph_gas_set_stiffened + + subroutine sph_gas_set_particle_control(h_value, mass_value, volume_value, ierr) + real(rk), intent(in) :: h_value + real(rk), intent(in) :: mass_value + real(rk), intent(in) :: volume_value + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + if (h_value <= 0.0_rk .or. mass_value <= 0.0_rk .or. volume_value <= 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas particle control values must be positive') + ierr = SPH_ERROR_ARGUMENT + return + end if + gas_default_h = h_value + gas_default_mass = mass_value + gas_default_volume = volume_value + gas_particle_control_defined = .true. + end subroutine sph_gas_set_particle_control + + subroutine sph_gas_set_body_force(force, ierr) + real(rk), intent(in) :: force(SPH_NDIM) + integer, intent(out) :: ierr + integer :: i + + gas_body_force = force + do i = 1, npart + if (sph_gas_is_active_particle(i)) acc_body(:,i) = force + end do + ierr = SPH_SUCCESS + end subroutine sph_gas_set_body_force + + subroutine sph_gas_validate_input(ierr) + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + if (.not. gas_material_seen) return + if (.not. gas_eos_defined) then + call sph_error_set(SPH_ERROR_INPUT, 'input is missing *SPHLITE EOS for Gas') + ierr = SPH_ERROR_INPUT + return + end if + if (.not. sph_gas_has_material_density()) then + call sph_error_set(SPH_ERROR_INPUT, 'input is missing *DENSITY for Gas material') + ierr = SPH_ERROR_INPUT + return + end if + if (.not. sph_gas_has_section_binding()) then + call sph_error_set(SPH_ERROR_INPUT, 'input is missing Gas section binding') + ierr = SPH_ERROR_INPUT + end if + end subroutine sph_gas_validate_input + + logical function sph_gas_has_material_density() + integer :: i + + sph_gas_has_material_density = .false. + do i = 1, material_count + if (trim(sph_gas_upper(material_name(i))) == trim(sph_gas_upper(gas_material_name))) then + sph_gas_has_material_density = material_density(i) > 0.0_rk + return + end if + end do + end function sph_gas_has_material_density + + logical function sph_gas_has_section_binding() + integer :: i + + sph_gas_has_section_binding = .false. + do i = 1, section_count + if (section_material_id(i) == gas_material_internal_id) then + sph_gas_has_section_binding = .true. + return + end if + end do + end function sph_gas_has_section_binding + + subroutine sph_gas_apply_initial_conditions(ierr) + integer, intent(out) :: ierr + integer :: i + real(rk) :: density_value + + ierr = SPH_SUCCESS + if (.not. gas_material_seen) return + call sph_gas_validate_input(ierr) + if (ierr /= SPH_SUCCESS) return + density_value = sph_gas_material_density(gas_material_name) + do i = 1, npart + if (particle_status(i) /= SPH_STATUS_ACTIVE) cycle + if (.not. sph_gas_is_material_particle(i)) cycle + particle_phase(i) = SPH_PHASE_GAS + rho(i) = density_value + internal_energy(i) = gas_e0 + temperature(i) = 293.15_rk + mass(i) = gas_default_mass + volume(i) = gas_default_volume + h(i) = gas_default_h + acc_body(:,i) = gas_body_force + call sph_gas_compute_pressure(rho(i), internal_energy(i), pressure(i), ierr) + if (ierr /= SPH_SUCCESS) return + end do + call sph_gas_update_diagnostics(ierr) + end subroutine sph_gas_apply_initial_conditions + + subroutine sph_gas_compute_pressure(rho_value, energy_value, pressure_value, ierr) + real(rk), intent(in) :: rho_value + real(rk), intent(in) :: energy_value + real(rk), intent(out) :: pressure_value + integer, intent(out) :: ierr + real(rk) :: relative_volume + + ierr = SPH_SUCCESS + pressure_value = 0.0_rk + if (rho_value <= 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas density must be positive') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (energy_value < 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas internal energy must be non-negative') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (gas_eos_type == GAS_EOS_IDEAL) then + pressure_value = (gas_gamma - 1.0_rk) * rho_value * energy_value + gas_p0 + else if (gas_eos_type == GAS_EOS_JWL) then + if (gas_rho0 <= 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'JWL gas reference density must be positive') + ierr = SPH_ERROR_ARGUMENT + return + end if + relative_volume = gas_rho0 / rho_value + pressure_value = gas_jwl_a * exp(-gas_jwl_r1 * relative_volume) + & + gas_jwl_b * exp(-gas_jwl_r2 * relative_volume) + & + gas_jwl_omega * energy_value / relative_volume + else if (gas_eos_type == GAS_EOS_STIFFENED) then + pressure_value = (gas_gamma - 1.0_rk) * rho_value * energy_value - & + gas_gamma * gas_pinf + else + call sph_error_set(SPH_ERROR_INPUT, 'Gas EOS is not defined') + ierr = SPH_ERROR_INPUT + return + end if + if (.not. sph_gas_is_finite(pressure_value)) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas pressure is not finite') + ierr = SPH_ERROR_ARGUMENT + end if + end subroutine sph_gas_compute_pressure + + subroutine sph_gas_update_pressure(ierr) + integer, intent(out) :: ierr + integer :: i + + ierr = SPH_SUCCESS + do i = 1, npart + if (.not. sph_gas_is_active_particle(i)) cycle + call sph_gas_compute_pressure(rho(i), internal_energy(i), pressure(i), ierr) + if (ierr /= SPH_SUCCESS) return + end do + end subroutine sph_gas_update_pressure + + subroutine sph_gas_compute_density_rate(ierr) + integer, intent(out) :: ierr + integer :: i + integer :: n + integer :: j + integer :: index + real(rk) :: grad_w(SPH_NDIM) + real(rk) :: rij(SPH_NDIM) + real(rk) :: vij(SPH_NDIM) + + ierr = SPH_SUCCESS + if (npart <= 0) return + drho_dt = 0.0_rk + do i = 1, npart + if (.not. sph_gas_is_active_particle(i)) cycle + do n = 1, neighbor_count(i) + index = neighbor_offset(i) + n - 1 + j = neighbor_list(index) + if (.not. sph_gas_is_active_particle(j)) cycle + rij = x(:,i) - x(:,j) + vij = v(:,i) - v(:,j) + call sph_kernel_gradient(SPH_KERNEL_DEFAULT, rij, h(i), grad_w, ierr) + if (ierr /= SPH_SUCCESS) return + drho_dt(i) = drho_dt(i) + mass(j) * sum(vij * grad_w) + end do + end do + end subroutine sph_gas_compute_density_rate + + subroutine sph_gas_compute_pressure_acceleration(ierr) + integer, intent(out) :: ierr + integer :: i + integer :: n + integer :: j + integer :: index + real(rk) :: grad_w(SPH_NDIM) + real(rk) :: rij(SPH_NDIM) + real(rk) :: pressure_term + + ierr = SPH_SUCCESS + if (npart <= 0) return + acc_pressure = 0.0_rk + do i = 1, npart + if (.not. sph_gas_is_active_particle(i)) cycle + if (rho(i) <= 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas density must be positive for pressure force') + ierr = SPH_ERROR_ARGUMENT + return + end if + do n = 1, neighbor_count(i) + index = neighbor_offset(i) + n - 1 + j = neighbor_list(index) + if (.not. sph_gas_is_active_particle(j)) cycle + if (rho(j) <= 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas neighbor density must be positive') + ierr = SPH_ERROR_ARGUMENT + return + end if + rij = x(:,i) - x(:,j) + call sph_kernel_gradient(SPH_KERNEL_DEFAULT, rij, h(i), grad_w, ierr) + if (ierr /= SPH_SUCCESS) return + pressure_term = pressure(i) / (rho(i) * rho(i)) + pressure(j) / (rho(j) * rho(j)) + acc_pressure(:,i) = acc_pressure(:,i) - mass(j) * pressure_term * grad_w + end do + end do + end subroutine sph_gas_compute_pressure_acceleration + + subroutine sph_gas_compute_acceleration(ierr) + integer, intent(out) :: ierr + integer :: i + + call sph_gas_compute_pressure_acceleration(ierr) + if (ierr /= SPH_SUCCESS) return + do i = 1, npart + if (sph_gas_is_active_particle(i)) a(:,i) = acc_pressure(:,i) + acc_body(:,i) + end do + ierr = SPH_SUCCESS + end subroutine sph_gas_compute_acceleration + + subroutine sph_gas_update_diagnostics(ierr) + integer, intent(out) :: ierr + integer :: i + logical :: first_gas + real(rk) :: pressure_integral + real(rk) :: current_center(SPH_NDIM) + real(rk) :: pi_value + + ierr = SPH_SUCCESS + current_center = gas_center + call sph_db_clear_gas_diagnostics() + first_gas = .true. + pressure_integral = 0.0_rk + do i = 1, npart + if (.not. sph_gas_is_active_particle(i)) cycle + if (volume(i) <= 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas volume must be positive') + ierr = SPH_ERROR_ARGUMENT + return + end if + gas_particle_count = gas_particle_count + 1 + gas_mass = gas_mass + mass(i) + gas_volume = gas_volume + volume(i) + gas_momentum = gas_momentum + mass(i) * v(:,i) + gas_center = gas_center + volume(i) * x(:,i) + pressure_integral = pressure_integral + volume(i) * pressure(i) + if (first_gas) then + gas_max_pressure = pressure(i) + gas_min_density = rho(i) + gas_max_density = rho(i) + first_gas = .false. + else + if (pressure(i) > gas_max_pressure) gas_max_pressure = pressure(i) + if (rho(i) < gas_min_density) gas_min_density = rho(i) + if (rho(i) > gas_max_density) gas_max_density = rho(i) + end if + end do + if (gas_volume > 0.0_rk) then + gas_center = gas_center / gas_volume + gas_mean_pressure = pressure_integral / gas_volume + pi_value = acos(-1.0_rk) + gas_equivalent_radius = (3.0_rk * gas_volume / (4.0_rk * pi_value))**(1.0_rk / 3.0_rk) + end if + if (gas_bubble_history_valid) then + gas_radius_rate = gas_equivalent_radius - gas_previous_equivalent_radius + gas_center_shift = sqrt(sum((gas_center - gas_previous_center) * & + (gas_center - gas_previous_center))) + else + gas_radius_rate = 0.0_rk + gas_center_shift = 0.0_rk + end if + gas_previous_equivalent_radius = gas_equivalent_radius + gas_previous_center = gas_center + gas_bubble_history_valid = gas_particle_count > 0 + current_center = current_center + end subroutine sph_gas_update_diagnostics + + subroutine sph_gas_estimate_dt_limits(dt_cfl, dt_accel, dt_limit, ierr) + real(rk), intent(out) :: dt_cfl + real(rk), intent(out) :: dt_accel + real(rk), intent(out) :: dt_limit + integer, intent(out) :: ierr + integer :: i + real(rk) :: speed + real(rk) :: sound_speed + real(rk) :: accel_norm + real(rk) :: local_dt + + ierr = SPH_SUCCESS + dt_cfl = huge(1.0_rk) + dt_accel = huge(1.0_rk) + do i = 1, npart + if (.not. sph_gas_is_active_particle(i)) cycle + if (h(i) <= 0.0_rk .or. rho(i) <= 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'invalid Gas state for dt estimate') + ierr = SPH_ERROR_ARGUMENT + return + end if + sound_speed = sph_gas_sound_speed(i) + speed = sqrt(sum(v(:,i) * v(:,i))) + local_dt = h(i) / (sound_speed + speed) + if (local_dt < dt_cfl) dt_cfl = local_dt + accel_norm = sqrt(sum(a(:,i) * a(:,i))) + if (accel_norm > 0.0_rk) then + local_dt = sqrt(h(i) / accel_norm) + if (local_dt < dt_accel) dt_accel = local_dt + end if + end do + if (dt_cfl == huge(1.0_rk)) dt_cfl = 0.0_rk + if (dt_accel == huge(1.0_rk)) dt_accel = dt_cfl + dt_limit = dt_cfl + if (dt_accel > 0.0_rk .and. dt_accel < dt_limit) dt_limit = dt_accel + gas_dt_cfl = dt_cfl + gas_dt_accel = dt_accel + gas_dtcrit = dt_limit + end subroutine sph_gas_estimate_dt_limits + + real(rk) function sph_gas_sound_speed(i) + integer, intent(in) :: i + real(rk) :: pressure_scale + + pressure_scale = abs(pressure(i)) + abs(gas_pinf) + 1.0_rk + sph_gas_sound_speed = sqrt(max(pressure_scale / rho(i), 1.0e-12_rk)) + end function sph_gas_sound_speed + + logical function sph_gas_is_active_particle(i) + integer, intent(in) :: i + + sph_gas_is_active_particle = .false. + if (i < 1 .or. i > npart) return + if (particle_phase(i) /= SPH_PHASE_GAS) return + if (particle_status(i) /= SPH_STATUS_ACTIVE) return + sph_gas_is_active_particle = .true. + end function sph_gas_is_active_particle + + logical function sph_gas_is_material_particle(i) + integer, intent(in) :: i + integer :: mat_index + + sph_gas_is_material_particle = .false. + if (i < 1 .or. i > npart) return + mat_index = material_id(i) + if (mat_index < 1 .or. mat_index > material_count) return + if (index(sph_gas_upper(material_type(mat_index)), 'GAS') <= 0) return + sph_gas_is_material_particle = .true. + end function sph_gas_is_material_particle + + integer function sph_gas_find_material(name) + character(len=*), intent(in) :: name + integer :: i + + sph_gas_find_material = 0 + do i = 1, material_count + if (trim(sph_gas_upper(material_name(i))) == trim(sph_gas_upper(name))) then + sph_gas_find_material = i + return + end if + end do + end function sph_gas_find_material + + real(rk) function sph_gas_material_density(name) + character(len=*), intent(in) :: name + integer :: i + + sph_gas_material_density = gas_rho0 + do i = 1, material_count + if (trim(sph_gas_upper(material_name(i))) == trim(sph_gas_upper(name))) then + if (material_density(i) > 0.0_rk) sph_gas_material_density = material_density(i) + return + end if + end do + end function sph_gas_material_density + + logical function sph_gas_is_finite(value) + real(rk), intent(in) :: value + + sph_gas_is_finite = value == value .and. abs(value) < huge(value) + end function sph_gas_is_finite + + function sph_gas_upper(text) result(out) + character(len=*), intent(in) :: text + character(len=len(text)) :: out + integer :: i + integer :: code + + out = text + do i = 1, len(out) + code = ichar(out(i:i)) + if (code >= ichar('a') .and. code <= ichar('z')) then + out(i:i) = char(code - ichar('a') + ichar('A')) + end if + end do + end function sph_gas_upper + end module sph_gas diff --git a/src/input/sph_input_reader.f90 b/src/input/sph_input_reader.f90 index a4e1c76..741c9c5 100644 --- a/src/input/sph_input_reader.f90 +++ b/src/input/sph_input_reader.f90 @@ -8,11 +8,14 @@ module sph_input_reader SPH_REGION_COUPLING_CONTACT, SPH_REGION_COUPLING_CONTINUUM, & sph_db_allocate, sph_db_free, sph_db_set_node, sph_db_add_material, & sph_db_add_section, sph_db_add_nset, sph_db_add_elset, & - sph_db_add_element, sph_db_add_coupling_region + sph_db_add_element, sph_db_add_coupling_region, sph_db_apply_sections_to_particles use sph_database, only: sph_db_set_material_density use sph_water, only: sph_water_note_material, sph_water_set_eos, & sph_water_set_viscosity, sph_water_set_particle_control, & sph_water_set_body_force, sph_water_apply_initial_conditions + use sph_gas, only: sph_gas_note_material, sph_gas_set_eos, & + sph_gas_set_particle_control, sph_gas_set_body_force, & + sph_gas_apply_initial_conditions 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, & @@ -93,8 +96,12 @@ contains ierr = SPH_ERROR_INPUT return end if + call sph_db_apply_sections_to_particles(ierr) + if (ierr /= SPH_SUCCESS) return call sph_water_apply_initial_conditions(ierr) if (ierr /= SPH_SUCCESS) return + call sph_gas_apply_initial_conditions(ierr) + if (ierr /= SPH_SUCCESS) return call sph_neighbor_rebuild(ierr) end subroutine sph_input_build_model @@ -246,6 +253,7 @@ contains if (len_trim(type_value) == 0) type_value = 'UNSPECIFIED' call sph_db_add_material(value, type_value, ierr) call sph_water_note_material(value, type_value) + call sph_gas_note_material(value, type_value) else if (key_id == SPH_KEY_SECTION) then input_has_section = .true. call sph_input_get_option(line, 'ELSET', section_value) @@ -271,7 +279,7 @@ contains else if (index(upper_line, 'COUPLING REGION') > 0) then call sph_input_parse_coupling_region(line, ierr) else if (index(upper_line, 'EOS') > 0) then - call sph_input_parse_water_eos(line, ierr) + call sph_input_parse_eos(line, ierr) else if (index(upper_line, 'VISCOSITY') > 0) then call sph_input_parse_water_viscosity(line, ierr) else if (index(upper_line, 'PARTICLE CONTROL') > 0) then @@ -296,6 +304,21 @@ contains if (ierr /= SPH_SUCCESS) call sph_input_prefix_current_error(ierr) end subroutine sph_input_record_keyword + subroutine sph_input_parse_eos(line, ierr) + character(len=*), intent(in) :: line + integer, intent(out) :: ierr + character(len=SPH_INPUT_FIELD_LEN) :: eos_type + + ierr = SPH_SUCCESS + call sph_input_get_option(line, 'TYPE', eos_type) + eos_type = sph_inp_upper(eos_type) + if (index(eos_type, 'TAIT') > 0) then + call sph_input_parse_water_eos(line, ierr) + else + call sph_input_parse_gas_eos(line, ierr) + end if + end subroutine sph_input_parse_eos + subroutine sph_input_parse_water_eos(line, ierr) character(len=*), intent(in) :: line integer, intent(out) :: ierr @@ -332,6 +355,74 @@ contains b_value, has_b, ierr) end subroutine sph_input_parse_water_eos + subroutine sph_input_parse_gas_eos(line, ierr) + character(len=*), intent(in) :: line + integer, intent(out) :: ierr + character(len=SPH_INPUT_FIELD_LEN) :: eos_type + character(len=SPH_INPUT_FIELD_LEN) :: mat_name + real(rk) :: rho0 + real(rk) :: gamma + real(rk) :: e0 + real(rk) :: p0 + real(rk) :: a_value + real(rk) :: b_value + real(rk) :: r1 + real(rk) :: r2 + real(rk) :: omega + real(rk) :: pinf + character(len=SPH_INPUT_FIELD_LEN) :: upper_type + + ierr = SPH_SUCCESS + call sph_input_get_option(line, 'TYPE', eos_type) + call sph_input_get_option(line, 'MATERIAL', mat_name) + upper_type = sph_inp_upper(eos_type) + rho0 = 0.0_rk + gamma = 0.0_rk + e0 = 0.0_rk + p0 = 0.0_rk + a_value = 0.0_rk + b_value = 0.0_rk + r1 = 0.0_rk + r2 = 0.0_rk + omega = 0.0_rk + pinf = 0.0_rk + + if (len_trim(eos_type) == 0 .or. index(upper_type, 'IDEAL_GAS') > 0) then + call sph_input_get_required_real(line, 'GAMMA', gamma, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_input_get_required_real(line, 'E0', e0, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_input_get_optional_real(line, 'P0', p0, ierr) + if (ierr /= SPH_SUCCESS) return + else if (index(upper_type, 'JWL') > 0) then + call sph_input_get_required_real(line, 'RHO0', rho0, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_input_get_required_real(line, 'A', a_value, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_input_get_required_real(line, 'B', b_value, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_input_get_required_real(line, 'R1', r1, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_input_get_required_real(line, 'R2', r2, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_input_get_required_real(line, 'OMEGA', omega, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_input_get_required_real(line, 'E0', e0, ierr) + if (ierr /= SPH_SUCCESS) return + else if (index(upper_type, 'STIFFENED_GAS') > 0) then + call sph_input_get_required_real(line, 'RHO0', rho0, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_input_get_required_real(line, 'GAMMA', gamma, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_input_get_required_real(line, 'E0', e0, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_input_get_required_real(line, 'PINF', pinf, ierr) + if (ierr /= SPH_SUCCESS) return + end if + call sph_gas_set_eos(eos_type, mat_name, rho0, gamma, e0, p0, & + a_value, b_value, r1, r2, omega, pinf, ierr) + end subroutine sph_input_parse_gas_eos + subroutine sph_input_parse_water_viscosity(line, ierr) character(len=*), intent(in) :: line integer, intent(out) :: ierr @@ -368,6 +459,8 @@ contains call sph_input_get_required_real(line, 'VOLUME', volume_value, ierr) if (ierr /= SPH_SUCCESS) return call sph_water_set_particle_control(h_value, mass_value, volume_value, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_gas_set_particle_control(h_value, mass_value, volume_value, ierr) end subroutine sph_input_parse_particle_control subroutine sph_input_parse_body_force(line, ierr) @@ -384,6 +477,8 @@ contains call sph_input_get_optional_real(line, 'Z', force(3), ierr) if (ierr /= SPH_SUCCESS) return call sph_water_set_body_force(force, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_gas_set_body_force(force, ierr) end subroutine sph_input_parse_body_force subroutine sph_input_parse_density(line, ierr) @@ -543,17 +638,25 @@ contains integer, intent(out) :: ierr character(len=SPH_INPUT_FIELD_LEN) :: fields(SPH_INPUT_MAX_FIELDS) integer :: nfields - integer :: first_member + integer :: members(SPH_INPUT_MAX_FIELDS) + integer :: i integer :: ios ierr = SPH_SUCCESS - first_member = 0 + members = 0 call sph_inp_split_csv(line, fields, nfields) - if (nfields > 0) read(fields(1), *, iostat=ios) first_member + do i = 1, nfields + read(fields(i), *, iostat=ios) members(i) + if (ios /= 0) then + call sph_input_set_error('invalid set member id') + ierr = SPH_ERROR_INPUT + return + end if + end do if (is_node_set) then - call sph_db_add_nset(set_name, nfields, first_member, ierr) + call sph_db_add_nset(set_name, nfields, members, ierr) else - call sph_db_add_elset(set_name, nfields, first_member, ierr) + call sph_db_add_elset(set_name, nfields, members, ierr) end if end subroutine sph_input_parse_set_members diff --git a/src/water/sph_water.f90 b/src/water/sph_water.f90 index 7c034c3..4a6a449 100644 --- a/src/water/sph_water.f90 +++ b/src/water/sph_water.f90 @@ -7,7 +7,7 @@ module sph_water water_momentum, water_max_pressure, water_min_density, water_max_density, & water_pressure_impulse, water_dt_cfl, water_dt_accel, water_dt_viscous, & SPH_PHASE_WATER, SPH_STATUS_ACTIVE, material_count, material_name, & - material_density, & + material_type, material_density, material_id, & sph_db_clear_water_diagnostics use sph_particle, only: SPH_KERNEL_DEFAULT, sph_kernel_gradient implicit none @@ -240,6 +240,7 @@ contains if (ierr /= SPH_SUCCESS) return do i = 1, npart if (particle_status(i) /= SPH_STATUS_ACTIVE) cycle + if (.not. sph_water_is_material_particle(i)) cycle particle_phase(i) = SPH_PHASE_WATER rho(i) = water_rho0 pressure(i) = water_p0 @@ -533,6 +534,18 @@ contains sph_water_is_active_particle = .true. end function sph_water_is_active_particle + logical function sph_water_is_material_particle(i) + integer, intent(in) :: i + integer :: mat_index + + sph_water_is_material_particle = .false. + if (i < 1 .or. i > npart) return + mat_index = material_id(i) + if (mat_index < 1 .or. mat_index > material_count) return + if (index(sph_water_upper(material_type(mat_index)), 'WATER') <= 0) return + sph_water_is_material_particle = .true. + end function sph_water_is_material_particle + function x_vector(i, j) result(rij) integer, intent(in) :: i integer, intent(in) :: j diff --git a/tests/module/test_input_gas.f90 b/tests/module/test_input_gas.f90 new file mode 100644 index 0000000..5119d31 --- /dev/null +++ b/tests/module/test_input_gas.f90 @@ -0,0 +1,117 @@ +program test_input_gas + use sph_error, only: SPH_SUCCESS + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model, & + sph_get_particle_count, sph_get_input_warning_count + use sph_database, only: particle_phase, material_count, section_count, & + SPH_PHASE_WATER, SPH_PHASE_GAS + use sph_gas, only: gas_material_seen, gas_eos_defined, gas_material_name, & + gas_material_internal_id, gas_default_h, gas_default_mass, & + gas_default_volume, gas_body_force, gas_dtcrit, gas_particle_count + implicit none + + integer :: ierr + integer :: count + integer :: warnings + + call check_good_gas_input('tests/data/gas_ideal_minimal_030.inp') + call check_good_gas_input('tests/data/gas_jwl_minimal_030.inp') + call check_good_gas_input('tests/data/gas_stiffened_minimal_030.inp') + call check_warning_input('tests/data/gas_unknown_sphlite_warning_030.inp') + call check_mixed_input() + call check_input_fails('tests/data/gas_missing_density_030.inp') + call check_input_fails('tests/data/gas_missing_eos_030.inp') + call check_input_fails('tests/data/gas_missing_eos_type_030.inp') + call check_input_fails('tests/data/gas_missing_eos_material_030.inp') + call check_input_fails('tests/data/gas_bad_eos_value_030.inp') + +contains + + subroutine check_good_gas_input(filename) + character(len=*), intent(in) :: filename + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Gas input') + call sph_read_inp(filename, ierr) + call assert_ok(ierr, 'Gas input read failed') + if (.not. gas_material_seen) call fail('Gas material was not recognized') + if (.not. gas_eos_defined) call fail('Gas EOS was not recognized') + if (len_trim(gas_material_name) <= 0) call fail('Gas material name missing') + if (gas_material_internal_id <= 0) call fail('Gas material id missing') + if (gas_default_h <= 0.0 .or. gas_default_mass <= 0.0 .or. & + gas_default_volume <= 0.0) call fail('Gas particle control missing') + if (maxval(abs(gas_body_force)) > 1.0e-12) call fail('Gas body force default mismatch') + call sph_build_model(ierr) + call assert_ok(ierr, 'Gas model build failed') + call sph_get_particle_count(count, ierr) + call assert_ok(ierr, 'Gas particle count failed') + if (count /= 4) call fail('Gas particle count mismatch') + if (particle_phase(1) /= SPH_PHASE_GAS) call fail('Gas phase mismatch') + if (gas_particle_count /= 4) call fail('Gas diagnostic count mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Gas input') + end subroutine check_good_gas_input + + subroutine check_warning_input(filename) + character(len=*), intent(in) :: filename + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Gas warning input') + call sph_read_inp(filename, ierr) + call assert_ok(ierr, 'Gas warning input read failed') + call sph_get_input_warning_count(warnings, ierr) + call assert_ok(ierr, 'Gas warning count failed') + if (warnings <= 0) call fail('Gas warning was not counted') + call sph_build_model(ierr) + call assert_ok(ierr, 'Gas warning model build failed') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Gas warning input') + end subroutine check_warning_input + + subroutine check_mixed_input() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for mixed input') + call sph_read_inp('tests/data/water_gas_mixed_030.inp', ierr) + call assert_ok(ierr, 'mixed input read failed') + call sph_build_model(ierr) + call assert_ok(ierr, 'mixed model build failed') + call sph_get_particle_count(count, ierr) + call assert_ok(ierr, 'mixed particle count failed') + if (count /= 4) call fail('mixed particle count mismatch') + if (material_count /= 2) call fail('mixed material metadata mismatch') + if (section_count /= 2) call fail('mixed section metadata mismatch') + if (particle_phase(1) /= SPH_PHASE_WATER) call fail('mixed water phase mismatch') + if (particle_phase(3) /= SPH_PHASE_GAS) call fail('mixed gas phase mismatch') + if (gas_dtcrit < 0.0) call fail('Gas dt diagnostic default mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for mixed input') + end subroutine check_mixed_input + + subroutine check_input_fails(filename) + character(len=*), intent(in) :: filename + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for failing Gas input') + call sph_read_inp(filename, ierr) + if (ierr == SPH_SUCCESS) then + call sph_build_model(ierr) + if (ierr == SPH_SUCCESS) call fail('failing Gas input built successfully') + end if + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after failing Gas input') + end subroutine check_input_fails + + 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_input_gas diff --git a/todo.md b/todo.md index bab59cd..75f14a8 100644 --- a/todo.md +++ b/todo.md @@ -36,29 +36,29 @@ ## 阶段 C:Gas 输入解析 -- [ ] 扩展 material 识别以支持 `TYPE=GAS`。 -- [ ] 保持 set 只作为集合而不参与 Gas 类型判断。 -- [ ] 解析 `*SPHLITE EOS, TYPE=IDEAL_GAS`。 -- [ ] 解析 `*SPHLITE EOS, TYPE=JWL`。 -- [ ] 解析 `*SPHLITE EOS, TYPE=STIFFENED_GAS`。 -- [ ] 校验 Gas EOS 必需字段缺失时报 error。 -- [ ] 校验 Gas EOS 非法数值时报 error。 -- [ ] 校验 Gas material 与 section 绑定关系。 -- [ ] 确认 Gas warning 不影响可运行输入。 -- [ ] 增加 `input_gas` 测试目标并接入 CTest。 +- [x] 扩展 material 识别以支持 `TYPE=GAS`。 +- [x] 保持 set 只作为集合而不参与 Gas 类型判断。 +- [x] 解析 `*SPHLITE EOS, TYPE=IDEAL_GAS`。 +- [x] 解析 `*SPHLITE EOS, TYPE=JWL`。 +- [x] 解析 `*SPHLITE EOS, TYPE=STIFFENED_GAS`。 +- [x] 校验 Gas EOS 必需字段缺失时报 error。 +- [x] 校验 Gas EOS 非法数值时报 error。 +- [x] 校验 Gas material 与 section 绑定关系。 +- [x] 确认 Gas warning 不影响可运行输入。 +- [x] 增加 `input_gas` 测试目标并接入 CTest。 ## 阶段 D:Gas 模块状态 -- [ ] 初始化 Gas 模块生命周期状态。 -- [ ] 重置 Gas 模块时清空 EOS 和诊断状态。 -- [ ] 在数据库中确认 `SPH_PHASE_GAS` 常量与 C/Python 常量一致。 -- [ ] 建立 Gas material 名称和内部 id 记录。 -- [ ] 建立 Gas 默认 `h`、`mass` 和 `volume` 控制参数。 -- [ ] 建立 Gas 外部体力状态。 -- [ ] 建立 Gas 稳定时间步诊断状态。 -- [ ] 建立 Gas summary 诊断状态。 -- [ ] 建立 Gas bubble metrics 历史状态。 -- [ ] 确认 Water 状态不读取 Gas 专属变量。 +- [x] 初始化 Gas 模块生命周期状态。 +- [x] 重置 Gas 模块时清空 EOS 和诊断状态。 +- [x] 在数据库中确认 `SPH_PHASE_GAS` 常量与 C/Python 常量一致。 +- [x] 建立 Gas material 名称和内部 id 记录。 +- [x] 建立 Gas 默认 `h`、`mass` 和 `volume` 控制参数。 +- [x] 建立 Gas 外部体力状态。 +- [x] 建立 Gas 稳定时间步诊断状态。 +- [x] 建立 Gas summary 诊断状态。 +- [x] 建立 Gas bubble metrics 历史状态。 +- [x] 确认 Water 状态不读取 Gas 专属变量。 ## 阶段 E:理想气体 EOS