ScanXml.py

nrelmat.ScanXml.main()[source]

Test driver: Extracts info from a VASP vasprun.xml file.

Command line parameters:

Parameter Type Description
-bugLev integer Debug level. Normally 0.
-inFile string Input file
-maxLev int max xml print level
class nrelmat.ScanXml.ResClass[source]

An empty class used as a data container for results.

nrelmat.ScanXml.parseXml(bugLev, inFile, maxLev, resObj)[source]

Extracts info from the vasprun.xml file from a VASP run, using the Python xml.etree.cElementTree API.

Parameters:

  • bugLev (int): Debug level. Normally 0.
  • inFile (str): Path of the input vasprun.xml file.
  • resObj (class ResClass): data object: we set attributes here.

Returns:

  • None
nrelmat.ScanXml.printNode(node, curLev, maxLev)[source]

Recursively prints an XML tree, given an xml.etree.cElementTree node.

Parameters:

  • node (xml.etree.ElementTree.Element): The root of the XML tree.
  • curLev (int): The current recursion level. Starts at 0 and is incremented for each recursive call.
  • maxLev (int): The max number of levels to print

Returns:

  • None
nrelmat.ScanXml.parseText(path, nmin, nmax, dtype, text)[source]

Splits text into tokens, and converts each token to dtype.

Called by getVec, getRawArray.

Parameters:

  • path (str): the XML tree path to the current node, for error msgs.
  • nmin (int): the minimum num tokens. If fewer are found, throwerr.
  • nmax (int): the maximum num tokens. If more are found, throwerr.
  • dtype (python type): Either int, float, or str: the tokens are converted to dtype.
  • text (str): the text string to be split.

Returns:

  • list of tokens each having type = dtype.
nrelmat.ScanXml.getVec(root, path, nmin, nmax, dtype)[source]

Gets text at the specified XML path, splits, and converts tokens dtype.

Parameters:

  • root (xml.etree.ElementTree.Element): The current XML node.
  • path (str): the XML path from the current node.
  • nmin (int): the minimum num tokens. If fewer are found, throwerr.
  • nmax (int): the maximum num tokens. If more are found, throwerr.
  • dtype (python type): Either int, float, or str: the tokens are converted to dtype.

Returns:

  • list of tokens each having type = dtype.
nrelmat.ScanXml.getString(root, path)[source]

Gets text at the specified XML path, insures there’s just 1, and returns it.

Parameters:

  • root (xml.etree.ElementTree.Element): The current XML node.
  • path (str): the XML path from the current node.

Returns:

  • stripped string.
nrelmat.ScanXml.getScalar(root, path, dtype)[source]

Gets text at the specified XML path, and converts it to dtype.

Parameters:

  • root (xml.etree.ElementTree.Element): The current XML node.
  • path (str): the XML path from the current node.
  • dtype (python type): Either int, float, or str: the token is converted to dtype.

Returns:

  • item having type = dtype.
nrelmat.ScanXml.getRawArray(root, path, nrow, ncol, dtype)[source]

Gets text at the specified XML path, and converts to a 2D numpy array of dtype.

The text must be organized as one text element per row.

Parameters:

  • root (xml.etree.ElementTree.Element): The current XML node.
  • path (str): the XML path from the current node.
  • nrow (int): the number of rows. If 0, allow any number.
  • ncol (int): the number of columns. If 0, allow any number.
  • dtype (python type): Either int, float, or str: the tokens are converted to dtype.

Returns:

  • A regular 2-dimensional numpy array of dtype.
nrelmat.ScanXml.getArrayByPath(bugLev, baseNode, path)[source]

Converts an XML <array> element in vasprun.xml to a map with an array.

See getArrayByNode() for details.

Parameters:

  • bugLev (int): Debug level. Normally 0.
  • baseNode (xml.etree.ElementTree.Element): current XML node
  • path (str): XML path from baseNode for the <array> element.

Returns:

  • A Python array
nrelmat.ScanXml.getArrayByNode(bugLev, arrNode)[source]

Converts an XML <array> element in vasprun.xml to a map with an array.

Calls getArraySub to extract each field. The output Python map has the following structure:

key value
_dimLens numpy vec of dimension lengths. len( dimLens) == n == numDimensions.
_dimNames numpy vec of dimension names. len( dimLens) == n == numDimensions.
_fieldNames numpy vec of field names in the parallel arrays. len( fieldNames) == numVariables.
_fieldTypes numpy vec of field types in the parallel arrays. len( fieldTypes) == numVariables. The types are: ‘i’: int, ‘f’: float, ‘s’: str
<fieldName> numpy n-dimensional array of the field <fieldName>
<fieldName> numpy n-dimensional array of the field <fieldName>
<fieldName> numpy n-dimensional array of the field <fieldName>
...  

Example XML for a 1-dimensional array with 2 fields:

<array name="atoms" >
 <dimension dim="1">ion</dimension>
 <field type="string">element</field>
 <field type="int">atomtype</field>
 <set>
  <rc><c>C </c><c>   1</c></rc>
  <rc><c>Fe</c><c>   2</c></rc>
  <rc><c>Fe</c><c>   2</c></rc>
  <rc><c>Fe</c><c>   2</c></rc>
  <rc><c>Fe</c><c>   2</c></rc>
 </set>
</array>

Example resulting map:

_dimLens: [5]
_dimNames: ['ion']
_fieldNames: ['element' 'atomtype']
_fieldTypes: ['s' 'i']
element: ['C' 'Fe' 'Fe' 'Fe' 'Fe']
atomtype: [1 2 2 2 2]

Multiple dimension arrays also are supported.

The vasprun.xml handling of dimensions is unusual. What they claim is dim="1" actually is the least significant dimension and varies fastest, both in the XML data and in our resulting Python array.

So the XML <dimension dim="1">band</dimension> becomes the last dimension in the resulting Python array.

Example XML for a 3 dimensional array with 2 fields:

<array>
 <dimension dim="1">band</dimension>
 <dimension dim="2">kpoint</dimension>
 <dimension dim="3">spin</dimension>
 <field>eigene</field>
 <field>occ</field>
 <set>
  <set comment="spin 1">
   <set comment="kpoint 1">
    <r>   -6.5058    1.0000 </r>
    <r>    0.2537    1.0000 </r>
    <r>    0.7101    1.0000 </r>
    ...
    <r>    8.1390    0.0000 </r>
   </set>
   <set comment="kpoint 2">
    <r>   -6.3718    1.0000 </r>
    <r>   -0.0841    1.0000 </r>
    <r>    0.7508    1.0000 </r>
   ...
   </set>
   <set comment="kpoint 101">
    <r>   -5.8567    1.0000 </r>
    <r>   -0.0854    1.0000 </r>
    <r>    0.9602    1.0000 </r>
    <r>    7.7174    0.0000 </r>
    <r>    7.8556    0.0000 </r>
   </set>
  </set>
 </set>
</array>

Example resulting map:

_dimLens: [  1 101  22]
_dimNames: ['spin' 'kpoint' 'band']
_fieldNames: ['eigene' 'occ']
_fieldTypes: ['f' 'f']
eigene: [[[-6.5058  0.2537  0.7101 ...,  7.6096  7.8817  8.139 ]
    [-6.3718 -0.0841  0.7508 ...,  7.481   7.8491  7.9595]
    [-6.1332 -0.611   1.0672 ...,  7.0857  7.8655  7.9314]
    ...,
    [-5.8462  0.3687  0.9498 ...,  7.1721  7.4739  7.6631]
    [-5.8016  0.5503  0.5886 ...,  7.4113  7.5794  7.7332]
    [-5.8567 -0.0854  0.9602 ...,  7.2729  7.7174  7.8556]]]
occ: [[[ 1.      1.      1.     ...,  0.      0.      0.    ]
    [ 1.      1.      1.     ...,  0.      0.      0.    ]
    [ 1.      1.      1.     ...,  1.      0.      0.    ]
    ...,
    [ 1.      1.      1.     ...,  1.      0.      0.    ]
    [ 1.      1.      1.     ...,  0.      0.      0.    ]
    [ 1.      1.      1.     ...,  0.9751  0.      0.    ]]]

Parameters:

  • bugLev (int): Debug level. Normally 0.
  • node (xml.etree.ElementTree.Element): The XML node for the <array> element.

Returns:

  • A Python array
nrelmat.ScanXml.getArraySub(bugLev, setNode, ifield, fieldTypes, idim, dimLens)[source]

Decodes the XML for one field (one variable) for an <array>.

Called by getArrayByNode. See getArrayByNode() for details.

Parameters:

  • bugLev (int): Debug level. Normally 0.
  • setNode (xml.etree.ElementTree.Element): the element for <set>.
  • ifield (int): the index number of the field.
  • fieldTypes (int[]): the numeric field types so far. The numeric types are: 0: int, 1: float, 2: str. We take the max of the field types.
  • tp (Python type): The desired type.
  • idim (int): dimension number == recursion level == array nest level. 0 on the first call, 1 for the next level array, etc.
  • dimLens (int[]): list of dimension lengths. Updated.

Returns:

  • A Python array with elements of type str. The caller converts them to the correct type.
nrelmat.ScanXml.maxAbsDiff(mata, matb)[source]

Returns the max abs diff between two 2D numpy matrices.

Parameters:

  • mata (numpy 2D array): Array to be compared.
  • matb (numpy 2D array): Array to be compared.

Returns:

  • float scalar: max_i( max_j( abs( mata[i][j] - matb[i][j]))
nrelmat.ScanXml.calcMatDeltas(mats)[source]

Returns the max abs diffs between adjacent pairs of a list of 2D numpy matrices.

Parameters:

  • mats (list of 2D numpy matrices)

Returns:

  • deltas (float[]): deltas[k] = maxAbsDiff( mats[k-1], mats[k])
nrelmat.ScanXml.printMrr(vmap)[source]

Prints the Mrr map returned by getArrayByPath or getArrayByNode.

Parameters:

  • vmap (map): the MRR map

Returns:

  • None
nrelmat.ScanXml.throwerr(msg)[source]

Prints an error message and raises Exception.

Parameters:

  • msg (str): Error message.

Returns

  • (Never returns)

Raises

  • Exception

Previous topic

ScanOutcar.py

Next topic

wrapReceive.py

This Page