Skip to content

Commit

Permalink
Update plot during simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
t-sommer committed Sep 16, 2024
1 parent 1ebad83 commit 4dd2e47
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 29 deletions.
58 changes: 39 additions & 19 deletions fmusim-gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,19 @@ MainWindow::MainWindow(QWidget *parent)

simulationThread = new SimulationThread(this);

progressDialog = new QProgressDialog(this);

progressDialog->setWindowTitle("FMUSim");
progressDialog->setLabelText("Simulating...");
progressDialog->setRange(0, 100);
progressDialog->setMinimumDuration(1000);
progressDialog->setWindowModality(Qt::WindowModal);

Qt::WindowFlags flags = progressDialog->windowFlags();
Qt::WindowFlags closeFlag = Qt::WindowCloseButtonHint;
flags = flags & (~closeFlag);
progressDialog->setWindowFlags(flags);
progressDialog->reset();

connect(simulationThread, &SimulationThread::progressChanged, progressDialog, &QProgressDialog::setValue);
connect(simulationThread, &SimulationThread::finished, progressDialog, &QProgressDialog::reset);
connect(simulationThread, &SimulationThread::plotChanged, this, &MainWindow::runPlotScript);
connect(simulationThread, &SimulationThread::finished, this, &MainWindow::simulationFinished);
connect(progressDialog, &QProgressDialog::canceled, simulationThread, &SimulationThread::stop);
connect(this, &MainWindow::stopSimulationRequested, simulationThread, &SimulationThread::stop);
connect(this, &MainWindow::plotVariablesChanged, simulationThread, &SimulationThread::setPlotVariables);

buildPlatformBinaryThread = new BuildPlatformBinaryThread(this);

buildPlatformBinaryProgressDialog = new QProgressDialog(this);

Qt::WindowFlags flags = buildPlatformBinaryProgressDialog->windowFlags();
Qt::WindowFlags closeFlag = Qt::WindowCloseButtonHint;
flags = flags & (~closeFlag);

buildPlatformBinaryProgressDialog->setWindowTitle("FMUSim");
buildPlatformBinaryProgressDialog->setLabelText("Building Platform Binary...");
buildPlatformBinaryProgressDialog->setRange(0, 0);
Expand Down Expand Up @@ -536,6 +526,11 @@ void MainWindow::setColorScheme(Qt::ColorScheme colorScheme) {

void MainWindow::simulate() {

if (simulationThread->isRunning()) {
emit stopSimulationRequested();
return;
}

ui->logPlainTextEdit->clear();

double stopTime = stopTimeLineEdit->text().toDouble();
Expand All @@ -547,6 +542,7 @@ void MainWindow::simulate() {
outputInterval = stopTime / ui->maxSamplesLineEdit->text().toDouble();
}

simulationThread->setPlotVariables(plotVariables);
simulationThread->logFMICalls = ui->logFMICallsCheckBox->isChecked();

const QString logLevel = ui->logLevelComboBox->currentText();
Expand Down Expand Up @@ -624,6 +620,10 @@ void MainWindow::simulate() {
simulationThread->inputFilename = "";
}

ui->simulateAction->setIcon(QIcon(":/buttons/dark/stop.svg"));

setCurrentPage(ui->plotPage);

simulationThread->start();
}

Expand Down Expand Up @@ -744,16 +744,34 @@ void MainWindow::unloadFMU() {
interfaceTypeComboBox->setEnabled(false);
}

void MainWindow::runPlotScript(QString javaScript) {
ui->plotWebEngineView->page()->runJavaScript(javaScript);
}

void MainWindow::addPlotVariable(const FMIModelVariable* variable) {

plotVariables.append(variable);

variablesListModel->setPlotVariables(&plotVariables);
updatePlot();

emit plotVariablesChanged(plotVariables);

if (!simulationThread->isRunning()) {
updatePlot();
}
}

void MainWindow::removePlotVariable(const FMIModelVariable* variable) {

plotVariables.removeAll(variable);

variablesListModel->setPlotVariables(&plotVariables);
updatePlot();

emit plotVariablesChanged(plotVariables);

if (!simulationThread->isRunning()) {
updatePlot();
}
}

void MainWindow::setOptionalColumnsVisible(bool visible) {
Expand All @@ -776,6 +794,8 @@ void MainWindow::setOptionalColumnsVisible(bool visible) {

void MainWindow::simulationFinished() {

ui->simulateAction->setIcon(QIcon(":/buttons/dark/play.svg"));

ui->logPlainTextEdit->setPlainText(simulationThread->messages.join('\n'));

updatePlot();
Expand Down
8 changes: 7 additions & 1 deletion fmusim-gui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class MainWindow : public QMainWindow
ModelVariablesTreeModel* modelVariablesTreeModel = nullptr;
SimulationThread* simulationThread = nullptr;
BuildPlatformBinaryThread* buildPlatformBinaryThread = nullptr;
QProgressDialog* progressDialog;
QProgressDialog* buildPlatformBinaryProgressDialog;

static MainWindow* currentMainWindow;
Expand All @@ -70,6 +69,9 @@ class MainWindow : public QMainWindow
void updatePlot();
void unloadFMU();

public slots:
void runPlotScript(QString javaScript);

private slots:
void openFile();
void selectInputFile();
Expand All @@ -83,5 +85,9 @@ private slots:
void buildPlatformBinary();
void showModelVariablesListView(bool show);

signals:
void stopSimulationRequested();
void plotVariablesChanged(QList<const FMIModelVariable*> plotVariables);

};
#endif // MAINWINDOW_H
22 changes: 17 additions & 5 deletions fmusim-gui/SimulationThread.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <QDateTime>
#include "SimulationThread.h"
#include "PlotUtil.h"


SimulationThread* SimulationThread::currentSimulationThread = nullptr;
Expand Down Expand Up @@ -60,7 +61,8 @@ void SimulationThread::run() {

emit progressChanged(0);

const qint64 startTime = QDateTime::currentMSecsSinceEpoch();
startTime = QDateTime::currentMSecsSinceEpoch();
nextPlotTime = startTime;

char platformBinaryPath[2048] = "";

Expand Down Expand Up @@ -106,14 +108,12 @@ void SimulationThread::run() {
}
}

FMIRecorder* initialRecorder = FMICreateRecorder(S, modelDescription->nModelVariables, (const FMIModelVariable**)modelDescription->modelVariables);
settings->initialRecorder = FMICreateRecorder(S, modelDescription->nModelVariables, (const FMIModelVariable**)modelDescription->modelVariables);

FMIRecorder* recorder = FMICreateRecorder(S, recordedVariables.size(), (const FMIModelVariable**)recordedVariables.data());
settings->recorder = FMICreateRecorder(S, recordedVariables.size(), (const FMIModelVariable**)recordedVariables.data());

settings->S = S;
settings->modelDescription = modelDescription;
settings->initialRecorder = initialRecorder;
settings->recorder = recorder;
settings->input = input;

status = FMISimulate(settings);
Expand All @@ -127,6 +127,10 @@ void SimulationThread::stop() {
continueSimulation = false;
}

void SimulationThread::setPlotVariables(QList<const FMIModelVariable *> plotVariables) {
this->plotVariables = plotVariables;
}

bool SimulationThread::stepFinished(const FMISimulationSettings* settings, double time) {

SimulationThread* simulationThread = static_cast<SimulationThread*>(settings->userData);
Expand All @@ -135,6 +139,14 @@ bool SimulationThread::stepFinished(const FMISimulationSettings* settings, doubl

emit simulationThread->progressChanged(progress);

const qint64 currentTime = QDateTime::currentMSecsSinceEpoch();

if (currentTime >= simulationThread->nextPlotTime) {
const QString plot = PlotUtil::createPlot(settings->initialRecorder, settings->recorder, simulationThread->plotVariables, Qt::ColorScheme::Dark);
emit simulationThread->plotChanged(plot);
simulationThread->nextPlotTime = currentTime + simulationThread->plotInterval;
}

return simulationThread->continueSimulation;
}

Expand Down
6 changes: 6 additions & 0 deletions fmusim-gui/SimulationThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class SimulationThread : public QThread
Q_OBJECT

public:
qint64 plotInterval = 1000;
FMIStatus logLevel = FMIOK;
bool logFMICalls = false;
FMIInterfaceType interfaceType;
Expand All @@ -30,15 +31,20 @@ class SimulationThread : public QThread

public slots:
void stop();
void setPlotVariables(QList<const FMIModelVariable*> plotVariables);

private:
qint64 startTime;
qint64 nextPlotTime;
QList<const FMIModelVariable*> plotVariables;
static SimulationThread* currentSimulationThread;
static void appendMessage(const char* message, va_list args);
static bool stepFinished(const FMISimulationSettings* settings, double time);
bool continueSimulation = true;

signals:
void progressChanged(int progress);
void plotChanged(QString);

};

Expand Down
Binary file modified fmusim-gui/resources/buttons/buttons.afdesign
Binary file not shown.
4 changes: 2 additions & 2 deletions fmusim-gui/resources/buttons/dark/stop.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions fmusim-gui/resources/buttons/light/stop.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4dd2e47

Please sign in to comment.