Skip to content

Commit

Permalink
[field] Add Field.as_boundary()
Browse files Browse the repository at this point in the history
  • Loading branch information
holl- committed May 1, 2024
1 parent 2d6296f commit 8d4762a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/Complex_Boundaries.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
{
"cell_type": "markdown",
"source": [
"Next, we define the full domain boundary. When using a `Field` as a Dirichlet boundary condition, we need to convert it using `field.as_boundary()`. For the right side, we use free flow (Neumann) and for the top and bottom, we set velocity=0."
"Next, we define the full domain boundary. When using a `Field` as a Dirichlet boundary condition, we need to convert it using `as_boundary()`. For the right side, we use free flow (Neumann) and for the top and bottom, we set velocity=0."
],
"metadata": {
"id": "FTr1BaPXFdVC"
Expand All @@ -132,7 +132,7 @@
{
"cell_type": "code",
"source": [
"boundary = {'x-': field.as_boundary(inflow, None), 'x+': ZERO_GRADIENT, 'y': 0}"
"boundary = {'x-': inflow.as_boundary(), 'x+': ZERO_GRADIENT, 'y': 0}"
],
"metadata": {
"id": "VeZgY-liClie"
Expand Down
18 changes: 13 additions & 5 deletions phi/field/_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,18 @@ def grid_scatter(self, *args, **kwargs):
from ._resample import grid_scatter
return grid_scatter(self, *args, **kwargs)

def as_boundary(self) -> Extrapolation:
"""
Returns an `Extrapolation` representing this 'Field''s values as a Dirichlet (constant) boundary.
If this `Field` encloses the required boundaries, its values will be interpolated to the required boundaries.
If boundaries outside of this `Field`'s sampled domain are required, this `Field`'s boundary conditions will be applied to determine the boundary values.
Returns:
`Extrapolation`
"""
from ._embed import FieldEmbedding
return FieldEmbedding(self)


def as_boundary(obj: Union[Extrapolation, Tensor, float, Field, None], _geometry=None) -> Extrapolation:
"""
Expand All @@ -741,11 +753,7 @@ def as_boundary(obj: Union[Extrapolation, Tensor, float, Field, None], _geometry
Returns:
`Extrapolation`
"""
if isinstance(obj, Field):
from ._embed import FieldEmbedding
return FieldEmbedding(obj)
else:
return math.extrapolation.as_extrapolation(obj)
return obj.as_boundary() if isinstance(obj, Field) else math.extrapolation.as_extrapolation(obj)


def is_staggered(values: Tensor, geometry: Geometry):
Expand Down

0 comments on commit 8d4762a

Please sign in to comment.