You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

85 lines
2.3 KiB

"""
pyngspice - Python bindings for ngspice circuit simulator.
Provides embedded ngspice simulation with a SpiceRunner interface
compatible with pyTesla. Can serve as a drop-in alternative to LTspice
for cross-platform Tesla coil simulation.
Basic usage:
from pyngspice import NgspiceRunner, RawRead
runner = NgspiceRunner(working_directory="./output")
raw_file, log_file = runner.run("circuit.net")
raw = RawRead(raw_file)
trace = raw.get_trace("V(out)")
data = trace.get_wave(0) # numpy array
Low-level usage (PyLTSpice-compatible):
from pyngspice import SimRunner, RawRead
runner = SimRunner(output_folder="./output")
raw_file, log_file = runner.run_now("circuit.net")
"""
__version__ = "43.0.0"
__author__ = "ngspice team"
# On Windows with Python 3.8+, add MinGW DLL directory for dependent DLLs
import sys
import os
if sys.platform == 'win32' and hasattr(os, 'add_dll_directory'):
# MinGW runtime DLLs (libgcc_s_seh-1.dll, libstdc++-6.dll, libwinpthread-1.dll)
mingw_bin = r'C:\mingw64\bin'
if os.path.isdir(mingw_bin):
os.add_dll_directory(mingw_bin)
# Import the compiled C++ extension module
try:
from ._pyngspice import (
Simulator,
SimRunner,
RawRead,
Trace,
NgSpice,
)
_cpp_available = True
except ImportError as e:
_cpp_available = False
_cpp_error = str(e)
# Provide helpful error message
import warnings
warnings.warn(
f"Failed to import pyngspice C++ extension: {e}\n"
f"The embedded ngspice runner will not be available.\n"
f"Try: pip install -e . (from the ngspice source directory)",
ImportWarning,
stacklevel=2,
)
# Define stubs so imports don't fail
Simulator = None
SimRunner = None
RawRead = None
Trace = None
NgSpice = None
# Import Python-level API
from .runner import SpiceRunner, NgspiceRunner, SubprocessRunner, SimulationError
from .netlist import preprocess_netlist
__all__ = [
# High-level API (what pyTesla uses)
"SpiceRunner",
"NgspiceRunner",
"SubprocessRunner",
"SimulationError",
"preprocess_netlist",
# Low-level C++ bindings
"Simulator",
"SimRunner",
"RawRead",
"Trace",
"NgSpice",
# State
"_cpp_available",
]