Skip to content

Commit 9652b05

Browse files
authored
Added new to_hoomd method (#212)
* Added new to_hoomd method * Updated changelog and credits * Removed working tempfiles and rolled back bumpversion * Added to_json method to base class * Added mapping functions to utils * Migrated to_hoomd functions to use internal to_json method * Updated tests * Fixed capitalization errors * Added more detail to return descriptor * Updated credits * Updated explanation for centering shapes in to_hoomd
1 parent 6dee178 commit 9652b05

16 files changed

+410
-5
lines changed

ChangeLog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Added
1313
- ``simplices``, ``equations``, and ``face_centroids`` properties for the
1414
ConvexPolyhedron class.
1515
- Additional pytests for surface area, volume, centroid, moment of inertia, and equations properties.
16+
- Added ``to_hoomd`` export method for use with simulation tools
1617

1718
Changed
1819
~~~~~~~

Credits.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Jen Bradley
123123
* Added ``simplices``, ``equations``, and ``face_centroids`` properties to the
124124
ConvexPolyhedron class.
125125
* Optimized pytest configurations for more efficient use of local and remote resources.
126+
* Added ``to_json`` and ``to_hoomd`` export methods.
126127

127128
Domagoj Fijan
128129

coxeter/shapes/base_classes.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,30 @@ def to_plato_scene(self, backend="matplotlib", scene=None, scene_kwargs=None):
197197
scene.add_primitive(prim)
198198
return scene
199199

200+
def to_json(self, attributes: list):
201+
"""Get a JSON-serializable subset of shape properties.
202+
203+
Args:
204+
attributes (list):
205+
List of attributes to export. Each element must be a valid attribute
206+
of the class.
207+
208+
Returns
209+
-------
210+
dict
211+
A dict containing the requested attributes.
212+
213+
Raises
214+
------
215+
AttributeError:
216+
If any keys in the input list are invalid.
217+
"""
218+
export = {}
219+
for attribute in attributes:
220+
# If an invalid key is passed, this will raise an attribute error
221+
export.update({attribute: getattr(self, attribute)})
222+
return export
223+
200224

201225
class Shape2D(Shape):
202226
"""An abstract representation of a shape in 2 dimensions."""

coxeter/shapes/convex_spheropolygon.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .base_classes import Shape2D
1313
from .convex_polygon import ConvexPolygon, _is_convex
1414
from .polygon import _align_points_by_normal
15+
from .utils import _hoomd_dict_mapping, _map_dict_keys
1516

1617

1718
class ConvexSpheropolygon(Shape2D):
@@ -267,3 +268,37 @@ def _plato_primitive(self, backend):
267268
vertices=verts[:, :2],
268269
radius=self.radius,
269270
)
271+
272+
def to_hoomd(self):
273+
"""Get a JSON-serializable subset of ConvexSpheropolygon properties.
274+
275+
The JSON-serializable output of the to_hoomd method can be directly imported
276+
into data management tools like signac. This data can then be queried for use in
277+
HOOMD simulations. Key naming matches HOOMD integrators: for example, the
278+
moment_inertia key links to data from coxeter's inertia_tensor. Stored values
279+
are based on the shape with its centroid at the origin.
280+
281+
For a ConvexSpheropolygon, the following properties are stored:
282+
283+
* vertices (list(list)):
284+
The vertices of the shape.
285+
* centroid (list(float))
286+
The centroid of the shape.
287+
This is set to [0,0,0] per HOOMD's spec.
288+
* sweep_radius (float):
289+
The rounding radius of the shape.
290+
* area (float)
291+
The area of the shape.
292+
293+
Returns
294+
-------
295+
dict
296+
Dict containing a subset of shape properties required for HOOMD function.
297+
"""
298+
old_centroid = self._polygon.centroid
299+
data = self.to_json(["vertices", "radius", "area"])
300+
hoomd_dict = _map_dict_keys(data, key_mapping=_hoomd_dict_mapping)
301+
hoomd_dict["centroid"] = [0, 0, 0]
302+
303+
self._polygon.centroid = old_centroid
304+
return hoomd_dict

coxeter/shapes/convex_spheropolyhedron.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from .base_classes import Shape3D
1313
from .convex_polyhedron import ConvexPolyhedron
14+
from .utils import _hoomd_dict_mapping, _map_dict_keys
1415

1516

1617
class ConvexSpheropolyhedron(Shape3D):
@@ -328,3 +329,38 @@ def _plato_primitive(self, backend):
328329
vertices=self.vertices,
329330
radius=self.radius,
330331
)
332+
333+
def to_hoomd(self):
334+
"""Get a JSON-serializable subset of ConvexSpheropolyhedron properties.
335+
336+
The JSON-serializable output of the to_hoomd method can be directly imported
337+
into data management tools like signac. This data can then be queried for use in
338+
HOOMD simulations. Key naming matches HOOMD integrators: for example, the
339+
moment_inertia key links to data from coxeter's inertia_tensor. Stored values
340+
are based on the shape with its centroid at the origin.
341+
342+
For a ConvexSpheropolyhedron, the following properties are stored:
343+
344+
* vertices (list(list)):
345+
The vertices of the shape.
346+
* centroid (list(float))
347+
The centroid of the shape.
348+
This is set to [0,0,0] per HOOMD's spec.
349+
* sweep_radius (float):
350+
The rounding radius of the shape.
351+
* volume (float)
352+
The volume of the shape.
353+
354+
Returns
355+
-------
356+
dict
357+
Dict containing a subset of shape properties required for HOOMD function.
358+
"""
359+
old_centroid = self._polyhedron.centroid
360+
self._polyhedron.centroid = np.array([0, 0, 0])
361+
data = self.to_json(["vertices", "radius", "volume"])
362+
hoomd_dict = _map_dict_keys(data, key_mapping=_hoomd_dict_mapping)
363+
hoomd_dict["centroid"] = [0, 0, 0]
364+
365+
self._polyhedron.centroid = old_centroid
366+
return hoomd_dict

coxeter/shapes/ellipsoid.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from .base_classes import Shape3D
1010
from .sphere import Sphere
11-
from .utils import translate_inertia_tensor
11+
from .utils import _hoomd_dict_mapping, _map_dict_keys, translate_inertia_tensor
1212

1313

1414
class Ellipsoid(Shape3D):
@@ -225,3 +225,41 @@ def __repr__(self):
225225
f"coxeter.shapes.Ellipsoid(a={self.a}, b={self.b}, c={self.c}, "
226226
f"center={self.centroid.tolist()})"
227227
)
228+
229+
def to_hoomd(self):
230+
"""Get a JSON-serializable subset of Ellipsoid properties.
231+
232+
The JSON-serializable output of the to_hoomd method can be directly imported
233+
into data management tools like signac. This data can then be queried for use in
234+
HOOMD simulations. Key naming matches HOOMD integrators: for example, the
235+
moment_inertia key links to data from coxeter's inertia_tensor. Stored values
236+
are based on the shape with its centroid at the origin.
237+
238+
For an Ellipsoid, the following properties are stored:
239+
240+
* a (float):
241+
half axis of ellipsoid in the x direction
242+
* b (float):
243+
half axis of ellipsoid in the y direction
244+
* c (float):
245+
half axis of ellipsoid in the z direction
246+
* centroid (list(float))
247+
The centroid of the shape.
248+
This is set to [0,0,0] per HOOMD's spec.
249+
* volume (float)
250+
The volume of the shape.
251+
* moment_inertia (list(list))
252+
The shape's inertia tensor.
253+
254+
Returns
255+
-------
256+
dict
257+
Dict containing a subset of shape properties required for HOOMD function.
258+
"""
259+
old_centroid = self.centroid
260+
self.centroid = np.array([0, 0, 0])
261+
data = self.to_json(["a", "b", "c", "centroid", "volume", "inertia_tensor"])
262+
hoomd_dict = _map_dict_keys(data, key_mapping=_hoomd_dict_mapping)
263+
264+
self.centroid = old_centroid
265+
return hoomd_dict

coxeter/shapes/polygon.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
from ..extern.polytri import polytri
1313
from .base_classes import Shape2D
1414
from .circle import Circle
15-
from .utils import _generate_ax, rotate_order2_tensor, translate_inertia_tensor
15+
from .utils import (
16+
_generate_ax,
17+
_hoomd_dict_mapping,
18+
_map_dict_keys,
19+
rotate_order2_tensor,
20+
translate_inertia_tensor,
21+
)
1622

1723
try:
1824
import miniball
@@ -761,3 +767,40 @@ def _plato_primitive(self, backend):
761767
colors=np.array([[0.5, 0.5, 0.5, 1]]),
762768
vertices=verts[:, :2],
763769
)
770+
771+
def to_hoomd(self):
772+
"""Get a JSON-serializable subset of Polygon properties.
773+
774+
The JSON-serializable output of the to_hoomd method can be directly imported
775+
into data management tools like signac. This data can then be queried for use in
776+
HOOMD simulations. Key naming matches HOOMD integrators: for example, the
777+
moment_inertia key links to data from coxeter's inertia_tensor. Stored values
778+
are based on the shape with its centroid at the origin.
779+
780+
For a Polygon or ConvexPolygon, the following properties are stored:
781+
782+
* vertices (list(list)):
783+
The vertices of the shape.
784+
* centroid (list(float))
785+
The centroid of the shape.
786+
This is set to [0,0,0] per HOOMD's spec.
787+
* sweep_radius (float):
788+
The rounding radius of the shape (0.0).
789+
* area (float)
790+
The area of the shape.
791+
* moment_inertia (list(list))
792+
The shape's inertia tensor.
793+
794+
Returns
795+
-------
796+
dict
797+
Dict containing a subset of shape properties required for HOOMD function.
798+
"""
799+
old_centroid = self.centroid
800+
self.centroid = np.array([0, 0, 0])
801+
data = self.to_json(["vertices", "centroid", "area", "inertia_tensor"])
802+
hoomd_dict = _map_dict_keys(data, key_mapping=_hoomd_dict_mapping)
803+
hoomd_dict["sweep_radius"] = 0.0
804+
805+
self.centroid = old_centroid
806+
return hoomd_dict

coxeter/shapes/polyhedron.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
from .convex_polygon import ConvexPolygon, _is_convex
1616
from .polygon import Polygon, _is_simple
1717
from .sphere import Sphere
18-
from .utils import _generate_ax, _set_3d_axes_equal, translate_inertia_tensor
18+
from .utils import (
19+
_generate_ax,
20+
_hoomd_dict_mapping,
21+
_map_dict_keys,
22+
_set_3d_axes_equal,
23+
translate_inertia_tensor,
24+
)
1925

2026
try:
2127
import miniball
@@ -973,3 +979,44 @@ def _plato_primitive(self, backend):
973979
indices=self.faces,
974980
shape_colors=np.array([[0.5, 0.5, 0.5, 1]]),
975981
)
982+
983+
def to_hoomd(self):
984+
"""Get a JSON-serializable subset of Polyhedron properties.
985+
986+
The JSON-serializable output of the to_hoomd method can be directly imported
987+
into data management tools like signac. This data can then be queried for use in
988+
HOOMD simulations. Key naming matches HOOMD integrators: for example, the
989+
moment_inertia key links to data from coxeter's inertia_tensor. Stored values
990+
are based on the shape with its centroid at the origin.
991+
992+
For a Polyhedron or ConvexPolyhedron, the following properties are stored:
993+
994+
* vertices (list(list)):
995+
The vertices of the shape.
996+
* faces (list(list)):
997+
The faces of the shape.
998+
* centroid (list(float))
999+
The centroid of the shape.
1000+
This is set to [0,0,0] per HOOMD's spec.
1001+
* sweep_radius (float):
1002+
The rounding radius of the shape (0.0).
1003+
* volume (float)
1004+
The volume of the shape.
1005+
* moment_inertia (list(list))
1006+
The shape's inertia tensor.
1007+
1008+
Returns
1009+
-------
1010+
dict
1011+
Dict containing a subset of shape properties required for HOOMD function.
1012+
"""
1013+
old_centroid = self.centroid
1014+
self.centroid = np.array([0, 0, 0])
1015+
data = self.to_json(
1016+
["vertices", "faces", "centroid", "volume", "inertia_tensor"]
1017+
)
1018+
hoomd_dict = _map_dict_keys(data, key_mapping=_hoomd_dict_mapping)
1019+
hoomd_dict["sweep_radius"] = 0.0
1020+
1021+
self.centroid = old_centroid
1022+
return hoomd_dict

coxeter/shapes/sphere.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy as np
77

88
from .base_classes import Shape3D
9-
from .utils import translate_inertia_tensor
9+
from .utils import _hoomd_dict_mapping, _map_dict_keys, translate_inertia_tensor
1010

1111

1212
class Sphere(Shape3D):
@@ -68,6 +68,18 @@ def radius(self, value):
6868
else:
6969
raise ValueError("Radius must be greater than zero.")
7070

71+
@property
72+
def diameter(self):
73+
"""float: Get or set the radius of the sphere."""
74+
return 2 * self._radius
75+
76+
@diameter.setter
77+
def diameter(self, value):
78+
if value > 0:
79+
self._radius = value / 2
80+
else:
81+
raise ValueError("Diameter must be greater than zero.")
82+
7183
def _rescale(self, scale):
7284
"""Multiply length scale.
7385
@@ -202,3 +214,37 @@ def _plato_primitive(self, backend):
202214
colors=np.array([[0.5, 0.5, 0.5, 1]]),
203215
radii=[self.radius],
204216
)
217+
218+
def to_hoomd(self):
219+
"""Get a dict of JSON-serializable subset of Sphere properties.
220+
221+
The JSON-serializable output of the to_hoomd method can be directly imported
222+
into data management tools like signac. This data can then be queried for use in
223+
HOOMD simulations. Key naming matches HOOMD integrators: for example, the
224+
moment_inertia key links to data from coxeter's inertia_tensor. Stored values
225+
are based on the shape with its centroid at the origin.
226+
227+
For a Sphere, the following properties are stored:
228+
229+
* diameter (float):
230+
The diameter of the sphere, equal to twice the radius.
231+
* centroid (list(float))
232+
The centroid of the shape.
233+
This is set to [0,0,0] per HOOMD's spec.
234+
* volume (float)
235+
The volume of the shape.
236+
* moment_inertia (list(list))
237+
The shape's inertia tensor.
238+
239+
Returns
240+
-------
241+
dict
242+
Dict containing a subset of shape properties required for HOOMD function.
243+
"""
244+
old_centroid = self.centroid
245+
self.centroid = np.array([0, 0, 0])
246+
data = self.to_json(["diameter", "centroid", "volume", "inertia_tensor"])
247+
hoomd_dict = _map_dict_keys(data, key_mapping=_hoomd_dict_mapping)
248+
249+
self.centroid = old_centroid
250+
return hoomd_dict

0 commit comments

Comments
 (0)