dfttools.parsers

dfttools.parsers.elk

Parsing ELK files.

class dfttools.parsers.elk.Bands(file)[source]

Class for parsing band structure from BAND.OUT file.

Args:

data (string): contents of Elk BAND.OUT file
bands()[source]

Retrieves the band structure and strores it into a flattened UnitCell.

Returns:

A Unit cell with the band structure.

Note

This method can be shortcut dfttools.simple.parse(file,"band-structure").

class dfttools.parsers.elk.Input(file)[source]

Class for parsing elk.in input file.

Args:

data (string): contents of elk.in file
kp_path(basis=None)[source]

Calculates k-point path from input and returns it as an array of crystal coordinates.

Kwargs:

basis (Basis): a reciprocal basis for the k-vectors.

Returns:

An array of k-point coordinates in reciprocal basis.
unitCell()[source]

Retrieves the unit cell specified in the input file.

Returns:

The unit cell with atomic positions.
class dfttools.parsers.elk.Output(file)[source]

Class for parsing INFO.OUT of elk output.

Args:

data (string): contents of INFO.OUT file
reciprocal()[source]

Retrieves the reciprocal basis.

Returns:

A reciprocal basis.
unitCell()[source]

Retrieves the unit cell.

Returns:

A Cell object with atomic coordinates.

Note

This method can be shortcut dfttools.simple.parse(file,"unit-cell").

class dfttools.parsers.elk.UnitCellsParser(file)[source]

Class for parsing elk GEOMETRY_OPT.OUT.

Args:

data (string): contents of GEOMETRY_OPT.OUT file
unitCells()[source]

Retrives the geometry optimization steps as unit cells.

Returns:

An array with unit cells.

Note

This method can be shortcut dfttools.simple.parse(file,"unit-cell").

dfttools.parsers.elk.bands

alias of Bands

dfttools.parsers.elk.input

alias of Input

dfttools.parsers.elk.output

alias of Output

dfttools.parsers.elk.unitcells

alias of UnitCellsParser

dfttools.parsers.generic

Contains helper routines to parse text.

class dfttools.parsers.generic.AbstractJSONParser(data)[source]

A root class for JSON parsers.

Args:

data (str): text representation of JSON to parse.
class dfttools.parsers.generic.AbstractParser(file)[source]

A root class for text parsers.

Args:

data (str): text to parse or a file to read.
static valid_filename(name)[source]

Checks whether the file name is an expected one. Used in automatic determination of file format.

Args:

name (str): the file name;

Returns:

True if the name is as expected.
static valid_header(header)[source]

Checks whether the file header is an expected one. Used in automatic determination of file format.

Args:

header (str): the file header;

Returns:

True if the header is as expected.
class dfttools.parsers.generic.StringParser(string)[source]

Simple parser for a string with position memory.

This class can be used to parse words, numbers, floats and arrays from a given string. Based on re, it provides the basic functionality for the rest of parsing libraries.

Args:

string (str): input string to be parsed.

Note

The input string can be further accessed by self.string field. The contents of the string is not copied.

closest(exprs)[source]

Returns the closest match of a set of expressions.

Args:

exprs (list): a set of expressions being matched.

Returns:

Index of the closest expression. The distance is measured to the beginnings of matches. Returns None if none of expressions matched.

Example:

>>> sp = StringParser("This is a large string")
>>> sp.closest(("a","string","this"))
2
distance(expression, n=1, default=None)[source]

Calculates distance to nth occurrence of expression in characters.

Args:

expression (str,re.RegexObject): expression to match. If expression is str then the case is ignored.

Kwargs:

n (int): consequetive number of expression to calculate distance to;

default: return value if StopIteration occurs. Ignored if None.

Returns:

Numbers of characters between caret position and nth occurrence of expression or default if too few occurrences found.

Raises:

StopIteration: No occurrences left in the string.
floatAfter(after, n=None)[source]

Reads floats from string after the next regular expression. Returns the caret to initial position. Particularly useful for getting value for parameter name.

Args:

after (re.RegexObject) - pattern to skip;

Kwargs:

n (array,int,str,re.RegexObject): specifies either shape of the numpy array returned or the regular expression to stop matching before;

Returns:

If n is specified returns a numpy array of a given shape filled with floats from string. Otherwise returns a single float.

Raises:

StopIteration: Not enough floats left in the string.

Example:

>>> sp = StringParser("apples = 3.4; bananas = 7")
>>> sp.floatAfter("bananas")
7.0
>>> sp.floatAfter("apples")
3.4
goto(expression, n=1)[source]

Goes to the beginning of nth occurrence of expression in the string.

Args:

expression (str,re.RegexObject): expression to match. If expression is str then the case is ignored.

Kwargs:

n (int): number of occurrences to match.

Raises:

StopIteration: No occurrences left in the string.
intAfter(after, n=None)[source]

Reads integers from string after the next regular expression. Returns the caret to initial position. Particularly useful for getting value for parameter name.

Args:

after (re.RegexObject) - pattern to skip;

Kwargs:

n (array,int,str,re.RegexObject): specifies either shape of the numpy array returned or the regular expression to stop matching before;

Returns:

If n is specified returns a numpy array of a given shape filled with integers from string. Otherwise returns a single int.

Raises:

StopIteration: Not enough integers left in the string.

Example:

>>> sp = StringParser("cows = 3, rabbits = 5")
>>> sp.intAfter("rabbits")
5
>>> sp.intAfter("cows")
3
matchAfter(after, match, n=None)[source]

Matches pattern after another pattern and returns caret to initial position. Particularly useful for getting value for parameter name. Supports matching arrays via keyword parameter n.

Args:

after (re.RegexObject): pattern to skip;

match (re.RegexObject): pattern to match;

Kwargs:

n (array,int,str,re.RegexObject): specifies either shape of the numpy array returned or the regular expression to stop matching before;

Returns:

If n is specified returns a numpy array of a given shape filled with matches from string. Otherwise returns a single match.

Raises:

StopIteration: Not enough matches left in the string.

The function is equal to

>>> sp = StringParser("Some string")
>>> sp.save()
>>> sp.skip(after)
>>> result = sp.nextMatch(match, n = n)
>>> sp.pop()
nextFloat(n=None)[source]

Reads floats from string.

Kwargs:

n (array,int,str,re.RegexObject): specifies either shape of the numpy array returned or the regular expression to stop matching before;

Returns:

If n is specified returns a numpy array of a given shape filled with floats from string. Otherwise returns a single float. The caret is put behind the last float read.

Raises:

StopIteration: Not enough floats left in the string.

Example:

>>> sp = StringParser("1.9 2.8 3.7 56.2E-2 abc")
>>> sp.nextFloat(2)
array([ 1.9, 2.8])
>>> sp.nextFloat("abc")
array([ 3.7  ,  0.562])
nextInt(n=None)[source]

Reads integers from string.

Kwargs:

n (array,int,str,re.RegexObject): specifies either shape of the numpy array returned or the regular expression to stop matching before;

Returns:

If n is specified returns a numpy array of a given shape filled with integers from string. Otherwise returns a single int. The caret is put behind the last integer read.

Raises:

StopIteration: Not enough integers left in the string.

Example:

>>> sp = StringParser("1 2 3 4 5 6 7 8 9 abc 10")
>>> sp.nextInt((2,3))
array([[1, 2, 3],
    [4, 5, 6]])
>>> sp.nextInt("abc")
array([ 7.,  8.,  9.])
nextLine(n=None)[source]

Reads lines from string.

Kwargs:

n (array,int,str,re.RegexObject): specifies either shape of the numpy array returned or the regular expression to stop matching before;

Returns:

If n is specified returns a numpy array of a given shape filled with lines from string. Otherwise returns a single line. The caret is put behind the last line read.

Raises:

StopIteration: Not enough lines left in the string.
nextMatch(match, n=None)[source]

Basic function for matching data.

Args:

match (re.RegexObject): object to match;

Kwargs:

n (array,int,str,re.RegexObject): specifies either shape of the numpy array returned or the regular expression to stop matching before;

Returns:

If n is specified returns a numpy array of a given shape filled with matches from string. Otherwise returns a single match. The caret is put behind the last match.

Raises:

StopIteration: Not enough matches left in the string.
pop()[source]

Returns to the previously saved position of the parser.

Raises:

IndexError: No saved positions left.
present(expression)[source]

Test the string for the presence of expression.

Args:

expression (str,re.RegexObject): expression to match. If expression is str then the case is ignored.

Returns:

True if expression is matched to the right of current position of the caret.
reset()[source]

Resets the caret to the beginning of the string.

save()[source]

Saves the current position of the parser.

Example:

sp = StringParser("A very important integer 123 describes something.")

sp.skip("very") # The caret is set to the right of "very"
sp.save() # The caret position is saved

sp.skip("describes") # The caret is set to the right of "describes"
# Now the call to StringParser.nextInt() will yield StopIteration.
# To return the caret to the previously saved position
# StringParser.pop() is used.
sp.pop()

# Now it is possible to read the integer
sp.nextInt()
skip(expression, n=1)[source]

Skips n occurrences of expression in the string.

Args:

expression (str,re.RegexObject): expression to match. If expression is str then the case is ignored.

Kwargs:

n (int): number of occurrences to skip.

Raises:

StopIteration: No occurrences left in the string.
skipAll(expression)[source]

Goes to the end of the last occurrence of a given expression in the string.

Args:

expression (str,re.RegexObject): expression to match. If expression is str then the case is ignored.

Raises:

StopIteration: No occurrences left in the string.
startOfLine()[source]

Goes to the beginning of the current line.

dfttools.parsers.generic.parse

alias of StringParser

dfttools.parsers.openmx

Parsing OpenMX files.

class dfttools.parsers.openmx.Bands(file)[source]

Class for parsing band structure from openmx.Band file.

Args:

data (string): contents of OpenMX Band file
bands()[source]

Retrieves bands.

Returns:

A UnitCell object with band energies.

Note

This method can be shortcut dfttools.simple.parse(file,"band-structure").

captions()[source]

Retrieves user-specified K-point captions.

Returns:

A dict with k-point number - k-point caption pairs.
fermi()[source]

Retrieves Fermi energy.

Returns:

Fermi energy.
class dfttools.parsers.openmx.Input(file)[source]

Class for parsing parameter values from OpenMX input files.

Args:

data (str): contents of OpenMX input file
getFloat(parameter)[source]

A shortcut to parser.StringParser.matchAfter designed to obtain float parameter values from textual configuration files.

Args:

parameter (str): parameter name

Returns:

parameter value
getInt(parameter)[source]

A shortcut to parser.StringParser.matchAfter designed to obtain integer parameter values from textual configuration files.

Args:

parameter (str): parameter name

Returns:

parameter value
getNonSpaced(parameter)[source]

A shortcut to parser.StringParser.matchAfter designed to obtain parameter values without spaces from textual configuration files.

Args:

parameter (str): parameter name

Returns:

parameter value
getWord(parameter)[source]

A shortcut to parser.StringParser.matchAfter designed to obtain word-like parameter values from textual configuration files.

Args:

parameter (str): parameter name

Returns:

parameter value
unitCell(l=None, r=None, tolerance=1e-12)[source]

Retrieves atomic position data.

Kwargs:

l,r (UnitCell): left lead and right lead cells. This information is required for parsing the cell from NEGF calculation input file;

tolerance (float): a tolerance for comparing atomic position data from the keywords and from the file itself in aBohr.

Returns:

An input unit cell.

Raises:

ValueError: left and right lead cells are not specified for NEGF input file.

Note

This method can be shortcut dfttools.simple.parse(file,"unit-cell").

class dfttools.parsers.openmx.JSON_DOS(data)[source]

Parses JSON with OpenMX density of states.

Args:

data (str): contents of OpenMX JSON DoS file.
basis()[source]

Retrieves the basis set for density weights.

Returns:

A dict contatining basis description.
energies()[source]

Retrieves corresponding energies.

Returns:

A 1D array with energy values.
ky()[source]

Retrieves values of ky.

Returns:

Values of ky.
kz()[source]

Retrieves values of kz.

Returns:

Values of kz.
weights()[source]

Retrieves the densities.

Returns:

Densities in a 4D array with the following index order: * ky * kz * energy * state
class dfttools.parsers.openmx.Output(file)[source]

Class for parsing parameter values from OpenMX output files.

Args:

data (string): contents of OpenMX output file
convergence()[source]

Retrieves convergence error values.

Returns:

A numpy array of convergence errors.
nat()[source]

Retrieves number of atoms.

Returns:

Number of atoms for the first relaxation step.
neutral_charge()[source]

Retrieves the number of valence electrons in the calculation for the charge neutral system.

Returns:

The number of electrons.
populations()[source]

Retrieves Mulliken populations during scf process.

Returns:

A numpy array where the first index corresponds to iteration number and the second one is atomic ID. The populations are renormalized to reproduce the total charge.
solvers()[source]

Retrieves the solver used for each iteration.

Returns:

A list of solver names.
total()[source]

Retrieves total energy calculated.

Returns:

An array of floats with total energy per each SCF cycle.
unitCells(startingCell, noraise=False)[source]

Retrieves atomic positions data for relax calculation.

Args:

startingCell (qetools.cell.Cell): a unit cell from the input file. It is required since no chemical captions are written in the output.

Kwargs:

noraise (bool): retirieves as much structures as possible without raising exceptions.

Returns:

A set of all unit cells found.
version()[source]

Retrieves OpenMX version as reported in the output.

Returns:

OpenMX program version as string.
class dfttools.parsers.openmx.Transmission(file)[source]

Class for parsing transmission from openmx.tran file.

Args:

data (string): contents of openmx.tran file
energy()[source]

Retrieves energy points of computed transmission.

Returns:

A numpy array containing energies with imaginary part discarded.
total()[source]

Retrieves total transmission 1D array.

Returns:

A numpy array containing total transmission values with imaginary part discarded.
dfttools.parsers.openmx.bands

alias of Bands

dfttools.parsers.openmx.input

alias of Input

dfttools.parsers.openmx.joint_populations(files)[source]

Collects several files with Lowdin populations and parses them into a single object.

Args:

files (list of str): file contents in an array.

Returns:

A dict with joint JSON data.
dfttools.parsers.openmx.output

alias of Output

dfttools.parsers.openmx.populations(s)[source]

Parses JSON with Lowdin populations. Replaces corresponding arrays with numpy objects. Adds units where needed.

Args:

s (str): file contents.

Returns:

A dict with JSON data.
dfttools.parsers.openmx.transmission

alias of Transmission

dfttools.parsers.qe

Parsing Quantum Espresso files.

class dfttools.parsers.qe.Bands(file)[source]

Class for parsing output files created by bands.x binary of Quantum Espresso package.

Args:

data (str): string with the contents of the bands.x output file.
bands(basis)[source]

Retrieves the bands.

Args:

basis (types.Basis): the reciprocal unit cell of the band structure.

Returns:

A unit cell containing band energies.
ne()[source]

Retrieves number of bands from the output file header.

Returns:

Integer number of bands.
nk()[source]

Retrieves number of k points from the output file header.

Returns:

Integer number of k points.
reciprocal_data(basis)[source]

Retrieves the band structure data.

Args:

basis (types.Basis): the reciprocal unit cell of the band structure.

Returns:

A unit cell containing band structure data.

Note

This method can be shortcut dfttools.simple.parse(file,"basis-dependent").

class dfttools.parsers.qe.Cond(file)[source]

Class for parsing output files created by pwcond.x binary of Quantum Espresso package.

Args:

data (str): string with the contents of the output file.
transmission(kind='resolved')[source]

Retrives transmission data from pwcond output file.

Kwargs:

kind (str): either “resolved”, “total”, “states_in” or

“states_out”.

  • resolved: retrieves transmisson matrix elements btw pairs of states;
  • total: retrieves total transmission for all incoming states;
  • states_in, states_out: retrieves only incoming or outgoing states without forming pairs and obtaining transmissions.

Warning

The “resolved” mode essentially picks all transmission matrix elements available in the input. Therefore it will not record incident states without corresponding outgoing states. However these states will show up in “total” regime with zero transmission.

Returns:

A numpy array of records with states and transmission:

  • energy (float): energy of the state in eV;
  • kx,ky (float): x and y components of k-vectors;
  • incoming (float): z component of k-vector of incoming state (only for kind == “resolved” or kind == “total” or kind == “states_in”);
  • outgoing (float): z component of k-vector of outgoing state (only for kind == “resolved” or kind == “states_out”);
  • transmission (float): corresponding transmission matrix element or total transmission (only for kind == “resolved” or kind == “total”).

The k vector projections are given in units of reciprocal lattice.

class dfttools.parsers.qe.Input(file)[source]

Class for parsing input file for pw.x binary of a Quantum Espresso package.

Args:

data (str): string with the contents of the input file.
namelists()[source]

Retrieves all namelists.

Returns:

A dictionary representing this namelist.
unitCell()[source]

Retrieves a unit cell from this input file.

Returns:

A unit cell with atomic coordinates.

Note

This method can be shortcut dfttools.simple.parse(file,"unit-cell").

class dfttools.parsers.qe.Output(file)[source]

Class for parsing output files created by pw.x binary of Quantum Espresso package.

Args:

data (str): string with the contents of the output file.
alat()[source]

Retrieves QE “alat” length.

Returns:

The first record of “alat” in m as float.
bands(index=-1, skipVCRelaxException=False)[source]

Retrieves bands.

Kwargs:

index (int or None): index of a band structure or None if all band structures need to be parsed. Supports negative indexing.

skipVCRelaxException (bool): forces to skip variable cell relaxation exception. In this very special case no reciprocal lattice vectors are provided for the new cells in the output file.

Returns:

A set of Cell objects with bands data stored in Cell.values. Specifically, Cell.values is a n by m array where n is a number of k points and m is a number of bands.

Raises:

Exception: if a variable cell calculation data found.
fermi()[source]

Retrieves Fermi energies.

Returns:

A numpy array containing Fermi energies for each MD step.
force()[source]

Retrieves total force.

Returns:

A numpy array containing total forces for each self-consistent calculation.
routineError()[source]

Checks “error in routine” entry in the file.

Returns:

String with textual information about the error. Returns None if no error recorded.
scf_accuracy()[source]

Retrieves scf convergence history.

Returns:

A numpy array containing estimated errors after all scf steps during calculations. The energies are given in eV.
scf_failed()[source]

Checks for “convergence NOT achieved” signature.

Returns:

True if the signature is present.
scf_steps()[source]

Retrieved number of scf steps.

Returns:

A numpy array containing numbers of consequetive scf steps performed to reach convergences.
success()[source]

Checks for success signature in the end of the file.

Returns:

True if the signature is present.
threads()[source]

Retrieves the number of MPI threads.

Returns:

A number of MPI threads for this calculation as an integer.
time()[source]

Retrieves cpu times.

Returns:

Time stamps measured by Quantum Espresso in a numpy array.
total()[source]

Retrieves total energies.

Returns:

A numpy array containing total energies for each self-consistent calculation.
unitCells()[source]

Retrieves atomic position data.

Returns:

A set of all unit cells found.

Note

This method can be shortcut dfttools.simple.parse(file,"unit-cell").

class dfttools.parsers.qe.Proj(file)[source]

Class for parsing output files created by projwfc.x binary of Quantum Espresso package.

Args:

data (str): string with the contents of the output file.
basis()[source]

Retrieves the localized basis set.

Returns:

A numpy array of records:

  • state (int): ID of the state as provided by Quantum Espresso;
  • atom (int): ID of particular atom in the unit cell;
  • atomName (str): chemical caption;
  • wfc (int): particular wave function ID;
  • m,l (float): quantum numbers for collinear calculation;
  • j,m_j,l (float): quantum numbers for non-collinear calculation.
weights()[source]

Retrieves projection weights onto localized basis set.

Returns:

A k by n by m numpy array with weights.

  • k is a number of k points
  • n is a number of bands
  • m is a localized basis set size
dfttools.parsers.qe.bands

alias of Bands

dfttools.parsers.qe.cond

alias of Cond

dfttools.parsers.qe.input

alias of Input

dfttools.parsers.qe.output

alias of Output

dfttools.parsers.qe.proj

alias of Proj

dfttools.parsers.structure

Parsing various atomic structure files.

class dfttools.parsers.structure.GaussianCube(file)[source]

Class for parsing Gaussian CUBE files.

Args:

data (str): string with the contents of the Gaussian CUBE file.
grid()[source]

Retrieves the grid.

Returns:

A grid.
unitCell()[source]

Retrieves a unit cell.

Returns:

A unit cell with atomic positions data.

Note

This method can be shortcut dfttools.simple.parse(file,"unit-cell").

class dfttools.parsers.structure.XSF(file)[source]

Class for parsing xcrysden files commonly used in solid state visualisations.

Args:

data (str): string with the contents of the xsf file.
grids()[source]

Retrieves the grids.

Returns:

An array of cells with data on the grid. ( grid origin, grid vectors, data on the grid, grid name, grid block name ).
unitCells()[source]

Retrieves unit cells.

Returns:

A set of unit cells with atomic positions data.

Note

This method can be shortcut dfttools.simple.parse(file,"unit-cell").

class dfttools.parsers.structure.XYZ(file)[source]

Class for parsing XYZ structure files.

Args:

data (str): string with the contents of the XYZ file.
unitCell()[source]

Retrieves a unit cell.

Returns:

A unit cell with atomic positions data.

Note

This method can be shortcut dfttools.simple.parse(file,"unit-cell").

dfttools.parsers.structure.cube

alias of GaussianCube

dfttools.parsers.structure.xsf

alias of XSF

dfttools.parsers.structure.xyz

alias of XYZ

dfttools.parsers.vasp

Parsing VASP files.

class dfttools.parsers.vasp.Output(file)[source]

Class for parsing VASP OUTCAR.

Args:

data (string): contents of OUTCAR file
bands()[source]

Retrieves bands.

Returns:

A UnitCells with the band structure.

Note

This method can be shortcut dfttools.simple.parse(file,"band-structure").

fermi()[source]

Retrieves Fermi energies.

Returns:

A numpy array containing Fermi energies for each MD step.
class dfttools.parsers.vasp.Structure(file)[source]

Class for parsing VASP POSCAR.

Args:

data (string): contents of OUTCAR file