Skip to content

Commit

Permalink
switches work
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann authored and Sven Eggimann committed Sep 13, 2018
1 parent e111ad2 commit 9589f2a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 42 deletions.
6 changes: 1 addition & 5 deletions energy_demand/config_data/06-switches/switches_service.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
enduse,tech,service_share_ey,switch_yr,sector
rs_space_heating,heat_pumps_electricity,0.4,2030
rs_space_heating,heat_pumps_electricity,0.6,2050
rs_water_heating,heat_pumps_electricity,0.2,2050
ss_space_heating,heat_pumps_electricity,0.2,2050
ss_water_heating,heat_pumps_electricity,0.2,2050
is_space_heating,heat_pumps_electricity,0.2,2050
ss_water_heating,heat_pumps_electricity,0.2,2050
13 changes: 9 additions & 4 deletions energy_demand/scripts/init_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
from collections import defaultdict
import numpy as np
import time

from energy_demand.geography import spatial_diffusion
from energy_demand.read_write import read_data
Expand Down Expand Up @@ -212,7 +213,7 @@ def switch_calculations(
for submodel in data['assumptions'].submodels_names:
for enduse in data['enduses'][submodel]:
for sector in data['sectors'][submodel]:

print("============= Calculating Sigmoid {} {}".format(enduse, sector), flush=True)
sig_param_tech[enduse][sector] = sig_param_calc_incl_fuel_switch(
narrative_timesteps,
data['assumptions'].base_yr,
Expand Down Expand Up @@ -640,17 +641,21 @@ def sig_param_calc_incl_fuel_switch(
for region in regions:
sig_param_tech[switch_yr][region] = sig_param_tech_all_regs_value
else:
logging.info("... calc region specific parameters of `{}` for year `{}` sector: {}".format(
enduse, switch_yr, sector))

#logging.debug("... calc region specific parameters of `{}` for year `{}` sector: {}".format(enduse, switch_yr, sector))
#print("STARTING REGIONS ", flush=True)
start = time.time()
for region in regions:
#print("REGION " + str(region), flush=True)
sig_param_tech[switch_yr][region] = s_generate_sigmoid.tech_sigmoid_parameters(
switch_yr,
switch_yr_start,
technologies,
l_values_sig[region],
s_tech_by_p,
s_tech_switched_p[region][switch_yr])
end = time.time()
#print("FINISHED REGIONS", flush=True)
print("USED TIME: " + str(end - start), flush=True)

return sig_param_tech

Expand Down
65 changes: 34 additions & 31 deletions energy_demand/scripts/s_generate_sigmoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
"""
import logging
from collections import defaultdict
import math
import numpy as np
from scipy.optimize import curve_fit
from energy_demand.technologies import diffusion_technologies
from energy_demand.plotting import plotting_program
#from energy_demand.plotting import plotting_program

def calc_sigmoid_parameters(
l_value,
xdata,
ydata,
fit_assump_init=0.001,
error_range=0.00002
error_range=0.00002,
number_of_iterations=1000
):
"""Calculate sigmoid parameters. Check if fitting is good enough.
Expand All @@ -41,7 +41,7 @@ def calc_sigmoid_parameters(
Note
-------
`error_range` can be changed if the plotting is weird. If you increase
chances are however hiigher that the fitting does not work anymore.
chances are however higher that the fitting does not work anymore.
Returns
------
Expand All @@ -52,13 +52,11 @@ def calc_sigmoid_parameters(
# Generate possible starting parameters for fit
# ---------------------------------------------
start_param_list = [
0.0, 1.0, 0.0001, 0.001, 0.01,
0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35,
0.4, 0.45, 0.5, 0.55, 0.6, 0.65,
10, 20, 30, 40, 50, 60, 70, 80,
90, 100.0, 200.0, 400.0, 500.0, 1000, 10000]

assert l_value >= ydata[0]
0.0, 1.0, 0.0001, 0.001, 0.01] #,
#0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35,
#0.4, 0.45, 0.5, 0.55, 0.6, 0.65,
#10, 20, 30, 40, 50, 60, 70, 80,
#90, 100.0, 200.0, 400.0, 500.0, 1000, 10000]

# ---------------------------------------------
# Fit
Expand Down Expand Up @@ -96,7 +94,7 @@ def calc_sigmoid_parameters(
xdata,
ydata,
start_parameters,
number_of_iterations=1000)
number_of_iterations=number_of_iterations)

# Test if start paramters are identical to fitting parameters
if (crit_plus_minus == 'plus' and fit_parameter[1] < 0) or (
Expand Down Expand Up @@ -137,7 +135,7 @@ def calc_sigmoid_parameters(
cnt += 1
else:
successfull = True

'''logging.info(
".... fitting successfull %s %s %s", fit_measure_p_ey, fit_measure_p_by, fit_parameter)
plotting_program.plotout_sigmoid_tech_diff(
Expand Down Expand Up @@ -512,8 +510,9 @@ def tech_sigmoid_parameters(
differences in percentual service demand.
"""
rounding_accuracy = 4 # Criteria how much difference in % can be rounded
linear_approx_crit = 0.001 # Criteria of difference in decimal from which on linear approximation is used
error_range = 0.0002 # Error how close the fit must be
linear_approx_crit = 0.001 # Criteria to simplify with linear approximation if difference is smaller (decimal) # 0.001
error_range = 0.0002 # Error how close the fit must be # 0.0002
number_of_iterations = 100 # Number of iterations of sigmoid fitting algorithm # 1000

# Technologies to apply calculation
installed_techs = s_tech_switched_p.keys()
Expand All @@ -525,7 +524,7 @@ def tech_sigmoid_parameters(
pass
else:
for tech in installed_techs:

#print("TECH " + str(tech)) #, flush=True)
# --------
# Test whether technology has the market entry before or after base year,
# If afterwards, set very small number in market entry year
Expand All @@ -534,7 +533,7 @@ def tech_sigmoid_parameters(
point_x_by = technologies[tech].market_entry
point_y_by = fit_assump_init
else:
point_x_by = switch_yr_start # Base year
point_x_by = switch_yr_start # Base year
point_y_by = s_tech_by_p[tech] # Base year service share

# If the base year is the market entry year use a very small number
Expand Down Expand Up @@ -570,12 +569,13 @@ def tech_sigmoid_parameters(

# Test if end year share is larger than technological maximum
# Test wheter maximum diffusion is larger than simulated end year share
if math.isnan(ydata[1]):
pass
else:
assert ydata[1] <= l_values[tech] + linear_approx_crit
#if math.isnan(ydata[1]):
# pass
#else:
#assert ydata[1] <= l_values[tech] + linear_approx_crit
assert ydata[1] <= l_values[tech] + linear_approx_crit

# If no change in by to ey but not zero (lineare change)
# If no change in by to ey but not zero (linear change)
if (round(point_y_by, rounding_accuracy) == round(point_y_ey, rounding_accuracy)) and (
point_y_ey != fit_assump_init) and (
point_y_by != fit_assump_init):
Expand All @@ -595,42 +595,45 @@ def tech_sigmoid_parameters(

# If difference is smaller than a certain share, approximate with linear
if abs(ydata[1] - ydata[0]) < linear_approx_crit:

#logging.info("Linear approximation...")
sig_params[tech]['midpoint'] = 'linear'
sig_params[tech]['steepness'] = 'linear'
sig_params[tech]['l_parameter'] = 'linear'

try:
# Parameter fitting
##print("----start fitting" ) #, flush=True)
fit_parameter = calc_sigmoid_parameters(
l_values[tech],
xdata,
ydata,
fit_assump_init=fit_assump_init,
error_range=error_range)
error_range=error_range,
number_of_iterations=number_of_iterations)

sig_params[tech]['midpoint'] = fit_parameter[0] # midpoint (x0)
sig_params[tech]['steepness'] = fit_parameter[1] # Steepnes (k)
sig_params[tech]['midpoint'] = fit_parameter[0]
sig_params[tech]['steepness'] = fit_parameter[1]
sig_params[tech]['l_parameter'] = l_values[tech] # maximum p
#logging.debug("... successfull fitting")
##print("-------------successfull fitting") #, flush=True)

if plot_sigmoid_diffusion:
'''if plot_sigmoid_diffusion:
plotting_program.plotout_sigmoid_tech_diff(
l_values[tech],
tech,
xdata,
ydata,
fit_parameter,
plot_crit=True,
close_window_crit=True)
close_window_crit=True)'''
except:
"""If sigmoid fitting failed, implement linear diffusion
The sigmoid diffusion may fail if the fitting does not work
because the points to fit are too similar.
because the points to fit are too similar. This could be
improved by increasing the number of iterations 'number_of_iterations'
at higher computation costs
"""
logging.warning("Instead of sigmoid a linear approximation is used %s %s %s %s", s_tech_by_p[tech], tech, xdata, ydata)
#logging.warning("Instead of sigmoid a linear approximation is used %s %s %s", tech, xdata, ydata)
sig_params[tech]['midpoint'] = 'linear'
sig_params[tech]['steepness'] = 'linear'
sig_params[tech]['l_parameter'] = 'linear'
Expand Down
4 changes: 2 additions & 2 deletions energy_demand/validation/lad_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ def spatial_validation(
result_dict['modelled_demand'][reg_geocode] = subnational_modelled[reg_geocode]

except KeyError:
logging.warning(
logging.debug(
"Sub-national spatial validation: No fuel for region %s", reg_geocode)

# --------------------
Expand Down Expand Up @@ -866,7 +866,7 @@ def spatial_validation_multiple(
result_dict['real_demand'][reg_geocode] = subnational_real[reg_geocode]
result_dict['modelled_demand'][reg_geocode] = subnational_modelled[reg_geocode]
except KeyError:
logging.warning(
logging.debug(
"Sub-national spatial validation: No fuel for region %s", reg_geocode)

# --------------------
Expand Down

0 comments on commit 9589f2a

Please sign in to comment.