Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-janssen committed Jun 19, 2024
1 parent 3cad333 commit 325c746
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
5 changes: 4 additions & 1 deletion llm_compmat/tools/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ def get_element_property_mendeleev(chemical_symbol: str, property: str) -> str:
if hasattr(elem, property):
property_value = getattr(elem, property)
unit = property_units.get(property, '')
return f"{property_value} {unit}"
if unit != "":
return f"{property_value} {unit}"
else:
return str(property_value)
else:
return f"Property '{property}' is not available for the element '{chemical_symbol}'."
Empty file added tests/__init__.py
Empty file.
71 changes: 71 additions & 0 deletions tests/test_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from unittest import TestCase
import numpy as np
from llm_compmat.tools.interface import (
get_equilibrium_lattice,
get_bulk_modulus,
get_equilibrium_volume,
get_experimental_elastic_property_wikipedia,
get_element_property_mendeleev,
)


class TestInterfaceEMT(TestCase):
@classmethod
def setUpClass(cls):
cls.calculator_str = "emt"
cls.structure = get_equilibrium_lattice.invoke({"chemical_symbol": "Al", "calculator_str": cls.calculator_str})

def test_structure(self):
self.assertTrue(np.all(np.isclose(self.structure.positions, [[0.0, 0.0, 0.0]])))
self.assertEqual(self.structure.numbers, [13])
self.assertTrue(all(self.structure.pbc))
self.assertAlmostEqual(np.linalg.det(self.structure.cell), 15.931389072530033)

def test_bulk_modulus(self):
b0 = get_bulk_modulus.invoke({"atom_dict": self.structure, "calculator_str": self.calculator_str})
self.assertAlmostEqual(b0, 39.518515298270835)

def test_equilibrium_volume(self):
v0 = get_equilibrium_volume.invoke({"atom_dict": self.structure, "calculator_str": self.calculator_str})
self.assertAlmostEqual(v0, 15.931553356465589)


class TestInterfaceMace(TestCase):
@classmethod
def setUpClass(cls):
cls.calculator_str = "mace"
cls.structure = get_equilibrium_lattice.invoke({"chemical_symbol": "Al", "calculator_str": cls.calculator_str})

def test_structure(self):
self.assertTrue(np.all(np.isclose(self.structure.positions, [[0.0, 0.0, 0.0]])))
self.assertEqual(self.structure.numbers, [13])
self.assertTrue(all(self.structure.pbc))
self.assertAlmostEqual(np.linalg.det(self.structure.cell), 16.73685817365007)

def test_bulk_modulus(self):
b0 = get_bulk_modulus.invoke({"atom_dict": self.structure, "calculator_str": self.calculator_str})
self.assertAlmostEqual(b0, 63.86053931471149)

def test_equilibrium_volume(self):
v0 = get_equilibrium_volume.invoke({"atom_dict": self.structure, "calculator_str": self.calculator_str})
self.assertAlmostEqual(v0, 16.73677698606934)


class TestExperimentalReference(TestCase):
# def test_wikipedia(self):
# self.assertAlmostEqual(
# get_experimental_elastic_property_wikipedia.invoke({"chemical_symbol": "Al", "property": "youngs_modulus"}),
# 70.0)
# self.assertAlmostEqual(
# get_experimental_elastic_property_wikipedia.invoke({"chemical_symbol": "Al", "property": "poissons_ratio"}),
# 0.35)
# self.assertAlmostEqual(
# get_experimental_elastic_property_wikipedia.invoke({"chemical_symbol": "Al", "property": "bulk_modulus"}),
# 76.0)
# self.assertAlmostEqual(
# get_experimental_elastic_property_wikipedia.invoke({"chemical_symbol": "Al", "property": "shear_modulus"}),
# 26.0)

def test_mendeleev(self):
self.assertEqual(get_element_property_mendeleev.invoke({"chemical_symbol": "Al", "property": "atomic_number"}), "13")

0 comments on commit 325c746

Please sign in to comment.