From 81ba21af55912439eff7ab1e085f9a15d5ceb32d Mon Sep 17 00:00:00 2001 From: shenw Date: Mon, 22 Jun 2026 23:20:01 +0800 Subject: [PATCH] Add basic Robin boundary support --- src/base/fem_api.f90 | 3 + src/boundary/fem_boundary.f90 | 89 ++++++++++++++++++++++++ src/mesh/fem_database.f90 | 98 +++++++++++++++++++++++++++ src/solver/fem_solver_implicit.f90 | 93 +++++++++++++++++++++++++ tests/module/test_solver_implicit.f90 | 63 +++++++++++++++++ 5 files changed, 346 insertions(+) diff --git a/src/base/fem_api.f90 b/src/base/fem_api.f90 index e8e6f8c..9825f30 100644 --- a/src/base/fem_api.f90 +++ b/src/base/fem_api.f90 @@ -758,6 +758,9 @@ contains call boundary_apply_prescribed_loads(current_time, ierr) if (ierr .ne. FEM_SUCCESS) return + call boundary_apply_robin_forces(current_time, 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_boundary.f90 b/src/boundary/fem_boundary.f90 index 96f1dbb..f984dee 100644 --- a/src/boundary/fem_boundary.f90 +++ b/src/boundary/fem_boundary.f90 @@ -37,6 +37,24 @@ contains ierr = FEM_SUCCESS end subroutine boundary_apply_prescribed_loads + subroutine boundary_apply_robin_forces(time, ierr) + real(rk), intent(in) :: time + integer, intent(out) :: ierr + integer :: item + + if (.not. allocated(fext)) then + call error_set(FEM_ERR_INTERNAL, 'external force vector is not allocated', ierr) + return + end if + + do item = 1, nrobin + call apply_robin_item(item, time, ierr) + if (ierr .ne. FEM_SUCCESS) return + end do + + ierr = FEM_SUCCESS + end subroutine boundary_apply_robin_forces + subroutine boundary_apply_dirichlet(time, ierr) real(rk), intent(in) :: time integer, intent(out) :: ierr @@ -77,6 +95,49 @@ contains ierr = FEM_SUCCESS end subroutine apply_boundary_item + subroutine apply_robin_item(item, time, ierr) + integer, intent(in) :: item + real(rk), intent(in) :: time + integer, intent(out) :: ierr + integer :: node + integer :: set_id + integer :: pos + integer :: external_id + integer :: ios + real(rk) :: scale + + scale = database_amplitude_value(robin_amp(item), time) + + read(robin_target(item),*,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, 'Robin references unknown node', ierr) + return + end if + call add_robin_force_node(node, item, scale, ierr) + return + end if + + set_id = database_find_nset(robin_target(item)) + if (set_id .eq. 0) then + call error_set(FEM_ERR_INPUT, 'Robin 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, 'Robin node set contains unknown node', ierr) + return + end if + call add_robin_force_node(node, item, scale, ierr) + if (ierr .ne. FEM_SUCCESS) return + end do + + ierr = FEM_SUCCESS + end subroutine apply_robin_item + subroutine apply_dload_item(item, ierr) integer, intent(in) :: item integer, intent(out) :: ierr @@ -223,6 +284,34 @@ contains ierr = FEM_SUCCESS end subroutine add_nodal_force + subroutine add_robin_force_node(node, item, scale, ierr) + integer, intent(in) :: node + integer, intent(in) :: item + real(rk), intent(in) :: scale + integer, intent(out) :: ierr + integer :: dof_num + integer :: dof + real(rk) :: stiffness + real(rk) :: damping + real(rk) :: value + + dof_num = robin_dof(item) + if (dof_num .lt. 1 .or. dof_num .gt. NDOF_NODE) then + call error_set(FEM_ERR_INPUT, 'Robin dof is outside 1..6', ierr) + return + end if + + dof = dof_id(dof_num,node) + if (dof .gt. 0) then + stiffness = scale * robin_stiffness(item) + damping = scale * robin_damping(item) + value = -stiffness * (u(dof) - robin_reference(item)) - damping * v(dof) + fext(dof) = fext(dof) + value + end if + + ierr = FEM_SUCCESS + end subroutine add_robin_force_node + subroutine set_dirichlet_node(node, dof_num, value, ierr) integer, intent(in) :: node integer, intent(in) :: dof_num diff --git a/src/mesh/fem_database.f90 b/src/mesh/fem_database.f90 index d38db24..34c8498 100644 --- a/src/mesh/fem_database.f90 +++ b/src/mesh/fem_database.f90 @@ -18,6 +18,7 @@ module fem_database integer :: nbc = 0 integer :: ncload = 0 integer :: ndload = 0 + integer :: nrobin = 0 real(rk), allocatable :: coord0(:,:) real(rk), allocatable :: coord(:,:) @@ -115,6 +116,13 @@ module fem_database real(rk), allocatable :: dload_value(:) integer, allocatable :: dload_amp(:) + character(len=NAME_LEN), allocatable :: robin_target(:) + integer, allocatable :: robin_dof(:) + real(rk), allocatable :: robin_stiffness(:) + real(rk), allocatable :: robin_damping(:) + real(rk), allocatable :: robin_reference(:) + integer, allocatable :: robin_amp(:) + contains subroutine database_allocate_mesh(num_nodes, num_elems, ierr) @@ -427,6 +435,45 @@ contains ierr = FEM_SUCCESS end subroutine database_allocate_loads + subroutine database_allocate_robin(num_robin, ierr) + integer, intent(in) :: num_robin + integer, intent(out) :: ierr + integer :: stat + + if (num_robin .lt. 0) then + call error_set(FEM_ERR_INPUT, 'number of Robin entries must be non-negative', ierr) + return + end if + + if (allocated(robin_target)) deallocate(robin_target) + if (allocated(robin_dof)) deallocate(robin_dof) + if (allocated(robin_stiffness)) deallocate(robin_stiffness) + if (allocated(robin_damping)) deallocate(robin_damping) + if (allocated(robin_reference)) deallocate(robin_reference) + if (allocated(robin_amp)) deallocate(robin_amp) + + nrobin = num_robin + + if (num_robin .gt. 0) then + allocate(robin_target(num_robin), robin_dof(num_robin), & + robin_stiffness(num_robin), robin_damping(num_robin), & + robin_reference(num_robin), robin_amp(num_robin), stat=stat) + if (stat .ne. 0) then + nrobin = 0 + call error_set(FEM_ERR_INTERNAL, 'failed to allocate Robin arrays', ierr) + return + end if + robin_target = ' ' + robin_dof = 0 + robin_stiffness = ZERO + robin_damping = ZERO + robin_reference = ZERO + robin_amp = 0 + end if + + ierr = FEM_SUCCESS + end subroutine database_allocate_robin + subroutine database_allocate_basic(num_nodes, num_elems, num_dof, ierr) integer, intent(in) :: num_nodes integer, intent(in) :: num_elems @@ -589,6 +636,12 @@ contains if (allocated(dload_label)) dload_label = ' ' if (allocated(dload_value)) dload_value = ZERO if (allocated(dload_amp)) dload_amp = 0 + if (allocated(robin_target)) robin_target = ' ' + if (allocated(robin_dof)) robin_dof = 0 + if (allocated(robin_stiffness)) robin_stiffness = ZERO + if (allocated(robin_damping)) robin_damping = ZERO + if (allocated(robin_reference)) robin_reference = ZERO + if (allocated(robin_amp)) robin_amp = 0 ierr = FEM_SUCCESS end subroutine database_initialize @@ -811,6 +864,9 @@ contains if (ierr .ne. FEM_SUCCESS) return call validate_dload_data(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call validate_robin_data(ierr) end subroutine database_validate_model subroutine validate_materials(ierr) @@ -1066,6 +1122,41 @@ contains ierr = FEM_SUCCESS end subroutine validate_dload_data + subroutine validate_robin_data(ierr) + integer, intent(out) :: ierr + integer :: item + + do item = 1, nrobin + if (.not. node_or_nset_exists(robin_target(item))) then + call error_set(FEM_ERR_INPUT, 'Robin target is not a node or node set', ierr) + return + end if + if (robin_dof(item) .lt. 1 .or. robin_dof(item) .gt. NDOF_NODE) then + call error_set(FEM_ERR_INPUT, 'Robin dof is outside 1..6', ierr) + return + end if + if (robin_stiffness(item) .lt. ZERO) then + call error_set(FEM_ERR_INPUT, 'Robin stiffness must be non-negative', ierr) + return + end if + if (robin_damping(item) .lt. ZERO) then + call error_set(FEM_ERR_INPUT, 'Robin damping must be non-negative', ierr) + return + end if + if (abs(robin_stiffness(item)) .le. tiny(ONE) .and. & + abs(robin_damping(item)) .le. tiny(ONE)) then + call error_set(FEM_ERR_INPUT, 'Robin stiffness and damping cannot both be zero', ierr) + return + end if + if (robin_amp(item) .lt. 0 .or. robin_amp(item) .gt. n_amp) then + call error_set(FEM_ERR_INPUT, 'Robin references invalid amplitude', ierr) + return + end if + end do + + ierr = FEM_SUCCESS + end subroutine validate_robin_data + logical function node_or_nset_exists(target) character(len=*), intent(in) :: target integer :: id @@ -1180,6 +1271,12 @@ contains if (allocated(dload_label)) deallocate(dload_label) if (allocated(dload_value)) deallocate(dload_value) if (allocated(dload_amp)) deallocate(dload_amp) + if (allocated(robin_target)) deallocate(robin_target) + if (allocated(robin_dof)) deallocate(robin_dof) + if (allocated(robin_stiffness)) deallocate(robin_stiffness) + if (allocated(robin_damping)) deallocate(robin_damping) + if (allocated(robin_reference)) deallocate(robin_reference) + if (allocated(robin_amp)) deallocate(robin_amp) nnode = 0 nelem = 0 @@ -1195,6 +1292,7 @@ contains nbc = 0 ncload = 0 ndload = 0 + nrobin = 0 ierr = FEM_SUCCESS end subroutine database_finalize diff --git a/src/solver/fem_solver_implicit.f90 b/src/solver/fem_solver_implicit.f90 index 1852d98..d8aff60 100644 --- a/src/solver/fem_solver_implicit.f90 +++ b/src/solver/fem_solver_implicit.f90 @@ -59,6 +59,9 @@ contains if (allocated(fsph)) rhs = rhs + fsph if (allocated(fcontact)) rhs = rhs + fcontact + call apply_implicit_robin(kmat, rhs, time, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + call apply_implicit_dirichlet(kmat, rhs, time, ierr) if (ierr .ne. FEM_SUCCESS) go to 900 @@ -126,6 +129,96 @@ contains ierr = FEM_SUCCESS end subroutine apply_implicit_dirichlet + subroutine apply_implicit_robin(kmat, rhs, time, ierr) + type(csr_matrix), intent(inout) :: kmat + real(rk), intent(inout) :: rhs(:) + real(rk), intent(in) :: time + integer, intent(out) :: ierr + integer :: item + real(rk) :: scale + + do item = 1, nrobin + scale = database_amplitude_value(robin_amp(item), time) + call apply_robin_target(kmat, rhs, robin_target(item), item, scale, ierr) + if (ierr .ne. FEM_SUCCESS) return + end do + + ierr = FEM_SUCCESS + end subroutine apply_implicit_robin + + subroutine apply_robin_target(kmat, rhs, target, item, scale, ierr) + type(csr_matrix), intent(inout) :: kmat + real(rk), intent(inout) :: rhs(:) + character(len=*), intent(in) :: target + integer, intent(in) :: item + real(rk), intent(in) :: scale + integer, intent(out) :: ierr + integer :: node + integer :: set_id + integer :: pos + integer :: external_id + integer :: ios + + 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, 'implicit Robin references unknown node', ierr) + return + end if + call apply_robin_node(kmat, rhs, node, item, scale, ierr) + return + end if + + set_id = database_find_nset(target) + if (set_id .eq. 0) then + call error_set(FEM_ERR_INPUT, 'implicit Robin 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, 'implicit Robin node set contains unknown node', ierr) + return + end if + call apply_robin_node(kmat, rhs, node, item, scale, ierr) + if (ierr .ne. FEM_SUCCESS) return + end do + + ierr = FEM_SUCCESS + end subroutine apply_robin_target + + subroutine apply_robin_node(kmat, rhs, node, item, scale, ierr) + type(csr_matrix), intent(inout) :: kmat + real(rk), intent(inout) :: rhs(:) + integer, intent(in) :: node + integer, intent(in) :: item + real(rk), intent(in) :: scale + integer, intent(out) :: ierr + integer :: dof_num + integer :: dof + real(rk) :: stiffness + + dof_num = robin_dof(item) + if (dof_num .lt. 1 .or. dof_num .gt. NDOF_NODE) then + call error_set(FEM_ERR_INPUT, 'implicit Robin dof is outside 1..6', ierr) + return + end if + + dof = dof_id(dof_num,node) + if (dof .gt. 0) then + stiffness = scale * robin_stiffness(item) + if (abs(stiffness) .gt. tiny(ONE)) then + call csr_add_value(kmat, dof, dof, stiffness, ierr) + if (ierr .ne. FEM_SUCCESS) return + rhs(dof) = rhs(dof) + stiffness * robin_reference(item) + end if + end if + + ierr = FEM_SUCCESS + end subroutine apply_robin_node + subroutine apply_bc_target(kmat, rhs, target, dof_num, value, ierr) type(csr_matrix), intent(inout) :: kmat real(rk), intent(inout) :: rhs(:) diff --git a/tests/module/test_solver_implicit.f90 b/tests/module/test_solver_implicit.f90 index 74f25d0..78f9b46 100644 --- a/tests/module/test_solver_implicit.f90 +++ b/tests/module/test_solver_implicit.f90 @@ -2,6 +2,7 @@ program test_solver_implicit use fem_precision use fem_error use fem_database + use fem_boundary use fem_solver_implicit implicit none @@ -9,6 +10,8 @@ program test_solver_implicit call check_single_dof_pull() call check_prescribed_value() + call check_robin_explicit_force() + call check_robin_implicit_spring() contains @@ -61,6 +64,66 @@ contains if (ierr .ne. FEM_SUCCESS) call fail(26) end subroutine check_prescribed_value + subroutine check_robin_explicit_force() + integer :: ierr + integer :: dof + + call setup_base_tet(0, 0, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(30) + call database_allocate_robin(1, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(31) + + robin_target(1) = '2' + robin_dof(1) = 1 + robin_stiffness(1) = 10.0_rk + robin_damping(1) = 2.0_rk + robin_reference(1) = 0.25_rk + robin_amp(1) = 0 + + dof = dof_id(1,2) + u(dof) = 0.5_rk + v(dof) = -0.1_rk + fext = ZERO + + call boundary_apply_robin_forces(ZERO, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(32) + call check_close(fext(dof), -2.3_rk, 33) + + call database_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(34) + end subroutine check_robin_explicit_force + + subroutine check_robin_implicit_spring() + integer :: ierr + integer :: iter + real(rk) :: res_norm + integer :: dof + + call setup_pull_model(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(40) + call database_allocate_robin(1, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(41) + + robin_target(1) = '2' + robin_dof(1) = 1 + robin_stiffness(1) = 6.0_rk + robin_damping(1) = ZERO + robin_reference(1) = 0.25_rk + robin_amp(1) = 0 + + call implicit_solve_linear_static(ZERO, 50, 1.0e-12_rk, iter, res_norm, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(42) + if (res_norm .gt. TOL) call fail(43) + + dof = dof_id(1,2) + call check_close(u(dof), 13.0_rk / 24.0_rk, 44) + call check_close(u(dof_id(2,2)), ZERO, 45) + call check_close(u(dof_id(3,2)), ZERO, 46) + + call database_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(47) + end subroutine check_robin_implicit_spring + subroutine setup_pull_model(ierr) integer, intent(out) :: ierr