diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a34094..219efbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.20) project(FemLite - VERSION 0.4.1 + VERSION 0.4.3 LANGUAGES C Fortran ) @@ -81,6 +81,7 @@ add_library(femlite_core STATIC src/element/fem_element_internal_helpers.f90 src/element/fem_element_internal.f90 src/element/fem_element_stiffness.f90 + src/boundary/fem_contact_candidate_cache.f90 src/solver/fem_sparse_csr.f90 src/solver/fem_linear_solver_cg.f90 src/solver/fem_assembly_csr.f90 @@ -91,6 +92,7 @@ add_library(femlite_core STATIC src/boundary/fem_contact_target.f90 src/boundary/fem_contact_geometry.f90 src/boundary/fem_contact_search.f90 + src/boundary/fem_contact_history.f90 src/boundary/fem_contact_friction.f90 src/boundary/fem_contact_force.f90 src/boundary/fem_contact_tangent.f90 @@ -292,6 +294,13 @@ add_executable(perf_p1 target_link_libraries(perf_p1 PRIVATE femlite_core) femlite_apply_fortran_options(perf_p1) +add_executable(perf_contact_pair + tests/performance/perf_contact_pair.f90 +) + +target_link_libraries(perf_contact_pair PRIVATE femlite_core) +femlite_apply_fortran_options(perf_contact_pair) + add_test(NAME smoke COMMAND test_smoke) set_tests_properties(smoke PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" @@ -375,6 +384,21 @@ set_tests_properties(solver_implicit PROPERTIES add_test(NAME contact COMMAND test_contact) set_tests_properties(contact PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + RUN_SERIAL TRUE +) + +add_test(NAME contact_omp1 COMMAND test_contact) +set_tests_properties(contact_omp1 PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + ENVIRONMENT "OMP_NUM_THREADS=1" + RUN_SERIAL TRUE +) + +add_test(NAME contact_omp4 COMMAND test_contact) +set_tests_properties(contact_omp4 PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + ENVIRONMENT "OMP_NUM_THREADS=4" + RUN_SERIAL TRUE ) add_test(NAME vtk_output COMMAND test_vtk_output) @@ -392,6 +416,18 @@ set_tests_properties(c_api PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" ) +add_test(NAME perf_contact_pair_smoke COMMAND perf_contact_pair 32 1) +set_tests_properties(perf_contact_pair_smoke PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + LABELS "performance_smoke" +) + +add_test(NAME perf_contact_pair_medium COMMAND perf_contact_pair 64 1) +set_tests_properties(perf_contact_pair_medium PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + LABELS "performance_smoke" +) + if(Python3_Interpreter_FOUND) add_test(NAME source_size COMMAND "${Python3_EXECUTABLE}" @@ -407,6 +443,18 @@ if(Python3_Interpreter_FOUND) WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" ) + add_test(NAME perf_contact_pair_omp_consistency + COMMAND "${Python3_EXECUTABLE}" + "${CMAKE_SOURCE_DIR}/tests/scripts/check_perf_contact_pair_omp.py" + "${CMAKE_SOURCE_DIR}" + "$" + ) + set_tests_properties(perf_contact_pair_omp_consistency PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + LABELS "performance_smoke" + RUN_SERIAL TRUE + ) + add_test(NAME python_debug COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tests/scripts/test_python_debug.py" @@ -502,6 +550,25 @@ if(Python3_Interpreter_FOUND) LABELS "example_smoke" TIMEOUT 60 ) + + add_test(NAME example_contact_block_sliding_smoke + COMMAND "${Python3_EXECUTABLE}" + "${CMAKE_SOURCE_DIR}/tests/scripts/run_example_smoke.py" + "${CMAKE_SOURCE_DIR}" + "$" + "${CMAKE_BINARY_DIR}" + contact_block_sliding + examples/contact_block_sliding/block_sliding_friction.inp + run + --dynamic-steps 20 + --output-interval 10 + --require-contact + --require-friction + ) + set_tests_properties(example_contact_block_sliding_smoke PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + LABELS "example_smoke" + ) endif() add_test(NAME implicit_inp COMMAND test_implicit_inp) diff --git a/docs/03_detailed_design.md b/docs/03_detailed_design.md index d8e6755..d5a55d6 100644 --- a/docs/03_detailed_design.md +++ b/docs/03_detailed_design.md @@ -60,6 +60,8 @@ src/boundary/fem_contact_state.f90 src/boundary/fem_contact_target.f90 src/boundary/fem_contact_geometry.f90 src/boundary/fem_contact_search.f90 +src/boundary/fem_contact_candidate_cache.f90 +src/boundary/fem_contact_history.f90 src/boundary/fem_contact_friction.f90 src/boundary/fem_contact_force.f90 src/boundary/fem_contact_tangent.f90 @@ -910,9 +912,14 @@ elements_step_xxxxx.csv 至少包含: - `convergence.csv`:静力和隐式动力增量迭代历史。 - `reactions_step_xxxxx.csv`:节点反力。 - `contact_XXXXXXXX.csv`:接触状态、接触力和接触功。 - 当前字段为 `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 行。 + 当前字段为 `kind,pair_id,active_count,max_penetration,force_norm,work_estimate,cumulative_work,stick_count,slip_count,accumulated_slip,friction_work,friction_work_total,friction_regularization,ftx,fty,ftz,status,activation_count,release_count,gap_residual,aug_factor,aug_lambda,search_facets,search_masters,search_candidates,search_active,search_bvh_nodes,search_cache_used,search_cache_overflow,search_cpu_time,contact_force_cpu_time,contact_tangent_cpu_time`,包含 summary 行和逐 contact pair 行。 - 静力求解结束后必须写出与显式结果一致的 VTK 场文件。 +第四版增加: + +- restart v2 在原有 DOF、单元状态和高斯点状态之后增加 `CONTACT_HISTORY` 段,保存 mortar 积分点摩擦历史槽。 +- restart 读入端兼容 v1;当文件没有 `CONTACT_HISTORY` 段时,应清空当前接触历史,避免旧模型状态串入重启计算。 + ## 19. 主程序模块 模块:fem_program.f90 diff --git a/docs/04_mathematical_model_and_algorithms.md b/docs/04_mathematical_model_and_algorithms.md index 0f10ab0..de6f1ec 100644 --- a/docs/04_mathematical_model_and_algorithms.md +++ b/docs/04_mathematical_model_and_algorithms.md @@ -798,12 +798,57 @@ K_eff Delta_u = R - 面-面 contact pair 使用边界 facet 的当前构形坐标、速度、法向、面积、中心和等效半径。 - slave facet 沿 master 法向投影到 master facet 平面,在局部二维坐标中执行多边形裁剪;裁剪面积大于零且法向间隙为负时激活。多个 master facet 同时激活时按裁剪面积贡献,且总面积不超过 slave facet 面积。 - 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 * 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 约束求解、摩擦正则化参数体系和大规模接触收敛控制。 +- 当前实现包含 master facet 均匀空间网格候选搜索、master facet BVH 宽相搜索和 facet 中心/等效半径 broad-phase 精筛;OpenMP 可用时 BVH 搜索按 slave facet 并行遍历。V4.4 增加 force/tangent 共享的跨调用 broad candidate 缓存,缓存命中只复用待检查 master facet 列表,仍重新计算当前 gap、法向速度、mortar 权重和 active 状态;当拓扑、pair 参数或 facet 运动超阈值时失效并回退 BVH 搜索。接触状态记录激活贡献数、最大穿透量、接触力范数和 `0.5 * |F_contact| * penetration` 形式的接触功估计。 +- 0.3.2 输出 contact pair 的 stick/slip 计数、累计滑移和切向摩擦力历史。当前仍未实现工程级 mortar 约束求解和大规模接触收敛控制。 - 0.3.3 支持 Abaqus `*SURFACE, TYPE=ELEMENT` 输入子集、face label 筛选、壳 `SPOS`/`SNEG` 侧向语义和数据行式 `*CONTACT PAIR` 子集;这些输入语义只改变候选 facet、有效法向和接触目标匹配,不改变上述 penalty/mortar 力学离散形式。 +第四版 V4.2 增强 active set 与 augmented penalty 起步路径: + +- contact pair active set 以最近一次接触力评估的激活贡献数为基础,状态分为 `open`、`activated`、`maintained` 和 `released`。 +- 法向 gap 残差采用当前 pair 内激活贡献的最大穿透量: + +```text +g_res = max(max(-g_n, 0)) +``` + +- 当前增强罚函数采用接触对级标量乘子,而不是完整 mortar 积分点 Lagrange multiplier。法向接触压力为: + +```text +p_n = k_n * max(-g_n, 0) + lambda_n +``` + +- 每次接触力评估后,对 active pair 更新: + +```text +lambda_n <- lambda_n + alpha_aug * k_n * g_res +``` + +其中 `alpha_aug` 为 `AUGMENT` 或 `AUGMENT_FACTOR` 参数。released pair 自动清零 `lambda_n`,避免重新接触时继承已释放状态的乘子。 +- 该路径适用于刚性平面 analytical contact pair 和面-面 contact pair 的法向力;mortar 权重、V4.3 摩擦历史和切线装配沿用 contact pair 路径。 +- V4.2 的 augmented penalty 是工程增强和诊断框架,不等价于完整 augmented Lagrangian/mortar 约束求解器;完整积分点乘子、摩擦正则化和更严格收敛准则进入后续 0.4.x 阶段。 + +第四版 V4.3 起步增强 contact-pair 级摩擦正则化和摩擦功输出: + +- 默认 `FRICTION_REGULARIZATION = 0` 时,保持第三版 Coulomb 截断路径。 +- 当正则化滑移尺度 `s_reg > 0` 时,Coulomb 极限使用平滑因子: + +```text +F_t_limit = mu * F_n * tanh(s_eq / s_reg) +s_eq = max(|Delta s_t|, |F_t_trial| / k_t) +``` + +- 滑移分支的摩擦耗散功估计为: + +```text +W_fric_step += max(0, -F_t . Delta s_t) +``` + +- 当前 `Delta s_t` 沿用 facet 平均切向相对速度/滑移增量近似,历史变量按 mortar 积分点权重拆分保存。 +- 接触切线的切向近似块使用同一 `tanh(s_eq / s_reg)` 因子折减 `mu`,使正则化摩擦力和 pair 级切线在趋势上保持一致。当前为工程近似一致切线,完整 stick/slip 算法切线仍可在后续增强。 +- 当前摩擦历史已经从单一 contact pair 标量推进到 mortar 积分点槽,槽键为 `pair + slave element/face + master element/face + point_id`。每个槽保存切向力、累计滑移、累计摩擦功和 stick/slip 状态;下一步 trial 历史由同一 facet pair 下的积分点槽求和得到。 + 第一版不实现接触,只保留 fcontact 向量和接口位置。 ## 18. 稳定性与误差控制 diff --git a/docs/05_interface_control_document.md b/docs/05_interface_control_document.md index 144a388..88a9f6d 100644 --- a/docs/05_interface_control_document.md +++ b/docs/05_interface_control_document.md @@ -38,6 +38,12 @@ - 节点反力查询接口。 - 接触状态、接触力和接触功查询接口。 +第四版扩展: + +- node-based surface、analytical rigid plane surface 和 contact property 查询/输入子集。 +- contact pair active-set、gap residual 和 augmented penalty 诊断接口。 +- contact pair 搜索统计接口,用于 V4.4 搜索/并行性能验收。 + ## 3. 接口设计原则 接口设计遵循: @@ -555,16 +561,132 @@ subroutine fem_get_contact_summary(active_count, work_step, work_total, & end subroutine ``` +第四版追加接触诊断查询接口,不改变旧 summary 接口: + +```fortran +subroutine fem_get_contact_diagnostics(active_pair_count, & + activated_pair_count, & + maintained_pair_count, & + released_pair_count, & + max_gap_residual, max_aug_lambda, ierr) + integer, intent(out) :: active_pair_count + integer, intent(out) :: activated_pair_count + integer, intent(out) :: maintained_pair_count + integer, intent(out) :: released_pair_count + real(rk), intent(out) :: max_gap_residual + real(rk), intent(out) :: max_aug_lambda + integer, intent(out) :: ierr +end subroutine +``` + +- `active_pair_count`:最近一次接触力评估中至少有一个激活贡献的 contact pair 数。 +- `activated_pair_count`:本次由 open 切换为 active 的 pair 数。 +- `maintained_pair_count`:本次保持 active 的 pair 数。 +- `released_pair_count`:本次由 active 切换为 open 的 pair 数。 +- `max_gap_residual`:逐 pair gap 残差最大值,当前定义为最近一次力评估中的最大穿透量。 +- `max_aug_lambda`:逐 pair augmented penalty 法向乘子最大值。 + +第四版 V4.3 起步追加摩擦输出查询接口: + +```fortran +subroutine fem_get_contact_friction(work_step, work_total, & + max_accumulated_slip, ierr) + real(rk), intent(out) :: work_step + real(rk), intent(out) :: work_total + real(rk), intent(out) :: max_accumulated_slip + integer, intent(out) :: ierr +end subroutine +``` + +- `work_step`:最近一次接触评估中的正摩擦耗散功估计。 +- `work_total`:累计正摩擦耗散功估计。 +- `max_accumulated_slip`:逐 contact pair 累计滑移最大值。 + +V4.3 进一步追加 mortar 积分点级历史统计接口: + +```fortran +subroutine fem_get_contact_hist_stats(slot_count, max_slot_slip, & + max_slot_work, max_tangent_force, ierr) + integer, intent(out) :: slot_count + real(rk), intent(out) :: max_slot_slip + real(rk), intent(out) :: max_slot_work + real(rk), intent(out) :: max_tangent_force + integer, intent(out) :: ierr +end subroutine +``` + +- `slot_count`:当前模型内已创建的 mortar 积分点摩擦历史槽数量。 +- `max_slot_slip`:单个历史槽的最大累计滑移。 +- `max_slot_work`:单个历史槽的最大累计摩擦功。 +- `max_tangent_force`:单个历史槽中历史切向力模长最大值。 +- 当前历史槽键为 `pair + slave element/face + master element/face + point_id`,其中 `point_id` 是该 slave/master facet overlap 内的 mortar 积分点序号。 + +V4.4 起步追加 contact pair 搜索统计接口: + +```fortran +subroutine fem_get_contact_search_stats(facet_count, master_count, & + broad_count, active_count, & + bvh_node_count, ierr) + integer, intent(out) :: facet_count + integer, intent(out) :: master_count + integer, intent(out) :: broad_count + integer, intent(out) :: active_count + integer, intent(out) :: bvh_node_count + integer, intent(out) :: ierr +end subroutine +``` + +- `facet_count`:最近一次接触力评估中累计处理的 boundary facet 数;多 contact pair 时按 pair 评估次数累计。 +- `master_count`:最近一次接触力评估中累计识别的 master facet 数。 +- `broad_count`:搜索第一遍候选评估次数;全搜索、uniform grid 和 BVH 路径均记录。 +- `active_count`:候选评估中判定为接触激活的次数。 +- `bvh_node_count`:最近一次接触力评估累计构建的 BVH 节点数;非 BVH 路径为 0。 + +V4.4 追加接触性能计时接口,时间单位为秒,口径为 Fortran `cpu_time`: + +```fortran +subroutine fem_get_contact_perf_stats(search_time, force_time, & + tangent_time, ierr) + real(rk), intent(out) :: search_time + real(rk), intent(out) :: force_time + real(rk), intent(out) :: tangent_time + integer, intent(out) :: ierr +end subroutine +``` + +- `search_time`:最近一次接触状态周期内 contact pair 搜索准备和第一遍候选评估累计 CPU 时间。 +- `force_time`:最近一次 `contact_apply_plane_forces` 总 CPU 时间。 +- `tangent_time`:最近一次接触切线装配累计 CPU 时间;若未调用切线装配则为 0。 + +V4.4 追加 BVH 局部候选缓存统计接口: + +```fortran +subroutine fem_get_contact_cache_stats(used_count, overflow_count, ierr) + integer, intent(out) :: used_count + integer, intent(out) :: overflow_count + integer, intent(out) :: ierr +end subroutine +``` + +- `used_count`:最近一次接触力评估中使用 BVH active 候选缓存施力的 slave facet 数。 +- `overflow_count`:active 候选数超过固定缓存容量并回退到旧 BVH 遍历施力路径的 slave facet 数。 + +Restart 说明: + +- restart 文件格式从 V4.3 起写出为 `FEMLITE_RESTART 2`。 +- v2 在 DOF、单元状态和高斯点状态之后追加 `CONTACT_HISTORY` 段,用于保存 mortar 积分点摩擦历史槽。 +- 读入端兼容旧的 v1 文件;若旧文件没有 `CONTACT_HISTORY` 段,则读入时清空当前接触历史。 + 第三版当前 contact pair 支持两种配置方式: 1. INP 文件中的 FemLite 子集: ```inp -*CONTACT PAIR, SLAVE=slave_target, MASTER=master_target, PENALTY=value, DAMPING=value, SEARCH=value, FRICTION=value +*CONTACT PAIR, SLAVE=slave_target, MASTER=master_target, PENALTY=value, DAMPING=value, SEARCH=value, FRICTION=value, FRICTION_REGULARIZATION=value, AUGMENT=value ``` - header 式写法中,`SLAVE`、`MASTER` 和 `PENALTY` 为必填参数。 -- `DAMPING`、`SEARCH` 和 `FRICTION` 为可选参数,未给出时默认为 0。 +- `DAMPING`、`SEARCH`、`FRICTION`、`FRICTION_REGULARIZATION` 和 `AUGMENT` 为可选参数,未给出时默认为 0。 - `SLAVE` 和 `MASTER` 可为 surface 名称、elset 名称、element id 字符串或 `ALL`。 - 解析器接受大小写混用、参数等号两侧空格以及单/双引号包裹的名称。 @@ -575,17 +697,17 @@ end subroutine slave_elset_or_element, face_label *SURFACE, NAME=master_surface, TYPE=ELEMENT master_elset_or_element, face_label -*CONTACT PAIR, PENALTY=value, DAMPING=value, SEARCH=value, FRICTION=value +*CONTACT PAIR, PENALTY=value, DAMPING=value, SEARCH=value, FRICTION=value, FRICTION_REGULARIZATION=value slave_surface, master_surface ``` -- `*SURFACE` 当前支持 `NAME` 参数;`TYPE` 可省略或显式写为 `ELEMENT`,其它 `TYPE` 会作为输入错误拒绝。 +- `*SURFACE` 当前支持 `TYPE=ELEMENT`、`TYPE=NODE` 和 analytical rigid `PLANE` 子集;`TYPE` 可省略,缺省为 `ELEMENT`。 - `OP` 可省略或写为 `ADD` / `NEW`:省略时按 `ADD` 处理;同名 surface 的 `ADD` 会追加数据行,`NEW` 会清除该 surface 已有数据行后重新定义。 - surface 可包含多条数据行;每行第一个字段可为 elset 名称或 element id,二者可在同一 surface 中混用。 - 第二字段 `face_label` 可省略;省略、`ALL` 或 `EXTERIOR` 表示该 target 的所有候选 facet 均可匹配。 - 当前支持 `S1`、`S2` 等面号标签,以及 `F1`、`FACE1` 等等价写法;壳单元支持 `SPOS`/`SNEG` 匹配其当前单一 shell facet。 - `SPOS`/`SNEG` 已参与接触计算语义:master surface 的 `SNEG` 会翻转有效接触法向;slave surface 若显式给出 `SPOS`/`SNEG`,候选接触只接受该侧法向朝向 master facet 的组合。 -- 更完整的 Abaqus surface 变体、surface 操作、node-based surface、analytic rigid surface 和更复杂的数据行变体仍列入后续版本。 +- 更完整的 Abaqus surface 变体、曲线/球/柱 analytical surface 和更复杂的数据行变体仍列入后续版本。 - 数据行式 `*CONTACT PAIR` 的 header 中仍要求 `PENALTY`,slave/master 从下一行读取。 2. Fortran95 核心数据库接口: @@ -616,6 +738,8 @@ end subroutine - `contact_pair_damping(pair)`:法向阻尼,必须非负。 - `contact_pair_search(pair)`:搜索距离,0 表示只按 facet 等效半径过滤。 - `contact_pair_friction(pair)`:Coulomb 摩擦系数,必须非负;0 表示无摩擦。 +- `contact_pair_friction_reg(pair)`:摩擦正则化滑移尺度,必须非负;0 表示经典 Coulomb 截断。 +- `contact_pair_aug_factor(pair)`:augmented penalty 更新系数,必须非负;0 表示仅使用 penalty。 当前 contact pair 支持投影裁剪 mortar 形函数积分、多 master facet 面积上限修正、Coulomb 粘滑历史、均匀空间网格候选搜索、OpenMP BVH 宽相搜索和 facet 空间 broad-phase 精筛。 @@ -627,7 +751,7 @@ end subroutine - `contact_max_penetration`:最近一次接触力评估的最大穿透量。 - `contact_force_norm`:最近一次接触力评估的接触力范数累加。 -逐 contact pair 统计保存在 `contact_pair_active/work/max_penetration/force_norm`、`contact_pair_stick_count/slip_count/slip/tangent_force` 数组中,并写入 `contact_XXXXXXXX.csv`。 +逐 contact pair 统计保存在 `contact_pair_active/work/max_penetration/force_norm`、`contact_pair_status/activation_count/release_count/gap_residual/aug_lambda`、`contact_pair_stick_count/slip_count/slip/tangent_force`、`contact_pair_friction_work/fric_work_total` 数组中,并写入 `contact_XXXXXXXX.csv`。V4.4 起,contact CSV 的 `SUMMARY` 行写出 `search_facets/search_masters/search_candidates/search_active/search_bvh_nodes` 全局搜索统计、`search_cache_used/search_cache_overflow` 缓存统计,以及 `search_cpu_time/contact_force_cpu_time/contact_tangent_cpu_time` 计时统计;逐 `PAIR` 行通过内部 Fortran 查询 `contact_get_pair_stats` 写出对应接触对的搜索、缓存和计时统计。当前 C ABI 只暴露全局 search/perf/cache 统计,pair 级查询先作为 Fortran 内部接口和 CSV 输出提供。 ## 13. 输出控制接口 @@ -771,3 +895,13 @@ fem_finalize - C ABI 暴露静力求解、反力查询和收敛状态查询。 - Python 调试接口支持运行静力算例并导出关键位移、反力和残差历史。 + +第四版包装层扩展: + +- C ABI 追加 `femlite_get_contact_diagnostics`,返回 active pair 数、activated/maintained/released pair 数、最大 gap residual 和最大 augmented penalty 乘子。 +- C ABI 追加 `femlite_get_contact_friction_summary`,返回摩擦 step/total 功和最大累计滑移。 +- C ABI 追加 `femlite_get_contact_history_stats`,返回 mortar 积分点摩擦历史槽数量、单槽最大累计滑移、单槽最大累计摩擦功和单槽最大切向力模长。 +- C ABI 追加 `femlite_get_contact_search_stats`,返回 contact pair 搜索 facet/master/candidate/active/BVH 节点统计。 +- C ABI 追加 `femlite_get_contact_perf_stats`,返回 contact search/force/tangent CPU 时间统计。 +- C ABI 追加 `femlite_get_contact_cache_stats`,返回 BVH 候选缓存使用和溢出统计。 +- 上述函数只追加新符号,不改变 `femlite_get_contact_summary` 的签名和含义。 diff --git a/docs/06_test_and_verification_plan.md b/docs/06_test_and_verification_plan.md index 211df2f..6cbee2b 100644 --- a/docs/06_test_and_verification_plan.md +++ b/docs/06_test_and_verification_plan.md @@ -54,6 +54,13 @@ Python 环境约定: - 静力反力、收敛历史和 VTK 输出测试。 - 源码规模检查:普通过程/函数尽量不超过 100 行,超过 128 行必须登记审查理由;源文件尽量不超过 1500 行,超过 2000 行必须审查是否拆分。 +第四版扩展: + +- surface/contact 输入增强测试,包括 node-based surface、analytical rigid plane surface 和 contact property。 +- active set、gap residual 和 augmented penalty 诊断测试。 +- 摩擦正则化、摩擦功和最大累计滑移输出测试。 +- 接触示例 smoke 测试应覆盖刚性平面压入、两实体块面-面压缩和壳-刚性平面接触。 + ## 4. 测试目录结构 建议目录结构: @@ -266,6 +273,8 @@ SPH总力映射测试 relative tolerance 1.0e-10 to 1.0e-8 - amplitude 常值和表格插值。 - 刚性平面 contact penalty/damping 力和切线。 - 面-面 contact pair 法向力、阻尼项、Coulomb 粘滑历史、多 master 分摊、投影裁剪 mortar 权重、空间网格候选、BVH 宽相搜索、空间 broad-phase、CSR 图案、切线块和壳 `SPOS`/`SNEG` 侧向语义。 +- contact pair active set 的 activated/maintained/released 状态切换、gap residual、augmented penalty 乘子更新和释放后清零。 +- contact pair Coulomb 摩擦默认路径、正则化 Coulomb 路径、正则化切线折减、滑移耗散功和累计滑移。 工程卫生测试: @@ -273,15 +282,21 @@ SPH总力映射测试 relative tolerance 1.0e-10 to 1.0e-8 - 普通过程/函数 review target 为 100 行,超过 128 行必须在 `tests/source_size_exceptions.txt` 中登记具体审查理由和后续拆分方向。 - 源文件 review target 为 1500 行,超过 2000 行必须在 `tests/source_size_exceptions.txt` 中登记是否拆分、为何暂缓拆分及后续计划。 - 例外表只允许登记当前仍超过硬阈值的项目;过期例外会使 `source_size` 失败。 -- 0.3.3 当前无 active hard-threshold exception;剩余输出为 review-target warning,不阻断发布。 +- 0.4.3 当前无 active hard-threshold exception;剩余输出为 review-target warning,不阻断发布。 输出测试: - `history.csv` 字段存在。 - `nodes_step_xxxxx.csv` 字段存在。 - `elements_step_xxxxx.csv` 字段存在。 -- `contact_XXXXXXXX.csv` 字段存在,并包含 summary 与逐 contact pair 行、stick/slip 计数、累计滑移和切向力历史。 +- `contact_XXXXXXXX.csv` 字段存在,并包含 summary 与逐 contact pair 行、stick/slip 计数、累计滑移、切向力历史、summary 和 pair 级搜索统计、BVH 候选缓存统计和 CPU 时间统计。 - `fem_get_contact_summary` 返回激活数、单步/累计接触功、最大穿透量和接触力范数。 +- `fem_get_contact_diagnostics` 返回 active pair 数、activated/maintained/released pair 数、最大 gap residual 和最大 augmented penalty 乘子;C ABI 包装函数与 Fortran API 结果一致。 +- `fem_get_contact_friction` 和 C ABI `femlite_get_contact_friction_summary` 返回摩擦 step/total 功和最大累计滑移。 +- `fem_get_contact_hist_stats` 和 C ABI `femlite_get_contact_history_stats` 返回 mortar 积分点历史槽数、单槽最大滑移、单槽最大摩擦功和单槽最大切向力。 +- `fem_get_contact_search_stats` 和 C ABI `femlite_get_contact_search_stats` 返回 contact pair 搜索 facet/master/candidate/active/BVH 节点统计。 +- `fem_get_contact_perf_stats` 和 C ABI `femlite_get_contact_perf_stats` 返回 contact search/force/tangent CPU 时间统计。 +- `fem_get_contact_cache_stats` 和 C ABI `femlite_get_contact_cache_stats` 返回 BVH 候选缓存使用和溢出统计。 - 删除单元状态可输出。 示例结果留存策略: @@ -302,10 +317,21 @@ SPH总力映射测试 relative tolerance 1.0e-10 to 1.0e-8 接触验证扩展矩阵: - `*CONTACT PAIR` 合法/缺参/空格/引号/大小写输入解析。 -- `*SURFACE` element-target 子集、混合 elset/element-id 数据行、省略 face label、face label 筛选、`OP=ADD/NEW` 同名 surface 追加/覆盖、壳 `SPOS`/`SNEG` 法向符号和 slave 侧向筛选、非法 TYPE 拒绝与数据行式 `*CONTACT PAIR` 输入解析。 -- contact CSV schema 和 summary/pair 行字段检查。 -- 法向接触功、摩擦功、stick/slip 计数和累计滑移 sanity check。 -- 已接入 `example_smoke` CTest 分组:shell patch S3/S4 静力 smoke、clamped square plate pressure pulse S4 短步数动力 smoke、rigid sphere impact plate S3/S4 短步数动力 smoke;rigid impact smoke 需要 final contact CSV 中出现 active contact。 +- `*SURFACE` element-target 子集、混合 elset/element-id 数据行、省略 face label、face label 筛选、`OP=ADD/NEW` 同名 surface 追加/覆盖、node-based surface、analytical rigid plane surface、壳 `SPOS`/`SNEG` 法向符号和 slave 侧向筛选、非法 TYPE 拒绝与数据行式 `*CONTACT PAIR` 输入解析。 +- `*SURFACE INTERACTION` / `*CONTACT PROPERTY` 中 penalty、damping、search、friction、friction regularization、augment 参数解析和 contact pair 继承。 +- contact CSV schema 和 summary/pair 行字段检查,包括 search facet/master/candidate/active/BVH 节点统计列、BVH 候选缓存列、CPU 时间统计列和逐 `PAIR` 行统计值。 +- contact diagnostics API 和 C ABI 包装层检查。 +- contact friction summary API 和 C ABI 包装层检查。 +- contact history stats Fortran API 和 C ABI 包装层检查。 +- contact search stats Fortran API 和 C ABI 包装层检查;小算例要求候选数和 active 候选数非零,空模型要求全零。 +- contact performance stats Fortran API 和 C ABI 包装层检查;小算例要求非负,空模型要求全零。 +- contact cache stats Fortran API 和 C ABI 包装层检查;BVH 小算例要求 cache used 非零且 overflow 为零,空模型要求全零。 +- BVH 跨调用候选缓存检查;第一次 BVH 力计算应建立缓存,第二次同几何力计算应命中缓存且 `search_bvh_nodes=0`,后续切线装配应复用同一候选缓存,slave facet 移动超过失效阈值后应失效并重新构建 BVH。 +- restart v2 写读 mortar 积分点摩擦历史,验证重启后历史槽、累计滑移、摩擦功和切向力保持一致。 +- 法向接触功、摩擦功、stick/slip 计数、mortar 积分点历史槽、正则化摩擦切线和累计滑移 sanity check。 +- V4.2 新增示例:rigid plane indentation、two blocks compression、shell rigid plane contact;每个示例应可通过目录内 `run_case.bat` 自动运行和检查结果。 +- V4.3 新增示例:contact block sliding friction;检查正则化参数、累计滑移、切向力、摩擦功非负和 VTK 输出。 +- 已接入 `example_smoke` CTest 分组:shell patch S3/S4 静力 smoke、clamped square plate pressure pulse S4 短步数动力 smoke、rigid sphere impact plate S3/S4 短步数动力 smoke、contact block sliding friction 短步数动力 smoke;rigid impact smoke 需要 final contact CSV 中出现 active contact,sliding friction smoke 需要 final contact CSV 中出现正摩擦耗散。 - rigid sphere impact plate 的长步数结果保留为示例复现实验。 ## 11. 输入解析测试 @@ -342,6 +368,16 @@ SPH总力映射测试 relative tolerance 1.0e-10 to 1.0e-8 - *NODE FILE - *EL FILE +第四版新增 keyword 和参数测试: + +- *SURFACE, TYPE=NODE +- *SURFACE, TYPE=ANALYTICAL +- *SURFACE INTERACTION +- *CONTACT PROPERTY +- *FRICTION +- *CONTACT PAIR, INTERACTION=... +- *CONTACT PAIR, AUGMENT=... + 集合测试: - *NSET, GENERATE。 @@ -575,6 +611,16 @@ tests/reference - 峰值内存。 - 删除单元数量。 - 最小稳定时间步。 +- contact pair 全局与逐 pair facet/master/candidate/active/BVH 节点统计。 +- contact pair 全局与逐 pair BVH 候选缓存命中和溢出统计。 +- contact 全局与逐 pair search/force/tangent CPU 时间统计。 + +V4.4 contact pair 性能 smoke: + +- `perf_contact_pair_smoke` 使用程序内生成的 S4 master/slave 面对,默认 CTest 规模为 32 对面片;`perf_contact_pair_medium` 使用 64 对面片覆盖更厚的 BVH/cache/tangent 路径。 +- smoke 必须触发 BVH 搜索、active 接触和 BVH 候选缓存命中。 +- `perf_contact_pair_omp_consistency` 通过 `tests/scripts/check_perf_contact_pair_omp.py` 分别以 `OMP_NUM_THREADS=1/4` 运行同一性能 smoke,并要求 active、broad/active 候选、BVH 节点和缓存命中等非计时统计一致;该用例覆盖缩短后的 force 全局提交临界区和 tangent CSR 合并临界区。 +- 可执行程序 `perf_contact_pair ` 可用于放大到中等规模专项测试;`facet_count` 必须大于 16 才能触发 BVH 路径。 第一版不要求: @@ -626,7 +672,7 @@ tests/reference - 接入 CI。 - 增加 Windows/Linux 自动编译。 - 增加 C ABI 和 Python 接口测试。 -- 增加 VTK/restart 回归测试。 +- 增加 VTK/restart 回归测试;V4.3 已覆盖接触摩擦历史 restart 回归。 第三版: @@ -634,7 +680,9 @@ tests/reference - CI 增加壳静力小型基准测试。 - 本地发布前运行 Scordelis-Lo 和接触 P2 测试。 - 保留 Windows GCC、WSL gfortran 和 Intel Fortran 兼容性检查记录。 -- 当前本地记录:Windows GCC 16 CTest 33/33,WSL Ubuntu + gfortran 13.3 CTest 33/33,Windows Intel oneAPI `ifx` 2026.0 + MSVC `cl` 19.44 CTest 33/33;Intel/NMake `/machine:x64` ifx warning 已清理。 +- 0.4.2 本地发布记录:Windows GCC 16 CTest 33/33,WSL Ubuntu + gfortran 13.3 CTest 33/33,Windows Intel oneAPI `ifx` 2026.0 + MSVC `cl` 19.44 CTest 33/33;Intel/NMake `/machine:x64` ifx warning 已清理。 +- 0.4.3 当前 Windows GCC 16 回归记录:CTest 34/34。 +- V4.4 开发态新增 `contact_omp1` / `contact_omp4`,用 `OMP_NUM_THREADS=1/4` 重跑 contact 模块回归;新增 `perf_contact_pair_smoke` 和 `perf_contact_pair_medium` 覆盖 BVH contact pair 性能 smoke;新增 `perf_contact_pair_omp_consistency` 对比性能 smoke 在 OpenMP 1/4 线程下的非计时统计;contact CSV 已覆盖 summary 和逐 pair 统计;BVH force 权重准备统计改为 OpenMP reduction 后由 contact 定向回归覆盖;BVH force 并行装配已将法向/mortar/接触面积准备移出全局提交临界区,BVH tangent 并行装配已将 mortar/切线块准备移出 CSR 临界区;`contact` 覆盖跨调用候选缓存 force/tangent 命中和失效条件;Python 可用时 Windows GCC 16 CTest 当前为 39 项。 ## 21. 发布阻断条件 @@ -679,3 +727,16 @@ tests/reference - 简化接触静力和动力算例通过。 - 静力 VTK、CSV、反力和收敛历史输出字段完整。 - Windows 和 Linux 均可编译并通过第三版 P0 测试。 + +## 24. 第四版 V4.2 阶段验收准则 + +V4.2 阶段完成条件: + +- V4.1 的 surface/contact 输入增强无回退。 +- contact pair active set 可区分 open、activated、maintained 和 released。 +- augmented penalty 参数可由 contact property 或 contact pair 输入,并按 pair 级优先级继承。 +- 刚性平面 analytical contact pair 和面-面 contact pair 均使用统一的 penalty + augmented multiplier 法向压力路径。 +- contact CSV 输出包含状态、激活/释放次数、gap residual、augment factor 和 augmented multiplier。 +- Fortran API 和 C ABI 可查询接触诊断统计。 +- rigid plane indentation、two blocks compression 和 shell rigid plane contact 三个小算例可运行并生成 VTK 与接触结果。 +- CTest 全量回归通过,`source_size` 无 hard-threshold 失败。 diff --git a/docs/07_user_manual.md b/docs/07_user_manual.md index 4f66879..a3d764c 100644 --- a/docs/07_user_manual.md +++ b/docs/07_user_manual.md @@ -1,12 +1,12 @@ -# FemLite 0.3.3 用户手册 +# FemLite 0.4.3 用户手册 日期:2026-06-24 ## 1. 适用范围 -本文档面向 FemLite 0.3.3 的使用者,说明如何编译、运行、准备输入文件、查看结果和复现实例。 +本文档面向 FemLite 0.4.3 的使用者,说明如何编译、运行、准备输入文件、查看结果和复现实例。 -FemLite 0.3.3 是船舶接触爆炸结构响应 FEM 软件的第三版阶段验收版,当前支持: +FemLite 0.4.3 是船舶接触爆炸结构响应 FEM 软件的第四版接触增强阶段版,当前支持: - 三维实体和壳单元结构分析。 - 显式瞬态动力学。 @@ -14,11 +14,12 @@ FemLite 0.3.3 是船舶接触爆炸结构响应 FEM 软件的第三版阶段验 - 隐式 Newmark 和非线性 Newmark-Newton 主流程。 - 各向同性线弹性、J2 弹塑性、Johnson-Cook 相关材料路径。 - Abaqus inp 输入子集。 -- 刚性平面接触和面-面 contact pair 阶段能力。 +- element/node/analytical surface 子集、contact property 子集、刚性平面接触和面-面 contact pair 阶段能力。 +- contact pair active set、gap residual 和 augmented penalty 诊断输出。 - CSV、VTK、restart 和 contact CSV 输出。 - Fortran95 原生接口、C ABI 子集和 Python 调试脚本。 -0.3.3 不等价于完整 Abaqus 兼容求解器,也不等价于工程级通用 mortar/摩擦接触最终版。 +0.4.3 不等价于完整 Abaqus 兼容求解器,也不等价于工程级通用 mortar/摩擦接触最终版。 ## 2. 目录概览 @@ -84,7 +85,7 @@ wsl bash -lc "cd /mnt/d/Codex/FemLite && cmake --build build-wsl" ### 4.3 Intel oneAPI ifx -Intel oneAPI 在 Windows 上安装后,可通过 oneAPI 和 MSVC 环境脚本配置编译环境,再运行 CMake 构建。当前 0.3.3 已通过 Windows Intel oneAPI ifx 2026.0 + MSVC cl 19.44 构建与 CTest。 +Intel oneAPI 在 Windows 上安装后,可通过 oneAPI 和 MSVC 环境脚本配置编译环境,再运行 CMake 构建。0.3.3 发布基线已通过 Windows Intel oneAPI ifx 2026.0 + MSVC cl 19.44 构建与 CTest;0.4.x 发布前建议按同一流程复测。 ## 5. 命令行用法 @@ -126,7 +127,7 @@ build-win-gcc-16\femlite.exe examples\rigid_sphere_impact_plate\rigid_sphere_imp ## 6. 输入文件 -FemLite 使用 Abaqus inp 风格输入文件的子集。0.3.3 常用支持项包括: +FemLite 使用 Abaqus inp 风格输入文件的子集。0.4.3 常用支持项包括: - `*NODE` - `*ELEMENT` @@ -146,6 +147,9 @@ FemLite 使用 Abaqus inp 风格输入文件的子集。0.3.3 常用支持项包 - `*DYNAMIC` - `*STATIC` - `*SURFACE` +- `*SURFACE INTERACTION` +- `*CONTACT PROPERTY` +- `*FRICTION` - `*CONTACT PAIR` - `GENERATE` @@ -183,26 +187,32 @@ contact pair 支持两种输入方式。 header 式: ```inp -*CONTACT PAIR, SLAVE=slave_target, MASTER=master_target, PENALTY=value, DAMPING=value, SEARCH=value, FRICTION=value +*CONTACT PAIR, SLAVE=slave_target, MASTER=master_target, PENALTY=value, DAMPING=value, SEARCH=value, FRICTION=value, FRICTION_REGULARIZATION=value, AUGMENT=value ``` -数据行式: +contact property 继承式: ```inp *SURFACE, NAME=slave_surface, TYPE=ELEMENT slave_elset_or_element, face_label *SURFACE, NAME=master_surface, TYPE=ELEMENT master_elset_or_element, face_label -*CONTACT PAIR, PENALTY=value, DAMPING=value, SEARCH=value, FRICTION=value +*SURFACE INTERACTION, NAME=normal_contact, PENALTY=value, DAMPING=value, SEARCH=value, FRICTION_REGULARIZATION=value, AUGMENT=value +*FRICTION +mu +*CONTACT PAIR, INTERACTION=normal_contact slave_surface, master_surface ``` 说明: -- `PENALTY` 必填。 -- `DAMPING`、`SEARCH`、`FRICTION` 可选,默认 0。 +- `PENALTY` 可在 contact pair 或 contact property 中给出,最终值必须大于 0。 +- `DAMPING`、`SEARCH`、`FRICTION`、`FRICTION_REGULARIZATION`、`AUGMENT` 可选,默认 0。 +- `FRICTION_REGULARIZATION` 是接触对级摩擦正则化滑移尺度;0 表示经典 Coulomb 截断。 - `SLAVE` 和 `MASTER` 可为 surface 名称、elset 名称、element id 字符串或 `ALL`。 -- `*SURFACE` 当前支持 `TYPE=ELEMENT` 子集。 +- `*SURFACE` 当前支持 `TYPE=ELEMENT`、`TYPE=NODE` 和 `TYPE=ANALYTICAL` 刚性平面子集。 +- 面-面 contact pair 当前仍以 element surface facet 路径为主;node surface 和 analytical plane 主要用于节点目标/刚性平面接触子集。 +- analytical plane 数据行为 `PLANE, nx, ny, nz, offset`。 - face label 支持省略、`ALL`、`EXTERIOR`、`S1`、`F1`、`FACE1` 等形式。 - 壳单元支持 `SPOS`/`SNEG` 当前单一 shell facet 语义。 @@ -223,9 +233,11 @@ slave_surface, master_surface `contact_XXXXXXXX.csv` 当前字段包含: ```text -kind,pair_id,active_count,max_penetration,force_norm,work_estimate,cumulative_work,stick_count,slip_count,accumulated_slip,ftx,fty,ftz +kind,pair_id,active_count,max_penetration,force_norm,work_estimate,cumulative_work,stick_count,slip_count,accumulated_slip,friction_work,friction_work_total,friction_regularization,ftx,fty,ftz,status,activation_count,release_count,gap_residual,aug_factor,aug_lambda,search_facets,search_masters,search_candidates,search_active,search_bvh_nodes,search_cache_used,search_cache_overflow,search_cpu_time,contact_force_cpu_time,contact_tangent_cpu_time ``` +`search_*` 字段在 `SUMMARY` 行给出最近一次 contact pair 力评估的全局搜索统计、BVH 候选缓存使用情况和 `cpu_time` 口径秒数;逐 `PAIR` 行给出对应接触对的搜索、缓存和计时统计。`cpu_time` 是轻量趋势诊断,小算例中可能显示为 0。 + 建议把大型结果输出到 `examples/.../results/...` 或独立 `output/` 目录。仓库 `.gitignore` 默认忽略大规模结果文件。 ## 10. 示例算例 @@ -241,8 +253,12 @@ kind,pair_id,active_count,max_penetration,force_norm,work_estimate,cumulative_wo | `examples/shell_patch_test` | 壳单元 patch test | | `examples/clamped_square_plate_pressure_pulse` | 固支方板压力脉冲 | | `examples/rigid_sphere_impact_plate` | 刚球冲击板壳接触算例 | +| `examples/contact_rigid_plane_indentation` | 刚性平面压入接触算例 | +| `examples/contact_two_blocks_compression` | 两实体块面-面压缩接触算例 | +| `examples/shell_rigid_plane_contact` | S4 壳-刚性平面接触算例 | +| `examples/contact_block_sliding` | 两实体块正则化摩擦滑移接触算例 | -每个示例目录中的 `README.md` 给出几何、材料、边界、生成脚本和运行命令。 +每个示例目录中的 `README.md` 给出几何、材料、边界、生成脚本和运行命令。第四版新增接触示例提供 `run_case.bat`,可在 Windows 下双击自动运行并检查结果。 ## 11. 测试 @@ -258,11 +274,11 @@ ctest --test-dir build-win-gcc-16 --output-on-failure ctest --test-dir build-win-gcc-16 -R "^(contact|implicit_inp|source_size)$" --output-on-failure ``` -0.3.3 本地记录: +0.4.3 本地记录: -- Windows GCC 16:33/33 passed。 -- WSL Ubuntu + gfortran 13.3:33/33 passed。 -- Windows Intel oneAPI ifx 2026.0 + MSVC:33/33 passed。 +- Windows GCC 16:34/34 passed。 +- WSL Ubuntu + gfortran 13.3:沿用第三版发布流程复测。 +- Windows Intel oneAPI ifx 2026.0 + MSVC:沿用第三版发布流程复测。 ## 12. 常见问题 @@ -284,7 +300,7 @@ ctest --test-dir build-win-gcc-16 -R "^(contact|implicit_inp|source_size)$" --ou ## 13. 当前限制 - 不完整兼容 Abaqus 全部 keyword。 -- `*SURFACE` 是 element-target 子集,尚不支持完整 node-based/analytic rigid surface。 -- 面-面接触已具备 mortar 权重、摩擦历史和 BVH 搜索,但仍不是完整工程级 mortar 约束求解器。 +- `*SURFACE` 已支持 element/node/analytical plane 子集,但尚不支持完整 Abaqus surface 语义。 +- 面-面接触已具备 mortar 权重、mortar 积分点摩擦历史、正则化摩擦、摩擦功输出、BVH 搜索、active set 和 pair 级 augmented penalty 诊断,但仍不是完整工程级 mortar 约束求解器。 - 非线性 Newmark-Newton 和复杂壳塑性仍需要更多工程基准和长时间稳定性验证。 - 大型工程算例的并行、内存和接触收敛策略仍需后续版本完善。 diff --git a/docs/08_developer_manual.md b/docs/08_developer_manual.md index 45cef11..cf858ba 100644 --- a/docs/08_developer_manual.md +++ b/docs/08_developer_manual.md @@ -1,4 +1,4 @@ -# FemLite 0.3.3 开发者手册 +# FemLite 0.4.3 开发者手册 日期:2026-06-24 @@ -6,7 +6,7 @@ 本文档面向 FemLite 开发者,说明代码结构、构建测试、模块职责、接口扩展、编码约束和发布检查流程。 -FemLite 核心代码采用 Fortran 95 非面向对象风格,使用集中式 `fem_database` 保存模型、状态和求解数据。当前版本为 0.3.3。 +FemLite 核心代码采用 Fortran 95 非面向对象风格,使用集中式 `fem_database` 保存模型、状态和求解数据。当前版本为 0.4.3。 ## 2. 开发原则 @@ -28,7 +28,8 @@ CMake 主要目标: | `femlite_c` | 静态库 | C ABI 包装库 | | `femlite` | 可执行程序 | 命令行入口 | | `test_*` | 可执行程序 | 单元、模块和接口测试 | -| `perf_p1` | 可执行程序 | P1 性能测试 | +| `perf_p1` | 可执行程序 | P1 显式动力性能测试 | +| `perf_contact_pair` | 可执行程序 | contact pair BVH/缓存性能测试 | 常用构建: @@ -167,11 +168,13 @@ INP 相关文件: ```text fem_contact.f90 公开入口、刚性平面 penalty 接触 -fem_contact_state.f90 接触统计清零和累积 +fem_contact_state.f90 接触统计、active set、gap residual 和 augmented penalty 状态 fem_contact_target.f90 目标匹配、shared-node、pair 参数 fem_contact_geometry.f90 facet 几何、投影裁剪、mortar 权重 fem_contact_search.f90 空间网格、BVH、候选 master facet -fem_contact_friction.f90 Coulomb 粘滑历史 +fem_contact_candidate_cache.f90 跨调用 broad candidate 缓存 +fem_contact_history.f90 mortar 积分点摩擦历史槽 +fem_contact_friction.f90 Coulomb/正则化 Coulomb 粘滑历史和摩擦功 fem_contact_force.f90 contact pair 力路径 fem_contact_tangent.f90 contact pair CSR 切线装配 fem_contact_output.f90 contact CSV 字段辅助 @@ -184,6 +187,13 @@ fem_contact_output.f90 contact CSV 字段辅助 - 法向/切向力分配放在 `fem_contact_force`。 - 切线矩阵装配放在 `fem_contact_tangent`。 - 统计和输出字段放在 `fem_contact_state` 和 `fem_contact_output`。 +- 接触搜索统计由 `fem_contact_state` 管理,力路径第一遍候选搜索负责记录;切线路径复用 broad candidate 缓存或搜索 helper 时不重复累计全局搜索统计。 +- 接触性能计时由 `fem_contact_state` 管理,当前使用 Fortran `cpu_time`,用于趋势诊断和性能回归,不作为跨平台精确定时基准。 +- BVH 力路径使用固定容量的 slave facet 局部 active 候选缓存;缓存溢出时必须回退旧 BVH 遍历施力路径,保证结果不依赖缓存容量。 +- `fem_contact_candidate_cache` 保存跨调用 broad candidate 列表;force/tangent 缓存命中只跳过 BVH 构建,仍必须重新计算当前 gap、法向速度和 active 状态。 +- 跨调用候选缓存必须在数据库 finalize、contact pair 重分配、facet 拓扑变化、pair 名称/search 参数变化、候选列表溢出或 facet 运动超过失效阈值时失效。 +- BVH cached force 路径应先在线程本地准备法向、mortar 权重、接触面积和切向方向,只在 `femlite_contact_force` 临界区内提交摩擦历史、全局力向量、切向历史和 active 状态;BVH force 权重准备阶段的 broad/active 统计使用 OpenMP reduction 在线程内汇总,退出并行区后一次写入全局与 pair 级统计。继续降低锁竞争时,必须保持力向量、接触历史、active 状态和切线近似的一致更新边界。 +- BVH 切线路径复用跨调用 broad candidate 缓存和固定容量的 slave facet 局部 active 候选缓存;并行路径应先在线程本地准备法向、mortar 权重和切线块,只在 CSR 合并时进入 `femlite_contact_tangent` 临界区。缓存溢出时必须回退 cached broad candidate 遍历或旧 BVH 遍历切线路径。 新增 contact pair 行为应覆盖: @@ -191,6 +201,11 @@ fem_contact_output.f90 contact CSV 字段辅助 - uniform grid 路径。 - BVH 路径。 - force 和 tangent 路径。 +- active set、gap residual、augmented penalty 乘子和 release 清零路径。 +- 搜索统计的串行、uniform grid、BVH 路径。 +- search/force/tangent CPU 时间统计和 contact CSV schema。 +- BVH 候选缓存命中、溢出和回退路径。 +- 跨调用 broad candidate 缓存的 force/tangent 命中和失效路径。 - contact CSV 输出。 - 输入解析。 @@ -224,7 +239,7 @@ fem_contact_output.f90 contact CSV 字段辅助 - `fem_output_csv` 写 CSV。 - `fem_output_vtk` 写 legacy ASCII VTK。 -- `fem_restart` 写 restart。 +- `fem_restart` 写 restart;V4.3 restart v2 额外保存 mortar 积分点摩擦历史槽。 新增输出字段时: @@ -233,6 +248,12 @@ fem_contact_output.f90 contact CSV 字段辅助 3. 增加 schema 或 smoke 测试。 4. 避免把大型输出文件提交到 git。 +新增跨时间步状态时: + +1. 明确该状态是否影响继续计算。 +2. 若影响继续计算,同步更新 `fem_restart` 写读和 restart 回归。 +3. 若暂不支持重启,必须在接口文档和当前限制中说明。 + ## 11. Fortran API 公共 Fortran API 在 `src/base/fem_api.f90` 中。常用类别: @@ -242,7 +263,7 @@ fem_contact_output.f90 contact CSV 字段辅助 - 显式:`fem_advance_explicit`、`fem_run_explicit`。 - 静力/隐式:`fem_solve_static_linear`、`fem_solve_static_nonlinear`、`fem_run_implicit_newmark`、`fem_run_newmark_nonlinear`。 - SPH 耦合:`fem_clear_sph_forces`、`fem_apply_sph_nodal_forces`、`fem_get_boundary_facets`、`fem_apply_sph_facet_pressure`、`fem_apply_sph_facet_tractions`。 -- 查询:模型规模、当前时间、节点运动学、反力、单元状态、接触 summary。 +- 查询:模型规模、当前时间、节点运动学、反力、单元状态、接触 summary、contact diagnostics、contact friction summary 和 contact history stats。 - 输出:`fem_write_output`、`fem_write_restart`、`fem_read_restart`。 新增 API 时: @@ -260,7 +281,7 @@ C ABI: - 实现:`src/interface/fem_c_api.c`。 - Fortran-C bridge:`src/interface/fem_fortran_bridge.f90`。 -C ABI 当前暴露初始化、读取、构建、显式推进、SPH 力、facet 查询、节点运动学、contact summary 和 restart 子集。 +C ABI 当前暴露初始化、读取、构建、显式推进、SPH 力、facet 查询、节点运动学、contact summary、contact diagnostics、contact friction summary、contact history stats 和 restart 子集。 Python 调试接口: @@ -279,7 +300,7 @@ ctest --test-dir build-win-gcc-16 --output-on-failure 常用定向测试: ```powershell -ctest --test-dir build-win-gcc-16 -R "^(contact|implicit_inp|source_size)$" --output-on-failure +ctest --test-dir build-win-gcc-16 -R "^(contact|contact_omp1|contact_omp4|restart|c_api|implicit_inp|source_size)$" --output-on-failure ``` 测试分层: @@ -289,9 +310,9 @@ ctest --test-dir build-win-gcc-16 -R "^(contact|implicit_inp|source_size)$" --ou - 接口测试:C ABI、Python debug。 - CLI 测试:静力、隐式 Newmark 等命令行入口。 - 示例 smoke:shell patch、pressure pulse、rigid sphere impact。 -- 性能测试:`perf_p1`。 +- 性能测试:`perf_p1`、`perf_contact_pair`、`perf_contact_pair_omp_consistency`。 -0.3.3 当前 CTest 包含 33 项。 +0.4.3 基线 CTest 包含 34 项;V4.4 开发态加入 `contact_omp1`、`contact_omp4`、`perf_contact_pair_smoke`、`perf_contact_pair_medium` 和 `perf_contact_pair_omp_consistency` 后,在 Python 可用时包含 39 项。 ## 14. 源码规模门禁 @@ -309,7 +330,7 @@ python tests\scripts\check_source_size.py . --warn-file-lines 1500 --max-file-li - 超过 2000 行必须登记 hard-threshold exception。 - stale exception 会导致测试失败。 -0.3.3 当前无 active hard-threshold exception;软警告不阻断发布。 +0.4.3 当前无 active hard-threshold exception;软警告不阻断发布。 ## 15. 编码规范 @@ -335,6 +356,7 @@ python tests\scripts\check_source_size.py . --warn-file-lines 1500 --max-file-li - 网格/输入生成脚本。 - `.inp` 输入文件。 - 可选结果检查脚本。 +- Windows 双击自动运行脚本可命名为 `run_case.bat`;脚本应把结果写入示例目录下的 `results` 子目录,并支持自动检查失败码。 示例目录不应提交: @@ -361,15 +383,15 @@ python tests\scripts\check_source_size.py . --warn-file-lines 1500 --max-file-li 8. 更新 `docs/v*_acceptance_report.md` 和相关接口/测试文档。 9. 提交 git,必要时打 tag 并推送。 -0.3.3 发布基线记录见 `docs/v3_acceptance_report.md`。 +0.3.3 发布基线记录见 `docs/v3_acceptance_report.md`。0.4.x 阶段开发和阶段记录见 `docs/v4_development_plan.md`。 ## 18. 后续开发重点 建议后续版本重点推进: - 完整 Abaqus surface 语义。 -- node-based surface 和 analytic rigid surface。 -- 工程级 mortar 约束求解器。 +- 更完整的 analytical rigid surface 和 node-surface contact pair 路径。 +- 完整工程级 mortar/augmented Lagrangian 约束求解器。 - 摩擦正则化参数体系。 - 大规模接触收敛控制。 - 更多壳塑性和隐式动力长跑基准。 diff --git a/docs/v4_development_plan.md b/docs/v4_development_plan.md index 0a1db55..b2ac627 100644 --- a/docs/v4_development_plan.md +++ b/docs/v4_development_plan.md @@ -59,7 +59,7 @@ FemLite 0.4.x 定位为: - `*SURFACE` 当前仍为 element-target 子集,尚未支持完整 Abaqus surface 变体、node-based surface、analytic rigid surface 和复杂 surface 操作。 - 面-面 contact pair 已具备 mortar 权重、摩擦历史和 BVH 搜索,但仍不是完整工程级 mortar 约束求解器。 -- 摩擦模型当前以接触对级 Coulomb 粘滑历史为主,尚未形成接触积分点级历史和摩擦正则化参数体系。 +- 摩擦模型当前已具备 mortar 积分点级 Coulomb/正则化 Coulomb 粘滑历史、状态统计、摩擦功输出和 restart 保存。 - 大规模接触收敛控制、接触域分解和负载均衡仍需推进。 - 壳塑性一致切线仍需要更多弯曲/剪切塑性基准和长时间稳定性验证。 - 非线性 Newmark-Newton 仍需要更多工程基准、长时间稳定性验证和线性求解器鲁棒性增强。 @@ -82,7 +82,7 @@ FemLite 0.4.x 定位为: - `0.4.1`:V4.1 surface/contact 输入增强完成,包含 node-based surface、contact property 和 analytical rigid plane contact 子集。 - `0.4.2`:工程级 mortar/augmented penalty 接触框架增强。 - `0.4.3`:摩擦正则化、接触积分点历史和接触功输出增强。 -- `0.4.4`:隐式非线性长跑、步长控制和外部稀疏求解器接口。 +- `0.4.4`:搜索与并行增强,包含 contact pair 搜索统计、BVH/候选缓存和并行装配优化。 - `0.4.5`:壳塑性基准、性能优化、多平台发布整理。 发布前必须更新: @@ -545,7 +545,7 @@ FemLite 0.4.x 定位为: | 未支持 INP keyword | 未支持 keyword warning 跳过,关键数据缺失才 error | 不删除 | 保持兼容策略,但为 contact/surface 关键字建立更细的 warning/error 分级 | | 刚性平面接触 | 节点到刚性平面的 penalty/damping 法向接触 | 保留 | 作为接触 fallback 和调试基准,补充 active set 输出和接触功一致性检查 | | 面-面 contact pair | mortar 权重、投影裁剪、多 master 分摊、penalty 法向力和近似切线 | 不删除 | V4.2 升级为 active set + augmented penalty/mortar 残差和更一致切线 | -| 摩擦历史 | 接触对级 Coulomb 粘滑历史,切向刚度按 `penalty * area` 估计 | 不删除 | V4.3 升级为接触积分点或 mortar 积分点历史、正则化 Coulomb 和摩擦功输出 | +| 摩擦历史 | mortar 积分点级 Coulomb/正则化 Coulomb 粘滑历史,切向刚度按 `penalty * area` 估计 | 不删除 | V4.3 已完成阶段目标;V4.4/V4.5 可继续增强并行一致性和算法一致切线 | | 接触并行 | BVH 搜索可 OpenMP 并行,但力/切线装配仍依赖 critical 保护 | 不删除 | V4.4 降低锁竞争,增加候选缓存、并行一致性测试和性能统计 | | 壳 drilling 稳定 | 使用固定 `SHELL_DRILLING_STIFFNESS_FACTOR = 1.0e-4` | 不删除 | V4.7 参数化并增加敏感性测试,形成推荐范围和失稳诊断 | | S3 剪切锁定处理 | 使用经验型 `shell3_shear_locking_scale` | 不删除 | V4.7 通过基准算例验证,必要时替换为更系统的 MITC/ANS 或改进减缩策略 | @@ -555,7 +555,7 @@ FemLite 0.4.x 定位为: | 线性求解器 | 内部 CG + Jacobi 预条件为主 | 保留 | V4.6 增加求解器抽象、BiCGSTAB/GMRES、预条件和外部稀疏求解器预留 | | MPC | MPC 当前采用 penalty 装配 | 保留 | 非 V4 主线;接触/隐式增强后再考虑 Lagrange multiplier 或更严格约束形式 | | 材料大变形 | 当前为小应变 J2/J2_JC + Jaumann 客观率的 hypoelastic-plastic 路径 | 保留 | 第四版主要验证其在接触/隐式中的稳定性;完整 `F = Fe Fp` 可列入后续版本 | -| Restart | restart 保存已建模型上的 DOF、单元状态和高斯点状态,不保存完整 inp 模型数据库 | 保留 | 如 V4 新增接触积分点历史,restart 必须同步保存新增历史变量 | +| Restart | restart 保存已建模型上的 DOF、单元状态、高斯点状态和 V4.3 mortar 积分点摩擦历史,不保存完整 inp 模型数据库 | 保留 | 后续新增接触或求解器历史时,restart 必须同步保存新增历史变量 | | 输出 | CSV/VTK 为工程调试级输出,单元量多为平均值 | 保留 | V4 增加 contact/solver 统计和关键积分点历史输出,避免提交大规模全场结果 | 第四版启动前建议先建立对应测试保护: @@ -585,7 +585,7 @@ FemLite 0.4.x 定位为: | S2 | unsupported INP keyword warning 跳过 | B 类。建立 contact/surface 关键字 warning/error 分级表,关键数据缺失保持 error | V4.1 | | S3 | 刚性平面 penalty 接触 | B 类。保留为 fallback,补充 active set 统计、接触功一致性、接触状态输出和回归测试 | V4.2, V4.3 | | S4 | 面-面 penalty/mortar 近似接触 | A 类。升级为 active set + augmented penalty/mortar 残差 + 更一致切线;旧 penalty/mortar 路径保留为调试 fallback | V4.2 | -| S5 | contact-pair 级摩擦历史 | A 类。升级到接触积分点或 mortar 积分点历史,补充正则化 Coulomb、粘滑切换和摩擦功输出 | V4.3 | +| S5 | contact-pair 级摩擦历史 | DONE。已推进到 mortar 积分点级历史,补充粘滑状态、正则化摩擦、摩擦功输出和 restart 保存 | V4.3 | | S6 | BVH/OpenMP 装配依赖 critical | A 类。降低锁竞争,增加候选缓存、线程私有累加或分块装配,并建立串并行一致性测试 | V4.4 | | S7 | 固定 drilling 稳定系数 | B 类。改为可配置参数,增加默认推荐值、敏感性测试、失稳诊断和用户手册说明 | V4.7 | | S8 | S3 剪切锁定经验缩放 | B 类。通过壳基准验证误差;必要时增加改进减缩、MITC/ANS 预研路径或更明确适用范围 | V4.7 | @@ -595,7 +595,7 @@ FemLite 0.4.x 定位为: | S12 | CG + Jacobi 单一路径 | A 类。建立线性求解器抽象,增加 BiCGSTAB/GMRES 或等价内部求解器、预条件接口和外部稀疏求解器预留 | V4.6 | | S13 | MPC penalty 约束 | C 类。0.4.x 保留 penalty MPC,补充病态诊断和回归;严格 Lagrange multiplier 或 multiplier-augmented MPC 进入后续 | V4.5 | | S14 | 小应变 J2/J2_JC + Jaumann 客观率 | C 类。0.4.x 重点做接触/隐式稳定性验证和文档边界;完整 `F = Fe Fp` 大变形塑性列入 0.5.0 或后续 | V4.5, V4.7 | -| S15 | restart 不保存完整模型数据库 | B 类。0.4.x 同步保存新增接触历史、摩擦历史和求解状态;完整 inp 数据库级 restart 作为后续增强 | V4.3, V4.8 | +| S15 | restart 不保存完整模型数据库 | B 类。V4.3 已同步保存 mortar 积分点摩擦历史;0.4.x 后续继续同步新增求解状态,完整 inp 数据库级 restart 作为后续增强 | V4.3, V4.8 | | S16 | CSV/VTK 调试级输出和单元平均量 | B 类。增加 contact/solver 统计、关键积分点历史抽样和输出体量控制;全量高性能后处理格式后续推进 | V4.3, V4.8 | 0.4.x 验收时,上述列表应逐项给出状态: @@ -642,3 +642,133 @@ FemLite 0.4.x 定位为: - 在节点-面接触或后续 contact pair 扩展中接入 node-based slave surface。 - 扩展 analytical surface 到更完整的 Abaqus segments/cylinder/sphere 子集。 + +## 23. 0.4.2 开发记录 + +### V4.2 Active Set 与 Augmented Penalty 起步 + +完成状态:V4.2 阶段完成,可作为 FemLite 0.4.2 候选基线;完整工程级 augmented Lagrangian/mortar 约束求解器仍进入后续 0.4.x。 + +已完成: + +- contact pair 数据库新增增广 penalty 系数、法向增广乘子、显式设置标记、上一状态 active 数、当前状态、激活次数、释放次数和 gap 残差。 +- `*CONTACT PAIR` 支持 `AUGMENT=` / `AUGMENT_FACTOR=` pair 级参数。 +- `*SURFACE INTERACTION` / `*CONTACT PROPERTY` 支持 `AUGMENT=` / `AUGMENT_FACTOR=`,并可继承到 contact pair。 +- 刚性平面 analytical contact pair 和面-面 contact pair 的法向压力统一经过 `penalty * penetration + lambda` 路径。 +- 接触力计算后统一 finalize active-set 状态,区分 open、activated、maintained、released。 +- active pair 按当前 gap 残差更新增广乘子,released pair 自动清零增广乘子。 +- contact CSV 追加输出 pair 状态、激活/释放次数、gap 残差、增广系数和增广乘子。 +- 新增 Fortran API `fem_get_contact_diagnostics`,可查询 active pair 数、activated/maintained/released pair 数、最大 gap residual 和最大 augmented penalty 乘子。 +- C ABI 追加 `femlite_get_contact_diagnostics`,保持 `femlite_get_contact_summary` 向后兼容。 +- `examples/` 下新增刚性平面压入、两实体块面-面压缩、S4 壳-刚性平面接触三个独立小算例目录。 +- 每个新增接触算例目录提供 `run_case.bat`,支持 Windows 双击自动运行并检查结果。 + +已补充测试: + +- analytical rigid plane contact pair 的 active set 激活、保持、释放状态切换。 +- 增广乘子对第二次接触力的贡献和释放后的清零。 +- contact property 中 `AUGMENT` 参数解析和 pair 继承。 +- contact CSV header 回归覆盖 active-set/augmented penalty 诊断列。 +- Fortran API 和 C ABI 接触诊断查询回归。 +- 三个新增示例均已运行,检查脚本确认 active contact、gap residual、augmented multiplier 和 VTK 输出存在。 +- 三个 `run_case.bat` 已用 `FEMLITE_NO_PAUSE=1` 自动模式验证通过。 +- Windows GCC 16 构建和全量 CTest 已通过,33/33 passed。 + +后续: + +- 将 gap/mortar 残差接入非线性求解统计和收敛准则。 +- 增强 contact tangent 对 active set 状态和增广路径的诊断输出。 +- 将 pair 级 augmented penalty 乘子推进到 mortar 积分点级历史,并在后续 V4.x 中与摩擦历史统一调试输出。 + +## 24. 0.4.3 开发记录 + +### V4.3 摩擦正则化与摩擦功输出起步 + +完成状态:V4.3 阶段完成,可作为 FemLite 0.4.3 候选基线;完整工程级 mortar 约束求解器和更强并行接触装配仍进入后续 0.4.x。 + +已完成: + +- `*CONTACT PAIR` 和 `*SURFACE INTERACTION` / `*CONTACT PROPERTY` 支持 `FRICTION_REGULARIZATION=` / `FRICTION_REG=` 参数。 +- `*FRICTION` 数据行支持第二个可选值作为摩擦正则化滑移尺度。 +- contact pair 新增摩擦正则化滑移尺度、摩擦 step 功和摩擦累计功状态。 +- 默认 `FRICTION_REGULARIZATION=0` 时保持经典 Coulomb 截断;正则化大于 0 时使用 `tanh(s_eq/s_reg)` 平滑 Coulomb 极限。 +- 正则化摩擦切线使用相同 `tanh(s/s_reg)` 因子折减接触对级切向近似块,保持与力路径的参数趋势一致。 +- 新增 `fem_contact_history`,将切向摩擦历史从单一 contact pair 标量推进到 mortar 积分点槽,槽键为 `pair + slave element/face + master element/face + point_id`;数据库初始化和释放时清空历史,模型内跨时间步保留。 +- restart 文件格式升级到 v2,追加 `CONTACT_HISTORY` 段保存 mortar 积分点摩擦历史;读入端兼容旧 v1 文件,并在旧文件路径下清空当前接触历史。 +- contact CSV 和 history CSV 增加摩擦功、摩擦累计功和摩擦正则化参数字段。 +- 新增 Fortran API `fem_get_contact_friction` 和 C ABI `femlite_get_contact_friction_summary`。 +- 新增 Fortran API `fem_get_contact_hist_stats` 和 C ABI `femlite_get_contact_history_stats`,用于查询 mortar 积分点历史槽数量、单槽最大滑移、单槽最大摩擦功和单槽最大切向力。 +- `examples/contact_block_sliding/` 新增两块 C3D8 正则化摩擦滑移示例,包含 `run_case.bat` 和摩擦功检查脚本。 + +已补充测试: + +- 默认 Coulomb 摩擦力、滑移计数、累计滑移和摩擦功回归。 +- 正则化 Coulomb 摩擦力降低、切向近似切线折减、正摩擦耗散功和累计滑移回归。 +- mortar 积分点历史在连续模型运行中不会串状态,`contact` 回归覆盖数据库生命周期 reset。 +- mortar 积分点历史统计 API 回归覆盖非零历史槽和 C ABI 空模型零值。 +- `contact` 回归检查正则化摩擦会创建多个积分点历史槽,并验证这些槽处于 slip 状态。 +- `restart` 回归覆盖接触历史写读一致性,检查历史槽、累计滑移、摩擦功和切向力。 +- contact property 中 `FRICTION_REGULARIZATION` 参数解析和 pair 继承。 +- contact friction summary C ABI 空模型零值回归。 +- contact block sliding friction 示例已接入 `example_smoke`,短步数回归检查 active contact、正则化参数、累计滑移、切向力、摩擦功非负和 VTK 输出。 +- `contact`、`restart`、`c_api`、`implicit_inp`、`source_size` 和 `example_contact_block_sliding_smoke` 定向 CTest 已通过。 +- Windows GCC 16 全量 CTest 已通过,34/34 passed。 + +后续: + +- 继续完善 stick/slip 切换的更严格算法一致切线,并补充更多切线回归。 +- 在 V4.4 中降低 OpenMP 接触力/切线装配锁竞争,补充并行一致性测试。 +- 将 mortar 积分点历史暴露为可选调试输出,避免默认输出体量过大。 + +## 25. 0.4.4 开发记录 + +### V4.4 搜索统计与并行优化起步 + +完成状态:V4.4 起步,尚未作为发布版本;当前项目版本仍为 0.4.3。 + +已完成: + +- `fem_contact_state` 新增全局与逐 contact pair 搜索统计,记录 facet 数、master facet 数、候选评估数、active 候选数和 BVH 节点数。 +- `fem_contact_state` 新增 contact search/force/tangent CPU 时间统计,使用 Fortran `cpu_time` 作为轻量趋势诊断。 +- BVH contact pair 力路径新增 slave facet 局部 active 候选缓存,第一遍权重搜索保存 `master/gap/vn`,第二遍施力复用缓存;缓存溢出时自动回退旧 BVH 遍历施力路径。 +- BVH cached force 路径拆分为线程本地 `contact_prepare_force_state` 和短临界区 `contact_commit_force_state`;cached、cached broad overflow 和非缓存 BVH 回退路径均在 `critical` 外完成法向、mortar 权重、接触面积和切向方向准备,锁内只保留摩擦历史、全局力向量、切向历史和 active 状态提交。 +- BVH contact pair 切线路径复用同类 slave facet 局部 active 候选缓存,并按 slave facet 批量进入切线装配保护区;缓存溢出时回退旧 BVH 遍历切线路径。 +- BVH contact pair 切线并行装配拆分为线程本地 `contact_prepare_pair_tan` 和短临界区 CSR 合并;cached、cached broad overflow 和非缓存 BVH 回退路径均在 `critical` 外完成法向、mortar 权重和切线块计算。 +- 新增 `fem_contact_candidate_cache`,保存 BVH force/tangent 共享的跨调用 broad candidate 列表;当 facet 拓扑、pair 名称、search 距离一致且 facet 中心/半径变化未超过 `0.25 * search` 阈值时复用,命中后跳过 BVH 构建并重新计算当前 gap/vn/active 状态。 +- 候选缓存失效条件覆盖 contact pair 重分配、数据库 finalize、facet 拓扑变化、pair slave/master 名称变化、search 距离变化、候选列表溢出和 facet 运动超阈值。 +- `fem_contact_state` 新增 BVH 候选缓存使用和溢出统计。 +- 全搜索、uniform grid 和 BVH 力路径在第一遍候选评估中记录 broad/active 候选统计。 +- BVH 并行权重搜索改为局部候选计数后集中汇总,避免在热循环中直接写全局统计。 +- BVH force 权重准备阶段使用 OpenMP reduction 汇总 broad/active 统计,退出并行区后一次性写入全局与 pair 级统计,移除每个 slave facet 一次的统计临界区。 +- 新增 Fortran API `fem_get_contact_search_stats`。 +- 新增 Fortran API `fem_get_contact_perf_stats`。 +- 新增 Fortran API `fem_get_contact_cache_stats`。 +- C ABI 追加 `femlite_get_contact_search_stats`,保持既有 contact summary/diagnostics/friction/history 接口不变。 +- C ABI 追加 `femlite_get_contact_perf_stats`。 +- C ABI 追加 `femlite_get_contact_cache_stats`。 +- 新增 `perf_contact_pair` 性能程序,程序内生成 S4 master/slave 面对,输出 facet/master/candidate/active/BVH 节点、缓存命中和接触 search/force/tangent CPU 时间统计。 +- contact CSV 的 `SUMMARY` 行追加全局 `search_facets/search_masters/search_candidates/search_active/search_bvh_nodes`、`search_cache_used/search_cache_overflow` 和 `search_cpu_time/contact_force_cpu_time/contact_tangent_cpu_time` 字段;逐 `PAIR` 行通过 `contact_get_pair_stats` 写出对应接触对的搜索、缓存和计时统计。 + +已补充测试: + +- `contact` 回归检查实际 contact pair 小算例的 facet/master/broad/active 统计非零,非 BVH 小算例 BVH 节点数为 0。 +- `contact` 回归检查 contact CSV header 包含新增搜索统计和 CPU 时间统计字段。 +- `contact` 回归检查 `contact_get_pair_stats` 的逐 pair 统计,并确认 contact CSV 中存在 `PAIR` 行。 +- `contact` 回归检查 BVH 小算例 cache used 非零、cache overflow 为 0。 +- `contact` 回归检查 BVH 小算例第一次力计算建立跨调用候选缓存,第二次同几何力计算命中缓存且不重建 BVH,随后切线装配复用同一候选缓存,slave facet 移动超过失效阈值后缓存失效并重新构建 BVH。 +- 新增 `contact_omp1` 和 `contact_omp4` CTest 变体,分别以 `OMP_NUM_THREADS=1/4` 重跑 contact 模块,覆盖 OpenMP 串并行路径;contact 系列测试标记 `RUN_SERIAL`,避免固定 CSV 输出并行互踩。 +- 新增 `perf_contact_pair_smoke` 和 `perf_contact_pair_medium` CTest,分别以 32/64 对 S4 面片触发 BVH contact pair、active 接触、缓存命中和切线装配路径。 +- 新增 `perf_contact_pair_omp_consistency` CTest,通过 `tests/scripts/check_perf_contact_pair_omp.py` 对比 `perf_contact_pair` 在 `OMP_NUM_THREADS=1/4` 下的 active、broad/active 候选、BVH 节点和缓存命中统计,保护性能 smoke 的串并行一致性。 +- `c_api` 回归检查空接触模型搜索统计为全零。 +- `c_api` 回归检查空接触模型性能计时为全零。 +- `c_api` 回归检查空接触模型缓存统计为全零。 +- `source_size` 回归确认新增接口未触发源码尺寸硬阈值。 +- `contact`、`contact_omp1`、`contact_omp4`、`c_api`、`source_size`、`perf_contact_pair_smoke` 和 `example_contact_block_sliding_smoke` 定向 CTest 已通过;reduction 汇总后统计值保持回归一致,跨调用候选缓存 force/tangent 命中和失效回归通过。 +- `perf_contact_pair_smoke`、`perf_contact_pair_medium`、`perf_contact_pair_omp_consistency` 和 `source_size` 定向 CTest 已通过,确认新增性能一致性脚本、medium 性能 smoke 和源码规模检查可用。 +- Windows GCC 16 全量 CTest 已通过,39/39 passed。 +- Windows GCC 16 并行 CTest `-j 4` 已通过,37/37 passed;contact 系列 `RUN_SERIAL` 可避免固定 CSV 输出并行互踩。 + +后续: + +- 评估按 slave facet 的线程私有力增量和历史提交边界,进一步缩短 force 路径全局提交临界区。 +- 补充更大规模 OpenMP 性能测试,继续评估分块 CSR 累加或线程私有增量合并。 diff --git a/examples/contact_block_sliding/README.md b/examples/contact_block_sliding/README.md new file mode 100644 index 0000000..8425fe0 --- /dev/null +++ b/examples/contact_block_sliding/README.md @@ -0,0 +1,36 @@ +# contact_block_sliding + +Small V4.3 sliding-friction example for two C3D8 elastic blocks. + +Purpose: + +- Exercise face-to-face contact with Coulomb friction. +- Exercise `FRICTION_REGULARIZATION` inheritance from `*SURFACE INTERACTION`. +- Check contact CSV friction work, accumulated slip and tangential force output. +- Provide a compact sliding case for future contact history and tangent work. + +Model: + +- Two 0.10 m x 0.10 m x 0.05 m C3D8 blocks. +- The upper block starts with 0.001 m initial penetration into the lower block. +- The lower block is fixed, while the upper block is driven by a horizontal + nodal force and compressed by the prescribed top-face z displacement. +- Contact property: penalty = 1.0E7, damping = 1.0E2, friction = 0.30, + friction regularization = 1.0E-4, augment = 0.20. + +Run from the repository root: + +Double-click `run_case.bat` in this directory, or run manually: + +```powershell +New-Item -ItemType Directory -Force examples\contact_block_sliding\results | Out-Null +build-win-gcc-16\femlite.exe examples\contact_block_sliding\block_sliding_friction.inp --output-dir examples\contact_block_sliding\results --output-interval 10 --run +``` + +Check: + +```powershell +.\Python\.venv\Scripts\python.exe examples\contact_block_sliding\check_contact_results.py examples\contact_block_sliding\results +``` + +The double-click script writes repeatable output to `results\auto_run`. diff --git a/examples/contact_block_sliding/block_sliding_friction.inp b/examples/contact_block_sliding/block_sliding_friction.inp new file mode 100644 index 0000000..f4a32e7 --- /dev/null +++ b/examples/contact_block_sliding/block_sliding_friction.inp @@ -0,0 +1,57 @@ +*HEADING +FemLite V4.3 two C3D8 blocks sliding friction contact example +*NODE +1, -0.05, -0.05, -0.050 +2, 0.05, -0.05, -0.050 +3, 0.05, 0.05, -0.050 +4, -0.05, 0.05, -0.050 +5, -0.05, -0.05, 0.000 +6, 0.05, -0.05, 0.000 +7, 0.05, 0.05, 0.000 +8, -0.05, 0.05, 0.000 +9, -0.05, -0.05, -0.001 +10, 0.05, -0.05, -0.001 +11, 0.05, 0.05, -0.001 +12, -0.05, 0.05, -0.001 +13, -0.05, -0.05, 0.049 +14, 0.05, -0.05, 0.049 +15, 0.05, 0.05, 0.049 +16, -0.05, 0.05, 0.049 +*ELEMENT, TYPE=C3D8, ELSET=LOWER +1, 1, 2, 3, 4, 5, 6, 7, 8 +*ELEMENT, TYPE=C3D8, ELSET=UPPER +2, 9, 10, 11, 12, 13, 14, 15, 16 +*ELSET, ELSET=ALLSOLID +1, 2 +*NSET, NSET=LOWER_ALL +1, 2, 3, 4, 5, 6, 7, 8 +*NSET, NSET=UPPER_ALL +9, 10, 11, 12, 13, 14, 15, 16 +*NSET, NSET=UPPER_TOP +13, 14, 15, 16 +*MATERIAL, NAME=SOFT_SOLID +*DENSITY +1000.0 +*ELASTIC +1.0E7, 0.30 +*SOLID SECTION, ELSET=ALLSOLID, MATERIAL=SOFT_SOLID +*AMPLITUDE, NAME=RAMP +0.0, 0.0, 1.0E-3, 1.0 +*BOUNDARY +LOWER_ALL, 1, 3, 0.0 +UPPER_ALL, 2, 2, 0.0 +*BOUNDARY, AMPLITUDE=RAMP +UPPER_TOP, 3, 3, -2.0E-3 +*CLOAD, AMPLITUDE=RAMP +UPPER_ALL, 1, 50.0 +*SURFACE, NAME=LOWER_TOP, TYPE=ELEMENT +LOWER, S2 +*SURFACE, NAME=UPPER_BOTTOM, TYPE=ELEMENT +UPPER, S1 +*SURFACE INTERACTION, NAME=SLIDE_CONTACT, PENALTY=1.0E7, DAMPING=1.0E2, SEARCH=1.0E-2, AUGMENT=0.20, FRICTION=0.30, FRICTION_REGULARIZATION=1.0E-4 +*CONTACT PAIR, INTERACTION=SLIDE_CONTACT +UPPER_BOTTOM, LOWER_TOP +*STEP +*DYNAMIC +1.0E-5, 1.0E-3 +*END STEP diff --git a/examples/contact_block_sliding/check_contact_results.py b/examples/contact_block_sliding/check_contact_results.py new file mode 100644 index 0000000..70339d7 --- /dev/null +++ b/examples/contact_block_sliding/check_contact_results.py @@ -0,0 +1,56 @@ +from __future__ import annotations + +import csv +import glob +import os +import sys + + +def read_pair_rows(result_dir: str) -> list[dict[str, str]]: + rows: list[dict[str, str]] = [] + for path in sorted(glob.glob(os.path.join(result_dir, "contact_*.csv"))): + with open(path, newline="") as stream: + rows.extend(row for row in csv.DictReader(stream) if row["kind"] == "PAIR") + return rows + + +def main() -> int: + result_dir = sys.argv[1] if len(sys.argv) > 1 else "results" + pair_rows = read_pair_rows(result_dir) + if not pair_rows: + print("no contact pair rows found") + return 1 + + max_active = max(int(float(row["active_count"])) for row in pair_rows) + max_slip = max(float(row["accumulated_slip"]) for row in pair_rows) + max_fric_work = max(float(row["friction_work_total"]) for row in pair_rows) + min_fric_work = min(float(row["friction_work_total"]) for row in pair_rows) + max_ft = max(abs(float(row["ftx"])) for row in pair_rows) + max_reg = max(float(row["friction_regularization"]) for row in pair_rows) + + if max_active <= 0: + print("sliding contact did not activate") + return 1 + if max_reg <= 0.0: + print("friction regularization was not reported") + return 1 + if max_slip <= 0.0 or max_ft <= 0.0: + print("frictional sliding response was not detected") + return 1 + if min_fric_work < -1.0e-12 or max_fric_work <= 0.0: + print("friction work is not positive dissipative") + return 1 + if not glob.glob(os.path.join(result_dir, "result_*.vtk")): + print("no VTK output found") + return 1 + + print( + "ok: max_active={}, max_slip={:.6e}, max_ft={:.6e}, max_friction_work={:.6e}".format( + max_active, max_slip, max_ft, max_fric_work + ) + ) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/examples/contact_block_sliding/run_case.bat b/examples/contact_block_sliding/run_case.bat new file mode 100644 index 0000000..11a7412 --- /dev/null +++ b/examples/contact_block_sliding/run_case.bat @@ -0,0 +1,51 @@ +@echo off +setlocal + +set "CASE_DIR=%~dp0" +set "ROOT_DIR=%CASE_DIR%..\.." +set "EXE=%ROOT_DIR%\build-win-gcc-16\femlite.exe" +set "PYTHON=%ROOT_DIR%\Python\.venv\Scripts\python.exe" +set "INP=%CASE_DIR%block_sliding_friction.inp" +set "OUT_DIR=%CASE_DIR%results\auto_run" + +echo FemLite contact_block_sliding +echo. + +if not exist "%EXE%" ( + echo femlite.exe was not found. Trying to build build-win-gcc-16... + cmake --build "%ROOT_DIR%\build-win-gcc-16" --config Release + if errorlevel 1 goto fail +) + +if not exist "%OUT_DIR%" mkdir "%OUT_DIR%" +del /Q "%OUT_DIR%\history.csv" 2>nul +del /Q "%OUT_DIR%\nodes_*.csv" 2>nul +del /Q "%OUT_DIR%\elements_*.csv" 2>nul +del /Q "%OUT_DIR%\reactions_*.csv" 2>nul +del /Q "%OUT_DIR%\contact_*.csv" 2>nul +del /Q "%OUT_DIR%\result_*.vtk" 2>nul + +echo Running FemLite... +"%EXE%" "%INP%" --output-dir "%OUT_DIR%" --output-interval 10 --run +if errorlevel 1 goto fail + +if exist "%PYTHON%" ( + echo. + echo Checking results... + "%PYTHON%" "%CASE_DIR%check_contact_results.py" "%OUT_DIR%" + if errorlevel 1 goto fail +) else ( + echo. + echo Python virtual environment not found; skipping result checker. +) + +echo. +echo Case completed successfully. +if not "%FEMLITE_NO_PAUSE%"=="1" pause +exit /b 0 + +:fail +echo. +echo Case failed. +if not "%FEMLITE_NO_PAUSE%"=="1" pause +exit /b 1 diff --git a/examples/contact_rigid_plane_indentation/README.md b/examples/contact_rigid_plane_indentation/README.md new file mode 100644 index 0000000..c8b4b0c --- /dev/null +++ b/examples/contact_rigid_plane_indentation/README.md @@ -0,0 +1,36 @@ +# contact_rigid_plane_indentation + +Small V4.2 contact example for a C3D8 elastic block pressed into an +analytical rigid plane. + +Purpose: + +- Exercise `*SURFACE, TYPE=NODE`. +- Exercise `*SURFACE, TYPE=ANALYTICAL` rigid plane contact. +- Exercise `*SURFACE INTERACTION` property inheritance. +- Exercise `AUGMENT` active-set/augmented-penalty output columns. + +Model: + +- One C3D8 block, 0.10 m x 0.10 m x 0.05 m. +- Initial bottom gap above the rigid plane: 0.002 m. +- Top nodes are prescribed downward by 0.008 m over 1.0E-3 s. +- Bottom node surface contacts the analytical plane `z = 0`. +- Contact property: penalty = 1.0E7, damping = 1.0E2, augment = 0.25. + +Run from the repository root: + +Double-click `run_case.bat` in this directory, or run manually: + +```powershell +New-Item -ItemType Directory -Force examples\contact_rigid_plane_indentation\results | Out-Null +build-win-gcc-16\femlite.exe examples\contact_rigid_plane_indentation\rigid_plane_indentation.inp --output-dir examples\contact_rigid_plane_indentation\results --output-interval 10 --run +``` + +Check: + +```powershell +.\Python\.venv\Scripts\python.exe examples\contact_rigid_plane_indentation\check_contact_results.py examples\contact_rigid_plane_indentation\results +``` + +The double-click script writes repeatable output to `results\auto_run`. diff --git a/examples/contact_rigid_plane_indentation/check_contact_results.py b/examples/contact_rigid_plane_indentation/check_contact_results.py new file mode 100644 index 0000000..72e2caa --- /dev/null +++ b/examples/contact_rigid_plane_indentation/check_contact_results.py @@ -0,0 +1,54 @@ +from __future__ import annotations + +import csv +import glob +import os +import sys + + +def read_contact_rows(result_dir: str) -> list[dict[str, str]]: + rows: list[dict[str, str]] = [] + for path in sorted(glob.glob(os.path.join(result_dir, "contact_*.csv"))): + with open(path, newline="") as stream: + rows.extend(csv.DictReader(stream)) + return rows + + +def main() -> int: + result_dir = sys.argv[1] if len(sys.argv) > 1 else "results" + rows = read_contact_rows(result_dir) + pair_rows = [row for row in rows if row.get("kind") == "PAIR"] + if not pair_rows: + print("no PAIR rows found in contact CSV output") + return 1 + + max_active = max(int(float(row["active_count"])) for row in pair_rows) + max_gap = max(float(row["gap_residual"]) for row in pair_rows) + max_lambda = max(float(row["aug_lambda"]) for row in pair_rows) + max_status = max(int(float(row["status"])) for row in pair_rows) + + if max_active <= 0 or max_gap <= 0.0: + print("contact did not activate") + return 1 + if max_lambda <= 0.0: + print("augmented contact multiplier did not grow") + return 1 + if max_status <= 0: + print("active-set status was not recorded") + return 1 + + vtk_files = glob.glob(os.path.join(result_dir, "result_*.vtk")) + if not vtk_files: + print("no VTK output found") + return 1 + + print( + "ok: max_active={}, max_gap={:.6e}, max_aug_lambda={:.6e}".format( + max_active, max_gap, max_lambda + ) + ) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/examples/contact_rigid_plane_indentation/rigid_plane_indentation.inp b/examples/contact_rigid_plane_indentation/rigid_plane_indentation.inp new file mode 100644 index 0000000..01b8407 --- /dev/null +++ b/examples/contact_rigid_plane_indentation/rigid_plane_indentation.inp @@ -0,0 +1,42 @@ +*HEADING +FemLite V4.2 rigid analytical plane indentation contact example +*NODE +1, -0.05, -0.05, 0.002 +2, 0.05, -0.05, 0.002 +3, 0.05, 0.05, 0.002 +4, -0.05, 0.05, 0.002 +5, -0.05, -0.05, 0.052 +6, 0.05, -0.05, 0.052 +7, 0.05, 0.05, 0.052 +8, -0.05, 0.05, 0.052 +*ELEMENT, TYPE=C3D8, ELSET=BLOCK +1, 1, 2, 3, 4, 5, 6, 7, 8 +*NSET, NSET=BOTTOM +1, 2, 3, 4 +*NSET, NSET=TOP +5, 6, 7, 8 +*NSET, NSET=ALLNODES +1, 2, 3, 4, 5, 6, 7, 8 +*MATERIAL, NAME=SOFT_SOLID +*DENSITY +1000.0 +*ELASTIC +1.0E7, 0.30 +*SOLID SECTION, ELSET=BLOCK, MATERIAL=SOFT_SOLID +*AMPLITUDE, NAME=RAMP +0.0, 0.0, 1.0E-3, 1.0 +*BOUNDARY +ALLNODES, 1, 2, 0.0 +*BOUNDARY, AMPLITUDE=RAMP +TOP, 3, 3, -8.0E-3 +*SURFACE, NAME=BOTTOM_NODES, TYPE=NODE +BOTTOM +*SURFACE, NAME=RIGID_PLANE, TYPE=ANALYTICAL +PLANE, 0.0, 0.0, 1.0, 0.0 +*SURFACE INTERACTION, NAME=CONTACT_PROP, PENALTY=1.0E7, DAMPING=1.0E2, SEARCH=1.0E-2, AUGMENT=0.25 +*CONTACT PAIR, INTERACTION=CONTACT_PROP +BOTTOM_NODES, RIGID_PLANE +*STEP +*DYNAMIC +1.0E-5, 1.0E-3 +*END STEP diff --git a/examples/contact_rigid_plane_indentation/run_case.bat b/examples/contact_rigid_plane_indentation/run_case.bat new file mode 100644 index 0000000..f2e2cce --- /dev/null +++ b/examples/contact_rigid_plane_indentation/run_case.bat @@ -0,0 +1,51 @@ +@echo off +setlocal + +set "CASE_DIR=%~dp0" +set "ROOT_DIR=%CASE_DIR%..\.." +set "EXE=%ROOT_DIR%\build-win-gcc-16\femlite.exe" +set "PYTHON=%ROOT_DIR%\Python\.venv\Scripts\python.exe" +set "INP=%CASE_DIR%rigid_plane_indentation.inp" +set "OUT_DIR=%CASE_DIR%results\auto_run" + +echo FemLite contact_rigid_plane_indentation +echo. + +if not exist "%EXE%" ( + echo femlite.exe was not found. Trying to build build-win-gcc-16... + cmake --build "%ROOT_DIR%\build-win-gcc-16" --config Release + if errorlevel 1 goto fail +) + +if not exist "%OUT_DIR%" mkdir "%OUT_DIR%" +del /Q "%OUT_DIR%\history.csv" 2>nul +del /Q "%OUT_DIR%\nodes_*.csv" 2>nul +del /Q "%OUT_DIR%\elements_*.csv" 2>nul +del /Q "%OUT_DIR%\reactions_*.csv" 2>nul +del /Q "%OUT_DIR%\contact_*.csv" 2>nul +del /Q "%OUT_DIR%\result_*.vtk" 2>nul + +echo Running FemLite... +"%EXE%" "%INP%" --output-dir "%OUT_DIR%" --output-interval 10 --run +if errorlevel 1 goto fail + +if exist "%PYTHON%" ( + echo. + echo Checking results... + "%PYTHON%" "%CASE_DIR%check_contact_results.py" "%OUT_DIR%" + if errorlevel 1 goto fail +) else ( + echo. + echo Python virtual environment not found; skipping result checker. +) + +echo. +echo Case completed successfully. +if not "%FEMLITE_NO_PAUSE%"=="1" pause +exit /b 0 + +:fail +echo. +echo Case failed. +if not "%FEMLITE_NO_PAUSE%"=="1" pause +exit /b 1 diff --git a/examples/contact_two_blocks_compression/README.md b/examples/contact_two_blocks_compression/README.md new file mode 100644 index 0000000..db7e9d4 --- /dev/null +++ b/examples/contact_two_blocks_compression/README.md @@ -0,0 +1,37 @@ +# contact_two_blocks_compression + +Small V4.2 face-to-face contact example for two C3D8 elastic blocks. + +Purpose: + +- Exercise element-surface contact with explicit face labels. +- Exercise contact property inheritance through `*SURFACE INTERACTION`. +- Exercise active-set and augmented-penalty contact CSV diagnostics. +- Provide a compact two-body compression case for future nonlinear static + contact work. + +Model: + +- Two 0.10 m x 0.10 m x 0.05 m C3D8 blocks. +- The upper block starts with 0.001 m initial penetration into the lower top + face so contact is active immediately after the first force evaluation. +- Lower bottom face is fixed in z; all nodes are fixed in x/y. +- Upper top face is prescribed downward by 0.004 m over 1.0E-3 s. +- Contact property: penalty = 1.0E7, damping = 1.0E2, augment = 0.20. + +Run from the repository root: + +Double-click `run_case.bat` in this directory, or run manually: + +```powershell +New-Item -ItemType Directory -Force examples\contact_two_blocks_compression\results | Out-Null +build-win-gcc-16\femlite.exe examples\contact_two_blocks_compression\two_blocks_compression.inp --output-dir examples\contact_two_blocks_compression\results --output-interval 10 --run +``` + +Check: + +```powershell +.\Python\.venv\Scripts\python.exe examples\contact_two_blocks_compression\check_contact_results.py examples\contact_two_blocks_compression\results +``` + +The double-click script writes repeatable output to `results\auto_run`. diff --git a/examples/contact_two_blocks_compression/check_contact_results.py b/examples/contact_two_blocks_compression/check_contact_results.py new file mode 100644 index 0000000..c78a753 --- /dev/null +++ b/examples/contact_two_blocks_compression/check_contact_results.py @@ -0,0 +1,44 @@ +from __future__ import annotations + +import csv +import glob +import os +import sys + + +def main() -> int: + result_dir = sys.argv[1] if len(sys.argv) > 1 else "results" + pair_rows: list[dict[str, str]] = [] + for path in sorted(glob.glob(os.path.join(result_dir, "contact_*.csv"))): + with open(path, newline="") as stream: + pair_rows.extend(row for row in csv.DictReader(stream) if row["kind"] == "PAIR") + + if not pair_rows: + print("no contact pair rows found") + return 1 + + max_active = max(int(float(row["active_count"])) for row in pair_rows) + max_force = max(float(row["force_norm"]) for row in pair_rows) + max_gap = max(float(row["gap_residual"]) for row in pair_rows) + max_lambda = max(float(row["aug_lambda"]) for row in pair_rows) + + if max_active <= 0 or max_force <= 0.0 or max_gap <= 0.0: + print("face-to-face contact did not activate") + return 1 + if max_lambda <= 0.0: + print("augmented contact multiplier did not grow") + return 1 + if not glob.glob(os.path.join(result_dir, "result_*.vtk")): + print("no VTK output found") + return 1 + + print( + "ok: max_active={}, max_force={:.6e}, max_gap={:.6e}, max_aug_lambda={:.6e}".format( + max_active, max_force, max_gap, max_lambda + ) + ) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/examples/contact_two_blocks_compression/run_case.bat b/examples/contact_two_blocks_compression/run_case.bat new file mode 100644 index 0000000..052ca04 --- /dev/null +++ b/examples/contact_two_blocks_compression/run_case.bat @@ -0,0 +1,51 @@ +@echo off +setlocal + +set "CASE_DIR=%~dp0" +set "ROOT_DIR=%CASE_DIR%..\.." +set "EXE=%ROOT_DIR%\build-win-gcc-16\femlite.exe" +set "PYTHON=%ROOT_DIR%\Python\.venv\Scripts\python.exe" +set "INP=%CASE_DIR%two_blocks_compression.inp" +set "OUT_DIR=%CASE_DIR%results\auto_run" + +echo FemLite contact_two_blocks_compression +echo. + +if not exist "%EXE%" ( + echo femlite.exe was not found. Trying to build build-win-gcc-16... + cmake --build "%ROOT_DIR%\build-win-gcc-16" --config Release + if errorlevel 1 goto fail +) + +if not exist "%OUT_DIR%" mkdir "%OUT_DIR%" +del /Q "%OUT_DIR%\history.csv" 2>nul +del /Q "%OUT_DIR%\nodes_*.csv" 2>nul +del /Q "%OUT_DIR%\elements_*.csv" 2>nul +del /Q "%OUT_DIR%\reactions_*.csv" 2>nul +del /Q "%OUT_DIR%\contact_*.csv" 2>nul +del /Q "%OUT_DIR%\result_*.vtk" 2>nul + +echo Running FemLite... +"%EXE%" "%INP%" --output-dir "%OUT_DIR%" --output-interval 10 --run +if errorlevel 1 goto fail + +if exist "%PYTHON%" ( + echo. + echo Checking results... + "%PYTHON%" "%CASE_DIR%check_contact_results.py" "%OUT_DIR%" + if errorlevel 1 goto fail +) else ( + echo. + echo Python virtual environment not found; skipping result checker. +) + +echo. +echo Case completed successfully. +if not "%FEMLITE_NO_PAUSE%"=="1" pause +exit /b 0 + +:fail +echo. +echo Case failed. +if not "%FEMLITE_NO_PAUSE%"=="1" pause +exit /b 1 diff --git a/examples/contact_two_blocks_compression/two_blocks_compression.inp b/examples/contact_two_blocks_compression/two_blocks_compression.inp new file mode 100644 index 0000000..3fcbbba --- /dev/null +++ b/examples/contact_two_blocks_compression/two_blocks_compression.inp @@ -0,0 +1,55 @@ +*HEADING +FemLite V4.2 two C3D8 blocks face-to-face compression contact example +*NODE +1, -0.05, -0.05, -0.050 +2, 0.05, -0.05, -0.050 +3, 0.05, 0.05, -0.050 +4, -0.05, 0.05, -0.050 +5, -0.05, -0.05, 0.000 +6, 0.05, -0.05, 0.000 +7, 0.05, 0.05, 0.000 +8, -0.05, 0.05, 0.000 +9, -0.05, -0.05, -0.001 +10, 0.05, -0.05, -0.001 +11, 0.05, 0.05, -0.001 +12, -0.05, 0.05, -0.001 +13, -0.05, -0.05, 0.049 +14, 0.05, -0.05, 0.049 +15, 0.05, 0.05, 0.049 +16, -0.05, 0.05, 0.049 +*ELEMENT, TYPE=C3D8, ELSET=LOWER +1, 1, 2, 3, 4, 5, 6, 7, 8 +*ELEMENT, TYPE=C3D8, ELSET=UPPER +2, 9, 10, 11, 12, 13, 14, 15, 16 +*ELSET, ELSET=ALLSOLID +1, 2 +*NSET, NSET=LOWER_BOTTOM +1, 2, 3, 4 +*NSET, NSET=UPPER_TOP +13, 14, 15, 16 +*NSET, NSET=ALLNODES +1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 +*MATERIAL, NAME=SOFT_SOLID +*DENSITY +1000.0 +*ELASTIC +1.0E7, 0.30 +*SOLID SECTION, ELSET=ALLSOLID, MATERIAL=SOFT_SOLID +*AMPLITUDE, NAME=RAMP +0.0, 0.0, 1.0E-3, 1.0 +*BOUNDARY +ALLNODES, 1, 2, 0.0 +LOWER_BOTTOM, 3, 3, 0.0 +*BOUNDARY, AMPLITUDE=RAMP +UPPER_TOP, 3, 3, -4.0E-3 +*SURFACE, NAME=LOWER_TOP, TYPE=ELEMENT +LOWER, S2 +*SURFACE, NAME=UPPER_BOTTOM, TYPE=ELEMENT +UPPER, S1 +*SURFACE INTERACTION, NAME=BLOCK_CONTACT, PENALTY=1.0E7, DAMPING=1.0E2, SEARCH=1.0E-2, AUGMENT=0.20 +*CONTACT PAIR, INTERACTION=BLOCK_CONTACT +UPPER_BOTTOM, LOWER_TOP +*STEP +*DYNAMIC +1.0E-5, 1.0E-3 +*END STEP diff --git a/examples/shell_rigid_plane_contact/README.md b/examples/shell_rigid_plane_contact/README.md new file mode 100644 index 0000000..2663a26 --- /dev/null +++ b/examples/shell_rigid_plane_contact/README.md @@ -0,0 +1,34 @@ +# shell_rigid_plane_contact + +Small V4.2 shell-to-rigid-plane contact example using S4 shell elements. + +Purpose: + +- Exercise node-based shell contact against an analytical rigid plane. +- Exercise pair-level `AUGMENT` input on `*CONTACT PAIR`. +- Record active-set status and augmented-penalty diagnostics in contact CSV. + +Model: + +- 0.10 m x 0.10 m plate, thickness 0.002 m. +- Four S4 shell elements with edge nodes clamped. +- Center node is prescribed downward by 0.005 m over 2.0E-4 s. +- Plate starts 0.002 m above the analytical plane `z = 0`. +- Contact pair: node-based shell surface against analytical rigid plane. + +Run from the repository root: + +Double-click `run_case.bat` in this directory, or run manually: + +```powershell +New-Item -ItemType Directory -Force examples\shell_rigid_plane_contact\results | Out-Null +build-win-gcc-16\femlite.exe examples\shell_rigid_plane_contact\shell_rigid_plane_contact.inp --output-dir examples\shell_rigid_plane_contact\results --output-interval 10 --run +``` + +Check: + +```powershell +.\Python\.venv\Scripts\python.exe examples\shell_rigid_plane_contact\check_contact_results.py examples\shell_rigid_plane_contact\results +``` + +The double-click script writes repeatable output to `results\auto_run`. diff --git a/examples/shell_rigid_plane_contact/check_contact_results.py b/examples/shell_rigid_plane_contact/check_contact_results.py new file mode 100644 index 0000000..136d185 --- /dev/null +++ b/examples/shell_rigid_plane_contact/check_contact_results.py @@ -0,0 +1,43 @@ +from __future__ import annotations + +import csv +import glob +import os +import sys + + +def main() -> int: + result_dir = sys.argv[1] if len(sys.argv) > 1 else "results" + pair_rows: list[dict[str, str]] = [] + for path in sorted(glob.glob(os.path.join(result_dir, "contact_*.csv"))): + with open(path, newline="") as stream: + pair_rows.extend(row for row in csv.DictReader(stream) if row["kind"] == "PAIR") + + if not pair_rows: + print("no contact pair rows found") + return 1 + + max_active = max(int(float(row["active_count"])) for row in pair_rows) + max_gap = max(float(row["gap_residual"]) for row in pair_rows) + max_lambda = max(float(row["aug_lambda"]) for row in pair_rows) + + if max_active <= 0 or max_gap <= 0.0: + print("shell rigid-plane contact did not activate") + return 1 + if max_lambda <= 0.0: + print("augmented contact multiplier did not grow") + return 1 + if not glob.glob(os.path.join(result_dir, "result_*.vtk")): + print("no VTK output found") + return 1 + + print( + "ok: max_active={}, max_gap={:.6e}, max_aug_lambda={:.6e}".format( + max_active, max_gap, max_lambda + ) + ) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/examples/shell_rigid_plane_contact/run_case.bat b/examples/shell_rigid_plane_contact/run_case.bat new file mode 100644 index 0000000..46328b5 --- /dev/null +++ b/examples/shell_rigid_plane_contact/run_case.bat @@ -0,0 +1,51 @@ +@echo off +setlocal + +set "CASE_DIR=%~dp0" +set "ROOT_DIR=%CASE_DIR%..\.." +set "EXE=%ROOT_DIR%\build-win-gcc-16\femlite.exe" +set "PYTHON=%ROOT_DIR%\Python\.venv\Scripts\python.exe" +set "INP=%CASE_DIR%shell_rigid_plane_contact.inp" +set "OUT_DIR=%CASE_DIR%results\auto_run" + +echo FemLite shell_rigid_plane_contact +echo. + +if not exist "%EXE%" ( + echo femlite.exe was not found. Trying to build build-win-gcc-16... + cmake --build "%ROOT_DIR%\build-win-gcc-16" --config Release + if errorlevel 1 goto fail +) + +if not exist "%OUT_DIR%" mkdir "%OUT_DIR%" +del /Q "%OUT_DIR%\history.csv" 2>nul +del /Q "%OUT_DIR%\nodes_*.csv" 2>nul +del /Q "%OUT_DIR%\elements_*.csv" 2>nul +del /Q "%OUT_DIR%\reactions_*.csv" 2>nul +del /Q "%OUT_DIR%\contact_*.csv" 2>nul +del /Q "%OUT_DIR%\result_*.vtk" 2>nul + +echo Running FemLite... +"%EXE%" "%INP%" --output-dir "%OUT_DIR%" --output-interval 10 --run +if errorlevel 1 goto fail + +if exist "%PYTHON%" ( + echo. + echo Checking results... + "%PYTHON%" "%CASE_DIR%check_contact_results.py" "%OUT_DIR%" + if errorlevel 1 goto fail +) else ( + echo. + echo Python virtual environment not found; skipping result checker. +) + +echo. +echo Case completed successfully. +if not "%FEMLITE_NO_PAUSE%"=="1" pause +exit /b 0 + +:fail +echo. +echo Case failed. +if not "%FEMLITE_NO_PAUSE%"=="1" pause +exit /b 1 diff --git a/examples/shell_rigid_plane_contact/shell_rigid_plane_contact.inp b/examples/shell_rigid_plane_contact/shell_rigid_plane_contact.inp new file mode 100644 index 0000000..3e0c501 --- /dev/null +++ b/examples/shell_rigid_plane_contact/shell_rigid_plane_contact.inp @@ -0,0 +1,45 @@ +*HEADING +FemLite V4.2 S4 shell to analytical rigid plane contact example +*NODE +1, -0.05, -0.05, 0.002 +2, 0.00, -0.05, 0.002 +3, 0.05, -0.05, 0.002 +4, -0.05, 0.00, 0.002 +5, 0.00, 0.00, 0.002 +6, 0.05, 0.00, 0.002 +7, -0.05, 0.05, 0.002 +8, 0.00, 0.05, 0.002 +9, 0.05, 0.05, 0.002 +*ELEMENT, TYPE=S4, ELSET=PLATE +1, 1, 2, 5, 4 +2, 2, 3, 6, 5 +3, 4, 5, 8, 7 +4, 5, 6, 9, 8 +*NSET, NSET=EDGE +1, 2, 3, 4, 6, 7, 8, 9 +*NSET, NSET=CENTER +5 +*NSET, NSET=PLATE_NODES +1, 2, 3, 4, 5, 6, 7, 8, 9 +*MATERIAL, NAME=ALUMINUM +*DENSITY +2700.0 +*ELASTIC +7.0E10, 0.30 +*SHELL SECTION, ELSET=PLATE, MATERIAL=ALUMINUM +2.0E-3 +*AMPLITUDE, NAME=RAMP +0.0, 0.0, 2.0E-4, 1.0 +*BOUNDARY +EDGE, 1, 6, 0.0 +*BOUNDARY, AMPLITUDE=RAMP +CENTER, 3, 3, -5.0E-3 +*SURFACE, NAME=PLATE_NODE_SURFACE, TYPE=NODE +PLATE_NODES +*SURFACE, NAME=RIGID_PLANE, TYPE=ANALYTICAL +PLANE, 0.0, 0.0, 1.0, 0.0 +*CONTACT PAIR, SLAVE=PLATE_NODE_SURFACE, MASTER=RIGID_PLANE, PENALTY=5.0E8, DAMPING=1.0E4, AUGMENT=0.25 +*STEP +*DYNAMIC +2.0E-6, 2.0E-4 +*END STEP diff --git a/include/femlite_c_api.h b/include/femlite_c_api.h index cef59b1..37c5ac4 100644 --- a/include/femlite_c_api.h +++ b/include/femlite_c_api.h @@ -22,6 +22,28 @@ int femlite_get_contact_summary(int *active_count, double *work_total, double *max_penetration, double *force_norm); +int femlite_get_contact_diagnostics(int *active_pair_count, + int *activated_pair_count, + int *maintained_pair_count, + int *released_pair_count, + double *max_gap_residual, + double *max_aug_lambda); +int femlite_get_contact_friction_summary(double *work_step, + double *work_total, + double *max_accumulated_slip); +int femlite_get_contact_history_stats(int *slot_count, + double *max_slot_slip, + double *max_slot_work, + double *max_tangent_force); +int femlite_get_contact_search_stats(int *facet_count, + int *master_count, + int *broad_count, + int *active_count, + int *bvh_node_count); +int femlite_get_contact_perf_stats(double *search_time, + double *force_time, + double *tangent_time); +int femlite_get_contact_cache_stats(int *used_count, int *overflow_count); int femlite_clear_sph_forces(void); int femlite_apply_sph_nodal_forces(int num_force_nodes, diff --git a/src/base/fem_api.f90 b/src/base/fem_api.f90 index 35bca72..2916a31 100644 --- a/src/base/fem_api.f90 +++ b/src/base/fem_api.f90 @@ -13,6 +13,14 @@ module fem_api use fem_coupling use fem_boundary use fem_contact + use fem_contact_state, only: CONTACT_PAIR_ACTIVATED, & + CONTACT_PAIR_MAINTAINED, & + CONTACT_PAIR_RELEASED, & + contact_get_search_stats, & + contact_get_perf_stats, & + contact_get_cache_stats, & + contact_reset_search_stats + use fem_contact_history, only: contact_history_stats use fem_database_surface, only: db_surface_node_count, db_get_surface_nodes use fem_output_csv use fem_output_vtk @@ -29,6 +37,7 @@ contains call error_clear(ierr) call control_set_defaults() + call contact_reset_search_stats() call log_open('femlite.log', log_ierr) if (log_ierr .ne. FEM_SUCCESS) then call error_set(FEM_ERR_OUTPUT, 'failed to open femlite.log', ierr) @@ -44,6 +53,7 @@ contains integer, intent(out) :: ierr call cpl_finalize_facets() + call contact_reset_search_stats() call database_finalize(ierr) if (ierr .ne. FEM_SUCCESS) return @@ -311,6 +321,178 @@ contains ierr = FEM_SUCCESS end subroutine fem_get_contact_summary + subroutine fem_get_contact_diagnostics(active_pair_count, & + activated_pair_count, & + maintained_pair_count, & + released_pair_count, & + max_gap_residual, max_aug_lambda, & + ierr) + integer, intent(out) :: active_pair_count + integer, intent(out) :: activated_pair_count + integer, intent(out) :: maintained_pair_count + integer, intent(out) :: released_pair_count + real(rk), intent(out) :: max_gap_residual + real(rk), intent(out) :: max_aug_lambda + integer, intent(out) :: ierr + integer :: pair + + active_pair_count = 0 + activated_pair_count = 0 + maintained_pair_count = 0 + released_pair_count = 0 + max_gap_residual = ZERO + max_aug_lambda = ZERO + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_get_contact_diagnostics called before fem_initialize', ierr) + return + end if + + if (allocated(contact_pair_active)) then + do pair = 1, size(contact_pair_active) + if (contact_pair_active(pair) .gt. 0) active_pair_count = & + active_pair_count + 1 + end do + end if + if (allocated(contact_pair_status)) then + do pair = 1, size(contact_pair_status) + select case (contact_pair_status(pair)) + case (CONTACT_PAIR_ACTIVATED) + activated_pair_count = activated_pair_count + 1 + case (CONTACT_PAIR_MAINTAINED) + maintained_pair_count = maintained_pair_count + 1 + case (CONTACT_PAIR_RELEASED) + released_pair_count = released_pair_count + 1 + end select + end do + end if + if (allocated(contact_pair_gap_residual)) then + max_gap_residual = maxval(contact_pair_gap_residual) + end if + if (allocated(contact_pair_aug_lambda)) then + max_aug_lambda = maxval(contact_pair_aug_lambda) + end if + + ierr = FEM_SUCCESS + end subroutine fem_get_contact_diagnostics + + subroutine fem_get_contact_friction(work_step, work_total, & + max_accumulated_slip, ierr) + real(rk), intent(out) :: work_step + real(rk), intent(out) :: work_total + real(rk), intent(out) :: max_accumulated_slip + integer, intent(out) :: ierr + + work_step = ZERO + work_total = ZERO + max_accumulated_slip = ZERO + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_get_contact_friction called before fem_initialize', ierr) + return + end if + + work_step = contact_friction_work_step + work_total = contact_friction_work_total + if (allocated(contact_pair_slip)) then + max_accumulated_slip = maxval(contact_pair_slip) + end if + ierr = FEM_SUCCESS + end subroutine fem_get_contact_friction + + subroutine fem_get_contact_hist_stats(slot_count, max_slot_slip, & + max_slot_work, max_tangent_force, & + ierr) + integer, intent(out) :: slot_count + real(rk), intent(out) :: max_slot_slip + real(rk), intent(out) :: max_slot_work + real(rk), intent(out) :: max_tangent_force + integer, intent(out) :: ierr + + slot_count = 0 + max_slot_slip = ZERO + max_slot_work = ZERO + max_tangent_force = ZERO + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_get_contact_hist_stats called before fem_initialize', ierr) + return + end if + + call contact_history_stats(slot_count, max_slot_slip, max_slot_work, & + max_tangent_force) + ierr = FEM_SUCCESS + end subroutine fem_get_contact_hist_stats + + subroutine fem_get_contact_search_stats(facet_count, master_count, & + broad_count, active_count, & + bvh_node_count, ierr) + integer, intent(out) :: facet_count + integer, intent(out) :: master_count + integer, intent(out) :: broad_count + integer, intent(out) :: active_count + integer, intent(out) :: bvh_node_count + integer, intent(out) :: ierr + + facet_count = 0 + master_count = 0 + broad_count = 0 + active_count = 0 + bvh_node_count = 0 + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_get_contact_search_stats called before fem_initialize', ierr) + return + end if + + call contact_get_search_stats(facet_count, master_count, broad_count, & + active_count, bvh_node_count) + ierr = FEM_SUCCESS + end subroutine fem_get_contact_search_stats + + subroutine fem_get_contact_perf_stats(search_time, force_time, & + tangent_time, ierr) + real(rk), intent(out) :: search_time + real(rk), intent(out) :: force_time + real(rk), intent(out) :: tangent_time + integer, intent(out) :: ierr + + search_time = ZERO + force_time = ZERO + tangent_time = ZERO + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_get_contact_perf_stats called before fem_initialize', ierr) + return + end if + + call contact_get_perf_stats(search_time, force_time, tangent_time) + ierr = FEM_SUCCESS + end subroutine fem_get_contact_perf_stats + + subroutine fem_get_contact_cache_stats(used_count, overflow_count, ierr) + integer, intent(out) :: used_count + integer, intent(out) :: overflow_count + integer, intent(out) :: ierr + + used_count = 0 + overflow_count = 0 + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_get_contact_cache_stats called before fem_initialize', ierr) + return + end if + + call contact_get_cache_stats(used_count, overflow_count) + ierr = FEM_SUCCESS + end subroutine fem_get_contact_cache_stats + subroutine fem_get_boundary_facet_count(num_facets, max_face_nodes, ierr) integer, intent(out) :: num_facets integer, intent(out) :: max_face_nodes diff --git a/src/base/fem_precision.f90 b/src/base/fem_precision.f90 index 02011ec..8340477 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 = 4 - integer, parameter :: FEMLITE_VERSION_PATCH = 1 + integer, parameter :: FEMLITE_VERSION_PATCH = 3 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 82156ea..54131e1 100644 --- a/src/boundary/fem_contact.f90 +++ b/src/boundary/fem_contact.f90 @@ -21,6 +21,8 @@ contains subroutine contact_apply_plane_forces(ierr) integer, intent(out) :: ierr integer :: plane + real(rk) :: time_start + real(rk) :: time_finish if (.not. allocated(fcontact)) then call error_set(FEM_ERR_INTERNAL, 'contact force vector is not allocated', ierr) @@ -29,14 +31,30 @@ contains fcontact = ZERO call contact_reset_status() + call cpu_time(time_start) do plane = 1, ncontact_plane call apply_contact_plane(plane, ierr) - if (ierr .ne. FEM_SUCCESS) return + if (ierr .ne. FEM_SUCCESS) then + call cpu_time(time_finish) + call contact_record_force_time(time_finish - time_start) + return + end if end do call apply_analytic_pair_forces(ierr) - if (ierr .ne. FEM_SUCCESS) return + if (ierr .ne. FEM_SUCCESS) then + call cpu_time(time_finish) + call contact_record_force_time(time_finish - time_start) + return + end if call apply_contact_pair_forces(ierr) - if (ierr .ne. FEM_SUCCESS) return + if (ierr .ne. FEM_SUCCESS) then + call cpu_time(time_finish) + call contact_record_force_time(time_finish - time_start) + return + end if + call contact_finalize_status() + call cpu_time(time_finish) + call contact_record_force_time(time_finish - time_start) ierr = FEM_SUCCESS end subroutine contact_apply_plane_forces @@ -46,15 +64,32 @@ contains real(rk), intent(in) :: velocity_scale integer, intent(out) :: ierr integer :: plane + real(rk) :: time_start + real(rk) :: time_finish + call cpu_time(time_start) do plane = 1, ncontact_plane call apply_contact_plane_tangent(plane, kmat, velocity_scale, ierr) - if (ierr .ne. FEM_SUCCESS) return + if (ierr .ne. FEM_SUCCESS) then + call cpu_time(time_finish) + call contact_record_tangent_time(time_finish - time_start) + return + end if end do call apply_analytic_pair_tangents(kmat, velocity_scale, ierr) - if (ierr .ne. FEM_SUCCESS) return + if (ierr .ne. FEM_SUCCESS) then + call cpu_time(time_finish) + call contact_record_tangent_time(time_finish - time_start) + return + end if call apply_contact_pair_tangent(kmat, velocity_scale, ierr) - if (ierr .ne. FEM_SUCCESS) return + if (ierr .ne. FEM_SUCCESS) then + call cpu_time(time_finish) + call contact_record_tangent_time(time_finish - time_start) + return + end if + call cpu_time(time_finish) + call contact_record_tangent_time(time_finish - time_start) ierr = FEM_SUCCESS end subroutine contact_apply_plane_tangent @@ -576,7 +611,7 @@ contains integer :: dof vn = dot_product(normal, velocity) - force_mag = contact_pair_penalty(pair) * penetration + force_mag = contact_pair_normal_pressure(pair, penetration) if (vn .lt. ZERO) force_mag = force_mag + contact_pair_damping(pair) * (-vn) force_vec = force_mag * normal do idir = 1, NDIM diff --git a/src/boundary/fem_contact_candidate_cache.f90 b/src/boundary/fem_contact_candidate_cache.f90 new file mode 100644 index 0000000..2f7110f --- /dev/null +++ b/src/boundary/fem_contact_candidate_cache.f90 @@ -0,0 +1,288 @@ +module fem_contact_candidate_cache + use fem_precision + implicit none + + private + + integer, parameter, public :: CONTACT_CANDIDATE_CACHE_CAP = 64 + + public :: contact_candidate_cache_reset + public :: contact_candidate_cache_try + public :: contact_candidate_cache_store + public :: contact_candidate_cache_count + public :: contact_candidate_cache_master + public :: contact_candidate_cache_stats + + integer :: cache_npair = 0 + integer :: cache_nf = 0 + logical, allocatable :: cache_valid(:) + integer, allocatable :: cache_count(:,:) + integer, allocatable :: cache_master(:,:,:) + integer, allocatable :: cache_elem(:,:) + integer, allocatable :: cache_face(:,:) + real(rk), allocatable :: cache_center(:,:,:) + real(rk), allocatable :: cache_radius(:,:) + real(rk), allocatable :: cache_search(:) + character(len=NAME_LEN), allocatable :: cache_slave(:) + character(len=NAME_LEN), allocatable :: cache_master_name(:) + integer :: stat_hit_count = 0 + integer :: stat_miss_count = 0 + integer :: stat_invalid_count = 0 + integer :: stat_store_count = 0 + integer :: stat_overflow_count = 0 + +contains + + subroutine contact_candidate_cache_reset() + call free_cache_arrays() + cache_npair = 0 + cache_nf = 0 + stat_hit_count = 0 + stat_miss_count = 0 + stat_invalid_count = 0 + stat_store_count = 0 + stat_overflow_count = 0 + end subroutine contact_candidate_cache_reset + + subroutine contact_candidate_cache_try(pair, npair, nf, elem_id, face_id, & + center, radius, slave_name, & + master_name, search_value, reusable) + integer, intent(in) :: pair + integer, intent(in) :: npair + integer, intent(in) :: nf + integer, intent(in) :: elem_id(nf) + integer, intent(in) :: face_id(nf) + real(rk), intent(in) :: center(NDIM,nf) + real(rk), intent(in) :: radius(nf) + character(len=*), intent(in) :: slave_name + character(len=*), intent(in) :: master_name + real(rk), intent(in) :: search_value + logical, intent(out) :: reusable + logical :: ready + + reusable = .false. + call ensure_cache(npair, nf, ready) + if (.not. ready .or. pair .lt. 1 .or. pair .gt. cache_npair) then + stat_miss_count = stat_miss_count + 1 + return + end if + if (.not. cache_valid(pair)) then + stat_miss_count = stat_miss_count + 1 + return + end if + + if (.not. cache_signature_matches(pair, nf, elem_id, face_id, & + slave_name, master_name, search_value)) then + cache_valid(pair) = .false. + stat_invalid_count = stat_invalid_count + 1 + return + end if + if (.not. cache_motion_valid(pair, nf, center, radius, search_value)) then + cache_valid(pair) = .false. + stat_invalid_count = stat_invalid_count + 1 + return + end if + + reusable = .true. + stat_hit_count = stat_hit_count + 1 + end subroutine contact_candidate_cache_try + + subroutine contact_candidate_cache_store(pair, npair, nf, elem_id, face_id, & + center, radius, slave_name, & + master_name, search_value, & + broad_count, broad_master, & + broad_overflow) + integer, intent(in) :: pair + integer, intent(in) :: npair + integer, intent(in) :: nf + integer, intent(in) :: elem_id(nf) + integer, intent(in) :: face_id(nf) + real(rk), intent(in) :: center(NDIM,nf) + real(rk), intent(in) :: radius(nf) + character(len=*), intent(in) :: slave_name + character(len=*), intent(in) :: master_name + real(rk), intent(in) :: search_value + integer, intent(in) :: broad_count(nf) + integer, intent(in) :: broad_master(:,:) + logical, intent(in) :: broad_overflow(nf) + logical :: ready + integer :: sf + integer :: copy_count + + call ensure_cache(npair, nf, ready) + if (.not. ready .or. pair .lt. 1 .or. pair .gt. cache_npair) return + + if (any(broad_overflow)) then + cache_valid(pair) = .false. + stat_overflow_count = stat_overflow_count + 1 + return + end if + + cache_count(:,pair) = 0 + cache_master(:,:,pair) = 0 + do sf = 1, nf + copy_count = min(broad_count(sf), CONTACT_CANDIDATE_CACHE_CAP) + copy_count = min(copy_count, size(broad_master, 1)) + if (copy_count .gt. 0) then + cache_count(sf,pair) = copy_count + cache_master(1:copy_count,sf,pair) = broad_master(1:copy_count,sf) + end if + end do + + cache_elem(:,pair) = elem_id + cache_face(:,pair) = face_id + cache_center(:,:,pair) = center + cache_radius(:,pair) = radius + cache_slave(pair) = slave_name + cache_master_name(pair) = master_name + cache_search(pair) = search_value + cache_valid(pair) = .true. + stat_store_count = stat_store_count + 1 + end subroutine contact_candidate_cache_store + + integer function contact_candidate_cache_count(pair, sf) + integer, intent(in) :: pair + integer, intent(in) :: sf + + contact_candidate_cache_count = 0 + if (.not. cache_index_valid(pair, sf)) return + if (.not. cache_valid(pair)) return + contact_candidate_cache_count = cache_count(sf,pair) + end function contact_candidate_cache_count + + integer function contact_candidate_cache_master(pair, sf, item) + integer, intent(in) :: pair + integer, intent(in) :: sf + integer, intent(in) :: item + + contact_candidate_cache_master = 0 + if (.not. cache_index_valid(pair, sf)) return + if (.not. cache_valid(pair)) return + if (item .lt. 1 .or. item .gt. cache_count(sf,pair)) return + contact_candidate_cache_master = cache_master(item,sf,pair) + end function contact_candidate_cache_master + + subroutine contact_candidate_cache_stats(hit_count, miss_count, & + invalid_count, store_count, & + overflow_count) + integer, intent(out) :: hit_count + integer, intent(out) :: miss_count + integer, intent(out) :: invalid_count + integer, intent(out) :: store_count + integer, intent(out) :: overflow_count + + hit_count = stat_hit_count + miss_count = stat_miss_count + invalid_count = stat_invalid_count + store_count = stat_store_count + overflow_count = stat_overflow_count + end subroutine contact_candidate_cache_stats + + subroutine ensure_cache(npair, nf, ready) + integer, intent(in) :: npair + integer, intent(in) :: nf + logical, intent(out) :: ready + integer :: stat + + ready = .false. + if (npair .le. 0 .or. nf .le. 0) return + if (allocated(cache_valid)) then + if (cache_npair .eq. npair .and. cache_nf .eq. nf) then + ready = .true. + return + end if + end if + + call free_cache_arrays() + allocate(cache_valid(npair), cache_count(nf,npair), & + cache_master(CONTACT_CANDIDATE_CACHE_CAP,nf,npair), & + cache_elem(nf,npair), cache_face(nf,npair), & + cache_center(NDIM,nf,npair), cache_radius(nf,npair), & + cache_search(npair), cache_slave(npair), cache_master_name(npair), & + stat=stat) + if (stat .ne. 0) then + call free_cache_arrays() + return + end if + + cache_npair = npair + cache_nf = nf + cache_valid = .false. + cache_count = 0 + cache_master = 0 + cache_elem = 0 + cache_face = 0 + cache_center = ZERO + cache_radius = ZERO + cache_search = ZERO + cache_slave = ' ' + cache_master_name = ' ' + ready = .true. + end subroutine ensure_cache + + subroutine free_cache_arrays() + if (allocated(cache_valid)) deallocate(cache_valid) + if (allocated(cache_count)) deallocate(cache_count) + if (allocated(cache_master)) deallocate(cache_master) + if (allocated(cache_elem)) deallocate(cache_elem) + if (allocated(cache_face)) deallocate(cache_face) + if (allocated(cache_center)) deallocate(cache_center) + if (allocated(cache_radius)) deallocate(cache_radius) + if (allocated(cache_search)) deallocate(cache_search) + if (allocated(cache_slave)) deallocate(cache_slave) + if (allocated(cache_master_name)) deallocate(cache_master_name) + end subroutine free_cache_arrays + + logical function cache_signature_matches(pair, nf, elem_id, face_id, & + slave_name, master_name, search_value) + integer, intent(in) :: pair + integer, intent(in) :: nf + integer, intent(in) :: elem_id(nf) + integer, intent(in) :: face_id(nf) + character(len=*), intent(in) :: slave_name + character(len=*), intent(in) :: master_name + real(rk), intent(in) :: search_value + + cache_signature_matches = .false. + if (any(cache_elem(:,pair) .ne. elem_id)) return + if (any(cache_face(:,pair) .ne. face_id)) return + if (trim(cache_slave(pair)) .ne. trim(slave_name)) return + if (trim(cache_master_name(pair)) .ne. trim(master_name)) return + if (abs(cache_search(pair) - search_value) .gt. & + 10.0_rk * sqrt(tiny(ONE))) return + cache_signature_matches = .true. + end function cache_signature_matches + + logical function cache_motion_valid(pair, nf, center, radius, search_value) + integer, intent(in) :: pair + integer, intent(in) :: nf + real(rk), intent(in) :: center(NDIM,nf) + real(rk), intent(in) :: radius(nf) + real(rk), intent(in) :: search_value + real(rk) :: limit + real(rk) :: motion + integer :: sf + + limit = max(10.0_rk * sqrt(tiny(ONE)), 0.25_rk * max(ZERO, search_value)) + cache_motion_valid = .false. + do sf = 1, nf + motion = sqrt(sum((center(:,sf) - cache_center(:,sf,pair)) * & + (center(:,sf) - cache_center(:,sf,pair)))) + motion = motion + abs(radius(sf) - cache_radius(sf,pair)) + if (motion .gt. limit) return + end do + cache_motion_valid = .true. + end function cache_motion_valid + + logical function cache_index_valid(pair, sf) + integer, intent(in) :: pair + integer, intent(in) :: sf + + cache_index_valid = .false. + if (.not. allocated(cache_valid)) return + if (pair .lt. 1 .or. pair .gt. cache_npair) return + if (sf .lt. 1 .or. sf .gt. cache_nf) return + cache_index_valid = .true. + end function cache_index_valid + +end module fem_contact_candidate_cache diff --git a/src/boundary/fem_contact_force.f90 b/src/boundary/fem_contact_force.f90 index cc7c9ec..0e68412 100644 --- a/src/boundary/fem_contact_force.f90 +++ b/src/boundary/fem_contact_force.f90 @@ -4,20 +4,26 @@ module fem_contact_force use fem_database use fem_coupling use fem_contact_state - use fem_contact_target, only: facet_surface_matches, contact_pair_search_value, & - contact_pair_friction_value + use fem_contact_target, only: facet_surface_matches, contact_pair_search_value use fem_contact_geometry, only: facet_center_velocity, facet_radius_value, & surface_oriented_contact_normal, contact_mortar_integrate, & - contact_tangent_direction + contact_tangent_direction, contact_mortar_point_weights, & + CONTACT_MAX_MORTAR_QP use fem_contact_search, only: CONTACT_MAX_STACK, contact_pair_candidate, & contact_grid_dimensions, contact_grid_fill, contact_grid_range, & contact_grid_cell, contact_master_count, contact_bvh_build, & contact_aabb_overlap, contact_bvh_slave_weight + use fem_contact_candidate_cache, only: CONTACT_CANDIDATE_CACHE_CAP, & + contact_candidate_cache_try, contact_candidate_cache_store, & + contact_candidate_cache_count, contact_candidate_cache_master + use fem_contact_history, only: contact_history_sum, contact_history_store use fem_contact_friction, only: contact_friction_history implicit none private + integer, parameter :: CONTACT_BVH_CACHE_CAP = 32 + public :: apply_contact_pair_forces contains @@ -25,9 +31,14 @@ contains subroutine apply_contact_pair_forces(ierr) integer, intent(out) :: ierr integer :: pair + real(rk) :: time_start + real(rk) :: time_finish do pair = 1, ncontact_pair + call cpu_time(time_start) call apply_contact_pair_force_one(pair, ierr) + call cpu_time(time_finish) + call ct_pair_force_time(pair, time_finish - time_start) if (ierr .ne. FEM_SUCCESS) return end do @@ -37,23 +48,12 @@ contains subroutine apply_contact_pair_force_one(pair, ierr) integer, intent(in) :: pair integer, intent(out) :: ierr - integer :: nf, max_nodes, stat, q - integer :: grid_nx, grid_ny, grid_nz, grid_ncell, nmaster, bvh_nodes + integer :: nf, max_nodes, stat integer, allocatable :: facet_id(:), elem_id(:), face_id(:), fnnode(:) - integer, allocatable :: fconn(:,:), grid_head(:), grid_next(:) - integer, allocatable :: bvh_item(:), bvh_left(:), bvh_right(:) - integer, allocatable :: bvh_start(:), bvh_count(:) + integer, allocatable :: fconn(:,:) real(rk), allocatable :: x(:,:,:), vnod(:,:,:), normal(:,:), center(:,:) - real(rk), allocatable :: velc(:,:), area(:), radius(:) - real(rk), allocatable :: bvh_min(:,:), bvh_max(:,:) - real(rk) :: mu - real(rk) :: old_tangent(NDIM) + real(rk), allocatable :: area(:), radius(:) real(rk) :: new_tangent(NDIM) - real(rk) :: grid_min(NDIM) - real(rk) :: grid_h(NDIM) - real(rk) :: max_master_radius - logical :: use_grid - logical :: use_bvh call cpl_build_facets(ierr) if (ierr .ne. FEM_SUCCESS) return @@ -67,7 +67,7 @@ contains allocate(facet_id(nf), elem_id(nf), face_id(nf), fnnode(nf), & fconn(max_nodes,nf), x(NDIM,max_nodes,nf), & vnod(NDIM,max_nodes,nf), normal(NDIM,nf), & - center(NDIM,nf), velc(NDIM,nf), area(nf), radius(nf), stat=stat) + center(NDIM,nf), area(nf), radius(nf), stat=stat) if (stat .ne. 0) then call error_set(FEM_ERR_INTERNAL, 'failed to allocate contact pair facets', ierr) return @@ -77,18 +77,82 @@ contains fconn, x, vnod, normal, area, ierr) if (ierr .ne. FEM_SUCCESS) go to 900 + call pair_force_facet_geometry(nf, max_nodes, fnnode, x, vnod, center, radius) + new_tangent = ZERO + call pair_force_clear_tangent(pair) + call apply_pair_force_facets(pair, nf, max_nodes, fnnode, fconn, elem_id, & + face_id, x, vnod, normal, area, radius, & + center, new_tangent, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + call pair_force_store_tangent(pair, new_tangent) + + ierr = FEM_SUCCESS + +900 continue + if (allocated(facet_id)) deallocate(facet_id, elem_id, face_id, fnnode, & + fconn, x, vnod, normal, center, & + area, radius) + end subroutine apply_contact_pair_force_one + + subroutine pair_force_facet_geometry(nf, max_nodes, fnnode, x, vnod, & + center, radius) + integer, intent(in) :: nf, max_nodes + integer, intent(in) :: fnnode(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf) + real(rk), intent(in) :: vnod(NDIM,max_nodes,nf) + real(rk), intent(out) :: center(NDIM,nf) + real(rk), intent(out) :: radius(nf) + real(rk) :: velc(NDIM) + integer :: q + do q = 1, nf call facet_center_velocity(q, nf, max_nodes, fnnode, x, vnod, & - center(:,q), velc(:,q)) + center(:,q), velc) radius(q) = facet_radius_value(q, nf, max_nodes, fnnode, x, center(:,q)) end do - mu = contact_pair_friction_value(pair) - new_tangent = ZERO - call pair_force_take_tangent(pair, old_tangent) + end subroutine pair_force_facet_geometry + + subroutine apply_pair_force_facets(pair, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, area, & + radius, center, new_tangent, ierr) + integer, intent(in) :: pair, nf, max_nodes + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), area(nf), radius(nf) + real(rk), intent(in) :: center(NDIM,nf) + real(rk), intent(inout) :: new_tangent(NDIM) + integer, intent(out) :: ierr + integer :: grid_nx, grid_ny, grid_nz, grid_ncell, nmaster, bvh_nodes + integer, allocatable :: grid_head(:), grid_next(:) + integer, allocatable :: bvh_item(:), bvh_left(:), bvh_right(:) + integer, allocatable :: bvh_start(:), bvh_count(:) + real(rk), allocatable :: bvh_min(:,:), bvh_max(:,:) + real(rk) :: grid_min(NDIM) + real(rk) :: grid_h(NDIM) + real(rk) :: max_master_radius + real(rk) :: time_start + real(rk) :: time_finish + integer :: stat + logical :: use_grid + logical :: use_bvh + logical :: use_cached_candidates + + call cpu_time(time_start) nmaster = contact_master_count(pair, nf, elem_id, face_id) + bvh_nodes = 0 use_bvh = nmaster .gt. 16 use_grid = .false. + use_cached_candidates = .false. if (use_bvh) then + call contact_candidate_cache_try(pair, ncontact_pair, nf, elem_id, & + face_id, center, radius, & + contact_pair_slave(pair), & + contact_pair_master(pair), & + contact_pair_search_value(pair), & + use_cached_candidates) + end if + if (use_bvh .and. .not. use_cached_candidates) 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) @@ -105,6 +169,8 @@ contains grid_min, grid_h, grid_nx, grid_ny, grid_nz, & max_master_radius, use_grid) end if + call contact_record_search_setup(nf, nmaster, bvh_nodes) + call ct_pair_search_setup(pair, nf, nmaster, bvh_nodes) 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) @@ -115,13 +181,24 @@ contains call contact_grid_fill(pair, nf, elem_id, face_id, center, grid_min, grid_h, & grid_nx, grid_ny, grid_nz, grid_head, grid_next) end if + call cpu_time(time_finish) + call contact_record_search_time(time_finish - time_start) + call ct_pair_search_time(pair, time_finish - time_start) if (use_bvh) then - call contact_bvh_apply_pair_forces(pair, nf, max_nodes, fnnode, fconn, & - elem_id, face_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 (use_cached_candidates) then + call contact_cache_apply_forces(pair, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, & + vnod, normal, area, radius, & + center, new_tangent, ierr) + else + call contact_bvh_apply_pair_forces(pair, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, & + area, radius, center, bvh_item, & + bvh_left, bvh_right, bvh_start, & + bvh_count, bvh_min, bvh_max, & + new_tangent, ierr) + end if if (ierr .ne. FEM_SUCCESS) go to 900 go to 850 end if @@ -131,42 +208,33 @@ contains elem_id, face_id, x, vnod, normal, & area, radius, center, grid_min, grid_h, & grid_nx, grid_ny, grid_nz, grid_head, & - grid_next, max_master_radius, old_tangent, & - new_tangent, ierr) + grid_next, max_master_radius, new_tangent, & + ierr) else call apply_pair_force_slaves_all(pair, nf, max_nodes, fnnode, fconn, & elem_id, face_id, x, vnod, normal, & - area, radius, center, old_tangent, & - new_tangent, ierr) + area, radius, center, new_tangent, ierr) end if if (ierr .ne. FEM_SUCCESS) go to 900 850 continue - call pair_force_store_tangent(pair, new_tangent) - ierr = FEM_SUCCESS 900 continue - if (allocated(facet_id)) deallocate(facet_id, elem_id, face_id, fnnode, & - fconn, x, vnod, normal, center, velc, & - area, radius) if (allocated(grid_head)) deallocate(grid_head, grid_next) if (allocated(bvh_item)) deallocate(bvh_item, bvh_left, bvh_right, & bvh_start, bvh_count, bvh_min, bvh_max) - end subroutine apply_contact_pair_force_one + end subroutine apply_pair_force_facets - subroutine pair_force_take_tangent(pair, old_tangent) + subroutine pair_force_clear_tangent(pair) integer, intent(in) :: pair - real(rk), intent(out) :: old_tangent(NDIM) - old_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 - end subroutine pair_force_take_tangent + end subroutine pair_force_clear_tangent subroutine pair_force_store_tangent(pair, new_tangent) integer, intent(in) :: pair @@ -181,14 +249,13 @@ contains subroutine apply_pair_force_slaves_all(pair, nf, max_nodes, fnnode, fconn, & elem_id, face_id, x, vnod, normal, & - area, radius, center, old_tangent, & - new_tangent, ierr) + area, radius, center, new_tangent, ierr) integer, intent(in) :: pair, nf, max_nodes integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) integer, intent(in) :: elem_id(nf), face_id(nf) real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) real(rk), intent(in) :: normal(NDIM,nf), area(nf), radius(nf) - real(rk), intent(in) :: center(NDIM,nf), old_tangent(NDIM) + real(rk), intent(in) :: center(NDIM,nf) real(rk), intent(inout) :: new_tangent(NDIM) integer, intent(out) :: ierr integer :: sf @@ -198,8 +265,7 @@ contains contact_pair_slave(pair))) cycle call apply_pair_force_slave_all(pair, sf, nf, max_nodes, fnnode, fconn, & elem_id, face_id, x, vnod, normal, area, & - radius, center, old_tangent, new_tangent, & - ierr) + radius, center, new_tangent, ierr) if (ierr .ne. FEM_SUCCESS) return end do ierr = FEM_SUCCESS @@ -210,7 +276,7 @@ contains area, radius, center, grid_min, grid_h, & grid_nx, grid_ny, grid_nz, grid_head, & grid_next, max_master_radius, & - old_tangent, new_tangent, ierr) + new_tangent, ierr) integer, intent(in) :: pair, nf, max_nodes integer, intent(in) :: grid_nx, grid_ny, grid_nz integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) @@ -218,7 +284,7 @@ contains real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) real(rk), intent(in) :: normal(NDIM,nf), area(nf), radius(nf) real(rk), intent(in) :: center(NDIM,nf), grid_min(NDIM), grid_h(NDIM) - real(rk), intent(in) :: max_master_radius, old_tangent(NDIM) + real(rk), intent(in) :: max_master_radius real(rk), intent(inout) :: new_tangent(NDIM) integer, intent(out) :: ierr integer :: sf @@ -230,8 +296,7 @@ contains elem_id, face_id, x, vnod, normal, area, & radius, center, grid_min, grid_h, grid_nx, & grid_ny, grid_nz, grid_head, grid_next, & - max_master_radius, old_tangent, & - new_tangent, ierr) + max_master_radius, new_tangent, ierr) if (ierr .ne. FEM_SUCCESS) return end do ierr = FEM_SUCCESS @@ -240,27 +305,40 @@ contains subroutine apply_pair_force_slave_all(pair, sf, nf, max_nodes, fnnode, & fconn, elem_id, face_id, x, vnod, & normal, area, radius, center, & - old_tangent, new_tangent, ierr) + new_tangent, ierr) integer, intent(in) :: pair, sf, nf, max_nodes integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) integer, intent(in) :: elem_id(nf), face_id(nf) real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) real(rk), intent(in) :: normal(NDIM,nf), area(nf), radius(nf) - real(rk), intent(in) :: center(NDIM,nf), old_tangent(NDIM) + real(rk), intent(in) :: center(NDIM,nf) real(rk), intent(inout) :: new_tangent(NDIM) integer, intent(out) :: ierr integer :: mf real(rk) :: gap, vn, total_weight, pair_weight, area_scale + real(rk) :: time_start + real(rk) :: time_finish logical :: active total_weight = ZERO + call cpu_time(time_start) do mf = 1, nf call contact_pair_candidate(pair, sf, mf, nf, max_nodes, fnnode, fconn, & elem_id, face_id, x, vnod, normal, center, & radius, gap, vn, pair_weight, active, ierr) - if (ierr .ne. FEM_SUCCESS) return + if (ierr .ne. FEM_SUCCESS) then + call cpu_time(time_finish) + call contact_record_search_time(time_finish - time_start) + call ct_pair_search_time(pair, time_finish - time_start) + return + end if + call contact_record_search_test(active) + call ct_pair_search_test(pair, active) if (active) total_weight = total_weight + pair_weight end do + call cpu_time(time_finish) + call contact_record_search_time(time_finish - time_start) + call ct_pair_search_time(pair, time_finish - time_start) if (total_weight .le. tiny(ONE)) then ierr = FEM_SUCCESS return @@ -275,7 +353,7 @@ contains if (.not. active) cycle call add_pair_force_state(pair, sf, mf, nf, max_nodes, fnnode, fconn, & elem_id, face_id, x, vnod, normal, gap, vn, & - area_scale, old_tangent, new_tangent, ierr) + area_scale, new_tangent, ierr) if (ierr .ne. FEM_SUCCESS) return end do ierr = FEM_SUCCESS @@ -286,7 +364,7 @@ contains normal, area, radius, center, grid_min, & grid_h, grid_nx, grid_ny, grid_nz, & grid_head, grid_next, max_master_radius, & - old_tangent, new_tangent, ierr) + new_tangent, ierr) integer, intent(in) :: pair, sf, nf, max_nodes integer, intent(in) :: grid_nx, grid_ny, grid_nz integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) @@ -295,12 +373,14 @@ contains real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) real(rk), intent(in) :: normal(NDIM,nf), area(nf), radius(nf) real(rk), intent(in) :: center(NDIM,nf), grid_min(NDIM), grid_h(NDIM) - real(rk), intent(in) :: max_master_radius, old_tangent(NDIM) + real(rk), intent(in) :: max_master_radius real(rk), intent(inout) :: new_tangent(NDIM) integer, intent(out) :: ierr integer :: ix0, ix1, iy0, iy1, iz0, iz1 real(rk) :: total_weight, area_scale real(rk) :: candidate_radius + real(rk) :: time_start + real(rk) :: time_finish candidate_radius = radius(sf) + max_master_radius + & contact_pair_search_value(pair) @@ -308,11 +388,15 @@ contains grid_nx, grid_ny, grid_nz, ix0, ix1, iy0, iy1, & iz0, iz1) total_weight = ZERO + call cpu_time(time_start) call pair_force_grid_pass(pair, sf, nf, max_nodes, fnnode, fconn, elem_id, & face_id, x, vnod, normal, center, radius, & grid_nx, grid_ny, grid_nz, grid_head, grid_next, & ix0, ix1, iy0, iy1, iz0, iz1, ZERO, & - old_tangent, new_tangent, total_weight, .false., ierr) + new_tangent, total_weight, .false., ierr) + call cpu_time(time_finish) + call contact_record_search_time(time_finish - time_start) + call ct_pair_search_time(pair, time_finish - time_start) if (ierr .ne. FEM_SUCCESS) return if (total_weight .le. tiny(ONE)) then ierr = FEM_SUCCESS @@ -324,15 +408,15 @@ contains face_id, x, vnod, normal, center, radius, & grid_nx, grid_ny, grid_nz, grid_head, grid_next, & ix0, ix1, iy0, iy1, iz0, iz1, area_scale, & - old_tangent, new_tangent, total_weight, .true., ierr) + new_tangent, total_weight, .true., ierr) end subroutine apply_pair_force_slave_grid subroutine pair_force_grid_pass(pair, sf, nf, max_nodes, fnnode, fconn, & elem_id, face_id, x, vnod, normal, center, & radius, grid_nx, grid_ny, grid_nz, grid_head, & grid_next, ix0, ix1, iy0, iy1, iz0, iz1, & - area_scale, old_tangent, new_tangent, & - total_weight, apply_force, ierr) + area_scale, new_tangent, total_weight, & + apply_force, ierr) integer, intent(in) :: pair, sf, nf, max_nodes integer, intent(in) :: grid_nx, grid_ny, grid_nz integer, intent(in) :: ix0, ix1, iy0, iy1, iz0, iz1 @@ -341,7 +425,7 @@ contains integer, intent(in) :: grid_head(:), grid_next(nf) real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) real(rk), intent(in) :: normal(NDIM,nf), center(NDIM,nf), radius(nf) - real(rk), intent(in) :: area_scale, old_tangent(NDIM) + real(rk), intent(in) :: area_scale real(rk), intent(inout) :: new_tangent(NDIM), total_weight logical, intent(in) :: apply_force integer, intent(out) :: ierr @@ -360,11 +444,15 @@ contains normal, center, radius, gap, vn, & pair_weight, active, ierr) if (ierr .ne. FEM_SUCCESS) return + if (.not. apply_force) then + call contact_record_search_test(active) + call ct_pair_search_test(pair, active) + end if if (active .and. apply_force) then call add_pair_force_state(pair, sf, mf, nf, max_nodes, fnnode, & fconn, elem_id, face_id, x, vnod, & normal, gap, vn, area_scale, & - old_tangent, new_tangent, ierr) + new_tangent, ierr) if (ierr .ne. FEM_SUCCESS) return else if (active) then total_weight = total_weight + pair_weight @@ -388,8 +476,8 @@ contains end function contact_area_scale subroutine add_pair_force_state(pair, sf, mf, nf, max_nodes, fnnode, fconn, & - elem_id, face_id, x, vnod, normal, gap, vn, area_scale, & - old_tangent, new_tangent, ierr) + elem_id, face_id, x, vnod, normal, gap, vn, & + area_scale, new_tangent, ierr) integer, intent(in) :: pair integer, intent(in) :: sf integer, intent(in) :: mf @@ -405,24 +493,113 @@ contains 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 + logical :: do_add 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) + real(rk) :: qweight(CONTACT_MAX_MORTAR_QP) + integer :: qcount + + call contact_prepare_force_state(pair, sf, mf, nf, max_nodes, fnnode, & + elem_id, face_id, x, vnod, normal, & + gap, vn, area_scale, do_add, & + nrm, tdir, vt_norm, normal_mag, & + contact_area, mortar_area, slave_w, & + master_w, qcount, qweight, ierr) + if (ierr .ne. FEM_SUCCESS) return + if (.not. do_add) return + call contact_commit_force_state(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, gap, nrm, tdir, & + vt_norm, normal_mag, contact_area, & + mortar_area, slave_w, master_w, qcount, & + qweight, area_scale, new_tangent, ierr) + end subroutine add_pair_force_state + + subroutine add_pair_force_state_omp(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, & + normal, gap, vn, area_scale, & + new_tangent, parallel_ierr) + integer, intent(in) :: pair, sf, mf, nf, max_nodes + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), gap, vn, area_scale + real(rk), intent(inout) :: new_tangent(NDIM) + integer, intent(inout) :: parallel_ierr + logical :: do_add + integer :: local_ierr, qcount + real(rk) :: nrm(NDIM), tdir(NDIM), vt_norm, normal_mag + real(rk) :: contact_area, mortar_area + real(rk) :: slave_w(max_nodes), master_w(max_nodes) + real(rk) :: qweight(CONTACT_MAX_MORTAR_QP) + + if (parallel_ierr .ne. FEM_SUCCESS) return + call contact_prepare_force_state(pair, sf, mf, nf, max_nodes, fnnode, & + elem_id, face_id, x, vnod, normal, & + gap, vn, area_scale, do_add, & + nrm, tdir, vt_norm, normal_mag, & + contact_area, mortar_area, slave_w, & + master_w, qcount, qweight, 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 (.not. do_add) return + +!$omp critical(femlite_contact_force) + if (parallel_ierr .eq. FEM_SUCCESS) then + call contact_commit_force_state(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, gap, nrm, tdir, & + vt_norm, normal_mag, contact_area, & + mortar_area, slave_w, master_w, & + qcount, qweight, area_scale, & + new_tangent, local_ierr) + if (local_ierr .ne. FEM_SUCCESS) parallel_ierr = local_ierr + end if +!$omp end critical(femlite_contact_force) + end subroutine add_pair_force_state_omp + + subroutine contact_prepare_force_state(pair, sf, mf, nf, max_nodes, fnnode, & + elem_id, face_id, x, vnod, normal, & + gap, vn, area_scale, do_add, & + nrm, tdir, vt_norm, normal_mag, & + contact_area, mortar_area, slave_w, & + master_w, qcount, qweight, ierr) + integer, intent(in) :: pair, sf, mf, nf, max_nodes + integer, intent(in) :: fnnode(nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), gap, vn, area_scale + logical, intent(out) :: do_add + real(rk), intent(out) :: nrm(NDIM), tdir(NDIM), vt_norm, normal_mag + real(rk), intent(out) :: contact_area, mortar_area + real(rk), intent(out) :: slave_w(max_nodes), master_w(max_nodes) + integer, intent(out) :: qcount + real(rk), intent(out) :: qweight(CONTACT_MAX_MORTAR_QP) + integer, intent(out) :: ierr + real(rk) :: ss_w(max_nodes,max_nodes), sm_w(max_nodes,max_nodes) + real(rk) :: ms_w(max_nodes,max_nodes), mm_w(max_nodes,max_nodes) + + do_add = .false. + nrm = ZERO + tdir = ZERO + vt_norm = ZERO + normal_mag = ZERO + contact_area = ZERO + mortar_area = ZERO + slave_w = ZERO + master_w = ZERO + qcount = 0 + qweight = ZERO nrm = surface_oriented_contact_normal(normal(:,mf), elem_id(mf), & face_id(mf), & @@ -432,20 +609,56 @@ contains mortar_area, slave_w, master_w, ss_w, & sm_w, ms_w, mm_w, ierr) if (ierr .ne. FEM_SUCCESS) return + call contact_mortar_point_weights(sf, mf, nf, max_nodes, fnnode, x, nrm, & + CONTACT_MAX_MORTAR_QP, qcount, qweight, & + 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 + normal_mag = contact_pair_normal_pressure(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) + do_add = .true. + ierr = FEM_SUCCESS + end subroutine contact_prepare_force_state + + subroutine contact_commit_force_state(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, gap, nrm, tdir, & + vt_norm, normal_mag, contact_area, & + mortar_area, slave_w, master_w, & + qcount, qweight, area_scale, & + new_tangent, ierr) + integer, intent(in) :: pair, sf, mf, nf, max_nodes + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf), qcount + real(rk), intent(in) :: gap, nrm(NDIM), tdir(NDIM), vt_norm, normal_mag + real(rk), intent(in) :: contact_area, mortar_area, area_scale + real(rk), intent(in) :: slave_w(max_nodes), master_w(max_nodes) + real(rk), intent(in) :: qweight(CONTACT_MAX_MORTAR_QP) + real(rk), intent(inout) :: new_tangent(NDIM) + integer, intent(out) :: ierr + real(rk) :: slip_delta, friction_work + real(rk) :: local_tangent(NDIM), force_vec(NDIM), traction(NDIM) + real(rk) :: friction_force(NDIM) + logical :: sliding + + call contact_history_sum(pair, elem_id(sf), face_id(sf), elem_id(mf), & + face_id(mf), local_tangent) + call contact_friction_history(pair, normal_mag, contact_area, local_tangent, & + tdir, vt_norm, friction_force, slip_delta, & + friction_work, sliding) + call store_mortar_history(pair, elem_id(sf), face_id(sf), elem_id(mf), & + face_id(mf), qcount, qweight, mortar_area, & + friction_force, slip_delta, friction_work, & + sliding, ierr) + if (ierr .ne. FEM_SUCCESS) return new_tangent = new_tangent + friction_force force_vec = normal_mag * nrm + friction_force traction = force_vec / contact_area @@ -456,14 +669,286 @@ contains if (ierr .ne. FEM_SUCCESS) return call contact_record_status(pair, -gap, force_vec) ierr = FEM_SUCCESS - end subroutine add_pair_force_state + end subroutine contact_commit_force_state + + subroutine store_mortar_history(pair, selem, sface, melem, mface, qcount, & + qweight, mortar_area, friction_force, & + slip_delta, friction_work, sliding, ierr) + integer, intent(in) :: pair + integer, intent(in) :: selem + integer, intent(in) :: sface + integer, intent(in) :: melem + integer, intent(in) :: mface + integer, intent(in) :: qcount + real(rk), intent(in) :: qweight(CONTACT_MAX_MORTAR_QP) + real(rk), intent(in) :: mortar_area + real(rk), intent(in) :: friction_force(NDIM) + real(rk), intent(in) :: slip_delta + real(rk), intent(in) :: friction_work + logical, intent(in) :: sliding + integer, intent(out) :: ierr + real(rk) :: frac + integer :: q + + if (qcount .le. 0 .or. mortar_area .le. tiny(ONE)) then + call contact_history_store(pair, selem, sface, melem, mface, 1, & + friction_force, slip_delta, friction_work, sliding, ierr) + return + end if + + do q = 1, qcount + frac = qweight(q) / mortar_area + call contact_history_store(pair, selem, sface, melem, mface, q, & + frac * friction_force, slip_delta, frac * friction_work, sliding, & + ierr) + if (ierr .ne. FEM_SUCCESS) return + end do + + ierr = FEM_SUCCESS + end subroutine store_mortar_history + + subroutine contact_cache_apply_forces(pair, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, & + normal, area, radius, center, & + new_tangent, ierr) + integer, intent(in) :: pair, nf, max_nodes + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), area(nf), radius(nf) + real(rk), intent(in) :: center(NDIM,nf) + real(rk), intent(inout) :: new_tangent(NDIM) + integer, intent(out) :: ierr + real(rk), allocatable :: total_weight(:), area_scale(:) + real(rk), allocatable :: cache_gap(:,:), cache_vn(:,:) + integer, allocatable :: cache_count(:), cache_master(:,:) + logical, allocatable :: cache_overflow(:) + integer :: sf, stat, parallel_ierr + integer :: cache_used_total, cache_overflow_total + + allocate(total_weight(nf), area_scale(nf), cache_count(nf), & + cache_master(CONTACT_BVH_CACHE_CAP,nf), & + cache_gap(CONTACT_BVH_CACHE_CAP,nf), & + cache_vn(CONTACT_BVH_CACHE_CAP,nf), cache_overflow(nf), & + stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, & + 'failed to allocate cached contact weights', ierr) + return + end if + + call contact_cand_prepare_wts(pair, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, & + area, radius, center, total_weight, & + area_scale, cache_count, cache_master, & + cache_gap, cache_vn, cache_overflow, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + + call contact_bvh_count_cache_use(pair, nf, elem_id, face_id, total_weight, & + cache_overflow, cache_used_total, & + cache_overflow_total) + call contact_record_cache_counts(cache_used_total, cache_overflow_total) + call ct_pair_cache_counts(pair, cache_used_total, cache_overflow_total) + + parallel_ierr = FEM_SUCCESS +!$omp parallel do schedule(dynamic) private(sf) + do sf = 1, nf + if (parallel_ierr .eq. FEM_SUCCESS) then + if (facet_surface_matches(elem_id(sf), face_id(sf), & + contact_pair_slave(pair)) .and. & + total_weight(sf) .gt. tiny(ONE)) then + if (cache_overflow(sf)) then + call contact_cand_slave_force(pair, sf, nf, max_nodes, & + fnnode, fconn, elem_id, & + face_id, x, vnod, normal, & + center, radius, area_scale(sf), & + new_tangent, parallel_ierr) + else + call contact_bvh_slave_cached_force(pair, sf, nf, max_nodes, & + fnnode, fconn, elem_id, & + face_id, x, vnod, normal, & + cache_count(sf), & + cache_master(:,sf), & + cache_gap(:,sf), & + cache_vn(:,sf), & + area_scale(sf), new_tangent, & + parallel_ierr) + end if + end if + end if + end do +!$omp end parallel do + + ierr = parallel_ierr +900 continue + deallocate(total_weight, area_scale, cache_count, cache_master, & + cache_gap, cache_vn, cache_overflow) + end subroutine contact_cache_apply_forces + + subroutine contact_cand_prepare_wts(pair, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, & + vnod, normal, area, radius, & + center, total_weight, area_scale, & + cache_count, cache_master, cache_gap, & + cache_vn, cache_overflow, ierr) + integer, intent(in) :: pair, nf, max_nodes + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), area(nf), radius(nf) + real(rk), intent(in) :: center(NDIM,nf) + real(rk), intent(out) :: total_weight(nf), area_scale(nf) + real(rk), intent(out) :: cache_gap(:,:), cache_vn(:,:) + integer, intent(out) :: cache_count(nf), cache_master(:,:) + logical, intent(out) :: cache_overflow(nf) + integer, intent(out) :: ierr + real(rk) :: time_start, time_finish, local_weight + integer :: sf, item, mf, item_count, local_ierr + integer :: broad_total, active_total, parallel_ierr + + call contact_bvh_init_cache_arrays(nf, total_weight, area_scale, & + cache_count, cache_master, cache_gap, & + cache_vn, cache_overflow) + broad_total = 0 + active_total = 0 + parallel_ierr = FEM_SUCCESS + call cpu_time(time_start) +!$omp parallel do schedule(dynamic) private(sf,item,mf,item_count,local_ierr,local_weight) & +!$omp& reduction(+:broad_total,active_total) + do sf = 1, nf + if (parallel_ierr .eq. FEM_SUCCESS) then + if (facet_surface_matches(elem_id(sf), face_id(sf), & + contact_pair_slave(pair))) then + item_count = contact_candidate_cache_count(pair, sf) + do item = 1, item_count + mf = contact_candidate_cache_master(pair, sf, item) + if (mf .le. 0) cycle + call contact_candidate_weight(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, normal, center, radius, & + cache_count(sf), cache_master(:,sf), cache_gap(:,sf), & + cache_vn(:,sf), cache_overflow(sf), local_weight, & + active_total, broad_total, 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) = total_weight(sf) + local_weight + end if + end do + if (area(sf) .gt. tiny(ONE) .and. total_weight(sf) .gt. area(sf)) then + area_scale(sf) = area(sf) / total_weight(sf) + end if + end if + end if + end do +!$omp end parallel do + call contact_record_search_counts(broad_total, active_total) + call ct_pair_search_counts(pair, broad_total, active_total) + call cpu_time(time_finish) + call contact_record_search_time(time_finish - time_start) + call ct_pair_search_time(pair, time_finish - time_start) + ierr = parallel_ierr + end subroutine contact_cand_prepare_wts + + subroutine contact_candidate_weight(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, & + normal, center, radius, cache_count, & + cache_master, cache_gap, cache_vn, & + cache_overflow, local_weight, & + active_total, broad_total, ierr) + integer, intent(in) :: pair, sf, mf, nf, max_nodes + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), center(NDIM,nf), radius(nf) + integer, intent(inout) :: cache_count, cache_master(:) + real(rk), intent(inout) :: cache_gap(:), cache_vn(:) + logical, intent(inout) :: cache_overflow + real(rk), intent(out) :: local_weight + integer, intent(inout) :: active_total, broad_total + integer, intent(out) :: ierr + real(rk) :: gap, vn, pair_weight + logical :: active + + local_weight = ZERO + call contact_pair_candidate(pair, sf, mf, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, center, & + radius, gap, vn, pair_weight, active, ierr) + if (ierr .ne. FEM_SUCCESS) return + broad_total = broad_total + 1 + if (active) then + active_total = active_total + 1 + local_weight = pair_weight + call contact_cache_active(mf, gap, vn, cache_count, cache_master, & + cache_gap, cache_vn, cache_overflow) + end if + end subroutine contact_candidate_weight + + subroutine contact_cache_active(mf, gap, vn, cache_count, cache_master, & + cache_gap, cache_vn, cache_overflow) + integer, intent(in) :: mf + real(rk), intent(in) :: gap, vn + integer, intent(inout) :: cache_count, cache_master(:) + real(rk), intent(inout) :: cache_gap(:), cache_vn(:) + logical, intent(inout) :: cache_overflow + integer :: pos + + pos = cache_count + 1 + if (pos .gt. min(size(cache_master), size(cache_gap), size(cache_vn))) then + cache_overflow = .true. + return + end if + cache_count = pos + cache_master(pos) = mf + cache_gap(pos) = gap + cache_vn(pos) = vn + end subroutine contact_cache_active + + subroutine contact_cand_slave_force(pair, sf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, & + normal, center, radius, area_scale, & + new_tangent, parallel_ierr) + integer, intent(in) :: pair, sf, nf, max_nodes + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), center(NDIM,nf), radius(nf) + real(rk), intent(in) :: area_scale + real(rk), intent(inout) :: new_tangent(NDIM) + integer, intent(inout) :: parallel_ierr + real(rk) :: gap, vn, pair_weight + integer :: item, item_count, mf, local_ierr + logical :: active + + item_count = contact_candidate_cache_count(pair, sf) + do item = 1, item_count + if (parallel_ierr .ne. FEM_SUCCESS) return + mf = contact_candidate_cache_master(pair, sf, item) + if (mf .le. 0) cycle + call contact_pair_candidate(pair, sf, mf, nf, max_nodes, fnnode, fconn, & + elem_id, face_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 (.not. active) cycle + call add_pair_force_state_omp(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, normal, & + gap, vn, area_scale, new_tangent, & + parallel_ierr) + end do + end subroutine contact_cand_slave_force subroutine contact_bvh_apply_pair_forces(pair, nf, max_nodes, fnnode, fconn, & elem_id, face_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) + bvh_min, bvh_max, new_tangent, ierr) integer, intent(in) :: pair integer, intent(in) :: nf integer, intent(in) :: max_nodes @@ -484,85 +969,272 @@ contains 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 + real(rk), allocatable :: cache_gap(:,:) + real(rk), allocatable :: cache_vn(:,:) + integer, allocatable :: cache_count(:) + integer, allocatable :: cache_master(:,:) + integer, allocatable :: broad_count(:) + integer, allocatable :: broad_master(:,:) + logical, allocatable :: cache_overflow(:) + logical, allocatable :: broad_overflow(:) integer :: sf integer :: stat - integer :: local_ierr integer :: parallel_ierr + integer :: cache_used_total + integer :: cache_overflow_total - allocate(total_weight(nf), area_scale(nf), stat=stat) + allocate(total_weight(nf), area_scale(nf), cache_count(nf), & + cache_master(CONTACT_BVH_CACHE_CAP,nf), & + cache_gap(CONTACT_BVH_CACHE_CAP,nf), & + cache_vn(CONTACT_BVH_CACHE_CAP,nf), cache_overflow(nf), & + broad_count(nf), broad_master(CONTACT_CANDIDATE_CACHE_CAP,nf), & + broad_overflow(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_surface_matches(elem_id(sf), face_id(sf), & - contact_pair_slave(pair))) then - call contact_bvh_slave_weight(pair, sf, nf, max_nodes, fnnode, fconn, & - elem_id, face_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) + call contact_bvh_prepare_weights(pair, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, & + area, radius, center, bvh_item, bvh_left, & + bvh_right, bvh_start, bvh_count, bvh_min, & + bvh_max, total_weight, area_scale, & + cache_count, cache_master, cache_gap, & + cache_vn, cache_overflow, broad_count, & + broad_master, broad_overflow, ierr) + if (ierr .ne. FEM_SUCCESS) then + deallocate(total_weight, area_scale, cache_count, cache_master, & + cache_gap, cache_vn, cache_overflow, broad_count, & + broad_master, broad_overflow) return end if + call contact_candidate_cache_store(pair, ncontact_pair, nf, elem_id, & + face_id, center, radius, contact_pair_slave(pair), & + contact_pair_master(pair), contact_pair_search_value(pair), & + broad_count, broad_master, broad_overflow) + call contact_bvh_count_cache_use(pair, nf, elem_id, face_id, total_weight, & + cache_overflow, cache_used_total, & + cache_overflow_total) + call contact_record_cache_counts(cache_used_total, cache_overflow_total) + call ct_pair_cache_counts(pair, cache_used_total, cache_overflow_total) + + parallel_ierr = FEM_SUCCESS !$omp parallel do schedule(dynamic) private(sf) do sf = 1, nf if (parallel_ierr .eq. FEM_SUCCESS) then if (facet_surface_matches(elem_id(sf), face_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, face_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) + if (cache_overflow(sf)) then + call contact_bvh_slave_force(pair, sf, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, & + center, radius, bvh_item, bvh_left, & + bvh_right, bvh_start, bvh_count, & + bvh_min, bvh_max, area_scale(sf), & + new_tangent, parallel_ierr) + else + call contact_bvh_slave_cached_force(pair, sf, nf, max_nodes, & + fnnode, fconn, elem_id, & + face_id, x, vnod, normal, & + cache_count(sf), & + cache_master(:,sf), & + cache_gap(:,sf), & + cache_vn(:,sf), & + area_scale(sf), new_tangent, & + parallel_ierr) + end if end if end if end do !$omp end parallel do ierr = parallel_ierr - deallocate(total_weight) - deallocate(area_scale) + deallocate(total_weight, area_scale, cache_count, cache_master, & + cache_gap, cache_vn, cache_overflow, broad_count, & + broad_master, broad_overflow) end subroutine contact_bvh_apply_pair_forces + subroutine contact_bvh_prepare_weights(pair, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, & + area, radius, center, bvh_item, & + bvh_left, bvh_right, bvh_start, & + bvh_count, bvh_min, bvh_max, & + total_weight, area_scale, cache_count, & + cache_master, cache_gap, cache_vn, & + cache_overflow, broad_count, & + broad_master, broad_overflow, ierr) + integer, intent(in) :: pair, nf, max_nodes + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + integer, intent(in) :: bvh_item(:), bvh_left(:), bvh_right(:) + integer, intent(in) :: bvh_start(:), bvh_count(:) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), area(nf), radius(nf) + real(rk), intent(in) :: center(NDIM,nf), bvh_min(:,:), bvh_max(:,:) + real(rk), intent(out) :: total_weight(nf), area_scale(nf) + real(rk), intent(out) :: cache_gap(:,:), cache_vn(:,:) + integer, intent(out) :: cache_count(nf), cache_master(:,:) + logical, intent(out) :: cache_overflow(nf) + integer, intent(out) :: broad_count(nf), broad_master(:,:) + logical, intent(out) :: broad_overflow(nf) + integer, intent(out) :: ierr + real(rk) :: local_weight, time_start, time_finish + integer :: sf, local_ierr, parallel_ierr, local_broad, local_active + integer :: broad_total, active_total + + call contact_bvh_init_cache_arrays(nf, total_weight, area_scale, & + cache_count, cache_master, cache_gap, & + cache_vn, cache_overflow) + broad_count = 0 + broad_master = 0 + broad_overflow = .false. + parallel_ierr = FEM_SUCCESS + broad_total = 0 + active_total = 0 + call cpu_time(time_start) +!$omp parallel do schedule(dynamic) private(sf,local_weight,local_ierr,local_broad,local_active) & +!$omp& reduction(+:broad_total,active_total) + do sf = 1, nf + if (parallel_ierr .eq. FEM_SUCCESS) then + if (facet_surface_matches(elem_id(sf), face_id(sf), & + contact_pair_slave(pair))) then + call contact_bvh_slave_weight(pair, sf, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, center, radius, bvh_item, & + bvh_left, bvh_right, bvh_start, bvh_count, bvh_min, bvh_max, & + local_weight, local_broad, local_active, local_ierr, & + CONTACT_BVH_CACHE_CAP, cache_count(sf), cache_master(:,sf), & + cache_gap(:,sf), cache_vn(:,sf), cache_overflow(sf), & + CONTACT_CANDIDATE_CACHE_CAP, broad_count(sf), & + broad_master(:,sf), broad_overflow(sf)) + call contact_bvh_store_weight(sf, area(sf), local_weight, & + local_ierr, parallel_ierr, total_weight, area_scale) + if (local_ierr .eq. FEM_SUCCESS) then + broad_total = broad_total + max(0, local_broad) + active_total = active_total + max(0, local_active) + end if + end if + end if + end do +!$omp end parallel do + call contact_record_search_counts(broad_total, active_total) + call ct_pair_search_counts(pair, broad_total, active_total) + call cpu_time(time_finish) + call contact_record_search_time(time_finish - time_start) + call ct_pair_search_time(pair, time_finish - time_start) + ierr = parallel_ierr + end subroutine contact_bvh_prepare_weights + + subroutine contact_bvh_init_cache_arrays(nf, total_weight, area_scale, & + cache_count, cache_master, & + cache_gap, cache_vn, cache_overflow) + integer, intent(in) :: nf + real(rk), intent(out) :: total_weight(nf), area_scale(nf) + integer, intent(out) :: cache_count(nf), cache_master(:,:) + real(rk), intent(out) :: cache_gap(:,:), cache_vn(:,:) + logical, intent(out) :: cache_overflow(nf) + + total_weight = ZERO + area_scale = ONE + cache_count = 0 + cache_master = 0 + cache_gap = ZERO + cache_vn = ZERO + cache_overflow = .false. + end subroutine contact_bvh_init_cache_arrays + + subroutine contact_bvh_store_weight(sf, slave_area, local_weight, local_ierr, & + parallel_ierr, total_weight, area_scale) + integer, intent(in) :: sf, local_ierr + real(rk), intent(in) :: slave_area, local_weight + integer, intent(inout) :: parallel_ierr + real(rk), intent(inout) :: total_weight(:), area_scale(:) + + 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 + total_weight(sf) = local_weight + if (slave_area .gt. tiny(ONE) .and. local_weight .gt. slave_area) then + area_scale(sf) = slave_area / local_weight + end if + end subroutine contact_bvh_store_weight + + subroutine contact_bvh_count_cache_use(pair, nf, elem_id, face_id, & + total_weight, cache_overflow, & + used_count, overflow_count) + integer, intent(in) :: pair + integer, intent(in) :: nf + integer, intent(in) :: elem_id(nf) + integer, intent(in) :: face_id(nf) + real(rk), intent(in) :: total_weight(nf) + logical, intent(in) :: cache_overflow(nf) + integer, intent(out) :: used_count + integer, intent(out) :: overflow_count + integer :: sf + + used_count = 0 + overflow_count = 0 + do sf = 1, nf + if (.not. facet_surface_matches(elem_id(sf), face_id(sf), & + contact_pair_slave(pair))) cycle + if (total_weight(sf) .le. tiny(ONE)) cycle + if (cache_overflow(sf)) then + overflow_count = overflow_count + 1 + else + used_count = used_count + 1 + end if + end do + end subroutine contact_bvh_count_cache_use + + subroutine contact_bvh_slave_cached_force(pair, sf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, & + normal, cache_count, cache_master, & + cache_gap, cache_vn, area_scale, & + 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) + integer, intent(in) :: face_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) + integer, intent(in) :: cache_count + integer, intent(in) :: cache_master(:) + real(rk), intent(in) :: cache_gap(:) + real(rk), intent(in) :: cache_vn(:) + real(rk), intent(in) :: area_scale + real(rk), intent(inout) :: new_tangent(NDIM) + integer, intent(inout) :: parallel_ierr + integer :: item + integer :: mf + + if (cache_count .le. 0) return + + do item = 1, cache_count + if (parallel_ierr .ne. FEM_SUCCESS) return + mf = cache_master(item) + call add_pair_force_state_omp(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, normal, & + cache_gap(item), cache_vn(item), & + area_scale, new_tangent, parallel_ierr) + end do + end subroutine contact_bvh_slave_cached_force + subroutine contact_bvh_slave_force(pair, sf, nf, max_nodes, fnnode, fconn, & elem_id, face_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) + area_scale, new_tangent, parallel_ierr) integer, intent(in) :: pair integer, intent(in) :: sf integer, intent(in) :: nf @@ -584,7 +1256,6 @@ contains 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) @@ -628,18 +1299,9 @@ contains !$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, elem_id, face_id, 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 + if (active) call add_pair_force_state_omp(pair, sf, mf, nf, & + max_nodes, fnnode, fconn, elem_id, face_id, x, vnod, normal, & + gap, vn, area_scale, new_tangent, parallel_ierr) end do else if (bvh_left(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then diff --git a/src/boundary/fem_contact_friction.f90 b/src/boundary/fem_contact_friction.f90 index f92eb7f..7cee64c 100644 --- a/src/boundary/fem_contact_friction.f90 +++ b/src/boundary/fem_contact_friction.f90 @@ -11,7 +11,8 @@ module fem_contact_friction contains subroutine contact_friction_history(pair, normal_mag, contact_area, & - old_tangent, tdir, vt_norm, friction_force) + old_tangent, tdir, vt_norm, friction_force, & + slip_delta, work_value, sliding) integer, intent(in) :: pair real(rk), intent(in) :: normal_mag real(rk), intent(in) :: contact_area @@ -19,6 +20,9 @@ contains real(rk), intent(in) :: tdir(NDIM) real(rk), intent(in) :: vt_norm real(rk), intent(out) :: friction_force(NDIM) + real(rk), intent(out) :: slip_delta + real(rk), intent(out) :: work_value + logical, intent(out) :: sliding real(rk) :: mu real(rk) :: kt real(rk) :: trial(NDIM) @@ -27,6 +31,9 @@ contains real(rk) :: slip_inc friction_force = ZERO + slip_delta = ZERO + work_value = ZERO + sliding = .false. mu = contact_pair_friction_value(pair) if (mu .le. ZERO .or. normal_mag .le. ZERO .or. contact_area .le. tiny(ONE)) return @@ -34,7 +41,7 @@ contains slip_inc = vt_norm trial = old_tangent - kt * slip_inc * tdir trial_norm = sqrt(sum(trial * trial)) - limit_force = mu * normal_mag + limit_force = friction_limit(pair, mu, normal_mag, kt, slip_inc, trial_norm) if (trial_norm .le. limit_force + tiny(ONE)) then friction_force = trial @@ -45,18 +52,64 @@ contains end if else friction_force = limit_force * trial / max(trial_norm, tiny(ONE)) + sliding = .true. 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 + slip_delta = max(ZERO, (trial_norm - limit_force) / kt) 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) + contact_pair_slip(pair) = contact_pair_slip(pair) + slip_delta end if end if + work_value = max(ZERO, -sum(friction_force * (slip_inc * tdir))) + call record_friction_work(pair, work_value) end if end subroutine contact_friction_history + real(rk) function friction_limit(pair, mu, normal_mag, kt, slip_inc, trial_norm) + integer, intent(in) :: pair + real(rk), intent(in) :: mu + real(rk), intent(in) :: normal_mag + real(rk), intent(in) :: kt + real(rk), intent(in) :: slip_inc + real(rk), intent(in) :: trial_norm + real(rk) :: reg + real(rk) :: equiv_slip + + friction_limit = mu * normal_mag + reg = contact_pair_regularization(pair) + if (reg .le. ZERO) return + equiv_slip = max(abs(slip_inc), trial_norm / max(kt, tiny(ONE))) + friction_limit = friction_limit * tanh(equiv_slip / reg) + end function friction_limit + + real(rk) function contact_pair_regularization(pair) + integer, intent(in) :: pair + + contact_pair_regularization = ZERO + if (.not. allocated(contact_pair_friction_reg)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_friction_reg)) return + contact_pair_regularization = max(ZERO, contact_pair_friction_reg(pair)) + end function contact_pair_regularization + + subroutine record_friction_work(pair, work_value) + integer, intent(in) :: pair + real(rk), intent(in) :: work_value + + if (work_value .le. ZERO) return + contact_friction_work_step = contact_friction_work_step + work_value + contact_friction_work_total = contact_friction_work_total + work_value + if (allocated(contact_pair_friction_work)) then + if (pair .ge. 1 .and. pair .le. size(contact_pair_friction_work)) then + contact_pair_friction_work(pair) = & + contact_pair_friction_work(pair) + work_value + contact_pair_fric_work_total(pair) = & + contact_pair_fric_work_total(pair) + work_value + end if + end if + end subroutine record_friction_work + end module fem_contact_friction diff --git a/src/boundary/fem_contact_geometry.f90 b/src/boundary/fem_contact_geometry.f90 index e9762a9..0cd4ae0 100644 --- a/src/boundary/fem_contact_geometry.f90 +++ b/src/boundary/fem_contact_geometry.f90 @@ -8,13 +8,16 @@ module fem_contact_geometry private integer, parameter :: CONTACT_MAX_CLIP = 64 + integer, parameter :: CONTACT_MAX_MORTAR_QP = (CONTACT_MAX_CLIP - 2) * 7 public :: CONTACT_MAX_CLIP + public :: CONTACT_MAX_MORTAR_QP public :: normalized_contact_normal public :: surface_oriented_contact_normal public :: contact_tangent_direction public :: pair_contact_gap public :: contact_mortar_integrate + public :: contact_mortar_point_weights public :: facet_center_velocity public :: facet_radius_value @@ -344,6 +347,88 @@ contains ierr = FEM_SUCCESS end subroutine contact_mortar_integrate + subroutine contact_mortar_point_weights(sf, mf, nf, max_nodes, fnnode, x, & + nrm, max_qp, qcount, qweight, ierr) + 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) :: max_qp + real(rk), intent(in) :: x(NDIM,max_nodes,nf) + real(rk), intent(in) :: nrm(NDIM) + integer, intent(out) :: qcount + real(rk), intent(out) :: qweight(max_qp) + integer, intent(out) :: ierr + real(rk) :: basis1(NDIM), basis2(NDIM), origin(NDIM), point(NDIM) + real(rk) :: mpoly(2,CONTACT_MAX_CLIP), spoly(2,CONTACT_MAX_CLIP) + real(rk) :: poly(2,CONTACT_MAX_CLIP), clipped(2,CONTACT_MAX_CLIP) + real(rk) :: tri_area, signed_area + real(rk) :: bary(3,7), rule_weight(7) + integer :: i, j, k, npoly, nclip + + qcount = 0 + qweight = ZERO + if (fnnode(sf) .gt. CONTACT_MAX_CLIP .or. & + fnnode(mf) .gt. CONTACT_MAX_CLIP) then + call error_set(FEM_ERR_COUPLING, 'contact mortar point 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 + + call contact_triangle_quadrature_7(bary, rule_weight) + 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 + if (qcount .ge. max_qp) then + call error_set(FEM_ERR_COUPLING, 'contact mortar qpoint overflow', ierr) + return + end if + qcount = qcount + 1 + qweight(qcount) = tri_area * rule_weight(k) + end do + end do + + ierr = FEM_SUCCESS + end subroutine contact_mortar_point_weights + subroutine contact_triangle_quadrature_7(bary, qweight) real(rk), intent(out) :: bary(3,7) real(rk), intent(out) :: qweight(7) diff --git a/src/boundary/fem_contact_history.f90 b/src/boundary/fem_contact_history.f90 new file mode 100644 index 0000000..ebb4ae6 --- /dev/null +++ b/src/boundary/fem_contact_history.f90 @@ -0,0 +1,385 @@ +module fem_contact_history + use fem_precision + use fem_error + implicit none + + private + + public :: contact_history_old + public :: contact_history_store + public :: contact_history_sum + public :: contact_history_reset + public :: contact_history_stats + public :: contact_history_state_stats + public :: contact_history_write + public :: contact_history_read + + integer, parameter, public :: CONTACT_HIST_OPEN = 0 + integer, parameter, public :: CONTACT_HIST_STICK = 1 + integer, parameter, public :: CONTACT_HIST_SLIP = 2 + + integer, allocatable :: hist_pair(:) + integer, allocatable :: hist_slave_elem(:) + integer, allocatable :: hist_slave_face(:) + integer, allocatable :: hist_master_elem(:) + integer, allocatable :: hist_master_face(:) + integer, allocatable :: hist_point(:) + integer, allocatable :: hist_status(:) + real(rk), allocatable :: hist_tangent(:,:) + real(rk), allocatable :: hist_slip(:) + real(rk), allocatable :: hist_work_total(:) + integer :: hist_count = 0 + integer :: hist_capacity = 0 + +contains + + subroutine contact_history_reset() + if (allocated(hist_pair)) deallocate(hist_pair, hist_slave_elem, & + hist_slave_face, hist_master_elem, hist_master_face, hist_point, & + hist_status) + if (allocated(hist_tangent)) deallocate(hist_tangent, hist_slip, hist_work_total) + hist_count = 0 + hist_capacity = 0 + end subroutine contact_history_reset + + subroutine contact_history_stats(slot_count, max_slip, max_work, & + max_tangent) + integer, intent(out) :: slot_count + real(rk), intent(out) :: max_slip + real(rk), intent(out) :: max_work + real(rk), intent(out) :: max_tangent + integer :: slot + + slot_count = hist_count + max_slip = ZERO + max_work = ZERO + max_tangent = ZERO + if (hist_count .le. 0 .or. .not. allocated(hist_pair)) return + + do slot = 1, hist_count + max_slip = max(max_slip, hist_slip(slot)) + max_work = max(max_work, hist_work_total(slot)) + max_tangent = max(max_tangent, sqrt(sum(hist_tangent(:,slot) * & + hist_tangent(:,slot)))) + end do + end subroutine contact_history_stats + + subroutine contact_history_state_stats(slot_count, stick_count, slip_count) + integer, intent(out) :: slot_count + integer, intent(out) :: stick_count + integer, intent(out) :: slip_count + integer :: slot + + slot_count = hist_count + stick_count = 0 + slip_count = 0 + if (hist_count .le. 0 .or. .not. allocated(hist_status)) return + + do slot = 1, hist_count + if (hist_status(slot) .eq. CONTACT_HIST_STICK) then + stick_count = stick_count + 1 + else if (hist_status(slot) .eq. CONTACT_HIST_SLIP) then + slip_count = slip_count + 1 + end if + end do + end subroutine contact_history_state_stats + + subroutine contact_history_write(unit_id, ios) + integer, intent(in) :: unit_id + integer, intent(out) :: ios + integer :: slot + + write(unit_id,'(A,1X,I12)',iostat=ios) 'CONTACT_HISTORY', hist_count + if (ios .ne. 0) return + + do slot = 1, hist_count + write(unit_id,'(7(I12,1X),5(ES24.16,1X))',iostat=ios) & + hist_pair(slot), hist_slave_elem(slot), hist_slave_face(slot), & + hist_master_elem(slot), hist_master_face(slot), & + hist_point(slot), hist_status(slot), & + hist_tangent(1,slot), hist_tangent(2,slot), hist_tangent(3,slot), & + hist_slip(slot), hist_work_total(slot) + if (ios .ne. 0) return + end do + end subroutine contact_history_write + + subroutine contact_history_read(unit_id, count_in, ierr) + integer, intent(in) :: unit_id + integer, intent(in) :: count_in + integer, intent(out) :: ierr + integer :: slot + integer :: ios + character(len=512) :: line + + call contact_history_reset() + if (count_in .lt. 0) then + call error_set(FEM_ERR_OUTPUT, 'invalid restart contact history count', & + ierr) + return + end if + + call ensure_history_capacity(count_in, ierr) + if (ierr .ne. FEM_SUCCESS) return + hist_count = count_in + + do slot = 1, hist_count + read(unit_id,'(A)',iostat=ios) line + if (ios .eq. 0) then + call read_history_line(line, slot, ios) + end if + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, 'invalid restart contact history', ierr) + return + end if + end do + + ierr = FEM_SUCCESS + end subroutine contact_history_read + + subroutine contact_history_old(pair, selem, sface, melem, mface, point_id, & + tangent) + integer, intent(in) :: pair + integer, intent(in) :: selem + integer, intent(in) :: sface + integer, intent(in) :: melem + integer, intent(in) :: mface + integer, intent(in) :: point_id + real(rk), intent(out) :: tangent(NDIM) + integer :: slot + + tangent = ZERO + slot = find_history_slot(pair, selem, sface, melem, mface, point_id) + if (slot .gt. 0) tangent = hist_tangent(:,slot) + end subroutine contact_history_old + + subroutine contact_history_sum(pair, selem, sface, melem, mface, tangent) + integer, intent(in) :: pair + integer, intent(in) :: selem + integer, intent(in) :: sface + integer, intent(in) :: melem + integer, intent(in) :: mface + real(rk), intent(out) :: tangent(NDIM) + integer :: slot + + tangent = ZERO + if (.not. allocated(hist_pair)) return + do slot = 1, hist_count + if (hist_pair(slot) .ne. pair) cycle + if (hist_slave_elem(slot) .ne. selem) cycle + if (hist_slave_face(slot) .ne. sface) cycle + if (hist_master_elem(slot) .ne. melem) cycle + if (hist_master_face(slot) .ne. mface) cycle + tangent = tangent + hist_tangent(:,slot) + end do + end subroutine contact_history_sum + + subroutine contact_history_store(pair, selem, sface, melem, mface, & + point_id, tangent, slip_delta, & + work_value, sliding, ierr) + integer, intent(in) :: pair + integer, intent(in) :: selem + integer, intent(in) :: sface + integer, intent(in) :: melem + integer, intent(in) :: mface + integer, intent(in) :: point_id + real(rk), intent(in) :: tangent(NDIM) + real(rk), intent(in) :: slip_delta + real(rk), intent(in) :: work_value + logical, intent(in) :: sliding + integer, intent(out) :: ierr + integer :: slot + + slot = find_history_slot(pair, selem, sface, melem, mface, point_id) + if (slot .eq. 0) then + call ensure_history_capacity(hist_count + 1, ierr) + if (ierr .ne. FEM_SUCCESS) return + hist_count = hist_count + 1 + slot = hist_count + hist_pair(slot) = pair + hist_slave_elem(slot) = selem + hist_slave_face(slot) = sface + hist_master_elem(slot) = melem + hist_master_face(slot) = mface + hist_point(slot) = point_id + end if + + hist_tangent(:,slot) = tangent + hist_slip(slot) = hist_slip(slot) + max(ZERO, slip_delta) + hist_work_total(slot) = hist_work_total(slot) + max(ZERO, work_value) + if (sliding) then + hist_status(slot) = CONTACT_HIST_SLIP + else + hist_status(slot) = CONTACT_HIST_STICK + end if + ierr = FEM_SUCCESS + end subroutine contact_history_store + + integer function find_history_slot(pair, selem, sface, melem, mface, point_id) + integer, intent(in) :: pair + integer, intent(in) :: selem + integer, intent(in) :: sface + integer, intent(in) :: melem + integer, intent(in) :: mface + integer, intent(in) :: point_id + integer :: slot + + find_history_slot = 0 + if (.not. allocated(hist_pair)) return + do slot = 1, hist_count + if (hist_pair(slot) .ne. pair) cycle + if (hist_slave_elem(slot) .ne. selem) cycle + if (hist_slave_face(slot) .ne. sface) cycle + if (hist_master_elem(slot) .ne. melem) cycle + if (hist_master_face(slot) .ne. mface) cycle + if (hist_point(slot) .ne. point_id) cycle + find_history_slot = slot + return + end do + end function find_history_slot + + subroutine ensure_history_capacity(required, ierr) + integer, intent(in) :: required + integer, intent(out) :: ierr + integer :: new_capacity + integer :: stat + + if (hist_capacity .ge. required) then + ierr = FEM_SUCCESS + return + end if + + new_capacity = max(16, max(required, 2 * hist_capacity)) + call grow_history_arrays(new_capacity, stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate contact friction history', ierr) + return + end if + + hist_capacity = new_capacity + ierr = FEM_SUCCESS + end subroutine ensure_history_capacity + + subroutine grow_history_arrays(new_capacity, stat) + integer, intent(in) :: new_capacity + integer, intent(out) :: stat + integer, allocatable :: new_pair(:) + integer, allocatable :: new_selem(:) + integer, allocatable :: new_sface(:) + integer, allocatable :: new_melem(:) + integer, allocatable :: new_mface(:) + integer, allocatable :: new_point(:) + integer, allocatable :: new_status(:) + real(rk), allocatable :: new_tangent(:,:) + real(rk), allocatable :: new_slip(:) + real(rk), allocatable :: new_work(:) + + allocate(new_pair(new_capacity), new_selem(new_capacity), & + new_sface(new_capacity), new_melem(new_capacity), & + new_mface(new_capacity), new_point(new_capacity), & + new_status(new_capacity), new_tangent(NDIM,new_capacity), & + new_slip(new_capacity), new_work(new_capacity), stat=stat) + if (stat .ne. 0) return + + call init_history_arrays(new_capacity, new_pair, new_selem, new_sface, & + new_melem, new_mface, new_point, new_status, & + new_tangent, new_slip, new_work) + call copy_history_arrays(new_pair, new_selem, new_sface, new_melem, & + new_mface, new_point, new_status, new_tangent, & + new_slip, new_work) + call replace_history_arrays(new_pair, new_selem, new_sface, new_melem, & + new_mface, new_point, new_status, & + new_tangent, new_slip, new_work) + deallocate(new_pair, new_selem, new_sface, new_melem, new_mface) + deallocate(new_point, new_status) + deallocate(new_tangent, new_slip, new_work) + end subroutine grow_history_arrays + + subroutine init_history_arrays(n, pair, selem, sface, melem, mface, point, & + status, tangent, slip, work) + integer, intent(in) :: n + integer, intent(out) :: pair(n), selem(n), sface(n), melem(n), mface(n) + integer, intent(out) :: point(n), status(n) + real(rk), intent(out) :: tangent(NDIM,n), slip(n), work(n) + + pair = 0 + selem = 0 + sface = 0 + melem = 0 + mface = 0 + point = 0 + status = CONTACT_HIST_OPEN + tangent = ZERO + slip = ZERO + work = ZERO + end subroutine init_history_arrays + + subroutine copy_history_arrays(pair, selem, sface, melem, mface, point, & + status, tangent, slip, work) + integer, intent(inout) :: pair(:), selem(:), sface(:), melem(:), mface(:) + integer, intent(inout) :: point(:), status(:) + real(rk), intent(inout) :: tangent(:,:), slip(:), work(:) + + if (hist_count .le. 0 .or. .not. allocated(hist_pair)) return + pair(1:hist_count) = hist_pair(1:hist_count) + selem(1:hist_count) = hist_slave_elem(1:hist_count) + sface(1:hist_count) = hist_slave_face(1:hist_count) + melem(1:hist_count) = hist_master_elem(1:hist_count) + mface(1:hist_count) = hist_master_face(1:hist_count) + point(1:hist_count) = hist_point(1:hist_count) + status(1:hist_count) = hist_status(1:hist_count) + tangent(:,1:hist_count) = hist_tangent(:,1:hist_count) + slip(1:hist_count) = hist_slip(1:hist_count) + work(1:hist_count) = hist_work_total(1:hist_count) + end subroutine copy_history_arrays + + subroutine replace_history_arrays(pair, selem, sface, melem, mface, point, & + status, tangent, slip, work) + integer, intent(in) :: pair(:), selem(:), sface(:), melem(:), mface(:) + integer, intent(in) :: point(:), status(:) + real(rk), intent(in) :: tangent(:,:), slip(:), work(:) + + if (allocated(hist_pair)) deallocate(hist_pair, hist_slave_elem, & + hist_slave_face, hist_master_elem, hist_master_face, hist_point, & + hist_status) + if (allocated(hist_tangent)) deallocate(hist_tangent, hist_slip, hist_work_total) + allocate(hist_pair(size(pair)), hist_slave_elem(size(pair)), & + hist_slave_face(size(pair)), hist_master_elem(size(pair)), & + hist_master_face(size(pair)), hist_point(size(pair)), & + hist_status(size(pair))) + allocate(hist_tangent(NDIM,size(pair)), hist_slip(size(pair)), & + hist_work_total(size(pair))) + hist_pair = pair + hist_slave_elem = selem + hist_slave_face = sface + hist_master_elem = melem + hist_master_face = mface + hist_point = point + hist_status = status + hist_tangent = tangent + hist_slip = slip + hist_work_total = work + end subroutine replace_history_arrays + + subroutine read_history_line(line, slot, ios) + character(len=*), intent(in) :: line + integer, intent(in) :: slot + integer, intent(out) :: ios + + read(line,*,iostat=ios) hist_pair(slot), hist_slave_elem(slot), & + hist_slave_face(slot), hist_master_elem(slot), hist_master_face(slot), & + hist_point(slot), hist_status(slot), hist_tangent(1,slot), & + hist_tangent(2,slot), hist_tangent(3,slot), hist_slip(slot), & + hist_work_total(slot) + if (ios .eq. 0) return + + read(line,*,iostat=ios) hist_pair(slot), hist_slave_elem(slot), & + hist_slave_face(slot), hist_master_elem(slot), hist_master_face(slot), & + hist_tangent(1,slot), hist_tangent(2,slot), hist_tangent(3,slot), & + hist_slip(slot), hist_work_total(slot) + if (ios .eq. 0) then + hist_point(slot) = 1 + hist_status(slot) = CONTACT_HIST_OPEN + end if + end subroutine read_history_line + +end module fem_contact_history diff --git a/src/boundary/fem_contact_output.f90 b/src/boundary/fem_contact_output.f90 index 6cb6d67..857cf72 100644 --- a/src/boundary/fem_contact_output.f90 +++ b/src/boundary/fem_contact_output.f90 @@ -8,7 +8,16 @@ module fem_contact_output public :: contact_pair_stick_value public :: contact_pair_slip_value public :: contact_pair_accumulated_slip + public :: contact_pair_fric_work_value + public :: contact_pair_fric_total_value + public :: contact_pair_fric_reg_value public :: contact_pair_tangent_component + public :: contact_pair_status_value + public :: contact_pair_activation_value + public :: contact_pair_release_value + public :: contact_pair_gap_value + public :: contact_pair_aug_factor_value + public :: contact_pair_aug_lambda_value contains @@ -50,4 +59,85 @@ contains contact_pair_tangent_component = contact_pair_tangent_force(idir,pair) end function contact_pair_tangent_component + real(rk) function contact_pair_fric_work_value(pair) + integer, intent(in) :: pair + + contact_pair_fric_work_value = ZERO + if (.not. allocated(contact_pair_friction_work)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_friction_work)) return + contact_pair_fric_work_value = contact_pair_friction_work(pair) + end function contact_pair_fric_work_value + + real(rk) function contact_pair_fric_total_value(pair) + integer, intent(in) :: pair + + contact_pair_fric_total_value = ZERO + if (.not. allocated(contact_pair_fric_work_total)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_fric_work_total)) return + contact_pair_fric_total_value = contact_pair_fric_work_total(pair) + end function contact_pair_fric_total_value + + real(rk) function contact_pair_fric_reg_value(pair) + integer, intent(in) :: pair + + contact_pair_fric_reg_value = ZERO + if (.not. allocated(contact_pair_friction_reg)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_friction_reg)) return + contact_pair_fric_reg_value = contact_pair_friction_reg(pair) + end function contact_pair_fric_reg_value + + integer function contact_pair_status_value(pair) + integer, intent(in) :: pair + + contact_pair_status_value = 0 + if (.not. allocated(contact_pair_status)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_status)) return + contact_pair_status_value = contact_pair_status(pair) + end function contact_pair_status_value + + integer function contact_pair_activation_value(pair) + integer, intent(in) :: pair + + contact_pair_activation_value = 0 + if (.not. allocated(contact_pair_activation_count)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_activation_count)) return + contact_pair_activation_value = contact_pair_activation_count(pair) + end function contact_pair_activation_value + + integer function contact_pair_release_value(pair) + integer, intent(in) :: pair + + contact_pair_release_value = 0 + if (.not. allocated(contact_pair_release_count)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_release_count)) return + contact_pair_release_value = contact_pair_release_count(pair) + end function contact_pair_release_value + + real(rk) function contact_pair_gap_value(pair) + integer, intent(in) :: pair + + contact_pair_gap_value = ZERO + if (.not. allocated(contact_pair_gap_residual)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_gap_residual)) return + contact_pair_gap_value = contact_pair_gap_residual(pair) + end function contact_pair_gap_value + + real(rk) function contact_pair_aug_factor_value(pair) + integer, intent(in) :: pair + + contact_pair_aug_factor_value = ZERO + if (.not. allocated(contact_pair_aug_factor)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_aug_factor)) return + contact_pair_aug_factor_value = contact_pair_aug_factor(pair) + end function contact_pair_aug_factor_value + + real(rk) function contact_pair_aug_lambda_value(pair) + integer, intent(in) :: pair + + contact_pair_aug_lambda_value = ZERO + if (.not. allocated(contact_pair_aug_lambda)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_aug_lambda)) return + contact_pair_aug_lambda_value = contact_pair_aug_lambda(pair) + end function contact_pair_aug_lambda_value + end module fem_contact_output diff --git a/src/boundary/fem_contact_search.f90 b/src/boundary/fem_contact_search.f90 index f8077a2..7cf6f3b 100644 --- a/src/boundary/fem_contact_search.f90 +++ b/src/boundary/fem_contact_search.f90 @@ -395,7 +395,12 @@ contains elem_id, face_id, x, vnod, normal, center, & radius, bvh_item, bvh_left, bvh_right, & bvh_start, bvh_count, bvh_min, bvh_max, & - total_weight, ierr) + total_weight, candidate_count, & + active_count, ierr, cache_capacity, & + cache_count, cache_master, cache_gap, & + cache_vn, cache_overflow, & + broad_capacity, broad_count, & + broad_master, broad_overflow) integer, intent(in) :: pair integer, intent(in) :: sf integer, intent(in) :: nf @@ -417,7 +422,19 @@ contains real(rk), intent(in) :: bvh_min(:,:) real(rk), intent(in) :: bvh_max(:,:) real(rk), intent(out) :: total_weight + integer, intent(out) :: candidate_count + integer, intent(out) :: active_count integer, intent(out) :: ierr + integer, intent(in), optional :: cache_capacity + integer, intent(out), optional :: cache_count + integer, intent(out), optional :: cache_master(:) + real(rk), intent(out), optional :: cache_gap(:) + real(rk), intent(out), optional :: cache_vn(:) + logical, intent(out), optional :: cache_overflow + integer, intent(in), optional :: broad_capacity + integer, intent(out), optional :: broad_count + integer, intent(out), optional :: broad_master(:) + logical, intent(out), optional :: broad_overflow real(rk) :: query_min(NDIM) real(rk) :: query_max(NDIM) real(rk) :: candidate_radius @@ -429,9 +446,31 @@ contains integer :: bvh_node integer :: bvh_pos integer :: mf + integer :: cache_limit, broad_limit + logical :: cache_enabled, broad_enabled logical :: active total_weight = ZERO + candidate_count = 0 + active_count = 0 + cache_limit = 0 + cache_enabled = present(cache_capacity) .and. present(cache_count) .and. & + present(cache_master) .and. present(cache_gap) .and. & + present(cache_vn) .and. present(cache_overflow) + broad_enabled = present(broad_capacity) .and. present(broad_count) .and. & + present(broad_master) .and. present(broad_overflow) + if (cache_enabled) then + cache_count = 0 + cache_overflow = .false. + cache_limit = min(cache_capacity, size(cache_master), & + size(cache_gap), size(cache_vn)) + end if + broad_limit = 0 + if (broad_enabled) then + broad_count = 0 + broad_overflow = .false. + broad_limit = min(broad_capacity, size(broad_master)) + end if candidate_radius = radius(sf) + contact_pair_search_value(pair) query_min = center(:,sf) - candidate_radius query_max = center(:,sf) + candidate_radius @@ -452,7 +491,18 @@ contains center, radius, gap, vn, pair_weight, & active, ierr) if (ierr .ne. FEM_SUCCESS) return - if (active) total_weight = total_weight + pair_weight + candidate_count = candidate_count + 1 + if (broad_enabled) then + call cache_bvh_master_candidate(broad_limit, mf, broad_count, & + broad_master, broad_overflow) + end if + if (active) then + active_count = active_count + 1 + total_weight = total_weight + pair_weight + call cache_bvh_candidate(cache_enabled, cache_limit, mf, gap, vn, & + cache_count, cache_master, cache_gap, & + cache_vn, cache_overflow) + end if end do else if (bvh_left(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then @@ -469,6 +519,53 @@ contains ierr = FEM_SUCCESS end subroutine contact_bvh_slave_weight + subroutine cache_bvh_master_candidate(cache_limit, mf, cache_count, & + cache_master, cache_overflow) + integer, intent(in) :: cache_limit + integer, intent(in) :: mf + integer, intent(inout), optional :: cache_count + integer, intent(inout), optional :: cache_master(:) + logical, intent(inout), optional :: cache_overflow + integer :: pos + + if (.not. present(cache_count)) return + pos = cache_count + 1 + if (pos .gt. cache_limit) then + if (present(cache_overflow)) cache_overflow = .true. + return + end if + cache_count = pos + if (present(cache_master)) cache_master(pos) = mf + end subroutine cache_bvh_master_candidate + + subroutine cache_bvh_candidate(enabled, cache_limit, mf, gap, vn, & + cache_count, cache_master, cache_gap, & + cache_vn, cache_overflow) + logical, intent(in) :: enabled + integer, intent(in) :: cache_limit + integer, intent(in) :: mf + real(rk), intent(in) :: gap + real(rk), intent(in) :: vn + integer, intent(inout), optional :: cache_count + integer, intent(inout), optional :: cache_master(:) + real(rk), intent(inout), optional :: cache_gap(:) + real(rk), intent(inout), optional :: cache_vn(:) + logical, intent(inout), optional :: cache_overflow + integer :: pos + + if (.not. enabled) return + if (.not. present(cache_count)) return + pos = cache_count + 1 + if (pos .gt. cache_limit) then + if (present(cache_overflow)) cache_overflow = .true. + return + end if + cache_count = pos + if (present(cache_master)) cache_master(pos) = mf + if (present(cache_gap)) cache_gap(pos) = gap + if (present(cache_vn)) cache_vn(pos) = vn + end subroutine cache_bvh_candidate + logical function slave_side_faces_master(pair, sf, mf, nf, elem_id, face_id, & center, normal) integer, intent(in) :: pair diff --git a/src/boundary/fem_contact_state.f90 b/src/boundary/fem_contact_state.f90 index 3390a56..7ee3bf3 100644 --- a/src/boundary/fem_contact_state.f90 +++ b/src/boundary/fem_contact_state.f90 @@ -5,24 +5,371 @@ module fem_contact_state private + integer, parameter, public :: CONTACT_PAIR_OPEN = 0 + integer, parameter, public :: CONTACT_PAIR_ACTIVATED = 1 + integer, parameter, public :: CONTACT_PAIR_MAINTAINED = 2 + integer, parameter, public :: CONTACT_PAIR_RELEASED = 3 + public :: contact_reset_status public :: contact_record_status + public :: contact_finalize_status + public :: contact_pair_normal_pressure + public :: contact_reset_search_stats + public :: contact_record_search_setup + public :: contact_record_search_test + public :: contact_record_search_counts + public :: contact_record_search_time + public :: contact_record_force_time + public :: contact_record_tangent_time + public :: contact_record_cache_counts + public :: ct_pair_search_setup + public :: ct_pair_search_test + public :: ct_pair_search_counts + public :: ct_pair_search_time + public :: ct_pair_force_time + public :: ct_pair_tangent_time + public :: ct_pair_cache_counts + public :: contact_get_search_stats + public :: contact_get_perf_stats + public :: contact_get_cache_stats + public :: contact_get_pair_stats + + integer :: contact_search_facet_count = 0 + integer :: contact_search_master_count = 0 + integer :: contact_search_broad_count = 0 + integer :: contact_search_active_count = 0 + integer :: contact_search_bvh_node_count = 0 + integer :: contact_cache_used_count = 0 + integer :: contact_cache_over_count = 0 + real(rk) :: contact_search_cpu_time = ZERO + real(rk) :: contact_force_cpu_time = ZERO + real(rk) :: contact_tangent_cpu_time = ZERO + integer, allocatable :: pair_search_facet_count(:) + integer, allocatable :: pair_search_master_count(:) + integer, allocatable :: pair_search_broad_count(:) + integer, allocatable :: pair_search_active_count(:) + integer, allocatable :: pair_search_bvh_node_count(:) + integer, allocatable :: pair_cache_used_count(:) + integer, allocatable :: pair_cache_over_count(:) + real(rk), allocatable :: pair_search_cpu_time(:) + real(rk), allocatable :: pair_force_cpu_time(:) + real(rk), allocatable :: pair_tangent_cpu_time(:) contains subroutine contact_reset_status() + call contact_reset_search_stats() contact_active_count = 0 contact_work_step = ZERO + contact_friction_work_step = ZERO contact_max_penetration = ZERO contact_force_norm = ZERO + if (allocated(contact_pair_prev_active) .and. allocated(contact_pair_active)) then + contact_pair_prev_active = contact_pair_active + end if if (allocated(contact_pair_active)) contact_pair_active = 0 + if (allocated(contact_pair_status)) contact_pair_status = CONTACT_PAIR_OPEN if (allocated(contact_pair_work)) contact_pair_work = ZERO + if (allocated(contact_pair_friction_work)) contact_pair_friction_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_gap_residual)) contact_pair_gap_residual = 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_reset_search_stats() + contact_search_facet_count = 0 + contact_search_master_count = 0 + contact_search_broad_count = 0 + contact_search_active_count = 0 + contact_search_bvh_node_count = 0 + contact_cache_used_count = 0 + contact_cache_over_count = 0 + contact_search_cpu_time = ZERO + contact_force_cpu_time = ZERO + contact_tangent_cpu_time = ZERO + call contact_reset_pair_stats() + end subroutine contact_reset_search_stats + + subroutine contact_reset_pair_stats() + integer :: stat + + call contact_dealloc_pair_stats() + if (ncontact_pair .le. 0) return + allocate(pair_search_facet_count(ncontact_pair), & + pair_search_master_count(ncontact_pair), & + pair_search_broad_count(ncontact_pair), & + pair_search_active_count(ncontact_pair), & + pair_search_bvh_node_count(ncontact_pair), & + pair_cache_used_count(ncontact_pair), & + pair_cache_over_count(ncontact_pair), & + pair_search_cpu_time(ncontact_pair), & + pair_force_cpu_time(ncontact_pair), & + pair_tangent_cpu_time(ncontact_pair), stat=stat) + if (stat .ne. 0) then + call contact_dealloc_pair_stats() + return + end if + pair_search_facet_count = 0 + pair_search_master_count = 0 + pair_search_broad_count = 0 + pair_search_active_count = 0 + pair_search_bvh_node_count = 0 + pair_cache_used_count = 0 + pair_cache_over_count = 0 + pair_search_cpu_time = ZERO + pair_force_cpu_time = ZERO + pair_tangent_cpu_time = ZERO + end subroutine contact_reset_pair_stats + + subroutine contact_dealloc_pair_stats() + if (allocated(pair_search_facet_count)) deallocate(pair_search_facet_count) + if (allocated(pair_search_master_count)) deallocate(pair_search_master_count) + if (allocated(pair_search_broad_count)) deallocate(pair_search_broad_count) + if (allocated(pair_search_active_count)) deallocate(pair_search_active_count) + if (allocated(pair_search_bvh_node_count)) deallocate(pair_search_bvh_node_count) + if (allocated(pair_cache_used_count)) deallocate(pair_cache_used_count) + if (allocated(pair_cache_over_count)) deallocate(pair_cache_over_count) + if (allocated(pair_search_cpu_time)) deallocate(pair_search_cpu_time) + if (allocated(pair_force_cpu_time)) deallocate(pair_force_cpu_time) + if (allocated(pair_tangent_cpu_time)) deallocate(pair_tangent_cpu_time) + end subroutine contact_dealloc_pair_stats + + subroutine contact_record_search_setup(facet_count, master_count, bvh_nodes) + integer, intent(in) :: facet_count + integer, intent(in) :: master_count + integer, intent(in) :: bvh_nodes + + contact_search_facet_count = contact_search_facet_count + & + max(0, facet_count) + contact_search_master_count = contact_search_master_count + & + max(0, master_count) + contact_search_bvh_node_count = contact_search_bvh_node_count + & + max(0, bvh_nodes) + end subroutine contact_record_search_setup + + subroutine contact_record_search_test(active) + logical, intent(in) :: active + + contact_search_broad_count = contact_search_broad_count + 1 + if (active) contact_search_active_count = & + contact_search_active_count + 1 + end subroutine contact_record_search_test + + subroutine contact_record_search_counts(broad_count, active_count) + integer, intent(in) :: broad_count + integer, intent(in) :: active_count + + contact_search_broad_count = contact_search_broad_count + & + max(0, broad_count) + contact_search_active_count = contact_search_active_count + & + max(0, active_count) + end subroutine contact_record_search_counts + + subroutine contact_record_search_time(elapsed_time) + real(rk), intent(in) :: elapsed_time + + contact_search_cpu_time = contact_search_cpu_time + & + max(ZERO, elapsed_time) + end subroutine contact_record_search_time + + subroutine contact_record_force_time(elapsed_time) + real(rk), intent(in) :: elapsed_time + + contact_force_cpu_time = contact_force_cpu_time + & + max(ZERO, elapsed_time) + end subroutine contact_record_force_time + + subroutine contact_record_tangent_time(elapsed_time) + real(rk), intent(in) :: elapsed_time + + contact_tangent_cpu_time = contact_tangent_cpu_time + & + max(ZERO, elapsed_time) + end subroutine contact_record_tangent_time + + subroutine contact_record_cache_counts(used_count, overflow_count) + integer, intent(in) :: used_count + integer, intent(in) :: overflow_count + + contact_cache_used_count = contact_cache_used_count + & + max(0, used_count) + contact_cache_over_count = contact_cache_over_count + & + max(0, overflow_count) + end subroutine contact_record_cache_counts + + subroutine ct_pair_search_setup(pair, facet_count, master_count, bvh_nodes) + integer, intent(in) :: pair, facet_count, master_count, bvh_nodes + + if (.not. contact_pair_stat_valid(pair)) return + pair_search_facet_count(pair) = pair_search_facet_count(pair) + & + max(0, facet_count) + pair_search_master_count(pair) = pair_search_master_count(pair) + & + max(0, master_count) + pair_search_bvh_node_count(pair) = pair_search_bvh_node_count(pair) + & + max(0, bvh_nodes) + end subroutine ct_pair_search_setup + + subroutine ct_pair_search_test(pair, active) + integer, intent(in) :: pair + logical, intent(in) :: active + + if (.not. contact_pair_stat_valid(pair)) return + pair_search_broad_count(pair) = pair_search_broad_count(pair) + 1 + if (active) pair_search_active_count(pair) = & + pair_search_active_count(pair) + 1 + end subroutine ct_pair_search_test + + subroutine ct_pair_search_counts(pair, broad_count, active_count) + integer, intent(in) :: pair, broad_count, active_count + + if (.not. contact_pair_stat_valid(pair)) return + pair_search_broad_count(pair) = pair_search_broad_count(pair) + & + max(0, broad_count) + pair_search_active_count(pair) = pair_search_active_count(pair) + & + max(0, active_count) + end subroutine ct_pair_search_counts + + subroutine ct_pair_search_time(pair, elapsed_time) + integer, intent(in) :: pair + real(rk), intent(in) :: elapsed_time + + if (.not. contact_pair_stat_valid(pair)) return + pair_search_cpu_time(pair) = pair_search_cpu_time(pair) + & + max(ZERO, elapsed_time) + end subroutine ct_pair_search_time + + subroutine ct_pair_force_time(pair, elapsed_time) + integer, intent(in) :: pair + real(rk), intent(in) :: elapsed_time + + if (.not. contact_pair_stat_valid(pair)) return + pair_force_cpu_time(pair) = pair_force_cpu_time(pair) + max(ZERO, elapsed_time) + end subroutine ct_pair_force_time + + subroutine ct_pair_tangent_time(pair, elapsed_time) + integer, intent(in) :: pair + real(rk), intent(in) :: elapsed_time + + if (.not. contact_pair_stat_valid(pair)) return + pair_tangent_cpu_time(pair) = pair_tangent_cpu_time(pair) + & + max(ZERO, elapsed_time) + end subroutine ct_pair_tangent_time + + subroutine ct_pair_cache_counts(pair, used_count, overflow_count) + integer, intent(in) :: pair, used_count, overflow_count + + if (.not. contact_pair_stat_valid(pair)) return + pair_cache_used_count(pair) = pair_cache_used_count(pair) + max(0, used_count) + pair_cache_over_count(pair) = pair_cache_over_count(pair) + max(0, overflow_count) + end subroutine ct_pair_cache_counts + + subroutine contact_get_search_stats(facet_count, master_count, & + broad_count, active_count, & + bvh_node_count) + integer, intent(out) :: facet_count + integer, intent(out) :: master_count + integer, intent(out) :: broad_count + integer, intent(out) :: active_count + integer, intent(out) :: bvh_node_count + + facet_count = contact_search_facet_count + master_count = contact_search_master_count + broad_count = contact_search_broad_count + active_count = contact_search_active_count + bvh_node_count = contact_search_bvh_node_count + end subroutine contact_get_search_stats + + subroutine contact_get_perf_stats(search_time, force_time, tangent_time) + real(rk), intent(out) :: search_time + real(rk), intent(out) :: force_time + real(rk), intent(out) :: tangent_time + + search_time = contact_search_cpu_time + force_time = contact_force_cpu_time + tangent_time = contact_tangent_cpu_time + end subroutine contact_get_perf_stats + + subroutine contact_get_cache_stats(used_count, overflow_count) + integer, intent(out) :: used_count + integer, intent(out) :: overflow_count + + used_count = contact_cache_used_count + overflow_count = contact_cache_over_count + end subroutine contact_get_cache_stats + + subroutine contact_get_pair_stats(pair, facet_count, master_count, & + broad_count, active_count, bvh_node_count, & + cache_used, cache_overflow, search_time, & + force_time, tangent_time) + integer, intent(in) :: pair + integer, intent(out) :: facet_count, master_count, broad_count, active_count + integer, intent(out) :: bvh_node_count, cache_used, cache_overflow + real(rk), intent(out) :: search_time, force_time, tangent_time + + facet_count = 0 + master_count = 0 + broad_count = 0 + active_count = 0 + bvh_node_count = 0 + cache_used = 0 + cache_overflow = 0 + search_time = ZERO + force_time = ZERO + tangent_time = ZERO + if (.not. contact_pair_stat_valid(pair)) return + facet_count = pair_search_facet_count(pair) + master_count = pair_search_master_count(pair) + broad_count = pair_search_broad_count(pair) + active_count = pair_search_active_count(pair) + bvh_node_count = pair_search_bvh_node_count(pair) + cache_used = pair_cache_used_count(pair) + cache_overflow = pair_cache_over_count(pair) + search_time = pair_search_cpu_time(pair) + force_time = pair_force_cpu_time(pair) + tangent_time = pair_tangent_cpu_time(pair) + end subroutine contact_get_pair_stats + + logical function contact_pair_stat_valid(pair) + integer, intent(in) :: pair + + contact_pair_stat_valid = .false. + if (pair .lt. 1) return + if (.not. allocated(pair_search_facet_count)) return + if (pair .gt. size(pair_search_facet_count)) return + contact_pair_stat_valid = .true. + end function contact_pair_stat_valid + + subroutine contact_finalize_status() + integer :: pair + + if (.not. allocated(contact_pair_active)) return + if (.not. allocated(contact_pair_prev_active)) return + if (.not. allocated(contact_pair_status)) return + + do pair = 1, size(contact_pair_active) + if (contact_pair_active(pair) .gt. 0) then + if (contact_pair_prev_active(pair) .gt. 0) then + contact_pair_status(pair) = CONTACT_PAIR_MAINTAINED + else + contact_pair_status(pair) = CONTACT_PAIR_ACTIVATED + if (allocated(contact_pair_activation_count)) then + contact_pair_activation_count(pair) = contact_pair_activation_count(pair) + 1 + end if + end if + call update_augmented_lambda(pair) + else if (contact_pair_prev_active(pair) .gt. 0) then + contact_pair_status(pair) = CONTACT_PAIR_RELEASED + if (allocated(contact_pair_release_count)) then + contact_pair_release_count(pair) = contact_pair_release_count(pair) + 1 + end if + if (allocated(contact_pair_aug_lambda)) contact_pair_aug_lambda(pair) = ZERO + else + contact_pair_status(pair) = CONTACT_PAIR_OPEN + end if + end do + end subroutine contact_finalize_status + subroutine contact_record_status(pair, penetration, force_vec) integer, intent(in) :: pair real(rk), intent(in) :: penetration @@ -48,10 +395,44 @@ contains contact_pair_work(pair) = contact_pair_work(pair) + work_estimate contact_pair_max_penetration(pair) = & max(contact_pair_max_penetration(pair), penetration) + contact_pair_gap_residual(pair) = & + max(contact_pair_gap_residual(pair), penetration) contact_pair_force_norm(pair) = contact_pair_force_norm(pair) + force_norm end if end if end if end subroutine contact_record_status + real(rk) function contact_pair_normal_pressure(pair, penetration) + integer, intent(in) :: pair + real(rk), intent(in) :: penetration + + contact_pair_normal_pressure = ZERO + if (pair .lt. 1) return + if (.not. allocated(contact_pair_penalty)) return + if (pair .gt. size(contact_pair_penalty)) return + contact_pair_normal_pressure = contact_pair_penalty(pair) * max(ZERO, penetration) + if (allocated(contact_pair_aug_lambda)) then + contact_pair_normal_pressure = & + contact_pair_normal_pressure + max(ZERO, contact_pair_aug_lambda(pair)) + end if + end function contact_pair_normal_pressure + + subroutine update_augmented_lambda(pair) + integer, intent(in) :: pair + real(rk) :: residual + real(rk) :: factor + + if (.not. allocated(contact_pair_aug_factor)) return + if (.not. allocated(contact_pair_aug_lambda)) return + if (.not. allocated(contact_pair_gap_residual)) return + if (.not. allocated(contact_pair_penalty)) return + if (pair .lt. 1 .or. pair .gt. size(contact_pair_aug_factor)) return + factor = max(ZERO, contact_pair_aug_factor(pair)) + if (factor .le. ZERO) return + residual = max(ZERO, contact_pair_gap_residual(pair)) + contact_pair_aug_lambda(pair) = max(ZERO, contact_pair_aug_lambda(pair) + & + factor * contact_pair_penalty(pair) * residual) + end subroutine update_augmented_lambda + end module fem_contact_state diff --git a/src/boundary/fem_contact_tangent.f90 b/src/boundary/fem_contact_tangent.f90 index 3c9c282..267b383 100644 --- a/src/boundary/fem_contact_tangent.f90 +++ b/src/boundary/fem_contact_tangent.f90 @@ -4,20 +4,26 @@ module fem_contact_tangent use fem_database use fem_sparse_csr use fem_coupling + use fem_contact_state, only: ct_pair_tangent_time use fem_contact_target, only: facet_surface_matches, contact_pair_search_value, & contact_pair_friction_value use fem_contact_geometry, only: facet_center_velocity, facet_radius_value, & - surface_oriented_contact_normal, contact_mortar_integrate + surface_oriented_contact_normal, contact_mortar_integrate, & + contact_tangent_direction use fem_contact_search, only: CONTACT_MAX_STACK, contact_pair_candidate, & contact_grid_dimensions, contact_grid_fill, contact_grid_range, & contact_grid_cell, contact_master_count, contact_bvh_build, & contact_aabb_overlap, contact_bvh_slave_weight + use fem_contact_candidate_cache, only: contact_candidate_cache_try, & + contact_candidate_cache_count, contact_candidate_cache_master implicit none private public :: apply_contact_pair_tangent + integer, parameter :: CONTACT_TANGENT_CACHE_CAP = 32 + contains subroutine apply_contact_pair_tangent(kmat, velocity_scale, ierr) @@ -25,9 +31,14 @@ contains real(rk), intent(in) :: velocity_scale integer, intent(out) :: ierr integer :: pair + real(rk) :: time_start + real(rk) :: time_finish do pair = 1, ncontact_pair + call cpu_time(time_start) call apply_contact_pair_tan_one(pair, kmat, velocity_scale, ierr) + call cpu_time(time_finish) + call ct_pair_tangent_time(pair, time_finish - time_start) if (ierr .ne. FEM_SUCCESS) return end do @@ -40,20 +51,10 @@ contains real(rk), intent(in) :: velocity_scale integer, intent(out) :: ierr integer :: nf, max_nodes, stat, q - integer :: grid_nx, grid_ny, grid_nz, grid_ncell, nmaster, bvh_nodes integer, allocatable :: facet_id(:), elem_id(:), face_id(:), fnnode(:) - integer, allocatable :: fconn(:,:), grid_head(:), grid_next(:) - integer, allocatable :: bvh_item(:), bvh_left(:), bvh_right(:) - integer, allocatable :: bvh_start(:), bvh_count(:) + integer, allocatable :: fconn(:,:) real(rk), allocatable :: x(:,:,:), vnod(:,:,:), normal(:,:), center(:,:) real(rk), allocatable :: velc(:,:), area(:), radius(:) - real(rk), allocatable :: bvh_min(:,:), bvh_max(:,:) - real(rk) :: mu - real(rk) :: grid_min(NDIM) - real(rk) :: grid_h(NDIM) - real(rk) :: max_master_radius - logical :: use_grid - logical :: use_bvh call cpl_build_facets(ierr) if (ierr .ne. FEM_SUCCESS) return @@ -82,11 +83,56 @@ contains center(:,q), velc(:,q)) radius(q) = facet_radius_value(q, nf, max_nodes, fnnode, x, center(:,q)) end do + + call apply_pair_tan_facets(pair, kmat, velocity_scale, nf, max_nodes, & + fnnode, fconn, elem_id, face_id, x, vnod, & + normal, area, radius, center, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + + ierr = FEM_SUCCESS + +900 continue + if (allocated(facet_id)) deallocate(facet_id, elem_id, face_id, fnnode, & + fconn, x, vnod, normal, center, velc, & + area, radius) + end subroutine apply_contact_pair_tan_one + + subroutine apply_pair_tan_facets(pair, kmat, velocity_scale, nf, max_nodes, & + fnnode, fconn, elem_id, face_id, x, vnod, & + normal, area, radius, center, ierr) + integer, intent(in) :: pair, nf, max_nodes + type(csr_matrix), intent(inout) :: kmat + real(rk), intent(in) :: velocity_scale + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), area(nf), radius(nf) + real(rk), intent(in) :: center(NDIM,nf) + integer, intent(out) :: ierr + integer :: grid_nx, grid_ny, grid_nz, grid_ncell, nmaster, bvh_nodes + integer, allocatable :: grid_head(:), grid_next(:) + integer, allocatable :: bvh_item(:), bvh_left(:), bvh_right(:) + integer, allocatable :: bvh_start(:), bvh_count(:) + real(rk), allocatable :: bvh_min(:,:), bvh_max(:,:) + real(rk) :: grid_min(NDIM), grid_h(NDIM), max_master_radius, mu + integer :: stat + logical :: use_grid, use_bvh, use_cached_candidates + mu = contact_pair_friction_value(pair) nmaster = contact_master_count(pair, nf, elem_id, face_id) + bvh_nodes = 0 use_bvh = nmaster .gt. 16 use_grid = .false. + use_cached_candidates = .false. if (use_bvh) then + call contact_candidate_cache_try(pair, ncontact_pair, nf, elem_id, & + face_id, center, radius, & + contact_pair_slave(pair), & + contact_pair_master(pair), & + contact_pair_search_value(pair), & + use_cached_candidates) + end if + if (use_bvh .and. .not. use_cached_candidates) 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) @@ -115,12 +161,20 @@ contains end if if (use_bvh) then - call contact_bvh_apply_pair_tangent(pair, kmat, velocity_scale, mu, nf, & - max_nodes, fnnode, fconn, elem_id, face_id, & - x, vnod, normal, area, radius, center, & - bvh_item, bvh_left, bvh_right, & - bvh_start, bvh_count, bvh_min, bvh_max, & - ierr) + if (use_cached_candidates) then + call contact_cache_apply_tan(pair, kmat, velocity_scale, mu, nf, & + max_nodes, fnnode, fconn, elem_id, & + face_id, x, vnod, normal, area, & + radius, center, ierr) + else + call contact_bvh_apply_pair_tangent(pair, kmat, velocity_scale, mu, & + nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, & + normal, area, radius, center, & + bvh_item, bvh_left, bvh_right, & + bvh_start, bvh_count, bvh_min, & + bvh_max, ierr) + end if if (ierr .ne. FEM_SUCCESS) go to 900 go to 850 end if @@ -144,13 +198,10 @@ contains ierr = FEM_SUCCESS 900 continue - if (allocated(facet_id)) deallocate(facet_id, elem_id, face_id, fnnode, & - fconn, x, vnod, normal, center, velc, & - area, radius) if (allocated(grid_head)) deallocate(grid_head, grid_next) if (allocated(bvh_item)) deallocate(bvh_item, bvh_left, bvh_right, & bvh_start, bvh_count, bvh_min, bvh_max) - end subroutine apply_contact_pair_tan_one + end subroutine apply_pair_tan_facets subroutine apply_pair_tangent_slaves_all(kmat, pair, nf, max_nodes, & fnnode, fconn, elem_id, face_id, x, & @@ -248,7 +299,7 @@ contains if (ierr .ne. FEM_SUCCESS) return if (.not. active) cycle call contact_add_pair_tangent(kmat, pair, sf, mf, nf, max_nodes, fnnode, & - fconn, elem_id, face_id, x, normal, gap, & + fconn, elem_id, face_id, x, vnod, normal, gap, & vn, area_scale, velocity_scale, mu, ierr) if (ierr .ne. FEM_SUCCESS) return end do @@ -339,7 +390,7 @@ contains if (active .and. apply_tangent) then call contact_add_pair_tangent(kmat, pair, sf, mf, nf, max_nodes, & fnnode, fconn, elem_id, face_id, & - x, normal, gap, vn, area_scale, & + x, vnod, normal, gap, vn, area_scale, & velocity_scale, mu, ierr) if (ierr .ne. FEM_SUCCESS) return else if (active) then @@ -363,6 +414,218 @@ contains end if end function contact_tangent_area_scale + subroutine contact_cache_apply_tan(pair, kmat, velocity_scale, mu, nf, & + max_nodes, fnnode, fconn, elem_id, & + face_id, x, vnod, normal, area, & + radius, center, ierr) + integer, intent(in) :: pair, nf, max_nodes + type(csr_matrix), intent(inout) :: kmat + real(rk), intent(in) :: velocity_scale, mu + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), area(nf), radius(nf) + real(rk), intent(in) :: center(NDIM,nf) + integer, intent(out) :: ierr + real(rk), allocatable :: total_weight(:), area_scale(:) + real(rk), allocatable :: cache_gap(:,:), cache_vn(:,:) + integer, allocatable :: cache_count(:), cache_master(:,:) + logical, allocatable :: cache_overflow(:) + integer :: sf, stat, parallel_ierr + + allocate(total_weight(nf), area_scale(nf), cache_count(nf), & + cache_master(CONTACT_TANGENT_CACHE_CAP,nf), & + cache_gap(CONTACT_TANGENT_CACHE_CAP,nf), & + cache_vn(CONTACT_TANGENT_CACHE_CAP,nf), cache_overflow(nf), & + stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, & + 'failed to allocate cached contact tangent weights', ierr) + return + end if + + call contact_cand_tan_wts(pair, nf, max_nodes, fnnode, fconn, elem_id, & + face_id, x, vnod, normal, area, radius, & + center, total_weight, area_scale, cache_count, & + cache_master, cache_gap, cache_vn, & + cache_overflow, ierr) + if (ierr .ne. FEM_SUCCESS) go to 900 + + parallel_ierr = FEM_SUCCESS +!$omp parallel do schedule(dynamic) private(sf) + do sf = 1, nf + if (parallel_ierr .eq. FEM_SUCCESS) then + if (facet_surface_matches(elem_id(sf), face_id(sf), & + contact_pair_slave(pair)) .and. & + total_weight(sf) .gt. tiny(ONE)) then + if (cache_overflow(sf)) then + call contact_cand_slave_tan(pair, kmat, velocity_scale, mu, sf, & + nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, & + center, radius, area_scale(sf), & + parallel_ierr) + else + call contact_bvh_slave_cached_tan(pair, kmat, velocity_scale, mu, & + sf, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, & + normal, cache_count(sf), & + cache_master(:,sf), & + cache_gap(:,sf), cache_vn(:,sf), & + area_scale(sf), parallel_ierr) + end if + end if + end if + end do +!$omp end parallel do + + ierr = parallel_ierr +900 continue + deallocate(total_weight, area_scale, cache_count, cache_master, & + cache_gap, cache_vn, cache_overflow) + end subroutine contact_cache_apply_tan + + subroutine contact_cand_tan_wts(pair, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, area, & + radius, center, total_weight, area_scale, & + cache_count, cache_master, cache_gap, & + cache_vn, cache_overflow, ierr) + integer, intent(in) :: pair, nf, max_nodes + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), area(nf), radius(nf) + real(rk), intent(in) :: center(NDIM,nf) + real(rk), intent(out) :: total_weight(nf), area_scale(nf) + real(rk), intent(out) :: cache_gap(:,:), cache_vn(:,:) + integer, intent(out) :: cache_count(nf), cache_master(:,:) + logical, intent(out) :: cache_overflow(nf) + integer, intent(out) :: ierr + real(rk) :: local_weight + integer :: sf, item, mf, item_count, local_ierr, parallel_ierr + + call contact_bvh_tan_init_cache(nf, total_weight, area_scale, cache_count, & + cache_master, cache_gap, cache_vn, & + cache_overflow) + parallel_ierr = FEM_SUCCESS +!$omp parallel do schedule(dynamic) private(sf,item,mf,item_count,local_ierr,local_weight) + do sf = 1, nf + if (parallel_ierr .eq. FEM_SUCCESS) then + if (facet_surface_matches(elem_id(sf), face_id(sf), & + contact_pair_slave(pair))) then + item_count = contact_candidate_cache_count(pair, sf) + do item = 1, item_count + mf = contact_candidate_cache_master(pair, sf, item) + if (mf .le. 0) cycle + call contact_cand_tan_weight(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, normal, center, radius, & + cache_count(sf), cache_master(:,sf), cache_gap(:,sf), & + cache_vn(:,sf), cache_overflow(sf), 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) = total_weight(sf) + local_weight + end if + end do + area_scale(sf) = contact_tangent_area_scale(area(sf), & + total_weight(sf)) + end if + end if + end do +!$omp end parallel do + ierr = parallel_ierr + end subroutine contact_cand_tan_wts + + subroutine contact_cand_tan_weight(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, & + normal, center, radius, cache_count, & + cache_master, cache_gap, cache_vn, & + cache_overflow, local_weight, ierr) + integer, intent(in) :: pair, sf, mf, nf, max_nodes + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), center(NDIM,nf), radius(nf) + integer, intent(inout) :: cache_count, cache_master(:) + real(rk), intent(inout) :: cache_gap(:), cache_vn(:) + logical, intent(inout) :: cache_overflow + real(rk), intent(out) :: local_weight + integer, intent(out) :: ierr + real(rk) :: gap, vn, pair_weight + logical :: active + + local_weight = ZERO + call contact_pair_candidate(pair, sf, mf, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, center, & + radius, gap, vn, pair_weight, active, ierr) + if (ierr .ne. FEM_SUCCESS) return + if (active) then + local_weight = pair_weight + call contact_cache_active_tan(mf, gap, vn, cache_count, cache_master, & + cache_gap, cache_vn, cache_overflow) + end if + end subroutine contact_cand_tan_weight + + subroutine contact_cache_active_tan(mf, gap, vn, cache_count, cache_master, & + cache_gap, cache_vn, cache_overflow) + integer, intent(in) :: mf + real(rk), intent(in) :: gap, vn + integer, intent(inout) :: cache_count, cache_master(:) + real(rk), intent(inout) :: cache_gap(:), cache_vn(:) + logical, intent(inout) :: cache_overflow + integer :: pos + + pos = cache_count + 1 + if (pos .gt. min(size(cache_master), size(cache_gap), size(cache_vn))) then + cache_overflow = .true. + return + end if + cache_count = pos + cache_master(pos) = mf + cache_gap(pos) = gap + cache_vn(pos) = vn + end subroutine contact_cache_active_tan + + subroutine contact_cand_slave_tan(pair, kmat, velocity_scale, mu, sf, nf, & + max_nodes, fnnode, fconn, elem_id, & + face_id, x, vnod, normal, center, radius, & + area_scale, parallel_ierr) + integer, intent(in) :: pair, sf, nf, max_nodes + type(csr_matrix), intent(inout) :: kmat + real(rk), intent(in) :: velocity_scale, mu, area_scale + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), center(NDIM,nf), radius(nf) + integer, intent(inout) :: parallel_ierr + real(rk) :: gap, vn, pair_weight + integer :: item, item_count, mf, local_ierr + logical :: active + + item_count = contact_candidate_cache_count(pair, sf) + do item = 1, item_count + if (parallel_ierr .ne. FEM_SUCCESS) return + mf = contact_candidate_cache_master(pair, sf, item) + if (mf .le. 0) cycle + call contact_pair_candidate(pair, sf, mf, nf, max_nodes, fnnode, fconn, & + elem_id, face_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 (.not. active) cycle + call contact_add_pair_tan_omp(kmat, pair, sf, mf, nf, max_nodes, & + fnnode, fconn, elem_id, face_id, x, & + vnod, normal, gap, vn, area_scale, & + velocity_scale, mu, parallel_ierr) + end do + end subroutine contact_cand_slave_tan + subroutine contact_bvh_apply_pair_tangent(pair, kmat, velocity_scale, mu, nf, & max_nodes, fnnode, fconn, elem_id, face_id, & x, vnod, normal, area, radius, center, & @@ -395,74 +658,189 @@ contains integer, intent(out) :: ierr real(rk), allocatable :: total_weight(:) real(rk), allocatable :: area_scale(:) - real(rk) :: local_weight + real(rk), allocatable :: cache_gap(:,:) + real(rk), allocatable :: cache_vn(:,:) + integer, allocatable :: cache_count(:) + integer, allocatable :: cache_master(:,:) + logical, allocatable :: cache_overflow(:) integer :: sf integer :: stat - integer :: local_ierr integer :: parallel_ierr - allocate(total_weight(nf), area_scale(nf), stat=stat) + allocate(total_weight(nf), area_scale(nf), cache_count(nf), & + cache_master(CONTACT_TANGENT_CACHE_CAP,nf), & + cache_gap(CONTACT_TANGENT_CACHE_CAP,nf), & + cache_vn(CONTACT_TANGENT_CACHE_CAP,nf), cache_overflow(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_surface_matches(elem_id(sf), face_id(sf), & - contact_pair_slave(pair))) then - call contact_bvh_slave_weight(pair, sf, nf, max_nodes, fnnode, fconn, & - elem_id, face_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) + call contact_bvh_tan_prepare_weights(pair, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, & + area, radius, center, bvh_item, & + bvh_left, bvh_right, bvh_start, & + bvh_count, bvh_min, bvh_max, & + total_weight, area_scale, & + cache_count, cache_master, cache_gap, & + cache_vn, cache_overflow, ierr) + if (ierr .ne. FEM_SUCCESS) then + deallocate(total_weight, area_scale, cache_count, cache_master, & + cache_gap, cache_vn, cache_overflow) return end if + parallel_ierr = FEM_SUCCESS !$omp parallel do schedule(dynamic) private(sf) do sf = 1, nf if (parallel_ierr .eq. FEM_SUCCESS) then if (facet_surface_matches(elem_id(sf), face_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, face_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) + if (cache_overflow(sf)) then + call contact_bvh_slave_tangent(pair, kmat, velocity_scale, mu, sf, nf, & + max_nodes, fnnode, fconn, elem_id, face_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) + else + call contact_bvh_slave_cached_tan(pair, kmat, velocity_scale, mu, & + sf, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, & + cache_count(sf), cache_master(:,sf), & + cache_gap(:,sf), cache_vn(:,sf), & + area_scale(sf), parallel_ierr) + end if end if end if end do !$omp end parallel do ierr = parallel_ierr - deallocate(total_weight) - deallocate(area_scale) + deallocate(total_weight, area_scale, cache_count, cache_master, & + cache_gap, cache_vn, cache_overflow) end subroutine contact_bvh_apply_pair_tangent + subroutine contact_bvh_tan_prepare_weights(pair, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, & + normal, area, radius, center, & + bvh_item, bvh_left, bvh_right, & + bvh_start, bvh_count, bvh_min, & + bvh_max, total_weight, area_scale, & + cache_count, cache_master, & + cache_gap, cache_vn, & + cache_overflow, ierr) + integer, intent(in) :: pair, nf, max_nodes + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + integer, intent(in) :: bvh_item(:), bvh_left(:), bvh_right(:) + integer, intent(in) :: bvh_start(:), bvh_count(:) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), area(nf), radius(nf) + real(rk), intent(in) :: center(NDIM,nf), bvh_min(:,:), bvh_max(:,:) + real(rk), intent(out) :: total_weight(nf), area_scale(nf) + real(rk), intent(out) :: cache_gap(:,:), cache_vn(:,:) + integer, intent(out) :: cache_count(nf), cache_master(:,:) + logical, intent(out) :: cache_overflow(nf) + integer, intent(out) :: ierr + real(rk) :: local_weight + integer :: sf, local_ierr, parallel_ierr, local_broad, local_active + + call contact_bvh_tan_init_cache(nf, total_weight, area_scale, cache_count, & + cache_master, cache_gap, cache_vn, & + cache_overflow) + parallel_ierr = FEM_SUCCESS +!$omp parallel do schedule(dynamic) private(sf,local_weight,local_ierr,local_broad,local_active) + do sf = 1, nf + if (parallel_ierr .eq. FEM_SUCCESS) then + if (facet_surface_matches(elem_id(sf), face_id(sf), & + contact_pair_slave(pair))) then + call contact_bvh_slave_weight(pair, sf, nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, center, radius, bvh_item, & + bvh_left, bvh_right, bvh_start, bvh_count, bvh_min, bvh_max, & + local_weight, local_broad, local_active, local_ierr, & + CONTACT_TANGENT_CACHE_CAP, cache_count(sf), cache_master(:,sf), & + cache_gap(:,sf), cache_vn(:,sf), cache_overflow(sf)) + call contact_bvh_tan_store_weight(sf, area(sf), local_weight, & + local_ierr, parallel_ierr, & + total_weight, area_scale) + end if + end if + end do +!$omp end parallel do + ierr = parallel_ierr + end subroutine contact_bvh_tan_prepare_weights + + subroutine contact_bvh_tan_init_cache(nf, total_weight, area_scale, & + cache_count, cache_master, cache_gap, & + cache_vn, cache_overflow) + integer, intent(in) :: nf + real(rk), intent(out) :: total_weight(nf), area_scale(nf) + integer, intent(out) :: cache_count(nf), cache_master(:,:) + real(rk), intent(out) :: cache_gap(:,:), cache_vn(:,:) + logical, intent(out) :: cache_overflow(nf) + + total_weight = ZERO + area_scale = ONE + cache_count = 0 + cache_master = 0 + cache_gap = ZERO + cache_vn = ZERO + cache_overflow = .false. + end subroutine contact_bvh_tan_init_cache + + subroutine contact_bvh_tan_store_weight(sf, slave_area, local_weight, & + local_ierr, parallel_ierr, & + total_weight, area_scale) + integer, intent(in) :: sf, local_ierr + real(rk), intent(in) :: slave_area, local_weight + integer, intent(inout) :: parallel_ierr + real(rk), intent(inout) :: total_weight(:), area_scale(:) + + 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 + total_weight(sf) = local_weight + if (slave_area .gt. tiny(ONE) .and. local_weight .gt. slave_area) then + area_scale(sf) = slave_area / local_weight + end if + end subroutine contact_bvh_tan_store_weight + + subroutine contact_bvh_slave_cached_tan(pair, kmat, velocity_scale, mu, sf, & + nf, max_nodes, fnnode, fconn, & + elem_id, face_id, x, vnod, normal, & + cache_count, cache_master, cache_gap, & + cache_vn, area_scale, parallel_ierr) + integer, intent(in) :: pair, sf, nf, max_nodes + type(csr_matrix), intent(inout) :: kmat + real(rk), intent(in) :: velocity_scale, mu + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), area_scale + integer, intent(in) :: cache_count, cache_master(:) + real(rk), intent(in) :: cache_gap(:), cache_vn(:) + integer, intent(inout) :: parallel_ierr + integer :: item, mf + + if (cache_count .le. 0) return + + do item = 1, cache_count + if (parallel_ierr .ne. FEM_SUCCESS) return + mf = cache_master(item) + call contact_add_pair_tan_omp(kmat, pair, sf, mf, nf, max_nodes, & + fnnode, fconn, elem_id, face_id, x, & + vnod, normal, cache_gap(item), & + cache_vn(item), area_scale, & + velocity_scale, mu, parallel_ierr) + end do + end subroutine contact_bvh_slave_cached_tan + subroutine contact_bvh_slave_tangent(pair, kmat, velocity_scale, mu, sf, nf, & max_nodes, fnnode, fconn, elem_id, face_id, & x, vnod, normal, center, radius, bvh_item, & @@ -534,18 +912,9 @@ contains !$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, elem_id, face_id, & - 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 + if (active) call contact_add_pair_tan_omp(kmat, pair, sf, mf, nf, & + max_nodes, fnnode, fconn, elem_id, face_id, x, vnod, normal, gap, & + vn, area_scale, velocity_scale, mu, parallel_ierr) end do else if (bvh_left(bvh_node) .gt. 0 .and. stack_top .lt. CONTACT_MAX_STACK) then @@ -561,19 +930,17 @@ contains end subroutine contact_bvh_slave_tangent subroutine contact_add_pair_tangent(kmat, pair, sf, mf, nf, max_nodes, & - fnnode, fconn, elem_id, face_id, x, normal, gap, vn, & - area_scale, velocity_scale, mu, ierr) + fnnode, fconn, elem_id, face_id, x, vnod, & + normal, gap, vn, area_scale, & + velocity_scale, mu, ierr) type(csr_matrix), intent(inout) :: kmat - integer, intent(in) :: pair - integer, intent(in) :: sf - integer, intent(in) :: mf - integer, intent(in) :: nf - integer, intent(in) :: max_nodes + integer, intent(in) :: pair, sf, mf, nf, max_nodes integer, intent(in) :: fnnode(nf) integer, intent(in) :: fconn(max_nodes,nf) integer, intent(in) :: elem_id(nf) integer, intent(in) :: face_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) :: gap real(rk), intent(in) :: vn @@ -581,25 +948,113 @@ contains 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 + logical :: do_add + integer :: nslave, nmaster + integer :: slave_conn(max_nodes), master_conn(max_nodes) + real(rk) :: scale, ktan(NDIM,NDIM) + real(rk) :: ss_w(max_nodes,max_nodes), sm_w(max_nodes,max_nodes) + real(rk) :: ms_w(max_nodes,max_nodes), mm_w(max_nodes,max_nodes) + call contact_prepare_pair_tan(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, normal, & + gap, vn, area_scale, velocity_scale, mu, & + do_add, nslave, slave_conn, nmaster, & + master_conn, ktan, ss_w, sm_w, ms_w, & + mm_w, scale, ierr) + if (ierr .ne. FEM_SUCCESS) return + if (.not. do_add) return + call add_contact_pair_tan_mortar(kmat, max_nodes, nslave, slave_conn, & + nmaster, master_conn, ktan, ss_w, sm_w, & + ms_w, mm_w, scale, ierr) + end subroutine contact_add_pair_tangent + + subroutine contact_add_pair_tan_omp(kmat, pair, sf, mf, nf, max_nodes, & + fnnode, fconn, elem_id, face_id, x, & + vnod, normal, gap, vn, area_scale, & + velocity_scale, mu, parallel_ierr) + type(csr_matrix), intent(inout) :: kmat + integer, intent(in) :: pair, sf, mf, nf, max_nodes + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), gap, vn, area_scale + real(rk), intent(in) :: velocity_scale, mu + integer, intent(inout) :: parallel_ierr + logical :: do_add + integer :: local_ierr, nslave, nmaster + integer :: slave_conn(max_nodes), master_conn(max_nodes) + real(rk) :: scale, ktan(NDIM,NDIM) + real(rk) :: ss_w(max_nodes,max_nodes), sm_w(max_nodes,max_nodes) + real(rk) :: ms_w(max_nodes,max_nodes), mm_w(max_nodes,max_nodes) + + if (parallel_ierr .ne. FEM_SUCCESS) return + call contact_prepare_pair_tan(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, normal, & + gap, vn, area_scale, velocity_scale, mu, & + do_add, nslave, slave_conn, nmaster, & + master_conn, ktan, ss_w, sm_w, ms_w, & + mm_w, scale, 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 (.not. do_add) return + +!$omp critical(femlite_contact_tangent) + if (parallel_ierr .eq. FEM_SUCCESS) then + call add_contact_pair_tan_mortar(kmat, max_nodes, nslave, slave_conn, & + nmaster, master_conn, ktan, ss_w, & + sm_w, ms_w, mm_w, scale, local_ierr) + if (local_ierr .ne. FEM_SUCCESS) parallel_ierr = local_ierr + end if +!$omp end critical(femlite_contact_tangent) + end subroutine contact_add_pair_tan_omp + + subroutine contact_prepare_pair_tan(pair, sf, mf, nf, max_nodes, fnnode, & + fconn, elem_id, face_id, x, vnod, & + normal, gap, vn, area_scale, & + velocity_scale, mu, do_add, nslave, & + slave_conn, nmaster, master_conn, ktan, & + ss_w, sm_w, ms_w, mm_w, scale, ierr) + integer, intent(in) :: pair, sf, mf, nf, max_nodes + integer, intent(in) :: fnnode(nf), fconn(max_nodes,nf) + integer, intent(in) :: elem_id(nf), face_id(nf) + real(rk), intent(in) :: x(NDIM,max_nodes,nf), vnod(NDIM,max_nodes,nf) + real(rk), intent(in) :: normal(NDIM,nf), gap, vn, area_scale + real(rk), intent(in) :: velocity_scale, mu + logical, intent(out) :: do_add + integer, intent(out) :: nslave, nmaster + integer, intent(out) :: slave_conn(max_nodes), master_conn(max_nodes) + real(rk), intent(out) :: ktan(NDIM,NDIM) + 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) + real(rk), intent(out) :: scale + integer, intent(out) :: ierr + real(rk) :: mortar_area, contact_area, coeff, mu_eff + real(rk) :: nrm(NDIM), tdir(NDIM), vt_norm + real(rk) :: slave_w(max_nodes), master_w(max_nodes) + integer :: idir, jdir + + do_add = .false. + nslave = 0 + nmaster = 0 + slave_conn = 0 + master_conn = 0 + ktan = ZERO + scale = area_scale if (gap .ge. ZERO) then ierr = FEM_SUCCESS return end if + nslave = fnnode(sf) + nmaster = fnnode(mf) + slave_conn = fconn(:,sf) + master_conn = fconn(:,mf) nrm = surface_oriented_contact_normal(normal(:,mf), elem_id(mf), & face_id(mf), & contact_pair_master(pair), ierr) @@ -618,22 +1073,51 @@ contains if (vn .lt. ZERO) then coeff = coeff + velocity_scale * contact_pair_damping(pair) end if + call contact_tangent_direction(sf, mf, nf, max_nodes, fnnode, & + vnod, nrm, tdir, vt_norm) + mu_eff = contact_tangent_mu(pair, mu, vt_norm) ktan = ZERO do idir = 1, NDIM do jdir = 1, NDIM ktan(idir,jdir) = coeff * nrm(idir) * nrm(jdir) - if (mu .gt. ZERO) then - if (idir .eq. jdir) ktan(idir,jdir) = ktan(idir,jdir) + mu * coeff - ktan(idir,jdir) = ktan(idir,jdir) - mu * coeff * nrm(idir) * nrm(jdir) + if (mu_eff .gt. ZERO) then + if (idir .eq. jdir) ktan(idir,jdir) = ktan(idir,jdir) + mu_eff * coeff + ktan(idir,jdir) = ktan(idir,jdir) - mu_eff * coeff * nrm(idir) * nrm(jdir) end if end do end do - 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 + do_add = maxval(abs(ktan)) .gt. tiny(ONE) + ierr = FEM_SUCCESS + end subroutine contact_prepare_pair_tan + + real(rk) function contact_tangent_mu(pair, mu, vslip) + integer, intent(in) :: pair + real(rk), intent(in) :: mu + real(rk), intent(in) :: vslip + real(rk) :: reg + real(rk) :: slip_scale + + contact_tangent_mu = mu + if (mu .le. ZERO) return + + reg = ZERO + if (allocated(contact_pair_friction_reg)) then + if (pair .ge. 1 .and. pair .le. size(contact_pair_friction_reg)) then + reg = max(ZERO, contact_pair_friction_reg(pair)) + end if + end if + if (reg .le. ZERO) return + + slip_scale = max(ZERO, abs(vslip)) + if (allocated(contact_pair_slip)) then + if (pair .ge. 1 .and. pair .le. size(contact_pair_slip)) then + slip_scale = max(slip_scale, contact_pair_slip(pair)) + end if + end if + contact_tangent_mu = mu * tanh(slip_scale / reg) + end function contact_tangent_mu subroutine add_contact_pair_tan_mortar(kmat, max_nodes, nslave, slave_conn, & nmaster, master_conn, kmat3, ss_w, & diff --git a/src/input/fem_inp_helpers.f90 b/src/input/fem_inp_helpers.f90 index 86b89df..d2d8fb7 100644 --- a/src/input/fem_inp_helpers.f90 +++ b/src/input/fem_inp_helpers.f90 @@ -1001,6 +1001,29 @@ subroutine start_nset(set_index, item_pos, set_name_value) contact_pair_friction_set(pair_index) = .true. end if + call get_keyword_value(line, 'FRICTION_REGULARIZATION', value) + if (len_trim(value) .eq. 0) call get_keyword_value(line, 'FRICTION_REG', value) + if (len_trim(value) .gt. 0) then + read(value,*,iostat=ios) contact_pair_friction_reg(pair_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, & + 'failed to parse *CONTACT PAIR FRICTION_REGULARIZATION', ierr) + return + end if + contact_pair_friction_reg_set(pair_index) = .true. + end if + + call get_keyword_value(line, 'AUGMENT', value) + if (len_trim(value) .eq. 0) call get_keyword_value(line, 'AUGMENT_FACTOR', value) + if (len_trim(value) .gt. 0) then + read(value,*,iostat=ios) contact_pair_aug_factor(pair_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse *CONTACT PAIR AUGMENT', ierr) + return + end if + contact_pair_aug_set(pair_index) = .true. + end if + ierr = FEM_SUCCESS end subroutine parse_contact_pair_header @@ -1029,6 +1052,14 @@ subroutine start_nset(set_index, item_pos, set_name_value) call parse_contact_prop_real(line, prop_index, 'SEARCH', ierr) if (ierr .ne. FEM_SUCCESS) return call parse_contact_prop_real(line, prop_index, 'FRICTION', ierr) + if (ierr .ne. FEM_SUCCESS) return + call parse_contact_prop_real(line, prop_index, 'FRICTION_REGULARIZATION', ierr) + if (ierr .ne. FEM_SUCCESS) return + call parse_contact_prop_real(line, prop_index, 'FRICTION_REG', ierr) + if (ierr .ne. FEM_SUCCESS) return + call parse_contact_prop_real(line, prop_index, 'AUGMENT', ierr) + if (ierr .ne. FEM_SUCCESS) return + call parse_contact_prop_real(line, prop_index, 'AUGMENT_FACTOR', ierr) end subroutine parse_contact_property_header subroutine parse_contact_prop_real(line, prop_index, key, ierr) @@ -1061,6 +1092,7 @@ subroutine start_nset(set_index, item_pos, set_name_value) character(len=LINE_LEN) :: token integer :: ios real(rk) :: mu + real(rk) :: reg if (prop_index .le. 0 .or. prop_index .gt. n_contact_property) then call error_set(FEM_ERR_INPUT, '*FRICTION must follow a contact property', ierr) @@ -1077,6 +1109,17 @@ subroutine start_nset(set_index, item_pos, set_name_value) return end if call set_contact_property_value(prop_index, 'FRICTION', mu) + + call csv_token(line, 2, token) + if (len_trim(token) .gt. 0) then + read(token,*,iostat=ios) reg + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, & + 'failed to parse *FRICTION regularization', ierr) + return + end if + call set_contact_property_value(prop_index, 'FRICTION_REGULARIZATION', reg) + end if ierr = FEM_SUCCESS end subroutine parse_contact_friction_line @@ -1098,6 +1141,12 @@ subroutine start_nset(set_index, item_pos, set_name_value) case ('FRICTION') contact_property_friction(prop_index) = value contact_property_friction_set(prop_index) = .true. + case ('FRICTION_REGULARIZATION', 'FRICTION_REG') + contact_property_friction_reg(prop_index) = value + contact_prop_fric_reg_set(prop_index) = .true. + case ('AUGMENT', 'AUGMENT_FACTOR') + contact_property_aug_factor(prop_index) = value + contact_property_aug_set(prop_index) = .true. end select end subroutine set_contact_property_value diff --git a/src/interface/fem_c_api.c b/src/interface/fem_c_api.c index 1aa4d70..42207c1 100644 --- a/src/interface/fem_c_api.c +++ b/src/interface/fem_c_api.c @@ -21,6 +21,12 @@ #define F_GET_TIME FC_GLOBAL(femlite_f_get_time, FEMLITE_F_GET_TIME) #define F_GET_SIZE FC_GLOBAL(femlite_f_get_size, FEMLITE_F_GET_SIZE) #define F_GET_CONTACT_SUMMARY FC_GLOBAL(femlite_f_get_contact_summary, FEMLITE_F_GET_CONTACT_SUMMARY) +#define F_GET_CONTACT_DIAG FC_GLOBAL(femlite_f_get_contact_diag, FEMLITE_F_GET_CONTACT_DIAG) +#define F_GET_CONTACT_FRICTION FC_GLOBAL(femlite_f_get_contact_friction, FEMLITE_F_GET_CONTACT_FRICTION) +#define F_GET_CHIST_STATS FC_GLOBAL(femlite_f_get_chist_stats, FEMLITE_F_GET_CHIST_STATS) +#define F_GET_CSEARCH_STATS FC_GLOBAL(femlite_f_get_csearch_stats, FEMLITE_F_GET_CSEARCH_STATS) +#define F_GET_CPERF_STATS FC_GLOBAL(femlite_f_get_cperf_stats, FEMLITE_F_GET_CPERF_STATS) +#define F_GET_CCACHE_STATS FC_GLOBAL(femlite_f_get_ccache_stats, FEMLITE_F_GET_CCACHE_STATS) #define F_CLEAR_SPH FC_GLOBAL(femlite_f_clear_sph, FEMLITE_F_CLEAR_SPH) #define F_APPLY_SPH_NODES FC_GLOBAL(femlite_f_apply_sph_nodes, FEMLITE_F_APPLY_SPH_NODES) #define F_GET_FACET_COUNT FC_GLOBAL(femlite_f_get_facet_count, FEMLITE_F_GET_FACET_COUNT) @@ -48,6 +54,35 @@ extern void F_GET_CONTACT_SUMMARY(int *active_count, double *max_penetration, double *force_norm, int *ierr); +extern void F_GET_CONTACT_DIAG(int *active_pair_count, + int *activated_pair_count, + int *maintained_pair_count, + int *released_pair_count, + double *max_gap_residual, + double *max_aug_lambda, + int *ierr); +extern void F_GET_CONTACT_FRICTION(double *work_step, + double *work_total, + double *max_accumulated_slip, + int *ierr); +extern void F_GET_CHIST_STATS(int *slot_count, + double *max_slot_slip, + double *max_slot_work, + double *max_tangent_force, + int *ierr); +extern void F_GET_CSEARCH_STATS(int *facet_count, + int *master_count, + int *broad_count, + int *active_count, + int *bvh_node_count, + int *ierr); +extern void F_GET_CPERF_STATS(double *search_time, + double *force_time, + double *tangent_time, + int *ierr); +extern void F_GET_CCACHE_STATS(int *used_count, + int *overflow_count, + int *ierr); extern void F_CLEAR_SPH(int *ierr); extern void F_APPLY_SPH_NODES(int *num_force_nodes, const int *node_ids, @@ -221,6 +256,89 @@ int femlite_get_contact_summary(int *active_count, return ierr; } +int femlite_get_contact_diagnostics(int *active_pair_count, + int *activated_pair_count, + int *maintained_pair_count, + int *released_pair_count, + double *max_gap_residual, + double *max_aug_lambda) +{ + int ierr = 0; + if (active_pair_count == NULL || activated_pair_count == NULL || + maintained_pair_count == NULL || released_pair_count == NULL || + max_gap_residual == NULL || max_aug_lambda == NULL) { + return -1; + } + F_GET_CONTACT_DIAG(active_pair_count, activated_pair_count, + maintained_pair_count, released_pair_count, + max_gap_residual, max_aug_lambda, &ierr); + return ierr; +} + +int femlite_get_contact_friction_summary(double *work_step, + double *work_total, + double *max_accumulated_slip) +{ + int ierr = 0; + if (!work_step || !work_total || !max_accumulated_slip) { + return -1; + } + F_GET_CONTACT_FRICTION(work_step, work_total, max_accumulated_slip, &ierr); + return ierr; +} + +int femlite_get_contact_history_stats(int *slot_count, + double *max_slot_slip, + double *max_slot_work, + double *max_tangent_force) +{ + int ierr = 0; + if (!slot_count || !max_slot_slip || !max_slot_work || !max_tangent_force) { + return -1; + } + F_GET_CHIST_STATS(slot_count, max_slot_slip, max_slot_work, + max_tangent_force, &ierr); + return ierr; +} + +int femlite_get_contact_search_stats(int *facet_count, + int *master_count, + int *broad_count, + int *active_count, + int *bvh_node_count) +{ + int ierr = 0; + if (!facet_count || !master_count || !broad_count || !active_count || + !bvh_node_count) { + return -1; + } + F_GET_CSEARCH_STATS(facet_count, master_count, broad_count, active_count, + bvh_node_count, &ierr); + return ierr; +} + +int femlite_get_contact_perf_stats(double *search_time, + double *force_time, + double *tangent_time) +{ + int ierr = 0; + if (!search_time || !force_time || !tangent_time) { + return -1; + } + F_GET_CPERF_STATS(search_time, force_time, tangent_time, &ierr); + return ierr; +} + +int femlite_get_contact_cache_stats(int *used_count, int *overflow_count) +{ + int ierr = 0; + if (!used_count || !overflow_count) { + return -1; + } + F_GET_CCACHE_STATS(used_count, overflow_count, &ierr); + return ierr; +} + int femlite_clear_sph_forces(void) { int ierr = 0; diff --git a/src/interface/fem_fortran_bridge.f90 b/src/interface/fem_fortran_bridge.f90 index ada8ec0..4a5cbcf 100644 --- a/src/interface/fem_fortran_bridge.f90 +++ b/src/interface/fem_fortran_bridge.f90 @@ -204,6 +204,93 @@ subroutine femlite_f_get_contact_summary(active_count, work_step, work_total, & max_penetration, force_norm, ierr) end subroutine femlite_f_get_contact_summary +subroutine femlite_f_get_contact_diag(active_pair_count, & + activated_pair_count, & + maintained_pair_count, & + released_pair_count, & + max_gap_residual, max_aug_lambda, ierr) + use fem_api + use fem_precision + implicit none + integer, intent(out) :: active_pair_count + integer, intent(out) :: activated_pair_count + integer, intent(out) :: maintained_pair_count + integer, intent(out) :: released_pair_count + real(rk), intent(out) :: max_gap_residual + real(rk), intent(out) :: max_aug_lambda + integer, intent(out) :: ierr + + call fem_get_contact_diagnostics(active_pair_count, activated_pair_count, & + maintained_pair_count, released_pair_count, max_gap_residual, & + max_aug_lambda, ierr) +end subroutine femlite_f_get_contact_diag + +subroutine femlite_f_get_contact_friction(work_step, work_total, & + max_accumulated_slip, ierr) + use fem_api + use fem_precision + implicit none + real(rk), intent(out) :: work_step + real(rk), intent(out) :: work_total + real(rk), intent(out) :: max_accumulated_slip + integer, intent(out) :: ierr + + call fem_get_contact_friction(work_step, work_total, & + max_accumulated_slip, ierr) +end subroutine femlite_f_get_contact_friction + +subroutine femlite_f_get_chist_stats(slot_count, max_slot_slip, & + max_slot_work, max_tangent_force, ierr) + use fem_api + use fem_precision + implicit none + integer, intent(out) :: slot_count + real(rk), intent(out) :: max_slot_slip + real(rk), intent(out) :: max_slot_work + real(rk), intent(out) :: max_tangent_force + integer, intent(out) :: ierr + + call fem_get_contact_hist_stats(slot_count, max_slot_slip, max_slot_work, & + max_tangent_force, ierr) +end subroutine femlite_f_get_chist_stats + +subroutine femlite_f_get_csearch_stats(facet_count, master_count, broad_count, & + active_count, bvh_node_count, ierr) + use fem_api + implicit none + integer, intent(out) :: facet_count + integer, intent(out) :: master_count + integer, intent(out) :: broad_count + integer, intent(out) :: active_count + integer, intent(out) :: bvh_node_count + integer, intent(out) :: ierr + + call fem_get_contact_search_stats(facet_count, master_count, broad_count, & + active_count, bvh_node_count, ierr) +end subroutine femlite_f_get_csearch_stats + +subroutine femlite_f_get_cperf_stats(search_time, force_time, tangent_time, ierr) + use fem_api + use fem_precision + implicit none + real(rk), intent(out) :: search_time + real(rk), intent(out) :: force_time + real(rk), intent(out) :: tangent_time + integer, intent(out) :: ierr + + call fem_get_contact_perf_stats(search_time, force_time, tangent_time, ierr) +end subroutine femlite_f_get_cperf_stats + +subroutine femlite_f_get_ccache_stats(used_count, overflow_count, ierr) + use fem_api + implicit none + integer, intent(out) :: used_count + integer, intent(out) :: overflow_count + integer, intent(out) :: ierr + + call fem_get_contact_cache_stats(used_count, overflow_count, ierr) +end subroutine femlite_f_get_ccache_stats + subroutine femlite_f_get_facets(nfacet, max_fnodes, facet_id, elem_id, & face_id, facet_nnode, facet_conn, x, vnod, & normal, area, ierr) diff --git a/src/mesh/fem_database.f90 b/src/mesh/fem_database.f90 index f400761..4f0e632 100644 --- a/src/mesh/fem_database.f90 +++ b/src/mesh/fem_database.f90 @@ -3,6 +3,8 @@ module fem_database use fem_error use fem_element_topology use fem_database_state + use fem_contact_candidate_cache, only: contact_candidate_cache_reset + use fem_contact_history, only: contact_history_reset implicit none contains @@ -465,30 +467,15 @@ contains return end if - if (allocated(contact_pair_slave)) deallocate(contact_pair_slave) - if (allocated(contact_pair_master)) deallocate(contact_pair_master) - if (allocated(contact_pair_property)) deallocate(contact_pair_property) - if (allocated(contact_pair_penalty)) deallocate(contact_pair_penalty) - if (allocated(contact_pair_damping)) deallocate(contact_pair_damping) - if (allocated(contact_pair_search)) deallocate(contact_pair_search) - if (allocated(contact_pair_friction)) deallocate(contact_pair_friction) - if (allocated(contact_pair_penalty_set)) deallocate(contact_pair_penalty_set) - if (allocated(contact_pair_damping_set)) deallocate(contact_pair_damping_set) - if (allocated(contact_pair_search_set)) deallocate(contact_pair_search_set) - if (allocated(contact_pair_friction_set)) deallocate(contact_pair_friction_set) - if (allocated(contact_pair_active)) deallocate(contact_pair_active) - 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) + call contact_candidate_cache_reset() + call db_state_free_cpair_arrays() ncontact_pair = num_pairs contact_active_count = 0 contact_work_step = ZERO contact_work_total = ZERO + contact_friction_work_step = ZERO + contact_friction_work_total = ZERO contact_max_penetration = ZERO contact_force_norm = ZERO @@ -497,15 +484,24 @@ contains contact_pair_property(num_pairs), & contact_pair_penalty(num_pairs), contact_pair_damping(num_pairs), & contact_pair_search(num_pairs), contact_pair_friction(num_pairs), & + contact_pair_friction_reg(num_pairs), & + contact_pair_aug_factor(num_pairs), contact_pair_aug_lambda(num_pairs), & contact_pair_penalty_set(num_pairs), & contact_pair_damping_set(num_pairs), & contact_pair_search_set(num_pairs), & contact_pair_friction_set(num_pairs), & - contact_pair_active(num_pairs), contact_pair_work(num_pairs), & + contact_pair_friction_reg_set(num_pairs), & + contact_pair_aug_set(num_pairs), & + contact_pair_active(num_pairs), contact_pair_prev_active(num_pairs), & + contact_pair_status(num_pairs), & + contact_pair_activation_count(num_pairs), & + contact_pair_release_count(num_pairs), contact_pair_work(num_pairs), & contact_pair_max_penetration(num_pairs), & contact_pair_force_norm(num_pairs), & + contact_pair_gap_residual(num_pairs), & contact_pair_tangent_force(NDIM,num_pairs), & - contact_pair_slip(num_pairs), & + contact_pair_slip(num_pairs), contact_pair_friction_work(num_pairs), & + contact_pair_fric_work_total(num_pairs), & contact_pair_stick_count(num_pairs), & contact_pair_slip_count(num_pairs), & stat=stat) @@ -514,25 +510,7 @@ contains call error_set(FEM_ERR_INTERNAL, 'failed to allocate contact pair arrays', ierr) return end if - contact_pair_slave = 'ALL' - contact_pair_master = 'ALL' - contact_pair_property = ' ' - contact_pair_penalty = ZERO - contact_pair_damping = ZERO - contact_pair_search = ZERO - contact_pair_friction = ZERO - contact_pair_penalty_set = .false. - contact_pair_damping_set = .false. - contact_pair_search_set = .false. - contact_pair_friction_set = .false. - contact_pair_active = 0 - 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 + call db_state_init_cpair_arrays() end if ierr = FEM_SUCCESS @@ -557,24 +535,20 @@ contains contact_property_damping(num_props), & contact_property_search(num_props), & contact_property_friction(num_props), & + contact_property_friction_reg(num_props), & + contact_property_aug_factor(num_props), & contact_property_penalty_set(num_props), & contact_property_damping_set(num_props), & contact_property_search_set(num_props), & - contact_property_friction_set(num_props), stat=stat) + contact_property_friction_set(num_props), & + contact_prop_fric_reg_set(num_props), & + contact_property_aug_set(num_props), stat=stat) if (stat .ne. 0) then n_contact_property = 0 call error_set(FEM_ERR_INTERNAL, 'failed to allocate contact property arrays', ierr) return end if - contact_property_name = ' ' - contact_property_penalty = ZERO - contact_property_damping = ZERO - contact_property_search = ZERO - contact_property_friction = ZERO - contact_property_penalty_set = .false. - contact_property_damping_set = .false. - contact_property_search_set = .false. - contact_property_friction_set = .false. + call db_state_init_contact_props() end if ierr = FEM_SUCCESS @@ -718,6 +692,8 @@ contains subroutine database_initialize(ierr) integer, intent(out) :: ierr + call contact_history_reset() + call contact_candidate_cache_reset() if (allocated(coord0)) coord0 = ZERO if (allocated(coord)) coord = ZERO if (allocated(node_id_ext)) node_id_ext = 0 @@ -813,52 +789,11 @@ contains if (allocated(mpc_node)) mpc_node = 0 if (allocated(mpc_dof)) mpc_dof = 0 if (allocated(mpc_coeff)) mpc_coeff = ZERO - call db_init_contact_state() + call db_state_init_contact_arrays() ierr = FEM_SUCCESS end subroutine database_initialize - subroutine db_init_contact_state() - if (allocated(contact_target)) contact_target = ' ' - if (allocated(contact_normal)) contact_normal = ZERO - if (allocated(contact_offset)) contact_offset = ZERO - if (allocated(contact_penalty)) contact_penalty = ZERO - if (allocated(contact_damping)) contact_damping = ZERO - if (allocated(contact_pair_slave)) contact_pair_slave = 'ALL' - if (allocated(contact_pair_master)) contact_pair_master = 'ALL' - if (allocated(contact_pair_property)) contact_pair_property = ' ' - if (allocated(contact_pair_penalty)) contact_pair_penalty = ZERO - if (allocated(contact_pair_damping)) contact_pair_damping = ZERO - if (allocated(contact_pair_search)) contact_pair_search = ZERO - if (allocated(contact_pair_friction)) contact_pair_friction = ZERO - if (allocated(contact_pair_penalty_set)) contact_pair_penalty_set = .false. - if (allocated(contact_pair_damping_set)) contact_pair_damping_set = .false. - if (allocated(contact_pair_search_set)) contact_pair_search_set = .false. - if (allocated(contact_pair_friction_set)) contact_pair_friction_set = .false. - if (allocated(contact_property_name)) contact_property_name = ' ' - if (allocated(contact_property_penalty)) contact_property_penalty = ZERO - if (allocated(contact_property_damping)) contact_property_damping = ZERO - if (allocated(contact_property_search)) contact_property_search = ZERO - if (allocated(contact_property_friction)) contact_property_friction = ZERO - if (allocated(contact_property_penalty_set)) contact_property_penalty_set = .false. - if (allocated(contact_property_damping_set)) contact_property_damping_set = .false. - if (allocated(contact_property_search_set)) contact_property_search_set = .false. - if (allocated(contact_property_friction_set)) contact_property_friction_set = .false. - contact_active_count = 0 - contact_work_step = ZERO - contact_work_total = ZERO - contact_max_penetration = ZERO - contact_force_norm = ZERO - if (allocated(contact_pair_active)) contact_pair_active = 0 - 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 - end subroutine db_init_contact_state - subroutine database_assign_dofs(ierr) integer, intent(out) :: ierr logical, allocatable :: has_translation(:) @@ -1485,6 +1420,15 @@ contains call error_set(FEM_ERR_INPUT, 'contact property friction must be non-negative', ierr) return end if + if (contact_property_friction_reg(prop) .lt. ZERO) then + call error_set(FEM_ERR_INPUT, & + 'contact property friction regularization must be non-negative', ierr) + return + end if + if (contact_property_aug_factor(prop) .lt. ZERO) then + call error_set(FEM_ERR_INPUT, 'contact property augment must be non-negative', ierr) + return + end if end do ierr = FEM_SUCCESS @@ -1518,6 +1462,14 @@ contains contact_property_friction_set(prop)) then contact_pair_friction(pair) = contact_property_friction(prop) end if + if (.not. contact_pair_friction_reg_set(pair) .and. & + contact_prop_fric_reg_set(prop)) then + contact_pair_friction_reg(pair) = contact_property_friction_reg(prop) + end if + if (.not. contact_pair_aug_set(pair) .and. & + contact_property_aug_set(prop)) then + contact_pair_aug_factor(pair) = contact_property_aug_factor(prop) + end if end do ierr = FEM_SUCCESS @@ -1610,6 +1562,19 @@ contains return end if end if + if (allocated(contact_pair_friction_reg)) then + if (contact_pair_friction_reg(item) .lt. ZERO) then + call error_set(FEM_ERR_INPUT, & + 'contact pair friction regularization must be non-negative', ierr) + return + end if + end if + if (allocated(contact_pair_aug_factor)) then + if (contact_pair_aug_factor(item) .lt. ZERO) then + call error_set(FEM_ERR_INPUT, 'contact pair augment must be non-negative', ierr) + return + end if + end if end do ierr = FEM_SUCCESS @@ -1865,6 +1830,7 @@ contains subroutine database_finalize(ierr) integer, intent(out) :: ierr + call contact_history_reset() if (allocated(coord0)) deallocate(coord0) if (allocated(coord)) deallocate(coord) if (allocated(node_id_ext)) deallocate(node_id_ext) diff --git a/src/mesh/fem_database_state.f90 b/src/mesh/fem_database_state.f90 index 703266a..a82ee92 100644 --- a/src/mesh/fem_database_state.f90 +++ b/src/mesh/fem_database_state.f90 @@ -162,30 +162,48 @@ module fem_database_state real(rk_state), allocatable :: contact_pair_damping(:) real(rk_state), allocatable :: contact_pair_search(:) real(rk_state), allocatable :: contact_pair_friction(:) + real(rk_state), allocatable :: contact_pair_friction_reg(:) + real(rk_state), allocatable :: contact_pair_aug_factor(:) + real(rk_state), allocatable :: contact_pair_aug_lambda(:) logical, allocatable :: contact_pair_penalty_set(:) logical, allocatable :: contact_pair_damping_set(:) logical, allocatable :: contact_pair_search_set(:) logical, allocatable :: contact_pair_friction_set(:) + logical, allocatable :: contact_pair_friction_reg_set(:) + logical, allocatable :: contact_pair_aug_set(:) character(len=NAME_LEN_STATE), allocatable :: contact_property_name(:) real(rk_state), allocatable :: contact_property_penalty(:) real(rk_state), allocatable :: contact_property_damping(:) real(rk_state), allocatable :: contact_property_search(:) real(rk_state), allocatable :: contact_property_friction(:) + real(rk_state), allocatable :: contact_property_friction_reg(:) + real(rk_state), allocatable :: contact_property_aug_factor(:) logical, allocatable :: contact_property_penalty_set(:) logical, allocatable :: contact_property_damping_set(:) logical, allocatable :: contact_property_search_set(:) logical, allocatable :: contact_property_friction_set(:) + logical, allocatable :: contact_prop_fric_reg_set(:) + logical, allocatable :: contact_property_aug_set(:) integer :: contact_active_count = 0 real(rk_state) :: contact_work_step = ZERO_STATE real(rk_state) :: contact_work_total = ZERO_STATE + real(rk_state) :: contact_friction_work_step = ZERO_STATE + real(rk_state) :: contact_friction_work_total = ZERO_STATE real(rk_state) :: contact_max_penetration = ZERO_STATE real(rk_state) :: contact_force_norm = ZERO_STATE integer, allocatable :: contact_pair_active(:) + integer, allocatable :: contact_pair_prev_active(:) + integer, allocatable :: contact_pair_status(:) + integer, allocatable :: contact_pair_activation_count(:) + integer, allocatable :: contact_pair_release_count(:) real(rk_state), allocatable :: contact_pair_work(:) real(rk_state), allocatable :: contact_pair_max_penetration(:) real(rk_state), allocatable :: contact_pair_force_norm(:) + real(rk_state), allocatable :: contact_pair_gap_residual(:) real(rk_state), allocatable :: contact_pair_tangent_force(:,:) real(rk_state), allocatable :: contact_pair_slip(:) + real(rk_state), allocatable :: contact_pair_friction_work(:) + real(rk_state), allocatable :: contact_pair_fric_work_total(:) integer, allocatable :: contact_pair_stick_count(:) integer, allocatable :: contact_pair_slip_count(:) @@ -221,18 +239,17 @@ contains if (allocated(contact_property_damping)) deallocate(contact_property_damping) if (allocated(contact_property_search)) deallocate(contact_property_search) if (allocated(contact_property_friction)) deallocate(contact_property_friction) + if (allocated(contact_property_friction_reg)) deallocate(contact_property_friction_reg) + if (allocated(contact_property_aug_factor)) deallocate(contact_property_aug_factor) if (allocated(contact_property_penalty_set)) deallocate(contact_property_penalty_set) if (allocated(contact_property_damping_set)) deallocate(contact_property_damping_set) if (allocated(contact_property_search_set)) deallocate(contact_property_search_set) if (allocated(contact_property_friction_set)) deallocate(contact_property_friction_set) + if (allocated(contact_prop_fric_reg_set)) deallocate(contact_prop_fric_reg_set) + if (allocated(contact_property_aug_set)) deallocate(contact_property_aug_set) end subroutine db_state_free_contact_props - subroutine db_state_free_contact_arrays() - if (allocated(contact_target)) deallocate(contact_target) - if (allocated(contact_normal)) deallocate(contact_normal) - if (allocated(contact_offset)) deallocate(contact_offset) - if (allocated(contact_penalty)) deallocate(contact_penalty) - if (allocated(contact_damping)) deallocate(contact_damping) + subroutine db_state_free_cpair_arrays() if (allocated(contact_pair_slave)) deallocate(contact_pair_slave) if (allocated(contact_pair_master)) deallocate(contact_pair_master) if (allocated(contact_pair_property)) deallocate(contact_pair_property) @@ -240,18 +257,106 @@ contains if (allocated(contact_pair_damping)) deallocate(contact_pair_damping) if (allocated(contact_pair_search)) deallocate(contact_pair_search) if (allocated(contact_pair_friction)) deallocate(contact_pair_friction) + if (allocated(contact_pair_friction_reg)) deallocate(contact_pair_friction_reg) + if (allocated(contact_pair_aug_factor)) deallocate(contact_pair_aug_factor) + if (allocated(contact_pair_aug_lambda)) deallocate(contact_pair_aug_lambda) if (allocated(contact_pair_penalty_set)) deallocate(contact_pair_penalty_set) if (allocated(contact_pair_damping_set)) deallocate(contact_pair_damping_set) if (allocated(contact_pair_search_set)) deallocate(contact_pair_search_set) if (allocated(contact_pair_friction_set)) deallocate(contact_pair_friction_set) + if (allocated(contact_pair_friction_reg_set)) deallocate(contact_pair_friction_reg_set) + if (allocated(contact_pair_aug_set)) deallocate(contact_pair_aug_set) if (allocated(contact_pair_active)) deallocate(contact_pair_active) + if (allocated(contact_pair_prev_active)) deallocate(contact_pair_prev_active) + if (allocated(contact_pair_status)) deallocate(contact_pair_status) + if (allocated(contact_pair_activation_count)) deallocate(contact_pair_activation_count) + if (allocated(contact_pair_release_count)) deallocate(contact_pair_release_count) 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_gap_residual)) deallocate(contact_pair_gap_residual) if (allocated(contact_pair_tangent_force)) deallocate(contact_pair_tangent_force) if (allocated(contact_pair_slip)) deallocate(contact_pair_slip) + if (allocated(contact_pair_friction_work)) deallocate(contact_pair_friction_work) + if (allocated(contact_pair_fric_work_total)) deallocate(contact_pair_fric_work_total) if (allocated(contact_pair_stick_count)) deallocate(contact_pair_stick_count) if (allocated(contact_pair_slip_count)) deallocate(contact_pair_slip_count) + end subroutine db_state_free_cpair_arrays + + subroutine db_state_init_contact_props() + if (allocated(contact_property_name)) contact_property_name = ' ' + if (allocated(contact_property_penalty)) contact_property_penalty = ZERO_STATE + if (allocated(contact_property_damping)) contact_property_damping = ZERO_STATE + if (allocated(contact_property_search)) contact_property_search = ZERO_STATE + if (allocated(contact_property_friction)) contact_property_friction = ZERO_STATE + if (allocated(contact_property_friction_reg)) contact_property_friction_reg = ZERO_STATE + if (allocated(contact_property_aug_factor)) contact_property_aug_factor = ZERO_STATE + if (allocated(contact_property_penalty_set)) contact_property_penalty_set = .false. + if (allocated(contact_property_damping_set)) contact_property_damping_set = .false. + if (allocated(contact_property_search_set)) contact_property_search_set = .false. + if (allocated(contact_property_friction_set)) contact_property_friction_set = .false. + if (allocated(contact_prop_fric_reg_set)) contact_prop_fric_reg_set = .false. + if (allocated(contact_property_aug_set)) contact_property_aug_set = .false. + end subroutine db_state_init_contact_props + + subroutine db_state_init_cpair_arrays() + if (allocated(contact_pair_slave)) contact_pair_slave = 'ALL' + if (allocated(contact_pair_master)) contact_pair_master = 'ALL' + if (allocated(contact_pair_property)) contact_pair_property = ' ' + if (allocated(contact_pair_penalty)) contact_pair_penalty = ZERO_STATE + if (allocated(contact_pair_damping)) contact_pair_damping = ZERO_STATE + if (allocated(contact_pair_search)) contact_pair_search = ZERO_STATE + if (allocated(contact_pair_friction)) contact_pair_friction = ZERO_STATE + if (allocated(contact_pair_friction_reg)) contact_pair_friction_reg = ZERO_STATE + if (allocated(contact_pair_aug_factor)) contact_pair_aug_factor = ZERO_STATE + if (allocated(contact_pair_aug_lambda)) contact_pair_aug_lambda = ZERO_STATE + if (allocated(contact_pair_penalty_set)) contact_pair_penalty_set = .false. + if (allocated(contact_pair_damping_set)) contact_pair_damping_set = .false. + if (allocated(contact_pair_search_set)) contact_pair_search_set = .false. + if (allocated(contact_pair_friction_set)) contact_pair_friction_set = .false. + if (allocated(contact_pair_friction_reg_set)) contact_pair_friction_reg_set = .false. + if (allocated(contact_pair_aug_set)) contact_pair_aug_set = .false. + contact_active_count = 0 + contact_work_step = ZERO_STATE + contact_work_total = ZERO_STATE + contact_friction_work_step = ZERO_STATE + contact_friction_work_total = ZERO_STATE + contact_max_penetration = ZERO_STATE + contact_force_norm = ZERO_STATE + if (allocated(contact_pair_active)) contact_pair_active = 0 + if (allocated(contact_pair_prev_active)) contact_pair_prev_active = 0 + if (allocated(contact_pair_status)) contact_pair_status = 0 + if (allocated(contact_pair_activation_count)) contact_pair_activation_count = 0 + if (allocated(contact_pair_release_count)) contact_pair_release_count = 0 + if (allocated(contact_pair_work)) contact_pair_work = ZERO_STATE + if (allocated(contact_pair_max_penetration)) contact_pair_max_penetration = ZERO_STATE + if (allocated(contact_pair_force_norm)) contact_pair_force_norm = ZERO_STATE + if (allocated(contact_pair_gap_residual)) contact_pair_gap_residual = ZERO_STATE + if (allocated(contact_pair_tangent_force)) contact_pair_tangent_force = ZERO_STATE + if (allocated(contact_pair_slip)) contact_pair_slip = ZERO_STATE + if (allocated(contact_pair_friction_work)) contact_pair_friction_work = ZERO_STATE + if (allocated(contact_pair_fric_work_total)) contact_pair_fric_work_total = ZERO_STATE + if (allocated(contact_pair_stick_count)) contact_pair_stick_count = 0 + if (allocated(contact_pair_slip_count)) contact_pair_slip_count = 0 + end subroutine db_state_init_cpair_arrays + + subroutine db_state_init_contact_arrays() + if (allocated(contact_target)) contact_target = ' ' + if (allocated(contact_normal)) contact_normal = ZERO_STATE + if (allocated(contact_offset)) contact_offset = ZERO_STATE + if (allocated(contact_penalty)) contact_penalty = ZERO_STATE + if (allocated(contact_damping)) contact_damping = ZERO_STATE + call db_state_init_cpair_arrays() + call db_state_init_contact_props() + end subroutine db_state_init_contact_arrays + + subroutine db_state_free_contact_arrays() + if (allocated(contact_target)) deallocate(contact_target) + if (allocated(contact_normal)) deallocate(contact_normal) + if (allocated(contact_offset)) deallocate(contact_offset) + if (allocated(contact_penalty)) deallocate(contact_penalty) + if (allocated(contact_damping)) deallocate(contact_damping) + call db_state_free_cpair_arrays() call db_state_free_contact_props() end subroutine db_state_free_contact_arrays @@ -281,6 +386,8 @@ contains contact_active_count = 0 contact_work_step = ZERO_STATE contact_work_total = ZERO_STATE + contact_friction_work_step = ZERO_STATE + contact_friction_work_total = ZERO_STATE contact_max_penetration = ZERO_STATE contact_force_norm = ZERO_STATE end subroutine db_state_reset_counts diff --git a/src/output/fem_output_csv.f90 b/src/output/fem_output_csv.f90 index 60cadee..570f3cd 100644 --- a/src/output/fem_output_csv.f90 +++ b/src/output/fem_output_csv.f90 @@ -4,6 +4,8 @@ module fem_output_csv use fem_control use fem_database use fem_contact_output + use fem_contact_state, only: contact_get_search_stats, contact_get_perf_stats, & + contact_get_cache_stats, contact_get_pair_stats use fem_element_state implicit none @@ -72,7 +74,8 @@ contains write(unit_id,'(A)',iostat=ios) & 'step,time,total_mass,kinetic_energy,max_displacement,max_velocity,'// & 'max_acceleration,contact_active_count,contact_work_step,'// & - 'contact_work_total,contact_max_penetration,contact_force_norm' + 'contact_work_total,contact_friction_work_step,'// & + 'contact_friction_work_total,contact_max_penetration,contact_force_norm' if (ios .ne. 0) then close(unit_id) call error_set(FEM_ERR_OUTPUT, 'failed to write history CSV header', ierr) @@ -80,11 +83,12 @@ contains end if end if - write(unit_id,'(I12,A,5(ES24.16,A),ES24.16,A,I12,4(A,ES24.16))',iostat=ios) & + write(unit_id,'(I12,A,5(ES24.16,A),ES24.16,A,I12,6(A,ES24.16))',iostat=ios) & step, ',', time, ',', & total_mass, ',', kinetic_energy, ',', max_displacement, ',', & max_velocity, ',', max_acceleration, ',', contact_active_count, & ',', contact_work_step, ',', contact_work_total, ',', & + contact_friction_work_step, ',', contact_friction_work_total, ',', & contact_max_penetration, ',', contact_force_norm if (ios .ne. 0) then close(unit_id) @@ -318,6 +322,16 @@ contains real(rk) :: max_pen real(rk) :: force_sum real(rk) :: work_sum + integer :: search_facets + integer :: search_masters + integer :: search_broad + integer :: search_active + integer :: search_bvh_nodes + real(rk) :: search_time + real(rk) :: force_time + real(rk) :: tangent_time + integer :: cache_used + integer :: cache_overflow if (ncontact_plane .le. 0 .and. ncontact_pair .le. 0) then ierr = FEM_SUCCESS @@ -333,20 +347,40 @@ contains return end if + call contact_get_search_stats(search_facets, search_masters, & + search_broad, search_active, & + search_bvh_nodes) + call contact_get_perf_stats(search_time, force_time, tangent_time) + call contact_get_cache_stats(cache_used, cache_overflow) + write(unit_id,'(A)',iostat=ios) & 'kind,pair_id,active_count,max_penetration,force_norm,work_estimate,'// & - 'cumulative_work,stick_count,slip_count,accumulated_slip,ftx,fty,ftz' + 'cumulative_work,stick_count,slip_count,accumulated_slip,'// & + 'friction_work,friction_work_total,friction_regularization,ftx,fty,ftz,'// & + 'status,activation_count,release_count,gap_residual,aug_factor,aug_lambda,'// & + 'search_facets,search_masters,search_candidates,search_active,'// & + 'search_bvh_nodes,search_cache_used,search_cache_overflow,'// & + 'search_cpu_time,contact_force_cpu_time,'// & + 'contact_tangent_cpu_time' 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),2(A,I12),4(A,ES24.16))',iostat=ios) & + write(unit_id,'(A,A,I12,A,I12,4(A,ES24.16),2(A,I12),'// & + '7(A,ES24.16),3(A,I12),3(A,ES24.16),7(A,I12),'// & + '3(A,ES24.16))',iostat=ios) & 'SUMMARY', ',', 0, ',', contact_active_count, & ',', contact_max_penetration, ',', contact_force_norm, & ',', contact_work_step, ',', contact_work_total, ',', 0, ',', 0, & - ',', ZERO, ',', ZERO, ',', ZERO, ',', ZERO + ',', ZERO, ',', contact_friction_work_step, & + ',', contact_friction_work_total, ',', ZERO, & + ',', ZERO, ',', ZERO, ',', ZERO, & + ',', 0, ',', 0, ',', 0, ',', ZERO, ',', ZERO, ',', ZERO, & + ',', search_facets, ',', search_masters, ',', search_broad, & + ',', search_active, ',', search_bvh_nodes, ',', cache_used, & + ',', cache_overflow, ',', search_time, ',', force_time, ',', tangent_time if (ios .ne. 0) then close(unit_id) call error_set(FEM_ERR_OUTPUT, 'failed to write contact CSV summary row', ierr) @@ -364,15 +398,32 @@ contains end if if (allocated(contact_pair_force_norm)) force_sum = contact_pair_force_norm(pair) if (allocated(contact_pair_work)) work_sum = contact_pair_work(pair) + call contact_get_pair_stats(pair, search_facets, search_masters, & + search_broad, search_active, search_bvh_nodes, cache_used, & + cache_overflow, search_time, force_time, tangent_time) - write(unit_id,'(A,A,I12,A,I12,4(A,ES24.16),2(A,I12),4(A,ES24.16))',iostat=ios) & + write(unit_id,'(A,A,I12,A,I12,4(A,ES24.16),2(A,I12),'// & + '7(A,ES24.16),3(A,I12),3(A,ES24.16),7(A,I12),'// & + '3(A,ES24.16))',iostat=ios) & 'PAIR', ',', pair, ',', active, ',', max_pen, ',', force_sum, & ',', work_sum, ',', ZERO, & ',', contact_pair_stick_value(pair), ',', contact_pair_slip_value(pair), & ',', contact_pair_accumulated_slip(pair), & + ',', contact_pair_fric_work_value(pair), & + ',', contact_pair_fric_total_value(pair), & + ',', contact_pair_fric_reg_value(pair), & ',', contact_pair_tangent_component(pair, 1), & ',', contact_pair_tangent_component(pair, 2), & - ',', contact_pair_tangent_component(pair, 3) + ',', contact_pair_tangent_component(pair, 3), & + ',', contact_pair_status_value(pair), & + ',', contact_pair_activation_value(pair), & + ',', contact_pair_release_value(pair), & + ',', contact_pair_gap_value(pair), & + ',', contact_pair_aug_factor_value(pair), & + ',', contact_pair_aug_lambda_value(pair), & + ',', search_facets, ',', search_masters, ',', search_broad, & + ',', search_active, ',', search_bvh_nodes, ',', cache_used, & + ',', cache_overflow, ',', search_time, ',', force_time, ',', tangent_time if (ios .ne. 0) then close(unit_id) call error_set(FEM_ERR_OUTPUT, 'failed to write contact CSV pair row', ierr) diff --git a/src/output/fem_restart.f90 b/src/output/fem_restart.f90 index 7ee5143..2b346df 100644 --- a/src/output/fem_restart.f90 +++ b/src/output/fem_restart.f90 @@ -4,6 +4,7 @@ module fem_restart use fem_control use fem_database use fem_element_state + use fem_contact_history, only: contact_history_write, contact_history_read implicit none private @@ -45,6 +46,8 @@ contains if (ios .ne. 0) go to 900 call write_restart_gauss(unit_id, ios) if (ios .ne. 0) go to 900 + call contact_history_write(unit_id, ios) + if (ios .ne. 0) go to 900 write(unit_id,'(A)',iostat=ios) 'END' if (ios .ne. 0) go to 900 @@ -91,7 +94,7 @@ contains read(unit_id,*,iostat=ios) tag, version if (ios .ne. 0 .or. trim(tag) .ne. 'FEMLITE_RESTART') go to 910 - if (version .ne. 1) then + if (version .lt. 1 .or. version .gt. 2) then close(unit_id) call error_set(FEM_ERR_OUTPUT, 'unsupported restart version', ierr) return @@ -126,8 +129,11 @@ contains return end if - read(unit_id,*,iostat=ios) tag - if (ios .ne. 0 .or. trim(tag) .ne. 'END') go to 910 + call read_restart_tail(unit_id, ierr) + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if close(unit_id) current_step = file_step @@ -145,7 +151,7 @@ contains integer, intent(in) :: unit_id integer, intent(out) :: ios - write(unit_id,'(A,1X,I4)',iostat=ios) 'FEMLITE_RESTART', 1 + write(unit_id,'(A,1X,I4)',iostat=ios) 'FEMLITE_RESTART', 2 if (ios .ne. 0) return write(unit_id,'(A,4(1X,I12))',iostat=ios) 'COUNTS', nnode, nelem, & ndof, ngauss_total @@ -154,6 +160,50 @@ contains 'TIME', current_step, current_time end subroutine write_restart_header + subroutine read_restart_tail(unit_id, ierr) + integer, intent(in) :: unit_id + integer, intent(out) :: ierr + character(len=256) :: line + character(len=32) :: tag + integer :: ios + integer :: hist_slots + + read(unit_id,'(A)',iostat=ios) line + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, 'restart missing END marker', ierr) + return + end if + + read(line,*,iostat=ios) tag + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, 'invalid restart tail marker', ierr) + return + end if + + if (trim(tag) .eq. 'CONTACT_HISTORY') then + read(line,*,iostat=ios) tag, hist_slots + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, 'invalid restart contact history', ierr) + return + end if + call contact_history_read(unit_id, hist_slots, ierr) + if (ierr .ne. FEM_SUCCESS) return + read(unit_id,*,iostat=ios) tag + if (ios .ne. 0 .or. trim(tag) .ne. 'END') then + call error_set(FEM_ERR_OUTPUT, 'restart missing END marker', ierr) + return + end if + else if (trim(tag) .eq. 'END') then + call contact_history_read(unit_id, 0, ierr) + if (ierr .ne. FEM_SUCCESS) return + else + call error_set(FEM_ERR_OUTPUT, 'invalid restart file tail', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine read_restart_tail + subroutine write_restart_dofs(unit_id, ios) integer, intent(in) :: unit_id integer, intent(out) :: ios diff --git a/tests/data/s4_contact_property.inp b/tests/data/s4_contact_property.inp index c7e0b19..edfb459 100644 --- a/tests/data/s4_contact_property.inp +++ b/tests/data/s4_contact_property.inp @@ -26,7 +26,7 @@ FemLite S4 contact property parser test MASTER, SPOS *SURFACE, NAME=SLAVE_SURF, TYPE=ELEMENT SLAVE, SNEG -*SURFACE INTERACTION, NAME=PROP1, PENALTY=1.0E3, DAMPING=2.0, SEARCH=0.1 +*SURFACE INTERACTION, NAME=PROP1, PENALTY=1.0E3, DAMPING=2.0, SEARCH=0.1, FRICTION_REGULARIZATION=0.05, AUGMENT=0.25 *FRICTION 0.2 *CONTACT PAIR, INTERACTION=PROP1 diff --git a/tests/interface/test_c_api.c b/tests/interface/test_c_api.c index d396b94..1b43ab5 100644 --- a/tests/interface/test_c_api.c +++ b/tests/interface/test_c_api.c @@ -30,6 +30,17 @@ int main(void) int max_face_nodes = 0; int step = 0; int active_contact = -1; + int active_pair_count = -1; + int activated_pair_count = -1; + int maintained_pair_count = -1; + int released_pair_count = -1; + int contact_search_facets = -1; + int contact_search_masters = -1; + int contact_search_broad = -1; + int contact_search_active = -1; + int contact_search_bvh_nodes = -1; + int contact_cache_used = -1; + int contact_cache_overflow = -1; int node_ids[1] = {2}; int facet_id[8]; int elem_id[8]; @@ -50,13 +61,25 @@ int main(void) double contact_work_total = -1.0; double contact_max_penetration = -1.0; double contact_force_norm = -1.0; + double contact_gap_residual = -1.0; + double contact_aug_lambda = -1.0; + double contact_friction_work_step = -1.0; + double contact_friction_work_total = -1.0; + double contact_max_slip = -1.0; + int contact_history_slots = -1; + double max_slot_slip = -1.0; + double max_slot_work = -1.0; + double max_tangent_force = -1.0; + double contact_search_time = -1.0; + double contact_force_time = -1.0; + double contact_tangent_time = -1.0; ierr = femlite_initialize(); if (ierr != 0) return check_ierr(ierr, 1); ierr = femlite_get_version(&major, &minor, &patch); if (ierr != 0) return check_ierr(ierr, 2); - if (major != 0 || minor != 4 || patch != 1) return 3; + if (major != 0 || minor != 4 || patch != 3) return 3; ierr = femlite_read_inp("examples/c3d4_one_element.inp"); if (ierr != 0) return check_ierr(ierr, 4); @@ -76,6 +99,55 @@ int main(void) contact_work_total != 0.0 || contact_max_penetration != 0.0 || contact_force_norm != 0.0) return 25; + ierr = femlite_get_contact_diagnostics(&active_pair_count, + &activated_pair_count, + &maintained_pair_count, + &released_pair_count, + &contact_gap_residual, + &contact_aug_lambda); + if (ierr != 0) return check_ierr(ierr, 26); + if (active_pair_count != 0 || activated_pair_count != 0 || + maintained_pair_count != 0 || released_pair_count != 0 || + contact_gap_residual != 0.0 || contact_aug_lambda != 0.0) return 27; + + ierr = femlite_get_contact_friction_summary(&contact_friction_work_step, + &contact_friction_work_total, + &contact_max_slip); + if (ierr != 0) return check_ierr(ierr, 28); + if (contact_friction_work_step != 0.0 || + contact_friction_work_total != 0.0 || + contact_max_slip != 0.0) return 29; + + ierr = femlite_get_contact_history_stats(&contact_history_slots, + &max_slot_slip, + &max_slot_work, + &max_tangent_force); + if (ierr != 0) return check_ierr(ierr, 30); + if (contact_history_slots != 0 || max_slot_slip != 0.0 || + max_slot_work != 0.0 || max_tangent_force != 0.0) return 31; + + ierr = femlite_get_contact_search_stats(&contact_search_facets, + &contact_search_masters, + &contact_search_broad, + &contact_search_active, + &contact_search_bvh_nodes); + if (ierr != 0) return check_ierr(ierr, 32); + if (contact_search_facets != 0 || contact_search_masters != 0 || + contact_search_broad != 0 || contact_search_active != 0 || + contact_search_bvh_nodes != 0) return 33; + + ierr = femlite_get_contact_perf_stats(&contact_search_time, + &contact_force_time, + &contact_tangent_time); + if (ierr != 0) return check_ierr(ierr, 34); + if (contact_search_time != 0.0 || contact_force_time != 0.0 || + contact_tangent_time != 0.0) return 35; + + ierr = femlite_get_contact_cache_stats(&contact_cache_used, + &contact_cache_overflow); + if (ierr != 0) return check_ierr(ierr, 36); + if (contact_cache_used != 0 || contact_cache_overflow != 0) return 37; + ierr = femlite_compute_lumped_mass(); if (ierr != 0) return check_ierr(ierr, 8); ierr = femlite_estimate_stable_dt(&dtcrit); diff --git a/tests/module/test_contact.f90 b/tests/module/test_contact.f90 index 2216357..31e5e33 100644 --- a/tests/module/test_contact.f90 +++ b/tests/module/test_contact.f90 @@ -3,6 +3,13 @@ program test_contact use fem_error use fem_database use fem_contact + use fem_contact_state, only: CONTACT_PAIR_ACTIVATED, & + CONTACT_PAIR_MAINTAINED, CONTACT_PAIR_RELEASED, & + contact_get_search_stats, contact_get_perf_stats, contact_get_cache_stats, & + contact_get_pair_stats + use fem_contact_candidate_cache, only: contact_candidate_cache_stats + use fem_contact_history, only: contact_history_stats, & + contact_history_state_stats use fem_api use fem_assembly_csr use fem_sparse_csr @@ -14,11 +21,13 @@ program test_contact call check_plane_force() call check_target_node_only() call check_analytic_pair_plane_force() + call check_pair_active_set_augmented() call check_explicit_contact_step() call check_surface_pair_force() call check_surface_pair_shell_sides() call check_surface_pair_tangent() call check_surface_pair_friction() + call check_pair_friction_reg() call check_surface_pair_multimaster() call check_pair_clipped() call check_pair_grid() @@ -134,6 +143,84 @@ contains if (ierr .ne. FEM_SUCCESS) call fail(124) end subroutine check_analytic_pair_plane_force + subroutine check_pair_active_set_augmented() + integer :: ierr + integer :: dof + integer :: active_pair_count + integer :: activated_pair_count + integer :: maintained_pair_count + integer :: released_pair_count + real(rk) :: max_gap_residual + real(rk) :: max_aug_lambda + + call fem_initialize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(145) + call setup_basic_nodes(1, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(125) + call database_allocate_surfaces(2, 2, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(126) + call database_allocate_contact_pair(1, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(127) + + surface_name(1) = 'SLAVE_NODES' + surface_type(1) = 'NODE' + surface_start(1) = 1 + surface_count(1) = 1 + surface_owner(1) = 1 + surface_target(1) = '1' + surface_name(2) = 'RIGID_PLANE' + surface_type(2) = 'ANALYTICAL' + surface_start(2) = 2 + surface_count(2) = 1 + surface_owner(2) = 2 + surface_target(2) = 'PLANE' + surface_plane_normal(:,2) = (/ ZERO, ZERO, ONE /) + surface_plane_offset(2) = ZERO + contact_pair_slave(1) = 'SLAVE_NODES' + contact_pair_master(1) = 'RIGID_PLANE' + contact_pair_penalty(1) = 100.0_rk + contact_pair_damping(1) = ZERO + contact_pair_aug_factor(1) = 0.5_rk + + dof = dof_id(3,1) + u(dof) = -0.01_rk + call contact_apply_plane_forces(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(128) + call check_close(fcontact(dof), ONE, 129) + if (contact_pair_status(1) .ne. CONTACT_PAIR_ACTIVATED) call fail(130) + if (contact_pair_activation_count(1) .ne. 1) call fail(131) + call check_close(contact_pair_gap_residual(1), 0.01_rk, 132) + call check_close(contact_pair_aug_lambda(1), 0.5_rk, 133) + + call contact_apply_plane_forces(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(134) + call check_close(fcontact(dof), 1.5_rk, 135) + if (contact_pair_status(1) .ne. CONTACT_PAIR_MAINTAINED) call fail(136) + if (contact_pair_activation_count(1) .ne. 1) call fail(137) + call check_close(contact_pair_aug_lambda(1), ONE, 138) + call fem_get_contact_diagnostics(active_pair_count, & + activated_pair_count, maintained_pair_count, released_pair_count, & + max_gap_residual, max_aug_lambda, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(146) + if (active_pair_count .ne. 1) call fail(147) + if (activated_pair_count .ne. 0) call fail(148) + if (maintained_pair_count .ne. 1) call fail(149) + if (released_pair_count .ne. 0) call fail(150) + call check_close(max_gap_residual, 0.01_rk, 151) + call check_close(max_aug_lambda, ONE, 152) + + u(dof) = 0.01_rk + call contact_apply_plane_forces(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(139) + call check_close(fcontact(dof), ZERO, 140) + if (contact_pair_status(1) .ne. CONTACT_PAIR_RELEASED) call fail(141) + if (contact_pair_release_count(1) .ne. 1) call fail(142) + call check_close(contact_pair_aug_lambda(1), ZERO, 143) + + call fem_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(144) + end subroutine check_pair_active_set_augmented + subroutine check_explicit_contact_step() integer :: ierr integer :: dof @@ -339,6 +426,10 @@ contains 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 check_close(contact_friction_work_step, 2.0_rk, 504) + call check_close(contact_friction_work_total, 2.0_rk, 505) + call check_close(contact_pair_friction_work(1), 2.0_rk, 506) + call check_close(contact_pair_fric_work_total(1), 2.0_rk, 507) call csr_init(kmat) call assembly_build_csr_pattern(kmat, ierr) @@ -357,6 +448,84 @@ contains if (ierr .ne. FEM_SUCCESS) call fail(58) end subroutine check_surface_pair_friction + subroutine check_pair_friction_reg() + type(csr_matrix) :: kmat + integer :: ierr + integer :: node + integer :: dof + integer :: row + integer :: col + integer :: stat + integer :: slot_count + integer :: stick_slots + integer :: slip_slots + real(rk) :: expected_force + real(rk) :: expected_node_force + real(rk) :: expected_slip + real(rk) :: expected_tangent + real(rk) :: max_slot_slip + real(rk) :: max_slot_work + real(rk) :: max_tangent_force + real(rk) :: value + + call setup_contact_shell_pair(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(520) + contact_pair_friction(1) = 0.2_rk + contact_pair_friction_reg(1) = ONE + do node = 5, 8 + dof = dof_id(1,node) + v(dof) = ONE + end do + + call contact_apply_plane_forces(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(521) + + expected_force = 2.0_rk * tanh(ONE) + expected_node_force = expected_force / 4.0_rk + expected_slip = (1000.0_rk - expected_force) / 1000.0_rk + do node = 1, 4 + dof = dof_id(1,node) + call check_close(fcontact(dof), expected_node_force, 522) + end do + do node = 5, 8 + dof = dof_id(1,node) + call check_close(fcontact(dof), -expected_node_force, 523) + end do + if (contact_pair_slip_count(1) .ne. 1) call fail(524) + call check_close(contact_pair_slip(1), expected_slip, 525) + call check_close(contact_pair_tangent_force(1,1), -expected_force, 526) + call check_close(contact_friction_work_step, expected_force, 527) + call check_close(contact_pair_friction_work(1), expected_force, 528) + call contact_history_stats(slot_count, max_slot_slip, max_slot_work, & + max_tangent_force) + if (slot_count .le. 1) call fail(535) + call check_close(max_slot_slip, expected_slip, 536) + if (max_slot_work .le. ZERO .or. max_slot_work .gt. expected_force) & + call fail(537) + if (max_tangent_force .le. ZERO .or. & + max_tangent_force .gt. expected_force) call fail(538) + call contact_history_state_stats(slot_count, stick_slots, slip_slots) + if (stick_slots .ne. 0) call fail(539) + if (slip_slots .ne. slot_count) call fail(534) + + call csr_init(kmat) + call assembly_build_csr_pattern(kmat, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(529) + call contact_apply_plane_tangent(kmat, ZERO, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(530) + + row = dof_id(1,5) + col = dof_id(1,5) + call csr_get_value(kmat, row, col, value, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(531) + expected_tangent = 200.0_rk * tanh(ONE) / 9.0_rk + call check_close(value, expected_tangent, 532) + + call csr_free(kmat, stat) + call database_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(533) + end subroutine check_pair_friction_reg + subroutine check_surface_pair_multimaster() integer :: ierr integer :: node @@ -388,6 +557,14 @@ contains subroutine check_pair_clipped() integer :: ierr integer :: node + integer :: search_facets + integer :: search_masters + integer :: search_broad + integer :: search_active + integer :: search_bvh_nodes + real(rk) :: search_time + real(rk) :: force_time + real(rk) :: tangent_time call setup_contact_shell_pair(ierr) if (ierr .ne. FEM_SUCCESS) call fail(70) @@ -417,12 +594,34 @@ contains 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 contact_get_search_stats(search_facets, search_masters, & + search_broad, search_active, & + search_bvh_nodes) + if (search_facets .le. 0) call fail(602) + if (search_masters .le. 0) call fail(603) + if (search_broad .le. 0) call fail(604) + if (search_active .le. 0) call fail(605) + if (search_bvh_nodes .ne. 0) call fail(606) + call contact_get_perf_stats(search_time, force_time, tangent_time) + if (search_time .lt. ZERO) call fail(608) + if (force_time .lt. ZERO) call fail(609) + if (tangent_time .lt. ZERO) call fail(610) call output_csv_write(910, ZERO, ierr) if (ierr .ne. FEM_SUCCESS) call fail(600) call require_contains('contact_00000910.csv', & 'kind,pair_id,active_count,max_penetration,force_norm,work_estimate,'// & - 'cumulative_work,stick_count,slip_count,accumulated_slip,ftx,fty,ftz', 601) + 'cumulative_work,stick_count,slip_count,accumulated_slip,'// & + 'friction_work,friction_work_total,friction_regularization,ftx,fty,ftz,'// & + 'status,activation_count,release_count,gap_residual,aug_factor,aug_lambda', 601) + call require_contains('contact_00000910.csv', & + 'search_facets,search_masters,search_candidates,search_active,'// & + 'search_bvh_nodes', 607) + call require_contains('contact_00000910.csv', & + 'search_cache_used,search_cache_overflow', 612) + call require_contains('contact_00000910.csv', & + 'search_cpu_time,contact_force_cpu_time,contact_tangent_cpu_time', 611) + call require_contains('contact_00000910.csv', 'PAIR', 613) call database_finalize(ierr) if (ierr .ne. FEM_SUCCESS) call fail(88) @@ -436,6 +635,28 @@ contains integer :: dof integer :: row integer :: col + integer :: search_facets + integer :: search_masters + integer :: search_broad + integer :: search_active + integer :: search_bvh_nodes + integer :: cache_used + integer :: cache_overflow + integer :: pair_facets + integer :: pair_masters + integer :: pair_broad + integer :: pair_active + integer :: pair_bvh_nodes + integer :: pair_cache_used + integer :: pair_cache_overflow + integer :: cache_hits + integer :: cache_misses + integer :: cache_invalid + integer :: cache_stores + integer :: cache_broad_overflow + real(rk) :: pair_search_time + real(rk) :: pair_force_time + real(rk) :: pair_tangent_time real(rk) :: value call setup_contact_grid_pair(ierr) @@ -458,12 +679,51 @@ contains end do if (contact_active_count .ne. 1) call fail(95) call check_close(contact_force_norm, 10.0_rk, 96) + call contact_get_search_stats(search_facets, search_masters, & + search_broad, search_active, & + search_bvh_nodes) + if (search_bvh_nodes .le. 0) call fail(620) + call contact_get_cache_stats(cache_used, cache_overflow) + if (cache_used .le. 0) call fail(621) + if (cache_overflow .ne. 0) call fail(622) + call contact_get_pair_stats(1, pair_facets, pair_masters, pair_broad, & + pair_active, pair_bvh_nodes, pair_cache_used, pair_cache_overflow, & + pair_search_time, pair_force_time, pair_tangent_time) + if (pair_facets .le. 0) call fail(623) + if (pair_masters .le. 0) call fail(624) + if (pair_broad .le. 0) call fail(625) + if (pair_active .le. 0) call fail(626) + if (pair_bvh_nodes .le. 0) call fail(627) + if (pair_cache_used .le. 0) call fail(628) + if (pair_cache_overflow .ne. 0) call fail(629) + if (pair_search_time .lt. ZERO) call fail(630) + if (pair_force_time .lt. ZERO) call fail(631) + call contact_candidate_cache_stats(cache_hits, cache_misses, & + cache_invalid, cache_stores, cache_broad_overflow) + if (cache_stores .le. 0) call fail(633) + if (cache_hits .ne. 0) call fail(634) + + call contact_apply_plane_forces(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(635) + call contact_get_search_stats(search_facets, search_masters, & + search_broad, search_active, & + search_bvh_nodes) + if (search_bvh_nodes .ne. 0) call fail(636) + call contact_candidate_cache_stats(cache_hits, cache_misses, & + cache_invalid, cache_stores, cache_broad_overflow) + if (cache_hits .le. 0) call fail(637) + if (cache_invalid .ne. 0) call fail(638) 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) + call require_candidate_cache_hit(cache_hits, 642) + call contact_get_pair_stats(1, pair_facets, pair_masters, pair_broad, & + pair_active, pair_bvh_nodes, pair_cache_used, pair_cache_overflow, & + pair_search_time, pair_force_time, pair_tangent_time) + if (pair_tangent_time .lt. ZERO) call fail(632) row = dof_id(3,73) col = dof_id(3,73) call csr_get_value(kmat, row, col, value, ierr) @@ -475,10 +735,33 @@ contains call check_close(value, -1000.0_rk / 9.0_rk, 515) call csr_free(kmat, stat) + do node = 73, 76 + coord0(1,node) = coord0(1,node) + ONE + end do + coord = coord0 + call contact_apply_plane_forces(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(639) + call contact_get_search_stats(search_facets, search_masters, & + search_broad, search_active, & + search_bvh_nodes) + if (search_bvh_nodes .le. 0) call fail(640) + call contact_candidate_cache_stats(cache_hits, cache_misses, & + cache_invalid, cache_stores, cache_broad_overflow) + if (cache_invalid .le. 0) call fail(641) + call database_finalize(ierr) if (ierr .ne. FEM_SUCCESS) call fail(97) end subroutine check_pair_grid + subroutine require_candidate_cache_hit(previous_hits, code) + integer, intent(in) :: previous_hits + integer, intent(in) :: code + integer :: hits, misses, invalid, stores, overflow + + call contact_candidate_cache_stats(hits, misses, invalid, stores, overflow) + if (hits .le. previous_hits) call fail(code) + end subroutine require_candidate_cache_hit + subroutine setup_basic_nodes(num_nodes, ierr) integer, intent(in) :: num_nodes integer, intent(out) :: ierr diff --git a/tests/module/test_implicit_inp.f90 b/tests/module/test_implicit_inp.f90 index ed48245..ee5fac9 100644 --- a/tests/module/test_implicit_inp.f90 +++ b/tests/module/test_implicit_inp.f90 @@ -12,6 +12,9 @@ program test_implicit_inp n_contact_property, contact_property_name, & contact_property_penalty, contact_property_damping, & contact_property_search, contact_property_friction, & + contact_property_friction_reg, & + contact_property_aug_factor, contact_pair_friction_reg, & + contact_pair_aug_factor, & contact_pair_property use fem_database_surface, only: db_contact_facet_match, & db_contact_facet_normal_sign, & @@ -512,6 +515,8 @@ contains call check_close(contact_property_damping(1), 2.0_rk, 535) call check_close(contact_property_search(1), 0.1_rk, 536) call check_close(contact_property_friction(1), 0.2_rk, 537) + call check_close(contact_property_aug_factor(1), 0.25_rk, 545) + call check_close(contact_property_friction_reg(1), 0.05_rk, 547) if (trim(contact_pair_property(1)) .ne. 'PROP1') call fail(538) call fem_build_model(ierr) @@ -520,6 +525,8 @@ contains call check_close(contact_pair_damping(1), 2.0_rk, 541) call check_close(contact_pair_search(1), 0.1_rk, 542) call check_close(contact_pair_friction(1), 0.2_rk, 543) + call check_close(contact_pair_aug_factor(1), 0.25_rk, 546) + call check_close(contact_pair_friction_reg(1), 0.05_rk, 548) call fem_finalize(ierr) if (ierr .ne. 0) call fail(544) diff --git a/tests/module/test_restart.f90 b/tests/module/test_restart.f90 index 3fc01b8..d5f85f1 100644 --- a/tests/module/test_restart.f90 +++ b/tests/module/test_restart.f90 @@ -5,8 +5,12 @@ program test_restart implicit none real(rk), parameter :: DT_TEST = 1.0e-8_rk + real(rk), parameter :: DT_CONTACT = 1.0e-5_rk + integer, parameter :: CONTACT_STEPS = 20 character(len=*), parameter :: RESTART_FILE = & 'build-wsl-debug/restart_check.frs' + character(len=*), parameter :: CONTACT_RESTART_FILE = & + 'build-wsl-debug/contact_history_restart.frs' integer :: ierr integer :: ref_step @@ -36,8 +40,11 @@ program test_restart call check_vector(rst_v, ref_v, 8) call check_vector(rst_a, ref_a, 9) + call check_contact_history_restart(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(11) + call fem_finalize(ierr) - if (ierr .ne. FEM_SUCCESS) call fail(10) + if (ierr .ne. FEM_SUCCESS) call fail(12) contains @@ -120,6 +127,75 @@ contains call fem_advance_explicit(DT_TEST, ierr) end subroutine loaded_step + subroutine check_contact_history_restart(ierr) + integer, intent(out) :: ierr + integer :: slots_before + integer :: slots_after + real(rk) :: slip_before + real(rk) :: work_before + real(rk) :: tangent_before + real(rk) :: slip_after + real(rk) :: work_after + real(rk) :: tangent_after + + call prepare_contact_model(ierr) + if (ierr .ne. FEM_SUCCESS) return + call run_contact_steps(CONTACT_STEPS, ierr) + if (ierr .ne. FEM_SUCCESS) return + call fem_get_contact_hist_stats(slots_before, slip_before, & + work_before, tangent_before, ierr) + if (ierr .ne. FEM_SUCCESS) return + + if (slots_before .le. 0) call fail(13) + if (slip_before .le. ZERO) call fail(14) + if (work_before .le. ZERO) call fail(15) + if (tangent_before .le. ZERO) call fail(16) + + call fem_write_restart(CONTACT_RESTART_FILE, ierr) + if (ierr .ne. FEM_SUCCESS) return + call prepare_contact_model(ierr) + if (ierr .ne. FEM_SUCCESS) return + call fem_get_contact_hist_stats(slots_after, slip_after, work_after, & + tangent_after, ierr) + if (ierr .ne. FEM_SUCCESS) return + if (slots_after .ne. 0) call fail(17) + + call fem_read_restart(CONTACT_RESTART_FILE, ierr) + if (ierr .ne. FEM_SUCCESS) return + call fem_get_contact_hist_stats(slots_after, slip_after, work_after, & + tangent_after, ierr) + if (ierr .ne. FEM_SUCCESS) return + + if (slots_after .ne. slots_before) call fail(18) + call check_close(slip_after, slip_before, 19) + call check_close(work_after, work_before, 20) + call check_close(tangent_after, tangent_before, 21) + end subroutine check_contact_history_restart + + subroutine prepare_contact_model(ierr) + integer, intent(out) :: ierr + + call fem_read_inp('examples/contact_block_sliding/block_sliding_friction.inp', & + ierr) + if (ierr .ne. FEM_SUCCESS) return + call fem_build_model(ierr) + if (ierr .ne. FEM_SUCCESS) return + call fem_compute_lumped_mass(ierr) + end subroutine prepare_contact_model + + subroutine run_contact_steps(step_count, ierr) + integer, intent(in) :: step_count + integer, intent(out) :: ierr + integer :: step + + do step = 1, step_count + call fem_advance_explicit(DT_CONTACT, ierr) + if (ierr .ne. FEM_SUCCESS) return + end do + + ierr = FEM_SUCCESS + end subroutine run_contact_steps + subroutine check_vector(actual, expected, code) real(rk), intent(in) :: actual(NDIM) real(rk), intent(in) :: expected(NDIM) diff --git a/tests/performance/perf_contact_pair.f90 b/tests/performance/perf_contact_pair.f90 new file mode 100644 index 0000000..e193c22 --- /dev/null +++ b/tests/performance/perf_contact_pair.f90 @@ -0,0 +1,247 @@ +program perf_contact_pair + use fem_precision + use fem_error + use fem_database + use fem_contact + use fem_contact_state, only: contact_get_search_stats, & + contact_get_perf_stats, contact_get_cache_stats + use fem_assembly_csr + use fem_sparse_csr + implicit none + + type(csr_matrix) :: kmat + integer :: ierr + integer :: nfacet + integer :: do_tangent + integer :: active_count + integer :: search_facets + integer :: search_masters + integer :: search_broad + integer :: search_active + integer :: search_bvh_nodes + integer :: cache_used + integer :: cache_overflow + real(rk) :: search_time + real(rk) :: force_time + real(rk) :: tangent_time + real(rk) :: t0 + real(rk) :: t1 + + nfacet = 64 + do_tangent = 1 + call parse_args(nfacet, do_tangent) + + call setup_contact_perf_model(nfacet, ierr) + call exit_if_error(ierr, 'setup_contact_perf_model') + + call cpu_time(t0) + call contact_apply_plane_forces(ierr) + call cpu_time(t1) + call exit_if_error(ierr, 'contact_apply_plane_forces') + + if (do_tangent .ne. 0) then + call csr_init(kmat) + call assembly_build_csr_pattern(kmat, ierr) + call exit_if_error(ierr, 'assembly_build_csr_pattern') + call contact_apply_plane_tangent(kmat, ZERO, ierr) + call exit_if_error(ierr, 'contact_apply_plane_tangent') + call csr_free(kmat, ierr) + call exit_if_error(ierr, 'csr_free') + end if + + active_count = contact_active_count + call contact_get_search_stats(search_facets, search_masters, search_broad, & + search_active, search_bvh_nodes) + call contact_get_cache_stats(cache_used, cache_overflow) + call contact_get_perf_stats(search_time, force_time, tangent_time) + + call require_positive(active_count, 'active_count') + call require_positive(search_bvh_nodes, 'search_bvh_nodes') + call require_positive(cache_used, 'cache_used') + if (cache_overflow .ne. 0) call die('unexpected contact cache overflow') + + write(*,'(A)') 'FemLite contact pair performance' + write(*,'(A,I8)') ' facets_per_side: ', nfacet + write(*,'(A,I8)') ' active_count: ', active_count + write(*,'(A,I8)') ' search_facets: ', search_facets + write(*,'(A,I8)') ' search_masters: ', search_masters + write(*,'(A,I8)') ' search_broad: ', search_broad + write(*,'(A,I8)') ' search_active: ', search_active + write(*,'(A,I8)') ' bvh_nodes: ', search_bvh_nodes + write(*,'(A,I8)') ' cache_used: ', cache_used + write(*,'(A,ES12.5)') ' search_cpu: ', search_time + write(*,'(A,ES12.5)') ' force_cpu: ', force_time + write(*,'(A,ES12.5)') ' tangent_cpu: ', tangent_time + write(*,'(A,F12.6)') ' wall_force_cpu: ', t1 - t0 + write(*,'(A)') ' result: PASS' + + call database_finalize(ierr) + call exit_if_error(ierr, 'database_finalize') + +contains + + subroutine parse_args(nfacet_out, do_tangent_out) + integer, intent(inout) :: nfacet_out + integer, intent(inout) :: do_tangent_out + character(len=64) :: arg + integer :: argc + integer :: ios + + intrinsic iargc + intrinsic getarg + + argc = iargc() + if (argc .ge. 1) then + call getarg(1, arg) + read(arg, *, iostat=ios) nfacet_out + if (ios .ne. 0) call die('invalid facet count argument') + end if + if (argc .ge. 2) then + call getarg(2, arg) + read(arg, *, iostat=ios) do_tangent_out + if (ios .ne. 0) call die('invalid tangent flag argument') + end if + if (nfacet_out .lt. 17) call die('facet count must be at least 17') + end subroutine parse_args + + subroutine setup_contact_perf_model(nfacet_in, ierr) + integer, intent(in) :: nfacet_in + integer, intent(out) :: ierr + integer :: num_nodes + integer :: num_elems + integer :: elem + + num_nodes = 8 * nfacet_in + num_elems = 2 * nfacet_in + call database_allocate_model(num_nodes, num_elems, 0, 2, 0, num_elems, & + 1, 0, 1, 0, 0, 0, 0, 0, ierr) + if (ierr .ne. FEM_SUCCESS) return + + do elem = 1, nfacet_in + call fill_shell_pair(elem, nfacet_in) + end do + coord = coord0 + + call fill_sets_and_material(nfacet_in) + call database_assign_dofs(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call database_allocate_contact_pair(1, ierr) + if (ierr .ne. FEM_SUCCESS) return + contact_pair_slave(1) = 'SLAVE' + contact_pair_master(1) = 'MASTER' + contact_pair_penalty(1) = 1000.0_rk + contact_pair_damping(1) = ZERO + contact_pair_search(1) = 0.25_rk + contact_pair_friction(1) = ZERO + ierr = FEM_SUCCESS + end subroutine setup_contact_perf_model + + subroutine fill_shell_pair(index, nfacet_in) + integer, intent(in) :: index + integer, intent(in) :: nfacet_in + integer :: master_node + integer :: slave_node + integer :: slave_elem + real(rk) :: x0 + + master_node = 8 * (index - 1) + slave_node = master_node + 4 + slave_elem = nfacet_in + index + x0 = 2.0_rk * real(index - 1, rk) + + call set_quad_nodes(master_node, x0, ZERO) + call set_quad_nodes(slave_node, x0, -0.01_rk) + call set_shell_elem(index, master_node, 'S4') + call set_shell_elem(slave_elem, slave_node, 'S4') + end subroutine fill_shell_pair + + subroutine set_quad_nodes(node_base, x0, z0) + integer, intent(in) :: node_base + real(rk), intent(in) :: x0 + real(rk), intent(in) :: z0 + integer :: node + + do node = 1, 4 + node_id_ext(node_base + node) = node_base + node + end do + coord0(:,node_base+1) = (/ x0, ZERO, z0 /) + coord0(:,node_base+2) = (/ x0 + ONE, ZERO, z0 /) + coord0(:,node_base+3) = (/ x0 + ONE, ONE, z0 /) + coord0(:,node_base+4) = (/ x0, ONE, z0 /) + end subroutine set_quad_nodes + + subroutine set_shell_elem(elem, node_base, etype) + integer, intent(in) :: elem + integer, intent(in) :: node_base + character(len=*), intent(in) :: etype + + elem_id_ext(elem) = elem + elem_type(elem) = etype + elem_nnode(elem) = 4 + elem_conn(:,elem) = 0 + elem_conn(1:4,elem) = (/ node_base + 1, node_base + 2, & + node_base + 3, node_base + 4 /) + elem_mat(elem) = 1 + elem_sec(elem) = 1 + end subroutine set_shell_elem + + subroutine fill_sets_and_material(nfacet_in) + integer, intent(in) :: nfacet_in + integer :: i + + elset_name(1) = 'MASTER' + elset_start(1) = 1 + elset_count(1) = nfacet_in + do i = 1, nfacet_in + elset_items(i) = i + end do + elset_name(2) = 'SLAVE' + elset_start(2) = nfacet_in + 1 + elset_count(2) = nfacet_in + do i = 1, nfacet_in + elset_items(nfacet_in + i) = nfacet_in + i + end do + + mat_name(1) = 'MAT' + mat_density(1) = ONE + mat_e(1) = 30.0_rk + mat_nu(1) = 0.25_rk + section_type(1) = 'SHELL' + section_elset_name(1) = 'ALL' + section_material_name(1) = 'MAT' + section_thickness(1) = 0.1_rk + end subroutine fill_sets_and_material + + subroutine require_positive(value, label) + integer, intent(in) :: value + character(len=*), intent(in) :: label + + if (value .gt. 0) return + write(*,'(A,A,A,I8)') 'expected positive ', trim(label), ': ', value + stop 1 + end subroutine require_positive + + subroutine exit_if_error(ierr_in, label) + integer, intent(in) :: ierr_in + character(len=*), intent(in) :: label + integer :: code + integer :: ierr_local + character(len=256) :: message + + if (ierr_in .eq. FEM_SUCCESS) return + call fem_get_last_error(code, message, ierr_local) + write(*,'(A,A)') 'failed in ', trim(label) + write(*,'(A,I8)') 'FemLite error code: ', code + write(*,'(A,A)') 'FemLite error: ', trim(message) + stop 1 + end subroutine exit_if_error + + subroutine die(message) + character(len=*), intent(in) :: message + + write(*,'(A,A)') 'perf_contact_pair: ', trim(message) + stop 2 + end subroutine die + +end program perf_contact_pair diff --git a/tests/scripts/check_perf_contact_pair_omp.py b/tests/scripts/check_perf_contact_pair_omp.py new file mode 100644 index 0000000..4b39554 --- /dev/null +++ b/tests/scripts/check_perf_contact_pair_omp.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 +"""Check contact pair performance smoke stats across OpenMP thread counts.""" + +from __future__ import annotations + +import os +import re +import subprocess +import sys +from pathlib import Path + + +METRICS = ( + "active_count", + "search_facets", + "search_masters", + "search_broad", + "search_active", + "bvh_nodes", + "cache_used", +) + + +def run_case(source_root: Path, executable: Path, threads: int) -> dict[str, int]: + env = os.environ.copy() + env["OMP_NUM_THREADS"] = str(threads) + proc = subprocess.run( + [str(executable), "32", "1"], + cwd=source_root, + env=env, + text=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + check=False, + ) + if proc.returncode != 0: + print(proc.stdout, end="") + raise SystemExit( + f"perf_contact_pair failed for OMP_NUM_THREADS={threads}: " + f"exit {proc.returncode}" + ) + return parse_metrics(proc.stdout, threads) + + +def parse_metrics(output: str, threads: int) -> dict[str, int]: + values: dict[str, int] = {} + for line in output.splitlines(): + match = re.match(r"\s*([A-Za-z_]+):\s+([-+]?\d+)\s*$", line) + if match and match.group(1) in METRICS: + values[match.group(1)] = int(match.group(2)) + + missing = [metric for metric in METRICS if metric not in values] + if missing: + print(output, end="") + raise SystemExit( + f"missing metrics for OMP_NUM_THREADS={threads}: {', '.join(missing)}" + ) + if values["active_count"] <= 0: + raise SystemExit(f"no active contact for OMP_NUM_THREADS={threads}") + if values["cache_used"] <= 0: + raise SystemExit(f"no BVH cache use for OMP_NUM_THREADS={threads}") + return values + + +def main(argv: list[str]) -> int: + if len(argv) != 3: + print( + "usage: check_perf_contact_pair_omp.py " + "" + ) + return 2 + + source_root = Path(argv[1]).resolve() + executable = Path(argv[2]).resolve() + serial = run_case(source_root, executable, 1) + parallel = run_case(source_root, executable, 4) + + mismatches = [ + metric + for metric in METRICS + if serial[metric] != parallel[metric] + ] + if mismatches: + print("OpenMP contact performance statistics differ:") + for metric in mismatches: + print( + f" {metric}: OMP=1 {serial[metric]}, " + f"OMP=4 {parallel[metric]}" + ) + return 1 + + print("perf_contact_pair OpenMP consistency: PASS") + for metric in METRICS: + print(f" {metric}: {serial[metric]}") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main(sys.argv)) diff --git a/tests/scripts/run_example_smoke.py b/tests/scripts/run_example_smoke.py index 39f1dea..6f8e1ab 100644 --- a/tests/scripts/run_example_smoke.py +++ b/tests/scripts/run_example_smoke.py @@ -18,6 +18,7 @@ def parse_args() -> argparse.Namespace: parser.add_argument("--dynamic-steps", type=int, default=0) parser.add_argument("--output-interval", type=int, default=0) parser.add_argument("--require-contact", action="store_true") + parser.add_argument("--require-friction", action="store_true") return parser.parse_args() @@ -95,11 +96,39 @@ def check_contact_output(build_dir: Path, case_name: str, step: int) -> None: raise RuntimeError(f"{contact_file} did not record active contact") +def check_friction_output(build_dir: Path, case_name: str, step: int) -> None: + contact_file = build_dir / "example_smoke" / case_name / f"contact_{step:08d}.csv" + if not contact_file.exists(): + raise RuntimeError(f"missing contact output: {contact_file}") + + with contact_file.open(newline="", encoding="utf-8") as handle: + rows = [row for row in csv.DictReader(handle) if row.get("kind") == "PAIR"] + if not rows: + raise RuntimeError(f"{contact_file} does not contain contact pair rows") + + max_slip = max(float(row["accumulated_slip"]) for row in rows) + max_work = max(float(row["friction_work_total"]) for row in rows) + min_work = min(float(row["friction_work_total"]) for row in rows) + max_reg = max(float(row["friction_regularization"]) for row in rows) + max_ft = max( + max(abs(float(row["ftx"])), abs(float(row["fty"])), abs(float(row["ftz"]))) + for row in rows + ) + if max_reg <= 0.0: + raise RuntimeError(f"{contact_file} did not report friction regularization") + if max_slip <= 0.0 or max_ft <= 0.0: + raise RuntimeError(f"{contact_file} did not record frictional slip") + if min_work < -1.0e-12 or max_work <= 0.0: + raise RuntimeError(f"{contact_file} did not record positive friction work") + + def main() -> int: args = parse_args() _, steps = run_case(args) if args.require_contact: check_contact_output(Path(args.build_dir).resolve(), args.case_name, steps) + if args.require_friction: + check_friction_output(Path(args.build_dir).resolve(), args.case_name, steps) return 0 diff --git a/tests/unit/test_smoke.f90 b/tests/unit/test_smoke.f90 index a9f3a2e..7226334 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. 4) stop 4 - if (patch .ne. 1) stop 5 + if (patch .ne. 3) stop 5 call fem_read_inp('examples/c3d4_one_element.inp', ierr) if (ierr .ne. 0) stop 11