Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_virtual_storage_duplicate_route():
""" Test the VirtualStorage node """
model = pywr.core.Model()
inpt = Input(model, "Input", max_flow=20)
lnk = Link(model, "Link")
inpt.connect(lnk)
otpt = Output(model, "Output", max_flow=10, cost=-10.0)
lnk.connect(otpt)
vs = pywr.core.VirtualStorage(model, "Licence", [lnk, otpt], factors=[0.5, 1.0], initial_volume=10.0, max_volume=10.0)
model.setup()
assert_allclose(vs.volume, [10], atol=1e-7)
model.step()
assert_allclose(otpt.flow, [10/1.5], atol=1e-7)
assert_allclose(vs.volume, [0], atol=1e-7)
def test_virtual_storage():
""" Test the VirtualStorage node """
model = pywr.core.Model()
inpt = Input(model, "Input", max_flow=20)
lnk = Link(model, "Link")
inpt.connect(lnk)
otpt = Output(model, "Output", max_flow=10, cost=-10.0)
lnk.connect(otpt)
vs = pywr.core.VirtualStorage(model, "Licence", [lnk], initial_volume=10.0, max_volume=10.0)
model.setup()
assert_allclose(vs.volume, [10], atol=1e-7)
model.step()
assert_allclose(otpt.flow, [10], atol=1e-7)
assert_allclose(vs.volume, [0], atol=1e-7)
Demand
^
|
Reservoir <- Pumping
| ^
v |
Compensation |
| |
v |
Catchment -> River 1 -> River 2 ----> MRFA -> Waste
| ^
|---> MRFB ----|
"""
model = Model()
catchment = Input(model, "catchment", max_flow=500, min_flow=500)
reservoir = Storage(model, "reservoir", max_volume=10000, initial_volume=5000)
demand = Output(model, "demand", max_flow=50, cost=-100)
pumping_station = Link(model, "pumping station", max_flow=100, cost=-10)
river1 = Link(model, "river1")
river2 = Link(model, "river2")
compensation = Link(model, "compensation", cost=600)
mrfA = Link(model, "mrfA", cost=-500, max_flow=50)
mrfB = Link(model, "mrfB")
waste = Output(model, "waste")
catchment.connect(river1)
river1.connect(river2)
river2.connect(mrfA)
river2.connect(mrfB)
def create_model():
# create a model
model = Model(start="2016-01-01", end="2019-12-31", timestep=7)
# create three nodes (an input, a link, and an output)
A = Input(model, name="A", max_flow=10.0)
B = Link(model, name="B", cost=10.0)
C = Output(model, name="C", max_flow=5.0, cost=-20.0)
# connect the nodes together
A.connect(B)
B.connect(C)
return model
def __init__(self, *args, **kwargs):
"""Initialise a new Input node
Parameters
----------
min_flow : float (optional)
A simple minimum flow constraint for the input. Defaults to None
max_flow : float (optional)
A simple maximum flow constraint for the input. Defaults to 0.0
"""
super(Input, self).__init__(*args, **kwargs)
self.color = '#F26C4F' # light red
if extra_slots+1 != len(self.slot_names):
raise ValueError("slot_names must be one more than the number of extra_slots.")
factors = kwargs.pop('factors', None)
# Finally initialise the parent.
super(MultiSplitLink, self).__init__(*args, **kwargs)
self._extra_inputs = []
self._extra_outputs = []
n = len(self.sublinks) - extra_slots
for i in range(extra_slots):
# create a new input inside the piecewise link which only has access
# to flow travelling via the last sublink (X2)
otpt = Output(self.model, '{} Extra Output {}'.format(self.name, i),
domain=self.sub_domain, parent=self)
inpt = Input(self.model, '{} Extra Input {}'.format(self.name, i), parent=self)
otpt.connect(inpt)
self.sublinks[n+i].connect(otpt)
self._extra_inputs.append(inpt)
self._extra_outputs.append(otpt)
# Now create an aggregated node for addition constaints if required.
if factors is not None:
if extra_slots+1 != len(factors):
raise ValueError("factors must have a length equal to extra_slots.")
nodes = []
valid_factors = []
for r, nd in zip(factors, self.sublinks[n-1:]):
if r is not None:
from pywr.nodes import Node, Domain, Input, Output, Link, Storage, PiecewiseLink, MultiSplitLink
from pywr.parameters import pop_kwarg_parameter, ConstantParameter, Parameter, load_parameter
from pywr.parameters.control_curves import ControlCurveParameter
DEFAULT_RIVER_DOMAIN = Domain(name='river', color='#33CCFF')
class RiverDomainMixin(object):
def __init__(self, *args, **kwargs):
# if 'domain' not in kwargs:
# kwargs['domain'] = DEFAULT_RIVER_DOMAIN
if 'color' not in kwargs:
self.color = '#6ECFF6' # blue
super(RiverDomainMixin, self).__init__(*args, **kwargs)
class Catchment(RiverDomainMixin, Input):
"""A hydrological catchment, supplying water to the river network"""
def __init__(self, *args, **kwargs):
"""Initialise a new Catchment node.
A Catchment is an input node with a fixed inflow. I.e. min_flow and
max_flow are the same. The value is specified as a flow keyword, and
overrides any min_flow or max_flow keyword arguments.
Parameters
----------
flow : float or function
The amount of water supplied by the catchment each timestep
"""
self.color = '#82CA9D' # green
# Grab flow from kwargs
flow = kwargs.pop('flow', 0.0)
def __init__(self, *args, **kwargs):
self.allow_isolated = True
costs = kwargs.pop('cost')
max_flows = kwargs.pop('max_flow')
super(PiecewiseLink, self).__init__(*args, **kwargs)
if len(costs) != len(max_flows):
raise ValueError("Piecewise max_flow and cost keywords must be the same length.")
# TODO look at the application of Domains here. Having to use
# Input/Output instead of BaseInput/BaseOutput because of a different
# domain is required on the sub-nodes and they need to be connected
self.sub_domain = Domain()
self.input = Input(self.model, name='{} Input'.format(self.name), parent=self)
self.output = Output(self.model, name='{} Output'.format(self.name), parent=self)
self.sub_output = Output(self.model, name='{} Sub Output'.format(self.name), parent=self,
domain=self.sub_domain)
self.sub_output.connect(self.input)
self.sublinks = []
for max_flow, cost in zip(max_flows, costs):
self.sublinks.append(Input(self.model, name='{} Sublink {}'.format(self.name, len(self.sublinks)),
cost=cost, max_flow=max_flow, parent=self, domain=self.sub_domain))
self.sublinks[-1].connect(self.sub_output)
self.output.connect(self.sublinks[-1])
self.allow_isolated = True
output_name = "{} Output".format(name)
input_name = "{} Input".format(name)
param_name = "{} - delay parameter".format(name)
assert(output_name not in model.nodes)
assert(input_name not in model.nodes)
assert(param_name not in model.parameters)
days = kwargs.pop('days', 0)
timesteps = kwargs.pop('timesteps', 0)
initial_flow = kwargs.pop('initial_flow', 0.0)
self.output = Output(model, name=output_name)
self.delay_param = FlowDelayParameter(model, self.output, timesteps=timesteps, days=days,
initial_flow=initial_flow, name=param_name)
self.input = Input(model, name=input_name, min_flow=self.delay_param, max_flow=self.delay_param)
super().__init__(model, name, **kwargs)