Skip to content

Commit

Permalink
fix calculate_structure_factor bug and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
harripj committed Apr 15, 2024
1 parent d325b89 commit 02d9fb9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion ked/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2
0.2.1
36 changes: 19 additions & 17 deletions ked/reciprocal_lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ase.data import atomic_numbers
from diffpy.structure import Atom as diffpyAtom
from diffpy.structure import Structure
from diffpy.structure.spacegroupmod import SpaceGroup
import numpy as np
from numpy.typing import ArrayLike, DTypeLike, NDArray
import pandas as pd
Expand Down Expand Up @@ -103,7 +104,7 @@ def get_debye_waller_factors_greater_than_80K() -> pd.DataFrame:
def calculate_debye_waller_factor(
element: Union[aseAtom, diffpyAtom, str, int],
T: float = 293.0,
structure: Optional[str] = None,
structure_name: Optional[str] = None,
) -> float:
"""
Calculate the Debye-Waller factor B for an element at temperature T.
Expand Down Expand Up @@ -138,13 +139,14 @@ def calculate_debye_waller_factor(
row = factors[factors["Element"] == element]

# apply structure filtering
if structure is not None:
row = row[row["Structure"].str.lower() == structure.lower()]
if structure_name is not None:
row = row[row["Structure"].str.lower() == structure_name.lower()]

n = len(row)
if not n:
temp = {structure if structure is not None else "Any"}
raise ValueError(f"{element} with structure {temp} not found in database.")
raise ValueError(
f"{element} with structure {structure_name or 'Any'} not found in database."
)
elif n > 1:
logging.error(
f"Multiple element with name {element} found in database: "
Expand Down Expand Up @@ -324,6 +326,7 @@ def calculate_structure_factor(
scale_by_scattering_angle: bool = True,
debye_waller: bool = True,
T: float = 293.0,
space_group: Optional[SpaceGroup] = None,
) -> NDArray:
"""
Expand Down Expand Up @@ -361,29 +364,28 @@ def calculate_structure_factor(

# atomic scattering factors
if scale_by_scattering_angle:
f = np.transpose(
[calculate_scattering_factor(atom, g_abs) for atom in structure]
)
f = [calculate_scattering_factor(atom, g_abs) for atom in structure]
else:
# evaluate every atoms for 0 scattering vector
f = np.transpose([calculate_scattering_factor(atom, 0.0) for atom in structure])
f = [calculate_scattering_factor(atom, 0.0) for atom in structure]
f = np.transpose(f)

if debye_waller:
if False: # isinstance(atoms, aseAtoms):
if space_group:
# automatic filtering of BCC and FCC lattices with spacegroup
# numbers 229 and 225
if atoms.info["spacegroup"].no == 225:
structure = "f.c.c." # as written in table
elif atoms.info["spacegroup"].no == 229:
structure = "b.c.c."
if space_group.number == 225:
structure_name = "f.c.c." # as written in table
elif space_group.number == 229:
structure_name = "b.c.c."
else:
structure = None
structure_name = None
# TODO: spacegroup information for diffpy.structure
else:
structure = None
structure_name = None
B = np.array(
[
calculate_debye_waller_factor(atom, T, structure=structure)
calculate_debye_waller_factor(atom, T, structure_name=structure_name)
for atom in structure
]
)
Expand Down
16 changes: 13 additions & 3 deletions tests/test_generator.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
from diffpy.structure import Structure
from orix.quaternion import Orientation
import pytest

from ked.generator import CrystalDiffractionGenerator, DiffractionGeneratorType
from ked.template import DiffractionTemplate, DiffractionTemplateBlock


def test_generator_init(cif_Fe_BCC):
generator = CrystalDiffractionGenerator(cif_Fe_BCC, 200)
@pytest.mark.parametrize(
"kV, asf, db",
[(200, False, False), (300, True, False), (80, False, True), (1_000, True, True)],
)
def test_generator_init(cif_Fe_BCC, kV, asf, db):
generator = CrystalDiffractionGenerator(
cif_Fe_BCC, voltage=kV, atomic_scattering_factor=asf, debye_waller=db
)
assert isinstance(generator, CrystalDiffractionGenerator)
assert isinstance(generator.structure, Structure)
assert generator.voltage * 1e3 == 2e5 # 200 kV
assert generator.voltage == kV
assert generator.voltage <= 1e3 # voltage is in kV
assert generator.max_angle == 5 # by default
assert generator.kind == DiffractionGeneratorType.CRYSTAL
assert generator.atomic_scattering_factor == asf
assert generator.debye_waller == db


def test_generate_template(cif_Fe_BCC):
Expand Down

0 comments on commit 02d9fb9

Please sign in to comment.