def __dataop__(self, other, linear_if_scalar, data_operator): if isinstance(other, StaggeredGrid): assert self.compatible( other), 'Fields are not compatible: %s and %s' % (self, other) data = [ data_operator(c1, c2) for c1, c2 in zip(self.data, other.data) ] flags = propagate_flags_operation(self.flags + other.flags, False, self.rank, self.component_count) elif math.ndims(other) > 0 and math.staticshape(other)[-1] > 1: other_components = math.unstack(math.as_tensor(other), axis=-1, keepdims=True) data = [ data_operator(c1, c2) for c1, c2 in zip(self.data, other_components) ] flags = propagate_flags_operation(self.flags, False, self.rank, self.component_count) else: data = [data_operator(c1, other) for c1 in self.data] flags = propagate_flags_operation(self.flags, linear_if_scalar, self.rank, self.component_count) return self.copied_with(data=np.array(data, dtype=np.object), flags=flags)
def resolution(self): if self.content_type in (struct.VALID, struct.INVALID): return math.as_tensor(math.staticshape(self.data)[1:-1]) elif self.content_type in (struct.shape, struct.staticshape): return self.data[1:-1] else: raise AssertionError( 'Cannot compute resolution of invalid CenteredGrid (content type = %s)' % self.content_type)
def buoyancy(density, gravity, buoyancy_factor): """ Computes the buoyancy force proportional to the density. :param density: CenteredGrid :param gravity: vector or float :param buoyancy_factor: float :return: StaggeredGrid for the domain of the density """ if isinstance(gravity, (int, float)): gravity = math.to_float(math.as_tensor([gravity] + ([0] * (density.rank - 1)))) result = StaggeredGrid.from_scalar(density, -gravity * buoyancy_factor) return result
def buoyancy(density, gravity, buoyancy_factor): """ Computes the buoyancy force proportional to the density. :param density: CenteredGrid :param gravity: vector or float :param buoyancy_factor: float :return: StaggeredGrid for the domain of the density """ warnings.warn( 'buoyancy() is deprecated. Use (density * -gravity * buoyancy_factor).at(target_grid) instead.', DeprecationWarning) if isinstance(gravity, (int, float)): gravity = math.to_float( math.as_tensor([gravity] + ([0] * (density.rank - 1)))) result = StaggeredGrid.from_scalar(density, -gravity * buoyancy_factor) return result
def resolution(self): return math.as_tensor(math.staticshape(self.data)[1:-1])
def upper(self, upper): return math.to_float(math.as_tensor(upper))
def lower(self, lower): return math.to_float(math.as_tensor(lower))
def center(self, center): return math.as_tensor(center)
def radius(self, radius): return math.as_tensor(radius)