abtem.core.grid#
Module for the Grid class and related functions.
Module Contents#
Classes#
The Grid object represent the simulation grid on which the wave functions and potential are discretized. |
|
Functions#
Adjust the number of grid points to match a target sampling. |
|
Return all indices inside a disk with a given radius. |
|
Yield chunks of disk indices without building the full (2r+1)² array. |
|
Return the polar spatial frequencies of a grid. |
|
Whether grids abTEM derives on its own are rounded to fast FFT lengths. |
|
Return the spatial frequencies of a grid. |
|
Ensure that the prodived grid points are valid. |
Data#
API#
- class abtem.core.grid.Grid(...)[source]#
Bases:
abtem.core.utils.CopyMixin,abtem.core.utils.EqualityMixinThe Grid object represent the simulation grid on which the wave functions and potential are discretized.
- Parameters:
extent (two float) – Grid extent in each dimension [Å].
gpts (two int) – Number of grid points in each dimension.
sampling (two float) – Grid sampling in each dimension [Å].
dimensions (int) – Number of dimensions represented by the grid.
endpoint (bool) – If true include the grid endpoint. Default is False. For periodic grids the endpoint should not be included.
lock_extent (bool) – If true the extent cannot be modified. Default is False.
lock_gpts (bool) – If true the gpts cannot be modified. Default is False.
lock_sampling (bool) – If true the sampling cannot be modified. Default is False.
Initialization
- check_match(...)[source]#
Raise error if the grid of another object is different from this object.
- Parameters:
other (Grid object) – The grid that should be checked.
- match(...)[source]#
Set the parameters of this grid to match another grid.
- Parameters:
other (Grid object) – The grid that should be matched.
check_match (bool) – If true check whether grids can match without overriding already defined grid parameters.
- round_to_fast_fft() tuple[int, ...][source]#
Round the grid gpts up to the nearest fast FFT lengths.
Fast lengths factorize completely into the primes 2, 3, 5 and 7, for which FFT libraries (FFTW, pocketfft, MKL and cuFFT) ship optimized kernels; any other length falls back to a slower generic algorithm – on cuFFT the Bluestein algorithm, which additionally allocates a workspace of several times the transform size. Rounding is always upward, so the realized sampling is never coarser than before.
Every gpts is rounded, including on an endpoint grid – unlike the automatic rounding, which leaves endpoint grids alone because they are not periodic FFT grids.
Automatic rounding is governed by the configuration option
grid.round-to-fast-fft:'auto'(the default) rounds the grids abTEM derives on its own, such asPotential(sampling='auto');Trueadditionally rounds gpts derived from a numeric sampling;Falsedisables it everywhere.Grids that are never Fourier transformed (
fft_grid=False, e.g. the probe positions of aGridScan) are returned unchanged: a fast length buys them nothing, and changing them would change what is simulated rather than how fast it runs.
- round_to_power(...) tuple[int, ...][source]#
Round the grid gpts up to a whole power of one of the given bases.
Each gpts becomes
base ** kfor whichever base gives the smallest such value at or above it – a pure power, not a product of several bases, so 2623 rounds to 4096 rather than to 2625. That is a much larger grid than fast FFTs actually require: seeround_to_fast_fft(), which rounds to the nearest length whose prime factors all lie in {2, 3, 5, 7} and is what “faster for arrays whose size factorizes into small primes” normally means.(For a handful of inputs that are already exact powers of 5 or 7 – 125, 15625, 16807 – floating-point
logrounds the exponent up and the result overshoots to the next power.)
- exception abtem.core.grid.GridUndefinedError[source]#
Bases:
ExceptionException raised when the grid is not defined.
Initialization
Initialize self. See help(type(self)) for accurate signature.
- class abtem.core.grid.HasGrid2DMixin[source]#
- abtem.core.grid.T#
‘TypeVar(…)’
- abtem.core.grid.U#
‘TypeVar(…)’
- abtem.core.grid.adjusted_gpts(...) tuple[tuple[float, ...], tuple[int, ...]][source]#
Adjust the number of grid points to match a target sampling.
- abtem.core.grid.disk_meshgrid(...) ndarray[source]#
Return all indices inside a disk with a given radius.
- abtem.core.grid.disk_meshgrid_iter(...)[source]#
Yield chunks of disk indices without building the full (2r+1)² array.
For large radii the standard
disk_meshgrid()allocates two(2r+1, 2r+1)intermediate arrays on the CPU and a single output that can each be many gigabytes. This generator builds the result row-by-row and yields it in manageable chunks of at most chunk_size(row, col)pairs.
- abtem.core.grid.polar_spatial_frequencies(...) tuple[ndarray, ndarray][source]#
Return the polar spatial frequencies of a grid.
- Parameters:
- Returns:
k_and_phi – Tuple of spatial frequencies in polar coordinates. First element is the radial frequency and the second element is the azimuthal angle.
- Return type:
- abtem.core.grid.round_auto_derived_gpts() bool[source]#
Whether grids abTEM derives on its own are rounded to fast FFT lengths.
This covers grids abTEM chooses without a user-supplied sampling, such as
Potential(..., sampling='auto'). Grids derived from a numeric sampling are governed separately (they are only rounded in the'always'mode), because rounding those changes the result of an existing script.
- abtem.core.grid.spatial_frequencies(...)[source]#
Return the spatial frequencies of a grid.
- Parameters:
- Returns:
spatial_frequencies (tuple of numpy.ndarray) – Tuple of spatial frequencies in each dimension.
spatial_frequencies_grid (numpy.ndarray) – If return_grid is True, the spatial frequencies as a single meshgrid array.
- abtem.core.grid.validate_gpts(...) tuple[int, ...][source]#
Ensure that the prodived grid points are valid.
- Parameters:
gpts (tuple of int) – The tuple of integers representing the GPTs (General Purpose Tokens).
- Returns:
The validated tuple of integers representing the GPTs.
- Return type:
- Raises:
ValueError – If any value in the gpts tuple is not greater than 0.