CorrelationCalculator

class torch_sim.properties.correlations.CorrelationCalculator(*, window_size, properties, device, normalize=True)[source]

Bases: object

Efficient on-the-fly correlation function calculator.

Manage the calculation of time correlation functions during simulation, with support for both autocorrelation and cross-correlation of arbitrary properties. It maintains a sliding window of historical data and performs efficient updates.

Variables:
  • window_size – Number of steps to keep in memory

  • properties – Map of property names to their calculators

  • buffers – Circular buffers for storing historical data

  • correlations – Current correlation results

  • device – Device where calculations are performed

Parameters:

Example: Computing correlation function in loop:

corr_calc = CorrelationCalculator(
    window_size=100,
    properties={"velocity": lambda state: state.velocities},
)

for step in range(n_steps):
    state = integrator.step(state)
    # Call update at desired frequency
    if step % 10 == 0:  # Sample every 10 steps
        corr_calc.update(state)

    # Periodically retrieve correlation functions
    if step % 1000 == 0:
        acfs = corr_calc.get_auto_correlations()
        # Process or save acfs...
add_property(name, calculator)[source]

Track a new simulation property.

Parameters:
  • name (str) – Name of the property

  • calculator (Callable[[SimState], Tensor]) – Function that calculates property from a SimState

Return type:

None

update(state)[source]

Update correlation calculations with new state data.

Parameters:

state (SimState) – Current simulation state

Return type:

None

get_auto_correlations()[source]

Get autocorrelation results.

Returns:

Dictionary mapping property names to their correlation tensors

Return type:

dict[str, Tensor]

get_cross_correlations()[source]

Get cross-correlation results.

Returns:

Dictionary mapping pairs of property names to their cross-correlation tensors

Return type:

dict[tuple[str, str], Tensor]

reset()[source]

Reset all buffers and correlations.

Return type:

None

to(device)[source]

Move calculator to specified device.

Parameters:

device (device) – Target device

Returns:

Self, for method chaining

Return type:

CorrelationCalculator