FiPy如何查找两个Gmsh生成网格交界面处的节点与边线
FiPy跨独立网格查询交界面的实现方法
FiPy本身没有提供直接查询两个独立网格交界面的内置接口,你可以根据你的使用场景从以下两种方案中选择:
方案1:优先推荐:合并为单个Gmsh网格,直接提取交界面
你现在是分开定义两个Geometry生成两个独立网格,其实可以直接把两个几何定义写在同一段Gmsh脚本里,同时给公共边界定义Physical Line,后续直接在FiPy中提取即可,示例修改后的代码如下:
from fipy import Gmsh2D combinedGeometry = ''' SetFactory("OpenCASCADE"); //set node spacing ns = 1e-1; ns2 = 1e-2; // 硅区几何 x1 = 0; y1 = 0; x2 = 1; y2 = 0.5; Point(1) = {x1, y1, 0, ns}; Point(2) = {x2, y1, 0, ns}; Point(3) = {x2, y2, 0, ns2}; Point(4) = {x1, y2, 0, ns2}; Line(1) = {1, 2}; Line(2) = {2, 3}; Line(3) = {3, 4}; Line(4) = {4, 1}; Curve Loop(1) = {1, 2, 3, 4}; Plane Surface(1) = {1}; Physical Surface("Silicon") = {1}; // 氧化层几何 y1_oxide = 0.5; y2_oxide = 1; Point(5) = {x1, y1_oxide, 0, ns2}; Point(6) = {x2, y1_oxide, 0, ns2}; Point(7) = {x2, y2_oxide, 0, ns}; Point(8) = {x1, y2_oxide, 0, ns}; Line(5) = {5, 6}; Line(6) = {6, 7}; Line(7) = {7, 8}; Line(8) = {8, 5}; Curve Loop(2) = {5, 6, 7, 8}; Plane Surface(2) = {2}; Physical Surface("Oxide") = {2}; // 定义公共交界面为物理线 Physical Line("Si_Oxide_Interface") = {3, 5}; ''' mesh = Gmsh2D(combinedGeometry) # 直接提取交界面的面元/节点 interface_faces = mesh.exteriorFaces & mesh.physicalFaces["Si_Oxide_Interface"] interface_nodes = interface_faces.nodeIDs
这种方案的优势是节点天然对齐,不需要额外做坐标匹配,精度最高,计算量最小。
方案2:针对两个独立网格,手动坐标匹配提取交界面
如果你必须保留两个独立网格的结构,可以通过比对两个网格的边界节点坐标来筛选交界面节点,示例代码如下:
import numpy as np from fipy import Gmsh2D siliconGeometry = ''' SetFactory("OpenCASCADE"); ns = 1e-1; ns2 = 1e-2; x1 = 0; y1 = 0; x2 = 1; y2 = 0.5; Point(1) = {x1, y1, 0, ns}; Point(2) = {x2, y1, 0, ns}; Point(3) = {x2, y2, 0, ns2}; Point(4) = {x1, y2, 0, ns2}; Line(1) = {1, 2}; Line(2) = {2, 3}; Line(3) = {3, 4}; Line(4) = {4, 1}; Curve Loop(1) = {1, 2, 3, 4}; Plane Surface(1) = {1}; Physical Surface("Silicon") = {1}; ''' oxideGeometry = ''' SetFactory("OpenCASCADE"); ns = 1e-1; ns2 = 1e-2; x1 = 0; y1 = 0.5; x2 = 1; y2 = 1; Point(5) = {x1, y1, 0, ns2}; Point(6) = {x2, y1, 0, ns2}; Point(7) = {x2, y2, 0, ns}; Point(8) = {x1, y2, 0, ns}; Line(5) = {5, 6}; Line(6) = {6, 7}; Line(7) = {7, 8}; Line(8) = {8, 5}; Curve Loop(2) = {5, 6, 7, 8}; Plane Surface(2) = {2}; Physical Surface("Oxide") = {2}; ''' m0 = Gmsh2D(siliconGeometry) m1 = Gmsh2D(oxideGeometry) # 取两个网格的所有边界节点坐标 m0_boundary_nodes = m0.exteriorFaces.nodeIDs m0_boundary_coords = m0.vertexCoords[:, m0_boundary_nodes].T m1_boundary_nodes = m1.exteriorFaces.nodeIDs m1_boundary_coords = m1.vertexCoords[:, m1_boundary_nodes].T # 容差匹配坐标重合的节点,容差可以根据你的网格精度调整 tolerance = 1e-6 # 匹配m0上的交界面节点 m0_interface_mask = np.all(np.isin(m0_boundary_coords.round(decimals=6), m1_boundary_coords.round(decimals=6)), axis=1) m0_interface_nodes = m0_boundary_nodes[m0_interface_mask] # 匹配m1上的交界面节点 m1_interface_mask = np.all(np.isin(m1_boundary_coords.round(decimals=6), m0_boundary_coords.round(decimals=6)), axis=1) m1_interface_nodes = m1_boundary_nodes[m1_interface_mask]
注意这种方案要求两个网格在交界面处的节点坐标完全对齐,否则会出现匹配误差,需要根据你的网格大小调整容差数值。
内容的提问来源于stack exchange,提问作者mindful_machina
相关产品推荐
相关产品推荐

