forked from LBNL-ETA/MSWH
-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
1674 lines (1316 loc) · 65 KB
/
models.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
import logging
import numpy as np
import pandas as pd
from mswh.system.components import Converter, Storage, Distribution
from mswh.tools.unit_converters import UnitConv
from mswh.comm.label_map import SwhLabels
log = logging.getLogger(__name__)
class System(object):
"""Project level system models:
* Assembles system configurations
* Performs timestep simulation
* Returns annual and timestep project and household
level results, such as gas and electricity use, heat delivered,
unmet demand and solar fraction.
Parameters:
sys_params: pd df
Main system component performance parameters per project
Default: None (default model parameters will get used)
backup_params: pd df
Backup system performance parameters per project.
It should contain a household ID column, otherwise
columns identical to params.
sys_sizes: pd df
Main system component sizes
Default: 1. (see individual components for specifics)
backup_sizes: pd df
Backup system component sizes, contains household id column
Default: 1. (see individual components for specifics)
weather: pd df
Weather data timeseries.
Number of rows equals the number of timesteps. Can be
generated using the Source.irradiation_and_water_main
method
Example:
>>> sourceASource(read_from_input_dataframes = inputs)
Oakland climate zone in CEC weather data is '03':
>>> self.weather = source.irradiation_and_water_main('03', method='isotropic diffuse')
loads: pd df
A dataframe with loads for all individual household
served by the project level system. It should
contain 3 columns: household id, occupancy and a column
with a load array in m3 for each household.
Example:
>>> loads_com = pd.DataFrame(data = [[1, occ_indiv - 1., 0.8 * load_array],[2, occ_indiv, 1. * load_array],[3, occ_indiv, 1.2 * load_array],[4, occ_indiv + 1., 1.4 * load_array]], columns = [self.c['id'], self.c['occ'], self.c['load_m3']])
timestep: float, h
Duration of a single timestep, in hours
Default: 1. h
log_level: None or python logger logging level,
Default: logging.DEBUG
This applies for a subset of the class functionality, mostly
used to deprecate logger messages for certain calculations.
For Example: log_level = logging.ERROR will only throw error
messages and ignore INFO, DEBUG and WARNING.
Examples:
See :func:`mswh.system.tests.test_components <mswh.system.tests.test_components>` module and
:func:`scripts/MSWH System Tool.ipynb <scripts/MSWH System Tool.ipynb>`
for examples on how to use the methods as stand alone and
in a system model simulation.
"""
def __init__(self, sys_params=None, backup_params=None, \
weather=None, sys_sizes=1., backup_sizes=1., \
loads=None, timestep=1., log_level=logging.DEBUG):
# this is to defend from using the models wihtout
# setting up the inputs as recommended
if (sys_params is None) and (backup_params is None) and \
(weather is None) and (loads is None):
msg = "Please consult example setup either in the "\
"test suite or in the example notebooks to "\
"appropriatelly set up a System instance."
log.error(msg)
raise ValueError
# log level (e.g. only partial functionality of the class
# is being used and one does not desire to see all infos)
self.log_level = log_level
logging.getLogger().setLevel(log_level)
self.s = SwhLabels().set_prod_labels()
self.c = SwhLabels().set_hous_labels()
self.r = SwhLabels().set_res_labels()
# assume individual system unless the loads contain multiple
# individual household loads
self.community = False
# main system performance parameters
self.sys_params = sys_params
# backup system performance parameters
self.backup_params = backup_params
self.weather = weather
# prepare loads
if loads is None:
self.load = 0.01 # m3
msg = 'The system did not receive any load requirement. '\
'Setting hot water draw to 0.01 m3/h.'
log.info(msg)
# initiate household level result df assuming 4
# occupants
self.cons_total = pd.DataFrame(
data=[[1, 4]],
columns=[self.c['id'], self.c['occ']])
# for the distribution losses
self.load_max = self.load * 1.
else:
# initiate household level result df
self.cons_total = pd.DataFrame(
columns=[self.c['id'], self.c['occ'], self.r['gas_use'],
self.r['gas_use_s'], self.r['gas_use_w'],
self.r['el_use'],
self.r['el_use_s'], self.r['el_use_w'],
self.r['sol_fra'],
self.r['q_del_bckp'], self.r['q_unmet']])
self.cons_total[self.c['id']] = loads[self.c['id']]
self.cons_total[self.c['occ']] = loads[self.c['occ']]
self.indiv_loads = pd.DataFrame(loads)
if loads.shape[0] > 1:
self.community = True
# set community load by summing individual loads
sum_loads = pd.DataFrame([(loads * 1.).sum()])
self.load = sum_loads[self.c['load_m3']].values[0] * 1.
self.loads = loads * 1.
# annual fractions for each household
ann_cons_fract = loads[self.c['load_m3']].apply(
lambda x: x.sum()) / self.load.sum()
# this is only to avoid division zero
sum_loads.loc[0, self.c['load_m3']][
sum_loads.loc[0, self.c['load_m3']] == 0] = np.inf
# timeseries household load fractions in the
# community load profile
self.indiv_load_ratios = loads * 1.
self.indiv_load_ratios[self.c['load_m3']] = (
loads[self.c['load_m3']] /
pd.concat(
[sum_loads[self.c['load_m3']]] * loads.shape[0],
ignore_index=True))
self.indiv_load_ratios[self.c['id']] = loads[self.c['id']]
self.indiv_load_ratios[self.c['occ']] = loads[self.c['occ']]
# prepare for usage after simulation (+1 timestep)
self.indiv_load_ratios[self.c['load_m3']] = \
self.indiv_load_ratios[self.c['load_m3']].apply(
lambda x: np.nan_to_num(np.append(0, x)))
# household load fractions in the total load
self.indiv_load_ratios['ann_cons_fract'] = ann_cons_fract
# for the distribution system
self.load_max = self.load.max()
# main system component sizes
self.sys_sizes = sys_sizes
# backup system component sizes
self.backup_sizes = backup_sizes
self.timestep = timestep
self.num_timesteps = len(self.load)
@property
def weather(self):
return self.__weather
@weather.setter
def weather(self, value):
"""Re-extracts weather timeseries if a new weather dataset
is assigned to an instance
"""
self.__weather = value
if isinstance(value, pd.DataFrame):
self.t_amb = UnitConv(
self.weather[self.c['t_amb_C']].values).degC_K(unit_in='degC')
self.t_wet_bulb = UnitConv(
self.weather[self.c['t_wet_bulb_C']].values).degC_K(
unit_in='degC')
self.inc_rad = self.weather[self.c['irrad_on_tilt']].values
self.t_main = UnitConv(
self.weather[self.c['t_main_C']].values).degC_K(
unit_in='degC')
self.month = np.append(1, self.weather[self.c['month']].values)
self.season = np.append(
'winter',
self.weather[self.c['season']].values)
self.it_is_summer = self.season == self.c['summer']
self.it_is_winter = self.season == self.c['winter']
self.day = np.append(1, self.weather[self.c['day']].values)
self.hour = np.append(1, self.weather[self.c['hour']].values)
msg = 'Assigned weather data timeseries.'
log.info(msg)
elif not value:
self.t_amb = 293.15 # K
self.t_wet_bulb = 283.15 # K
self.inc_rad = 800 # W
self.t_main = 291.15 # K
self.month = None
self.day = None
self.season = None
msg = 'No weather data got passed to converters. '\
'Setting default scalar values for ambient temperature, '\
'{}, and solar irradiation, {}.'
log.info(msg.format(self.t_amb, self.inc_rad))
def solar_thermal(self, backup='gas'):
"""Connects the components of the solar thermal system and
simulates it in discrete timesteps.
Parameters:
backup: string
retrofit - pulls from the basecase for each household
gas, electric - instantaneous WHs (new installations)
Returns:
self.cons_total: pd df
Consumer level energy use [W], heat rates [W],
average temperatures [K],
and solar fraction for the analysis period.
proj_total: pd series
Project level energy use [W], heat rates [W],
average temperatures [K],
and solar fraction for the analysis period.
sol_fra: dict
Solar fraction. Keys: 'annual', 'monthly'
pump_el_use: dict
Electricity use broken into end uses
'dist_pump' - distribution pump, if present
'sol_pump' - solar pump
ts_res: pd df
Timestep project level results for all energy uses [W],
heat rates [W], temperatures [K], and the load.
backup_ts_cons: dict of dicts
Timestep household level results for energy uses [W],
and heat rates [W].
rel_err: float
Balancing error due to limitations of finite
timestep averaging.
"""
# initiate a dataframe with hourly results
ts_res = pd.DataFrame(index=range(0, self.num_timesteps + 1))
# components
converters = Converter(
params=self.sys_params,
weather=self.weather,
sizes=self.sys_sizes,
log_level=self.log_level)
indirect_tank = Storage(
params=self.sys_params,
type='sol_tank',
size=self.sys_sizes,
timestep=self.timestep,
log_level=self.log_level)
# Initiate arrays to hold the results during calculation
# (improved performance with arrays, assigning to df post-calc)
T_coil_out = np.empty(self.num_timesteps + 1)
Q_sol_col = np.zeros(self.num_timesteps + 1)
# demand heat rate
Q_dem = np.zeros(self.num_timesteps + 1)
Q_dem_with_dist_loss = np.zeros(self.num_timesteps + 1)
q_dem_balance = np.zeros(self.num_timesteps + 1)
# input solar tank gain and loss heat rates
Q_coil_sol_tank = np.zeros(self.num_timesteps + 1)
Q_loss_lower_sol_tank = np.zeros(self.num_timesteps + 1)
Q_loss_upper_sol_tank = np.zeros(self.num_timesteps + 1)
# output solar tank heat rates
Q_del_sol_tank = np.zeros(self.num_timesteps + 1)
Q_unmet_sol_tank = np.zeros(self.num_timesteps + 1)
Q_dump_sol_tank = np.zeros(self.num_timesteps + 1)
Q_ovrcool_sol_tank = np.zeros(self.num_timesteps + 1)
# tank temperature states
T_upper_sol_tank = np.empty(self.num_timesteps + 1)
T_lower_sol_tank = np.empty(self.num_timesteps + 1)
T_set = np.empty(self.num_timesteps + 1)
dT_dist_loss = np.empty(self.num_timesteps + 1)
Q_dist_loss = np.empty(self.num_timesteps + 1)
# instantaneous gas wh states
Q_del_bckp = np.zeros(self.num_timesteps + 1)
Q_gas_use_bckp = np.zeros(self.num_timesteps + 1)
Q_unmet = np.zeros(self.num_timesteps + 1)
# pump on time timestep fractions
Q_pump_on_fraction = np.empty(self.num_timesteps + 1)
# set values for the initial timestep
T_coil_out[0] = self.t_main[0]
T_upper_sol_tank[0] = self.t_main[0]
T_lower_sol_tank[0] = self.t_main[0]
# simulate main system
for ts in range(self.num_timesteps):
sol_col_res = converters.solar_collector(
T_coil_out[ts],
t_amb=self.t_amb[ts],
inc_rad=self.inc_rad[ts])
# gross collector gain that gets fed into the tank coil
Q_sol_col[ts] = sol_col_res['Q_gain']
sto_res = indirect_tank.thermal_tank(
pre_T_amb=self.t_amb[ts],
pre_T_feed=self.t_main[ts],
pre_T_upper=T_upper_sol_tank[ts],
pre_T_lower=T_lower_sol_tank[ts],
pre_V_tap=self.load[ts],
pre_Q_in=Q_sol_col[ts],
max_V_tap=self.load_max)
# solar collector return temperature (from tank)
T_coil_out[ts + 1] = sto_res[self.r['t_coil_out']]
# demand heat rate
Q_dem[ts + 1] = sto_res[self.r['q_dem']]
Q_dem_with_dist_loss[ts + 1] = sto_res[self.r['q_dem_tot']]
q_dem_balance[ts + 1] = sto_res[self.r['q_dem_balance']]
# solar tank heat sources
Q_coil_sol_tank[ts + 1] = sto_res[self.r['q_del_sol']]
Q_ovrcool_sol_tank[ts + 1] = sto_res[self.r['q_ovrcool_tank']]
# solar tank heat sinks
Q_loss_lower_sol_tank[ts + 1] = sto_res[self.r['q_loss_low']]
Q_loss_upper_sol_tank[ts + 1] = sto_res[self.r['q_loss_up']]
Q_del_sol_tank[ts + 1] = sto_res[self.r['q_del_tank']]
Q_unmet_sol_tank[ts + 1] = sto_res[self.r['q_unmet_tank']]
Q_dump_sol_tank[ts + 1] = sto_res[self.r['q_dump']]
# tank temperatures and set temperature
T_upper_sol_tank[ts + 1] = sto_res[self.r['t_tank_up']]
T_lower_sol_tank[ts + 1] = sto_res[self.r['t_tank_low']]
T_set[ts + 1] = sto_res[self.r['t_set']]
# distribution system
dT_dist_loss[ts + 1] = sto_res[self.r['dt_dist']]
Q_dist_loss[ts + 1] = sto_res[self.r['q_dist_loss']]
Q_pump_on_fraction[ts + 1] = sto_res[self.r['flow_on_frac']]
# simulate backup heater and assign household level gas
# consumption
if backup == 'gas':
backup_proj_total, backup_ts_proj, backup_ts_cons = \
self._gas_instantaneous_backup(
Q_unmet_sol_tank,
Q_dist_loss)
elif backup == 'retrofit':
backup_proj_total, backup_ts_proj, backup_ts_cons = \
self._gas_tank_backup(
(np.append(0., T_upper_sol_tank[:self.num_timesteps]) -
np.append(0., dT_dist_loss[:self.num_timesteps])),
np.append(0., T_upper_sol_tank[:self.num_timesteps]))
else:
msg = 'Backup types supported are gas or retrofit. '\
'Please provide one of the supported types.'
log.error(msg)
raise ValueError
# Please be aware that outputs in step (ts + 1) are results
# for the inputs in step ts
ts_res[self.r['q_dem']] = Q_dem
ts_res[self.r['q_dem_tot']] = Q_dem_with_dist_loss
ts_res[self.r['q_dem_balance']] = q_dem_balance
ts_res[self.r['q_del_sol']] = Q_coil_sol_tank
ts_res[self.r['q_loss_low']] = Q_loss_lower_sol_tank
ts_res[self.r['q_loss_up']] = Q_loss_upper_sol_tank
ts_res[self.r['q_del_tank']] = Q_del_sol_tank
ts_res[self.r['q_unmet_tank']] = Q_unmet_sol_tank
ts_res[self.r['q_dump']] = Q_dump_sol_tank
ts_res[self.r['q_ovrcool_tank']] = Q_ovrcool_sol_tank
ts_res[self.r['t_tank_up']] = T_upper_sol_tank
ts_res[self.r['t_tank_low']] = T_lower_sol_tank
ts_res[self.r['t_coil_out']] = T_coil_out
ts_res[self.r['t_set']] = T_set
ts_res[self.r['dt_dist']] = dT_dist_loss
ts_res[self.r['q_dist_loss']] = Q_dist_loss
ts_res[self.r['q_dist_loss_at_bckp']] = np.minimum(
Q_dist_loss,
Q_unmet_sol_tank) * (Q_unmet_sol_tank > 0.)
ts_res[self.r['q_dist_loss_at_bckp_sum']] = np.minimum(
Q_dist_loss,
Q_unmet_sol_tank) * (Q_unmet_sol_tank > 0.) * self.it_is_summer
ts_res[self.r['q_dist_loss_at_bckp_win']] = np.minimum(
Q_dist_loss,
Q_unmet_sol_tank) * (Q_unmet_sol_tank > 0.) * self.it_is_winter
ts_res[self.r['q_del_bckp']] = backup_ts_proj[self.r['q_del_bckp']]
ts_res[self.r['gas_use']] = backup_ts_proj[self.r['gas_use']]
ts_res[self.r['gas_use_s']] = backup_ts_proj[self.r['gas_use_s']]
ts_res[self.r['gas_use_w']] = backup_ts_proj[self.r['gas_use_w']]
ts_res[self.r['gas_use_no_dist']] = backup_ts_proj[
self.r['gas_use_no_dist']]
ts_res[self.r['gas_use_s_no_dist']] = backup_ts_proj[
self.r['gas_use_s_no_dist']]
ts_res[self.r['gas_use_w_no_dist']] = backup_ts_proj[
self.r['gas_use_w_no_dist']]
ts_res[self.r['q_unmet']] = backup_ts_proj[self.r['q_unmet']]
ts_res[self.r['q_del']] = (Q_del_sol_tank +
backup_ts_proj[self.r['q_del_bckp']])
# store timestep weather and water main inputs
ts_res[self.c['t_amb']] = np.append(0., self.t_amb)
ts_res[self.c['t_main']] = np.append(0., self.t_main)
# store load
ts_res[self.r['proj_load']] = np.append(0., self.load)
# get average temperatures and total heat rates
t_columns = [col for col in ts_res.columns if 'Temperature' in col]
# all other columns contain timestep heat rate
q_columns = [x for x in (set(ts_res.columns) - set(t_columns))]
# project results
proj_total = ts_res.sum()
proj_total[t_columns] = ts_res[t_columns].mean()
proj_total[self.r['proj_load']] = ts_res[self.r['proj_load']].mean()
if not round(proj_total[self.r['gas_use']], 4) == \
round(backup_proj_total[self.r['gas_use']], 4):
msg = 'Check project output of the backup - timestep '\
'sum preformed in this method and the total are '\
'not equal.'
log.error(msg)
raise Exception
# household results (annual quantities split according
# to individual loads)
self.cons_total[self.r['q_dem']] = self.indiv_load_ratios[
self.c['load_m3']].apply(
lambda x: (x * Q_dem).sum())
self.cons_total[self.r['q_del']] = (self.timestep *
self.indiv_load_ratios[self.c['load_m3']].apply(
lambda x: (x * Q_del_sol_tank).sum()) +
self.cons_total[self.r['q_del_bckp']])
self.cons_total[self.r['q_unmet_tank']] = (self.timestep *
self.indiv_load_ratios[self.c['load_m3']].apply(
lambda x: (x * Q_unmet_sol_tank).sum()))
self.cons_total[self.r['q_del_tank']] = (self.timestep *
self.indiv_load_ratios[self.c['load_m3']].apply(
lambda x: (x * Q_del_sol_tank).sum()))
self.cons_total[self.r['q_dist_loss']] = (self.timestep *
self.indiv_load_ratios[self.c['load_m3']].apply(
lambda x: (x * Q_dist_loss).sum()))
self.cons_total[self.r['q_dist_loss_at_bckp']] = (self.timestep *\
self.indiv_load_ratios[self.c['load_m3']].apply(
lambda x: (x * ts_res[
self.r['q_dist_loss_at_bckp']].values).sum()))
self.cons_total[self.r['q_dist_loss_at_bckp_win']] = (self.timestep *
self.indiv_load_ratios[self.c['load_m3']].apply(
lambda x: (x * ts_res[
self.r['q_dist_loss_at_bckp_win']].values).sum()))
self.cons_total[self.r['q_dist_loss_at_bckp_sum']] = (self.timestep *
self.indiv_load_ratios[self.c['load_m3']].apply(
lambda x: (x * ts_res[
self.r['q_dist_loss_at_bckp_sum']].values).sum()))
# Calculate pump electricity consumption
if self.community:
dist_pump_on_timestep_frac = Q_pump_on_fraction * 1.
else:
# single households do not require
# a distribution pump due to the
# water main pressure
dist_pump_on_timestep_frac = None
pump_ts_el_use, pump_el_use, pump_op_hour = \
self._get_pump_energy_use(
dist_pump_on_timestep_frac=dist_pump_on_timestep_frac,
gain_from_collector=Q_coil_sol_tank)
# Store project level electricity consumption
proj_total[self.r['el_use']] = pump_el_use['total']
proj_total[self.r['el_use_s']] = (
pump_ts_el_use['total'] * self.it_is_summer).sum()
proj_total[self.r['el_use_w']] = (
pump_ts_el_use['total'] * self.it_is_winter).sum()
# Assign household level electricity consumption
self.cons_total = self._split_utility(
self.cons_total,
value=proj_total[self.r['el_use']],
var=self.r['el_use'],
on=self.c['occ'],
drop=False)
self.cons_total = self._split_utility(
self.cons_total,
value=proj_total[self.r['el_use_s']],
var=self.r['el_use_s'],
on=self.c['occ'],
drop=False)
self.cons_total = self._split_utility(
self.cons_total,
value=proj_total[self.r['el_use_w']],
var=self.r['el_use_w'],
on=self.c['occ'],
drop=False)
# project level solar fraction
sol_fra = dict()
sol_fra['annual'] = (proj_total[self.r['q_dem']] -
proj_total[self.r['q_del_bckp']]) / proj_total[self.r['q_dem']]
proj_total[self.r['sol_fra']] = sol_fra['annual']
# get annual and monthly project level solar fractions
if self.month is not None:
sol_fra_hourly = pd.DataFrame()
sol_fra_hourly[self.c['month']] = self.month
sol_fra_hourly[self.c['season']] = self.season
sol_fra_hourly['hourly'] = (ts_res[self.r['q_dem']] -
ts_res[self.r['q_del_bckp']]) / ts_res[self.r['q_dem']]
sol_fra['monthly'] = sol_fra_hourly.groupby(
self.c['month']).mean().rename(
columns={'hourly': self.r['month_sol_fra']})
sol_fra['seasonal'] = sol_fra_hourly.groupby(
self.c['season']).mean().rename(
columns={'hourly': self.r['season_sol_fra']})
sol_fra['seasonal'] = sol_fra['seasonal'].drop(
columns=self.c['month'])
# household level solar fraction
self.cons_total[self.r['sol_fra']] = \
self.indiv_load_ratios.apply(
lambda row: (row[self.c['load_m3']] * (
ts_res[self.r['q_dem']] -
ts_res[self.r['q_del_bckp']])).sum() /
(row['ann_cons_fract'] *
proj_total[self.r['q_dem']]),
axis=1)
# check annual tank balance
_in = proj_total[self.r['q_del_sol']]
_outs = (proj_total[self.r['q_del_tank']] +
proj_total[self.r['q_dump']] +
proj_total[self.r['q_loss_up']] +
proj_total[self.r['q_loss_low']] -
proj_total[self.r['q_ovrcool_tank']])
rel_err = abs(_in - _outs) / _in
if rel_err > .01:
msg = 'Solar tank balance error is {}.'
log.warning(msg.format(rel_err))
# time indices for timestep results only
ts_res[self.c['month']] = self.month
ts_res[self.c['season']] = self.season
ts_res[self.c['day']] = self.day
ts_res[self.c['hour']] = self.hour
proj_total[self.c['max_load']] = UnitConv(self.load_max).m3_gal(
unit_in='m3')
return self.cons_total.round(2), proj_total.round(2), [
proj_total.round(2).to_dict(), sol_fra, pump_el_use,
pump_op_hour, ts_res,
backup_ts_cons, rel_err]
def solar_electric(self, backup='electric'):
"""Connects the components of the
solar electric system and enables simulation.
Parameters:
backup: string
electric - instantaneous WHs (new installations)
Returns:
sys_en_use: dict
System level energy use for the analysis period:
'electricity', Wh
sol_fra: dict
Solar fraction. Keys: 'annual', 'monthly'
ts_res: pd df
Colums populated with state variable timeseries, such as
average timstep heat rates and temperatures
res: dict
Summarizes ts_res. Any heat rates are summed, while the
temperatures are averaged for the analysis period
(usually one year)
el_use: dict
Electricity use broken into end uses
'dist_pump' - distribution pump, if present
rel_err: float
Balancing error due to limitations of finite
timestep averaging. More precisely, due to
selecting minimum tank temperature
as the lower between the water main and the ambient.
"""
# initiate a dataframe with hourly results
ts_res = pd.DataFrame(index=range(0, self.num_timesteps + 1))
# components
converters = Converter(
params=self.sys_params,
weather=self.weather,
sizes=self.sys_sizes)
hp_tank = Storage(
params=self.sys_params,
size=self.sys_sizes,
timestep=self.timestep,
type='hp_tank')
# Initiate arrays to hold the results during calculation
# (improved performance with arrays, assigning to df post-calc)
# Heat pump electricity usage
P_hp = np.zeros(self.num_timesteps + 1)
# Photovoltaic gain (before and after inverter)
P_pv_ac = np.zeros(self.num_timesteps + 1)
P_pv_dc = np.zeros(self.num_timesteps + 1)
# Electric power generated by PV, usages
P_pv_to_hp = np.zeros(self.num_timesteps + 1)
P_pv_after_hp = np.zeros(self.num_timesteps + 1)
# Electric power available for feeding back to grid or
# powering e.g. household appliances
P_surplus = np.zeros(self.num_timesteps + 1)
# demand heat rate
Q_dem = np.zeros(self.num_timesteps + 1)
Q_dem_with_dist_loss = np.zeros(self.num_timesteps + 1)
q_dem_balance = np.zeros(self.num_timesteps + 1)
# input balance on the heat pump tank
Q_hp = np.zeros(self.num_timesteps + 1)
Q_loss_lower_hp_tank = np.zeros(self.num_timesteps + 1)
Q_loss_upper_hp_tank = np.zeros(self.num_timesteps + 1)
# output balance on the heat pump tank
Q_del_hp_tank = np.zeros(self.num_timesteps + 1)
Q_unmet_hp_tank = np.zeros(self.num_timesteps + 1)
Q_dump_hp_tank = np.zeros(self.num_timesteps + 1)
Q_ovrcool_hp_tank = np.zeros(self.num_timesteps + 1)
# tank temperature states
T_upper_hp_tank = np.zeros(self.num_timesteps + 1)
T_lower_hp_tank = np.zeros(self.num_timesteps + 1)
# set values for the initial timestep
T_upper_hp_tank[0] = self.t_main[0]
T_lower_hp_tank[0] = self.t_main[0]
# ambient air and main water temperatures
T_main = np.zeros(self.num_timesteps + 1)
T_wet_bulb = np.zeros(self.num_timesteps + 1)
T_amb = np.zeros(self.num_timesteps + 1)
# set initial values
T_main[0] = self.t_main[0]
T_wet_bulb[0] = self.t_wet_bulb[0]
T_amb[0] = self.t_amb[0]
# intial HP status
hp_status = True
dt_hist = 5.
T_limit = hp_tank.T_max
for ts in range(self.num_timesteps):
# Use upper tank temperature, since this will be tapped and has
# to meet T_draw_set
T_tank = T_upper_hp_tank[ts]
# hp uses temperature hysteresis as on/off control
if (((hp_status) and (T_tank < T_limit)) or
((not hp_status) and (T_tank < (T_limit - dt_hist)))):
hp_res = converters.heat_pump(
T_wet_bulb=self.t_wet_bulb[ts],
T_tank=T_tank)
# enable heat pump (or keep it on)
hp_status = True
else:
# Set heat pump results to zero
hp_res = {'heat_cap': 0.,
'el_use': 0.,
'cop': 0.}
# disable heat pump (or leave it off)
hp_status = False
# hp heat gain to the tank (updated in
# each timestep, recorded later as
# output of the hp tank)
Q_hp_cap = hp_res['heat_cap']
# electricity use of the heat pump
P_hp[ts] = hp_res['el_use']
pv_res = converters.photovoltaic(
use_p_peak=False,
inc_rad=self.inc_rad[ts])
# Electric power produced by photovoltaic module
P_pv_ac[ts] = pv_res['ac']
P_pv_dc[ts] = pv_res['dc']
# Calculate electric power generated by PV used for supplying
# the HP
P_pv_to_hp[ts] = min(P_hp[ts], P_pv_ac[ts])
# Calculate amount of electricity available for other
# applications
P_pv_after_hp[ts] = max((P_pv_ac[ts] - P_hp[ts]), 0.)
sto_res = hp_tank.thermal_tank(
pre_T_amb=self.t_amb[ts],
pre_T_feed=self.t_main[ts],
pre_T_upper=T_upper_hp_tank[ts],
pre_T_lower=T_lower_hp_tank[ts],
pre_V_tap=self.load[ts],
pre_Q_in=Q_hp_cap,
max_V_tap=self.load_max)
# demand heat rate
Q_dem[ts + 1] = sto_res[self.r['q_dem']]
Q_dem_with_dist_loss[ts + 1] = sto_res[self.r['q_dem_tot']]
q_dem_balance[ts + 1] = sto_res[self.r['q_dem_balance']]
# heat pump tank heat sources
Q_hp[ts + 1] = sto_res[self.r['q_del_hp']]
Q_ovrcool_hp_tank[ts + 1] = sto_res[self.r['q_ovrcool_tank']]
# heat pump tank heat sinks
Q_loss_lower_hp_tank[ts + 1] = sto_res[self.r['q_loss_low']]
Q_loss_upper_hp_tank[ts + 1] = sto_res[self.r['q_loss_up']]
Q_del_hp_tank[ts + 1] = sto_res[self.r['q_del_tank']]
Q_unmet_hp_tank[ts + 1] = sto_res[self.r['q_unmet_tank']]
Q_dump_hp_tank[ts + 1] = sto_res[self.r['q_dump']]
# resulting temperatures in the heat pump tank
T_upper_hp_tank[ts + 1] = sto_res[self.r['t_tank_up']]
T_lower_hp_tank[ts + 1] = sto_res[self.r['t_tank_low']]
# set ambient air and main water temperatures
T_main[ts + 1] = self.t_main[ts]
T_wet_bulb[ts + 1] = self.t_wet_bulb[ts]
T_amb[ts + 1] = self.t_amb[ts]
# simulate backup heater and assign household level
# electricity consumption
if backup == 'electric':
backup_proj_total, backup_ts_proj, \
backup_ts_cons, P_pv_after_bckp = \
self._electric_instantaneous_backup(
Q_unmet_hp_tank,
P_pv_after_hp)
else:
msg = 'Backup types supported are: \'electric\'. '\
'Please provide one of the supported types.'
log.error(msg)
raise ValueError
# store timestep results in a dataframe
ts_res[self.r['q_dem']] = Q_dem
ts_res[self.r['q_del_hp']] = Q_hp
ts_res[self.r['q_del_bckp']] = backup_ts_proj[self.r['q_del_bckp']]
ts_res[self.r['q_loss_low']] = Q_loss_lower_hp_tank
ts_res[self.r['q_loss_up']] = Q_loss_upper_hp_tank
ts_res[self.r['q_del_tank']] = Q_del_hp_tank
ts_res[self.r['q_unmet_tank']] = Q_unmet_hp_tank
ts_res[self.r['q_dump']] = Q_dump_hp_tank
ts_res[self.r['q_ovrcool_tank']] = Q_ovrcool_hp_tank
ts_res[self.r['t_tank_up']] = T_upper_hp_tank
ts_res[self.r['t_tank_low']] = T_lower_hp_tank
# PV generated electricity (AC)
ts_res[self.r['p_pv_ac']] = P_pv_ac
# PV generated electricity (DC)
ts_res[self.r['p_pv_dc']] = P_pv_dc
# total heat gain (heat pump and electric resistance)
ts_res[self.r['q_del']] = Q_hp + backup_ts_proj[self.r['q_del_bckp']]
ts_res[self.r['q_unmet']] = backup_ts_proj[self.r['q_unmet']]
# Usage of electric power generated by PV
ts_res[self.r['p_pv_to_hp']] = P_pv_to_hp
ts_res[self.r['p_pv_to_el_res']] = P_pv_after_bckp['used_for_bckp']
# Electricity usage (from grid and PV)
# Heat Pump
ts_res[self.r['p_hp_el_use']] = P_hp
# Electric Resistance
ts_res[self.r['p_el_res_use']] = backup_ts_proj[self.r['grs_el_use']]
# also include the water and air temperatures in the results
ts_res[self.r['t_main']] = T_main
ts_res[self.r['t_amb']] = T_amb
ts_res[self.r['t_wet_bulb']] = T_wet_bulb
# get average temperatures and total heat rates
t_columns = [col for col in ts_res.columns if 'Temperature' in col]
# all other columns contain timestep heat rate
q_columns = [x for x in (set(ts_res.columns) - set(t_columns))]
# project results
proj_total = ts_res.sum()
proj_total[t_columns] = ts_res[t_columns].mean()
# household results (annual quantities split according
# to individual loads)
self.cons_total[self.r['q_dem']] = self.indiv_load_ratios[
self.c['load_m3']].apply(
lambda x: (x * Q_dem).sum())
self.cons_total[self.r['q_del']] = (self.indiv_load_ratios[
self.c['load_m3']].apply(
lambda x: (x * Q_del_hp_tank).sum()) +
self.cons_total[self.r['q_del_bckp']])
self.cons_total[self.r['q_unmet_tank']] = self.indiv_load_ratios[
self.c['load_m3']].apply(
lambda x: (x * Q_unmet_hp_tank).sum())
self.cons_total[self.r['q_del_tank']] = self.indiv_load_ratios[
self.c['load_m3']].apply(
lambda x: (x * Q_del_hp_tank).sum())
# Calculate pump electricity use
if self.community:
dist_pump_on_timestep_frac = Q_del_hp_tank + 0.
else:
# single households do not require
# a distribution pump due to the
# water main pressure
dist_pump_on_timestep_frac = None
pump_ts_el_use, pump_el_use, pump_op_hour = \
self._get_pump_energy_use(
dist_pump_on_timestep_frac=dist_pump_on_timestep_frac)
# if there is any PV power left, use it for the pumps
pump_ts_el_use_after_pv = (pump_ts_el_use['total'] -
P_pv_after_bckp['rem_after'])
pump_ts_el_use_after_pv[pump_ts_el_use_after_pv < 0.] = 0.
pump_el_use['after_pv'] = pump_ts_el_use_after_pv.sum()
# Distribute pump el. use onto households and add to
# total electricity use
self.cons_total = self._split_utility(
self.cons_total,
value=pump_el_use['after_pv'],
var='pump_el_use',
on=self.c['occ'],
drop=False)
self.cons_total[self.r['el_use']] += self.cons_total['pump_el_use']
self.cons_total = self.cons_total.drop(
columns=['pump_el_use'])
# Total electricity use
ts_res[self.r['grs_el_use']] = (ts_res[self.r['p_hp_el_use']] +
ts_res[self.r['p_el_res_use']] +
pump_el_use['total'])
# Electricity available after hp, backup heater and pumps
P_surplus = P_pv_after_bckp['rem_after'] - pump_ts_el_use['total']
P_surplus[P_surplus < 0.] = 0.
ts_res[self.r['p_surplus']] = P_surplus
# project level solar fraction (excludes pumping energy)
sol_fra = dict()
# Calculate the fraction of HP el. use that came from the PV
# annual
fraction_pv_to_hp = (proj_total[self.r['p_pv_to_hp']] /
proj_total[self.r['p_hp_el_use']])
# annual
fraction_pv_to_el_res = (proj_total[self.r['p_pv_to_el_res']] /
proj_total[self.r['p_el_res_use']])
# for each timestep
# Calculate solar fraction between generated PV power and
# total electricity use
sol_fra['annual'] = ((proj_total[self.r['q_del_hp']] *
fraction_pv_to_hp +
proj_total[self.r['q_del_bckp']] * fraction_pv_to_el_res) /
proj_total[self.r['q_dem']])
# check annual tank balance
_in = proj_total[self.r['q_del_hp']]
_outs = (proj_total[self.r['q_del_tank']] +
proj_total[self.r['q_dump']] +
proj_total[self.r['q_loss_up']] +
proj_total[self.r['q_loss_low']] -
proj_total[self.r['q_ovrcool_tank']])
rel_err = abs(_in - _outs) / _in
if rel_err > .01:
msg = 'Heat pump tank balance error is {}.'
log.warning(msg.format(rel_err))
return self.cons_total, proj_total.round(2), [
proj_total.round(2).to_dict(),
sol_fra, pump_el_use,
pump_op_hour, ts_res, rel_err]
def conventional_gas_tank(self):
"""Basecase conventional gas tank water heater.
Make sure to size the tank according to the recommended
sizing rules, since the WHAM model does not apply to
tanks that are not appropriately sized.
Returns:
ts_proj: dict of arrays, W
Heat:
* self.r['q_del']: delivered
* self.r['gas_use']: gas consumed
"""
# components
heater = Storage(
params=self.sys_params,
size=self.sys_sizes,
timestep=self.timestep,
type='wham_tank',