-
Notifications
You must be signed in to change notification settings - Fork 7
/
spatial_diffusion.py
679 lines (554 loc) · 21.8 KB
/
spatial_diffusion.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
"""This file contains all calculations related
to spatial explicit calculations of technology/innovation
penetration."""
import logging
from collections import defaultdict
import numpy as np
from energy_demand.plotting import plotting_program
def realdata_to_spatialdiffval(regions, real_values, speed_con_max):
"""Create SDI from socio-economic data
concepts : list
List with concepts (e.g. poor, welathy, rich)
Info
------
* SPEED 'speed_con_min'
"""
diffusion_values = {}
# Diffusion speed assumptions
speed_con_min = 1 # Speed at val_con == 0
speed_con_max = speed_con_max # Speed at con_val == 1
# ----------------
# plot real values to check for outliers
# ----------------
#plotting_program.plot_xy(list(real_values.values()))
# Select number of outliers to remove extrems
nr_of_utliers = 20
sorted_vals = list(real_values.values())
sorted_vals.sort()
# Get value of largest outlier
treshold_upper_real_value = sorted_vals[-nr_of_utliers]
for reg, val in real_values.items():
if val > treshold_upper_real_value:
real_values[reg] = treshold_upper_real_value
# Plot after removing outliers
#plotting_program.plot_xy(list(real_values.values()))
# ----------------
# Concruence calculations
# -----------------
# Max congruence value
con_max = max(real_values.values())
for region in regions:
# Multiply speed of diffusion of concept with concept congruence value
try:
real_value = real_values[region]
except KeyError:
real_value = np.average(real_values.values())
logging.warning("Set average real data for region %s", region)
# Calculate congruence value
congruence_value = real_value / con_max
# Calculate diffusion value
lower_concept_val = (1 - congruence_value) * speed_con_min
higher_concept_val = congruence_value * speed_con_max
diffusion_values[region] = lower_concept_val + higher_concept_val
logging.info(
"Reg: %s diffusion_value: %s real_value: %s",
region,
lower_concept_val + higher_concept_val,
real_value)
return diffusion_values
def spatial_diffusion_values(regions, real_values, speed_con_max):
"""Load spatial diffusion values
This are the values which already incorporate different
speeds in diffusion and the congruence values
e.g. based on urban/rural population
Arguments
---------
regions : dict
Regions
Returns
-------
spatial_diff : dict
Spatial diffusion values based on speed assumptions
"""
spatial_diff = {}
# Diffusion values based on urban/rural
spatial_diff_urban_rural = realdata_to_spatialdiffval(
regions=regions, # Regions
real_values=real_values, # Real values
speed_con_max=speed_con_max) # TODOsomehow not works if set to 1.0 (meanign all value are the same)
for region in regions:
spatial_diff[region] = spatial_diff_urban_rural[region]
return spatial_diff
def calc_diffusion_f(regions, f_reg, spatial_diff_values, fuels):
"""From spatial diffusion values calculate diffusion
factor for every region (which needs to sum up to one
across all regions) and end use. With help of these calculation diffusion
factors, a spatial explicit diffusion of innovations can be
implemented.
Arguments
----------
regions : dict
Regions
f_reg : dict
Regional not weighted diffusion factors
spatial_diff_values : dict
Spatial diffusion index values
fuels : array
Fuels per enduse or fuel per sector and enduse
Example
-------
If the national assumption of a technology diffusion
of 50% is defined (e.g. 50% of service are heat pumps), this
percentage can be changed per region, i.e. in some regions
with higher diffusion factors, a larger percentage adopt
the technology on the expense of other regions, where a
lower percentage adopt this technology. In sum however,
for all regions, the total service still sums up to 50%.
Note
-----
The total sum can be higher than 1 in case of high values.
Therfore the factors need to be capped. TODO MORE INFO
"""
# Calculate fraction of energy demand of every region of total demand
reg_enduse_p = defaultdict(dict)
fuels_enduse = {}
for fuel_submodel in fuels:
# -----------------------------------
# Sum fuel across sectors
# -----------------------------------
fuel_submodel_new = defaultdict(dict)
for reg, entries in fuel_submodel.items():
enduses = entries.keys()
try:
for enduse in entries:
for sector in entries[enduse]:
fuel_submodel_new[reg][enduse] = 0
for enduse in entries:
for sector in entries[enduse]:
fuel_submodel_new[reg][enduse] += np.sum(entries[enduse][sector])
fuel_submodel = fuel_submodel_new
except IndexError:
enduses = entries.keys()
break
# --------------------
# Calculate fraction of fuel for each region
# --------------------
for enduse in enduses:
fuels_enduse[enduse] = 0
# Total uk fuel of enduse
tot_enduse_uk = 0
for reg in regions:
tot_enduse_uk += np.sum(fuel_submodel[reg][enduse])
# Calculate regional % of enduse
for region in regions:
reg_enduse_p[enduse][region] = np.sum(fuel_submodel[region][enduse]) / tot_enduse_uk
fuels_enduse[enduse] += np.sum(fuel_submodel[region][enduse])
# ----------
# Norm spatial factor (f_reg_norm) with population (does not sum upt to 1.p Eq. 7 Appendix)
# ----------
f_reg_norm = {}
for enduse, regions_fuel_p in reg_enduse_p.items():
# Sum across all regs (factor * fuel_p)
sum_p_f_all_regs = 0
for i in regions:
sum_p_f_all_regs += f_reg[i] * regions_fuel_p[reg]
f_reg_norm[enduse] = {}
for reg, fuel_p in regions_fuel_p.items():
f_reg_norm[enduse][reg] = f_reg[reg] / sum_p_f_all_regs
# ----------
# Norm which sums up to 1 (f_reg_norm_abs) (e.g. distriubte 200 units across space)
# ----------
f_reg_norm_abs = {}
for enduse, regions_fuel_p in reg_enduse_p.items():
f_reg_norm_abs[enduse] = {}
for reg, fuel_p in regions_fuel_p.items():
f_reg_norm_abs[enduse][reg] = fuel_p * spatial_diff_values[reg]
#-----------
# Normalize f_reg_norm_abs
#-----------
for enduse in f_reg_norm_abs:
sum_enduse = sum(f_reg_norm_abs[enduse].values())
for reg in f_reg_norm_abs[enduse]:
f_reg_norm_abs[enduse][reg] = f_reg_norm_abs[enduse][reg] / sum_enduse
# Testing
for enduse in f_reg_norm_abs:
np.testing.assert_almost_equal(
sum(f_reg_norm_abs[enduse].values()),
1,
decimal=2)
#-----------
# TESTING
#-----------
'''enduse = 'ss_water_heating'
tot_amount_to_distribute = 200
# -----
# f_reg_norm_abs
# -----
# To distribute full absolute amount across all regs
__t = 0
for reg in regions:
__t += f_reg_norm_abs[enduse][reg] * tot_amount_to_distribute
logging.info("TOTAL MOUNT TO DISTR: " + str(tot_amount_to_distribute))
logging.info("TOTAL MOUNT TO DISTR CTONRLL: " + str(__t))
# -----
# f_reg_norm_abs
# -----
global_f = 0.9
_0 = []
_1 = []
_2 = []
for reg in regions:
logging.warning("-------")
logging.warning("Not Normed: %s ", f_reg[reg] * global_f) # not normed
logging.warning("Normed: %s ", f_reg_norm[enduse][reg] * global_f) # normed with not summing up to 1
logging.warning("Sums up to one: %s ", (f_reg_norm_abs[enduse][reg] * global_f)) #new
logging.warning((f_reg_norm_abs[enduse][reg] * global_f) / reg_enduse_p[enduse][reg]) #old
_0.append(f_reg[reg] * global_f)
_1.append(f_reg_norm[enduse][reg] * global_f)
_2.append(f_reg_norm_abs[enduse][reg] * global_f)
logging.info("INFOF FOR DEBBUGING")
logging.info(max(_0))
logging.info(max(_1))
logging.info(max(_2))
logging.info("--")
logging.info(sum(_0))
logging.info(sum(_1))
logging.info(sum(_2))'''
return f_reg_norm_abs, f_reg_norm
def calc_regional_services(
enduse,
uk_techs_service_p,
regions,
spatial_factors,
fuel_disaggregated,
techs_affected_spatial_f
):
"""Calculate regional specific end year service shares
of technologies (rs_reg_enduse_tech_p_ey)
Arguments
---------
uk_techs_service_p : dict
Service shares per technology for future year
regions : dict
Regions
spatial_factors : dict
Spatial factor per enduse and region
fuel_disaggregated : dict
Fuels per region
techs_affected_spatial_f : list
List with technologies where spatial diffusion is affected
Returns
-------
rs_reg_enduse_tech_p_ey : dict
Regional specific model end year service shares of techs
Modelling steps
-----
A.) Calculation national end use service to reduce
(e.g. 50% heat pumps for all regions) (uk_tech_service_ey_p)
B.) Distribute this service according to spatial index for
techs where the spatial explicit diffusion applies (techs_affected_spatial_f).
Otherwise disaggregated according to fuel
C.) Convert regional service reduction to ey % in region
"""
reg_enduse_tech_p_ey = defaultdict(dict)
# ------------------------------------
# Calculate national total enduse fuel and service
# ------------------------------------
uk_enduse_fuel = 0
for region in regions:
reg_enduse_tech_p_ey[region] = {}
uk_enduse_fuel += np.sum(fuel_disaggregated[region][enduse])
# ----
# Service of enduse for all regions
# ----
for region in regions:
# Disaggregation factor
f_fuel_disagg = np.sum(fuel_disaggregated[region][enduse]) / uk_enduse_fuel
# Calculate fraction of regional service
for tech, uk_tech_service_ey_p in uk_techs_service_p.items():
global_tech_service_ey_p = uk_tech_service_ey_p
# ---------------------------------------------
# B.) Calculate regional service for technology
# ---------------------------------------------
if tech in techs_affected_spatial_f:
# Use spatial factors
reg_service_tech = global_tech_service_ey_p * spatial_factors[enduse][region]
else:
# If not specified, use fuel disaggregation for enduse factor
reg_service_tech = global_tech_service_ey_p #* f_fuel_disagg
reg_enduse_tech_p_ey[region][tech] = reg_service_tech
# ---------------------------------------------
# C.) Calculate regional fraction
# ---------------------------------------------
for tech, service_tech in reg_enduse_tech_p_ey[region].items():
# ----------------------------------
#MAYBE ADD CAPPING VALUE TODO
# ----------------------------------
capping_val = 1
service_share = service_tech
if service_share > capping_val:
reg_enduse_tech_p_ey[region][tech] = capping_val
logging.warning("CAPPING VALUE REACHED {} {} ".format(region, service_share))
else:
reg_enduse_tech_p_ey[region][tech] = service_share
return dict(reg_enduse_tech_p_ey)
def calc_spatially_diffusion_factors(
regions,
fuel_disagg,
real_values,
speed_con_max
):
"""
Arguments
---------
regions : dict
Regions
fuel_disagg : dict
Disaggregated fuel per region
real_values : dict
Real values
Returns
-------
f_reg_norm_abs : dict
Diffusion values with normed population. If no value
is larger than 1, the total sum of all shares calculated
for every region is identical to the defined scenario variable.
spatial_diff_values : dict
Spatial diffusion values (not normed, only considering differences
in speed and congruence values)
Explanation
============
(I) Load diffusion values
(II) Calculate diffusion factors
(III) Calculate sigmoid diffusion values for technology
specific enduse service shares for every region
"""
# -----
# I. Diffusion diffusion values
# -----
spatial_diff_values = spatial_diffusion_values(
regions=regions,
real_values=real_values,
speed_con_max=speed_con_max)
# -----
# II. Calculation of diffusion factors ( Not weighted with demand)
# -----
f_reg = {}
max_value_diffusion = max(list(spatial_diff_values.values()))
for region in regions:
f_reg[region] = spatial_diff_values[region] / max_value_diffusion
# Weighted with demand
f_reg_norm_abs, f_reg_norm = calc_diffusion_f(
regions,
f_reg,
spatial_diff_values,
[fuel_disagg['rs_fuel_disagg'], fuel_disagg['ss_fuel_disagg'], fuel_disagg['is_fuel_disagg']])
return f_reg, f_reg_norm, f_reg_norm_abs
def spatially_differentiated_modelling(
regions,
fuel_disagg,
rs_share_s_tech_ey_p,
ss_share_s_tech_ey_p,
is_share_s_tech_ey_p,
techs_affected_spatial_f,
spatial_diffusion_factor,
spatial_exliclit_diffusion=False
):
"""
Regional diffusion shares of technologies is calculated
based on calcualted spatial diffusion factors
Arguments
---------
regions : dict
Regions
fuel_disagg : dict
Fuel per region
rs_share_s_tech_ey_p : dict
Global technology service shares
ss_share_s_tech_ey_p : dict
Global technology service shares
is_share_s_tech_ey_p : dict
Global technology service shares
techs_affected_spatial_f : list
Technologies which are affected by spatially heterogeneous diffusion
spatial_diffusion_factor : dict
Spatial diffusion factor
Returns
--------
XX_reg_share_s_tech_ey_p :
Technology specific service shares for every region (residential)
considering differences in diffusion speed.
If the calculate regional shares are larger than 1.0, the
diffusion is set to the maximum criteria (`cap_max`).
This means that if some regions reach the maximum defined value,
thes cannot futher increase their share. This means that other regions diffuse
slower and do not reach such high leves (and because the faster regions
cannot over-compensate, the total sum is not identical).
Calculate sigmoid diffusion values for technology
specific enduse service shares for every region
"""
'''if spatial_exliclit_diffusion:
rs_reg_share_s_tech_ey_p = {}
for enduse, uk_techs_service_p in rs_share_s_tech_ey_p.items():
rs_reg_share_s_tech_ey_p[enduse] = calc_regional_services(
enduse,
uk_techs_service_p,
regions,
spatial_diffusion_factor,
fuel_disagg['rs_fuel_disagg'],
techs_affected_spatial_f)
else:'''
if 1 == 1:
# Residential spatial explicit modelling
rs_reg_share_s_tech_ey_p = {}
for enduse, uk_techs_service_p in rs_share_s_tech_ey_p.items():
rs_reg_share_s_tech_ey_p[enduse] = calc_regional_services(
enduse,
uk_techs_service_p,
regions,
spatial_diffusion_factor,
fuel_disagg['rs_fuel_disagg'],
techs_affected_spatial_f)
ss_reg_share_s_tech_ey_p = {}
for sector, uk_techs_service_enduses_p in ss_share_s_tech_ey_p.items():
ss_reg_share_s_tech_ey_p[sector] = {}
for enduse, uk_techs_service_p in uk_techs_service_enduses_p.items():
ss_reg_share_s_tech_ey_p[sector][enduse] = calc_regional_services(
enduse,
uk_techs_service_p,
regions,
spatial_diffusion_factor,
fuel_disagg['ss_fuel_disagg_sum_all_sectors'],
techs_affected_spatial_f)
is_reg_share_s_tech_ey_p = {}
for sector, uk_techs_service_enduses_p in is_share_s_tech_ey_p.items():
is_reg_share_s_tech_ey_p[sector] = {}
for enduse, uk_techs_service_p in uk_techs_service_enduses_p.items():
is_reg_share_s_tech_ey_p[sector][enduse] = calc_regional_services(
enduse,
uk_techs_service_p,
regions,
spatial_diffusion_factor,
fuel_disagg['is_aggr_fuel_sum_all_sectors'],
techs_affected_spatial_f)
return rs_reg_share_s_tech_ey_p, ss_reg_share_s_tech_ey_p, is_reg_share_s_tech_ey_p
def factor_improvements_single(
factor_uk,
regions,
f_reg,
f_reg_norm,
f_reg_norm_abs,
fuel_regs_enduse
):
"""Calculate regional specific end year service shares
of technologies (rs_reg_enduse_tech_p_ey)
Arguments
=========
factor_uk : float
Improvement of either an enduse or a variable for the whole UK
regions : dict
Regions
f_reg : dict
Regional spatial factors not normed with fuel demand
f_reg_norm : dict
Regional spatial factors normed with fuel demand (sum is not 1)
f_reg_norm_abs : dict
Regional spatial factors normed with fuel demand and normed that sum is 1
spatial_diff_values : dict
Spatial diffusion values
fuel_regs_enduse : dict
Fuels per region and end use
Returns
-------
rs_reg_enduse_tech_p_ey : dict
Regional specific model end year service shares of techs
Modelling steps
-----
A.) Calculation national end use service to reduce
(e.g. 50% heat pumps for all regions) (uk_tech_service_ey_p)
B.) Distribute this service according to spatial index for
techs where the spatial explicit diffusion applies (techs_affected_spatial_f).
Otherwise disaggregated according to fuel
C.) Convert regional service reduction to ey % in region
"""
reg_enduse_tech_p_ey = {}
# Check which factors is to be used
# if only distribute: f_reg_norm_abs
# if max 1: f_reg_nrm
# if not intersted in correct sum: f_reg
if fuel_regs_enduse == {}:
logging.info("spatial_factor: fuel_regs_enduse")
spatial_factor = f_reg
else:
logging.info("spatial_factor: f_reg_norm_abs")
spatial_factor = f_reg_norm_abs
# Sum fuel for all regions
uk_enduse_fuel = sum(fuel_regs_enduse.values())
test = 0
for region in regions:
try:
test += (reg_enduse_tech_p_ey[region] * np.sum(fuel_regs_enduse[region]))
logging.info(
"FUEL FACTOR reg: {} val: {}, fuel: {} fuel: {} ".format(
region,
round(reg_enduse_tech_p_ey[region],3),
round(uk_enduse_fuel, 3),
round(np.sum(fuel_regs_enduse[region]),3)))
except:
pass
reg_enduse_tech_p_ey[region] = factor_uk * spatial_factor[region]
logging.info("spatial single factor reg: {} val: {}".format(
region,
round(reg_enduse_tech_p_ey[region],3)))
# ---------
# PROBLEM THAT MORE THAN 100 percent could be reached if nt normed
# ---------
reg_enduse_tech_p_ey_capped = {}
# Cap regions which have already reached and are larger than 1.0
cap_max_crit = 1.0 #100%
demand_lost = 0
for region, region_factor in reg_enduse_tech_p_ey.items():
if region_factor > cap_max_crit:
logging.warning("INFO: FOR A REGION THE SHARE OF IMPROVEMENTIS LARGER THAN 1.0.")
# Demand which is lost and capped
diff_to_cap = region_factor - cap_max_crit
demand_lost += diff_to_cap * np.sum(fuel_regs_enduse[region])
reg_enduse_tech_p_ey_capped[region] = cap_max_crit
else:
reg_enduse_tech_p_ey_capped[region] = region_factor
# Replace
reg_enduse_tech_p_ey = reg_enduse_tech_p_ey_capped
logging.warning("FAKTOR UK :" + str(factor_uk))
logging.warning("Lost demand: " + str(demand_lost))
logging.warning("TESTDUM a " + str(test))
logging.warning("TESTDUM b " + str(uk_enduse_fuel * factor_uk))
return reg_enduse_tech_p_ey
def get_enduse_regs(
enduse,
fuels_disagg):
"""
Get a specific enduse for all regions
Arguments
---------
enduse : str
Enduse to sum
fuels_disagg : list
Fuels per disaggregated regions
Returns
-------
fuels_enduse : dict
Fuels of an enduse for all regions
{'reg': np.array(enduse_fuel)}
"""
fuels_enduse = {}
if enduse == []:
pass
else:
for fuel_submodel in fuels_disagg:
for reg, enduse_fuels in fuel_submodel.items():
for enduse_to_match, fuels_regs in enduse_fuels.items():
if enduse == enduse_to_match:
fuels_enduse[reg] = fuels_regs
#if fuels_enduse == {}:
# raise Exception("Could not find fuel to get for regions %s", enduse)
return fuels_enduse