Tools and helper functions for abinit calculations

class Condition(cmap=None)[source]

Bases: object

This object receives a dictionary that defines a boolean condition whose syntax is similar to the one used in mongodb (albeit not all the operators available in mongodb are supported here).


$gt: {field: {$gt: value} }

$gt selects those documents where the value of the field is greater than (i.e. >) the specified value.

$and performs a logical AND operation on an array of two or more expressions (e.g. <expression1>, <expression2>, etc.) and selects the documents that satisfy all the expressions in the array.

{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }

Consider the following example:

db.inventory.find( { qty: { $gt: 20 } } ) This query will select all documents in the inventory collection where the qty field value is greater than 20. Consider the following example:

db.inventory.find( { qty: { $gt: 20 } } ) db.inventory.find({ $and: [ { price: 1.99 }, { qty: { $lt: 20 } }, { sale: true } ] } )

classmethod as_condition(obj)[source]

Convert obj into Condition

class Directory(path)[source]

Bases: object

Very simple class that provides helper functions wrapping the most commonly used functions defined in os.path.


Directory basename.


Remove all files in the directory tree while preserving the directory

copy_abiext(inext, outext)[source]

Copy the Abinit file with extension inext to a new file withw extension outext


True if file exists.


Abinit adds the idir-ipert index at the end of the 1DEN file and this breaks the extension e.g. out_DEN1. This method scans the files in the directories and returns a list of namedtuple Each named tuple gives the path of the 1DEN file and the pertcase index.


Abinit adds the idir-ipert index at the end of the 1WF file and this breaks the extension e.g. out_1WF4. This method scans the files in the directories and returns a list of namedtuple Each named tuple gives the path of the 1FK file and the pertcase index.


ABINIT produces lots of out_TIM1_DEN files for each step and we need to find the lat one in order to prepare the restart or to connect other tasks to the structural relaxation.

This function finds all the TIM?_DEN files in self and return a namedtuple (path, step) where path is the path of the last TIM?_DEN file and step is the iteration number. Returns None if the directory does not contain TIM?_DEN files.


Returns the absolute path of the ABINIT file with extension ext. Support both Fortran files and netcdf files. In the later case, we check whether a file with extension ext + ”.nc” is present in the directory. Returns empty string is file is not present.

  • ValueError if multiple files with the given ext are found.
  • This implies that this method is not compatible with multiple datasets.

Return the list of absolute filepaths in the directory.


String of tokens separated by “|”. Each token represents a pattern. If wildcard is not None, we return only those files that match the given shell pattern (uses fnmatch). .. rubric:: Example

wildcard=”.nc|.pdf” selects only those files that end with .nc or .pdf


Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist.


Absolute path of the directory.


Return the absolute path of filename in the directory.


Join two or more pathname components, inserting ‘/’ as needed. If any component is an absolute path, all previous path components will be discarded.


Relative path.


Remove the files with the given extensions. Unlike rmtree, this function preserves the directory path. Return list with the absolute paths of the files that have been removed.

rename_abiext(inext, outext)[source]

Rename the Abinit file with extension inext with the new extension outext


Recursively delete the directory tree

Create a simbolic link (outext –> inext). The file names are implicitly given by the ABINIT file extension.


outdir.symlink_abiext(‘1WF’, ‘DDK’)

creates the link out_DDK that points to out_1WF

Return: 0 if success.

Raise: RuntimeError

class Editor(editor=None)[source]

Bases: object

Wrapper class that calls the editor specified by the user or the one specified in the $EDITOR env variable.

If editor is None, $EDITOR is used.

edit_files(fnames, ask_for_exit=True)[source]
static user_wants_to_exit()[source]

Show an interactive prompt asking if exit is wanted.

class File(path)[source]

Bases: object

Very simple class used to store file basenames, absolute paths and directory names. Provides wrappers for the most commonly used functions defined in os.path.


File basename.


Change the access permissions of a file.


Absolute path of the directory where the file is located.


True if file exists.


Results from os.stat


Return the size, in bytes, of path. Return 0 if the file is empty or it does not exist.


True if self is a NetCDF file


Make the directory where the file is located.


Recursively move a file or directory to another location. This is similar to the Unix “mv” command.


Absolute path of the file.


Read data from file.


Read lines from files.


Relative path.


Remove the file.


Write string to file.


Write a list of strings to file.

class FilepathFixer[source]

Bases: object

This object modifies the names of particular output files produced by ABINIT so that the file extension is preserved. Having a one-to-one mapping between file extension and data format is indeed fundamental for the correct behaviour of abinit since:

  • We locate the output file by just inspecting the extension
  • We select the variables that must be added to the input file on the basis of the extension specified by the user during the initialization of the AbinitFlow.

Unfortunately, ABINIT developers like to append extra stuff to the initial extension and therefore we have to call FilepathFixer to fix the output files produced by the run.


>>> fixer = FilepathFixer()
>>> assert fixer.fix_paths('/foo/out_1WF17') == {'/foo/out_1WF17': '/foo/out_1WF'}
>>> assert fixer.fix_paths('/foo/') == {'/foo/': '/foo/'}

Fix the filenames in the iterable paths

Returns:Mapping old_path –> new_path
Return type:old2new
class SparseHistogram(items, key=None, num=None, step=None)[source]

Bases: object

plot(ax=None, **kwargs)[source]
Plot the histogram with matplotlib, returns matplotlib figure.

keyword arguments controlling the display of the figure:

kwargs Meaning
title Title of the plot (Default: None).
show True to show the figure (default: True).
savefig ‘abc.png’ or ‘abc.eps’ to save the figure to a file.
size_kwargs Dictionary with options passed to fig.set_size_inches example: size_kwargs=dict(w=3, h=4)
tight_layout True if to call fig.tight_layout (default: False)

List with all the ABINIT extensions that are registered.


Split the ABINIT extension from a filename. “Extension” are found by searching in an internal database.

Returns “(root, ext)” where ext is the registered ABINIT extension The final ”.nc” is included (if any)

>>> assert abi_splitext("foo_WFK") == ('foo_', 'WFK')
>>> assert abi_splitext("/home/guido/") == ('foo_bar_', '')

Convert a string into a boolean.

>>> assert as_bool(True) is True and as_bool("Yes") is True and as_bool("false") is False

Evaluates the RPN form produced my map2rpn.


Returns a dictionary with the ABINIT variables that must be used to read the file with extension ext.

map2rpn(map, obj)[source]

Convert a Mongodb-like dictionary to a RPN list of operands and operators.

Reverse Polish notation (RPN) is a mathematical notation in which every operator follows all of its operands, e.g.

3 - 4 + 5 –> 3 4 - 5 +

>>> d = {2.0: {'$eq': 1.0}}
>>> assert map2rpn(d, None) == [2.0, 1.0, '$eq']