def load_files(self): """Load nodes and elements from all network files and return them in a |Selections| instance. Each single network file defines a separate |Selection| instance. Additionally, all |Element| and |Node| objects are bundled in a selection named `complete`. """ selections = selectiontools.Selections() for (filename, path) in zip(self.filenames, self.filepaths): # Ensure both `Node` and `Element`start with a `fresh` memory. devicetools.Node.gather_new_nodes() devicetools.Element.gather_new_elements() try: info = runpy.run_path(path) except BaseException: objecttools.augment_excmessage( 'While trying to load the network file `%s`' % path) try: selections += selectiontools.Selection( filename.split('.')[0], info['Node'].gather_new_nodes(), info['Element'].gather_new_elements()) except KeyError as exc: raise KeyError( 'The class `%s` cannot be loaded from the network ' 'file `%s`. Please refer to the HydPy documentation ' 'on how to prepare network files properly.' % (exc.args[0], filename)) selections += selectiontools.Selection( 'complete', info['Node'].registered_nodes(), info['Element'].registered_elements()) return selections
def load(self): """Load nodes and elements from all network files and return them in a :class:`~hydpy.selectiontools.Selections` instance. Each single network file defines a seperate :class:`~hydpy.selectiontools.Selection` instance. Additionally, all elements and nodes are bundled in a selection named `complete`. """ selections = selectiontools.Selections() for (filename, path) in zip(self.filenames, self.filepaths): # Ensure both `Node` and `Element`start with a `fresh` memory. devicetools.Node.gathernewnodes() devicetools.Element.gathernewelements() info = {} try: with open(path) as file_: code = compile(file_.read(), path, 'exec') exec(code, {}, info) except Exception: prefix = 'While trying to load the network file `%s`' % path objecttools.augmentexcmessage(prefix) try: selections += selectiontools.Selection( filename.split('.')[0], info['Node'].gathernewnodes(), info['Element'].gathernewelements()) except KeyError as exc: KeyError('The class `%s` cannot be loaded from the network ' 'file `%s`. Please refer to the HydPy documentation ' 'on how to prepare network files properly.' % (exc.args[0], filename)) selections += selectiontools.Selection( 'complete', info['Node'].registerednodes(), info['Element'].registeredelements()) return selections
def selection(self): """A complete |Selection| object of all "supplying" and "routing" elements and required nodes. >>> from hydpy import RiverBasinNumbers2Selection >>> rbns2s = RiverBasinNumbers2Selection( ... (111, 113, 1129, 11269, 1125, 11261, ... 11262, 1123, 1124, 1122, 1121)) >>> rbns2s.selection Selection("complete", elements=("land_111", "land_1121", "land_1122", "land_1123", "land_1124", "land_1125", "land_11261", "land_11262", "land_11269", "land_1129", "land_113", "stream_1123", "stream_1125", "stream_11269", "stream_1129", "stream_113"), nodes=("node_1123", "node_1125", "node_11269", "node_1129", "node_113", "node_outlet")) Besides the possible modifications on the names of the different nodes and elements, the name of the selection can be set differently: >>> rbns2s.selection_name = 'sel' >>> from hydpy import pub >>> with pub.options.ellipsis(1): ... print(repr(rbns2s.selection)) Selection("sel", elements=("land_111", ...,"stream_113"), nodes=("node_1123", ...,"node_outlet")) """ return selectiontools.Selection(self.selection_name, self.nodes, self.elements)
def selection(self) -> selectiontools.Selection: """A complete |Selection| object containing all required elements and nodes. >>> from hydpy import RiverBasinNumbers2Selection >>> rbns2s = RiverBasinNumbers2Selection( ... (111, 113, 1129, 11269, 1125, 11261, ... 11262, 1123, 1124, 1122, 1121)) >>> rbns2s.selection Selection("complete", nodes=("node_1123", "node_1125", "node_11269", "node_1129", "node_113", "node_outlet"), elements=("land_111", "land_1121", "land_1122", "land_1123", "land_1124", "land_1125", "land_11261", "land_11262", "land_11269", "land_1129", "land_113", "stream_1123", "stream_1125", "stream_11269", "stream_1129", "stream_113")) Besides the possible modifications on the names of the different nodes and elements, one is also free to define an arbitrary selection name: >>> rbns2s.selection_name = "sel" >>> from hydpy import pub >>> with pub.options.ellipsis(1): ... print(repr(rbns2s.selection)) Selection("sel", nodes=("node_1123", ...,"node_outlet"), elements=("land_111", ...,"stream_113")) """ return selectiontools.Selection(self.selection_name, self.nodes, self.elements)
def __init__(self, element, seqs=None, inits=None): nodes = devicetools.Nodes() for connection in (element.inlets, element.outlets, element.receivers, element.senders): nodes += connection.slaves for (name, node) in nodes: if (node in element.inlets) or (node in element.receivers): node.routingmode = 'oldsim' sim = node.sequences.sim sim.ramflag = True sim._setarray(numpy.zeros(len(pub.timegrids.init), dtype=float)) for (name, seq) in getattr(element.model.sequences, 'inputs', ()): seq.ramflag = True seq._setarray(numpy.zeros(len(pub.timegrids.init), dtype=float)) if seqs is None: seqs = [] for subseqs in ('inputs', 'fluxes', 'states'): for (name, seq) in getattr(element.model.sequences, subseqs, ()): seqs.append(seq) for (name, node) in nodes: seqs.append(node.sequences.sim) element.prepare_fluxseries() element.prepare_stateseries() self.element = element self.nodes = nodes self.seqs = seqs self.inits = {} if inits is None else inits self.model = element.model hydpytools.HydPy.nmb_instances = 0 self.hp = hydpytools.HydPy() self.hp.updatedevices(selectiontools.Selection('test', nodes, element))
def __init__(self, element, seqs=None, inits=None): """Prepare the element and its nodes and put them into a HydPy object and make their sequences ready for use for integration testing.""" del self.inits self.element = element self.nodes = self.extract_nodes() self.prepare_input_node_sequences() self.prepare_input_model_sequences() self.parseqs = seqs if seqs else self.extract_print_sequences() self.inits = inits self.model = element.model hydpytools.HydPy.nmb_instances = 0 self.hp = hydpytools.HydPy() self.hp.updatedevices(selectiontools.Selection( 'test', self.nodes, element))
def distinct_networks(self): sels1 = selectiontools.Selections() sels2 = selectiontools.Selections() complete = selectiontools.Selection('complete', self.nodes, self.elements) for (name, node) in self.endnodes: sel = complete.copy(name).select_upstream(node) sels1 += sel sels2 += sel.copy(name) for (name1, sel1) in sels1: for (name2, sel2) in sels2: if name1 != name2: sel1 -= sel2 for name in sels1.names: if not sels1[name].elements: del sels1[name] return sels1
def numberofnetworks(self): """The number of distinct networks defined by the|Node| and |Element| objects currently handled by the |HydPy| object.""" sels1 = selectiontools.Selections() sels2 = selectiontools.Selections() complete = selectiontools.Selection('complete', self.nodes, self.elements) for node in self.endnodes: sel = complete.copy(node.name).select_upstream(node) sels1 += sel sels2 += sel.copy(node.name) for sel1 in sels1: for sel2 in sels2: if sel1.name != sel2.name: sel1 -= sel2 for name in list(sels1.names): if not sels1[name].elements: del sels1[name] return sels1
def __init__(self, element, seqs=None, inits=None): """Prepare the element and its nodes and put them into a HydPy object and make their sequences ready for use for integration testing.""" del self.inits self.element = element self.elements = devicetools.Element.query_all() self.nodes = devicetools.Node.query_all() self.prepare_node_sequences() self.prepare_input_model_sequences() self.parseqs = seqs if seqs else self.extract_print_sequences() self.inits = inits self.model = element.model hydpytools.HydPy.nmb_instances = 0 self.hydpy = hydpytools.HydPy() self.hydpy.update_devices( selectiontools.Selection('test', self.nodes, self.elements)) self._src = None self._width = None self._height = None