Debian Codegraph @

avogadro/qtplugins/qtaim/qtaimodeintegrator.cpp

raw
1 /******************************************************************************
2
3 This source file is part of the Avogadro project.
4
5 Copyright (C) 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 Based on codes written by Herman Watts, Lawrence Shampine, and John Burkardt.
19 */
20
21 #include "qtaimodeintegrator.h"
22
23 #include "qtaimmathutilities.h"
24
25 namespace Avogadro::QtPlugins {
26
27 QTAIMODEIntegrator::QTAIMODEIntegrator(QTAIMWavefunctionEvaluator& eval,
28 const qint64 mode)
29 : m_eval(&eval), m_mode(mode), m_associatedSphere(0)
30 {
31 m_betaSpheres.clear();
32 }
33
34 QVector3D QTAIMODEIntegrator::integrate(QVector3D x0y0z0)
35 {
36 qreal x0 = x0y0z0.x();
37 qreal y0 = x0y0z0.y();
38 qreal z0 = x0y0z0.z();
39
40 const qint64 NEQN = 3;
41
42 abserr_save = -1.0;
43 flag_save = -1000;
44 h = -1.0;
45 init = -1000;
46 kflag = -1000;
47 kop = -1;
48 nfe = -1;
49 relerr_save = -1.0;
50 remin = 1.0e-12;
51
52 qreal abserr;
53 qint64 flag;
54 qint64 i_step;
55 qint64 n_step = 0;
56 qreal relerr;
57 qreal t;
58 qreal t_out;
59 qreal t_start;
60 qreal t_stop;
61 qreal y[3];
62 qreal yp[3];
63
64 switch (m_mode) {
65 case SteepestAscentPathInElectronDensity:
66 abserr = 1.e-5; // sqrt ( r8_epsilon ( ) );
67 relerr = 1.e10; // sqrt ( r8_epsilon ( ) );
68 t_start = 0.0;
69 t_stop = 10.0;
70 n_step = t_stop * 20;
71 break;
72 case CMBPMinusThreeGradientInElectronDensity:
73 abserr = sqrt(r8_epsilon());
74 relerr = sqrt(r8_epsilon());
75 t_start = 0.0;
76 t_stop = 1.0;
77 n_step = t_stop * 1000;
78 break;
79 case CMBPMinusOneGradientInElectronDensity:
80 abserr = sqrt(r8_epsilon());
81 relerr = sqrt(r8_epsilon());
82 t_start = 0.0;
83 t_stop = 1.0;
84 n_step = t_stop * 100;
85 break;
86 case CMBPPlusOneGradientInElectronDensity:
87 abserr = sqrt(r8_epsilon());
88 relerr = sqrt(r8_epsilon());
89 t_start = 0.0;
90 t_stop = 1.0;
91 n_step = t_stop * 200;
92 break;
93 case CMBPPlusThreeGradientInElectronDensity:
94 abserr = sqrt(r8_epsilon());
95 relerr = sqrt(r8_epsilon());
96 t_start = 0.0;
97 t_stop = 1.0;
98 n_step = t_stop * 200;
99 break;
100 case CMBPMinusThreeGradientInElectronDensityLaplacian:
101 abserr = sqrt(r8_epsilon());
102 relerr = sqrt(r8_epsilon());
103 t_start = 0.0;
104 t_stop = 1.0;
105 n_step = t_stop * 200;
106 break;
107 case CMBPMinusOneGradientInElectronDensityLaplacian:
108 abserr = sqrt(r8_epsilon());
109 relerr = sqrt(r8_epsilon());
110 t_start = 0.0;
111 t_stop = 1.0;
112 n_step = t_stop * 200;
113 break;
114 case CMBPPlusOneGradientInElectronDensityLaplacian:
115 abserr = sqrt(r8_epsilon());
116 relerr = sqrt(r8_epsilon());
117 t_start = 0.0;
118 t_stop = 1.0;
119 n_step = t_stop * 200;
120 break;
121 case CMBPPlusThreeGradientInElectronDensityLaplacian:
122 abserr = sqrt(r8_epsilon());
123 relerr = sqrt(r8_epsilon());
124 t_start = 0.0;
125 t_stop = 1.0;
126 n_step = t_stop * 200;
127 break;
128 default:
129 qDebug() << "Catastrophic: No ODE parameters for this property.";
130 break;
131 }
132
133 y[0] = x0;
134 y[1] = y0;
135 y[2] = z0;
136
137 m_path.clear();
138 m_path.append(QVector3D(y[0], y[1], y[2]));
139
140 flag = 1;
141
142 for (i_step = 1; i_step <= n_step; i_step++) {
143 t =
144 ((qreal)(n_step - i_step + 1) * t_start + (qreal)(i_step - 1) * t_stop) /
145 (qreal)(n_step);
146
147 t_out = ((qreal)(n_step - i_step) * t_start + (qreal)(i_step)*t_stop) /
148 (qreal)(n_step);
149
150 flag = QTAIMODEIntegrator::r8_rkf45(NEQN, y, yp, &t, t_out, &relerr, abserr,
151 flag);
152
153 m_status = flag;
154 m_path.append(QVector3D(y[0], y[1], y[2]));
155
156 if (flag == 7) {
157 flag = 2;
158 m_status = flag;
159 }
160
161 if (flag != 2) {
162 m_status = flag;
163 return QVector3D(y[0], y[1], y[2]);
164 }
165
166 if (m_betaSpheres.length() > 0) {
167 for (qint64 n = 0; n < m_betaSpheres.length(); ++n) {
168 Matrix<qreal, 3, 1> a(y[0], y[1], y[2]);
169 Matrix<qreal, 3, 1> b(m_betaSpheres.at(n).first.x(),
170 m_betaSpheres.at(n).first.y(),
171 m_betaSpheres.at(n).first.z());
172
173 qreal distance = QTAIMMathUtilities::distance(a, b);
174
175 if (distance < m_betaSpheres.at(n).second) {
176 m_status = 0;
177 m_associatedSphere = n;
178 return QVector3D(m_betaSpheres.at(n).first.x(),
179 m_betaSpheres.at(n).first.y(),
180 m_betaSpheres.at(n).first.z());
181 }
182 }
183 } // beta spheres
184
185 } // ODE step
186
187 return QVector3D(y[0], y[1], y[2]);
188 }
189
190 void QTAIMODEIntegrator::r8_f(qreal t, qreal y[], qreal yp[])
191 {
192
193 t = t; // suppress warning
194
195 Matrix<qreal, 3, 1> gradient;
196
197 Matrix<qreal, 3, 4> gH;
198 Matrix<qreal, 3, 1> g;
199 Matrix<qreal, 3, 3> H;
200
201 Matrix<qreal, 3, 1> xyz;
202 xyz << y[0], y[1], y[2];
203
204 if (m_mode == SteepestAscentPathInElectronDensity) {
205 g = m_eval->gradientOfElectronDensity(xyz);
206 } else {
207 if (m_mode == 1 || m_mode == 2 || m_mode == 3 || m_mode == 4) {
208 gH = m_eval->gradientAndHessianOfElectronDensity(xyz);
209 } else {
210 gH = m_eval->gradientAndHessianOfElectronDensityLaplacian(xyz);
211 }
212
213 g(0) = gH(0, 0);
214 g(1) = gH(1, 0);
215 g(2) = gH(2, 0);
216 H(0, 0) = gH(0, 1);
217 H(1, 0) = gH(1, 1);
218 H(2, 0) = gH(2, 1);
219 H(0, 1) = gH(0, 2);
220 H(1, 1) = gH(1, 2);
221 H(2, 1) = gH(2, 2);
222 H(0, 2) = gH(0, 3);
223 H(1, 2) = gH(1, 3);
224 H(2, 2) = gH(2, 3);
225 }
226
227 switch (m_mode) {
228 case SteepestAscentPathInElectronDensity:
229 gradient = g;
230 break;
231 case CMBPMinusThreeGradientInElectronDensity:
232 gradient = QTAIMMathUtilities::minusThreeSignatureLocatorGradient(g, H);
233 break;
234 case CMBPMinusOneGradientInElectronDensity:
235 gradient = QTAIMMathUtilities::minusOneSignatureLocatorGradient(g, H);
236 break;
237 case CMBPPlusOneGradientInElectronDensity:
238 gradient = QTAIMMathUtilities::plusOneSignatureLocatorGradient(g, H);
239 break;
240 case CMBPPlusThreeGradientInElectronDensity:
241 gradient = QTAIMMathUtilities::plusThreeSignatureLocatorGradient(g, H);
242 break;
243 case CMBPMinusThreeGradientInElectronDensityLaplacian:
244 gradient = QTAIMMathUtilities::minusThreeSignatureLocatorGradient(g, H);
245 break;
246 case CMBPMinusOneGradientInElectronDensityLaplacian:
247 gradient = QTAIMMathUtilities::minusOneSignatureLocatorGradient(g, H);
248 break;
249 case CMBPPlusOneGradientInElectronDensityLaplacian:
250 gradient = QTAIMMathUtilities::plusOneSignatureLocatorGradient(g, H);
251 break;
252 case CMBPPlusThreeGradientInElectronDensityLaplacian:
253 gradient = QTAIMMathUtilities::plusThreeSignatureLocatorGradient(g, H);
254 break;
255 default:
256 qDebug() << "Catastrophic: No ODE parameters for this property.";
257 break;
258 }
259
260 qreal normGradient = std::hypot(gradient(0), gradient(1), gradient(2));
261
262 yp[0] = gradient(0) / normGradient;
263 yp[1] = gradient(1) / normGradient;
264 yp[2] = gradient(2) / normGradient;
265 }
266
267 //****************************************************************************80
268
269 qreal QTAIMODEIntegrator::r8_abs(qreal x)
270
271 //****************************************************************************80
272 //
273 // Purpose:
274 //
275 // R8_ABS returns the absolute value of an R8.
276 //
277 // Licensing:
278 //
279 // This code is distributed under the GNU LGPL license.
280 //
281 // Modified:
282 //
283 // 02 April 2005
284 //
285 // Author:
286 //
287 // John Burkardt
288 //
289 // Parameters:
290 //
291 // Input, qreal X, the quantity whose absolute value is desired.
292 //
293 // Output, qreal R8_ABS, the absolute value of X.
294 //
295 {
296 if (0.0 <= x) {
297 return x;
298 } else {
299 return (-x);
300 }
301 }
302 //****************************************************************************80
303
304 qreal QTAIMODEIntegrator::r8_epsilon()
305
306 //****************************************************************************80
307 //
308 // Purpose:
309 //
310 // R8_EPSILON returns the R8 round off unit.
311 //
312 // Discussion:
313 //
314 // R8_EPSILON is a number R which is a power of 2 with the property that,
315 // to the precision of the computer's arithmetic,
316 // 1 < 1 + R
317 // but
318 // 1 = ( 1 + R / 2 )
319 //
320 // Licensing:
321 //
322 // This code is distributed under the GNU LGPL license.
323 //
324 // Modified:
325 //
326 // 06 May 2003
327 //
328 // Author:
329 //
330 // John Burkardt
331 //
332 // Parameters:
333 //
334 // Output, qreal R8_EPSILON, the R8 round-off unit.
335 //
336 {
337 qreal r = 1.0;
338
339 while (1.0 < (qreal)(1.0 + r)) {
340 r = r / 2.0;
341 }
342
343 return (2.0 * r);
344 }
345 //****************************************************************************80
346
347 void QTAIMODEIntegrator::r8_fehl(qint64 neqn, qreal y[], qreal t, qreal h_,
348 qreal yp[], qreal f1[], qreal f2[], qreal f3[],
349 qreal f4[], qreal f5[], qreal s[])
350
351 //****************************************************************************80
352 //
353 // Purpose:
354 //
355 // R8_FEHL takes one Fehlberg fourth-fifth order step.
356 //
357 // Discussion:
358 //
359 // This version of the routine uses qreal real arithmetic.
360 //
361 // This routine integrates a system of NEQN first order ordinary differential
362 // equations of the form
363 // dY(i)/dT = F(T,Y(1:NEQN))
364 // where the initial values Y and the initial derivatives
365 // YP are specified at the starting point T.
366 //
367 // The routine advances the solution over the fixed step H and returns
368 // the fifth order (sixth order accurate locally) solution
369 // approximation at T+H in array S.
370 //
371 // The formulas have been grouped to control loss of significance.
372 // The routine should be called with an H not smaller than 13 units of
373 // roundoff in T so that the various independent arguments can be
374 // distinguished.
375 //
376 // Licensing:
377 //
378 // This code is distributed under the GNU LGPL license.
379 //
380 // Modified:
381 //
382 // 27 March 2004
383 //
384 // Author:
385 //
386 // Original FORTRAN77 version by Herman Watts, Lawrence Shampine.
387 // C++ version by John Burkardt.
388 //
389 // Reference:
390 //
391 // Erwin Fehlberg,
392 // Low-order Classical Runge-Kutta Formulas with Stepsize Control,
393 // NASA Technical Report R-315, 1969.
394 //
395 // Lawrence Shampine, Herman Watts, S Davenport,
396 // Solving Non-stiff Ordinary Differential Equations - The State of the Art,
397 // SIAM Review,
398 // Volume 18, pages 376-411, 1976.
399 //
400 // Parameters:
401 //
402 // Input, external F, a user-supplied subroutine to evaluate the
403 // derivatives Y'(T), of the form:
404 //
405 // void f ( qreal t, qreal y[], qreal yp[] )
406 //
407 // Input, qint64 NEQN, the number of equations to be integrated.
408 //
409 // Input, qreal Y[NEQN], the current value of the dependent variable.
410 //
411 // Input, qreal T, the current value of the independent variable.
412 //
413 // Input, qreal H, the step size to take.
414 //
415 // Input, qreal YP[NEQN], the current value of the derivative of the
416 // dependent variable.
417 //
418 // Output, qreal F1[NEQN], F2[NEQN], F3[NEQN], F4[NEQN], F5[NEQN], derivative
419 // values needed for the computation.
420 //
421 // Output, qreal S[NEQN], the estimate of the solution at T+H.
422 //
423 {
424 qreal ch = h_ / 4.0;
425
426 for (qint64 i = 0; i < neqn; i++) {
427 f5[i] = y[i] + ch * yp[i];
428 }
429
430 QTAIMODEIntegrator::r8_f(t + ch, f5, f1);
431
432 ch = 3.0 * h_ / 32.0;
433
434 for (qint64 i = 0; i < neqn; i++) {
435 f5[i] = y[i] + ch * (yp[i] + 3.0 * f1[i]);
436 }
437
438 QTAIMODEIntegrator::r8_f(t + 3.0 * h_ / 8.0, f5, f2);
439
440 ch = h_ / 2197.0;
441
442 for (qint64 i = 0; i < neqn; i++) {
443 f5[i] = y[i] + ch * (1932.0 * yp[i] + (7296.0 * f2[i] - 7200.0 * f1[i]));
444 }
445
446 QTAIMODEIntegrator::r8_f(t + 12.0 * h_ / 13.0, f5, f3);
447
448 ch = h_ / 4104.0;
449
450 for (qint64 i = 0; i < neqn; i++) {
451 f5[i] = y[i] + ch * ((8341.0 * yp[i] - 845.0 * f3[i]) +
452 (29440.0 * f2[i] - 32832.0 * f1[i]));
453 }
454
455 QTAIMODEIntegrator::r8_f(t + h_, f5, f4);
456
457 ch = h_ / 20520.0;
458
459 for (qint64 i = 0; i < neqn; i++) {
460 f1[i] = y[i] + ch * ((-6080.0 * yp[i] + (9295.0 * f3[i] - 5643.0 * f4[i])) +
461 (41040.0 * f1[i] - 28352.0 * f2[i]));
462 }
463
464 QTAIMODEIntegrator::r8_f(t + h_ / 2.0, f1, f5);
465 //
466 // Ready to compute the approximate solution at T+H.
467 //
468 ch = h_ / 7618050.0;
469
470 for (qint64 i = 0; i < neqn; i++) {
471 s[i] = y[i] +
472 ch * ((902880.0 * yp[i] + (3855735.0 * f3[i] - 1371249.0 * f4[i])) +
473 (3953664.0 * f2[i] + 277020.0 * f5[i]));
474 }
475 }
476 //****************************************************************************80
477
478 qreal QTAIMODEIntegrator::r8_max(qreal x, qreal y)
479
480 //****************************************************************************80
481 //
482 // Purpose:
483 //
484 // R8_MAX returns the maximum of two R8's.
485 //
486 // Licensing:
487 //
488 // This code is distributed under the GNU LGPL license.
489 //
490 // Modified:
491 //
492 // 10 January 2002
493 //
494 // Author:
495 //
496 // John Burkardt
497 //
498 // Parameters:
499 //
500 // Input, qreal X, Y, the quantities to compare.
501 //
502 // Output, qreal R8_MAX, the maximum of X and Y.
503 //
504 {
505 if (y < x) {
506 return x;
507 } else {
508 return y;
509 }
510 }
511 //****************************************************************************80
512
513 qreal QTAIMODEIntegrator::r8_min(qreal x, qreal y)
514
515 //****************************************************************************80
516 //
517 // Purpose:
518 //
519 // R8_MIN returns the minimum of two R8's.
520 //
521 // Licensing:
522 //
523 // This code is distributed under the GNU LGPL license.
524 //
525 // Modified:
526 //
527 // 09 May 2003
528 //
529 // Author:
530 //
531 // John Burkardt
532 //
533 // Parameters:
534 //
535 // Input, qreal X, Y, the quantities to compare.
536 //
537 // Output, qreal R8_MIN, the minimum of X and Y.
538 //
539 {
540 if (y < x) {
541 return y;
542 } else {
543 return x;
544 }
545 }
546 //****************************************************************************80
547
548 qint64 QTAIMODEIntegrator::r8_rkf45(qint64 neqn, qreal y[], qreal yp[],
549 qreal* t, qreal tout, qreal* relerr,
550 qreal abserr, qint64 flag)
551
552 //****************************************************************************80
553 //
554 // Purpose:
555 //
556 // R8_RKF45 carries out the Runge-Kutta-Fehlberg method.
557 //
558 // Discussion:
559 //
560 // This version of the routine uses qreal real arithmetic.
561 //
562 // This routine is primarily designed to solve non-stiff and mildly stiff
563 // differential equations when derivative evaluations are inexpensive.
564 // It should generally not be used when the user is demanding
565 // high accuracy.
566 //
567 // This routine integrates a system of NEQN first-order ordinary differential
568 // equations of the form:
569 //
570 // dY(i)/dT = F(T,Y(1),Y(2),...,Y(NEQN))
571 //
572 // where the Y(1:NEQN) are given at T.
573 //
574 // Typically the subroutine is used to integrate from T to TOUT but it
575 // can be used as a one-step integrator to advance the solution a
576 // single step in the direction of TOUT. On return, the parameters in
577 // the call list are set for continuing the integration. The user has
578 // only to call again (and perhaps define a new value for TOUT).
579 //
580 // Before the first call, the user must
581 //
582 // * supply the subroutine F(T,Y,YP) to evaluate the right hand side;
583 // and declare F in an EXTERNAL statement;
584 //
585 // * initialize the parameters:
586 // NEQN, Y(1:NEQN), T, TOUT, RELERR, ABSERR, FLAG.
587 // In particular, T should initially be the starting point for integration,
588 // Y should be the value of the initial conditions, and FLAG should
589 // normally be +1.
590 //
591 // Normally, the user only sets the value of FLAG before the first call, and
592 // thereafter, the program manages the value. On the first call, FLAG should
593 // normally be +1 (or -1 for single step mode.) On normal return, FLAG will
594 // have been reset by the program to the value of 2 (or -2 in single
595 // step mode), and the user can continue to call the routine with that
596 // value of FLAG.
597 //
598 // (When the input magnitude of FLAG is 1, this indicates to the program
599 // that it is necessary to do some initialization work. An input magnitude
600 // of 2 lets the program know that initialization can be skipped,
601 // and that useful information was computed earlier.)
602 //
603 // The routine returns with all the information needed to continue
604 // the integration. If the integration reached TOUT, the user need only
605 // define a new TOUT and call again. In the one-step integrator
606 // mode, returning with FLAG = -2, the user must keep in mind that
607 // each step taken is in the direction of the current TOUT. Upon
608 // reaching TOUT, indicated by the output value of FLAG switching to 2,
609 // the user must define a new TOUT and reset FLAG to -2 to continue
610 // in the one-step integrator mode.
611 //
612 // In some cases, an error or difficulty occurs during a call. In that case,
613 // the output value of FLAG is used to indicate that there is a problem
614 // that the user must address. These values include:
615 //
616 // * 3, integration was not completed because the input value of RELERR, the
617 // relative error tolerance, was too small. RELERR has been increased
618 // appropriately for continuing. If the user accepts the output value of
619 // RELERR, then simply reset FLAG to 2 and continue.
620 //
621 // * 4, integration was not completed because more than MAXNFE derivative
622 // evaluations were needed. This is approximately (MAXNFE/6) steps.
623 // The user may continue by simply calling again. The function counter
624 // will be reset to 0, and another MAXNFE function evaluations are allowed.
625 //
626 // * 5, integration was not completed because the solution vanished,
627 // making a pure relative error test impossible. The user must use
628 // a non-zero ABSERR to continue. Using the one-step integration mode
629 // for one step is a good way to proceed.
630 //
631 // * 6, integration was not completed because the requested accuracy
632 // could not be achieved, even using the smallest allowable stepsize.
633 // The user must increase the error tolerances ABSERR or RELERR before
634 // continuing. It is also necessary to reset FLAG to 2 (or -2 when
635 // the one-step integration mode is being used). The occurrence of
636 // FLAG = 6 indicates a trouble spot. The solution is changing
637 // rapidly, or a singularity may be present. It often is inadvisable
638 // to continue.
639 //
640 // * 7, it is likely that this routine is inefficient for solving
641 // this problem. Too much output is restricting the natural stepsize
642 // choice. The user should use the one-step integration mode with
643 // the stepsize determined by the code. If the user insists upon
644 // continuing the integration, reset FLAG to 2 before calling
645 // again. Otherwise, execution will be terminated.
646 //
647 // * 8, invalid input parameters, indicates one of the following:
648 // NEQN <= 0;
649 // T = TOUT and |FLAG| /= 1;
650 // RELERR < 0 or ABSERR < 0;
651 // FLAG == 0 or FLAG < -2 or 8 < FLAG.
652 //
653 // Licensing:
654 //
655 // This code is distributed under the GNU LGPL license.
656 //
657 // Modified:
658 //
659 // 27 March 2004
660 //
661 // Author:
662 //
663 // Original FORTRAN77 version by Herman Watts, Lawrence Shampine.
664 // C++ version by John Burkardt.
665 //
666 // Reference:
667 //
668 // Erwin Fehlberg,
669 // Low-order Classical Runge-Kutta Formulas with Stepsize Control,
670 // NASA Technical Report R-315, 1969.
671 //
672 // Lawrence Shampine, Herman Watts, S Davenport,
673 // Solving Non-stiff Ordinary Differential Equations - The State of the Art,
674 // SIAM Review,
675 // Volume 18, pages 376-411, 1976.
676 //
677 // Parameters:
678 //
679 // Input, external F, a user-supplied subroutine to evaluate the
680 // derivatives Y'(T), of the form:
681 //
682 // void f ( qreal t, qreal y[], qreal yp[] )
683 //
684 // Input, qint64 NEQN, the number of equations to be integrated.
685 //
686 // Input/output, qreal Y[NEQN], the current solution vector at T.
687 //
688 // Input/output, qreal YP[NEQN], the derivative of the current solution
689 // vector at T. The user should not set or alter this information!
690 //
691 // Input/output, qreal *T, the current value of the independent variable.
692 //
693 // Input, qreal TOUT, the output point at which solution is desired.
694 // TOUT = T is allowed on the first call only, in which case the routine
695 // returns with FLAG = 2 if continuation is possible.
696 //
697 // Input, qreal *RELERR, ABSERR, the relative and absolute error tolerances
698 // for the local error test. At each step the code requires:
699 // abs ( local error ) <= RELERR * abs ( Y ) + ABSERR
700 // for each component of the local error and the solution vector Y.
701 // RELERR cannot be "too small". If the routine believes RELERR has been
702 // set too small, it will reset RELERR to an acceptable value and return
703 // immediately for user action.
704 //
705 // Input, qint64 FLAG, indicator for status of integration. On the first
706 // call,
707 // set FLAG to +1 for normal use, or to -1 for single step mode. On
708 // subsequent continuation steps, FLAG should be +2, or -2 for single
709 // step mode.
710 //
711 // Output, qint64 RKF45_D, indicator for status of integration. A value of 2
712 // or -2 indicates normal progress, while any other value indicates a
713 // problem that should be addressed.
714 //
715 {
716 // ECB: originally 3000
717 #define MAXNFE 1000
718
719 qreal ae;
720 qreal dt;
721 qreal ee;
722 qreal eeoet;
723 qreal eps;
724 qreal esttol;
725 qreal et;
726 qreal* f1;
727 qreal* f2;
728 qreal* f3;
729 qreal* f4;
730 qreal* f5;
731 bool hfaild;
732 qreal hmin;
733 qint64 i;
734 qint64 k;
735 qint64 mflag;
736 bool output;
737 qreal relerr_min;
738 qreal s;
739 qreal scale;
740 qreal tol;
741 qreal toln;
742 qreal ypk;
743 //
744 // Check the input parameters.
745 //
746 eps = r8_epsilon();
747
748 if (neqn < 1) {
749 return 8;
750 }
751
752 if ((*relerr) < 0.0) {
753 return 8;
754 }
755
756 if (abserr < 0.0) {
757 return 8;
758 }
759
760 if (flag == 0 || 8 < flag || flag < -2) {
761 return 8;
762 }
763
764 mflag = (qint64)abs((long)flag);
765 //
766 // Is this a continuation call?
767 //
768 if (mflag != 1) {
769 if (*t == tout && kflag != 3) {
770 return 8;
771 }
772 //
773 // FLAG = -2 or +2:
774 //
775 if (mflag == 2) {
776 if (kflag == 3) {
777 flag = flag_save;
778 mflag = (qint64)abs((long)flag);
779 } else if (init == 0) {
780 flag = flag_save;
781 } else if (kflag == 4) {
782 nfe = 0;
783 } else if (kflag == 5 && abserr == 0.0) {
784 // error
785 } else if (kflag == 6 && (*relerr) <= relerr_save &&
786 abserr <= abserr_save) {
787 // error
788 }
789 }
790 //
791 // FLAG = 3, 4, 5, 6, 7 or 8.
792 //
793 else {
794 if (flag == 3) {
795 flag = flag_save;
796 if (kflag == 3) {
797 mflag = (qint64)abs((long)flag);
798 }
799 } else if (flag == 4) {
800 nfe = 0;
801 flag = flag_save;
802 if (kflag == 3) {
803 mflag = (qint64)abs((long)flag);
804 }
805 } else if (flag == 5 && 0.0 < abserr) {
806 flag = flag_save;
807 if (kflag == 3) {
808 mflag = (qint64)abs((long)flag);
809 }
810 }
811 //
812 // Integration cannot be continued because the user did not respond to
813 // the instructions pertaining to FLAG = 5, 6, 7 or 8.
814 //
815 else {
816 // error
817 }
818 }
819 }
820 //
821 // Save the input value of FLAG.
822 // Set the continuation flag KFLAG for subsequent input checking.
823 //
824 flag_save = flag;
825 kflag = 0;
826 //
827 // Save RELERR and ABSERR for checking input on subsequent calls.
828 //
829 relerr_save = (*relerr);
830 abserr_save = abserr;
831 //
832 // Restrict the relative error tolerance to be at least
833 //
834 // 2*EPS+REMIN
835 //
836 // to avoid limiting precision difficulties arising from impossible
837 // accuracy requests.
838 //
839 relerr_min = 2.0 * r8_epsilon() + remin;
840 //
841 // Is the relative error tolerance too small?
842 //
843 if ((*relerr) < relerr_min) {
844 (*relerr) = relerr_min;
845 kflag = 3;
846 return 3;
847 }
848
849 dt = tout - *t;
850 //
851 // Initialization:
852 //
853 // Set the initialization completion indicator, INIT;
854 // set the indicator for too many output points, KOP;
855 // evaluate the initial derivatives
856 // set the counter for function evaluations, NFE;
857 // estimate the starting stepsize.
858 //
859 f1 = new qreal[neqn];
860 f2 = new qreal[neqn];
861 f3 = new qreal[neqn];
862 f4 = new qreal[neqn];
863 f5 = new qreal[neqn];
864
865 if (mflag == 1) {
866 init = 0;
867 kop = 0;
868 QTAIMODEIntegrator::r8_f(*t, y, yp);
869 nfe = 1;
870
871 if (*t == tout) {
872 return 2;
873 }
874 }
875
876 if (init == 0) {
877 init = 1;
878 h = r8_abs(dt);
879 toln = 0.0;
880
881 for (k = 0; k < neqn; k++) {
882 tol = (*relerr) * r8_abs(y[k]) + abserr;
883 if (0.0 < tol) {
884 toln = tol;
885 ypk = r8_abs(yp[k]);
886 if (tol < ypk * pow(h, 5)) {
887 h = pow((tol / ypk), 0.2);
888 }
889 }
890 }
891
892 if (toln <= 0.0) {
893 h = 0.0;
894 }
895
896 h = r8_max(h, 26.0 * eps * r8_max(r8_abs(*t), r8_abs(dt)));
897
898 if (flag < 0) {
899 flag_save = -2;
900 } else {
901 flag_save = 2;
902 }
903 }
904 //
905 // Set stepsize for integration in the direction from T to TOUT.
906 //
907 h = r8_sign(dt) * r8_abs(h);
908 //
909 // Test to see if too may output points are being requested.
910 //
911 if (2.0 * r8_abs(dt) <= r8_abs(h)) {
912 kop = kop + 1;
913 }
914 //
915 // Unnecessary frequency of output.
916 //
917 if (kop == 100) {
918 kop = 0;
919 delete[] f1;
920 delete[] f2;
921 delete[] f3;
922 delete[] f4;
923 delete[] f5;
924 return 7;
925 }
926 //
927 // If we are too close to the output point, then simply extrapolate and
928 // return.
929 //
930 if (r8_abs(dt) <= 26.0 * eps * r8_abs(*t)) {
931 *t = tout;
932 for (i = 0; i < neqn; i++) {
933 y[i] = y[i] + dt * yp[i];
934 }
935 QTAIMODEIntegrator::r8_f(*t, y, yp);
936 nfe = nfe + 1;
937
938 delete[] f1;
939 delete[] f2;
940 delete[] f3;
941 delete[] f4;
942 delete[] f5;
943 return 2;
944 }
945 //
946 // Initialize the output point indicator.
947 //
948 output = false;
949 //
950 // To avoid premature underflow in the error tolerance function,
951 // scale the error tolerances.
952 //
953 scale = 2.0 / (*relerr);
954 ae = scale * abserr;
955 //
956 // Step by step integration.
957 //
958 for (;;) {
959 hfaild = false;
960 //
961 // Set the smallest allowable stepsize.
962 //
963 hmin = 26.0 * eps * r8_abs(*t);
964 //
965 // Adjust the stepsize if necessary to hit the output point.
966 //
967 // Look ahead two steps to avoid drastic changes in the stepsize and
968 // thus lessen the impact of output points on the code.
969 //
970 dt = tout - *t;
971
972 if (2.0 * r8_abs(h) <= r8_abs(dt)) {
973 } else
974 //
975 // Will the next successful step complete the integration to the output
976 // point?
977 //
978 {
979 if (r8_abs(dt) <= r8_abs(h)) {
980 output = true;
981 h = dt;
982 } else {
983 h = 0.5 * dt;
984 }
985 }
986 //
987 // Here begins the core integrator for taking a single step.
988 //
989 // The tolerances have been scaled to avoid premature underflow in
990 // computing the error tolerance function ET.
991 // To avoid problems with zero crossings, relative error is measured
992 // using the average of the magnitudes of the solution at the
993 // beginning and end of a step.
994 // The error estimate formula has been grouped to control loss of
995 // significance.
996 //
997 // To distinguish the various arguments, H is not permitted
998 // to become smaller than 26 units of roundoff in T.
999 // Practical limits on the change in the stepsize are enforced to
1000 // smooth the stepsize selection process and to avoid excessive
1001 // chattering on problems having discontinuities.
1002 // To prevent unnecessary failures, the code uses 9/10 the stepsize
1003 // it estimates will succeed.
1004 //
1005 // After a step failure, the stepsize is not allowed to increase for
1006 // the next attempted step. This makes the code more efficient on
1007 // problems having discontinuities and more effective in general
1008 // since local extrapolation is being used and extra caution seems
1009 // warranted.
1010 //
1011 // Test the number of derivative function evaluations.
1012 // If okay, try to advance the integration from T to T+H.
1013 //
1014 for (;;) {
1015 //
1016 // Have we done too much work?
1017 //
1018 if (MAXNFE < nfe) {
1019 kflag = 4;
1020 delete[] f1;
1021 delete[] f2;
1022 delete[] f3;
1023 delete[] f4;
1024 delete[] f5;
1025 return 4;
1026 }
1027 //
1028 // Advance an approximate solution over one step of length H.
1029 //
1030 r8_fehl(neqn, y, *t, h, yp, f1, f2, f3, f4, f5, f1);
1031 nfe = nfe + 5;
1032 //
1033 // Compute and test allowable tolerances versus local error estimates
1034 // and remove scaling of tolerances. The relative error is
1035 // measured with respect to the average of the magnitudes of the
1036 // solution at the beginning and end of the step.
1037 //
1038 eeoet = 0.0;
1039
1040 for (k = 0; k < neqn; k++) {
1041 et = r8_abs(y[k]) + r8_abs(f1[k]) + ae;
1042
1043 if (et <= 0.0) {
1044 delete[] f1;
1045 delete[] f2;
1046 delete[] f3;
1047 delete[] f4;
1048 delete[] f5;
1049 return 5;
1050 }
1051
1052 ee = r8_abs((-2090.0 * yp[k] + (21970.0 * f3[k] - 15048.0 * f4[k])) +
1053 (22528.0 * f2[k] - 27360.0 * f5[k]));
1054
1055 eeoet = r8_max(eeoet, ee / et);
1056 }
1057
1058 esttol = r8_abs(h) * eeoet * scale / 752400.0;
1059
1060 if (esttol <= 1.0) {
1061 break;
1062 }
1063 //
1064 // Unsuccessful step. Reduce the stepsize, try again.
1065 // The decrease is limited to a factor of 1/10.
1066 //
1067 hfaild = true;
1068 output = false;
1069
1070 if (esttol < 59049.0) {
1071 s = 0.9 / pow(esttol, 0.2);
1072 } else {
1073 s = 0.1;
1074 }
1075
1076 h = s * h;
1077
1078 if (r8_abs(h) < hmin) {
1079 kflag = 6;
1080 delete[] f1;
1081 delete[] f2;
1082 delete[] f3;
1083 delete[] f4;
1084 delete[] f5;
1085 return 6;
1086 }
1087 }
1088 //
1089 // We exited the loop because we took a successful step.
1090 // Store the solution for T+H, and evaluate the derivative there.
1091 //
1092 *t = *t + h;
1093 for (i = 0; i < neqn; i++) {
1094 y[i] = f1[i];
1095 }
1096 QTAIMODEIntegrator::r8_f(*t, y, yp);
1097 nfe = nfe + 1;
1098 //
1099 // Choose the next stepsize. The increase is limited to a factor of 5.
1100 // If the step failed, the next stepsize is not allowed to increase.
1101 //
1102 if (0.0001889568 < esttol) {
1103 s = 0.9 / pow(esttol, 0.2);
1104 } else {
1105 s = 5.0;
1106 }
1107
1108 if (hfaild) {
1109 s = r8_min(s, 1.0);
1110 }
1111
1112 h = r8_sign(h) * r8_max(s * r8_abs(h), hmin);
1113 //
1114 // End of core integrator
1115 //
1116 // Should we take another step?
1117 //
1118 if (output) {
1119 *t = tout;
1120 delete[] f1;
1121 delete[] f2;
1122 delete[] f3;
1123 delete[] f4;
1124 delete[] f5;
1125 return 2;
1126 }
1127
1128 if (flag <= 0) {
1129 delete[] f1;
1130 delete[] f2;
1131 delete[] f3;
1132 delete[] f4;
1133 delete[] f5;
1134 return (-2);
1135 }
1136 }
1137 #undef MAXNFE
1138 }
1139 //****************************************************************************80
1140
1141 qreal QTAIMODEIntegrator::r8_sign(qreal x)
1142
1143 //****************************************************************************80
1144 //
1145 // Purpose:
1146 //
1147 // R8_SIGN returns the sign of an R8.
1148 //
1149 // Licensing:
1150 //
1151 // This code is distributed under the GNU LGPL license.
1152 //
1153 // Modified:
1154 //
1155 // 27 March 2004
1156 //
1157 // Author:
1158 //
1159 // John Burkardt
1160 //
1161 // Parameters:
1162 //
1163 // Input, qreal X, the number whose sign is desired.
1164 //
1165 // Output, qreal R8_SIGN, the sign of X.
1166 //
1167 {
1168 if (x < 0.0) {
1169 return (-1.0);
1170 } else {
1171 return (+1.0);
1172 }
1173 }
1174 //****************************************************************************80
1175
1176 } // namespace Avogadro::QtPlugins