Skip to content

Comments

Fix supply function#585

Merged
tsmbland merged 24 commits intomainfrom
supply_func2
Feb 3, 2025
Merged

Fix supply function#585
tsmbland merged 24 commits intomainfrom
supply_func2

Conversation

@tsmbland
Copy link
Collaborator

@tsmbland tsmbland commented Nov 28, 2024

To be reviewed after #647

The main changes in the PR are a rewrite of the supply and emission functions, along with some changes to broadcast_techs

supply

  • I've tried to keep this function true to how it was originally written, so most results are unaffected (except multi-region models, see next point), however I've tidied it up to make it more readable
  • There was a bug when using multi-region models. demand has a "region" dimension, so the line expanded_demand = (demand * maxprod / maxprod.sum(demsum)).fillna(0) created an array with separate "asset" and "region" dimensions, which essentially duplicated each asset over every region (this is why regression tests are failing in Xarray patch: Check array dimensions #647). This led to the problem described in [BUG] Unexpected behaviour in multi-region models #475, and also inflated commodity prices in multi-region models (see Prevent arrays from having separate "asset" and "region" dimensions #619). I've fixed this by calling broadcast_techs on the demand data so that each asset only sees the demand for its respective region
  • Added some tests for single and multi-region models
  • Trade models get treated a bit differently. I've done my best attempt at coding what I think should happen in trade models, however trade models have never worked to begin with, it's unclear how they are supposed to work, and it's not worth my time right now to dig too deep into this. So I've added a note that it may be incorrect, possibly to return to in the future.

emission

  • Emissions should be scaled according to production amplitude (as calculated by production_amplitude), rather than the sum of end-use quantities as was being done before. This is especially relevant for technologies with multiple end-use commodities, or output ratios not equal to 1.
  • Added a test

broadcast_techs

  • Since this function is no longer used exclusively on technology data, I've added a note about this in the docstring.
  • I've added some detail to the docstring about the installed_as_year parameter, as this is actually very important and care needs to be taken depending on the type of data that the function is called on.
  • I've also removed the interpolation feature, as this shouldn't be necessary if the technology data covers the appropriate years found in the assets template. (In general I think interpolation is overused, and should rarely be required as there's a well defined time framework which all datasets should follow).
  • Also removed filtering with kwargs as this wasn't being used anywhere
  • Rewritten the test

Results

  • Fixes the issue in [BUG] Unexpected behaviour in multi-region models #475. Results now look much more reasonable in multi-region models:
    Screenshot 2025-01-30 152917
  • Commodity prices in multi-region models are now more in line with how they are in single region models
  • For some reason the Power_Supply.csv files now have data for 2020 which was missing before - not sure why, but I'll take it

Close #475

@tsmbland tsmbland deleted the branch main December 3, 2024 12:41
@tsmbland tsmbland closed this Dec 3, 2024
@tsmbland tsmbland reopened this Dec 3, 2024
Base automatically changed from v1.3 to main December 3, 2024 12:46
@tsmbland tsmbland changed the base branch from main to check_arrays January 29, 2025 14:48
@tsmbland tsmbland marked this pull request as ready for review January 30, 2025 10:43
@tsmbland tsmbland requested a review from dalonsoa January 30, 2025 15:50
@tsmbland tsmbland linked an issue Jan 30, 2025 that may be closed by this pull request
Copy link
Collaborator

@dalonsoa dalonsoa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks OK. The code is cleaner and with better explanations. Having said that, I've a few questions/doubts.

result = result.where(result >= expanded_minprod, expanded_minprod)

# add production of environmental pollutants
# Multi-region models
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I got a bit confused here. What's the point of multi-region simulations if there's no trade between them? You could equally have individual simulations for each of the regions as they are, in practice, independent from each other, right?

For some reason, I had always implicitly assumed that if you have multiple regions, you're going to send stuff from one region to another. I never consider that that was not required.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I agree, it's pointless without trade

However, trade requires things to be set up in a particular way, with a TradeTechnodata.csv file, and it's still possible to run a multi-region model without this file, which we need to account for (at least for now).

The multi-region model in the tutorials doesn't have any trade, and is just used to demonstrate how regions with different technology parameters can differ - although obviously you could do the same with two single-region models!

This function broadcast the first representation to the shape and coordinates
of the second.

Note: this is not necessarily limited to `technology` datasets. For
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this function is not limited to technology datasets, it should be renamed to something different as well as well as use variable names that do not indicate the type of data. Maybe broadcast_data for the function name, and just data or dataset instead of technologies. The docstring and comments will need to be updated accordingly.

applied to the `year` dimension of the technologies dataset
kwargs: further arguments are used initial filters over the
`technologies` dataset.
installed_as_year: True means that the "year" dimension in the technologies
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth to add an explanation of the use case for this to be False, as well.

Comment on lines +264 to +269
# If installed_as_year is True, we need to rename the installed dimension to "year"
# TODO: this should be stricter, and enforce that the template has "installed" data,
# and that the technologies dataset has a "year" dimension.
# if installed_as_year:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there seem to be more things to do, maybe implement the changes I suggest above as a separate PR, alongside this TODO.

Base automatically changed from check_arrays to main February 3, 2025 16:02
@tsmbland tsmbland merged commit 0429239 into main Feb 3, 2025
11 of 14 checks passed
@tsmbland tsmbland deleted the supply_func2 branch February 3, 2025 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] Unexpected behaviour in multi-region models

2 participants