Skip to content

Commit 763079c

Browse files
authored
Merge pull request #373 from CCPBioSim/372-cache-customised-united-atom-axes
Cache customised united-atom axes topology for frame covariance
2 parents 4ddbe42 + e815cb1 commit 763079c

8 files changed

Lines changed: 1205 additions & 3 deletions

File tree

‎CodeEntropy/levels/axes.py‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,161 @@ def get_UA_axes(self, data_container, index: int):
216216

217217
return trans_axes, rot_axes, center, moment_of_inertia
218218

219+
def get_UA_axes_from_topology(
220+
self,
221+
*,
222+
u,
223+
residue_atoms,
224+
topology,
225+
box: np.ndarray | None,
226+
):
227+
"""Compute UA axes using cached static topology.
228+
229+
This is the cached-index equivalent of ``get_UA_axes``. It preserves the
230+
frame-dependent numerical calculations, but avoids repeated MDAnalysis
231+
selection strings for heavy atoms, bonded atoms, and UA masses.
232+
233+
Args:
234+
u: Current-frame universe.
235+
residue_atoms: AtomGroup for the parent residue in the current frame.
236+
topology: Cached ``UAAxesTopology`` for this UA bead.
237+
box: Current periodic box lengths. If omitted, ``u.dimensions`` is used.
238+
239+
Returns:
240+
Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
241+
- trans_axes: Translational axes, shape ``(3, 3)``.
242+
- rot_axes: Rotational axes, shape ``(3, 3)``.
243+
- center: Rotation centre, shape ``(3,)``.
244+
- moment_of_inertia: Principal moments, shape ``(3,)``.
245+
246+
Raises:
247+
ValueError: If cached bonded-axis construction fails.
248+
"""
249+
dimensions = (
250+
np.asarray(box, dtype=float)
251+
if box is not None
252+
else np.asarray(u.dimensions[:3], dtype=float)
253+
)
254+
255+
heavy_atoms = u.atoms[topology.residue_heavy_indices]
256+
heavy_atom = u.atoms[int(topology.heavy_atom_index)]
257+
258+
if len(heavy_atoms) > 1:
259+
center = residue_atoms.center_of_mass(unwrap=True)
260+
moment_of_inertia_tensor = self.get_moment_of_inertia_tensor(
261+
center_of_mass=center,
262+
positions=heavy_atoms.positions,
263+
masses=topology.residue_ua_masses,
264+
dimensions=dimensions,
265+
)
266+
trans_axes, _moment_of_inertia = self.get_custom_principal_axes(
267+
moment_of_inertia_tensor
268+
)
269+
else:
270+
make_whole(residue_atoms)
271+
trans_axes = residue_atoms.principal_axes()
272+
273+
center = heavy_atom.position
274+
rot_axes, moment_of_inertia = self.get_bonded_axes_from_topology(
275+
u=u,
276+
heavy_atom=heavy_atom,
277+
topology=topology,
278+
dimensions=dimensions,
279+
)
280+
if rot_axes is None or moment_of_inertia is None:
281+
raise ValueError("Unable to compute bonded axes for cached UA bead.")
282+
283+
logger.debug("Translational Axes: %s", trans_axes)
284+
logger.debug("Rotational Axes: %s", rot_axes)
285+
logger.debug("Center: %s", center)
286+
logger.debug("Moment of Inertia: %s", moment_of_inertia)
287+
288+
return trans_axes, rot_axes, center, moment_of_inertia
289+
290+
def get_bonded_axes_from_topology(
291+
self,
292+
*,
293+
u,
294+
heavy_atom,
295+
topology,
296+
dimensions: np.ndarray,
297+
):
298+
"""Compute UA bonded axes using cached bonded atom indices.
299+
300+
This mirrors ``get_bonded_axes`` but receives precomputed bonded atom
301+
memberships from ``UAAxesTopology`` instead of rediscovering them with
302+
MDAnalysis selection strings inside the frame loop.
303+
304+
Args:
305+
u: Current-frame universe.
306+
heavy_atom: Current-frame heavy atom for the UA bead.
307+
topology: Cached ``UAAxesTopology`` for the UA bead.
308+
dimensions: Simulation box lengths, shape ``(3,)``.
309+
310+
Returns:
311+
Tuple[np.ndarray | None, np.ndarray | None]:
312+
- custom_axes: Custom rotation axes, shape ``(3, 3)``, or ``None``.
313+
- custom_moment_of_inertia: Principal moments, shape ``(3,)``, or
314+
``None``.
315+
"""
316+
if not heavy_atom.mass > 1.1:
317+
return None, None
318+
319+
custom_moment_of_inertia = None
320+
custom_axes = None
321+
322+
heavy_bonded = u.atoms[topology.bonded_heavy_indices]
323+
light_bonded = u.atoms[topology.bonded_light_indices]
324+
ua = u.atoms[topology.ua_atom_indices]
325+
ua_all = u.atoms[topology.ua_all_atom_indices]
326+
327+
if len(heavy_bonded) == 0:
328+
custom_axes, custom_moment_of_inertia = self.get_vanilla_axes(ua_all)
329+
330+
if len(heavy_bonded) == 1 and len(light_bonded) == 0:
331+
custom_axes = self.get_custom_axes(
332+
a=heavy_atom.position,
333+
b_list=[heavy_bonded[0].position],
334+
c=np.zeros(3),
335+
dimensions=dimensions,
336+
)
337+
338+
if len(heavy_bonded) == 1 and len(light_bonded) >= 1:
339+
custom_axes = self.get_custom_axes(
340+
a=heavy_atom.position,
341+
b_list=[heavy_bonded[0].position],
342+
c=light_bonded[0].position,
343+
dimensions=dimensions,
344+
)
345+
346+
if len(heavy_bonded) >= 2:
347+
custom_axes = self.get_custom_axes(
348+
a=heavy_atom.position,
349+
b_list=heavy_bonded.positions,
350+
c=heavy_bonded[1].position,
351+
dimensions=dimensions,
352+
)
353+
354+
if custom_axes is None:
355+
return None, None
356+
357+
if custom_moment_of_inertia is None:
358+
custom_moment_of_inertia = self.get_custom_moment_of_inertia(
359+
UA=ua,
360+
custom_rotation_axes=custom_axes,
361+
center_of_mass=heavy_atom.position,
362+
dimensions=dimensions,
363+
)
364+
365+
custom_axes = self.get_flipped_axes(
366+
ua,
367+
custom_axes,
368+
heavy_atom.position,
369+
dimensions,
370+
)
371+
372+
return custom_axes, custom_moment_of_inertia
373+
219374
def get_bonded_axes(self, system, atom, dimensions: np.ndarray):
220375
r"""Compute UA rotational axes from bonded topology around a heavy atom.
221376

‎CodeEntropy/levels/level_dag.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from CodeEntropy.levels.frame_dag import FrameGraph
2020
from CodeEntropy.levels.neighbors import Neighbors
2121
from CodeEntropy.levels.nodes.accumulators import InitCovarianceAccumulatorsNode
22+
from CodeEntropy.levels.nodes.axes_topology import BuildAxesTopologyNode
2223
from CodeEntropy.levels.nodes.beads import BuildBeadsNode
2324
from CodeEntropy.levels.nodes.detect_levels import DetectLevelsNode
2425
from CodeEntropy.levels.nodes.detect_molecules import DetectMoleculesNode
@@ -49,6 +50,11 @@ def build(self) -> LevelDAG:
4950
self._add_static("detect_molecules", DetectMoleculesNode())
5051
self._add_static("detect_levels", DetectLevelsNode(), deps=["detect_molecules"])
5152
self._add_static("build_beads", BuildBeadsNode(), deps=["detect_levels"])
53+
self._add_static(
54+
"build_axes_topology",
55+
BuildAxesTopologyNode(),
56+
deps=["build_beads"],
57+
)
5258
self._add_static(
5359
"init_covariance_accumulators",
5460
InitCovarianceAccumulatorsNode(),

0 commit comments

Comments
 (0)