-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq.cpp
More file actions
153 lines (137 loc) · 2.88 KB
/
Copy pathq.cpp
File metadata and controls
153 lines (137 loc) · 2.88 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
#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <fstream>
#include "utilityfunctions.cpp"
float map(float, float, float, float, float);
float randf(float, float);
float Hv(float);
int N{100}; // # agents
int n{500}; // # time steps
constexpr float W{1200.};
constexpr float H{1200.};
constexpr float beta = 60;
constexpr float dt = 0.01;
float w2, h2;
class MOVER
{
public:
float x, y;
float vx, vy;
float ax, ay;
MOVER()
{
x = randf(-w2, w2);
y = randf(-h2, h2);
vx = randf(-10, 10) * 0;
vy = randf(-10, 10) * 0;
ax = 0.;
ay = 0.;
}
void checkedgesreflect()
{
if (x > w2)
{
x = w2;
vx *= -1;
}
else if (x < -w2)
{
x = -w2;
vx *= -1;
}
if (y > h2)
{
y = h2;
vy *= -1;
}
else if (y < -h2)
{
y = -h2;
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 forcemag(float r)
{
return 1E9 / (r * r); // gravity;
// lennard jones
/*float eps = 1e6;
float sig = 0.1;
float f = 4 * eps;
f *= (12 * pow(sig, 12) * pow(r, -13)) -
6 * pow(sig, 6) * pow(r, -7);
return f; */
}
void initaccn(MOVER M[])
{ /*
Sets accn of all cells to zero.
Called in every time step */
for (int i = 0; i < N; i++)
{
M[i].ax = 0.;
M[i].ay = 0.;
}
}
void getinteractionforce(MOVER &A, MOVER &B)
{
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);
A.ax += fx;
A.ay += fy;
// B.ax -= fx;
// B.ay -= fy;
}
void looploop(MOVER M[])
{
/*Two loops over every interacting pair*/
initaccn(M);
for (int i = 0; i < N; i++)
{ //every particle except the last
for (int j = 0; j < N; j++)
{ //every particle starting from i,
// including the last
getinteractionforce(M[i], M[j]);
}
}
}
void updatepos(MOVER M[])
{
for (int i = 0; i < N; i++)
{
M[i].vx += M[i].ax * dt;
M[i].vy += M[i].ay * dt;
M[i].x += M[i].vx * dt;
M[i].y += M[i].vy * dt;
// M[i].checkedgesreflect();
}
}
int main(int argc, char *argv[])
{
N = std::stoi(argv[1]);
n = std::stoi(argv[2]);
w2 = W / 2;
h2 = H / 2;
MOVER M[N];
srand(1);
std::ofstream posfile;
posfile.open("positiondata.csv");
for (int t = 0; t < n; t++)
{
// time loop
looploop(M);
updatepos(M);
writeposition(M, posfile);
}
std::cout << "simulation complete\n";
}