-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqf.cpp
More file actions
200 lines (181 loc) · 4.04 KB
/
Copy pathqf.cpp
File metadata and controls
200 lines (181 loc) · 4.04 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#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 CELL
{
public:
float x, y,
vx, vy,
ax, ay,
avx, avy;
int ninr;
CELL()
{
x = randf(-w2, w2);
y = randf(-h2, h2);
vx = randf(-10, 10);
vy = randf(-10, 10);
ax = ay = 0.;
avx = avy = 0.; // viscek accn.
ninr = 0; // viscek neighbors
}
void update()
{
vx += (ax * dt);
vy += (ay * dt);
x += (vx * dt);
y += (vy * dt);
}
void wallreflect()
{
if ((x > w2) || (x < -w2))
{
vx *= -1;
}
if ((y > h2) || (y < -h2))
{
vy *= -1;
}
}
void wallperiodic()
{
if (x > w2)
x = -w2;
if (x < -w2)
x = w2;
if (y > h2)
y = -h2;
if (y < -h2)
y = h2;
}
};
void writeposition(CELL M[], std::ofstream &file)
{ /*
Writes the coordinates of all the cells to 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 initaccn(CELL M[])
{ /*
Sets accn of all cells to zero.
As well as the number of neighbors
*/
for (int i = 0; i < N; i++)
{
M[i].ax = M[i].ay = 0.;
M[i].avx = M[i].avy = 0.;
M[i].ninr = 0;
}
}
float forcemag(float r)
{ /*
magnitude of the force
*/
// return 1e4 / r * r;//gravity
/* Interaction potential (Nir Gov 2015)*/
if (r > 70)
return 0.;
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;
/* lennard jones */
// float eps = 21.;
// float sig = 78.;
// float force = 0.;
// force += 6 * pow(sig, 6) / pow(r, 7);
// force -= 12 * pow(sig, 12) / pow(r, 13);
// force *= 4 * eps;
// return force;
}
void addforcetoaccn(CELL &A, CELL &B, float fx, float fy)
{
A.ax += fx;
A.ay += fy;
B.ax -= fx;
B.ay -= fy;
}
void setaccn(CELL &A, CELL &B)
{ /*
Sets the acceleration of the cells passed
*/
//interaction force
float dx = B.x - A.x;
float dy = B.y - A.y;
float r = sqrt(dx * dx + dy * dy);
float f = forcemag(r);
float fx = f * (dx / r);
float fy = f * (dy / r);
addforcetoaccn(A, B, fx, fy);
//viscek force
if (r < 100.)
{
A.avx += B.vx - A.vx;
A.avy += B.vy - A.vy;
A.ninr += 1;
B.avx += A.vx - B.vx;
B.avy += A.vy - B.vy;
B.ninr += 1;
}
}
void looploop(CELL M[])
{
initaccn(M);
// find acceleration
for (int i = 0; i < N - 1; i++)
{ // every particle except the last
for (int j = i + 1; j < N; j++)
{ // every particle from i
setaccn(M[i], M[j]);
}
}
for (int i = 0; i < N; i++)
{ // add vicesk accn to accn
M[i].avx *= (beta / M[i].ninr);
M[i].avy *= (beta / M[i].ninr);
M[i].ax += M[i].avx;
M[i].ay += M[i].avy;
}
for (int i = 0; i < N; i++)
{ // position update loop
M[i].update();
// M[i].wallreflect();
// M[i].wallperiodic();
}
}
int main(int argc, char *argv[])
{
N = std::stoi(argv[1]);
n = std::stoi(argv[2]);
w2 = W / 2;
h2 = H / 2;
CELL M[N];
srand(1);
std::ofstream posfile;
posfile.open("positiondata.csv");
for (int t = 0; t < n; t++)
{
looploop(M);
writeposition(M, posfile);
}
std::cout << "simulation complete\n";
}