Skip to content

Commit

Permalink
Add unittests for command
Browse files Browse the repository at this point in the history
  • Loading branch information
alchem0x2A committed Aug 20, 2021
1 parent ea56d45 commit 7e357a2
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/ex02_h2_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

d = 0.9575
h2_root = Atoms("H2", positions=[(d, 0, 0), (0, 0, 0)], cell=[8, 8, 8], pbc=True)
rootdir = Path(__file__).parents[1] / "sandbox"
rootdir = Path(__file__).resolve().parents[1] / "sandbox"
fmax = 0.005
ediff = 1e-7

Expand Down
4 changes: 2 additions & 2 deletions examples/worker-cpu-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ metadata:
spec:
restartPolicy: Never
containers:
- image: ulissigroup/kubeflow_vasp:amptorch
- image: ulissigroup/kubeflow_vasp:beef_vdw
imagePullPolicy: Always
args: [dask-worker, --nthreads, '1', --no-dashboard, --memory-limit, 2GB, --death-timeout, '60']
name: dask
env:
- name: PYTHONPATH
value: "/home/jovyan/data/vasp-interactive-test:/home/jovyan/data/vasp-interactive-test/examples"
- name: ASE_VASP_COMMAND
value: "mpirun -np 4 --mca btl_vader_single_copy_mechanism none /opt/vasp.6.1.2_pgi_mkl/bin/vasp_std"
value: "mpirun -np 4 --mca btl_vader_single_copy_mechanism none /opt/vasp.6.1.2_pgi_mkl/bin/vasp_gam"
resources:
limits:
cpu: "4"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="vasp-interactive",
version="0.0.1",
version="0.0.2",
packages=["vasp_interactive", "vasp_interactive.kubernetes"],
install_requires=[
"ase",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_class():

"""Test if VaspInteractive correctly write inputs
"""
atoms = molecule("C2H2", vacuum=5)
atoms = molecule("C2H2", vacuum=5, pbc=True)
with tempfile.TemporaryDirectory() as tempdir:
tempdir = Path(tempdir)
calc = VaspInteractive(xc="pbe", directory=tempdir)
Expand Down Expand Up @@ -56,7 +56,7 @@ def test_output():

"""Test if VaspInteractive correctly write inputs
"""
atoms = molecule("C2H2", vacuum=5)
atoms = molecule("C2H2", vacuum=5, pbc=True)
with tempfile.TemporaryDirectory() as tempdir:
# No value provided, default to vasp.out
calc = VaspInteractive(xc="pbe", directory=tempdir)
Expand Down
53 changes: 53 additions & 0 deletions tests/test_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Testing if environmental variable correctly passes to process
"""
import pytest
from vasp_interactive import VaspInteractive
import tempfile
from pathlib import Path
import os
import sys


def test_command_env():
from ase.build import molecule

# When testing under the github action system, it should be
# `mpirun -np 1 <options> <path>/vasp_gam`
default_command = os.environ["VASP_COMMAND"]

"""Test if VaspInteractive correctly catches command if env changes
"""
atoms = molecule("H2", vacuum=5, pbc=True)
with tempfile.TemporaryDirectory() as tempdir:
tempdir = Path(tempdir)
calc = VaspInteractive(xc="pbe", directory=tempdir)
with calc:
atoms.calc = calc
assert calc.command is None
command = calc.make_command()
assert command == default_command
atoms.get_potential_energy()
# calc.process the shell mode, so args is a string
assert calc.process.args == default_command

# Harmless change
new_command = default_command.replace("-np", "-n")
calc.command = new_command
# Should be no change
assert calc.process.args == default_command

with calc:
atoms.rattle()
atoms.get_potential_energy()
print(calc.process.args)
assert calc.process.args == new_command

with calc:
# reset the environ
os.environ["VASP_COMMAND"] = "echo TEST && " + default_command
atoms.rattle()
calc.command = None
atoms.get_potential_energy()
assert calc.command is None
print(calc.process.args)
assert "TEST" in calc.process.args
13 changes: 4 additions & 9 deletions vasp_interactive/vasp_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ def __init__(
self.process = None
self.allow_restart_process = allow_restart_process

# Make command a list of args for Popen
cmd = self.make_command(self.command)
if isinstance(cmd, str):
cmd = cmd.split()
self._args = cmd

# Ionic steps counter. Note this number will be 1 more than that in ase.optimize
self.steps = 0
# Is the relaxation finished?
Expand Down Expand Up @@ -196,7 +190,6 @@ def _stdin(self, text, out=None, ending="\n"):
raise RuntimeError("VaspInteractive does not have the VASP process.")

def _stdout(self, text, out=None):
""" """
if out is not None:
out.write(text)

Expand Down Expand Up @@ -240,9 +233,11 @@ def _run(self, atoms, out):
self.initialize(atoms)
self.write_input(atoms)
self._stdout("Starting VASP for initial step...\n", out=out)
# Drop py2 support
# Dynamic generation of command args
command = self.make_command(self.command)
self.process = Popen(
self._args,
command,
shell=True,
stdout=PIPE,
stdin=PIPE,
stderr=PIPE,
Expand Down

0 comments on commit 7e357a2

Please sign in to comment.