Example #1
0
def diffusive_solidification_resolution_specific_params(p):
    """
    :param p: ConvergenceTestParams
    :return:
    """
    mushy_layer_base_dir = get_mushy_layer_dir()
    params_file = os.path.join(
        mushy_layer_base_dir,
        'params/convergenceTest/noFlowConvTest.parameters')

    params = read_inputs(params_file)
    grid_file = ''

    nx_coarse = 4
    params['main.domain_width'] = str(4.0 * float(nx_coarse) /
                                      float(p.nz_coarse))
    linear_temperature_gradient = 1.0 / 4.0  # delta T / height
    params['main.refine_thresh'] = str(linear_temperature_gradient * 16.0 /
                                       float(p.nz_coarse))
    params['main.tag_buffer_size'] = str(int(float(p.nz_coarse) / 8))
    params[
        'main.steady_state'] = '1e-8'  # str(1e-4 * pow(32.0/float(nz_coarse),2))
    params['main.max_grid_size'] = '32'
    params['main.max_step'] = 10000
    params[
        'main.debug'] = 'true'  # make sure we output things like Temperature error

    return nx_coarse, params, grid_file
Example #2
0
def test_fixed_porous_hole():
    base_output_dir = get_base_output_dir()
    # matlab_command = get_matlab_base_command()

    print(Fore.GREEN + 'Setup tests for (fixed) porous hole' + Style.RESET_ALL)
    physical_problem = 'DBVariablePorosity'
    data_folder = os.path.join(
        base_output_dir,
        'FixedPorousHole-1proc-minPorosity%s' % get_min_porosity())

    amr_setup, num_procs = get_setup()

    # Setup up the post processing command
    # uniform_prefix = 'Uniform-DBVariablePorosity-'
    python_compare_file = os.path.join(get_mushy_layer_dir(), 'test',
                                       'run_chombo_compare.py')
    chombo_compare_analyse = 'python %s -f %s -a -v \'xDarcy velocity\' ' \
                             '-e L2 -r True -n 6 -d %s \n \n' % (python_compare_file,
                                                                 data_folder,
                                                                 base_output_dir)

    # Just do analysis - make figures manually once we know the sidewall problem has also finished
    analysis_command = chombo_compare_analyse

    # Run
    extra_params = {}
    run_test(data_folder, physical_problem,
             fixed_porous_resolution_specific_params, amr_setup, num_procs,
             analysis_command, extra_params)
Example #3
0
    def get_mushy_layer_exec(self):
        exec_name = 'mushyLayer%dd' % self.properties['dim']
        exec_dir = os.path.join(mushyLayerRunUtils.get_mushy_layer_dir(), 'execSubcycle')
        mushy_layer_exec_path = mushyLayerRunUtils.get_executable_name(exec_dir, exec_name=exec_name,
                                                                       return_full_path=True)

        return mushy_layer_exec_path
Example #4
0
def base_inputs():
    test_dir = os.path.join(get_mushy_layer_dir(), 'test/diffusiveGrowth')
    default_inputs = read_inputs(os.path.join(test_dir, 'inputs'))

    default_inputs['main.min_time'] = 0.5
    default_inputs['main.max_time'] = 3.0
    default_inputs['main.steady_state'] = 1e-8

    return default_inputs
def base_inputs():
    test_dir = os.path.join(mushyLayerRunUtils.get_mushy_layer_dir(),
                            'test/poiseuilleFlow')
    default_inputs = mushyLayerRunUtils.read_inputs(
        os.path.join(test_dir, 'inputs'))

    default_inputs['main.min_time'] = 0.2
    default_inputs['main.max_time'] = 3.0
    default_inputs['main.steady_state'] = 1e-5

    return default_inputs
def base_inputs():

    test_dir = os.path.join(mushyLayerRunUtils.get_mushy_layer_dir(),
                            'test/diffusiveGrowth')
    inputs = mushyLayerRunUtils.read_inputs(os.path.join(test_dir, 'inputs'))

    inputs['main.min_time'] = 0.5
    inputs['main.max_time'] = 3.0
    inputs['main.steady_state'] = 1e-8

    return inputs
Example #7
0
def hele_shaw_resolution_specific_params(nz_coarse, ref_rat, max_level,
                                         max_refinement):
    mushyLayerBaseDir = get_mushy_layer_dir()

    params_file = mushyLayerBaseDir + '/params/convergenceTest/FixedChill.parameters'

    params = read_inputs(params_file)
    nx_coarse = -1

    gridFile = ''  # mushyLayerBaseDir + '/grids/topMiddle/' + str(nx_coarse) + 'x' + str(nz_coarse)

    return nx_coarse, params, gridFile
Example #8
0
def test_porous_mushy_hole(argv):
    # Defaults:
    max_time = 1.5e-4
    hole_radius = 0.04
    plot_interval = None

    try:
        opts, _ = getopt.getopt(argv, "t:h:p:")
    except getopt.GetoptError as err:
        print(str(err))
        print('testPorousMushyHole.py -t<max time> -h<hole radius=0.02>')
        sys.exit(2)

    for opt, arg in opts:
        if opt in "-t":
            max_time = float(arg)
        elif opt in "-h":
            hole_radius = float(arg)
        elif opt in "-p":
            plot_interval = int(arg)

    base_output_dir = get_base_output_dir()
    matlab_command = get_matlab_base_command()

    print(Fore.GREEN + 'Setup tests for porous mushy hole' + Style.RESET_ALL)
    physical_problem = 'PorousMushyHole'
    min_concentration = -5.0  # default -5
    folder_name = 'PorousMushyHole-t%s-minC%s' % (max_time, min_concentration)
    data_folder = os.path.join(base_output_dir, folder_name)

    amr_setup, num_procs = get_setup()

    # Setup up the post processing command
    python_compare_file = os.path.join(get_mushy_layer_dir(), 'test', 'run_chombo_compare.py')
    chombo_compare_analyse = 'python %s -f %s -a -v Porosity -e L2 -r True -n 8 -d %s \n \n' % (python_compare_file,
                                                                                                data_folder,
                                                                                                base_output_dir)

    make_figures = porous_hole_command(folder_name=folder_name)
    analysis_command = '%s\n\n%s" %s; exit;"' % (chombo_compare_analyse, matlab_command, make_figures)

    # Run
    extra_params = {'main.max_time': max_time,
                    'main.radius': hole_radius,
                    'bc.bulkConcentrationHiVal': [-1, min_concentration]}

    if plot_interval:
        extra_params['main.plot_interval'] = plot_interval

    run_test(data_folder, physical_problem, porous_mushy_hole_resolution_specific_params, amr_setup, num_procs,
             analysis_command, extra_params)
Example #9
0
def hele_shaw_resolution_specific_params(p):
    """
    :param p: ConvergenceTestParams
    :return:
    """
    mushy_layer_base_dir = get_mushy_layer_dir()

    params_file = mushy_layer_base_dir + '/params/convergenceTest/FixedChill.parameters'

    params = read_inputs(params_file)
    nx_coarse = -1

    grid_file = ''  # mushyLayerBaseDir + '/grids/topMiddle/' + str(nx_coarse) + 'x' + str(nz_coarse)

    return nx_coarse, params, grid_file
def porous_mushy_hole_resolution_specific_params(nz_coarse, ref_rat, max_level, max_refinement):
    mushyLayerBaseDir = get_mushy_layer_dir()
    params_file = os.path.join(mushyLayerBaseDir,'params/convergenceTest/porousMushyHole.parameters')

    params = read_inputs(params_file)


    nx_coarse = nz_coarse

    Nx_grid = nx_coarse
    if ref_rat == 4:
        Nx_grid = 2 * nx_coarse

    gridFile = mushyLayerBaseDir + '/grids/middleXSmall/' + str(Nx_grid) + 'x' + str(Nx_grid)

    # dx = 1 / float(nx_coarse)
    # dt = 5e-7 * 64.0 / float(nx_coarse)
    # Coarsest Nx is 8
    coarsest_dt = 1e-4
    coarsest_nx = 8
    dt = float(coarsest_dt) * float(coarsest_nx) / float(nx_coarse)
    params['main.fixed_dt'] = dt  # dt
    params['main.plot_period'] = float(params['main.max_time'])/4  # produce same number of plot files per run (4)

    # Check this dt will go into max_time an integer number of times
    # If it doesn't, different simulations will be integrated for different lengths of time which can be an issue
    # when we come to comparing them (they won't be the same)
    num_steps = float(params['main.max_time'])/dt
    if not num_steps.is_integer():
        print(Fore.RED + 'WARNING: dt is not an integer division of max time')

    regrid_int = int(math.ceil(float(nz_coarse) / 16.0))
    regrid_int = max(regrid_int, 1)

    params['main.refine_thresh'] = float(params['main.radius']) * 10.0 / float(nz_coarse)  # 0.05*64.0/float(nz_coarse)

    bf = max(int(nx_coarse / 4), 4)

    maxGridSize = 1024 #bf * 2
    gbs = max(bf / 8, 2)

    params['main.block_factor'] = str(bf)
    params['main.grid_buffer_size'] = gbs
    params['main.tag_buffer_size'] = 0
    params['main.regrid_interval'] = str(regrid_int) + ' ' + str(regrid_int) + ' ' + str(regrid_int)
    params['main.max_grid_size'] = str(maxGridSize)  # Must be greater than block factor

    return nx_coarse, params, gridFile
def create_refined_restart(argv):
    # Default vals:
    old_dir = ''
    new_dir = ''
    refinement = ''
    
    try:
        opts, args = getopt.getopt(argv, "n:p:r:")
    except getopt.GetoptError as err:
        print(str(err))
        print('create_refined_restart.py -n<new folder> -p<previous dir>'
              ' -r<refinement>')
        sys.exit(2)

    for opt, arg in opts:
        if opt in "-n":
            new_dir = str(arg)
        elif opt in "-p":
            old_dir = str(arg)
        elif opt in "-r":
            refinement = int(arg)
            
            
    prev_chk_file = os.path.join(old_dir, get_final_chk_file(old_dir))
    old_inputs_loc = os.path.join(old_dir, 'inputs')
    old_inputs = read_inputs(old_inputs_loc)
    new_box_size = int(old_inputs['main.max_grid_size']) * refinement
    
    out_file = os.path.join(new_dir, 'restart.2d.hdf5')


    new_inputs = {'inFile': prev_chk_file,
                  'run_inputs': old_inputs_loc,
                  'outFile': out_file,
                  'box_size': new_box_size,
                  'refinement': refinement}
                        
    new_inputs_loc = os.path.join(new_dir, 'inputsRefine')
    write_inputs(new_inputs_loc, new_inputs)
    
    # Run refine code
    exec_file = os.path.join(get_mushy_layer_dir(),
                                  'setupNewRun', get_executable('setupnewrun'))
   
    cmd = 'cd %s; %s %s' % (new_dir, exec_file, new_inputs_loc)
    print(cmd)
    os.system(cmd)
def uniform_porous_resolution_specific_params(p):
    mushy_layer_base_dir = get_mushy_layer_dir()

    nx_coarse = p.nz_coarse

    grid_file = mushy_layer_base_dir + '/grids/leftRight/' + str(
        nx_coarse) + 'x' + str(p.nz_coarse)

    params_file = mushy_layer_base_dir + '/params/convergenceTest/convectionDarcyBrinkmanConvTest.parameters'
    params = read_inputs(params_file)

    params['main.refine_thresh'] = str(3.0 / float(p.nz_coarse))
    params['main.tag_buffer_size'] = str(max(2, int(float(p.nz_coarse) / 8)))

    # Make sure we don't split up the grids as there's currently a bug in Chombo with higher order advection methods
    params['main.max_grid_size'] = int(p.nz_coarse * 2)

    return nx_coarse, params, grid_file
def test_folder(test_directory, verbose_output=False):
    """
    Run the regression test contained within a folder. Expects the folder o have the following files:
    - inputs
    - properties.json
    - at least one file ending with .expected to compare with the computed ouptut,
        e.g. pout.0.expected, or plt000010.2d.hdf5.expected
    :param test_directory:
    :return: success - if test was successful or not
    """

    logger.log('Processing folder "%s"' % test_directory)

    remove_existing_diffs_cmd = 'rm %s/diff-*' % test_directory
    logger.log('Remove existing diffs: ' + remove_existing_diffs_cmd)
    os.system(remove_existing_diffs_cmd)

    test_files = os.listdir(test_directory)

    logger.log('Initial files in folder ' + str(test_files))

    # Check the required files exist
    # if not, skip this test
    if not all(elem in test_files for elem in REQUIRED):
        test_name = test_directory.split('/')[-1]
        logger.logl('%-25s    ' % test_name, console_display=True)

        logger.log(
            '  Required files for conducting a test (%s) not found in %s' %
            (REQUIRED, test_directory))
        logger.log('  Skipping test \n')

        logger.log_void()
        return False, 'Void'

    # Load properties
    with open(os.path.join(test_directory, PROPERTIES_FILE)) as json_file:
        properties = json.load(json_file)

    # logger.log('==Running test: %s==' % properties['name'])
    logger.logl('%-25s    ' % properties['name'], console_display=True)

    try:
        mpi = subprocess.check_output(['which', 'mpiruna'])
        mpi_path = str(mpi.decode()).strip()
    except subprocess.CalledProcessError:
        mpi = None
        mpi_path = None

    # Skip if parallel test and no mpirun
    if properties['proc'] > 1 and mpi is None:
        logger.log_void()
        return False, 'Void'

    # Get correct executable
    # mushy_layer_exec = mushyLayerRunUtils.get_executable(dim=properties['dim'])
    # mushy_layer_exec_path = os.path.join(mushyLayerRunUtils.get_mushy_layer_dir(), 'execSubcycle', mushy_layer_exec)
    exec_name = 'mushyLayer%dd' % properties['dim']
    exec_dir = os.path.join(mushyLayerRunUtils.get_mushy_layer_dir(),
                            'execSubcycle')
    mushy_layer_exec_path = mushyLayerRunUtils.get_executable_name(
        exec_dir, exec_name=exec_name, return_full_path=True)

    if not os.path.exists(mushy_layer_exec_path):
        logger.log('\n**Could not find mushy layer executable: %s' %
                   mushy_layer_exec_path)
        logger.log(
            '**Have you compiled the code for the right number of dimensions?')
        logger.logl('**Use \'make all DIM=3\' to compile in 3D    ')

        logger.log_failed()
        return False, 'Failed'

    # Run test
    # try:
    if mpi is not None:
        cmd = 'cd %s; %s -np %d %s inputs' % (test_directory, mpi_path,
                                              properties['proc'],
                                              mushy_layer_exec_path)
        os.system(cmd)
        # mpi_path = 'mpirun'
        # res = subprocess.run([mpi_path, '-n ', str(properties['proc']), ' inputs'], cwd=test_directory)

    else:
        cmd = 'cd %s; %s inputs > pout.0' % (test_directory,
                                             mushy_layer_exec_path)
        logger.log(cmd)
        res = subprocess.check_output(cmd, shell=True)

        logger.logl('Response: "%s"' % str(res.decode()))

    # os.system(cmd)

    # except subprocess.CalledProcessError:
    #     logger.logl('[Exception]')
    #     logger.log_failed()
    #     return False, 'Failed'

    # Compare output against the expected output
    # logger.log('Test files: %s' % test_files)

    expected_files = [f for f in test_files if EXPECTED in f and DIFF not in f]
    if not expected_files:
        logger.log('No expected files to compared against')
        logger.log_void()
        return False, 'Void'

    failed_test = False
    for expected_file in expected_files:
        logger.log('Expected file: %s' % expected_file)

        # Check an output file to compare against exists
        test_output_filename = expected_file.replace(EXPECTED, '')
        test_output_file_path = os.path.join(test_directory,
                                             test_output_filename)
        if not os.path.exists(test_output_file_path):
            logger.log('No output file generated to compare against: %s' %
                       test_output_file_path)
            logger.log_failed()
            return False, 'Failed'

        if '.hdf5' in expected_file:
            # Do chombo compare
            # logger.log('Using chombo compare')

            # Make diffs folder if it doesn't exist
            diffs_folder = os.path.join(test_directory, DIFF_FOLDER)
            if not os.path.exists(diffs_folder):
                os.makedirs(diffs_folder)
                logger.log('Making folder %s' % diffs_folder)

            chombo_dir = os.environ['CHOMBO_HOME']
            compare_dir = os.path.join(chombo_dir, 'util', 'ChomboCompare')
            compare_exec = mushyLayerRunUtils.get_executable_name(
                compare_dir, 'compare%dd' % properties['dim'])
            compare_exec = os.path.join(compare_dir, compare_exec)
            # logger.log('Found executable %s ' % compare_exec)

            if not os.path.exists(compare_exec):
                logger.log('Could not find Chombo compare executable %s' %
                           compare_exec)
                logger.log('So cannot compare hdf5 output files')

            compare_params_file = os.path.join(test_directory,
                                               'compare.inputs')

            computed_file = os.path.join(test_directory, test_output_file_path)
            exact_file = os.path.join(test_directory, expected_file)
            error_file = os.path.join(diffs_folder,
                                      DIFF + test_output_filename)
            compare_params = {
                'compare.sameSize': 1,
                'compare.exactRoot': exact_file,
                'compare.computedRoot': computed_file,
                'compare.errorRoot': error_file,
                'compare.doPlots': 1,
                'compare.HOaverage': 0,
                'compare.no_average_var': 'T err'
            }

            mushyLayerRunUtils.write_inputs(compare_params_file,
                                            compare_params)
            cmd = 'cd %s ; %s %s > pout.0' % (diffs_folder, compare_exec,
                                              compare_params_file)

            logger.log('Executing: %s' % cmd)
            res = subprocess.check_call(cmd,
                                        shell=True,
                                        stderr=subprocess.STDOUT)
            logger.log('Compare command run, response: %s' % res)

            # Rename pout.0 in case we make lots
            old_pout_name = os.path.join(diffs_folder, 'pout.0')
            new_pout_name = os.path.join(diffs_folder,
                                         'pout-%s.0' % test_output_filename)
            # logger.log('Rename %s to %s' % (old_pout_name, new_pout_name))
            if not os.path.exists(old_pout_name):
                logger.log('Cannot find %s' % old_pout_name,
                           console_display=True)

            os.rename(old_pout_name, new_pout_name)

            # Now check the output diff
            errs = chcompare.load_error_file(new_pout_name)
            # logger.log(errs)

            for field in errs.keys():
                field_errs = errs[field]
                # logger.log(field_errs)
                for err_type in field_errs.keys():
                    this_field_err = field_errs[err_type]
                    if abs(this_field_err) > err_tolerance:
                        logger.log('Error in field %s is non-zero' % field)
                        logger.log('See %s and %s for more info' %
                                   (new_pout_name, error_file))
                        # logger.log_failed()
                        # return False, 'Failed'
                        failed_test = True

        elif '.csv' in expected_file:
            data = pd.read_csv(
                os.path.join(test_directory, test_output_file_path))
            data_expected = pd.read_csv(
                os.path.join(test_directory, expected_file))

            data_keys = data.keys()

            diff = data.copy()

            for key in data_expected.keys():
                if key not in data_keys:
                    logger.log('Key not found: %s' % key, verbose_output)
                    failed_test = True
                    break

                tolerance = 1e-5
                expected_value = data_expected[key].iloc[0]
                value_diff = float(data[key].iloc[0] - data_expected[key])

                # Compute relative different for large values
                if abs(expected_value) > 1.0:
                    value_diff = value_diff / expected_value

                diff[key] = value_diff
                if abs(value_diff) > tolerance:
                    logger.log('Error in field %s = %.2g' % (key, value_diff),
                               verbose_output)
                    failed_test = True

            diff.to_csv(os.path.join(test_directory,
                                     'diff-%s' % expected_file))

        else:
            # Assume text file - do a diff
            text1 = open(os.path.join(test_directory,
                                      expected_file)).readlines()
            text2 = open(os.path.join(test_directory,
                                      test_output_file_path)).readlines()

            text1 = filter_pout(text1)
            text2 = filter_pout(text2)

            diff = difflib.unified_diff(text1, text2)
            # diff = difflib.ndiff(text1, text2)
            differences = [line for line in diff]

            diff_out_file = os.path.join(test_directory,
                                         DIFF + test_output_filename)

            with open(diff_out_file, 'w') as diff_file:
                diff_file.writelines(differences)

            logger.log('Diff: %s' % differences)

            if differences:
                logger.log('Differences found in %s' % test_output_file_path)
                logger.log('For details, see %s' % diff_out_file)
                # logger.log('** Test failed \n')
                # logger.log_failed()
                # return False, 'Failed'
                # failed_test = True

    if failed_test:
        logger.log_failed()
        return False, 'Failed'
    else:
        logger.log_ok()
        return True, 'OK'
Example #14
0
    def __init__(self, base_output_dir, num_proc, parameters, slurm_job,
                 allow_multiple_dirs, program_name):

        self.num_proc = num_proc
        self.parameters = parameters
        self.program_name = program_name

        # Allow us to create output dirs -0, -1, -2 etc.
        # If set to false, and the output dir ending -0 already exists,
        # then we won't do the run
        self.allow_multiple_output_dirs = allow_multiple_dirs

        self.makeSlurmJob = True
        self.slurm_job = slurm_job

        self.base_output_dir = base_output_dir

        mushy_layer_dir = get_mushy_layer_dir()
        print('Mushy layer dir: %s' % mushy_layer_dir)
        self.exec_dir = os.path.join(mushy_layer_dir, 'execSubcycle')

        # Make outputDir if it doesn't exist
        if not (exists(self.base_output_dir)):
            os.makedirs(self.base_output_dir)

        # Choose max grid size sensibly
        max_level = 0
        if 'main.max_level' in parameters:
            max_level = int(parameters['main.max_level'])

        if 'main.gridfile' in parameters and max_level > 0 and os.path.exists(
                parameters['main.gridfile']):

            with open(parameters['main.gridfile'], 'r') as f:
                # all_lines = f.read()
                max_box_length = 0.0
                pattern = r'\(\((\d+),(\d+)\) \((\d+),(\d+)\) \((\d+),(\d+)\)\)'

                for line in f.readlines():
                    m = re.search(pattern, line)
                    if m:
                        # Determine size of box
                        x_lo = int(m.group(1))
                        y_lo = int(m.group(2))
                        x_hi = int(m.group(3))
                        y_hi = int(m.group(4))
                        z_lo = int(m.group(5))
                        z_hi = int(m.group(6))

                        max_box_length = max(max_box_length, y_hi + 1 - y_lo)
                        max_box_length = max(max_box_length, x_hi + 1 - x_lo)
                        max_box_length = max(max_box_length, z_hi + 1 - z_lo)

                # Make sure max_grid_size is at least as big as the largest
                # box in the specified grid
                parameters['main.max_grid_size'] = str(
                    max(int(parameters['main.max_grid_size']), max_box_length))
        elif 'main.num_cells' in parameters and 'main.max_grid_size' not in parameters:
            # want to choose max_grid_size to do parallel stuff most effectively
            # Want to choose a box size to make the parallel computations most
            # efficiently
            if not isinstance(parameters['main.num_cells'], list):
                ncells = parameters['main.num_cells'].split(' ')
                grid_res_x = int(ncells[0])
                # grid_res_y = int(ncells[0])
            else:
                grid_res_x = parameters['main.num_cells'][0]

            if math.log(grid_res_x,
                        2.0) == math.floor(math.log(grid_res_x, 2.0)):
                min_box_size = math.sqrt(grid_res_x * grid_res_x / num_proc)

                # Round this up to the nearest power of 2
                min_box_size = int(2**(math.ceil(math.log(min_box_size, 2))))
                parameters['main.max_grid_size'] = str(min_box_size)
def test_uniform_porous_convection(argv):
    # Default vals:
    cfl = get_default_cfl()
    nz_uniform = 64  # don't do uniform mesh simulations by default
    nz_vm = 64
    chi = 0.4
    bc_accuracy = 2
    u_del_u_method = 0
    advection_method = 1

    fixed_da = -1.0
    fixed_ra = -1.0

    try:
        opts, _ = getopt.getopt(argv, "n:v:c:p:b:u:a:d:r:")
    except getopt.GetoptError as err:
        print(str(err))
        print(
            'test_uniform_porous_convection.py -n<num uniform mesh grid points> -v<num variable mesh points>'
            ' -c<cfl> -p <chi> -b <BC accuracy> -u <u del u method> -a <advection method> '
            '-d<fixed darcy> -r<fixed Ra>')
        sys.exit(2)

    for opt, arg in opts:
        if opt in "-n":
            nz_uniform = int(arg)
        elif opt in "-c":
            cfl = float(arg)
        elif opt in "-v":
            nz_vm = int(arg)
        elif opt in "-p":
            chi = float(arg)
        elif opt in "-b":
            bc_accuracy = int(arg)
        elif opt in "-u":
            u_del_u_method = int(arg)
        elif opt in "-a":
            advection_method = int(arg)
        elif opt in "-d":
            fixed_da = float(arg)
        elif opt in "-r":
            fixed_ra = float(arg)

    base_output_dir = get_base_output_dir()
    matlab_command = get_matlab_base_command()

    # Check we have the executables needed to run this test
    success = check_exec_exists(
        os.path.join(get_mushy_layer_dir(), 'execSubcycle'), 'mushyLayer')
    success = success and check_exec_exists(
        os.path.join(get_mushy_layer_dir(), 'setupNewRun'), 'setupnewrun')

    if not success:
        sys.exit(-1)

    print(Fore.GREEN +
          'Setup tests for convection in a fixed uniform porous medium' +
          Style.RESET_ALL)
    physical_problem = 'convectionDB'
    # Nz_uniform = 128
    # if Nz_vm <= 0:
    #    Nz_vm = Nz_uniform #int(float(Nz_uniform) / 2)

    amr_setup = []
    if nz_uniform > 0:
        amr_setup.append({
            'max_level':
            0,
            'ref_rat':
            2,
            'run_types': ['uniform'],
            'Nzs': [int(nz_uniform / 2), nz_uniform, 2 * nz_uniform]
        })

    if nz_vm > 0:
        these_nzs = [nz_vm, 2 * nz_vm, 4 * nz_vm]
        amr_setup.append({
            'max_level': 1,
            'ref_rat': 2,
            'run_types': ['variable'],
            'Nzs': these_nzs
        })

    num_procs = [1, 1, 1]
    # chi = 0.4

    # Try and speed things up for now, should eventually make this criteria smaller
    # extra_params = {'main.steady_state': 1e-4}
    extra_params = {
        'main.BCAccuracy': bc_accuracy,
        'main.uDeluMethod': u_del_u_method,
        'main.advectionMethod': advection_method,
        'main.cfl': cfl,
        'main.initial_cfl': cfl / 10
    }

    base_data_folder = os.path.join(base_output_dir,
                                    'ConvectionDB-cfl' + str(cfl))

    da_ra_vals = [{
        'Da': 1e-6,
        'RaT': [1e7, 1e8, 1e9],
        'lebars': [1.08, 3.07, 12.9]
    }, {
        'Da': 1e-2,
        'RaT': [1e3, 1e4, 1e5, 5e5],
        'lebars': [1.01, 1.41, 3.17, 5.24]
    }]

    if fixed_da > 0.0:
        if fixed_ra > 0.0:
            da_ra_vals = [{
                'Da': fixed_da,
                'RaT': [fixed_ra],
                'lebars': [float('NaN')]
            }]
        elif fixed_da == 1e-6:
            da_ra_vals = [{
                'Da': 1e-6,
                'RaT': [1e7, 1e8, 1e9],
                'lebars': [1.08, 3.07, 12.9]
            }]
        elif fixed_da == 1e-2:
            da_ra_vals = [{
                'Da': 1e-2,
                'RaT': [1e3, 1e4, 1e5, 5e5],
                'lebars': [1.01, 1.41, 3.17, 5.24]
            }]

    # [1.01, 1.41, 3.17, 5.24];
    # [1.08, 3.07, 12.9];
    all_job_ids = []

    analysis_command = matlab_command + ' " '

    ra_format = "%1.1e"
    da_format = "%1.1e"
    chi_format = "%1.1f"

    chi_str = chi_format % chi

    for da_ra in da_ra_vals:
        da = da_ra['Da']
        da_str = da_format % da
        nu_lebars = [str(a) for a in da_ra['lebars']]

        # extra_params['main.max_time'] = 0.1 / float(Da)
        #        extra_params['main.max_dt'] = ?

        for ra in da_ra['RaT']:
            extra_params['parameters.rayleighTemp'] = ra
            extra_params['main.plot_interval'] = -1
            extra_params['main.plot_period'] = 0.05
            extra_params['main.checkpoint_interval'] = 2000

            # this is for testing purposes
            # extra_params['main.max_step'] = 10

            if chi < 1.0:
                extra_params['parameters.darcy'] = da * pow(1 - chi, 2) / pow(
                    chi, 3.0)
            else:
                extra_params['parameters.darcy'] = da

            extra_params['bc.porosityHiVal'] = str(chi) + ' ' + str(
                chi)  # 0.4 0.4
            extra_params['bc.porosityLoVal'] = str(chi) + ' ' + str(chi)

            # output_dir = 'chi' + str(chi) + '-Da' + str(Da) + '-Ra' + str(extra_params['parameters.rayleighTemp'])
            ra_str = ra_format % ra
            output_dir = "chi" + chi_str + "-Da" + da_str + "-Ra" + ra_str

            this_data_folder = os.path.join(base_data_folder, output_dir)
            job_ids = run_test(this_data_folder,
                               physical_problem,
                               uniform_porous_resolution_specific_params,
                               amr_setup,
                               num_procs,
                               '',
                               extra_params,
                               restart_from_low_res=False)

            all_job_ids = all_job_ids + job_ids

        ra_str_vals = [ra_format % a for a in da_ra['RaT']]
        ra_str = '{\'' + '\',\''.join(ra_str_vals) + '\'}'

        analysis_command += ' compileNu(\'' + base_data_folder + '\', \'' \
                            + chi_str + '\', \'' + da_str + '\', ' + ra_str + ', ' \
                            + str(nz_uniform) + ', ' + str(nz_vm) + ', [' + ','.join(nu_lebars) + ']);'

        # Now do analysis
    analysis_command = analysis_command + ' exit; " '

    run_analysis_name = 'runAnalysis.sh'

    job_name = physical_problem + '-analysis'
    s = BatchJob(base_data_folder, job_name, '')

    s.set_dependency(all_job_ids)
    s.set_custom_command(analysis_command)
    # s.write_slurm_file(run_analysis_name)
    s.run_task(run_analysis_name)
    print(Fore.GREEN + 'Submitted analysis job \n' + Fore.RESET)
Example #16
0
def fixed_porous_resolution_specific_params(p):
    """
    :param p: ConvergenceTestParams
    :return:
    """
    mushy_layer_base_dir = get_mushy_layer_dir()

    integration_time = 2e-4
    nx_coarse = p.nz_coarse

    if p.ref_rat == 2:
        grid_file = mushy_layer_base_dir + '/grids/middleXSmall/' + str(
            nx_coarse) + 'x' + str(p.nz_coarse)
    else:
        grid_file = mushy_layer_base_dir + '/grids/middleXSmall/' + str(
            nx_coarse * 2) + 'x' + str(p.nz_coarse * 2)
    # params = read_inputs(mushyLayerBaseDir + )

    params_file = mushy_layer_base_dir + '/params/convergenceTest/DBVariablePorosityConvTest.parameters'

    params = read_inputs(params_file)

    # runTypes = ['uniform', 'amr', 'variable']
    # runTypes = ['uniform', 'amr']
    # runTypes = ['uniform']
    # runTypes = ['amr']

    # For a fixed dt:
    dt = 1e-5 * float(16.0 / float(p.nz_coarse))
    num_steps = float(integration_time) / dt

    params['main.plot_interval'] = str(int(num_steps / 5.0))

    params['main.max_step'] = '100000'

    params['main.min_time'] = integration_time
    params['main.max_time'] = integration_time

    # params['main.vel_refine_thresh'] = 5.0
    # params['main.stdev'] = '0.002'

    regrid_int = int(4.0 * float(nx_coarse) / 16.0)

    # Make sure we always have one grid
    max_grid_size = max(nx_coarse * p.max_refinement, 4)

    # bf = max(maxGridSize/2,4)
    bf = max(nx_coarse * p.max_refinement / 8, 4)

    grid_buffer = 0  # max(bf/4,1)
    tag_buffer = 0  # gridBuffer

    if p.max_level == 2:
        grid_buffer = max(bf / 4, 4)

    params['main.block_factor'] = str(bf)
    params['main.grid_buffer_size'] = str(grid_buffer)
    params['main.tag_buffer_size'] = str(tag_buffer)
    params['main.regrid_interval'] = str(regrid_int) + ' ' + str(
        regrid_int) + ' ' + str(regrid_int)
    params['main.max_grid_size'] = str(
        max_grid_size)  # Must be greater than block factor

    chi = str(get_min_porosity())  # '0.05'
    params['bc.porosityHiVal'] = chi + ' ' + chi  # 0.4 0.4
    params['bc.porosityLoVal'] = chi + ' ' + chi

    params['main.fixed_dt'] = str(dt)

    return nx_coarse, params, grid_file
def testPorousMushyHole(argv):

    # Defaults:
    max_time = 1.5e-4
    hole_radius = 0.04

    try:
        opts, args = getopt.getopt(argv, "t:h:")
    except getopt.GetoptError as err:
        print(str(err))
        print('testPorousMushyHole.py -t<max time> -h<hole radius=0.02>')
        sys.exit(2)

    for opt, arg in opts:
        if opt in "-t":
            max_time = float(arg)
        elif opt in "-h":
            hole_radius = float(arg)

    base_output_dir = get_base_output_dir()
    matlab_command = get_matlab_base_command()

    print(Fore.GREEN + 'Setup tests for porous mushy hole' + Style.RESET_ALL)
    physicalProblem = 'PorousMushyHole'
    # dataFolder = os.path.join(base_output_dir, 'PorousMushyHole-t' + str(max_time) + '-hole' + str(hole_radius) )
    minC = -5.0 # default -5
    dataFolder = os.path.join(base_output_dir, 'PorousMushyHole-t%s-minC%s' % (max_time, minC))

    Nz_uniform = [16, 32, 64, 128, 256, 512]
    AMRSetup = [{'max_level': 0, 'ref_rat': 1, 'run_types': ['uniform'], 'Nzs': Nz_uniform},
                {'max_level': 1, 'ref_rat': 2, 'run_types': ['amr'], 'Nzs': [16, 32, 64, 128]},
                {'max_level': 2, 'ref_rat': 2, 'run_types': ['amr'], 'Nzs': [8, 16, 32, 64]},
                {'max_level': 1, 'ref_rat': 4, 'run_types': ['amr'], 'Nzs': [8, 16, 32, 64]}]

    # While testing:
   # Nz_uniform = [16, 32, 64]
   # AMRSetup = [{'max_level': 0, 'ref_rat': 1, 'run_types': ['uniform'], 'Nzs': Nz_uniform}]

    # Nzs 	  = [16, 32, 64]
    #num_procs = [1, 1, 1, 4, 4, 4]  # Needs to be as long as the longest Nzs
    num_procs = [1] * len(Nz_uniform)

    # Setup up the post processing command

    # figureName = os.path.join(dataFolder, 'noFlow.pdf')
    uniform_prefix = 'Uniform-' + physicalProblem + '-'

    python_compare_file = os.path.join(get_mushy_layer_dir(), 'test', 'run_chombo_compare.py')
    chombo_compare_analyse ='python %s -f %s -a -v Porosity -e L2 -r True -n 8 \n \n' % (python_compare_file, dataFolder)

    # make_figures  = 'Fig7PorousHole(\'' + dataFolder + '\', \'' + dataFolder + '\')'
    make_figures = porous_hole_command()
    #analyse_in_matlab = 'analyseVariablePorosityTest(\'' + dataFolder + '\', [' + ','.join([str(a) for a in Nz_uniform]) + '],' \
    # 'true, true, \'' + uniform_prefix + '\', \'Porosity\', \'L2\');'

    analysis_command = chombo_compare_analyse + '\n\n' + \
                       matlab_command + ' " %s; exit;"' % make_figures

    # Run
    extra_params = {'main.max_time':max_time,
                    'main.radius': hole_radius,
                    'bc.bulkConcentrationHiVal' : [-1, minC]}

    runTest(dataFolder, physicalProblem, porous_mushy_hole_resolution_specific_params,
            AMRSetup, num_procs, analysis_command, extra_params)
def amr_convergence_test(params,
                         full_output_dir,
                         physicalProblem,
                         nzs,
                         num_procs=[1],
                         num_restarts=0,
                         analysis_command='',
                         restart_from_low_res=False):

    os.environ["CH_TIMER"] = "1"

    dependencies = []
    prev_job_id = -1
    prev_dir = ''
    prev_num_cells = 0

    while len(num_procs) < len(nzs):
        num_procs.append(num_procs[-1])

    param_i = -1

    for p in params:
        param_i = param_i + 1

        num_proc = p['num_proc']

        # Don't repeat runs, unless we're restarting

        #cwd = os.getcwd()
        run_name = p['main.plot_prefix']
        test_name = run_name + get_suffix(0)
        full_path = os.path.join(full_output_dir, test_name)

        allow_restarts = False

        print(full_path)
        if os.path.exists(full_path):

            print(Fore.YELLOW + '    Run already done' + Fore.RESET)

            if num_restarts > 0:

                print('    ' + str(num_restarts) + ' restart(s) allowed \n ')
                # Find most recent folder and restart from there

                i = 0
                while os.path.exists(full_path):
                    i = i + 1
                    test_name = run_name + get_suffix(i)
                    full_path = os.path.join(full_output_dir, test_name)

                if i > num_restarts:
                    continue

                most_recent_path = os.path.join(full_output_dir,
                                                run_name + get_suffix(i - 1))
                # Now get the restart file
                restart_file = get_restart_file(most_recent_path)
                if restart_file:
                    p['main.restart_file'] = os.path.join(
                        most_recent_path, restart_file)
                    allow_restarts = True

                    print('    Set restart file: ' + p['main.restart_file'])
            else:

                continue

        else:
            print(Fore.YELLOW + '    No previous runs' + Fore.RESET)

        print(Fore.GREEN + '    **Do run**' + Fore.RESET)

        # Don't need output dir or exec dir here, MushyLayerRun will fill these in
        s = BatchJob('', p['concise_run_name'], '', num_proc)

        if restart_from_low_res:
            python_file = os.path.join(get_mushy_layer_dir(), 'test',
                                       'create_refined_restart.py')
            new_dir = full_path

            if prev_job_id > -1:

                refinement = int(
                    float(p['main.num_cells'][0]) / float(prev_num_cells[0]))

                preprocess_cmd = 'python %s -p %s -n %s -r %d \n' % (
                    python_file, prev_dir, new_dir, refinement)
                s.set_dependency(prev_job_id)
                s.set_preprocess(preprocess_cmd)
                p['main.restart_file'] = os.path.join(new_dir,
                                                      'restart.2d.hdf5')
            else:
                # Search for an old file we can restart from, just in case
                refinement = 2
                this_nx = int(p['main.num_cells'][0])
                coarse_nx = int(float(this_nx) / 2.0)
                coarser_dir = new_dir.replace(str(this_nx), str(coarse_nx))

                if os.path.exists(coarser_dir) and get_final_chk_file(
                        coarser_dir):
                    preprocess_cmd = 'python %s -p %s -n %s -r %d \n' % (
                        python_file, coarser_dir, new_dir, refinement)
                    s.set_preprocess(preprocess_cmd)
                    p['main.restart_file'] = os.path.join(
                        new_dir, 'restart.2d.hdf5')

        ml_run = MushyLayerRunSimple(full_output_dir, num_proc, p, s,
                                     allow_restarts, get_executable_name())
        ml_run.single_run(run_name)

        prev_job_id = s.job_id
        prev_dir = full_path
        prev_num_cells = p['main.num_cells']
        dependencies.append(s.job_id)

        print('==================')

    return dependencies
    return default_inputs


if __name__ == "__main__":

    # Ensure we can find HDF5 libraries to run mushy layer code
    hdf5_library_path = '/home/parkinsonjl/soft/hdf5-1.8.21p-new/lib/'
    if 'LD_LIBRARY_PATH' in os.environ:
        os.environ['LD_LIBRARY_PATH'] = os.environ[
            'LD_LIBRARY_PATH'] + ':' + hdf5_library_path
    else:
        os.environ['LD_LIBRARY_PATH'] = hdf5_library_path

    # All simulations will be put in their own sub directory within this directory:
    base_dir = os.path.join(mushyLayerRunUtils.get_mushy_layer_dir(), 'test',
                            'poiseuilleFlow', 'output')
    if not os.path.exists(base_dir):
        os.makedirs(base_dir)

    # Chose some parameter options:
    porosity_functions = [
        'Constant', 'Linear', 'Gaussian', 'GaussianSinusodial'
    ]
    permeability_functions = [
        'Pure fluid',
        'Cubic',
        'Kozeny',
        'Log',
        'Xsquare',
        'Porosity'  # set permeability = porosity
import mushyLayerRunUtils as util
import os
from valgrind_parser import ValgrindLogParser
import shutil

mem_leak_path = os.path.join(util.get_mushy_layer_dir(), 'test', 'memory_leak')
VALGRIND_LOG = os.path.join(mem_leak_path, 'valgrind_log.txt')

shutil.copy('valgrind_regexes.json', '../../env/lib/python3.6/site-packages/valgrind_parser/data/')

# Create inputs file and directory
inputs_base = os.path.join(util.get_mushy_layer_dir(), 'test', 'regression', 'AMRDarcyBrinkman', 'inputs')
inputs = util.read_inputs(inputs_base)

inputs['main.max_step'] = 5
util.write_inputs('inputs', inputs)

exec_file = util.get_executable_name(exec_name='mushyLayer2d', return_full_path=True)

# , os.path.join(mem_leak_path, 'inputs')
#  --log-file="%s"
cmd = 'valgrind --child-silent-after-fork=yes --xml=yes --xml-file=valgrind.xml --leak-check=full ' \
      '--show-reachable=yes %s inputs > %s ' % (exec_file, VALGRIND_LOG)
# print(cmd)
os.system(cmd)

vlp = ValgrindLogParser(VALGRIND_LOG, html_report_location='valgrind.html')
vlp.generate_html_report()
Example #21
0
 def test_get_mush_layer_dir(self):
     with mock.patch("os.path.abspath", return_value='/path/to/this/file/'):
         self.assertEqual(util.get_mushy_layer_dir(), '/path/to/this')