Exemplo n.º 1
0
def pc_hel_spec(var,magnetic=False):
    """shell averaged helicity power spectrum on pencil code data
    """
    if magnetic:
        # bb is field; aa is vector potential
        bb = (pc.curl(var.f[4:7],var.dx,var.dy,var.dz))[:,3:-3,3:-3,3:-3]
        aa = var.f[4:7,3:-3,3:-3,3:-3]
    else:
        # bb is vorticity; aa is velocity
        aa = var.f[0:3,3:-3,3:-3,3:-3]
        bb = (pc.curl(var.f[0:3],var.dx,var.dy,var.dz))[:,3:-3,3:-3,3:-3]

    helhat = []
    enhat = []
    for i in range(3):
        if hasattr(var,'deltay'):
            aahat = fft_shear(aa[i],var.deltay,var.x[3:-3],var.dx,var.dy,var.dz)/aa.size
            bbhat = fft_shear(bb[i],var.deltay,var.x[3:-3],var.dx,var.dy,var.dz)/bb.size
        else:
            aahat = fpack.fftn(aa[i])/aa[i].size
            bbhat = fpack.fftn(bb[i])/bb[i].size
        if magnetic:
            enhat.append(bbhat)
        else:
            enhat.append(uuhat)
        helhat.append(aahat*bbhat.conj() + aahat.conj()*bbhat)

    hpower = 0.5*(na.array(helhat)).sum(axis=0)
    epower = 0.5*(na.array([na.abs(b)**2 for b in enhat])).sum(axis=0)
    ff = FourierFilter(hpower) #they both have the same shape, so this is fine
    k = ff.get_shell_k(dx=var.dx)    
    hspec = na.array([hpower[ff.get_shell(bin)].sum() for bin in range(ff.nx)])
    espec = na.array([epower[ff.get_shell(bin)].sum() for bin in range(ff.nx)])

    return k, hspec, espec
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
def pc_en_spec(var,magnetic=False):
    """shell averaged energy power spectrum on pencil code data
    """
    if magnetic:
        data = (pc.curl(var.f[4:7],var.dx,var.dy,var.dz))[:,3:-3,3:-3,3:-3]
    else:
        data = var.f[0:3,3:-3,3:-3,3:-3]
    uuhat = []
    for i in range(3):
        if hasattr(var,'deltay'):
            uuhat.append(fft_shear(data[i],var.deltay,var.x[3:-3],var.dx,var.dy,var.dz)/data.size)
        else:
            uuhat.append(fpack.fftn(data[i])/data[i].size)
    power = 0.5*(na.array([na.abs(b)**2 for b in uuhat])).sum(axis=0)
    ff = FourierFilter(power)

    return na.array([power[ff.get_shell(bin)].sum() for bin in range(ff.nx)])
Exemplo n.º 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()
Exemplo n.º 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()