Fleur tools/utility

Dealing with XML Schema files

This file is just were to hardcode some schema file paths

Structure Data util

XML utility

In this module contains useful methods for handling xml trees and files which are used by the Fleur code and the fleur plugin.

aiida_fleur.tools.xml_util.add_num_to_att(xmltree, xpathn, attributename, set_val, mode='abs', occ=[0], create=False)[source]

Routine adds something to the value of an attribute in the xml file (should be a number here)

Param:an etree a xpath from root to the attribute and the attribute value
Param:mode: ‘abs’, ‘rel’, change by absolut or relative amount
Returns:None, or an etree
Comment: Element.set will add the attribute if it does not exist,
xpath expression has to exist
example: add_num_to_add(tree, ‘/fleurInput/bzIntegration’, ‘valenceElectrons’, ‘1’)
add_num_to_add(tree, ‘/fleurInput/bzIntegration’, ‘valenceElectrons’, ‘1.1’, mode=’rel’)
aiida_fleur.tools.xml_util.change_atomgr_att_label(fleurinp_tree_copy, attributedict, at_label, create=False)[source]

This method calls change_atomgr_att method for a certain atom specie that corresponds to an atom with a given label

aiida_fleur.tools.xml_util.convert_ev_to_htr(value, parser_info_out=None)[source]

Divides the value given with the Hartree factor (converts htr to eV)

aiida_fleur.tools.xml_util.convert_fleur_lo(loelements)[source]

Converts lo xml elements from the inp.xml file into a lo string for the inpgen

aiida_fleur.tools.xml_util.convert_from_fortran_bool(stringbool)[source]

Converts a string in this case (‘T’, ‘F’, or ‘t’, ‘f’) to True or False

Parameters:stringbool – a string (‘t’, ‘f’, ‘F’, ‘T’)
Returns:boolean (either True or False)
aiida_fleur.tools.xml_util.convert_htr_to_ev(value, parser_info_out=None)[source]

Multiplies the value given with the Hartree factor (converts htr to eV)

aiida_fleur.tools.xml_util.convert_to_float(value_string, parser_info_out=None, suc_return=True)[source]

Tries to make a float out of a string. If it can’t it logs a warning and returns True or False if convertion worked or not.

Parameters:value_string – a string
Returns value:the new float or value_string: the string given
Returns:True or False
aiida_fleur.tools.xml_util.convert_to_fortran_bool(boolean)[source]

Converts a Boolean as string to the format definded in the input

Parameters:boolean – either a boolean or a string (‘True’, ‘False’, ‘F’, ‘T’)
Returns:a string (either ‘t’ or ‘f’)
aiida_fleur.tools.xml_util.convert_to_fortran_string(string)[source]

converts some parameter strings to the format for the inpgen :param string: some string :returns: string in right format (extra “”)

aiida_fleur.tools.xml_util.convert_to_int(value_string, parser_info_out=None, suc_return=True)[source]

Tries to make a int out of a string. If it can’t it logs a warning and returns True or False if convertion worked or not.

Parameters:value_string – a string
Returns value:the new int or value_string: the string given
Returns:True or False, if suc_return=True
aiida_fleur.tools.xml_util.create_tag(xmlnode, xpath, newelement, create=False, place_index=None, tag_order=None)[source]

This method evaluates an xpath expresion and creates tag in an xmltree under the returned nodes. If the path does exist things will be overriden, or created. Per default the new element is appended to the elements, but it can also be inserted in a certain position or after certain other tags.

aiida_fleur.tools.xml_util.delete_att(xmltree, xpath, attrib)[source]

deletes an xml tag in an xmletree in place

param: xmltree: xmltree (etree) param: xpath: xpathexpression

aiida_fleur.tools.xml_util.delete_tag(xmltree, xpath)[source]

deletes an xml tag in an xmletree in place

param: xmltree: xmltree (etree) param: xpath: xpathexpression

aiida_fleur.tools.xml_util.eval_xpath(node, xpath, parser_info=None)[source]

Tries to evalutate an xpath expression. If it fails it logs it. If seferal paths are found, return a list. If only one - returns the value.

:param root node of an etree and an xpath expression (relative, or absolute) :returns either nodes, or attributes, or text

aiida_fleur.tools.xml_util.eval_xpath2(node, xpath, parser_info=None)[source]

Tries to evalutate an xpath expression. If it fails it logs it. Always return a list.

:param root node of an etree and an xpath expression (relative, or absolute) :returns a node list

aiida_fleur.tools.xml_util.eval_xpath3(node, xpath, create=False, place_index=None, tag_order=None)[source]

Tries to evalutate an xpath expression. If it fails it logs it. If create == True, creates a tag

:param root node of an etree and an xpath expression (relative, or absolute) :returns always a node list

aiida_fleur.tools.xml_util.get_inpgen_para_from_xml(inpxmlfile)[source]

This routine returns an python dictionary produced from the inp.xml file, which can be used as a parameterdata node by inpgen.

Returns:dict

Warning: This routine is prelimitary, it is far from complete. Also be aware that inpgen does not take all information that is contained in an inp.xml file

aiida_fleur.tools.xml_util.get_inpgen_paranode_from_xml(inpxmlfile)[source]

This routine returns an AiiDA Parameter Data type produced from the inp.xml file, which can be used by inpgen.

Returns:ParameterData node
aiida_fleur.tools.xml_util.get_inpxml_file_structure()[source]

This routine returns the structure/layout of the ‘inp.xml’ file.

Basicly the plug-in should know from this routine, what things are allowed to be set and where, i.e all attributes and their xpaths. As a developer make sure to use this routine always of you need information about the inp.xml file structure. Therefore, this plug-in should be easy to adjust to other codes with xml files as input files. Just rewrite this routine.

For now the structure of the xmlinp file for fleur is hardcoded. If big changes are in the ‘inp.xml’ file, maintain this routine. TODO: Maybe this is better done, by reading the xml schema datei instead. And maybe it should also work without the schema file, do we want this?

Parameters:Nothing – TODO xml schema
Return all_switches_once:
 list of all switches (‘T’ or ‘F’) which are allowed to be set
Return all_switches_several:
 list of all switches (‘T’ or ‘F’) which are allowed to be set
Return other_attributes_once:
 list of all attributes, which occur just once (can be tested)
Return other_attributes_several:
 list of all attributes, which can occur more then once
Return all_text:
 list of all text of tags, which can be set
Return all_attrib_xpath:
 dictonary (attrib, xpath), of all possible attributes with their xpath expression for the xmp inp
Return expertkey:
 keyname (should not be in any other list), which can be used to set anything in the file, by hand, (for experts, and that plug-in does not need to be directly maintained if xmlinp gets a new switch)
aiida_fleur.tools.xml_util.get_xml_attribute(node, attributename, parser_info_out={'parser_warnings': []})[source]

Get an attribute value from a node.

Params node:a node from etree
Params attributename:
 a string with the attribute name.
Returns:either attributevalue, or None
aiida_fleur.tools.xml_util.inpxml_todict(parent, xmlstr)[source]

Recursive operation which transforms an xml etree to python nested dictionaries and lists. Decision to add a list is if the tag name is in the given list tag_several

Parameters:
  • parent – some xmltree, or xml element
  • xmlstr – structure/layout of the xml file in xmlstr is tags_several: a list of the tags, which should be converted to a list, not a dictionary(because they are known to occur more often, and want to be accessed in a list later.
Returns:

a python dictionary

aiida_fleur.tools.xml_util.is_sequence(arg)[source]

Checks if arg is a sequence

aiida_fleur.tools.xml_util.replace_tag(xmltree, xpath, newelement)[source]

replaces a xml tag by another tag on an xmletree in place

param: xmltree: xmltree (etree) param: xpath: xpathexpression param: newelement: xmlElement

aiida_fleur.tools.xml_util.set_dict_or_not(para_dict, key, value)[source]

setter method for a dictionary that will not set the key, value pair. if the key is [] or None.

aiida_fleur.tools.xml_util.set_species(fleurinp_tree_copy, species_name, attributedict, create=False)[source]

Method to set parameters of a species tag of the fleur inp.xml file.

param: fleurinp_tree_copy, xml etree of the inp.xml param: species_name : string, name of the species you want to change param: attributedict: python dict: what you want to change param: create: bool, if species does not exist create it and all subtags?

raises: ValueError, if species name is non existent in inp.xml and should not be created. also if other given tags are garbage. (errors from eval_xpath() methods)

return: fleurinp_tree_copy: xml etree of the new inp.xml

aiida_fleur.tools.xml_util.set_species_label(fleurinp_tree_copy, at_label, attributedict, create=False)[source]

This method calls set_species method for a certain atom specie that corresponds to an atom with a given label

aiida_fleur.tools.xml_util.write_new_fleur_xmlinp_file(inp_file_xmltree, fleur_change_dic, xmlinpstructure)[source]

This modifies the xml-inp file. Makes all the changes wanted by the user or sets some default values for certain modes

Params inp_file_xmltree:
 xml-tree of the xml-inp file
Params fleur_change_dic:
 dictionary {attrib_name : value} with all the wanted changes.
Returns:an etree of the xml-inp file with changes.
aiida_fleur.tools.xml_util.xml_set_all_attribv(xmltree, xpathn, attributename, attribv, create=False)[source]

Routine sets the value of an attribute in the xml file on all places it occurs

Param:an etree a xpath from root to the attribute and the attribute value
Returns:None, or an etree
Comment: Element.set will add the attribute if it does not exist,
xpath expression has to exist
example: xml_set_first_attribv(tree, ‘/fleurInput/atomGroups/atomGroup/force’, ‘relaxXYZ’, ‘TTF’)
xml_set_first_attribv(tree, ‘/fleurInput/atomGroups/atomGroup/force’, ‘calculate’, ‘F’)
aiida_fleur.tools.xml_util.xml_set_all_text(xmltree, xpathn, text, create=False)[source]

Routine sets the text of a tag in the xml file Input: an etree a xpath from root to the tag and the text Output: none, an etree example: xml_set_text(tree, ‘/fleurInput/comment’, ‘Test Fleur calculation for AiiDA plug-in’) but also cordinates and Bravais Matrix!: xml_set_text(tree, ‘/fleurInput/atomGroups/atomGroup/relPos’, ‘1.20000 PI/3 5.1-MYCrazyCostant’)

aiida_fleur.tools.xml_util.xml_set_attribv_occ(xmltree, xpathn, attributename, attribv, occ=None, create=False)[source]

Routine sets the value of an attribute in the xml file on only the places specified in occ

Param:an etree a xpath from root to the attribute and the attribute value
Param:occ, list of integers
Returns:None, or an etree
Comment: Element.set will add the attribute if it does not exist,
xpath expression has to exist
example: xml_set_first_attribv(tree, ‘/fleurInput/calculationSetup’, ‘band’, ‘T’)
xml_set_first_attribv(tree, ‘/fleurInput/calculationSetup’, ‘dos’, ‘F’)
aiida_fleur.tools.xml_util.xml_set_first_attribv(xmltree, xpathn, attributename, attribv, create=False)[source]

Routine sets the value of an attribute in the xml file

Param:an etree a xpath from root to the attribute and the attribute value
Returns:None, or an etree
Comment: Element.set will add the attribute if it does not exist,
xpath expression has to exist
example: xml_set_first_attribv(tree, ‘/fleurInput/calculationSetup’, ‘band’, ‘T’)
xml_set_first_attribv(tree, ‘/fleurInput/calculationSetup’, ‘dos’, ‘F’)
aiida_fleur.tools.xml_util.xml_set_text(xmltree, xpathn, text, create=False, place_index=None, tag_order=None)[source]

Routine sets the text of a tag in the xml file Input: an etree a xpath from root to the tag and the text Output: none, an etree example: xml_set_text(tree, ‘/fleurInput/comment’, ‘Test Fleur calculation for AiiDA plug-in’) but also cordinates and Bravais Matrix!: xml_set_text(tree, ‘/fleurInput/atomGroups/atomGroup/relPos’, ‘1.20000 PI/3 5.1-MYCrazyCostant’)

aiida_fleur.tools.xml_util.xml_set_text_occ(xmltree, xpathn, text, create=False, occ=0, place_index=None, tag_order=None)[source]

Routine sets the text of a tag in the xml file Input: an etree a xpath from root to the tag and the text Output: none, an etree example: xml_set_text(tree, ‘/fleurInput/comment’, ‘Test Fleur calculation for AiiDA plug-in’) but also cordinates and Bravais Matrix!: xml_set_text(tree, ‘/fleurInput/atomGroups/atomGroup/relPos’, ‘1.20000 PI/3 5.1-MYCrazyCostant’)

Parameter utility

General Parameter

This contains code snippets and utility useful for dealing with parameter data nodes commonly used by the fleur plugin and workflows

aiida_fleur.tools.dict_util.dict_merger(dict1, dict2)[source]

Merge rekursively two nested python dictionaries.

If key is in both digionaries tries to add the entries in both dicts. (merges two subdicts, adds strings and numbers together)

Returns:dict
aiida_fleur.tools.dict_util.extract_elementpara(parameter_dict, element)[source]
Parameters:
  • parameter_dict – python dict, parameter node for inpgen
  • element – string, i.e ‘W’
Returns:

python dictionary, parameter node which contains only the atom parameters for the given element

Merge Parameter

Corehole/level utility

You find the usual binding_energy for all elements in the periodic table.

aiida_fleur.tools.element_econfig_list.convert_fleur_config_to_econfig(fleurconf_str, keep_spin=False)[source]

‘[Kr] (4d3/2) (4d5/2) (4f5/2) (4f7/2)’ -> ‘[Kr] 4d10 4f14’, or ‘[Kr] 4d3/2 4d5/2 4f5/2 4f7/2’

# for now only use for coreconfig, it will fill all orbitals, since it has no information on the filling.

aiida_fleur.tools.element_econfig_list.econfigstr_hole(econfigstr, corelevel, highesunoccp, htype='valence')[source]

# ‘1s2 | 2s2’ , ‘1s2’ , ‘2p0’ -> ‘1s1 | 2s2 2p1’

Param:string
Param:string
Param:string
Returns:string
aiida_fleur.tools.element_econfig_list.get_coreconfig(element, full=False)[source]

returns the econfiguration as a string of an element.

Param:element string
Param:full, bool (econfig without [He]…)
Returns:string
Note:Be careful with base strings…
aiida_fleur.tools.element_econfig_list.get_econfig(element, full=False)[source]

returns the econfiguration as a string of an element.

Params element:element string
Params full:a bool (econfig without [He]…)
Returns:a econfig string
aiida_fleur.tools.element_econfig_list.get_spin_econfig(fulleconfigstr)[source]

converts and econfig string to a full spin econfig 1s2 2s2 2p6’ - > ‘1s1/2 2s1/2 2p1/2 2p3/2’

aiida_fleur.tools.element_econfig_list.get_state_occ(econfigstr, corehole='', valence='', ch_occ=1.0)[source]

finds out all not full occupied states and returns a dictionary of them return a dict i.e corehole ‘4f 5/2’ ch_occ full or fractional corehole occupation? valence: orbital sting ‘5d’, is to adjust the charges for fractional coreholes To that orbital occupation ch_occ - 1 will be added.

aiida_fleur.tools.element_econfig_list.highest_unocc_valence(econfigstr)[source]

returns the highest not full valence orbital. If all are full, it returns ‘’ #maybe should be advanced to give back the next highest unocc

aiida_fleur.tools.element_econfig_list.rek_econ(econfigstr)[source]

rekursive routine to return a full econfig ‘[Xe] 4f14 | 5d10 6s2 6p4’ -> ‘1s 2s … 4f14 | 5d10 6s2 6p4’

Common aiida utility

Reading in Cif files

IO routines

Here we collect io routines and their utility. For writting certain things to files. For example collection of data or database evaluations, for other people.

Common utitlity for fleur workchains

In here we put all things (methods) that are common to workflows AND DO NOT depend on AiiDA classes, therefore can be used without loading the dbenv. Util that does depend on AiiDA classes should go somewhere else.

aiida_fleur.tools.common_fleur_wf_util.balance_equation(equation_string, allow_negativ=False, allow_zero=False, eval_linear=True)[source]

Method that balances a chemical equation.

param equation_string: string (with ‘->’) param allow_negativ: bool, default False, allows for negative coefficents for the products.

return string: balanced equation

balance_equation(“C7H16+O2 -> CO2+H2O”)) balance_equation(“Be12W->Be22W+Be12W”) balance_equation(“Be12W->Be12W”)

1*C7H16+11*O2 ->7* CO2+8*H2O None 1*Be12W->1*Be12W #TODO The solver better then what we need. Currently if system is over #”Be12W->Be2W+W+Be” solves to {a: 24, b: -d/2 + 144, c: d/2 - 120}-> FAIL-> None # The code fails in the later stage, but this solution should maybe be used.

code adapted from stack exchange (the messy part): https://codegolf.stackexchange.com/questions/8728/balance-chemical-equations

aiida_fleur.tools.common_fleur_wf_util.calc_stoi(unitcellratios, formulas, error_ratio=[])[source]

Calculate the Stoichiometry with errors from a given unit cell ratio, formulas.

Example: calc_stoi([10, 1, 7], [‘Be12Ti’, ‘Be17Ti2’, ‘Be2’], [0.1, 0.01, 0.1]) ({‘Be’: 12.583333333333334, ‘Ti’: 1.0}, {‘Be’: 0.12621369924887876, ‘Ti’: 0.0012256517540566825}) calc_stoi([10, 1, 7], [‘Be12Ti’, ‘Be17Ti2’, ‘Be2’]) ({‘Be’: 12.583333333333334, ‘Ti’: 1.0}, {})

aiida_fleur.tools.common_fleur_wf_util.check_eos_energies(energylist)[source]

Checks if there is an abnormality in the total energies from the Equation of states. i.e. if one point has a larger energy then its two neighbors

Parameters:energylist – list of floats
Returs nnormalies:
 integer
aiida_fleur.tools.common_fleur_wf_util.convert_eq_to_dict(equationstring)[source]

Converts an equation string to a dictionary convert_eq_to_dict(‘1*Be12Ti->10*Be+1*Be2Ti+5*Be’) -> {‘products’: {‘Be’: 15, ‘Be2Ti’: 1}, ‘educts’: {‘Be12Ti’: 1}}

aiida_fleur.tools.common_fleur_wf_util.convert_formula_to_formula_unit(formula)[source]

Converts a formula to the smalles chemical formula unit ‘Be4W2’ -> ‘Be2W’

aiida_fleur.tools.common_fleur_wf_util.determine_convex_hull(formation_en_grid)[source]

Wraps the scipy.spatial ConvexHull algo for our purposes. For now only for 2D phase diagrams Adds the points [1.0, 0.0] and [0.0, 1.0], because in material science these are always there.

Params:formation_en_grid: list of points in phase space [[x, formation_energy]]
Returns:a hul datatype
aiida_fleur.tools.common_fleur_wf_util.determine_formation_energy(struc_te_dict, ref_struc_te_dict)[source]

This method determines the formation energy. E_form = E(A_xB_y) - x*E(A) - y*E(B)

Params struc_te_dict:
 python dictionary in the form of {‘formula’ : total_energy} for the compound(s)
Params ref_struc_te_dict:
 python dictionary in the form of {‘formula’ : total_energy per atom, or per unit cell} for the elements (if the formula of the elements contains a number the total energy is devided by that number)
Returns:list of floats, dict {formula : eform, ..} units energy/per atom, energies have some unit as energies given
aiida_fleur.tools.common_fleur_wf_util.determine_reactions(formula, available_data)[source]

Determines and balances theoretical possible reaction.

Stoichiometry ‘Be12W’, [Be12W, Be2W, Be, W, Be22W] -> [[Be22W+Be2W], [Be12W], [Be12+W],…] params formula: string, given educts (left side of equation), params available_data: list of strings of compounds (products), from which all possibilities will be constructed

aiida_fleur.tools.common_fleur_wf_util.get_atomprocent(formula)[source]

This converts a formula to a dictionary with elemnt : atomprocent example converts ‘Be24W2’ to {‘Be’: 24/26, ‘W’ : 2/26}, also BeW to {‘Be’ : 0.5, ‘W’ : 0.5} :params: formula: string :returns: a dict, element : atomprocent

# Todo alternative with structuredata

aiida_fleur.tools.common_fleur_wf_util.get_enhalpy_of_equation(reaction, formenergydict)[source]

calculate the enhalpy per atom of a given reaction from the given data.

param reaction: string param fromenergydict: dictionary that contains the {compound: formationenergy per atom}

# TODO check if physics is right

aiida_fleur.tools.common_fleur_wf_util.get_natoms_element(formula)[source]

Converts ‘Be24W2’ to {‘Be’: 24, ‘W’ : 2}, also BeW to {‘Be’ : 1, ‘W’ : 1}

aiida_fleur.tools.common_fleur_wf_util.get_weight_procent(formula)[source]

This converts a formula to a dictionary with elemnt : weightprocent example converts ‘Be24W2’ to {‘Be’: , ‘W’ : }, also BeW to {‘Be’ : , ‘W’ : } :params: formula: string :returns: a dict, element : weightprocent

# Todo alternative with structuredata

aiida_fleur.tools.common_fleur_wf_util.inpgen_dict_set_mesh(inpgendict, mesh)[source]

params: python dict, used for inpgen parameterdata node params: mesh either as returned by kpointsdata or tuple of 3 integers

returns: python dict, used for inpgen parameterdata node

aiida_fleur.tools.common_fleur_wf_util.powerset(L)[source]

Constructs the power set, ‘potenz Menge’ of a given list.

return list: of all possible subsets

aiida_fleur.tools.common_fleur_wf_util.ucell_to_atompr(ratio, formulas, element, error_ratio=[])[source]

Converts unit cell ratios into atom ratios.

len(ratio) == len(formulas) (== len(error_ratio)) ucell_to_atompr([10, 1, 7], [‘Be12Ti’, ‘Be17Ti2’, ‘Be2’], element=’Be’, [0.1, 0.1, 0.1])