-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcds.cc
95 lines (81 loc) · 2.48 KB
/
cds.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* A simple command-line tool for coupled dynamical systems simulation
* This program simulates a threshold-coupled lattice of chaotic logistic maps
* As described by Sudeshna Sinha et al.
*
* See:
* Sinha and Biswas "Adaptive Dynamics on a Chaotic Lattice" Physical Review Letters vol 71 no 13 September 1993
* Sinha and Ditto "Computing with Distributed Chaos" Physical Review E vol 60 no 1, July 1999
*
* Author: Matt Squire
* Date: Fri Dec 14, 2012 2:30 PM
*
* http://github.com/archena/CDS
*/
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string.h>
#include "lattice.hh"
using namespace std;
int main(int argc, char* argv[])
{
if(argc < 4)
{
cout << "Usage: cds <size> <iterations> <threshold> [(csv|png)] [filename]. The final two arguments optionally and used to specify a file export format." << endl;
exit(1);
}
unsigned size = atoi(argv[1]);
unsigned iterations = atoi(argv[2]);
cell_t t = atof(argv[3]);
cell_t a = 4;
// Set up the lattice with update and coupling functions
Lattice lattice(size,
[a](cell_t x) { return a * x * (1 - x); },
[t](cell_t& x1, cell_t& x2)
{
cell_t excess = x1 > t ? x1 - t : 0;
x1 -= excess;
x2 += excess;
});
lattice.randomize(0, 1);
if(argc == 5)
{
if(strncmp(argv[4], "csv", 3) == 0)
{
// const unsigned max_filename = 100;
// char filename[max_filename + 1] = {0};
// strncpy(filename, argv[5], max_filename);
// Output as csv
cell_t excess = 0;
// Initial state followed by a number of updates
for(int i = 0; i <= iterations; ++i)
{
for_each(lattice.begin(), lattice.end(),
[](cell_t x) { cout << x << ","; });
cout << excess << endl;
excess = lattice.update();
}
}
else if(strncmp(argv[4], "png", 3) == 0)
{
// Write output as an image (time-space diagram)
cout << "Image export not supported" << endl;
}
}
else
{
// Print a textual summary to standard out
cout << lattice
<< endl
<< endl;
for(int i = 0; i < iterations; ++i)
{
cell_t excess = lattice.update();
cout << lattice << " | " << excess << endl;
}
cout << endl
<< size << " cells with "
<< iterations << " iterations and threshold " << t << endl;
}
}