Adding equate variables constraint to limit flow from one transformer to a bus

I want to limit the share of flow from a transformer to a bus by certain factor which is mentioned by share_in_bus. I tried to incorporate equate_variables function from solph.constraints. However, the function gives me ValueError, I think it could not distinguish between Investmenflow and the actual flow. How can I change my code so that it know which flow to take into consideration?

Any hints would be much appreciated,

Regards,
Dev

def add_flow_limits(projectname, om, bus_i, tech_i, H):
    project = projectname
    input_dir = f'input\\input_table_{project}.xlsx'
    input_fl = pd.read_excel(input_dir, sheet_name='flow_limits', header=12)

    for i, fl in input_fl.iterrows():
        if not fl['include']:
            continue

        from_transformer = tech_i[fl['from_transformer']]
        to_bus = bus_i[fl['to_bus']]

        flow_from_transformer = sum(om.flow[i, o, t] for (i, o) in om.FLOWS if i == from_transformer
                                    for t in om.TIMESTEPS)
        total_flow_to_bus = sum(
            om.flow[i, o, t] for (i, o) in om.FLOWS if o == to_bus for t in om.TIMESTEPS
        )

        if fl['comparison'] == 'equal':
            constraints.equate_variables(
                om,
                flow_from_transformer,
                total_flow_to_bus,
                factor1=fl['share_in_bus'],
                name='flow_limit_equal',
            )
        elif fl['comparison'] == 'greater_than':
            constraints.equate_variables(
                om,
                flow_from_transformer,
                total_flow_to_bus,
                factor1=fl['share_in_bus'],
                name='flow_limit_greater',
            )
            om.flow_limit_greater.lower = 0
        elif fl['comparison'] == 'smaller_than':
            constraints.equate_variables(
                om,
                flow_from_transformer,
                total_flow_to_bus,
                factor1=fl['share_in_bus'],
                name='flow_limit_smaller',
            )
            om.flow_limit_smaller.upper = 0

    return om

Hi @Dev,

honestly, I do not get where flow_limit_greater.lower and om.flow_limit_smaller.upper are defined.

Hi,

I think you might want to look into defining own constraints. This is included in the flexible_modelling example. constraints.equate_variables can be used to check for equality, but you want to check for a value being greater than resp. lower than another value. As long as you don’t introduce a slack variable, you need to formulate a “greater than” or “smaller than” equation yourself.

Here is the example, where own constraints are introduced: oemof-solph/examples/flexible_modelling/add_constraints.py at dev · oemof/oemof-solph · GitHub

Hope that helps.

Best,
Johannes