| 1 |
/****************************************************************************** |
| 2 |
|
| 3 |
This source file is part of the Avogadro project. |
| 4 |
|
| 5 |
Copyright 2010 Eric C. Brown |
| 6 |
|
| 7 |
This source code is released under the New BSD License, (the "License"). |
| 8 |
|
| 9 |
Unless required by applicable law or agreed to in writing, software |
| 10 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
See the License for the specific language governing permissions and |
| 13 |
limitations under the License. |
| 14 |
|
| 15 |
******************************************************************************/ |
| 16 |
|
| 17 |
/* |
| 18 |
From [email protected] Wed Apr 24 01:35:52 1991 |
| 19 |
Return-Path: |
| 20 |
Date: Wed, 24 Apr 91 03:35:24 CDT |
| 21 |
From: [email protected] |
| 22 |
To: [email protected] |
| 23 |
Subject: lsoda.c |
| 24 |
Cc: [email protected] |
| 25 |
|
| 26 |
|
| 27 |
I'm told by Steve Nichols at Georgia Tech that you are interested in |
| 28 |
a stiff integrator. Here's a translation of the fortran code LSODA. |
| 29 |
|
| 30 |
Please note |
| 31 |
that there is no comment. The interface is the same as the FORTRAN |
| 32 |
code and I believe the documentation in LSODA will suffice. |
| 33 |
As usual, a free software comes with no guarantee. |
| 34 |
|
| 35 |
Hon Wah Tam |
| 36 |
Wolfram Research, Inc. |
| 37 |
[email protected] |
| 38 |
*/ |
| 39 |
|
| 40 |
#include "qtaimlsodaintegrator.h" |
| 41 |
#include "qtaimmathutilities.h" |
| 42 |
|
| 43 |
namespace Avogadro::QtPlugins { |
| 44 |
|
| 45 |
QTAIMLSODAIntegrator::QTAIMLSODAIntegrator(QTAIMWavefunctionEvaluator& eval, |
| 46 |
const qint64 mode) |
| 47 |
: m_eval(&eval), m_mode(mode), m_associatedSphere(0) |
| 48 |
{ |
| 49 |
m_betaSpheres.clear(); |
| 50 |
} |
| 51 |
|
| 52 |
QVector3D QTAIMLSODAIntegrator::integrate(QVector3D x0y0z0) |
| 53 |
{ |
| 54 |
qreal x0 = x0y0z0.x(); |
| 55 |
qreal y0 = x0y0z0.y(); |
| 56 |
qreal z0 = x0y0z0.z(); |
| 57 |
|
| 58 |
mord[0] = 0; |
| 59 |
mord[1] = 12; |
| 60 |
mord[2] = 5; |
| 61 |
|
| 62 |
sm1[0] = 0.; |
| 63 |
sm1[1] = 0.5; |
| 64 |
sm1[2] = 0.575; |
| 65 |
sm1[3] = 0.55; |
| 66 |
sm1[4] = 0.45; |
| 67 |
sm1[5] = 0.35; |
| 68 |
sm1[6] = 0.25; |
| 69 |
sm1[7] = 0.2; |
| 70 |
sm1[8] = 0.15; |
| 71 |
sm1[9] = 0.1; |
| 72 |
sm1[10] = 0.075; |
| 73 |
sm1[11] = 0.05; |
| 74 |
sm1[12] = 0.025; |
| 75 |
|
| 76 |
illin = 0; |
| 77 |
init = 0; |
| 78 |
ntrep = 0; |
| 79 |
ixpr = 0; |
| 80 |
mesflg = 1; |
| 81 |
|
| 82 |
double rwork1, rwork5, rwork6, rwork7; |
| 83 |
double atol[4], rtol[4], t, tout, y[4]; |
| 84 |
int iwork1, iwork2, iwork5, iwork6, iwork7, iwork8, iwork9; |
| 85 |
int neq = 3; |
| 86 |
int itol, itask, istate, iopt, jt; |
| 87 |
|
| 88 |
y[1] = x0; |
| 89 |
y[2] = y0; |
| 90 |
y[3] = z0; |
| 91 |
|
| 92 |
double t0; |
| 93 |
double tf = 100.0; |
| 94 |
double dt = 0.1; |
| 95 |
|
| 96 |
m_path.clear(); |
| 97 |
m_path.append(QVector3D(y[1], y[2], y[3])); |
| 98 |
|
| 99 |
t = 0.0; |
| 100 |
for (t0 = 0.0; t0 < tf; t0 = t0 + dt) { |
| 101 |
|
| 102 |
iwork1 = iwork2 = iwork5 = iwork6 = iwork7 = iwork8 = iwork9 = 0; |
| 103 |
rwork1 = rwork5 = rwork6 = rwork7 = 0.0; |
| 104 |
|
| 105 |
t = t0; |
| 106 |
tout = t0 + dt; |
| 107 |
itol = 2; |
| 108 |
rtol[0] = 0.0; |
| 109 |
atol[0] = 0.0; |
| 110 |
rtol[1] = 0.0; |
| 111 |
rtol[2] = 0.0; |
| 112 |
rtol[3] = 0.0; |
| 113 |
atol[1] = 5.0E-5; |
| 114 |
atol[2] = 5.0E-5; |
| 115 |
atol[3] = 5.0E-5; |
| 116 |
itask = 1; |
| 117 |
istate = 1; |
| 118 |
iopt = 0; |
| 119 |
jt = 2; |
| 120 |
|
| 121 |
// if( t0 + dt > tf ) qDebug() << "END"; |
| 122 |
// beta spheres |
| 123 |
if (m_mode == QTAIMLSODAIntegrator::SteepestAscentPathInElectronDensity) { |
| 124 |
if (m_betaSpheres.length() > 0) { |
| 125 |
for (qint64 n_ = 0; n_ < m_betaSpheres.length(); ++n_) { |
| 126 |
Matrix<qreal, 3, 1> a(y[1], y[2], y[3]); |
| 127 |
Matrix<qreal, 3, 1> b(m_betaSpheres.at(n_).first.x(), |
| 128 |
m_betaSpheres.at(n_).first.y(), |
| 129 |
m_betaSpheres.at(n_).first.z()); |
| 130 |
|
| 131 |
qreal distance = QTAIMMathUtilities::distance(a, b); |
| 132 |
|
| 133 |
if (distance < m_betaSpheres.at(n_).second) { |
| 134 |
m_status = 0; |
| 135 |
m_associatedSphere = n_; |
| 136 |
return QVector3D(m_betaSpheres.at(n_).first.x(), |
| 137 |
m_betaSpheres.at(n_).first.y(), |
| 138 |
m_betaSpheres.at(n_).first.z()); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} // beta spheres |
| 143 |
|
| 144 |
lsoda(neq, y, &t, tout, itol, rtol, atol, itask, &istate, iopt, jt, iwork1, |
| 145 |
iwork2, iwork5, iwork6, iwork7, iwork8, iwork9, rwork1, rwork5, |
| 146 |
rwork6, rwork7); |
| 147 |
|
| 148 |
m_path.append(QVector3D(y[1], y[2], y[3])); |
| 149 |
|
| 150 |
// qDebug(" at t= %12.4e y= %14.6e %14.6e %14.6e", t,y[1],y[2],y[3]); |
| 151 |
if (istate <= 0) { |
| 152 |
// qDebug("error istate = %d",istate); |
| 153 |
// qDebug(" at t= %12.4e y= %14.6e %14.6e %14.6e", |
| 154 |
// t,y[1],y[2],y[3]); |
| 155 |
return QVector3D(y[1], y[2], y[3]); |
| 156 |
} |
| 157 |
|
| 158 |
} // ode step |
| 159 |
|
| 160 |
return QVector3D(y[1], y[2], y[3]); |
| 161 |
} |
| 162 |
|
| 163 |
void QTAIMLSODAIntegrator::f(int neq, double t, double* y, double* ydot) |
| 164 |
{ |
| 165 |
|
| 166 |
neq = neq; // suppress warning |
| 167 |
t = t; // suppress warning |
| 168 |
|
| 169 |
Matrix<qreal, 3, 1> gradient; |
| 170 |
|
| 171 |
Matrix<qreal, 3, 4> gH; |
| 172 |
Matrix<qreal, 3, 1> g; |
| 173 |
Matrix<qreal, 3, 3> H; |
| 174 |
|
| 175 |
Matrix<qreal, 3, 1> xyz; |
| 176 |
xyz << y[1], y[2], y[3]; |
| 177 |
|
| 178 |
if (m_mode == SteepestAscentPathInElectronDensity) { |
| 179 |
g = m_eval->gradientOfElectronDensity(xyz); |
| 180 |
} else { |
| 181 |
if (m_mode == 1 || m_mode == 2 || m_mode == 3 || m_mode == 4) { |
| 182 |
gH = m_eval->gradientAndHessianOfElectronDensity(xyz); |
| 183 |
} else { |
| 184 |
gH = m_eval->gradientAndHessianOfElectronDensityLaplacian(xyz); |
| 185 |
} |
| 186 |
|
| 187 |
g(0) = gH(0, 0); |
| 188 |
g(1) = gH(1, 0); |
| 189 |
g(2) = gH(2, 0); |
| 190 |
H(0, 0) = gH(0, 1); |
| 191 |
H(1, 0) = gH(1, 1); |
| 192 |
H(2, 0) = gH(2, 1); |
| 193 |
H(0, 1) = gH(0, 2); |
| 194 |
H(1, 1) = gH(1, 2); |
| 195 |
H(2, 1) = gH(2, 2); |
| 196 |
H(0, 2) = gH(0, 3); |
| 197 |
H(1, 2) = gH(1, 3); |
| 198 |
H(2, 2) = gH(2, 3); |
| 199 |
} |
| 200 |
|
| 201 |
switch (m_mode) { |
| 202 |
case SteepestAscentPathInElectronDensity: |
| 203 |
gradient = g; |
| 204 |
break; |
| 205 |
case CMBPMinusThreeGradientInElectronDensity: |
| 206 |
gradient = QTAIMMathUtilities::minusThreeSignatureLocatorGradient(g, H); |
| 207 |
break; |
| 208 |
case CMBPMinusOneGradientInElectronDensity: |
| 209 |
gradient = QTAIMMathUtilities::minusOneSignatureLocatorGradient(g, H); |
| 210 |
break; |
| 211 |
case CMBPPlusOneGradientInElectronDensity: |
| 212 |
gradient = QTAIMMathUtilities::plusOneSignatureLocatorGradient(g, H); |
| 213 |
break; |
| 214 |
case CMBPPlusThreeGradientInElectronDensity: |
| 215 |
gradient = QTAIMMathUtilities::plusThreeSignatureLocatorGradient(g, H); |
| 216 |
break; |
| 217 |
case CMBPMinusThreeGradientInElectronDensityLaplacian: |
| 218 |
gradient = QTAIMMathUtilities::minusThreeSignatureLocatorGradient(g, H); |
| 219 |
break; |
| 220 |
case CMBPMinusOneGradientInElectronDensityLaplacian: |
| 221 |
gradient = QTAIMMathUtilities::minusOneSignatureLocatorGradient(g, H); |
| 222 |
break; |
| 223 |
case CMBPPlusOneGradientInElectronDensityLaplacian: |
| 224 |
gradient = QTAIMMathUtilities::plusOneSignatureLocatorGradient(g, H); |
| 225 |
break; |
| 226 |
case CMBPPlusThreeGradientInElectronDensityLaplacian: |
| 227 |
gradient = QTAIMMathUtilities::plusThreeSignatureLocatorGradient(g, H); |
| 228 |
break; |
| 229 |
default: |
| 230 |
qDebug() << "Catastrophic: No ODE parameters for this property."; |
| 231 |
break; |
| 232 |
} |
| 233 |
|
| 234 |
qreal normGradient = std::hypot(gradient(0), gradient(1), gradient(2)); |
| 235 |
|
| 236 |
ydot[1] = gradient(0) / normGradient; |
| 237 |
ydot[2] = gradient(1) / normGradient; |
| 238 |
ydot[3] = gradient(2) / normGradient; |
| 239 |
} |
| 240 |
|
| 241 |
void QTAIMLSODAIntegrator::daxpy(int n_, double da, double* dx, int incx, |
| 242 |
double* dy, int incy) |
| 243 |
|
| 244 |
/* |
| 245 |
Purpose : To compute |
| 246 |
|
| 247 |
dy = da * dx + dy |
| 248 |
|
| 249 |
|
| 250 |
--- Input --- |
| 251 |
|
| 252 |
n : number of elements in input vector(s) |
| 253 |
da : double scalar multiplier |
| 254 |
dx : double vector with n+1 elements, dx[0] is not used |
| 255 |
incx : storage spacing between elements of dx |
| 256 |
dy : double vector with n+1 elements, dy[0] is not used |
| 257 |
incy : storage spacing between elements of dy |
| 258 |
|
| 259 |
|
| 260 |
--- Output --- |
| 261 |
|
| 262 |
dy = da * dx + dy, unchanged if n <= 0 |
| 263 |
|
| 264 |
|
| 265 |
For i = 0 to n-1, replace dy[ly+i*incy] with |
| 266 |
da*dx[lx+i*incx] + dy[ly+i*incy], where lx = 1 |
| 267 |
if incx >= 0, else lx = (-incx)*(n-1)+1 and ly is |
| 268 |
defined in a similar way using incy. |
| 269 |
|
| 270 |
*/ |
| 271 |
|
| 272 |
{ |
| 273 |
int ix, iy, i, m; |
| 274 |
|
| 275 |
if (n_ < 0 || da == 0.) |
| 276 |
return; |
| 277 |
|
| 278 |
/* Code for nonequal or nonpositive increments. */ |
| 279 |
|
| 280 |
if (incx != incy || incx < 1) { |
| 281 |
ix = 1; |
| 282 |
iy = 1; |
| 283 |
if (incx < 0) |
| 284 |
ix = (-n_ + 1) * incx + 1; |
| 285 |
if (incy < 0) |
| 286 |
iy = (-n_ + 1) * incy + 1; |
| 287 |
for (i = 1; i <= n_; i++) { |
| 288 |
dy[iy] = dy[iy] + da * dx[ix]; |
| 289 |
ix = ix + incx; |
| 290 |
iy = iy + incy; |
| 291 |
} |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
/* Code for both increments equal to 1. */ |
| 296 |
|
| 297 |
/* Clean-up loop so remaining vector length is a multiple of 4. */ |
| 298 |
|
| 299 |
if (incx == 1) { |
| 300 |
m = n_ % 4; |
| 301 |
if (m != 0) { |
| 302 |
for (i = 1; i <= m; i++) |
| 303 |
dy[i] = dy[i] + da * dx[i]; |
| 304 |
if (n_ < 4) |
| 305 |
return; |
| 306 |
} |
| 307 |
for (i = m + 1; i <= n_; i = i + 4) { |
| 308 |
dy[i] = dy[i] + da * dx[i]; |
| 309 |
dy[i + 1] = dy[i + 1] + da * dx[i + 1]; |
| 310 |
dy[i + 2] = dy[i + 2] + da * dx[i + 2]; |
| 311 |
dy[i + 3] = dy[i + 3] + da * dx[i + 3]; |
| 312 |
} |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
/* Code for equal, positive, nonunit increments. */ |
| 317 |
|
| 318 |
for (i = 1; i <= n_ * incx; i = i + incx) |
| 319 |
dy[i] = da * dx[i] + dy[i]; |
| 320 |
} |
| 321 |
|
| 322 |
double QTAIMLSODAIntegrator::ddot(int n_, double* dx, int incx, double* dy, |
| 323 |
int incy) |
| 324 |
|
| 325 |
/* |
| 326 |
Purpose : Inner product dx . dy |
| 327 |
|
| 328 |
|
| 329 |
--- Input --- |
| 330 |
|
| 331 |
n : number of elements in input vector(s) |
| 332 |
dx : double vector with n+1 elements, dx[0] is not used |
| 333 |
incx : storage spacing between elements of dx |
| 334 |
dy : double vector with n+1 elements, dy[0] is not used |
| 335 |
incy : storage spacing between elements of dy |
| 336 |
|
| 337 |
|
| 338 |
--- Output --- |
| 339 |
|
| 340 |
ddot : dot product dx . dy, 0 if n <= 0 |
| 341 |
|
| 342 |
|
| 343 |
ddot = sum for i = 0 to n-1 of |
| 344 |
dx[lx+i*incx] * dy[ly+i*incy] where lx = 1 if |
| 345 |
incx >= 0, else lx = (-incx)*(n-1)+1, and ly |
| 346 |
is defined in a similar way using incy. |
| 347 |
|
| 348 |
*/ |
| 349 |
|
| 350 |
{ |
| 351 |
double dotprod; |
| 352 |
int ix, iy, i, m; |
| 353 |
|
| 354 |
dotprod = 0.; |
| 355 |
if (n_ <= 0) |
| 356 |
return dotprod; |
| 357 |
|
| 358 |
/* Code for unequal or nonpositive increments. */ |
| 359 |
|
| 360 |
if (incx != incy || incx < 1) { |
| 361 |
ix = 1; |
| 362 |
iy = 1; |
| 363 |
if (incx < 0) |
| 364 |
ix = (-n_ + 1) * incx + 1; |
| 365 |
if (incy < 0) |
| 366 |
iy = (-n_ + 1) * incy + 1; |
| 367 |
for (i = 1; i <= n_; i++) { |
| 368 |
dotprod = dotprod + dx[ix] * dy[iy]; |
| 369 |
ix = ix + incx; |
| 370 |
iy = iy + incy; |
| 371 |
} |
| 372 |
return dotprod; |
| 373 |
} |
| 374 |
|
| 375 |
/* Code for both increments equal to 1. */ |
| 376 |
|
| 377 |
/* Clean-up loop so remaining vector length is a multiple of 5. */ |
| 378 |
|
| 379 |
if (incx == 1) { |
| 380 |
m = n_ % 5; |
| 381 |
if (m != 0) { |
| 382 |
for (i = 1; i <= m; i++) |
| 383 |
dotprod = dotprod + dx[i] * dy[i]; |
| 384 |
if (n_ < 5) |
| 385 |
return dotprod; |
| 386 |
} |
| 387 |
for (i = m + 1; i <= n_; i = i + 5) |
| 388 |
dotprod = dotprod + dx[i] * dy[i] + dx[i + 1] * dy[i + 1] + |
| 389 |
dx[i + 2] * dy[i + 2] + dx[i + 3] * dy[i + 3] + |
| 390 |
dx[i + 4] * dy[i + 4]; |
| 391 |
return dotprod; |
| 392 |
} |
| 393 |
|
| 394 |
/* Code for positive equal nonunit increments. */ |
| 395 |
|
| 396 |
for (i = 1; i <= n_ * incx; i = i + incx) |
| 397 |
dotprod = dotprod + dx[i] * dy[i]; |
| 398 |
return dotprod; |
| 399 |
} |
| 400 |
|
| 401 |
void QTAIMLSODAIntegrator::dgefa(double** a, int n_, int* ipvt_, int* info) |
| 402 |
|
| 403 |
/* |
| 404 |
Purpose : dgefa factors a double matrix by Gaussian elimination. |
| 405 |
|
| 406 |
dgefa is usually called by dgeco, but it can be called directly |
| 407 |
with a saving in time if rcond is not needed. |
| 408 |
(Time for dgeco) = (1+9/n)*(time for dgefa). |
| 409 |
|
| 410 |
This c version uses algorithm kji rather than the kij in dgefa.f. |
| 411 |
Note that the fortran version input variable lda is not needed. |
| 412 |
|
| 413 |
|
| 414 |
On Entry : |
| 415 |
|
| 416 |
a : double matrix of dimension ( n+1, n+1 ), |
| 417 |
the 0-th row and column are not used. |
| 418 |
a is created using NewDoubleMatrix, hence |
| 419 |
lda is unnecessary. |
| 420 |
n : the row dimension of a. |
| 421 |
|
| 422 |
On Return : |
| 423 |
|
| 424 |
a : a lower triangular matrix and the multipliers |
| 425 |
which were used to obtain it. The factorization |
| 426 |
can be written a = L * U where U is a product of |
| 427 |
permutation and unit upper triangular matrices |
| 428 |
and L is lower triangular. |
| 429 |
ipvt : an n+1 integer vector of pivot indices. |
| 430 |
*info : = 0 normal value, |
| 431 |
= k if U[k][k] == 0. This is not an error |
| 432 |
condition for this subroutine, but it does |
| 433 |
indicate that dgesl or dgedi will divide by |
| 434 |
zero if called. Use rcond in dgeco for |
| 435 |
a reliable indication of singularity. |
| 436 |
|
| 437 |
Notice that the calling program must use &info. |
| 438 |
|
| 439 |
BLAS : daxpy, dscal, idamax |
| 440 |
*/ |
| 441 |
|
| 442 |
{ |
| 443 |
int j, k, i; |
| 444 |
double t; |
| 445 |
|
| 446 |
/* Gaussian elimination with partial pivoting. */ |
| 447 |
|
| 448 |
*info = 0; |
| 449 |
for (k = 1; k <= n_ - 1; k++) { |
| 450 |
/* |
| 451 |
Find j = pivot index. Note that a[k]+k-1 is the address of |
| 452 |
the 0-th element of the row vector whose 1st element is a[k][k]. |
| 453 |
*/ |
| 454 |
j = idamax(n_ - k + 1, a[k] + k - 1, 1) + k - 1; |
| 455 |
ipvt_[k] = j; |
| 456 |
/* |
| 457 |
Zero pivot implies this row already triangularized. |
| 458 |
*/ |
| 459 |
if (a[k][j] == 0.) { |
| 460 |
*info = k; |
| 461 |
continue; |
| 462 |
} |
| 463 |
/* |
| 464 |
Interchange if necessary. |
| 465 |
*/ |
| 466 |
if (j != k) { |
| 467 |
t = a[k][j]; |
| 468 |
a[k][j] = a[k][k]; |
| 469 |
a[k][k] = t; |
| 470 |
} |
| 471 |
/* |
| 472 |
Compute multipliers. |
| 473 |
*/ |
| 474 |
t = -1. / a[k][k]; |
| 475 |
dscal(n_ - k, t, a[k] + k, 1); |
| 476 |
/* |
| 477 |
Column elimination with row indexing. |
| 478 |
*/ |
| 479 |
for (i = k + 1; i <= n_; i++) { |
| 480 |
t = a[i][j]; |
| 481 |
if (j != k) { |
| 482 |
a[i][j] = a[i][k]; |
| 483 |
a[i][k] = t; |
| 484 |
} |
| 485 |
daxpy(n_ - k, t, a[k] + k, 1, a[i] + k, 1); |
| 486 |
} |
| 487 |
} /* end k-loop */ |
| 488 |
|
| 489 |
ipvt_[n_] = n_; |
| 490 |
if (a[n_][n_] == 0.) |
| 491 |
*info = n_; |
| 492 |
} |
| 493 |
|
| 494 |
void QTAIMLSODAIntegrator::dgesl(double** a, int n_, int* ipvt_, double* b, |
| 495 |
int job) |
| 496 |
|
| 497 |
/* |
| 498 |
Purpose : dgesl solves the linear system |
| 499 |
a * x = b or Transpose(a) * x = b |
| 500 |
using the factors computed by dgeco or degfa. |
| 501 |
|
| 502 |
|
| 503 |
On Entry : |
| 504 |
|
| 505 |
a : double matrix of dimension ( n+1, n+1 ), |
| 506 |
the output from dgeco or dgefa. |
| 507 |
The 0-th row and column are not used. |
| 508 |
n : the row dimension of a. |
| 509 |
ipvt : the pivot vector from degco or dgefa. |
| 510 |
b : the right hand side vector. |
| 511 |
job : = 0 to solve a * x = b, |
| 512 |
= nonzero to solve Transpose(a) * x = b. |
| 513 |
|
| 514 |
|
| 515 |
On Return : |
| 516 |
|
| 517 |
b : the solution vector x. |
| 518 |
|
| 519 |
|
| 520 |
Error Condition : |
| 521 |
|
| 522 |
A division by zero will occur if the input factor contains |
| 523 |
a zero on the diagonal. Technically this indicates |
| 524 |
singularity but it is often caused by improper arguments or |
| 525 |
improper setting of the pointers of a. It will not occur |
| 526 |
if the subroutines are called correctly and if dgeco has |
| 527 |
set rcond > 0 or dgefa has set info = 0. |
| 528 |
|
| 529 |
|
| 530 |
BLAS : daxpy, ddot |
| 531 |
*/ |
| 532 |
|
| 533 |
{ |
| 534 |
int k, j; |
| 535 |
double t; |
| 536 |
|
| 537 |
/* |
| 538 |
Job = 0, solve a * x = b. |
| 539 |
*/ |
| 540 |
if (job == 0) { |
| 541 |
/* |
| 542 |
First solve L * y = b. |
| 543 |
*/ |
| 544 |
for (k = 1; k <= n_; k++) { |
| 545 |
t = ddot(k - 1, a[k], 1, b, 1); |
| 546 |
b[k] = (b[k] - t) / a[k][k]; |
| 547 |
} |
| 548 |
/* |
| 549 |
Now solve U * x = y. |
| 550 |
*/ |
| 551 |
for (k = n_ - 1; k >= 1; k--) { |
| 552 |
b[k] = b[k] + ddot(n_ - k, a[k] + k, 1, b + k, 1); |
| 553 |
j = ipvt_[k]; |
| 554 |
if (j != k) { |
| 555 |
t = b[j]; |
| 556 |
b[j] = b[k]; |
| 557 |
b[k] = t; |
| 558 |
} |
| 559 |
} |
| 560 |
return; |
| 561 |
} |
| 562 |
|
| 563 |
/* |
| 564 |
Job = nonzero, solve Transpose(a) * x = b. |
| 565 |
|
| 566 |
First solve Transpose(U) * y = b. |
| 567 |
*/ |
| 568 |
for (k = 1; k <= n_ - 1; k++) { |
| 569 |
j = ipvt_[k]; |
| 570 |
t = b[j]; |
| 571 |
if (j != k) { |
| 572 |
b[j] = b[k]; |
| 573 |
b[k] = t; |
| 574 |
} |
| 575 |
daxpy(n_ - k, t, a[k] + k, 1, b + k, 1); |
| 576 |
} |
| 577 |
/* |
| 578 |
Now solve Transpose(L) * x = y. |
| 579 |
*/ |
| 580 |
for (k = n_; k >= 1; k--) { |
| 581 |
b[k] = b[k] / a[k][k]; |
| 582 |
t = -b[k]; |
| 583 |
daxpy(k - 1, t, a[k], 1, b, 1); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
void QTAIMLSODAIntegrator::dscal(int n_, double da, double* dx, int incx) |
| 588 |
|
| 589 |
/* Purpose : scalar vector multiplication |
| 590 |
|
| 591 |
dx = da * dx |
| 592 |
|
| 593 |
|
| 594 |
--- Input --- |
| 595 |
|
| 596 |
n : number of elements in input vector |
| 597 |
da : double scale factor |
| 598 |
dx : double vector with n+1 elements, dx[0] is not used |
| 599 |
incx : storage spacing between elements of dx |
| 600 |
|
| 601 |
|
| 602 |
--- Output --- |
| 603 |
|
| 604 |
dx = da * dx, unchanged if n <= 0 |
| 605 |
|
| 606 |
|
| 607 |
For i = 0 to n-1, replace dx[1+i*incx] with |
| 608 |
da * dx[1+i*incx]. |
| 609 |
|
| 610 |
*/ |
| 611 |
|
| 612 |
{ |
| 613 |
int m, i; |
| 614 |
|
| 615 |
if (n_ <= 0) |
| 616 |
return; |
| 617 |
|
| 618 |
/* Code for increments not equal to 1. */ |
| 619 |
|
| 620 |
if (incx != 1) { |
| 621 |
for (i = 1; i <= n_ * incx; i = i + incx) |
| 622 |
dx[i] = da * dx[i]; |
| 623 |
return; |
| 624 |
} |
| 625 |
|
| 626 |
/* Code for increments equal to 1. */ |
| 627 |
|
| 628 |
/* Clean-up loop so remaining vector length is a multiple of 5. */ |
| 629 |
|
| 630 |
m = n_ % 5; |
| 631 |
if (m != 0) { |
| 632 |
for (i = 1; i <= m; i++) |
| 633 |
dx[i] = da * dx[i]; |
| 634 |
if (n_ < 5) |
| 635 |
return; |
| 636 |
} |
| 637 |
for (i = m + 1; i <= n_; i = i + 5) { |
| 638 |
dx[i] = da * dx[i]; |
| 639 |
dx[i + 1] = da * dx[i + 1]; |
| 640 |
dx[i + 2] = da * dx[i + 2]; |
| 641 |
dx[i + 3] = da * dx[i + 3]; |
| 642 |
dx[i + 4] = da * dx[i + 4]; |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
int QTAIMLSODAIntegrator::idamax(int n_, double* dx, int incx) |
| 647 |
|
| 648 |
/* Purpose : Find largest component of double vector dx |
| 649 |
|
| 650 |
|
| 651 |
--- Input --- |
| 652 |
|
| 653 |
n : number of elements in input vector |
| 654 |
dx : double vector with n+1 elements, dx[0] is not used |
| 655 |
incx : storage spacing between elements of dx |
| 656 |
|
| 657 |
|
| 658 |
--- Output --- |
| 659 |
|
| 660 |
idamax : smallest index, 0 if n <= 0 |
| 661 |
|
| 662 |
|
| 663 |
Find smallest index of maximum magnitude of dx. |
| 664 |
idamax = first i, i=1 to n, to minimize fabs( dx[1-incx+i*incx] ). |
| 665 |
|
| 666 |
*/ |
| 667 |
|
| 668 |
{ |
| 669 |
double dmax, xmag; |
| 670 |
int i, ii, xindex; |
| 671 |
|
| 672 |
xindex = 0; |
| 673 |
if (n_ <= 0) |
| 674 |
return xindex; |
| 675 |
xindex = 1; |
| 676 |
if (n_ <= 1 || incx <= 0) |
| 677 |
return xindex; |
| 678 |
|
| 679 |
/* Code for increments not equal to 1. */ |
| 680 |
|
| 681 |
if (incx != 1) { |
| 682 |
dmax = fabs(dx[1]); |
| 683 |
ii = 2; |
| 684 |
for (i = 1 + incx; i <= n_ * incx; i = i + incx) { |
| 685 |
xmag = fabs(dx[i]); |
| 686 |
if (xmag > dmax) { |
| 687 |
xindex = ii; |
| 688 |
dmax = xmag; |
| 689 |
} |
| 690 |
ii++; |
| 691 |
} |
| 692 |
return xindex; |
| 693 |
} |
| 694 |
|
| 695 |
/* Code for increments equal to 1. */ |
| 696 |
|
| 697 |
dmax = fabs(dx[1]); |
| 698 |
for (i = 2; i <= n_; i++) { |
| 699 |
xmag = fabs(dx[i]); |
| 700 |
if (xmag > dmax) { |
| 701 |
xindex = i; |
| 702 |
dmax = xmag; |
| 703 |
} |
| 704 |
} |
| 705 |
return xindex; |
| 706 |
} |
| 707 |
|
| 708 |
// lsoda.c |
| 709 |
|
| 710 |
#define max(a, b) ((a) > (b) ? (a) : (b)) |
| 711 |
#define min(a, b) ((a) < (b) ? (a) : (b)) |
| 712 |
|
| 713 |
#define ETA 2.2204460492503131e-16 |
| 714 |
|
| 715 |
/* |
| 716 |
static void |
| 717 |
prja(), |
| 718 |
solsy(), |
| 719 |
stoda(), |
| 720 |
cfode(), |
| 721 |
ewset(), |
| 722 |
intdy(), |
| 723 |
terminate(), |
| 724 |
terminate2(), |
| 725 |
successreturn(), |
| 726 |
scaleh(), |
| 727 |
correction(), |
| 728 |
methodswitch(), |
| 729 |
orderswitch(), |
| 730 |
endstoda(), |
| 731 |
resetcoeff(), |
| 732 |
freevectors(), |
| 733 |
corfailure(); |
| 734 |
|
| 735 |
static double |
| 736 |
vmnorm(), bnorm(), fnorm(); |
| 737 |
*/ |
| 738 |
|
| 739 |
/* |
| 740 |
The following are useful statistics. |
| 741 |
|
| 742 |
hu, |
| 743 |
h, |
| 744 |
tn, |
| 745 |
tolsf, |
| 746 |
tsw, |
| 747 |
nst, |
| 748 |
nfe, |
| 749 |
nje, |
| 750 |
nqu, |
| 751 |
nq, |
| 752 |
imxer, |
| 753 |
mused, |
| 754 |
meth |
| 755 |
*/ |
| 756 |
|
| 757 |
/* |
| 758 |
Terminate lsoda due to illegal input. |
| 759 |
*/ |
| 760 |
|
| 761 |
void QTAIMLSODAIntegrator::terminate(int* istate) |
| 762 |
{ |
| 763 |
if (illin == 5) { |
| 764 |
qDebug("lsoda -- repeated occurrence of illegal input"); |
| 765 |
qDebug(" run aborted.. apparent infinite loop"); |
| 766 |
} else { |
| 767 |
illin++; |
| 768 |
*istate = -3; |
| 769 |
} |
| 770 |
} /* end terminate */ |
| 771 |
|
| 772 |
/* |
| 773 |
Terminate lsoda due to various error conditions. |
| 774 |
*/ |
| 775 |
|
| 776 |
void QTAIMLSODAIntegrator::terminate2(double* y, double* t) |
| 777 |
{ |
| 778 |
int i; |
| 779 |
|
| 780 |
yp1 = yh[1]; |
| 781 |
for (i = 1; i <= n; i++) |
| 782 |
y[i] = yp1[i]; |
| 783 |
*t = tn; |
| 784 |
illin = 0; |
| 785 |
freevectors(); |
| 786 |
|
| 787 |
} /* end terminate2 */ |
| 788 |
|
| 789 |
/* |
| 790 |
The following block handles all successful returns from lsoda. |
| 791 |
If itask != 1, y is loaded from yh and t is set accordingly. |
| 792 |
*Istate is set to 2, the illegal input counter is zeroed, and the |
| 793 |
optional outputs are loaded into the work arrays before returning. |
| 794 |
*/ |
| 795 |
|
| 796 |
void QTAIMLSODAIntegrator::successreturn(double* y, double* t, int itask, |
| 797 |
int ihit, double tcrit, int* istate) |
| 798 |
{ |
| 799 |
int i; |
| 800 |
|
| 801 |
yp1 = yh[1]; |
| 802 |
for (i = 1; i <= n; i++) |
| 803 |
y[i] = yp1[i]; |
| 804 |
*t = tn; |
| 805 |
if (itask == 4 || itask == 5) |
| 806 |
if (ihit) |
| 807 |
*t = tcrit; |
| 808 |
*istate = 2; |
| 809 |
illin = 0; |
| 810 |
freevectors(); |
| 811 |
|
| 812 |
} /* end successreturn */ |
| 813 |
|
| 814 |
/* |
| 815 |
In this version all memory allocated using malloc is freed upon exit. |
| 816 |
Therefore *istate = 2 and *istate = 3 will not work. |
| 817 |
*/ |
| 818 |
|
| 819 |
void QTAIMLSODAIntegrator::lsoda(int neq, double* y, double* t, double tout, |
| 820 |
int itol, double* rtol, double* atol, |
| 821 |
int itask, int* istate, int iopt, int jt, |
| 822 |
int iwork1, int iwork2, int iwork5, int iwork6, |
| 823 |
int iwork7, int iwork8, int iwork9, |
| 824 |
double rwork1, double rwork5, double rwork6, |
| 825 |
double rwork7) |
| 826 |
/* |
| 827 |
If the user does not supply any of these values, the calling program |
| 828 |
should initialize those untouched working variables to zero. |
| 829 |
|
| 830 |
ml = iwork1 |
| 831 |
mu = iwork2 |
| 832 |
ixpr = iwork5 |
| 833 |
mxstep = iwork6 |
| 834 |
mxhnil = iwork7 |
| 835 |
mxordn = iwork8 |
| 836 |
mxords = iwork9 |
| 837 |
|
| 838 |
tcrit = rwork1 |
| 839 |
h0 = rwork5 |
| 840 |
hmax = rwork6 |
| 841 |
hmin = rwork7 |
| 842 |
*/ |
| 843 |
{ |
| 844 |
int mxstp0 = 500, mxhnl0 = -1 /* 10 */; |
| 845 |
|
| 846 |
int i, iflag, lenyh; |
| 847 |
double atoli, ayi, big, hmax, hmx, rh, rtoli, tdist, tnext, tol, tolsf, tp, |
| 848 |
size, sum, w0; |
| 849 |
|
| 850 |
/* |
| 851 |
Block a. |
| 852 |
This code block is executed on every call. |
| 853 |
It tests *istate and itask for legality and branches appropriately. |
| 854 |
If *istate > 1 but the flag init shows that initialization has not |
| 855 |
yet been done, an error return occurs. |
| 856 |
If *istate = 1 and tout = t, return immediately. |
| 857 |
*/ |
| 858 |
|
| 859 |
if (*istate < 1 || *istate > 3) { |
| 860 |
qDebug("lsoda -- illegal istate = %d", *istate); |
| 861 |
terminate(istate); |
| 862 |
return; |
| 863 |
} |
| 864 |
if (itask < 1 || itask > 5) { |
| 865 |
qDebug("lsoda -- illegal itask = %d", itask); |
| 866 |
terminate(istate); |
| 867 |
return; |
| 868 |
} |
| 869 |
if (init == 0 && (*istate == 2 || *istate == 3)) { |
| 870 |
qDebug("lsoda -- istate > 1 but lsoda not initialized"); |
| 871 |
terminate(istate); |
| 872 |
return; |
| 873 |
} |
| 874 |
if (*istate == 1) { |
| 875 |
init = 0; |
| 876 |
if (tout == *t) { |
| 877 |
ntrep++; |
| 878 |
if (ntrep < 5) |
| 879 |
return; |
| 880 |
qDebug("lsoda -- repeated calls with istate = 1 and tout = t"); |
| 881 |
qDebug(" run aborted.. apparent infinite loop"); |
| 882 |
return; |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
/* |
| 887 |
Block b. |
| 888 |
The next code block is executed for the initial call ( *istate = 1 ), |
| 889 |
or for a continuation call with parameter changes ( *istate = 3 ). |
| 890 |
It contains checking of all inputs and various initializations. |
| 891 |
|
| 892 |
First check legality of the non-optional inputs neq, itol, iopt, |
| 893 |
jt, ml, and mu. |
| 894 |
*/ |
| 895 |
double h0(0.0); |
| 896 |
if (*istate == 1 || *istate == 3) { |
| 897 |
ntrep = 0; |
| 898 |
if (neq <= 0) { |
| 899 |
qDebug("lsoda -- neq = %d is less than 1", neq); |
| 900 |
terminate(istate); |
| 901 |
return; |
| 902 |
} |
| 903 |
if (*istate == 3 && neq > n) { |
| 904 |
qDebug("lsoda -- istate = 3 and neq increased"); |
| 905 |
terminate(istate); |
| 906 |
return; |
| 907 |
} |
| 908 |
n = neq; |
| 909 |
if (itol < 1 || itol > 4) { |
| 910 |
qDebug("lsoda -- itol = %d illegal", itol); |
| 911 |
terminate(istate); |
| 912 |
return; |
| 913 |
} |
| 914 |
if (iopt < 0 || iopt > 1) { |
| 915 |
qDebug("lsoda -- iopt = %d illegal", iopt); |
| 916 |
terminate(istate); |
| 917 |
return; |
| 918 |
} |
| 919 |
if (jt == 3 || jt < 1 || jt > 5) { |
| 920 |
qDebug("lsoda -- jt = %d illegal", jt); |
| 921 |
terminate(istate); |
| 922 |
return; |
| 923 |
} |
| 924 |
jtyp = jt; |
| 925 |
if (jt > 2) { |
| 926 |
ml = iwork1; |
| 927 |
mu = iwork2; |
| 928 |
if (ml < 0 || ml >= n) { |
| 929 |
qDebug("lsoda -- ml = %d not between 1 and neq", ml); |
| 930 |
terminate(istate); |
| 931 |
return; |
| 932 |
} |
| 933 |
if (mu < 0 || mu >= n) { |
| 934 |
qDebug("lsoda -- mu = %d not between 1 and neq", mu); |
| 935 |
terminate(istate); |
| 936 |
return; |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
/* Next process and check the optional inpus. */ |
| 941 |
|
| 942 |
/* Default options. */ |
| 943 |
if (iopt == 0) { |
| 944 |
ixpr = 0; |
| 945 |
mxstep = mxstp0; |
| 946 |
mxhnil = mxhnl0; |
| 947 |
hmxi = 0.; |
| 948 |
hmin = 0.; |
| 949 |
if (*istate == 1) { |
| 950 |
h0 = 0.; |
| 951 |
mxordn = mord[1]; |
| 952 |
mxords = mord[2]; |
| 953 |
} |
| 954 |
} /* end if ( iopt == 0 ) */ |
| 955 |
|
| 956 |
/* Optional inputs. */ |
| 957 |
|
| 958 |
else { /* if ( iopt = 1 ) */ |
| 959 |
ixpr = iwork5; |
| 960 |
if (ixpr < 0 || ixpr > 1) { |
| 961 |
qDebug("lsoda -- ixpr = %d is illegal", ixpr); |
| 962 |
terminate(istate); |
| 963 |
return; |
| 964 |
} |
| 965 |
mxstep = iwork6; |
| 966 |
if (mxstep < 0) { |
| 967 |
qDebug("lsoda -- mxstep < 0"); |
| 968 |
terminate(istate); |
| 969 |
return; |
| 970 |
} |
| 971 |
if (mxstep == 0) |
| 972 |
mxstep = mxstp0; |
| 973 |
mxhnil = iwork7; |
| 974 |
if (mxhnil < 0) { |
| 975 |
qDebug("lsoda -- mxhnil < 0"); |
| 976 |
terminate(istate); |
| 977 |
return; |
| 978 |
} |
| 979 |
if (*istate == 1) { |
| 980 |
h0 = rwork5; |
| 981 |
mxordn = iwork8; |
| 982 |
if (mxordn < 0) { |
| 983 |
qDebug("lsoda -- mxordn = %d is less than 0", mxordn); |
| 984 |
terminate(istate); |
| 985 |
return; |
| 986 |
} |
| 987 |
if (mxordn == 0) |
| 988 |
mxordn = 100; |
| 989 |
mxordn = min(mxordn, mord[1]); |
| 990 |
mxords = iwork9; |
| 991 |
if (mxords < 0) { |
| 992 |
qDebug("lsoda -- mxords = %d is less than 0", mxords); |
| 993 |
terminate(istate); |
| 994 |
return; |
| 995 |
} |
| 996 |
if (mxords == 0) |
| 997 |
mxords = 100; |
| 998 |
mxords = min(mxords, mord[2]); |
| 999 |
if ((tout - *t) * h0 < 0.) { |
| 1000 |
qDebug("lsoda -- tout = %g behind t = %g", tout, *t); |
| 1001 |
qDebug(" integration direction is given by %g", h0); |
| 1002 |
terminate(istate); |
| 1003 |
return; |
| 1004 |
} |
| 1005 |
} /* end if ( *istate == 1 ) */ |
| 1006 |
hmax = rwork6; |
| 1007 |
if (hmax < 0.) { |
| 1008 |
qDebug("lsoda -- hmax < 0."); |
| 1009 |
terminate(istate); |
| 1010 |
return; |
| 1011 |
} |
| 1012 |
hmxi = 0.; |
| 1013 |
if (hmax > 0) |
| 1014 |
hmxi = 1. / hmax; |
| 1015 |
hmin = rwork7; |
| 1016 |
if (hmin < 0.) { |
| 1017 |
qDebug("lsoda -- hmin < 0."); |
| 1018 |
terminate(istate); |
| 1019 |
return; |
| 1020 |
} |
| 1021 |
} /* end else */ /* end iopt = 1 */ |
| 1022 |
} /* end if ( *istate == 1 || *istate == 3 ) */ |
| 1023 |
/* |
| 1024 |
If *istate = 1, meth is initialized to 1. |
| 1025 |
|
| 1026 |
Also allocate memory for yh, wm, ewt, savf, acor, ipvt. |
| 1027 |
*/ |
| 1028 |
if (*istate == 1) { |
| 1029 |
/* |
| 1030 |
If memory were not freed, *istate = 3 need not reallocate memory. |
| 1031 |
Hence this section is not executed by *istate = 3. |
| 1032 |
*/ |
| 1033 |
sqrteta = sqrt(ETA); |
| 1034 |
meth = 1; |
| 1035 |
nyh = n; |
| 1036 |
lenyh = 1 + max(mxordn, mxords); |
| 1037 |
|
| 1038 |
m_lenyh = lenyh; |
| 1039 |
m_nyh = nyh; |
| 1040 |
|
| 1041 |
yh = (double**)malloc((1 + lenyh) * sizeof(*yh)); |
| 1042 |
if (yh == nullptr) { |
| 1043 |
qDebug("lsoda -- insufficient memory for your problem"); |
| 1044 |
terminate(istate); |
| 1045 |
return; |
| 1046 |
} |
| 1047 |
for (i = 1; i <= lenyh; i++) |
| 1048 |
yh[i] = (double*)malloc((1 + nyh) * sizeof(double)); |
| 1049 |
|
| 1050 |
wm = (double**)malloc((1 + nyh) * sizeof(*wm)); |
| 1051 |
if (wm == nullptr) { |
| 1052 |
free(yh); |
| 1053 |
qDebug("lsoda -- insufficient memory for your problem"); |
| 1054 |
terminate(istate); |
| 1055 |
return; |
| 1056 |
} |
| 1057 |
for (i = 1; i <= nyh; i++) |
| 1058 |
wm[i] = (double*)malloc((1 + nyh) * sizeof(double)); |
| 1059 |
|
| 1060 |
ewt = (double*)malloc((1 + nyh) * sizeof(double)); |
| 1061 |
if (ewt == nullptr) { |
| 1062 |
free(yh); |
| 1063 |
free(wm); |
| 1064 |
qDebug("lsoda -- insufficient memory for your problem"); |
| 1065 |
terminate(istate); |
| 1066 |
return; |
| 1067 |
} |
| 1068 |
|
| 1069 |
savf = (double*)malloc((1 + nyh) * sizeof(double)); |
| 1070 |
if (savf == nullptr) { |
| 1071 |
free(yh); |
| 1072 |
free(wm); |
| 1073 |
free(ewt); |
| 1074 |
qDebug("lsoda -- insufficient memory for your problem"); |
| 1075 |
terminate(istate); |
| 1076 |
return; |
| 1077 |
} |
| 1078 |
|
| 1079 |
acor = (double*)malloc((1 + nyh) * sizeof(double)); |
| 1080 |
if (acor == nullptr) { |
| 1081 |
free(yh); |
| 1082 |
free(wm); |
| 1083 |
free(ewt); |
| 1084 |
free(savf); |
| 1085 |
qDebug("lsoda -- insufficient memory for your problem"); |
| 1086 |
terminate(istate); |
| 1087 |
return; |
| 1088 |
} |
| 1089 |
|
| 1090 |
ipvt = (int*)malloc((1 + nyh) * sizeof(int)); |
| 1091 |
if (ipvt == nullptr) { |
| 1092 |
free(yh); |
| 1093 |
free(wm); |
| 1094 |
free(ewt); |
| 1095 |
free(savf); |
| 1096 |
free(acor); |
| 1097 |
qDebug("lsoda -- insufficient memory for your problem"); |
| 1098 |
terminate(istate); |
| 1099 |
return; |
| 1100 |
} |
| 1101 |
} |
| 1102 |
/* |
| 1103 |
Check rtol and atol for legality. |
| 1104 |
*/ |
| 1105 |
if (*istate == 1 || *istate == 3) { |
| 1106 |
rtoli = rtol[1]; |
| 1107 |
atoli = atol[1]; |
| 1108 |
for (i = 1; i <= n; i++) { |
| 1109 |
if (itol >= 3) |
| 1110 |
rtoli = rtol[i]; |
| 1111 |
if (itol == 2 || itol == 4) |
| 1112 |
atoli = atol[i]; |
| 1113 |
if (rtoli < 0.) { |
| 1114 |
qDebug("lsoda -- rtol = %g is less than 0.", rtoli); |
| 1115 |
terminate(istate); |
| 1116 |
freevectors(); |
| 1117 |
return; |
| 1118 |
} |
| 1119 |
if (atoli < 0.) { |
| 1120 |
qDebug("lsoda -- atol = %g is less than 0.", atoli); |
| 1121 |
terminate(istate); |
| 1122 |
freevectors(); |
| 1123 |
return; |
| 1124 |
} |
| 1125 |
} /* end for */ |
| 1126 |
} /* end if ( *istate == 1 || *istate == 3 ) */ |
| 1127 |
/* |
| 1128 |
If *istate = 3, set flag to signal parameter changes to stoda. |
| 1129 |
*/ |
| 1130 |
if (*istate == 3) { |
| 1131 |
jstart = -1; |
| 1132 |
} |
| 1133 |
/* |
| 1134 |
Block c. |
| 1135 |
The next block is for the initial call only ( *istate = 1 ). |
| 1136 |
It contains all remaining initializations, the initial call to f, |
| 1137 |
and the calculation of the initial step size. |
| 1138 |
The error weights in ewt are inverted after being loaded. |
| 1139 |
*/ |
| 1140 |
int tcrit(0); |
| 1141 |
if (*istate == 1) { |
| 1142 |
tn = *t; |
| 1143 |
tsw = *t; |
| 1144 |
maxord = mxordn; |
| 1145 |
if (itask == 4 || itask == 5) { |
| 1146 |
tcrit = rwork1; |
| 1147 |
if ((tcrit - tout) * (tout - *t) < 0.) { |
| 1148 |
qDebug("lsoda -- itask = 4 or 5 and tcrit behind tout"); |
| 1149 |
terminate(istate); |
| 1150 |
freevectors(); |
| 1151 |
return; |
| 1152 |
} |
| 1153 |
if (h0 != 0. && (*t + h0 - tcrit) * h0 > 0.) |
| 1154 |
h0 = tcrit - *t; |
| 1155 |
} |
| 1156 |
jstart = 0; |
| 1157 |
nhnil = 0; |
| 1158 |
nst = 0; |
| 1159 |
nje = 0; |
| 1160 |
nslast = 0; |
| 1161 |
hu = 0.; |
| 1162 |
nqu = 0; |
| 1163 |
mused = 0; |
| 1164 |
miter = 0; |
| 1165 |
ccmax = 0.3; |
| 1166 |
maxcor = 3; |
| 1167 |
msbp = 20; |
| 1168 |
mxncf = 10; |
| 1169 |
/* |
| 1170 |
Initial call to f. |
| 1171 |
*/ |
| 1172 |
f(neq, *t, y, yh[2]); |
| 1173 |
nfe = 1; |
| 1174 |
/* |
| 1175 |
Load the initial value vector in yh. |
| 1176 |
*/ |
| 1177 |
yp1 = yh[1]; |
| 1178 |
for (i = 1; i <= n; i++) |
| 1179 |
yp1[i] = y[i]; |
| 1180 |
/* |
| 1181 |
Load and invert the ewt array. ( h is temporarily set to 1. ) |
| 1182 |
*/ |
| 1183 |
nq = 1; |
| 1184 |
h = 1.; |
| 1185 |
ewset(itol, rtol, atol, y); |
| 1186 |
for (i = 1; i <= n; i++) { |
| 1187 |
if (ewt[i] <= 0.) { |
| 1188 |
qDebug("lsoda -- ewt[%d] = %g <= 0.", i, ewt[i]); |
| 1189 |
// ECB Comment out because wrong number of arguments. |
| 1190 |
// terminate( y, yh, t, tn ); |
| 1191 |
return; |
| 1192 |
} |
| 1193 |
ewt[i] = 1. / ewt[i]; |
| 1194 |
} |
| 1195 |
|
| 1196 |
/* |
| 1197 |
The coding below computes the step size, h0, to be attempted on the |
| 1198 |
first step, unless the user has supplied a value for this. |
| 1199 |
First check that tout - *t differs significantly from zero. |
| 1200 |
A scalar tolerance quantity tol is computed, as max(rtol[i]) |
| 1201 |
if this is positive, or max(atol[i]/fabs(y[i])) otherwise, adjusted |
| 1202 |
so as to be between 100*ETA and 0.001. |
| 1203 |
Then the computed value h0 is given by |
| 1204 |
|
| 1205 |
h0^(-2) = 1. / ( tol * w0^2 ) + tol * ( norm(f) )^2 |
| 1206 |
|
| 1207 |
where w0 = max( fabs(*t), fabs(tout) ), |
| 1208 |
f = the initial value of the vector f(t,y), and |
| 1209 |
norm() = the weighted vector norm used throughout, given by |
| 1210 |
the vmnorm function routine, and weighted by the |
| 1211 |
tolerances initially loaded into the ewt array. |
| 1212 |
|
| 1213 |
The sign of h0 is inferred from the initial values of tout and *t. |
| 1214 |
fabs(h0) is made < fabs(tout-*t) in any case. |
| 1215 |
*/ |
| 1216 |
if (h0 == 0.) { |
| 1217 |
tdist = fabs(tout - *t); |
| 1218 |
w0 = max(fabs(*t), fabs(tout)); |
| 1219 |
if (tdist < 2. * ETA * w0) { |
| 1220 |
qDebug("lsoda -- tout too close to t to start integration "); |
| 1221 |
terminate(istate); |
| 1222 |
freevectors(); |
| 1223 |
return; |
| 1224 |
} |
| 1225 |
tol = rtol[1]; |
| 1226 |
if (itol > 2) { |
| 1227 |
for (i = 2; i <= n; i++) |
| 1228 |
tol = max(tol, rtol[i]); |
| 1229 |
} |
| 1230 |
if (tol <= 0.) { |
| 1231 |
atoli = atol[1]; |
| 1232 |
for (i = 1; i <= n; i++) { |
| 1233 |
if (itol == 2 || itol == 4) |
| 1234 |
atoli = atol[i]; |
| 1235 |
ayi = fabs(y[i]); |
| 1236 |
if (ayi != 0.) |
| 1237 |
tol = max(tol, atoli / ayi); |
| 1238 |
} |
| 1239 |
} |
| 1240 |
tol = max(tol, 100. * ETA); |
| 1241 |
tol = min(tol, 0.001); |
| 1242 |
sum = vmnorm(n, yh[2], ewt); |
| 1243 |
sum = 1. / (tol * w0 * w0) + tol * sum * sum; |
| 1244 |
h0 = 1. / sqrt(sum); |
| 1245 |
h0 = min(h0, tdist); |
| 1246 |
h0 = h0 * ((tout - *t >= 0.) ? 1. : -1.); |
| 1247 |
} /* end if ( h0 == 0. ) */ |
| 1248 |
/* |
| 1249 |
Adjust h0 if necessary to meet hmax bound. |
| 1250 |
*/ |
| 1251 |
rh = fabs(h0) * hmxi; |
| 1252 |
if (rh > 1.) |
| 1253 |
h0 /= rh; |
| 1254 |
/* |
| 1255 |
Load h with h0 and scale yh[2] by h0. |
| 1256 |
*/ |
| 1257 |
h = h0; |
| 1258 |
yp1 = yh[2]; |
| 1259 |
for (i = 1; i <= n; i++) |
| 1260 |
yp1[i] *= h0; |
| 1261 |
} /* if ( *istate == 1 ) */ |
| 1262 |
/* |
| 1263 |
Block d. |
| 1264 |
The next code block is for continuation calls only ( *istate = 2 or 3 ) |
| 1265 |
and is to check stop conditions before taking a step. |
| 1266 |
*/ |
| 1267 |
int ihit(0); |
| 1268 |
if (*istate == 2 || *istate == 3) { |
| 1269 |
nslast = nst; |
| 1270 |
switch (itask) { |
| 1271 |
case 1: |
| 1272 |
if ((tn - tout) * h >= 0.) { |
| 1273 |
intdy(tout, 0, y, &iflag); |
| 1274 |
if (iflag != 0) { |
| 1275 |
qDebug("lsoda -- trouble from intdy, itask = %d, tout = %g", itask, |
| 1276 |
tout); |
| 1277 |
terminate(istate); |
| 1278 |
freevectors(); |
| 1279 |
return; |
| 1280 |
} |
| 1281 |
*t = tout; |
| 1282 |
*istate = 2; |
| 1283 |
illin = 0; |
| 1284 |
freevectors(); |
| 1285 |
return; |
| 1286 |
} |
| 1287 |
break; |
| 1288 |
case 2: |
| 1289 |
break; |
| 1290 |
case 3: |
| 1291 |
tp = tn - hu * (1. + 100. * ETA); |
| 1292 |
if ((tp - tout) * h > 0.) { |
| 1293 |
qDebug("lsoda -- itask = %d and tout behind tcur - hu", itask); |
| 1294 |
terminate(istate); |
| 1295 |
freevectors(); |
| 1296 |
return; |
| 1297 |
} |
| 1298 |
if ((tn - tout) * h < 0.) |
| 1299 |
break; |
| 1300 |
successreturn(y, t, itask, ihit, tcrit, istate); |
| 1301 |
return; |
| 1302 |
case 4: |
| 1303 |
tcrit = rwork1; |
| 1304 |
if ((tn - tcrit) * h > 0.) { |
| 1305 |
qDebug("lsoda -- itask = 4 or 5 and tcrit behind tcur"); |
| 1306 |
terminate(istate); |
| 1307 |
freevectors(); |
| 1308 |
return; |
| 1309 |
} |
| 1310 |
if ((tcrit - tout) * h < 0.) { |
| 1311 |
qDebug("lsoda -- itask = 4 or 5 and tcrit behind tout"); |
| 1312 |
terminate(istate); |
| 1313 |
freevectors(); |
| 1314 |
return; |
| 1315 |
} |
| 1316 |
if ((tn - tout) * h >= 0.) { |
| 1317 |
intdy(tout, 0, y, &iflag); |
| 1318 |
if (iflag != 0) { |
| 1319 |
qDebug("lsoda -- trouble from intdy, itask = %d, tout = %g", itask, |
| 1320 |
tout); |
| 1321 |
terminate(istate); |
| 1322 |
freevectors(); |
| 1323 |
return; |
| 1324 |
} |
| 1325 |
*t = tout; |
| 1326 |
*istate = 2; |
| 1327 |
illin = 0; |
| 1328 |
freevectors(); |
| 1329 |
return; |
| 1330 |
} |
| 1331 |
case 5: |
| 1332 |
if (itask == 5) { |
| 1333 |
tcrit = rwork1; |
| 1334 |
if ((tn - tcrit) * h > 0.) { |
| 1335 |
qDebug("lsoda -- itask = 4 or 5 and tcrit behind tcur"); |
| 1336 |
terminate(istate); |
| 1337 |
freevectors(); |
| 1338 |
return; |
| 1339 |
} |
| 1340 |
} |
| 1341 |
hmx = fabs(tn) + fabs(h); |
| 1342 |
ihit = fabs(tn - tcrit) <= (100. * ETA * hmx); |
| 1343 |
if (ihit) { |
| 1344 |
*t = tcrit; |
| 1345 |
successreturn(y, t, itask, ihit, tcrit, istate); |
| 1346 |
return; |
| 1347 |
} |
| 1348 |
tnext = tn + h * (1. + 4. * ETA); |
| 1349 |
if ((tnext - tcrit) * h <= 0.) |
| 1350 |
break; |
| 1351 |
h = (tcrit - tn) * (1. - 4. * ETA); |
| 1352 |
if (*istate == 2) |
| 1353 |
jstart = -2; |
| 1354 |
break; |
| 1355 |
} /* end switch */ |
| 1356 |
} /* end if ( *istate == 2 || *istate == 3 ) */ |
| 1357 |
|
| 1358 |
/* |
| 1359 |
Block e. |
| 1360 |
The next block is normally executed for all calls and contains |
| 1361 |
the call to the one-step core integrator stoda. |
| 1362 |
|
| 1363 |
This is a looping point for the integration steps. |
| 1364 |
|
| 1365 |
First check for too many steps being taken, update ewt ( if not at |
| 1366 |
start of problem). Check for too much accuracy being requested, and |
| 1367 |
check for h below the roundoff level in *t. |
| 1368 |
*/ |
| 1369 |
while (true) { |
| 1370 |
if (*istate != 1 || nst != 0) { |
| 1371 |
if ((nst - nslast) >= mxstep) { |
| 1372 |
// qDebug( "lsoda -- %d steps taken before reaching tout", |
| 1373 |
// mxstep ); |
| 1374 |
*istate = -1; |
| 1375 |
terminate2(y, t); |
| 1376 |
return; |
| 1377 |
} |
| 1378 |
ewset(itol, rtol, atol, yh[1]); |
| 1379 |
for (i = 1; i <= n; i++) { |
| 1380 |
if (ewt[i] <= 0.) { |
| 1381 |
qDebug("lsoda -- ewt[%d] = %g <= 0.", i, ewt[i]); |
| 1382 |
*istate = -6; |
| 1383 |
terminate2(y, t); |
| 1384 |
return; |
| 1385 |
} |
| 1386 |
ewt[i] = 1. / ewt[i]; |
| 1387 |
} |
| 1388 |
} |
| 1389 |
tolsf = ETA * vmnorm(n, yh[1], ewt); |
| 1390 |
if (tolsf > 0.01) { |
| 1391 |
tolsf = tolsf * 200.; |
| 1392 |
if (nst == 0) { |
| 1393 |
qDebug("lsoda -- at start of problem, too much accuracy"); |
| 1394 |
qDebug(" requested for precision of machine,"); |
| 1395 |
qDebug(" suggested scaling factor = %g", tolsf); |
| 1396 |
terminate(istate); |
| 1397 |
freevectors(); |
| 1398 |
return; |
| 1399 |
} |
| 1400 |
qDebug("lsoda -- at t = %g, too much accuracy requested", *t); |
| 1401 |
qDebug(" for precision of machine, suggested"); |
| 1402 |
qDebug(" scaling factor = %g", tolsf); |
| 1403 |
*istate = -2; |
| 1404 |
terminate2(y, t); |
| 1405 |
return; |
| 1406 |
} |
| 1407 |
if ((tn + h) == tn) { |
| 1408 |
nhnil++; |
| 1409 |
if (nhnil <= mxhnil) { |
| 1410 |
qDebug("lsoda -- warning..internal t = %g and h = %g are", tn, h); |
| 1411 |
qDebug(" such that in the machine, t + h = t on the next step"); |
| 1412 |
qDebug(" solver will continue anyway."); |
| 1413 |
if (nhnil == mxhnil) { |
| 1414 |
qDebug("lsoda -- above warning has been issued %d times,", nhnil); |
| 1415 |
qDebug(" it will not be issued again for this problem"); |
| 1416 |
} |
| 1417 |
} |
| 1418 |
} |
| 1419 |
|
| 1420 |
/* |
| 1421 |
Call stoda |
| 1422 |
*/ |
| 1423 |
stoda(neq, y); |
| 1424 |
|
| 1425 |
/* |
| 1426 |
qDebug( "meth= %d, order= %d, nfe= %d, nje= %d", |
| 1427 |
meth, nq, nfe, nje ); |
| 1428 |
qDebug( "t= %20.15e, h= %20.15e, nst=%d", tn, h, nst ); |
| 1429 |
qDebug( "y= %20.15e, %20.15e, %20.15e", |
| 1430 |
yh[1][1], yh[1][2], yh[1][3] ); |
| 1431 |
*/ |
| 1432 |
|
| 1433 |
if (kflag == 0) { |
| 1434 |
/* |
| 1435 |
Block f. |
| 1436 |
The following block handles the case of a successful return from the |
| 1437 |
core integrator ( kflag = 0 ). |
| 1438 |
If a method switch was just made, record tsw, reset maxord, |
| 1439 |
set jstart to -1 to signal stoda to complete the switch, |
| 1440 |
and do extra printing of data if ixpr = 1. |
| 1441 |
Then, in any case, check for stop conditions. |
| 1442 |
*/ |
| 1443 |
init = 1; |
| 1444 |
if (meth != mused) { |
| 1445 |
tsw = tn; |
| 1446 |
maxord = mxordn; |
| 1447 |
if (meth == 2) |
| 1448 |
maxord = mxords; |
| 1449 |
jstart = -1; |
| 1450 |
if (ixpr) { |
| 1451 |
if (meth == 2) |
| 1452 |
qDebug() << "lsoda -- a switch to the stiff method has occurred"; |
| 1453 |
if (meth == 1) |
| 1454 |
qDebug() << "lsoda -- a switch to the nonstiff method has occurred"; |
| 1455 |
qDebug() << " at t = " << tn |
| 1456 |
<< ", tentative step size h = " << h |
| 1457 |
<< ", step nst = " << nst; |
| 1458 |
} |
| 1459 |
} /* end if ( meth != mused ) */ |
| 1460 |
/* |
| 1461 |
itask = 1. |
| 1462 |
If tout has been reached, interpolate. |
| 1463 |
*/ |
| 1464 |
if (itask == 1) { |
| 1465 |
if ((tn - tout) * h < 0.) |
| 1466 |
continue; |
| 1467 |
intdy(tout, 0, y, &iflag); |
| 1468 |
*t = tout; |
| 1469 |
*istate = 2; |
| 1470 |
illin = 0; |
| 1471 |
freevectors(); |
| 1472 |
return; |
| 1473 |
} |
| 1474 |
/* |
| 1475 |
itask = 2. |
| 1476 |
*/ |
| 1477 |
if (itask == 2) { |
| 1478 |
successreturn(y, t, itask, ihit, tcrit, istate); |
| 1479 |
return; |
| 1480 |
} |
| 1481 |
/* |
| 1482 |
itask = 3. |
| 1483 |
Jump to exit if tout was reached. |
| 1484 |
*/ |
| 1485 |
if (itask == 3) { |
| 1486 |
if ((tn - tout) * h >= 0.) { |
| 1487 |
successreturn(y, t, itask, ihit, tcrit, istate); |
| 1488 |
return; |
| 1489 |
} |
| 1490 |
continue; |
| 1491 |
} |
| 1492 |
/* |
| 1493 |
itask = 4. |
| 1494 |
See if tout or tcrit was reached. Adjust h if necessary. |
| 1495 |
*/ |
| 1496 |
if (itask == 4) { |
| 1497 |
if ((tn - tout) * h >= 0.) { |
| 1498 |
intdy(tout, 0, y, &iflag); |
| 1499 |
*t = tout; |
| 1500 |
*istate = 2; |
| 1501 |
illin = 0; |
| 1502 |
freevectors(); |
| 1503 |
return; |
| 1504 |
} else { |
| 1505 |
hmx = fabs(tn) + fabs(h); |
| 1506 |
ihit = fabs(tn - tcrit) <= (100. * ETA * hmx); |
| 1507 |
if (ihit) { |
| 1508 |
successreturn(y, t, itask, ihit, tcrit, istate); |
| 1509 |
return; |
| 1510 |
} |
| 1511 |
tnext = tn + h * (1. + 4. * ETA); |
| 1512 |
if ((tnext - tcrit) * h <= 0.) |
| 1513 |
continue; |
| 1514 |
h = (tcrit - tn) * (1. - 4. * ETA); |
| 1515 |
jstart = -2; |
| 1516 |
continue; |
| 1517 |
} |
| 1518 |
} /* end if ( itask == 4 ) */ |
| 1519 |
/* |
| 1520 |
itask = 5. |
| 1521 |
See if tcrit was reached and jump to exit. |
| 1522 |
*/ |
| 1523 |
if (itask == 5) { |
| 1524 |
hmx = fabs(tn) + fabs(h); |
| 1525 |
ihit = fabs(tn - tcrit) <= (100. * ETA * hmx); |
| 1526 |
successreturn(y, t, itask, ihit, tcrit, istate); |
| 1527 |
return; |
| 1528 |
} |
| 1529 |
} /* end if ( kflag == 0 ) */ |
| 1530 |
/* |
| 1531 |
kflag = -1, error test failed repeatedly or with fabs(h) = hmin. |
| 1532 |
kflag = -2, convergence failed repeatedly or with fabs(h) = hmin. |
| 1533 |
*/ |
| 1534 |
if (kflag == -1 || kflag == -2) { |
| 1535 |
qDebug("lsoda -- at t = %g and step size h = %g, the", tn, h); |
| 1536 |
if (kflag == -1) { |
| 1537 |
qDebug(" error test failed repeatedly or"); |
| 1538 |
qDebug(" with fabs(h) = hmin"); |
| 1539 |
*istate = -4; |
| 1540 |
} |
| 1541 |
if (kflag == -2) { |
| 1542 |
qDebug(" corrector convergence failed repeatedly or"); |
| 1543 |
qDebug(" with fabs(h) = hmin"); |
| 1544 |
*istate = -5; |
| 1545 |
} |
| 1546 |
big = 0.; |
| 1547 |
imxer = 1; |
| 1548 |
for (i = 1; i <= n; i++) { |
| 1549 |
size = fabs(acor[i]) * ewt[i]; |
| 1550 |
if (big < size) { |
| 1551 |
big = size; |
| 1552 |
imxer = i; |
| 1553 |
} |
| 1554 |
} |
| 1555 |
terminate2(y, t); |
| 1556 |
return; |
| 1557 |
} /* end if ( kflag == -1 || kflag == -2 ) */ |
| 1558 |
} /* end while */ |
| 1559 |
|
| 1560 |
} /* end lsoda */ |
| 1561 |
|
| 1562 |
void QTAIMLSODAIntegrator::stoda(int neq, double* y) |
| 1563 |
{ |
| 1564 |
int corflag, orderflag; |
| 1565 |
int i, i1, j, m, ncf; |
| 1566 |
double del, delp, dup, exup, r, rh, rhup, told; |
| 1567 |
double pdh, pnorm; |
| 1568 |
|
| 1569 |
/* |
| 1570 |
stoda performs one step of the integration of an initial value |
| 1571 |
problem for a system of ordinary differential equations. |
| 1572 |
Note.. stoda is independent of the value of the iteration method |
| 1573 |
indicator miter, when this is != 0, and hence is independent |
| 1574 |
of the type of chord method used, or the Jacobian structure. |
| 1575 |
Communication with stoda is done with the following variables: |
| 1576 |
|
| 1577 |
jstart = an integer used for input only, with the following |
| 1578 |
values and meanings: |
| 1579 |
|
| 1580 |
0 perform the first step, |
| 1581 |
> 0 take a new step continuing from the last, |
| 1582 |
-1 take the next step with a new value of h, |
| 1583 |
n, meth, miter, and/or matrix parameters. |
| 1584 |
-2 take the next step with a new value of h, |
| 1585 |
but with other inputs unchanged. |
| 1586 |
|
| 1587 |
kflag = a completion code with the following meanings: |
| 1588 |
|
| 1589 |
0 the step was successful, |
| 1590 |
-1 the requested error could not be achieved, |
| 1591 |
-2 corrector convergence could not be achieved, |
| 1592 |
-3 fatal error in prja or solsy. |
| 1593 |
|
| 1594 |
miter = corrector iteration method: |
| 1595 |
|
| 1596 |
0 functional iteration, |
| 1597 |
>0 a chord method corresponding to jacobian type jt. |
| 1598 |
|
| 1599 |
*/ |
| 1600 |
kflag = 0; |
| 1601 |
told = tn; |
| 1602 |
ncf = 0; |
| 1603 |
ierpj = 0; |
| 1604 |
iersl = 0; |
| 1605 |
jcur = 0; |
| 1606 |
delp = 0.; |
| 1607 |
|
| 1608 |
/* |
| 1609 |
On the first call, the order is set to 1, and other variables are |
| 1610 |
initialized. rmax is the maximum ratio by which h can be increased |
| 1611 |
in a single step. It is initially 1.e4 to compensate for the small |
| 1612 |
initial h, but then is normally equal to 10. If a filure occurs |
| 1613 |
(in corrector convergence or error test), rmax is set at 2 for |
| 1614 |
the next increase. |
| 1615 |
cfode is called to get the needed coefficients for both methods. |
| 1616 |
*/ |
| 1617 |
if (jstart == 0) { |
| 1618 |
lmax = maxord + 1; |
| 1619 |
nq = 1; |
| 1620 |
l = 2; |
| 1621 |
ialth = 2; |
| 1622 |
rmax = 10000.; |
| 1623 |
rc = 0.; |
| 1624 |
el0 = 1.; |
| 1625 |
crate = 0.7; |
| 1626 |
hold = h; |
| 1627 |
nslp = 0; |
| 1628 |
ipup = miter; |
| 1629 |
/* |
| 1630 |
Initialize switching parameters. meth = 1 is assumed initially. |
| 1631 |
*/ |
| 1632 |
icount = 20; |
| 1633 |
irflag = 0; |
| 1634 |
pdest = 0.; |
| 1635 |
pdlast = 0.; |
| 1636 |
ratio = 5.; |
| 1637 |
cfode(2); |
| 1638 |
for (i = 1; i <= 5; i++) |
| 1639 |
cm2[i] = tesco[i][2] * elco[i][i + 1]; |
| 1640 |
cfode(1); |
| 1641 |
for (i = 1; i <= 12; i++) |
| 1642 |
cm1[i] = tesco[i][2] * elco[i][i + 1]; |
| 1643 |
resetcoeff(); |
| 1644 |
} /* end if ( jstart == 0 ) */ |
| 1645 |
/* |
| 1646 |
The following block handles preliminaries needed when jstart = -1. |
| 1647 |
ipup is set to miter to force a matrix update. |
| 1648 |
If an order increase is about to be considered ( ialth = 1 ), |
| 1649 |
ialth is reset to 2 to postpone consideration one more step. |
| 1650 |
If the caller has changed meth, cfode is called to reset |
| 1651 |
the coefficients of the method. |
| 1652 |
If h is to be changed, yh must be rescaled. |
| 1653 |
If h or meth is being changed, ialth is reset to l = nq + 1 |
| 1654 |
to prevent further changes in h for that many steps. |
| 1655 |
*/ |
| 1656 |
if (jstart == -1) { |
| 1657 |
ipup = miter; |
| 1658 |
lmax = maxord + 1; |
| 1659 |
if (ialth == 1) |
| 1660 |
ialth = 2; |
| 1661 |
if (meth != mused) { |
| 1662 |
cfode(meth); |
| 1663 |
ialth = l; |
| 1664 |
resetcoeff(); |
| 1665 |
} |
| 1666 |
if (h != hold) { |
| 1667 |
rh = h / hold; |
| 1668 |
h = hold; |
| 1669 |
scaleh(&rh, &pdh); |
| 1670 |
} |
| 1671 |
} /* if ( jstart == -1 ) */ |
| 1672 |
|
| 1673 |
if (jstart == -2) { |
| 1674 |
if (h != hold) { |
| 1675 |
rh = h / hold; |
| 1676 |
h = hold; |
| 1677 |
scaleh(&rh, &pdh); |
| 1678 |
} |
| 1679 |
} /* if ( jstart == -2 ) */ |
| 1680 |
|
| 1681 |
/* |
| 1682 |
Prediction. |
| 1683 |
This section computes the predicted values by effectively |
| 1684 |
multiplying the yh array by the pascal triangle matrix. |
| 1685 |
rc is the ratio of new to old values of the coefficient h * el[1]. |
| 1686 |
When rc differs from 1 by more than ccmax, ipup is set to miter |
| 1687 |
to force pjac to be called, if a jacobian is involved. |
| 1688 |
In any case, prja is called at least every msbp steps. |
| 1689 |
*/ |
| 1690 |
|
| 1691 |
while (true) { |
| 1692 |
while (true) { |
| 1693 |
if (fabs(rc - 1.) > ccmax) |
| 1694 |
ipup = miter; |
| 1695 |
if (nst >= nslp + msbp) |
| 1696 |
ipup = miter; |
| 1697 |
tn += h; |
| 1698 |
for (j = nq; j >= 1; j--) |
| 1699 |
for (i1 = j; i1 <= nq; i1++) { |
| 1700 |
yp1 = yh[i1]; |
| 1701 |
yp2 = yh[i1 + 1]; |
| 1702 |
for (i = 1; i <= n; i++) |
| 1703 |
yp1[i] += yp2[i]; |
| 1704 |
} |
| 1705 |
pnorm = vmnorm(n, yh[1], ewt); |
| 1706 |
|
| 1707 |
correction(neq, y, &corflag, pnorm, &del, &delp, &told, &ncf, &rh, &m); |
| 1708 |
if (corflag == 0) |
| 1709 |
break; |
| 1710 |
if (corflag == 1) { |
| 1711 |
rh = max(rh, hmin / fabs(h)); |
| 1712 |
scaleh(&rh, &pdh); |
| 1713 |
continue; |
| 1714 |
} |
| 1715 |
if (corflag == 2) { |
| 1716 |
kflag = -2; |
| 1717 |
hold = h; |
| 1718 |
jstart = 1; |
| 1719 |
return; |
| 1720 |
} |
| 1721 |
} /* end inner while ( corrector loop ) */ |
| 1722 |
/* |
| 1723 |
The corrector has converged. jcur is set to 0 |
| 1724 |
to signal that the Jacobian involved may need updating later. |
| 1725 |
The local error test is done now. |
| 1726 |
*/ |
| 1727 |
jcur = 0; |
| 1728 |
double dsm(0.0); |
| 1729 |
if (m == 0) |
| 1730 |
dsm = del / tesco[nq][2]; |
| 1731 |
if (m > 0) |
| 1732 |
dsm = vmnorm(n, acor, ewt) / tesco[nq][2]; |
| 1733 |
if (dsm <= 1.) { |
| 1734 |
/* |
| 1735 |
After a successful step, update the yh array. |
| 1736 |
Decrease icount by 1, and if it is -1, consider switching methods. |
| 1737 |
If a method switch is made, reset various parameters, |
| 1738 |
rescale the yh array, and exit. If there is no switch, |
| 1739 |
consider changing h if ialth = 1. Otherwise decrease ialth by 1. |
| 1740 |
If ialth is then 1 and nq < maxord, then acor is saved for |
| 1741 |
use in a possible order increase on the next step. |
| 1742 |
If a change in h is considered, an increase or decrease in order |
| 1743 |
by one is considered also. A change in h is made only if it is by |
| 1744 |
a factor of at least 1.1. If not, ialth is set to 3 to prevent |
| 1745 |
testing for that many steps. |
| 1746 |
*/ |
| 1747 |
kflag = 0; |
| 1748 |
nst++; |
| 1749 |
hu = h; |
| 1750 |
nqu = nq; |
| 1751 |
mused = meth; |
| 1752 |
for (j = 1; j <= l; j++) { |
| 1753 |
yp1 = yh[j]; |
| 1754 |
r = el[j]; |
| 1755 |
for (i = 1; i <= n; i++) |
| 1756 |
yp1[i] += r * acor[i]; |
| 1757 |
} |
| 1758 |
icount--; |
| 1759 |
if (icount < 0) { |
| 1760 |
methodswitch(dsm, pnorm, &pdh, &rh); |
| 1761 |
if (meth != mused) { |
| 1762 |
rh = max(rh, hmin / fabs(h)); |
| 1763 |
scaleh(&rh, &pdh); |
| 1764 |
rmax = 10.; |
| 1765 |
endstoda(); |
| 1766 |
break; |
| 1767 |
} |
| 1768 |
} |
| 1769 |
/* |
| 1770 |
No method switch is being made. Do the usual step/order selection. |
| 1771 |
*/ |
| 1772 |
ialth--; |
| 1773 |
if (ialth == 0) { |
| 1774 |
rhup = 0.; |
| 1775 |
if (l != lmax) { |
| 1776 |
yp1 = yh[lmax]; |
| 1777 |
for (i = 1; i <= n; i++) |
| 1778 |
savf[i] = acor[i] - yp1[i]; |
| 1779 |
dup = vmnorm(n, savf, ewt) / tesco[nq][3]; |
| 1780 |
exup = 1. / (double)(l + 1); |
| 1781 |
rhup = 1. / (1.4 * pow(dup, exup) + 0.0000014); |
| 1782 |
} |
| 1783 |
orderswitch(&rhup, dsm, &pdh, &rh, &orderflag); |
| 1784 |
/* |
| 1785 |
No change in h or nq. |
| 1786 |
*/ |
| 1787 |
if (orderflag == 0) { |
| 1788 |
endstoda(); |
| 1789 |
break; |
| 1790 |
} |
| 1791 |
/* |
| 1792 |
h is changed, but not nq. |
| 1793 |
*/ |
| 1794 |
if (orderflag == 1) { |
| 1795 |
rh = max(rh, hmin / fabs(h)); |
| 1796 |
scaleh(&rh, &pdh); |
| 1797 |
rmax = 10.; |
| 1798 |
endstoda(); |
| 1799 |
break; |
| 1800 |
} |
| 1801 |
/* |
| 1802 |
both nq and h are changed. |
| 1803 |
*/ |
| 1804 |
if (orderflag == 2) { |
| 1805 |
resetcoeff(); |
| 1806 |
rh = max(rh, hmin / fabs(h)); |
| 1807 |
scaleh(&rh, &pdh); |
| 1808 |
rmax = 10.; |
| 1809 |
endstoda(); |
| 1810 |
break; |
| 1811 |
} |
| 1812 |
} /* end if ( ialth == 0 ) */ |
| 1813 |
if (ialth > 1 || l == lmax) { |
| 1814 |
endstoda(); |
| 1815 |
break; |
| 1816 |
} |
| 1817 |
yp1 = yh[lmax]; |
| 1818 |
for (i = 1; i <= n; i++) |
| 1819 |
yp1[i] = acor[i]; |
| 1820 |
endstoda(); |
| 1821 |
break; |
| 1822 |
} /* end if ( dsm <= 1. ) */ |
| 1823 |
/* |
| 1824 |
The error test failed. kflag keeps track of multiple failures. |
| 1825 |
Restore tn and the yh array to their previous values, and prepare |
| 1826 |
to try the step again. Compute the optimum step size for this or |
| 1827 |
one lower. After 2 or more failures, h is forced to decrease |
| 1828 |
by a factor of 0.2 or less. |
| 1829 |
*/ |
| 1830 |
else { |
| 1831 |
kflag--; |
| 1832 |
tn = told; |
| 1833 |
for (j = nq; j >= 1; j--) |
| 1834 |
for (i1 = j; i1 <= nq; i1++) { |
| 1835 |
yp1 = yh[i1]; |
| 1836 |
yp2 = yh[i1 + 1]; |
| 1837 |
for (i = 1; i <= n; i++) |
| 1838 |
yp1[i] -= yp2[i]; |
| 1839 |
} |
| 1840 |
rmax = 2.; |
| 1841 |
if (fabs(h) <= hmin * 1.00001) { |
| 1842 |
kflag = -1; |
| 1843 |
hold = h; |
| 1844 |
jstart = 1; |
| 1845 |
break; |
| 1846 |
} |
| 1847 |
if (kflag > -3) { |
| 1848 |
rhup = 0.; |
| 1849 |
orderswitch(&rhup, dsm, &pdh, &rh, &orderflag); |
| 1850 |
if (orderflag == 1 || orderflag == 0) { |
| 1851 |
if (orderflag == 0) |
| 1852 |
rh = min(rh, 0.2); |
| 1853 |
rh = max(rh, hmin / fabs(h)); |
| 1854 |
scaleh(&rh, &pdh); |
| 1855 |
} |
| 1856 |
if (orderflag == 2) { |
| 1857 |
resetcoeff(); |
| 1858 |
rh = max(rh, hmin / fabs(h)); |
| 1859 |
scaleh(&rh, &pdh); |
| 1860 |
} |
| 1861 |
continue; |
| 1862 |
} /* if ( kflag > -3 ) */ |
| 1863 |
/* |
| 1864 |
Control reaches this section if 3 or more failures have occurred. |
| 1865 |
If 10 failures have occurred, exit with kflag = -1. |
| 1866 |
It is assumed that the derivatives that have accumulated in the |
| 1867 |
yh array have errors of the wrong order. Hence the first |
| 1868 |
derivative is recomputed, and the order is set to 1. Then |
| 1869 |
h is reduced by a factor of 10, and the step is retried, |
| 1870 |
until it succeeds or h reaches hmin. |
| 1871 |
*/ |
| 1872 |
else { |
| 1873 |
if (kflag == -10) { |
| 1874 |
kflag = -1; |
| 1875 |
hold = h; |
| 1876 |
jstart = 1; |
| 1877 |
break; |
| 1878 |
} else { |
| 1879 |
rh = 0.1; |
| 1880 |
rh = max(hmin / fabs(h), rh); |
| 1881 |
h *= rh; |
| 1882 |
yp1 = yh[1]; |
| 1883 |
for (i = 1; i <= n; i++) |
| 1884 |
y[i] = yp1[i]; |
| 1885 |
f(neq, tn, y, savf); |
| 1886 |
nfe++; |
| 1887 |
yp1 = yh[2]; |
| 1888 |
for (i = 1; i <= n; i++) |
| 1889 |
yp1[i] = h * savf[i]; |
| 1890 |
ipup = miter; |
| 1891 |
ialth = 5; |
| 1892 |
if (nq == 1) |
| 1893 |
continue; |
| 1894 |
nq = 1; |
| 1895 |
l = 2; |
| 1896 |
resetcoeff(); |
| 1897 |
continue; |
| 1898 |
} |
| 1899 |
} /* end else -- kflag <= -3 */ |
| 1900 |
} /* end error failure handling */ |
| 1901 |
} /* end outer while */ |
| 1902 |
|
| 1903 |
} /* end stoda */ |
| 1904 |
|
| 1905 |
void QTAIMLSODAIntegrator::ewset(int itol, double* rtol, double* atol, |
| 1906 |
double* ycur) |
| 1907 |
{ |
| 1908 |
int i; |
| 1909 |
|
| 1910 |
switch (itol) { |
| 1911 |
case 1: |
| 1912 |
for (i = 1; i <= n; i++) |
| 1913 |
ewt[i] = rtol[1] * fabs(ycur[i]) + atol[1]; |
| 1914 |
break; |
| 1915 |
case 2: |
| 1916 |
for (i = 1; i <= n; i++) |
| 1917 |
ewt[i] = rtol[1] * fabs(ycur[i]) + atol[i]; |
| 1918 |
break; |
| 1919 |
case 3: |
| 1920 |
for (i = 1; i <= n; i++) |
| 1921 |
ewt[i] = rtol[i] * fabs(ycur[i]) + atol[1]; |
| 1922 |
break; |
| 1923 |
case 4: |
| 1924 |
for (i = 1; i <= n; i++) |
| 1925 |
ewt[i] = rtol[i] * fabs(ycur[i]) + atol[i]; |
| 1926 |
break; |
| 1927 |
} |
| 1928 |
|
| 1929 |
} /* end ewset */ |
| 1930 |
|
| 1931 |
void QTAIMLSODAIntegrator::intdy(double t, int k, double* dky, int* iflag) |
| 1932 |
/* |
| 1933 |
Intdy computes interpolated values of the k-th derivative of the |
| 1934 |
dependent variable vector y, and stores it in dky. This routine |
| 1935 |
is called within the package with k = 0 and *t = tout, but may |
| 1936 |
also be called by the user for any k up to the current order. |
| 1937 |
( See detailed instructions in the usage documentation. ) |
| 1938 |
|
| 1939 |
The computed values in dky are gotten by interpolation using the |
| 1940 |
Nordsieck history array yh. This array corresponds uniquely to a |
| 1941 |
vector-valued polynomial of degree nqcur or less, and dky is set |
| 1942 |
to the k-th derivative of this polynomial at t. |
| 1943 |
The formula for dky is |
| 1944 |
|
| 1945 |
q |
| 1946 |
dky[i] = sum c[k][j] * ( t - tn )^(j-k) * h^(-j) * yh[j+1][i] |
| 1947 |
j=k |
| 1948 |
|
| 1949 |
where c[k][j] = j*(j-1)*...*(j-k+1), q = nqcur, tn = tcur, h = hcur. |
| 1950 |
The quantities nq = nqcur, l = nq+1, n = neq, tn, and h are declared |
| 1951 |
static globally. The above sum is done in reverse order. |
| 1952 |
*iflag is returned negative if either k or t is out of bounds. |
| 1953 |
*/ |
| 1954 |
|
| 1955 |
{ |
| 1956 |
int i, ic, j, jj, jp1; |
| 1957 |
double c, r, s, tp; |
| 1958 |
|
| 1959 |
*iflag = 0; |
| 1960 |
if (k < 0 || k > nq) { |
| 1961 |
qDebug("intdy -- k = %d illegal", k); |
| 1962 |
*iflag = -1; |
| 1963 |
return; |
| 1964 |
} |
| 1965 |
tp = tn - hu - 100. * ETA * (tn + hu); |
| 1966 |
if ((t - tp) * (t - tn) > 0.) { |
| 1967 |
qDebug("intdy -- t = %g illegal", t); |
| 1968 |
qDebug(" t not in interval tcur - hu to tcur"); |
| 1969 |
*iflag = -2; |
| 1970 |
return; |
| 1971 |
} |
| 1972 |
|
| 1973 |
s = (t - tn) / h; |
| 1974 |
ic = 1; |
| 1975 |
for (jj = l - k; jj <= nq; jj++) |
| 1976 |
ic *= jj; |
| 1977 |
c = (double)ic; |
| 1978 |
yp1 = yh[l]; |
| 1979 |
for (i = 1; i <= n; i++) |
| 1980 |
dky[i] = c * yp1[i]; |
| 1981 |
for (j = nq - 1; j >= k; j--) { |
| 1982 |
jp1 = j + 1; |
| 1983 |
ic = 1; |
| 1984 |
for (jj = jp1 - k; jj <= j; jj++) |
| 1985 |
ic *= jj; |
| 1986 |
c = (double)ic; |
| 1987 |
yp1 = yh[jp1]; |
| 1988 |
for (i = 1; i <= n; i++) |
| 1989 |
dky[i] = c * yp1[i] + s * dky[i]; |
| 1990 |
} |
| 1991 |
if (k == 0) |
| 1992 |
return; |
| 1993 |
r = pow(h, (double)(-k)); |
| 1994 |
for (i = 1; i <= n; i++) |
| 1995 |
dky[i] *= r; |
| 1996 |
|
| 1997 |
} /* end intdy */ |
| 1998 |
|
| 1999 |
void QTAIMLSODAIntegrator::cfode(int meth_) |
| 2000 |
{ |
| 2001 |
int i, nq_, nqm1, nqp1; |
| 2002 |
double agamq, fnq, fnqm1, pc[13], pint, ragq, rqfac, rq1fac, tsign, xpin; |
| 2003 |
/* |
| 2004 |
cfode is called by the integrator routine to set coefficients |
| 2005 |
needed there. The coefficients for the current method, as |
| 2006 |
given by the value of meth, are set for all orders and saved. |
| 2007 |
The maximum order assumed here is 12 if meth = 1 and 5 if meth = 2. |
| 2008 |
( A smaller value of the maximum order is also allowed. ) |
| 2009 |
cfode is called once at the beginning of the problem, and |
| 2010 |
is not called again unless and until meth is changed. |
| 2011 |
|
| 2012 |
The elco array contains the basic method coefficients. |
| 2013 |
The coefficients el[i], 1 < i < nq+1, for the method of |
| 2014 |
order nq are stored in elco[nq][i]. They are given by a generating |
| 2015 |
polynomial, i.e., |
| 2016 |
|
| 2017 |
l(x) = el[1] + el[2]*x + ... + el[nq+1]*x^nq. |
| 2018 |
|
| 2019 |
For the implicit Adams method, l(x) is given by |
| 2020 |
|
| 2021 |
dl/dx = (x+1)*(x+2)*...*(x+nq-1)/factorial(nq-1), l(-1) = 0. |
| 2022 |
|
| 2023 |
For the bdf methods, l(x) is given by |
| 2024 |
|
| 2025 |
l(x) = (x+1)*(x+2)*...*(x+nq)/k, |
| 2026 |
|
| 2027 |
where k = factorial(nq)*(1+1/2+...+1/nq). |
| 2028 |
|
| 2029 |
The tesco array contains test constants used for the |
| 2030 |
local error test and the selection of step size and/or order. |
| 2031 |
At order nq, tesco[nq][k] is used for the selection of step |
| 2032 |
size at order nq-1 if k = 1, at order nq if k = 2, and at order |
| 2033 |
nq+1 if k = 3. |
| 2034 |
*/ |
| 2035 |
if (meth_ == 1) { |
| 2036 |
elco[1][1] = 1.; |
| 2037 |
elco[1][2] = 1.; |
| 2038 |
tesco[1][1] = 0.; |
| 2039 |
tesco[1][2] = 2.; |
| 2040 |
tesco[2][1] = 1.; |
| 2041 |
tesco[12][3] = 0.; |
| 2042 |
pc[1] = 1.; |
| 2043 |
rqfac = 1.; |
| 2044 |
for (nq_ = 2; nq_ <= 12; nq_++) { |
| 2045 |
/* |
| 2046 |
The pc array will contain the coefficients of the polynomial |
| 2047 |
|
| 2048 |
p(x) = (x+1)*(x+2)*...*(x+nq-1). |
| 2049 |
|
| 2050 |
Initially, p(x) = 1. |
| 2051 |
*/ |
| 2052 |
rq1fac = rqfac; |
| 2053 |
rqfac = rqfac / (double)nq_; |
| 2054 |
nqm1 = nq_ - 1; |
| 2055 |
fnqm1 = (double)nqm1; |
| 2056 |
nqp1 = nq_ + 1; |
| 2057 |
/* |
| 2058 |
Form coefficients of p(x)*(x+nq-1). |
| 2059 |
*/ |
| 2060 |
pc[nq_] = 0.; |
| 2061 |
for (i = nq_; i >= 2; i--) |
| 2062 |
pc[i] = pc[i - 1] + fnqm1 * pc[i]; |
| 2063 |
pc[1] = fnqm1 * pc[1]; |
| 2064 |
/* |
| 2065 |
Compute integral, -1 to 0, of p(x) and x*p(x). |
| 2066 |
*/ |
| 2067 |
pint = pc[1]; |
| 2068 |
xpin = pc[1] / 2.; |
| 2069 |
tsign = 1.; |
| 2070 |
for (i = 2; i <= nq_; i++) { |
| 2071 |
tsign = -tsign; |
| 2072 |
pint += tsign * pc[i] / (double)i; |
| 2073 |
xpin += tsign * pc[i] / (double)(i + 1); |
| 2074 |
} |
| 2075 |
/* |
| 2076 |
Store coefficients in elco and tesco. |
| 2077 |
*/ |
| 2078 |
elco[nq_][1] = pint * rq1fac; |
| 2079 |
elco[nq_][2] = 1.; |
| 2080 |
for (i = 2; i <= nq_; i++) |
| 2081 |
elco[nq_][i + 1] = rq1fac * pc[i] / (double)i; |
| 2082 |
agamq = rqfac * xpin; |
| 2083 |
ragq = 1. / agamq; |
| 2084 |
tesco[nq_][2] = ragq; |
| 2085 |
if (nq_ < 12) |
| 2086 |
tesco[nqp1][1] = ragq * rqfac / (double)nqp1; |
| 2087 |
tesco[nqm1][3] = ragq; |
| 2088 |
} /* end for */ |
| 2089 |
return; |
| 2090 |
} /* end if ( meth == 1 ) */ |
| 2091 |
|
| 2092 |
/* |
| 2093 |
meth = 2. |
| 2094 |
*/ |
| 2095 |
pc[1] = 1.; |
| 2096 |
rq1fac = 1.; |
| 2097 |
/* |
| 2098 |
The pc array will contain the coefficients of the polynomial |
| 2099 |
|
| 2100 |
p(x) = (x+1)*(x+2)*...*(x+nq). |
| 2101 |
|
| 2102 |
Initially, p(x) = 1. |
| 2103 |
*/ |
| 2104 |
for (nq_ = 1; nq_ <= 5; nq_++) { |
| 2105 |
fnq = (double)nq_; |
| 2106 |
nqp1 = nq_ + 1; |
| 2107 |
/* |
| 2108 |
Form coefficients of p(x)*(x+nq). |
| 2109 |
*/ |
| 2110 |
pc[nqp1] = 0.; |
| 2111 |
for (i = nq_ + 1; i >= 2; i--) |
| 2112 |
pc[i] = pc[i - 1] + fnq * pc[i]; |
| 2113 |
pc[1] *= fnq; |
| 2114 |
/* |
| 2115 |
Store coefficients in elco and tesco. |
| 2116 |
*/ |
| 2117 |
for (i = 1; i <= nqp1; i++) |
| 2118 |
elco[nq_][i] = pc[i] / pc[2]; |
| 2119 |
elco[nq_][2] = 1.; |
| 2120 |
tesco[nq_][1] = rq1fac; |
| 2121 |
tesco[nq_][2] = ((double)nqp1) / elco[nq_][1]; |
| 2122 |
tesco[nq_][3] = ((double)(nq_ + 2)) / elco[nq_][1]; |
| 2123 |
rq1fac /= fnq; |
| 2124 |
} |
| 2125 |
} /* end cfode */ |
| 2126 |
|
| 2127 |
void QTAIMLSODAIntegrator::scaleh(double* rh, double* pdh) |
| 2128 |
{ |
| 2129 |
double r; |
| 2130 |
int j, i; |
| 2131 |
/* |
| 2132 |
If h is being changed, the h ratio rh is checked against |
| 2133 |
rmax, hmin, and hmxi, and the yh array is rescaled. ialth is set to |
| 2134 |
l = nq + 1 to prevent a change of h for that many steps, unless |
| 2135 |
forced by a convergence or error test failure. |
| 2136 |
*/ |
| 2137 |
*rh = min(*rh, rmax); |
| 2138 |
*rh = *rh / max(1., fabs(h) * hmxi * *rh); |
| 2139 |
/* |
| 2140 |
If meth = 1, also restrict the new step size by the stability region. |
| 2141 |
If this reduces h, set irflag to 1 so that if there are roundoff |
| 2142 |
problems later, we can assume that is the cause of the trouble. |
| 2143 |
*/ |
| 2144 |
if (meth == 1) { |
| 2145 |
irflag = 0; |
| 2146 |
*pdh = max(fabs(h) * pdlast, 0.000001); |
| 2147 |
if ((*rh * *pdh * 1.00001) >= sm1[nq]) { |
| 2148 |
*rh = sm1[nq] / *pdh; |
| 2149 |
irflag = 1; |
| 2150 |
} |
| 2151 |
} |
| 2152 |
r = 1.; |
| 2153 |
for (j = 2; j <= l; j++) { |
| 2154 |
r *= *rh; |
| 2155 |
yp1 = yh[j]; |
| 2156 |
for (i = 1; i <= n; i++) |
| 2157 |
yp1[i] *= r; |
| 2158 |
} |
| 2159 |
h *= *rh; |
| 2160 |
rc *= *rh; |
| 2161 |
ialth = l; |
| 2162 |
|
| 2163 |
} /* end scaleh */ |
| 2164 |
|
| 2165 |
void QTAIMLSODAIntegrator::prja(int neq, double* y) |
| 2166 |
{ |
| 2167 |
int i, ier, j; |
| 2168 |
double fac, hl0, r, r0, yj; |
| 2169 |
/* |
| 2170 |
prja is called by stoda to compute and process the matrix |
| 2171 |
P = I - h * el[1] * J, where J is an approximation to the Jacobian. |
| 2172 |
Here J is computed by finite differencing. |
| 2173 |
J, scaled by -h * el[1], is stored in wm. Then the norm of J ( the |
| 2174 |
matrix norm consistent with the weighted max-norm on vectors given |
| 2175 |
by vmnorm ) is computed, and J is overwritten by P. P is then |
| 2176 |
subjected to LU decomposition in preparation for later solution |
| 2177 |
of linear systems with p as coefficient matrix. This is done |
| 2178 |
by dgefa if miter = 2, and by dgbfa if miter = 5. |
| 2179 |
*/ |
| 2180 |
nje++; |
| 2181 |
ierpj = 0; |
| 2182 |
jcur = 1; |
| 2183 |
hl0 = h * el0; |
| 2184 |
/* |
| 2185 |
If miter = 2, make n calls to f to approximate J. |
| 2186 |
*/ |
| 2187 |
if (miter != 2) { |
| 2188 |
qDebug("prja -- miter != 2"); |
| 2189 |
return; |
| 2190 |
} |
| 2191 |
|
| 2192 |
if (miter == 2) { |
| 2193 |
fac = vmnorm(n, savf, ewt); |
| 2194 |
r0 = 1000. * fabs(h) * ETA * ((double)n) * fac; |
| 2195 |
if (r0 == 0.) |
| 2196 |
r0 = 1.; |
| 2197 |
for (j = 1; j <= n; j++) { |
| 2198 |
yj = y[j]; |
| 2199 |
r = max(sqrteta * fabs(yj), r0 / ewt[j]); |
| 2200 |
y[j] += r; |
| 2201 |
fac = -hl0 / r; |
| 2202 |
f(neq, tn, y, acor); |
| 2203 |
for (i = 1; i <= n; i++) |
| 2204 |
wm[i][j] = (acor[i] - savf[i]) * fac; |
| 2205 |
y[j] = yj; |
| 2206 |
} |
| 2207 |
nfe += n; |
| 2208 |
/* |
| 2209 |
Compute norm of Jacobian. |
| 2210 |
*/ |
| 2211 |
pdnorm = fnorm(n, wm, ewt) / fabs(hl0); |
| 2212 |
/* |
| 2213 |
Add identity matrix. |
| 2214 |
*/ |
| 2215 |
for (i = 1; i <= n; i++) |
| 2216 |
wm[i][i] += 1.; |
| 2217 |
/* |
| 2218 |
Do LU decomposition on P. |
| 2219 |
*/ |
| 2220 |
dgefa(wm, n, ipvt, &ier); |
| 2221 |
if (ier != 0) |
| 2222 |
ierpj = 1; |
| 2223 |
return; |
| 2224 |
} |
| 2225 |
|
| 2226 |
} /* end prja */ |
| 2227 |
|
| 2228 |
double QTAIMLSODAIntegrator::vmnorm(int n_, double* v, double* w) |
| 2229 |
/* |
| 2230 |
This function routine computes the weighted max-norm |
| 2231 |
of the vector of length n contained in the array v, with weights |
| 2232 |
contained in the array w of length n. |
| 2233 |
|
| 2234 |
vmnorm = max( i = 1, ..., n ) fabs( v[i] ) * w[i]. |
| 2235 |
*/ |
| 2236 |
|
| 2237 |
{ |
| 2238 |
int i; |
| 2239 |
double vm; |
| 2240 |
|
| 2241 |
vm = 0.; |
| 2242 |
for (i = 1; i <= n_; i++) |
| 2243 |
vm = max(vm, fabs(v[i]) * w[i]); |
| 2244 |
return vm; |
| 2245 |
|
| 2246 |
} /* end vmnorm */ |
| 2247 |
|
| 2248 |
double QTAIMLSODAIntegrator::fnorm(int n_, double** a, double* w) |
| 2249 |
/* |
| 2250 |
This subroutine computes the norm of a full n by n matrix, |
| 2251 |
stored in the array a, that is consistent with the weighted max-norm |
| 2252 |
on vectors, with weights stored in the array w. |
| 2253 |
|
| 2254 |
fnorm = max(i=1,...,n) ( w[i] * sum(j=1,...,n) fabs( a[i][j] ) / w[j] ) |
| 2255 |
*/ |
| 2256 |
|
| 2257 |
{ |
| 2258 |
int i, j; |
| 2259 |
double an, sum, *ap1; |
| 2260 |
|
| 2261 |
an = 0.; |
| 2262 |
for (i = 1; i <= n_; i++) { |
| 2263 |
sum = 0.; |
| 2264 |
ap1 = a[i]; |
| 2265 |
for (j = 1; j <= n_; j++) |
| 2266 |
sum += fabs(ap1[j]) / w[j]; |
| 2267 |
an = max(an, sum * w[i]); |
| 2268 |
} |
| 2269 |
return an; |
| 2270 |
|
| 2271 |
} /* end fnorm */ |
| 2272 |
|
| 2273 |
// double QTAIMLSODAIntegrator::bnorm() |
| 2274 |
// { |
| 2275 |
// } /* end bnorm */ |
| 2276 |
|
| 2277 |
void QTAIMLSODAIntegrator::correction(int neq, double* y, int* corflag, |
| 2278 |
double pnorm, double* del, double* delp, |
| 2279 |
double* told, int* ncf, double* rh, |
| 2280 |
int* m) |
| 2281 |
/* |
| 2282 |
*corflag = 0 : corrector converged, |
| 2283 |
1 : step size to be reduced, redo prediction, |
| 2284 |
2 : corrector cannot converge, failure flag. |
| 2285 |
*/ |
| 2286 |
|
| 2287 |
{ |
| 2288 |
int i; |
| 2289 |
double rm, rate, dcon; |
| 2290 |
|
| 2291 |
/* |
| 2292 |
Up to maxcor corrector iterations are taken. A convergence test is |
| 2293 |
made on the r.m.s. norm of each correction, weighted by the error |
| 2294 |
weight vector ewt. The sum of the corrections is accumulated in the |
| 2295 |
vector acor[i]. The yh array is not altered in the corrector loop. |
| 2296 |
*/ |
| 2297 |
|
| 2298 |
*m = 0; |
| 2299 |
*corflag = 0; |
| 2300 |
rate = 0.; |
| 2301 |
*del = 0.; |
| 2302 |
yp1 = yh[1]; |
| 2303 |
for (i = 1; i <= n; i++) |
| 2304 |
y[i] = yp1[i]; |
| 2305 |
f(neq, tn, y, savf); |
| 2306 |
nfe++; |
| 2307 |
/* |
| 2308 |
If indicated, the matrix P = I - h * el[1] * J is reevaluated and |
| 2309 |
preprocessed before starting the corrector iteration. ipup is set |
| 2310 |
to 0 as an indicator that this has been done. |
| 2311 |
*/ |
| 2312 |
while (true) { |
| 2313 |
if (*m == 0) { |
| 2314 |
if (ipup > 0) { |
| 2315 |
prja(neq, y); |
| 2316 |
ipup = 0; |
| 2317 |
rc = 1.; |
| 2318 |
nslp = nst; |
| 2319 |
crate = 0.7; |
| 2320 |
if (ierpj != 0) { |
| 2321 |
corfailure(told, rh, ncf, corflag); |
| 2322 |
return; |
| 2323 |
} |
| 2324 |
} |
| 2325 |
for (i = 1; i <= n; i++) |
| 2326 |
acor[i] = 0.; |
| 2327 |
} /* end if ( *m == 0 ) */ |
| 2328 |
if (miter == 0) { |
| 2329 |
/* |
| 2330 |
In case of functional iteration, update y directly from |
| 2331 |
the result of the last function evaluation. |
| 2332 |
*/ |
| 2333 |
yp1 = yh[2]; |
| 2334 |
for (i = 1; i <= n; i++) { |
| 2335 |
savf[i] = h * savf[i] - yp1[i]; |
| 2336 |
y[i] = savf[i] - acor[i]; |
| 2337 |
} |
| 2338 |
*del = vmnorm(n, y, ewt); |
| 2339 |
yp1 = yh[1]; |
| 2340 |
for (i = 1; i <= n; i++) { |
| 2341 |
y[i] = yp1[i] + el[1] * savf[i]; |
| 2342 |
acor[i] = savf[i]; |
| 2343 |
} |
| 2344 |
} /* end functional iteration */ |
| 2345 |
/* |
| 2346 |
In the case of the chord method, compute the corrector error, |
| 2347 |
and solve the linear system with that as right-hand side and |
| 2348 |
P as coefficient matrix. |
| 2349 |
*/ |
| 2350 |
else { |
| 2351 |
yp1 = yh[2]; |
| 2352 |
for (i = 1; i <= n; i++) |
| 2353 |
y[i] = h * savf[i] - (yp1[i] + acor[i]); |
| 2354 |
solsy(y); |
| 2355 |
*del = vmnorm(n, y, ewt); |
| 2356 |
yp1 = yh[1]; |
| 2357 |
for (i = 1; i <= n; i++) { |
| 2358 |
acor[i] += y[i]; |
| 2359 |
y[i] = yp1[i] + el[1] * acor[i]; |
| 2360 |
} |
| 2361 |
} /* end chord method */ |
| 2362 |
/* |
| 2363 |
Test for convergence. If *m > 0, an estimate of the convergence |
| 2364 |
rate constant is stored in crate, and this is used in the test. |
| 2365 |
|
| 2366 |
We first check for a change of iterates that is the size of |
| 2367 |
roundoff error. If this occurs, the iteration has converged, and a |
| 2368 |
new rate estimate is not formed. |
| 2369 |
In all other cases, force at least two iterations to estimate a |
| 2370 |
local Lipschitz constant estimate for Adams method. |
| 2371 |
On convergence, form pdest = local maximum Lipschitz constant |
| 2372 |
estimate. pdlast is the most recent nonzero estimate. |
| 2373 |
*/ |
| 2374 |
if (*del <= 100. * pnorm * ETA) |
| 2375 |
break; |
| 2376 |
if (*m != 0 || meth != 1) { |
| 2377 |
if (*m != 0) { |
| 2378 |
rm = 1024.0; |
| 2379 |
if (*del <= (1024. * *delp)) |
| 2380 |
rm = *del / *delp; |
| 2381 |
rate = max(rate, rm); |
| 2382 |
crate = max(0.2 * crate, rm); |
| 2383 |
} |
| 2384 |
dcon = *del * min(1., 1.5 * crate) / (tesco[nq][2] * conit); |
| 2385 |
if (dcon <= 1.) { |
| 2386 |
pdest = max(pdest, rate / fabs(h * el[1])); |
| 2387 |
if (pdest != 0.) |
| 2388 |
pdlast = pdest; |
| 2389 |
break; |
| 2390 |
} |
| 2391 |
} |
| 2392 |
/* |
| 2393 |
The corrector iteration failed to converge. |
| 2394 |
If miter != 0 and the Jacobian is out of date, prja is called for |
| 2395 |
the next try. Otherwise the yh array is retracted to its values |
| 2396 |
before prediction, and h is reduced, if possible. If h cannot be |
| 2397 |
reduced or mxncf failures have occurred, exit with corflag = 2. |
| 2398 |
*/ |
| 2399 |
(*m)++; |
| 2400 |
if (*m == maxcor || (*m >= 2 && *del > 2. * *delp)) { |
| 2401 |
if (miter == 0 || jcur == 1) { |
| 2402 |
corfailure(told, rh, ncf, corflag); |
| 2403 |
return; |
| 2404 |
} |
| 2405 |
ipup = miter; |
| 2406 |
/* |
| 2407 |
Restart corrector if Jacobian is recomputed. |
| 2408 |
*/ |
| 2409 |
*m = 0; |
| 2410 |
rate = 0.; |
| 2411 |
*del = 0.; |
| 2412 |
yp1 = yh[1]; |
| 2413 |
for (i = 1; i <= n; i++) |
| 2414 |
y[i] = yp1[i]; |
| 2415 |
f(neq, tn, y, savf); |
| 2416 |
nfe++; |
| 2417 |
} |
| 2418 |
/* |
| 2419 |
Iterate corrector. |
| 2420 |
*/ |
| 2421 |
else { |
| 2422 |
*delp = *del; |
| 2423 |
f(neq, tn, y, savf); |
| 2424 |
nfe++; |
| 2425 |
} |
| 2426 |
} /* end while */ |
| 2427 |
} /* end correction */ |
| 2428 |
|
| 2429 |
void QTAIMLSODAIntegrator::corfailure(double* told, double* rh, int* ncf, |
| 2430 |
int* corflag) |
| 2431 |
{ |
| 2432 |
int j, i1, i; |
| 2433 |
|
| 2434 |
(*ncf)++; |
| 2435 |
rmax = 2.; |
| 2436 |
tn = *told; |
| 2437 |
for (j = nq; j >= 1; j--) |
| 2438 |
for (i1 = j; i1 <= nq; i1++) { |
| 2439 |
yp1 = yh[i1]; |
| 2440 |
yp2 = yh[i1 + 1]; |
| 2441 |
for (i = 1; i <= n; i++) |
| 2442 |
yp1[i] -= yp2[i]; |
| 2443 |
} |
| 2444 |
if (fabs(h) <= hmin * 1.00001 || *ncf == mxncf) { |
| 2445 |
*corflag = 2; |
| 2446 |
return; |
| 2447 |
} |
| 2448 |
*corflag = 1; |
| 2449 |
*rh = 0.25; |
| 2450 |
ipup = miter; |
| 2451 |
|
| 2452 |
} /* end corfailure */ |
| 2453 |
|
| 2454 |
void QTAIMLSODAIntegrator::solsy(double* y) |
| 2455 |
/* |
| 2456 |
This routine manages the solution of the linear system arising from |
| 2457 |
a chord iteration. It is called if miter != 0. |
| 2458 |
If miter is 2, it calls dgesl to accomplish this. |
| 2459 |
If miter is 5, it calls dgbsl. |
| 2460 |
|
| 2461 |
y = the right-hand side vector on input, and the solution vector |
| 2462 |
on output. |
| 2463 |
*/ |
| 2464 |
{ |
| 2465 |
iersl = 0; |
| 2466 |
if (miter != 2) { |
| 2467 |
qDebug("solsy -- miter != 2"); |
| 2468 |
return; |
| 2469 |
} |
| 2470 |
|
| 2471 |
if (miter == 2) |
| 2472 |
dgesl(wm, n, ipvt, y, 0); |
| 2473 |
|
| 2474 |
} /* end solsy */ |
| 2475 |
|
| 2476 |
void QTAIMLSODAIntegrator::methodswitch(double dsm, double pnorm, double* pdh, |
| 2477 |
double* rh) |
| 2478 |
{ |
| 2479 |
int lm1, lm1p1, lm2, lm2p1, nqm1, nqm2; |
| 2480 |
double rh1, rh2, rh1it, exm2, dm2, exm1, dm1, alpha, exsm; |
| 2481 |
|
| 2482 |
/* |
| 2483 |
We are current using an Adams method. Consider switching to bdf. |
| 2484 |
If the current order is greater than 5, assume the problem is |
| 2485 |
not stiff, and skip this section. |
| 2486 |
If the Lipschitz constant and error estimate are not polluted |
| 2487 |
by roundoff, perform the usual test. |
| 2488 |
Otherwise, switch to the bdf methods if the last step was |
| 2489 |
restricted to insure stability ( irflag = 1 ), and stay with Adams |
| 2490 |
method if not. When switching to bdf with polluted error estimates, |
| 2491 |
in the absence of other information, double the step size. |
| 2492 |
|
| 2493 |
When the estimates are ok, we make the usual test by computing |
| 2494 |
the step size we could have (ideally) used on this step, |
| 2495 |
with the current (Adams) method, and also that for the bdf. |
| 2496 |
If nq > mxords, we consider changing to order mxords on switching. |
| 2497 |
Compare the two step sizes to decide whether to switch. |
| 2498 |
The step size advantage must be at least ratio = 5 to switch. |
| 2499 |
*/ |
| 2500 |
if (meth == 1) { |
| 2501 |
if (nq > 5) |
| 2502 |
return; |
| 2503 |
if (dsm <= (100. * pnorm * ETA) || pdest == 0.) { |
| 2504 |
if (irflag == 0) |
| 2505 |
return; |
| 2506 |
rh2 = 2.; |
| 2507 |
nqm2 = min(nq, mxords); |
| 2508 |
} else { |
| 2509 |
exsm = 1. / (double)l; |
| 2510 |
rh1 = 1. / (1.2 * pow(dsm, exsm) + 0.0000012); |
| 2511 |
rh1it = 2. * rh1; |
| 2512 |
*pdh = pdlast * fabs(h); |
| 2513 |
if ((*pdh * rh1) > 0.00001) |
| 2514 |
rh1it = sm1[nq] / *pdh; |
| 2515 |
rh1 = min(rh1, rh1it); |
| 2516 |
if (nq > mxords) { |
| 2517 |
nqm2 = mxords; |
| 2518 |
lm2 = mxords + 1; |
| 2519 |
exm2 = 1. / (double)lm2; |
| 2520 |
lm2p1 = lm2 + 1; |
| 2521 |
dm2 = vmnorm(n, yh[lm2p1], ewt) / cm2[mxords]; |
| 2522 |
rh2 = 1. / (1.2 * pow(dm2, exm2) + 0.0000012); |
| 2523 |
} else { |
| 2524 |
dm2 = dsm * (cm1[nq] / cm2[nq]); |
| 2525 |
rh2 = 1. / (1.2 * pow(dm2, exsm) + 0.0000012); |
| 2526 |
nqm2 = nq; |
| 2527 |
} |
| 2528 |
if (rh2 < ratio * rh1) |
| 2529 |
return; |
| 2530 |
} |
| 2531 |
/* |
| 2532 |
The method switch test passed. Reset relevant quantities for bdf. |
| 2533 |
*/ |
| 2534 |
*rh = rh2; |
| 2535 |
icount = 20; |
| 2536 |
meth = 2; |
| 2537 |
miter = jtyp; |
| 2538 |
pdlast = 0.; |
| 2539 |
nq = nqm2; |
| 2540 |
l = nq + 1; |
| 2541 |
return; |
| 2542 |
} /* end if ( meth == 1 ) */ |
| 2543 |
/* |
| 2544 |
We are currently using a bdf method, considering switching to Adams. |
| 2545 |
Compute the step size we could have (ideally) used on this step, |
| 2546 |
with the current (bdf) method, and also that for the Adams. |
| 2547 |
If nq > mxordn, we consider changing to order mxordn on switching. |
| 2548 |
Compare the two step sizes to decide whether to switch. |
| 2549 |
The step size advantage must be at least 5/ratio = 1 to switch. |
| 2550 |
If the step size for Adams would be so small as to cause |
| 2551 |
roundoff pollution, we stay with bdf. |
| 2552 |
*/ |
| 2553 |
exsm = 1. / (double)l; |
| 2554 |
if (mxordn < nq) { |
| 2555 |
nqm1 = mxordn; |
| 2556 |
lm1 = mxordn + 1; |
| 2557 |
exm1 = 1. / (double)lm1; |
| 2558 |
lm1p1 = lm1 + 1; |
| 2559 |
dm1 = vmnorm(n, yh[lm1p1], ewt) / cm1[mxordn]; |
| 2560 |
rh1 = 1. / (1.2 * pow(dm1, exm1) + 0.0000012); |
| 2561 |
} else { |
| 2562 |
dm1 = dsm * (cm2[nq] / cm1[nq]); |
| 2563 |
rh1 = 1. / (1.2 * pow(dm1, exsm) + 0.0000012); |
| 2564 |
nqm1 = nq; |
| 2565 |
exm1 = exsm; |
| 2566 |
} |
| 2567 |
rh1it = 2. * rh1; |
| 2568 |
*pdh = pdnorm * fabs(h); |
| 2569 |
if ((*pdh * rh1) > 0.00001) |
| 2570 |
rh1it = sm1[nqm1] / *pdh; |
| 2571 |
rh1 = min(rh1, rh1it); |
| 2572 |
rh2 = 1. / (1.2 * pow(dsm, exsm) + 0.0000012); |
| 2573 |
if ((rh1 * ratio) < (5. * rh2)) |
| 2574 |
return; |
| 2575 |
alpha = max(0.001, rh1); |
| 2576 |
dm1 *= pow(alpha, exm1); |
| 2577 |
if (dm1 <= 1000. * ETA * pnorm) |
| 2578 |
return; |
| 2579 |
/* |
| 2580 |
The switch test passed. Reset relevant quantities for Adams. |
| 2581 |
*/ |
| 2582 |
*rh = rh1; |
| 2583 |
icount = 20; |
| 2584 |
meth = 1; |
| 2585 |
miter = 0; |
| 2586 |
pdlast = 0.; |
| 2587 |
nq = nqm1; |
| 2588 |
l = nq + 1; |
| 2589 |
|
| 2590 |
} /* end methodswitch */ |
| 2591 |
|
| 2592 |
/* |
| 2593 |
This routine returns from stoda to lsoda. Hence freevectors() is |
| 2594 |
not executed. |
| 2595 |
*/ |
| 2596 |
|
| 2597 |
void QTAIMLSODAIntegrator::endstoda() |
| 2598 |
{ |
| 2599 |
double r; |
| 2600 |
int i; |
| 2601 |
|
| 2602 |
r = 1. / tesco[nqu][2]; |
| 2603 |
for (i = 1; i <= n; i++) |
| 2604 |
acor[i] *= r; |
| 2605 |
hold = h; |
| 2606 |
jstart = 1; |
| 2607 |
|
| 2608 |
} /* end endstoda */ |
| 2609 |
|
| 2610 |
void QTAIMLSODAIntegrator::orderswitch(double* rhup, double dsm, double* pdh, |
| 2611 |
double* rh, int* orderflag) |
| 2612 |
/* |
| 2613 |
Regardless of the success or failure of the step, factors |
| 2614 |
rhdn, rhsm, and rhup are computed, by which h could be multiplied |
| 2615 |
at order nq - 1, order nq, or order nq + 1, respectively. |
| 2616 |
In the case of a failure, rhup = 0. to avoid an order increase. |
| 2617 |
The largest of these is determined and the new order chosen |
| 2618 |
accordingly. If the order is to be increased, we compute one |
| 2619 |
additional scaled derivative. |
| 2620 |
|
| 2621 |
orderflag = 0 : no change in h or nq, |
| 2622 |
1 : change in h but not nq, |
| 2623 |
2 : change in both h and nq. |
| 2624 |
*/ |
| 2625 |
{ |
| 2626 |
int newq, i; |
| 2627 |
double exsm, rhdn, rhsm, ddn, exdn, r; |
| 2628 |
|
| 2629 |
*orderflag = 0; |
| 2630 |
|
| 2631 |
exsm = 1. / (double)l; |
| 2632 |
rhsm = 1. / (1.2 * pow(dsm, exsm) + 0.0000012); |
| 2633 |
|
| 2634 |
rhdn = 0.; |
| 2635 |
if (nq != 1) { |
| 2636 |
ddn = vmnorm(n, yh[l], ewt) / tesco[nq][1]; |
| 2637 |
exdn = 1. / (double)nq; |
| 2638 |
rhdn = 1. / (1.3 * pow(ddn, exdn) + 0.0000013); |
| 2639 |
} |
| 2640 |
/* |
| 2641 |
If meth = 1, limit rh according to the stability region also. |
| 2642 |
*/ |
| 2643 |
if (meth == 1) { |
| 2644 |
*pdh = max(fabs(h) * pdlast, 0.000001); |
| 2645 |
if (l < lmax) |
| 2646 |
*rhup = min(*rhup, sm1[l] / *pdh); |
| 2647 |
rhsm = min(rhsm, sm1[nq] / *pdh); |
| 2648 |
if (nq > 1) |
| 2649 |
rhdn = min(rhdn, sm1[nq - 1] / *pdh); |
| 2650 |
pdest = 0.; |
| 2651 |
} |
| 2652 |
if (rhsm >= *rhup) { |
| 2653 |
if (rhsm >= rhdn) { |
| 2654 |
newq = nq; |
| 2655 |
*rh = rhsm; |
| 2656 |
} else { |
| 2657 |
newq = nq - 1; |
| 2658 |
*rh = rhdn; |
| 2659 |
if (kflag < 0 && *rh > 1.) |
| 2660 |
*rh = 1.; |
| 2661 |
} |
| 2662 |
} else { |
| 2663 |
if (*rhup <= rhdn) { |
| 2664 |
newq = nq - 1; |
| 2665 |
*rh = rhdn; |
| 2666 |
if (kflag < 0 && *rh > 1.) |
| 2667 |
*rh = 1.; |
| 2668 |
} else { |
| 2669 |
*rh = *rhup; |
| 2670 |
if (*rh >= 1.1) { |
| 2671 |
r = el[l] / (double)l; |
| 2672 |
nq = l; |
| 2673 |
l = nq + 1; |
| 2674 |
yp1 = yh[l]; |
| 2675 |
for (i = 1; i <= n; i++) |
| 2676 |
yp1[i] = acor[i] * r; |
| 2677 |
*orderflag = 2; |
| 2678 |
return; |
| 2679 |
} else { |
| 2680 |
ialth = 3; |
| 2681 |
return; |
| 2682 |
} |
| 2683 |
} |
| 2684 |
} |
| 2685 |
/* |
| 2686 |
If meth = 1 and h is restricted by stability, bypass 10 percent test. |
| 2687 |
*/ |
| 2688 |
if (meth == 1) { |
| 2689 |
if ((*rh * *pdh * 1.00001) < sm1[newq]) |
| 2690 |
if (kflag == 0 && *rh < 1.1) { |
| 2691 |
ialth = 3; |
| 2692 |
return; |
| 2693 |
} |
| 2694 |
} else { |
| 2695 |
if (kflag == 0 && *rh < 1.1) { |
| 2696 |
ialth = 3; |
| 2697 |
return; |
| 2698 |
} |
| 2699 |
} |
| 2700 |
if (kflag <= -2) |
| 2701 |
*rh = min(*rh, 0.2); |
| 2702 |
/* |
| 2703 |
If there is a change of order, reset nq, l, and the coefficients. |
| 2704 |
In any case h is reset according to rh and the yh array is rescaled. |
| 2705 |
Then exit or redo the step. |
| 2706 |
*/ |
| 2707 |
if (newq == nq) { |
| 2708 |
*orderflag = 1; |
| 2709 |
return; |
| 2710 |
} |
| 2711 |
nq = newq; |
| 2712 |
l = nq + 1; |
| 2713 |
*orderflag = 2; |
| 2714 |
|
| 2715 |
} /* end orderswitch */ |
| 2716 |
|
| 2717 |
void QTAIMLSODAIntegrator::resetcoeff() |
| 2718 |
/* |
| 2719 |
The el vector and related constants are reset |
| 2720 |
whenever the order nq is changed, or at the start of the problem. |
| 2721 |
*/ |
| 2722 |
{ |
| 2723 |
int i; |
| 2724 |
double* ep1; |
| 2725 |
|
| 2726 |
ep1 = elco[nq]; |
| 2727 |
for (i = 1; i <= l; i++) |
| 2728 |
el[i] = ep1[i]; |
| 2729 |
rc = rc * el[1] / el0; |
| 2730 |
el0 = el[1]; |
| 2731 |
conit = 0.5 / (double)(nq + 2); |
| 2732 |
|
| 2733 |
} /* end resetcoeff */ |
| 2734 |
|
| 2735 |
void QTAIMLSODAIntegrator::freevectors() |
| 2736 |
{ |
| 2737 |
int i; |
| 2738 |
for (i = 1; i <= m_lenyh; ++i) { |
| 2739 |
free(yh[i]); |
| 2740 |
} |
| 2741 |
free(yh); |
| 2742 |
|
| 2743 |
for (i = 1; i <= m_nyh; ++i) { |
| 2744 |
free(wm[i]); |
| 2745 |
} |
| 2746 |
free(wm); |
| 2747 |
|
| 2748 |
free(ewt); |
| 2749 |
free(savf); |
| 2750 |
free(acor); |
| 2751 |
free(ipvt); |
| 2752 |
} /* end freevectors */ |
| 2753 |
|
| 2754 |
} // namespace Avogadro::QtPlugins |