- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我维护a little Python package在用于网格表示的不同格式之间进行转换 à la
这些文件可能会变得很大,因此在使用 Python 读取它们时,高效地读取它们很重要。
最常用的格式之一是 msh来自 Gmsh .不幸的是,它的数据布局可以说不是最好的。示例文件:
$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
8
1 -0.5 -0.5 -0.5
2 0.5 -0.5 -0.5
3 -0.5 0.5 -0.5
4 0.5 0.5 -0.5
5 -0.5 -0.5 0.5
6 0.5 -0.5 0.5
7 -0.5 0.5 0.5
8 0.5 0.5 0.5
$EndNodes
$Elements
2
1 4 2 1 11 1 2 3 5
2 4 2 1 11 2 5 6 8
$EndElements
对于$Nodes
:
第一个数字 (8
) 是要跟随的节点数。
在每个节点行中,第一个数字是索引(格式的一部分实际上不需要,呃),然后是三个空间坐标。
到目前为止,我还没有想出比 for
循环中的 islice
更好的东西,它非常慢。
# The first line is the number of nodes
line = next(islice(f, 1))
num_nodes = int(line)
#
points = numpy.empty((num_nodes, 3))
for k, line in enumerate(islice(f, num_nodes)):
points[k, :] = numpy.array(line.split(), dtype=float)[1:]
line = next(islice(f, 1))
assert line.strip() == '$EndNodes'
对于 $Elements
:
第一个数字 (2
) 是后面的元素数。
在每个元素行中,第一个数字是索引,然后是元素类型的枚举(4
是四面体) .然后是此元素的整数标记数(此处每种情况下均为2
,即1
和11
) .对应于元素类型,此行中的最后几个条目对应于构成元素的 $Node
索引 - 在四面体的情况下,最后四个条目。
由于标记的数量可能因元素而异(即,行与行),就像元素类型和节点索引的数量一样,每行可能有不同数量的整数。
<对于 $Nodes
和 $Elements
,如果能帮助我们快速读取这些数据,我们将不胜感激。
最佳答案
这是一个基于 NumPy 的有点奇怪的实现:
f = open('foo.msh')
f.readline() # '$MeshFormat\n'
f.readline() # '2.2 0 8\n'
f.readline() # '$EndMeshFormat\n'
f.readline() # '$Nodes\n'
n_nodes = int(f.readline()) # '8\n'
nodes = numpy.fromfile(f,count=n_nodes*4, sep=" ").reshape((n_nodes,4))
# array([[ 1. , -0.5, -0.5, -0.5],
# [ 2. , 0.5, -0.5, -0.5],
# [ 3. , -0.5, 0.5, -0.5],
# [ 4. , 0.5, 0.5, -0.5],
# [ 5. , -0.5, -0.5, 0.5],
# [ 6. , 0.5, -0.5, 0.5],
# [ 7. , -0.5, 0.5, 0.5],
# [ 8. , 0.5, 0.5, 0.5]])
f.readline() # '$EndNodes\n'
f.readline() # '$Elements\n'
n_elems = int(f.readline()) # '2\n'
elems = numpy.fromfile(f,sep=" ")[:-1] # $EndElements read as -1
# This array must be reshaped based on the element type(s)
# array([ 1., 4., 2., 1., 11., 1., 2., 3., 5., 2., 4.,
# 2., 1., 11., 2., 5., 6., 8.])
关于python - 从文件的一部分快速读取格式化数据(Gmsh 网格格式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41641505/
请问如何在 ubuntu 16.04LTS 上安装最新版本的 gmsh,即 gmsh 3.0.7?使用 sudo apt install gmsh 它只会安装 gmsh 2.10.1。 我也更新和升级
我正在尝试从 gmsh 中的简单几何形状构建大型结构,并且我想使用结构化(四边形)网格。我首先创建该形状,然后根据需要经常复制和翻译它以构建我的最终结构。 问题是,即使我将原始形状的线和面定义为超限的
我刚刚在 gmsh 中用直线连接了两个点,但如何撤消它? 最佳答案 GMSH 不是一个与其他工具非常相似的 CAD 工具。它提供了一个GUI;然而,并不是所有的操作都可以通过它完成,即使是那些可以完成
我维护a little Python package在用于网格表示的不同格式之间进行转换 à la 这些文件可能会变得很大,因此在使用 Python 读取它们时,高效地读取它们很重要。 最常用的格式之
我对 Python 非常陌生,如果这是一个愚蠢的问题,我很抱歉。无论如何,当我尝试运行我在 PyCharm (Python IDE) 上安装的包时,我收到此错误: Traceback (most re
我想创建一个空心或厚半圆环,为此,我使用以下命令: SetFactory ("OpenCASCADE"); Torus (1) = {0,0,0, 170,30, Pi}; Torus (2) = {
我正在尝试对由 3 种 Material 组成的复杂几何体进行网格划分,如下所示: 几何图形是从 *.stp 文件导入的。 我在地理文件中定义了几个物理表面和 3 个物理域: Physical Sur
我正在尝试编写一个脚本,该脚本将使用 Gmsh Python API 自动网格化几何形状以进行 CFD 分析。我遇到了一些问题: 首先,我希望能够编写 Gmsh 脚本文件 (.geo) 以便进行调试。
我是 fipy 的新手,所以如果我问一些应该显而易见的问题,请原谅我的无知。但是我无法运行一个已经存在的(并且在其他机器上工作)脚本,得到 EnvironmentError: Gmsh version
我已经生成了一个 gmsh 网格,并通过 Gmsh2D 导入到 FiPy 中。我想解决表面问题以设置边界条件,但我不知道如何操作。 在我在文档中发现的 FiPy 示例中,建议命名某些行以便稍后处理它们
我是一名优秀的程序员,十分优秀!