Skip to content

Fix OPF time_elapsed calculation - #708

Open
jldonner wants to merge 1 commit into
devfrom
bug/693-OPF_time_elapsed_computed_with_seconds_and_int
Open

Fix OPF time_elapsed calculation#708
jldonner wants to merge 1 commit into
devfrom
bug/693-OPF_time_elapsed_computed_with_seconds_and_int

Conversation

@jldonner

@jldonner jldonner commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

Description

This PR fixes the calculation of time_elapsed in edisgo/io/powermodels_io.py when converting an eDisGo/PyPSA network to the PowerModels format.

Previously, time_elapsed was derived using .seconds and cast to int:

int((psa_net.snapshots[1] - psa_net.snapshots[0]).seconds / 3600)

This caused incorrect values for several valid snapshot resolutions:

15-minute snapshots resulted in time_elapsed = 0 instead of 0.25
24-hour gaps resulted in time_elapsed = 0
25-hour gaps resulted in time_elapsed = 1 instead of 25

Since time_elapsed is used in the Julia OPF formulation for inter-temporal constraints, this could decouple power and energy variables for batteries, heat storage, DSM, and EV charging.

The fix introduces an internal helper function _get_time_elapsed_in_hours(snapshots) which:

  1. uses .total_seconds() instead of .seconds
  2. keeps the result as a float instead of casting it to int
  3. checks that the snapshots are equidistant
  4. raises an error for invalid or non-equidistant snapshot indices

Fixes #693

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • New and adjusted code is formatted using the pre-commit hooks
  • New and adjusted code includes type hinting now
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • The Read the Docs documentation is compiling correctly
  • If new packages are needed, I added them the setup.py, and if needed the rtd_requirements.txt, the eDisGo_env.yml and the eDisGo_env_dev.yml.
  • I have added new features to the corresponding whatsnew file

@joda9

joda9 commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

@jldonner Can you write a test for this function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] OPF time_elapsed computed with .seconds and int() — sub-hourly runs silently decouple storage power from energy

2 participants