From f80529a66c953bb82995af34fde8978cf623aeb8 Mon Sep 17 00:00:00 2001 From: shenw Date: Mon, 22 Jun 2026 22:10:42 +0800 Subject: [PATCH] Add quadratic solid lumped mass support --- CMakeLists.txt | 12 ++ src/element/fem_element_mass.f90 | 13 +- tests/module/test_quadratic_mass.f90 | 187 +++++++++++++++++++++++++++ 3 files changed, 201 insertions(+), 11 deletions(-) create mode 100644 tests/module/test_quadratic_mass.f90 diff --git a/CMakeLists.txt b/CMakeLists.txt index e3398c4..45cce09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -144,6 +144,13 @@ add_executable(test_element_stiffness target_link_libraries(test_element_stiffness PRIVATE femlite_core) femlite_apply_fortran_options(test_element_stiffness) +add_executable(test_quadratic_mass + tests/module/test_quadratic_mass.f90 +) + +target_link_libraries(test_quadratic_mass PRIVATE femlite_core) +femlite_apply_fortran_options(test_quadratic_mass) + add_executable(test_solver_implicit tests/module/test_solver_implicit.f90 ) @@ -215,6 +222,11 @@ set_tests_properties(element_stiffness PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" ) +add_test(NAME quadratic_mass COMMAND test_quadratic_mass) +set_tests_properties(quadratic_mass PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" +) + add_test(NAME solver_implicit COMMAND test_solver_implicit) set_tests_properties(solver_implicit PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" diff --git a/src/element/fem_element_mass.f90 b/src/element/fem_element_mass.f90 index 84726eb..5c4107e 100644 --- a/src/element/fem_element_mass.f90 +++ b/src/element/fem_element_mass.f90 @@ -55,28 +55,19 @@ contains nlocal = elem_nnode(elem) select case (trim(etype)) - case ('C3D4') + case ('C3D4', 'C3D8', 'C3D10', 'C3D20') call element_validate_node_count(etype, nlocal, ierr) if (ierr .ne. FEM_SUCCESS) return call solid_volume_by_quadrature(elem, volume, ierr) if (ierr .ne. FEM_SUCCESS) return if (volume .le. ZERO) then - call error_set(FEM_ERR_MESH, 'C3D4 element has non-positive volume', ierr) + call error_set(FEM_ERR_MESH, 'solid element has non-positive volume', ierr) return end if elem_mass = density * volume nodal_mass = elem_mass / real(nlocal, rk) call add_translational_mass(elem, nodal_mass) - case ('C3D8') - call element_validate_node_count(etype, nlocal, ierr) - if (ierr .ne. FEM_SUCCESS) return - call solid_volume_by_quadrature(elem, volume, ierr) - if (ierr .ne. FEM_SUCCESS) return - elem_mass = density * volume - nodal_mass = elem_mass / real(nlocal, rk) - call add_translational_mass(elem, nodal_mass) - case ('S4') call element_validate_node_count(etype, nlocal, ierr) if (ierr .ne. FEM_SUCCESS) return diff --git a/tests/module/test_quadratic_mass.f90 b/tests/module/test_quadratic_mass.f90 new file mode 100644 index 0000000..4a28398 --- /dev/null +++ b/tests/module/test_quadratic_mass.f90 @@ -0,0 +1,187 @@ +program test_quadratic_mass + use fem_precision + use fem_error + use fem_database + use fem_element_mass + implicit none + + real(rk), parameter :: TOL = 1.0e-10_rk + + call check_c3d10_mass() + call check_c3d20_mass() + +contains + + subroutine check_c3d10_mass() + integer :: ierr + real(rk) :: total_mass + real(rk) :: rot_mass + real(rk) :: nodal_mass + + call setup_c3d10(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(1) + + call element_compute_lumped_mass(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(2) + call collect_mass(total_mass, rot_mass) + + call check_close(total_mass, 10.0_rk, 3) + call check_close(rot_mass, ZERO, 4) + nodal_mass = 10.0_rk / 10.0_rk + call check_close(mass_diag(dof_id(1,1)), nodal_mass, 5) + call check_close(mass_diag(dof_id(2,5)), nodal_mass, 6) + call check_close(mass_diag(dof_id(3,10)), nodal_mass, 7) + + call database_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(8) + end subroutine check_c3d10_mass + + subroutine check_c3d20_mass() + integer :: ierr + real(rk) :: total_mass + real(rk) :: rot_mass + real(rk) :: nodal_mass + + call setup_c3d20(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(20) + + call element_compute_lumped_mass(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(21) + call collect_mass(total_mass, rot_mass) + + call check_close(total_mass, 40.0_rk, 22) + call check_close(rot_mass, ZERO, 23) + nodal_mass = 40.0_rk / 20.0_rk + call check_close(mass_diag(dof_id(1,1)), nodal_mass, 24) + call check_close(mass_diag(dof_id(2,9)), nodal_mass, 25) + call check_close(mass_diag(dof_id(3,20)), nodal_mass, 26) + + call database_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(27) + end subroutine check_c3d20_mass + + subroutine setup_c3d10(ierr) + integer, intent(out) :: ierr + + call database_allocate_model(10, 1, 0, 1, 0, 1, 1, 0, 1, & + 0, 0, 0, 0, 0, ierr) + if (ierr .ne. FEM_SUCCESS) return + call fill_common_model('C3D10', 10, 60.0_rk) + + coord0(:,1) = (/ ZERO, ZERO, ZERO /) + coord0(:,2) = (/ ONE, ZERO, ZERO /) + coord0(:,3) = (/ ZERO, ONE, ZERO /) + coord0(:,4) = (/ ZERO, ZERO, ONE /) + coord0(:,5) = HALF * (coord0(:,1) + coord0(:,2)) + coord0(:,6) = HALF * (coord0(:,2) + coord0(:,3)) + coord0(:,7) = HALF * (coord0(:,3) + coord0(:,1)) + coord0(:,8) = HALF * (coord0(:,1) + coord0(:,4)) + coord0(:,9) = HALF * (coord0(:,2) + coord0(:,4)) + coord0(:,10) = HALF * (coord0(:,3) + coord0(:,4)) + coord = coord0 + + call database_assign_dofs(ierr) + end subroutine setup_c3d10 + + subroutine setup_c3d20(ierr) + integer, intent(out) :: ierr + + call database_allocate_model(20, 1, 0, 1, 0, 1, 1, 0, 1, & + 0, 0, 0, 0, 0, ierr) + if (ierr .ne. FEM_SUCCESS) return + call fill_common_model('C3D20', 20, 40.0_rk) + + coord0(:,1) = (/ ZERO, ZERO, ZERO /) + coord0(:,2) = (/ ONE, ZERO, ZERO /) + coord0(:,3) = (/ ONE, ONE, ZERO /) + coord0(:,4) = (/ ZERO, ONE, ZERO /) + coord0(:,5) = (/ ZERO, ZERO, ONE /) + coord0(:,6) = (/ ONE, ZERO, ONE /) + coord0(:,7) = (/ ONE, ONE, ONE /) + coord0(:,8) = (/ ZERO, ONE, ONE /) + coord0(:,9) = HALF * (coord0(:,1) + coord0(:,2)) + coord0(:,10) = HALF * (coord0(:,2) + coord0(:,3)) + coord0(:,11) = HALF * (coord0(:,3) + coord0(:,4)) + coord0(:,12) = HALF * (coord0(:,4) + coord0(:,1)) + coord0(:,13) = HALF * (coord0(:,5) + coord0(:,6)) + coord0(:,14) = HALF * (coord0(:,6) + coord0(:,7)) + coord0(:,15) = HALF * (coord0(:,7) + coord0(:,8)) + coord0(:,16) = HALF * (coord0(:,8) + coord0(:,5)) + coord0(:,17) = HALF * (coord0(:,1) + coord0(:,5)) + coord0(:,18) = HALF * (coord0(:,2) + coord0(:,6)) + coord0(:,19) = HALF * (coord0(:,3) + coord0(:,7)) + coord0(:,20) = HALF * (coord0(:,4) + coord0(:,8)) + coord = coord0 + + call database_assign_dofs(ierr) + end subroutine setup_c3d20 + + subroutine fill_common_model(etype, nlocal, density) + character(len=*), intent(in) :: etype + integer, intent(in) :: nlocal + real(rk), intent(in) :: density + integer :: i + + do i = 1, nlocal + node_id_ext(i) = i + coord0(:,i) = ZERO + coord(:,i) = ZERO + elem_conn(i,1) = i + end do + + elem_id_ext(1) = 1 + elem_type(1) = etype + elem_nnode(1) = nlocal + elem_mat(1) = 1 + elem_sec(1) = 1 + + elset_name(1) = 'EALL' + elset_start(1) = 1 + elset_count(1) = 1 + elset_items(1) = 1 + + mat_name(1) = 'MAT' + mat_density(1) = density + mat_e(1) = ONE + mat_nu(1) = 0.25_rk + + section_type(1) = 'SOLID' + section_elset_name(1) = 'EALL' + section_material_name(1) = 'MAT' + end subroutine fill_common_model + + subroutine collect_mass(total_mass, rot_mass) + real(rk), intent(out) :: total_mass + real(rk), intent(out) :: rot_mass + integer :: node + integer :: idir + integer :: dof + + total_mass = ZERO + rot_mass = ZERO + do node = 1, nnode + dof = dof_id(1,node) + if (dof .gt. 0) total_mass = total_mass + mass_diag(dof) + do idir = 4, NDOF_NODE + dof = dof_id(idir,node) + if (dof .gt. 0) rot_mass = rot_mass + mass_diag(dof) + end do + end do + end subroutine collect_mass + + subroutine check_close(actual, expected, code) + real(rk), intent(in) :: actual + real(rk), intent(in) :: expected + integer, intent(in) :: code + + if (abs(actual - expected) .gt. TOL * max(ONE, abs(expected))) call fail(code) + end subroutine check_close + + subroutine fail(code) + integer, intent(in) :: code + + write(*,*) 'test_quadratic_mass failed:', code + stop 1 + end subroutine fail + +end program test_quadratic_mass