Add reaction api and test

This commit is contained in:
WQY\qiong
2022-11-12 10:00:08 +08:00
parent 3d6d8995ef
commit 1631592b0a
6 changed files with 437 additions and 3 deletions

View File

@@ -65,6 +65,10 @@ from .s17_quality import get_quality_schema, get_quality, set_quality
from .s18_sources import SOURCE_TYPE_CONCEN, SOURCE_TYPE_MASS, SOURCE_TYPE_FLOWPACED, SOURCE_TYPE_SETPOINT
from .s18_sources import get_source_schema, get_source, set_source, add_source, delete_source
from .s19_reactions import get_global_reaction_schema, get_global_reaction, set_global_reaction
from .s19_reactions import get_pipe_reaction_schema, get_pipe_reaction, set_pipe_reaction
from .s19_reactions import get_tank_reaction_schema, get_tank_reaction, set_tank_reaction
from .s20_mixing import MIXING_MODEL_MIXED, MIXING_MODEL_2COMP, MIXING_MODEL_FIFO, MIXING_MODEL_LIFO
from .s20_mixing import get_mixing_schema, get_mixing, set_mixing, add_mixing, delete_mixing

View File

@@ -87,8 +87,8 @@ class PumpEnergy(object):
self.pattern = str(input['pattern']) if 'pattern' in input and input['pattern'] != None else None
self.effic = str(input['effic']) if 'effic' in input and input['effic'] != None else None
self.f_pump = f"'{self.pump}'"
self.f_type = f"'{self.type}'"
self.f_pump = f"'{self.pump}'"
self.f_price = self.price if self.price != None else 'null'
self.f_pattern = f"'{self.pattern}'" if self.pattern != None else 'null'
self.f_effic = f"'{self.effic}'" if self.effic != None else 'null'

194
api/s19_reactions.py Normal file
View File

@@ -0,0 +1,194 @@
from .operation import *
def get_global_reaction_schema(name: str) -> dict[str, dict[str, Any]]:
return { 'order_bulk' : {'type': 'float' , 'optional': True , 'readonly': False},
'order_wall' : {'type': 'float' , 'optional': True , 'readonly': False},
'order_tank' : {'type': 'float' , 'optional': True , 'readonly': False},
'global_bulk' : {'type': 'float' , 'optional': True , 'readonly': False},
'global_wall' : {'type': 'float' , 'optional': True , 'readonly': False},
'limiting_potential' : {'type': 'float' , 'optional': True , 'readonly': False},
'roughness_correlation' : {'type': 'float' , 'optional': True , 'readonly': False} }
def get_global_reaction(name: str) -> dict[str, Any]:
gr = read(name, f"select * from reactions_global")
d = {}
d['order_bulk'] = float(gr['order_bulk']) if gr['order_bulk'] != None else None
d['order_wall'] = float(gr['order_wall']) if gr['order_wall'] != None else None
d['order_tank'] = float(gr['order_tank']) if gr['order_tank'] != None else None
d['global_bulk'] = float(gr['global_bulk']) if gr['global_bulk'] != None else None
d['global_wall'] = float(gr['global_wall']) if gr['global_wall'] != None else None
d['limiting_potential'] = float(gr['limiting_potential']) if gr['limiting_potential'] != None else None
d['roughness_correlation'] = float(gr['roughness_correlation']) if gr['roughness_correlation'] != None else None
return d
class GlobalReaction(object):
def __init__(self, input: dict[str, Any]) -> None:
self.type = 'global_reaction'
self.order_bulk = float(input['order_bulk']) if 'order_bulk' in input and input['order_bulk'] != None else None
self.order_wall = float(input['order_wall']) if 'order_wall' in input and input['order_wall'] != None else None
self.order_tank = float(input['order_tank']) if 'order_tank' in input and input['order_tank'] != None else None
self.global_bulk = float(input['global_bulk']) if 'global_bulk' in input and input['global_bulk'] != None else None
self.global_wall = float(input['global_wall']) if 'global_wall' in input and input['global_wall'] != None else None
self.limiting_potential = float(input['limiting_potential']) if 'limiting_potential' in input and input['limiting_potential'] != None else None
self.roughness_correlation = float(input['roughness_correlation']) if 'roughness_correlation' in input and input['roughness_correlation'] != None else None
self.f_type = f"'{self.type}'"
self.f_order_bulk = self.order_bulk if self.order_bulk != None else 'null'
self.f_order_wall = self.order_wall if self.order_wall != None else 'null'
self.f_order_tank = self.order_tank if self.order_tank != None else 'null'
self.f_global_bulk = self.global_bulk if self.global_bulk != None else 'null'
self.f_global_wall = self.global_wall if self.global_wall != None else 'null'
self.f_limiting_potential = self.limiting_potential if self.limiting_potential != None else 'null'
self.f_roughness_correlation = self.roughness_correlation if self.roughness_correlation != None else 'null'
def as_dict(self) -> dict[str, Any]:
return { 'type': self.type, 'order_bulk': self.order_bulk, 'order_wall': self.order_wall, 'order_tank': self.order_tank, 'global_bulk': self.global_bulk, 'global_wall': self.global_wall, 'limiting_potential': self.limiting_potential, 'roughness_correlation': self.roughness_correlation }
def set_global_reaction_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
old = GlobalReaction(get_global_reaction(name))
raw_new = get_global_reaction(name)
new_dict = cs.operations[0]
schema = get_global_reaction_schema(name)
for key, value in schema.items():
if key in new_dict and not value['readonly']:
raw_new[key] = new_dict[key]
new = GlobalReaction(raw_new)
redo_sql = f"update reactions_global set order_bulk = {new.f_order_bulk}, order_wall = {new.f_order_wall}, order_tank = {new.f_order_tank}, global_bulk = {new.f_global_bulk}, global_wall = {new.f_global_wall}, limiting_potential = {new.f_limiting_potential}, roughness_correlation = {new.f_roughness_correlation} where _no = 0;"
undo_sql = f"update reactions_global set order_bulk = {old.f_order_bulk}, order_wall = {old.f_order_wall}, order_tank = {old.f_order_tank}, global_bulk = {old.f_global_bulk}, global_wall = {old.f_global_wall}, limiting_potential = {old.f_limiting_potential}, roughness_correlation = {old.f_roughness_correlation} where _no = 0;"
redo_cs = g_update_prefix | new.as_dict()
undo_cs = g_update_prefix | old.as_dict()
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
def set_global_reaction(name: str, cs: ChangeSet) -> ChangeSet:
return execute_command(name, set_global_reaction_cache(name, cs))
def get_pipe_reaction_schema(name: str) -> dict[str, dict[str, Any]]:
return { 'pipe' : {'type': 'str' , 'optional': False , 'readonly': True },
'bulk' : {'type': 'float' , 'optional': True , 'readonly': False},
'wall' : {'type': 'float' , 'optional': True , 'readonly': False} }
def get_pipe_reaction(name: str, pipe: str) -> dict[str, Any]:
d = {}
d['pipe'] = pipe
pr = try_read(name, f"select * from reactions_pipe_bulk where pipe = '{pipe}'")
d['bulk'] = float(pr['value']) if pr != None else None
pr = try_read(name, f"select * from reactions_pipe_wall where pipe = '{pipe}'")
d['wall'] = float(pr['value']) if pr != None else None
return d
class PipeReaction(object):
def __init__(self, input: dict[str, Any]) -> None:
self.type = 'pipe_reaction'
self.pipe = str(input['pipe'])
self.bulk = float(input['bulk']) if 'bulk' in input and input['bulk'] != None else None
self.wall = float(input['wall']) if 'wall' in input and input['wall'] != None else None
self.f_type = f"'{self.type}'"
self.f_pipe = f"'{self.pipe}'"
self.f_bulk = self.bulk if self.bulk != None else 'null'
self.f_wall = self.wall if self.wall != None else 'null'
def as_dict(self) -> dict[str, Any]:
return { 'type': self.type, 'pipe': self.pipe, 'bulk': self.bulk, 'wall': self.wall }
def set_pipe_reaction_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
old = PipeReaction(get_pipe_reaction(name, cs.operations[0]['pipe']))
raw_new = get_pipe_reaction(name, cs.operations[0]['pipe'])
new_dict = cs.operations[0]
schema = get_pipe_reaction_schema(name)
for key, value in schema.items():
if key in new_dict and not value['readonly']:
raw_new[key] = new_dict[key]
new = PipeReaction(raw_new)
redo_sql = f"delete from reactions_pipe_bulk where pipe = {new.f_pipe};\ndelete from reactions_pipe_wall where pipe = {new.f_pipe};"
if new.bulk != None:
redo_sql += f"\ninsert into reactions_pipe_bulk (pipe, value) values ({new.f_pipe}, {new.f_bulk});"
if new.wall != None:
redo_sql += f"\ninsert into reactions_pipe_wall (pipe, value) values ({new.f_pipe}, {new.f_wall});"
undo_sql = f"delete from reactions_pipe_bulk where pipe = {old.f_pipe};\ndelete from reactions_pipe_wall where pipe = {old.f_pipe};"
if old.bulk != None:
undo_sql += f"\ninsert into reactions_pipe_bulk (pipe, value) values ({old.f_pipe}, {old.f_bulk});"
if old.wall != None:
undo_sql += f"\ninsert into reactions_pipe_wall (pipe, value) values ({old.f_pipe}, {old.f_wall});"
redo_cs = g_update_prefix | new.as_dict()
undo_cs = g_update_prefix | old.as_dict()
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
def set_pipe_reaction(name: str, cs: ChangeSet) -> ChangeSet:
return execute_command(name, set_pipe_reaction_cache(name, cs))
def get_tank_reaction_schema(name: str) -> dict[str, dict[str, Any]]:
return { 'tank' : {'type': 'str' , 'optional': False , 'readonly': True },
'value' : {'type': 'float' , 'optional': True , 'readonly': False} }
def get_tank_reaction(name: str, tank: str) -> dict[str, Any]:
d = {}
d['tank'] = tank
pr = try_read(name, f"select * from reactions_tank where tank = '{tank}'")
d['value'] = float(pr['value']) if pr != None else None
return d
class TankReaction(object):
def __init__(self, input: dict[str, Any]) -> None:
self.type = 'tank_reaction'
self.tank = str(input['tank'])
self.value = float(input['value']) if 'value' in input and input['value'] != None else None
self.f_type = f"'{self.type}'"
self.f_tank = f"'{self.tank}'"
self.f_value = self.value if self.value != None else 'null'
def as_dict(self) -> dict[str, Any]:
return { 'type': self.type, 'tank': self.tank, 'value': self.value }
def set_tank_reaction_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
old = TankReaction(get_tank_reaction(name, cs.operations[0]['tank']))
raw_new = get_tank_reaction(name, cs.operations[0]['tank'])
new_dict = cs.operations[0]
schema = get_tank_reaction_schema(name)
for key, value in schema.items():
if key in new_dict and not value['readonly']:
raw_new[key] = new_dict[key]
new = TankReaction(raw_new)
redo_sql = f"delete from reactions_tank where tank = {new.f_tank};"
if new.value != None:
redo_sql += f"\ninsert into reactions_tank (tank, value) values ({new.f_tank}, {new.f_value});"
undo_sql = f"delete from reactions_tank where tank = {old.f_tank};"
if old.value != None:
undo_sql += f"\ninsert into reactions_tank (tank, value) values ({old.f_tank}, {old.f_value});"
redo_cs = g_update_prefix | new.as_dict()
undo_cs = g_update_prefix | old.as_dict()
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
def set_tank_reaction(name: str, cs: ChangeSet) -> ChangeSet:
return execute_command(name, set_tank_reaction_cache(name, cs))

View File

@@ -2,10 +2,18 @@
create table reactions_global
(
key text not null
, value numeric not null
_no integer primary key
, order_bulk numeric
, order_wall numeric
, order_tank numeric
, global_bulk numeric
, global_wall numeric
, limiting_potential numeric
, roughness_correlation numeric
);
insert into reactions_global values (0, null, null, null, null, null, null, null);
create table reactions_pipe_bulk
(
pipe varchar(32) primary key references pipes(id) not null

View File

@@ -2576,6 +2576,195 @@ class TestApi:
self.leave(p)
# 19 reaction
def test_global_reaction(self):
p = 'test_global_reaction'
self.enter(p)
gr = get_global_reaction(p)
assert gr['order_bulk'] == None
assert gr['order_wall'] == None
assert gr['order_tank'] == None
assert gr['global_bulk'] == None
assert gr['global_wall'] == None
assert gr['limiting_potential'] == None
assert gr['roughness_correlation'] == None
set_global_reaction(p, ChangeSet({ 'order_bulk' : 10.0 }))
gr = get_global_reaction(p)
assert gr['order_bulk'] == 10.0
assert gr['order_wall'] == None
assert gr['order_tank'] == None
assert gr['global_bulk'] == None
assert gr['global_wall'] == None
assert gr['limiting_potential'] == None
assert gr['roughness_correlation'] == None
set_global_reaction(p, ChangeSet({ 'order_bulk' : None }))
gr = get_global_reaction(p)
assert gr['order_bulk'] == None
assert gr['order_wall'] == None
assert gr['order_tank'] == None
assert gr['global_bulk'] == None
assert gr['global_wall'] == None
assert gr['limiting_potential'] == None
assert gr['roughness_correlation'] == None
self.leave(p)
def test_global_reaction_op(self):
p = 'test_global_reaction_op'
self.enter(p)
cs = set_global_reaction(p, ChangeSet({ 'order_bulk' : 10.0 })).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'global_reaction'
assert cs['order_bulk'] == 10.0
assert cs['order_wall'] == None
assert cs['order_tank'] == None
assert cs['global_bulk'] == None
assert cs['global_wall'] == None
assert cs['limiting_potential'] == None
assert cs['roughness_correlation'] == None
cs = execute_undo(p).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'global_reaction'
assert cs['order_bulk'] == None
assert cs['order_wall'] == None
assert cs['order_tank'] == None
assert cs['global_bulk'] == None
assert cs['global_wall'] == None
assert cs['limiting_potential'] == None
assert cs['roughness_correlation'] == None
cs = execute_redo(p).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'global_reaction'
assert cs['order_bulk'] == 10.0
assert cs['order_wall'] == None
assert cs['order_tank'] == None
assert cs['global_bulk'] == None
assert cs['global_wall'] == None
assert cs['limiting_potential'] == None
assert cs['roughness_correlation'] == None
self.leave(p)
def test_pipe_reaction(self):
p = 'test_pipe_reaction'
self.enter(p)
add_junction(p, ChangeSet({'id': 'j1', 'x': 0.0, 'y': 10.0, 'elevation': 20.0}))
add_junction(p, ChangeSet({'id': 'j2', 'x': 0.0, 'y': 10.0, 'elevation': 20.0}))
add_pipe(p, ChangeSet({'id': 'p0', 'node1': 'j1', 'node2': 'j2', 'length': 100.0, 'diameter': 10.0, 'roughness': 0.1, 'minor_loss': 0.5, 'status': PIPE_STATUS_OPEN }))
pp = get_pipe_reaction(p, 'p0')
assert pp['pipe'] == 'p0'
assert pp['bulk'] == None
assert pp['wall'] == None
set_pipe_reaction(p, ChangeSet({'pipe': 'p0', 'bulk': 10.0, 'wall': 20.0}))
pp = get_pipe_reaction(p, 'p0')
assert pp['pipe'] == 'p0'
assert pp['bulk'] == 10.0
assert pp['wall'] == 20.0
set_pipe_reaction(p, ChangeSet({'pipe': 'p0', 'bulk': None, 'wall': None}))
pp = get_pipe_reaction(p, 'p0')
assert pp['pipe'] == 'p0'
assert pp['bulk'] == None
assert pp['wall'] == None
self.leave(p)
def test_pipe_reaction_op(self):
p = 'test_pipe_reaction_op'
self.enter(p)
add_junction(p, ChangeSet({'id': 'j1', 'x': 0.0, 'y': 10.0, 'elevation': 20.0}))
add_junction(p, ChangeSet({'id': 'j2', 'x': 0.0, 'y': 10.0, 'elevation': 20.0}))
add_pipe(p, ChangeSet({'id': 'p0', 'node1': 'j1', 'node2': 'j2', 'length': 100.0, 'diameter': 10.0, 'roughness': 0.1, 'minor_loss': 0.5, 'status': PIPE_STATUS_OPEN }))
cs = set_pipe_reaction(p, ChangeSet({'pipe': 'p0', 'bulk': 10.0, 'wall': 20.0})).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'pipe_reaction'
assert cs['pipe'] == 'p0'
assert cs['bulk'] == 10.0
assert cs['wall'] == 20.0
cs = execute_undo(p).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'pipe_reaction'
assert cs['pipe'] == 'p0'
assert cs['bulk'] == None
assert cs['wall'] == None
cs = execute_redo(p).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'pipe_reaction'
assert cs['pipe'] == 'p0'
assert cs['bulk'] == 10.0
assert cs['wall'] == 20.0
self.leave(p)
def test_tank_reaction(self):
p = 'test_tank_reaction'
self.enter(p)
add_tank(p, ChangeSet({'id': 't0', 'x': 0.0, 'y': 10.0, 'elevation': 20.0, 'init_level': 1.0, 'min_level': 0.0, 'max_level': 2.0, 'diameter': 10.0, 'min_vol': 100.0, 'vol_curve': None, 'overflow': OVERFLOW_NO}))
pt = get_tank_reaction(p, 't0')
assert pt['tank'] == 't0'
assert pt['value'] == None
set_tank_reaction(p, ChangeSet({'tank': 't0', 'value': 10.0}))
pt = get_tank_reaction(p, 't0')
assert pt['tank'] == 't0'
assert pt['value'] == 10.0
set_tank_reaction(p, ChangeSet({'tank': 't0', 'value': None}))
pt = get_tank_reaction(p, 't0')
assert pt['tank'] == 't0'
assert pt['value'] == None
self.leave(p)
def test_tank_reaction_op(self):
p = 'test_tank_reaction_op'
self.enter(p)
add_tank(p, ChangeSet({'id': 't0', 'x': 0.0, 'y': 10.0, 'elevation': 20.0, 'init_level': 1.0, 'min_level': 0.0, 'max_level': 2.0, 'diameter': 10.0, 'min_vol': 100.0, 'vol_curve': None, 'overflow': OVERFLOW_NO}))
cs = set_tank_reaction(p, ChangeSet({'tank': 't0', 'value': 10.0})).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'tank_reaction'
assert cs['tank'] == 't0'
assert cs['value'] == 10.0
cs = execute_undo(p).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'tank_reaction'
assert cs['tank'] == 't0'
assert cs['value'] == None
cs = execute_redo(p).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'tank_reaction'
assert cs['tank'] == 't0'
assert cs['value'] == 10.0
self.leave(p)
# 20 mixing

View File

@@ -547,6 +547,38 @@ def delete_source(name: str, cs: ChangeSet) -> ChangeSet:
return api.delete_source(name, cs)
############################################################
# reaction 19.[REACTIONS]
############################################################
def get_global_reaction_schema(name: str) -> dict[str, dict[str, Any]]:
return api.get_global_reaction_schema(name)
def get_global_reaction(name: str) -> dict[str, Any]:
return api.get_global_reaction(name)
def set_global_reaction(name: str, cs: ChangeSet) -> ChangeSet:
return api.set_global_reaction(name, cs)
def get_pipe_reaction_schema(name: str) -> dict[str, dict[str, Any]]:
return api.get_pipe_reaction_schema(name)
def get_pipe_reaction(name: str, pipe: str) -> dict[str, Any]:
return api.get_pipe_reaction(name, pipe)
def set_pipe_reaction(name: str, cs: ChangeSet) -> ChangeSet:
return api.set_pipe_reaction(name, cs)
def get_tank_reaction_schema(name: str) -> dict[str, dict[str, Any]]:
return api.get_tank_reaction_schema(name)
def get_tank_reaction(name: str, tank: str) -> dict[str, Any]:
return api.get_tank_reaction(name, tank)
def set_tank_reaction(name: str, cs: ChangeSet) -> ChangeSet:
return api.set_tank_reaction(name, cs)
############################################################
# mixing 20.[MIXING]
############################################################
@@ -581,6 +613,13 @@ def set_time(name: str, cs: ChangeSet) -> ChangeSet:
return api.set_time(name, cs)
############################################################
# report 22.[REPORT]
############################################################
# hardcode...
############################################################
# option 23.[OPTIONS]
############################################################