diff --git a/.travis.yml b/.travis.yml index 979e5f40..b75b6cc6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,7 @@ install: - source activate test - conda install --yes numpy scipy matplotlib pip nose - pip install setuptools + - pip install enum34 - pip install numpy-stl - pip install coveralls - pip install coverage diff --git a/docs/source/file_handler.rst b/docs/source/file_handler.rst index e58c96d2..c22b353c 100644 --- a/docs/source/file_handler.rst +++ b/docs/source/file_handler.rst @@ -15,4 +15,16 @@ File Handler .. autoclass:: StlHandler :members: :private-members: - :special-members: \ No newline at end of file + :special-members: + +.. autoclass:: OpenFoamHandler + :members: + :private-members: + :special-members: + +.. autofunction:: _check_extension + +.. autofunction:: _check_filename_type + +.. autofunction:: _check_infile_instantiation + diff --git a/pygem/file_handler.py b/pygem/file_handler.py index 81bec548..e69a0685 100644 --- a/pygem/file_handler.py +++ b/pygem/file_handler.py @@ -5,45 +5,24 @@ from mpl_toolkits import mplot3d from matplotlib import pyplot from stl import mesh -#import os class FileHandler(object): """ A base class for file handling. - :cvar string filename: name of the file to be processed - :cvar string extension: extension of the file to be processed + :cvar string infile: name of the input file to be processed + :cvar string outfile: name of the output file where to write in + :cvar string extension: extension of the input/output files. It is specific for each + subclass """ def __init__(self): - self.filename = None + self.infile = None + self.outfile = None self.extension = None - def _extract_extension(self): - """ - It is used in child classes. - - .. todo:: DOCS - """ - ext = self.filename.split('.')[-1].lower() - return ext - - - def _check_extension(self, extension): - """ - Internal function that checks that the member extension is equal to - a given extension. - It is used in child classes. - - :param string extension: file extension to check. - """ - if not self.extension == extension: - raise ValueError('The input file does not have the proper extension. \ - It should be %s.' % extension) - - - def parse(self): + def parse(self, infile): """ Abstract method to parse a specific file. @@ -70,20 +49,14 @@ class UnvHandler(FileHandler): .. todo:: DOCS """ - def __init__(self, filename): + def __init__(self): super(UnvHandler, self).__init__() - - if not isinstance(filename, basestring): - raise TypeError("filename must be a string") - - self.filename = filename - self.extension = self._extract_extension() - self._check_extension('unv') + self.extension = 'unv' - def parse(self): + def parse(self, filename): """ - Method to parse the `filename`. It returns a matrix with all the coordinates. + Method to parse the file `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 @@ -93,7 +66,12 @@ def parse(self): - specify when it works """ - input_file = open(self.filename, 'r') + _check_filename_type(filename) + _check_extension(filename, self.extension) + + self.infile = filename + + input_file = open(self.infile, 'r') nline = 0 while True: line = input_file.readline() @@ -120,7 +98,7 @@ def parse(self): nline = 0 i = 0 - with open(self.filename, 'r') as input_file: + with open(self.infile, 'r') as input_file: for line in input_file: nline += 1 if nline % 2 == 1 and nline > start_line and nline < last_line: @@ -134,25 +112,28 @@ def parse(self): return mesh_points - def write(self, mesh_points, outfile): + def write(self, mesh_points, filename): """ - Writes a unv file, called outfile, copying all the lines from self.filename but + Writes a unv file, called filename, copying all the lines from self.filename but the coordinates. mesh_points is a matrix that contains the new coordinates to write in the unv file. :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. + :param string filename: name of the output file. .. todo:: DOCS """ - if not isinstance(outfile, basestring): - raise TypeError("outfile must be a string") + _check_filename_type(filename) + _check_extension(filename, self.extension) + _check_infile_instantiation(self.infile) + + self.outfile = filename n_points = mesh_points.shape[0] nrow = 0 i = 0 - with open(self.filename, 'r') as input_file, open(outfile, 'w') as output_file: + with open(self.infile, 'r') as input_file, open(self.outfile, 'w') as output_file: for line in input_file: nrow += 1 if nrow % 2 == 1 and nrow > 20 and nrow <= (20 + n_points * 2): @@ -171,18 +152,12 @@ class StlHandler(FileHandler): .. todo:: DOCS """ - def __init__(self, filename): + def __init__(self): super(StlHandler, self).__init__() + self.extension = 'stl' - if not isinstance(filename, basestring): - raise TypeError("filename must be a string") - - self.filename = filename - self.extension = self._extract_extension() - self._check_extension('stl') - - def parse(self): + def parse(self, filename): """ Method to parse the `filename`. It returns a matrix with all the coordinates. @@ -194,27 +169,35 @@ def parse(self): - specify when it works """ - stl_mesh = mesh.Mesh.from_file(self.filename) + _check_filename_type(filename) + _check_extension(filename, self.extension) + + self.infile = filename + + stl_mesh = mesh.Mesh.from_file(self.infile) mesh_points = np.array([stl_mesh.x.ravel(), stl_mesh.y.ravel(), stl_mesh.z.ravel()]) mesh_points = mesh_points.T return mesh_points - def write(self, mesh_points, outfile): + def write(self, mesh_points, filename): """ - Writes a unv file, called outfile, copying all the lines from self.filename but + Writes a unv file, called filename, copying all the lines from self.filename but the coordinates. mesh_points is a matrix that contains the new coordinates to 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. - :param string outfile: name of the output file. + :param string filename: name of the output file. .. todo:: DOCS """ - if not isinstance(outfile, basestring): - raise TypeError("outfile must be a string") + _check_filename_type(filename) + _check_extension(filename, self.extension) + _check_infile_instantiation(self.infile) + + self.outfile = filename n_vertices = mesh_points.shape[0] # number of triplets of vertices @@ -226,20 +209,19 @@ def write(self, mesh_points, outfile): for j in range(0, 3): data['vectors'][i][j] = mesh_points[3*i + j] - stl_mesh.save(outfile, mode=1, update_normals=True) + stl_mesh.save(self.outfile, mode=1, update_normals=True) def plot(self, plot_file=None): """ - Method to plot an stl file. If `plot_file` is not given it plots `self.filename`. + Method to plot an stl file. If `plot_file` is not given it plots `self.infile`. :param string plot_file: the stl filename you want to plot. """ if plot_file is None: - plot_file = self.filename + plot_file = self.infile else: - if not isinstance(plot_file, basestring): - raise TypeError("plot_file must be a string") + _check_filename_type(plot_file) # Create a new plot figure = pyplot.figure() @@ -255,24 +237,21 @@ 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): + def __init__(self): super(OpenFoamHandler, self).__init__() + self.extension = '' - if not isinstance(filename, basestring): - raise TypeError("filename must be a string") - - self.filename = filename - - def parse(self): + def parse(self, filename): """ Method to parse the `filename`. It returns a matrix with all the coordinates. @@ -284,15 +263,19 @@ def parse(self): - specify when it works """ - + _check_filename_type(filename) + _check_extension(filename, self.extension) + + self.infile = filename + nrow = 0 i = 0 - with open(self.filename, 'r') as input_file: + with open(self.infile, '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)) + 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 @@ -305,32 +288,74 @@ def parse(self): - def write(self, mesh_points, outfile): + def write(self, mesh_points, filename): """ - Writes a openFOAM file, called outfile, copying all the lines from self.filename but + Writes a openFOAM file, called filename, 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. - + :param string filename: name of the output file. + .. todo:: DOCS """ - if not isinstance(outfile, basestring): - raise TypeError("outfile must be a string") + _check_filename_type(filename) + _check_extension(filename, self.extension) + _check_infile_instantiation(self.infile) + + self.outfile = filename n_points = mesh_points.shape[0] nrow = 0 i = 0 - with open(self.filename, 'r') as input_file, open(outfile, 'w') as output_file: + with open(self.infile, 'r') as input_file, open(self.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('(' + 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) - - + + + +def _check_extension(filename, extension): + """ + This private method checks if the given `filename` has the proper `extension`. + If not it raises a ValueError. + + :param string filename: file to check. + :param string extension: file extension to check. + """ + file_ext = filename.split('.')[-1].lower() + # to manage the case of open foam (no extension) the following check is needed + if file_ext == filename.lower(): + pass + elif not file_ext == extension: + raise ValueError('The input file does not have the proper extension. \ + It should be %s.' % extension) + + +def _check_filename_type(filename): + """ + This private method checks if `filename` is a string. If not it raises a TypeError. + + :param string filename: file to check. + """ + if not isinstance(filename, basestring): + raise TypeError('The given filename (%s) must be a string' % filename) + + +def _check_infile_instantiation(infile): + """ + This private method checks if the input file `infile` is instantiated. If not it means + that nobody called the parse method, i.e. `self.infile` is None. If the check fails + it raises a RuntimeError. + + :param string infile: file to check. + """ + if not infile: + raise RuntimeError("You can not write a file without having parsed one.") diff --git a/setup.py b/setup.py index 6271fabb..67456e57 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,8 @@ def readme(): 'numpy', 'numpy-stl', 'scipy', - 'matplotlib' + 'matplotlib', + 'enum34' ], test_suite='nose.collector', tests_require=['nose'], diff --git a/tests/test_file_handler.py b/tests/test_file_handler.py index 00597666..0611fd0c 100644 --- a/tests/test_file_handler.py +++ b/tests/test_file_handler.py @@ -10,9 +10,14 @@ class TestFileHandler(TestCase): - def test_base_class_filename(self): + def test_base_class_infile(self): file_handler = fh.FileHandler() - assert file_handler.filename == None + assert file_handler.infile == None + + + def test_base_class_outfile(self): + file_handler = fh.FileHandler() + assert file_handler.outfile == None def test_base_class_extension(self): @@ -23,7 +28,7 @@ def test_base_class_extension(self): def test_base_class_parse(self): file_handler = fh.FileHandler() with self.assertRaises(NotImplementedError): - file_handler.parse() + file_handler.parse('input') def test_base_class_write(self): @@ -34,76 +39,112 @@ def test_base_class_write(self): # UNV tests - def test_unv_filename_member(self): - unv_handler = fh.UnvHandler('tests/test_datasets/test_square.unv') - assert unv_handler.filename == 'tests/test_datasets/test_square.unv' + def test_unv_instantiation(self): + unv_handler = fh.UnvHandler() - def test_unv_extension_member(self): - unv_handler = fh.UnvHandler('tests/test_datasets/test_square.unv') - assert unv_handler.extension == 'unv' + def test_unv_default_infile_member(self): + unv_handler = fh.UnvHandler() + assert unv_handler.infile == None - def test_unv_failing_filename_type(self): - with self.assertRaises(TypeError): - unv_handler = fh.UnvHandler(3) + def test_unv_default_outfile_member(self): + unv_handler = fh.UnvHandler() + assert unv_handler.outfile == None - def test_unv_instantiation(self): - unv_handler = fh.UnvHandler('tests/test_datasets/test_square.unv') + def test_unv_default_extension_member(self): + unv_handler = fh.UnvHandler() + assert unv_handler.extension == 'unv' - def test_unv_failing_check_extension(self): + def test_unv_parse_failing_filename_type(self): + unv_handler = fh.UnvHandler() + with self.assertRaises(TypeError): + mesh_points = unv_handler.parse(5.2) + + + def test_unv_parse_failing_check_extension(self): + unv_handler = fh.UnvHandler() with self.assertRaises(ValueError): - unv_handler = fh.UnvHandler('tests/test_datasets/test_square.iges') + mesh_points = unv_handler.parse('tests/test_datasets/test_square.iges') + + + def test_unv_parse_infile(self): + unv_handler = fh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + assert unv_handler.infile == 'tests/test_datasets/test_square.unv' def test_unv_parse_shape(self): - unv_handler = fh.UnvHandler('tests/test_datasets/test_square.unv') - mesh_points = unv_handler.parse() + unv_handler = fh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') assert mesh_points.shape == (256, 3) def test_unv_parse_coords_1(self): - unv_handler = fh.UnvHandler('tests/test_datasets/test_square.unv') - mesh_points = unv_handler.parse() + unv_handler = fh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') np.testing.assert_almost_equal(mesh_points[33][0], 1.0) def test_unv_parse_coords_2(self): - unv_handler = fh.UnvHandler('tests/test_datasets/test_square.unv') - mesh_points = unv_handler.parse() + unv_handler = fh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') np.testing.assert_almost_equal(mesh_points[178][1], 0.4) def test_unv_parse_coords_3(self): - unv_handler = fh.UnvHandler('tests/test_datasets/test_square.unv') - mesh_points = unv_handler.parse() + unv_handler = fh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') np.testing.assert_almost_equal(mesh_points[100][2], 0.0) def test_unv_parse_coords_4(self): - unv_handler = fh.UnvHandler('tests/test_datasets/test_square.unv') - mesh_points = unv_handler.parse() + unv_handler = fh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') np.testing.assert_almost_equal(mesh_points[0][0], 0.0) def test_unv_parse_coords_5(self): - unv_handler = fh.UnvHandler('tests/test_datasets/test_square.unv') - mesh_points = unv_handler.parse() + unv_handler = fh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') np.testing.assert_almost_equal(mesh_points[-1][2], 0.0) - def test_unv_write_failing_outfile_type(self): - unv_handler = fh.UnvHandler('tests/test_datasets/test_square.unv') - mesh_points = unv_handler.parse() + def test_unv_write_failing_filename_type(self): + unv_handler = fh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') with self.assertRaises(TypeError): - unv_handler.write(mesh_points, 3) - + unv_handler.write(mesh_points, -2) + + + def test_unv_write_failing_check_extension(self): + unv_handler = fh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + with self.assertRaises(ValueError): + unv_handler.write(mesh_points, 'tests/test_datasets/test_square.iges') + + + def test_unv_write_failing_infile_instantiation(self): + unv_handler = fh.UnvHandler() + mesh_points = np.zeros((20, 3)) + with self.assertRaises(RuntimeError): + unv_handler.write(mesh_points, 'tests/test_datasets/test_square_out.unv') + def test_unv_write_outfile(self): - unv_handler = fh.UnvHandler('tests/test_datasets/test_square.unv') - mesh_points = unv_handler.parse() + unv_handler = fh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + outfilename = 'tests/test_datasets/test_square_out.unv' + unv_handler.write(mesh_points, outfilename) + assert unv_handler.outfile == outfilename + os.remove(outfilename) + + + def test_unv_write_comparison(self): + unv_handler = fh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') mesh_points[0][0] = 2.2 mesh_points[5][1] = 4.3 mesh_points[9][2] = 0.5 @@ -120,76 +161,112 @@ def test_unv_write_outfile(self): # STL tests - def test_stl_filename_member(self): - stl_handler = fh.StlHandler('tests/test_datasets/test_sphere.stl') - assert stl_handler.filename == 'tests/test_datasets/test_sphere.stl' + def test_stl_instantiation(self): + stl_handler = fh.StlHandler() - def test_stl_extension_member(self): - stl_handler = fh.StlHandler('tests/test_datasets/test_sphere.stl') - assert stl_handler.extension == 'stl' + def test_stl_default_infile_member(self): + stl_handler = fh.StlHandler() + assert stl_handler.infile == None - def test_stl_failing_filename_type(self): - with self.assertRaises(TypeError): - stl_handler = fh.StlHandler(3) + def test_stl_default_outfile_member(self): + stl_handler = fh.StlHandler() + assert stl_handler.outfile == None - def test_stl_instantiation(self): - stl_handler = fh.StlHandler('tests/test_datasets/test_sphere.stl') + def test_stl_default_extension_member(self): + stl_handler = fh.StlHandler() + assert stl_handler.extension == 'stl' - def test_stl_failing_check_extension(self): + def test_stl_parse_failing_filename_type(self): + stl_handler = fh.StlHandler() + with self.assertRaises(TypeError): + mesh_points = stl_handler.parse(5.2) + + + def test_stl_parse_failing_check_extension(self): + stl_handler = fh.StlHandler() with self.assertRaises(ValueError): - stl_handler = fh.StlHandler('tests/test_datasets/test_square.iges') + mesh_points = stl_handler.parse('tests/test_datasets/test_square.iges') + + + def test_stl_parse_infile(self): + stl_handler = fh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + assert stl_handler.infile == 'tests/test_datasets/test_sphere.stl' def test_stl_parse_shape(self): - stl_handler = fh.StlHandler('tests/test_datasets/test_sphere.stl') - mesh_points = stl_handler.parse() + stl_handler = fh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') assert mesh_points.shape == (7200, 3) def test_stl_parse_coords_1(self): - stl_handler = fh.StlHandler('tests/test_datasets/test_sphere.stl') - mesh_points = stl_handler.parse() + stl_handler = fh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') np.testing.assert_almost_equal(mesh_points[33][0], -21.31975937) def test_stl_parse_coords_2(self): - stl_handler = fh.StlHandler('tests/test_datasets/test_sphere.stl') - mesh_points = stl_handler.parse() + stl_handler = fh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') np.testing.assert_almost_equal(mesh_points[1708][1], 2.58431911) def test_stl_parse_coords_3(self): - stl_handler = fh.StlHandler('tests/test_datasets/test_sphere.stl') - mesh_points = stl_handler.parse() + stl_handler = fh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') np.testing.assert_almost_equal(mesh_points[3527][2], -2.47207999) def test_stl_parse_coords_4(self): - stl_handler = fh.StlHandler('tests/test_datasets/test_sphere.stl') - mesh_points = stl_handler.parse() + stl_handler = fh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') np.testing.assert_almost_equal(mesh_points[0][0], -21.31975937) def test_stl_parse_coords_5(self): - stl_handler = fh.StlHandler('tests/test_datasets/test_sphere.stl') - mesh_points = stl_handler.parse() + stl_handler = fh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') np.testing.assert_almost_equal(mesh_points[-1][2], -39.05963898) - def test_stl_write_failing_outfile_type(self): - stl_handler = fh.StlHandler('tests/test_datasets/test_sphere.stl') - mesh_points = stl_handler.parse() + def test_stl_write_failing_filename_type(self): + stl_handler = fh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') with self.assertRaises(TypeError): - stl_handler.write(mesh_points, 3) - + stl_handler.write(mesh_points, 4.) + + + def test_stl_write_failing_check_extension(self): + stl_handler = fh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + with self.assertRaises(ValueError): + stl_handler.write(mesh_points, 'tests/test_datasets/test_square.iges') + + + def test_stl_write_failing_infile_instantiation(self): + stl_handler = fh.StlHandler() + mesh_points = np.zeros((40, 3)) + with self.assertRaises(RuntimeError): + stl_handler.write(mesh_points, 'tests/test_datasets/test_sphere_out.stl') + def test_stl_write_outfile(self): - stl_handler = fh.StlHandler('tests/test_datasets/test_sphere.stl') - mesh_points = stl_handler.parse() + stl_handler = fh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + outfilename = 'tests/test_datasets/test_sphere_out.stl' + stl_handler.write(mesh_points, outfilename) + assert stl_handler.outfile == outfilename + os.remove(outfilename) + + + def test_stl_write_comparison(self): + stl_handler = fh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') mesh_points[0] = [-40.2, -20.5, 60.9] mesh_points[1] = [-40.2, -10.5, 60.9] mesh_points[2] = [-40.2, -10.5, 60.9] @@ -209,72 +286,118 @@ def test_stl_write_outfile(self): def test_stl_plot_failing_outfile_type(self): - stl_handler = fh.StlHandler('tests/test_datasets/test_sphere.stl') + stl_handler = fh.StlHandler() with self.assertRaises(TypeError): stl_handler.plot(plot_file=3) # openFOAM tests - def test_open_foam_filename_member(self): - open_foam_handler = fh.OpenFoamHandler('tests/test_datasets/test_openFOAM') - assert open_foam_handler.filename == 'tests/test_datasets/test_openFOAM' + def test_open_foam_instantiation(self): + open_foam_handler = fh.OpenFoamHandler() + + def test_open_foam_default_infile_member(self): + open_foam_handler = fh.OpenFoamHandler() + assert open_foam_handler.infile == None - def test_open_foam_failing_filename_type(self): + + def test_open_foam_default_outfile_member(self): + open_foam_handler = fh.OpenFoamHandler() + assert open_foam_handler.outfile == None + + + def test_open_foam_default_extension_member(self): + open_foam_handler = fh.OpenFoamHandler() + assert open_foam_handler.extension == '' + + + def test_open_foam_parse_failing_filename_type(self): + open_foam_handler = fh.OpenFoamHandler() with self.assertRaises(TypeError): - open_foam_handler = fh.OpenFoamHandler(3) + mesh_points = open_foam_handler.parse(.2) + + def test_open_foam_parse_failing_check_extension(self): + open_foam_handler = fh.OpenFoamHandler() + with self.assertRaises(ValueError): + mesh_points = open_foam_handler.parse('tests/test_datasets/test_square.iges') - def test_open_foam_instantiation(self): - open_foam_handler = fh.OpenFoamHandler('tests/test_datasets/test_openFOAM') + + def test_open_foam_parse_infile(self): + open_foam_handler = fh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + assert open_foam_handler.infile == 'tests/test_datasets/test_openFOAM' def test_open_foam_parse_shape(self): - open_foam_handler = fh.OpenFoamHandler('tests/test_datasets/test_openFOAM') - mesh_points = open_foam_handler.parse() + open_foam_handler = fh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') assert mesh_points.shape == (21812, 3) def test_open_foam_parse_coords_1(self): - open_foam_handler = fh.OpenFoamHandler('tests/test_datasets/test_openFOAM') - mesh_points = open_foam_handler.parse() + open_foam_handler = fh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') np.testing.assert_almost_equal(mesh_points[33][0], 1.42254) def test_open_foam_parse_coords_2(self): - open_foam_handler = fh.OpenFoamHandler('tests/test_datasets/test_openFOAM') - mesh_points = open_foam_handler.parse() + open_foam_handler = fh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') np.testing.assert_almost_equal(mesh_points[1708][1], -3.13059) def test_open_foam_parse_coords_3(self): - open_foam_handler = fh.OpenFoamHandler('tests/test_datasets/test_openFOAM') - mesh_points = open_foam_handler.parse() + open_foam_handler = fh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') np.testing.assert_almost_equal(mesh_points[3527][2], .0) def test_open_foam_parse_coords_4(self): - open_foam_handler = fh.OpenFoamHandler('tests/test_datasets/test_openFOAM') - mesh_points = open_foam_handler.parse() + open_foam_handler = fh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') np.testing.assert_almost_equal(mesh_points[0][0], -17.5492) def test_open_foam_parse_coords_5(self): - open_foam_handler = fh.OpenFoamHandler('tests/test_datasets/test_openFOAM') - mesh_points = open_foam_handler.parse() + open_foam_handler = fh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') np.testing.assert_almost_equal(mesh_points[-1][2], 0.05) - def test_open_foam_write_failing_outfile_type(self): - open_foam_handler = fh.OpenFoamHandler('tests/test_datasets/test_openFOAM') - mesh_points = open_foam_handler.parse() + def test_open_foam_write_failing_filename_type(self): + open_foam_handler = fh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') with self.assertRaises(TypeError): - open_foam_handler.write(mesh_points, 3) - + open_foam_handler.write(mesh_points, -1.) + + + def test_open_foam_write_failing_check_extension(self): + open_foam_handler = fh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + with self.assertRaises(ValueError): + open_foam_handler.write(mesh_points, 'tests/test_datasets/test_square.iges') + + + def test_open_foam_write_failing_infile_instantiation(self): + open_foam_handler = fh.OpenFoamHandler() + mesh_points = np.zeros((40, 3)) + with self.assertRaises(RuntimeError): + open_foam_handler.write(mesh_points, 'tests/test_datasets/test_openFOAM_out') + def test_open_foam_write_outfile(self): - open_foam_handler = fh.OpenFoamHandler('tests/test_datasets/test_openFOAM') - mesh_points = open_foam_handler.parse() + open_foam_handler = fh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + outfilename = 'tests/test_datasets/test_openFOAM_out' + open_foam_handler.write(mesh_points, outfilename) + assert open_foam_handler.outfile == outfilename + os.remove(outfilename) + + + def test_open_foam_write_comparison(self): + open_foam_handler = fh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') mesh_points[0] = [-14., 1.55, 0.2] mesh_points[1] = [-14.3, 2.55, 0.3] mesh_points[2] = [-14.3, 2.55, 0.3] diff --git a/tests/test_ffd.py b/tests/test_free_form.py similarity index 100% rename from tests/test_ffd.py rename to tests/test_free_form.py