Skip to content

Commit

Permalink
crystal: Added typecasting pymatgen elements to species in .from_pyma…
Browse files Browse the repository at this point in the history
…tgen
  • Loading branch information
Somerandomguy10111 committed Jun 30, 2024
1 parent a3f21a6 commit 9c554e2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 36 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ pip-delete-this-directory.txt
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
Expand Down
3 changes: 0 additions & 3 deletions CrystalStructure/atomic_constants/atomic_constants.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import json
import os.path
from typing import Union

from pymatgen.core import Species, Element

SCATTERING_PARAMS_FILENAME = 'atomic_scattering_params.json'
COVALENT_RADI_FILENAME = 'covalent_radius.json'
Expand Down
19 changes: 8 additions & 11 deletions CrystalStructure/crystal/atomic_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import json
from dataclasses import dataclass
from typing import Union, Optional
from pymatgen.core import Species, Element, DummySpecies
from typing import Optional

from holytools.abstract import Serializable
from pymatgen.core import Species, DummySpecies
from pymatgen.util.typing import SpeciesLike

from CrystalStructure.atomic_constants.atomic_constants import AtomicConstants
Expand All @@ -30,7 +30,7 @@ class AtomicSite(Serializable):
wyckoff_letter : Optional[str] = None

def __post_init__(self):
self.atom_type = AtomType(symbol=self.species_symbol)
self.atom_type : AtomType = AtomType(symbol=self.species_symbol)

@classmethod
def make_void(cls) -> AtomicSite:
Expand All @@ -48,9 +48,6 @@ def is_nonstandard(self) -> bool:
# ---------------------------------------------------------
# properties

def get_symbol(self) -> str:
return self.atom_type.get_symbol()

def as_list(self) -> list[float]:
site_arr = [*self.get_scattering_params(), self.x, self.y, self.z, self.occupancy]
return site_arr
Expand Down Expand Up @@ -87,14 +84,14 @@ class AtomType:

def __init__(self, symbol : str):
if symbol == self.void_symbol:
self.species_like : SpeciesLike = DummySpecies(symbol=self.void_symbol)
self.specifier : SpeciesLike = DummySpecies(symbol=self.void_symbol)
if symbol == self.placeholder_symbol:
self.species_like : SpeciesLike = DummySpecies(symbol=self.placeholder_symbol)
self.specifier : SpeciesLike = DummySpecies(symbol=self.placeholder_symbol)
else:
self.species_like : SpeciesLike = Species.from_str(species_string=symbol)
self.specifier : SpeciesLike = Species.from_str(species_string=symbol)

def get_symbol(self):
return str(self.species_like)
return str(self.specifier)

@property
def scattering_params(self) -> ScatteringParams:
Expand All @@ -106,7 +103,7 @@ def scattering_params(self) -> ScatteringParams:
else:
# TODO: This casting only currently exists beacuse the scattering param table only has values for (unoxidized) elements, not ions
# TODO: Normally would simply be species_symbol=str(self.species_like)
species_symbol = self.species_like.element.symbol
species_symbol = self.specifier.element.symbol
values = AtomicConstants.get_scattering_params(species_symbol=species_symbol)

(a1, b1), (a2, b2), (a3, b3), (a4, b4) = values
Expand Down
2 changes: 1 addition & 1 deletion CrystalStructure/crystal/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, atomic_sites : Optional[list[AtomicSite]] = None):
def calculate_atomic_volume(self) -> float:
total_atomic_volume = 0
for site in self.get_non_void_sites():
element_symbol : str = site.get_symbol()
element_symbol : str = site.species_symbol
covalent_radius = AtomicConstants.get_covalent(element_symbol=element_symbol)
vdw_radius = AtomicConstants.get_vdw_radius(element_symbol=element_symbol)

Expand Down
10 changes: 6 additions & 4 deletions CrystalStructure/crystal/crystal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import numpy as np
from holytools.abstract import JsonDataclass
from pymatgen.core import Structure, Lattice
from pymatgen.core import Structure, Lattice, Species, Element
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.symmetry.groups import SpaceGroup

Expand Down Expand Up @@ -59,7 +59,7 @@ def standardize(self):
new_base = CrystalBase()
for site in self.base:
x,y,z = apply_permutation([site.x, site.y, site.z], sort_permutation)
new_site = AtomicSite(x=x, y=y, z=z, occupancy=site.occupancy, atom_type=site.atom_type)
new_site = AtomicSite(x=x, y=y, z=z, occupancy=site.occupancy, species_symbol=site.species_symbol)
new_base.append(new_site)


Expand Down Expand Up @@ -101,8 +101,10 @@ def from_pymatgen(cls, pymatgen_structure: Structure) -> CrystalStructure:
for index, site in enumerate(pymatgen_structure.sites):
site_composition = site.species
for species, occupancy in site_composition.items():
if isinstance(species, Element):
species = Species(symbol=species.symbol, oxidation_state=0)
x,y,z = lattice.get_fractional_coords(site.coords)
atomic_site = AtomicSite(x, y, z, occupancy=occupancy, atom_type=species)
atomic_site = AtomicSite(x, y, z, occupancy=occupancy, species_symbol=str(species))
base.append(atomic_site)

crystal_str = cls(lengths=Lengths(a=lattice.a, b=lattice.b, c=lattice.c),
Expand All @@ -120,7 +122,7 @@ def to_pymatgen(self) -> Structure:
lattice = Lattice.from_parameters(a, b, c, alpha, beta, gamma)

non_void_sites = self.base.get_non_void_sites()
atoms = [site.atom_type for site in non_void_sites]
atoms = [site.atom_type.specifier for site in non_void_sites]
positions = [(site.x, site.y, site.z) for site in non_void_sites]
return Structure(lattice, atoms, positions)

Expand Down
22 changes: 6 additions & 16 deletions tests/t_crystal/t_standard.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from holytools.devtools import Unittest
from pymatgen.core import Species

from CrystalStructure.atomic_constants.atomic_constants import Void
from CrystalStructure.crystal import CrystalStructure, AtomicSite, Lengths, Angles, CrystalBase
from CrystalStructure.crystal.atomic_site import AtomType


# ---------------------------------------------------------
Expand All @@ -12,9 +11,9 @@ def setUp(self):
primitives = Lengths(5, 3, 4)
mock_angles = Angles(90, 90, 90)
mock_base = CrystalBase([
AtomicSite(x=0.5, y=0.5, z=0.5, occupancy=1.0, atom_type=Species("Si")),
AtomicSite(x=0.1, y=0.1, z=0.1, occupancy=1.0, atom_type=Species("O")),
AtomicSite(x=0.9, y=0.9, z=0.9, occupancy=1.0, atom_type=Void())
AtomicSite(x=0.5, y=0.5, z=0.5, occupancy=1.0, species_symbol="Si"),
AtomicSite(x=0.1, y=0.1, z=0.1, occupancy=1.0, species_symbol="O"),
AtomicSite(x=0.9, y=0.9, z=0.9, occupancy=1.0, species_symbol=AtomType.void_symbol)
])
crystal = CrystalStructure(lengths=primitives, angles=mock_angles, base=mock_base)
crystal.calculate_properties()
Expand All @@ -35,8 +34,8 @@ def test_scaling(self):

def test_standardization(self):
self.mock_crystal.standardize()
expected_species_list = ['O', 'Si', Void.symbol]
acrual_species_list = [self.get_site_symbol(site) for site in self.mock_crystal.base]
expected_species_list = ['O', 'Si', AtomType.void_symbol]
acrual_species_list = [site.species_symbol for site in self.mock_crystal.base]
self.assertEqual(acrual_species_list, expected_species_list)

actual_primitives = self.mock_crystal.lengths.as_tuple()
Expand All @@ -45,14 +44,5 @@ def test_standardization(self):



@staticmethod
def get_site_symbol(site : AtomicSite):
if isinstance(site.atom_type, Void):
symbol = Void.symbol
else:
symbol = site.atom_type.element.symbol
return symbol


if __name__ == "__main__":
TestCrystalStandardization.execute_all()

0 comments on commit 9c554e2

Please sign in to comment.