Brightway 2.5 - 使用数据包将场景与我自己的数据库和 Ecoinvent 进行比较

问题描述 投票:0回答:1

我正在尝试创建一些代码来将场景与相同的数据库(活动和交流)进行比较。 交换的价值将在每次运行时更新。

我想避免在每次交换时使用“exc['amount'] 和 exc.save()”。我知道数据包可以更快地完成这个任务,就像在这个笔记本

中一样

我最终得到了一个小型数据库示例的以下代码,但出现以下错误:

bw2calc.errors.NonsquareTechnosphere: Technosphere matrix is not square: 1 activities (columns) and 4 products (rows).

最后我将拥有超过 50 个场景和一个包含大约 70 个新活动的数据库。

有人有带有数据包和 Ecoinvent 数据库的代码示例吗?我认为我的错误来自 Ecoinvent 和我的数据库之间的链接。

我使用 Python 3.12.2 和以下 Brightway 2.5 包:

bw2calc                   2.0.dev18                  
bw2data                   4.0.dev46                
bw2io                     0.9.dev30                                     
bw_processing             0.9.6     

    
import bw2data as bd
import bw2io as bi
import numpy as np
import bw2calc as bc
import bw_processing as bwp


bd.projects.set_current("Save3")

# ====== Import ecoinvent ======
if 'Ecoinvent_3.9.1_apos' in bd.databases:
    print("Database has already been imported.")

# ======  Database creation ====== 
MyDB = bd.Database('MyDB')
MyDB.register()

eidb = bd.Database('Ecoinvent_3.9.1_apos')
eidb.order_by='name'
bio = bd.Database("biosphere3")

# ======  Create New Activity ====== 
Prod_Sirop = MyDB.new_activity(code='Psirop', name="Production de sirop", unit="kilogram",
                            comment="production de sirop sur l'année de référence (2019)")
Prod_Sirop.save()

electricityMix_FR = bd.utils.get_node(database = 'Ecoinvent_3.9.1_apos', name = 'market for electricity, medium voltage', location = 'FR', unit = 'kilowatt hour')
Prod_Sirop.new_exchange(input=electricityMix_FR.key, amount=0,unit="kWh", type="technosphere").save()

market_selsNACl = bd.utils.get_node(database = 'Ecoinvent_3.9.1_apos', name = 'market for sodium chloride, powder', location = 'GLO', unit = 'kilogram')
Prod_Sirop.new_exchange(input=market_selsNACl.key, amount=50000, unit="kilogram", type="technosphere").save()

SiropProduction = 20000000 # m3/an
Prod_Sirop.new_exchange(input=Prod_Sirop, amount=SiropProduction, unit="cubic meter", type="production").save()
Prod_Sirop.save()


# ======  create datapackage ====== 
dp_newscenario = bwp.create_datapackage()

t_indices= np.array([(electricityMix_FR.id, Prod_Sirop.id),
                     (market_selsNACl.id, Prod_Sirop.id)],
                    dtype=bwp.INDICES_DTYPE)
t_data =np.array([2000, 4000])

# Create vector for a new scenario
dp_newscenario.add_persistent_vector(
    matrix='technosphere_matrix',
    indices_array=t_indices,
    data_array=t_data,
    flip_array=np.array([True, True]),
    name='Sc1')

# ======  Do lci/ lcia ====== 
lca = bc.LCA(
    demand={Prod_Sirop: 1},
    data_objs=[dp_newscenario],
    use_distributions=False,
    use_arrays=True,
    method=('ReCiPe 2016 v1.03, midpoint (H)','climate change','global warming potential (GWP1000)'))

lca.lci()
lca.lcia()

brightway python-3.12
1个回答
0
投票

当您定义 LCA 时,我认为您需要的不仅仅是您创建的数据包,它告诉了技术圈矩阵中需要修改的内容。 LCA 对象需要其余信息。

检查这个例子

尝试在 lca 之前运行

fu, data_objs , _ = bd.prepare_lca_inputs
以获取所有其余信息,然后将其提供给 lca 对象。

我还怀疑您的 t_data 中缺少一些方括号。请注意,您使用的示例仅修改了一个交换,而您正在修改两个交换。尝试先只修改一个,然后让示例变得更复杂。

© www.soinside.com 2019 - 2024. All rights reserved.