You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在二维非结构化网格节点间沿直线提取插值标量数据?

解决非结构化网格沿固定坐标直线提取标量的问题

核心思路

放弃全局结构化插值方案,改为直接在目标直线上生成采样点,基于原始非结构化点的局部三角剖分做插值——这种方式能完全利用自适应网格的局部密集点分布,避免全局结构化网格导致的细节丢失。

具体实现方法

方法1:纯SciPy工具链(轻量高效)

利用SciPy的三角剖分和二维插值工具,直接针对直线采样点计算标量:

  1. 加载CSV数据,提取坐标和标量字段
  2. 对原始非结构化点做Delaunay三角剖分,生成适配原始点分布的三角网格
  3. 在目标直线上生成高密度采样点(可按需调整密度)
  4. 基于三角剖分做插值,得到每个采样点的标量值
  5. 绘制变化坐标与标量的关系图

代码示例:

import numpy as np
import pandas as pd
from scipy.spatial import Delaunay
from scipy.interpolate import CloughTocher2DInterpolator  # C1连续插值,也可替换为LinearNDInterpolator
import matplotlib.pyplot as plt

# 1. 加载数据
df = pd.read_csv('your_data.csv')
x = df['x-coordinate'].values
y = df['y-coordinate'].values
scalar = df['scalar'].values

# 2. 构建适配原始点的三角剖分
tri = Delaunay(np.column_stack((x, y)))

# 3. 生成目标直线的采样点(示例:y=0.5,x从0到1,1000个点)
num_samples = 1000
sample_x = np.linspace(0, 1, num_samples)
sample_y = np.full(num_samples, 0.5)
sample_points = np.column_stack((sample_x, sample_y))

# 4. 插值计算标量
interp = CloughTocher2DInterpolator(tri, scalar)
sample_scalar = interp(sample_points)

# 5. 绘图
plt.figure(figsize=(10, 6))
plt.plot(sample_x, sample_scalar, '-', linewidth=1.2)
plt.xlabel('x-coordinate')
plt.ylabel('scalar')
plt.title('Scalar Distribution Along y=0.5')
plt.grid(True, alpha=0.3)
plt.show()

方法2:用PyVista处理非结构化网格(专业网格工具)

PyVista专门针对网格数据设计,内置的采样方法能自动利用网格局部结构,操作更简洁:

代码示例:

import numpy as np
import pandas as pd
import pyvista as pv
import matplotlib.pyplot as plt

# 1. 加载数据并转成PyVista兼容的3D点(z轴补0)
df = pd.read_csv('your_data.csv')
points_2d = df[['x-coordinate', 'y-coordinate']].values
points_3d = np.column_stack((points_2d, np.zeros(len(points_2d))))
scalar = df['scalar'].values

# 2. 创建非结构化三角网格
mesh = pv.PolyData(points_3d)
mesh['scalar'] = scalar
mesh = mesh.delaunay_2d()  # 基于原始点生成三角剖分

# 3. 生成目标直线(示例:从(0,0.5,0)到(1,0.5,0),1000个采样点)
line = pv.Line((0, 0.5, 0), (1, 0.5, 0), resolution=1000)

# 4. 在直线上采样插值
sampled_data = mesh.sample(line)

# 5. 提取数据绘图
sample_x = sampled_data.points[:, 0]
sample_scalar = sampled_data['scalar']

plt.figure(figsize=(10, 6))
plt.plot(sample_x, sample_scalar, '-', linewidth=1.2)
plt.xlabel('x-coordinate')
plt.ylabel('scalar')
plt.title('Scalar Distribution Along y=0.5')
plt.grid(True, alpha=0.3)
plt.show()

关键优势说明

  • 三角剖分完全基于原始非结构化点的分布,自适应网格中局部密集的区域会生成更精细的三角形,插值时自然保留这些区域的细节
  • 无需生成全局结构化网格,避免了固定分辨率带来的信息丢失
  • 采样点密度可灵活调整:在需要关注的局部区域可以加密采样,进一步提升细节保留程度

注意事项

  • 如果原始数据存在重复点或共线点,Delaunay剖分可能抛出警告,可先通过df.drop_duplicates(subset=['x-coordinate', 'y-coordinate'])去重
  • 若需要严格线性插值(而非平滑插值),可将CloughTocher2DInterpolator替换为LinearNDInterpolator
  • 对于垂直线(如x=0.3,y从0到1),只需调整采样点的生成逻辑:sample_y = np.linspace(0,1,num_samples),sample_x = np.full(num_samples, 0.3)

内容的提问来源于stack exchange,提问作者Manahara Manatunga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 22:35:15