next up previous contents
Next: Precision and Error Up: Monte-Carlo Simulation with MCS-libre Previous: Retrieving Statistical Information   Contents

A Detailed Example Simulation

The following simulation program demonstrates the use of the mcsLibre and mcsLibreCollectStats classes in tandom. In this case propagation of error in a calculation of the Reynold's number is simulated. The Reynold's number is a non-dimensional value important in the field of fluid mechanics. This file is included in the "examples" directory of the toolkit distribution.

/* Demonstration of MCS-libre toolkit for computing propagation of
   error in the Reynolds number. */

#include <iostream.h>

// include mcsLibre simulation library
#include "../include/mcsLibre.h" 

// include mcsLibre statistical probe class
#include "../include/mcsLibreCollectStats.h"

main()
{
  // create object for simulation
  mcsLibre reynolds;

  // create probe for data collection
  mcsLibreCollectStats reynolds_stats;

  // define parameter types
  double Re, Re_mean, Re_stdev;    // Reynolds number of flow
  double nu, nu_mean, nu_stdev;    // kinematic viscosity of fluid
  double v, v_mean, v_stdev;       // velocity of flow
  double x, x_mean, x_stdev;       // distance considered

  // set mean and standard deviations from measurements
  nu_mean = 0.0000157; nu_stdev = 0.000002;  //  m^2 / s
  v_mean = 2; v_stdev = 0.1;                 //  m / s
  x_mean = 12; x_stdev = 1;                  //  m

  // simulation loop
  int samples = 15000;      // number of simulations
  for (int loopvar = 0; loopvar < samples; loopvar++)
    {
      // simulate nu from stream 1
      nu = reynolds.normal(nu_mean, nu_stdev, 1);

      // simulate v from stream 2
      v = reynolds.normal(v_mean, v_stdev, 2);

      // simulate x from stream 3
      x = reynolds.normal(x_mean, x_stdev, 3);
      
      // calculate a sample Reynolds number
      Re = (v * x) / nu;

      // send sample value to data collection probe
      reynolds_stats.add_value(Re);
    }

  // retrieve mean value from simulation
  Re_mean = reynolds_stats.get_mean_value();

  // retrieve standard deviation value from simulation
  Re_stdev = reynolds_stats.get_stdev_value();

  cout << endl << "Simulation of Error Propagation " 
       << "in Reynold's Number" << endl;
  cout << "Re = (v * x) / nu" << endl << endl;
  cout << "Let v = normal(" << v_mean 
       << ", " << v_stdev << "), x = normal(" 
       << x_mean << ", " << x_stdev << ")," << endl;
  cout << "and nu = normal(" << nu_mean << ", " 
       << nu_stdev << ")." << endl << endl;
  cout << "Simulating " << samples 
       << " Reynold's numbers yields:" << endl << endl;
  cout << "Sample Mean = " << Re_mean 
       << "   Sample Standard Deviation = " 
       << Re_stdev << endl;
  cout << "Sample Max = " << reynolds_stats.get_max_value();
  cout << "    Sample Min = " 
       << reynolds_stats.get_min_value() << endl;
  cout << "Sum of Samples = " 
       << reynolds_stats.get_sum_of_values() << endl;
  cout << "Number of Samples = " 
       << reynolds_stats.get_number_samples() << endl;
  cout << endl;
}



Hydroponics 2002-06-08