MCPcopy
hub / github.com/tum-pbs/PhiFlow / sample

Function sample

phi/field/_resample.py:75–174  ·  view source on GitHub ↗

Computes the field value inside the volume of the (batched) `geometry`. The field value may be determined by integrating over the volume, sampling the central value or any other way. The batch dimensions of `geometry` are matched with this field. The `geometry` must not share any

(field: Union[Field, Geometry, FieldInitializer, Callable],
           geometry: Geometry or Field or Tensor,
           at: str = 'center',
           boundary: Union[Extrapolation, Tensor, Number] = None,
           dot_face_normal: Optional[Geometry] = None,
           **kwargs)

Source from the content-addressed store, hash-verified

73
74
75def sample(field: Union[Field, Geometry, FieldInitializer, Callable],
76 geometry: Geometry or Field or Tensor,
77 at: str = 'center',
78 boundary: Union[Extrapolation, Tensor, Number] = None,
79 dot_face_normal: Optional[Geometry] = None,
80 **kwargs) -> math.Tensor:
81 """
82 Computes the field value inside the volume of the (batched) `geometry`.
83
84 The field value may be determined by integrating over the volume, sampling the central value or any other way.
85
86 The batch dimensions of `geometry` are matched with this field.
87 The `geometry` must not share any channel dimensions with this field.
88 Spatial dimensions of `geometry` can be used to sample a grid of geometries.
89
90 See Also:
91 `Field.at()`, [Resampling overview](https://tum-pbs.github.io/PhiFlow/Fields.html#resampling-fields).
92
93 Args:
94 field: Source `Field` to sample.
95 geometry: Single or batched `phi.geom.Geometry` or `Field` or location `Tensor`.
96 When passing a `Field`, its `elements` are used as sample points.
97 When passing a vector-valued `Tensor`, a `Point` geometry will be created.
98 at: One of 'center', 'face', 'vertex'
99 boundary: Target extrapolation.
100 dot_face_normal: If not `None` and , `field` is a vector field and `at=='face'`, the dot product between sampled field vectors and the face normals is returned instead.
101 **kwargs: Sampling arguments, e.g. to specify the numerical scheme.
102 By default, linear interpolation is used.
103 Grids also support 6th order implicit sampling at mid-points.
104
105 Returns:
106 Sampled values as a `phi.math.Tensor`
107 """
108 # --- Process args ---
109 assert at in ['center', 'face', 'vertex']
110 if at == 'face':
111 assert boundary is not None, "boundaries must be given when sampling at faces"
112 geometry = _get_geometry(geometry)
113 boundary = as_boundary(boundary, geometry) if boundary is not None else None
114 if dot_face_normal is True:
115 dot_face_normal = geometry
116 if isinstance(field, Geometry):
117 from ._field_math import mask
118 field = mask(field)
119 if isinstance(field, FieldInitializer):
120 values = field._sample(geometry, at, boundary, **kwargs)
121 if dot_face_normal is not None and at == 'face' and channel(values):
122 if _are_axis_aligned(dot_face_normal.face_normals):
123 values = math.stack([values[{'vector': i, '~vector': i}] for i in range(geometry.spatial_rank)], dual(**geometry.shape['vector'].untyped_dict))
124 else:
125 raise NotImplementedError
126 field = Field(geometry, values, boundary)
127 if callable(field):
128 values = sample_function(field, geometry, at, boundary)
129 field = Field(geometry, values, boundary)
130 # --- Resample ---
131 assert isinstance(field, Field), f"field must be a Field, Geometry or initializer but got {type(field)}"
132 if at == 'center':

Callers 15

eulerFunction · 0.90
rk4Function · 0.90
finite_rk4Function · 0.90
mac_cormackFunction · 0.90
__call__Method · 0.90
at_centersMethod · 0.90
at_facesMethod · 0.90
sampleMethod · 0.90
with_valuesMethod · 0.90
_op2Method · 0.90
CenteredGridFunction · 0.90
StaggeredGridFunction · 0.90

Calls 15

with_boundaryMethod · 0.95
sample_functionFunction · 0.90
_get_geometryFunction · 0.85
as_boundaryFunction · 0.85
maskFunction · 0.85
_are_axis_alignedFunction · 0.85
FieldClass · 0.85
sample_grid_at_centersFunction · 0.85
sample_staggered_gridFunction · 0.85
sample_meshFunction · 0.85
scatter_to_centersFunction · 0.85
slice_off_constant_facesFunction · 0.85

Tested by

no test coverage detected