glotzerlab / hoomd-blue

Molecular dynamics and Monte Carlo soft matter simulation on GPUs.
http://glotzerlab.engin.umich.edu/hoomd-blue
BSD 3-Clause "New" or "Revised" License
323 stars 127 forks source link

bonds don't work with plugins #1591

Closed astatt closed 11 months ago

astatt commented 12 months ago

Description

The bond python class isn't working with plugins because it is missing the string that let's you redefine where the module is implemented, see pair python class for reference. The fix should be fairly simple, see attached python snippet with proposed changes for the bond class.

I am happy to supply a bug fix.

Script

class Bond(Force):
    """Base class bond force.

    `Bond` is the base class for all bond forces.

    Warning:
        This class should not be instantiated by users. The class can be used
        for `isinstance` or `issubclass` checks.
    """

    def __init__(self):
        super().__init__()

    def _attach_hook(self):
        """Create the c++ mirror class."""
        if isinstance(self._simulation.device, hoomd.device.CPU):
            cpp_cls = getattr(_md, self._cpp_class_name)
        else:
            cpp_cls = getattr(_md, self._cpp_class_name + "GPU")

        self._cpp_obj = cpp_cls(self._simulation.state._cpp_sys_def)

changed to:

class Bond(Force):
    """Base class bond force.

    `Bond` is the base class for all bond forces.

    Warning:
        This class should not be instantiated by users. The class can be used
        for `isinstance` or `issubclass` checks.
    """

    # Module where the C++ class is defined. Reassign this when developing an
    # external plugin.
    _ext_module = _md

    def __init__(self):
        super().__init__()

    def _attach_hook(self):
        """Create the c++ mirror class."""
        if isinstance(self._simulation.device, hoomd.device.CPU):
            cpp_cls = getattr(self._ext_module, self._cpp_class_name)
        else:
            cpp_cls = getattr(self._ext_module, self._cpp_class_name + "GPU")

        self._cpp_obj = cpp_cls(self._simulation.state._cpp_sys_def)

Input files

No response

Output

When developing a plugin with a new bond potential, it will compile but then not correctly find it, i.e when wanting to use or import "plugin_name.bond.BondPotentialName" it will instead look for "hoomd.bond.BondPotentialName". The simple inclusion of "_ext_module = _md" that can be overwritten by the plugin fixes this issue.

Expected output

Plugins should be able to develop bond potentials the same way as pair potentials.

Platform

CPU, GPU, Linux, macOS

Installation method

Compiled from source

HOOMD-blue version

4.0.1

Python version

3.10

joaander commented 12 months ago

Thank you for reporting this. I would welcome a pull request implementing the _ext_module solution. Could you do the same for angles, dihedrals, impropers, and special pairs for completeness?