forked from gevolution-code/gevolution-1.2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.hpp
685 lines (617 loc) · 25.1 KB
/
tools.hpp
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//////////////////////////
// tools.hpp
//////////////////////////
//
// Collection of analysis tools for gevolution
//
// Author: Julian Adamek (Université de Genève & Observatoire de Paris & Queen Mary University of London)
//
// Last modified: April 2019
//
//////////////////////////
#ifndef TOOLS_HEADER
#define TOOLS_HEADER
#ifndef Cplx
#define Cplx Imag
#endif
#define KTYPE_GRID 0
#define KTYPE_LINEAR 1
using namespace std;
using namespace LATfield2;
#ifdef FFT3D
//////////////////////////
// extractCrossSpectrum
//////////////////////////
// Description:
// generates the cross spectrum for two Fourier images
//
// Arguments:
// fld1FT reference to the first Fourier image for which the cross spectrum should be extracted
// fld2FT reference to the second Fourier image for which the cross spectrum should be extracted
// kbin allocated array that will contain the central k-value for the bins
// power allocated array that will contain the average power in each bin
// kscatter allocated array that will contain the k-scatter for each bin
// pscatter allocated array that will contain the scatter in power for each bin
// occupation allocated array that will count the number of grid points contributing to each bin
// numbin number of bins (minimum size of all arrays)
// ktype flag indicating which definition of momentum to be used
// 0: grid momentum
// 1: linear (default)
// comp1 for component-wise cross spectra, the component for the first field (ignored if negative)
// comp2 for component-wise cross spectra, the component for the second field (ignored if negative)
//
// Returns:
//
//////////////////////////
void extractCrossSpectrum(Field<Cplx> & fld1FT, Field<Cplx> & fld2FT, Real * kbin, Real * power, Real * kscatter, Real * pscatter, int * occupation, const int numbins, const bool deconvolve = true, const int ktype = KTYPE_LINEAR, const int comp1 = -1, const int comp2 = -1)
{
int i, weight;
const int linesize = fld1FT.lattice().size(1);
Real * typek2;
Real * sinc;
Real k2max, k2, s;
rKSite k(fld1FT.lattice());
Cplx p;
typek2 = (Real *) malloc(linesize * sizeof(Real));
sinc = (Real *) malloc(linesize * sizeof(Real));
if (ktype == KTYPE_GRID)
{
for (i = 0; i < linesize; i++)
{
typek2[i] = 2. * (Real) linesize * sin(M_PI * (Real) i / (Real) linesize);
typek2[i] *= typek2[i];
}
}
else
{
for (i = 0; i <= linesize/2; i++)
{
typek2[i] = 2. * M_PI * (Real) i;
typek2[i] *= typek2[i];
}
for (; i < linesize; i++)
{
typek2[i] = 2. * M_PI * (Real) (linesize-i);
typek2[i] *= typek2[i];
}
}
sinc[0] = 1.;
if (deconvolve)
{
for (i = 1; i <= linesize / 2; i++)
{
sinc[i] = sin(M_PI * (float) i / (float) linesize) * (float) linesize / (M_PI * (float) i);
}
}
else
{
for (i = 1; i <= linesize / 2; i++)
{
sinc[i] = 1.;
}
}
for (; i < linesize; i++)
{
sinc[i] = sinc[linesize-i];
}
k2max = 3. * typek2[linesize/2];
for (i = 0; i < numbins; i++)
{
kbin[i] = 0.;
power[i] = 0.;
kscatter[i] = 0.;
pscatter[i] = 0.;
occupation[i] = 0;
}
for (k.first(); k.test(); k.next())
{
if (k.coord(0) == 0 && k.coord(1) == 0 && k.coord(2) == 0)
continue;
else if (k.coord(0) == 0)
weight = 1;
else if ((k.coord(0) == linesize/2) && (linesize % 2 == 0))
weight = 1;
else
weight = 2;
k2 = typek2[k.coord(0)] + typek2[k.coord(1)] + typek2[k.coord(2)];
s = sinc[k.coord(0)] * sinc[k.coord(1)] * sinc[k.coord(2)];
s *= s;
if (comp1 >= 0 && comp2 >= 0 && comp1 < fld1FT.components() && comp2 < fld2FT.components())
{
p = fld1FT(k, comp1) * fld2FT(k, comp2).conj();
}
else if (fld1FT.symmetry() == LATfield2::symmetric)
{
p = fld1FT(k, 0, 1) * fld2FT(k, 0, 1).conj();
p += fld1FT(k, 0, 2) * fld2FT(k, 0, 2).conj();
p += fld1FT(k, 1, 2) * fld2FT(k, 1, 2).conj();
p *= 2.;
p += fld1FT(k, 0, 0) * fld2FT(k, 0, 0).conj();
p += fld1FT(k, 1, 1) * fld2FT(k, 1, 1).conj();
p += fld1FT(k, 2, 2) * fld2FT(k, 2, 2).conj();
}
else
{
p = Cplx(0., 0.);
for (i = 0; i < fld1FT.components(); i++)
p += fld1FT(k, i) * fld2FT(k, i).conj();
}
i = (int) floor((double) ((Real) numbins * sqrt(k2 / k2max)));
if (i < numbins)
{
kbin[i] += weight * sqrt(k2);
kscatter[i] += weight * k2;
power[i] += weight * p.real() * k2 * sqrt(k2) / s;
pscatter[i] += weight * p.real() * p.real() * k2 * k2 * k2 / s / s;
occupation[i] += weight;
}
}
free(typek2);
free(sinc);
if (parallel.isRoot())
{
#ifdef SINGLE
MPI_Reduce(MPI_IN_PLACE, (void *) kbin, numbins, MPI_FLOAT, MPI_SUM, 0, parallel.lat_world_comm());
MPI_Reduce(MPI_IN_PLACE, (void *) kscatter, numbins, MPI_FLOAT, MPI_SUM, 0, parallel.lat_world_comm());
MPI_Reduce(MPI_IN_PLACE, (void *) power, numbins, MPI_FLOAT, MPI_SUM, 0, parallel.lat_world_comm());
MPI_Reduce(MPI_IN_PLACE, (void *) pscatter, numbins, MPI_FLOAT, MPI_SUM, 0, parallel.lat_world_comm());
#else
MPI_Reduce(MPI_IN_PLACE, (void *) kbin, numbins, MPI_DOUBLE, MPI_SUM, 0, parallel.lat_world_comm());
MPI_Reduce(MPI_IN_PLACE, (void *) kscatter, numbins, MPI_DOUBLE, MPI_SUM, 0, parallel.lat_world_comm());
MPI_Reduce(MPI_IN_PLACE, (void *) power, numbins, MPI_DOUBLE, MPI_SUM, 0, parallel.lat_world_comm());
MPI_Reduce(MPI_IN_PLACE, (void *) pscatter, numbins, MPI_DOUBLE, MPI_SUM, 0, parallel.lat_world_comm());
#endif
MPI_Reduce(MPI_IN_PLACE, (void *) occupation, numbins, MPI_INT, MPI_SUM, 0, parallel.lat_world_comm());
for (i = 0; i < numbins; i++)
{
if (occupation[i] > 0)
{
kscatter[i] = sqrt(kscatter[i] * occupation[i] - kbin[i] * kbin[i]) / occupation[i];
if (!isfinite(kscatter[i])) kscatter[i] = 0.;
kbin[i] = kbin[i] / occupation[i];
power[i] /= occupation[i];
pscatter[i] = sqrt(pscatter[i] / occupation[i] - power[i] * power[i]);
if (!isfinite(pscatter[i])) pscatter[i] = 0.;
}
}
}
else
{
#ifdef SINGLE
MPI_Reduce((void *) kbin, NULL, numbins, MPI_FLOAT, MPI_SUM, 0, parallel.lat_world_comm());
MPI_Reduce((void *) kscatter, NULL, numbins, MPI_FLOAT, MPI_SUM, 0, parallel.lat_world_comm());
MPI_Reduce((void *) power, NULL, numbins, MPI_FLOAT, MPI_SUM, 0, parallel.lat_world_comm());
MPI_Reduce((void *) pscatter, NULL, numbins, MPI_FLOAT, MPI_SUM, 0, parallel.lat_world_comm());
#else
MPI_Reduce((void *) kbin, NULL, numbins, MPI_DOUBLE, MPI_SUM, 0, parallel.lat_world_comm());
MPI_Reduce((void *) kscatter, NULL, numbins, MPI_DOUBLE, MPI_SUM, 0, parallel.lat_world_comm());
MPI_Reduce((void *) power, NULL, numbins, MPI_DOUBLE, MPI_SUM, 0, parallel.lat_world_comm());
MPI_Reduce((void *) pscatter, NULL, numbins, MPI_DOUBLE, MPI_SUM, 0, parallel.lat_world_comm());
#endif
MPI_Reduce((void *) occupation, NULL, numbins, MPI_INT, MPI_SUM, 0, parallel.lat_world_comm());
}
}
//////////////////////////
// extractPowerSpectrum
//////////////////////////
// Description:
// generates the power spectrum for a Fourier image
//
// Arguments:
// fldFT reference to the Fourier image for which the power spectrum should be extracted
// kbin allocated array that will contain the central k-value for the bins
// power allocated array that will contain the average power in each bin
// kscatter allocated array that will contain the k-scatter for each bin
// pscatter allocated array that will contain the scatter in power for each bin
// occupation allocated array that will count the number of grid points contributing to each bin
// numbin number of bins (minimum size of all arrays)
// ktype flag indicating which definition of momentum to be used
// 0: grid momentum
// 1: linear (default)
//
// Returns:
//
//////////////////////////
void extractPowerSpectrum(Field<Cplx> & fldFT, Real * kbin, Real * power, Real * kscatter, Real * pscatter, int * occupation, const int numbins, const bool deconvolve = true, const int ktype = KTYPE_LINEAR)
{
extractCrossSpectrum(fldFT, fldFT, kbin, power, kscatter, pscatter, occupation, numbins, deconvolve, ktype);
}
#endif
//////////////////////////
// writePowerSpectrum
//////////////////////////
// Description:
// writes power spectra as tabulated data into ASCII file
//
// Arguments:
// kbin array containing the central values of k for each bin
// power array containing the central values of P(k) for each bin
// kscatter array containing the statistical error on k for each bin
// pscatter array containing the statistical error on P(k) for each bin
// occupation array containing the number of k-modes contributing to each bin
// numbins total number of bins (length of the arrays)
// rescalek unit conversion factor for k
// rescalep unit conversion factor for P(k)
// filename output file name
// description descriptive header
// a scale factor for this spectrum
// z_target target redshift for this output (used only if EXACT_OUTPUT_REDSHIFTS is defined)
//
// Returns:
//
//////////////////////////
void writePowerSpectrum(Real * kbin, Real * power, Real * kscatter, Real * pscatter, int * occupation, const int numbins, const Real rescalek, const Real rescalep, const char * filename, const char * description, double a, const double z_target = -1)
{
if (parallel.isRoot())
{
#ifdef EXACT_OUTPUT_REDSHIFTS
Real * power2 = (Real *) malloc(numbins * sizeof(Real));
for (int i = 0; i < numbins; i++)
power2[i] = power[i]/rescalep;
if (1. / a < z_target + 1.)
{
FILE * infile = fopen(filename, "r");
double weight = 1.;
int count = 0;
if (infile != NULL)
{
fscanf(infile, "%*[^\n]\n");
if (fscanf(infile, "# redshift z=%lf\n", &weight) != 1)
{
cout << " error parsing power spectrum file header for interpolation (EXACT_OUTPUT_REDSHIFTS)" << endl;
weight = 1.;
}
else
{
weight = (weight - z_target) / (1. + weight - 1./a);
fscanf(infile, "%*[^\n]\n");
for (int i = 0; i < numbins; i++)
{
if (occupation[i] > 0)
{
#ifdef SINGLE
if(fscanf(infile, " %*e %e %*e %*e %*d \n", power2+i) != 1)
#else
if(fscanf(infile, " %*e %le %*e %*e %*d \n", power2+i) != 1)
#endif
{
cout << " error parsing power spectrum file data " << i << " for interpolation (EXACT_OUTPUT_REDSHIFTS)" << endl;
break;
}
else count++;
}
}
}
fclose(infile);
for (int i = 0; i < numbins; i++)
power2[i] = (1.-weight)*power2[i] + weight*power[i]/rescalep;
a = 1. / (z_target + 1.);
}
}
#endif // EXACT_OUTPUT_REDSHIFTS
FILE * outfile = fopen(filename, "w");
if (outfile == NULL)
{
cout << " error opening file for power spectrum output!" << endl;
}
else
{
fprintf(outfile, "# %s\n", description);
fprintf(outfile, "# redshift z=%f\n", (1./a)-1.);
fprintf(outfile, "# k Pk sigma(k) sigma(Pk) count\n");
for (int i = 0; i < numbins; i++)
{
if (occupation[i] > 0)
#ifdef EXACT_OUTPUT_REDSHIFTS
fprintf(outfile, " %e %e %e %e %d\n", kbin[i]/rescalek, power2[i], kscatter[i]/rescalek, pscatter[i]/rescalep/ sqrt(occupation[i]), occupation[i]);
#else
fprintf(outfile, " %e %e %e %e %d\n", kbin[i]/rescalek, power[i]/rescalep, kscatter[i]/rescalek, pscatter[i]/rescalep/ sqrt(occupation[i]), occupation[i]);
#endif
}
fclose(outfile);
}
#ifdef EXACT_OUTPUT_REDSHIFTS
free(power2);
#endif
}
}
//////////////////////////
// computeVectorDiagnostics
//////////////////////////
// Description:
// computes some diagnostics for the spin-1 perturbation
//
// Arguments:
// Bi reference to the real-space vector field to analyze
// mdivB will contain the maximum value of the divergence of Bi
// mcurlB will contain the maximum value of the curl of Bi
//
// Returns:
//
//////////////////////////
void computeVectorDiagnostics(Field<Real> & Bi, Real & mdivB, Real & mcurlB)
{
Real b1, b2, b3, b4;
const Real linesize = (Real) Bi.lattice().sizeLocal(0);
Site x(Bi.lattice());
mdivB = 0.;
mcurlB = 0.;
for (x.first(); x.test(); x.next())
{
b1 = fabs((Bi(x,0)-Bi(x-0,0)) + (Bi(x,1)-Bi(x-1,1)) + (Bi(x,2)-Bi(x-2,2))) * linesize;
if (b1 > mdivB) mdivB = b1;
b1 = 0.5 * (Bi(x,0) + Bi(x+0,1) - Bi(x+1,0) - Bi(x,1) + Bi(x+2,0) + Bi(x+0+2,1) - Bi(x+1+2,0) - Bi(x+2,1)) * linesize;
b2 = 0.5 * (Bi(x,0) + Bi(x+0,2) - Bi(x+2,0) - Bi(x,2) + Bi(x+1,0) + Bi(x+0+1,2) - Bi(x+2+1,0) - Bi(x+1,2)) * linesize;
b3 = 0.5 * (Bi(x,2) + Bi(x+2,1) - Bi(x+1,2) - Bi(x,1) + Bi(x+0,2) + Bi(x+2+0,1) - Bi(x+1+0,2) - Bi(x+0,1)) * linesize;
b4 = sqrt(b1 * b1 + b2 * b2 + b3 * b3);
if (b4 > mcurlB) mcurlB = b4;
}
parallel.max<Real>(mdivB);
parallel.max<Real>(mcurlB);
}
//////////////////////////
// computeTensorDiagnostics
//////////////////////////
// Description:
// computes some diagnostics for the spin-2 perturbation
//
// Arguments:
// hij reference to the real-space tensor field to analyze
// mdivh will contain the maximum value of the divergence of hij
// mtraceh will contain the maximum value of the trace of hij
// mnormh will contain the maximum value of the norm of hij
//
// Returns:
//
//////////////////////////
void computeTensorDiagnostics(Field<Real> & hij, Real & mdivh, Real & mtraceh, Real & mnormh)
{
Real d1, d2, d3;
const Real linesize = (Real) hij.lattice().sizeLocal(0);
Site x(hij.lattice());
mdivh = 0.;
mtraceh = 0.;
mnormh = 0.;
for (x.first(); x.test(); x.next())
{
d1 = (hij(x+0, 0, 0) - hij(x, 0, 0) + hij(x, 0, 1) - hij(x-1, 0, 1) + hij(x, 0, 2) - hij(x-2, 0, 2)) * linesize;
d2 = (hij(x+1, 1, 1) - hij(x, 1, 1) + hij(x, 0, 1) - hij(x-0, 0, 1) + hij(x, 1, 2) - hij(x-2, 1, 2)) * linesize;
d3 = (hij(x+2, 2, 2) - hij(x, 2, 2) + hij(x, 0, 2) - hij(x-0, 0, 2) + hij(x, 1, 2) - hij(x-1, 1, 2)) * linesize;
d1 = sqrt(d1 * d1 + d2 * d2 + d3 * d3);
if (d1 > mdivh) mdivh = d1;
d1 = fabs(hij(x, 0, 0) + hij(x, 1, 1) + hij(x, 2, 2));
if (d1 > mtraceh) mtraceh = d1;
d1 = sqrt(hij(x, 0, 0) * hij(x, 0, 0) + 2. * hij(x, 0, 1) * hij(x, 0, 1) + 2. * hij(x, 0, 2)* hij(x, 0, 2) + hij(x, 1, 1) * hij(x, 1, 1) + 2. * hij(x, 1, 2) * hij(x, 1, 2) + hij(x, 2, 2) * hij(x, 2, 2));
if (d1 > mnormh) mnormh = d1;
}
parallel.max<Real>(mdivh);
parallel.max<Real>(mtraceh);
parallel.max<Real>(mnormh);
}
//////////////////////////
// findIntersectingLightcones
//////////////////////////
// Description:
// determines periodic copies of light cone vertex for which the present
// look-back interval may overlap with a given spatial domain
//
// Arguments:
// lightcone reference to structure describing light cone geometry
// outer outer (far) limit of look-back interval
// inner inner (close) limit of look-back interval
// domain array of domain boundaries
// vertex will contain array of relevant vertex locations
//
// Returns:
// number of vertices found
//
//////////////////////////
int findIntersectingLightcones(lightcone_geometry & lightcone, double outer, double inner, double * domain, double vertex[MAX_INTERSECTS][3])
{
int range = (int) ceil(outer) + 1;
int u, v, w, n = 0;
double corner[8][3];
double rdom, dist;
corner[0][0] = domain[0];
corner[0][1] = domain[1];
corner[0][2] = domain[2];
corner[1][0] = domain[3];
corner[1][1] = domain[1];
corner[1][2] = domain[2];
corner[2][0] = domain[0];
corner[2][1] = domain[4];
corner[2][2] = domain[2];
corner[3][0] = domain[3];
corner[3][1] = domain[4];
corner[3][2] = domain[2];
corner[4][0] = domain[0];
corner[4][1] = domain[1];
corner[4][2] = domain[5];
corner[5][0] = domain[3];
corner[5][1] = domain[1];
corner[5][2] = domain[5];
corner[6][0] = domain[0];
corner[6][1] = domain[4];
corner[6][2] = domain[5];
corner[7][0] = domain[3];
corner[7][1] = domain[4];
corner[7][2] = domain[5];
for (u = -range; u <= range; u++)
{
for (v = -range; v <= range; v++)
{
for (w = -range; w <= range; w++)
{
if (n >= MAX_INTERSECTS)
{
cout << COLORTEXT_YELLOW << " /!\\ warning" << COLORTEXT_RESET << ": maximum number of lightcone intersects exceeds MAX_INTERSECTS = " << MAX_INTERSECTS << " for domain (" << domain[0] << ", " << domain[1] << ", " << domain[2] << ") - (" << domain[3] << ", " << domain[4] << ", " << domain[5] << "); some data may be missing in output!" << endl;
return MAX_INTERSECTS;
}
vertex[n][0] = lightcone.vertex[0] + u;
vertex[n][1] = lightcone.vertex[1] + v;
vertex[n][2] = lightcone.vertex[2] + w;
// first, check if domain lies outside outer sphere
if (vertex[n][0] < domain[0])
{
if (vertex[n][1] < domain[1])
{
if (vertex[n][2] < domain[2])
{
if (sqrt((vertex[n][0]-corner[0][0])*(vertex[n][0]-corner[0][0]) + (vertex[n][1]-corner[0][1])*(vertex[n][1]-corner[0][1]) + (vertex[n][2]-corner[0][2])*(vertex[n][2]-corner[0][2])) > outer) continue;
}
else if (vertex[n][2] > domain[5])
{
if (sqrt((vertex[n][0]-corner[4][0])*(vertex[n][0]-corner[4][0]) + (vertex[n][1]-corner[4][1])*(vertex[n][1]-corner[4][1]) + (vertex[n][2]-corner[4][2])*(vertex[n][2]-corner[4][2])) > outer) continue;
}
else if (sqrt((vertex[n][0]-domain[0])*(vertex[n][0]-domain[0]) + (vertex[n][1]-domain[1])*(vertex[n][1]-domain[1])) > outer) continue;
}
else if (vertex[n][1] > domain[4])
{
if (vertex[n][2] < domain[2])
{
if (sqrt((vertex[n][0]-corner[2][0])*(vertex[n][0]-corner[2][0]) + (vertex[n][1]-corner[2][1])*(vertex[n][1]-corner[2][1]) + (vertex[n][2]-corner[2][2])*(vertex[n][2]-corner[2][2])) > outer) continue;
}
else if (vertex[n][2] > domain[5])
{
if (sqrt((vertex[n][0]-corner[6][0])*(vertex[n][0]-corner[6][0]) + (vertex[n][1]-corner[6][1])*(vertex[n][1]-corner[6][1]) + (vertex[n][2]-corner[6][2])*(vertex[n][2]-corner[6][2])) > outer) continue;
}
else if (sqrt((vertex[n][0]-domain[0])*(vertex[n][0]-domain[0]) + (vertex[n][1]-domain[4])*(vertex[n][1]-domain[4])) > outer) continue;
}
else
{
if (vertex[n][2] < domain[2])
{
if (sqrt((vertex[n][0]-domain[0])*(vertex[n][0]-domain[0]) + (vertex[n][2]-domain[2])*(vertex[n][2]-domain[2])) > outer) continue;
}
else if (vertex[n][2] > domain[5])
{
if (sqrt((vertex[n][0]-domain[0])*(vertex[n][0]-domain[0]) + (vertex[n][2]-domain[5])*(vertex[n][2]-domain[5])) > outer) continue;
}
else if (domain[0]-vertex[n][0] > outer) continue;
}
}
else if (vertex[n][0] > domain[3])
{
if (vertex[n][1] < domain[1])
{
if (vertex[n][2] < domain[2])
{
if (sqrt((vertex[n][0]-corner[1][0])*(vertex[n][0]-corner[1][0]) + (vertex[n][1]-corner[1][1])*(vertex[n][1]-corner[1][1]) + (vertex[n][2]-corner[1][2])*(vertex[n][2]-corner[1][2])) > outer) continue;
}
else if (vertex[n][2] > domain[5])
{
if (sqrt((vertex[n][0]-corner[5][0])*(vertex[n][0]-corner[5][0]) + (vertex[n][1]-corner[5][1])*(vertex[n][1]-corner[5][1]) + (vertex[n][2]-corner[5][2])*(vertex[n][2]-corner[5][2])) > outer) continue;
}
else if (sqrt((vertex[n][0]-domain[3])*(vertex[n][0]-domain[3]) + (vertex[n][1]-domain[1])*(vertex[n][1]-domain[1])) > outer) continue;
}
else if (vertex[n][1] > domain[4])
{
if (vertex[n][2] < domain[2])
{
if (sqrt((vertex[n][0]-corner[3][0])*(vertex[n][0]-corner[3][0]) + (vertex[n][1]-corner[3][1])*(vertex[n][1]-corner[3][1]) + (vertex[n][2]-corner[3][2])*(vertex[n][2]-corner[3][2])) > outer) continue;
}
else if (vertex[n][2] > domain[5])
{
if (sqrt((vertex[n][0]-corner[7][0])*(vertex[n][0]-corner[7][0]) + (vertex[n][1]-corner[7][1])*(vertex[n][1]-corner[7][1]) + (vertex[n][2]-corner[7][2])*(vertex[n][2]-corner[7][2])) > outer) continue;
}
else if (sqrt((vertex[n][0]-domain[3])*(vertex[n][0]-domain[3]) + (vertex[n][1]-domain[4])*(vertex[n][1]-domain[4])) > outer) continue;
}
else
{
if (vertex[n][2] < domain[2])
{
if (sqrt((vertex[n][0]-domain[3])*(vertex[n][0]-domain[3]) + (vertex[n][2]-domain[2])*(vertex[n][2]-domain[2])) > outer) continue;
}
else if (vertex[n][2] > domain[5])
{
if (sqrt((vertex[n][0]-domain[3])*(vertex[n][0]-domain[3]) + (vertex[n][2]-domain[5])*(vertex[n][2]-domain[5])) > outer) continue;
}
else if (vertex[n][0]-domain[3] > outer) continue;
}
}
else
{
if (vertex[n][1] < domain[1])
{
if (vertex[n][2] < domain[2])
{
if (sqrt((vertex[n][1]-domain[1])*(vertex[n][1]-domain[1]) + (vertex[n][2]-domain[2])*(vertex[n][2]-domain[2])) > outer) continue;
}
else if (vertex[n][2] > domain[5])
{
if (sqrt((vertex[n][1]-domain[1])*(vertex[n][1]-domain[1]) + (vertex[n][2]-domain[5])*(vertex[n][2]-domain[5])) > outer) continue;
}
else if (domain[1]-vertex[n][1] > outer) continue;
}
else if (vertex[n][1] > domain[4])
{
if (vertex[n][2] < domain[2])
{
if (sqrt((vertex[n][1]-domain[4])*(vertex[n][1]-domain[4]) + (vertex[n][2]-domain[2])*(vertex[n][2]-domain[2])) > outer) continue;
}
else if (vertex[n][2] > domain[5])
{
if (sqrt((vertex[n][1]-domain[4])*(vertex[n][1]-domain[4]) + (vertex[n][2]-domain[5])*(vertex[n][2]-domain[5])) > outer) continue;
}
else if (vertex[n][1]-domain[4] > outer) continue;
}
else if (vertex[n][2]-domain[5] > outer || domain[2]-vertex[n][2] > outer) continue;
}
if (sqrt((corner[0][0]-vertex[n][0])*(corner[0][0]-vertex[n][0]) + (corner[0][1]-vertex[n][1])*(corner[0][1]-vertex[n][1]) + (corner[0][2]-vertex[n][2])*(corner[0][2]-vertex[n][2])) < inner && sqrt((corner[1][0]-vertex[n][0])*(corner[1][0]-vertex[n][0]) + (corner[1][1]-vertex[n][1])*(corner[1][1]-vertex[n][1]) + (corner[1][2]-vertex[n][2])*(corner[1][2]-vertex[n][2])) < inner && sqrt((corner[2][0]-vertex[n][0])*(corner[2][0]-vertex[n][0]) + (corner[2][1]-vertex[n][1])*(corner[2][1]-vertex[n][1]) + (corner[2][2]-vertex[n][2])*(corner[2][2]-vertex[n][2])) < inner && sqrt((corner[3][0]-vertex[n][0])*(corner[3][0]-vertex[n][0]) + (corner[3][1]-vertex[n][1])*(corner[3][1]-vertex[n][1]) + (corner[3][2]-vertex[n][2])*(corner[3][2]-vertex[n][2])) < inner && sqrt((corner[4][0]-vertex[n][0])*(corner[4][0]-vertex[n][0]) + (corner[4][1]-vertex[n][1])*(corner[4][1]-vertex[n][1]) + (corner[4][2]-vertex[n][2])*(corner[4][2]-vertex[n][2])) < inner && sqrt((corner[5][0]-vertex[n][0])*(corner[5][0]-vertex[n][0]) + (corner[5][1]-vertex[n][1])*(corner[5][1]-vertex[n][1]) + (corner[5][2]-vertex[n][2])*(corner[5][2]-vertex[n][2])) < inner && sqrt((corner[6][0]-vertex[n][0])*(corner[6][0]-vertex[n][0]) + (corner[6][1]-vertex[n][1])*(corner[6][1]-vertex[n][1]) + (corner[6][2]-vertex[n][2])*(corner[6][2]-vertex[n][2])) < inner && sqrt((corner[7][0]-vertex[n][0])*(corner[7][0]-vertex[n][0]) + (corner[7][1]-vertex[n][1])*(corner[7][1]-vertex[n][1]) + (corner[7][2]-vertex[n][2])*(corner[7][2]-vertex[n][2])) < inner) continue; // domain lies within inner sphere
rdom = 0.5 * sqrt((domain[3]-domain[0])*(domain[3]-domain[0]) + (domain[4]-domain[1])*(domain[4]-domain[1]) + (domain[5]-domain[2])*(domain[5]-domain[2]));
dist = sqrt((0.5*domain[0]+0.5*domain[3]-vertex[n][0])*(0.5*domain[0]+0.5*domain[3]-vertex[n][0]) + (0.5*domain[1]+0.5*domain[4]-vertex[n][1])*(0.5*domain[1]+0.5*domain[4]-vertex[n][1]) + (0.5*domain[2]+0.5*domain[5]-vertex[n][2])*(0.5*domain[2]+0.5*domain[5]-vertex[n][2]));
if (dist <= rdom) // vertex lies within domain enclosing sphere
{
n++;
continue;
}
if (((0.5*domain[0]+0.5*domain[3]-vertex[n][0])*lightcone.direction[0] + (0.5*domain[1]+0.5*domain[4]-vertex[n][1])*lightcone.direction[1] + (0.5*domain[2]+0.5*domain[5]-vertex[n][2])*lightcone.direction[2]) / dist >= lightcone.opening) // center of domain lies within opening
{
n++;
continue;
}
if (dist > outer && acos(((0.5*domain[0]+0.5*domain[3]-vertex[n][0])*lightcone.direction[0] + (0.5*domain[1]+0.5*domain[4]-vertex[n][1])*lightcone.direction[1] + (0.5*domain[2]+0.5*domain[5]-vertex[n][2])*lightcone.direction[2]) / dist) - acos(lightcone.opening) <= acos((outer*outer + dist*dist - rdom*rdom) / (2. * outer * dist))) // enclosing sphere within opening
{
n++;
continue;
}
if (dist <= outer && acos(((0.5*domain[0]+0.5*domain[3]-vertex[n][0])*lightcone.direction[0] + (0.5*domain[1]+0.5*domain[4]-vertex[n][1])*lightcone.direction[1] + (0.5*domain[2]+0.5*domain[5]-vertex[n][2])*lightcone.direction[2]) / dist) - acos(lightcone.opening) <= asin(rdom / dist)) // enclosing sphere within opening
{
n++;
}
}
}
}
return n;
}
//////////////////////////
// hourMinSec
//////////////////////////
// Description:
// generates formatted output for cpu-time: hh..h:mm:ss.s
//
// Arguments:
// seconds number of seconds
//
// Returns:
// formatted string
//
//////////////////////////
string hourMinSec(double seconds)
{
string output;
char ptr[20];
int h, m, s, f;
h = (int) floor(seconds / 3600.);
seconds -= 3600. * h;
m = (int) floor(seconds / 60.);
seconds -= 60. * m;
s = (int) floor(seconds);
seconds -= s;
f = (int) floor(10. * seconds);
sprintf(ptr, "%d:%02d:%02d.%d", h, m, s, f);
output.reserve(20);
output.assign(ptr);
return output;
}
#endif