Skip to content

Commit

Permalink
Merge pull request duartegroup#134 from duartegroup/v1.2.2
Browse files Browse the repository at this point in the history
v1.2.2
  • Loading branch information
t-young31 authored Mar 24, 2022
2 parents 5d5f151 + f42f6f7 commit 239d9fb
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 12 deletions.
2 changes: 1 addition & 1 deletion autode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
- Merge when tests pass
"""

__version__ = '1.2.1'
__version__ = '1.2.2'


__all__ = [
Expand Down
3 changes: 2 additions & 1 deletion autode/calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ def file_lines(self) -> List[str]:
if self.filename is None or not os.path.exists(self.filename):
raise ex.NoCalculationOutput

return open(self.filename, 'r', encoding="utf-8").readlines()
file = open(self.filename, 'r', encoding='utf-8', errors='ignore')
return file.readlines()

@property
def exists(self) -> bool:
Expand Down
51 changes: 45 additions & 6 deletions autode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import shutil
from time import time
from typing import Any, Optional, List
from typing import Any, Optional, Sequence
from functools import wraps
from subprocess import Popen, PIPE, STDOUT
from tempfile import mkdtemp
Expand Down Expand Up @@ -99,8 +99,10 @@ def run_external(params, output_filename):


@check_sufficient_memory
def run_external_monitored(params, output_filename, break_word='MPI_ABORT',
break_words=None):
def run_external_monitored(params: Sequence[str],
output_filename: str,
break_word: str = 'MPI_ABORT',
break_words: Optional[str] = None):
"""
Run an external process monitoring the standard output and error for a
word that will terminate the process
Expand All @@ -111,7 +113,6 @@ def run_external_monitored(params, output_filename, break_word='MPI_ABORT',
output_filename (str):
Keyword Arguments:
break_word (str): String that if found will terminate the process
break_words (list(str) | None): List of break_word-s
Expand Down Expand Up @@ -173,8 +174,8 @@ def wrapped_function(*args, **kwargs):
return func_decorator


def work_in_tmp_dir(filenames_to_copy: Optional[List[str]] = None,
kept_file_exts: Optional[List[str]] = None,
def work_in_tmp_dir(filenames_to_copy: Optional[Sequence[str]] = None,
kept_file_exts: Optional[Sequence[str]] = None,
use_ll_tmp: bool = False):
"""Execute a function in a temporary directory.
Expand Down Expand Up @@ -410,3 +411,41 @@ def hashable(_method_name: str, _object: Any):
"""Multiprocessing requires hashable top-level functions to be executed,
so convert a method into a top-level function"""
return getattr(_object, _method_name)


def run_in_tmp_environment(**kwargs):
"""
Apply a set of environment variables, execute a function and reset them
"""

class EnvVar:
def __init__(self, name, val):
self.name = str(name)
self.val = os.getenv(str(name), None)
self.new_val = str(val)

env_vars = [EnvVar(k, v) for k, v in kwargs.items()]

def func_decorator(func):

@wraps(func)
def wrapped_function(*args, **_kwargs):

for env_var in env_vars:
logger.info(f'Setting the {env_var.name} to {env_var.new_val}')
os.environ[env_var.name] = env_var.new_val

result = func(*args, **_kwargs)

for env_var in env_vars:
if env_var.val is None:
# Remove from the environment
os.environ.pop(env_var.name)
else:
# otherwise set it back to the old value
os.environ[env_var.name] = env_var.val

return result

return wrapped_function
return func_decorator
6 changes: 3 additions & 3 deletions autode/wrappers/XTB.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from autode.config import Config
from autode.constants import Constants
from autode.exceptions import AtomsNotFound, CouldNotGetProperty
from autode.utils import work_in_tmp_dir
from autode.utils import work_in_tmp_dir, run_in_tmp_environment
from autode.log import logger


Expand Down Expand Up @@ -162,9 +162,9 @@ def execute(self, calc):
@work_in_tmp_dir(filenames_to_copy=calc.input.filenames,
kept_file_exts=('.xyz', '.out', '.pc', '.grad'),
use_ll_tmp=True)
@run_in_tmp_environment(OMP_NUM_THREADS=calc.n_cores,
GFORTRAN_UNBUFFERED_ALL=1)
def execute_xtb():
logger.info(f'Setting the number of OMP threads to {calc.n_cores}')
os.environ['OMP_NUM_THREADS'] = str(calc.n_cores)

logger.info(f'Running XTB with: {" ".join(flags)}')
run_external(params=[calc.method.path, calc.input.filename]+flags,
Expand Down
12 changes: 12 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ Changelog
=========


1.2.2
--------
----------

Bugfix release.


Bug Fixes
*********
- Fixes output redirection from XTB calculations resulting in missed lines on Mac


1.2.1
--------
----------
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
extra_link_args=["-std=c++11"])]

setup(name='autode',
version='1.2.1',
version='1.2.2',
packages=['autode',
'autode.conformers',
'autode.pes',
Expand Down
18 changes: 18 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,21 @@ def f(mol):
m.graph = None

f(m) # No graph


def test_tmp_env():

os.environ['OMP_NUM_THREADS'] = '1'

@utils.run_in_tmp_environment(tmp_key_str='tmp_value',
tmp_key_int=1,
OMP_NUM_THREADS=9999)
def f():
assert os.environ['tmp_key_int'] == '1'
assert os.environ['tmp_key_str'] == 'tmp_value'
assert os.environ['OMP_NUM_THREADS'] == '9999'

f()
assert 'tmp_key_int' not in os.environ
assert 'tmp_key_str' not in os.environ
assert os.environ['OMP_NUM_THREADS'] == '1'

0 comments on commit 239d9fb

Please sign in to comment.