修复求解器多参数无法启动的问题

master
禅元天道 2023-07-25 10:06:03 +08:00
parent 97798acba5
commit b4c4e7e064
1 changed files with 102 additions and 114 deletions

View File

@ -37,23 +37,31 @@
#include <QDebug> #include <QDebug>
#include "PythonModule/PyAgent.h" #include "PythonModule/PyAgent.h"
namespace SolverControl namespace SolverControl {
{
SolverControlBase::SolverControlBase(GUI::MainWindow *mainwindow, ConfigOption::SolverInfo *solver, ModelData::ModelDataBase *m, bool sonly) SolverControlBase::SolverControlBase(GUI::MainWindow* mainwindow,
: _mainWindow(mainwindow), _solver(solver), _model(m), _solveOnly(sonly) ConfigOption::SolverInfo* solver,
ModelData::ModelDataBase* m, bool sonly)
: _mainWindow(mainwindow)
, _solver(solver)
, _model(m)
, _solveOnly(sonly)
{ {
connect(&_process, SIGNAL(readyReadStandardOutput()), this, SLOT(readSolverOutput())); connect(&_process, SIGNAL(readyReadStandardOutput()), this, SLOT(readSolverOutput()));
connect(this, SIGNAL(sendMessage(QString)), _mainWindow, SIGNAL(printMessageToMessageWindow(QString))); connect(this, SIGNAL(sendMessage(QString)), _mainWindow,
connect(this, SIGNAL(solverStarted(QWidget *)), _mainWindow, SIGNAL(addProcessBarSig(QWidget *))); SIGNAL(printMessageToMessageWindow(QString)));
connect(_mainWindow, SIGNAL(stopSolve(QWidget *)), this, SLOT(stopSolver(QWidget *))); connect(this, SIGNAL(solverStarted(QWidget*)), _mainWindow,
connect(&_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus))); SIGNAL(addProcessBarSig(QWidget*)));
connect(_mainWindow, SIGNAL(stopSolve(QWidget*)), this, SLOT(stopSolver(QWidget*)));
connect(&_process, SIGNAL(finished(int, QProcess::ExitStatus)), this,
SLOT(processFinished(int, QProcess::ExitStatus)));
connect(this, SIGNAL(processFinish(int)), _mainWindow, SIGNAL(processFinished(int))); connect(this, SIGNAL(processFinish(int)), _mainWindow, SIGNAL(processFinished(int)));
connect(this, SIGNAL(openRealTime(Post::RealTimeWindowBase *, int)), _mainWindow, SIGNAL(openRealTimeWindowSig(Post::RealTimeWindowBase *, int))); connect(this, SIGNAL(openRealTime(Post::RealTimeWindowBase*, int)), _mainWindow,
SIGNAL(openRealTimeWindowSig(Post::RealTimeWindowBase*, int)));
} }
SolverControlBase::~SolverControlBase() SolverControlBase::~SolverControlBase()
{ {
if (_processBar != nullptr) if(_processBar != nullptr)
delete _processBar; delete _processBar;
} }
@ -67,48 +75,44 @@ namespace SolverControl
QString solverPath = _solver->getExePath(); QString solverPath = _solver->getExePath();
qDebug() << solverPath; qDebug() << solverPath;
QFileInfo info(solverPath); QFileInfo info(solverPath);
int id = _model->getID(); int id = _model->getID();
if ((!info.exists()) || (!info.isFile())) if((!info.exists()) || (!info.isFile())) {
{ QMessageBox::warning(nullptr, tr("Warning"),
QMessageBox::warning(nullptr, tr("Warning"), tr("Solver Path Error! Solve Path : %1").arg(solverPath)); tr("Solver Path Error! Solve Path : %1").arg(solverPath));
emit processFinish(id); emit processFinish(id);
return; return;
} }
bool ok = preProcess(); bool ok = preProcess();
if (!ok) if(!ok) {
{
QMessageBox::warning(nullptr, tr("Warning"), tr("Input file write failed !")); QMessageBox::warning(nullptr, tr("Warning"), tr("Input file write failed !"));
emit processFinish(id); emit processFinish(id);
return; return;
} }
QString oldDir = ""; QString oldDir = "";
QString startPath = _model->getPath(); QString startPath = _model->getPath();
if (_solver->getType() == ConfigOption::SelfDevelop) if(_solver->getType() == ConfigOption::SelfDevelop) {
{
QDir dir(startPath); QDir dir(startPath);
if (dir.exists() && (!startPath.isEmpty())) if(dir.exists() && (!startPath.isEmpty())) {
{
oldDir = QDir::currentPath(); oldDir = QDir::currentPath();
QDir::setCurrent(startPath); QDir::setCurrent(startPath);
} }
} }
_processBar = new ModuleBase::ProcessBar(_mainWindow, _description, false); _processBar = new ModuleBase::ProcessBar(_mainWindow, _description, false);
QString args = _solver->getParameter(); QString args = _solver->getParameter();
QRegExp regExp("%.*%"); QRegExp regExp("%.*%");
regExp.setMinimal(true); regExp.setMinimal(true);
// int pos = regExp.indexIn(args); // int pos = regExp.indexIn(args);
QStringList variables = regExp.capturedTexts(); QStringList variables = regExp.capturedTexts();
for (int i = 0; i < variables.size(); ++i) for(int i = 0; i < variables.size(); ++i) {
{
QString variable = variables.at(i); QString variable = variables.at(i);
QString va = variable.remove("%"); QString va = variable.remove("%");
if (va.toLower() == "modelpath") if(va.toLower() == "modelpath")
args.replace(variables.at(i), startPath); args.replace(variables.at(i), startPath);
// qDebug() << args; // qDebug() << args;
} }
@ -122,53 +126,49 @@ namespace SolverControl
// startProcess = startProcess + " " + _args; // startProcess = startProcess + " " + _args;
// qDebug() << startProcess; // qDebug() << startProcess;
_process.start(solverPath, QStringList(args)); _process.start(solverPath, args.split(" ", QString::SkipEmptyParts));
emit solverStarted(_processBar); emit solverStarted(_processBar);
ModelData::ModelDataBaseExtend *extend = dynamic_cast<ModelData::ModelDataBaseExtend *>(_model); ModelData::ModelDataBaseExtend* extend =
if (extend != nullptr && !_solveOnly) dynamic_cast<ModelData::ModelDataBaseExtend*>(_model);
{ if(extend != nullptr && !_solveOnly) {
QList<ConfigOption::PostCurve *> curves = extend->getMonitorCurves(); QList<ConfigOption::PostCurve*> curves = extend->getMonitorCurves();
if (curves.size() > 0) if(curves.size() > 0) {
{ int id = _model->getID();
int id = _model->getID(); Post::RealTimeWindowBase* w = new Post::RealTimeWindowBase(_mainWindow, id);
Post::RealTimeWindowBase *w = new Post::RealTimeWindowBase(_mainWindow, id); emit openRealTime(w, id);
emit openRealTime(w, id);
} }
} }
if (_solver->getType() == ConfigOption::SelfDevelop) if(_solver->getType() == ConfigOption::SelfDevelop) {
{ if(!oldDir.isEmpty())
if (!oldDir.isEmpty())
QDir::setCurrent(oldDir); QDir::setCurrent(oldDir);
} }
} }
void SolverControlBase::readSolverOutput() void SolverControlBase::readSolverOutput()
{ {
_outputBuffer = QString::fromLocal8Bit(_process.readAll()); _outputBuffer = QString::fromLocal8Bit(_process.readAll());
QString process = _solver->getProcessKeyWord() + "(\\d+)(?:\\s*)"; QString process = _solver->getProcessKeyWord() + "(\\d+)(?:\\s*)";
// qDebug() << process; // qDebug() << process;
QRegExp rx(process); QRegExp rx(process);
int pos = _outputBuffer.indexOf(rx); int pos = _outputBuffer.indexOf(rx);
if (pos > -1 && _processBar != nullptr) if(pos > -1 && _processBar != nullptr) {
{
QString s = rx.cap(1); QString s = rx.cap(1);
_processBar->setProcess(s.toInt()); _processBar->setProcess(s.toInt());
} }
emit sendMessage(_outputBuffer); emit sendMessage(_outputBuffer);
} }
void SolverControlBase::stopSolver(QWidget *w) void SolverControlBase::stopSolver(QWidget* w)
{ {
if (w != _processBar) if(w != _processBar)
return; return;
int id = -1; int id = -1;
if (_model != nullptr) if(_model != nullptr)
id = _model->getID(); id = _model->getID();
emit removeSolver(id); emit removeSolver(id);
if (!_processFinished) if(!_processFinished) {
{
_process.kill(); _process.kill();
} }
_processBar = nullptr; _processBar = nullptr;
@ -177,54 +177,49 @@ namespace SolverControl
void SolverControlBase::stopSolver() void SolverControlBase::stopSolver()
{ {
if (!_processFinished) if(!_processFinished) {
{
_process.kill(); _process.kill();
} }
} }
void SolverControlBase::startMPI(int nprocess) void SolverControlBase::startMPI(int nprocess)
{ {
QString solverPath = _solver->getExePath(); QString solverPath = _solver->getExePath();
QFileInfo info(solverPath); QFileInfo info(solverPath);
if ((!info.exists()) || (!info.isFile())) if((!info.exists()) || (!info.isFile())) {
{ QMessageBox::warning(nullptr, tr("Warning"),
QMessageBox::warning(nullptr, tr("Warning"), tr("Solver Path Error! Solve Path : %1").arg(solverPath)); tr("Solver Path Error! Solve Path : %1").arg(solverPath));
return; return;
} }
bool ok = preProcess(); bool ok = preProcess();
if (!ok) if(!ok) {
{
QMessageBox::warning(nullptr, tr("Warning"), tr("Input file write failed !")); QMessageBox::warning(nullptr, tr("Warning"), tr("Input file write failed !"));
return; return;
} }
QString oldDir = ""; QString oldDir = "";
QString startPath = _model->getPath(); QString startPath = _model->getPath();
if (_solver->getType() == ConfigOption::SelfDevelop) if(_solver->getType() == ConfigOption::SelfDevelop) {
{
QDir dir(startPath); QDir dir(startPath);
if (dir.exists() && (!startPath.isEmpty())) if(dir.exists() && (!startPath.isEmpty())) {
{
oldDir = QDir::currentPath(); oldDir = QDir::currentPath();
QDir::setCurrent(startPath); QDir::setCurrent(startPath);
} }
} }
_processBar = new ModuleBase::ProcessBar(_mainWindow, _description, false); _processBar = new ModuleBase::ProcessBar(_mainWindow, _description, false);
QString args = _solver->getParameter(); QString args = _solver->getParameter();
QRegExp regExp("%.*%"); QRegExp regExp("%.*%");
regExp.setMinimal(true); regExp.setMinimal(true);
// int pos = regExp.indexIn(args); // int pos = regExp.indexIn(args);
QStringList variables = regExp.capturedTexts(); QStringList variables = regExp.capturedTexts();
for (int i = 0; i < variables.size(); ++i) for(int i = 0; i < variables.size(); ++i) {
{
QString variable = variables.at(i); QString variable = variables.at(i);
QString va = variable.remove("%"); QString va = variable.remove("%");
if (va.toLower() == "modelpath") if(va.toLower() == "modelpath")
args.replace(variables.at(i), startPath); args.replace(variables.at(i), startPath);
// qDebug() << args; // qDebug() << args;
} }
@ -242,22 +237,20 @@ namespace SolverControl
_process.start(c); _process.start(c);
emit solverStarted(_processBar); emit solverStarted(_processBar);
ModelData::ModelDataBaseExtend *extend = dynamic_cast<ModelData::ModelDataBaseExtend *>(_model); ModelData::ModelDataBaseExtend* extend =
if (extend != nullptr && !_solveOnly) dynamic_cast<ModelData::ModelDataBaseExtend*>(_model);
{ if(extend != nullptr && !_solveOnly) {
QList<ConfigOption::PostCurve *> curves = extend->getMonitorCurves(); QList<ConfigOption::PostCurve*> curves = extend->getMonitorCurves();
if (curves.size() > 0) if(curves.size() > 0) {
{ int id = _model->getID();
int id = _model->getID(); Post::RealTimeWindowBase* w = new Post::RealTimeWindowBase(_mainWindow, id);
Post::RealTimeWindowBase *w = new Post::RealTimeWindowBase(_mainWindow, id); emit openRealTime(w, id);
emit openRealTime(w, id);
} }
} }
if (_solver->getType() == ConfigOption::SelfDevelop) if(_solver->getType() == ConfigOption::SelfDevelop) {
{ if(!oldDir.isEmpty())
if (!oldDir.isEmpty())
QDir::setCurrent(oldDir); QDir::setCurrent(oldDir);
} }
} }
@ -269,8 +262,8 @@ namespace SolverControl
void SolverControlBase::startSolverClear() void SolverControlBase::startSolverClear()
{ {
_solveOnly = true; _solveOnly = true;
_processBar = new ModuleBase::ProcessBar(_mainWindow, _description, false); _processBar = new ModuleBase::ProcessBar(_mainWindow, _description, false);
QString solverPath = _solver->getExePath(); QString solverPath = _solver->getExePath();
_process.start(solverPath, QStringList(_args)); _process.start(solverPath, QStringList(_args));
@ -284,33 +277,31 @@ namespace SolverControl
_processFinished = true; _processFinished = true;
int id = -1; int id = -1;
if (_model != nullptr) if(_model != nullptr)
id = _model->getID(); id = _model->getID();
emit processFinish(id); emit processFinish(id);
switch (exitStatus) switch(exitStatus) {
{ case QProcess::NormalExit:
case QProcess::NormalExit: emit sendMessage("************************************");
emit sendMessage("************************************"); emit sendMessage("***** Solving process finished *****");
emit sendMessage("***** Solving process finished *****"); emit sendMessage("************************************");
emit sendMessage("************************************");
break; break;
case QProcess::CrashExit: case QProcess::CrashExit:
emit sendMessage("************************************"); emit sendMessage("************************************");
emit sendMessage("***** Solving process Crashed ******"); emit sendMessage("***** Solving process Crashed ******");
emit sendMessage("************************************"); emit sendMessage("************************************");
break; break;
default: default:
emit sendMessage("***********************************"); emit sendMessage("***********************************");
emit sendMessage("***** Solving process Stopped *****"); emit sendMessage("***** Solving process Stopped *****");
emit sendMessage("***********************************"); emit sendMessage("***********************************");
break; break;
} }
if (_processBar != nullptr) if(_processBar != nullptr) {
{ if(_processBar->isBusy())
if (_processBar->isBusy())
_processBar->setProcessRange(0, 100); _processBar->setProcessRange(0, 100);
_processBar->setProcess(100); _processBar->setProcess(100);
} }
@ -320,17 +311,14 @@ namespace SolverControl
bool SolverControlBase::preProcess() bool SolverControlBase::preProcess()
{ {
if (_solveOnly) if(_solveOnly)
return true; return true;
bool istemp = _solver->isWriteTemplate(); bool istemp = _solver->isWriteTemplate();
if (istemp) if(istemp) {
{ QString te = _solver->getTemplate();
QString te = _solver->getTemplate();
QString path = _model->getPath(); QString path = _model->getPath();
return IO::SolverIO::replaceTemplate(te, path, _model); return IO::SolverIO::replaceTemplate(te, path, _model);
} } else {
else
{
QString format = _solver->getInputFormat(); QString format = _solver->getInputFormat();
return IO::SolverIO::writeInpFile(format, _model); return IO::SolverIO::writeInpFile(format, _model);
} }
@ -339,11 +327,11 @@ namespace SolverControl
bool SolverControlBase::postPorocess() bool SolverControlBase::postPorocess()
{ {
if (_solveOnly) if(_solveOnly)
return true; return true;
QString trans = _solver->getTransfer(); QString trans = _solver->getTransfer();
QString path = _model->getPath(); QString path = _model->getPath();
return IO::SolverIO::transformFile(trans, path); return IO::SolverIO::transformFile(trans, path);
} }
} } // namespace SolverControl