首页 示例 截图 用户手册 Bluesky logo YouTube
OghmaNano 模拟有机/钙钛矿太阳能电池、OFET 和 OLED 下载

Python 脚本

1. 介绍

在 OghmaNano 中,Python 脚本通过直接编辑磁盘上的仿真配置文件 然后调用仿真引擎(oghma_core.exe)来运行模型。 本节将详细说明这一工作流程。

2. 使用 Python 运行仿真文件

一个 OghmaNano 仿真完全由单个 JSON 配置文件 sim.json 定义。 该文件包含仿真的完整状态,包括器件结构、 材料参数、数值设置和输出配置。 在许多情况下,sim.json 已经作为普通文件存在于仿真目录中。

使用 Python 驱动 OghmaNano 时,脚本会使用 Python 标准 json 库将 sim.json 读入内存,修改一个或多个参数,然后将更新后的 JSON 写回磁盘。

关键的是,Python 脚本必须在其工作目录设置为仿真目录的情况下执行 —— 也就是包含 sim.json(以及 sim.oghma)的目录。 这也是启动 oghma_core.exe 的目录。

JSON 文件更新后,脚本会 从同一目录 启动 oghma_core.exe。 仿真引擎总是从当前工作目录读取其输入文件, 因此如果工作目录不正确,仿真将失败或运行错误的模型。

这种职责分离是有意且明确的: Python 负责定义仿真中改变什么, 而 OghmaNano 负责利用当前目录中存在的文件运行物理模型。

下面的示例通过加载 sim.json、修改第一器件层的载流子 迁移率、将更新后的配置写回磁盘,然后 从仿真目录执行仿真来演示这一过程。


       import json
       import os
       import sys

       f=open('sim.json')              #open the sim.json file
       lines=f.readlines()
       f.close()
       lines="".join(lines)    #convert the text to a python json object
     data = json.loads(lines)

     #Edit a value (use firefox as a json viewer
     # to help you figure out which value to edit)
     # this time we are editing the mobility of layer 1
     data['epitaxy']['segment1']['shape_dos']['mue_y']=1.0


     #convert the json object back to a string
     jstr = json.dumps(data, sort_keys=False, indent='\t')

     #write it back to disk
     f=open('sim.json',"w")
     f.write(jstr)
     f.close()

     #run the simulation using oghma_core
     os.system("oghma_core.exe")

如果 sim.json 中的仿真被设置为运行 J–V 曲线,OghmaNano 将把输出文件写入 仿真目录,其中包含诸如 PCE、填充因子、\(J_{sc}\)\(V_{oc}\) 等量。

下面的示例展示了如何读取 JSON 输出文件,并将 \(V_{oc}\) 的值附加到单独的文本文件中。

f=open('sim_info.dat')
lines=f.readlines()
f.close()
lines="".join(lines)
data = json.loads(lines)

f=open('out.dat',"a")
f.write(str(data["Voc"])+"\n");
f.close()

3. 更复杂的仿真

在许多脚本工作流程中,您会希望使用不同参数值多次运行同一个基础仿真, 并将每次运行各自隔离在自己的目录中。这是保持输出整洁并避免 意外覆盖结果的最简单方法。

下面的示例创建了一组对应于四个迁移率的目录 (1e-51e-61e-71e-8)。对于每个目录,它会:

  1. 创建该目录(如果它尚不存在)。
  2. 将当前的 sim.json 复制到该目录中。
  3. 编辑复制后的 sim.json 以设置目标迁移率。
  4. 将工作目录切换到该目录。
  5. 在该目录中运行求解器,从而生成该次运行本地的输出。

这种模式是 OghmaNano 中批处理脚本的基础:每次运行一个目录,每次运行一个配置文件, 并为每个参数值保留一个干净的输出文件夹。


       import json
       import os
       import shutil

       # The script must be started in the base simulation directory
       # (i.e. the directory containing the reference sim.json).
       base_dir = os.getcwd()

       mobilities = [1e-5, 1e-6, 1e-7, 1e-8]

       for mu in mobilities:
           # Create a directory name that is easy to read and sorts nicely
           run_dir = os.path.join(base_dir, f"mu_{mu:.0e}")
           os.makedirs(run_dir, exist_ok=True)

           # Copy the base sim.json into the run directory
           src_sim = os.path.join(base_dir, "sim.json")
           dst_sim = os.path.join(run_dir, "sim.json")
           shutil.copyfile(src_sim, dst_sim)

           # Load the copied sim.json and edit the mobility in THAT copy
           f = open(dst_sim)
           lines = f.readlines()
           f.close()
           lines = "".join(lines)
           data = json.loads(lines)

           # Edit a value: set the mobility for the first device layer
           data['epitaxy']['segment1']['shape_dos']['mue_y'] = mu

           jstr = json.dumps(data, sort_keys=False, indent='\t')
           f = open(dst_sim, "w")
           f.write(jstr)
           f.close()

           # Change into the run directory and execute the solver there
           os.chdir(run_dir)

           # If your command is "augment.nano" instead, replace the string below.
           os.system("oghma_core.exe")

           # Return to the base directory for the next run
           os.chdir(base_dir)

运行后,每个目录都将包含对应于该迁移率值的输出。 在下一节中,我们将扩展此脚本,以收集每次运行中的关键结果并将它们汇总到 单个摘要文件中。

绘制 \(V_{oc}\) 与反迁移率的关系

在单独目录中运行完一批仿真之后,下一步通常是从每次运行中提取一个关键性能指标 并将趋势可视化。一个常见示例是从 JSON 输出文件(sim_info.dat)中读取开路电压 \(V_{oc}\),并将其相对于 反迁移率 \(1/\mu\) 作图。

下面的脚本会扫描一组运行目录(例如 mu_1e-05mu_1e-06、...), 并对每个目录执行:

  1. sim_info.dat 作为 JSON 读取。
  2. 提取 Voc
  3. 使用与该目录对应的迁移率计算 \(1/\mu\)
  4. 写出一个小型摘要文件(voc_vs_inv_mobility.dat)。
  5. 绘制 \(V_{oc}\)\(1/\mu\) 的图。

       import json
       import os

       # Optional: plotting (requires matplotlib)
       import matplotlib.pyplot as plt

       base_dir = os.getcwd()

       # Mobilities must match the run directories you generated earlier
       mobilities = [1e-5, 1e-6, 1e-7, 1e-8]

       inv_mu = []
       vocs = []

       for mu in mobilities:
           run_dir = os.path.join(base_dir, f"mu_{mu:.0e}")
           info_path = os.path.join(run_dir, "sim_info.dat")

           # Read the JSON output file and extract Voc
           f = open(info_path)
           lines = f.readlines()
           f.close()
           lines = "".join(lines)
           data = json.loads(lines)

           voc = data["Voc"]

           inv_mu.append(1.0 / mu)
           vocs.append(voc)

       # Write a small summary table to disk
       out = open("voc_vs_inv_mobility.dat", "w")
       out.write("# inv_mobility(1/mu)    Voc(V)\n")
       for x, y in zip(inv_mu, vocs):
           out.write(f"{x:.6e}    {y}\n")
       out.close()

       # Plot Voc against inverse mobility
       plt.plot(inv_mu, vocs, "o-")
       plt.xlabel("Inverse mobility (1/μ)")
       plt.ylabel("Open-circuit voltage Voc (V)")
       plt.title("Voc vs inverse mobility")
       plt.grid(True)
       plt.tight_layout()
       plt.show()

📺 相关视频

下面的视频演示了如何使用 Python 脚本驱动 OghmaNano。

注意: 视频中显示的 API 和内部脚本接口现已弃用,并且不再随软件提供, 因为它们被证明难以维护。当前推荐的工作流程是使用外部 Python 脚本直接编辑 JSON 仿真文件,然后调用 oghma_core.exe。这种方法 更简单、更稳健,也更容易维护。您可能仍会发现该视频有助于理解整体工作流程和典型使用场景,但对于 最新方法,请遵循上文所述步骤。

在 OghmaNano 中对钙钛矿太阳能电池仿真进行 Python 脚本控制