73 lines
2.1 KiB
C++
73 lines
2.1 KiB
C++
#include <cassert>
|
|
#include <cstdlib>
|
|
#include <ctime>
|
|
#include <fstream>
|
|
|
|
#include "chain-random-walk.h"
|
|
|
|
namespace {
|
|
constexpr int default_size = 7;
|
|
constexpr int default_steps = 100000000;
|
|
constexpr int outlines = 25;
|
|
}
|
|
|
|
/* first argument: array size
|
|
* second argument: number of steps
|
|
*/
|
|
int main(int argc, char** argv)
|
|
{
|
|
long size = default_size;
|
|
if(argc > 1) size = std::atol(argv[1]);
|
|
assert(size > 0);
|
|
|
|
long steps = default_steps;
|
|
if(argc > 2) steps = std::atol(argv[2]);
|
|
assert(steps > 0);
|
|
|
|
long outfreq = steps / outlines;
|
|
if(outfreq < 1) outfreq = 1;
|
|
|
|
/* the configuration is given by an array of float numbers */
|
|
float* present_configuration = new float[size]();
|
|
|
|
/* store the one with greatest deviation between min and max element */
|
|
float* extreme_configuration = present_configuration;
|
|
float extreme_elongation = 0.0;
|
|
double el_sum = 0.0;
|
|
double el_avg = 0.0;
|
|
|
|
std::srand(std::time(nullptr)); // initialize the random number generator
|
|
|
|
for(long i = 0; i < steps; i++)
|
|
{
|
|
if(i % outfreq == 0)
|
|
crw::output(i, size, present_configuration, extreme_configuration, el_avg);
|
|
|
|
/* here we are calling the random walk step;
|
|
* this returns a new configuration (float array) on the heap
|
|
*/
|
|
|
|
// Simple fix to memleak:
|
|
float* old_configuration = present_configuration;
|
|
present_configuration = crw::step(size, present_configuration);
|
|
delete[] old_configuration;
|
|
float present_elongation = crw::elongation(size, present_configuration);
|
|
|
|
if(present_elongation > extreme_elongation)
|
|
{
|
|
extreme_configuration = present_configuration;
|
|
extreme_elongation = present_elongation;
|
|
}
|
|
el_sum += present_elongation;
|
|
el_avg = el_sum / (i + 1.0);
|
|
}
|
|
crw::output(steps, size, present_configuration, extreme_configuration, el_avg);
|
|
|
|
std::ofstream myfile;
|
|
myfile.open ("elongation.csv", std::ios::app);
|
|
myfile << size << ", " << el_avg << ", " << crw::elongation(size, extreme_configuration) << "\n";
|
|
myfile.close();
|
|
|
|
delete[] present_configuration;
|
|
}
|