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 addInactive(hit_parent, parent, children): """ If there are any inactive blocks, add them """ inactive = [] for child in children: entry = parent.children.get(child, None) if entry and entry.checkInactive(): inactive.append(entry.name) if inactive: hit_parent.addChild(hit.NewField("inactive", "String", "'%s'" % ' '.join(inactive)))
def __addParam(self, name, value): """(private) Add a new parameter to the given node.""" if isinstance(value, int): kind = hit.FieldKind.Int elif isinstance(value, float): kind = hit.FieldKind.Float elif isinstance(value, bool): kind = hit.FieldKind.Bool elif isinstance(value, str): kind = hit.FieldKind.String else: kind = hit.FieldKind.NotField param = hit.NewField(name, kind, str(value)) self.__hitnode.addChild(param)
def addParam(self, name, value): """ Add a new parameter to the given node. Inputs: name[str]: The name of the parameter value[Int|Float|Bool|String]: The parameter value """ if isinstance(value, int): kind = hit.FieldKind.Int elif isinstance(value, float): kind = hit.FieldKind.Float elif isinstance(value, bool): kind = hit.FieldKind.Bool elif isinstance(value, str): kind = hit.FieldKind.String else: kind = hit.FieldKind.NotField param = hit.NewField(name, kind, str(value)) self.__hitnode.addChild(param)
def nodeParamsString(hit_parent, entry, ignore_type=False): params = entry.getParamNames() for name in params: if ignore_type and name == "type": continue info = entry.parameters[name] val = info.inputFileValue() if not val and name != 'active': # we generally don't want to write out empty strings continue comments = info.comments if info.hasChanged() or info.user_added or info.set_in_input_file: hit_param = hit.NewField(info.name, info.hitType(), val) hit_parent.addChild(hit_param) if comments: commentNode(hit_param, comments, True) type_info = entry.parameters.get("type") if entry.types and type_info: type_name = type_info.value type_entry = entry.types.get(type_name) if type_entry: nodeParamsString(hit_parent, type_entry, ignore_type=True)