-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #146 from uppittu11/dihedral_class
Add dihedral class
- Loading branch information
Showing
8 changed files
with
332 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import warnings | ||
|
||
from topology.core.connection import Connection | ||
from topology.core.dihedral_type import DihedralType | ||
from topology.exceptions import TopologyError | ||
|
||
|
||
class Dihedral(Connection): | ||
"""A 4-partner connection between sites. | ||
Partners | ||
-------- | ||
connection_members: list of topology.Site | ||
Should be length 4 | ||
connection_type : topology.DihedralType | ||
name : name of the dihedral | ||
inherits the name attribute from Connection | ||
Notes | ||
----- | ||
Inherits some methods from Connection: | ||
__eq__, __repr__, _validate methods | ||
Addiitonal _validate methods are presented | ||
""" | ||
|
||
def __init__(self, connection_members=[], connection_type=None, name="Dihedral"): | ||
connection_members = _validate_four_partners(connection_members) | ||
connection_type = _validate_dihedraltype(connection_type) | ||
|
||
super(Dihedral, self).__init__(connection_members=connection_members, | ||
connection_type=connection_type, name=name) | ||
|
||
def __eq__(self, other): | ||
return hash(self) == hash(other) | ||
|
||
def __hash__(self): | ||
if self.connection_type: | ||
return hash( | ||
tuple( | ||
( | ||
self.name, | ||
self.connection_type, | ||
tuple(self.connection_members), | ||
) | ||
) | ||
) | ||
return hash(tuple(self.connection_members)) | ||
|
||
|
||
def _validate_four_partners(connection_members): | ||
"""Ensure 4 partners are involved in Dihedral""" | ||
if len(connection_members) != 4: | ||
raise TopologyError("Trying to create an Dihedral " | ||
"with {} connection members". format(len(connection_members))) | ||
|
||
return connection_members | ||
|
||
|
||
def _validate_dihedraltype(contype): | ||
"""Ensure connection_type is a DihedralType """ | ||
if contype is None: | ||
warnings.warn("Non-parametrized Dihedral detected") | ||
elif not isinstance(contype, DihedralType): | ||
raise TopologyError("Supplied non-DihedralType {}".format(contype)) | ||
return contype |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import warnings | ||
import unyt as u | ||
|
||
from topology.core.potential import Potential | ||
from topology.exceptions import TopologyError | ||
|
||
|
||
class DihedralType(Potential): | ||
"""A Potential between 4-bonded partners. | ||
Parameters | ||
---------- | ||
name : str | ||
expression : str or sympy.Expression | ||
See `Potential` documentation for more information | ||
parameters : dict {str, unyt.unyt_quantity} | ||
See `Potential` documentation for more information | ||
independent vars : set of str | ||
See `Potential` documentation for more information | ||
member_types : list of topology.AtomType.name (str) | ||
Notes | ||
---- | ||
Inherits many functions from topology.Potential: | ||
__eq__, _validate functions | ||
""" | ||
|
||
def __init__(self, | ||
name='DihedralType', | ||
expression='k * (1 + cos(n * phi - phi_eq))**2', | ||
parameters={ | ||
'k': 1000 * u.Unit('kJ / (deg**2)'), | ||
'phi_eq': 180 * u.deg, | ||
'n': 1*u.dimensionless | ||
}, | ||
independent_variables={'phi'}, | ||
member_types=[]): | ||
|
||
super(DihedralType, self).__init__(name=name, expression=expression, | ||
parameters=parameters, independent_variables=independent_variables) | ||
|
||
self._member_types = _validate_four_member_type_names(member_types) | ||
|
||
@property | ||
def member_types(self): | ||
return self._member_types | ||
|
||
@member_types.setter | ||
def member_types(self, val): | ||
if self.member_types != val: | ||
warnings.warn("Changing an DihedralType's constituent " | ||
"member types: {} to {}".format(self.member_types, val)) | ||
self._member_types = _validate_four_member_type_names(val) | ||
|
||
def __repr__(self): | ||
return "<DihedralType {}, id {}>".format(self.name, id(self)) | ||
|
||
def _validate_four_member_type_names(types): | ||
"""Ensure 4 partners are involved in DihedralType""" | ||
if len(types) != 4 and len(types) != 0: | ||
raise TopologyError("Trying to create an DihedralType " | ||
"with {} constituent types". format(len(types))) | ||
if not all([isinstance(t, str) for t in types]): | ||
raise TopologyError("Types passed to DihedralType " | ||
"need to be strings corresponding to AtomType names") | ||
|
||
return types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.