diff --git a/CMakeLists.txt b/CMakeLists.txt index d2aa524..cbd50dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.20) project(FemLite - VERSION 0.3.1 + VERSION 0.3.2 LANGUAGES C Fortran ) @@ -13,6 +13,7 @@ FortranCInterface_HEADER( MACRO_NAMESPACE "FC_" ) find_package(Python3 COMPONENTS Interpreter) +option(FEMLITE_ENABLE_OPENMP "Enable OpenMP acceleration for contact BVH search" ON) set(CMAKE_Fortran_STANDARD 95) set(CMAKE_Fortran_STANDARD_REQUIRED ON) @@ -78,6 +79,15 @@ target_include_directories(femlite_core PUBLIC femlite_apply_fortran_options(femlite_core) +if(FEMLITE_ENABLE_OPENMP) + find_package(OpenMP) + if(OpenMP_Fortran_FOUND) + target_link_libraries(femlite_core PUBLIC OpenMP::OpenMP_Fortran) + else() + message(WARNING "OpenMP Fortran was requested but not found; contact BVH search will run serially.") + endif() +endif() + add_library(femlite_c STATIC src/interface/fem_c_api.c ) diff --git a/docs/02_software_architecture_design.md b/docs/02_software_architecture_design.md index 6f4987f..e322f7d 100644 --- a/docs/02_software_architecture_design.md +++ b/docs/02_software_architecture_design.md @@ -392,7 +392,7 @@ coupling 层职责: - 接触状态、接触力和接触功输出 - 隐式增量残差和迭代次数输出 -第三版当前接触模块保留刚性平面接触,并新增基于边界 facet 的面-面 contact pair force/tangent 路径。0.3.1 已补充投影裁剪面积积分、多 master 面积上限修正、Coulomb 摩擦、均匀空间网格候选搜索、facet 空间 broad-phase 精筛,以及接触状态/接触功 CSV 与 API 输出。完整 mortar 形函数积分、摩擦历史和并行 BVH 仍属于后续算法增强项。 +第三版当前接触模块保留刚性平面接触,并新增基于边界 facet 的面-面 contact pair force/tangent 路径。0.3.1 已补充投影裁剪面积积分、多 master 面积上限修正、Coulomb 摩擦、均匀空间网格候选搜索、facet 空间 broad-phase 精筛,以及接触状态/接触功 CSV 与 API 输出。0.3.2 已补充投影交叠域 mortar 形函数积分、接触对级粘滑历史变量、stick/slip/累计滑移输出,以及可选 OpenMP 的 master facet BVH 宽相搜索。 输出控制: diff --git a/docs/03_detailed_design.md b/docs/03_detailed_design.md index cdca68a..857c9d0 100644 --- a/docs/03_detailed_design.md +++ b/docs/03_detailed_design.md @@ -897,7 +897,7 @@ elements_step_xxxxx.csv 至少包含: - `convergence.csv`:静力和隐式动力增量迭代历史。 - `reactions_step_xxxxx.csv`:节点反力。 - `contact_step_xxxxx.csv`:接触状态、接触力和接触功。 - 当前字段为 `kind,pair_id,active_count,max_penetration,force_norm,work_estimate,cumulative_work`,包含 summary 行和逐 contact pair 行。 + 当前字段为 `kind,pair_id,active_count,max_penetration,force_norm,work_estimate,cumulative_work,stick_count,slip_count,accumulated_slip,ftx,fty,ftz`,包含 summary 行和逐 contact pair 行。 - 静力求解结束后必须写出与显式结果一致的 VTK 场文件。 ## 19. 主程序模块 diff --git a/docs/04_mathematical_model_and_algorithms.md b/docs/04_mathematical_model_and_algorithms.md index a6e35f9..c0de706 100644 --- a/docs/04_mathematical_model_and_algorithms.md +++ b/docs/04_mathematical_model_and_algorithms.md @@ -797,10 +797,11 @@ K_eff Delta_u = R - 刚性平面接触仍采用节点到平面的 penalty/damping 法向接触。 - 面-面 contact pair 使用边界 facet 的当前构形坐标、速度、法向、面积、中心和等效半径。 - slave facet 沿 master 法向投影到 master facet 平面,在局部二维坐标中执行多边形裁剪;裁剪面积大于零且法向间隙为负时激活。多个 master facet 同时激活时按裁剪面积贡献,且总面积不超过 slave facet 面积。 -- 接触力为 `penalty * penetration * clipped_area` 加可选法向阻尼项,并可叠加基于相对切向速度的 Coulomb 摩擦力。 -- 接触切线采用法向 penalty/damping 块加切向摩擦近似块,装配 slave-slave、slave-master、master-slave 和 master-master 四类自由度耦合。 -- 当前实现包含 master facet 均匀空间网格候选搜索和 facet 中心/等效半径 broad-phase 精筛。接触状态记录激活贡献数、最大穿透量、接触力范数和 `0.5 * |F_contact| * penetration` 形式的接触功估计。 -- 当前投影裁剪属于面积型 mortar 近似,尚未实现完整 mortar 形函数积分、摩擦粘滑历史变量和并行 BVH。 +- 0.3.2 在投影交叠多边形上做三角剖分和 Dunavant 积分,计算 `int Ns dA`、`int Nm dA`、`int Ns Ns dA`、`int Ns Nm dA`、`int Nm Ns dA` 和 `int Nm Nm dA`,用于接触力分配和切线块装配。 +- 接触法向力为 `penalty * penetration * mortar_area` 加可选法向阻尼项;切向力采用接触对级历史切向力 trial、Coulomb 极限和返回映射式粘滑判定。 +- 接触切线采用法向 penalty/damping 块加切向摩擦近似块,按 mortar 形函数乘积积分装配 slave-slave、slave-master、master-slave 和 master-master 四类自由度耦合。 +- 当前实现包含 master facet 均匀空间网格候选搜索、master facet BVH 宽相搜索和 facet 中心/等效半径 broad-phase 精筛;OpenMP 可用时 BVH 搜索按 slave facet 并行遍历。接触状态记录激活贡献数、最大穿透量、接触力范数和 `0.5 * |F_contact| * penetration` 形式的接触功估计。 +- 0.3.2 输出 contact pair 的 stick/slip 计数、累计滑移和切向摩擦力历史。当前仍未实现工程级 mortar 约束求解、摩擦正则化参数体系和大规模接触收敛控制。 第一版不实现接触,只保留 fcontact 向量和接口位置。 diff --git a/docs/05_interface_control_document.md b/docs/05_interface_control_document.md index f57adc0..400c686 100644 --- a/docs/05_interface_control_document.md +++ b/docs/05_interface_control_document.md @@ -573,7 +573,7 @@ end subroutine - `contact_pair_search(pair)`:搜索距离,0 表示只按 facet 等效半径过滤。 - `contact_pair_friction(pair)`:Coulomb 摩擦系数,必须非负;0 表示无摩擦。 -当前 contact pair 支持投影裁剪面积积分、多 master facet 面积上限修正、Coulomb 摩擦、均匀空间网格候选搜索和 facet 空间 broad-phase 精筛。 +当前 contact pair 支持投影裁剪 mortar 形函数积分、多 master facet 面积上限修正、Coulomb 粘滑历史、均匀空间网格候选搜索、OpenMP BVH 宽相搜索和 facet 空间 broad-phase 精筛。 接触统计包括: @@ -583,7 +583,7 @@ end subroutine - `contact_max_penetration`:最近一次接触力评估的最大穿透量。 - `contact_force_norm`:最近一次接触力评估的接触力范数累加。 -逐 contact pair 统计保存在 `contact_pair_active/work/max_penetration/force_norm` 数组中,并写入 `contact_step_xxxxx.csv`。 +逐 contact pair 统计保存在 `contact_pair_active/work/max_penetration/force_norm`、`contact_pair_stick_count/slip_count/slip/tangent_force` 数组中,并写入 `contact_step_xxxxx.csv`。 ## 13. 输出控制接口 diff --git a/docs/06_test_and_verification_plan.md b/docs/06_test_and_verification_plan.md index d607d1c..e7b9991 100644 --- a/docs/06_test_and_verification_plan.md +++ b/docs/06_test_and_verification_plan.md @@ -264,14 +264,14 @@ SPH总力映射测试 relative tolerance 1.0e-10 to 1.0e-8 - 面压力等效节点力。 - amplitude 常值和表格插值。 - 刚性平面 contact penalty/damping 力和切线。 -- 面-面 contact pair 法向力、阻尼项、Coulomb 摩擦、多 master 分摊、投影裁剪面积、空间网格候选、空间 broad-phase、CSR 图案和切线块。 +- 面-面 contact pair 法向力、阻尼项、Coulomb 粘滑历史、多 master 分摊、投影裁剪 mortar 权重、空间网格候选、BVH 宽相搜索、空间 broad-phase、CSR 图案和切线块。 输出测试: - `history.csv` 字段存在。 - `nodes_step_xxxxx.csv` 字段存在。 - `elements_step_xxxxx.csv` 字段存在。 -- `contact_step_xxxxx.csv` 字段存在,并包含 summary 与逐 contact pair 行。 +- `contact_step_xxxxx.csv` 字段存在,并包含 summary 与逐 contact pair 行、stick/slip 计数、累计滑移和切向力历史。 - `fem_get_contact_summary` 返回激活数、单步/累计接触功、最大穿透量和接触力范数。 - 删除单元状态可输出。 diff --git a/docs/v3_development_plan.md b/docs/v3_development_plan.md index a1e6745..6519085 100644 --- a/docs/v3_development_plan.md +++ b/docs/v3_development_plan.md @@ -273,7 +273,7 @@ K_eff = K_t + a0 M + a1 C 1. `*SURFACE` 输入子集。 2. `*CONTACT PAIR` 输入子集。 3. 接触面数据结构。 -4. 接触搜索,先采用均匀空间网格,后续可升级 BVH 或并行搜索。 +4. 接触搜索,采用均匀空间网格和可选 OpenMP BVH 宽相搜索。 5. 节点-面接触。 6. 面-面接触框架。 7. penalty 法默认接触力。 @@ -286,12 +286,13 @@ K_eff = K_t + a0 M + a1 C - 保留第二版刚性平面 penalty 接触路径。 - 新增 `contact_pair_slave/master/penalty/damping/search/friction` 集中式数据库数组,可通过 elset、element id 或 `ALL` 指定接触对目标。 - 面-面接触采用边界 facet 的当前构形、法向间隙和 penalty/damping 法向力映射;0.3.1 已从质心权重推进到 master 平面局部二维投影裁剪面积积分,并对重复 master 面做从面面积上限修正。 -- 0.3.1 支持基于相对切向速度的 Coulomb 摩擦力和切向近似切线。 -- 0.3.1 增加基于 master facet 桶的均匀空间网格候选搜索,并保留 facet 中心/等效半径 broad-phase 精筛。 -- 0.3.1 新增全局和逐 contact pair 的激活数、最大穿透量、接触力范数和接触功估计统计,并输出到 `history.csv` 与 `contact_step_xxxxx.csv`。 +- 0.3.2 已补充投影交叠域上的 mortar 形函数积分,面力和接触切线按 `Ns`、`Nm` 及其乘积积分分配到 slave/master 节点。 +- 0.3.2 支持基于接触对历史切向力的 Coulomb 粘滑判定,记录 stick/slip 计数、累计滑移和切向摩擦力历史。 +- 0.3.2 增加 master facet BVH 宽相搜索;启用 OpenMP 时按 slave facet 并行遍历 BVH,装配全局接触力/切线和摩擦历史时使用受保护临界区。 +- 0.3.1 新增全局和逐 contact pair 的激活数、最大穿透量、接触力范数和接触功估计统计;0.3.2 扩展 `contact_step_xxxxx.csv` 输出 stick/slip、累计滑移和切向力。 - CSR 稀疏图案会为 contact pair 预留 slave-slave、slave-master、master-slave 和 master-master 切线块。 -- 已补充 contact pair 法向力、摩擦力、多 master 分摊、投影裁剪面积、空间网格分支、接触统计和切线装配测试。 -- 当前仍未实现完整 mortar 形函数积分、摩擦粘滑历史变量、并行 BVH 和工程级接触收敛控制。 +- 已补充 contact pair 法向力、摩擦力、多 master 分摊、投影裁剪面积、mortar 权重、空间网格/BVH 分支、粘滑历史、接触统计和切线装配测试。 +- 当前仍未实现工程级 mortar 约束求解、摩擦正则化参数体系和大规模接触收敛控制。 验收条件: @@ -332,6 +333,10 @@ K_eff = K_t + a0 M + a1 C - force_norm - work_estimate - cumulative_work + - stick_count + - slip_count + - accumulated_slip + - ftx, fty, ftz 4. 静力 VTK 输出: - displacement - rotation @@ -446,7 +451,7 @@ P2 测试: - 先实体,再壳;先 S4/S3,再 S6/S8。 - Scordelis-Lo 作为壳静力主基准,分网格逐步验证。 - 非线性静力先完成实体 J2 算法切线,再推进壳膜内塑性切线,0.3.1 补齐壳弯曲/剪切 section tangent 映射。 -- 接触先保留刚性平面和面-面框架,0.3.1 补充投影裁剪面积、多 master 面积上限、Coulomb 摩擦、均匀空间网格和接触输出,再继续推进节点-面、完整 mortar 形函数积分、摩擦历史和并行 BVH。 +- 接触先保留刚性平面和面-面框架,0.3.1 补充投影裁剪面积、多 master 面积上限、Coulomb 摩擦、均匀空间网格和接触输出,0.3.2 补充 mortar 形函数积分、粘滑历史和 OpenMP BVH 搜索。 - 输出字段先冻结格式,再增加内容。 - 每个阶段均保持 CTest 可通过。 @@ -455,7 +460,7 @@ P2 测试: 第三版不强制完成: - 高性能外部稀疏求解器绑定。 -- 并行接触搜索。 +- 大规模并行接触域分解和负载均衡。 - 完整摩擦接触模型。 - 完整热传导求解。 - 完整 Abaqus inp 全关键字兼容。 diff --git a/src/base/fem_precision.f90 b/src/base/fem_precision.f90 index 0c20c29..811c52d 100644 --- a/src/base/fem_precision.f90 +++ b/src/base/fem_precision.f90 @@ -16,7 +16,7 @@ module fem_precision integer, parameter :: FEMLITE_VERSION_MAJOR = 0 integer, parameter :: FEMLITE_VERSION_MINOR = 3 - integer, parameter :: FEMLITE_VERSION_PATCH = 1 + integer, parameter :: FEMLITE_VERSION_PATCH = 2 real(rk), parameter :: ZERO = 0.0_rk real(rk), parameter :: ONE = 1.0_rk diff --git a/src/boundary/fem_contact.f90 b/src/boundary/fem_contact.f90 index f1308c5..91ad799 100644 --- a/src/boundary/fem_contact.f90 +++ b/src/boundary/fem_contact.f90 @@ -9,6 +9,7 @@ module fem_contact private integer, parameter :: CONTACT_MAX_CLIP = 64 + integer, parameter :: CONTACT_MAX_STACK = 128 public :: contact_apply_plane_forces public :: contact_apply_plane_tangent @@ -61,6 +62,8 @@ contains if (allocated(contact_pair_work)) contact_pair_work = ZERO if (allocated(contact_pair_max_penetration)) contact_pair_max_penetration = ZERO if (allocated(contact_pair_force_norm)) contact_pair_force_norm = ZERO + if (allocated(contact_pair_stick_count)) contact_pair_stick_count = 0 + if (allocated(contact_pair_slip_count)) contact_pair_slip_count = 0 end subroutine contact_reset_status subroutine contact_record_status(pair, penetration, force_vec) @@ -367,6 +370,11 @@ contains integer, allocatable :: fconn(:,:) integer, allocatable :: grid_head(:) integer, allocatable :: grid_next(:) + integer, allocatable :: bvh_item(:) + integer, allocatable :: bvh_left(:) + integer, allocatable :: bvh_right(:) + integer, allocatable :: bvh_start(:) + integer, allocatable :: bvh_count(:) real(rk), allocatable :: x(:,:,:) real(rk), allocatable :: vnod(:,:,:) real(rk), allocatable :: normal(:,:) @@ -374,21 +382,20 @@ contains real(rk), allocatable :: velc(:,:) real(rk), allocatable :: area(:) real(rk), allocatable :: radius(:) + real(rk), allocatable :: bvh_min(:,:) + real(rk), allocatable :: bvh_max(:,:) real(rk) :: gap real(rk) :: vn - real(rk) :: force_mag - real(rk) :: normal_mag real(rk) :: total_weight real(rk) :: pair_weight real(rk) :: area_scale - real(rk) :: contact_area real(rk) :: mu - real(rk) :: vt_norm - real(rk) :: nrm(NDIM) - real(rk) :: tdir(NDIM) - real(rk) :: force_vec(NDIM) + real(rk) :: old_tangent(NDIM) + real(rk) :: new_tangent(NDIM) real(rk) :: grid_min(NDIM) real(rk) :: grid_h(NDIM) + real(rk) :: query_min(NDIM) + real(rk) :: query_max(NDIM) real(rk) :: max_master_radius real(rk) :: candidate_radius integer :: q @@ -396,6 +403,12 @@ contains integer :: grid_ny integer :: grid_nz integer :: grid_ncell + integer :: nmaster + integer :: bvh_nodes + integer :: bvh_pos + integer :: stack(CONTACT_MAX_STACK) + integer :: stack_top + integer :: bvh_node integer :: ix0 integer :: ix1 integer :: iy0 @@ -407,6 +420,7 @@ contains integer :: iz integer :: cell logical :: use_grid + logical :: use_bvh logical :: active call cpl_build_facets(ierr) @@ -437,10 +451,35 @@ contains radius(q) = facet_radius_value(q, nf, max_nodes, fnnode, x, center(:,q)) end do mu = contact_pair_friction_value(pair) - call contact_grid_dimensions(pair, nf, elem_id, center, radius, & - grid_min, grid_h, grid_nx, grid_ny, grid_nz, & - max_master_radius, use_grid) - if (use_grid) then + old_tangent = ZERO + new_tangent = ZERO + if (allocated(contact_pair_tangent_force)) then + if (pair .ge. 1 .and. pair .le. size(contact_pair_tangent_force,2)) then + old_tangent = contact_pair_tangent_force(:,pair) + contact_pair_tangent_force(:,pair) = ZERO + end if + end if + nmaster = contact_master_count(pair, nf, elem_id) + use_bvh = nmaster .gt. 16 + use_grid = .false. + if (use_bvh) then + allocate(bvh_item(nmaster), bvh_left(2*nmaster), bvh_right(2*nmaster), & + bvh_start(2*nmaster), bvh_count(2*nmaster), & + bvh_min(NDIM,2*nmaster), bvh_max(NDIM,2*nmaster), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate contact BVH', ierr) + go to 900 + end if + call contact_bvh_build(pair, nf, elem_id, center, radius, bvh_item, & + bvh_left, bvh_right, bvh_start, bvh_count, & + bvh_min, bvh_max, bvh_nodes, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + else + call contact_grid_dimensions(pair, nf, elem_id, center, radius, & + grid_min, grid_h, grid_nx, grid_ny, grid_nz, & + max_master_radius, use_grid) + end if + if (.not. use_bvh .and. use_grid) then grid_ncell = grid_nx * grid_ny * grid_nz allocate(grid_head(grid_ncell), grid_next(nf), stat=stat) if (stat .ne. 0) then @@ -451,10 +490,53 @@ contains grid_nx, grid_ny, grid_nz, grid_head, grid_next) end if + if (use_bvh) then + call contact_bvh_apply_pair_forces(pair, nf, max_nodes, fnnode, fconn, & + elem_id, x, vnod, normal, area, radius, & + center, bvh_item, bvh_left, bvh_right, & + bvh_start, bvh_count, bvh_min, bvh_max, & + old_tangent, new_tangent, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + go to 850 + end if + do sf = 1, nf if (.not. facet_target_matches(elem_id(sf), contact_pair_slave(pair))) cycle total_weight = ZERO - if (use_grid) then + if (use_bvh) then + candidate_radius = radius(sf) + contact_pair_search_value(pair) + query_min = center(:,sf) - candidate_radius + query_max = center(:,sf) + candidate_radius + stack_top = 1 + stack(1) = 1 + do while (stack_top .gt. 0) + bvh_node = stack(stack_top) + stack_top = stack_top - 1 + if (.not. contact_aabb_overlap(query_min, query_max, & + bvh_min(:,bvh_node), bvh_max(:,bvh_node))) cycle + if (bvh_left(bvh_node) .eq. 0 .and. bvh_right(bvh_node) .eq. 0) then + do bvh_pos = bvh_start(bvh_node), & + bvh_start(bvh_node) + bvh_count(bvh_node) - 1 + mf = bvh_item(bvh_pos) + call contact_pair_candidate(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, x, vnod, normal, & + center, radius, gap, vn, pair_weight, & + active, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + if (active) total_weight = total_weight + pair_weight + end do + else + if (bvh_left(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_left(bvh_node) + end if + if (bvh_right(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_right(bvh_node) + end if + end if + end do + else if (use_grid) then candidate_radius = radius(sf) + max_master_radius + & contact_pair_search_value(pair) call contact_grid_range(center(:,sf), candidate_radius, grid_min, grid_h, & @@ -494,7 +576,45 @@ contains area_scale = area(sf) / total_weight end if - if (use_grid) then + if (use_bvh) then + candidate_radius = radius(sf) + contact_pair_search_value(pair) + query_min = center(:,sf) - candidate_radius + query_max = center(:,sf) + candidate_radius + stack_top = 1 + stack(1) = 1 + do while (stack_top .gt. 0) + bvh_node = stack(stack_top) + stack_top = stack_top - 1 + if (.not. contact_aabb_overlap(query_min, query_max, & + bvh_min(:,bvh_node), bvh_max(:,bvh_node))) cycle + if (bvh_left(bvh_node) .eq. 0 .and. bvh_right(bvh_node) .eq. 0) then + do bvh_pos = bvh_start(bvh_node), & + bvh_start(bvh_node) + bvh_count(bvh_node) - 1 + mf = bvh_item(bvh_pos) + call contact_pair_candidate(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, x, vnod, normal, & + center, radius, gap, vn, pair_weight, & + active, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + if (active) then + call add_pair_force_state(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, x, vnod, normal, gap, vn, & + area_scale, old_tangent, new_tangent, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + end if + end do + else + if (bvh_left(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_left(bvh_node) + end if + if (bvh_right(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_right(bvh_node) + end if + end if + end do + else if (use_grid) then candidate_radius = radius(sf) + max_master_radius + & contact_pair_search_value(pair) call contact_grid_range(center(:,sf), candidate_radius, grid_min, grid_h, & @@ -512,29 +632,10 @@ contains active, ierr) if (ierr .ne. FEM_SUCCESS) go to 900 if (active) then - contact_area = pair_weight * area_scale - if (contact_area .gt. tiny(ONE)) then - nrm = normalized_contact_normal(normal(:,mf), ierr) - if (ierr .ne. FEM_SUCCESS) go to 900 - normal_mag = contact_pair_penalty(pair) * (-gap) * contact_area - if (vn .lt. ZERO) then - normal_mag = normal_mag + contact_pair_damping(pair) * & - (-vn) * contact_area - end if - call contact_tangent_direction(sf, mf, nf, max_nodes, fnnode, & - vnod, nrm, tdir, vt_norm) - force_vec = normal_mag * nrm - if (mu .gt. ZERO .and. vt_norm .gt. tiny(ONE) .and. & - normal_mag .gt. ZERO) then - force_mag = mu * normal_mag - force_vec = force_vec - force_mag * tdir - end if - call add_contact_pair_force_vec(max_nodes, fnnode(sf), & - fconn(:,sf), fnnode(mf), & - fconn(:,mf), force_vec, ierr) - if (ierr .ne. FEM_SUCCESS) go to 900 - call contact_record_status(pair, -gap, force_vec) - end if + call add_pair_force_state(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, x, vnod, normal, gap, vn, & + area_scale, old_tangent, new_tangent, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 end if mf = grid_next(mf) end do @@ -550,30 +651,21 @@ contains if (ierr .ne. FEM_SUCCESS) go to 900 if (.not. active) cycle - contact_area = pair_weight * area_scale - if (contact_area .le. tiny(ONE)) cycle - nrm = normalized_contact_normal(normal(:,mf), ierr) + call add_pair_force_state(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, x, vnod, normal, gap, vn, & + area_scale, old_tangent, new_tangent, ierr) if (ierr .ne. FEM_SUCCESS) go to 900 - normal_mag = contact_pair_penalty(pair) * (-gap) * contact_area - if (vn .lt. ZERO) then - normal_mag = normal_mag + contact_pair_damping(pair) * & - (-vn) * contact_area - end if - call contact_tangent_direction(sf, mf, nf, max_nodes, fnnode, & - vnod, nrm, tdir, vt_norm) - force_vec = normal_mag * nrm - if (mu .gt. ZERO .and. vt_norm .gt. tiny(ONE) .and. normal_mag .gt. ZERO) then - force_mag = mu * normal_mag - force_vec = force_vec - force_mag * tdir - end if - call add_contact_pair_force_vec(max_nodes, fnnode(sf), fconn(:,sf), & - fnnode(mf), fconn(:,mf), force_vec, ierr) - if (ierr .ne. FEM_SUCCESS) go to 900 - call contact_record_status(pair, -gap, force_vec) end do end if end do +850 continue + if (allocated(contact_pair_tangent_force)) then + if (pair .ge. 1 .and. pair .le. size(contact_pair_tangent_force,2)) then + contact_pair_tangent_force(:,pair) = new_tangent + end if + end if + ierr = FEM_SUCCESS 900 continue @@ -591,8 +683,82 @@ contains if (allocated(radius)) deallocate(radius) if (allocated(grid_head)) deallocate(grid_head) if (allocated(grid_next)) deallocate(grid_next) + if (allocated(bvh_item)) deallocate(bvh_item) + if (allocated(bvh_left)) deallocate(bvh_left) + if (allocated(bvh_right)) deallocate(bvh_right) + if (allocated(bvh_start)) deallocate(bvh_start) + if (allocated(bvh_count)) deallocate(bvh_count) + if (allocated(bvh_min)) deallocate(bvh_min) + if (allocated(bvh_max)) deallocate(bvh_max) end subroutine apply_contact_pair_force_one + subroutine add_pair_force_state(pair, sf, mf, nf, max_nodes, fnnode, fconn, & + x, vnod, normal, gap, vn, area_scale, & + old_tangent, new_tangent, ierr) + integer, intent(in) :: pair + integer, intent(in) :: sf + integer, intent(in) :: mf + integer, intent(in) :: nf + integer, intent(in) :: max_nodes + integer, intent(in) :: fnnode(nf) + integer, intent(in) :: fconn(max_nodes,nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf) + real(rk), intent(in) :: vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf) + real(rk), intent(in) :: gap + real(rk), intent(in) :: vn + real(rk), intent(in) :: area_scale + real(rk), intent(in) :: old_tangent(NDIM) + real(rk), intent(inout) :: new_tangent(NDIM) + integer, intent(out) :: ierr + real(rk) :: mortar_area + real(rk) :: contact_area + real(rk) :: normal_mag + real(rk) :: vt_norm + real(rk) :: nrm(NDIM) + real(rk) :: tdir(NDIM) + real(rk) :: force_vec(NDIM) + real(rk) :: traction(NDIM) + real(rk) :: friction_force(NDIM) + real(rk) :: slave_w(max_nodes) + real(rk) :: master_w(max_nodes) + real(rk) :: ss_w(max_nodes,max_nodes) + real(rk) :: sm_w(max_nodes,max_nodes) + real(rk) :: ms_w(max_nodes,max_nodes) + real(rk) :: mm_w(max_nodes,max_nodes) + + nrm = normalized_contact_normal(normal(:,mf), ierr) + if (ierr .ne. FEM_SUCCESS) return + call contact_mortar_integrate(sf, mf, nf, max_nodes, fnnode, x, nrm, & + mortar_area, slave_w, master_w, ss_w, & + sm_w, ms_w, mm_w, ierr) + if (ierr .ne. FEM_SUCCESS) return + contact_area = mortar_area * area_scale + if (contact_area .le. tiny(ONE)) then + ierr = FEM_SUCCESS + return + end if + + normal_mag = contact_pair_penalty(pair) * (-gap) * contact_area + if (vn .lt. ZERO) then + normal_mag = normal_mag + contact_pair_damping(pair) * (-vn) * contact_area + end if + call contact_tangent_direction(sf, mf, nf, max_nodes, fnnode, & + vnod, nrm, tdir, vt_norm) + call contact_friction_history(pair, normal_mag, contact_area, old_tangent, & + tdir, vt_norm, friction_force) + new_tangent = new_tangent + friction_force + force_vec = normal_mag * nrm + friction_force + traction = force_vec / contact_area + + call add_contact_pair_force_mortar(max_nodes, fnnode(sf), fconn(:,sf), & + fnnode(mf), fconn(:,mf), traction, & + slave_w, master_w, area_scale, ierr) + if (ierr .ne. FEM_SUCCESS) return + call contact_record_status(pair, -gap, force_vec) + ierr = FEM_SUCCESS + end subroutine add_pair_force_state + subroutine apply_contact_pair_tan_one(pair, kmat, velocity_scale, ierr) integer, intent(in) :: pair type(csr_matrix), intent(inout) :: kmat @@ -610,6 +776,11 @@ contains integer, allocatable :: fconn(:,:) integer, allocatable :: grid_head(:) integer, allocatable :: grid_next(:) + integer, allocatable :: bvh_item(:) + integer, allocatable :: bvh_left(:) + integer, allocatable :: bvh_right(:) + integer, allocatable :: bvh_start(:) + integer, allocatable :: bvh_count(:) real(rk), allocatable :: x(:,:,:) real(rk), allocatable :: vnod(:,:,:) real(rk), allocatable :: normal(:,:) @@ -617,6 +788,8 @@ contains real(rk), allocatable :: velc(:,:) real(rk), allocatable :: area(:) real(rk), allocatable :: radius(:) + real(rk), allocatable :: bvh_min(:,:) + real(rk), allocatable :: bvh_max(:,:) real(rk) :: gap real(rk) :: vn real(rk) :: total_weight @@ -625,6 +798,8 @@ contains real(rk) :: mu real(rk) :: grid_min(NDIM) real(rk) :: grid_h(NDIM) + real(rk) :: query_min(NDIM) + real(rk) :: query_max(NDIM) real(rk) :: max_master_radius real(rk) :: candidate_radius integer :: q @@ -632,6 +807,12 @@ contains integer :: grid_ny integer :: grid_nz integer :: grid_ncell + integer :: nmaster + integer :: bvh_nodes + integer :: bvh_pos + integer :: stack(CONTACT_MAX_STACK) + integer :: stack_top + integer :: bvh_node integer :: ix0 integer :: ix1 integer :: iy0 @@ -643,6 +824,7 @@ contains integer :: iz integer :: cell logical :: use_grid + logical :: use_bvh logical :: active call cpl_build_facets(ierr) @@ -673,10 +855,27 @@ contains radius(q) = facet_radius_value(q, nf, max_nodes, fnnode, x, center(:,q)) end do mu = contact_pair_friction_value(pair) - call contact_grid_dimensions(pair, nf, elem_id, center, radius, & - grid_min, grid_h, grid_nx, grid_ny, grid_nz, & - max_master_radius, use_grid) - if (use_grid) then + nmaster = contact_master_count(pair, nf, elem_id) + use_bvh = nmaster .gt. 16 + use_grid = .false. + if (use_bvh) then + allocate(bvh_item(nmaster), bvh_left(2*nmaster), bvh_right(2*nmaster), & + bvh_start(2*nmaster), bvh_count(2*nmaster), & + bvh_min(NDIM,2*nmaster), bvh_max(NDIM,2*nmaster), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate contact tangent bvh', ierr) + go to 900 + end if + call contact_bvh_build(pair, nf, elem_id, center, radius, bvh_item, & + bvh_left, bvh_right, bvh_start, bvh_count, & + bvh_min, bvh_max, bvh_nodes, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + else + call contact_grid_dimensions(pair, nf, elem_id, center, radius, & + grid_min, grid_h, grid_nx, grid_ny, grid_nz, & + max_master_radius, use_grid) + end if + if (.not. use_bvh .and. use_grid) then grid_ncell = grid_nx * grid_ny * grid_nz allocate(grid_head(grid_ncell), grid_next(nf), stat=stat) if (stat .ne. 0) then @@ -687,10 +886,54 @@ contains grid_nx, grid_ny, grid_nz, grid_head, grid_next) end if + if (use_bvh) then + call contact_bvh_apply_pair_tangent(pair, kmat, velocity_scale, mu, nf, & + max_nodes, fnnode, fconn, elem_id, & + x, vnod, normal, area, radius, center, & + bvh_item, bvh_left, bvh_right, & + bvh_start, bvh_count, bvh_min, bvh_max, & + ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + go to 850 + end if + do sf = 1, nf if (.not. facet_target_matches(elem_id(sf), contact_pair_slave(pair))) cycle total_weight = ZERO - if (use_grid) then + if (use_bvh) then + candidate_radius = radius(sf) + contact_pair_search_value(pair) + query_min = center(:,sf) - candidate_radius + query_max = center(:,sf) + candidate_radius + stack_top = 1 + stack(1) = 1 + do while (stack_top .gt. 0) + bvh_node = stack(stack_top) + stack_top = stack_top - 1 + if (.not. contact_aabb_overlap(query_min, query_max, & + bvh_min(:,bvh_node), bvh_max(:,bvh_node))) cycle + if (bvh_left(bvh_node) .eq. 0 .and. bvh_right(bvh_node) .eq. 0) then + do bvh_pos = bvh_start(bvh_node), & + bvh_start(bvh_node) + bvh_count(bvh_node) - 1 + mf = bvh_item(bvh_pos) + call contact_pair_candidate(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, x, vnod, normal, & + center, radius, gap, vn, pair_weight, & + active, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + if (active) total_weight = total_weight + pair_weight + end do + else + if (bvh_left(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_left(bvh_node) + end if + if (bvh_right(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_right(bvh_node) + end if + end if + end do + else if (use_grid) then candidate_radius = radius(sf) + max_master_radius + & contact_pair_search_value(pair) call contact_grid_range(center(:,sf), candidate_radius, grid_min, grid_h, & @@ -730,7 +973,45 @@ contains area_scale = area(sf) / total_weight end if - if (use_grid) then + if (use_bvh) then + candidate_radius = radius(sf) + contact_pair_search_value(pair) + query_min = center(:,sf) - candidate_radius + query_max = center(:,sf) + candidate_radius + stack_top = 1 + stack(1) = 1 + do while (stack_top .gt. 0) + bvh_node = stack(stack_top) + stack_top = stack_top - 1 + if (.not. contact_aabb_overlap(query_min, query_max, & + bvh_min(:,bvh_node), bvh_max(:,bvh_node))) cycle + if (bvh_left(bvh_node) .eq. 0 .and. bvh_right(bvh_node) .eq. 0) then + do bvh_pos = bvh_start(bvh_node), & + bvh_start(bvh_node) + bvh_count(bvh_node) - 1 + mf = bvh_item(bvh_pos) + call contact_pair_candidate(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, x, vnod, normal, & + center, radius, gap, vn, pair_weight, & + active, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + if (active) then + call contact_add_pair_tangent(kmat, pair, sf, mf, nf, max_nodes, & + fnnode, fconn, x, normal, gap, vn, & + area_scale, velocity_scale, mu, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + end if + end do + else + if (bvh_left(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_left(bvh_node) + end if + if (bvh_right(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_right(bvh_node) + end if + end if + end do + else if (use_grid) then candidate_radius = radius(sf) + max_master_radius + & contact_pair_search_value(pair) call contact_grid_range(center(:,sf), candidate_radius, grid_min, grid_h, & @@ -749,9 +1030,8 @@ contains if (ierr .ne. FEM_SUCCESS) go to 900 if (active) then call contact_add_pair_tangent(kmat, pair, sf, mf, nf, max_nodes, & - fnnode, fconn, normal, gap, vn, & - pair_weight, area_scale, velocity_scale, & - mu, ierr) + fnnode, fconn, x, normal, gap, vn, & + area_scale, velocity_scale, mu, ierr) if (ierr .ne. FEM_SUCCESS) go to 900 end if mf = grid_next(mf) @@ -768,14 +1048,14 @@ contains if (ierr .ne. FEM_SUCCESS) go to 900 if (.not. active) cycle call contact_add_pair_tangent(kmat, pair, sf, mf, nf, max_nodes, & - fnnode, fconn, normal, gap, vn, & - pair_weight, area_scale, velocity_scale, & - mu, ierr) + fnnode, fconn, x, normal, gap, vn, & + area_scale, velocity_scale, mu, ierr) if (ierr .ne. FEM_SUCCESS) go to 900 end do end if end do +850 continue ierr = FEM_SUCCESS 900 continue @@ -793,12 +1073,474 @@ contains if (allocated(radius)) deallocate(radius) if (allocated(grid_head)) deallocate(grid_head) if (allocated(grid_next)) deallocate(grid_next) + if (allocated(bvh_item)) deallocate(bvh_item) + if (allocated(bvh_left)) deallocate(bvh_left) + if (allocated(bvh_right)) deallocate(bvh_right) + if (allocated(bvh_start)) deallocate(bvh_start) + if (allocated(bvh_count)) deallocate(bvh_count) + if (allocated(bvh_min)) deallocate(bvh_min) + if (allocated(bvh_max)) deallocate(bvh_max) end subroutine apply_contact_pair_tan_one + subroutine contact_bvh_apply_pair_forces(pair, nf, max_nodes, fnnode, fconn, & + elem_id, x, vnod, normal, area, radius, & + center, bvh_item, bvh_left, bvh_right, & + bvh_start, bvh_count, bvh_min, bvh_max, & + old_tangent, new_tangent, ierr) + integer, intent(in) :: pair + integer, intent(in) :: nf + integer, intent(in) :: max_nodes + integer, intent(in) :: fnnode(nf) + integer, intent(in) :: fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf) + real(rk), intent(in) :: vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf) + real(rk), intent(in) :: area(nf) + real(rk), intent(in) :: radius(nf) + real(rk), intent(in) :: center(NDIM,nf) + integer, intent(in) :: bvh_item(:) + integer, intent(in) :: bvh_left(:) + integer, intent(in) :: bvh_right(:) + integer, intent(in) :: bvh_start(:) + integer, intent(in) :: bvh_count(:) + real(rk), intent(in) :: bvh_min(:,:) + real(rk), intent(in) :: bvh_max(:,:) + real(rk), intent(in) :: old_tangent(NDIM) + real(rk), intent(inout) :: new_tangent(NDIM) + integer, intent(out) :: ierr + real(rk), allocatable :: total_weight(:) + real(rk), allocatable :: area_scale(:) + real(rk) :: local_weight + integer :: sf + integer :: stat + integer :: local_ierr + integer :: parallel_ierr + + allocate(total_weight(nf), area_scale(nf), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate contact bvh weights', ierr) + return + end if + total_weight = ZERO + area_scale = ONE + parallel_ierr = FEM_SUCCESS + +!$omp parallel do schedule(dynamic) private(sf,local_weight,local_ierr) + do sf = 1, nf + if (parallel_ierr .eq. FEM_SUCCESS) then + if (facet_target_matches(elem_id(sf), contact_pair_slave(pair))) then + call contact_bvh_slave_weight(pair, sf, nf, max_nodes, fnnode, fconn, & + elem_id, x, vnod, normal, center, radius, & + bvh_item, bvh_left, bvh_right, bvh_start, & + bvh_count, bvh_min, bvh_max, local_weight, & + local_ierr) + if (local_ierr .ne. FEM_SUCCESS) then +!$omp critical(femlite_contact_error) + if (parallel_ierr .eq. FEM_SUCCESS) parallel_ierr = local_ierr +!$omp end critical(femlite_contact_error) + else + total_weight(sf) = local_weight + if (area(sf) .gt. tiny(ONE) .and. local_weight .gt. area(sf)) then + area_scale(sf) = area(sf) / local_weight + end if + end if + end if + end if + end do +!$omp end parallel do + if (parallel_ierr .ne. FEM_SUCCESS) then + ierr = parallel_ierr + deallocate(total_weight) + deallocate(area_scale) + return + end if + +!$omp parallel do schedule(dynamic) private(sf) + do sf = 1, nf + if (parallel_ierr .eq. FEM_SUCCESS) then + if (facet_target_matches(elem_id(sf), contact_pair_slave(pair)) .and. & + total_weight(sf) .gt. tiny(ONE)) then + call contact_bvh_slave_force(pair, sf, nf, max_nodes, fnnode, fconn, & + elem_id, x, vnod, normal, center, radius, & + bvh_item, bvh_left, bvh_right, bvh_start, & + bvh_count, bvh_min, bvh_max, area_scale(sf), & + old_tangent, new_tangent, parallel_ierr) + end if + end if + end do +!$omp end parallel do + + ierr = parallel_ierr + deallocate(total_weight) + deallocate(area_scale) + end subroutine contact_bvh_apply_pair_forces + + subroutine contact_bvh_apply_pair_tangent(pair, kmat, velocity_scale, mu, nf, & + max_nodes, fnnode, fconn, elem_id, & + x, vnod, normal, area, radius, center, & + bvh_item, bvh_left, bvh_right, & + bvh_start, bvh_count, bvh_min, bvh_max, & + ierr) + integer, intent(in) :: pair + type(csr_matrix), intent(inout) :: kmat + real(rk), intent(in) :: velocity_scale + real(rk), intent(in) :: mu + integer, intent(in) :: nf + integer, intent(in) :: max_nodes + integer, intent(in) :: fnnode(nf) + integer, intent(in) :: fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf) + real(rk), intent(in) :: vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf) + real(rk), intent(in) :: area(nf) + real(rk), intent(in) :: radius(nf) + real(rk), intent(in) :: center(NDIM,nf) + integer, intent(in) :: bvh_item(:) + integer, intent(in) :: bvh_left(:) + integer, intent(in) :: bvh_right(:) + integer, intent(in) :: bvh_start(:) + integer, intent(in) :: bvh_count(:) + real(rk), intent(in) :: bvh_min(:,:) + real(rk), intent(in) :: bvh_max(:,:) + integer, intent(out) :: ierr + real(rk), allocatable :: total_weight(:) + real(rk), allocatable :: area_scale(:) + real(rk) :: local_weight + integer :: sf + integer :: stat + integer :: local_ierr + integer :: parallel_ierr + + allocate(total_weight(nf), area_scale(nf), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate contact bvh tangent weights', ierr) + return + end if + total_weight = ZERO + area_scale = ONE + parallel_ierr = FEM_SUCCESS + +!$omp parallel do schedule(dynamic) private(sf,local_weight,local_ierr) + do sf = 1, nf + if (parallel_ierr .eq. FEM_SUCCESS) then + if (facet_target_matches(elem_id(sf), contact_pair_slave(pair))) then + call contact_bvh_slave_weight(pair, sf, nf, max_nodes, fnnode, fconn, & + elem_id, x, vnod, normal, center, radius, & + bvh_item, bvh_left, bvh_right, bvh_start, & + bvh_count, bvh_min, bvh_max, local_weight, & + local_ierr) + if (local_ierr .ne. FEM_SUCCESS) then +!$omp critical(femlite_contact_error) + if (parallel_ierr .eq. FEM_SUCCESS) parallel_ierr = local_ierr +!$omp end critical(femlite_contact_error) + else + total_weight(sf) = local_weight + if (area(sf) .gt. tiny(ONE) .and. local_weight .gt. area(sf)) then + area_scale(sf) = area(sf) / local_weight + end if + end if + end if + end if + end do +!$omp end parallel do + if (parallel_ierr .ne. FEM_SUCCESS) then + ierr = parallel_ierr + deallocate(total_weight) + deallocate(area_scale) + return + end if + +!$omp parallel do schedule(dynamic) private(sf) + do sf = 1, nf + if (parallel_ierr .eq. FEM_SUCCESS) then + if (facet_target_matches(elem_id(sf), contact_pair_slave(pair)) .and. & + total_weight(sf) .gt. tiny(ONE)) then + call contact_bvh_slave_tangent(pair, kmat, velocity_scale, mu, sf, nf, & + max_nodes, fnnode, fconn, elem_id, x, & + vnod, normal, center, radius, bvh_item, & + bvh_left, bvh_right, bvh_start, bvh_count, & + bvh_min, bvh_max, area_scale(sf), & + parallel_ierr) + end if + end if + end do +!$omp end parallel do + + ierr = parallel_ierr + deallocate(total_weight) + deallocate(area_scale) + end subroutine contact_bvh_apply_pair_tangent + + subroutine contact_bvh_slave_weight(pair, sf, nf, max_nodes, fnnode, fconn, & + elem_id, x, vnod, normal, center, radius, & + bvh_item, bvh_left, bvh_right, bvh_start, & + bvh_count, bvh_min, bvh_max, total_weight, ierr) + integer, intent(in) :: pair + integer, intent(in) :: sf + integer, intent(in) :: nf + integer, intent(in) :: max_nodes + integer, intent(in) :: fnnode(nf) + integer, intent(in) :: fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf) + real(rk), intent(in) :: vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf) + real(rk), intent(in) :: center(NDIM,nf) + real(rk), intent(in) :: radius(nf) + integer, intent(in) :: bvh_item(:) + integer, intent(in) :: bvh_left(:) + integer, intent(in) :: bvh_right(:) + integer, intent(in) :: bvh_start(:) + integer, intent(in) :: bvh_count(:) + real(rk), intent(in) :: bvh_min(:,:) + real(rk), intent(in) :: bvh_max(:,:) + real(rk), intent(out) :: total_weight + integer, intent(out) :: ierr + real(rk) :: query_min(NDIM) + real(rk) :: query_max(NDIM) + real(rk) :: candidate_radius + real(rk) :: gap + real(rk) :: vn + real(rk) :: pair_weight + integer :: stack(CONTACT_MAX_STACK) + integer :: stack_top + integer :: bvh_node + integer :: bvh_pos + integer :: mf + logical :: active + + total_weight = ZERO + candidate_radius = radius(sf) + contact_pair_search_value(pair) + query_min = center(:,sf) - candidate_radius + query_max = center(:,sf) + candidate_radius + stack_top = 1 + stack(1) = 1 + + do while (stack_top .gt. 0) + bvh_node = stack(stack_top) + stack_top = stack_top - 1 + if (.not. contact_aabb_overlap(query_min, query_max, & + bvh_min(:,bvh_node), bvh_max(:,bvh_node))) cycle + if (bvh_left(bvh_node) .eq. 0 .and. bvh_right(bvh_node) .eq. 0) then + do bvh_pos = bvh_start(bvh_node), & + bvh_start(bvh_node) + bvh_count(bvh_node) - 1 + mf = bvh_item(bvh_pos) + call contact_pair_candidate(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, x, vnod, normal, & + center, radius, gap, vn, pair_weight, & + active, ierr) + if (ierr .ne. FEM_SUCCESS) return + if (active) total_weight = total_weight + pair_weight + end do + else + if (bvh_left(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_left(bvh_node) + end if + if (bvh_right(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_right(bvh_node) + end if + end if + end do + + ierr = FEM_SUCCESS + end subroutine contact_bvh_slave_weight + + subroutine contact_bvh_slave_force(pair, sf, nf, max_nodes, fnnode, fconn, & + elem_id, x, vnod, normal, center, radius, & + bvh_item, bvh_left, bvh_right, bvh_start, & + bvh_count, bvh_min, bvh_max, area_scale, & + old_tangent, new_tangent, parallel_ierr) + integer, intent(in) :: pair + integer, intent(in) :: sf + integer, intent(in) :: nf + integer, intent(in) :: max_nodes + integer, intent(in) :: fnnode(nf) + integer, intent(in) :: fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf) + real(rk), intent(in) :: vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf) + real(rk), intent(in) :: center(NDIM,nf) + real(rk), intent(in) :: radius(nf) + integer, intent(in) :: bvh_item(:) + integer, intent(in) :: bvh_left(:) + integer, intent(in) :: bvh_right(:) + integer, intent(in) :: bvh_start(:) + integer, intent(in) :: bvh_count(:) + real(rk), intent(in) :: bvh_min(:,:) + real(rk), intent(in) :: bvh_max(:,:) + real(rk), intent(in) :: area_scale + real(rk), intent(in) :: old_tangent(NDIM) + real(rk), intent(inout) :: new_tangent(NDIM) + integer, intent(inout) :: parallel_ierr + real(rk) :: query_min(NDIM) + real(rk) :: query_max(NDIM) + real(rk) :: candidate_radius + real(rk) :: gap + real(rk) :: vn + real(rk) :: pair_weight + integer :: stack(CONTACT_MAX_STACK) + integer :: stack_top + integer :: bvh_node + integer :: bvh_pos + integer :: mf + integer :: local_ierr + logical :: active + + candidate_radius = radius(sf) + contact_pair_search_value(pair) + query_min = center(:,sf) - candidate_radius + query_max = center(:,sf) + candidate_radius + stack_top = 1 + stack(1) = 1 + + do while (stack_top .gt. 0) + if (parallel_ierr .ne. FEM_SUCCESS) return + bvh_node = stack(stack_top) + stack_top = stack_top - 1 + if (.not. contact_aabb_overlap(query_min, query_max, & + bvh_min(:,bvh_node), bvh_max(:,bvh_node))) cycle + if (bvh_left(bvh_node) .eq. 0 .and. bvh_right(bvh_node) .eq. 0) then + do bvh_pos = bvh_start(bvh_node), & + bvh_start(bvh_node) + bvh_count(bvh_node) - 1 + if (parallel_ierr .ne. FEM_SUCCESS) return + mf = bvh_item(bvh_pos) + call contact_pair_candidate(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, x, vnod, normal, & + center, radius, gap, vn, pair_weight, & + active, local_ierr) + if (local_ierr .ne. FEM_SUCCESS) then +!$omp critical(femlite_contact_error) + if (parallel_ierr .eq. FEM_SUCCESS) parallel_ierr = local_ierr +!$omp end critical(femlite_contact_error) + return + end if + if (active) then +!$omp critical(femlite_contact_force) + if (parallel_ierr .eq. FEM_SUCCESS) then + call add_pair_force_state(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, x, vnod, normal, gap, vn, & + area_scale, old_tangent, new_tangent, & + local_ierr) + if (local_ierr .ne. FEM_SUCCESS) parallel_ierr = local_ierr + end if +!$omp end critical(femlite_contact_force) + end if + end do + else + if (bvh_left(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_left(bvh_node) + end if + if (bvh_right(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_right(bvh_node) + end if + end if + end do + end subroutine contact_bvh_slave_force + + subroutine contact_bvh_slave_tangent(pair, kmat, velocity_scale, mu, sf, nf, & + max_nodes, fnnode, fconn, elem_id, x, & + vnod, normal, center, radius, bvh_item, & + bvh_left, bvh_right, bvh_start, bvh_count, & + bvh_min, bvh_max, area_scale, parallel_ierr) + integer, intent(in) :: pair + type(csr_matrix), intent(inout) :: kmat + real(rk), intent(in) :: velocity_scale + real(rk), intent(in) :: mu + integer, intent(in) :: sf + integer, intent(in) :: nf + integer, intent(in) :: max_nodes + integer, intent(in) :: fnnode(nf) + integer, intent(in) :: fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf) + real(rk), intent(in) :: vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf) + real(rk), intent(in) :: center(NDIM,nf) + real(rk), intent(in) :: radius(nf) + integer, intent(in) :: bvh_item(:) + integer, intent(in) :: bvh_left(:) + integer, intent(in) :: bvh_right(:) + integer, intent(in) :: bvh_start(:) + integer, intent(in) :: bvh_count(:) + real(rk), intent(in) :: bvh_min(:,:) + real(rk), intent(in) :: bvh_max(:,:) + real(rk), intent(in) :: area_scale + integer, intent(inout) :: parallel_ierr + real(rk) :: query_min(NDIM) + real(rk) :: query_max(NDIM) + real(rk) :: candidate_radius + real(rk) :: gap + real(rk) :: vn + real(rk) :: pair_weight + integer :: stack(CONTACT_MAX_STACK) + integer :: stack_top + integer :: bvh_node + integer :: bvh_pos + integer :: mf + integer :: local_ierr + logical :: active + + candidate_radius = radius(sf) + contact_pair_search_value(pair) + query_min = center(:,sf) - candidate_radius + query_max = center(:,sf) + candidate_radius + stack_top = 1 + stack(1) = 1 + + do while (stack_top .gt. 0) + if (parallel_ierr .ne. FEM_SUCCESS) return + bvh_node = stack(stack_top) + stack_top = stack_top - 1 + if (.not. contact_aabb_overlap(query_min, query_max, & + bvh_min(:,bvh_node), bvh_max(:,bvh_node))) cycle + if (bvh_left(bvh_node) .eq. 0 .and. bvh_right(bvh_node) .eq. 0) then + do bvh_pos = bvh_start(bvh_node), & + bvh_start(bvh_node) + bvh_count(bvh_node) - 1 + if (parallel_ierr .ne. FEM_SUCCESS) return + mf = bvh_item(bvh_pos) + call contact_pair_candidate(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, x, vnod, normal, & + center, radius, gap, vn, pair_weight, & + active, local_ierr) + if (local_ierr .ne. FEM_SUCCESS) then +!$omp critical(femlite_contact_error) + if (parallel_ierr .eq. FEM_SUCCESS) parallel_ierr = local_ierr +!$omp end critical(femlite_contact_error) + return + end if + if (active) then +!$omp critical(femlite_contact_tangent) + if (parallel_ierr .eq. FEM_SUCCESS) then + call contact_add_pair_tangent(kmat, pair, sf, mf, nf, max_nodes, & + fnnode, fconn, x, normal, gap, vn, & + area_scale, velocity_scale, mu, & + local_ierr) + if (local_ierr .ne. FEM_SUCCESS) parallel_ierr = local_ierr + end if +!$omp end critical(femlite_contact_tangent) + end if + end do + else + if (bvh_left(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_left(bvh_node) + end if + if (bvh_right(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then + stack_top = stack_top + 1 + stack(stack_top) = bvh_right(bvh_node) + end if + end if + end do + end subroutine contact_bvh_slave_tangent + subroutine contact_add_pair_tangent(kmat, pair, sf, mf, nf, max_nodes, & - fnnode, fconn, normal, gap, vn, & - pair_weight, area_scale, velocity_scale, & - mu, ierr) + fnnode, fconn, x, normal, gap, vn, & + area_scale, velocity_scale, mu, ierr) type(csr_matrix), intent(inout) :: kmat integer, intent(in) :: pair integer, intent(in) :: sf @@ -807,18 +1549,25 @@ contains integer, intent(in) :: max_nodes integer, intent(in) :: fnnode(nf) integer, intent(in) :: fconn(max_nodes,nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf) real(rk), intent(in) :: normal(NDIM,nf) real(rk), intent(in) :: gap real(rk), intent(in) :: vn - real(rk), intent(in) :: pair_weight real(rk), intent(in) :: area_scale real(rk), intent(in) :: velocity_scale real(rk), intent(in) :: mu integer, intent(out) :: ierr + real(rk) :: mortar_area real(rk) :: contact_area real(rk) :: coeff real(rk) :: nrm(NDIM) real(rk) :: ktan(NDIM,NDIM) + real(rk) :: slave_w(max_nodes) + real(rk) :: master_w(max_nodes) + real(rk) :: ss_w(max_nodes,max_nodes) + real(rk) :: sm_w(max_nodes,max_nodes) + real(rk) :: ms_w(max_nodes,max_nodes) + real(rk) :: mm_w(max_nodes,max_nodes) integer :: idir integer :: jdir @@ -827,17 +1576,21 @@ contains return end if - contact_area = pair_weight * area_scale + nrm = normalized_contact_normal(normal(:,mf), ierr) + if (ierr .ne. FEM_SUCCESS) return + call contact_mortar_integrate(sf, mf, nf, max_nodes, fnnode, x, nrm, & + mortar_area, slave_w, master_w, ss_w, & + sm_w, ms_w, mm_w, ierr) + if (ierr .ne. FEM_SUCCESS) return + contact_area = mortar_area * area_scale if (contact_area .le. tiny(ONE)) then ierr = FEM_SUCCESS return end if - nrm = normalized_contact_normal(normal(:,mf), ierr) - if (ierr .ne. FEM_SUCCESS) return - coeff = contact_pair_penalty(pair) * contact_area + coeff = contact_pair_penalty(pair) if (vn .lt. ZERO) then - coeff = coeff + velocity_scale * contact_pair_damping(pair) * contact_area + coeff = coeff + velocity_scale * contact_pair_damping(pair) end if ktan = ZERO @@ -851,8 +1604,9 @@ contains end do end do - call add_contact_pair_tan_matrix(kmat, max_nodes, fnnode(sf), fconn(:,sf), & - fnnode(mf), fconn(:,mf), ktan, ierr) + call add_contact_pair_tan_mortar(kmat, max_nodes, fnnode(sf), fconn(:,sf), & + fnnode(mf), fconn(:,mf), ktan, ss_w, sm_w, & + ms_w, mm_w, area_scale, ierr) end subroutine contact_add_pair_tangent logical function facet_target_matches(elem_ext, target) @@ -1143,6 +1897,158 @@ contains contact_grid_cell = ix + (iy - 1) * nx + (iz - 1) * nx * ny end function contact_grid_cell + integer function contact_master_count(pair, nf, elem_id) + integer, intent(in) :: pair + integer, intent(in) :: nf + integer, intent(in) :: elem_id(nf) + integer :: mf + + contact_master_count = 0 + do mf = 1, nf + if (facet_target_matches(elem_id(mf), contact_pair_master(pair))) then + contact_master_count = contact_master_count + 1 + end if + end do + end function contact_master_count + + subroutine contact_bvh_build(pair, nf, elem_id, center, radius, bvh_item, & + bvh_left, bvh_right, bvh_start, bvh_count, & + bvh_min, bvh_max, bvh_nodes, ierr) + integer, intent(in) :: pair + integer, intent(in) :: nf + integer, intent(in) :: elem_id(nf) + real(rk), intent(in) :: center(NDIM,nf) + real(rk), intent(in) :: radius(nf) + integer, intent(inout) :: bvh_item(:) + integer, intent(inout) :: bvh_left(:) + integer, intent(inout) :: bvh_right(:) + integer, intent(inout) :: bvh_start(:) + integer, intent(inout) :: bvh_count(:) + real(rk), intent(inout) :: bvh_min(:,:) + real(rk), intent(inout) :: bvh_max(:,:) + integer, intent(out) :: bvh_nodes + integer, intent(out) :: ierr + integer :: mf + integer :: nmaster + integer :: root + + nmaster = 0 + do mf = 1, nf + if (.not. facet_target_matches(elem_id(mf), contact_pair_master(pair))) cycle + nmaster = nmaster + 1 + bvh_item(nmaster) = mf + end do + + bvh_left = 0 + bvh_right = 0 + bvh_start = 0 + bvh_count = 0 + bvh_min = ZERO + bvh_max = ZERO + bvh_nodes = 0 + if (nmaster .le. 0) then + ierr = FEM_SUCCESS + return + end if + + call contact_bvh_build_node(1, nmaster, center, radius, bvh_item, & + bvh_left, bvh_right, bvh_start, bvh_count, & + bvh_min, bvh_max, bvh_nodes, root) + ierr = FEM_SUCCESS + end subroutine contact_bvh_build + + recursive subroutine contact_bvh_build_node(first, last, center, radius, & + bvh_item, bvh_left, bvh_right, & + bvh_start, bvh_count, bvh_min, & + bvh_max, bvh_nodes, node_id) + integer, intent(in) :: first + integer, intent(in) :: last + real(rk), intent(in) :: center(:,:) + real(rk), intent(in) :: radius(:) + integer, intent(inout) :: bvh_item(:) + integer, intent(inout) :: bvh_left(:) + integer, intent(inout) :: bvh_right(:) + integer, intent(inout) :: bvh_start(:) + integer, intent(inout) :: bvh_count(:) + real(rk), intent(inout) :: bvh_min(:,:) + real(rk), intent(inout) :: bvh_max(:,:) + integer, intent(inout) :: bvh_nodes + integer, intent(out) :: node_id + real(rk) :: extent(NDIM) + real(rk) :: search + integer :: axis + integer :: mid + integer :: idx + integer :: mf + integer :: left_id + integer :: right_id + + bvh_nodes = bvh_nodes + 1 + node_id = bvh_nodes + bvh_start(node_id) = first + bvh_count(node_id) = last - first + 1 + bvh_min(:,node_id) = huge(ONE) + bvh_max(:,node_id) = -huge(ONE) + search = ZERO + do idx = first, last + mf = bvh_item(idx) + bvh_min(:,node_id) = min(bvh_min(:,node_id), center(:,mf) - radius(mf) - search) + bvh_max(:,node_id) = max(bvh_max(:,node_id), center(:,mf) + radius(mf) + search) + end do + + if (bvh_count(node_id) .le. 4) return + + extent = bvh_max(:,node_id) - bvh_min(:,node_id) + axis = 1 + if (extent(2) .gt. extent(axis)) axis = 2 + if (extent(3) .gt. extent(axis)) axis = 3 + call contact_bvh_sort_range(first, last, axis, center, bvh_item) + mid = (first + last) / 2 + call contact_bvh_build_node(first, mid, center, radius, bvh_item, & + bvh_left, bvh_right, bvh_start, bvh_count, & + bvh_min, bvh_max, bvh_nodes, left_id) + call contact_bvh_build_node(mid + 1, last, center, radius, bvh_item, & + bvh_left, bvh_right, bvh_start, bvh_count, & + bvh_min, bvh_max, bvh_nodes, right_id) + bvh_left(node_id) = left_id + bvh_right(node_id) = right_id + end subroutine contact_bvh_build_node + + subroutine contact_bvh_sort_range(first, last, axis, center, bvh_item) + integer, intent(in) :: first + integer, intent(in) :: last + integer, intent(in) :: axis + real(rk), intent(in) :: center(:,:) + integer, intent(inout) :: bvh_item(:) + integer :: i + integer :: j + integer :: item + + do i = first + 1, last + item = bvh_item(i) + j = i - 1 + do while (j .ge. first) + if (center(axis,bvh_item(j)) .le. center(axis,item)) exit + bvh_item(j + 1) = bvh_item(j) + j = j - 1 + end do + bvh_item(j + 1) = item + end do + end subroutine contact_bvh_sort_range + + logical function contact_aabb_overlap(qmin, qmax, bmin, bmax) + real(rk), intent(in) :: qmin(NDIM) + real(rk), intent(in) :: qmax(NDIM) + real(rk), intent(in) :: bmin(NDIM) + real(rk), intent(in) :: bmax(NDIM) + + contact_aabb_overlap = .false. + if (qmax(1) .lt. bmin(1) .or. qmin(1) .gt. bmax(1)) return + if (qmax(2) .lt. bmin(2) .or. qmin(2) .gt. bmax(2)) return + if (qmax(3) .lt. bmin(3) .or. qmin(3) .gt. bmax(3)) return + contact_aabb_overlap = .true. + end function contact_aabb_overlap + function normalized_contact_normal(raw_normal, ierr) result(nrm) real(rk), intent(in) :: raw_normal(NDIM) integer, intent(out) :: ierr @@ -1194,6 +2100,55 @@ contains end if end subroutine contact_tangent_direction + subroutine contact_friction_history(pair, normal_mag, contact_area, & + old_tangent, tdir, vt_norm, friction_force) + integer, intent(in) :: pair + real(rk), intent(in) :: normal_mag + real(rk), intent(in) :: contact_area + real(rk), intent(in) :: old_tangent(NDIM) + real(rk), intent(in) :: tdir(NDIM) + real(rk), intent(in) :: vt_norm + real(rk), intent(out) :: friction_force(NDIM) + real(rk) :: mu + real(rk) :: kt + real(rk) :: trial(NDIM) + real(rk) :: trial_norm + real(rk) :: limit_force + real(rk) :: slip_inc + + friction_force = ZERO + mu = contact_pair_friction_value(pair) + if (mu .le. ZERO .or. normal_mag .le. ZERO .or. contact_area .le. tiny(ONE)) return + + kt = max(contact_pair_penalty(pair) * contact_area, tiny(ONE)) + slip_inc = vt_norm + trial = old_tangent - kt * slip_inc * tdir + trial_norm = sqrt(sum(trial * trial)) + limit_force = mu * normal_mag + + if (trial_norm .le. limit_force + tiny(ONE)) then + friction_force = trial + if (allocated(contact_pair_stick_count)) then + if (pair .ge. 1 .and. pair .le. size(contact_pair_stick_count)) then + contact_pair_stick_count(pair) = contact_pair_stick_count(pair) + 1 + end if + end if + else + friction_force = limit_force * trial / max(trial_norm, tiny(ONE)) + if (allocated(contact_pair_slip_count)) then + if (pair .ge. 1 .and. pair .le. size(contact_pair_slip_count)) then + contact_pair_slip_count(pair) = contact_pair_slip_count(pair) + 1 + end if + end if + if (allocated(contact_pair_slip)) then + if (pair .ge. 1 .and. pair .le. size(contact_pair_slip)) then + contact_pair_slip(pair) = contact_pair_slip(pair) + & + max(ZERO, (trial_norm - limit_force) / kt) + end if + end if + end if + end subroutine contact_friction_history + subroutine pair_contact_gap(pair, sf, mf, nf, max_nodes, fnnode, x, vnod, & normal, gap, vn, pair_weight, ierr) integer, intent(in) :: pair @@ -1334,6 +2289,306 @@ contains ierr = FEM_SUCCESS end subroutine contact_projected_overlap + subroutine contact_mortar_integrate(sf, mf, nf, max_nodes, fnnode, x, nrm, & + overlap_area, slave_w, master_w, & + ss_w, sm_w, ms_w, mm_w, ierr) + integer, intent(in) :: sf + integer, intent(in) :: mf + integer, intent(in) :: nf + integer, intent(in) :: max_nodes + integer, intent(in) :: fnnode(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf) + real(rk), intent(in) :: nrm(NDIM) + real(rk), intent(out) :: overlap_area + real(rk), intent(out) :: slave_w(max_nodes) + real(rk), intent(out) :: master_w(max_nodes) + real(rk), intent(out) :: ss_w(max_nodes,max_nodes) + real(rk), intent(out) :: sm_w(max_nodes,max_nodes) + real(rk), intent(out) :: ms_w(max_nodes,max_nodes) + real(rk), intent(out) :: mm_w(max_nodes,max_nodes) + integer, intent(out) :: ierr + real(rk) :: basis1(NDIM) + real(rk) :: basis2(NDIM) + real(rk) :: origin(NDIM) + real(rk) :: point(NDIM) + real(rk) :: mpoly(2,CONTACT_MAX_CLIP) + real(rk) :: spoly(2,CONTACT_MAX_CLIP) + real(rk) :: poly(2,CONTACT_MAX_CLIP) + real(rk) :: clipped(2,CONTACT_MAX_CLIP) + real(rk) :: qp(2) + real(rk) :: qs(CONTACT_MAX_CLIP) + real(rk) :: qm(CONTACT_MAX_CLIP) + real(rk) :: tri_area + real(rk) :: tri_w + real(rk) :: signed_area + real(rk) :: bary(3,7) + real(rk) :: qweight(7) + integer :: i + integer :: j + integer :: a + integer :: b + integer :: k + integer :: npoly + integer :: nclip + + overlap_area = ZERO + slave_w = ZERO + master_w = ZERO + ss_w = ZERO + sm_w = ZERO + ms_w = ZERO + mm_w = ZERO + if (fnnode(sf) .gt. CONTACT_MAX_CLIP .or. & + fnnode(mf) .gt. CONTACT_MAX_CLIP) then + call error_set(FEM_ERR_COUPLING, 'contact mortar polygon buffer is too small', ierr) + return + end if + + origin = x(:,1,mf) + call contact_build_basis(mf, nf, max_nodes, fnnode, x, nrm, basis1, basis2, ierr) + if (ierr .ne. FEM_SUCCESS) return + + do i = 1, fnnode(mf) + point = x(:,i,mf) - origin + mpoly(1,i) = dot_product(point, basis1) + mpoly(2,i) = dot_product(point, basis2) + end do + + do i = 1, fnnode(sf) + point = x(:,i,sf) - dot_product(nrm, x(:,i,sf) - origin) * nrm - origin + spoly(1,i) = dot_product(point, basis1) + spoly(2,i) = dot_product(point, basis2) + poly(:,i) = spoly(:,i) + end do + + npoly = fnnode(sf) + signed_area = polygon_signed_area(fnnode(mf), mpoly) + if (abs(signed_area) .le. tiny(ONE)) then + ierr = FEM_SUCCESS + return + end if + + do i = 1, fnnode(mf) + j = i + 1 + if (j .gt. fnnode(mf)) j = 1 + call clip_polygon_halfplane(poly, npoly, mpoly(:,i), mpoly(:,j), & + signed_area, clipped, nclip) + if (nclip .le. 2) then + ierr = FEM_SUCCESS + return + end if + npoly = nclip + poly(:,1:npoly) = clipped(:,1:npoly) + end do + + bary(:,1) = (/ ONE / 3.0_rk, ONE / 3.0_rk, ONE / 3.0_rk /) + qweight(1) = 0.225_rk + bary(:,2) = (/ 0.470142064105115_rk, 0.470142064105115_rk, & + 0.059715871789770_rk /) + bary(:,3) = (/ 0.470142064105115_rk, 0.059715871789770_rk, & + 0.470142064105115_rk /) + bary(:,4) = (/ 0.059715871789770_rk, 0.470142064105115_rk, & + 0.470142064105115_rk /) + qweight(2:4) = 0.132394152788506_rk + bary(:,5) = (/ 0.101286507323456_rk, 0.101286507323456_rk, & + 0.797426985353087_rk /) + bary(:,6) = (/ 0.101286507323456_rk, 0.797426985353087_rk, & + 0.101286507323456_rk /) + bary(:,7) = (/ 0.797426985353087_rk, 0.101286507323456_rk, & + 0.101286507323456_rk /) + qweight(5:7) = 0.125939180544827_rk + + do i = 2, npoly - 1 + tri_area = triangle_area_2d(poly(:,1), poly(:,i), poly(:,i+1)) + if (tri_area .le. tiny(ONE)) cycle + do k = 1, 7 + tri_w = tri_area * qweight(k) + qp = bary(1,k) * poly(:,1) + bary(2,k) * poly(:,i) + & + bary(3,k) * poly(:,i+1) + call contact_face_shape(fnnode(sf), spoly, qp, qs, ierr) + if (ierr .ne. FEM_SUCCESS) return + call contact_face_shape(fnnode(mf), mpoly, qp, qm, ierr) + if (ierr .ne. FEM_SUCCESS) return + + overlap_area = overlap_area + tri_w + do a = 1, fnnode(sf) + slave_w(a) = slave_w(a) + qs(a) * tri_w + do b = 1, fnnode(sf) + ss_w(a,b) = ss_w(a,b) + qs(a) * qs(b) * tri_w + end do + do b = 1, fnnode(mf) + sm_w(a,b) = sm_w(a,b) + qs(a) * qm(b) * tri_w + end do + end do + do a = 1, fnnode(mf) + master_w(a) = master_w(a) + qm(a) * tri_w + do b = 1, fnnode(sf) + ms_w(a,b) = ms_w(a,b) + qm(a) * qs(b) * tri_w + end do + do b = 1, fnnode(mf) + mm_w(a,b) = mm_w(a,b) + qm(a) * qm(b) * tri_w + end do + end do + end do + end do + + ierr = FEM_SUCCESS + end subroutine contact_mortar_integrate + + subroutine contact_face_shape(nface, poly, point, shape, ierr) + integer, intent(in) :: nface + real(rk), intent(in) :: poly(2,CONTACT_MAX_CLIP) + real(rk), intent(in) :: point(2) + real(rk), intent(out) :: shape(CONTACT_MAX_CLIP) + integer, intent(out) :: ierr + real(rk) :: l1 + real(rk) :: l2 + real(rk) :: l3 + real(rk) :: r + real(rk) :: s + + shape = ZERO + l1 = ZERO + l2 = ZERO + l3 = ZERO + r = ZERO + s = ZERO + select case (nface) + case (3, 6) + call triangle_barycentric(poly(:,1), poly(:,2), poly(:,3), point, & + l1, l2, l3, ierr) + if (ierr .ne. FEM_SUCCESS) return + if (nface .eq. 3) then + shape(1) = l1 + shape(2) = l2 + shape(3) = l3 + else + shape(1) = l1 * (2.0_rk * l1 - ONE) + shape(2) = l2 * (2.0_rk * l2 - ONE) + shape(3) = l3 * (2.0_rk * l3 - ONE) + shape(4) = 4.0_rk * l1 * l2 + shape(5) = 4.0_rk * l2 * l3 + shape(6) = 4.0_rk * l3 * l1 + end if + case (4, 8) + call quad_inverse_bilinear(poly, point, r, s) + if (nface .eq. 4) then + shape(1) = 0.25_rk * (ONE - r) * (ONE - s) + shape(2) = 0.25_rk * (ONE + r) * (ONE - s) + shape(3) = 0.25_rk * (ONE + r) * (ONE + s) + shape(4) = 0.25_rk * (ONE - r) * (ONE + s) + else + shape(1) = -0.25_rk * (ONE - r) * (ONE - s) * (ONE + r + s) + shape(2) = -0.25_rk * (ONE + r) * (ONE - s) * (ONE - r + s) + shape(3) = -0.25_rk * (ONE + r) * (ONE + s) * (ONE - r - s) + shape(4) = -0.25_rk * (ONE - r) * (ONE + s) * (ONE + r - s) + shape(5) = HALF * (ONE - r * r) * (ONE - s) + shape(6) = HALF * (ONE + r) * (ONE - s * s) + shape(7) = HALF * (ONE - r * r) * (ONE + s) + shape(8) = HALF * (ONE - r) * (ONE - s * s) + end if + case default + call error_set(FEM_ERR_COUPLING, 'unsupported contact face node count', ierr) + return + end select + ierr = FEM_SUCCESS + end subroutine contact_face_shape + + subroutine triangle_barycentric(a, b, c, p, l1, l2, l3, ierr) + real(rk), intent(in) :: a(2) + real(rk), intent(in) :: b(2) + real(rk), intent(in) :: c(2) + real(rk), intent(in) :: p(2) + real(rk), intent(out) :: l1 + real(rk), intent(out) :: l2 + real(rk), intent(out) :: l3 + integer, intent(out) :: ierr + real(rk) :: denom + + l1 = ZERO + l2 = ZERO + l3 = ZERO + denom = (b(2) - c(2)) * (a(1) - c(1)) + & + (c(1) - b(1)) * (a(2) - c(2)) + if (abs(denom) .le. tiny(ONE)) then + call error_set(FEM_ERR_COUPLING, 'contact triangle mapping is singular', ierr) + return + end if + l1 = ((b(2) - c(2)) * (p(1) - c(1)) + & + (c(1) - b(1)) * (p(2) - c(2))) / denom + l2 = ((c(2) - a(2)) * (p(1) - c(1)) + & + (a(1) - c(1)) * (p(2) - c(2))) / denom + l3 = ONE - l1 - l2 + ierr = FEM_SUCCESS + end subroutine triangle_barycentric + + subroutine quad_inverse_bilinear(poly, point, r, s) + real(rk), intent(in) :: poly(2,CONTACT_MAX_CLIP) + real(rk), intent(in) :: point(2) + real(rk), intent(out) :: r + real(rk), intent(out) :: s + real(rk) :: n(4) + real(rk) :: dndr(4) + real(rk) :: dnds(4) + real(rk) :: xq(2) + real(rk) :: res(2) + real(rk) :: j11 + real(rk) :: j12 + real(rk) :: j21 + real(rk) :: j22 + real(rk) :: detj + real(rk) :: dr + real(rk) :: ds + integer :: iter + integer :: a + + r = ZERO + s = ZERO + do iter = 1, 8 + n(1) = 0.25_rk * (ONE - r) * (ONE - s) + n(2) = 0.25_rk * (ONE + r) * (ONE - s) + n(3) = 0.25_rk * (ONE + r) * (ONE + s) + n(4) = 0.25_rk * (ONE - r) * (ONE + s) + dndr(1) = -0.25_rk * (ONE - s) + dndr(2) = 0.25_rk * (ONE - s) + dndr(3) = 0.25_rk * (ONE + s) + dndr(4) = -0.25_rk * (ONE + s) + dnds(1) = -0.25_rk * (ONE - r) + dnds(2) = -0.25_rk * (ONE + r) + dnds(3) = 0.25_rk * (ONE + r) + dnds(4) = 0.25_rk * (ONE - r) + xq = ZERO + j11 = ZERO + j12 = ZERO + j21 = ZERO + j22 = ZERO + do a = 1, 4 + xq = xq + n(a) * poly(:,a) + j11 = j11 + dndr(a) * poly(1,a) + j21 = j21 + dndr(a) * poly(2,a) + j12 = j12 + dnds(a) * poly(1,a) + j22 = j22 + dnds(a) * poly(2,a) + end do + res = point - xq + detj = j11 * j22 - j12 * j21 + if (abs(detj) .le. tiny(ONE)) exit + dr = ( res(1) * j22 - j12 * res(2)) / detj + ds = ( j11 * res(2) - res(1) * j21) / detj + r = max(-ONE, min(ONE, r + dr)) + s = max(-ONE, min(ONE, s + ds)) + if (dr * dr + ds * ds .le. 1.0e-24_rk) exit + end do + end subroutine quad_inverse_bilinear + + real(rk) function triangle_area_2d(a, b, c) + real(rk), intent(in) :: a(2) + real(rk), intent(in) :: b(2) + real(rk), intent(in) :: c(2) + + triangle_area_2d = HALF * abs((b(1) - a(1)) * (c(2) - a(2)) - & + (b(2) - a(2)) * (c(1) - a(1))) + end function triangle_area_2d + subroutine contact_build_basis(mf, nf, max_nodes, fnnode, x, nrm, basis1, basis2, ierr) integer, intent(in) :: mf integer, intent(in) :: nf @@ -1542,52 +2797,58 @@ contains end do end function facet_radius_value - subroutine add_contact_pair_force_vec(max_nodes, nslave, slave_conn, nmaster, & - master_conn, force_vec, ierr) + subroutine add_contact_pair_force_mortar(max_nodes, nslave, slave_conn, & + nmaster, master_conn, traction, & + slave_w, master_w, scale, ierr) integer, intent(in) :: max_nodes integer, intent(in) :: nslave integer, intent(in) :: nmaster integer, intent(in) :: slave_conn(max_nodes) integer, intent(in) :: master_conn(max_nodes) - real(rk), intent(in) :: force_vec(NDIM) + real(rk), intent(in) :: traction(NDIM) + real(rk), intent(in) :: slave_w(max_nodes) + real(rk), intent(in) :: master_w(max_nodes) + real(rk), intent(in) :: scale integer, intent(out) :: ierr integer :: local_node integer :: node integer :: idir integer :: dof - real(rk) :: weight - weight = ONE / real(nslave, rk) do local_node = 1, nslave node = database_find_node(slave_conn(local_node)) if (node .eq. 0) then - call error_set(FEM_ERR_INPUT, 'contact pair slave facet has unknown node', ierr) + call error_set(FEM_ERR_INPUT, 'contact mortar slave facet has unknown node', ierr) return end if do idir = 1, NDIM dof = dof_id(idir,node) - if (dof .gt. 0) fcontact(dof) = fcontact(dof) + weight * force_vec(idir) + if (dof .gt. 0) then + fcontact(dof) = fcontact(dof) + scale * slave_w(local_node) * traction(idir) + end if end do end do - weight = ONE / real(nmaster, rk) do local_node = 1, nmaster node = database_find_node(master_conn(local_node)) if (node .eq. 0) then - call error_set(FEM_ERR_INPUT, 'contact pair master facet has unknown node', ierr) + call error_set(FEM_ERR_INPUT, 'contact mortar master facet has unknown node', ierr) return end if do idir = 1, NDIM dof = dof_id(idir,node) - if (dof .gt. 0) fcontact(dof) = fcontact(dof) - weight * force_vec(idir) + if (dof .gt. 0) then + fcontact(dof) = fcontact(dof) - scale * master_w(local_node) * traction(idir) + end if end do end do ierr = FEM_SUCCESS - end subroutine add_contact_pair_force_vec + end subroutine add_contact_pair_force_mortar - subroutine add_contact_pair_tan_matrix(kmat, max_nodes, nslave, slave_conn, & - nmaster, master_conn, kmat3, ierr) + subroutine add_contact_pair_tan_mortar(kmat, max_nodes, nslave, slave_conn, & + nmaster, master_conn, kmat3, ss_w, & + sm_w, ms_w, mm_w, scale, ierr) type(csr_matrix), intent(inout) :: kmat integer, intent(in) :: max_nodes integer, intent(in) :: nslave @@ -1595,28 +2856,29 @@ contains integer, intent(in) :: slave_conn(max_nodes) integer, intent(in) :: master_conn(max_nodes) real(rk), intent(in) :: kmat3(NDIM,NDIM) + real(rk), intent(in) :: ss_w(max_nodes,max_nodes) + real(rk), intent(in) :: sm_w(max_nodes,max_nodes) + real(rk), intent(in) :: ms_w(max_nodes,max_nodes) + real(rk), intent(in) :: mm_w(max_nodes,max_nodes) + real(rk), intent(in) :: scale integer, intent(out) :: ierr integer :: i integer :: j - real(rk) :: ws - real(rk) :: wm if (maxval(abs(kmat3)) .le. tiny(ONE)) then ierr = FEM_SUCCESS return end if - ws = ONE / real(nslave, rk) - wm = ONE / real(nmaster, rk) do i = 1, nslave do j = 1, nslave call add_contact_pair_mat_block(kmat, slave_conn(i), slave_conn(j), & - ws * ws, kmat3, ierr) + scale * ss_w(i,j), kmat3, ierr) if (ierr .ne. FEM_SUCCESS) return end do do j = 1, nmaster call add_contact_pair_mat_block(kmat, slave_conn(i), master_conn(j), & - -ws * wm, kmat3, ierr) + -scale * sm_w(i,j), kmat3, ierr) if (ierr .ne. FEM_SUCCESS) return end do end do @@ -1624,18 +2886,18 @@ contains do i = 1, nmaster do j = 1, nslave call add_contact_pair_mat_block(kmat, master_conn(i), slave_conn(j), & - -wm * ws, kmat3, ierr) + -scale * ms_w(i,j), kmat3, ierr) if (ierr .ne. FEM_SUCCESS) return end do do j = 1, nmaster call add_contact_pair_mat_block(kmat, master_conn(i), master_conn(j), & - wm * wm, kmat3, ierr) + scale * mm_w(i,j), kmat3, ierr) if (ierr .ne. FEM_SUCCESS) return end do end do ierr = FEM_SUCCESS - end subroutine add_contact_pair_tan_matrix + end subroutine add_contact_pair_tan_mortar subroutine add_contact_pair_mat_block(kmat, node_a_ext, node_b_ext, scale, & kmat3, ierr) diff --git a/src/mesh/fem_database.f90 b/src/mesh/fem_database.f90 index 4402935..e4e5a66 100644 --- a/src/mesh/fem_database.f90 +++ b/src/mesh/fem_database.f90 @@ -156,6 +156,10 @@ module fem_database real(rk), allocatable :: contact_pair_work(:) real(rk), allocatable :: contact_pair_max_penetration(:) real(rk), allocatable :: contact_pair_force_norm(:) + real(rk), allocatable :: contact_pair_tangent_force(:,:) + real(rk), allocatable :: contact_pair_slip(:) + integer, allocatable :: contact_pair_stick_count(:) + integer, allocatable :: contact_pair_slip_count(:) contains @@ -627,6 +631,10 @@ contains if (allocated(contact_pair_work)) deallocate(contact_pair_work) if (allocated(contact_pair_max_penetration)) deallocate(contact_pair_max_penetration) if (allocated(contact_pair_force_norm)) deallocate(contact_pair_force_norm) + if (allocated(contact_pair_tangent_force)) deallocate(contact_pair_tangent_force) + if (allocated(contact_pair_slip)) deallocate(contact_pair_slip) + if (allocated(contact_pair_stick_count)) deallocate(contact_pair_stick_count) + if (allocated(contact_pair_slip_count)) deallocate(contact_pair_slip_count) ncontact_pair = num_pairs contact_active_count = 0 @@ -642,6 +650,10 @@ contains contact_pair_active(num_pairs), contact_pair_work(num_pairs), & contact_pair_max_penetration(num_pairs), & contact_pair_force_norm(num_pairs), & + contact_pair_tangent_force(NDIM,num_pairs), & + contact_pair_slip(num_pairs), & + contact_pair_stick_count(num_pairs), & + contact_pair_slip_count(num_pairs), & stat=stat) if (stat .ne. 0) then ncontact_pair = 0 @@ -658,6 +670,10 @@ contains contact_pair_work = ZERO contact_pair_max_penetration = ZERO contact_pair_force_norm = ZERO + contact_pair_tangent_force = ZERO + contact_pair_slip = ZERO + contact_pair_stick_count = 0 + contact_pair_slip_count = 0 end if ierr = FEM_SUCCESS @@ -862,6 +878,10 @@ contains if (allocated(contact_pair_work)) contact_pair_work = ZERO if (allocated(contact_pair_max_penetration)) contact_pair_max_penetration = ZERO if (allocated(contact_pair_force_norm)) contact_pair_force_norm = ZERO + if (allocated(contact_pair_tangent_force)) contact_pair_tangent_force = ZERO + if (allocated(contact_pair_slip)) contact_pair_slip = ZERO + if (allocated(contact_pair_stick_count)) contact_pair_stick_count = 0 + if (allocated(contact_pair_slip_count)) contact_pair_slip_count = 0 ierr = FEM_SUCCESS end subroutine database_initialize @@ -1658,6 +1678,10 @@ contains if (allocated(contact_pair_work)) deallocate(contact_pair_work) if (allocated(contact_pair_max_penetration)) deallocate(contact_pair_max_penetration) if (allocated(contact_pair_force_norm)) deallocate(contact_pair_force_norm) + if (allocated(contact_pair_tangent_force)) deallocate(contact_pair_tangent_force) + if (allocated(contact_pair_slip)) deallocate(contact_pair_slip) + if (allocated(contact_pair_stick_count)) deallocate(contact_pair_stick_count) + if (allocated(contact_pair_slip_count)) deallocate(contact_pair_slip_count) nnode = 0 nelem = 0 diff --git a/src/output/fem_output_csv.f90 b/src/output/fem_output_csv.f90 index e26ee34..d11cadb 100644 --- a/src/output/fem_output_csv.f90 +++ b/src/output/fem_output_csv.f90 @@ -333,17 +333,19 @@ contains end if write(unit_id,'(A)',iostat=ios) & - 'kind,pair_id,active_count,max_penetration,force_norm,work_estimate,cumulative_work' + 'kind,pair_id,active_count,max_penetration,force_norm,work_estimate,'// & + 'cumulative_work,stick_count,slip_count,accumulated_slip,ftx,fty,ftz' if (ios .ne. 0) then close(unit_id) call error_set(FEM_ERR_OUTPUT, 'failed to write contact CSV header', ierr) return end if - write(unit_id,'(A,A,I12,A,I12,4(A,ES24.16))',iostat=ios) & + write(unit_id,'(A,A,I12,A,I12,4(A,ES24.16),2(A,I12),4(A,ES24.16))',iostat=ios) & 'SUMMARY', ',', 0, ',', contact_active_count, & ',', contact_max_penetration, ',', contact_force_norm, & - ',', contact_work_step, ',', contact_work_total + ',', contact_work_step, ',', contact_work_total, ',', 0, ',', 0, & + ',', ZERO, ',', ZERO, ',', ZERO, ',', ZERO if (ios .ne. 0) then close(unit_id) call error_set(FEM_ERR_OUTPUT, 'failed to write contact CSV summary row', ierr) @@ -362,9 +364,14 @@ contains if (allocated(contact_pair_force_norm)) force_sum = contact_pair_force_norm(pair) if (allocated(contact_pair_work)) work_sum = contact_pair_work(pair) - write(unit_id,'(A,A,I12,A,I12,4(A,ES24.16))',iostat=ios) & + write(unit_id,'(A,A,I12,A,I12,4(A,ES24.16),2(A,I12),4(A,ES24.16))',iostat=ios) & 'PAIR', ',', pair, ',', active, ',', max_pen, ',', force_sum, & - ',', work_sum, ',', ZERO + ',', work_sum, ',', ZERO, & + ',', contact_pair_stick_value(pair), ',', contact_pair_slip_value(pair), & + ',', contact_pair_accumulated_slip(pair), & + ',', contact_pair_tangent_component(pair, 1), & + ',', contact_pair_tangent_component(pair, 2), & + ',', contact_pair_tangent_component(pair, 3) if (ios .ne. 0) then close(unit_id) call error_set(FEM_ERR_OUTPUT, 'failed to write contact CSV pair row', ierr) @@ -376,6 +383,44 @@ contains ierr = FEM_SUCCESS end subroutine output_csv_write_contact + integer function contact_pair_stick_value(pair) + integer, intent(in) :: pair + + contact_pair_stick_value = 0 + if (.not. allocated(contact_pair_stick_count)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_stick_count)) return + contact_pair_stick_value = contact_pair_stick_count(pair) + end function contact_pair_stick_value + + integer function contact_pair_slip_value(pair) + integer, intent(in) :: pair + + contact_pair_slip_value = 0 + if (.not. allocated(contact_pair_slip_count)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_slip_count)) return + contact_pair_slip_value = contact_pair_slip_count(pair) + end function contact_pair_slip_value + + real(rk) function contact_pair_accumulated_slip(pair) + integer, intent(in) :: pair + + contact_pair_accumulated_slip = ZERO + if (.not. allocated(contact_pair_slip)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_slip)) return + contact_pair_accumulated_slip = contact_pair_slip(pair) + end function contact_pair_accumulated_slip + + real(rk) function contact_pair_tangent_component(pair, idir) + integer, intent(in) :: pair + integer, intent(in) :: idir + + contact_pair_tangent_component = ZERO + if (.not. allocated(contact_pair_tangent_force)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_tangent_force,2)) return + if (idir .lt. 1 .or. idir .gt. NDIM) return + contact_pair_tangent_component = contact_pair_tangent_force(idir,pair) + end function contact_pair_tangent_component + subroutine output_csv_average_element(elem, sig, eps, peps, eqps, damage) integer, intent(in) :: elem real(rk), intent(out) :: sig(NSTRESS) diff --git a/tests/interface/test_c_api.c b/tests/interface/test_c_api.c index 2fa77a4..e25084c 100644 --- a/tests/interface/test_c_api.c +++ b/tests/interface/test_c_api.c @@ -56,7 +56,7 @@ int main(void) ierr = femlite_get_version(&major, &minor, &patch); if (ierr != 0) return check_ierr(ierr, 2); - if (major != 0 || minor != 3 || patch != 1) return 3; + if (major != 0 || minor != 3 || patch != 2) return 3; ierr = femlite_read_inp("examples/c3d4_one_element.inp"); if (ierr != 0) return check_ierr(ierr, 4); diff --git a/tests/module/test_contact.f90 b/tests/module/test_contact.f90 index 85c0f70..31326f4 100644 --- a/tests/module/test_contact.f90 +++ b/tests/module/test_contact.f90 @@ -172,12 +172,12 @@ contains col = dof_id(3,5) call csr_get_value(kmat, row, col, value, ierr) if (ierr .ne. FEM_SUCCESS) call fail(43) - call check_close(value, 62.5_rk, 44) + call check_close(value, 1000.0_rk / 9.0_rk, 44) col = dof_id(3,1) call csr_get_value(kmat, row, col, value, ierr) if (ierr .ne. FEM_SUCCESS) call fail(45) - call check_close(value, -62.5_rk, 46) + call check_close(value, -1000.0_rk / 9.0_rk, 46) call csr_free(kmat, stat) call database_finalize(ierr) @@ -213,6 +213,10 @@ contains dof = dof_id(1,node) call check_close(fcontact(dof), -0.5_rk, 53) end do + if (contact_pair_stick_count(1) .ne. 0) call fail(500) + if (contact_pair_slip_count(1) .ne. 1) call fail(501) + call check_close(contact_pair_slip(1), 0.998_rk, 502) + call check_close(contact_pair_tangent_force(1,1), -2.0_rk, 503) call csr_init(kmat) call assembly_build_csr_pattern(kmat, ierr) @@ -224,7 +228,7 @@ contains col = dof_id(1,5) call csr_get_value(kmat, row, col, value, ierr) if (ierr .ne. FEM_SUCCESS) call fail(56) - call check_close(value, 12.5_rk, 57) + call check_close(value, 200.0_rk / 9.0_rk, 57) call csr_free(kmat, stat) call database_finalize(ierr) @@ -262,7 +266,6 @@ contains subroutine check_pair_clipped() integer :: ierr integer :: node - integer :: dof call setup_contact_shell_pair(ierr) if (ierr .ne. FEM_SUCCESS) call fail(70) @@ -275,32 +278,37 @@ contains call contact_apply_plane_forces(ierr) if (ierr .ne. FEM_SUCCESS) call fail(71) - do node = 5, 8 - dof = dof_id(3,node) - call check_close(fcontact(dof), 1.25_rk, 72) - end do - do node = 1, 4 - dof = dof_id(3,node) - call check_close(fcontact(dof), -1.25_rk, 73) - end do + call check_close(fcontact(dof_id(3,5)), 1.875_rk, 72) + call check_close(fcontact(dof_id(3,6)), 0.625_rk, 73) + call check_close(fcontact(dof_id(3,7)), 0.625_rk, 74) + call check_close(fcontact(dof_id(3,8)), 1.875_rk, 75) + call check_close(fcontact(dof_id(3,1)), -0.625_rk, 76) + call check_close(fcontact(dof_id(3,2)), -1.875_rk, 77) + call check_close(fcontact(dof_id(3,3)), -1.875_rk, 78) + call check_close(fcontact(dof_id(3,4)), -0.625_rk, 79) - if (contact_active_count .ne. 1) call fail(74) - if (contact_pair_active(1) .ne. 1) call fail(75) - call check_close(contact_max_penetration, 0.01_rk, 76) - call check_close(contact_pair_max_penetration(1), 0.01_rk, 77) - call check_close(contact_force_norm, 5.0_rk, 78) - call check_close(contact_pair_force_norm(1), 5.0_rk, 79) - call check_close(contact_work_step, 0.025_rk, 80) - call check_close(contact_pair_work(1), 0.025_rk, 81) + if (contact_active_count .ne. 1) call fail(80) + if (contact_pair_active(1) .ne. 1) call fail(81) + call check_close(contact_max_penetration, 0.01_rk, 82) + call check_close(contact_pair_max_penetration(1), 0.01_rk, 83) + call check_close(contact_force_norm, 5.0_rk, 84) + call check_close(contact_pair_force_norm(1), 5.0_rk, 85) + call check_close(contact_work_step, 0.025_rk, 86) + call check_close(contact_pair_work(1), 0.025_rk, 87) call database_finalize(ierr) - if (ierr .ne. FEM_SUCCESS) call fail(82) + if (ierr .ne. FEM_SUCCESS) call fail(88) end subroutine check_pair_clipped subroutine check_pair_grid() + type(csr_matrix) :: kmat integer :: ierr + integer :: stat integer :: node integer :: dof + integer :: row + integer :: col + real(rk) :: value call setup_contact_grid_pair(ierr) if (ierr .ne. FEM_SUCCESS) call fail(90) @@ -323,6 +331,22 @@ contains if (contact_active_count .ne. 1) call fail(95) call check_close(contact_force_norm, 10.0_rk, 96) + call csr_init(kmat) + call assembly_build_csr_pattern(kmat, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(510) + call contact_apply_plane_tangent(kmat, ZERO, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(511) + row = dof_id(3,73) + col = dof_id(3,73) + call csr_get_value(kmat, row, col, value, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(512) + call check_close(value, 1000.0_rk / 9.0_rk, 513) + col = dof_id(3,1) + call csr_get_value(kmat, row, col, value, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(514) + call check_close(value, -1000.0_rk / 9.0_rk, 515) + call csr_free(kmat, stat) + call database_finalize(ierr) if (ierr .ne. FEM_SUCCESS) call fail(97) end subroutine check_pair_grid @@ -564,7 +588,10 @@ contains real(rk), intent(in) :: expected integer, intent(in) :: code - if (abs(actual - expected) .gt. TOL * max(ONE, abs(expected))) call fail(code) + if (abs(actual - expected) .gt. TOL * max(ONE, abs(expected))) then + write(*,*) 'check_close actual expected:', actual, expected + call fail(code) + end if end subroutine check_close subroutine fail(code) diff --git a/tests/performance/perf_p1.f90 b/tests/performance/perf_p1.f90 index 01a254f..7e8dc13 100644 --- a/tests/performance/perf_p1.f90 +++ b/tests/performance/perf_p1.f90 @@ -40,7 +40,7 @@ program perf_p1 call fem_get_version(major, minor, patch, ierr) call exit_if_error(ierr) - if (major .ne. 0 .or. minor .ne. 3 .or. patch .ne. 1) then + if (major .ne. 0 .or. minor .ne. 3 .or. patch .ne. 2) then write(*,'(A,I1,A,I1,A,I1)') 'unexpected FemLite version ', major, '.', minor, '.', patch call exit(3) end if diff --git a/tests/unit/test_smoke.f90 b/tests/unit/test_smoke.f90 index 3a4bad9..ca73689 100644 --- a/tests/unit/test_smoke.f90 +++ b/tests/unit/test_smoke.f90 @@ -94,7 +94,7 @@ program test_smoke if (ierr .ne. 0) stop 2 if (major .ne. 0) stop 3 if (minor .ne. 3) stop 4 - if (patch .ne. 1) stop 5 + if (patch .ne. 2) stop 5 call fem_read_inp('examples/c3d4_one_element.inp', ierr) if (ierr .ne. 0) stop 11