Add basic rigid plane contact support

This commit is contained in:
shenw
2026-06-22 23:33:51 +08:00
parent 473d97891c
commit 131d7eb0c3
5 changed files with 403 additions and 0 deletions
+4
View File
@@ -12,6 +12,7 @@ module fem_api
use fem_solver_implicit
use fem_coupling
use fem_boundary
use fem_contact
use fem_output_csv
implicit none
@@ -764,6 +765,9 @@ contains
call boundary_apply_mpc_forces(ierr)
if (ierr .ne. FEM_SUCCESS) return
call contact_apply_plane_forces(ierr)
if (ierr .ne. FEM_SUCCESS) return
do dof = 1, ndof
rhs = fext(dof) + fsph(dof) + fcontact(dof) - fint(dof)
a(dof) = rhs / mass_diag(dof)
+135
View File
@@ -0,0 +1,135 @@
module fem_contact
use fem_precision
use fem_error
use fem_database
implicit none
private
public :: contact_apply_plane_forces
contains
subroutine contact_apply_plane_forces(ierr)
integer, intent(out) :: ierr
integer :: plane
if (.not. allocated(fcontact)) then
call error_set(FEM_ERR_INTERNAL, 'contact force vector is not allocated', ierr)
return
end if
fcontact = ZERO
do plane = 1, ncontact_plane
call apply_contact_plane(plane, ierr)
if (ierr .ne. FEM_SUCCESS) return
end do
ierr = FEM_SUCCESS
end subroutine contact_apply_plane_forces
subroutine apply_contact_plane(plane, ierr)
integer, intent(in) :: plane
integer, intent(out) :: ierr
integer :: node
integer :: set_id
integer :: pos
integer :: external_id
integer :: ios
character(len=NAME_LEN) :: target
target = adjustl(contact_target(plane))
if (len_trim(target) .eq. 0 .or. trim(target) .eq. 'ALL' .or. &
trim(target) .eq. 'all') then
do node = 1, nnode
call apply_contact_plane_node(plane, node, ierr)
if (ierr .ne. FEM_SUCCESS) return
end do
ierr = FEM_SUCCESS
return
end if
read(target,*,iostat=ios) external_id
if (ios .eq. 0) then
node = database_find_node(external_id)
if (node .eq. 0) then
call error_set(FEM_ERR_INPUT, 'contact plane references unknown node', ierr)
return
end if
call apply_contact_plane_node(plane, node, ierr)
return
end if
set_id = database_find_nset(target)
if (set_id .eq. 0) then
call error_set(FEM_ERR_INPUT, 'contact plane references unknown node set', ierr)
return
end if
do pos = nset_start(set_id), nset_start(set_id) + nset_count(set_id) - 1
node = database_find_node(nset_items(pos))
if (node .eq. 0) then
call error_set(FEM_ERR_INPUT, 'contact node set contains unknown node', ierr)
return
end if
call apply_contact_plane_node(plane, node, ierr)
if (ierr .ne. FEM_SUCCESS) return
end do
ierr = FEM_SUCCESS
end subroutine apply_contact_plane
subroutine apply_contact_plane_node(plane, node, ierr)
integer, intent(in) :: plane
integer, intent(in) :: node
integer, intent(out) :: ierr
real(rk) :: normal(NDIM)
real(rk) :: position(NDIM)
real(rk) :: velocity(NDIM)
real(rk) :: norm_n
real(rk) :: offset
real(rk) :: gap
real(rk) :: vn
real(rk) :: force_mag
integer :: idir
integer :: dof
if (node .lt. 1 .or. node .gt. nnode) then
call error_set(FEM_ERR_INPUT, 'contact node is out of range', ierr)
return
end if
normal = contact_normal(:,plane)
norm_n = sqrt(sum(normal * normal))
if (norm_n .le. tiny(ONE)) then
call error_set(FEM_ERR_INPUT, 'contact plane normal must be non-zero', ierr)
return
end if
normal = normal / norm_n
offset = contact_offset(plane) / norm_n
position = coord0(:,node)
velocity = ZERO
do idir = 1, NDIM
dof = dof_id(idir,node)
if (dof .gt. 0) then
position(idir) = position(idir) + u(dof)
velocity(idir) = v(dof)
end if
end do
gap = dot_product(normal, position) - offset
if (gap .lt. ZERO) then
vn = dot_product(normal, velocity)
force_mag = contact_penalty(plane) * (-gap)
if (vn .lt. ZERO) force_mag = force_mag + contact_damping(plane) * (-vn)
do idir = 1, NDIM
dof = dof_id(idir,node)
if (dof .gt. 0) fcontact(dof) = fcontact(dof) + force_mag * normal(idir)
end do
end if
ierr = FEM_SUCCESS
end subroutine apply_contact_plane_node
end module fem_contact
+102
View File
@@ -21,6 +21,7 @@ module fem_database
integer :: nrobin = 0
integer :: nmpc = 0
integer :: nmpc_term = 0
integer :: ncontact_plane = 0
real(rk), allocatable :: coord0(:,:)
real(rk), allocatable :: coord(:,:)
@@ -133,6 +134,12 @@ module fem_database
integer, allocatable :: mpc_dof(:)
real(rk), allocatable :: mpc_coeff(:)
character(len=NAME_LEN), allocatable :: contact_target(:)
real(rk), allocatable :: contact_normal(:,:)
real(rk), allocatable :: contact_offset(:)
real(rk), allocatable :: contact_penalty(:)
real(rk), allocatable :: contact_damping(:)
contains
subroutine database_allocate_mesh(num_nodes, num_elems, ierr)
@@ -546,6 +553,43 @@ contains
ierr = FEM_SUCCESS
end subroutine database_allocate_mpc
subroutine database_allocate_contact(num_planes, ierr)
integer, intent(in) :: num_planes
integer, intent(out) :: ierr
integer :: stat
if (num_planes .lt. 0) then
call error_set(FEM_ERR_INPUT, 'number of contact planes must be non-negative', ierr)
return
end if
if (allocated(contact_target)) deallocate(contact_target)
if (allocated(contact_normal)) deallocate(contact_normal)
if (allocated(contact_offset)) deallocate(contact_offset)
if (allocated(contact_penalty)) deallocate(contact_penalty)
if (allocated(contact_damping)) deallocate(contact_damping)
ncontact_plane = num_planes
if (num_planes .gt. 0) then
allocate(contact_target(num_planes), contact_normal(NDIM,num_planes), &
contact_offset(num_planes), contact_penalty(num_planes), &
contact_damping(num_planes), stat=stat)
if (stat .ne. 0) then
ncontact_plane = 0
call error_set(FEM_ERR_INTERNAL, 'failed to allocate contact plane arrays', ierr)
return
end if
contact_target = ' '
contact_normal = ZERO
contact_offset = ZERO
contact_penalty = ZERO
contact_damping = ZERO
end if
ierr = FEM_SUCCESS
end subroutine database_allocate_contact
subroutine database_allocate_basic(num_nodes, num_elems, num_dof, ierr)
integer, intent(in) :: num_nodes
integer, intent(in) :: num_elems
@@ -721,6 +765,11 @@ contains
if (allocated(mpc_node)) mpc_node = 0
if (allocated(mpc_dof)) mpc_dof = 0
if (allocated(mpc_coeff)) mpc_coeff = ZERO
if (allocated(contact_target)) contact_target = ' '
if (allocated(contact_normal)) contact_normal = ZERO
if (allocated(contact_offset)) contact_offset = ZERO
if (allocated(contact_penalty)) contact_penalty = ZERO
if (allocated(contact_damping)) contact_damping = ZERO
ierr = FEM_SUCCESS
end subroutine database_initialize
@@ -949,6 +998,9 @@ contains
if (ierr .ne. FEM_SUCCESS) return
call validate_mpc_data(ierr)
if (ierr .ne. FEM_SUCCESS) return
call validate_contact_data(ierr)
end subroutine database_validate_model
subroutine validate_materials(ierr)
@@ -1283,6 +1335,50 @@ contains
ierr = FEM_SUCCESS
end subroutine validate_mpc_data
subroutine validate_contact_data(ierr)
integer, intent(out) :: ierr
integer :: item
real(rk) :: normal_norm
do item = 1, ncontact_plane
if (.not. contact_target_exists(contact_target(item))) then
call error_set(FEM_ERR_INPUT, 'contact target is not ALL, a node, or a node set', ierr)
return
end if
normal_norm = sqrt(sum(contact_normal(:,item) * contact_normal(:,item)))
if (normal_norm .le. tiny(ONE)) then
call error_set(FEM_ERR_INPUT, 'contact plane normal must be non-zero', ierr)
return
end if
if (contact_penalty(item) .le. ZERO) then
call error_set(FEM_ERR_INPUT, 'contact plane penalty must be positive', ierr)
return
end if
if (contact_damping(item) .lt. ZERO) then
call error_set(FEM_ERR_INPUT, 'contact plane damping must be non-negative', ierr)
return
end if
end do
ierr = FEM_SUCCESS
end subroutine validate_contact_data
logical function contact_target_exists(target)
character(len=*), intent(in) :: target
character(len=NAME_LEN) :: local_target
local_target = adjustl(target)
contact_target_exists = .false.
if (len_trim(local_target) .eq. 0) then
contact_target_exists = .true.
else if (trim(local_target) .eq. 'ALL' .or. trim(local_target) .eq. 'all') then
contact_target_exists = .true.
else
contact_target_exists = node_or_nset_exists(local_target)
end if
end function contact_target_exists
logical function node_or_nset_exists(target)
character(len=*), intent(in) :: target
integer :: id
@@ -1410,6 +1506,11 @@ contains
if (allocated(mpc_node)) deallocate(mpc_node)
if (allocated(mpc_dof)) deallocate(mpc_dof)
if (allocated(mpc_coeff)) deallocate(mpc_coeff)
if (allocated(contact_target)) deallocate(contact_target)
if (allocated(contact_normal)) deallocate(contact_normal)
if (allocated(contact_offset)) deallocate(contact_offset)
if (allocated(contact_penalty)) deallocate(contact_penalty)
if (allocated(contact_damping)) deallocate(contact_damping)
nnode = 0
nelem = 0
@@ -1428,6 +1529,7 @@ contains
nrobin = 0
nmpc = 0
nmpc_term = 0
ncontact_plane = 0
ierr = FEM_SUCCESS
end subroutine database_finalize