Skip to content

Commit

Permalink
Merge branch 'master' into fixI
Browse files Browse the repository at this point in the history
  • Loading branch information
mph- committed Nov 15, 2024
2 parents 62cad23 + c085481 commit e9269d1
Show file tree
Hide file tree
Showing 68 changed files with 628 additions and 225 deletions.
18 changes: 2 additions & 16 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ causality since both parts lumped together.

39. Avoid pole-zero cancellation for a transfer function derived from a differential or difference equation.

40. Defer evaluation of twoports.

Testing
=======
Expand Down Expand Up @@ -178,22 +179,7 @@ Documentation
Additional features
===================

1. Handle mechanical and acoustical components. Perhaps use lower case for
mechanical/acoustical. For example,

Force f (N)
Mass m (kg)
Spring k (N/m)
Damper r (kg/s)
Velocity u (m/s)

Could support both electro-mechnical analogues? cct.electrical_analogue(1)?
m -> L, C
k -> C, L
f -> I, V
u -> V, I

2. Expand two-ports, xtals, opamps, etc. cct = cct.expand() Perhaps
1. Expand two-ports, xtals, opamps, etc. cct = cct.expand() Perhaps
have arg to expand to select components and/or types of component to
expand?

Expand Down
5 changes: 5 additions & 0 deletions doc/examples/netlists/tf2-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from lcapy import Circuit

a = Circuit('tf2.sch')

H1 = a.transfer('V1', 'R3')
5 changes: 5 additions & 0 deletions doc/examples/netlists/tf2-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from lcapy import Circuit

a = Circuit('tf2.sch')

H2 = a.transfer('V2', 'R3')
Binary file added doc/examples/netlists/tf2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions doc/examples/netlists/tf2.sch
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
R1 3 1; down
W1 3 4; right
R2 4 5; down
W2 2 6; right
W3 4 8; right
R3 8 9; down=2
W4 6 9; right
V2 5 6 ac; down
V1 1 2 ac; down
; draw_nodes=connections, label_nodes=none, style=american, voltage_dir=RP, label_style=value
6 changes: 6 additions & 0 deletions doc/examples/schematics/frankenstein.sch
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
R 1 2; right
L 2 3; right
I 3 4; right
V 4 5; right
; label_nodes=false, draw_nodes=connections, resistor_style=european
; inductor_style=american, voltage_source_style=american, current_source_style=european
Binary file modified doc/examples/schematics/inductors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions doc/examples/schematics/inductors.sch
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ L3 3 4; right, kind=tunable, l=tunable
L4 4 5; right, kind=sensor, l=sensor
L5 5 6; right, kind=choke, l=choke
L6 6 7; right, kind=twolineschoke, l=twolineschoke
L7 7 8; right, kind=american, l=american
L8 8 9; right, kind=european, l=european
; label_nodes=false, draw_nodes=connections
Binary file added doc/examples/schematics/label_value_style_eng.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions doc/examples/schematics/label_value_style_eng.sch
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
R1 1 2 3.1; right
R2 2 3 3.1e6; right
R3 3 4 3.141e3; right
C1 4 5 3.14e-9; right
; label_value_style=eng, label_style=value, label_nodes=false, draw_nodes=connections
Binary file added doc/examples/schematics/label_value_style_sci.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions doc/examples/schematics/label_value_style_sci.sch
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
R1 1 2 3.1; right
R2 2 3 3.1e6; right
R3 3 4 3.141e3; right
C1 4 5 3.14e-9; right
; label_value_style=sci, label_style=value, label_nodes=false, draw_nodes=connections
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions doc/examples/schematics/label_value_style_spice.sch
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
R1 1 2 3.1; right
R2 2 3 3.1e6; right
R3 3 4 3.141e3; right
C1 4 5 3.14e-9; right
; label_value_style=spice, label_style=value, label_nodes=false, draw_nodes=connections
Binary file modified doc/examples/schematics/lpf1-buffer-loaded3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions doc/examples/schematics/lpf1-buffer-loaded3.sch
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ W 2 2_2; right
E1 3 0_3 2 0 A; down, l=A V_{in}
Rout 3 4; right=1.5
RL 4 0_4; down, v=V_o
W 0_2 0_3; size=1.2
W 0_2 0_3; size=1.3
W 0_3 0_4
P1 4 0_4; down
; style=european
; style=european
Binary file modified doc/examples/tutorials/RCnoise/RCparallel1noise.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/RCnoise/RCparallel1noiseplot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/annotations/circuit1_component_currents2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/annotations/circuit1_node_voltages1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/annotations/circuit1_node_voltages2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/annotations/circuit1_node_voltages3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/basic/VRC1plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/basic/VRC1stepplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/basic/VRC2HdBplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/basic/VRC2Hmagplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/basic/VRC2Hphaseplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/basic/VRC2plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/basic/VRC2plot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/discretetime/filter1-response.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/opamps/inoise1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/opamps/opamp-open-loop1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/examples/tutorials/opamps/opamp-piezo-amplifier2-asd2.png
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

a = Circuit('opamp-piezo-amplifier1.sch')

H = a.transfer('Cs', 'Po')(f).limit('A', oo)
H = a.kill().transfer('Cs', 'Po')(f).limit('A', oo)

defs1 = {'R1':100, 'R2':900, 'Cs':1e-9, 'Rs':100e6, 'C':100e-6, 'Vn':2e-9, 'Inp':5e-15, 'Inn':5e-15}
defs2 = {'R1':100, 'R2':900, 'Cs':1e-9, 'Rs':100e6, 'C':10e-6, 'Vn':2e-9, 'Inp':5e-15, 'Inn':5e-15}
Expand All @@ -22,6 +22,3 @@

from matplotlib.pyplot import savefig
savefig(__file__.replace('.py', '.png'), bbox_inches='tight')



Binary file modified doc/examples/tutorials/opamps/vnoise1.png
16 changes: 10 additions & 6 deletions doc/netlists.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ A Component object is obtained from a Circuit object using member notation, for
Component specification
-----------------------

Each line in a netlist describes a single component, with the
general form::
Each line in a netlist describes a single component, except for lines
starting with `#`. The latter are treated as comments.

The general form of a netlist component definition is::

`component-name positive-node negative-node arg1 [arg2 etc.] [; attributes]

If no args are specified then the component value is assigned a
If no arguments are specified then the component value is assigned a
symbolic name specified by `component-name`.

Lines starting with `#` are treated as comments.


The attributes are primarily for controlling the appearance of the schematic. The attribute `nosim` is used to ignore the component for electrical analysis.

Expand Down Expand Up @@ -525,15 +527,17 @@ Circuit methods
nodes `Np` and `Nm` and `impedance(cpt)` returns the driving-point
impedance between the nodes of the specified component

- `in_parallel(component_name)` Returns a set of component names that are connected in parallel with `component_name`
- `in_parallel(component_name)` Returns a set of component names that are connected in parallel with `component_name` (see also `across_nodes()` and `in_series()`)

- `in_series(component_name)` Returns a set of component names that are connected in series with `component_name`
- `in_series(component_name)` Returns a set of component names that are connected in series with `component_name` (see also `in_parallel()`)

- `kill()` Kills specified independent sources (voltage sources
become short-circuits and current sources become open-circuits)

- `kill_except()` Kills all but the specified independent sources

- `prune(component_name)` Returns a copy of the netlist with the component specified by `component_name` removed

- `noise_model()` Replaces resistors with a series combination of a
resistor and a noise voltage source. For example,

Expand Down
27 changes: 26 additions & 1 deletion doc/schematics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ Inductors and chokes
.. literalinclude:: examples/schematics/inductors.sch

.. image:: examples/schematics/inductors.png
:width: 10cm
:width: 15cm


.. _chips:
Expand Down Expand Up @@ -1256,6 +1256,20 @@ Alternatively the style can specified by a schematic option. For example:
:width: 10.5cm


The style of inductors, resistors, current sources, and voltage
sources can be overridden with `inductor_style`, `resistor_style`,
`current_source_style`, and `voltage_source_style`. These can be
`american` or `european`. In addition, the inductor style can be
`cute`.

Here's an example (I wonder who would do this?):

.. literalinclude:: examples/schematics/frankenstein.sch

.. image:: examples/schematics/frankenstein.png
:width: 8cm


Colors
------

Expand Down Expand Up @@ -1497,6 +1511,17 @@ There are several label formatting styles controlled by the
The component name and value can be overridden with the 'l' attribute.


.. _label_value_style:

Label value style
-----------------

Label values are formatted according to the `label_value_style`
attribute. The default is `eng3` which uses an engineering format
with a maximum of three digits. Other formats include 'SPICE' and
`sci` (scientific).


.. _label_placement:

Label placement
Expand Down
22 changes: 11 additions & 11 deletions lcapy/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def latex_with_units(self, eng_format=False, show_units=True,
"""Make LaTeX string with optional units. Units are only
shown for numerical values."""

from .engformatter import EngFormatter
from .valueformatter import value_formatter

expr = self

Expand All @@ -138,7 +138,7 @@ def latex_with_units(self, eng_format=False, show_units=True,
value = expr.sympy

if evalf and value.is_number and eng_format:
return EngFormatter(num_digits=num_digits).latex(value, units)
return value_formatter().latex(value, units)

s = latex(value, **kwargs)
if show_units and units != '':
Expand Down Expand Up @@ -3245,7 +3245,7 @@ def canonical(self, factor_const=False):

ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self
return self.__class__(ratfun.canonical(factor_const),
**self.assumptions)

Expand All @@ -3258,7 +3258,7 @@ def general(self):

ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self
return self.__class__(ratfun.general(), **self.assumptions)

def partfrac(self, combine_conjugates=False, pairs=False, damping=None,
Expand Down Expand Up @@ -3287,7 +3287,7 @@ def partfrac(self, combine_conjugates=False, pairs=False, damping=None,
try:
ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self
return self.__class__(ratfun.partfrac(pairs, damping, method),
**self.assumptions)
except ValueError:
Expand Down Expand Up @@ -3319,7 +3319,7 @@ def recippartfrac(self, combine_conjugates=False, pairs=False,
pairs = pairs or combine_conjugates

if self._ratfun is None:
return self.copy()
return self

tmpsym = miscsymbol('qtmp')

Expand All @@ -3345,7 +3345,7 @@ def standard(self):

ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self
return self.__class__(ratfun.standard(), **self.assumptions)

def mixedfrac(self):
Expand All @@ -3363,7 +3363,7 @@ def timeconst(self):

ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self
return self.__class__(ratfun.timeconst(), **self.assumptions)

def timeconst_terms(self):
Expand Down Expand Up @@ -3393,7 +3393,7 @@ def ZPK(self, pairs=False, combine_conjugates=False):

ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self
return self.__class__(ratfun.ZPK(combine_conjugates or pairs),
**self.assumptions)

Expand All @@ -3413,7 +3413,7 @@ def factored(self, pairs=False):

ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self
return self.__class__(ratfun.ZPK(pairs), **self.assumptions)

def expandcanonical(self):
Expand All @@ -3426,7 +3426,7 @@ def expandcanonical(self):

ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self
return self.__class__(ratfun.expandcanonical(), **self.assumptions)

def expand_functions(self):
Expand Down
14 changes: 8 additions & 6 deletions lcapy/labelmaker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .expr import Expr
from .latex import latex_format_label
from .engformatter import EngFormatter
from .valueformatter import value_formatter
from .valueparser import value_parser
import sympy as sym


Expand All @@ -10,9 +11,9 @@ def _format_expr(self, expr):

return Expr(expr, cache=False).latex_math()

def _format_value_units(self, value, units):
def _format_value_units(self, value, units, style):

return EngFormatter().latex_math(value, units)
return value_formatter(style=style).latex_math(value, units)

def _format_name(self, cpt_type, cpt_id):

Expand All @@ -30,7 +31,7 @@ def _format_name(self, cpt_type, cpt_id):
name = name + '_{%s}' % subscript
return latex_format_label('$' + name + '$')

def make(self, cpt, label_ports=False):
def make(self, cpt, label_ports=False, style='SI'):

# There are two possible labels for a component:
# 1. Component name, e.g., R1
Expand All @@ -55,7 +56,8 @@ def make(self, cpt, label_ports=False):
units_map = {'V': 'V', 'I': 'A', 'R': '$\Omega$',
'C': 'F', 'L': 'H'}

expr = cpt.args[0]
expr = value_parser(cpt.args[0])

if cpt.classname in ('Vstep', 'Istep'):
expr = '(%s) * Heaviside(t)' % expr
value_label = self._format_expr(expr)
Expand All @@ -80,7 +82,7 @@ def make(self, cpt, label_ports=False):
value = float(sym.Rational(expr))
if cpt.type in units_map:
value_label = self._format_value_units(
value, units_map[cpt.type])
value, units_map[cpt.type], style)
else:
value_label = self._format_expr(expr)

Expand Down
6 changes: 6 additions & 0 deletions lcapy/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ def f(x):

return self.applyfunc(f)

def limit(self, var, value, dir='+'):

def f(x): return expr(x).limit(var, value, dir)

return self.applyfunc(f)

def next_timestep(self):

def f(x): return expr(x).next_timestep()
Expand Down
Loading

0 comments on commit e9269d1

Please sign in to comment.