예제 #1
0
 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))
예제 #2
0
 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))
예제 #3
0
def prepare_full_example_2(
    lastdate: "timetools.DateConstrArg" = "1996-01-05",
) -> Tuple[hydpytools.HydPy, "pubtools.Pub", Type[testtools.TestIO]]:
    """Prepare the `LahnH` project on disk and in RAM.

    Function |prepare_full_example_2| is an extensions of function
    |prepare_full_example_1|.  Besides preparing the project data of
    the `LahnH` example project, it performs all necessary steps to
    start a simulation run.  Therefore, it returns a readily prepared
    |HydPy| instance, as well as, for convenience, module |pub| and
    class |TestIO|:

    >>> from hydpy.examples import prepare_full_example_2
    >>> hp, pub, TestIO = prepare_full_example_2()
    >>> hp.nodes
    Nodes("dill", "lahn_1", "lahn_2", "lahn_3")
    >>> hp.elements
    Elements("land_dill", "land_lahn_1", "land_lahn_2", "land_lahn_3",
             "stream_dill_lahn_2", "stream_lahn_1_lahn_2",
             "stream_lahn_2_lahn_3")
    >>> pub.timegrids
    Timegrids("1996-01-01 00:00:00",
              "1996-01-05 00:00:00",
              "1d")
    >>> from hydpy import classname
    >>> classname(TestIO)
    'TestIO'

    Function |prepare_full_example_2| is primarily thought for testing
    and thus does not allow for many configurations except changing the
    end date of the initialisation period:

    >>> hp, pub, TestIO = prepare_full_example_2("1996-02-01")
    >>> pub.timegrids
    Timegrids("1996-01-01 00:00:00",
              "1996-02-01 00:00:00",
              "1d")
    """
    prepare_full_example_1()
    with testtools.TestIO():
        hp = hydpytools.HydPy("LahnH")
        hydpy.pub.timegrids = "1996-01-01", lastdate, "1d"
        hp.prepare_everything()
    return hp, hydpy.pub, testtools.TestIO
예제 #4
0
 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
예제 #5
0
    def initialise(self, projectname: str, xmlfile: str) -> None:
        """Initialise a *HydPy* project based on the given XML configuration
        file agreeing with `HydPyConfigMultipleRuns.xsd`.

        We use the `LahnH` project and its (complicated) XML configuration
        file `multiple_runs.xml` as an example (module |xmltools| provides
        information on interpreting this file):

        >>> from hydpy.examples import prepare_full_example_1
        >>> prepare_full_example_1()
        >>> from hydpy import print_values, TestIO
        >>> from hydpy.exe.servertools import ServerState
        >>> state = ServerState()
        >>> with TestIO():    # doctest: +ELLIPSIS
        ...     state.initialise('LahnH', 'multiple_runs.xml')
        Start HydPy project `LahnH` (...).
        Read configuration file `multiple_runs.xml` (...).
        Interpret the defined options (...).
        Interpret the defined period (...).
        Read all network files (...).
        Activate the selected network (...).
        Read the required control files (...).
        Read the required condition files (...).
        Read the required time series files (...).

        After initialisation, all defined exchange items are available:

        >>> for item in state.parameteritems:
        ...     print(item)
        SetItem('alpha', 'hland_v1', 'control.alpha', 0)
        SetItem('beta', 'hland_v1', 'control.beta', 0)
        SetItem('lag', 'hstream_v1', 'control.lag', 0)
        SetItem('damp', 'hstream_v1', 'control.damp', 0)
        AddItem('sfcf_1', 'hland_v1', 'control.sfcf', 'control.rfcf', 0)
        AddItem('sfcf_2', 'hland_v1', 'control.sfcf', 'control.rfcf', 0)
        AddItem('sfcf_3', 'hland_v1', 'control.sfcf', 'control.rfcf', 1)
        >>> for item in state.conditionitems:
        ...     print(item)
        SetItem('sm_lahn_2', 'hland_v1', 'states.sm', 0)
        SetItem('sm_lahn_1', 'hland_v1', 'states.sm', 1)
        SetItem('quh', 'hland_v1', 'logs.quh', 0)
        >>> for item in state.getitems:
        ...     print(item)
        GetItem('hland_v1', 'fluxes.qt')
        GetItem('hland_v1', 'fluxes.qt.series')
        GetItem('hland_v1', 'states.sm')
        GetItem('hland_v1', 'states.sm.series')
        GetItem('nodes', 'nodes.sim.series')

        The initialisation also memorises the initial conditions of
        all elements:

        >>> for element in state.init_conditions:
        ...     print(element)
        land_dill
        land_lahn_1
        land_lahn_2
        land_lahn_3
        stream_dill_lahn_2
        stream_lahn_1_lahn_2
        stream_lahn_2_lahn_3

        The initialisation also prepares all selected series arrays and
        reads the required input data:

        >>> print_values(
        ...     state.hp.elements.land_dill.model.sequences.inputs.t.series)
        -0.298846, -0.811539, -2.493848, -5.968849, -6.999618
        >>> state.hp.nodes.dill.sequences.sim.series
        InfoArray([ nan,  nan,  nan,  nan,  nan])
        """
        write = commandtools.print_textandtime
        write(f'Start HydPy project `{projectname}`')
        hp = hydpytools.HydPy(projectname)
        write(f'Read configuration file `{xmlfile}`')
        interface = xmltools.XMLInterface(xmlfile)
        write('Interpret the defined options')
        interface.update_options()
        write('Interpret the defined period')
        interface.update_timegrids()
        write('Read all network files')
        hp.prepare_network()
        write('Activate the selected network')
        hp.update_devices(interface.fullselection)
        write('Read the required control files')
        hp.prepare_models()
        write('Read the required condition files')
        interface.conditions_io.load_conditions()
        write('Read the required time series files')
        interface.series_io.prepare_series()
        interface.exchange.prepare_series()
        interface.series_io.load_series()
        self.hp = hp
        self.parameteritems = interface.exchange.parameteritems
        self.conditionitems = interface.exchange.conditionitems
        self.getitems = interface.exchange.getitems
        self.conditions = {}
        self.parameteritemvalues = collections.defaultdict(lambda: {})
        self.modifiedconditionitemvalues = collections.defaultdict(lambda: {})
        self.getitemvalues = collections.defaultdict(lambda: {})
        self.init_conditions = hp.conditions
        self.timegrids = {}