Skip to content

Commit 031ea56

Browse files
authored
Merge pull request #199 from pyiron/extend_test
More tests
2 parents d6a4531 + 2b36513 commit 031ea56

File tree

2 files changed

+119
-81
lines changed

2 files changed

+119
-81
lines changed

tests/test_ase_constraints.py

Lines changed: 0 additions & 76 deletions
This file was deleted.

tests/test_ase_interface.py

Lines changed: 119 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
import logging
12
import unittest
23

34
from ase.build import bulk
5+
from ase.constraints import FixAtoms, FixedPlane, FixCom
46
import numpy as np
57

68
from pylammpsmpi import LammpsASELibrary, LammpsLibrary
9+
from pylammpsmpi.wrapper.ase import (
10+
cell_is_skewed,
11+
get_species_symbols,
12+
get_structure_indices,
13+
get_lammps_indicies_from_ase_structure,
14+
set_selective_dynamics,
15+
)
716

817

918
class TestLammpsASELibrary(unittest.TestCase):
@@ -12,13 +21,13 @@ def test_static(self):
1221
working_directory=None,
1322
cores=1,
1423
comm=None,
15-
logger=None,
24+
logger=logging.getLogger("TestStaticLogger"),
1625
log_file=None,
1726
library=LammpsLibrary(cores=2, mode='local'),
1827
diable_log_file=True,
1928
)
2029
structure = bulk("Al", cubic=True).repeat([2, 2, 2])
21-
lmp.interactive_lib_command(command="units lj")
30+
lmp.interactive_lib_command(command="units lj")
2231
lmp.interactive_lib_command(command="atom_style atomic")
2332
lmp.interactive_lib_command(command="atom_modify map array")
2433
lmp.interactive_structure_setter(
@@ -32,6 +41,8 @@ def test_static(self):
3241
)
3342
lmp.interactive_lib_command("pair_style lj/cut 6.0")
3443
lmp.interactive_lib_command("pair_coeff 1 1 1.0 1.0 4.04")
44+
lmp.interactive_lib_command(command="thermo_style custom step temp pe etotal pxx pxy pxz pyy pyz pzz vol")
45+
lmp.interactive_lib_command(command="thermo_modify format float %20.15g")
3546
lmp.interactive_lib_command("run 0")
3647
self.assertTrue(np.all(np.isclose(lmp.interactive_cells_getter(), structure.cell.array)))
3748
self.assertTrue(np.isclose(lmp.interactive_energy_pot_getter(), -0.04342932384411341))
@@ -41,20 +52,26 @@ def test_static(self):
4152
self.assertTrue(np.all(lmp.interactive_indices_getter() == [1] * len(structure)))
4253
self.assertEqual(lmp.interactive_steps_getter(), 0)
4354
self.assertEqual(lmp.interactive_temperatures_getter(), 0)
55+
self.assertTrue(np.isclose(np.sum(lmp.interactive_pressures_getter()), -0.015661731917941832))
56+
self.assertEqual(np.sum(lmp.interactive_velocities_getter()), 0.0)
57+
self.assertTrue(np.isclose(np.sum(lmp.interactive_positions_getter()), 291.6))
58+
lmp.interactive_cells_setter(cell=1.01 * structure.cell.array)
59+
lmp.interactive_lib_command("run 0")
60+
self.assertTrue(np.all(np.isclose(lmp.interactive_cells_getter(), 1.01 * structure.cell.array)))
4461
lmp.close()
4562

4663
def test_static_with_statement(self):
4764
structure = bulk("Al").repeat([2, 2, 2])
4865
with LammpsASELibrary(
4966
working_directory=None,
50-
cores=1,
67+
cores=2,
5168
comm=None,
5269
logger=None,
5370
log_file=None,
54-
library=LammpsLibrary(cores=2, mode='local'),
71+
library=None,
5572
diable_log_file=True,
5673
) as lmp:
57-
lmp.interactive_lib_command(command="units lj")
74+
lmp.interactive_lib_command(command="units lj")
5875
lmp.interactive_lib_command(command="atom_style atomic")
5976
lmp.interactive_lib_command(command="atom_modify map array")
6077
lmp.interactive_structure_setter(
@@ -68,6 +85,8 @@ def test_static_with_statement(self):
6885
)
6986
lmp.interactive_lib_command("pair_style lj/cut 6.0")
7087
lmp.interactive_lib_command("pair_coeff 1 1 1.0 1.0 4.04")
88+
lmp.interactive_lib_command(command="thermo_style custom step temp pe etotal pxx pxy pxz pyy pyz pzz vol")
89+
lmp.interactive_lib_command(command="thermo_modify format float %20.15g")
7190
lmp.interactive_lib_command("run 0")
7291
self.assertTrue(np.isclose(lmp.interactive_energy_pot_getter(), -0.3083820387630098))
7392
self.assertTrue(np.isclose(lmp.interactive_energy_tot_getter(), -0.3083820387630098))
@@ -76,3 +95,98 @@ def test_static_with_statement(self):
7695
self.assertTrue(np.all(lmp.interactive_indices_getter() == [1] * len(structure)))
7796
self.assertEqual(lmp.interactive_steps_getter(), 0)
7897
self.assertEqual(lmp.interactive_temperatures_getter(), 0)
98+
self.assertTrue(np.isclose(np.sum(lmp.interactive_pressures_getter()), -0.00937227406237915))
99+
self.assertEqual(np.sum(lmp.interactive_velocities_getter()), 0.0)
100+
101+
102+
class TestASEHelperFunctions(unittest.TestCase):
103+
@classmethod
104+
def setUpClass(cls):
105+
cls.structure_skewed = bulk("Al").repeat([2, 2, 2])
106+
cls.structure_cubic = bulk("Al", cubic=True).repeat([2, 2, 2])
107+
108+
def test_get_species_symbols(self):
109+
self.assertEqual(get_species_symbols(structure=self.structure_cubic),['Al'])
110+
111+
def test_get_structure_indices(self):
112+
indicies = get_structure_indices(structure=self.structure_cubic)
113+
self.assertEqual(len(indicies), len(self.structure_cubic))
114+
self.assertEqual(len(set(indicies)), 1)
115+
self.assertEqual(set(indicies), {0})
116+
117+
def test_cell_is_skewed(self):
118+
self.assertTrue(cell_is_skewed(cell=self.structure_skewed.cell))
119+
self.assertFalse(cell_is_skewed(cell=self.structure_cubic.cell))
120+
121+
def test_get_lammps_indicies_from_ase_structure(self):
122+
indicies = get_lammps_indicies_from_ase_structure(
123+
structure=self.structure_cubic,
124+
el_eam_lst=["Al", "H"]
125+
)
126+
self.assertEqual(len(indicies), len(self.structure_cubic))
127+
self.assertEqual(len(set(indicies)), 1)
128+
self.assertEqual(set(indicies), {1})
129+
130+
131+
class TestConstraints(unittest.TestCase):
132+
@classmethod
133+
def setUpClass(cls):
134+
structure = bulk("Cu", cubic=True)
135+
structure.symbols[2:] = "Al"
136+
cls.structure = structure
137+
138+
def test_selective_dynamics_mixed_calcmd(self):
139+
atoms = self.structure.copy()
140+
c1 = FixAtoms(indices=[atom.index for atom in atoms if atom.symbol == 'Cu'])
141+
c2 = FixedPlane(
142+
[atom.index for atom in atoms if atom.symbol == 'Al'],
143+
[1, 0, 0],
144+
)
145+
atoms.set_constraint([c1, c2])
146+
control_dict = set_selective_dynamics(structure=atoms, calc_md=True)
147+
self.assertEqual(len(control_dict), 6)
148+
self.assertTrue(control_dict['group constraintxyz'], 'id 1 2')
149+
self.assertTrue(control_dict['fix constraintxyz'], 'constraintxyz setforce 0.0 0.0 0.0')
150+
self.assertTrue(control_dict['velocity constraintxyz'], 'set 0.0 0.0 0.0')
151+
self.assertTrue(control_dict['group constraintx'], 'id 3 4')
152+
self.assertTrue(control_dict['fix constraintx'], 'constraintx setforce 0.0 NULL NULL')
153+
self.assertTrue(control_dict['velocity constraintx'], 'set 0.0 NULL NULL')
154+
155+
def test_selective_dynamics_mixed(self):
156+
atoms = self.structure.copy()
157+
c1 = FixAtoms(indices=[atom.index for atom in atoms if atom.symbol == 'Cu'])
158+
c2 = FixedPlane(
159+
[atom.index for atom in atoms if atom.symbol == 'Al'],
160+
[1, 0, 0],
161+
)
162+
atoms.set_constraint([c1, c2])
163+
control_dict = set_selective_dynamics(structure=atoms, calc_md=False)
164+
self.assertEqual(len(control_dict), 4)
165+
self.assertTrue(control_dict['group constraintxyz'], 'id 1 2')
166+
self.assertTrue(control_dict['fix constraintxyz'], 'constraintxyz setforce 0.0 0.0 0.0')
167+
self.assertTrue(control_dict['group constraintx'], 'id 3 4')
168+
self.assertTrue(control_dict['fix constraintx'], 'constraintx setforce 0.0 NULL NULL')
169+
170+
def test_selective_dynamics_single_fix(self):
171+
atoms = self.structure.copy()
172+
c1 = FixAtoms(indices=[atom.index for atom in atoms if atom.symbol == 'Cu'])
173+
atoms.set_constraint(c1)
174+
control_dict = set_selective_dynamics(structure=atoms, calc_md=False)
175+
self.assertEqual(len(control_dict), 2)
176+
self.assertTrue(control_dict['group constraintxyz'], 'id 1 2')
177+
self.assertTrue(control_dict['fix constraintxyz'], 'constraintxyz setforce 0.0 0.0 0.0')
178+
179+
def test_selective_dynamics_errors(self):
180+
atoms = self.structure.copy()
181+
atoms.set_constraint(FixCom())
182+
with self.assertRaises(ValueError):
183+
set_selective_dynamics(structure=atoms, calc_md=False)
184+
185+
def test_selective_dynamics_wrong_plane(self):
186+
atoms = self.structure.copy()
187+
atoms.set_constraint(FixedPlane(
188+
[atom.index for atom in atoms if atom.symbol == 'Al'],
189+
[2, 1, 0],
190+
))
191+
with self.assertRaises(ValueError):
192+
set_selective_dynamics(structure=atoms, calc_md=False)

0 commit comments

Comments
 (0)