mirror of https://gitlab.com/pamhyr/pamhyr2
Solver: Add custom command line parameters and solver default command line args.
parent
cd07f3aa8d
commit
d58a509c1d
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from tools import flatten
|
||||||
|
|
||||||
from Model.DB import SQLSubModel
|
from Model.DB import SQLSubModel
|
||||||
|
|
||||||
from Model.Network.Node import Node
|
from Model.Network.Node import Node
|
||||||
|
|
@ -382,3 +384,16 @@ class River(Graph, SQLSubModel):
|
||||||
|
|
||||||
def set_current_reach(self, reach):
|
def set_current_reach(self, reach):
|
||||||
self._current_reach = reach
|
self._current_reach = reach
|
||||||
|
|
||||||
|
def has_sediment(self):
|
||||||
|
has = len(self._sediment_layers) != 0
|
||||||
|
has &= any(
|
||||||
|
filter(
|
||||||
|
lambda p: p.sl != None,
|
||||||
|
flatten(
|
||||||
|
map(lambda e: e.reach.profiles, self.edges())
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return has
|
||||||
|
|
|
||||||
|
|
@ -167,6 +167,17 @@ class SolverParametersList(SQLSubModel):
|
||||||
def get(self, index):
|
def get(self, index):
|
||||||
return self._parameters[index]
|
return self._parameters[index]
|
||||||
|
|
||||||
|
def get_by_key(self, key):
|
||||||
|
try:
|
||||||
|
return next(
|
||||||
|
filter(
|
||||||
|
lambda p: p["name"] == key,
|
||||||
|
self._parameters
|
||||||
|
)
|
||||||
|
)["value"]
|
||||||
|
except:
|
||||||
|
return None
|
||||||
|
|
||||||
def set(self, index, new):
|
def set(self, index, new):
|
||||||
self._parameters[index] = new
|
self._parameters[index] = new
|
||||||
self._status.modified()
|
self._status.modified()
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,7 @@ class AbstractSolver(object):
|
||||||
("all_init_time", "000:00:00:00"),
|
("all_init_time", "000:00:00:00"),
|
||||||
("all_final_time", "999:99:00:00"),
|
("all_final_time", "999:99:00:00"),
|
||||||
("all_timestep", "300.0"),
|
("all_timestep", "300.0"),
|
||||||
|
("all_command_line_arguments", ""),
|
||||||
]
|
]
|
||||||
|
|
||||||
return lst
|
return lst
|
||||||
|
|
@ -153,6 +154,17 @@ class AbstractSolver(object):
|
||||||
def export(self, study, repertory, qlog = None):
|
def export(self, study, repertory, qlog = None):
|
||||||
raise NotImplementedMethodeError(self, self.export)
|
raise NotImplementedMethodeError(self, self.export)
|
||||||
|
|
||||||
|
def cmd_args(self, study):
|
||||||
|
"""Return solver command line arguments list
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Command line arguments list
|
||||||
|
"""
|
||||||
|
params = study.river.get_params(self.type)
|
||||||
|
args = params.get_by_key("all_command_line_arguments")
|
||||||
|
|
||||||
|
return args.split(" ")
|
||||||
|
|
||||||
def input_param(self):
|
def input_param(self):
|
||||||
"""Return input command line parameter(s)
|
"""Return input command line parameter(s)
|
||||||
|
|
||||||
|
|
@ -190,7 +202,7 @@ class AbstractSolver(object):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def _format_command(self, cmd, path = ""):
|
def _format_command(self, study, cmd, path = ""):
|
||||||
"""Format command line
|
"""Format command line
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -206,6 +218,7 @@ class AbstractSolver(object):
|
||||||
cmd = cmd.replace("@path", path.replace(" ", "\ "))
|
cmd = cmd.replace("@path", path.replace(" ", "\ "))
|
||||||
cmd = cmd.replace("@input", self.input_param())
|
cmd = cmd.replace("@input", self.input_param())
|
||||||
cmd = cmd.replace("@dir", self._process.workingDirectory())
|
cmd = cmd.replace("@dir", self._process.workingDirectory())
|
||||||
|
cmd = cmd.replace("@args", " ".join(self.cmd_args(study)))
|
||||||
|
|
||||||
logger.debug(f"! {cmd}")
|
logger.debug(f"! {cmd}")
|
||||||
|
|
||||||
|
|
@ -233,13 +246,13 @@ class AbstractSolver(object):
|
||||||
logger.info(f"! {exe} {args}")
|
logger.info(f"! {exe} {args}")
|
||||||
return exe, args
|
return exe, args
|
||||||
|
|
||||||
def run_input_data_fomater(self):
|
def run_input_data_fomater(self, study):
|
||||||
if self._cmd_input == "":
|
if self._cmd_input == "":
|
||||||
self._run_next()
|
self._run_next(study)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
cmd = self._cmd_input
|
cmd = self._cmd_input
|
||||||
exe, args = self._format_command(cmd, self._path_input)
|
exe, args = self._format_command(study, cmd, self._path_input)
|
||||||
|
|
||||||
if not os.path.exists(exe):
|
if not os.path.exists(exe):
|
||||||
error = f"[ERROR] Path {exe} do not exists"
|
error = f"[ERROR] Path {exe} do not exists"
|
||||||
|
|
@ -252,13 +265,13 @@ class AbstractSolver(object):
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def run_solver(self):
|
def run_solver(self, study):
|
||||||
if self._cmd_solver == "":
|
if self._cmd_solver == "":
|
||||||
self._run_next()
|
self._run_next(study)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
cmd = self._cmd_solver
|
cmd = self._cmd_solver
|
||||||
exe, args = self._format_command(cmd, self._path_solver)
|
exe, args = self._format_command(study, cmd, self._path_solver)
|
||||||
|
|
||||||
if not os.path.exists(exe):
|
if not os.path.exists(exe):
|
||||||
error = f"[ERROR] Path {exe} do not exists"
|
error = f"[ERROR] Path {exe} do not exists"
|
||||||
|
|
@ -272,13 +285,13 @@ class AbstractSolver(object):
|
||||||
self._status = STATUS.RUNNING
|
self._status = STATUS.RUNNING
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def run_output_data_fomater(self):
|
def run_output_data_fomater(self, study):
|
||||||
if self._cmd_output == "":
|
if self._cmd_output == "":
|
||||||
self._run_next()
|
self._run_next(study)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
cmd = self._cmd_output
|
cmd = self._cmd_output
|
||||||
exe, args = self._format_command(cmd, self._path_output)
|
exe, args = self._format_command(study, cmd, self._path_output)
|
||||||
|
|
||||||
if not os.path.exists(exe):
|
if not os.path.exists(exe):
|
||||||
error = f"[ERROR] Path {exe} do not exists"
|
error = f"[ERROR] Path {exe} do not exists"
|
||||||
|
|
@ -297,29 +310,29 @@ class AbstractSolver(object):
|
||||||
for x in s.split('\n'):
|
for x in s.split('\n'):
|
||||||
self._output.put(x)
|
self._output.put(x)
|
||||||
|
|
||||||
def _run_next(self):
|
def _run_next(self, study):
|
||||||
self._step += 1
|
self._step += 1
|
||||||
if self._step < len(self._runs):
|
if self._step < len(self._runs):
|
||||||
res = self._runs[self._step]()
|
res = self._runs[self._step](study)
|
||||||
if res is not True:
|
if res is not True:
|
||||||
self._output.put(res)
|
self._output.put(res)
|
||||||
else:
|
else:
|
||||||
self._status = STATUS.STOPED
|
self._status = STATUS.STOPED
|
||||||
|
|
||||||
def _finished(self, exit_code, exit_status):
|
def _finished(self, study, exit_code, exit_status):
|
||||||
if self._output is not None:
|
if self._output is not None:
|
||||||
self._output.put(exit_code)
|
self._output.put(exit_code)
|
||||||
|
|
||||||
self._run_next()
|
self._run_next(study)
|
||||||
|
|
||||||
def run(self, process = None, output_queue = None):
|
def run(self, study, process = None, output_queue = None):
|
||||||
if process is not None:
|
if process is not None:
|
||||||
self._process = process
|
self._process = process
|
||||||
if output_queue is not None:
|
if output_queue is not None:
|
||||||
self._output = output_queue
|
self._output = output_queue
|
||||||
|
|
||||||
self._process.readyRead.connect(self._data_ready)
|
self._process.readyRead.connect(self._data_ready)
|
||||||
self._process.finished.connect(self._finished)
|
self._process.finished.connect(lambda c, s: self._finished(study, c, s))
|
||||||
|
|
||||||
self._runs = [
|
self._runs = [
|
||||||
self.run_input_data_fomater,
|
self.run_input_data_fomater,
|
||||||
|
|
@ -328,7 +341,7 @@ class AbstractSolver(object):
|
||||||
]
|
]
|
||||||
self._step = 0
|
self._step = 0
|
||||||
# Run first step
|
# Run first step
|
||||||
res = self._runs[0]()
|
res = self._runs[0](study)
|
||||||
if res is not True:
|
if res is not True:
|
||||||
self._output.put(res)
|
self._output.put(res)
|
||||||
|
|
||||||
|
|
@ -340,14 +353,14 @@ class AbstractSolver(object):
|
||||||
self._status = STATUS.STOPED
|
self._status = STATUS.STOPED
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def start(self, process = None):
|
def start(self, study, process = None):
|
||||||
if _signal:
|
if _signal:
|
||||||
if self._status == STATUS.PAUSED:
|
if self._status == STATUS.PAUSED:
|
||||||
os.kill(self._process.pid(), SIGCONT)
|
os.kill(self._process.pid(), SIGCONT)
|
||||||
self._status = STATUS.RUNNING
|
self._status = STATUS.RUNNING
|
||||||
return True
|
return True
|
||||||
|
|
||||||
self.run(process)
|
self.run(study, process)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def pause(self):
|
def pause(self):
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,14 @@ class Mage(AbstractSolver):
|
||||||
# Export #
|
# Export #
|
||||||
##########
|
##########
|
||||||
|
|
||||||
|
def cmd_args(self, study):
|
||||||
|
l = super(Mage, self).cmd_args(study)
|
||||||
|
|
||||||
|
l.append("-r")
|
||||||
|
l.append("-fp=1")
|
||||||
|
|
||||||
|
return l
|
||||||
|
|
||||||
def input_param(self):
|
def input_param(self):
|
||||||
return "0.REP"
|
return "0.REP"
|
||||||
|
|
||||||
|
|
@ -440,6 +448,14 @@ class Mage8(Mage):
|
||||||
# Export #
|
# Export #
|
||||||
##########
|
##########
|
||||||
|
|
||||||
|
def cmd_args(self, study):
|
||||||
|
l = super(Mage8, self).cmd_args(study)
|
||||||
|
|
||||||
|
if study.river.has_sediment():
|
||||||
|
l.append("-c=3")
|
||||||
|
|
||||||
|
return l
|
||||||
|
|
||||||
@timer
|
@timer
|
||||||
def _export_PAR(self, study, repertory, qlog = None):
|
def _export_PAR(self, study, repertory, qlog = None):
|
||||||
files = []
|
files = []
|
||||||
|
|
|
||||||
|
|
@ -147,6 +147,7 @@ class SolverLogWindow(ASubMainWindow, ListedSubWindow):
|
||||||
|
|
||||||
self._log(f" *** Run solver {self._solver.name}", color="blue")
|
self._log(f" *** Run solver {self._solver.name}", color="blue")
|
||||||
self._solver.run(
|
self._solver.run(
|
||||||
|
study,
|
||||||
process = self._process,
|
process = self._process,
|
||||||
output_queue = self._output
|
output_queue = self._output
|
||||||
)
|
)
|
||||||
|
|
@ -220,7 +221,7 @@ class SolverLogWindow(ASubMainWindow, ListedSubWindow):
|
||||||
|
|
||||||
self._log(" *** Start", color="blue")
|
self._log(" *** Start", color="blue")
|
||||||
self._results = None
|
self._results = None
|
||||||
self._solver.start(self._process)
|
self._solver.start(self._study, process = self._process)
|
||||||
|
|
||||||
self.find(QAction, "action_start").setEnabled(False)
|
self.find(QAction, "action_start").setEnabled(False)
|
||||||
if _signal:
|
if _signal:
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,7 @@ def init():
|
||||||
"all_init_time": _translate("SolverParameters", "Initial time (jj:hh:mm:ss)"),
|
"all_init_time": _translate("SolverParameters", "Initial time (jj:hh:mm:ss)"),
|
||||||
"all_final_time": _translate("SolverParameters", "Final time (jj:hh:mm:ss)"),
|
"all_final_time": _translate("SolverParameters", "Final time (jj:hh:mm:ss)"),
|
||||||
"all_timestep": _translate("SolverParameters", "Timestep (second)"),
|
"all_timestep": _translate("SolverParameters", "Timestep (second)"),
|
||||||
|
"all_command_line_arguments": _translate("SolverParameters", "Command line arguments"),
|
||||||
# Mage specific parameters
|
# Mage specific parameters
|
||||||
"mage_min_timestep": _translate("SolverParameters", "Minimum timestep (second)"),
|
"mage_min_timestep": _translate("SolverParameters", "Minimum timestep (second)"),
|
||||||
"mage_timestep_tra": _translate("SolverParameters", "Time step of writing on .TRA"),
|
"mage_timestep_tra": _translate("SolverParameters", "Time step of writing on .TRA"),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue