Non-Parametric Monte-Carlo Simulation

Program consists of the following files. All are tested using Visual C++ of Microsoft. There could be minor incompatibilities with other compilers. Program doesn’t have graphic capabilities which requires it to be written as an Windows program. In order to save space this program only displays console output and saves the results to a file.

The equation.cpp given here has ODE for spring with friction. Monint2vc.cpp is the sample code which has the main file.

This version of the non-parametric Monte-Carlo simulation program uses step halfing method when randomly generates numbers between two existing data points (interpolation). When the point is bigger or smaller then the points in the database, it randomly generates an angle and calculates the correspoing point.


 

 

Description

Files

Sets

Foundation classes. Includes vector and matrix classes which are used in the integrator

sets.cpp

sets.h

Integ

ODE Integrator

integ.cpp

integ.h

Equation

Equation to be integrated and the envelope functions for the monotonic function

equation.cpp

equation.h

Monoto6

Monotonic function definitions

monoto6.cpp

monoto6.h

Sets.cpp

Defines the vortex and matrix classes. These classes are used by the integrator and are designed to allow easy reading and programming. Elimates extra code entry for vector and matrix operations.

Descriptions of the classes within this file is as following:

class set

set is the parent class to vector and matrix classes. Member functions are a double pointer and an integer keeping the dimension of the set. It is declared by passing the dimension.

set (int n) (where n is the dimension of the set)

set overloads brackets [], therefore it is possible to read and write its member double numbers. Index starts from 1 for both vector and matrix.

a[2] = 12.23; //sets second member of set a to 12.23

cout << a[2]; //prints second member of set a to screen

Other methods of the class set are

Method

Description

void reset (int )

Resets the dimension to a new value and deletes the old data in set

int getdim ( )

Returns the dimension of the set. 

double* getpointer ( )

Returns the pointer to the contents of the set

void operator=(set &)

Makes two sets equal

int operator==(set &)

Returns 1 if two sets are equal, returns 0 if not

int operator!=(set &)

Returns 1 if two sets are not equal, returns 0 if not

int operator+=(set &)

Adds each corresponding value of set a number which is given by the set passed in to this function. 
x += y means x[i] = x[i] + y[i] for all i

int operator-=(set &)

Same as the above but with minus sign

void SetAll(double )

Makes all the members of the set equal to the double value.

void AddAll(double )

Adds the double value to all the members of the set

void MulAll(double )

Multiplies members of the set with double

double findmax( )

Finds the maximum number within the set and returns its value

double findmin( )

Finds the minimum number within the set and returns its value

void reducedim( )

Redecuses dimension by one and discards the first value of the old set.

void getvalues( )

Allows user to enter values of the set from keyboard

void print( )

Prints the contents of the set to screen

class vector

Vector inherits class set. This means that vector has access to all the members and methods of set.

Method

Description

vector (int )

Constructs the class by calling the set constructor to allocate an amount of memory indicated by the passed integer value

vector (const vector&)

Copy constructor calls copy constructor of set.

vector operator+(vector& )

Vector addition

vector operator-(vector& )

Vector substraction

double operator*(vector& )

Vector multiplication. Returns the result.

double norm()

Calculates norm of the vector.

void unit()

Makes vector a unit vector.

Following are the friend functions they are not part of the class but has access to all the data within the class.

Friend Method

Description

vector operator*(double , vector& )

Multiplicaiton of double with a vector. Returns a vector

vector operator*(vector , double )

Same as above.

File Integ

Includes the integrator. Adaptive step size Runge Kutta and Bulirsch-Stoer methods are taken from Numerical Recipies for C and adapted to C++. This file also includes class ODE as a parent class to class Equation class. Class Equation is defined under the equation file. This seperation is made to be able to compile user defined equations without compiling the integ.cpp file.

Integrator is called using the following.

odeint(vector ystart, int nvar, double x1, double x2,

double eps, double h1, double hmin, int nok, int nbad,

ODE eq, matrix yp, vector xp);

vector ystart is the initial values which will be passed to the integrator. Results will be stored to this variable
int nvar is the dimension of the ystart.
double x1 is the starting x value where the initial ystart was.
double x2 is the x value where we want to find the values of the state variables.
double eps is the precision we want
double h1 is the step size which will be tried first. Could be set to x2-x2
double hmin is the minimum allowed step size. This could be set to zero.
int nok returns the number of the good steps taken.
int nbad returns the number of bad steps taken.
ODE eq is the equation which will be defined at equation.cpp
matrix yp user storage for intermediate results. If kmax ¹ 0 results are stored at intervals dxsav in yp and xp
vector xp as described above.

It is possible switch between Runge-Kutta and Bulirsch-Stoer by changing rkqc to bsstep or vice-versa in odeint.

This file has three values which has to be included in the main file. These are

int kount, kmax

double dxsav
 

These variables are used for storage of the intermediate values when odeint is called. kmax must be set equal to zero. kmax is the maximum number of interval recordings we want to make. xp and yp has to have same dimensions as kmax. dxsav shows the intervals.

class ODE

Only member is the dimension which is an integer value. This value is used for some of the problems. (e.g. For the integration of 4 point vortices on a plane, this dimension becomes 4)

Method

Description

ODE (int )

Constructs the class and sets the dimension equal to the integter value passed.

ODE ( )

Constructs the calss and sets the dimension to zero. 

int getdim ( )

Returns the dimension

virtual void derivs(double, vector &, vector &)

This is the function which will be overloaded by the child class. The method in parent class is empty.

File equation

This file includes envelope functions of following format.

double <function_name> (double )
 

class Equation

This class is child class of ODE. It declares monotonic functions in its constructor and over-rides derivs method.
 

File Monoto6

This file is the only code you will need if you have your own integrator. File includes two classes TwoDouble and Monotonic.

class TwoDouble

These classes defines twosingle linked lists which are keeping double numbers. Members are two double numbers and a pointer to the next TwoDouble item.

Method

Description

TwoDouble (double ,double )

Constructs the class and stores the two double numbers

TwoDouble ()

Constructs the class 

double getX()

Returns the x value

double getY()

Returns the x value

TwoDouble *getNext()

Returns a pointer to the next item.

TwoDouble *find (double )

Returns the item with an x value which is just before the passed double number. (e.g. If the list has numbers 1,3,5,7 and if we ask to find 4. This function returns 3)

void sortStore (TwoDouble **)

When the starting pointer to pointer is passed this TwoDouble item is stored in its place. (e.g. If we are going to store 4 as in the above example, this function stores it after 3)

class Monotonic

Members are :

double (*upEnvelope) (double) upper envelope function

double (*lowEnvelope) (double) lower envelope function

TwoDouble *start pointer to the first item of the double linked list

long int tot keeps track of total numbers items stored

Method

Description

Monotonic (double (*up) (double), double (*low) (double))

Constructs the class and stores the enveloping functions

Monotonic ()

Constructs the class with no envelopes

TwoDouble* getStart()

Returns the beginning of the link list

void passesThru (double x, double y)

Stores this pair into the link list. 

double getY(double x)

Randomly generates a y value and .stores it to the link list.

void print()

Prints all the stored values in the link list to the screen

long getTot()

Returns number of TwoDouble items created.