예제 #1
0
def test_write_phantom_config():
    """Test writing Phantom config files."""
    tmp_file = pathlib.Path('tmp.in')

    conf = pc.read_config(test_phantom_file)
    conf.write_phantom(tmp_file)

    conf = pc.read_config(tmp_file)

    assert conf.config == test_data.config
    assert conf.header == test_data.header
    assert conf.datetime == test_data._datetime

    tmp_file.unlink()
def generate_files():
    for N in [1, 3]:
        pc.parameter_sweep(
            filename='dustyshock.setup',
            template=pc.read_config(FILE_DIR / f'dustyshock-N_{N}.setup'),
            parameters=PARAMETERS,
            dummy_parameters=['hfact'],
            prefix=f'N_{N}-',
            output_dir=OUTPUT_DIR,
        )
        pc.parameter_sweep(
            filename='dustyshock.in',
            template=pc.read_config(FILE_DIR / f'dustyshock-N_{N}.in'),
            parameters=PARAMETERS,
            dummy_parameters=['nx', 'smooth_fac'],
            prefix=f'N_{N}-',
            output_dir=OUTPUT_DIR,
        )
예제 #3
0
def test_add_value():
    """Testing adding, removing, modifying values."""
    conf = pc.read_config(test_phantom_file)
    conf.add_variable('new_variable', 999)
    conf.add_variable('new_variable',
                      999,
                      comment='No comment',
                      block='New block')
    assert conf.config['new_variable'].value == 999
예제 #4
0
def test_write_toml_config():
    """Test writing TOML config files."""
    tmp_file = pathlib.Path('tmp.toml')

    conf = pc.read_config(test_phantom_file)
    conf.write_toml(tmp_file)

    conf = pc.read_toml(tmp_file)

    assert conf.variables == test_data.variables
    assert conf.values == test_data.values
    assert conf.header == test_data.header
    assert conf.datetime == test_data._datetime

    tmp_file.unlink()
예제 #5
0
def add_to_header_from_infile(
    snapfile: Union[str, Path],
    infile: Union[str, Path],
    parameters: List[str] = None,
):
    """Add missing values to Phantom snapshot header.

    Some values can be missing from the Phantom snapshot header after
    conversion from the non-HDF5 file format. These values are
    available in the in-file. This function reads the infile and sets
    the values in the Phantom-HDF5 snapshot file header.

    Parameters
    ----------
    snapfile
        The path to the snapshot file.
    infile
        The path to the in-file.
    parameters : optional
        A list of strings of parameters to set in the snap header from
        the in-file.
    """
    try:
        import phantomconfig as pc
    except ModuleNotFoundError as e:
        print('phantom-config unavailable, '
              'install with python -m pip install phantomconfig')
        raise e
    if parameters is None:
        parameters = missing_infile_parameters
    config = pc.read_config(infile)
    logger.info(f'Reading parameters in {infile}')
    filename = Path(snapfile).expanduser()
    if not filename.is_file():
        raise FileNotFoundError('Cannot find snapshot file')
    file = h5py.File(filename, mode='r+')
    logger.info(f'Opening snapshot file {snapfile}')
    for param in parameters:
        print(param)
        if param in config.config:
            logger.info(f'Updating {param} from in-file')
            file[f'header/{param}'][()] = config.config[param].value
            print(param)
            print(file[f'header/{param}'][()])
    file.close()
예제 #6
0
def test_read_phantom_config():
    """Test reading Phantom config files."""
    conf = pc.read_config(test_phantom_file)
    assert conf.config == test_data.config
    assert conf.header == test_data.header
    assert conf.datetime == test_data._datetime
예제 #7
0
def test_change_variable():
    """Test changing a variable."""
    conf = pc.read_config(test_phantom_file)
    hfact_prev = conf.config['hfact'].value
    conf.change_value('hfact', 1.2)
    assert conf.config['hfact'].value != hfact_prev
예제 #8
0
def test_remove_variable():
    """Test removing a variable."""
    conf = pc.read_config(test_phantom_file)
    conf.remove_variable('dtmax')
    assert 'dtmax' not in conf.variables