/
idassolver.cpp
477 lines (342 loc) · 16.5 KB
/
idassolver.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*******************************************************************************
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 <http://www.gnu.org/licenses/>.
*******************************************************************************/
//==============================================================================
// IDAS solver
//==============================================================================
#include "idassolver.h"
//==============================================================================
#include "idas/idas.h"
#include "idas/idas_direct.h"
#include "idas/idas_spils.h"
#include "sunlinsol/sunlinsol_band.h"
#include "sunlinsol/sunlinsol_dense.h"
#include "sunlinsol/sunlinsol_spbcgs.h"
#include "sunlinsol/sunlinsol_spgmr.h"
#include "sunlinsol/sunlinsol_sptfqmr.h"
//==============================================================================
namespace OpenCOR {
namespace IDASSolver {
//==============================================================================
int residualFunction(double pVoi, N_Vector pStates, N_Vector pRates,
N_Vector pResiduals, void *pUserData)
{
// Compute the residual function
IdasSolverUserData *userData = static_cast<IdasSolverUserData *>(pUserData);
double *rates = N_VGetArrayPointer(pRates);
double *states = N_VGetArrayPointer(pStates);
double *residuals = N_VGetArrayPointer(pResiduals);
userData->computeRootInformation()(pVoi, userData->constants(), rates,
userData->oldRates(), states,
userData->oldStates(),
userData->algebraic(),
userData->condVar());
userData->computeEssentialVariables()(pVoi, userData->constants(), rates,
userData->oldRates(), states,
userData->oldStates(),
userData->algebraic(),
userData->condVar());
userData->computeRates()(pVoi, userData->constants(), rates,
userData->oldRates(), states,
userData->oldStates(), userData->algebraic(),
userData->condVar(), residuals);
return 0;
}
//==============================================================================
int rootFindingFunction(double pVoi, N_Vector pStates, N_Vector pRates,
double *pRoots, void *pUserData)
{
// Compute the root finding function
IdasSolverUserData *userData = static_cast<IdasSolverUserData *>(pUserData);
userData->computeRootInformation()(pVoi, userData->constants(),
N_VGetArrayPointer(pRates),
userData->oldRates(),
N_VGetArrayPointer(pStates),
userData->oldStates(),
userData->algebraic(), pRoots);
return 0;
}
//==============================================================================
void errorHandler(int pErrorCode, const char *pModule, const char *pFunction,
char *pErrorMessage, void *pUserData)
{
Q_UNUSED(pModule);
Q_UNUSED(pFunction);
// Forward errors to our IdasSolver object
if (pErrorCode != IDA_WARNING)
static_cast<IdasSolver *>(pUserData)->emitError(pErrorMessage);
}
//==============================================================================
IdasSolverUserData::IdasSolverUserData(double *pConstants, double *pOldRates,
double *pOldStates, double *pAlgebraic,
double *pCondVar,
Solver::DaeSolver::ComputeRatesFunction pComputeRates,
Solver::DaeSolver::ComputeEssentialVariablesFunction pComputeEssentialVariables,
Solver::DaeSolver::ComputeRootInformationFunction pComputeRootInformation) :
mConstants(pConstants),
mOldRates(pOldRates),
mOldStates(pOldStates),
mAlgebraic(pAlgebraic),
mCondVar(pCondVar),
mComputeRates(pComputeRates),
mComputeEssentialVariables(pComputeEssentialVariables),
mComputeRootInformation(pComputeRootInformation)
{
}
//==============================================================================
double * IdasSolverUserData::constants() const
{
// Return our constants array
return mConstants;
}
//==============================================================================
double * IdasSolverUserData::oldRates() const
{
// Return our old rates array
return mOldRates;
}
//==============================================================================
double * IdasSolverUserData::oldStates() const
{
// Return our old states array
return mOldStates;
}
//==============================================================================
double * IdasSolverUserData::algebraic() const
{
// Return our algebraic array
return mAlgebraic;
}
//==============================================================================
double * IdasSolverUserData::condVar() const
{
// Return our condVar array
return mCondVar;
}
//==============================================================================
Solver::DaeSolver::ComputeRatesFunction IdasSolverUserData::computeRates() const
{
// Return our compute rates function
return mComputeRates;
}
//==============================================================================
Solver::DaeSolver::ComputeEssentialVariablesFunction IdasSolverUserData::computeEssentialVariables() const
{
// Return our compute essential variables function
return mComputeEssentialVariables;
}
//==============================================================================
Solver::DaeSolver::ComputeRootInformationFunction IdasSolverUserData::computeRootInformation() const
{
// Return our compute root information function
return mComputeRootInformation;
}
//==============================================================================
IdasSolver::IdasSolver() :
mSolver(0),
mRatesVector(0),
mStatesVector(0),
mMatrix(0),
mLinearSolver(0),
mUserData(0),
mInterpolateSolution(InterpolateSolutionDefaultValue)
{
}
//==============================================================================
IdasSolver::~IdasSolver()
{
// Make sure that the solver has been initialised
if (!mSolver)
return;
// Delete some internal objects
N_VDestroy_Serial(mRatesVector);
N_VDestroy_Serial(mStatesVector);
SUNLinSolFree(mLinearSolver);
SUNMatDestroy(mMatrix);
IDAFree(&mSolver);
delete mUserData;
}
//==============================================================================
void IdasSolver::initialize(const double &pVoiStart, const double &pVoiEnd,
const int &pRatesStatesCount,
const int &pCondVarCount, double *pConstants,
double *pRates, double *pStates, double *pAlgebraic,
double *pCondVar, ComputeRatesFunction pComputeRates,
ComputeEssentialVariablesFunction pComputeEssentialVariables,
ComputeRootInformationFunction pComputeRootInformation,
ComputeStateInformationFunction pComputeStateInformation)
{
// Check whether we need to initialise or reinitialise ourselves
if (!mSolver) {
// Retrieve our properties
double maximumStep = MaximumStepDefaultValue;
int maximumNumberOfSteps = MaximumNumberOfStepsDefaultValue;
QString linearSolver = LinearSolverDefaultValue;
int upperHalfBandwidth = UpperHalfBandwidthDefaultValue;
int lowerHalfBandwidth = LowerHalfBandwidthDefaultValue;
double relativeTolerance = RelativeToleranceDefaultValue;
double absoluteTolerance = AbsoluteToleranceDefaultValue;
if (mProperties.contains(MaximumStepId)) {
maximumStep = mProperties.value(MaximumStepId).toDouble();
} else {
emit error(tr("the \"Maximum step\" property value could not be retrieved"));
return;
}
if (mProperties.contains(MaximumNumberOfStepsId)) {
maximumNumberOfSteps = mProperties.value(MaximumNumberOfStepsId).toInt();
} else {
emit error(tr("the \"Maximum number of steps\" property value could not be retrieved"));
return;
}
if (mProperties.contains(LinearSolverId)) {
linearSolver = mProperties.value(LinearSolverId).toString();
if (!linearSolver.compare(BandedLinearSolver)) {
if (mProperties.contains(UpperHalfBandwidthId)) {
upperHalfBandwidth = mProperties.value(UpperHalfBandwidthId).toInt();
if ( (upperHalfBandwidth < 0)
|| (upperHalfBandwidth >= pRatesStatesCount)) {
emit error(tr("the \"Upper half-bandwidth\" property must have a value between 0 and %1").arg(pRatesStatesCount-1));
return;
}
} else {
emit error(tr("the \"Upper half-bandwidth\" property value could not be retrieved"));
return;
}
if (mProperties.contains(LowerHalfBandwidthId)) {
lowerHalfBandwidth = mProperties.value(LowerHalfBandwidthId).toInt();
if ( (lowerHalfBandwidth < 0)
|| (lowerHalfBandwidth >= pRatesStatesCount)) {
emit error(tr("the \"Lower half-bandwidth\" property must have a value between 0 and %1").arg(pRatesStatesCount-1));
return;
}
} else {
emit error(tr("the \"Lower half-bandwidth\" property value could not be retrieved"));
return;
}
}
} else {
emit error(tr("the \"Linear solver\" property value could not be retrieved"));
return;
}
if (mProperties.contains(RelativeToleranceId)) {
relativeTolerance = mProperties.value(RelativeToleranceId).toDouble();
if (relativeTolerance < 0) {
emit error(tr("the \"Relative tolerance\" property must have a value greater than or equal to 0"));
return;
}
} else {
emit error(tr("the \"Relative tolerance\" property value could not be retrieved"));
return;
}
if (mProperties.contains(AbsoluteToleranceId)) {
absoluteTolerance = mProperties.value(AbsoluteToleranceId).toDouble();
if (absoluteTolerance < 0) {
emit error(tr("the \"Absolute tolerance\" property must have a value greater than or equal to 0"));
return;
}
} else {
emit error(tr("the \"Absolute tolerance\" property value could not be retrieved"));
return;
}
if (mProperties.contains(InterpolateSolutionId)) {
mInterpolateSolution = mProperties.value(InterpolateSolutionId).toBool();
} else {
emit error(tr("the \"Interpolate solution\" property value could not be retrieved"));
return;
}
// Initialise our DAE solver
OpenCOR::Solver::DaeSolver::initialize(pVoiStart, pVoiEnd,
pRatesStatesCount, pCondVarCount,
pConstants, pRates, pStates,
pAlgebraic, pCondVar,
pComputeRates,
pComputeEssentialVariables,
pComputeRootInformation,
pComputeStateInformation);
// Create our states vector
mRatesVector = N_VMake_Serial(pRatesStatesCount, pRates);
mStatesVector = N_VMake_Serial(pRatesStatesCount, pStates);
// Create our IDAS solver
mSolver = IDACreate();
// Use our own error handler
IDASetErrHandlerFn(mSolver, errorHandler, this);
// Initialise our IDAS solver
IDAInit(mSolver, residualFunction, pVoiStart,
mStatesVector, mRatesVector);
IDARootInit(mSolver, pCondVarCount, rootFindingFunction);
//---OPENCOR--- NEED TO CHECK THAT THINGS WORK AS EXPECTED BY TRYING IT
// OUT ON A MODEL THAT NEEDS ROOT FINDING (E.G. THE
// SAUCERMAN MODEL)...
// Set our user data
mUserData = new IdasSolverUserData(pConstants, mOldRates, mOldStates,
pAlgebraic, pCondVar, pComputeRates,
pComputeEssentialVariables,
pComputeRootInformation);
IDASetUserData(mSolver, mUserData);
// Set our maximum step
IDASetMaxStep(mSolver, maximumStep);
// Set our maximum number of steps
IDASetMaxNumSteps(mSolver, maximumNumberOfSteps);
// Set our linear solver
if (!linearSolver.compare(DenseLinearSolver)) {
mMatrix = SUNDenseMatrix(pRatesStatesCount, pRatesStatesCount);
mLinearSolver = SUNDenseLinearSolver(mStatesVector, mMatrix);
IDADlsSetLinearSolver(mSolver, mLinearSolver, mMatrix);
} else if (!linearSolver.compare(BandedLinearSolver)) {
mMatrix = SUNBandMatrix(pRatesStatesCount,
upperHalfBandwidth, lowerHalfBandwidth,
upperHalfBandwidth+lowerHalfBandwidth);
mLinearSolver = SUNBandLinearSolver(mStatesVector, mMatrix);
IDADlsSetLinearSolver(mSolver, mLinearSolver, mMatrix);
} else if (!linearSolver.compare(GmresLinearSolver)) {
mLinearSolver = SUNSPGMR(mStatesVector, PREC_LEFT, 0);
IDASpilsSetLinearSolver(mSolver, mLinearSolver);
} else if (!linearSolver.compare(BiCgStabLinearSolver)) {
mLinearSolver = SUNSPBCGS(mStatesVector, PREC_LEFT, 0);
IDASpilsSetLinearSolver(mSolver, mLinearSolver);
} else {
mLinearSolver = SUNSPTFQMR(mStatesVector, PREC_LEFT, 0);
IDASpilsSetLinearSolver(mSolver, mLinearSolver);
}
// Set our relative and absolute tolerances
IDASStolerances(mSolver, relativeTolerance, absoluteTolerance);
} else {
// Reinitialise our IDAS object
IDAReInit(mSolver, pVoiStart, mStatesVector, mRatesVector);
}
// Compute our model's (new) initial conditions
double *id = new double[pRatesStatesCount];
pComputeStateInformation(id);
N_Vector idVector = N_VMake_Serial(pRatesStatesCount, id);
IDASetId(mSolver, idVector);
IDACalcIC(mSolver, IDA_YA_YDP_INIT, pVoiEnd);
IDAGetConsistentIC(mSolver, mStatesVector, mRatesVector);
N_VDestroy_Serial(idVector);
delete[] id;
}
//==============================================================================
void IdasSolver::solve(double &pVoi, const double &pVoiEnd) const
{
// Solve the model
if (!mInterpolateSolution)
IDASetStopTime(mSolver, pVoiEnd);
IDASolve(mSolver, pVoiEnd, &pVoi, mStatesVector, mRatesVector, IDA_NORMAL);
memcpy(mOldRates, N_VGetArrayPointer(mRatesVector), mRatesStatesCount*OpenCOR::Solver::SizeOfDouble);
memcpy(mOldStates, N_VGetArrayPointer(mStatesVector), mRatesStatesCount*OpenCOR::Solver::SizeOfDouble);
}
//==============================================================================
} // namespace IDASSolver
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================