/
simulationworker.cpp
342 lines (232 loc) · 9.53 KB
/
simulationworker.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
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://gnu.org/licenses>.
*******************************************************************************/
//==============================================================================
// Simulation worker
//==============================================================================
#include "cellmlfileruntime.h"
#include "corecliutils.h"
#include "simulation.h"
#include "simulationworker.h"
//==============================================================================
#include <QElapsedTimer>
#include <QMutex>
#include <QThread>
//==============================================================================
namespace OpenCOR {
namespace SimulationSupport {
//==============================================================================
SimulationWorker::SimulationWorker(Simulation *pSimulation, QThread *pThread,
SimulationWorker *&pSelf) :
mSimulation(pSimulation),
mThread(pThread),
mRuntime(pSimulation->runtime()),
mSelf(pSelf)
{
}
//==============================================================================
bool SimulationWorker::isRunning() const
{
// Return whether our thread is running
return mThread->isRunning() && !mPaused;
}
//==============================================================================
bool SimulationWorker::isPaused() const
{
// Return whether our thread is paused
return mThread->isRunning() && mPaused;
}
//==============================================================================
double SimulationWorker::currentPoint() const
{
// Return our current point
return mThread->isRunning()?
mCurrentPoint:
mSimulation->data()->startingPoint();
}
//==============================================================================
void SimulationWorker::run()
{
// Let people know that we are running
emit running(false);
// Set up our ODE solver
auto odeSolver = static_cast<Solver::OdeSolver *>(mSimulation->data()->odeSolverInterface()->solverInstance());
// Set up our NLA solver, if needed
// Note: we unset it at the end of this method...
Solver::NlaSolver *nlaSolver = nullptr;
if (mRuntime->needNlaSolver()) {
nlaSolver = static_cast<Solver::NlaSolver *>(mSimulation->data()->nlaSolverInterface()->solverInstance());
Solver::setNlaSolver(mRuntime->address(), nlaSolver);
}
// Keep track of any error that might be reported by any of our solvers
mStopped = false;
mError = false;
connect(odeSolver, &Solver::OdeSolver::error,
this, &SimulationWorker::emitError);
if (nlaSolver != nullptr) {
connect(nlaSolver, &Solver::NlaSolver::error,
this, &SimulationWorker::emitError);
}
// Retrieve our simulation properties
double startingPoint = mSimulation->data()->startingPoint();
double endingPoint = mSimulation->data()->endingPoint();
double pointInterval = mSimulation->data()->pointInterval();
quint64 pointCounter = 0;
mCurrentPoint = startingPoint;
// Initialise our ODE solver
odeSolver->setProperties(mSimulation->data()->odeSolverProperties());
odeSolver->initialize(mCurrentPoint, mRuntime->statesCount(),
mSimulation->data()->constants(),
mSimulation->data()->rates(),
mSimulation->data()->states(),
mSimulation->data()->algebraic(),
mRuntime->computeRates());
// Initialise our NLA solver, if any
if (nlaSolver != nullptr) {
nlaSolver->setProperties(mSimulation->data()->nlaSolverProperties());
}
// Now, we are ready to compute our model, but only if no error has occurred
// so far
// Note: we use -1 as a way to indicate that something went wrong...
qint64 elapsedTime = 0;
if (!mError) {
// Start our timer
QElapsedTimer timer;
timer.start();
// Add our first point
mSimulation->results()->addPoint(mCurrentPoint);
// Our main work loop
// Note: for performance reasons, it is essential that the following
// loop doesn't emit any signal, be it directly or indirectly,
// unless it is to let people know that we are pausing or running.
// Indeed, the signal/slot mechanism adds a certain level of
// overhead and, here, we want things to be as fast as possible...
QMutex pausedMutex;
forever {
// Reinitialise our solver, if we have an NLA solver or if the model
// got reset
// Note: indeed, with a solver such as CVODE, we need to update our
// internals...
if ((nlaSolver != nullptr) || mReset) {
odeSolver->reinitialize(mCurrentPoint);
mReset = false;
}
// Determine our next point and compute our model up to it
++pointCounter;
odeSolver->solve(mCurrentPoint,
qMin(endingPoint,
startingPoint+pointCounter*pointInterval));
// Make sure that no error occurred
if (mError) {
break;
}
// Add our new point
mSimulation->results()->addPoint(mCurrentPoint);
// Some post-processing, if needed
if (qFuzzyCompare(mCurrentPoint, endingPoint) || mStopped) {
// We have reached our ending point or we have been asked to
// stop, so leave our main work loop
break;
}
// Delay things a bit, if needed
if (mSimulation->delay() != nullptr) {
Core::doNothing(mSimulation->delay(), &mStopped);
}
// Pause ourselves, if needed
if (mPaused) {
// We should be paused, so stop our timer
elapsedTime += timer.elapsed();
// Let people know that we are paused
emit paused();
// Actually pause ourselves
pausedMutex.lock();
mPausedCondition.wait(&pausedMutex);
pausedMutex.unlock();
// We are not paused anymore
mPaused = false;
// Let people know that we are running again
emit running(true);
// (Re)start our timer
timer.start();
}
}
// Retrieve the total elapsed time, should no error have occurred
if (!mError) {
elapsedTime += timer.elapsed();
}
}
// Delete our solver(s)
delete odeSolver;
if (nlaSolver != nullptr) {
delete nlaSolver;
Solver::unsetNlaSolver(mRuntime->address());
}
// Reset our simulation owner's knowledge of us
// Note: if we were to do it the Qt way, our simulation owner would have a
// slot for our done() signal, but we want our simulation owner to
// know as quickly as possible that we are done...
mSelf = nullptr;
// Let people know that we are done and give them the elapsed time
emit done(mError?-1:elapsedTime);
}
//==============================================================================
void SimulationWorker::pause()
{
// Pause ourselves, if we are currently running
if (isRunning()) {
mPaused = true;
}
}
//==============================================================================
void SimulationWorker::resume()
{
// Resume ourselves, if we are currently paused
if (isPaused()) {
mPausedCondition.wakeOne();
}
}
//==============================================================================
void SimulationWorker::stop()
{
// Stop ourselves, if we are currently running or paused
if (isRunning() || isPaused()) {
mStopped = true;
if (isPaused()) {
mPausedCondition.wakeOne();
}
}
}
//==============================================================================
void SimulationWorker::reset()
{
// Stop ourselves, if we are currently running or paused
if (isRunning() || isPaused()) {
mReset = true;
}
}
//==============================================================================
void SimulationWorker::emitError(const QString &pMessage)
{
// A solver error occurred, so keep track of it and let people know about
// it, but only if another error hasn't already been received
if (!mError) {
mError = true;
emit error(pMessage);
}
}
//==============================================================================
} // namespace SimulationSupport
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================