def test_refl_nonmag_air_no_roughness(self): # test ambient layer non-air Q = np.linspace(0.001, 0.5, 1000, dtype=np.float64) SLDs = np.array([6.36e+00, (4.66e+00 - 1.60e-02j), 0e+00], dtype=np.complex128) * 1e-6 n = 1.0 - SLDs Vp = ((2.0 * np.pi / 4.5)**2 * (2 * SLDs - SLDs**2)).astype( np.complex128 ) #((2.0*np.pi/4.5)**2*(1-n**2)).astype(np.complex128) Vm = Vp d = np.array([0, 100, 0], dtype=np.float64) M_ang = np.array([ 0.0, 0.0, 0.0, ], dtype=np.float64) sigma = None G0 = paratt.ReflQ(Q, 4.5, n, d, d * 0., return_int=True) G1 = neutron_refl.Refl(Q, Vp, Vm, d, M_ang, sigma, return_int=True) G2 = neutron_numba.Refl(Q, Vp, Vm, d, M_ang, sigma, return_int=True) np.testing.assert_array_almost_equal( G0, G1[0]) # Matrix not so accurate without magnetization np.testing.assert_array_almost_equal(G1, G2) if CUDA: G2 = neutron_cuda.Refl(Q, Vp, Vm, d, M_ang, sigma, return_int=True) np.testing.assert_array_almost_equal(G1, G2)
def test_refl_air_nonmag(self): # test ambient layer non-air Q = np.linspace(0.001, 0.5, 1000, dtype=np.float64) SLDs = np.array([6.36e+00, (4.66e+00 - 1.60e-02j), 0e+00], dtype=np.complex128) * 1e-6 n = 1.0 - SLDs Vp = ((2.0 * np.pi / 4.5)**2 * (1 - n**2)).astype(np.complex128) Vm = Vp d = np.array([0, 100, 0], dtype=np.float64) M_ang = np.array([ 0.0, 0.0, 0.0, ], dtype=np.float64) sigma = np.array([10., 3., 0.], dtype=np.float64) G0 = paratt.ReflQ(Q, 4.5, n, d, sigma, return_int=True) G1 = neutron_refl.Refl(Q, Vp, Vm, d, M_ang, sigma, return_int=True) G2 = neutron_numba.Refl(Q, Vp, Vm, d, M_ang, sigma, return_int=True) np.testing.assert_array_almost_equal(G0, G1[0]) np.testing.assert_array_almost_equal(G1, G2) if CUDA: G2 = neutron_cuda.Refl(Q, Vp, Vm, d, M_ang, sigma, return_int=True) np.testing.assert_array_almost_equal(G1, G2)
def test_refl_nonmag_no_roughness(self): # test ambient layer non-air Q = np.linspace(0.001, 0.5, 1000, dtype=np.float64) SLDs = np.array([6.36e+00, (4.66e+00 - 1.60e-02j), 2.07e+00], dtype=np.complex128) * 1e-6 n = 1.0 - SLDs n_prime = 1.0 - (SLDs - SLDs[-1] ) # use corrected SLD for matrix method Vp = ((2.0 * np.pi / 4.5)**2 * (1 - n_prime**2)).astype(np.complex128) Vm = Vp d = np.array([0, 100, 0], dtype=np.float64) M_ang = np.array([ 0.0, 0.0, 0.0, ], dtype=np.float64) sigma = None G0 = paratt.ReflQ(Q, 4.5, n, d, d * 0., return_int=True) G1 = neutron_refl.Refl(Q, Vp, Vm, d, M_ang, sigma, return_int=True) G2 = neutron_numba.Refl(Q, Vp, Vm, d, M_ang, sigma, return_int=True) np.testing.assert_array_almost_equal(G0, G1[0], decimal=4) np.testing.assert_array_almost_equal(G1, G2) if CUDA: G2 = neutron_cuda.Refl(Q, Vp, Vm, d, M_ang, sigma, return_int=True) np.testing.assert_array_almost_equal(G1, G2)
def test_refl_no_roughness(self): Q = np.linspace(0.001, 0.5, 1000, dtype=np.float64) # sld_Fe=8e-6 sld_Fe_p = 12.9e-6 sld_Fe_m = 2.9e-6 sld_Pt = 6.22e-6 Vp = np.array([ sld_Pt / np.pi, sld_Fe_p / np.pi, sld_Pt / np.pi, sld_Fe_p / np.pi, 0 ], dtype=np.complex128) Vm = np.array([ sld_Pt / np.pi, sld_Fe_m / np.pi, sld_Pt / np.pi, sld_Fe_m / np.pi, 0 ], dtype=np.complex128) d = np.array([3, 100, 50, 100, 3], dtype=np.float64) M_ang = np.array([ 0.0, 45 * np.pi / 180, 0.0, 90 * np.pi / 180, 0.0, ], dtype=np.float64) sigma = None G1 = neutron_refl.Refl(Q, Vp, Vm, d, M_ang, sigma, return_int=True) G2 = neutron_numba.Refl(Q, Vp, Vm, d, M_ang, sigma, return_int=True) np.testing.assert_array_almost_equal(G1, G2) if CUDA: G2 = neutron_cuda.Refl(Q, Vp, Vm, d, M_ang, sigma, return_int=True) np.testing.assert_array_almost_equal(G1, G2)
def Specular(TwoThetaQz, sample, instrument): """ Simulate the specular signal from sample when probed with instrument # BEGIN Parameters TwoThetaQz data.x # END Parameters """ # preamble to get it working with my class interface restype = instrument.getRestype() Q, TwoThetaQz, weight = resolution_init(TwoThetaQz, instrument) if any(Q < q_limit): raise ValueError('The q vector has to be above %.1e' % q_limit) type = instrument.getProbe() pol = instrument.getPol() parameters = sample.resolveLayerParameters() if type == instrument_string_choices['probe'][0] or type == 0: #fb = array(parameters['f'], dtype = complex64) e = AA_to_eV / instrument.getWavelength() fb = refl.cast_to_array(parameters['f'], e) else: fb = array(parameters['b'], dtype=complex128) * 1e-5 abs_xs = array(parameters['xs_ai'], dtype=complex128) * (1e-4)**2 dens = array(parameters['dens'], dtype=complex64) d = array(parameters['d'], dtype=float64) magn = array(parameters['magn'], dtype=float64) #Transform to radians magn_ang = array(parameters['magn_ang'], dtype=float64) * pi / 180.0 sigma = array(parameters['sigma'], dtype=float64) if type == instrument_string_choices['probe'][0] or type == 0: sld = dens * fb * instrument.getWavelength()**2 / 2 / pi else: wl = instrument.getWavelength() #sld = dens*(wl**2/2/pi*sqrt(fb**2 - (abs_xs/2.0/wl)**2) - # 1.0J*abs_xs*wl/4/pi) sld = neutron_sld(abs_xs, dens, fb, wl) # Ordinary Paratt X-rays if type == instrument_string_choices['probe'][0] or type == 0: R = Paratt.ReflQ(Q, instrument.getWavelength(), 1.0 - 2.82e-5 * sld, d, sigma) #reload(slow_paratt) #R = slow_paratt.reflq_kin(Q, instrument.getWavelength(), 1.0 - 2.82e-5 * sld, d, sigma) #R = slow_paratt.reflq_pseudo_kin(Q, instrument.getWavelength(), 1.0 - 2.82e-5 * sld, d, sigma) #R = slow_paratt.reflq_sra(Q, instrument.getWavelength(), 1.0 - 2.82e-5 * sld, d, sigma) #Ordinary Paratt Neutrons elif type == instrument_string_choices['probe'][1] or type == 1: R = Paratt.ReflQ(Q, instrument.getWavelength(), 1.0 - sld, d, sigma) #Ordinary Paratt but with magnetization elif type == instrument_string_choices['probe'][2] or type == 2: msld = 2.645e-5 * magn * dens * instrument.getWavelength()**2 / 2 / pi # Polarization uu or ++ if pol == instrument_string_choices['pol'][0] or pol == 0: R = Paratt.ReflQ(Q,instrument.getWavelength(),\ 1.0-sld-msld,d,sigma) # Polarization dd or -- elif pol == instrument_string_choices['pol'][1] or pol == 1: R = Paratt.ReflQ(Q,instrument.getWavelength(),\ 1.0-sld+msld,d,sigma) elif pol == instrument_string_choices['pol'][3] or pol == 3: Rp = Paratt.ReflQ(Q, instrument.getWavelength(), 1.0 - sld - msld, d, sigma) Rm = Paratt.ReflQ(Q, instrument.getWavelength(), 1.0 - sld + msld, d, sigma) R = (Rp - Rm) / (Rp + Rm) else: raise ValueError('The value of the polarization is WRONG.' ' It should be uu(0) or dd(1)') # Spin flip elif type == instrument_string_choices['probe'][3] or type == 3: # Check if we have calcluated the same sample previous: if Buffer.TwoThetaQz is not None: Q_ok = Buffer.TwoThetaQz.shape == Q.shape if Q_ok: Q_ok = any(not_equal(Buffer.TwoThetaQz, Q)) if Buffer.parameters != parameters or not Q_ok: msld = 2.645e-5 * magn * dens * instrument.getWavelength( )**2 / 2 / pi np = 1.0 - sld - msld nm = 1.0 - sld + msld Vp = (2 * pi / instrument.getWavelength())**2 * (1 - np**2) Vm = (2 * pi / instrument.getWavelength())**2 * (1 - nm**2) (Ruu, Rdd, Rud, Rdu) = MatrixNeutron.Refl(Q, Vp, Vm, d, magn_ang, sigma) Buffer.Ruu = Ruu Buffer.Rdd = Rdd Buffer.Rud = Rud Buffer.parameters = parameters.copy() Buffer.TwoThetaQz = Q.copy() else: pass # Polarization uu or ++ if pol == instrument_string_choices['pol'][0] or pol == 0: R = Buffer.Ruu # Polarization dd or -- elif pol == instrument_string_choices['pol'][1] or pol == 1: R = Buffer.Rdd # Polarization ud or +- elif (pol == instrument_string_choices['pol'][2] or pol == 2 or pol == instrument_string_choices['pol'][4] or pol == 4): R = Buffer.Rud # Calculating the asymmetry ass elif pol == instrument_string_choices['pol'][3] or pol == 3: R = (Buffer.Ruu - Buffer.Rdd) / (Buffer.Ruu + Buffer.Rdd + 2 * Buffer.Rud) else: raise ValueError('The value of the polarization is WRONG.' ' It should be uu(0), dd(1) or ud(2)') # tof elif type == instrument_string_choices['probe'][4] or type == 4: wl = 4 * pi * sin(instrument.getIncangle() * pi / 180) / Q sld = neutron_sld(abs_xs[:, newaxis], dens[:, newaxis], fb[:, newaxis], wl) R = Paratt.Refl_nvary2(instrument.getIncangle()*ones(Q.shape),\ (4*pi*sin(instrument.getIncangle()*pi/180)/Q),\ 1.0-sld,d,sigma) # tof spin polarized elif type == instrument_string_choices['probe'][5] or type == 5: wl = 4 * pi * sin(instrument.getIncangle() * pi / 180) / Q sld = neutron_sld(abs_xs[:, newaxis], dens[:, newaxis], fb[:, newaxis], wl) msld = 2.645e-5*magn[:,newaxis]*dens[:,newaxis]\ *(4*pi*sin(instrument.getIncangle()*pi/180)/Q)**2/2/pi # polarization uu or ++ if pol == instrument_string_choices['pol'][0] or pol == 0: R = Paratt.Refl_nvary2(instrument.getIncangle()*ones(Q.shape),\ (4*pi*sin(instrument.getIncangle()*pi/180)/Q),\ 1.0-sld-msld,d,sigma) # polarization dd or -- elif pol == instrument_string_choices['pol'][1] or pol == 1: R = Paratt.Refl_nvary2(instrument.getIncangle()*ones(Q.shape),\ (4*pi*sin(instrument.getIncangle()*pi/180)/Q),\ 1.0-sld+msld,d,sigma) # Calculating the asymmetry elif pol == instrument_string_choices['pol'][3] or pol == 3: Rd = Paratt.Refl_nvary2( instrument.getIncangle() * ones(Q.shape), (4 * pi * sin(instrument.getIncangle() * pi / 180) / Q), 1.0 - sld + msld, d, sigma) Ru = Paratt.Refl_nvary2( instrument.getIncangle() * ones(Q.shape), (4 * pi * sin(instrument.getIncangle() * pi / 180) / Q), 1.0 - sld - msld, d, sigma) R = (Ru - Rd) / (Ru + Rd) else: raise ValueError('The value of the polarization is WRONG.' ' It should be uu(0) or dd(1) or ass') else: raise ValueError('The choice of probe is WRONG') #FootprintCorrections foocor = footprintcorr(Q, instrument) #Resolution corrections R = resolutioncorr(R, TwoThetaQz, foocor, instrument, weight) return R * instrument.getI0() + instrument.getIbkg()
def Specular(TwoThetaQz, sample, instrument): """ Simulate the specular signal from sample when probed with instrument # BEGIN Parameters TwoThetaQz data.x # END Parameters """ # preamble to get it working with my class interface restype = instrument.getRestype() Q, TwoThetaQz, weight = resolution_init(TwoThetaQz, instrument) if any(Q < q_limit): raise ValueError('The q vector has to be above %.1e' % q_limit) type = instrument.getProbe() pol = instrument.getPol() parameters = sample.resolveLayerParameters() if type == instrument_string_choices['probe'][0] or type == 0: #fb = array(parameters['f'], dtype = complex64) e = AA_to_eV / instrument.getWavelength() sld = refl.cast_to_array(parameters['sld_x'], e) * 1e-6 else: sld = array(parameters['sld_n'], dtype=complex128) * 1e-6 d = array(parameters['d'], dtype=float64) sld_m = array(parameters['sld_m'], dtype=float64) * 1e-6 #Transform to radians magn_ang = array(parameters['magn_ang'], dtype=float64) * pi / 180.0 sigma = array(parameters['sigma'], dtype=float64) wl = instrument.getWavelength() l2pi = wl**2 / 2 / 3.141592 # Ordinary Paratt X-rays if type == instrument_string_choices['probe'][0] or type == 0: R = Paratt.ReflQ(Q, instrument.getWavelength(), 1.0 - l2pi * sld, d, sigma) #Ordinary Paratt Neutrons elif type == instrument_string_choices['probe'][1] or type == 1: R = Paratt.ReflQ(Q, instrument.getWavelength(), 1.0 - l2pi * sld, d, sigma) #Ordinary Paratt but with magnetization elif type == instrument_string_choices['probe'][2] or type == 2: # Polarization uu or ++ if pol == instrument_string_choices['pol'][0] or pol == 0: R = Paratt.ReflQ(Q,instrument.getWavelength(),\ 1.0 - l2pi*(sld + sld_m), d, sigma) # Polarization dd or -- elif pol == instrument_string_choices['pol'][1] or pol == 1: R = Paratt.ReflQ(Q,instrument.getWavelength(),\ 1.0 - l2pi*(sld - sld_m), d, sigma) elif pol == instrument_string_choices['pol'][3] or pol == 3: Rp = Paratt.ReflQ(Q, instrument.getWavelength(), 1.0 - l2pi * (sld - sld_m), d, sigma) Rm = Paratt.ReflQ(Q, instrument.getWavelength(), 1.0 - l2pi * (sld + sld_m), d, sigma) R = (Rp - Rm) / (Rp + Rm) else: raise ValueError('The value of the polarization is WRONG.' ' It should be uu(0) or dd(1)') # Spin flip elif type == instrument_string_choices['probe'][3] or type == 3: # Check if we have calcluated the same sample previous: if Buffer.TwoThetaQz is not None: Q_ok = Buffer.TwoThetaQz.shape == Q.shape if Q_ok: Q_ok = any(not_equal(Buffer.TwoThetaQz, Q)) if Buffer.parameters != parameters or not Q_ok: #msld = 2.645e-5*magn*dens*instrument.getWavelength()**2/2/pi np = 1.0 - l2pi * (sld + sld_m) nm = 1.0 - l2pi * (sld - sld_m) Vp = (2 * pi / instrument.getWavelength())**2 * (1 - np**2) Vm = (2 * pi / instrument.getWavelength())**2 * (1 - nm**2) (Ruu, Rdd, Rud, Rdu) = MatrixNeutron.Refl(Q, Vp, Vm, d, magn_ang, sigma) Buffer.Ruu = Ruu Buffer.Rdd = Rdd Buffer.Rud = Rud Buffer.parameters = parameters.copy() Buffer.TwoThetaQz = Q.copy() else: pass # Polarization uu or ++ if pol == instrument_string_choices['pol'][0] or pol == 0: R = Buffer.Ruu # Polarization dd or -- elif pol == instrument_string_choices['pol'][1] or pol == 1: R = Buffer.Rdd # Polarization ud or +- elif (pol == instrument_string_choices['pol'][2] or pol == 2 or pol == instrument_string_choices['pol'][4] or pol == 4): R = Buffer.Rud # Calculating the asymmetry ass elif pol == instrument_string_choices['pol'][3] or pol == 3: R = (Buffer.Ruu - Buffer.Rdd) / (Buffer.Ruu + Buffer.Rdd + 2 * Buffer.Rud) else: raise ValueError( 'The value of the polarization is WRONG. It should be uu(0), dd(1) or ud(2)' ) else: raise ValueError('The choice of probe is WRONG') #FootprintCorrections foocor = footprintcorr(Q, instrument) #Resolution corrections R = resolutioncorr(R, TwoThetaQz, foocor, instrument, weight) return R * instrument.getI0() + instrument.getIbkg()