def fromfile(cls, fileobj): parser = HSDParser() builder = VariableTreeBuilder() treebuilder = HSDTreeBuilder(parser=parser, builder=builder) openclose = isinstance(fileobj, str) if openclose: fp = open(fileobj, "r") else: fp = fileobj tree = treebuilder.build(fp) if openclose: fp.close() query = HSDQuery(checkuniqueness=True, markprocessed=True) return cls.fromhsd(tree, query)
def _test_module(): from sktools.hsd.treebuilder import HSDTreeBuilder from sktools.hsd.query import HSDQuery from sktools.hsd.parser import HSDParser parser = HSDParser(lowertagnames=True) treebuilder = HSDTreeBuilder(parser=parser) fp = open("skdefs.hsd", "r") tree = treebuilder.build(fp) fp.close() query = HSDQuery(checkuniqueness=True, markprocessed=True) skdefs = Skdef.fromhsd(tree, query) print(skdefs.onecenterparameters["n"].calculator.exponents) print(skdefs.onecenterparameters["n"].deltafilling) unprocessed_list = query.findunprocessednodes(tree) for unprocessed in unprocessed_list: print("Unprocessed element '{}' at line {:d}!".format( unprocessed.hsdattrib[hsd.HSDATTR_TAG], unprocessed.hsdattrib[hsd.HSDATTR_LINE] + 1))
def fromfile(cls, fileobj): """Initializes an SkgenPaths instance from a file. Args: fileobj: File name or file like object containing the text representation of a path tree. Returns: Initialized instance. """ parser = HSDParser() builder = VariableTreeBuilder() treebuilder = HSDTreeBuilder(parser=parser, builder=builder) openclose = isinstance(fileobj, str) if openclose: fp = open(fileobj, "r") else: fp = fileobj tree = treebuilder.build(fp) if openclose: fp.close() query = HSDQuery(checkuniqueness=True) myself = cls(tree, query) return myself
def _test_module(): """Testing module capabilities.""" from io import StringIO from sktools.hsd.treebuilder import HSDTreeBuilder from sktools.hsd.parser import HSDParser from sktools.hsd.tree import HSDTree import sktools.hsd.converter as conv unit_attr = "unit" unit_only = frozenset([unit_attr]) parser = HSDParser(defattrib=unit_attr) builder = HSDTreeBuilder(parser=parser) # Defining force type (scalar, list) force_units = {"ev/aa": 0.0194469050555} # Trivial unit conversion routine. def multiply_unit(child, value, unitattr, units): unit = child.get(unitattr) convfact = units.get(unit.lower(), None) if convfact is None: hsd.HSDInvalidAttributeValueException( node=child, msg="Invalid unit '{}'".format(unit)) return value * convfact stream = StringIO(""" # Various driver possibilities # # No driver specification #Driver {} # Use the default driver (whatever it is) #Driver = None {} # Use the driver None {} #Driver = None Driver = ConjugateGradient { MaxForceComponent [eV/AA] = 1e-2 } Hamiltonian = DFTB { # SCC = True # SCCTolerance = 1e-4 # MaxSCCIterations = 100 MaxAngularMomentum { O = "p" H = "s" } Mixer = Broyden #Mixer = Broyden { # MixingParameter = 0.3 #} #ReadInitialCharges = No KPointsAndWeights { 0.0 0.0 0.0 0.25 0.25 0.25 0.25 0.75 } } Options { WriteAutotestTag = Yes UnknownOption = No } #ParserOptions { # ParserVersion = 4 #} """) root = builder.build(stream) qy = HSDQuery(markprocessed=True) # A complex case: If driver was not specified, it defaults to None {} # If it was specified but nothing was assinged to it (no child) # it defaults to ConjugateGradient {}. dtype, driver = qy.getvaluenode(root, "Driver", "None", allowtextvalue=True, returnchild=True) # Since the in the previous getvaluenode() call a default had been specified # dtype can only be None, if "Driver" was in the input, but had no # child (e.g. 'Driver {}' or 'Driver = ;'). In this case we set # it to ConjugateGradient if dtype is None: dtype = qy.getchild(driver, "ConjugateGradient", optional=True) if dtype.tag == "None": pass elif dtype.tag == "ConjugateGradient": forcetol, child = qy.getvalue(dtype, "MaxForceComponent", conv.float0, 1e-4, returnchild=True, attribs=unit_only) multiply_unit(child, forcetol, unit_attr, force_units) elif dtype.tag == "SteepestDescent": forcetol, child = qy.getvalue(dtype, "MaxForceComponent", conv.float0, 1e-4, returnchild=True, attribs=unit_only) multiply_unit(child, forcetol, unit_attr, force_units) stepsize = qy.getvalue(dtype, "StepSize", conv.float0, 40.0) pass else: raise hsd.HSDInvalidTagException(node=dtype, msg="Unknown driver type '{}'".format( dtype.tag)) ham = qy.getchild(root, "Hamiltonian") dftb = qy.getchild(ham, "DFTB") scc = qy.getvalue(dftb, "SCC", conv.bool0, True) scctol = qy.getvalue(dftb, "SCCTolerance", conv.float0, 1e-4) scciter = qy.getvalue(dftb, "MaxSCCIterations", conv.int0, 100) mangmom = qy.getchild(dftb, "MaxAngularMomentum") maxangs = [ qy.getvalue(mangmom, species, conv.str0) for species in ["O", "H"] ] mixer = qy.getvaluenode(dftb, "Mixer", "Broyden", allowtextvalue=True) if mixer.tag == "Broyden": mixparam = qy.getvalue(mixer, "MixingParameter", conv.float0, 0.2) else: raise hsd.HSDInvalidTagException(node=mixer, msg="Unknown mixer type '{}'.".format( mixer.tag)) readcharges = qy.getvalue(dftb, "ReadInitalCharges", conv.bool0, False) kpoints = qy.getvalue(dftb, "KPointsAndWeights", conv.float1) if len(kpoints) % 4: raise hsd.HSDInvalidTagValueException(node=kpoints, msg="Incorrect number of floats") options = qy.getchild(root, "Options", optional=True) autotest = qy.getvalue(options, "WriteAutotestTag", conv.bool0, False) parseroptions = qy.getchild(root, "ParserOptions", optional=True) parserversion = qy.getvalue(parseroptions, "ParserVersion", conv.int0, 4) tree = HSDTree(root) tree.writehsd() print("\nUnprocessed: ", qy.findunprocessednodes(root))