Skip to content

Commit

Permalink
Merge pull request #37 from FrederikLizakJohansen/pymatgen
Browse files Browse the repository at this point in the history
Allow pymatgen objects, minor revisions, new tests, merging
  • Loading branch information
FrederikLizakJohansen authored Sep 15, 2024
2 parents d3d0ac8 + 4c31393 commit 68840cb
Show file tree
Hide file tree
Showing 19 changed files with 3,624 additions and 3,376 deletions.
4 changes: 1 addition & 3 deletions Demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@
"source": [
"# Helper functions for generating different particle models\n",
"from debyecalculator.utility.generate import (\n",
" generate_core_shell_models, \n",
" generate_core_shell_models_fixed_size, \n",
" generate_substitutional_alloy_models,\n",
" generate_periodic_plane_substitution,\n",
" generate_spherical_particle,\n",
Expand Down Expand Up @@ -617,7 +615,7 @@
"source": [
"# Particle Playground\n",
"\n",
"In the following sections, we will dive into how the `DebyeCalculator` can be utilized to model and analyze the scattering properties of simulated nanoparticles. We’ve prepared several examples that demonstrate how to simulate particles with core-shell structures, substitutional alloy particles, and toy-model intermetallic alloys. Feel free to experiment and create your own models to explore the full capabilities of `DebyeCalculator`."
"In the following sections, we will dive into how the `DebyeCalculator` can be utilized to model and analyze the scattering properties of simulated nanoparticles. We’ve prepared several examples that demonstrate how to simulate substitutional alloy particles, and toy-model intermetallic alloys. Feel free to experiment and create your own models to explore the full capabilities of `DebyeCalculator`."
]
},
{
Expand Down
53 changes: 43 additions & 10 deletions debyecalculator/debye_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
from functools import partial
from tqdm.auto import tqdm

try:
from pymatgen.core import Structure
pymatgen_available = True
except ImportError:
pymatgen_available = False

# NamedTuple definitions
StructureTuple = namedtuple('StructureTuple', 'elements size occupancy xyz triu_indices unique_inverse unique_form_factors form_avg_sq structure_inverse')
IqTuple = namedtuple('IqTuple', 'q i')
Expand All @@ -59,16 +65,31 @@

ArrayLike = Union[np.ndarray, torch.Tensor]
IntArrayLike = Union[List[int], np.ndarray, torch.Tensor]
StructureSourceType = Union[
Tuple[List[str], ArrayLike],
Tuple[IntArrayLike, ArrayLike],
List[Tuple[List[str], ArrayLike]],
List[Tuple[IntArrayLike, ArrayLike]],
str,
List[str],
Atoms,
List[Atoms],
]

if pymatgen_available:
StructureSourceType = Union[
Tuple[List[str], ArrayLike],
Tuple[IntArrayLike, ArrayLike],
List[Tuple[List[str], ArrayLike]],
List[Tuple[IntArrayLike, ArrayLike]],
str,
List[str],
Atoms,
List[Atoms],
Structure,
List[Structure],
]
else:
StructureSourceType = Union[
Tuple[List[str], ArrayLike],
Tuple[IntArrayLike, ArrayLike],
List[Tuple[List[str], ArrayLike]],
List[Tuple[IntArrayLike, ArrayLike]],
str,
List[str],
Atoms,
List[Atoms],
]

class DebyeCalculator:
"""
Expand Down Expand Up @@ -462,6 +483,18 @@ def parse_elements(elements, size):

triu_indices, unique_inverse, unique_form_factors, form_avg_sq, structure_inverse = parse_elements(elements, size)

return StructureTuple(elements, size, occupancy, xyz, triu_indices, unique_inverse, unique_form_factors, form_avg_sq, structure_inverse)
elif isinstance(structure_source, Structure):
try:
elements = [site.species_string for site in structure_source.sites]
size = len(elements)
occupancy = torch.ones((size), dtype=torch.float32).to(device=self.device)
xyz = torch.from_numpy(np.array([[site.a, site.b, site.c] for site in structure_source.sites])).to(device=self.device, dtype=torch.float32)
except:
raise ValueError(f'Encountered invalid Structure object')

triu_indices, unique_inverse, unique_form_factors, form_avg_sq, structure_inverse = parse_elements(elements, size)

return StructureTuple(elements, size, occupancy, xyz, triu_indices, unique_inverse, unique_form_factors, form_avg_sq, structure_inverse)
else:
raise TypeError('Encountered unknown structure source')
Expand Down
468 changes: 468 additions & 0 deletions debyecalculator/old_unit_tests.py

Large diffs are not rendered by default.

616 changes: 180 additions & 436 deletions debyecalculator/test_debye_calculator.py

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions debyecalculator/unittests_files/AntiFluorite_Co2O.cif
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
data_image0
_chemical_formula_structural Co8O4
_chemical_formula_sum "Co8 O4"
_cell_length_a 4.86543
_cell_length_b 4.86543
_cell_length_c 4.86543
_cell_angle_alpha 90
_cell_angle_beta 90
_cell_angle_gamma 90

_space_group_name_H-M_alt "P 1"
_space_group_IT_number 1

loop_
_space_group_symop_operation_xyz
'x, y, z'

loop_
_atom_site_type_symbol
_atom_site_label
_atom_site_symmetry_multiplicity
_atom_site_fract_x
_atom_site_fract_y
_atom_site_fract_z
_atom_site_occupancy
Co Co1 1.0 0.25000 0.25000 0.25000 1.0000
Co Co2 1.0 0.75000 0.75000 0.25000 1.0000
Co Co3 1.0 0.75000 0.25000 0.75000 1.0000
Co Co4 1.0 0.25000 0.75000 0.75000 1.0000
Co Co5 1.0 0.25000 0.25000 0.75000 1.0000
Co Co6 1.0 0.75000 0.75000 0.75000 1.0000
Co Co7 1.0 0.25000 0.75000 0.25000 1.0000
Co Co8 1.0 0.75000 0.25000 0.25000 1.0000
O O1 1.0 0.00000 0.00000 0.00000 1.0000
O O2 1.0 0.00000 0.50000 0.50000 1.0000
O O3 1.0 0.50000 0.00000 0.50000 1.0000
O O4 1.0 0.50000 0.50000 0.00000 1.0000
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
qmin:1.0
qmax:30.0
qdamp:0.04
qstep:0.05
rmin:0.0
rmax:20.0
rstep:0.01
rthres:0.0
biso:0.3
device:cuda
batch_size:10000
lorch_mod:False
radiation_type:xray

q,F(Q)
1.0,-0.4698953
1.05,-0.5819457
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
qmin:1.0
qmax:30.0
qdamp:0.04
qstep:0.05
rmin:0.0
rmax:20.0
rstep:0.01
rthres:0.0
biso:0.3
device:cuda
batch_size:10000
lorch_mod:False
radiation_type:xray

r,G(r)
0.0,0.0
0.01,-0.001527964
Expand Down
Loading

0 comments on commit 68840cb

Please sign in to comment.