CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
StepDoublingRKStepper.cc
Go to the documentation of this file.
1#include "CLHEP/GenericFunctions/StepDoublingRKStepper.hh"
2#include <stdexcept>
3#include <cmath>
4#include <vector>
5namespace Genfun {
6
7
8 StepDoublingRKStepper::StepDoublingRKStepper(const ButcherTableau & xtableau):tableau(xtableau) {
9 }
10
12 }
13
17 std::vector<double> & errors) const {
18 const unsigned int nvar = (unsigned int)s.variable.size();
19 RKIntegrator::RKData::Data d1(nvar),d2(nvar);
20
21 doStep(data,s,d);
22 double dt = (d.time - s.time);
23 d1.time = s.time + dt/2.0;
24 d2.time = d.time;
25
26 doStep(data, s,d1);
27 doStep(data,d1,d2);
28
29 // Error estimate:
30 errors.resize(nvar);
31 for (size_t v=0;v<nvar;v++) errors[v]=fabs(d2.variable[v]-d.variable[v]);
32
33 // Final correction:
34 for (size_t v=0;v<nvar;v++) d.variable[v] = d2.variable[v] + ((d2.variable[v]-d.variable[v])/double(std::pow(2.,(int)(tableau.order())-1)));
35
36 }
37
41 // First step:
42 double h = (d.time - s.time);
43
44
45 if (h<=0) throw std::runtime_error ("SimpleRKStepper: negative stepsize");
46 const unsigned int nvar = (unsigned int)s.variable.size();
47 // Compute all of the k's..:
48 //
49 std::vector<std::vector<double> >k(tableau.nSteps());
50 for (unsigned int i=0;i<tableau.nSteps();i++) {
51 k[i].resize(nvar,0);
52 Argument arg(nvar);
53 for (unsigned int v=0;v<nvar;v++) arg[v]=s.variable[v];
54 for (unsigned int j=0;j<i;j++) {
55 for (unsigned int v=0;v<nvar;v++) arg[v] += h*tableau.A(i,j)*k[j][v];
56 }
57 for (unsigned int v=0;v<nvar;v++) k[i][v]=(*data->_diffEqn[v])(arg);
58 }
59 //
60 // Final result.
61 //
62 for (unsigned int v=0;v<nvar;v++) d.firstDerivative[v] = 0;
63 for (unsigned int i=0;i<tableau.nSteps();i++) {
64 for (unsigned int v=0;v<nvar;v++) d.firstDerivative[v] += tableau.b(i)*k[i][v];
65 }
66 for (unsigned int v=0;v<nvar;v++) d.variable[v] =s.variable[v]+h*d.firstDerivative[v];
67
68 }
69
70
71
72
74 return new StepDoublingRKStepper(*this);
75 }
76
77 unsigned int StepDoublingRKStepper::order() const {
78 return tableau.order();
79 }
80
81
82}
unsigned int nSteps() const
double & A(unsigned int i, unsigned int j)
double & b(unsigned int i)
unsigned int order() const
std::vector< const AbsFunction * > _diffEqn
virtual void step(const RKIntegrator::RKData *data, const RKIntegrator::RKData::Data &sdata, RKIntegrator::RKData::Data &ddata, std::vector< double > &errors) const
virtual unsigned int order() const
void doStep(const RKIntegrator::RKData *data, const RKIntegrator::RKData::Data &s, RKIntegrator::RKData::Data &d) const
virtual StepDoublingRKStepper * clone() const
StepDoublingRKStepper(const ButcherTableau &tableau)
Definition: Abs.hh:14
std::vector< double > firstDerivative
std::vector< double > variable