Skip to content

[linopy v1] Coordinate mismatch on shared dim in adjacent-step constraints #730

Description

@FBumann

Category: coordinate alignment (most frequent — 11 code sites, ~150 test failures under v1)

Symptom

Under linopy.options["semantics"] = "v1", building any model with a storage or on/off component raises:

ValueError: Coordinate mismatch on shared dimension 'time':
left=[...t1..tN], right=[...t0..t(N-1)].
Resolve with `.sel(...)` / `.reindex(...)` ... `.assign_coords(...)` ...

(origin: linopy/expressions.py merge; under legacy the same site emits a LinopySemanticsWarning: "Coordinate mismatch in merge along dim '_term' silently aligned by legacy".)

Location (leaf sites)

  • flixopt/components.py _build_energy_balance_lhs — storage SOC balance
  • flixopt/modeling.py consecutive_duration_tracking — forward / backward / lb
  • flixopt/modeling.py state_transition_bounds, continuous_transition_bounds, level tracking
  • flixopt/features.py InvestmentModel._create_variables_and_constraints — linked periods (dim period)

Root cause

These constraints relate x[t+1] to x[t] by slicing the same variable two ways — x.isel(dim=slice(1, None)) and x.isel(dim=slice(None, -1)) — which carry different coordinate labels on the shared dim (labels[1:] vs labels[:-1]). Legacy realigned by position silently; v1 aligns strictly by label and raises.

Repro

Any tests/test_math/test_storage.py or on/off test under v1, e.g. test_storage_losses. Minimal:

import linopy, pandas as pd
linopy.options["semantics"] = "v1"
m = linopy.Model(); t = pd.date_range("2020", periods=4, freq="h", name="time")
x = m.add_variables(coords=[t], name="x")
x.isel(time=slice(1, None)) - x.isel(time=slice(None, -1))   # raises

Proposed fix

modeling._lead(var, dim) = var.isel({dim: slice(1, None)}).assign_coords({dim: var.indexes[dim][:-1]}) — relabels the leading slice onto the trailing slice's labels. Positional pairing preserved; identical under legacy (positional == label once relabelled). This is the guide-prescribed .assign_coords fix. .shift() is unsuitable (its boundary row diverges legacy↔v1).

Severity

Build-breaking under v1 (raises); warning-only under legacy. No numeric change once fixed.

Fixed in #729.

Metadata

Metadata

Assignees

No one assigned

    Labels

    linopy-v1Compatibility with linopy v1 semantics (PR #717)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions