Skip to content

Commit

Permalink
modified scaling in api calc
Browse files Browse the repository at this point in the history
  • Loading branch information
markushal authored and wingechr committed Sep 16, 2024
1 parent 0e458fb commit d47d627
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
19 changes: 13 additions & 6 deletions ptxboa/api_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def calculate(data: CalculateDataType) -> pd.DataFrame:

if not is_transport:
flh = step_data["FLH"]
liefetime = step_data["LIFETIME"]
lifetime = step_data["LIFETIME"]
capex_rel = step_data["CAPEX"]
opex_f = step_data["OPEX-F"]

Expand All @@ -72,7 +72,7 @@ def calculate(data: CalculateDataType) -> pd.DataFrame:
capacity = main_output_value / flh

capex = capacity * capex_rel
capex_ann = annuity(wacc, liefetime, capex)
capex_ann = annuity(wacc, lifetime, capex)
opex = opex_f * capacity + opex_o * main_output_value

results.append((result_process_type, process_code, "CAPEX", capex_ann))
Expand All @@ -99,14 +99,14 @@ def calculate(data: CalculateDataType) -> pd.DataFrame:
]

# no FLH
liefetime = sec_process_data["LIFETIME"]
lifetime = sec_process_data["LIFETIME"]
capex = sec_process_data["CAPEX"]
opex_f = sec_process_data["OPEX-F"]
opex_o = sec_process_data["OPEX-O"]

capacity = flow_value # no FLH
capex = capacity * capex
capex_ann = annuity(wacc, liefetime, capex)
capex_ann = annuity(wacc, lifetime, capex)
opex = opex_f * capacity + opex_o * flow_value

results.append(
Expand Down Expand Up @@ -168,9 +168,16 @@ def calculate(data: CalculateDataType) -> pd.DataFrame:
results = results.groupby(dim_columns).sum().reset_index()

# normalization:
# scale so that we star twith 1 EL input,
# scale so that we start with 1 EL input,
# rescale so that we have 1 unit output
norm_factor = sum_el / main_output_value
norm_factor = 1 / main_output_value
results["values"] = results["values"] * norm_factor

# rescale again ONLY RES to account for additionally needed electricity
# sum_el is larger than 1.0
norm_factor_el = sum_el
idx = results["process_type"] == "Electricity generation"
assert idx.any() # must have at least one entry
results.loc[idx, "values"] = results.loc[idx, "values"] * norm_factor_el

return results
31 changes: 20 additions & 11 deletions tests/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,17 @@ def network_green_iron(api) -> Tuple[pypsa.Network, dict, dict]:
return n, metadata, settings


@pytest.mark.xfail
def test_issue_564(network_green_iron, api):
# calculate costs from optimization tab:
n, metadata, settings = network_green_iron
res_opt = calc_aggregate_statistics(n, include_debugging_output=True)
res_costs = api.calculate(**settings)
res_costs_agg = (
res_costs[0]
.pivot_table(
index="process_type", columns="cost_type", values="values", aggfunc=sum
)
.fillna(0)
)

# get costs from costs tab:
df_res_costs, _ = api.calculate(**settings)

res_costs_agg = df_res_costs.pivot_table(
index="process_type", columns="cost_type", values="values", aggfunc=sum
).fillna(0)

res_costs_agg["total"] = res_costs_agg.sum(axis=1)
res_costs_agg.loc["Total"] = res_costs_agg.sum(axis=0)
Expand Down Expand Up @@ -282,14 +281,19 @@ def test_issue_564(network_green_iron, api):
res_costs_agg["total_opt"] - res_costs_agg["total"]
).fillna(0)

# call optimize function directly:
res_optimize = optimize(metadata["opt_input_data"])[0]

# write costs data to excel, and metadata to json:
if not os.path.exists("tests/out"):
os.makedirs("tests/out")
res_costs_agg.to_excel("tests/out/test_issue_564.xlsx")
with open("tests/out/issue_564_metadata.json", "w") as f:
with open("tests/out/issue_564_metadata_optimize_input.json", "w") as f:
dump(metadata, f)
with open("tests/out/issue_564_metadata_optimize_output.json", "w") as f:
dump(res_optimize, f)

# extract input data:
# extract DRI input data:
input_data = api.get_input_data(scenario=settings["scenario"])
input_data_dri = input_data.loc[
input_data["process_code"] == "Green iron reduction"
Expand All @@ -309,6 +313,11 @@ def test_issue_564(network_green_iron, api):
# annuized capex should match:
assert capex_ann_input + opex_fix == pytest.approx(capex_ann_opt)

# FLH from optimization tab and optimize function output should match:
flh_opt_tab = res_opt.at["Derivative production", "Full load hours (h)"]
flh_opt_function = res_optimize["DERIV"]["FLH"] * 8760
assert flh_opt_tab == pytest.approx(flh_opt_function)

# assert that differences between costs and opt tab are zero:
# this currently fails
for i in res_costs_agg["diff"]:
Expand Down

0 comments on commit d47d627

Please sign in to comment.