Prado EFO PVA
network.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 """
3 Created on Tue May 19 07:25:09 2020
4 
5 @author: cd
6 """
7 
8 
9 import abc
10 import numpy as np
11 from efo.time import TimeBase, TimeFcst
12 from efo.junction import ReservoirJunction
13 import concurrent.futures
14 
15 
16 class NetworkBase(metaclass=abc.ABCMeta):
17  # TODO: May not need constants
18  def __init__(self, name, time, junctions):
19  self.namename = name
20  self.junctionsjunctions = junctions
21  self.nJunctionsnJunctions = len(junctions)
22  # self.compQvals = compQvals
23  # TODO: Should use error handling instead
24  if issubclass(type(time), TimeBase):
25  self.TT = time
26  super().__init__()
27 
28  # TODO: May want to override the iterate function for this
29  # TODO: This could be lambda expression so it could be multi-purpose
30  @abc.abstractmethod
31  def process_junctions(self):
32  pass
33 
34  @classmethod
35  def __subclasshook__(cls, C):
36  if cls is NetworkBase:
37  attrs = set(dir(C))
38  if set(cls.__abstractmethods__) <= attrs:
39  return True
40  return NotImplemented
41 
42 
44  def __init__(self, name, time, junctions):
45  # Call super class constructor
46  super().__init__(name, time, junctions)
47 
48  def process_junctions(self):
49  qOutReturn = np.empty(self.nJunctionsnJunctions)
50  storReturn = np.empty(self.nJunctionsnJunctions)
51  storReturn[:] = np.nan
52  for i, curJnc in enumerate(self.junctionsjunctions):
53  qOutReturn[i] = curJnc.calc_qout()
54  if issubclass(type(curJnc), ReservoirJunction):
55  storReturn[i] = curJnc.stor[self.TT.step]
56  # return (qOutReturn, storReturn)
57 
58 
59 # This provides a link between your network and your forecast network
61  def __init__(self, name, timeFcst, junctions, fcstJunctions):
62  # Call super class constructor
63  # TODO: Need to make sure that time is type timeFcst
64  # TODO: fcstJunctions and junctions should be a linked list
65  super().__init__(name, timeFcst, junctions)
66  # Sub class properties
67  # TODO: This is temporary until we can create a build method
68  # self.fcstJunctions = fcstJunctions
69  self.fcstNetworkfcstNetwork = Network('fcstNework', timeFcst, fcstJunctions)
70  # TODO: Would be good to use some sort of mapping object to link junctions
71  # TODO: Seems like python linked lists might be good for this
72 
74  # TODO: This will build forecast juctions from regular juctions using shallow (maybe deep) copy methods
75  # TODO: Will have to redefine qIn objects with the passed forecasted flows
76  # TODO: Will have have to also shallow copy reaches
77  # TODO: Might be able to deep copy and then redefine everything with setter methods
78  pass
79 
80  def set_init_cond(self):
81  # TODO: To start just going to initialize qOut to zero
82  # TODO: Junctions should have some sort of setter method for this
83  for i in range(len(self.fcstNetwork.junctions)):
84  self.fcstNetwork.junctions[i].qOut[0] = self.junctions[i].qOut[max(0, self.T.Tcont.step-1)]
85  # if issubclass(type(curJnc), ReservoirJunction):
86  if issubclass(type(self.fcstNetwork.junctions[i]), ReservoirJunction):
87  self.fcstNetwork.junctions[i].stor[0] = \
88  self.junctions[i].stor[max(0, self.T.Tcont.step-1)]
89  self.fcstNetwork.junctions[i].rlsCtrl[0] = \
90  self.junctions[i].rlsCtrl[max(0, self.T.Tcont.step-1)]
91  if self.fcstNetwork.junctions[i].outletUnCtrl:
92  self.fcstNetwork.junctions[i].rlsUnCtrl[0] = \
93  self.junctions[i].rlsUnCtrl[max(0, self.T.Tcont.step-1)]
94 
96  self.fcstNetworkfcstNetwork.process_junctions()
97  # with concurrent.futures.ProcessPoolExecutor() as executor:
98  # # for i, curPrime in enumerate(executor.map(self.task.is_prime, self.primes)):
99  # # isPrime[i]=curPrime
100  # # print('%d is prime: %s' % (PRIMES[i], isPrime[i]))
101  # executor.map
102 
103  def get_storage(self):
104  pass
105 
106 
107 # TODO: Not sure we need an EFO of networks
108 # TODO: Efo network will keep track of the efo res and other junctions
109 # This provides a link between your network and your forecast network
110 # class NetworkEfo(NetworkBase):
111 # def __init__(self, name, timeFcst, junctions, fcstNetworks):
112 # # Call super class constructor
113 # super().__init__(name, name, timeFcst, junctions)
114 # # Sub class properties
115 # # TODO: This is temporary until we can create a build method
116 # self.fcstNetworks = fcstNetworks
117 
118 # def build_fcst_networks(self):
119 # # TODO: This should build an ensemble of forecst networks with passed hindcasts and junctions
120 # # TODO: For reservoirs this would have to assume not uncontrolled spillway
121 # # TODO: Since your forecast reservoirs and your simulation reservoir assumptiong
122 # # may be different you may not be able to use a generalized function like this
123 
124 
125 
126 
127 
128 
def __subclasshook__(cls, C)
Definition: network.py:35
def __init__(self, name, time, junctions)
Definition: network.py:18
def process_junctions(self)
Definition: network.py:31
def set_init_cond(self)
Definition: network.py:80
def get_storage(self)
Definition: network.py:103
def __init__(self, name, timeFcst, junctions, fcstJunctions)
Definition: network.py:61
def process_fcst_junctions(self)
Definition: network.py:95
def build_fcst_junctions(self)
Definition: network.py:73
def __init__(self, name, time, junctions)
Definition: network.py:44
def process_junctions(self)
Definition: network.py:48