-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheapTools.py
More file actions
1948 lines (1526 loc) · 85.6 KB
/
CheapTools.py
File metadata and controls
1948 lines (1526 loc) · 85.6 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
import numpy as np
from scipy.special import erf, expi
# -----------------------------------------------
#heaviside
#_safelog10
#_safelog
#_safeexpi
#GaussWeightsAndNodes
#FromSigmaToAbundance
# -----------------------------------------------
#Get_psi
#Get_ipsi
#Get_DTD
#Get_DTD_arr
#Get_infall
# -----------------------------------------------
#R1a_numeric
#R1a_analytic_gaussian
#R1a_analytic_exponential
#R1a_analytic_inverse
#R1a_analytic
#iR1a_analytic
#Get_CIa
# -----------------------------------------------
#SolveChemEvolModel_GaussianTerm
#SolveChemEvolModel_ExponentialTerm
#SolveChemEvolModel_InverseTerm
#SolveChemEvolModel_InhomogeneousTrivialTerm
#SolveChemEvolModel
# -----------------------------------------------
#ChemicalSolutionVerifier
# -----------------------------------------------
#add_element
#prepare_chemdict
#QD
#PD
#SD
#QD_gorro
#SD_gorro
# -----------------------------------------------
#LatexCheckEqA9a
#LatexCheckEqA9b
#LatexCheckEqA9c
# -----------------------------------------------
#Load_MR01_dict
#Load_G05Wide_dict
#Load_G05Close_dict
#Load_P08_dict
#Load_T08_dict
#Load_S05_dict
#Load_MVP06_dict
# -----------------------------------------------
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# AUX FUNCTIONS
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# heaviside ------------------------
def heaviside(x): return(1.*(x>0));
# ----------------------------------
# _safelog10--------------------------
def _safelog10(num, den):
not_null_den = den!=0
output = np.zeros_like(num)
output[not_null_den] = np.log10(num[not_null_den]/den[not_null_den])
zero_num = (num==0)
output[zero_num] = 0.
null_den = (den==0)
output[null_den] = np.nan
return(output)
# ---------------------------------
# _safelog--------------------------
def _safelog(num, den):
not_null_den = den!=0
output = np.zeros_like(num)
output[not_null_den] = np.log(num[not_null_den]/den[not_null_den])
zero_num = (num==0)
output[zero_num] = 0.
null_den = (den==0)
output[null_den] = np.nan
return(output)
# ---------------------------------
# _safeexpi--------------------------------------------
def _safeexpi(x): return( (x>0)*expi(x + 1.*(x<=0)) );# Avoid the divergence when expi(0)
# -----------------------------------------------------
# GaussWeightsAndNodes---------------------------------------------------------------------
def GaussWeightsAndNodes(order=64):
''' Return the weights and nodes for the Gaussian Quadrature method
weights, nodes = GaussWeightsAndNodes(order=100)'''
if order==20:
Gauss_nodes = [-0.148874338981631, 0.148874338981631, -0.433395394129247, 0.433395394129247, -0.679409568299024, 0.679409568299024, -0.865063366688985, 0.865063366688985, -0.973906528517172, 0.973906528517172]
weights = [0.295524224714753, 0.295524224714753, 0.269266719309996, 0.269266719309996, 0.219086362515982, 0.219086362515982, 0.149451349150581, 0.149451349150581, 0.066671344308688, 0.066671344308688]
elif order==40:
weights = [0.0775059479784248,0.0775059479784248,0.077039818164248,0.077039818164248,0.0761103619006262,0.0761103619006262,0.0747231690579683,0.0747231690579683,0.0728865823958041,0.0728865823958041,0.0706116473912868,0.0706116473912868,0.0679120458152339,0.0679120458152339,0.064804013456601,0.064804013456601,0.0613062424929289,0.0613062424929289,0.0574397690993916,0.0574397690993916,0.0532278469839368,0.0532278469839368,0.0486958076350722,0.0486958076350722,0.0438709081856733,0.0438709081856733,0.038782167974472,0.038782167974472,0.0334601952825478,0.0334601952825478,0.0279370069800234,0.0279370069800234,0.022245849194167,0.022245849194167,0.0164210583819079,0.0164210583819079,0.0104982845311528,0.0104982845311528,0.0045212770985332,0.0045212770985332]
Gauss_nodes = [-0.0387724175060508,0.0387724175060508,-0.1160840706752552,0.1160840706752552,-0.1926975807013711,0.1926975807013711,-0.2681521850072537,0.2681521850072537,-0.3419940908257585,0.3419940908257585,-0.413779204371605,0.413779204371605,-0.4830758016861787,0.4830758016861787,-0.5494671250951282,0.5494671250951282,-0.6125538896679802,0.6125538896679802,-0.6719566846141796,0.6719566846141796,-0.7273182551899271,0.7273182551899271,-0.7783056514265194,0.7783056514265194,-0.8246122308333117,0.8246122308333117,-0.8659595032122595,0.8659595032122595,-0.9020988069688743,0.9020988069688743,-0.9328128082786765,0.9328128082786765,-0.9579168192137917,0.9579168192137917,-0.9772599499837743,0.9772599499837743,-0.990726238699457,0.990726238699457,-0.9982377097105593,0.9982377097105593]
elif order==100:
Gauss_nodes = [-0.0243502926634244, 0.0243502926634244, -0.072993121787799, 0.072993121787799, -0.1214628192961206, 0.1214628192961206, -0.1696444204239928, 0.1696444204239928, -0.2174236437400071, 0.2174236437400071, -0.2646871622087674, 0.2646871622087674, -0.311322871990211, 0.311322871990211, -0.3572201583376681, 0.3572201583376681, -0.4022701579639916, 0.4022701579639916, -0.4463660172534641, 0.4463660172534641, -0.489403145707053, 0.489403145707053, -0.5312794640198946, 0.5312794640198946, -0.571895646202634, 0.571895646202634, -0.6111553551723933, 0.6111553551723933, -0.6489654712546573, 0.6489654712546573, -0.6852363130542333, 0.6852363130542333, -0.7198818501716109, 0.7198818501716109, -0.7528199072605319, 0.7528199072605319, -0.7839723589433414, 0.7839723589433414, -0.8132653151227975, 0.8132653151227975, -0.8406292962525803, 0.8406292962525803, -0.8659993981540928, 0.8659993981540928, -0.8893154459951141, 0.8893154459951141, -0.9105221370785028, 0.9105221370785028, -0.9295691721319396, 0.9295691721319396, -0.9464113748584028, 0.9464113748584028, -0.9610087996520538, 0.9610087996520538, -0.973326827789911, 0.973326827789911, -0.983336253884626, 0.983336253884626, -0.9910133714767443, 0.9910133714767443, -0.9963401167719553, 0.9963401167719553, -0.9993050417357722, 0.9993050417357722]
weights = [0.0486909570091397, 0.0486909570091397, 0.0485754674415034, 0.0485754674415034, 0.048344762234803, 0.048344762234803, 0.0479993885964583, 0.0479993885964583, 0.0475401657148303, 0.0475401657148303, 0.04696818281621, 0.04696818281621, 0.0462847965813144, 0.0462847965813144, 0.0454916279274181, 0.0454916279274181, 0.0445905581637566, 0.0445905581637566, 0.0435837245293235, 0.0435837245293235, 0.0424735151236536, 0.0424735151236536, 0.0412625632426235, 0.0412625632426235, 0.0399537411327203, 0.0399537411327203, 0.0385501531786156, 0.0385501531786156, 0.03705512854024, 0.03705512854024, 0.0354722132568824, 0.0354722132568824, 0.0338051618371416, 0.0338051618371416, 0.0320579283548516, 0.0320579283548516, 0.0302346570724025, 0.0302346570724025, 0.0283396726142595, 0.0283396726142595, 0.0263774697150547, 0.0263774697150547, 0.0243527025687109, 0.0243527025687109, 0.0222701738083833, 0.0222701738083833, 0.0201348231535302, 0.0201348231535302, 0.0179517157756973, 0.0179517157756973, 0.0157260304760247, 0.0157260304760247, 0.0134630478967186, 0.0134630478967186, 0.0111681394601311, 0.0111681394601311, 0.0088467598263639, 0.0088467598263639, 0.0065044579689784, 0.0065044579689784, 0.0041470332605625, 0.0041470332605625, 0.0017832807216964, 0.0017832807216964]
else:
Gauss_nodes = [-0.99971372677344123367822847, -0.99849195063959581840016336, -0.99629513473312514918613173, -0.99312493703744345965200989, -0.98898439524299174800441875, -0.98387754070605701549610016, -0.97780935848691828855378109, -0.97078577576370633193089786, -0.96281365425581552729365933, -0.95390078292549174284933693, -0.94405587013625597796277471, -0.93328853504307954592433367, -0.92160929814533395266695133, -0.90902957098252969046712634, -0.89556164497072698669852102, -0.88121867938501841557331683, -0.86601468849716462341074, -0.84996452787959128429336259, -0.83308387988840082354291583, -0.8153892383391762543939888, -0.7968978923903144763895729, -0.7776279096494954756275514, -0.757598118519707176035668, -0.73682808980202070551242772, -0.71533811757305644645996712, -0.69314919935580196594864794, -0.67028301560314101580258701, -0.6467619085141292798326303, -0.62260886020370777160419085, -0.59784747024717872126480655, -0.57250193262138119131687044, -0.54659701206509416746799426, -0.52015801988176305664681575, -0.4932107892081909335693088, -0.4657816497733580422492166, -0.43789740217203151310897804, -0.4095852916783015425288684, -0.3808729816246299567633625, -0.35178852637242172097234383, -0.32236034390052915172247658, -0.2926171880384719647375559, -0.26258812037150347916892934, -0.23230248184497396964950996, -0.20178986409573599723604886, -0.17108008053860327488753238, -0.14020313723611397320751461, -0.10918920358006111500342601, -0.07806858281343663669481737, -0.046871682421591631614923913, -0.0156289844215430828722167, 0.0156289844215430828722167, 0.04687168242159163161492391, 0.07806858281343663669481737, 0.10918920358006111500342601, 0.14020313723611397320751461, 0.17108008053860327488753238, 0.20178986409573599723604886, 0.23230248184497396964950996, 0.2625881203715034791689293, 0.29261718803847196473755589, 0.3223603439005291517224766, 0.35178852637242172097234383, 0.38087298162462995676336255, 0.4095852916783015425288684, 0.437897402172031513108978, 0.4657816497733580422492166, 0.4932107892081909335693088, 0.52015801988176305664681575, 0.54659701206509416746799426, 0.57250193262138119131687044, 0.59784747024717872126480655, 0.62260886020370777160419085, 0.6467619085141292798326303, 0.67028301560314101580258701, 0.69314919935580196594864794, 0.71533811757305644645996712, 0.73682808980202070551242772, 0.75759811851970717603566796, 0.77762790964949547562755139, 0.79689789239031447638957288, 0.81538923833917625439398876, 0.83308387988840082354291583, 0.84996452787959128429336259, 0.86601468849716462341073997, 0.88121867938501841557331683, 0.89556164497072698669852102, 0.90902957098252969046712634, 0.92160929814533395266695133, 0.93328853504307954592433367, 0.94405587013625597796277471, 0.95390078292549174284933693, 0.96281365425581552729365933, 0.97078577576370633193089786, 0.97780935848691828855378109, 0.98387754070605701549610016, 0.98898439524299174800441875, 0.99312493703744345965200989, 0.99629513473312514918613173, 0.99849195063959581840016336, 0.99971372677344123367822847]
weights = [7.3463449050567173040632E-4, 0.0017093926535181052395294, 0.0026839253715534824194396, 0.00365596120132637518234246, 0.004624450063422119351095789, 0.00558842800386551515721195, 0.0065469484508453227641521, 0.00749907325546471157882874, 0.00844387146966897140262083, 0.00938041965369445795141824, 0.0103078025748689695857821, 0.01122511402318597711722157, 0.0121314576629794974077448, 0.01302594789297154228555858, 0.01390771070371877268795415, 0.01477588452744130176887999, 0.01562962107754600272393687, 0.01646808617614521264310498, 0.0172904605683235824393442, 0.0180959407221281166643908, 0.01888373961337490455294117, 0.01965308749443530586538147, 0.02040323264620943276683885, 0.0211334421125276415426723, 0.02184300241624738631395374, 0.022531220256336272701796971, 0.02319742318525412162248885, 0.0238409602659682059625604, 0.024461202707957052719975, 0.0250575444815795897037642, 0.02562940291020811607564201, 0.02617621923954567634230874, 0.02669745918357096266038466, 0.02719261344657688013649157, 0.02766119822079238829420416, 0.02810275565910117331764833, 0.02851685432239509799093676, 0.0289030896011252031348762, 0.02926108411063827662011902, 0.02959048805991264251175451, 0.02989097959333283091683681, 0.0301622651051691449190687, 0.03040407952645482001650786, 0.03061618658398044849645944, 0.0307983790311525904277139, 0.0309504788504909882340635, 0.03107233742756651658781017, 0.03116383569620990678381832, 0.0312248842548493577323765, 0.0312554234538633569476425, 0.03125542345386335694764247, 0.0312248842548493577323765, 0.0311638356962099067838183, 0.03107233742756651658781017, 0.0309504788504909882340635, 0.0307983790311525904277139, 0.03061618658398044849645944, 0.03040407952645482001650786, 0.03016226510516914491906868, 0.02989097959333283091683681, 0.0295904880599126425117545, 0.029261084110638276620119, 0.02890308960112520313487623, 0.0285168543223950979909368, 0.0281027556591011733176483, 0.0276611982207923882942042, 0.0271926134465768801364916, 0.02669745918357096266038466, 0.02617621923954567634230874, 0.025629402910208116075642, 0.0250575444815795897037642, 0.024461202707957052719975, 0.02384096026596820596256041, 0.0231974231852541216224889, 0.022531220256336272701797, 0.0218430024162473863139537, 0.0211334421125276415426723, 0.0204032326462094327668389, 0.0196530874944353058653815, 0.0188837396133749045529412, 0.01809594072212811666439075, 0.0172904605683235824393442, 0.01646808617614521264310498, 0.01562962107754600272393687, 0.01477588452744130176888, 0.013907710703718772687954149, 0.0130259478929715422855586, 0.0121314576629794974077448, 0.01122511402318597711722157, 0.0103078025748689695857821, 0.00938041965369445795141824, 0.0084438714696689714026208, 0.00749907325546471157882874, 0.0065469484508453227641521, 0.00558842800386551515721195, 0.00462445006342211935109579, 0.003655961201326375182342459, 0.00268392537155348241943959, 0.001709392653518105239529358, 7.34634490505671730406321E-4]
weights = np.array(weights)
Gauss_nodes = np.array(Gauss_nodes)
return(weights, Gauss_nodes)
# ---------------------------------------------------------------------------------
# FromSigmaToAbundance---------------------------------------------------------------------
def FromSigmaToAbundance(t, sigmaX, chemdict):
'''Computes the abundance ratio [X/H] at the time t from the density of the X-element sigmaX.'''
sigma_gas = Get_psi(t, chemdict)/chemdict["nuL"];
abundance = _safelog10(sigmaX, sigma_gas)
return(abundance)
# -------------------------# End of FromSigmaToAbundance ----------------------------------
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# MODEL FUNCTION DEFINITIONS
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# psi ---------------------------------------------------------------------------------
def Get_psi(t, chemdict):
'''Psi function Get_psi(t, chemdict)
Returns the SFR'''
# Extract the values of the parameters:
omega = chemdict["omega"];
R = chemdict["R"];
nuL = chemdict["nuL"];
tauj = chemdict["tauj"];
tj = chemdict["tj"];
assert(len(tauj)==len(tj)), "tj, tauj lengths mismatch"
Aj = chemdict["Aj"]
assert(len(tauj)==len(Aj)), "Aj, tauj lengths mismatch"
sigma_gas_0 = chemdict["sigma_gas_0"]
# Useful definitions
alpha = (1.+omega-R)*nuL
inv_alpha = 1./alpha;
N = len(tj)
# Evaluate each infall
value = 0.;
for j in range(N):
deltatj = t-tj[j];
if tauj[j]!=inv_alpha:
# Case when alpha!=1/tauj[j]:
value += Aj[j]/(alpha-1./tauj[j])*heaviside(deltatj)*( np.exp(-deltatj/tauj[j] )-np.exp(-alpha*deltatj) )
else:
# Special case when alpha=1/tauj[j]:
value += Aj[j]*heaviside(deltatj)*deltatj*np.exp(-alpha*deltatj)
# Add the zero-th case
value += + sigma_gas_0*np.exp(-alpha*t)*heaviside(t)
# Multiply by nuL
value = nuL*value;
return(value)
# -------------------------# End of psi -------------------------------------------------
# ipsi ------------------------------------------------------------------------------------
def Get_ipsi(t, chemdict):
'''Get_ipsi(t, chemdict)
Returns a primitive of psi(t) function evaluated at time t'''
assert(0), "Add the tauj=1/alpha case!!!!"
# Extract the values of the parameters:
omega = chemdict["omega"];
R = chemdict["R"];
nuL = chemdict["nuL"];
tauj = chemdict["tauj"]
tj = chemdict["tj"];
assert(len(tauj)==len(tj)), "tj, tauj lengths mismatch"
N = len(tj)
Aj = chemdict["Aj"]
assert(len(tauj)==len(Aj)), "Aj, tauj lengths mismatch"
sigma_gas_0 = chemdict["sigma_gas_0"]
# Useful definitions
alpha = (1.+omega-R)*nuL
inv_alpha = 1./alpha; # The inverse of alpha
# Now separate the cases tauj!=1/alpha and tauj==1/alpha:
(tauj, tj, Aj, tj_gorro, Aj_gorro) = _separate_cases(alpha, tauj, tj, Aj)
N, N_gorro = len(tj), len(tj_gorro)
Kj = [nuL*Aj[j]/(alpha-1./tauj[j]) for j in range(N)]
value = 0.;
# Case when alpha!=1/tauj[j]:
# 3) Integration of Psi (alpha!=1/tauj[j]):
IntPsi = 0.;
for j in range(N):
deltatj = t-tj[j]
# Positive j:
IntPsi += Kj[j]*heaviside(deltatj)*tauj[j]*(1.-np.exp(-deltatj/tauj[j]))
# Negative j:
IntPsi -= Kj[j]*heaviside(deltatj)/alpha*(1.-np.exp(-deltatj*alpha))
# "Zero" term:
IntPsi += nuL*sigma_gas_0*heaviside(t)/alpha*(1.-np.exp(-t*alpha))
# 4) Integration of Psi (alpha!=1/tauj[j]):
for j in range(N_gorro):
deltatj = t-tj_gorro[j]
# Unique term:
IntPsi += nuL*Aj_gorro[j]*heaviside(deltatj)*(inv_alpha**2)*(1.-(1.+alpha*deltatj)*np.exp(-alpha*deltatj))
return(IntPsi)
# -------------------------# End of ipsi -------------------------------------------------
# Get_DTD ------------------------------------------------------------------------------------
def Get_DTD(t, chemdict):
''' Returns the DTD evaluated at time t
y = DTD(t, chemdict).
Deprecated. Use Get_DTD_arr'''
DTD_value = Get_DTD_arr(t, chemdict)
return(DTD_value)
# -------------------------# End of DTD -------------------------------------------------
# Get_DTD_arr ------------------------------------------------------------------------------------
def Get_DTD_arr(t, chemdict):
''' Returns the DTD evaluated at time t
y = Get_DTD_arr(t, chemdict)'''
chemdict = prepare_chemdict( chemdict )
#-------------------------------------
# Gaussian DTD parameters
AG_arr = chemdict.get("AG",[])
Ng = len(AG_arr)# Get the number of Gaussian
sigma_p_arr = chemdict.get("sigma_p",[])
taup_arr = chemdict.get("taup",[])
tau1G_arr = chemdict.get("tau1G",[])
tau2G_arr = chemdict.get("tau2G",[])
# Exponential DTD parameters
AE_arr = chemdict.get("AE",[])
Ne = len(AE_arr)# Get the number of Exponentials
tauD_arr = chemdict.get("tauD",[])
tau1E_arr = chemdict.get("tau1E",[])
tau2E_arr = chemdict.get("tau2E",[])
# Inverse DTD parameters
AI_arr = chemdict.get("AI",[])
Ni = len(AI_arr)# Get the number of 1/t DTD terms
tauI_arr = chemdict.get("tauI", Ni*[1.]) # Since it is degenerated with Ai, we set it to one by default
tau0_arr = chemdict["tau0"];
tau1I_arr = chemdict.get("tau1I",[])
tau2I_arr = chemdict.get("tau2I",[])
#-------------------------------------
# Safety checks:
assert( (len(sigma_p_arr)==Ng) and (len(taup_arr)==Ng) and (len(tau1G_arr)==Ng) and (len(tau2G_arr)==Ng)), "Dimensions of Gaussian DTD parameters mismatch"
assert( (len(tauD_arr)==Ne) and (len(tau1E_arr)==Ne) and (len(tau2E_arr)==Ne) ), "Dimensions of Exponential DTD parameters mismatch"
assert( (len(tauI_arr)==Ni) and (len(tau0_arr)==Ni) and (len(tau1I_arr)==Ni) and (len(tau2I_arr)==Ni) ), "Dimensions of 1/t DTD parameters mismatch"
#-------------------------------------
# ------------------------------------
DTD_value = np.zeros_like(t) # Cumulative value
# The Gaussian term
for i in range(Ng):
AG = AG_arr[i]
if AG==0: continue
# Extract the DTD parameters:
sigma_p = sigma_p_arr[i]
taup = taup_arr[i]
tau1 = tau1G_arr[i]
tau2 = tau2G_arr[i]
DTD_value += AG*np.exp(-0.5*((t-taup)/sigma_p)**2 )*(t>=tau1)*(t<tau2);
# The Exponential term
for i in range(Ne):
AE = AE_arr[i]
if AE==0: continue
# Extract the DTD parameters:
tauD = tauD_arr[i]
tau1 = tau1E_arr[i]
tau2 = tau2E_arr[i]
DTD_value += AE*np.exp(-t/tauD)*(t>=tau1)*(t<tau2);
# The 1/(t-tau0) term
for i in range(Ni):
AI = AI_arr[i]
if AI==0: continue
# Extract the DTD parameters:
tauI = tauI_arr[i]
tau0 = tau0_arr[i]
tau1 = tau1I_arr[i]
tau2 = tau2I_arr[i]
assert(tau0<tau1), " ERROR: tau0 must be LOWER than tau1"
DTD_value += AI*(tauI/((t-tau0)*(t>tau0)+1.*(t<=tau0)))*(t!=tau0)*(t>=tau1)*(t<tau2);
return(DTD_value)
# -------------------------# End of Get_DTD_arr -------------------------------------------------
# Get_infall--------------------------------
def Get_infall(t, chemdict):
''' Computes the infall given by the t, Aj, and tauj parameters'''
t = np.array(t);
I = 0.*t;
# Read the infall parameters:
tauj = chemdict.get("tauj", None)
tj = chemdict.get("tj", None)
Aj = chemdict.get("Aj", None)
assert(len(tj)==len(Aj)),"len(tj) is not equal to len(Aj)"
assert(len(tj)==len(tauj)),"len(tj) is not equal to len(tauj)"
for j in range(len(Aj)): I = Aj[j]*np.exp(-(t-tj[j])*heaviside(t-tj[j])/tauj[j])*heaviside(t-tj[j]);
return(I);
# -------------------------# End of Get_infall -------------------------------------------------
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# R1a ASSOCIATED FUNCTIONS
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# R1a_numeric------------------------
def R1a_numeric(t, chemdict):
''' Numeric solution for R1a(t)
R1a_numeric(t, chemdict) '''
chemdict = prepare_chemdict( chemdict )
# Extract the values of the parameters:
omega = chemdict["omega"];
R = chemdict["R"];
nuL = chemdict["nuL"];
tauj = chemdict["tauj"]
tj = chemdict["tj"];
assert(len(tauj)==len(tj)), "tj, tauj lengths mismatch"
Ninfall = len(tj)
Aj = chemdict["Aj"]
sigma_gas_0 = chemdict["sigma_gas_0"]
# DTD
CIa = chemdict["CIa"]
# Gaussian DTD parameters
AG_arr = chemdict["AG"]
Ng = len(AG_arr)# Get the number of Gaussians
sigma_p_arr = chemdict["sigma_p"]
taup_arr = chemdict["taup"]
tau1G_arr = chemdict["tau1G"]
tau2G_arr = chemdict["tau2G"]
# Exponential DTD parameters
AE_arr = chemdict["AE"]
Ne = len(AE_arr)# Get the number of Exponentials
tauD_arr = chemdict["tauD"]
tau1E_arr = chemdict["tau1E"]
tau2E_arr = chemdict["tau2E"]
# Inverse DTD parameters
AI_arr = chemdict["AI"]
Ni = len(AI_arr)# Get the number of Inverses
tauI_arr = chemdict["tauI"]
tau0_arr = chemdict["tau0"]
tau1I_arr = chemdict["tau1I"]
tau2I_arr = chemdict["tau2I"]
#-------------------------------------
#-------------------------------------
# Safety checks:
assert(len(tauj)==Ninfall), "tj, tauj lengths mismatch"
assert(len(Aj)==Ninfall), "tj, Aj lengths mismatch"
assert( (len(sigma_p_arr)==Ng) and (len(taup_arr)==Ng) and (len(tau1G_arr)==Ng) and (len(tau2G_arr)==Ng)), "Dimensions of Gaussian DTD parameters mismatch"
assert( (len(tauD_arr)==Ne) and (len(tau1E_arr)==Ne) and (len(tau2E_arr)==Ne) ), "Dimensions of Exponential DTD parameters mismatch"
assert( (len(tauI_arr)==Ni) and (len(tau0_arr)==Ni) and (len(tau1I_arr)==Ni) and (len(tau2I_arr)==Ni) ), "Dimensions of 1/t DTD parameters mismatch"
#-------------------------------------
# Useful definitions
Ninfall = len(tj)# Number of infalls
alpha = (1.+omega-R)*nuL
betaj = [alpha - 1./tauj[j] for j in range(Ninfall)]
# Load the weights and the nodes
weights, Gauss_nodes = GaussWeightsAndNodes()
# We want t to be an array:
try:
len(t)
except:
t = [t];
#-------------------------------------
# Numerical integration of the R1a term
Integr = np.zeros(len(t), dtype=np.float32);
for t_n, t_now in enumerate(t):
tau_nodes = 0.5*t_now*(1.+Gauss_nodes); # Nodes of integration
DTD = Get_DTD_arr(tau_nodes, chemdict) # Perfomed over all the i-th individual DTDs
psi_t_tau = 0.*tau_nodes
for j in range(Ninfall):
deltatj = t_now-tj[j]
if betaj[j]!=0:
# The most common case
inv_betaj = 1./betaj[j];
psi_t_tau += Aj[j]*inv_betaj*heaviside(deltatj-tau_nodes)*( np.exp(-(deltatj-tau_nodes)/tauj[j] )-np.exp(-alpha*(deltatj-tau_nodes)) )
else:
# The peculiar case when alpha=1/tauj[j]
psi_t_tau += Aj[j]*(deltatj-tau_nodes)*heaviside(deltatj-tau_nodes)*np.exp(-alpha*(deltatj-tau_nodes))
# Zero term:
psi_t_tau += sigma_gas_0*heaviside(t_now-tau_nodes)*np.exp(-alpha*(t_now-tau_nodes))
Integr_tau = DTD*psi_t_tau*(t_now>0);
Integr[t_n] = np.dot(weights, Integr_tau)*(0.5*t_now);
# Multiply by nuL and CIa:
Integr = CIa*nuL*Integr;
return(Integr)
# ---------------------------------------------------------------
# R1a_analytic_gaussian------------------------
def R1a_analytic_gaussian(t, alpha, nuL, Aj, tauj, tj, sigma_gas_0, CIa, AG, taup, sigma_p, tau1, tau2):
''' Exact solution for R1a(t) using Gaussian DTD
R1a_analytic_gaussian(t, alpha, nuL, Aj, tauj, tj, sigma_gas_0, CIa, AG, taup, sigma_p, tau1, tau2) '''
# Extract the values of the parameters:
N = len(tj)
#-------------------------------------
# Safety checks:
assert(len(tauj)==len(tj)), "tj, tauj lengths mismatch"
assert(len(Aj)==len(tj)), "tj, Aj lengths mismatch"
assert(tau1<tau2)," Error: tau2 must be GREATER than tau1"
#-------------------------------------
# Useful definitions
Ninfall = len(tj)# Number of infalls
inv_alpha = 1./alpha;
betaj = [alpha - 1./tauj[j] for j in range(Ninfall)]
# 1) R1a Gaussian term
# ---------
R1a_g = 0.
# Useful definitions
etaalpha = taup + sigma_p**2*alpha
# Useful param.
R1a_gj = np.zeros(N+1, dtype=np.float32).tolist();
for j in range(Ninfall):
deltatj = t - tj[j]
mint1 = deltatj*(deltatj<tau1) + tau1*(deltatj>=tau1)
mint2 = deltatj*(deltatj<tau2) + tau2*(deltatj>=tau2)
if(betaj[j]!=0):
# Useful definitions
etaj = taup + sigma_p**2/tauj[j]
Kj_gorro = Aj[j]/betaj[j]*np.exp(taup/tauj[j] + 0.5*sigma_p**2/tauj[j]**2)
Kjalpha_gorro = Aj[j]/betaj[j]*np.exp(taup*alpha + 0.5*sigma_p**2*alpha**2)
# Positive j:
R1a_gj[j] += Kj_gorro*np.sqrt(np.pi*0.5)*np.exp(-deltatj/tauj[j])*(erf( (mint2-etaj)/np.sqrt(2.)/sigma_p )-erf( (mint1-etaj)/np.sqrt(2.)/sigma_p ))*heaviside(deltatj)
# Negative j:
R1a_gj[j] -= Kjalpha_gorro*np.sqrt(np.pi*0.5)*np.exp(-alpha*deltatj)*(erf( (mint2-etaalpha)/np.sqrt(2.)/sigma_p )-erf( (mint1-etaalpha)/np.sqrt(2.)/sigma_p ))*heaviside(deltatj)
else:
# Unique term:
R1a_gj[j] += Aj[j]*np.exp(-alpha*(deltatj-taup-0.5*alpha*sigma_p**2))*(np.sqrt(0.5*np.pi)*(deltatj-etaalpha)*(erf((mint2-etaalpha)/(np.sqrt(2)*sigma_p))-erf((mint1-etaalpha)/(np.sqrt(2)*sigma_p))) + sigma_p*(np.exp(-0.5*((mint2-etaalpha)/sigma_p)**2)-np.exp(-0.5*((mint1-etaalpha)/sigma_p)**2) ) )*heaviside(deltatj)
print(" Unique term used in the Gaussian DTD")
# "Zero" term:
mint1 = t*(t<tau1) + tau1*(t>=tau1)
mint2 = t*(t<tau2) + tau2*(t>=tau2)
R1a_gj[-1] = sigma_gas_0*np.exp(-alpha*t)*np.sqrt(np.pi*0.5)*np.exp(taup*alpha+0.5*sigma_p**2*alpha**2)*(erf( (mint2-etaalpha)/np.sqrt(2.)/sigma_p )-erf( (mint1-etaalpha)/np.sqrt(2.)/sigma_p ))*heaviside(t)
# Constant factor (including nuL)
for j in range(Ninfall+1): R1a_gj[j] = CIa*AG*sigma_p*nuL*R1a_gj[j];
R1a_g += np.array(R1a_gj).sum(0); # Sumation
del(R1a_gj)
return(R1a_g)
# -------------------------# End of R1a_analytic_gaussian -------------------------------------------------
# R1a_analytic_exponential------------------------
def R1a_analytic_exponential(t, alpha, nuL, Aj, tauj, tj, sigma_gas_0, CIa, AE, tauD, tau1, tau2):
''' Exact solution for R1a(t) using the Exponential DTD
R1a_analytic_exponential(t, alpha, nuL, Aj, tauj, tj, sigma_gas_0, CIa, AE, tauD, tau1, tau2) '''
# Extract the values of the parameters:
N = len(tj)
#-------------------------------------
# Safety checks:
assert(len(tauj)==len(tj)), "tj, tauj lengths mismatch"
assert(len(Aj)==len(tj)), "tj, Aj lengths mismatch"
assert(tau1<tau2)," Error: tau2 must be GREATER than tau1"
#-------------------------------------
# Useful definitions
Ninfall = len(tj)# Number of infalls
betaj = [alpha - 1./tauj[j] for j in range(Ninfall)]
inv_alpha = 1./alpha;
# 1) R1a Exponential term
# ---------
R1a_e = 0.
R1a_ej = np.zeros(N+1, dtype=np.float32).tolist();
for j in range(Ninfall):
deltatj = t - tj[j]
mint1 = deltatj*(deltatj<tau1) + tau1*(deltatj>=tau1)
mint2 = deltatj*(deltatj<tau2) + tau2*(deltatj>=tau2)
if betaj[j]!=0:
# Qj will discern the special case with taueff-> inf
# Positive j:
R1a_ej[j] += Aj[j]/betaj[j]*(QD(mint1, tauj[j], tauD)-QD(mint2, tauj[j], tauD))*np.exp(-deltatj/tauj[j])*heaviside(deltatj)
# Negative j:
R1a_ej[j] -= Aj[j]/betaj[j]*(QD(mint1, inv_alpha, tauD)-QD(mint2, inv_alpha, tauD))*np.exp(-deltatj*alpha)*heaviside(deltatj)
else:
# Unique term
R1a_ej[j] += Aj[j]*np.exp(-alpha*deltatj)*( QD_gorro(mint2, deltatj, inv_alpha, tauD)-QD_gorro(mint1, deltatj, inv_alpha, tauD) )
print(" Unique term used in the Exponential DTD")
# "Zero" term:
mint1 = t*(t<tau1) + tau1*(t>=tau1)
mint2 = t*(t<tau2) + tau2*(t>=tau2)
R1a_ej[-1] += sigma_gas_0*np.exp(-t*alpha)*( QD(mint1, inv_alpha, tauD)-QD(mint2, inv_alpha, tauD) )*heaviside(t)
# Constant factor
for j in range(Ninfall+1): R1a_ej[j] = CIa*AE*nuL*R1a_ej[j];# Including nuL
R1a_e += np.array(R1a_ej).sum(0); # Sumation
del(R1a_ej)
return(R1a_e)
# -------------------------# End of R1a_analytic_exponential -------------------------------------------------
# R1a_analytic_inverse------------------------
def R1a_analytic_inverse(t, alpha, nuL, Aj, tauj, tj, sigma_gas_0, CIa, AI, tauI, tau0, tau1, tau2):
''' Exact solution for R1a(t) using the Inverse of t DTD
R1a_analytic_inverse(t, alpha, nuL, Aj, tauj, tj, sigma_gas_0, CIa, AI, tauI, tau0, tau1, tau2) '''
# Extract the values of the parameters:
N = len(tj)
#-------------------------------------
# Safety checks:
assert(len(tauj)==len(tj)), "tj, tauj lengths mismatch"
assert(len(Aj)==len(tj)), "tj, Aj lengths mismatch"
assert(tau1<tau2)," Error: tau2 must be GREATER than tau1"
assert(tau0<tau1)," Error: tau0 must be LOWER than tau1"
#-------------------------------------
# Useful definitions
Ninfall = len(tj)# Number of infalls
inv_alpha = 1./alpha;
betaj = [alpha - 1./tauj[j] for j in range(Ninfall)]
# 1) R1a Inverse term
# ---------
R1a_i = 0.
# Offset correction:
t = t - tau0;
tau1 = tau1 - tau0;
tau2 = tau2 - tau0;
R1a_ij = np.zeros(N+1, dtype=np.float32).tolist();
for j in range(Ninfall):
deltatj = t - tj[j]
mint1 = deltatj*(deltatj<tau1) + tau1*(deltatj>=tau1)
mint2 = deltatj*(deltatj<tau2) + tau2*(deltatj>=tau2)
if betaj[j]!=0:
# Positive j:
R1a_ij[j] += Aj[j]/betaj[j]*heaviside(deltatj-tau1)*(np.exp(-deltatj/tauj[j])*_safeexpi(mint2/tauj[j])-np.exp(-alpha*deltatj)*_safeexpi(alpha*mint2))
# Negative j:
R1a_ij[j] -= Aj[j]/betaj[j]*heaviside(deltatj-tau1)*(np.exp(-deltatj/tauj[j])*_safeexpi(tau1/tauj[j])-np.exp(-alpha*deltatj)*_safeexpi(alpha*tau1))
else:
# Unique term:
R1a_ij[j] += Aj[j]*( deltatj*np.exp(-alpha*deltatj)*(_safeexpi(alpha*mint2)-_safeexpi(alpha*mint1)) + inv_alpha*np.exp(alpha*(mint1-deltatj)) - inv_alpha*np.exp(alpha*(mint2-deltatj)) )
print(" Unique term used in the inverse DTD")
# "Zero" term:
mint1 = t*(t<tau1) + tau1*(t>=tau1)
mint2 = t*(t<tau2) + tau2*(t>=tau2)
R1a_ij[-1] += sigma_gas_0*np.exp(-alpha*t)*(_safeexpi(mint2*alpha)-_safeexpi(mint1*alpha))*heaviside(t)
# Constant factor
for j in range(Ninfall+1): R1a_ij[j] = CIa*tauI*AI*nuL*R1a_ij[j];# Includes the nuL term
R1a_i += np.array(R1a_ij).sum(0); # Sumation
del(R1a_ij)
return(R1a_i)
# -------------------------# End of R1a_analytic_inverse -------------------------------------------------
# R1a_analytic------------------------
def R1a_analytic(t, chemdict, separated_terms=False):
''' Exact solution for R1a(t)
R1a_analytic(t, chemdict)
When separated_terms is True, the output is a 3-element array'''
chemdict = prepare_chemdict( chemdict )
# Extract the values of the parameters:
omega = chemdict["omega"];
R = chemdict["R"];
nuL = chemdict["nuL"];
tauj = chemdict["tauj"]
tj = chemdict["tj"];
assert(len(tauj)==len(tj)), "tj, tauj lengths mismatch"
N = len(tj)
Aj = chemdict["Aj"]
sigma_gas_0 = chemdict["sigma_gas_0"]
# DTD
CIa = chemdict["CIa"]
#-------------------------------------
#-------------------------------------
# Safety checks:
assert(len(tauj)==len(tj)), "tj, tauj lengths mismatch"
assert(len(Aj)==len(tj)), "tj, Aj lengths mismatch"
#-------------------------------------
# Useful definitions
alpha = (1.+omega-R)*nuL
gamma = nuL*(1.+omega-R);
# 1) R1a Gaussian term
# ---------
# Gaussian DTD parameters
AG_arr = chemdict["AG"]
Ng = len(AG_arr)# Get the number of Gaussians
sigma_p_arr = chemdict["sigma_p"]
taup_arr = chemdict["taup"]
tau1G_arr = chemdict["tau1G"]
tau2G_arr = chemdict["tau2G"]
assert( (len(sigma_p_arr)==Ng) and (len(taup_arr)==Ng) and (len(tau1G_arr)==Ng) and (len(tau2G_arr)==Ng)), "Dimensions of Gaussian DTD parameters mismatch"
R1a_g = 0. # Cumulative value
for i in range(Ng):
AG = AG_arr[i]
taup = taup_arr[i]
sigma_p = sigma_p_arr[i]
tau1 = tau1G_arr[i]
tau2 = tau2G_arr[i]
R1a_g += R1a_analytic_gaussian(t, alpha, nuL, Aj, tauj, tj, sigma_gas_0, CIa, AG, taup, sigma_p, tau1, tau2)
# 2) R1a Exponential term
# ---------
# Exponential DTD parameters
AE_arr = chemdict["AE"]
Ne = len(AE_arr)# Get the number of Exponentials
tauD_arr = chemdict["tauD"]
tau1E_arr = chemdict["tau1E"]
tau2E_arr = chemdict["tau2E"]
assert( (len(tauD_arr)==Ne) and (len(tau1E_arr)==Ne) and (len(tau2E_arr)==Ne) ), "Dimensions of Exponential DTD parameters mismatch"
R1a_e = 0.# Cumulative value
for i in range(Ne):
AE = AE_arr[i]
tauD = tauD_arr[i]
tau1 = tau1E_arr[i]
tau2 = tau2E_arr[i]
R1a_e += R1a_analytic_exponential(t, alpha, nuL, Aj, tauj, tj, sigma_gas_0, CIa, AE, tauD, tau1, tau2);
# 3) R1a Inverse term
# ---------
# Inverse DTD parameters
AI_arr = chemdict["AI"]
Ni = len(AI_arr)# Get the number of Inverses
tauI_arr = chemdict["tauI"]
tau0_arr = chemdict["tau0"]
tau1I_arr = chemdict["tau1I"]
tau2I_arr = chemdict["tau2I"]
assert( (len(tauI_arr)==Ni) and (len(tau0_arr)==Ni) and (len(tau1I_arr)==Ni) and (len(tau2I_arr)==Ni) ), "Dimensions of 1/t DTD parameters mismatch"
R1a_i = 0.# Cumulative value
for i in range(Ni):
AI = AI_arr[i]
tauI = tauI_arr[i]
tau0 = tau0_arr[i]
tau1 = tau1I_arr[i]
tau2 = tau2I_arr[i]
R1a_i += R1a_analytic_inverse(t, alpha, nuL, Aj, tauj, tj, sigma_gas_0, CIa, AI, tauI, tau0, tau1, tau2)
# Prepare the output
if separated_terms:
return([R1a_g,R1a_e,R1a_i])
else:
return(R1a_g+R1a_e+R1a_i)
# -------------------------# End of R1a_analytic -------------------------------------------------
# iR1a_analytic----------------------------------------------------------------------------------
def iR1a_analytic(t, chemdict):
chemdict = prepare_chemdict( chemdict )
# Extract the values of the parameters:
omega = chemdict["omega"];
R = chemdict["R"];
nuL = chemdict["nuL"];
tauj = chemdict["tauj"]
tj = chemdict["tj"];
assert(len(tauj)==len(tj)), "tj, tauj lengths mismatch"
N = len(tj)
Aj = chemdict["Aj"]
sigma_gas_0 = chemdict["sigma_gas_0"]
# DTD
CIa = chemdict["CIa"]
# Gaussian DTD parameters
AG_arr = chemdict["AG"]
Ng = len(AG_arr)# Get the number of Gaussians
sigma_p_arr = chemdict["sigma_p"]
taup_arr = chemdict["taup"]
tau1G_arr = chemdict["tau1G"]
tau2G_arr = chemdict["tau2G"]
# Exponential DTD parameters
AE_arr = chemdict["AE"]
Ne = len(AE_arr)# Get the number of Exponentials
tauD_arr = chemdict["tauD"]
tau1E_arr = chemdict["tau1E"]
tau2E_arr = chemdict["tau2E"]
# Inverse DTD parameters
AI_arr = chemdict["AI"]
Ni = len(AI_arr)# Get the number of Inverses
tauI_arr = chemdict["tauI"]
tau0_arr = chemdict["tau0"]
tau1I_arr = chemdict["tau1I"]
tau2I_arr = chemdict["tau2I"]
#-------------------------------------
#-------------------------------------
# Safety checks:
assert(len(tauj)==len(tj)), "tj, tauj lengths mismatch"
assert(len(Aj)==len(tj)), "tj, Aj lengths mismatch"
assert( (len(sigma_p_arr)==Ng) and (len(taup_arr)==Ng) and (len(tau1G_arr)==Ng) and (len(tau2G_arr)==Ng)), "Dimensions of Gaussian DTD parameters mismatch"
assert( (len(tauD_arr)==Ne) and (len(tau1E_arr)==Ne) and (len(tau2E_arr)==Ne) ), "Dimensions of Exponential DTD parameters mismatch"
assert( (len(tauI_arr)==Ni) and (len(tau0_arr)==Ni) and (len(tau1I_arr)==Ni) and (len(tau2I_arr)==Ni) ), "Dimensions of 1/t DTD parameters mismatch"
#-------------------------------------
# Useful definitions
Ninfall = len(tj)# Number of infalls
alpha = (1.+omega-R)*nuL
betaj = [- 1./tauj[j] for j in range(Ninfall)]
Kj = [nuL*Aj[j]/(alpha-1./tauj[j]) for j in range(Ninfall)]
# ------------------------------------------------------------------------
# 1) Non-Homogeneous non-trivial gaussian term:
global_nhg = 0.;
for i in range(Ng):
AG = AG_arr[i]
taup = taup_arr[i]
sigma_p = sigma_p_arr[i]
tau1 = tau1G_arr[i]
tau2 = tau2G_arr[i]
# Useful definitions
etaj = [(taup+sigma_p**2/tauj[j]) for j in range(Ninfall)]
Kj_gorro = [Kj[j]*np.exp(taup/tauj[j] + 0.5*sigma_p**2/tauj[j]**2) for j in range(Ninfall)]
Kjalpha_gorro = [Kj[j]*np.exp(taup*alpha + 0.5*sigma_p**2*alpha**2) for j in range(Ninfall)]
etaalpha = taup + sigma_p**2*alpha
aux_nhg = 0.;
for j in range(Ninfall):
deltatj = t - tj[j];
mint1 = deltatj*(deltatj<tau1) + tau1*(deltatj>=tau1)
mint2 = deltatj*(deltatj<tau2) + tau2*(deltatj>=tau2)
# From 0 to tau2:
aux_nhg += -Kj_gorro[j]*heaviside(deltatj-tau1)*tauj[j]*np.exp(betaj[j]*etaj[j]+0.5*betaj[j]**2*sigma_p**2)*(erf( (taup-mint2)/(np.sqrt(2.)*sigma_p))-erf( (taup-mint1)/(np.sqrt(2.)*sigma_p))); # Positive (first term)
aux_nhg += -Kj_gorro[j]*heaviside(deltatj-tau1)*tauj[j]*np.exp(betaj[j]*mint2)*(erf( (mint2-etaj[j])/(np.sqrt(2.)*sigma_p) )-erf( (tau1-etaj[j])/(np.sqrt(2.)*sigma_p) ))# Positive (second term)
aux_nhg += Kjalpha_gorro[j]*heaviside(deltatj-tau1)/alpha*np.exp(-alpha*etaalpha-0.5*alpha**2*sigma_p**2)*(erf( (-alpha*sigma_p**2+etaalpha-mint2)/(np.sqrt(2.)*sigma_p))-erf( (-alpha*sigma_p**2+etaalpha-mint1)/(np.sqrt(2.)*sigma_p))); # Negative (first term)
aux_nhg += Kjalpha_gorro[j]*heaviside(deltatj-tau1)/alpha*np.exp(-alpha*mint2)*(erf( (mint2-etaalpha)/(np.sqrt(2.)*sigma_p) )-erf( (tau1-etaalpha)/(np.sqrt(2.)*sigma_p) ))# Negative (second term)
# Zero term:
mint1 = t*(t<tau1) + tau1*(t>=tau1)
mint2 = t*(t<tau2) + tau2*(t>=tau2)
aux_nhg += -nuL*sigma_gas_0*heaviside(t-tau1)/alpha*np.exp(-alpha*etaalpha-0.5*alpha**2*sigma_p**2)*(erf( (-alpha*sigma_p**2+etaalpha-mint2)/(np.sqrt(2.)*sigma_p))-erf( (-alpha*sigma_p**2+etaalpha-mint1)/(np.sqrt(2.)*sigma_p))); # Zero term (first term)
aux_nhg -= nuL*sigma_gas_0*heaviside(t-tau1)/alpha*np.exp(-alpha*mint2)*(erf( (mint2-etaalpha)/(np.sqrt(2.)*sigma_p) )-erf( (tau1-etaalpha)/(np.sqrt(2.)*sigma_p) )) # Zero term (second term)
# From tau2 to t:
for j in range(Ninfall):
deltatj = t - tj[j];
# From tau2 to t:
aux_nhg += -Kj_gorro[j]*heaviside(deltatj-tau2)*tauj[j]*( erf( (tau2-etaj[j])/(np.sqrt(2.)*sigma_p)) - erf( (tau1-etaj[j])/(np.sqrt(2.)*sigma_p)) )*(np.exp(betaj[j]*deltatj)-np.exp(betaj[j]*tau2) ); # Positive (unique term)
aux_nhg += Kjalpha_gorro[j]*heaviside(deltatj-tau2)*alpha*( erf( (tau2-etaalpha)/(np.sqrt(2.)*sigma_p)) - erf( (tau1-etaalpha)/(np.sqrt(2.)*sigma_p)) )*(np.exp(-alpha*deltatj)-np.exp(-alpha*tau2) ) # Negative (unique term)
# Zero term:
aux_nhg -= nuL*sigma_gas_0*heaviside(t-tau2)*alpha*( erf( (tau2-etaalpha)/(np.sqrt(2.)*sigma_p)) - erf( (tau1-etaalpha)/(np.sqrt(2.)*sigma_p)) )*(np.exp(-alpha*t)-np.exp(-alpha*tau2) ) # Zero term (unique term)
global_nhg += np.sqrt(np.pi*0.5)*sigma_p*CIa*AG*aux_nhg# Multiply by the constants
# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
# 2) Non-Homogeneous non-trivial exponential term:
global_nhe = 0.;
for i in range(Ne):
AE = AE_arr[i]
tauD = tauD_arr[i]
tau1 = tau1E_arr[i]
tau2 = tau2E_arr[i]
# Useful definitions
taueffj = [ 1./(1./tauD-1./tauj[j]) for j in range(Ninfall)]
taualpha = 1./(1./tauD-alpha)
aux_nhe = 0.;
for j in range(Ninfall):
deltatj = t - tj[j];
mint2 = deltatj*(deltatj<tau2) + tau2*(deltatj>=tau2)
# From 0 to tau2:
aux_nhe += Kj[j]*taueffj[j]*heaviside(deltatj-tau1)*( -(np.exp(betaj[j]*mint2-tau1/taueffj[j])-np.exp(betaj[j]*tau1-tau1/taueffj[j]))*tauj[j] + tauD*(np.exp(-mint2/tauD)-np.exp(-tau1/tauD) ) )# Positive term
aux_nhe -= Kj[j]*taualpha*heaviside(deltatj-tau1)*( (np.exp(-alpha*mint2-tau1/taualpha)-np.exp(-tau1/tauD))/(-alpha) + tauD*(np.exp(-mint2/tauD)-np.exp(-tau1/tauD) ) )# Negative term
# Zero term:
mint2 = t*(t<tau2) + tau2*(t>=tau2)
aux_nhe += nuL*sigma_gas_0*taualpha*heaviside(t-tau1)*( (np.exp(-alpha*mint2-tau1/taualpha)-np.exp(-tau1/tauD))/(-alpha) + tauD*(np.exp(-mint2/tauD)-np.exp(-tau1/tauD) ) )# Zero term
# From tau2 to t:
for j in range(Ninfall):
deltatj = t - tj[j];
aux_nhe += -Kj[j]*taueffj[j]*heaviside(deltatj-tau2)*tauj[j]*(np.exp(-tau1/taueffj[j])-np.exp(-tau2/taueffj[j]))*(np.exp(-deltatj/tauj[j])-np.exp(-tau2/tauj[j])); # Positive (unique term)
aux_nhe -= -Kj[j]*taualpha*heaviside(deltatj-tau2)/alpha*(np.exp(-tau1/taualpha)-np.exp(-tau2/taualpha))*(np.exp(-alpha*deltatj)-np.exp(-alpha*tau2)) # Negative (unique term)
# Zero term:
aux_nhe += -nuL*sigma_gas_0*taualpha*heaviside(t-tau2)/alpha*(np.exp(-tau1/taualpha)-np.exp(-tau2/taualpha))*(np.exp(-alpha*t)-np.exp(-alpha*tau2)) # Zero term (unique term)
global_nhe += CIa*AE*aux_nhe# Multiply by the constants
# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
# 3) Non-Homogeneous non-trivial inverse term:
global_nhi = 0.;
for i in range(Ni):
AI = AI_arr[i]
tauI = tauI_arr[i]
tau0 = tau0_arr[i]
tau1 = tau1I_arr[i] - tau0; # We apply the offset to tau1 here
tau2 = tau2I_arr[i] - tau0; # We apply the offset to tau2 here
assert(tau1>0)," ERROR: tau0 must be LOWER than tau1 (tau0<tau1)"
assert(tau1<=tau2)," ERROR: tau1 must be LOWER than tau2 (tau1<tau2)"
aux_nhi = 0.;
for j in range(Ninfall):
deltatj = t - tj[j] - tau0; # We apply the offset to deltatj here
mint1 = deltatj*(deltatj<tau1) + tau1*(deltatj>=tau1); # mint1 already has the offset in tau0
mint2 = deltatj*(deltatj<tau2) + tau2*(deltatj>=tau2) # mint2 already has the offset in tau0
# From 0 to tau2 and from tau2 to t:
aux_nhi += Kj[j]*tauj[j]*heaviside(deltatj-tau1)*(_safelog(mint2,mint1)-np.exp(-mint2/tauj[j])*_safeexpi(mint2/tauj[j]) + _safeexpi(tau2/tauj[j])*( np.exp(-mint2/tauj[j])-np.exp(-deltatj/tauj[j]) ) + _safeexpi(tau1/tauj[j])*np.exp(-deltatj/tauj[j]) )# Positive term
aux_nhi -= Kj[j]/alpha*heaviside(deltatj-tau1)*(_safelog(mint2,mint1)-np.exp(-alpha*mint2)*_safeexpi(alpha*mint2) + _safeexpi(alpha*tau2)*( np.exp(-alpha*mint2)-np.exp(-alpha*deltatj) ) + _safeexpi(alpha*tau1)*np.exp(-alpha*deltatj) )# Negative term
# Zero term:
t0 = t - tau0; # We apply the offset to t here. No dependence on "t" should be observed in the "zero term" but on "t0"
del(t)#We do not need it anymore here
mint1 = t0*(t0<tau1) + tau1*(t0>=tau1)# mint1 already has the offset in tau0
mint2 = t0*(t0<tau2) + tau2*(t0>=tau2)# mint2 already has the offset in tau0
aux_nhi += nuL*sigma_gas_0*heaviside(t0-tau1)/alpha*(_safelog(mint2,mint1)-np.exp(-alpha*mint2)*_safeexpi(alpha*mint2) + _safeexpi(alpha*tau2)*( np.exp(-alpha*mint2)-np.exp(-alpha*t0) ) + _safeexpi(alpha*tau1)*np.exp(-alpha*t0) )# Zero term
global_nhi += CIa*AI*tauI*aux_nhi# Multiply by the constants
# Combine both terms:
primitive_R1a = global_nhg + global_nhe + global_nhi
return(primitive_R1a)
# -------------------------# End of iR1a_analytic -------------------------------------------
# Get_CIa-------------------------------------------------------------------------------------
def Get_CIa(TypeIa_SNe_ratio, Area, chemdict, present_day_time=13.8):
''' Set the value CIa given a TypeIa_SNe_ratio at present-da and an area:
Get_CIa(TypeIa_SNe_ratio, Area, chemdict, present_day_time=13.8)
TypeIa_SNe_ratio in events per Gyr
Area in pc**2
chemdict: Chemical dictionary associated with the model.
present_day_time: Evolutionary time (in Gyr)
returns: CIa'''
chemdict["CIa"] = chemdict.get("CIa",1.);
R1a_today = R1a_analytic(present_day_time, chemdict)
CIa = TypeIa_SNe_ratio/Area/R1a_today*chemdict.get("CIa",1.);
return(CIa)
# -------------------------# End of Get_CIa -------------------------------------------------
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# MODEL EQUATION AND SOLVERS
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# SolveChemEvolModel_GaussianTerm -------------------------------------------------------------------------------
def SolveChemEvolModel_GaussianTerm(t, alpha, nuL, Aj, tauj, tj, sigma_gas_0, CIa, AG, taup, sigma_p, tau1, tau2, mx1a):
'''
Returns the part of the solution that corresponds to the Gaussian DTD.
'''
assert(len(tauj)==len(tj)), "tj, tauj lengths mismatch"
N = len(tj)
assert(tau2>tau1), "tau2<=tau1"
#-------------------------------------
#-------------------------------------
# Useful definitions
gamma = alpha
N = len(tj)
betaj = [gamma - 1./tauj[j] for j in range(N)]
etaalpha = taup + sigma_p**2*alpha
#-------------------------------------
#-------------------------------------
#problem = [betaj[j]*etaj[j]+betaj[j]**2*sigma_p**2*0.5 for j in range(N)]
#for j in range(N): print(j, problem[j], np.round(np.exp(problem[j]),3) )
#print("....")
#problem = [taup/tauj[j]+0.5*sigma_p**2/tauj[j]**2 for j in range(N)]
#for j in range(N): print(j, problem[j], np.round(np.exp(problem[j]),3) )
#del(problem)
#-------------------------------------
# ------------------------------------------------------------------------
# 1) Non-Homogeneous non-trivial gaussian term:
sol_gauss = 0.;
for j in range(N):
# Time variables
deltatj = t - tj[j];
mint1 = deltatj*(deltatj<tau1) + tau1*(deltatj>=tau1)
mint2 = deltatj*(deltatj<tau2) + tau2*(deltatj>=tau2)
if betaj[j]!=0:
# Case alpha!=1/tauj[j]
# Useful parameters
etaj = taup+sigma_p**2/tauj[j]