Lattice3D

class Lattice3D.Lattice3D(x_min, x_max, y_min, y_max, z_min, z_max, num_points_x, num_points_y, num_points_z, n_sigma_x=None, n_sigma_y=None, n_sigma_z=None)[source]

Represents a 3D lattice with evenly spaced points.

Parameters:
x_minfloat

The minimum value along the x-axis.

x_maxfloat

The maximum value along the x-axis.

y_minfloat

The minimum value along the y-axis.

y_maxfloat

The maximum value along the y-axis.

z_minfloat

The minimum value along the z-axis.

z_maxfloat

The maximum value along the z-axis.

num_points_xint

The number of points along the x-axis.

num_points_yint

The number of points along the y-axis.

num_points_zint

The number of points along the z-axis.

n_sigma_xfloat

Number of sigmas to consider in smearing in x direction. Default is 3.

n_sigma_yfloat

Number of sigmas to consider in smearing in y direction. Default is 3.

n_sigma_zfloat

Number of sigmas to consider in smearing in z direction. Default is 3.

Attributes:
x_min_float

The minimum value along the x-axis.

x_max_float

The maximum value along the x-axis.

y_min_float

The minimum value along the y-axis.

y_max_float

The maximum value along the y-axis.

z_min_float

The minimum value along the z-axis.

z_max_float

The maximum value along the z-axis.

num_points_x_int

The number of points along the x-axis.

num_points_y_int

The number of points along the y-axis.

num_points_z_int

The number of points along the z-axis.

spacing_x_float

The spacing between points along the x-axis.

spacing_y_float

The spacing between points along the y-axis.

spacing_z_float

The spacing between points along the z-axis.

density_x_float

The density of points along the x-axis.

density_y_float

The density of points along the y-axis.

density_z_float

The density of points along the z-axis.

cell_volume_float

The volume of each cell in the lattice.

x_values_numpy.ndarray

The array of x-axis values.

y_values_numpy.ndarray

The array of y-axis values.

z_values_numpy.ndarray

The array of z-axis values.

grid_numpy.ndarray

The 3D grid containing the values at each lattice point.

Methods

set_value_by_index:

Set the value at the specified indices in the grid.

get_value_by_index:

Get the value at the specified indices in the grid.

set_value:

Set the value at the specified coordinates in the lattice.

set_value_nearest_neighbor:

Set the value at the nearest neighbor of the specified coordinates within the lattice.

get_value:

Get the value at the specified coordinates in the lattice.

get_value_nearest_neighbor:

Get the value of the nearest neighbor at the specified coordinates within the lattice.

get_coordinates:

Get the coordinates corresponding to the given indices.

find_closest_indices:

Find the closest indices in the lattice for the given coordinates.

interpolate_value:

Interpolate the value at the specified position using trilinear interpolation.

average:

Compute the element-wise average of multiple Lattice3D objects.

rescale:

Rescale the values of the lattice by a specified factor.

save_to_csv:

Save the lattice data, including metadata, to a CSV file.

load_from_csv:

Load lattice data, including metadata, from a CSV file.

visualize:

Visualize the lattice data in a 3D plot.

extract_slice:

Extract a 2D slice from the lattice along the specified axis at the given index.

save_slice_to_csv:

Save a 2D slice from the lattice along the specified axis and index to a CSV file.

interpolate_to_lattice:

Interpolate the current lattice data to a new lattice with the specified number of points along each axis.

interpolate_to_lattice_new_extent:

Interpolate the current lattice data to a new lattice with the specified number of points and extent.

reset:

Reset the values of all grid points in the lattice to zero.

add_same_spaced_grid:

Add the values from another grid with the same spacing.

add_particle_data:

Add particle data to the lattice.

Lattice3D.set_value_by_index(i, j, k, value)[source]

Set the value at the specified indices (i, j, k) in the grid.

Parameters:
iint

The index along the x-axis.

jint

The index along the y-axis.

kint

The index along the z-axis.

valueint or float

The value to set at the specified indices.

Returns:
None
Lattice3D.get_value_by_index(i, j, k)[source]

Get the value at the specified indices (i, j, k) in the grid.

Parameters:
iint

The index along the x-axis.

jint

The index along the y-axis.

kint

The index along the z-axis.

Returns:
int or float or None

The value at the specified indices if the indices are valid, otherwise None.

Lattice3D.set_value(x, y, z, value)[source]

Set the value at the specified coordinates within the lattice.

Parameters:
xint or float

The x-coordinate where the value is to be set.

yint or float

The y-coordinate where the value is to be set.

zint or float

The z-coordinate where the value is to be set.

valueint or float

The value to be set.

Raises:
ValueError

If any of the coordinates are outside their specified ranges.

Lattice3D.set_value_nearest_neighbor(x, y, z, value)[source]

Set the value at the nearest neighbor of the specified coordinates within the lattice.

Parameters:
xint or float

The x-coordinate where the value is to be set.

yint or float

The y-coordinate where the value is to be set.

zint or float

The z-coordinate where the value is to be set.

valueint or float

The value to be set.

Raises:
ValueError

If any of the coordinates are outside their specified ranges.

Lattice3D.get_value(x, y, z)[source]

Get the value at the specified coordinates within the lattice.

Parameters:
xint or float

The x-coordinate where the value is to be retrieved.

yint or float

The y-coordinate where the value is to be retrieved.

zint or float

The z-coordinate where the value is to be retrieved.

Returns:
int or float or None

The value at the specified coordinates.

Raises:
ValueError

If any of the coordinates are outside their specified ranges.

Lattice3D.get_value_nearest_neighbor(x, y, z)[source]

Get the value of the nearest neighbor at the specified coordinates within the lattice.

Parameters:
xint or float

The x-coordinate where the value is to be retrieved.

yint or float

The y-coordinate where the value is to be retrieved.

zint or float

The z-coordinate where the value is to be retrieved.

Returns:
int or float or None

The value at the specified coordinates.

Raises:
ValueError

If any of the coordinates are outside their specified ranges.

Lattice3D.get_coordinates(i, j, k)[source]

Retrieve the coordinates associated with the given indices.

Parameters:
iint

The index along the x-axis.

jint

The index along the y-axis.

kint

The index along the z-axis.

Returns:
tuple

A tuple containing the x, y, and z coordinates corresponding to the given indices.

Raises:
ValueError

If any of the indices are outside the specified range.

Lattice3D.find_closest_indices(x, y, z)[source]

Find the closest indices in the lattice corresponding to the given coordinates.

Parameters:
xfloat

The x-coordinate.

yfloat

The y-coordinate.

zfloat

The z-coordinate.

Returns:
tuple

A tuple of three integers representing the closest indices in the lattice for the given coordinates (x, y, z).

Lattice3D.interpolate_value(x, y, z, method='nearest')[source]

Interpolate the value at the specified position using (up to) trilinear interpolation.

Parameters:
xfloat

The x-coordinate.

yfloat

The y-coordinate.

zfloat

The z-coordinate.

methodstr

Scipy option for interpolation method. Default is ‘nearest’.

Returns:
float or None

The interpolated value at the specified position. If the provided position is outside the lattice range, None is returned.

Lattice3D.average(*lattices)[source]

Compute the average of multiple Lattice3D objects element-wise.

Parameters:
*latticesLattice3D

Multiple Lattice3D objects to compute the average.

Returns:
Lattice3D

A new Lattice3D object containing the element-wise average.

Raises:
TypeError

If any of the operands are not of type ‘Lattice3D’.

ValueError

If the lattices do not have the same shape.

Examples

If you want to average over 3 lattices, you can do the following:

 1>>> lattice1 = Lattice3D(x_min=0, x_max=1, y_min=0, y_max=1, z_min=0,
 2>>>    z_max=1, num_points_x=10, num_points_y=10, num_points_z=10)
 3>>>
 4>>> lattice2 = Lattice3D(x_min=0, x_max=1, y_min=0, y_max=1, z_min=0,
 5>>>    z_max=1, num_points_x=10, num_points_y=10, num_points_z=10)
 6>>>
 7>>> lattice3 = Lattice3D(x_min=0, x_max=1, y_min=0, y_max=1, z_min=0,
 8>>>    z_max=1, num_points_x=10, num_points_y=10, num_points_z=10)
 9>>>
10>>> lattice1=lattice1.average(lattice2, lattice3)
Lattice3D.rescale(factor)[source]

Rescale the values of the lattice by a specified factor.

Parameters:
factornumeric

The rescaling factor.

Returns:
None
Lattice3D.save_to_csv(filename)[source]

Save the lattice data, including metadata, to a CSV file.

Parameters:
filenamestr

The filename of the CSV file to save.

Returns:
None
classmethod Lattice3D.load_from_csv(filename)[source]

Load lattice data, including metadata, from a CSV file.

Parameters:
filenamestr

The filename of the CSV file to load.

Returns:
latticeLattice3D

The loaded Lattice3D object containing the data and metadata.

Lattice3D.visualize()[source]

Visualize the lattice data in a 3D plot.

The lattice values are represented as a scatter plot in a 3D space.

Lattice3D.extract_slice(axis, index)[source]

Extract a 2D slice from the lattice along the specified axis at the given index.

Parameters:
axisstr

The axis along which to extract the slice. Must be ‘x’, ‘y’, or ‘z’.

indexint

The index of the slice along the specified axis.

Returns:
A tuple containing the following:
slice_datandarray

The 2D slice data extracted from the lattice.

slice_valuestuple of 2 ndarrays

A tuple of coordinate lists containing the 2D coordinates of the slice.

slice_labelstr

The label describing the slice plane.

Raises:
ValueError

If the axis parameter is invalid or the index is out of range.

Lattice3D.save_slice_to_csv(axis, index, filename)[source]

Save a 2D slice from the lattice along the specified axis and index to a CSV file.

Parameters:
axisstr

The axis along which to extract the slice. Must be ‘x’, ‘y’, or ‘z’.

indexint

The index of the slice along the specified axis.

filenamestr

The name of the CSV file to save the slice data.

Raises:
ValueError

If the axis parameter is invalid or the index is out of range.

Lattice3D.interpolate_to_lattice(num_points_x, num_points_y, num_points_z)[source]

Interpolate the current lattice data to a new lattice with the specified number of points along each axis.

Parameters:
num_points_xint

The number of points along the X-axis of the new lattice.

num_points_yint

The number of points along the Y-axis of the new lattice.

num_points_zint

The number of points along the Z-axis of the new lattice.

Returns:
Lattice3D

A new Lattice3D object containing the interpolated data.

Notes

This method performs spline interpolation to generate the values for each grid point of the new lattice. The interpolation is based on the current lattice data and the desired number of points along each axis.

Lattice3D.interpolate_to_lattice_new_extent(num_points_x, num_points_y, num_points_z, x_min, x_max, y_min, y_max, z_min, z_max)[source]

Interpolate the current lattice data to a new lattice with the specified number of points and extent.

Parameters:
num_points_xint

The number of points along the X-axis of the new lattice.

num_points_yint

The number of points along the Y-axis of the new lattice.

num_points_zint

The number of points along the Z-axis of the new lattice.

x_minfloat

The minimum value of the X-axis for the new lattice extent.

x_maxfloat

The maximum value of the X-axis for the new lattice extent.

y_minfloat

The minimum value of the Y-axis for the new lattice extent.

y_maxfloat

The maximum value of the Y-axis for the new lattice extent.

z_minfloat

The minimum value of the Z-axis for the new lattice extent.

z_maxfloat

The maximum value of the Z-axis for the new lattice extent.

Returns:
Lattice3D

A new Lattice3D object containing the interpolated data.

Notes

This method performs spline interpolation to generate the values for each grid point of the new lattice. The interpolation is based on the current lattice data and the desired number of points and extent along each axis.

Lattice3D.reset()[source]

Reset the values of all grid points in the lattice to zero.

Notes

This method iterates over all grid points in the lattice and sets their values to zero.

Lattice3D.add_same_spaced_grid(other, center_x, center_y, center_z)[source]

Add the values of grid points of another lattice with same spacing.

Parameters:
otherLattice3D

The other Lattice3D object to add, which is expected to have the same spacing.

center_xfloat

x-position in this grid at which the center of the other grid should be placed.

center_yfloat

y-position in this grid at which the center of the other grid should be placed.

center_zfloat

z-position in this grid at which the center of the other grid should be placed.

Raises:
TypeError

If the operand other is not of type Lattice3D.

ValueError

If other is of wrong spacing.

Lattice3D.add_particle_data(particle_data, sigma, quantity, kernel='covariant', add=False)[source]

Add particle data to the lattice.

Parameters:
particle_datalist

A list of Particle objects containing the particle data.

sigmafloat

The standard deviation of the Gaussian kernel used for smearing the particle data.

quantitystr

The quantity of the particle data to be added. Supported values are ‘energy density’, ‘number’, ‘charge’, and ‘baryon number’.

kernelstr, optional

The type of kernel to use for smearing the particle data. Supported values are ‘gaussian’ and ‘covariant’. The default is ‘covariant’.

addbool, optional

Specifies whether to add the particle data to the existing lattice values or replace them. If True, the particle data will be added to the existing lattice values. If False (default), the lattice values will be reset before adding the particle data.

Raises:
ValueError

If an unknown quantity is specified or if the particle data contains NaN values.

Notes

This method calculates the smearing of the particle data using a Gaussian kernel centered at each particle’s coordinates. The smeared values are added to the corresponding grid points in the lattice.

The Gaussian kernel is defined by the provided standard deviation sigma. The larger the sigma value, the smoother the smearing effect.

The supported quantities for particle data are as follows:

  • ‘energy_density’: Uses the particle’s energy (E) as the value to be added to the lattice.

  • ‘number_density’: Adds a value of 1.0 to the grid for each particle.

  • ‘charge_density’: Uses the particle’s charge as the value to be added to the lattice.

  • ‘baryon_density’: Uses the particle’s baryon number as the value to be added to the lattice.

  • ‘strangeness_density’: Uses the particle’s strangeness as the value to be added to the lattice.