proj home

Files   Classes   Functions   Hierarchy  

desys< NINT, T > Class Template Reference

Numerical Integration of a system of differential equations (de's). More...

#include <desys.h>

Collaboration diagram for desys< NINT, T >:

List of all members.

Public Member Functions

TyiGet (uintc i, uintc k)
 Write to the yi state matrix.
T const yiGet (uintc i, uintc k) const
 Access the yi state matrix.
 desys (uintc dim_, uintc order_)
 Allocate memory yi uninitialized.
 ~desys ()
 Cleanup.
void eval ()
 Increment the system.
ostreamprint (ostream &os) const
 Output the state of the de system.

Public Attributes

uint dim
 The dimension of the system.
uint order
 The highest order of the derivatives.
Tdy
 The infinitessimal - small change added to y for the next step.
T xval
 The independent variable x.
Tyi
 A 2D matrix of yi[0], yi[1],.
NINT integrator
 Numerical Integrator.
T hstep
 The step sizes.


Detailed Description

template<typename NINT, typename T>
class desys< NINT, T >

Numerical Integration of a system of differential equations (de's).

Theory
The integrators calculate the infinatesimal which is what is added to the previous y to get the new state. ie y[n+1] = y[n] + dy where dy is the infinitesimal.
The infinitesimal is not a derivative except in the simplest case of the Euler method where dy=h*y'. dy approximates (taylor series-y).

Interfaces
The numerical integrator support the following interface.
  void integrator.eval
  ( 
    T & dy,          // Infitesimal or change in y.
    T & h,           // The step size
    T const y,       // dependent variable
    T const x        // independent variable
  );
  integrator(&xval,yi);
  ++integrator.yDorder;

The DE solver has a 2D matrix yi. The first row is yi[0], yi[1], ..., yi[order-1] correspond with y, y', y'', .... y^(order-1). The rows correspond to the dimension, the columns the order.

The numerical integrator is passed the locations to xval and yi[] when it is constructed. This coupling lets the equations see all the variables.

Since there is only one integrator, when the dimension is >1 it needs to behave as the current equation. Since this design eliminated virtual functions it is the clients responsibility to define a ++ operator on D of NINT<D,T> to move to the next equation in the system of equations.

Whether this helps for efficiency I do not know. For when dim>1 the client can use an array of function pointers or a case statement.

Example 1
y'' + y = 0, y(0) = 1 cos(x) is the exact solution.
class desystestf1
{
public:

  double * xval;
  // yi[0] is y, yi[1] is y'
  double * yi;

  desystestf1(double * xval_, double * yi_)
    : xval(xval_), yi(yi_) {}

  void operator ++ () {}

  // y''=-y 
  void operator () (double &  Dz, double const z, double const x) const
    { Dz = -yi[0]; }
};
...
  typedef nintegrationEuler< desystestf1, double > integrator;
  desys< integrator ,double > de(1,2);

  de.yiGet(0,0)=1.0;
  de.yiGet(0,1)=0.0;
  de.hstep=0.001;

  cout << de << endl;

  for (; de.xval < 0.3; de.eval() )
    {}
  cout << de << endl;

Definition at line 93 of file desys.h.


Constructor & Destructor Documentation

template<typename NINT , typename T >
desys< NINT, T >::desys ( uintc  dim_,
uintc  order_ 
) [inline]

Allocate memory yi uninitialized.

Definition at line 181 of file desys.h.

References desys< NINT, T >::dim, desys< NINT, T >::order, and desys< NINT, T >::xval.

00182   : dim(dim_), order(order_), dy(new T[dim]),
00183     yi(new T[order*dim]), integrator(&xval,yi) 
00184 {
00185   assert(dim>0);
00186   assert(order>0);
00187   xval=0.0;
00188 }

template<typename NINT , typename T >
desys< NINT, T >::~desys (  )  [inline]

Cleanup.

Definition at line 174 of file desys.h.

References desys< NINT, T >::dy, and desys< NINT, T >::yi.

00175 {
00176   delete[] dy;
00177   delete[] yi;
00178 }


Member Function Documentation

template<typename NINT , typename T >
void desys< NINT, T >::eval (  )  [inline]

Increment the system.

cout << SHOW(yi[order+c]) << endl;

Definition at line 192 of file desys.h.

References desys< NINT, T >::dim, desys< NINT, T >::dy, desys< NINT, T >::hstep, desys< NINT, T >::integrator, desys< NINT, T >::order, desys< NINT, T >::xval, and desys< NINT, T >::yi.

Referenced by helixtestscope::helixtest::idle03(), desystest::test01(), desystest::test02(), desystest::test03(), desystest::test04(), and desystest::test05().

00193 {
00194   uint c;
00195   assert(hstep!=0);
00196   for (uint i=0; i<dim; ++i )
00197   {
00198     c = i*order+order-1;
00199 
00200 //cout << SHOW(xval) << endl;
00201 
00202 //cout << SHOW(order) << endl;
00203 //cout << SHOW(c) << endl;
00204 //cout << SHOW(order+c) << endl;
00205 //cout << SHOW(i*(order+1)) << endl;
00206 //cout << SHOW(yi[c]) << endl;
00207     integrator.eval(dy[i],hstep,yi[c],xval); 
00208 //print(cout); cout << endl;
00210     yi[c] += dy[i];
00211 //cout << SHOW(yi[c]) << endl;
00212 //cout << SHOW(yi[order-1+c]) << endl;
00213 //cout << "*";
00214 //print(cout); cout << endl;
00215 
00216     ++integrator.yDorder;
00217   }
00218   xval += hstep;
00219 
00220   if (order<=1)
00221     return;
00222     
00223   for (uint i=0; i<dim; ++i )
00224   {
00225     c = i*order + order;
00226 
00227     for (uint k=2; k<=order; ++k ) 
00228       yi[c-k] += yi[c-k+1]*hstep;
00229   }
00230 }

template<typename NINT , typename T >
ostream & desys< NINT, T >::print ( ostream os  )  const [inline]

Output the state of the de system.

Definition at line 150 of file desys.h.

References desys< NINT, T >::dim, desys< NINT, T >::hstep, desys< NINT, T >::order, desys< NINT, T >::xval, and desys< NINT, T >::yi.

00151 {
00152   os << "dim=" << dim << endl;
00153   os << "order=" << order << endl;
00154   os << "hstep=" << hstep << endl;
00155   os << "xval=" << xval << endl;
00156   os << "(y0, y0', ...)" << endl;
00157   os << "(y1, y1', ...)" << endl;
00158   os << "=" << endl;
00159 
00160   os << "yi" << endl;
00161   for (uint i=0; i<dim; ++i)
00162   {
00163     os << "[" << i << "]: ";
00164     for (uint k=0; k<order; ++k)
00165       os << yi[i*order+k] << " ";
00166     os << endl;
00167   }
00168  
00169   return os;
00170 }

template<typename NINT, typename T>
T const desys< NINT, T >::yiGet ( uintc  i,
uintc  k 
) const [inline]

Access the yi state matrix.

Definition at line 118 of file desys.h.

References desys< NINT, T >::dim, desys< NINT, T >::order, and desys< NINT, T >::yi.

00119     { assert(i<dim); assert(k<order); return yi[k+i*order]; }

template<typename NINT, typename T>
T& desys< NINT, T >::yiGet ( uintc  i,
uintc  k 
) [inline]

Write to the yi state matrix.

Definition at line 114 of file desys.h.

References desys< NINT, T >::dim, desys< NINT, T >::order, and desys< NINT, T >::yi.

Referenced by helixtestscope::helixtest::idle03(), desystest::test02(), helixtestscope::helixtest::test03(), desystest::test03(), desystest::test04(), and desystest::test05().

00115     { assert(i<dim); assert(k<order); return yi[k+i*order]; }


Member Data Documentation

template<typename NINT, typename T>
uint desys< NINT, T >::dim

The dimension of the system.

Definition at line 101 of file desys.h.

Referenced by desys< NINT, T >::desys(), desys< NINT, T >::eval(), desys< NINT, T >::print(), and desys< NINT, T >::yiGet().

template<typename NINT, typename T>
T* desys< NINT, T >::dy

The infinitessimal - small change added to y for the next step.

Definition at line 106 of file desys.h.

Referenced by desys< NINT, T >::eval(), and desys< NINT, T >::~desys().

template<typename NINT, typename T>
T desys< NINT, T >::hstep

template<typename NINT, typename T>
NINT desys< NINT, T >::integrator

Numerical Integrator.

Definition at line 122 of file desys.h.

Referenced by desys< NINT, T >::eval(), helixtestscope::helixtest::test03(), and desystest::test05().

template<typename NINT, typename T>
uint desys< NINT, T >::order

The highest order of the derivatives.

Definition at line 103 of file desys.h.

Referenced by desys< NINT, T >::desys(), desys< NINT, T >::eval(), desys< NINT, T >::print(), and desys< NINT, T >::yiGet().

template<typename NINT, typename T>
T desys< NINT, T >::xval

template<typename NINT, typename T>
T* desys< NINT, T >::yi

A 2D matrix of yi[0], yi[1],.

..,yi[Order] variable rows for each dimension.

Definition at line 111 of file desys.h.

Referenced by desys< NINT, T >::eval(), desys< NINT, T >::print(), desys< NINT, T >::yiGet(), and desys< NINT, T >::~desys().


The documentation for this class was generated from the following file:

Generated on Fri Mar 4 00:49:55 2011 for Chelton Evans Source by  doxygen 1.5.8