def test_refl_nvary_amp_roughness(self): theta = np.linspace(0., 5., 1000, dtype=np.float64) lamda = np.linspace(4., 5.0, 1000, dtype=np.float64) n = np.array([ 1 - 7.57e-6 + 1.73e-7j, 1 - 2.24e-5 + 2.89e-6j, 1 - 7.57e-6 + 1.73e-7j, 1 - 2.24e-5 + 2.89e-6j, 1 - 7.57e-6 + 1.73e-7j, 1 - 2.24e-5 + 2.89e-6j, 1 - 7.57e-6 + 1.73e-7j, 1 ], dtype=np.complex128) n = n[:, np.newaxis] * lamda[np.newaxis, :] / 4.0 d = np.array([2, 80, 20, 80, 20, 80, 20, 2], dtype=np.float64) sigma = np.array([10, 10, 10, 10, 10, 10, 10, 10], dtype=np.float64) G1 = paratt.Refl_nvary2(theta, lamda, n, d, sigma, return_int=False) G2 = paratt_numba.Refl_nvary2(theta, lamda, n, d, sigma, return_int=False) np.testing.assert_array_almost_equal(G1, G2) if CUDA: G2 = paratt_cuda.Refl_nvary2(theta, lamda, n, d, sigma, return_int=False) np.testing.assert_array_almost_equal(G1, G2)
def EnergySpecular(Energy, TwoThetaQz, sample, instrument): ''' Simulate the specular signal from sample when probed with instrument. Energy should be in eV. # BEGIN Parameters Energy data.x TwoThetaQz 3.0 # END Parameters ''' # preamble to get it working with my class interface restype = instrument.getRestype() #TODO: Fix so that resolution can be included. if restype != 0 and restype != instrument_string_choices['restype'][0]: raise ValueError('Only no resolution is allowed for energy scans.') wl = AA_to_eV / Energy # TTH values given as x if instrument.getCoords() == instrument_string_choices['coords'][1] \ or instrument.getCoords() == 1: theta = TwoThetaQz / 2.0 # Q vector given.... elif instrument.getCoords() == instrument_string_choices['coords'][0] \ or instrument.getCoords() == 0: theta = arcsin(TwoThetaQz * wl / 4 / pi) * 180.0 / pi else: raise ValueError('The value for coordinates, coords, is WRONG!' 'should be q(0) or tth(1).') Q = 4 * pi / wl * sin((2 * theta + instrument.getTthoff()) * pi / 360.0) type = instrument.getProbe() parameters = sample.resolveLayerParameters() if type == instrument_string_choices['probe'][0] or type == 0: fb = refl.cast_to_array(parameters['f'], Energy) else: fb = array(parameters['b'], dtype=complex64) * 1e-5 abs_xs = array(parameters['xs_ai'], dtype=complex64) * (1e-4)**2 dens = array(parameters['dens'], dtype=complex64) d = array(parameters['d'], dtype=float64) sigma = array(parameters['sigma'], dtype=float64) if type == instrument_string_choices['probe'][0] or type == 0: sld = dens[:, newaxis] * fb * wl**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) # 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) R = Paratt.Refl_nvary2(theta, wl, 1.0 - 2.82e-5 * sld, d, sigma) else: raise ValueError('The choice of probe is WRONG') #TODO: Fix corrections #FootprintCorrections #foocor = footprintcorr(Q, instrument) #Resolution corrections #R = resolutioncorr(R, TwoThetaQz, foocor, instrument, weight) return R * instrument.getI0() + instrument.getIbkg()
if __name__ == '__main__': import genx.models.lib.paratt as paratt import time import pylab as pl theta = arange(0, 10, 0.01) + 1e-12 q = 4 * math.pi / 1.54 * sin(theta * pi / 180.0) rep = 1000 n = array([1 - 7.57e-6 + 1.73e-7j] + [ 1 - 2.24e-5 + 2.89e-6j, 1 - 7.57e-6 + 1.73e-7j, 1 - 2.24e-5 + 2.89e-6j, 1 - 7.57e-6 + 1.73e-7j, 1 - 2.24e-5 + 2.89e-6j, 1 - 7.57e-6 + 1.73e-7j ] * rep + [1]) d = array([1] + [80, 20, 80, 20, 80, 20] * rep + [1]) * 1.0 sigma = array([0] + [0, 0, 0, 0, 0, 0] * rep + [0]) * 1.0 # print(n.shape) t1 = time.clock() c1 = paratt.Refl_nvary2(theta, 1.54 * ones(theta.shape), n[:, newaxis] * ones(theta.shape), d, sigma) # c2=paratt.Refl_nvary2(theta, 1.54*ones(theta.shape), n[:, newaxis]*ones(theta.shape), d,sigma*0) # c3 = paratt.Refl(theta, 1.54, n, d, sigma) t2 = time.clock() c4 = Refl_nvary2(theta, 1.54 * ones(theta.shape), n[:, newaxis] * ones(theta.shape), d, sigma) # c5 = Refl_nvary2_nosigma(theta, 1.54*ones(theta.shape), n[:, newaxis]*ones(theta.shape), d) # c6 = Refl(theta, 1.54, n, d, sigma) t3 = time.clock() print(t2 - t1, t3 - t2) pl.plot(theta, log10(c1), 'x', theta, log10(c4)) pl.show()
def specular_calcs(TwoThetaQz, sample, instrument, return_int=True): """ 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, return_int=return_int) #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, return_int=return_int) #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, return_int=return_int) # 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, return_int=return_int) elif pol == instrument_string_choices['pol'][3] or pol == 3: Rp = Paratt.ReflQ(Q, instrument.getWavelength(), 1.0 - sld - msld, d, sigma, return_int=return_int) Rm = Paratt.ReflQ(Q, instrument.getWavelength(), 1.0 - sld + msld, d, sigma, return_int=return_int) 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, return_int=return_int) 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, return_int=return_int) # 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, return_int=return_int) # 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, return_int=return_int) # 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, return_int=return_int) Ru = Paratt.Refl_nvary2( instrument.getIncangle() * ones(Q.shape), (4 * pi * sin(instrument.getIncangle() * pi / 180) / Q), 1.0 - sld - msld, d, sigma, return_int=return_int) 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') if return_int: # FootprintCorrections foocor = footprintcorr(Q, instrument) # Resolution corrections R = resolutioncorr(R, TwoThetaQz, foocor, instrument, weight) return R * instrument.getI0() + instrument.getIbkg() else: return R