SpiceCircuit

class PyLTSpice.editor.spice_editor.SpiceCircuit[source]

Bases: BaseEditor

The Spice Circuit represents sub-circuits within a SPICE circuit and since sub-circuits can have sub-circuits inside them, it serves as base for the top level netlist. See class SpiceEditor This hierarchical approach helps to encapsulate and protect parameters and components from edits made at a higher level.

The netlist information is stored in a list, each element of the list corresponds to a SPICE instruction. If an instruction spawns more than a line with the ‘+’ operator, it is contained in the same element.

This class serves as subclass to the SpiceEditor class.

add_instruction(instruction: str) None[source]

Serves to add SPICE instructions to the simulation netlist. For example:

.tran 10m ; makes a transient simulation
.meas TRAN Icurr AVG I(Rs1) TRIG time=1.5ms TARG time=2.5ms" ; Establishes a measuring
.step run 1 100, 1 ; makes the simulation run 100 times
Parameters:

instruction (str) – Spice instruction to add to the netlist. This instruction will be added at the end of the netlist, typically just before the .BACKANNO statement

Returns:

Nothing

static add_library_search_paths(paths: Union[str, List[str]]) None[source]

Adding search paths for libraries. By default, the local directory and the ~username/”Documents/LTspiceXVII/lib/sub will be searched forehand. Only when a library is not found in these paths then the paths added by this method will be searched. Alternatively PyLTSpice.SpiceEditor.LibSearchPaths.append(paths) can be used.”

Parameters:

paths (str) – Path to add to the Search path

Returns:

Nothing

Return type:

None

property circuit_file: Path

This is only here to avoid breaking compatibility with the BaseEditor superclass. It will always return ‘’

clone(**kwargs) SpiceCircuit[source]

Creates a new copy of the SpiceCircuit. Change done at the new copy are not affecting the original

Key new_name:

The new name to be given to the circuit

Key type new_name:

str

Returns:

The new replica of the SpiceCircuit object

Return type:

SpiceCircuit

get_all_nodes() List[str][source]

A function that retrieves all nodes existing on a Netlist

Returns:

Circuit Nodes

Return type:

list[str]

get_component_info(component) dict[source]

Retrieves the component information as defined in the corresponding REGEX. The line number is also added.

Parameters:

component (str) – Reference of the component

Returns:

Dictionary with the component information

Return type:

dict

Raises:

UnrecognizedSyntaxError when the line doesn’t match the expected REGEX. NotImplementedError of there isn’t an associated regular expression for the component prefix.

get_component_nodes(element: str) List[str][source]

Returns the nodes to which the component is attached to.

Parameters:

element (str) – Reference of the circuit element to get the nodes.

Returns:

List of nodes

Return type:

list

get_component_value(element: str) str[source]

Returns the value of a component retrieved from the netlist.

Parameters:

element (str) – Reference of the circuit element to get the value.

Returns:

value of the circuit element .

Return type:

str

Raises:

ComponentNotFoundError - In case the component is not found

NotImplementedError - for not supported operations

get_components(prefixes='*') list[source]

Returns a list of components that match the list of prefixes indicated on the parameter prefixes. In case prefixes is left empty, it returns all the ones that are defined by the REPLACE_REGEXES. The list will contain the designators of all components found.

Parameters:

prefixes (str) – Type of prefixes to search for. Examples: ‘C’ for capacitors; ‘R’ for Resistors; etc… See prefixes in SPICE documentation for more details. The default prefix is ‘*’ which is a special case that returns all components.

Returns:

A list of components matching the prefixes demanded.

get_parameter(param: str) str[source]

Retrieves a Parameter from the Netlist

Parameters:

param (str) – Name of the parameter to be retrieved

Returns:

Value of the parameter being sought

Return type:

str

Raises:

ParameterNotFoundError - In case the component is not found

name() str[source]

Returns the name of the Sub-Circuit -> str.

Return type:

str

remove_component(designator: str) None[source]

Removes a component from the design. Note: Current implementation only allows removal of a component from the main netlist, not from a sub-circuit.

Parameters:

designator (str) – Component reference in the design. Ex: V1, C1, R1, etc…

Returns:

Nothing

Raises:

ComponentNotFoundError - When the component doesn’t exist on the netlist.

remove_instruction(instruction: str) None[source]

Usage a previously added instructions. Example:

LTC.remove_instruction(".STEP run -1 1023 1")

This only works if the instruction exactly matches the line on the netlist. This means that space characters, and upper case and lower case differences will not match the line.

Parameters:

instruction (str) – The list of instructions to remove. Each instruction is of the type ‘str’

Returns:

Nothing

reset_netlist() None[source]

Resets the netlist to the original state

set_component_value(device: str, value: Union[str, int, float]) None[source]

Changes the value of a component, such as a Resistor, Capacitor or Inductor. For components inside sub-circuits, use the sub-circuit designator prefix with ‘:’ as separator (Example X1:R1) Usage:

LTC.set_component_value('R1', '3.3k')
LTC.set_component_value('X1:C1', '10u')
Parameters:
  • device (str) – Reference of the circuit element to be updated.

  • value (str, int or float) – value to be set on the given circuit element. Float and integer values will be automatically formatted as per the engineering notations ‘k’ for kilo, ‘m’, for mili and so on.

Raises:

ComponentNotFoundError - In case the component is not found

ValueError - In case the value doesn’t correspond to the expected format

NotImplementedError - In case the circuit element is defined in a format which is not supported by this version.

If this is the case, use GitHub to start a ticket. https://github.com/nunobrum/PyLTSpice

set_element_model(element: str, model: str) None[source]

Changes the value of a circuit element, such as a diode model or a voltage supply. Usage:

LTC.set_element_model('D1', '1N4148')
LTC.set_element_model('V1' "SINE(0 1 3k 0 0 0)")
Parameters:
  • element (str) – Reference of the circuit element to be updated.

  • model (str) – model name of the device to be updated

Raises:

ComponentNotFoundError - In case the component is not found

ValueError - In case the model format contains irregular characters

NotImplementedError - In case the circuit element is defined in a format which is not supported by this version.

If this is the case, use GitHub to start a ticket. https://github.com/nunobrum/PyLTSpice

set_parameter(param: str, value: Union[str, int, float]) None[source]

Adds a parameter to the SPICE netlist.

Usage:

LTC.set_parameter("TEMP", 80)

This adds onto the netlist the following line:

.PARAM TEMP=80

This is an alternative to the set_parameters which is more pythonic in it’s usage, and allows setting more than one parameter at once.

Parameters:
  • param (str) – Spice Parameter name to be added or updated.

  • value (str, int or float) – Parameter Value to be set.

Returns:

Nothing

setname(new_name: str)[source]

Renames the sub-circuit to a new name. No check is done to the new game give. It is up to the user to make sure that the new name is valid.

Parameters:

new_name (str) – The new Name.

Returns:

Nothing

Return type:

None

write_lines(f)[source]

Internal function. Do not use.

write_netlist(run_netlist_file: Union[str, Path]) None[source]

Writes the netlist to a file