def test_sqe_normalization(): # We need some lines L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0), x0=0.0, width=0.4, c=0.0, weight=2) L2 = LorentzianLine(name="Lorentzian2", domain=(-5.0, 5.0), x0=-1.0, width=0.4, c=0.02, weight=1) # Contruct a SqE model sqe = SqE(lines=(L1, L2), lam=6.0, dlam=0.12, l_SD=3.43, T=20) new_domain = (-1 * energy_from_lambda(6.0), UPPER_INTEGRATION_LIMIT) sqe.update_domain(new_domain) # integrate over domain from scipy.integrate import quad to_integrate = lambda x: sqe(x) valdom, errdom = quad(to_integrate, *new_domain) valover, errover = quad(to_integrate, -15, 20) print( f"Integration value over domain from {new_domain[0]:.5f} to {UPPER_INTEGRATION_LIMIT}: {valdom:.5f} +- {errdom:.5f}" ) # | normalization factor: {n:.5f}") print( f"Integration value beyond domain from -15.0 to 20.0: {valover:.5f} +- {errover:.5f}" )
def test_DetectorEfficiency_cancel(): # We need some lines L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0), x0=0.0, width=0.4, c=0.0, weight=2) L2 = LorentzianLine(name="Lorentzian2", domain=(-5.0, 5.0), x0=-1.0, width=0.4, c=0.02, weight=1) # Contruct a SqE model sqe = SqE(lines=(L1, L2), lam=6.0, dlam=0.12, lSD=3.43, T=20) new_domain = (-1 * energy_from_lambda(6.0), UPPER_INTEGRATION_LIMIT) sqe.update_domain(new_domain) # init energycutoff decf = DetectorEfficiencyCorrectionFactor(sqe, ne=10, nlam=5) ee, ll = energy_lambda_nrange(15.0, 6.0, 0.12, 10000, 20) # print(detector_efficiency(ee, ll, 1) * decf(ee, ll)) print( trapz(trapz(decf(ee, ll) * decf.legacy_calc(ee, ll, 0), ee, axis=0), ll[0])) # print(trapz(trapz(ones(ll.shape), ee, axis=0), ll[0])) print(trapz(trapz(decf.legacy_calc(ee, ll, 1), ee, axis=0), ll[0])) print(trapz(trapz(decf.legacy_calc(ee, ll, 0), ee, axis=0), ll[0])) print( trapz( trapz(decf.legacy_calc(ee, ll, 0), ee, axis=0) / trapz(trapz(decf.legacy_calc(ee, ll, 1), ee, axis=0), ll[0]), ll[0]))
def test_export_load(): # We need some lines L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0), x0=0.0, width=0.4, c=0.0, weight=2) L2 = LorentzianLine(name="Lorentzian2", domain=(-5.0, 5.0), x0=-1.0, width=0.4, c=0.02, weight=1) # Contruct a SqE model sqe = SqE(lines=(L1, L2), lam=6.0, dlam=0.12, lSD=3.43, T=20) new_domain = (-1 * energy_from_lambda(6.0), UPPER_INTEGRATION_LIMIT) sqe.update_domain(new_domain) # init energycutoff decf = DetectorEfficiencyCorrectionFactor(sqe, ne=10000, nlam=20) # exports corrdict = decf.export_to_dict() decf.export_to_jsonfile( f"{testdir}/resources/test_correction_export_load_file.json") # loading decf_from_dict = decf.load_from_dict(**corrdict) print(decf_from_dict.export_to_dict()) print("", "Loading successful: ", decf, decf_from_dict, sep='\n') decf_from_jsonfile = decf.load_from_jsonfile( f"{testdir}/resources/test_correction_export_load_file.json") print("Loading successful: ", decf, decf_from_jsonfile, sep='\n')
def test_EnergyCutoffCorrectionFactor(): # We need some lines L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0), x0=0.0, width=0.4, c=0.0, weight=2) L2 = LorentzianLine(name="Lorentzian2", domain=(-5.0, 5.0), x0=-1.0, width=0.4, c=0.02, weight=1) # Contruct a SqE model sqe = SqE(lines=(L1, L2), lam=6.0, dlam=0.12, lSD=3.43, T=20) new_domain = (-1 * energy_from_lambda(6.0), UPPER_INTEGRATION_LIMIT) sqe.update_domain(new_domain) # init energycutoff eccf = EnergyCutOffCorrectionFactor(sqe, ne=10000, nlam=20) ne = 10000 nlam = 5 lam = 6.0 * linspace(1 - 0.12 * 1.01, 1 + 0.12 * 1.01, nlam) lams = tile(lam, (ne, 1)) a = -0.99999 * energy_from_lambda(lam) b = 15.0 + a es = linspace(a, b, ne) ### Calculate the trapz integral over the S(q,E) # Only over domain (interval length: 15 meV) I_over_dom_only = trapz(sqe(es[:, 2]), es[:, 2]) print("Trapz integration over the domain.") print(f"Interval {a[2]:.4f} - {b[2]:.4f} -> length {b[2]-a[2]:.4f} meV.") print(f"#Steps = {ne}") print(f"Integral value: {I_over_dom_only:.4f}") # plt.plot(es[:,2], sqe(es[:,2]), label="Over domain only") # plt.show() # Beyond domain same array length es_same_length = linspace(-UPPER_INTEGRATION_LIMIT, UPPER_INTEGRATION_LIMIT, ne) I_beyond_dom_same_length = trapz(sqe(es_same_length), es_same_length) print("\nTrapz integration beyond the domain with varrying stepsize.") print( f"Interval {-UPPER_INTEGRATION_LIMIT} - {UPPER_INTEGRATION_LIMIT} -> length {30.0} meV." ) print(f"#Steps = {ne}") print(f"Integral value: {I_beyond_dom_same_length:.4f}") # plt.plot(es_same_length, sqe(es_same_length), ls="--", label="Beyond domain ne=10000") # plt.show() # Beyond domain same step size es_same_stepsize = arange(-UPPER_INTEGRATION_LIMIT, UPPER_INTEGRATION_LIMIT + 0.001, 15e-3) I_beyond_dom_same_stepsize = trapz(sqe(es_same_stepsize), es_same_stepsize) print("\nTrapz integration beyond the domain with varrying stepsize.") print( f"Interval {-UPPER_INTEGRATION_LIMIT} - {UPPER_INTEGRATION_LIMIT} -> length {30.0} meV." ) print(f"#Steps = {30.0 / 0.015}") print(f"Integral value: {I_beyond_dom_same_stepsize:.4f}")