diff --git a/smithers/io/openfoam/mesh_parser.py b/smithers/io/openfoam/mesh_parser.py index 4dc8ad6..2d91a07 100755 --- a/smithers/io/openfoam/mesh_parser.py +++ b/smithers/io/openfoam/mesh_parser.py @@ -198,19 +198,18 @@ def parse_line(line): except ValueError: pass - string_coords = content[start_idx:start_idx+num_points] + if not is_binary: + string_coords = content[start_idx:start_idx+num_points] - data = np.array(list(map(parse_line, string_coords)), dtype=float) - return data - - #TODO binary - # if is_binary: - # buf = b''.join(content[n+1:]) - # disp = struct.calcsize('c') - # vv = np.array(struct.unpack('{}d'.format(num*3), - # buf[disp:num*3*struct.calcsize('d') + disp])) - # data = vv.reshape((num, 3)) + data = np.array(list(map(parse_line, string_coords)), dtype=float) + else: + buf = b''.join(content[start_idx-1:]) + disp = struct.calcsize('c') + vv = np.array(struct.unpack('{}d'.format(num_points*3), + buf[disp:num_points*3*struct.calcsize('d') + disp])) + data = vv.reshape((num_points, 3)) + return data @classmethod def parse_owner_neighbour_content(cls, content, is_binary, skip=10): diff --git a/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/boundary b/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/boundary new file mode 100644 index 0000000..7ecdd09 --- /dev/null +++ b/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/boundary @@ -0,0 +1,71 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: 2406 | +| \\ / A nd | Website: www.openfoam.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + arch "LSB;label=32;scalar=64"; + class polyBoundaryMesh; + location "constant/polyMesh"; + object boundary; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + + +6 +( + inlet + { + type patch; + nFaces 100; + startFace 143280; + } + + outlet + { + type patch; + nFaces 100; + startFace 143380; + } + + top + { + type wall; + inGroups List 1(wall); + nFaces 420; + startFace 143480; + } + + bottom + { + type wall; + inGroups List 1(wall); + nFaces 420; + startFace 143900; + } + + obstacle + { + type wall; + inGroups List 1(wall); + nFaces 400; + startFace 144320; + } + + frontAndBack + { + type empty; + inGroups List 1(empty); + nFaces 144000; + startFace 144720; + } + +) + + +// ************************************************************************* // diff --git a/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/faces b/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/faces new file mode 100644 index 0000000..a605693 Binary files /dev/null and b/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/faces differ diff --git a/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/neighbour b/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/neighbour new file mode 100644 index 0000000..8aa3732 Binary files /dev/null and b/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/neighbour differ diff --git a/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/owner b/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/owner new file mode 100644 index 0000000..7e38077 Binary files /dev/null and b/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/owner differ diff --git a/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/points b/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/points new file mode 100644 index 0000000..b6e807e Binary files /dev/null and b/tests/test_datasets/notime_openfoam_mesh_binary/constant/polyMesh/points differ diff --git a/tests/test_datasets/notime_openfoam_mesh_binary/constant/transportProperties b/tests/test_datasets/notime_openfoam_mesh_binary/constant/transportProperties new file mode 100644 index 0000000..e153d3f --- /dev/null +++ b/tests/test_datasets/notime_openfoam_mesh_binary/constant/transportProperties @@ -0,0 +1,21 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | foam-extend: Open Source CFD | +| \\ / O peration | Version: 4.0 | +| \\ / A nd | Web: http://www.foam-extend.org | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + location "constant"; + object transportProperties; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +transportModel Newtonian; + +nu nu [0 2 -1 0 0 0 0] 1.5e-5; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/tests/test_datasets/notime_openfoam_mesh_binary/constant/turbulenceProperties b/tests/test_datasets/notime_openfoam_mesh_binary/constant/turbulenceProperties new file mode 100644 index 0000000..24b57e7 --- /dev/null +++ b/tests/test_datasets/notime_openfoam_mesh_binary/constant/turbulenceProperties @@ -0,0 +1,19 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | foam-extend: Open Source CFD | +| \\ / O peration | Version: 4.0 | +| \\ / A nd | Web: http://www.foam-extend.org | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + location "constant"; + object RASProperties; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +simulationType laminar; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/tests/test_datasets/openfoam_mesh_binary/0/U b/tests/test_datasets/openfoam_mesh_binary/0/U new file mode 100644 index 0000000..b62548c --- /dev/null +++ b/tests/test_datasets/openfoam_mesh_binary/0/U @@ -0,0 +1,56 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: 2406 | +| \\ / A nd | Website: www.openfoam.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format binary; + arch "LSB;label=32;scalar=64"; + class volVectorField; + location "0"; + object U; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [ 0 1 -1 0 0 0 0 ]; + +internalField uniform ( 0 0 0 ); + +boundaryField +{ + inlet + { + type fixedValue; + value uniform ( 0.0001 0 0 ); + } + outlet + { + type zeroGradient; + } + top + { + type fixedValue; + value uniform ( 0 0 0 ); + } + bottom + { + type fixedValue; + value uniform ( 0 0 0 ); + } + obstacle + { + type fixedValue; + value uniform ( 0 0 0 ); + } + frontAndBack + { + type empty; + } +} + + +// ************************************************************************* // diff --git a/tests/test_datasets/openfoam_mesh_binary/0/p b/tests/test_datasets/openfoam_mesh_binary/0/p new file mode 100644 index 0000000..db118b1 --- /dev/null +++ b/tests/test_datasets/openfoam_mesh_binary/0/p @@ -0,0 +1,53 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: 2406 | +| \\ / A nd | Website: www.openfoam.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format binary; + arch "LSB;label=32;scalar=64"; + class volScalarField; + location "0"; + object p; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [ 0 2 -2 0 0 0 0 ]; + +internalField uniform 0; + +boundaryField +{ + inlet + { + type zeroGradient; + } + outlet + { + type fixedValue; + value uniform 0; + } + top + { + type zeroGradient; + } + bottom + { + type zeroGradient; + } + obstacle + { + type zeroGradient; + } + frontAndBack + { + type empty; + } +} + + +// ************************************************************************* // diff --git a/tests/test_datasets/openfoam_mesh_binary/1088/U b/tests/test_datasets/openfoam_mesh_binary/1088/U new file mode 100644 index 0000000..bd4aa12 Binary files /dev/null and b/tests/test_datasets/openfoam_mesh_binary/1088/U differ diff --git a/tests/test_datasets/openfoam_mesh_binary/1088/p b/tests/test_datasets/openfoam_mesh_binary/1088/p new file mode 100644 index 0000000..a7a2d6d Binary files /dev/null and b/tests/test_datasets/openfoam_mesh_binary/1088/p differ diff --git a/tests/test_datasets/openfoam_mesh_binary/1088/phi b/tests/test_datasets/openfoam_mesh_binary/1088/phi new file mode 100644 index 0000000..b6deba4 Binary files /dev/null and b/tests/test_datasets/openfoam_mesh_binary/1088/phi differ diff --git a/tests/test_datasets/openfoam_mesh_binary/1088/uniform/time b/tests/test_datasets/openfoam_mesh_binary/1088/uniform/time new file mode 100644 index 0000000..6e80ec8 --- /dev/null +++ b/tests/test_datasets/openfoam_mesh_binary/1088/uniform/time @@ -0,0 +1,29 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 8 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + location "1088/uniform"; + object time; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +value 1088.00000000011028; + +name "1088"; + +index 5440; + +deltaT 0.2; + +deltaT0 0.2; + + +// ************************************************************************* // diff --git a/tests/test_datasets/openfoam_mesh_binary/4196/U b/tests/test_datasets/openfoam_mesh_binary/4196/U new file mode 100644 index 0000000..cb98674 Binary files /dev/null and b/tests/test_datasets/openfoam_mesh_binary/4196/U differ diff --git a/tests/test_datasets/openfoam_mesh_binary/4196/p b/tests/test_datasets/openfoam_mesh_binary/4196/p new file mode 100644 index 0000000..4d92851 Binary files /dev/null and b/tests/test_datasets/openfoam_mesh_binary/4196/p differ diff --git a/tests/test_datasets/openfoam_mesh_binary/4196/phi b/tests/test_datasets/openfoam_mesh_binary/4196/phi new file mode 100644 index 0000000..9ef90ee Binary files /dev/null and b/tests/test_datasets/openfoam_mesh_binary/4196/phi differ diff --git a/tests/test_datasets/openfoam_mesh_binary/4196/uniform/time b/tests/test_datasets/openfoam_mesh_binary/4196/uniform/time new file mode 100644 index 0000000..e6a04c4 --- /dev/null +++ b/tests/test_datasets/openfoam_mesh_binary/4196/uniform/time @@ -0,0 +1,29 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 8 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + location "4196/uniform"; + object time; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +value 4195.99999999837473; + +name "4196"; + +index 20980; + +deltaT 0.2; + +deltaT0 0.2; + + +// ************************************************************************* // diff --git a/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/boundary b/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/boundary new file mode 100644 index 0000000..7ecdd09 --- /dev/null +++ b/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/boundary @@ -0,0 +1,71 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: 2406 | +| \\ / A nd | Website: www.openfoam.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + arch "LSB;label=32;scalar=64"; + class polyBoundaryMesh; + location "constant/polyMesh"; + object boundary; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + + +6 +( + inlet + { + type patch; + nFaces 100; + startFace 143280; + } + + outlet + { + type patch; + nFaces 100; + startFace 143380; + } + + top + { + type wall; + inGroups List 1(wall); + nFaces 420; + startFace 143480; + } + + bottom + { + type wall; + inGroups List 1(wall); + nFaces 420; + startFace 143900; + } + + obstacle + { + type wall; + inGroups List 1(wall); + nFaces 400; + startFace 144320; + } + + frontAndBack + { + type empty; + inGroups List 1(empty); + nFaces 144000; + startFace 144720; + } + +) + + +// ************************************************************************* // diff --git a/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/faces b/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/faces new file mode 100644 index 0000000..a605693 Binary files /dev/null and b/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/faces differ diff --git a/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/neighbour b/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/neighbour new file mode 100644 index 0000000..8aa3732 Binary files /dev/null and b/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/neighbour differ diff --git a/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/owner b/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/owner new file mode 100644 index 0000000..7e38077 Binary files /dev/null and b/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/owner differ diff --git a/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/points b/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/points new file mode 100644 index 0000000..b6e807e Binary files /dev/null and b/tests/test_datasets/openfoam_mesh_binary/constant/polyMesh/points differ diff --git a/tests/test_datasets/openfoam_mesh_binary/constant/transportProperties b/tests/test_datasets/openfoam_mesh_binary/constant/transportProperties new file mode 100644 index 0000000..e153d3f --- /dev/null +++ b/tests/test_datasets/openfoam_mesh_binary/constant/transportProperties @@ -0,0 +1,21 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | foam-extend: Open Source CFD | +| \\ / O peration | Version: 4.0 | +| \\ / A nd | Web: http://www.foam-extend.org | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + location "constant"; + object transportProperties; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +transportModel Newtonian; + +nu nu [0 2 -1 0 0 0 0] 1.5e-5; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/tests/test_datasets/openfoam_mesh_binary/constant/turbulenceProperties b/tests/test_datasets/openfoam_mesh_binary/constant/turbulenceProperties new file mode 100644 index 0000000..24b57e7 --- /dev/null +++ b/tests/test_datasets/openfoam_mesh_binary/constant/turbulenceProperties @@ -0,0 +1,19 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | foam-extend: Open Source CFD | +| \\ / O peration | Version: 4.0 | +| \\ / A nd | Web: http://www.foam-extend.org | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + location "constant"; + object RASProperties; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +simulationType laminar; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/tests/test_openfoamhandler.py b/tests/test_openfoamhandler.py index 02ad3b5..6cb29ca 100644 --- a/tests/test_openfoamhandler.py +++ b/tests/test_openfoamhandler.py @@ -1,16 +1,20 @@ from unittest import TestCase import unittest +import pytest import numpy as np from smithers.io.openfoam import OpenFoamHandler, FoamMesh openfoam_mesh_path = "tests/test_datasets/openfoam_mesh" +openfoam_mesh_binary_path = "tests/test_datasets/openfoam_mesh_binary" notime_openfoam_mesh_path = "tests/test_datasets/notime_openfoam_mesh" -handler = OpenFoamHandler() -mesh = handler.read(openfoam_mesh_path) +notime_openfoam_mesh_binary_path = "tests/test_datasets/notime_openfoam_mesh_binary" +mesh_ascii = OpenFoamHandler().read(openfoam_mesh_path) +mesh_binary = OpenFoamHandler().read(openfoam_mesh_binary_path) truth_mesh = FoamMesh(openfoam_mesh_path) -def test_read(): +@pytest.mark.parametrize("mesh", [mesh_ascii, mesh_binary]) +def test_read(mesh): assert type(mesh) == dict assert "points" in mesh["0"] @@ -19,7 +23,8 @@ def test_read(): assert "cells" in mesh["0"] -def test_read_boundary_names(): +@pytest.mark.parametrize("mesh", [mesh_ascii, mesh_binary]) +def test_read_boundary_names(mesh): assert set(mesh["0"]["boundary"].keys()) == set( [ b"inlet", @@ -32,19 +37,23 @@ def test_read_boundary_names(): ) -def test_read_points(): +@pytest.mark.parametrize("mesh", [mesh_ascii, mesh_binary]) +def test_read_points(mesh): np.testing.assert_almost_equal(mesh["0"]["points"], truth_mesh.points) -def test_read_faces(): +@pytest.mark.parametrize("mesh", [mesh_ascii, mesh_binary]) +def test_read_faces(mesh): np.testing.assert_almost_equal(mesh["0"]["faces"], truth_mesh.faces) -def test_read_cells(): +@pytest.mark.parametrize("mesh", [mesh_ascii, mesh_binary]) +def test_read_cells(mesh): assert len(mesh["0"]["cells"]) == len(truth_mesh.cell_faces) -def test_read_cell_faces(): +@pytest.mark.parametrize("mesh", [mesh_ascii, mesh_binary]) +def test_read_cell_faces(mesh): a_key = list(mesh["0"]["cells"].keys())[0] smithers_cell = mesh["0"]["cells"][a_key] @@ -53,7 +62,8 @@ def test_read_cell_faces(): ) -def test_read_cell_neighbors(): +@pytest.mark.parametrize("mesh", [mesh_ascii, mesh_binary]) +def test_read_cell_neighbors(mesh): a_key = list(mesh["0"]["cells"].keys())[-1] smithers_cell = mesh["0"]["cells"][a_key] np.testing.assert_almost_equal( @@ -61,7 +71,8 @@ def test_read_cell_neighbors(): ) -def test_read_cell_points(): +@pytest.mark.parametrize("mesh", [mesh_ascii, mesh_binary]) +def test_read_cell_points(mesh): a_key = list(mesh["0"]["cells"].keys())[-1] smithers_cell = mesh["0"]["cells"][a_key] @@ -74,7 +85,8 @@ def test_read_cell_points(): np.testing.assert_almost_equal(smithers_cell["points"], faces_points) -def test_boundary(): +@pytest.mark.parametrize("mesh", [mesh_ascii, mesh_binary]) +def test_boundary(mesh): ofpp_obstacle = truth_mesh.boundary[b"obstacle"] smithers_obstacle = mesh["0"]["boundary"][b"obstacle"] @@ -104,40 +116,46 @@ def test_boundary(): ) -def test_read_fields_time_instants_all(): - all_numeric_mesh = handler.read( - openfoam_mesh_path, time_instants="all_numeric" +@pytest.mark.parametrize("path", [openfoam_mesh_path, openfoam_mesh_binary_path]) +def test_read_fields_time_instants_all(path): + all_numeric_mesh = OpenFoamHandler().read( + path, time_instants="all_numeric" ) assert set(all_numeric_mesh.keys()) == set(["0", "1088", "4196"]) -def test_read_fields_time_instants_first(): +@pytest.mark.parametrize("mesh", [mesh_ascii, mesh_binary]) +def test_read_fields_time_instants_first(mesh): assert set(mesh.keys()) == set(["0"]) -def test_read_fields_time_instants_list(): - handler = OpenFoamHandler() - time_list_mesh = handler.read(openfoam_mesh_path, time_instants=["1088"]) +@pytest.mark.parametrize("path", [openfoam_mesh_path, openfoam_mesh_binary_path]) +def test_read_fields_time_instants_list(path): + time_list_mesh = OpenFoamHandler().read(path, time_instants=["1088"]) assert set(time_list_mesh.keys()) == set(["1088"]) -def test_read_fields_all(): +@pytest.mark.parametrize("mesh", [mesh_ascii, mesh_binary]) +def test_read_fields_all(mesh): for tdc in mesh.values(): assert set(tdc["fields"].keys()) == set(["U", "p"]) -def test_read_fields_list(): - fields_list_mesh = handler.read(openfoam_mesh_path, field_names=["p"]) +@pytest.mark.parametrize("path", [openfoam_mesh_path, openfoam_mesh_binary_path]) +def test_read_fields_list(path): + fields_list_mesh = OpenFoamHandler().read(path, field_names=["p"]) for tdc in fields_list_mesh.values(): assert set(tdc["fields"].keys()) == set(["p"]) -def test_no_time_instants(): +@pytest.mark.parametrize("path", [notime_openfoam_mesh_path, notime_openfoam_mesh_binary_path]) +def test_no_time_instants(path): # assert that this doesn't raise anything - handler.read(notime_openfoam_mesh_path) + OpenFoamHandler().read(path) -def test_area(): +@pytest.mark.parametrize("mesh", [mesh_ascii, mesh_binary]) +def test_area(mesh): np.testing.assert_almost_equal( mesh["0"]["boundary"][b"obstacle"]["faces"]["area"][100], 0.039269502373542965,