Skip to content

Commit

Permalink
fixed bug in apply_scenario_drivers()
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann authored and Sven Eggimann committed Sep 5, 2018
1 parent 5e6b325 commit 4c5be27
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 52 deletions.
1 change: 1 addition & 0 deletions energy_demand/assumptions/general_assumptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ def __init__(
# Provide for every fueltype of an enduse
# the share of fuel which is used by technologies for thebase year
# ============================================================
# SNAKE: SUM ALL IN ONE
self.rs_fuel_tech_p_by, self.ss_fuel_tech_p_by, self.is_fuel_tech_p_by = fuel_shares.assign_by_fuel_tech_p(
enduses,
sectors,
Expand Down
2 changes: 0 additions & 2 deletions energy_demand/basic/demand_supply_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ def write_national_results(
str_name_non_heat = "{}_non_heat".format(submodel)
row[str_name_non_heat] = ed_submodel_non_heating_h

#print("INFO {} {} {} {}".format(submodel_nr, ed_submodel_h, ed_submodel_non_heating_h, ed_submodel_heating_h))

rows.append(row)

# Create dataframe
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions energy_demand/config_data/06-switches/switches_service.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enduse,tech,service_share_ey,switch_yr,sector
2 changes: 1 addition & 1 deletion energy_demand/dwelling_stock/dw_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def ss_dw_stock(
try:
gva_sector_lu = lookup_tables.economic_sectors_regional_MISTRAL()
gva_nr = gva_sector_lu[sector]['match_int']
gva_dw_data = scenario_data['gva_industry_service'][curr_yr][region][gva_nr]
gva_dw_data = scenario_data['gva_industry'][curr_yr][region][gva_nr]
except KeyError:
# If not sector specific GVA, use overal GVA per head
gva_dw_data = scenario_data['gva_per_head'][curr_yr][region]
Expand Down
60 changes: 27 additions & 33 deletions energy_demand/enduse_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(
self.fuel_yh = 0
self.enduse_techs = []
else:
logging.debug("------INFO {} {} {}".format(self.enduse, region, curr_yr))
#logging.info("------INFO {} {} {} {}".format(self.enduse, sector, region, curr_yr))
# Get technologies of enduse
self.enduse_techs = get_enduse_techs(fuel_tech_p_by)

Expand All @@ -135,11 +135,7 @@ def __init__(
assumptions.enduse_space_heating,
assumptions.ss_enduse_space_cooling)
self.fuel_y = _fuel_new_y
logging.debug("... Fuel train B0: " + str(np.sum(self.fuel_y)))

if enduse == 'is_other':
logging.debug("_____________________{} {}".format(str(curr_yr), sector))
logging.debug("... Fuel train B0: " + str(np.sum(self.fuel_y)))
logging.debug("FUEL TRAIN B0: " + str(np.sum(self.fuel_y)))

_fuel_new_y = apply_smart_metering(
enduse,
Expand All @@ -148,30 +144,30 @@ def __init__(
strategy_vars,
curr_yr)
self.fuel_y = _fuel_new_y
logging.debug("... Fuel train C0: " + str(np.sum(self.fuel_y)))
logging.debug("FUEL TRAIN C0: " + str(np.sum(self.fuel_y)))

_fuel_new_y = apply_specific_change(
enduse,
self.fuel_y,
strategy_vars,
curr_yr)
self.fuel_y = _fuel_new_y
logging.debug("... Fuel train D0: " + str(np.sum(self.fuel_y)))
logging.debug("FUEL TRAIN D0: " + str(np.sum(self.fuel_y)))

_fuel_new_y = apply_scenario_drivers(
enduse,
sector,
self.fuel_y,
dw_stock,
region,
scenario_data['gva_industry_service'],
scenario_data['gva_per_head'],
scenario_data['population'],
reg_scen_drivers,
base_yr,
curr_yr)
enduse=enduse,
sector=sector,
fuel_y=self.fuel_y,
dw_stock=dw_stock,
region=region,
gva_industry=scenario_data['gva_industry'],
gva_per_head=scenario_data['gva_per_head'],
population=scenario_data['population'],
reg_scen_drivers=reg_scen_drivers,
base_yr=base_yr,
curr_yr=curr_yr)
self.fuel_y = _fuel_new_y
logging.debug("... Fuel train E0: " + str(np.sum(self.fuel_y)))
logging.debug("FUEL TRAIN E0: " + str(np.sum(self.fuel_y)))

# Apply cooling scenario variable
_fuel_new_y = apply_cooling(
Expand All @@ -181,7 +177,7 @@ def __init__(
assumptions.cooled_ss_floorarea_by,
curr_yr)
self.fuel_y = _fuel_new_y
logging.debug("... Fuel train E1: " + str(np.sum(self.fuel_y)))
logging.debug("FUEL TRAIN E1: " + str(np.sum(self.fuel_y)))

# Industry related change
_fuel_new_y = industry_enduse_changes(
Expand All @@ -193,7 +189,7 @@ def __init__(
self.fuel_y,
assumptions)
self.fuel_y = _fuel_new_y
logging.debug("... Fuel train E2: " + str(np.sum(self.fuel_y)))
logging.debug("FUEL TRAIN E2: " + str(np.sum(self.fuel_y)))

# Generic fuel switch of an enduse
_fuel_new_y = generic_fuel_switch(
Expand Down Expand Up @@ -295,7 +291,7 @@ def __init__(
fueltypes_nr,
fueltypes,
mode_constrained)
logging.debug("... Fuel train H0: " + str(np.sum(self.fuel_y)))
logging.debug("H0: " + str(np.sum(self.fuel_y)))

# Delete all technologies with no fuel assigned
for tech, fuel_tech in fuel_tech_y.items():
Expand Down Expand Up @@ -1062,9 +1058,8 @@ def apply_scenario_drivers(
#TODO :ADD OTHER driver
"""
if not dw_stock:
"""Calculate non-dwelling related scenario drivers, if no dwelling stock
Info: No dwelling stock is defined for this submodel
if not dw_stock: # No dwelling stock is available
"""Calculate non-dwelling related scenario drivers
"""
scenario_drivers = reg_scen_drivers[enduse]

Expand All @@ -1074,20 +1069,21 @@ def apply_scenario_drivers(

# Get correct data depending on driver
if scenario_driver == 'gva':
if sector == None:
if not sector:
by_driver_data = gva_per_head[base_yr][region]
cy_driver_data = gva_per_head[curr_yr][region]
else:
try:
gva_sector_lu = lookup_tables.economic_sectors_regional_MISTRAL()
gva_nr = gva_sector_lu[sector]['match_int']
gva_sector_nr = gva_sector_lu[sector]['match_int']

# Get sector specific GVA
by_driver_data = gva_industry[base_yr][region][gva_nr]
cy_driver_data = gva_industry[curr_yr][region][gva_nr]
by_driver_data = gva_industry[base_yr][gva_sector_nr][region]
cy_driver_data = gva_industry[curr_yr][gva_sector_nr][region]

except KeyError:
# No sector specific GVA data defined
logging.info("No gva data found for sector {} ".format(sector))
by_driver_data = gva_per_head[base_yr][region]
cy_driver_data = gva_per_head[curr_yr][region]

Expand All @@ -1098,7 +1094,7 @@ def apply_scenario_drivers(
# Multiply drivers
by_driver *= by_driver_data
cy_driver *= cy_driver_data

# Testing
if math.isnan(by_driver_data) or math.isnan(cy_driver_data):
raise Exception("Scenario driver error")
Expand Down Expand Up @@ -1135,8 +1131,6 @@ def apply_scenario_drivers(
else:
pass #enduse not define with scenario drivers

assert math.isnan(np.sum(fuel_y)) != 'nan' #TODO REMOVE

return fuel_y

def apply_specific_change(
Expand Down
3 changes: 2 additions & 1 deletion energy_demand/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#TODO Write out full result. Then write function to aggregate accordingly
#TODO SIMple aggregation. Write out sectormodel, enduse, region, fueltypes.... --> Do all aggregation based on that
#- Make that fuel swtich can be made in any industry sector irrespective of technologies
# Combine all switches of end_use_submodel #TODO
"""
import os
import sys
Expand Down Expand Up @@ -180,7 +181,7 @@ def energy_demand_model(regions, data, assumptions):
data['scenario_data']['population'] = data_loader.read_scenario_data(name_population_dataset)

# Read GVA sector specific data
data['scenario_data']['gva_industry_service'] = data_loader.read_scenario_data_gva(name_gva_dataset, all_dummy_data=False)
data['scenario_data']['gva_industry'] = data_loader.read_scenario_data_gva(name_gva_dataset, all_dummy_data=False)
data['scenario_data']['gva_per_head'] = data_loader.read_scenario_data(name_gva_dataset_per_head)

# Read sector assignement lookup values
Expand Down
26 changes: 18 additions & 8 deletions energy_demand/plotting/plotting_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ def run_all_plot_functions(
rs_enduses_sorted,
rs_color_list,
os.path.join(
result_paths['data_results_PDF'], "stacked_rs_country.pdf"))
result_paths['data_results_PDF'], "stacked_rs_country.pdf"),
plot_legend=True)

# Service
plt_stacked_enduse(
Expand All @@ -243,7 +244,8 @@ def run_all_plot_functions(
ss_enduses_sorted,
ss_color_list,
os.path.join(
result_paths['data_results_PDF'], "stacked_ss_country.pdf"))
result_paths['data_results_PDF'], "stacked_ss_country.pdf"),
plot_legend=True)

# Industry
plt_stacked_enduse(
Expand All @@ -252,7 +254,8 @@ def run_all_plot_functions(
is_enduses_sorted,
is_color_list,
os.path.join(
result_paths['data_results_PDF'], "stacked_is_country_.pdf"))
result_paths['data_results_PDF'], "stacked_is_country_.pdf"),
plot_legend=True)

# ------------------------------
# Plot annual demand for enduses for all submodels
Expand Down Expand Up @@ -692,7 +695,8 @@ def plt_stacked_enduse(
results_enduse_every_year,
enduses,
color_list,
fig_name
fig_name,
plot_legend=True
):
"""Plots stacked energy demand
Expand All @@ -713,7 +717,7 @@ def plt_stacked_enduse(
- Not possible to plot single year
https://matplotlib.org/examples/pylab_examples/stackplot_demo.html
"""
"""
nr_of_modelled_years = len(years_simulated)

x_data = np.array(years_simulated)
Expand Down Expand Up @@ -745,8 +749,13 @@ def plt_stacked_enduse(
# Add array with values for every year to list
y_value_arrays.append(y_values_enduse_yrs)

# Smooth line
x_data_smoothed, y_value_arrays_smoothed = plotting_results.smooth_data(x_data, y_value_arrays, num=40000)
# Try smoothing line
try:
x_data_smoothed, y_value_arrays_smoothed = plotting_results.smooth_data(
x_data, y_value_arrays, num=40000)
except:
x_data_smoothed = x_data
y_value_arrays_smoothed = y_value_arrays

# Convert to stacked
y_stacked = np.row_stack((y_value_arrays_smoothed))
Expand All @@ -768,7 +777,6 @@ def plt_stacked_enduse(
alpha=0.8,
colors=color_stackplots)

plot_legend=False
if plot_legend:
plt.legend(
legend_entries,
Expand Down Expand Up @@ -803,6 +811,8 @@ def plt_stacked_enduse(
#plt.title("ED whole UK", fontsize=10)

# Tight layout
fig.tight_layout()

plt.margins(x=0)
plt.savefig(fig_name)
plt.close()
Expand Down
5 changes: 3 additions & 2 deletions energy_demand/read_write/write_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import csv
import yaml
import numpy as np

from energy_demand.basic import basic_functions
from energy_demand.geography import write_shp
#from energy_demand.geography import write_shp
from energy_demand.technologies import tech_related
from energy_demand.basic import conversions
from energy_demand.plotting import plotting_results
#from energy_demand.plotting import plotting_results
from energy_demand.basic import date_prop

class ExplicitDumper(yaml.Dumper):
Expand Down
5 changes: 0 additions & 5 deletions energy_demand/scripts/init_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,6 @@ def switch_calculations(
data['assumptions'].is_fuel_tech_p_by,
data['assumptions'].base_yr)

# Update narrative of service
#ss_narrative_timesteps = get_all_narrative_timesteps(
# [ss_service_switches_inlc_cap, data['assumptions'].ss_fuel_switches, data['assumptions'].ss_capacity_switches])

# ======================================================================================
# Service switches
#
Expand Down Expand Up @@ -308,7 +304,6 @@ def switch_calculations(
# Residential
rs_sig_param_tech = {}
for enduse in data['enduses']['rs_enduses']:

rs_sig_param_tech[enduse] = sig_param_calc_incl_fuel_switch(
rs_narrative_timesteps,
data['assumptions'].base_yr,
Expand Down

0 comments on commit 4c5be27

Please sign in to comment.