Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions CodeEntropy/config/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,18 @@ class ArgSpec:
default=True,
help="Use combined force-torque matrix for residue-level vibrational entropies",
),
"customised_axes": ArgSpec(
type=bool,
default=True,
help="Use bonded axes to rotate forces for united-atom vibrational entropies",
"axes": ArgSpec(
type=str,
default="local",
help="Axes to be used for residue and UA level rotations. If set to principal, "
"principal axes of residue are used for residue rotation and UA translation "
"and principal axes of UA are used for UA rotation. If set to topology, "
"axes are determined using the cached topology indices rather than repeating "
"MDAnalysis selections each frame. Residue level rotation and UA translation "
"use principal axes of the residue. UA rotation uses bond-derived axes. If set "
"to local, local level-specific custom axes are used. Residue rotation and UA "
"translation use local internal coordinate-derived axes. "
"UA rotation uses bond-derived axes.",
),
"search_type": ArgSpec(
type=str,
Expand Down Expand Up @@ -506,6 +514,18 @@ def validate_inputs(self, u: Any, args: argparse.Namespace) -> None:
self._check_input_temperature(args)
self._check_input_force_partitioning(args)
self._check_parallel_frame_options(args)
self._check_input_axes(args)

@staticmethod
def _check_input_axes(args: argparse.Namespace) -> None:
"""Check that the axes option is one of the supported values."""
valid = ("principal", "topology", "local")
axes = str(args.axes).strip().lower()
if axes not in valid:
raise ValueError(
f"Invalid 'axes': {args.axes!r}. Must be one of {', '.join(valid)}."
)
args.axes = axes

@staticmethod
def _check_input_start(u: Any, args: argparse.Namespace) -> None:
Expand Down
4 changes: 2 additions & 2 deletions CodeEntropy/levels/nodes/axes_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class BuildAxesTopologyNode:
def run(self, shared_data: dict[str, Any]) -> dict[str, Any]:
"""Build cached axes topology and write it into shared data.

The cache is only populated when ``args.customised_axes`` is true. When
The cache is only populated when ``args.axes`` is equal to ``topology``. When
customised axes are disabled, an empty cache is still written so later
stages can read ``shared_data["axes_topology"]`` safely.

Expand All @@ -97,7 +97,7 @@ def run(self, shared_data: dict[str, Any]) -> dict[str, Any]:
args = shared_data["args"]
topology = AxesTopology()

if not bool(getattr(args, "customised_axes", False)):
if args.axes != "topology":
shared_data["axes_topology"] = topology
return {"axes_topology": topology}

Expand Down
85 changes: 49 additions & 36 deletions CodeEntropy/levels/nodes/covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def run(self, ctx: FrameCtx) -> dict[str, Any]:

fp = float(args.force_partitioning)
combined = bool(getattr(args, "combined_forcetorque", False))
customised_axes = bool(getattr(args, "customised_axes", False))
axes = args.axes

box = self._try_get_box(u)
fragments = u.atoms.fragments
Expand Down Expand Up @@ -114,7 +114,7 @@ def run(self, ctx: FrameCtx) -> dict[str, Any]:
axes_topology=axes_topology,
box=box,
force_partitioning=fp,
customised_axes=customised_axes,
axes=axes,
is_highest=("united_atom" == level_list[-1]),
out_force=out_force,
out_torque=out_torque,
Expand All @@ -131,7 +131,7 @@ def run(self, ctx: FrameCtx) -> dict[str, Any]:
axes_manager=axes_manager,
axes_topology=axes_topology,
box=box,
customised_axes=customised_axes,
axes=axes,
force_partitioning=fp,
is_highest=("residue" == level_list[-1]),
out_force=out_force,
Expand Down Expand Up @@ -178,7 +178,7 @@ def _process_united_atom(
axes_topology: Any | None,
box: np.ndarray | None,
force_partitioning: float,
customised_axes: bool,
axes: str,
is_highest: bool,
out_force: dict[str, dict[Any, Matrix]],
out_torque: dict[str, dict[Any, Matrix]],
Expand All @@ -205,7 +205,7 @@ def _process_united_atom(
axes_topology: Optional cached axes topology generated during static setup.
box: Optional periodic box vector.
force_partitioning: Force partitioning factor for highest-level vectors.
customised_axes: Whether customised UA axes should be used.
axes: Which UA axes should be used.
is_highest: Whether united atom is the highest active level.
out_force: Frame-local force second-moment accumulator, mutated in place.
out_torque: Frame-local torque second-moment accumulator, mutated in place.
Expand Down Expand Up @@ -237,7 +237,7 @@ def _process_united_atom(

else:
res_position = None
residue_group = res
residue_group = res.atoms
bead_key = (mol_id, "united_atom", local_res_i)
bead_idx_list = beads.get(bead_key, [])
if not bead_idx_list:
Expand All @@ -257,7 +257,7 @@ def _process_united_atom(
axes_topology=axes_topology,
box=box,
force_partitioning=force_partitioning,
customised_axes=customised_axes,
axes=axes,
is_highest=is_highest,
res_position=res_position,
)
Expand All @@ -281,7 +281,7 @@ def _process_residue(
axes_manager: Any,
axes_topology: Any | None,
box: np.ndarray | None,
customised_axes: bool,
axes: str,
force_partitioning: float,
is_highest: bool,
out_force: dict[str, dict[Any, Matrix]],
Expand All @@ -301,7 +301,7 @@ def _process_residue(
axes_manager: Axes helper used to build translation and rotation axes.
axes_topology: Optional cached axes topology generated during static setup.
box: Optional periodic box vector.
customised_axes: Whether customised residue axes should be used.
axes: Which residue axes should be used.
force_partitioning: Force partitioning factor for highest-level vectors.
is_highest: Whether residue is the highest active level.
out_force: Frame-local force second-moment accumulator, mutated in place.
Expand All @@ -327,7 +327,7 @@ def _process_residue(
axes_manager=axes_manager,
axes_topology=axes_topology,
box=box,
customised_axes=customised_axes,
axes=axes,
force_partitioning=force_partitioning,
is_highest=is_highest,
)
Expand Down Expand Up @@ -447,7 +447,7 @@ def _build_ua_vectors(
axes_topology: Any | None,
box: np.ndarray | None,
force_partitioning: float,
customised_axes: bool,
axes: str,
is_highest: bool,
res_position: int,
) -> tuple[list[np.ndarray], list[np.ndarray]]:
Expand All @@ -463,7 +463,7 @@ def _build_ua_vectors(
axes_topology: Optional cached axes topology generated during static setup.
box: Optional periodic box vector.
force_partitioning: Force partitioning factor for highest-level vectors.
customised_axes: Whether customised UA axes should be used.
axes: Which UA axes should be used.
is_highest: Whether UA is the highest active level.
res_position: Where the residue is in the residue group

Expand All @@ -472,7 +472,6 @@ def _build_ua_vectors(
"""
force_vecs: list[np.ndarray] = []
torque_vecs: list[np.ndarray] = []

for ua_i, bead in enumerate(bead_groups):
if res_position == -1:
residue = residue_group.residues[0]
Expand All @@ -481,10 +480,9 @@ def _build_ua_vectors(
else:
residue = residue_group

if customised_axes:
ua_topology = None
if axes_topology is not None:
ua_topology = axes_topology.ua.get((mol_id, local_res_i, ua_i))
ua_topology = None
if axes == "topology":
ua_topology = axes_topology.ua.get((mol_id, local_res_i, ua_i))
if ua_topology is not None:
trans_axes, rot_axes, center, moi = (
axes_manager.get_UA_axes_from_topology(
Expand All @@ -495,21 +493,31 @@ def _build_ua_vectors(
)
)
else:
# we don't use the topology
# custom local axes if no topology
make_whole(residue_group)
make_whole(bead)
trans_axes, rot_axes, center, moi = axes_manager.get_UA_axes(
residue_group, ua_i, res_position
)
elif axes == "local":
# we don't use the topology
make_whole(residue_group)
make_whole(bead)
trans_axes, rot_axes, center, moi = axes_manager.get_UA_axes(
residue_group, ua_i, res_position
)

else:
elif axes == "principal":
# principal axes
make_whole(residue.atoms)
make_whole(bead)
trans_axes = axes_manager.get_principal_axes_from_group(residue.atoms)
rot_axes, moi = axes_manager.get_molecule_axes(bead)
center = bead.center_of_mass(unwrap=True)

else:
raise ValueError(f"Unknown axes option: {axes!r}")

force_vecs.append(
self._ft.get_weighted_forces(
bead=bead,
Expand Down Expand Up @@ -542,7 +550,7 @@ def _build_residue_vectors(
axes_manager: Any,
axes_topology: Any | None,
box: np.ndarray | None,
customised_axes: bool,
axes: str,
force_partitioning: float,
is_highest: bool,
) -> tuple[list[np.ndarray], list[np.ndarray]]:
Expand All @@ -556,7 +564,7 @@ def _build_residue_vectors(
axes_manager: Axes helper used to select axes, centres, and moments.
axes_topology: Optional cached axes topology generated during static setup.
box: Optional periodic box vector.
customised_axes: Whether customised residue axes should be used.
axes: Which residue axes that should be used.
force_partitioning: Force partitioning factor for highest-level vectors.
is_highest: Whether residue is the highest active level.

Expand All @@ -578,7 +586,7 @@ def _build_residue_vectors(
relative_res_i=relative_res_i,
axes_manager=axes_manager,
axes_topology=axes_topology,
customised_axes=customised_axes,
axes=axes,
)

force_vecs.append(
Expand Down Expand Up @@ -614,7 +622,7 @@ def _get_residue_axes(
relative_res_i: int,
axes_manager: Any,
axes_topology: Any | None,
customised_axes: bool,
axes: str,
) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
"""Return axes, centre, and inertia data for a residue bead.

Expand All @@ -628,38 +636,43 @@ def _get_residue_axes(
might differ from residue index of first residue in topology.
axes_manager: Axes helper used to select axes, centres, and moments.
axes_topology: Optional cached axes topology generated during static setup.
customised_axes: Whether customised residue axes should be used.
axes: Which residue axes should be used.

Returns:
A tuple of translation axes, rotation axes, centre, and moments of inertia.
"""
if customised_axes:
if axes == "topology" or axes == "local":
res = mol.residues[local_res_i]
residue_topology = None
if axes_topology is not None:
if axes == "topology" and axes_topology is not None:
# can use the cached axes topology
residue_topology = axes_topology.residue.get((mol_id, local_res_i))

if residue_topology is not None:
return axes_manager.get_residue_axes_from_topology(
u=u, mol=mol, residue_atoms=res.atoms, topology=residue_topology
)

# no topology or local axes argument
return axes_manager.get_residue_axes(
mol, local_res_i, relative_res_i, residue=res.atoms
)

if axes != "principal":
raise ValueError(f"Unknown axes option: {axes!r}")

make_whole(mol.atoms)
make_whole(bead)

trans_axes = axes_manager.get_principal_axes_from_group(mol.atoms)
rot_axes, moi = axes_manager.get_molecule_axes(bead)
center = bead.center_of_mass(unwrap=True)
return (
np.asarray(trans_axes),
np.asarray(rot_axes),
np.asarray(center),
np.asarray(moi),
)
if axes == "principal":
trans_axes = axes_manager.get_principal_axes_from_group(mol.atoms)
rot_axes, moi = axes_manager.get_molecule_axes(bead)
center = bead.center_of_mass(unwrap=True)
return (
np.asarray(trans_axes),
np.asarray(rot_axes),
np.asarray(center),
np.asarray(moi),
)

def _get_polymer_axes(
self,
Expand Down
12 changes: 8 additions & 4 deletions docs/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,14 @@ The ``top_traj_file`` argument is required; other arguments have default values.
2019 paper.
- ``True``
- ``bool``
* - ``--customised_axes``
- Use custom bonded axes to get COM, MOI and PA that match the 2019 paper.
- ``True``
- ``bool``
* - ``--axes``
- Axes used for residue and united-atom rotations. One of ``principal``
(principal axes of the residue/UA), ``topology`` (bonded axes using cached
topology indices, matching the 2019 paper) or ``local`` (local internal
coordinate-derived axes for residues and UA translation, bonded axes for UA
rotation).
- ``local``
- ``str``
* - ``--search_type``
- Method for finding neighbouring molecules.
- ``RAD``
Expand Down
10 changes: 5 additions & 5 deletions tests/regression/baselines/benzaldehyde/axes_off.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"groups": {
"0": {
"components": {
"united_atom:Transvibrational": 0.06950663196232491,
"united_atom:Rovibrational": 35.19655714759134,
"residue:FTmat-Transvibrational": 86.64136384477892,
"residue:FTmat-Rovibrational": 62.24303760058521,
"united_atom:Transvibrational": 0.06950663196232494,
"united_atom:Rovibrational": 35.19655714759137,
"residue:FTmat-Transvibrational": 86.64136384477902,
"residue:FTmat-Rovibrational": 62.24303760058522,
"united_atom:Conformational": 0.0,
"residue:Conformational": 0.0,
"residue:Orientational": 20.481571492615355
},
"total": 204.63203671753317
"total": 204.63203671753328
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"groups": {
"0": {
"components": {
"united_atom:Transvibrational": 0.06817494136874551,
"united_atom:Rovibrational": 49.686697381523466,
"residue:Transvibrational": 69.24977659775257,
"united_atom:Transvibrational": 0.06950663196232494,
"united_atom:Rovibrational": 49.686697381523565,
"residue:Transvibrational": 69.24977659775254,
"residue:Rovibrational": 69.73922011617867,
"united_atom:Conformational": 0.0,
"residue:Conformational": 0.0,
"residue:Orientational": 20.481571492615355
},
"total": 209.22544052943883
"total": 209.22677222003247
}
}
}
8 changes: 4 additions & 4 deletions tests/regression/baselines/benzaldehyde/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"groups": {
"0": {
"components": {
"united_atom:Transvibrational": 24.921822103314035,
"united_atom:Rovibrational": 27.950376507672594,
"residue:FTmat-Transvibrational": 71.231562004914,
"united_atom:Transvibrational": 24.94016991205995,
"united_atom:Rovibrational": 27.950376507672583,
"residue:FTmat-Transvibrational": 71.23156200491398,
"residue:FTmat-Rovibrational": 60.43602521189648,
"united_atom:Conformational": 7.522643899702263,
"residue:Conformational": 0.0,
"residue:Orientational": 59.43920971558428
},
"total": 251.50163944308366
"total": 251.51998725182955
}
}
}
Loading
Loading