Add solid linear stiffness matrices

This commit is contained in:
shenw
2026-06-22 21:55:56 +08:00
parent 594af0784f
commit 4f716ad0b1
5 changed files with 560 additions and 1 deletions
+284
View File
@@ -0,0 +1,284 @@
module fem_element_stiffness
use fem_precision
use fem_error
use fem_database
use fem_element_topology
use fem_quadrature
use fem_shape_functions
implicit none
private
public :: element_linear_stiffness
contains
subroutine element_linear_stiffness(elem, elem_dofs, n_elem_dof, ke, ierr)
integer, intent(in) :: elem
integer, intent(out) :: elem_dofs(:)
integer, intent(out) :: n_elem_dof
real(rk), intent(out) :: ke(:,:)
integer, intent(out) :: ierr
character(len=ELEM_TYPE_LEN) :: etype
elem_dofs = 0
n_elem_dof = 0
ke = ZERO
if (elem .lt. 1 .or. elem .gt. nelem) then
call error_set(FEM_ERR_MESH, 'linear stiffness element index is out of range', ierr)
return
end if
if (.not. element_is_solid(elem_type(elem))) then
call error_set(FEM_ERR_MESH, 'linear stiffness is implemented for solid elements only', ierr)
return
end if
etype = adjustl(elem_type(elem))
select case (trim(etype))
case ('C3D4', 'C3D8')
call solid_linear_stiffness(elem, elem_dofs, n_elem_dof, ke, ierr)
case default
call error_set(FEM_ERR_MESH, 'linear stiffness element type is not implemented', ierr)
end select
end subroutine element_linear_stiffness
subroutine solid_linear_stiffness(elem, elem_dofs, n_elem_dof, ke, ierr)
integer, intent(in) :: elem
integer, intent(out) :: elem_dofs(:)
integer, intent(out) :: n_elem_dof
real(rk), intent(out) :: ke(:,:)
integer, intent(out) :: ierr
real(rk) :: dmat(NSTRESS,NSTRESS)
real(rk) :: gp_nat(NDIM,27)
real(rk) :: gp_weight(27)
real(rk) :: natural(NDIM)
real(rk) :: nshape(MAX_NEN)
real(rk) :: dndr(NDIM,MAX_NEN)
real(rk) :: dndx(NDIM,MAX_NEN)
real(rk) :: bmat(NSTRESS,MAX_NEN*3)
real(rk) :: db(NSTRESS,MAX_NEN*3)
real(rk) :: detj
real(rk) :: factor
integer :: q
integer :: npoint
integer :: nlocal
nlocal = elem_nnode(elem)
n_elem_dof = 3 * nlocal
if (size(elem_dofs) .lt. n_elem_dof) then
call error_set(FEM_ERR_INTERNAL, 'linear stiffness dof buffer is too small', ierr)
return
end if
if (size(ke,1) .lt. n_elem_dof .or. size(ke,2) .lt. n_elem_dof) then
call error_set(FEM_ERR_INTERNAL, 'linear stiffness matrix buffer is too small', ierr)
return
end if
call fill_solid_dofs(elem, elem_dofs, n_elem_dof, ierr)
if (ierr .ne. FEM_SUCCESS) return
call elastic_matrix_3d(elem_mat(elem), dmat, ierr)
if (ierr .ne. FEM_SUCCESS) return
call quadrature_get_rule(elem_type(elem), 27, npoint, gp_nat, gp_weight, ierr)
if (ierr .ne. FEM_SUCCESS) return
do q = 1, npoint
natural = gp_nat(:,q)
call shape_evaluate(elem_type(elem), natural, nshape, dndr, ierr)
if (ierr .ne. FEM_SUCCESS) return
call shape_gradients_current(elem, nlocal, dndr, dndx, detj, ierr)
if (ierr .ne. FEM_SUCCESS) return
call build_solid_b(nlocal, dndx, bmat)
db(:,1:n_elem_dof) = matmul(dmat, bmat(:,1:n_elem_dof))
factor = gp_weight(q) * detj
ke(1:n_elem_dof,1:n_elem_dof) = ke(1:n_elem_dof,1:n_elem_dof) + &
factor * matmul(transpose(bmat(:,1:n_elem_dof)), db(:,1:n_elem_dof))
end do
ierr = FEM_SUCCESS
end subroutine solid_linear_stiffness
subroutine fill_solid_dofs(elem, elem_dofs, n_elem_dof, ierr)
integer, intent(in) :: elem
integer, intent(out) :: elem_dofs(:)
integer, intent(in) :: n_elem_dof
integer, intent(out) :: ierr
integer :: local_node
integer :: node
integer :: idir
integer :: pos
integer :: dof
if (.not. allocated(dof_id)) then
call error_set(FEM_ERR_INTERNAL, 'linear stiffness requires assigned dof_id table', ierr)
return
end if
pos = 0
do local_node = 1, elem_nnode(elem)
node = elem_conn(local_node, elem)
if (node .lt. 1 .or. node .gt. nnode) then
call error_set(FEM_ERR_MESH, 'linear stiffness element references invalid node', ierr)
return
end if
do idir = 1, NDIM
dof = dof_id(idir,node)
if (dof .le. 0) then
call error_set(FEM_ERR_MESH, 'linear stiffness element has inactive translation dof', ierr)
return
end if
pos = pos + 1
if (pos .gt. n_elem_dof) then
call error_set(FEM_ERR_INTERNAL, 'linear stiffness dof list overflow', ierr)
return
end if
elem_dofs(pos) = dof
end do
end do
ierr = FEM_SUCCESS
end subroutine fill_solid_dofs
subroutine elastic_matrix_3d(mat, dmat, ierr)
integer, intent(in) :: mat
real(rk), intent(out) :: dmat(NSTRESS,NSTRESS)
integer, intent(out) :: ierr
real(rk) :: young
real(rk) :: poisson
real(rk) :: lambda
real(rk) :: mu
dmat = ZERO
if (mat .lt. 1 .or. mat .gt. n_material) then
call error_set(FEM_ERR_MATERIAL, 'invalid material index for linear stiffness', ierr)
return
end if
young = mat_e(mat)
poisson = mat_nu(mat)
if (young .le. ZERO) then
call error_set(FEM_ERR_MATERIAL, 'linear stiffness requires positive elastic modulus', ierr)
return
end if
if (poisson .le. -ONE .or. poisson .ge. HALF) then
call error_set(FEM_ERR_MATERIAL, 'linear stiffness Poisson ratio must be in (-1,0.5)', ierr)
return
end if
lambda = young * poisson / ((ONE + poisson) * (ONE - 2.0_rk * poisson))
mu = young / (2.0_rk * (ONE + poisson))
dmat(1,1) = lambda + 2.0_rk * mu
dmat(2,2) = dmat(1,1)
dmat(3,3) = dmat(1,1)
dmat(1,2) = lambda
dmat(1,3) = lambda
dmat(2,1) = lambda
dmat(2,3) = lambda
dmat(3,1) = lambda
dmat(3,2) = lambda
dmat(4,4) = mu
dmat(5,5) = mu
dmat(6,6) = mu
ierr = FEM_SUCCESS
end subroutine elastic_matrix_3d
subroutine shape_gradients_current(elem, nlocal, dndr, dndx, detj, ierr)
integer, intent(in) :: elem
integer, intent(in) :: nlocal
real(rk), intent(in) :: dndr(NDIM,MAX_NEN)
real(rk), intent(out) :: dndx(NDIM,MAX_NEN)
real(rk), intent(out) :: detj
integer, intent(out) :: ierr
real(rk) :: jac(NDIM,NDIM)
real(rk) :: invj(NDIM,NDIM)
integer :: local_node
integer :: node
integer :: i
integer :: j
integer :: k
jac = ZERO
dndx = ZERO
do local_node = 1, nlocal
node = elem_conn(local_node, elem)
do i = 1, NDIM
do j = 1, NDIM
jac(i,j) = jac(i,j) + coord0(i,node) * dndr(j,local_node)
end do
end do
end do
call inverse3(jac, invj, detj, ierr)
if (ierr .ne. FEM_SUCCESS) return
if (detj .le. ZERO) then
call error_set(FEM_ERR_MESH, 'linear stiffness element has non-positive Jacobian', ierr)
return
end if
do local_node = 1, nlocal
do i = 1, NDIM
dndx(i,local_node) = ZERO
do k = 1, NDIM
dndx(i,local_node) = dndx(i,local_node) + invj(k,i) * dndr(k,local_node)
end do
end do
end do
ierr = FEM_SUCCESS
end subroutine shape_gradients_current
subroutine build_solid_b(nlocal, dndx, bmat)
integer, intent(in) :: nlocal
real(rk), intent(in) :: dndx(NDIM,MAX_NEN)
real(rk), intent(out) :: bmat(NSTRESS,MAX_NEN*3)
integer :: local_node
integer :: base
bmat = ZERO
do local_node = 1, nlocal
base = 3 * (local_node - 1)
bmat(1,base+1) = dndx(1,local_node)
bmat(2,base+2) = dndx(2,local_node)
bmat(3,base+3) = dndx(3,local_node)
bmat(4,base+1) = dndx(2,local_node)
bmat(4,base+2) = dndx(1,local_node)
bmat(5,base+1) = dndx(3,local_node)
bmat(5,base+3) = dndx(1,local_node)
bmat(6,base+2) = dndx(3,local_node)
bmat(6,base+3) = dndx(2,local_node)
end do
end subroutine build_solid_b
subroutine inverse3(a, ainv, det, ierr)
real(rk), intent(in) :: a(NDIM,NDIM)
real(rk), intent(out) :: ainv(NDIM,NDIM)
real(rk), intent(out) :: det
integer, intent(out) :: ierr
det = a(1,1) * (a(2,2) * a(3,3) - a(2,3) * a(3,2)) - &
a(1,2) * (a(2,1) * a(3,3) - a(2,3) * a(3,1)) + &
a(1,3) * (a(2,1) * a(3,2) - a(2,2) * a(3,1))
if (abs(det) .le. tiny(ONE)) then
call error_set(FEM_ERR_MESH, 'linear stiffness element Jacobian is singular', ierr)
return
end if
ainv(1,1) = (a(2,2) * a(3,3) - a(2,3) * a(3,2)) / det
ainv(1,2) = -(a(1,2) * a(3,3) - a(1,3) * a(3,2)) / det
ainv(1,3) = (a(1,2) * a(2,3) - a(1,3) * a(2,2)) / det
ainv(2,1) = -(a(2,1) * a(3,3) - a(2,3) * a(3,1)) / det
ainv(2,2) = (a(1,1) * a(3,3) - a(1,3) * a(3,1)) / det
ainv(2,3) = -(a(1,1) * a(2,3) - a(1,3) * a(2,1)) / det
ainv(3,1) = (a(2,1) * a(3,2) - a(2,2) * a(3,1)) / det
ainv(3,2) = -(a(1,1) * a(3,2) - a(1,2) * a(3,1)) / det
ainv(3,3) = (a(1,1) * a(2,2) - a(1,2) * a(2,1)) / det
ierr = FEM_SUCCESS
end subroutine inverse3
end module fem_element_stiffness
+11 -1
View File
@@ -2,6 +2,7 @@ module fem_assembly_csr
use fem_precision
use fem_error
use fem_database
use fem_element_topology
use fem_sparse_csr
implicit none
@@ -25,6 +26,7 @@ contains
integer :: local_node
integer :: node
integer :: dof_num
integer :: last_dof
integer :: dof
n_elem_dof = 0
@@ -42,6 +44,14 @@ contains
call error_set(FEM_ERR_INTERNAL, 'assembly requires allocated element connectivity', ierr)
return
end if
if (element_is_solid(elem_type(elem))) then
last_dof = 3
else if (element_is_shell(elem_type(elem))) then
last_dof = NDOF_NODE
else
call error_set(FEM_ERR_MESH, 'unsupported element type for assembly dofs', ierr)
return
end if
do local_node = 1, elem_nnode(elem)
node = elem_conn(local_node, elem)
@@ -49,7 +59,7 @@ contains
call error_set(FEM_ERR_MESH, 'assembly element references invalid node', ierr)
return
end if
do dof_num = 1, NDOF_NODE
do dof_num = 1, last_dof
dof = dof_id(dof_num, node)
if (dof .gt. 0) then
if (n_elem_dof .ge. size(elem_dofs)) then