diff --git a/CMakeLists.txt b/CMakeLists.txt index 0788e87..6fa950d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,7 @@ add_library(femlite_core STATIC src/solver/fem_solver_implicit.f90 src/coupling/fem_coupling.f90 src/boundary/fem_boundary.f90 + src/boundary/fem_contact.f90 src/output/fem_output_csv.f90 src/base/fem_api.f90 ) @@ -186,6 +187,13 @@ add_executable(test_solver_implicit target_link_libraries(test_solver_implicit PRIVATE femlite_core) femlite_apply_fortran_options(test_solver_implicit) +add_executable(test_contact + tests/module/test_contact.f90 +) + +target_link_libraries(test_contact PRIVATE femlite_core) +femlite_apply_fortran_options(test_contact) + add_executable(test_implicit_inp tests/module/test_implicit_inp.f90 ) @@ -280,6 +288,11 @@ set_tests_properties(solver_implicit PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" ) +add_test(NAME contact COMMAND test_contact) +set_tests_properties(contact PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" +) + add_test(NAME implicit_inp COMMAND test_implicit_inp) set_tests_properties(implicit_inp PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" diff --git a/src/base/fem_api.f90 b/src/base/fem_api.f90 index 8dfc5e3..61ec656 100644 --- a/src/base/fem_api.f90 +++ b/src/base/fem_api.f90 @@ -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) diff --git a/src/boundary/fem_contact.f90 b/src/boundary/fem_contact.f90 new file mode 100644 index 0000000..7468b82 --- /dev/null +++ b/src/boundary/fem_contact.f90 @@ -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 diff --git a/src/mesh/fem_database.f90 b/src/mesh/fem_database.f90 index d0df3d1..df71924 100644 --- a/src/mesh/fem_database.f90 +++ b/src/mesh/fem_database.f90 @@ -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 diff --git a/tests/module/test_contact.f90 b/tests/module/test_contact.f90 new file mode 100644 index 0000000..5d0f5dd --- /dev/null +++ b/tests/module/test_contact.f90 @@ -0,0 +1,149 @@ +program test_contact + use fem_precision + use fem_error + use fem_database + use fem_contact + use fem_api + implicit none + + real(rk), parameter :: TOL = 1.0e-10_rk + + call check_plane_force() + call check_target_node_only() + call check_explicit_contact_step() + +contains + + subroutine check_plane_force() + integer :: ierr + integer :: dof1 + integer :: dof2 + + call setup_basic_nodes(2, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(1) + call database_allocate_contact(1, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(2) + + contact_target(1) = 'ALL' + contact_normal(:,1) = (/ ZERO, ZERO, ONE /) + contact_offset(1) = ZERO + contact_penalty(1) = 100.0_rk + contact_damping(1) = 10.0_rk + + dof1 = dof_id(3,1) + dof2 = dof_id(3,2) + u(dof1) = -0.02_rk + v(dof1) = -0.10_rk + u(dof2) = 0.02_rk + + call contact_apply_plane_forces(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(3) + call check_close(fcontact(dof1), 3.0_rk, 4) + call check_close(fcontact(dof2), ZERO, 5) + + call database_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(6) + end subroutine check_plane_force + + subroutine check_target_node_only() + integer :: ierr + integer :: dof1 + integer :: dof2 + + call setup_basic_nodes(2, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(10) + call database_allocate_contact(1, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(11) + + contact_target(1) = '2' + contact_normal(:,1) = (/ ZERO, ZERO, 2.0_rk /) + contact_offset(1) = ZERO + contact_penalty(1) = 100.0_rk + contact_damping(1) = ZERO + + dof1 = dof_id(3,1) + dof2 = dof_id(3,2) + u(dof1) = -0.02_rk + u(dof2) = -0.02_rk + + call contact_apply_plane_forces(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(12) + call check_close(fcontact(dof1), ZERO, 13) + call check_close(fcontact(dof2), 2.0_rk, 14) + + call database_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(15) + end subroutine check_target_node_only + + subroutine check_explicit_contact_step() + integer :: ierr + integer :: dof + + call fem_initialize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(20) + + call setup_basic_nodes(1, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(21) + mass_diag = ONE + call database_allocate_contact(1, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(22) + + contact_target(1) = 'ALL' + contact_normal(:,1) = (/ ZERO, ZERO, ONE /) + contact_offset(1) = ZERO + contact_penalty(1) = 10.0_rk + contact_damping(1) = ZERO + + dof = dof_id(3,1) + u(dof) = -0.10_rk + + call fem_advance_explicit(0.10_rk, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(23) + call check_close(a(dof), ONE, 24) + call check_close(v(dof), 0.10_rk, 25) + call check_close(u(dof), -0.095_rk, 26) + + call fem_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(27) + end subroutine check_explicit_contact_step + + subroutine setup_basic_nodes(num_nodes, ierr) + integer, intent(in) :: num_nodes + integer, intent(out) :: ierr + integer :: node + integer :: idir + integer :: counter + + call database_allocate_basic(num_nodes, 0, 3 * num_nodes, ierr) + if (ierr .ne. FEM_SUCCESS) return + + counter = 0 + do node = 1, num_nodes + node_id_ext(node) = node + coord0(:,node) = ZERO + coord(:,node) = ZERO + do idir = 1, NDIM + counter = counter + 1 + dof_id(idir,node) = counter + end do + end do + + ierr = FEM_SUCCESS + end subroutine setup_basic_nodes + + 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_contact failed:', code + stop 1 + end subroutine fail + +end program test_contact