-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht.cpp
More file actions
executable file
·152 lines (136 loc) · 3.45 KB
/
Copy patht.cpp
File metadata and controls
executable file
·152 lines (136 loc) · 3.45 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <fstream>
// void calcthetas(MOVER M[]);
// void updateposition(MOVER M[]);
// void writeposition(MOVER M[], std::ofstream&);
float unitrand();
float map(float ri, float x1, float x2, float y1, float y2);
const double PI = 3.14159265358979323846;
float W = 100.; // total W
float H = 100.; // total H
float w2, h2; // half W and H
float r = 5.0; // interaction radius
float r2; // squared iteraction radius
int N = 500; // number of particles
int n = 500; // number of time-steps
float eta; // randomness parameter
class MOVER
{
public:
float x, y;
float vx, vy;
float vabs, theta;
MOVER()
{
x = map(unitrand(), 0., 1., -w2, w2);
y = map(unitrand(), 0., 1., -h2, h2);
theta = map(unitrand(), 0., 1., -PI, PI);
vabs = 1.;
}
void update()
{
vx = vabs * cos(theta);
vy = vabs * sin(theta);
x = x + vx;
y = y + vy;
}
void display()
{
std::cout << x << ' ' << y << '\n';
}
};
void calcthetas(MOVER M[])
/*Calculates the directions for all the movers*/
{
int ninr;
float avtheta, ssin, scos, dx, dy, dxe, dye, d2;
for (int i = 0; i < N; i++)
{
// outer object loop
ninr = 0;
avtheta = 0.;
ssin = scos = 0.;
for (int j = 0; j < N; j++)
{
// inner object loop
dx = M[j].x - M[i].x;
dy = M[j].y - M[i].y;
dxe = dx - W * std::nearbyintf(dx / W);
dye = dy - H * std::nearbyintf(dy / H);
d2 = pow(dxe, 2) + pow(dye, 2);
if (d2 < r2)
{
// check radius
ninr++;
ssin += sin(M[j].theta);
scos += cos(M[j].theta);
}
}
ssin /= ninr; // averaging out
scos /= ninr; // "
avtheta = atan2(ssin, scos); // direction
M[i].theta = avtheta + map(unitrand(), 0, 1, -eta, eta); // assigning direction, adding randomness
}
}
void updateposition(MOVER M[])
/*Updates the position of all the movers */
{
for (int i = 0; i < N; i++)
{
M[i].update();
}
}
void writeposition(MOVER M[], std::ofstream &file)
{
for (int i = 0; i < N - 1; i++)
{
file << M[i].x << ',' << M[i].y << ',';
}
file << M[N - 1].x << ',' << M[N - 1].y << '\n';
}
void writevelocity(MOVER M[], std::ofstream &file)
{
for (int i = 0; i < N - 1; i++)
{
file << M[i].vx << ',' << M[i].vy << ',';
}
file << M[N - 1].vx << ',' << M[N - 1].vy << '\n';
}
float unitrand()
/*Returns a float chosen randomly from [0,1] */
{
return float(rand()) / INT_MAX;
}
float map(float ri, float x1, float x2, float y1, float y2)
/*Maps one interval to another */
{
float runit = (ri - x1) / (x2 - x1);
float rf = runit * (y2 - y1) + y1;
return rf;
}
int main()
{
srand(1); // random seed
std::ofstream posfile; // file object
std::ofstream velfile;
posfile.open("positiondata.csv"); // open file
velfile.open("velocitydata.csv");
eta = PI/8;
w2 = W / 2;
h2 = H / 2;
r2 = r * r;
MOVER M[N];
// time-loop
for (int i = 0; i < n; i++)
{
calcthetas(M);
updateposition(M);
writeposition(M, posfile);
writevelocity(M, velfile);
}
posfile.close();
velfile.close();
return 0;
}