XLB utils
Downsample a JAX array by a factor of factor
along each axis.
Parameters
field : jax.numpy.ndarray The input vector field to be downsampled. This should be a 3D or 4D JAX array where the last dimension is 2 or 3 (vector components). factor : int The factor by which to downsample the field. The dimensions of the field will be divided by this factor. method : str, optional The method to use for downsampling. Default is ‘bicubic’.
Returns
jax.numpy.ndarray The downsampled field.
Source code in src/utils.py
Save an image of a field at a given timestep.
Parameters
timestep : int The timestep at which the field is being saved. fld : jax.numpy.ndarray The field to be saved. This should be a 2D or 3D JAX array. If the field is 3D, the magnitude of the field will be calculated and saved. prefix : str, optional A prefix to be added to the filename. The filename will be the name of the main script file by default.
Returns
None
Notes
This function saves the field as an image in the PNG format. The filename is based on the name of the main script file, the provided prefix, and the timestep number. If the field is 3D, the magnitude of the field is calculated and saved. The image is saved with the ‘nipy_spectral’ colormap and the origin set to ‘lower’.
Source code in src/utils.py
Save VTK fields to the specified directory.
Parameters
timestep (int): The timestep number to be associated with the saved fields. fields (Dict[str, np.ndarray]): A dictionary of fields to be saved. Each field must be an array-like object with dimensions (nx, ny) for 2D fields or (nx, ny, nz) for 3D fields, where: - nx : int, number of grid points along the x-axis - ny : int, number of grid points along the y-axis - nz : int, number of grid points along the z-axis (for 3D fields only) The key value for each field in the dictionary must be a string containing the name of the field. output_dir (str, optional, default: ‘.’): The directory in which to save the VTK files. Defaults to the current directory. prefix (str, optional, default: ‘fields’): A prefix to be added to the filename. Defaults to ‘fields’.
Returns
None
Notes
This function saves the VTK fields in the specified directory, with filenames based on the provided timestep number and the filename. For example, if the timestep number is 10 and the file name is fields, the VTK file will be saved as ‘fields_0000010.vtk’in the specified directory.
Source code in src/utils.py
Live rendering of a 3D volume using pyvista.
Parameters
field (np.ndarray): A 3D array containing the field to be rendered.
Returns
None
Notes
This function uses pyvista to render a 3D volume. The volume is rendered with a colormap based on the field values. The colormap is updated every 0.1 seconds to reflect changes to the field.
Source code in src/utils.py
Save boundary conditions as VTK format to the specified directory.
Parameters
timestep (int): The timestep number to be associated with the saved fields. BCs (List[BC]): A list of boundary conditions to be saved. Each boundary condition must be an object of type BC.
Returns
None
Notes
This function saves the boundary conditions in the specified directory, with filenames based on the provided timestep number and the filename. For example, if the timestep number is 10, the VTK file will be saved as ‘BCs_0000010.vtk’in the specified directory.
Source code in src/utils.py
Rotates a voxelized mesh around a given axis.
Parameters
indices : array-like The indices of the voxels in the mesh. origin : array-like The coordinates of the origin of the rotation axis. axis : array-like The direction vector of the rotation axis. This should be a 3-element sequence. angle : float The angle by which to rotate the mesh, in radians.
Returns
tuple The indices of the voxels in the rotated mesh.
Notes
This function rotates the mesh by applying a rotation matrix to the voxel indices. The rotation matrix is calculated using the axis-angle representation of rotations. The origin of the rotation axis is assumed to be at (0, 0, 0).
Source code in src/utils.py
Converts an STL file to a voxelized mesh.
Parameters
stl_filename : str The name of the STL file to be voxelized. length_lbm_unit : float, optional The unit length in LBM. Either this or ‘pitch’ must be provided. tranformation_matrix : array-like, optional A transformation matrix to be applied to the mesh before voxelization. pitch : float, optional The pitch of the voxel grid. Either this or ‘length_lbm_unit’ must be provided.
Returns
trimesh.VoxelGrid, float The voxelized mesh and the pitch of the voxel grid.
Notes
This function uses the trimesh library to load the STL file and voxelized the mesh. If a transformation matrix is provided, it is applied to the mesh before voxelization. The pitch of the voxel grid is calculated based on the maximum extent of the mesh and the provided lattice Boltzmann unit length, unless a pitch is provided directly.