Files
FemLite/tests/module/test_element_stiffness.f90
2026-06-22 22:14:39 +08:00

341 lines
10 KiB
Fortran

program test_element_stiffness
use fem_precision
use fem_error
use fem_database
use fem_element_stiffness
implicit none
integer, parameter :: MAX_TEST_DOF = 60
real(rk), parameter :: TOL = 1.0e-8_rk
call check_c3d4_stiff()
call check_c3d8_stiff()
call check_c3d10_stiff()
call check_c3d20_stiff()
call check_bad_stiff_type()
contains
subroutine check_c3d4_stiff()
integer :: ierr
integer :: elem_dofs(MAX_TEST_DOF)
integer :: n_elem_dof
real(rk) :: ke(MAX_TEST_DOF,MAX_TEST_DOF)
call setup_c3d4(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(1)
call element_linear_stiffness(1, elem_dofs, n_elem_dof, ke, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(2)
if (n_elem_dof .ne. 12) call fail(3)
if (elem_dofs(1) .ne. 1 .or. elem_dofs(12) .ne. 12) call fail(4)
call check_symmetric(ke, n_elem_dof, 5)
call check_rigid_translation(ke, n_elem_dof, 1, 6)
call check_rigid_translation(ke, n_elem_dof, 2, 7)
call check_rigid_translation(ke, n_elem_dof, 3, 8)
call check_positive_energy(ke, n_elem_dof, 9)
call database_finalize(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(10)
end subroutine check_c3d4_stiff
subroutine check_c3d8_stiff()
integer :: ierr
integer :: elem_dofs(MAX_TEST_DOF)
integer :: n_elem_dof
real(rk) :: ke(MAX_TEST_DOF,MAX_TEST_DOF)
call setup_c3d8(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(20)
call element_linear_stiffness(1, elem_dofs, n_elem_dof, ke, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(21)
if (n_elem_dof .ne. 24) call fail(22)
if (elem_dofs(1) .ne. 1 .or. elem_dofs(24) .ne. 24) call fail(23)
call check_symmetric(ke, n_elem_dof, 24)
call check_rigid_translation(ke, n_elem_dof, 1, 25)
call check_rigid_translation(ke, n_elem_dof, 2, 26)
call check_rigid_translation(ke, n_elem_dof, 3, 27)
call check_positive_energy(ke, n_elem_dof, 28)
call database_finalize(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(29)
end subroutine check_c3d8_stiff
subroutine check_c3d10_stiff()
integer :: ierr
integer :: elem_dofs(MAX_TEST_DOF)
integer :: n_elem_dof
real(rk) :: ke(MAX_TEST_DOF,MAX_TEST_DOF)
call setup_c3d10(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(30)
call element_linear_stiffness(1, elem_dofs, n_elem_dof, ke, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(31)
if (n_elem_dof .ne. 30) call fail(32)
if (elem_dofs(1) .ne. 1 .or. elem_dofs(30) .ne. 30) call fail(33)
call check_symmetric(ke, n_elem_dof, 34)
call check_rigid_translation(ke, n_elem_dof, 1, 35)
call check_rigid_translation(ke, n_elem_dof, 2, 36)
call check_rigid_translation(ke, n_elem_dof, 3, 37)
call check_positive_energy(ke, n_elem_dof, 38)
call database_finalize(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(39)
end subroutine check_c3d10_stiff
subroutine check_c3d20_stiff()
integer :: ierr
integer :: elem_dofs(MAX_TEST_DOF)
integer :: n_elem_dof
real(rk) :: ke(MAX_TEST_DOF,MAX_TEST_DOF)
call setup_c3d20(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(50)
call element_linear_stiffness(1, elem_dofs, n_elem_dof, ke, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(51)
if (n_elem_dof .ne. 60) call fail(52)
if (elem_dofs(1) .ne. 1 .or. elem_dofs(60) .ne. 60) call fail(53)
call check_symmetric(ke, n_elem_dof, 54)
call check_rigid_translation(ke, n_elem_dof, 1, 55)
call check_rigid_translation(ke, n_elem_dof, 2, 56)
call check_rigid_translation(ke, n_elem_dof, 3, 57)
call check_positive_energy(ke, n_elem_dof, 58)
call database_finalize(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(59)
end subroutine check_c3d20_stiff
subroutine check_bad_stiff_type()
integer :: ierr
integer :: elem_dofs(MAX_TEST_DOF)
integer :: n_elem_dof
real(rk) :: ke(MAX_TEST_DOF,MAX_TEST_DOF)
call database_allocate_model(4, 1, 0, 0, 0, 0, 1, 0, 0, &
0, 0, 0, 0, 0, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(70)
call fill_node_ids(4)
elem_id_ext(1) = 1
elem_type(1) = 'S4'
elem_nnode(1) = 4
elem_conn(:,1) = 0
elem_conn(1:4,1) = (/ 1, 2, 3, 4 /)
elem_mat(1) = 1
mat_e(1) = 30.0_rk
mat_nu(1) = 0.25_rk
call database_assign_dofs(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(71)
call element_linear_stiffness(1, elem_dofs, n_elem_dof, ke, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(72)
call database_finalize(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(73)
end subroutine check_bad_stiff_type
subroutine setup_c3d4(ierr)
integer, intent(out) :: ierr
call database_allocate_model(4, 1, 0, 0, 0, 0, 1, 0, 0, &
0, 0, 0, 0, 0, ierr)
if (ierr .ne. FEM_SUCCESS) return
call fill_node_ids(4)
coord0(:,1) = (/ ZERO, ZERO, ZERO /)
coord0(:,2) = (/ ONE, ZERO, ZERO /)
coord0(:,3) = (/ ZERO, ONE, ZERO /)
coord0(:,4) = (/ ZERO, ZERO, ONE /)
elem_id_ext(1) = 1
elem_type(1) = 'C3D4'
elem_nnode(1) = 4
elem_conn(:,1) = 0
elem_conn(1:4,1) = (/ 1, 2, 3, 4 /)
elem_mat(1) = 1
mat_e(1) = 30.0_rk
mat_nu(1) = 0.25_rk
call database_assign_dofs(ierr)
end subroutine setup_c3d4
subroutine setup_c3d8(ierr)
integer, intent(out) :: ierr
call database_allocate_model(8, 1, 0, 0, 0, 0, 1, 0, 0, &
0, 0, 0, 0, 0, ierr)
if (ierr .ne. FEM_SUCCESS) return
call fill_node_ids(8)
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 /)
elem_id_ext(1) = 1
elem_type(1) = 'C3D8'
elem_nnode(1) = 8
elem_conn(:,1) = 0
elem_conn(1:8,1) = (/ 1, 2, 3, 4, 5, 6, 7, 8 /)
elem_mat(1) = 1
mat_e(1) = 30.0_rk
mat_nu(1) = 0.25_rk
call database_assign_dofs(ierr)
end subroutine setup_c3d8
subroutine setup_c3d10(ierr)
integer, intent(out) :: ierr
call database_allocate_model(10, 1, 0, 0, 0, 0, 1, 0, 0, &
0, 0, 0, 0, 0, ierr)
if (ierr .ne. FEM_SUCCESS) return
call fill_node_ids(10)
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))
elem_id_ext(1) = 1
elem_type(1) = 'C3D10'
elem_nnode(1) = 10
elem_conn(:,1) = 0
elem_conn(1:10,1) = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 /)
elem_mat(1) = 1
mat_e(1) = 30.0_rk
mat_nu(1) = 0.25_rk
call database_assign_dofs(ierr)
end subroutine setup_c3d10
subroutine setup_c3d20(ierr)
integer, intent(out) :: ierr
call database_allocate_model(20, 1, 0, 0, 0, 0, 1, 0, 0, &
0, 0, 0, 0, 0, ierr)
if (ierr .ne. FEM_SUCCESS) return
call fill_node_ids(20)
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))
elem_id_ext(1) = 1
elem_type(1) = 'C3D20'
elem_nnode(1) = 20
elem_conn(:,1) = 0
call fill_sequence(elem_conn(:,1), 20)
elem_mat(1) = 1
mat_e(1) = 30.0_rk
mat_nu(1) = 0.25_rk
call database_assign_dofs(ierr)
end subroutine setup_c3d20
subroutine fill_node_ids(count)
integer, intent(in) :: count
integer :: i
do i = 1, count
node_id_ext(i) = i
coord0(:,i) = ZERO
coord(:,i) = ZERO
end do
end subroutine fill_node_ids
subroutine fill_sequence(values, count)
integer, intent(out) :: values(MAX_NEN)
integer, intent(in) :: count
integer :: i
values = 0
do i = 1, count
values(i) = i
end do
end subroutine fill_sequence
subroutine check_symmetric(ke, n, code)
real(rk), intent(in) :: ke(MAX_TEST_DOF,MAX_TEST_DOF)
integer, intent(in) :: n
integer, intent(in) :: code
integer :: i
integer :: j
do i = 1, n
do j = 1, n
if (abs(ke(i,j) - ke(j,i)) .gt. TOL) call fail(code)
end do
end do
end subroutine check_symmetric
subroutine check_rigid_translation(ke, n, idir, code)
real(rk), intent(in) :: ke(MAX_TEST_DOF,MAX_TEST_DOF)
integer, intent(in) :: n
integer, intent(in) :: idir
integer, intent(in) :: code
real(rk) :: x(MAX_TEST_DOF)
real(rk) :: y(MAX_TEST_DOF)
integer :: i
x = ZERO
y = ZERO
do i = idir, n, NDIM
x(i) = ONE
end do
y(1:n) = matmul(ke(1:n,1:n), x(1:n))
if (maxval(abs(y(1:n))) .gt. TOL) call fail(code)
end subroutine check_rigid_translation
subroutine check_positive_energy(ke, n, code)
real(rk), intent(in) :: ke(MAX_TEST_DOF,MAX_TEST_DOF)
integer, intent(in) :: n
integer, intent(in) :: code
real(rk) :: x(MAX_TEST_DOF)
real(rk) :: y(MAX_TEST_DOF)
x = ZERO
y = ZERO
x(n) = ONE
y(1:n) = matmul(ke(1:n,1:n), x(1:n))
if (dot_product(x(1:n), y(1:n)) .le. TOL) call fail(code)
end subroutine check_positive_energy
subroutine fail(code)
integer, intent(in) :: code
write(*,*) 'test_element_stiffness failed:', code
stop 1
end subroutine fail
end program test_element_stiffness