How to implement fast file writing from different arrays in Python?

Viewed 33

I want to write a file for input to a CFD program that describes the elements of a geometry created in a meshing program. After reading the mesh I have an array with node coordinates node and a separate file that connects the nodes with elements tetra.

Their structure looks like this:

>>> node[0:4]
array([[0.0000, 0.0000, 0.0000],
       [1.0000, 0.0000, 0.0000],
       [0.0000, 1.0000, 0.0000],
       [0.0000, 0.0000, 1.0000]])

>>> tetra[0:3]
array([[0, 1, 2, 3],
       [1, 3, 4, 2],
       [4, 5, 6, 7]])

This is only an example. The nodal indices are not in order so I need to use the indices information stored in the tetra array. The elements can also contain other number of nodal points for another type of element. There could be also 5-6 nodes but this is stored in another array.

What I want to do is to create a file that contains the nodal coordinates for all elements and each element has a header 1 0 1 in the following way:

1 0 1
0.0000 0.0000 0.0000
1.0000 0.0000 0.0000
0.0000 1.0000 0.0000
0.0000 0.0000 1.0000
1 0 1
1.0000 0.0000 0.0000
0.0000 0.0000 1.0000
...

I have a lot of elements and so far I did it iterating through the tetra array and based on the indices stored in there I retrieved the coordinates, but that takes a lot of time.

with open('geometry.in', 'w') as geo:
    for i in range(len(tet):
        geo.write('1 0 1')
        np.savetxt(geo, node[tetra[i, 0:4]])

Is there a smarter/better/faster solution for this?

0 Answers
Related