Files
TJWaterServer/api/parser.py
WQY\qiong 75aec8e40e Add inps
2022-10-15 18:06:10 +08:00

129 lines
4.4 KiB
Python

from typing import Any
from .project import *
from .s1_title import *
from .s2_junctions import *
from .s3_reservoirs import *
from .s4_tanks import *
from .s5_pipes import *
from .s6_pumps import *
from .s7_valves import *
def read_inp(name: str, inp: str):
if is_project_open(name):
close_project(name)
if have_project(name):
delete_project(name)
create_project(name)
open_project(name)
junctions : dict[str, dict[str, Any]] = {}
reservoirs : dict[str, dict[str, Any]] = {}
tanks : dict[str, dict[str, Any]] = {}
pipes : dict[str, dict[str, Any]] = {}
pumps : dict[str, dict[str, Any]] = {}
valves : dict[str, dict[str, Any]] = {}
section = ''
for line in open(inp):
line = line.lstrip()
if line.startswith(';'):
continue
if line.startswith('[TITLE'):
section = 'title'
continue
if line.startswith('[JUNCTION'):
section = JUNCTION
continue
if line.startswith('[RESERVOIR'):
section = RESERVOIR
continue
if line.startswith('[TANK'):
section = TANK
continue
if line.startswith('[PIPE'):
section = PIPE
continue
if line.startswith('[PUMP'):
section = PUMP
continue
if line.startswith('[VALVE'):
section = VALVE
continue
if line.startswith('[COORDINATE'):
section = 'coordinate'
continue
if line.startswith('['):
section = ''
continue
tokens = line.split()
if len(tokens) == 0:
continue
if section == 'title':
# set_title(name, ChangeSet({'value': tokens[0]}))
continue
elif section == JUNCTION:
if tokens[0] not in junctions:
junctions[tokens[0]] = {}
junctions[tokens[0]] |= {'id': tokens[0], 'elevation': tokens[1]}
continue
elif section == RESERVOIR:
if tokens[0] not in reservoirs:
reservoirs[tokens[0]] = {}
reservoirs[tokens[0]] |= {'id': tokens[0], 'head': tokens[1]}
continue
elif section == TANK:
if tokens[0] not in tanks:
tanks[tokens[0]] = {}
tanks[tokens[0]] = {'id': tokens[0], 'elevation': tokens[1], 'init_level': tokens[2], 'min_level': tokens[3], 'max_level': tokens[4], 'diameter': tokens[5], 'min_vol': tokens[6]}
continue
elif section == PIPE:
if len(tokens) == 7:
tokens.append(PIPE_STATUS_OPEN)
pipes[tokens[0]] = {'id': tokens[0], 'node1': tokens[1], 'node2': tokens[2], 'length': tokens[3], 'diameter': tokens[4], 'roughness': tokens[5], 'minor_loss': tokens[6], 'status': tokens[7].lower()}
continue
elif section == PUMP:
pumps[tokens[0]] = {'id': tokens[0], 'node1': tokens[1], 'node2': tokens[2]}
continue
elif section == VALVE:
valves[tokens[0]] = {'id': tokens[0], 'node1': tokens[1], 'node2': tokens[2], 'diameter': tokens[3], 'v_type': tokens[4].lower(), 'setting': tokens[5], 'minor_loss': tokens[6]}
continue
elif section == 'coordinate':
if tokens[0] in junctions:
junctions[tokens[0]] |= {'x': tokens[1], 'y': tokens[2]}
elif tokens[0] in reservoirs:
reservoirs[tokens[0]] |= {'x': tokens[1], 'y': tokens[2]}
elif tokens[0] in tanks:
tanks[tokens[0]] |= {'x': tokens[1], 'y': tokens[2]}
continue
for value in junctions.values():
if 'x' not in value:
value['x'] = 0.0
if 'y' not in value:
value['y'] = 0.0
add_junction(name, ChangeSet(value))
for value in reservoirs.values():
if 'x' not in value:
value['x'] = 0.0
if 'y' not in value:
value['y'] = 0.0
add_reservoir(name, ChangeSet(value))
for value in tanks.values():
if 'x' not in value:
value['x'] = 0.0
if 'y' not in value:
value['y'] = 0.0
add_tank(name, ChangeSet(value))
for value in pipes.values():
add_pipe(name, ChangeSet(value))
for value in pumps.values():
add_pump(name, ChangeSet(value))
for value in valves.values():
add_valve(name, ChangeSet(value))