コード例 #1
0
 def cell(self, p):
     geometry, options = p.cell_spec  # type: TGeometry, Optional[Dict[str, Any]]
     assert options is None or isinstance(options, dict)
     if options is None:
         options = {"name": p.INTEGER}
     else:
         options["name"] = p.INTEGER
     if p.cell_material is not None:
         composition_no, density = p.cell_material  # type: int, float
         composition = self.compositions[composition_no]
         if density > 0:
             material = Material(composition=composition,
                                 concentration=density * 1e24)
         else:
             material = Material(composition=composition, density=-density)
         options["MAT"] = material
     return self.build_cell(geometry, options)
コード例 #2
0
ファイル: test_fmesh.py プロジェクト: MC-kit/mckit
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"])
コード例 #3
0
ファイル: test_body.py プロジェクト: rorni/mckit
class TestBody:
    kwarg_data = [
        {'name': 1},
        {'name': 2, 'MAT': Material(atomic=[('C-12', 1)], density=3.5)},
        {'name': 3, 'U': 4},
        {'name': 4, 'U': 5, 'MAT': Material(atomic=[('C-12', 1)], density=2.7)}
    ]

    @pytest.mark.parametrize('case_no', range(len(basic_geoms)))
    @pytest.mark.parametrize('kwargs', kwarg_data)
    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)

    @pytest.mark.parametrize('case_no, polish', enumerate(TestShape.polish_cases))
    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]

    @pytest.mark.parametrize('kwargs', kwarg_data)
    @pytest.mark.parametrize('no1, no2',
        [(i, j) for i in range(len(basic_geoms)) \
                for j in range(len(basic_geoms)) if i != j]
    )
    def test_intersection(self, geometry, no1, no2, kwargs):
        body1 = Body(geometry[no1], **kwargs)
        body2 = Body(geometry[no2], name='1001')
        body = body1.intersection(body2)
        assert body.shape == body1.shape.intersection(body2.shape)
        for k, v in kwargs.items():
            assert body.options[k] == v

    @pytest.mark.parametrize('kwargs', kwarg_data)
    @pytest.mark.parametrize('no1, no2',
        [(i, j) for i in range(len(basic_geoms)) \
                for j in range(len(basic_geoms)) if i != j]
    )
    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

    @pytest.mark.slow
    @pytest.mark.parametrize('kwarg', kwarg_data)
    @pytest.mark.parametrize('case_no, expected', [
        (0, [2, 'C', 3, 'I', 1, 'I', 5, 'C', 'I', 4, 'C', 'U']),
        (1, [6, 'C']),
        (2, [1, 'C']),
        (3, [2, 'C', 3, 'I', 1, 'I', 5, 'C', 'I', 4, 'C', 'U']),
        (4, [5, 'C', 3, 'I', 2, 'C', 'I', 1, 'C', 'U']),
        (5, [3, 8, 'C', 'I', 5, 'C', 'I', 4, 'C', 'U', 6, 'C', 'U']),
        pytest.param(6, [4, 'C'], marks=pytest.mark.xfail(reason="need full simplification approach")),
        pytest.param(7, [4], marks=pytest.mark.xfail(reason="need full simplification approach")),
        (8, [Shape('E')]),
        (9, [Shape('E')]),
        (10, [Shape('E')]),
        (11, [Shape('R')])
    ])
    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)

    @pytest.mark.parametrize('fill_tr', [
        None,
        Transformation(translation=[2, -1, -0.5]),
        Transformation(translation=[1, 2, 3]),
        Transformation(translation=[-4, 2, -3]),
        Transformation(translation=[3, 0, 9],
                       rotation=[30, 60, 90, 120, 30, 90, 90, 90, 0],
                       indegrees=True),
        Transformation(translation=[1, 4, -2],
                       rotation=[0, 90, 90, 90, 30, 60, 90, 120, 30],
                       indegrees=True),
        Transformation(translation=[-2, 5, 3],
                       rotation=[30, 90, 60, 90, 0, 90, 120, 90, 30],
                       indegrees=True)
    ])
    @pytest.mark.parametrize('tr', [
        Transformation(translation=[-3, 2, 0.5]),
        Transformation(translation=[1, 2, 3]),
        Transformation(translation=[-4, 2, -3]),
        Transformation(translation=[3, 0, 9],
                       rotation=[30, 60, 90, 120, 30, 90, 90, 90, 0],
                       indegrees=True),
        Transformation(translation=[1, 4, -2],
                       rotation=[0, 90, 90, 90, 30, 60, 90, 120, 30],
                       indegrees=True),
        Transformation(translation=[-2, 5, 3],
                       rotation=[30, 90, 60, 90, 0, 90, 120, 90, 30],
                       indegrees=True)
    ])
    @pytest.mark.parametrize('case_no', range(len(basic_geoms)))
    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)

    @pytest.mark.skip
    def test_print(self):
        raise NotImplementedError

    @pytest.mark.skip
    def test_fill(self):
        raise NotImplementedError
コード例 #4
0
         Body(Shape("C", Sphere([0, 4, 0], 0.5, name=9)), name=9),
     ],
     "new",
     [5, 6],
     [Sphere([0, 3, 0], 0.5, name=6),
      Sphere([0, 4, 0], 0.5, name=7)],
     [],
 ),  # 15
 (
     1,
     Body(
         Shape("C", Sphere([0, 3, 0], 0.5, name=8)),
         name=7,
         MAT=Material(
             composition=Composition(
                 atomic=[("C-12", 1)], name=2, lib="31c"),
             density=2.0,
         ),
     ),
     "keep",
     [7],
     [Sphere([0, 3, 0], 0.5, name=8)],
     [],
 ),  # 16
 (
     1,
     Body(
         Shape("C", Sphere([0, 3, 0], 0.5, name=8)),
         name=7,
         MAT=Material(
             composition=Composition(
コード例 #5
0
ファイル: test_fmesh.py プロジェクト: rorni/mckit
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'])
コード例 #6
0
ファイル: material_creation.py プロジェクト: MC-kit/mckit
from mckit.material import Material

# for testing _get_material function purposes
materials = [
    Material(atomic=[(1000, 2), (8000, 1)], density=0.9982),
    Material(atomic=[(1000, 2), (8000, 1)], density=0.1),
]

densities = [
    (-0.9982, materials[0]),
    (-0.1, materials[1]),
    (-2.0, None),
    (-0.9984, None),
    (-0.9980, None),
    (1.0010337342849073e23, materials[0]),
    (1.0028388442044753e22, materials[1]),
    (1.0012337342849073e23, None),
    (1.0098337342849073e23, None),
    (-0.99825, materials[0]),
    (-0.99815, materials[0]),
]

compositions = {
    1: {
        "atomic": [(1000, 2), (8000, 1)]
    },
    2: {
        "atomic": [("Si", 1), ("O", 2)]
    },
    3: {
        "wgt": [("N", 75.5), ("O", 23.15), ("Ar", 1.292)]