|
1 | 1 | """Orientational entropy calculations. |
2 | 2 |
|
3 | 3 | This module defines `OrientationalEntropy`, which computes orientational entropy |
4 | | -from a neighbor-count mapping. |
5 | | -
|
6 | | -The current implementation supports non-water neighbors. Water-specific behavior |
7 | | -can be implemented later behind an interface so the core calculation remains |
8 | | -stable and testable. |
| 4 | +from a neighbour count and symmetry information. |
9 | 5 | """ |
10 | 6 |
|
11 | 7 | from __future__ import annotations |
12 | 8 |
|
13 | 9 | import logging |
14 | 10 | import math |
15 | 11 | from dataclasses import dataclass |
16 | | -from typing import Any, Mapping |
17 | 12 |
|
18 | 13 | import numpy as np |
19 | 14 |
|
@@ -48,111 +43,77 @@ class OrientationalEntropy: |
48 | 43 |
|
49 | 44 | def __init__( |
50 | 45 | self, |
51 | | - run_manager: Any, |
52 | | - args: Any, |
53 | | - universe: Any, |
54 | | - reporter: Any, |
55 | | - group_molecules: Any, |
56 | 46 | gas_constant: float = _GAS_CONST_J_PER_MOL_K, |
57 | 47 | ) -> None: |
58 | 48 | """Initialize the orientational entropy calculator. |
59 | 49 |
|
60 | 50 | Args: |
61 | | - run_manager: Run manager (currently unused by this class). |
62 | | - args: User arguments (currently unused by this class). |
63 | | - universe: MDAnalysis Universe (currently unused by this class). |
64 | | - reporter: Data logger (currently unused by this class). |
65 | | - group_molecules: Grouping helper (currently unused by this class). |
66 | 51 | gas_constant: Gas constant in J/(mol*K). |
67 | 52 | """ |
68 | | - self._run_manager = run_manager |
69 | | - self._args = args |
70 | | - self._universe = universe |
71 | | - self._reporter = reporter |
72 | | - self._group_molecules = group_molecules |
73 | 53 | self._gas_constant = float(gas_constant) |
74 | 54 |
|
75 | | - def calculate(self, neighbours: Mapping[str, int]) -> OrientationalEntropyResult: |
76 | | - """Calculate orientational entropy from neighbor counts. |
77 | | -
|
78 | | - For each neighbor species (except water), the number of orientations is |
79 | | - estimated as: |
| 55 | + def calculate_orientational( |
| 56 | + self, |
| 57 | + neighbour_count: float, |
| 58 | + symmetry_number: int, |
| 59 | + linear: bool, |
| 60 | + ) -> OrientationalEntropyResult: |
| 61 | + """Calculate orientational entropy from neighbour counts. |
80 | 62 |
|
81 | | - Ω = sqrt(Nc^3 * π) |
| 63 | + The number of orientations is estimated as: |
| 64 | + Ω = sqrt(N_av^3 * π)/symmetry_number for non-linear molecules |
| 65 | + Ω = N_av / symmetry_number for linear molecules |
82 | 66 |
|
83 | 67 | and the entropy contribution is: |
84 | 68 |
|
85 | 69 | S = R * ln(Ω) |
86 | 70 |
|
87 | | - where Nc is the neighbor count and R is the gas constant. |
| 71 | + where N_av is the average number of neighbours and R is the gas constant. |
88 | 72 |
|
89 | 73 | Args: |
90 | | - neighbours: Mapping of neighbor species name to count. |
| 74 | + neighbours: average number of neighbours |
| 75 | + symmetry_number: symmetry number of molecule of interest |
| 76 | + linear: True if molecule of interest is linear |
91 | 77 |
|
92 | 78 | Returns: |
93 | 79 | OrientationalEntropyResult containing the total entropy in J/mol/K. |
94 | | - """ |
95 | | - total = 0.0 |
96 | | - for species, count in neighbours.items(): |
97 | | - if self._is_water(species): |
98 | | - logger.debug( |
99 | | - "Skipping water species %s in orientational entropy.", species |
100 | | - ) |
101 | | - continue |
102 | | - |
103 | | - contribution = self._entropy_contribution(count) |
104 | | - logger.debug( |
105 | | - "Orientational entropy contribution for %s: %s", species, contribution |
106 | | - ) |
107 | | - total += contribution |
108 | | - |
109 | | - logger.debug("Final orientational entropy total: %s", total) |
110 | | - return OrientationalEntropyResult(total=float(total)) |
111 | | - |
112 | | - @staticmethod |
113 | | - def _is_water(species: str) -> bool: |
114 | | - """Return True if the species should be treated as water. |
115 | | -
|
116 | | - Args: |
117 | | - species: Species identifier. |
118 | | -
|
119 | | - Returns: |
120 | | - True if the species is considered water. |
121 | | - """ |
122 | | - return species in {"H2O", "WAT", "HOH"} |
123 | | - |
124 | | - def _entropy_contribution(self, neighbour_count: int) -> float: |
125 | | - """Compute the entropy contribution for a single neighbor count. |
126 | | -
|
127 | | - Args: |
128 | | - neighbour_count: Number of neighbors (Nc). |
129 | | -
|
130 | | - Returns: |
131 | | - Entropy contribution in J/mol/K. |
132 | 80 |
|
133 | 81 | Raises: |
134 | | - ValueError: If neighbour_count is negative. |
| 82 | + ValueError if number of neighbours is negative. |
135 | 83 | """ |
136 | 84 | if neighbour_count < 0: |
137 | 85 | raise ValueError(f"neighbour_count must be >= 0, got {neighbour_count}") |
138 | 86 |
|
139 | | - if neighbour_count == 0: |
140 | | - return 0.0 |
| 87 | + omega = self._omega(neighbour_count, symmetry_number, linear) |
141 | 88 |
|
142 | | - omega = self._omega(neighbour_count) |
143 | | - if omega <= 0.0: |
144 | | - return 0.0 |
| 89 | + total = self._gas_constant * math.log(omega) |
| 90 | + logger.debug("Orientational entropy total: %s", total) |
145 | 91 |
|
146 | | - return self._gas_constant * math.log(omega) |
| 92 | + return OrientationalEntropyResult(total=float(total)) |
147 | 93 |
|
148 | 94 | @staticmethod |
149 | | - def _omega(neighbour_count: int) -> float: |
| 95 | + def _omega(neighbour_count: int, symmetry: int, linear: bool) -> float: |
150 | 96 | """Compute the number of orientations Ω. |
151 | 97 |
|
152 | 98 | Args: |
153 | | - neighbour_count: Number of neighbors (Nc). |
| 99 | + neighbour_count: average number of neighbours. |
| 100 | + symmetry_number: The symmetry number of the molecule. |
| 101 | + linear: Is the molecule linear (True or False). |
154 | 102 |
|
155 | 103 | Returns: |
156 | 104 | Ω (unitless). |
157 | 105 | """ |
158 | | - return float(np.sqrt((neighbour_count**3) * math.pi)) |
| 106 | + # symmetry number 0 = spherically symmetric = no orientational entropy |
| 107 | + if symmetry == 0: |
| 108 | + omega = 1 |
| 109 | + else: |
| 110 | + if linear: |
| 111 | + omega = neighbour_count / symmetry |
| 112 | + else: |
| 113 | + omega = np.sqrt((neighbour_count**3) * math.pi) / symmetry |
| 114 | + |
| 115 | + # avoid negative orientational entropy |
| 116 | + if omega < 1: |
| 117 | + omega = 1 |
| 118 | + |
| 119 | + return omega |
0 commit comments