Skip to content

Commit

Permalink
Updated aggregation and fixed pasted out testin for negative values i…
Browse files Browse the repository at this point in the history
…n enduse function for speed up
  • Loading branch information
Sven Eggimann committed Mar 5, 2019
1 parent 273204f commit 3783971
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 38 deletions.
12 changes: 6 additions & 6 deletions energy_demand/basic/testing_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def test_if_minus_value_in_array(arraytotest):#, tolerance_min_max=0.00000000001
only_neg_elements = arraytotest[arraytotest < 0]

if len(only_neg_elements) > 0:
for element in only_neg_elements:
#for element in only_neg_elements:
#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)))
#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)))
return True
else:
return False
Expand Down
30 changes: 16 additions & 14 deletions energy_demand/enduse_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,13 @@ def __init__(
self.flat_profile_crit = flat_profile_crit
self.techs_fuel_yh = None

#If enduse has no fuel return empty shapes
if np.sum(fuel) == 0:
if np.sum(fuel) == 0: #If enduse has no fuel return empty shapes
self.flat_profile_crit = True
self.fuel_y = fuel
self.fuel_yh = 0
self.enduse_techs = []
else:
#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 @@ -222,8 +220,7 @@ def __init__(
mode_constrained=False,
param_lf_improved_cy=strategy_vars['dm_improvement'][enduse][curr_yr],
make_all_flat=make_all_flat)
else:
#If technologies are defined for an enduse
else: #If technologies are defined for an enduse
mode_constrained = get_enduse_configuration(
criterias['mode_constrained'],
enduse,
Expand Down Expand Up @@ -291,7 +288,11 @@ def __init__(
'''for tech, fuel_tech in fuel_tech_y.items():
if np.sum(fuel_tech) == 0:
self.enduse_techs.remove(tech)'''


#for key, i in fuel_tech_y.items():
#print(" t: {}".format(key))
# assert not testing_functions.test_if_minus_value_in_array(i)
#assert not testing_functions.test_if_minus_value_in_array(self.fuel_y)
# ------------------------------------------
# Assign load profiles
# ------------------------------------------
Expand Down Expand Up @@ -325,6 +326,11 @@ def __init__(
assumptions.technologies,
fuel_yh,
param_lf_improved_cy=strategy_vars['dm_improvement'][enduse][curr_yr])

#assert not testing_functions.test_if_minus_value_in_array(self.fuel_y)
#for key, i in self.techs_fuel_yh.items():
#print(" t: {}".format(key))
# assert not testing_functions.test_if_minus_value_in_array(i)
else:
self.fuel_yh = load_shifting(
enduse,
Expand All @@ -333,8 +339,8 @@ def __init__(
param_lf_improved_cy=strategy_vars['dm_improvement'][enduse][curr_yr],
make_all_flat=make_all_flat)

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)))
#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)))

def load_shifting_multiple_tech(
enduse,
Expand Down Expand Up @@ -880,18 +886,14 @@ def service_to_fuel(

if mode_constrained:
for tech, service in service_tech.items():

tech_eff = tech_stock.get_tech_attr(
enduse, sector, tech, 'eff_cy')
fueltype_int = tech_stock.get_tech_attr(
enduse, sector, tech, 'fueltype_int')

# Convert to fuel
fuel_tech = service / tech_eff

# Add fuel
fuel_tech_y[tech] = fuel_tech
fuel_tech = service / tech_eff # Convert to fuel

fuel_tech_y[tech] = fuel_tech # Add fuel
fuel_y[fueltype_int] += fuel_tech
else:
for tech, fuel_tech in service_tech.items():
Expand Down
2 changes: 1 addition & 1 deletion energy_demand/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def energy_demand_model(
# Log model results
write_data.logg_info(modelrun, fuels_in, data)

return modelrun
return modelrun

if __name__ == "__main__":
"""
Expand Down
29 changes: 12 additions & 17 deletions energy_demand/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,47 +761,42 @@ def aggregate_single_region(
# Aggregate fuel of all technologies
# -----------------------------------------------------------------
techs_fueltypes_yh = get_fuels_yh(enduse_object, 'techs_fuel_yh')
enduse_array_nr = lookup_enduses[enduse_object.enduse]
submodel_nr = submodel_to_idx[enduse_object.submodel_name]

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

reshaped_fuel = fuel_tech.reshape(8760)
# -------------------------------------------
# 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)

aggr_results['ed_enduse_fueltype_regs_yh'][enduse_array_nr][tech_fueltype][reg_array_nr] += reshaped_fuel

# -----------------------------------------------------------------
# Aggregate fuel of constrained technologies for heating
# Only add heating technologies to constrained results
# -----------------------------------------------------------------
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
if tech in aggr_results['results_constrained'].keys():
aggr_results['results_constrained'][tech][submodel_nr][reg_array_nr][tech_fueltype] += fuel_tech.reshape(8760)
aggr_results['results_constrained'][tech][submodel_nr][reg_array_nr][tech_fueltype] += reshaped_fuel
else:
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] += 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'])))
aggr_results['results_constrained'][tech][submodel_nr][reg_array_nr][tech_fueltype] += reshaped_fuel
else:
# Only add heating technologies to constrained results
fueltype_yh_365_24 = get_fuels_yh(enduse_object, 'fuel_yh')
fueltype_yh_8760 = fueltype_yh_365_24.reshape(fueltypes_nr, 8760)

if enduse_object.enduse in enduse_space_heating:
if np.sum(fueltype_yh_8760) != 0:
raise Exception("WHAT IST THIS?? {} {}".format(enduse_object.enduse, fueltype_yh_8760.shape))
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

Expand Down

0 comments on commit 3783971

Please sign in to comment.