# 一维网格基础 本教程介绍一维网格的处理方式,以及一维网格与二维网格之间的交互。 [返回示例目录](index.md) 以下保留原笔记本的代码和已保存输出;转换过程中未重新执行代码。 首先导入所需的库。 ```python import matplotlib.pyplot as plt import numpy as np from meshkernel import Mesh1d, GeometryList, MakeGridParameters, MeshKernel ``` 首先,使用正弦函数上的八个点描述一维网格。原说明写为六个点,此处按下方 `np.linspace(..., 8)` 代码修正。 ```python node_x = np.linspace(0, 2 * np.pi, 8) node_y = np.sin(node_x) ``` 为了形成一条连续的折线,将每个点与下一个点相连。 ```python edge_nodes = np.zeros(node_x.size * 2, np.int32) edge_index = 0 for node_index in range(node_x.size - 1): edge_nodes[edge_index] = node_index edge_index += 1 edge_nodes[edge_index] = node_index + 1 edge_index += 1 ``` 然后创建 `Mesh1d` 实例。 ```python mesh1d_input = Mesh1d(node_x, node_y, edge_nodes) ``` 创建 `MeshKernel` 实例。 ```python mk = MeshKernel() ``` 使用 `curvilinear_compute_rectangular_grid` 方法创建一个简单的曲线网格。该方法的完整参数请参阅相应文档。 ```python make_grid_parameters = MakeGridParameters() make_grid_parameters.num_columns = 7 make_grid_parameters.num_rows = 3 make_grid_parameters.angle = 0.0 make_grid_parameters.origin_x = -0.1 make_grid_parameters.origin_y = -1.5 make_grid_parameters.block_size_x = 1.0 make_grid_parameters.block_size_y = 1.0 mk.curvilinear_compute_rectangular_grid(make_grid_parameters) ``` 将曲线网格转换为非结构 `mesh2d`,并从 `MeshKernel` 中获取该网格。 ```python mk.curvilinear_convert_to_mesh2d() mesh2d_input = mk.mesh2d_get() ``` 设置 `mesh1d`。 ```python mk.mesh1d_set(mesh1d_input) ``` 当前网格如下图所示: ```python fig, ax = plt.subplots() mesh1d_input.plot_edges(ax, color="blue") mesh2d_input.plot_edges(ax, color="black") ``` ![单元格 18 的绘图输出](images/02_mesh1d_basics_18_0.png) 同时使用一维和二维网格时,通常需要在它们之间建立连接(contacts)。 所有连接计算方法都需要节点掩码,用于确定哪些一维节点应参与连接。 本例考虑所有节点。 ```python node_mask = np.full(mesh1d_input.node_x.size, True) ``` 调用 `contacts_compute_multiple` 方法建立连接。 ```python mk.contacts_compute_multiple(node_mask) ``` 然后从 `MeshKernel` 实例中获取状态。 ```python mesh1d_output_0 = mk.mesh1d_get() mesh2d_output_0 = mk.mesh2d_get() contacts_output_0 = mk.contacts_get() ``` 可以看到,一维节点与二维网格面之间已经建立了连接。 ```python fig, ax = plt.subplots() mesh1d_output_0.plot_edges(ax, color="blue") mesh2d_output_0.plot_edges(ax, color="black") contacts_output_0.plot_edges(ax, mesh1d_output_0, mesh2d_output_0, color="red") ``` ![单元格 26 的绘图输出](images/02_mesh1d_basics_26_0.png)