aiida_castep.utils package#

Submodules#

aiida_castep.utils.dos module#

Module for density of state proces processing

class aiida_castep.utils.dos.DOSProcessor(bands_data, weights, smearing=0.05, min_eng=None, max_eng=None, bin_width_internal=0.001)[source]#

Bases: object

Class for post-processing DOS data

__dict__ = mappingproxy({'__module__': 'aiida_castep.utils.dos', '__doc__': '\n    Class for post-processing DOS data\n    ', '__init__': <function DOSProcessor.__init__>, 'has_spin': <property object>, 'get_dos': <function DOSProcessor.get_dos>, 'min_eigen_value': <property object>, 'max_eigen_value': <property object>, 'bin_width': <property object>, '__dict__': <attribute '__dict__' of 'DOSProcessor' objects>, '__weakref__': <attribute '__weakref__' of 'DOSProcessor' objects>, '__annotations__': {}})#
__init__(bands_data, weights, smearing=0.05, min_eng=None, max_eng=None, bin_width_internal=0.001)[source]#

Instantiate an DOSProcessor object

Parameters:
  • bands_data – An np array of the eigenvalues. The order of the dimensions should be (Nspin, Nkpoints, Nbands), or (Nkpoints, Nbands).

  • weights – An 1D array of the weights of each kpoint.

  • smearing – Width for Gaussian Smearing

  • max_eng – Maximum energy for the bins

  • min_eng – Minimum energy for the bins

  • bin_width_internal – Width of the bin used for counting number of states.

__module__ = 'aiida_castep.utils.dos'#
__weakref__#

list of weak references to the object (if defined)

property bin_width#

Width of the energy bins

get_dos(dropdim=False, npoints=2000)[source]#

Process the density of states by Gaussian smearing

Here we use a two-step process. First the eigenvalues counted nto a fine grid, and under which a 1d gaussian filter is applied. Then the fine grid is resampled to the normal bin size. This way, energies that are near the bin edges are smeared with better accuracy.

Parameters:
  • dropdim – Squeeze the first dimension of the output array, e.g. output 1D array if there is only a single spin channel.

  • npoints – Number of the points for the output DOS array.

Returns:

A 1D array of the density of states.

property has_spin#
property max_eigen_value#

Maximum eigenvalue

property min_eigen_value#

Minimum eigenvalue

aiida_castep.utils.dos.gaussian_kernel(sigma, bin_width, truncate=10)[source]#

Computer the gaussian kernel used for performing convolution

Parameters:
  • sigma – The sigma of the gaussian.

  • bin_width – Width of the energy bins.

  • truncate – Beyond how many sigma should the values be truncated.

Returns a 1D array of the gaussian kernel.

aiida_castep.utils.mock module#

Mock running CASTEP

class aiida_castep.utils.mock.MockOutput(base_dir=None)[source]#

Bases: object

__dict__ = mappingproxy({'__module__': 'aiida_castep.utils.mock', '__init__': <function MockOutput.__init__>, 'calculate_hash': <function MockOutput.calculate_hash>, '_reg_file': <property object>, 'registry': <property object>, 'register_node': <function MockOutput.register_node>, 'register': <function MockOutput.register>, 'copy_results': <function MockOutput.copy_results>, 'run': <function MockOutput.run>, '__dict__': <attribute '__dict__' of 'MockOutput' objects>, '__weakref__': <attribute '__weakref__' of 'MockOutput' objects>, '__doc__': None, '__annotations__': {}})#
__init__(base_dir=None)[source]#

Initialize the object

__module__ = 'aiida_castep.utils.mock'#
__weakref__#

list of weak references to the object (if defined)

property _reg_file#

Path to the registry file

calculate_hash(path)[source]#
copy_results(rel_path)[source]#

Copy existing calculation to the folder

register(seedpath, tag=None)[source]#

Register completed calculation. Such calculation must be in the directroy tree of the mock_castep.py

register_node(calcjob, tag=None)[source]#

Register the result of a CalcJob node :param calcjob: The CalcJob node to be used :param tag: Tag for the results, used as the folder name

property registry#

Registry, a dictionary loaded from the json

run(seedname)[source]#

Run the ‘Calculation’, if the seed is known we just copy the results Can be overiden by the ‘MOCK_CALC’ environmental varaible , which define the folder of results to be copied

aiida_castep.utils.mock.get_hash(dict_obj)[source]#

Return hash of a dict of strings

aiida_castep.utils.mock.test_hash()[source]#

Test hashing

aiida_castep.utils.mock_code module#

Mock CASTEP 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.

class aiida_castep.utils.mock_code.MockCastep(workdir: Union[str, Path], registry: MockRegistry, seed='aiida')[source]#

Bases: object

Mock CastepExecutable

__dict__ = mappingproxy({'__module__': 'aiida_castep.utils.mock_code', '__doc__': '\n    Mock CastepExecutable\n    ', '__init__': <function MockCastep.__init__>, 'run': <function MockCastep.run>, 'is_runnable': <property object>, '__dict__': <attribute '__dict__' of 'MockCastep' objects>, '__weakref__': <attribute '__weakref__' of 'MockCastep' objects>, '__annotations__': {}})#
__init__(workdir: Union[str, Path], registry: MockRegistry, seed='aiida')[source]#

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

__module__ = 'aiida_castep.utils.mock_code'#
__weakref__#

list of weak references to the object (if defined)

property is_runnable: bool#

Return wether the mock code can be run

run(debug=True)[source]#

Run the mock CASTEP

class aiida_castep.utils.mock_code.MockRegistry(base_path)[source]#

Bases: object

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

Calculations are identified using the hash of the parsed inputs.

__dict__ = mappingproxy({'__module__': 'aiida_castep.utils.mock_code', '__doc__': '\n    A class to create and manage a registry of completed calculations.\n\n    Calculations are identified using the hash of the parsed inputs.\n    ', '__init__': <function MockRegistry.__init__>, '_setup_logger': <function MockRegistry._setup_logger>, 'scan': <function MockRegistry.scan>, 'get_path_by_hash': <function MockRegistry.get_path_by_hash>, 'get_path_by_name': <function MockRegistry.get_path_by_name>, 'compute_hash': <staticmethod object>, 'extract_calc_by_path': <function MockRegistry.extract_calc_by_path>, 'extract_calc_by_hash': <function MockRegistry.extract_calc_by_hash>, 'upload_calc': <function MockRegistry.upload_calc>, '_register_folder': <function MockRegistry._register_folder>, 'upload_aiida_calc': <function MockRegistry.upload_aiida_calc>, 'upload_aiida_work': <function MockRegistry.upload_aiida_work>, '__dict__': <attribute '__dict__' of 'MockRegistry' objects>, '__weakref__': <attribute '__weakref__' of 'MockRegistry' objects>, '__annotations__': {}})#
__init__(base_path)[source]#

Instantiate and Registry

__module__ = 'aiida_castep.utils.mock_code'#
__weakref__#

list of weak references to the object (if defined)

_register_folder(calc_base: Path)[source]#

Register a folder inside the repository

_setup_logger(level=20)[source]#

Setup the logger

static compute_hash(input_folder: Path, seed='aiida')[source]#

Compute the hash of a input folder

extract_calc_by_hash(hash_val, dst, include_inputs=False)[source]#

Extract an registerred calculation using hash.

extract_calc_by_path(rel_path: Path, dst_path: Path, include_inputs: bool = True)[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.

get_path_by_hash(hash_val)[source]#

Return the output folder for a given hash

get_path_by_name(name)[source]#

Return the output folder for a given hash

scan()[source]#

Scan the base folder and locate input/output folders

upload_aiida_calc(calc_node, rel_path: Union[str, Path], excluded_names=None)[source]#

Register an aiida calc_class

upload_aiida_work(worknode, rel_path: Union[str, Path])[source]#

Upload all calculations in a workchain node

upload_calc(folder: Path, rel_path: Union[Path, str], excluded_object=None)[source]#

Register a calculation folder to the repository

aiida_castep.utils.mock_code.copy_from_aiida(name: str, node, dst: Path)[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.

aiida_castep.utils.mock_code.get_hash(dict_obj)[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.

Module contents#

Utility module with useful functions

aiida_castep.utils.ase_to_castep_index(atoms, indices)[source]#

Convert a list of indices to castep style return list of (element, i in the same element)

aiida_castep.utils.atoms_to_castep(atoms, index)[source]#

Convert ase atoms’ index to castep like return (Specie, Ion) Deprecated, use ase_to_castep_index

aiida_castep.utils.band_array_ensure_ndim(array)[source]#

Ensure there are three dimensions for a band-like array.

Sometimes, we general band array with (nk, nb) without the first dimension. This function ensures that there are three dimensions (ns, nk, nb).

aiida_castep.utils.castep_to_atoms(atoms, specie, ion)[source]#

Convert castep like index to ase Atoms index

aiida_castep.utils.compute_kpoints_spacing(cell, grid, unit='2pi')[source]#

Compute the spacing of the kpoints in the reciprocal space. Spacing = 1 / cell_length / mesh for each dimension. Assume orthogonal cell shape.

aiida_castep.utils.desort_atoms_castep(atoms, original_atoms)[source]#

Recover the original ordering of the atoms. CASTEP sort the atoms in the order of atomic number and then the order of appearance internally.

Parameters:

copy – If True then return an copy of the atoms

Returns:

A ase.Atoms object that has be sorted

aiida_castep.utils.export_calculation(node, output_dir, prefix=None)[source]#

Export one calculation a a directory

aiida_castep.utils.generate_ionic_fix_cons(atoms, indices, mask=None, count_start=1)[source]#

create ionic constraint section via indices and ase Atoms

Parameters:
  • atoms – atoms object to be fixed

  • indices – indices of the atoms to be fixed

  • mask – a list of 3 integers, must be 0 (no fix) or 1 (fix this Cartesian)

Returns:

(lines, index of the next constraint)

aiida_castep.utils.generate_rel_fix(atoms, indices, ref_index=0, count_start=1)[source]#

Generate relative constraints

Note

In CASTEP the mass of atoms are coupled in the fix, hence to truelly fix the relative positions you will have to declare all atoms having the same weight using the SPECIES_MASS block.

Parameters:
  • atoms – atoms object to be fixed

  • indices – indices of the atoms to be fixed

  • ref_index – index of the reference atom in amoung the atoms being fixed. Default is the first atom appear in the indices.

Returns:

(lines, index of the next constraint)

aiida_castep.utils.get_remote_folder_info(calc, transport)[source]#

Get the information of the remote folder of a calculation

aiida_castep.utils.get_remote_folder_size(calc, transport)[source]#

Return size of a remote folder of a calculation in MB

aiida_castep.utils.is_castep_sorted(atoms)[source]#

Check if an Atoms object is CASTEP style sorted

aiida_castep.utils.read_popn(fname)[source]#

Read population file into pandas dataframe

aiida_castep.utils.reuse_kpoints_grid(grid, lowest_pk=False)[source]#

Retrieve previously stored kpoints mesh data node. If there is no such KpointsData, a new node will be created. Will return the one with highest pk :param grid: Grid to be retrieved :param bool lowest_pk: If set to True will return the node with lowest pk

Returns:

A KpointsData node representing the grid requested

aiida_castep.utils.sort_atoms_castep(atoms, copy=True, order=None)[source]#

Sort ase.Atoms instance to castep style. A sorted Atoms will have the same index before and after calculation. This is useful when chaining optimisation requires specifying per atoms tags such as SPIN and ionic_constraints. :param order: orders of coordinates. (0, 1, 2) means the sorted atoms will be ascending by x, then y, then z if there are equal x or ys.

Returns:

A ase.Atoms object that is sorted.

aiida_castep.utils.take_popn(seed)[source]#

Take section of population analysis from a seed.castep file Return a list of StringIO of the population analysis section

aiida_castep.utils.traj_to_atoms(traj, combine_ancesters=False, eng_key='enthalpy')[source]#

Generate a list of ASE Atoms given an AiiDA TrajectoryData object :param bool combine_ancesters: If true will try to combine trajectory from ancestor calculations

Returns:

A list of atoms for the trajectory.