Debian Codegraph @

Tests/Py/Functional/known_slicing.py

raw symbols SCIP
1 """
2 Check slicing mechanism for spherical particles crossing an interface,
3 using known decomposition of particles (as opposed to automatic slicing, tested elsewhere)
4 """
5
6 import unittest
7 import PyFuTestInfrastructure as infrastruct
8 import bornagain as ba
9 from bornagain import deg, R3
10
11 matSubstrate_color = (0.55, 0.55, 0.55)
12 matSubstrate = ba.RefractiveMaterial("Substrate", matSubstrate_color, 3.2e-6, 3.2e-8)
13 matVacuum = ba.Vacuum()
14 matParticle_color = (0.86, 0.24, 0.18)
15 matParticle = ba.RefractiveMaterial("Ag", matParticle_color, 1.2e-5, 5.4e-7)
16
17 R = 10.0
18 dz = 4.0 # shift beneath interface
19
20
21 def get_sample(particle_to_air=None, particle_to_substrate=None):
22 """
23 Returns a sample, with given particles attached to substrate or vacuum layer.
24 """
25
26 vacuum_layer = ba.Layer(matVacuum)
27 if particle_to_air:
28 vacuum_layer.deposit2D(ba.Dilute2D(0.001, particle_to_air))
29
30 substrate = ba.Layer(matSubstrate)
31 if particle_to_substrate:
32 substrate.deposit2D(ba.Dilute2D(0.001, particle_to_substrate))
33
34 sample = ba.Sample()
35 sample.addLayer(vacuum_layer)
36 sample.addLayer(substrate)
37 return sample
38
39
40 class SlicedSpheresTest(unittest.TestCase):
41
42 def testSphericalCupOnTopOfSubstrate(self):
43 print("""
44 Simulation #1: truncated sphere on top of substrate.
45 Simulation #2: sphere crossing the interface.
46 Both particles are made of same material as substrate.
47 Same scattering expected from both sample models.
48 """, flush=True)
49
50 # truncated sphere (dz removed from bottom) on top of substrate
51 SphericalSegment = ba.Particle(matSubstrate,
52 ba.SphericalSegment(R, 0, dz))
53
54 # same without truncation, sphere penetrating into substrate layer
55 sphere = ba.Particle(matSubstrate, ba.Sphere(R))
56 sphere.translate(0, 0, -dz)
57
58 d1 = infrastruct.run_simulation_MiniGISAS(get_sample(SphericalSegment))
59 d2 = infrastruct.run_simulation_MiniGISAS(get_sample(sphere))
60 self.assertTrue(ba.checkRelativeDifference(d1, d2, 1e-15))
61
62 def testSphericalLacuneInSubstrate(self):
63 print("""
64 Similar to previous. Truncated sphere and sphere are made of vacuum material.
65 From scattering point of view, both cases should look like an vacuum lacune in substrate.
66 """, flush=True)
67
68 # Sphere truncated from top. Intended to go below interface.
69 SphericalSegment = ba.Particle(matVacuum,
70 ba.SphericalSegment(R, dz, 0))
71 SphericalSegment.translate(0, 0, -dz)
72
73 # sphere crossing interface to look like truncated sphere above
74 sphere = ba.Particle(matVacuum, ba.Sphere(R))
75 sphere.translate(0, 0, -dz)
76
77 d1 = infrastruct.run_simulation_MiniGISAS(get_sample(SphericalSegment))
78 d2 = infrastruct.run_simulation_MiniGISAS(get_sample(sphere))
79 self.assertTrue(ba.checkRelativeDifference(d1, d2, 1e-15))
80
81 def testSpheresCrossingInterface(self):
82 print("""
83 Same particle in same position, but attached to two different layers.
84 Of course, results shall be identical.
85 """, flush=True)
86
87 # Sphere intended for vacuum layer and crossing interface
88 sphere1 = ba.Particle(matParticle, ba.Sphere(R))
89 sphere1.translate(0, 0, -dz)
90
91 # Sphere intended for substrate layer and crossing interface
92 sphere2 = ba.Particle(matParticle, ba.Sphere(R))
93 sphere2.translate(0, 0, -dz)
94
95 d1 = infrastruct.run_simulation_MiniGISAS(get_sample(sphere1))
96 d2 = infrastruct.run_simulation_MiniGISAS(get_sample(sphere2))
97 self.assertTrue(ba.checkRelativeDifference(d1, d2, 1e-14))
98
99
100 class SlicedSpheresTest(unittest.TestCase):
101
102 def get_compound(self, top_material, bottom_material):
103 """
104 Returns particle compound representing sphere made of two different materials.
105 Origin of new object is at the bottom of resulting sphere.
106 """
107
108 topCup = ba.Particle(top_material,
109 ba.SphericalSegment(R, 0, dz))
110 bottomCup = ba.Particle(bottom_material,
111 ba.SphericalSegment(R, R * 2 - dz, 0))
112
113 # origin of resulting sphere will be at the bottom
114 result = ba.Compound()
115 result.addComponent(topCup, R3(0, 0, dz))
116 result.addComponent(bottomCup, R3(0, 0, 0))
117
118 return result
119
120 def get_compound_v2(self, top_material, bottom_material):
121 """
122 Returns particle compound representing sphere made of two different materials.
123 Alternative to previous method.
124 Rotation is used to get bottom part
125 """
126
127 topCup = ba.Particle(top_material,
128 ba.SphericalSegment(R, 0, dz))
129 bottomCup = ba.Particle(bottom_material, ba.SphericalSegment(R, 0, R * 2 - dz))
130 bottomCup.rotate(ba.RotationX(180 * deg))
131
132 # origin of resulting sphere will be at the bottom
133 result = ba.Compound()
134 result.addComponent(topCup, R3(0, 0, dz))
135 result.addComponent(bottomCup, R3(0, 0, dz))
136
137 return result
138
139 def testCompound(self):
140 print("""
141 Compares two simulation intended to provide identical results.
142 Simulation #1: spherical particle on top of substrate
143 Simulation #2: spherical compound on top of substrate, where
144 top and bottom are made of same material.
145 """, flush=True)
146
147 sphere = ba.Particle(matParticle, ba.Sphere(R))
148 compound = self.get_compound(matParticle, matParticle)
149
150 d1 = infrastruct.run_simulation_MiniGISAS(get_sample(sphere))
151 d2 = infrastruct.run_simulation_MiniGISAS(get_sample(compound))
152 self.assertTrue(ba.checkRelativeDifference(d1, d2, 3e-14))
153
154 def testCompoundBuilds(self):
155 print("""
156 Compares two simulation intended to provide identical results.
157 Simulation #1: spherical particle compound on top of substrate
158 Simulation #2: same, but made using rotation
159 """, flush=True)
160
161 compound1 = self.get_compound(matParticle, matSubstrate)
162 compound2 = self.get_compound_v2(matParticle, matSubstrate)
163
164 d1 = infrastruct.run_simulation_MiniGISAS(get_sample(compound1))
165 d2 = infrastruct.run_simulation_MiniGISAS(get_sample(compound2))
166 self.assertTrue(ba.checkRelativeDifference(d1, d2, 7e-14))
167
168 # def testSlicedCompound(self):
169 # print("""
170 # Compares two simulation intended to provide identical results.
171 # Simulation #1: spherical particle on top of substrate.
172 # Simulation #2: spherical compound on top of substrate,
173 # where top and bottom are made of same material.
174 # Both particles are inserted in vacuum layer with shift to go below interface
175 # """, flush=True)
176
177 # shift = 3.0
178
179 # sphere = ba.Particle(matParticle, ba.Sphere(R))
180 # sphere.translate(0, 0, -shift)
181
182 # compound = self.get_compound(matParticle, matParticle)
183 # compound.translate(0, 0, -shift)
184
185 # d1 = infrastruct.run_simulation_MiniGISAS(get_sample(sphere))
186 # d2 = infrastruct.run_simulation_MiniGISAS(get_sample(compound))
187 # self.assertTrue(ba.checkRelativeDifference(d1, d2, 1e-14))
188
189 # def testSphericalCupOnTopOfSubstrate(self):
190 # print("""
191 # Compares two simulation intended to provide identical results.
192 # Simulation #1: truncated sphere on top of substrate.
193 # Simulation #2: spherical particle compound crossing the interface.
194 # Bottom part of compound is made from substrate material.
195 # both cases should look like a truncated sphere on top of substrate.
196 # """, flush=True)
197
198 # # truncated sphere on top of substrate with height 16nm
199 # SphericalSegment = ba.Particle(matParticle,
200 # ba.SphericalSegment(R, 0, dz))
201
202 # # Particle compound, top part made of same material, as particle.
203 # # Bottom part made of same material as substrate.
204 # compound = self.get_compound(matParticle, matSubstrate)
205 # compound.translate(0, 0, -dz)
206
207 # d1 = infrastruct.run_simulation_MiniGISAS(get_sample(SphericalSegment))
208 # d2 = infrastruct.run_simulation_MiniGISAS(get_sample(compound))
209 # self.assertTrue(ba.checkRelativeDifference(d1, d2, 1e-15))
210
211
212 if __name__ == '__main__':
213 unittest.main()