diff --git a/pyproject.toml b/pyproject.toml index cf4b7b5..966634a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,14 +34,7 @@ pillow = ">=9.3.0" [tool.poetry.dev-dependencies] pytest = "^8.0" pytest-cov = "^4.0" -matplotlib = "^3.0" -jupyter-book = "^1.0.0" -toml = ">=0.10.2" -pandas = "^2.0" -tqdm = ">=4.63" -packaging = ">=21.1" -pydot = ">=3.0.0" -pillow = ">=9.3.0" +pytest-benchmark = "^4.0" [tool.poetry.group.docs] optional = true @@ -76,7 +69,4 @@ requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" [tool.pytest.ini_options] -#addopts = [ -# "--import-mode=importlib", -#] pythonpath = "src" diff --git a/src/sysloss/system.py b/src/sysloss/system.py index 7961dd5..a24c513 100644 --- a/src/sysloss/system.py +++ b/src/sysloss/system.py @@ -148,6 +148,7 @@ def from_file(cls, fname: str): ) # add sources/pmux for e in range(1, len(entires)): + vo = 0.0 if sys[entires[e]]["type"] == "SOURCE": vo = _get_mand(sys[entires[e]]["params"], "vo") rs = _get_opt(sys[entires[e]]["params"], "rs", RS_DEFAULT) diff --git a/tests/benchmark/conftest.py b/tests/benchmark/conftest.py new file mode 100644 index 0000000..44bf6db --- /dev/null +++ b/tests/benchmark/conftest.py @@ -0,0 +1,85 @@ +# MIT License +# +# Copyright (c) 2024, Geir Drange +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import pytest + +from sysloss.system import System +from sysloss.components import * + + +@pytest.fixture(scope="class") +def sys1(): + """System 1 test fixture""" + sys = System("sys1_fixture", Source("20V", vo=20)) + sys.add_source(Source("-24V", vo=-24, rs=0.05)) + sys.add_comp("-24V", comp=Rectifier("D-bridge", vdrop=0.3)) + sys.add_comp("20V", comp=Rectifier("M-bridge", rs=0.017, iq=1e-3, ig=2e-3)) + sys.add_comp(["D-bridge", "M-bridge"], comp=PMux("Pmux", rs=0.01), rail="Vsys") + d1 = {"vi": [3.3], "io": [0.1, 0.5, 0.9], "vdrop": [[0.55, 0.78, 0.92]]} + sys.add_comp("Vsys", comp=VLoss("Diode 1", vdrop=d1)) + sys.add_comp("Diode 1", comp=RLoss("R1", rs=1.247)) + sys.add_comp("R1", comp=ILoad("I-load 1", ii=0.125)) + e2 = { + "vi": [3.3, 15.0, 48.0], + "io": [0.1, 0.5, 0.9], + "eff": [[0.55, 0.78, 0.92], [0.5, 0.74, 0.83], [0.4, 0.6, 0.766]], + } + sys.add_comp("Vsys", comp=Converter("Buck 5.0", vo=5.0, eff=e2)) + igdata = { + "vi": [15, 50], + "io": [0.0, 0.01, 0.02, 0.1], + "ig": [[0.12e-6, 0.51e-3, 1.52e-3, 2.3e-3], [1e-6, 1e-3, 2e-3, 3e-3]], + } + sys.add_comp("Buck 5.0", comp=LinReg("LDO", vo=2.5, ig=igdata)) + sys.add_comp("LDO", comp=PLoad("Load 2", pwr=0.33)) + sys.add_comp("Vsys", comp=RLoad("Load 3", rs=1234)) + sys.add_comp("Vsys", comp=PSwitch("Sw1", rs=0.077)) + sys.add_comp("Sw1", comp=RLoss("R2", rs=10.247)) + sys.add_comp("R2", comp=PLoad("Load 4", pwr=0.099)) + sys.add_comp("R2", comp=RLoss("R3", rs=20.33)) + sys.add_comp("R3", comp=PLoad("Load 5", pwr=0.33)) + sys.add_comp("R3", comp=RLoss("R4", rs=15)) + sys.add_comp("R4", comp=PLoad("Load 6", pwr=1)) + sys_phases = {"one": 1, "two": 1} + sys.set_sys_phases(sys_phases) + sys.set_comp_phases("20V", phase_conf=["one"]) + sys.set_comp_phases("-24V", phase_conf=["two"]) + return sys + + +@pytest.fixture(scope="class") +def sys2(): + """System 2 test fixture""" + sdc = System("Sensor Daisy chain", Source("Li-ion", vo=14.4, rs=0.2)) + sdc.add_comp("Li-ion", comp=Converter("Boost 48V", vo=48.0, eff=0.88)) + parent = "Boost 48V" + for i in range(1, 17, 1): + idx = " [{}]".format(i) + sdc.add_comp(parent, comp=RLoss("Twisted pair" + idx, rs=4.3)) + sdc.add_comp("Twisted pair" + idx, comp=VLoss("Diode" + idx, vdrop=0.54)) + sdc.add_comp( + "Diode" + idx, comp=PSwitch("Power switch" + idx, rs=0.03, ig=10e-6) + ) + sdc.add_comp("Diode" + idx, comp=Converter("Buck 3.3V" + idx, vo=3.3, eff=0.72)) + sdc.add_comp("Buck 3.3V" + idx, comp=PLoad("Sensor unit" + idx, pwr=0.35)) + parent = "Power switch" + idx + return sdc diff --git a/tests/benchmark/test_bench.py b/tests/benchmark/test_bench.py new file mode 100644 index 0000000..cc6cc35 --- /dev/null +++ b/tests/benchmark/test_bench.py @@ -0,0 +1,45 @@ +# MIT License +# +# Copyright (c) 2024, Geir Drange +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import pytest +from sysloss.system import System +from sysloss.components import * + + +def test_sys1(sys1: System, benchmark): + """Benchmark 1""" + + def run1(): + sys1.solve() + + benchmark(run1) + assert benchmark.stats["max"] < 0.1 + + +def test_sys2(sys2: System, benchmark): + """Benchmark 2""" + + def run2(): + sys2.solve() + + benchmark(run2) + assert benchmark.stats["max"] < 0.5 diff --git a/tests/unit/test_comp_arith.py b/tests/unit/test_comp_arith.py index f8c8b2a..818eec5 100644 --- a/tests/unit/test_comp_arith.py +++ b/tests/unit/test_comp_arith.py @@ -857,9 +857,9 @@ def rectifier_d(name, vdrop, rt): return Rectifier( name, vdrop=vdrop, - rs=random.uniform(0.0, 4.3), - ig=random.uniform(0.0, 1.2e-3), - iq=random.uniform(0.0, 1.5e-4), + rs=random.SystemRandom().uniform(0.0, 4.3), + ig=random.SystemRandom().uniform(0.0, 1.2e-3), + iq=random.SystemRandom().uniform(0.0, 1.5e-4), rt=rt, ) diff --git a/tests/unit/test_components.py b/tests/unit/test_components.py index a1f3631..82fa7e6 100644 --- a/tests/unit/test_components.py +++ b/tests/unit/test_components.py @@ -298,17 +298,17 @@ def test_pmux(): assert pa._limits == pc._limits, "PMux limits from file" pd = PMux( "P-mux", - rs=[0.33, 0.42], + rs=[0.31, 0.47], ig={ - "vi": [1.9, 5.8, 12.5], - "io": [0.002, 0.02, 0.2], + "vi": [2.3, 4.7, 11.8], + "io": [0.001, 0.015, 0.22], "ig": [ - [3.0e-6, 3.0e-6, 4.0e-6], - [9.0e-6, 9.0e-6, 99.0e-6], - [56.0e-6, 67.0e-6, 77.0e-6], + [3.1e-5, 3.2e-5, 4.2e-5], + [7.8e-5, 8.8e-5, 97.0e-5], + [52.0e-5, 62.0e-5, 72.0e-5], ], }, - iis=0.57e-6, + iis=0.67e-5, ) with pytest.raises(ValueError): pd._solv_outp_volt( diff --git a/tests/unit/test_system.py b/tests/unit/test_system.py index be38602..c72440a 100644 --- a/tests/unit/test_system.py +++ b/tests/unit/test_system.py @@ -158,9 +158,9 @@ def test_case5b(): with pytest.raises(ValueError): case5b.add_comp("5V system", comp=LinReg("LDO 3.3", vo=3.3, ig=igdata)) igdata = { - "vi": [5.0], - "io": [0.0, 0.4, 0.6, 0.8], - "ig": [[-0.3, -0.4, -0.67, -0.89]], + "vi": [4.5], + "io": [0.0, 0.3, 0.55, 0.75], + "ig": [[-0.2, -0.3, -0.5, -0.78]], } with pytest.raises(ValueError): case5b.add_comp("5V system", comp=LinReg("LDO 3.3", vo=3.3, ig=igdata)) @@ -192,7 +192,11 @@ def test_case5b(): def test_case5c(): """PSwitch with invalid interpolation parameter""" case5c = System("Case5c system", Source("5V system", vo=5.0)) - igdata = {"vi": [5.0], "io": [0.1, 0.4, 0.6, 0.5], "ig": [[0.3, 0.4, 0.67, 0.89]]} + igdata = { + "vi": [3.3], + "io": [0.11, 0.41, 0.61, 0.51], + "ig": [[0.3, 0.4, 0.67, 0.89]], + } with pytest.raises(ValueError): case5c.add_comp("5V system", comp=PSwitch("PSwitch", rs=3.3, ig=igdata)) igdata = {