Skip to content

Commit

Permalink
tests: Pymatgen tests now performed on both example cifs
Browse files Browse the repository at this point in the history
  • Loading branch information
Somerandomguy10111 committed Jun 23, 2024
1 parent 06dc7e5 commit f52e9b0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
1 change: 1 addition & 0 deletions tests/t_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from CrystalStructure.examples import CrystalExamples

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

class TestCrystalBase(Unittest):
def test_scattering_params(self):
Expand Down
2 changes: 0 additions & 2 deletions tests/t_fromcif.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from holytools.devtools import Unittest
from pymatgen.core import Structure

from CrystalStructure.crystal import CrystalStructure
from CrystalStructure.examples import CrystalExamples

Expand Down
3 changes: 2 additions & 1 deletion tests/t_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from CrystalStructure.crystal import CrystalStructure, Lengths, Angles, CrystalBase, AtomicSite
from CrystalStructure.atomic_constants.atomic_constants import Void


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


class TestCrystalCalculations(Unittest):
def setUp(self):
self.primitives = Lengths(5, 3, 4)
Expand Down Expand Up @@ -53,6 +53,7 @@ def get_site_symbol(site : AtomicSite):
symbol = site.species.element.symbol
return symbol


if __name__ == '__main__':
TestCrystalCalculations.execute_all()

62 changes: 30 additions & 32 deletions tests/t_pymatgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,42 @@
from CrystalStructure.crystal import CrystalStructure
from CrystalStructure.examples import CrystalExamples

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

class TestPymatgenSpacegroup(Unittest):
class TestPymatgenStructure(Unittest):
def setUp(self):
self.cifs : list[str] = [CrystalExamples.get_cif_content(), CrystalExamples.get_cif_content(secondary=True)]

self.pymatgen_structures : list[Structure] = [Structure.from_str(cif, fmt='cif') for cif in self.cifs]
self.crystal_structures : list[CrystalStructure] = [CrystalStructure.from_cif(cif_content=cif) for cif in self.cifs]
self.spgs : list[int] = [self.extract_spg(cif) for cif in self.cifs]


def test_spacegroup_calculation(self):
for cif in self.cifs:
structure = CrystalStructure.from_cif(cif_content=cif)
structure.calculate_properties()
computed_sg = structure.space_group
for spg, crystal in zip(self.spgs,self.crystal_structures):
crystal.calculate_properties()
computed_sg = crystal.space_group
print(f'Computed space group = {computed_sg}')
original_sg = self.extract_spg(cif)

if computed_sg != original_sg:
raise ValueError(f'Computed spg {computed_sg} does not match actual spg {original_sg} given in cif files')
if computed_sg != spg:
raise ValueError(f'Computed spg {computed_sg} does not match actual spg {spg} given in cif files')


def test_to_pymatgen(self):
for struct, crystal in zip(self.pymatgen_structures, self.crystal_structures):
actual = crystal.to_pymatgen()
expected = struct

self.assertEqual(len(actual.sites), len(expected.sites))
print(f'Actual sites = {actual.sites}; Expected sites = {expected.sites}')

actual_sites = sorted(actual.sites, key=self.euclidean_distance)
expected_sites = sorted(expected.sites, key=self.euclidean_distance)
for s1,s2 in zip(actual_sites, expected_sites):
self.assertEqual(s1,s2)

print(f'Composition = {actual.composition}')


@staticmethod
def extract_spg(cif : str) -> int:
Expand All @@ -35,33 +56,10 @@ def extract_spg(cif : str) -> int:



class TestPymatgenCompatibility(Unittest):
@classmethod
def setUpClass(cls):
pymatgen_structure = Structure.from_str(CrystalExamples.get_cif_content(), fmt='cif')
cls.crystal = CrystalStructure.from_pymatgen(pymatgen_structure=pymatgen_structure)
cls.pymatgen_structure = pymatgen_structure

def test_pymatgen_roundtrip(self):
actual = self.crystal.to_pymatgen()
expected = self.pymatgen_structure

self.assertEqual(len(actual.sites), len(expected.sites))
print(f'Actual sites = {actual.sites}; Expected sites = {expected.sites}')

actual_sites = sorted(actual.sites, key=self.euclidean_distance)
expected_sites = sorted(expected.sites, key=self.euclidean_distance)
for s1,s2 in zip(actual_sites, expected_sites):
self.assertEqual(s1,s2)

print(f'Composition = {actual.composition}')


@staticmethod
def euclidean_distance(site):
return math.sqrt(site.x ** 2 + site.y ** 2 + site.z ** 2)


if __name__ == "__main__":
TestPymatgenCompatibility.execute_all()
TestPymatgenSpacegroup.execute_all()
TestPymatgenStructure.execute_all()

0 comments on commit f52e9b0

Please sign in to comment.