Skip to content

Commit

Permalink
Pass expr to valueformatter
Browse files Browse the repository at this point in the history
  • Loading branch information
mph- committed Nov 16, 2024
1 parent a7f54db commit 3aa2d1f
Show file tree
Hide file tree
Showing 18 changed files with 120 additions and 70 deletions.
Binary file modified doc/examples/netlists/circuit-VRLC1.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: 1 addition & 1 deletion doc/examples/netlists/circuit-VRLC1.sch
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
V 1 0 step 10; down
V 1 0 step 10; down=1.5
L 1 2 1e-3; right=1.5
C 2 3 1e-4; right=1.5
R 3 0_1 10; down
Expand Down
Binary file modified doc/examples/netlists/circuit-VRLC2.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: 1 addition & 1 deletion doc/examples/netlists/circuit-VRLC2.sch
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
V 1 0 step 10; down
V 1 0 step 10; down=1.5
L 1 2 1e-3; right=1.5
C 2 3 1e-4; right=1.5
R 3 0_1 1; down
Expand Down
2 changes: 1 addition & 1 deletion doc/examples/schematics/frankenstein.sch
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
R 1 2; right
L 2 3; right
I 3 4; right
I1 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 added doc/examples/schematics/label_value_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions doc/examples/schematics/label_value_style.sch
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
; label_nodes=none, draw_nodes=connections, label_style=value
; label_value_style=eng3
R11 11 12 3141.259; right=1.25, a=eng3
; label_value_style=eng2
R12 12 13 3141.259; right=1.25, a=eng2
; label_value_style=sci3
R13 13 14 3141.259; right=1.25, a=sci3
; label_value_style=spice3
R14 14 15 3141.259; right=1.25, a=spice3
; label_value_style=ratfun
R15 15 16 3141.259; right=1.25, a=ratfun
W 11 21; down

; label_value_style=eng3
R21 21 22 314.1259; right=1.25, a=eng3
; label_value_style=eng2
R22 22 23 314.1259; right=1.25, a=eng2
; label_value_style=sci3
R23 23 24 314.1259; right=1.25, a=sci3
; label_value_style=spice3
R24 24 25 314.1259; right=1.25, a=spice3
; label_value_style=ratfun
R25 25 26 314.1259; right=1.25, a=ratfun
W 21 31; down

; label_value_style=eng3
R31 31 32 31.41259; right=1.25, a=eng3
; label_value_style=eng2
R32 32 33 31.41259; right=1.25, a=eng2
; label_value_style=sci3
R33 33 34 31.41259; right=1.25, a=sci3
; label_value_style=spice3
R34 34 35 31.41259; right=1.25, a=spice3
; label_value_style=ratfun
R35 35 36 31.41259; right=1.25, a=ratfun
W 31 41; down

; label_value_style=eng3
R41 41 42 3.141259; right=1.25, a=eng3
; label_value_style=eng2
R42 42 43 3.141259; right=1.25, a=eng2
; label_value_style=sci3
R43 43 44 3.141259; right=1.25, a=sci3
; label_value_style=spice3
R44 44 45 3.141259; right=1.25, a=spice3
; label_value_style=ratfun
R45 45 46 3.141259; right=1.25, a=ratfun
W 41 51; down

; label_value_style=eng3
R51 51 52 0.3141259; right=1.25, a=eng3
; label_value_style=eng2
R52 52 53 0.3141259; right=1.25, a=eng2
; label_value_style=sci3
R53 53 54 0.3141259; right=1.25, a=sci3
; label_value_style=spice3
R54 54 55 0.3141259; right=1.25, a=spice3
; label_value_style=ratfun
R55 55 56 0.3141259; right=1.25, a=ratfun
Binary file removed doc/examples/schematics/label_value_style_eng.png
Binary file not shown.
5 changes: 0 additions & 5 deletions doc/examples/schematics/label_value_style_eng.sch

This file was deleted.

Binary file removed doc/examples/schematics/label_value_style_sci.png
Binary file not shown.
5 changes: 0 additions & 5 deletions doc/examples/schematics/label_value_style_sci.sch

This file was deleted.

Binary file removed doc/examples/schematics/label_value_style_spice.png
Binary file not shown.
5 changes: 0 additions & 5 deletions doc/examples/schematics/label_value_style_spice.sch

This file was deleted.

41 changes: 10 additions & 31 deletions doc/netlists.rst
Original file line number Diff line number Diff line change
Expand Up @@ -459,46 +459,25 @@ Circuit methods
- `annotate_currents(cpts)` Produces a new netlist with drawing
commands to annotate component currents for specified components (see :ref:`annotated_currents`)

- `apply_test_current_source(Np, Nm)` Copies the netlist, kills all
the sources, and applies a Dirac delta test current source across
the specified nodes. If the netlist is not connected to ground,
the negative specified node is connected to ground. The new
netlist is returned.

- `apply_test_voltage_source(Np, Nm)` Copies the netlist, kills all
the sources, and applies a Dirac delta test voltage source across
the specified nodes. If the netlist is not connected to ground,
the negative specified node is connected to ground. The new
netlist is returned.

- `branch_currents()` returns an `ExprList` of the branch currents,
where each element is `SuperpositionCurrent`. Thus to get the
branch currents in the time-domain:
- `apply_test_current_source(Np, Nm)` Copies the netlist, kills all the sources, and applies a Dirac delta test current source across the specified nodes. If the netlist is not connected to ground, the negative specified node is connected to ground. The new netlist is returned.

- `apply_test_voltage_source(Np, Nm)` Copies the netlist, kills all the sources, and applies a Dirac delta test voltage source across the specified nodes. If the netlist is not connected to ground, the negative specified node is connected to ground. The new netlist is returned.

- `branch_currents()` returns an `ExprList` of the branch currents, where each element is `SuperpositionCurrent`. Thus to get the branch currents in the time-domain:

>>> bi = cct.branch_currents()(t)

- `branch_current_names()` returns an `ExprList` of the branch current
names. Each element has the form `i_cptname` for the time-domain
and of the form `I_cptname` othwerwise.
- `branch_current_names()` returns an `ExprList` of the branch current names. Each element has the form `i_cptname` for the time-domain and of the form `I_cptname` othwerwise.

- `branch_voltages()` returns an `ExprList` of the branch voltages,
where each element is `SuperpositionVoltage`. Thus to get the branch voltages in the Laplace-domain:
- `branch_voltages()` returns an `ExprList` of the branch voltages, where each element is `SuperpositionVoltage`. Thus to get the branch voltages in the Laplace-domain:

>>> bV = cct.branch_voltages()(s)

- `branch_voltage_names()` returns an `ExprList` of the branch voltage
names. Each element has the form `i_cptname` for the time-domain
and of the form `I_cptname` othwerwise.
- `branch_voltage_names()` returns an `ExprList` of the branch voltage names. Each element has the form `i_cptname` for the time-domain and of the form `I_cptname` othwerwise.

- `convert_IVP(t)` Returns a new circuit suitable for solving as an
initial value problem. Any switches in the circuit are evaluated at
the specified time `t`. Note, when solving the IVP, time is
referred to when the last switch activated prior to the time
specified for `t`.
- `convert_IVP(t)` Returns a new circuit suitable for solving as an initial value problem. Any switches in the circuit are evaluated at the specified time `t`. Note, when solving the IVP, time is referred to when the last switch activated prior to the time specified for `t`.

- `defs(ignore)` Returns a directory of argname-value pair for all
components except for components with names specified by
`ignore`. For example,
- `defs(ignore)` Returns a directory of argname-value pair for all components except for components with names specified by `ignore`. For example,

>>> cct = Circuit("""
... R 1 2 3
Expand Down
30 changes: 29 additions & 1 deletion doc/releases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@
Release notes
=============

V1.25
=====

- Note, after this release, the current source direction will reverse to match SPICE

- Fixes state-space analysis for constant independent sources

- Adds `Circuit.sympify()` to strip numerical component values

- Adds `Circuit.defs()` to return dictionary of numerical component values

- Removes independent source for `Circuit.transfer()`

- Adds `Circuit.prune()` to remove specified components

- Adds `inductor_style` schematic attribute

- Adds `label_value_style` schematic attribute for formatting values. This can be `sci` for scientific notation, `eng` for engineering notation, `spice` for SPICE notation, or `ratfun` for rational functions

- Calculates two-port B-parameters directly

- Supports variable resistors for circuit analysis

- Ignore autoground if ground specified in schematic

- Fixes sign for CCCS


V1.24
=====

Expand Down Expand Up @@ -65,7 +93,7 @@ V1.23
V1.22
=====

- Improves 'Node' infrastructure for lcapy-gui
- Improves `Node` infrastructure for lcapy-gui

- Handles more Fourier transforms by splitting into partial fractions

Expand Down
4 changes: 2 additions & 2 deletions lcapy/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 value_formatter().latex(value, units)
return value_formatter().latex(expr, units)

s = latex(value, **kwargs)
if show_units and units != '':
Expand Down Expand Up @@ -969,7 +969,7 @@ def fval(self):
real, imag = val.as_real_imag()
real = float(real)
imag = float(imag)
if abs(imag / real) > 1e-15:
if abs(imag) > 1e-15 * abs(real):
warn('Discarding imaginary part')
return real

Expand Down
5 changes: 1 addition & 4 deletions lcapy/labelmaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ def _format_value_units(self, value, units, style):
if not expr.is_constant or units == '':
return expr.latex_math()

if style == 'ratfun':
value = expr

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

def _format_name(self, cpt_type, cpt_id):

Expand Down
30 changes: 16 additions & 14 deletions lcapy/valueformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ def __init__(self, trim=True, hundreds=False, fmt=''):
except ValueError:
raise ValueError('Expected a digit for format, got ' + fmt)

def _do(self, value, unit, aslatex):
def _do(self, expr, unit, aslatex):

prefixes = ('f', 'p', 'n', 'u', 'm', '', 'k', 'M', 'G', 'T')

value = float(value)
# FIXME for complex values
value = expr.value

if value == 0:
return self._fmt('0', unit, '', aslatex)
Expand Down Expand Up @@ -72,20 +73,20 @@ def _do(self, value, unit, aslatex):

return self._fmt(valstr, unit, prefixes[idx], aslatex)

def latex_math(self, value, unit):
def latex_math(self, expr, unit):
"""This is equivalent to the `latex()` method but encloses in $ $."""

return '$' + self.latex(value, unit) + '$'
return '$' + self.latex(expr, unit) + '$'

def latex(self, value, unit):
def latex(self, expr, unit):
"""Make latex string assuming math mode."""

return self._do(value, unit, aslatex=True)
return self._do(expr, unit, aslatex=True)

def str(self, value, unit):
def str(self, expr, unit):
"""Make string."""

return self._do(value, unit, aslatex=False)
return self._do(expr, unit, aslatex=False)


class EngValueFormatter(ValueFormatter):
Expand Down Expand Up @@ -142,9 +143,10 @@ def _fmt(self, valstr, unit='', prefix='', aslatex=True):

class SciValueFormatter(ValueFormatter):

def _do(self, value, unit, aslatex):
def _do(self, expr, unit, aslatex):

value = float(value)
# FIXME for complex values
value = expr.value

fmt = '%%.%dE' % (self.num_digits - 1)

Expand Down Expand Up @@ -175,15 +177,15 @@ def _do(self, value, unit, aslatex):

class RatfunValueFormatter(ValueFormatter):

def _do(self, value, unit, aslatex):
def _do(self, expr, unit, aslatex):

if not aslatex:
return str(value) + ' ' + unit
return str(expr) + ' ' + unit

if unit.startswith('$'):
return value.latex() + '\\,' + unit[1:-1]
return expr.latex() + '\\,' + unit[1:-1]

return value.latex() + '\\,' + '\\mathrm{' + unit + '}'
return expr.latex() + '\\,' + '\\mathrm{' + unit + '}'


def value_formatter(style='eng3'):
Expand Down

0 comments on commit 3aa2d1f

Please sign in to comment.