diff --git a/PSSE/psse_mcp.py b/PSSE/psse_mcp.py index 1d71b007..bada1e90 100644 --- a/PSSE/psse_mcp.py +++ b/PSSE/psse_mcp.py @@ -24,7 +24,8 @@ # --------------------------------------------------------------------------- # Lazy, memoized PSS/E engine initialization. # -# psspy lives in a local PSS/E install dir and psseinit() starts the engine. +# psspy lives in a local PSS/E install dir and psseinit() starts the engine, +# sized by _resolve_bus_size(). # Both used to run at import time, which crashed this module on any machine # without PSS/E and blocked packaging/discovery. They now run once, on the first # tool call. The PSSPYxxx / PSSBIN dirs come from ~/.powermcp/config.toml (keys @@ -50,11 +51,44 @@ def _resolve_psse_paths(): return _PSSE_LEGACY["python_lib"], _PSSE_LEGACY["bin"] +def _resolve_bus_size() -> int: + """Return the bus capacity passed to psseinit(). + + POWERMCP_PSSE_BUS_SIZE, then [psse].bus_size in ~/.powermcp/config.toml, + else 0, which asks PSS/E for its default size level: the bus size set in the + PSS/E GUI's network options (50,000 unless changed). The old psseinit(50) + got the 1,000-bus minimum. The size must be right at startup: a second + psseinit() in the process is ignored, and raising bus_size_level() later + does not grow working memory, so a RAW read into a smaller engine drops the + buses past the limit while still returning ierr=0. Only saved-case reads + (psspy.case) grow it on their own. + """ + raw = os.environ.get("POWERMCP_PSSE_BUS_SIZE") + if raw is None: + try: + from powermcp.config import get + raw = get("psse", "bus_size") + except Exception: + raw = None + if raw is None or not str(raw).strip(): + return 0 + text = str(raw).strip() + if not text.isdigit(): + raise RuntimeError( + f"Invalid PSS/E bus size {text!r}: psse.bus_size must be a whole number " + "of buses, or 0 for the PSS/E default. Fix it with " + "`powermcp config set psse.bus_size ` or the " + "POWERMCP_PSSE_BUS_SIZE environment variable." + ) + return int(text) + + def _ensure_psse(): - """Inject PSS/E paths, import psspy, and call psseinit(50) exactly once.""" + """Inject PSS/E paths, import psspy, and call psseinit() exactly once.""" global psspy, _psse_ready if _psse_ready: return psspy + bus_size = _resolve_bus_size() python_lib, bin_dir = _resolve_psse_paths() for p in (python_lib, bin_dir): if not p: @@ -71,7 +105,7 @@ def _ensure_psse(): "are configured: `powermcp config set psse.python_lib ` and " "`powermcp config set psse.bin `. Original error: " + repr(e) ) - _psspy.psseinit(50) # 50 = PSS/E 35+ bus mode; do NOT change + _psspy.psseinit(bus_size) psspy = _psspy _psse_ready = True return psspy diff --git a/README.md b/README.md index af8983f9..ea5ba1d8 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ These tools wrap commercial or locally-installed software, so PowerMCP stores th | Tool | Config keys | Example | |---|---|---| -| PSS/E | `psse.python_lib`, `psse.bin` | `powermcp config set psse.python_lib "C:\Program Files\PTI\PSSE36\36.2\PSSPY311"` | +| PSS/E | `psse.python_lib`, `psse.bin`, optional `psse.bus_size` | `powermcp config set psse.python_lib "C:\Program Files\PTI\PSSE36\36.2\PSSPY311"`. The engine starts at the bus size set in the PSS/E GUI; for a larger case, `powermcp config set psse.bus_size 150000` | | PSLF | `pslf.python_lib` | `powermcp config set pslf.python_lib "C:\Program Files\GE PSLF\PSLF_PYTHON"` | | PowerFactory | `powerfactory.python_path` | `powermcp config set powerfactory.python_path "...\DIgSILENT\PowerFactory 2024\Python\3.11"` | | LTSpice | `ltspice.exe` *(auto-detected)* | Found automatically in standard locations — usually no setup needed. Override: `powermcp config set ltspice.exe "C:\Program Files\ADI\LTspice\LTspice.exe"` | diff --git a/powermcp/__init__.py b/powermcp/__init__.py index 47fbcddf..fd619e36 100644 --- a/powermcp/__init__.py +++ b/powermcp/__init__.py @@ -7,6 +7,6 @@ Configure and connect MCP clients with the `powermcp` CLI (`powermcp install`). """ -__version__ = "0.5.0" +__version__ = "0.5.1" __all__ = ["__version__"] diff --git a/pyproject.toml b/pyproject.toml index cea01003..4640881a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ build-backend = "hatchling.build" [project] name = "powermcp" -version = "0.5.0" +version = "0.5.1" description = "MCP servers for power-system software (PowerWorld, OpenDSS, PSS/E, pandapower, PyPSA, and more)" readme = "README.md" license = { file = "LICENSE" } diff --git a/tests/test_psse_server.py b/tests/test_psse_server.py index 30f8c01c..4a044fc6 100644 --- a/tests/test_psse_server.py +++ b/tests/test_psse_server.py @@ -53,6 +53,62 @@ def _use(monkeypatch, fake): monkeypatch.setattr(psse_mcp, "_ensure_psse", lambda: fake) +@pytest.fixture +def psseinit_calls(monkeypatch, isolated_config): + """An uninitialized server whose psspy import records psseinit's argument.""" + calls = [] + fake = types.ModuleType("psspy") + fake.psseinit = lambda buses: calls.append(buses) + monkeypatch.setitem(sys.modules, "psspy", fake) + monkeypatch.setitem(sys.modules, "psse36", types.ModuleType("psse36")) + monkeypatch.setattr(psse_mcp, "psspy", None) + monkeypatch.setattr(psse_mcp, "_psse_ready", False) + monkeypatch.setattr(psse_mcp, "_resolve_psse_paths", lambda: (None, None)) + return calls + + +def test_engine_starts_at_the_pss_e_default_bus_size(psseinit_calls): + """psseinit(50) capped the engine at 1,000 buses, and a RAW read past that + dropped buses while returning ierr=0. 0 asks PSS/E for the default size + level the GUI starts at. The size cannot be raised later in the process.""" + psse_mcp._ensure_psse() + + assert psseinit_calls == [0] + + +def test_bus_size_comes_from_config(psseinit_calls): + from powermcp import config as cfg + + cfg.set_value("psse", "bus_size", "150000") + + psse_mcp._ensure_psse() + + assert psseinit_calls == [150000] + + +def test_bus_size_environment_variable_overrides_config(psseinit_calls, monkeypatch): + from powermcp import config as cfg + + cfg.set_value("psse", "bus_size", "150000") + monkeypatch.setenv("POWERMCP_PSSE_BUS_SIZE", "200000") + + psse_mcp._ensure_psse() + + assert psseinit_calls == [200000] + + +@pytest.mark.parametrize("value", ["lots", "-5", "1.5"]) +def test_invalid_bus_size_is_refused_before_starting_the_engine(psseinit_calls, value): + from powermcp import config as cfg + + cfg.set_value("psse", "bus_size", value) + + with pytest.raises(RuntimeError, match="psse.bus_size"): + psse_mcp._ensure_psse() + assert psseinit_calls == [] + assert psse_mcp._psse_ready is False + + def test_open_case_returns_the_counts_of_the_loaded_case(monkeypatch, case_file): fake = FakePsspy() _use(monkeypatch, fake) diff --git a/tests/test_registry.py b/tests/test_registry.py index 5a0c3228..88525ab9 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -17,7 +17,7 @@ def test_package_versions_match(): project = tomllib.loads((registry.REPO_ROOT / "pyproject.toml").read_text())["project"] - assert project["version"] == __version__ == "0.5.0" + assert project["version"] == __version__ == "0.5.1" def test_core_tools_present_and_have_no_extra(): diff --git a/tests/test_vendor_import.py b/tests/test_vendor_import.py index 8c8c4247..792724c3 100644 --- a/tests/test_vendor_import.py +++ b/tests/test_vendor_import.py @@ -22,7 +22,7 @@ def _load(mod_name: str, path) -> types.ModuleType: return mod -def test_psse_import_side_effect_free_then_inits_once(monkeypatch): +def test_psse_import_side_effect_free_then_inits_once(monkeypatch, isolated_config): init_calls = [] fake_psspy = types.ModuleType("psspy") fake_psspy.psseinit = lambda n: init_calls.append(n) @@ -39,7 +39,7 @@ def test_psse_import_side_effect_free_then_inits_once(monkeypatch): # First use initializes exactly once; second use is memoized. mod._ensure_psse() mod._ensure_psse() - assert init_calls == [50] + assert init_calls == [0] # 0 = PSS/E's default bus size level assert mod.psspy is fake_psspy monkeypatch.delitem(sys.modules, "psse_mcp_under_test", raising=False)