-
Notifications
You must be signed in to change notification settings - Fork 298
/
SystemSimulationInformationDialog.cpp
356 lines (337 loc) · 15.6 KB
/
SystemSimulationInformationDialog.cpp
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
* OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
/*
* @author Adeel Asghar <adeel.asghar@liu.se>
*/
#include "SystemSimulationInformationDialog.h"
#include "Modeling/ModelWidgetContainer.h"
#include "Modeling/Commands.h"
#include <QGridLayout>
#include <QMessageBox>
SystemSimulationInformationWidget::SystemSimulationInformationWidget(ModelWidget *pModelWidget)
: QWidget(pModelWidget)
{
mpModelWidget = pModelWidget;
QIntValidator *pIntValidator = new QIntValidator(this);
QDoubleValidator *pDoubleValidator = new QDoubleValidator(this);
if (mpModelWidget->getLibraryTreeItem()->isTLMSystem()) {
// IP address
mpIpAddressLabel = new Label(tr("IP Adress:"));
mpIpAddressTextBox = new QLineEdit("127.0.1.1");
// manager port
mpManagerPortLabel = new Label(tr("Manager Port:"));
mpManagerPortTextBox = new QLineEdit("11117");
mpManagerPortTextBox->setValidator(pIntValidator);
// monitor port
mpMonitorPortLabel = new Label(tr("Monitor Port:"));
mpMonitorPortTextBox = new QLineEdit("12117");
mpMonitorPortTextBox->setValidator(pIntValidator);
}
if (mpModelWidget->getLibraryTreeItem()->isWCSystem() || mpModelWidget->getLibraryTreeItem()->isSCSystem()) {
// solver
mpSolverLabel = new Label(tr("Solver:"));
mpSolverComboBox = new QComboBox;
// fixed step size
mpFixedStepSizeLabel = new Label(tr("Fixed Step Size:"));
mpFixedStepSizeTextBox = new QLineEdit;
mpFixedStepSizeTextBox->setValidator(pDoubleValidator);
double fixedStepSize;
if (OMSProxy::instance()->getFixedStepSize(mpModelWidget->getLibraryTreeItem()->getNameStructure(), &fixedStepSize)) {
mpFixedStepSizeTextBox->setText(QString::number(fixedStepSize));
}
// initial step size
mpInitialStepSizeLabel = new Label(tr("Initial Step Size:"));
mpInitialStepSizeTextBox = new QLineEdit;
mpInitialStepSizeTextBox->setValidator(pDoubleValidator);
// minimum step size
mpMinimumStepSizeLabel = new Label(tr("Minimum Step Size:"));
mpMinimumStepSizeTextBox = new QLineEdit;
mpMinimumStepSizeTextBox->setValidator(pDoubleValidator);
// maximum step size
mpMaximumStepSizeLabel = new Label(tr("Maximum Step Size:"));
mpMaximumStepSizeTextBox = new QLineEdit;
mpMaximumStepSizeTextBox->setValidator(pDoubleValidator);
double initialStepSize, minimumStepSize, maximumStepSize;
if (OMSProxy::instance()->getVariableStepSize(mpModelWidget->getLibraryTreeItem()->getNameStructure(), &initialStepSize, &minimumStepSize, &maximumStepSize)) {
mpInitialStepSizeTextBox->setText(QString::number(initialStepSize));
mpMinimumStepSizeTextBox->setText(QString::number(minimumStepSize));
mpMaximumStepSizeTextBox->setText(QString::number(maximumStepSize));
}
// absolute tolerance
mpAbsoluteToleranceLabel = new Label("Absolute Tolerance:");
mpAbsoluteToleranceTextBox = new QLineEdit;
mpAbsoluteToleranceTextBox->setValidator(pDoubleValidator);
// relative tolerance
mpRelativeToleranceLabel = new Label("Relative Tolerance:");
mpRelativeToleranceTextBox = new QLineEdit;
mpRelativeToleranceTextBox->setValidator(pDoubleValidator);
double absoluteTolerance, relativeTolerance;
if (OMSProxy::instance()->getTolerance(mpModelWidget->getLibraryTreeItem()->getNameStructure(), &absoluteTolerance, &relativeTolerance)) {
mpAbsoluteToleranceTextBox->setText(QString::number(absoluteTolerance));
mpRelativeToleranceTextBox->setText(QString::number(relativeTolerance));
}
}
if (mpModelWidget->getLibraryTreeItem()->isWCSystem()) { // oms_system_wc
mpSolverComboBox->addItem("oms-ma", oms_solver_wc_ma);
mpSolverComboBox->addItem("oms-mav", oms_solver_wc_mav);
mpSolverComboBox->addItem("oms-mav-2", oms_solver_wc_mav2);
} else if (mpModelWidget->getLibraryTreeItem()->isSCSystem()) { // oms_system_sc
mpSolverComboBox->addItem("cvode", oms_solver_sc_cvode);
mpSolverComboBox->addItem("explicit-euler", oms_solver_sc_explicit_euler);
}
if (mpModelWidget->getLibraryTreeItem()->isWCSystem() || mpModelWidget->getLibraryTreeItem()->isSCSystem()) {
oms_solver_enu_t solver;
if (OMSProxy::instance()->getSolver(mpModelWidget->getLibraryTreeItem()->getNameStructure(), &solver)) {
int currentIndex = mpSolverComboBox->findData(solver);
if (currentIndex > -1) {
mpSolverComboBox->setCurrentIndex(currentIndex);
}
}
// update based on selected solver
connect(mpSolverComboBox, SIGNAL(currentIndexChanged(int)), SLOT(solverChanged(int)));
solverChanged(mpSolverComboBox->currentIndex());
}
// set the layout
QGridLayout *pMainLayout = new QGridLayout;
pMainLayout->setContentsMargins(0, 0, 0, 0);
pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
int row = 0;
if (mpModelWidget->getLibraryTreeItem()->isTLMSystem()) {
pMainLayout->addWidget(mpIpAddressLabel, row, 0);
pMainLayout->addWidget(mpIpAddressTextBox, row++, 1);
pMainLayout->addWidget(mpManagerPortLabel, row, 0);
pMainLayout->addWidget(mpManagerPortTextBox, row++, 1);
pMainLayout->addWidget(mpMonitorPortLabel, row, 0);
pMainLayout->addWidget(mpMonitorPortTextBox, row++, 1);
} else if (mpModelWidget->getLibraryTreeItem()->isWCSystem() || mpModelWidget->getLibraryTreeItem()->isSCSystem()) {
pMainLayout->addWidget(mpSolverLabel, row, 0);
pMainLayout->addWidget(mpSolverComboBox, row++, 1);
pMainLayout->addWidget(mpFixedStepSizeLabel, row, 0);
pMainLayout->addWidget(mpFixedStepSizeTextBox, row++, 1);
pMainLayout->addWidget(mpInitialStepSizeLabel, row, 0);
pMainLayout->addWidget(mpInitialStepSizeTextBox, row++, 1);
pMainLayout->addWidget(mpMinimumStepSizeLabel, row, 0);
pMainLayout->addWidget(mpMinimumStepSizeTextBox, row++, 1);
pMainLayout->addWidget(mpMaximumStepSizeLabel, row, 0);
pMainLayout->addWidget(mpMaximumStepSizeTextBox, row++, 1);
pMainLayout->addWidget(mpAbsoluteToleranceLabel, row, 0);
pMainLayout->addWidget(mpAbsoluteToleranceTextBox, row++, 1);
pMainLayout->addWidget(mpRelativeToleranceLabel, row, 0);
pMainLayout->addWidget(mpRelativeToleranceTextBox, row++, 1);
}
setLayout(pMainLayout);
}
/*!
* \brief SystemSimulationInformationWidget::solverChanged
* Slot activated when mpSolverComboBox currentIndexChanged SIGNAL is raised.
* Enables the step size controls based on the selected solver.
* \param index
*/
void SystemSimulationInformationWidget::solverChanged(int index)
{
oms_solver_enu_t solver = (oms_solver_enu_t)mpSolverComboBox->itemData(index).toInt();
switch (solver) {
case oms_solver_wc_mav:
case oms_solver_wc_mav2:
case oms_solver_sc_cvode:
mpInitialStepSizeTextBox->setEnabled(true);
mpMinimumStepSizeTextBox->setEnabled(true);
mpMaximumStepSizeTextBox->setEnabled(true);
mpFixedStepSizeTextBox->setEnabled(false);
break;
case oms_solver_wc_ma:
case oms_solver_sc_explicit_euler:
default:
mpInitialStepSizeTextBox->setEnabled(false);
mpMinimumStepSizeTextBox->setEnabled(false);
mpMaximumStepSizeTextBox->setEnabled(false);
mpFixedStepSizeTextBox->setEnabled(true);
break;
}
}
/*!
* \brief SystemSimulationInformationWidget::setSystemSimulationInformation
* Sets the simulation information of the system.
* \param pushOnStack
* \return
*/
bool SystemSimulationInformationWidget::setSystemSimulationInformation(bool pushOnStack)
{
if (mpModelWidget->getLibraryTreeItem()->isTLMSystem()) {
if (mpIpAddressTextBox->text().isEmpty()) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg("IP Address"), Helper::ok);
return false;
}
if (mpManagerPortTextBox->text().isEmpty()) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg("Manager Port"), Helper::ok);
return false;
}
if (mpMonitorPortTextBox->text().isEmpty()) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg("Monitor Port"), Helper::ok);
return false;
}
} else if (mpModelWidget->getLibraryTreeItem()->isWCSystem() || mpModelWidget->getLibraryTreeItem()->isSCSystem()) {
oms_solver_enu_t solver = (oms_solver_enu_t)mpSolverComboBox->itemData(mpSolverComboBox->currentIndex()).toInt();
switch (solver) {
case oms_solver_wc_mav:
case oms_solver_wc_mav2:
case oms_solver_sc_cvode:
if (mpInitialStepSizeTextBox->text().isEmpty()) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg("Initial Step Size"), Helper::ok);
return false;
}
if (mpMinimumStepSizeTextBox->text().isEmpty()) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg("Minimum Step Size"), Helper::ok);
return false;
}
if (mpMaximumStepSizeTextBox->text().isEmpty()) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg("Maximum Step Size"), Helper::ok);
return false;
}
break;
case oms_solver_wc_ma:
case oms_solver_sc_explicit_euler:
default:
if (mpFixedStepSizeTextBox->text().isEmpty()) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg("Fixed Step Size"), Helper::ok);
return false;
}
break;
}
if (mpAbsoluteToleranceTextBox->text().isEmpty()) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg("Absolute Tolerance"), Helper::ok);
return false;
}
if (mpRelativeToleranceTextBox->text().isEmpty()) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg("Relative Tolerance"), Helper::ok);
return false;
}
}
LibraryTreeItem *pLibraryTreeItem = mpModelWidget->getLibraryTreeItem();
const QString cref = pLibraryTreeItem->getNameStructure();
if (pLibraryTreeItem->isTLMSystem()) {
if (!OMSProxy::instance()->setTLMSocketData(cref, mpIpAddressTextBox->text(), mpManagerPortTextBox->text().toInt(), mpMonitorPortTextBox->text().toInt())) {
return false;
}
} else if (pLibraryTreeItem->isWCSystem() || pLibraryTreeItem->isSCSystem()) {
// set solver
oms_solver_enu_t solver = (oms_solver_enu_t)mpSolverComboBox->itemData(mpSolverComboBox->currentIndex()).toInt();
if (!OMSProxy::instance()->setSolver(cref, solver)) {
return false;
}
// set step size
switch (solver) {
case oms_solver_wc_mav:
case oms_solver_wc_mav2:
case oms_solver_sc_cvode:
if (!OMSProxy::instance()->setVariableStepSize(cref, mpInitialStepSizeTextBox->text().toDouble(), mpMinimumStepSizeTextBox->text().toDouble(),
mpMaximumStepSizeTextBox->text().toDouble())) {
return false;
}
break;
case oms_solver_wc_ma:
case oms_solver_sc_explicit_euler:
default:
if (!OMSProxy::instance()->setFixedStepSize(cref, mpFixedStepSizeTextBox->text().toDouble())) {
return false;
}
break;
}
// set tolerance
if (!OMSProxy::instance()->setTolerance(cref, mpAbsoluteToleranceTextBox->text().toDouble(), mpRelativeToleranceTextBox->text().toDouble())) {
return false;
}
}
// push on stack
if (pushOnStack) {
mpModelWidget->createOMSimulatorUndoCommand(QString("System %1 simulation information").arg(cref));
mpModelWidget->updateModelText();
}
return true;
}
/*!
* \class SystemSimulationInformationDialog
* \brief A dialog for system simulation information.
*/
/*!
* \brief SystemSimulationInformationDialog::SystemSimulationInformationDialog
* \param pModelWidget
*/
SystemSimulationInformationDialog::SystemSimulationInformationDialog(ModelWidget *pModelWidget)
: QDialog(pModelWidget)
{
setWindowTitle(QString("%1 - %2 - %3").arg(Helper::applicationName, Helper::systemSimulationInformation, pModelWidget->getLibraryTreeItem()->getName()));
setAttribute(Qt::WA_DeleteOnClose);
// set heading
mpHeading = Utilities::getHeadingLabel(QString("%1 - %2").arg(Helper::systemSimulationInformation, pModelWidget->getLibraryTreeItem()->getName()));
// set separator line
mpHorizontalLine = Utilities::getHeadingLine();
// system simulation information widget
mpSystemSimulationInformationWidget = new SystemSimulationInformationWidget(pModelWidget);
// buttons
mpOkButton = new QPushButton(Helper::ok);
mpOkButton->setAutoDefault(true);
mpOkButton->setEnabled(!pModelWidget->getLibraryTreeItem()->isSystemLibrary());
connect(mpOkButton, SIGNAL(clicked()), SLOT(setSystemSimulationInformation()));
mpCancelButton = new QPushButton(Helper::cancel);
mpCancelButton->setAutoDefault(false);
connect(mpCancelButton, SIGNAL(clicked()), SLOT(reject()));
// add buttons to the button box
mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
// set the layout
QGridLayout *pMainLayout = new QGridLayout;
pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
pMainLayout->addWidget(mpHeading, 0, 0, 1, 2);
pMainLayout->addWidget(mpHorizontalLine, 1, 0, 1, 2);
pMainLayout->addWidget(mpSystemSimulationInformationWidget, 2, 0, 1, 2);
pMainLayout->addWidget(mpButtonBox, 3, 0, 1, 2, Qt::AlignRight);
setLayout(pMainLayout);
}
/*!
* \brief SystemSimulationInformationDialog::setSystemSimulationInformation
* Sets the simulation information of the system.
*/
void SystemSimulationInformationDialog::setSystemSimulationInformation()
{
if (mpSystemSimulationInformationWidget->setSystemSimulationInformation(true)) {
accept();
}
}