kups.potential.common.energy
¶
Energy computation patterns with incremental updates.
This module provides the infrastructure for converting energy functions into full potentials with gradients, Hessians, and efficient incremental updates. The key abstraction is the composition pattern that enables reusing previous computations during Monte Carlo moves.
Key components:
- EnergyFunction: Protocol for simple energy functions
- PotentialFromEnergy: Converts energy functions to full potentials
- SumComposer: Plans incremental energy updates The composition pattern allows efficient Monte Carlo by computing only energy differences rather than full recomputation (e.g., subtract old particle contribution, add new particle contribution, reuse rest).
EnergyFunction
¶
Bases: Protocol
Protocol for functions computing energy from graph inputs.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required | |
Input
|
Graph input type (e.g., GraphPotentialInput) |
required |
Source code in src/kups/potential/common/energy.py
__call__(inp)
¶
Compute energy from input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp
|
Input
|
Graph potential input |
required |
Returns:
| Type | Description |
|---|---|
EnergyAndCachePatch[State]
|
Energy and optional state patch |
IdentityComposer
¶
Bases: SumComposer[Input, Input, Patch]
Simple composer that always returns input state unchanged.
Used for potentials without incremental update support (always full recomputation).
Source code in src/kups/potential/common/energy.py
PotentialFromEnergy
¶
Converts energy functions to full potentials with gradients and Hessians.
The core building block for all potential implementations. Takes a simple energy function and automatically adds:
- Incremental updates via SumComposer
- Gradients via automatic differentiation
- Hessians via forward-on-backward differentiation
- Caching and state patches
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required | |
Input
|
Energy function input type |
required | |
Gradients
|
Gradient structure type |
required | |
Hessians
|
Hessian structure type |
required | |
StatePatch
|
Patch
|
Patch type for state updates |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
energy_fn |
EnergyFunction[State, Input]
|
Energy function to wrap |
composer |
SumComposer[State, Input, StatePatch]
|
Plans incremental updates from state and patch |
gradient_lens |
Lens[Input, Gradients]
|
Selects tensors for gradient computation |
hessian_lens |
Lens[Gradients, Hessians]
|
Selects gradients for Hessian computation |
hessian_idx_view |
View[State, Hessians]
|
Extracts Hessian row/column indices from state |
cache_lens |
Lens[State, PotentialOut[Gradients, Hessians]] | None
|
Lens to cached potential output (optional) |
patch_idx_view |
View[State, PotentialOut[Gradients, Hessians]] | None
|
Index structure for cache updates (optional) |
Gradients are computed of total energy with respect to tensors specified by
gradient_lens. Hessians are computed row-by-row via forward-on-backward mode,
with indices specified by hessian_idx_view.
Hessian indices have shape (num_calls, 2, num_entries_per_call):
- First dimension: vectorized over (parallel rows)
- Second dimension: [row_indices, column_indices]
Example - Computing 3×3 Hessian for first 3 particles:
hessian_indices = [
[[0, 0, 0], [0, 1, 2]], # ∂²E/∂x₀∂(x₀,x₁,x₂)
[[1, 1, 1], [0, 1, 2]], # ∂²E/∂x₁∂(x₀,x₁,x₂)
[[2, 2, 2], [0, 1, 2]], # ∂²E/∂x₂∂(x₀,x₁,x₂)
]
For batched systems, parallelize over both batches and rows:
# Two systems with 3 particles each (particles 0-2 and 6-8)
hessian_indices = [
[[0, 0, 0, 6, 6, 6], [0, 1, 2, 6, 7, 8]], # Rows 0 and 0
[[1, 1, 1, 7, 7, 7], [0, 1, 2, 6, 7, 8]], # Rows 1 and 1
[[2, 2, 2, 8, 8, 8], [0, 1, 2, 6, 7, 8]], # Rows 2 and 2
]
Warning - Potentially incorrect parallelization example:
# WRONG: This computes d(df/dx₀ + df/dx₁)/dx instead of separate Hessian rows
hessian_indices = [
[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]],
]
# Gives: d(df/dx₀ + df/dx₁)/dx₀, d(df/dx₀ + df/dx₁)/dx₁, d(df/dx₀ + df/dx₁)/dx₂
# Expected: d²f/dx₀², d²f/dx₀dx₁, d²f/dx₀dx₂, d²f/dx₁², d²f/dx₁dx₂, d²f/dx₂²
Each vectorized call must compute independent Hessian rows. Mixing rows from different particles in the same call causes gradient interference.
Source code in src/kups/potential/common/energy.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | |
Sum
¶
Bases: list[Summand[Input]]
Sequence of weighted configurations for incremental energy updates.
Represents a plan for computing energy changes efficiently. For example, when moving a particle: subtract old contribution (weight=-1), add new contribution (weight=1), optionally reuse cached total.
The add_previous_total flag enables reusing previous full energy calculations,
crucial for incremental updates.
Attributes:
| Name | Type | Description |
|---|---|---|
add_previous_total |
Whether to include previous total energy in plan |
Source code in src/kups/potential/common/energy.py
SumComposer
¶
Bases: Protocol
Protocol for generating incremental energy update plans.
Given a state and proposed patch, returns a sum of weighted configurations to compute efficiently. Enables O(k) updates instead of O(N) recomputation for Monte Carlo moves affecting k particles.
Example plan for moving a particle: 1. Subtract energy of old configuration (weight=-1) 2. Add energy of new configuration (weight=1) 3. Reuse previous total (add_previous_total=True)
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required | |
Input
|
Energy function input type |
required | |
StatePatch
|
Patch
|
Patch type for state updates |
required |
Source code in src/kups/potential/common/energy.py
__call__(state, patch)
¶
Generate incremental update plan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
Current simulation state |
required |
patch
|
StatePatch | None
|
Proposed changes (or None for full computation) |
required |
Returns:
| Type | Description |
|---|---|
Sum[Input]
|
Sum of weighted configurations to evaluate |
Source code in src/kups/potential/common/energy.py
Summand
¶
Bases: NamedTuple
Weighted input configuration for energy computation.
Attributes:
| Name | Type | Description |
|---|---|---|
inp |
Input
|
Input configuration |
weight |
float
|
Multiplicative weight (typically 1 or -1 for add/subtract) |