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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkRungeKutta4.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 "vtkRungeKutta4.h"
#include "vtkFunctionSet.h"
#include "vtkObjectFactory.h"
vtkStandardNewMacro(vtkRungeKutta4);
vtkRungeKutta4::vtkRungeKutta4()
{
for(int i=0; i<3; i++)
{
this->NextDerivs[i] = 0;
}
}
vtkRungeKutta4::~vtkRungeKutta4()
{
for(int i=0; i<3; i++)
{
delete[] this->NextDerivs[i];
this->NextDerivs[i] = 0;
}
}
void vtkRungeKutta4::Initialize()
{
this->vtkInitialValueProblemSolver::Initialize();
if (!this->Initialized)
{
return;
}
// Allocate memory for temporary derivatives array
for(int i=0; i<3; i++)
{
this->NextDerivs[i] =
new double[this->FunctionSet->GetNumberOfFunctions()];
}
}
// For a detailed description of Runge-Kutta methods,
// see, for example, Numerical Recipes in (C/Fortran/Pascal) by
// Press et al. (Cambridge University Press) or
// Applied Numerical Analysis by C. F. Gerald and P. O. Wheatley
// (Addison Wesley)
int vtkRungeKutta4::ComputeNextStep(double* xprev, double* dxprev, double* xnext,
double t, double& delT, double& delTActual,
double, double, double, double& error)
{
int i, numDerivs, numVals;
delTActual = delT;
error = 0;
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;
// 4th order
// 1
if (dxprev)
{
for(i=0; i<numDerivs; i++)
{
this->Derivs[i] = dxprev[i];
}
}
else if ( !this->FunctionSet->FunctionValues(this->Vals, this->Derivs) )
{
return OUT_OF_DOMAIN;
}
for(i=0; i<numVals-1; i++)
{
this->Vals[i] = xprev[i] + delT/2.0*this->Derivs[i];
}
this->Vals[numVals-1] = t + delT/2.0;
// 2
if (!this->FunctionSet->FunctionValues(this->Vals, this->NextDerivs[0]))
{
return OUT_OF_DOMAIN;
}
for(i=0; i<numVals-1; i++)
{
this->Vals[i] = xprev[i] + delT/2.0*this->NextDerivs[0][i];
}
this->Vals[numVals-1] = t + delT/2.0;
// 3
if (!this->FunctionSet->FunctionValues(this->Vals, this->NextDerivs[1]))
{
return OUT_OF_DOMAIN;
}
for(i=0; i<numVals-1; i++)
{
this->Vals[i] = xprev[i] + delT*this->NextDerivs[1][i];
}
this->Vals[numVals-1] = t + delT;
// 4
if (!this->FunctionSet->FunctionValues(this->Vals, this->NextDerivs[2]))
{
return OUT_OF_DOMAIN;
}
for(i=0; i<numDerivs; i++)
{
xnext[i] = xprev[i] + delT*(this->Derivs[i]/6.0 +
this->NextDerivs[0][i]/3.0 +
this->NextDerivs[1][i]/3.0 +
this->NextDerivs[2][i]/6.0);
}
return 0;
}
void vtkRungeKutta4::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Runge-Kutta 4 function derivatives: "
<< this->NextDerivs[0] << " " << this->NextDerivs[1] << " "
<< this->NextDerivs[2] << endl;
}
|