Skip to content

feat: add tags to elements for user-defined grouping #121

@FBumann

Description

@FBumann

Summary

Add a tags: list[str] field to the main element dataclasses so users can attach arbitrary labels for custom grouping, filtering, and post-processing — without overloading id or short_id.

Motivation

Users often need to group or filter elements by criteria orthogonal to the energy model (e.g. "renewable", "site_north", "curtailable"). Currently there's no standard place for this metadata. Tags provide a lightweight, flexible mechanism that the library preserves but doesn't interpret.

Design

Tagged elements

Add tags: list[str] = field(default_factory=list) to:

  • Flow
  • Effect
  • Storage
  • Carrier
  • Port
  • Converter

No deduplication — the library doesn't interpret tags, so duplicates are harmless.

No propagation

Tags do not propagate from components to child flows. Each element is tagged independently.

Preservation

Tags must survive through the full pipeline (elements → ModelData → Result) and round-trip through serialization.

Example usage

solar = Flow("elec", tags=["renewable", "curtailable"])
gas = Flow("gas", tags=["fossil"])
grid = Port("grid", exports=[solar, gas], tags=["external"])

# Post-processing:
renewable_flows = [f for f in all_flows if "renewable" in f.tags]

Tasks

  • Add tags field to Flow, Effect, Storage, Carrier, Port, Converter
  • Preserve tags through ModelData and Result
  • Add tests for tag preservation through the full pipeline
  • Document in docstrings

Future enhancements

Intentionally deferred to keep the initial implementation simple:

  • Tag propagation from components to flows — e.g. Port(tags=["x"]) automatically adds "x" to all child flows. Deferred because it adds complexity around lifecycle (what if tags are added after __post_init__?), requires parent references or explicit add_tag/remove_tag methods, and interacts with Storage's charge/discharge renaming.
  • Deduplication — could validate uniqueness on construction. Not needed yet since the library doesn't interpret tags.
  • Key-value tagsdict[str, str] or structured tags. list[str] covers most use cases; users can encode structure as "key:value" strings if needed.

Thoughts

  • How to store tags in xarray Datasets is an open question — non-dimension coordinates are robust but awkward for list data; attrs are simpler but can be silently dropped by xarray operations. Decide during implementation.
  • Tags on Carrier might be less useful than on the other elements — worth including for consistency, but could be dropped if it adds friction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:newNovel concept not yet fitting an existing area (temporary — recategorize when a new area emerges)type:featNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions