File: SimulatorTab.cpp

package info (click to toggle)
dyssol 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,184 kB
  • sloc: cpp: 53,870; sh: 85; python: 59; makefile: 11
file content (212 lines) | stat: -rw-r--r-- 7,160 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* Copyright (c) 2020, Dyssol Development Team.
 * Copyright (c) 2023, DyssolTEC GmbH.
 * All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#include "SimulatorTab.h"
#include "Flowsheet.h"
#include "DyssolStringConstants.h"
#include <QMessageBox>
#include <QDateTime>

CSimulatorTab::CSimulatorTab(CFlowsheet* _pFlowsheet, CSimulator* _pSimulator, QWidget* _parent)
	: CQtDialog{ _parent }
	, m_pFlowsheet{ _pFlowsheet }
	, m_pSimulator{ _pSimulator }
{
	ui.setupUi(this);

	SetHelpLink("001_ui/gui.html#sec-gui-tabs-simulator");
}

CSimulatorTab::~CSimulatorTab()
{
	m_simulationThread->RequestStop();
	m_simulationThread->deleteLater();
}

void CSimulatorTab::InitializeConnections() const
{
	connect(ui.lineEditTime,		          &QLineEdit::editingFinished, this, &CSimulatorTab::SetSimulationTime);
	connect(ui.buttonRun,			          &QPushButton::clicked,	   this, &CSimulatorTab::StartStopSimulation);
	connect(ui.buttonClearResults,	          &QPushButton::clicked,	   this, &CSimulatorTab::ClearSimulationResults);
	connect(ui.buttonClearRecycles,	          &QPushButton::clicked,	   this, &CSimulatorTab::ClearInitialRecycleStreams);
	connect(ui.buttonClearResultsAndRecycles, &QPushButton::clicked,	   this, &CSimulatorTab::ClearAll);

	connect(m_simulationThread,	&CSimulationThread::Finished,		this, &CSimulatorTab::SimulationFinished);
	connect(&m_logTimer,	    &QTimer::timeout,				this, &CSimulatorTab::UpdateLog);
}

void CSimulatorTab::setVisible(bool _visible)
{
	QWidget::setVisible(_visible);
	if (_visible)
		UpdateWholeView();
}

void CSimulatorTab::UpdateWholeView() const
{
	UpdateSimulationTime();
}

void CSimulatorTab::OnNewFlowsheet() const
{
	ClearLog();
}

void CSimulatorTab::SetSimulationTime()
{
	m_pFlowsheet->GetParameters()->EndSimulationTime(ui.lineEditTime->text().toDouble());
	UpdateSimulationTime();
	emit DataChanged();
}

void CSimulatorTab::StartStopSimulation()
{
	if (m_pSimulator->GetCurrentStatus() == ESimulatorState::RUNNING)
		AbortSimulation();
	else if (m_pSimulator->GetCurrentStatus() == ESimulatorState::IDLE)
	{
		// run timer
		const QDateTime now = QDateTime::currentDateTime();
		m_simulationTimer.start();

		// initialize GUI elements
		ui.buttonRun->setText(StrConst::ST_ButtonRunTextStop);
		ui.tableLog->SetItemNotEditable(EStatTable::STARTED_TIME, 0, now.toString("hh:mm:ss"));
		ui.textBrowserLog->clear();
		ui.textBrowserLog->append(QString::fromStdString(StrConst::ST_LogSimStart(now.toString("hh:mm:ss").toStdString(), now.toString("dd.MM.yyyy").toStdString())));
		ui.textBrowserLog->append(StrConst::ST_LogInitStart);

		// initialize flowsheet
		const std::string error = m_pFlowsheet->Initialize();
		if (!error.empty())
		{
			ui.textBrowserLog->setTextColor(QColor(Qt::red));
			ui.textBrowserLog->append(QString::fromStdString(error));
			ui.textBrowserLog->setTextColor(QColor(Qt::black));
			ui.buttonRun->setText(StrConst::ST_ButtonRunTextRun);
			return;
		}

		ui.textBrowserLog->append(StrConst::ST_LogInitFinish);

		emit DataChanged();

		// run simulation
		BlockUI(true);
		emit SimulatorStateChanged(ESimulatorState::RUNNING);
		m_simulationThread->Run();
		m_logTimer.start(100);
	}
}

void CSimulatorTab::AbortSimulation()
{
	// stop simulation
	m_simulationThread->RequestStop();

	// set the message to the log
	ui.textBrowserLog->setTextColor(QColor(Qt::red));
	ui.textBrowserLog->append(StrConst::ST_LogSimStopRequest);
	ui.textBrowserLog->setTextColor(QColor(Qt::black));
}

void CSimulatorTab::SimulationFinished()
{
	// stop simulation threads and timers
	const QDateTime now = QDateTime::currentDateTime();
	m_logTimer.stop();
	m_simulationThread->Stop();

	// update simulation log
	UpdateLog();
	if (m_simulationThread->WasAborted())
	{
		ui.textBrowserLog->setTextColor(QColor(Qt::red));
		ui.textBrowserLog->append(StrConst::ST_LogSimUserStop);
		ui.textBrowserLog->setTextColor(QColor(Qt::black));
	}

	// setup GUI elements
	ui.textBrowserLog->append(QString::fromStdString(StrConst::ST_LogSimFinishedTime(now.toString("hh:mm:ss").toStdString(), now.toString("dd.MM.yyyy").toStdString(), QString::number(m_simulationTimer.elapsed()/1000.).toStdString())));
	ui.buttonRun->setText(StrConst::ST_ButtonRunTextRun);

	BlockUI(false);
	emit SimulatorStateChanged(ESimulatorState::IDLE);
}

void CSimulatorTab::ClearSimulationResults()
{
	if (QMessageBox::question(this, StrConst::ST_TitleClearResults, StrConst::ST_QuestionClearResults, QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel) != QMessageBox::Yes)
		return;

	m_pFlowsheet->ClearSimulationResults();
	ClearLog();

	emit DataChanged();
}

void CSimulatorTab::ClearInitialRecycleStreams()
{
	if (QMessageBox::question(this, StrConst::ST_TitleClearRecycles, StrConst::ST_QuestionClearRecycles, QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel) != QMessageBox::Yes)
		return;

	m_pFlowsheet->GetCalculationSequence()->ClearInitialStreamsData();

	emit DataChanged();
}

void CSimulatorTab::ClearAll()
{
	if (QMessageBox::question(this, StrConst::ST_TitleClearAll, StrConst::ST_QuestionClearAll, QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel) != QMessageBox::Yes)
		return;

	m_pFlowsheet->ClearSimulationResults();
	m_pFlowsheet->GetCalculationSequence()->ClearInitialStreamsData();
	ClearLog();

	emit DataChanged();
}

void CSimulatorTab::ClearLog() const
{
	ui.textBrowserLog->clear();
	ui.tableLog->clearContents();
}

void CSimulatorTab::UpdateLog() const
{
	while (!m_pSimulator->m_log.EndOfLog())
	{
		switch (m_pSimulator->m_log.GetReadColor())
		{
		case CSimulatorLog::ELogColor::DEFAULT: ui.textBrowserLog->setTextColor(QColor(Qt::black));		break;
		case CSimulatorLog::ELogColor::RED:		ui.textBrowserLog->setTextColor(QColor(Qt::red));		break;
		case CSimulatorLog::ELogColor::ORANGE:	ui.textBrowserLog->setTextColor(QColor(255, 128, 0));	break;
		}
		ui.textBrowserLog->append(QString::fromStdString(m_pSimulator->m_log.Read()));
	}
	ui.textBrowserLog->setTextColor(QColor(Qt::black));

	const auto status = m_pSimulator->GetCurrentPartitionStatus();
	ui.tableLog->SetItemNotEditable(EStatTable::TIME_WIN_START,   0, status.dTWStart);
	ui.tableLog->SetItemNotEditable(EStatTable::TIME_WIN_END,     0, status.dTWEnd);
	ui.tableLog->SetItemNotEditable(EStatTable::TIME_WIN_LENGTH,  0, status.dTWLength);
	ui.tableLog->SetItemNotEditable(EStatTable::ITERATION_NUMBER, 0, status.iTWIterationFull);
	ui.tableLog->SetItemNotEditable(EStatTable::WINDOW_NUMBER,    0, status.iWindowNumber);
	ui.tableLog->SetItemNotEditable(EStatTable::UNIT_NAME,        0, m_pSimulator->m_unitName);
	ui.tableLog->SetItemNotEditable(EStatTable::ELAPSED_TIME,     0, QDateTime::fromTime_t(m_simulationTimer.elapsed() / 1000).toUTC().toString("hh:mm:ss"));
}

void CSimulatorTab::UpdateSimulationTime() const
{
	ui.lineEditTime->setText(QString::number(m_pFlowsheet->GetParameters()->endSimulationTime));
}

void CSimulatorTab::BlockUI(bool _block) const
{
	ui.buttonClearRecycles->setEnabled(!_block);
	ui.buttonClearResults->setEnabled(!_block);
	ui.buttonClearResultsAndRecycles->setEnabled(!_block);
	ui.lineEditTime->setEnabled(!_block);
}