Example #1
0
    def __init__(self, root_dir, file_dir, fig_name, n_files=None, **kwargs):
        """
        Initializes the profile plotter.

        Attributes:
        -----------
        root_dir : string
            Root file directory of output files
        file_dir : string
            subdirectory of root_dir where the data to make PDFs is contained
        fig_name : string
            As in class-level docstring
        n_files  : int, optional
            Number of files to process. If None, all of them.
        **kwargs : Additional keyword arguments for FileReader()
        """
        self.reader = FileReader(root_dir, sub_dirs=[file_dir,], num_files=[n_files,], **kwargs)
        self.fig_name = fig_name
        self.out_dir  = '{:s}/{:s}/'.format(root_dir, fig_name)
        if self.reader.comm.rank == 0 and not os.path.exists('{:s}'.format(self.out_dir)):
            os.mkdir('{:s}'.format(self.out_dir))
        self.my_sync = Sync(self.reader.comm)

        self.files = self.reader.local_file_lists[file_dir]
        self.idle  = self.reader.idle[file_dir]
        self.dist_comm  = self.reader.distribution_comms[file_dir]
Example #2
0
def test_cartesian_output_virtual(dtype, dealias, output_scales):
    Nx = Ny = Nz = 16
    Lx = Ly = Lz = 2 * np.pi
    # Bases
    c = coords.CartesianCoordinates('x', 'y', 'z')
    d = distributor.Distributor((c,), mesh=(2,2))
    Fourier = {np.float64: basis.RealFourier, np.complex128: basis.ComplexFourier}[dtype]
    xb = Fourier(c.coords[0], size=Nx, bounds=(0, Lx), dealias=dealias)
    yb = Fourier(c.coords[1], size=Ny, bounds=(0, Ly), dealias=dealias)
    zb = Fourier(c.coords[2], size=Nz, bounds=(0, Lz), dealias=dealias)
    x = xb.local_grid(1)
    y = yb.local_grid(1)
    z = zb.local_grid(1)
    # Fields
    u = field.Field(name='u', dist=d, bases=(xb,yb,zb), dtype=dtype)
    v = field.Field(name='v', dist=d, bases=(xb,yb,zb), tensorsig=(c,), dtype=dtype)
    u['g'] = np.sin(x) * np.sin(y) * np.sin(z)
    # Problem
    dt = operators.TimeDerivative
    problem = problems.IVP([u, v])
    problem.add_equation((dt(u) + u, 0))
    problem.add_equation((dt(v) + v, 0))
    # Solver
    solver = solvers.InitialValueSolver(problem, timesteppers.RK222)
    # Output
    tasks = [u, u(x=0), u(y=0), u(z=0), u(x=0,y=0), u(x=0,z=0), u(y=0,z=0), u(x=0,y=0,z=0),
             v, v(x=0), v(y=0), v(z=0), v(x=0,y=0), v(x=0,z=0), v(y=0,z=0), v(x=0,y=0,z=0)]
    output = solver.evaluator.add_file_handler('test_output', iter=1, max_writes=1, virtual_file=True)
    for task in tasks:
        output.add_task(task, layout='g', name=str(task), scales=output_scales)
    solver.evaluator.evaluate_handlers([output])
    # Check solution
    errors = []
    d.comm.Barrier()
    with h5py.File('test_output/test_output_s1.h5', mode='r') as file:
        for task in tasks:
            task_name = str(task)
            task = task.evaluate()
            task.change_scales(output_scales)
            local_slices = (slice(None),) * len(task.tensorsig) + d.grid_layout.slices(task.domain, task.scales)
            task_saved = file['tasks'][task_name][-1]
            task_saved = task_saved[local_slices]
            local_error = task['g'] - task_saved
            if local_error.size:
                errors.append(np.max(np.abs(task['g'] - task_saved)))
    with Sync() as sync:
        if sync.comm.rank == 0:
            shutil.rmtree('test_output')
    assert np.allclose(errors, 0)
cP = γ / (γ - 1)

data_dir = sys.argv[0].split('.py')[0]
data_dir += "_nh{}_μ{}_Pr{}".format(args['--n_h'], args['--mu'],
                                    args['--Prandtl'])
data_dir += "_{}_a{}".format(strat_label, args['--aspect'])
data_dir += "_nz{:d}_nx{:d}".format(nz, nx)
if args['--label']:
    data_dir += '_{:s}'.format(args['--label'])

from dedalus.tools.config import config
config['logging']['filename'] = os.path.join(data_dir, 'logs/dedalus_log')
config['logging']['file_level'] = 'DEBUG'

with Sync() as sync:
    if sync.comm.rank == 0:
        if not os.path.exists('{:s}/'.format(data_dir)):
            os.mkdir('{:s}/'.format(data_dir))
        logdir = os.path.join(data_dir, 'logs')
        if not os.path.exists(logdir):
            os.mkdir(logdir)

import dedalus.public as de
from dedalus.extras import flow_tools

logger.info(args)
logger.info("saving data in: {}".format(data_dir))

# this assumes h_bot=1, grad_φ = (γ-1)/γ (or L=Hρ)
h_bot = 1
Example #4
0
    def __init__(self,
                 base_path,
                 *args,
                 max_writes=np.inf,
                 max_size=2**30,
                 parallel=None,
                 mode=None,
                 set_num_in,
                 **kw):

        Handler.__init__(self, *args, **kw)

        # Resolve defaults from config
        if parallel is None:
            parallel = FILEHANDLER_PARALLEL_DEFAULT
        if mode is None:
            mode = FILEHANDLER_MODE_DEFAULT

        # Check base_path
        base_path = pathlib.Path(base_path).resolve()
        if any(base_path.suffixes):
            raise ValueError(
                "base_path should indicate a folder for storing HDF5 files.")

        # Attributes
        self.base_path = base_path
        self.max_writes = max_writes
        self.max_size = max_size
        self.parallel = parallel
        self._sl_array = np.zeros(1, dtype=int)

        # Resolve mode
        mode = mode.lower()
        # ~ if mode not in ['overwrite', 'append','specify']:
        if mode not in ['specify']:
            raise ValueError("Write mode {} not defined.".format(mode))

        comm = self.domain.dist.comm_cart
        if comm.rank == 0:
            set_pattern = '%s_s*' % (self.base_path.stem)
            sets = list(self.base_path.glob(set_pattern))
            if mode == "overwrite":
                for set in sets:
                    if set.is_dir():
                        shutil.rmtree(str(set))
                    else:
                        set.unlink()
                set_num = 1
                total_write_num = 1
            elif mode == "append":
                set_nums = []
                if sets:
                    for set in sets:
                        m = re.match("{}_s(\d+)$".format(base_path.stem),
                                     set.stem)
                        if m:
                            set_nums.append(int(m.groups()[0]))
                    max_set = max(set_nums)
                    joined_file = base_path.joinpath("{}_s{}.h5".format(
                        base_path.stem, max_set))
                    p0_file = base_path.joinpath(
                        "{0}_s{1}/{0}_s{1}_p0.h5".format(
                            base_path.stem, max_set))
                    if os.path.exists(str(joined_file)):
                        with h5py.File(str(joined_file), 'r') as testfile:
                            last_write_num = testfile['/scales/write_number'][
                                -1]
                    elif os.path.exists(str(p0_file)):
                        with h5py.File(str(p0_file), 'r') as testfile:
                            last_write_num = testfile['/scales/write_number'][
                                -1]
                    else:
                        last_write_num = 0
                        logger.warn(
                            "Cannot determine write num from files. Restarting count."
                        )
                else:
                    max_set = 0
                    last_write_num = 0
                set_num = max_set + 1
                total_write_num = last_write_num + 1
            elif mode == "specify":
                set_num = set_num_in
                last_write_num = 0
                total_write_num = last_write_num + 1

        else:
            set_num = None
            total_write_num = None
        # Communicate set and write numbers
        self.set_num = comm.bcast(set_num, root=0)
        self.total_write_num = comm.bcast(total_write_num, root=0)

        # Create output folder
        with Sync(comm):
            if comm.rank == 0:
                base_path.mkdir(exist_ok=True)

        if parallel:
            # Set HDF5 property list for collective writing
            self._property_list = h5py.h5p.create(h5py.h5p.DATASET_XFER)
            self._property_list.set_dxpl_mpio(h5py.h5fd.MPIO_COLLECTIVE)