how to apply custom force?

293 views
Skip to first unread message

Ye Zhang

unread,
Apr 7, 2022, 8:11:45 AM4/7/22
to hoomd-users
I'm using custom force in Hoomd V-3

class MyCustomForce(hoomd.md.force.Custom):
    def __init__(self):
        super().__init__(aniso=True)

    def set_forces(self,timestep):
        with self.cpu_local_force_arrays as arrays:
            arrays.force[:] += -5
custom_force = MyCustomForce()

If I run this:
custom_force.set_forces(sim.timestep)

I get the error:
How can I apply the forces?

Thanks for your help~
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [21], in <cell line: 9>()
      7             arrays.force[:] += -5
      8 custom_force = MyCustomForce()
----> 9 custom_force.set_forces(sim.timestep)

Input In [21], in MyCustomForce.set_forces(self, timestep)
      5 def set_forces(self,timestep):
----> 6     with self.cpu_local_force_arrays as arrays:
      7         arrays.force[:] += -5

File ~/miniconda3/envs/HoomdLatest/lib/python3.10/site-packages/hoomd/md/force.py:177, in Force.cpu_local_force_arrays(self)
    173 if self._in_context_manager:
    174     raise RuntimeError("Cannot enter cpu_local_force_arrays context "
    175                        "manager inside another local_force_arrays "
    176                        "context manager")
--> 177 return hoomd.md.data.ForceLocalAccess(self)

File ~/miniconda3/envs/HoomdLatest/lib/python3.10/site-packages/hoomd/md/data/local_access.py:28, in _ForceLocalAccessBase.__init__(self, force_obj)
     26 super().__init__()
     27 self._force_obj = force_obj
---> 28 self._cpp_obj = self._cpp_cls(force_obj._cpp_obj)

TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. hoomd._hoomd.LocalForceComputeDataHost(arg0: hoomd._hoomd.ForceCompute)

Invoked with: None
----------


tom...@umich.edu

unread,
Apr 7, 2022, 9:51:01 AM4/7/22
to hoomd-users
Hi,

Add the custom force to your simulation's integrator like you would any other force object in hoomd, there is no need to explicitly call the `set_forces` method. The `set_forces` method will be called in the simulation loop automatically.

-Tommy

Ye Zhang

unread,
Apr 11, 2022, 5:08:11 AM4/11/22
to hoomd-users
Thanks for your help~
Maybe it is helpful  adding  this answer to  the document for people new to python~

Joshua Anderson

unread,
Apr 11, 2022, 5:17:39 AM4/11/22
to hoomd...@googlegroups.com
Ye Zhang,

The documentation already clearly and explicitly states that forces in the `hoomd.md.Integrator.forces` list are those that are applied to the simulation. For example:
https://hoomd-blue.readthedocs.io/en/v3.0.1/package-md.html#hoomd.md.Integrator
https://hoomd-blue.readthedocs.io/en/v3.0.1/tutorial/01-Introducing-Molecular-Dynamics/01-Molecular-Dynamics-Simulations.html
------
Joshua A. Anderson, Ph.D.
Research Area Specialist, Chemical Engineering, University of Michigan
> --
> You received this message because you are subscribed to the Google Groups "hoomd-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to hoomd-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/hoomd-users/f6ecd064-9e5d-45cd-aa48-7f8abd7411ben%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages