Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Down sampling #33

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions ModData.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
from hypatia import Model,Plotter, Sensitivity

test = Model(
path = 'test/yeees_modified/decentral_onshore_market/sets',
path = 'test/yeees_modified/ammonia_import/sets',
mode = 'Planning', period_step = 5)

#%%
import pandas as pd
path = "test/yeees_modified/decentral_onshore_market/parameters"
path2 = "test/yeees_modified/decentral_onshore_market_snapshot/parameters"
path = "test/yeees_modified/ammonia_import/parameters"
path2 = "test/yeees_modified/ammonia_import_snapshot_AD/parameters"

#%%

Expand Down Expand Up @@ -538,7 +539,7 @@

## Writing the parameter_connection file
with pd.ExcelWriter(
r"{}/parameters_connections_.xlsx".format(path2)
r"{}/parameters_connections.xlsx".format(path2)
) as writer:

for key, value in connection_data_new.items():
Expand Down Expand Up @@ -568,7 +569,7 @@
## Writing the parameter_global file

with pd.ExcelWriter(
r"{}/parameters_global_.xlsx".format(path2)
r"{}/parameters_global.xlsx".format(path2)
) as writer:

for key, value in global_data_new.items():
Expand Down
6 changes: 3 additions & 3 deletions hypatia/analysis/postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@
import os


def dict_to_csv(Dict, path):
def dict_to_csv(Dict, path,sep):
"""Writes nested dicts to csv"""

for key, value in Dict.items():
if isinstance(value, pd.DataFrame):
value.to_csv(f"{path}//{key}.csv")
value.to_csv(f"{path}//{key}.csv",sep=sep)
else:
new_path = f"{path}//{key}"
os.makedirs(new_path, exist_ok=True)
dict_to_csv(value, new_path)
dict_to_csv(value, new_path,sep)


def year_slice_index(
Expand Down
197 changes: 113 additions & 84 deletions hypatia/backend/Build.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __init__(self, sets):
self._constr_resource_tech_availability()
self._constr_tech_efficiency()
# self._constr_prod_annual()
self._constr_prod()
#self._constr_prod()
#self._constr_emission_cap()
self._calc_variable_storage_SOC()
self._constr_storage_max_min_charge()
Expand Down Expand Up @@ -486,19 +486,6 @@ def _calc_variable_planning(self):
self.sets.data[reg]["inv_taxsub"]["Sub"][key],
)

salvage_inv_regional[key] = cp.multiply(
salvage_factor(
self.sets.main_years,
self.sets.Technologies[reg][key],
self.sets.data[reg]["tech_lifetime"].loc[:, key],
self.sets.data[reg]["interest_rate"].loc[:, key],
self.sets.data[reg]["discount_rate"],
self.sets.data[reg]["economic_lifetime"].loc[:, key],
self.sets.period_step
),
cost_inv_regional[key],
)

accumulated_newcapacity_regional[key] = newcap_accumulated(
self.variables["newcapacity"][reg][key],
self.sets.Technologies[reg][key],
Expand All @@ -523,18 +510,7 @@ def _calc_variable_planning(self):
self.sets.data[reg]["fix_taxsub"]["Sub"][key],
)

decomcapacity_regional[key] = decomcap(
self.variables["newcapacity"][reg][key],
self.sets.Technologies[reg][key],
self.sets.main_years,
self.sets.data[reg]["tech_lifetime"].loc[:, key],
self.sets.period_step,
)

cost_decom_regional[key] = cp.multiply(
self.sets.data[reg]["tech_decom_cost"].loc[:, key].values,
decomcapacity_regional[key],
)

production_annual_regional[key] = annual_activity(
self.variables["productionbyTechnology"][reg][key],
Expand Down Expand Up @@ -568,16 +544,45 @@ def _calc_variable_planning(self):
CO2_equivalent_regional[key],
self.sets.data[reg]["carbon_tax"].loc[:, key],
)

if not self.sets.snapshot:

salvage_inv_regional[key] = cp.multiply(
salvage_factor(
self.sets.main_years,
self.sets.Technologies[reg][key],
self.sets.data[reg]["tech_lifetime"].loc[:, key],
self.sets.data[reg]["interest_rate"].loc[:, key],
self.sets.data[reg]["discount_rate"],
self.sets.data[reg]["economic_lifetime"].loc[:, key],
self.sets.period_step
),
cost_inv_regional[key],
)

decomcapacity_regional[key] = decomcap(
self.variables["newcapacity"][reg][key],
self.sets.Technologies[reg][key],
self.sets.main_years,
self.sets.data[reg]["tech_lifetime"].loc[:, key],
self.sets.period_step,
)

cost_decom_regional[key] = cp.multiply(
self.sets.data[reg]["tech_decom_cost"].loc[:, key].values,
decomcapacity_regional[key],
)

cost_fvalue_regional[key] = invcosts_annuity(
cost_inv_regional[key],
self.sets.data[reg]["interest_rate"].loc[:, key],
self.sets.data[reg]["economic_lifetime"].loc[:, key],
self.sets.Technologies[reg][key],
self.sets.main_years,
self.sets.data[reg]["discount_rate"],
self.sets.period_step
)

cost_fvalue_regional[key] = invcosts_annuity(
cost_inv_regional[key],
self.sets.data[reg]["interest_rate"].loc[:, key],
self.sets.data[reg]["economic_lifetime"].loc[:, key],
self.sets.Technologies[reg][key],
self.sets.main_years,
self.sets.data[reg]["discount_rate"],
self.sets.period_step
)

self.cost_inv[reg] = cost_inv_regional
self.cost_inv_tax[reg] = cost_inv_tax_regional
Expand Down Expand Up @@ -646,20 +651,7 @@ def _calc_variable_planning_line(self):
+ self.sets.trade_data["line_residual_cap"].loc[:, line].values
)

self.line_decommissioned_capacity[line] = line_decomcap(
self.line_newcapacity[line],
self.sets.trade_line[line],
self.sets.main_years,
self.sets.trade_data["line_lifetime"].loc[:, line],
self.sets.period_step,
)

self.cost_decom_line[line] = cp.multiply(cp.multiply(
self.sets.trade_data["line_decom_cost"].loc[:, line].values,
self.line_decommissioned_capacity[line],
), self.sets.trade_data["line_length"].loc[:,line].values)



self.cost_inv_line[line] = cp.multiply(cp.multiply(self.sets.trade_data["line_inv_{}".format(self.sets.sizes[0])].loc[:,line].values ,
self.variables["line_newcapacity_lump"][self.sets.sizes[0]][line]),
Expand All @@ -681,19 +673,33 @@ def _calc_variable_planning_line(self):
self.variables["line_newcapacity_lump"][size][line]),
self.sets.trade_data["line_length"].loc[:,line].values)

self.salvage_inv_line[line] = cp.multiply(
salvage_factor_line(

if not self.sets.snapshot:
self.line_decommissioned_capacity[line] = line_decomcap(
self.line_newcapacity[line],
self.sets.trade_line[line],
self.sets.main_years,
carr_list,
self.sets.trade_data["line_lifetime"].loc[:, line],
self.sets.trade_data["interest_rate"].loc[:, line],
self.sets.global_data["global_discount_rate"],
self.sets.trade_data["line_economic_lifetime"].loc[:, line],
self.sets.period_step
),
self.cost_inv_line[line],
self.sets.period_step,
)


self.cost_decom_line[line] = cp.multiply(cp.multiply(
self.sets.trade_data["line_decom_cost"].loc[:, line].values,
self.line_decommissioned_capacity[line],
), self.sets.trade_data["line_length"].loc[:,line].values)

self.salvage_inv_line[line] = cp.multiply(
salvage_factor_line(
self.sets.main_years,
carr_list,
self.sets.trade_data["line_lifetime"].loc[:, line],
self.sets.trade_data["interest_rate"].loc[:, line],
self.sets.global_data["global_discount_rate"],
self.sets.trade_data["line_economic_lifetime"].loc[:, line],
self.sets.period_step
),
self.cost_inv_line[line],
)

self.cost_variable_line = line_varcost(
self.sets.trade_data["line_var_cost"],
Expand Down Expand Up @@ -1527,7 +1533,7 @@ def _constr_storage_max_flow_in_out(self):
self.sets.data[reg]["tech_capacity_factor"]["Storage"].values[
indx : indx + 1, :
],
self.timeslice_fraction,
self.timeslice_fraction
)

max_storage_flow_out = storage_max_flow(
Expand All @@ -1536,7 +1542,7 @@ def _constr_storage_max_flow_in_out(self):
self.sets.data[reg]["tech_capacity_factor"]["Storage"].values[
indx : indx + 1, :
],
self.timeslice_fraction,
self.timeslice_fraction
)

self.constr.append(
Expand Down Expand Up @@ -1605,24 +1611,38 @@ def _set_regional_objective_planning(self):
# axis=1,
# )

totalcost_regional += cp.sum(
self.cost_inv_tax[reg][ctgry]
- self.cost_inv_sub[reg][ctgry]
+ self.cost_fix[reg][ctgry]
+ self.cost_fix_tax[reg][ctgry]
- self.cost_fix_sub[reg][ctgry]
+ self.cost_variable_annual[reg][ctgry]
+ self.cost_decom[reg][ctgry]
- self.salvage_inv[reg][ctgry],
axis=1,
)

if not self.sets.snapshot:

totalcost_regional += cp.sum(
self.cost_inv_tax[reg][ctgry]
- self.cost_inv_sub[reg][ctgry]
+ self.cost_fix[reg][ctgry]
+ self.cost_fix_tax[reg][ctgry]
- self.cost_fix_sub[reg][ctgry]
+ self.cost_variable_annual[reg][ctgry]
+ self.cost_decom[reg][ctgry]
- self.salvage_inv[reg][ctgry],
axis=1,
)


self.inv_allregions += self.cost_inv_fvalue[reg][ctgry]


else:

totalcost_regional += cp.sum(
self.cost_inv_tax[reg][ctgry]
- self.cost_inv_sub[reg][ctgry]
+ self.cost_fix[reg][ctgry]
+ self.cost_fix_tax[reg][ctgry]
- self.cost_fix_sub[reg][ctgry]
+ self.cost_variable_annual[reg][ctgry],

axis=1,
)

#total_salvage_regional += cp.sum(self.salvage_inv[reg][ctgry], axis =1)



self.inv_allregions += self.cost_inv_fvalue[reg][ctgry]

if ctgry != "Transmission" and ctgry != "Storage":

Expand Down Expand Up @@ -1688,16 +1708,25 @@ def _set_lines_objective_planning(self):
#self.total_salvage_line = np.zeros((len(self.sets.main_years), 1))

for line in self.sets.trade_line.keys():

self.totalcost_lines += cp.sum(
self.cost_inv_line[line]
+ self.cost_fix_line[line]
+ self.cost_decom_line[line]
- self.salvage_inv_line[line],
axis=1,
)


if not self.sets.snapshot:

self.totalcost_lines += cp.sum(
self.cost_inv_line[line]
+ self.cost_fix_line[line]
+ self.cost_decom_line[line]
- self.salvage_inv_line[line],
axis=1,
)

else:

self.totalcost_lines += cp.sum(
self.cost_inv_line[line]
+ self.cost_fix_line[line],
axis=1,
)

#self.total_salvage_line += cp.sum(self.salvage_inv_line[line],axis=1)

for reg_ in self.sets.trade_regions.keys():
Expand Down
Loading