示例#1
0
 def test_staggered_interpolation(self):
     # 2x2 cells
     data_x = math.zeros([1, 2, 3, 1])
     data_x[0, :, :, 0] = [[1, 2, 3], [4, 5, 6]]
     data_y = math.zeros([1, 3, 2, 1])
     data_y[0, :, :, 0] = [[-1, -2], [-3, -4], [-5, -6]]
     v = StaggeredGrid([data_y, data_x])
     centered = v.at_centers()
     np.testing.assert_equal(centered.data.shape, [1, 2, 2, 2])
示例#2
0
 def test_staggered_construction(self):
     # pylint: disable-msg = unsubscriptable-object
     tensor = math.zeros([1, 5, 5, 2])
     staggered = StaggeredGrid(tensor, name='')
     assert len(staggered.data) == 2
     assert isinstance(staggered.data[0], CenteredGrid)
     assert staggered.data[0].component_count == 1
     np.testing.assert_equal(staggered.data[0].box.lower, [-0.5, 0])
     staggered2 = StaggeredGrid(unstack_staggered_tensor(tensor), name='')
     struct.print_differences(staggered, staggered2)
     self.assertEqual(staggered, staggered2)
     staggered3 = StaggeredGrid([staggered.data[0], staggered2.data[1]], name='')
     self.assertEqual(staggered3, staggered)
示例#3
0
 def staggered_grid(self,
                    data,
                    dtype=None,
                    name=None,
                    batch_size=None,
                    extrapolation=None):
     if extrapolation is None:
         extrapolation = Material.extrapolation_mode(self.boundaries)
     if callable(data):  # data is an initializer
         shape = self.staggered_shape(batch_size=batch_size,
                                      name=name,
                                      extrapolation=extrapolation,
                                      age=())
         try:
             data = data(shape, dtype=dtype)
         except TypeError:
             data = data(shape)
         if data.age == ():
             data._age = 0.0
             for field in data.data:
                 field._age = 0.0
     from phi.physics.field import Field
     if isinstance(data, Field):
         from phi.physics.field import StaggeredGrid
         if isinstance(data, StaggeredGrid) and np.all(
                 data.resolution ==
                 self.resolution) and data.box == self.box:
             grid = data
         else:
             grid = data.at(
                 StaggeredGrid.sample(
                     0, self,
                     batch_size=batch_size))  # ToDo this is not ideal
     elif isinstance(data, (int, float)):
         shape = self.staggered_shape(batch_size=batch_size,
                                      name=name,
                                      extrapolation=extrapolation)
         from phi.physics.field import DIVERGENCE_FREE
         grid = (math.zeros(shape, dtype=dtype) +
                 data).copied_with(flags=[DIVERGENCE_FREE])
     else:
         from .field import StaggeredGrid
         grid = StaggeredGrid(data,
                              self.box,
                              name,
                              batch_size=None,
                              extrapolation=extrapolation)
     return grid
示例#4
0
 def staggered_shape(self,
                     batch_size=1,
                     name=None,
                     extrapolation=None,
                     age=0.0):
     grids = []
     for axis in range(self.rank):
         shape = _extend1(tensor_shape(batch_size, self.resolution, 1),
                          axis)
         from phi.physics.field.staggered_grid import staggered_component_box
         box = staggered_component_box(self.resolution, axis, self.box)
         from phi.physics.field import CenteredGrid
         grid = CenteredGrid(shape,
                             box,
                             age=age,
                             extrapolation=extrapolation,
                             name=None,
                             batch_size=batch_size,
                             flags=(),
                             content_type=struct.Struct.shape)
         grids.append(grid)
     from phi.physics.field import StaggeredGrid
     return StaggeredGrid(grids,
                          age=age,
                          box=self.box,
                          name=name,
                          batch_size=batch_size,
                          extrapolation=extrapolation,
                          flags=(),
                          content_type=struct.Struct.shape)
示例#5
0
def staggered_curl_2d(grid, pad_width=(1, 2)):
    assert isinstance(grid, CenteredGrid)
    kernel = math.zeros((3, 3, 1, 2))
    kernel[1, :, 0, 0] = [0, 1, -1]  # y-component: - dz/dx
    kernel[:, 1, 0, 1] = [0, -1, 1]  # x-component: dz/dy
    scalar_potential = grid.padded([pad_width, pad_width]).data
    vector_field = math.conv(scalar_potential, kernel, padding='valid')
    return StaggeredGrid(vector_field, box=grid.box)
示例#6
0
    def test_bounds(self):
        tensor = math.zeros([1, 5, 5, 2])
        f = StaggeredGrid(tensor)
        bounds = data_bounds(f)
        self.assertIsInstance(bounds, AABox)
        np.testing.assert_equal(bounds.lower, 0)
        np.testing.assert_equal(bounds.upper, [4, 4])

        a = CenteredGrid(np.zeros([1, 4, 4, 1]))
        np.testing.assert_equal(a.box.size, [4, 4])

        a = CenteredGrid(np.zeros([1, 4, 4, 1]), 1)
        np.testing.assert_equal(a.box.size, 1)