Simulators

class spicelib.sim.simulator.Simulator[source]

Bases: ABC

Pure static class template for Spice simulators. This class only defines the interface of the subclasses. The variables below shall be overridden by the subclasses. Instantiating this class will raise a SpiceSimulatorError exception.

A typical subclass for a Windows installation is:

class MySpiceWindowsInstallation(Simulator):
    spice_exe = ['<path to the spice executable>']
    process_name = "<name of the process on Windows Task Manager>"

or on a Linux distribution:

class MySpiceLinuxInstallation(Simulator):
    spice_exe = ['<wine_command>', '<path to the spice executable>']
    process_name = "<name of the process within the system>"

If you use MacOS, you can choose either one of the 2 above. If you are on Intel, running LTSpice under wine (therefore: like under Linux) is preferred.

The subclasses should then implement at least the run() function as a classmethod.

@classmethod
def run(cls, netlist_file: str | Path, cmd_line_switches: list | None = None, timeout: float | None = None,
    stdout=None, stderr=None, cwd: str | Path | None = None, exe_log: bool = False) -> int:
    '''This method implements the call for the simulation of the netlist file. '''
    cmd_run = cls.spice_exe + ['-Run'] + ['-b'] + [netlist_file] + cmd_line_switches
    return run_function(cmd_run, timeout=timeout, stdout=stdout, stderr=stderr, cwd=cwd)

The run_function() can be imported from the simulator.py with from spicelib.sim.simulator import run_function instruction.

classmethod create_from(path_to_exe, process_name=None)[source]

Creates a simulator class from a path to the simulator executable

Parameters:
  • path_to_exe (pathlib.Path or str. If it is a string, it supports multiple sections, allowing loaders like wine, but MUST be in posix format in that case, and the last section MUST be the simulator executable.)

  • process_name – the process_name to be used for killing phantom processes. If not provided, it will be

Returns:

a class instance representing the Spice simulator

Return type:

Simulator

static expand_and_check_local_dir(path, exe_path=None)[source]

Expands a directory path to become an absolute path, while taking into account a potential use under wine (under MacOS and Linux). Will also check if that directory exists. The path must either be an absolute path or start with ~. Relative paths are not supported. On MacOS or Linux, it will try to replace any reference to the virtual windows root under wine into a host OS path.

Examples:

  • under windows:

    • C:/mydir -> C:/mydir

    • ~/mydir -> C:/Users/myuser/mydir

  • under linux, and if the executable is /mywineroot/.wine/drive_c/(something):

    • C:/mydir -> /mywineroot/.wine/drive_c/mydir

    • ~/mydir -> /mywineroot/.wine/drive_c/users/myuser/mydir

Parameters:
  • path (str) – The path to expand. Must be in posix format, use PureWindowsPath(path).as_posix() to transform a windows path to a posix path.

  • exe_path (str | None) – path to a related executable that may or may not be under wine, defaults to None, ignored on Windows

Returns:

the fully expanded path, as posix path, will return None if the path does not exist.

Return type:

str | None

classmethod get_default_library_paths()[source]

Return the directories that contain the standard simulator’s libraries, as derived from the simulator’s executable path and platform. spice_exe must be set before calling this method.

This is companion with set_custom_library_paths()

Returns:

the list of paths where the libraries should be located.

Return type:

list[str]

static guess_process_name(exe)[source]

Guess the process name based on the executable path

classmethod is_available()[source]

This method checks if the simulator exists in the system. It will return a boolean value indicating if the simulator is installed or not.

process_name: str

the name of the process in the task manager

abstract classmethod run(netlist_file, cmd_line_switches=None, timeout=None, stdout=None, stderr=None, cwd=None, exe_log=False)[source]

This method implements the call for the simulation of the netlist file. This should be overriden by its subclass.

spice_exe: list[str]

The executable. If using a loader (like wine), make sure that the last in the array is the real simulator.

abstract classmethod valid_switch(switch, switch_param)[source]

This method validates that a switch exist and is valid. This should be overriden by its subclass.

LTSpice

class PyLTSpice.sim.ltspice_simulator.LTspice[source]

Bases: Simulator

Stores the simulator location and command line options and is responsible for generating netlists and running simulations.

classmethod create_netlist(circuit_file, cmd_line_switches=None, timeout=None, stdout=None, stderr=None, cwd=None, exe_log=False)[source]

Create a netlist out of the circuit file

Parameters:
  • circuit_file (str | Path) – path to the circuit file

  • cmd_line_switches (list | None) – additional command line options. Best to have been validated by valid_switch(), defaults to None

  • timeout (float | None) – If timeout is given, and the process takes too long, a TimeoutExpired exception will be raised, defaults to None

  • stdout (_FILE, optional) – control redirection of the command’s stdout. Valid values are None, subprocess.PIPE, subprocess.DEVNULL, an existing file descriptor (a positive integer), and an existing file object with a valid file descriptor. With the default settings of None, no redirection will occur. Also see exe_log for a simpler form of control.

  • stderr (_FILE, optional) – Like stdout, but affecting the command’s error output. Also see exe_log for a simpler form of control.

  • cwd (str | Path | None) – The current working directory to run the command in. If None, no change will be done of the working directory. This may not work as wanted when using the simulator under wine.

  • exe_log (bool) – If True, stdout and stderr will be ignored, and the simulator’s execution console messages will be written to a log file (named …exe.log) instead of console. This is especially useful when running under wine or when running simultaneous tasks.

Raises:
  • NotImplementedError – when the requested execution is not possible on this platform.

  • RuntimeError – when the netlist cannot be created

Returns:

path to the netlist produced

Return type:

Path

process_name: str

the name of the process in the task manager

classmethod run(netlist_file, cmd_line_switches=None, timeout=None, stdout=None, stderr=None, cwd=None, exe_log=False)[source]

Executes a LTspice simulation run.

A raw file and a log file will be generated, with the same name as the netlist file, but with .raw and .log extension.

Parameters:
  • netlist_file (str | Path) – path to the netlist file

  • cmd_line_switches (list | None) – additional command line options. Best to have been validated by valid_switch(), defaults to None

  • timeout (float | None) – If timeout is given, and the process takes too long, a TimeoutExpired exception will be raised, defaults to None

  • stdout (_FILE, optional) – control redirection of the command’s stdout. Valid values are None, subprocess.PIPE, subprocess.DEVNULL, an existing file descriptor (a positive integer), and an existing file object with a valid file descriptor. With the default settings of None, no redirection will occur. Also see exe_log for a simpler form of control.

  • stderr (_FILE, optional) – Like stdout, but affecting the command’s error output. Also see exe_log for a simpler form of control.

  • cwd (str | Path | None) – The current working directory to run the command in. If None, no change will be done of the working directory. This may not work as wanted when using the simulator under wine.

  • exe_log (bool) – If True, stdout and stderr will be ignored, and the simulator’s execution console messages will be written to a log file (named …exe.log) instead of console. This is especially useful when running under wine or when running simultaneous tasks.

Raises:
  • SpiceSimulatorError – when the executable is not found.

  • NotImplementedError – when the requested execution is not possible on this platform.

Returns:

return code from the process

Return type:

int

spice_exe: list[str]

The executable. If using a loader (like wine), make sure that the last in the array is the real simulator.

classmethod using_macos_native_sim()[source]

Tells if the simulator used is the macOS native LTspice

Returns:

True if the macOS native LTspice is used, False otherwise (will also return False on Windows or Linux)

Return type:

bool

classmethod valid_switch(switch, path='')[source]

Validates a command line switch. The following options are available for Windows/wine LTspice:

  • -alt: Set solver to Alternate.

  • -ascii: Use ASCII.raw files. Seriously degrades program performance.

  • -encrypt: Encrypt a model library.For 3rd parties wishing to allow people to use libraries without revealing implementation details. Not used by AnalogDevices models.

  • -fastaccess: Batch conversion of a binary.rawfile to Fast Access format.

  • -FixUpSchematicFonts: Convert the font size field of very old user - authored schematic text to the modern default.

  • -FixUpSymbolFonts: Convert the font size field of very old user - authored symbols to the modern default. See Changelog.txt for application hints.

  • -ini <path>: Specify an .ini file to use other than %APPDATA%LTspice.ini

  • -I<path>: Specify a path to insert in the symbol and file search paths. Must be the last specified option.

  • -netlist: Batch conversion of a schematic to a netlist.

  • -normal: Set solver to Normal.

  • -PCBnetlistBatch: Conversion of a schematic to a PCB format netlist.

  • -SOI: Allow MOSFET’s to have up to 7 nodes even in subcircuit expansion.

  • -sync: Update component libraries

The following parameters will already be filled in by spicelib, and cannot be set:

  • -Run: Start simulating the schematic opened on the command line without pressing the Run button.

  • -b: Run in batch mode.

macOS native LTspice accepts no command line switches (yet), use it under wine for full support.

Parameters:
  • switch (str) – switch to be added.

  • path (str) – path to the file related to the switch being given.

Returns:

Nothing

Return type:

list