aiida_vasp.utils.mock_code#

Mock vasp code.

A more advanced way of mocking. The input of a calculation can be hash and match from a repository of calculation that has been run.

This way we can perform tests for workchain without the need for injecting test code into the workchain logic itself.

Module Contents#

Classes#

MockRegistry

A class to create and manage a registry of completed calculations.

VaspMockRegistry

Registry of mock code for VASP

MockVasp

Mock VaspExecutable

Functions#

data_path

Return a path to a file in the test data directory.

get_hash

Return the hash for a dictionary of arbitrary items.

copy_from_aiida

Copy objects from aiida repository.

Data#

API#

aiida_vasp.utils.mock_code.INPUT_OBJECTS = ('POSCAR', 'INCAR', 'KPOINTS')#
aiida_vasp.utils.mock_code.DEFAULT_EXCLUDED = ('POTCAR', '.aiida', 'LOCPOT', 'DOSCAR', 'PCDAT', 'EIGENVAL', 'OSZICAR', 'PCDAT', 'XDATCAR', 'REPORT...#
aiida_vasp.utils.mock_code.data_path(*args: str) str[source]#

Return a path to a file in the test data directory.

aiida_vasp.utils.mock_code.get_hash(dict_obj: dict | list) tuple[str, list[bytes]][source]#

Return the hash for a dictionary of arbitrary items.

This is not meant to be robust for all cases, but should be OK for matching hashes of calculation inputs.

The point here is to make the value invariant to the permutation of key orders.

class aiida_vasp.utils.mock_code.MockRegistry(base_path: str | pathlib.Path | list[str | pathlib.Path] | None = None)[source]#

A class to create and manage a registry of completed calculations.

Calculations are identified using the hash of the parsed inputs.

The class uses environmental variables to control its behaviour:

  • MOCK_{CODE}_REG_BASE: Prefix to the upload relative path

  • MOCK_{CODE}_UPLOAD_PREFIX: Prefix to the upload relative path

The {CODE} is replaced with the CODE_NAME class attribute of the subclass.

Initialization

Instantiate and Registry

CODE_NAME = 'ABSTRACT'#
append_search_path(path: str | pathlib.Path) None[source]#

Add a path to the list of search paths

property base_path: pathlib.Path#

Return the base repository path of the registry

property search_paths: list[pathlib.Path]#

Return a list of all search paths

_setup_logger(level: int = logging.INFO) None[source]#

Setup the logger

scan() None[source]#

Scan the base folder and locate input/output folders

get_path_by_hash(hash_val: str) pathlib.Path[source]#

Return the output folder for a given hash

get_path_by_name(name: str) pathlib.Path[source]#

Return the output folder for a given hash

extract_calc_by_path(rel_path: pathlib.Path | str, dst_path: pathlib.Path | str, include_inputs: bool = True) None[source]#

Copy the content of a give hash to a destination.

Parameters:
  • rel_path – The relative path of the calculation folder to be extracted.

  • dst – The destination path to be extracted to - must already exists.

extract_calc_by_hash(hash_val: str, dst: pathlib.Path | str, include_inputs: bool = False) None[source]#

Extract an registerred calculation using hash.

upload_calc(folder: pathlib.Path, rel_path: pathlib.Path | str, excluded_object: list[str] | None = None, included_object: list[str] | None = None) None[source]#

Register a calculation folder to primary search path of the registry

_register_folder(calc_base: pathlib.Path) None[source]#

Register a folder inside the repository

classmethod from_env() aiida_vasp.utils.mock_code.MockRegistry[source]#

Instantiate from environmental variable

abstract static compute_hash(folder: pathlib.Path) str[source]#

Compute the hash for a target folder

abstract upload_aiida_calc(calc_node: aiida.orm.CalcJobNode, rel_path: str | pathlib.Path, excluded_names: list[str] | None = None) None[source]#

Update a calculation into the registry

abstract upload_aiida_work(work_node: aiida.orm.WorkChainNode, rel_path: str | pathlib.Path) None[source]#

Update all calculations run by an workflow into the registry

get_upload_prefix() str[source]#

Prefix of the name of the calculation folder

class aiida_vasp.utils.mock_code.VaspMockRegistry(base_path: str | pathlib.Path | list[str | pathlib.Path] | None = None)[source]#

Bases: aiida_vasp.utils.mock_code.MockRegistry

Registry of mock code for VASP

The registry’s base folder defaults to the test_data folder, but can be modified with the MOCK_VASP_REG_BASE environmental variable.

Initialization

Instantiate and Registry

CODE_NAME = 'VASP'#
upload_aiida_calc(calc_node: aiida.orm.CalcJobNode, rel_path: str | pathlib.Path, excluded_names: list[str] | None = None) None[source]#

Register an aiida calc_class

upload_aiida_work(work_node: aiida.orm.WorkChainNode, rel_path: str | pathlib.Path) None[source]#

Upload all calculations in a workchain node

static compute_hash(folder: pathlib.Path) str[source]#

Compute the hash of a input folder

class aiida_vasp.utils.mock_code.MockVasp(workdir: str | pathlib.Path, registry: aiida_vasp.utils.mock_code.VaspMockRegistry, vasp_cmd: str | list[str] | None = None, stdout_fname: str = 'vasp_output')[source]#

Mock VaspExecutable

Initialization

Mock VASP executable that copies over outputs from existing calculations. Inputs are hash and looked for.

Notice that we do not set the hash value at init of workdir as we allow the unit of the MockVasp at any point, typically, you are preparing for a VASP calculation. Only when you execute VASP is the files checked, in this case when executing run. Thus, we calculate the hash of the workdir only then.

If the vasp_cmd is provided the mock vasp will run the command if needed and upload the results to the registry. This can be useful for generating test/demo data.

run(debug: bool = True) None[source]#

Run the mock vasp

property is_runnable: bool#

Check if the mock code can be executed.

aiida_vasp.utils.mock_code.copy_from_aiida(name: str, node: aiida.orm.Node, dst: pathlib.Path) None[source]#

Copy objects from aiida repository.

Parameters:
  • name – The full name (including the parent path) of the object.

  • node – Node object for which the objects in the repo to be copied.

  • dst – Path of the destination folder.

This is a recursive function so directory copying also works.