Skip to content

Commit

Permalink
refactor: remove duplicate code in Components
Browse files Browse the repository at this point in the history
  • Loading branch information
geddy11 committed Oct 16, 2024
1 parent 31873dd commit 488d451
Showing 1 changed file with 12 additions and 107 deletions.
119 changes: 12 additions & 107 deletions src/sysloss/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,17 @@ def _get_annot(self):
"""Get interpolation figure annotations in format [xlabel, ylabel, title]"""
return ["xlabel", "ylabel", "title"]

def _get_params(self, pdict):
"""Return dict with component parameters"""
ret = pdict
for param in pdict:
if param in self._params:
if isinstance(self._params[param], dict):
ret[param] = "interp"
else:
ret[param] = self._params[param]
return ret


class Source(_Component):
"""Voltage source.
Expand Down Expand Up @@ -354,13 +365,6 @@ def _solv_get_warns(self, vi, vo, ii, io, ta, phase, phase_conf={}):
self._limits, {"io": io, "po": vo * io, "pl": self._params["rs"] * io * io}
)

def _get_params(self, pdict):
"""Return dict with component parameters"""
ret = pdict
ret["vo"] = self._params["vo"]
ret["rs"] = self._params["rs"]
return ret


class PLoad(_Component):
"""Power load.
Expand Down Expand Up @@ -462,14 +466,6 @@ def _solv_get_warns(self, vi, vo, ii, io, ta, phase, phase_conf={}):
tp = tr + ta
return _get_warns(self._limits, {"vi": vi, "ii": ii, "tr": tr, "tp": tp})

def _get_params(self, pdict):
"""Return dict with component parameters"""
ret = pdict
ret["pwr"] = self._params["pwr"]
ret["pwrs"] = self._params["pwrs"]
ret["rt"] = self._params["rt"]
return ret


class ILoad(PLoad):
"""Current load.
Expand Down Expand Up @@ -551,14 +547,6 @@ def _solv_get_warns(self, vi, vo, ii, io, ta, phase, phase_conf={}):
tp = tr + ta
return _get_warns(self._limits, {"vi": vi, "pi": vi * ii, "tr": tr, "tp": tp})

def _get_params(self, pdict):
"""Return dict with component parameters"""
ret = pdict
ret["ii"] = self._params["ii"]
ret["iis"] = self._params["iis"]
ret["rt"] = self._params["rt"]
return ret


class RLoad(PLoad):
"""Resistive load.
Expand Down Expand Up @@ -635,13 +623,6 @@ def _solv_get_warns(self, vi, vo, ii, io, ta, phase, phase_conf={}):
self._limits, {"vi": vi, "ii": ii, "pi": vi * ii, "tr": tr, "tp": tp}
)

def _get_params(self, pdict):
"""Return dict with component parameters"""
ret = pdict
ret["rs"] = self._params["rs"]
ret["rt"] = self._params["rt"]
return ret


class RLoss(_Component):
"""Resistive loss.
Expand Down Expand Up @@ -755,15 +736,8 @@ def _solv_get_warns(self, vi, vo, ii, io, ta, phase, phase_conf={}):
},
)

def _get_params(self, pdict):
"""Return dict with component parameters"""
ret = pdict
ret["rs"] = self._params["rs"]
ret["rt"] = self._params["rt"]
return ret


class VLoss(_Component):
class VLoss(RLoss):
"""Voltage loss.
This loss element is connected in series with other elements.
Expand Down Expand Up @@ -877,27 +851,6 @@ def _solv_pwr_loss(self, vi, vo, ii, io, ta, phase, phase_conf={}, pstate={}):
tr = loss * self._params["rt"]
return pwr, loss, _get_eff(pwr, pwr - loss, 100.0), tr, ta + tr

def _solv_get_warns(self, vi, vo, ii, io, ta, phase, phase_conf={}):
"""Check limits"""
pl = abs(vi) * ii - abs(vo) * io
tr = pl * self._params["rt"]
tp = tr + ta
return _get_warns(
self._limits,
{
"vi": vi,
"vo": vo,
"vd": abs(vi - vo),
"ii": ii,
"io": io,
"pi": abs(vi * ii),
"po": abs(vo * io),
"pl": pl,
"tr": tr,
"tp": tp,
},
)

def _get_annot(self):
"""Get interpolation figure annotations in format [xlabel, ylabel, title]"""
if isinstance(self._ipr, _Interp1d):
Expand All @@ -912,16 +865,6 @@ def _get_annot(self):
"{} voltage drop".format(self._params["name"]),
]

def _get_params(self, pdict):
"""Return dict with component parameters"""
ret = pdict
ret["rt"] = self._params["rt"]
if isinstance(self._ipr, _Interp0d):
ret["vdrop"] = abs(self._params["vdrop"])
else:
ret["vdrop"] = "interp"
return ret


class Converter(_Component):
"""Voltage converter.
Expand Down Expand Up @@ -1138,19 +1081,6 @@ def _get_annot(self):
"{} efficiency for Vo={}V".format(self._params["name"], self._params["vo"]),
]

def _get_params(self, pdict):
"""Return dict with component parameters"""
ret = pdict
ret["vo"] = self._params["vo"]
ret["iq"] = self._params["iq"]
if isinstance(self._ipr, _Interp0d):
ret["eff"] = abs(self._params["eff"])
else:
ret["eff"] = "interp"
ret["iis"] = self._params["iis"]
ret["rt"] = self._params["rt"]
return ret


class LinReg(_Component):
"""Linear voltage regulator.
Expand Down Expand Up @@ -1393,19 +1323,6 @@ def _get_annot(self):
),
]

def _get_params(self, pdict):
"""Return dict with component parameters"""
ret = pdict
ret["vo"] = self._params["vo"]
ret["vdrop"] = self._params["vdrop"]
if isinstance(self._ipr, _Interp0d):
ret["ig"] = abs(self._params["ig"])
else:
ret["ig"] = "interp"
ret["iis"] = self._params["iis"]
ret["rt"] = self._params["rt"]
return ret


class PSwitch(_Component):
"""Power switch.
Expand Down Expand Up @@ -1594,15 +1511,3 @@ def _get_annot(self):
"Input voltage (V)",
"{} ground current".format(self._params["name"]),
]

def _get_params(self, pdict):
"""Return dict with component parameters"""
ret = pdict
ret["rs"] = self._params["rs"]
if isinstance(self._ipr, _Interp0d):
ret["ig"] = abs(self._params["ig"])
else:
ret["ig"] = "interp"
ret["iis"] = self._params["iis"]
ret["rt"] = self._params["rt"]
return ret

0 comments on commit 488d451

Please sign in to comment.