Skip to content

Commit

Permalink
Add gaff support in hoomd by fixing iterable periodic dihedrals. Rela…
Browse files Browse the repository at this point in the history
…x uniqueness in gmso xsd for bonded types.
  • Loading branch information
CalCraven committed Sep 17, 2023
1 parent 4bb656e commit abce90c
Show file tree
Hide file tree
Showing 6 changed files with 47,419 additions and 165 deletions.
97 changes: 73 additions & 24 deletions gmso/external/convert_hoomd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Convert GMSO Topology to GSD snapshot."""
from __future__ import division

import copy

Check notice

Code scanning / CodeQL

Module is imported with 'import' and 'import from' Note library

Module 'copy' is imported with both 'import' and 'import from'.
import itertools
import json
import re
Expand Down Expand Up @@ -1126,45 +1127,91 @@ def _parse_dihedral_forces(
if int(hoomd_version[0]) >= 4 or (
int(hoomd_version[0]) == 3 and int(hoomd_version[1]) >= 8
):
dtype_group_map["PeriodicTorsionPotential"] = (
{
"container": hoomd.md.dihedral.Periodic,
"parser": _parse_periodic_dihedral,
},
)
v_hoomd = "gt3.8"
dtype_group_map["PeriodicTorsionPotential"] = {
"container": hoomd.md.dihedral.Periodic,
"parser": _parse_periodic_dihedral,
}

else:
v_hoomd = "lt3.8"
# Should this be periodic, deprecated starting from 3.8.0
dtype_group_map["PeriodicTorsionPotential"] = (
{
"container": hoomd.md.dihedral.Harmonic,
"parser": _parse_periodic_dihedral,
},
)
dtype_group_map["PeriodicTorsionPotential"] = {
"container": hoomd.md.dihedral.Harmonic,
"parser": _parse_periodic_dihedral,
}

dihedral_forces = list()
for group in groups:
dihedral_forces.append(
dtype_group_map[group]["parser"](
container=dtype_group_map[group]["container"](),
dtypes=groups[group],
container = dtype_group_map[group]["container"]
if isinstance(container(), hoomd.md.dihedral.OPLS):
dihedral_forces.append(
dtype_group_map[group]["parser"](
container=container(),
dtypes=groups[group],
)
)
elif v_hoomd == "gt3.8" and isinstance(
container(), hoomd.md.dihedral.Periodic
):
dihedral_forces.extend(
dtype_group_map[group]["parser"](
container=container,
dtypes=groups[group],
)
)
elif v_hoomd == "lt3.8" and isinstance(
container(), hoomd.md.dihedral.Harmonic
):
dihedral_forces.extend(
dtype_group_map[group]["parser"](
container=container,
dtypes=groups[group],
)
)
)
return dihedral_forces


def _parse_periodic_dihedral(
container,
dtypes,
):
containersList = []
for _ in range(5):
containersList.append(copy.deepcopy(container)())
for dtype in dtypes:
member_types = sort_member_types(dtype)
container.params["-".join(member_types)] = {
"k": dtype.parameters["k"],
"d": 1,
"n": dtype.parameters["n"],
"phi0": dtype.parameters["phi_eq"],
}
return container
if isinstance(dtype.parameters["k"], u.array.unyt_quantity):
containersList[0].params["-".join(member_types)] = {
"k": dtype.parameters["k"].to_value(),
"d": 1,
"n": dtype.parameters["n"].to_value(),
"phi0": dtype.parameters["phi_eq"].to_value(),
}
elif isinstance(dtype.parameters["k"], u.array.unyt_array):
paramsLen = len(dtype.parameters["k"])
for nIndex in range(paramsLen):
containersList[nIndex].params["-".join(member_types)] = {
"k": dtype.parameters["k"].to_value()[nIndex],
"d": 1,
"n": dtype.parameters["n"].to_value()[nIndex],
"phi0": dtype.parameters["phi_eq"].to_value()[nIndex],
}
filled_containersList = []
for i in range(5): # take only periodic terms that have parameters
if len(tuple(containersList[i].params.keys())) == 0:
continue
# add in extra parameters
for key in containersList[0].params.keys():
if key not in containersList[i].params.keys():
containersList[i].params[key] = {
"k": 0,
"d": 1,
"n": 0,
"phi0": 0,
}
filled_containersList.append(containersList[i])
return filled_containersList


def _parse_opls_dihedral(
Expand Down Expand Up @@ -1370,6 +1417,8 @@ def _validate_base_units(base_units, top, auto_scale, potential_types=None):
base_units[key] = 1 * base_units[key]
# Add angle unit (since HOOMD will use radian across the board)
base_units["angle"] = 1 * u.radian
# add dimensionless handling
base_units["dimensionless"] = 1 * u.dimensionless

Check failure

Code scanning / CodeQL

Modification of parameter with default Error library

This expression mutates a
default value
.
This expression mutates a
default value
.

return base_units

Expand Down
8 changes: 8 additions & 0 deletions gmso/tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,3 +677,11 @@ def harmonic_parmed_types_charmm(self):
assert_improper_params=False,
)
return struc

@pytest.fixture
def gaff_forcefield(self):
return ForceField(get_fn("gmso_xmls/test_ffstyles/gaff.xml"))

@pytest.fixture
def oplsaa_forcefield(self):
return ForceField("oplsaa")
Loading

0 comments on commit abce90c

Please sign in to comment.