| 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 |
| 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 |
|
| 51 |
SphericalSegment = ba.Particle(matSubstrate, |
| 52 |
ba.SphericalSegment(R, 0, dz)) |
| 53 |
|
| 54 |
|
| 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 |
|
| 69 |
SphericalSegment = ba.Particle(matVacuum, |
| 70 |
ba.SphericalSegment(R, dz, 0)) |
| 71 |
SphericalSegment.translate(0, 0, -dz) |
| 72 |
|
| 73 |
|
| 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 |
|
| 88 |
sphere1 = ba.Particle(matParticle, ba.Sphere(R)) |
| 89 |
sphere1.translate(0, 0, -dz) |
| 90 |
|
| 91 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
if __name__ == '__main__': |
| 213 |
unittest.main() |