Skip to content

Commit d173890

Browse files
complete unit example
1 parent 4f5fb76 commit d173890

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

examples/basic-usages/unit.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,44 @@
22

33
####################################################################################################
44

5-
import PySpice.Logging.Logging as Logging
5+
from PySpice.Logging import Logging
66
logger = Logging.setup_logging()
77

88
####################################################################################################
99

10-
from PySpice import Circuit
10+
from PySpice import Circuit, Simulator
1111

1212
####################################################################################################
1313

14-
from PySpice.Unit import *
14+
# pylint: disable=no-name-in-module
15+
from PySpice.Unit import (
16+
kilo,
17+
as_Ω, u_kOhm, U_Ω, u_kΩ,
18+
u_Degree, u_Hz, u_mA, u_mH, u_ms, u_uF, u_V,
19+
)
20+
# pylint: enable=no-name-in-module
1521

16-
foo = kilo(1) # unit less
22+
foo = kilo(1) # unit less
1723

1824
resistance_unit = U_Ω
1925

2026
resistance1 = u_kΩ(1)
21-
resistance1 = u_kOhm(1) # ASCII variant
27+
resistance1 = u_kOhm(1) # ASCII variant
2228

2329
resistance1 = 1@u_kΩ # using Python 3.5 syntax
2430
resistance1 = 1 @u_kΩ # space doesn't matter
25-
resistance1 = 1 @ u_kΩ #
31+
resistance1 = 1 @ u_kΩ
2632

27-
resistance2 = as_Ω(resistance1) # check unit
33+
resistance2 = as_Ω(resistance1) # check unit
2834

29-
resistances = u_kΩ(range(1, 11)) # same as [u_kΩ(x) for x in range(1, 11)]
35+
resistances = u_kΩ(range(1, 11)) # same as [u_kΩ(x) for x in range(1, 11)]
3036
resistances = range(1, 11)@u_kΩ # using Python 3.5 syntax
3137

3238
capacitance = u_uF(200)
3339
inductance = u_mH(1)
3440
temperature = u_Degree(25)
3541

36-
voltage = resistance1 * u_mA(1) # compute unit
42+
voltage = resistance1 * u_mA(1) # compute unit
3743

3844
frequency = u_ms(20).frequency
3945
period = u_Hz(50).period
@@ -78,9 +84,24 @@
7884

7985
circuit.V('input', 1, circuit.gnd, 10*u.V)
8086
circuit.R(1, 1, 2, 2*u.)
81-
circuit.R(2, 1, 3, 1*u.)
82-
circuit.R(3, 2, circuit.gnd, 1*u.)
87+
circuit.R(2, 1, 3, 1*u.)
88+
circuit.R(3, 2, circuit.gnd, 1*u.)
8389
circuit.R(4, 3, circuit.gnd, 2*u.)
8490
circuit.R(5, 3, 2, 2*u.)
8591

8692
print(circuit)
93+
94+
simulator = Simulator.factory()
95+
simulation = simulator.simulation(circuit, temperature=25, nominal_temperature=25)
96+
analysis = simulation.operating_point()
97+
98+
for node in analysis.nodes.values():
99+
# Fixme: format value + unit
100+
# Fixme: DeprecationWarning: Conversion of an arraywith ndim > 0
101+
# to a scalar is deprecated, and will error in future. Ensure you
102+
# extract a single element from your array before performing this
103+
# operation. (Deprecated NumPy 1.25.)
104+
# _ = float(node)
105+
_ = float(node[0])
106+
print(f'Node {node}: {_:4.1f} V')
107+
#o#

0 commit comments

Comments
 (0)