-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.cpp
260 lines (250 loc) · 11 KB
/
generate.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "include/traj.h"
void generateFromFile(float a0, float r0, int *qs, int *mp, float pos0x[2][n_partd], float pos0y[2][n_partd], float pos0z[2][n_partd],
float pos1x[2][n_partd], float pos1y[2][n_partd], float pos1z[2][n_partd], int q[2][n_partd], int m[2][n_partd], int *nt)
{
ifstream infile("input/pos.txt");
if (!infile.is_open())
{
cerr << "Error reading file (sad)" << endl;
exit(1);
}
string line;
istringstream liness;
float scale;
getline(infile, line);
liness = istringstream(line);
liness >> scale;
float x, y, z, v;
float r = n_space * scale * a0; // occupy 80% of the space (change if needed)
for (int p = 0; p < 2; p++)
{
int n = 0;
getline(infile, line); // the header
cout << "Reading " << line << endl;
while (n < n_partd && getline(infile, line))
{
// input should be in this format: x, y, z, vx, vy, vz
// bounds: -1 < x, y, z < 1; any number for vx, vy, vz, but be realistic.
liness = istringstream(line);
liness >> x;
liness >> y;
liness >> z;
liness >> v;
pos0x[p][n] = x * r;
pos1x[p][n] = pos0x[p][n] + v * r;
liness >> v;
pos0y[p][n] = y * r;
pos1y[p][n] = pos0y[p][n] + v * r;
liness >> v;
pos0z[p][n] = z * r;
pos1z[p][n] = pos0z[p][n] + v * r;
if (n == 0)
cout << pos0x[p][n] << " " << pos1x[p][n] << " " << pos0y[p][n] << " " << pos1y[p][n] << " "
<< pos0z[p][n] << " " << pos1z[p][n] << endl;
n++;
q[p][n] = qs[p];
m[p][n] = mp[p];
nt[p] += q[p][n];
if (n == n_partd)
cout << "SAMPLE: " << x << " " << y << " " << z << " " << v << endl;
}
}
}
void generateLineOfElectrons(float a0, float r0, int *qs, int *mp, float pos0x[2][n_partd], float pos0y[2][n_partd], float pos0z[2][n_partd],
float pos1x[2][n_partd], float pos1y[2][n_partd], float pos1z[2][n_partd], int q[2][n_partd], int m[2][n_partd], int *nt)
{
int particles = 1024;
float r = n_space * a0;
float step = 2.0f / particles;
for (int i = 0; i < n_partd; i++)
{
int part = i % particles; // FINISHME: hack method, should generate them sequentially instead of ABCABCABC
pos0x[0][i] = (part * step - 1.0) * r * 0.8f; //-1.8e3
pos1x[0][i] = pos0x[0][i] + 0.8f * r * step / 4000.0f;
pos0y[0][i] = pos0z[0][i] = pos1y[0][i] = pos1z[0][i] = 0;
pos0x[1][i] = pos0y[1][i] = pos0z[1][i] = pos1x[1][i] = pos1y[1][i] = pos1z[1][i] = 0;
q[0][i] = qs[0];
q[1][i] = qs[1];
m[0][i] = mp[0];
m[1][i] = mp[1];
nt[0] += q[0][i];
nt[1] += q[1][i];
// cout << pos0x[0][i] << " " << r * step / (float)100 << endl;
// cout << pos0y[0][i] << " " << pos1y[0][i] << " " << pos0z[0][i] << " " << pos1z[0][i] << endl;
}
}
void generate8ElectronCorners(float a0, float r0, int *qs, int *mp, float pos0x[2][n_partd], float pos0y[2][n_partd], float pos0z[2][n_partd],
float pos1x[2][n_partd], float pos1y[2][n_partd], float pos1z[2][n_partd], int q[2][n_partd], int m[2][n_partd], int *nt)
{
float c = n_space * a0 * 0.8f;
float xs[] = {c, c, c, c, -c, -c, -c, -c},
ys[] = {c, c, -c, -c, c, c, -c, -c},
zs[] = {c, -c, c, -c, c, -c, c, -c};
for (int i = 0; i < n_partd; ++i)
{
int id = i % 8;
pos0x[0][i] = pos1x[0][i] = xs[id];
pos0y[0][i] = pos1y[0][i] = ys[id];
pos0z[0][i] = pos1z[0][i] = zs[id];
pos0x[1][i] = pos0y[1][i] = pos0z[1][i] = pos1x[1][i] = pos1y[1][i] = pos1z[1][i] = 0;
}
}
void generateElectronAtLocation(float a0, float r0, int *qs, int *mp, float pos0x[2][n_partd], float pos0y[2][n_partd], float pos0z[2][n_partd],
float pos1x[2][n_partd], float pos1y[2][n_partd], float pos1z[2][n_partd], int q[2][n_partd], int m[2][n_partd], int *nt)
{
int particles = 1024;
float r = n_space * a0;
float step = 2.0f / particles;
for (int i = 0; i < n_partd; i++)
{
// pos0x[0][i] = pos0y[0][i] = pos0z[0][i] = pos1x[0][i] = pos1y[0][i] = pos1z[0][i] = -a0 / 2.0f;
pos0x[0][i] = pos1x[0][i] = 0.f;
pos1x[0][i] = -a0 * 0.005f;
pos0y[0][i] = pos1y[0][i] = 0.f;
pos0z[0][i] = pos1z[0][i] = a0 * -.25; // a0 * -30;
pos0x[1][i] = pos0y[1][i] = pos0z[1][i] = pos1x[1][i] = pos1y[1][i] = pos1z[1][i] = 0;
q[0][i] = qs[0]; // q[1][i] = qs[1];
m[0][i] = mp[0]; // m[1][i] = mp[1];
nt[0] += q[0][i]; // nt[1] += q[1][i];
}
/* backup code
for(int i=0; i < n_partd; ++i){
pos0x[0][i] = pos1x[0][i] = a0 * (n_space_divx - 1) / 2.0 - 1e-5;
pos0y[0][i] = pos1y[0][i] = a0 * (n_space_divy - 1) / 2.0 - 1e-5;
pos0z[0][i] = pos1z[0][i] = a0 * (n_space_divz - 1) / 2.0 - 1e-5;
pos0x[1][i] = pos0y[1][i] = pos0z[1][i] = pos1x[1][i] = pos1y[1][i] = pos1z[1][i] = 0;
q[0][i] = qs[0];
m[0][i] = mp[0];
nt[0] += q[0][i];
}
*/
}
void generateSphere(float a0, float r0, int *qs, int *mp, float pos0x[2][n_partd], float pos0y[2][n_partd], float pos0z[2][n_partd],
float pos1x[2][n_partd], float pos1y[2][n_partd], float pos1z[2][n_partd], int q[2][n_partd], int m[2][n_partd], int *nt)
{
// https://stackoverflow.com/a/16128461
float half_cell = a0 / 2.f;
int particles = n_partd;
float dlong = pi * (3.0f - sqrt(5.0f)), dz = 2.0 / n_partd, lg = 0.0f, z = 1.0f - dz / 2.0f;
for (int i = 0; i < n_partd; ++i)
{
float r = sqrt(1.0 - z * z) * r0;
pos0x[0][i] = pos1x[0][i] = cos(lg) * r; // + half_cell;
pos0y[0][i] = pos1y[0][i] = sin(lg) * r; // + half_cell;
pos0z[0][i] = pos1z[0][i] = z * r0; // + half_cell;
pos0x[1][i] = pos0y[1][i] = pos0z[1][i] = pos1x[1][i] = pos1y[1][i] = pos1z[1][i] = 0;
z -= dz;
lg += dlong;
q[0][i] = qs[0];
m[0][i] = mp[0];
nt[0] += q[0][i];
}
}
void generate2Electrons(float a0, float r0, int *qs, int *mp, float pos0x[2][n_partd], float pos0y[2][n_partd], float pos0z[2][n_partd],
float pos1x[2][n_partd], float pos1y[2][n_partd], float pos1z[2][n_partd], int q[2][n_partd], int m[2][n_partd], int *nt)
{
float half_cell = a0 / 2;
for (int i = 0; i < n_partd; ++i)
{
pos0x[0][i] = pos1x[0][i] = i < n_partd / 2 ? half_cell : -half_cell;
pos0y[0][i] = pos1y[0][i] = half_cell;
pos0z[0][i] = pos1z[0][i] = half_cell;
pos0x[1][i] = pos0y[1][i] = pos0z[1][i] = pos1x[1][i] = pos1y[1][i] = pos1z[1][i] = 0;
q[0][i] = qs[0];
m[0][i] = mp[0];
nt[0] += qs[0];
}
}
void generateParticles(float a0, float r0, int *qs, int *mp, float pos0x[2][n_partd], float pos0y[2][n_partd], float pos0z[2][n_partd],
float pos1x[2][n_partd], float pos1y[2][n_partd], float pos1z[2][n_partd], int q[2][n_partd], int m[2][n_partd], int *nt)
{
// generateLineOfElectrons(a0, r0, qs, mp, pos0x, pos0y, pos0z, pos1x, pos1y, pos1z, q, m, nt);
// generate8ElectronCorners(a0, r0, qs, mp, pos0x, pos0y, pos0z, pos1x, pos1y, pos1z, q, m, nt);
generateElectronAtLocation(a0, r0, qs, mp, pos0x, pos0y, pos0z, pos1x, pos1y, pos1z, q, m, nt);
// generateSphere(a0, r0, qs, mp, pos0x, pos0y, pos0z, pos1x, pos1y, pos1z, q, m, nt);
// generate2Electrons(a0, r0, qs, mp, pos0x, pos0y, pos0z, pos1x, pos1y, pos1z, q, m, nt);
}
void generateEmptyField(float Ee[3][n_space_divz][n_space_divy][n_space_divx], float Be[3][n_space_divz][n_space_divy][n_space_divx])
{
// technically, we don't need to do this because 0 is just blank
for (unsigned int i = 0; i < n_space_divx; i++)
{
// float x = ((float)i - (float)(n_space_divx) / 2.0) / ((float)(n_space_divx));
for (unsigned int j = 0; j < n_space_divy; j++)
{
// float y = ((float)j - (float)(n_space_divy) / 2.0) / ((float)(n_space_divy));
for (unsigned int k = 0; k < n_space_divz; k++)
{
// float z = ((float)k - (float)(n_space_divz) / 2.0) / ((float)(n_space_divz));
// float r = sqrt(x * x + y * y + z * z); unused
Ee[0][k][j][i] = 0; // 1000+i*100;
Ee[1][k][j][i] = 0; // 2000+j*100;
Ee[2][k][j][i] = Ez0; // 3000+k*100;
Be[0][k][j][i] = 0; // z/sqrt(x*x+y*y);
Be[1][k][j][i] = 0; // z/sqrt(x*x+y*y);
Be[2][k][j][i] = Bz0; // 1*z*z+1;
}
}
}
}
void generateStripedEField(float Ee[3][n_space_divz][n_space_divy][n_space_divx], float Be[3][n_space_divz][n_space_divy][n_space_divx])
{
for (unsigned int k = 0; k < n_space_divz; k++)
for (unsigned int j = 0; j < n_space_divy; j++)
for (unsigned int i = 0; i < n_space_divx; i++)
{
Ee[0][k][j][i] = i < 16 ? 1.f : 1e4;
Ee[1][k][j][i] = 0.f;
Ee[2][k][j][i] = 0.f;
Be[0][k][j][i] = Be[1][k][j][i] = Be[2][k][j][i] = 0.f;
}
}
void generateConstantBField(float Ee[3][n_space_divz][n_space_divy][n_space_divx], float Be[3][n_space_divz][n_space_divy][n_space_divx])
{
for (unsigned int k = 0; k < n_space_divz; k++)
for (unsigned int j = 0; j < n_space_divy; j++)
for (unsigned int i = 0; i < n_space_divx; i++)
{
Be[0][k][j][i] = 0.f;
Be[1][k][j][i] = 0.0015f;
Be[2][k][j][i] = 0.f;
}
}
void generateZpinchField(float Ee[3][n_space_divz][n_space_divy][n_space_divx], float Be[3][n_space_divz][n_space_divy][n_space_divx])
{
// radius of z-pinch
double r0 = r0_f * a0;
for ( int i = 0; i < n_space_divx; i++)
{
double x = (i - n_space_divx / 2) * a0;
for ( int j = 0; j < n_space_divy; j++)
{
double y = (j - n_space_divy / 2) * a0;
double r = sqrt(pow(x, 2) + pow(y, 2));
for (unsigned int k = 0; k < n_space_divz; k++)
{
Ee[0][k][j][i] = 0; // 1000+i*100;
Ee[1][k][j][i] = 0; // 2000+j*100;
Ee[2][k][j][i] = Ez0; // 3000+k*100;
if (r > r0)
{
Be[0][k][j][i] = -Btheta0 * y / (r * r) * r0;
Be[1][k][j][i] = Btheta0 * x / (r * r) * r0;
}
else
{
Be[0][k][j][i] = -Btheta0 * y / r0;
Be[1][k][j][i] = Btheta0 * x / r0;
}
Be[2][k][j][i] = Bz0; // 1*z*z+1;
}
}
}
}
void generateField(fields *fi, par *par)
{
// generateEmptyField(fi->Ee, fi->Be);
generateZpinchField(fi->Ee, fi->Be);
// generateStripedEField(Ee, Be);
// generateConstantBField(Ee, Be);
}