Create an n-dimensional grid with values sampled at the cell centers. A centered grid is defined through its `CenteredGrid.values` `phi.math.Tensor`, its `CenteredGrid.bounds` `phi.geom.Box` describing the physical size, and its `CenteredGrid.extrapolation` (`phi.math.extrapolation.Extrapol
(values: Any = 0.,
boundary: Any = 0.,
bounds: Box or float = None,
resolution: int or Shape = None,
extrapolation: Any = None,
convert=True,
**resolution_: int or Tensor)
| 19 | |
| 20 | |
| 21 | def CenteredGrid(values: Any = 0., |
| 22 | boundary: Any = 0., |
| 23 | bounds: Box or float = None, |
| 24 | resolution: int or Shape = None, |
| 25 | extrapolation: Any = None, |
| 26 | convert=True, |
| 27 | **resolution_: int or Tensor) -> Field: |
| 28 | """ |
| 29 | Create an n-dimensional grid with values sampled at the cell centers. |
| 30 | A centered grid is defined through its `CenteredGrid.values` `phi.math.Tensor`, its `CenteredGrid.bounds` `phi.geom.Box` describing the physical size, and its `CenteredGrid.extrapolation` (`phi.math.extrapolation.Extrapolation`). |
| 31 | |
| 32 | Centered grids support batch, spatial and channel dimensions. |
| 33 | |
| 34 | See Also: |
| 35 | `StaggeredGrid`, |
| 36 | `Grid`, |
| 37 | `Field`, |
| 38 | `Field`, |
| 39 | module documentation at https://tum-pbs.github.io/PhiFlow/Fields.html |
| 40 | |
| 41 | Args: |
| 42 | values: Values to use for the grid. |
| 43 | Has to be one of the following: |
| 44 | |
| 45 | * `phi.geom.Geometry`: sets inside values to 1, outside to 0 |
| 46 | * `Field`: resamples the Field to the staggered sample points |
| 47 | * `Number`: uses the value for all sample points |
| 48 | * `tuple` or `list`: interprets the sequence as vector, used for all sample points |
| 49 | * `phi.math.Tensor` compatible with grid dims: uses tensor values as grid values |
| 50 | * Function `values(x)` where `x` is a `phi.math.Tensor` representing the physical location. |
| 51 | The spatial dimensions of the grid will be passed as batch dimensions to the function. |
| 52 | |
| 53 | extrapolation: The grid extrapolation determines the value outside the `values` tensor. |
| 54 | Allowed types: `float`, `phi.math.Tensor`, `phi.math.extrapolation.Extrapolation`. |
| 55 | bounds: Physical size and location of the grid as `phi.geom.Box`. |
| 56 | If the resolution is determined through `resolution` of `values`, a `float` can be passed for `bounds` to create a unit box. |
| 57 | resolution: Grid resolution as purely spatial `phi.math.Shape`. |
| 58 | If `bounds` is given as a `Box`, the resolution may be specified as an `int` to be equal along all axes. |
| 59 | **resolution_: Spatial dimensions as keyword arguments. Typically either `resolution` or `spatial_dims` are specified. |
| 60 | convert: Whether to convert `values` to the default backend. |
| 61 | """ |
| 62 | extrapolation = boundary if extrapolation is None else extrapolation |
| 63 | if resolution is None and not resolution_: |
| 64 | assert isinstance(values, math.Tensor), "Grid resolution must be specified when 'values' is not a Tensor." |
| 65 | resolution = values.shape.spatial |
| 66 | elements = UniformGrid(resolution, bounds) |
| 67 | else: |
| 68 | resolution = _get_resolution(resolution, resolution_, bounds) |
| 69 | elements = UniformGrid(resolution, bounds) |
| 70 | if isinstance(values, math.Tensor): |
| 71 | values = math.expand(values, resolution) |
| 72 | elif isinstance(values, (Field, FieldInitializer, Geometry)): |
| 73 | values = sample(values, elements) |
| 74 | elif callable(values): |
| 75 | values = sample_function(values, elements, 'center', extrapolation) |
| 76 | else: |
| 77 | if isinstance(values, (tuple, list)) and len(values) == resolution.rank: |
| 78 | values = math.tensor(values, channel(vector=resolution.names)) |