summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/nucleardata/nucleardata.pytxcode
blob: 075f1e13466b9bc461534a254bbe567e86aa4231 (plain)
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
=>PYTHONTEX#py#default#default#0#code#####26#
import sys
import fileinput
import csv
from subprocess import check_output
import random as nucrandom

u2Mev=931.502
u2kev=u2Mev*1000.
Mev2u=1.0/u2Mev
kev2u=1.0/u2kev
u2kg=1.660539e-27
kg2u=1.0/u2kg

hbarev=6.5828e-16
hbarmev=hbarev/1.e6
hbarkev=hbarev/1.e3

class Nucdata(object):
    def __init__(self):
        self.units_dict={
            'ev':'\si{\electronvolt}',
            'mev':'\si{\mega\electronvolt}',
            'Mev':'\si{\mega\electronvolt}',
            'kev':'\si{\kilo\electronvolt}',
            'us':'\si{\micro\second}',
            'ns':'\si{\\nano\second}',
            'y':'\si{\ensuremath{\mathrm{yr}}}',
            'yr':'\si{\ensuremath{\mathrm{yr}}}',
            'm':'\si{\min}',
            'min':'\si{\min}',
            'h':'\si{\hour}',
            'hr':'\si{\hour}',
            'd':'\si{\day}',
            'day':'\si{\day}',
            's':'\si{\second}',
            'ms':'\si{\ms}',
            'ps':'\si{\pico\second}',
            'fs':'\si{\\femto\second}',
            'as':'\si{\\atto\second}',
            'My':'\si{\ensuremath{\mathrm{My}}}',
            'Gy':'\si{\ensuremath{\mathrm{Gy}}}'
            }

        self.time_dict={
            "yr":1,
            "y":1,
            "d":365.25,
            "day":365.25,
            "days":365.25,
            "h":365.25*24,
            "hr":365.25*24,
            "m":365.25*24*60,
            "min":365.25*24*60,
            "s":365.25*24*3600,
            "ms":365.25*24*3600*1e3,
            "us":365.25*24*3600*1e6,
            "ns":365.25*24*3600*1e9,
            "ps":365.25*24*3600*1e12,
            "fs":365.25*24*3600*1e15,
            "as":365.25*24*3600*1e18,
            "My":1e-6,
            "Gy":1e-9}

    def timeenergy(self,ene):
        return hbarev*0.69315/ene/self.time_dict.get("s")

    def  getNuclide(self,EL,A):
        res=[v for v in nucleardata if  v.get('SYM').strip().upper()== EL.upper() and v.get('A') == A]
        return res

    def getElement(self,Z):
        res=[v for v in nucleardata if  int(v.get('Z'))== Z]
        return res[0].get('SYM')

    def getMass_u(self,EL,A):
        try:
            EL = int(eval(str(EL)))
        except:
            res=[v for v in nucleardata if  v.get('SYM').strip().upper() == EL.upper() and v.get('A') == A]
        else:
            res=[v for v in nucleardata if  int(v.get('Z')) == EL and v.get('A') == A]
        return res[0].get('mass')

    def getNuclearMass_u(self,EL,A):
        try:
            EL = int(eval(str(EL)))
            res=[v for v in nucleardata if  int(v.get('Z'))== EL and v.get('A') == A]
        except:
            res=[v for v in nucleardata if  v.get('SYM').strip().upper()== EL.upper() and v.get('A') == A]

        return res[0].get('mass')-int(res[0].get('Z'))*0.511/931.502

    def getMass_mev(self,EL,A):
        massu=self.getMass_u(EL,A)
        return massu*931.502

    def getMass_kev(self,EL,A):
        massu=self.getMass_u(EL,A)
        return massu*931502.

    def getNuclearMass_mev(self,EL,A):
        massu=self.getNuclearMass_u(EL,A)
        return massu*931.502

    def getNuclearMass_kev(self,EL,A):
        massu=self.getNuclearMass_u(EL,A)
        return massu*931502.

    def getReaction_u(self,EL1,A1,EL2,A2,EL3,A3,EL4,A4):
        m1=self.getNuclearMass_u(EL1,A1)
        m2=self.getNuclearMass_u(EL2,A2)
        m3=self.getNuclearMass_u(EL3,A3)
        m4=self.getNuclearMass_u(EL4,A4)
        return m1+m2-m3-m4

    def getReaction_mev(self,EL1,A1,EL2,A2,EL3,A3,EL4,A4):
        qu=self.getReaction_u(EL1,A1,EL2,A2,EL3,A3,EL4,A4)
        return qu*931.502

    def getReaction_kev(self,EL1,A1,EL2,A2,EL3,A3,EL4,A4):
        qu=self.getReaction_u(EL1,A1,EL2,A2,EL3,A3,EL4,A4)
        return qu*931502.

    def getExcess(self,EL,A):
        try:
            EL = int(eval(str(EL)))
            res=[v for v in nucleardata if  int(v.get('Z'))== EL and v.get('A') == A]
        except:
            res=[v for v in nucleardata if  v.get('SYM').strip().upper()== EL.upper() and v.get('A') == A]

        return res[0].get('Excess')

    def getHalfLife(self,EL,A,newunit):
        try:
            EL = int(eval(str(EL)))
            res=[v for v in nucleardata if  int(v.get('Z'))== EL and v.get('A') == int(A)]
        except:
            res=[v for v in nucleardata if  v.get('SYM').strip().upper()== EL.upper() and v.get('A') == int(A)]

        rest=res[0].get('halflife')
        if type(rest)==type('x'):
            if rest.lower().strip():
                value= rest.lower().strip().split()
                if len(value)>1:
                    number = eval(value[0].strip())
                    unit = value[1].strip()
                    if newunit: # user wants to change the unit
                        if unit in ['ev','kev','mev']:
                            number=self.timeenergy(number)
                            if unit=='kev':
                                number=number/1.e3
                            elif unit=='mev':
                                number=number/1.e6
                            unit='y'
                        number=number*self.time_dict.get(newunit)/self.time_dict.get(unit)
                        unit=newunit
                    result= "\\num{" + str(number) + "} " + self.units_dict.get(unit)
                else:
                    result=value[0].strip()
                return result
            else:
                return 'undetermined'
        else:
            return rest

    def getHalfLifeValue(self,EL,A,newunit):
        try:
            EL = int(eval(str(EL)))
            res=[v for v in nucleardata if  int(v.get('Z'))== EL and v.get('A') == A]
        except:
            res=[v for v in nucleardata if  v.get('SYM').strip().upper()== EL.upper() and v.get('A') == A]

        rest=res[0].get('halflife')
        if type(rest)==type('x'):
            if rest.lower().strip():
                value= rest.lower().strip().split()
                if len(value)>1:
                    number = eval(value[0].strip())
                    unit = value[1].strip()
                    if newunit: # user wants to change the unit
                        if unit in ['ev','kev','mev']:
                            number=self.timeenergy(number)
                            if unit=='kev':
                                number=number/1.e3
                            elif unit=='mev':
                                number=number/1.e6
                            unit='y'
                        number=number*self.time_dict.get(newunit)/self.time_dict.get(unit)
                        unit=newunit
                    result= number
                else:
                    result=value[0].strip()
                return result
            else:
                return 'undetermined'
        else:
            return rest

    def getHalfLifeUnit(self,EL,A):
        try:
            EL = int(eval(str(EL)))
            res=[v for v in nucleardata if  int(v.get('Z'))== EL and v.get('A') == A]
        except:
            res=[v for v in nucleardata if  v.get('SYM').strip().upper()== EL.upper() and v.get('A') == A]

        rest=res[0].get('halflife')
        if type(rest)==type('x'):
            value= rest.lower().strip().split()
            if len(value)>1:
                unit = value[1].strip()
                result= self.units_dict.get(unit)
            else:
                result=value[0].strip()
            return result

        else:
            return(rest)

    def getBea(self,EL,A):
        try:
            EL = int(eval(str(EL)))
            res=[v for v in nucleardata if  int(v.get('Z'))== EL and v.get('A') == A]
        except:
            res=[v for v in nucleardata if  v.get('SYM').strip().upper()== EL.upper() and v.get('A') == A]

        return res[0].get('BEA')

    def getQbeta(self,EL,A):
        try:
            EL = int(eval(str(EL)))
            res=[v for v in nucleardata if  int(v.get('Z'))== EL and v.get('A') == A]
        except:
            res=[v for v in nucleardata if  v.get('SYM').strip().upper()== EL.upper() and v.get('A') == A]
        return res[0].get('Qbeta')

    def getQposi(self,EL,A):
        try:
            EL = int(eval(str(EL)))
            res=[v for v in nucleardata if  int(v.get('Z'))== EL and v.get('A') == A]
        except:
            res=[v for v in nucleardata if  v.get('SYM').strip().upper()== EL.upper() and v.get('A') == A]
        return res[0].get('Qposi')

    def getQec(self,EL,A):
        try:
            EL = int(eval(str(EL)))
            res=[v for v in nucleardata if  int(v.get('Z'))== EL and v.get('A') == A]
        except:
            res=[v for v in nucleardata if  v.get('SYM').strip().upper()== EL.upper() and v.get('A') == A]
        return res[0].get('Qec')

    def getQalpha(self,EL,A):
        try:
            EL = int(eval(str(EL)))
            res=[v for v in nucleardata if  int(v.get('Z'))== EL and v.get('A') == A]
        except:
            res=[v for v in nucleardata if  v.get('SYM').strip().upper()== EL.upper() and v.get('A') == A]
        return res[0].get('Qalpha')

    def is_alpha(self,EL,A):
        try:
            val= self.getQalpha(EL,A)>0
        except:
            val = False
        return val

    def is_beta(self,EL,A):
        try:
            val= self.getQbeta(EL,A)>0
        except:
            val = False
        return val

    def is_posi(self,EL,A):
        try:
            val= self.getQposi(EL,A)>0
        except:
            val = False
        return val

    def is_ec(self,EL,A):
        try:
            val= self.getQec(EL,A)>0
        except:
            val = False
        return val

    def getIsotopes(self,EL):
        try:
            EL = int(eval(str(EL)))

        except:
            res=[v for v in nucleardata if  v.get('SYM').strip().upper()== EL.upper()]
        else:
            res=[v for v in nucleardata if  int(v.get('Z'))== EL]

        isotopes=[]
        for v in res:
            isotopes.append(int(v.get('A')))
        return isotopes

    def getZ(self,name):
        res=[v for v in elementdata if  v.get('SYM').strip().upper()== name.upper() or v.get('NAME').strip().upper()== name.upper()]
        return int(res[0].get('Z'))

    def getSymbol(self,namez):
        try:
            namez = int(eval(str(namez)))
        except:
            pass
        res=[v for v in elementdata if  int(v.get('Z'))== namez or v.get('NAME').strip().upper()== str(namez).upper()]
        return res[0].get('SYM').strip()

    def getName(self,namez):
        try:
            namez = int(eval(str(namez)))
        except:
            pass
        res=[v for v in elementdata if  int(v.get('Z'))== namez or v.get('SYM').strip().upper()== str(namez).upper()]
        return res[0].get('NAME').strip()

    def getSpin(self,EL,A):
        try:
            EL = int(eval(str(EL)))
            res=[v for v in nucleardata if  int(v.get('Z'))== EL and v.get('A') == A]
        except:
            res=[v for v in nucleardata if  v.get('SYM').strip().upper()== EL.upper() and v.get('A') == A]
        return res[0].get('spin-parity')

    def makeNucRandom(self,count,repeater):
        alist=[]
        zlist=[]
        for k in range(0,count):
            if repeater==0:
                ok=0
                while ok==0:
                    myz=nucrandom.randint(1,118)
                    ok=1 if (myz not in zlist) else 0
                zlist.append(myz)
            else:
                zlist.append(nucrandom.randint(1,118))
            mya=nucrandom.choice(self.getIsotopes(str(zlist[k])))
            alist.append(mya)
        return(zlist,alist)

    def getRandomA(self,EL):
        aRandomA=nucrandom.choice(self.getIsotopes(EL))
        return(aRandomA)

elementdata=[]  #list of dictionaries of Z, Elemental symbol, Name of element

filename=check_output("kpsewhich elementlist.csv", shell=True)

if sys.version_info.major == 2:
    g = open(filename[0:-2], 'rb')
else:
    g = open(filename[0:-2], 'r', newline='')
gin=csv.DictReader(g)#,quoting=csv.QUOTE_NONNUMERIC)

#headline=['Z','SYM','NAME']
for line in gin:
    elementdata.append(line)
g.close()

nucleardata=[] #list of dictionaries of nuclear mass, halflife, etc.

filename=check_output("kpsewhich massdata.csv", shell=True)

if sys.version_info.major == 2:
    g = open(filename[0:-2], 'rb')
else:
    g = open(filename[0:-2], 'r', newline='')
gin=csv.DictReader(g,quoting=csv.QUOTE_NONNUMERIC)

#headline=["C","N","Z","A","SYM","ORIG","Excess","unc1","BEA","unc2","btype","benergy","bunc","mass","munc","halflife","spin-parity","Qbeta","Qposi","Qec","Qalpha"]

for line in gin:
    nucleardata.append(line)
g.close()

nuc=Nucdata()

=>PYTHONTEX:SETTINGS#
version=0.16
outputdir=pythontex-files-nucleardata
workingdir=.
workingdirset=false
gobble=none
rerun=default
hashdependencies=default
makestderr=false
stderrfilename=full
keeptemps=none
pyfuture=default
pyconfuture=none
pygments=true
pygglobal=:GLOBAL||
fvextfile=-1
pyconbanner=none
pyconfilename=stdin
depythontex=false
pygfamily=py|python|
pygfamily=pycon|pycon|
pygfamily=sympy|python|
pygfamily=sympycon|pycon|
pygfamily=pylab|python|
pygfamily=pylabcon|pycon|