-
Notifications
You must be signed in to change notification settings - Fork 15
/
data.jl
1939 lines (1622 loc) · 67.6 KB
/
data.jl
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
# tools for working with GasModels internal data format
"GasModels wrapper for the InfrastructureModels `apply!` function."
function apply_gm!(func!::Function, data::Dict{String, <:Any}; apply_to_subnetworks::Bool = true)
_IM.apply!(func!, data, gm_it_name; apply_to_subnetworks = apply_to_subnetworks)
end
"GasModels wrapper for the InfrastructureModels `get_data` function."
function get_data_gm(func::Function, data::Dict{String, <:Any}; apply_to_subnetworks::Bool = true)
return _IM.get_data(func, data, gm_it_name; apply_to_subnetworks = apply_to_subnetworks)
end
"Convenience function for retrieving the gas-only portion of network data."
function get_gm_data(data::Dict{String, <:Any})
return _IM.ismultiinfrastructure(data) ? data["it"][gm_it_name] : data
end
"data getters"
@inline get_base_pressure(data::Dict{String, <:Any}) = get_data_gm((x -> return x["base_pressure"]), data; apply_to_subnetworks = false)
@inline get_base_density(data::Dict{String, <:Any}) = get_data_gm((x -> return x["base_density"]), data; apply_to_subnetworks = false)
@inline get_base_length(data::Dict{String, <:Any}) = get_data_gm((x -> return x["base_length"]), data; apply_to_subnetworks = false)
@inline get_base_flow(data::Dict{String, <:Any}) = get_data_gm((x -> return x["base_flow"]), data; apply_to_subnetworks = false)
@inline get_base_flux(data::Dict{String, <:Any}) = get_data_gm((x -> return x["base_flux"]), data; apply_to_subnetworks = false)
@inline get_base_time(data::Dict{String, <:Any}) = get_data_gm((x -> return x["base_time"]), data; apply_to_subnetworks = false)
@inline get_base_diameter(data::Dict{String, <:Any}) = get_data_gm((x -> return x["base_diameter"]), data; apply_to_subnetworks = false)
@inline get_base_volume(data::Dict{String, <:Any}) = get_data_gm((x -> return x["base_volume"]), data; apply_to_subnetworks = false)
@inline get_sound_speed(data::Dict{String, <:Any}) = get_data_gm((x -> return get(x, "sound_speed", 371.6643)), data; apply_to_subnetworks = false)
@inline get_specific_heat_capacity_ratio(data::Dict{String, <:Any}) = get_data_gm((x -> return get(x, "specific_heat_capacity_ratio", 0.6)), data; apply_to_subnetworks = false)
@inline get_gas_specific_gravity(data::Dict{String, <:Any}) = get_data_gm((x -> return get(x, "gas_specific_gravity", 0.6)), data; apply_to_subnetworks = false)
@inline get_gas_constant(data::Dict{String, <:Any}) = get_data_gm((x -> return get(x, "R", 8.314)), data; apply_to_subnetworks = false)
@inline get_temperature(data::Dict{String, <:Any}) = get_data_gm((x -> return get(x, "temperature", 288.7060)), data; apply_to_subnetworks = false)
@inline get_base_mass(data::Dict{String, <:Any}) = get_base_flow(data) * get_base_time(data)
@inline get_economic_weighting(data::Dict{String, <:Any}) = get_data_gm((x -> return get(x, "economic_weighting", 1.0)), data; apply_to_subnetworks = false)
"calculates base_pressure"
function calc_base_pressure(data::Dict{String,<:Any})
p_squares = [junc["p_max"]^2 for junc in values(data["junction"])]
if length(p_squares) > 0
# Use the square root of the median max squared pressure.
return sqrt(Statistics.median(p_squares))
else
return 1.379e6 # In Pascals, 200 PSI.
end
end
"calculates the base_time"
function calc_base_time(data::Dict{String,<:Any})
return get_base_length(data) / get_sound_speed(data)
end
"calculates the base_flow - this is actually wrong terminology (has to be base_flux - kg/m^2/s, flow is kg/s)"
function calc_base_flow(data::Dict{String,<:Any})
return get_base_pressure(data) / get_sound_speed(data)
end
"calculates the base_flux"
function calc_base_flux(data::Dict{String,<:Any})
return get_base_density(data) * get_sound_speed(data)
end
"calculates the base density"
function calc_base_density(data::Dict{String,<:Any})
return get_base_pressure(data) / get_sound_speed(data)^2
end
"estimates standard density from existing data"
function _estimate_standard_density(data::Dict{String,<:Any})
standard_pressure = 101325.0 # 1 atm in Pascals
molecular_mass_of_air = 0.02896
temperature = get_temperature(data)
specific_gravity = get_gas_specific_gravity(data)
gas_constant = get_gas_constant(data)
return standard_pressure * specific_gravity *
molecular_mass_of_air * inv(temperature * gas_constant)
end
"apply a function on a dict entry"
function _apply_func!(data::Dict{String,Any}, key::String, func)
if haskey(data, key)
data[key] = func(data[key])
end
end
"if original data is in per-unit ensure it has base values"
function per_unit_data_field_check!(data::Dict{String, <:Any})
apply_gm!(_per_unit_data_field_check!, data; apply_to_subnetworks = false)
end
"if original data is in per-unit ensure it has base values"
function _per_unit_data_field_check!(data::Dict{String,Any})
if get(data, "is_per_unit", false) == true
if get(data, "base_pressure", false) == false || get(data, "base_length", false) == false
Memento.error(_LOGGER, "data in .m file is in per unit but no base_pressure (in Pa) and base_length (in m) values are provided")
else
if get(data, "base_density", false) == false
data["base_density"] = calc_base_density(data)
end
data["base_diameter"] = 1.0
data["base_time"] = calc_base_time(data)
if get(data, "base_flow", false) == false
data["base_flow"] = calc_base_flow(data)
end
if get(data, "base_flux", false) == false
data["base_flux"] = calc_base_flux(data)
end
end
end
end
"adds additional non-dimensional constants to data dictionary"
function add_base_values!(data::Dict{String, <:Any})
apply_gm!(_add_base_values!, data; apply_to_subnetworks = false)
end
"adds additional non-dimensional constants to data dictionary"
function _add_base_values!(data::Dict{String,Any})
if get(data, "base_pressure", false) == false
data["base_pressure"] = calc_base_pressure(data)
end
if get(data, "base_density", false) == false
data["base_density"] = calc_base_density(data)
end
if get(data, "base_length", false) == false
data["base_length"] = 5000.0
end
data["base_diameter"] = 1.0
data["base_time"] = calc_base_time(data)
if get(data, "base_flow", false) == false
data["base_flow"] = calc_base_flow(data)
end
if get(data, "base_flux", false) == false
data["base_flux"] = calc_base_flux(data)
end
if get(data, "base_volume", false) == false
data["base_volume"] = data["base_length"]
end
if get(data, "base_mass", false) == false
data["base_mass"] = data["base_density"] * data["base_volume"]
end
end
"make transient data to si units"
function make_si_units!(transient_data::Array{Dict{String, Any}, 1}, static_data::Dict{String,Any})
gm_static_data = get_gm_data(static_data)
if gm_static_data["units"] == "si"
return
end
mmscfd_to_kgps = x -> x * get_mmscfd_to_kgps_conversion_factor(gm_static_data)
inv_mmscfd_to_kgps = x -> x / get_mmscfd_to_kgps_conversion_factor(gm_static_data)
pressure_params = [
"p_min",
"p_max",
"p_nominal",
"p",
"inlet_p_min",
"inlet_p_max",
"outlet_p_min",
"outlet_p_max",
"design_inlet_pressure",
"design_outlet_pressure",
"pressure_nominal",
"reservoir_p_max",
"reservoir_pressure",
]
flow_params = [
"f",
"fd",
"ft",
"fg",
"flow_min",
"flow_max",
"withdrawal_min",
"withdrawal_max",
"withdrawal_nominal",
"injection_min",
"injection_max",
"injection_nominal",
"design_flow_rate",
"flow_injection_rate_min",
"flow_injection_rate_max",
"flow_withdrawal_rate_min",
"flow_withdrawal_rate_max",
"storage_flow"
]
inv_flow_params = ["bid_price", "offer_price"]
for line in transient_data
param = line["parameter"]
if param in pressure_params
line["value"] = psi_to_pascal(line["value"])
end
if param in flow_params
line["value"] = mmscfd_to_kgps(line["value"])
end
if param in inv_flow_params
line["value"] = inv_mmscfd_to_kgps(line["value"])
end
end
end
const _params_for_unit_conversions = Dict(
"junction" => [
"p_min",
"p_max",
"p_nominal",
"p",
"pressure",
"density",
"net_injection",
"net_nodal_edge_out_flow",
],
"original_junction" => ["p_min", "p_max", "p_nominal", "p"],
"pipe" => [
"length",
"p_min",
"p_max",
"f",
"flux",
"flow",
"flow_min",
"flow_max",
"flux_avg",
"flux_neg",
"flux_fr",
"flux_to",
"flow_avg",
"flow_neg",
"flow_fr",
"flow_to",
],
"original_pipe" => ["length", "p_min", "p_max", "f", "flow_min", "flow_max"],
"ne_pipe" => ["length", "p_min", "p_max", "f", "flow_min", "flow_max"],
"compressor" => [
"length",
"flow_min",
"flow_max",
"inlet_p_min",
"inlet_p_max",
"outlet_p_min",
"outlet_p_max",
"f",
"power_max",
"flow",
"power",
"power_var",
"power_expr"
],
"ne_compressor" => [
"length",
"flow_min",
"flow_max",
"inlet_p_min",
"inlet_p_max",
"outlet_p_min",
"outlet_p_max",
"f",
"power_max",
],
"transfer" => [
"withdrawal_min",
"withdrawal_max",
"withdrawal_nominal",
"ft",
"bid_price",
"offer_price",
"injection",
"withdrawal",
],
"receipt" => [
"injection_min",
"injection_max",
"injection_nominal",
"fg",
"offer_price",
"injection",
],
"delivery" => [
"withdrawal_min",
"withdrawal_max",
"withdrawal_nominal",
"fd",
"bid_price",
"withdrawal",
],
"regulator" => [
"flow_min",
"flow_max",
"design_flow_rate",
"design_inlet_pressure",
"design_outlet_pressure",
"f",
],
"storage" => [
"well_depth",
"reservoir_p_max",
"flow_injection_rate_min",
"flow_injection_rate_max",
"flow_withdrawal_rate_min",
"flow_withdrawal_rate_max",
"base_gas_capacity",
"total_field_capacity",
"reservoir_density",
"reservoir_pressure",
"well_pressure",
"storage_flow"
],
"loss_resistor" => [
"f",
"p_loss",
"flow_min",
"flow_max"
],
"resistor" => [
"f",
"flow_min",
"flow_max"
],
"valve" => [
"flow_min",
"flow_max"
],
)
function _rescale_functions(rescale_pressure::Function, rescale_density::Function, rescale_length::Function, rescale_diameter::Function, rescale_flow::Function, rescale_mass::Function, rescale_inv_flow::Function)::Dict{String,Function}
Dict{String,Function}(
"p_min" => rescale_pressure,
"p_max" => rescale_pressure,
"p_nominal" => rescale_pressure,
"p" => rescale_pressure,
"inlet_p_min" => rescale_pressure,
"inlet_p_max" => rescale_pressure,
"outlet_p_min" => rescale_pressure,
"outlet_p_max" => rescale_pressure,
"reservoir_pressure" => rescale_pressure,
"well_pressure" => rescale_pressure,
"pressure" => rescale_pressure,
"p_loss" => rescale_pressure,
"design_inlet_pressure" => rescale_pressure,
"design_outlet_pressure" => rescale_pressure,
"pressure_nominal" => rescale_pressure,
"reservoir_p_max" => rescale_pressure,
"density" => rescale_density,
"reservoir_density" => rescale_density,
"well_density" => rescale_density,
"length" => rescale_length,
"well_depth" => rescale_length,
"diameter" => rescale_diameter,
"well_diameter" => rescale_diameter,
"f" => rescale_flow,
"flow_min" => rescale_flow,
"flow_max" => rescale_flow,
"flow" => rescale_flow,
"flux_avg" => rescale_flow,
"flux_neg" => rescale_flow,
"flux_fr" => rescale_flow,
"flux_to" => rescale_flow,
"flow_avg" => rescale_flow,
"flow_neg" => rescale_flow,
"flow_fr" => rescale_flow,
"flow_to" => rescale_flow,
"withdrawal" => rescale_flow,
"injection" => rescale_flow,
"power" => rescale_flow,
"flux" => rescale_flow,
"flow" => rescale_flow,
"withdrawal_max" => rescale_flow,
"withdrawal_min" => rescale_flow,
"injection_min" => rescale_flow,
"injection_max" => rescale_flow,
"net_injection" => rescale_flow,
"net_nodal_edge_out_flow" => rescale_flow,
"withdrawal_nominal" => rescale_flow,
"injection_nominal" => rescale_flow,
"fd" => rescale_flow,
"fg" => rescale_flow,
"ft" => rescale_flow,
"power_max" => rescale_flow,
"power_var" => rescale_flow,
"power_expr" => rescale_flow,
"design_flow_rate" => rescale_flow,
"flow_injection_rate_min" => rescale_flow,
"flow_injection_rate_max" => rescale_flow,
"flow_withdrawal_rate_min" => rescale_flow,
"flow_withdrawal_rate_max" => rescale_flow,
"base_gas_capacity" => rescale_mass,
"total_field_capacity" => rescale_mass,
"storage_flow" => rescale_flow,
"bid_price" => rescale_inv_flow,
"offer_price" => rescale_inv_flow,
)
end
"Transforms data to si units"
function si_to_pu!(data::Dict{String,<:Any}; id = "0")
rescale_flow = x -> x / get_base_flow(data)
rescale_inv_flow = x -> x * get_base_flow(data)
rescale_pressure = x -> x / get_base_pressure(data)
rescale_density = x -> x / get_base_density(data)
rescale_length = x -> x / get_base_length(data)
rescale_time = x -> x / get_base_time(data)
rescale_mass = x -> x / get_base_flow(data) / get_base_time(data)
rescale_diameter = x -> x / get_base_diameter(data)
functions = _rescale_functions(
rescale_pressure,
rescale_density,
rescale_length,
rescale_diameter,
rescale_flow,
rescale_mass,
rescale_inv_flow,
)
gm_data = get_gm_data(data)
gm_nw_data = (id == "0") ? gm_data : gm_data["nw"][id]
_apply_func!(gm_nw_data, "time_point", rescale_time)
for (component, parameters) in _params_for_unit_conversions
for (i, comp) in get(gm_nw_data, component, [])
if !haskey(comp, "is_per_unit") && !haskey(gm_data, "is_per_unit")
Memento.error(_LOGGER, "the current units of the data/result dictionary unknown")
end
if !haskey(comp, "is_per_unit") && haskey(gm_data, "is_per_unit")
comp["is_per_unit"] = gm_data["is_per_unit"]
comp["is_si_units"] = 0
comp["is_english_units"] = 0
end
if comp["is_si_units"] == true && comp["is_per_unit"] == false
for param in parameters
_apply_func!(comp, param, functions[param])
end
comp["is_si_units"] = 0
comp["is_english_units"] = 0
comp["is_per_unit"] = 1
end
end
end
end
function pu_to_si!(data::Dict{String,<:Any}; id = "0")
rescale_flow = x -> x * get_base_flow(data)
rescale_inv_flow = x -> x / get_base_flow(data)
rescale_pressure = x -> x * get_base_pressure(data)
rescale_density = x -> x * get_base_density(data)
rescale_length = x -> x * get_base_length(data)
rescale_time = x -> x * get_base_time(data)
rescale_mass = x -> x * get_base_flow(data) * get_base_time(data)
rescale_diameter = x -> x * get_base_diameter(data)
functions = _rescale_functions(
rescale_pressure,
rescale_density,
rescale_length,
rescale_diameter,
rescale_flow,
rescale_mass,
rescale_inv_flow,
)
gm_data = get_gm_data(data)
gm_nw_data = (id == "0") ? gm_data : gm_data["nw"][id]
_apply_func!(gm_nw_data, "time_point", rescale_time)
for (component, parameters) in _params_for_unit_conversions
for (i, comp) in get(gm_nw_data, component, [])
if !haskey(comp, "is_per_unit") && !haskey(gm_data, "is_per_unit")
Memento.error(_LOGGER, "the current units of the data/result dictionary unknown")
end
if !haskey(comp, "is_per_unit") && haskey(gm_data, "is_per_unit")
@assert gm_data["is_per_unit"] == 1
comp["is_per_unit"] = gm_data["is_per_unit"]
comp["is_si_units"] = 0
comp["is_english_units"] = 0
end
if comp["is_si_units"] == false && comp["is_per_unit"] == true
for param in parameters
_apply_func!(comp, param, functions[param])
comp["is_si_units"] = 1
comp["is_english_units"] = 0
comp["is_per_unit"] = 0
end
end
end
end
end
function si_to_english!(data::Dict{String,<:Any}; id = "0")
rescale_flow = x -> x * get_kgps_to_mmscfd_conversion_factor(data)
rescale_inv_flow = x -> x / get_kgps_to_mmscfd_conversion_factor(data)
rescale_mass = x -> x / get_mmscfd_to_kgps_conversion_factor(data) / 86400.0
rescale_density = x -> x
rescale_pressure = pascal_to_psi
rescale_length = m_to_miles
rescale_diameter = m_to_inches
functions = _rescale_functions(
rescale_pressure,
rescale_density,
rescale_length,
rescale_diameter,
rescale_flow,
rescale_mass,
rescale_inv_flow,
)
gm_data = get_gm_data(data)
gm_nw_data = (id == "0") ? gm_data : gm_data["nw"][id]
for (component, parameters) in _params_for_unit_conversions
for (i, comp) in get(gm_nw_data, component, [])
if !haskey(comp, "is_per_unit") && !haskey(gm_data, "is_per_unit")
Memento.error(_LOGGER, "the current units of the data/result dictionary unknown")
end
if !haskey(comp, "is_per_unit") && haskey(gm_data, "is_per_unit")
@assert gm_data["is_per_unit"] == 1
comp["is_per_unit"] = gm_data["is_per_unit"]
comp["is_si_units"] = 0
comp["is_english_units"] = 0
end
if comp["is_english_units"] == false && comp["is_si_units"] == true
for param in parameters
_apply_func!(comp, param, functions[param])
comp["is_si_units"] = 0
comp["is_english_units"] = 1
comp["is_per_unit"] = 0
end
end
end
end
end
function english_to_si!(data::Dict{String,<:Any}; id = "0")
rescale_flow = x -> x * get_mmscfd_to_kgps_conversion_factor(data)
rescale_inv_flow = x -> x / get_mmscfd_to_kgps_conversion_factor(data)
rescale_mass = x -> x * get_mmscfd_to_kgps_conversion_factor(data) * 86400.0
rescale_density = x -> x
rescale_pressure = psi_to_pascal
rescale_length = miles_to_m
rescale_diameter = inches_to_m
functions = _rescale_functions(
rescale_pressure,
rescale_density,
rescale_length,
rescale_diameter,
rescale_flow,
rescale_mass,
rescale_inv_flow,
)
gm_data = get_gm_data(data)
gm_nw_data = (id == "0") ? gm_data : gm_data["nw"][id]
for (component, parameters) in _params_for_unit_conversions
for (i, comp) in get(gm_nw_data, component, [])
if !haskey(comp, "is_per_unit") && !haskey(gm_data, "is_per_unit")
Memento.error(_LOGGER, "the current units of the data/result dictionary unknown")
end
if !haskey(comp, "is_per_unit") && haskey(gm_data, "is_per_unit")
@assert gm_data["is_per_unit"] == 1
comp["is_per_unit"] = gm_data["is_per_unit"]
comp["is_si_units"] = 0
comp["is_english_units"] = 0
end
if comp["is_english_units"] == true && comp["is_si_units"] == false
for param in parameters
_apply_func!(comp, param, functions[param])
comp["is_si_units"] = 1
comp["is_english_units"] = 0
comp["is_per_unit"] = 0
end
end
end
end
end
"transforms data to si units"
function make_si_units!(data::Dict{String, <:Any})
apply_gm!(_make_si_units!, data; apply_to_subnetworks = false)
end
"transforms data to si units"
function _make_si_units!(data::Dict{String,<:Any})
if get(data, "is_si_units", false) == true
return
end
if get(data, "is_per_unit", false) == true
if _IM.ismultinetwork(data)
for (i, _) in data["nw"]
pu_to_si!(data, id = i)
end
else
pu_to_si!(data)
end
if haskey(data, "time_step")
rescale_time = x -> x * get_base_time(data)
data["time_step"] = rescale_time(data["time_step"])
end
data["is_si_units"] = 1
data["is_english_units"] = 0
data["is_per_unit"] = 0
end
if get(data, "is_english_units", false) == true
if _IM.ismultinetwork(data)
for (i, _) in data["nw"]
english_to_si!(data, id = i)
end
else
english_to_si!(data)
end
data["is_si_units"] = 1
data["is_english_units"] = 0
data["is_per_unit"] = 0
end
end
"Transforms network data into English units"
function make_english_units!(data::Dict{String, <:Any})
apply_gm!(_make_english_units!, data; apply_to_subnetworks = false)
end
"Transforms network data into English units"
function _make_english_units!(data::Dict{String, <:Any})
if get(data, "is_english_units", false) == true
return
end
if get(data, "is_per_unit", false) == true
make_si_units!(data)
end
if get(data, "is_si_units", false) == true
if _IM.ismultinetwork(data)
for (i, _) in data["nw"]
si_to_english!(data, id = i)
end
else
si_to_english!(data)
end
data["is_si_units"] = 0
data["is_english_units"] = 1
data["is_per_unit"] = 0
end
end
"Transforms network data into per unit"
function make_per_unit!(data::Dict{String, <:Any})
apply_gm!(_make_per_unit!, data; apply_to_subnetworks = false)
end
"Transforms network data into per unit"
function _make_per_unit!(data::Dict{String,<:Any})
if get(data, "is_per_unit", false) == true
return
end
if get(data, "is_english_units", false) == true
make_si_units!(data)
end
if get(data, "is_si_units", false) == true
if _IM.ismultinetwork(data)
for (i, _) in data["nw"]
si_to_pu!(data, id = i)
end
else
si_to_pu!(data)
end
if haskey(data, "time_step")
rescale_time = x -> x / get_base_time(data)
data["time_step"] = rescale_time(data["time_step"])
end
data["is_si_units"] = 0
data["is_english_units"] = 0
data["is_per_unit"] = 1
end
end
"checks for non-negativity of certain fields in the data"
function check_non_negativity(data::Dict{String, <:Any})
apply_gm!(_check_non_negativity, data; apply_to_subnetworks = false)
end
"checks for non-zero of certain fields in the data"
function check_non_zero(data::Dict{String, <:Any})
apply_gm!(_check_non_zero, data; apply_to_subnetworks = false)
end
"checks for rouge junction ids in data"
function check_rouge_junction_ids(data::Dict{String, <:Any})
apply_gm!(_check_rouge_junction_ids, data; apply_to_subnetworks = false)
end
"checks for rouge junction ids in data"
function _check_rouge_junction_ids(data::Dict{String,<:Any})
junction_ids = []
for (i, junction) in get(data, "junction", [])
push!(junction_ids, junction["id"])
end
for field in keys(rouge_junction_id_fields)
for (i, table) in get(data, field, [])
for column_name in get(rouge_junction_id_fields, field, [])
if !(table[column_name] in junction_ids)
Memento.error(_LOGGER, "junction_id of $field[$i] does not exist in junction table.")
end
end
end
end
end
"checks for non-negativity of certain fields in the data"
function _check_non_negativity(data::Dict{String, <:Any})
for field in non_negative_metadata
if get(data, field, 0.0) < 0.0
Memento.error(_LOGGER, "Metadata $field is less than zero.")
end
end
for field in keys(non_negative_data)
for (i, table) in get(data, field, [])
for column_name in get(non_negative_data, field, [])
if get(table, column_name, 0.0) < 0.0
Memento.error(_LOGGER, "$field[$i][$column_name] is less than zero.")
end
end
end
end
end
"checks for zero in certain fields in the data"
function _check_non_zero(data::Dict{String, <:Any})
for field in keys(non_zero_data)
for (i, table) in get(data, field, [])
for column_name in get(non_negative_data, field, [])
if get(table, column_name, 0.0) < 0.0
Memento.error(_LOGGER, "$field[$i][$column_name] is zero.")
end
end
end
end
end
"checks validity of global-level parameters"
function check_global_parameters(data::Dict{String, <:Any})
apply_gm!(_check_global_parameters, data; apply_to_subnetworks = false)
end
"checks validity of global-level parameters"
function _check_global_parameters(data::Dict{String, <:Any})
if get(data, "temperature", 273.15) < 260 || get(data, "temperature", 273.15) > 320
Memento.warn(_LOGGER, "temperature of $(data["temperature"]) K is unrealistic")
end
if get(data, "specific_heat_capacity_ratio", 1.4) < 1.2 || get(data, "specific_heat_capacity_ratio", 1.4) > 1.6
Memento.warn(_LOGGER, "specific heat capacity ratio of $(data["specific_heat_capacity_ratio"]) is unrealistic")
end
if get(data, "gas_specific_gravity", 0.6) < 0.5 || get(data, "gas_specific_gravity", 0.6) > 0.7
Memento.warn(_LOGGER, "gas specific gravity $(data["gas_specific_gravity"]) is unrealistic")
end
if get(data, "sound_speed", 355.0) < 300.0 || get(data, "sound_speed", 355.0) > 410.0
Memento.warn(_LOGGER, "sound speed of $(data["sound_speed"]) m/s is unrealistic")
end
if get(data, "compressibility_factor", 0.8) < 0.7 || get(data, "compressibility_factor", 0.8) > 1.0
Memento.warn(_LOGGER, "compressibility_factor $(data["compressibility_factor"]) is unrealistic")
end
end
"Correct mass flow bounds"
function correct_f_bounds!(data::Dict{String, <:Any})
apply_gm!(_correct_f_bounds!, data; apply_to_subnetworks = false)
end
"Correct mass flow bounds"
function _correct_f_bounds!(data::Dict{String,Any})
mf = _calc_max_mass_flow(
get(data, "receipt", Dict()),
get(data, "storage", Dict()),
get(data, "transfer", Dict()),
)
for (idx, pipe) in get(data, "pipe", Dict())
pipe["flow_min"] = _calc_pipe_flow_min(
-mf,
pipe,
data["junction"][string(pipe["fr_junction"])],
data["junction"][string(pipe["to_junction"])],
data["base_length"],
data["base_pressure"],
data["base_flow"],
data["sound_speed"],
)
pipe["flow_max"] = _calc_pipe_flow_max(
mf,
pipe,
data["junction"][string(pipe["fr_junction"])],
data["junction"][string(pipe["to_junction"])],
data["base_length"],
data["base_pressure"],
data["base_flow"],
data["sound_speed"],
)
end
for (idx, compressor) in get(data, "compressor", Dict())
compressor["flow_min"] = _calc_compressor_flow_min(-mf, compressor)
compressor["flow_max"] = _calc_compressor_flow_max(mf, compressor)
end
for (idx, pipe) in get(data, "short_pipe", Dict())
pipe["flow_min"] = _calc_short_pipe_flow_min(-mf, pipe)
pipe["flow_max"] = _calc_short_pipe_flow_max(mf, pipe)
end
if "standard_density" in keys(data)
density = data["standard_density"]
else
density = _estimate_standard_density(data)
end
for (idx, resistor) in get(data, "resistor", Dict())
resistor["flow_min"] = _calc_resistor_flow_min(
-mf, resistor, data["junction"][string(resistor["fr_junction"])],
data["junction"][string(resistor["to_junction"])],
Float64(data["base_pressure"]), Float64(data["base_flow"]), density)
resistor["flow_max"] = _calc_resistor_flow_max(
mf, resistor, data["junction"][string(resistor["fr_junction"])],
data["junction"][string(resistor["to_junction"])],
Float64(data["base_pressure"]), Float64(data["base_flow"]), density)
end
for (idx, loss_resistor) in get(data, "loss_resistor", Dict())
loss_resistor["flow_min"] = _calc_loss_resistor_flow_min(-mf, loss_resistor)
loss_resistor["flow_max"] = _calc_loss_resistor_flow_max(mf, loss_resistor)
end
for (idx, valve) in get(data, "valve", Dict())
valve["flow_min"] = _calc_valve_flow_min(-mf, valve)
valve["flow_max"] = _calc_valve_flow_max(mf, valve)
end
for (idx, regulator) in get(data, "regulator", Dict())
regulator["flow_min"] = _calc_regulator_flow_min(-mf, regulator)
regulator["flow_max"] = _calc_regulator_flow_max(mf, regulator)
end
for (idx, pipe) in get(data, "ne_pipe", Dict())
pipe["flow_min"] = _calc_ne_pipe_flow_min(
-mf,
pipe,
data["junction"][string(pipe["fr_junction"])],
data["junction"][string(pipe["to_junction"])],
data["base_length"],
data["base_pressure"],
data["base_flow"],
data["sound_speed"],
)
pipe["flow_max"] = _calc_ne_pipe_flow_max(
mf,
pipe,
data["junction"][string(pipe["fr_junction"])],
data["junction"][string(pipe["to_junction"])],
data["base_length"],
data["base_pressure"],
data["base_flow"],
data["sound_speed"],
)
end
for (idx, compressor) in get(data, "ne_compressor", Dict())
compressor["flow_min"] = _calc_ne_compressor_flow_min(-mf, compressor)
compressor["flow_max"] = _calc_ne_compressor_flow_max(mf, compressor)
end
end
"Correct minimum pressures"
function correct_p_mins!(data::Dict{String, <:Any})
apply_gm!(_correct_p_mins!, data; apply_to_subnetworks = false)
end
"Correct minimum pressures"
function _correct_p_mins!(data::Dict{String,Any}; si_value = 1.37e6, english_value = 200.0)
for (i, junction) in get(data, "junction", [])
if junction["p_min"] < 0.0
Memento.warn(_LOGGER, "junction $i's p_min changed to 1.37E6 Pa (200 PSI) from < 0")
(data["is_si_units"] == 1) && (junction["p_min"] = si_value)
(data["is_english_units"] == 1) && (junction["p_min"] = english_value)
end
end
for (i, pipe) in get(data, "pipe", [])
if pipe["p_min"] < 0.0
Memento.warn(_LOGGER, "pipe $i's p_min changed to 1.37E6 Pa (200 PSI) from < 0")
(data["is_si_units"] == 1) && (pipe["p_min"] = si_value)
(data["is_english_units"] == 1) && (pipe["p_min"] = english_value)
end
end
for (i, compressor) in get(data, "compressor", [])
if compressor["inlet_p_min"] < 0
Memento.warn(_LOGGER, "compressor $i's inlet_p_min changed to 1.37E6 Pa (200 PSI) from < 0")
(data["is_si_units"] == 1) && (compressor["inlet_p_min"] = si_value)
(data["is_english_units"] == 1) && (compressor["inlet_p_min"] = english_value)
end
if compressor["outlet_p_min"] < 0
Memento.warn(_LOGGER, "compressor $i's outlet_p_min changed to 1.37E6 Pa (200 PSI) from < 0")
(data["is_si_units"] == 1) && (compressor["outlet_p_min"] = si_value)
(data["is_english_units"] == 1) && (compressor["outlet_p_min"] = english_value)
end
end
end
"add additional compressor fields - required for transient"
function add_compressor_fields!(data::Dict{String, <:Any})
apply_gm!(_add_compressor_fields!, data; apply_to_subnetworks = false)
end
"add additional compressor fields - required for transient"
function _add_compressor_fields!(data::Dict{String,<:Any})
is_si_units = get(data, "is_si_units", 0)
is_english_units = get(data, "is_english_units", 0)
is_per_unit = get(data, "is_per_unit", false)
if haskey(data, "compressor")
for (i, compressor) in data["compressor"]
if is_si_units == true
compressor["diameter"] = 1.0
compressor["length"] = 250.0
compressor["friction_factor"] = 0.001
end
if is_english_units == true
compressor["diameter"] = 39.37
compressor["length"] = 0.16
compressor["friction_factor"] = 0.001