Skip to content

Commit

Permalink
Merge pull request #1596 from glotzerlab/add_args_to_rattle_methods
Browse files Browse the repository at this point in the history
added `default_gamma` and `default_gamma_r` arguments to rattle methods
  • Loading branch information
joaander committed Aug 4, 2023
2 parents 749f791 + e217417 commit 3026173
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 25 deletions.
115 changes: 90 additions & 25 deletions hoomd/md/methods/rattle.py
@@ -1,7 +1,18 @@
# Copyright (c) 2009-2023 The Regents of the University of Michigan.
# Part of HOOMD-blue, released under the BSD 3-Clause License.

"""MD integration methods with manifold constraints."""
"""MD integration methods with manifold constraints.
.. invisible-code-block: python
simulation = hoomd.util.make_example_simulation()
simulation.operations.integrator = hoomd.md.Integrator(dt=0.001)
logger = hoomd.logging.Logger()
# Rename pytest's tmp_path fixture for clarity in the documentation.
path = tmp_path
"""

from hoomd.md import _md
import hoomd
Expand Down Expand Up @@ -220,6 +231,12 @@ class Langevin(MethodRATTLE):
The units of tolerance match that of the selected manifold's
implicit function. Defaults to 1e-6
default_gamma (float): Default drag coefficient for all particle types
:math:`[\mathrm{mass} \cdot \mathrm{time}^{-1}]`.
default_gamma_r ([`float`, `float`, `float`]): Default rotational drag
coefficient tensor for all particles :math:`[\mathrm{time}^{-1}]`.
.. rubric:: Translational degrees of freedom
`Langevin` uses the same integrator as `hoomd.md.methods.Langevin`, which
Expand All @@ -231,12 +248,18 @@ class Langevin(MethodRATTLE):
Use `Brownian` if your system is not underdamped.
Example::
.. rubric:: Example
sphere = hoomd.md.manifold.Sphere(r=10)
.. code-block:: python
sphere = hoomd.md.manifold.Sphere(r=5)
langevin_rattle = hoomd.md.methods.rattle.Langevin(
filter=hoomd.filter.All(), kT=0.2, manifold_constraint=sphere,
seed=1)
filter=hoomd.filter.All(),
kT=1.5,
manifold_constraint=sphere,
default_gamma=1.0,
default_gamma_r=(1.0, 1.0, 1.0))
simulation.operations.integrator.methods = [langevin_rattle]
Attributes:
filter (hoomd.filter.filter_like): Subset of particles to apply this
Expand All @@ -263,12 +286,16 @@ class Langevin(MethodRATTLE):
:math:`[\mathrm{time}^{-1}]`.
"""

def __init__(self,
filter,
kT,
manifold_constraint,
tally_reservoir_energy=False,
tolerance=0.000001):
def __init__(
self,
filter,
kT,
manifold_constraint,
tally_reservoir_energy=False,
tolerance=0.000001,
default_gamma=1.0,
default_gamma_r=(1.0, 1.0, 1.0),
):

# store metadata
param_dict = ParameterDict(
Expand All @@ -283,11 +310,13 @@ def __init__(self,
gamma = TypeParameter('gamma',
type_kind='particle_types',
param_dict=TypeParameterDict(1., len_keys=1))
gamma.default = default_gamma

gamma_r = TypeParameter('gamma_r',
type_kind='particle_types',
param_dict=TypeParameterDict((1., 1., 1.),
len_keys=1))
gamma_r.default = default_gamma_r

self._extend_typeparam([gamma, gamma_r])

Expand Down Expand Up @@ -332,6 +361,12 @@ class Brownian(MethodRATTLE):
The units of tolerance match that of the selected manifold's
implicit function. Defaults to 1e-6
default_gamma (float): Default drag coefficient for all particle types
:math:`[\mathrm{mass} \cdot \mathrm{time}^{-1}]`.
default_gamma_r ([`float`, `float`, `float`]): Default rotational drag
coefficient tensor for all particles :math:`[\mathrm{time}^{-1}]`.
`Brownian` uses the same integrator as `hoomd.md.methods.Brownian`, which
follows the overdamped Langevin equations of motion with the additional
force term :math:`- \lambda \vec{F}_\mathrm{M}`. The force
Expand All @@ -340,14 +375,18 @@ class Brownian(MethodRATTLE):
algorithm. For more details about Brownian dynamics see
`hoomd.md.methods.Brownian`.
Examples of using ``manifold_constraint``::
.. rubric:: Example
sphere = hoomd.md.manifold.Sphere(r=10)
.. code-block:: python
sphere = hoomd.md.manifold.Sphere(r=5)
brownian_rattle = hoomd.md.methods.rattle.Brownian(
filter=hoomd.filter.All(), kT=0.2, manifold_constraint=sphere,
seed=1)
integrator = hoomd.md.Integrator(dt=0.001, methods=[brownian_rattle],
forces=[lj])
filter=hoomd.filter.All(),
kT=1.5,
manifold_constraint=sphere,
default_gamma=1.0,
default_gamma_r=(1.0, 1.0, 1.0))
simulation.operations.integrator.methods = [brownian_rattle]
Attributes:
filter (hoomd.filter.filter_like): Subset of particles to apply this
Expand All @@ -374,7 +413,13 @@ class Brownian(MethodRATTLE):
:math:`[\mathrm{time}^{-1}]`.
"""

def __init__(self, filter, kT, manifold_constraint, tolerance=1e-6):
def __init__(self,
filter,
kT,
manifold_constraint,
tolerance=1e-6,
default_gamma=1.0,
default_gamma_r=(1.0, 1.0, 1.0)):

# store metadata
param_dict = ParameterDict(
Expand All @@ -389,11 +434,14 @@ def __init__(self, filter, kT, manifold_constraint, tolerance=1e-6):
gamma = TypeParameter('gamma',
type_kind='particle_types',
param_dict=TypeParameterDict(1., len_keys=1))
gamma.default = default_gamma

gamma_r = TypeParameter('gamma_r',
type_kind='particle_types',
param_dict=TypeParameterDict((1., 1., 1.),
len_keys=1))
gamma_r.default = default_gamma_r

self._extend_typeparam([gamma, gamma_r])

super().__init__(manifold_constraint, tolerance)
Expand Down Expand Up @@ -433,6 +481,12 @@ class OverdampedViscous(MethodRATTLE):
units of tolerance match that of the selected manifold's implicit
function. Defaults to 1e-6
default_gamma (float): Default drag coefficient for all particle types
:math:`[\mathrm{mass} \cdot \mathrm{time}^{-1}]`.
default_gamma_r ([`float`, `float`, `float`]): Default rotational drag
coefficient tensor for all particles :math:`[\mathrm{time}^{-1}]`.
`OverdampedViscous` uses the same integrator as
`hoomd.md.methods.OverdampedViscous`, with the additional force term
:math:`- \lambda \vec{F}_\mathrm{M}`. The force :math:`\vec{F}_\mathrm{M}`
Expand All @@ -441,14 +495,17 @@ class OverdampedViscous(MethodRATTLE):
details about overdamped viscous dynamics see
`hoomd.md.methods.OverdampedViscous`.
Examples of using ``manifold_constraint``::
.. rubric:: Example
sphere = hoomd.md.manifold.Sphere(r=10)
odv_rattle = hoomd.md.methods.rattle.OverdampedViscous(
filter=hoomd.filter.All(), manifold_constraint=sphere, seed=1)
integrator = hoomd.md.Integrator(
dt=0.001, methods=[odv_rattle], forces=[lj])
.. code-block:: python
sphere = hoomd.md.manifold.Sphere(r=5)
odv_rattle = hoomd.md.methods.rattle.OverdampedViscous(
filter=hoomd.filter.All(),
manifold_constraint=sphere,
default_gamma=1.0,
default_gamma_r=(1.0, 1.0, 1.0))
simulation.operations.integrator.methods = [odv_rattle]
Attributes:
filter (hoomd.filter.filter_like): Subset of particles to apply this
Expand All @@ -472,7 +529,12 @@ class OverdampedViscous(MethodRATTLE):
:math:`[\mathrm{time}^{-1}]`.
"""

def __init__(self, filter, manifold_constraint, tolerance=1e-6):
def __init__(self,
filter,
manifold_constraint,
tolerance=1e-6,
default_gamma=1.0,
default_gamma_r=(1.0, 1.0, 1.0)):
# store metadata
param_dict = ParameterDict(filter=ParticleFilter,)
param_dict.update(dict(filter=filter))
Expand All @@ -483,11 +545,14 @@ def __init__(self, filter, manifold_constraint, tolerance=1e-6):
gamma = TypeParameter('gamma',
type_kind='particle_types',
param_dict=TypeParameterDict(1., len_keys=1))
gamma.default = default_gamma

gamma_r = TypeParameter('gamma_r',
type_kind='particle_types',
param_dict=TypeParameterDict((1., 1., 1.),
len_keys=1))
gamma_r.default = default_gamma_r

self._extend_typeparam([gamma, gamma_r])

super().__init__(manifold_constraint, tolerance)
Expand Down
11 changes: 11 additions & 0 deletions hoomd/md/pytest/test_methods.py
Expand Up @@ -328,6 +328,17 @@ def test_constant_pressure_attributes_attached_2d(
'kT': 1.5
}),
(hoomd.md.methods.OverdampedViscous, {}),
(hoomd.md.methods.rattle.Brownian, {
'kT': 1.5,
'manifold_constraint': hoomd.md.manifold.Sphere(r=10)
}),
(hoomd.md.methods.rattle.Langevin, {
'kT': 1.5,
'manifold_constraint': hoomd.md.manifold.Sphere(r=10)
}),
(hoomd.md.methods.rattle.OverdampedViscous, {
'manifold_constraint': hoomd.md.manifold.Sphere(r=10)
}),
])
def test_default_gamma(self, cls, init_args):
c = cls(filter=hoomd.filter.All(), **init_args)
Expand Down

0 comments on commit 3026173

Please sign in to comment.