Spice Sim

diodeinc/pcb/skills/spice-sim

作者 diodeincb373e22a6778无许可证456 个星标收录于 2026年10月8日更新于 2026年10月8日仓库今天更新

Add or run an ngspice-backed Zener testbench with `pcb sim`.

AI 生成的概览

指导通过 pcb sim 命令添加或运行由 ngspice 支持的 Zener 测试台。

功能
该技能说明如何为启动、使能、保护阈值或电流限制等电路行为编写并运行聚焦的 SPICE 测试台。内容涵盖为元件附加 SPICE 模型、将 nets 与子电路端子对应、传入 PARAMS 参数、编写包含电源、分析和绘图命令的 ngspice setup 字符串,以及解读 pcb sim 的输出。它还介绍结果处理,包括测量行、输出目录、SVG 转 PNG 以及通过导出网表进行更长时间的仿真。
适用场景
当需要针对特定行为建立电路仿真测试台、需要将 SPICE 模型接入元件,或需要解读或扩展 pcb sim 运行结果时使用。
运行要求
需要 pcb sim 命令和 ngspice 可执行文件,以及 Zener 元件和 Simulation 定义。非通用元件需要厂商或行为级 SPICE 模型。绘图转换需要 rsvg-convert,且在 hardcopy 或 wrdata 写入前必须存在输出目录。该技能不附带脚本。

Spice Simulation

Use a focused testbench such as <package>/testbench/test_<scenario>.zen for one behavior: startup, enable, a protection threshold, or current limit. Reuse an existing bench when it covers the request. Run it with pcb sim -v <file>.zen; a separate dummy simulation is only useful when diagnosing setup problems.

pcb sim runs the ngspice executable in batch mode in the testbench's directory and stops it after 5 seconds. Without -v, a successful run prints only that it passed.

Model and testbench

Every component that is not DNP needs a SPICE model. The stdlib generics, such as resistors, capacitors, inductors, ferrite beads, crystals, diodes, LEDs, and TVS diodes, have their own. A leaf component needs spice_model=SpiceModel(...). Obtain a vendor model or, when appropriate, use a behavioral model with explicit limitations. Match nets to the subcircuit's declared terminal order, and pass every PARAMS: entry in args, because defaults are not applied:

zen
Component(    name="MyPart",    symbol=Symbol(library="MyPart.kicad_sym"),    footprint=File("MyPart.kicad_mod"),    part=Part(mpn="MYPART-1", manufacturer="Vendor"),    pins={"VIN": VIN, "VOUT": VOUT, "GND": GND},    spice_model=SpiceModel(        "MyPart.lib",        "MyPart_SUBCKT",        nets=[VIN, VOUT, GND],        args={},    ),)

The model text goes into the netlist, so a .include in the model file resolves from the testbench's directory. The .subckt line must list all its terminals on one line; terminals on + continuation lines are not counted. For a PSpice model, put set ngbehavior=psa in a .spiceinit file beside the testbench; for an LTspice model, set ngbehavior=ltpsa. Without it, PSpice expressions such as VALUE={LIMIT(...)} give wrong results without an error. Verilog-A and encrypted models do not run.

Instantiate the module under test and needed loads/passives in Zener. Put sources, waveforms, analysis, and plot commands in the raw ngspice setup string of a top-level Simulation:

zen
load("@stdlib/properties.zen", "Simulation")
Simulation(    name="SIM",    setup="""V_IN VIN GND DC 12.control  tran 10u 10m  meas tran vout_final FIND v(VOUT) AT=10m  set hcopydevtype = svg  hardcopy output/startup.svg v(VIN) v(VOUT).endc""",)

Adapt the source and analysis to the behavior; for example, PULSE(...) for enable timing or PWL(...) for a changing input. Operating point, DC sweep, AC, transient, and noise analyses all run.

Results

pcb sim reports success whenever ngspice exits normally, including when a meas fails, a plot is not written, or an expression divides by zero. Read the -v output: take values from meas or echo lines, because print tables lose their column separators. hardcopy and wrdata write only into an existing directory, so create testbench/output/ first. Convert an SVG plot to PNG with rsvg-convert before viewing it, and inspect only the signals needed to establish the result.

A switching converter at nanosecond steps simulates only about 1 to 3 ms within the 5-second limit. For a longer run, write the netlist with pcb sim <file>.zen -o <file>.cir, then run ngspice -b <file>.cir in the testbench's directory with a longer timeout.

Distinguish a model or simulator failure from an electrical finding, and state what the model and analysis actually verify.

来源与署名

来源:diodeinc/pcb位于skills/spice-sim提交b373e22

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架