-
Notifications
You must be signed in to change notification settings - Fork 37
/
misc.impl.h
executable file
·414 lines (364 loc) · 14.1 KB
/
misc.impl.h
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#ifndef MISC_IMPL_H
#define MISC_IMPL_H
#include "misc.h"
const int VEL_STRUCT_SAMPLES_PER_OUTSTEP = 1000;
const int POS_STRUCT_SAMPLES_PER_OUTSTEP = 1000;
const int FULL_VEL_STRUCT_RES = 3000;
template<bool ifFunct(Cparticle &p)>
void Nmisc::getRandomNeighbrPair(CdataLL *data,gsl_rng *rng,Cparticle **p1,Cparticle **p2){
vector<Cparticle> *ips = data->getParticles();
int numParticles = ips->size();
int ip1 = (int)floor(gsl_rng_uniform(rng)*numParticles);
while (!ifFunct((*ips)[ip1])) {
ip1 = (int)floor(gsl_rng_uniform(rng)*numParticles);
}
vector<Cparticle *> neighbrs;
data->calcNeighbours(neighbrs,(*ips)[ip1]);
int numNeighbrs = neighbrs.size();
int ip2 = (int)floor(gsl_rng_uniform(rng)*numNeighbrs);
int j=0;
while (!ifFunct(*(neighbrs[ip2]))) {
ip2 = (int)floor(gsl_rng_uniform(rng)*numNeighbrs);
j++;
}
*p1 = &((*ips)[ip1]);
*p2 = neighbrs[ip2];
}
template<bool ifFunct(Cparticle &p)>
void Nmisc::getRandomPair(particleContainer &ps,gsl_rng *rng,Cparticle **p1,Cparticle **p2){
int finished = 0;
while (!finished) {
int numParticles = ps.size();
int ip1 = (int)floor(gsl_rng_uniform(rng)*numParticles);
while (!ifFunct(ps[ip1])) {
ip1 = (int)floor(gsl_rng_uniform(rng)*numParticles);
}
const int numBins = int(floor(log((RMAX[0]-RMIN[0])/PSEP)/log(2.0)));
const int bin = (int)floor(gsl_rng_uniform(rng)*numBins);
const double r2 = pow((RMAX[0]-RMIN[0])/pow(2.0,numBins-bin),2);
const double r2_floor = pow((RMAX[0]-RMIN[0])/pow(2.0,numBins-bin+0.5),2);
const double r2_ceil = pow((RMAX[0]-RMIN[0])/pow(2.0,numBins-bin-0.5),2);
vector<Cparticle *> candidates;
for (int j=0;j<numParticles;j++) {
double thisR2 = len2(ps[j].r-ps[ip1].r);
if ((thisR2>=r2_floor)&&(thisR2<=r2_ceil)&&ifFunct(ps[j])) {
candidates.push_back(&(ps[j]));
}
}
int ip2;
if (!candidates.empty()) {
ip2 = (int)floor(gsl_rng_uniform(rng)*candidates.size());
finished = 1;
} else {
cout <<"No candidates!: r = "<<ps[ip1].r<<" r_floor = "<<sqrt(r2_floor)<<" r2_ceil = "<<sqrt(r2_ceil)<<endl;
continue;
}
*p1 = &(ps[ip1]);
*p2 = candidates[ip2];
}
}
inline void calcVelocityMode(Cparticle &p,CglobalVars &g,vectInt &coords) {
vect refr=2.0*p.r/(RMAX[1]-RMIN[1]);
double lenCentre = len(refr);
refr = (refr+1.0)/2.0;
//double v2m2rd = 1.0/(RMAX[0]-RMIN[0])*len2(p.v)*pow(p.mass,2)/p.dens;
double v2m2rd;
if (PERIODIC[0]) {
v2m2rd = len2(p.v)*pow(p.mass,2)/((RMAX[1]-RMIN[1])*p.dens);
} else {
if (lenCentre<1) {
v2m2rd = 0.5*(1.0-cos(PI*(lenCentre+1)))*(0.5*p.mass*len2(p.v)-(g.eKE/g.nSph))*p.mass/(0.5*(RMAX[1]-RMIN[1])*p.dens);
} else {
v2m2rd = 0.0;
}
}
//v2m2rd = len2(p.v)*pow(p.mass,2)/((RMAX[1]-RMIN[1])*p.dens);
//double v2m2rd = pow(p.mass*p.v[1],2)/((RMAX[1]-RMIN[1])*p.dens);
double loc = 2.0*PI*dot(coords,refr);
//cout <<"spectrum with v = "<<p.v<<"mass = "<<p.mass<<" dens = "<<p.dens<<" refr = "<<refr<<endl;
g.tmpArray[0] += v2m2rd*cos(loc);
g.tmpArray[1] += v2m2rd*sin(loc);
}
template<bool ifFunct(Cparticle &p)>
void Nmisc::calcVelocityStructure(CdataLL *data,gsl_rng *rng,Cio_data_vtk *io) {
string filename = "VelStruct";
//open new output file
char strTimestep[20];
sprintf(strTimestep,"%7.7d",data->globals.outstep);
string outputFN = io->getFilename()+filename+strTimestep+".dat";
ofstream newFile(outputFN.c_str(),ios::out|ios::trunc);
if (data->globals.mpiRank==0) cout <<"calculating velocity structure info for neighbours. Putting into file: "<<outputFN<<endl;
newFile <<"#dvx dvy drx dry"<<endl;
Cparticle *p1;
Cparticle *p2;
for (int i=0;i<VEL_STRUCT_SAMPLES_PER_OUTSTEP;i++) {
getRandomNeighbrPair<ifFunct>(data,rng,&p1,&p2);
//getRandomPair<ifFunct>(*ips,rng,&p1,&p2);
vect dr = p1->r-p2->r;
vect dv = p1->v-p2->v;
newFile <<dv[0]<<' '<<dv[1]<<' '<<dr[0]<<' '<<dr[1]<<endl;
}
newFile.close();
}
template<bool ifFunct(Cparticle &p)>
void Nmisc::calcFullVelocityStructure(CdataLL *data,Cio_data_vtk *io) {
string filename = "VelStructFull";
//open new output file
if (data->globals.mpiRank==0) cout <<"calculating full velocity structure info"<<endl;
char strTimestep[20];
sprintf(strTimestep,"%7.7d",data->globals.outstep);
string outputFN = io->getFilename()+filename+strTimestep+".dat";
ofstream newFile(outputFN.c_str(),ios::out|ios::trunc);
newFile <<"#r v v2 v3 v12"<<endl;
vector<double> resv(FULL_VEL_STRUCT_RES,0);
vector<double> resv2(FULL_VEL_STRUCT_RES,0);
vector<double> resv3(FULL_VEL_STRUCT_RES,0);
vector<double> resv12(FULL_VEL_STRUCT_RES,0);
vector<unsigned int> counter(FULL_VEL_STRUCT_RES,0);
vector<Cparticle> *ips = data->getParticles();
int numParticles = ips->size();
for (int i=0;i<numParticles;i++) {
Cparticle *p1 = &((*ips)[i]);
if (!ifFunct(*p1)) continue;
for (int j=0;j<numParticles;j++) {
Cparticle *p2 = &((*ips)[j]);
if (!ifFunct(*p2)) continue;
vect drVect = p1->r-p2->r;
double dr = len(drVect);
double dv = abs(dot(p1->v-p2->v,drVect)/dr);
if (dr<(RMAX[1]-RMIN[1])) {
double dv2 = pow(dv,2);
double dv3 = dv2*dv;
double dv12 = dv3*pow(dv2,2);
int index = int(dr*(FULL_VEL_STRUCT_RES/(RMAX[1]-RMIN[1]))+0.5);
counter[index]++;
resv[index] += dv;
resv2[index] += dv2;
resv3[index] += dv3;
resv12[index] += dv12;
}
}
}
if (data->globals.mpiRank==0) cout <<"writing full velocity structure info to file: "<<outputFN<<endl;
for (int i=0;i<FULL_VEL_STRUCT_RES;i++) {
if (counter[i]>0) {
newFile <<i*((RMAX[1]-RMIN[1])/FULL_VEL_STRUCT_RES)<<' '<<resv[i]/counter[i]<<' '<<resv2[i]/counter[i]<<' '<<resv3[i]/counter[i]<<' '<<resv12[i]/counter[i]<<endl;
}
}
newFile.close();
}
template<bool ifFunct(Cparticle &p)>
void Nmisc::calcPositionStructure(CdataLL *data,gsl_rng *rng,Cio_data_vtk *io) {
//open new output file
string filename = "PosStruct";
char strTimestep[20];
sprintf(strTimestep,"%7.7d",data->globals.outstep);
string outputFN = io->getFilename()+filename+strTimestep+".dat";
ofstream newFile(outputFN.c_str(),ios::out|ios::trunc);
newFile <<"# drx dry"<<endl;
Cparticle *p1;
Cparticle *p2;
for (int i=0;i<POS_STRUCT_SAMPLES_PER_OUTSTEP;i++) {
getRandomNeighbrPair<ifFunct>(data,rng,&p1,&p2);
vect dr = p2->r-p1->r;
//TODO: this is 2D specific!!
newFile <<dr[0]<<' '<<dr[1]<<endl;
}
newFile.close();
}
template<bool ifFunct(Cparticle &p)>
Array<myComplex,NDIM> *Nmisc::calcVelocitySpectrum(CdataLL *data, const int numModes) {
const int numModes2 = int(pow(double(numModes),2));
Array<myComplex,NDIM> *modes = new Array<myComplex,NDIM>(numModes);
*modes = 0;
for (Array<myComplex,NDIM>::iterator i=modes->begin();i!=modes->end();i++) {
vectInt coords = i.position();
int mag2 = int(len2(coords));
if (mag2<numModes2) {
data->globals.tmpArray[0] = 0;
data->globals.tmpArray[1] = 0;
data->traverse<vectInt,calcVelocityMode,ifFunct>(coords);
*i = myComplex(data->globals.tmpArray[0],data->globals.tmpArray[1]);
//*i = sqrt(pow(data->globals.tmpArray[0],2) + pow(data->globals.tmpArray[1],2));
//*i = sqrt(pow(data->globals.tmpArray[0],2));
}
}
return modes;
}
template<bool ifFunct(Cparticle &p)>
void Nmisc::outputVelocitySpectrum(CdataLL *data,Cio_data_vtk *io) {
const int numModes = int(0.4*(RMAX[1]-RMIN[1])/PSEP);
Array<myComplex,NDIM> *modes = calcVelocitySpectrum<ifFunct>(data,numModes);
vector<double> oneD(numModes,0);
vector<int> nums(numModes,0);
for (Array<myComplex,NDIM>::iterator i=modes->begin();i!=modes->end();i++) {
vectInt coords = i.position();
int mag = int(len(coords));
if (mag<numModes) {
nums[mag] += 1;
oneD[mag] += abs(*i);
}
}
vector<int>::iterator inum = nums.begin();
for (vector<double>::iterator i=oneD.begin();i!=oneD.end();i++) {
*i /= *inum;
inum++;
}
ofstream fo;
string outputFN = "VelSpec";
char strTimestep[20];
sprintf(strTimestep,"%7.7d",data->globals.outstep);
string outputFN_1D = io->getFilename()+outputFN+"1D"+strTimestep+".dat";
string outputFN_2D = io->getFilename()+outputFN+"2D"+strTimestep+".dat";
cout <<"writing 1D spectrum to file: "<<outputFN_1D<<endl;
fo.open(outputFN_1D.c_str(),ios::out|ios::trunc);
fo << "# mode 1DSpectrum"<<endl;
int n = oneD.size();
for (int i=0;i<n;i++) {
fo<<i<<' '<<oneD[i]<<endl;
}
fo.close();
#ifdef _2D_
cout <<"writing 2D spectrum to file: "<<outputFN_2D<<endl;
fo.open(outputFN_2D.c_str(),ios::out|ios::trunc);
fo << "# "<<numModes<<" x "<<numModes<<" matrix"<<endl;
for (int i=0;i<numModes;i++) {
for (int j=0;j<numModes;j++) {
fo<<' '<<real((*modes)(i,j));
}
fo<<endl;
}
for (int i=0;i<numModes;i++) {
for (int j=0;j<numModes;j++) {
fo<<' '<<imag((*modes)(i,j));
}
fo<<endl;
}
fo.close();
#endif
delete modes;
}
template<double specFunct(Cparticle &p,CglobalVars &g)>
inline void calcMode(Cparticle &p,CglobalVars &g,vectInt &coords) {
vect refr=2.0*p.r/(RMAX[1]-RMIN[1]);
double lenCentre = len(refr);
refr = (refr+1.0)/2.0;
//double v2m2rd = 1.0/(RMAX[0]-RMIN[0])*len2(p.v)*pow(p.mass,2)/p.dens;
double v2m2rd;
if (PERIODIC[0]) {
v2m2rd = 2.0*specFunct(p,g)*p.mass/((RMAX[1]-RMIN[1])*p.dens);
//v2m2rd = len2(p.v)*pow(p.mass,2)/((RMAX[1]-RMIN[1])*p.dens);
} else {
if (lenCentre<1) {
v2m2rd = (1.0-cos(PI*(lenCentre+1)))*specFunct(p,g)*p.mass/((RMAX[1]-RMIN[1])*p.dens);
//v2m2rd = 0.5*(1.0-cos(PI*(lenCentre+1)))*(0.5*p.mass*len2(p.v)-(g.eKE/g.nSph))*p.mass/(0.5*(RMAX[1]-RMIN[1])*p.dens);
} else {
v2m2rd = 0.0;
}
}
//v2m2rd = len2(p.v)*pow(p.mass,2)/((RMAX[1]-RMIN[1])*p.dens);
//double v2m2rd = pow(p.mass*p.v[1],2)/((RMAX[1]-RMIN[1])*p.dens);
double loc = 2.0*PI*dot(coords,refr);
//cout <<p.tag<<" ";
//cout <<"spectrum with v = "<<p.v<<"mass = "<<p.mass<<" dens = "<<p.dens<<" refr = "<<refr<<endl;
g.tmpArray[0] += v2m2rd*cos(loc);
g.tmpArray[1] += v2m2rd*sin(loc);
}
template<bool ifFunct(Cparticle &p),double specFunct(Cparticle &p,CglobalVars &g)>
Array<myComplex,NDIM> *Nmisc::calcSpectrum(CdataLL *data, const int numModes) {
vectInt numModesVect = numModes;
for (int i=1;i<NDIM;i++) {
numModesVect[i] = numModesVect[i]*2-1;
}
vectInt lbounds = 0;
for (int i=1;i<NDIM;i++) {
lbounds[i] = -numModes+1;
}
const int numModes2 = int(pow(double(numModes),2));
Array<myComplex,NDIM> *modes = new Array<myComplex,NDIM>(lbounds,numModesVect);
*modes = 0;
for (Array<myComplex,NDIM>::iterator i=modes->begin();i!=modes->end();i++) {
vectInt coords = i.position();
int mag2 = int(len2(coords));
if (mag2<numModes2) {
//cout << "calculating mode = "<<coords<<endl;
data->globals.tmpArray[0] = 0;
data->globals.tmpArray[1] = 0;
data->traverse<vectInt,calcMode<specFunct>,ifFunct>(coords);
*i = myComplex(data->globals.tmpArray[0],data->globals.tmpArray[1]);
//*i = sqrt(pow(data->globals.tmpArray[0],2) + pow(data->globals.tmpArray[1],2));
//*i = sqrt(pow(data->globals.tmpArray[0],2));
}
}
return modes;
}
template<bool ifFunct(Cparticle &p),void specFunct(Cparticle &p,CglobalVars &g,vectInt &coords)>
void Nmisc::setSpectrum(CdataLL *data, const int numModes) {
//const vectInt numModesVect = numModes*2-1;
//const vectInt lbounds = -numModes+1;
const vectInt numModesVect = numModes;
const vectInt lbounds = 0;
const int numModes2 = int(pow(double(numModes),2));
Array<myComplex,NDIM> *modes = new Array<myComplex,NDIM>(lbounds,numModesVect);
for (Array<myComplex,NDIM>::iterator i=modes->begin();i!=modes->end();i++) {
vectInt coords = i.position();
int mag2 = int(len2(coords));
if (mag2<numModes2) {
//cout << "setting mode = "<<coords<<endl;
data->traverse<vectInt,specFunct,ifFunct>(coords);
}
}
}
template<bool ifFunct(Cparticle &p),double specFunct(Cparticle &p,CglobalVars &g)>
void Nmisc::outputSpectrum(CdataLL *data,Cio_data_vtk *io,string name) {
const int numModes = int(0.4*(RMAX[1]-RMIN[1])/PSEP);
Array<myComplex,NDIM> *modes = calcSpectrum<ifFunct,specFunct>(data,numModes);
vector<double> oneD(numModes,0);
vector<int> nums(numModes,0);
for (Array<myComplex,NDIM>::iterator i=modes->begin();i!=modes->end();i++) {
vectInt coords = i.position();
int mag = int(len(coords));
if (mag<numModes) {
nums[mag] += 1;
oneD[mag] += abs(*i);
}
}
vector<int>::iterator inum = nums.begin();
for (vector<double>::iterator i=oneD.begin();i!=oneD.end();i++) {
*i /= *inum;
inum++;
}
ofstream fo;
char strTimestep[20];
sprintf(strTimestep,"%7.7d",data->globals.outstep);
string outputFN_1D = io->getFilename()+name+"1D"+strTimestep+".dat";
string outputFN_2D = io->getFilename()+name+"2D"+strTimestep+".dat";
cout <<"writing 1D spectrum to file: "<<outputFN_1D<<endl;
fo.open(outputFN_1D.c_str(),ios::out|ios::trunc);
fo << "# mode 1DSpectrum"<<endl;
int n = oneD.size();
for (int i=0;i<n;i++) {
fo<<i<<' '<<oneD[i]<<endl;
}
fo.close();
#ifdef _2D_
cout <<"writing 2D spectrum to file: "<<outputFN_2D<<endl;
fo.open(outputFN_2D.c_str(),ios::out|ios::trunc);
fo << "# "<<numModes<<" x "<<numModes<<" matrix"<<endl;
for (int i=0;i<numModes;i++) {
for (int j=-numModes+1;j<numModes;j++) {
fo<<' '<<real((*modes)(i,j));
}
fo<<endl;
}
for (int i=0;i<numModes;i++) {
for (int j=-numModes+1;j<numModes;j++) {
fo<<' '<<imag((*modes)(i,j));
}
fo<<endl;
}
fo.close();
#endif
delete modes;
}
#endif