# Path: msibi/tests/test_pair.py
# @pytest.fixture
# def init_state(state_number):
#     pair = Pair('0', '1', potential=mie(r, 1.0, 1.0))
#     topology_filename = get_fn('final.hoomdxml')
#     traj_filename = get_fn('state{0}/query.dcd'.format(state_number))
#     t = md.load(traj_filename, top=topology_filename)
#     pair_list = t.top.select_pairs('name "0"', 'name "1"')
#     rdf_filename = get_fn('state{0}/target-rdf.txt'.format(state_number))
#     rdf = np.loadtxt(rdf_filename)
#     alpha = 0.5
#     state_dir = get_fn('state{0}/'.format(state_number))
#     state = State(k_B*T, state_dir=state_dir,
#                   top_file='sys.hoomdxml', name='state0')
#     pair.add_state(state, rdf, alpha, pair_list)
#     return pair, state, rdf
# 
# Path: msibi/utils/exceptions.py
# class UnsupportedEngine(Exception):
#     def __init__(self, engine):
#         message = 'Unsupported engine: "{0}". Supported engines are: {1}'.format(
#             engine, ', '.join(SUPPORTED_ENGINES))
#         super(UnsupportedEngine, self).__init__(message)
# 
# Path: msibi/workers.py
# def run_query_simulations(states, engine='hoomd'):
#     """Run all query simulations for a single iteration. """
# 
#     # Gather hardware info.
#     gpus = _get_gpu_info()
#     if gpus is None:
#         n_procs = cpu_count()
#         gpus = []
#         logging.info("Launching {n_procs} CPU threads...".format(**locals()))
#     else:
#         n_procs = len(gpus)
#         logging.info("Launching {n_procs} GPU threads...".format(**locals()))
# 
#     if engine.lower() == 'hoomd':
#         worker = _hoomd_worker
#     else:
#         raise UnsupportedEngine(engine)
# 
#     n_states = len(states)
#     worker_args = zip(states, range(n_states), itertools.repeat(gpus))
#     chunk_size = ceil(n_states / n_procs)
# 
#     pool = Pool(n_procs)
#     pool.imap(worker, worker_args, chunk_size)
#     pool.close()
#     pool.join()
# 
# Path: msibi/workers.py
# def _post_query(state):
#     """Reload the query trajectory and make backups. """
# 
#     state.reload_query_trajectory()
#     backup_file(os.path.join(state.state_dir, 'log.txt'))
#     backup_file(os.path.join(state.state_dir, 'err.txt'))
#     if state.backup_trajectory:
#         backup_file(state.traj_path)


import os
import pytest
from msibi.tests.test_pair import init_state
from msibi.utils.exceptions import UnsupportedEngine
from msibi.workers import run_query_simulations
from msibi.workers import _post_query



def test_unsupported_engine():
    engine = 'crammps'
    with pytest.raises(UnsupportedEngine):
        run_query_simulations(['margaret', 'thatcher'], engine=engine)


def test_post_query():
    pair, state0, rdf = init_state(0)
    _post_query(state0)
    assert state0.traj is not None

# Path: msibi/tests/test_pair.py
# @pytest.fixture
# def init_state(state_number):
#     pair = Pair('0', '1', potential=mie(r, 1.0, 1.0))
#     topology_filename = get_fn('final.hoomdxml')
#     traj_filename = get_fn('state{0}/query.dcd'.format(state_number))
#     t = md.load(traj_filename, top=topology_filename)
#     pair_list = t.top.select_pairs('name "0"', 'name "1"')
#     rdf_filename = get_fn('state{0}/target-rdf.txt'.format(state_number))
#     rdf = np.loadtxt(rdf_filename)
#     alpha = 0.5
#     state_dir = get_fn('state{0}/'.format(state_number))
#     state = State(k_B*T, state_dir=state_dir,
#                   top_file='sys.hoomdxml', name='state0')
#     pair.add_state(state, rdf, alpha, pair_list)
#     return pair, state, rdf
# 
# Path: msibi/utils/exceptions.py
# class UnsupportedEngine(Exception):
#     def __init__(self, engine):
#         message = 'Unsupported engine: "{0}". Supported engines are: {1}'.format(
#             engine, ', '.join(SUPPORTED_ENGINES))
#         super(UnsupportedEngine, self).__init__(message)
# 
# Path: msibi/workers.py
# def run_query_simulations(states, engine='hoomd'):
#     """Run all query simulations for a single iteration. """
# 
#     # Gather hardware info.
#     gpus = _get_gpu_info()
#     if gpus is None:
#         n_procs = cpu_count()
#         gpus = []
#         logging.info("Launching {n_procs} CPU threads...".format(**locals()))
#     else:
#         n_procs = len(gpus)
#         logging.info("Launching {n_procs} GPU threads...".format(**locals()))
# 
#     if engine.lower() == 'hoomd':
#         worker = _hoomd_worker
#     else:
#         raise UnsupportedEngine(engine)
# 
#     n_states = len(states)
#     worker_args = zip(states, range(n_states), itertools.repeat(gpus))
#     chunk_size = ceil(n_states / n_procs)
# 
#     pool = Pool(n_procs)
#     pool.imap(worker, worker_args, chunk_size)
#     pool.close()
#     pool.join()
# 
# Path: msibi/workers.py
# def _post_query(state):
#     """Reload the query trajectory and make backups. """
# 
#     state.reload_query_trajectory()
#     backup_file(os.path.join(state.state_dir, 'log.txt'))
#     backup_file(os.path.join(state.state_dir, 'err.txt'))
#     if state.backup_trajectory:
#         backup_file(state.traj_path)


# Path: msibi/tests/test_workers.py
import os
import pytest
from msibi.tests.test_pair import init_state
from msibi.utils.exceptions import UnsupportedEngine
from msibi.workers import run_query_simulations
from msibi.workers import _post_query




def test_unsupported_engine():
    engine = 'crammps'
    with pytest.raises(UnsupportedEngine):
        run_query_simulations(['margaret', 'thatcher'], engine=engine)


def test_post_query():
    pair, state0, rdf = init_state(0)
    _post_query(state0)
    assert state0.traj is not None

    assert os.path.isfile(os.path.join(state0.state_dir, '_.0.log.txt'))