/usr/include/crystalspace-2.0/csgeom/odesolver.h is in libcrystalspace-dev 2.0+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | /*
Copyright (C) 2006 by Marten Svanfeldt
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/**\file
* ODE solvers
*/
#ifndef __CS_CSGEOM_ODESOLVER_H__
#define __CS_CSGEOM_ODESOLVER_H__
#include "csgeom/vector3.h"
namespace CS
{
namespace Math
{
/**
* Embedded Runge-Kutta 4/5th order ODE solver for non-stiff ODEs.
*
* Solve the system
*
* y' = f(t, y)
*
* where y (and y') are scalar or vector.
*
* For reference, see:
* "Ordinary and partial differential equation routines in C, C++, Fortran,
* Java, Maple and MATLAB" by H.J. Lee & W.E. Schiesser
*/
class Ode45
{
public:
/**
* Step system a single step with step length h.
*
* \param f Function in y' = f(t, y)
* \param h Step length
* \param t0 Initial time
* \param y0 Initial y value
* \param yout Resulting y value
* \param size Number of elements in y0 and yout
* \return Error estimate
*/
template<typename FuncType, typename ArgType>
static ArgType Step (FuncType& f, ArgType h, ArgType t0, ArgType* y0,
ArgType* yout, size_t size)
{
// We need k1-k6
CS_ALLOC_STACK_ARRAY(ArgType, k1, size);
CS_ALLOC_STACK_ARRAY(ArgType, k2, size);
CS_ALLOC_STACK_ARRAY(ArgType, k3, size);
CS_ALLOC_STACK_ARRAY(ArgType, k4, size);
CS_ALLOC_STACK_ARRAY(ArgType, k5, size);
CS_ALLOC_STACK_ARRAY(ArgType, k6, size);
CS_ALLOC_STACK_ARRAY(ArgType, tmp, size);
// k1
f (t0, y0, k1, size);
// prepare for k2
for (size_t i = 0; i < size; ++i)
{
k1[i] *= h;
tmp[i] = y0[i] + 0.25*k1[i];
}
// k2
f (t0 + 0.25f*h, tmp, k2, size);
// prepare for k3
for (size_t i = 0; i < size; ++i)
{
k2[i] *= h;
tmp[i] = y0[i] + (3.0/32)*k1[i]
+ (9.0/32)*k2[i];
}
// k3
f (t0 + (3.0f/8)*h, tmp, k3, size);
// prepare for k4
for (size_t i = 0; i < size; ++i)
{
k3[i] *= h;
tmp[i] = y0[i] + (1932.0/2197)*k1[i]
- (7200.0/2197)*k2[i]
+ (7296.0/2197)*k3[i];
}
// k4
f (t0 + (12.0f/13)*h, tmp, k4, size);
// prepare for k5
for (size_t i = 0; i < size; ++i)
{
k4[i] *= h;
tmp[i] = y0[i] + (439.0/216)*k1[i]
- (8.0)*k2[i]
+ (3680.0/513)*k3[i]
- (845.0/4104)*k4[i];
}
// k5
f (t0 + h, tmp, k5, size);
// prepare for k6
for (size_t i = 0; i < size; ++i)
{
k5[i] *= h;
tmp[i] = y0[i] - (8.0/27)*k1[i]
+ (2.0)*k2[i]
- (3544.0/2565)*k3[i]
+ (1859.0/4104)*k4[i]
- (11.0/40)*k5[i];
}
// k6
f (t0 + 0.5f*h, tmp, k6, size);
ArgType errMag = 0;
// Finally calculate 4th and 5th order result, error term and final result
for (size_t i = 0; i < size; ++i)
{
k6[i] *= h;
ArgType y4 = y0[i] + (25.0/216)*k1[i]
+ (1408.0/2565)*k3[i]
+ (2197.0/4104)*k4[i]
- (1.0/5)*k5[i];
ArgType y5 = y0[i] + (16.0/315)*k1[i]
+ (6656.0/12825)*k3[i]
+ (28561.0/56430)*k4[i]
- (9.0f/50)*k5[i]
+ (2.0/55)*k6[i];
ArgType yErr = y4 - y5;
yout[i] = y5 + yErr;
errMag += yErr*yErr;
}
return sqrtf (errMag);
}
/**
* Step system a single step with step length h.
*
* \param f Function in y' = f(t, y)
* \param h Step length
* \param t0 Initial time
* \param y0 Initial y value
* \param yout Resulting y value
* \return Error estimate
*/
template<typename FuncType, typename ArgType>
static float Step (FuncType& f, ArgType h, ArgType t0, csVector3 y0,
csVector3& yout)
{
// We need k1-k6
csVector3 k1, k2, k3, k4, k5, k6;
// k1
k1 = h * f (t0, y0);
// k2
k2 = h * f (t0 + 0.25f*h, y0 + 0.25f*k1);
// k3
k3 = h * f (t0 + (3.0f/8)*h, y0 + (3.0f/32)*k1
+ (9.0f/32)*k2);
// k4
k4 = h * f (t0 + (12.0f/13)*h, y0 + (1932.0f/2197)*k1
- (7200.0f/2197)*k2
+ (7296.0f/2197)*k3);
// k5
k5 = h * f (t0 + h, y0 + (439.0f/216)*k1
- 8.0f*k2
+ (3680.0f/513)*k3
- (845.0f/4104)*k4);
// k6
k6 = h * f (t0 + 0.5f*h, y0 - (8.0f/27)*k1
+ (2.0f)*k2
- (3544.0f/2565)*k3
+ (1859.0f/4104)*k4
- (11.0f/40)*k5);
// Finally calculate 4th and 5th order result, error term and final result
csVector3 y4 = y0 + (25.0f/216)*k1
+ (1408.0f/2565)*k3
+ (2197.0f/4104)*k4
- (1.0f/5)*k5;
csVector3 y5 = y0 + (16.0f/315)*k1
+ (6656.0f/12825)*k3
+ (28561.0f/56430)*k4
- (9.0f/50)*k5
+ (2.0f/55)*k6;
csVector3 yErr = y4 - y5;
yout = y5 + yErr;
return yErr.Norm ();
}
/**
* Step system a single step with step length h.
*
* \param f Function in y' = f(t, y)
* \param h Step length
* \param t0 Initial time
* \param y0 Initial y value
* \param yout Resulting y value
* \return Error estimate
*/
template<typename FuncType, typename ArgType>
static ArgType Step (FuncType& f, ArgType h, ArgType t0, ArgType y0,
ArgType& yout)
{
// We need k1-k6
ArgType k1, k2, k3, k4, k5, k6;
// k1
k1 = h * f (t0, y0);
// k2
k2 = h * f (t0 + 0.25f*h, y0 + 0.25f*k1);
// k3
k3 = h * f (t0 + (3.0f/8)*h, y0 + (3.0f/32)*k1
+ (9.0f/32)*k2);
// k4
k4 = h * f (t0 + (12.0f/13)*h, y0 + (1932.0f/2197)*k1
- (7200.0f/2197)*k2
+ (7296.0f/2197)*k3);
// k5
k5 = h * f (t0 + h, y0 + (439.0f/216)*k1
- 8.0f*k2
+ (3680.0f/513)*k3
- (845.0f/4104)*k4);
// k6
k6 = h * f (t0 + 0.5f*h, y0 - (8.0f/27)*k1
+ (2.0f)*k2
- (3544.0f/2565)*k3
+ (1859.0f/4104)*k4
- (11.0f/40)*k5);
// Finally calculate 4th and 5th order result, error term and final result
ArgType y4 = y0 + (25.0f/216)*k1
+ (1408.0f/2565)*k3
+ (2197.0f/4104)*k4
- (1.0f/5)*k5;
ArgType y5 = y0 + (16.0f/315)*k1
+ (6656.0f/12825)*k3
+ (28561.0f/56430)*k4
- (9.0f/50)*k5
+ (2.0f/55)*k6;
ArgType yErr = y4 - y5;
yout = y5 + yErr;
return yErr;
}
};
}
}
#endif
|