Exemple #1
0
 def calculate_residuals(self, parameters):
     ma = ModelAdapter(self.sample_model)
     ma.update_model_from_params(parameters)
     layers = [self.sample_model.incoming_media
               ] + self.sample_model.layers + [self.sample_model.substrate]
     sublayers = generateSublayers(layers)[0]
     chi_array = []
     for ds in self.data_model.datasets:
         if ds.R is not None and len(ds.R) > 1:
             q = ds.Q / 2.
             r = reflection(q, sublayers)
             pol_eff = np.ones(len(q), dtype=np.complex128)
             an_eff = np.ones(len(q), dtype=np.complex128)
             rr = np.real(
                 spin_av(r, ds.pol_Polarizer, ds.pol_Analyzer, pol_eff,
                         an_eff))
             sigma = ds.sigmaQ
             rrr = resolut(
                 rr, q, sigma, 4
             ) * self.data_model.theory_factor + self.data_model.background
             chi_array.append(
                 (rrr / self.data_model.experiment_factor - ds.R) / ds.E)
     chi = np.array(chi_array).ravel()
     self.chiSquaredChanged.emit((chi**2).mean())
     return chi
    def test_reference_results(self):
        paramfile = open(os.path.join(os.path.dirname(__file__),'data/refl_par.dat'),'r')
        n_monte_carlo = int(paramfile.readline())
        formalism = int(paramfile.readline())
        res_mode = int(paramfile.readline())
        n_of_outputs = int(paramfile.readline())
        pol_vecs = np.array([float(value) for value in paramfile.readline().strip().split()]).reshape(6,3)
        an_vecs = np.array([float(value) for value in paramfile.readline().strip().split()]).reshape(6,3)
        pol_fun = [int(value) for value in paramfile.readline().split()]
        norm_factor = [int(value) for value in paramfile.readline().split()]
        maxwell = int(paramfile.readline())
        glance_angle = int(paramfile.readline())
        background = float(paramfile.readline())
        percentage = float(paramfile.readline())
        nlayers1 = int(paramfile.readline())
        substrate_tmp = [float(value) for value in paramfile.readline().split()]
        substrate=Layer()
        substrate.nsld = complex(substrate_tmp[0],substrate_tmp[1])
        NC = float(paramfile.readline())
        layers = [Layer(name='Incoming media')]
        for i in range(nlayers1):
            l = Layer()
            l.thickness = float(paramfile.readline())
            nsld_tmp = [float(value) for value in paramfile.readline().split()]
            l.nsld = complex(nsld_tmp[0], nsld_tmp[1])
            msld_xyz = np.array([float(value) for value in paramfile.readline().split()])
            l.msld.rho = np.sqrt(msld_xyz.dot(msld_xyz))
            l.msld.phi= np.degrees(np.arctan2(msld_xyz[1],msld_xyz[0]))
            l.msld.theta=0
            if l.msld.rho.value!=0:
                l.msld.theta=np.degrees(np.arccos(np.nan_to_num(msld_xyz[2]/l.msld.rho.value)))
            l.NC = float(paramfile.readline())
            layers.append(l)

        paramfile.close()

        q, dq = np.loadtxt(os.path.join(os.path.dirname(__file__),'data/refl_q_dq.dat'),unpack=True)
        inc_moment = q / 2.0

        pol_eff = np.ones(len(q), dtype=np.complex128)
        an_eff = np.ones(len(q), dtype=np.complex128)
        layers.append(substrate)
        R = reflection.reflection(inc_moment, layers)
        for k in range(n_of_outputs):
            RR = reflection.spin_av(R, pol_vecs[k], an_vecs[k], pol_eff, an_eff)
            RR = np.real(RR)
            for res_mode in range(1,3):
                RRr = reflection.resolut(RR, q, dq, res_mode)
                RRr = RRr * norm_factor[k] + background
                reference_values = np.loadtxt(os.path.join(os.path.dirname(__file__),'data/res_mode'+str(res_mode)+'refl'+str(k+1)+'.dat'), unpack=True)
                assert_array_almost_equal(reference_values, RRr)
class Testchi3_137(unittest.TestCase):
    Q = np.loadtxt(os.path.join(os.path.dirname(__file__),'data/chi3_137/q.dat'),unpack=True)
    inc_moment = Q / 2.0

    sigma = res_chi3_137(Q)
    substrate = Layer()
    substrate.nsld = complex(3.6214e-006, 0.0)
    layer_info = np.loadtxt(os.path.join(os.path.dirname(__file__), 'data/chi3_137/profile_sublayers.dat'))
    layer_info = layer_info[:-1]
    layers = [Layer(name='Incoming media')]
    for line in layer_info:
        l = Layer()
        l.thickness = line[1]
        l.nsld = complex(line[2], line[3])
        msld = [line[4], line[5], line[6]]
        #l.msld = [msld[0]*np.sin(msld[2])*np.cos(msld[1]), msld[0]*np.sin(msld[2])*np.sin(msld[1]), msld[0]*np.cos(msld[2])]
        l.msld.rho=msld[0]
        l.msld.theta=msld[2]
        l.msld.phi=msld[1]
        l.NC = 0.0
        layers.append(l)
    layers.append(substrate)
    R = reflection.reflection(inc_moment, layers)
    pol_vecs = [[0.98,0.0,0.0], [-0.98,0.0,0.0], [0.98,0.0,0.0], [0.0,0.0,0.0], [0.0,0.0,0.0], [0.0,0.0,0.0]]
    an_vecs = [[0.98,0.0,0.0], [-0.98,0.0,0.0], [-0.98,0.0,0.0], [0.0,0.0,0.0], [0.0,0.0,0.0], [0.0,0.0,0.0]]
    norm_factor=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
    background=1.2e-05;

    pol_eff = np.ones(len(Q), dtype = np.complex128)
    an_eff = np.ones(len(Q), dtype = np.complex128)

    n_of_outputs = 3
    for k in range(n_of_outputs):
        RR = reflection.spin_av(R, pol_vecs[k], an_vecs[k], pol_eff, an_eff)
        RR = np.real(RR)
        RRr = reflection.resolut(RR, Q, sigma, 3)
        RRr = RRr * norm_factor[k] + background
        fig,ax = plt.subplots()
        ax.semilogy(Q,RRr,label='calculation')
        #plt.xlim([0.0,0.06])
        #plt.ylim([1.0e-4,10.0])
        reference_values = np.loadtxt(os.path.join(os.path.dirname(__file__),'data/chi3_137/rtheory'+str(k+1)+'.dat'), unpack=True)
        assert_array_almost_equal(RRr,reference_values,2)
        ax.semilogy(Q, reference_values, label='reference')
        ax.set_xlabel('Momentum Transfer $\AA^{-1}$')
        ax.set_ylabel('Reflectivity')
        ax.set_title('Polarization ({},{},{}), Analysis({},{},{})'.format(pol_vecs[k][0],pol_vecs[k][1],pol_vecs[k][2],an_vecs[k][0],an_vecs[k][1],an_vecs[k][2]))
        ax.legend()
        fig.savefig('chi3_137_'+str(k+1)+'.pdf')
        plt.close()