From 10777d836c1617d1555c99fb51e4345f06a74c00 Mon Sep 17 00:00:00 2001 From: shenw Date: Wed, 24 Jun 2026 19:06:37 +0800 Subject: [PATCH] Add node surface query API --- docs/v4_development_plan.md | 4 +- src/base/fem_api.f90 | 33 +++++++ src/mesh/fem_database_surface.f90 | 148 ++++++++++++++++++++++++++++- tests/module/test_implicit_inp.f90 | 16 ++++ 4 files changed, 197 insertions(+), 4 deletions(-) diff --git a/docs/v4_development_plan.md b/docs/v4_development_plan.md index 64feab1..213032b 100644 --- a/docs/v4_development_plan.md +++ b/docs/v4_development_plan.md @@ -615,6 +615,7 @@ FemLite 0.4.x 定位为: - `*SURFACE` 的 `TYPE` 缺省仍为 `ELEMENT`,保持 0.3.3 输入兼容。 - `TYPE=NODE` 可读取节点号、节点集或 `ALL` 目标,并在数据库校验阶段检查节点/节点集是否存在。 - `TYPE=NODE` 数据行暂不允许 face label。 +- 新增 `fem_get_surface_node_count` / `fem_get_surface_nodes`,外部驱动可按 surface 名称查询 node-based surface 的原始节点编号。 - contact pair 仍基于 facet 接触路径;若 contact pair 直接引用 node-based surface,数据库校验阶段报错,避免静默无效接触。 - facet 匹配函数仅对 `ELEMENT` surface 生效,避免把 node surface 的节点号误匹配为单元号。 - 新增 `*SURFACE INTERACTION` / `*CONTACT PROPERTY` 子集,支持法向 penalty、damping、search 和 `*FRICTION` 摩擦系数。 @@ -626,12 +627,11 @@ FemLite 0.4.x 定位为: - `tests/data/s4_node_surface.inp`:node-based surface 解析和数据库校验。 - `tests/data/s4_contact_property.inp`:contact property 解析、friction 读取和 contact pair 参数继承。 - `tests/data/s4_contact_pair_unknown_property.inp`:未知 `INTERACTION` 名称在模型构建阶段报错。 -- `implicit_inp`:检查 `surface_type`、node target、facet 匹配隔离和 `fem_build_model`。 +- `implicit_inp`:检查 `surface_type`、node target、facet 匹配隔离、node surface 查询接口和 `fem_build_model`。 - `contact`:确认旧 element surface/contact pair 路径不回退。 - `source_size`:确认数据库状态拆分后源文件不超过硬性行数限制。 后续: -- 增加 node-based surface 查询接口。 - 在节点-面接触或后续 contact pair 扩展中接入 node-based slave surface。 - 增加 analytic rigid surface 子集。 diff --git a/src/base/fem_api.f90 b/src/base/fem_api.f90 index 60d3d09..35bca72 100644 --- a/src/base/fem_api.f90 +++ b/src/base/fem_api.f90 @@ -13,6 +13,7 @@ module fem_api use fem_coupling use fem_boundary use fem_contact + use fem_database_surface, only: db_surface_node_count, db_get_surface_nodes use fem_output_csv use fem_output_vtk use fem_restart @@ -349,6 +350,38 @@ contains facet_conn, x, vnod, normal, area, ierr) end subroutine fem_get_boundary_facets + subroutine fem_get_surface_node_count(surface, node_count, ierr) + character(len=*), intent(in) :: surface + integer, intent(out) :: node_count + integer, intent(out) :: ierr + + node_count = 0 + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_get_surface_node_count called before fem_initialize', ierr) + return + end if + + call db_surface_node_count(surface, node_count, ierr) + end subroutine fem_get_surface_node_count + + subroutine fem_get_surface_nodes(surface, max_nodes, node_ids, node_count, ierr) + character(len=*), intent(in) :: surface + integer, intent(in) :: max_nodes + integer, intent(out) :: node_ids(max_nodes) + integer, intent(out) :: node_count + integer, intent(out) :: ierr + + node_count = 0 + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, & + 'fem_get_surface_nodes called before fem_initialize', ierr) + return + end if + + call db_get_surface_nodes(surface, max_nodes, node_ids, node_count, ierr) + end subroutine fem_get_surface_nodes + subroutine fem_apply_sph_facet_pressure(nfacet_in, facet_id, pressure, ierr) integer, intent(in) :: nfacet_in integer, intent(in) :: facet_id(nfacet_in) diff --git a/src/mesh/fem_database_surface.f90 b/src/mesh/fem_database_surface.f90 index 01cdb3d..45bbf1f 100644 --- a/src/mesh/fem_database_surface.f90 +++ b/src/mesh/fem_database_surface.f90 @@ -1,17 +1,161 @@ module fem_database_surface use fem_precision, only: NAME_LEN + use fem_error use fem_element_topology use fem_database, only: n_surface, n_surface_item, surface_owner, & surface_target, surface_face, surface_type, & database_find_surface, database_find_elset, & - database_find_elem, elset_start, elset_count, & - elset_items, elem_type, db_upper, & + database_find_elem, database_find_node, & + database_find_nset, elset_start, elset_count, & + elset_items, nset_start, nset_count, nset_items, & + nnode, node_id_ext, elem_type, db_upper, & surface_face_normal_sign, & surface_face_has_explicit_side implicit none + public :: db_surface_node_count + public :: db_get_surface_nodes + contains + subroutine db_surface_node_count(name, node_count, ierr) + character(len=*), intent(in) :: name + integer, intent(out) :: node_count + integer, intent(out) :: ierr + integer, allocatable :: nodes(:) + integer :: stat + + node_count = 0 + allocate(nodes(max(1, nnode)), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate surface node query', ierr) + return + end if + call collect_surface_nodes(name, nodes, node_count, ierr) + deallocate(nodes) + end subroutine db_surface_node_count + + subroutine db_get_surface_nodes(name, max_nodes, node_ids, node_count, ierr) + character(len=*), intent(in) :: name + integer, intent(in) :: max_nodes + integer, intent(out) :: node_ids(max_nodes) + integer, intent(out) :: node_count + integer, intent(out) :: ierr + integer, allocatable :: nodes(:) + integer :: stat + integer :: item + + node_ids = 0 + node_count = 0 + if (max_nodes .lt. 0) then + call error_set(FEM_ERR_INPUT, 'surface node output size is negative', ierr) + return + end if + allocate(nodes(max(1, nnode)), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate surface node query', ierr) + return + end if + call collect_surface_nodes(name, nodes, node_count, ierr) + if (ierr .eq. FEM_SUCCESS .and. node_count .gt. max_nodes) then + call error_set(FEM_ERR_INPUT, 'surface node output array is too small', ierr) + else if (ierr .eq. FEM_SUCCESS) then + do item = 1, node_count + node_ids(item) = node_id_ext(nodes(item)) + end do + end if + deallocate(nodes) + end subroutine db_get_surface_nodes + + subroutine collect_surface_nodes(name, nodes, node_count, ierr) + character(len=*), intent(in) :: name + integer, intent(inout) :: nodes(:) + integer, intent(out) :: node_count + integer, intent(out) :: ierr + integer :: surface_id + integer :: pos + + node_count = 0 + surface_id = database_find_surface(adjustl(name)) + if (surface_id .eq. 0) then + call error_set(FEM_ERR_INPUT, 'surface node query references unknown surface', ierr) + return + end if + if (trim(surface_type(surface_id)) .ne. 'NODE') then + call error_set(FEM_ERR_INPUT, 'surface node query requires TYPE=NODE', ierr) + return + end if + do pos = 1, n_surface_item + if (surface_owner(pos) .eq. surface_id) then + call add_surface_target_nodes(surface_target(pos), nodes, node_count, ierr) + if (ierr .ne. FEM_SUCCESS) return + end if + end do + ierr = FEM_SUCCESS + end subroutine collect_surface_nodes + + subroutine add_surface_target_nodes(target, nodes, node_count, ierr) + character(len=*), intent(in) :: target + integer, intent(inout) :: nodes(:) + integer, intent(inout) :: node_count + integer, intent(out) :: ierr + integer :: external_id + integer :: ios + integer :: node + integer :: set_id + integer :: pos + + if (trim(target) .eq. 'ALL') then + do node = 1, nnode + call add_unique_node(node, nodes, node_count) + end do + ierr = FEM_SUCCESS + return + end if + + read(target,*,iostat=ios) external_id + if (ios .eq. 0) then + node = database_find_node(external_id) + if (node .eq. 0) then + call error_set(FEM_ERR_INPUT, 'node surface references unknown node', ierr) + return + end if + call add_unique_node(node, nodes, node_count) + ierr = FEM_SUCCESS + return + end if + + set_id = database_find_nset(target) + if (set_id .eq. 0) then + call error_set(FEM_ERR_INPUT, 'node surface references unknown node set', ierr) + return + end if + do pos = nset_start(set_id), nset_start(set_id) + nset_count(set_id) - 1 + node = database_find_node(nset_items(pos)) + if (node .eq. 0) then + call error_set(FEM_ERR_INPUT, 'node surface set contains unknown node', ierr) + return + end if + call add_unique_node(node, nodes, node_count) + end do + ierr = FEM_SUCCESS + end subroutine add_surface_target_nodes + + subroutine add_unique_node(node, nodes, node_count) + integer, intent(in) :: node + integer, intent(inout) :: nodes(:) + integer, intent(inout) :: node_count + integer :: item + + do item = 1, node_count + if (nodes(item) .eq. node) return + end do + if (node_count .lt. size(nodes)) then + node_count = node_count + 1 + nodes(node_count) = node + end if + end subroutine add_unique_node + logical function db_contact_elem_match(elem_ext, target) integer, intent(in) :: elem_ext character(len=*), intent(in) :: target diff --git a/tests/module/test_implicit_inp.f90 b/tests/module/test_implicit_inp.f90 index 4c3221a..45c67e4 100644 --- a/tests/module/test_implicit_inp.f90 +++ b/tests/module/test_implicit_inp.f90 @@ -503,6 +503,9 @@ contains subroutine check_node_surface_inp() integer :: ierr + integer :: node_count + integer :: node_ids(4) + integer :: small_node_ids(2) call fem_initialize(ierr) if (ierr .ne. 0) call fail(480) @@ -521,6 +524,19 @@ contains call fem_build_model(ierr) if (ierr .ne. 0) call fail(490) + call fem_get_surface_node_count('NODE_SURF', node_count, ierr) + if (ierr .ne. 0) call fail(492) + if (node_count .ne. 3) call fail(493) + + call fem_get_surface_nodes('NODE_SURF', 4, node_ids, node_count, ierr) + if (ierr .ne. 0) call fail(494) + if (node_count .ne. 3) call fail(495) + if (node_ids(1) .ne. 3 .or. node_ids(2) .ne. 4 .or. & + node_ids(3) .ne. 1) call fail(496) + + call fem_get_surface_nodes('NODE_SURF', 2, small_node_ids, node_count, ierr) + if (ierr .eq. 0) call fail(497) + call fem_finalize(ierr) if (ierr .ne. 0) call fail(491) end subroutine check_node_surface_inp