Start parse all sections

This commit is contained in:
WQY\qiong
2022-11-12 14:17:11 +08:00
parent 69eb6660fb
commit a7140813a0

View File

@@ -16,9 +16,57 @@ from .s13_controls import *
from .s14_rules import * from .s14_rules import *
from .s15_energy import * from .s15_energy import *
from .s16_emitters import * from .s16_emitters import *
from .s16_emitters import * from .s17_quality import *
from .s18_sources import *
from .s19_reactions import *
from .s20_mixing import *
from .s21_times import * from .s21_times import *
from .s22_report import *
from .s23_options import * from .s23_options import *
from .s24_coordinates import *
from .s25_vertices import *
from .s26_labels import *
from .s27_backdrop import *
from .s28_end import *
def parse_inp(inp: str) -> dict[str, list[str]]:
section_name = ['TITLE', 'JUNCTIONS', 'RESERVOIRS', 'TANKS', 'PIPES',
'PUMPS', 'VALVES', 'TAGS', 'DEMANDS', 'STATUS',
'PATTERNS', 'CURVES', 'CONTROLS', 'RULES', 'ENERGY',
'EMITTERS', 'QUALITY', 'SOURCES', 'REACTIONS', 'MIXING',
'TIMES', 'REPORT', 'OPTIONS', 'COORDINATES', 'VERTICES',
'LABELS', 'BACKDROP', 'END']
file: dict[str, list[str]] = {}
for s in section_name:
file[s] = []
section = ''
for line in open(inp):
line = line.strip()
if line == '':
section = ''
continue
if line.startswith('['):
is_section = False
for s in section_name:
if line.startswith(f'[{s}'):
section = s
is_section = True
break
if is_section:
continue
if section != '':
file[section].append(line)
return file
def read_inp(name: str, inp: str): def read_inp(name: str, inp: str):
@@ -33,25 +81,27 @@ def read_inp(name: str, inp: str):
section = '' section = ''
title : str = '' file: dict[str, list[str]] = {}
junctions : dict[str, dict[str, Any]] = {}
reservoirs : dict[str, dict[str, Any]] = {} _1_title : str = ''
tanks : dict[str, dict[str, Any]] = {} _2_junctions : dict[str, dict[str, Any]] = {}
pipes : dict[str, dict[str, Any]] = {} _3_reservoirs : dict[str, dict[str, Any]] = {}
pumps : dict[str, dict[str, Any]] = {} _4_tanks : dict[str, dict[str, Any]] = {}
valves : dict[str, dict[str, Any]] = {} _5_pipes : dict[str, dict[str, Any]] = {}
demands : dict[str, list[dict[str, Any]]] = {} _6_pumps : dict[str, dict[str, Any]] = {}
status : dict[str, dict[str, Any]] = {} _7_valves : dict[str, dict[str, Any]] = {}
patterns : dict[str, list[float]] = {} _9_demands : dict[str, list[dict[str, Any]]] = {}
curves : dict[str, list[dict[str, float]]] = {} _10_status : dict[str, dict[str, Any]] = {}
emitters : dict[str, float] = {} _11_patterns : dict[str, list[float]] = {}
times : dict[str, str] = {} _12_curves : dict[str, list[dict[str, float]]] = {}
options : dict[str, str] = {} _16_emitters : dict[str, float] = {}
_21_times : dict[str, str] = {}
_22_options : dict[str, str] = {}
for line in open(inp): for line in open(inp):
line = line.strip() line = line.strip()
if line.startswith(';'): if line.startswith(';') and not line.endswith(':'):
continue continue
if line.endswith(';'): if line.endswith(';'):
line = line.removesuffix(';') line = line.removesuffix(';')
@@ -115,111 +165,111 @@ def read_inp(name: str, inp: str):
continue continue
if section == 'title': if section == 'title':
if title == '': if _1_title == '':
title += '\n' _1_title += '\n'
title += line _1_title += line
continue continue
elif section == JUNCTION: elif section == JUNCTION:
junction_demand = float(tokens[2]) if tokens_len >= 3 else None junction_demand = float(tokens[2]) if tokens_len >= 3 else None
junction_pattern = tokens[3] if tokens_len == 4 else None junction_pattern = tokens[3] if tokens_len == 4 else None
junctions[tokens[0]] = {'id': tokens[0], 'elevation': tokens[1], 'demand': junction_demand, 'pattern': junction_pattern} _2_junctions[tokens[0]] = {'id': tokens[0], 'elevation': tokens[1], 'demand': junction_demand, 'pattern': junction_pattern}
continue continue
elif section == RESERVOIR: elif section == RESERVOIR:
reservoir_pattern = tokens[2] if tokens_len == 3 else None reservoir_pattern = tokens[2] if tokens_len == 3 else None
reservoirs[tokens[0]] = {'id': tokens[0], 'head': tokens[1], 'pattern': reservoir_pattern} _3_reservoirs[tokens[0]] = {'id': tokens[0], 'head': tokens[1], 'pattern': reservoir_pattern}
continue continue
elif section == TANK: elif section == TANK:
tank_vol_curve = tokens[7] if tokens_len >= 8 else None tank_vol_curve = tokens[7] if tokens_len >= 8 else None
tank_overflow = tokens[8].upper() if tokens_len == 9 else None tank_overflow = tokens[8].upper() if tokens_len == 9 else None
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], 'vol_curve': tank_vol_curve, 'overflow': tank_overflow} _4_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], 'vol_curve': tank_vol_curve, 'overflow': tank_overflow}
continue continue
elif section == PIPE: elif section == PIPE:
# status is must-have, here fix input # status is must-have, here fix input
pipe_status = tokens[7].upper() if tokens_len == 8 else PIPE_STATUS_OPEN pipe_status = tokens[7].upper() if tokens_len == 8 else 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': pipe_status} _5_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': pipe_status}
continue continue
elif section == PUMP: elif section == PUMP:
pumps[tokens[0]] = {'id': tokens[0], 'node1': tokens[1], 'node2': tokens[2]} _6_pumps[tokens[0]] = {'id': tokens[0], 'node1': tokens[1], 'node2': tokens[2]}
for i in range(3, tokens_len, 2): for i in range(3, tokens_len, 2):
pumps[tokens[0]] |= { tokens[i].lower(): tokens[i + 1] } _6_pumps[tokens[0]] |= { tokens[i].lower(): tokens[i + 1] }
continue continue
elif section == VALVE: elif section == VALVE:
valves[tokens[0]] = {'id': tokens[0], 'node1': tokens[1], 'node2': tokens[2], 'diameter': tokens[3], 'v_type': tokens[4], 'setting': tokens[5], 'minor_loss': tokens[6]} _7_valves[tokens[0]] = {'id': tokens[0], 'node1': tokens[1], 'node2': tokens[2], 'diameter': tokens[3], 'v_type': tokens[4], 'setting': tokens[5], 'minor_loss': tokens[6]}
continue continue
elif section == 'demand': elif section == 'demand':
demand_pattern = tokens[2] if tokens_len >= 3 else None demand_pattern = tokens[2] if tokens_len >= 3 else None
demand_category = tokens[3] if tokens_len == 4 else None demand_category = tokens[3] if tokens_len == 4 else None
demands[tokens[0]].append({'demand': tokens[1], 'pattern': demand_pattern, 'category': demand_category}) _9_demands[tokens[0]].append({'demand': tokens[1], 'pattern': demand_pattern, 'category': demand_category})
continue continue
elif section == 'status': elif section == 'status':
if tokens[0] not in status: if tokens[0] not in _10_status:
status[tokens[0]] = {} _10_status[tokens[0]] = {}
setting = None setting = None
try: try:
setting = float(tokens[1]) setting = float(tokens[1])
except: except:
setting = None setting = None
if setting != None: if setting != None:
status[tokens[0]]['setting'] = setting _10_status[tokens[0]]['setting'] = setting
else: else:
status[tokens[0]]['status'] = tokens[1].upper() _10_status[tokens[0]]['status'] = tokens[1].upper()
continue continue
elif section == PATTERN: elif section == PATTERN:
if tokens[0] not in patterns: if tokens[0] not in _11_patterns:
patterns[tokens[0]] = [] _11_patterns[tokens[0]] = []
for i in range(1, tokens_len): for i in range(1, tokens_len):
patterns[tokens[0]].append(float(tokens[i])) _11_patterns[tokens[0]].append(float(tokens[i]))
continue continue
elif section == CURVE: elif section == CURVE:
if tokens[0] not in curves: if tokens[0] not in _12_curves:
curves[tokens[0]] = [] _12_curves[tokens[0]] = []
for i in range(1, tokens_len, 2): for i in range(1, tokens_len, 2):
curves[tokens[0]].append({ 'x': float(tokens[i]), 'y': float(tokens[i + 1]) }) _12_curves[tokens[0]].append({ 'x': float(tokens[i]), 'y': float(tokens[i + 1]) })
continue continue
elif section == 'emitter': elif section == 'emitter':
emitters[tokens[0]] = float(tokens[1]) _16_emitters[tokens[0]] = float(tokens[1])
continue continue
elif section == 'time': elif section == 'time':
if tokens_len == 2: if tokens_len == 2:
times[tokens[0]] = tokens[1] _21_times[tokens[0]] = tokens[1]
elif tokens_len == 3: elif tokens_len == 3:
times[tokens[0] + ' ' + tokens[1]] = tokens[2] _21_times[tokens[0] + ' ' + tokens[1]] = tokens[2]
elif tokens_len == 4: elif tokens_len == 4:
times[tokens[0] + ' ' + tokens[1]] = tokens[2] + ' ' + tokens[3] _21_times[tokens[0] + ' ' + tokens[1]] = tokens[2] + ' ' + tokens[3]
continue continue
elif section == 'option': elif section == 'option':
if tokens[0] == 'HYDRAULICS' or tokens[0] == 'MAP': if tokens[0] == 'HYDRAULICS' or tokens[0] == 'MAP':
continue continue
if tokens_len == 2: if tokens_len == 2:
options[tokens[0]] = tokens[1] _22_options[tokens[0]] = tokens[1]
elif tokens_len == 3: elif tokens_len == 3:
if tokens[0] == 'UNBALANCED' or tokens[0] == 'QUALITY': if tokens[0] == 'UNBALANCED' or tokens[0] == 'QUALITY':
options[tokens[0]] = tokens[1] + ' ' + tokens[2] _22_options[tokens[0]] = tokens[1] + ' ' + tokens[2]
else: else:
options[tokens[0] + ' ' + tokens[1]] = tokens[2] _22_options[tokens[0] + ' ' + tokens[1]] = tokens[2]
continue continue
elif section == 'coordinate': elif section == 'coordinate':
if tokens[0] in junctions: if tokens[0] in _2_junctions:
junctions[tokens[0]] |= {'x': tokens[1], 'y': tokens[2]} _2_junctions[tokens[0]] |= {'x': tokens[1], 'y': tokens[2]}
elif tokens[0] in reservoirs: elif tokens[0] in _3_reservoirs:
reservoirs[tokens[0]] |= {'x': tokens[1], 'y': tokens[2]} _3_reservoirs[tokens[0]] |= {'x': tokens[1], 'y': tokens[2]}
elif tokens[0] in tanks: elif tokens[0] in _4_tanks:
tanks[tokens[0]] |= {'x': tokens[1], 'y': tokens[2]} _4_tanks[tokens[0]] |= {'x': tokens[1], 'y': tokens[2]}
continue continue
# title # title
set_title(name, ChangeSet({ 'value': title })) set_title(name, ChangeSet({ 'value': _1_title }))
# pattern # pattern
for key, value in patterns.items(): for key, value in _11_patterns.items():
set_pattern(name, ChangeSet({'id': key, 'factors': value})) set_pattern(name, ChangeSet({'id': key, 'factors': value}))
# curve # curve
for key, value in curves.items(): for key, value in _12_curves.items():
set_curve(name, ChangeSet({'id': key, 'coords': value})) set_curve(name, ChangeSet({'id': key, 'coords': value}))
# junction # junction
for value in junctions.values(): for value in _2_junctions.values():
if 'x' not in value: if 'x' not in value:
value['x'] = 0.0 value['x'] = 0.0
if 'y' not in value: if 'y' not in value:
@@ -227,7 +277,7 @@ def read_inp(name: str, inp: str):
add_junction(name, ChangeSet(value)) add_junction(name, ChangeSet(value))
# reservoir # reservoir
for value in reservoirs.values(): for value in _3_reservoirs.values():
if 'x' not in value: if 'x' not in value:
value['x'] = 0.0 value['x'] = 0.0
if 'y' not in value: if 'y' not in value:
@@ -235,7 +285,7 @@ def read_inp(name: str, inp: str):
add_reservoir(name, ChangeSet(value)) add_reservoir(name, ChangeSet(value))
# tank # tank
for value in tanks.values(): for value in _4_tanks.values():
if 'x' not in value: if 'x' not in value:
value['x'] = 0.0 value['x'] = 0.0
if 'y' not in value: if 'y' not in value:
@@ -243,33 +293,33 @@ def read_inp(name: str, inp: str):
add_tank(name, ChangeSet(value)) add_tank(name, ChangeSet(value))
# pipe # pipe
for value in pipes.values(): for value in _5_pipes.values():
add_pipe(name, ChangeSet(value)) add_pipe(name, ChangeSet(value))
# pump # pump
for value in pumps.values(): for value in _6_pumps.values():
add_pump(name, ChangeSet(value)) add_pump(name, ChangeSet(value))
# valve # valve
for value in valves.values(): for value in _7_valves.values():
add_valve(name, ChangeSet(value)) add_valve(name, ChangeSet(value))
# demand # demand
for key, value in demands.items(): for key, value in _9_demands.items():
set_demand(name, ChangeSet({'junction': key, 'demands': value})) set_demand(name, ChangeSet({'junction': key, 'demands': value}))
# status # status
for key, value in status.items(): for key, value in _10_status.items():
set_status(name, ChangeSet({'link': key} | value)) set_status(name, ChangeSet({'link': key} | value))
# emitter # emitter
for key, value in emitters.items(): for key, value in _16_emitters.items():
set_emitter(name, ChangeSet({'junction': key, 'coefficient': value})) set_emitter(name, ChangeSet({'junction': key, 'coefficient': value}))
# time # time
set_time(name, ChangeSet(times)) set_time(name, ChangeSet(_21_times))
# option # option
set_option(name, ChangeSet(options)) set_option(name, ChangeSet(_22_options))
close_project(name) close_project(name)