calliope-project / calliope

A multi-scale energy systems modelling framework
https://www.callio.pe
Apache License 2.0
287 stars 93 forks source link

cost_var duplicate index error with multiple carriers #663

Closed jmorrisnrel closed 1 week ago

jmorrisnrel commented 4 weeks ago

What happened?

I've been testing some of the multi-carrier functionality with conversion and supply and have run into an issue trying to set an export cost on one carrier while putting a production cost on the other. Setting multiple variable cost parameters (cost_flow_in, cost_flow_out or cost_export) for different carriers on a single tech/node seems to trigger multiple equations being added for cost_var on the same index. This occurs on both supply and conversion (and I imagine on other archetypes as well).

CHP:
  base_tech: conversion
  carrier_in: Fuel
  carrier_out: [Power,Heat]
  carrier_export: Heat
  cost_flow_cap:
    data: 20
    dims: [costs, carriers]
    index: [[monetary,Power]]
  cost_flow_out:
    data: 0.05
    dims: [costs,carriers]
    index: [[monetary,Power]]
  cost_export:
    data: -0.03
    dims: [costs,carriers]
    index: [[monetary,Heat]]

Setting multiple instances of a single cost parameter causes the issue as well:

CHP:
  base_tech: conversion
  carrier_in: Fuel
  carrier_out: [Power,Heat]
  carrier_export: Power
  cost_flow_cap:
    data: [20]
    dims: [costs, carriers]
    index: [[monetary,Power]]
  cost_flow_out:
    data: [0.05,-0.01]
    dims: [costs,carriers]
    index: [[monetary,Power],[monetary,Heat]]
  cost_export:
    data: -0.03
    dims: [costs,carriers]
    index: [[monetary,Power]]

The issue occurs if you specify the constraints with a CSV as well.

cost_flow_cap does not seem to be affected and builds correctly, although I haven't tested all combinations of carriers and cost parameters so it may not just be the cost_var ones.

Which operating systems have you used?

Version

v0.7.0-dev3

Relevant log output

Traceback (most recent call last):
  File "c:\Users\jmorris4\Documents\model-profiling\test-model.py", line 59, in <module>
    model.run()
  File "C:\Users\jmorris4\.conda\envs\calliope-07-dev\Lib\site-packages\calliope\model.py", line 494, in run
    self.build(force=force_rerun)
  File "C:\Users\jmorris4\.conda\envs\calliope-07-dev\Lib\site-packages\calliope\model.py", line 391, in build
    backend._build()
  File "C:\Users\jmorris4\.conda\envs\calliope-07-dev\Lib\site-packages\calliope\backend\backend_model.py", line 225, in _build
    getattr(self, f"add_{component}")(name)
  File "C:\Users\jmorris4\.conda\envs\calliope-07-dev\Lib\site-packages\calliope\backend\pyomo_backend_model.py", line 150, in add_global_expression
    self._add_component(
  File "C:\Users\jmorris4\.conda\envs\calliope-07-dev\Lib\site-packages\calliope\backend\backend_model.py", line 340, in _add_component
    raise BackendError(f"{element.name} | {substring}")
calliope.exceptions.BackendError: global_expressions:cost_var:0-cost_export:1-cost_flow_in:1-cost_flow_out:0 | trying to set two equations for the same index:
MultiIndex([('region_1', 'CCGT', 'monetary', '2024-01-01 00:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 01:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 02:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 03:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 04:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 05:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 06:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 07:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 08:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 09:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 10:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 11:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 12:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 13:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 14:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 15:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 16:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 17:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 18:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 19:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 20:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 21:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 22:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-01 23:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 00:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 01:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 02:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 03:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 04:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 05:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 06:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 07:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 08:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 09:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 10:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 11:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 12:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 13:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 14:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 15:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 16:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 17:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 18:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 19:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 20:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 21:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 22:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-02 23:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 00:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 01:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 02:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 03:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 04:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 05:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 06:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 07:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 08:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 09:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 10:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 11:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 12:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 13:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 14:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 15:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 16:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 17:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 18:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 19:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 20:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 21:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 22:00:00'),
            ('region_1', 'CCGT', 'monetary', '2024-01-03 23:00:00')],
           names=['nodes', 'techs', 'costs', 'timesteps'])
brynpickering commented 3 weeks ago

Thanks @jmorrisnrel . I can confirm that I am able to reproduce this locally with the following applied as an override to the urban scale example model:

techs:
  chp:
    cost_flow_out:
      data: [0.004, 0.001]
      index: [[monetary, electricity], [monetary, heat]]
      dims: [costs, carriers]
    cost_flow_export:
      data: 1
      index: [[monetary, electricity]]
      dims: [costs, carriers]

I have a fix I'll open a pull request for today.