Function for calculating the total emissions of an energy system

Hi,

I am trying to write a function to calculate the total emissions of my energy system with oemof. I have started from the function in the repository:

emission_limit

because I want to know also the total amount. I characterized the flows that I want to include in the calculation with the attribute ‘emission’, and formulated it as:

def emission_tot(om,flows=None):
    
    if flows is None:
        flows = {}
        for (i, o) in om.flows:
            if hasattr(om.flows[i, o], 'emission'):
                flows[(i, o)] = om.flows[i, o]

    else:
        for (i, o) in flows:
            if not hasattr(flows[i, o], 'emission'):
                raise ValueError(('Flow with source: {0} and target: {1} '
                                 'has no attribute emission.').format(i.label, o.label))
                                                                  
    return sum(om.flow[i, o, t]*om.timeincrement[t]*om.flows[i, o].emission
                for (i, o) in flows
                for t in om.TIMESTEPS)
   
    om.emission_tot = emission_tot.__call__(om)

but if I call it as:

global_emissions = emission_tot(om, flows=None)

I get the summation expression, but I can’t evaluate it. I imagine it is an issue of pyomo language, but I can’t find the way to get the result of the expression.
Do you have any suggestion?

Thanks!

Hi,

I modified the code this way:

import pyomo.environ as po

def emission_tot(om,flows=None):
    
    if flows is None:
        flows = {}
        for (i, o) in om.flows:
            if hasattr(om.flows[i, o], 'emission'):
                flows[(i, o)] = om.flows[i, o]

    else:
        for (i, o) in flows:
            if not hasattr(flows[i, o], 'emission'):
                raise ValueError(('Flow with source: {0} and target: {1} '
                                 'has no attribute emission.').format(i.label, o.label))
    def emission_cal(m):                                                              
       return sum(m.flow[i, o, t]*m.timeincrement[t]*m.flows[i, o].emission
                for (i, o) in flows
                for t in m.TIMESTEPS)
    
    om.emission_tot = po.Objective(rule=emission_cal)
   
    return om.emission_tot()

and it seems returning a result.
Do you think is it ok?

Maybe I do not understand your question. Do you want to get the emission results or do you want to set a constraint?

Because the emission limit is a function that sets a constraint. You use it before you pass your model to the solver.

If you want to calculate the overall emission in the results you can just use the results dictionary.