如何向现有Brightway2数据库新增自定义活动并核算影响
Brightway2创建混合纤维自定义活动及影响核算操作流程
一、先修正现有配置的错误
你当前写的配置有几个会直接导致运行报错/结果错误的问题,必须先调整:
- 不要直接在原始ecoinvent数据库里写入自定义活动,单独新建一个自定义库存放,避免污染原始官方数据,后续修改回溯也更方便
- 活动自身的产出交换类型必须设为
production,不能写technosphere,否则Brightway无法识别参考产品流,LCA计算会找不到功能单位 - 活动
code字段不能填"Not available",必须设一个全局唯一的字符串,否则数据库写入会触发主键冲突报错 reference product字段要和你实际产出的产品名(混合纤维)保持一致,不要前后不匹配activity type存在拼写错误,正确写法是aggregated market activity- 输入输出的单位要提前和上游ecoinvent数据集对齐,默认按质量混合比的话统一用kg即可,避免量纲不匹配
二、写入自定义活动
修正后可直接运行的代码如下:
import brightway2 as bw # 确认已加载ecoinvent 3.8 cutoff数据库 eidb = bw.Database('ecoinvent_3_8_cutoff') # 新建独立的自定义数据库 custom_db_name = "custom_fibre_process_db" custom_db = bw.Database(custom_db_name) # 给自定义生产活动设置唯一识别code act_unique_code = "cotton_poly_mixed_fibre_prod_v1" # 构造活动数据 custom_act_config = { (custom_db_name, act_unique_code): { 'activity': 'mixed_fibre_blending_process', 'activity type': 'aggregated market activity', 'code': act_unique_code, 'location': 'GLO', 'name':'Cotton-polyester blended mixed fibre production', 'parameters': {}, 'production amount': 1.0, 'reference product': 'mixed fibre', 'unit': 'kg', 'exchanges': [ # 棉纤维输入 { 'input': next(eidb.search("market for fibre, cotton", limit=1)).key, 'amount': 0.5, 'type': 'technosphere', 'unit': 'kg' }, # 聚酯纤维输入 { 'input': next(eidb.search("market for fibre, polyester", limit=1)).key, 'amount': 0.5, 'type': 'technosphere', 'unit': 'kg' }, # 混合纤维产出(生产交换,必填) { 'input': (custom_db_name, act_unique_code), 'amount': 1, 'type': 'production', 'unit': 'kg' } ] } } # 写入数据库 custom_db.write(custom_act_config) # 校验写入结果 target_act = custom_db.get(act_unique_code) print("活动写入成功,包含交换如下:") for exc in target_act.exchanges(): print(f"- 交换类型:{exc['type']},来源:{exc['input']},数量:{exc['amount']}{exc['unit']}")
提示:用
search匹配上游活动时,建议人工核对返回的活动名称、地理位置、单位是否正确,避免匹配到错误数据集导致结果偏差。
三、环境影响核算
活动写入完成后,选定你需要的影响评价方法即可开展计算,示例代码如下:
# 可通过bw.methods查看本地已导入的所有影响评价方法,按需选择 # 示例选取IPCC 2021 100年时间尺度全球变暖潜值 selected_method = ('IPCC 2021', 'climate change', 'GWP 100a') # 定义功能单位:生产1kg混合纤维 functional_unit = {target_act: 1} # 初始化LCA模块并计算 lca = bw.LCA(functional_unit, selected_method) lca.lci() # 计算生命周期清单 lca.lcia() # 计算生命周期影响评价结果 # 输出结果 print(f"功能单位环境影响结果:{lca.score} kg CO2-eq") # 如需计算多个影响类别,遍历方法列表重复上述lcia步骤即可
内容的提问来源于stack exchange,提问作者gary_brown
相关产品推荐
相关产品推荐

