示例#1
0
def make_movie(
        field="uu1",
        datadir="data/",
        proc=-1,
        extension="xz",
        format="native",
        tmin=0.0,
        tmax=1.0e38,
        amin=0.0,
        amax=1.0,
        transform="",
        oldfile=False,
        norm=None,
        save=None,
        figsize=(16, 4),
        fps=12,
):
    """
    read 2D slice files and assemble an animation in a mpg movie.

    Quickly written from the example at http://matplotlib.sourceforge.net/faq/howto_faq.html

    Options:

     field  --- which variable to slice
     datadir--- path to data directory
     proc   --- an integer giving the processor to read a slice from
     extension --- which plane of xy,xz,yz,Xz. for 2D this should be overwritten.
     format  --- endian. one of little, big, or native (default)
     tmin    --- start time
     tmax    --- end time
     amin    --- minimum value for image scaling
     amax    --- maximum value for image scaling
     transform --- insert arbitrary numerical code to modify the slice
     norm    --- scales calar data
     save    --- directory to save file
     figsize --- tuple containing the size of the figure
     fps     --- Frames per seconds for the video
    """
    import os
    from pencil.io import npfile
    from pencil import read
    import numpy as np
    import pylab as plt
    from matplotlib import colors

    # Global configuration:
    # lines
    plt.rcParams["lines.linewidth"] = 2
    plt.rcParams["lines.color"] = "k"
    # font
    plt.rcParams["font.size"] = 30

    plt.rcParams["font.family"] = "serif"
    # legend
    plt.rcParams["legend.fontsize"] = 20
    plt.rcParams["legend.fancybox"] = False
    plt.rcParams["legend.numpoints"] = 2
    plt.rcParams["legend.shadow"] = False
    plt.rcParams["legend.frameon"] = False
    # latex
    plt.rc("text", usetex=True)
    plt.rcParams["text.latex.preamble"] = [r"\usepackage{amsmath}"]

    datadir = os.path.expanduser(datadir)
    if proc < 0:
        filename = os.path.join(datadir, "slice_" + field + "." + extension)
    else:
        filename = os.path.join(
            datadir, "proc" + str(proc) + "/slice_" + field + "." + extension)

    # global dim
    # param = read.param(datadir)

    dim = read.dim(datadir, proc)

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

    grid = read.grid(datadir=datadir, trim=True)
    # set up slice plane
    if extension == "xy" or extension == "Xy":
        hsize = dim.nx
        vsize = dim.ny
        xlabel = "x"
        ylabel = "y"
        x = grid.x
        y = grid.y
    if extension == "xz":
        hsize = dim.nx
        vsize = dim.nz
        xlabel = "x"
        ylabel = "z"
        x = grid.x
        y = grid.z
    if extension == "yz":
        hsize = dim.ny
        vsize = dim.nz
        xlabel = "y"
        ylabel = "z"
        x = grid.y
        y = grid.z

    plane = np.zeros((vsize, hsize), dtype=precision)

    infile = npfile(filename, endian=format)

    files = []
    fig = plt.figure(figsize=figsize)
    fig.subplots_adjust(left=0.12,
                        bottom=0.1,
                        right=0.98,
                        top=0.96,
                        wspace=0.23,
                        hspace=0.2)
    ax = fig.add_subplot(111)

    ifirst = True
    islice = 0
    while 1:
        try:
            raw_data = infile.fort_read(precision)
        except ValueError:
            break
        except TypeError:
            break

        if oldfile:
            t = raw_data[-1]
            plane = raw_data[:-1].reshape(vsize, hsize)
        else:
            slice_z2pos = raw_data[-1]
            t = raw_data[-2]
            plane = raw_data[:-2].reshape(vsize, hsize)

        if transform:
            exec("plane = plane" + transform)

        if t > tmin and t < tmax:
            ax.cla()
            title = "t = %11.3e" % t
            ax.set_title(title)
            ax.set_xlabel(xlabel)
            ax.set_ylabel(ylabel)

            ax.imshow(
                plane,
                origin="lower",
                vmin=amin,
                vmax=amax,
                norm=norm,
                cmap="hot",
                extent=[x[0], x[-1], y[0], y[-1]],
                aspect=1,
            )
            fname = "_tmp%03d.png" % islice
            print("Saving frame", fname)
            fig.savefig(fname)
            files.append(fname)

            if ifirst:
                print(
                    "----islice----------t---------min-------max-------delta")
            print("%10i %10.3e %10.3e %10.3e %10.3e" %
                  (islice, t, plane.min(), plane.max(),
                   plane.max() - plane.min()))

            ifirst = False
            islice += 1
        if t > tmax:
            break

    print("Making movie animation.mpg - this make take a while")
    os.system(
        f"mencoder 'mf://_tmp*.png' -mf type=png:fps={fps} -ovc lavc -lavcopts vcodec=wmv2 -oac copy -o animation.mpg"
    )
    if save:
        os.system(f"mv _tmp*.png {save}")
        print(f"Moving files to {save}")
    else:
        os.system("rm _tmp*.png")
        print("Removing all files")
    infile.close()
示例#2
0
    def calc(
            self,
            aver=[],
            datatopdir=".",
            lskip_zeros=False,
            proc=0,
            rank=0,
            rmfzeros=1,
            rmbzeros=1,
            iy=None,
            l_correction=False,
            t_correction=0.0,
            dim=None,
            timereducer=None,
            trargs=[],
            tindex=(0, None),
            imask=None,
    ):
        """object returns time dependent meridional tensors
        from Averages object aver.z. u, acoef and bcoef and aver.t

        For long DNS runs the 'zaverages.dat' file can be very large
        so MPI may be required and the data is loaded by processor
        as default.

        lskip_zeros=True identifies the resetting of the testfield
        and rmbzeros and rmfzeros number to exclude before and following
        By default none are removed.

        iy is the index array that is computed in this MPI process, which
        may be a subset of the array on this processor

        l_correction=True permits the pencil coefficients computed
        prior to the Pencil Code correction implemented after
        time=t_correction to be rescaled accordingly to match the new
        formulation.

        trargs contain optional arguments for the time treatments: mean,
        smoothing, etc.

        tindex is set to limit the range of the iterations loaded from
        Averages in zaverages.dat

        The index imask, excluding the resets, can be specified to
        ensure all processes use the same mask
        """
        import numpy as np
        import os
        from pencil import read

        os.chdir(datatopdir)  # return to working directory
        grid = read.grid(proc=proc, trim=True, quiet=True)
        # if iy None or scalar create numpy array
        try:
            iy.size > 0
        except:
            print("exception")
            if iy == None:
                print("exception None")
                iy = np.arange(grid.y.size)
            else:
                print("exception int")
                iy = np.array(iy)
        if rank == 0:
            print("iy size is {0}".format(iy.shape))
        r, theta = np.meshgrid(grid.x, grid.y[iy], indexing="ij")
        del (grid, theta)  # conserve memory

        print("rank {0} calculating tensors for proc {1}".format(rank, proc))

        # string containers for zaverages.z keys
        uformat = "u{0}mxy"
        alpformat = "alp{0}{1}xy"
        etaformat = "eta{0}{1}{2}xy"

        # imask calculated once for MPI/processor consistency
        if rank == 0:
            print("Removing zeros")
        old_size = aver.t.shape

        # if imask is not provided either exclude the zeros or use the full time series
        try:
            imask.size > 0
            print("imask shape is {}".format(imask.shape))
        except:
            if lskip_zeros:
                index = alpformat.format(1, 1)
                izero = np.array(
                    np.where(
                        aver.z.__getattribute__(index)
                        [:,
                         int(aver.z.__getattribute__(index).shape[-2] / 2),
                         int(aver.z.__getattribute__(index).shape[-1] /
                             2), ] == 0))[0]
                rmfrange = np.arange(0, rmfzeros - 1)
                rmbrange = np.arange(0, rmbzeros - 1)
                rmpoints = np.array([], dtype=int)
                for zero in izero:
                    rmpoints = np.append(rmpoints, rmfrange + zero)
                    rmpoints = np.append(rmpoints, zero - rmbrange)
                if izero.size > 0:
                    imask = np.delete(np.where(aver.t), rmpoints)
                    if rank == 0:
                        print("Removed {0} zeros from {1} resets".format(
                            len(rmpoints), len(izero)))
                        print(
                            "Resets occured at save points {0}".format(izero))
                else:
                    imask = np.where(aver.t)[0]
                del (rmpoints, rmbrange, rmfrange)
            else:
                imask = np.arange(aver.t.size)
                if rank == 0:
                    print("Skipped zero removals.")
        # update the time of the snapshots included
        self.t = aver.t[imask]

        # Correction to Pencil Code error may be required on old data
        if l_correction:
            if dim == None:
                dim = read.dim(quiet=True)
            itcorr = np.where(aver.t[imask] < t_correction)[0]
            index = alpformat.format(1, 3)
            aver.z.__getattribute__(
                index)[itcorr] *= -dim.nprocz / (dim.nprocz - 2.0)
            for j in range(0, 3):
                index = alpformat.format(3, j + 1)
                aver.z.__getattribute__(
                    index)[itcorr] *= -dim.nprocz / (dim.nprocz - 2.0)
            index = etaformat.format(1, 1, 1)
            aver.z.__getattribute__(
                index)[itcorr] *= -dim.nprocz / (dim.nprocz - 2.0)
            for j in range(0, 3):
                index = etaformat.format(j + 1, 2, 1)
                aver.z.__getattribute__(
                    index)[itcorr] *= -dim.nprocz / (dim.nprocz - 2.0)
            index = etaformat.format(1, 1, 2)
            aver.z.__getattribute__(
                index)[itcorr] *= -dim.nprocz / (dim.nprocz - 2.0)
            for j in range(0, 3):
                index = etaformat.format(j + 1, 2, 2)
                aver.z.__getattribute__(
                    index)[itcorr] *= -dim.nprocz / (dim.nprocz - 2.0)

        # set up place holders for the Pencil Code tensor coefficients
        index = alpformat.format(1, 1)
        u = np.zeros(
            [3,
             len(imask),
             aver.z.__getattribute__(index).shape[-2], iy.size])
        alp = np.zeros([
            3, 3,
            len(imask),
            aver.z.__getattribute__(index).shape[-2], iy.size
        ])
        eta = np.zeros([
            3, 3, 3,
            len(imask),
            aver.z.__getattribute__(index).shape[-2], iy.size
        ])
        if rank == 0:
            print(u.shape, aver.z.__getattribute__(index)[imask, :, :].shape)
        # store the individual components in the z-averages as tensors
        for i, coord in zip(range(0, 3), ("x", "y", "z")):
            try:
                index = uformat.format(coord)
                if iy.size > 1:
                    tmp = aver.z.__getattribute__(index)[:, :, iy]
                    u[i, :, :, :] = tmp[imask]
                else:
                    u[i, :, :, 0] = aver.z.__getattribute__(index)[imask, :,
                                                                   iy]
            except KeyError:
                pass
        for i in range(0, 3):
            for j in range(0, 3):
                index = alpformat.format(i + 1, j + 1)
                if iy.size > 1:
                    tmp = aver.z.__getattribute__(index)[:, :, iy]
                    alp[j, i, :, :, :] = tmp[imask]
                else:
                    alp[j, i, :, :,
                        0] = aver.z.__getattribute__(index)[imask, :, iy]
        for i in range(0, 3):
            for j in range(0, 3):
                index1 = etaformat.format(i + 1, j + 1, 1)
                index2 = etaformat.format(i + 1, j + 1, 2)
                # Sign difference with Schrinner + r correction
                if iy.size > 1:
                    tmp = aver.z.__getattribute__(index1)[:, :, iy]
                    # eta[0,j,i,:,:,:] = -tmp[imask]  # JOERN, no sign correction
                    eta[0, j, i, :, :, :] = tmp[imask]
                    tmp = aver.z.__getattribute__(index2)[:, :, iy]
                    # eta[1,j,i,:,:,:] = -tmp[imask]*r # JOERN, no sign correction
                    eta[1, j, i, :, :, :] = tmp[imask] * r
                    del tmp
                else:
                    # eta[0,j,i,:,:,0] = -aver.z.__getattribute__(index1)[imask,:,iy] # JOERN, no sign correction
                    # eta[1,j,i,:,:,0] = -aver.z.__getattribute__(index2)[imask,:,iy]*r[:,0] # JOERN, no sign correction
                    eta[0, j, i, :, :,
                        0] = aver.z.__getattribute__(index1)[imask, :, iy]
                    eta[1, j, i, :, :,
                        0] = (aver.z.__getattribute__(index2)[imask, :, iy] *
                              r[:, 0])

        # apply the specified averaging or smoothing: 'None' returns unprocessed arrays
        if callable(timereducer):
            u = timereducer(u, trargs)
            alp = timereducer(alp, trargs)
            eta = timereducer(eta, trargs)

        if rank == 0:
            print("Old time dimension has length: {0}".format(old_size))
            print("New time dimension has length: {0}".format(alp.shape[-3]))

        # Create output tensors
        datatype = alp.dtype
        datashape = [alp.shape[-3], alp.shape[-2], alp.shape[-1], 1]
        setattr(self, "utensor", np.zeros([3] + datashape, dtype=datatype))
        setattr(self, "alpha", np.zeros([3, 3] + datashape, dtype=datatype))
        setattr(self, "beta", np.zeros([3, 3] + datashape, dtype=datatype))
        setattr(self, "gamma", np.zeros([3] + datashape, dtype=datatype))
        setattr(self, "delta", np.zeros([3] + datashape, dtype=datatype))
        setattr(self, "kappa", np.zeros([3, 3, 3] + datashape, dtype=datatype))
        setattr(self, "acoef", np.zeros([3, 3] + datashape, dtype=datatype))
        setattr(self, "bcoef", np.zeros([3, 3, 3] + datashape, dtype=datatype))
        """
        All tensors need to be reordered nz,ny,nx,nt for efficient writing to disk
        """
        # Calculating a and b matrices
        self.acoef[:, :, :, :, :, 0] = np.copy(alp)
        self.acoef = np.swapaxes(self.acoef, -4, -1)
        self.acoef = np.swapaxes(self.acoef, -3, -2)
        self.bcoef[:, :, :, :, :, :, 0] = np.copy(eta)
        self.bcoef = np.swapaxes(self.bcoef, -4, -1)
        self.bcoef = np.swapaxes(self.bcoef, -3, -2)

        irr, ith, iph = 0, 1, 2

        # u-tensor
        print("Calculating utensor on rank {}".format(rank))
        # utensor[:,:,:,:,0] = u[:,:,:,:] - np.mean(u[:,:,:,:],axis=1,keepdims=True)
        self.utensor[:, :, :, :, 0] = u[:, :, :, :]
        self.utensor = np.swapaxes(self.utensor, -4, -1)
        self.utensor = np.swapaxes(self.utensor, -3, -2)
        # Alpha tensor
        print("Calculating alpha on rank {}".format(rank))
        self.alpha[irr, irr, :, :, :, 0] = (alp[irr, irr, :, :, :] -
                                            eta[ith, ith, irr, :, :, :] / r)
        self.alpha[irr, ith, :, :, :, 0] = 0.5 * (
            alp[ith, irr, :, :, :] + eta[ith, irr, irr, :, :, :] / r +
            alp[irr, ith, :, :, :] - eta[ith, ith, ith, :, :, :] / r)
        self.alpha[irr, iph, :, :, :,
                   0] = 0.5 * (alp[iph, irr, :, :, :] + alp[irr, iph, :, :, :]
                               - eta[ith, ith, iph, :, :, :] / r)
        self.alpha[ith, irr, :, :, :, 0] = self.alpha[irr, ith, :, :, :, 0]
        self.alpha[ith, ith, :, :, :, 0] = (alp[ith, ith, :, :, :] +
                                            eta[ith, irr, ith, :, :, :] / r)
        self.alpha[ith, iph, :, :, :,
                   0] = 0.5 * (alp[iph, ith, :, :, :] + alp[ith, iph, :, :, :]
                               + eta[ith, irr, iph, :, :, :] / r)
        self.alpha[iph, irr, :, :, :, 0] = self.alpha[irr, iph, :, :, :, 0]
        self.alpha[iph, ith, :, :, :, 0] = self.alpha[ith, iph, :, :, :, 0]
        self.alpha[iph, iph, :, :, :, 0] = alp[iph, iph, :, :, :]
        self.alpha = np.swapaxes(self.alpha, -4, -1)
        self.alpha = np.swapaxes(self.alpha, -3, -2)
        # Gamma vector
        print("Calculating gamma on rank {}".format(rank))
        self.gamma[irr, :, :, :,
                   0] = -0.5 * (alp[iph, ith, :, :, :] - alp[ith, iph, :, :, :]
                                - eta[ith, irr, iph, :, :, :] / r)
        self.gamma[ith, :, :, :,
                   0] = -0.5 * (alp[irr, iph, :, :, :] - alp[iph, irr, :, :, :]
                                - eta[ith, ith, iph, :, :, :] / r)
        self.gamma[iph, :, :, :, 0] = -0.5 * (
            alp[ith, irr, :, :, :] - alp[irr, ith, :, :, :] +
            eta[ith, irr, irr, :, :, :] / r + eta[ith, ith, ith, :, :, :] / r)
        self.gamma = np.swapaxes(self.gamma, -4, -1)
        self.gamma = np.swapaxes(self.gamma, -3, -2)
        # Beta tensor
        print("Calculating beta on rank {}".format(rank))
        self.beta[irr, irr, :, :, :, 0] = -0.5 * eta[ith, iph, irr, :, :, :]
        self.beta[irr, ith, :, :, :, 0] = 0.25 * (eta[irr, iph, irr, :, :, :] -
                                                  eta[ith, iph, ith, :, :, :])
        self.beta[irr, iph, :, :, :, 0] = 0.25 * (eta[ith, irr, irr, :, :, :] -
                                                  eta[ith, iph, iph, :, :, :] -
                                                  eta[irr, ith, irr, :, :, :])
        self.beta[ith, irr, :, :, :, 0] = self.beta[irr, ith, :, :, :, 0]
        self.beta[ith, ith, :, :, :, 0] = 0.5 * eta[irr, iph, ith, :, :, :]
        self.beta[ith, iph, :, :, :, 0] = 0.25 * (eta[ith, irr, ith, :, :, :] +
                                                  eta[irr, iph, iph, :, :, :] -
                                                  eta[irr, ith, ith, :, :, :])
        self.beta[iph, irr, :, :, :, 0] = self.beta[irr, iph, :, :, :, 0]
        self.beta[iph, ith, :, :, :, 0] = self.beta[ith, iph, :, :, :, 0]
        self.beta[iph, iph, :, :, :, 0] = 0.5 * (eta[ith, irr, iph, :, :, :] -
                                                 eta[irr, ith, iph, :, :, :])
        # Sign convention to match with meanfield_e_tensor
        # self.beta = -self.beta #JOERN, not needed
        self.beta = np.swapaxes(self.beta, -4, -1)
        self.beta = np.swapaxes(self.beta, -3, -2)
        # Delta vector
        print("Calculating delta on rank {}".format(rank))
        self.delta[irr, :, :, :, 0] = 0.25 * (eta[irr, ith, ith, :, :, :] -
                                              eta[ith, irr, ith, :, :, :] +
                                              eta[irr, iph, iph, :, :, :])
        self.delta[ith, :, :, :, 0] = 0.25 * (eta[ith, irr, irr, :, :, :] -
                                              eta[irr, ith, irr, :, :, :] +
                                              eta[ith, iph, iph, :, :, :])
        self.delta[iph, :, :, :, 0] = -0.25 * (eta[irr, iph, irr, :, :, :] +
                                               eta[ith, iph, ith, :, :, :])
        # Sign convention to match with meanfield_e_tensor
        # self.delta = -self.delta #JOERN, not needed
        self.delta = np.swapaxes(self.delta, -4, -1)
        self.delta = np.swapaxes(self.delta, -3, -2)
        # Kappa tensor
        print("Calculating kappa on rank {}".format(rank))
        for i in range(0, 3):
            self.kappa[irr, irr, i, :, :, :, 0] = -eta[irr, irr, i, :, :, :]
            self.kappa[ith, irr, i, :, :, :,
                       0] = -0.5 * (eta[ith, irr, i, :, :, :] +
                                    eta[irr, ith, i, :, :, :])
            self.kappa[iph, irr, i, :, :, :,
                       0] = -0.5 * eta[irr, iph, i, :, :, :]
            self.kappa[irr, ith, i, :, :, :, 0] = self.kappa[ith, irr,
                                                             i, :, :, :, 0]
            self.kappa[ith, ith, i, :, :, :, 0] = -eta[ith, ith, i, :, :, :]
            self.kappa[iph, ith, i, :, :, :,
                       0] = -0.5 * eta[ith, iph, i, :, :, :]
            self.kappa[irr, iph, i, :, :, :, 0] = self.kappa[iph, irr,
                                                             i, :, :, :, 0]
            self.kappa[ith, iph, i, :, :, :, 0] = self.kappa[iph, ith,
                                                             i, :, :, :, 0]
            self.kappa[iph, iph, i, :, :, :, 0] = 1e-91
        # Sign convention to match with meanfield_e_tensor
        # self.kappa = -self.kappa #JOERN, not needed
        self.kappa = np.swapaxes(self.kappa, -4, -1)
        self.kappa = np.swapaxes(self.kappa, -3, -2)
        setattr(self, "imask", imask)
示例#3
0
def zav2h5(
    folder='.',
    dataset='',
    filename='emftensors.h5',
    timereducer='mean',
    hdf5dir='data/',
    l_correction=True,
    t_correction=8972.,
    rmfzeros=4,
    rmbzeros=2,
    dgroup='emftensor',
):
    """
    If large dataset MPI may be required.
    Loads Averages object and applies tensors calculation and reforms for
    efficient writing to hdf5 for mean field module simulations.
    MPI call needs to be improved to avoid MemoryError for large files
    with read.aver(plane_list=['z'])
    timereducers needs to be expanded to include various smoothing options
    """
    import numpy as np
    from pencil import read
    from pencil.read import aver
    from pencil.export import create_h5, fvars, create_aver_sph
    #    from pencil.export import create_h5.fvars as fvars
    #    from pencil.export import create_aver_sph
    from pencil.calc import tensors_sph
    import h5py
    import copy

    timereducers = {
        'mean':
        lambda x, args: np.mean(x, axis=-3, keepdims=True),
        #np.std(x,axis=-3)),
        'mean_last':
        lambda x, args: np.mean(np.take(
            x, np.arange(-int(args[0]), 0, 1), axis=-3),
                                axis=-3,
                                keepdims=True),
        'none':
        lambda x, args: x
    }
    if not timereducer in timereducers:
        raise ValueError(
            'timereducer "{}" undefined in timereducers'.format(timereducer) +
            ' options: {}'.format(timereducers.keys()))
    if len(dataset) == 0:
        dataset = timereducer
    with open('zaver.in', 'r') as f:
        zavers = f.read().splitlines()
    """ Find out if the calculation is parallel and distribute the arrays
        according to y-index and ipz=0 processor layout
    """
    try:
        from mpi4py import MPI

        comm = MPI.COMM_WORLD
        rank = comm.Get_rank()  # rank of processor on which this script runs
        size = comm.Get_size()  # number of  ~  ~  ~  ~

        l_mpi = True
        l_mpi = l_mpi and (size != 1)
    except ImportError:
        l_mpi = False
        rank = 0
        size = 1
        comm = None
    dim = read.dim()
    nx, nny = dim.nx, dim.ny
    ayindex = np.arange(nny)
    if l_mpi:
        y_chunks = np.array_split(ayindex, size, axis=0)
        yindex = y_chunks[rank]
        ny = yindex.size
    else:
        yindex = ayindex  # vector 0 ... nygrid-1
        ny = nny

    ncpus = dim.nprocx * dim.nprocy
    aprocs = np.arange(ncpus)  # vector 0 ... nprocx*nprocy-1
    if np.mod(ncpus, size) > 0:
        print('number of processes must divide {} cpus'.format(ncpus))
        quit()
    if l_mpi:
        if size > aprocs.size:
            nsz = size / aprocs.size
            for ii in range(1, nsz):
                tmproc = np.append(aprocs, aprocs)
            aprocs = np.sort(tmproc)
        proc_chunks = np.array_split(aprocs, size, axis=0)
        proc = proc_chunks[rank]
    else:
        proc = aprocs
    """Set up hdf5 file and create datasets in which to save the tensors
    """
    lskip_zeros = rmfzeros + rmbzeros > 0
    if rank == 0:  # if root processor
        grid = read.grid(trim=True, quiet=True)  # read grid
        zav = read.aver(proc=0,
                        plane_list=['z'
                                    ])  # read zaverages of root proc of PC run
        tensor = tensors_sph(  # decompose into individual effect tensors
            zav,
            proc=proc[0],
            rank=0,
            lskip_zeros=lskip_zeros,
            iy=[
                int(ny / 2 / dim.nprocy),
            ],
            timereducer=timereducers[timereducer],
            #trargs=trargs,
            rmfzeros=rmfzeros,
            rmbzeros=rmbzeros,
            l_correction=l_correction,
            t_correction=t_correction,
            dim=dim,
            #tindex=tindex
        )
        if 'mean' in dataset:
            nt = 1
        else:
            nt = tensor.t.size
        create_aver_sph(hdf5dir + filename,
                        dataset,
                        fvars, (1, ny, nx, nt), (0, grid.y, grid.x, tensor.t),
                        hdf5dir=hdf5dir,
                        dgroup=dgroup)
    if l_mpi:
        imask = comm.bcast(tensor.imask, root=0)
    else:
        imask = tensor.imask

    import os

    if os.path.exists(os.path.join(folder, 'averages/z.h5')):
        zav = aver(plane_list=['z'])  # read all averages
        tensor_buf = tensors_sph(  # calculate tensors
            aver=zav,
            rank=rank,
            lskip_zeros=lskip_zeros,
            timereducer=timereducers[timereducer],
            #trargs=trargs,
            rmfzeros=rmfzeros,
            rmbzeros=rmbzeros,
            l_correction=l_correction,
            t_correction=t_correction,
            dim=dim,
            #tindex=tindex,
            imask=imask)
    else:
        yndx_tmp = np.array_split(yindex, dim.nprocy)
        # list of vectors ipy*ny/nprocy ... (ipy+1)*ny/nprocy - 1, ipy=0,nprocy-1

        for ipy in range(dim.nprocy):  # over all y processors of the PC run
            for ipx in range(
                    dim.nprocx):  # over all x processors of the PC run

                iproc = dim.nprocx * ipy + ipx  # proc rank of the PC run (0 ... nprocx*nprocy-1)
                yndx = yndx_tmp[ipy] - ipy * int(dim.nygrid / dim.nprocy)

                zav = aver(proc=iproc,
                           plane_list=['z'])  # read averages from proc iproc

                print('calculating tensors on proc {0} rank {1}'.format(
                    iproc, rank))
                """
                if iproc==1:             # as there is corrupted data on proc 1
                    with open('zaver.in', 'r') as f:
                        zavers = f.read().splitlines()
                    for zaver in  zavers:
                        zav.z.__setattr__(zaver,np.insert(
                                zav.z.__getattribute__(zaver),3766,
                                0.5*(zav.z.__getattribute__(zaver)[3766]+
                                zav.z.__getattribute__(zaver)[3767]),axis=0))
                        zav.t=np.insert(zav.t,3766,0.5*(zav.t[3766]+zav.t[3767]),axis=0)
                """
                tensor_buf = tensors_sph(  # calculate tensors
                    aver=zav,
                    proc=iproc,
                    rank=rank,
                    lskip_zeros=lskip_zeros,
                    iy=yndx,
                    timereducer=timereducers[timereducer],
                    #trargs=trargs,
                    rmfzeros=rmfzeros,
                    rmbzeros=rmbzeros,
                    l_correction=l_correction,
                    t_correction=t_correction,
                    dim=dim,
                    #tindex=tindex,
                    imask=imask)
                if ipx == 0:
                    tensor = copy.deepcopy(tensor_buf)
                else:
                    for field, comp in fvars:
                        setattr(
                            tensor, field,
                            np.concatenate(
                                (tensor.__getattribute__(field),
                                 tensor_buf.__getattribute__(field)),
                                axis=len(comp) + 2))

        if l_mpi:
            comm.barrier()
            ds = h5py.File(hdf5dir + filename, 'a', driver='mpio', comm=comm)
        else:
            ds = h5py.File(hdf5dir + filename, 'a')  # open HDF5 file

        for field, comp in fvars:

            print('writing {0} from rank {1} for proc {2}'.format(
                field, rank, iproc))

            dsname = '{0}/{1}/{2}'.format(dgroup, field, dataset)
            if len(comp) == 1:
                ds[dsname][:, :,
                           yndx_tmp[ipy], :] = tensor.__getattribute__(field)
            elif len(comp) == 2:
                ds[dsname][:, :, :,
                           yndx_tmp[ipy], :] = tensor.__getattribute__(field)
            else:
                ds[dsname][:, :, :, :,
                           yndx_tmp[ipy], :] = tensor.__getattribute__(field)
        ds.close()
示例#4
0
    def find_fixed(
        self,
        datadir="data",
        var_file="VAR0",
        trace_field="bb",
        ti=-1,
        tf=-1,
        tracer_file_name=None,
    ):
        """
        Find the fixed points to a snapshot or existing tracer file.

        call signature::

          find_fixed(datadir='data', var_file='VAR0', trace_field='bb',
                     ti=-1, tf=-1, tracer_file_name=None):

        Keyword arguments:

          *datadir*:
            Data directory.

         *var_file*:
           Varfile to be read.

         *trace_field*:
           Vector field used for the streamline tracing.

          *ti*:
            Initial VAR file index for tracer time sequences. Overrides 'var_file'.

          *tf*:
            Final VAR file index for tracer time sequences. Overrides 'var_file'.

         *tracer_file_name*
           Name of the tracer file to be read.
           If 'None' compute the tracers.
        """

        import numpy as np
        import multiprocessing as mp
        from pencil import read
        from pencil import math
        from pencil.diag.tracers import Tracers
        from pencil.calc.streamlines import Stream
        from pencil.math.interpolation import vec_int

        if self.params.int_q == "curly_A":
            self.curly_A = []
        if self.params.int_q == "ee":
            self.ee = []

        # Multi core setup.
        if not (np.isscalar(self.params.n_proc)) or (self.params.n_proc % 1 != 0):
            print("Error: invalid processor number")
            return -1
        queue = mp.Queue()

        # Make sure to read the var files with the correct magic.
        magic = []
        if trace_field == "bb":
            magic.append("bb")
        if trace_field == "jj":
            magic.append("jj")
        if trace_field == "vort":
            magic.append("vort")
        if self.params.int_q == "ee":
            magic.append("bb")
            magic.append("jj")
        dim = read.dim(datadir=datadir)

        # Check if user wants a tracer time series.
        if (ti % 1 == 0) and (tf % 1 == 0) and (ti >= 0) and (tf >= ti):
            series = True
            var_file = "VAR{0}".format(ti)
            n_times = tf - ti + 1
        else:
            series = False
            n_times = 1
        self.t = np.zeros(n_times)

        # Read the initial field.
        var = read.var(
            var_file=var_file, datadir=datadir, magic=magic, quiet=True, trimall=True
        )

        self.t[0] = var.t
        grid = read.grid(datadir=datadir, quiet=True, trim=True)
        field = getattr(var, trace_field)
        param2 = read.param(datadir=datadir, quiet=True)
        if self.params.int_q == "ee":
            ee = var.jj * param2.eta - math.cross(var.uu, var.bb)
        self.params.datadir = datadir
        self.params.var_file = var_file
        self.params.trace_field = trace_field

        # Get the simulation parameters.
        self.params.dx = var.dx
        self.params.dy = var.dy
        self.params.dz = var.dz
        self.params.Ox = var.x[0]
        self.params.Oy = var.y[0]
        self.params.Oz = var.z[0]
        self.params.Lx = grid.Lx
        self.params.Ly = grid.Ly
        self.params.Lz = grid.Lz
        self.params.nx = dim.nx
        self.params.ny = dim.ny
        self.params.nz = dim.nz

        tracers = Tracers()
        tracers.params = self.params
        # Create the mapping for all times.
        if not tracer_file_name:
            tracers.find_tracers(
                var_file=var_file,
                datadir=datadir,
                trace_field=trace_field,
                ti=ti,
                tf=tf,
            )
        else:
            tracers.read(datadir=datadir, file_name=tracer_file_name)
        self.tracers = tracers

        # Set some default values.
        self.t = np.zeros((tf - ti + 1) * series + (1 - series))
        self.fixed_index = np.zeros((tf - ti + 1) * series + (1 - series))
        self.poincare = np.zeros(
            [
                int(self.params.trace_sub * dim.nx),
                int(self.params.trace_sub * dim.ny),
                n_times,
            ]
        )
        ix0 = range(0, int(self.params.nx * self.params.trace_sub) - 1)
        iy0 = range(0, int(self.params.ny * self.params.trace_sub) - 1)
        self.fixed_points = []
        self.fixed_sign = []
        self.fixed_tracers = []

        # Start the parallelized fixed point finding.
        for tidx in range(n_times):
            if tidx > 0:
                var = read.var(
                    var_file="VAR{0}".format(tidx + ti),
                    datadir=datadir,
                    magic=magic,
                    quiet=True,
                    trimall=True,
                )
                field = getattr(var, trace_field)
                self.t[tidx] = var.t

            proc = []
            sub_data = []
            fixed = []
            fixed_sign = []
            fixed_tracers = []
            for i_proc in range(self.params.n_proc):
                proc.append(
                    mp.Process(
                        target=self.__sub_fixed,
                        args=(queue, ix0, iy0, field, self.tracers, tidx, var, i_proc),
                    )
                )
            for i_proc in range(self.params.n_proc):
                proc[i_proc].start()
            for i_proc in range(self.params.n_proc):
                sub_data.append(queue.get())
            for i_proc in range(self.params.n_proc):
                proc[i_proc].join()
            for i_proc in range(self.params.n_proc):
                # Extract the data from the single cores. Mind the order.
                sub_proc = sub_data[i_proc][0]
                fixed.extend(sub_data[i_proc][1])
                fixed_tracers.extend(sub_data[i_proc][2])
                fixed_sign.extend(sub_data[i_proc][3])
                self.fixed_index[tidx] += sub_data[i_proc][4]
                self.poincare[sub_proc :: self.params.n_proc, :, tidx] = sub_data[
                    i_proc
                ][5]
            for i_proc in range(self.params.n_proc):
                proc[i_proc].terminate()

            # Discard fixed points which lie too close to each other.
            fixed, fixed_tracers, fixed_sign = self.__discard_close_fixed_points(
                np.array(fixed), np.array(fixed_sign), np.array(fixed_tracers), var
            )
            if self.fixed_points is None:
                self.fixed_points = []
                self.fixed_sign = []
                self.fixed_tracers = []
            self.fixed_points.append(np.array(fixed))
            self.fixed_sign.append(np.array(fixed_sign))
            self.fixed_tracers.append(fixed_tracers)

        # Compute the traced quantities along the fixed point streamlines.
        if (self.params.int_q == "curly_A") or (self.params.int_q == "ee"):
            for t_idx in range(0, n_times):
                if self.params.int_q == "curly_A":
                    self.curly_A.append([])
                if self.params.int_q == "ee":
                    self.ee.append([])
                for fixed in self.fixed_points[t_idx]:
                    # Trace the stream line.
                    xx = np.array([fixed[0], fixed[1], self.params.Oz])
                    #                    time = np.linspace(0, self.params.Lz/np.max(abs(field[2])), 10)
                    field_strength_z0 = vec_int(
                        xx,
                        field,
                        [var.dx, var.dy, var.dz],
                        [var.x[0], var.y[0], var.z[0]],
                        [len(var.x), len(var.y), len(var.z)],
                        interpolation=self.params.interpolation,
                    )
                    field_strength_z0 = np.sqrt(np.sum(field_strength_z0 ** 2))
                    time = np.linspace(0, 4 * self.params.Lz / field_strength_z0, 500)
                    stream = Stream(field, self.params, xx=xx, time=time)
                    # Do the field line integration.
                    if self.params.int_q == "curly_A":
                        curly_A = 0
                        for l in range(stream.iterations - 1):
                            aaInt = vec_int(
                                (stream.tracers[l + 1] + stream.tracers[l]) / 2,
                                var.aa,
                                [var.dx, var.dy, var.dz],
                                [var.x[0], var.y[0], var.z[0]],
                                [len(var.x), len(var.y), len(var.z)],
                                interpolation=self.params.interpolation,
                            )
                            curly_A += np.dot(
                                aaInt, (stream.tracers[l + 1] - stream.tracers[l])
                            )
                        self.curly_A[-1].append(curly_A)
                    if self.params.int_q == "ee":
                        ee_p = 0
                        for l in range(stream.iterations - 1):
                            eeInt = vec_int(
                                (stream.tracers[l + 1] + stream.tracers[l]) / 2,
                                ee,
                                [var.dx, var.dy, var.dz],
                                [var.x[0], var.y[0], var.z[0]],
                                [len(var.x), len(var.y), len(var.z)],
                                interpolation=self.params.interpolation,
                            )
                            ee_p += np.dot(
                                eeInt, (stream.tracers[l + 1] - stream.tracers[l])
                            )
                        self.ee[-1].append(ee_p)
                if self.params.int_q == "curly_A":
                    self.curly_A[-1] = np.array(self.curly_A[-1])
                if self.params.int_q == "ee":
                    self.ee[-1] = np.array(self.ee[-1])

        return 0
示例#5
0
    def update(self, hard=False, quiet=True):
        """Update simulation object:
            if not read in:
                - read param.nml
                - read grid and ghost grid

            Set hard=True to force update.
        """
        from os.path import exists
        from os.path import join
        from pencil.read import param, grid, dim

        REEXPORT = False

        if hard == True:
            self.param = False
            self.grid = False
            self.ghost_grid = False
            self.dim = False
            REEXPORT = True

        if self.param == False:
            try:
                if exists(join(self.datadir, 'param.nml')):
                    print('~ Reading param.nml.. ')
                    param = param(quiet=quiet, datadir=self.datadir)
                    self.param = {}
                    # read params into Simulation object
                    for key in dir(param):
                        if key.startswith('_') or key == 'read': continue
                        if type(getattr(param,
                                        key)) in [bool, list, float, int, str]:
                            self.param[key] = getattr(param, key)
                        else:
                            try:
                                # allow for nested param objects
                                self.param[key] = {}
                                for subkey in dir(getattr(param, key)):
                                    if subkey.startswith(
                                            '_') or subkey == 'read':
                                        continue
                                    if type(
                                            getattr(getattr(param, key),
                                                    subkey)) in [
                                                        bool, list, float, int,
                                                        str
                                                    ]:
                                        self.param[key][subkey] = getattr(
                                            getattr(param, key), subkey)
                            except:
                                # not nested param objects
                                continue
                    REEXPORT = True
                else:
                    if not quiet:
                        print('? WARNING: for ' + self.path +
                              '\n? Simulation has ' +
                              'not run yet! Meaning: No param.nml found!')
                    REEXPORT = True
            except:
                print('! ERROR: while reading param.nml for ' + self.path)
                self.param = False
                REEXPORT = True

        if self.param != False and (self.grid == False
                                    or self.ghost_grid == False):
            # read grid only if param is not False
            try:
                print('~ Reading grid.. ')
                self.grid = grid(datadir=self.datadir, trim=True, quiet=True)
                print('~ Reading ghost_grid.. ')
                self.ghost_grid = grid(datadir=self.datadir,
                                       trim=False,
                                       quiet=True)
                print('~ Reading dim.. ')
                self.dim = dim(datadir=self.datadir)
                if not quiet:
                    print('# Updating grid and ghost_grid succesfull')
                REEXPORT = True
                # adding lx, dx etc to params
                self.param['Lx'] = self.grid.Lx
                self.param['Ly'] = self.grid.Ly
                self.param['Lz'] = self.grid.Lz
                self.param['lx'] = self.grid.Lx
                self.param['ly'] = self.grid.Ly
                self.param['lz'] = self.grid.Lz
                self.param['dx'] = self.grid.dx
                self.param['dy'] = self.grid.dy
                self.param['dz'] = self.grid.dz
            except:
                if not quiet:
                    print(
                        '? WARNING: Updating grid and ghost_grid ' +
                        'was not successfull, since run has not yet started.')
                if self.started() or (not quiet):
                    print('? WARNING: Couldnt load grid for ' + self.path)
                self.grid = False
                self.ghost_grid = False
                self.dim = False
                REEXPORT = True
        elif self.param == False:
            if not quiet:
                print('? WARNING: Updating grid and ghost_grid ' +
                      'was not successfull, since run has not yet started.')
            self.grid = False
            self.ghost_grid = False
            self.dim = False
            REEXPORT = True

        if REEXPORT == True: self.export()
        return self
def kernel_smooth(
        sim_path,
        src,
        dst,
        magic=["meanuu"],
        par=[],
        comm=None,
        gd=[],
        grp_overwrite=False,
        overwrite=False,
        rank=0,
        size=1,
        nghost=3,
        kernel=1.,
        status="a",
        chunksize=1000.0,
        dtype=np.float64,
        quiet=True,
        nmin=32,
        typ='piecewise',
        mode=list(),
):

    if comm:
        overwrite = False
    if isinstance(par, list):
        os.chdir(sim_path)
        par = read.param(quiet=True, conflicts_quiet=True)
    if isinstance(gd, list):
        os.chdir(sim_path)
        gd = read.grid(quiet=True)
    # get data dimensions
    nx, ny, nz = (
        src["settings"]["nx"][0],
        src["settings"]["ny"][0],
        src["settings"]["nz"][0],
    )
    mx, my, mz = (
        src["settings"]["mx"][0],
        src["settings"]["my"][0],
        src["settings"]["mz"][0],
    )
    # extend gost zones to include up to 1.5 * kernel length)
    dx = max(src['grid/dx'][()], src['grid/dy'][()], src['grid/dz'][()])
    nkernel = np.int(2.5 * kernel / dx)
    sigma = kernel / dx
    print('sigma {:.2f}, kernel {:.2f}, dx {:.2f}'.format(sigma, kernel, dx))
    # split data into manageable memory chunks
    dstchunksize = 8 * nx * ny * nz / 1024 * 1024
    if dstchunksize > chunksize:
        nchunks = cpu_optimal(
            nx,
            ny,
            nz,
            quiet=quiet,
            mvar=src["settings/mvar"][0],
            maux=src["settings/maux"][0],
            MBmin=chunksize,
            nmin=nmin,
            size=size,
        )[1]
    else:
        nchunks = [1, 1, 1]
    print("nchunks {}".format(nchunks))
    # for mpi split chunks across processes
    if size > 1:
        locindx = np.array_split(np.arange(nx) + nghost, nchunks[0])
        locindy = np.array_split(np.arange(ny) + nghost, nchunks[1])
        locindz = np.array_split(np.arange(nz) + nghost, nchunks[2])
        indx = [
            locindx[np.mod(
                rank + int(rank / nchunks[2]) + int(rank / nchunks[1]),
                nchunks[0])]
        ]
        indy = [locindy[np.mod(rank + int(rank / nchunks[2]), nchunks[1])]]
        indz = [locindz[np.mod(rank, nchunks[2])]]
        allchunks = 1
    else:
        locindx = np.array_split(np.arange(nx) + nghost, nchunks[0])
        locindy = np.array_split(np.arange(ny) + nghost, nchunks[1])
        locindz = np.array_split(np.arange(nz) + nghost, nchunks[2])
        indx = np.array_split(np.arange(nx) + nghost, nchunks[0])
        indy = np.array_split(np.arange(ny) + nghost, nchunks[1])
        indz = np.array_split(np.arange(nz) + nghost, nchunks[2])
        allchunks = nchunks[0] * nchunks[1] * nchunks[2]
    if 1 in nchunks:
        mode = ["reflect", "reflect", "reflect"]
        for ich in range(3):
            if nchunks[ich] == 1:
                mode[2 - ich] = "wrap"
            if mode[2 - ich] == "reflect":
                typ = "piecewise"
            else:
                typ = "all"
    print('mode:', mode, 'typ:', typ)
    # save time
    dataset_h5(
        dst,
        "time",
        status=status,
        data=src["time"][()],
        comm=comm,
        size=size,
        rank=rank,
        overwrite=overwrite,
        dtype=dtype,
    )
    # ensure derived variables are in a list
    if isinstance(magic, list):
        magic = magic
    else:
        magic = [magic]
    # initialise group
    group = group_h5(
        dst,
        "data",
        status="a",
        overwrite=grp_overwrite,
        comm=comm,
        rank=rank,
        size=size,
    )
    for key in magic:
        if is_vector(key):
            dataset_h5(
                group,
                key + str(nkernel),
                status=status,
                shape=[3, mz, my, mx],
                comm=comm,
                size=size,
                rank=rank,
                overwrite=overwrite,
                dtype=dtype,
            )
            print("writing " + key + " shape {}".format([3, mz, my, mx]))
        else:
            dataset_h5(
                group,
                key + str(nkernel),
                status=status,
                shape=[mz, my, mx],
                comm=comm,
                size=size,
                rank=rank,
                overwrite=overwrite,
                dtype=dtype,
            )
            print("writing " + key + " shape {}".format([mz, my, mx]))
        for ichunk in range(allchunks):
            for iz in [indz[np.mod(ichunk, nchunks[2])]]:
                if nchunks[2] == 1:
                    zextra = nghost
                else:
                    zextra = nkernel + nghost
                n1, n2 = iz[0] - zextra, iz[-1] + zextra + 1
                lindz = np.arange(n1, n2)
                n1out = n1 + zextra
                n2out = n2 - zextra
                varn1 = zextra
                varn2 = -zextra
                if iz[0] == locindz[0][0]:
                    n1out = 0
                    varn1 = zextra - nghost
                if iz[-1] == locindz[-1][-1]:
                    n2out = n2 - zextra + nghost
                    varn2 = n2 - n1 - zextra + nghost
                if n1 < 0:
                    lindz[np.where(lindz < nghost)[0]] += nz
                if n2 > mz - 1:
                    lindz[np.where(lindz > mz - 1 - nghost)[0]] -= nz
                print('n1out {},n2out {},varn1 {},varn2 {},zextra {}'.format(
                    n1out, n2out, varn1, varn2, zextra))
                for iy in [
                        indy[np.mod(ichunk + int(ichunk / nchunks[2]),
                                    nchunks[1])]
                ]:
                    if nchunks[1] == 1:
                        yextra = nghost
                    else:
                        yextra = nkernel + nghost
                    m1, m2 = iy[0] - yextra, iy[-1] + yextra + 1
                    lindy = np.arange(m1, m2)
                    m1out = m1 + yextra
                    m2out = m2 + 1 - yextra
                    varm1 = yextra
                    varm2 = -yextra
                    if iy[0] == locindy[0][0]:
                        m1out = 0
                        varm1 = yextra - nghost
                    if iy[-1] == locindy[-1][-1]:
                        m2out = m2 - yextra + nghost
                        varm2 = m2 - m1 - yextra + nghost
                    if m1 < 0:
                        lindy[np.where(lindy < 0)[0]] += ny
                    if m2 > my - 1:
                        lindy[np.where(lindy > my - 1)[0]] -= ny
                    print(
                        'm1out {},m2out {},varm1 {},varm2 {},yextra {}'.format(
                            m1out, m2out, varm1, varm2, yextra))
                for iy in [
                        indy[np.mod(ichunk + int(ichunk / nchunks[2]),
                                    nchunks[1])]
                ]:
                    for ix in [
                            indx[np.mod(
                                ichunk + int(ichunk / nchunks[2]) +
                                int(ichunk / nchunks[1]),
                                nchunks[0],
                            )]
                    ]:
                        if nchunks[1] == 1:
                            xextra = nghost
                        else:
                            xextra = nkernel + nghost
                        l1, l2 = ix[0] - xextra, ix[-1] + xextra + 1
                        lindx = np.arange(l1, l2)
                        l1out = l1 + xextra
                        l2out = l2 + 1 - xextra
                        varl1 = xextra
                        varl2 = -xextra
                        if ix[0] == locindx[0][0]:
                            l1out = 0
                            varl1 = xextra - nghost
                        if ix[-1] == locindx[-1][-1]:
                            l2out = l2 - xextra + nghost
                            varl2 = l2 - l1 - xextra + nghost
                        if l1 < 0:
                            lindx[np.where(lindx < 0)[0]] += nx
                        if l2 > mx - 1:
                            lindx[np.where(lindx > mx - 1)[0]] -= nx
                        print('l1out {},l2out {},varl1 {},varl2 {},xextra {}'.
                              format(l1out, l2out, varl1, varl2, xextra))
                        if not quiet:
                            print("remeshing " + key +
                                  " chunk {}".format([iz, iy, ix]))
                        print('sending ichunk {} with index ranges {}'.format(
                            ichunk, [n1, n2, m1, m2, l1, l2]))
                        var = smoothed_data(src["data"], dst["data"], key, par,
                                            gd, lindx, lindy, lindz, nghost,
                                            sigma, typ, mode)
                        print(
                            'ichunk {}, var min {:.1e}, var max {:.1e}'.format(
                                ichunk, var.min(), var.max()))
                        # print('var shape {}'.format(var.shape))
                        # if not quiet:
                        #    print('writing '+key+
                        #                   ' shape {} chunk {}'.format(
                        #                         var.shape, [iz,iy,ix]))
                        print('ichunk: out indices {}'.format(
                            [n1out, n2out, m1out, m2out, l1out, l2out]))
                        if is_vector(key):
                            dst["data"][key +
                                        str(nkernel)][:, n1out:n2out,
                                                      m1out:m2out,
                                                      l1out:l2out] = dtype(
                                                          var[:, varn1:varn2,
                                                              varm1:varm2,
                                                              varl1:varl2])
                        else:
                            dst["data"][key +
                                        str(nkernel)][n1out:n2out, m1out:m2out,
                                                      l1out:l2out] = dtype(
                                                          var[varn1:varn2,
                                                              varm1:varm2,
                                                              varl1:varl2])
示例#7
0
def rhs_data(sim_path, src, dst, magic=["uxb","etadel2a"], par=[], comm=None,
                gd=[], grp_overwrite=False, overwrite=False, 
                rank=0, size=1, nghost=3,status="a",
                chunksize = 1000.0, dtype=np.float64, quiet=True, nmin=32,
                Reynolds_shock=False, lmix=False
               ):

    if comm:
        overwrite = False
    if isinstance(par, list):
        os.chdir(sim_path)
        par = read.param(quiet=True,conflicts_quiet=True)
    if isinstance(gd, list):
        os.chdir(sim_path)
        gd = read.grid(quiet=True)
    #get data dimensions
    nx, ny, nz = src["settings"]["nx"][0],\
                 src["settings"]["ny"][0],\
                 src["settings"]["nz"][0]
    mx, my, mz = src["settings"]["mx"][0],\
                 src["settings"]["my"][0],\
                 src["settings"]["mz"][0]
    #split data into manageable memory chunks
    dstchunksize = 8*nx*ny*nz/1024*1024
    if dstchunksize > chunksize:
        nchunks = cpu_optimal(nx,ny,nz,quiet=quiet,
                              mvar=src["settings/mvar"][0],
                              maux=src["settings/maux"][0],
                              MBmin=chunksize,nmin=nmin,size=size)[1]
    else:
        nchunks = [1,1,1]
    print("nchunks {}".format(nchunks)) 
    # for mpi split chunks across processes
    if size > 1:
        locindx = np.array_split(np.arange(nx)+nghost,nchunks[0])
        locindy = np.array_split(np.arange(ny)+nghost,nchunks[1])
        locindz = np.array_split(np.arange(nz)+nghost,nchunks[2])
        indx = [locindx[np.mod(rank+int(rank/nchunks[2])
                                   +int(rank/nchunks[1]),nchunks[0])]]
        indy = [locindy[np.mod(rank+int(rank/nchunks[2]),nchunks[1])]]
        indz = [locindz[np.mod(rank,nchunks[2])]]
        allchunks = 1
    else:
        locindx = np.array_split(np.arange(nx)+nghost,nchunks[0])
        locindy = np.array_split(np.arange(ny)+nghost,nchunks[1])
        locindz = np.array_split(np.arange(nz)+nghost,nchunks[2])
        indx = np.array_split(np.arange(nx)+nghost,nchunks[0])
        indy = np.array_split(np.arange(ny)+nghost,nchunks[1])
        indz = np.array_split(np.arange(nz)+nghost,nchunks[2])
        allchunks = nchunks[0]*nchunks[1]*nchunks[2]
    # save time
    dataset_h5(dst, "time", status=status, data=src["time"][()],
                          comm=comm, size=size, rank=rank,
                          overwrite=overwrite, dtype=dtype)
    # ensure derived variables are in a list
    if isinstance(magic, list):
        magic = magic
    else:
        magic = [magic]
    # confirm exists group
    group_h5(dst, "data", status="a", overwrite=grp_overwrite,
                     comm=comm, rank=rank, size=size)
    # initialise group
    group = group_h5(dst, "calc", status="a", overwrite=grp_overwrite,
                     comm=comm, rank=rank, size=size)
    for key in magic:
        if is_vector(key):
            dataset_h5(group, key, status=status, shape=[3,mz,my,mx],
                          comm=comm, size=size, rank=rank,
                          overwrite=overwrite, dtype=dtype)
            print("writing "+key+" shape {}".format([3,mz,my,mx]))
        else:
            dataset_h5(group, key, status=status, shape=[mz,my,mx],
                          comm=comm, size=size, rank=rank,
                          overwrite=overwrite, dtype=dtype)
            print("writing "+key+" shape {}".format([mz,my,mx]))
        for ichunk in range(allchunks):
            for iz in [indz[np.mod(ichunk,nchunks[2])]]:
                n1, n2 = iz[ 0]-nghost,\
                         iz[-1]+nghost+1
                n1out = n1+nghost
                n2out = n2-nghost
                varn1 =  nghost
                varn2 = -nghost
                if iz[0] == locindz[0][0]:
                    n1out = 0
                    varn1 = 0
                if iz[-1] == locindz[-1][-1]:
                    n2out = n2
                    varn2 = n2
                for iy in [indy[np.mod(ichunk+
                                   int(ichunk/nchunks[2]),nchunks[1])]]:
                    m1, m2 = iy[ 0]-nghost,\
                             iy[-1]+nghost+1
                    m1out = m1+nghost
                    m2out = m2-nghost
                    varm1 =  nghost
                    varm2 = -nghost
                    if iy[0] == locindy[0][0]:
                        m1out = 0
                        varm1 = 0
                    if iy[-1] == locindy[-1][-1]:
                        m2out = m2
                        varm2 = m2
                    for ix in [indx[np.mod(ichunk+int(ichunk/nchunks[2])
                                   +int(ichunk/nchunks[1]),nchunks[0])]]:
                        l1, l2 = ix[ 0]-nghost,\
                                 ix[-1]+nghost+1
                        l1out = l1+nghost
                        l2out = l2-nghost
                        varl1 =  nghost
                        varl2 = -nghost
                        if ix[0] == locindx[0][0]:
                            l1out = 0
                            varl1 = 0
                        if ix[-1] == locindx[-1][-1]:
                            l2out = l2
                            varl2 = l2
                        if not quiet:
                            print("remeshing "+key+" chunk {}".format(
                                   [iz,iy,ix]))
                        var = calc_rhs_data(src, dst,
                              key, par, gd, l1, l2, m1, m2, n1, n2,
                              nghost=nghost, Reynolds_shock=Reynolds_shock,
                              lmix=lmix)
                        if is_vector(key):
                            dst["calc"][key][:,n1out:n2out,
                                                m1out:m2out,
                                                l1out:l2out] = dtype(var[:,
                                                         varn1:varn2,
                                                         varm1:varm2,
                                                         varl1:varl2])
                        else:
                            dst["calc"][key][n1out:n2out,
                                             m1out:m2out,
                                             l1out:l2out] = dtype(var[
                                                         varn1:varn2,
                                                         varm1:varm2,
                                                         varl1:varl2])
示例#8
0
def write_h5_snapshot(
    snapshot,
    file_name="VAR0",
    datadir="data/allprocs",
    precision="d",
    nghost=3,
    persist=None,
    settings=None,
    param=None,
    grid=None,
    lghosts=False,
    indx=None,
    proc=None,
    ipx=None,
    ipy=None,
    ipz=None,
    procdim=None,
    unit=None,
    t=None,
    x=None,
    y=None,
    z=None,
    state="a",
    quiet=True,
    lshear=False,
    driver=None,
    comm=None,
    overwrite=False,
    rank=0,
    size=1,
):
    """
    Write a snapshot given as numpy array.
    We assume by default that a run simulation directory has already been
    constructed and start completed successfully in h5 format so that
    files dim, grid and param files are already present.
    If not the contents of these will need to be supplied as dictionaries
    along with persist if included.

    call signature:

    write_h5_snapshot(snapshot, file_name='VAR0', datadir='data/allprocs',
                   precision='d', nghost=3, persist=None, settings=None,
                   param=None, grid=None, lghosts=False, indx=None,
                   unit=None, t=None, x=None, y=None, z=None, procdim=None,
                   quiet=True, lshear=False, driver=None, comm=None)

    Keyword arguments:

    *snapshot*:
      Numpy array containing the snapshot.
      Must be of shape [nvar, nz, ny, nx] without boundaries or.
      Must be of shape [nvar, mz, my, mx] with boundaries for lghosts=True.

    *file_name*:
      Name of the snapshot file to be written, e.g. VAR0 or var.

    *datadir*:
      Directory where the data is stored.

    *precision*:
      Single 'f' or double 'd' precision.

    *persist*:
      optional dictionary of persistent variable.

    *settings*:
      optional dictionary of persistent variable.

    *param*:
      optional Param object.

    *grid*:
      optional Pencil Grid object of grid parameters.

    *nghost*:
      Number of ghost zones.

    *lghosts*:
      If True the snapshot contains the ghost zones.

    *indx*
      Index object of index for each variable in f-array

    *unit*:
      Optional dictionary of simulation units.

    *quiet*:
      Option to print output.

    *t*:
      Time of the snapshot.

    *xyz*:
      xyz arrays of the domain with ghost zones.
      This will normally be obtained from Grid object, but facility to
      redefine an alternative grid value.

    *lshear*:
      Flag for the shear.

    *driver*
      File driver for hdf5 io for use in serial or MPI parallel.

    *comm*
      MPI objects supplied if driver is 'mpio'.

    *overwrite*
      flag to replace existing h5 snapshot file.

    *rank*
      rank of process with root=0.
    """

    import numpy as np
    from os.path import join

    from pencil import read
    from pencil.io import open_h5, group_h5, dataset_h5
    from pencil import is_sim_dir

    # test if simulation directory
    if not is_sim_dir():
        print("ERROR: Directory needs to be a simulation")
        sys.stdout.flush()
    if indx == None:
        indx = read.index()
    #
    if settings == None:
        settings = {}
        skeys = [
            "l1",
            "l2",
            "m1",
            "m2",
            "n1",
            "n2",
            "nx",
            "ny",
            "nz",
            "mx",
            "my",
            "mz",
            "nprocx",
            "nprocy",
            "nprocz",
            "maux",
            "mglobal",
            "mvar",
            "precision",
        ]
        dim = read.dim()
        for key in skeys:
            settings[key] = dim.__getattribute__(key)
        settings["precision"] = precision.encode()
        settings["nghost"] = nghost
        settings["version"] = np.int32(0)
    nprocs = settings["nprocx"] * settings["nprocy"] * settings["nprocz"]
    gkeys = [
        "x",
        "y",
        "z",
        "Lx",
        "Ly",
        "Lz",
        "dx",
        "dy",
        "dz",
        "dx_1",
        "dy_1",
        "dz_1",
        "dx_tilde",
        "dy_tilde",
        "dz_tilde",
    ]
    if grid == None:
        grid = read.grid(quiet=True)
    else:
        gd_err = False
        for key in gkeys:
            if not key in grid.__dict__.keys():
                print("ERROR: key " + key + " missing from grid")
                sys.stdout.flush()
                gd_err = True
        if gd_err:
            print("ERROR: grid incomplete")
            sys.stdout.flush()
    ukeys = [
        "length",
        "velocity",
        "density",
        "magnetic",
        "time",
        "temperature",
        "flux",
        "energy",
        "mass",
        "system",
    ]
    if param == None:
        param = read.param(quiet=True)
        param.__setattr__("unit_mass",
                          param.unit_density * param.unit_length**3)
        param.__setattr__("unit_energy",
                          param.unit_mass * param.unit_velocity**2)
        param.__setattr__("unit_time", param.unit_length / param.unit_velocity)
        param.__setattr__("unit_flux", param.unit_mass / param.unit_time**3)
        param.unit_system = param.unit_system.encode()

    # check whether the snapshot matches the simulation shape
    if lghosts:
        try:
            snapshot.shape[0] == settings["mvar"]
            snapshot.shape[1] == settings["mx"]
            snapshot.shape[2] == settings["my"]
            snapshot.shape[3] == settings["mz"]
        except ValueError:
            print("ERROR: snapshot shape {} ".format(snapshot.shape) +
                  "does not match simulation dimensions with ghosts.")
            sys.stdout.flush()
    else:
        try:
            snapshot.shape[0] == settings["mvar"]
            snapshot.shape[1] == settings["nx"]
            snapshot.shape[2] == settings["ny"]
            snapshot.shape[3] == settings["nz"]
        except ValueError:
            print("ERROR: snapshot shape {} ".format(snapshot.shape) +
                  "does not match simulation dimensions without ghosts.")
            sys.stdout.flush()

    # Determine the precision used and ensure snapshot has correct data_type.
    if precision == "f":
        data_type = np.float32
        snapshot = np.float32(snapshot)
    elif precision == "d":
        data_type = np.float64
        snapshot = np.float64(snapshot)
    else:
        print("ERROR: Precision {0} not understood.".format(precision) +
              " Must be either 'f' or 'd'")
        sys.stdout.flush()
        return -1

    # Check that the shape does not conflict with the proc numbers.
    if ((settings["nx"] % settings["nprocx"] > 0)
            or (settings["ny"] % settings["nprocy"] > 0)
            or (settings["nz"] % settings["nprocz"] > 0)):
        print("ERROR: Shape of the input array is not compatible with the " +
              "cpu layout. Make sure that nproci devides ni.")
        sys.stdout.flush()
        return -1

    # Check the shape of the xyz arrays if specified and overwrite grid values.
    if x != None:
        if len(x) != settings["mx"]:
            print("ERROR: x array is incompatible with the shape of snapshot.")
            sys.stdout.flush()
            return -1
        grid.x = data_type(x)
    if y != None:
        if len(y) != settings["my"]:
            print("ERROR: y array is incompatible with the shape of snapshot.")
            sys.stdout.flush()
            return -1
        grid.y = data_type(y)
    if z != None:
        if len(z) != settings["mz"]:
            print("ERROR: z array is incompatible with the shape of snapshot.")
            sys.stdout.flush()
            return -1
        grid.z = data_type(z)

    # Define a time.
    if t is None:
        t = data_type(0.0)

    # making use of pc_hdf5 functionality:
    if not proc == None:
        state = "a"
    else:
        state = "w"
    filename = join(datadir, file_name)
    print("write_h5_snapshot: filename =", filename)
    with open_h5(
            filename,
            state,
            driver=driver,
            comm=comm,
            overwrite=overwrite,
            rank=rank,
            size=size,
    ) as ds:
        data_grp = group_h5(
            ds,
            "data",
            status=state,
            delete=False,
            overwrite=overwrite,
            rank=rank,
            size=size,
        )
        if not procdim:
            for key in indx.__dict__.keys():
                if key in ["uu", "keys", "aa", "KR_Frad", "uun", "gg", "bb"]:
                    continue
                #create ghost zones if required
                if not lghosts:
                    tmp_arr = np.zeros([
                        snapshot.shape[1] + 2 * nghost,
                        snapshot.shape[2] + 2 * nghost,
                        snapshot.shape[3] + 2 * nghost,
                    ])
                    tmp_arr[dim.n1:dim.n2 + 1, dim.m1:dim.m2 + 1,
                            dim.l1:dim.l2 + 1] = np.array(
                                snapshot[indx.__getattribute__(key) - 1])
                    dataset_h5(
                        data_grp,
                        key,
                        status=state,
                        data=tmp_arr,
                        dtype=data_type,
                        overwrite=overwrite,
                        rank=rank,
                        comm=comm,
                        size=size,
                    )
                else:
                    dataset_h5(
                        data_grp,
                        key,
                        status=state,
                        data=np.array(snapshot[indx.__getattribute__(key) -
                                               1]),
                        dtype=data_type,
                        overwrite=overwrite,
                        rank=rank,
                        comm=comm,
                        size=size,
                    )
        else:
            for key in indx.__dict__.keys():
                if key in ["uu", "keys", "aa", "KR_Frad", "uun", "gg", "bb"]:
                    continue
                dataset_h5(
                    data_grp,
                    key,
                    status=state,
                    shape=(settings["mz"], settings["my"], settings["mx"]),
                    dtype=data_type,
                    rank=rank,
                    comm=comm,
                    size=size,
                )
            # adjust indices to include ghost zones at boundaries
            l1, m1, n1 = procdim.l1, procdim.m1, procdim.n1
            if procdim.ipx == 0:
                l1 = 0
            if procdim.ipy == 0:
                m1 = 0
            if procdim.ipz == 0:
                n1 = 0
            l2, m2, n2 = procdim.l2, procdim.m2, procdim.n2
            if procdim.ipx == settings["nprocx"] - 1:
                l2 = procdim.l2 + settings["nghost"]
            if procdim.ipy == settings["nprocy"] - 1:
                m2 = procdim.m2 + settings["nghost"]
            if procdim.ipz == settings["nprocz"] - 1:
                n2 = procdim.n2 + settings["nghost"]
            nx, ny, nz = procdim.nx, procdim.ny, procdim.nz
            ipx, ipy, ipz = procdim.ipx, procdim.ipy, procdim.ipz
            for key in indx.__dict__.keys():
                if key in ["uu", "keys", "aa", "KR_Frad", "uun", "gg", "bb"]:
                    continue
                tmp_arr = np.array(snapshot[indx.__getattribute__(key) - 1])
                data_grp[key][n1 + ipz * nz:n2 + ipz * nz + 1,
                              m1 + ipy * ny:m2 + ipy * ny + 1,
                              l1 + ipx * nx:l2 + ipx * nx +
                              1, ] = tmp_arr[n1:n2 + 1, m1:m2 + 1, l1:l2 + 1]
        dataset_h5(
            ds,
            "time",
            status=state,
            data=np.array(t),
            size=size,
            dtype=data_type,
            rank=rank,
            comm=comm,
            overwrite=overwrite,
        )
        # add settings
        sets_grp = group_h5(
            ds,
            "settings",
            status=state,
            delete=False,
            overwrite=overwrite,
            rank=rank,
            size=size,
        )
        for key in settings.keys():
            if "precision" in key:
                dataset_h5(
                    sets_grp,
                    key,
                    status=state,
                    data=(settings[key], ),
                    dtype=None,
                    rank=rank,
                    comm=comm,
                    size=size,
                    overwrite=overwrite,
                )
            else:
                dataset_h5(
                    sets_grp,
                    key,
                    status=state,
                    data=(settings[key], ),
                    dtype=data_type,
                    rank=rank,
                    comm=comm,
                    size=size,
                    overwrite=overwrite,
                )
        # add grid
        grid_grp = group_h5(
            ds,
            "grid",
            status=state,
            delete=False,
            overwrite=overwrite,
            rank=rank,
            size=size,
        )
        for key in gkeys:
            dataset_h5(
                grid_grp,
                key,
                status=state,
                data=(grid.__getattribute__(key)),
                dtype=data_type,
                rank=rank,
                comm=comm,
                size=size,
                overwrite=overwrite,
            )
        dataset_h5(
            grid_grp,
            "Ox",
            status=state,
            data=(param.__getattribute__("xyz0")[0], ),
            dtype=data_type,
            rank=rank,
            comm=comm,
            size=size,
            overwrite=overwrite,
        )
        dataset_h5(
            grid_grp,
            "Oy",
            status=state,
            data=(param.__getattribute__("xyz0")[1], ),
            dtype=data_type,
            rank=rank,
            comm=comm,
            size=size,
            overwrite=overwrite,
        )
        dataset_h5(
            grid_grp,
            "Oz",
            status=state,
            data=(param.__getattribute__("xyz0")[2], ),
            dtype=data_type,
            rank=rank,
            comm=comm,
            size=size,
            overwrite=overwrite,
        )
        # add physical units
        unit_grp = group_h5(
            ds,
            "unit",
            status=state,
            delete=False,
            overwrite=overwrite,
            rank=rank,
            size=size,
        )
        for key in ukeys:
            if "system" in key:
                dataset_h5(
                    unit_grp,
                    key,
                    status=state,
                    data=(param.__getattribute__("unit_" + key), ),
                    rank=rank,
                    comm=comm,
                    size=size,
                    overwrite=overwrite,
                )
            else:
                dataset_h5(
                    unit_grp,
                    key,
                    status=state,
                    data=param.__getattribute__("unit_" + key),
                    rank=rank,
                    comm=comm,
                    size=size,
                    overwrite=overwrite,
                )
        # add optional persistent data
        if persist != None:
            pers_grp = group_h5(
                ds,
                "persist",
                status=state,
                size=size,
                delete=False,
                overwrite=overwrite,
                rank=rank,
            )
            for key in persist.keys():
                if not quiet:
                    print(key, type(persist[key][()]))
                    sys.stdout.flush()
                arr = np.empty(nprocs, dtype=type(persist[key][()]))
                arr[:] = persist[key][()]
                dataset_h5(
                    pers_grp,
                    key,
                    status=state,
                    data=(arr),
                    size=size,
                    dtype=data_type,
                    rank=rank,
                    comm=comm,
                    overwrite=overwrite,
                )
示例#9
0
def write_h5_grid(
    file_name="grid",
    datadir="data",
    precision="d",
    nghost=3,
    settings=None,
    param=None,
    grid=None,
    unit=None,
    quiet=True,
    driver=None,
    comm=None,
    overwrite=False,
    rank=0,
):
    """
    Write the grid information as hdf5.
    We assume by default that a run simulation directory has already been
    constructed, but start has not been executed in h5 format so that
    binary sim files dim, grid and param files are already present in the sim
    directory, or provided from an old binary sim source directory as inputs.

    call signature:

    write_h5_grid(file_name='grid', datadir='data', precision='d', nghost=3,
                  settings=None, param=None, grid=None, unit=None, quiet=True,
                  driver=None, comm=None)

    Keyword arguments:

    *file_name*:
      Prefix of the file name to be written, 'grid'.

    *datadir*:
      Directory where 'grid.h5' is stored.

    *precision*:
      Single 'f' or double 'd' precision.

    *nghost*:
      Number of ghost zones.

    *settings*:
      Optional dictionary of persistent variable.

    *param*:
      Optional Param object.

    *grid*:
      Optional Pencil Grid object of grid parameters.

    *unit*:
      Optional dictionary of simulation units.

    *quiet*:
      Option to print output.
    """

    from os.path import join
    import numpy as np

    from pencil import read
    from pencil.io import open_h5, group_h5, dataset_h5
    from pencil import is_sim_dir

    # test if simulation directory
    if not is_sim_dir():
        print("ERROR: Directory needs to be a simulation")
        sys.stdout.flush()
    #
    if settings == None:
        settings = {}
        skeys = [
            "l1",
            "l2",
            "m1",
            "m2",
            "n1",
            "n2",
            "nx",
            "ny",
            "nz",
            "mx",
            "my",
            "mz",
            "nprocx",
            "nprocy",
            "nprocz",
            "maux",
            "mglobal",
            "mvar",
            "precision",
        ]
        dim = read.dim()
        for key in skeys:
            settings[key] = dim.__getattribute__(key)
        settings["precision"] = precision.encode()
        settings["nghost"] = nghost
        settings["version"] = np.int32(0)
    gkeys = [
        "x",
        "y",
        "z",
        "Lx",
        "Ly",
        "Lz",
        "dx",
        "dy",
        "dz",
        "dx_1",
        "dy_1",
        "dz_1",
        "dx_tilde",
        "dy_tilde",
        "dz_tilde",
    ]
    if grid == None:
        grid = read.grid(quiet=True)
    else:
        gd_err = False
        for key in gkeys:
            if not key in grid.__dict__.keys():
                print("ERROR: key " + key + " missing from grid")
                sys.stdout.flush()
                gd_err = True
        if gd_err:
            print("ERROR: grid incomplete")
            sys.stdout.flush()
    ukeys = [
        "length",
        "velocity",
        "density",
        "magnetic",
        "time",
        "temperature",
        "flux",
        "energy",
        "mass",
        "system",
    ]
    if param == None:
        param = read.param(quiet=True)
        param.__setattr__("unit_mass",
                          param.unit_density * param.unit_length**3)
        param.__setattr__("unit_energy",
                          param.unit_mass * param.unit_velocity**2)
        param.__setattr__("unit_time", param.unit_length / param.unit_velocity)
        param.__setattr__("unit_flux", param.unit_mass / param.unit_time**3)
        param.unit_system = param.unit_system.encode()

    # open file for writing data
    filename = join(datadir, file_name + ".h5")
    with open_h5(filename,
                 "w",
                 driver=driver,
                 comm=comm,
                 overwrite=overwrite,
                 rank=rank) as ds:
        # add settings
        sets_grp = group_h5(ds, "settings", status="w")
        for key in settings.keys():
            if "precision" in key:
                dataset_h5(sets_grp, key, status="w", data=(settings[key], ))
            else:
                dataset_h5(sets_grp, key, status="w", data=(settings[key], ))
        # add grid
        grid_grp = group_h5(ds, "grid", status="w")
        for key in gkeys:
            dataset_h5(grid_grp,
                       key,
                       status="w",
                       data=(grid.__getattribute__(key)))
        dataset_h5(grid_grp,
                   "Ox",
                   status="w",
                   data=(param.__getattribute__("xyz0")[0], ))
        dataset_h5(grid_grp,
                   "Oy",
                   status="w",
                   data=(param.__getattribute__("xyz0")[1], ))
        dataset_h5(grid_grp,
                   "Oz",
                   status="w",
                   data=(param.__getattribute__("xyz0")[2], ))
        # add physical units
        unit_grp = group_h5(ds, "unit", status="w")
        for key in ukeys:
            if "system" in key:
                dataset_h5(
                    unit_grp,
                    key,
                    status="w",
                    data=(param.__getattribute__("unit_" + key), ),
                )
            else:
                dataset_h5(
                    unit_grp,
                    key,
                    status="w",
                    data=param.__getattribute__("unit_" + key),
                )
示例#10
0
def sim2h5(
    newdir=".",
    olddir=".",
    varfile_names=None,
    todatadir="data/allprocs",
    fromdatadir="data",
    precision="d",
    nghost=3,
    lpersist=True,
    x=None,
    y=None,
    z=None,
    lshear=False,
    snap_by_proc=False,
    aver_by_proc=False,
    lremove_old_snapshots=False,
    lremove_old_slices=False,
    lread_all_videoslices=False,
    vlarge=100000000,
    lremove_old_averages=False,
    execute=False,
    quiet=True,
    l2D=True,
    lvars=True,
    lvids=True,
    laver=True,
    laver2D=False,
    lremove_deprecated_vids=False,
    lsplit_slices=False,
):
    """
    Copy a simulation object written in Fortran binary to hdf5.
    The default is to copy all snapshots from/to the current simulation
    directory. Optionally the old files can be removed to

    call signature:

    sim2h5(newdir='.', olddir='.', varfile_names=None,
           todatadir='data/allprocs', fromdatadir='data',
           precision='d', nghost=3, lpersist=False,
           x=None, y=None, z=None, lshear=False,
           snap_by_proc=False, aver_by_proc=False,
           lremove_old_snapshots=False,
           lremove_old_slices=False, lread_all_videoslices=True,
           lremove_old_averages=False, execute=False, quiet=True,
           l2D=True, lvars=True, lvids=True, laver=True)

    Keyword arguments:

    *olddir*:
      String path to simulation source directory.
      Path may be relative or absolute.

    *newdir*:
      String path to simulation destination directory.
      Path may be relative or absolute.

    *varfile_names*:
      A list of names of the snapshot files to be written, e.g. VAR0
      If None all varfiles in olddir+'/data/proc0/' will be converted

    *todatadir*:
      Directory to which the data is stored.

    *fromdatadir*:
      Directory from which the data is collected.

    *precision*:
      Single 'f' or double 'd' precision for new data.

    *nghost*:
      Number of ghost zones.
      TODO: handle switching size of ghost zones.

    *lpersist*:
      option to include persistent variables from snapshots.

    *xyz*:
      xyz arrays of the domain with ghost zones.
      This will normally be obtained from Grid object, but facility to
      redefine an alternative grid value.

    *lshear*:
      Flag for the shear.

    *execute*:
      optional confirmation required if lremove_old.

    *lremove_old_snapshots*:
      If True the old snapshot data will be deleted once the new h5 data
      has been saved.

    *lremove_old_slices*:
      If True the old video slice data will be deleted once the new h5 data
      has been saved.

    *lremove_old_averages*:
      If True the old averages data will be deleted once the new h5 data
      has been saved.

    *aver_by_proc*
      Option to read old binary files by processor and write in
      parallel

    *laver2D*
      If True apply to each plane_list 'y', 'z' and load each variable
      sequentially

    *l_mpi*:
      Applying MPI parallel process

    *driver*:
      HDF5 file io driver either None or mpio

    *comm*:
      MPI library calls

    *rank*:
      Integer ID of processor

    *size*:
      Number of MPI processes
    """

    import glob
    import numpy as np
    import os
    from os.path import exists, join
    import subprocess as sub
    import sys

    from .. import read
    from .. import sim
    from . import write_h5_grid
    from pencil.util import is_sim_dir

    try:
        from mpi4py import MPI

        comm = MPI.COMM_WORLD
        rank = comm.Get_rank()
        size = comm.Get_size()
        driver = "mpio"
        l_mpi = True
        l_mpi = l_mpi and (size != 1)
    except ImportError:
        comm = None
        driver = None
        rank = 0
        size = 1
        l_mpi = False
    if not l_mpi:
        comm = None
        driver = None
    print("rank {} and size {}".format(rank, size))
    sys.stdout.flush()
    if rank == size - 1:
        print("l_mpi", l_mpi)
        sys.stdout.flush()

    # test if simulation directories
    if newdir == ".":
        newdir = os.getcwd()
    if olddir == ".":
        olddir = os.getcwd()
    os.chdir(olddir)
    if not is_sim_dir():
        if rank == 0:
            print("ERROR: Directory (" + olddir + ") needs to be a simulation")
            sys.stdout.flush()
        return -1
    if newdir != olddir:
        if not exists(newdir):
            cmd = "pc_newrun -s " + newdir
            if rank == size - 1:
                process = sub.Popen(cmd.split(), stdout=sub.PIPE)
                output, error = process.communicate()
                print(cmd, output, error)
                # os.system(cmd)
            if comm:
                comm.Barrier()
        os.chdir(newdir)
        if not is_sim_dir():
            if rank == 0:
                print("ERROR: Directory (" + newdir +
                      ") needs to be a simulation")
                sys.stdout.flush()
            return -1
    #
    lremove_old = lremove_old_snapshots or lremove_old_slices or lremove_old_averages
    if lremove_old:
        if not execute:
            os.chdir(olddir)
            if rank == 0:
                print("WARNING: Are you sure you wish to remove the Fortran" +
                      " binary files from \n" + os.getcwd() + ".\n" +
                      "Set execute=True to proceed.")
                sys.stdout.flush()
            return -1

    os.chdir(olddir)
    if lvars:
        if varfile_names == None:
            os.chdir(fromdatadir + "/proc0")
            lVARd = False
            varfiled_names = natural_sort(glob.glob("VARd*"))
            if len(varfiled_names) > 0:
                varfile_names = natural_sort(glob.glob("VAR*"))
                for iv in range(len(varfile_names) - 1, -1, -1):
                    if "VARd" in varfile_names[iv]:
                        varfile_names.remove(varfile_names[iv])
                lVARd = True
            else:
                varfile_names = natural_sort(glob.glob("VAR*"))
            os.chdir(olddir)
        else:
            lVARd = False
            if isinstance(varfile_names, list):
                varfile_names = varfile_names
            else:
                varfile_names = [varfile_names]
            varfiled_names = []
            tmp_names = []
            for varfile_name in varfile_names:
                if "VARd" in varfile_names:
                    varfiled_names.append(varfile_name)
                    lVARd = True
                else:
                    tmp_names.append(varfile_name)
            varfile_names = tmp_names
    gkeys = [
        "x",
        "y",
        "z",
        "Lx",
        "Ly",
        "Lz",
        "dx",
        "dy",
        "dz",
        "dx_1",
        "dy_1",
        "dz_1",
        "dx_tilde",
        "dy_tilde",
        "dz_tilde",
    ]
    grid = None
    if rank == size - 1:
        grid = read.grid(quiet=True)
    if l_mpi:
        grid = comm.bcast(grid, root=size - 1)
    if not quiet:
        print(rank, grid)
        sys.stdout.flush()
    for key in gkeys:
        if not key in grid.__dict__.keys():
            if rank == 0:
                print("ERROR: key " + key + " missing from grid")
                sys.stdout.flush()
            return -1
    # obtain the settings from the old simulation
    settings = {}
    skeys = [
        "l1",
        "l2",
        "m1",
        "m2",
        "n1",
        "n2",
        "nx",
        "ny",
        "nz",
        "mx",
        "my",
        "mz",
        "nprocx",
        "nprocy",
        "nprocz",
        "maux",
        "mglobal",
        "mvar",
        "precision",
    ]
    if rank == 0:
        olddim = read.dim()
        for key in skeys:
            settings[key] = np.array(olddim.__getattribute__(key))
        olddim = None
        settings["nghost"] = np.array(nghost)
        settings["precision"] = precision.encode()
    if l_mpi:
        settings = comm.bcast(settings, root=0)
    if snap_by_proc:
        nprocs = settings["nprocx"] * settings["nprocy"] * settings["nprocz"]
        if np.mod(nprocs, size) != 0:
            print("WARNING: efficiency requires cpus to divide ncpus")
            sys.stdout.flush()
    if not quiet:
        print(rank, grid)
        sys.stdout.flush()
    # obtain physical units from old simulation
    ukeys = [
        "length",
        "velocity",
        "density",
        "magnetic",
        "time",
        "temperature",
        "flux",
        "energy",
        "mass",
        "system",
    ]
    param = read.param(quiet=True)
    param.__setattr__("unit_mass", param.unit_density * param.unit_length**3)
    param.__setattr__("unit_energy", param.unit_mass * param.unit_velocity**2)
    param.__setattr__("unit_time", param.unit_length / param.unit_velocity)
    param.__setattr__("unit_flux", param.unit_mass / param.unit_time**3)
    param.unit_system = param.unit_system.encode()
    # index list for variables in f-array
    if not quiet:
        print(rank, param)
        sys.stdout.flush()
    indx = None
    if rank == 0:
        indx = read.index()
    if l_mpi:
        indx = comm.bcast(indx, root=0)

    # check consistency between Fortran binary and h5 data
    os.chdir(newdir)
    dim = None
    if is_sim_dir():
        if rank == size - 1:
            if exists(join(newdir, "data", "dim.dat")):
                try:
                    dim = read.dim()
                except ValueError:
                    pass
        if l_mpi:
            dim = comm.bcast(dim, root=size - 1)
        if dim:
            if not quiet:
                print(rank, dim)
                sys.stdout.flush()
            try:
                dim.mvar == settings["mvar"]
                dim.mx == settings["mx"]
                dim.my == settings["my"]
                dim.mz == settings["mz"]
            except ValueError:
                if rank == size - 1:
                    print("ERROR: new simulation dimensions do not match.")
                    sys.stdout.flush()
                return -1
            dim = None
    os.chdir(olddir)
    if rank == size - 1:
        print("precision is ", precision)
        sys.stdout.flush()
    if laver2D:
        aver2h5(
            newdir,
            olddir,
            todatadir="data/averages",
            fromdatadir="data",
            l2D=False,
            precision=precision,
            quiet=quiet,
            laver2D=laver2D,
            lremove_old_averages=False,
            aver_by_proc=aver_by_proc,
            l_mpi=l_mpi,
            driver=driver,
            comm=comm,
            rank=rank,
            size=size,
        )
        l2D = False
    # copy snapshots
    if lvars and len(varfile_names) > 0:
        var2h5(
            newdir,
            olddir,
            varfile_names,
            todatadir,
            fromdatadir,
            snap_by_proc,
            precision,
            lpersist,
            quiet,
            nghost,
            settings,
            param,
            grid,
            x,
            y,
            z,
            lshear,
            lremove_old_snapshots,
            indx,
            l_mpi=l_mpi,
            driver=driver,
            comm=comm,
            rank=rank,
            size=size,
        )
    # copy downsampled snapshots if present
    if lvars and lVARd:
        var2h5(
            newdir,
            olddir,
            varfiled_names,
            todatadir,
            fromdatadir,
            False,
            precision,
            lpersist,
            quiet,
            nghost,
            settings,
            param,
            grid,
            x,
            y,
            z,
            lshear,
            lremove_old_snapshots,
            indx,
            trimall=True,
            l_mpi=l_mpi,
            driver=driver,
            comm=comm,
            rank=rank,
            size=size,
        )
    if lvars:
        var2h5(
            newdir,
            olddir,
            [
                "var.dat",
            ],
            todatadir,
            fromdatadir,
            snap_by_proc,
            precision,
            lpersist,
            quiet,
            nghost,
            settings,
            param,
            grid,
            x,
            y,
            z,
            lshear,
            lremove_old_snapshots,
            indx,
            l_mpi=l_mpi,
            driver=driver,
            comm=comm,
            rank=rank,
            size=size,
        )
    # copy old video slices to new h5 sim
    if lvids:
        if lremove_deprecated_vids:
            for ext in [
                    "bb.", "uu.", "ux.", "uy.", "uz.", "bx.", "by.", "bz."
            ]:
                cmd = "rm -f " + join(olddir, fromdatadir, "proc*",
                                      "slice_" + ext + "*")
                if rank == 0:
                    process = sub.Popen(cmd.split(), stdout=sub.PIPE)
                    output, error = process.communicate()
                    print(cmd, output, error)
                cmd = "rm -f " + join(fromdatadir, "slice_" + ext + "*")
                if rank == 0:
                    process = sub.Popen(cmd.split(), stdout=sub.PIPE)
                    output, error = process.communicate()
                    print(cmd, output, error)
        if comm:
            comm.Barrier()
        cmd = "src/read_all_videofiles.x"
        if rank == size - 1 and lread_all_videoslices:
            process = sub.Popen(cmd.split(), stdout=sub.PIPE)
            output, error = process.communicate()
            print(cmd, output, error)
        if comm:
            comm.Barrier()
        slices2h5(
            newdir,
            olddir,
            grid,
            todatadir="data/slices",
            fromdatadir=fromdatadir,
            precision=precision,
            quiet=quiet,
            vlarge=vlarge,
            lsplit_slices=lsplit_slices,
            lremove_old_slices=lremove_old_slices,
            l_mpi=l_mpi,
            driver=driver,
            comm=comm,
            rank=rank,
            size=size,
        )
    # copy old averages data to new h5 sim
    if laver:
        aver2h5(
            newdir,
            olddir,
            todatadir="data/averages",
            fromdatadir=fromdatadir,
            l2D=l2D,
            precision=precision,
            quiet=quiet,
            aver_by_proc=False,
            lremove_old_averages=lremove_old_averages,
            l_mpi=l_mpi,
            driver=driver,
            comm=comm,
            rank=rank,
            size=size,
        )
    # check some critical sim files are present for new sim without start
    # construct grid.h5 sim information if requied for new h5 sim
    os.chdir(newdir)
    if l_mpi:
        comm.Barrier()
    if rank == 0:
        write_h5_grid(
            file_name="grid",
            datadir="data",
            precision=precision,
            nghost=nghost,
            settings=settings,
            param=param,
            grid=grid,
            unit=None,
            quiet=quiet,
        )
        source_file = join(olddir, fromdatadir, "proc0/varN.list")
        target_file = join(newdir, todatadir, "varN.list")
        if exists(source_file):
            cmd = "cp " + source_file + " " + target_file
            process = sub.Popen(cmd.split(), stdout=sub.PIPE)
            output, error = process.communicate()
            print(cmd, output, error)
        items = [
            "def_var.pro",
            "index.pro",
            "jobid.dat",
            "param.nml",
            "particle_index.pro",
            "pc_constants.pro",
            "pointmass_index.pro",
            "pt_positions.dat",
            "sn_series.dat",
            "svnid.dat",
            "time_series.dat",
            "tsnap.dat",
            "tspec.dat",
            "tvid.dat",
            "t2davg.dat",
            "var.general",
            "variables.pro",
            "varname.dat",
        ]
        for item in items:
            source_file = join(olddir, fromdatadir, item)
            target_file = join(newdir, fromdatadir, item)
            if exists(source_file):
                if not exists(target_file):
                    cmd = "cp " + source_file + " " + target_file
                    process = sub.Popen(cmd.split(), stdout=sub.PIPE)
                    output, error = process.communicate()
                    print(cmd, output, error)
    print("Simulation Fortran to h5 completed on rank {}.".format(rank))
    sys.stdout.flush()
示例#11
0
    def find_tracers(self, var_file='VAR0', datadir='data', trace_field='bb',
                     ti=-1, tf=-1):
        """
        Trace streamlines of the vectofield 'field' from z = z0 to z = z1
        and integrate quantities 'int_q' along the lines. Creates a 2d
        mapping as in 'streamlines.f90'.

        call signature:

        find_tracers(var_file='VAR0', datadir='data', trace_field='bb',
                     ti=-1, tf=-1)

        Keyword arguments:

        *var_file*:
          Varfile to be read.

        *datadir*:
          Directory where the data is stored.

        *trace_field*:
          Vector field used for the streamline tracing.

        *ti*:
          Initial VAR file index for tracer time sequences. Overrides 'var_file'.

        *tf*:
          Final VAR file index for tracer time sequences. Overrides 'var_file'.
        """

        import numpy as np
        import multiprocessing as mp
        from pencil import read
        from pencil import math

        # Write the tracing parameters.
        self.params.trace_field = trace_field
        self.params.datadir = datadir

        # Multi core setup.
        if not(np.isscalar(self.params.n_proc)) or (self.params.n_proc%1 != 0):
            print("error: invalid processor number")
            return -1
        queue = mp.Queue()

        # Read the data.
        magic = []
        if trace_field == 'bb':
            magic.append('bb')
        if trace_field == 'jj':
            magic.append('jj')
        if trace_field == 'vort':
            magic.append('vort')
        if self.params.int_q == 'ee':
            magic.append('bb')
            magic.append('jj')
        dim = read.dim(datadir=datadir)
        self.params.var_file = var_file

        # Check if user wants a tracer time series.
        if (ti%1 == 0) and (tf%1 == 0) and (ti >= 0) and (tf >= ti):
            series = True
            nTimes = tf-ti+1
        else:
            series = False
            nTimes = 1

        # Initialize the arrays.
        self.x0 = np.zeros([int(self.params.trace_sub*dim.nx),
                            int(self.params.trace_sub*dim.ny), nTimes])
        self.y0 = np.zeros([int(self.params.trace_sub*dim.nx),
                            int(self.params.trace_sub*dim.ny), nTimes])
        self.x1 = np.zeros([int(self.params.trace_sub*dim.nx),
                            int(self.params.trace_sub*dim.ny), nTimes])
        self.y1 = np.zeros([int(self.params.trace_sub*dim.nx),
                            int(self.params.trace_sub*dim.ny), nTimes])
        self.z1 = np.zeros([int(self.params.trace_sub*dim.nx),
                            int(self.params.trace_sub*dim.ny), nTimes])
        self.l = np.zeros([int(self.params.trace_sub*dim.nx),
                           int(self.params.trace_sub*dim.ny), nTimes])
        if self.params.int_q == 'curly_A':
            self.curly_A = np.zeros([int(self.params.trace_sub*dim.nx),
                                     int(self.params.trace_sub*dim.ny), nTimes])
        if self.params.int_q == 'ee':
            self.ee = np.zeros([int(self.params.trace_sub*dim.nx),
                                int(self.params.trace_sub*dim.ny), nTimes])
        self.mapping = np.zeros([int(self.params.trace_sub*dim.nx),
                                 int(self.params.trace_sub*dim.ny),
                                 nTimes, 3])
        self.t = np.zeros(nTimes)

        for t_idx in range(ti, tf+1):
            if series:
                var_file = 'VAR' + str(t_idx)

            # Read the data.
            var = read.var(var_file=var_file, datadir=datadir, magic=magic,
                           quiet=True, trimall=True)
            grid = read.grid(datadir=datadir, quiet=True, trim=True)
            param2 = read.param(datadir=datadir, quiet=True)
            self.t[t_idx] = var.t

            # Extract the requested vector trace_field.
            field = getattr(var, trace_field)
            if self.params.int_q == 'curly_A':
                self.aa = var.aa
            if self.params.int_q == 'ee':
                self.ee = var.jj*param2.eta - math.cross(var.uu, var.bb)

            # Get the simulation parameters.
            self.params.dx = var.dx
            self.params.dy = var.dy
            self.params.dz = var.dz
            self.params.Ox = var.x[0]
            self.params.Oy = var.y[0]
            self.params.Oz = var.z[0]
            self.params.Lx = grid.Lx
            self.params.Ly = grid.Ly
            self.params.Lz = grid.Lz
            self.params.nx = dim.nx
            self.params.ny = dim.ny
            self.params.nz = dim.nz

            # Initialize the tracers.
            for ix in range(int(self.params.trace_sub*dim.nx)):
                for iy in range(int(self.params.trace_sub*dim.ny)):
                    self.x0[ix, iy, t_idx] = grid.x[0] + grid.dx/self.params.trace_sub*ix
                    self.x1[ix, iy, t_idx] = self.x0[ix, iy, t_idx].copy()
                    self.y0[ix, iy, t_idx] = grid.y[0] + grid.dy/self.params.trace_sub*iy
                    self.y1[ix, iy, t_idx] = self.y0[ix, iy, t_idx].copy()
                    self.z1[ix, iy, t_idx] = grid.z[0]

            proc = []
            sub_data = []
            for i_proc in range(self.params.n_proc):
                proc.append(mp.Process(target=self.__sub_tracers,
                                       args=(queue, field, t_idx, i_proc, self.params.n_proc)))
            for i_proc in range(self.params.n_proc):
                proc[i_proc].start()
            for i_proc in range(self.params.n_proc):
                sub_data.append(queue.get())
            for i_proc in range(self.params.n_proc):
                proc[i_proc].join()
            for i_proc in range(self.params.n_proc):
                # Extract the data from the single cores. Mind the order.
                sub_proc = sub_data[i_proc][0]
                self.x1[sub_proc::self.params.n_proc, :, t_idx] = sub_data[i_proc][1]
                self.y1[sub_proc::self.params.n_proc, :, t_idx] = sub_data[i_proc][2]
                self.z1[sub_proc::self.params.n_proc, :, t_idx] = sub_data[i_proc][3]
                self.l[sub_proc::self.params.n_proc, :, t_idx] = sub_data[i_proc][4]
                self.mapping[sub_proc::self.params.n_proc, :, t_idx, :] = sub_data[i_proc][5]
                if self.params.int_q == 'curly_A':
                    self.curly_A[sub_proc::self.params.n_proc, :, t_idx] = sub_data[i_proc][6]
                if self.params.int_q == 'ee':
                    self.ee[sub_proc::self.params.n_proc, :, t_idx] = sub_data[i_proc][7]
            for i_proc in range(self.params.n_proc):
                proc[i_proc].terminate()

            return 0
示例#12
0
def slices2vtk(field='',
               extension='',
               datadir='data',
               destination='slices',
               proc=-1):
    """
    Convert slices from PencilCode format to vtk.

    call signature::

      slices2vtk(field='', extension='', datadir='data', destination='slices', proc=-1)

    Read slice files specified by *variables* and convert
    them into vtk format for the specified extensions.
    Write the result in *destination*.
    NB: You need to have called src/read_videofiles.x before using this script.

    Keyword arguments:

      *field*:
        All allowed fields which can be written as slice files, e.g. b2, uu1, lnrho, ...
        See the pencil code manual for more (chapter: "List of parameters for `video.in'").

      *extension*:
        List of slice positions.

      *datadir*:
        Directory where the data is stored.

      *destination*:
        Destination files.

      *proc*:
        Processor which should be read. Set to -1 for all processors.
    """

    import sys
    import numpy as np
    from pencil import read

    # Convert single variable string into length 1 list of arrays.
    if (len(field) > 0):
        if (len(field[0]) == 1):
            field = [field]
    if (len(extension) > 0):
        if (len(extension[0]) == 1):
            extension = [extension]

    # Read the grid dimensions.
    grid = read.grid(datadir=datadir, proc=proc, trim=True, quiet=True)

    # Read the dimensions.
    dim = read.dim(datadir=datadir, proc=proc)

    # Read the user given parameters for the slice positions.
    params = read.param(quiet=True)

    # Read the slice file for all specified variables and extensions.
    slices = read.slices(field=field,
                         extension=extension,
                         datadir=datadir,
                         proc=proc)

    # Determine the position of the slices.
    if params.ix != -1:
        x0 = grid.x[params.ix]
    elif params.slice_position == 'm':
        x0 = grid.x[int(len(grid.x) / 2)]
    if params.iy != -1:
        y0 = grid.y[params.iy]
    elif params.slice_position == 'm':
        y0 = grid.y[int(len(grid.y) / 2)]
    if params.iz != -1:
        z0 = grid.z[params.iz]
    elif params.slice_position == 'm':
        z0 = grid.z[int(len(grid.z) / 2)]
    if params.iz2 != -1:
        z02 = grid.z[params.iz]
    elif params.slice_position == 'm':
        z02 = grid.z[int(len(grid.z) / 2)]

    for t_idx, t in enumerate(slices.t):
        for ext in extension:
            # Open the destination file for writing.
            fd = open(destination + '_' + ext + '_' + str(t_idx) + '.vtk',
                      'wb')

            # Write the header.
            fd.write('# vtk DataFile Version 2.0\n'.encode('utf-8'))
            fd.write('slices {0}\n'.format(ext).encode('utf-8'))
            fd.write('BINARY\n'.encode('utf-8'))
            fd.write('DATASET STRUCTURED_POINTS\n'.encode('utf-8'))
            if ext == 'xy':
                fd.write('DIMENSIONS {0:9} {1:9} {2:9}\n'.format(
                    dim.nx, dim.ny, 1).encode('utf-8'))
                fd.write('ORIGIN {0:8.12} {1:8.12} {2:8.12}\n'.format(
                    grid.x[0], grid.y[0], z0).encode('utf-8'))
                fd.write('SPACING {0:8.12} {1:8.12} {2:8.12}\n'.format(
                    grid.dx, grid.dy, 1.).encode('utf-8'))
                dim_p = dim.nx
                dim_q = dim.ny
            if ext == 'xy2':
                fd.write('DIMENSIONS {0:9} {1:9} {2:9}\n'.format(
                    dim.nx, dim.ny, 1).encode('utf-8'))
                fd.write('ORIGIN {0:8.12} {1:8.12} {2:8.12}\n'.format(
                    grid.x[0], grid.y[0], z02).encode('utf-8'))
                fd.write('SPACING {0:8.12} {1:8.12} {2:8.12}\n'.format(
                    grid.dx, grid.dy, 1.).encode('utf-8'))
                dim_p = dim.nx
                dim_q = dim.ny
            if ext == 'xz':
                fd.write('DIMENSIONS {0:9} {1:9} {2:9}\n'.format(
                    dim.nx, 1, dim.nz).encode('utf-8'))
                fd.write('ORIGIN {0:8.12} {1:8.12} {2:8.12}\n'.format(
                    grid.x[0], y0, grid.z[0]).encode('utf-8'))
                fd.write('SPACING {0:8.12} {1:8.12} {2:8.12}\n'.format(
                    grid.dx, 1., grid.dz).encode('utf-8'))
                dim_p = dim.nx
                dim_q = dim.nz
            if ext == 'yz':
                fd.write('DIMENSIONS {0:9} {1:9} {2:9}\n'.format(
                    1, dim.ny, dim.nz).encode('utf-8'))
                fd.write('ORIGIN {0:8.12} {1:8.12} {2:8.12}\n'.format(
                    x0, grid.y[0], grid.z[0]).encode('utf-8'))
                fd.write('SPACING {0:8.12} {1:8.12} {2:8.12}\n'.format(
                    1., grid.dy, grid.dz).encode('utf-8'))
                dim_p = dim.ny
                dim_q = dim.nz
            fd.write('POINT_DATA {0:9}\n'.format(dim_p *
                                                 dim_q).encode('utf-8'))

            # Write the data.
            for fi in field:
                data = getattr(getattr(slices, ext), fi)
                fd.write(
                    ('SCALARS ' + ext + '_' + fi + ' float\n').encode('utf-8'))
                fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
                if sys.byteorder == 'little':
                    data = data.astype(np.float32).byteswap()
                else:
                    data = data.astype(np.float32)
                fd.write(data[t_idx].tobytes())

            fd.close()
示例#13
0
def var2vtk(var_file='var.dat',
            datadir='data',
            proc=-1,
            variables=None,
            b_ext=False,
            magic=[],
            destination='work',
            quiet=True,
            trimall=True,
            ti=-1,
            tf=-1):
    """
    Convert data from PencilCode format to vtk.

    call signature::

      var2vtk(var_file='', datadir='data', proc=-1,
             variables='', b_ext=False,
             destination='work', quiet=True, trimall=True, ti=-1, tf=-1)

    Read *var_file* and convert its content into vtk format. Write the result
    in *destination*.

    Keyword arguments:

      *var_file*:
        The original var_file.

      *datadir*:
        Directory where the data is stored.

      *proc*:
        Processor which should be read. Set to -1 for all processors.

      *variables*:
        List of variables which should be written. If None all.

      *b_ext*:
        Add the external magnetic field.

      *destination*:
        Destination file.

      *quiet*:
        Keep quiet when reading the var files.

      *trimall*:
        Trim the data cube to exclude ghost zones.

      *ti, tf*:
        Start and end index for animation. Leave negative for no animation.
        Overwrites variable var_file.
    """

    import numpy as np
    import sys
    from pencil import read
    from pencil import math

    # Determine of we want an animation.
    if ti < 0 or tf < 0:
        animation = False
    else:
        animation = True

    # If no variables specified collect all by default
    if not variables:
        variables = []
        indx = read.index()
        for key in indx.__dict__.keys():
            if 'keys' not in key:
                variables.append(key)
        if 'uu' in variables:
            magic.append('vort')
            variables.append('vort')
        if 'rho' in variables or 'lnrho' in variables:
            if 'ss' in variables:
                magic.append('tt')
                variables.append('tt')
                magic.append('pp')
                variables.append('pp')
        if 'aa' in variables:
            magic.append('bb')
            variables.append('bb')
            magic.append('jj')
            variables.append('jj')
            variables.append('ab')
            variables.append('b_mag')
            variables.append('j_mag')
    else:
        # Convert single variable string into length 1 list of arrays.
        if (len(variables) > 0):
            if (len(variables[0]) == 1):
                variables = [variables]
        if 'tt' in variables:
            magic.append('tt')
        if 'pp' in variables:
            magic.append('pp')
        if 'bb' in variables:
            magic.append('bb')
        if 'jj' in variables:
            magic.append('jj')
        if 'vort' in variables:
            magic.append('vort')
        if 'b_mag' in variables and not 'bb' in magic:
            magic.append('bb')
        if 'j_mag' in variables and not 'jj' in magic:
            magic.append('jj')
        if 'ab' in variables and not 'bb' in magic:
            magic.append('bb')

    for t_idx in range(ti, tf + 1):
        if animation:
            var_file = 'VAR' + str(t_idx)

        # Read the PencilCode variables and set the dimensions.
        var = read.var(var_file=var_file,
                       datadir=datadir,
                       proc=proc,
                       magic=magic,
                       trimall=True,
                       quiet=quiet)

        grid = read.grid(datadir=datadir, proc=proc, trim=trimall, quiet=True)

        params = read.param(quiet=True)

        # Add external magnetic field.
        if (b_ext == True):
            B_ext = np.array(params.b_ext)
            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)

        # Write the vtk header.
        if animation:
            fd = open(destination + str(t_idx) + '.vtk', 'wb')
        else:
            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'))

        # Write the data.
        for v in variables:
            print('Writing {0}.'.format(v))
            # Prepare the data to the correct format.
            if v == 'ab':
                data = math.dot(var.aa, var.bb)
            elif v == 'b_mag':
                data = np.sqrt(math.dot2(var.bb))
            elif v == 'j_mag':
                data = np.sqrt(math.dot2(var.jj))
            else:
                data = getattr(var, v)
            if sys.byteorder == 'little':
                data = data.astype(np.float32).byteswap()
            else:
                data = data.astype(np.float32)
            # Check if we have vectors or scalars.
            if data.ndim == 4:
                data = np.moveaxis(data, 0, 3)
                fd.write('VECTORS {0} float\n'.format(v).encode('utf-8'))
            else:
                fd.write('SCALARS {0} float\n'.format(v).encode('utf-8'))
                fd.write('LOOKUP_TABLE default\n'.encode('utf-8'))
            fd.write(data.tobytes())

        del (var)

        fd.close()
示例#14
0
def derive_data(sim_path,
                src,
                dst,
                magic=['pp', 'tt'],
                par=[],
                comm=None,
                gd=[],
                overwrite=False,
                rank=0,
                size=1,
                nghost=3,
                status='a',
                chunksize=1000.0,
                dtype=np.float64,
                quiet=True,
                nmin=32):

    if comm:
        overwrite = False
    if isinstance(par, list):
        os.chdir(sim_path)
        par = read.param(quiet=True, conflicts_quiet=True)
    if isinstance(gd, list):
        os.chdir(sim_path)
        gd = read.grid(quiet=True)
    #get data dimensions
    nx, ny, nz = src['settings']['nx'][0],\
                 src['settings']['ny'][0],\
                 src['settings']['nz'][0]
    mx, my, mz = src['settings']['mx'][0],\
                 src['settings']['my'][0],\
                 src['settings']['mz'][0]
    #split data into manageable memory chunks
    dstchunksize = 8 * nx * ny * nz / 1024 * 1024
    if dstchunksize > chunksize:
        nchunks = cpu_optimal(nx,
                              ny,
                              nz,
                              quiet=quiet,
                              mvar=src['settings/mvar'][0],
                              maux=src['settings/maux'][0],
                              MBmin=chunksize,
                              nmin=nmin,
                              size=size)[1]
    else:
        nchunks = [1, 1, 1]
    print('nchunks {}'.format(nchunks))
    # for mpi split chunks across processes
    if size > 1:
        locindx = np.array_split(np.arange(nx) + nghost, nchunks[0])
        locindy = np.array_split(np.arange(ny) + nghost, nchunks[1])
        locindz = np.array_split(np.arange(nz) + nghost, nchunks[2])
        indx = [
            locindx[np.mod(
                rank + int(rank / nchunks[2]) + int(rank / nchunks[1]),
                nchunks[0])]
        ]
        indy = [locindy[np.mod(rank + int(rank / nchunks[2]), nchunks[1])]]
        indz = [locindz[np.mod(rank, nchunks[2])]]
        allchunks = 1
    else:
        locindx = np.array_split(np.arange(nx) + nghost, nchunks[0])
        locindy = np.array_split(np.arange(ny) + nghost, nchunks[1])
        locindz = np.array_split(np.arange(nz) + nghost, nchunks[2])
        indx = np.array_split(np.arange(nx) + nghost, nchunks[0])
        indy = np.array_split(np.arange(ny) + nghost, nchunks[1])
        indz = np.array_split(np.arange(nz) + nghost, nchunks[2])
        allchunks = nchunks[0] * nchunks[1] * nchunks[2]
    # save time
    dataset_h5(dst,
               'time',
               status=status,
               data=src['time'][()],
               comm=comm,
               size=size,
               rank=rank,
               overwrite=overwrite,
               dtype=dtype)
    # ensure derived variables are in a list
    if isinstance(magic, list):
        magic = magic
    else:
        magic = [magic]
    # initialise group
    group = group_h5(dst,
                     'data',
                     status='a',
                     overwrite=overwrite,
                     comm=comm,
                     rank=rank,
                     size=size)
    for key in magic:
        if is_vector(key):
            dataset_h5(group,
                       key,
                       status=status,
                       shape=[3, mz, my, mx],
                       comm=comm,
                       size=size,
                       rank=rank,
                       overwrite=overwrite,
                       dtype=dtype)
            print('writing ' + key + ' shape {}'.format([3, mz, my, mx]))
        else:
            dataset_h5(group,
                       key,
                       status=status,
                       shape=[mz, my, mx],
                       comm=comm,
                       size=size,
                       rank=rank,
                       overwrite=overwrite,
                       dtype=dtype)
            print('writing ' + key + ' shape {}'.format([mz, my, mx]))
        for ichunk in range(allchunks):
            for iz in [indz[np.mod(ichunk, nchunks[2])]]:
                n1, n2 = iz[ 0]-nghost,\
                         iz[-1]+nghost+1
                n1out = n1 + nghost
                n2out = n2 - nghost
                varn1 = nghost
                varn2 = -nghost
                if iz[0] == locindz[0][0]:
                    n1out = 0
                    varn1 = 0
                if iz[-1] == locindz[-1][-1]:
                    n2out = n2
                    varn2 = n2
                for iy in [
                        indy[np.mod(ichunk + int(ichunk / nchunks[2]),
                                    nchunks[1])]
                ]:
                    m1, m2 = iy[ 0]-nghost,\
                             iy[-1]+nghost+1
                    m1out = m1 + nghost
                    m2out = m2 - nghost
                    varm1 = nghost
                    varm2 = -nghost
                    if iy[0] == locindy[0][0]:
                        m1out = 0
                        varm1 = 0
                    if iy[-1] == locindy[-1][-1]:
                        m2out = m2
                        varm2 = m2
                    for ix in [
                            indx[np.mod(
                                ichunk + int(ichunk / nchunks[2]) +
                                int(ichunk / nchunks[1]), nchunks[0])]
                    ]:
                        l1, l2 = ix[ 0]-nghost,\
                                 ix[-1]+nghost+1
                        l1out = l1 + nghost
                        l2out = l2 - nghost
                        varl1 = nghost
                        varl2 = -nghost
                        if ix[0] == locindx[0][0]:
                            l1out = 0
                            varl1 = 0
                        if ix[-1] == locindx[-1][-1]:
                            l2out = l2
                            varl2 = l2
                        if not quiet:
                            print('remeshing ' + key +
                                  ' chunk {}'.format([iz, iy, ix]))
                        var = calc_derived_data(src['data'],
                                                dst['data'],
                                                key,
                                                par,
                                                gd,
                                                l1,
                                                l2,
                                                m1,
                                                m2,
                                                n1,
                                                n2,
                                                nghost=nghost)
                        #print('var shape {}'.format(var.shape))
                        #if not quiet:
                        #    print('writing '+key+
                        #                   ' shape {} chunk {}'.format(
                        #                         var.shape, [iz,iy,ix]))
                        if is_vector(key):
                            dst['data'][key][:, n1out:n2out, m1out:m2out,
                                             l1out:l2out] = dtype(
                                                 var[:, varn1:varn2,
                                                     varm1:varm2, varl1:varl2])
                        else:
                            dst['data'][key][n1out:n2out, m1out:m2out,
                                             l1out:l2out] = dtype(
                                                 var[varn1:varn2, varm1:varm2,
                                                     varl1:varl2])