Skip to content

Commit

Permalink
rewrite base_assumptions to generate YAML parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann committed Nov 10, 2017
1 parent 48486ea commit 2e5724c
Show file tree
Hide file tree
Showing 19 changed files with 1,736 additions and 144 deletions.
358 changes: 271 additions & 87 deletions energy_demand/assumptions/base_assumptions.py

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions energy_demand/basic/basic_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,43 @@ def get_month_from_string(month_string):
month = 12

return int(month)

def get_month_from_int(month_int):
"""Convert string month to int month with Jan == 1
Argument
--------
month_string : str
Month given as a string
Returns
--------
month : int
Month as an integer (jan = 1, dez = 12)
"""
if month_int == 1:
month_str = 'Jan'
elif month_int == 2:
month_str = 'Feb'
elif month_int == 3:
month_str = 'Mar'
elif month_int == 4:
month_str = 'Apr'
elif month_int == 5:
month_str = 'May'
elif month_int == 6:
month_str = 'Jun'
elif month_int == 7:
month_str = 'Jul'
elif month_int == 8:
month_str = 'Aug'
elif month_int == 9:
month_str = 'Sep'
elif month_int == 10:
month_str = 'Oct'
elif month_int == 11:
month_str = 'Nov'
elif month_int == 12:
month_str = 'Dec'

return str(month_str)
7 changes: 5 additions & 2 deletions energy_demand/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ def run_model(args):
data['sim_param']['base_yr'] = 2015
data['sim_param']['simulated_yrs'] = [2015, 2018, 2025, 2050] #[2015, 2020, 2025]

data['assumptions'] = base_assumptions.load_assumptions(
base_assumptions.load_assumptions(
data['paths'], data['enduses'], data['lookups'], data['fuels'], data['sim_param'])

data['assumptions'] = read_data.read_param_yaml(data['paths']['yaml_parameters'])

data['assumptions']['seasons'] = date_prop.read_season(year_to_model=2015)
data['assumptions'] = base_assumptions.update_assumptions(data['assumptions'])
data['assumptions']['technologies'] = base_assumptions.update_assumptions(data['assumptions']['technologies'], data['assumptions']['eff_achieving_factor']['factor_achieved'])
data['tech_lp'] = data_loader.load_data_profiles(data['paths'], data['local_paths'], data['assumptions'])
data['weather_stations'], data['temp_data'] = data_loader.load_temp_data(data['local_paths'])

Expand Down
39 changes: 16 additions & 23 deletions energy_demand/enduse_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,8 @@ def __init__(
self.fuel_new_y = apply_smart_metering(
enduse,
self.fuel_new_y,
assumptions,
sim_param,
assumptions['enduse_overall_change_ey']['year_until_changed'])
assumptions['smart_meter_assump'],
sim_param)
logging.debug("... Fuel train C: " + str(np.sum(self.fuel_new_y)))

# --Enduse specific consumption change in %
Expand Down Expand Up @@ -335,8 +334,8 @@ def __init__(
enduse,
sim_param,
loadfactor_yd_cy,
assumptions['demand_management'],
assumptions['demand_management']['year_until_changed'])
assumptions['demand_management']['enduses_demand_managent'],
assumptions['demand_management']['demand_management_year_until_changed'])

if not peak_shift_crit:
self.fuel_yh = fuel_yh
Expand Down Expand Up @@ -1014,17 +1013,14 @@ def apply_heat_recovery(enduse, assumptions, service, crit_dict, base_sim_param)
# Fraction of heat recovered until end_year
heat_recovered_p_by = assumptions['heat_recovered'][enduse]

# Year until recovered
year_until_recovered = assumptions['heat_recovered']['year_until_recovered']

if heat_recovered_p_by == 0:
return service
else:
# Fraction of heat recovered in current year
sig_diff_factor = diffusion_technologies.sigmoid_diffusion(
base_sim_param['base_yr'],
base_sim_param['curr_yr'],
year_until_recovered,
assumptions['heat_recovered']['heat_recovered_year_until_changed'],
assumptions['other_enduse_mode_info']['sigmoid']['sig_midpoint'],
assumptions['other_enduse_mode_info']['sigmoid']['sig_steeppness'])

Expand Down Expand Up @@ -1248,7 +1244,7 @@ def apply_climate_change(enduse, fuel_new_y, cooling_factor_y, heating_factor_y,

return fuel_new_y

def apply_smart_metering(enduse, fuel_y, assumptions, base_sim_param, year_until_changed):
def apply_smart_metering(enduse, fuel_y, smart_meter_assump, base_sim_param):
"""Calculate fuel savings depending on smart meter penetration
Arguments
Expand All @@ -1257,8 +1253,8 @@ def apply_smart_metering(enduse, fuel_y, assumptions, base_sim_param, year_until
Enduse
fuel_y : array
Yearly fuel per fueltype
assumptions : dict
assumptions
smart_meter_assump : dict
smart meter assumptions
base_sim_param : dict
Base simulation parameters
Expand All @@ -1274,25 +1270,22 @@ def apply_smart_metering(enduse, fuel_y, assumptions, base_sim_param, year_until
- In the assumptions the maximum penetration and also the
generally fuel savings for each enduse can be defined.
"""
if enduse in assumptions['savings_smart_meter']:

# Year until changed
year_until_changed = year_until_changed
if enduse in smart_meter_assump['savings_smart_meter']:

# Sigmoid diffusion up to current year
sigm_factor = diffusion_technologies.sigmoid_diffusion(
base_sim_param['base_yr'],
base_sim_param['curr_yr'],
year_until_changed,
assumptions['smart_meter_diff_params']['sig_midpoint'],
assumptions['smart_meter_diff_params']['sig_steeppness'])
smart_meter_assump['smart_meter_year_until_changed'],
smart_meter_assump['smart_meter_diff_params']['sig_midpoint'],
smart_meter_assump['smart_meter_diff_params']['sig_steeppness'])

# Smart Meter penetration (percentage of people having smart meters)
penetration_by = assumptions['smart_meter_p_by']
penetration_cy = assumptions['smart_meter_p_by'] + (
sigm_factor * (assumptions['smart_meter_p_ey'] - assumptions['smart_meter_p_by']))
penetration_by = smart_meter_assump['smart_meter_p_by']
penetration_cy = smart_meter_assump['smart_meter_p_by'] + (
sigm_factor * (smart_meter_assump['smart_meter_p_future'] - smart_meter_assump['smart_meter_p_by']))

savings = assumptions['savings_smart_meter'][enduse]
savings = smart_meter_assump['savings_smart_meter'][enduse]
saved_fuel = fuel_y * (penetration_by - penetration_cy) * savings
fuel_y = fuel_y - saved_fuel

Expand Down
4 changes: 2 additions & 2 deletions energy_demand/geography/weather_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def __init__(
temp_cy = change_temp_climate(
temp_by,
assumptions['yeardays_month_days'],
assumptions['climate_change_temp_diff_month'],
assumptions['climate_change_temp_diff_month']['temps'],
sim_param,
assumptions['climate_change_temp_diff_year_until_changed'])
assumptions['climate_change_temp_diff_month']['climate_change_temp_diff_year_until_changed'])

#Change temp_cy depending on climate assumptions
rs_t_base_heating_cy = hdd_cdd.sigm_temp(
Expand Down
19 changes: 14 additions & 5 deletions energy_demand/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
https://nismod.github.io/docs/smif-prerequisites.html#sector-modeller
# TODOEnd year diffusion assumption
'''
5'''
import os
import sys
import logging
Expand All @@ -17,12 +17,11 @@
from energy_demand import energy_model
from energy_demand.assumptions import base_assumptions
from energy_demand.read_write import data_loader
from energy_demand.read_write import read_data
from energy_demand.basic import testing_functions as testing
from energy_demand.validation import lad_validation
from energy_demand.plotting import plotting_results
from energy_demand.basic import logger_setup
from energy_demand.read_write import write_data
from energy_demand.read_write import write_data, read_data
from energy_demand.basic import basic_functions
from energy_demand.basic import date_prop

Expand Down Expand Up @@ -105,14 +104,24 @@ def energy_demand_model(data, fuel_in=0, fuel_in_elec=0):
data['sim_param']['base_yr'] = 2015
data['sim_param']['simulated_yrs'] = [2015, 2018, 2025, 2050]

data['assumptions'] = base_assumptions.load_assumptions(
# TODO: MOVE TO SCENARIO INIT
base_assumptions.load_assumptions(
data['paths'], data['enduses'], data['lookups'], data['fuels'], data['sim_param'])

# Read parameters from yaml file
# ------------------------------
print("Read assumptions from file: " + str(data['paths']['yaml_parameters']))
data['assumptions'] = read_data.read_param_yaml(data['paths']['yaml_parameters'])

data['assumptions']['seasons'] = date_prop.read_season(year_to_model=2015)
data['assumptions']['model_yeardays_daytype'], data['assumptions']['yeardays_month'], data['assumptions']['yeardays_month_days'] = date_prop.get_model_yeardays_datype(year_to_model=2015)
data['tech_lp'] = data_loader.load_data_profiles(data['paths'], data['local_paths'], data['assumptions'])
data['assumptions'] = base_assumptions.update_assumptions(data['assumptions'])
data['assumptions']['technologies'] = base_assumptions.update_assumptions(data['assumptions']['technologies'], data['assumptions']['eff_achieving_factor']['factor_achieved'])
data['weather_stations'], data['temp_data'] = data_loader.load_temp_data(data['local_paths'])

# ------------------------------


# ==========
data['lu_reg'] = data_loader.load_LAC_geocodes_info(data['local_paths']['path_dummy_regions'])
data = data_loader.dummy_data_generation(data)
Expand Down
5 changes: 4 additions & 1 deletion energy_demand/read_write/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,10 @@ def load_paths(path):
'lp_elec_primary_heating': os.path.join(
path, 'config_data', 'submodel_residential', 'lp_elec_primary_heating_HES.csv'),
'lp_elec_secondary_heating': os.path.join(
path, 'config_data', 'submodel_residential', 'lp_elec_secondary_heating_HES.csv')
path, 'config_data', 'submodel_residential', 'lp_elec_secondary_heating_HES.csv'),

'yaml_parameters': os.path.join(path, 'yaml_parameters.yml'),
'yaml_parameters_complete': os.path.join(path, 'yaml_parameters_complete.yml')
}

return paths
Expand Down
9 changes: 9 additions & 0 deletions energy_demand/read_write/read_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@
import csv
import json
import logging
import yaml
from collections import defaultdict
import numpy as np
from energy_demand.technologies import tech_related
from energy_demand.read_write import read_weather_data
from energy_demand.profiles import load_profile

def read_param_yaml(path):
"""
"""
with open(path, 'r') as ymlfile:
parameter_dict = yaml.load(ymlfile)

return parameter_dict

def read_in_results(path_runs, lookups, seasons, model_yeardays_daytype):
"""Read and post calculate
results from txt files
Expand Down
50 changes: 50 additions & 0 deletions energy_demand/read_write/write_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,56 @@
import numpy as np
import configparser
from energy_demand.basic import basic_functions
import yaml
from yaml import Loader, Dumper
import collections

def dump(data, file_path):
"""Write plain data to a file as yaml
Parameters
----------
file_path : str
The path of the configuration file to write
data
Data to write (should be lists, dicts and simple values)
"""
with open(file_path, 'w') as file_handle:
return yaml.dump(data, file_handle, Dumper=Dumper, default_flow_style=False)

def write_yaml_param_complete(path_yaml, dict_to_dump):
"""Write all assumption parameters to YAML
#TODO :ORDER
"""
list_to_dump_complete = []

for dict_key, dict_values in dict_to_dump.items():
try:
parameter_infos = dict_values['param_infos']

for paramter_info in parameter_infos:
dict_to_dump_complete = {} #collections.OrderedDict()
dict_to_dump_complete['suggested_range'] = paramter_info['suggested_range']
dict_to_dump_complete['absolute_range'] = paramter_info['absolute_range']
dict_to_dump_complete['description'] = paramter_info['description']
dict_to_dump_complete['name'] = paramter_info['name']
dict_to_dump_complete['default_value'] = paramter_info['default_value']
dict_to_dump_complete['units'] = paramter_info['units']
list_to_dump_complete.append(dict_to_dump_complete)
except:
pass #not correctly formated assumption

# Dump list
dump(list_to_dump_complete, path_yaml)
return

def write_yaml_param(path_yaml, dict_to_dump):
"""Write all assumption parameters to YAML
"""
with open(path_yaml, 'w') as file_handle:
yaml.dump(dict_to_dump, file_handle)
return

def write_simulation_inifile(path, sim_param, enduses, assumptions, reg_nrs):
"""Write .ini file with simulation parameters
Expand Down
11 changes: 7 additions & 4 deletions energy_demand/scripts/init_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from energy_demand.basic import basic_functions
from energy_demand.basic import logger_setup
from energy_demand.basic import date_prop
from energy_demand.read_write import read_data, write_data

def post_install_setup(args):
"""Run initialisation scripts
Expand Down Expand Up @@ -54,10 +55,11 @@ def post_install_setup(args):
data['sim_param']['base_yr'] = 2015
data['sim_param']['simulated_yrs'] = [2015, 2020, 2025]

data['assumptions'] = base_assumptions.load_assumptions(
base_assumptions.load_assumptions(
data['paths'], data['enduses'], data['lookups'], data['fuels'], data['sim_param'])
data['assumptions'] = read_data.read_param_yaml(data['paths']['yaml_parameters'])
data['assumptions']['seasons'] = date_prop.read_season(year_to_model=2015)
data['assumptions'] = base_assumptions.update_assumptions(data['assumptions'])
data['assumptions']['technologies'] = base_assumptions.update_assumptions(data['assumptions']['technologies'], data['assumptions']['eff_achieving_factor']['factor_achieved'])

# Delete all previous data from previous model runs
basic_functions.del_previous_setup(data['local_paths']['data_processed'])
Expand Down Expand Up @@ -119,10 +121,11 @@ def scenario_initalisation(path_data_energy_demand, data=False):
data['sim_param']['simulated_yrs'] = [2015, 2020, 2025]
data['sim_param']['curr_yr'] = 2015 #NEEDED

data['assumptions'] = base_assumptions.load_assumptions(
base_assumptions.load_assumptions(
data['paths'], data['enduses'], data['lookups'], data['fuels'], data['sim_param'])
data['assumptions'] = read_data.read_param_yaml(data['paths']['yaml_parameters'])
data['assumptions']['seasons'] = date_prop.read_season(year_to_model=2015)
data['assumptions'] = base_assumptions.update_assumptions(data['assumptions'])
data['assumptions']['technologies'] = base_assumptions.update_assumptions(data['assumptions']['technologies'], data['assumptions']['eff_achieving_factor']['factor_achieved'])

data['lu_reg'] = data_loader.load_LAC_geocodes_info(data['local_paths']['path_dummy_regions'])
data = data_loader.dummy_data_generation(data)
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 @@ -283,7 +283,7 @@ def rs_disaggregate(lu_reg, sim_param, data, rs_national_fuel):
sim_param,
lu_reg,
data['temp_data'],
data['assumptions']['smart_meter_diff_params'],
data['assumptions']['smart_meter_assump']['smart_meter_diff_params'],
data['assumptions']['rs_t_base_heating'],
data['reg_coord'],
data['weather_stations'])
Expand Down
12 changes: 12 additions & 0 deletions kernel-b2106b65-0cb8-4928-8ad9-8a3cc7ed39ef.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"shell_port": 63583,
"iopub_port": 63584,
"stdin_port": 63585,
"control_port": 63586,
"hb_port": 63587,
"ip": "127.0.0.1",
"key": "ac60daed-38c2cb2e371228d413597b1c",
"transport": "tcp",
"signature_scheme": "hmac-sha256",
"kernel_name": ""
}

0 comments on commit 2e5724c

Please sign in to comment.