Esempio n. 1
0
 def factor(self, p):
     item: int = p.integer
     if 0 < item:
         opc = "S"
     else:
         opc = "C"
         item = -item
     surface: Surface = self.surfaces[item]
     return [Shape(opc, surface)]
Esempio n. 2
0
def create_node(kind, args, surfs):
    new_args = []
    for g in args:
        if isinstance(g, tuple):
            g = create_node(g[0], g[1], surfs)
        else:
            g = surfs[g]
        new_args.append(g)
    return Shape(kind, *new_args)
Esempio n. 3
0
 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)
Esempio n. 4
0
 def test_pickle(self, surfaces, case_no, polish):
     polish = [self.filter_arg(a, surfaces) for a in polish]
     g = Shape.from_polish_notation(polish)
     # The idea is to generate many random points. This points have some
     # definite test results with respect to the body being tested.
     # After body saving and loading 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])
     results = g.test_points(points)
     with open('test.pic', 'bw') as f:
         pickle.dump(g, f, pickle.HIGHEST_PROTOCOL)
     with open('test.pic', 'br') as f:
         g_pic = pickle.load(f)
     result_pic = g_pic.test_points(points)
     np.testing.assert_array_equal(results, result_pic)
Esempio n. 5
0
 def factor(self, p):
     body = self.cells[p.integer]
     return [Shape("C", body)]
Esempio n. 6
0
 def factor(self, p):
     surface = self.surfaces[p.integer]
     return [Shape("S", surface)]
Esempio n. 7
0
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
Esempio n. 8
0
 def test_union(self, geometry, surfaces, no1, no2, opc, args):
     expected = create_node(opc, args, surfaces)
     result = Shape.union(geometry[no1], geometry[no2])
     assert result == expected
Esempio n. 9
0
 def test_from_polish(self, geometry, surfaces, case_no, polish):
     polish = [self.filter_arg(a, surfaces) for a in polish]
     shape = Shape.from_polish_notation(polish)
     assert shape == geometry[case_no]
Esempio n. 10
0
 def test_create_failure(self, surfaces, opc, args):
     args = [self.filter_arg(a, surfaces) for a in args]
     with pytest.raises(ValueError):
         Shape(opc, *args)
Esempio n. 11
0
 def test_create(self, surfaces, opc, args, ans_opc, ans_args):
     args = [self.filter_arg(a, surfaces) for a in args]
     ans_args = sorted([self.filter_arg(a, surfaces) for a in ans_args], key=hash)
     shape = Shape(opc, *args)
     assert shape.opc == ans_opc
     assert shape.args == tuple(ans_args)
Esempio n. 12
0
@pytest.mark.parametrize(
    "case, points, answer",
    [(
        1,
        [[-2, 0, 4], [10, 10, -10], [0, 0, 0], [0, 0, -2.5], [0, 1, 0]],
        [1, 3, 0, 2, 0],
    )],
)
def test_points(universe, case, points, answer):
    u = universe(case)
    result = u.test_points(points)
    np.testing.assert_array_equal(result, answer)


_emp = Shape("R")


@pytest.mark.parametrize(
    "kwargs",
    [
        {
            "name": 3,
            "verbose_name": "verbose",
            "comment": "comment"
        },
        {},
        {
            "name": 4,
            "comment": ["1", "2"]
        },