Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def simple_piecewise_model(request):
"""
Make a simple model with a single Input and Output and PiecewiseLink
Input -> PiecewiseLink -> Output
"""
in_flow, out_flow, benefit = request.param
min_flow_req = 5.0
model = pywr.core.Model()
inpt = pywr.core.Input(model, name="Input", max_flow=in_flow)
lnk = pywr.core.PiecewiseLink(model, name="Link", cost=[-1.0, 0.0], max_flow=[min_flow_req, None])
inpt.connect(lnk)
otpt = pywr.core.Output(model, name="Output", min_flow=out_flow, cost=-benefit)
lnk.connect(otpt)
default = inpt.domain
expected_sent = in_flow if benefit > 1.0 else out_flow
expected_node_results = {
"Input": expected_sent,
"Link": expected_sent,
"Link Sublink 0": min(min_flow_req, expected_sent),
"Link Sublink 1": expected_sent - min(min_flow_req, expected_sent),
Use a simple model of a Reservoir to test that a control curve
behaves as expected.
The control curve should alter the cost of the Reservoir when it
is above or below a particular threshold.
(flow = 8.0) (max_flow = 10.0)
Catchment -> River -> DemandCentre
| ^
(max_flow = 2.0) v | (max_flow = 2.0)
Reservoir
"""
in_flow = 8
model = pywr.core.Model()
catchment = river.Catchment(model, name="Catchment", flow=in_flow)
lnk = river.River(model, name="River")
catchment.connect(lnk)
demand = pywr.core.Output(model, name="Demand", cost=-10.0, max_flow=10)
lnk.connect(demand)
from pywr.parameters import ConstantParameter
control_curve = ConstantParameter(model, 0.8)
reservoir = river.Reservoir(model, name="Reservoir", max_volume=10, cost=-20, above_curve_cost=0.0,
control_curve=control_curve, initial_volume=10)
reservoir.inputs[0].max_flow = 2.0
reservoir.outputs[0].max_flow = 2.0
lnk.connect(reservoir)
reservoir.connect(demand)
model.setup()
def simple_storage_model(request):
"""
Make a simple model with a single Input, Storage and Output.
Input -> Storage -> Output
"""
model = pywr.core.Model(
start=pandas.to_datetime('2016-01-01'),
end=pandas.to_datetime('2016-01-05'),
timestep=datetime.timedelta(1),
)
inpt = Input(model, name="Input", max_flow=5.0, cost=-1)
res = Storage(model, name="Storage", num_outputs=1, num_inputs=1, max_volume=20, initial_volume=10)
otpt = Output(model, name="Output", max_flow=8, cost=-999)
inpt.connect(res)
res.connect(otpt)
return model
"""
model = pywr.core.Model()
S = supply_amplitude
w = frequency
def supply_func(parent, index):
t = parent.model.timestamp.timetuple().tm_yday
return S*np.cos(t*w)+S
supply = pywr.core.Supply(model, name='supply', max_flow=supply_func)
demand = pywr.core.Demand(model, name='demand', demand=demand)
res = pywr.core.Reservoir(model, name='reservoir')
res.properties['max_volume'] = pywr.parameters.ParameterConstant(1e6)
res.properties['current_volume'] = pywr.core.Variable(initial_volume)
supply_res_link = pywr.core.Link(model, name='link1')
res_demand_link = pywr.core.Link(model, name='link2')
supply.connect(supply_res_link)
supply_res_link.connect(res)
res.connect(res_demand_link)
res_demand_link.connect(demand)
return model
supply is a annual cosine function with amplitude supply_amplitude and
frequency
"""
model = pywr.core.Model()
S = supply_amplitude
w = frequency
def supply_func(parent, index):
t = parent.model.timestamp.timetuple().tm_yday
return S*np.cos(t*w)+S
supply = pywr.core.Supply(model, name='supply', max_flow=supply_func)
demand = pywr.core.Demand(model, name='demand', demand=demand)
res = pywr.core.Reservoir(model, name='reservoir')
res.properties['max_volume'] = pywr.parameters.ParameterConstant(1e6)
res.properties['current_volume'] = pywr.core.Variable(initial_volume)
supply_res_link = pywr.core.Link(model, name='link1')
res_demand_link = pywr.core.Link(model, name='link2')
supply.connect(supply_res_link)
supply_res_link.connect(res)
res.connect(res_demand_link)
res_demand_link.connect(demand)
return model
frequency
"""
model = pywr.core.Model()
S = supply_amplitude
w = frequency
def supply_func(parent, index):
t = parent.model.timestamp.timetuple().tm_yday
return S*np.cos(t*w)+S
supply = pywr.core.Supply(model, name='supply', max_flow=supply_func)
demand = pywr.core.Demand(model, name='demand', demand=demand)
res = pywr.core.Reservoir(model, name='reservoir')
res.properties['max_volume'] = pywr.parameters.ParameterConstant(1e6)
res.properties['current_volume'] = pywr.core.Variable(initial_volume)
supply_res_link = pywr.core.Link(model, name='link1')
res_demand_link = pywr.core.Link(model, name='link2')
supply.connect(supply_res_link)
supply_res_link.connect(res)
res.connect(res_demand_link)
res_demand_link.connect(demand)
return model
def parse_xml(data):
'''Create a new Model from XML data'''
root = ET.fromstring(data)
# parse solver
xml_solver = root.find('solver')
if xml_solver is not None:
solver = xml_solver.get('name')
else:
solver = None
model = pywr.core.Model(solver=solver)
# parse metadata
xml_metadatas = root.find('metadata')
for xml_metadata in xml_metadatas.getchildren():
tag = xml_metadata.tag.lower()
text = xml_metadata.text.strip()
model.metadata[tag] = text
# parse parameters
xml_parameters = root.find('parameters')
if xml_parameters:
for xml_parameter in xml_parameters.getchildren():
tag = xml_parameter.tag.lower()
if tag == 'parameter':
key = xml_parameter.get('key')
parameter_type = xml_parameter.get('type')
def __init__(self, edge, *args, **kwargs):
self.edge = edge
super(Edge, self).__init__(*args, **kwargs)
self.setZValue(-1)
node1, node2 = self.edge
# style
pen = QtGui.QPen()
pen.setWidth(2)
# TODO: this is crude - use an attribute on the node instead?
if(isinstance(node1, (core.River, core.Catchment, core.Terminator)) \
and isinstance(node2, (core.River, core.Catchment, core.Terminator))):
color = '#0892D0' # blue
else:
color = '#000'
pen.setColor(color)
self.setPen(pen)
# position
self.refresh()
def refresh(self):