Skip to content
115 changes: 48 additions & 67 deletions pandapower/panda_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
sys.path.insert(0, _server_dir)
from audit import audit_network as _audit_network
from operating_point import validate_operating_point as _validate_operating_point
from security_assessment import run_security_assessment as _run_security_assessment

_repo_root = str(Path(__file__).resolve().parents[1])
_repo_root_added = _repo_root not in sys.path
Expand Down Expand Up @@ -250,78 +251,58 @@ def validate_operating_point(
return {"status": "success", "validation_status": verdict, **report}

@mcp.tool()
def run_contingency_analysis(contingency_type: str = "N-1",
elements: Optional[List[str]] = None) -> Dict[str, Any]:
"""Run contingency analysis on the current network.

def run_contingency_analysis(
contingency_type: str = "N-1",
elements: Optional[List[str]] = None,
voltage_min_pu: float = 0.95,
voltage_max_pu: float = 1.05,
line_loading_limit_percent: float = 100.0,
trafo_loading_limit_percent: float = 100.0,
) -> Dict[str, Any]:
"""Run topology-aware N-1 security assessment on the current network.

The analysis checks switch-aware islands before solving each outage, reports
unsupplied load, and checks bus voltage plus line and transformer loading.
Only N-1 is currently implemented; N-2 is rejected rather than silently
running single outages under an N-2 label.

Args:
contingency_type: Type of contingency analysis ("N-1" or "N-2")
elements: List of specific elements to analyze (optional)

contingency_type: Must be "N-1" (N-2 combinations are not implemented).
elements: Optional pandapower element tables to outage. Defaults to
lines and two-winding transformers for backward compatibility.
voltage_min_pu: Fallback lower voltage limit; bus min_vm_pu overrides it.
voltage_max_pu: Fallback upper voltage limit; bus max_vm_pu overrides it.
line_loading_limit_percent: Fallback line loading limit; line-specific
max_loading_percent overrides it.
trafo_loading_limit_percent: Loading limit for two- and three-winding
transformers; element-specific max_loading_percent overrides it.

Returns:
Dict containing contingency analysis results
Structured N-1 report with islanding classification, violations,
severity components, and summary counts.
"""
logger.info("Running contingency analysis")
logger.info("Running topology-aware N-1 security assessment")
try:
net = _get_network()

# Store original state
orig_net = copy.deepcopy(net)
results = []

# Define elements to analyze
if elements is None:
elements = ['line', 'trafo']

# Perform contingency analysis
for element_type in elements:
for idx in net[element_type].index:
# Create contingency by taking element out of service
contingency_net = copy.deepcopy(orig_net)
contingency_net[element_type].at[idx, 'in_service'] = False

try:
pp.runpp(contingency_net)

# Check for violations
violations = {
'voltage_violations': contingency_net.res_bus[
(contingency_net.res_bus.vm_pu < 0.95) |
(contingency_net.res_bus.vm_pu > 1.05)
].index.tolist(),
'loading_violations': contingency_net.res_line[
contingency_net.res_line.loading_percent > 100
].index.tolist()
}

results.append({
'contingency': f"{element_type}_{idx}",
'converged': contingency_net.converged,
'violations': violations
})

except Exception as e:
results.append({
'contingency': f"{element_type}_{idx}",
'converged': False,
'error': str(e)
})

return {
"status": "success",
"message": "Contingency analysis completed",
"results": results
}
if contingency_type.strip().upper() != "N-1":
return {
"status": "error",
"message": "Only N-1 contingency analysis is implemented; N-2 is not silently approximated.",
}
return _run_security_assessment(
_get_network(),
elements=elements,
voltage_min_pu=voltage_min_pu,
voltage_max_pu=voltage_max_pu,
line_loading_limit_percent=line_loading_limit_percent,
trafo_loading_limit_percent=trafo_loading_limit_percent,
)
except RuntimeError as re:
return {
"status": "error",
"message": str(re)
}
except Exception as e:
return {
"status": "error",
"message": f"Contingency analysis failed: {str(e)}"
}
return {"status": "error", "message": str(re)}
except (TypeError, ValueError) as exc:
return {"status": "error", "message": str(exc)}
except Exception as exc:
logger.exception("Contingency analysis failed")
return {"status": "error", "message": f"Contingency analysis failed: {exc}"}

@mcp.tool()
def get_network_info() -> Dict[str, Any]:
Expand Down
Loading
Loading