Skip to content

Commit

Permalink
tests for jobs and calculators
Browse files Browse the repository at this point in the history
  • Loading branch information
OMalenfantThuot committed Oct 10, 2019
1 parent cd241ac commit f9ace1c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 46 deletions.
18 changes: 18 additions & 0 deletions tests/test_calculators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
import pytest
import numpy as np
from mlcalcdriver.calculators import Calculator, SchnetPackCalculator

model_folder = "tests/models/"

class TestCalc:

mp1 = os.path.join(model_folder, "N2_model")

def test_calc_needs_methods(self):
with pytest.raises(NotImplementedError):
c = Calculator()
c = Calculator(available_properties="energy")
assert(c.available_properties == "energy")
with pytest.raises(NotImplementedError):
c.run()
21 changes: 15 additions & 6 deletions tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@

pos_folder = "tests/posinp_files/"


class TestJob:

file1, file2, file3 = os.path.join(pos_folder, "free.xyz"), os.path.join(pos_folder, "surface.xyz"), os.path.join(pos_folder, "N2.xyz")
pos1, pos2, pos3 = Posinp.from_file(file1), Posinp.from_file(file2), Posinp.from_file(file3)
file1, file2, file3 = (
os.path.join(pos_folder, "free.xyz"),
os.path.join(pos_folder, "surface.xyz"),
os.path.join(pos_folder, "N2.xyz"),
)
pos1, pos2, pos3 = (
Posinp.from_file(file1),
Posinp.from_file(file2),
Posinp.from_file(file3),
)

job = Job(posinp=[pos1, pos2, pos3])

Expand All @@ -19,7 +28,7 @@ def test_raises_no_positions(self):

def test_raises_posinp_types(self):
with pytest.raises(TypeError):
j = Job(posinp=[self.pos1,1])
j = Job(posinp=[self.pos1, 1])

def test_posinp_types(self):
job1 = Job(posinp=self.pos1)
Expand All @@ -32,8 +41,8 @@ def test_posinp_types(self):
(job.num_struct, 3),
(job.posinp[1], pos2),
(job.results.positions, job.posinp),
(job.results.n_at, [4, 4, 2])]
)

(job.results.n_at, [4, 4, 2]),
],
)
def test_values(self, value, expected):
assert value == expected
111 changes: 71 additions & 40 deletions tests/test_posinp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

tests_fol = "tests/posinp_files/"


class TestPosinp:

# Posinp with surface boundary conditions
Expand Down Expand Up @@ -63,81 +64,111 @@ def test_translate_atom(self):
assert new_pos != self.pos
assert new_pos[0] == Atom("C", [0.58333333333, 0.5, 0.25])


@pytest.mark.parametrize("fname", [
"free_reduced.xyz",
"missing_atom.xyz",
"additional_atom.xyz",
])
@pytest.mark.parametrize(
"fname", ["free_reduced.xyz", "missing_atom.xyz", "additional_atom.xyz"]
)
def test_init_raises_ValueError(self, fname):
with pytest.raises(ValueError):
Posinp.from_file(os.path.join(tests_fol, fname))

@pytest.mark.parametrize("to_evaluate", [
"Posinp([Atom('C', [0, 0, 0])], 'bohr', 'periodic')",
"Posinp([Atom('C', [0, 0, 0])], 'bohr', 'periodic', cell=[1, 1])",
"Posinp([Atom('C', [0, 0, 0])], 'bohr', 'periodic', cell=[1,'inf',1])",
])
@pytest.mark.parametrize(
"to_evaluate",
[
"Posinp([Atom('C', [0, 0, 0])], 'bohr', 'periodic')",
"Posinp([Atom('C', [0, 0, 0])], 'bohr', 'periodic', cell=[1, 1])",
"Posinp([Atom('C', [0, 0, 0])], 'bohr', 'periodic', cell=[1,'inf',1])",
],
)
def test_init_raises_ValueError2(self, to_evaluate):
with pytest.raises(ValueError):
eval(to_evaluate)

def test_positions(self):
expected = [7.327412521, 0.0, 3.461304757]
pos1 = Posinp([Atom('C', expected)], units="angstroem",
boundary_conditions="free")
pos1 = Posinp(
[Atom("C", expected)], units="angstroem", boundary_conditions="free"
)
pos2 = pos1.translate_atom(0, [-7.327412521, 0.0, -3.461304757])
assert np.allclose(pos1.positions, expected)
assert np.allclose(pos2.positions, [0, 0, 0])

def test___eq__(self):
atom1 = Atom('N', [0.0, 0.0, 0.0])
atom2 = Atom('N', [0.0, 0.0, 1.1])
pos1 = Posinp([atom1, atom2], 'angstroem', 'free')
pos2 = Posinp([atom2, atom1], 'angstroem', 'free')
atom1 = Atom("N", [0.0, 0.0, 0.0])
atom2 = Atom("N", [0.0, 0.0, 1.1])
pos1 = Posinp([atom1, atom2], "angstroem", "free")
pos2 = Posinp([atom2, atom1], "angstroem", "free")
assert pos1 == pos2 # The order of the atoms in the list do not count
assert pos1 != 1 # No error if other object is not a posinp

def test_with_surface_boundary_conditions(self):
# Two Posinp instances with surface BC are the same even if they
# have a different cell size along y-axis
pos_with_inf = Posinp(
[Atom('N', [2.97630782434901e-23, 6.87220595204354e-23,
0.0107161998748779]),
Atom('N', [-1.10434491945017e-23, -4.87342174483075e-23,
1.10427379608154])],
'angstroem', 'surface', cell=[40, ".inf", 40])
[
Atom(
"N",
[2.97630782434901e-23, 6.87220595204354e-23, 0.0107161998748779],
),
Atom(
"N",
[-1.10434491945017e-23, -4.87342174483075e-23, 1.10427379608154],
),
],
"angstroem",
"surface",
cell=[40, ".inf", 40],
)
pos_wo_inf = Posinp(
[Atom('N', [2.97630782434901e-23, 6.87220595204354e-23,
0.0107161998748779]),
Atom('N', [-1.10434491945017e-23, -4.87342174483075e-23,
1.10427379608154])],
'angstroem', 'surface', cell=[40, 40, 40])
[
Atom(
"N",
[2.97630782434901e-23, 6.87220595204354e-23, 0.0107161998748779],
),
Atom(
"N",
[-1.10434491945017e-23, -4.87342174483075e-23, 1.10427379608154],
),
],
"angstroem",
"surface",
cell=[40, 40, 40],
)
assert pos_with_inf == pos_wo_inf
# They are obviously different if the cell size along the other
# directions are not the same
pos2_wo_inf = Posinp(
[Atom('N', [2.97630782434901e-23, 6.87220595204354e-23,
0.0107161998748779]),
Atom('N', [-1.10434491945017e-23, -4.87342174483075e-23,
1.10427379608154])],
'angstroem', 'surface', cell=[20, "inf", 40])
[
Atom(
"N",
[2.97630782434901e-23, 6.87220595204354e-23, 0.0107161998748779],
),
Atom(
"N",
[-1.10434491945017e-23, -4.87342174483075e-23, 1.10427379608154],
),
],
"angstroem",
"surface",
cell=[20, "inf", 40],
)
assert pos_with_inf != pos2_wo_inf
# They still have the same BC
assert pos2_wo_inf.boundary_conditions == pos_with_inf.boundary_conditions

def test_to_centroid(self):
atoms = [Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])]
atoms = [Atom("N", [0, 0, 0]), Atom("N", [0, 0, 1.1])]
pos = Posinp(atoms, units="angstroem", boundary_conditions="free")
expected_atoms = [Atom('N', [0, 0, -0.55]), Atom('N', [0, 0, 0.55])]
expected_pos = Posinp(expected_atoms, units="angstroem",
boundary_conditions="free")
expected_atoms = [Atom("N", [0, 0, -0.55]), Atom("N", [0, 0, 0.55])]
expected_pos = Posinp(
expected_atoms, units="angstroem", boundary_conditions="free"
)
assert pos.to_centroid() == expected_pos

def test_to_barycenter(self):
atoms = [Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])]
atoms = [Atom("N", [0, 0, 0]), Atom("N", [0, 0, 1.1])]
pos = Posinp(atoms, units="angstroem", boundary_conditions="free")
expected_atoms = [Atom('N', [0, 0, -0.55]), Atom('N', [0, 0, 0.55])]
expected_pos = Posinp(expected_atoms, units="angstroem",
boundary_conditions="free")
expected_atoms = [Atom("N", [0, 0, -0.55]), Atom("N", [0, 0, 0.55])]
expected_pos = Posinp(
expected_atoms, units="angstroem", boundary_conditions="free"
)
assert pos.to_barycenter() == expected_pos

0 comments on commit f9ace1c

Please sign in to comment.