Skip to content

Commit 7595881

Browse files
fix #157
1 parent d5fed48 commit 7595881

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

PySpice/Spice/BasicElement.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@
9797

9898
####################################################################################################
9999

100+
import logging
101+
100102
from ..Tools.StringTools import str_spice, join_list, join_dict
101103
from ..Unit import U_m, U_s, U_A, U_V, U_Degree, U_Ω, U_F, U_H, U_Hz
102104
from .Netlist import (Element, AnyPinElement, FixedPinElement, NPinElement, OptionalPin)
@@ -118,6 +120,10 @@
118120

119121
####################################################################################################
120122

123+
_module_logger = logging.getLogger(__name__)
124+
125+
####################################################################################################
126+
121127
class DipoleElement(FixedPinElement):
122128
"""This class implements a base class for dipole element."""
123129
__pins__ = ('plus', 'minus')
@@ -656,13 +662,28 @@ class CoupledInductor(AnyPinElement):
656662
inductor2 = ElementNamePositionalParameter(position=1, key_parameter=False)
657663
coupling_factor = FloatPositionalParameter(position=2, key_parameter=False)
658664

665+
_logger = _module_logger.getChild('CoupledInductor')
666+
659667
##############################################
660668

661669
def __init__(self, name, *args, **kwargs):
662670

663671
super().__init__(name, *args, **kwargs)
664672

665-
self._inductors = (self.inductor1, self.inductor2)
673+
self._inductors = []
674+
for inductor in (self.inductor1, self.inductor2):
675+
try:
676+
self.netlist.element(inductor)
677+
except KeyError:
678+
try:
679+
inductor = 'L' + inductor
680+
self.netlist.element(inductor)
681+
self._logger.info('Prefixed element {}'.format(inductor))
682+
except KeyError:
683+
raise ValueError('Element with name {} not found'.format(inductor))
684+
# Fixme: str or Element instance ?
685+
self._inductors.append(inductor)
686+
self.inductor1, self.inductor2 = self._inductors
666687

667688
####################################################################################################
668689

PySpice/Spice/Netlist.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,15 +895,26 @@ def subcircuit_names(self):
895895

896896
##############################################
897897

898+
def element(self, name):
899+
return self._elements[name]
900+
901+
def model(self, name):
902+
return self._models[name]
903+
904+
def node(self, name):
905+
return self._nodes[name]
906+
907+
##############################################
908+
898909
def __getitem__(self, attribute_name):
899910

900911
if attribute_name in self._elements:
901-
return self._elements[attribute_name]
912+
return self.element(attribute_name)
902913
elif attribute_name in self._models:
903-
return self._models[attribute_name]
914+
return self.model(attribute_name)
904915
# Fixme: subcircuits
905916
elif attribute_name in self._nodes:
906-
return self._nodes[attribute_name]
917+
return self.node(attribute_name)
907918
else:
908919
raise IndexError(attribute_name) # KeyError
909920

issues/issue-157-2.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,23 @@
1818
L_S1 = 10e-6
1919
K_P1S1 = 0.1
2020

21-
circuit = Circuit("2CoupledInductors")
21+
circuit = Circuit('2CoupledInductors')
2222

2323
#Primary Side
24-
circuit.I("I2", circuit.gnd, "N1", "AC " + str(I_P1) + "")
25-
circuit.L("L_P1", circuit.gnd, "N1", str(L_P1))
24+
circuit.I('I2', circuit.gnd, 'N1', 'AC ' + str(I_P1) + '')
25+
circuit.L('L_P1', circuit.gnd, 'N1', str(L_P1))
2626

2727
# Secondary Side
28-
circuit.L("L_S1", circuit.gnd, "N2", str(L_S1) )
29-
circuit.K("K_P1S1", "LL_P1", "LL_S1", K_P1S1) # NB, it adds an L to the name of the inductor ...
28+
circuit.L('L_S1', circuit.gnd, 'N2', str(L_S1) )
29+
circuit.K('K_P1S1', 'LL_P1', 'LL_S1', K_P1S1) # NB, it adds an L to the name of the inductor ...
3030

31-
# Do the simulation
32-
simulator = circuit.simulator(temperature=25, nominal_temperature=25)
33-
analysis = simulator.ac(variation="lin", number_of_points=1, start_frequency=frequency, stop_frequency=frequency)
31+
print(circuit)
3432

35-
# Print the results
36-
print("--- Results ---")
37-
for node in analysis.nodes.values():
38-
print('Node {}: {:5.2f} V'.format(str(node), float(abs(node))))
33+
# # Do the simulation
34+
# simulator = circuit.simulator(temperature=25, nominal_temperature=25)
35+
# analysis = simulator.ac(variation='lin', number_of_points=1, start_frequency=frequency, stop_frequency=frequency)
36+
37+
# # Print the results
38+
# print('--- Results ---')
39+
# for node in analysis.nodes.values():
40+
# print('Node {}: {:5.2f} V'.format(str(node), float(abs(node))))

issues/issue-157.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,25 @@
1818
L_S1 = 10e-6
1919
K_P1S1 = 0.1
2020

21-
circuit = Circuit("2CoupledInductors")
21+
circuit = Circuit('2CoupledInductors')
2222

2323
#Primary Side
24-
circuit.I("I2", circuit.gnd, "N1", "AC " + str(I_P1) + "")
25-
circuit.L("L_P1", circuit.gnd, "N1", str(L_P1))
24+
circuit.I('I2', circuit.gnd, 'N1', 'AC ' + str(I_P1) + '')
25+
circuit.L('L_P1', circuit.gnd, 'N1', str(L_P1))
2626

2727
# Secondary Side
28-
circuit.L("L_S1", circuit.gnd, "N2", str(L_S1) )
29-
circuit.K("K_P1S1", "L_P1", "L_S1", K_P1S1)
28+
circuit.L('L_S1', circuit.gnd, 'N2', str(L_S1) )
29+
circuit.K('K_P1S1', 'L_P1', 'L_S1', K_P1S1)
30+
# circuit.K('K_P1S1', 'L_P1', 'LL_S1', K_P1S1)
31+
# circuit.K('K_P1S1', 'LL_P1', 'LL_S1', K_P1S1)
32+
33+
print(circuit)
3034

3135
# Do the simulation
3236
simulator = circuit.simulator(temperature=25, nominal_temperature=25)
33-
analysis = simulator.ac(variation="lin", number_of_points=1, start_frequency=frequency, stop_frequency=frequency)
37+
analysis = simulator.ac(variation='lin', number_of_points=1, start_frequency=frequency, stop_frequency=frequency)
3438

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

0 commit comments

Comments
 (0)