crystal_state
Mutable crystal state for geometry optimization and perturbation propagation.
This module provides data structures that separate the immutable reference structure from the mutable positions, enabling efficient updates during geometry optimization while maintaining all symmetry relationships.
CrystalPerturbationManager
dataclass
Manager for applying and tracking perturbations to crystal structure.
This class provides a higher-level interface for geometry optimization, handling both atomic positions and (optionally) lattice parameters.
Attributes:
| Name | Type | Description |
|---|---|---|
state |
MutableCrystalState
|
The MutableCrystalState being managed |
original_asym_positions |
ndarray
|
Original positions for reference |
mol_orbit |
MoleculeOrbitTable | None
|
Optional MoleculeOrbitTable for molecular crystals |
Source code in chmpy/crystal/crystal_state.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
n_degrees_of_freedom
property
Number of independent positional degrees of freedom.
apply_displacement(displacement)
Apply a displacement to the asymmetric unit positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
displacement
|
ndarray
|
(N_asym, 3) displacement in fractional coordinates |
required |
Source code in chmpy/crystal/crystal_state.py
from_crystal(crystal, tolerance=0.01, include_molecules=True)
classmethod
Create a CrystalPerturbationManager from a Crystal object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crystal
|
Crystal
|
The crystal structure |
required |
tolerance
|
float
|
Tolerance for site merging |
0.01
|
include_molecules
|
bool
|
Whether to also build MoleculeOrbitTable |
True
|
Returns:
| Type | Description |
|---|---|
CrystalPerturbationManager
|
CrystalPerturbationManager ready for use |
Source code in chmpy/crystal/crystal_state.py
get_displacement_from_original()
Get current displacement from original positions.
Returns:
| Type | Description |
|---|---|
ndarray
|
(N_asym, 3) displacement in fractional coordinates |
Source code in chmpy/crystal/crystal_state.py
get_flat_gradient(uc_gradient)
Convert UC gradient to flat asymmetric unit gradient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uc_gradient
|
ndarray
|
(N_uc, 3) gradient on unit cell positions |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
(N_asym * 3,) flat gradient on asymmetric unit positions |
Source code in chmpy/crystal/crystal_state.py
get_flat_positions()
get_molecule_centroids()
Get current molecule centroids (recomputed if positions changed).
Returns:
| Type | Description |
|---|---|
ndarray | None
|
(N_uc_mol, 3) array of Cartesian centroids, or None if no mol_orbit |
Source code in chmpy/crystal/crystal_state.py
reset_to_original()
set_flat_positions(flat_positions)
Set asymmetric unit positions from flat array.
Source code in chmpy/crystal/crystal_state.py
MutableCrystalState
dataclass
Mutable state for a crystal structure during optimization.
This class separates the immutable reference crystal from the mutable atomic positions, allowing efficient updates during geometry optimization while preserving symmetry relationships.
Attributes:
| Name | Type | Description |
|---|---|---|
atom_orbit |
AtomOrbitTable
|
AtomOrbitTable containing symmetry mappings |
asym_frac_positions |
ndarray
|
(N_asym, 3) current fractional positions of asym atoms |
uc_frac_positions |
ndarray
|
(N_uc, 3) current fractional positions of UC atoms |
uc_cart_positions |
ndarray
|
(N_uc, 3) current Cartesian positions of UC atoms |
_to_cartesian |
callable
|
Function to convert fractional to Cartesian |
_to_fractional |
callable
|
Function to convert Cartesian to fractional |
Source code in chmpy/crystal/crystal_state.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
from_crystal(crystal, tolerance=0.01)
classmethod
Create a MutableCrystalState from a Crystal object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crystal
|
Crystal
|
The crystal structure |
required |
tolerance
|
float
|
Tolerance for site merging |
0.01
|
Returns:
| Type | Description |
|---|---|
MutableCrystalState
|
MutableCrystalState initialized with current positions |
Source code in chmpy/crystal/crystal_state.py
get_asym_gradient_from_uc_gradient(uc_gradient)
Back-propagate gradients from unit cell to asymmetric unit.
This sums contributions from all symmetry-equivalent copies to get the effective gradient on the asymmetric unit positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uc_gradient
|
ndarray
|
(N_uc, 3) gradient on unit cell positions |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
(N_asym, 3) gradient on asymmetric unit positions |
Source code in chmpy/crystal/crystal_state.py
get_uc_gradient_from_asym_gradient(asym_gradient)
Convert gradients on asymmetric unit to gradients on unit cell.
For geometry optimization, we compute gradients on the independent (asymmetric unit) coordinates. This propagates those gradients to the full unit cell positions using the symmetry operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asym_gradient
|
ndarray
|
(N_asym, 3) gradient on asymmetric unit positions |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
(N_uc, 3) gradient on unit cell positions |
Source code in chmpy/crystal/crystal_state.py
perturb_asym_position(asym_idx, delta_frac)
Perturb a single asymmetric unit atom and propagate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asym_idx
|
int
|
Index of atom to perturb |
required |
delta_frac
|
ndarray
|
(3,) perturbation in fractional coordinates |
required |
Source code in chmpy/crystal/crystal_state.py
to_dict()
Convert current state to dictionary format compatible with Crystal.unit_cell_atoms().
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with keys: asym_atom, frac_pos, cart_pos, element, |
dict
|
symop, label, occupation |
Source code in chmpy/crystal/crystal_state.py
update_asym_positions(new_asym_frac)
Update asymmetric unit positions and propagate to unit cell.
This is the key operation for geometry optimization: given new asymmetric unit positions, efficiently recompute all unit cell positions using the stored symmetry mappings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_asym_frac
|
ndarray
|
(N_asym, 3) new fractional positions |
required |