Skip to content

API Reference - Arcs

This section of the documentation provides a reference for the API of the arcs.arcs module.

Created on Wed Apr 7 08:43:32 2021.

@author: Barney

Converted to totals on Thur Apr 21 2022

AltQueueArc

Bases: QueueArc

Source code in wsimod\arcs\arcs.py
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
class AltQueueArc(QueueArc):
    """"""

    def __init__(self, **kwargs):
        """A simpler queue arc that has a queue that is a dict where each key is the
        travel time.

        Cannot be used if arc capacity is dynamic. Cannot be used for pulls.
        """
        self.queue_arc_sum = self.alt_queue_arc_sum

        super().__init__(**kwargs)
        self.queue = {0: self.empty_vqip(), 1: self.empty_vqip()}
        self.max_travel = 1

    def alt_queue_arc_sum(self):
        """Sum the total water in the queue of the arc.

        Returns:
            (dict): A VQIP amount of water/pollutants in the arc
        """
        queue_storage = self.empty_vqip()
        for request in self.queue.values():
            queue_storage = self.sum_vqip(queue_storage, request)
        return queue_storage

    def enter_queue(self, request, direction="push", tag="default"):
        """Add a request into the arc's queue.

        Args:
            request (dict): A dict with a VQIP under the 'vqip' key and the travel
                time under the 'time' key.
            direction (str): Direction of flow, can be 'push' only. Defaults to 'push'
            tag (str, optional): Optional message for out_port's query handler, can be
                'default' only. Defaults to 'default'.
        """
        # Update inflows and format request
        request = self.enter_arc(request, direction, tag)

        # Sum into queue
        if request["time"] in self.queue.keys():
            self.queue[request["time"]] = self.sum_vqip(
                self.queue[request["time"]], request["vqip"]
            )
        else:
            self.queue[request["time"]] = request["vqip"]
            self.max_travel = max(self.max_travel, request["time"])

    def update_queue(self, direction=None, backflow_enabled=True):
        """Trigger the push of water in the 0th key for the queue, if the out_port
        responds that it cannot receive the push, then this water will be returned as
        backflow (if enabled).

        Args:
            direction (str): Direction of flow, can be 'push' only. Defaults to 'push'
            backflow_enabled (bool, optional): Enable backflow, described above, if not
                enabled then the request will remain in the queue until all water has
                been received. Defaults to True.

        Returns:
            backflow (dict): In the case of a push direction, any backflow will be
                returned as a VQIP amount
        """
        # TODO - can this work for pulls??

        total_removed = self.copy_vqip(self.queue[0])

        # Push 0 travel time water
        backflow = self.out_port.push_set(total_removed)

        if not backflow_enabled:
            self.queue[0] = backflow
            backflow = self.empty_vqip()
        else:
            self.queue[0] = self.empty_vqip()

        total_removed = self.v_change_vqip(
            total_removed, total_removed["volume"] - backflow["volume"]
        )

        self.flow_out += total_removed["volume"]
        self.vqip_out = self.sum_vqip(self.vqip_out, total_removed)

        return backflow

    def end_timestep(self):
        """End timestep in an arc, resetting flow/vqip in/out (which determine) the
        capacity for that timestep.

        Update timings in the queue.
        """
        self.vqip_in = self.empty_vqip()
        self.vqip_out = self.empty_vqip()
        self.flow_in = 0
        self.flow_out = 0
        self.queue_storage_ = self.copy_vqip(self.queue_storage)
        self.queue_storage = self.empty_vqip()

        queue_ = self.queue.copy()
        keys = self.queue.keys()
        for i in range(self.max_travel):
            if (i + 1) in keys:
                self.queue[i] = queue_[i + 1]
                self.queue[i + 1] = self.empty_vqip()

        self.queue[0] = self.sum_vqip(queue_[0], queue_[1])

    def reinit(self):
        """"""
        self.end_timestep()
        self.queue = {0: self.empty_vqip(), 1: self.empty_vqip()}

__init__(**kwargs)

A simpler queue arc that has a queue that is a dict where each key is the travel time.

Cannot be used if arc capacity is dynamic. Cannot be used for pulls.

Source code in wsimod\arcs\arcs.py
558
559
560
561
562
563
564
565
566
567
568
def __init__(self, **kwargs):
    """A simpler queue arc that has a queue that is a dict where each key is the
    travel time.

    Cannot be used if arc capacity is dynamic. Cannot be used for pulls.
    """
    self.queue_arc_sum = self.alt_queue_arc_sum

    super().__init__(**kwargs)
    self.queue = {0: self.empty_vqip(), 1: self.empty_vqip()}
    self.max_travel = 1

alt_queue_arc_sum()

Sum the total water in the queue of the arc.

Returns:

Type Description
dict

A VQIP amount of water/pollutants in the arc

Source code in wsimod\arcs\arcs.py
570
571
572
573
574
575
576
577
578
579
def alt_queue_arc_sum(self):
    """Sum the total water in the queue of the arc.

    Returns:
        (dict): A VQIP amount of water/pollutants in the arc
    """
    queue_storage = self.empty_vqip()
    for request in self.queue.values():
        queue_storage = self.sum_vqip(queue_storage, request)
    return queue_storage

end_timestep()

End timestep in an arc, resetting flow/vqip in/out (which determine) the capacity for that timestep.

Update timings in the queue.

Source code in wsimod\arcs\arcs.py
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
def end_timestep(self):
    """End timestep in an arc, resetting flow/vqip in/out (which determine) the
    capacity for that timestep.

    Update timings in the queue.
    """
    self.vqip_in = self.empty_vqip()
    self.vqip_out = self.empty_vqip()
    self.flow_in = 0
    self.flow_out = 0
    self.queue_storage_ = self.copy_vqip(self.queue_storage)
    self.queue_storage = self.empty_vqip()

    queue_ = self.queue.copy()
    keys = self.queue.keys()
    for i in range(self.max_travel):
        if (i + 1) in keys:
            self.queue[i] = queue_[i + 1]
            self.queue[i + 1] = self.empty_vqip()

    self.queue[0] = self.sum_vqip(queue_[0], queue_[1])

enter_queue(request, direction='push', tag='default')

Add a request into the arc's queue.

Parameters:

Name Type Description Default
request dict

A dict with a VQIP under the 'vqip' key and the travel time under the 'time' key.

required
direction str

Direction of flow, can be 'push' only. Defaults to 'push'

'push'
tag str

Optional message for out_port's query handler, can be 'default' only. Defaults to 'default'.

'default'
Source code in wsimod\arcs\arcs.py
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
def enter_queue(self, request, direction="push", tag="default"):
    """Add a request into the arc's queue.

    Args:
        request (dict): A dict with a VQIP under the 'vqip' key and the travel
            time under the 'time' key.
        direction (str): Direction of flow, can be 'push' only. Defaults to 'push'
        tag (str, optional): Optional message for out_port's query handler, can be
            'default' only. Defaults to 'default'.
    """
    # Update inflows and format request
    request = self.enter_arc(request, direction, tag)

    # Sum into queue
    if request["time"] in self.queue.keys():
        self.queue[request["time"]] = self.sum_vqip(
            self.queue[request["time"]], request["vqip"]
        )
    else:
        self.queue[request["time"]] = request["vqip"]
        self.max_travel = max(self.max_travel, request["time"])

reinit()

Source code in wsimod\arcs\arcs.py
662
663
664
665
def reinit(self):
    """"""
    self.end_timestep()
    self.queue = {0: self.empty_vqip(), 1: self.empty_vqip()}

update_queue(direction=None, backflow_enabled=True)

Trigger the push of water in the 0th key for the queue, if the out_port responds that it cannot receive the push, then this water will be returned as backflow (if enabled).

Parameters:

Name Type Description Default
direction str

Direction of flow, can be 'push' only. Defaults to 'push'

None
backflow_enabled bool

Enable backflow, described above, if not enabled then the request will remain in the queue until all water has been received. Defaults to True.

True

Returns:

Name Type Description
backflow dict

In the case of a push direction, any backflow will be returned as a VQIP amount

Source code in wsimod\arcs\arcs.py
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
def update_queue(self, direction=None, backflow_enabled=True):
    """Trigger the push of water in the 0th key for the queue, if the out_port
    responds that it cannot receive the push, then this water will be returned as
    backflow (if enabled).

    Args:
        direction (str): Direction of flow, can be 'push' only. Defaults to 'push'
        backflow_enabled (bool, optional): Enable backflow, described above, if not
            enabled then the request will remain in the queue until all water has
            been received. Defaults to True.

    Returns:
        backflow (dict): In the case of a push direction, any backflow will be
            returned as a VQIP amount
    """
    # TODO - can this work for pulls??

    total_removed = self.copy_vqip(self.queue[0])

    # Push 0 travel time water
    backflow = self.out_port.push_set(total_removed)

    if not backflow_enabled:
        self.queue[0] = backflow
        backflow = self.empty_vqip()
    else:
        self.queue[0] = self.empty_vqip()

    total_removed = self.v_change_vqip(
        total_removed, total_removed["volume"] - backflow["volume"]
    )

    self.flow_out += total_removed["volume"]
    self.vqip_out = self.sum_vqip(self.vqip_out, total_removed)

    return backflow

Arc

Bases: WSIObj

Source code in wsimod\arcs\arcs.py
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
class Arc(WSIObj):
    """"""

    def __init__(
        self,
        name="",
        capacity=constants.UNBOUNDED_CAPACITY,
        preference=1,
        in_port=None,
        out_port=None,
        **kwargs,
    ):
        """Arc objects are the way for information to be passed between nodes in WSIMOD.
        They have an in_port (where a message comes from) and an out_port (where a
        message goes to).

        Returns:
            name (str): Name of arc. Defaults to ''.
            capacity (float): Capacity of flow along an arc (vol/timestep).
                Defaults to constants.UNBOUNDED_CAPACITY.
            preference (float): Number used to prioritise or deprioritise use of an arc
                when flexibility exists
            in_port: A WSIMOD node object where the arc starts
            out_port: A WSIMOD node object where the arc ends
        """
        # Default essential parameters
        self.name = name
        self.in_port = in_port
        self.out_port = out_port
        self.capacity = capacity
        self.preference = preference

        # Update args
        WSIObj.__init__(self)
        self.__dict__.update(kwargs)

        # def all_subclasses(cls):
        #     return set(cls.__subclasses__()).union(
        #         [s for c in cls.__subclasses__() for s in all_subclasses(c)])
        # node_types = [x.__name__ for x in all_subclasses(nodes.Node)] + ['Node']

        # if self.name in node_types:
        #     print('Warning: arc name should not take a node class name')
        #     #TODO... not sure why... also currently commented for import issues..

        # Initialise states
        self.flow_in = 0
        self.flow_out = 0
        self.vqip_in = self.empty_vqip()
        self.vqip_out = self.empty_vqip()

        # Update ports
        self.in_port.out_arcs[self.name] = self
        self.out_port.in_arcs[self.name] = self

        out_type = self.out_port.__class__.__name__
        in_type = self.in_port.__class__.__name__

        if hasattr(self.in_port, "out_arcs_type"):
            self.in_port.out_arcs_type[out_type][self.name] = self

        if hasattr(self.out_port, "in_arcs_type"):
            self.out_port.in_arcs_type[in_type][self.name] = self

        # Mass balance checking
        self.mass_balance_in = [lambda: self.vqip_in]
        self.mass_balance_out = [lambda: self.vqip_out]
        self.mass_balance_ds = [lambda: self.empty_vqip()]

    def apply_overrides(self, overrides: Dict[str, Any] = {}) -> None:
        """Apply overrides to the node.

        Args:
            overrides (dict, optional): Dictionary of overrides. Defaults to {}.
        """
        self.capacity = overrides.pop("capacity", self.capacity)
        self.preference = overrides.pop("preference", self.preference)
        if len(overrides) > 0:
            print(f"No override behaviour defined for: {overrides.keys()}")

    def arc_mass_balance(self):
        """Checks mass balance for inflows/outflows/storage change in an arc.

        Returns:
            in_ (dict) Total vqip of vqip_in and other inputs in mass_balance_in
            ds_ (dict): Total vqip of change in arc in mass_balance_ds
            out_ (dict): Total vqip of vqip_out and other outputs in mass_balance_out

        Examples:
            arc_in, arc_out, arc_ds = my_arc.arc_mass_balance()
        """
        in_, ds_, out_ = self.mass_balance()
        return in_, ds_, out_

    def send_push_request(self, vqip, tag="default", force=False):
        """Function used to transmit a push request from one node (in_port) to another
        node (out_port).

        Args:
            vqip (dict): A dict VQIP of water to push
            tag (str, optional):  optional message to direct the out_port's query_
                handler which function to call. Defaults to 'default'.
            force (bool, optional): Argument used to cause function to ignore tank
                capacity of out_port, possibly resulting in pooling. Should not be used
                    unless
                out_port is a tank object. Defaults to False.

        Returns:
            (dict): A VQIP amount of water that was not successfully pushed
        """
        vqip = self.copy_vqip(vqip)

        # Apply pipe capacity
        if force:
            not_pushed = self.empty_vqip()
        else:
            excess_in = self.get_excess(direction="push", vqip=vqip, tag=tag)
            not_pushed = self.v_change_vqip(
                vqip, max(vqip["volume"] - excess_in["volume"], 0)
            )

        # Don't attempt to send volume that exceeds capacity
        vqip = self.extract_vqip(vqip, not_pushed)

        # Set push
        reply = self.out_port.push_set(vqip, tag)

        # Update total amount successfully sent
        vqip = self.extract_vqip(vqip, reply)

        # Combine non-sent water
        reply = self.sum_vqip(reply, not_pushed)

        # Update mass balance
        self.flow_in += vqip["volume"]
        self.flow_out = self.flow_in

        self.vqip_in = self.sum_vqip(self.vqip_in, vqip)
        self.vqip_out = self.vqip_in

        return reply

    def send_pull_request(self, vqip, tag="default"):
        """Function used to transmit a pull request from one node (in_port) to another
        node (out_port).

        Args:
            vqip (dict): A dict VQIP of water to pull (by default, only 'volume' key is
                used)
            tag (str, optional): optional message to direct the out_port's query_handler
                which
                function to call. Defaults to 'default'.

        Returns:
            (dict): A VQIP amount of water that was successfully pulled
        """
        volume = vqip["volume"]
        # Apply pipe capacity
        excess_in = self.get_excess(direction="pull", vqip=vqip, tag=tag)["volume"]
        not_pulled = max(volume - excess_in, 0)
        volume -= not_pulled

        if volume > 0:
            for pol in constants.ADDITIVE_POLLUTANTS:
                if pol in vqip.keys():
                    vqip[pol] *= volume / vqip["volume"]

        vqip["volume"] = volume

        # Make pull
        vqip = self.in_port.pull_set(vqip, tag)

        # Update mass balance
        self.flow_in += vqip["volume"]
        self.flow_out = self.flow_in

        self.vqip_in = self.sum_vqip(self.vqip_in, vqip)
        self.vqip_out = self.vqip_in

        return vqip

    def send_push_check(self, vqip=None, tag="default"):
        """Function used to transmit a push check from one node (in_port) to another
        node (out_port).

        Args:
            vqip (dict): A dict VQIP of water to push that can be specified. Defaults to
                None, which returns maximum capacity to push.
            tag (str, optional):  optional message to direct the out_port's
                query_handler which function to call. Defaults to 'default'.

        Returns:
            (dict): A VQIP amount of water that could be pushed
        """
        return self.get_excess(direction="push", vqip=vqip, tag=tag)

    def send_pull_check(self, vqip=None, tag="default"):
        """Function used to transmit a pull check from one node (in_port) to another
        node (out_port).

        Args:
            vqip (dict): A dict VQIP of water to pull that can be specified (by default,
                only the 'volume' key is used). Defaults to None, which returns all
                    available water to pull.
            tag (str, optional):  optional message to direct the out_port's
                query_handler which function to call. Defaults to 'default'.

        Returns:
            (dict): A VQIP amount of water that could be pulled
        """
        return self.get_excess(direction="pull", vqip=vqip, tag=tag)

    def get_excess(self, direction, vqip=None, tag="default"):
        """Calculate how much could be pull/pulled along the arc by combining both arc
        capacity and out_port check information.

        Args:
            direction (str): should be 'pull' or 'push'
            vqip (dict, optional): A VQIP amount to push/pull that can be
                specified. Defaults to None, which returns all available water to
                pull or maximum capacity to push (depending on 'direction').
            tag (str, optional): optional message to direct the out_port's query_handler
                which function to call. Defaults to 'default'.

        Returns:
            (dict): A VQIP amount of water that could be pulled/pushed
        """
        # Pipe capacity
        pipe_excess = self.capacity - self.flow_in

        # Node capacity
        if direction == "push":
            node_excess = self.out_port.push_check(vqip, tag)
        elif direction == "pull":
            node_excess = self.in_port.pull_check(vqip, tag)
        excess = min(pipe_excess, node_excess["volume"])

        # TODO sensible to min(vqip, excess) here? (though it should be applied by node)

        return self.v_change_vqip(node_excess, excess)

    def end_timestep(self):
        """End timestep in an arc, resetting flow/vqip in/out (which determine) the
        capacity for that timestep."""
        self.vqip_in = self.empty_vqip()
        self.vqip_out = self.empty_vqip()
        self.flow_in = 0
        self.flow_out = 0

    def reinit(self):
        """Reinitiatilise."""
        self.end_timestep()

__init__(name='', capacity=constants.UNBOUNDED_CAPACITY, preference=1, in_port=None, out_port=None, **kwargs)

Arc objects are the way for information to be passed between nodes in WSIMOD. They have an in_port (where a message comes from) and an out_port (where a message goes to).

Returns:

Name Type Description
name str

Name of arc. Defaults to ''.

capacity float

Capacity of flow along an arc (vol/timestep). Defaults to constants.UNBOUNDED_CAPACITY.

preference float

Number used to prioritise or deprioritise use of an arc when flexibility exists

in_port

A WSIMOD node object where the arc starts

out_port

A WSIMOD node object where the arc ends

Source code in wsimod\arcs\arcs.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def __init__(
    self,
    name="",
    capacity=constants.UNBOUNDED_CAPACITY,
    preference=1,
    in_port=None,
    out_port=None,
    **kwargs,
):
    """Arc objects are the way for information to be passed between nodes in WSIMOD.
    They have an in_port (where a message comes from) and an out_port (where a
    message goes to).

    Returns:
        name (str): Name of arc. Defaults to ''.
        capacity (float): Capacity of flow along an arc (vol/timestep).
            Defaults to constants.UNBOUNDED_CAPACITY.
        preference (float): Number used to prioritise or deprioritise use of an arc
            when flexibility exists
        in_port: A WSIMOD node object where the arc starts
        out_port: A WSIMOD node object where the arc ends
    """
    # Default essential parameters
    self.name = name
    self.in_port = in_port
    self.out_port = out_port
    self.capacity = capacity
    self.preference = preference

    # Update args
    WSIObj.__init__(self)
    self.__dict__.update(kwargs)

    # def all_subclasses(cls):
    #     return set(cls.__subclasses__()).union(
    #         [s for c in cls.__subclasses__() for s in all_subclasses(c)])
    # node_types = [x.__name__ for x in all_subclasses(nodes.Node)] + ['Node']

    # if self.name in node_types:
    #     print('Warning: arc name should not take a node class name')
    #     #TODO... not sure why... also currently commented for import issues..

    # Initialise states
    self.flow_in = 0
    self.flow_out = 0
    self.vqip_in = self.empty_vqip()
    self.vqip_out = self.empty_vqip()

    # Update ports
    self.in_port.out_arcs[self.name] = self
    self.out_port.in_arcs[self.name] = self

    out_type = self.out_port.__class__.__name__
    in_type = self.in_port.__class__.__name__

    if hasattr(self.in_port, "out_arcs_type"):
        self.in_port.out_arcs_type[out_type][self.name] = self

    if hasattr(self.out_port, "in_arcs_type"):
        self.out_port.in_arcs_type[in_type][self.name] = self

    # Mass balance checking
    self.mass_balance_in = [lambda: self.vqip_in]
    self.mass_balance_out = [lambda: self.vqip_out]
    self.mass_balance_ds = [lambda: self.empty_vqip()]

apply_overrides(overrides={})

Apply overrides to the node.

Parameters:

Name Type Description Default
overrides dict

Dictionary of overrides. Defaults to {}.

{}
Source code in wsimod\arcs\arcs.py
87
88
89
90
91
92
93
94
95
96
def apply_overrides(self, overrides: Dict[str, Any] = {}) -> None:
    """Apply overrides to the node.

    Args:
        overrides (dict, optional): Dictionary of overrides. Defaults to {}.
    """
    self.capacity = overrides.pop("capacity", self.capacity)
    self.preference = overrides.pop("preference", self.preference)
    if len(overrides) > 0:
        print(f"No override behaviour defined for: {overrides.keys()}")

arc_mass_balance()

Checks mass balance for inflows/outflows/storage change in an arc.

Returns:

Name Type Description

in_ (dict) Total vqip of vqip_in and other inputs in mass_balance_in

ds_ dict

Total vqip of change in arc in mass_balance_ds

out_ dict

Total vqip of vqip_out and other outputs in mass_balance_out

Examples:

arc_in, arc_out, arc_ds = my_arc.arc_mass_balance()

Source code in wsimod\arcs\arcs.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def arc_mass_balance(self):
    """Checks mass balance for inflows/outflows/storage change in an arc.

    Returns:
        in_ (dict) Total vqip of vqip_in and other inputs in mass_balance_in
        ds_ (dict): Total vqip of change in arc in mass_balance_ds
        out_ (dict): Total vqip of vqip_out and other outputs in mass_balance_out

    Examples:
        arc_in, arc_out, arc_ds = my_arc.arc_mass_balance()
    """
    in_, ds_, out_ = self.mass_balance()
    return in_, ds_, out_

end_timestep()

End timestep in an arc, resetting flow/vqip in/out (which determine) the capacity for that timestep.

Source code in wsimod\arcs\arcs.py
259
260
261
262
263
264
265
def end_timestep(self):
    """End timestep in an arc, resetting flow/vqip in/out (which determine) the
    capacity for that timestep."""
    self.vqip_in = self.empty_vqip()
    self.vqip_out = self.empty_vqip()
    self.flow_in = 0
    self.flow_out = 0

get_excess(direction, vqip=None, tag='default')

Calculate how much could be pull/pulled along the arc by combining both arc capacity and out_port check information.

Parameters:

Name Type Description Default
direction str

should be 'pull' or 'push'

required
vqip dict

A VQIP amount to push/pull that can be specified. Defaults to None, which returns all available water to pull or maximum capacity to push (depending on 'direction').

None
tag str

optional message to direct the out_port's query_handler which function to call. Defaults to 'default'.

'default'

Returns:

Type Description
dict

A VQIP amount of water that could be pulled/pushed

Source code in wsimod\arcs\arcs.py
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def get_excess(self, direction, vqip=None, tag="default"):
    """Calculate how much could be pull/pulled along the arc by combining both arc
    capacity and out_port check information.

    Args:
        direction (str): should be 'pull' or 'push'
        vqip (dict, optional): A VQIP amount to push/pull that can be
            specified. Defaults to None, which returns all available water to
            pull or maximum capacity to push (depending on 'direction').
        tag (str, optional): optional message to direct the out_port's query_handler
            which function to call. Defaults to 'default'.

    Returns:
        (dict): A VQIP amount of water that could be pulled/pushed
    """
    # Pipe capacity
    pipe_excess = self.capacity - self.flow_in

    # Node capacity
    if direction == "push":
        node_excess = self.out_port.push_check(vqip, tag)
    elif direction == "pull":
        node_excess = self.in_port.pull_check(vqip, tag)
    excess = min(pipe_excess, node_excess["volume"])

    # TODO sensible to min(vqip, excess) here? (though it should be applied by node)

    return self.v_change_vqip(node_excess, excess)

reinit()

Reinitiatilise.

Source code in wsimod\arcs\arcs.py
267
268
269
def reinit(self):
    """Reinitiatilise."""
    self.end_timestep()

send_pull_check(vqip=None, tag='default')

Function used to transmit a pull check from one node (in_port) to another node (out_port).

Parameters:

Name Type Description Default
vqip dict

A dict VQIP of water to pull that can be specified (by default, only the 'volume' key is used). Defaults to None, which returns all available water to pull.

None
tag str

optional message to direct the out_port's query_handler which function to call. Defaults to 'default'.

'default'

Returns:

Type Description
dict

A VQIP amount of water that could be pulled

Source code in wsimod\arcs\arcs.py
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
def send_pull_check(self, vqip=None, tag="default"):
    """Function used to transmit a pull check from one node (in_port) to another
    node (out_port).

    Args:
        vqip (dict): A dict VQIP of water to pull that can be specified (by default,
            only the 'volume' key is used). Defaults to None, which returns all
                available water to pull.
        tag (str, optional):  optional message to direct the out_port's
            query_handler which function to call. Defaults to 'default'.

    Returns:
        (dict): A VQIP amount of water that could be pulled
    """
    return self.get_excess(direction="pull", vqip=vqip, tag=tag)

send_pull_request(vqip, tag='default')

Function used to transmit a pull request from one node (in_port) to another node (out_port).

Parameters:

Name Type Description Default
vqip dict

A dict VQIP of water to pull (by default, only 'volume' key is used)

required
tag str

optional message to direct the out_port's query_handler which function to call. Defaults to 'default'.

'default'

Returns:

Type Description
dict

A VQIP amount of water that was successfully pulled

Source code in wsimod\arcs\arcs.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
def send_pull_request(self, vqip, tag="default"):
    """Function used to transmit a pull request from one node (in_port) to another
    node (out_port).

    Args:
        vqip (dict): A dict VQIP of water to pull (by default, only 'volume' key is
            used)
        tag (str, optional): optional message to direct the out_port's query_handler
            which
            function to call. Defaults to 'default'.

    Returns:
        (dict): A VQIP amount of water that was successfully pulled
    """
    volume = vqip["volume"]
    # Apply pipe capacity
    excess_in = self.get_excess(direction="pull", vqip=vqip, tag=tag)["volume"]
    not_pulled = max(volume - excess_in, 0)
    volume -= not_pulled

    if volume > 0:
        for pol in constants.ADDITIVE_POLLUTANTS:
            if pol in vqip.keys():
                vqip[pol] *= volume / vqip["volume"]

    vqip["volume"] = volume

    # Make pull
    vqip = self.in_port.pull_set(vqip, tag)

    # Update mass balance
    self.flow_in += vqip["volume"]
    self.flow_out = self.flow_in

    self.vqip_in = self.sum_vqip(self.vqip_in, vqip)
    self.vqip_out = self.vqip_in

    return vqip

send_push_check(vqip=None, tag='default')

Function used to transmit a push check from one node (in_port) to another node (out_port).

Parameters:

Name Type Description Default
vqip dict

A dict VQIP of water to push that can be specified. Defaults to None, which returns maximum capacity to push.

None
tag str

optional message to direct the out_port's query_handler which function to call. Defaults to 'default'.

'default'

Returns:

Type Description
dict

A VQIP amount of water that could be pushed

Source code in wsimod\arcs\arcs.py
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def send_push_check(self, vqip=None, tag="default"):
    """Function used to transmit a push check from one node (in_port) to another
    node (out_port).

    Args:
        vqip (dict): A dict VQIP of water to push that can be specified. Defaults to
            None, which returns maximum capacity to push.
        tag (str, optional):  optional message to direct the out_port's
            query_handler which function to call. Defaults to 'default'.

    Returns:
        (dict): A VQIP amount of water that could be pushed
    """
    return self.get_excess(direction="push", vqip=vqip, tag=tag)

send_push_request(vqip, tag='default', force=False)

Function used to transmit a push request from one node (in_port) to another node (out_port).

Parameters:

Name Type Description Default
vqip dict

A dict VQIP of water to push

required
tag str

optional message to direct the out_port's query_ handler which function to call. Defaults to 'default'.

'default'
force bool

Argument used to cause function to ignore tank capacity of out_port, possibly resulting in pooling. Should not be used unless out_port is a tank object. Defaults to False.

False

Returns:

Type Description
dict

A VQIP amount of water that was not successfully pushed

Source code in wsimod\arcs\arcs.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def send_push_request(self, vqip, tag="default", force=False):
    """Function used to transmit a push request from one node (in_port) to another
    node (out_port).

    Args:
        vqip (dict): A dict VQIP of water to push
        tag (str, optional):  optional message to direct the out_port's query_
            handler which function to call. Defaults to 'default'.
        force (bool, optional): Argument used to cause function to ignore tank
            capacity of out_port, possibly resulting in pooling. Should not be used
                unless
            out_port is a tank object. Defaults to False.

    Returns:
        (dict): A VQIP amount of water that was not successfully pushed
    """
    vqip = self.copy_vqip(vqip)

    # Apply pipe capacity
    if force:
        not_pushed = self.empty_vqip()
    else:
        excess_in = self.get_excess(direction="push", vqip=vqip, tag=tag)
        not_pushed = self.v_change_vqip(
            vqip, max(vqip["volume"] - excess_in["volume"], 0)
        )

    # Don't attempt to send volume that exceeds capacity
    vqip = self.extract_vqip(vqip, not_pushed)

    # Set push
    reply = self.out_port.push_set(vqip, tag)

    # Update total amount successfully sent
    vqip = self.extract_vqip(vqip, reply)

    # Combine non-sent water
    reply = self.sum_vqip(reply, not_pushed)

    # Update mass balance
    self.flow_in += vqip["volume"]
    self.flow_out = self.flow_in

    self.vqip_in = self.sum_vqip(self.vqip_in, vqip)
    self.vqip_out = self.vqip_in

    return reply

DecayArc

Bases: QueueArc, DecayObj

Source code in wsimod\arcs\arcs.py
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
class DecayArc(QueueArc, DecayObj):
    """"""

    def __init__(self, decays={}, **kwargs):
        """A QueueArc that applies decays from a DecayObj.

        Args:
            decays (dict, optional): A dict of dicts containing a key for each pollutant
                that decays and within that, a key for each parameter (a constant and
                exponent). Defaults to {}.
        """
        self.decays = decays

        QueueArc.__init__(self, **kwargs)
        DecayObj.__init__(self, decays)

        self.mass_balance_out.append(lambda: self.total_decayed)

    def enter_queue(self, request, direction=None, tag="default"):
        """Add a request into the arc's queue list. Apply the make_decay function (i.e.,
        the decay that occur's this timestep).

        Args:
            request (dict): A dict with a VQIP under the 'vqip' key and the travel
                time under the 'time' key.
            direction (str): Direction of flow, can be 'push' or 'pull
            tag (str, optional):  optional message to direct the out_port's
                query_handler which function to call. Defaults to 'default'.
        """
        # Update inflows and format
        request = self.enter_arc(request, direction, tag)

        # TODO - currently decay depends on temp at the in_port data object..
        # surely on vqip would be more sensible? (though this is true in many
        # places including WTW)

        # Decay on entry
        request["vqip"] = self.make_decay(request["vqip"])

        # Append to queue
        self.queue.append(request)

    def end_timestep(self):
        """End timestep in an arc, resetting flow/vqip in/out (which determine) the
        capacity for that timestep.

        Update times of requests in the queue. Apply the make_decay function (i.e., the
        decay that occurs in the following timestep).
        """
        self.vqip_in = self.empty_vqip()
        self.vqip_out = self.empty_vqip()
        self.total_decayed = self.empty_vqip()
        self.flow_in = 0
        self.flow_out = 0

        self.queue_storage_ = self.copy_vqip(self.queue_storage)
        self.queue_storage = self.empty_vqip()

        for request in self.queue:
            request["vqip"] = self.make_decay(request["vqip"])
            request["time"] = max(request["time"] - 1, 0)

__init__(decays={}, **kwargs)

A QueueArc that applies decays from a DecayObj.

Parameters:

Name Type Description Default
decays dict

A dict of dicts containing a key for each pollutant that decays and within that, a key for each parameter (a constant and exponent). Defaults to {}.

{}
Source code in wsimod\arcs\arcs.py
671
672
673
674
675
676
677
678
679
680
681
682
683
684
def __init__(self, decays={}, **kwargs):
    """A QueueArc that applies decays from a DecayObj.

    Args:
        decays (dict, optional): A dict of dicts containing a key for each pollutant
            that decays and within that, a key for each parameter (a constant and
            exponent). Defaults to {}.
    """
    self.decays = decays

    QueueArc.__init__(self, **kwargs)
    DecayObj.__init__(self, decays)

    self.mass_balance_out.append(lambda: self.total_decayed)

end_timestep()

End timestep in an arc, resetting flow/vqip in/out (which determine) the capacity for that timestep.

Update times of requests in the queue. Apply the make_decay function (i.e., the decay that occurs in the following timestep).

Source code in wsimod\arcs\arcs.py
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
def end_timestep(self):
    """End timestep in an arc, resetting flow/vqip in/out (which determine) the
    capacity for that timestep.

    Update times of requests in the queue. Apply the make_decay function (i.e., the
    decay that occurs in the following timestep).
    """
    self.vqip_in = self.empty_vqip()
    self.vqip_out = self.empty_vqip()
    self.total_decayed = self.empty_vqip()
    self.flow_in = 0
    self.flow_out = 0

    self.queue_storage_ = self.copy_vqip(self.queue_storage)
    self.queue_storage = self.empty_vqip()

    for request in self.queue:
        request["vqip"] = self.make_decay(request["vqip"])
        request["time"] = max(request["time"] - 1, 0)

enter_queue(request, direction=None, tag='default')

Add a request into the arc's queue list. Apply the make_decay function (i.e., the decay that occur's this timestep).

Parameters:

Name Type Description Default
request dict

A dict with a VQIP under the 'vqip' key and the travel time under the 'time' key.

required
direction str

Direction of flow, can be 'push' or 'pull

None
tag str

optional message to direct the out_port's query_handler which function to call. Defaults to 'default'.

'default'
Source code in wsimod\arcs\arcs.py
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
def enter_queue(self, request, direction=None, tag="default"):
    """Add a request into the arc's queue list. Apply the make_decay function (i.e.,
    the decay that occur's this timestep).

    Args:
        request (dict): A dict with a VQIP under the 'vqip' key and the travel
            time under the 'time' key.
        direction (str): Direction of flow, can be 'push' or 'pull
        tag (str, optional):  optional message to direct the out_port's
            query_handler which function to call. Defaults to 'default'.
    """
    # Update inflows and format
    request = self.enter_arc(request, direction, tag)

    # TODO - currently decay depends on temp at the in_port data object..
    # surely on vqip would be more sensible? (though this is true in many
    # places including WTW)

    # Decay on entry
    request["vqip"] = self.make_decay(request["vqip"])

    # Append to queue
    self.queue.append(request)

DecayArcAlt

Bases: AltQueueArc, DecayObj

Source code in wsimod\arcs\arcs.py
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
class DecayArcAlt(AltQueueArc, DecayObj):
    """"""

    def __init__(self, decays={}, **kwargs):
        """An AltQueueArc that applies decays from a DecayObj.

        Args:
            decays (dict, optional): A dict of dicts containing a key for each pollutant
                that decays and within that, a key for each parameter (a constant and
                exponent). Defaults to {}.
        """
        self.decays = {}

        # super().__init__(**kwargs)
        AltQueueArc.__init__(self, **kwargs)
        DecayObj.__init__(self, decays)

        self.end_timestep = self._end_timestep

        self.mass_balance_out.append(lambda: self.total_decayed)

    def enter_queue(self, request, direction=None, tag="default"):
        """Add a request into the arc's queue. Apply the make_decay function (i.e., the
        decay that occur's this timestep).

        Args:
            request (dict): A dict with a VQIP under the 'vqip' key and the travel
                time under the 'time' key.
            direction (str): Direction of flow, can be 'push' only. Defaults to 'push'
            tag (str, optional): Optional message for out_port's query handler, can be
                'default' only. Defaults to 'default'.
        """
        # TODO- has no tags

        # Update inflows and format
        request = self.enter_arc(request, direction, tag)

        # Decay on entry
        request["vqip"] = self.make_decay(request["vqip"])

        # Sum into queue
        if request["time"] in self.queue.keys():
            self.queue[request["time"]] = self.sum_vqip(
                self.queue[request["time"]], request["vqip"]
            )
        else:
            self.queue[request["time"]] = request["vqip"]
            self.max_travel = max(self.max_travel, request["time"])

    def _end_timestep(self):
        """End timestep in an arc, resetting flow/vqip in/out (which determine) the
        capacity for that timestep.

        Update timings in the queue. Apply the make_decay function (i.e., the decay that
        occurs in the following timestep).
        """
        self.vqip_in = self.empty_vqip()
        self.vqip_out = self.empty_vqip()
        self.total_decayed = self.empty_vqip()
        self.flow_in = 0
        self.flow_out = 0

        self.queue_storage_ = self.copy_vqip(self.queue_storage)
        self.queue_storage = (
            self.empty_vqip()
        )  # TODO I don't think this (or any queue_storage=  empty) is necessary

        queue_ = self.queue.copy()
        keys = self.queue.keys()
        for i in range(self.max_travel):
            if (i + 1) in keys:
                self.queue[i] = self.make_decay(queue_[i + 1])
                self.queue[i + 1] = self.empty_vqip()

        self.queue[0] = self.sum_vqip(self.queue[0], self.make_decay(queue_[0]))

__init__(decays={}, **kwargs)

An AltQueueArc that applies decays from a DecayObj.

Parameters:

Name Type Description Default
decays dict

A dict of dicts containing a key for each pollutant that decays and within that, a key for each parameter (a constant and exponent). Defaults to {}.

{}
Source code in wsimod\arcs\arcs.py
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
def __init__(self, decays={}, **kwargs):
    """An AltQueueArc that applies decays from a DecayObj.

    Args:
        decays (dict, optional): A dict of dicts containing a key for each pollutant
            that decays and within that, a key for each parameter (a constant and
            exponent). Defaults to {}.
    """
    self.decays = {}

    # super().__init__(**kwargs)
    AltQueueArc.__init__(self, **kwargs)
    DecayObj.__init__(self, decays)

    self.end_timestep = self._end_timestep

    self.mass_balance_out.append(lambda: self.total_decayed)

enter_queue(request, direction=None, tag='default')

Add a request into the arc's queue. Apply the make_decay function (i.e., the decay that occur's this timestep).

Parameters:

Name Type Description Default
request dict

A dict with a VQIP under the 'vqip' key and the travel time under the 'time' key.

required
direction str

Direction of flow, can be 'push' only. Defaults to 'push'

None
tag str

Optional message for out_port's query handler, can be 'default' only. Defaults to 'default'.

'default'
Source code in wsimod\arcs\arcs.py
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
def enter_queue(self, request, direction=None, tag="default"):
    """Add a request into the arc's queue. Apply the make_decay function (i.e., the
    decay that occur's this timestep).

    Args:
        request (dict): A dict with a VQIP under the 'vqip' key and the travel
            time under the 'time' key.
        direction (str): Direction of flow, can be 'push' only. Defaults to 'push'
        tag (str, optional): Optional message for out_port's query handler, can be
            'default' only. Defaults to 'default'.
    """
    # TODO- has no tags

    # Update inflows and format
    request = self.enter_arc(request, direction, tag)

    # Decay on entry
    request["vqip"] = self.make_decay(request["vqip"])

    # Sum into queue
    if request["time"] in self.queue.keys():
        self.queue[request["time"]] = self.sum_vqip(
            self.queue[request["time"]], request["vqip"]
        )
    else:
        self.queue[request["time"]] = request["vqip"]
        self.max_travel = max(self.max_travel, request["time"])

PullArc

Bases: Arc

Source code in wsimod\arcs\arcs.py
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
class PullArc(Arc):
    """"""

    def __init__(self, **kwargs):
        """Subclass of Arc where pushes return no availability to push.

        This creates an Arc where only pull requests/checks can be sent, similar to a
        river abstraction.
        """
        super().__init__(**kwargs)
        self.send_push_request = self.send_push_deny
        self.send_push_check = self.send_push_check_deny

    def send_push_deny(self, vqip, tag="default", force=False):
        """Function used to deny any push requests.

        Args:
            vqip (dict): A dict VQIP of water to push
            tag (str, optional):  optional message to direct the out_port's
                query_handler which function to call. Defaults to 'default'.
            force (bool, optional): Argument used to cause function to ignore tank
                capacity of out_port, possibly resulting in pooling. Should not be used
                unless out_port is a tank object. Defaults to False.

        Returns:
            (dict): A VQIP amount of water that was not successfully pushed
        """
        return vqip

    def send_push_check_deny(self, vqip=None, tag="default"):
        """Function used to deny any push checks.

        Args:
            vqip (dict): A dict VQIP of water to push that can be specified. Defaults to
                None, which returns maximum capacity to push.
            tag (str, optional):  optional message to direct the out_port's
                query_handler which function to call. Defaults to 'default'.

        Returns:
            (dict): An empty VQIP amount of water indicating no water can be pushed
        """
        return self.empty_vqip()

__init__(**kwargs)

Subclass of Arc where pushes return no availability to push.

This creates an Arc where only pull requests/checks can be sent, similar to a river abstraction.

Source code in wsimod\arcs\arcs.py
811
812
813
814
815
816
817
818
819
def __init__(self, **kwargs):
    """Subclass of Arc where pushes return no availability to push.

    This creates an Arc where only pull requests/checks can be sent, similar to a
    river abstraction.
    """
    super().__init__(**kwargs)
    self.send_push_request = self.send_push_deny
    self.send_push_check = self.send_push_check_deny

send_push_check_deny(vqip=None, tag='default')

Function used to deny any push checks.

Parameters:

Name Type Description Default
vqip dict

A dict VQIP of water to push that can be specified. Defaults to None, which returns maximum capacity to push.

None
tag str

optional message to direct the out_port's query_handler which function to call. Defaults to 'default'.

'default'

Returns:

Type Description
dict

An empty VQIP amount of water indicating no water can be pushed

Source code in wsimod\arcs\arcs.py
837
838
839
840
841
842
843
844
845
846
847
848
849
def send_push_check_deny(self, vqip=None, tag="default"):
    """Function used to deny any push checks.

    Args:
        vqip (dict): A dict VQIP of water to push that can be specified. Defaults to
            None, which returns maximum capacity to push.
        tag (str, optional):  optional message to direct the out_port's
            query_handler which function to call. Defaults to 'default'.

    Returns:
        (dict): An empty VQIP amount of water indicating no water can be pushed
    """
    return self.empty_vqip()

send_push_deny(vqip, tag='default', force=False)

Function used to deny any push requests.

Parameters:

Name Type Description Default
vqip dict

A dict VQIP of water to push

required
tag str

optional message to direct the out_port's query_handler which function to call. Defaults to 'default'.

'default'
force bool

Argument used to cause function to ignore tank capacity of out_port, possibly resulting in pooling. Should not be used unless out_port is a tank object. Defaults to False.

False

Returns:

Type Description
dict

A VQIP amount of water that was not successfully pushed

Source code in wsimod\arcs\arcs.py
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
def send_push_deny(self, vqip, tag="default", force=False):
    """Function used to deny any push requests.

    Args:
        vqip (dict): A dict VQIP of water to push
        tag (str, optional):  optional message to direct the out_port's
            query_handler which function to call. Defaults to 'default'.
        force (bool, optional): Argument used to cause function to ignore tank
            capacity of out_port, possibly resulting in pooling. Should not be used
            unless out_port is a tank object. Defaults to False.

    Returns:
        (dict): A VQIP amount of water that was not successfully pushed
    """
    return vqip

PushArc

Bases: Arc

Source code in wsimod\arcs\arcs.py
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
class PushArc(Arc):
    """"""

    def __init__(self, **kwargs):
        """Subclass of Arc where pushes return no availability to pull.

        This creates an Arc where only push requests/checks can be sent, similar to a
        CSO.
        """
        super().__init__(**kwargs)
        self.send_pull_request = self.send_pull_deny
        self.send_pull_check = self.send_pull_check_deny

    def send_pull_deny(self, vqip, tag="default", force=False):
        """Function used to deny any pull requests.

        Args:
            vqip (dict): A dict VQIP of water to pull
            tag (str, optional):  optional message to direct the out_port's
                query_handler which function to call. Defaults to 'default'.
            force (bool, optional): Argument used to cause function to ignore tank
                capacity of out_port, possibly resulting in pooling. Should not be used
                unless  out_port is a tank object. Defaults to False.

        Returns:
            (dict): A VQIP amount of water that was successfully pulled
        """
        return self.empty_vqip()

    def send_pull_check_deny(self, vqip=None, tag="default"):
        """Function used to deny any pull checks.

        Args:
            vqip (dict): A dict VQIP of water to pull that can be specified. Defaults to
                None, which returns maximum capacity to pull.
            tag (str, optional):  optional message to direct the out_port's
                query_handler which function to call. Defaults to 'default'.

        Returns:
            (dict): An empty VQIP amount of water indicating no water can be pulled
        """
        return self.empty_vqip()

__init__(**kwargs)

Subclass of Arc where pushes return no availability to pull.

This creates an Arc where only push requests/checks can be sent, similar to a CSO.

Source code in wsimod\arcs\arcs.py
855
856
857
858
859
860
861
862
863
def __init__(self, **kwargs):
    """Subclass of Arc where pushes return no availability to pull.

    This creates an Arc where only push requests/checks can be sent, similar to a
    CSO.
    """
    super().__init__(**kwargs)
    self.send_pull_request = self.send_pull_deny
    self.send_pull_check = self.send_pull_check_deny

send_pull_check_deny(vqip=None, tag='default')

Function used to deny any pull checks.

Parameters:

Name Type Description Default
vqip dict

A dict VQIP of water to pull that can be specified. Defaults to None, which returns maximum capacity to pull.

None
tag str

optional message to direct the out_port's query_handler which function to call. Defaults to 'default'.

'default'

Returns:

Type Description
dict

An empty VQIP amount of water indicating no water can be pulled

Source code in wsimod\arcs\arcs.py
881
882
883
884
885
886
887
888
889
890
891
892
893
def send_pull_check_deny(self, vqip=None, tag="default"):
    """Function used to deny any pull checks.

    Args:
        vqip (dict): A dict VQIP of water to pull that can be specified. Defaults to
            None, which returns maximum capacity to pull.
        tag (str, optional):  optional message to direct the out_port's
            query_handler which function to call. Defaults to 'default'.

    Returns:
        (dict): An empty VQIP amount of water indicating no water can be pulled
    """
    return self.empty_vqip()

send_pull_deny(vqip, tag='default', force=False)

Function used to deny any pull requests.

Parameters:

Name Type Description Default
vqip dict

A dict VQIP of water to pull

required
tag str

optional message to direct the out_port's query_handler which function to call. Defaults to 'default'.

'default'
force bool

Argument used to cause function to ignore tank capacity of out_port, possibly resulting in pooling. Should not be used unless out_port is a tank object. Defaults to False.

False

Returns:

Type Description
dict

A VQIP amount of water that was successfully pulled

Source code in wsimod\arcs\arcs.py
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
def send_pull_deny(self, vqip, tag="default", force=False):
    """Function used to deny any pull requests.

    Args:
        vqip (dict): A dict VQIP of water to pull
        tag (str, optional):  optional message to direct the out_port's
            query_handler which function to call. Defaults to 'default'.
        force (bool, optional): Argument used to cause function to ignore tank
            capacity of out_port, possibly resulting in pooling. Should not be used
            unless  out_port is a tank object. Defaults to False.

    Returns:
        (dict): A VQIP amount of water that was successfully pulled
    """
    return self.empty_vqip()

QueueArc

Bases: Arc

Source code in wsimod\arcs\arcs.py
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
class QueueArc(Arc):
    """"""

    def __init__(self, number_of_timesteps=0, **kwargs):
        """A queue arc that stores each push or pull individually in the queue. Enables
        implementation of travel time. A fixed number of timesteps can be specified as a
        parameter, and additional number of timesteps can be specified when the requests
        are made.

        The queue is a list of requests, where their travel time is decremented
        by 1 each timestep. Any requests with a travel time of 0 will be sent
        onwards if the 'update_queue' function is called.

        Args:
            number_of_timesteps (int, optional): Fixed number of timesteps that
                it takes to traverse the arc. Defaults to 0.
        """
        self.number_of_timesteps = number_of_timesteps
        self.queue = []
        super().__init__(**kwargs)

        self.queue_storage = self.empty_vqip()
        self.queue_storage_ = self.empty_vqip()

        self.mass_balance_ds.append(lambda: self.queue_arc_ds())

    def queue_arc_ds(self):
        """Calculate change in amount of water and other pollutants in the arc.

        Returns:
            (dict): A VQIP amount of change
        """
        self.queue_storage = self.queue_arc_sum()
        return self.extract_vqip(self.queue_storage, self.queue_storage_)

    def queue_arc_sum(self):
        """Sum the total water in the requests in the queue of the arc.

        Returns:
            (dict): A VQIP amount of water/pollutants in the arc
        """
        queue_storage = self.empty_vqip()
        for request in self.queue:
            queue_storage = self.sum_vqip(queue_storage, request["vqip"])
        return queue_storage

    def send_pull_request(self, vqip, tag="default", time=0):
        """Function used to transmit a pull request from one node (in_port) to another
        node (out_port). Any pulled water is immediately removed from the out_port and
        then takes the travel time to be received. This function has not been
        extensively tested.

        Args:
            vqip (_type_): A dict VQIP of water to pull (by default, only 'volume' key
                is used)
            tag (str, optional): optional message to direct the out_port's query_handler
                which function to call. Defaults to 'default'.
            time (int, optional): Travel time for request to spend in the arc (in
                addition to the arc's 'number_of_timesteps' parameter). Defaults to 0.

        Returns:
            (dict): A VQIP amount of water that was successfully pulled.
        """
        volume = vqip["volume"]
        # Apply pipe capacity
        excess_in = self.get_excess(direction="pull", vqip=vqip)["volume"]
        not_pulled = max(volume - excess_in, 0)
        volume -= not_pulled

        for pol in constants.ADDITIVE_POLLUTANTS:
            if pol in vqip.keys():
                vqip[pol] *= volume / vqip["volume"]

        vqip["volume"] = volume

        # Make pull
        vqip = self.in_port.pull_set(vqip)

        # Update to queue request
        request = {"time": time + self.number_of_timesteps, "vqip": vqip}

        # vqtip enters arc as a request
        self.enter_queue(request, direction="pull")

        # Update request queue and return pulls from queue
        reply = self.update_queue(direction="pull")
        return reply

    def send_push_request(self, vqip_, tag="default", force=False, time=0):
        """Function used to transmit a push request from one node (in_port) to another
        node (out_port).

        Args:
            vqip_ (dict): A dict VQIP of water to push.
            tag (str, optional): optional message to direct the out_port's query_handler
                which function to call. Defaults to 'default'.
            force (bool, optional): Ignore the capacity of the arc (note does not
                currently, pass the force argument to the out_port). Defaults to False.
            time (int, optional): Travel time for request to spend in the arc (in
                addition to the arc's 'number_of_timesteps' parameter). Defaults to 0.

        Returns:
            (dict): A VQIP amount of water that was not successfully pushed
        """
        vqip = self.copy_vqip(vqip_)

        if vqip["volume"] < constants.FLOAT_ACCURACY:
            return self.empty_vqip()

        # Apply pipe capacity
        if force:
            not_pushed = self.empty_vqip()
        else:
            excess_in = self.get_excess(direction="push", vqip=vqip, tag=tag)
            not_pushed = self.v_change_vqip(
                vqip, max(vqip["volume"] - excess_in["volume"], 0)
            )

        vqip = self.extract_vqip(vqip, not_pushed)

        # Update to queue request
        request = {"time": time + self.number_of_timesteps, "vqip": vqip}

        # vqtip enters arc as a request
        self.enter_queue(request, direction="push", tag=tag)

        # Update request queue
        backflow = self.update_queue(direction="push")
        not_pushed = self.sum_vqip(not_pushed, backflow)

        if backflow["volume"] > vqip_["volume"]:
            print("more backflow than vqip...")

        self.vqip_in = self.extract_vqip(self.vqip_in, backflow)

        return not_pushed

    def enter_arc(self, request, direction, tag):
        """Function used to cause format a request into the format expected by the
        enter_queue function.

        Args:
            request (dict): A dict with a VQIP under the 'vqip' key and the travel
                time under the 'time' key.
            direction (str): Direction of flow, can be 'push' or 'pull
            tag (str, optional):  optional message to direct the out_port's
                query_handler which function to call. Defaults to 'default'.

        Returns:
            (dict): The request dict with additional information entered for the queue.
        """
        request["average_flow"] = request["vqip"]["volume"] / (request["time"] + 1)
        request["direction"] = direction
        request["tag"] = tag

        self.flow_in += request["average_flow"]
        self.vqip_in = self.sum_vqip(self.vqip_in, request["vqip"])

        return request

    def enter_queue(self, request, direction=None, tag="default"):
        """Add a request into the arc's queue list.

        Args:
            request (dict): A dict with a VQIP under the 'vqip' key and the travel
                time under the 'time' key.
            direction (str): Direction of flow, can be 'push' or 'pull
            tag (str, optional):  optional message to direct the out_port's
                query_handler which function to call. Defaults to 'default'.
        """
        # Update inflows and format request
        request = self.enter_arc(request, direction, tag)

        # Enter queue
        self.queue.append(request)

    def update_queue(self, direction=None, backflow_enabled=True):
        """Iterate over all requests in the queue, removing them if they have no volume.

        If a request is a push and has 0 travel time remaining then
        the push will be triggered at the out_port, if the out_port responds that
        it cannot receive the push, then this water will be returned as backflow
        (if enabled).

        If a request is a pull and has 0 travel time remaining then it is simply summed
        with other 0 travel time pull_requests and returned (since the pull is made at
        the out_port when the send_pull_request is made).


        Args:
            direction (str, optional): Direction of flow, can be 'push' or 'pull.
                Defaults to None.
            backflow_enabled (bool, optional): Enable backflow, described above, if not
                enabled then the request will remain in the queue until all water has
                been received. Defaults to True.

        Returns:
            total_backflow (dict): In the case of a push direction, any backflow will be
                returned as a VQIP amount
            total_removed (dict): In the case of a pull direction, any pulled water will
                be returned as a VQIP amount
        """
        done_requests = []

        total_removed = self.empty_vqip()
        total_backflow = self.empty_vqip()
        # Iterate over requests
        for request in self.queue:
            if request["direction"] == direction:
                vqip = request["vqip"]

                if vqip["volume"] < constants.FLOAT_ACCURACY:
                    # Add to queue for removal
                    done_requests.append(request)
                elif request["time"] == 0:
                    if direction == "push":
                        # Attempt to push request
                        reply = self.out_port.push_set(vqip, request["tag"])
                        removed = vqip["volume"] - reply["volume"]

                    elif direction == "pull":
                        # Water has already been pulled, so assume all received
                        removed = vqip["volume"]

                    else:
                        print("No direction")

                    # Update outflows
                    self.flow_out += request["average_flow"] * removed / vqip["volume"]
                    vqip_ = self.v_change_vqip(vqip, removed)
                    total_removed = self.sum_vqip(total_removed, vqip_)

                    # Assume that any water that cannot arrive at destination this
                    # timestep is backflow
                    rejected = self.v_change_vqip(vqip, vqip["volume"] - removed)

                    if backflow_enabled | (
                        rejected["volume"] < constants.FLOAT_ACCURACY
                    ):
                        total_backflow = self.sum_vqip(rejected, total_backflow)
                        done_requests.append(request)
                    else:
                        request["vqip"] = rejected

        self.vqip_out = self.sum_vqip(self.vqip_out, total_removed)

        # Remove done requests
        for request in done_requests:
            self.queue.remove(request)

        # return total_removed
        if direction == "pull":
            return total_removed
        elif direction == "push":
            return total_backflow
        else:
            print("No direction")

    def end_timestep(self):
        """End timestep in an arc, resetting flow/vqip in/out (which determine) the
        capacity for that timestep.

        Update times of requests in the queue.
        """
        self.vqip_in = self.empty_vqip()
        self.vqip_out = self.empty_vqip()
        self.flow_in = 0
        self.flow_out = 0

        self.queue_storage_ = self.copy_vqip(self.queue_storage)
        self.queue_storage = self.empty_vqip()

        for request in self.queue:
            request["time"] = max(request["time"] - 1, 0)

        # TODO - update_queue here?

    def reinit(self):
        """"""
        self.end_timestep()
        self.queue = []

__init__(number_of_timesteps=0, **kwargs)

A queue arc that stores each push or pull individually in the queue. Enables implementation of travel time. A fixed number of timesteps can be specified as a parameter, and additional number of timesteps can be specified when the requests are made.

The queue is a list of requests, where their travel time is decremented by 1 each timestep. Any requests with a travel time of 0 will be sent onwards if the 'update_queue' function is called.

Parameters:

Name Type Description Default
number_of_timesteps int

Fixed number of timesteps that it takes to traverse the arc. Defaults to 0.

0
Source code in wsimod\arcs\arcs.py
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
def __init__(self, number_of_timesteps=0, **kwargs):
    """A queue arc that stores each push or pull individually in the queue. Enables
    implementation of travel time. A fixed number of timesteps can be specified as a
    parameter, and additional number of timesteps can be specified when the requests
    are made.

    The queue is a list of requests, where their travel time is decremented
    by 1 each timestep. Any requests with a travel time of 0 will be sent
    onwards if the 'update_queue' function is called.

    Args:
        number_of_timesteps (int, optional): Fixed number of timesteps that
            it takes to traverse the arc. Defaults to 0.
    """
    self.number_of_timesteps = number_of_timesteps
    self.queue = []
    super().__init__(**kwargs)

    self.queue_storage = self.empty_vqip()
    self.queue_storage_ = self.empty_vqip()

    self.mass_balance_ds.append(lambda: self.queue_arc_ds())

end_timestep()

End timestep in an arc, resetting flow/vqip in/out (which determine) the capacity for that timestep.

Update times of requests in the queue.

Source code in wsimod\arcs\arcs.py
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
def end_timestep(self):
    """End timestep in an arc, resetting flow/vqip in/out (which determine) the
    capacity for that timestep.

    Update times of requests in the queue.
    """
    self.vqip_in = self.empty_vqip()
    self.vqip_out = self.empty_vqip()
    self.flow_in = 0
    self.flow_out = 0

    self.queue_storage_ = self.copy_vqip(self.queue_storage)
    self.queue_storage = self.empty_vqip()

    for request in self.queue:
        request["time"] = max(request["time"] - 1, 0)

enter_arc(request, direction, tag)

Function used to cause format a request into the format expected by the enter_queue function.

Parameters:

Name Type Description Default
request dict

A dict with a VQIP under the 'vqip' key and the travel time under the 'time' key.

required
direction str

Direction of flow, can be 'push' or 'pull

required
tag str

optional message to direct the out_port's query_handler which function to call. Defaults to 'default'.

required

Returns:

Type Description
dict

The request dict with additional information entered for the queue.

Source code in wsimod\arcs\arcs.py
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
def enter_arc(self, request, direction, tag):
    """Function used to cause format a request into the format expected by the
    enter_queue function.

    Args:
        request (dict): A dict with a VQIP under the 'vqip' key and the travel
            time under the 'time' key.
        direction (str): Direction of flow, can be 'push' or 'pull
        tag (str, optional):  optional message to direct the out_port's
            query_handler which function to call. Defaults to 'default'.

    Returns:
        (dict): The request dict with additional information entered for the queue.
    """
    request["average_flow"] = request["vqip"]["volume"] / (request["time"] + 1)
    request["direction"] = direction
    request["tag"] = tag

    self.flow_in += request["average_flow"]
    self.vqip_in = self.sum_vqip(self.vqip_in, request["vqip"])

    return request

enter_queue(request, direction=None, tag='default')

Add a request into the arc's queue list.

Parameters:

Name Type Description Default
request dict

A dict with a VQIP under the 'vqip' key and the travel time under the 'time' key.

required
direction str

Direction of flow, can be 'push' or 'pull

None
tag str

optional message to direct the out_port's query_handler which function to call. Defaults to 'default'.

'default'
Source code in wsimod\arcs\arcs.py
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
def enter_queue(self, request, direction=None, tag="default"):
    """Add a request into the arc's queue list.

    Args:
        request (dict): A dict with a VQIP under the 'vqip' key and the travel
            time under the 'time' key.
        direction (str): Direction of flow, can be 'push' or 'pull
        tag (str, optional):  optional message to direct the out_port's
            query_handler which function to call. Defaults to 'default'.
    """
    # Update inflows and format request
    request = self.enter_arc(request, direction, tag)

    # Enter queue
    self.queue.append(request)

queue_arc_ds()

Calculate change in amount of water and other pollutants in the arc.

Returns:

Type Description
dict

A VQIP amount of change

Source code in wsimod\arcs\arcs.py
298
299
300
301
302
303
304
305
def queue_arc_ds(self):
    """Calculate change in amount of water and other pollutants in the arc.

    Returns:
        (dict): A VQIP amount of change
    """
    self.queue_storage = self.queue_arc_sum()
    return self.extract_vqip(self.queue_storage, self.queue_storage_)

queue_arc_sum()

Sum the total water in the requests in the queue of the arc.

Returns:

Type Description
dict

A VQIP amount of water/pollutants in the arc

Source code in wsimod\arcs\arcs.py
307
308
309
310
311
312
313
314
315
316
def queue_arc_sum(self):
    """Sum the total water in the requests in the queue of the arc.

    Returns:
        (dict): A VQIP amount of water/pollutants in the arc
    """
    queue_storage = self.empty_vqip()
    for request in self.queue:
        queue_storage = self.sum_vqip(queue_storage, request["vqip"])
    return queue_storage

reinit()

Source code in wsimod\arcs\arcs.py
549
550
551
552
def reinit(self):
    """"""
    self.end_timestep()
    self.queue = []

send_pull_request(vqip, tag='default', time=0)

Function used to transmit a pull request from one node (in_port) to another node (out_port). Any pulled water is immediately removed from the out_port and then takes the travel time to be received. This function has not been extensively tested.

Parameters:

Name Type Description Default
vqip _type_

A dict VQIP of water to pull (by default, only 'volume' key is used)

required
tag str

optional message to direct the out_port's query_handler which function to call. Defaults to 'default'.

'default'
time int

Travel time for request to spend in the arc (in addition to the arc's 'number_of_timesteps' parameter). Defaults to 0.

0

Returns:

Type Description
dict

A VQIP amount of water that was successfully pulled.

Source code in wsimod\arcs\arcs.py
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
def send_pull_request(self, vqip, tag="default", time=0):
    """Function used to transmit a pull request from one node (in_port) to another
    node (out_port). Any pulled water is immediately removed from the out_port and
    then takes the travel time to be received. This function has not been
    extensively tested.

    Args:
        vqip (_type_): A dict VQIP of water to pull (by default, only 'volume' key
            is used)
        tag (str, optional): optional message to direct the out_port's query_handler
            which function to call. Defaults to 'default'.
        time (int, optional): Travel time for request to spend in the arc (in
            addition to the arc's 'number_of_timesteps' parameter). Defaults to 0.

    Returns:
        (dict): A VQIP amount of water that was successfully pulled.
    """
    volume = vqip["volume"]
    # Apply pipe capacity
    excess_in = self.get_excess(direction="pull", vqip=vqip)["volume"]
    not_pulled = max(volume - excess_in, 0)
    volume -= not_pulled

    for pol in constants.ADDITIVE_POLLUTANTS:
        if pol in vqip.keys():
            vqip[pol] *= volume / vqip["volume"]

    vqip["volume"] = volume

    # Make pull
    vqip = self.in_port.pull_set(vqip)

    # Update to queue request
    request = {"time": time + self.number_of_timesteps, "vqip": vqip}

    # vqtip enters arc as a request
    self.enter_queue(request, direction="pull")

    # Update request queue and return pulls from queue
    reply = self.update_queue(direction="pull")
    return reply

send_push_request(vqip_, tag='default', force=False, time=0)

Function used to transmit a push request from one node (in_port) to another node (out_port).

Parameters:

Name Type Description Default
vqip_ dict

A dict VQIP of water to push.

required
tag str

optional message to direct the out_port's query_handler which function to call. Defaults to 'default'.

'default'
force bool

Ignore the capacity of the arc (note does not currently, pass the force argument to the out_port). Defaults to False.

False
time int

Travel time for request to spend in the arc (in addition to the arc's 'number_of_timesteps' parameter). Defaults to 0.

0

Returns:

Type Description
dict

A VQIP amount of water that was not successfully pushed

Source code in wsimod\arcs\arcs.py
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
def send_push_request(self, vqip_, tag="default", force=False, time=0):
    """Function used to transmit a push request from one node (in_port) to another
    node (out_port).

    Args:
        vqip_ (dict): A dict VQIP of water to push.
        tag (str, optional): optional message to direct the out_port's query_handler
            which function to call. Defaults to 'default'.
        force (bool, optional): Ignore the capacity of the arc (note does not
            currently, pass the force argument to the out_port). Defaults to False.
        time (int, optional): Travel time for request to spend in the arc (in
            addition to the arc's 'number_of_timesteps' parameter). Defaults to 0.

    Returns:
        (dict): A VQIP amount of water that was not successfully pushed
    """
    vqip = self.copy_vqip(vqip_)

    if vqip["volume"] < constants.FLOAT_ACCURACY:
        return self.empty_vqip()

    # Apply pipe capacity
    if force:
        not_pushed = self.empty_vqip()
    else:
        excess_in = self.get_excess(direction="push", vqip=vqip, tag=tag)
        not_pushed = self.v_change_vqip(
            vqip, max(vqip["volume"] - excess_in["volume"], 0)
        )

    vqip = self.extract_vqip(vqip, not_pushed)

    # Update to queue request
    request = {"time": time + self.number_of_timesteps, "vqip": vqip}

    # vqtip enters arc as a request
    self.enter_queue(request, direction="push", tag=tag)

    # Update request queue
    backflow = self.update_queue(direction="push")
    not_pushed = self.sum_vqip(not_pushed, backflow)

    if backflow["volume"] > vqip_["volume"]:
        print("more backflow than vqip...")

    self.vqip_in = self.extract_vqip(self.vqip_in, backflow)

    return not_pushed

update_queue(direction=None, backflow_enabled=True)

Iterate over all requests in the queue, removing them if they have no volume.

If a request is a push and has 0 travel time remaining then the push will be triggered at the out_port, if the out_port responds that it cannot receive the push, then this water will be returned as backflow (if enabled).

If a request is a pull and has 0 travel time remaining then it is simply summed with other 0 travel time pull_requests and returned (since the pull is made at the out_port when the send_pull_request is made).

Parameters:

Name Type Description Default
direction str

Direction of flow, can be 'push' or 'pull. Defaults to None.

None
backflow_enabled bool

Enable backflow, described above, if not enabled then the request will remain in the queue until all water has been received. Defaults to True.

True

Returns:

Name Type Description
total_backflow dict

In the case of a push direction, any backflow will be returned as a VQIP amount

total_removed dict

In the case of a pull direction, any pulled water will be returned as a VQIP amount

Source code in wsimod\arcs\arcs.py
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
def update_queue(self, direction=None, backflow_enabled=True):
    """Iterate over all requests in the queue, removing them if they have no volume.

    If a request is a push and has 0 travel time remaining then
    the push will be triggered at the out_port, if the out_port responds that
    it cannot receive the push, then this water will be returned as backflow
    (if enabled).

    If a request is a pull and has 0 travel time remaining then it is simply summed
    with other 0 travel time pull_requests and returned (since the pull is made at
    the out_port when the send_pull_request is made).


    Args:
        direction (str, optional): Direction of flow, can be 'push' or 'pull.
            Defaults to None.
        backflow_enabled (bool, optional): Enable backflow, described above, if not
            enabled then the request will remain in the queue until all water has
            been received. Defaults to True.

    Returns:
        total_backflow (dict): In the case of a push direction, any backflow will be
            returned as a VQIP amount
        total_removed (dict): In the case of a pull direction, any pulled water will
            be returned as a VQIP amount
    """
    done_requests = []

    total_removed = self.empty_vqip()
    total_backflow = self.empty_vqip()
    # Iterate over requests
    for request in self.queue:
        if request["direction"] == direction:
            vqip = request["vqip"]

            if vqip["volume"] < constants.FLOAT_ACCURACY:
                # Add to queue for removal
                done_requests.append(request)
            elif request["time"] == 0:
                if direction == "push":
                    # Attempt to push request
                    reply = self.out_port.push_set(vqip, request["tag"])
                    removed = vqip["volume"] - reply["volume"]

                elif direction == "pull":
                    # Water has already been pulled, so assume all received
                    removed = vqip["volume"]

                else:
                    print("No direction")

                # Update outflows
                self.flow_out += request["average_flow"] * removed / vqip["volume"]
                vqip_ = self.v_change_vqip(vqip, removed)
                total_removed = self.sum_vqip(total_removed, vqip_)

                # Assume that any water that cannot arrive at destination this
                # timestep is backflow
                rejected = self.v_change_vqip(vqip, vqip["volume"] - removed)

                if backflow_enabled | (
                    rejected["volume"] < constants.FLOAT_ACCURACY
                ):
                    total_backflow = self.sum_vqip(rejected, total_backflow)
                    done_requests.append(request)
                else:
                    request["vqip"] = rejected

    self.vqip_out = self.sum_vqip(self.vqip_out, total_removed)

    # Remove done requests
    for request in done_requests:
        self.queue.remove(request)

    # return total_removed
    if direction == "pull":
        return total_removed
    elif direction == "push":
        return total_backflow
    else:
        print("No direction")

SewerArc

Bases: Arc

Source code in wsimod\arcs\arcs.py
896
897
898
899
class SewerArc(Arc):
    """"""

    pass

WeirArc

Bases: SewerArc

Source code in wsimod\arcs\arcs.py
902
903
904
905
class WeirArc(SewerArc):
    """"""

    pass