コード例 #1
0
ファイル: utils.py プロジェクト: kastalpes/newscripts
def pc_calc_helk(var,k):
    """return the helicity density at k, computed in real space

    H_k = < A_k * B_k> V/dk

    and 

    M_k  = < B_k**2 > V/dk (magnetic energy spectrum)

    since the latter comes for free

    see Brandenburg & Subramanian (2005) Phys Rep pp30-31 for derivation
    """

    aa_k = pc_calc_Ak(var,k)
    bb_k = pc.curl(aa_k,var.dx,var.dy,var.dz)
    
    hel = (pc.dot(aa_k,bb_k))[3:-3,3:-3,3:-3]
    mag  = (pc.dot2(bb_k))[3:-3,3:-3,3:-3]
    vol = (var.x[-3]-var.x[3])*(var.y[-3]-var.y[3])*(var.z[-3]-var.z[3])

    # NB: this assumes cubic zones
    dk = FourierFilter(aa_k[0]).dk * 2*na.pi/var.dx
    if k == 0:
        k = 1

    # i think it should be k, and not dk; but i'm not sure yet
    return hel.mean()/k, mag.mean()/(2.*k)
コード例 #2
0
ファイル: pc2vtk.py プロジェクト: belfhi/pencil-code
def pc2vtk_vid(
    ti=0,
    tf=1,
    datadir="data/",
    proc=-1,
    variables=["rho", "uu", "bb"],
    magic=[],
    b_ext=False,
    destination="animation",
    quiet=True,
):
    """
    Convert data from PencilCode format to vtk.

    call signature::
    
      pc2vtk(ti = 0, tf = 1, datadir = 'data/', proc = -1,
           variables = ['rho','uu','bb'], magic = [],
           destination = 'animation')
    
    Read *varfile* and convert its content into vtk format. Write the result
    in *destination*.
    
    Keyword arguments:
    
      *ti*:
        Initial time.
        
      *tf*:
        Final time.
        
      *datadir*:
        Directory where the data is stored.
       
      *proc*:
        Processor which should be read. Set to -1 for all processors.
      
      *variables* = [ 'rho' , 'lnrho' , 'uu' , 'bb', 'b_mag', 'jj', 'j_mag', 'aa', 'ab', 'TT', 'lnTT', 'cc', 'lncc', 'ss', 'vort' ]
        Variables which should be written.
        
      *magic*: [ 'vort' , 'bb' ]
        Additional variables which should be written.
       
      *b_ext*:
        Add the external magnetic field.
        
      *destination*:
        Destination files without '.vtk' extension. 
        
      *quiet*:
        Keep quiet when reading the var files.
    """

    # this should correct for the case the user type only one variable
    if len(variables) > 0:
        if len(variables[0]) == 1:
            variables = [variables]
    # this should correct for the case the user type only one variable
    if len(magic) > 0:
        if len(magic[0]) == 1:
            magic = [magic]

    # make sure magic is set when writing 'vort' or 'bb'
    try:
        index = variables.index("vort")
        magic.append("vort")
    except:
        pass
    try:
        index = variables.index("bb")
        magic.append("bb")
    except:
        pass
    try:
        index = variables.index("b_mag")
        magic.append("bb")
    except:
        pass
    try:
        index = variables.index("jj")
        magic.append("jj")
    except:
        pass
    try:
        index = variables.index("j_mag")
        magic.append("jj")
    except:
        pass

    for i in range(ti, tf + 1):
        varfile = "VAR" + str(i)
        # reading pc variables and setting dimensions
        var = pc.read_var(varfile=varfile, datadir=datadir, proc=proc, magic=magic, trimall=True, quiet=quiet)

        grid = pc.read_grid(datadir=datadir, proc=proc, trim=True, quiet=True)

        params = pc.read_param(param2=True, quiet=True)
        B_ext = np.array(params.b_ext)
        # add external magnetic field
        if b_ext == True:
            var.bb[0, ...] += B_ext[0]
            var.bb[1, ...] += B_ext[1]
            var.bb[2, ...] += B_ext[2]

        dimx = len(grid.x)
        dimy = len(grid.y)
        dimz = len(grid.z)
        dim = dimx * dimy * dimz
        dx = (np.max(grid.x) - np.min(grid.x)) / (dimx - 1)
        dy = (np.max(grid.y) - np.min(grid.y)) / (dimy - 1)
        dz = (np.max(grid.z) - np.min(grid.z)) / (dimz - 1)

        # fd = open(destination + "{0:1.0f}".format(var.t*1e5) + '.vtk', 'wb')
        fd = open(destination + str(i) + ".vtk", "wb")
        fd.write("# vtk DataFile Version 2.0\n")
        fd.write("density + magnetic field\n")
        fd.write("BINARY\n")
        fd.write("DATASET STRUCTURED_POINTS\n")
        fd.write("DIMENSIONS {0:9} {1:9} {2:9}\n".format(dimx, dimy, dimz))
        fd.write("ORIGIN {0:8.12} {1:8.12} {2:8.12}\n".format(grid.x[0], grid.y[0], grid.z[0]))
        fd.write("SPACING {0:8.12} {1:8.12} {2:8.12}\n".format(dx, dy, dz))
        fd.write("POINT_DATA {0:9}\n".format(dim))

        try:
            index = variables.index("rho")
            print("writing rho")
            fd.write("SCALARS rho float\n")
            fd.write("LOOKUP_TABLE default\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.rho[k, j, i]))
        except:
            pass

        try:
            index = variables.index("lnrho")
            print("writing lnrho")
            fd.write("SCALARS lnrho float\n")
            fd.write("LOOKUP_TABLE default\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.lnrho[k, j, i]))
        except:
            pass

        try:
            index = variables.index("uu")
            print("writing uu")
            fd.write("VECTORS vfield float\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.uu[0, k, j, i]))
                        fd.write(struct.pack(">f", var.uu[1, k, j, i]))
                        fd.write(struct.pack(">f", var.uu[2, k, j, i]))
        except:
            pass

        try:
            index = variables.index("bb")
            print("writing bb")
            fd.write("VECTORS bfield float\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.bb[0, k, j, i]))
                        fd.write(struct.pack(">f", var.bb[1, k, j, i]))
                        fd.write(struct.pack(">f", var.bb[2, k, j, i]))
        except:
            pass

        try:
            index = variables.index("b_mag")
            b_mag = np.sqrt(pc.dot2(var.bb))
            print("writing b_mag")
            fd.write("SCALARS b_mag float\n")
            fd.write("LOOKUP_TABLE default\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", b_mag[k, j, i]))
        except:
            pass

        try:
            index = variables.index("jj")
            print("writing jj")
            fd.write("VECTORS jfield float\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.jj[0, k, j, i]))
                        fd.write(struct.pack(">f", var.jj[1, k, j, i]))
                        fd.write(struct.pack(">f", var.jj[2, k, j, i]))
        except:
            pass

        try:
            index = variables.index("j_mag")
            j_mag = np.sqrt(pc.dot2(var.jj))
            print("writing j_mag")
            fd.write("SCALARS j_mag float\n")
            fd.write("LOOKUP_TABLE default\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", j_mag[k, j, i]))
        except:
            pass

        try:
            index = variables.index("aa")
            print("writing aa")
            fd.write("VECTORS afield float\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.aa[0, k, j, i]))
                        fd.write(struct.pack(">f", var.aa[1, k, j, i]))
                        fd.write(struct.pack(">f", var.aa[2, k, j, i]))
        except:
            pass

        try:
            index = variables.index("ab")
            ab = pc.dot(var.aa, var.bb)
            print("writing ab")
            fd.write("SCALARS ab float\n")
            fd.write("LOOKUP_TABLE default\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", ab[k, j, i]))
        except:
            pass

        try:
            index = variables.index("TT")
            print("writing TT")
            fd.write("SCALARS TT float\n")
            fd.write("LOOKUP_TABLE default\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.TT[k, j, i]))
        except:
            pass

        try:
            index = variables.index("lnTT")
            print("writing lnTT")
            fd.write("SCALARS lnTT float\n")
            fd.write("LOOKUP_TABLE default\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.lnTT[k, j, i]))
        except:
            pass

        try:
            index = variables.index("cc")
            print("writing cc")
            fd.write("SCALARS cc float\n")
            fd.write("LOOKUP_TABLE default\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.cc[k, j, i]))
        except:
            pass

        try:
            index = variables.index("lncc")
            print("writing lncc")
            fd.write("SCALARS lncc float\n")
            fd.write("LOOKUP_TABLE default\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.lncc[k, j, i]))
        except:
            pass

        try:
            index = variables.index("ss")
            print("writing ss")
            fd.write("SCALARS ss float\n")
            fd.write("LOOKUP_TABLE default\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.ss[k, j, i]))
        except:
            pass

        try:
            index = variables.index("vort")
            print("writing vort")
            fd.write("VECTORS vorticity float\n")
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.vort[0, k, j, i]))
                        fd.write(struct.pack(">f", var.vort[1, k, j, i]))
                        fd.write(struct.pack(">f", var.vort[2, k, j, i]))
        except:
            pass

        del (var)

        fd.close()
コード例 #3
0
def pc2vtk_vid(ti=0,
               tf=1,
               datadir='data/',
               proc=-1,
               variables=['rho', 'uu', 'bb'],
               magic=[],
               b_ext=False,
               destination='animation',
               quiet=True):
    """
    Convert data from PencilCode format to vtk.

    call signature::
    
      pc2vtk(ti = 0, tf = 1, datadir = 'data/', proc = -1,
           variables = ['rho','uu','bb'], magic = [],
           destination = 'animation')
    
    Read *varfile* and convert its content into vtk format. Write the result
    in *destination*.
    
    Keyword arguments:
    
      *ti*:
        Initial time.
        
      *tf*:
        Final time.
        
      *datadir*:
        Directory where the data is stored.
       
      *proc*:
        Processor which should be read. Set to -1 for all processors.
      
      *variables* = [ 'rho' , 'lnrho' , 'uu' , 'bb', 'b_mag', 'jj', 'j_mag', 'aa', 'ab', 'TT', 'lnTT', 'cc', 'lncc', 'ss', 'vort' ]
        Variables which should be written.
        
      *magic*: [ 'vort' , 'bb' ]
        Additional variables which should be written.
       
      *b_ext*:
        Add the external magnetic field.
        
      *destination*:
        Destination files without '.vtk' extension. 
        
      *quiet*:
        Keep quiet when reading the var files.
    """

    # this should correct for the case the user type only one variable
    if (len(variables) > 0):
        if (len(variables[0]) == 1):
            variables = [variables]
    # this should correct for the case the user type only one variable
    if (len(magic) > 0):
        if (len(magic[0]) == 1):
            magic = [magic]

    # make sure magic is set when writing 'vort' or 'bb'
    try:
        index = variables.index('vort')
        magic.append('vort')
    except:
        pass
    try:
        index = variables.index('bb')
        magic.append('bb')
    except:
        pass
    try:
        index = variables.index('b_mag')
        magic.append('bb')
    except:
        pass
    try:
        index = variables.index('jj')
        magic.append('jj')
    except:
        pass
    try:
        index = variables.index('j_mag')
        magic.append('jj')
    except:
        pass

    for i in range(ti, tf + 1):
        varfile = 'VAR' + str(i)
        # reading pc variables and setting dimensions
        var = pc.read_var(varfile=varfile,
                          datadir=datadir,
                          proc=proc,
                          magic=magic,
                          trimall=True,
                          quiet=quiet)

        grid = pc.read_grid(datadir=datadir, proc=proc, trim=True, quiet=True)

        params = pc.read_param(param2=True, quiet=True)
        B_ext = np.array(params.b_ext)
        # add external magnetic field
        if (b_ext == True):
            var.bb[0, ...] += B_ext[0]
            var.bb[1, ...] += B_ext[1]
            var.bb[2, ...] += B_ext[2]

        dimx = len(grid.x)
        dimy = len(grid.y)
        dimz = len(grid.z)
        dim = dimx * dimy * dimz
        dx = (np.max(grid.x) - np.min(grid.x)) / (dimx - 1)
        dy = (np.max(grid.y) - np.min(grid.y)) / (dimy - 1)
        dz = (np.max(grid.z) - np.min(grid.z)) / (dimz - 1)

        #fd = open(destination + "{0:1.0f}".format(var.t*1e5) + '.vtk', 'wb')
        fd = open(destination + str(i) + '.vtk', 'wb')
        fd.write('# vtk DataFile Version 2.0\n'.encode('utf-8'))
        fd.write('density + magnetic field\n'.encode('utf-8'))
        fd.write('BINARY\n'.encode('utf-8'))
        fd.write('DATASET STRUCTURED_POINTS\n'.encode('utf-8'))
        fd.write('DIMENSIONS {0:9} {1:9} {2:9}\n'.format(dimx, dimy,
                                                         dimz).encode('utf-8'))
        fd.write('ORIGIN {0:8.12} {1:8.12} {2:8.12}\n'.format(
            grid.x[0], grid.y[0], grid.z[0]).encode('utf-8'))
        fd.write('SPACING {0:8.12} {1:8.12} {2:8.12}\n'.format(
            dx, dy, dz).encode('utf-8'))
        fd.write('POINT_DATA {0:9}\n'.format(dim).encode('utf-8'))

        try:
            index = variables.index('rho')
            print('writing rho')
            fd.write('SCALARS rho float\n'.encode('utf-8'))
            fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.rho[k, j, i]))
        except:
            pass

        try:
            index = variables.index('lnrho')
            print('writing lnrho')
            fd.write('SCALARS lnrho float\n'.encode('utf-8'))
            fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.lnrho[k, j, i]))
        except:
            pass

        try:
            index = variables.index('uu')
            print('writing uu')
            fd.write('VECTORS vfield float\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.uu[0, k, j, i]))
                        fd.write(struct.pack(">f", var.uu[1, k, j, i]))
                        fd.write(struct.pack(">f", var.uu[2, k, j, i]))
        except:
            pass

        try:
            index = variables.index('bb')
            print('writing bb')
            fd.write('VECTORS bfield float\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.bb[0, k, j, i]))
                        fd.write(struct.pack(">f", var.bb[1, k, j, i]))
                        fd.write(struct.pack(">f", var.bb[2, k, j, i]))
        except:
            pass

        try:
            index = variables.index('b_mag')
            b_mag = np.sqrt(pc.dot2(var.bb))
            print('writing b_mag')
            fd.write('SCALARS b_mag float\n'.encode('utf-8'))
            fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", b_mag[k, j, i]))
        except:
            pass

        try:
            index = variables.index('jj')
            print('writing jj')
            fd.write('VECTORS jfield float\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.jj[0, k, j, i]))
                        fd.write(struct.pack(">f", var.jj[1, k, j, i]))
                        fd.write(struct.pack(">f", var.jj[2, k, j, i]))
        except:
            pass

        try:
            index = variables.index('j_mag')
            j_mag = np.sqrt(pc.dot2(var.jj))
            print('writing j_mag')
            fd.write('SCALARS j_mag float\n'.encode('utf-8'))
            fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", j_mag[k, j, i]))
        except:
            pass

        try:
            index = variables.index('aa')
            print('writing aa')
            fd.write('VECTORS afield float\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.aa[0, k, j, i]))
                        fd.write(struct.pack(">f", var.aa[1, k, j, i]))
                        fd.write(struct.pack(">f", var.aa[2, k, j, i]))
        except:
            pass

        try:
            index = variables.index('ab')
            ab = pc.dot(var.aa, var.bb)
            print('writing ab')
            fd.write('SCALARS ab float\n'.encode('utf-8'))
            fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", ab[k, j, i]))
        except:
            pass

        try:
            index = variables.index('TT')
            print('writing TT')
            fd.write('SCALARS TT float\n'.encode('utf-8'))
            fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.TT[k, j, i]))
        except:
            pass

        try:
            index = variables.index('lnTT')
            print('writing lnTT')
            fd.write('SCALARS lnTT float\n'.encode('utf-8'))
            fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.lnTT[k, j, i]))
        except:
            pass

        try:
            index = variables.index('cc')
            print('writing cc')
            fd.write('SCALARS cc float\n'.encode('utf-8'))
            fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.cc[k, j, i]))
        except:
            pass

        try:
            index = variables.index('lncc')
            print('writing lncc')
            fd.write('SCALARS lncc float\n'.encode('utf-8'))
            fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.lncc[k, j, i]))
        except:
            pass

        try:
            index = variables.index('ss')
            print('writing ss')
            fd.write('SCALARS ss float\n'.encode('utf-8'))
            fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.ss[k, j, i]))
        except:
            pass

        try:
            index = variables.index('vort')
            print('writing vort')
            fd.write('VECTORS vorticity float\n'.encode('utf-8'))
            for k in range(dimz):
                for j in range(dimy):
                    for i in range(dimx):
                        fd.write(struct.pack(">f", var.vort[0, k, j, i]))
                        fd.write(struct.pack(">f", var.vort[1, k, j, i]))
                        fd.write(struct.pack(">f", var.vort[2, k, j, i]))
        except:
            pass

        del (var)

        fd.close()
コード例 #4
0
def post_compute(variables = ['b2m'], datadir = 'data'):
    """
    Compute diagnostic variables from the VAR files.

    call signature::
    
      post_compute(variables = ['urms'], datadir = 'data')
    
    Read the VAR files and compute the diagnostic variables.
    Write the result in 'post_evaluation.dat'.
    
    Keyword arguments:
    
      *variables*:
        The diagnostic variables to be computed.
        
      *datadir*:
        Data directory.
        
    """

    # open the destination file for writing
    fd = open(datadir + '/post_evaluation.dat', 'w')
    
    # write the header
    fd.write('#--t-------------'.encode('utf-8'))
    for variable in variables:
        fd.write('--{0:15}'.format((variable+'--------------------')[:15]).encode('utf-8'))
    fd.write('\n')
    
    # read the list of all VAR files
    var_list_file = open('data/proc0/varN.list')
    var_list = var_list_file.readlines()
    var_list_file.close()
    
    if (len(variables[0]) == 1):        
        variables = [variables]
        
    # check if bb needs to be computed
    bb_flag = False
    # array containing the variables which depend on bb
    b_dep = ['ab_int', 'jb_int', 'b1m', 'b2m', 'bm2', 'abm', 'abrms', 'jbm', 'brms', 'gffz']
    if (len(set(variables + b_dep)) < len(variables + b_dep)):
        bb_flag = True
        
    # check if jj needs to be computed
    jj_flag = False
    # array containing the variables which depend on jj
    j_dep = ['jb_int', 'j2m', 'jm2', 'jbm', 'jrms']
    if (len(set(variables + j_dep)) < len(variables + j_dep)):
        jj_flag = True
    
    for var_file in var_list:
        # read the var file
        var = pc.read_var(varfile = var_file[:-1], datadir = datadir, quiet = True)
        # the output string which will be written in the destination file
        out_string = '{0:1.9e}  '.format(np.float64(var.t))
        aa = var.aa[:,var.n1:var.n2,var.m1:var.m2,var.l1:var.l2]
        if bb_flag:
            bb = pc.curl(var.aa, var.dx, var.dy, var.dz, var.x, var.y, var.z)
            bb = bb[:,var.n1:var.n2,var.m1:var.m2,var.l1:var.l2]
        if jj_flag:
            jj = pc.curl2(var.aa, var.dx, var.dy, var.dz, var.x, var.y, var.z)
            jj = jj[:,var.n1:var.n2,var.m1:var.m2,var.l1:var.l2]
            
        for variable in variables:
            if variable == 'b2m':
                b2m = (pc.dot2(bb)).mean()
                out_string += '{0:1.9e}  '.format(np.float64(b2m))
            elif variable == 'j2m':
                j2m = (pc.dot2(jj)).mean()
                out_string += '{0:1.9e}  '.format(np.float64(j2m))
                break
            elif variable == 'abm':
                abm = (pc.dot(var.aa, bb)).mean()
                out_string += '{0:1.9e}  '.format(np.float64(abm))
            # generalized flux function (see Yeates, Hornig 2011)
            elif variable == 'gffz':
                gffz = np.sum(pc.dot(aa, bb)*np.sqrt(pc.dot2(bb)) / \
                       ((bb[2,:,:,:]) * np.mean(np.mean(np.sqrt(pc.dot2(bb)), axis = 2), axis = 1)))
                out_string += '{0:1.9e}  '.format(np.float64(gffz))
                    
        fd.write((out_string+'\n').encode('utf-8'))
            
    fd.close()
コード例 #5
0
def post_compute(variables = ['b2m'], datadir = 'data'):
    """
    Compute diagnostic variables from the VAR files.

    call signature::
    
      post_compute(variables = ['urms'], datadir = 'data')
    
    Read the VAR files and compute the diagnostic variables.
    Write the result in 'post_evaluation.dat'.
    
    Keyword arguments:
    
      *variables*:
        The diagnostic variables to be computed.
        
      *datadir*:
        Data directory.
        
    """

    # open the destination file for writing
    fd = open(datadir + '/post_evaluation.dat', 'w')
    
    # write the header
    fd.write('#--t-------------'.encode('utf-8'))
    for variable in variables:
        fd.write('--{0:15}'.format((variable+'--------------------')[:15]).encode('utf-8'))
    fd.write('\n')
    
    # read the list of all VAR files
    var_list_file = open('data/proc0/varN.list')
    var_list = var_list_file.readlines()
    var_list_file.close()
    
    if (len(variables[0]) == 1):        
        variables = [variables]
        
    # check if bb needs to be computed
    bb_flag = False
    # array containing the variables which depend on bb
    b_dep = ['ab_int', 'jb_int', 'b1m', 'b2m', 'bm2', 'abm', 'abrms', 'jbm', 'brms', 'gffz']
    if (len(set(variables + b_dep)) < len(variables + b_dep)):
        bb_flag = True
        
    # check if jj needs to be computed
    jj_flag = False
    # array containing the variables which depend on jj
    j_dep = ['jb_int', 'j2m', 'jm2', 'jbm', 'jrms']
    if (len(set(variables + j_dep)) < len(variables + j_dep)):
        jj_flag = True
    
    for var_file in var_list:
        # read the var file
        var = pc.read_var(varfile = var_file[:-1], datadir = datadir, quiet = True)
        # the output string which will be written in the destination file
        out_string = '{0:1.9e}  '.format(np.float64(var.t))
        aa = var.aa[:,var.n1:var.n2,var.m1:var.m2,var.l1:var.l2]
        if bb_flag:
            bb = pc.curl(var.aa, var.dx, var.dy, var.dz)
            bb = bb[:,var.n1:var.n2,var.m1:var.m2,var.l1:var.l2]
        if jj_flag:
            jj = pc.curl2(var.aa, var.dx, var.dy, var.dz)
            jj = jj[:,var.n1:var.n2,var.m1:var.m2,var.l1:var.l2]
            
        for variable in variables:
            if variable == 'b2m':
                b2m = (pc.dot2(bb)).mean()
                out_string += '{0:1.9e}  '.format(np.float64(b2m))
            elif variable == 'j2m':
                j2m = (pc.dot2(jj)).mean()
                out_string += '{0:1.9e}  '.format(np.float64(j2m))
                break
            elif variable == 'abm':
                abm = (pc.dot(var.aa, bb)).mean()
                out_string += '{0:1.9e}  '.format(np.float64(abm))
            # generalized flux function (see Yeates, Hornig 2011)
            elif variable == 'gffz':
                gffz = np.sum(pc.dot(aa, bb)*np.sqrt(pc.dot2(bb)) / \
                       ((bb[2,:,:,:]) * np.mean(np.mean(np.sqrt(pc.dot2(bb)), axis = 2), axis = 1)))
                out_string += '{0:1.9e}  '.format(np.float64(gffz))
                    
        fd.write((out_string+'\n').encode('utf-8'))
            
    fd.close()
コード例 #6
0
ファイル: pc2vtk.py プロジェクト: debanjan0811/pencil-code
def pc2vtk(varfile = 'var.dat', datadir = 'data/', proc = -1,
           variables = ['rho','uu','bb'], magic = [], b_ext = False,
           destination = 'work', quiet = True):
    """
    Convert data from PencilCode format to vtk.

    call signature::
    
      pc2vtk(varfile = 'var.dat', datadir = 'data/', proc = -1,
           variables = ['rho','uu','bb'], magic = [],
           destination = 'work.vtk')
    
    Read *varfile* and convert its content into vtk format. Write the result
    in *destination*.
    
    Keyword arguments:
    
      *varfile*:
        The original varfile.
        
      *datadir*:
        Directory where the data is stored.
       
      *proc*:
        Processor which should be read. Set to -1 for all processors.
      
      *variables* = [ 'rho' , 'lnrho' , 'uu' , 'bb', 'b_mag', 'jj', 'j_mag', 'aa', 'ab', 'TT', 'lnTT', 'cc', 'lncc', 'ss', 'vort' ]
        Variables which should be written.
        
      *magic*: [ 'vort' , 'bb' ]
        Additional variables which should be written.
       
      *b_ext*:
        Add the external magnetic field.
        
      *destination*:
        Destination file.
        
      *quiet*:
        Keep quiet when reading the var files.
    """

    # this should correct for the case the user type only one variable
    if (len(magic) > 0):
        if (len(magic[0]) == 1):
            magic = [magic]

    # make sure magic is set when writing 'vort' or 'bb'
    try:
        index = variables.index('vort')
        magic.append('vort')
    except:
        pass      
    try:
        index = variables.index('bb')
        magic.append('bb')
    except:
        pass
    try:
        index = variables.index('b_mag')
        magic.append('bb')
    except:
        pass
    try:
        index = variables.index('jj')
        magic.append('jj')
    except:
        pass
    try:
        index = variables.index('j_mag')
        magic.append('jj')
    except:
        pass

    # reading pc variables and setting dimensions
    var = pc.read_var(varfile = varfile, datadir = datadir, proc = proc,
                    magic = magic, trimall = True, quiet = quiet)
                    
    grid = pc.read_grid(datadir = datadir, proc = proc, trim = True, quiet = True)
    
    params = pc.read_param(param2 = True, quiet = True)
    B_ext = np.array(params.b_ext)    
    # add external magnetic field
    if (b_ext == True):
        var.bb[0,...] += B_ext[0]
        var.bb[1,...] += B_ext[1]
        var.bb[2,...] += B_ext[2]
       
    dimx = len(grid.x)
    dimy = len(grid.y)
    dimz = len(grid.z)
    dim = dimx * dimy * dimz
    dx = (np.max(grid.x) - np.min(grid.x))/(dimx-1)
    dy = (np.max(grid.y) - np.min(grid.y))/(dimy-1)
    dz = (np.max(grid.z) - np.min(grid.z))/(dimz-1)
    
    fd = open(destination + '.vtk', 'wb')
    fd.write('# vtk DataFile Version 2.0\n'.encode('utf-8'))
    fd.write('VAR files\n'.encode('utf-8'))
    fd.write('BINARY\n'.encode('utf-8'))
    fd.write('DATASET STRUCTURED_POINTS\n'.encode('utf-8'))
    fd.write('DIMENSIONS {0:9} {1:9} {2:9}\n'.format(dimx, dimy, dimz).encode('utf-8'))
    fd.write('ORIGIN {0:8.12} {1:8.12} {2:8.12}\n'.format(grid.x[0], grid.y[0], grid.z[0]).encode('utf-8'))
    fd.write('SPACING {0:8.12} {1:8.12} {2:8.12}\n'.format(dx, dy, dz).encode('utf-8'))
    fd.write('POINT_DATA {0:9}\n'.format(dim).encode('utf-8'))
    
    # this should correct for the case the user type only one variable
    if (len(variables) > 0):
        if (len(variables[0]) == 1):
            variables = [variables]
      
    try:
        index = variables.index('rho')
        print('writing rho')
        fd.write('SCALARS rho float\n'.encode('utf-8'))
        fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", var.rho[k,j,i]))
    except:
        pass
    
    try:
        index = variables.index('lnrho')
        print('writing lnrho')
        fd.write('SCALARS lnrho float\n'.encode('utf-8'))
        fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", var.lnrho[k,j,i]))
    except:
        pass
                
    try:
        index = variables.index('uu')
        print('writing uu')
        fd.write('VECTORS vfield float\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", var.uu[0,k,j,i]))
                    fd.write(struct.pack(">f", var.uu[1,k,j,i]))
                    fd.write(struct.pack(">f", var.uu[2,k,j,i]))
    except:
        pass
    
    try:
        index = variables.index('bb')
        print('writing bb')
        fd.write('VECTORS bfield float\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", var.bb[0,k,j,i]))
                    fd.write(struct.pack(">f", var.bb[1,k,j,i]))
                    fd.write(struct.pack(">f", var.bb[2,k,j,i]))
    except:
        pass
    
    try:
        index = variables.index('b_mag')
        b_mag = np.sqrt(pc.dot2(var.bb))
        print('writing b_mag')
        fd.write('SCALARS b_mag float\n'.encode('utf-8'))
        fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", b_mag[k,j,i]))
    except:
        pass
    
    try:
        index = variables.index('jj')
        print('writing jj')
        fd.write('VECTORS jfield float\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", var.jj[0,k,j,i]))
                    fd.write(struct.pack(">f", var.jj[1,k,j,i]))
                    fd.write(struct.pack(">f", var.jj[2,k,j,i]))
    except:
        pass
        
    try:
        index = variables.index('j_mag')
        j_mag = np.sqrt(pc.dot2(var.jj))
        print('writing j_mag')
        fd.write('SCALARS j_mag float\n'.encode('utf-8'))
        fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", j_mag[k,j,i]))
    except:
        pass
    
    try:
        index = variables.index('aa')
        print('writing aa')
        fd.write('VECTORS afield float\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", var.aa[0,k,j,i]))
                    fd.write(struct.pack(">f", var.aa[1,k,j,i]))
                    fd.write(struct.pack(">f", var.aa[2,k,j,i]))
    except:
        pass
    
    try:
        index = variables.index('ab')
        ab = pc.dot(var.aa, var.bb)
        print('writing ab')
        fd.write('SCALARS ab float\n'.encode('utf-8'))
        fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", ab[k,j,i]))
    except:
        pass
    
    try:
        index = variables.index('TT')
        print('writing TT')
        fd.write('SCALARS TT float\n'.encode('utf-8'))
        fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", var.TT[k,j,i]))
    except:
        pass

    try:
        index = variables.index('lnTT')
        print('writing lnTT')
        fd.write('SCALARS lnTT float\n'.encode('utf-8'))
        fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", var.lnTT[k,j,i]))
    except:
        pass
                    
    try:
        index = variables.index('cc')
        print('writing cc')
        fd.write('SCALARS cc float\n'.encode('utf-8'))
        fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", var.cc[k,j,i]))
    except:
        pass

    try:
        index = variables.index('lncc')
        print('writing lncc')
        fd.write('SCALARS lncc float\n'.encode('utf-8'))
        fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", var.lncc[k,j,i]))                    
    except:
        pass
    
    try:
        index = variables.index('ss')
        print('writing ss')
        fd.write('SCALARS ss float\n'.encode('utf-8'))
        fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", var.ss[k,j,i]))                    
    except:
        pass
      
    try:
        index = variables.index('vort')
        print('writing vort')
        fd.write('VECTORS vorticity float\n'.encode('utf-8'))
        for k in range(dimz):
            for j in range(dimy):
                for i in range(dimx):
                    fd.write(struct.pack(">f", var.vort[0,k,j,i]))
                    fd.write(struct.pack(">f", var.vort[1,k,j,i]))
                    fd.write(struct.pack(">f", var.vort[2,k,j,i]))
    except:
        pass

    del(var)
    
    fd.close()