Skip to content

CoupledInductor does not add prefix to inductor names #157

@mpea413

Description

@mpea413

Environment (OS, Python version, PySpice version, simulator)

Windows 7,
'3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)]'
'1.3.2'
ngspice

Description of the bug

Running a simple coupled inductor circuit, with an ac analysis

# Imports
import numpy as np
import matplotlib.pyplot as plt

import PySpice.Logging.Logging as Logging
logger = Logging.setup_logging()

from PySpice.Probe.Plot import plot
from PySpice.Spice.Netlist import Circuit
from PySpice.Unit import *

# Parameters
frequency = 1e3
period = 1 / frequency
omega = 2 * np.pi * frequency

I_P1 = 100
L_P1 = 1e-6
L_S1 = 10e-6
K_P1S1 = 0.1

circuit = Circuit("2CoupledInductors")

#Primary Side
circuit.I("I2", circuit.gnd, "N1", "AC " + str(I_P1) + "")
circuit.L("L_P1", circuit.gnd, "N1", str(L_P1))

# Secondary Side
circuit.L("L_S1", circuit.gnd,  "N2", str(L_S1) )
circuit.K("K_P1S1", "L_P1", "L_S1", K_P1S1)

# Do the simulation
simulator = circuit.simulator(temperature=25, nominal_temperature=25)
analysis = simulator.ac(variation="lin", number_of_points=1, start_frequency=frequency, stop_frequency=frequency)

# Print the results
print("--- Results ---")
for node in analysis.nodes.values():
  print('Node {}: {:5.2f} V'.format(str(node), float(abs(node))))

Raises a NameError('Invalid plot name') from _run in PySpice/Spice/NgSpice/Simulation.py. I think this is because the simulator failed to run (could update the exception with that fact).
I then printed the netlist by adding

print(str(self))

to that same _run method in PySpice/Spice/NgSpice/Simulation.py

The netlist that was produced is

.title 2CoupledInductors
II2 0 N1 AC 100
LL_P1 0 N1 1e-06
LL_S1 0 N2 1e-05
KK_P1S1 L_P1 L_S1 0.1
.options TEMP = 25°C
.options TNOM = 25°C
.ic 
.ac lin 1 1000.0Hz 1000.0Hz
.end

Which highlights that the error is because the inductors are defined with an extra L prefix in their definitions, but this prefix is not added to the inductors in the CoupledInductor command.

The code runs as expected when this change is made

# Imports
import numpy as np
import matplotlib.pyplot as plt

import PySpice.Logging.Logging as Logging
logger = Logging.setup_logging()

from PySpice.Probe.Plot import plot
from PySpice.Spice.Netlist import Circuit
from PySpice.Unit import *

# Parameters
frequency = 1e3
period = 1 / frequency
omega = 2 * np.pi * frequency

I_P1 = 100
L_P1 = 1e-6
L_S1 = 10e-6
K_P1S1 = 0.1

circuit = Circuit("2CoupledInductors")

#Primary Side
circuit.I("I2", circuit.gnd, "N1", "AC " + str(I_P1) + "")
circuit.L("L_P1", circuit.gnd, "N1", str(L_P1))

# Secondary Side
circuit.L("L_S1", circuit.gnd,  "N2", str(L_S1) )
circuit.K("K_P1S1", "LL_P1", "LL_S1", K_P1S1) # NB, it adds an L to the name of the inductor ... 

# Do the simulation
simulator = circuit.simulator(temperature=25, nominal_temperature=25)
analysis = simulator.ac(variation="lin", number_of_points=1, start_frequency=frequency, stop_frequency=frequency)

# Print the results
print("--- Results ---")
for node in analysis.nodes.values():
  print('Node {}: {:5.2f} V'.format(str(node), float(abs(node))))

prints the expected results

--- Results ---
Node n2:  0.20 V
Node n1:  0.63 V

Suggestions

It seems like perhaps the CoupledInductor method should automatically add the L prefix to the passed inductor names, to match with the Inductor method behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions