-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
1342 lines (1135 loc) · 45.7 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
270
271
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
553
554
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
666
667
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
729
730
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
806
807
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
850
851
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""Created on Mon Jul 4 16:01:48 2022.
@author: bdobson
"""
import csv
import gzip
import inspect
import os
import sys
from datetime import datetime
from math import log10
import dill as pickle
import yaml
from tqdm import tqdm
from wsimod.arcs import arcs as arcs_mod
from wsimod.core import constants
from wsimod.core.core import WSIObj
from wsimod.nodes.land import ImperviousSurface
from wsimod.nodes.nodes import NODES_REGISTRY
from wsimod.nodes.tanks import QueueTank, ResidenceTank, Tank
os.environ["USE_PYGEOS"] = "0"
class to_datetime:
""""""
# TODO document and make better
def __init__(self, date_string):
"""Simple datetime wrapper that has key properties used in WSIMOD components.
Args:
date_string (str): A string containing the date, expected in
format %Y-%m-%d or %Y-%m.
"""
self._date = self._parse_date(date_string)
def __str__(self):
return self._date.strftime("%Y-%m-%d")
def __repr__(self):
return self._date.strftime("%Y-%m-%d")
@property
def dayofyear(self):
"""
Returns:
"""
return self._date.timetuple().tm_yday
@property
def day(self):
"""
Returns:
"""
return self._date.day
@property
def year(self):
"""
Returns:
"""
return self._date.year
@property
def month(self):
"""
Returns:
"""
return self._date.month
def to_period(self, args="M"):
"""
Args:
args:
Returns:
"""
return to_datetime(f"{self._date.year}-{str(self._date.month).zfill(2)}")
def is_leap_year(self):
"""
Returns:
"""
year = self._date.year
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
def _parse_date(self, date_string, date_format="%Y-%m-%d %H:%M:%S"):
try:
return datetime.strptime(date_string, date_format)
except ValueError:
try:
return datetime.strptime(date_string, "%Y-%m-%d")
except ValueError:
try:
# Check if valid 'YYYY-MM' format
if len(date_string.split("-")[0]) == 4:
int(date_string.split("-")[0])
if len(date_string.split("-")[1]) == 2:
int(date_string.split("-")[1])
return date_string
except ValueError:
raise ValueError
def __eq__(self, other):
if isinstance(other, to_datetime):
return self._date == other._date
return False
def __hash__(self):
return hash(self._date)
class Model(WSIObj):
""""""
def __init__(self):
"""Object to contain nodes and arcs that provides a default orchestration.
Returns:
Model: An empty model object
"""
super().__init__()
self.arcs = {}
# self.arcs_type = {} #not sure that this would be necessary
self.nodes = {}
self.nodes_type = {}
self.extensions = []
self.river_discharge_order = []
# Default orchestration
self.orchestration = [
{"FWTW": "treat_water"},
{"Demand": "create_demand"},
{"Land": "run"},
{"Groundwater": "infiltrate"},
{"Sewer": "make_discharge"},
{"Foul": "make_discharge"},
{"WWTW": "calculate_discharge"},
{"Groundwater": "distribute"},
{"River": "calculate_discharge"},
{"Reservoir": "make_abstractions"},
{"Land": "apply_irrigation"},
{"WWTW": "make_discharge"},
{"Catchment": "route"},
]
def get_init_args(self, cls):
"""Get the arguments of the __init__ method for a class and its superclasses."""
init_args = []
for c in cls.__mro__:
# Get the arguments of the __init__ method
args = inspect.getfullargspec(c.__init__).args[1:]
init_args.extend(args)
return init_args
def load(self, address, config_name="config.yml", overrides={}):
"""
Args:
address:
config_name:
overrides:
"""
from ..extensions import apply_patches
with open(os.path.join(address, config_name), "r") as file:
data: dict = yaml.safe_load(file)
for key, item in overrides.items():
data[key] = item
constants.POLLUTANTS = data.get("pollutants", constants.POLLUTANTS)
constants.ADDITIVE_POLLUTANTS = data.get(
"additive_pollutants", constants.ADDITIVE_POLLUTANTS
)
constants.NON_ADDITIVE_POLLUTANTS = data.get(
"non_additive_pollutants", constants.NON_ADDITIVE_POLLUTANTS
)
constants.FLOAT_ACCURACY = float(
data.get("float_accuracy", constants.FLOAT_ACCURACY)
)
self.__dict__.update(Model().__dict__)
"""
FLAG:
E.G. ADDITION FOR NEW ORCHESTRATION
"""
load_extension_files(data.get("extensions", []))
self.extensions = data.get("extensions", [])
if "orchestration" in data.keys():
# Update orchestration
self.orchestration = data["orchestration"]
if "nodes" not in data.keys():
raise ValueError("No nodes found in the config")
nodes = data["nodes"]
for name, node in nodes.items():
if "filename" in node.keys():
node["data_input_dict"] = read_csv(
os.path.join(address, node["filename"])
)
del node["filename"]
if "surfaces" in node.keys():
for key, surface in node["surfaces"].items():
if "filename" in surface.keys():
node["surfaces"][key]["data_input_dict"] = read_csv(
os.path.join(address, surface["filename"])
)
del surface["filename"]
node["surfaces"] = list(node["surfaces"].values())
arcs = data.get("arcs", {})
self.add_nodes(list(nodes.values()))
self.add_arcs(list(arcs.values()))
self.add_overrides(data.get("overrides", {}))
if "dates" in data.keys():
self.dates = [to_datetime(x) for x in data["dates"]]
apply_patches(self)
def save(self, address, config_name="config.yml", compress=False):
"""Save the model object to a yaml file and input data to csv.gz format in the
directory specified.
Args:
address (str): Path to a directory
config_name (str, optional): Name of yaml model file.
Defaults to 'model.yml'
"""
if not os.path.exists(address):
os.mkdir(address)
nodes = {}
if compress:
file_type = "csv.gz"
else:
file_type = "csv"
for node in self.nodes.values():
init_args = self.get_init_args(node.__class__)
special_args = set(["surfaces", "parent", "data_input_dict"])
node_props = {
x: getattr(node, x) for x in set(init_args).difference(special_args)
}
node_props["type_"] = node.__class__.__name__
node_props["node_type_override"] = (
repr(node.__class__).split(".")[-1].replace("'>", "")
)
if "surfaces" in init_args:
surfaces = {}
for surface in node.surfaces:
surface_args = self.get_init_args(surface.__class__)
surface_props = {
x: getattr(surface, x)
for x in set(surface_args).difference(special_args)
}
surface_props["type_"] = surface.__class__.__name__
# Exceptions...
# TODO I need a better way to do this
del surface_props["capacity"]
if set(["rooting_depth", "pore_depth"]).intersection(surface_args):
del surface_props["depth"]
if "data_input_dict" in surface_args:
if surface.data_input_dict:
filename = (
"{0}-{1}-inputs.{2}".format(
node.name, surface.surface, file_type
)
.replace("(", "_")
.replace(")", "_")
.replace("/", "_")
.replace(" ", "_")
)
write_csv(
surface.data_input_dict,
{"node": node.name, "surface": surface.surface},
os.path.join(address, filename),
compress=compress,
)
surface_props["filename"] = filename
surfaces[surface_props["surface"]] = surface_props
node_props["surfaces"] = surfaces
if "data_input_dict" in init_args:
if node.data_input_dict:
filename = "{0}-inputs.{1}".format(node.name, file_type)
write_csv(
node.data_input_dict,
{"node": node.name},
os.path.join(address, filename),
compress=compress,
)
node_props["filename"] = filename
nodes[node.name] = node_props
arcs = {}
for arc in self.arcs.values():
init_args = self.get_init_args(arc.__class__)
special_args = set(["in_port", "out_port"])
arc_props = {
x: getattr(arc, x) for x in set(init_args).difference(special_args)
}
arc_props["type_"] = arc.__class__.__name__
arc_props["in_port"] = arc.in_port.name
arc_props["out_port"] = arc.out_port.name
arcs[arc.name] = arc_props
data = {
"nodes": nodes,
"arcs": arcs,
"orchestration": self.orchestration,
"pollutants": constants.POLLUTANTS,
"additive_pollutants": constants.ADDITIVE_POLLUTANTS,
"non_additive_pollutants": constants.NON_ADDITIVE_POLLUTANTS,
"float_accuracy": constants.FLOAT_ACCURACY,
"extensions": self.extensions,
"river_discharge_order": self.river_discharge_order,
}
if hasattr(self, "dates"):
data["dates"] = [str(x) for x in self.dates]
def coerce_value(value):
"""
Args:
value:
Returns:
"""
conversion_options = {
"__float__": float,
"__iter__": list,
"__int__": int,
"__str__": str,
"__bool__": bool,
}
converted = False
for property, func in conversion_options.items():
if hasattr(value, property):
try:
yaml.safe_dump(func(value))
value = func(value)
converted = True
break
except Exception:
raise ValueError(f"Cannot dump: {value} of type {type(value)}")
if not converted:
raise ValueError(f"Cannot dump: {value} of type {type(value)}")
return value
def check_and_coerce_dict(data_dict):
"""
Args:
data_dict:
"""
for key, value in data_dict.items():
if isinstance(value, dict):
check_and_coerce_dict(value)
else:
try:
yaml.safe_dump(value)
except yaml.representer.RepresenterError:
if hasattr(value, "__iter__"):
for idx, val in enumerate(value):
if isinstance(val, dict):
check_and_coerce_dict(val)
else:
value[idx] = coerce_value(val)
data_dict[key] = coerce_value(value)
check_and_coerce_dict(data)
write_yaml(address, config_name, data)
def load_pickle(self, fid):
"""Load model object to a pickle file, including the model states.
Args:
fid (str): File address to load the pickled model from
Returns:
model (obj): loaded model
Example:
>>> # Load and run your model
>>> my_model.load(model_dir,config_name = 'config.yml')
>>> _ = my_model.run()
>>>
>>> # Save it including its different states
>>> my_model.save_pickle('model_at_end_of_run.pkl')
>>>
>>> # Load it at another time to resume the model from the end
>>> # of the previous run
>>> new_model = Model()
>>> new_model = new_model.load_pickle('model_at_end_of_run.pkl')
"""
file = open(fid, "rb")
return pickle.load(file)
def save_pickle(self, fid):
"""Save model object to a pickle file, including saving the model states.
Args:
fid (str): File address to save the pickled model to
Returns:
message (str): Exit message of pickle dump
"""
file = open(fid, "wb")
pickle.dump(self, file)
return file.close()
def add_nodes(self, nodelist):
"""Add nodes to the model object from a list of dicts, where each dict contains
all of the parameters for a node. Intended to be called before add_arcs.
Args:
nodelist (list): List of dicts, where a dict is a node
"""
for data in nodelist:
name = data["name"]
type_ = data["type_"]
if "node_type_override" in data.keys():
node_type = data["node_type_override"]
del data["node_type_override"]
else:
node_type = type_
if "foul" in name:
# Absolute hack to enable foul sewers to be treated separate from storm
type_ = "Foul"
if "geometry" in data.keys():
del data["geometry"]
del data["type_"]
if node_type not in NODES_REGISTRY.keys():
raise ValueError(f"Node type {node_type} not recognised")
if type_ not in self.nodes_type.keys():
self.nodes_type[type_] = {}
self.nodes_type[type_][name] = NODES_REGISTRY[node_type](**dict(data))
self.nodes[name] = self.nodes_type[type_][name]
self.nodelist = [x for x in self.nodes.values()]
def add_instantiated_nodes(self, nodelist):
"""Add nodes to the model object from a list of objects, where each object is an
already instantiated node object. Intended to be called before add_arcs.
Args:
nodelist (list): list of objects that are nodes
"""
self.nodelist = nodelist
self.nodes = {x.name: x for x in nodelist}
for x in nodelist:
type_ = x.__class__.__name__
if type_ not in self.nodes_type.keys():
self.nodes_type[type_] = {}
self.nodes_type[type_][x.name] = x
def add_arcs(self, arclist):
"""Add nodes to the model object from a list of dicts, where each dict contains
all of the parameters for an arc.
Args:
arclist (list): list of dicts, where a dict is an arc
"""
river_arcs = {}
for arc in arclist:
name = arc["name"]
type_ = arc["type_"]
del arc["type_"]
arc["in_port"] = self.nodes[arc["in_port"]]
arc["out_port"] = self.nodes[arc["out_port"]]
self.arcs[name] = getattr(arcs_mod, type_)(**dict(arc))
if arc["in_port"].__class__.__name__ in [
"River",
"Node",
"Waste",
"Reservoir",
]:
if arc["out_port"].__class__.__name__ in [
"River",
"Node",
"Waste",
"Reservoir",
]:
river_arcs[name] = self.arcs[name]
self.river_discharge_order = []
if not any(river_arcs):
return
upstreamness = (
{x: 0 for x in self.nodes_type["Waste"].keys()}
if "Waste" in self.nodes_type
else {}
)
upstreamness = self.assign_upstream(river_arcs, upstreamness)
if "River" in self.nodes_type:
for node in sorted(
upstreamness.items(), key=lambda item: item[1], reverse=True
):
if node[0] in self.nodes_type["River"]:
self.river_discharge_order.append(node[0])
def add_instantiated_arcs(self, arclist):
"""Add arcs to the model object from a list of objects, where each object is an
already instantiated arc object.
Args:
arclist (list): list of objects that are arcs.
"""
self.arclist = arclist
self.arcs = {x.name: x for x in arclist}
river_arcs = {}
for arc in arclist:
if arc.in_port.__class__.__name__ in [
"River",
"Node",
"Waste",
"Reservoir",
]:
if arc.out_port.__class__.__name__ in [
"River",
"Node",
"Waste",
"Reservoir",
]:
river_arcs[arc.name] = arc
if not any(river_arcs):
return
upstreamness = (
{x: 0 for x in self.nodes_type["Waste"].keys()}
if "Waste" in self.nodes_type
else {}
)
upstreamness = {x: 0 for x in self.nodes_type["Waste"].keys()}
upstreamness = self.assign_upstream(river_arcs, upstreamness)
self.river_discharge_order = []
if "River" in self.nodes_type:
for node in sorted(
upstreamness.items(), key=lambda item: item[1], reverse=True
):
if node[0] in self.nodes_type["River"]:
self.river_discharge_order.append(node[0])
def assign_upstream(self, arcs, upstreamness):
"""Recursive function to trace upstream up arcs to determine which are the most
upstream.
Args:
arcs (list): list of dicts where dicts are arcs
upstreamness (dict): dictionary contain nodes in
arcs as keys and a number representing upstreamness
(higher numbers = more upstream)
Returns:
upstreamness (dict): final version of upstreamness
"""
upstreamness_ = upstreamness.copy()
in_nodes = [
x.in_port.name
for x in arcs.values()
if x.out_port.name in upstreamness.keys()
]
ind = max(list(upstreamness_.values())) + 1
in_nodes = list(set(in_nodes).difference(upstreamness.keys()))
for node in in_nodes:
upstreamness[node] = ind
if upstreamness == upstreamness_:
return upstreamness
else:
upstreamness = self.assign_upstream(arcs, upstreamness)
return upstreamness
def add_overrides(self, config: dict):
"""Apply overrides to nodes and arcs in the model object.
Args:
config (dict): dictionary of overrides to apply to the model object.
"""
for node in config.get("nodes", {}).values():
type_ = node.pop("type_")
name = node.pop("name")
if type_ not in self.nodes_type.keys():
raise ValueError(f"Node type {type_} not recognised")
if name not in self.nodes_type[type_].keys():
raise ValueError(f"Node {name} not recognised")
self.nodes_type[type_][name].apply_overrides(node)
for arc in config.get("arcs", {}).values():
name = arc.pop("name")
type_ = arc.pop("type_")
if name not in self.arcs.keys():
raise ValueError(f"Arc {name} not recognised")
self.arcs[name].apply_overrides(arc)
def debug_node_mb(self):
"""Simple function that iterates over nodes calling their mass balance
function."""
for node in self.nodelist:
_ = node.node_mass_balance()
def default_settings(self):
"""Incomplete function that enables easy specification of results storage.
Returns:
(dict): default settings
"""
return {
"arcs": {"flows": True, "pollutants": True},
"tanks": {"storages": True, "pollutants": True},
"mass_balance": False,
}
def change_runoff_coefficient(self, relative_change, nodes=None):
"""Clunky way to change the runoff coefficient of a land node.
Args:
relative_change (float): amount that the impervious area in the land
node is multiplied by (grass area is changed in compensation)
nodes (list, optional): list of land nodes to change the parameters of.
Defaults to None, which applies the change to all land nodes.
"""
# Multiplies impervious area by relative change and adjusts grassland
# accordingly
if nodes is None:
nodes = self.nodes_type["Land"].values()
if isinstance(relative_change, float):
relative_change = {x: relative_change for x in nodes}
for node in nodes:
surface_dict = {x.surface: x for x in node.surfaces}
if "Impervious" in surface_dict.keys():
impervious_area = surface_dict["Impervious"].area
grass_area = surface_dict["Grass"].area
new_impervious_area = impervious_area * relative_change[node]
new_grass_area = grass_area + (impervious_area - new_impervious_area)
if new_grass_area < 0:
print("not enough grass")
break
surface_dict["Impervious"].area = new_impervious_area
surface_dict["Impervious"].capacity *= relative_change[node]
surface_dict["Grass"].area = new_grass_area
surface_dict["Grass"].capacity *= new_grass_area / grass_area
for pol in constants.ADDITIVE_POLLUTANTS + ["volume"]:
surface_dict["Grass"].storage[pol] *= new_grass_area / grass_area
for pool in surface_dict["Grass"].nutrient_pool.pools:
for nutrient in pool.storage.keys():
pool.storage[nutrient] *= new_grass_area / grass_area
def run(
self,
dates=None,
settings=None,
record_arcs=None,
record_tanks=None,
record_surfaces=None,
verbose=True,
record_all=True,
objectives=[],
):
"""Run the model object with the default orchestration.
Args:
dates (list, optional): Dates to simulate. Defaults to None, which
simulates all dates that the model has data for.
settings (dict, optional): Dict to specify what results are stored,
not currently used. Defaults to None.
record_arcs (list, optional): List of arcs to store result for.
Defaults to None.
record_tanks (list, optional): List of nodes with water stores to
store results for. Defaults to None.
record_surfaces (list, optional): List of tuples of
(land node, surface) to store results for. Defaults to None.
verbose (bool, optional): Prints updates on simulation if true.
Defaults to True.
record_all (bool, optional): Specifies to store all results.
Defaults to True.
objectives (list, optional): A list of dicts with objectives to
calculate (see examples). Defaults to [].
Returns:
flows: simulated flows in a list of dicts
tanks: simulated tanks storages in a list of dicts
objective_results: list of values based on objectives list
surfaces: simulated surface storages of land nodes in a list of dicts
Examples:
# Run a model without storing any results but calculating objectives
import statistics as stats
objectives = [{'element_type' : 'flows',
'name' : 'my_river',
'function' : @ (x, _) stats.mean([y['phosphate'] for y in x])
},
{'element_type' : 'tanks',
'name' : 'my_reservoir',
'function' : @ (x, model) sum([y['storage'] < (model.nodes
['my_reservoir'].tank.capacity / 2) for y in x])
}]
_, _, results, _ = my_model.run(record_all = False, objectives = objectives)
"""
if record_arcs is None:
record_arcs = []
if record_all:
record_arcs = list(self.arcs.keys())
if record_tanks is None:
record_tanks = []
if record_surfaces is None:
record_surfaces = []
if settings is None:
settings = self.default_settings()
def blockPrint():
"""
Returns:
"""
stdout = sys.stdout
sys.stdout = open(os.devnull, "w")
return stdout
def enablePrint(stdout):
"""
Args:
stdout:
"""
sys.stdout = stdout
if not verbose:
stdout = blockPrint()
if dates is None:
dates = self.dates
for objective in objectives:
if objective["element_type"] == "tanks":
record_tanks.append(objective["name"])
elif objective["element_type"] == "flows":
record_arcs.append(objective["name"])
elif objective["element_type"] == "surfaces":
record_surfaces.append((objective["name"], objective["surface"]))
else:
print("element_type not recorded")
flows = []
tanks = []
surfaces = []
for date in tqdm(dates, disable=(not verbose)):
# for date in dates:
for node in self.nodelist:
node.t = date
node.monthyear = date.to_period("M")
# Iterate over orchestration
for timestep_item in self.orchestration:
for node_type, function in timestep_item.items():
for node in self.nodes_type.get(node_type, {}).values():
getattr(node, function)()
# river
for node_name in self.river_discharge_order:
self.nodes[node_name].distribute()
# mass balance checking
# nodes/system
sys_in = self.empty_vqip()
sys_out = self.empty_vqip()
sys_ds = self.empty_vqip()
# arcs
for arc in self.arcs.values():
in_, ds_, out_ = arc.arc_mass_balance()
for v in constants.ADDITIVE_POLLUTANTS + ["volume"]:
sys_in[v] += in_[v]
sys_out[v] += out_[v]
sys_ds[v] += ds_[v]
for node in self.nodelist:
# print(node.name)
in_, ds_, out_ = node.node_mass_balance()
# temp = {'name' : node.name,
# 'time' : date}
# for lab, dict_ in zip(['in','ds','out'], [in_, ds_, out_]):
# for key, value in dict_.items():
# temp[(lab, key)] = value
# node_mb.append(temp)
for v in constants.ADDITIVE_POLLUTANTS + ["volume"]:
sys_in[v] += in_[v]
sys_out[v] += out_[v]
sys_ds[v] += ds_[v]
for v in constants.ADDITIVE_POLLUTANTS + ["volume"]:
# Find the largest value of in_, out_, ds_
largest = max(sys_in[v], sys_in[v], sys_in[v])
if largest > constants.FLOAT_ACCURACY:
# Convert perform comparison in a magnitude to match the largest
# value
magnitude = 10 ** int(log10(largest))
in_10 = sys_in[v] / magnitude
out_10 = sys_in[v] / magnitude
ds_10 = sys_in[v] / magnitude
else:
in_10 = sys_in[v]
ds_10 = sys_in[v]
out_10 = sys_in[v]
if (in_10 - ds_10 - out_10) > constants.FLOAT_ACCURACY:
print(
"system mass balance error for "
+ v
+ " of "
+ str(sys_in[v] - sys_ds[v] - sys_out[v])
)
# Store results
for arc in record_arcs:
arc = self.arcs[arc]
flows.append(
{"arc": arc.name, "flow": arc.vqip_out["volume"], "time": date}
)
for pol in constants.POLLUTANTS:
flows[-1][pol] = arc.vqip_out[pol]
for node in record_tanks:
node = self.nodes[node]
tanks.append(
{
"node": node.name,
"storage": node.tank.storage["volume"],
"time": date,
}
)
for node, surface in record_surfaces:
node = self.nodes[node]
name = node.name
surface = node.get_surface(surface)
if not isinstance(surface, ImperviousSurface):
surfaces.append(
{
"node": name,
"surface": surface.surface,
"percolation": surface.percolation["volume"],
"subsurface_r": surface.subsurface_flow["volume"],
"surface_r": surface.infiltration_excess["volume"],
"storage": surface.storage["volume"],
"evaporation": surface.evaporation["volume"],
"precipitation": surface.precipitation["volume"],
"tank_recharge": surface.tank_recharge,
"capacity": surface.capacity,
"time": date,
"et0_coef": surface.et0_coefficient,
# 'crop_factor' : surface.crop_factor
}
)
for pol in constants.POLLUTANTS:
surfaces[-1][pol] = surface.storage[pol]
else:
surfaces.append(
{
"node": name,
"surface": surface.surface,
"storage": surface.storage["volume"],
"evaporation": surface.evaporation["volume"],
"precipitation": surface.precipitation["volume"],
"capacity": surface.capacity,
"time": date,
}
)
for pol in constants.POLLUTANTS:
surfaces[-1][pol] = surface.storage[pol]
if record_all:
for node in self.nodes.values():
for prop_ in dir(node):
prop = node.__getattribute__(prop_)
if prop.__class__ in [QueueTank, Tank, ResidenceTank]:
tanks.append(
{
"node": node.name,
"time": date,
"storage": prop.storage["volume"],
"prop": prop_,
}
)
for pol in constants.POLLUTANTS:
tanks[-1][pol] = prop.storage[pol]
for name, node in self.nodes_type.get("Land", {}).items():
for surface in node.surfaces:
if not isinstance(surface, ImperviousSurface):
surfaces.append(
{
"node": name,
"surface": surface.surface,
"percolation": surface.percolation["volume"],
"subsurface_r": surface.subsurface_flow["volume"],
"surface_r": surface.infiltration_excess["volume"],
"storage": surface.storage["volume"],
"evaporation": surface.evaporation["volume"],
"precipitation": surface.precipitation["volume"],
"tank_recharge": surface.tank_recharge,
"capacity": surface.capacity,
"time": date,
"et0_coef": surface.et0_coefficient,
# 'crop_factor' : surface.crop_factor
}
)
for pol in constants.POLLUTANTS:
surfaces[-1][pol] = surface.storage[pol]
else:
surfaces.append(
{
"node": name,
"surface": surface.surface,
"storage": surface.storage["volume"],
"evaporation": surface.evaporation["volume"],
"precipitation": surface.precipitation["volume"],
"capacity": surface.capacity,
"time": date,
}
)
for pol in constants.POLLUTANTS:
surfaces[-1][pol] = surface.storage[pol]
for node in self.nodes.values():
node.end_timestep()
for arc in self.arcs.values():
arc.end_timestep()
objective_results = []
for objective in objectives:
if objective["element_type"] == "tanks":
val = objective["function"](
[x for x in tanks if x["node"] == objective["name"]], self
)
elif objective["element_type"] == "flows":
val = objective["function"](
[x for x in flows if x["arc"] == objective["name"]], self
)
elif objective["element_type"] == "surfaces":
val = objective["function"](
[
x
for x in surfaces
if (x["node"] == objective["name"])
& (x["surface"] == objective["surface"])
],
self,
)
objective_results.append(val)
if not verbose:
enablePrint(stdout)
return flows, tanks, objective_results, surfaces
def reinit(self):
"""Reinitialise by ending all node/arc timesteps and calling reinit function in