mythos.input.gromacs_input

GROMACS input file parser.

Handles reading and writing of GROMACS .mdp (molecular dynamics parameter) files. The format is a simple key = value format, similar to oxDNA input files.

Attributes

logger

ParamsDict

INVALID_LINE

Classes

GromacsParamsParser

Parser and parameter replacer for params in GROMACS topology files.

Functions

_parse_numeric(→ tuple[float | int, bool])

Try to parse a value as a numeric type.

_parse_boolean(→ tuple[bool, bool])

Try to parse a value as a boolean.

_parse_value(→ str | float | int | bool)

Parse a value string, handling comments and type inference.

read_mdp(→ dict[str, str | float | int | bool])

Read a GROMACS .mdp input file.

write_mdp_to(→ None)

Write a GROMACS .mdp input configuration to a file handle.

write_mdp(→ None)

Write a GROMACS .mdp input file.

update_mdp_params(→ None)

Update parameters in a GROMACS .mdp file.

read_params_from_topology(→ dict[str, ParamsDict])

Read a preprocessed GROMACS topology file.

replace_params_in_topology(→ None)

Write a modified GROMACS topology file with replaced parameters.

Module Contents

mythos.input.gromacs_input.logger
type mythos.input.gromacs_input.ParamsDict = dict[str, float]
mythos.input.gromacs_input.INVALID_LINE = 'Invalid line: {}'
mythos.input.gromacs_input._parse_numeric(value: str) tuple[float | int, bool][source]

Try to parse a value as a numeric type.

mythos.input.gromacs_input._parse_boolean(value: str) tuple[bool, bool][source]

Try to parse a value as a boolean.

mythos.input.gromacs_input._parse_value(value: str) str | float | int | bool[source]

Parse a value string, handling comments and type inference.

mythos.input.gromacs_input.read_mdp(input_file: pathlib.Path) dict[str, str | float | int | bool][source]

Read a GROMACS .mdp input file.

Parameters:

input_file – Path to the .mdp file.

Returns:

Dictionary of key-value pairs from the input file.

mythos.input.gromacs_input.write_mdp_to(input_config: dict, f: io.TextIOWrapper) None[source]

Write a GROMACS .mdp input configuration to a file handle.

Parameters:
  • input_config – Dictionary of configuration key-value pairs.

  • f – File handle to write to.

mythos.input.gromacs_input.write_mdp(input_config: dict, input_file: pathlib.Path) None[source]

Write a GROMACS .mdp input file.

Parameters:
  • input_config – Dictionary of configuration key-value pairs.

  • input_file – Path to write the .mdp file.

mythos.input.gromacs_input.update_mdp_params(mdp_file: pathlib.Path, params: dict, out_file: pathlib.Path | None = None) None[source]

Update parameters in a GROMACS .mdp file.

Parameters:
  • mdp_file – Path to the .mdp file to update.

  • params – Dictionary of parameters to update.

  • out_file – Optional path to write the updated .mdp file. By default overwrites the original file.

class mythos.input.gromacs_input.GromacsParamsParser(filename: str | pathlib.Path)[source]

Parser and parameter replacer for params in GROMACS topology files.

Reads in a preprocessed topology file, extracts parameters into structured dictionaries. When writing, it replaces parameters in the original file with values from a provided dictionary, preserving other content.

In both cases, it is important the topology file is preprocessed to in order that macros have been expanded.

file
_parser_init() None[source]
parse() dict[str, ParamsDict][source]

Parse topology content and return structured data.

Returns:

Dictionary with keys ‘nonbond_params’, ‘bond_params’, ‘angle_params’, each mapping parameter names to values.

Raises:

ValueError – If nonbond_params references unknown atom types.

replace(params: ParamsDict, output_file: str | pathlib.Path) None[source]

Write topology with replaced parameters to a new file.

Reads the original topology file and writes a new file with parameters replaced from the provided dictionary.

Parameters:
  • params – Dictionary mapping parameter names to new values. Keys should match the format from parse(): - “bond_k_MOLNAME_ATOMI_ATOMJ”, “bond_r0_MOLNAME_ATOMI_ATOMJ” - “angle_k_MOLNAME_ATOMI_ATOMJ_ATOMK”, “angle_theta0_MOLNAME_ATOMI_ATOMJ_ATOMK” - “lj_sigma_TYPE1_TYPE2”, “lj_epsilon_TYPE1_TYPE2”

  • output_file – Path to write the modified topology.

_process_line(line: str) None[source]
_handle_section_header(stripped: str) None[source]
_handle_section_data(stripped: str, original_line: str) None[source]
_handle_molecule_section_data(section: str | None, parts: list[str], original_line: str) str[source]
_handle_nonbond_params(parts: list[str], original_line: str) str[source]
mythos.input.gromacs_input.read_params_from_topology(topology_file: pathlib.Path) dict[str, ParamsDict][source]

Read a preprocessed GROMACS topology file.

This parses the [atomtypes] section to get bead types, the [nonbond_params] section for nonbonded parameters, and all [moleculetype] sections for bonds and angles.

Parameters are stored with descriptive keys: - Bonds: “bond_k_MOLNAME_ATOMI_ATOMJ” and “bond_r0_MOLNAME_ATOMI_ATOMJ” - Angles: “angle_k_MOLNAME_ATOMI_ATOMJ_ATOMK” and “angle_theta0_MOLNAME_ATOMI_ATOMJ_ATOMK” - Nonbonded: “lj_sigma_TYPE1_TYPE2” and “lj_epsilon_TYPE1_TYPE2”

Parameters:

topology_file – Path to the preprocessed topology file.

Returns:

Dictionary with keys ‘nonbond_params’, ‘bond_params’, ‘angle_params’.

mythos.input.gromacs_input.replace_params_in_topology(topology_file: pathlib.Path, params: ParamsDict, output_file: pathlib.Path) None[source]

Write a modified GROMACS topology file with replaced parameters.

Reads an existing topology file and writes a new file with parameters replaced from the provided dictionary.

Parameters:
  • topology_file – Path to the input preprocessed topology file.

  • params – Dictionary mapping parameter names to new values.

  • output_file – Path to write the modified topology.