Exemplo n.º 1
0
def run_simulation(mdu_filepath, mdu_report=None, verbose=False):
    original_dir = os.getcwd()
    os.chdir(os.path.dirname(mdu_filepath))
    if 'index.txt' in os.listdir('.'):
        mdu_report.index_lines = open('index.txt').readlines()
    logger.debug("Loading %s...", mdu_filepath)

    mdu_error = check_mdu_file(mdu_filepath)
    if mdu_error:
        # Temp check for faulty default NTimesteps value
        logger.error(mdu_error)
        mdu_report.loadable = False
        mdu_report.log = mdu_error
        mdu_report.status = SOME_ERROR
        os.chdir(original_dir)
        return

    cmd = '/opt/3di/bin/subgridf90 %s --autostartstop --nodisplay' % os.path.basename(
        mdu_filepath)
    # ^^^ TODO: hardcoded.
    logger.debug("Running %s", cmd)
    exit_code, output = system(cmd)
    last_output = ''.join(output.split('\n')[-2:]).lower()
    if verbose:
        logger.info(output)
    if exit_code or ('error' in last_output
                     and 'quitting' in last_output):
        logger.error("Loading failed: %s", mdu_filepath)
        mdu_report.loadable = False
        mdu_report.log = output
        if 'Segmentation fault' in output:
            mdu_report.status = CRASHED
        else:
            mdu_report.status = SOME_ERROR
    else:
        mdu_report.status = LOADED
        logger.info("Successfully loaded: %s", mdu_filepath)
        mdu_report.successfully_loaded_log = output
        mdu_report.model_parameters = list(model_parameters(mdu_filepath))
        csv_filenames = [f for f in os.listdir('.') if f.endswith('.csv')]
        for csv_filename in csv_filenames:
            logger.info("Reading instructions from %s", csv_filename)
            check_csv(csv_filename, mdu_report=mdu_report)

    # Cleanup: zap *.nc files.
    for nc in [f for f in os.listdir('.') if f.endswith('.nc')]:
        os.remove(nc)

    os.chdir(original_dir)
Exemplo n.º 2
0
def run_flow_simulation(model_dir, inp_report=None, verbose=False):
    """
    Run simulation using python-flow

    Params:
        model_dir: path to the model dir
        inp_report: formerly mdu_report
    """
    original_dir = os.getcwd()
    os.chdir(model_dir)
    # os.chdir("..")
    if 'index.txt' in os.listdir('.'):
        inp_report.index_lines = open('index.txt').readlines()
    logger.debug("Loading %s...", model_dir)
    buildout_dir = original_dir

    pyflow = os.path.join(buildout_dir, 'bin', 'pyflow')
    ini_files = glob.glob(os.path.join(os.path.abspath(model_dir), '*.ini'))
    if len(ini_files) != 1:
        logger.error("No or more than one ini file found. ini_files: %s",
                     ini_files)
        return
    ini_file = ini_files[0]
    ini_name = os.path.splitext(ini_file)[0]
    variant_dir = os.path.join(model_dir, ini_name)
    cmd = '%s %s -m -o debug' % (pyflow, ini_file)
    logger.debug("Running %s", cmd)
    exit_code, output = system(cmd)
    last_output = ''.join(output.split('\n')[-2:]).lower()

    if verbose:
        logger.info(output)
    if exit_code or ('error' in last_output
                     and 'quitting' in last_output):
        logger.error("Loading failed: %s", model_dir)
        inp_report.loadable = False
        inp_report.log = output
        if 'Segmentation fault' in output:
            inp_report.status = CRASHED
        else:
            inp_report.status = SOME_ERROR
    else:
        inp_report.status = LOADED
        logger.info("Successfully loaded: %s", model_dir)
        inp_report.successfully_loaded_log = output
        inp_report.input_files = input_files(model_dir)
        csv_filenames = [f for f in os.listdir('.') if f.endswith('.csv')]
        for csv_filename in csv_filenames:
            logger.info("Reading instructions from %s", csv_filename)
            netcdf_path = os.path.join(ini_name, 'results/subgrid_map.nc')
            check_csv(csv_filename, netcdf_path, mdu_report=inp_report)

    # Cleanup results
    for f in os.listdir(os.path.join(variant_dir,'results')):
        item = os.path.join(os.path.join(variant_dir,'results'), f)
        if os.path.isfile(item):
            os.remove(item)
    # Also delete makegrid files because of interference with 'hg update'
    for f in os.listdir(os.path.join(variant_dir, 'preprocessed')):
        item = os.path.join(os.path.join(variant_dir, 'preprocessed'), f)
        if os.path.isfile(item):
            os.remove(item)

    os.chdir(original_dir)
Exemplo n.º 3
0
def run_subgrid_simulation(mdu_filepath, mdu_report=None, verbose=False):
    original_dir = os.getcwd()
    os.chdir(os.path.dirname(mdu_filepath))
    if 'index.txt' in os.listdir('.'):
        mdu_report.index_lines = open('index.txt').readlines()
    logger.debug("Loading %s...", mdu_filepath)

    mdu_error = check_mdu_file(mdu_filepath)
    if mdu_error:
        # Temp check for faulty default NTimesteps value
        logger.error(mdu_error)
        mdu_report.loadable = False
        mdu_report.log = mdu_error
        mdu_report.status = SOME_ERROR
        os.chdir(original_dir)
        return

    # cmd = '/opt/3di/bin/subgridf90 %s --autostartstop --nodisplay' % os.path.basename(
    #     mdu_filepath)
    # ^^^ Direct subgrid executable call
    # Below: new via-the-library call
    buildout_dir = original_dir
    # kill_after_timeout_command = "timeout 5m"
    #subgridpy = os.path.join(buildout_dir, 'bin', 'subgridpy')
    subgridpy = os.path.join(buildout_dir, 'bin', 'simplesubgrid')
    cmd = '%s %s' % (subgridpy, os.path.basename(mdu_filepath))
    logger.debug("Running %s", cmd)
    exit_code, output = system(cmd)
    last_output = ''.join(output.split('\n')[-2:]).lower()
    if verbose:
        logger.info(output)
    if exit_code or ('error' in last_output
                     and 'quitting' in last_output):
        logger.error("Loading failed: %s", mdu_filepath)
        mdu_report.loadable = False
        mdu_report.log = output
        if 'Segmentation fault' in output:
            mdu_report.status = CRASHED
        else:
            mdu_report.status = SOME_ERROR
    else:
        mdu_report.status = LOADED
        logger.info("Successfully loaded: %s", mdu_filepath)
        mdu_report.successfully_loaded_log = output
        mdu_report.model_parameters = list(model_parameters(mdu_filepath))
        csv_filenames = [f for f in os.listdir('.') if f.endswith('.csv')]
        for csv_filename in csv_filenames:
            logger.info("Reading instructions from %s", csv_filename)
            is_his = False
            if 'his' in csv_filename:
                netcdf_path = 'subgrid_his.nc'
                is_his = True
            else:
                netcdf_path = 'subgrid_map.nc'
            check_csv(csv_filename, netcdf_path, mdu_report=mdu_report,
                      is_his=is_his)

    # Cleanup: zap *.nc files.
    for nc in [f for f in os.listdir('.') if f.endswith('.nc')]:
        os.remove(nc)

    os.chdir(original_dir)