Skip to content

Commit

Permalink
fix(system): add check of component name and set default load phase i…
Browse files Browse the repository at this point in the history
…n change_comp()
  • Loading branch information
geddy11 committed Sep 2, 2024
1 parent fa36896 commit 1a06e47
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/sysloss/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ def _chk_parent(self, parent: str):

return True

def _chk_comp(self, comp: str):
"""Check if component exists"""
if not comp in self._g.attrs["nodes"].keys():
raise ValueError('Component name "{}" not found!'.format(comp))

return True

def _chk_name(self, name: str):
"""Check if component name is valid"""
# check if name exists
Expand Down Expand Up @@ -371,13 +378,16 @@ def change_comp(self, name: str, *, comp):
------
ValueError
If trying to change a `source` component to a different type, or
if the target component does not exist or
if the parent does not accept a connection to the new component.
Examples
--------
>>> sys.change_comp("Buck", comp=LinReg("LDO", vo=1.8))
"""
# check that component exists
self._chk_comp(name)
# if component name changes, check that it is unique
if name != comp._params["name"]:
self._chk_name(comp._params["name"])
Expand All @@ -401,6 +411,9 @@ def change_comp(self, name: str, *, comp):
# replace node name in graph dict
del [self._g.attrs["nodes"][name]]
self._g.attrs["nodes"][comp._params["name"]] = eidx
# delete old phase config and set new default
del [self._g.attrs["phase_conf"][name]]
self._g.attrs["phase_conf"][comp._params["name"]] = {}

def del_comp(self, name: str, *, del_childs: bool = True):
"""Delete component.
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,16 @@ def test_case10():
"""Change component"""
case10 = System("Case10 system", Source("24V system", vo=24.0, rs=12e-3))
case10.add_comp("24V system", comp=Converter("Buck", vo=3.3, eff=0.80))
case10.add_comp("Buck", comp=PLoad("Load", pwr=0.5))
case10.change_comp("Buck", comp=LinReg("LDO", vo=3.3))
with pytest.raises(ValueError):
case10.change_comp("LDO", comp=Source("5V", vo=5.0))
with pytest.raises(ValueError):
case10.change_comp("24V system", comp=LinReg("LDO2", vo=3.3))
with pytest.raises(ValueError):
case10.change_comp("Non-exist", comp=Source("5V", vo=5.0))
df = case10.solve()
assert len(df) == 4, "Case10 parameters row count"


def test_case11():
Expand Down

0 comments on commit 1a06e47

Please sign in to comment.