|
6 | 6 | import numpy as np |
7 | 7 |
|
8 | 8 | 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 |
10 | 10 |
|
11 | 11 |
|
12 | 12 | class Sphere(Shape3D): |
@@ -68,6 +68,18 @@ def radius(self, value): |
68 | 68 | else: |
69 | 69 | raise ValueError("Radius must be greater than zero.") |
70 | 70 |
|
| 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 | + |
71 | 83 | def _rescale(self, scale): |
72 | 84 | """Multiply length scale. |
73 | 85 |
|
@@ -202,3 +214,37 @@ def _plato_primitive(self, backend): |
202 | 214 | colors=np.array([[0.5, 0.5, 0.5, 1]]), |
203 | 215 | radii=[self.radius], |
204 | 216 | ) |
| 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