使用numpy在Python中导入nastran节点卡片组 [英] Import nastran nodes deck in Python using numpy

查看:188
本文介绍了使用numpy在Python中导入nastran节点卡片组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想将Nastran Deck导入Python数组中,请使用numpy一次完成.我该怎么办?我要去哪里错了?

If I want to import a Nastran Deck into a Python Array, in one fell swoop using numpy. How can I go about it? Where am I going wrong?

我有一个只有网格的文件.网格都与Nastran的短翻译器(8个字符)等距分布

I have a file with only Grids in it. Grids are all equally spaced with Nastran's Short translator (8 characters)

$ MH Nodes
$2345678$2345678$2345678$2345678$2345678$2345678
GRID     25601          58.50002-57.749923.05                      
GRID     25602          58.81002-57.749923.05  

如果我正确理解的话,使用Numpy的dtype命令

很好.这是我的代码:

using the dtype command from Numpy, is great if I understand it correctly. Here is my code:

fileMH = "Gnodes.bdf"

dtyp = np.dtype([
                ("Grid",(np.void,8)),
                ("GN",(np.int,8)),
                ("Prop",(np.void,8)),
                ("X",(np.float,8)),
                ("Y",(np.float,8)),
                ("Z",(np.float,8)),
])

f = np.loadtxt(fileMH,dtyp,comments="$")

我得到的错误是一个浮点错误,但是我期望dtype一次从字符串中提取8个字符.这是错误:

The error I get is a float error, however I was expecting dtype to extract 8 characters at a time out of the string. Here is the error:

ValueError: invalid literal for float(): 58.50002-57.749923.05

我们非常感谢您的帮助.

Help is appreciated.

P.S.可以请一些nastran标签做吗,在nastran中完成大量数据处理,这需要智能编程.这将是有帮助的.

P.S. Can some please make a nastran tag, huge amounts of data crunching is done in nastran, that requires smart programing. It would be helpful.

推荐答案

dtype指定二进制数据在内存中的布局.它没有定义文件中文本的格式.

The dtype specifies the layout of the binary data in memory. It doesn't define the format of text in a file.

您的数据文件具有固定宽度的字段.即,每个字段使用八个字符,并且没有特殊的分隔符来分隔字段.您可以使用 numpy.genfromtxt 读取此类文件,通过在delimiter参数中指定字段宽度.

Your data file has fixed-width fields. That is, each field uses eight characters, and there is no special delimiter character to separate the fields. You can read such a file using numpy.genfromtxt, by specifying the field widths in the delimiter argument.

这是ipython会话中的一个示例.首先,这是您的示例文件. (我编辑了文件以确保第二行和第三行中最后一个05后面有三个空格,但这不是必需的.)

Here's an example in an ipython session. First, here's your sample file. (I edited the file to ensure that there are three spaces after the final 05 in the second and third lines, but this wasn't necessary.)

In [15]: !cat nastran_data.txt
$ MH Nodes
$2345678$2345678$2345678$2345678$2345678$2345678
GRID     25601          58.50002-57.749923.05   
GRID     25602          58.81002-57.749923.05   

这是genfromtxt创建的数组的数据类型.请注意,GN字段的类型为np.int64,而XYZ字段的类型为np.float64.

Here's the data type of the array to be created by genfromtxt. Note that the GN field has type np.int64, and the X, Y and Z fields have type np.float64.

In [16]: dt = np.dtype([('Grid', 'S8'), ('GN', np.int64), ('Prop', 'S8'), ('X', np.float64), ('Y', np.float64), ('Z', np.float64)])

使用genfromtxt读取文件,该文件具有六个固定宽度的字段,每个字段的长度为8:

Read the file using genfromtxt, with six fixed-width fields, each of length 8:

In [18]: a = np.genfromtxt('nastran_data.txt', dtype=dt, delimiter=(8, 8, 8, 8, 8, 8), skip_header=2)

In [19]: a
Out[19]: 
array([('GRID    ', 25601, '        ', 58.50002, -57.7499, 23.05),
       ('GRID    ', 25602, '        ', 58.81002, -57.7499, 23.05)], 
      dtype=[('Grid', 'S8'), ('GN', '<i8'), ('Prop', 'S8'), ('X', '<f8'), ('Y', '<f8'), ('Z', '<f8')])

In [20]: a['GN']
Out[20]: array([25601, 25602])

In [21]: a['X']
Out[21]: array([ 58.50002,  58.81002])

这篇关于使用numpy在Python中导入nastran节点卡片组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆