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 | |
__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 | |
__len__
¤
__len__()
Source code in torchfsm/mesh.py
46 47 | |
__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 | |
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 | |
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 | |
to
¤
to(device=None, dtype=None)
Source code in torchfsm/mesh.py
144 145 | |
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 | |
__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 | |
__len__
¤
__len__()
Source code in torchfsm/mesh.py
175 176 | |
__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 | |
to
¤
to(device=None, dtype=None)
Source code in torchfsm/mesh.py
215 216 | |
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 | |
__init__
¤
__init__(fft_frequency: FFTFrequency) -> None
Source code in torchfsm/mesh.py
234 235 236 237 238 239 | |
__len__
¤
__len__()
Source code in torchfsm/mesh.py
241 242 | |
__getitem__
¤
__getitem__(idx: int) -> torch.Tensor
Source code in torchfsm/mesh.py
244 245 246 247 248 249 250 251 252 253 254 | |
to
¤
to(device=None, dtype=None)
Source code in torchfsm/mesh.py
296 297 298 | |
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 | |
__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 | |
set_default_rel_freq_threshold
¤
set_default_rel_freq_threshold(threshold: float)
Source code in torchfsm/mesh.py
353 354 | |
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 | |
laplacian
¤
laplacian() -> FourierTensor['B C H ...']
Linear operator for the nth order Laplacian.
Source code in torchfsm/mesh.py
421 422 423 424 425 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
fft
¤
fft(u) -> FourierTensor['B C H ...']
Fast Fourier Transform
Source code in torchfsm/mesh.py
514 515 516 517 518 | |
ifft
¤
ifft(u_fft) -> SpatialTensor['B C H ...']
Inverse Fast Fourier Transform
Source code in torchfsm/mesh.py
520 521 522 523 524 | |
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 | |
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 | |