File: transform_cube.py

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (162 lines) | stat: -rw-r--r-- 5,854 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""
Test of rotation/positioning of simple cubic particle.
Original particle is compared with the one obtained.
"""

import unittest
import PyFuTestInfrastructure as infrastruct
from bornagain import *


class RotationsCubeTest(unittest.TestCase):
    """
    Test of rotations and translations of simple cube in three layers system
    """

    def get_sample(self,
                   formfactor,
                   rot=None,
                   pos=None,
                   layout_rot=None,
                   layout_pos=None,
                   add_to="Vacuum"):
        mParticle = RefractiveMaterial("Particle", 6e-4, 2e-8)
        mMiddle = RefractiveMaterial("MidleLayer", 5e-5, 2e-8)
        mSubstrate = RefractiveMaterial("Substrate", 6e-6, 2e-8)

        particle = Particle(mParticle, formfactor)
        if rot:
            particle.rotate(rot)
        if pos:
            particle.translate(pos)

        if layout_rot:
            particle.rotate(layout_rot)
        if layout_pos:
            particle.translate(layout_pos)

        layout = ParticleLayout()
        layout.addParticle(particle)

        vacuum_layer = Layer(Vacuum())
        middle_layer = Layer(mMiddle, 50)
        substrate = Layer(mSubstrate)

        if add_to == "Vacuum":
            vacuum_layer.addLayout(layout)
        else:
            middle_layer.addLayout(layout)

        sample = Sample()
        sample.addLayer(vacuum_layer)
        sample.addLayer(middle_layer)
        sample.addLayer(substrate)
        return sample

    def get_result(self, data, add_to="Vacuum"):
        ff = data[0]
        rot = data[1]
        pos = data[2]
        layout_rot = data[3]
        layout_pos = data[4]
        sample = self.get_sample(ff, rot, pos, layout_rot, layout_pos, add_to)
        # simulation = self.get_simulation(sample)
        simulation = infrastruct.get_simulation_MiniGISAS(sample)
        return simulation.simulate()

    def testRotationZ(self):
        """
        Cube is Z-rotated either through setRotation method or through particle layout.
        The result is compared with unrotated cube.
        """
        box = Box(10, 10, 10)

        data_to_test = [
            # ff  rot                     pos                    layout_rot              layout_pos
            (box, None, None, None, None),  # reference
            (box, RotationZ(90. * deg), None, None, None),  # rotating particle
            (box, RotationZ(-90. * deg), None, None, None),
            (box, RotationZ(180. * deg), None, None, None),
            (box, None, None, RotationZ(90. * deg),
             None),  # rotating through layout
            (box, RotationZ(45. * deg), None, RotationZ(45. * deg),
             None),  # cumulative rotation
        ]

        reference = self.get_result(data_to_test[0])
        for i in range(1, len(data_to_test)):
            simulated = self.get_result(data_to_test[i])
            self.assertTrue(checkRelativeDifference(simulated, reference, 1e-10))

    def testRotationY(self):
        """
        Cube is Y-rotated either through setRotation method or through particle layout.
        Additional translation is applied if necessary.
        The result is compared with unrotated cube.
        """
        box = Box(10, 10, 10)

        data_to_test = [
            # ff rot pos layout_rot layout_pos
            (box, None, None, None, None),  # reference
            (box, RotationY(90. * deg), R3(0, 0, 5), None,
             None),  # rotating and translating
            (box, None, None, RotationY(90. * deg),
             R3(0, 0, 5)),  # rotating and translating
            (box, RotationY(90. * deg), None, None,
             R3(0, 0, 5)),  # rotating and translating
            (box, RotationY(45. * deg), R3(0, 0, 0), RotationY(45. * deg),
             R3(0, 0, 5)),  # rotating and translating
        ]

        reference = self.get_result(data_to_test[0])
        for i in range(1, len(data_to_test)):
            simulated = self.get_result(data_to_test[i])
            self.assertTrue(checkRelativeDifference(simulated, reference, 1e-10))

    def testRotationX(self):
        """
        Cube is Z-rotated either through setRotation method or through particle layout.
        Additional translation is applied if necessary.
        The result is compared with unrotated cube.
        """
        box = Box(10, 10, 10)

        data_to_test = [
            # ff rot pos layout_rot layout_pos
            (box, None, None, None, None),  # reference
            (box, RotationX(90. * deg), R3(0, 0, 5), None,
             None),  # rotating and translating
            (box, None, None, RotationX(90. * deg),
             R3(0, 0, 5)),  # rotating and translating
            (box, RotationX(90. * deg), None, None,
             R3(0, 0, 5)),  # rotating and translating
            (box, RotationX(45. * deg), R3(0, 0, 0), RotationX(45. * deg),
             R3(0, 0, 5)),  # rotating and translating
        ]

        reference = self.get_result(data_to_test[0])
        for i in range(1, len(data_to_test)):
            simulated = self.get_result(data_to_test[i])
            self.assertTrue(checkRelativeDifference(simulated, reference, 1e-10))

    def testRotationsInMiddleLayer(self):
        """
        """
        box = Box(10, 10, 10)

        data_to_test = [
            # ff rot pos layout_rot layout_pos
            (box, None, R3(0, 0, -25), None, None),  # reference
            (box, RotationX(90. * deg), R3(0, 0, -20), None,
             None),  # rotating and translating
        ]

        reference = self.get_result(data_to_test[0])
        for i in range(1, len(data_to_test)):
            simulated = self.get_result(data_to_test[i])
            self.assertTrue(checkRelativeDifference(simulated, reference, 1e-10))


if __name__ == '__main__':
    unittest.main()