def build_surface(self, name: int, kind: str, params: List[float], transform, modifier) -> Surface: options = {"name": name} if transform is not None: transformation = self.transformations[transform] if transformation: options["transform"] = transformation if modifier is not None: options["modifier"] = modifier _surface = create_surface(kind, *params, **options) return _surface
def surfaces(): surf_data = { 1: ('sx', [4, 2]), 2: ('cx', [2]), 3: ('px', [-3]), 4: ('sx', [-3, 1]), 5: ('px', [4]), 6: ('sx', [4, 1]), 7: ('cx', [3]), 8: ('cx', [1]), 9: ('px', [-5]), 10: ('px', [8]), 11: ('px', [20]), 12: ('px', [22]), 13: ('so', [100]) } surfs = {} for name, (kind, params) in surf_data.items(): surfs[name] = create_surface(kind, *params, name=name) return surfs
class TestRectMesh: @pytest.mark.parametrize( "mi, ti", [ (0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3), ], ) def test_creation(self, mi: int, ti: int): bin = bins[mi] tr = transforms[ti] mesh = create_rmesh(bin, tr) np.testing.assert_array_almost_equal(mesh._xbins, bin["xbins"]) np.testing.assert_array_almost_equal(mesh._ybins, bin["ybins"]) np.testing.assert_array_almost_equal(mesh._zbins, bin["zbins"]) origin = [bin["xbins"][0], bin["ybins"][0], bin["zbins"][0]] ex = EX ey = EY ez = EZ if tr is not None: ex = tr.apply2vector(ex) ey = tr.apply2vector(ey) ez = tr.apply2vector(ez) origin = tr.apply2point(origin) np.testing.assert_array_almost_equal(mesh._origin, origin) np.testing.assert_array_almost_equal(mesh._ex, ex) np.testing.assert_array_almost_equal(mesh._ey, ey) np.testing.assert_array_almost_equal(mesh._ez, ez) @pytest.mark.parametrize("ti", range(4)) @pytest.mark.parametrize("mi", range(3)) def test_bounding_box(self, mi: int, ti: int): tr = transforms[ti] bin = bins[mi] mesh = create_rmesh(bin, tr) box = mesh.bounding_box() corners = box.corners ans_corners = [ [bin["xbins"][0], bin["ybins"][0], bin["zbins"][0]], [bin["xbins"][0], bin["ybins"][0], bin["zbins"][-1]], [bin["xbins"][0], bin["ybins"][-1], bin["zbins"][0]], [bin["xbins"][0], bin["ybins"][-1], bin["zbins"][-1]], [bin["xbins"][-1], bin["ybins"][0], bin["zbins"][0]], [bin["xbins"][-1], bin["ybins"][0], bin["zbins"][-1]], [bin["xbins"][-1], bin["ybins"][-1], bin["zbins"][0]], [bin["xbins"][-1], bin["ybins"][-1], bin["zbins"][-1]], ] if tr: ans_corners = tr.apply2point(ans_corners) np.testing.assert_array_almost_equal(corners, ans_corners) @pytest.mark.parametrize( "mi, ti, ans", [ (0, 0, (4, 2, 2)), (0, 1, (4, 2, 2)), (0, 2, (4, 2, 2)), (0, 3, (4, 2, 2)), (1, 0, (3, 1, 2)), (1, 1, (3, 1, 2)), (1, 2, (3, 1, 2)), (1, 3, (3, 1, 2)), (2, 0, (2, 3, 2)), (2, 1, (2, 3, 2)), (2, 2, (2, 3, 2)), (2, 3, (2, 3, 2)), ], ) def test_shape(self, mi, ti, ans): mesh = create_rmesh(bins[mi], transforms[ti]) assert mesh.shape == ans surfaces = { 1: create_surface("PY", 3.5), 2: create_surface("PY", -1.0), 3: create_surface("PX", 3.0), 4: create_surface("PX", 1.5), 5: create_surface("PZ", 2.0), 6: create_surface("PZ", -2.0), 7: create_surface("PX", 5.0), } # noinspection PyTypeChecker bodies = [ Body( [ surfaces[1], "C", surfaces[2], "I", surfaces[3], "C", "I", surfaces[4], "I", surfaces[5], "C", "I", surfaces[6], "I", ], name=1, MAT=Material(atomic=[("Fe", 1)], density=7.8), ), Body( [ surfaces[1], "C", surfaces[2], "I", surfaces[7], "C", "I", surfaces[3], "I", surfaces[5], "C", "I", surfaces[6], "I", ], name=2, MAT=Material(atomic=[("C", 1)], density=2.7), ), Body( [ surfaces[1], surfaces[2], "C", "U", surfaces[4], "C", "U", surfaces[7], "U", surfaces[5], "U", surfaces[6], "C", "U", ], name=3, ), ] @pytest.mark.parametrize("mi, ti", product(range(len(bins)), range(len(transforms)))) def test_get_voxel(self, mi: int, ti: int): tr = transforms[ti] bin = bins[mi] mesh = create_rmesh(bin, tr) shape = mesh.shape ex = EX ey = EY ez = EZ for i, j, k in product(range(shape[0]), range(shape[1]), range(shape[2])): vox = mesh.get_voxel(i, j, k) corners = [] for pr in product( bin["xbins"][i:i + 2], bin["ybins"][j:j + 2], bin["zbins"][k:k + 2], ): corners.append(list(pr)) if tr is not None: corners = tr.apply2point(corners) np.testing.assert_array_almost_equal(vox.corners, corners) points = [ [0.5, 0.9, 0], [-1.4, 0.5, 0.1], [3, 2.5, 1.9], [0.5, 0.1, -0.3], [-1.8, -1.5, -3.5], [-0.1, 0.4, -4.1], [5.1, 4.9, 1.4], [ [0.5, 0.9, 0], [-1.4, 0.5, 0.1], [3, 2.5, 1.9], [0.5, 0.1, -0.3], [-1.8, -1.5, -3.5], [-0.1, 0.4, -4.1], [5.1, 4.9, 1.4], ], ] @pytest.mark.parametrize( "mi, ti, pi, local", product(range(len(bins)), range(len(transforms)), range(len(points)), [False, True]), ) def test_voxel_index(self, mi: int, ti: int, pi: int, local: bool): tr = transforms[ti] bin = bins[mi] mesh = create_rmesh(bin, tr) pt = self.points[pi] result = mesh.voxel_index(pt, local=local) if local is False and tr is not None: pt = tr.reverse().apply2point(pt) def check_one(r, pt): if r is not None: print(r) i, j, k = r assert bin["xbins"][i] <= pt[0] <= bin["xbins"][i + 1] assert bin["ybins"][j] <= pt[1] <= bin["ybins"][j + 1] assert bin["zbins"][k] <= pt[2] <= bin["zbins"][k + 1] else: px = pt[0] <= bin["xbins"][0] or pt[0] >= bin["xbins"][-1] py = pt[1] <= bin["ybins"][0] or pt[1] >= bin["ybins"][-1] pz = pt[2] <= bin["zbins"][0] or pt[2] >= bin["zbins"][-1] assert px or py or pz if np.array(pt).shape == (3, ): check_one(result, pt) else: for r, p in zip(result, pt): check_one(r, p) @pytest.mark.parametrize( "mi, index, answer", [ (0, (3, 1, 0), (3, 1, 0)), (0, (0, 0, 0), (0, 0, 0)), (0, (-1, 0, 0), None), (0, (0, -1, 0), None), (0, (0, 0, -1), None), (0, (4, 1, 1), None), (0, (3, 2, 1), None), (0, (3, 1, 2), None), (1, (2, 0, 1), (2, 0, 1)), (1, (2, 1, 2), None), (2, (1, 2, 1), (1, 2, 1)), (2, (1, 3, 1), None), (2, (10, 10, 10), None), (2, (10, -10, 10), None), ], ) def test_check_indices(self, mi, index, answer): mesh = create_rmesh(bins[mi], None) check = mesh.check_indices(*index) assert check == answer @pytest.mark.parametrize("ti", range(len(transforms))) @pytest.mark.parametrize( "mi, args, expected", [ (0, {}, None), (0, { "X": 0.5 }, { "axis": 0, "index": 0, "x": [1.0, 3.5], "y": [0.0, 2.0] }), ( 0, { "Y": 0.5 }, { "axis": 1, "index": 0, "x": [0.5, 1.5, 3.0, 5.0], "y": [0.0, 2.0] }, ), ( 0, { "Z": 0.5 }, { "axis": 2, "index": 0, "x": [0.5, 1.5, 3.0, 5.0], "y": [1.0, 3.5] }, ), (0, { "X": -1 }, None), (1, { "Y": 10 }, None), (2, { "Z": -100 }, None), (0, { "X": 0.5, "Y": 0.5, "Z": 0.5 }, None), (1, { "X": 0, "Y": 2.5 }, None), (1, { "X": 3, "Y": 0 }, None), (1, { "X": 3, "Z": 9 }, None), (2, { "X": -2, "Y": 0.5 }, None), ( 1, { "Y": 2.9 }, { "axis": 1, "index": 0, "x": [3.0, 5.0, 7.0], "y": [2.5, 5.5] }, ), (1, { "Z": 4.9 }, { "axis": 2, "index": 1, "x": [3.0, 5.0, 7.0], "y": [2.5] }), ( 2, { "X": 0.0 }, { "axis": 0, "index": 1, "x": [-1.5, -0.5, 0.5], "y": [-4.5, -3.5] }, ), ( 2, { "Z": -4.1 }, { "axis": 2, "index": 0, "x": [-2.0, 0.0], "y": [-1.5, -0.5, 0.5] }, ), (0, { "X": 5 }, { "axis": 0, "index": 3, "x": [1.0, 3.5], "y": [0.0, 2.0] }), ], ) def test_slice_index(self, ti: int, mi: int, args, expected): tr = transforms[ti] bin = bins[mi] mesh = create_rmesh(bin, tr) if expected is None: with pytest.raises(ValueError): mesh.slice_axis_index(**args) else: axis, index, x, y = mesh.slice_axis_index(**args) assert axis == expected["axis"] assert index == expected["index"] np.testing.assert_array_almost_equal(x, expected["x"]) np.testing.assert_array_almost_equal(y, expected["y"])
("+2 py 2", [ ('MODIFIER', '+'), ("INTEGER", 2), ("SURFACE_TYPE", "PY"), ("INTEGER", 2), ]), ]) def test_surface_lexer(text, expected): lexer = srp.Lexer() actual = [(f.type, f.value) for f in lexer.tokenize(text)] assert actual == expected # noinspection PyTypeChecker @pytest.mark.parametrize("text,expected", [ ("1 PX 0", create_surface("PX", 0.0, name=1)), ("*1 P 1.5 1.4 1.3 1.2", create_surface("P", 1.5, 1.4, 1.3, 1.2, name=1, modifier='*')), ]) def test_good_path(text, expected): actual = srp.parse(text) assert actual.mcnp_words() == expected.mcnp_words() # noinspection PyTypeChecker @pytest.mark.parametrize("text,expected", [ ("1 2 PX 0", create_surface("PX", 0.0)), ]) def test_absent_surface_with_ignore_strategy(text, expected): actual = srp.parse(text, transformations=IgnoringIndex()) assert actual == expected
("FLOAT", 662.547063827514990), ], ), ], ) def test_surface_lexer(text, expected): lexer = srp.Lexer() actual = [(f.type, f.value) for f in lexer.tokenize(text)] assert actual == expected # noinspection PyTypeChecker @pytest.mark.parametrize( "text,expected", [ ("1 PX 0", create_surface("PX", 0.0, name=1)), ( "*1 P 1.5 1.4 1.3 1.2", create_surface("P", 1.5, 1.4, 1.3, 1.2, name=1, modifier="*"), ), ( """ 158214 RPP +558.364973080895990 +902.003588792269060 & -428.589771011879980 -105.426621291914000 & +438.709115982056010 +662.547063827514990 """, create_surface( "RPP", 558.364973080895990, 902.003588792269060, -428.589771011879980,
assert added_cell is not cell assert added_cell.name() == name assert added_cell.options["U"] is u if cell.material() is not None: assert added_cell.material().composition == cell.material( ).composition assert_change(s_before, s_after, new_surfs) assert_change(c_before, c_after, new_comps) @pytest.mark.parametrize( "case, cells, shapes", [( 1, Body(Shape("C", create_surface("P", 0, 0, -1, -3, name=8)), name=5), [Shape("S", create_surface("P", 0, 0, 1, 3, name=2))], )], ) def test_add_cells_neg(universe, case, cells, shapes): u = universe(case) s_before = u.get_surfaces() u.add_cells(cells, name_rule="keep") s_after = u.get_surfaces() if isinstance(cells, Body): cells = [cells] added_cells = u._cells[-len(cells):] if isinstance(cells, Body): cells = [cells] for added_cell, cell, shape in zip(added_cells, cells, shapes): assert added_cell.shape == shape
class TestRectMesh: @pytest.mark.parametrize('mi, ti', [ (0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3) ]) def test_creation(self, mi, ti): bin = bins[mi] tr = transforms[ti] mesh = create_rmesh(bin, tr) np.testing.assert_array_almost_equal(mesh._xbins, bin['xbins']) np.testing.assert_array_almost_equal(mesh._ybins, bin['ybins']) np.testing.assert_array_almost_equal(mesh._zbins, bin['zbins']) origin = [bin['xbins'][0], bin['ybins'][0], bin['zbins'][0]] ex = EX ey = EY ez = EZ if tr is not None: ex = tr.apply2vector(ex) ey = tr.apply2vector(ey) ez = tr.apply2vector(ez) origin = tr.apply2point(origin) np.testing.assert_array_almost_equal(mesh._origin, origin) np.testing.assert_array_almost_equal(mesh._ex, ex) np.testing.assert_array_almost_equal(mesh._ey, ey) np.testing.assert_array_almost_equal(mesh._ez, ez) @pytest.mark.parametrize('ti', range(4)) @pytest.mark.parametrize('mi', range(3)) def test_bounding_box(self, mi, ti): tr = transforms[ti] bin = bins[mi] mesh = create_rmesh(bin, tr) box = mesh.bounding_box() corners = box.corners ans_corners = [ [bin['xbins'][0], bin['ybins'][0], bin['zbins'][0]], [bin['xbins'][0], bin['ybins'][0], bin['zbins'][-1]], [bin['xbins'][0], bin['ybins'][-1], bin['zbins'][0]], [bin['xbins'][0], bin['ybins'][-1], bin['zbins'][-1]], [bin['xbins'][-1], bin['ybins'][0], bin['zbins'][0]], [bin['xbins'][-1], bin['ybins'][0], bin['zbins'][-1]], [bin['xbins'][-1], bin['ybins'][-1], bin['zbins'][0]], [bin['xbins'][-1], bin['ybins'][-1], bin['zbins'][-1]], ] if tr: ans_corners = tr.apply2point(ans_corners) np.testing.assert_array_almost_equal(corners, ans_corners) @pytest.mark.parametrize('mi, ti, ans', [ (0, 0, (4, 2, 2)), (0, 1, (4, 2, 2)), (0, 2, (4, 2, 2)), (0, 3, (4, 2, 2)), (1, 0, (3, 1, 2)), (1, 1, (3, 1, 2)), (1, 2, (3, 1, 2)), (1, 3, (3, 1, 2)), (2, 0, (2, 3, 2)), (2, 1, (2, 3, 2)), (2, 2, (2, 3, 2)), (2, 3, (2, 3, 2)) ]) def test_shape(self, mi, ti, ans): mesh = create_rmesh(bins[mi], transforms[ti]) assert mesh.shape == ans surfaces = { 1: create_surface('PY', 3.5), 2: create_surface('PY', -1.0), 3: create_surface('PX', 3.0), 4: create_surface('PX', 1.5), 5: create_surface('PZ', 2.0), 6: create_surface('PZ', -2.0), 7: create_surface('PX', 5.0) } bodies = [ Body([surfaces[1], 'C', surfaces[2], 'I', surfaces[3], 'C', 'I', surfaces[4], 'I', surfaces[5], 'C', 'I', surfaces[6], 'I'], name=1, MAT=Material(atomic=[('Fe', 1)], density=7.8)), Body([surfaces[1], 'C', surfaces[2], 'I', surfaces[7], 'C', 'I', surfaces[3], 'I', surfaces[5], 'C', 'I', surfaces[6], 'I'], name=2, MAT=Material(atomic=[('C', 1)], density=2.7)), Body([surfaces[1], surfaces[2], 'C', 'U', surfaces[4], 'C', 'U', surfaces[7], 'U', surfaces[5], 'U', surfaces[6], 'C', 'U'], name=3) ] @pytest.mark.parametrize('mi, ti', product(range(len(bins)), range(len(transforms)))) def test_get_voxel(self, mi, ti): tr = transforms[ti] bin = bins[mi] mesh = create_rmesh(bin, tr) shape = mesh.shape ex = EX ey = EY ez = EZ for i, j, k in product(range(shape[0]), range(shape[1]), range(shape[2])): vox = mesh.get_voxel(i, j, k) corners = [] for pr in product(bin['xbins'][i:i+2], bin['ybins'][j:j+2], bin['zbins'][k:k+2]): corners.append(list(pr)) if tr is not None: corners = tr.apply2point(corners) np.testing.assert_array_almost_equal(vox.corners, corners) points = [ [0.5, 0.9, 0], [-1.4, 0.5, 0.1], [3, 2.5, 1.9], [0.5, 0.1, -0.3], [-1.8, -1.5, -3.5], [-0.1, 0.4, -4.1], [5.1, 4.9, 1.4], [[0.5, 0.9, 0], [-1.4, 0.5, 0.1], [3, 2.5, 1.9], [0.5, 0.1, -0.3], [-1.8, -1.5, -3.5], [-0.1, 0.4, -4.1], [5.1, 4.9, 1.4]] ] @pytest.mark.parametrize('mi, ti, pi, local', product(range(len(bins)), range(len(transforms)), range(len(points)), [False, True])) def test_voxel_index(self, mi, ti, pi, local): tr = transforms[ti] bin = bins[mi] mesh = create_rmesh(bin, tr) pt = self.points[pi] result = mesh.voxel_index(pt, local=local) if local == False and tr is not None: pt = tr.reverse().apply2point(pt) def check_one(r, pt): if r is not None: print(r) i, j, k = r assert bin['xbins'][i] <= pt[0] <= bin['xbins'][i+1] assert bin['ybins'][j] <= pt[1] <= bin['ybins'][j+1] assert bin['zbins'][k] <= pt[2] <= bin['zbins'][k+1] else: px = pt[0] <= bin['xbins'][0] or pt[0] >= bin['xbins'][-1] py = pt[1] <= bin['ybins'][0] or pt[1] >= bin['ybins'][-1] pz = pt[2] <= bin['zbins'][0] or pt[2] >= bin['zbins'][-1] assert px or py or pz if np.array(pt).shape == (3,): check_one(result, pt) else: for r, p in zip(result, pt): check_one(r, p) @pytest.mark.parametrize('mi, index, answer', [ (0, (3, 1, 0), (3, 1, 0)), (0, (0, 0, 0), (0, 0, 0)), (0, (-1, 0, 0), None), (0, (0, -1, 0), None), (0, (0, 0, -1), None), (0, (4, 1, 1), None), (0, (3, 2, 1), None), (0, (3, 1, 2), None), (1, (2, 0, 1), (2, 0, 1)), (1, (2, 1, 2), None), (2, (1, 2, 1), (1, 2, 1)), (2, (1, 3, 1), None), (2, (10, 10, 10), None), (2, (10, -10, 10), None) ]) def test_check_indices(self, mi, index, answer): mesh = create_rmesh(bins[mi], None) check = mesh.check_indices(*index) assert check == answer @pytest.mark.parametrize('ti', range(len(transforms))) @pytest.mark.parametrize('mi, args, expected', [ (0, {}, None), (0, {'X': 0.5}, {'axis': 0, 'index': 0, 'x': [1.0, 3.5], 'y': [0.0, 2.0]}), (0, {'Y': 0.5}, {'axis': 1, 'index': 0, 'x': [0.5, 1.5, 3.0, 5.0], 'y': [0.0, 2.0]}), (0, {'Z': 0.5}, {'axis': 2, 'index': 0, 'x': [0.5, 1.5, 3.0, 5.0], 'y': [1.0, 3.5]}), (0, {'X': -1}, None), (1, {'Y': 10}, None), (2, {'Z': -100}, None), (0, {'X': 0.5, 'Y': 0.5, 'Z': 0.5}, None), (1, {'X': 0, 'Y': 2.5}, None), (1, {'X': 3, 'Y': 0}, None), (1, {'X': 3, 'Z': 9}, None), (2, {'X': -2, 'Y': 0.5}, None), (1, {'Y': 2.9}, {'axis': 1, 'index': 0, 'x': [3.0, 5.0, 7.0], 'y': [2.5, 5.5]}), (1, {'Z': 4.9}, {'axis': 2, 'index': 1, 'x': [3.0, 5.0, 7.0], 'y': [2.5]}), (2, {'X': 0.0}, {'axis': 0, 'index': 1, 'x': [-1.5, -0.5, 0.5], 'y': [-4.5, -3.5]}), (2, {'Z': -4.1}, {'axis': 2, 'index': 0, 'x': [-2.0, 0.0], 'y': [-1.5, -0.5, 0.5]}), (0, {'X': 5}, {'axis': 0, 'index': 3, 'x': [1.0, 3.5], 'y': [0.0, 2.0]}) ]) def test_slice_index(self, ti, mi, args, expected): tr = transforms[ti] bin = bins[mi] mesh = create_rmesh(bin, tr) if expected is None: with pytest.raises(ValueError): mesh.slice_axis_index(**args) else: axis, index, x, y = mesh.slice_axis_index(**args) assert axis == expected['axis'] assert index == expected['index'] np.testing.assert_array_almost_equal(x, expected['x']) np.testing.assert_array_almost_equal(y, expected['y'])