Skip to main content

Phantom config

phantom-config: parse, convert, modify, and generate Phantom config files

phantom-config can read Phantom .in and .setup files. (They have the same format.) You can, for example:

  • modify config values or comment strings,
  • add new variables or delete old ones,
  • write the config to a JSON or TOML file,
  • generate a config file from a Python dictionary.

Build Status Coverage Status PyPI

Installation

Install phantom-config with pip

python -m pip install phantomconfig

Requirements

Python 3.7+ only. Optionally tomlkit for read/write to TOML format.

Usage

Basic usage

Import phantom-config.

>>> import phantomconfig

To read in a Phantom config file

>>> input_file = phantomconfig.read_config('prefix.in')

Print a summary

>>> input_file.summary()

The variables, with their values, comment string, and the block they are a member of, are stored in a dictionary accessed by the .config method.

>>> dtmax = input_file.config['dtmax']

The keys of this dictionary correspond to the variable name, and values are a ConfigVariable named tuple with the variable name, value, comment, and block.

>>> dtmax.name
>>> dtmax.value
>>> dtmax.comment
>>> dtmax.block

You can just get the value if you want.

input_file.get_value('dtmax')

If you like, you can write the Phantom config as a JSON file, and you can read the JSON file.

>>> input_file.write_json('prefix-in.json')
>>> json_file = phantomconfig.read_json('prefix-in.json')

Check that the configs are equal

>>> input_file.config == json_file.config

You can also read and write TOML files.

>>> input_file.write_toml('prefix-in.toml')
>>> toml_file = phantomconfig.read_toml('prefix-in.toml')

You can add a new variable, remove a variable, and change the value of a variable.

# Add new variable
>>> input_file.add_variable(
...     'new_var',
...     12345678,
...     comment='Sets thing',
...     block='options controlling things',
... )

# Remove a variable
>>> input_file.remove_variable('dtmax')

# Change the value of a variable
>>> input_file.change_value('dumpfile', 'new_dumpfile_name')

Then you can write the Phantom config file with the modified values.

>>> input_file.write_phantom('new.in')

Examples

Generate a config from a dictionary

You can create a Phantom .setup file from a Python dictionary. First create the dictionary

>>> setup = dict()
>>> setup['gas properties'] = {
...     'cs': (cs, 'sound speed'),
...     'npart': (npart, 'number of particles in x direction'),
...     'rhozero': (rhozero, 'initial density'),
...     'ilattice': (ilattice, 'lattice type'),
... }

Then you can read the dictionary with phantomconfig, and write to a Phantom .setup file

>>> setup_config = phantomconfig.read_dict(setup)
>>> setup_config.header = [
...     'input file for some particular setup routine',
...     'short description of what it does',
... ]
>>> setup_config.write_phantom('filename.setup')

This writes a file like

# input file for some particular setup routine
# short description of what it does

# gas properties
                  cs =        1.000   ! sound speed
               npart =         9999   ! number of particles in x direction
             rhozero =        0.100   ! initial density
            ilattice =            2   ! lattice type

Writing multiple configs

Say you want to write multiple configs, each with a different parameter value. For example, you have a template .in file and you want to vary the alpha parameter. The following

  1. reads the template file
  2. loops over a list of alpha values, writing a new .in file for each value in the list
alphas = [0.1, 0.2, 0.3]
infile = phantomconfig.read_config('template.in')

for alpha in alphas:
    infile.change_value('alpha', alpha)
    infile.write_phantom(f'alpha={alpha}.in')

See also

phantom-build

phantom-build is a Python package designed to make it easy to generate reproducible Phantom builds for writing reproducible papers. You can generate .in and .setup files with phantom-config and then, with phantom-build, you can compile Phantom and set up multiple runs, and schedule them via, for example, the Slurm job scheduler.

phantom-setup

phantom-setup is a (work in progress) Python package designed to set up Phantom initial conditions in pure Python, i.e. with no Fortran dependencies. It uses NumPy and Numba to achieve Fortran like performance for computationally expensive operations.

Metadata

Release files for phantomconfig 0.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for phantomconfig 0.3.4
File Size Uploaded
phantomconfig-0.3.4.tar.gz 27.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for phantomconfig 0.3.4
File Interpreter ABI Platform
phantomconfig-0.3.4-py3-none-any.whl Python 3 none any Details

Total release size: 54.7 kB

Release files / phantomconfig-0.3.4.tar.gz

Download URL phantomconfig-0.3.4.tar.gz
Size 27.3 kB
Tags Source
SHA-256 checksum
How to use checksums
52d576f3bd6eb5b78af692fce5c2125c97ea734b8006601b3fc162bac62b0959
BLAKE2b-256 checksum
How to use checksums
96e7c546b00160a4d26c5dc78a9eb3a10fc7f6b8ba6174b5aab489313189e368
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.4

Release files / phantomconfig-0.3.4-py3-none-any.whl

Download URL phantomconfig-0.3.4-py3-none-any.whl
Size 27.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1e438ba318dd1e6d3e7339670758c1e65048dd3295f1c922b2dea384009c28d2
BLAKE2b-256 checksum
How to use checksums
3868e3eb6595b2009d18e6a6e04cb3191360061718bd236ed80d9e5fd5d56b9e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.4

Release history Release notifications | RSS feed

This release

0.3.4 This release

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page