Skip to content

Commit

Permalink
add tests for socket vol diff output
Browse files Browse the repository at this point in the history
  • Loading branch information
alchem0x2A committed Jan 19, 2024
1 parent e2c0fba commit 2cc02c6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
7 changes: 5 additions & 2 deletions sparc/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ def _extract_static_results(self, raw_results, index=":"):
calc_results = []
# Use extra lattice information to construct the positions
cell = self.init_atoms.cell
# import pdb; pdb.set_trace()
static_results = _add_cell_info(static_results, cell)

if isinstance(index, int):
Expand All @@ -532,12 +533,14 @@ def _extract_static_results(self, raw_results, index=":"):
partial_results["stress_equiv"] = static_results["stress_equiv"]

atoms = self.init_atoms.copy()
# import pdb; pdb.set_trace()
if "atoms" in static_results:
atoms_dict = static_results["atoms"]

# The socket mode case. Reset all cell and positions
if "lattice" in atoms_dict:
lat = atoms_dict["lattice"]
# Be careful,
if "lattice" in static_results:
lat = static_results["lattice"]
atoms.set_cell(lat, scale_atoms=False)
if "coord" not in atoms_dict:
raise KeyError(
Expand Down
39 changes: 26 additions & 13 deletions tests/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,40 +103,53 @@ def mock_socket_run(*args, **kwargs):
monkeypatch.setattr("subprocess.run", mock_socket_run)
assert calc.detect_socket_compatibility() is True


def test_socket_read_same_cell():
"""Test parsing multi-image static files from socket"""
from sparc.io import read_sparc
from ase.io import read
import numpy as np
from ase.io import read

from sparc.io import read_sparc

bundle = test_output_dir / "Al_socket_bfgs.sparc"
# References from standard socket output
ref_images = read(bundle / "sparc-socket.traj", ":")
cell0 = ref_images[0].cell
parsed_images = read_sparc(bundle, ":")
assert len(ref_images) == len(parsed_images)
for r_img, p_img in zip(ref_images, parsed_images):
assert np.isclose(r_img.positions, p_img.positions, 1.e-4).all()
assert np.isclose(r_img.cell, p_img.cell, 1.e-4).all()
assert np.isclose(cell0, p_img.cell, 1.e-4).all()
assert np.isclose(r_img.get_potential_energy(), p_img.get_potential_energy(), 1.e-4)
assert np.isclose(r_img.get_forces(), p_img.get_forces(), 1.e-4).all()
assert np.isclose(
r_img.get_potential_energy(), p_img.get_potential_energy(), 1.0e-4
)
assert np.isclose(r_img.get_forces(), p_img.get_forces(), 1.0e-4).all()
assert np.isclose(r_img.positions, p_img.positions, 1.0e-4).all()
assert np.isclose(r_img.cell, p_img.cell, 1.0e-4).all()
assert np.isclose(cell0, p_img.cell, 1.0e-4).all()


def test_socket_read_diff_cell():
"""Test parsing multi-image static files from socket. The volumes of the cells change"""
from sparc.io import read_sparc
import numpy as np

from sparc.io import read_sparc

bundle = test_output_dir / "Al_socket_volchange.sparc"
parsed_images = read_sparc(bundle, ":")
assert len(parsed_images) == 10
cell0 = parsed_images[0].cell

ratios = [1.0, 1.01, 1.02, 1.03, 1.04, 1.0, 0.99, 0.98, 0.97, 0.96]
for i, p_img in enumerate(parsed_images):
print(i)
bundle_ref = bundle / "single-points" / f"sp_image{i:02d}"
r_img = read_sparc(bundle_ref)
# For systems with cell change, force error may be larger. This may be due to mesh resizing
assert np.isclose(
r_img.get_potential_energy(), p_img.get_potential_energy(), rtol=1.0e-3
)
assert np.isclose(r_img.get_forces(), p_img.get_forces(), atol=6.0e-2).all()
# Wrap may be necessary for p_img
p_img.wrap()
assert np.isclose(r_img.positions, p_img.positions, 1.e-4).all()
assert np.isclose(r_img.cell, p_img.cell, 1.e-4).all()
assert np.isclose(cell0, p_img.cell, 1.e-4).all()
assert np.isclose(r_img.get_potential_energy(), p_img.get_potential_energy(), 1.e-4)
assert np.isclose(r_img.get_forces(), p_img.get_forces(), 1.e-4).all()
assert np.isclose(r_img.positions, p_img.positions, 1.0e-4).all()
assert np.isclose(r_img.cell, p_img.cell, 1.0e-4).all()
assert np.isclose(cell0 * ratios[i], p_img.cell, 1.0e-4).all()

0 comments on commit 2cc02c6

Please sign in to comment.