DanPorter / Dans_Diffraction

Reads crystallographic cif files and simulates diffraction
Apache License 2.0
45 stars 13 forks source link

phases #5

Closed aslarsen closed 3 years ago

aslarsen commented 3 years ago

Hi Is it possible to print the hkl, intensity and phase of each reflection? Or get the data in arrays?

DanPorter commented 3 years ago

Hi aslarsen,

Yes it is possible to get this information. Here is some code to achieve this:

import Dans_Diffraction as dif
xtl = dif.Crystal("somefile.cif")

xtl.Scatter.setup_scatter('x-ray') # choose the scattering factors (e.g. 'x-ray' or 'neutron')

# list of hkl reflections
hkl = [ [1, 1, 1], [0, 0, 1] ]
# hkl = xtl.Cell.all_hkl(energy_kev=8) # to get all reflections available at this energy

# calculate intensity
inten = xtl.Scatter.intensity(hkl)
# calculate structure factor
xtl.Scatter._return_structure_factor = True
sf = xtl.Scatter.intensity(hkl)
xtl.Scatter._return_structure_factor = False

# phase
phase = np.angle(sf)

# print the arrays
for n in range(len(hkl)):
    print(f"{hkl[n]} intensity={inten[n]: 10.3f}, phase={phase[n]: 7.3f}")
DanPorter commented 3 years ago

Hi again,

I have just pushed a new version that includes a "structure_factor" function that will remove a couple of lines from the code above. This function is available in versions 1.9.6 onwards.

The code is now:

import Dans_Diffraction as dif
xtl = dif.Crystal("somefile.cif")

xtl.Scatter.setup_scatter('x-ray') # choose the scattering factors (e.g. 'x-ray' or 'neutron')

# list of hkl reflections
hkl = [ [1, 1, 1], [0, 0, 1] ]
# hkl = xtl.Cell.all_hkl(energy_kev=8) # to get all reflections available at this energy

# calculate intensity
inten = xtl.Scatter.intensity(hkl)
# calculate structure factor (dif.__version__ 1.9.6 onwards)
sf = xtl.Scatter.structure_factor(hkl)
# phase
phase = np.angle(sf)

# print the arrays
for n in range(len(hkl)):
    print(f"{hkl[n]} intensity={inten[n]: 10.3f}, phase={phase[n]: 7.3f}")
aslarsen commented 3 years ago

Thanks!