# title: Flow share per timestep
#
# description: |
#     Set the per-timestep share of a technology's (or group thereof) out/inflow met by other technologies to specific values.
#     If a single technology, it needs to be explicitly defined (e.g., `flow_in[techs=demand_power]`).
#     If a group of technologies, they can be explicitly defined as a list or can be consolidated by a shared attribute (e.g., they produce a specific carrier: `flow_out[carriers=power]`)
#     The parameters (demand_share_per_timestep_equals, supply_share_per_timestep_equals) can be defined per technology (and optionally per node) as a single value or as a time varying value (e.g., `techs.my_tech.constraints.demand_share_per_timestep_equals: file=my_file.csv`)
#
#     New technology-level parameters:
#
#     - `demand_share_per_timestep_equals`
#     - `supply_share_per_timestep_equals`
#
#     Helper functions used:
#
#     - `sum` (expression)
#
# ---

constraints:
  demand_share_per_timestep_equals_per_tech:
    description: >
      Set the per-timestep outflow of certain technologies which produce
      the `power` carrier to a share of demand inflow.
    foreach: [nodes, techs, timesteps]
    where: demand_share_per_timestep_equals
    equations:
      - expression: >
          sum(flow_out, over=carriers) ==
          sum(flow_in[techs=$demand_tech], over=carriers)
          * demand_share_per_timestep_equals
    slices:
      demand_tech:
        - expression: demand_share_tech  # assigned as an indexed parameter

  supply_share_per_timestep_equals_per_tech:
    description: >
      Set the per-timestep outflow of certain technologies which produce
      a particular carrier to a share of per-timestep outflow of
      that carrier in each node.
    foreach: [nodes, techs, timesteps]
    where: supply_share_per_timestep_equals
    equations:
      - expression: >
          flow_out[carriers=$carrier] ==
          sum(flow_out[carriers=$carrier], over=techs)
          * supply_share_per_timestep_equals
    slices:
      carrier:
        - expression: supply_share_carrier  # assigned as an indexed parameter
