| 1 |
""" |
| 2 |
BornAgain functional test. |
| 3 |
Test of rotation/position of particle composition. |
| 4 |
The composition consists of two boxes made of the same material. |
| 5 |
It is compared against reference single box made of the same material. |
| 6 |
Composition might be rotated to get reference shape. |
| 7 |
Both, reference box and composition are placed in the center of |
| 8 |
middle layer of 3 layers system. |
| 9 |
""" |
| 10 |
|
| 11 |
import unittest |
| 12 |
import PyFuTestInfrastructure as infrastruct |
| 13 |
from bornagain import * |
| 14 |
|
| 15 |
layer_thickness = 100.0 |
| 16 |
rotate_y_layer_thickness = 120.0 |
| 17 |
com_length = 50.0 |
| 18 |
com_width = 20.0 |
| 19 |
com_height = 10.0 |
| 20 |
particle_color = (0.86, 0.24, 0.18) |
| 21 |
particle_mat = RefractiveMaterial("Ag", particle_color, 1.245e-5, 5.419e-7) |
| 22 |
|
| 23 |
|
| 24 |
class TransformBoxCompositionTest(unittest.TestCase): |
| 25 |
|
| 26 |
def get_sample(self, particle, thickness=layer_thickness): |
| 27 |
mAmbience = Vacuum() |
| 28 |
mMiddle_color = (0.70, 0.73, 0.78) |
| 29 |
mMiddle = RefractiveMaterial("Teflon", mMiddle_color, 2.900e-6, 6.019e-9) |
| 30 |
mSubstrate_color = (0.55, 0.55, 0.55) |
| 31 |
mSubstrate = RefractiveMaterial( |
| 32 |
"Substrate", mSubstrate_color, 3.212e-6, 3.244e-8) |
| 33 |
|
| 34 |
vacuum_layer = Layer(mAmbience) |
| 35 |
middle_layer = Layer(mMiddle, thickness) |
| 36 |
middle_layer.deposit2D(Dilute2D(0.001, particle)) |
| 37 |
substrate = Layer(mSubstrate) |
| 38 |
|
| 39 |
sample = Sample() |
| 40 |
sample.addLayer(vacuum_layer) |
| 41 |
sample.addLayer(middle_layer) |
| 42 |
sample.addLayer(substrate) |
| 43 |
return sample |
| 44 |
|
| 45 |
def get_result(self, particle, thickness=layer_thickness): |
| 46 |
sample = self.get_sample(particle, thickness) |
| 47 |
simulation = infrastruct.get_simulation_MiniGISAS(sample) |
| 48 |
simulation.options().setUseAvgMaterials(False) |
| 49 |
return simulation.simulate() |
| 50 |
|
| 51 |
def test_BoxComposition(self): |
| 52 |
""" |
| 53 |
Compares simple box with the one composed from two smaller boxes |
| 54 |
of the same material to get same size |
| 55 |
""" |
| 56 |
|
| 57 |
length = 50.0 |
| 58 |
width = 20.0 |
| 59 |
height = 10.0 |
| 60 |
particle = Particle(particle_mat, Box(length, width, height)) |
| 61 |
particle.translate( |
| 62 |
R3(0, 0, -layer_thickness / 2.0 - height / 2)) |
| 63 |
|
| 64 |
reference = self.get_result(particle) |
| 65 |
|
| 66 |
|
| 67 |
box = Particle(particle_mat, |
| 68 |
Box(com_length / 2, com_width, com_height)) |
| 69 |
composition = Compound() |
| 70 |
|
| 71 |
composition.addComponent(box, R3(0, 0, 0)) |
| 72 |
composition.addComponent(box, R3(com_length / 2, 0, 0)) |
| 73 |
composition.translate( |
| 74 |
R3(0, 0, -layer_thickness / 2.0 - com_height / 2)) |
| 75 |
|
| 76 |
data = self.get_result(composition) |
| 77 |
|
| 78 |
self.assertTrue(checkRelativeDifference(data, reference, 1e-10)) |
| 79 |
|
| 80 |
def test_BoxCompositionRotateX(self): |
| 81 |
""" |
| 82 |
Compares simple box with the one composed from two smaller boxes |
| 83 |
of the same material to get same size. |
| 84 |
Composed box is rotated around X, to get same orientation as the original box. |
| 85 |
""" |
| 86 |
|
| 87 |
length = 50.0 |
| 88 |
width = 10.0 |
| 89 |
height = 20.0 |
| 90 |
particle = Particle(particle_mat, Box(length, width, height)) |
| 91 |
particle.translate( |
| 92 |
R3(0, 0, -layer_thickness / 2.0 - height / 2)) |
| 93 |
|
| 94 |
reference = self.get_result(particle) |
| 95 |
|
| 96 |
|
| 97 |
box = Particle(particle_mat, |
| 98 |
Box(com_length / 2, com_width, com_height)) |
| 99 |
composition = Compound() |
| 100 |
composition.addComponent(box, R3(0, 0, 0)) |
| 101 |
composition.addComponent(box, R3(com_length / 2, 0, 0)) |
| 102 |
composition.rotate(RotationX(90 * deg)) |
| 103 |
composition.translate(R3(0, 0, -layer_thickness / 2.)) |
| 104 |
|
| 105 |
data = self.get_result(composition) |
| 106 |
|
| 107 |
self.assertTrue(checkRelativeDifference(data, reference, 1e-10)) |
| 108 |
|
| 109 |
def test_BoxCompositionRotateY(self): |
| 110 |
""" |
| 111 |
Compares simple box with the one composed from two smaller boxes |
| 112 |
of the same material to get same size. |
| 113 |
Composed box is rotated around Y, to get same orientation as the original box. |
| 114 |
""" |
| 115 |
|
| 116 |
length = 10.0 |
| 117 |
width = 20.0 |
| 118 |
height = 50.0 |
| 119 |
particle = Particle(particle_mat, Box(length, width, height)) |
| 120 |
particle.translate( |
| 121 |
R3(0, 0, -rotate_y_layer_thickness / 2.0 - height / 2)) |
| 122 |
|
| 123 |
reference = self.get_result(particle, rotate_y_layer_thickness) |
| 124 |
|
| 125 |
|
| 126 |
box = Particle(particle_mat, |
| 127 |
Box(com_length / 2, com_width, com_height)) |
| 128 |
composition = Compound() |
| 129 |
composition.addComponent(box, R3(0, 0, 0)) |
| 130 |
composition.addComponent(box, R3(com_length / 2, 0, 0)) |
| 131 |
composition.rotate(RotationY(90 * deg)) |
| 132 |
composition.translate( |
| 133 |
R3(0, 0, -rotate_y_layer_thickness / 2. + com_length / 4.)) |
| 134 |
|
| 135 |
data = self.get_result(composition, rotate_y_layer_thickness) |
| 136 |
|
| 137 |
self.assertTrue(checkRelativeDifference(data, reference, 1e-10)) |
| 138 |
|
| 139 |
def test_BoxCompositionRotateZ(self): |
| 140 |
""" |
| 141 |
Compares simple box with the one composed from two smaller boxes |
| 142 |
of the same material to get same size. |
| 143 |
Composed box is rotated around Z, to get same orientation as the original box. |
| 144 |
""" |
| 145 |
|
| 146 |
length = 20.0 |
| 147 |
width = 50.0 |
| 148 |
height = 10.0 |
| 149 |
particle = Particle(particle_mat, Box(length, width, height)) |
| 150 |
particle.translate( |
| 151 |
R3(0, 0, -layer_thickness / 2.0 - height / 2)) |
| 152 |
|
| 153 |
reference = self.get_result(particle) |
| 154 |
|
| 155 |
|
| 156 |
box = Particle(particle_mat, |
| 157 |
Box(com_length / 2, com_width, com_height)) |
| 158 |
composition = Compound() |
| 159 |
composition.addComponent(box, R3(0, 0, 0)) |
| 160 |
composition.addComponent(box, R3(com_length / 2, 0, 0)) |
| 161 |
composition.rotate(RotationZ(90 * deg)) |
| 162 |
composition.translate( |
| 163 |
R3(0, 0, -layer_thickness / 2.0 - com_height / 2)) |
| 164 |
|
| 165 |
data = self.get_result(composition) |
| 166 |
|
| 167 |
self.assertTrue(checkRelativeDifference(data, reference, 1e-10)) |
| 168 |
|
| 169 |
def test_BoxCompositionRotateZandY(self): |
| 170 |
""" |
| 171 |
Compares simple box with the one composed from two smaller boxes |
| 172 |
of the same material to get same size. |
| 173 |
Composed box is rotated around Y, to get same orientation as the original box. |
| 174 |
""" |
| 175 |
|
| 176 |
length = 10.0 |
| 177 |
width = 50.0 |
| 178 |
height = 20.0 |
| 179 |
particle = Particle(particle_mat, Box(length, width, height)) |
| 180 |
particle.translate( |
| 181 |
R3(0, 0, -layer_thickness / 2.0 - height / 2)) |
| 182 |
|
| 183 |
reference = self.get_result(particle) |
| 184 |
|
| 185 |
|
| 186 |
box = Particle(particle_mat, |
| 187 |
Box(com_length / 2, com_width, com_height)) |
| 188 |
composition = Compound() |
| 189 |
composition.addComponent(box, R3(0, 0, 0)) |
| 190 |
composition.addComponent(box, R3(com_length / 2, 0, 0)) |
| 191 |
composition.rotate(RotationZ(90 * deg)) |
| 192 |
composition.rotate(RotationY(90 * deg)) |
| 193 |
composition.translate(R3(0, 0, -layer_thickness / 2.)) |
| 194 |
|
| 195 |
data = self.get_result(composition) |
| 196 |
|
| 197 |
self.assertTrue(checkRelativeDifference(data, reference, 1e-10)) |
| 198 |
|
| 199 |
def test_BoxStackComposition(self): |
| 200 |
""" |
| 201 |
2 different boxes are first rotated and then combined into the composition. |
| 202 |
Composition is then rotated. |
| 203 |
Composition is compared against reference box. |
| 204 |
""" |
| 205 |
|
| 206 |
length = 10.0 |
| 207 |
width = 20.0 |
| 208 |
height = 50.0 |
| 209 |
particle = Particle(particle_mat, Box(length, width, height)) |
| 210 |
particle.translate( |
| 211 |
R3(0, 0, -layer_thickness / 2.0 - height / 2)) |
| 212 |
reference = self.get_result(particle) |
| 213 |
|
| 214 |
|
| 215 |
composition = Compound() |
| 216 |
|
| 217 |
|
| 218 |
box1_length = 20.0 |
| 219 |
box1_width = 50.0 |
| 220 |
box1_height = 5.0 |
| 221 |
box1 = Particle(particle_mat, |
| 222 |
Box(box1_length, box1_width, box1_height)) |
| 223 |
box1.rotate(RotationZ(90 * deg)) |
| 224 |
|
| 225 |
|
| 226 |
box2_length = 5.0 |
| 227 |
box2_width = 20.0 |
| 228 |
box2_height = 50.0 |
| 229 |
box2 = Particle(particle_mat, |
| 230 |
Box(box2_length, box2_width, box2_height)) |
| 231 |
box2.rotate(RotationY(90 * deg)) |
| 232 |
box2.translate(R3(-box2_height / 2, 0, box2_length / 2)) |
| 233 |
|
| 234 |
composition.addComponent(box1, R3(0, 0, 0)) |
| 235 |
composition.addComponent(box2, R3(0, 0, box1_height)) |
| 236 |
composition.rotate(RotationY(90 * deg)) |
| 237 |
composition.translate(R3(0, 0, -layer_thickness / 2.)) |
| 238 |
|
| 239 |
data = self.get_result(composition) |
| 240 |
|
| 241 |
self.assertTrue(checkRelativeDifference(data, reference, 1e-10)) |
| 242 |
|
| 243 |
|
| 244 |
if __name__ == '__main__': |
| 245 |
unittest.main() |