From 6bb6e2ec0cae0f562180018a4d6cd2ef73e5d6a2 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Fri, 21 Jan 2022 08:18:54 -0600 Subject: [PATCH] Fix failing unit tests --- openmc/lib/mesh.py | 68 ++++++++++++++++++++++++++-------- tests/unit_tests/test_lib.py | 30 +++++++-------- tests/unit_tests/test_model.py | 4 +- 3 files changed, 69 insertions(+), 33 deletions(-) diff --git a/openmc/lib/mesh.py b/openmc/lib/mesh.py index 43f66d7a1..df64eee30 100644 --- a/openmc/lib/mesh.py +++ b/openmc/lib/mesh.py @@ -285,6 +285,18 @@ class RectilinearMesh(Mesh): return (lower_left, upper_right, dimension, width) def set_grid(self, x_grid, y_grid, z_grid): + """Set grid values + + Parameters + ---------- + x_grid : iterable of float + Mesh boundary points along the x-axis. + y_grid : iterable of float + Mesh boundary points along the y-axis. + z_grid : iterable of float + Mesh boundary points along the z-axis. + + """ nx = len(x_grid) x_grid = (c_double*nx)(*x_grid) ny = len(y_grid) @@ -371,15 +383,27 @@ class CylindricalMesh(Mesh): return (lower_left, upper_right, dimension, width) - def set_grid(self, x_grid, y_grid, z_grid): - nx = len(x_grid) - x_grid = (c_double*nx)(*x_grid) - ny = len(y_grid) - y_grid = (c_double*ny)(*y_grid) + def set_grid(self, r_grid, phi_grid, z_grid): + """Set grid values + + Parameters + ---------- + r_grid : iterable of float + Mesh boundary points along the r-axis + phi_grid : Iterable of float + Mesh boundary points along the phi-axis + z_grid : Iterable of float + Mesh boundary points along the z-axis + + """ + nr = len(r_grid) + r_grid = (c_double*nr)(*r_grid) + nphi = len(phi_grid) + phi_grid = (c_double*nphi)(*phi_grid) nz = len(z_grid) z_grid = (c_double*nz)(*z_grid) - _dll.openmc_cylindrical_mesh_set_grid(self._index, x_grid, nx, y_grid, - ny, z_grid, nz) + _dll.openmc_cylindrical_mesh_set_grid(self._index, r_grid, nr, phi_grid, + nphi, z_grid, nz) class SphericalMesh(Mesh): """SphericalMesh stored internally. @@ -457,15 +481,27 @@ class SphericalMesh(Mesh): return (lower_left, upper_right, dimension, width) - def set_grid(self, x_grid, y_grid, z_grid): - nx = len(x_grid) - x_grid = (c_double*nx)(*x_grid) - ny = len(y_grid) - y_grid = (c_double*ny)(*y_grid) - nz = len(z_grid) - z_grid = (c_double*nz)(*z_grid) - _dll.openmc_spherical_mesh_set_grid(self._index, x_grid, nx, y_grid, - ny, z_grid, nz) + def set_grid(self, r_grid, theta_grid, phi_grid): + """Set grid values + + Parameters + ---------- + r_grid : iterable of float + Mesh boundary points along the r-axis + theta_grid : Iterable of float + Mesh boundary points along the theta-axis + phi_grid : Iterable of float + Mesh boundary points along the phi-axis + + """ + nr = len(r_grid) + r_grid = (c_double*nr)(*r_grid) + ntheta = len(theta_grid) + theta_grid = (c_double*ntheta)(*theta_grid) + nphi = len(phi_grid) + phi_grid = (c_double*nphi)(*phi_grid) + _dll.openmc_spherical_mesh_set_grid(self._index, r_grid, nr, theta_grid, + ntheta, phi_grid, nphi) _MESH_TYPE_MAP = { diff --git a/tests/unit_tests/test_lib.py b/tests/unit_tests/test_lib.py index 3572e9863..e14e04adb 100644 --- a/tests/unit_tests/test_lib.py +++ b/tests/unit_tests/test_lib.py @@ -583,17 +583,17 @@ def test_rectilinear_mesh(lib_init): def test_cylindrical_mesh(lib_init): deg2rad = lambda deg: deg*np.pi/180 mesh = openmc.lib.CylindricalMesh() - x_grid = [0., 5., 10.] - y_grid = [0., 10., 20.] + r_grid = [0., 5., 10.] + phi_grid = np.radians([0., 10., 20.]) z_grid = [10., 20., 30.] - mesh.set_grid(x_grid, y_grid, z_grid) + mesh.set_grid(r_grid, phi_grid, z_grid) assert np.all(mesh.lower_left == (0., 0., 10.)) assert np.all(mesh.upper_right == (10., deg2rad(20.), 30.)) assert np.all(mesh.dimension == (2, 2, 2)) - for i, diff_x in enumerate(np.diff(x_grid)): - for j, diff_y in enumerate(np.diff(y_grid)): - for k, diff_z in enumerate(np.diff(z_grid)): - assert np.all(mesh.width[i, j, k, :] == (5, deg2rad(10), 10)) + for i, _ in enumerate(np.diff(r_grid)): + for j, _ in enumerate(np.diff(phi_grid)): + for k, _ in enumerate(np.diff(z_grid)): + assert np.allclose(mesh.width[i, j, k, :], (5, deg2rad(10), 10)) with pytest.raises(exc.AllocationError): mesh2 = openmc.lib.CylindricalMesh(mesh.id) @@ -614,17 +614,17 @@ def test_cylindrical_mesh(lib_init): def test_spherical_mesh(lib_init): deg2rad = lambda deg: deg*np.pi/180 mesh = openmc.lib.SphericalMesh() - x_grid = [0., 5., 10.] - y_grid = [0., 10., 20.] - z_grid = [10., 20., 30.] - mesh.set_grid(x_grid, y_grid, z_grid) + r_grid = [0., 5., 10.] + theta_grid = np.radians([0., 10., 20.]) + phi_grid = np.radians([10., 20., 30.]) + mesh.set_grid(r_grid, theta_grid, phi_grid) assert np.all(mesh.lower_left == (0., 0., deg2rad(10.))) assert np.all(mesh.upper_right == (10., deg2rad(20.), deg2rad(30.))) assert np.all(mesh.dimension == (2, 2, 2)) - for i, diff_x in enumerate(np.diff(x_grid)): - for j, diff_y in enumerate(np.diff(y_grid)): - for k, diff_z in enumerate(np.diff(z_grid)): - assert np.all(abs(mesh.width[i, j, k, :] - (5, deg2rad(10), deg2rad(10))) < 1e-16) + for i, _ in enumerate(np.diff(r_grid)): + for j, _ in enumerate(np.diff(theta_grid)): + for k, _ in enumerate(np.diff(phi_grid)): + assert np.allclose(mesh.width[i, j, k, :], (5, deg2rad(10), deg2rad(10))) with pytest.raises(exc.AllocationError): mesh2 = openmc.lib.SphericalMesh(mesh.id) diff --git a/tests/unit_tests/test_model.py b/tests/unit_tests/test_model.py index d22485b3c..80c13fe61 100644 --- a/tests/unit_tests/test_model.py +++ b/tests/unit_tests/test_model.py @@ -195,8 +195,8 @@ def test_from_xml(run_in_tmpdir, pin_model_attributes): keys = sorted(k for k in settings.__dict__.keys() if k not in no_test) for ref_k in keys: assert test_model.settings.__dict__[ref_k] == settings.__dict__[ref_k] - assert len(test_model.tallies) == 0 - assert len(test_model.plots) == 0 + assert len(test_model.tallies) == 1 + assert len(test_model.plots) == 2 assert test_model._materials_by_id == \ {1: test_model.materials[0], 2: test_model.materials[1], 3: test_model.materials[2]}