def parse_macros(e): macros = parse_list(e, "macros") for m in macros: if(not Tinker.is_valid_verilog_name(m)): Tinker.value_error_xml("macros", m, "Valid Verilog Names", ET.tostring(e)) return macros
def check_roles(cls, d): cls.check_interfaces(d) ifs = d["interfaces"] pid = None others = [] for i in ifs: r = d[i]["role"] if (r == "primary" and pid != None): sys.exit( "Error! Two primary interfaces \"%s\" and \"%s\" found." % (pid, i)) elif (r == "primary"): pid = i else: others.append(i) for i in others: # TODO: Check master? m = d[i]["master"] r = d[i]["role"] if (m != pid): print "In key-value map:" print Tinker.tostr_dict(d) sys.exit( "Error! Interface \"%s\" has role \"%s\" but " % (i, r) + "specified master \"%s\" does not match primary \"%s\"" % (m, pid))
def parse_grouping(cls, e): ids = parse_list(e, "grouping") for i in ids: if(not Tinker.is_id(i)): Tinker.value_error_xml("grouping", i, "Alphabetic Characters", ET.tostring(e)) return ids
def parse_string(e, k): s = e.get(k) if (s is None): Tinker.key_error(k, ET.tostring(e)) elif (not Tinker.is_string(s)): Tinker.value_error_xml(k, s, "Strings", ET.tostring(e)) return s
def parse_oct_pins(cls, e): pins = parse_list(e, "oct_pins") for p in pins: if(not Tinker.is_valid_verilog_name(p)): Tinker.value_error_xml("oct_pins", p, "Valid Verilog Names", ET.tostring(e)) return pins
def parse_macros(e): macros = parse_list(e, "macros") for m in macros: if (not Tinker.is_valid_verilog_name(m)): Tinker.value_error_xml("macros", m, "Valid Verilog Names", ET.tostring(e)) return macros
def parse_oct_pins(cls, e): pins = parse_list(e, "oct_pins") for p in pins: if (not Tinker.is_valid_verilog_name(p)): Tinker.value_error_xml("oct_pins", p, "Valid Verilog Names", ET.tostring(e)) return pins
def __generate_macro_file(self, p): Tinker.check_path(p) ms = self.__d.get_macros(self.__t.get_version(), False) mfp = open(p + "/tinker.vh", "w") for m in ms: mfp.write("define `%s + \n" % m) mfp.close()
def parse_grouping(cls, e): ids = parse_list(e, "grouping") for i in ids: if (not Tinker.is_id(i)): Tinker.value_error_xml("grouping", i, "Alphabetic Characters", ET.tostring(e)) return ids
def parse_ratio(cls, desc): r = parse_string(desc, "ratio") if (r not in cls._C_INTERFACE_RATIOS): Tinker.value_error_map("ratio", str(r), str(cls._C_INTERFACE_RATIOS), Tinker.tostr_dict(desc)) return r
def parse_string(e, k): s = e.get(k) if(s is None): Tinker.key_error(k, ET.tostring(e)) elif(not Tinker.is_string(s)): Tinker.value_error_xml(k, s, "Strings", ET.tostring(e)) return s
def parse_interface_keys(cls, desc): k = set(desc.keys()) err = (k - cls._C_MEMORY_KEYS) if(err != set()): print "In description:" Tinker.tostr_dict(desc) sys.exit("Error! Unknown keys: %s" % str(list(err))) return k
def parse_interface_keys(cls, desc): k = set(desc.keys()) err = (k - cls._C_MEMORY_KEYS) if (err != set()): print "In description:" Tinker.tostr_dict(desc) sys.exit("Error! Unknown keys: %s" % str(list(err))) return k
def write(self, p): Tinker.check_path(p) p += "/" + "system.qsys" Tinker.check_path(p) r = self.getroot() ifs = self.__d.get_pin_elements(self.__b["version"], False) for i in ifs: r.append(i) super(System, self).write(p, encoding="UTF-8", xml_declaration=True)
def parse_keys(cls,desc): k = set(desc.keys()) ifs = set(cls.parse_interfaces(desc)) err = (k - ifs - cls._C_DESCRIPTION_KEYS) if(err != set()): print "In description:" Tinker.print_description(desc) sys.exit("Error! Unknown keys: %s" % str(list(err))) return k | ifs
def parse_keys(cls, desc): k = set(desc.keys()) ifs = set(cls.parse_interfaces(desc)) err = (k - ifs - cls._C_DESCRIPTION_KEYS) if (err != set()): print "In description:" Tinker.print_description(desc) sys.exit("Error! Unknown keys: %s" % str(list(err))) return k | ifs
def check_quantity(cls, d): q = parse_int(d, "quantity") if (not Tinker.is_in_range(q, cls._C_INTERFACE_QUANTITY_RANGE[0], cls._C_INTERFACE_QUANTITY_RANGE[1])): Tinker.value_error_map( "quantity", str(q), "range(%d, %d)" % (cls._C_INTERFACE_QUANTITY_RANGE[0], cls._C_INTERFACE_QUANTITY_RANGE[1]), Tinker.tostr_dict(d))
def check_width(cls, d): w = parse_int(d, "width") if (not Tinker.is_in_range(w, cls._C_INTERFACE_WIDTH_RANGE[0], cls._C_INTERFACE_WIDTH_RANGE[1])): Tinker.value_error_map( "width", str(w), "range(%d, %d)" % (cls._C_INTERFACE_WIDTH_RANGE[0], cls._C_INTERFACE_WIDTH_RANGE[1]), Tinker.tostr_dict(d))
def check_quantity(cls, d): super(GlobalMemoryInterface,cls).check_quantity(d) cls.check_interfaces(d) ifs = cls.parse_interfaces(d) q = parse_int(d, "quantity") if(q != len(ifs)): Tinker.value_error_map("quantity",str(q),str(ifs), Tinker.tostr_dict(d))
def check_quantity(cls, d): super(GlobalMemoryInterface, cls).check_quantity(d) cls.check_interfaces(d) ifs = cls.parse_interfaces(d) q = parse_int(d, "quantity") if (q != len(ifs)): Tinker.value_error_map("quantity", str(q), str(ifs), Tinker.tostr_dict(d))
def parse_burst(cls, d): b = parse_int(d, "burst") if (not Tinker.is_in_range(b, cls._C_INTERFACE_BURST_RANGE[0], cls._C_INTERFACE_BURST_RANGE[1])): Tinker.value_error_map( "burst", str(b), "range(%d, %d)" % (cls._C_INTERFACE_BURST_RANGE[0], cls._C_INTERFACE_BURST_RANGE[1]), Tinker.tostr_dict(d)) return b
def construct(t): import GlobalMemoryInterface, HostInterface, KernelInterface if(t == "GlobalMemory"): return GlobalMemoryInterface.GlobalMemoryInterface elif(t == "Host"): return HostInterface.HostInterface elif(t == "Kernel"): return KernelInterface.KernelInterface else: Tinker.value_error("interfaces", str(t), str(Interface._C_INTERFACE_TYPES))
def parse_burst(cls,d): b = parse_int(d, "burst") if(not Tinker.is_in_range(b,cls._C_INTERFACE_BURST_RANGE[0], cls._C_INTERFACE_BURST_RANGE[1])): Tinker.value_error_map("burst", str(b), "range(%d, %d)" % (cls._C_INTERFACE_BURST_RANGE[0], cls._C_INTERFACE_BURST_RANGE[1]), Tinker.tostr_dict(d)) return b
def check_width(cls,d): w = parse_int(d, "width") if(not Tinker.is_in_range(w, cls._C_INTERFACE_WIDTH_RANGE[0], cls._C_INTERFACE_WIDTH_RANGE[1])): Tinker.value_error_map("width", str(w), "range(%d, %d)" % (cls._C_INTERFACE_WIDTH_RANGE[0], cls._C_INTERFACE_WIDTH_RANGE[1]), Tinker.tostr_dict(d))
def check_quantity(cls,d): q = parse_int(d, "quantity") if(not Tinker.is_in_range(q, cls._C_INTERFACE_QUANTITY_RANGE[0], cls._C_INTERFACE_QUANTITY_RANGE[1])): Tinker.value_error_map("quantity", str(q), "range(%d, %d)" % (cls._C_INTERFACE_QUANTITY_RANGE[0], cls._C_INTERFACE_QUANTITY_RANGE[1]), Tinker.tostr_dict(d))
def check_roles(cls, d): cls.check_interfaces(d) ifs = d["interfaces"] pid = None spec = (d[ifs[0]].get("role") != None) for i in ifs: r = d[i].get("role") if (r is None and spec is True): Tinker.key_error("role", str(d[i])) elif (r != None and spec is False): print "In description:" Tinker.print_description(d) sys.exit( "Roles must be specified for all Memory Interfaces, or none of them." ) elif (r != None and r not in cls._C_INTERFACE_ROLES): Tinker.value_error_map("role", str(r), str(cls._C_INTERFACE_ROLES), Tinker.tostr_dict()) elif (r != None and r == "primary" and pid != None): print "In description:" Tinker.print_description(d) sys.exit( "Error! Two primary interfaces \"%s\" and \"%s\" found." % (pid, i)) elif (r == "primary"): pid = i
def check_burst(cls, d): burst = d.get("burst") if (burst is None): Tinker.key_error("burst", Tinker.tostr_dict(d)) if (not Tinker.is_pow_2(burst)): Tinker.value_error_map("burst", str(burst), "Integer powers of 2", Tinker.tostr_dict(d)) if (burst not in cls._C_BURST_WIDTHS): Tinker.value_error_map("burst", str(burst), str(list(cls._C_BURST_WIDTHS)), Tinker.tostr_dict(d))
def construct(t): import GlobalMemoryInterface, HostInterface, KernelInterface if (t == "GlobalMemory"): return GlobalMemoryInterface.GlobalMemoryInterface elif (t == "Host"): return HostInterface.HostInterface elif (t == "Kernel"): return KernelInterface.KernelInterface else: Tinker.value_error("interfaces", str(t), str(Interface._C_INTERFACE_TYPES))
def __check_interfaces(self, d): self.__check_types(d) ts = d["types"] for t in ts: dt = d[t] interfaces = dt.get("interfaces") if (interfaces is None): Tinker.key_error("interfaces", Tinker.tostr_dict(dt)) for i in interfaces: if (not Tinker.is_id(i)): value_error_map("interfaces", i, "Alphbetic Strings", Tinker.tostr_dict(dt))
def __parse(self, e): d = dict() d["type"] = "memory" d["types"] = parse_list(e, "types") for t in d["types"]: te = findsingle(e, "./%s" % t) if (t not in self._C_KNOWN_TYPES): Tinker.value_error_xml("types", t, str(list(self._C_KNOWN_TYPES)), ET.tostring(e)) d[t] = self.__parse_type(t, te) return d
def parse_keys(cls,desc): k = set(desc.keys()) if("interfaces" not in desc): ifs = set([]) else: ifs = set(cls.parse_interfaces(desc)) err = (k - ifs - cls._C_INTERFACE_KEYS) if(err != set()): print "In description:" Tinker.print_description(desc) sys.exit("Error! Unknown keys: %s" % str(list(err))) return k | ifs
def parse_keys(cls, desc): k = set(desc.keys()) if ("interfaces" not in desc): ifs = set([]) else: ifs = set(cls.parse_interfaces(desc)) err = (k - ifs - cls._C_INTERFACE_KEYS) if (err != set()): print "In description:" Tinker.print_description(desc) sys.exit("Error! Unknown keys: %s" % str(list(err))) return k | ifs
def check_burst(cls, d): burst = d.get("burst") if(burst is None): Tinker.key_error("burst", Tinker.tostr_dict(d)) if(not Tinker.is_pow_2(burst)): Tinker.value_error_map("burst", str(burst), "Integer powers of 2", Tinker.tostr_dict(d)) if(burst not in cls._C_BURST_WIDTHS): Tinker.value_error_map("burst", str(burst), str(list(cls._C_BURST_WIDTHS)), Tinker.tostr_dict(d))
def parse_interfaces(cls, desc): ifs = parse_list(desc, "interfaces") if(ifs == []): print "In description:" Tinker.print_description(d) sys.exit("Error! A Global Memory must have more than one interface!") for i in ifs: if(ifs.count(i) > 1): sys.exit("Error! Interface \"%s\" was not unique in list %s" % (i, str(ifs))) parse_dict(desc, i) return ifs
def check_oct_pin(cls, d): oct_pin = d.get("oct_pin") oct_pins = d.get("oct_pins") if (oct_pin is None): Tinker.key_error("oct_pin", Tinker.tostr_dict(d)) if (oct_pin is None): Tinker.key_error("oct_pins", Tinker.tostr_dict(d)) if (oct_pin not in self["oct_pins"]): Tinker.value_error_map("oct_pin", role, str(oct_pins), Tinker.tostr_dict(d))
def check_oct_pin(cls, d): oct_pin = d.get("oct_pin") oct_pins = d.get("oct_pins") if(oct_pin is None): Tinker.key_error("oct_pin", Tinker.tostr_dict(d)) if(oct_pin is None): Tinker.key_error("oct_pins", Tinker.tostr_dict(d)) if(oct_pin not in self["oct_pins"]): Tinker.value_error_map("oct_pin", role, str(oct_pins), Tinker.tostr_dict(d))
def parse_interfaces(cls, desc): ifs = parse_list(desc, "interfaces") if (ifs == []): print "In description:" Tinker.print_description(d) sys.exit( "Error! A Global Memory must have more than one interface!") for i in ifs: if (ifs.count(i) > 1): sys.exit("Error! Interface \"%s\" was not unique in list %s" % (i, str(ifs))) parse_dict(desc, i) return ifs
def check_base_address(cls, d): cls.check_size(d) sz = d["size"] base = d.get("base_address") if(base is None): Tinker.key_error("base_address", Tinker.tostr_dict(d)) if(not Tinker.is_in_range(base, 0, (2 ** 64) - sz)): Tinker.value_error_map("base_address", str(base), "Range(0x%x, 0x%x)" % (0, (2**64) - sz), Tinker.tostr_dict(d)) if((base % sz) != 0): Tinker.value_error_map("base_address", str(base), "Multiples of 0x%x (Size)" % sz, Tinker.tostr_dict(d))
def check_base_address(cls, d): cls.check_size(d) sz = d["size"] base = d.get("base_address") if (base is None): Tinker.key_error("base_address", Tinker.tostr_dict(d)) if (not Tinker.is_in_range(base, 0, (2**64) - sz)): Tinker.value_error_map("base_address", str(base), "Range(0x%x, 0x%x)" % (0, (2**64) - sz), Tinker.tostr_dict(d)) if ((base % sz) != 0): Tinker.value_error_map("base_address", str(base), "Multiples of 0x%x (Size)" % sz, Tinker.tostr_dict(d))
def parse(cls,e): d = super(Phy, cls).parse(e) i = parse_id(e) fref_mhz = parse_float(e, "fref_mhz") fphy_mhz = parse_float(e, "fphy_mhz") dq_pins = parse_int(e, "dq_pins") macros = parse_macros(e) d["id"] = i d["fphy_mhz"] = fphy_mhz d["fref_mhz"] = fref_mhz d["dq_pins"] = dq_pins d["macros"] = macros group = cls.parse_grouping(e) group.remove(i) octs = cls.parse_oct_pins(e) d["group"] = group d["oct_pins"] = octs pow2_dq_pins = int(2 ** Tinker.clog2(dq_pins)) d["pow2_dq_pins"] = pow2_dq_pins d["bandwidth_bs"] = int((d["fphy_mhz"] * 10**6 * cls._C_RATE * pow2_dq_pins) / 8) return d
def parse(cls, e): d = super(Phy, cls).parse(e) i = parse_id(e) fref_mhz = parse_float(e, "fref_mhz") fphy_mhz = parse_float(e, "fphy_mhz") dq_pins = parse_int(e, "dq_pins") macros = parse_macros(e) d["id"] = i d["fphy_mhz"] = fphy_mhz d["fref_mhz"] = fref_mhz d["dq_pins"] = dq_pins d["macros"] = macros group = cls.parse_grouping(e) group.remove(i) octs = cls.parse_oct_pins(e) d["group"] = group d["oct_pins"] = octs pow2_dq_pins = int(2**Tinker.clog2(dq_pins)) d["pow2_dq_pins"] = pow2_dq_pins d["bandwidth_bs"] = int( (d["fphy_mhz"] * 10**6 * cls._C_RATE * pow2_dq_pins) / 8) return d
def findunique(r, t): es = findall(r, t) if (Tinker.contains_duplicates([e.tag for e in es])): print "In XML Element:" print ET.tostring(r) sys.exit("Subelements with matching Tags found. Tags must be unique") return es
def findunique(r, t): es = findall(r, t) if(Tinker.contains_duplicates([e.tag for e in es])): print "In XML Element:" print ET.tostring(r) sys.exit("Subelements with matching Tags found. Tags must be unique") return es
def SetupNWChem(MMoutp, NWCheminp, numDigits, settings, adjRMSDcutoff): #Reads conformer geometry, energies and atom labels from Tinker output #(atoms, conformers) = ReadConformers(MMoutp, MaxEnergy) if settings.MMTinker: #Reads conformer geometry, energies and atom labels from Tinker output (atoms, conformers, charge) = Tinker.ReadTinker(MMoutp, settings) else: (atoms, conformers, charge) = MacroModel.ReadMacromodel(MMoutp, settings) if settings.charge is not None: charge = settings.charge #Prune similar conformations, if the number exceeds the limit if len(conformers) > settings.PerStructConfLimit: pruned = ConfPrune.RMSDPrune(conformers, atoms, adjRMSDcutoff) else: pruned = conformers print str(len(conformers) - len(pruned)) +\ " or " + "{:.1f}".format(100 * (len(conformers) - len(pruned)) / len(conformers)) + "% of conformations have been pruned based on " + \ str(adjRMSDcutoff) + " angstrom cutoff" for num in range(0, len(pruned)): filename = NWCheminp + str(num + 1).zfill(3) WriteNWChemFile(filename, pruned[num], atoms, charge, settings) print str(len(pruned)) + " .nw files written"
def parse_dict(d, k): dct = d.get(k) if(dct is None): Tinker.key_error(k, Tinker.tostr_dict(d)) elif(not Tinker.is_dict(dct)): Tinker.value_error_map(k, dct, "Dictionary", Tinker.tostr_dict(d)) return dct
def parse_list(d, k): l = d.get(k) if (l is None): Tinker.key_error(k, Tinker.tostr_dict(d)) elif (not Tinker.is_list(l)): Tinker.value_error_map(k, l, "Lists", Tinker.tostr_dict(d)) return l
def parse_list(d, k): l = d.get(k) if(l is None): Tinker.key_error(k, Tinker.tostr_dict(d)) elif(not Tinker.is_list(l)): Tinker.value_error_map(k, l, "Lists", Tinker.tostr_dict(d)) return l
def parse_string(d, k): s = d.get(k) if(s is None): Tinker.key_error(k, Tinker.tostr_dict(d)) elif(not Tinker.is_string(s)): Tinker.value_error_map(k, s, "Strings", Tinker.tostr_dict(d)) return s
def parse_dict(d, k): dct = d.get(k) if (dct is None): Tinker.key_error(k, Tinker.tostr_dict(d)) elif (not Tinker.is_dict(dct)): Tinker.value_error_map(k, dct, "Dictionary", Tinker.tostr_dict(d)) return dct
def parse_string(d, k): s = d.get(k) if (s is None): Tinker.key_error(k, Tinker.tostr_dict(d)) elif (not Tinker.is_string(s)): Tinker.value_error_map(k, s, "Strings", Tinker.tostr_dict(d)) return s
def validate(cls, d): """ Validate the parameters that describe the intrinsic settings of this Interface Arguments: d -- A Description object, containing the parsed user description of a custom board """ cls.check_role(d) if(d["role"] == "independent" and "master" not in d): Tinker.key_error("master", str(d)) else: return cls.check_master(d)
def check_dq_pins(cls, d): p = d.get("dq_pins") dq_min = cls._C_DQ_PIN_RANGE[0] dq_max = cls._C_DQ_PIN_RANGE[1] if(p is None): Tinker.key_error("dq_pins", Tinker.tostr_dict(d)) if(not Tinker.is_in_range(p, dq_min, dq_max)): Tinker.value_error_map("dq_pins", str(p), "Range(0x%x, 0x%x)" % (dq_min, dq_max), Tinker.tostr_dict(d))
def check_frequency(cls, d): freq = d.get("freq_mhz") freq_min = cls._C_INTERFACE_MHZ_RANGE[0] freq_max = cls._C_INTERFACE_MHZ_RANGE[1] if(freq is None): Tinker.key_error("freq_mhz", Tinker.tostr_dict(d)) if(not Tinker.is_in_range(freq, freq_min, freq_max)): Tinker.value_error_map("freq_mhz", str(freq), "Range(0x%x, 0x%x)" % (freq_min, freq_max), Tinker.tostr_dict(d))
def check_bandwidth_bs(cls, d): bw = d.get("bandwidth_bs") bw_min = cls._C_BANDWIDTH_BS_RANGE[0] bw_max = cls._C_BANDWIDTH_BS_RANGE[1] if(bw is None): Tinker.key_error("bandwidth_bs", Tinker.tostr_dict(d)) if(not Tinker.is_in_range(bw,bw_min,bw_max)): Tinker.value_error_map("bandwidth_bs", str(hex(bw)), "Range(0x%x, 0x%x)" % (bw_min, bw_max), Tinker.tostr_dict(d))
def check_latency(cls, d): l = d.get("latency") l_min = cls._C_LATENCY_RANGE[0] l_max = cls._C_LATENCY_RANGE[1] if(l is None): Tinker.key_error("latency", Tinker.tostr_dict(d)) if(not Tinker.is_in_range(l, l_min, l_max)): Tinker.value_error_map("latency", str(hex(l)), "Range(0x%x, 0x%x)" % (l_min, l_max), Tinker.tostr_dict(d))
def check_name(cls, d): n = cls.parse_string(d, "name") if(not Tinker.is_valid_verilog_name(n)): Tinker.value_error_map("name", n, "Valid Verilog Names", Tinker.tostr_dict(d)) if(len(n) > cls._C_): Tinker.value_error_map("name", n, "Strings less than 32 characters", Tinker.tostr_dict(d)) return n
def check_size(cls, d): sz = d.get("size") sz_min = cls._C_INTERFACE_SIZE_RANGE[0] sz_max = cls._C_INTERFACE_SIZE_RANGE[1] if(sz is None): Tinker.key_error("size", Tinker.tostr_dict(d)) if(not Tinker.is_in_range(sz, sz_min, sz_max)): Tinker.value_error_map("size", str(hex(sz)), "Range(0x%x, 0x%x)" % (sz_min, sz_max), Tinker.tostr_dict(d))