Exemplo n.º 1
0
    def __init__(self,path,field="n",meta=None,fast_center=True,get_Xc=True,
                 get_lambda=True,debug=False,fast = True):
    
        self.path=path
        self.field=field
        #print 'read the inp'
        f=open(path+'/BOUT.log.0', 'r')
        self.md5 = hashlib.md5(f.read()).hexdigest()
        f.close()


        
        defaults = {'dx':1,'x0':0,'dy':1,'y0':0,'log_n':False}   

        for key,val in defaults.items():
            if not hasattr(self,key):
                #print 'setting: ',key,val
                setattr(self, key, val)
                
        #read all metadata from BOUT.inp
        #print 'read the inp'
        inp = read_inp(path=path,boutinp='BOUT.inp')
        inp = parse_inp(inp) #inp file
        
        
        for k, v in inp.items():
                setattr(self, k, v)
          
        for k,v in (getattr(self,'[main]')).items():
            #print k,v
            try:
                setattr(self, str.lower(k), float(v))
            except:
                setattr(self, str.lower(k), v)


        for k,v in (getattr(self,'[physics]')).items():
            #print k,v
            try:
                setattr(self, str.lower(k), float(v))
            except:
                try:
                    setattr(self, str.bool(k), v)
                except:
                    setattr(self, str.lower(k), v)
       

        if meta is not None:
            for k, v in meta.items():
                setattr(self, k, v)

      
        self.mz = self.mz-1
        self.nz  = self.mz       #self.nz = self.np.squeeze(collect("MZ",xind=[0,0],path=path,info=False))-1
        nxpe = self.nxpe# np.squeeze(collect("NXPE",xind=[0,0],path=path,info=False))
        mxg = self.mxg #np.squeeze(collect("MXG",xind=[0,0],path=path,info=False))
        
        self.mxsub = np.squeeze(collect("MXSUB",xind=[0,0],path=path,info=False)) #gaurds
        self.nx = int(self.mxsub*nxpe + 2*mxg) #including gaurds
        nx,ny,nz = self.nx,self.nz,self.nz
        
        self.dx = np.squeeze(collect("dx",path=path,xind=[0,0]))
        self.dy = np.squeeze(collect("dz",path=path,xind=[0,0]))
        self.zmax = np.squeeze(collect("ZMAX",path=path))

        self.time = np.array(np.squeeze(collect("t_array",path=path,xind=[0,0])))
        nt = self.nt = len(self.time)

        try:
            self.nave = np.squeeze(collect("nave",path=path))
            self.nave = self.nave/nt
            have_nave = True
        except:
            have_nave = False

        try:
            self.G = np.squeeze(collect("gamma",path=path))
            self.G = self.G/nt
            have_flux = True
        except:
            havex_flux = False
           
        self.debug = debug

        if debug:        
            t_chunk = 20
            t_stop  = np.max(self.nt)
            t1 = np.int(self.nt*.90)
        else:
            t_chunk = 30
            t_stop  = np.max(self.nt)
            t1 = 0 #self.time[0]

        self.t_chunk  = t_chunk  
        self.t1 = t1
        self.t_stop = t_stop
        tarray = np.array(np.arange(nt - nt/2))
        
        t2 = np.min([t1+t_chunk,t_stop])

        self.xmoment = {'1':[],'2':[]}
        self.ymoment = {'1':[],'2':[]}
        self.max_val = []
        
        
        y0,x0 = (self.y0,self.x0)
        dx,dy = (self.dx,self.dy)

        
        ymin = y0
        xmin = x0
        xmax = nx*dx + xmin
        ymax = ny*dy + ymin

        self.kx_max = nx
        self.ky_max = ny
        kxmax,kymax = nx,ny
        kxmin = 0.
        kymin = 0.
   
        dky = self.dky = 1.0/self.zmax
        dkx = self.dkx = 2.*np.pi/self.dx
        self.kx = np.arange(0,nx)*self.dkx
        self.ky = np.arange(0,ny)*self.dky

        #print nx,nx*self.mxsub
        self.pos_i = np.mgrid[0:nx:1,0:ny:1]
        self.x = np.mgrid[xmin:xmax:self.dx]
        self.y = np.mgrid[xmin:xmax:self.dy]
        
       
        sys.stdout = mystdout = StringIO()
        a = np.squeeze(collect("alpha",path=path))
        sys.stdout = old_stdout
        

        #self.a_ave 
        a_ave = self.a_ave = np.average(a,axis=1)
        # popt, pcov= curve_fit(fit_tanh,xnew,a_ave,p0=p0)
        # w = run['w'] =  popt[0]*dx

        
        try:  
            xstart = np.min(np.where((a_ave-.1*a_ave.max()>0)) )
        
            self.xstart = xstart
            
            xstop = np.int(xstart+ nx/2.)
            if xstop > nx:
                xstop = nx -1;
        except:
            xstart= np.int(nx/3.)            
            xstop = np.int(xstart+ nx/2.)

        self.fast = fast
        #will return 'real n' not 'log n' statistics and profiles, spasly sampled in x, dense in t
        moving_min_n, moving_max_n, moving_min, moving_max, nave_net, nrms_net ,nmin_net,nmax_net = self.compute_profile() 
        pdf_x = []
        df_x = []
        
        
        self.nbins = nbins = 300
        for x in xrange(nx):
            #print moving_min_n[x]
            pdf_x.append(np.mgrid[moving_min_n[x]-.01:moving_max_n[x] +.01:complex(0,nbins)])
            df_x.append(np.mgrid[moving_min[x]-.01:moving_max[x] +.01:complex(0,nbins)])
            
        
        
        self.pdf_x = pdf_x
        self.df_x = df_x

        pdf_y=nx*[None]
        df_y=nx*[None]

        flux_pdf_y=nx*[None]
        flux_df_y =nx*[None]
        pdf_y_net = 0
        df_y_net = 0
        # self.moving_min_n= moving_min_n
        # self.moving_max_n= moving_max_n
        # self.moving_min= moving_min
        # self.moving_max= moving_max


        for i in range(nx):
            #print "some unique object %d" % ( i, )
            pdf_y[i]= 0*pdf_x[i]
            df_y[i]= 0*pdf_x[i]
            flux_pdf_y[i]= 0*pdf_x[i]
            flux_df_y[i]= 0*pdf_x[i]

        
        
        xstart = np.min(np.where(a_ave - 0.1 * a_ave.max() > 0))
        xstop = np.int(xstart + nx / 2.0)
        x1 = np.int(xstart)
        x2 = np.int(xstart + nx/5.0)
        # x1 = np.min(np.where((a_ave-.1*a_ave.max()>0)) )
        # x2 = np.min(np.where((a_ave-.9*a_ave.max()>0)) )

        df_x_start = np.array(df_x[x1:x2]).min()
        df_x_stop = np.array(df_x[x1:x2]).max()
        pdf_x_start = np.array(pdf_x[x1:x2]).min()
        pdf_x_stop = np.array(pdf_x[x1:x2]).max()
        self.df_x_start = df_x_start
        self.df_x_stop = df_x_stop
        self.pdf_x_start = pdf_x_start
        self.pdf_x_stop = pdf_x_stop
        if xstop > nx:
            xstop = nx - 1
            
        j = 0
        print 't2,t_stop:', t2, t_stop
        self.nave = []
        self.flux = []
        self.v = []
        while t2 < t_stop:
            print 'computing statistical measures'
            (nave, dn), (flux, flux_max, d_flux), (vx,vy),data = self.dens_and_flux(t1, t2)
            self.save_linlam(np.log(nave), np.log(dn))
            self.nave.append([nave, dn])
            self.flux.append([flux, flux_max, d_flux])
            self.v.append([vx,vy])
            #print len(self.flux)
            ntt, nxx, nyy = data.shape
            #print 'data.shape: ', data.shape, t1, t_stop
            if t1 > np.int(2*t_stop)/3:
                j = j + 1
                for x in xrange(nx):
                    if not fast:
                        p = np.polyfit(np.arange(ntt), np.squeeze(data[:, x, :]).mean(axis=1), 1)
                        nave2d = p[1] + p[0] * np.arange(ntt)
                        
                        p = np.polyfit(np.arange(ntt), np.squeeze(flux[:, x, :]).mean(axis=1), 1)
                        flux2d_x = p[1] + p[0] * np.arange(ntt)
                        if self.log_n:
                            cond = np.exp(data[:, x, :]) - np.exp(np.reshape(np.repeat(nave2d, ny), [ntt, ny]))
                            cond_flux =  np.exp(data[:, x, :])*vy - np.reshape(np.repeat(flux2d_x, ny), [ntt, ny])
                        else:
                            cond = data[:, x, :] - np.reshape(np.repeat(nave2d, ny), [ntt, ny])
                    elif self.log_n:
                        #print nave.shape,data.shape
                        cond = np.exp(data[:, x, :]) - nave[x]
                    else:
                        cond = data[:, x, :] - nave[x]
                    nrms = cond.std()
                    datamax = data[:, x, :].max()
                    if np.isfinite(datamax):
                        dfadd = np.squeeze(np.histogram(cond, bins=nbins, normed=False, range=(df_x[x].min(), df_x[x].max()))[0])
                        pdfadd = np.squeeze(np.histogram(cond / nrms, bins=nbins, normed=False, range=(pdf_x[x].min(), pdf_x[x].max()))[0])
                        if np.isfinite(np.sum(pdfadd)):
                            pdf_y[x] = pdf_y[x] + pdfadd
                            df_y[x] = df_y[x] + dfadd
                            if abs(x - xstart) < nx / 20.0:
                                pdf_y_net = pdf_y_net + np.squeeze(np.histogram(cond / nrms, bins=nbins, normed=False, range=(pdf_x_start, pdf_x_stop))[0])
                                #print x1, x2
                                df_y_net = df_y_net + np.squeeze(np.histogram(cond, bins=nbins, normed=False, range=(df_x_start, df_x_stop))[0])

            self.pdf_y = pdf_y
            self.pdf_y_net = pdf_y_net
            self.df_y = df_y
            self.df_y_net = df_y_net
            self.t.append(t1 + 0.5 * (t2 - t1))
            t1 = t2 + 1
            t2 = np.min([t1 + t_chunk - 1, t_stop + 1])

        self.nave = np.array(self.nave)
        self.flux = np.array(self.flux)
        self.pdf_stats = []
        self.df_stats = []
        print 'almost done'
        for x in xrange(nx):
            if np.mean(nrms_net[:, x]) != 0:
                self.pdf_y[x] = pdf_y[x] / sum(pdf_y[x])
                self.df_y[x] = df_y[x] / sum(df_y[x])
            else:
                self.pdf_x[x] = pdf_x[x - 1]
                
        if hasattr(self, 'pdf_y_net'):
            self.pdf_y_net = pdf_y_net / np.float(sum(pdf_y_net))
            self.df_y_net = df_y_net / np.float(sum(df_y_net))
Exemplo n.º 2
0
# inp = read_inp(path=path,boutinp='BOUT.inp')
# inp = parse_inp(inp)

# inp2 = {}
# for k, v in inp.items():
#      inp2[k] = v

# print inp2

#read the data and process
t1= 0
t2 = 200
try:
     inp = read_inp(path=path,boutinp='BOUT.inp')
     inp = parse_inp(inp)
     
     dx = np.double(inp['[mesh]']['dx'])
     #print 'dx',dx
     zmax = np.double(inp['[main]']['ZMAX'])

     print dx,zmax
     n = (np.squeeze(collect("n",path=path,tind =[t1,t2])))
     print dx,zmax
     n0 = (np.squeeze(collect("n0",path=path,tind =[t1,t2])))
     u = np.squeeze(collect("u",path=path,tind =[t1,t2]))
     phi = np.squeeze(collect("phi",path=path,tind =[t1,t2]))
     time = np.squeeze(collect("t_array",path=path,xind=[0,0]))
     #dx = np.squeeze(collect("dx",path=path))
     #zmax = np.squeeze(collect("ZMAX",path=path))
    
Exemplo n.º 3
0
cache=path+'_movie'

nfile = path+ 'nfile.dat'
ufile = path+ 'ufile.dat'
phifile = path+ 'phifile.dat'
Akfiile = path+'Akfile.dat'
from boutdata import collect
from boutdata import collect2 
from collect2 import collect2

from boututils import savemovie

import numpy as np

inp = read_inp(path=path)
meta = parse_inp(inp)
#print meta
nz = np.double(meta['[main]']['MZ'])
nxpe = np.double(meta['[main]']['NXPE'])
nype = np.double(meta['[main]']['NYPE'])
mxg = np.double(meta['[main]']['MXG'])
dx =  np.double(meta['[mesh]']['dx'])
zmax = np.double(meta['[main]']['ZMAX'])
#mxsub = meta['[main]']['']
#nz = np.squeeze(collect("MZ",xind=[0,0],path=path,info=False))
#nxpe=  np.squeeze(collect("NXPE",xind=[0,0],path=path,info=False))
mxsub = 15
#mxsub = np.squeeze(collect("MXSUB",xind=[0,0],path=path,info=True)) #without gaurds
#mxg = np.squeeze(collect("MXG",xind=[0,0],path=path,info=False))

nx = mxsub*nxpe + 2*mxg