Debian Codegraph @

Tests/Py/Functional/transform_cube.py

raw symbols SCIP
1 """
2 Test of rotation/positioning of a simple cubic particle.
3 The transformed particle is compared with the original one.
4 """
5
6 import unittest
7 import PyFuTestInfrastructure as infrastruct
8 from bornagain import *
9
10
11 class RotationsCubeTest(unittest.TestCase):
12 """
13 Test of rotations and translations of a simple cube in a 3-layer system
14 """
15
16 def get_sample(self,
17 formfactor,
18 rot=None,
19 pos=None,
20 layout_rot=None,
21 layout_pos=None,
22 add_to="Vacuum"):
23 mParticle = RefractiveMaterial("Particle", (0.86, 0.24, 0.18), 6e-4,
24 2e-8)
25 mMiddle = RefractiveMaterial("MiddleLayer", (0.70, 0.73, 0.78), 5e-5,
26 2e-8)
27 mSubstrate = RefractiveMaterial("Substrate", (0.55, 0.55, 0.55), 6e-6,
28 2e-8)
29
30 particle = Particle(mParticle, formfactor)
31 if rot:
32 particle.rotate(rot)
33 if pos:
34 particle.translate(pos)
35
36 if layout_rot:
37 particle.rotate(layout_rot)
38 if layout_pos:
39 particle.translate(layout_pos)
40
41 vacuum_layer = Layer(Vacuum())
42 middle_layer = Layer(mMiddle, 50)
43 substrate = Layer(mSubstrate)
44
45 if add_to == "Vacuum":
46 vacuum_layer.deposit2D(Dilute2D(0.001, particle))
47 else:
48 middle_layer.deposit2D(Dilute2D(0.001, particle))
49
50 sample = Sample()
51 sample.addLayer(vacuum_layer)
52 sample.addLayer(middle_layer)
53 sample.addLayer(substrate)
54 return sample
55
56 def get_result(self, data, add_to="Vacuum"):
57 ff = data[0]
58 rot = data[1]
59 pos = data[2]
60 layout_rot = data[3]
61 layout_pos = data[4]
62 sample = self.get_sample(ff, rot, pos, layout_rot, layout_pos, add_to)
63 simulation = infrastruct.get_simulation_MiniGISAS(sample)
64 simulation.options().setUseAvgMaterials(False)
65 return simulation.simulate()
66
67 def testRotationZ(self):
68 """
69 Cube is Z-rotated either through the particle or through the layout.
70 The result is compared with the unrotated cube.
71 """
72 box = Box(10, 10, 10)
73
74 data_to_test = [
75 # ff, rot, pos, layout_rot, layout_pos
76 (box, None, None, None, None), # reference
77 (box, RotationZ(90. * deg), None, None, None), # via particle
78 (box, RotationZ(-90. * deg), None, None, None),
79 (box, RotationZ(180. * deg), None, None, None),
80 (box, None, None, RotationZ(90. * deg), None), # via layout
81 (box, RotationZ(45. * deg), None, RotationZ(45. * deg),
82 None), # cumulative
83 ]
84
85 reference = self.get_result(data_to_test[0])
86 for i in range(1, len(data_to_test)):
87 simulated = self.get_result(data_to_test[i])
88 self.assertTrue(
89 checkRelativeDifference(simulated, reference, 1e-10))
90
91 def testRotationY(self):
92 """
93 Cube is Y-rotated either through the particle or through the layout.
94 Under the current alignment model deposit2D bottom-anchors the rotated
95 cube, so no compensating translation is needed; the result must match
96 the unrotated cube.
97 """
98 box = Box(10, 10, 10)
99
100 data_to_test = [
101 # ff rot pos layout_rot layout_pos
102 (box, None, None, None, None), # reference
103 (box, RotationY(90. * deg), None, None, None), # via particle
104 (box, None, None, RotationY(90. * deg), None), # via layout
105 (box, RotationY(45. * deg), None, RotationY(45. * deg),
106 None), # cumulative 90 deg
107 ]
108
109 reference = self.get_result(data_to_test[0])
110 for i in range(1, len(data_to_test)):
111 simulated = self.get_result(data_to_test[i])
112 self.assertTrue(
113 checkRelativeDifference(simulated, reference, 1e-10))
114
115 def testRotationX(self):
116 """
117 Cube is X-rotated either through the particle or through the layout.
118 Under the current alignment model deposit2D bottom-anchors the rotated
119 cube, so no compensating translation is needed; the result must match
120 the unrotated cube.
121 """
122 box = Box(10, 10, 10)
123
124 data_to_test = [
125 # ff rot pos layout_rot layout_pos
126 (box, None, None, None, None), # reference
127 (box, RotationX(90. * deg), None, None, None), # via particle
128 (box, None, None, RotationX(90. * deg), None), # via layout
129 (box, RotationX(45. * deg), None, RotationX(45. * deg),
130 None), # cumulative 90 deg
131 ]
132
133 reference = self.get_result(data_to_test[0])
134 for i in range(1, len(data_to_test)):
135 simulated = self.get_result(data_to_test[i])
136 self.assertTrue(
137 checkRelativeDifference(simulated, reference, 1e-10))
138
139 def testRotationsInMiddleLayer(self):
140 """
141 Cube deposited inside the middle layer, X-rotated about its reference
142 point. Bottom-anchoring keeps it at the same place as the unrotated
143 cube, so the two must scatter identically.
144 """
145 box = Box(10, 10, 10)
146
147 data_to_test = [
148 # ff rot pos layout_rot layout_pos
149 (box, None, R3(0, 0, 20), None, None), # reference
150 (box, RotationX(90. * deg), R3(0, 0, 20), None, None), # rotated
151 ]
152
153 reference = self.get_result(data_to_test[0], "Middle")
154 for i in range(1, len(data_to_test)):
155 simulated = self.get_result(data_to_test[i], "Middle")
156 self.assertTrue(
157 checkRelativeDifference(simulated, reference, 1e-10))
158
159
160 if __name__ == '__main__':
161 unittest.main()