Energy
energy
Energy diagnostics for field dynamics.
This module provides functions to compute and track various energy components:
- Kinetic energy: \(\frac{1}{2} \langle \dot{\phi}^2 \rangle\)
- Gradient energy: \(\frac{1}{2} \langle (\nabla \phi)^2 \rangle\)
- Potential energy: \(\langle V(\phi) \rangle\)
- Total energy and conservation checking
Both volume-averaged and volume-integrated functions are provided.
For expanding universe simulations, provides physical energy densities with proper scale factor scaling for Friedmann equation integration.
Inspired by CosmoLattice's energy measurement framework.
EnergyTracker()
Track energy evolution and conservation over time.
This class stores energy measurements at each timestep and provides methods to analyze energy conservation.
Attributes:
| Name | Type | Description |
|---|---|---|
times |
List of measurement times |
|
energies |
List of energy component dictionaries |
Example
from jaxlatt.potentials import quadratic_potential
V = quadratic_potential(m=1.0)
tracker = EnergyTracker()
# During evolution
for t, snapshot in zip(times, snapshots):
tracker.add_measurement(t, snapshot, V)
# Check conservation
conservation = tracker.energy_conservation()
print(f"Energy drift: {conservation['relative_change']:.2e}")
Initialize an empty energy tracker.
Creates storage for simulation times and energy-component snapshots.
Source code in jaxlatt/observables/energy.py
add_measurement(time, lattice, potential_func)
Add energy measurement at given time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
float
|
Current simulation time |
required |
lattice
|
Lattice
|
Current lattice state |
required |
potential_func
|
Callable
|
Potential function |
required |
Source code in jaxlatt/observables/energy.py
get_component(component)
Get time series of a specific energy component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component
|
str
|
Energy component name ('kinetic', 'gradient', 'potential', or 'total') |
required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
Tuple of (times, energy_values) as numpy arrays |
Source code in jaxlatt/observables/energy.py
energy_conservation()
Analyze energy conservation.
Computes various metrics to assess how well energy is conserved during the simulation.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary with: - 'initial': Initial total energy - 'final': Final total energy - 'mean': Mean total energy - 'std': Standard deviation - 'relative_change': (E_final - E_initial) / E_initial - 'max_deviation': max|E - E_mean| / E_mean |
Example
Source code in jaxlatt/observables/energy.py
summary()
Generate human-readable summary of energy tracking.
Returns:
| Type | Description |
|---|---|
str
|
Formatted string with energy statistics |
Source code in jaxlatt/observables/energy.py
to_dict()
Export all data as dictionary for saving.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with 'times' and 'energies' arrays |
Example
Source code in jaxlatt/observables/energy.py
kinetic_energy_averaged(lattice)
Compute volume-averaged kinetic energy density.
Kinetic energy density: \(\rho_\mathrm{kin} = \frac{1}{2}\dot{\phi}^2\)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
Lattice
|
Lattice object with field_dot |
required |
Returns:
| Type | Description |
|---|---|
float
|
Volume-averaged kinetic energy: \(\langle \rho_\mathrm{kin} \rangle = \frac{1}{2}\langle \dot{\phi}^2 \rangle\) |
Source code in jaxlatt/observables/energy.py
gradient_energy_averaged(lattice)
Compute volume-averaged gradient energy density.
Gradient energy density: \(\rho_\mathrm{grad} = \frac{1}{2}(\nabla\phi)^2\)
Uses FFT-based gradient computation for accuracy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
Lattice
|
Lattice object with field |
required |
Returns:
| Type | Description |
|---|---|
float
|
Volume-averaged gradient energy: \(\langle \rho_\mathrm{grad} \rangle = \frac{1}{2}\langle (\nabla\phi)^2 \rangle\) |
Source code in jaxlatt/observables/energy.py
potential_energy_averaged(lattice, potential_func)
Compute volume-averaged potential energy density.
Potential energy density: \(\rho_\mathrm{pot} = V(\phi)\)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
Lattice
|
Lattice object with field |
required |
potential_func
|
Callable
|
Potential function \(V(\phi) \to V\); should be JIT-compiled. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Volume-averaged potential energy: \(\langle \rho_\mathrm{pot} \rangle = \langle V(\phi) \rangle\) |
Example
Source code in jaxlatt/observables/energy.py
total_energy_averaged(lattice, potential_func)
Compute total volume-averaged energy density.
Total energy: \(E_\mathrm{tot} = E_\mathrm{kin} + E_\mathrm{grad} + E_\mathrm{pot}\)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
Lattice
|
Lattice object |
required |
potential_func
|
Callable
|
Potential function \(V(\phi) \to V\) |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total volume-averaged energy density |
Example
Source code in jaxlatt/observables/energy.py
energy_components_averaged(lattice, potential_func)
Compute all energy components.
Returns a dictionary with all energy contributions for easy tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
Lattice
|
Lattice object |
required |
potential_func
|
Callable
|
Potential function \(V(\phi) \to V\) |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary with keys: |
dict[str, float]
|
|
dict[str, float]
|
|
dict[str, float]
|
|
dict[str, float]
|
|
Example
Source code in jaxlatt/observables/energy.py
kinetic_energy_integrated(lattice)
Compute integrated kinetic energy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
Lattice
|
Lattice state containing |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total kinetic energy, integrated over the full simulation volume. |
Source code in jaxlatt/observables/energy.py
potential_energy_integrated(lattice, potential)
Compute integrated potential energy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
Lattice
|
Lattice state containing scalar field values. |
required |
potential
|
Callable
|
Potential function |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total potential energy, integrated over the full simulation volume. |
Source code in jaxlatt/observables/energy.py
energy_components_integrated(lattice, potential)
Compute all integrated energy components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
Lattice
|
Lattice state. |
required |
potential
|
Callable
|
Potential function |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary with integrated |
Source code in jaxlatt/observables/energy.py
total_energy_integrated(lattice, potential)
Compute integrated total energy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
Lattice
|
Lattice state. |
required |
potential
|
Callable
|
Potential function |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total energy integrated over the full simulation volume. |
Source code in jaxlatt/observables/energy.py
compute_physical_scalar_gradient_energy(field, scale_factor, dx)
Compute physical gradient energy density with scale factor scaling.
In expanding universe (conformal time):
where \(\nabla\) is the comoving gradient operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
Scalar field configuration |
required |
scale_factor
|
float
|
FLRW scale factor \(a(\tau)\) |
required |
dx
|
float
|
Lattice spacing (comoving) |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Physical gradient energy density (averaged over lattice) |
Note
Uses FFT-based gradient computation for spectral accuracy. For expanding universe, this should be called with comoving coordinates.
Source code in jaxlatt/observables/energy.py
compute_physical_scalar_kinetic_energy(field_dot, scale_factor)
Compute physical kinetic energy density with scale factor scaling.
In conformal time with canonical momentum \(\pi = a^3 \dot{\phi}\):
For proper implementation, we assume field_dot stores the conformal
time derivative (\(\phi'\) in conformal time), so:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_dot
|
Array
|
Time derivative of field (conformal time) |
required |
scale_factor
|
float
|
FLRW scale factor \(a(\tau)\) |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Physical kinetic energy density (averaged) |
Note
Convention depends on how field_dot is stored. Here we assume it's the conformal time derivative, giving \(a^{-2}\) scaling.
Source code in jaxlatt/observables/energy.py
compute_physical_potential_energy(field, potential_func)
Compute physical potential energy density.
Potential energy has no scale factor dependence:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
Scalar field configuration |
required |
potential_func
|
Callable[[Array], ndarray]
|
Potential function \(V(\phi)\) |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Physical potential energy density (averaged) |
Source code in jaxlatt/observables/energy.py
compute_physical_gauge_electric_energy(E_field, scale_factor)
Compute physical electric field energy density.
Electric field energy in expanding universe:
where \(E\) is the comoving electric field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
E_field
|
Array
|
Electric field components (3, N, N, N) |
required |
scale_factor
|
float
|
FLRW scale factor \(a(\tau)\) |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Physical electric energy density (averaged) |
Source code in jaxlatt/observables/energy.py
compute_physical_gauge_magnetic_energy(links, scale_factor, dx, g)
Compute physical magnetic field energy density.
Magnetic field energy in expanding universe:
where \(B\) is extracted from plaquettes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
links
|
Array
|
Gauge link variables (3, N, N, N) |
required |
scale_factor
|
float
|
FLRW scale factor \(a(\tau)\) |
required |
dx
|
float
|
Lattice spacing (comoving) |
required |
g
|
float
|
Gauge coupling |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Physical magnetic energy density (averaged) |
Note
Imports magnetic_field from operators.gauge to avoid circular dependency.
Source code in jaxlatt/observables/energy.py
compute_physical_energy_density(lattice, scale_factor, dx, potential_func)
Compute total physical energy density for Friedmann equation.
This is the source term \(\rho_\mathrm{total}\) in the Friedmann equation:
Includes all energy components with correct scale factor scaling:
- Scalar kinetic: \(|\pi|^2/(2a^6)\) where \(\pi\) is canonical momentum
- Scalar gradient: \((\nabla\phi)^2/(2a^2)\)
- Scalar potential: \(V(\phi)\)
- Electric field: \(E^2/(2a^4)\)
- Magnetic field: \(B^2/(2a^4)\)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
CoupledLattice
|
CoupledLattice with scalar and gauge fields |
required |
scale_factor
|
float
|
Current scale factor \(a(\tau)\) |
required |
dx
|
float
|
Lattice spacing (comoving) |
required |
potential_func
|
Callable[[Array], ndarray]
|
Scalar potential \(V(\phi)\) |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total physical energy density \(\rho_\mathrm{phys}\) (averaged over lattice) |
Note
For scalar-only simulations, pass lattice with dummy gauge fields or use scalar-specific functions above.
Source code in jaxlatt/observables/energy.py
compute_physical_energy_components(lattice, scale_factor, dx, potential_func)
Compute all physical energy density components separately.
Useful for diagnostics and tracking how energy is distributed between different field components during expansion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
CoupledLattice
|
CoupledLattice state |
required |
scale_factor
|
float
|
Current scale factor \(a(\tau)\) |
required |
dx
|
float
|
Lattice spacing (comoving) |
required |
potential_func
|
Callable[[Array], ndarray]
|
Scalar potential \(V(\phi)\) |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary with keys: |
dict[str, float]
|
|
dict[str, float]
|
|
dict[str, float]
|
|
dict[str, float]
|
|
dict[str, float]
|
|
dict[str, float]
|
|
Example
Source code in jaxlatt/observables/energy.py
compute_comoving_energy_density(lattice, dx, potential_func)
Compute comoving energy density (no scale factor corrections).
This is what standard (non-expanding) simulations compute. Useful for comparison and debugging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
CoupledLattice
|
CoupledLattice state |
required |
dx
|
float
|
Lattice spacing |
required |
potential_func
|
Callable[[Array], ndarray]
|
Scalar potential \(V(\phi)\) |
required |
Returns:
| Type | Description |
|---|---|
float
|
Comoving energy density (what you'd get with \(a=1\)) |
Source code in jaxlatt/observables/energy.py
kinetic_energy(field_dot, dV)
Compute total kinetic energy: \(E_\mathrm{kin} = \frac{1}{2} \int (\partial\phi/\partial t)^2\,dV\)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_dot
|
Array
|
Time derivative of the field |
required |
dV
|
float
|
Volume element (grid cell volume) |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total kinetic energy (integrated, not averaged) |
Source code in jaxlatt/observables/energy.py
potential_energy(field, potential, dV)
Compute total potential energy: \(E_\mathrm{pot} = \int V(\phi)\,dV\)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
Field configuration |
required |
potential
|
Callable[[Array], ndarray]
|
Potential function \(V(\phi)\) |
required |
dV
|
float
|
Volume element |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total potential energy (integrated, not averaged) |
Source code in jaxlatt/observables/energy.py
compute_energy_components(lattice, potential)
Compute all energy components separately (volume-integrated).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
Lattice
|
Lattice object containing field and field_dot |
required |
potential
|
Callable[[Array], ndarray]
|
Potential function \(V(\phi)\) |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary with keys |
Source code in jaxlatt/observables/energy.py
compute_energy(lattice, potential)
Compute total energy of a field configuration (volume-integrated).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lattice
|
Lattice
|
Lattice object containing field and field_dot |
required |
potential
|
Callable[[Array], ndarray]
|
Potential function \(V(\phi)\) |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total energy (integrated) |
Source code in jaxlatt/observables/energy.py
energy_density(field, field_dot, potential, dx)
Compute local energy density at each lattice point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
Field configuration |
required |
field_dot
|
Array
|
Time derivative of field |
required |
potential
|
Callable[[Array], ndarray]
|
Potential function |
required |
dx
|
float
|
Lattice spacing |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Energy density at each lattice point |