1+ from __future__ import annotations
2+
13from abc import ABC , abstractmethod
4+ from typing import TYPE_CHECKING
5+
6+ if TYPE_CHECKING :
7+ import numpy as np
28
39
410class BaseGrid (ABC ):
511 @abstractmethod
6- def search (self , z : float , y : float , x : float , ei = None , search2D : bool = False ) :
12+ def search (self , z : float , y : float , x : float , ei = None ) -> dict [ str , tuple [ int , float | np . ndarray ]] :
713 """
814 Perform a spatial (and optionally vertical) search to locate the grid element
915 that contains a given point (x, y, z).
1016
1117 This method delegates to grid-type-specific logic (e.g., structured or unstructured)
12- to determine the appropriate indices and interpolation coordinates for evaluating a field.
18+ to determine the appropriate indices and barycentric coordinates for evaluating a field.
1319
1420 Parameters
1521 ----------
@@ -28,12 +34,21 @@ def search(self, z: float, y: float, x: float, ei=None, search2D: bool = False):
2834
2935 Returns
3036 -------
31- bcoords : np.ndarray or tuple
32- Interpolation weights or barycentric coordinates within the containing cell/face.
33- The interpretation of `bcoords` depends on the grid type.
34- ei : int
35- Encoded index of the identified grid cell or face. This value can be cached for
36- future lookups to accelerate repeated searches.
37+ dict
38+ A dictionary mapping spatial axis names to tuples of (index, barycentric_coordinates).
39+ The returned axes depend on the grid dimensionality and type:
40+
41+ - 3D structured grid: {"Z": (zi, zeta), "Y": (yi, eta), "X": (xi, xsi)}
42+ - 2D structured grid: {"Y": (yi, eta), "X": (xi, xsi)}
43+ - 1D structured grid (depth): {"Z": (zi, zeta)}
44+ - Unstructured grid: {"Z": (zi, zeta), "FACE": (fi, bcoords)}
45+
46+ Where:
47+ - index (int): The cell position of a particle along the given axis
48+ - barycentric_coordinates (float or np.ndarray): The coordinates defining
49+ a particle's position within the grid cell. For structured grids, this
50+ is a single coordinate per axis; for unstructured grids, this can be
51+ an array of coordinates for the face polygon.
3752
3853 Raises
3954 ------
@@ -43,3 +58,72 @@ def search(self, z: float, y: float, x: float, ei=None, search2D: bool = False):
4358 Raised if the search method is not implemented for the current grid type.
4459 """
4560 ...
61+
62+ @abstractmethod
63+ def ravel_index (self , axis_indices : dict [str , int ]) -> int :
64+ """
65+ Convert a dictionary of axis indices to a single encoded index (ei).
66+
67+ This method takes the individual indices for each spatial axis and combines them
68+ into a single integer that uniquely identifies a grid cell. This encoded
69+ index can be used for efficient caching and lookup operations.
70+
71+ Parameters
72+ ----------
73+ axis_indices : dict[str, int]
74+ A dictionary mapping axis names to their corresponding indices.
75+ The expected keys depend on the grid dimensionality and type:
76+
77+ - 3D structured grid: {"Z": zi, "Y": yi, "X": xi}
78+ - 2D structured grid: {"Y": yi, "X": xi}
79+ - 1D structured grid: {"Z": zi}
80+ - Unstructured grid: {"Z": zi, "FACE": fi}
81+
82+ Returns
83+ -------
84+ int
85+ The encoded index (ei) representing the unique grid cell or face.
86+
87+ Raises
88+ ------
89+ KeyError
90+ Raised when required axis keys are missing from axis_indices.
91+ ValueError
92+ Raised when index values are out of bounds for the grid.
93+ NotImplementedError
94+ Raised if the method is not implemented for the current grid type.
95+ """
96+ ...
97+
98+ @abstractmethod
99+ def unravel_index (self , ei : int ) -> dict [str , int ]:
100+ """
101+ Convert a single encoded index (ei) back to a dictionary of axis indices.
102+
103+ This method is the inverse of ravel_index, taking an encoded index and
104+ decomposing it back into the individual indices for each spatial axis.
105+
106+ Parameters
107+ ----------
108+ ei : int
109+ The encoded index representing a unique grid cell or face.
110+
111+ Returns
112+ -------
113+ dict[str, int]
114+ A dictionary mapping axis names to their corresponding indices.
115+ The returned keys depend on the grid dimensionality and type:
116+
117+ - 3D structured grid: {"Z": zi, "Y": yi, "X": xi}
118+ - 2D structured grid: {"Y": yi, "X": xi}
119+ - 1D structured grid: {"Z": zi}
120+ - Unstructured grid: {"Z": zi, "FACE": fi}
121+
122+ Raises
123+ ------
124+ ValueError
125+ Raised when the encoded index is out of bounds or invalid for the grid.
126+ NotImplementedError
127+ Raised if the method is not implemented for the current grid type.
128+ """
129+ ...
0 commit comments