INP和VTK 文件

其实当我们了解了一些基本的VTK 文件格式,例如如何编写含有unstructured grid信息的.vtk文件,之后,我们就可以自己根据构思来去设计一些体素模型。然而,对于复杂模型来说,或者含有大量几何信息的模型来说,凭脑子来一点点的设计和堆积一个个CELL到我们的文件数据集中,肯定是不可能的。毕竟人脑能够构思框架和使用,但是机脑才是那个负责重复劳动的东西。

对于熟悉ABAQUS有限元的人来说,INP文件是最常接触的文件,它里面涵盖了有限元分析所需求的各种模型信息以及边界条件。ABAQUS的后处理功能虽然能够提供我们必要的一些信息,但是如果我们想了解有限元分析结果的数据结构,判断其结果准确性等等,我们就需要一些更强大的图形展示窗口,比如PARAVIEW。而且,我们还能更方便的基于从ABAQUS 中获得的结果文件,做进一步的分析,比如,Multiscale modelling 多尺度分析。但是在这里就不深入讲解了。

综上,INP文件转化为VTK文件,然后显示到PARAVIEW,或者我们自己编写的基于VTK 的图形窗口中,就显得很必要了。

INP 文件格式

VTK 文件格式在之前的文章中已经介绍了,但是还没有介绍过INP文件格式,所以在这里简单说一下吧。

INP2VTK

当前我只测试了C3D8R的文件格式。因为它所含有的网格节点排列顺序和 构建VTK中的UNSTRUCTURED GRID的顺序是一样的。在VTK 文件中的CELL TYPE是选择了 12 (VTK_HEXAHEDRON)。因为它能够体现不同的网格形态(更能适应不同的ASPECT RATIO)。

代码

首先我们需要分别读取 INP 和写入 VTK 文件,所以这里定义了两个 FUNCTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def read_inp(self, inp_path, result_path):
lnum_node = 0 # Node number start
lnum_element = 0 # Node number end
lnum_element_end = 0 # Element number end
node_dic = {} # Node dictionary: node number and coordinate
ele_dic = {} # Node dictionary: node number and coordinate

inp_file = open(inp_path)
inp_lines = inp_file.readlines()

if result_path:
result_file = open(result_path)
result_lines = result_file.readlines()

# Node information
for i in range(len(inp_lines)):
if inp_lines[i] == '*Node\n':
lnum_node = i + 1
print('Current line number of *Node: ', lnum_node)
if inp_lines[i] == '*Element, type=C3D8R\n':
lnum_element = i + 1
print('Current line number of *Element: ', lnum_element)
if inp_lines[i] == '*End Part\n':
lnum_element_end = i + 1
print('Current line number of *End Part: ', lnum_element_end)

# Element information
for i in range(lnum_node, lnum_element-1):
current_line = inp_lines[i]
current_line = current_line.replace(' ', '').replace('\n', '').split(',')
current_coord = []
for j in range(len(current_line)):
current_coord.append(float(current_line[j]))
node_dic[current_line[0]] = current_coord[1:]

for i in range(lnum_element, lnum_element_end-1):
current_line = inp_lines[i]
current_line = current_line.replace(' ', '').replace('\n', '').split(',')
current_node_group = []
for j in range(len(current_line)):
current_node_group.append(int(current_line[j]))
ele_dic[i - lnum_element + 1] = current_node_group[1:]

return node_dic, ele_dic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def save_vtk(self, vtk_path, node_dic, element_dic):
vtk_file = open(vtk_path, 'a')
vtk_file.write('# vtk DataFile Version 2.0\n')
vtk_file.write('Unstructured Grid - ' + vtk_path.strip('.vtk') + '\n')
vtk_file.write('ASCII\n')
vtk_file.write('\n')
vtk_file.write('DATASET UNSTRUCTURED_GRID\n')
vtk_file.write('POINTS ' + str(len(node_dic)) + ' float\n')
for i in range(len(node_dic)):
current_value = node_dic[str(i + 1)]
# print(current_value)
# print(type(current_value))
for j in range(len(current_value)):
vtk_file.write(str(current_value[j])+' ')
if j == len(current_value) - 1:
vtk_file.write('\n')

vtk_file.write('\n')
vtk_file.write('CELLS ' + str(len(element_dic)) + ' ' + str(9*len(element_dic)) + '\n')
for i in range(len(element_dic)):
current_value = element_dic[i + 1]
print(current_value)
print(type(current_value))
vtk_file.write('8 ')
for j in range(len(current_value)):
vtk_file.write(str(current_value[j]-1)+' ')
if j == len(current_value) - 1:
vtk_file.write('\n')

vtk_file.write('\n')
vtk_file.write('CELL_TYPES ' + str(len(element_dic)) + '\n')
for i in range(len(element_dic)):
vtk_file.write('12' + '\n')