Skip to content

xray_utils

X-ray scattering utility funcitons

bmatrix(a, b=None, c=None, alpha=90.0, beta=90.0, gamma=90.0)

Calculate the B matrix as defined in Busing&Levy Acta Cyst. 22, 457 (1967) Creates a matrix to transform (hkl) into a cartesian basis: (qx,qy,qz)' = B.(h,k,l)' (where ' indicates a column vector)

The B matrix is related to the reciprocal basis vectors: (astar, bstar, cstar) = 2 * np.pi * B.T Where cstar is defined along the z axis

The B matrix is related to the real-space unit vectors: (A, B, C) = B^-1 = inv(B)

Parameters:

Name Type Description Default
a

lattice parameter a in Anstroms

required
b

lattice parameter b in Anstroms

None
c

lattice parameter c in Anstroms

None
alpha

lattice angle alpha in degrees

90.0
beta

lattice angle beta in degrees

90.0
gamma

lattice angle gamma in degrees

90.0

Returns:

Type Description

[3x3] array B matrix in inverse-Angstroms (no 2pi)

Source code in mmg_toolbox/utils/xray_utils.py
def bmatrix(a, b=None, c=None, alpha=90., beta=90., gamma=90.):
    """
    Calculate the B matrix as defined in Busing&Levy Acta Cyst. 22, 457 (1967)
    Creates a matrix to transform (hkl) into a cartesian basis:
        (qx,qy,qz)' = B.(h,k,l)'       (where ' indicates a column vector)

    The B matrix is related to the reciprocal basis vectors:
        (astar, bstar, cstar) = 2 * np.pi * B.T
    Where cstar is defined along the z axis

    The B matrix is related to the real-space unit vectors:
        (A, B, C) = B^-1 = inv(B)

    :param a: lattice parameter a in Anstroms
    :param b: lattice parameter b in Anstroms
    :param c: lattice parameter c in Anstroms
    :param alpha: lattice angle alpha in degrees
    :param beta: lattice angle beta in degrees
    :param gamma: lattice angle gamma in degrees
    :returns: [3x3] array B matrix in inverse-Angstroms (no 2pi)
    """
    if b is None:
        b = a
    if c is None:
        c = a
    alpha1 = np.deg2rad(alpha)
    alpha2 = np.deg2rad(beta)
    alpha3 = np.deg2rad(gamma)

    beta1 = np.arccos((np.cos(alpha2) * np.cos(alpha3) - np.cos(alpha1)) / (np.sin(alpha2) * np.sin(alpha3)))
    beta2 = np.arccos((np.cos(alpha1) * np.cos(alpha3) - np.cos(alpha2)) / (np.sin(alpha1) * np.sin(alpha3)))
    beta3 = np.arccos((np.cos(alpha1) * np.cos(alpha2) - np.cos(alpha3)) / (np.sin(alpha1) * np.sin(alpha2)))

    b1 = 1 / (a * np.sin(alpha2) * np.sin(beta3))
    b2 = 1 / (b * np.sin(alpha3) * np.sin(beta1))
    b3 = 1 / (c * np.sin(alpha1) * np.sin(beta2))

    c1 = b1 * b2 * np.cos(beta3)
    c2 = b1 * b3 * np.cos(beta2)
    c3 = b2 * b3 * np.cos(beta1)

    bm = np.array([
        [b1, b2 * np.cos(beta3), b3 * np.cos(beta2)],
        [0, b2 * np.sin(beta3), -b3 * np.sin(beta2) * np.cos(alpha1)],
        [0, 0, 1 / c]
    ])
    return bm

photon_energy(wavelength_a)

Converts wavelength in A to energy in keV energy_kev = photon_energy(wavelength_a) Energy [keV] = h*c/L = 12.3984 / lambda [A]

Source code in mmg_toolbox/utils/xray_utils.py
def photon_energy(wavelength_a):
    """
    Converts wavelength in A to energy in keV
     energy_kev = photon_energy(wavelength_a)
     Energy [keV] = h*c/L = 12.3984 / lambda [A]
    """

    # SI: E = hc/lambda
    lam = wavelength_a * Const.A
    E = Const.h * Const.c / lam

    # Electron Volts:
    energy = E / Const.e
    return energy / 1000.0

photon_wavelength(energy_kev)

Converts energy in keV to wavelength in A wavelength_a = photon_wavelength(energy_kev) lambda [A] = h*c/E = 12.3984 / E [keV]

Source code in mmg_toolbox/utils/xray_utils.py
def photon_wavelength(energy_kev):
    """
    Converts energy in keV to wavelength in A
     wavelength_a = photon_wavelength(energy_kev)
     lambda [A] = h*c/E = 12.3984 / E [keV]
    """

    # Electron Volts:
    E = 1000 * energy_kev * Const.e

    # SI: E = hc/lambda
    lam = Const.h * Const.c / E
    wavelength = lam / Const.A
    return wavelength

wavevector(wavelength_a)

Return wavevector = 2pi/lambda

Source code in mmg_toolbox/utils/xray_utils.py
def wavevector(wavelength_a):
    """Return wavevector = 2pi/lambda"""
    return 2 * np.pi / wavelength_a