-
Notifications
You must be signed in to change notification settings - Fork 10
/
crevolutor.cc
2593 lines (2117 loc) · 107 KB
/
crevolutor.cc
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
/**
* @file crevolutor.cc
* @authors Luca Maccione, Daniele Gaggero
* @email [email protected]
* @email [email protected]
* @brief Classes for the solution of the transport equation are implemented. See the .h file.
*/
#include "crevolutor.h"
#include "grid.h"
#include "gas.h"
#include "galaxy.h"
#include "xsec.h"
#include "utilities.h"
#include "input.h"
#include "sources.h"
#include "diffusion.h"
#include "config.h"
#include <gsl/gsl_integration.h>
#ifdef _OPENMP
#include <omp.h>
#endif
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
TCREvolutor::TCREvolutor(Galaxy* gal1) {
gal = gal1;
in = gal1->GetInput();
coord = gal->GetCoordinates();
dimr = coord->GetDimR();
dimz = coord->GetDimZ();
dimE = coord->GetDimE();
riac1 = vector<double>(dimr*dimz*dimE,0);
riac2 = vector<double>(dimr*dimz*dimE,0);
riac3 = vector<double>(dimr*dimz*dimE,0);
Pdotup = vector<double>(dimr*dimz*dimE,0);
Pdotdown = vector<double>(dimr*dimz*dimE,0);
}
//2D engine -------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------
void TCREvolutor::Run(vector<double>& N, vector<double>& N_previous, TInelasticCrossSection *xsec_object, const vector<double>& dpdt, const vector<double>& SecSource_, const vector<double>& spectrum, double A, double Z, double lifetime, int daughter, bool SecEl, int K_electron, bool isDM, bool isextra) {
//-----------------------------------------------------------------------------------------------------------------------------------
if (in->feedback >1) cout << "This is the 2D engine! " << endl;
int uid = 1000*Z+A;
double injfactor = CalcInjFactor(K_electron, SecEl, isDM, A, Z);
if (in->feedback >1) cout << endl << "*** We are propagating this particle: *** " << endl;
if (in->feedback >1) cout << "A = " << A << " Z = " << Z << " SecEl = " << SecEl << " K_electron = " << K_electron << " injfactor = " << injfactor << endl;
if (A==0 && Z==-1 && SecEl==0 && in->feedback >1)
cout << "Primary electrons" << endl;
if (A==0 && Z==-1 && SecEl==1 && in->feedback >1)
cout << "Secondary electrons" << endl;
if (in->feedback >1) cout << "Starting propagation... " << endl;
vector<double>::const_iterator itsecsource = max_element(SecSource_.begin(), SecSource_.end());
if (injfactor == 0 && (*itsecsource) == 0 && in->feedback >1) {
cout << "No primary nor secondary source. Returning." << endl;
return ;
}
vector<double> xsec;
map<pair<int,int>, vector<double> > xsec_extended;
/*if (in->spallationxsec == Fluka)
xsec_extended = xsec_object->GetXSec_extended();
else*/
xsec = xsec_object->GetXSec();
TGas* totalgas = gal->GetTotalGas();
map<int, double> gas_abundances = gal->GetGasAbundances();
TSource* source = GetSourceTerm(isDM, isextra);
TConvectionVelocity* vC = gal->GetVC();
TDiffusionCoefficient* dperp = NULL;
TReaccelerationCoefficient* dpp = NULL;
vector<double> totmomentum;
if (A == 0) {
dperp = gal->GetDiffCoeffEl();
dpp = gal->GetReaccCoeffEl();
totmomentum = coord->GetMomentumEl();
}
else {
dperp = gal->GetDiffCoeff();
dpp = gal->GetReaccCoeff();
totmomentum = coord->GetMomentum();
}
// Following quantities depend on rigidity, but are computed independently of the nucleus, to save memory and time. Here the necessary additional nucleus-dependent factors are restored.
int bin=0;
for (int k = 0; k < dimE; k++) if(totmomentum[k]<dperp->Getrho_b()) bin=k;
double dperpfactor[dimE];
double dppfactor[dimE];
for (int k = 0; k < dimE; k++){
if(k<=bin) dperpfactor[k] = (A==0) ? 1.0 : pow(A/fabs(Z),dperp->GetDelta());
else dperpfactor[k] = (A==0) ? 1.0 : pow(A/fabs(Z),dperp->GetDelta_h());
dppfactor[k]=1.0/dperpfactor[k];
}
vector<double> gamma(coord->GetGamma());
/* Some initialization */
int ind = -1;
int indspat = -1;
int indspat_up=-1;//for nonequidistant grid, Iris 02/25/13
int indspat_down=-1;//for nonequidistant grid, Iris 02/25/13
double ds_up=0;
double ds_down=0;
double ds_double=0;
int i = 0;
int j = 0;
int k = 0;
int ip = 0;
const double dvcdz = (vC==NULL) ? 0.0 : vC->GetDvdz()/3.0;
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(i,j,k,indspat,ind) schedule(dynamic) num_threads(OMP_NUM_THREADS)
#endif
for (i = 0; i < dimr; i++) {
for (j = 0; j < dimz; j++) {
indspat = coord->index(i,j);
double dpp1 = 0.0;
for (k = 1; k < dimE-1; k++) {
if (dpp) dpp1 = dppfactor[k]*dpp->GetReaccelerationCoefficient(indspat);
double momentumup = totmomentum[k+1];
double momentumfix = totmomentum[k];
double momentumdown = totmomentum[k-1];
double upfix = momentumup-momentumfix;
double updown = momentumup-momentumdown;
double fixdown = momentumfix-momentumdown;
ind = indspat*dimE+k;
Pdotup[ind] = dpdt[ind+1]/(upfix);
Pdotdown[ind] = dpdt[ind]/(upfix);
if (A != 0) {
Pdotup[ind] /= A;
Pdotdown[ind] /= A;
}
if (vC) {
Pdotup[ind] += momentumup*dvcdz/(upfix);
Pdotdown[ind] += momentumfix*dvcdz/(upfix);
}
if (dpp) {
double dppfix = dpp1*dpp->GetSpectrum(k);
double dppdown = dpp1*dpp->GetSpectrum(k-1);
riac1[ind] = 2.0*dppfix/(updown)/(upfix);
riac2[ind] = ( - (dppfix-dppdown)/(fixdown*fixdown) +
2.0*dppfix*(1.0/(updown)*(1.0/(upfix) + 1.0/(fixdown)) +
1.0/momentumfix/(fixdown))
);
riac3[ind] = ( - (dppfix-dppdown)/(fixdown) +
2.0*(dppfix/(updown) +
dppdown/momentumdown))/(fixdown);
}
}
ind = indspat*dimE;
Pdotup[ind] = dpdt[ind+1]/(totmomentum[1]-totmomentum[0]);
Pdotdown[ind] = dpdt[ind]/(totmomentum[1]-totmomentum[0]);
if (A != 0) {
Pdotup[ind] /= A;
Pdotdown[ind] /= A;
}
if (vC) {
Pdotup[ind] += totmomentum[1]*dvcdz/(totmomentum[1]-totmomentum[0]);
Pdotdown[ind] += totmomentum[0]*dvcdz/(totmomentum[1]-totmomentum[0]);
}
// Boundary conditions
Pdotup[ind+dimE-1] = Pdotup[ind+dimE-2];
Pdotdown[ind+dimE-1] = Pdotdown[ind+dimE-2];
if (dpp) {
riac1[ind] = riac1[ind+1];
riac2[ind] = riac2[ind+1];
riac3[ind] = riac3[ind+1];
riac1[ind+dimE-1] = riac1[ind+dimE-2];
riac2[ind+dimE-1] = riac2[ind+dimE-2];
riac3[ind+dimE-1] = riac3[ind+dimE-2];
}
}
}
double dt = in->dtmax;
double dtbar = 0.0;
double halfdtbar = 0.0;
double halfdt = 0.0;
double halfdt_dperp_factor[dimE];
for(int e=0;e<dimE;e++) halfdt_dperp_factor[e]=0.;
const double decay = (daughter!=0);
int Niter = 0;
double value = 0.0;
long int count_temp = 0 ;
while (dt > in->dtmin) {
dtbar = dt/p;
count_temp++;
if (in->feedback >1) cout << "dt = " << dt << endl;
halfdtbar = 0.5*dtbar;
halfdt = 0.5*dt;
for (int e=0;e<dimE;e++) halfdt_dperp_factor[e] = halfdt*dperpfactor[e];
for (Niter = 0; Niter < in->Nrept; ++Niter) {
/*************************************************/
// Here propagation in Z direction starts.
// ATTENTION -- there was a bug here before -- all indexes have to be declared as private (including ip) for parallelization!! Otherwise there are runtime errors on some systems
#ifdef _OPENMP
#pragma omp parallel default(shared) private(i,j,k,ip,indspat,ind,value) num_threads(OMP_NUM_THREADS)
#endif
{
vector<double> Rzz(dimz, 0.0);
vector<double> dzz(dimz, 0.0);
vector<double> uodzz(dimz, 0.0);
vector<double> lodzz(dimz, 0.0);
vector<double> yy(dimz, 0.0);
vector<double> Rrr(dimr, 0.0);
vector<double> drr(dimr, 0.0);
vector<double> uodrr(dimr, 0.0);
vector<double> lodrr(dimr, 0.0);
vector<double> xx(dimr, 0.0);
vector<double> de(dimE, 0.0);
vector<double> ee(dimE, 0.0);
vector<double> odeu(dimE, 0.0);
vector<double> oded(dimE, 0.0);
vector<double> Re(dimE, 0.0);
#ifdef _OPENMP
#pragma omp for schedule(dynamic)
#endif
for (ip = 0; ip < dimE; ip++) {
//double halfdtbarxseck = halfdtbar*xsec[ip];
double halfdtbar_xsec;
//if (in->spallationxsec != Fluka)
halfdtbar_xsec = halfdtbar*xsec[ip];
//else {
// cout << "ip = " << ip <<endl;
// halfdtbar_xsec = halfdtbar*( xsec_extended[make_pair(uid,1001)][ip]*gas_abundances[1001] +
// xsec_extended[make_pair(uid,2004)][ip]*gas_abundances[2004]);
// + xsec_extended[make_pair(uid,6012)][ip]*gas_abundances[6012]);
//}
double halfdtbarlifetimegammak = halfdtbar*(decay)/(lifetime*gamma[ip]);
double dtbarinjfactorspeck = dtbar*injfactor*spectrum[ip];
double sp = dperp->GetSpectrum(ip);
for (j = 0; j < dimr-1; j++) {
for (i = 0; i < dimz; i++) {
//MW130705: consistency to 3D case
indspat = coord->index(j,i);
ind = indspat*dimE + ip;
double CNalphaz1 = dperp->GetCNdiff_alpha1_z(ind);
double CNalphaz2 = dperp->GetCNdiff_alpha2_z(ind);
double CNalphaz3 = dperp->GetCNdiff_alpha3_z(ind);
double vCi = 0.0; // vC(i)
double vCi1 = 0.0; // vC(i+1)
double vC1i = 0.0; // vC(i-1)
if (vC)
{
vC1i = vC->GetCNconv_alpha1_z(indspat);
vCi = vC->GetCNconv_alpha2_z(indspat);
vCi1 = vC->GetCNconv_alpha3_z(indspat);
}
uodzz[i] = -CNalphaz3*halfdt_dperp_factor[ip] - halfdt*vCi1;
lodzz[i] = -CNalphaz1*halfdt_dperp_factor[ip] - halfdt*vC1i;
//totalgas->GetGas(indspat)*halfdtbarxseck
double gas_xsec = halfdtbar_xsec * totalgas->GetGas(indspat);
dzz[i] = 1. + CNalphaz2*halfdt_dperp_factor[ip] + gas_xsec + halfdtbarlifetimegammak + halfdt*vCi;
// cout << "[MW-DEBUG] " << j << " " << i << " | " << CNalphaz1 << " " << CNalphaz2 << " " << CNalphaz3 << " " << vC1i << " " << vCi << " " << vCi1 << " | " << dzz[i] << endl;
Rzz[i] = N[ind] * (2. - dzz[i]) + source->GetSource(indspat)*dtbarinjfactorspeck + dtbar*SecSource_[ind];
if (i < dimz-1) Rzz[i] -= N[ind+dimE] * uodzz[i] ;
if (i > 0) Rzz[i] -= N[ind-dimE] * lodzz[i];
} // for i
// Compute N
//cout << "test" << endl;
//#pragma omp critical
//{
// if ((j==0) && (ip==0))
// cout<<"dt = "<<dt<<"; iteration = "<<Niter<<"; --> DEBUG: "<<lodzz[0]<<" "<<dzz[0]<<" "<<uodzz[0]<<" "<<Rzz[0]<<" "<<yy[0]<<"; dimz = "<<dimz<<endl;
//}
//exit(-1);
//Utility::solve_tridag(&(lodzz[0]), &(dzz[0]), &(uodzz[0]), &(Rzz[0]), &(yy[0]), dimz);
Utility::solve_tridag(lodzz, dzz, uodzz, Rzz, yy, dimz);
for (i = dimz-2; i > 0; --i) {
value = yy[i];
N[index(j,i,ip)] = (value > 0) ? value : 0.0;
}
} // for k
} // for j
/***********************************************/
/*********************************************/
// Here propagation in Momentum direction starts: Reacceleration and/or energy losses
#ifdef _OPENMP
#pragma omp for schedule(dynamic)
#endif
for (i = 0; i < dimr-1; i++) {
for (j = 1; j < dimz-1; j++) {
indspat = coord->index(i,j);
//double totgas = totalgas->GetGas(indspat);
double gas_tot = totalgas->GetGas(indspat);
double dtbarprimsource = dtbar*injfactor*source->GetSource(indspat);
ind = indspat*dimE;
for (ip = 0; ip < dimE; ip++) {
int ind1 = ind + ip;
double gas_xsec;
//if (in->spallationxsec != Fluka)
gas_xsec = gas_tot * (halfdtbar*xsec[ip]);
//else
// gas_xsec = gas_tot * (halfdtbar*( xsec_extended[make_pair(uid,1001)][ip]*gas_abundances[1001] +
// xsec_extended[make_pair(uid,2004)][ip]*gas_abundances[2004]));
//+xsec_extended[make_pair(uid,6012)][ip]*gas_abundances[6012]) );
double devect = 1.0
+ gas_xsec + halfdtbar*(decay)/(lifetime*gamma[ip])
+ halfdt*(riac2[ind1] + Pdotdown[ind1]);
double odeuvect = halfdt*(riac1[ind1] + Pdotup[ind1]);
de[ip] = devect;
odeu[ip] = -odeuvect;
oded[ip] = -halfdt*riac3[ind1];
// cout << "[MW-DEBUG] " << ip << " " << i << " " << j << " | " << Pdotup[ind1] << " " << Pdotdown[ind1] << " " << riac1[ind1] << " " << riac2[ind1] << " " << riac3[ind1] << " " << totgas*xsec[ip] << " " << (decay)/(lifetime*gamma[ip]) << endl;
Re[ip] = dtbarprimsource*spectrum[ip] + dtbar*SecSource_[ind1] + (2.0-devect)*N[ind1];
if (ip > 0) Re[ip] -= oded[ip]*N[ind1-1];
if (ip < dimE-1) Re[ip] += odeuvect*N[ind1+1];
}
//Utility::solve_tridag(&(oded[0]), &(de[0]), &(odeu[0]), &(Re[0]), &(ee[0]), dimE);
Utility::solve_tridag(oded, de, odeu, Re, ee, dimE);
for (ip = 0; ip < dimE; ip++) {
value = ee[ip];
N[ind+ip] = (value > 0) ? value : 0.0;
}
}
}
/********************************************/
// Here propagation in R direction starts.
#ifdef _OPENMP
#pragma omp for schedule(dynamic)
#endif
for (ip = 0; ip < dimE; ip++) {
//double halfdtbarxseck = halfdtbar*xsec[ip];
double halfdtbar_xsec;
//if (in->spallationxsec != Fluka)
halfdtbar_xsec = halfdtbar*xsec[ip];
//else
// halfdtbar_xsec = halfdtbar*( xsec_extended[make_pair(uid,1001)][ip]*gas_abundances[1001] +
// xsec_extended[make_pair(uid,2004)][ip]*gas_abundances[2004]);
// + xsec_extended[make_pair(uid,6012)][ip]*gas_abundances[6012]);
double halfdtbarlifetimegammak = halfdtbar*(decay)/(lifetime*gamma[ip]);
double dtbarinjfactorspeck = dtbar*injfactor*spectrum[ip];
double sp = dperp->GetSpectrum(ip);
for (j = 1; j < dimz-1; j++) {
for (i = 0; i < dimr; i++) {
//MW130705: consistency to 3D case
indspat = coord->index(i,j);
ind = indspat*dimE + ip;
double CNalphar1 = dperp->GetCNdiff_alpha1_r(ind);
double CNalphar2 = dperp->GetCNdiff_alpha2_r(ind);
double CNalphar3 = dperp->GetCNdiff_alpha3_r(ind);
double halfdtdperpfactorphi = halfdt_dperp_factor[ip]*dperp->GetPhi(indspat)*sp;
double gas_xsec = halfdtbar_xsec * totalgas->GetGas(indspat);
drr[i] = 1.0 + CNalphar2*halfdt_dperp_factor[ip] + gas_xsec + halfdtbarlifetimegammak;
if(i==0){
uodrr[i] = -CNalphar3*halfdt_dperp_factor[ip];
uodrr[i] *= 2; //Symmetry condition at R = 0
}
else uodrr[i] = -CNalphar3*halfdt_dperp_factor[ip]-halfdtdperpfactorphi;
lodrr[i] = -CNalphar1*halfdt_dperp_factor[ip]+halfdtdperpfactorphi;
Rrr[i] = N[ind] * (2.0-drr[i]) + source->GetSource(indspat)*dtbarinjfactorspeck + dtbar*SecSource_[ind];
if (i < dimr-1) Rrr[i] -= N[ind+dimE*dimz]*uodrr[i];
if (i > 0) Rrr[i] -= N[ind-dimE*dimz]*lodrr[i];
// cout << "[MW-DEBUG] " << ip << " " << i << " " << j << " | " << CNalphar1 << " " << CNalphar2 << " " << CNalphar3 << " " << drr[i] << " " << lodrr[i] << " " << uodrr[i] << " | " << Rrr[i] << endl;
}
// Calculate N
//Utility::solve_tridag(&(lodrr[0]), &(drr[0]), &(uodrr[0]), &(Rrr[0]), &(xx[0]), dimr);
Utility::solve_tridag(lodrr, drr, uodrr, Rrr, xx, dimr);
for (int i = dimr-2; i >= 0; --i) {
value = xx[i];
N[index(i,j,ip)] = (value > 0) ? value : 0.0;
}
}
}
} // #pragma omp parallel
} // for Niter
dt *= in->dtfactor;
} // while (dt > dtmin)
return ;
}
TCREvolutorADI::TCREvolutorADI(Galaxy* gal1) {
gal = gal1;
in = gal1->GetInput();
tolerance = in->tol;
coord = gal->GetCoordinates();
dimr = coord->GetDimR();
dimz = coord->GetDimZ();
dimE = coord->GetDimE();
N1 = vector<double>(dimr*dimz*dimE,0);
N2 = vector<double>(dimr*dimz*dimE,0);
riac1 = vector<double>(dimr*dimz*dimE,0);
riac2 = vector<double>(dimr*dimz*dimE,0);
riac3 = vector<double>(dimr*dimz*dimE,0);
Pdotup = vector<double>(dimr*dimz*dimE,0);
Pdotdown = vector<double>(dimr*dimz*dimE,0);
}
double TCREvolutorADI::FindMax(TDiffusionCoefficient* dperp) {
vector<double> spatial = dperp->GetDiffusionCoefficient();
vector<double> spectrum = dperp->GetSpectrum();
vector<double>::iterator it = max_element(spatial.begin(), spatial.end());
vector<double>::iterator itsp = max_element(spectrum.begin(), spectrum.end());
return (*itsp)*(*it);
}
double TCREvolutorADI::FindTRiacc(TReaccelerationCoefficient* dpp, vector<double>& totmomentum) {
double result = 1e20;
double aux = 0.0;
for (int i = 1; i < dimE; i++) {
aux = pow(totmomentum[i]-totmomentum[i-1],2.0)/dpp->GetSpectrum(i);
result = min(aux, result);
}
vector<double> spat = dpp->GetReaccelerationCoefficient();
vector<double>::iterator it = max_element(spat.begin(), spat.end());
return result/(*it);
}
double TCREvolutorADI::FindTLoss(const vector<double>& dpdt, vector<double>& totmomentum) {
double result = 1e20;
double aux = 0.0;
for (int k = 0; k < dimE; k++) {
for (int i = 0; i < dimr; i++) {
for (int j = 0; j < dimz; j++) {
aux = totmomentum[k]/dpdt[index(i,j,k)];
result = min(aux, result);
}
}
}
return result;
}
double TCREvolutorADI::FindTInt(TGas* gas, const vector<double>& xsec) {
vector<double> gasvec = gas->GetGas();
vector<double>::iterator itgas = max_element(gasvec.begin(), gasvec.end());
vector<double>::const_iterator itxsec = max_element(xsec.begin(), xsec.end());
return 1.0/((*itgas)*(*itxsec));
}
//modified
void TCREvolutorADI::Run(vector<double>& N, vector<double>& N_previous, TInelasticCrossSection *xsec_object, const vector<double>& dpdt, const vector<double>& SecSource_, const vector<double>& spectrum, double A, double Z, double lifetime, int daughter, bool SecEl, int K_electron, bool isDM, bool isextra) {
// //MW130706: Disable the whole ADI stuff because we never use it and it relies on Phi,Psi which I got rid of.
//
// vector<double>::const_iterator itsecsource = max_element(SecSource_.begin(), SecSource_.end());
// if (gal->GetSourceAbundance(int(A+1000*Z)) == 0 && (*itsecsource) == 0) return ;
//
// TDiffusionCoefficient* dperp = NULL;
// TReaccelerationCoefficient* dpp = NULL;
//
// if (A == 0) {
// dperp = gal->GetDiffCoeffEl();
// dpp = gal->GetReaccCoeffEl();
// }
// else {
// dperp = gal->GetDiffCoeff();
// dpp = gal->GetReaccCoeff();
// }
//
// // Following quantities depend on rigidity, but are computed independently of the nucleus, to save memory and time. Here the necessary additional nucleus-dependent factors are restored.
//
// const double dperpfactor = (A==0) ? 1.0 : pow(A/fabs(Z),dperp->GetDelta());
// const double dppfactor = 1.0/dperpfactor; // = A*A/dperpfactor/A/A
//
// /*
// #ifdef HAVE_DS
// const double injfactor = (SecEl) ? 0 : 1;
// #else
// const double injfactor = (SecEl) ? 0 : (A==0) ? gal->GetSourceAbundance(int(A+1000*Z)) : gal->GetSourceAbundance(int(A+1000*Z))/A;
// #endif
// */
// double injfactor = CalcInjFactor(K_electron, SecEl, isDM, A, Z);
// /*
// double injfactor = 0.0;
// if (SecEl) injfactor = 0.0;
// else if (A > 0) injfactor = gal->GetSourceAbundance(int(A+1000*Z))/A;
// else injfactor = 1.0;
// */
//
// TGas* totalgas = gal->GetTotalGas();
// TSource* source = GetSourceTerm(isDM, isextra);
//
// // TSource* source = (isDM) ? gal->GetDMSource() : gal->GetSource();
// TConvectionVelocity* vC = gal->GetVC();
//
// // TEST!!!
// cout << A << " " << Z << " " << SecEl << " " << injfactor << endl;
// //if (K_electron > 0) injfactor = 0.; // the nucleus that already attached an electron only has a secondary source term!!
//
// vector<double> totmomentum;
// if (A == 0) totmomentum = coord->GetMomentumEl();
// else totmomentum = coord->GetMomentum();
//
// vector<double> gamma(coord->GetGamma());
//
// /* Some initialization */
//
// int ind = -1;
// int indspat = -1;
//
// for (int i = 0; i < dimr; i++) {
// for (int j = 0; j < dimz; j++) {
// indspat = coord->index(i,j);
// for (int k = 1; k < dimE-1; k++) {
// double momentumup = totmomentum[k+1];
// double momentumfix = totmomentum[k];
// double momentumdown = totmomentum[k-1];
// double upfix = momentumup-momentumfix;
// double updown = momentumup-momentumdown;
// double fixdown = momentumfix-momentumdown;
//
// ind = indspat*dimE+k;
// Pdotup[ind] = dpdt[ind+1]/(upfix);
// Pdotdown[ind] = dpdt[ind]/(upfix);
// if (A != 0) {
// Pdotup[ind] /= A;
// Pdotdown[ind] /= A;
// }
// if (vC) {
// Pdotup[ind] += momentumup/3.0*vC->GetDvdz()/(upfix);
// Pdotdown[ind] += momentumfix/3.0*vC->GetDvdz()/(upfix);
// }
//
// if (dpp) {
// double dppfix = dppfactor*dpp->GetReaccelerationCoefficient(indspat, k);
// double dppdown = dppfactor*dpp->GetReaccelerationCoefficient(indspat, k-1);
// riac1[ind] = 2.0*dppfix/(updown)/(upfix);
// riac2[ind] = ( - (dppfix-dppdown)/pow(fixdown, 2.0) +
// 2.0*dppfix/(updown)*(1.0/(upfix) + 1.0/(fixdown)) +
// 2.0*dppfix/momentumfix/(fixdown)
// );
// riac3[ind] = ( - (dppfix-dppdown)/pow(fixdown, 2.0) +
// 2.0*dppfix/(updown)/(fixdown) +
// 2.0*dppdown/momentumdown/(fixdown)
// );
// }
// }
// ind = indspat*dimE;
// Pdotup[ind] = dpdt[indspat+1]/(totmomentum[1]-totmomentum[0]);
// Pdotdown[ind] = dpdt[ind]/(totmomentum[1]-totmomentum[0]);
// if (A != 0) {
// Pdotup[ind] /= A;
// Pdotdown[ind] /= A;
// }
//
// if (vC) {
// Pdotup[ind] += totmomentum[1]/3.0*vC->GetDvdz()/(totmomentum[1]-totmomentum[0]);
// Pdotdown[ind] += totmomentum[0]/3.0*vC->GetDvdz()/(totmomentum[1]-totmomentum[0]);
// }
//
// // Boundary conditions
// Pdotup[ind+dimE-1] = Pdotup[ind+dimE-2];
// Pdotdown[ind+dimE-1] = Pdotdown[ind+dimE-2];
// if (dpp) {
// riac1[ind] = riac1[ind+1];
// riac2[ind] = riac2[ind+1];
// riac3[ind] = riac3[ind+1];
// riac1[ind+dimE-1] = riac1[ind+dimE-2];
// riac2[ind+dimE-1] = riac2[ind+dimE-2];
// riac3[ind+dimE-1] = riac3[ind+dimE-2];
// }
// }
// }
//
// double decay = (daughter!=0);
// // const double maxDperp = dperpfactor*FindMax(dperp);
// const double minTRiacc = (dpp) ? 1.0/dppfactor*FindTRiacc(dpp, totmomentum) : 1e20;
// const double minTLoss = (A == 0) ? FindTLoss(dpdt, totmomentum) : A*FindTLoss(dpdt, totmomentum);
// const double minTInt = (A == 0) ? 1e20 : FindTInt(totalgas, xsec);
// const double minTDec = (daughter != 0) ? lifetime : 1e20;
//
// const double ADIfactor = 1.0/p;
//
// bool conv = false;
//
// double vCi = 0.0; // vC(i)
// double vCi1 = 0.0; // vC(i+1)
// double vC1i = 0.0; // vC(i-1)
// double sp = 0.0;
//
// int counter = 0;
//
// vector<double> Rzz(dimz-2, 0.0);
// vector<double> dzz(dimz-2, 0.0);
// vector<double> uodzz(dimz-2, 0.0);
// vector<double> lodzz(dimz-2, 0.0);
// vector<double> yy(dimz-2, 0.0);
//
//
// vector<double> Rrr(dimr-1, 0.0);
// vector<double> drr(dimr-1, 0.0);
// vector<double> uodrr(dimr-1, 0.0);
// vector<double> lodrr(dimr-1, 0.0);
// vector<double> xx(dimr-1, 0.0);
//
// vector<double> de(dimE-2, 0.0);
// vector<double> ee(dimE-2, 0.0);
// vector<double> odeu(dimE-2, 0.0);
// vector<double> oded(dimE-2, 0.0);
// vector<double> Re(dimE-2, 0.0);
//
//
// while (!conv) {
// double error = 0.0;
//
// /*************************************************/
// // Here propagation in Z direction starts.
// for (int k = 1; k < dimE-1; k++) {
//
//
//
// for (int j = 0; j < dimr-1; j++) {
// double dr = coord->GetDeltaR_central(j);
// double coeffz = 1/dr/dr;
// for (int i = 1; i < dimz-1; i++) {
// double dz = coord->GetDeltaZ_central(i);
// double coeffr = 1/dz/dz;
//
// //MW130620: dtADI --> in->alpha and multiply dr*dz on it later on. - have to move this inside of the loop
//
// double dtbar = ADIfactor * in->alpha * dr * dz;//min(dtADI, min(minTRiacc, min(minTLoss, min(minTInt,minTDec) ) ) );
// double halfdtbar = 0.5*dtbar;
//
// double halfdtbarxseck = halfdtbar*xsec[k];
// double halfdtbarlifetimegammak = halfdtbar*(decay)/(lifetime*gamma[k]);
// double dtbarinjfactorspeck = dtbar*injfactor*spectrum[k];
// sp = dperpfactor*dperp->GetSpectrum(k);
//
//
// indspat = coord->index(j,i);
// ind = indspat*dimE+k;
// double Nind = N[ind];
// double psi = dperp->GetPsi(indspat);
//
// double diffcoeff = dperp->GetDiffusionCoefficient(indspat)*sp;
// double coeffrdiffcoeff = coeffr*diffcoeff;
//
// if (vC) {
// if (i > (dimz-1)/2) {
// vCi1 = 0.0;
// vC1i = vC->GetVC(j,i-2)/dz;
// vCi = vC->GetVC(j,i)/dz;
// }
// else if (i < (dimz-1)/2) {
// vC1i = 0.0;
// vCi1 = -vC->GetVC(j,i+1)/dz;
// vCi = -vC->GetVC(j,i)/dz;
// }
// else {
// vCi = 0.0;
// vCi1 = 0.0;
// vC1i = 0.0;
// }
// }
//
// double dzzcoeff = dtbar*coeffrdiffcoeff + (totalgas->GetGas(indspat)*halfdtbarxseck + halfdtbarlifetimegammak) + halfdtbar*vCi;
// dzz[i-1] = 1.0 + dzzcoeff;
//
// if (i < dimz-2) uodzz[i-1] = -halfdtbar*(coeffrdiffcoeff + psi*sp) - halfdtbar*vCi1;
// if (i > 1) lodzz[i-1] = -halfdtbar*(coeffrdiffcoeff - dperp->GetPsi(indspat-1)*sp) - halfdtbar*vC1i;
//
// Rzz[i-1] = Nind*(1.0 - dzzcoeff)
// + N[ind+dimE]*(halfdtbar*(coeffrdiffcoeff + psi*sp) + vCi1)
// + N[ind-dimE]*(halfdtbar*(coeffrdiffcoeff - psi*sp) + vC1i)
// + source->GetSource(indspat)*dtbarinjfactorspeck
// + dtbar*SecSource_[ind]
// ;
//
// if (j == 0) Rzz[i-1] += 2.0*dtbar*coeffz*diffcoeff*(N[ind+dimz*dimE] - Nind);
// else Rzz[i-1] += dtbar*(
// (coeffz*diffcoeff+dperp->GetPhi(indspat)*sp)*N[ind+dimz*dimE]
// - 2.0*coeffz*diffcoeff*Nind
// + (coeffz*diffcoeff-dperp->GetPhi(indspat)*sp)*N[ind-dimz*dimE]);
//
// Rzz[i-1] += dtbar*(
// (riac1[ind]+Pdotup[ind])*N[ind+1]
// - (riac2[ind]+Pdotdown[ind])*Nind
// + riac3[ind]*N[ind-1]);
// } // dimz-1
//
// // Calcolare N
// //Utility::solve_tridag(lodzz, dzz, uodzz, Rzz, yy);
// Utility::solve_tridag(&(lodzz[0]), &(dzz[0]), &(uodzz[0]), &(Rzz[0]), &(yy[0]), dimz);
// for (int i = dimz-2; i > 0; --i) N1[index(j,i,k)] = yy[i-1];
// } // dimr-1
// } // dimE
//
// /***********************************************/
// /*********************************************/
//
// // Here propagation in Momentum direction starts: Reacceleration and/or energy losses
// for (int i = 0; i < dimr-1; i++) {
// double dr = coord->GetDeltaR_central(i);
// double coeffz = 1/dr/dr;
// for (int j = 1; j < dimz-1; j++) {
// double dz = coord->GetDeltaZ_central(j);
// double coeffr = 1/dz/dz;
//
// //MW130620: dtADI --> in->alpha and multiply dr*dz on it later on. - have to move this inside of the loop
// double dtbar = ADIfactor * in->alpha * dr * dz;//min(dtADI, min(minTRiacc, min(minTLoss, min(minTInt,minTDec) ) ) );
// double halfdtbar = 0.5*dtbar;
//
// indspat = coord->index(i,j);
// double totgas = totalgas->GetGas(indspat);
// ind = indspat*dimE;
//
// for (int k = 1; k < dimE-1; k++) {
// int ind1 = ind + k;
// double N1ind1 = N1[ind1];
// sp = dperpfactor*dperp->GetSpectrum(k);
//
// double dtbarprimsource = dtbar*injfactor*source->GetSource(indspat);
// double defact = halfdtbar*(riac2[ind1] + Pdotdown[ind1] + totgas*xsec[k] + (decay)/(lifetime*gamma[k]));
//
// de[k-1] = 1.0 + defact;
//
// if (k < dimE-2) odeu[k-1] = - halfdtbar*(riac1[ind1] + Pdotup[ind1]);
// if (k > 1) oded[k-1] = - halfdtbar*riac3[ind1-1];
//
//
// Re[k-1] = N1ind1*(1.0 - defact)
// + N1[ind1+1]*halfdtbar*(riac1[ind1] + Pdotup[ind1])
// + N1[ind1-1]*halfdtbar*riac3[ind1]
// + dtbarprimsource*spectrum[k] // primary source
// + dtbar*SecSource_[ind1] // secondary source
// ;
//
// double diffcoeff = dperp->GetDiffusionCoefficient(indspat)*sp;
//
// if (i == 0) Re[k-1] += dtbar*coeffz*diffcoeff*(2.0*N1[ind1+dimz*dimE] - 2.0*N1ind1);
// else Re[k-1] += dtbar*(
// (coeffz*diffcoeff + dperp->GetPhi(indspat)*sp)*N1[ind1+dimz*dimE]
// - 2.0*coeffz*diffcoeff*N1ind1
// + (coeffz*diffcoeff - dperp->GetPhi(indspat)*sp)*N1[ind1-dimz*dimE]
// );
//
// Re[k-1] += dtbar*(
// (coeffr*diffcoeff + dperp->GetPsi(indspat)*sp)*N1[ind1+dimE]
// - 2.0*coeffr*diffcoeff*N1ind1
// + (coeffr*diffcoeff - dperp->GetPsi(indspat)*sp)*N1[ind1-dimE]
// );
// if (vC) {
// if (j > (dimz-1)/2) Re[k-1] -= dtbar/dz*(vC->GetVC(i,j)*N1ind1 - vC->GetVC(i,j-1)*N1[ind1-dimE]);
// else if (j < (dimz-1)/2) Re[k-1] += dtbar/dz*(vC->GetVC(i,j)*N1ind1 - vC->GetVC(i,j+1)*N1[ind1+dimE]);
// }
// } // dimE-1
//
// //Utility::solve_tridag(oded, de, odeu, Re, ee);
// Utility::solve_tridag(&(oded[0]), &(de[0]), &(odeu[0]), &(Re[0]), &(ee[0]), dimE);
// for (int k = 1; k < dimE-1; k++) N2[index(i,j,k)] = ee[k-1];
//
// } // dimz-1
// } // dimr-1
//
//
// /********************************************/
//
// // Here propagation in R direction starts.
// for (int k = 1; k < dimE-1; k++) {
//
// for (int j = 1; j < dimz-1; j++) {
// double dz = coord->GetDeltaZ_central(j);
// double coeffr = 1/dz/dz;
// for (int i = 0; i < dimr-1; i++) {
// double dr = coord->GetDeltaR_central(i);
// double coeffz = 1/dr/dr;
//
// //MW130620: have to move this (has been dtADI) inside of the loop
// double dtbar = ADIfactor * in->alpha * dr * dz;//min(dtADI, min(minTRiacc, min(minTLoss, min(minTInt,minTDec) ) ) );
// double halfdtbar = 0.5*dtbar;
// double halfdtbarxseck = halfdtbar*xsec[k];
// double halfdtbarlifetimegammak = halfdtbar*(decay)/(lifetime*gamma[k]);
// double dtbarinjfactorspeck = dtbar*injfactor*spectrum[k];
// sp = dperpfactor*dperp->GetSpectrum(k);
//
// indspat = coord->index(i,j);
// ind = indspat*dimE+k;
//
// double N2ind = N2[ind];
//
// double diffcoeff = dperp->GetDiffusionCoefficient(indspat)*sp;
// double coeffzdiffcoeff = coeffz*diffcoeff;
//
// double drrcoeff = dtbar*coeffzdiffcoeff + (totalgas->GetGas(indspat)*halfdtbarxseck + halfdtbarlifetimegammak);
// drr[i] = 1.0 + drrcoeff;
//
// double phi = dperp->GetPhi(indspat);
//
// if (i == 0) uodrr[i] = -dtbar*coeffzdiffcoeff; // i == 0, Symmetry condition at R = 0
// else uodrr[i] = -halfdtbar*(coeffzdiffcoeff + phi*sp);
// if (i > 0) lodrr[i] = -halfdtbar*(coeffzdiffcoeff - phi*sp);
//
// Rrr[i] = N2ind*(1.0 - drrcoeff)
// + N2[ind+dimz*dimE]*((i==0)*dtbar*coeffzdiffcoeff + (i>0)*halfdtbar*(coeffzdiffcoeff + phi*sp))
// + source->GetSource(indspat)*dtbarinjfactorspeck
// + dtbar*SecSource_[ind]
// ;
//
// if (i > 0) Rrr[i] += N2[ind-dimz*dimE]*halfdtbar*(coeffzdiffcoeff - phi*sp);
// Rrr[i] += dtbar*(
// (coeffr*diffcoeff + dperp->GetPsi(indspat)*sp)*N2[ind+dimE]
// - 2.0*coeffr*diffcoeff*N2ind
// + (coeffr*diffcoeff - dperp->GetPsi(indspat)*sp)*N2[ind-dimE]
// );
//
// Rrr[i] += dtbar*(
// (riac1[ind] + Pdotup[ind])*N2[ind+1]
// - (riac2[ind] + Pdotdown[ind])*N2ind
// + riac3[ind]*N2[ind-1]
// );
//
// if (vC) {
// if (j > (dimz-1)/2) Rrr[i] -= dtbar/dz*(vC->GetVC(i,j)*N2ind - vC->GetVC(i,j-1)*N2[ind-dimE]);
// else if (j < (dimz-1)/2) Rrr[i] += dtbar/dz*(vC->GetVC(i,j)*N2ind - vC->GetVC(i,j+1)*N2[ind+dimE]);
// }
// } // dimr-1
//
// // Calculate N
// // Utility::solve_tridag(lodrr, drr, uodrr, Rrr, xx);
// Utility::solve_tridag(&(lodrr[0]), &(drr[0]), &(uodrr[0]), &(Rrr[0]), &(xx[0]), dimr);
//
// for (int i = dimr-2; i >= 0; --i) {
// error = max(fabs(1.0 - xx[i]/N[index(i,j,k)]), error);
// N[index(i,j,k)] = xx[i];
// }
//
// } // dimz-1
// } // dimE-1
//
// if (counter%100 == 0) cout << error << endl;
// counter++;
// if (error < tolerance) conv = true;
// } // while (!conv)
//
// cout << "Steps: " << counter << endl;
return ;
}
// MODIFIED 17-09-2012
//**********************************************************************************************************************************************************
//**********************************************************************************************************************************************************
//**********************************************************************************************************************************************************
TCREvolutor3D::TCREvolutor3D(Galaxy* gal1) {
gal = gal1;
in = gal1->GetInput();
coord = gal->GetCoordinates();
dimx = coord->GetDimX();
dimy = coord->GetDimY();
dimz = coord->GetDimZ();
dimE = coord->GetDimE();
riac1 = vector<double>(dimx*dimy*dimz*dimE,0);
riac2 = vector<double>(dimx*dimy*dimz*dimE,0);
riac3 = vector<double>(dimx*dimy*dimz*dimE,0);
Pdotup = vector<double>(dimx*dimy*dimz*dimE,0);
Pdotdown = vector<double>(dimx*dimy*dimz*dimE,0);
}
//**********************************************************************************************************************************************************
//**********************************************************************************************************************************************************
//**********************************************************************************************************************************************************
// implemented by Daniele Gaggero ([email protected]) -- september 2012
// last modified by Daniele Gaggero ([email protected]) -- november 2012 -- tested point source propagation against analytical solution -- tested against 2D code in realistic setup
// anisotropic diffusion implemented by Luca Maccione and Daniele Gaggero -- november 2012 -- !!!under testing (dec 2012)!!!
// moving sources added by D.Gaggero -- 17/01/2013
// changed structure to improve performance - M. Weinreuter 05/07/2013
void TCREvolutor3D::Run(vector<double>& N, vector<double>& N_previous, TInelasticCrossSection *xsec_object, const vector<double>& dpdt, const vector<double>& SecSource_, const vector<double>& spectrum, double A, double Z, double lifetime, int daughter, bool SecEl, int K_electron, bool isDM, bool isExtra) {
// This is for the horizon(E) plot -- just remove that after the plot is done
if (A == 0 && Z == -1) {
TDiffusionCoefficient* dperp = gal->GetDiffCoeffEl();
vector<double> totmomentum = coord->GetMomentumEl();
vector<double> dperp_vec = dperp->GetSpectrum();
vector<double> x = (gal->GetCoordinates()->GetX());
vector<double> y = (gal->GetCoordinates()->GetY());
vector<double> z = (gal->GetCoordinates()->GetZ());
double deltax = ( (x.back() - x.front()) / (dimx-1) );
double deltay = ( (y.back() - y.front()) / (dimy-1) );
double deltaz = ( (z.back() - z.front()) / (dimz-1) );
unsigned int ixsun = (unsigned int) ((in->xobs-x.front())/(2.0*x.back())*(double)(dimx-1));
unsigned int iysun = (unsigned int) ((in->yobs-y.front())/(2.0*y.back())*(double)(dimy-1));
unsigned int izsun = (unsigned int) ((in->zobs-z.front())/(2.0*z.back())*(double)(dimz-1));
long int indspat = coord->indexD(ixsun,iysun,izsun);
cout << ixsun << " " << iysun << " " << izsun << endl;
cout << "momentum vector in GeV *** " << endl;
for (int ip = 0; ip < totmomentum.size(); ip++)
cout << totmomentum[ip] << ", " ;
cout << endl;
cout << "eloss vector at Sun position in GeV/Myr *** " << endl;
for (int ip = 0; ip < totmomentum.size(); ip++) {
long int ind = indspat*dimE+ip;
cout << dpdt[ip] << ", " ;
}