-
Notifications
You must be signed in to change notification settings - Fork 2
/
Energy_Models_DigitalTwin.py
932 lines (775 loc) · 37.2 KB
/
Energy_Models_DigitalTwin.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
from gym import spaces
import numpy as np
class Building:
def __init__(
self,
buildingId,
dhw_storage=None,
cooling_storage=None,
electrical_storage=None,
dhw_heating_device=None,
cooling_device=None,
save_memory=True,
):
"""
Args:
buildingId (int)
dhw_storage (EnergyStorage)
cooling_storage (EnergyStorage)
electrical_storage (Battery)
dhw_heating_device (ElectricHeater or HeatPump)
cooling_device (HeatPump)
"""
# Building attributes
self.building_type = None
self.climate_zone = None
self.solar_power_capacity = None
self.buildingId = buildingId
self.dhw_storage = dhw_storage
self.cooling_storage = cooling_storage
self.electrical_storage = electrical_storage
self.dhw_heating_device = dhw_heating_device
self.cooling_device = cooling_device
self.observation_space = None
self.action_space = None
self.time_step = 0
self.sim_results = {}
self.save_memory = save_memory
if self.dhw_storage is not None:
self.dhw_storage.reset()
if self.cooling_storage is not None:
self.cooling_storage.reset()
if self.electrical_storage is not None:
self.electrical_storage.reset()
if self.dhw_heating_device is not None:
self.dhw_heating_device.reset()
if self.cooling_device is not None:
self.cooling_device.reset()
self._electric_consumption_cooling_storage = 0.0
self._electric_consumption_dhw_storage = 0.0
self.cooling_demand_building = []
self.dhw_demand_building = []
self.electric_consumption_appliances = []
self.electric_generation = []
self.electric_consumption_cooling = []
self.electric_consumption_cooling_storage = []
self.electric_consumption_dhw = []
self.electric_consumption_dhw_storage = []
self.net_electric_consumption = []
self.net_electric_consumption_no_storage = []
self.net_electric_consumption_no_pv_no_storage = []
self.cooling_device_to_building = []
self.cooling_storage_to_building = []
self.cooling_device_to_storage = []
self.cooling_storage_soc = []
self.dhw_heating_device_to_building = []
self.dhw_storage_to_building = []
self.dhw_heating_device_to_storage = []
self.dhw_storage_soc = []
self.electrical_storage_electric_consumption = []
self.electrical_storage_soc = []
def set_state_space(self, high_state, low_state):
# Setting the state space and the lower and upper bounds of each state-variable
self.observation_space = spaces.Box(
low=low_state, high=high_state, dtype=np.float32
)
def set_action_space(self, max_action, min_action):
# Setting the action space and the lower and upper bounds of each action-variable
self.action_space = spaces.Box(
low=min_action, high=max_action, dtype=np.float32
)
def set_storage_electrical(self, action, C_p_bat, SOC_bat):
"""
Args:
action (float): Amount of heating energy stored (added) in that time-step as a ratio of the maximum capacity of the energy storage device.
-1 =< action < 0 : Energy Storage Unit releases energy into the building and its State of Charge decreases
0 < action <= 1 : Energy Storage Unit receives energy from the energy supply device and its State of Charge increases
The actions are always subject to the constraints of the power capacity of the heating supply unit, the DHW demand of the
building (which limits the maximum amount of DHW that the energy storage can provide to the building), and the state of charge of the
energy storage unit itself
Return:
elec_demand_heating (float): electricity consumption needed for space heating and heating storage
"""
electrical_energy_balance = self.electrical_storage.charge(
action * C_p_bat, SOC_bat
)
if self.save_memory == False:
self.electrical_storage_electric_consumption.append(
electrical_energy_balance
)
self.electrical_storage_soc.append(SOC_bat)
self.electrical_storage.time_step += 1
return electrical_energy_balance
def set_storage_heating(self, action, E_ehH_max, C_p_Hsto, SOC_Hsto, H_bd):
"""
Args:
action (float): Amount of heating energy stored (added) in that time-step as a ratio of the maximum capacity of the energy storage device.
-1 =< action < 0 : Energy Storage Unit releases energy into the building and its State of Charge decreases
0 < action <= 1 : Energy Storage Unit receives energy from the energy supply device and its State of Charge increases
The actions are always subject to the constraints of the power capacity of the heating supply unit, the DHW demand of the
building (which limits the maximum amount of DHW that the energy storage can provide to the building), and the state of charge of the
energy storage unit itself
Return:
elec_demand_heating (float): electricity consumption needed for space heating and heating storage
"""
# Heating power that could be possible to supply to the storage device to increase its State of Charge once the heating demand of the building has been satisfied
heat_power_avail = E_ehH_max - H_bd
# The storage device is charged (action > 0) or discharged (action < 0) taking into account the max power available and that the storage device cannot be discharged by an amount of energy greater than the energy demand of the building.
heating_energy_balance = self.dhw_storage.charge(
max(-H_bd, min(heat_power_avail, action * C_p_Hsto)), SOC_Hsto
)
if self.save_memory == False:
self.dhw_heating_device_to_storage.append(max(0, heating_energy_balance))
self.dhw_storage_to_building.append(-min(0, heating_energy_balance))
self.dhw_heating_device_to_building.append(
H_bd + min(0, heating_energy_balance)
)
self.dhw_storage_soc.append(SOC_Hsto)
# The energy that the energy supply device must provide is the sum of the energy balance of the storage unit (how much net energy it will lose or get) plus the energy supplied to the building. A constraint is added to guarantee it's always positive.
heating_energy_balance = max(0, heating_energy_balance + H_bd)
# Electricity consumed by the energy supply unit
elec_demand_heating = (
self.dhw_heating_device.set_total_electric_consumption_heating(
heat_supply=heating_energy_balance
)
)
# Electricity consumption used (if +) or saved (if -) due to the change in the state of charge of the energy storage device
self._electric_consumption_dhw_storage = (
elec_demand_heating
- self.dhw_heating_device.get_electric_consumption_heating(heat_supply=H_bd)
)
if self.save_memory == False:
self.electric_consumption_dhw.append(elec_demand_heating)
self.electric_consumption_dhw_storage.append(
self._electric_consumption_dhw_storage
)
self.dhw_heating_device.time_step += 1
return elec_demand_heating
def set_storage_cooling(self, action, C_p_Csto, SOC_Csto, C_bd, COP_C, E_hpC_max):
"""
Args:
action (float): Amount of cooling energy stored (added) in that time-step as a ratio of the maximum capacity of the energy storage device.
1 =< action < 0 : Energy Storage Unit releases energy into the building and its State of Charge decreases
0 < action <= -1 : Energy Storage Unit receives energy from the energy supply device and its State of Charge increases
The actions are always subject to the constraints of the power capacity of the cooling supply unit, the cooling demand of the
building (which limits the maximum amount of cooling energy that the energy storage can provide to the building), and the state of charge of the energy storage unit itself
Return:
elec_demand_cooling (float): electricity consumption needed for space cooling and cooling storage
"""
# Cooling power that could be possible to supply to the storage device to increase its State of Charge once the heating demand of the building has been satisfied
cooling_power_avail = (self.cooling_device.get_max_cooling_power(COP_C, E_hpC_max) - C_bd)
# The storage device is charged (action > 0) or discharged (action < 0) taking into account the max power available and that the storage device cannot be discharged by an amount of energy greater than the energy demand of the building.
cooling_energy_balance = self.cooling_storage.charge(
max(-C_bd, min(cooling_power_avail, action * C_p_Csto)), SOC_Csto
)
if self.save_memory == False:
self.cooling_device_to_storage.append(max(0, cooling_energy_balance))
self.cooling_storage_to_building.append(-min(0, cooling_energy_balance))
self.cooling_device_to_building.append(
C_bd + min(0, cooling_energy_balance)
)
self.cooling_storage_soc.append(SOC_Csto)
# The energy that the energy supply device must provide is the sum of the energy balance of the storage unit (how much net energy it will lose or get) plus the energy supplied to the building. A constraint is added to guarantee it's always positive.
cooling_energy_balance = max(0, cooling_energy_balance + C_bd)
# Electricity consumed by the energy supply unit
elec_demand_cooling = (
self.cooling_device.set_total_electric_consumption_cooling(
COP_C, cooling_supply=cooling_energy_balance
)
)
# Electricity consumption used (if +) or saved (if -) due to the change in the state of charge of the energy storage device
self._electric_consumption_cooling_storage = (
elec_demand_cooling
- self.cooling_device.get_electric_consumption_cooling(
COP_C, cooling_supply=C_bd
)
)
if self.save_memory == False:
self.electric_consumption_cooling.append(np.float32(elec_demand_cooling))
self.electric_consumption_cooling_storage.append(
np.float32(self._electric_consumption_cooling_storage)
)
self.cooling_device.time_step += 1
return elec_demand_cooling
def get_non_shiftable_load(self, E_NS):
return E_NS
def get_solar_power(self, solar_gen):
return solar_gen
def get_dhw_electric_demand(self):
return self.dhw_heating_device._electrical_consumption_heating
def get_cooling_electric_demand(self):
return self.cooling_device._electrical_consumption_cooling
def reset(self):
self.current_net_electricity_demand = (
self.sim_results["non_shiftable_load"][self.time_step]
- self.sim_results["solar_gen"][self.time_step]
)
if self.dhw_storage is not None:
self.dhw_storage.reset()
if self.cooling_storage is not None:
self.cooling_storage.reset()
if self.electrical_storage is not None:
self.electrical_storage.reset()
if self.dhw_heating_device is not None:
self.dhw_heating_device.reset()
self.current_net_electricity_demand += (
self.dhw_heating_device.get_electric_consumption_heating(
self.sim_results["dhw_demand"][self.time_step]
)
)
if self.cooling_device is not None:
self.cooling_device.reset()
self.current_net_electricity_demand += (
self.cooling_device.get_electric_consumption_cooling(
self.sim_results["cooling_demand"][self.time_step]
)
)
self._electric_consumption_cooling_storage = 0.0
self._electric_consumption_dhw_storage = 0.0
self.cooling_demand_building = []
self.dhw_demand_building = []
self.electric_consumption_appliances = []
self.electric_generation = []
self.electric_consumption_cooling = []
self.electric_consumption_cooling_storage = []
self.electric_consumption_dhw = []
self.electric_consumption_dhw_storage = []
self.net_electric_consumption = []
self.net_electric_consumption_no_storage = []
self.net_electric_consumption_no_pv_no_storage = []
self.cooling_device_to_building = []
self.cooling_storage_to_building = []
self.cooling_device_to_storage = []
self.SOC_Csto = []
self.dhw_heating_device_to_building = []
self.dhw_storage_to_building = []
self.dhw_heating_device_to_storage = []
self.SOC_Hsto = []
self.electrical_storage_electric_consumption = []
self.SOC_bat = []
def terminate(self):
if self.dhw_storage is not None:
self.dhw_storage.terminate()
if self.cooling_storage is not None:
self.cooling_storage.terminate()
if self.electrical_storage is not None:
self.electrical_storage.terminate()
if self.dhw_heating_device is not None:
self.dhw_heating_device.terminate()
if self.cooling_device is not None:
self.cooling_device.terminate()
if self.save_memory == False:
self.cooling_demand_building = np.array(
self.sim_results["cooling_demand"][: self.time_step]
)
self.dhw_demand_building = np.array(
self.sim_results["dhw_demand"][: self.time_step]
)
self.electric_consumption_appliances = np.array(
self.sim_results["non_shiftable_load"][: self.time_step]
)
self.electric_generation = np.array(
self.sim_results["solar_gen"][: self.time_step]
)
elec_consumption_dhw = 0
elec_consumption_dhw_storage = 0
if (
self.dhw_heating_device.time_step == self.time_step
and self.dhw_heating_device is not None
):
elec_consumption_dhw = np.array(self.electric_consumption_dhw)
elec_consumption_dhw_storage = np.array(
self.electric_consumption_dhw_storage
)
elec_consumption_cooling = 0
elec_consumption_cooling_storage = 0
if (
self.cooling_device.time_step == self.time_step
and self.cooling_device is not None
):
elec_consumption_cooling = np.array(self.electric_consumption_cooling)
elec_consumption_cooling_storage = np.array(
self.electric_consumption_cooling_storage
)
self.net_electric_consumption = (
np.array(self.electric_consumption_appliances)
+ elec_consumption_cooling
+ elec_consumption_dhw
- np.array(self.electric_generation)
)
self.net_electric_consumption_no_storage = (
np.array(self.electric_consumption_appliances)
+ (elec_consumption_cooling - elec_consumption_cooling_storage)
+ (elec_consumption_dhw - elec_consumption_dhw_storage)
- np.array(self.electric_generation)
)
self.net_electric_consumption_no_pv_no_storage = np.array(
self.net_electric_consumption_no_storage
) + np.array(self.electric_generation)
self.cooling_demand_building = np.array(self.cooling_demand_building)
self.dhw_demand_building = np.array(self.dhw_demand_building)
self.electric_consumption_appliances = np.array(
self.electric_consumption_appliances
)
self.electric_generation = np.array(self.electric_generation)
self.electric_consumption_cooling = np.array(
self.electric_consumption_cooling
)
self.electric_consumption_cooling_storage = np.array(
self.electric_consumption_cooling_storage
)
self.electric_consumption_dhw = np.array(self.electric_consumption_dhw)
self.electric_consumption_dhw_storage = np.array(
self.electric_consumption_dhw_storage
)
self.net_electric_consumption = np.array(self.net_electric_consumption)
self.net_electric_consumption_no_storage = np.array(
self.net_electric_consumption_no_storage
)
self.net_electric_consumption_no_pv_no_storage = np.array(
self.net_electric_consumption_no_pv_no_storage
)
self.cooling_device_to_building = np.array(self.cooling_device_to_building)
self.cooling_storage_to_building = np.array(
self.cooling_storage_to_building
)
self.cooling_device_to_storage = np.array(self.cooling_device_to_storage)
self.SOC_Csto = np.array(self.SOC_Csto)
self.dhw_heating_device_to_building = np.array(
self.dhw_heating_device_to_building
)
self.dhw_storage_to_building = np.array(self.dhw_storage_to_building)
self.dhw_heating_device_to_storage = np.array(
self.dhw_heating_device_to_storage
)
self.SOC_Hsto = np.array(self.SOC_Hsto)
self.electrical_storage_electric_consumption = np.array(
self.electrical_storage_electric_consumption
)
self.SOC_bat = np.array(self.SOC_bat)
class HeatPump:
def __init__(
self,
nominal_power,
eta_tech: float = 0.22,
t_target_heating=None,
t_target_cooling: int = 8,
save_memory=True,
):
"""
Args:
nominal_power (float): Maximum amount of electric power that the heat pump can consume from the power grid (given by the nominal power of the compressor)
eta_tech (float): Technical efficiency
t_target_heating (float): Temperature at which the heating energy is released
t_target_cooling (float): Temperature at which the cooling energy is released
"""
# Parameters
self.nominal_power = nominal_power
self.eta_tech = eta_tech
# self.COP_C = COP_C
# Variables
self.max_cooling = None
self.max_heating = None
# self._cop_heating = None
# self._cop_cooling = None
self.t_target_heating = t_target_heating
self.t_target_cooling = t_target_cooling
self.t_source_heating = None
self.t_source_cooling = None
# self.cop_heating = []
# self.cop_cooling = []
self.electrical_consumption_cooling = []
self.electrical_consumption_heating = []
self.heat_supply = []
self.cooling_supply = []
self.time_step = 0
self.save_memory = save_memory
def get_max_cooling_power(self, COP_C, max_electric_power=None):
"""
Args:
max_electric_power (float): Maximum amount of electric power that the heat pump can consume from the power grid
Returns:
max_cooling (float): maximum amount of cooling energy that the heatpump can provide
"""
if max_electric_power is None:
self.max_cooling = self.nominal_power * COP_C
else:
self.max_cooling = max_electric_power * COP_C
return self.max_cooling
def get_max_heating_power(self, E_ehH_max):
"""
Method that calculates the heating COP and the maximum heating power available
Args:
max_electric_power (float): Maximum amount of electric power that the heat pump can consume from the power grid
Returns:
max_heating (float): maximum amount of heating energy that the heatpump can provide
"""
# if max_electric_power is None:
# self.max_heating = self.nominal_power*self.cop_cooling[self.time_step]
self.max_heating = min(E_ehH_max, self.nominal_power) * COP_C
return self.max_heating
def set_total_electric_consumption_cooling(self, COP_C, cooling_supply=0):
"""
Method that calculates the total electricity consumption of the heat pump given an amount of cooling energy to be supplied to both the building and the storage unit
Args:
cooling_supply (float): Total amount of cooling energy that the heat pump is going to supply
Returns:
_electrical_consumption_cooling (float): electricity consumption for cooling
"""
self.cooling_supply.append(cooling_supply)
self._electrical_consumption_cooling = cooling_supply / COP_C
if self.save_memory == False:
self.electrical_consumption_cooling.append(
np.float32(self._electrical_consumption_cooling)
)
return self._electrical_consumption_cooling
def get_electric_consumption_cooling(self, COP_C, cooling_supply=0):
"""
Method that calculates the electricity consumption of the heat pump given an amount of cooling energy
Args:
cooling_supply (float): Amount of cooling energy
Returns:
_electrical_consumption_cooling (float): electricity consumption for that amount of cooling
"""
_elec_consumption_cooling = cooling_supply / COP_C
return _elec_consumption_cooling
def set_total_electric_consumption_heating(self, COP_C, heat_supply=0):
"""
Method that calculates the electricity consumption of the heat pump given an amount of heating energy to be supplied
Args:
heat_supply (float): Amount of heating energy that the heat pump is going to supply
Returns:
_elec_consumption_heating (float): electricity consumption for heating
"""
self.heat_supply.append(heat_supply)
self._electrical_consumption_heating = heat_supply / COP_C
if self.save_memory == False:
self.electrical_consumption_heating.append(
np.float32(self._electrical_consumption_heating)
)
return self._electrical_consumption_heating
def get_electric_consumption_heating(self, COP_C, heat_supply=0):
"""
Method that calculates the electricity consumption of the heat pump given an amount of heating energy to be supplied
Args:
heat_supply (float): Amount of heating energy that the heat pump is going to supply
Returns:
_elec_consumption_heating (float): electricity consumption for heating
"""
_elec_consumption_heating = heat_supply / COP_C
return _elec_consumption_heating
def reset(self):
self.t_source_heating = None
self.t_source_cooling = None
self.max_cooling = None
self.max_heating = None
self.COP_C = None
# self._cop_heating = None
# self._cop_cooling = None
self._electrical_consumption_cooling = 0
self._electrical_consumption_heating = 0
self.electrical_consumption_cooling = []
self.electrical_consumption_heating = []
self.heat_supply = []
self.cooling_supply = []
self.time_step = 0
def terminate(self):
if self.save_memory == False:
self.cop_heating = self.cop_heating[: self.time_step]
self.cop_cooling = self.cop_cooling[: self.time_step]
self.electrical_consumption_cooling = np.array(
self.electrical_consumption_cooling
)
self.electrical_consumption_heating = np.array(
self.electrical_consumption_heating
)
self.heat_supply = np.array(self.heat_supply)
self.cooling_supply = np.array(self.cooling_supply)
class ElectricHeater:
def __init__(self, nominal_power, efficiency: float = 0.9, save_memory=True):
"""
Args:
nominal_power (float): Maximum amount of electric power that the electric heater can consume from the power grid
efficiency (float): efficiency
"""
# Parameters
self.nominal_power = nominal_power
self.efficiency = 0.9
# Variables
self.max_heating = None
self.electrical_consumption_heating = []
self._electrical_consumption_heating = 0
self.heat_supply = []
self.time_step = 0
self.save_memory = save_memory
def terminate(self):
if self.save_memory == False:
self.electrical_consumption_heating = np.array(
self.electrical_consumption_heating
)
self.heat_supply = np.array(self.heat_supply)
def get_max_heating_power(
self, max_electric_power, t_source_heating=None, t_target_heating=None
):
"""Method that calculates the maximum heating power available
Args:
max_electric_power (float): Maximum amount of electric power that the electric heater can consume from the power grid
t_source_heating (float): Not used by the electric heater
t_target_heating (float): Not used by electric heater
Returns:
max_heating (float): maximum amount of heating energy that the electric heater can provide
"""
self.max_heating = self.max_electric_power * self.efficiency
return self.max_heating
def set_total_electric_consumption_heating(self, heat_supply=0):
"""
Args:
heat_supply (float): Amount of heating energy that the electric heater is going to supply
Returns:
_electrical_consumption_heating (float): electricity consumption for heating
"""
self.heat_supply.append(heat_supply)
self._electrical_consumption_heating = heat_supply / self.efficiency
if self.save_memory == False:
self.electrical_consumption_heating.append(
np.float32(self._electrical_consumption_heating)
)
return self._electrical_consumption_heating
def get_electric_consumption_heating(self, heat_supply=0):
"""
Args:
heat_supply (float): Amount of heating energy that the electric heater is going to supply
Returns:
_electrical_consumption_heating (float): electricity consumption for heating
"""
_electrical_consumption_heating = heat_supply / self.efficiency
return _electrical_consumption_heating
def reset(self):
self.max_heating = None
self.electrical_consumption_heating = []
self.heat_supply = []
class EnergyStorage:
def __init__(
self,
capacity=None,
max_power_output=None,
max_power_charging=None,
efficiency=1,
loss_coef=0,
save_memory=True,
):
"""
Generic energy storage class. It can be used as a chilled water storage tank or a DHW storage tank
Args:
capacity (float): Maximum amount of energy that the storage unit is able to store (kWh)
max_power_output (float): Maximum amount of power that the storage unit can output (kW)
max_power_charging (float): Maximum amount of power that the storage unit can use to charge (kW)
efficiency (float): Efficiency factor of charging and discharging the storage unit (from 0 to 1)
loss_coef (float): Loss coefficient used to calculate the amount of energy lost every hour (from 0 to 1)
"""
self.capacity = capacity
self.max_power_output = max_power_output
self.max_power_charging = max_power_charging
self.efficiency = efficiency ** 0.5
self.loss_coef = loss_coef
self.soc = []
self._soc = 0 # State of Charge
self.energy_balance = []
self._energy_balance = 0
self.save_memory = save_memory
def terminate(self):
if self.save_memory == False:
self.energy_balance = np.array(self.energy_balance)
self.soc = np.array(self.soc)
def charge(self, energy, SOC_Hsto):
"""Method that controls both the energy CHARGE and DISCHARGE of the energy storage device
energy < 0 -> Discharge
energy > 0 -> Charge
Args:
energy (float): Amount of energy stored in that time-step (Wh)
Return:
energy_balance (float):
"""
# The initial State Of Charge (SOC) is the previous SOC minus the energy losses
soc_init = SOC_Hsto * (1 - self.loss_coef)
# Charging
if energy >= 0:
if self.max_power_charging is not None:
energy = min(energy, self.max_power_charging)
SOC_Hsto = soc_init + energy * self.efficiency
# Discharging
else:
if self.max_power_output is not None:
energy = max(-self.max_power_output, energy)
SOC_Hsto = max(0, SOC_Hsto + energy / self.efficiency)
if self.capacity is not None:
SOC_Hsto = min(SOC_Hsto, self.capacity)
# Calculating the energy balance with its external environmrnt (amount of energy taken from or relseased to the environment)
# Charging
if energy >= 0:
self._energy_balance = (SOC_Hsto - soc_init) / self.efficiency
# Discharging
else:
self._energy_balance = (SOC_Hsto - soc_init) * self.efficiency
if self.save_memory == False:
self.energy_balance.append(np.float32(self._energy_balance))
self.soc.append(np.float32(SOC_Hsto))
self._soc = SOC_Hsto
return self._energy_balance
def reset(self):
self.soc = []
self._soc = 0 # State of charge
self.energy_balance = [] # Positive for energy entering the storage
self._energy_balance = 0
self.time_step = 0
class Battery:
def __init__(
self,
capacity,
nominal_power=None,
capacity_loss_coef=None,
power_efficiency_curve=None,
capacity_power_curve=None,
efficiency=None,
loss_coef=0,
save_memory=True,
):
"""
Generic energy storage class. It can be used as a chilled water storage tank or a DHW storage tank
Args:
capacity (float): Maximum amount of energy that the storage unit is able to store (kWh)
max_power_charging (float): Maximum amount of power that the storage unit can use to charge (kW)
efficiency (float): Efficiency factor of charging and discharging the storage unit (from 0 to 1)
loss_coef (float): Loss coefficient used to calculate the amount of energy lost every hour (from 0 to 1)
power_efficiency_curve (float): Charging/Discharging efficiency as a function of the power released or consumed
capacity_power_curve (float): Max. power of the battery as a function of its current state of charge
capacity_loss_coef (float): Battery degradation. Storage capacity lost in each charge and discharge cycle (as a fraction of the total capacity)
"""
self.capacity = capacity
self.c0 = capacity
self.nominal_power = nominal_power
self.capacity_loss_coef = capacity_loss_coef
if power_efficiency_curve is not None:
self.power_efficiency_curve = np.array(power_efficiency_curve).T
else:
self.power_efficiency_curve = power_efficiency_curve
if capacity_power_curve is not None:
self.capacity_power_curve = np.array(capacity_power_curve).T
else:
self.capacity_power_curve = capacity_power_curve
self.efficiency = efficiency ** 0.5
self.loss_coef = loss_coef
self.max_power = None
self._eff = []
self._energy = []
self._max_power = []
self.soc = []
self._soc = 0 # State of Charge
self.energy_balance = []
self._energy_balance = 0
self.save_memory = save_memory
def terminate(self):
if self.save_memory == False:
self.energy_balance = np.array(self.energy_balance)
self.SOC_bat = np.array(self.SOC_bat)
def charge(self, energy, SOC_bat):
"""Method that controls both the energy CHARGE and DISCHARGE of the energy storage device
energy < 0 -> Discharge
energy > 0 -> Charge
Args:
energy (float): Amount of energy stored in that time-step (Wh)
Return:
energy_balance (float):
"""
# The initial State Of Charge (SOC) is the previous SOC minus the energy losses
soc_init = SOC_bat * (1 - self.loss_coef)
if self.capacity_power_curve is not None:
soc_normalized = soc_init / self.capacity
# Calculating the maximum power rate at which the battery can be charged or discharged
idx = max(0, np.argmax(soc_normalized <= self.capacity_power_curve[0]) - 1)
self.max_power = self.nominal_power * (
self.capacity_power_curve[1][idx]
+ (
self.capacity_power_curve[1][idx + 1]
- self.capacity_power_curve[1][idx]
)
* (soc_normalized - self.capacity_power_curve[0][idx])
/ (
self.capacity_power_curve[0][idx + 1]
- self.capacity_power_curve[0][idx]
)
)
else:
self.max_power = self.nominal_power
# Charging
if energy >= 0:
if self.nominal_power is not None:
energy = min(energy, self.max_power)
if self.power_efficiency_curve is not None:
# Calculating the maximum power rate at which the battery can be charged or discharged
energy_normalized = np.abs(energy) / self.nominal_power
idx = max(
0,
np.argmax(energy_normalized <= self.power_efficiency_curve[0])
- 1,
)
self.efficiency = self.power_efficiency_curve[1][idx] + (
energy_normalized - self.power_efficiency_curve[0][idx]
) * (
self.power_efficiency_curve[1][idx + 1]
- self.power_efficiency_curve[1][idx]
) / (
self.power_efficiency_curve[0][idx + 1]
- self.power_efficiency_curve[0][idx]
)
self.efficiency = self.efficiency ** 0.5
SOC_bat = soc_init + energy * self.efficiency
# Discharging
else:
if self.nominal_power is not None:
energy = max(-self.max_power, energy)
if self.power_efficiency_curve is not None:
# Calculating the maximum power rate at which the battery can be charged or discharged
energy_normalized = np.abs(energy) / self.nominal_power
idx = max(
0,
np.argmax(energy_normalized <= self.power_efficiency_curve[0]) - 1,
)
self.efficiency = self.power_efficiency_curve[1][idx] + (
energy_normalized - self.power_efficiency_curve[0][idx]
) * (
self.power_efficiency_curve[1][idx + 1]
- self.power_efficiency_curve[1][idx]
) / (
self.power_efficiency_curve[0][idx + 1]
- self.power_efficiency_curve[0][idx]
)
self.efficiency = self.efficiency ** 0.5
SOC_bat = max(0, soc_init + energy / self.efficiency)
if self.capacity is not None:
SOC_bat = min(SOC_bat, self.capacity)
# Calculating the energy balance with its external environment (amount of energy taken from or relseased to the environment)
# Charging
if energy >= 0:
self._energy_balance = (SOC_bat - soc_init) / self.efficiency
# Discharging
else:
self._energy_balance = (SOC_bat - soc_init) * self.efficiency
# Calculating the degradation of the battery: new max. capacity of the battery after charge/discharge
self.capacity -= (
self.capacity_loss_coef
* self.c0
* np.abs(self._energy_balance)
/ (2 * self.capacity)
)
if self.save_memory == False:
self.energy_balance.append(np.float32(self._energy_balance))
self.soc.append(np.float32(SOC_bat))
self._soc = SOC_bat
return self._energy_balance
def reset(self):
self.soc = []
self._soc = 0 # State of charge
self.energy_balance = [] # Positive for energy entering the storage
self._energy_balance = 0
self.time_step = 0