From 90d62a61fb87620cae4866dd2973c6afa49befc8 Mon Sep 17 00:00:00 2001 From: Tom Bland Date: Mon, 2 Dec 2024 11:23:46 +0000 Subject: [PATCH 1/3] Switch demand fulfillment check to annual --- src/muse/mca.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/muse/mca.py b/src/muse/mca.py index 3f2fcea2c..2bc43da1d 100644 --- a/src/muse/mca.py +++ b/src/muse/mca.py @@ -499,7 +499,9 @@ def check_demand_fulfillment(market: Dataset, tol: float) -> bool: from logging import getLogger future = market.year[-1].item() - delta = (market.supply - market.consumption).sel(year=future) + delta = (market.supply.sum("timeslice") - market.consumption.sum("timeslice")).sel( + year=future + ) unmet = (delta < tol).any([u for u in delta.dims if u != "commodity"]) if unmet.any(): From 11ac0c501a760b198d74ac95e0c0e04e98d224f3 Mon Sep 17 00:00:00 2001 From: Tom Bland Date: Mon, 2 Dec 2024 11:43:58 +0000 Subject: [PATCH 2/3] More informative error message --- src/muse/mca.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/muse/mca.py b/src/muse/mca.py index 2bc43da1d..e95206566 100644 --- a/src/muse/mca.py +++ b/src/muse/mca.py @@ -485,7 +485,7 @@ def find_equilibrium( def check_demand_fulfillment(market: Dataset, tol: float) -> bool: - """Checks if the supply will fulfill all the demand in the future. + """Checks if the supply will fulfill across the full year. If it does not, it logs a warning. @@ -499,21 +499,28 @@ def check_demand_fulfillment(market: Dataset, tol: float) -> bool: from logging import getLogger future = market.year[-1].item() - delta = (market.supply.sum("timeslice") - market.consumption.sum("timeslice")).sel( - year=future - ) + supply = market.supply.sum("timeslice").sel(year=future) + consumption = market.consumption.sum("timeslice").sel(year=future) + delta = supply - consumption unmet = (delta < tol).any([u for u in delta.dims if u != "commodity"]) if unmet.any(): - commodities = ", ".join(unmet.commodity.sel(commodity=unmet.values).values) + unmet_commodities = unmet.commodity.sel(commodity=unmet.values).values + unmet_details = [] + for commodity in unmet_commodities: + unmet_details.append( + f"{commodity} " + f"(consumption={consumption.sel(commodity=commodity).item():.2f}, " + f"supply={supply.sel(commodity=commodity).item():.2f})" + ) + + commodities_details = ", ".join(unmet_details) msg = ( f"Consumption exceeds supply in the year {future} for the following " - f"commodities: {commodities} " + f"commodities: {commodities_details}" ) getLogger(__name__).warning(msg) - return False - return True From eaddda42406837d7a839011cef70a99ab0487cae Mon Sep 17 00:00:00 2001 From: Tom Bland Date: Mon, 2 Dec 2024 11:54:13 +0000 Subject: [PATCH 3/3] Revert "More informative error message" This reverts commit 11ac0c501a760b198d74ac95e0c0e04e98d224f3. --- src/muse/mca.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/muse/mca.py b/src/muse/mca.py index e95206566..2bc43da1d 100644 --- a/src/muse/mca.py +++ b/src/muse/mca.py @@ -485,7 +485,7 @@ def find_equilibrium( def check_demand_fulfillment(market: Dataset, tol: float) -> bool: - """Checks if the supply will fulfill across the full year. + """Checks if the supply will fulfill all the demand in the future. If it does not, it logs a warning. @@ -499,28 +499,21 @@ def check_demand_fulfillment(market: Dataset, tol: float) -> bool: from logging import getLogger future = market.year[-1].item() - supply = market.supply.sum("timeslice").sel(year=future) - consumption = market.consumption.sum("timeslice").sel(year=future) - delta = supply - consumption + delta = (market.supply.sum("timeslice") - market.consumption.sum("timeslice")).sel( + year=future + ) unmet = (delta < tol).any([u for u in delta.dims if u != "commodity"]) if unmet.any(): - unmet_commodities = unmet.commodity.sel(commodity=unmet.values).values - unmet_details = [] - for commodity in unmet_commodities: - unmet_details.append( - f"{commodity} " - f"(consumption={consumption.sel(commodity=commodity).item():.2f}, " - f"supply={supply.sel(commodity=commodity).item():.2f})" - ) - - commodities_details = ", ".join(unmet_details) + commodities = ", ".join(unmet.commodity.sel(commodity=unmet.values).values) msg = ( f"Consumption exceeds supply in the year {future} for the following " - f"commodities: {commodities_details}" + f"commodities: {commodities} " ) getLogger(__name__).warning(msg) + return False + return True