def __init__(self, parent=None, hitnode=None, offset=0): if isinstance(hitnode, str): hitnode = hit.NewSection(hitnode) elif hitnode is None: hitnode = hit.NewSection('') super().__init__(parent, hitnode.path()) self.__hitnode = hitnode # hit.Node object self.__hitblockcomment = None # hit.Comment object for this block self.__hitparamcomments = dict() # hit.Comment objects for the parameters within this block self.__hitoffset = offset # hit index used for inserting new hit nodes self.__reinitComments()
def build_hit(expr, name, **kwargs): """ Create a hit node containing a ParsedFunction of the given expression Inputs: expr[sympy.core.Expr]: The sympy expression to convert name[str]: The name of the input file block to create kwargs: Key, value pairs for val, vals input parameters (defaults to 1.0) if not provided """ import hit symbols = set([str(s) for s in expr.free_symbols ]).difference(set(['R.x', 'R.y', 'R.z', 't'])) for symbol in symbols: kwargs.setdefault(symbol, 1.) root = hit.NewSection(name) root.addChild(hit.NewField('type', hit.FieldKind.String, 'ParsedFunction')) root.addChild( hit.NewField('value', hit.FieldKind.String, "'{}'".format(str(fparser(expr))))) if kwargs: pvars = ' '.join(kwargs.keys()) pvals = ' '.join([str(v) for v in kwargs.values()]) root.addChild( hit.NewField('vars', hit.FieldKind.String, "'{}'".format(pvars))) root.addChild( hit.NewField('vals', hit.FieldKind.String, "'{}'".format(pvals))) return root
def insert(self, index, name, **kwargs): """ Insert a new input file block before the supplied index Inputs: index[int]: The index to insert before name[str]: The name of the section to add kwargs[dict]: Parameters to populate to the new section """ count = 0 hit_index = 0 for child in self.__hitnode.children(): if count == index: break hit_index += 1 if child.type() == hit.NodeType.Section: count += 1 new = hit.NewSection(name) self.__hitnode.insertChild(hit_index, new) node = Node(None, new) super().insert(index, node) for key, value in kwargs.items(): node.__addParam(key, value) return node
def append(self, name, **kwargs): """ Append a child input block, with the given *name*, to the end of current block. The keyword arguments supplied are added as parameters to the added block. """ new = hit.NewSection(name) self.__hitnode.addChild(new) node = Node(self, new) for key, value in kwargs.items(): node.__addParam(key, value) return node
def append(self, name, **kwargs): """ Append a new input file block. Inputs: name[str]: The name of the section to add kwargs[dict]: Parameters to populate to the new section """ new = hit.NewSection(name) self.__hitnode.addChild(new) node = Node(self, new) for key, value in kwargs.items(): node.__addParam(key, value) return node
def addNode(parent_hit_node, entry): """ Adds a node and its children to the HIT tree. Input: parent_hit_node[hit.Node]: Parent to add children to entry[BlockInfo]: The block to add """ name = entry.name hit_node = hit.NewSection(name) parent_hit_node.addChild(hit_node) if entry.comments: commentNode(hit_node, entry.comments, False) nodeParamsString(hit_node, entry) children = entry.getChildNames() addInactive(hit_node, entry, children) for child in children: child_entry = entry.children.get(child, None) if child_entry and child_entry.wantsToSave(): addNode(hit_node, child_entry)
def insert(self, index, name, **kwargs): """ Insert a child input block, with the given *name*, to the current block +before+ the *index*. The keyword arguments supplied are added as parameters to the inserted block. """ count = 0 hit_index = 0 for child in self.__hitnode.children(): if count == index: break hit_index += 1 if child.type() == hit.NodeType.Section: count += 1 new = hit.NewSection(name) self.__hitnode.insertChild(hit_index, new) node = Node(None, new) super().insert(index, node) for key, value in kwargs.items(): node.__addParam(key, value) return node
def inputTreeToString(root): """ Main access point to write an InputTree to a string. Input: root[BlockInfo]: Root item of the tree. Return: str: The input file """ children = root.getChildNames() hit_node = hit.NewSection("") if root.comments: commentNode(hit_node, root.comments, False) addInactive(hit_node, root, children) last_child = children[-1] for child in children: entry = root.children.get(child, None) if entry and entry.wantsToSave(): addNode(hit_node, entry) if child != last_child: hit_node.addChild(hit.NewBlank()) return hit_node.render()