1.8 KiB
1.8 KiB
在给定几何区域内生成简单三角网格
本教程介绍如何在给定几何区域内生成二维网格。
首先导入所需的库。
以下保留原笔记本的代码和已保存输出;转换过程中未重新执行代码。
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
from meshkernel import GeometryList, MeshKernel
首先,使用 NumPy 从 Deltares 自定义多边形文件 test.pol 中加载数据。
注意,需要忽略文件开头的若干行,以跳过 Deltares 特有的头部数据。
polygon_file_path = Path().absolute() / "data_examples" / "test.pol"
polygon_np = np.loadtxt(polygon_file_path, comments="*", skiprows=8, dtype=np.double)
提取从文件中加载的数据,并按照 MeshKernel 的要求,将其存入 GeometryList。
x_coordinates = np.array(polygon_np[:, 0], dtype=np.double)
y_coordinates = np.array(polygon_np[:, 1], dtype=np.double)
polygon = GeometryList(x_coordinates, y_coordinates)
导入的多边形如下图所示:
fig, ax = plt.subplots()
ax.plot(x_coordinates, y_coordinates, ".-", color="green");
接着创建一个 MeshKernel 实例。
mk = MeshKernel()
现在可以调用 MeshKernel 的 mesh2d_make_triangular_mesh_from_polygon 方法,根据给定多边形生成三角网格。
mk.mesh2d_make_triangular_mesh_from_polygon(polygon)
然后从 MeshKernel 实例中获取状态。
mesh2d_output_0 = mk.mesh2d_get()
绘制生成的网格。
fig, ax = plt.subplots()
mesh2d_output_0.plot_edges(ax, color="black")

