Skip to content

Commit

Permalink
complete unit example
Browse files Browse the repository at this point in the history
  • Loading branch information
FabriceSalvaire committed Jan 26, 2024
1 parent 4f5fb76 commit d173890
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions examples/basic-usages/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,44 @@

####################################################################################################

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

####################################################################################################

from PySpice import Circuit
from PySpice import Circuit, Simulator

####################################################################################################

from PySpice.Unit import *
# pylint: disable=no-name-in-module
from PySpice.Unit import (
kilo,
as_Ω, u_kOhm, U_Ω, u_kΩ,
u_Degree, u_Hz, u_mA, u_mH, u_ms, u_uF, u_V,
)
# pylint: enable=no-name-in-module

foo = kilo(1) # unit less
foo = kilo(1) # unit less

resistance_unit = U_Ω

resistance1 = u_kΩ(1)
resistance1 = u_kOhm(1) # ASCII variant
resistance1 = u_kOhm(1) # ASCII variant

resistance1 = 1@u_kΩ # using Python 3.5 syntax
resistance1 = 1 @u_kΩ # space doesn't matter
resistance1 = 1 @ u_kΩ #
resistance1 = 1 @ u_kΩ

resistance2 = as_Ω(resistance1) # check unit
resistance2 = as_Ω(resistance1) # check unit

resistances = u_kΩ(range(1, 11)) # same as [u_kΩ(x) for x in range(1, 11)]
resistances = u_kΩ(range(1, 11)) # same as [u_kΩ(x) for x in range(1, 11)]
resistances = range(1, 11)@u_kΩ # using Python 3.5 syntax

capacitance = u_uF(200)
inductance = u_mH(1)
temperature = u_Degree(25)

voltage = resistance1 * u_mA(1) # compute unit
voltage = resistance1 * u_mA(1) # compute unit

frequency = u_ms(20).frequency
period = u_Hz(50).period
Expand Down Expand Up @@ -78,9 +84,24 @@

circuit.V('input', 1, circuit.gnd, 10*u.V)
circuit.R(1, 1, 2, 2*u.)
circuit.R(2, 1, 3, 1*u.)
circuit.R(3, 2, circuit.gnd, 1*u.)
circuit.R(2, 1, 3, 1*u.)
circuit.R(3, 2, circuit.gnd, 1*u.)
circuit.R(4, 3, circuit.gnd, 2*u.)
circuit.R(5, 3, 2, 2*u.)

print(circuit)

simulator = Simulator.factory()
simulation = simulator.simulation(circuit, temperature=25, nominal_temperature=25)
analysis = simulation.operating_point()

for node in analysis.nodes.values():
# Fixme: format value + unit
# Fixme: DeprecationWarning: Conversion of an arraywith ndim > 0
# to a scalar is deprecated, and will error in future. Ensure you
# extract a single element from your array before performing this
# operation. (Deprecated NumPy 1.25.)
# _ = float(node)
_ = float(node[0])
print(f'Node {node}: {_:4.1f} V')
#o#

0 comments on commit d173890

Please sign in to comment.