profile.py 5.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
from pylab import *
import scipy
from scipy.weave import *
from scipy.special import *
from matplotlib import *
PI = math.pi

class profile:
    def __init__(self, N, x):
        self.shift = 0.
        self.xmin = 0.
        self.xmax = N*x
        self.stepsize = x
14
        self.data = zeros(N)
15 16 17 18
        self.size = N
        self.contam = 0.
        
    def axisdepth(self):
19
        return arange(self.contam, self.size*self.stepsize+self.contam, self.stepsize)
20 21 22 23

    def clear(self):
        self.data = zeros(self.size)

24 25 26 27 28
    def soma(self, qt):
        for j in range(self.size):
            if self.data[j] > 0: 
                self.data[j] = self.data[j] + qt

29 30
    def resize(self, size):
        N = self.size
31 32
        M = size
        self.data = list(self.data)
33 34 35 36 37 38 39 40 41
        if M > N:
            for j in range(M-N):
                self.data.insert(N+j,0)
        elif N > M:
            for j in range(N-M):
                self.data.pop()
        self.size = M
        self.xmax = M*self.stepsize

42 43 44 45 46 47 48 49 50 51 52 53
    def shiftx(self, x, x2):
        self.data = list(self.data)
        if x < x2:
            for y in arange(int(x/self.stepsize), int(x2/self.stepsize)):
                self.data.insert(y,0)
                self.data.pop()
        elif x > x2: # funciona mal
            for y in arange(int(x2/self.stepsize), int(self.xmax/self.stepsize)):
                if y < self.size-1:
                    self.data[y] = self.data[y+1]
                self.data[self.size-1] = 0

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
    def setcontam(self, newcontam):
        delta = newcontam-self.contam
        dx = math.ceil(delta/self.stepsize)
        if (delta < 0):
            for i in arange(math.ceil(newcontam*self.stepsize),self.xmax):
                self.data[i] = self.data[i-dx]
            resize(self.size+delta)
        elif (delta > 0):
            self.resize(self.size + delta)
            datatemp = profile(self.xmax,self.stepsize)
            datatemp.data = self.data
            for i in arange(math.floor(newcontam/self.stepsize),self.xmax):
                self.data[i] = datatemp.data[i-dx]
        contam = newcontam
        for i in range(math.ceil(newcontam/self.stepsize)):
            data[i]=0

    def drawline(self, xa, xb, ca, cb):
        contam = self.contam
        stepsize = self.stepsize
        if (xb >= self.xmax):
            xb = self.xmax 
        if (xa < 0):
            xa = 0.
        if (xa >= self.xmax):
            xa = self.xmax
        if (xb < 0):
            xb = 0
        if (xa < xb):
83
            for i in arange(math.floor(xa/stepsize), math.floor(xb/stepsize),1):
84 85 86 87 88 89
                if (i > math.floor(contam/stepsize)):
                    a = (cb - ca) / (xb - xa)
                    b = -a*xa + ca
                    conc = a*(1.*i*stepsize) + b
                    if (conc < 0):
                        conc = 0.
90
                    self.data[int(i)] = conc;
91
        if (xb < xa):
92
            for i in arange(math.floor(xb/stepsize), math.floor(xa/stepsize),1):
93 94 95 96 97 98
                if (i > math.floor(contam/stepsize)):
                    a = (ca - cb) / (xa - xb)
                    b = -a*xb + cb
                    conc = a*(1.*i*stepsize) + b
                    if(conc < 0):
                        conc = 0
99
                    self.data[int(i)] = conc
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
              
    def adderfc(self, qtd, sigma):
        xsigma = self.xmax
        ratio = self.contam/self.stepsize
        if (4.*sigma/self.stepsize < self.xmax):
            xsigma = 4.*sigma/self.stepsize
        for i in arange(math.ceil(ratio)+1, xsigma+ratio):
            j = i - math.floor(ratio)
            self.data[i] = self.data[i] + qtd*scipy.special.erfc(j*self.stepsize/sqrt(2.)/sigma);

    def remerfc(self, qtd, sigma):
        xsigma = self.xmax
        ratio = self.contam/self.stepsize
        if (4.*sigma/self.stepsize < xmax):
            xsigma = 4.*sigma/self.stepsize
        for i in arange(math.ceil(ratio)+1, xsigma+ratio):
            j = i - math.floor(ratio)
            self.data[i] = self.data[i] - qtd*scipy.special.erfc(j*self.stepsize/sqrt(2.)/sigma)

    def erfc(self, x):
        passo = .005
        valor = 0.
        for i in range(x/passo + 1):
            valor += exp(-(i*passo)**2)
            erf = 1. - 2.*valor*passo/sqrt(PI)
        if erf<0:
            erf = 0.
        return erf

    def quant(self):
        soma = 0.
        for i in range(self.size):
            soma = soma + self.data[i]
        soma = soma*self.stepsize
        return  soma

    def Kn(self, n, m, tltcorr): #Nao compila
        tltcorr = 1.
        stepsize = self.stepsize
        data = tuple(self.data)
        xmax = self.xmax
        datasize = int(self.size)
        code = """
            long double fatorial(int a)
            {
            int aa;
            long double valor=1.0;
            for(aa=1;aa<=a;aa++)
                valor=valor*aa;
            return valor;
            }

            double datab[datasize];
            for (int k=0;k<datasize;k++)
                datab[k] = data[k];
            long double kk=0.0;
            if(n==0){
               for(int x=0;x<xmax;x++) 
                   kk = kk + 1.0*exp(-1.0*M*x*stepsize*tltcorr)*datab[x];
               kk = kk*pow(stepsize,1.0);}
            else{
                for(int x=0;x<xmax;x++)
                    kk = kk + 1.0*powl(1.0*x*tltcorr/(1.0/stepsize),n)*exp(-1.0*M*x*tltcorr/(1.0/stepsize))*datab[x];
                kk = kk*1.0*(1.0*powl(1.0*M,n)/(1.0*fatorial(n)*1.0/stepsize));}
            return_val =  kk/stepsize*1000.0;}
            """
        return inline_tools.inline(code,['n','m','tltcorr','stepsize','data','xmax', 'datasize'],type_converters=converters.blitz,compiler = 'gcc')

    def smooth(self, qts):
        for n in range(qts):
            for i in arange(math.ceil(self.contam/self.stepsize)+4,self.xmax-6):
                self.data[i] = (self.data[i-3]+self.data[i-2]+self.data[i-1]+self.data[i]+self.data[i+1]+self.data[i+2]+self.data[i+3])/7.