def S_QCalculation(Q, I_Q, Ibkg_Q, scaleFactor, J_Q, Sinf, fe_Q, Ztot, density, Iincoh_Q, minQ, QmaxIntegrate, maxQ, smoothFactor, dampFactor): """Function for the S(Q) calculation. """ 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) return SsmoothDamp_Q
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()
def SQ(self): """Function to calculate and plot the structure factor S(Q)""" self.elementList = Utility.molToElemList(self.molecule) # self.elementList = Utility.molToElemList("Ar") self.elementParameters = Utility.read_parameters( self.elementList, "./elementParameters.txt") # print(elementList) # print(elementParameters) self.Q, self.I_Q, self.Qbkg, self.Ibkg_Q = UtilityAnalysis.check_data_length( self.Q, self.I_Q, self.Qbkg, self.Ibkg_Q, self.ui.minQ.value(), self.ui.maxQ.value()) two_theta = UtilityAnalysis.Qto2theta(self.Q) absCorrFactor = Geometry.calcAbsCorrection( self.ui.absLength.value(), two_theta, self.ui.dacThickness.value(), self.ui.dacAngle.value()) self.I_Q = self.I_Q / absCorrFactor self.Ibkg_Q = self.Ibkg_Q / absCorrFactor self.fe_Q, self.Ztot = MainFunctions.calc_eeff(self.elementList, self.Q, self.elementParameters) self.Iincoh_Q = MainFunctions.calc_Iincoh(self.elementList, self.Q, self.elementParameters) self.J_Q = MainFunctions.calc_JQ(self.Iincoh_Q, self.Ztot, self.fe_Q) self.Sinf = MainFunctions.calc_Sinf(self.elementList, self.fe_Q, self.Q, self.Ztot, self.elementParameters) self.dampingFunct = UtilityAnalysis.calc_dampingFunction( self.Q, self.ui.dampingFactor.value(), self.ui.QmaxIntegrate.value(), self.ui.dampingFunction.currentText()) Isample_Q = MainFunctions.calc_IsampleQ( self.I_Q, self.ui.scaleFactorValue.value(), 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, self.ui.densityValue.value()) 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()) self.SsmoothDamp_Q = UtilityAnalysis.calc_SQdamp( Ssmooth_Q, self.Sinf, self.dampingFunct) # self.ui.factorPlot.canvas.ax.plot(self.Q, self.SsmoothDamp_Q, "b", label=r"$S(Q)$") # self.ui.factorPlot.canvas.ax.legend() # self.ui.factorPlot.canvas.draw() return self.SsmoothDamp_Q
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)
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)
# _, 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---------------------------- scaleFactor = variables.scaleFactor density = variables.density smoothingFactor = 0.2 Isample_Q = MainFunctions.calc_IsampleQ(I_Q, scaleFactor, Ibkg_Q) alpha = MainFunctions.calc_alpha(J_Q[Q <= variables.QmaxIntegrate], Sinf, Q[Q <= variables.QmaxIntegrate], Isample_Q[Q <= variables.QmaxIntegrate], fe_Q[Q <= variables.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, variables.minQ, variables.QmaxIntegrate, variables.maxQ) Ssmooth_Q = UtilityAnalysis.calc_SQsmoothing(Q, S_Q, Sinf, smoothingFactor, variables.minQ, variables.QmaxIntegrate, variables.maxQ)
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
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
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