Skip to content

Commit

Permalink
TST: More updated to use absolute path for data files
Browse files Browse the repository at this point in the history
  • Loading branch information
pastewka committed Jan 4, 2024
1 parent 12cae8c commit 01f26f5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 33 deletions.
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os.path

import pytest

@pytest.fixture
def datafile_directory():
"""Absolute path to the `tests` directory (which also contains the data files)"""
return os.path.dirname(__file__)
4 changes: 2 additions & 2 deletions tests/test_bop.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ def test_crystal_elastic_constants(a0, par):
TersoffBrenner(tersoff_brenner.Tersoff_PRB_39_5566_Si_C),
StillingerWeber(stillinger_weber.Stillinger_Weber_PRB_31_5262_Si)
])
def test_amorphous(par):
def test_amorphous(par, datafile_directory):
# Tests for amorphous Si
aSi = ase.io.read(f'{os.path.dirname(__file__)}/aSi_N8.xyz')
aSi = ase.io.read(f'{datafile_directory}/aSi_N8.xyz')
aSi.calc = Manybody(**par)
# Non-zero forces and Hessian
compute_forces_and_hessian(aSi, par)
Expand Down
11 changes: 5 additions & 6 deletions tests/test_eam_average_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ======================================================================

import os.path
import unittest

import numpy as np

import os

from matscipy.calculators.eam import io, average_atom

import matscipytest
Expand Down Expand Up @@ -79,11 +78,11 @@ def test_average_atom_Ni_Al(self):
input_table = "Mishin-Ni-Al-2009.eam.alloy"
reference_table = "Mishin-Ni-Al-2009_reference_A-atom_Ni85Al15.eam.alloy"
concentrations = np.array((0.85, 0.15))
source, parameters, F, f, rep = io.read_eam(input_table)
source, parameters, F, f, rep = io.read_eam(f'{os.path.dirname(__file__)}/{input_table}')
(new_parameters, new_F, new_f, new_rep) = average_atom.average_potential(
concentrations, parameters, F, f, rep
)
ref_source, ref_parameters, ref_F, ref_f, ref_rep = io.read_eam(reference_table)
ref_source, ref_parameters, ref_F, ref_f, ref_rep = io.read_eam(f'{os.path.dirname(__file__)}/{reference_table}')
diff_F = np.linalg.norm(ref_F - new_F)
diff_f = np.linalg.norm(ref_f - new_f)
diff_rep = np.linalg.norm(ref_rep - new_rep)
Expand Down Expand Up @@ -113,11 +112,11 @@ def test_average_atom_Fe_Cu_Ni(self):
input_table = "FeCuNi.eam.alloy"
reference_table = "FeCuNi_reference_A-atom_Fe33Cu33Ni33.eam.alloy"
concentrations = np.array((1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0))
source, parameters, F, f, rep = io.read_eam(input_table)
source, parameters, F, f, rep = io.read_eam(f'{os.path.dirname(__file__)}/{input_table}')
(new_parameters, new_F, new_f, new_rep) = average_atom.average_potential(
concentrations, parameters, F, f, rep
)
ref_source, ref_parameters, ref_F, ref_f, ref_rep = io.read_eam(reference_table)
ref_source, ref_parameters, ref_F, ref_f, ref_rep = io.read_eam(f'{os.path.dirname(__file__)}/{reference_table}')
diff_F = np.linalg.norm(ref_F - new_F)
diff_f = np.linalg.norm(ref_f - new_f)
diff_rep = np.linalg.norm(ref_rep - new_rep)
Expand Down
26 changes: 13 additions & 13 deletions tests/test_eam_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#

import gzip
import random
import os.path
import unittest

import numpy as np
Expand Down Expand Up @@ -53,8 +53,8 @@ class TestEAMCalculator(matscipytest.MatSciPyTestCase):
tol = 2e-6

def test_forces(self):
for calc in [EAM('Au-Grochola-JCP05.eam.alloy')]:
a = io.read('Au_923.xyz')
for calc in [EAM(f'{os.path.dirname(__file__)}/Au-Grochola-JCP05.eam.alloy')]:
a = io.read(f'{os.path.dirname(__file__)}/Au_923.xyz')
a.center(vacuum=10.0)
a.calc = calc
f = a.get_forces()
Expand All @@ -67,7 +67,7 @@ def test_forces(self):

def test_stress(self):
a = FaceCenteredCubic('Au', size=[2,2,2])
calc = EAM('Au-Grochola-JCP05.eam.alloy')
calc = EAM(f'{os.path.dirname(__file__)}/Au-Grochola-JCP05.eam.alloy')
a.calc = calc
self.assertArrayAlmostEqual(a.get_stress(), numerical_stress(a), tol=self.tol)

Expand All @@ -80,7 +80,7 @@ def test_stress(self):

def test_Grochola(self):
a = FaceCenteredCubic('Au', size=[2,2,2])
calc = EAM('Au-Grochola-JCP05.eam.alloy')
calc = EAM(f'{os.path.dirname(__file__)}/Au-Grochola-JCP05.eam.alloy')
a.calc = calc
FIRE(StrainFilter(a, mask=[1,1,1,0,0,0]), logfile=None).run(fmax=0.001)
a0 = a.cell.diagonal().mean()/2
Expand All @@ -94,17 +94,17 @@ def test_Grochola(self):
def test_direct_evaluation(self):
a = FaceCenteredCubic('Au', size=[2,2,2])
a.rattle(0.1)
calc = EAM('Au-Grochola-JCP05.eam.alloy')
calc = EAM(f'{os.path.dirname(__file__)}/Au-Grochola-JCP05.eam.alloy')
a.calc = calc
f = a.get_forces()

calc2 = EAM('Au-Grochola-JCP05.eam.alloy')
calc2 = EAM(f'{os.path.dirname(__file__)}/Au-Grochola-JCP05.eam.alloy')
i_n, j_n, dr_nc, abs_dr_n = neighbour_list('ijDd', a, cutoff=calc2.cutoff)
epot, virial, f2 = calc2.energy_virial_and_forces(a.numbers, i_n, j_n, dr_nc, abs_dr_n)
self.assertArrayAlmostEqual(f, f2)

a = FaceCenteredCubic('Cu', size=[2,2,2])
calc = EAM('CuAg.eam.alloy')
calc = EAM(f'{os.path.dirname(__file__)}/CuAg.eam.alloy')
a.calc = calc
FIRE(StrainFilter(a, mask=[1,1,1,0,0,0]), logfile=None).run(fmax=0.001)
e_Cu = a.get_potential_energy()/len(a)
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_CuZr(self):
# This is a test for the potential published in:
# Mendelev, Sordelet, Kramer, J. Appl. Phys. 102, 043501 (2007)
a = FaceCenteredCubic('Cu', size=[2,2,2])
calc = EAM('CuZr_mm.eam.fs', kind='eam/fs')
calc = EAM(f'{os.path.dirname(__file__)}/CuZr_mm.eam.fs', kind='eam/fs')
a.calc = calc
FIRE(StrainFilter(a, mask=[1,1,1,0,0,0]), logfile=None).run(fmax=0.001)
a_Cu = a.cell.diagonal().mean()/2
Expand Down Expand Up @@ -215,20 +215,20 @@ def test_forces_CuZr_glass(self):
modify sort id format float "%.14g"
"""
format = "lammps-dump" if "lammps-dump" in io.formats.all_formats.keys() else "lammps-dump-text"
atoms = io.read("CuZr_glass_460_atoms_forces.lammps.dump.gz", format=format)
atoms = io.read(f"{os.path.dirname(__file__)}/CuZr_glass_460_atoms_forces.lammps.dump.gz", format=format)
old_atomic_numbers = atoms.get_atomic_numbers()
sel, = np.where(old_atomic_numbers == 1)
new_atomic_numbers = np.zeros_like(old_atomic_numbers)
new_atomic_numbers[sel] = 40 # Zr
sel, = np.where(old_atomic_numbers == 2)
new_atomic_numbers[sel] = 29 # Cu
atoms.set_atomic_numbers(new_atomic_numbers)
calculator = EAM('ZrCu.onecolumn.eam.alloy')
calculator = EAM(f'{os.path.dirname(__file__)}/ZrCu.onecolumn.eam.alloy')
atoms.calc = calculator
atoms.pbc = [True, True, True]
forces = atoms.get_forces()
# Read tabulated forces and compare
with gzip.open("CuZr_glass_460_atoms_forces.lammps.dump.gz") as file:
with gzip.open(f"{os.path.dirname(__file__)}/CuZr_glass_460_atoms_forces.lammps.dump.gz") as file:
for line in file:
if line.startswith(b"ITEM: ATOMS "): # ignore header
break
Expand Down Expand Up @@ -275,7 +275,7 @@ def test_funcfl(self):
for i in range(1, 202):
latticeconstant = amin + i * da
atoms = FaceCenteredCubic(symbol='Au', size=[5,5,5], pbc=(1,1,1), latticeconstant=latticeconstant)
calc = EAM('Au-Grochola-JCP05.eam.alloy')
calc = EAM(f'{os.path.dirname(__file__)}/Au-Grochola-JCP05.eam.alloy')
atoms.calc = calc
energy = atoms.get_potential_energy()
print(energy)
Expand Down
21 changes: 11 additions & 10 deletions tests/test_eam_calculator_forces_and_hessian.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ======================================================================

import os.path
import unittest

import gzip
Expand Down Expand Up @@ -94,20 +95,20 @@ def test_forces_CuZr_glass(self):
modify sort id format float "%.14g"
"""
format = "lammps-dump" if "lammps-dump" in io.formats.all_formats.keys() else "lammps-dump-text"
atoms = io.read("CuZr_glass_460_atoms_forces.lammps.dump.gz", format=format)
atoms = io.read(f"{os.path.dirname(__file__)}/CuZr_glass_460_atoms_forces.lammps.dump.gz", format=format)
old_atomic_numbers = atoms.get_atomic_numbers()
sel, = np.where(old_atomic_numbers == 1)
new_atomic_numbers = np.zeros_like(old_atomic_numbers)
new_atomic_numbers[sel] = 40 # Zr
sel, = np.where(old_atomic_numbers == 2)
new_atomic_numbers[sel] = 29 # Cu
atoms.set_atomic_numbers(new_atomic_numbers)
calculator = EAM('ZrCu.onecolumn.eam.alloy')
calculator = EAM(f'{os.path.dirname(__file__)}/ZrCu.onecolumn.eam.alloy')
atoms.set_calculator(calculator)
atoms.pbc = [True, True, True]
forces = atoms.get_forces()
# Read tabulated forces and compare
with gzip.open("CuZr_glass_460_atoms_forces.lammps.dump.gz") as file:
with gzip.open(f"{os.path.dirname(__file__)}/CuZr_glass_460_atoms_forces.lammps.dump.gz") as file:
for line in file:
if line.startswith(b"ITEM: ATOMS "): # ignore header
break
Expand All @@ -123,7 +124,7 @@ def test_hessian_monoatomic(self):
"""
def _test_for_size(size):
atoms = FaceCenteredCubic('Cu', size=size)
calculator = EAM('CuAg.eam.alloy')
calculator = EAM(f'{os.path.dirname(__file__)}/CuAg.eam.alloy')
self._test_hessian(atoms, calculator)
_test_for_size(size=[1, 1, 1])
_test_for_size(size=[2, 2, 2])
Expand All @@ -143,7 +144,7 @@ def test_hessian_monoatomic_with_duplicate_pairs(self):
Hessian from ASE
"""
atoms = FaceCenteredCubic('Cu', size=[2, 2, 2])
calculator = EAM('CuAg.eam.alloy')
calculator = EAM(f'{os.path.dirname(__file__)}/CuAg.eam.alloy')
self._test_hessian(atoms, calculator)

def test_hessian_crystalline_alloy(self):
Expand All @@ -152,7 +153,7 @@ def test_hessian_crystalline_alloy(self):
Reference: finite difference approximation of
Hessian from ASE
"""
calculator = EAM('ZrCu.onecolumn.eam.alloy')
calculator = EAM(f'{os.path.dirname(__file__)}/ZrCu.onecolumn.eam.alloy')
lattice_size = [4, 4, 4]
# The lattice parameters are not correct, but that should be irrelevant
# CuZr3
Expand All @@ -171,9 +172,9 @@ def test_hessian_amorphous_alloy(self):
Reference: finite difference approximation of
Hessian from ASE
"""
atoms = io.read('CuZr_glass_460_atoms.gz')
atoms = io.read(f'{os.path.dirname(__file__)}/CuZr_glass_460_atoms.gz')
atoms.pbc = [True, True, True]
calculator = EAM('ZrCu.onecolumn.eam.alloy')
calculator = EAM(f'{os.path.dirname(__file__)}/ZrCu.onecolumn.eam.alloy')
self._test_hessian(atoms, calculator)

def test_dynamical_matrix(self):
Expand All @@ -184,9 +185,9 @@ def test_dynamical_matrix(self):
first form the complete Hessian and then divide by masses.
The former method is implemented.
"""
atoms = io.read('CuZr_glass_460_atoms.gz')
atoms = io.read(f'{os.path.dirname(__file__)}/CuZr_glass_460_atoms.gz')
atoms.pbc = [True, True, True]
calculator = EAM('ZrCu.onecolumn.eam.alloy')
calculator = EAM(f'{os.path.dirname(__file__)}/ZrCu.onecolumn.eam.alloy')
dynamical_matrix = calculator.calculate_hessian_matrix(
atoms, divide_by_masses=True
)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_hessian_precon.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os.path
import unittest

import numpy as np
Expand All @@ -23,7 +24,7 @@ def setUp(self):
del self.atoms[0]
np.random.seed(0)
self.atoms.rattle(1e-2)
self.eam = EAM('Mishin-Ni-Al-2009.eam.alloy')
self.eam = EAM(f'{os.path.dirname(__file__)}/Mishin-Ni-Al-2009.eam.alloy')
self.tol = 1e-6

def test_newton_opt(self):
Expand Down
5 changes: 4 additions & 1 deletion tests/test_mcfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

import os.path

import numpy as np

import ase.io
Expand Down Expand Up @@ -233,7 +236,7 @@ def create_mcfm_potential(atoms,
class TestMCFM(matscipytest.MatSciPyTestCase):

def prepare_data(self):
self.atoms = load_atoms("carbon_chain.xyz")
self.atoms = load_atoms(f"{os.path.dirname(__file__)}/carbon_chain.xyz")
self.morse1 = MorsePotentialPerAtom(r0=2, epsilon=2, rho0=6)
self.morse2 = MorsePotentialPerAtom(r0=2, epsilon=4, rho0=6)
self.mcfm_pot = create_mcfm_potential(self.atoms,
Expand Down

0 comments on commit 01f26f5

Please sign in to comment.