Skip to content

hecss.parallel

Support for parallel/async calulations.

__all__ = [] module-attribute

__calculate_aio(self, atoms=None, properties=('energy',), system_changes=tuple(calculator.all_changes)) async

Do a VASP calculation in the specified directory.

This will generate the necessary VASP input files, and then execute VASP. After execution, the energy, forces. etc. are read from the VASP output files.

This is an asyncio version of the function.

Source code in hecss/parallel.py
@patch
async def __calculate_aio(self: Vasp,
                        atoms=None,
                        properties=('energy', ),
                        system_changes=tuple(calculator.all_changes)
                       ):
    """
    Do a VASP calculation in the specified directory.

    This will generate the necessary VASP input files, and then
    execute VASP. After execution, the energy, forces. etc. are read
    from the VASP output files.

    This is an asyncio version of the function.
    """
    # Check for zero-length lattice vectors and PBC
    # and that we actually have an Atoms object.
    check_atoms(atoms)

    self.clear_results()

    if atoms is not None:
        self.atoms = atoms.copy()

    command = self.make_command(self.command)
    # Make sure the working directory exists
    with workdir(self.directory, mkdir=True):
        pass
    self.write_input(self.atoms, properties, system_changes)

    with self._txt_outstream() as out:
        errorcode = await self._arun(command=command,
                                     out=out,
                                     directory=self.directory)

    if errorcode:
        raise calculator.CalculationFailed(
            '{} in {} returned an error: {:d}'.format(
                self.name, self.directory, errorcode))

    # Read results from calculation
    self.update_atoms(atoms)
    self.read_results()
    return errorcode

__run_async(func, *args, **kwargs)

Run async methods detecting running loop in jupyter.

Source code in hecss/parallel.py
def __run_async(func, *args, **kwargs):
    '''
    Run async methods detecting running loop in jupyter.
    '''
    try:
        loop = asyncio.get_running_loop()
    except RuntimeError:  # 'RuntimeError: There is no current event loop...'
        loop = None

    if loop and loop.is_running():        
        # print('Async event loop already running. Running in new thread.')
        # Create a separate thread so we can block before returning
        with ThreadPoolExecutor(1) as pool:
            result = pool.submit(lambda: asyncio.run(func(*args, **kwargs))).result()
    else:
        # print('Starting new event loop')
        result = asyncio.run(func(*args, **kwargs))
    return result