From a47f3abbde71fb5b952cd6156698db08bd6197e2 Mon Sep 17 00:00:00 2001 From: tjlaboss Date: Mon, 21 May 2018 17:13:37 -0400 Subject: [PATCH 1/6] New class method, Mesh.fromRectLattice() --- openmc/mesh.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/openmc/mesh.py b/openmc/mesh.py index c9c76552b..7c6fc4867 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -182,6 +182,40 @@ class Mesh(IDManagerMixin): return mesh + @classmethod + def fromRectLattice(cls, lattice, division=1, mesh_id=None, name=''): + """Create mesh from an existing rectangular lattice + + Parameters + ---------- + lattice : openmc.RectLattice + Rectangular lattice used as a template for this mesh + division : int, optional + Number of mesh cells per lattice cell. + If not specified, there will be 1 mesh cell per lattice cell. + mesh_id : int, optional + Unique identifier for the mesh + name : str, optional + Name of the mesh + + Returns + ------- + openmc.Mesh + Mesh instance + + """ + cv.check_type('rectangular lattice', lattice, openmc.RectLattice) + + shape = np.array(lattice.shape) + width = lattice.pitch*shape + + mesh = cls(mesh_id, name) + mesh.lower_left = lattice.lower_left + mesh.upper_right = lattice.lower_left + width + mesh.dimension = shape*division + + return mesh + def to_xml_element(self): """Return XML representation of the mesh From 75dc4131dc6538f7ed243ef95eb274cb9ddd7a18 Mon Sep 17 00:00:00 2001 From: tjlaboss Date: Tue, 22 May 2018 13:36:50 -0400 Subject: [PATCH 2/6] Long live PEP8 --- openmc/mesh.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 7c6fc4867..3f53582d5 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -183,19 +183,19 @@ class Mesh(IDManagerMixin): return mesh @classmethod - def fromRectLattice(cls, lattice, division=1, mesh_id=None, name=''): + def from_rect_lattice(cls, lattice, division=1, mesh_id=None, name=''): """Create mesh from an existing rectangular lattice Parameters ---------- lattice : openmc.RectLattice Rectangular lattice used as a template for this mesh - division : int, optional + division : int Number of mesh cells per lattice cell. If not specified, there will be 1 mesh cell per lattice cell. - mesh_id : int, optional + mesh_id : int Unique identifier for the mesh - name : str, optional + name : str Name of the mesh Returns From 0da329e0ae4fe52bcbdebd8d88c53e1193617f68 Mon Sep 17 00:00:00 2001 From: tjlaboss Date: Tue, 22 May 2018 17:04:07 -0400 Subject: [PATCH 3/6] Unit test for Mesh.from_rect_lattice() --- tests/unit_tests/test_mesh_from_lattice.py | 116 +++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 tests/unit_tests/test_mesh_from_lattice.py diff --git a/tests/unit_tests/test_mesh_from_lattice.py b/tests/unit_tests/test_mesh_from_lattice.py new file mode 100644 index 000000000..6091dd1cd --- /dev/null +++ b/tests/unit_tests/test_mesh_from_lattice.py @@ -0,0 +1,116 @@ +import numpy as np +import openmc +import pytest + + +@pytest.fixture(scope='module') +def pincell1(uo2, water): + cyl = openmc.ZCylinder(R=0.35) + fuel = openmc.Cell(fill=uo2, region=-cyl) + moderator = openmc.Cell(fill=water, region=+cyl) + + univ = openmc.Universe(cells=[fuel, moderator]) + univ.fuel = fuel + univ.moderator = moderator + return univ + + +@pytest.fixture(scope='module') +def pincell2(uo2, water): + cyl = openmc.ZCylinder(R=0.4) + fuel = openmc.Cell(fill=uo2, region=-cyl) + moderator = openmc.Cell(fill=water, region=+cyl) + + univ = openmc.Universe(cells=[fuel, moderator]) + univ.fuel = fuel + univ.moderator = moderator + return univ + + +@pytest.fixture(scope='module') +def zr(): + zr = openmc.Material() + zr.add_element('Zr', 1.0) + zr.set_density('g/cm3', 1.0) + return zr + + +@pytest.fixture(scope='module') +def rlat2(pincell1, pincell2, uo2, water, zr): + """2D Rectangular lattice for testing.""" + all_zr = openmc.Cell(fill=zr) + pitch = 1.2 + n = 3 + u1, u2 = pincell1, pincell2 + lattice = openmc.RectLattice() + lattice.lower_left = (-pitch*n/2, -pitch*n/2) + lattice.pitch = (pitch, pitch) + lattice.outer = openmc.Universe(cells=[all_zr]) + lattice.universes = [ + [u1, u2, u1], + [u2, u1, u2], + [u2, u1, u1] + ] + + return lattice + + +@pytest.fixture(scope='module') +def rlat3(pincell1, pincell2, uo2, water, zr): + """3D Rectangular lattice for testing.""" + + # Create another universe for top layer + hydrogen = openmc.Material() + hydrogen.add_element('H', 1.0) + hydrogen.set_density('g/cm3', 0.09) + h_cell = openmc.Cell(fill=hydrogen) + u3 = openmc.Universe(cells=[h_cell]) + + all_zr = openmc.Cell(fill=zr) + pitch = 1.2 + n = 3 + u1, u2 = pincell1, pincell2 + lattice = openmc.RectLattice() + lattice.lower_left = (-pitch*n/2, -pitch*n/2, -10.0) + lattice.pitch = (pitch, pitch, 10.0) + lattice.outer = openmc.Universe(cells=[all_zr]) + lattice.universes = [ + [[u1, u2, u1], + [u2, u1, u2], + [u2, u1, u1]], + [[u3, u1, u2], + [u1, u3, u2], + [u2, u1, u1]] + ] + + return lattice + + +def test_mesh2d(rlat2d): + shape = np.array(rlat2d.shape) + width = shape*rlat2d.pitch + + mesh1 = openmc.Mesh().from_rect_lattice(rlat2d) + assert np.array_equal(mesh1.dimension, (3, 3)) + assert np.array_equal(mesh1.lower_left, rlat2d.lower_left) + assert np.array_equal(mesh1.upper_right, rlat2d.lower_left + width) + + mesh2 = openmc.Mesh().from_rect_lattice(rlat2d, division=3) + assert np.array_equal(mesh2.dimension, (9, 9)) + assert np.array_equal(mesh2.lower_left, rlat2d.lower_left) + assert np.array_equal(mesh2.upper_right, rlat2d.lower_left + width) + + +def test_mesh3d(rlat3d): + shape = np.array(rlat3d.shape) + width = shape*rlat3d.pitch + + mesh1 = openmc.Mesh().from_rect_lattice(rlat3d) + assert np.array_equal(mesh1.dimension, (3, 3, 2)) + assert np.array_equal(mesh1.lower_left, rlat3d.lower_left) + assert np.array_equal(mesh1.upper_right, rlat3d.lower_left + width) + + mesh2 = openmc.Mesh().from_rect_lattice(rlat3d, division=3) + assert np.array_equal(mesh2.dimension, (9, 9, 6)) + assert np.array_equal(mesh2.lower_left, rlat3d.lower_left) + assert np.array_equal(mesh2.upper_right, rlat3d.lower_left + width) From 7801761a7035f3ba2f8a326390b6fbf9fda87f5a Mon Sep 17 00:00:00 2001 From: tjlaboss Date: Tue, 22 May 2018 23:49:07 -0400 Subject: [PATCH 4/6] Correct names of fixtures `rlat2d` --> `rlat2` `rlat3d` --> `rlat3` --- tests/unit_tests/test_mesh_from_lattice.py | 36 +++++++++++----------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/unit_tests/test_mesh_from_lattice.py b/tests/unit_tests/test_mesh_from_lattice.py index 6091dd1cd..563e51c3b 100644 --- a/tests/unit_tests/test_mesh_from_lattice.py +++ b/tests/unit_tests/test_mesh_from_lattice.py @@ -86,31 +86,31 @@ def rlat3(pincell1, pincell2, uo2, water, zr): return lattice -def test_mesh2d(rlat2d): - shape = np.array(rlat2d.shape) - width = shape*rlat2d.pitch +def test_mesh2d(rlat2): + shape = np.array(rlat2.shape) + width = shape*rlat2.pitch - mesh1 = openmc.Mesh().from_rect_lattice(rlat2d) + mesh1 = openmc.Mesh().from_rect_lattice(rlat2) assert np.array_equal(mesh1.dimension, (3, 3)) - assert np.array_equal(mesh1.lower_left, rlat2d.lower_left) - assert np.array_equal(mesh1.upper_right, rlat2d.lower_left + width) + assert np.array_equal(mesh1.lower_left, rlat2.lower_left) + assert np.array_equal(mesh1.upper_right, rlat2.lower_left + width) - mesh2 = openmc.Mesh().from_rect_lattice(rlat2d, division=3) + mesh2 = openmc.Mesh().from_rect_lattice(rlat2, division=3) assert np.array_equal(mesh2.dimension, (9, 9)) - assert np.array_equal(mesh2.lower_left, rlat2d.lower_left) - assert np.array_equal(mesh2.upper_right, rlat2d.lower_left + width) + assert np.array_equal(mesh2.lower_left, rlat2.lower_left) + assert np.array_equal(mesh2.upper_right, rlat2.lower_left + width) -def test_mesh3d(rlat3d): - shape = np.array(rlat3d.shape) - width = shape*rlat3d.pitch +def test_mesh3d(rlat3): + shape = np.array(rlat3.shape) + width = shape*rlat3.pitch - mesh1 = openmc.Mesh().from_rect_lattice(rlat3d) + mesh1 = openmc.Mesh().from_rect_lattice(rlat3) assert np.array_equal(mesh1.dimension, (3, 3, 2)) - assert np.array_equal(mesh1.lower_left, rlat3d.lower_left) - assert np.array_equal(mesh1.upper_right, rlat3d.lower_left + width) + assert np.array_equal(mesh1.lower_left, rlat3.lower_left) + assert np.array_equal(mesh1.upper_right, rlat3.lower_left + width) - mesh2 = openmc.Mesh().from_rect_lattice(rlat3d, division=3) + mesh2 = openmc.Mesh().from_rect_lattice(rlat3, division=3) assert np.array_equal(mesh2.dimension, (9, 9, 6)) - assert np.array_equal(mesh2.lower_left, rlat3d.lower_left) - assert np.array_equal(mesh2.upper_right, rlat3d.lower_left + width) + assert np.array_equal(mesh2.lower_left, rlat3.lower_left) + assert np.array_equal(mesh2.upper_right, rlat3.lower_left + width) From 36e2ea6b8aeef9996c3049070e0126c69b22b30e Mon Sep 17 00:00:00 2001 From: tjlaboss Date: Wed, 23 May 2018 11:51:26 -0400 Subject: [PATCH 5/6] Avoid instantiating a Mesh during class method --- tests/unit_tests/test_mesh_from_lattice.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit_tests/test_mesh_from_lattice.py b/tests/unit_tests/test_mesh_from_lattice.py index 563e51c3b..4d79fc579 100644 --- a/tests/unit_tests/test_mesh_from_lattice.py +++ b/tests/unit_tests/test_mesh_from_lattice.py @@ -90,12 +90,12 @@ def test_mesh2d(rlat2): shape = np.array(rlat2.shape) width = shape*rlat2.pitch - mesh1 = openmc.Mesh().from_rect_lattice(rlat2) + mesh1 = openmc.Mesh.from_rect_lattice(rlat2) assert np.array_equal(mesh1.dimension, (3, 3)) assert np.array_equal(mesh1.lower_left, rlat2.lower_left) assert np.array_equal(mesh1.upper_right, rlat2.lower_left + width) - mesh2 = openmc.Mesh().from_rect_lattice(rlat2, division=3) + mesh2 = openmc.Mesh.from_rect_lattice(rlat2, division=3) assert np.array_equal(mesh2.dimension, (9, 9)) assert np.array_equal(mesh2.lower_left, rlat2.lower_left) assert np.array_equal(mesh2.upper_right, rlat2.lower_left + width) @@ -105,12 +105,12 @@ def test_mesh3d(rlat3): shape = np.array(rlat3.shape) width = shape*rlat3.pitch - mesh1 = openmc.Mesh().from_rect_lattice(rlat3) + mesh1 = openmc.Mesh.from_rect_lattice(rlat3) assert np.array_equal(mesh1.dimension, (3, 3, 2)) assert np.array_equal(mesh1.lower_left, rlat3.lower_left) assert np.array_equal(mesh1.upper_right, rlat3.lower_left + width) - mesh2 = openmc.Mesh().from_rect_lattice(rlat3, division=3) + mesh2 = openmc.Mesh.from_rect_lattice(rlat3, division=3) assert np.array_equal(mesh2.dimension, (9, 9, 6)) assert np.array_equal(mesh2.lower_left, rlat3.lower_left) assert np.array_equal(mesh2.upper_right, rlat3.lower_left + width) From 2e6b40cb1b7fd18ee945fd3cac517a281b67b05d Mon Sep 17 00:00:00 2001 From: Alex Lindsay Date: Wed, 23 May 2018 14:16:17 -0600 Subject: [PATCH 6/6] Consistently check derived for sum and sum_sq in tallies. Closes #1010 --- openmc/tallies.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/tallies.py b/openmc/tallies.py index a5341cbed..74f342903 100644 --- a/openmc/tallies.py +++ b/openmc/tallies.py @@ -248,7 +248,7 @@ class Tally(IDManagerMixin): @property def sum_sq(self): - if not self._sp_filename: + if not self._sp_filename or self.derived: return None if not self._results_read: