修改关阀分析算法,支持多管段分析
This commit is contained in:
@@ -20,26 +20,45 @@ def _parse_link_entry(link_entry: str) -> tuple[str, str, str, str]:
|
|||||||
return parts[0], parts[1], parts[2], parts[3]
|
return parts[0], parts[1], parts[2], parts[3]
|
||||||
|
|
||||||
|
|
||||||
def valve_isolation_analysis(network: str, accident_element: str) -> dict[str, Any]:
|
def valve_isolation_analysis(
|
||||||
|
network: str, accident_elements: str | list[str]
|
||||||
|
) -> dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
关阀搜索/分析:基于拓扑结构确定事故隔离所需关阀。
|
关阀搜索/分析:基于拓扑结构确定事故隔离所需关阀。
|
||||||
:param network: 模型名称
|
:param network: 模型名称
|
||||||
:param accident_element: 事故点(节点或管道/泵/阀门ID)
|
:param accident_elements: 事故点(节点或管道/泵/阀门ID),可以是单个ID字符串或ID列表
|
||||||
:return: dict,包含受影响节点、必须关闭阀门、可选阀门等信息
|
:return: dict,包含受影响节点、必须关闭阀门、可选阀门等信息
|
||||||
"""
|
"""
|
||||||
if is_node(network, accident_element):
|
if isinstance(accident_elements, str):
|
||||||
start_nodes = {accident_element}
|
target_elements = [accident_elements]
|
||||||
accident_type = "node"
|
else:
|
||||||
elif is_link(network, accident_element):
|
target_elements = accident_elements
|
||||||
accident_type = get_link_type(network, accident_element)
|
|
||||||
link_props = get_link_properties(network, accident_element)
|
start_nodes = set()
|
||||||
|
accident_types = set()
|
||||||
|
|
||||||
|
for element in target_elements:
|
||||||
|
if is_node(network, element):
|
||||||
|
start_nodes.add(element)
|
||||||
|
accident_types.add("node")
|
||||||
|
elif is_link(network, element):
|
||||||
|
l_type = get_link_type(network, element)
|
||||||
|
accident_types.add(l_type)
|
||||||
|
link_props = get_link_properties(network, element)
|
||||||
node1 = link_props.get("node1")
|
node1 = link_props.get("node1")
|
||||||
node2 = link_props.get("node2")
|
node2 = link_props.get("node2")
|
||||||
if not node1 or not node2:
|
if not node1 or not node2:
|
||||||
raise ValueError("Accident link missing node endpoints")
|
# 如果是批量处理,可以选择跳过错误或记录错误,这里暂时保持严谨抛出异常
|
||||||
start_nodes = {node1, node2}
|
raise ValueError(f"Accident link {element} missing node endpoints")
|
||||||
|
start_nodes.add(node1)
|
||||||
|
start_nodes.add(node2)
|
||||||
else:
|
else:
|
||||||
raise ValueError("Accident element not found")
|
raise ValueError(f"Accident element {element} not found")
|
||||||
|
|
||||||
|
# 如果有多种类型混合,简化返回类型描述
|
||||||
|
accident_type_str = (
|
||||||
|
list(accident_types)[0] if len(accident_types) == 1 else "mixed"
|
||||||
|
)
|
||||||
|
|
||||||
adjacency: dict[str, set[str]] = defaultdict(set)
|
adjacency: dict[str, set[str]] = defaultdict(set)
|
||||||
valve_links: dict[str, tuple[str, str]] = {}
|
valve_links: dict[str, tuple[str, str]] = {}
|
||||||
@@ -76,11 +95,16 @@ def valve_isolation_analysis(network: str, accident_element: str) -> dict[str, A
|
|||||||
must_close_valves.sort()
|
must_close_valves.sort()
|
||||||
optional_valves.sort()
|
optional_valves.sort()
|
||||||
|
|
||||||
return {
|
result = {
|
||||||
"accident_element": accident_element,
|
"accident_elements": target_elements,
|
||||||
"accident_type": accident_type,
|
"accident_type": accident_type_str,
|
||||||
"affected_nodes": sorted(affected_nodes),
|
"affected_nodes": sorted(affected_nodes),
|
||||||
"must_close_valves": must_close_valves,
|
"must_close_valves": must_close_valves,
|
||||||
"optional_valves": optional_valves,
|
"optional_valves": optional_valves,
|
||||||
"isolatable": len(must_close_valves) > 0,
|
"isolatable": len(must_close_valves) > 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(target_elements) == 1:
|
||||||
|
result["accident_element"] = target_elements[0]
|
||||||
|
|
||||||
|
return result
|
||||||
|
|||||||
@@ -238,7 +238,9 @@ async def fastapi_valve_close_analysis(
|
|||||||
|
|
||||||
|
|
||||||
@router.get("/valve_isolation_analysis/")
|
@router.get("/valve_isolation_analysis/")
|
||||||
async def valve_isolation_endpoint(network: str, accident_element: str):
|
async def valve_isolation_endpoint(
|
||||||
|
network: str, accident_element: List[str] = Query(...)
|
||||||
|
):
|
||||||
return analyze_valve_isolation(network, accident_element)
|
return analyze_valve_isolation(network, accident_element)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,5 +3,7 @@ from typing import Any
|
|||||||
from app.algorithms.valve_isolation import valve_isolation_analysis
|
from app.algorithms.valve_isolation import valve_isolation_analysis
|
||||||
|
|
||||||
|
|
||||||
def analyze_valve_isolation(network: str, accident_element: str) -> dict[str, Any]:
|
def analyze_valve_isolation(
|
||||||
|
network: str, accident_element: str | list[str]
|
||||||
|
) -> dict[str, Any]:
|
||||||
return valve_isolation_analysis(network, accident_element)
|
return valve_isolation_analysis(network, accident_element)
|
||||||
|
|||||||
Reference in New Issue
Block a user