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():
""" 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)
model.step()
assert_allclose(otpt.flow, [0], atol=1e-7)
|---> 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)
mrfA.connect(waste)
mrfB.connect(waste)
river2.connect(pumping_station)
pumping_station.connect(reservoir)
reservoir.connect(compensation)
compensation.connect(river1)
reservoir.connect(demand)
model.check()
model.setup()
| ^
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)
mrfA.connect(waste)
mrfB.connect(waste)
river2.connect(pumping_station)
pumping_station.connect(reservoir)
def test_empty_storage_min_flow():
model = Model()
storage = Storage(model, "storage", initial_volume=100, max_volume=100, num_inputs=1, num_outputs=0)
otpt = Output(model, "output", min_flow=75)
storage.connect(otpt)
model.check()
model.step()
with pytest.raises(RuntimeError):
model.step()
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):
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])
def __init__(self, model, name, **kwargs):
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)
def __init__(self, *args, **kwargs):
"""Initialise a new Output node
Parameters
----------
min_flow : float (optional)
A simple minimum flow constraint for the output. Defaults to 0.0
max_flow : float (optional)
A simple maximum flow constraint for the output. Defaults to None
"""
kwargs['color'] = kwargs.pop('color', '#FFF467') # light yellow
super(Output, self).__init__(*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])