Add api for simulation and dump output

This commit is contained in:
WQY\qiong
2022-11-19 18:41:49 +08:00
parent e2a0f6f95c
commit 80b88976a4
2 changed files with 30 additions and 5 deletions

View File

@@ -126,7 +126,7 @@ class Output:
return usages
def dump(self, cache: bool = False) -> str:
def dump(self, cache: bool = True) -> str:
data = {}
data |= { 'version' : self.version() }
data |= { 'net_size' : self.net_size() }
@@ -135,7 +135,7 @@ class Output:
data |= { 'element_name' : self.element_name() }
data |= { 'energy_usage' : self.energy_usage() }
if cache:
with open(self.opt + '.json', 'w') as f:
with open(self._path + '.json', 'w') as f:
json.dump(data, f)
return json.dumps(data)
@@ -174,10 +174,23 @@ def run_project(name: str) -> str:
return dump_output(opt)
def run_inp(inp: str) -> str:
return ''
def run_inp(name: str) -> str:
dir = os.path.dirname(os.getcwd())
exe = os.path.join(os.path.join(dir, 'epanet'), 'runepanet.exe')
inp = os.path.join(os.path.join(dir, 'inp'), name + '.inp')
rpt = os.path.join(os.path.join(dir, 'temp'), name + '.rpt')
opt = os.path.join(os.path.join(dir, 'temp'), name + '.opt')
command = f'{exe} {inp} {rpt} {opt}'
result = os.system(command)
if result != 0:
msg = f'Failed to run simulation for project [{name}]'
raise Exception(msg)
return dump_output(opt)
if __name__ == '__main__':
_verify_platform()
print(run_project('net3'))
print(run_inp('net3'))