###################################################################################### ##### Classe que define o vetor que representa um espectro de perda de energia ##### ###################################################################################### import scipy from pylab import * ### Contem funcoes matematicas ### from scipy.weave import * ### Biblioteca para implementar codigo em C ### PI = math.pi class spectro: def __init__(self, N, x): self.eshift = 0. ### Deslocamento em energia ### self.mshift = 0 ### MEIS shift ### self.Emin = 0. self.Emax = 0. self.stepsize = x ### Passo em energia ### self.spectro = zeros(N) ### Representa as contagens do espectro ### self.size = N ### Tamanho do Vetor ### def norma(self): ### Retorna a area total do espectro ### return sum(self.spectro)*self.stepsize def axisenergy(self): ### Retorna o vetor com as energias analogas a self.spectro ### ae = arange(self.eshift,((self.size*self.stepsize)+self.eshift-1),self.stepsize) return ae def resize(self,M): ### Altera o tamanho do vetor para M ### N = self.size self.spectro = list(self.spectro) if M > N: for j in range(M-N): self.spectro.insert(N+j+1,0) elif N > M: for j in range(N-M): del self.spectro[-1] self.size = M def clear(self): ### Limpa (zera) o espectro ### self.spectro = zeros(self.size) self.eshift = 0. def reverse(self): temp = list() for j in range(self.size): temp.insert(j, self.spectro[int(self.size-j-1)]) self.spectro = temp def multiply(self, factor): ### Multiplica o espectro por um fator ### for j in range(self.size): self.spectro[j] = self.spectro[j]*factor def soma(self, lista): ### Soma um vetor ou lista ao espectro ### if len(lista) > self.size: self.resize(len(lista)) for j in range(len(lista)): self.spectro[j] = self.spectro[j] + lista[j] def setnorma(self, norman): ### Ajusta o espectro para conter a area dada ### normaold = self.norma() for n in range(self.size): self.spectro[n] = self.spectro[n]*nan_to_num(norman/normaold) def normalize(self): ### Ajusta o espectro para conter a area = 1 ### self.spectro = self.spectro/self.norma() def value(self, x): ### Retorna o valor ### return self.spectro[self.eshift + x/self.stepsize] def venergy(self, energy): ### Retorna o valor em energia ### i = int(math.floor((self.eshift - energy + self.mshift)/self.stepsize)) if i < 0 or i > self.size: return 0 else: return (self.spectro[i+1]-self.spectro[i])/(self.stepsize*(energy+self.eshift)+self.spectro[i]-i*self.spectro[i+1]+self.spectro[i]) def setprimary(self, N, step, Emin, Emax): imin = math.ceil(Emin/step) imax = math.floor(Emax/step) self.clear(); self.resize(Npontos); for i in arange(imin,imax,1): self.Data[i]=1.*(1./(i+0.5)-1./(i-0.5))*step**(-2.) if((imin-0.5)*stepEmin): self.Data[imin-1]=1.*(1./(step*(imin-0.5))-1./Emin)/step if(Emax<(imax+0.5)*step): self.Data[imax]-=1.*(1./(step*(imax+0.5))-1./Emax)/step if(Emax>(imax+0.5)*step): self.Data[imax+1]=1.*(1./Emax-1./(step*(imax+0.5)))/step self.normalize() def firstmoment(self): ### Retorna o primeiro momento da distribuicao ### moment=0.; i=0; #for i in arange(0,self.size,1): # moment = moment + (1.*i*self.stepsize)*self.spectro[i] while moment < self.norma()/2.: moment = moment +self.spectro[i]*self.stepsize i= i +1 return i*self.stepsize def secondmoment(self): ### Retorna o segundo momento da distribuicao ### moment=0; for i in arange(0,self.size,1): moment = moment + (1.*i*self.stepsize)**2*self.spectro[i] #a = list() #for k in range(self.size): # a.insert(k, [self.axisenergy()[k],self.spectro[k]]) #return np.var(a)# return moment/self.norma() #print self.axisenergy() def thirdmoment(self): ### Retorna o terceiro momento da distribuicao ### moment=0; for i in arange(0,self.size,1): moment = moment + (1.*i*self.stepsize)**3*self.spectro[i] return moment/self.norma() def ConvolveGauss(self, wg): ### Convolui o espectro com uma curva normal (gaussiana) ### normaantes = self.norma() stepsize = self.stepsize self.resize(self.size + int(math.ceil(8.*wg/stepsize))) datasize = int(self.size) data = tuple(self.spectro) ### Utiliza inline tools para rodar em C e obter um ganho em performance ### code = """ py::tuple temp(datasize); double datadois[datasize]; double expo[datasize]; double wg2=double(wg)*double(wg); double wg4_step=4.0*double(wg)/stepsize; double temptemp[datasize]; for (int k=0;k