Skip to content

Instantly share code, notes, and snippets.

@QuentinN42
Last active July 6, 2020 18:16
Show Gist options
  • Save QuentinN42/5d0fda76f7a2f3ddaab3a258176c9d51 to your computer and use it in GitHub Desktop.
Save QuentinN42/5d0fda76f7a2f3ddaab3a258176c9d51 to your computer and use it in GitHub Desktop.
Resistor / Impedence / Capacitor
from functools import reduce
from operator import add, truediv
from typing import Union
def component_connect(val1: float, val2: float, chx: bool) -> float:
r""" Parallel / Series calculus
Parameters
----------
val1 : float
val2 : float
chx : bool
True:
val1 + val2
False:
$$\\frac{val1 \\times val2}{val1 + val2}$$
Returns
-------
float
calculus result
"""
assert val1 >= 0
assert val2 >= 0
if chx:
return val1 + val2
else:
if val1 != 0 or val2 != 0:
return (val1 + val2) / (val1 * val2)
else:
return 0
class Component:
""" Electrical component
Include resistors, inductors and capacitors
Raises
------
NotImplementedError
WIP
"""
r: float
l: float
c: float
""" init """
def __init__(self, r: Union[float, int] = 0, l: Union[float, int] = 0, c: Union[float, int] = 0) -> None:
self.r = float(r)
self.l = float(l)
self.c = float(c)
def __repr__(self) -> str:
return f"{type(self).__name__}(r={self.r}, l={self.l}, c={self.c})"
def __str__(self) -> str:
return repr(self)
""" *// """
def __add__(self, other: "Component") -> "Component":
""" Series association
Associate self and other in series
Parameters
----------
other: Component
the other component
Returns
-------
Component
A component emulating self and other
Raises
------
NotImplementedError
WIP
See Also
--------
https://www.electronics-tutorials.ws/resistor/res_3.html
https://www.electronics-tutorials.ws/inductor/series-inductors.html
https://www.electronics-tutorials.ws/capacitor/cap_7.html
"""
if type(other) == type(self):
return type(self)(
r=component_connect(self.r, other.r, True),
l=component_connect(self.l, other.l, True),
c=component_connect(self.c, other.c, False)
)
else:
raise NotImplementedError("Only implemented for Component x Component")
def __truediv__(self, other: "Component") -> "Component":
""" Parallel association
Associate self and other in parallel
Parameters
----------
other: Component
the other component
Returns
-------
Component
A component emulating self and other
Raises
------
NotImplementedError
WIP
See Also
--------
https://www.electronics-tutorials.ws/resistor/res_4.html
https://www.electronics-tutorials.ws/inductor/parallel-inductors.html
https://www.electronics-tutorials.ws/capacitor/cap_6.html
"""
if type(other) == type(self):
return type(self)(
r=component_connect(self.r, other.r, False),
l=component_connect(self.l, other.l, False),
c=component_connect(self.c, other.c, True)
)
else:
raise NotImplementedError("Only implemented for Component x Component")
def __mul__(self, other: int) -> "Component":
""" Series chain association
Associate self in series other times
Parameters
----------
other: int
the number of association
Returns
-------
Component
A component emulating the setup
Raises
------
NotImplementedError
WIP
"""
if type(other) == int:
return reduce(add, [self] * other)
else:
raise NotImplementedError("Only implemented for Component x int")
def __floordiv__(self, other: int) -> "Component":
""" Parallel chain association
Associate self in parallel other times
Parameters
----------
other: int
the number of association
Returns
-------
Component
A component emulating the setup
Raises
------
NotImplementedError
WIP
"""
if type(other) == int:
return reduce(truediv, [self] * other)
else:
raise NotImplementedError("Only implemented for Component x int")
""" <=> """
def __eq__(self, other: "Component") -> bool:
""" self == other
Test if components are the same
Parameters
----------
other: Component
the other component
Returns
-------
bool
True if R, L and C are the same
Raises
------
NotImplementedError
WIP
"""
if type(other) == type(self):
return self.r == other.r and self.c == other.c and self.l == other.l
else:
raise NotImplementedError("Only implemented for Component x Component")
def __hash__(self) -> int:
""" hash(self)
return the component hash
Returns
-------
int
repr hash
"""
return hash(repr(self))
def __gt__(self, other: "Component") -> bool:
""" self > other
Test if self > other
Parameters
----------
other: Component
the other component
Returns
-------
bool
True simple component and self.value > other.value
Raises
------
NotImplementedError
WIP
"""
if type(other) == type(self):
return self.value > other.value
else:
raise NotImplementedError("Only implemented for Component x Component")
def __lt__(self, other: "Component") -> bool:
""" self < other
Test if self < other
Parameters
----------
other: Component
the other component
Returns
-------
bool
True simple component and self.value < other.value
Raises
------
NotImplementedError
WIP
"""
if type(other) == type(self):
return self.value < other.value
else:
raise NotImplementedError("Only implemented for Component x Component")
def __ge__(self, other: "Component") -> bool:
""" self >= other
Test if self >= other
Parameters
----------
other: Component
the other component
Returns
-------
bool
True simple component and self.value >= other.value
Raises
------
NotImplementedError
WIP
"""
if type(other) == type(self):
return self.value >= other.value
else:
raise NotImplementedError("Only implemented for Component x Component")
def __le__(self, other: "Component") -> bool:
""" self <= other
Test if self <= other
Parameters
----------
other: Component
the other component
Returns
-------
bool
True simple component and self.value <= other.value
Raises
------
NotImplementedError
WIP
"""
if type(other) == type(self):
return self.value <= other.value
else:
raise NotImplementedError("Only implemented for Component x Component")
""" Properties """
@property
def tuple(self) -> tuple:
""" C L R
Simple tuple with C, L and R
Returns
-------
tuple
(C L R)
"""
return self.c, self.l, self.r
@property
def simple_component(self) -> bool:
""" only one value
If the component is only a Resistor, an Inductance or a Capacitor
Returns
-------
bool
component is simple ?
"""
return self.tuple.count(0.0) >= 2
@property
def value(self) -> float:
""" the value
If the component is simple, return the value
Returns
-------
float
component value
Raises
------
NotImplementedError
"""
if self.simple_component:
return sum(self.tuple)
else:
NotImplementedError("Only implemented for simples Components")
def __call__(self, w: float) -> complex:
""" Electrical impedance
See Also
--------
https://en.wikipedia.org/wiki/Electrical_impedance
"""
return sum(f(w) for f in self.ztuple)
def zr(self, w: float) -> complex:
return complex(self.r, 0*w)
def zl(self, w: float) -> complex:
return complex(0, self.l * abs(w))
def zc(self, w: float) -> complex:
return 1/complex(0, self.c * abs(w))
@property
def ztuple(self) -> tuple:
""" Zc Zl Zr
Simple tuple with complex impedance of C, L and R
Returns
-------
tuple
(Zc Zl Zr)
"""
return self.zc, self.zl, self.zr
res = Component(r=1)
cap = Component(c=1)
ind = Component(l=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment