Ejemplo n.º 1
0
def calc_intraComponentFZ(Q, fe_Q, Ztot, QmaxIntegrate, maxQ, elementList, element, \
    x, y, z, elementParameters, damping_factor, aff_mean_squared):
    """Function to calculate the intra-molecular components.

    Parameters
    ----------
    Q                 : numpy array
                        momentum transfer (nm^-1)
    fe_Q              : numpy array
                        effective electric form factor
    Ztot              : int
                        total Z number
    QmaxIntegrate     : float
                        maximum Q value for the intagrations
    maxQ              : float
                        maximum Q value
    elementList       : dictionary("element": multiplicity)
                        chemical elements of the sample with their multiplicity
                        element      : string
                                       chemical element
                        multiplicity : int
                                       chemical element multiplicity
    element           : string array
                        array with the elements in the xyz_file
    x, y, z           : float array
                        atomic coordinate in the xyz_file (nm)
    elementParameters : dictionary("element": parameters)
                        chemical elements of the sample with their parameters
                        element    : string
                                     chemical element
                        parameters : list
                                     list of the parameters
                                     (Z, a1, b1, a2, b2, a3, b3, a4, b4, c, M, K, L)
    damping_factor    : float
                        damp factor

    Returns
    -------
    r                 : numpy array
                        atomic distance (nm)
    Fintra_r          : numpy array
                        intramolecular contribution of F(r)
    """

    iintra_Q = calc_iintraFZ(Q, QmaxIntegrate, maxQ, elementList, element, x,
                             y, z, elementParameters, aff_mean_squared)
    iintradamp_Q = UtilityAnalysis.calc_iintradamp(iintra_Q, Q, QmaxIntegrate,
                                                   damping_factor)
    r = MainFunctions.calc_r(Q)
    Fintra_r = MainFunctions.calc_Fr(r, Q[Q <= QmaxIntegrate],
                                     iintradamp_Q[Q <= QmaxIntegrate])

    return (r, iintradamp_Q, Fintra_r)
Ejemplo n.º 2
0
    def Fr(self):
        """Function to calculte and plot F(r)"""

        self.i_Q = MainFunctions.calc_iQ(self.SsmoothDamp_Q, self.Sinf)
        # self.r = MainFunctions.calc_r(self.Q)
        Qi_Q = self.Q * self.i_Q
        self.r, self.F_r = MainFunctions.calc_Fr(
            self.Q[self.Q <= self.ui.QmaxIntegrate.value()],
            Qi_Q[self.Q <= self.ui.QmaxIntegrate.value()])

        # self.ui.distfuncPlot.canvas.ax.plot(self.r, self.F_r, "b", label=r"$F(r)$")
        # self.ui.distfuncPlot.canvas.ax.legend()
        # self.ui.distfuncPlot.canvas.draw()

        return (self.r, self.F_r)
Ejemplo n.º 3
0
def calc_optimize_FrFZ(iteration, F_r, Fintra_r, rho0, i_Q, Q, Iincoh_Q, r,
                       rmin):
    """Function to calculate the F(r) optimization (eq 47, 48, 49).

    Parameters
    ----------
    iteration : int
                number of iterations
    F_r       : numpy array
                F(r)
    rho0      : float
                atomic density
    i_Q       : numpy array
                i(Q)
    Q         : numpy array
                momentum transfer (nm^-1)
    Sinf      : float
                value of S(Q) for Q->inf
    J_Q       : numpy array
                J(Q)
    r         : numpy array
                atomic distance (nm)
    rmin      : float
                r cut-off value (nm)
    plot_iter : string
                flag to plot the F(r) iterations

    Returns
    -------
    F_r       : numpy array
                optimazed F(r)
    deltaF_r  : numpy array
                difference between the last F(r) and its theoretical value
    """

    for i in range(iteration):
        deltaF_r = Optimization.calc_deltaFr(F_r, Fintra_r, r, rho0)
        i_Q = calc_iQiFZ(i_Q, Q, Iincoh_Q, deltaF_r, r, rmin)
        i_Q[0] = 0.0
        F_r = MainFunctions.calc_Fr(r, Q, i_Q)

    return (F_r, deltaF_r)
Ejemplo n.º 4
0
    def Optimization(self):
        """Function to optimize and plot F(r)"""

        #-------------------Intra-molecular components-----------------------------

        # numAtoms, element, x, y, z = Utility.read_xyz_file(self.XYZFilePath)
        numAtoms, element, x, y, z = Utility.read_xyz_file(
            "/Users/ciccio/work/ID27/LASDiA/xyzFiles/Ar.xyz")

        iintra_Q = Optimization.calc_iintra(self.Q, self.fe_Q, self.Ztot,
                                            self.ui.QmaxIntegrate.value(),
                                            self.ui.maxQ.value(),
                                            self.elementList, element, x, y, z,
                                            self.elementParameters)
        iintradamp_Q = UtilityAnalysis.calc_iintradamp(iintra_Q,
                                                       self.dampingFunct)
        Qiintradamp_Q = self.Q * iintradamp_Q
        rintra, Fintra_r = MainFunctions.calc_Fr(
            self.Q[self.Q <= self.ui.QmaxIntegrate.value()],
            Qiintradamp_Q[self.Q <= self.ui.QmaxIntegrate.value()])

        scaleFactor = self.ui.scaleFactorValue.value()
        density0 = self.ui.densityValue.value()

        # ----------------------First scale minimization---------------------------

        scaleStep = 0.05

        # sth = 0.008
        # s0th = 0.006
        sth = 0.0
        s0th = 0.0
        phi_matrix = 0.0
        thickness_sampling = 0.0

        scaleFactor = Minimization.OptimizeScale(
            self.Q, self.I_Q, self.Ibkg_Q, self.J_Q, self.Iincoh_Q, self.fe_Q,
            self.ui.maxQ.value(), self.ui.minQ.value(),
            self.ui.QmaxIntegrate.value(), self.Ztot, density0,
            scaleFactor, self.Sinf, self.ui.smoothingFactor.value(),
            self.ui.rmin.value(), self.dampingFunct, Fintra_r,
            self.ui.iterations.value(), scaleStep, sth, s0th,
            thickness_sampling, phi_matrix, "n")

        # ----------------------First density minimization-------------------------

        densityStep = density0 / 50
        densityStepEnd = density0 / 250

        density = Minimization.OptimizeDensity(
            self.Q, self.I_Q, self.Ibkg_Q, self.J_Q, self.Iincoh_Q, self.fe_Q,
            self.ui.maxQ.value(), self.ui.minQ.value(),
            self.ui.QmaxIntegrate.value(), self.Ztot, density0,
            scaleFactor, self.Sinf, self.ui.smoothingFactor.value(),
            self.ui.rmin.value(), self.dampingFunct, Fintra_r,
            self.ui.iterations.value(), densityStep, densityStepEnd, sth, s0th,
            thickness_sampling, phi_matrix, "n")

        # print("density0, density", density0, density)
        numLoopIteration = 0

        while 1:
            if np.abs(density - density0) > density / 25:
                # print("First")
                scaleStep = 0.006
                densityStep = density / 10
                WSamplestep = 0.0008
                WRefstep = 0.0008
            elif np.abs(density - density0) > density / 75:
                # print("Second")
                scaleStep = 0.0006
                densityStep = density / 100
                WSamplestep = 0.0002
                WRefstep = 0.0002
            else:
                # print("Third")
                scaleStep = 0.00006
                densityStep = density / 1000
                WSamplestep = 0.0001
                WRefstep = 0.0001

            scaleFactor = Minimization.OptimizeScale(
                self.Q, self.I_Q, self.Ibkg_Q, self.J_Q, self.Iincoh_Q,
                self.fe_Q, self.ui.maxQ.value(), self.ui.minQ.value(),
                self.ui.QmaxIntegrate.value(), self.Ztot, density,
                scaleFactor, self.Sinf, self.ui.smoothingFactor.value(),
                self.ui.rmin.value(), self.dampingFunct, Fintra_r,
                self.ui.iterations.value(), scaleStep, sth, s0th,
                thickness_sampling, phi_matrix, "n")

            density0 = density

            density = Minimization.OptimizeDensity(
                self.Q, self.I_Q, self.Ibkg_Q, self.J_Q, self.Iincoh_Q,
                self.fe_Q, self.ui.maxQ.value(), self.ui.minQ.value(),
                self.ui.QmaxIntegrate.value(), self.Ztot, density0,
                scaleFactor, self.Sinf, self.ui.smoothingFactor.value(),
                self.ui.rmin.value(), self.dampingFunct, Fintra_r,
                self.ui.iterations.value(), densityStep, density / 250, sth,
                s0th, thickness_sampling, phi_matrix, "n")

            numLoopIteration += 1
            # print("numLoopIteration", numLoopIteration, scaleFactor, density)
            if (np.abs(density - density0) > np.abs(
                    density / 2500)) and (numLoopIteration <= 30):
                continue
            else:
                break

        # print("final scale", scaleFactor, "final density", density)

        self.ui.scaleFactorValue.setValue(scaleFactor)
        self.ui.densityValue.setValue(density)

        Isample_Q = MainFunctions.calc_IsampleQ(self.I_Q, scaleFactor,
                                                self.Ibkg_Q)
        alpha = MainFunctions.calc_alpha(
            self.J_Q[self.Q <= self.ui.QmaxIntegrate.value()], self.Sinf,
            self.Q[self.Q <= self.ui.QmaxIntegrate.value()],
            Isample_Q[self.Q <= self.ui.QmaxIntegrate.value()],
            self.fe_Q[self.Q <= self.ui.QmaxIntegrate.value()], self.Ztot,
            density)
        Icoh_Q = MainFunctions.calc_Icoh(alpha, Isample_Q, self.Iincoh_Q)

        S_Q = MainFunctions.calc_SQ(Icoh_Q, self.Ztot, self.fe_Q, self.Sinf,
                                    self.Q, self.ui.minQ.value(),
                                    self.ui.QmaxIntegrate.value(),
                                    self.ui.maxQ.value())

        Ssmooth_Q = UtilityAnalysis.calc_SQsmoothing(
            self.Q, S_Q, self.Sinf, self.ui.smoothingFactor.value(),
            self.ui.minQ.value(), self.ui.QmaxIntegrate.value(),
            self.ui.maxQ.value())

        SsmoothDamp_Q = UtilityAnalysis.calc_SQdamp(Ssmooth_Q, self.Sinf,
                                                    self.dampingFunct)

        i_Q = MainFunctions.calc_iQ(SsmoothDamp_Q, self.Sinf)

        Qi_Q = self.Q * i_Q
        r, F_r = MainFunctions.calc_Fr(
            self.Q[self.Q <= self.ui.QmaxIntegrate.value()],
            Qi_Q[self.Q <= self.ui.QmaxIntegrate.value()])
        Fopt_r, deltaFopt_r = Optimization.calc_optimize_Fr(
            self.ui.iterations.value(), F_r, Fintra_r, density,
            i_Q[self.Q <= self.ui.QmaxIntegrate.value()],
            self.Q[self.Q <= self.ui.QmaxIntegrate.value()], self.Sinf,
            self.J_Q[self.Q <= self.ui.QmaxIntegrate.value()], r,
            self.ui.rmin.value(), "n")

        Scorr_Q = MainFunctions.calc_SQCorr(Fopt_r, r, self.Q, self.Sinf)

        self.ui.distfuncPlot.canvas.ax.plot(r,
                                            Fopt_r,
                                            "g",
                                            label=r"$F_{opt}(r)$")
        self.ui.distfuncPlot.canvas.ax.legend()
        self.ui.distfuncPlot.canvas.draw()

        self.ui.factorPlot.canvas.ax.plot(self.Q,
                                          Scorr_Q,
                                          "g",
                                          label=r"$S_{opt}(Q)$")
        self.ui.factorPlot.canvas.ax.legend()
        self.ui.factorPlot.canvas.draw()
Ejemplo n.º 5
0
def Kaplow_method(Q, I_Q, Ibkg_Q, J_Q, fe_Q, Iincoh_Q, Sinf, Ztot, scaleFactor,
                  density, Fintra_r, r, minQ, QmaxIntegrate, maxQ,
                  smoothFactor, dampFactor, iteration, rmin):
    """Function to apply the Kaplow method.

    Parameters
    ----------
    variables     : module
                    input variables setted by the user
    Q             : numpy array
                    momentum transfer (nm^-1)
    I_Q           : numpy array
                    measured scattering intensity
    Ibkg_Q        : numpy array
                    background scattering intensity
    J_Q           : numpy array
                    J(Q)
    fe_Q          : numpy array
                    effective electric form factor
    Iincoh_Q      : numpy array
                    incoherent scattering intensity
    Sinf          : float
                    value of S(Q) for Q->inf
    Ztot          : int
                    total Z number
    scaleFactor   : float
                    scale factor
    density       : float
                    average atomic density
    Fintra_r      : numpy array
                    intramolecular contribution of F(r)
    r             : numpy array
                    atomic distance (nm)

    Returns
    -------
    chi2          : float
                    chi2 value
    SsmoothDamp_Q : numpy array
                    smoothed and damped structure factor
    Fopt_r        : numpy array
                    optimized F(r)
    """

    Isample_Q = MainFunctions.calc_IsampleQ(I_Q, scaleFactor, Ibkg_Q)
    alpha = MainFunctions.calc_alpha(J_Q[Q <= QmaxIntegrate], Sinf,
                                     Q[Q <= QmaxIntegrate],
                                     Isample_Q[Q <= QmaxIntegrate],
                                     fe_Q[Q <= QmaxIntegrate], Ztot, density)
    Icoh_Q = MainFunctions.calc_Icoh(alpha, Isample_Q, Iincoh_Q)

    S_Q = MainFunctions.calc_SQ(Icoh_Q, Ztot, fe_Q, Sinf, Q, minQ,
                                QmaxIntegrate, maxQ)
    Ssmooth_Q = UtilityAnalysis.calc_SQsmoothing(Q, S_Q, Sinf, smoothFactor,
                                                 minQ, QmaxIntegrate, maxQ)
    SsmoothDamp_Q = UtilityAnalysis.calc_SQdamp(Ssmooth_Q, Q, Sinf,
                                                QmaxIntegrate, dampFactor)

    i_Q = MainFunctions.calc_iQ(SsmoothDamp_Q, Sinf)
    F_r = MainFunctions.calc_Fr(r, Q[Q <= QmaxIntegrate],
                                i_Q[Q <= QmaxIntegrate])

    Fopt_r, deltaFopt_r = Optimization.calc_optimize_Fr(
        iteration, F_r, Fintra_r, density, i_Q[Q <= QmaxIntegrate],
        Q[Q <= QmaxIntegrate], Sinf, J_Q[Q <= QmaxIntegrate], r, rmin, "n")

    chi2 = simps(deltaFopt_r[r < rmin]**2, r[r < rmin])

    return (chi2, SsmoothDamp_Q, F_r, Fopt_r)
Ejemplo n.º 6
0
def calc_optimize_Fr(iterations, F_r, Fintra_r, density, i_Q, Q, Sinf, J_Q, r,
                     rmin, plot_iter):
    """Function to calculate the F(r) optimization (eq 47, 48, 49).

    Parameters
    ----------
    iterations : int
                 number of iterations
    F_r        : numpy array
                 F(r)
    density       : float
                 atomic density
    i_Q        : numpy array
                 i(Q)
    Q          : numpy array
                 momentum transfer (nm^-1)
    Sinf       : float
                 value of S(Q) for Q->inf
    J_Q        : numpy array
                 J(Q)
    r          : numpy array
                 atomic distance (nm)
    rmin       : float
                 r cut-off value (nm)
    plot_iter  : string
                 flag to plot the F(r) iterations

    Returns
    -------
    F_r        : numpy array
                 optimazed F(r)
    deltaF_r   : numpy array
                 difference between the last F(r) and its theoretical value
    """

    # if plot_iter.lower() == "y":
    # plt.ion()
    # plt.figure("F_rIt")
    # plt.plot(r, F_r, label="F(r)")
    # plt.xlabel("r (nm)")
    # plt.ylabel("F(r)")
    # plt.legend()
    # plt.grid(True)

    for i in range(iterations):
        deltaF_r = calc_deltaFr(F_r, Fintra_r, r, density)
        i_Q[0] = 0.0
        i_Q[1:] = calc_iQi(i_Q[1:], Q[1:], Sinf, J_Q[1:], deltaF_r, r, rmin)

        r, F_r = MainFunctions.calc_Fr(Q, Q * i_Q)
        # if plot_iter.lower() == "y":
        # j = i+1
        # plt.figure("F_rIt")
        # plt.plot(r, F_r, label="%s iteration F(r)" %j)
        # plt.legend()
        # plt.draw()

        # time.sleep(1.0)

    # if plot_iter.lower() == "y":
    # plt.ioff()

    return (F_r, deltaF_r)
Ejemplo n.º 7
0
def Kaplow_methodFZ(numAtoms, variables, Q, I_Q, Ibkg_Q, aff_squared_mean,
                    aff_mean_squared, Iincoh_Q, sf, rho0, Fintra_r, r):
    """Function to apply the Kaplow method with Waseda FZ formalism.

    Parameters
    ----------
    numAtoms  : int
                number of atoms in the molecule
    variables : module
                input variables setted by the user
    Q         : numpy array
                momentum transfer (nm^-1)
    I_Q       : numpy array
                measured scattering intensity
    Ibkg_Q    : numpy array
                background scattering intensity
    aff_squared_mean  : numpy array
                        mean of the squared form factor: <f^2>
    aff_mean_squared : numpy array
                       squared of the mean form factor: <f>^2
    Iincoh_Q : numpy array
               incoherent scattering intensity
    sf       : float
               scale factor
    rho0     : float
               average atomic density
    Fintra_r : numpy array
               intramolecular contribution of F(r)
    r        : numpy array
               atomic distance (nm)

    Returns
    -------
    chi2     : float
               chi2 value
    """

    Isample_Q = MainFunctions.calc_IsampleQ(I_Q, sf, Ibkg_Q)
    alpha = Formalism.calc_alphaFZ(Q, Isample_Q, Iincoh_Q, rho0,
                                   aff_squared_mean, aff_mean_squared)
    Icoh_Q = MainFunctions.calc_Icoh(alpha, Isample_Q, Iincoh_Q)

    S_Q = Formalism.calc_SFZ_Q(Q, Icoh_Q, aff_squared_mean, aff_mean_squared, variables.minQ, \
        variables.QmaxIntegrate, variables.maxQ)
    S_Qsmoothed = UtilityAnalysis.calc_SQsmoothing(Q, S_Q, 1, variables.smooth_factor, \
        variables.minQ, variables.QmaxIntegrate, variables.maxQ)
    S_QsmoothedDamp = UtilityAnalysis.calc_SQdamp(S_Qsmoothed, Q, 1, \
        variables.QmaxIntegrate, variables.damping_factor)

    i_Q = MainFunctions.calc_iQ(S_QsmoothedDamp, 1)
    F_r = MainFunctions.calc_Fr(r, Q[Q<=variables.QmaxIntegrate], \
        i_Q[Q<=variables.QmaxIntegrate])

    # J_Q = Iincoh_Q/aff_mean_squared

    F_rIt, deltaF_rIt = Formalism.calc_optimize_FrFZ(variables.iteration, F_r, \
        Fintra_r, rho0, i_Q[Q<=variables.QmaxIntegrate], Q[Q<=variables.QmaxIntegrate], \
        Iincoh_Q[Q<=variables.QmaxIntegrate], r, variables.rmin)

    chi2 = simps(deltaF_rIt[r < variables.rmin]**2, r[r < variables.rmin])

    return (chi2, S_QsmoothedDamp, F_rIt)
Ejemplo n.º 8
0
    Sinf = MainFunctions.calc_Sinf(elementList, fe_Q, Q, Ztot,
                                   elementParameters)

    dampingFunction = UtilityAnalysis.calc_dampingFunction(
        Q, variables.dampingFactor, variables.QmaxIntegrate,
        variables.typeFunction)

    # ------------------Intra-molecular components-----------------------------

    iintra_Q = Optimization.calc_iintra(Q, fe_Q, Ztot, variables.QmaxIntegrate,
                                        variables.maxQ, elementList, element,
                                        x, y, z, elementParameters)
    iintradamp_Q = UtilityAnalysis.calc_iintradamp(iintra_Q, dampingFunction)
    Qiintradamp_Q = Q * iintradamp_Q
    rintra, Fintra_r = MainFunctions.calc_Fr(
        Q[Q <= variables.QmaxIntegrate],
        Qiintradamp_Q[Q <= variables.QmaxIntegrate])

    # _, Fintra_r = UtilityAnalysis.rebinning(rintra, Fintra_r, np.amin(Fintra_r),
    # np.amax(Fintra_r), 8192)

    # _, dampingFunction = UtilityAnalysis.rebinning(Q, dampingFunction, 0.0,
    # variables.maxQ, variables.NumPoints)

    # ---------------------Geometrical correction------------------------------

    absCorrFactor = IgorFunctions.absorption(Q)
    I_Q = I_Q / (absCorrFactor)
    Ibkg_Q = Ibkg_Q / (absCorrFactor)

    # ------------------------Starting minimization----------------------------
Ejemplo n.º 9
0
    Sinf = MainFunctions.calc_Sinf(elementList, fe_Q, Q, Ztot,
                                   elementParameters)
    dampingFunction = UtilityAnalysis.calc_dampingFunction(
        Q, inputVariables["dampingFactor"], inputVariables["QmaxIntegrate"],
        inputVariables["typeFunction"])

    #-------------------Intra-molecular components-----------------------------

    iintra_Q = Optimization.calc_iintra(
        Q, fe_Q, Ztot, inputVariables["QmaxIntegrate"], inputVariables["maxQ"],
        elementList, elementPosition["element"], elementPosition["x"],
        elementPosition["y"], elementPosition["z"], elementParameters)
    iintradamp_Q = UtilityAnalysis.calc_iintradamp(iintra_Q, dampingFunction)
    Qiintradamp_Q = Q * iintradamp_Q
    rintra, Fintra_r = MainFunctions.calc_Fr(
        Q[Q <= inputVariables["QmaxIntegrate"]],
        Qiintradamp_Q[Q <= inputVariables["QmaxIntegrate"]])

    # ---------------------Geometrical correction------------------------------

    # global phi_matrix
    # global thickness_sampling

    if inputVariables["mccFlag"].lower() == "y":
        print("Start matrix calculation or reading")
        thickness_sampling, phi_matrix = Geometry.check_phi_matrix(
            Q, inputVariables["ws1"], inputVariables["ws2"],
            inputVariables["r1"], inputVariables["r2"], inputVariables["d"],
            inputVariables["phiMatrixCalcFlag"],
            inputVariables["phiMatrixPath"])
        print("End matrix calculation or reading")
Ejemplo n.º 10
0
def OptimizeScale(Q, I_Q, Ibkg_Q, J_Q, Iincoh_Q, fe_Q, minQ, QmaxIntegrate,
                  maxQ, Ztot, density, scaleFactor, Sinf, smoothingFactor,
                  rmin, dampingFunction, Fintra_r, iterations, scaleStep, sth,
                  s0th, mccFlag, thickness_sampling, phi_matrix):
    """Function for the scale factor optimization.

    Q                  : numpy array
                         momentum transfer (nm^-1)
    I_Q                : numpy array
                         measured scattering intensity 
    Ibkg_Q             : numpy array
                         background scattering intensity
    J_Q                : numpy array
                         J(Q)
    Iincoh_Q           : numpy array
                         incoherent scattering intensity
    fe_Q               : numpy array
                         effective electric form factor
    minQ               : float
                         minimum Q value
    QmaxIntegrate      : float
                         maximum Q value for the intagrations
    maxQ               : float
                         maximum Q value
    Ztot               : int
                         total Z number
    density            : float
                         average atomic density
    scaleFactor        : float
                         scale factor
    Sinf               : float
                         value of S(Q) for Q->inf
    smoothingFactor    : float
                         smoothing factor
    rmin               : float
                         r cut-off value (nm)
    dampingFunction    : numpy array
                         damping function
    Fintra_r           : numpy array
                         intramolecular contribution of F(r)
    iterations         : int
                         number of iterations
    scaleStep
    sth
    s0th
    MCC_flag
    """

    numSample = 23

    if mccFlag.lower() == "y":
        T_MCC_sth, T_MCC_corr_factor_bkg = Geometry.MCC_correction(
            sth, s0th, thickness_sampling, phi_matrix)

        # print(thickness_sampling)
        # plt.contour(phi_matrix)
        # plt.show()

        # x = np.linspace(0, len(T_MCC_sth), len(T_MCC_sth), endpoint=True)
        # _, T_MCC_sth = UtilityAnalysis.rebinning(x, T_MCC_sth, np.amin(T_MCC_sth),
        #     np.amax(T_MCC_sth), len(I_Q))

        # plt.plot(T_MCC_sth)
        # plt.show()

        I_Q = I_Q / T_MCC_sth
        Ibkg_Q = Ibkg_Q * T_MCC_corr_factor_bkg / (T_MCC_sth)

    flag = 0
    # Loop for the range shifting
    while 1:
        # print(type(scaleFactor))
        # print(type(scaleStep))
        scaleArray = UtilityAnalysis.makeArrayLoop(scaleFactor, scaleStep)
        chi2Array = np.zeros(numSample)
        flag += 1
        print("iter flag ", flag)

        for i in range(numSample):
            print("sample ", i, scaleArray[i])
            # ------------------Kaplow method for scale--------------------

            Isample_Q = MainFunctions.calc_IsampleQ(I_Q, scaleArray[i], Ibkg_Q)
            alpha = MainFunctions.calc_alpha(J_Q[Q <= QmaxIntegrate], Sinf,
                                             Q[Q <= QmaxIntegrate],
                                             Isample_Q[Q <= QmaxIntegrate],
                                             fe_Q[Q <= QmaxIntegrate], Ztot,
                                             density)
            Icoh_Q = MainFunctions.calc_Icoh(alpha, Isample_Q, Iincoh_Q)

            S_Q = MainFunctions.calc_SQ(Icoh_Q, Ztot, fe_Q, Sinf, Q, minQ,
                                        QmaxIntegrate, maxQ)

            Ssmooth_Q = UtilityAnalysis.calc_SQsmoothing(
                Q, S_Q, Sinf, smoothingFactor, minQ, QmaxIntegrate, maxQ)

            SsmoothDamp_Q = UtilityAnalysis.calc_SQdamp(
                Ssmooth_Q, Sinf, dampingFunction)

            i_Q = MainFunctions.calc_iQ(SsmoothDamp_Q, Sinf)

            Qi_Q = Q * i_Q
            r, F_r = MainFunctions.calc_Fr(Q[Q <= QmaxIntegrate],
                                           Qi_Q[Q <= QmaxIntegrate])

            Fopt_r, deltaFopt_r = Optimization.calc_optimize_Fr(
                iterations, F_r, Fintra_r, density, i_Q[Q <= QmaxIntegrate],
                Q[Q <= QmaxIntegrate], Sinf, J_Q[Q <= QmaxIntegrate], r, rmin,
                "n")

            deltaFopt_r[np.where(r >= rmin)] = 0.0  # Igor version
            chi2Array[i] = np.mean(deltaFopt_r**2)  # Igor version
            # Fth_r = Fintra_r - 4*np.pi*r*density
            # plt.plot(r, Fth_r)
            # plt.show()
            # print(Fth_r)
            # print(Fintra_r)
            # chi2Array[i] = simps(deltaFopt_r[np.where((r>0)&(r<rmin))]**2)

        # --------------------Range shifting selection --------------------

        plt.scatter(scaleArray, chi2Array)
        plt.grid(True)
        plt.show()

        maxLimit = 10**5
        if np.amax(chi2Array) >= maxLimit:
            scaleArray = scaleArray[0:np.argmax(chi2Array >= maxLimit)]
            chi2Array = chi2Array[0:np.argmax(chi2Array >= maxLimit)]

        if np.argmin(chi2Array) < 6:
            scaleFactor -= scaleStep * 10
        if np.argmin(chi2Array) > 16:
            scaleFactor += scaleStep * 10

        if (np.argmin(chi2Array) >= 6 and np.argmin(chi2Array) <= 16):
            break
        else:
            continue

    # ------------------------chi2 curve fit for scale-------------------------
    # plt.ioff()

    xFit, yFit, scaleFactor, chi2 = IgorFunctions.chi2Fit(
        scaleFactor, scaleArray, chi2Array)

    print("final scale factor", scaleFactor)

    plt.scatter(scaleArray, chi2Array)
    plt.plot(xFit, yFit)
    plt.grid(True)
    plt.show()

    return scaleFactor
Ejemplo n.º 11
0
def OptimizeThicknessRef(Q, I_Q, Ibkg_Q, J_Q, Iincoh_Q, fe_Q, maxQ, minQ,
                         QmaxIntegrate, Ztot, density, scaleFactor, sth, s0th,
                         Sinf, smoothingFactor, rmin, dampingFunction,
                         Fintra_r, iterations, s0thStep, ws1, ws2, r1, r2, d,
                         phi_matrix_flag):
    """Function for the thickness optimization.
    """

    Flag = 0
    NoPeak = 0
    s0th = max(s0th - s0thStep * 11, 0.0)
    s0thStepEnd = 0.0006
    numSample = 23

    # Loop for the range shifting
    while 1:
        s0thArray = IgorFunctions.makeArrayLoop(s0th, s0thStep)
        chi2Array = np.zeros(numSample)

        for i in range(numSample):

            # ------------------Kaplow method for scale--------------------

            T_MCC_sth, T_MCC_corr_factor_bkg = Geometry.MCCCorrection(
                sth, s0thArray[i], thickness_sampling, phi_matrix)

            I_Q = I_Q / T_MCC_sth
            Ibkg_Q = Ibkg_Q * T_MCC_corr_factor_bkg / (T_MCC_sth)

            Isample_Q = MainFunctions.calc_IsampleQ(I_Q, scaleFactor, Ibkg_Q)
            alpha = MainFunctions.calc_alpha(J_Q[Q <= QmaxIntegrate], Sinf,
                                             Q[Q <= QmaxIntegrate],
                                             Isample_Q[Q <= QmaxIntegrate],
                                             fe_Q[Q <= QmaxIntegrate], Ztot,
                                             density)
            Icoh_Q = MainFunctions.calc_Icoh(alpha, Isample_Q, Iincoh_Q)

            S_Q = MainFunctions.calc_SQ(Icoh_Q, Ztot, fe_Q, Sinf, Q, minQ,
                                        QmaxIntegrate, maxQ)

            Ssmooth_Q = UtilityAnalysis.calc_SQsmoothing(
                Q, S_Q, Sinf, smoothingFactor, minQ, QmaxIntegrate, maxQ)

            SsmoothDamp_Q = UtilityAnalysis.calc_SQdamp(
                Ssmooth_Q, Sinf, dampingFunction)

            i_Q = MainFunctions.calc_iQ(SsmoothDamp_Q, Sinf)

            Qi_Q = Q * i_Q
            r, F_r = MainFunctions.calc_Fr(Q[Q <= QmaxIntegrate],
                                           Qi_Q[Q <= QmaxIntegrate])

            Fopt_r, deltaFopt_r = Optimization.calc_optimize_Fr(
                iterations, F_r, Fintra_r, density, i_Q[Q <= QmaxIntegrate],
                Q[Q <= QmaxIntegrate], Sinf, J_Q[Q <= QmaxIntegrate], r, rmin,
                "n")

            deltaFopt_r[np.where(r >= rmin)] = 0.0
            chi2Array[i] = np.mean(deltaFopt_r**2)

        # --------------------Range shifting selection --------------------

        if np.amax(chi2Array) > 10**8:
            s0thIdx = np.argmin(chi2Array[0:np.argmax(chi2Array)])
        else:
            s0thIdx = np.argmin(chi2Array)

        s0th = s0thArray[s0thIdx] - s0thStep * 1.1
        nearIdx, nearEl = UtilityAnalysis.find_nearest(s0thArray, s0th)

        if nearIdx == 0:
            print("out1")
            s0th -= s0thStep * 10
            s0thStep *= 10
            NoPeak += 1
        if nearIdx >= numSample - 2:
            print("out2")
            s0th += s0thStep * 10
            s0thStep *= 10
            NoPeak += 1

        s0thStep /= 10
        Flag += 1

        if ((10 * s0thStep > s0thStepEnd) and (NoPeak < 5)):
            continue
        else:
            break

    # ------------------------chi2 curve fit for scale-------------------------

    xFit, yFit, sth, chi2 = IgorFunctions.chi2Fit(s0th, s0thArray, chi2Array)

    print("final sample thickness ref", s0th)

    return s0th
Ejemplo n.º 12
0
def OptimizeDensity(Q, I_Q, Ibkg_Q, J_Q, Iincoh_Q, fe_Q, minQ, QmaxIntegrate,
                    maxQ, Ztot, density, scaleFactor, Sinf, smoothingFactor,
                    rmin, dampingFunction, Fintra_r, iterations, densityStep,
                    sth, s0th, mccFlag, thickness_sampling, phi_matrix):
    """Function for the density optimization.

    Q                  : numpy array
                         momentum transfer (nm^-1)
    I_Q                : numpy array
                         measured scattering intensity 
    Ibkg_Q             : numpy array
                         background scattering intensity
    J_Q                : numpy array
                         J(Q)
    Iincoh_Q           : numpy array
                         incoherent scattering intensity
    fe_Q               : numpy array
                         effective electric form factor
    minQ               : float
                         minimum Q value
    QmaxIntegrate      : float
                         maximum Q value for the intagrations
    maxQ               : float
                         maximum Q value
    Ztot               : int
                         total Z number
    density            : float
                         average atomic density
    scaleFactor        : float
                         scale factor
    Sinf               : float
                         value of S(Q) for Q->inf
    smoothingFactor    : float
                         smoothing factor
    rmin               : float
                         r cut-off value (nm)
    dampingFunction    : numpy array
                         damping function
    Fintra_r           : numpy array
                         intramolecular contribution of F(r)
    iterations         : int
                         number of iterations
    scaleStep
    sth
    s0th
    thickness_sampling : float
    phi_matrix
    MCC_flag
    """

    numSample = 23

    if mccFlag.lower() == "y":
        T_MCC_sth, T_MCC_corr_factor_bkg = Geometry.MCC_correction(
            sth, s0th, thickness_sampling, phi_matrix)

        I_Q = I_Q / T_MCC_sth
        Ibkg_Q = Ibkg_Q * T_MCC_corr_factor_bkg / (T_MCC_sth)

    flag = 0
    # Loop for the range shifting
    while 1:
        flag += 1
        print("iter flag ", flag)

        densityArray = UtilityAnalysis.makeArrayLoop(density, densityStep)
        chi2Array = np.zeros(numSample)

        for i in range(numSample):
            print("sample ", i, densityArray[i])

            # ------------------Kaplow method for scale--------------------

            Isample_Q = MainFunctions.calc_IsampleQ(I_Q, scaleFactor, Ibkg_Q)
            alpha = MainFunctions.calc_alpha(J_Q[Q <= QmaxIntegrate], Sinf,
                                             Q[Q <= QmaxIntegrate],
                                             Isample_Q[Q <= QmaxIntegrate],
                                             fe_Q[Q <= QmaxIntegrate], Ztot,
                                             densityArray[i])
            Icoh_Q = MainFunctions.calc_Icoh(alpha, Isample_Q, Iincoh_Q)

            S_Q = MainFunctions.calc_SQ(Icoh_Q, Ztot, fe_Q, Sinf, Q, minQ,
                                        QmaxIntegrate, maxQ)

            Ssmooth_Q = UtilityAnalysis.calc_SQsmoothing(
                Q, S_Q, Sinf, smoothingFactor, minQ, QmaxIntegrate, maxQ)

            SsmoothDamp_Q = UtilityAnalysis.calc_SQdamp(
                Ssmooth_Q, Sinf, dampingFunction)

            i_Q = MainFunctions.calc_iQ(SsmoothDamp_Q, Sinf)

            Qi_Q = Q * i_Q
            r, F_r = MainFunctions.calc_Fr(Q[Q <= QmaxIntegrate],
                                           Qi_Q[Q <= QmaxIntegrate])

            # r, F_r = MainFunctions.calc_Fr(Q[Q<=QmaxIntegrate],
            #     Qi_Q[Q<=QmaxIntegrate])

            Fopt_r, deltaFopt_r = Optimization.calc_optimize_Fr(
                iterations, F_r, Fintra_r, densityArray[i],
                i_Q[Q <= QmaxIntegrate], Q[Q <= QmaxIntegrate], Sinf,
                J_Q[Q <= QmaxIntegrate], r, rmin, "n")

            deltaFopt_r[np.where(r >= rmin)] = 0.0
            chi2Array[i] = np.mean(deltaFopt_r**2)

        # --------------------Range shifting selection --------------------

        # densityIdx = np.argmin(chi2Array)
        # density = densityArray[densityIdx] - densityStep*1.1

        # nearIdx, nearEl = UtilityAnalysis.find_nearest(densityArray, density)

        plt.scatter(densityArray, chi2Array)
        plt.grid(True)
        plt.show()

        maxLimit = 10**5
        if np.amax(chi2Array) >= maxLimit:
            densityArray = densityArray[0:np.argmax(chi2Array >= maxLimit)]
            chi2Array = chi2Array[0:np.argmax(chi2Array >= maxLimit)]

        # print(np.argmin(chi2Array))

        if np.argmin(chi2Array) < 6:
            # print("bug")
            density -= densityStep * 10
        if np.argmin(chi2Array) > 16:
            density += densityStep * 10

        if (np.argmin(chi2Array) >= 6 and np.argmin(chi2Array) <= 16):
            break
        else:
            continue

    # ------------------------chi2 curve fit for scale-------------------------

    #xFit, yFit, density, chi2Min = chi2Fit(densityArray, chi2Array)
    xFit, yFit, density, chi2 = IgorFunctions.chi2Fit(density, densityArray,
                                                      chi2Array)

    print("final density", density)

    plt.scatter(densityArray, chi2Array)
    plt.plot(xFit, yFit)
    plt.grid(True)
    plt.show()

    return density