From 8d4762adbdbd1d07db914173be3ff5510eaf444b Mon Sep 17 00:00:00 2001 From: Philipp Holl Date: Wed, 1 May 2024 14:35:07 +0200 Subject: [PATCH] [field] Add Field.as_boundary() --- docs/Complex_Boundaries.ipynb | 4 ++-- phi/field/_field.py | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/docs/Complex_Boundaries.ipynb b/docs/Complex_Boundaries.ipynb index 707550914..f9508656e 100644 --- a/docs/Complex_Boundaries.ipynb +++ b/docs/Complex_Boundaries.ipynb @@ -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" @@ -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" diff --git a/phi/field/_field.py b/phi/field/_field.py index 83e8606e7..fbbb22310 100644 --- a/phi/field/_field.py +++ b/phi/field/_field.py @@ -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: """ @@ -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):