Skip to content

Commit

Permalink
working on fixing base year bug shares
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann committed Dec 17, 2018
1 parent 71f0223 commit 9176226
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 52 deletions.
4 changes: 2 additions & 2 deletions energy_demand/dwelling_stock/dw_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,10 @@ def generate_dw_existing(
dwtype_floorarea = floorarea_p[dwtype_name] * floorarea_by

# Distribute according to age
for dwtype_age in dwtype_age_distr_by.keys():
for dwtype_age, distribution in dwtype_age_distr_by.items():

# Floor area of dwelling_class_age (distribute proportionally floor area)
dwtype_age_class_floorarea = dwtype_floorarea * dwtype_age_distr_by[dwtype_age]
dwtype_age_class_floorarea = dwtype_floorarea * distribution

# Floor area per person is divided by base area value to calc pop
if floorarea_pp != 0:
Expand Down
55 changes: 31 additions & 24 deletions energy_demand/enduse_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ def __init__(
#print("------INFO {} {} {} {}".format(self.enduse, sector, region, curr_yr))
#print("FUEL TRAIN A0: " + str(np.sum(self.fuel_y)))

if curr_yr > 2015:
print("tt")
#if curr_yr > 2015 and enduse == 'rs_space_heating':
# print("tt")
# Get technologies of enduse
self.enduse_techs = get_enduse_techs(fuel_tech_p_by)

Expand Down Expand Up @@ -247,7 +247,7 @@ def __init__(
# ------------------------------------
# Calculate regional energy service
# ------------------------------------
print("A " + str(np.sum(self.fuel_y)))
#print("A " + str(np.sum(self.fuel_y)))
s_tot_y_cy, s_tech_y_by = fuel_to_service(
enduse,
sector,
Expand All @@ -256,7 +256,8 @@ def __init__(
tech_stock,
fueltypes,
mode_constrained)
print(np.sum(s_tot_y_cy))
#print(np.sum(s_tot_y_cy))
#print(np.sum(list(s_tech_y_by.values()))) #NEW
# ------------------------------------
# Reduction of service because of heat recovery
# ------------------------------------
Expand All @@ -266,7 +267,8 @@ def __init__(
s_tot_y_cy,
s_tech_y_by,
curr_yr)
print(np.sum(s_tot_y_cy))
#print(np.sum(s_tot_y_cy))
#print(np.sum(list(s_tech_y_cy.values()))) #NEW
# ------------------------------------
# Reduction of service because of improvement in air leakeage
# ------------------------------------
Expand All @@ -276,7 +278,8 @@ def __init__(
s_tot_y_cy,
s_tech_y_cy,
curr_yr)
print(np.sum(s_tot_y_cy))
#print(np.sum(s_tot_y_cy))
#print(np.sum(list(s_tech_y_cy.values()))) #NEW
# --------------------------------
# Switches
# --------------------------------
Expand All @@ -287,14 +290,15 @@ def __init__(
curr_yr=curr_yr,
base_yr=base_yr,
sector=sector,
annual_tech_diff_params=strategy_vars['annual_tech_diff_params'],
annual_tech_diff_params=strategy_vars['annual_tech_diff_params'][enduse][sector],
crit_switch_happening=assumptions.crit_switch_happening)
print(np.sum(s_tot_y_cy))
#print(np.sum(s_tot_y_cy))
#print(np.sum(list(s_tech_y_cy.values()))) #NEW
# -------------------------------------------
# Convert annual service to fuel per fueltype
# -------------------------------------------
print("--")
print(np.sum(self.fuel_y))
#print("--")
#print(np.sum(self.fuel_y))
self.fuel_y, fuel_tech_y = service_to_fuel(
enduse,
sector,
Expand All @@ -303,7 +307,7 @@ def __init__(
fueltypes_nr,
fueltypes,
mode_constrained)
print("B" + str(np.sum(self.fuel_y)))
#print("B" + str(np.sum(self.fuel_y)))
# Delete all technologies with no fuel assigned
for tech, fuel_tech in fuel_tech_y.items():
if np.sum(fuel_tech) == 0:
Expand Down Expand Up @@ -834,10 +838,9 @@ def service_to_fuel(
fuel_tech_y[tech] = fuel_tech

fuel_y[fueltype_int] += fuel_tech
#logging.debug("S --> F: tech: {} eff: {} fuel: {} fuel {}".format(tech, tech_eff, fuel_y[fueltype_int], fuel_tech))
#print("S --> F: tech: {} eff: {} fuel: {} service {}".format(tech, tech_eff, fuel_y[fueltype_int], service))
else:
for tech, fuel_tech in service_tech.items():

fuel_y[fueltypes['heat']] += fuel_tech
fuel_tech_y[tech] = fuel_tech

Expand Down Expand Up @@ -907,10 +910,10 @@ def fuel_to_service(
techs_with_fuel = tech_list

for tech, fuel_share in techs_with_fuel.items():


#Constrained version
if mode_constrained:
"""Constrained version
"""

tech_eff = tech_stock.get_tech_attr(enduse, sector, tech, 'eff_by')

# Get fuel share and convert fuel to service per technology
Expand All @@ -921,12 +924,10 @@ def fuel_to_service(
# Sum total yearly service
s_tot_y += s_tech #(y)

##logging.debug("F --> S: tech: {} eff: {} fuel: {} service {}".format(tech, tech_eff, fuel_y[fueltype_int], s_tech))
#print("F --> S: tech: {} eff: {} fuel: {} service {}".format(tech, tech_eff, fuel_y[fueltype_int], s_tech))
else:
"""Unconstrained version
efficiencies are not considered, because not technology
specific service calculation
"""
#Unconstrained version (efficiencies are not considered, because not technology specific service calculation)

# Calculate fuel share
fuel_tech = fuel_y[fueltype_int] * fuel_share

Expand Down Expand Up @@ -1470,18 +1471,24 @@ def apply_service_switch(
base_yr,
curr_yr)

# TESTING
#_sum = 0
#for tech in annual_tech_diff_params:
# _sum += annual_tech_diff_params[tech][curr_yr]
#
#print("TOTAL SUM " + str(_sum))
# ---------------------------------------
# Calculate switch
# ----------------------------------------
if crit_switch_service:
switched_s_tech_y_cy = {}

# Service of all technologies
service_all_techs = sum(s_tech_y_cy.values())


switched_s_tech_y_cy = {}
for tech in all_technologies:
# Get service share per tech of cy of sigmoid parameter calculations
p_s_tech_cy = annual_tech_diff_params[enduse][sector][tech][curr_yr]
p_s_tech_cy = annual_tech_diff_params[tech][curr_yr]
switched_s_tech_y_cy[tech] = service_all_techs * p_s_tech_cy
return switched_s_tech_y_cy
else:
Expand Down
2 changes: 1 addition & 1 deletion energy_demand/geography/weather_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ def get_weather_station_selection(
station_id = all_stations_of_weather_yr[counter]

all_weather_stations_out = {station_id: all_weather_stations[weather_yr][station_id]}
except:
except KeyError:
# Not enough stations to select position in list
station_id = False
all_weather_stations_out = []
Expand Down
3 changes: 2 additions & 1 deletion energy_demand/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ def energy_demand_model(
# Simulated yrs
#sim_yrs = [base_yr, 2030, user_defined_simulation_end_yr]
#sim_yrs = [2015, 2020, 2025, 2030, 2035, 2040, 2045, 2050]
sim_yrs = [2015, 2020, 2050]
#sim_yrs = [2015, 2020, 2050]
sim_yrs = [2015, 2050]
weather_yr_scenario = 2015 # Default weather year

if len(sys.argv) > 3: #user defined arguments are provide
Expand Down
30 changes: 28 additions & 2 deletions energy_demand/plotting/fig_3_plot_over_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def scenario_over_time(
scenario_result_container,
sim_yrs,
fig_name,
result_path
result_path,
plot_points
):
"""Plot peak over time
"""
Expand Down Expand Up @@ -80,6 +81,18 @@ def scenario_over_time(
df_q_05 = national_peak.quantile(quantile_05)
df_q_95 = national_peak.quantile(quantile_95)

# ------------------------
# Plot calculated data points
# ------------------------
if plot_points:

plt.scatter(
sim_yrs,
mean_national_peak,
c=color,
s=10,
clip_on=False) #do not clip points on axis

# --------------------
# Try to smooth lines
# --------------------
Expand Down Expand Up @@ -177,6 +190,7 @@ def fueltypes_over_time(
fig_name,
fueltypes,
result_path,
plot_points=False,
unit='TWh'
):
"""Plot fueltypes over time
Expand Down Expand Up @@ -238,6 +252,18 @@ def fueltypes_over_time(
df_q_05 = national_sum.quantile(quantile_05)
df_q_95 = national_sum.quantile(quantile_95)

# ------------------------
# Plot calculated data points
# ------------------------
if plot_points:

plt.scatter(
sim_yrs,
mean_national_sum,
c=color,
s=10,
clip_on=False) #do not clip points on axis

# --------------------
# Try to smooth lines
# --------------------
Expand Down Expand Up @@ -325,7 +351,7 @@ def fueltypes_over_time(
legend = plt.legend(
#title="tt",
ncol=2,
prop={'size': 3},
prop={'size': 6},
loc='upper center',
bbox_to_anchor=(0.5, -0.1),
frameon=False)
Expand Down
4 changes: 3 additions & 1 deletion energy_demand/result_processing/weather_result_charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ def main(
fig_name="fueltypes_over_time_{}.pdf".format(fueltype_str),
fueltypes=['electricity', 'gas', 'hydrogen'],
result_path=result_path,
unit='TWh')
unit='TWh',
plot_points=True)
#raise Exception("TT")
# ------------------------------
# Plot national peak change over time for each scenario
Expand All @@ -208,6 +209,7 @@ def main(
scenario_result_container=scenario_result_container,
sim_yrs=data['assumptions']['sim_yrs'],
fig_name="scenarios_peak_over_time_{}.pdf".format(fueltype_str),
plot_points=True,
result_path=result_path)
raise Exception("TT3")

Expand Down
31 changes: 27 additions & 4 deletions energy_demand/scripts/init_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_all_narrative_timesteps(switches_list):
switches_list)

for enduse in enduses:
narrative_timesteps[enduse] = set([])
narrative_timesteps[enduse] = set()

for switch in switches_list:
if switch.enduse == enduse:
Expand Down Expand Up @@ -239,9 +239,13 @@ def switch_calculations(
for submodel in data['assumptions'].submodels_names:
for enduse in data['enduses'][submodel]:
for sector in data['sectors'][submodel]:


#NEW Add base year to timesteps
#timsteps_plus_by = [2015] + narrative_timesteps[enduse]

diffusion_param_tech[enduse][sector] = sig_param_calc_incl_fuel_switch(
narrative_timesteps,
narrative_timesteps, #timsteps_plus_by,
data['assumptions'].base_yr,
data['assumptions'].crit_switch_happening,
data['assumptions'].technologies,
Expand All @@ -261,7 +265,26 @@ def switch_calculations(
annual_tech_diff_params = s_scenario_param.calc_annual_switch_params(
sim_yrs,
data['regions'],
dict(diffusion_param_tech))
dict(diffusion_param_tech),
base_yr=data['assumptions'].base_yr, #NEW
s_tech_by_p=s_tech_by_p) #NEW

# Test whether the calculated service shares sum up to 1
for region in annual_tech_diff_params:
for enduse in annual_tech_diff_params[region]:
for sector in annual_tech_diff_params[region][enduse]:
print("TTTTTTTT {} {} {} ".format(region, enduse, sector))
print(annual_tech_diff_params[region][enduse][sector])

for year in sim_yrs:
if annual_tech_diff_params[region][enduse][sector] != []:
_sum = 0
for tech in annual_tech_diff_params[region][enduse][sector]:
print("TECH " + str(tech))
print("TECH " + str(annual_tech_diff_params[region][enduse][sector][tech]))
_sum += annual_tech_diff_params[region][enduse][sector][tech][year]
print(_sum)
assert round(_sum, 2) == 1

return annual_tech_diff_params

Expand Down Expand Up @@ -741,7 +764,7 @@ def sig_param_calc_incl_fuel_switch(
# -----------------------------------------------
#logging.debug("---------- switches %s %s %s", enduse, crit_switch_service, crit_fuel_switch)
if crit_all_the_same:
logging.info("... calc parameters of `{}` for year `{}` {}".format(enduse, switch_yr, sector))
print("... calc parameters of `{}` for year `{}` {}".format(enduse, switch_yr, sector))
# Calculate for one region
sig_param_tech_all_regs_value = s_generate_sigmoid.tech_sigmoid_parameters(
switch_yr,
Expand Down
2 changes: 1 addition & 1 deletion energy_demand/scripts/s_disaggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def disaggr_demand(data, crit_temp_min_max, spatial_calibration=False):
can be calibrated for gas and electricity based on actual
measured demand data
Note: All other fueltypes are not skaled
Note: All other fueltypes are not scaled
'''
calibrate_residential = False # Calibrate residential demands
calibrate_non_residential = True # Calibrate non residential demands
Expand Down

0 comments on commit 9176226

Please sign in to comment.