Skip to content

Commit

Permalink
Ensure that property setters are used in CylindricalMesh and Spherica…
Browse files Browse the repository at this point in the history
…lMesh (#2709)
  • Loading branch information
paulromano authored Sep 27, 2023
1 parent 11a4638 commit 0c6da38
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
32 changes: 16 additions & 16 deletions openmc/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ def x_grid(self):
@x_grid.setter
def x_grid(self, grid):
cv.check_type('mesh x_grid', grid, Iterable, Real)
self._x_grid = np.asarray(grid)
self._x_grid = np.asarray(grid, dtype=float)

@property
def y_grid(self):
Expand All @@ -1045,7 +1045,7 @@ def y_grid(self):
@y_grid.setter
def y_grid(self, grid):
cv.check_type('mesh y_grid', grid, Iterable, Real)
self._y_grid = np.asarray(grid)
self._y_grid = np.asarray(grid, dtype=float)

@property
def z_grid(self):
Expand All @@ -1054,7 +1054,7 @@ def z_grid(self):
@z_grid.setter
def z_grid(self, grid):
cv.check_type('mesh z_grid', grid, Iterable, Real)
self._z_grid = np.asarray(grid)
self._z_grid = np.asarray(grid, dtype=float)

@property
def _grids(self):
Expand Down Expand Up @@ -1249,9 +1249,9 @@ def __init__(
):
super().__init__(mesh_id, name)

self._r_grid = r_grid
self._phi_grid = phi_grid
self._z_grid = z_grid
self.r_grid = r_grid
self.phi_grid = phi_grid
self.z_grid = z_grid
self.origin = origin

@property
Expand Down Expand Up @@ -1281,7 +1281,7 @@ def r_grid(self):
@r_grid.setter
def r_grid(self, grid):
cv.check_type('mesh r_grid', grid, Iterable, Real)
self._r_grid = np.asarray(grid)
self._r_grid = np.asarray(grid, dtype=float)

@property
def phi_grid(self):
Expand All @@ -1290,7 +1290,7 @@ def phi_grid(self):
@phi_grid.setter
def phi_grid(self, grid):
cv.check_type('mesh phi_grid', grid, Iterable, Real)
self._phi_grid = np.asarray(grid)
self._phi_grid = np.asarray(grid, dtype=float)

@property
def z_grid(self):
Expand All @@ -1299,7 +1299,7 @@ def z_grid(self):
@z_grid.setter
def z_grid(self, grid):
cv.check_type('mesh z_grid', grid, Iterable, Real)
self._z_grid = np.asarray(grid)
self._z_grid = np.asarray(grid, dtype=float)

@property
def _grids(self):
Expand Down Expand Up @@ -1610,9 +1610,9 @@ def __init__(
):
super().__init__(mesh_id, name)

self._r_grid = r_grid
self._theta_grid = theta_grid
self._phi_grid = phi_grid
self.r_grid = r_grid
self.theta_grid = theta_grid
self.phi_grid = phi_grid
self.origin = origin

@property
Expand All @@ -1633,7 +1633,7 @@ def origin(self):
def origin(self, coords):
cv.check_type('mesh origin', coords, Iterable, Real)
cv.check_length("mesh origin", coords, 3)
self._origin = np.asarray(coords)
self._origin = np.asarray(coords, dtype=float)

@property
def r_grid(self):
Expand All @@ -1642,7 +1642,7 @@ def r_grid(self):
@r_grid.setter
def r_grid(self, grid):
cv.check_type('mesh r_grid', grid, Iterable, Real)
self._r_grid = np.asarray(grid)
self._r_grid = np.asarray(grid, dtype=float)

@property
def theta_grid(self):
Expand All @@ -1651,7 +1651,7 @@ def theta_grid(self):
@theta_grid.setter
def theta_grid(self, grid):
cv.check_type('mesh theta_grid', grid, Iterable, Real)
self._theta_grid = np.asarray(grid)
self._theta_grid = np.asarray(grid, dtype=float)

@property
def phi_grid(self):
Expand All @@ -1660,7 +1660,7 @@ def phi_grid(self):
@phi_grid.setter
def phi_grid(self, grid):
cv.check_type('mesh phi_grid', grid, Iterable, Real)
self._phi_grid = np.asarray(grid)
self._phi_grid = np.asarray(grid, dtype=float)

@property
def _grids(self):
Expand Down
2 changes: 1 addition & 1 deletion openmc/weight_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ def from_xml_element(cls, elem: ET.Element, meshes: dict) -> WeightWindowGenerat
wwg.on_the_fly = bool(get_text(elem, 'on_the_fly'))
wwg.method = get_text(elem, 'method')

if elem.find('update_parameters'):
if elem.find('update_parameters') is not None:
update_parameters = {}
params_elem = elem.find('update_parameters')
for entry in params_elem:
Expand Down
4 changes: 2 additions & 2 deletions tests/regression_tests/time_cutoff/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def time_model():
settings_file.batches = 10
settings_file.particles = 100
settings_file.cutoff = {'time_neutron': time_cutoff}
settings_file.source = openmc.source.Source(space=openmc.stats.Point(),
energy=openmc.stats.Discrete([1e4], [1]))
settings_file.source = openmc.IndependentSource(
space=openmc.stats.Point(), energy=openmc.stats.Discrete([1e4], [1]))
model.settings = settings_file

# Tally flux under time cutoff
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/test_deplete_transfer_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def test_get_set(model, case_name, transfer_rates):
transfer_rate)
elif case_name == 'rates_invalid_2':
with pytest.raises(ValueError, match='Cannot add transfer '
f'rate for element Gd to material 1 with '
'transfer rate\(s\) for nuclide\(s\) '
f'rate for element Gd to material 1 with '
r'transfer rate\(s\) for nuclide\(s\) '
'Gd156, Gd157.'):
for component, transfer_rate in transfer_rates.items():
transfer.set_transfer_rate(material_input,
Expand Down
24 changes: 16 additions & 8 deletions tests/unit_tests/test_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_SphericalMesh_initiation():
# test defaults
mesh = openmc.SphericalMesh(r_grid=(0, 10))
assert (mesh.origin == np.array([0, 0, 0])).all()
assert mesh.r_grid == (0, 10)
assert (mesh.r_grid == np.array([0, 10])).all()
assert (mesh.theta_grid == np.array([0, pi])).all()
assert (mesh.phi_grid == np.array([0, 2*pi])).all()

Expand All @@ -113,22 +113,26 @@ def test_SphericalMesh_initiation():
phi_grid=(2, 4)
)
assert (mesh.origin == np.array([1, 2, 3])).all()
assert mesh.r_grid == (0., 2.)
assert mesh.theta_grid == (1, 3)
assert mesh.phi_grid == (2, 4)
assert (mesh.r_grid == np.array([0., 2.])).all()
assert (mesh.theta_grid == np.array([1, 3])).all()
assert (mesh.phi_grid == np.array([2, 4])).all()

# test attribute changing
mesh.r_grid = (0, 11)
assert (mesh.r_grid == np.array([0., 11.])).all()

# waffles and pancakes are unfortunately not valid radii
with pytest.raises(TypeError):
openmc.SphericalMesh(('🧇', '🥞'))


def test_CylindricalMesh_initiation():
# test defaults
mesh = openmc.CylindricalMesh(r_grid=(0, 10), z_grid=(0, 10))
assert (mesh.origin == np.array([0, 0, 0])).all()
assert mesh.r_grid == (0, 10)
assert mesh.phi_grid == (0, 2*pi)
assert mesh.z_grid == (0, 10)
assert (mesh.r_grid == np.array([0, 10])).all()
assert (mesh.phi_grid == np.array([0, 2*pi])).all()
assert (mesh.z_grid == np.array([0, 10])).all()

# test setting on creation
mesh = openmc.CylindricalMesh(
Expand All @@ -139,11 +143,15 @@ def test_CylindricalMesh_initiation():
)
assert (mesh.origin == np.array([1, 2, 3])).all()
assert (mesh.r_grid == np.array([0., 2.])).all()
assert mesh.z_grid == (1, 3)
assert (mesh.z_grid == np.array([1, 3])).all()
assert (mesh.phi_grid == np.array([2, 4])).all()

# test attribute changing
mesh.r_grid = (0., 10.)
assert (mesh.r_grid == np.array([0, 10.])).all()
mesh.z_grid = (0., 4.)
assert (mesh.z_grid == np.array([0, 4.])).all()

# waffles and pancakes are unfortunately not valid radii
with pytest.raises(TypeError):
openmc.SphericalMesh(('🧇', '🥞'))

0 comments on commit 0c6da38

Please sign in to comment.