def xder_6th(f,dx,x=[],y=[],z=[],param=[],dim=[]):

    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError

    if not param:
        param=read_param(quiet=True)
    if not dim:
        dim=read_dim()
    if len(x) < 1:
        gd  = read_grid(quiet=True)
        x = gd.x
    dx=N.gradient(x)
    if (dim.nx!=1):
        dx2 = 1./(60.*dx)
    dfdx = N.zeros_like(f)
    l1 = 3
    l2 = f.shape[-1]-3
    if (l2 > l1 and dim.nx!=1):
        for l in range(l1,l2):
            dfdx[...,l] = dx2[l]*( +45.*(f[...,l+1]-f[...,l-1])
                                    -9.*(f[...,l+2]-f[...,l-2])
                                    +   (f[...,l+3]-f[...,l-3]) )
    else:
        dfdx = 0.
    return dfdx
def xder2_6th(f, dx, x=[], y=[], z=[], param=[], dim=[]):

    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError

    if not param:
        param = read_param(quiet=True)
    if not dim:
        dim = read_dim()

    dx = N.gradient(x)
    if (dim.nx != 1):
        dx2 = 1. / (180. * dx**2.)
    dfdx = N.zeros_like(f)
    l1 = 3
    l2 = f.shape[-1] - 3

    if (l2 > l1 and dim.nx != 1):
        for l in range(l1, l2):
            dfdx[..., l] = dx2[l] * (-490. * f[..., l] + 270. *
                                     (f[..., l - 1] + f[..., l + 1]) - 27. *
                                     (f[..., l - 2] + f[..., l + 2]) + 2. *
                                     (f[..., l - 3] + f[..., l + 3]))
    else:
        dfdx = 0.

    return dfdx
Beispiel #3
0
def yder_6th(f, dy, x=[], y=[], z=[], param=[], dim=[]):

    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError

    if not param:
        param = read_param(quiet=True)
    if not dim:
        dim = read_dim()

    dy = N.gradient(y)
    if (dim.ny != 1):
        dy2 = 1. / (60. * dy)
    dfdy = N.zeros_like(f)
    m1 = 3
    m2 = f.shape[-2] - 3

    if (m2 > m1 and dim.ny != 1):
        for m in range(m1, m2):
            dfdy[...,
                 m, :] = dy2[m] * (+45. *
                                   (f[..., m + 1, :] - f[..., m - 1, :]) - 9. *
                                   (f[..., m + 2, :] - f[..., m - 2, :]) +
                                   (f[..., m + 3, :] - f[..., m - 3, :]))
    else:
        dfdy = 0.
    if param.coord_system == ('cylindric' or 'spherical'):
        if len(x) < 1:
            gd = read_grid(quiet=True)
            x = gd.x
        dfdy /= x

    return dfdy
Beispiel #4
0
def yder2_6th(f,dy,x=[],y=[],z=[]):

    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError
    
    param=read_param(quiet=True)

    dy = N.gradient(y)
    dy2 = 1./(180.*dy**2.)
    dfdy = N.zeros_like(f)
    m1 = 3
    m2 = f.shape[-2]-3

    if (m2 > m1):
        for m in range(m1,m2+1):
            dfdy[...,m,:] = dy2[m]*(-490.* f[...,m,:]
                                    +270.*(f[...,m-1,:]+f[...,m+1,:])
                                    - 27.*(f[...,m-2,:]+f[...,m+2,:])
                                    +  2.*(f[...,m-3,:]+f[...,m+3,:]) )
    else:
        dfdy = 0.
    if param.coord_system == ('cylindric' or 'spherical'):
        if (len(x) or len(y)) < 1:
            gd=read_grid(quiet=True)
            x=gd.x; y=gd.y
        dfdy /= x**2

    return dfdy
Beispiel #5
0
def del2(f,dx,dy,dz,x=[],y=[],z=[]):
    """taken from pencil code's sub.f90 
    !  calculate del6 (defined here as d^6/dx^6 + d^6/dy^6 + d^6/dz^6, rather
    !  than del2^3) of a scalar for hyperdiffusion
    Duplcation of laplacian why? Fred - added curvelinear
    """
    param = read_param(quiet=True)
    gd  = read_grid(quiet=True)
    if len(x) < 1:
        x = gd.x
    if len(y) < 1:
        y = gd.y
    if len(z) < 1:
        z = gd.z

    del2 =        xder2(f,dx,x=x,y=y,z=z)
    del2 = del2 + yder2(f,dy,x=x,y=y,z=z)
    del2 = del2 + zder2(f,dz,x=x,y=y,z=z)

    if param.coord_system == 'cylindric':
        del2 += xder(f,dx,x=x,y=y,z=z)/x
    if param.coord_system == 'spherical':
        sin_y = N.sin(y)
        cos_y = N.cos(y)
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            cos_y[i_sin] = 0.; sin_y[i_sin] = 1
        x_2, cotth = N.meshgrid(1./x**2, cos_y/sin_y)
        del2 += 2*xder(f,dx,x=x,y=y,z=z)/x +\
                  yder(f,dy,x=x,y=y,z=z)*x_2*cotth

    return del2
def zder2_6th(f,dz,x=[],y=[],z=[]):

    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError

    param=read_param(quiet=True)

    dz2 = 1./(180.*dz**2.)
    dfdz = N.zeros_like(f)
    n1 = 3
    n2 = f.shape[-3]-3
    if (n2 > n1):
        dfdz[...,n1:n2,:,:] = dz2*(-490.*f[...,n1:n2,:,:]
                              +270.*(f[...,n1-1:n2-1,:,:]+f[...,n1+1:n2+1,:,:])
                              - 27.*(f[...,n1-2:n2-2,:,:]+f[...,n1+2:n2+2,:,:])
                              +  2.*(f[...,n1-3:n2-3,:,:]+f[...,n1+3:n2+3,:,:]) )
    else:
        dfdz = 0.
    if param.coord_system == 'spherical':
        if (len(x) or len(y)) < 1:
            gd=read_grid(quiet=True)
            x=gd.x; y=gd.y
        sin_y = N.sin(y)
        siny1 = 1./sin_y
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            siny1[i_sin] = 0.
        x_2, sin2th = N.meshgrid(1./x**2, siny1**2)
        dfdz *= x_2*sin2th

    return dfdz
def yder_6th(f,dy,x=[],y=[],z=[]):

    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError

    param=read_param(quiet=True)

    dy2 = 1./(60.*dy)
    dfdy = N.zeros_like(f)
    m1 = 3
    m2 = f.shape[-2]-3

    if (m2 > m1):
        dfdy[...,m1:m2,:] = dy2*( +45.*(f[...,m1+1:m2+1,:]-f[...,m1-1:m2-1,:]) 
                                  -9.*(f[...,m1+2:m2+2,:]-f[...,m1-2:m2-2,:]) 
                                  +(f[...,m1+3:m2+3,:]-f[...,m1-3:m2-3,:]) )
    else:
        dfdy = 0.
    if param.coord_system == ('cylindric' or 'spherical'):
        if len(x) < 1:
            gd=read_grid(quiet=True)
            x=gd.x
        dfdy /= x
        
    return dfdy
def yder_6th(f,dy,x=[],y=[],z=[],param=[],dim=[]):

    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError

    if not param:
        param=read_param(quiet=True)
    if not dim:
        dim=read_dim()

    if len(y) < 1:
        gd  = read_grid(quiet=True)
        y = gd.y
    dy=N.gradient(y)
    if (dim.ny!=1):
        dy2 = 1./(60.*dy)
    dfdy = N.zeros_like(f)
    m1 = 3
    m2 = f.shape[-2]-3

    if (m2 > m1 and dim.ny != 1):
        for m in range(m1,m2):
            dfdy[...,m,:] = dy2[m]*( +45.*(f[...,m+1,:]-f[...,m-1,:]) 
                                      -9.*(f[...,m+2,:]-f[...,m-2,:]) 
                                      +   (f[...,m+3,:]-f[...,m-3,:]) )
    else:
        dfdy = 0.
    if param.coord_system == ('cylindric' or 'spherical'):
        if len(x) < 1:
            gd=read_grid(quiet=True)
            x=gd.x
        dfdy /= x
        
    return dfdy
def div(f,dx,dy,dz,x=[],y=[],z=[],param=[],dim=[]):
    """
    take divergence of pencil code vector array
    """
    if (f.ndim != 4):
        print("div: must have vector 4-D array f[mvar,mz,my,mx] for divergence")
        raise ValueError
    if not param:
        param = read_param(quiet=True)
    if not dim:
        dim = read_dim()
    gd  = read_grid(quiet=True, param=param)
    if len(x) < 1:
        x = gd.x
        y = gd.y
        z = gd.z

    div = xder(f[0,...],dx,x=x,y=y,z=z,param=param,dim=dim) +\
          yder(f[1,...],dy,x=x,y=y,z=z,param=param,dim=dim) +\
          zder(f[2,...],dz,x=x,y=y,z=z,param=param,dim=dim)

    if param.coord_system == 'cylindric':
        div += f[0,...]/x
    if param.coord_system == 'spherical':
        sin_y = N.sin(y)
        cos_y = N.cos(y)
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            cos_y[i_sin] = 0.; sin_y[i_sin] = 1
        x_1, cotth = N.meshgrid(1./gd.x, cos_y/sin_y)
        div += 2*f[0,...]*x_1 + f[1,...]*x_1*cotth
   
    return div
def xder_6th(f, dx, x=[], y=[], z=[], param=[], dim=[]):

    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError

    if not param:
        param = read_param(quiet=True)
    if not dim:
        dim = read_dim()
    if len(x) < 1:
        gd = read_grid(quiet=True)
        x = gd.x
    dx = N.gradient(x)
    if (dim.nx != 1):
        dx2 = 1. / (60. * dx)
    dfdx = N.zeros_like(f)
    l1 = 3
    l2 = f.shape[-1] - 3
    if (l2 > l1 and dim.nx != 1):
        for l in range(l1, l2):
            dfdx[...,
                 l] = dx2[l] * (+45. * (f[..., l + 1] - f[..., l - 1]) - 9. *
                                (f[..., l + 2] - f[..., l - 2]) +
                                (f[..., l + 3] - f[..., l - 3]))
    else:
        dfdx = 0.
    return dfdx
def laplacian(f,dx,dy,dz,x=[],y=[],z=[],param=[],dim=[]):
    """
    take the laplacian of a pencil code scalar array
    """
    if not param:
        param = read_param(quiet=True)
    if not dim:
        dim = read_dim()
    if len(x) < 1:
        gd  = read_grid(quiet=True)
        x = gd.x
        y = gd.y
        z = gd.z

    laplacian = N.empty(f.shape)
    laplacian = xder2(f,dx,x=x,y=y,z=z,param=param,dim=dim) +\
                yder2(f,dy,x=x,y=y,z=z,param=param,dim=dim) +\
                zder2(f,dz,x=x,y=y,z=z,param=param,dim=dim)

    if param.coord_system == 'cylindric':
        laplacian += xder(f,dx,x=x,y=y,z=z,param=param,dim=dim)/x
    if param.coord_system == 'spherical':
        sin_y = N.sin(y)
        cos_y = N.cos(y)
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            cos_y[i_sin] = 0.; sin_y[i_sin] = 1
        x_2, cotth = N.meshgrid(1./x**2, cos_y/sin_y)
        laplacian += 2*xder(f,dx,x=x,y=y,z=z,param=param,dim=dim)/x +\
                       yder(f,dy,x=x,y=y,z=z,param=param,dim=dim)*x_2*cotth

    return laplacian
def del2(f,dx,dy,dz,x=[],y=[],z=[]):
    """taken from pencil code's sub.f90 
    !  calculate del6 (defined here as d^6/dx^6 + d^6/dy^6 + d^6/dz^6, rather
    !  than del2^3) of a scalar for hyperdiffusion
    Duplcation of laplacian why? Fred - added curvelinear
    """
    param = read_param(quiet=True)
    gd  = read_grid(quiet=True)
    if len(x) < 1:
        x = gd.x
    if len(y) < 1:
        y = gd.y
    if len(z) < 1:
        z = gd.z

    del2 =        xder2(f,dx,x=x,y=y,z=z)
    del2 = del2 + yder2(f,dy,x=x,y=y,z=z)
    del2 = del2 + zder2(f,dz,x=x,y=y,z=z)

    if param.coord_system == 'cylindric':
        del2 += xder(f,dx,x=x,y=y,z=z)/x
    if param.coord_system == 'spherical':
        sin_y = N.sin(y)
        cos_y = N.cos(y)
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            cos_y[i_sin] = 0.; sin_y[i_sin] = 1
        x_2, cotth = N.meshgrid(1./x**2, cos_y/sin_y)
        del2 += 2*xder(f,dx,x=x,y=y,z=z)/x +\
                  yder(f,dy,x=x,y=y,z=z)*x_2*cotth

    return del2
Beispiel #13
0
def laplacian(f,dx,dy,dz,x=[],y=[],z=[]):
    """
    take the laplacian of a pencil code scalar array
    """
    param = read_param(quiet=True)
    gd  = read_grid(quiet=True)
    if len(x) < 1:
        x = gd.x
    if len(y) < 1:
        y = gd.y
    if len(z) < 1:
        z = gd.z

    laplacian = N.empty(f.shape)
    laplacian = xder2(f,dx,x=x,y=y,z=z)+\
                yder2(f,dy,x=x,y=y,z=z)+\
                zder2(f,dz,x=x,y=y,z=z)

    if param.coord_system == 'cylindric':
        laplacian += xder(f,dx,x=x,y=y,z=z)/x
    if param.coord_system == 'spherical':
        sin_y = N.sin(y)
        cos_y = N.cos(y)
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            cos_y[i_sin] = 0.; sin_y[i_sin] = 1
        x_2, cotth = N.meshgrid(1./x**2, cos_y/sin_y)
        laplacian += 2*xder(f,dx,x=x,y=y,z=z)/x +\
                       yder(f,dy,x=x,y=y,z=z)*x_2*cotth

    return laplacian
Beispiel #14
0
def div(f,dx,dy,dz,x=[],y=[],z=[]):
    """
    take divergence of pencil code vector array
    """
    if (f.ndim != 4):
        print("div: must have vector 4-D array f[mvar,mz,my,mx] for divergence")
        raise ValueError
    param = read_param(quiet=True)
    gd  = read_grid(quiet=True)
    if len(x) < 1:
        x = gd.x
    if len(y) < 1:
        y = gd.y
    if len(z) < 1:
        z = gd.z

    div = xder(f[0,...],dx,x=x,y=y,z=z) +\
          yder(f[1,...],dy,x=x,y=y,z=z) +\
          zder(f[2,...],dz,x=x,y=y,z=z)

    if param.coord_system == 'cylindric':
        div += f[0,...]/x
    if param.coord_system == 'spherical':
        sin_y = N.sin(y)
        cos_y = N.cos(y)
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            cos_y[i_sin] = 0.; sin_y[i_sin] = 1
        x_1, cotth = N.meshgrid(1./gd.x, cos_y/sin_y)
        div += 2*f[0,...]*x_1 + f[1,...]*x_1*cotth
   
    return div
def xder2_6th(f,dx,x=[],y=[],z=[],param=[],dim=[]):

    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError

    if not param:
        param=read_param(quiet=True)
    if not dim: 
        dim=read_dim()

    dx = N.gradient(x)
    if (dim.nx!=1):
        dx2 = 1./(180.*dx**2.)
    dfdx = N.zeros_like(f)
    l1 = 3
    l2 = f.shape[-1]-3

    if (l2 > l1 and dim.nx!=1):
        for l in range(l1,l2): 
            dfdx[...,l] = dx2[l]*(-490.* f[...,l]
                                  +270.*(f[...,l-1]+f[...,l+1])
                                  - 27.*(f[...,l-2]+f[...,l+2])
                                  +  2.*(f[...,l-3]+f[...,l+3]) )
    else:
        dfdx = 0.

    return dfdx
def zder_6th(f, dz, x=[], y=[], z=[], run2D=False, param=[], dim=[]):

    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError

    if not param:
        param = read_param(quiet=True)
    if not dim:
        dim = read_dim()

    if len(z) < 1:
        gd = read_grid(quiet=True)
        z = gd.z
    dz = N.gradient(z)
    if (dim.nz != 1):
        dz2 = 1. / (60. * dz)
    dfdz = N.zeros_like(f)

    n1 = 3
    if run2D:
        n2 = f.shape[1] - 3
    else:
        n2 = f.shape[-3] - 3

    if (n2 > n1 and dim.nz != 1):
        if (run2D):
            # f[...,z,x] or f[...,z,y]
            for n in range(n1, n2):
                dfdz[...,
                     n, :] = dz2[n] * (+45. *
                                       (f[..., n + 1, :] - f[..., n - 1, :]) -
                                       9. *
                                       (f[..., n + 2, :] - f[..., n - 2, :]) +
                                       (f[..., n + 3, :] - f[..., n - 3, :]))

        else:
            # f[...,z,y,x]
            for n in range(n1, n2):
                dfdz[..., n, :, :] = dz2[n] * (
                    +45. * (f[..., n + 1, :, :] - f[..., n - 1, :, :]) - 9. *
                    (f[..., n + 2, :, :] - f[..., n - 2, :, :]) +
                    (f[..., n + 3, :, :] - f[..., n - 3, :, :]))
    else:
        dfdz = 0
    if param.coord_system == 'spherical':
        if (len(x) or len(y)) < 1:
            gd = read_grid(quiet=True)
            x = gd.x
            y = gd.y
        sin_y = N.sin(y)
        siny1 = 1. / sin_y
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            siny1[i_sin] = 0.
        x_1, sin1th = N.meshgrid(1. / x, siny1)
        dfdz *= x_1 * sin1th

    return dfdz
def curl(f,dx,dy,dz,x=[],y=[],z=[],run2D=False,param=[],dim=[]):
    """
    take the curl of a pencil code vector array.
    23-fev-2009/dintrans+morin: introduced the run2D parameter to deal
    with pure 2-D snapshots (solved the (x,z)-plane pb)
    """
    if (f.shape[0] != 3):
        print("curl: must have vector 4-D array f[3,mz,my,mx] for curl")
        raise ValueError

    if not param:
        param = read_param(quiet=True)
    if not dim:
        dim = read_dim()
    if len(x) < 1:
        gd = read_grid(quiet=True, param=param)
        x = gd.x
        y = gd.y
        z = gd.z

    curl = N.empty_like(f)
    if (not(run2D)):
    # 3-D case
        curl[0,...] = yder(f[2,...],dy,x=x,y=y,z=z,param=param,dim=dim) -\
                      zder(f[1,...],dz,x=x,y=y,z=z,param=param,dim=dim)
        curl[1,...] = zder(f[0,...],dz,x=x,y=y,z=z,param=param,dim=dim) -\
                      xder(f[2,...],dx,x=x,y=y,z=z,param=param,dim=dim)
        curl[2,...] = xder(f[1,...],dx,x=x,y=y,z=z,param=param,dim=dim) -\
                      yder(f[0,...],dy,x=x,y=y,z=z,param=param,dim=dim)
    elif (dim.ny == 1):
    # 2-D case in the (x,z)-plane
    # f[...,nz,1,nx] if run2D=False or f[...,nz,nx] if run2D=True
        curl[0,...] = zder(f,dz,x=x,y=y,z=z,run2D=run2D,param=param, \
        dim=dim)[0,...] - xder(f,dx,x=x,y=y,z=z,param=param,dim=dim)[2,...]
    elif (dim.nz ==1):
    # 2-D case in the (x,y)-plane
    # f[...,1,ny,nx] if run2D=False or f[...,ny,nx] if run2D=True
        curl[0,...] = xder(f,dx,x=x,y=y,z=z,param=param,dim=dim)[1,...] -\
                      yder(f,dy,x=x,y=y,z=z,param=param,dim=dim)[0,...]

    if param.coord_system == 'cylindric':
    # 2-D case in the (r,theta)-plane
        if run2D:
            curl[0,...] += f[1,...]/x
        else:
    # 3-D case
            curl[2,...] += f[1,...]/x
    if param.coord_system == 'spherical':
        sin_y = N.sin(y)
        cos_y = N.cos(y)
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            cos_y[i_sin] = 0.; sin_y[i_sin] = 1
        x_1, cotth = N.meshgrid(1./x, cos_y/sin_y)
        curl[0,...] += f[2,...]*x_1*cotth
        curl[1,...] -= f[2,...]/x
        curl[2,...] += f[1,...]/x

    return curl
Beispiel #18
0
def curl(f,dx,dy,dz,x=[],y=[],z=[],run2D=False,param=[]):
    """
    take the curl of a pencil code vector array.
    23-fev-2009/dintrans+morin: introduced the run2D parameter to deal
    with pure 2-D snapshots (solved the (x,z)-plane pb)
    """
    if (f.shape[0] != 3):
        print("curl: must have vector 4-D array f[3,mz,my,mx] for curl")
        raise ValueError

    if not param:
        param = read_param(quiet=True)
    if len(x) < 1:
        gd = read_grid(quiet=True)
        x = gd.x
        y = gd.y
        z = gd.z

    curl = N.empty_like(f)
    if (dy != 0. and dz != 0.):
    # 3-D case
        curl[0,...] = yder(f[2,...],dy,x=x,y=y,z=z) -\
                      zder(f[1,...],dz,x=x,y=y,z=z)
        curl[1,...] = zder(f[0,...],dz,x=x,y=y,z=z) -\
                      xder(f[2,...],dx,x=x,y=y,z=z)
        curl[2,...] = xder(f[1,...],dx,x=x,y=y,z=z) -\
                      yder(f[0,...],dy,x=x,y=y,z=z)
    elif (dy == 0.):
    # 2-D case in the (x,z)-plane
    # f[...,nz,1,nx] if run2D=False or f[...,nz,nx] if run2D=True
        curl[0,...] = zder(f,dz,x=x,y=y,z=z,run2D=run2D)[0,...] -\
                      xder(f,dx,x=x,y=y,z=z)[2,...]
    else:
    # 2-D case in the (x,y)-plane
    # f[...,1,ny,nx] if run2D=False or f[...,ny,nx] if run2D=True
        curl[0,...] = xder(f,dx,x=x,y=y,z=z)[1,...] -\
                      yder(f,dy,x=x,y=y,z=z)[0,...]

    if param.coord_system == 'cylindric':
    # 2-D case in the (r,theta)-plane
        if run2D:
            curl[0,...] += f[1,...]/x
        else:
    # 3-D case
            curl[2,...] += f[1,...]/x
    if param.coord_system == 'spherical':
        sin_y = N.sin(y)
        cos_y = N.cos(y)
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            cos_y[i_sin] = 0.; sin_y[i_sin] = 1
        x_1, cotth = N.meshgrid(1./x, cos_y/sin_y)
        curl[0,...] += f[2,...]*x_1*cotth
        curl[1,...] -= f[2,...]/x
        curl[2,...] += f[1,...]/x

    return curl
Beispiel #19
0
def curl2(f, dx, dy, dz, x=[], y=[], z=[]):
    """
    take the double curl of a pencil code vector array.
    """
    if (f.ndim != 4 or f.shape[0] != 3):
        print("curl2: must have vector 4-D array f[3,mz,my,mx] for curl2")
        raise ValueError
    param = read_param(quiet=True)
    gd = read_grid(quiet=True)
    if len(x) < 1:
        x = gd.x
    if len(y) < 1:
        y = gd.y
    if len(z) < 1:
        z = gd.z

    curl2 = N.empty(f.shape)
    curl2[0,...] = xder(yder(f[1,...],dy,x=x,y=y,z=z) +
                        zder(f[2,...],dz,x=x,y=y,z=z),dx,x=x,y=y,z=z) -\
                   yder2(f[0,...],dy,x=x,y=y,z=z) -\
                   zder2(f[0,...],dz,x=x,y=y,z=z)
    curl2[1,...] = yder(xder(f[0,...],dx,x=x,y=y,z=z) +
                        zder(f[2,...],dz,x=x,y=y,z=z),dy,x=x,y=y,z=z) -\
                   xder2(f[1,...],dx,x=x,y=y,z=z) -\
                   zder2(f[1,...],dz,x=x,y=y,z=z)
    curl2[2,...] = zder(xder(f[0,...],dx,x=x,y=y,z=z) +
                        yder(f[1,...],dy,x=x,y=y,z=z),dz,x=x,y=y,z=z) -\
                   xder2(f[2,...],dx,x=x,y=y,z=z) -\
                   yder2(f[2,...],dy,x=x,y=y,z=z)

    if param.coord_system == 'cylindric':
        curl2[0, ...] += yder(f[1, ...], dy, x=x, y=y, z=z) / x**2
        curl2[1, ...] += f[1, ...] / gd.x**2 - xder(
            f[1, ...], dx, x=x, y=y, z=z) / x
        curl2[2, ...] += (zder(f[0, ...], dz, x=x, y=y, z=z) -
                          xder(f[2, ...], dx, x=x, y=y, z=z)) / x
    if param.coord_system == 'spherical':
        sin_y = N.sin(y)
        cos_y = N.cos(y)
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            cos_y[i_sin] = 0.
            sin_y[i_sin] = 1
        x_1, cotth = N.meshgrid(1. / x, cos_y / sin_y)
        sin2th, x_2 = N.meshgrid(1. / x**2, 1 / sin_y**2)
        curl2[0,...] += (yder(f[1,...],dy,x=x,y=y,z=z) +
                         zder(f[2,...],dz,x=x,y=y,z=z))/x +\
              x_1*cotth*(xder(f[1,...],dx,x=x,y=y,z=z) -
                         yder(f[0,...],dy,x=x,y=y,z=z) + f[1,...]/x )
        curl2[1,...] +=  zder(f[2,...],dz,x=x,y=y,z=z)*x_1*cotth -\
                       2*xder(f[1,...],dx,x=x,y=y,z=z)/x
        curl2[2,...] += x_2*sin2th*f[2,...] - \
                       2*xder(f[2,...],dx,x=x,y=y,z=z)/x - (
                         yder(f[2,...],dy,x=x,y=y,z=z) +
                         zder(f[1,...],dz,x=x,y=y,z=z))*x_1*cotth

    return curl2
def curl2(f,dx,dy,dz,x=[],y=[],z=[]):
    """
    take the double curl of a pencil code vector array.
    """
    if (f.ndim != 4 or f.shape[0] != 3):
        print("curl2: must have vector 4-D array f[3,mz,my,mx] for curl2")
        raise ValueError
    param = read_param(quiet=True)
    gd  = read_grid(quiet=True)
    if len(x) < 1:
        x = gd.x
    if len(y) < 1:
        y = gd.y
    if len(z) < 1:
        z = gd.z

    curl2 = N.empty(f.shape)
    curl2[0,...] = xder(yder(f[1,...],dy,x=x,y=y,z=z) +
                        zder(f[2,...],dz,x=x,y=y,z=z),dx,x=x,y=y,z=z) -\
                   yder2(f[0,...],dy,x=x,y=y,z=z) -\
                   zder2(f[0,...],dz,x=x,y=y,z=z)
    curl2[1,...] = yder(xder(f[0,...],dx,x=x,y=y,z=z) +
                        zder(f[2,...],dz,x=x,y=y,z=z),dy,x=x,y=y,z=z) -\
                   xder2(f[1,...],dx,x=x,y=y,z=z) -\
                   zder2(f[1,...],dz,x=x,y=y,z=z)
    curl2[2,...] = zder(xder(f[0,...],dx,x=x,y=y,z=z) +
                        yder(f[1,...],dy,x=x,y=y,z=z),dz,x=x,y=y,z=z) -\
                   xder2(f[2,...],dx,x=x,y=y,z=z) -\
                   yder2(f[2,...],dy,x=x,y=y,z=z)

    if param.coord_system == 'cylindric':
        curl2[0,...] +=                    yder(f[1,...],dy,x=x,y=y,z=z)/x**2
        curl2[1,...] += f[1,...]/gd.x**2 - xder(f[1,...],dx,x=x,y=y,z=z)/x
        curl2[2,...] +=                   (zder(f[0,...],dz,x=x,y=y,z=z) -
                                           xder(f[2,...],dx,x=x,y=y,z=z))/x
    if param.coord_system == 'spherical':
        sin_y = N.sin(y)
        cos_y = N.cos(y)
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            cos_y[i_sin] = 0.; sin_y[i_sin] = 1
        x_1 ,cotth  = N.meshgrid(   1./x, cos_y/sin_y)
        sin2th, x_2 = N.meshgrid(1./x**2, 1/sin_y**2 )
        curl2[0,...] += (yder(f[1,...],dy,x=x,y=y,z=z) +
                         zder(f[2,...],dz,x=x,y=y,z=z))/x +\
              x_1*cotth*(xder(f[1,...],dx,x=x,y=y,z=z) -
                         yder(f[0,...],dy,x=x,y=y,z=z) + f[1,...]/x )
        curl2[1,...] +=  zder(f[2,...],dz,x=x,y=y,z=z)*x_1*cotth -\
                       2*xder(f[1,...],dx,x=x,y=y,z=z)/x
        curl2[2,...] += x_2*sin2th*f[2,...] - \
                       2*xder(f[2,...],dx,x=x,y=y,z=z)/x - (
                         yder(f[2,...],dy,x=x,y=y,z=z) +
                         zder(f[1,...],dz,x=x,y=y,z=z))*x_1*cotth

    return curl2
def zder_6th(f,dz,x=[],y=[],z=[],run2D=False,param=[],dim=[]):
    
    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError

    if not param:
        param=read_param(quiet=True)
    if not dim:
        dim=read_dim()

    if len(z) < 1:
        gd  = read_grid(quiet=True)
        z = gd.z
    dz=N.gradient(z)
    if (dim.nz!=1):
        dz2 = 1./(60.*dz)
    dfdz = N.zeros_like(f)

    n1 = 3
    if run2D:
        n2 = f.shape[1]-3
    else:
        n2 = f.shape[-3]-3

    if (n2 > n1 and dim.nz!=1):
        if (run2D):
            # f[...,z,x] or f[...,z,y]
            for n in range(n1,n2):
                dfdz[...,n,:] = dz2[n]*(+45.*(f[...,n+1,:]-f[...,n-1,:])
                                         -9.*(f[...,n+2,:]-f[...,n-2,:])
                                            +(f[...,n+3,:]-f[...,n-3,:]) )

        else:
            # f[...,z,y,x]
            for n in range(n1,n2):
                dfdz[...,n,:,:] = dz2[n]*(+45.*(f[...,n+1,:,:]-f[...,n-1,:,:])
                                           -9.*(f[...,n+2,:,:]-f[...,n-2,:,:])
                                              +(f[...,n+3,:,:]-f[...,n-3,:,:]) )
    else:
        dfdz=0
    if param.coord_system == 'spherical':
        if (len(x) or len(y)) < 1:
            gd=read_grid(quiet=True)
            x=gd.x; y=gd.y
        sin_y = N.sin(y)
        siny1 = 1./sin_y
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            siny1[i_sin] = 0.
        x_1, sin1th = N.meshgrid(1./x, siny1)
        dfdz *= x_1*sin1th

    return dfdz
def zder_6th(f,dz,x=[],y=[],z=[],run2D=False):
    
    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError

    param=read_param(quiet=True)

    dz2 = 1./(60.*dz)
    dfdz = N.zeros_like(f)
    n1 = 3
    if run2D:
        n2 = f.shape[1]-3
    else:
        n2 = f.shape[-3]-3

    if (n2 > n1):
       if (run2D):
          # f[...,z,x] or f[...,z,y]
          dfdz[...,n1:n2,:] = dz2*(+45.*(f[...,n1+1:n2+1,:]
                              -f[...,n1-1:n2-1,:])
                              -9.*(f[...,n1+2:n2+2,:]
                              -f[...,n1-2:n2-2,:]) 
                              +(f[...,n1+3:n2+3,:]-f[...,n1-3:n2-3,:]) )

       else:
          # f[...,z,y,x]
          dfdz[...,n1:n2,:,:] = dz2*(+45.*(f[...,n1+1:n2+1,:,:]
                                -f[...,n1-1:n2-1,:,:])
                                -9.*(f[...,n1+2:n2+2,:,:]
                                -f[...,n1-2:n2-2,:,:]) 
                                +(f[...,n1+3:n2+3,:,:]-f[...,n1-3:n2-3,:,:]) )
    else:
        dfdz=0
    if param.coord_system == 'spherical':
        if (len(x) or len(y)) < 1:
            gd=read_grid(quiet=True)
            x=gd.x; y=gd.y
        sin_y = N.sin(y)
        siny1 = 1./sin_y
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            siny1[i_sin] = 0.
        x_1, sin1th = N.meshgrid(1./x, siny1)
        dfdz *= x_1*sin1th

    return dfdz
def zder2_6th(f, dz, x=[], y=[], z=[], param=[], dim=[]):

    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError

    if (len(z) < 1):
        gd = read_grid(quiet=True)
        z = gd.z
    if not param:
        param = read_param(quiet=True)
    if not dim:
        dim = read_dim()

    dz = N.gradient(z)
    if (dim.nz != 1):
        dz2 = 1. / (180. * dz**2.)
    dfdz = N.zeros_like(f)
    n1 = 3
    n2 = f.shape[-3] - 3

    if (n2 > n1 and dim.nz != 1):
        for n in range(n1, n2):
            dfdz[..., n, :, :] = dz2[n] * (
                -490. * f[..., n, :, :] + 270. *
                (f[..., n - 1, :, :] + f[..., n + 1, :, :]) - 27. *
                (f[..., n - 2, :, :] + f[..., n + 2, :, :]) + 2. *
                (f[..., n - 3, :, :] + f[..., n + 3, :, :]))
    else:
        dfdz = 0.
    if param.coord_system == 'spherical':
        if (len(x) or len(y)) < 1:
            gd = read_grid(quiet=True)
            x = gd.x
            y = gd.y
        sin_y = N.sin(y)
        siny1 = 1. / sin_y
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            siny1[i_sin] = 0.
        x_2, sin2th = N.meshgrid(1. / x**2, siny1**2)
        dfdz *= x_2 * sin2th

    return dfdz
def zder2_6th(f,dz,x=[],y=[],z=[],param=[],dim=[]):

    if (f.ndim != 3 and f.ndim != 4):
        print("%s dimension arrays not handled." % (str(f.ndim)))
        raise ValueError

    if (len(z) < 1):
        gd=read_grid(quiet=True)
        z=gd.z
    if not param:
        param=read_param(quiet=True)
    if not dim:
        dim=read_dim()

    dz = N.gradient(z)
    if (dim.nz!=1):
        dz2 = 1./(180.*dz**2.)
    dfdz = N.zeros_like(f)
    n1 = 3
    n2 = f.shape[-3]-3
    
    if (n2 > n1 and dim.nz!=1):
        for n in range(n1,n2): 
            dfdz[...,n,:,:] = dz2[n]*(-490.* f[...,n,:,:]
                                      +270.*(f[...,n-1,:,:]+f[...,n+1,:,:])
                                      - 27.*(f[...,n-2,:,:]+f[...,n+2,:,:])
                                      +  2.*(f[...,n-3,:,:]+f[...,n+3,:,:]) )
    else:
        dfdz = 0.
    if param.coord_system == 'spherical':
        if (len(x) or len(y)) < 1:
            gd=read_grid(quiet=True)
            x=gd.x; y=gd.y
        sin_y = N.sin(y)
        siny1 = 1./sin_y
        i_sin = N.where(N.abs(sin_y) < 1e-5)[0]
        if i_sin.size > 0:
            siny1[i_sin] = 0.
        x_2, sin2th = N.meshgrid(1./x**2, siny1**2)
        dfdz *= x_2*sin2th

    return dfdz
def grad(f,dx,dy,dz,x=[],y=[],z=[],param=[],dim=[]):
    """
    take the gradient of a pencil code scalar array.
    """
    if (f.ndim != 3):
        print("grad: must have scalar 3-D array f[mz,my,mx] for gradient")
        raise ValueError

    if not param:
        param=read_param(quiet=True)
    if not dim:
        dim=read_dim()
    if len(x) < 1:
        gd = read_grid(quiet=True)
        x = gd.x
        y = gd.y
        z = gd.z

    grad = N.empty((3,)+f.shape)
    grad[0,...] = xder(f,dx,x=x,y=y,z=z,param=param,dim=dim)
    grad[1,...] = yder(f,dy,x=x,y=y,z=z,param=param,dim=dim)
    grad[2,...] = zder(f,dz,x=x,y=y,z=z,param=param,dim=dim)

    return grad
Beispiel #26
0
    def __init__(self,
                 varfile='',
                 datadir='data/',
                 proc=-1,
                 ivar=-1,
                 quiet=False,
                 trimall=False,
                 format='native',
                 param=None,
                 dim=None,
                 index=None,
                 run2D=False,
                 magic=None,
                 setup=None):
        """
        Description:
        -----------
        Read VAR files from pencil code. if proc < 0, then load all data
        and assemble. otherwise, load VAR file from specified processor.

        format -- one of (['native', 'n'], ['ieee-le', 'l'],
        ['ieee-be', 'B']) for byte-ordering

        Params:
        ------
            varfile=''
            datadir='data/'
            proc=-1
            ivar=-1
            quiet=False
            trimall=False
            format='native'
            param=None
            dim=None
            index=None
            run2D=False

        Example of usage
        ------
        ff=pc.read_var(trimall=True,ivar=100,magic=['tt','vort'])

        """
        if (setup is not None):
            datadir = os.path.expanduser(setup.datadir)
            dim = setup.dim
            param = setup.param
            index = setup.index
            run2D = setup.run2D
        else:
            datadir = os.path.expanduser(datadir)
            if dim is None:
                ldownsampled = 'VARd' in varfile
                dim = read_dim(datadir, proc, down=ldownsampled)
            if param is None:
                param = read_param(datadir=datadir, quiet=quiet)
            if index is None:
                index = read_index(datadir=datadir, down=ldownsampled)

        if dim.precision == 'D':
            precision = 'd'
        else:
            precision = 'f'

        if param.lwrite_aux:
            totalvars = dim.mvar + dim.maux
        else:
            totalvars = dim.mvar
        if 'VARd' in varfile:
            if param.mvar_down > 0:
                totalvars = param.mvar_down

        # Read index.pro to get positions and "names"
        # of variables in f(mx,my,mz,nvar).
        # Thomas: seems useless now ?
        #exec(index) # this loads the indicies.

        if (not varfile):
            if ivar < 0:
                varfile = 'var.dat'
            else:
                varfile = 'VAR' + str(ivar)

        if proc < 0:
            procdirs = natural_sort(
                filter(lambda s: s.startswith('proc'), os.listdir(datadir)))
        else:
            procdirs = ['proc' + str(proc)]

        #global array
        if (not run2D):
            f = np.zeros((totalvars, dim.mz, dim.my, dim.mx), dtype=precision)
        else:
            if dim.ny == 1:
                f = np.zeros((totalvars, dim.mz, dim.mx), dtype=precision)
            else:
                f = np.zeros((totalvars, dim.my, dim.mx), dtype=precision)
        x = np.zeros(dim.mx, dtype=precision)
        y = np.zeros(dim.my, dtype=precision)
        z = np.zeros(dim.mz, dtype=precision)
        for directory in procdirs:

            proc = int(directory[4:])
            procdim = read_dim(datadir, proc, down=ldownsampled)
            if (not quiet):
                #print "reading data from processor %i of %i ..." \ # Python 2
                #% (proc, len(procdirs)) # Python 2
                print("reading data from processor {0} of {1} ...".format(
                    proc, len(procdirs)))

            mxloc = procdim.mx
            myloc = procdim.my
            mzloc = procdim.mz

            #read data
            filename = os.path.join(datadir, directory, varfile)
            infile = npfile(filename, endian=format)
            if (not run2D):
                f_loc = infile.fort_read(precision,
                                         shape=(-1, mzloc, myloc, mxloc))
            else:
                if dim.ny == 1:
                    f_loc = infile.fort_read(precision,
                                             shape=(-1, mzloc, mxloc))
                else:
                    f_loc = infile.fort_read(precision,
                                             shape=(-1, myloc, mxloc))
            raw_etc = infile.fort_read(precision)
            infile.close()

            t = raw_etc[0]
            x_loc = raw_etc[1:mxloc + 1]
            y_loc = raw_etc[mxloc + 1:mxloc + myloc + 1]
            z_loc = raw_etc[mxloc + myloc + 1:mxloc + myloc + mzloc + 1]
            if (param.lshear):
                shear_offset = 1
                deltay = raw_etc[-1]
            else:
                shear_offset = 0

            dx = raw_etc[-3 - shear_offset]
            dy = raw_etc[-2 - shear_offset]
            dz = raw_etc[-1 - shear_offset]

            if len(procdirs) > 1:
                # Calculate where the local processor will go in
                # the global array.

                # Don't overwrite ghost zones of processor to the left (and
                # accordingly in y and z direction--makes a difference on the
                # diagonals).
                #
                # Recall that in NumPy, slicing is NON-INCLUSIVE on the right end
                # ie, x[0:4] will slice all of a 4-digit array, not produce
                # an error like in idl.

                if procdim.ipx == 0:
                    i0x = 0
                    i1x = i0x + procdim.mx
                    i0xloc = 0
                    i1xloc = procdim.mx
                else:
                    i0x = procdim.ipx * procdim.nx + procdim.nghostx
                    i1x = i0x + procdim.mx - procdim.nghostx
                    i0xloc = procdim.nghostx
                    i1xloc = procdim.mx

                if procdim.ipy == 0:
                    i0y = 0
                    i1y = i0y + procdim.my
                    i0yloc = 0
                    i1yloc = procdim.my
                else:
                    i0y = procdim.ipy * procdim.ny + procdim.nghosty
                    i1y = i0y + procdim.my - procdim.nghosty
                    i0yloc = procdim.nghosty
                    i1yloc = procdim.my

                if procdim.ipz == 0:
                    i0z = 0
                    i1z = i0z + procdim.mz
                    i0zloc = 0
                    i1zloc = procdim.mz
                else:
                    i0z = procdim.ipz * procdim.nz + procdim.nghostz
                    i1z = i0z + procdim.mz - procdim.nghostz
                    i0zloc = procdim.nghostz
                    i1zloc = procdim.mz

                x[i0x:i1x] = x_loc[i0xloc:i1xloc]
                y[i0y:i1y] = y_loc[i0yloc:i1yloc]
                z[i0z:i1z] = z_loc[i0zloc:i1zloc]

                if (not run2D):
                    f[:, i0z:i1z, i0y:i1y, i0x:i1x] = \
                        f_loc[:, i0zloc:i1zloc, i0yloc:i1yloc, i0xloc:i1xloc]
                else:
                    if dim.ny == 1:
                        f[:, i0z:i1z, i0x:i1x] = \
                              f_loc[:, i0zloc:i1zloc, i0xloc:i1xloc]
                    else:
                        f[:, i0y:i1y, i0x:i1x] = \
                              f_loc[:, i0yloc:i1yloc, i0xloc:i1xloc]
            else:
                f = f_loc
                x = x_loc
                y = y_loc
                z = z_loc
            #endif MPI run
        #endfor directories loop

        if (magic is not None):
            if ('bb' in magic):
                # Compute the magnetic field before doing trimall.
                aa = f[index['ax'] - 1:index['az'], ...]
                self.bb = curl(aa, dx, dy, dz, x, y, z, run2D=param.lwrite_2d)
                if (trimall):
                    self.bb = self.bb[:, dim.n1:dim.n2 + 1, dim.m1:dim.m2 + 1,
                                      dim.l1:dim.l2 + 1]
            if ('jj' in magic):
                # Compute the electric current field before doing trimall.
                aa = f[index['ax'] - 1:index['az'], ...]
                self.jj = curl2(aa, dx, dy, dz, x, y, z)
                if (trimall):
                    self.jj = self.jj[:, dim.n1:dim.n2 + 1, dim.m1:dim.m2 + 1,
                                      dim.l1:dim.l2 + 1]
            if ('vort' in magic):
                # Compute the vorticity field before doing trimall.
                uu = f[index['ux'] - 1:index['uz'], ...]
                self.vort = curl(uu,
                                 dx,
                                 dy,
                                 dz,
                                 x,
                                 y,
                                 z,
                                 run2D=param.lwrite_2d)
                if (trimall):
                    if (param.lwrite_2d):
                        if (dim.nz == 1):
                            self.vort = self.vort[:, dim.m1:dim.m2 + 1,
                                                  dim.l1:dim.l2 + 1]
                        else:
                            self.vort = self.vort[:, dim.n1:dim.n2 + 1,
                                                  dim.l1:dim.l2 + 1]
                    else:
                        self.vort = self.vort[:, dim.n1:dim.n2 + 1,
                                              dim.m1:dim.m2 + 1,
                                              dim.l1:dim.l2 + 1]

        # Trim the ghost zones of the global f-array if asked.
        if trimall:
            self.x = x[dim.l1:dim.l2 + 1]
            self.y = y[dim.m1:dim.m2 + 1]
            self.z = z[dim.n1:dim.n2 + 1]
            if (not run2D):
                self.f = f[:, dim.n1:dim.n2 + 1, dim.m1:dim.m2 + 1,
                           dim.l1:dim.l2 + 1]
            else:
                if dim.ny == 1:
                    self.f = f[:, dim.n1:dim.n2 + 1, dim.l1:dim.l2 + 1]
                else:
                    self.f = f[:, dim.m1:dim.m2 + 1, dim.l1:dim.l2 + 1]
        else:
            self.x = x
            self.y = y
            self.z = z
            self.f = f
            self.l1 = dim.l1
            self.l2 = dim.l2 + 1
            self.m1 = dim.m1
            self.m2 = dim.m2 + 1
            self.n1 = dim.n1
            self.n2 = dim.n2 + 1

        # Assign an attribute to self for each variable defined in
        # 'data/index.pro' so that e.g. self.ux is the x-velocity.
        for key, value in index.items():
            # print key,value.
            if key != 'global_gg':
                setattr(self, key, self.f[value - 1, ...])
        # special treatment for vector quantities
        if 'uu' in index.keys():
            self.uu = self.f[index['ux'] - 1:index['uz'], ...]
        if 'aa' in index.keys():
            self.aa = self.f[index['ax'] - 1:index['az'], ...]
        # Also treat Fcr (from cosmicrayflux) as a vector.
        if 'fcr' in index.keys():
            self.fcr = self.f[index['fcr'] - 1:index['fcr'] + 2, ...]
            self.fcrx = self.fcr[0]
            self.fcry = self.fcr[1]
            self.fcrz = self.fcr[2]

        self.t = t
        self.dx = dx
        self.dy = dy
        self.dz = dz
        if param.lshear:
            self.deltay = deltay

        # Do the rest of magic after the trimall (i.e. no additional curl...).
        self.magic = magic
        if self.magic is not None:
            self.__magicAttributes(param)
Beispiel #27
0
    def __init__(self,
                 datadir='data/',
                 proc=-1,
                 ivar=-1,
                 quiet=False,
                 trim=False,
                 format='native',
                 param=None,
                 down=False):
        """
        Read grid from pencil code. if proc < 0, then load all data
        and assemble. otherwise, load grid from specified processor.
        """
        datadir = os.path.expanduser(datadir)
        if param is None:
            param = read_param(datadir, quiet=quiet)
        dim = read_dim(datadir, proc, down=down)
        if dim.precision == 'D':
            precision = 'd'
        else:
            precision = 'f'

        if proc < 0:
            procdirs = list(
                filter(lambda s: s.startswith('proc'), os.listdir(datadir)))
            if (param.io_strategy == 'MPI-IO|collect'
                    or param.io_strategy == 'collect'):
                procdirs = ['allprocs']
        else:
            procdirs = ['proc' + str(proc)]

        #global array
        x = N.zeros(dim.mx, dtype=precision)
        y = N.zeros(dim.my, dtype=precision)
        z = N.zeros(dim.mz, dtype=precision)
        dx_1 = N.zeros(dim.mx, dtype=precision)
        dy_1 = N.zeros(dim.my, dtype=precision)
        dz_1 = N.zeros(dim.mz, dtype=precision)
        dx_tilde = N.zeros(dim.mx, dtype=precision)
        dy_tilde = N.zeros(dim.my, dtype=precision)
        dz_tilde = N.zeros(dim.mz, dtype=precision)

        for directory in procdirs:
            if directory != 'allprocs':
                proc = int(directory[4:])
            procdim = read_dim(datadir, proc, down=down)
            if not quiet:
                #print "reading data from processor %i of %i ..." \ # Python 2
                #% (proc, len(procdirs)) # Python 2
                print("reading data from processor {0} of {1} ...".format(
                    proc, len(procdirs)))

            mxloc = procdim.mx
            myloc = procdim.my
            mzloc = procdim.mz

            #read data
            if down:
                griddat = 'grid_down.dat'
            else:
                griddat = 'grid.dat'
            filename = os.path.join(datadir, directory, griddat)
            infile = npfile(filename, endian=format)
            grid_raw = infile.fort_read(precision)
            dx, dy, dz = tuple(infile.fort_read(precision))
            Lx, Ly, Lz = tuple(infile.fort_read(precision))
            dx_1_raw = infile.fort_read(precision)
            dx_tilde_raw = infile.fort_read(precision)
            infile.close()

            #reshape
            t = grid_raw[0]
            x_loc = grid_raw[1:mxloc + 1]
            y_loc = grid_raw[mxloc + 1:mxloc + myloc + 1]
            z_loc = grid_raw[mxloc + myloc + 1:mxloc + myloc + mzloc + 1]
            dx_1_loc = dx_1_raw[0:mxloc]
            dy_1_loc = dx_1_raw[mxloc:mxloc + myloc]
            dz_1_loc = dx_1_raw[mxloc + myloc:mxloc + myloc + mzloc]
            dx_tilde_loc = dx_tilde_raw[0:mxloc]
            dy_tilde_loc = dx_tilde_raw[mxloc:mxloc + myloc]
            dz_tilde_loc = dx_tilde_raw[mxloc + myloc:mxloc + myloc + mzloc]

            if len(procdirs) > 1:
                if procdim.ipx == 0:
                    i0x = 0
                    i1x = i0x + procdim.mx
                    i0xloc = 0
                    i1xloc = procdim.mx
                else:
                    i0x = procdim.ipx * procdim.nx + procdim.nghostx
                    i1x = i0x + procdim.mx - procdim.nghostx
                    i0xloc = procdim.nghostx
                    i1xloc = procdim.mx

                if procdim.ipy == 0:
                    i0y = 0
                    i1y = i0y + procdim.my
                    i0yloc = 0
                    i1yloc = procdim.my
                else:
                    i0y = procdim.ipy * procdim.ny + procdim.nghosty
                    i1y = i0y + procdim.my - procdim.nghosty
                    i0yloc = procdim.nghosty
                    i1yloc = procdim.my

                if procdim.ipz == 0:
                    i0z = 0
                    i1z = i0z + procdim.mz
                    i0zloc = 0
                    i1zloc = procdim.mz
                else:
                    i0z = procdim.ipz * procdim.nz + procdim.nghostz
                    i1z = i0z + procdim.mz - procdim.nghostz
                    i0zloc = procdim.nghostz
                    i1zloc = procdim.mz

                x[i0x:i1x] = x_loc[i0xloc:i1xloc]
                y[i0y:i1y] = y_loc[i0yloc:i1yloc]
                z[i0z:i1z] = z_loc[i0zloc:i1zloc]
                dx_1[i0x:i1x] = dx_1_loc[i0xloc:i1xloc]
                dy_1[i0y:i1y] = dy_1_loc[i0yloc:i1yloc]
                dz_1[i0z:i1z] = dz_1_loc[i0zloc:i1zloc]
                dx_tilde[i0x:i1x] = dx_tilde_loc[i0xloc:i1xloc]
                dy_tilde[i0y:i1y] = dy_tilde_loc[i0yloc:i1yloc]
                dz_tilde[i0z:i1z] = dz_tilde_loc[i0zloc:i1zloc]

            else:
                x = x_loc
                y = y_loc
                z = z_loc
                dx_1 = dx_1_loc
                dy_1 = dy_1_loc
                dz_1 = dz_1_loc
                dx_tilde = dx_tilde_loc
                dy_tilde = dy_tilde_loc
                dz_tilde = dz_tilde_loc
            #endif MPI run

        # end directories loop
        if trim:
            self.x = x[dim.l1:dim.l2 + 1]
            self.y = y[dim.m1:dim.m2 + 1]
            self.z = z[dim.n1:dim.n2 + 1]
            self.dx_1 = dx_1[dim.l1:dim.l2 + 1]
            self.dy_1 = dy_1[dim.m1:dim.m2 + 1]
            self.dx_1 = dz_1[dim.n1:dim.n2 + 1]
            self.dx_tilde = dx_tilde[dim.l1:dim.l2 + 1]
            self.dy_tilde = dy_tilde[dim.m1:dim.m2 + 1]
            self.dx_tilde = dz_tilde[dim.n1:dim.n2 + 1]
        else:
            self.x = x
            self.y = y
            self.z = z
            self.dx_1 = dx_1
            self.dy_1 = dy_1
            self.dx_1 = dz_1
            self.dx_tilde = dx_tilde
            self.dy_tilde = dy_tilde
            self.dx_tilde = dz_tilde

        self.t = t
        self.dx = dx
        self.dy = dy
        self.dz = dz
        self.Lx = Lx
        self.Ly = Ly
        self.Lz = Lz
Beispiel #28
0
    def __init__(self, varfile='', datadir='data/', proc=-1, ivar=-1,
                 quiet=False, trimall=False, format='native',
                 param=None, dim=None, index=None, run2D=False,
                 magic=None, setup=None):
        """
        Description:
        -----------
        Read VAR files from pencil code. if proc < 0, then load all data
        and assemble. otherwise, load VAR file from specified processor.

        format -- one of (['native', 'n'], ['ieee-le', 'l'],
        ['ieee-be', 'B']) for byte-ordering

        Params:
        ------
            varfile=''
            datadir='data/'
            proc=-1
            ivar=-1
            quiet=False
            trimall=False
            format='native'
            param=None
            dim=None
            index=None
            run2D=False
        """
        if (setup is not None):
            datadir = os.path.expanduser(setup.datadir)
            dim = setup.dim
            param = setup.param
            index = setup.index
            run2D = setup.run2D
        else:
            datadir = os.path.expanduser(datadir)
            if dim is None:
                dim = read_dim(datadir,proc)
            if param is None:
                param = read_param(datadir=datadir, quiet=quiet)
            if index is None:
                index = read_index(datadir=datadir)

        if dim.precision == 'D':
            precision = 'd'
        else:
            precision = 'f'

        if param.lwrite_aux:
            totalvars = dim.mvar+dim.maux
        else:
            totalvars = dim.mvar

        # Read index.pro to get positions and "names"
        # of variables in f(mx,my,mz,nvar).
        # Thomas: seems useless now ?
        #exec(index) # this loads the indicies.

        if (not varfile):
            if ivar < 0:
                varfile = 'var.dat'
            else:
                varfile = 'VAR'+str(ivar)

        if proc < 0:
            procdirs = natural_sort(filter(lambda s:s.startswith('proc'),
                                    os.listdir(datadir)))
        else:
            procdirs = ['proc'+str(proc)]

        #global array
        if (not run2D):
            f = np.zeros((totalvars, dim.mz, dim.my, dim.mx),
                         dtype=precision)
        else:
            if dim.ny == 1:
                f = np.zeros((totalvars, dim.mz, dim.mx), dtype=precision)
            else:
                f = np.zeros((totalvars, dim.my, dim.mx), dtype=precision)
        x = np.zeros(dim.mx, dtype=precision)
        y = np.zeros(dim.my, dtype=precision)
        z = np.zeros(dim.mz, dtype=precision)
        for directory in procdirs:

            proc = int(directory[4:])
            procdim = read_dim(datadir, proc)
            if (not quiet):
                #print "reading data from processor %i of %i ..." \ # Python 2
                      #% (proc, len(procdirs)) # Python 2
                print("reading data from processor {0} of {1} ...".format(proc, len(procdirs)))

            mxloc = procdim.mx
            myloc = procdim.my
            mzloc = procdim.mz

            #read data
            filename = os.path.join(datadir,directory,varfile)
            infile = npfile(filename, endian=format)
            if (not run2D):
                f_loc = infile.fort_read(precision,
                                         shape=(-1, mzloc, myloc, mxloc))
            else:
                if dim.ny == 1:
                    f_loc = infile.fort_read(precision, shape=(-1, mzloc, mxloc))
                else:
                    f_loc = infile.fort_read(precision, shape=(-1, myloc, mxloc))
            raw_etc = infile.fort_read(precision)
            infile.close()

            t = raw_etc[0]
            x_loc = raw_etc[1:mxloc+1]
            y_loc = raw_etc[mxloc+1:mxloc+myloc+1]
            z_loc = raw_etc[mxloc+myloc+1:mxloc+myloc+mzloc+1]
            if (param.lshear):
                shear_offset = 1
                deltay = raw_etc[-1]
            else:
                shear_offset = 0

            dx = raw_etc[-3-shear_offset]
            dy = raw_etc[-2-shear_offset]
            dz = raw_etc[-1-shear_offset]

            if len(procdirs) > 1:
                # Calculate where the local processor will go in
                # the global array.

                # Don't overwrite ghost zones of processor to the left (and
                # accordingly in y and z direction--makes a difference on the
                # diagonals).
                #
                # Recall that in NumPy, slicing is NON-INCLUSIVE on the right end
                # ie, x[0:4] will slice all of a 4-digit array, not produce
                # an error like in idl.

                if procdim.ipx == 0:
                    i0x = 0
                    i1x = i0x+procdim.mx
                    i0xloc = 0
                    i1xloc = procdim.mx
                else:
                    i0x = procdim.ipx*procdim.nx+procdim.nghostx
                    i1x = i0x+procdim.mx-procdim.nghostx
                    i0xloc = procdim.nghostx
                    i1xloc = procdim.mx

                if procdim.ipy == 0:
                    i0y = 0
                    i1y = i0y+procdim.my
                    i0yloc = 0
                    i1yloc = procdim.my
                else:
                    i0y = procdim.ipy*procdim.ny+procdim.nghosty
                    i1y = i0y+procdim.my-procdim.nghosty
                    i0yloc = procdim.nghosty
                    i1yloc = procdim.my

                if procdim.ipz == 0:
                    i0z = 0
                    i1z = i0z+procdim.mz
                    i0zloc = 0
                    i1zloc = procdim.mz
                else:
                    i0z = procdim.ipz*procdim.nz+procdim.nghostz
                    i1z = i0z+procdim.mz-procdim.nghostz
                    i0zloc = procdim.nghostz
                    i1zloc = procdim.mz

                x[i0x:i1x] = x_loc[i0xloc:i1xloc]
                y[i0y:i1y] = y_loc[i0yloc:i1yloc]
                z[i0z:i1z] = z_loc[i0zloc:i1zloc]

                if (not run2D):
                    f[:, i0z:i1z, i0y:i1y, i0x:i1x] = \
                        f_loc[:, i0zloc:i1zloc, i0yloc:i1yloc, i0xloc:i1xloc]
                else:
                    if dim.ny == 1:
                        f[:, i0z:i1z, i0x:i1x] = \
                              f_loc[:, i0zloc:i1zloc, i0xloc:i1xloc]
                    else:
                        f[:, i0y:i1y, i0x:i1x] = \
                              f_loc[:, i0yloc:i1yloc, i0xloc:i1xloc]
            else:
                f = f_loc
                x = x_loc
                y = y_loc
                z = z_loc
            #endif MPI run
        #endfor directories loop

        if (magic is not None):
            if ('bb' in magic):
                # Compute the magnetic field before doing trimall.
                aa = f[index['ax']-1:index['az'],...]
                self.bb = curl(aa,dx,dy,dz,run2D=param.lwrite_2d)
                if (trimall): self.bb=self.bb[:, dim.n1:dim.n2+1,
                dim.m1:dim.m2+1, dim.l1:dim.l2+1]
            if ('jj' in magic):
                # Compute the electric current field before doing trimall.
                aa = f[index['ax']-1:index['az'],...]
                self.jj = curl2(aa,dx,dy,dz)
                if (trimall): self.jj=self.jj[:, dim.n1:dim.n2+1,
                dim.m1:dim.m2+1, dim.l1:dim.l2+1]
            if ('vort' in magic):
                # Compute the vorticity field before doing trimall.
                uu = f[index['ux']-1:index['uz'],...]
                self.vort = curl(uu,dx,dy,dz,run2D=param.lwrite_2d)
                if (trimall):
                    if (param.lwrite_2d):
                        if (dim.nz == 1):
                            self.vort=self.vort[:, dim.m1:dim.m2+1,
                            dim.l1:dim.l2+1]
                        else:
                            self.vort=self.vort[:, dim.n1:dim.n2+1,
                            dim.l1:dim.l2+1]
                    else:
                        self.vort=self.vort[:, dim.n1:dim.n2+1,
                        dim.m1:dim.m2+1, dim.l1:dim.l2+1]

        # Trim the ghost zones of the global f-array if asked.
        if trimall:
            self.x = x[dim.l1:dim.l2+1]
            self.y = y[dim.m1:dim.m2+1]
            self.z = z[dim.n1:dim.n2+1]
            if (not run2D):
                self.f = f[:, dim.n1:dim.n2+1, dim.m1:dim.m2+1, dim.l1:dim.l2+1]
            else:
               if dim.ny == 1:
                   self.f = f[:, dim.n1:dim.n2+1, dim.l1:dim.l2+1]
               else:
                   self.f = f[:, dim.m1:dim.m2+1, dim.l1:dim.l2+1]
        else:
            self.x = x
            self.y = y
            self.z = z
            self.f = f
            self.l1 = dim.l1
            self.l2 = dim.l2+1
            self.m1 = dim.m1
            self.m2 = dim.m2+1
            self.n1 = dim.n1
            self.n2 = dim.n2+1

        # Assign an attribute to self for each variable defined in
        # 'data/index.pro' so that e.g. self.ux is the x-velocity.
        for key,value in index.items():
            # print key,value.
            if key != 'global_gg':
                setattr(self,key,self.f[value-1,...])
        # special treatment for vector quantities
        if 'uu' in index.keys():
            self.uu = self.f[index['ux']-1:index['uz'],...]
        if 'aa' in index.keys():
            self.aa = self.f[index['ax']-1:index['az'],...]
        # Also treat Fcr (from cosmicrayflux) as a vector.
        if 'fcr' in index.keys():
            self.fcr = self.f[index['fcr']-1:index['fcr']+2,...]
            self.fcrx = self.fcr[0]
            self.fcry = self.fcr[1]
            self.fcrz = self.fcr[2]

        self.t = t
        self.dx = dx
        self.dy = dy
        self.dz = dz
        if param.lshear:
            self.deltay = deltay

        # Do the rest of magic after the trimall (i.e. no additional curl...).
        self.magic = magic
        if self.magic is not None:
            self.__magicAttributes(param)
Beispiel #29
0
    def __init__(self, datadir='data/', proc=-1, ivar=-1, quiet=False,
                 trim=False, format='native', param=None, down=False):
        """
        Read grid from pencil code. if proc < 0, then load all data
        and assemble. otherwise, load grid from specified processor.
        """
        datadir = os.path.expanduser(datadir)
        if param is None:
            param = read_param(datadir, quiet=quiet)
        dim = read_dim(datadir, proc, down=down)
        if dim.precision == 'D':
            precision = 'd'
        else:
            precision = 'f'

        if proc < 0:
            procdirs = list(filter(lambda s:s.startswith('proc'),
                                   os.listdir(datadir)))
        else:
            procdirs = ['proc'+str(proc)]

        #global array
        x = N.zeros(dim.mx, dtype=precision)
        y = N.zeros(dim.my, dtype=precision)
        z = N.zeros(dim.mz, dtype=precision)
        dx_1 = N.zeros(dim.mx, dtype=precision)
        dy_1 = N.zeros(dim.my, dtype=precision)
        dz_1 = N.zeros(dim.mz, dtype=precision)
        dx_tilde = N.zeros(dim.mx, dtype=precision)
        dy_tilde = N.zeros(dim.my, dtype=precision)
        dz_tilde = N.zeros(dim.mz, dtype=precision)

        for directory in procdirs:
            proc = int(directory[4:])
            procdim = read_dim(datadir, proc, down=down)
            if not quiet:
                #print "reading data from processor %i of %i ..." \ # Python 2
                      #% (proc, len(procdirs)) # Python 2
                print("reading data from processor {0} of {1} ...".format(proc, len(procdirs)))

            mxloc = procdim.mx
            myloc = procdim.my
            mzloc = procdim.mz

            #read data
            if down:
                griddat='grid_down.dat'
            else:
                griddat='grid.dat'
            filename = os.path.join(datadir, directory, griddat)
            infile = npfile(filename, endian=format)
            grid_raw = infile.fort_read(precision)
            dx, dy, dz = tuple(infile.fort_read(precision))
            Lx, Ly, Lz = tuple(infile.fort_read(precision))
            dx_1_raw = infile.fort_read(precision)
            dx_tilde_raw = infile.fort_read(precision)
            infile.close()

            #reshape
            t = grid_raw[0]
            x_loc = grid_raw[1:mxloc+1]
            y_loc = grid_raw[mxloc+1:mxloc+myloc+1]
            z_loc = grid_raw[mxloc+myloc+1:mxloc+myloc+mzloc+1]
            dx_1_loc = dx_1_raw[0:mxloc]
            dy_1_loc = dx_1_raw[mxloc:mxloc+myloc]
            dz_1_loc = dx_1_raw[mxloc+myloc:mxloc+myloc+mzloc]
            dx_tilde_loc = dx_tilde_raw[0:mxloc]
            dy_tilde_loc = dx_tilde_raw[mxloc:mxloc+myloc]
            dz_tilde_loc = dx_tilde_raw[mxloc+myloc:mxloc+myloc+mzloc]

            if len(procdirs) >1:
                if procdim.ipx == 0:
                    i0x = 0
                    i1x = i0x+procdim.mx
                    i0xloc = 0
                    i1xloc = procdim.mx
                else:
                    i0x = procdim.ipx*procdim.nx+procdim.nghostx
                    i1x = i0x+procdim.mx-procdim.nghostx
                    i0xloc = procdim.nghostx
                    i1xloc = procdim.mx

                if procdim.ipy == 0:
                    i0y = 0
                    i1y = i0y+procdim.my
                    i0yloc = 0
                    i1yloc = procdim.my
                else:
                    i0y = procdim.ipy*procdim.ny+procdim.nghosty
                    i1y = i0y+procdim.my-procdim.nghosty
                    i0yloc = procdim.nghosty
                    i1yloc = procdim.my

                if procdim.ipz == 0:
                    i0z = 0
                    i1z = i0z+procdim.mz
                    i0zloc = 0
                    i1zloc = procdim.mz
                else:
                    i0z = procdim.ipz*procdim.nz+procdim.nghostz
                    i1z = i0z+procdim.mz-procdim.nghostz
                    i0zloc = procdim.nghostz
                    i1zloc = procdim.mz

                x[i0x:i1x] = x_loc[i0xloc:i1xloc]
                y[i0y:i1y] = y_loc[i0yloc:i1yloc]
                z[i0z:i1z] = z_loc[i0zloc:i1zloc]
                dx_1[i0x:i1x] = dx_1_loc[i0xloc:i1xloc]
                dy_1[i0y:i1y] = dy_1_loc[i0yloc:i1yloc]
                dz_1[i0z:i1z] = dz_1_loc[i0zloc:i1zloc]
                dx_tilde[i0x:i1x] = dx_tilde_loc[i0xloc:i1xloc]
                dy_tilde[i0y:i1y] = dy_tilde_loc[i0yloc:i1yloc]
                dz_tilde[i0z:i1z] = dz_tilde_loc[i0zloc:i1zloc]

            else:
                x = x_loc
                y = y_loc
                z = z_loc
                dx_1 = dx_1_loc
                dy_1 = dy_1_loc
                dz_1 = dz_1_loc
                dx_tilde = dx_tilde_loc
                dy_tilde = dy_tilde_loc
                dz_tilde = dz_tilde_loc
            #endif MPI run

        # end directories loop
        if trim:
            self.x = x[dim.l1:dim.l2+1]
            self.y = y[dim.m1:dim.m2+1]
            self.z = z[dim.n1:dim.n2+1]
            self.dx_1 = dx_1[dim.l1:dim.l2+1]
            self.dy_1 = dy_1[dim.m1:dim.m2+1]
            self.dx_1 = dz_1[dim.n1:dim.n2+1]
            self.dx_tilde = dx_tilde[dim.l1:dim.l2+1]
            self.dy_tilde = dy_tilde[dim.m1:dim.m2+1]
            self.dx_tilde = dz_tilde[dim.n1:dim.n2+1]
        else:
            self.x = x
            self.y = y
            self.z = z
            self.dx_1 = dx_1
            self.dy_1 = dy_1
            self.dx_1 = dz_1
            self.dx_tilde = dx_tilde
            self.dy_tilde = dy_tilde
            self.dx_tilde = dz_tilde

        self.t = t
        self.dx = dx
        self.dy = dy
        self.dz = dz
        self.Lx = Lx
        self.Ly = Ly
        self.Lz = Lz