Search code examples
brightway

how to paramaterise an existing exchange with brightway


I want to paramaterize exchanges of an existing brightway activity. in the example I've found the formula is defined for a new_exchange, can we do it for an existing one?

a practical example could be to redefine the fuel consumption as function of higher heating value and efficiency.

ex=[act for act in bw.Database('ei_34con') if 'natural gas' in act['name']
                                  and 'condensing' in act['name']
                                  and 'CH' in act['location']][0].copy()

ng_flow=[f for f in ex.technosphere() if ('natural gas' in f['name'])][0]


act_data=[{'name':'eff',
       'database':ex['database'],
       'code':ex['code'],
       'amount':0.95,
       'unit':''},
       {'name':'HHV',
       'database':ex['database'],
       'code':ex['code'],
       'amount':37,
       'unit':'MJ/m3'}]

bw.parameters.new_activity_parameters(act_data, "my group")

I naively tried

ng_flow['formula']='1/eff/HHV'

bw.parameters.add_exchanges_to_group("my group", ex)
ActivityParameter.recalculate_exchanges("my group")

but parameters did not update the amount of the exchange.


Solution

  • You were quite close.

    I reran your code and the line

    bw.parameters.add_exchanges_to_group("my group", ex)
    

    returns 0. This means no parameters were added.

    However, if I save the exchange first:

     ng_flow.save()
     bw.parameters.add_exchanges_to_group("my group", ex)
    

    returns 1, and

    for exc in ex.technosphere():
        if "natural gas" in exc['name']:
            print(exc.amount, exc.input, exc.output)
    

    Prints

    0.028449502133712657 'market for natural gas, low pressure' (cubic meter, CH, None) 'heat production, natural gas, at boiler condensing modulating <100kW' (megajoule, CH, None)
    

    Note that ng_flow.as_dict() does not show the updated value.