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 163 164 165 166 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 213 214 215
|
"""
Check slicing mechanism for spherical particles crossing an interface,
using known decomposition of particles (as opposed to automatic slicing, tested elsewhere)
"""
import unittest
import PyFuTestInfrastructure as infrastruct
import bornagain as ba
from bornagain import deg, R3
matSubstrate = ba.RefractiveMaterial("Substrate", 3.2e-6, 3.2e-8)
matVacuum = ba.RefractiveMaterial("Vacuum", 0, 0)
matParticle = ba.RefractiveMaterial("Ag", 1.2e-5, 5.4e-7)
R = 10.0
dz = 4.0 # shift beneath interface
def get_sample(particle_to_air=None, particle_to_substrate=None):
"""
Returns a sample, with given particles attached to substrate or vacuum layer.
"""
vacuum_layer = ba.Layer(matVacuum)
if particle_to_air:
layout = ba.ParticleLayout()
layout.addParticle(particle_to_air)
vacuum_layer.addLayout(layout)
substrate = ba.Layer(matSubstrate)
if particle_to_substrate:
layout = ba.ParticleLayout()
layout.addParticle(particle_to_substrate)
substrate.addLayout(layout)
sample = ba.Sample()
sample.addLayer(vacuum_layer)
sample.addLayer(substrate)
return sample
class SlicedSpheresTest(unittest.TestCase):
def testSphericalCupOnTopOfSubstrate(self):
print("""
Simulation #1: truncated sphere on top of substrate.
Simulation #2: sphere crossing the interface.
Both particles are made of same material as substrate.
Same scattering expected from both sample models.
""", flush=True)
# truncated sphere (dz removed from bottom) on top of substrate
SphericalSegment = ba.Particle(matSubstrate,
ba.SphericalSegment(R, 0, dz))
# same without truncation, sphere penetrating into substrate layer
sphere = ba.Particle(matSubstrate, ba.Sphere(R))
sphere.translate(0, 0, -dz)
d1 = infrastruct.run_simulation_MiniGISAS(get_sample(SphericalSegment))
d2 = infrastruct.run_simulation_MiniGISAS(get_sample(sphere))
self.assertTrue(ba.checkRelativeDifference(d1, d2, 1e-15))
def testSphericalLacuneInSubstrate(self):
print("""
Similar to previous. Truncated sphere and sphere are made of vacuum material.
From scattering point of view, both cases should look like an vacuum lacune in substrate.
""", flush=True)
# Sphere truncated from top. Intended to go below interface.
SphericalSegment = ba.Particle(matVacuum,
ba.SphericalSegment(R, dz, 0))
SphericalSegment.translate(0, 0, -dz)
# sphere crossing interface to look like truncated sphere above
sphere = ba.Particle(matVacuum, ba.Sphere(R))
sphere.translate(0, 0, -dz)
d1 = infrastruct.run_simulation_MiniGISAS(get_sample(SphericalSegment))
d2 = infrastruct.run_simulation_MiniGISAS(get_sample(sphere))
self.assertTrue(ba.checkRelativeDifference(d1, d2, 1e-15))
def testSpheresCrossingInterface(self):
print("""
Same particle in same position, but attached to two different layers.
Of course, results shall be identical.
""", flush=True)
# Sphere intended for vacuum layer and crossing interface
sphere1 = ba.Particle(matParticle, ba.Sphere(R))
sphere1.translate(0, 0, -dz)
# Sphere intended for substrate layer and crossing interface
sphere2 = ba.Particle(matParticle, ba.Sphere(R))
sphere2.translate(0, 0, -dz)
d1 = infrastruct.run_simulation_MiniGISAS(get_sample(sphere1))
d2 = infrastruct.run_simulation_MiniGISAS(get_sample(sphere2))
self.assertTrue(ba.checkRelativeDifference(d1, d2, 1e-14))
class SlicedSpheresTest(unittest.TestCase):
def get_compound(self, top_material, bottom_material):
"""
Returns particle compound representing sphere made of two different materials.
Origin of new object is at the bottom of resulting sphere.
"""
topCup = ba.Particle(top_material,
ba.SphericalSegment(R, 0, dz))
bottomCup = ba.Particle(bottom_material,
ba.SphericalSegment(R, R * 2 - dz, 0))
# origin of resulting sphere will be at the bottom
result = ba.Compound()
result.addComponent(topCup, R3(0, 0, dz))
result.addComponent(bottomCup, R3(0, 0, 0))
return result
def get_compound_v2(self, top_material, bottom_material):
"""
Returns particle compound representing sphere made of two different materials.
Alternative to previous method.
Rotation is used to get bottom part
"""
topCup = ba.Particle(top_material,
ba.SphericalSegment(R, 0, dz))
bottomCup = ba.Particle(bottom_material, ba.SphericalSegment(R, 0, R * 2 - dz))
bottomCup.rotate(ba.RotationX(180 * deg))
# origin of resulting sphere will be at the bottom
result = ba.Compound()
result.addComponent(topCup, R3(0, 0, dz))
result.addComponent(bottomCup, R3(0, 0, dz))
return result
def testCompound(self):
print("""
Compares two simulation intended to provide identical results.
Simulation #1: spherical particle on top of substrate
Simulation #2: spherical compound on top of substrate, where
top and bottom are made of same material.
""", flush=True)
sphere = ba.Particle(matParticle, ba.Sphere(R))
compound = self.get_compound(matParticle, matParticle)
d1 = infrastruct.run_simulation_MiniGISAS(get_sample(sphere))
d2 = infrastruct.run_simulation_MiniGISAS(get_sample(compound))
self.assertTrue(ba.checkRelativeDifference(d1, d2, 2e-14))
def testCompoundBuilds(self):
print("""
Compares two simulation intended to provide identical results.
Simulation #1: spherical particle compound on top of substrate
Simulation #2: same, but made using rotation
""", flush=True)
compound1 = self.get_compound(matParticle, matSubstrate)
compound2 = self.get_compound_v2(matParticle, matSubstrate)
d1 = infrastruct.run_simulation_MiniGISAS(get_sample(compound1))
d2 = infrastruct.run_simulation_MiniGISAS(get_sample(compound2))
self.assertTrue(ba.checkRelativeDifference(d1, d2, 1e-14))
# def testSlicedCompound(self):
# print("""
# Compares two simulation intended to provide identical results.
# Simulation #1: spherical particle on top of substrate.
# Simulation #2: spherical compound on top of substrate,
# where top and bottom are made of same material.
# Both particles are inserted in vacuum layer with shift to go below interface
# """, flush=True)
# shift = 3.0
# sphere = ba.Particle(matParticle, ba.Sphere(R))
# sphere.translate(0, 0, -shift)
# compound = self.get_compound(matParticle, matParticle)
# compound.translate(0, 0, -shift)
# d1 = infrastruct.run_simulation_MiniGISAS(get_sample(sphere))
# d2 = infrastruct.run_simulation_MiniGISAS(get_sample(compound))
# self.assertTrue(ba.checkRelativeDifference(d1, d2, 1e-14))
# def testSphericalCupOnTopOfSubstrate(self):
# print("""
# Compares two simulation intended to provide identical results.
# Simulation #1: truncated sphere on top of substrate.
# Simulation #2: spherical particle compound crossing the interface.
# Bottom part of compound is made from substrate material.
# both cases should look like a truncated sphere on top of substrate.
# """, flush=True)
# # truncated sphere on top of substrate with height 16nm
# SphericalSegment = ba.Particle(matParticle,
# ba.SphericalSegment(R, 0, dz))
# # Particle compound, top part made of same material, as particle.
# # Bottom part made of same material as substrate.
# compound = self.get_compound(matParticle, matSubstrate)
# compound.translate(0, 0, -dz)
# d1 = infrastruct.run_simulation_MiniGISAS(get_sample(SphericalSegment))
# d2 = infrastruct.run_simulation_MiniGISAS(get_sample(compound))
# self.assertTrue(ba.checkRelativeDifference(d1, d2, 1e-15))
if __name__ == '__main__':
unittest.main()
|