# Demo entry 6637229

Python 3

Submitted by anonymous on Aug 30, 2017 at 15:19
Language: Python 3. Code size: 2.4 kB.

```########################################################################
#SRIM Full Damage mode - weighted recoil spectrum
########################################################################
import numpy as np

Epka=[]  #List of recoils energy
Disp=[]   #List of displacements

with open('COLLISON_FD.txt','r+',encoding = 'cp1252') as f:
for line in f:
if 'Summary' in line:
line=line.replace(',','.')
CatchE=line[63:73] #Extract recoils energy
CatchD=line[74:80] #Extract displacements number
Epka.append(float(CatchE))
Disp.append(float(CatchD))

Epka=np.array(Epka) #Convert list to array
Disp=np.array(Disp) #Convert list to array
DispSum=Disp.sum()  #Summation of total displacements

#Fraction of displacements
for i in range(len(Disp)):
Disp[i]=Disp[i]/DispSum

#Sort the list of recoils energy and of fraction of displacements
EpkaSort,DispSort=(list(x) for x in zip(*sorted(zip(Epka,Disp))))
#Convert list to array after sort, which is the energy used in the figure
EpkaSortA=np.array(EpkaSort)
DispSortA=np.array(DispSort) #Convert list to array after sort

#Perform cumulative summation on array of fraction of displacements,
#which is the weighted fraction of recoils used in the figure
DispSortC=DispSortA.cumsum()

#Calculate the half fraction energy Eh by linear interpolation
for hc in range(len(DispSortC)):
if DispSortC[hc] > 0.5:
n=hc
break

Eh=((0.5-DispSortC[n-1])/(DispSortC[n]-DispSortC[n-1]))*\
(EpkaSortA[n]-EpkaSortA[n-1])+EpkaSortA[n-1]

#Export the list of recoils energy and of weighted fraction
#into the file of "FD_Weighted_Spectrum.txt"
with open('FD_Weighted_Spectrum.txt','w',encoding='cp1252') as h:
h.write('# ')
h.write('%11s '%('Energy(eV)'))
h.write('%17s '%('Weihted fraction'))
h.write('\n')
for ii in range(len(EpkaSortA)):
h.write('%13.6e '%(EpkaSortA[ii]))
h.write('%13.6e '%(DispSortC[ii]))
h.write('\n')
########################################################################
#SRIM Full Damage mode - weighted recoil spectrum
########################################################################
```

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.