Skip to content

Commit

Permalink
Run ASE inside a temporary directory
Browse files Browse the repository at this point in the history
Avoid calcualtions from intefering with each other
  • Loading branch information
WardLT committed Dec 7, 2023
1 parent f12c4d2 commit 5577c30
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
22 changes: 17 additions & 5 deletions jitterbug/parsl.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Wrappers for functions compatible with the Parsl workflow engine"""
import os
from tempfile import TemporaryDirectory
from typing import Optional
from pathlib import Path

import ase

from jitterbug.utils import make_calculator, write_to_string


def get_energy(atoms: ase.Atoms, method: str, basis: Optional[str], **kwargs) -> str:
def get_energy(atoms: ase.Atoms, method: str, basis: Optional[str], scratch_dir: Optional[str] = None, **kwargs) -> str:
"""Compute the energy of an atomic structure
Keyword arguments are passed to :meth:`make_calculator`.
Expand All @@ -15,11 +18,20 @@ def get_energy(atoms: ase.Atoms, method: str, basis: Optional[str], **kwargs) ->
atoms: Structure to evaluate
method: Name of the method to use (e.g., B3LYP)
basis: Basis set to use (e.g., cc-PVTZ)
scratch_dir: Path to the scratch directory.
Returns:
Atoms record serialized with the energy and any other data produced by the calculator
"""

calc = make_calculator(method, basis, **kwargs)
atoms.calc = calc
atoms.get_potential_energy()
return write_to_string(atoms, 'json')
# Make a temporary directory
start_dir = Path.cwd()
tmp = TemporaryDirectory(dir=scratch_dir, prefix='jitterbug_')
try:
os.chdir(tmp.name)
calc = make_calculator(method, basis, directory=tmp.name, **kwargs)
atoms.calc = calc
atoms.get_potential_energy()
return write_to_string(atoms, 'json')
finally:
os.chdir(start_dir)
tmp.cleanup()
7 changes: 7 additions & 0 deletions tests/test_parsl.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
from pathlib import Path

from ase.io import read

from jitterbug.parsl import get_energy
from jitterbug.utils import read_from_string


def test_energy(xyz_path):
mopac_out = Path('mopac.out')
mopac_out.unlink(missing_ok=True)

atoms = read(xyz_path)
atoms_msg = get_energy(atoms, 'pm7', None)
new_atoms = read_from_string(atoms_msg, 'json')
assert 'energy' in new_atoms.calc.results

assert not mopac_out.exists()

0 comments on commit 5577c30

Please sign in to comment.