Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion pygem/file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from mpl_toolkits import mplot3d
from matplotlib import pyplot
from stl import mesh
#import os


class FileHandler(object):
Expand Down Expand Up @@ -141,6 +142,7 @@ def write(self, mesh_points, outfile):

:param ndarray mesh_points: it is a `n_points`-by-3 matrix containing
the coordinates of the points of the mesh
:param string outfile: name of the output file.

.. todo:: DOCS
"""
Expand Down Expand Up @@ -206,7 +208,8 @@ def write(self, mesh_points, outfile):
write in the unv file.

:param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing
the coordinates of the points of the mesh
the coordinates of the points of the mesh.
:param string outfile: name of the output file.

.. todo:: DOCS
"""
Expand Down Expand Up @@ -252,3 +255,82 @@ def plot(self, plot_file=None):

# Show the plot to the screen
pyplot.show()


class OpenFoamHandler(FileHandler):
"""
OpenFOAM mesh file handler class.

.. todo:: DOCS
"""
def __init__(self, filename):
super(OpenFoamHandler, self).__init__()

if not isinstance(filename, basestring):
raise TypeError("filename must be a string")

self.filename = filename


def parse(self):
"""
Method to parse the `filename`. It returns a matrix with all the coordinates.

:return: mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of
the points of the mesh
:rtype: numpy.ndarray

.. todo::

- specify when it works
"""

nrow = 0
i = 0
with open(self.filename, 'r') as input_file:
for line in input_file:
nrow += 1
if nrow == 19:
n_points = int(line)
mesh_points = np.zeros(shape=(n_points,3))
if nrow > 20 and nrow < 21 + n_points:
line = line[line.index("(") + 1:line.rindex(")")]
j = 0
for number in line.split():
mesh_points[i][j] = float(number)
j += 1
i += 1

return mesh_points



def write(self, mesh_points, outfile):
"""
Writes a openFOAM file, called outfile, copying all the lines from self.filename but
the coordinates. mesh_points is a matrix that contains the new coordinates to
write in the openFOAM file.

:param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing
the coordinates of the points of the mesh.
:param string outfile: name of the output file.

.. todo:: DOCS
"""
if not isinstance(outfile, basestring):
raise TypeError("outfile must be a string")

n_points = mesh_points.shape[0]
nrow = 0
i = 0
with open(self.filename, 'r') as input_file, open(outfile, 'w') as output_file:
for line in input_file:
nrow += 1
if nrow > 20 and nrow < 21 + n_points:
output_file.write('(' + str(mesh_points[i][0]) + ' ' + str(mesh_points[i][1]) + ' ' + str(mesh_points[i][2]) +')')
output_file.write('\n')
i += 1
else:
output_file.write(line)


76 changes: 76 additions & 0 deletions tests/test_datasets/parameters_test_openFOAM.prm
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

n_control_points_x: 2
n_control_points_y: 2
n_control_points_z: 1

lenght_box_x: 60
lenght_box_y: 9.0
lenght_box_z: 0.7

origin_box_x: -30.0
origin_box_y: -4.0
origin_box_z: -0.1

rot_angle_x: .0
rot_angle_y: .0
rot_angle_z: .0

mu_x:
0 0 0 0.0
0 0 1 0.0
0 1 0 0.0
0 1 1 0.0
0 2 0 0.0
0 2 1 0.0
1 0 0 0.0
1 0 1 0.0
1 1 0 0.0
1 1 1 0.0
1 2 0 0.0
1 2 1 0.0
2 0 0 0.0
2 0 1 0.0
2 1 0 0.0
2 1 1 0.0
2 2 0 0.0
2 2 1 0.0

mu_y:
0 0 0 0.0
0 0 1 0.0
0 1 0 0.0
0 1 1 0.0
0 2 0 0.0
0 2 1 0.0
1 0 0 0.0
1 0 1 0.0
1 1 0 0.5
1 1 1 0.5
1 2 0 0.0
1 2 1 0.0
2 0 0 0.0
2 0 1 0.0
2 1 0 0.0
2 1 1 0.0
2 2 0 0.0
2 2 1 0.0

mu_z:
0 0 0 0.0
0 0 1 0.0
0 1 0 0.0
0 1 1 0.0
0 2 0 0.0
0 2 1 0.0
1 0 0 0.0
1 0 1 0.0
1 1 0 0.0
1 1 1 0.0
1 2 0 0.0
1 2 1 0.0
2 0 0 0.0
2 0 1 0.0
2 1 0 0.0
2 1 1 0.0
2 2 0 0.0
2 2 1 0.0
Loading