-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp.cpp
More file actions
184 lines (162 loc) · 4.02 KB
/
Copy pathp.cpp
File metadata and controls
184 lines (162 loc) · 4.02 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <fstream>
#include "utilityfunctions.cpp"
int N{100}; // #agents
int n{500}; // #time steps
constexpr float W{1200.};
constexpr float H{1200.};
constexpr float dt = 0.01;
constexpr float beta = 60;
float w2, h2;
float unitrand();
float map(float, float, float, float, float);
float Hv(float);
float noisemag(float);
class MOVER
{
public:
float x, y;
float vx, vy;
MOVER()
{
x = map(unitrand(), 0., 1., -w2, w2);
y = map(unitrand(), 0., 1., -h2, h2);
// vx = 0.;
// vy = 0.;
vx = (unitrand() - 0.5) * 500;
vy = (unitrand() - 0.5) * 500;
}
void update()
{
x += vx * dt;
y += vy * dt;
}
void checkedgesperiodic()
{
if (x > w2)
// x -= W;
x = -w2;
else if (x < 0)
// x += W;
x = w2;
if (y > h2)
// y -= H;
y = -h2;
else if (y < 0)
// y += H;
y = h2;
}
void checkedgesreflective()
{
if (x > w2)
vx *= -1;
else if (x < -w2)
vx *= -1;
if (y > h2)
vy *= -1;
else if (y < -w2)
vy *= -1;
}
};
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';
}
float getinteractionforce(float r)
{
// float U0 = 2650, U1 = 30, U2 = 2, U3 = 1;
// float A0 = 8, A1 = 2, A2 = 25, A3 = 26;
// float force = 0;
// force += U0 * r * exp(-(pow((r / A0), 2)));
// force += U2 * exp(-r / A2);
// force -= U3 * pow(r - A3, 2) * Hv(r - A3);
// force += U1 * (r - A1) * Hv(r - A1);
// return force;
return 10000000. / (r * r); // gravity
}
void calcvelocities(MOVER M[])
{
float f;
float dx, dy, vx, vy, ax, ay, r;
float theta;
for (int i = 0; i < N; i++)
{
// new branch
ax = 0;
ay = 0;
for (int j = 0; j < N; j++)
{ // interaction force
if (i != j)
{
// interactionforce(&ax, &ay, M[i], M[j]);
dx = M[j].x - M[i].x;
dy = M[j].y - M[i].y;
r = sqrt(dx * dx + dy * dy);
// if (r < 70)
// {
theta = atan2(dy, dx);
// f = -1 * getinteractionforce(r);
f = 1 * getinteractionforce(r);
ax += f * cos(theta);
ay += f * sin(theta);
// }
// interaction force end
}
}
// float axt = 0, ayt = 0;
// int Ni = 1; // number of nearest neighbors of M[i]
// for (int j = 0; j < N; j++)
// { // viscek force
// if (i != j)
// {
// dx = M[j].x - M[i].x;
// dy = M[j].y - M[i].y;
// r = sqrt(dx * dx + dy * dy);
// if (r < 70)
// {
// Ni += 1;
// axt += (M[j].vx - M[i].vx);
// ayt += (M[j].vy - M[i].vy);
// }
// }
// }
// axt *= (beta / Ni);
// ayt *= (beta / Ni);
// ax += axt;
// ay += ayt;
M[i].vx += ax * dt;
M[i].vy += ay * dt;
// std::cout << " vx " << vx << " vy " << vy << "\n";
}
}
void updateposition(MOVER M[])
{
for (int i = 0; i < N; i++)
{
M[i].update();
// M[i].checkedgesperiodic();
// M[i].checkedgesreflective();
}
}
int main(int argc, char *argv[])
{
N = std::stoi(argv[1]);
n = std::stoi(argv[2]);
srand(1);
std::ofstream posfile;
posfile.open("positiondata.csv");
w2 = W / 2;
h2 = H / 2;
MOVER M[N];
for (int t = 0; t < n; t++)
{
calcvelocities(M);
updateposition(M);
writeposition(M, posfile);
}
std::cout << "simulation complete\n";
}