Skip to content

Commit

Permalink
update printing and aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann committed Mar 5, 2019
1 parent 8ef1c97 commit 273204f
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 108 deletions.
4 changes: 2 additions & 2 deletions energy_demand/basic/demand_supply_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import pandas as pd

from energy_demand.basic import date_prop, testing_functions, lookup_tables, basic_functions
from energy_demand.basic import date_prop, testing_functions, lookup_tables

def constrained_results(
results_constrained,
Expand Down Expand Up @@ -85,7 +85,7 @@ def constrained_results(
# Substract constrained fuel from nonconstrained (total) fuel
non_heating_ed = results_unconstrained - sum(results_constrained.values())

#assert not testing_functions.test_if_minus_value_in_array(results_unconstrained)
assert not testing_functions.test_if_minus_value_in_array(non_heating_ed)

# ---------------------------------
# Add non_heating for all fueltypes
Expand Down
6 changes: 4 additions & 2 deletions energy_demand/basic/testing_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from energy_demand.basic import basic_functions
from energy_demand.basic import lookup_tables

def test_if_minus_value_in_array(arraytotest, tolerance_min_max=0.00000000001):
def test_if_minus_value_in_array(arraytotest):#, tolerance_min_max=0.00000000001):
"""Test if array has negative value according to a tolerance
criteria
Expand All @@ -25,7 +25,9 @@ def test_if_minus_value_in_array(arraytotest, tolerance_min_max=0.00000000001):

if len(only_neg_elements) > 0:
for element in only_neg_elements:
if (element > tolerance_min_max) or (element < tolerance_min_max):
#if (element > tolerance_min_max) or (element < tolerance_min_max):
if element < 0:
print("---")
print("Sum of all negative: " + str(np.sum(only_neg_elements)))
print("Average negative value: " + str(np.sum(only_neg_elements) / len(only_neg_elements)))
print("Smalles value: " + str(np.min(only_neg_elements)))
Expand Down
15 changes: 4 additions & 11 deletions energy_demand/enduse_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ def __init__(
self.fuel_yh = 0
self.enduse_techs = []
else:
#print("------INFO {} {} {} {}".format(self.enduse, sector, region, curr_yr))
#print("strarr")
#logging.debug("------INFO {} {} {} {}".format(self.enduse, sector, region, curr_yr))
# Get technologies of enduse
self.enduse_techs = get_enduse_techs(fuel_tech_p_by)

Expand Down Expand Up @@ -198,7 +197,7 @@ def __init__(
strategy_vars['generic_fuel_switch'],
self.fuel_y)
self.fuel_y = _fuel_new_y
#print("eeeemd")

# ----------------------------------
# Hourly Disaggregation
# ----------------------------------
Expand All @@ -225,8 +224,6 @@ def __init__(
make_all_flat=make_all_flat)
else:
#If technologies are defined for an enduse
#print("A")
# Get enduse specific configurations
mode_constrained = get_enduse_configuration(
criterias['mode_constrained'],
enduse,
Expand All @@ -235,7 +232,6 @@ def __init__(
# ------------------------------------
# Calculate regional energy service
# ------------------------------------
#print("A " + str(np.sum(self.fuel_y)))
s_tot_y_cy, s_tech_y_by = fuel_to_service(
enduse,
sector,
Expand Down Expand Up @@ -302,7 +298,6 @@ def __init__(
if self.flat_profile_crit:
pass
else:
#print("B")
fuel_yh = calc_fuel_tech_yh(
enduse,
sector,
Expand All @@ -312,7 +307,7 @@ def __init__(
fueltypes_nr,
fueltypes,
mode_constrained)
#print("C")

# --------------------------------------
# Peak shifting
# --------------------------------------
Expand All @@ -322,7 +317,6 @@ def __init__(
# If no demand management improvenent, no peak shifting
if strategy_vars['dm_improvement'][enduse][curr_yr] == 0:
self.techs_fuel_yh = fuel_yh
#print("D1")
else:
self.techs_fuel_yh = load_shifting_multiple_tech(
enduse,
Expand All @@ -331,15 +325,14 @@ def __init__(
assumptions.technologies,
fuel_yh,
param_lf_improved_cy=strategy_vars['dm_improvement'][enduse][curr_yr])
#print("D2")
else:
self.fuel_yh = load_shifting(
enduse,
fuel_yh,
mode_constrained=False,
param_lf_improved_cy=strategy_vars['dm_improvement'][enduse][curr_yr],
make_all_flat=make_all_flat)
#print("D3")

if testing_functions.test_if_minus_value_in_array(self.fuel_yh):
raise Exception("Minus fuel value detected: {} {} {}".format(enduse, sector, np.sum(self.fuel_yh)))

Expand Down
1 change: 0 additions & 1 deletion energy_demand/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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.basic import lookup_tables, date_prop, basic_functions, conversions
from energy_demand import enduse_func
from energy_demand.technologies import tech_related

Expand Down
57 changes: 26 additions & 31 deletions energy_demand/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
testable and refactored going forward, so some profiling would help direct
attention to where further work is most appropriate
"""
import copy
import logging
from collections import defaultdict
import numpy as np

Expand All @@ -43,7 +43,6 @@ def tqdm(iterator, *_, **__):
from energy_demand.profiles import load_factors
from energy_demand.profiles import generic_shapes
from energy_demand.basic import demand_supply_interaction
from energy_demand.basic import testing_functions
from energy_demand.geography import weather_station_location

class EnergyDemandModel(object):
Expand Down Expand Up @@ -78,7 +77,7 @@ def __init__(
# ------------------------
# Create Dwelling Stock
# ------------------------
logging.debug("... generating cy dwelling stocks", flush=True)
logging.debug("... generating dwelling stocks", flush=True)
lookups = lookup_tables.basic_lookups()

if criterias['virtual_building_stock_criteria']:
Expand All @@ -103,15 +102,13 @@ def __init__(
# -------------------------------------------
# Simulate regions
# -------------------------------------------
logging.debug("... generating by dwelling stocks", flush=True)
#_all_closest_weather_stations = []
for reg_array_nr, region in enumerate(tqdm(regions)):

print("... Simulate: region %s, simulation year: %s, percent: (%s)",
region, assumptions.curr_yr, round((100/assumptions.reg_nrs)*reg_array_nr, 2), flush=True)
#logging.info("... Simulate: region %s, simulation year: %s, percent: (%s)",
# region, assumptions.curr_yr, round((100/assumptions.reg_nrs)*reg_array_nr, 2))
logging.debug("... Simulate: region %s, simulation year: %s, percent: (%s)",
region, assumptions.curr_yr, round((100/assumptions.reg_nrs)*reg_array_nr, 2), flush=True)

all_submodels = simulate_region(
region,
data,
Expand All @@ -126,7 +123,6 @@ def __init__(
# Aggregate results specifically over regions
# ---------------------------------------------
aggr_results = aggregate_single_region(
assumptions,
assumptions.reg_nrs,
assumptions.lookup_enduses,
aggr_results,
Expand Down Expand Up @@ -183,20 +179,6 @@ def aggregate_across_all_regs(
Technologies per enduse
"""
fueltypes = lookup_tables.basic_lookups()['fueltypes']
'''# -----------------------------
# Aggregate residential demand [fueltype, region]
# -----------------------------
submodel_nr = 0
array_init = np.zeros((fueltypes_nr, reg_nrs))
for enduse_array_nr in assumptions.lookup_sector_enduses[submodel_nr]:
for enduse in aggr_results['ed_enduse_fueltype_regs_yh'][enduse_array_nr]:
# Sum across all hours in a year
array_init += np.sum(aggr_results['ed_enduse_fueltype_regs_yh'][enduse_array_nr], axis=2)
aggr_results['ed_residential_tot_reg_y'] = array_init'''

# ----------------------------------------------------
# Aggregate: [fueltype, region, fuel_yh_8760]
Expand Down Expand Up @@ -396,7 +378,7 @@ def simulate_region(
for submodel_name in submodel_names:
for sector in data['sectors'][submodel_name]:
for enduse in data['enduses'][submodel_name]:
#print("A - 0")

# ------------------------------------------------------
# Configure and select correct Enduse specific inputs
# ------------------------------------------------------
Expand Down Expand Up @@ -724,7 +706,6 @@ def aggregate_result_unconstrained(
return constrained_array

def aggregate_single_region(
assumptions,
reg_nrs,
lookup_enduses,
aggr_results,
Expand Down Expand Up @@ -782,17 +763,23 @@ def aggregate_single_region(
techs_fueltypes_yh = get_fuels_yh(enduse_object, 'techs_fuel_yh')

if isinstance(techs_fueltypes_yh, dict):

enduse_array_nr = lookup_enduses[enduse_object.enduse]
submodel_nr = submodel_to_idx[enduse_object.submodel_name]

for tech, fuel_tech in techs_fueltypes_yh.items():
tech_fueltype = technologies[tech].fueltype_int

# -------------------------------------------
# Add all technologies to main aggreator (heating & non heating)
# -------------------------------------------
aggr_results['ed_enduse_fueltype_regs_yh'][enduse_array_nr][tech_fueltype][reg_array_nr] += fuel_tech.reshape(8760)

# -----------------------------------------------------------------
# Aggregate fuel of constrained technologies for heating
# -----------------------------------------------------------------
if mode_constrained:

# Only add heating technologies to constrained results
if enduse_object.enduse in enduse_space_heating:

# Aggregate Submodel (sector) specific enduse for fueltype
Expand All @@ -803,8 +790,14 @@ def aggregate_single_region(
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] += fuel_tech.reshape(8760)

# Sum non heating techs
aggr_results['ed_enduse_fueltype_regs_yh'][enduse_array_nr][tech_fueltype][reg_array_nr] += fuel_tech.reshape(8760)
#else: # Add all other non-heating technologies to main aggreator
# aggr_results['ed_enduse_fueltype_regs_yh'][enduse_array_nr][tech_fueltype][reg_array_nr] += fuel_tech.reshape(8760)

#non_heating_ed = results_unconstrained - sum(results_constrained.values())
#assert not testing_functions.test_if_minus_value_in_array(non_heating_ed)
#print("AAA {} {}".format(
# np.sum(aggr_results['ed_enduse_fueltype_regs_yh']),
# np.sum(aggr_results['results_constrained'])))
else:
fueltype_yh_365_24 = get_fuels_yh(enduse_object, 'fuel_yh')
fueltype_yh_8760 = fueltype_yh_365_24.reshape(fueltypes_nr, 8760)
Expand Down Expand Up @@ -875,14 +868,16 @@ def aggregate_from_full_results(
time_resolution,
per_region,
per_sector,
per_enduse,
reg_array_nr=False
per_enduse
):
"""Function to read from full simulation data
specific demands
"""Function to read from full simulation data specific demands
Argument
--------
lookup_enduse : dict
Enduse lookup
aggregated_container : dict
Container to aggregate
full_sim_data : dict
Modelling results per submodel, enduse, region, fueltype, 8760h
time_resolution : str
Expand Down
6 changes: 0 additions & 6 deletions energy_demand/plotting/_scrap.py

This file was deleted.

37 changes: 0 additions & 37 deletions energy_demand/read_write/load_parameter_values.py

This file was deleted.

10 changes: 5 additions & 5 deletions energy_demand/read_write/narrative_related.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Functions handling the narratives
"""
import logging
from collections import defaultdict

from energy_demand.basic import lookup_tables
Expand Down Expand Up @@ -195,8 +196,8 @@ def default_narrative(
'sig_midpoint': sig_midpoint,
'sig_steepness': sig_steepness,
'regional_specific': regional_specific,
'fueltype_replace': 0,
'fueltype_new': 0
'fueltype_replace': fueltype_replace,
'fueltype_new': fueltype_new
}]

def autocomplete(parameter_narratives, simulation_base_yr, sub_param_crit):
Expand All @@ -205,7 +206,7 @@ def autocomplete(parameter_narratives, simulation_base_yr, sub_param_crit):
autocomplet_param_narr = defaultdict(dict)

for sub_param_name, narratives_sector in parameter_narratives.items():
print(" ... " + str(sub_param_name))
logging.debug(" ... " + str(sub_param_name))
for sector, narratives in narratives_sector.items():
autocomplet_param_narr[sub_param_name][sector] = []

Expand Down Expand Up @@ -339,7 +340,6 @@ def read_user_defined_param(
_ = default_streategy_var[enduse]
defined_in_model = True
except KeyError:
#print("... not defined in model")
defined_in_model = False

# All entries of this year df_enduse and this fueltype
Expand Down Expand Up @@ -451,7 +451,7 @@ def read_user_defined_param(
# ------------
# Autocomplete
# ------------
print("... autocomplete")
logging.debug("... autocomplete")
parameter_narratives = autocomplete(
parameter_narratives,
simulation_base_yr,
Expand Down

0 comments on commit 273204f

Please sign in to comment.