File: vtkRungeKutta45.cxx

package info (click to toggle)
vtk 5.8.0-13
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 130,524 kB
  • sloc: cpp: 1,129,256; ansic: 708,203; tcl: 48,526; python: 20,875; xml: 6,779; yacc: 4,208; perl: 3,121; java: 2,788; lex: 931; sh: 660; asm: 471; makefile: 299
file content (325 lines) | stat: -rw-r--r-- 8,345 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkRungeKutta45.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkRungeKutta45.h"

#include "vtkObjectFactory.h"
#include "vtkFunctionSet.h"

vtkStandardNewMacro(vtkRungeKutta45);

//----------------------------------------------------------------------------
// Cash-Karp parameters
double vtkRungeKutta45::A[5] = { 1.0L/5.0, 3.0L/10.0, 3.0L/5.0, 1.0,
                 7.0L/8.0 };
double vtkRungeKutta45::B[5][5] = { { 1.0L/5.0, 0, 0, 0, 0 },
                    { 3.0L/40.0, 9.0L/40.0,  0, 0, 0 },
                    { 3.0L/10.0, -9.0L/10.0, 6.0L/5.0, 0, 0 },
                    { -11.0L/54.0,
                      5.0L/2.0,
                      -70.0L/27.0,
                      35.0L/27.0,
                      0 },
                    { 1631.0L/55296.0,
                      175.0/512.0,
                      575.0/13824.0,
                      44275.0/110592.0,
                      253.0L/4096.0 } };
double vtkRungeKutta45::C[6] = {37.0L/378.0,
                0,
                250.0L/621.0,
                125.0L/594.0,
                0,
                512.0L/1771.0 };
double vtkRungeKutta45::DC[6] = { 37.0L/378.0 - 2825.0L/27648.0,
                  0,
                  250.0L/621.0 - 18575.0L/48384.0,
                  125.0L/594.0 - 13525.0L/55296,
                  -277.0L/14336.0,
                  512.0L/1771.0 - 1.0L/4.0};

//----------------------------------------------------------------------------
vtkRungeKutta45::vtkRungeKutta45()
{
  for(int i=0; i<6; i++)
    {
    this->NextDerivs[i] = 0;
    }
  this->Adaptive = 1;
}

//----------------------------------------------------------------------------
vtkRungeKutta45::~vtkRungeKutta45()
{
  for(int i=0; i<6; i++)
    {
    delete[] this->NextDerivs[i];
    this->NextDerivs[i] = 0;
    }
}

//----------------------------------------------------------------------------
void vtkRungeKutta45::Initialize()
{
  this->vtkInitialValueProblemSolver::Initialize();
  if (!this->Initialized)
    {
    return;
    }
  // Allocate memory for temporary derivatives array
  for(int i=0; i<6; i++)
    {
    this->NextDerivs[i] =
      new double[this->FunctionSet->GetNumberOfFunctions()];
    }
}

//----------------------------------------------------------------------------
int vtkRungeKutta45::ComputeNextStep(double* xprev, double* dxprev,
                                     double* xnext, double t, double& delT,
                                     double& delTActual,
                                     double minStep, double maxStep,
                                     double maxError, double& estErr )
{
  estErr = VTK_DOUBLE_MAX;

  // Step size should always be positive. We'll check anyway.
  if (minStep < 0)
    {
    minStep = -minStep;
    }
  if (maxStep < 0)
    {
    maxStep = -maxStep;
    }

  delTActual = delT;

  // No step size control if minStep == maxStep == delT
  double absDT = fabs(delT);
  if ( ((minStep == absDT) && (maxStep == absDT)) ||
       (maxError <= 0.0) )
    {
    return this->ComputeAStep(xprev, dxprev, xnext, t, delT, estErr);
    }
  else if ( minStep > maxStep )
    {
    return UNEXPECTED_VALUE;
    }

  double errRatio, tmp, tmp2;
  int retVal, shouldBreak = 0;

  // Reduce the step size until estimated error <= maximum allowed error
  while ( estErr > maxError )
    {
    if ((retVal =
     this->ComputeAStep(xprev, dxprev, xnext, t, delT, estErr)))
      {
      delTActual = delT;
      return retVal;
      }
    // If the step just taken was either min, we are done,
    // break
    absDT = fabs(delT);
    if ( absDT == minStep )
      {
      break;
      }

    errRatio = static_cast<double>(estErr) / static_cast<double>(maxError);
    // Empirical formulae for calculating next step size
    // 0.9 is a safety factor to prevent infinite loops (see reference)
    if ( errRatio == 0.0 ) // avoid pow errors
      {
      tmp = delT < 0 ? -minStep : minStep;  // arbitrarily set to minStep
      }
    else if ( errRatio > 1 )
      {
      tmp = 0.9*delT*pow(errRatio, -0.25);
      }
    else
      {
      tmp = 0.9*delT*pow(errRatio, -0.2);
      }
    tmp2 = fabs(tmp);

    // Re-adjust step size if it exceeds the bounds
    // If this happens, calculate once with the extrama step
    // size and break (flagged by setting shouldBreak, see below)
    if (tmp2 > maxStep )
      {
      delT = maxStep * delT/fabs(delT);
      shouldBreak = 1;
      }
    else if (tmp2 < minStep)
      {
      delT = minStep * delT/fabs(delT);
      shouldBreak = 1;
      }
    else
      {
      delT = tmp;
      }

    tmp2 = t + delT;
    if ( tmp2 == t )
      {
      vtkWarningMacro("Step size underflow. You must choose a larger "
              "tolerance or set the minimum step size to a larger "
              "value.");
      return UNEXPECTED_VALUE;
      }

    // If the new step size is equal to min or max,
    // calculate once with the extrama step size and break
    // (flagged by setting shouldBreak, see above)
    if (shouldBreak)
      {
      if ( (retVal =
            this->ComputeAStep(xprev, dxprev, xnext, t, delT, estErr)) )
        {
        delTActual = delT;
        return retVal;
        }
      break;
      }
    }

  delTActual = delT;
  return 0;
}

//----------------------------------------------------------------------------
// Calculate next time step
int vtkRungeKutta45::ComputeAStep(double* xprev, double* dxprev,
                  double* xnext, double t, double& delT,
                  double& error)
{
  int i, j, k, numDerivs, numVals;


  if (!this->FunctionSet)
    {
    vtkErrorMacro("No derivative functions are provided!");
    return NOT_INITIALIZED ;
    }

  if (!this->Initialized)
    {
    vtkErrorMacro("Integrator not initialized!");
    return NOT_INITIALIZED;
    }


  numDerivs = this->FunctionSet->GetNumberOfFunctions();
  numVals = numDerivs + 1;
  for(i=0; i<numVals-1; i++)
    {
    this->Vals[i] = xprev[i];
    }
  this->Vals[numVals-1] = t;

  // Obtain the derivatives dx_i at x_i
  if (dxprev)
    {
    for(i=0; i<numDerivs; i++)
      {
      this->NextDerivs[0][i] = dxprev[i];
      }
    }
  else if ( !this->FunctionSet->FunctionValues(this->Vals,
                           this->NextDerivs[0]) )
    {
    for(i=0; i<numVals-1; i++)
      {
      xnext[i] = this->Vals[i];
      }
    return OUT_OF_DOMAIN;
    }

  double sum;
  for (i=1; i<6; i++)
    {
    // Step i
    // Calculate k_i (NextDerivs) for each step
    for(j=0; j<numVals-1; j++)
      {
      sum = 0;
      for (k=0; k<i; k++)
        {
        sum += B[i-1][k]*this->NextDerivs[k][j];
        }
      this->Vals[j] = xprev[j] + delT*sum;
      }
    this->Vals[numVals-1] = t + delT*A[i-1];

    if ( !this->FunctionSet->FunctionValues(this->Vals,
                                            this->NextDerivs[i]) )
      {
      for(i=0; i<numVals-1; i++)
        {
        xnext[i] = this->Vals[i];
        }
      return OUT_OF_DOMAIN;
      }
    }


  // Calculate xnext
  for(i=0; i<numDerivs; i++)
    {
    sum = 0;
    for (j=0; j<6; j++)
      {
      sum += C[j]*this->NextDerivs[j][i];
      }
    xnext[i] = xprev[i] + delT*sum;
    }

  // Calculate norm of error vector
  double err=0;
  for(i=0; i<numDerivs; i++)
    {
    sum = 0;
    for (j=0; j<6; j++)
      {
      sum += DC[j]*this->NextDerivs[j][i];
      }
    err += delT*sum*delT*sum;
    }
  error = sqrt(err);

  int numZero = 0;
  for(i=0; i<numDerivs; i++)
    {
    if ( xnext[i] == xprev[i] )
      {
      numZero++;
      }
    }
  if (numZero == numDerivs)
    {
    return UNEXPECTED_VALUE;
    }

  return 0;
}

//----------------------------------------------------------------------------
void vtkRungeKutta45::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
}