Skip to content

Commit

Permalink
updated .main.py file for cluster run
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann committed Mar 7, 2019
1 parent 4b5d33e commit 9918984
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 121 deletions.
39 changes: 3 additions & 36 deletions energy_demand/basic/demand_supply_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@

def constrained_results(
results_constrained,
#results_unconstrained,
results_unconstrained_no_heating,
submodels_names,
technologies,
reg_nrs,
fueltype_nrs
):
"""Prepare results for energy supply model for
constrained model running mode (no heat is provided but
Expand All @@ -35,9 +32,9 @@ def constrained_results(
results_constrained : dict
Aggregated results in form
{technology: np.array((sector, region, fueltype, timestep))}
results_unconstrained : array
Restuls of unconstrained mode
np.array((sector, regions, fueltype, timestep))
results_unconstrained_no_heating : dict
Aggregated results without heating
{technology: np.array((sector, region, fueltype, timestep))}
submodels_names : list
Names of sectors fur supply model
technologies : dict
Expand Down Expand Up @@ -65,45 +62,16 @@ def constrained_results(
# Add all constrained results (technology specific results)
# Aggregate according to submodel, fueltype, technology, region, timestep
# ----------------------------------------
#summed_heating_demand = np.zeros((len(submodels_names), reg_nrs, fueltype_nrs, 8760))
for submodel_nr, submodel in enumerate(submodels_names):
for tech, fuel_tech in results_constrained.items():
# ----
# Technological simplifications because of different technology definition and because not all technologies are used in supply model
# ----
tech_simplified = model_tech_simplification(tech)
fueltype_str = technologies[tech_simplified].fueltype_str
fueltype_int = technologies[tech_simplified].fueltype_int
key_name = "{}_{}_{}".format(submodel, fueltype_str, tech_simplified)

# Out results
supply_results[key_name] = fuel_tech[submodel_nr][:, fueltype_int, :]

#for key_name, values in supply_results.items():
# if testing_functions.test_if_minus_value_in_array(values):
# raise Exception("TTT {} {}".format(key_name, np.sum(values)))
#--------------------------------
# Get all non heating related enduse
# --------------------------------
#assert not testing_functions.test_if_minus_value_in_array(results_unconstrained)
#assert not testing_functions.test_if_minus_value_in_array(summed_heating_demand)
#print(fuel_tech[submodel_nr][:, fueltype_int, :].shape)
#print("ddddd")
#print(np.sum(summed_heating_demand))
#print(np.sum(results_unconstrained))
#print(np.sum(results_unconstrained_no_heating))
#print(results_unconstrained.shape)
#print(summed_heating_demand.shape)
#print(results_unconstrained_no_heating.shape)
#print("DDdf --")
#assert not testing_functions.test_if_minus_value_in_array(results_unconstrained_no_heating)
# Substract constrained fuel from nonconstrained (total) fuel
#non_heating_ed_OLD = results_unconstrained - summed_heating_demand #sum(results_constrained.values())

#print(np.sum(non_heating_ed_OLD))
#print(np.sum(non_heating_ed))
#print(non_heating_ed_OLD.shape)
#print(non_heating_ed.shape)
assert not testing_functions.test_if_minus_value_in_array(results_unconstrained_no_heating)

# ---------------------------------
Expand All @@ -125,7 +93,6 @@ def constrained_results(
# --------------------------------------------
for key_name, values in supply_results.items():
if testing_functions.test_if_minus_value_in_array(values):
logging.info("info tt: {} {}".format(values, np.sum(values)))
raise Exception("Error d: Negative entry in results {} {}".format(key_name, np.sum(values)))

return supply_results
Expand Down
17 changes: 1 addition & 16 deletions energy_demand/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
Examples
python energy_demand/energy_demand/main.py C:/Users/cenv0553/ed/energy_demand/local_run_config_file.ini test_run
python energy_demand/energy_demand/main.py C:/Users/cenv0553/ed/energy_demand/local_run_config_file.ini h_max NF1 h_max
"""
import os
import sys
import time
import logging
from collections import defaultdict
import pandas as pd
import numpy as np

from energy_demand.basic import basic_functions, date_prop, demand_supply_interaction, testing_functions, lookup_tables
from energy_demand import model
Expand All @@ -24,8 +22,6 @@
from energy_demand.validation import lad_validation
from energy_demand.scripts import s_scenario_param, init_scripts, s_disaggregation
from energy_demand.plotting import fig_enduse_yh
from energy_demand import enduse_func
from energy_demand.technologies import tech_related

def energy_demand_model(
regions,
Expand Down Expand Up @@ -475,7 +471,7 @@ def energy_demand_model(
if config['CRITERIA']['mode_constrained']:
supply_results = demand_supply_interaction.constrained_results(
sim_obj.results_constrained,
sim_obj.results_unconstrained,
sim_obj.results_unconstrained_no_heating,
data['assumptions'].submodels_names,
data['assumptions'].technologies)
else:
Expand Down Expand Up @@ -541,18 +537,7 @@ def energy_demand_model(
data['weather_yr_result_paths']['data_results_model_run_results_txt'],
sim_obj.tot_fuel_y_enduse_specific_yh,
"out_enduse_specific")

# Check peak
fueltype_int = tech_related.get_fueltype_int('electricity')
national_hourly_demand = np.sum(sim_obj.ed_fueltype_regs_yh[fueltype_int], axis=0)
peak_day_electricity, _ = enduse_func.get_peak_day_single_fueltype(national_hourly_demand)
selected_hours = date_prop.convert_yearday_to_8760h_selection(peak_day_electricity)
else:
#write_data.write_residential_tot_demands(
# sim_yr,
# data['weather_yr_result_paths']['data_results_model_run_results_txt'],
# sim_obj.ed_residential_tot_reg_y,
# "ed_residential_tot_reg_y")
write_data.write_supply_results(
sim_yr,
"ed_fueltype_regs_yh",
Expand Down
58 changes: 2 additions & 56 deletions energy_demand/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def __init__(
"""Constructor
"""
self.curr_yr = assumptions.curr_yr

# ------------------------
# Create Dwelling Stock
# ------------------------
Expand Down Expand Up @@ -241,29 +240,15 @@ def aggregate_across_all_regs(
fueltypes_nr,
reg_nrs)

# ----------------------------------------------------
# Unconstrained results
# np.array of all fueltypes (submodel, region, fueltype, hours)
# ----------------------------------------------------
'''aggr_results['results_unconstrained'] = aggregate_result_unconstrained(
assumptions.nr_of_submodels,
assumptions.lookup_sector_enduses,
aggr_results['ed_enduse_fueltype_regs_yh'],
fueltypes_nr,
reg_nrs)'''

# ----------------------------------------------------
# Generate dict for supply model
# ----------------------------------------------------
if criterias['mode_constrained']:
aggr_results['supply_results'] = demand_supply_interaction.constrained_results(
aggr_results['results_constrained'],
#aggr_results['results_unconstrained'],
aggr_results['results_unconstrained_no_heating'],
assumptions.submodels_names,
technologies,
reg_nrs,
assumptions.fueltypes_nr)
technologies)
else:
aggr_results['supply_results'] = demand_supply_interaction.unconstrained_results(
aggr_results['results_unconstrained'],
Expand Down Expand Up @@ -671,44 +656,6 @@ def create_dwelling_stock(regions, curr_yr, data):
#data['ss_dw_stock'][region][curr_yr] = dw_stock.createNEWCASTLE_dwelling_stock(self.curr_yr)
return data

def aggregate_result_unconstrained(
nr_of_submodels,
lookup_sector_enduses,
ed_enduse_fueltype_regs_yh,
fueltypes_nr,
reg_nrs
):
"""Aggregated unconstrained results. Summarise energy demand
of unconstrained mode (heat is provided)
Arguments
---------
nr_of_submodels : int
Number of submodels
submodels_enduses : dict
Submodels and enduses
ed_enduse_fueltype_regs_yh : array
Fuel array
fueltypes_nr : int
Number of fueltypes
reg_nrs : int
Number of regions
Returns
--------
constrained_array : array
assumptions.nr_of_submodels, reg_nrs, fueltypes_nr, 8760
"""
constrained_array = np.zeros((nr_of_submodels, reg_nrs, fueltypes_nr, 8760), dtype="float")

for submodel_nr, enduse_array_nrs in lookup_sector_enduses.items():
for enduse_array_nr in enduse_array_nrs:
for fueltype_nr in range(fueltypes_nr):
for region_nr in range(reg_nrs):
constrained_array[submodel_nr][region_nr][fueltype_nr] += ed_enduse_fueltype_regs_yh[enduse_array_nr][fueltype_nr][region_nr]

return constrained_array

def aggregate_single_region(
reg_nrs,
lookup_enduses,
Expand Down Expand Up @@ -792,7 +739,6 @@ def aggregate_single_region(
if tech not in aggr_results['results_constrained']:
aggr_results['results_constrained'][tech] = np.zeros((len(submodel_to_idx), reg_nrs, fueltypes_nr, 8760), dtype="float")
aggr_results['results_constrained'][tech][submodel_nr][reg_array_nr][tech_fueltype] += reshaped_fuel

else:
aggr_results['results_unconstrained_no_heating'][submodel_nr][reg_array_nr][tech_fueltype] += reshaped_fuel
else:
Expand All @@ -805,7 +751,7 @@ def aggregate_single_region(

for fueltype_nr, fuels_8760 in enumerate(fueltype_yh_8760):
aggr_results['ed_enduse_fueltype_regs_yh'][enduse_array_nr][fueltype_nr][reg_array_nr] += fuels_8760

aggr_results['results_unconstrained_no_heating'][submodel_nr][reg_array_nr][fueltype_nr] += fuels_8760

return aggr_results
Expand Down
6 changes: 2 additions & 4 deletions energy_demand/read_write/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ def ss_read_shapes_enduse_techs(ss_shapes_dh, ss_shapes_yd):
return ss_all_tech_shapes_dh, ss_all_tech_shapes_yd

def read_scenario_data(path_to_csv, value_name='value', region_name='region'):
"""
"""Function to read in scenario data
"""
data = {}

Expand All @@ -1273,8 +1273,8 @@ def read_scenario_data(path_to_csv, value_name='value', region_name='region'):
with open(path_to_csv, 'r') as csvfile:
rows = csv.reader(csvfile, delimiter=',')
headings = next(rows) # Skip first row
for row in rows:

for row in rows:
region = str(row[read_data.get_position(headings, region_name)])
year = int(float(row[read_data.get_position(headings, 'timestep')]))
value = float(row[read_data.get_position(headings, value_name)])
Expand Down Expand Up @@ -1310,13 +1310,11 @@ def read_scenario_data_gva(path_to_csv, region_name='region', value_name='value'
for year_dummy in range(2015, 2051):
for sector_dummy in range(1, 47):
dummy_sector_value = 1

try:
out_dict[year_dummy][region][sector_dummy] = dummy_sector_value
except KeyError:
out_dict[year_dummy] = defaultdict(dict)
out_dict[year_dummy][region][sector_dummy] = dummy_sector_value

else:
if row[read_data.get_position(headings, 'timestep')] == '': #No data provided
region = str(row[read_data.get_position(headings, region_name)])
Expand Down
2 changes: 1 addition & 1 deletion energy_demand/result_processing/p3_weather_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def main(
[results_container['regional_share_national_peak'][simulation_yr_to_plot]],
columns=data['regions'])
regional_share_national_peak = regional_share_national_peak.append(realisation_data)

# --Regional percentage of national peak demand per person
realisation_data = pd.DataFrame(
[results_container['regional_share_national_peak_pp'][simulation_yr_to_plot]],
Expand Down
4 changes: 2 additions & 2 deletions energy_demand/scripts/s_post_installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def post_install_setup(args):
basic_functions.create_folder(folder)

print("... Read in residential submodel load profiles", flush=True)
'''s_rs_raw_shapes.run(
s_rs_raw_shapes.run(
data['paths'],
data['local_paths'],
base_yr)
Expand All @@ -83,7 +83,7 @@ def post_install_setup(args):
data['lookups'])

# Input data preparation
print("Generate additional data", flush=True)'''
print("Generate additional data", flush=True)

# Extract NISMOD population data
path_to_zip_file = os.path.join(local_data_path,"population-economic-smif-csv-from-nismod-db.zip")
Expand Down
8 changes: 4 additions & 4 deletions energy_demand/scripts/weather_scripts/weather_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ def write_weather_data(data_list):
stations_data[station_id_cnt] = station_data

# Weather station metadata
station_lon = row[1]
station_lat = row[0]
station_lon = row[1]

station_id = "station_id_{}".format(station_id_cnt)

Expand Down Expand Up @@ -259,11 +259,11 @@ def weather_dat_prepare(data_path, result_path, years_to_clean=range(2020, 2049)
# Create folder
path_realization = os.path.join(path_year, realization_name)
basic_functions.create_folder(path_realization)

# Data to extract
path_tasmin = os.path.join(path_realizations, realization_name, 'daily', 'WAH_{}_tasmin_daily_g2_{}.nc'.format(realization_name, year))
path_tasmax = os.path.join(path_realizations, realization_name, 'daily', 'WAH_{}_tasmax_daily_g2_{}.nc'.format(realization_name, year))

# Load data
print(" ..load data", flush=True)
df_min = get_temp_data_from_nc(path_tasmin, 'tasmin')
Expand Down Expand Up @@ -295,7 +295,7 @@ def weather_dat_prepare(data_path, result_path, years_to_clean=range(2020, 2049)
print("... finished cleaning weather data")

clean_original_files = False
stich_weather_scenario = True
stich_weather_scenario = False

if clean_original_files:
# ------------------------
Expand Down
5 changes: 3 additions & 2 deletions energy_demand/wrapper_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ def load_data_before_simulation(
name_population_dataset = config['DATA_PATHS']['path_population_data_for_disaggregation_msoa']
else:
name_population_dataset = config['DATA_PATHS']['path_population_data_for_disaggregation_lad']

data['pop_for_disag'] = data_loader.read_scenario_data(
name_population_dataset,
region_name='lad_uk_2016',
value_name='population')
region_name='region',
value_name='value')

# ------------------------------------------------
# Load building related data
Expand Down

0 comments on commit 9918984

Please sign in to comment.