diff --git a/CMakeLists.txt b/CMakeLists.txt index 1063921..ccbe40a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -223,6 +223,18 @@ set_target_properties(test_solid_gradient_correction PROPERTIES Fortran_MODULE_D add_test(NAME solid_gradient_correction COMMAND test_solid_gradient_correction) set_tests_properties(solid_gradient_correction PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_solid_reference_neighbor tests/module/test_solid_reference_neighbor.f90) +target_link_libraries(test_solid_reference_neighbor PRIVATE sphlite_core) +set_target_properties(test_solid_reference_neighbor PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME solid_reference_neighbor COMMAND test_solid_reference_neighbor) +set_tests_properties(solid_reference_neighbor PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_solid_correction_matrix tests/module/test_solid_correction_matrix.f90) +target_link_libraries(test_solid_correction_matrix PRIVATE sphlite_core) +set_target_properties(test_solid_correction_matrix PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME solid_correction_matrix COMMAND test_solid_correction_matrix) +set_tests_properties(solid_correction_matrix PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_executable(test_solid_stable_dt tests/module/test_solid_stable_dt.f90) target_link_libraries(test_solid_stable_dt PRIVATE sphlite_core) set_target_properties(test_solid_stable_dt PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") diff --git a/src/database/sph_database.f90 b/src/database/sph_database.f90 index bd42280..e1f9630 100644 --- a/src/database/sph_database.f90 +++ b/src/database/sph_database.f90 @@ -104,6 +104,9 @@ module sph_database real(rk) :: solid_dt_cfl = 0.0_rk real(rk) :: solid_dt_accel = 0.0_rk real(rk) :: solid_dtcrit = 0.0_rk + integer :: solid_reference_neighbor_min = 0 + real(rk) :: solid_correction_det_min = 0.0_rk + integer :: solid_correction_status_max = 0 integer :: material_count = 0 character(len=SPH_NAME_LEN) :: material_name(SPH_MAX_MATERIALS) @@ -334,6 +337,9 @@ contains solid_dt_cfl = 0.0_rk solid_dt_accel = 0.0_rk solid_dtcrit = 0.0_rk + solid_reference_neighbor_min = 0 + solid_correction_det_min = 0.0_rk + solid_correction_status_max = 0 end subroutine sph_db_clear_solid_diagnostics subroutine sph_db_allocate_neighbor_storage(max_links, num_cells, ierr) diff --git a/src/input/sph_input_reader.f90 b/src/input/sph_input_reader.f90 index ab6a984..03aa148 100644 --- a/src/input/sph_input_reader.f90 +++ b/src/input/sph_input_reader.f90 @@ -24,7 +24,8 @@ 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, sph_solid_update_diagnostics + use sph_solid_dynamics, only: sph_solid_set_body_force, & + sph_solid_build_reference_state, 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, & @@ -125,6 +126,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_build_reference_state(ierr) + if (ierr /= SPH_SUCCESS) return call sph_solid_update_diagnostics(ierr) if (ierr /= SPH_SUCCESS) return call sph_neighbor_rebuild(ierr) diff --git a/src/solid/sph_solid.f90 b/src/solid/sph_solid.f90 index f7b7bf2..5c66d6c 100644 --- a/src/solid/sph_solid.f90 +++ b/src/solid/sph_solid.f90 @@ -377,7 +377,10 @@ contains solid_reference_volume(npart), & solid_deformation_gradient(SPH_NDIM,SPH_NDIM,npart), & solid_correction_matrix(SPH_NDIM,SPH_NDIM,npart), & + solid_correction_determinant(npart), & solid_acc_internal(SPH_NDIM,npart), & + solid_reference_neighbor_count(npart), & + solid_reference_degenerate(npart), solid_correction_status(npart), & solid_failure_status(npart), stat=stat) if (stat /= 0) then call sph_solid_deallocate_state() @@ -400,7 +403,11 @@ contains if (allocated(solid_reference_volume)) deallocate(solid_reference_volume) if (allocated(solid_deformation_gradient)) deallocate(solid_deformation_gradient) if (allocated(solid_correction_matrix)) deallocate(solid_correction_matrix) + if (allocated(solid_correction_determinant)) deallocate(solid_correction_determinant) if (allocated(solid_acc_internal)) deallocate(solid_acc_internal) + if (allocated(solid_reference_neighbor_count)) deallocate(solid_reference_neighbor_count) + if (allocated(solid_reference_degenerate)) deallocate(solid_reference_degenerate) + if (allocated(solid_correction_status)) deallocate(solid_correction_status) if (allocated(solid_failure_status)) deallocate(solid_failure_status) end subroutine sph_solid_deallocate_state @@ -416,7 +423,11 @@ contains if (allocated(solid_reference_volume)) solid_reference_volume = 0.0_rk if (allocated(solid_deformation_gradient)) solid_deformation_gradient = 0.0_rk if (allocated(solid_correction_matrix)) solid_correction_matrix = 0.0_rk + if (allocated(solid_correction_determinant)) solid_correction_determinant = 0.0_rk if (allocated(solid_acc_internal)) solid_acc_internal = 0.0_rk + if (allocated(solid_reference_neighbor_count)) solid_reference_neighbor_count = 0 + if (allocated(solid_reference_degenerate)) solid_reference_degenerate = 0 + if (allocated(solid_correction_status)) solid_correction_status = 0 if (allocated(solid_failure_status)) solid_failure_status = SPH_STATUS_ACTIVE end subroutine sph_solid_clear_particle_state diff --git a/src/solid/sph_solid_dynamics.f90 b/src/solid/sph_solid_dynamics.f90 index a6f722d..a48d827 100644 --- a/src/solid/sph_solid_dynamics.f90 +++ b/src/solid/sph_solid_dynamics.f90 @@ -2,12 +2,15 @@ module sph_solid_dynamics 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, & - x, v, a, mass, rho, volume, h, solid_particle_count, solid_failed_count, & + x0, x, v, a, mass, rho, volume, h, 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, SPH_PHASE_SOLID, & + solid_dt_cfl, solid_dt_accel, solid_dtcrit, & + solid_reference_neighbor_min, solid_correction_det_min, & + solid_correction_status_max, SPH_PHASE_SOLID, & SPH_PHASE_SOLID_FAILED, SPH_STATUS_ACTIVE, SPH_STATUS_FAILED, & sph_db_clear_solid_diagnostics use sph_solid_state @@ -84,10 +87,10 @@ contains subroutine sph_solid_build_reference_state(ierr) integer, intent(out) :: ierr integer :: i - integer :: j ierr = SPH_SUCCESS if (.not. allocated(solid_correction_matrix)) return + call sph_solid_clear_reference_state() do i = 1, npart if (.not. sph_solid_is_active(i)) cycle if (h(i) <= 0.0_rk) then @@ -99,15 +102,164 @@ contains if (solid_reference_volume(i) <= 0.0_rk .and. rho(i) > 0.0_rk) then solid_reference_volume(i) = mass(i) / rho(i) end if - solid_correction_matrix(:,:,i) = 0.0_rk - solid_deformation_gradient(:,:,i) = 0.0_rk - do j = 1, SPH_NDIM - solid_correction_matrix(j,j,i) = 1.0_rk - solid_deformation_gradient(j,j,i) = 1.0_rk - end do + if (solid_reference_volume(i) <= 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, & + 'Solid reference state requires positive volume') + ierr = SPH_ERROR_ARGUMENT + return + end if + call sph_solid_set_identity(solid_deformation_gradient(:,:,i)) + end do + do i = 1, npart + if (.not. sph_solid_is_active(i)) cycle + call sph_solid_build_particle_reference(i, ierr) + if (ierr /= SPH_SUCCESS) return end do end subroutine sph_solid_build_reference_state + subroutine sph_solid_clear_reference_state() + if (allocated(solid_reference_volume)) solid_reference_volume = 0.0_rk + if (allocated(solid_deformation_gradient)) solid_deformation_gradient = 0.0_rk + if (allocated(solid_correction_matrix)) solid_correction_matrix = 0.0_rk + if (allocated(solid_correction_determinant)) then + solid_correction_determinant = 0.0_rk + end if + if (allocated(solid_reference_neighbor_count)) then + solid_reference_neighbor_count = 0 + end if + if (allocated(solid_reference_degenerate)) solid_reference_degenerate = 0 + if (allocated(solid_correction_status)) solid_correction_status = 0 + end subroutine sph_solid_clear_reference_state + + subroutine sph_solid_build_particle_reference(index, ierr) + integer, intent(in) :: index + integer, intent(out) :: ierr + integer :: j + integer :: row + integer :: col + real(rk) :: rij(SPH_NDIM) + real(rk) :: distance + real(rk) :: radius + real(rk) :: weight + real(rk) :: moment(SPH_NDIM,SPH_NDIM) + real(rk) :: inverse(SPH_NDIM,SPH_NDIM) + real(rk) :: det_value + logical :: invertible + + ierr = SPH_SUCCESS + moment = 0.0_rk + do j = 1, npart + if (j == index) cycle + if (.not. sph_solid_is_active(j)) cycle + radius = 2.0_rk * max(h(index), h(j)) + rij = x0(:,j) - x0(:,index) + distance = sqrt(sum(rij * rij)) + if (distance <= 0.0_rk .or. distance > radius) cycle + solid_reference_neighbor_count(index) = & + solid_reference_neighbor_count(index) + 1 + weight = solid_reference_volume(j) + do row = 1, SPH_NDIM + do col = 1, SPH_NDIM + moment(row,col) = moment(row,col) + weight * rij(row) * rij(col) + end do + end do + end do + if (solid_reference_neighbor_count(index) < SPH_NDIM) then + call sph_solid_reference_fallback(index, & + SPH_SOLID_CORRECTION_UNDERCONSTRAINED, 0.0_rk) + return + end if + call sph_solid_invert_3x3(moment, inverse, det_value, invertible) + solid_correction_determinant(index) = det_value + if (.not. invertible) then + call sph_solid_reference_fallback(index, & + SPH_SOLID_CORRECTION_SINGULAR, det_value) + return + end if + solid_correction_matrix(:,:,index) = inverse + solid_correction_status(index) = SPH_SOLID_CORRECTION_OK + solid_reference_degenerate(index) = SPH_SOLID_REFERENCE_OK + end subroutine sph_solid_build_particle_reference + + subroutine sph_solid_reference_fallback(index, status, det_value) + integer, intent(in) :: index + integer, intent(in) :: status + real(rk), intent(in) :: det_value + + call sph_solid_set_identity(solid_correction_matrix(:,:,index)) + solid_correction_determinant(index) = det_value + solid_correction_status(index) = status + solid_reference_degenerate(index) = SPH_SOLID_REFERENCE_DEGENERATE + end subroutine sph_solid_reference_fallback + + subroutine sph_solid_invert_3x3(matrix, inverse, det_value, invertible) + real(rk), intent(in) :: matrix(SPH_NDIM,SPH_NDIM) + real(rk), intent(out) :: inverse(SPH_NDIM,SPH_NDIM) + real(rk), intent(out) :: det_value + logical, intent(out) :: invertible + real(rk) :: scale + real(rk) :: tolerance + + inverse = 0.0_rk + det_value = sph_solid_det3(matrix) + invertible = .false. + if (.not. sph_solid_matrix_is_finite(matrix)) return + if (det_value /= det_value) return + scale = maxval(abs(matrix)) + if (scale <= 0.0_rk) return + tolerance = 1000.0_rk * epsilon(1.0_rk) * max(scale**3, tiny(1.0_rk)) + if (abs(det_value) <= tolerance) return + inverse(1,1) = (matrix(2,2) * matrix(3,3) - matrix(2,3) * matrix(3,2)) + inverse(1,2) = (matrix(1,3) * matrix(3,2) - matrix(1,2) * matrix(3,3)) + inverse(1,3) = (matrix(1,2) * matrix(2,3) - matrix(1,3) * matrix(2,2)) + inverse(2,1) = (matrix(2,3) * matrix(3,1) - matrix(2,1) * matrix(3,3)) + inverse(2,2) = (matrix(1,1) * matrix(3,3) - matrix(1,3) * matrix(3,1)) + inverse(2,3) = (matrix(1,3) * matrix(2,1) - matrix(1,1) * matrix(2,3)) + inverse(3,1) = (matrix(2,1) * matrix(3,2) - matrix(2,2) * matrix(3,1)) + inverse(3,2) = (matrix(1,2) * matrix(3,1) - matrix(1,1) * matrix(3,2)) + inverse(3,3) = (matrix(1,1) * matrix(2,2) - matrix(1,2) * matrix(2,1)) + inverse = inverse / det_value + if (.not. sph_solid_matrix_is_finite(inverse)) return + invertible = .true. + end subroutine sph_solid_invert_3x3 + + real(rk) function sph_solid_det3(matrix) + real(rk), intent(in) :: matrix(SPH_NDIM,SPH_NDIM) + + sph_solid_det3 = matrix(1,1) * (matrix(2,2) * matrix(3,3) - & + matrix(2,3) * matrix(3,2)) - & + matrix(1,2) * (matrix(2,1) * matrix(3,3) - & + matrix(2,3) * matrix(3,1)) + & + matrix(1,3) * (matrix(2,1) * matrix(3,2) - & + matrix(2,2) * matrix(3,1)) + end function sph_solid_det3 + + logical function sph_solid_matrix_is_finite(matrix) + real(rk), intent(in) :: matrix(SPH_NDIM,SPH_NDIM) + integer :: row + integer :: col + + sph_solid_matrix_is_finite = .true. + do row = 1, SPH_NDIM + do col = 1, SPH_NDIM + if (matrix(row,col) /= matrix(row,col)) sph_solid_matrix_is_finite = .false. + if (abs(matrix(row,col)) > huge(matrix(row,col)) * 0.5_rk) then + sph_solid_matrix_is_finite = .false. + end if + end do + end do + end function sph_solid_matrix_is_finite + + subroutine sph_solid_set_identity(matrix) + real(rk), intent(out) :: matrix(SPH_NDIM,SPH_NDIM) + integer :: i + + matrix = 0.0_rk + do i = 1, SPH_NDIM + matrix(i,i) = 1.0_rk + end do + end subroutine sph_solid_set_identity + subroutine sph_solid_compute_particle_strain_increment(index, dt, strain_inc, ierr) integer, intent(in) :: index real(rk), intent(in) :: dt @@ -204,6 +356,7 @@ contains solid_max_damage = solid_damage(i) solid_min_softening = solid_softening(i) solid_max_softening = solid_softening(i) + call sph_solid_set_reference_diagnostics(i) first_active = .false. else if (vm_value > solid_max_von_mises) solid_max_von_mises = vm_value @@ -212,10 +365,45 @@ contains call sph_solid_update_range(solid_damage(i), solid_min_damage, solid_max_damage) call sph_solid_update_range(solid_softening(i), solid_min_softening, & solid_max_softening) + call sph_solid_update_reference_diagnostics(i) end if end do end subroutine sph_solid_update_diagnostics + subroutine sph_solid_set_reference_diagnostics(index) + integer, intent(in) :: index + + if (allocated(solid_reference_neighbor_count)) then + solid_reference_neighbor_min = solid_reference_neighbor_count(index) + end if + if (allocated(solid_correction_determinant)) then + solid_correction_det_min = solid_correction_determinant(index) + end if + if (allocated(solid_correction_status)) then + solid_correction_status_max = solid_correction_status(index) + end if + end subroutine sph_solid_set_reference_diagnostics + + subroutine sph_solid_update_reference_diagnostics(index) + integer, intent(in) :: index + + if (allocated(solid_reference_neighbor_count)) then + if (solid_reference_neighbor_count(index) < solid_reference_neighbor_min) then + solid_reference_neighbor_min = solid_reference_neighbor_count(index) + end if + end if + if (allocated(solid_correction_determinant)) then + if (solid_correction_determinant(index) < solid_correction_det_min) then + solid_correction_det_min = solid_correction_determinant(index) + end if + end if + if (allocated(solid_correction_status)) then + if (solid_correction_status(index) > solid_correction_status_max) then + solid_correction_status_max = solid_correction_status(index) + end if + end if + end subroutine sph_solid_update_reference_diagnostics + subroutine sph_solid_estimate_dt_limits(dt_cfl, dt_accel, dt_limit, ierr) real(rk), intent(out) :: dt_cfl real(rk), intent(out) :: dt_accel diff --git a/src/solid/sph_solid_state.f90 b/src/solid/sph_solid_state.f90 index aa40616..2b97218 100644 --- a/src/solid/sph_solid_state.f90 +++ b/src/solid/sph_solid_state.f90 @@ -21,7 +21,18 @@ module sph_solid_state real(rk), allocatable :: solid_reference_volume(:) real(rk), allocatable :: solid_deformation_gradient(:,:,:) real(rk), allocatable :: solid_correction_matrix(:,:,:) + real(rk), allocatable :: solid_correction_determinant(:) real(rk), allocatable :: solid_acc_internal(:,:) + integer, allocatable :: solid_reference_neighbor_count(:) + integer, allocatable :: solid_reference_degenerate(:) + integer, allocatable :: solid_correction_status(:) integer, allocatable :: solid_failure_status(:) + integer, parameter :: SPH_SOLID_REFERENCE_OK = 0 + integer, parameter :: SPH_SOLID_REFERENCE_DEGENERATE = 1 + integer, parameter :: SPH_SOLID_CORRECTION_OK = 0 + integer, parameter :: SPH_SOLID_CORRECTION_UNDERCONSTRAINED = 1 + integer, parameter :: SPH_SOLID_CORRECTION_SINGULAR = 2 + integer, parameter :: SPH_SOLID_CORRECTION_FALLBACK = 3 + end module sph_solid_state diff --git a/tests/module/test_solid_correction_matrix.f90 b/tests/module/test_solid_correction_matrix.f90 new file mode 100644 index 0000000..48e1349 --- /dev/null +++ b/tests/module/test_solid_correction_matrix.f90 @@ -0,0 +1,121 @@ +program test_solid_correction_matrix + use sph_precision, only: rk, SPH_NDIM + use sph_error, only: SPH_SUCCESS + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model + use sph_database, only: npart, solid_correction_det_min, & + solid_correction_status_max + use sph_solid_state, only: solid_correction_matrix, & + solid_deformation_gradient, solid_correction_determinant, & + solid_correction_status, SPH_SOLID_CORRECTION_OK, & + SPH_SOLID_CORRECTION_UNDERCONSTRAINED + implicit none + + integer :: ierr + + call check_normal_correction_matrix() + call check_degenerate_correction_fallback() + +contains + + subroutine check_normal_correction_matrix() + integer :: i + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for normal correction matrix') + call sph_read_inp('tests/data/solid_reference_normal_051.inp', ierr) + call assert_ok(ierr, 'read failed for normal correction matrix') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for normal correction matrix') + if (solid_correction_status_max /= SPH_SOLID_CORRECTION_OK) then + call fail('normal correction summary status mismatch') + end if + if (solid_correction_det_min <= 0.0_rk) call fail('normal correction determinant minimum missing') + do i = 1, npart + if (solid_correction_status(i) /= SPH_SOLID_CORRECTION_OK) then + call fail('normal correction particle status mismatch') + end if + if (solid_correction_determinant(i) <= 0.0_rk) then + call fail('normal correction particle determinant missing') + end if + call assert_finite_matrix(solid_correction_matrix(:,:,i), & + 'normal correction matrix is not finite') + call assert_identity(solid_deformation_gradient(:,:,i), & + 'normal deformation gradient is not identity') + end do + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for normal correction matrix') + end subroutine check_normal_correction_matrix + + subroutine check_degenerate_correction_fallback() + integer :: i + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for degenerate correction matrix') + call sph_read_inp('tests/data/solid_reference_degenerate_051.inp', ierr) + call assert_ok(ierr, 'read failed for degenerate correction matrix') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for degenerate correction matrix') + if (solid_correction_status_max /= SPH_SOLID_CORRECTION_UNDERCONSTRAINED) then + call fail('degenerate correction summary status mismatch') + end if + do i = 1, npart + if (solid_correction_status(i) /= SPH_SOLID_CORRECTION_UNDERCONSTRAINED) then + call fail('degenerate correction particle status mismatch') + end if + if (abs(solid_correction_determinant(i)) > 0.0_rk) then + call fail('degenerate correction determinant mismatch') + end if + call assert_identity(solid_correction_matrix(:,:,i), & + 'degenerate correction fallback mismatch') + call assert_identity(solid_deformation_gradient(:,:,i), & + 'degenerate deformation gradient mismatch') + end do + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for degenerate correction matrix') + end subroutine check_degenerate_correction_fallback + + subroutine assert_identity(matrix, message) + real(rk), intent(in) :: matrix(:,:) + character(len=*), intent(in) :: message + integer :: i + integer :: j + real(rk) :: expected + + do i = 1, SPH_NDIM + do j = 1, SPH_NDIM + expected = 0.0_rk + if (i == j) expected = 1.0_rk + if (abs(matrix(i,j) - expected) > 1.0e-12_rk) call fail(message) + end do + end do + end subroutine assert_identity + + subroutine assert_finite_matrix(matrix, message) + real(rk), intent(in) :: matrix(:,:) + character(len=*), intent(in) :: message + integer :: i + integer :: j + + do i = 1, SPH_NDIM + do j = 1, SPH_NDIM + if (matrix(i,j) /= matrix(i,j)) call fail(message) + if (abs(matrix(i,j)) > huge(matrix(i,j)) * 0.5_rk) call fail(message) + end do + end do + end subroutine assert_finite_matrix + + 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_correction_matrix diff --git a/tests/module/test_solid_gradient_correction.f90 b/tests/module/test_solid_gradient_correction.f90 index 1889227..ced74c6 100644 --- a/tests/module/test_solid_gradient_correction.f90 +++ b/tests/module/test_solid_gradient_correction.f90 @@ -5,7 +5,9 @@ program test_solid_gradient_correction use sph_database, only: npart, h use sph_solid_dynamics, only: sph_solid_build_reference_state use sph_solid_state, only: solid_correction_matrix, solid_deformation_gradient, & - solid_reference_volume + solid_reference_volume, solid_correction_determinant, & + solid_reference_neighbor_count, solid_correction_status, & + SPH_SOLID_CORRECTION_OK implicit none integer :: ierr @@ -27,9 +29,15 @@ contains call sph_solid_build_reference_state(ierr) call assert_ok(ierr, 'Solid reference state build failed') do i = 1, npart - call assert_identity(solid_correction_matrix(:,:,i), 'Solid correction matrix mismatch') call assert_identity(solid_deformation_gradient(:,:,i), 'Solid deformation gradient mismatch') + call assert_finite_matrix(solid_correction_matrix(:,:,i), & + 'Solid correction matrix is not finite') if (solid_reference_volume(i) <= 0.0_rk) call fail('Solid reference volume missing') + if (solid_reference_neighbor_count(i) < 3) call fail('Solid reference neighbor count missing') + if (solid_correction_status(i) /= SPH_SOLID_CORRECTION_OK) then + call fail('Solid correction matrix was not accepted') + end if + if (solid_correction_determinant(i) <= 0.0_rk) call fail('Solid correction determinant missing') end do call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed for Solid reference state') @@ -65,6 +73,20 @@ contains end do end subroutine assert_identity + subroutine assert_finite_matrix(matrix, message) + real(rk), intent(in) :: matrix(:,:) + character(len=*), intent(in) :: message + integer :: i + integer :: j + + do i = 1, SPH_NDIM + do j = 1, SPH_NDIM + if (matrix(i,j) /= matrix(i,j)) call fail(message) + if (abs(matrix(i,j)) > huge(matrix(i,j)) * 0.5_rk) call fail(message) + end do + end do + end subroutine assert_finite_matrix + subroutine assert_ok(ierr_value, message) integer, intent(in) :: ierr_value character(len=*), intent(in) :: message diff --git a/tests/module/test_solid_reference_neighbor.f90 b/tests/module/test_solid_reference_neighbor.f90 new file mode 100644 index 0000000..71fbbca --- /dev/null +++ b/tests/module/test_solid_reference_neighbor.f90 @@ -0,0 +1,92 @@ +program test_solid_reference_neighbor + use sph_error, only: SPH_SUCCESS + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model + use sph_database, only: npart, solid_reference_neighbor_min, & + solid_correction_status_max + use sph_solid_state, only: solid_reference_neighbor_count, & + solid_reference_degenerate, solid_correction_status, & + SPH_SOLID_REFERENCE_OK, SPH_SOLID_REFERENCE_DEGENERATE, & + SPH_SOLID_CORRECTION_OK, SPH_SOLID_CORRECTION_UNDERCONSTRAINED + implicit none + + integer :: ierr + + call check_normal_reference_neighbors() + call check_degenerate_reference_neighbors() + call check_non_solid_is_excluded() + +contains + + subroutine check_normal_reference_neighbors() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for normal reference neighbors') + call sph_read_inp('tests/data/solid_reference_normal_051.inp', ierr) + call assert_ok(ierr, 'read failed for normal reference neighbors') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for normal reference neighbors') + if (solid_reference_neighbor_min < 3) call fail('reference neighbor minimum too small') + if (solid_correction_status_max /= SPH_SOLID_CORRECTION_OK) then + call fail('normal reference correction status mismatch') + end if + if (minval(solid_reference_neighbor_count(1:npart)) < 3) then + call fail('normal reference neighbor count mismatch') + end if + if (maxval(solid_reference_degenerate(1:npart)) /= SPH_SOLID_REFERENCE_OK) then + call fail('normal reference was marked degenerate') + end if + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for normal reference neighbors') + end subroutine check_normal_reference_neighbors + + subroutine check_degenerate_reference_neighbors() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for degenerate reference neighbors') + call sph_read_inp('tests/data/solid_reference_degenerate_051.inp', ierr) + call assert_ok(ierr, 'read failed for degenerate reference neighbors') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for degenerate reference neighbors') + if (solid_reference_neighbor_min >= 3) call fail('degenerate neighbor minimum mismatch') + if (solid_correction_status_max /= SPH_SOLID_CORRECTION_UNDERCONSTRAINED) then + call fail('degenerate correction status mismatch') + end if + if (maxval(solid_reference_degenerate(1:npart)) /= & + SPH_SOLID_REFERENCE_DEGENERATE) then + call fail('degenerate reference was not marked') + end if + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for degenerate reference neighbors') + end subroutine check_degenerate_reference_neighbors + + subroutine check_non_solid_is_excluded() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for mixed reference neighbors') + call sph_read_inp('tests/data/solid_state_mixed_050.inp', ierr) + call assert_ok(ierr, 'read failed for mixed reference neighbors') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for mixed reference neighbors') + if (solid_reference_neighbor_count(1) /= 0) then + call fail('non-Solid particle polluted Solid reference neighbors') + end if + if (solid_correction_status(1) /= SPH_SOLID_CORRECTION_UNDERCONSTRAINED) then + call fail('single Solid reference status mismatch') + end if + if (solid_reference_neighbor_count(2) /= 0) call fail('non-Solid neighbor state mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for mixed reference neighbors') + end subroutine check_non_solid_is_excluded + + 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_reference_neighbor diff --git a/tests/module/test_solid_state_build.f90 b/tests/module/test_solid_state_build.f90 index e9d98cb..099757c 100644 --- a/tests/module/test_solid_state_build.f90 +++ b/tests/module/test_solid_state_build.f90 @@ -8,7 +8,10 @@ program test_solid_state_build solid_bulk_modulus, solid_wave_speed, solid_stress, solid_strain, & solid_plastic_strain, solid_plastic_strain_rate, solid_temperature, & solid_damage, solid_softening, solid_internal_energy, & - solid_deformation_gradient, solid_correction_matrix, solid_failure_status + solid_deformation_gradient, solid_correction_matrix, & + solid_correction_determinant, solid_reference_neighbor_count, & + solid_correction_status, solid_failure_status, & + SPH_SOLID_CORRECTION_OK implicit none integer :: ierr @@ -39,7 +42,15 @@ contains if (any(abs(solid_internal_energy(1:npart)) > 0.0_rk)) call fail('Solid internal energy not zeroed') if (any(abs(solid_softening(1:npart) - 1.0_rk) > 1.0e-12_rk)) call fail('Solid softening default mismatch') call assert_identity(solid_deformation_gradient(:,:,1), 'Solid deformation gradient default mismatch') - call assert_identity(solid_correction_matrix(:,:,1), 'Solid correction matrix default mismatch') + if (solid_reference_neighbor_count(1) < 3) call fail('Solid reference neighbor count missing') + if (solid_correction_status(1) /= SPH_SOLID_CORRECTION_OK) then + call fail('Solid correction status mismatch') + end if + if (solid_correction_determinant(1) <= 0.0_rk) then + call fail('Solid correction determinant missing') + end if + call assert_finite_matrix(solid_correction_matrix(:,:,1), & + 'Solid correction matrix is not finite') if (rho(1) <= 0.0_rk) call fail('Solid density was not applied to particle state') call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed for Solid state input') @@ -61,6 +72,9 @@ contains if (abs(solid_temperature(2)) > 0.0_rk) call fail('non-Solid temperature default mismatch') if (any(abs(solid_deformation_gradient(:,:,2)) > 0.0_rk)) call fail('non-Solid deformation default mismatch') if (any(abs(solid_correction_matrix(:,:,2)) > 0.0_rk)) call fail('non-Solid correction default mismatch') + if (solid_reference_neighbor_count(2) /= 0) call fail('non-Solid neighbor count mismatch') + if (solid_correction_status(2) /= 0) call fail('non-Solid correction status mismatch') + if (abs(solid_correction_determinant(2)) > 0.0_rk) call fail('non-Solid correction determinant mismatch') if (solid_failure_status(2) /= 0) call fail('non-Solid failure status default mismatch') call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed for mixed Solid state input') @@ -92,6 +106,20 @@ contains end do end subroutine assert_identity + subroutine assert_finite_matrix(matrix, message) + real(rk), intent(in) :: matrix(:,:) + character(len=*), intent(in) :: message + integer :: i + integer :: j + + do i = 1, size(matrix, 1) + do j = 1, size(matrix, 2) + if (matrix(i,j) /= matrix(i,j)) call fail(message) + if (abs(matrix(i,j)) > huge(matrix(i,j)) * 0.5_rk) call fail(message) + end do + end do + end subroutine assert_finite_matrix + subroutine assert_ok(ierr_value, message) integer, intent(in) :: ierr_value character(len=*), intent(in) :: message diff --git a/todo.md b/todo.md index 2fb948e..1d867f6 100644 --- a/todo.md +++ b/todo.md @@ -61,31 +61,31 @@ ## 阶段 C:参考邻域、修正矩阵与变形梯度状态 -- [ ] 复核 Solid 参考坐标、参考体积和参考邻域现有状态。 -- [ ] 新增或补齐 Solid 每粒子参考邻居数状态。 -- [ ] 新增或补齐 Solid 参考邻域退化状态。 -- [ ] 新增或补齐 Solid 修正矩阵状态码。 -- [ ] 新增或补齐 Solid 修正矩阵行列式诊断。 -- [ ] 在 Solid reset 路径初始化参考邻域诊断状态。 -- [ ] 在 Solid allocate/build 路径分配参考邻域诊断状态。 -- [ ] 在 Solid clear/finalize 路径释放参考邻域诊断状态。 -- [ ] 构建 active Solid 粒子的参考邻域计数。 -- [ ] 在参考邻域计数中排除非 Solid 粒子污染。 -- [ ] 计算 active Solid 粒子的最小参考邻居数诊断。 -- [ ] 识别参考邻域不足的退化 Solid 粒子。 -- [ ] 对退化参考邻域给出受控 warning 或 error。 -- [ ] 构建 Solid 参考梯度矩阵或等价矩阵。 -- [ ] 计算 Solid 修正矩阵行列式。 -- [ ] 检查 Solid 修正矩阵有限性。 -- [ ] 检查 Solid 修正矩阵近奇异状态。 -- [ ] 为不可逆修正矩阵提供受控 fallback。 -- [ ] 将修正矩阵质量诊断写入 Solid summary。 -- [ ] 将变形梯度初始值设置为单位阵。 -- [ ] 对非 active Solid 粒子设置安全变形梯度默认值。 -- [ ] 新增 `solid_reference_neighbor` 测试。 -- [ ] 新增 `solid_correction_matrix` 测试。 -- [ ] 将 `solid_reference_neighbor` 接入 CTest。 -- [ ] 将 `solid_correction_matrix` 接入 CTest。 +- [x] 复核 Solid 参考坐标、参考体积和参考邻域现有状态。 +- [x] 新增或补齐 Solid 每粒子参考邻居数状态。 +- [x] 新增或补齐 Solid 参考邻域退化状态。 +- [x] 新增或补齐 Solid 修正矩阵状态码。 +- [x] 新增或补齐 Solid 修正矩阵行列式诊断。 +- [x] 在 Solid reset 路径初始化参考邻域诊断状态。 +- [x] 在 Solid allocate/build 路径分配参考邻域诊断状态。 +- [x] 在 Solid clear/finalize 路径释放参考邻域诊断状态。 +- [x] 构建 active Solid 粒子的参考邻域计数。 +- [x] 在参考邻域计数中排除非 Solid 粒子污染。 +- [x] 计算 active Solid 粒子的最小参考邻居数诊断。 +- [x] 识别参考邻域不足的退化 Solid 粒子。 +- [x] 对退化参考邻域给出受控 warning 或 error。 +- [x] 构建 Solid 参考梯度矩阵或等价矩阵。 +- [x] 计算 Solid 修正矩阵行列式。 +- [x] 检查 Solid 修正矩阵有限性。 +- [x] 检查 Solid 修正矩阵近奇异状态。 +- [x] 为不可逆修正矩阵提供受控 fallback。 +- [x] 将修正矩阵质量诊断写入 Solid summary。 +- [x] 将变形梯度初始值设置为单位阵。 +- [x] 对非 active Solid 粒子设置安全变形梯度默认值。 +- [x] 新增 `solid_reference_neighbor` 测试。 +- [x] 新增 `solid_correction_matrix` 测试。 +- [x] 将 `solid_reference_neighbor` 接入 CTest。 +- [x] 将 `solid_correction_matrix` 接入 CTest。 ## 阶段 D:速度梯度、应变率和运动学驱动本构