Skip to content

Commit

Permalink
tests: Added optional num attribute in CrystalExample methods; Remove…
Browse files Browse the repository at this point in the history
…d ElementSymbol
  • Loading branch information
Somerandomguy10111 committed Jun 30, 2024
1 parent af0eb7c commit 360187d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 53 deletions.
2 changes: 1 addition & 1 deletion CrystalStructure/atomic_constants/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .atomic_constants import ElementSymbol, AtomicConstants
from .atomic_constants import AtomicConstants
10 changes: 4 additions & 6 deletions CrystalStructure/atomic_constants/atomic_constants.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import json
import os.path
from typing import Literal, Union
from pymatgen.core import Species, Element
from typing import Union

from pymatgen.core import Species, Element

SCATTERING_PARAMS_FILENAME = 'atomic_scattering_params.json'
COVALENT_RADI_FILENAME = 'covalent_radius.json'
VDW_FILENAME = 'vdw_radius.json'

ElementSymbol = Literal['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K', 'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn', 'Fr', 'Ra', 'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md', 'No', 'Lr', 'Rf', 'Db']

def load_constants_json(fname: str) -> dict:
dirpath = os.path.dirname(__file__)
fpath = os.path.join(dirpath, fname)
Expand All @@ -34,11 +32,11 @@ class AtomicConstants:
# get

@classmethod
def get_vdw_radius(cls, element_symbol: ElementSymbol) -> float:
def get_vdw_radius(cls, element_symbol: str) -> float:
return cls._vdw[element_symbol]

@classmethod
def get_covalent(cls, element_symbol: ElementSymbol) -> float:
def get_covalent(cls, element_symbol: str) -> float:
return cls._covalent[element_symbol]

@classmethod
Expand Down
6 changes: 3 additions & 3 deletions CrystalStructure/crystal/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import math
import json
from typing import Optional
from typing import Optional, Iterable

from holytools.abstract import Serializable

from CrystalStructure.atomic_constants import ElementSymbol, AtomicConstants
from CrystalStructure.atomic_constants import AtomicConstants
from .atomic_site import AtomicSite


Expand All @@ -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 : ElementSymbol = site.get_symbol()
element_symbol : str = site.get_symbol()
covalent_radius = AtomicConstants.get_covalent(element_symbol=element_symbol)
vdw_radius = AtomicConstants.get_vdw_radius(element_symbol=element_symbol)

Expand Down
2 changes: 1 addition & 1 deletion CrystalStructure/crystal/crystal.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def distance_from_origin(atomic_site : AtomicSite):
cartesian_coords = new_lattice.get_fractional_coords(coords)
return np.sum(cartesian_coords**2)

new_base = sorted(new_base, key=distance_from_origin)
new_base = sorted(new_base.atomic_sites, key=distance_from_origin)
self.base = CrystalBase(new_base)


Expand Down
44 changes: 7 additions & 37 deletions CrystalStructure/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,27 @@

from CrystalStructure.crystal import CrystalStructure, CrystalBase

cif1_fpath = os.path.join(os.path.dirname(__file__), 'cifs', "test1.cif")
cif2_fpath = os.path.join(os.path.dirname(__file__), 'cifs', 'test2.cif')

# ---------------------------------------------------------

class CrystalExamples:
@staticmethod
def get_crystal(secondary : bool, mute : bool = False):
cif_content = CrystalExamples.get_cif_content(secondary=secondary)
def get_crystal(num: int, mute: bool = False):
cif_content = CrystalExamples.get_cif_content(num=num)
crystal_structure = CrystalStructure.from_cif(cif_content=cif_content)
if not mute:
print(f'--> Cif content:\n {open(cif1_fpath).read()}')
print(f'--> Cif content:\n {cif_content}')
print(f'--> Crystal structure:\n {crystal_structure}')
return crystal_structure


@staticmethod
def get_base(mute : bool = True) -> CrystalBase:
crystal_stucture = CrystalExamples.get_crystal(mute=mute)
def get_base(num : int = 1, mute : bool = True) -> CrystalBase:
crystal_stucture = CrystalExamples.get_crystal(num=num, mute=mute)
return crystal_stucture.base

@staticmethod
def get_crystal(mute : bool = False):
cif_content = CrystalExamples.get_cif_content()
crystal_structure = CrystalStructure.from_cif(cif_content=cif_content)
if not mute:
print(f'--> Cif content:\n {open(cif1_fpath).read()}')
print(f'--> Crystal structure:\n {crystal_structure}')
return crystal_structure

@staticmethod
def get_cif_content(secondary : bool = False) -> str:
cif_fpath = cif1_fpath if not secondary else cif2_fpath
def get_cif_content(num : int = 1) -> str:
cif_fpath = os.path.join(os.path.dirname(__file__), 'cifs', f"test{num}.cif")
with open(cif_fpath, 'r') as f:
cif_content = f.read()
return cif_content



# @staticmethod
# def get_label() -> PowderExperiment:
# sample = PowderSample(crystal_structure=CrystalExamples.get_crystal(mute=True), crystallite_size=500)
# artifact = CrystalExamples.get_artifacts()
# powder_sample = PowderExperiment(powder=sample, artifacts=artifact, is_simulated=True)
# return powder_sample

# @staticmethod
# def get_artifacts() -> Artifacts:
# artifacts = Artifacts(primary_wavelength=1.54056, secondary_wavelength=1.54439, secondary_to_primary=0.5)
# return artifacts

# if __name__ == "__main__":
# the_sample = CrystalExamples.get_label()
# the_sample.make_empty()
8 changes: 3 additions & 5 deletions tests/t_crystal/crystal_test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import math

from holytools.devtools import Unittest
from pymatgen.core import Structure

from CrystalStructure.atomic_constants.atomic_constants import Void
from CrystalStructure.crystal import CrystalStructure, AtomicSite
from CrystalStructure.crystal import CrystalStructure
from CrystalStructure.examples import CrystalExamples


# ---------------------------------------------------------

class CrystalTest(Unittest):
def setUp(self):
self.cifs : list[str] = [CrystalExamples.get_cif_content(), CrystalExamples.get_cif_content(secondary=True)]
self.cifs : list[str] = [CrystalExamples.get_cif_content(j) for j in range(1, 4)]

self.pymatgen_structures : list[Structure] = [Structure.from_str(cif, fmt='cif') for cif in self.cifs]
self.crystals : list[CrystalStructure] = [CrystalStructure.from_cif(cif_content=cif) for cif in self.cifs]
Expand Down

0 comments on commit 360187d

Please sign in to comment.