| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Header: FGRungeKutta.h |
| 4 | Author: Thomas Kreitler |
| 5 | Date started: 04/9/2010 |
| 6 | |
| 7 | ------------- Copyright (C) ------------- |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify it under |
| 10 | the terms of the GNU Lesser General Public License as published by the Free Software |
| 11 | Foundation; either version 2 of the License, or (at your option) any later |
| 12 | version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 17 | details. |
| 18 | |
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | |
| 23 | Further information about the GNU Lesser General Public License can also be found on |
| 24 | the world wide web at http://www.gnu.org. |
| 25 | |
| 26 | HISTORY |
| 27 | -------------------------------------------------------------------------------- |
| 28 | |
| 29 | |
| 30 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 31 | SENTRY |
| 32 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 33 | |
| 34 | #ifndef FGRUNGEKUTTA_H |
| 35 | #define FGRUNGEKUTTA_H |
| 36 | |
| 37 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 38 | INCLUDES |
| 39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 40 | |
| 41 | namespace JSBSim { |
| 42 | |
| 43 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 44 | CLASS DOCUMENTATION |
| 45 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | Minimalistic implementation of some Runge-Kutta methods. Runge-Kutta methods |
| 50 | are a standard for solving ordinary differential equation (ODE) initial |
| 51 | value problems. The code follows closely the description given on |
| 52 | Wikipedia, see http://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods. |
| 53 | |
| 54 | For more powerfull routines see GNU Scientific Library (GSL) |
| 55 | or GNU Plotutils 'ode'. |
| 56 | */ |
| 57 | |
| 58 | |
| 59 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 60 | DECLARATION: FGRungeKuttaProblem |
| 61 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 62 | |
| 63 | /** |
| 64 | Abstract base for the function to solve. |
| 65 | */ |
| 66 | class FGRungeKuttaProblem { |
| 67 | public: |
| 68 | virtual double pFunc(double x, double y) = 0; |
| 69 | }; |
| 70 | |
| 71 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 72 | DECLARATION: FGRungeKutta |
| 73 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 74 | /** |
| 75 | Abstract base. |
| 76 | */ |
| 77 | |
| 78 | class FGRungeKutta { |
| 79 | |
| 80 | public: |
| 81 | |
| 82 | enum eStates { eNoError=0, eMathError=1, eFaultyInit=2, eEvolve=4, eUnknown=8} ; |
| 83 | |
| 84 | int init(double x_start, double x_end, int intervals = 4); |
| 85 | |
| 86 | double evolve(double y_0, FGRungeKuttaProblem *pf); |
| 87 | |
| 88 | double getXEnd() { return x_end; } |
| 89 | double getError() { return err; } |
| 90 | |
| 91 | int getStatus() { return status; } |
| 92 | int getIterations() { return iterations; } |
| 93 | void clearStatus() { status = eNoError; } |
| 94 | void setTrace(bool t) { trace_values = t; } |
| 95 | |
| 96 | protected: |
| 97 | // avoid accidents |
| 98 | FGRungeKutta(): status(eNoError), trace_values(false), iterations(0) {}; |
| 99 | virtual ~FGRungeKutta(); |
| 100 | |
| 101 | FGRungeKuttaProblem *pfo; |
| 102 | |
| 103 | double h; |
| 104 | double h05; // h*0.5, halfwidth |
| 105 | double err; |
| 106 | |
| 107 | private: |
| 108 | |
| 109 | virtual double approximate(double x, double y) = 0; |
| 110 | |
| 111 | bool sane_val(double x); |
| 112 | |
| 113 | static const double RealLimit; |
| 114 | |
| 115 | double x0, x1; |
| 116 | double safer_x1; |
| 117 | double x_end; |
| 118 | |
| 119 | int status; |
| 120 | bool trace_values; |
| 121 | int iterations; |
| 122 | |
| 123 | }; |
| 124 | |
| 125 | |
| 126 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 127 | DECLARATION: FGRK4 |
| 128 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 129 | /** |
| 130 | Classical RK4. |
| 131 | */ |
| 132 | |
| 133 | class FGRK4 : public FGRungeKutta { |
| 134 | virtual ~FGRK4(); |
| 135 | private: |
| 136 | double approximate(double x, double y); |
| 137 | }; |
| 138 | |
| 139 | |
| 140 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 141 | DECLARATION: FGRKFehlberg |
| 142 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 143 | |
| 144 | /** |
| 145 | Runge-Kutta-Fehlberg method. |
| 146 | This is a semi adaptive implementation of rkf - the interval only |
| 147 | shrinks. As a result interval calculations remain trivial, but |
| 148 | sometimes too many calculations are performed. |
| 149 | Rationale: this code is not meant to be a universal pain-reliever |
| 150 | for ode's. Rather it provides some safety if the number of |
| 151 | intervals is set too low, or the problem function behaves a bit |
| 152 | nasty in rare conditions. |
| 153 | */ |
| 154 | |
| 155 | |
| 156 | class FGRKFehlberg : public FGRungeKutta { |
| 157 | |
| 158 | public: |
| 159 | FGRKFehlberg() : shrink_avail(4), epsilon(1e-12) { }; |
| 160 | virtual ~FGRKFehlberg(); |
| 161 | double getEpsilon() { return epsilon; } |
| 162 | int getShrinkAvail() { return shrink_avail; } |
| 163 | void setEpsilon(double e) { epsilon = e; } |
| 164 | void setShrinkAvail(int s) { shrink_avail = s; } |
| 165 | |
| 166 | private: |
| 167 | |
| 168 | double approximate(double x, double y); |
| 169 | |
| 170 | int shrink_avail; |
| 171 | double epsilon; |
| 172 | |
| 173 | static const double A2[], A3[], A4[], A5[], A6[]; |
| 174 | static const double B[], Bs[], C[]; |
| 175 | |
| 176 | }; |
| 177 | |
| 178 | |
| 179 | } // namespace JSBSim |
| 180 | |
| 181 | #endif |