hashgraph-online/awesome-codex-plugins / plugins/aklofas/kicad-happy/skills/spice
spice
VerifiedRun automatic SPICE simulations on subcircuits detected from KiCad schematic analysis — validates filter frequencies, divider ratios, opamp gains, LC resonance, and crystal load capacitance. Supports ngspice, LTspice, and Xyce (auto-detected). Generates testbenches, runs batch mode, produces structured pass/warn/fail/info report. Use when the user asks to simulate, verify, or validate any analog subcircuit — RC filters, LC filters, voltage dividers, opamp circuits, crystal oscillators. Also for "simulate my circuit", "run spice", "verify with simulation", "check my filter cutoff", "does this divider give the right voltage", "what's the bandwidth of this opamp stage". Consider suggesting simulation during design reviews when the schematic analyzer reports simulatable subcircuits and a SPICE simulator is available.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 24.8 KB |
| references/simulation-models.md | markdown | 21.1 KB |
| scripts/_core_path.py | script | 697 B |
| scripts/extract_parasitics.py | script | 11.3 KB |
| scripts/run_external_deck.py | script | 7.3 KB |
| scripts/simulate_subcircuits.py | script | 30.4 KB |
| scripts/spice_model_cache.py | script | 19.0 KB |
| scripts/spice_model_generator.py | script | 6.9 KB |
| scripts/spice_models.py | script | 7.2 KB |
| scripts/spice_part_library.py | script | 27.6 KB |
| scripts/spice_results.py | script | 53.6 KB |
| scripts/spice_simulator.py | script | 26.6 KB |
| scripts/spice_spec_fetcher.py | script | 23.8 KB |
| scripts/spice_templates.py | script | 72.8 KB |
| scripts/spice_tolerance.py | script | 10.6 KB |
| scripts/test_spice_simulator.py | script | 43.1 KB |
scripts/simulate_subcircuits.py
View on GitHub#!/usr/bin/env python3
"""
SPICE simulation orchestrator for kicad-happy.
Reads analyzer JSON output (from analyze_schematic.py), identifies simulatable
subcircuits, generates ngspice testbenches, runs simulations, and produces a
structured report.
Usage:
python3 simulate_subcircuits.py analysis.json
python3 simulate_subcircuits.py --schematic analysis.json --output sim_report.json
python3 simulate_subcircuits.py --analysis-dir analysis/
python3 simulate_subcircuits.py analysis.json --workdir /tmp/spice_runs
python3 simulate_subcircuits.py analysis.json --timeout 10
python3 simulate_subcircuits.py analysis.json --types rc_filters,voltage_dividers
When --analysis-dir is given, the schematic is auto-resolved from the current
run (analysis/<run>/schematic.json), the output is written to spice.json in
the same run and registered in the manifest, and simulation artifacts (.cir,
.log) are written to analysis/<run>/spice_work/ instead of /tmp/.
Requires: ngspice (install: apt install ngspice / brew install ngspice)
"""
import argparse
import atexit
import dataclasses
import inspect
import json
import os
import shutil
import sys
import tempfile
import time
# Allow imports from same directory
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import _core_path # noqa: F401
from kicad_core.fileio import atomic_write_text, json_load
from kicad_core.cli import add_compact_arg, add_output_arg, add_text_arg, utf8_text_stdout
from kicad_core.diagnostics import OnceSink, record
from spice_templates import TEMPLATE_REGISTRY, TOPLEVEL_REGISTRY, list_supported_types, SpiceTestbench
from spice_results import (
EVALUATOR_REGISTRY,
build_report,
parse_output_file,
)
# Map detector key → singular subcircuit_type for output
# Most are just rstrip("s"), but some need special handling
_SINGULAR = {
"decoupling_analysis": "decoupling",
"power_regulators": "regulator_feedback",
"rf_matching": "rf_matching",
"bridge_circuits": "bridge_circuit",
"inrush_analysis": "inrush",
"bms_systems": "bms_balance",
"rf_chains": "rf_chain",
"snubber_circuits": "snubber_circuit",
}
def _singular_type(det_type):
"""Convert a detector key to its singular subcircuit_type name."""
if det_type in _SINGULAR:
return _SINGULAR[det_type]
return det_type.rstrip("s")
def find_ngspice():
"""Locate the ngspice binary. Returns path or None.
Legacy wrapper — prefer detect_simulator() for multi-simulator support.
"""
from spice_simulator import NgspiceBackend
backend = NgspiceBackend()
return backend.find()
def run_ngspice(cir_file, timeout=5, cwd=None):
"""Run ngspice in batch mode on a .cir file.
Legacy wrapper — prefer simulator_backend.run() for multi-simulator.
"""
from spice_simulator import NgspiceBackend
backend = NgspiceBackend()
return backend.run(cir_file, timeout=timeout, cwd=cwd)
def simulate_subcircuits(analysis_json, workdir=None, timeout=5, types=None,
parasitics=None, simulator_backend=None,
monte_carlo_n=0, mc_distribution="gaussian",
mc_seed=42, model_options=None):
"""Run SPICE simulations for all simulatable subcircuits in the analysis.
Args:
analysis_json: Parsed JSON dict from analyze_schematic.py
workdir: Directory whose new spice_sim_* subfolder receives the .cir and output files (default: temp dir)
timeout: Seconds per simulation (default: 5)
types: List of detector types to simulate, or None for all supported
parasitics: PCB parasitic data from extract_parasitics.py
simulator_backend: SimulatorBackend instance (auto-detected if None)
monte_carlo_n: Number of Monte Carlo tolerance trials per subcircuit (0=disabled)
mc_distribution: "gaussian" (3sigma=tolerance) or "uniform"
mc_seed: Random seed for reproducible Monte Carlo runs
model_options: spice_model_cache.ModelOptions (cache dir / offline) or None
Returns:
Report dict with simulation results
"""
# Detect pre-v1.3 format
if 'signal_analysis' in analysis_json and 'findings' not in analysis_json:
import sys as _sys
print('Warning: this JSON uses the pre-v1.3 signal_analysis wrapper '
'format. Re-run analyze_schematic.py to produce the current '
'findings[] format.', file=_sys.stderr)
return build_report([])
# Build detector-keyed lookup from flat findings[]
# Map detector names to legacy keys used by TEMPLATE_REGISTRY
_DET_KEY_OVERRIDES = {
"detect_decoupling": "decoupling_analysis",
"detect_integrated_ldos": "power_regulators",
}
# Analysis JSON may carry duplicate finding objects (e.g. cached
# analysis/ output from an older analyzer) — don't collect the
# same subcircuit for simulation twice. id()-based
# dedup is safe for any detector (a dict appearing twice in the same
# analysis output is always an aliasing quirk, never intentional). The
# (detector, components) key belt is scoped to detect_voltage_dividers
# only, mirroring the flatten-site fix in analyze_schematic.py — other
# detectors legitimately emit multiple distinct findings that share a
# component pair (e.g. VM-001 on different nets), so a components-only
# key would wrongly collapse those.
signal = {}
_seen_ids = set()
_seen_vd_keys = set()
for f in analysis_json.get("findings", []):
det = f.get("detector", "")
if not det:
continue
if id(f) in _seen_ids:
continue
_seen_ids.add(id(f))
if det == "detect_voltage_dividers":
vkey = (det, tuple(f.get("components") or []))
if vkey in _seen_vd_keys:
continue
_seen_vd_keys.add(vkey)
key = _DET_KEY_OVERRIDES.get(det)
if not key:
key = det[len("detect_"):] if det.startswith("detect_") else det
signal.setdefault(key, []).append(f)
if not signal:
return build_report([])
# Synthesize snubber_circuits from transistor_circuits with snubber_data
tc = signal.get("transistor_circuits", [])
if isinstance(tc, list):
snubber_circuits = [t for t in tc if isinstance(t, dict) and t.get("snubber_data")]
if snubber_circuits:
signal["snubber_circuits"] = snubber_circuits
# Filter to requested types
sim_types = types if types else list_supported_types()
# Set up working directory
cleanup_workdir = False
if workdir is None:
workdir = tempfile.mkdtemp(prefix="spice_sim_")
# Removed at exit, not on return, so library callers can still read the benches
atexit.register(shutil.rmtree, workdir, True)
cleanup_workdir = True
else:
if os.path.islink(workdir):
raise ValueError(f"workdir {workdir} is a symlink")
os.makedirs(workdir, exist_ok=True)
# Benches go into a new private folder, so files a project left in a reused
# workdir are never written through or sourced by the simulator
run_dir = workdir if cleanup_workdir else tempfile.mkdtemp(prefix="spice_sim_", dir=workdir)
sim_diagnostics: list = [] # Monte Carlo trials add only records the nominal runs lack
# An analysis that names no source file, or a relative one not found from the cwd,
# caches its models under this run's folder, and so does a project whose spice/ or
# spice/models/ is a symlink, which would send cache writes to the link's target.
from kicad_core.project import extraction_project_dir
if model_options is None or model_options.cache_dir is None:
project_dir = extraction_project_dir(analysis_json)
linked = None
if project_dir is not None:
linked = next((p for p in (project_dir / "spice", project_dir / "spice" / "models")
if p.is_symlink()), None)
if linked is not None:
record(sim_diagnostics, "simulate_subcircuits.simulate_subcircuits",
OSError(f"{linked} is a symlink"),
"part models cached in the run's folder")
if project_dir is None or linked is not None:
from spice_model_cache import ModelOptions
model_options = dataclasses.replace(model_options or ModelOptions(),
cache_dir=run_dir)
results = []
total_time = 0
# Monte Carlo setup (lazy import only when needed)
mc_rng = None
if monte_carlo_n > 0:
import random as _random
mc_rng = _random.Random(mc_seed)
def _run_single_sim(det_run, det_type, generator, evaluator,
cir_file, out_file, log_file, diagnostics=None):
"""Run one simulation: generate -> run -> parse -> evaluate.
Returns (result_dict, elapsed) or (None, 0) on failure/skip.
"""
nonlocal total_time
# The simulator runs inside the cir file's directory and sees only
# bare filenames, so no path (spaces, backslashes) reaches its
# control language.
out_file_spice = os.path.basename(out_file)
gen_kwargs = {"context": analysis_json, "parasitics": parasitics}
if model_options is not None and "model_options" in inspect.signature(generator).parameters:
gen_kwargs["model_options"] = model_options
# Repeated (Monte Carlo) runs hand the generator the once-sink, so a fallback it
# already reported is not printed or recorded again.
if isinstance(diagnostics, OnceSink) and "diagnostics" in inspect.signature(generator).parameters:
gen_kwargs["diagnostics"] = diagnostics
try:
cir_content = generator(det_run, out_file_spice, **gen_kwargs)
except (KeyError, TypeError, ValueError) as e:
return {"_generator_fail": True,
"note": f"testbench generation failed: {type(e).__name__}: {e}"}, 0
if cir_content is None:
return None, 0
if isinstance(cir_content, SpiceTestbench):
if diagnostics is not None:
diagnostics.extend(cir_content.diagnostics)
cir_content = cir_content.render(simulator_backend, out_file_spice)
with open(cir_file, "w", encoding="utf-8", newline="\n") as f:
f.write(cir_content)
t0 = time.monotonic()
if simulator_backend:
success, stdout, stderr = simulator_backend.run(
os.path.basename(cir_file), timeout=timeout,
cwd=os.path.dirname(os.path.abspath(cir_file)))
else:
success, stdout, stderr = run_ngspice(
os.path.basename(cir_file), timeout=timeout,
cwd=os.path.dirname(os.path.abspath(cir_file)))
elapsed = time.monotonic() - t0
total_time += elapsed
with open(log_file, "w", encoding="utf-8", newline="\n") as f:
f.write(f"=== stdout ===\n{stdout}\n=== stderr ===\n{stderr}\n")
if not success:
# the simulator's first error lines (bare deck name; an include error names the include as written)
err_lines = [line.strip() for line in (stderr or "").splitlines() if line.strip()]
reason = " ".join(err_lines[:3]) or "simulator exited with an error"
record(diagnostics, "simulate_subcircuits.run",
RuntimeError(f"{os.path.basename(cir_file)}: {reason}"), "simulation skipped")
return None, elapsed
if simulator_backend and hasattr(simulator_backend, 'parse_results'):
sim_data = simulator_backend.parse_results(out_file, stdout, stderr)
else:
sim_data = parse_output_file(out_file)
if evaluator:
det_run["_context"] = analysis_json
result = evaluator(det_run, sim_data)
det_run.pop("_context", None)
else:
result = {
"subcircuit_type": _singular_type(det_type),
"components": _get_components(det_run),
"status": "pass",
"simulated": sim_data,
}
return result, elapsed
def _run_detection_batch(det_type, detections, generator, evaluator):
"""Simulate a batch of detections — shared by signal_analysis and top-level loops."""
for i, det in enumerate(detections):
label = _make_label(det_type, det, i)
cir_file = os.path.join(run_dir, f"{label}.cir")
out_file = os.path.join(run_dir, f"{label}.out")
log_file = os.path.join(run_dir, f"{label}.log")
# Nominal simulation
result, elapsed = _run_single_sim(
det, det_type, generator, evaluator,
cir_file, out_file, log_file, diagnostics=sim_diagnostics)
if result is None:
if elapsed > 0:
results.append({
"subcircuit_type": _singular_type(det_type),
"components": _get_components(det),
"status": "skip",
"note": "Simulation failed",
"cir_file": cir_file,
"log_file": log_file,
"elapsed_s": round(elapsed, 3),
})
continue
# Generator-time failure — still emit a skip record
if isinstance(result, dict) and result.get("_generator_fail"):
results.append({
"subcircuit_type": _singular_type(det_type),
"components": _get_components(det),
"status": "skip",
"note": result.get("note", "testbench generation failed"),
"elapsed_s": 0,
})
continue
result["cir_file"] = cir_file
result["log_file"] = log_file
result["elapsed_s"] = round(elapsed, 3)
# Monte Carlo tolerance analysis
if monte_carlo_n > 0 and result.get("status") != "skip":
from spice_tolerance import (
find_toleranceable_components, sample_detection,
aggregate_mc_results,
)
components = find_toleranceable_components(det)
if components:
mc_results_list = []
sampled_values_list = []
mc_cir = os.path.join(run_dir, f"{label}_mc.cir")
mc_out = os.path.join(run_dir, f"{label}_mc.out")
mc_log = os.path.join(run_dir, f"{label}_mc.log")
trial_sink = OnceSink(sim_diagnostics)
seeded = len(trial_sink)
for _trial in range(monte_carlo_n):
sampled_det = sample_detection(
det, components, mc_rng, mc_distribution,
det_type=det_type)
mc_result, _mc_elapsed = _run_single_sim(
sampled_det, det_type, generator, evaluator,
mc_cir, mc_out, mc_log, diagnostics=trial_sink)
if mc_result is not None:
mc_results_list.append(mc_result)
# Store sampled values for sensitivity analysis
sv = {}
for idx, (kp, _ct, _nom, _tol) in enumerate(components):
try:
from spice_tolerance import _get_nested
sv[idx] = _get_nested(sampled_det, kp)
except (KeyError, TypeError, IndexError):
pass
sampled_values_list.append(sv)
sim_diagnostics.extend(trial_sink[seeded:])
# Store original det for component ref lookup
result["_det"] = det
result["tolerance_analysis"] = aggregate_mc_results(
result, mc_results_list, components,
sampled_values_list,
_singular_type(det_type),
monte_carlo_n, mc_distribution, mc_seed,
det_type=det_type,
)
result.pop("_det", None)
# Clean up MC temp files
for f in (mc_cir, mc_out, mc_log):
if os.path.exists(f):
os.remove(f)
results.append(result)
# Process signal_analysis detections
for det_type in sim_types:
if det_type not in signal or det_type not in TEMPLATE_REGISTRY:
continue
detections = signal[det_type]
if not isinstance(detections, list):
continue
_run_detection_batch(det_type, detections,
TEMPLATE_REGISTRY[det_type],
EVALUATOR_REGISTRY.get(det_type))
# Process top-level types (not under signal_analysis)
for tl_type, (list_key, generator) in TOPLEVEL_REGISTRY.items():
if types and tl_type not in types:
continue
tl_data = analysis_json.get(tl_type, {})
if not tl_data:
continue
detections = tl_data.get(list_key, [])
if not isinstance(detections, list):
continue
_run_detection_batch(tl_type, detections,
generator, EVALUATOR_REGISTRY.get(tl_type))
report = build_report(results)
report["diagnostics"] = sim_diagnostics + report["diagnostics"]
report["workdir"] = run_dir
report["total_elapsed_s"] = round(total_time, 3)
report["simulator"] = find_ngspice() or None
report["ngspice"] = report["simulator"] # backward compat alias
return report
def _make_label(det_type, det, index):
"""Generate a filesystem-safe label for a subcircuit simulation."""
parts = []
# Singular form of detector type
parts.append(_singular_type(det_type).replace("_", "-"))
# Component references
comps = _get_components(det)
if comps:
parts.append("_".join(comps))
else:
parts.append(f"idx{index}")
label = "_".join(parts)
# Sanitize for filesystem
return "".join(c if c.isalnum() or c in "_-" else "_" for c in label)
def _get_components(det):
"""Extract component reference list from any detector dict."""
refs = []
# Try common field patterns from different detector types
for key in ("resistor", "r_top", "inductor", "shunt"):
if key in det and isinstance(det[key], dict) and "ref" in det[key]:
refs.append(det[key]["ref"])
for key in ("capacitor", "r_bottom"):
if key in det and isinstance(det[key], dict) and "ref" in det[key]:
refs.append(det[key]["ref"])
if "reference" in det:
refs.append(det["reference"])
# Feedback/input resistors for opamps
for key in ("feedback_resistor", "input_resistor"):
if key in det and isinstance(det[key], dict) and "ref" in det[key]:
refs.append(det[key]["ref"])
# Decoupling capacitor list
if "capacitors" in det and isinstance(det["capacitors"], list):
for c in det["capacitors"][:5]: # Limit to 5 for label length
if isinstance(c, dict) and "ref" in c:
refs.append(c["ref"])
# BMS: IC ref + balance resistor refs
if "bms_reference" in det:
refs.append(det["bms_reference"])
br_list = det.get("balance_resistors", [])
if not isinstance(br_list, list):
br_list = [] # Old format was int count, not list
for br in br_list[:3]:
if isinstance(br, dict) and "reference" in br:
refs.append(br["reference"])
# Snubber components
sd = det.get("snubber_data")
if isinstance(sd, dict):
if sd.get("resistor_ref"):
refs.append(sd["resistor_ref"])
if sd.get("capacitor_ref"):
refs.append(sd["capacitor_ref"])
# RF chain components
for cat in ("transceivers", "amplifiers", "filters", "switches", "mixers"):
for c in det.get(cat, [])[:2]:
if isinstance(c, dict) and "reference" in c:
refs.append(c["reference"])
# Power regulators: IC ref + feedback divider resistors
if "ref" in det and "feedback_divider" in det:
refs.append(det["ref"])
fd = det["feedback_divider"]
if isinstance(fd, dict):
for k in ("r_top", "r_bottom"):
v = fd.get(k)
if isinstance(v, dict) and "ref" in v:
refs.append(v["ref"])
# RF matching components
if "antenna" in det and "components" in det:
for c in det.get("components", [])[:5]:
if isinstance(c, dict) and "ref" in c:
refs.append(c["ref"])
return refs
def _summary_line(report: dict) -> str:
"""One-line stderr summary; tolerates reports without total_elapsed_s."""
s = report["summary"]
elapsed = report.get("total_elapsed_s") or 0.0
return (f"Simulation complete: {s['total']} subcircuits — "
f"{s['pass']} pass, {s['warn']} warn, {s['fail']} fail, "
f"{s['skip']} skip, {s.get('info', 0)} info ({elapsed:.1f}s)")
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(
description="Run SPICE simulations for detected subcircuits"
)
parser.add_argument(
"input",
nargs="?",
help="Path to analyzer JSON output (from analyze_schematic.py --output)",
)
parser.add_argument(
"--schematic", "-s",
help="Path to schematic analyzer JSON (alias for positional input)",
)
parser.add_argument(
"--analysis-dir",
help="Analysis cache directory. When set, auto-resolves schematic "
"from current run and writes spice.json into the run folder.",
)
add_output_arg(parser, help="Path to write simulation report JSON (default: stdout)")
parser.add_argument(
"--workdir", "-w",
help="Directory for simulation files (default: <run>/spice_work/ "
"when --analysis-dir is set, else a temp dir)",
)
parser.add_argument(
"--timeout", "-t",
type=int,
default=5,
help="Timeout per simulation in seconds (default: 5)",
)
parser.add_argument(
"--types",
help="Comma-separated list of detector types to simulate "
f"(default: all supported: {', '.join(list_supported_types())})",
)
add_compact_arg(parser, help="Omit file paths from output (for clean reports)")
add_text_arg(parser, help="Human-readable text output instead of JSON")
parser.add_argument(
"--parasitics",
help="Path to parasitics JSON (from extract_parasitics.py) for "
"PCB-aware simulation. When provided, RC-filter testbenches "
"include trace resistance and via inductance.",
)
parser.add_argument(
"--simulator",
choices=["auto", "ngspice", "ltspice", "xyce"],
default="auto",
help="SPICE simulator to use (default: auto-detect)",
)
parser.add_argument(
"--monte-carlo",
type=int,
default=0,
metavar="N",
help="Run N Monte Carlo tolerance simulations per subcircuit "
"(e.g., --monte-carlo 100)",
)
parser.add_argument(
"--mc-distribution",
choices=["gaussian", "uniform"],
default="gaussian",
help="Distribution for tolerance sampling (default: gaussian, "
"3-sigma = tolerance band)",
)
parser.add_argument(
"--mc-seed",
type=int,
default=42,
help="Random seed for reproducible Monte Carlo runs (default: 42)",
)
parser.add_argument(
"--model-cache-dir",
metavar="DIR",
help="Directory under which cached SPICE part models are kept as "
"DIR/spice/models/ (default: beside the schematic the analysis JSON names)",
)
parser.add_argument(
"--offline",
action="store_true",
help="Never query distributor APIs or datasheets for part models; use the "
"project cache and the built-in lookup table only",
)
args = parser.parse_args(argv)
if args.text:
utf8_text_stdout()
# Resolve input: --schematic, positional, or --analysis-dir auto-resolve
input_path = args.schematic or args.input
workdir = args.workdir
if args.analysis_dir:
# analysis_cache lives in skills/kicad/scripts/
from analysis_cache import get_current_run, MANIFEST_FILENAME
analysis_dir = os.path.abspath(args.analysis_dir)
current = get_current_run(analysis_dir)
if current is None:
print(f"Error: no 'current' run in {analysis_dir}/{MANIFEST_FILENAME}. "
f"Run analyze_schematic.py --analysis-dir first.",
file=sys.stderr)
return 1
run_dir = current[0]
if not input_path:
input_path = os.path.join(run_dir, 'schematic.json')
if not workdir:
workdir = os.path.join(run_dir, 'spice_work')
if not input_path:
parser.error("one of --schematic / positional input / --analysis-dir is required")
# Detect simulator
from spice_simulator import detect_simulator
simulator_backend = detect_simulator(args.simulator)
if not simulator_backend:
print("Error: no SPICE simulator found. Install one of:", file=sys.stderr)
print(" ngspice: apt install ngspice / brew install ngspice", file=sys.stderr)
print(" LTspice: download from analog.com/ltspice", file=sys.stderr)
print(" Xyce: download from xyce.sandia.gov", file=sys.stderr)
print(" Or set NGSPICE_PATH env var to the ngspice binary path.", file=sys.stderr)
return 1
# Read analysis JSON
try:
with open(input_path, "r", encoding="utf-8") as f:
analysis = json_load(f)
except (json.JSONDecodeError, UnicodeDecodeError, OSError) as e:
print(f"Error reading {input_path}: {e}", file=sys.stderr)
return 1
if not isinstance(analysis, dict):
print(f"Error reading {input_path}: top level is "
f"{type(analysis).__name__}, not an object", file=sys.stderr)
return 1
# Parse types filter
types = None
if args.types:
types = [t.strip() for t in args.types.split(",")]
supported = set(list_supported_types())
unknown = set(types) - supported
if unknown:
print(f"Warning: unknown types ignored: {', '.join(sorted(unknown))}",
file=sys.stderr)
types = [t for t in types if t in supported]
# Load parasitics if provided
parasitics_data = None
if args.parasitics:
try:
with open(args.parasitics, "r", encoding="utf-8") as f:
parasitics_data = json_load(f)
if not isinstance(parasitics_data, dict):
parasitics_data = None
raise ValueError("top level is not an object")
n_nets = len(parasitics_data.get("nets", {}))
print(f"Loaded parasitics for {n_nets} nets from {args.parasitics}",
file=sys.stderr)
except (json.JSONDecodeError, OSError, ValueError) as e:
print(f"Warning: could not load parasitics: {e}", file=sys.stderr)
from kicad_core.project import load_user_config
spice_config = load_user_config().get("spice")
trust_project_models = (isinstance(spice_config, dict)
and spice_config.get("trust_project_models") is True)
model_options = None
if args.model_cache_dir or args.offline or trust_project_models:
from spice_model_cache import ModelOptions
model_options = ModelOptions(cache_dir=args.model_cache_dir, offline=args.offline,
trust_project_models=trust_project_models)
if workdir and os.path.islink(workdir):
print(f"Error: workdir {workdir} is a symlink; refusing to write SPICE files "
f"through it.", file=sys.stderr)
return 1
# Run simulations
report = simulate_subcircuits(
analysis,
workdir=workdir,
timeout=args.timeout,
types=types,
parasitics=parasitics_data,
simulator_backend=simulator_backend,
monte_carlo_n=args.monte_carlo,
mc_distribution=args.mc_distribution,
mc_seed=args.mc_seed,
model_options=model_options,
)
# Clean up file paths if compact mode
if args.compact:
for r in report.get("simulation_results", []):
r.pop("cir_file", None)
r.pop("log_file", None)
report.pop("workdir", None)
# Analysis cache integration — write spice.json into the current run folder
# and update the manifest so downstream tools pick it up.
# No project-config resolution in this CLI, so the analysis-cache knobs keep their defaults.
if args.analysis_dir and not args.output:
from output_io import write_primary_run
write_primary_run(analysis_dir, json.dumps(report, indent=2), analyzer='spice', source_path=input_path,
script_name='simulate_subcircuits.py', script_entry='simulate_subcircuits.py',
record_project=False)
# Output
output_json = json.dumps(report, indent=2)
if args.text:
# --text matches the other analyzers. Early-exit
# reports (no simulatable data) omit total_elapsed_s — tolerate it.
s = report.get("summary", {})
_elapsed = report.get("total_elapsed_s")
_suffix = (f" ({_elapsed:.1f}s)"
if isinstance(_elapsed, (int, float)) else "")
print(f"SPICE simulation: {s.get('total', 0)} subcircuits — "
f"{s.get('pass', 0)} pass, {s.get('warn', 0)} warn, "
f"{s.get('fail', 0)} fail, {s.get('skip', 0)} skip, "
f"{s.get('info', 0)} info{_suffix}")
from output_filters import format_text
print(format_text(report.get("findings", []), "designer"))
if args.output:
atomic_write_text(args.output, output_json, follow_symlinks=False)
elif args.output:
atomic_write_text(args.output, output_json, follow_symlinks=False)
# Print summary to stderr
print(_summary_line(report), file=sys.stderr)
elif not args.analysis_dir:
print(output_json)
return 0
if __name__ == "__main__":
from kicad_core.cli import utf8_stdio
utf8_stdio()
sys.exit(main())
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation). | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK). | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |