How can I access the BusBlock Object from a Bus?

Hello,
I try to implement a robust energystsystem optimization using oemofand rsome. While programming this I faced the follwing problem:

The constraints for the buses are implmented in the bus.py. Here in the Busblock class the create function states:

def _create(self, group=None):
        """Creates the balance constraints for the class:`BusBlock` block.

        Parameters
        ----------
        group : list
            List of oemof bus (b) object for which the bus balance is created
            e.g. group = [b1, b2, b3, .....]
        """
        if group is None:
            return None

        m = self.parent_block()

        ins = {}
        outs = {}
        for n in group:
            ins[n] = [i for i in n.inputs]
            outs[n] = [o for o in n.outputs]

        def _busbalance_rule(block):
            for t in m.TIMESTEPS:
                for g in group:
                    lhs = sum(m.flow[i, g, t] for i in ins[g])
                    rhs = sum(m.flow[g, o, t] for o in outs[g])
                    expr = lhs == rhs
                    # no inflows no outflows yield: 0 == 0 which is True
                    if expr is not True:
                        block.balance.add((g, t), expr)

        self.balance = Constraint(group, m.TIMESTEPS, noruleinit=True)
        self.balance_build = BuildAction(rule=_busbalance_rule)

here it is referend to the parent block of self (m=self.parent_block()). This i want to recrate outsite of the BusBlock class. Given a bus b i want the parent block of the BusBlock object.

I trieded it using the constraint_group() function on b,but here only the class is returned not an object of the class. Wenn I tried converting this into an object using this code:

 m = b.constraint_group()
 n=m()

Then n was None.
Does anyone have an idea?

Hello Lena,

I think, I do not fully understand your problem.
Code-wise, I think the parent block of the BusBlock is the model (instance of oemof.solph._models.Model) itself. Let’s assume your model is called m.
Then you can access the bus balancing constraint by model.BusBlock.balance or access the model as the parent block within your newly created constraints as in the code example you quote.

I hope this helps at least a bit. For creating custom constraints, there are some examples in the oemof.solph repository, e.g. this one: https://github.com/oemof/oemof-solph/blob/dev/examples/flexible_modelling/add_constraints.py

Maybe debugging your model in an IDE of your choice can also help you understand the hierarchy and how to access dedicated objects.

Best regards and good luck,
Johannes

Hi Lena,

Maybe, it helps to clarify that all Buses share the same BusBlock. Inside the BusBlock, a multi-dimensional constraint exists, that uses the Bus objects as the first index and the time step as the second index. (The same is true for all other entities. We do so, to have similar constraints grouped together, which in the end should accelerate the optimisation process.)

Hope this helps,
Patrik