Skip to content

7. mesh

torchfsm.mesh.MeshGrid ¤

An interable class that reprents a mesh grid. This class is particularly useful for generating the initial condition. The length of the class is the number of mesh dimensions. The attribute x, y, z are the mesh grid for the first three dimension. You can also access the mesh grid for other dimension by indexing the object. E.g., mesh_grid[0] is the mesh grid for the first dimension, equivalent to x. There is no limit for the number of dimension. Assume that the number of points in each dimension is \(n_1, n_2, n_3, \cdots, n_k\), the mesh grid will be of shape \((n_1,n_2,n_3,...,n_k)\). While for the attribute x, y, z, the shape will be \((n_1)\), \((n_2)\), \((n_3)\) respectively.

Parameters:

Name Type Description Default
mesh_info Sequence[tuple[float, float, int]]

sequence of tuple (start,end,n_points) for each dimension

required
device

device of the mesh.

None
dtype

data type of the mesh.

None

Methods:

Name Description
mesh_grid

Generate the mesh grid for all dimensions.

bc_mesh_grid

Generate the mesh grid with batch size and channel size.

Source code in torchfsm/mesh.py
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class MeshGrid:
    """
    An interable class that reprents a mesh grid.
    This class is particularly useful for generating the initial condition.
    The length of the class is the number of mesh dimensions.
    The attribute x, y, z are the mesh grid for the first three dimension.
    You can also access the mesh grid for other dimension by indexing the object.
    E.g., `mesh_grid[0]` is the mesh grid for the first dimension, equivalent to x.
    There is no limit for the number of dimension.
    Assume that the number of points in each dimension is $n_1, n_2, n_3, \cdots, n_k$, the mesh grid will be of shape $(n_1,n_2,n_3,...,n_k)$.
    While for the attribute x, y, z, the shape will be $(n_1)$, $(n_2)$, $(n_3)$ respectively.

    Args:
        mesh_info (Sequence[tuple[float,float,int]]): sequence of tuple (start,end,n_points) for each dimension
        device: device of the mesh.
        dtype: data type of the mesh.

    Methods:
        mesh_grid: Generate the mesh grid for all dimensions.
        bc_mesh_grid: Generate the mesh grid with batch size and channel size.

    """

    def __init__(
        self, mesh_info: Sequence[tuple[float, float, int]], device=None, dtype=None
    ) -> None:
        for dim_i in mesh_info:
            if len(dim_i) != 3:
                raise ValueError(
                    "each dimension should be a tuple of (start,end,n_points)"
                )
        self.mesh_info = mesh_info
        self.meshs = [[] for _ in range(len(mesh_info))]
        self._dim_names = ["x", "y", "z"]
        self.device, self.dtype = format_device_dtype(device, dtype)
        self.n_dim = len(mesh_info)

    def __len__(self):
        return len(self.mesh_info)

    def __getitem__(self, idx: int) -> SpatialTensor["H ..."]:
        if len(self.meshs) <= idx:
            if idx > 2:
                raise ValueError(f"mesh dim with id{idx} is not defined")
            else:
                raise ValueError(f"{self._dim_names[idx]} dim is not defined")
        if len(self.meshs[idx]) == 0:
            self.meshs[idx] = (
                (self.mesh_info[idx][1] - self.mesh_info[idx][0])
                * torch.arange(
                    0, self.mesh_info[idx][2], device=self.device, dtype=self.dtype
                )
                / self.mesh_info[idx][2]
            )
        return self.meshs[idx]

    @property
    def x(self) -> SpatialTensor["H"]:
        """
        Mesh grid for the first dimension
        """
        return self[0]

    @property
    def y(self) -> SpatialTensor["H"]:
        """
        Mesh grid for the second dimension
        """
        return self[1]

    @property
    def z(self) -> SpatialTensor["H"]:
        """
        Mesh grid for the third dimension
        """
        return self[2]

    def mesh_grid(
        self, numpy=False
    ) -> ValueList[
        Union[SpatialTensor["H ..."], Annotated[np.ndarray, "Spatial, H ..."]]
    ]:
        """
        Generate the mesh grid for all dimensions.
        The shape of the mesh grid will be (n1,n2,n3,...,nk).

        Args:
            numpy (bool): whether to return the mesh grid as numpy array

        Returns:
            torch.Tensor: mesh grid for all dimensions
        """
        if numpy:
            mesh_grid = np.meshgrid(*[self[i] for i in range(len(self))], indexing="ij")
        else:
            mesh_grid = torch.meshgrid(
                *[self[i] for i in range(len(self))], indexing="ij"
            )
        if len(mesh_grid) == 1:
            return mesh_grid[0]
        return mesh_grid

    def bc_mesh_grid(
        self, batch_size: int = 1, n_channel: int = 1, numpy=False
    ) -> ValueList[
        Union[SpatialTensor["B C H ..."], Annotated[np.ndarray, "Spatial, B C H ..."]]
    ]:
        """
        Generate the mesh grid with batch size and channel size.
        The shape of the mesh grid will be (batch_size,n_channel,n1,n2,n3,...,nk).

        Args:
            batch_size (int): batch size
            n_channel (int): channel size
            numpy (bool): whether to return the mesh grid as numpy array

        Returns:
            torch.Tensor: mesh grid with batch size and channel size
        """
        bc_mesh_grid = []
        mesh_grid = self.mesh_grid()
        if not isinstance(mesh_grid, Sequence):
            mesh_grid = [mesh_grid]
        for i in range(len(mesh_grid)):
            bc_mesh_grid.append(
                mesh_grid[i]
                .reshape(1, 1, *mesh_grid[i].shape)
                .repeat(batch_size, n_channel, *[1] * len(mesh_grid[i].shape))
            )
        if numpy:
            mesh_grid = [i.cpu().numpy() for i in bc_mesh_grid]
        if len(bc_mesh_grid) == 1:
            return bc_mesh_grid[0]
        return bc_mesh_grid

    def to(self, device=None, dtype=None):
        self.__init__(self.mesh_info, device=device, dtype=dtype)
mesh_info instance-attribute ¤
mesh_info = mesh_info
meshs instance-attribute ¤
meshs = [[] for _ in range(len(mesh_info))]
n_dim instance-attribute ¤
n_dim = len(mesh_info)
x property ¤
x: SpatialTensor[H]

Mesh grid for the first dimension

y property ¤
y: SpatialTensor[H]

Mesh grid for the second dimension

z property ¤
z: SpatialTensor[H]

Mesh grid for the third dimension

__init__ ¤
__init__(
    mesh_info: Sequence[tuple[float, float, int]],
    device=None,
    dtype=None,
) -> None
Source code in torchfsm/mesh.py
32
33
34
35
36
37
38
39
40
41
42
43
44
def __init__(
    self, mesh_info: Sequence[tuple[float, float, int]], device=None, dtype=None
) -> None:
    for dim_i in mesh_info:
        if len(dim_i) != 3:
            raise ValueError(
                "each dimension should be a tuple of (start,end,n_points)"
            )
    self.mesh_info = mesh_info
    self.meshs = [[] for _ in range(len(mesh_info))]
    self._dim_names = ["x", "y", "z"]
    self.device, self.dtype = format_device_dtype(device, dtype)
    self.n_dim = len(mesh_info)
__len__ ¤
__len__()
Source code in torchfsm/mesh.py
46
47
def __len__(self):
    return len(self.mesh_info)
__getitem__ ¤
__getitem__(idx: int) -> SpatialTensor['H ...']
Source code in torchfsm/mesh.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def __getitem__(self, idx: int) -> SpatialTensor["H ..."]:
    if len(self.meshs) <= idx:
        if idx > 2:
            raise ValueError(f"mesh dim with id{idx} is not defined")
        else:
            raise ValueError(f"{self._dim_names[idx]} dim is not defined")
    if len(self.meshs[idx]) == 0:
        self.meshs[idx] = (
            (self.mesh_info[idx][1] - self.mesh_info[idx][0])
            * torch.arange(
                0, self.mesh_info[idx][2], device=self.device, dtype=self.dtype
            )
            / self.mesh_info[idx][2]
        )
    return self.meshs[idx]
mesh_grid ¤
mesh_grid(
    numpy=False,
) -> ValueList[
    Union[
        SpatialTensor["H ..."],
        Annotated[np.ndarray, "Spatial, H ..."],
    ]
]

Generate the mesh grid for all dimensions. The shape of the mesh grid will be (n1,n2,n3,...,nk).

Parameters:

Name Type Description Default
numpy bool

whether to return the mesh grid as numpy array

False

Returns:

Type Description
ValueList[Union[SpatialTensor['H ...'], Annotated[ndarray, 'Spatial, H ...']]]

torch.Tensor: mesh grid for all dimensions

Source code in torchfsm/mesh.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def mesh_grid(
    self, numpy=False
) -> ValueList[
    Union[SpatialTensor["H ..."], Annotated[np.ndarray, "Spatial, H ..."]]
]:
    """
    Generate the mesh grid for all dimensions.
    The shape of the mesh grid will be (n1,n2,n3,...,nk).

    Args:
        numpy (bool): whether to return the mesh grid as numpy array

    Returns:
        torch.Tensor: mesh grid for all dimensions
    """
    if numpy:
        mesh_grid = np.meshgrid(*[self[i] for i in range(len(self))], indexing="ij")
    else:
        mesh_grid = torch.meshgrid(
            *[self[i] for i in range(len(self))], indexing="ij"
        )
    if len(mesh_grid) == 1:
        return mesh_grid[0]
    return mesh_grid
bc_mesh_grid ¤
bc_mesh_grid(
    batch_size: int = 1, n_channel: int = 1, numpy=False
) -> ValueList[
    Union[
        SpatialTensor["B C H ..."],
        Annotated[np.ndarray, "Spatial, B C H ..."],
    ]
]

Generate the mesh grid with batch size and channel size. The shape of the mesh grid will be (batch_size,n_channel,n1,n2,n3,...,nk).

Parameters:

Name Type Description Default
batch_size int

batch size

1
n_channel int

channel size

1
numpy bool

whether to return the mesh grid as numpy array

False

Returns:

Type Description
ValueList[Union[SpatialTensor['B C H ...'], Annotated[ndarray, 'Spatial, B C H ...']]]

torch.Tensor: mesh grid with batch size and channel size

Source code in torchfsm/mesh.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def bc_mesh_grid(
    self, batch_size: int = 1, n_channel: int = 1, numpy=False
) -> ValueList[
    Union[SpatialTensor["B C H ..."], Annotated[np.ndarray, "Spatial, B C H ..."]]
]:
    """
    Generate the mesh grid with batch size and channel size.
    The shape of the mesh grid will be (batch_size,n_channel,n1,n2,n3,...,nk).

    Args:
        batch_size (int): batch size
        n_channel (int): channel size
        numpy (bool): whether to return the mesh grid as numpy array

    Returns:
        torch.Tensor: mesh grid with batch size and channel size
    """
    bc_mesh_grid = []
    mesh_grid = self.mesh_grid()
    if not isinstance(mesh_grid, Sequence):
        mesh_grid = [mesh_grid]
    for i in range(len(mesh_grid)):
        bc_mesh_grid.append(
            mesh_grid[i]
            .reshape(1, 1, *mesh_grid[i].shape)
            .repeat(batch_size, n_channel, *[1] * len(mesh_grid[i].shape))
        )
    if numpy:
        mesh_grid = [i.cpu().numpy() for i in bc_mesh_grid]
    if len(bc_mesh_grid) == 1:
        return bc_mesh_grid[0]
    return bc_mesh_grid
to ¤
to(device=None, dtype=None)
Source code in torchfsm/mesh.py
144
145
def to(self, device=None, dtype=None):
    self.__init__(self.mesh_info, device=device, dtype=dtype)

torchfsm.mesh.FFTFrequency ¤

FFT frequency for each dimension. The length of the class is determined by the number of dimension. The attribute f_x, f_y, f_z are the fft frequency for the first three dimension. You can also access the fft frequency for other dimension by indexing the object. E.g., fft_frequency[0] is the fft frequency for the first dimension, equivalent to f_x. There is no limit for the number of dimension.

Parameters:

Name Type Description Default
mesh_info Sequence[tuple[float, float, int]]

sequence of tuple (start,end,n_points) for each dimension

[]
device

device for the fft frequency

None
dtype

data type for the fft frequency

None
Source code in torchfsm/mesh.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
class FFTFrequency:
    """
    FFT frequency for each dimension.
    The length of the class is determined by the number of dimension.
    The attribute f_x, f_y, f_z are the fft frequency for the first three dimension.
    You can also access the fft frequency for other dimension by indexing the object.
    E.g., fft_frequency[0] is the fft frequency for the first dimension, equivalent to f_x.
    There is no limit for the number of dimension.

    Args:
        mesh_info (Sequence[tuple[float,float,int]]): sequence of tuple (start,end,n_points) for each dimension
        device: device for the fft frequency
        dtype: data type for the fft frequency

    """

    def __init__(
        self,
        mesh_info: Sequence[tuple[float, float, int]] = [],
        device=None,
        dtype=None,
    ) -> None:
        self._dim_names = ["x", "y", "z"]
        self.mesh_info = mesh_info
        self.fs = [[] for _ in range(len(mesh_info))]
        self.device, self.dtype = format_device_dtype(device, dtype)

    def __len__(self):
        return len(self.mesh_info)

    def __getitem__(self, idx: int):
        if len(self.fs) <= idx:
            if idx > 2:
                raise ValueError(f"fft frequency with id{idx} is not defined")
            else:
                raise ValueError(f"{self._dim_names[idx]} fft frequency is not defined")
        if len(self.fs[idx]) == 0:
            self.fs[idx] = torch.fft.fftfreq(
                self.mesh_info[idx][2],
                (self.mesh_info[idx][1] - self.mesh_info[idx][0])
                / self.mesh_info[idx][2],
                device=self.device,
                dtype=self.dtype,
            )
        return self.fs[idx]

    @property
    def f_x(self) -> torch.Tensor:
        """
        FFT frequency for the first dimension
        """
        return self[0]

    @property
    def f_y(self) -> torch.Tensor:
        """
        FFT frequency for the second dimension
        """
        return self[1]

    @property
    def f_z(self) -> torch.Tensor:
        """
        FFT frequency for the third dimension
        """
        return self[2]

    def to(self, device=None, dtype=None):
        self.__init__(self.mesh_info, device=device, dtype=dtype)
mesh_info instance-attribute ¤
mesh_info = mesh_info
fs instance-attribute ¤
fs = [[] for _ in range(len(mesh_info))]
f_x property ¤
f_x: Tensor

FFT frequency for the first dimension

f_y property ¤
f_y: Tensor

FFT frequency for the second dimension

f_z property ¤
f_z: Tensor

FFT frequency for the third dimension

__init__ ¤
__init__(
    mesh_info: Sequence[tuple[float, float, int]] = [],
    device=None,
    dtype=None,
) -> None
Source code in torchfsm/mesh.py
164
165
166
167
168
169
170
171
172
173
def __init__(
    self,
    mesh_info: Sequence[tuple[float, float, int]] = [],
    device=None,
    dtype=None,
) -> None:
    self._dim_names = ["x", "y", "z"]
    self.mesh_info = mesh_info
    self.fs = [[] for _ in range(len(mesh_info))]
    self.device, self.dtype = format_device_dtype(device, dtype)
__len__ ¤
__len__()
Source code in torchfsm/mesh.py
175
176
def __len__(self):
    return len(self.mesh_info)
__getitem__ ¤
__getitem__(idx: int)
Source code in torchfsm/mesh.py
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
def __getitem__(self, idx: int):
    if len(self.fs) <= idx:
        if idx > 2:
            raise ValueError(f"fft frequency with id{idx} is not defined")
        else:
            raise ValueError(f"{self._dim_names[idx]} fft frequency is not defined")
    if len(self.fs[idx]) == 0:
        self.fs[idx] = torch.fft.fftfreq(
            self.mesh_info[idx][2],
            (self.mesh_info[idx][1] - self.mesh_info[idx][0])
            / self.mesh_info[idx][2],
            device=self.device,
            dtype=self.dtype,
        )
    return self.fs[idx]
to ¤
to(device=None, dtype=None)
Source code in torchfsm/mesh.py
215
216
def to(self, device=None, dtype=None):
    self.__init__(self.mesh_info, device=device, dtype=dtype)

torchfsm.mesh.BroadcastedFFTFrequency ¤

Broadcasted fft frequency for each dimension. The fft frequency is broadcasted to the shape of the value field. For example, if the value field is of shape (batch_size,1,nx,ny,nz), the fft frequency for the first, second and third dimension will be broadcasted to (1,1,nx,1,1), (1,1,1,ny,1), (1,1,1,1,nz) respectively. The length of the class is determined by the number of dimension. The attribute bf_x, bf_y, bf_z are the broadcasted fft frequency for the first three dimension. You can also access the broadcasted fft frequency for other dimension by indexing the object. E.g., broadcasted_fft_frequency[0] is the broadcasted fft frequency for the first dimension, equivalent to bf_x.

Parameters:

Name Type Description Default
fft_frequency FFTFrequency

FFTFrequency object

required
Source code in torchfsm/mesh.py
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
class BroadcastedFFTFrequency:
    """
    Broadcasted fft frequency for each dimension.
    The fft frequency is broadcasted to the shape of the value field.
    For example, if the value field is of shape (batch_size,1,nx,ny,nz),
    the fft frequency for the first, second and third dimension will be broadcasted to (1,1,nx,1,1), (1,1,1,ny,1), (1,1,1,1,nz) respectively.
    The length of the class is determined by the number of dimension.
    The attribute bf_x, bf_y, bf_z are the broadcasted fft frequency for the first three dimension.
    You can also access the broadcasted fft frequency for other dimension by indexing the object.
    E.g., broadcasted_fft_frequency[0] is the broadcasted fft frequency for the first dimension, equivalent to bf_x.

    Args:
        fft_frequency (FFTFrequency): FFTFrequency object
    """

    def __init__(self, fft_frequency: FFTFrequency) -> None:
        self.fft_frequency = fft_frequency
        self.bdks = [[] for _ in range(len(self.fft_frequency))]
        self._dim_names = self.fft_frequency._dim_names
        self.mesh_shape = tuple((i[-1] for i in self.fft_frequency.mesh_info))
        self._bf_vector = None

    def __len__(self):
        return len(self.fft_frequency)

    def __getitem__(self, idx: int) -> torch.Tensor:
        if len(self) <= idx:
            if idx > 2:
                raise ValueError(f"fft frequency with id{idx} is not defined")
            else:
                raise ValueError(f"{self._dim_names[idx]} fft frequency is not defined")
        if len(self.bdks[idx]) == 0:
            shapes = [1] * (len(self.fft_frequency) + 2)
            shapes[idx + 2] = self.fft_frequency[idx].shape[0]
            self.bdks[idx] = self.fft_frequency[idx].reshape(*shapes)
        return self.bdks[idx]

    @property
    def bf_vector(self):
        if self._bf_vector is None:
            shapes = (1, 1) + self.mesh_shape
            bks = []
            for i in range(len(self)):
                new_shape = list(shapes)
                new_shape[i + 2] = 1
                bks.append(self[i].repeat(*new_shape))
            self._bf_vector = torch.cat(bks, dim=1)
        return self._bf_vector

    @property
    def bf_vector_norm(self) -> torch.Tensor:
        """
        Broadcasted fft frequency vector norm
        """
        return torch.sqrt(torch.sum(self.bf_vector ** 2, dim=1, keepdim=True))

    @property
    def bf_x(self) -> torch.Tensor:
        """
        Broadcasted fft frequency for the first dimension
        """
        return self[0]

    @property
    def bf_y(self) -> torch.Tensor:
        """
        Broadcasted fft frequency for the second dimension
        """
        return self[1]

    @property
    def bf_z(self) -> torch.Tensor:
        """
        Broadcasted fft frequency for the third dimension
        """
        return self[2]

    def to(self, device=None, dtype=None):
        self.fft_frequency.to(device=device, dtype=dtype)
        self.__init__(self.fft_frequency)
fft_frequency instance-attribute ¤
fft_frequency = fft_frequency
bdks instance-attribute ¤
bdks = [[] for _ in range(len(fft_frequency))]
mesh_shape instance-attribute ¤
mesh_shape = tuple(i[-1] for i in mesh_info)
bf_vector property ¤
bf_vector
bf_vector_norm property ¤
bf_vector_norm: Tensor

Broadcasted fft frequency vector norm

bf_x property ¤
bf_x: Tensor

Broadcasted fft frequency for the first dimension

bf_y property ¤
bf_y: Tensor

Broadcasted fft frequency for the second dimension

bf_z property ¤
bf_z: Tensor

Broadcasted fft frequency for the third dimension

__init__ ¤
__init__(fft_frequency: FFTFrequency) -> None
Source code in torchfsm/mesh.py
234
235
236
237
238
239
def __init__(self, fft_frequency: FFTFrequency) -> None:
    self.fft_frequency = fft_frequency
    self.bdks = [[] for _ in range(len(self.fft_frequency))]
    self._dim_names = self.fft_frequency._dim_names
    self.mesh_shape = tuple((i[-1] for i in self.fft_frequency.mesh_info))
    self._bf_vector = None
__len__ ¤
__len__()
Source code in torchfsm/mesh.py
241
242
def __len__(self):
    return len(self.fft_frequency)
__getitem__ ¤
__getitem__(idx: int) -> torch.Tensor
Source code in torchfsm/mesh.py
244
245
246
247
248
249
250
251
252
253
254
def __getitem__(self, idx: int) -> torch.Tensor:
    if len(self) <= idx:
        if idx > 2:
            raise ValueError(f"fft frequency with id{idx} is not defined")
        else:
            raise ValueError(f"{self._dim_names[idx]} fft frequency is not defined")
    if len(self.bdks[idx]) == 0:
        shapes = [1] * (len(self.fft_frequency) + 2)
        shapes[idx + 2] = self.fft_frequency[idx].shape[0]
        self.bdks[idx] = self.fft_frequency[idx].reshape(*shapes)
    return self.bdks[idx]
to ¤
to(device=None, dtype=None)
Source code in torchfsm/mesh.py
296
297
298
def to(self, device=None, dtype=None):
    self.fft_frequency.to(device=device, dtype=dtype)
    self.__init__(self.fft_frequency)

torchfsm.mesh.FourierMesh ¤

A class contains the fft frequency information and basic deritivate operators for a mesh system. This class is used inside of an Operator class.

Parameters:

Name Type Description Default
mesh Union[Sequence[tuple[float, float, int]], MeshGrid]

mesh information for the Fourier spectral method it can be a sequence of tuple (start,end,n_points) for each dimension or a MeshGrid object

required
device

device for the fft frequency

None
dtype

data type for the fft frequency

None

Attributes:

Name Type Description
k FFTFrequency

fft frequency for each dimension It is indexed by the dimension id, e.g., k[0] is the fft frequency for the first dimension

bk BroadcastedFFTFrequency

broadcasted fft frequency for each dimension It is indexed by the dimension id, e.g., bk[0] is the broadcasted fft frequency for the first dimension

f_x Tensor

fft frequency for the first dimension

f_y Tensor

fft frequency for the second dimension

f_z Tensor

fft frequency for the third dimension

bf_x Tensor

broadcasted fft frequency for the first dimension

bf_y Tensor

broadcasted fft frequency for the second dimension

bf_z Tensor

broadcasted fft frequency for the third dimension

n_dim int

number of dimension

fft_dim tuple

tuple of the dimension for the fft operation

Parameters:

Name Type Description Default
mesh Union[Sequence[tuple[float, float, int]], MeshGrid]

mesh information for the Fourier spectral method it can be a sequence of tuple (start,end,n_points) for each dimension or a MeshGrid object

required
device

device for the fft frequency. If you initialze the obkjct with a MeshGrid object and you dont specify this parameter, the device will be the same as the MeshGrid object.

None
dtype

data type for the fft frequency. If you initialze the obkjct with a MeshGrid object and you dont specify this parameter, the dtype will be the same as the MeshGrid object.

None
Source code in torchfsm/mesh.py
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
class FourierMesh:
    """
    A class contains the fft frequency information and basic deritivate operators for a mesh system.
    This class is used inside of an Operator class.

    Args:
        mesh (Union[Sequence[tuple[float, float, int]],MeshGrid]): mesh information for the Fourier spectral method
            it can be a sequence of tuple (start,end,n_points) for each dimension or a MeshGrid object
        device: device for the fft frequency
        dtype: data type for the fft frequency

    Attributes:
        k (FFTFrequency): fft frequency for each dimension
            It is indexed by the dimension id, e.g., k[0] is the fft frequency for the first dimension
        bk (BroadcastedFFTFrequency): broadcasted fft frequency for each dimension
            It is indexed by the dimension id, e.g., bk[0] is the broadcasted fft frequency for the first dimension
        f_x (torch.Tensor): fft frequency for the first dimension
        f_y (torch.Tensor): fft frequency for the second dimension
        f_z (torch.Tensor): fft frequency for the third dimension
        bf_x (torch.Tensor): broadcasted fft frequency for the first dimension
        bf_y (torch.Tensor): broadcasted fft frequency for the second dimension
        bf_z (torch.Tensor): broadcasted fft frequency for the third dimension
        n_dim (int): number of dimension
        fft_dim (tuple): tuple of the dimension for the fft operation

    Args:
        mesh (Union[Sequence[tuple[float, float, int]],MeshGrid]): mesh information for the Fourier spectral method
            it can be a sequence of tuple (start,end,n_points) for each dimension or a MeshGrid object
        device: device for the fft frequency. If you initialze the obkjct with a MeshGrid object and you dont specify this parameter, the device will be the same as the MeshGrid object.
        dtype: data type for the fft frequency. If you initialze the obkjct with a MeshGrid object and you dont specify this parameter, the dtype will be the same as the MeshGrid object.
    """

    def __init__(
        self,
        mesh: Union[Sequence[tuple[float, float, int]], MeshGrid],
        device=None,
        dtype=None,
    ) -> None:
        if isinstance(mesh, MeshGrid):
            self.mesh_info = mesh.mesh_info
            self.device = default(device, mesh.device)
            self.dtype = default(dtype, mesh.dtype)
            self.device, self.dtype = format_device_dtype(self.device, self.dtype)
        else:
            self.mesh_info = mesh
            self.device, self.dtype = format_device_dtype(device, dtype)
        self.f = FFTFrequency(self.mesh_info, device=self.device, dtype=self.dtype)
        self.bf = BroadcastedFFTFrequency(self.f)
        self.n_dim = len(self.mesh_info)
        self.fft_dim = tuple(-1 * (i + 1) for i in range(self.n_dim))
        self._default_rel_freq_threshold = 2 / 3

    def set_default_rel_freq_threshold(self, threshold: float):
        self._default_rel_freq_threshold = threshold
        #self.low_pass_filter.cache_clear()

    @property
    def f_x(self) -> torch.Tensor:
        """
        Fft frequency for the first dimension
        """
        return self.f.f_x

    @property
    def f_y(self) -> torch.Tensor:
        """
        Fft frequency for the second dimension
        """
        return self.f.f_y

    @property
    def f_z(self) -> torch.Tensor:
        """
        Fft frequency for the third dimension
        """
        return self.f.f_z

    @property
    def bf_x(self) -> torch.Tensor:
        """
        Broadcasted fft frequency for the first dimension
        """
        return self.bf.bf_x

    @property
    def bf_y(self) -> torch.Tensor:
        """
        Broadcasted fft frequency for the second dimension
        """
        return self.bf.bf_y

    @property
    def bf_z(self) -> torch.Tensor:
        """
        Broadcasted fft frequency for the third dimension
        """
        return self.bf.bf_z

    @property
    def bf_vector(self) -> torch.Tensor:
        """
        Broadcasted fft frequency for all dimensions
        """
        return self.bf.bf_vector

    @property
    def bf_vector_norm(self) -> torch.Tensor:
        """
        Broadcasted fft frequency vector norm
        """
        return self.bf.bf_vector_norm

    #@lru_cache()
    def grad(self, dim_i: int, order: int) -> FourierTensor["B C H ..."]:
        """
        Linear operator for the nth order gradient w.r.t the ith dimension.
        """
        return (2j * torch.pi * self.bf[dim_i]) ** order

    #@lru_cache()
    def laplacian(self) -> FourierTensor["B C H ..."]:
        """
        Linear operator for the nth order Laplacian.
        """
        return self.nabla(2)

    #@lru_cache()
    def invert_laplacian(self) -> FourierTensor["B C H ..."]:
        """
        Linear operator for the nth order inverse Laplacian.
        """
        lap = self.laplacian()
        return torch.where(lap == 0, 1.0, 1 / lap)

    #@lru_cache()
    def nabla(self, order: int = 1) -> FourierTensor["B C H ..."]:
        """
        Linear operator for the nth order gradient.
        """
        return sum([self.grad(dim_i, order) for dim_i in range(len(self.bf))])

    #@lru_cache()
    def invert_nabla(self, order: int = 1) -> FourierTensor["B C H ..."]:
        """
        Linear operator for the nth order inverse gradient.
        """
        nab = self.nabla(order)
        return torch.where(nab == 0, 1.0, 1 / nab)

    #@lru_cache()
    def nabla_vector(self, order: int) -> FourierTensor["B C H ..."]:
        """
        Linear operator vector for the nth order gradient.
        """
        return (2j * torch.pi * self.bf.bf_vector) ** order

    #@lru_cache()
    def low_pass_filter(self, rel_freq_threshold: Optional[float] = None) -> torch.Tensor:
        """
        Low pass filter mask for the Fourier coefficients.

        The mask is a tensor of the same shape as the Fourier coefficients, with values 1 for frequencies below the threshold and 0 for frequencies above the threshold.
        Args:
            rel_freq_threshold (Optional[float]): The relative frequency threshold for the low pass filter. If None, the default frequency threshold will be used.
                Default is None, which uses the value set by `set_default_rel_freq_threshold`.

        Returns:
            torch.Tensor: A mask tensor with the same shape as the Fourier coefficients, where values are 1 for frequencies below the threshold and 0 for frequencies above the threshold.
        """
        rel_freq_threshold = default(rel_freq_threshold, self._default_rel_freq_threshold)
        mask = torch.ones_like(self.nabla().real)
        for i in range(len(self.bf)):
            abs_f = self.bf[i].abs()
            mask *= torch.where(abs_f > abs_f.max() * rel_freq_threshold, 0, 1)
        return mask.to(device=self.device, dtype=self.dtype)

    #@lru_cache()
    def abs_low_pass_filter(self, abs_freq_threshold: int) -> torch.Tensor:
        """
        Low pass filter mask for the Fourier coefficients.

        The mask is a tensor of the same shape as the Fourier coefficients, with values 1 for frequencies below the threshold and 0 for frequencies above the threshold.
        Args:
            abs_rel_freq_threshold (Optional[float]): The absolute frequency threshold for the low pass filter.

        Returns:
            torch.Tensor: A mask tensor with the same shape as the Fourier coefficients, where values are 1 for frequencies below the threshold and 0 for frequencies above the threshold.
        """
        mask = torch.ones_like(self.nabla().real)
        for i in range(len(self.bf)):
            abs_f = self.bf[i].abs()
            mask *= torch.where(abs_f > abs_freq_threshold, 0, 1)
        return mask.to(device=self.device, dtype=self.dtype)

    #@lru_cache()
    def normalized_low_pass_filter(self, normalized_freq_threshold: float) -> torch.Tensor:
        """
        Low pass filter mask for the Fourier coefficients based on normalized frequency.
        This is equivalent to the `abs_low_pass_filter` with abs_freq_threshold = normalized_freq_threshold/domain_length

        The mask is a tensor of the same shape as the Fourier coefficients, with values 1 for frequencies below the threshold and 0 for frequencies above the threshold.
        Args:
            normalized_freq_threshold (float): The normalized frequency threshold for the low pass filter.

        Returns:
            torch.Tensor: A mask tensor with the same shape as the Fourier coefficients, where values are 1 for frequencies below the threshold and 0 for frequencies above the threshold.
        """
        mask = torch.ones_like(self.nabla().real)
        for i in range(len(self.bf)):
            abs_f = self.bf[i].abs()
            mask *= torch.where(abs_f > normalized_freq_threshold/(self.mesh_info[i][1]-self.mesh_info[i][0]), 0, 1)
        return mask.to(device=self.device, dtype=self.dtype)

    def fft(self, u) -> FourierTensor["B C H ..."]:
        """
        Fast Fourier Transform
        """
        return torch.fft.fftn(u, dim=self.fft_dim)

    def ifft(self, u_fft) -> SpatialTensor["B C H ..."]:
        """
        Inverse Fast Fourier Transform
        """
        return torch.fft.ifftn(u_fft, dim=self.fft_dim)

    def to(self, device=None, dtype=None):
        """
        Move the mesh to a different device and dtype.
        """
        """
        self.__init__(self.mesh_info, device=device, dtype=dtype)
        self.grad.cache_clear()
        self.laplacian.cache_clear()
        self.invert_laplacian.cache_clear()
        self.nabla.cache_clear()
        self.invert_nabla.cache_clear()
        self.nabla_vector.cache_clear()
        self.low_pass_filter.cache_clear()
        """
mesh_info instance-attribute ¤
mesh_info = mesh_info
device instance-attribute ¤
device = default(device, device)
dtype instance-attribute ¤
dtype = default(dtype, dtype)
f instance-attribute ¤
f = FFTFrequency(mesh_info, device=device, dtype=dtype)
bf instance-attribute ¤
bf = BroadcastedFFTFrequency(f)
n_dim instance-attribute ¤
n_dim = len(mesh_info)
fft_dim instance-attribute ¤
fft_dim = tuple(-1 * i + 1 for i in range(n_dim))
f_x property ¤
f_x: Tensor

Fft frequency for the first dimension

f_y property ¤
f_y: Tensor

Fft frequency for the second dimension

f_z property ¤
f_z: Tensor

Fft frequency for the third dimension

bf_x property ¤
bf_x: Tensor

Broadcasted fft frequency for the first dimension

bf_y property ¤
bf_y: Tensor

Broadcasted fft frequency for the second dimension

bf_z property ¤
bf_z: Tensor

Broadcasted fft frequency for the third dimension

bf_vector property ¤
bf_vector: Tensor

Broadcasted fft frequency for all dimensions

bf_vector_norm property ¤
bf_vector_norm: Tensor

Broadcasted fft frequency vector norm

__init__ ¤
__init__(
    mesh: Union[
        Sequence[tuple[float, float, int]], MeshGrid
    ],
    device=None,
    dtype=None,
) -> None
Source code in torchfsm/mesh.py
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
def __init__(
    self,
    mesh: Union[Sequence[tuple[float, float, int]], MeshGrid],
    device=None,
    dtype=None,
) -> None:
    if isinstance(mesh, MeshGrid):
        self.mesh_info = mesh.mesh_info
        self.device = default(device, mesh.device)
        self.dtype = default(dtype, mesh.dtype)
        self.device, self.dtype = format_device_dtype(self.device, self.dtype)
    else:
        self.mesh_info = mesh
        self.device, self.dtype = format_device_dtype(device, dtype)
    self.f = FFTFrequency(self.mesh_info, device=self.device, dtype=self.dtype)
    self.bf = BroadcastedFFTFrequency(self.f)
    self.n_dim = len(self.mesh_info)
    self.fft_dim = tuple(-1 * (i + 1) for i in range(self.n_dim))
    self._default_rel_freq_threshold = 2 / 3
set_default_rel_freq_threshold ¤
set_default_rel_freq_threshold(threshold: float)
Source code in torchfsm/mesh.py
353
354
def set_default_rel_freq_threshold(self, threshold: float):
    self._default_rel_freq_threshold = threshold
grad ¤
grad(dim_i: int, order: int) -> FourierTensor['B C H ...']

Linear operator for the nth order gradient w.r.t the ith dimension.

Source code in torchfsm/mesh.py
414
415
416
417
418
def grad(self, dim_i: int, order: int) -> FourierTensor["B C H ..."]:
    """
    Linear operator for the nth order gradient w.r.t the ith dimension.
    """
    return (2j * torch.pi * self.bf[dim_i]) ** order
laplacian ¤
laplacian() -> FourierTensor['B C H ...']

Linear operator for the nth order Laplacian.

Source code in torchfsm/mesh.py
421
422
423
424
425
def laplacian(self) -> FourierTensor["B C H ..."]:
    """
    Linear operator for the nth order Laplacian.
    """
    return self.nabla(2)
invert_laplacian ¤
invert_laplacian() -> FourierTensor['B C H ...']

Linear operator for the nth order inverse Laplacian.

Source code in torchfsm/mesh.py
428
429
430
431
432
433
def invert_laplacian(self) -> FourierTensor["B C H ..."]:
    """
    Linear operator for the nth order inverse Laplacian.
    """
    lap = self.laplacian()
    return torch.where(lap == 0, 1.0, 1 / lap)
nabla ¤
nabla(order: int = 1) -> FourierTensor['B C H ...']

Linear operator for the nth order gradient.

Source code in torchfsm/mesh.py
436
437
438
439
440
def nabla(self, order: int = 1) -> FourierTensor["B C H ..."]:
    """
    Linear operator for the nth order gradient.
    """
    return sum([self.grad(dim_i, order) for dim_i in range(len(self.bf))])
invert_nabla ¤
invert_nabla(order: int = 1) -> FourierTensor['B C H ...']

Linear operator for the nth order inverse gradient.

Source code in torchfsm/mesh.py
443
444
445
446
447
448
def invert_nabla(self, order: int = 1) -> FourierTensor["B C H ..."]:
    """
    Linear operator for the nth order inverse gradient.
    """
    nab = self.nabla(order)
    return torch.where(nab == 0, 1.0, 1 / nab)
nabla_vector ¤
nabla_vector(order: int) -> FourierTensor['B C H ...']

Linear operator vector for the nth order gradient.

Source code in torchfsm/mesh.py
451
452
453
454
455
def nabla_vector(self, order: int) -> FourierTensor["B C H ..."]:
    """
    Linear operator vector for the nth order gradient.
    """
    return (2j * torch.pi * self.bf.bf_vector) ** order
low_pass_filter ¤
low_pass_filter(
    rel_freq_threshold: Optional[float] = None,
) -> torch.Tensor

Low pass filter mask for the Fourier coefficients.

The mask is a tensor of the same shape as the Fourier coefficients, with values 1 for frequencies below the threshold and 0 for frequencies above the threshold. Args: rel_freq_threshold (Optional[float]): The relative frequency threshold for the low pass filter. If None, the default frequency threshold will be used. Default is None, which uses the value set by set_default_rel_freq_threshold.

Returns:

Type Description
Tensor

torch.Tensor: A mask tensor with the same shape as the Fourier coefficients, where values are 1 for frequencies below the threshold and 0 for frequencies above the threshold.

Source code in torchfsm/mesh.py
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
def low_pass_filter(self, rel_freq_threshold: Optional[float] = None) -> torch.Tensor:
    """
    Low pass filter mask for the Fourier coefficients.

    The mask is a tensor of the same shape as the Fourier coefficients, with values 1 for frequencies below the threshold and 0 for frequencies above the threshold.
    Args:
        rel_freq_threshold (Optional[float]): The relative frequency threshold for the low pass filter. If None, the default frequency threshold will be used.
            Default is None, which uses the value set by `set_default_rel_freq_threshold`.

    Returns:
        torch.Tensor: A mask tensor with the same shape as the Fourier coefficients, where values are 1 for frequencies below the threshold and 0 for frequencies above the threshold.
    """
    rel_freq_threshold = default(rel_freq_threshold, self._default_rel_freq_threshold)
    mask = torch.ones_like(self.nabla().real)
    for i in range(len(self.bf)):
        abs_f = self.bf[i].abs()
        mask *= torch.where(abs_f > abs_f.max() * rel_freq_threshold, 0, 1)
    return mask.to(device=self.device, dtype=self.dtype)
abs_low_pass_filter ¤
abs_low_pass_filter(
    abs_freq_threshold: int,
) -> torch.Tensor

Low pass filter mask for the Fourier coefficients.

The mask is a tensor of the same shape as the Fourier coefficients, with values 1 for frequencies below the threshold and 0 for frequencies above the threshold. Args: abs_rel_freq_threshold (Optional[float]): The absolute frequency threshold for the low pass filter.

Returns:

Type Description
Tensor

torch.Tensor: A mask tensor with the same shape as the Fourier coefficients, where values are 1 for frequencies below the threshold and 0 for frequencies above the threshold.

Source code in torchfsm/mesh.py
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
def abs_low_pass_filter(self, abs_freq_threshold: int) -> torch.Tensor:
    """
    Low pass filter mask for the Fourier coefficients.

    The mask is a tensor of the same shape as the Fourier coefficients, with values 1 for frequencies below the threshold and 0 for frequencies above the threshold.
    Args:
        abs_rel_freq_threshold (Optional[float]): The absolute frequency threshold for the low pass filter.

    Returns:
        torch.Tensor: A mask tensor with the same shape as the Fourier coefficients, where values are 1 for frequencies below the threshold and 0 for frequencies above the threshold.
    """
    mask = torch.ones_like(self.nabla().real)
    for i in range(len(self.bf)):
        abs_f = self.bf[i].abs()
        mask *= torch.where(abs_f > abs_freq_threshold, 0, 1)
    return mask.to(device=self.device, dtype=self.dtype)
normalized_low_pass_filter ¤
normalized_low_pass_filter(
    normalized_freq_threshold: float,
) -> torch.Tensor

Low pass filter mask for the Fourier coefficients based on normalized frequency. This is equivalent to the abs_low_pass_filter with abs_freq_threshold = normalized_freq_threshold/domain_length

The mask is a tensor of the same shape as the Fourier coefficients, with values 1 for frequencies below the threshold and 0 for frequencies above the threshold. Args: normalized_freq_threshold (float): The normalized frequency threshold for the low pass filter.

Returns:

Type Description
Tensor

torch.Tensor: A mask tensor with the same shape as the Fourier coefficients, where values are 1 for frequencies below the threshold and 0 for frequencies above the threshold.

Source code in torchfsm/mesh.py
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
def normalized_low_pass_filter(self, normalized_freq_threshold: float) -> torch.Tensor:
    """
    Low pass filter mask for the Fourier coefficients based on normalized frequency.
    This is equivalent to the `abs_low_pass_filter` with abs_freq_threshold = normalized_freq_threshold/domain_length

    The mask is a tensor of the same shape as the Fourier coefficients, with values 1 for frequencies below the threshold and 0 for frequencies above the threshold.
    Args:
        normalized_freq_threshold (float): The normalized frequency threshold for the low pass filter.

    Returns:
        torch.Tensor: A mask tensor with the same shape as the Fourier coefficients, where values are 1 for frequencies below the threshold and 0 for frequencies above the threshold.
    """
    mask = torch.ones_like(self.nabla().real)
    for i in range(len(self.bf)):
        abs_f = self.bf[i].abs()
        mask *= torch.where(abs_f > normalized_freq_threshold/(self.mesh_info[i][1]-self.mesh_info[i][0]), 0, 1)
    return mask.to(device=self.device, dtype=self.dtype)
fft ¤
fft(u) -> FourierTensor['B C H ...']

Fast Fourier Transform

Source code in torchfsm/mesh.py
514
515
516
517
518
def fft(self, u) -> FourierTensor["B C H ..."]:
    """
    Fast Fourier Transform
    """
    return torch.fft.fftn(u, dim=self.fft_dim)
ifft ¤
ifft(u_fft) -> SpatialTensor['B C H ...']

Inverse Fast Fourier Transform

Source code in torchfsm/mesh.py
520
521
522
523
524
def ifft(self, u_fft) -> SpatialTensor["B C H ..."]:
    """
    Inverse Fast Fourier Transform
    """
    return torch.fft.ifftn(u_fft, dim=self.fft_dim)
to ¤
to(device=None, dtype=None)

Move the mesh to a different device and dtype.

Source code in torchfsm/mesh.py
526
527
528
529
530
531
532
533
534
535
536
537
538
539
def to(self, device=None, dtype=None):
    """
    Move the mesh to a different device and dtype.
    """
    """
    self.__init__(self.mesh_info, device=device, dtype=dtype)
    self.grad.cache_clear()
    self.laplacian.cache_clear()
    self.invert_laplacian.cache_clear()
    self.nabla.cache_clear()
    self.invert_nabla.cache_clear()
    self.nabla_vector.cache_clear()
    self.low_pass_filter.cache_clear()
    """

torchfsm.mesh.mesh_shape ¤

mesh_shape(
    mesh: Union[
        Sequence[tuple[float, float, int]],
        MeshGrid,
        FourierMesh,
    ],
    batch_size: int = 1,
    n_channel: int = 1,
) -> Tuple

Get the shape of the mesh. The shape is in the form of (batch_size, n_channel, n1, n2, n3, ...).

Parameters:

Name Type Description Default
mesh Union[Sequence[tuple[float, float, int]], MeshGrid, FourierMesh]

The mesh to get the shape from. If a sequence is provided, it should be in the form of [(x_min, x_max, n_points), ...].

required
batch_size int

The number of batches. Default is 1.

1
n_channel int

The number of channels. Default is 1.

1

Returns:

Name Type Description
Tuple Tuple

The shape of the mesh.

Source code in torchfsm/mesh.py
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
def mesh_shape(
    mesh: Union[Sequence[tuple[float, float, int]], MeshGrid, FourierMesh],
    batch_size: int = 1,
    n_channel: int = 1,
) -> Tuple:
    """
    Get the shape of the mesh.
    The shape is in the form of (batch_size, n_channel, n1, n2, n3, ...).

    Args:
        mesh (Union[Sequence[tuple[float, float, int]], MeshGrid, FourierMesh]): The mesh to get the shape from.
            If a sequence is provided, it should be in the form of [(x_min, x_max, n_points), ...].
        batch_size (int): The number of batches. Default is 1.
        n_channel (int): The number of channels. Default is 1.

    Returns:
        Tuple: The shape of the mesh.
    """
    if isinstance(mesh, FourierMesh) or isinstance(mesh, MeshGrid):
        mesh = mesh.mesh_info
    return tuple([batch_size, n_channel] + [m[2] for m in mesh])