-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate_collection.py
More file actions
2052 lines (1536 loc) · 102 KB
/
rate_collection.py
File metadata and controls
2052 lines (1536 loc) · 102 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
# Common Imports
import warnings
import functools
import math
import os
from operator import mul, add
from collections import OrderedDict, Counter
from ipywidgets import interact
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.ticker import MaxNLocator
import networkx as nx
# Import Rate
from rate import ChemSpecie, ChemRate, ChemComposition, SympyChemRate
import sys
#sys.path.append(os.path.abspath("/scratch/jh2/ps3459/pynucastro/pynucastro/rates"))
import constants as cons
class ChemRateCollection:
""" a collection of rates that together define a network """
pynucastro_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
def __init__(self, rates=None, tdot_switch=0):
"""rate_files are the files that together define the network. This
can be any iterable or single string.
If rates is supplied, initialize a RateCollection using the
Rate objects in the list 'rates'.
tdot_switch = 0 --> tdot are dT /dt
tdot_switch = 1 --> tdot are dEint /dt
"""
if isinstance(rates, ChemRate):
rates = [rates]
try:
for r in rates:
assert isinstance(r, ChemRate)
except:
print('Expected ChemRate object or list of ChemRate objects passed as the rates argument.')
raise
self.rates = rates
self.tdot_switch = tdot_switch
def get_allspecies(self):
# get the unique species
u = []
for r in self.rates:
u.append(r.reactants)
u.append(r.products)
#make a flattened list of u
cc = [item for sublist in u for item in sublist]
#to get unique species out, make it a set
#sort it by the order of specie mass to ensure RHS and LHS are consistently ordered during network integration
return sorted(list(set(cc)))
def evaluate_rates(self, T, composition):
rvals = []
#y_e = ChemComposition.eval_ye()
for r in self.rates:
val = r.eval(T, composition)
rvals.append(val)
return rvals
def evaluate_gamma(self, composition):
#gass = 0
#nmols = self.get_n(composition)
#for specie in self.get_allspecies():
# gass += composition[specie]/(specie.gamma - 1.0)
#gamma_index = 1.0 + nmols/gass
gamma_index = (5.0*(composition[ChemSpecie('elec')] + composition[ChemSpecie('h')] + composition[ChemSpecie('he')]) + \
7.0*(composition[ChemSpecie('h2')])) / (3.0*(composition[ChemSpecie('elec')] + composition[ChemSpecie('h')] + \
composition[ChemSpecie('he')]) + 5.0*(composition[ChemSpecie('h2')]))
return gamma_index
def evaluate_ydots(self, T, composition):
"""evaluate net rate of change of molar abundance for each nucleus
for a specific density, temperature, and composition"""
ydots = dict()
for specie in self.get_allspecies():
ydots[specie] = 0
for r in self.rates:
if specie in r.reactants:
#ydots[specie] += -r.eval(T, composition) * Counter(r.reactants)[specie] * \
# (composition[specie]**Counter(r.reactants)[specie]) * \
# functools.reduce(mul, [composition[q] for q in list(filter((specie).__ne__, r.reactants)) ], 1)
ydots[specie] += -r.eval(T, composition) * Counter(r.reactants)[specie] * \
functools.reduce(mul, [composition[q] for q in r.reactants])
#if specie == ChemSpecie('h'):
# print(-Counter(r.reactants)[specie], r)
if specie in r.products:
ydots[specie] += r.eval(T, composition) * Counter(r.products)[specie] * \
functools.reduce(mul, [composition[q] for q in r.reactants])
#if specie == ChemSpecie('h'):
# print(Counter(r.products)[specie], r)
return ydots
def evaluate_cooling(self, T, composition, redshift):
#NOTE - CIE cooling not used as per krome's test
sumcool = self.evaluate_cooling_cont(T, composition) + self.evaluate_cooling_compton(T, composition, redshift) + \
self.evaluate_cooling_chem(T, composition) + self.evaluate_cooling_atomic(T, composition) + \
self.evaluate_cooling_H2(T, composition) + self.evaluate_cooling_ff(T, composition)
if ChemSpecie('hd') in composition:
sumcool += self.evaluate_cooling_HD(T, composition, redshift)
return sumcool
def evaluate_heating(self, T, composition, redshift):
sumheat = self.evaluate_heating_chem(T, composition) + self.evaluate_heating_compress(T, composition)
return sumheat
def evaluate_tdot(self, T, composition, redshift):
if self.tdot_switch == 0:
#find dT/dt
tdot = (self.evaluate_heating(T, composition, redshift) - self.evaluate_cooling(T, composition, redshift)) * \
(self.evaluate_gamma(composition) - 1.0) / cons.boltzmann_erg / self.get_n(composition)
elif self.tdot_switch == 1:
#find dEint/dt
tdot = (self.evaluate_heating(T, composition, redshift) - self.evaluate_cooling(T, composition, redshift)) / self.get_rho(composition)
else:
raise ValueError('Incorrect value for tdot_switch!')
return tdot
def get_Hnuclei(self, composition):
nH = composition[ChemSpecie('hp')] + composition[ChemSpecie('h')] + composition[ChemSpecie('hm')] + \
composition[ChemSpecie('h2')]*2.0 + composition[ChemSpecie('h2p')]*2.0
if ChemSpecie('hd') in composition:
nH += composition[ChemSpecie('hd')] + composition[ChemSpecie('hdp')]
return nH
def get_n(self, composition):
n = composition[ChemSpecie('hp')] + composition[ChemSpecie('h')] + composition[ChemSpecie('hm')] + \
composition[ChemSpecie('h2')] + composition[ChemSpecie('h2p')] + composition[ChemSpecie('hepp')] + \
composition[ChemSpecie('he')] + composition[ChemSpecie('hep')] + composition[ChemSpecie('elec')]
if ChemSpecie('hd') in composition:
n += composition[ChemSpecie('hd')] + composition[ChemSpecie('hdp')]
if ChemSpecie('d') in composition:
n += composition[ChemSpecie('dm')] + composition[ChemSpecie('d')] + composition[ChemSpecie('dp')]
return n
def get_rho(self, composition):
rho = composition[ChemSpecie('hp')]*ChemSpecie('hp').m + composition[ChemSpecie('h')]*ChemSpecie('h').m + \
composition[ChemSpecie('hm')]*ChemSpecie('hm').m + composition[ChemSpecie('h2')]*ChemSpecie('h2').m + \
composition[ChemSpecie('h2p')]*ChemSpecie('h2p').m + composition[ChemSpecie('he')]*ChemSpecie('he').m + \
composition[ChemSpecie('hep')]*ChemSpecie('hep').m + composition[ChemSpecie('hepp')]*ChemSpecie('hepp').m + \
composition[ChemSpecie('elec')]*ChemSpecie('elec').m
if ChemSpecie('hd') in composition:
rho += composition[ChemSpecie('hd')]*ChemSpecie('hd').m + composition[ChemSpecie('hdp')]*ChemSpecie('hdp').m
if ChemSpecie('d') in composition:
rho += composition[ChemSpecie('d')]*ChemSpecie('d').m + composition[ChemSpecie('dm')]*ChemSpecie('dm').m + \
composition[ChemSpecie('dp')]*ChemSpecie('dp').m
return rho
def evaluate_heating_chem(self, T, composition):
dd = self.get_Hnuclei(composition)
small = 1e-99
heatingChem = 0.
ncrn = 1.0e6*(T**(-0.5))
ncrd1 = 1.6*np.exp(-(4.0e2/T)**2)
ncrd2 = 1.4*np.exp(-1.2e4/(T+1.2e3))
yH = composition[ChemSpecie('h')]/dd
yH2 = composition[ChemSpecie('h2')]/dd
ncr = ncrn/(ncrd1*yH + ncrd2*yH2)
h2heatfac = 1.0/(1.0 + ncr/dd)
HChem = 0.
a1, b1, c1, d1 = 0., 0., 0., 0.
for r in self.rates:
if {ChemSpecie('hm'), ChemSpecie('h')} == set(r.reactants) and {ChemSpecie('h2'), ChemSpecie('elec')} == set(r.products):
#reaction 10
a1 = ChemRate(reactants=r.reactants, products=r.products).eval(T, composition) * \
(3.53*h2heatfac*functools.reduce(mul, [composition[q] for q in r.reactants]))
for r in self.rates:
if {ChemSpecie('h2p'), ChemSpecie('h')} == set(r.reactants) and {ChemSpecie('hp'), ChemSpecie('h2')} == set(r.products):
#reaction 13
b1 = ChemRate(reactants=r.reactants, products=r.products).eval(T, composition) * \
(1.83*h2heatfac*functools.reduce(mul, [composition[q] for q in r.reactants]))
for r in self.rates:
if Counter(r.reactants)[ChemSpecie('h')] == 3 and {ChemSpecie('h2'), ChemSpecie('h')} == set(r.products):
#reaction 25
c1 = ChemRate(reactants=r.reactants, products=r.products).eval(T, composition) * \
(4.48*h2heatfac*functools.reduce(mul, [composition[q] for q in r.reactants]))
for r in self.rates:
if Counter(r.reactants)[ChemSpecie('h')] == 2 and Counter(r.reactants)[ChemSpecie('h2')] == 1 and Counter(r.products)[ChemSpecie('h2')] == 2:
#reaction 26
d1 = ChemRate(reactants=r.reactants, products=r.products).eval(T, composition) * \
(4.48*h2heatfac*functools.reduce(mul, [composition[q] for q in r.reactants]))
HChem = a1 + b1 + c1 + d1
return HChem*cons.eV_to_erg
def get_free_fall_time(self, composition):
rhogas = self.get_rho(composition)
free_fall_time = np.sqrt(3.0*np.pi/32.0/cons.gravity/rhogas)
return free_fall_time
def evaluate_heating_compress(self, T, composition):
free_fall_time = self.get_free_fall_time(composition)
dd = self.get_n(composition)
Hcompress = dd * cons.boltzmann_erg * T / free_fall_time #erg/s/cm3
return Hcompress
def evaluate_cooling_chem(self, T, composition):
CChem = 0.
a1, b1, c1 = 0., 0., 0.
for r in self.rates:
if {ChemSpecie('h2'), ChemSpecie('elec')} == set(r.reactants) and Counter(r.products)[ChemSpecie('h')] == 2 and Counter(r.products)[ChemSpecie('elec')] == 1:
#reaction 16
a1 = ChemRate(reactants=r.reactants, products=r.products).eval(T, composition) * \
(4.48*functools.reduce(mul, [composition[q] for q in r.reactants]))
for r in self.rates:
if {ChemSpecie('h2'), ChemSpecie('h')} == set(r.reactants) and Counter(r.products)[ChemSpecie('h')] == 3:
#reaction 17
b1 = ChemRate(reactants=r.reactants, products=r.products).eval(T, composition) * \
(4.48*functools.reduce(mul, [composition[q] for q in r.reactants]))
for r in self.rates:
if Counter(r.reactants)[ChemSpecie('h2')] == 2 and Counter(r.products)[ChemSpecie('h')] == 2 and Counter(r.products)[ChemSpecie('h2')] == 1:
#reaction 27
c1 = ChemRate(reactants=r.reactants, products=r.products).eval(T, composition) * \
(4.48*functools.reduce(mul, [composition[q] for q in r.reactants]))
CChem = a1 + b1 + c1
return CChem*cons.eV_to_erg
def evaluate_cooling_atomic(self, T, composition):
temp = max(T,10) #K
T5 = temp/1e5
Catomic = 0.
#COLLISIONAL IONIZATION: H, He, He+, He(2S)
Catomic = Catomic+ 1.27e-21*np.sqrt(temp)/(1.0+np.sqrt(T5))*np.exp(-1.578091e5/temp)*composition[ChemSpecie('elec')] * \
composition[ChemSpecie('h')]
Catomic = Catomic + 9.38e-22*np.sqrt(temp)/(1.0+np.sqrt(T5))*np.exp(-2.853354e5/temp)*composition[ChemSpecie('elec')] * \
composition[ChemSpecie('he')]
Catomic = Catomic + 4.95e-22*np.sqrt(temp)/(1.0+np.sqrt(T5))*np.exp(-6.31515e5/temp)*composition[ChemSpecie('elec')] * \
composition[ChemSpecie('hep')]
Catomic = Catomic + 5.01e-27*temp**(-0.1687)/(1.0+np.sqrt(T5))*np.exp(-5.5338e4/temp)*composition[ChemSpecie('elec')]**2 * \
composition[ChemSpecie('hep')]
#RECOMBINATION: H+, He+,He2+
Catomic = Catomic + 8.7e-27*np.sqrt(temp)*(temp/1.e3)**(-0.2)/(1.0+(temp/1.e6)**0.7)*composition[ChemSpecie('elec')] * \
composition[ChemSpecie('hp')]
Catomic = Catomic + 1.55e-26*temp**(0.3647)*composition[ChemSpecie('elec')]*composition[ChemSpecie('hep')]
Catomic = Catomic + 3.48e-26*np.sqrt(temp)*(temp/1.e3)**(-0.2)/(1.0+(temp/1.e6)**0.7)*composition[ChemSpecie('elec')] * \
composition[ChemSpecie('hepp')]
#!DIELECTRONIC RECOMBINATION: He
Catomic = Catomic + 1.24e-13*temp**(-1.5)*np.exp(-4.7e5/temp)*(1.0+0.30*np.exp(-9.4e4/temp))*composition[ChemSpecie('elec')] * \
composition[ChemSpecie('hep')]
#COLLISIONAL EXCITATION:
#H(all n), He(n=2,3,4 triplets), He+(n=2)
Catomic = Catomic + 7.5e-19/(1.0+np.sqrt(T5))*np.exp(-1.18348e5/temp)*composition[ChemSpecie('elec')] * \
composition[ChemSpecie('h')]
Catomic = Catomic + 9.1e-27*temp**(-.1687)/(1.0+np.sqrt(T5))*np.exp(-1.3179e4/temp)*composition[ChemSpecie('elec')]**2 * \
composition[ChemSpecie('hep')]
Catomic = Catomic + 5.54e-17*temp**(-.397)/(1.0+np.sqrt(T5))*np.exp(-4.73638e5/temp)*composition[ChemSpecie('elec')] * \
composition[ChemSpecie('hep')]
return Catomic
def evaluate_cooling_compton(self, T, composition, redshift):
Ccompton = 5.65e-36 * (1.0 + redshift)**4 * (T - 2.73 * (1.0 + redshift)) * composition[ChemSpecie('elec')] #erg/s/cm3
return Ccompton
def evaluate_cooling_ff(self, T, composition):
#based on Cen+1992
gaunt_factor = 1.5e0 #mean value
#BREMSSTRAHLUNG: all ions
bms_ions = composition[ChemSpecie('hp')] + composition[ChemSpecie('hep')] + composition[ChemSpecie('dp')] + 4.0*composition[ChemSpecie('hepp')]
Cff = 1.42e-27*gaunt_factor*(T**0.5)*bms_ions*composition[ChemSpecie('elec')] #erg/s/cm^3
return Cff
def kpla(self, composition):
rhogas = self.get_rho(composition)
kpla = 0.0
#opacity is zero under 1e-12 g/cm3
if rhogas < 1e-12:
return kpla
a0 = 1.000042e0
a1 = 2.14989e0
#log density cannot exceed 0.5 g/cm3
y = np.log10(min(rhogas,0.50))
kpla = 1e1**(a0*y + a1) #fit density only
return kpla
def get_jeans_length(self, T, composition):
rhogas = max(self.get_rho(composition), 1e-40)
mu = rhogas / max(self.get_n(composition), 1e-40) * cons.ip_mass
get_jeans_length = np.sqrt(np.pi * cons.boltzmann_erg * T/rhogas / cons.p_mass / cons.gravity / mu)
return get_jeans_length
def evaluate_cooling_cont(self, T, composition):
rhogas = self.get_rho(composition) #g/cm3
kgas = self.kpla(composition) #planck opacity cm2/g (Omukai+2000)
lj = self.get_jeans_length(T, composition) #cm
tau = lj * kgas * rhogas + 1e-40 #opacity
beta = min(1.0, tau**(-2)) #beta escape (always <1.)
Ccont = 4.0 * cons.stefboltz_erg * (T**4) * kgas * rhogas * beta #erg/s/cm3
return Ccont
def evaluate_cooling_CIE(self, T, composition, redshift):
CCIE = 0.0
Tcmb = 2.73*(1+redshift)
#set cooling to zero if n_H2 is smaller than 1e-12 1/cm3
#to avoid division by zero in opacity term due to tauCIE=0
if composition[ChemSpecie('h2')] < 1e-12:
return 0.
if T < Tcmb:
return 0.
x = np.log10(T)
x2 = x*x
x3 = x2*x
x4 = x3*x
x5 = x4*x
cool = 0.0
#outside boundaries below cooling is zero
logcool = -1e99
#evaluates fitting functions
if x > 2.0 and x < 2.95:
a0 = -30.3314216559651
a1 = 19.0004016698518
a2 = -17.1507937874082
a3 = 9.49499574218739
a4 = -2.54768404538229
a5 = 0.265382965410969
logcool = a0 + a1*x + a2*x2 + a3*x3 +a4*x4 +a5*x5
elif x >= 2.95 and x < 5:
b0 = -180.992524120965
b1 = 168.471004362887
b2 = -67.499549702687
b3 = 13.5075841245848
b4 = -1.31983368963974
b5 = 0.0500087685129987
logcool = b0 + b1*x + b2*x2 + b3*x3 +b4*x4 +b5*x5
elif x >= 5:
logcool = 3.0 * x - 21.2968837223113 #cubic extrapolation
#opacity according to RA04
tauCIE = (composition[ChemSpecie('h2')] * 1.4285714e-16)**2.8 #note: 1/7d15 = 1.4285714d-16
cool = cons.p_mass * 1e1**logcool #erg*cm3/s
CCIE = cool * min(1.0, (1.0-np.exp(-tauCIE))/tauCIE) * composition[ChemSpecie('h2')] * self.get_n(composition) #erg/cm3/s
return CCIE
def evaluate_cooling_HD(self, T, composition, redshift):
Tcmb = 2.73*(1+redshift)
CHD = 0.0 #erg/cm3/s
#this function does not have limits on density
#and temperature, even if the original paper do.
#However, we extrapolate the limits.
#exit on low temperature
if(T < Tcmb):
return 0.0
#extrapolate higher temperature limit
Tgas = min(T,1e4)
#calculate density
dd = composition[ChemSpecie('h')] #self.get_n(composition)
#exit if density is out of Lipovka bounds (uncomment if needed)
#if(dd<1d0 .or. dd>1d8) return
#extrapolate density limits
dd = min(max(dd,1e-2),1e10)
#POLYNOMIAL COEFFICIENT: TABLE 1 LIPOVKA
lipovka = np.zeros((5,5))
lipovka[0,:] = np.array([-42.56788, 0.92433, 0.54962, -0.07676, 0.00275])
lipovka[1,:] = np.array([21.93385, 0.77952, -1.06447, 0.11864, -0.00366])
lipovka[2,:] = np.array([-10.19097, -0.54263, 0.62343, -0.07366, 0.002514])
lipovka[3,:] = np.array([2.19906, 0.11711, -0.13768, 0.01759, -0.00066631])
lipovka[4,:] = np.array([-0.17334, -0.00835, 0.0106, -0.001482, 0.00006192])
logTgas = np.log10(Tgas)
lognH = np.log10(dd)
#loop to compute coefficients
logW = 0.0
for j in range(0,5):
lHj = lognH**j
for i in range(0,5):
logW += lipovka[i,j]*logTgas**i*lHj #erg/s
W = 10.0**(logW)
CHD = W * composition[ChemSpecie('hd')] #erg/cm3/s
return CHD
def sigmoid(self, x, x0, s):
sigmoid = 1e1/(1e1+np.exp(-s*(x-x0)))
return sigmoid
def wCool(self, logTgas, logTmin, logTmax):
x = (logTgas-logTmin)/(logTmax-logTmin)
wCool = 1e1**(2e2*(self.sigmoid(x,-2e-1,5e1)*self.sigmoid(-x,-1.2,5e1)-1.0))
if wCool < 1e-199:
wCool = 0.0
if wCool > 1:
raise ValueError("wCool > 1 in H2 cooling!")
return wCool
def evaluate_cooling_H2(self, T, composition):
temp = T
CH2 = 0.0
#if(temp<2d0) return
t3 = temp * 1e-3
logt3 = np.log10(t3)
logt = np.log10(temp)
cool = 0.0
logt32 = logt3 * logt3
logt33 = logt32 * logt3
logt34 = logt33 * logt3
logt35 = logt34 * logt3
logt36 = logt35 * logt3
logt37 = logt36 * logt3
logt38 = logt37 * logt3
w14 = self.wCool(logt, 1.0, 4.0)
w24 = self.wCool(logt, 2.0, 4.0)
#//H2-H
if temp <= 1e2:
fH2H = 1.e1**(-16.818342 + 3.7383713e1*logt3 + 5.8145166e1*logt32 + \
4.8656103e1*logt33 + 2.0159831e1*logt34 + 3.8479610*logt35) * composition[ChemSpecie('h')]
elif temp > 1e2 and temp <= 1e3:
fH2H = 1.e1**(-2.4311209e1 + 3.5692468*logt3 -1.1332860e1*logt32 - 2.7850082e1*logt33 - \
2.1328264e1*logt34 - 4.2519023*logt35) * composition[ChemSpecie('h')]
elif temp > 1e3 and temp <= 6e3:
fH2H = 1e1**(-2.4311209e1 + 4.6450521*logt3 - 3.7209846*logt32 + 5.9369081*logt33 - \
5.5108049*logt34 + 1.5538288*logt35) * composition[ChemSpecie('h')]
else:
fH2H = 1.862314467912518e-022*self.wCool(logt,1.0,np.log10(6e3)) * composition[ChemSpecie('h')]
cool += fH2H
#//H2-Hp
if temp > 1e1 and temp <= 1e4:
fH2Hp = 1e1**(-2.2089523e1 +1.5714711*logt3 + 0.015391166*logt32 - 0.23619985*logt33 - \
0.51002221*logt34 + 0.32168730*logt35) * composition[ChemSpecie('hp')]
else:
fH2Hp = 1.182509139382060e-021 * composition[ChemSpecie('hp')] * w14
cool += fH2Hp
#//H2-H2
fH2H2 = w24 * 1e1**(-2.3962112e1 + 2.09433740*logt3 - 0.77151436*logt32 + \
0.43693353*logt33 - 0.14913216*logt34 - 0.033638326*logt35) * composition[ChemSpecie('h2')]
cool += fH2H2
#//H2-e
fH2e = 0.0
if temp <= 5e2:
fH2e = 1e1**(min(-2.1928796e1 + 1.6815730e1*logt3 + 9.6743155e1*logt32 + 3.4319180e2*logt33 + \
7.3471651e2*logt34 + 9.8367576e2*logt35 + 8.0181247e2*logt36 + \
3.6414446e2*logt37 + 7.0609154e1*logt38,3e1)) * composition[ChemSpecie('elec')]
elif temp > 5e2:
fH2e = 1e1**(-2.2921189e1 + 1.6802758*logt3 + 0.93310622*logt32 + 4.0406627*logt33 - \
4.7274036*logt34 - 8.8077017*logt35 + 8.9167183*logt36 + 6.4380698*logt37 - \
6.3701156*logt38) * composition[ChemSpecie('elec')]
cool += fH2e*w24
#//H2-He
if temp > 1e1 and temp <= 1e4:
fH2He = 1e1**(-2.3689237e1 +2.1892372*logt3 - 0.81520438*logt32 + 0.29036281*logt33 - \
0.16596184*logt34 + 0.19191375*logt35) * composition[ChemSpecie('he')]
else:
fH2He = 1.002560385050777e-022 * composition[ChemSpecie('he')] * w14
cool += fH2He
#check error
if cool > 1e30:
raise ValueError(" ERROR: cooling >1.d30 erg/s/cm3")
#this to avoid negative, overflow and useless calculations below
if cool <= 0.0:
CH2 = 0.0
return CH2
#high density limit from HM79, GP98 below Tgas = 2d3
#UPDATED USING GLOVER 2015 for high temperature corrections, MNRAS
#IN THE HIGH DENSITY REGIME LAMBDA_H2 = LAMBDA_H2(LTE) = HDL
#the following mix of functions ensures the right behaviour
# at low (T<10 K) and high temperatures (T>2000 K) by
# using both the original Hollenbach and the new Glover data
# merged in a smooth way.
if temp < 2e3:
HDLR = ((9.5e-22*t3**3.76)/(1.+0.12*t3**2.1)*np.exp(-(0.13/t3)**3) + 3.e-24*np.exp(-0.51/t3)) #erg/s
HDLV = (6.7e-19*np.exp(-5.86/t3) + 1.6e-18*np.exp(-11.7/t3)) #erg/s
HDL = HDLR + HDLV #erg/s
elif temp >= 2e3 and temp <= 1e4:
HDL = 1e1**(-2.0584225e1 + 5.0194035*logt3 - 1.5738805*logt32 - 4.7155769*logt33 + \
2.4714161*logt34 + 5.4710750*logt35 - 3.9467356*logt36 - 2.2148338*logt37 + 1.8161874*logt38)
else:
dump14 = 1.0 / (1.0 + np.exp(min((temp-3e4)*2e-4,3e2)))
HDL = 5.531333679406485E-019*dump14
LDL = cool #erg/s
if HDL == 0.0:
CH2 = 0.0
else:
CH2 = composition[ChemSpecie('h2')]/(1.0/HDL+1.0/LDL) * \
min(1.0, max(1.25e-10 * self.get_n(composition), 1e-40)**(-0.45)) #erg/cm3/s
return CH2
def fft(self, y):
allspecies = self.get_allspecies()
comp = ChemComposition(specie=(allspecies)).set_specie_numberdens(nval=(y))
free_fall_time = self.get_free_fall_time(comp)
return free_fall_time
def rhs(self, t, y):
redshift = 30.0
allspecies = self.get_allspecies()
comp = ChemComposition(specie=(allspecies)).set_specie_numberdens(nval=(y[:-1]))
ydots = self.evaluate_ydots(y[len(y)-1], comp)
#don't need to sort ydots since ydots are already sorted
sorted_ydots = ydots #{key: value for key, value in sorted(ydots.items())}
tdot = self.evaluate_tdot(y[len(y)-1], comp, redshift)
bb = list(sorted_ydots.values())
bb.append(tdot)
return bb
class SympyChemRateCollection:
""" a collection of rates that together define a network """
pynucastro_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
def __init__(self, rates=None, tdot_switch=0, ydots_lambdified=False, jacs_lambdified=False, withD=1, massfracs=0):
"""rate_files are the files that together define the network. This
can be any iterable or single string.
If rates is supplied, initialize a RateCollection using the
Rate objects in the list 'rates'.
tdot_switch = 0 --> tdot are dT /dt
tdot_switch = 1 --> tdot are dEint /dt
"""
import sympy as sp
if isinstance(rates, SympyChemRate):
rates = [rates]
try:
for r in rates:
assert isinstance(r, SympyChemRate)
except:
print('Expected ChemRate object or list of ChemRate objects passed as the rates argument.')
raise
self.rates = rates
self.tdot_switch = tdot_switch
self.withD = withD
self.massfracs = massfracs
if (self.massfracs != 0) & (self.massfracs != 1):
raise ValueError('The code only works with mass fractions or number densities!')
if (self.tdot_switch != 0) & (self.tdot_switch != 1):
raise ValueError('The code only works with temperature or specific internal energy!')
if (ydots_lambdified):
print('Using ydots_lambdified')
redshift = 30
self.ydots_lambdified = self.get_lambdifys_ydots(redshift)
if (jacs_lambdified):
print('Using jacs_lambdified')
redshift = 30
if tdot_switch == 0:
self.jacs_lambdified = self.get_lambdifys_jacobian_T(redshift)
elif tdot_switch == 1:
self.jacs_lambdified = self.get_lambdifys_jacobian_Eint(redshift)
def get_allspecies_sym(self):
# get the unique species
# HARDCODED for now
if self.withD == 1:
splist = [ChemSpecie('e').sym_name, ChemSpecie('hp').sym_name, ChemSpecie('h').sym_name, ChemSpecie('hm').sym_name, \
ChemSpecie('dp').sym_name, ChemSpecie('d').sym_name, ChemSpecie('h2p').sym_name, ChemSpecie('dm').sym_name, \
ChemSpecie('h2').sym_name, ChemSpecie('hdp').sym_name, ChemSpecie('hd').sym_name, ChemSpecie('hepp').sym_name, \
ChemSpecie('hep').sym_name, ChemSpecie('he').sym_name]
elif self.withD == 0:
splist = [ChemSpecie('e').sym_name, ChemSpecie('hp').sym_name, ChemSpecie('h').sym_name, ChemSpecie('hm').sym_name, \
ChemSpecie('h2p').sym_name, \
ChemSpecie('h2').sym_name, ChemSpecie('hepp').sym_name, \
ChemSpecie('hep').sym_name, ChemSpecie('he').sym_name]
else:
raise ValueError('Incorrect value of withD!')
return splist
def get_allspecies(self):
# get the unique species
# HARDCODED for now
if self.withD == 1:
splist = (ChemSpecie('e'), ChemSpecie('hp'), ChemSpecie('h'), ChemSpecie('hm'), \
ChemSpecie('dp'), ChemSpecie('d'), ChemSpecie('h2p'), ChemSpecie('dm'), \
ChemSpecie('h2'), ChemSpecie('hdp'), ChemSpecie('hd'), ChemSpecie('hepp'), \
ChemSpecie('hep'), ChemSpecie('he'))
elif self.withD == 0:
splist = (ChemSpecie('e'), ChemSpecie('hp'), ChemSpecie('h'), ChemSpecie('hm'), \
ChemSpecie('h2p'), ChemSpecie('h2'), ChemSpecie('hepp'), \
ChemSpecie('hep'), ChemSpecie('he'))
else:
raise ValueError('Incorrect value of withD!')
return splist
def sympy_to_python(self, sympy_specie):
#given a sympy specie, it returns the chemspecie
#this is needed when using functools.reduce(mul) operation while evaluating rates below in ydots
allspecies =self.get_allspecies()
saved = ChemSpecie('dummy')
for specie in allspecies:
if specie.sym_name == sympy_specie:
saved = specie
break
if saved == ChemSpecie('dummy'):
raise ValueError('Converting sympy symbol of a specie back to ChemSpecie object failed!')
return saved
def evaluate_rates(self, T, composition, density=0):
rvals = []
#y_e = ChemComposition.eval_ye()
for r in self.rates:
val = r.eval(T, composition, density)
rvals.append(val)
return rvals
def evaluate_gamma(self, composition, density=0):
if self.massfracs == 1:
gamma_index = (5.0*density*(composition[ChemSpecie('elec').sym_name]/ChemSpecie('elec').m + composition[ChemSpecie('h').sym_name]/ChemSpecie('h').m + composition[ChemSpecie('he').sym_name]/ChemSpecie('he').m) + \
7.0*density*(composition[ChemSpecie('h2').sym_name]/ChemSpecie('h2').m)) / (3.0*density*(composition[ChemSpecie('elec').sym_name]/ChemSpecie('elec').m + composition[ChemSpecie('h').sym_name]/ChemSpecie('h').m + \
composition[ChemSpecie('he').sym_name]/ChemSpecie('he').m) + 5.0*density*(composition[ChemSpecie('h2').sym_name]/ChemSpecie('h2').m))
elif self.massfracs == 0:
gamma_index = (5.0*(composition[ChemSpecie('elec').sym_name] + composition[ChemSpecie('h').sym_name] + composition[ChemSpecie('he').sym_name]) + \
7.0*composition[ChemSpecie('h2').sym_name]) / (3.0*(composition[ChemSpecie('elec').sym_name] + composition[ChemSpecie('h').sym_name] + \
composition[ChemSpecie('he').sym_name]) + 5.0*composition[ChemSpecie('h2').sym_name])
return gamma_index
def evaluate_ydots(self, T, composition, density=0):
"""evaluate net rate of change of mass fraction abundance for each nucleus
for a specific density, temperature, and composition"""
ydots = dict()
for specie in self.get_allspecies_sym():
ydots[specie] = 0
for r in self.rates:
if specie in r.reactants:
if self.massfracs == 1:
ydots[specie] += -r.eval(T, composition, density) * Counter(r.reactants)[specie] * \
functools.reduce(mul, [composition[q]*density/self.sympy_to_python(q).m for q in r.reactants])
elif self.massfracs == 0:
ydots[specie] += -r.eval(T, composition, density) * Counter(r.reactants)[specie] * \
functools.reduce(mul, [composition[q] for q in r.reactants])
if specie in r.products:
if self.massfracs == 1:
ydots[specie] += r.eval(T, composition, density) * Counter(r.products)[specie] * \
functools.reduce(mul, [composition[q]*density/self.sympy_to_python(q).m for q in r.reactants])
elif self.massfracs == 0:
ydots[specie] += r.eval(T, composition, density) * Counter(r.products)[specie] * \
functools.reduce(mul, [composition[q] for q in r.reactants])
return ydots
def evaluate_cooling(self, T, composition, redshift, density=0):
#NOTE - CIE cooling not used as per krome's test
sumcool = self.evaluate_cooling_cont(T, composition, density) + self.evaluate_cooling_compton(T, composition, redshift, density) + \
self.evaluate_cooling_chem(T, composition, density) + self.evaluate_cooling_atomic(T, composition, density) + \
self.evaluate_cooling_H2(T, composition, density) + self.evaluate_cooling_ff(T, composition)
if self.withD == 1:
sumcool += self.evaluate_cooling_HD(T, composition, redshift, density)
return sumcool
def evaluate_heating(self, T, composition, redshift, density=0):
sumheat = self.evaluate_heating_chem(T, composition, density) + self.evaluate_heating_compress(T, composition, density)
return sumheat
def evaluate_tdot(self, T, composition, redshift, density=0):
if self.tdot_switch == 0:
#dT/ dt
tdot = (self.evaluate_heating(T, composition, redshift, density) - self.evaluate_cooling(T, composition, redshift, density)) * \
(self.evaluate_gamma(composition, density) - 1.0) / cons.boltzmann_erg / self.get_n(composition, density)
elif self.tdot_switch == 1:
#dEint / dt
if density == 0:
density = self.get_rho(composition)
tdot = (self.evaluate_heating(T, composition, redshift, density) - self.evaluate_cooling(T, composition, redshift, density)) / density
return tdot
def get_Hnuclei(self, composition, density=0):
import sympy as sp
if self.massfracs == 1:
nH = composition[ChemSpecie('hp').sym_name]/ChemSpecie('hp').m + composition[ChemSpecie('h').sym_name]/ChemSpecie('h').m + composition[ChemSpecie('hm').sym_name]/ChemSpecie('hm').m + \
composition[ChemSpecie('h2').sym_name]*2.0/ChemSpecie('h2').m + composition[ChemSpecie('h2p').sym_name]*2.0/ChemSpecie('h2p').m
if self.withD == 1:
nH += composition[ChemSpecie('hd').sym_name]/ChemSpecie('hd').m + composition[ChemSpecie('hdp').sym_name]/ChemSpecie('hdp').m
return nH*density
elif self.massfracs == 0:
nH = composition[ChemSpecie('hp').sym_name] + composition[ChemSpecie('h').sym_name] + composition[ChemSpecie('hm').sym_name] + \
composition[ChemSpecie('h2').sym_name]*2.0 + composition[ChemSpecie('h2p').sym_name]*2.0
if self.withD == 1:
nH += composition[ChemSpecie('hd').sym_name] + composition[ChemSpecie('hdp').sym_name]
return nH
def get_n(self, composition, density=0):
if self.massfracs == 1:
n = composition[ChemSpecie('hp').sym_name]/ChemSpecie('hp').m + composition[ChemSpecie('h').sym_name]/ChemSpecie('h').m + composition[ChemSpecie('hm').sym_name]/ChemSpecie('hm').m + \
composition[ChemSpecie('h2').sym_name]/ChemSpecie('h2').m + composition[ChemSpecie('h2p').sym_name]/ChemSpecie('h2p').m + composition[ChemSpecie('hepp').sym_name]/ChemSpecie('hepp').m + \
composition[ChemSpecie('he').sym_name]/ChemSpecie('he').m + composition[ChemSpecie('hep').sym_name]/ChemSpecie('hep').m + composition[ChemSpecie('elec').sym_name]/ChemSpecie('elec').m
if self.withD == 1:
n += composition[ChemSpecie('hd').sym_name]/ChemSpecie('hd').m + composition[ChemSpecie('hdp').sym_name]/ChemSpecie('hdp').m + \
composition[ChemSpecie('dm').sym_name]/ChemSpecie('dm').m + composition[ChemSpecie('d').sym_name]/ChemSpecie('d').m + composition[ChemSpecie('dp').sym_name]/ChemSpecie('dp').m
return n*density
elif self.massfracs == 0:
n = composition[ChemSpecie('hp').sym_name] + composition[ChemSpecie('h').sym_name] + composition[ChemSpecie('hm').sym_name] + \
composition[ChemSpecie('h2').sym_name] + composition[ChemSpecie('h2p').sym_name] + composition[ChemSpecie('hepp').sym_name] + \
composition[ChemSpecie('he').sym_name] + composition[ChemSpecie('hep').sym_name] + composition[ChemSpecie('elec').sym_name]
if self.withD == 1:
n += composition[ChemSpecie('hd').sym_name] + composition[ChemSpecie('hdp').sym_name] + \
composition[ChemSpecie('dm').sym_name] + composition[ChemSpecie('d').sym_name] + composition[ChemSpecie('dp').sym_name]
return n
def get_rho(self, composition, density=0):
if self.massfracs == 1:
return density
elif self.massfracs == 0:
rho = composition[ChemSpecie('hp').sym_name]*ChemSpecie('hp').m + composition[ChemSpecie('h').sym_name]*ChemSpecie('h').m + \
composition[ChemSpecie('hm').sym_name]*ChemSpecie('hm').m + composition[ChemSpecie('h2').sym_name]*ChemSpecie('h2').m + \
composition[ChemSpecie('h2p').sym_name]*ChemSpecie('h2p').m + composition[ChemSpecie('he').sym_name]*ChemSpecie('he').m + \
composition[ChemSpecie('hep').sym_name]*ChemSpecie('hep').m + composition[ChemSpecie('hepp').sym_name]*ChemSpecie('hepp').m + \
composition[ChemSpecie('elec').sym_name]*ChemSpecie('elec').m
if self.withD == 1:
rho += composition[ChemSpecie('hd').sym_name]*ChemSpecie('hd').m + composition[ChemSpecie('hdp').sym_name]*ChemSpecie('hdp').m + \
composition[ChemSpecie('d').sym_name]*ChemSpecie('d').m + composition[ChemSpecie('dm').sym_name]*ChemSpecie('dm').m + \
composition[ChemSpecie('dp').sym_name]*ChemSpecie('dp').m
return rho
def evaluate_heating_chem(self, T, composition, density=0):
import sympy as sp
dd = self.get_Hnuclei(composition, density)
small = 1e-99
heatingChem = 0.
ncrn = 1.0e6*(T**(-0.5))
ncrd1 = 1.6*sp.exp(-(4.0e2/T)**2)
ncrd2 = 1.4*sp.exp(-1.2e4/(T+1.2e3))
if self.massfracs == 1:
yH = composition[ChemSpecie('h').sym_name]*density/(dd*ChemSpecie('h').m)
yH2 = composition[ChemSpecie('h2').sym_name]*density/(dd*ChemSpecie('h2').m)
elif self.massfracs == 0:
yH = composition[ChemSpecie('h').sym_name]/dd
yH2 = composition[ChemSpecie('h2').sym_name]/dd
ncr = ncrn/(ncrd1*yH + ncrd2*yH2)
h2heatfac = 1.0/(1.0 + ncr/dd)
HChem = 0.
a1, b1, c1, d1 = 0., 0., 0., 0.
for r in self.rates:
if {ChemSpecie('hm').sym_name, ChemSpecie('h').sym_name} == set(r.reactants) and {ChemSpecie('h2').sym_name, ChemSpecie('elec').sym_name} == set(r.products):
#reaction 10
if self.massfracs == 1:
a1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(3.53*h2heatfac*functools.reduce(mul, [composition[q]*density/self.sympy_to_python(q).m for q in r.reactants]))
elif self.massfracs == 0:
a1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(3.53*h2heatfac*functools.reduce(mul, [composition[q] for q in r.reactants]))
for r in self.rates:
if {ChemSpecie('h2p').sym_name, ChemSpecie('h').sym_name} == set(r.reactants) and {ChemSpecie('hp').sym_name, ChemSpecie('h2').sym_name} == set(r.products):
#reaction 13
if self.massfracs == 1:
b1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(1.83*h2heatfac*functools.reduce(mul, [composition[q]*density/self.sympy_to_python(q).m for q in r.reactants]))
elif self.massfracs == 0:
b1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(1.83*h2heatfac*functools.reduce(mul, [composition[q] for q in r.reactants]))
for r in self.rates:
if Counter(r.reactants)[ChemSpecie('h').sym_name] == 3 and {ChemSpecie('h2').sym_name, ChemSpecie('h').sym_name} == set(r.products):
#reaction 25
if self.massfracs == 1:
c1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(4.48*h2heatfac*functools.reduce(mul, [composition[q]*density/self.sympy_to_python(q).m for q in r.reactants]))
elif self.massfracs == 0:
c1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(4.48*h2heatfac*functools.reduce(mul, [composition[q] for q in r.reactants]))
for r in self.rates:
if Counter(r.reactants)[ChemSpecie('h').sym_name] == 2 and Counter(r.reactants)[ChemSpecie('h2').sym_name] == 1 and Counter(r.products)[ChemSpecie('h2').sym_name] == 2:
#reaction 26
if self.massfracs == 1:
d1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(4.48*h2heatfac*functools.reduce(mul, [composition[q]*density/self.sympy_to_python(q).m for q in r.reactants]))
elif self.massfracs == 0:
d1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(4.48*h2heatfac*functools.reduce(mul, [composition[q] for q in r.reactants]))
HChem = a1 + b1 + c1 + d1
return HChem*cons.eV_to_erg
def get_free_fall_time(self, composition, density=0):
import sympy as sp
rhogas = self.get_rho(composition, density)
free_fall_time = sp.sqrt(3.0*sp.pi/32.0/cons.gravity/rhogas)
return free_fall_time
def evaluate_heating_compress(self, T, composition, density=0):
free_fall_time = self.get_free_fall_time(composition, density)
dd = self.get_n(composition, density)
Hcompress = dd * cons.boltzmann_erg * T / free_fall_time #erg/s/cm3
return Hcompress
def evaluate_cooling_chem(self, T, composition, density=0):
CChem = 0.
a1, b1, c1 = 0., 0., 0.
for r in self.rates:
if {ChemSpecie('h2').sym_name, ChemSpecie('elec').sym_name} == set(r.reactants) and Counter(r.products)[ChemSpecie('h').sym_name] == 2 and Counter(r.products)[ChemSpecie('elec').sym_name] == 1:
#reaction 16
if self.massfracs == 1:
a1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(4.48*functools.reduce(mul, [composition[q]*density/self.sympy_to_python(q).m for q in r.reactants]))
elif self.massfracs == 0:
a1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(4.48*functools.reduce(mul, [composition[q] for q in r.reactants]))
for r in self.rates:
if {ChemSpecie('h2').sym_name, ChemSpecie('h').sym_name} == set(r.reactants) and Counter(r.products)[ChemSpecie('h').sym_name] == 3:
#reaction 17
if self.massfracs == 1:
b1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(4.48*functools.reduce(mul, [composition[q]*density/self.sympy_to_python(q).m for q in r.reactants]))
elif self.massfracs == 0:
b1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(4.48*functools.reduce(mul, [composition[q] for q in r.reactants]))
for r in self.rates:
if Counter(r.reactants)[ChemSpecie('h2').sym_name] == 2 and Counter(r.products)[ChemSpecie('h').sym_name] == 2 and Counter(r.products)[ChemSpecie('h2').sym_name] == 1:
#reaction 27
if self.massfracs == 1:
c1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(4.48*functools.reduce(mul, [composition[q]*density/self.sympy_to_python(q).m for q in r.reactants]))
elif self.massfracs == 0:
c1 = SympyChemRate(reactants=r.reactants, products=r.products).eval(T, composition, density) * \
(4.48*functools.reduce(mul, [composition[q] for q in r.reactants]))
CChem = a1 + b1 + c1
return CChem*cons.eV_to_erg
def evaluate_cooling_atomic(self, T, composition, density=0):
import sympy as sp
temp_old = sp.Max(T,10) #K
#if you dont rewrite dd_old as below, it will throw an error while using cxxcode on the derivative of dd_old later on
#because cxxcode does not like heaviside function which will appear in the derivative of dd_old
temp = temp_old.rewrite(sp.Piecewise)