-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleSampleViscous.cpp
More file actions
1729 lines (1494 loc) · 61.4 KB
/
ParticleSampleViscous.cpp
File metadata and controls
1729 lines (1494 loc) · 61.4 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
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This file is a part of runjam <https://github.com/idthic/runjam>.
Copyright (C) 2014-2020, Koichi Murase <myoga.murase at gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA */
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cfloat>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iterator>
#include <chrono>
#include <ksh/integrator.hpp>
#include <util.hpp>
#include "ParticleSampleViscous.hpp"
namespace idt {
namespace runjam {
namespace {
// 元々の hydrojet コードの値 (将来的に更新するべき)
static const double hbarc_GeVfm = 0.197327053;
// sqrt(pi/2)
static const double SQRT_TANGENT_ASYMPTOTE = M_SQRT2 / M_2_SQRTPI;
//---------------------------------------------------------------------------
// settings
// // double facranmax = 1.6;
// // double facranmax = 1.7;//06/28/2010, lower switching T, larger radial flow
// // double facranmax = 1.8;//08/27/2019, LHC, larger radial flow
// static const double momentumRandomMaxFactor = 1.8;
// static const double maximalMomentum = 6.0e3 / hbarc_MeVfm;
//di = 10;
static const int CONST_NumberOfBisectionIteration = 20; // di = 20;
// freezeout を実行する温度の最低を指定します。
static const double CONST_FreezeoutSkipTemperature = 0.01; // unit: [/fm]
static const double CONST_ENERGY_MAX_FACTOR = 100.0;
//! @def CFInterpolationType
//! - 1 Lagrange polynomials と局所 cubic spline の組合せ
//! - 2 局所 7次 Langrange polynomials
#define CFInterpolationType 1
//! @def ParticleSampleViscous20150330_InterpolateUCDF
//! UCDF を計算する際に補間テーブルを用いる事を指定します。
//!
//! (これを指定しても、そう速くならない。
//! テーブル生成も、補間も時間はかかっていない様だ。
//! 別の所に律速段階があるという事か。)
#define ParticleSampleViscous20150330_InterpolateUCDF
//---------------------------------------------------------------------------
// utilities
// 反変ベクトルを boost します。
// 共変ベクトルを逆 boost します。
void LorentzBoost(double* p, double const* u) {
double const p0_ = u[0] * p[0] + u[1] * p[1] + u[2] * p[2] + u[3] * p[3];
double const fac = (p0_ + p[0]) / (1.0 + u[0]);
p[0] = p0_;
p[1] += fac*u[1];
p[2] += fac*u[2];
p[3] += fac*u[3];
}
//-----------------------------------------------------------------------------
// test implementation for viscous case
namespace interpolation {
// cooper-frye integration without chemical potential
class CFIntegrator {
public:
double bmass;
double sign;
double xsig;
double vsig;
void calculateIntegrandBulk(double t, double& idealPart, double& viscousPart) const {
double const tantt = std::tan(t * t);
double const x = tantt + this->xsig;
if (x >= CONST_ENERGY_MAX_FACTOR) {
idealPart = 0.0, viscousPart = 0.0;
return;
}
double const jacob = 2 * t * (tantt * tantt + 1);
// f_0(x), \delta f(x)
double const bp2 = x * x - bmass * bmass;
double const exp_ = std::exp(x);
double const f0 = 1.0 / (exp_ - sign);
double const delf = f0 * f0 * exp_ * bp2;
// bE*bp
double const pE = x * std::sqrt(bp2);
idealPart = jacob * pE * f0;
viscousPart = jacob * pE * delf;
}
void integrateBulk(double& ideal, double& viscous) const {
typedef kashiwa::integrator_detail::GaussLegendre<100> gl_nodes;
double const lower = 0.0;
double const upper = SQRT_TANGENT_ASYMPTOTE;
double const center = 0.5 * (upper + lower);
double const dxds = 0.5 * (upper - lower);
ideal = 0.0, viscous = 0.0;
for (std::size_t i = 0; i < gl_nodes::data_size; i++) {
double const s = gl_nodes::data[i].t;
double const w = gl_nodes::data[i].w;
double i1, v1;
this->calculateIntegrandBulk(center + dxds * s, i1, v1);
double i2, v2;
this->calculateIntegrandBulk(center - dxds * s, i2, v2);
ideal += w * (i1 + i2);
viscous += w * (v1 + v2);
}
ideal *= dxds;
viscous *= dxds;
}
void calculateIntegrandSurface(double t, double& idealPart, double& viscousPart) const {
double const tantt = std::tan(t * t);
double const x = tantt + this->xsig;
if (x >= CONST_ENERGY_MAX_FACTOR) {
idealPart = 0.0, viscousPart = 0.0;
return;
}
double const jacob = 2 * t * (tantt * tantt + 1);
// f_0(x), \delta f(x)
double const bp2 = x * x - bmass * bmass;
double const exp_ = std::exp(x);
double const f0 = 1.0 / (exp_ - sign);
double const delf = f0 * f0 * exp_ * bp2;
// (x*vsig-bp)^2
double const bp = std::sqrt(bp2);
double const prod = x * vsig - bp;
double const prod2 = prod * prod;
idealPart = jacob * prod2 * f0 ;
viscousPart = jacob * prod2 * delf;
}
void integrateSurface(double& ideal, double& viscous) const {
typedef kashiwa::integrator_detail::GaussLegendre<100> gl_nodes;
double const lower = 0.0;
double const upper = SQRT_TANGENT_ASYMPTOTE;
double const center = 0.5 * (upper + lower);
double const dxds = 0.5 * (upper - lower);
ideal = 0.0, viscous = 0.0;
for (std::size_t i = 0; i < gl_nodes::data_size; i++) {
double const s = gl_nodes::data[i].t;
double const w = gl_nodes::data[i].w;
double i1, v1;
this->calculateIntegrandSurface(center + dxds * s, i1, v1);
double i2, v2;
this->calculateIntegrandSurface(center - dxds * s, i2, v2);
ideal += w * (i1 + i2);
viscous += w * (v1 + v2);
}
ideal *= dxds;
viscous *= dxds;
}
// xsig = bmass + tan(t*t)
void integrateSurfaceByParamTsig(double t, double& ipart, double& vpart) {
if (t > SQRT_TANGENT_ASYMPTOTE * (1.0 - DBL_EPSILON)) {
// avoid tan(t^2) jump error
ipart = 0.0;
vpart = 0.0;
} else {
double const tantt = std::tan(t * t);
this->xsig = tantt + this->bmass;
this->vsig = std::sqrt(1.0 - (bmass / xsig) * (bmass / xsig));
this->integrateSurface(ipart, vpart);
}
}
//-----------------------------------------------------------------------
// E^2, pE, p^2
void calculateIntegrandMoment(double t, double* buff) const {
double const tantt = std::tan(t * t);
double const x = tantt + this->xsig;
if (x >= CONST_ENERGY_MAX_FACTOR) {
std::fill(buff, buff + 6, 0.0);
return;
}
double const jacob = 2 * t * (tantt * tantt + 1);
// f_0(x), \delta f(x)
double const bp2 = x * x - bmass * bmass;
double const exp_ = std::exp(x);
double const f0 = 1.0 / (exp_ - sign);
double const delf = f0 * f0 * exp_ * bp2;
double const _xbp = x * std::sqrt(bp2);
double const _x2 = x * x;
buff[0] = jacob * f0 * _xbp; // f0 E p
buff[1] = jacob * delf * _xbp; // delf E p
buff[2] = jacob * f0 * _x2 ; // f0 E^2
buff[3] = jacob * delf * _x2 ; // delf E^2
buff[4] = jacob * f0 * bp2 ; // f0 p^2
buff[5] = jacob * delf * bp2 ; // delf p^2
}
void integrateMoment(double* buff) const {
typedef kashiwa::integrator_detail::GaussLegendre<100> gl_nodes;
double const lower = 0.0;
double const upper = SQRT_TANGENT_ASYMPTOTE;
double const center = 0.5 * (upper + lower);
double const dxds = 0.5 * (upper - lower);
std::fill(buff, buff + 6, 0.0);
for (std::size_t i = 0; i < gl_nodes::data_size; i++) {
double const s = gl_nodes::data[i].t;
double const w = gl_nodes::data[i].w;
double buffL[6], buffR[6];
this->calculateIntegrandMoment(center + dxds * s, buffL);
this->calculateIntegrandMoment(center - dxds * s, buffR);
for (int k = 0; k < 6; k++)
buff[k] += w * (buffL[k] + buffR[k]);
}
for (int k = 0; k < 6; k++) buff[k] *= dxds;
}
void integrateMomentByParamTsig(double t, double* buff) {
if (t > SQRT_TANGENT_ASYMPTOTE * (1.0 - DBL_EPSILON)) {
// avoid tan(t^2) jump error
std::fill(buff, buff + 6, 0.0);
} else {
double const tantt = std::tan(t * t);
this->xsig = tantt + this->bmass;
this->integrateMoment(buff);
}
}
};
template<int ORDER = 30>
class LagrangeInterpolation {
public:
typedef double interpolation_data[ORDER];
private:
double domain_begin;
double domain_end ;
private:
//! sampling points of Lagrange interpolation
std::vector<double> nodes;
void initializeNodes() {
this->nodes.resize(ORDER, 0.0);
// Chebychev nodes
{
for (int i = 0; i < ORDER; i++) nodes[i] = std::cos(M_PI - (0.5 * M_PI / ORDER) * (2 * i + 1));
double const scale = 1.0 / nodes[ORDER - 1];
for (int i = 0; i < ORDER; i++) nodes[i] *= scale;
}
// Gauss-Legendre points (Legendre nodes)
// {
// typedef kashiwa::integrator_detail::GaussLegendre<ORDER> gl_interp;
// double const scale = 1.0 / gl_interp::data[gl_interp::data_size - 1].t;
// for (int i = 0; i < gl_interp::data_size; i++) {
// double const s = gl_interp::data[i].t * scale;
// nodes[gl_interp::order / 2 + i] = +s;
// nodes[gl_interp::data_size - 1 - i] = -s;
// }
// }
// rescale
double const center = 0.5 * (domain_end + domain_begin);
double const half = 0.5 * (domain_end - domain_begin);
for (int i = 0; i < ORDER; i++) nodes[i] = center + half * nodes[i];
}
public:
void initialize(double domainBegin = 0.0, double domainEnd = SQRT_TANGENT_ASYMPTOTE) {
this->domain_begin = domainBegin;
this->domain_end = domainEnd;
this->initializeNodes();
}
private:
void calculateNormalization(double (&normalization)[ORDER]) const {
std::fill(normalization, normalization + ORDER, 1.0);
for (int i = 0; i < ORDER; i++) {
for (int j = i + 1; j < ORDER; j++) {
double const primary = 1.0 / (nodes[i] - nodes[j]);
normalization[i] *= +primary;
normalization[j] *= -primary;
}
}
}
public:
template<typename F>
void initializeData(int count, interpolation_data* data, F const& functor) const {
double normalization[ORDER];
this->calculateNormalization(normalization);
std::vector<double> buff;
buff.resize(count, 0.0);
for (int is = 0; is < ORDER; is++) {
functor(&buff[0], nodes[is]);
for (int k = 0; k < count; k++)
data[k][is] = buff[k] * normalization[is];
}
}
public:
LagrangeInterpolation(double domainBegin = 0.0, double domainEnd = SQRT_TANGENT_ASYMPTOTE) {
this->initialize(domainBegin, domainEnd);
}
private:
void checkDomain(double tsig) const {
const double margin = 1e-10;
if (tsig < domain_begin - margin || domain_end + margin < tsig) {
std::fprintf(
stderr, "ParticleSampleViscous.cpp(LagrangeInterpolation): out of domain (tsig=%g not in [%g, %g])\n",
tsig, domain_begin, domain_end);
std::exit(EXIT_FAILURE);
}
}
public:
// 複数の関数の同一点 tsig の上での補間値を得る。
// 複数の interpolation_data を受け取り複数の double を返す。
void interpolate(int count, double* result, interpolation_data const* data, double tsig) const {
this->checkDomain(tsig);
std::fill(result, result + count, 0.0);
double accumulated = 1.0;
for (int i = 0; i < ORDER; i++) {
double const primary = tsig - nodes[i];
for (int k = 0; k < count; k++)
result[k] = result[k] * primary + accumulated * data[k][i];
accumulated *= primary;
}
}
};
// Cubic spline interpolation with equi-partitioned intervals
template<int NPART>
class CubicSplineInterpolation {
public:
struct interpolation_data {
double y[NPART + 1];
double h[NPART + 1];
};
public:
template<typename F>
void initializeData(int count, interpolation_data* data, F const& functor) const {
double const lower = 0.0;
double const upper = SQRT_TANGENT_ASYMPTOTE;
double const dxds = (upper - lower) / double(NPART);
std::vector<double> buff;
buff.resize(count, 0.0);
for (int s = 0; s <= NPART; s++) {
double const x = lower + dxds * s;
functor(&buff[0], x);
for (int k = 0; k < count; k++)
data[k].y[s] = buff[k];
}
for (int k = 0; k < count; k++)
spline3_determine_natural_hvalues(NPART, data[k].y, data[k].h);
}
private:
#if 0
// global に決めようとすると数値誤差が蓄積して係数が発散する。
static void spline3_determine_natural_hvalues__global(int npart, double const* _yval, double* _hval) {
// (a-1) 順方向にする時
double const*& yval = _yval;
double*& hval = _hval;
// // (a-2) 逆方向
// std::reverse_iterator<double const*> yval(_yval + npart + 1);
// std::reverse_iterator<double*> hval(_hval + npart + 1);
// (a) 通常
{
hval[0] = 0;
hval[1] = 0;
for (int s = 2; s <= npart; s++) {
double const rhs = yval[s] - 2.0 * yval[s - 1] + yval[s - 2];
hval[s] = rhs - (4.0 * hval[s - 1] + hval[s - 2]);
}
double const sqrt3 = std::sqrt(3);
double const lam1 = -2.0 + sqrt3;
double const lam2 = -2.0 - sqrt3;
hval[1] = -2 * sqrt3 * hval[npart] / (std::pow(lam1, npart) - std::pow(lam2, npart));
// // (b-1)
// double lam1s = lam1;
// double lam2s = lam2;
// for (int s = 2; s < npart; s++) {
// lam1s *= lam1;
// lam2s *= lam2;
// hval[s] += (hval[1] / (2.0 * sqrt3)) * (lam1s - lam2s);
// }
// // (b-2)
// for (int s = 2; s < npart; s++)
// hval[s] += (hval[1] / (2.0 * sqrt3)) * (std::pow(lam1, s) - std::pow(lam2, s));
// (b-3)
for (int s = 2; s < npart; s++)
hval[s] = (yval[s] - 2.0 * yval[s - 1] + yval[s - 2]) - (4.0 * hval[s - 1] + hval[s - 2]);
hval[npart] = 0.0;
}
// // (c) 逐次的に改善 (発散は大して減らない。2, 3桁減る程度)
// {
// double const sqrt3 = std::sqrt(3);
// double const lam1 = -2.0 + sqrt3;
// double const lam2 = -2.0 - sqrt3;
// hval[0] = 0;
// hval[1] = 0;
// for (int n = 3; n <= npart; n++) {
// for (int s = 2; s <= n; s++) {
// double const rhs = yval[s] - 2.0 * yval[s - 1] + yval[s - 2];
// hval[s] = rhs - (4.0 * hval[s - 1] + hval[s - 2]);
// }
// hval[1] -= 2 * sqrt3 * hval[n] / (std::pow(lam1, n) - std::pow(lam2, n));
// }
// for (int s = 2; s <= npart; s++) {
// double const rhs = yval[s] - 2.0 * yval[s - 1] + yval[s - 2];
// hval[s] = rhs - (4.0 * hval[s - 1] + hval[s - 2]);
// }
// hval[npart] = 0;
// }
}
#endif
static void spline3_determine_natural_hvalues__local(int npart, double const* _yval, double* _hval) {
// (a-1) 順方向
double const*& yval = _yval;
double*& hval = _hval;
// // (a-2) 逆方向にしても大して変わらない
// std::reverse_iterator<double const*> yval(_yval + npart + 1);
// std::reverse_iterator<double*> hval(_hval + npart + 1);
// 大局的に spline の方程式を解くと数値的に不安定なので局所的に解く。
// 具体的には、現在地点 s0 での h 係数 (h[s0+1]) を、N個先で natural spline 条件を課して求める。
// (当然ながらこれにより、境界上での1,2次微係数の連続性は失われるが、ずれは小さいと期待する。)
// N は色々試した結果
const int N = 10;
// とする事にした。これより多くしても余り精度が改善しない様子だからである。
hval[0] = 0.0;
hval[1] = 0.0;
double const sqrt3 = std::sqrt(3);
double const lam2 = -2.0 - sqrt3;
double const lam2N = std::pow(lam2, N);
double const lam1N = 1.0 / lam2N;
double const scale = -2.0 * sqrt3 / (lam1N - lam2N);
for (int s0 = 0; s0 <= npart - N; s0++) {
for (int s = s0 + 2, sM = s0 + N; s <= sM; s++)
hval[s] = (yval[s] - 2.0 * yval[s - 1] + yval[s - 2]) - (4.0 * hval[s - 1] + hval[s - 2]);
double const h1del = scale * hval[s0 + N];
hval[s0 + 1] += h1del;
hval[s0 + 2] -= h1del * 4.0;
}
// この時点での状態:
// s0(last) = npart - N
// h[s0(last) + N] = h[npart] を 0 にする様に調整済
// h[s0(last) + 2] = h[npart - N + 2] まで確定済
for (int s = npart - N + 3; s <= npart; s++)
hval[s] = (yval[s] - 2.0 * yval[s - 1] + yval[s - 2]) - (4.0 * hval[s - 1] + hval[s - 2]);
hval[npart] = 0;
}
static void spline3_determine_natural_hvalues(int npart, double const* yval, double* hval) {
return spline3_determine_natural_hvalues__local(npart, yval, hval);
}
static double spline3_interpolate(double const a0, double const a1, double const* yval, double const* hval) {
return yval[0] * a0 + yval[1] * a1 - a0 * a1 * (hval[0] + hval[1] + hval[0] * a0 + hval[1] * a1);
}
public:
// 複数の関数の同一点 tsig の上での補間値を得る。
// 複数の interpolation_data を受け取り複数の double を返す。
void interpolate(int count, double* result, interpolation_data const* data, double tsig) const {
// double const lower = 0.0;
// double const upper = SQRT_TANGENT_ASYMPTOTE;
// double const dxds = (upper - lower) / NPART;
double const s = (NPART / SQRT_TANGENT_ASYMPTOTE) * tsig; // = tsig / dxds
int const is = int(s);
if (is < 0) {
for (int k = 0; k < count; k++)
result[k] = data[k].y[0];
} else if (is >= NPART) {
for (int k = 0; k < count; k++)
result[k] = data[k].y[NPART];
} else {
double const a1 = s - is;
double const a0 = 1.0 - a1;
for (int k = 0; k < count; k++)
result[k] = spline3_interpolate(a0, a1, data[k].y + is, data[k].h + is);
}
}
};
class CFInterpolater1 {
double bmass;
double integ_bulk_ideal;
double integ_bulk_viscous;
#ifdef ParticleSampleViscous20150330_InterpolateUCDF
static const int number_of_interpolated_functions = 8;
#else
static const int number_of_interpolated_functions = 2;
#endif
#if CFInterpolationType == 1
typedef LagrangeInterpolation<10> interpL_type;
typedef CubicSplineInterpolation<200> interpS_type;
interpL_type interpL;
interpS_type interpS;
interpL_type::interpolation_data dataL[number_of_interpolated_functions];
interpS_type::interpolation_data dataS[number_of_interpolated_functions];
#elif CFInterpolationType == 2
static const int NINTERP = 30;
LagrangeInterpolation<7> interpL[NINTERP];
LagrangeInterpolation<7>::interpolation_data dataL[NINTERP][number_of_interpolated_functions];
#else
# error runjam: invalid macro value of CFInterpolationType
#endif
private:
struct initializeData_lambda1 {
CFIntegrator& eval;
initializeData_lambda1(CFIntegrator& eval): eval(eval) {}
void operator()(double* buff, double tsig) const {
eval.integrateSurfaceByParamTsig(tsig, buff[0], buff[1]);
#ifdef ParticleSampleViscous20150330_InterpolateUCDF
eval.integrateMomentByParamTsig(tsig, buff + 2);
#endif
}
};
public:
void initializeData(ResonanceRecord const& reso, double beta) {
CFIntegrator eval;
this->bmass = beta * reso.mass;
eval.bmass = this->bmass;
eval.sign = -reso.bf;
eval.xsig = this->bmass;
eval.vsig = 0.0;
eval.integrateBulk(
this->integ_bulk_ideal,
this->integ_bulk_viscous);
initializeData_lambda1 lambda1(eval);
#if CFInterpolationType == 1
interpL.initialize(0.0, 0.1);
interpL.initializeData(number_of_interpolated_functions, dataL, lambda1);
interpS.initializeData(number_of_interpolated_functions, dataS, lambda1);
#elif CFInterpolationType == 2
const double width=SQRT_TANGENT_ASYMPTOTE/NINTERP;
for (int ipart=0; ipart<NINTERP; ipart++) {
interpL[ipart].initialize(width*ipart, width*(ipart+1));
interpL[ipart].initializeData(number_of_interpolated_functions, dataL[ipart], lambda1);
}
#endif
}
private:
void interpolateAt(int offset, int count, double* buff, double tvalue) const {
#if CFInterpolationType == 1
if (tvalue<0.1)
interpL.interpolate(count, buff, this->dataL + offset, tvalue);
else
interpS.interpolate(count, buff, this->dataS + offset, tvalue);
#elif CFInterpolationType == 2
const double width = SQRT_TANGENT_ASYMPTOTE/NINTERP;
int const ipart = std::max(0, std::min(NINTERP - 1, int(tvalue / width)));
interpL[ipart].interpolate(count, buff, this->dataL[ipart] + offset, tvalue);
#endif
}
public:
double IsotropicPartTotalCDF(double xsig, double dsig0, double dsig_abs, double piMax_2enthalpy) const {
double ret = 0.0;
if (dsig0 > 0) {
double const ideal = this->integ_bulk_ideal ;
double const viscous = this->integ_bulk_viscous;
ret += 4.0 * dsig0 * (ideal + piMax_2enthalpy * viscous);
}
if (std::abs(dsig0) < dsig_abs) {
double const tsig = std::sqrt(std::atan(xsig - this->bmass));
double result[2];
this->interpolateAt(0, 2, result, tsig);
double const ideal = result[0];
double const viscous = result[1];
ret += dsig_abs * (ideal + piMax_2enthalpy * viscous);
}
return ret;
}
public:
#ifdef ParticleSampleViscous20150330_InterpolateUCDF
double IsotropicPartUCDF(double xsig, double dsig0, double dsig_abs, double piMax_2enthalpy, double x) const {
double ret = 0.0;
double const t = std::sqrt(std::atan(x - this->bmass));
double integ_pE[2];
this->interpolateAt(2, 2, integ_pE, t);
// integ_pE[0] : E p f0
// integ_pE[1] : E p delf
if (dsig0 > 0) {
double const ideal = integ_pE[0];
double const viscous = integ_pE[1];
ret += 4.0 * dsig0 * (ideal + piMax_2enthalpy * viscous);
}
if (std::abs(dsig0) < dsig_abs) {
double ideal, viscous;
if (x <= xsig) {
// 積分範囲 [tsig, ∞)
double const tsig = std::sqrt(std::atan(xsig - this->bmass));
double integ_surf[2];
this->interpolateAt(0, 2, integ_surf, tsig);
ideal = integ_surf[0];
viscous = integ_surf[1];
} else {
// 積分範囲 [t, ∞)
double integ_mom[4];
this->interpolateAt(4, 4, integ_mom, t);
// integ_sum[0] : E^2 f0
// integ_sum[1] : E^2 delf
// integ_sum[2] : p^2 f0
// integ_sum[3] : p^2 delf
// (x*vsig-bp)^2
double const vsig = std::sqrt(1.0 - (bmass / xsig) * (bmass / xsig));
ideal = vsig * vsig * integ_mom[0] - 2.0 * vsig * integ_pE[0] + integ_mom[2];
viscous = vsig * vsig * integ_mom[1] - 2.0 * vsig * integ_pE[1] + integ_mom[3];
}
ret += dsig_abs * (ideal + piMax_2enthalpy * viscous);
}
return ret;
}
#endif
};
class TotalCDFInterpolater {
double m_beta;
std::vector<CFInterpolater1> m_data;
void initialize(ResonanceList* rlist, double beta) {
this->m_beta = beta;
this->m_data.resize(rlist->size());
std::size_t ireso = 0;
for (ResonanceRecord const& reso: *rlist)
m_data[ireso++].initializeData(reso, beta);
}
public:
TotalCDFInterpolater(ResonanceList* rlist, double beta) {
this->initialize(rlist, beta);
}
double beta() const { return this->m_beta; }
double IsotropicPartTotalCDF(int ireso, double xsig, double dsig0, double dsig_abs, double piMax_2enthalpy) const {
return this->m_data[ireso].IsotropicPartTotalCDF(xsig, dsig0, dsig_abs, piMax_2enthalpy);
}
#ifdef ParticleSampleViscous20150330_InterpolateUCDF
double IsotropicPartUCDF(int ireso, double xsig, double dsig0, double dsig_abs, double piMax_2enthalpy, double x) const {
return this->m_data[ireso].IsotropicPartUCDF(xsig, dsig0, dsig_abs, piMax_2enthalpy, x);
}
#endif
};
} /* end of namespace interpolation */
struct ViscousCooperFryeIntegrator {
double dsig0;
double dsig_abs;
double vsig; // in [0.0, 1.0] if the surface is spacelike, -1.0 if the surface is timelike
double piMax_2enthalpy;
// for each resonance
int ireso; // used by m_interp
double bmass;
double sign; // +1 for boson, -1 for fermion
double xsig;
private:
double calculateIntegrandBulk(double x) const {
if (x >= CONST_ENERGY_MAX_FACTOR) return 0;
//if (!isfinite(exp_)) { std::cerr << "exp: x=" << x << std::endl; std::exit(2); }
// f(x)
double const bp2 = x * x - bmass * bmass;
double const exp_ = std::exp(x);
double const f0 = 1.0 / (exp_ - sign);
double const f = f0 * (1 + piMax_2enthalpy * exp_ * f0 * bp2);
// bE*bp
double const pE = x * std::sqrt(bp2);
//if (!isfinite(pE * f))std::cerr << "x=" << x << std::endl;
return pE * f;
}
struct IntegrandBulk {
ViscousCooperFryeIntegrator const& parent;
double const& xsig;
IntegrandBulk(ViscousCooperFryeIntegrator const& parent, double const& xsig): parent(parent), xsig(xsig) {}
double operator()(double t) const {
// x, dx/dt
double const tantt = std::tan(t * t);
double const jacob = 2 * t * (tantt * tantt + 1);
double const x = tantt + xsig;
return jacob * parent.calculateIntegrandBulk(x);
}
};
double calculateIntegrandSurface(double x) const {
if (x >= CONST_ENERGY_MAX_FACTOR)return 0;
// f(x)
double const bp2 = x * x - bmass * bmass;
double const exp_ = std::exp(x);
double const f0 = 1.0 / (exp_ - sign);
double const f = f0 * (1 + piMax_2enthalpy * exp_ * f0 * bp2);
// (x*vsig-bp)^2
double const bp = std::sqrt(bp2);
double const prod = x * vsig - bp;
return f * prod * prod;
}
struct IntegrandSurface {
ViscousCooperFryeIntegrator const& parent;
double const& xsig;
IntegrandSurface(ViscousCooperFryeIntegrator const& parent, double const& xsig): parent(parent), xsig(xsig) {}
double operator()(double t) const {
// x, dx/dt
double const tantt = std::tan(t * t);
double const jacob = 2 * t * (tantt * tantt + 1);
double const x = tantt + xsig;
return jacob * parent.calculateIntegrandSurface(x);
}
};
public:
double internalIsotropicPartUCDF(double xlow) const {
double ret = 0.0;
if (dsig0 > 0.0) {
IntegrandBulk const integrand(*this, xlow);
ret += 4.0 * dsig0 * kashiwa::IntegrateByGaussLegendre<100>(0.0, SQRT_TANGENT_ASYMPTOTE, integrand);
}
if (vsig >= 0.0) {
IntegrandSurface const integrand(*this, std::max(xsig, xlow));
ret += dsig_abs * kashiwa::IntegrateByGaussLegendre<100>(0.0, SQRT_TANGENT_ASYMPTOTE, integrand);
}
return ret;
}
double internalIsotropicPartTotalCDF() const {
return internalIsotropicPartUCDF(this->bmass);
}
private:
interpolation::TotalCDFInterpolater const* m_interp;
public:
void setCDFInterpolater(interpolation::TotalCDFInterpolater* interp) {
this->m_interp = interp;
}
ViscousCooperFryeIntegrator() {
this->m_interp = NULL;
}
double IsotropicPartTotalCDF() const {
if (this->m_interp)
return this->m_interp->IsotropicPartTotalCDF(ireso, xsig, dsig0, dsig_abs, piMax_2enthalpy);
else
return this->internalIsotropicPartTotalCDF();
}
/// F1 の上側累積関数
/// @param xlow 条件 bmass<=xlow
double IsotropicPartUCDF(double xlow) const {
#ifdef ParticleSampleViscous20150330_InterpolateUCDF
// if (this->m_interp) {
// double const interpolated = this->m_interp->IsotropicPartUCDF(ireso, xsig, dsig0, dsig_abs, piMax_2enthalpy, xlow);
// double const integrated = this->internalIsotropicPartUCDF(xlow);
// //if (std::abs((interpolated - integrated) / (interpolated + integrated + 1e-8)) > 1e-5)
// if (std::abs(interpolated - integrated) > 1e-5)
// std::cerr << "IsotropicPartUCDF: " << interpolated << " (" << integrated << ")" << std::endl;
// }
if (this->m_interp)
return this->m_interp->IsotropicPartUCDF(ireso, xsig, dsig0, dsig_abs, piMax_2enthalpy, xlow);
else
#endif
return this->internalIsotropicPartUCDF(xlow);
}
/// F1 の上側累積分布関数の逆関数
/// @param value 累積確率を指定します。
/// @return 対応する x = beta*energy を返します。
double IsotropicPartInverseUCDF(double value) const {
// 二分法、目的関数が単調減少である事に注意。
double alow = 0.0;
double aupp = SQRT_TANGENT_ASYMPTOTE;
for (int i = 0; i < CONST_NumberOfBisectionIteration && aupp - alow > 1e-5; i++) {
double const asec = (alow + aupp) * 0.5;
double const tsec = std::tan(asec * asec);
double const xsec = bmass + tsec;
// assert F(alow) >= value > F(aupp)
if (IsotropicPartUCDF(xsec) >= value)
alow = asec;
else
aupp = asec;
}
double const a = (alow + aupp) * 0.5;
double const t = std::tan(a * a);
double const x = bmass + t;
return x;
}
};
struct SurfaceParticleSampler: ViscousCooperFryeIntegrator {
typedef ViscousCooperFryeIntegrator base;
using base::dsig0;
using base::dsig_abs;
using base::vsig;
using base::piMax_2enthalpy;
// change for each resonance
using base::bmass;
using base::sign;
using base::xsig;
// surface information
HypersurfaceElementC0Lrf const* surface;
double temperature;
double beta;
double dsig[4];
double _1_2enthalpy;
double const* stress;
private: // preferences
double m_overSamplingFactor;
public:
void setOverSamplingFactor(double value) {
this->m_overSamplingFactor = value;
}
// double getOverSamplingFactor() const {
// return this->m_overSamplingFactor;
// }
private:
bool m_turnsOffViscousEffect;
public:
void setTurnsOffViscousEffect(bool value) {
if ((this->m_turnsOffViscousEffect = value)) {
this->_1_2enthalpy = 0.0;
this->piMax_2enthalpy = 0.0;
} else {
// piMax = \pi_max = max{固有値 of \pi}
double const piMax = surface->m_stressMax;
this->_1_2enthalpy = 0.5 / (surface->m_energy + surface->m_pressure);
this->piMax_2enthalpy = piMax * _1_2enthalpy;
}
}
public:
SurfaceParticleSampler(HypersurfaceElementC0Lrf const* surface) {
this->m_overSamplingFactor = 1.0;
this->initialize(surface);
this->m_dominating = NULL;
this->totalIntegral = 0.0;
}
public:
void initialize(HypersurfaceElementC0Lrf const* surface) {
this->surface = surface;
this->temperature = surface->temperature();
this->stress = surface->m_stress;
double const tau = surface->position(0);
//double const tau = 1.0;
this->beta = 1.0 / temperature;
this->dsig[0] = surface->surfaceElement(0) * tau;
this->dsig[1] = surface->surfaceElement(1);
this->dsig[2] = surface->surfaceElement(2) * tau;
this->dsig[3] = surface->surfaceElement(3) * tau;
// 面素 dσ を局所静止系での表示に持ってくる。
// 共変ベクトル dsig を逆ブーストする (= 反変ベクトルの順ブーストと同じ操作)。
LorentzBoost(this->dsig, surface->m_velocity);
this->dsig0 = this->dsig[0];
this->dsig_abs = std::sqrt(dsig[1] * dsig[1] + dsig[2] * dsig[2] + dsig[3] * dsig[3]);
double const dsig0_abs = std::abs(dsig0);
if (dsig0_abs < dsig_abs) {
// spacelike surface
vsig = dsig0_abs / dsig_abs;
} else {
// timelike surface
vsig = -1.0;
}
this->sign = 0;
this->bmass = -1;
this->setTurnsOffViscousEffect(false);
}
private:
// stress =
// [ stress[0] stress[1] stress[2] ]
// [ stress[1] stress[3] stress[4] ]
// [ stress[2] stress[4] stress[5] ]
double UnisotropicPartProbability(double* bp) {
double const pppi
= stress[0] * bp[1] * bp[1]
+ stress[3] * bp[2] * bp[2]
+ stress[5] * bp[3] * bp[3]
+ 2.0*(
stress[1] * bp[1] * bp[2]
+stress[2] * bp[1] * bp[3]
+stress[4] * bp[2] * bp[3]);
double const x = bp[0];
double const bp2 = x * x - bmass * bmass;
// f0*exp(x) = exp(x)/(exp(x)-sgn) = 1/(1-sgn*exp(-x))
double const f0exp = 1.0 / (1.0 - sign * std::exp(-x));
double const prob1 = (1.0 + _1_2enthalpy * pppi * f0exp) / (1 + piMax_2enthalpy * bp2 * f0exp);
return std::max(prob1, 0.0);
}
private:
double totalIsotropicPartCDF;
/// 局所静止系での beta*momentum を生成します。
/// @return 棄却された場合に false を返します。
bool generateMomentum(double* bpout) {
// (1) 運動量分布(等方部分)
// (0, 1] の範囲で一様乱数を生成する為に 1.0-getRand() を用いる。
// 理由: ucdf==0 は E=p=∞ を意味するので除外したい。
// また、ucdf==1 は p=0, E=m を意味するので (位相体積0だが) 一応可能である。
// 従って、ucdf in (0, totalIsotropicPartCDF] となる。従って (0, 1] の乱数を用いたい。
// 一方で、getRand() は [0, 1) の一様乱数を生成するので 1.0-getRand() とすれば良い。
// To generate uniform random numbers in the interval (0, 1], we use 1.0-getRand() instead of getRand().