def setUp(self): self.q_values = np.logspace(-2.1, -.6, 100) L=4.75 dL=0.0475 dT=0.01 xs = [NeutronProbe(T=self.q_values, dT=dT, L=L, dL=dL) for _ in range(4)] probe = PolarizedNeutronProbe(xs) # Note: I don't use the usual shorthand to define the sample on purpose, # so I see explicitly what I'm using. # Note: 'MagneticSlab' is deprecated. Using magnetism=... instead. sample = ( Slab(material=SLD(name='Si', rho=2.07, irho=0.0)) | Slab(material=SLD(name='Cu', rho=6.5, irho=0.0), thickness=130, interface=15) | Slab(SLD(name='Stuff', rho=4.0, irho=0.0), thickness=50.0, interface=1.0, magnetism=Magnetism(rhoM=0.2, thetaM=270)) | Slab(material=SLD(name='air', rho=0, irho=0.0)) ) sample['Cu'].thickness.range(90.0, 200.0) probe.intensity = Parameter(value=1.0, name='normalization') probe.background = Parameter(value=0.0, name='background') self.expt = Experiment(probe=probe, sample=sample) # Reference self.expt.simulate_data(noise=1e-11) self.r_i = self.expt.probe.pp.R[0] self.r_f = self.expt.probe.pp.R[-1]
def test_save_json(self): """ Check that we can produce a json file """ q_values = np.logspace(-2.1, -.7, 100) # Fake data array zeros = np.zeros(len(q_values)) ones = np.ones(len(q_values)) # Q-resolution array q_resolution = q_values * 0.02 + 0.0001 probe = QProbe(q_values, q_resolution, data=(zeros, ones)) # Note: I don't use the usual shorthand to define the sample on purpose, so see # explicitly what I'm using. sample = Slab(material=SLD(name='Si', rho=2.07, irho=0.0)) \ | Slab(material=SLD(name='Cu', rho=6.5, irho=0.0), thickness=130, interface=15) \ | Slab(material=SLD(name='air', rho=0, irho=0.0)) sample['Cu'].thickness.range(90.0, 200.0) probe.intensity = Parameter(value=1.0, name='normalization') probe.background = Parameter(value=0.0, name='background') expt = Experiment(probe=probe, sample=sample) expt.save('output') self.assertTrue(os.path.isfile('output-expt.json'))
def setUp(self): self.q_values = np.logspace(-2.1, -.6, 100) L=4.75 dL=0.0475 dT=0.01 probe = NeutronProbe(T=self.q_values, dT=dT, L=L, dL=dL) # Note: I don't use the usual shorthand to define the sample on purpose, # so I see explicitly what I'm using. sample = ( Slab(material=SLD(name='Si', rho=2.07, irho=0.0)) | Slab(material=SLD(name='Cu', rho=6.5, irho=0.0), thickness=130, interface=15) | Slab(material=SLD(name='air', rho=0, irho=0.0)) ) sample['Cu'].thickness.range(90.0, 200.0) probe.intensity = Parameter(value=1.0, name='normalization') probe.background = Parameter(value=0.0, name='background') self.expt = Experiment(probe=probe, sample=sample) # Reference self.expt.simulate_data(noise=1e-11) self.r_i = self.expt.probe.R[0] self.r_f = self.expt.probe.R[-1]
def calculate_reflectivity(self, model_description, q, q_resolution=0.025): """ Reflectivity calculation using refl1d :param model_description: dict that holds parameters for the theoretical Refl1D model. Example dict for paramters: { 'back_sld': 2.07, 'back_roughness': 1.0, 'front_sld': 0, 'scale': 1, 'background': 0 'layers': [{ 'thickness': 10, 'sld': 3.5, 'isld': 0, 'roughness': 2}] } :param q: Momentum transfer (Q) to use to calculate the model :param q_resolution: Momentum transfer resolution to multiply by Q :return: Calculate reflectivity of the theoretical model """ from refl1d.names import \ Experiment, \ Parameter, \ QProbe, \ Slab, \ SLD zeros = np.zeros(len(q)) dq = q_resolution * q # The QProbe object represents the beam probe = QProbe(q, dq, data=(zeros, zeros)) sample = Slab(material=SLD(name='back', rho=model_description['back_sld']), interface=model_description['back_roughness']) # Add each layer for i, layer in enumerate(model_description['layers']): sample = sample | Slab(material=SLD( name='layer%s' % i, rho=layer['sld'], irho=layer['isld']), thickness=layer['thickness'], interface=layer['roughness']) sample = sample | Slab( material=SLD(name='front', rho=model_description['front_sld'])) probe.background = Parameter(value=model_description['background'], name='background') expt = Experiment(probe=probe, sample=sample) q, r = expt.reflectivity() return model_description['scale'] * r
def resolution_test(slabs, data): stk = Stack() for i, slab in enumerate(slabs[::-1]): m = SLD(f"layer {i}", rho=slab[1], irho=slab[2]) stk |= m(thickness=slab[0], interface=slab[-1]) probe = QProbe(Q=data[:, 0], dQ=data[:, 3]) probe.oversample(21, seed=1) try: M = Experiment(stk, probe) _, R = M.reflectivity() np.testing.assert_allclose(R, data[:, 1], rtol=0.033) except AssertionError: # Probe oversampling did not work. # make our own oversampling with a linearly spaced array warnings.warn( "QProbe oversampling didn't work. Trying linearly spaced points", RuntimeWarning, ) argmin = np.argmin(data[:, 0]) argmax = np.argmax(data[:, 0]) probe.calc_Qo = np.linspace( data[argmin, 0] - 3.5 * data[argmin, 3], data[argmax, 0] + 3.5 * data[argmax, 3], 21 * len(data), ) M = Experiment(stk, probe) _, R = M.reflectivity() np.testing.assert_allclose(R, data[:, 1], rtol=0.033)
class ExperimentNonMagSimulateTest(unittest.TestCase): """ Test the simulation functionality """ def setUp(self): self.q_values = np.logspace(-2.1, -.6, 100) L=4.75 dL=0.0475 dT=0.01 probe = NeutronProbe(T=self.q_values, dT=dT, L=L, dL=dL) # Note: I don't use the usual shorthand to define the sample on purpose, # so I see explicitly what I'm using. sample = ( Slab(material=SLD(name='Si', rho=2.07, irho=0.0)) | Slab(material=SLD(name='Cu', rho=6.5, irho=0.0), thickness=130, interface=15) | Slab(material=SLD(name='air', rho=0, irho=0.0)) ) sample['Cu'].thickness.range(90.0, 200.0) probe.intensity = Parameter(value=1.0, name='normalization') probe.background = Parameter(value=0.0, name='background') self.expt = Experiment(probe=probe, sample=sample) # Reference self.expt.simulate_data(noise=1e-11) self.r_i = self.expt.probe.R[0] self.r_f = self.expt.probe.R[-1] def test_noise_scalar_non_mag(self): """ Provide a scalar of dR with a non-magnetic sample """ self.expt.simulate_data(noise=2.5) self.assertEqual(len(self.expt.probe.dR), len(self.q_values)) ratio_i = self.expt.probe.dR[0] / self.r_i ratio_f = self.expt.probe.dR[-1] / self.r_f self.assertAlmostEqual(ratio_i, 0.025) self.assertAlmostEqual(ratio_f, 0.025) def test_noise_array_non_mag(self): """ Provide an array of dR with a non-magnetic sample """ m = 10.0 / (self.q_values[-1] - self.q_values[0]) b = 1 - m * self.q_values[0] _noise = m * self.q_values + b #print("noise input", _noise) self.expt.simulate_data(noise=_noise) self.assertEqual(len(self.expt.probe.dR), len(self.q_values)) ratio_i = self.expt.probe.dR[0] / self.r_i ratio_f = self.expt.probe.dR[-1] / self.r_f self.assertAlmostEqual(ratio_i, 0.01) self.assertAlmostEqual(ratio_f, 0.11)
def test(): import warnings # TODO: hard-code target output rather than relying on refl1d on the path try: from refl1d.names import silicon, air, QProbe, Experiment except ImportError: warnings.warn("Not testing Fresnel since refl1d is not on path") return # Rough silicon with an anomolously large absorbtion fresnel = Fresnel(rho=2.07, irho=0, sigma=20) sample = silicon(0, 20) | air Q = np.linspace(0, 5, 11) probe = QProbe(Q, Q * 0.02) m = Experiment(sample=sample, probe=probe) Rf = fresnel(Q) Rm = m.reflectivity() #print numpy.vstack((Q,Rf,Rm)).T # This is failing because reflectometry.model1d is failing; why that # is so has not yet been determined. relerr = np.linalg.norm((Rf - Rm) / Rm) assert relerr < 1e-10, "relative error is %g" % relerr
def test(): import warnings # TODO: hard-code target output rather than relying on refl1d on the path try: from refl1d.names import silicon, air, QProbe, Experiment except ImportError: warnings.warn("Not testing Fresnel since refl1d is not on path") return # Rough silicon with an anomolously large absorbtion fresnel = Fresnel(rho=2.07, irho=0, sigma=20) sample = silicon(0,20) | air Q = np.linspace(0, 5, 11) probe = QProbe(Q, Q*0.02) m = Experiment(sample=sample, probe=probe) Rf = fresnel(Q) Rm = m.reflectivity() #print numpy.vstack((Q,Rf,Rm)).T # This is failing because reflectometry.model1d is failing; why that # is so has not yet been determined. relerr = np.linalg.norm((Rf-Rm)/Rm) assert relerr < 1e-10, "relative error is %g"%relerr
class ExperimentMagneticSimulateTest(unittest.TestCase): """ Test the simulation functionality """ def setUp(self): self.q_values = np.logspace(-2.1, -.6, 100) L=4.75 dL=0.0475 dT=0.01 xs = [NeutronProbe(T=self.q_values, dT=dT, L=L, dL=dL) for _ in range(4)] probe = PolarizedNeutronProbe(xs) # Note: I don't use the usual shorthand to define the sample on purpose, # so I see explicitly what I'm using. # Note: 'MagneticSlab' is deprecated. Using magnetism=... instead. sample = ( Slab(material=SLD(name='Si', rho=2.07, irho=0.0)) | Slab(material=SLD(name='Cu', rho=6.5, irho=0.0), thickness=130, interface=15) | Slab(SLD(name='Stuff', rho=4.0, irho=0.0), thickness=50.0, interface=1.0, magnetism=Magnetism(rhoM=0.2, thetaM=270)) | Slab(material=SLD(name='air', rho=0, irho=0.0)) ) sample['Cu'].thickness.range(90.0, 200.0) probe.intensity = Parameter(value=1.0, name='normalization') probe.background = Parameter(value=0.0, name='background') self.expt = Experiment(probe=probe, sample=sample) # Reference self.expt.simulate_data(noise=1e-11) self.r_i = self.expt.probe.pp.R[0] self.r_f = self.expt.probe.pp.R[-1] def test_none_noise_with_mag(self): """ Provide None for noise with a magnetic sample """ self.expt.simulate_data(noise=None) self.assertEqual(len(self.expt.probe.pp.dR), len(self.q_values)) ratio_i = self.expt.probe.pp.dR[0] / self.r_i ratio_f = self.expt.probe.pp.dR[-1] / self.r_f self.assertAlmostEqual(ratio_i, 0.0) self.assertAlmostEqual(ratio_f, 0.0) def test_four_none_noise_with_mag(self): """ Provide [None]*4 for noise with a magnetic sample """ self.expt.simulate_data(noise=[None]*4) self.assertEqual(len(self.expt.probe.pp.dR), len(self.q_values)) ratio_i = self.expt.probe.pp.dR[0] / self.r_i ratio_f = self.expt.probe.pp.dR[-1] / self.r_f self.assertAlmostEqual(ratio_i, 0.0) self.assertAlmostEqual(ratio_f, 0.0) def test_noise_scalar_with_mag(self): """ Provide a scalar of dR with a magnetic sample """ self.expt.simulate_data(noise=2.5) self.assertEqual(len(self.expt.probe.pp.dR), len(self.q_values)) ratio_i = self.expt.probe.pp.dR[0] / self.r_i ratio_f = self.expt.probe.pp.dR[-1] / self.r_f self.assertAlmostEqual(ratio_i, 0.025) self.assertAlmostEqual(ratio_f, 0.025) def test_four_noise_scalar_with_mag(self): """ Provide four scalars of dR with a magnetic sample """ self.expt.simulate_data(noise=[2.5, 2.5, 2.5, 2.5]) self.assertEqual(len(self.expt.probe.pp.dR), len(self.q_values)) ratio_i = self.expt.probe.pp.dR[0] / self.r_i ratio_f = self.expt.probe.pp.dR[-1] / self.r_f self.assertAlmostEqual(ratio_i, 0.025) self.assertAlmostEqual(ratio_f, 0.025) #unittest.skip("ambiguous interface not supported") def not_test_noise_array_with_mag(self): """ Provide a dR array with a magnetic sample """ m = 10.0 / (self.q_values[-1] - self.q_values[0]) b = 1 - m * self.q_values[0] _noise = m * self.q_values + b self.expt.simulate_data(noise=_noise) self.assertEqual(len(self.expt.probe.pp.dR), len(self.q_values)) ratio_i = self.expt.probe.pp.dR[0] / self.r_i ratio_f = self.expt.probe.pp.dR[-1] / self.r_f self.assertAlmostEqual(ratio_i, 0.01) self.assertAlmostEqual(ratio_f, 0.11) def test_four_noise_array_with_mag(self): """ Provide four dR arrays with a magnetic sample """ m = 10.0 / (self.q_values[-1] - self.q_values[0]) b = 1 - m * self.q_values[0] _noise = m * self.q_values + b self.expt.simulate_data(noise=[_noise, _noise, _noise, _noise]) self.assertEqual(len(self.expt.probe.pp.dR), len(self.q_values)) ratio_i = self.expt.probe.pp.dR[0] / self.r_i ratio_f = self.expt.probe.pp.dR[-1] / self.r_f self.assertAlmostEqual(ratio_i, 0.01) self.assertAlmostEqual(ratio_f, 0.11)