Skip to content

valence #

Convert SMIRNOFF valence parameters into tensors.

Functions:

convert_valence_handlers #

convert_valence_handlers(
    handlers: list[SMIRNOFFCollection],
    handler_type: str,
    parameter_cols: tuple[str, ...],
) -> tuple[TensorPotential, list[ValenceParameterMap]]

Convert a list of SMIRNOFF valence handlers into a tensor potential and associated parameter maps.

Notes

This function assumes that all parameters come from the same force field

Parameters:

  • handlers (list[SMIRNOFFCollection]) –

    The list of SMIRNOFF valence handlers to convert.

  • handler_type (str) –

    The type of valence handler being converted.

  • parameter_cols (tuple[str, ...]) –

    The ordering of the parameter array columns.

Returns:

  • tuple[TensorPotential, list[ValenceParameterMap]]

    The potential containing tensors of the parameter values, and a list of parameter maps which map the parameters to the interactions they apply to.

Source code in smee/converters/openff/valence.py
def convert_valence_handlers(
    handlers: list[openff.interchange.smirnoff.SMIRNOFFCollection],
    handler_type: str,
    parameter_cols: tuple[str, ...],
) -> tuple[smee.TensorPotential, list[smee.ValenceParameterMap]]:
    """Convert a list of SMIRNOFF valence handlers into a tensor potential and
    associated parameter maps.

    Notes:
        This function assumes that all parameters come from the same force field

    Args:
        handlers: The list of SMIRNOFF valence handlers to convert.
        handler_type: The type of valence handler being converted.
        parameter_cols: The ordering of the parameter array columns.

    Returns:
        The potential containing tensors of the parameter values, and a list of
        parameter maps which map the parameters to the interactions they apply to.
    """
    potential = smee.converters.openff._openff._handlers_to_potential(
        handlers, handler_type, parameter_cols, None
    )

    parameter_key_to_idx = {
        parameter_key: i for i, parameter_key in enumerate(potential.parameter_keys)
    }
    parameter_maps = []

    for handler in handlers:
        particle_idxs = [topology_key.atom_indices for topology_key in handler.key_map]

        assignment_matrix = torch.zeros(
            (len(particle_idxs), len(potential.parameters)), dtype=torch.float64
        )

        for i, parameter_key in enumerate(handler.key_map.values()):
            assignment_matrix[i, parameter_key_to_idx[parameter_key]] += 1.0

        parameter_map = smee.ValenceParameterMap(
            torch.tensor(particle_idxs), assignment_matrix.to_sparse()
        )
        parameter_maps.append(parameter_map)

    return potential, parameter_maps