def test_union(self, geometry, no1, no2, kwargs): body1 = Body(geometry[no1], **kwargs) body2 = Body(geometry[no2], name='1001') body = body1.union(body2) assert body.shape == body1.shape.union(body2.shape) for k, v in kwargs.items(): assert body.options[k] == v
def test_transform(self, geometry, tr, case_no, fill_tr): # The idea is to generate many random points. This points have some # definite test results with respect to the body being tested. # After transformation they must have absolutely the same results. points = np.random.random((10000, 3)) points -= np.array([0.5, 0.5, 0.5]) points *= np.array([20, 10, 10]) if fill_tr is not None: fill = {'transform': fill_tr} points1 = fill_tr.apply2point(points) else: fill = None points1 = points body = Body(geometry[case_no], FILL=fill) results = body.shape.test_points(points1) new_body = body.transform(tr) if fill_tr: points2 = new_body.options['FILL']['transform'].apply2point(points) else: points2 = tr.apply2point(points) new_results = new_body.shape.test_points(points2) # TODO: Check testing of FILL without 'transform' case np.testing.assert_array_equal(results, new_results)
def test_create(self, geometry, case_no, kwargs): shape = geometry[case_no] body = Body(shape, **kwargs) assert body.shape == shape for k, v in kwargs.items(): assert body.options[k] == v assert body.material() == kwargs.get('MAT', None)
def test_simplify(self, geometry, surfaces, case_no, expected, kwarg): expected = [TestShape.filter_arg(a, surfaces) for a in expected] expected_shape = Shape.from_polish_notation(expected) body = Body(geometry[case_no], **kwarg) gb = Box([3, 0, 0], 26, 20, 20) simple_body = body.simplify(min_volume=0.001, box=gb) assert simple_body.shape == expected_shape for k, v in kwarg.items(): assert simple_body.options[k] == v assert simple_body.material() == kwarg.get('MAT', None)
def create_cell( cell_no: int, geometry: List, material: Optional[Material] = None, transformation: Optional[Transformation] = None, **options, ): if not options: options = dict() options["name"] = cell_no def convert_integers_to_surfaces(_geometry): for i, e in enumerate(_geometry): if isinstance(e, int): _geometry[i] = DummySurface(e) elif isinstance(e, list): convert_integers_to_surfaces(e) convert_integers_to_surfaces(geometry) if material: options["MAT"] = material if transformation: options["TRCL"] = transformation return Body(geometry, **options)
def cell(self, p): reference_body = self.cells[p[2]] options = filter_dict(reference_body.options, "original", "comment", "comment_above") options.update(p.attributes) options["name"] = p[0] new_body = Body(reference_body, **options) return self.build_cell(new_body, options)
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"])
def build_cell(self, geometry, options): if self.trailing_comments: options["comment"] = self.trailing_comments if self._original: options["original"] = self.original return Body(geometry, **options)
def test_create_polish(self, geometry, surfaces, case_no, polish): polish = [TestShape.filter_arg(a, surfaces) for a in polish] body = Body(polish) assert body.shape == geometry[case_no]
[ { "name": 3, "verbose_name": "verbose", "comment": "comment" }, {}, { "name": 4, "comment": ["1", "2"] }, ], ) @pytest.mark.parametrize( "cells", [[Body(_emp, name=2), Body(_emp, name=4)], [], [Body(_emp, name=5)]]) def test_init(cells, kwargs): u = Universe(cells, **kwargs) assert len(u._cells) == len(cells) for c1, c2 in zip(u._cells, cells): assert c1.name() == c2.name() assert c1.shape == c2.shape assert u.name() == kwargs.get("name", 0) assert u.verbose_name == kwargs.get("verbose_name", str(kwargs.get("name", 0))) assert u._comment == kwargs.get("comment", None) @pytest.mark.parametrize( "case, cells, name_rule, new_name, new_surfs, new_comps", [
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'])