Exemple #1
0
 def build_transformation(translation, rotation, in_degrees, inverted):
     return Transformation(
         translation=translation,
         rotation=rotation,
         indegrees=in_degrees,
         inverted=inverted,
     )
Exemple #2
0
 def transformation(self, p):
     name, in_degrees = p.NAME
     translation, rotation, inverted = p.transform_params
     return Transformation(
         translation=translation,
         rotation=rotation,
         indegrees=in_degrees,
         inverted=inverted,
         name=name,
     )
Exemple #3
0
 def test_replace_surf(self, geometry, case_no, replace_names):
     surfs = {s.name(): s for s in geometry[case_no].get_surfaces()}
     replace_dict = {}
     for name in replace_names:
         s = surfs[name]
         replace_dict[s] = s.transform(Transformation())
     new_shape = geometry[case_no].replace_surfaces(replace_dict)
     new_surfs = new_shape.get_surfaces()
     ids = {id(s) for s in new_surfs}
     ids_ans = {id(s) if n not in replace_names else id(replace_dict[s]) for n, s in surfs.items()}
     assert ids == ids_ans
Exemple #4
0
import numpy as np
import pytest

from mckit import read_meshtal
from mckit.body import Body
from mckit.fmesh import CylMesh, RectMesh
from mckit.geometry import EX, EY, EZ
from mckit.material import Material
from mckit.surface import create_surface
from mckit.transformation import Transformation
from mckit.utils.resource import filename_resolver

transforms = [
    None,
    Transformation(translation=[2, -1, 3]),
    Transformation(
        translation=[1, 2, -3],
        indegrees=True,
        rotation=[30, 60, 90, 120, 30, 90, 90, 90, 0],
    ),
    Transformation(
        translation=[1, 0, 0],
        rotation=[90, 0, 90, 180, 90, 90, 90, 90, 0],
        indegrees=True,
    ),
]

bins = [
    {
        "xbins": [0, 1, 2, 4, 6],
Exemple #5
0
    ),
])
def test_transformation_lexer(text, expected_types, expected_values):
    lexer = trp.Lexer()
    tokens = [t for t in lexer.tokenize(text)]
    actual_types = [t.type for t in tokens]
    assert actual_types == expected_types
    actual_values = [t.value for t in tokens]
    assert actual_values == expected_values


# noinspection PyTypeChecker
@pytest.mark.parametrize("text,expected", [
    (
        "tr2 0 0 1",
        Transformation(translation=[0., 0., 1.], name=2),
    ),
    (
        " *tr2 0 0 1 45 45 90 135 45 90 90 90 0",
        Transformation(
            translation=[0., 0., 1.],
            rotation=[45, 45, 90, 135, 45, 90, 90, 90, 0],
            indegrees=True,
            name=2,
        ),
    ),
])
def test_transformation_parser(text, expected):
    actual = trp.parse(text)
    assert actual == expected
Exemple #6
0
def trtr():
    tr = [Transformation(**tdata) for tdata in tr_tr_cases]
    return tr
Exemple #7
0
def transforms():
    return Transformation(
        translation=[1, 2, -3],
        indegrees=True,
        rotation=[30, 60, 90, 120, 30, 90, 90, 90, 0],
    )
Exemple #8
0
def test_orthogonalization(args):
    tr = Transformation(**args)
    result = np.dot(tr._u.transpose(), tr._u)
    np.testing.assert_array_almost_equal(result, IDENTITY_ROTATION)
Exemple #9
0
def test_name(args, options, answer):
    tr = Transformation(**args, **options)
    assert tr.name() == answer
Exemple #10
0
def test_words(args, answer):
    tr = Transformation(**args)
    words = [float(w) for w in "".join(tr.get_words()).split()]
    np.testing.assert_array_almost_equal(words, answer)
Exemple #11
0
def test_creation_failure(args):
    with pytest.raises(ValueError):
        Transformation(**args)
Exemple #12
0
def test_creation(args, rot, offset, options):
    tr = Transformation(**args)
    np.testing.assert_array_almost_equal(tr._u, rot)
    np.testing.assert_array_almost_equal(tr._t, offset)
    for k, v in options.items():
        assert tr[k] == v
Exemple #13
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
Exemple #14
0
        ),
    ],
)
def test_get_compositions(universe, case, answer):
    u = universe(case)
    comps = u.get_compositions()
    assert comps == answer
    names_ans = {c.name() for c in answer}
    names = {c.name() for c in comps}
    assert names == names_ans


@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],
    ],
)
def test_transformation_lexer(text, expected_types, expected_values):
    lexer = trp.Lexer()
    tokens = [t for t in lexer.tokenize(text)]
    actual_types = [t.type for t in tokens]
    assert actual_types == expected_types
    actual_values = [t.value for t in tokens]
    assert actual_values == expected_values


# noinspection PyTypeChecker
@pytest.mark.parametrize(
    "text,expected",
    [
        ("tr2 0 0 1", Transformation(translation=[0.0, 0.0, 1.0], name=2)),
        (
            " *tr2 0 0 1 45 45 90 135 45 90 90 90 0",
            Transformation(
                translation=[0.0, 0.0, 1.0],
                rotation=[45, 45, 90, 135, 45, 90, 90, 90, 0],
                indegrees=True,
                name=2,
            ),
        ),
        (
            "*tr1 0. 0. 0. 3.62 86.38 90. 93.62 3.62 90. 90. 90. 0.",
            Transformation(
                translation=[0.0, 0.0, 0.0],
                rotation=[
                    3.62, 86.38, 90.0, 93.62, 3.62, 90.0, 90.0, 90.0, 0.0