# Note: SBDF2 timestepper does not currently work with AE

if args['--SBDF2']:
    ts = de.timesteppers.SBDF2
if args['--SBDF4']:
    ts = de.timesteppers.SBDF4
else:
    ts = de.timesteppers.RK443

cfl_safety = float(args['--safety'])
mag_cfl_safety = float(args['--mag_safety'])
solver = problem.build_solver(ts)
logger.info('Solver built')

### 6. Set initial conditions: noise or loaded checkpoint
checkpoint = Checkpoint(data_dir)
checkpoint_min = 30
restart = args['--restart']
if restart is None:
    p = solver.state['p']
    T1 = solver.state['T1']
    T1_z = solver.state['T1_z']
    p.set_scales(domain.dealias)
    T1.set_scales(domain.dealias)
    T1_z.set_scales(domain.dealias)
    z_de = domain.grid(-1, scales=domain.dealias)

    A0 = 1e-6

    #Add noise kick
    noise = global_noise(domain, int(args['--seed']))
problem.add_bc("right(p) = 0", condition="(nx == 0) and (ny == 0)")
problem.add_bc("right(w) = 0", condition="(nx != 0) or  (ny != 0)")

### 5. Build solver
# Note: SBDF2 timestepper does not currently work with AE.
#ts = de.timesteppers.SBDF2
if args['--RK443']:
    ts = de.timesteppers.RK443
else:
    ts = de.timesteppers.RK222
cfl_safety = float(args['--safety'])
solver = problem.build_solver(ts)
logger.info('Solver built')

### 6. Set initial conditions: noise or loaded checkpoint
checkpoint = Checkpoint(data_dir)
checkpoint_min = 30
restart = args['--restart']
TT_to_FT = args['--TT_to_FT']
not_corrected_times = True
true_t_ff = 1
if restart is None and TT_to_FT is None:
    p = solver.state['p']
    T1 = solver.state['T1']
    T1_z = solver.state['T1_z']
    p.set_scales(domain.dealias)
    T1.set_scales(domain.dealias)
    T1_z.set_scales(domain.dealias)
    z_de = domain.grid(-1, scales=domain.dealias)

    A0 = 1e-6
Beispiel #3
0
########################################################
# Timestepping
logger.info("buoyancy_time = {}".format(buoyancy_time))

output_cadence = float(args['--out_cadence']) * buoyancy_time
max_dt = safety * 0.1 * buoyancy_time / 4  #np.sqrt(Reynolds)
dt = max_dt / 10

#########################################################
# Initial conditions & Checkpointing
T1 = solver.state['T1']
T1_z = solver.state['T1_z']
ln_rho1 = solver.state['ln_rho1']

logger.info('checkpointing in {}'.format(data_dir))
checkpoint = Checkpoint(data_dir)
mode = 'overwrite'
if restart is None:
    therm = Thermal(domain,
                    atmosphere,
                    falling=True,
                    radius=radius,
                    r_width=delta_r,
                    A0=epsilon,
                    z_pert=z_pert)
    therm.set_thermal(T1, T1_z, ln_rho1)
else:
    logger.info("restarting from {}".format(restart))
    dt = checkpoint.restart(restart, solver)
    mode = 'append'
checkpoint.set_checkpoint(solver, sim_dt=buoyancy_time, mode=mode)
Beispiel #4
0
            logger.info('Adding BC "{:15s}" of form: "{:s}" (condition: {})'.format(k, bc[0], bc[1]))
            problem.add_bc(bc[0], condition=bc[1])

### 5. Build solver
# Note: SBDF2 timestepper does not currently work with AE.
#ts = de.timesteppers.SBDF2
if args['--RK443']:
    ts = de.timesteppers.RK443
else:
    ts = de.timesteppers.RK222
cfl_safety = float(args['--safety'])
solver = problem.build_solver(ts)
logger.info('Solver built')

### 6. Set initial conditions: noise or loaded checkpoint
checkpoint = Checkpoint(data_dir)
checkpoint_dt = 100
restart = args['--restart']
not_corrected_times = True
if restart is None:
    x_de = domain.grid(0, scales=domain.dealias)
    y_de = domain.grid(1, scales=domain.dealias)
    z_de = domain.grid(-1, scales=domain.dealias)

    f1 = 1.0*np.sin(34*np.pi*x_de/aspect) + 1.1*np.cos(32*np.pi*x_de/aspect) + 0.9*np.sin(38*np.pi*x_de/aspect)
    f2 = 1.0*np.cos(24*np.pi*x_de/aspect) - 0.7*np.sin(28*np.pi*x_de/aspect) + 1.4*np.cos(34*np.pi*x_de/aspect)
    g1 = 0.6*np.cos(38*np.pi*y_de/aspect) - 1.0*np.sin(46*np.pi*y_de/aspect) - 1.2*np.sin(28*np.pi*y_de/aspect)
    g2 = 1.0*np.cos(32*np.pi*y_de/aspect) + 0.8*np.sin(36*np.pi*y_de/aspect) - 1.1*np.cos(42*np.pi*y_de/aspect)
    h1 = 1.0*np.sin(13*np.pi*z_de) - 0.8*np.sin(16*np.pi*z_de) + 1.2*np.sin(20*np.pi*z_de)
    h2 = 1.0*np.sin(15*np.pi*z_de) - 1.5*np.sin(12*np.pi*z_de) + 0.7*np.sin(18*np.pi*z_de)
    def solve_IVP(self,
                  dt,
                  CFL,
                  data_dir,
                  analysis_tasks,
                  task_args=(),
                  pre_loop_args=(),
                  task_kwargs={},
                  pre_loop_kwargs={},
                  time_div=None,
                  track_fields=['Pe'],
                  threeD=False,
                  Hermitian_cadence=100,
                  no_join=False,
                  mode='append'):
        """Logic for a while-loop that solves an initial value problem.

        Parameters
        ----------
        dt                  : float
            The initial timestep of the simulation
        CFL                 : a Dedalus CFL object
            A CFL object that calculates the timestep of the simulation on the fly
        data_dir            : string
            The parent directory of output files
        analysis_tasks      : OrderedDict()
            An OrderedDict of dedalus FileHandler objects
        task_args, task_kwargs : list, dict, optional
            arguments & keyword arguments to the self._special_tasks() function
        pre_loop_args, pre_loop_kwargs: list, dict, optional
            arguments & keyword arguments to the self.pre_loop_setup() function
        time_div            : float, optional
            A siulation time to divide the normal time by for easier output tracking
        threeD              : bool, optional
            If True, occasionally force the solution to grid space to remove Hermitian errors
        Hermitian_cadence   : int, optional
            The number of timesteps between grid space forcings in 3D.
        no_join             : bool, optional
            If True, do not join files at the end of the simulation run.
        mode                : string, optional
            Dedalus output mode for final checkpoint. "append" or "overwrite"
        args, kwargs        : list and dictionary
            Additional arguments and keyword arguments to be passed to the self.special_tasks() function
        """

        # Flow properties
        self.flow = flow_tools.GlobalFlowProperty(self.solver, cadence=1)
        for f in track_fields:
            self.flow.add_property(f, name=f)

        self.pre_loop_setup(*pre_loop_args, **pre_loop_kwargs)

        start_time = time.time()
        # Main loop
        count = 0
        try:
            logger.info('Starting loop')
            init_time = self.solver.sim_time
            start_iter = self.solver.iteration
            while (self.solver.ok):
                dt = CFL.compute_dt()
                self.solver.step(dt)  #, trim=True)

                # prevents blow-up over long timescales in 3D due to hermitian-ness
                effective_iter = self.solver.iteration - start_iter
                if threeD and effective_iter % Hermitian_cadence == 0:
                    for field in self.solver.state.fields:
                        field.require_grid_space()

                self.special_tasks(*task_args, **task_kwargs)

                #reporting string
                self.iteration_report(dt, track_fields, time_div=time_div)

                if not np.isfinite(self.flow.grid_average(track_fields[0])):
                    break
        except:
            raise
            logger.error('Exception raised, triggering end of main loop.')
        finally:
            end_time = time.time()
            main_loop_time = end_time - start_time
            n_iter_loop = self.solver.iteration - 1
            logger.info('Iterations: {:d}'.format(n_iter_loop))
            logger.info('Sim end time: {:f}'.format(self.solver.sim_time))
            logger.info('Run time: {:f} sec'.format(main_loop_time))
            logger.info('Run time: {:f} cpu-hr'.format(
                main_loop_time / 60 / 60 *
                self.de_domain.domain.dist.comm_cart.size))
            logger.info('iter/sec: {:f} (main loop only)'.format(
                n_iter_loop / main_loop_time))
            try:
                final_checkpoint = Checkpoint(
                    data_dir, checkpoint_name='final_checkpoint')
                final_checkpoint.set_checkpoint(self.solver,
                                                wall_dt=1,
                                                mode=mode)
                self.solver.step(
                    dt)  #clean this up in the future...works for now.
                post.merge_process_files(data_dir + '/final_checkpoint/',
                                         cleanup=False)
            except:
                raise
                print('cannot save final checkpoint')
            finally:
                if not no_join:
                    logger.info('beginning join operation')
                    post.merge_analysis(data_dir + 'checkpoint')

                    for key, task in analysis_tasks.items():
                        logger.info(task.base_path)
                        post.merge_analysis(task.base_path)

                logger.info(40 * "=")
                logger.info('Iterations: {:d}'.format(n_iter_loop))
                logger.info('Sim end time: {:f}'.format(self.solver.sim_time))
                logger.info('Run time: {:f} sec'.format(main_loop_time))
                logger.info('Run time: {:f} cpu-hr'.format(
                    main_loop_time / 60 / 60 *
                    self.de_domain.domain.dist.comm_cart.size))
                logger.info('iter/sec: {:f} (main loop only)'.format(
                    n_iter_loop / main_loop_time))
Beispiel #6
0
def FC_TriLayer_convection(input_dict):
    """
    A driver function for running FC convection in Dedalus.

    Parameters
    ----------
    input_dict   : dict
        A dictionary of strings whose keys are all of the options specified above in the FC_poly.py docstring
    """
    import os
    from collections import OrderedDict
    from dedalus.tools.config import config
    from logic.functions import mpi_makedirs

    #Get info on data directory, create directories, setup logging
    # (Note: this order of imports is bad form, but gets logs properly outputting)
    data_dir, case_name = name_case(input_dict)
    mpi_makedirs(data_dir)
    logdir = os.path.join(data_dir, 'logs')
    mpi_makedirs(logdir)
    file_level = config['logging']['file_level'] = 'debug'
    filename = config['logging']['filename'] = os.path.join(
        logdir, 'dedalus_log')

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

    from logic.atmospheres import TriLayerIH
    from logic.domains import DedalusDomain
    from logic.experiments import CompressibleConvection
    from logic.problems import DedalusIVP
    from logic.equations import KappaMuFCE
    from logic.ae_tools import FCAcceleratedEvolutionIVP
    from logic.outputs import initialize_output, ae_initialize_output
    from logic.checkpointing import Checkpoint
    from logic.field_averager import AveragerFCAE, AveragerFCStructure

    import logging
    logger = logging.getLogger(__name__)
    logger.info("Running polytrope case: {}".format(case_name))
    logger.info("Saving run in: {}".format(data_dir))

    # Read in command line args & process them
    # Atmosphere params
    Ra, Pr, n_rho_rzT, n_rho_cz, n_rho_rzB, eps, aspect = [
        float(input_dict[k])
        for k in ('--Rayleigh', '--Prandtl', '--n_rho_rzT', '--n_rho_cz',
                  '--n_rho_rzB', '--epsilon', '--aspect')
    ]
    threeD = input_dict['--3D']

    # BCs
    thermal_BC = OrderedDict((('flux', False), ('temp', False),
                              ('flux_temp', False), ('temp_flux', False)))
    velocity_BC = OrderedDict((('stress_free', False), ('no_slip', False)))
    thermal_BC[input_dict['--thermal_BC']] = True
    velocity_BC[input_dict['--velocity_BC']] = True

    # Coeff resolutions
    nx, ny, nz = [int(input_dict[n]) for n in ['--nx', '--ny', '--nz']]

    # 3D processor mesh.
    mesh = input_dict['--mesh']
    if mesh is not None:
        mesh = mesh.split(',')
        mesh = [int(mesh[0]), int(mesh[1])]

    # Stop conditions
    run_time_wall, run_time_buoy, run_time_therm = [
        input_dict[t]
        for t in ['--run_time_wall', '--run_time_buoy', '--run_time_therm']
    ]
    run_time_wall = float(run_time_wall)
    if run_time_buoy is not None:
        run_time_buoy = float(run_time_buoy)
        run_time_therm = None
        logger.info(
            "Terminating run after {} buoyancy times".format(run_time_buoy))
    else:
        run_time_therm = float(run_time_therm)
        logger.info(
            "Terminating run after {} thermal times".format(run_time_therm))

    # Initialize atmosphere class
    atmo_kwargs = OrderedDict([('epsilon', eps), ('n_rho_rzT', n_rho_rzT),
                               ('n_rho_cz', n_rho_cz),
                               ('n_rho_rzB', n_rho_rzB), ('gamma', 5. / 3),
                               ('R', 1)])
    atmosphere = TriLayerIH(**atmo_kwargs)

    # Initialize domain class
    resolution = (nz, nx, ny)
    if not threeD: resolution = resolution[:2]
    domain_args = (atmosphere, resolution, aspect)
    domain_kwargs = OrderedDict((('mesh', mesh), ))
    de_domain = DedalusDomain(*domain_args, **domain_kwargs)

    #Set diffusivities
    experiment_args = (de_domain, atmosphere, Ra, Pr)
    experiment_kwargs = {}
    experiment = CompressibleConvection(*experiment_args, **experiment_kwargs)

    #Set up problem and equations
    if args['--ae']:
        problem_type = FCAcceleratedEvolutionIVP
    else:
        problem_type = DedalusIVP
    de_problem = problem_type(de_domain)

    equations = KappaMuFCE(thermal_BC, velocity_BC, atmosphere, de_domain,
                           de_problem)

    atmosphere.save_atmo_file('./', de_domain)

    # Build solver, set stop times
    de_problem.build_solver(de.timesteppers.RK222)

    #Setup checkpointing and initial conditions
    checkpoint = Checkpoint(data_dir)
    dt, mode = experiment.set_IC(
        de_problem.solver,
        eps,
        checkpoint,
        restart=args['--restart'],
        seed=int(args['--seed']),
        checkpoint_dt=float(args['--checkpoint_buoy']) *
        atmosphere.atmo_params['t_buoy'])
    if run_time_buoy is None:
        stop_sim_time = run_time_therm * atmosphere.atmo_params['t_therm']
    else:
        stop_sim_time = run_time_buoy * atmosphere.atmo_params['t_buoy']
    if args['--run_time_restarted']:
        stop_sim_time += de_problem.solver.sim_time

    de_problem.set_stop_condition(stop_wall_time=run_time_wall * 3600,
                                  stop_sim_time=stop_sim_time)

    #Set up outputs
    output_dt = float(args['--output_dt']) * atmosphere.atmo_params['t_buoy']
    if args['--ae_outs']:
        analysis_tasks = ae_initialize_output(
            de_domain,
            de_problem,
            data_dir,
            output_dt=output_dt,
            output_vol_dt=atmosphere.atmo_params['t_buoy'],
            mode=mode)  # volumes_output=True)
    else:
        analysis_tasks = initialize_output(
            de_domain,
            de_problem,
            data_dir,
            output_dt=output_dt,
            output_vol_dt=atmosphere.atmo_params['t_buoy'],
            mode=mode)  # volumes_output=True)

    # Ensure good initial dt and setup CFL
    max_dt = min(
        (0.2, float(args['--output_dt']))) * atmosphere.atmo_params['t_buoy']
    if dt is None:
        dt = max_dt
    cfl_safety = 0.1
    CFL = flow_tools.CFL(de_problem.solver,
                         initial_dt=dt,
                         cadence=1,
                         safety=cfl_safety * 2,
                         max_change=1.5,
                         min_change=0.5,
                         max_dt=max_dt,
                         threshold=0.1)
    if threeD:
        CFL.add_velocities(('u', 'v', 'w'))
    else:
        CFL.add_velocities(('u', 'w'))

    # Solve the IVP.
    if args['--ae']:
        task_args = (thermal_BC, )
        pre_loop_args = ((AveragerFCAE, AveragerFCStructure), (True, False),
                         data_dir, atmo_kwargs, CompressibleConvection,
                         experiment_args, experiment_kwargs)
        task_kwargs = {}
        pre_loop_kwargs = {
            'sim_time_start':
            int(args['--ae_start_time']) * atmosphere.atmo_params['t_buoy'],
            'min_bvp_time':
            10 * atmosphere.atmo_params['t_buoy'],
            'between_ae_wait_time':
            20 * atmosphere.atmo_params['t_buoy'],
            'later_bvp_time':
            20 * atmosphere.atmo_params['t_buoy'],
            'ae_convergence':
            1e-2,
            'bvp_threshold':
            1e-2,
            'min_bvp_threshold':
            5e-3
        }
        solve_args = (dt, CFL, data_dir, analysis_tasks)
        solve_kwargs = {
            'task_args': task_args,
            'pre_loop_args': pre_loop_args,
            'task_kwargs': task_kwargs,
            'pre_loop_kwargs': pre_loop_kwargs,
            'time_div': atmosphere.atmo_params['t_buoy'],
            'track_fields': ['Pe_rms', 'dissipation'],
            'threeD': threeD,
            'Hermitian_cadence': 100,
            'no_join': args['--no_join'],
            'mode': mode
        }
        de_problem.solve_IVP(*solve_args, **solve_kwargs)
    else:
        de_problem.solve_IVP(dt,
                             CFL,
                             data_dir,
                             analysis_tasks,
                             time_div=atmosphere.atmo_params['t_buoy'],
                             track_fields=['Pe_rms', 'dissipation'],
                             threeD=threeD,
                             Hermitian_cadence=100,
                             no_join=args['--no_join'],
                             mode=mode)
Beispiel #7
0
problem.add_bc("left(wr) = 0")
problem.add_bc("left(rho1r) = 0")

#########################
# Initialization of run
#########################
solver = problem.build_solver(de.timesteppers.RK443)
logger.info('Solver built')

# Set up initial conditions
rho1 = solver.state['rho1']
rho1r = solver.state['rho1r']
w = solver.state['w']

logger.info('checkpointing in {}'.format(data_dir))
checkpoint = Checkpoint(data_dir)
mode = 'overwrite'
restart = args['--restart']

# Simulation termination parameters
solver.stop_sim_time = 20.
solver.stop_wall_time = np.inf
solver.stop_iteration = np.inf

#################################
# Initial conditions from Daniel
#################################
noise = domain.new_field()
noise.meta['r']['dirichlet'] = True
noise.set_scales(domain.dealias)