def test_ghost_zone_extrapolation(): ds = fake_random_ds(16) g = ds.index.grids[0] vec = g.get_vertex_centered_data(['x', 'y', 'z'], no_ghost=True) for i, ax in enumerate('xyz'): xc = g[ax] tf = lin.TrilinearFieldInterpolator( xc, (g.LeftEdge[0] + g.dds[0] / 2.0, g.RightEdge[0] - g.dds[0] / 2.0, g.LeftEdge[1] + g.dds[1] / 2.0, g.RightEdge[1] - g.dds[1] / 2.0, g.LeftEdge[2] + g.dds[2] / 2.0, g.RightEdge[2] - g.dds[2] / 2.0), ["x", "y", "z"], truncate=True) lx, ly, lz = np.mgrid[ g.LeftEdge[0]:g.RightEdge[0]:(g.ActiveDimensions[0] + 1) * 1j, g.LeftEdge[1]:g.RightEdge[1]:(g.ActiveDimensions[1] + 1) * 1j, g.LeftEdge[2]:g.RightEdge[2]:(g.ActiveDimensions[2] + 1) * 1j] xi = tf({'x': lx, 'y': ly, 'z': lz}) xz = np.zeros(g.ActiveDimensions + 1) ghost_zone_interpolate(1, xc, np.array([0.5, 0.5, 0.5], dtype="f8"), xz, np.array([0.0, 0.0, 0.0], dtype="f8")) ii = (lx, ly, lz)[i] assert_array_equal(ii, vec[ax]) assert_array_equal(ii, xi) assert_array_equal(ii, xz)
def get_vertex_centered_data( self, fields: List[Tuple[str, str]], smoothed: bool = True, no_ghost: bool = False, ): _old_api = isinstance(fields, (str, tuple)) if _old_api: message = ( "get_vertex_centered_data() requires list of fields, rather than " "a single field as an argument." ) warnings.warn(message, DeprecationWarning, stacklevel=2) fields = [fields] # type: ignore # Make sure the field list has only unique entries fields = list(set(fields)) new_fields = {} for field in fields: finfo = self.ds._get_field_info(field) new_fields[field] = self.ds.arr( np.zeros(self.ActiveDimensions + 1), finfo.output_units ) if no_ghost: for field in fields: # Ensure we have the native endianness in this array. Avoid making # a copy if possible. old_field = np.asarray(self[field], dtype="=f8") # We'll use the ghost zone routine, which will naturally # extrapolate here. input_left = np.array([0.5, 0.5, 0.5], dtype="float64") output_left = np.array([0.0, 0.0, 0.0], dtype="float64") # rf = 1 here ghost_zone_interpolate( 1, old_field, input_left, new_fields[field], output_left ) else: cg = self.retrieve_ghost_zones(1, fields, smoothed=smoothed) for field in fields: src = cg[field].in_units(new_fields[field].units).d dest = new_fields[field].d np.add(dest, src[1:, 1:, 1:], dest) np.add(dest, src[:-1, 1:, 1:], dest) np.add(dest, src[1:, :-1, 1:], dest) np.add(dest, src[1:, 1:, :-1], dest) np.add(dest, src[:-1, 1:, :-1], dest) np.add(dest, src[1:, :-1, :-1], dest) np.add(dest, src[:-1, :-1, 1:], dest) np.add(dest, src[:-1, :-1, :-1], dest) np.multiply(dest, 0.125, dest) if _old_api: return new_fields[fields[0]] return new_fields
def get_vertex_centered_data(self, fields, smoothed=True, no_ghost=False): _old_api = isinstance(fields, (string_types, tuple)) if _old_api: message = ( 'get_vertex_centered_data() requires list of fields, rather than ' 'a single field as an argument.' ) warnings.warn(message, DeprecationWarning, stacklevel=2) fields = [fields] # Make sure the field list has only unique entries fields = list(set(fields)) new_fields = {} for field in fields: new_fields[field] = np.zeros(self.ActiveDimensions + 1, dtype='float64') if no_ghost: for field in fields: # Ensure we have the native endianness in this array. Avoid making # a copy if possible. old_field = np.asarray(self[field], dtype="=f8") # We'll use the ghost zone routine, which will naturally # extrapolate here. input_left = np.array([0.5, 0.5, 0.5], dtype="float64") output_left = np.array([0.0, 0.0, 0.0], dtype="float64") # rf = 1 here ghost_zone_interpolate(1, old_field, input_left, new_fields[field], output_left) else: cg = self.retrieve_ghost_zones(1, fields, smoothed=smoothed) for field in fields: np.add(new_fields[field], cg[field][1: ,1: ,1: ], new_fields[field]) np.add(new_fields[field], cg[field][:-1,1: ,1: ], new_fields[field]) np.add(new_fields[field], cg[field][1: ,:-1,1: ], new_fields[field]) np.add(new_fields[field], cg[field][1: ,1: ,:-1], new_fields[field]) np.add(new_fields[field], cg[field][:-1,1: ,:-1], new_fields[field]) np.add(new_fields[field], cg[field][1: ,:-1,:-1], new_fields[field]) np.add(new_fields[field], cg[field][:-1,:-1,1: ], new_fields[field]) np.add(new_fields[field], cg[field][:-1,:-1,:-1], new_fields[field]) np.multiply(new_fields[field], 0.125, new_fields[field]) if _old_api: return new_fields[fields[0]] return new_fields