oemof / oemof-solph

A model generator for energy system modelling and optimisation (LP/MILP).
https://oemof.org
MIT License
282 stars 124 forks source link

How to define a switching Converter ? #1065

Open mdonat opened 2 months ago

mdonat commented 2 months ago

I would like to add a Converter which can only be on or off. So output flow value should be 0 or nominal_value. Is that possible? If not, how to define for example a pump which converts electrical energy to heat, and can only be on or off?

fwitte commented 2 months ago

Hi @mdonat,

you have to add the NonConvex on the input or output flow of the Converter, and the attribute min=1.

heat_pump = solph.components.Converter(
    "heat pump",
    inputs={b_electricity: solph.Flow()},
    outputs={
        b_heat: solph.Flow(min=1, nonconvex=solph.NonConvex(), nominal_value=10),  # e.g. 10 kW heat
    },
    conversion_factors={b_heat: COP}
)
mdonat commented 2 months ago

I tried this, but it seems that it cannot be solved. This is the code:

        energysystem = solph.EnergySystem(timeindex=times)

        bel = solph.buses.Bus(label='electricity')
        b_heat = solph.buses.Bus(label='heat')

        # create source object representing the e commodity
        energysystem.add(
                solph.components.Source(
                label="e-in",
                outputs={
                bel: solph.flows.Flow(variable_costs=50)
                },
            )
        )

        # create storage object for heat
        nominal_capacity = 4000
        nominal_value = 400

        heat_storage = solph.components.GenericStorage(
            nominal_storage_capacity=nominal_capacity,
            label="STORAGE_HEAT",
            inputs={b_heat: solph.flows.Flow(nominal_value=nominal_value, variable_costs=0.001)},
            outputs={
                b_heat: solph.flows.Flow(
                    nominal_value=nominal_value, variable_costs=0.001
                )
            },
            loss_rate=0.00,
            initial_storage_level=0.5,
            inflow_conversion_factor=1,
            outflow_conversion_factor=1,
        )

        energysystem.add(heat_storage)

        sink = solph.components.Sink(
            label="demand_th",
            inputs={b_heat: solph.Flow(nominal_value=200, fix=1)}
        )

        energysystem.add(solph.components.Converter(label="heatPump",
           inputs={bel: solph.flows.Flow(
           )},
           outputs={b_heat: solph.flows.Flow(nominal_value=400,
           nonconvex=solph.NonConvex(),
           min=1,
           )},
           conversion_factors={b_heat: 1}
           ))

        energysystem.add(bel, b_heat, sink)

        # solve problem
        om = solph.Model(energysystem)
        om.solve(solver="cbc", solve_kwargs={"tee": True})

If I uncomment "min=1" it solves with constant flow value of 200. With "min=1" it does not solve, ...only if I set nominal_value to 200, so that the heat pump will be constantly on.

fwitte commented 2 months ago

Your code is a incomplete, e.g. times is never defined, could you add that?

Also, the model might be infeasible, because your heat pump cannot provide 200 units of heat, only 400 or 0. Since your demand_th takes 200 units fixed at all times, nothing in your system can provide that amount of energy. The storage might be able to buffer that for up to 10 hours, but it is constrained to be cyclic by default. So depending on the number of timesteps you are running the model, it might be infeasible because the storage can never be balanced back to half way full.

To circumvent that, you could add an additional sink, which takes up any amount of heat but penalizes the model to do so (by giving the flow very high variable cost). Then you can check, why the infeasibility occurs.

mdonat commented 2 months ago

Missing code for times:

 today = datetime.date.today()
 start_day = today - datetime.timedelta(days=1)
 times = pd.date_range(start=start_day, end=today, freq='5T')

I have added an additional sink:

energysystem.add(
    solph.components.Sink(
        label="excess_bus_heat",
        inputs={b_heat: solph.flows.Flow(variable_costs=100)},
    )
)

But it does not work. I thought that, if the heat pump is working half the time, it provides 200 units of heat to demand_th, and 200 units to the storage. The other half, when it is switched off, the storage provides 200 units of heat to demand_th.

fwitte commented 2 months ago

Okay, then it was a misunderstanding. I thought the problem was infeasible...

After running it, in my opinion, the issue here is, that the problem seems to have an extremely flat optimum: There is basically tons of solutions which all lead to the same objective but are very different in dispatch (the model really is indifferent on when to charge or discharge storage).

mdonat commented 2 months ago

Ok, that's right, there is no clear optimum. But if I set min=0.5 it is working. Should have even more possible solutions then... Thank you for the quick support. :-)

fwitte commented 2 months ago

But if I set min=0.5 it is working. Should have even more possible solutions then...

Due to the storage operation cost the optimum is then very easy to find: never use the storage. ;)