def test2(): lambdas = np.arange(0.05, 5.5, 0.005) T = 300 texture_model = xopm.MarchDollase() texture_model.r[(0, 1, 1)] = 2 texture_model.beta[(0, 1, 1)] = np.deg2rad(60.) calc = xscalc.XSCalculator(fccAl, T, texture_model, max_diffraction_index=8) calc.xs_coh_el(1.5) coh_el_xs = calc.xs_coh_el(lambdas) inc_el_xs = calc.xs_inc_el(lambdas) inel_xs = calc.xs_inel(lambdas) abs_xs = calc.xs_abs(lambdas) coh_inel_xs = calc.xs_coh_inel(lambdas) inc_inel_xs = calc.xs_inc_inel(lambdas) total = calc.xs(lambdas) for i, l in enumerate(lambdas): assert np.isclose(calc.xs_coh_el(l), coh_el_xs[i]) assert np.isclose(calc.xs_inc_el(l), inc_el_xs[i]) assert np.isclose(calc.xs_inel(l), inel_xs[i]) assert np.isclose(calc.xs_abs(l), abs_xs[i]) assert np.isclose(calc.xs_coh_inel(l), coh_inel_xs[i]) assert np.isclose(calc.xs_inc_inel(l), inc_inel_xs[i]) assert np.isclose(calc.xs(l), total[i]) continue return
def test_fccNi(): lambdas = np.arange(0.05, 5.5, 0.001) T = 300 texture_model = xopm.MarchDollase() texture_model.r[(0, 0, 1)] = 2 texture_model.beta[(0, 0, 1)] = 1. calc = xscalc.XSCalculator(fccNi, T, texture_model) coh_el_xs = calc.xs_coh_el(lambdas) # coh_el_xs = [calc.xs_coh_el(l) for l in lambdas] data = np.array([lambdas, coh_el_xs]) # expected = np.load('expected/fccNi-coh-el-xs.npy') # assert np.isclose(data, expected).all() inc_el_xs = calc.xs_inc_el(lambdas) inel_xs = calc.xs_inel(lambdas) abs_xs = calc.xs_abs(lambdas) if interactive: from matplotlib import pyplot as plt plt.plot(lambdas, coh_el_xs) plt.plot(lambdas, inc_el_xs) plt.plot(lambdas, inel_xs) plt.plot(lambdas, abs_xs) plt.plot(lambdas, abs_xs + coh_el_xs + inc_el_xs + inel_xs) plt.show() return
def test_fccAl(): lambdas = np.arange(0.05, 5.5, 0.005) T = 300 # if max_diffraction_index is too small, the low wavelength portion will be a bit off calc = xscalc.XSCalculator(fccAl, T, max_diffraction_index=15) coh_el_xs = calc.xs_coh_el(lambdas) inc_el_xs = calc.xs_inc_el(lambdas) inel_xs = calc.xs_inel(lambdas) abs_xs = calc.xs_abs(lambdas) coh_inel_xs = calc.xs_coh_inel(lambdas) inc_inel_xs = calc.xs_inc_inel(lambdas) total = calc.xs(lambdas) data = np.array([lambdas, total]) # np.save(os.path.join(thisdir, 'expected', 'fccAl-total-xs.npy'), data) expected = np.load(os.path.join(thisdir, 'expected', 'fccAl-total-xs.npy')) assert np.isclose(data, expected).all() if interactive: from matplotlib import pyplot as plt plt.plot(lambdas, coh_el_xs, label='coh el') plt.plot(lambdas, inc_el_xs, label='inc el') plt.plot(lambdas, coh_inel_xs, label='coh inel') plt.plot(lambdas, inc_inel_xs, label='inc inel') # plt.plot(lambdas, inel_xs, label='inel') plt.plot(lambdas, abs_xs, label='abs') plt.plot(lambdas, total, label='total') plt.ylim(-0.2, None) plt.xlim(0, 7) plt.legend() plt.show() return
def test(): lambdas = np.arange(0.05, 5.5, 0.001) T = 300 calc = xscalc.XSCalculator(NiCr, T, max_diffraction_index=1) assert np.isclose(calc.coh_xs, ((10.3+3.635)/2)**2*4*np.pi/100) assert np.isclose(calc.inc_xs, 4*np.pi*((10.3**2+3.635**2)/2.-((10.3+3.635)/2.)**2)/100. + (5.2+1.83)/2) return
def test_fccNi_onepeak(): lambdas = np.arange(0.05, 5.5, 0.001) T = 300 calc = xscalc.XSCalculator(fccNi, T) xs = calc.xs_coh_el__peak(lambdas, calc.diffpeaks[6]) if interactive: from matplotlib import pyplot as plt plt.plot(lambdas, xs) plt.show() return
def test_fccNi(): lambdas = np.arange(0.05, 5.5, 0.001) T = 300 calc = xscalc.XSCalculator(fccNi, T, size=10e-6) xs1 = [calc.xs(l) for l in lambdas] xs = calc.xs(lambdas) assert np.allclose(xs, xs1) if interactive: print("plotting...") from matplotlib import pyplot as plt plt.plot(lambdas, xs) plt.show() return
def test(): lambdas = np.arange(0.05, 5.5, 0.001) T = 300 calc = xscalc.XSCalculator(mat, T) xs = [calc.xs(l) for l in lambdas] # np.save('expected/NiFeCrMo-xs.npy', np.array([lambdas, xs]).T) expected = np.load(os.path.join(thisdir, 'expected', 'NiFeCrMo-xs.npy')) assert np.allclose(expected[:, 1], xs) if interactive: from matplotlib import pyplot as plt plt.plot(lambdas, xs) plt.show() return
def test4(): # cross section from R wavelengths = np.arange(0.05, 5.5, 0.001) T = 300 from bem import xscalc calc = xscalc.XSCalculator(fccNi, T, max_diffraction_index=3) coh_el_xs = [calc.xs_coh_el(l) for l in wavelengths] # add texture from bem.texture import texture2R lambdas, Rs = texture2R.read_results(Rsamples_file) hkls = [eval(l) for l in open('hkls.txt').readlines()] from bem.texture.InterpolatedXOPM import InterpolatedXOPM texture_model = InterpolatedXOPM(hkls, lambdas, Rs) calc2 = xscalc.XSCalculator(fccNi, T, texture_model, max_diffraction_index=3) coh_el_xs2 = [calc2.xs_coh_el(l) for l in wavelengths] from matplotlib import pyplot as plt plt.figure() plt.plot(wavelengths, coh_el_xs, label='no texture') plt.plot(wavelengths, coh_el_xs2, label='with texture') plt.legend() plt.savefig("coh_el.png") plt.close() return
def d_hkl(scatterer): scat = bem.matter.loadCif('{}.cif'.format(scatterer)) material = scat xsc = xscalc.XSCalculator(material, 300) peaks = xsc.diffpeaks d = [p.d for p in peaks] hkl = [p.hkl for p in peaks] F = [p.F for p in peaks] d = np.hstack(d) F = np.hstack(F) # hkl=np.hstack(hkl) return (d, hkl, F)
def test_fccNi2(): lambdas = np.arange(0.05, 5.5, 0.001) T = 300 calc = xscalc.XSCalculator(fccNi, T) # coherent coh_el_xs = calc.xs_coh_el(lambdas) data = np.array([lambdas, coh_el_xs]) expected = np.load(os.path.join(thisdir, 'expected', 'fccNi-coh-el-xs.npy')) # total assert np.isclose(data, expected).all() xs = calc.xs(lambdas) if interactive: from matplotlib import pyplot as plt plt.plot(lambdas, xs) plt.show() return
def test_fccNi(): lambdas = np.arange(0.05, 5.5, 0.001) T = 300 calc = xscalc.XSCalculator(fccNi, T) coh_el_xs = [calc.xs_coh_el(l) for l in lambdas] data = np.array([lambdas, coh_el_xs]) expected = np.load(os.path.join(thisdir, 'expected', 'fccNi-coh-el-xs.npy')) assert np.isclose(data, expected).all() if not interactive: import matplotlib as mpl mpl.use('Agg') from matplotlib import pyplot as plt calc.plotAll(lambdas) if interactive: plt.show() return
def _test_Fe(): d = laz.read("Fe.laz") peaks = [ xscalc.DiffrPeak(hkl, F, d, mult) for hkl, F, d, mult in zip(d['hkl'], d['F'], d['d'], d['mult']) ] # "An experimental determination of the Debye-Waller factor for iron by neutron diffraction" S K Mohanlal # Journal of Physics C: Solid State Physics, Volume 12, Number 17 B = 0.35 Fe = xscalc.XSCalculator('Fe', 11.22, 0.4, 2.56, 2.886**3, peaks, B) print(Fe.xs(2200)) lambdas = np.arange(0.05, 5.5, 0.001) xs = [Fe.xs_coh(l) for l in lambdas] from matplotlib import pyplot as plt plt.plot(lambdas, xs) plt.show() return
def test_fccNi_onepeak(): lambdas = np.arange(0.05, 5.5, 0.001) T = 300 calc = xscalc.XSCalculator(fccNi, T) pk = calc.diffpeaks[6] xs = calc.xs_coh_el__peak(lambdas, pk) jorgensen = pp.Jorgensen(alpha=[1., 0.], beta=[2., 0], sigma=[0, 30e-3, 0]) jorgensen.set_d_spacing(pk.d) spectrum = jorgensen.convolve(lambdas, xs) if interactive: from matplotlib import pyplot as plt plt.plot(lambdas, xs, label='cross section') plt.plot(lambdas, spectrum, label='convolved') plt.legend() plt.show() return
def test_fccNi(): lambdas = np.arange(0.05, 5.5, 0.001) T = 300 xs_calculator = xscalc.XSCalculator(fccNi, T) jorgensen = pp.Jorgensen(alpha=[50, 0.], beta=[10, 0], sigma=[0, .003, 0]) spectrum_calculator = calc.BraggEdgeSpectrumCalculator( xs_calculator, jorgensen) spectrum = spectrum_calculator('total', lambdas) xs = xs_calculator.xs(lambdas) if interactive: from matplotlib import pyplot as plt plt.plot(lambdas, xs, label='cross section') plt.plot(lambdas, spectrum, label='convolved') plt.legend() plt.text(0.5, 115, "alpha=[50, 0.], beta=[10, 0], sigma=[0, .003, 0])") plt.show() return
def test_NaCl(): os.chdir(here) from bem import xscalc, diffraction, matter NaCl = matter.loadCif('NaCl.cif') lambdas = np.arange(1.5, 7, 0.01) T = 300 # if max_diffraction_index is too small, the low wavelength portion will be a bit off calc = xscalc.XSCalculator(NaCl, T, max_diffraction_index=7) coh_el_xs = calc.xs_coh_el(lambdas) inc_el_xs = calc.xs_inc_el(lambdas) inel_xs = calc.xs_inel(lambdas) abs_xs = calc.xs_abs(lambdas) coh_inel_xs = calc.xs_coh_inel(lambdas) inc_inel_xs = calc.xs_inc_inel(lambdas) total = calc.xs(lambdas) data = np.array([lambdas, total]) # np.save(os.path.join(here, 'expected', 'NaCl-total-xs.npy'), data) expected = np.load(os.path.join(here, 'expected', 'NaCl-total-xs.npy')) assert np.isclose(data, expected).all() if interactive: from matplotlib import pyplot as plt plt.figure(figsize=(9, 4)) # *4 to match what is done in "nxs" paper plt.plot(lambdas, coh_el_xs * 4, label='coh el') plt.plot(lambdas, inc_el_xs * 4, label='inc el') plt.plot(lambdas, coh_inel_xs * 4, label='coh inel') plt.plot(lambdas, inc_inel_xs * 4, label='inc inel') # plt.plot(lambdas, inel_xs, label='inel') plt.plot(lambdas, abs_xs, label='abs') plt.plot(lambdas, total, label='total') plt.ylim(-0.2, None) plt.xlim(2, 9) plt.legend() plt.show() os.chdir(saved_pwd) return
def test1(): lambdas = np.arange(0.05, 5.5, 0.005) T = 300 # if max_diffraction_index is too small, the low wavelength portion will be a bit off calc = xscalc.XSCalculator(fccAl, T, max_diffraction_index=8) calc.xs_coh_el(1.5) coh_el_xs = calc.xs_coh_el(lambdas) inc_el_xs = calc.xs_inc_el(lambdas) inel_xs = calc.xs_inel(lambdas) abs_xs = calc.xs_abs(lambdas) coh_inel_xs = calc.xs_coh_inel(lambdas) inc_inel_xs = calc.xs_inc_inel(lambdas) total = calc.xs(lambdas) for i, l in enumerate(lambdas): assert np.isclose(calc.xs_coh_el(l), coh_el_xs[i]) assert np.isclose(calc.xs_inc_el(l), inc_el_xs[i]) assert np.isclose(calc.xs_inel(l), inel_xs[i]) assert np.isclose(calc.xs_abs(l), abs_xs[i]) assert np.isclose(calc.xs_coh_inel(l), coh_inel_xs[i]) assert np.isclose(calc.xs_inc_inel(l), inc_inel_xs[i]) assert np.isclose(calc.xs(l), total[i]) continue return
def store_bragg_df_in_json( n_submit, test_passed, cif_uploads, cif_names, temperature_K, distance_m, delay_us, band_min, band_max, band_step, ): if test_passed: error_div_list = [] xs_dict = {} wavelengths_A = np.arange(band_min, band_max, band_step) if cif_uploads is not None: for each_index, each_content in enumerate(cif_uploads): try: print("'{}', reading .cif file...".format( cif_names[each_index])) _cif_struc = parse_cif_upload(content=each_content) _name_only = cif_names[each_index].split('.')[0] print("'{}', calculating cross-sections...".format( cif_names[each_index])) xscalculator = xscalc.XSCalculator(_cif_struc, temperature_K, max_diffraction_index=4) xs_dict[_name_only + ' (total)'] = xscalculator.xs(wavelengths_A) xs_dict[_name_only + ' (abs)'] = xscalculator.xs_abs(wavelengths_A) xs_dict[_name_only + ' (coh el)'] = xscalculator.xs_coh_el( wavelengths_A) xs_dict[_name_only + ' (inc el)'] = xscalculator.xs_inc_el( wavelengths_A) xs_dict[_name_only + ' (coh inel)'] = xscalculator.xs_coh_inel( wavelengths_A) xs_dict[_name_only + ' (inc inel)'] = xscalculator.xs_inc_inel( wavelengths_A) print("Calculation done.") except AttributeError as error_msg1: print(str(error_msg1)) error1 = "ERROR: '{}', ".format( cif_names[each_index] ) + str(error_msg1).split( '.' )[0] + '. The .cif format is not compatible, please reformat following ICSD database.' error_div_list.append(error1) except ValueError as error_msg2: error2 = "ERROR: '{}', ".format( cif_names[each_index]) + str(error_msg2).split( '.')[0] + '.' error_div_list.append(error2) if len(error_div_list) == 0: df_y = pd.DataFrame.from_dict(xs_dict) df_x = pd.DataFrame() df_x[constants.energy_name] = ir_util.angstroms_to_ev( wavelengths_A) df_x = fill_df_x_types(df=df_x, distance_m=distance_m, delay_us=delay_us) datasets = { 'x': df_x.to_json(orient='split', date_format='iso'), 'y': df_y.to_json(orient='split', date_format='iso'), } return json.dumps(datasets), True else: return None, error_div_list else: return None, False else: return None, False
Atom('Ni', (0.5, 0.5, 0)), Atom('Ni', (0.5, 0, 0.5)), Atom('Ni', (0, 0.5, 0.5)) ] a = 3.5238 alpha = 90. lattice = Lattice(a=a, b=a, c=a, alpha=alpha, beta=alpha, gamma=alpha) fccNi = Structure(atoms, lattice, sgid=225) # define wavelength axis import numpy as np wavelengths = np.arange(0.05, 5.5, 0.005) T = 300 # create calculator from bem import xscalc xscalculator = xscalc.XSCalculator(fccNi, T, max_diffraction_index=7) # compute various contributions # In neutron Bragg Edge data analysis, it may not be necessary to calculate all these # contributions, but it is useful to see them when exploring. coh_el_xs = xscalculator.xs_coh_el(wavelengths) inc_el_xs = xscalculator.xs_inc_el(wavelengths) abs_xs = xscalculator.xs_abs(wavelengths) coh_inel_xs = xscalculator.xs_coh_inel(wavelengths) inc_inel_xs = xscalculator.xs_inc_inel(wavelengths) # and the total cross section total = xscalculator.xs(wavelengths) # plot from matplotlib import pyplot as plt plt.plot(wavelengths, coh_el_xs, label='coh el') plt.plot(wavelengths, inc_el_xs, label='inc el') plt.plot(wavelengths, coh_inel_xs, label='coh inel')