首次提交
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
# 基于网格采样数据的二维网格脊线加密
|
||||
|
||||
[返回示例目录](index.md)
|
||||
|
||||
以下保留原笔记本的代码和已保存输出;转换过程中未重新执行代码。
|
||||
|
||||
首先导入所需的库。
|
||||
|
||||
```python
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from meshkernel import (
|
||||
GeometryList,
|
||||
GriddedSamples,
|
||||
MakeGridParameters,
|
||||
MeshKernel,
|
||||
MeshRefinementParameters,
|
||||
RefinementType,
|
||||
GriddedSamples,
|
||||
)
|
||||
```
|
||||
|
||||
`meshkernel` 提供了一组便捷方法,用于创建常见网格。
|
||||
|
||||
这里使用 `curvilinear_compute_rectangular_grid` 方法创建一个简单的曲线网格。该方法的完整参数请参阅相应文档。
|
||||
|
||||
```python
|
||||
mk = MeshKernel()
|
||||
|
||||
num_rows = 21
|
||||
num_columns = 41
|
||||
|
||||
make_grid_parameters = MakeGridParameters()
|
||||
make_grid_parameters.num_columns = num_columns
|
||||
make_grid_parameters.num_rows = num_rows
|
||||
make_grid_parameters.angle = 0.0
|
||||
make_grid_parameters.origin_x = 0.0
|
||||
make_grid_parameters.origin_y = 0.0
|
||||
make_grid_parameters.block_size_x = 10.0
|
||||
make_grid_parameters.block_size_y = 10.0
|
||||
|
||||
mk.curvilinear_compute_rectangular_grid(make_grid_parameters)
|
||||
```
|
||||
|
||||
将曲线网格转换为非结构网格,并获取生成的 `mesh2d`。
|
||||
|
||||
```python
|
||||
mk.curvilinear_convert_to_mesh2d()
|
||||
mesh2d_input = mk.mesh2d_get()
|
||||
```
|
||||
|
||||
生成的网格可以按如下方式可视化。
|
||||
|
||||
```python
|
||||
fig, ax = plt.subplots()
|
||||
mesh2d_input.plot_edges(ax, color="black")
|
||||
```
|
||||
|
||||

|
||||
|
||||
定义均匀间距的网格采样数据。
|
||||
|
||||
```python
|
||||
def read_asc_file(file_path, dtype=np.float32):
|
||||
"""读取 ASC 文件,返回文件头和 NumPy 数组形式的数据。
|
||||
参数:
|
||||
file_path (str):文件路径。
|
||||
返回值:
|
||||
header:ASCII 文件头。
|
||||
data:以双精度 NumPy 数组表示的 ASCII 数据。
|
||||
"""
|
||||
|
||||
header = {}
|
||||
data = []
|
||||
|
||||
with open(file_path, "r") as file:
|
||||
# 读取文件头信息
|
||||
for _ in range(6):
|
||||
line = file.readline().strip().split()
|
||||
header[line[0]] = float(line[1])
|
||||
|
||||
# 读取数据值
|
||||
for line in file:
|
||||
data_row = [float(value) for value in line.strip().split()]
|
||||
data.insert(0, data_row) # 将该行插入到开头
|
||||
|
||||
# 将数据展平
|
||||
data = np.array(data).flatten().astype(dtype)
|
||||
|
||||
return header, data
|
||||
```
|
||||
|
||||
```python
|
||||
header, values_np = read_asc_file("./data_examples/gaussian_bump.asc", dtype=np.float32)
|
||||
```
|
||||
|
||||
绘制从 ASC 文件读取的数据。
|
||||
|
||||
```python
|
||||
values_np_matrix = np.reshape(values_np, (int(header["nrows"]), int(header["ncols"])))
|
||||
plt.imshow(values_np_matrix, cmap="viridis", interpolation="nearest")
|
||||
plt.title("gaussian bump")
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
假设间距均匀,将 ASCII 数据存入 `GriddedSamples` 实例。
|
||||
|
||||
```python
|
||||
num_sample_x_coordinates = (num_columns - 1) * 2 + 1
|
||||
num_sample_y_coordinates = (num_rows - 1) * 2 + 1
|
||||
gridded_samples = GriddedSamples(
|
||||
num_x=num_sample_x_coordinates,
|
||||
num_y=num_sample_y_coordinates,
|
||||
x_origin=0.0,
|
||||
y_origin=-0.0,
|
||||
cell_size=5.0,
|
||||
values=values_np,
|
||||
)
|
||||
```
|
||||
|
||||
设置网格加密算法的参数。注意,必须正确设置脊线加密类型。
|
||||
|
||||
```python
|
||||
refinement_params = MeshRefinementParameters(
|
||||
refine_intersected=False,
|
||||
use_mass_center_when_refining=False,
|
||||
min_edge_size=2.0,
|
||||
refinement_type=RefinementType.RIDGE_DETECTION,
|
||||
connect_hanging_nodes=True,
|
||||
account_for_samples_outside_face=False,
|
||||
max_refinement_iterations=1,
|
||||
)
|
||||
```
|
||||
|
||||
现在可以执行加密。
|
||||
|
||||
```python
|
||||
relative_search_radius = 1.01
|
||||
minimum_num_samples = 1
|
||||
number_of_smoothing_iterations = 0
|
||||
|
||||
mk.mesh2d_refine_ridges_based_on_gridded_samples(
|
||||
gridded_samples=gridded_samples,
|
||||
relative_search_radius=relative_search_radius,
|
||||
minimum_num_samples=minimum_num_samples,
|
||||
number_of_smoothing_iterations=number_of_smoothing_iterations,
|
||||
mesh_refinement_params=refinement_params,
|
||||
)
|
||||
```
|
||||
|
||||
绘制加密后的网格。
|
||||
|
||||
```python
|
||||
mesh2d_output = mk.mesh2d_get()
|
||||
fig, ax = plt.subplots()
|
||||
mesh2d_output.plot_edges(ax, color="black")
|
||||
```
|
||||
|
||||

|
||||
Reference in New Issue
Block a user