示例#1
0
 def test_direct_initializers(self):
     np.testing.assert_equal(math.zeros([1, 16]), np.zeros([1, 16]))
     self.assertEqual(math.zeros([1, 16]).dtype, np.float32)
     np.testing.assert_equal(math.ones([1, 16, 1]), np.ones([1, 16, 1]))
     np.testing.assert_equal(math.zeros_like(math.ones([1, 16, 1])), np.zeros([1, 16, 1]))
     np.testing.assert_equal(math.randn([1, 4]).shape, [1, 4])
     self.assertEqual(math.randn([1, 4]).dtype, np.float32)
示例#2
0
    def test_jit_compile(self):
        @math.jit_compile
        def scalar_mul(x, fac=1):
            return x * fac

        for backend in BACKENDS:
            with backend:
                x = math.ones(spatial(x=4))
                trace_count_0 = len(scalar_mul.traces)
                math.assert_close(scalar_mul(x, fac=1), 1, msg=backend)
                if backend.supports(Backend.jit_compile):
                    self.assertEqual(len(scalar_mul.traces), trace_count_0 + 1)
                math.assert_close(scalar_mul(x, fac=1), 1, msg=backend)
                if backend.supports(Backend.jit_compile):
                    self.assertEqual(len(scalar_mul.traces), trace_count_0 + 1)
                math.assert_close(scalar_mul(x, fac=2), 2, msg=backend)
                if backend.supports(Backend.jit_compile):
                    self.assertEqual(len(scalar_mul.traces), trace_count_0 + 2)
                math.assert_close(scalar_mul(math.zeros(spatial(x=4)), fac=2),
                                  0,
                                  msg=backend)
                if backend.supports(Backend.jit_compile):
                    self.assertEqual(len(scalar_mul.traces), trace_count_0 + 2)
                math.assert_close(scalar_mul(math.zeros(spatial(y=4)), fac=2),
                                  0,
                                  msg=backend)
                if backend.supports(Backend.jit_compile):
                    self.assertEqual(len(scalar_mul.traces), trace_count_0 + 3)
示例#3
0
 def test_scatter_update_1d_batched(self):
     for backend in BACKENDS:
         with backend:
             # Only base batched
             base = math.zeros(spatial(x=4)) + math.tensor([0, 1])
             indices = math.wrap([1, 2], instance('points'))
             values = math.wrap([11, 12], instance('points'))
             updated = math.scatter(base, indices, values, mode='update', outside_handling='undefined')
             math.assert_close(updated, math.tensor([(0, 1), (11, 11), (12, 12), (0, 1)], spatial('x'), channel('vector')), msg=backend.name)
             # Only values batched
             base = math.ones(spatial(x=4))
             indices = math.wrap([1, 2], instance('points'))
             values = math.wrap([[11, 12], [-11, -12]], batch('batch'), instance('points'))
             updated = math.scatter(base, indices, values, mode='update', outside_handling='undefined')
             math.assert_close(updated, math.tensor([[1, 11, 12, 1], [1, -11, -12, 1]], batch('batch'), spatial('x')), msg=backend.name)
             # Only indices batched
             base = math.ones(spatial(x=4))
             indices = math.wrap([[0, 1], [1, 2]], batch('batch'), instance('points'))
             values = math.wrap([11, 12], instance('points'))
             updated = math.scatter(base, indices, values, mode='update', outside_handling='undefined')
             math.assert_close(updated, math.tensor([[11, 12, 1, 1], [1, 11, 12, 1]], batch('batch'), spatial('x')), msg=backend.name)
             # Everything batched
             base = math.zeros(spatial(x=4)) + math.tensor([0, 1], batch('batch'))
             indices = math.wrap([[0, 1], [1, 2]], batch('batch'), instance('points'))
             values = math.wrap([[11, 12], [-11, -12]], batch('batch'), instance('points'))
             updated = math.scatter(base, indices, values, mode='update', outside_handling='undefined')
             math.assert_close(updated, math.tensor([[11, 12, 0, 0], [1, -11, -12, 1]], batch('batch'), spatial('x')), msg=backend.name)
示例#4
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])
示例#5
0
 def test_assert_close(self):
     a = spatial(a=10)
     math.assert_close(math.zeros(a), math.zeros(a), math.zeros(a), rel_tolerance=0, abs_tolerance=0)
     assert_not_close(math.zeros(a), math.ones(a), rel_tolerance=0, abs_tolerance=0)
     for scale in (1, 0.1, 10):
         math.assert_close(math.zeros(a), math.ones(a) * scale, rel_tolerance=0, abs_tolerance=scale * 1.001)
         math.assert_close(math.zeros(a), math.ones(a) * scale, rel_tolerance=1, abs_tolerance=0)
         assert_not_close(math.zeros(a), math.ones(a) * scale, rel_tolerance=0.9, abs_tolerance=0)
         assert_not_close(math.zeros(a), math.ones(a) * scale, rel_tolerance=0, abs_tolerance=0.9 * scale)
     with math.precision(64):
         assert_not_close(math.zeros(a), math.ones(a) * 1e-100, rel_tolerance=0, abs_tolerance=0)
         math.assert_close(math.zeros(a), math.ones(a) * 1e-100, rel_tolerance=0, abs_tolerance=1e-15)
示例#6
0
 def test_inner_interpolation(self):
     data = math.zeros([1, 2, 3, 1])
     data[0, :, :, 0] = [[1, 2, 3], [4, 5, 6]]
     f = CenteredGrid(data, box[0:2, 0:3])
     g = CenteredGrid(math.zeros([1, 2, 2, 1]), box[0:2, 0.5:2.5])
     # Resample optimized
     resampled = f.at(g)
     self.assertTrue(resampled.compatible(g))
     np.testing.assert_equal(resampled.data[0, ..., 0], [[1.5, 2.5], [4.5, 5.5]])
     # Resample unoptimized
     resampled2 = Field.at(f, g)
     self.assertTrue(resampled2.compatible(g))
     np.testing.assert_equal(resampled2.data[0, ..., 0], [[1.5, 2.5], [4.5, 5.5]])
示例#7
0
 def test_tensor_from_tensor(self):
     ref = math.batch_stack([math.zeros(x=5), math.zeros(x=4)], 'stack')
     for backend in (NUMPY_BACKEND, TORCH_BACKEND, TF_BACKEND):
         with backend:
             tens = math.tensor(ref, convert=False)
             self.assertEqual(math.NUMPY_BACKEND, math.choose_backend(tens))
             self.assertEqual(2, tens.shape.get_size('stack'))
             self.assertEqual(('stack', 'x'), tens.shape.names)
             tens = math.tensor(ref)
             self.assertEqual(backend, math.choose_backend(tens))
             self.assertEqual(backend, math.choose_backend(tens.stack[0]))
             self.assertEqual(backend, math.choose_backend(tens.stack[1]))
             tens = math.tensor(ref, names=('n1', 'n2'))
             self.assertEqual(backend, math.choose_backend(tens))
示例#8
0
 def test_tensor_from_tensor(self):
     ref = math.stack([math.zeros(spatial(x=5)),
                       math.zeros(spatial(x=4))], batch('stack'))
     for backend in BACKENDS:
         with backend:
             tens = math.tensor(ref, convert=False)
             self.assertEqual(math.NUMPY, math.choose_backend(tens))
             self.assertEqual(2, tens.shape.get_size('stack'))
             self.assertEqual(('stack', 'x'), tens.shape.names)
             tens = math.tensor(ref)
             self.assertEqual(backend, math.choose_backend(tens))
             self.assertEqual(backend, math.choose_backend(tens.stack[0]))
             self.assertEqual(backend, math.choose_backend(tens.stack[1]))
             tens = math.tensor(ref, batch('n1', 'n2'))
             self.assertEqual(backend, math.choose_backend(tens))
示例#9
0
 def test_cos(self):
     for backend in BACKENDS:
         with backend:
             math.assert_close(math.cos(math.zeros(spatial(x=4))), 1, abs_tolerance=1e-6, msg=backend.name)
             math.assert_close(math.cos(math.tensor(math.PI / 2)), 0, abs_tolerance=1e-6, msg=backend.name)
             math.assert_close(math.cos(math.tensor(math.PI)), -1, abs_tolerance=1e-6, msg=backend.name)
             math.assert_close(math.cos(math.tensor(math.PI * 3 / 2)), 0, abs_tolerance=1e-6, msg=backend.name)
示例#10
0
 def test_struct_initializers(self):
     obj = ([4], CenteredGrid([1, 4, 1], box[0:1], content_type=struct.shape), ([9], [8, 2]))
     z = math.zeros(obj)
     self.assertIsInstance(z, tuple)
     np.testing.assert_equal(z[0], np.zeros([4]))
     z2 = math.zeros_like(z)
     np.testing.assert_equal(math.shape(z)[0], math.shape(z2)[0])
示例#11
0
 def test_zeros_nonuniform(self):
     nonuniform = shape_stack('stack', BATCH_DIM, shape(time=1, x=3, y=3),
                              shape(x=3, y=4), shape())
     self.assertEqual(math.zeros(nonuniform).shape, nonuniform)
     self.assertEqual(math.ones(nonuniform).shape, nonuniform)
     self.assertEqual(math.random_normal(nonuniform).shape, nonuniform)
     self.assertEqual(math.random_uniform(nonuniform).shape, nonuniform)
示例#12
0
 def test_flip_item_names(self):
     t = math.zeros(spatial(x=4, y=3), channel(vector='x,y'))
     self.assertEqual(('x', 'y'), t.vector.item_names)
     t_ = t.vector.flip()
     self.assertEqual(('y', 'x'), t_.vector.item_names)
     t_ = t.vector[::-1]
     self.assertEqual(('y', 'x'), t_.vector.item_names)
示例#13
0
 def test_collapsed(self):
     scalar = math.zeros(x=4, y=3)
     math.assert_close(scalar, 0)
     self.assertEqual('(x=4, y=3)', repr(scalar.shape))
     self.assertEqual('(x=4)', repr(scalar.y[0].shape))
     self.assertEqual('()', repr(scalar.y[0].x[0].shape))
     self.assertEqual(3, len(scalar.y.unstack()))
示例#14
0
 def test_collapsed(self):
     scalar = math.zeros(spatial(x=4, y=3))
     math.assert_close(scalar, 0)
     self.assertEqual((4, 3), scalar.shape.sizes)
     self.assertEqual(4, scalar.y[0].shape.size)
     self.assertEqual(0, scalar.y[0].x[0].shape.rank)
     self.assertEqual(3, len(scalar.y.unstack()))
示例#15
0
    def points(self,
               points: Tensor or Number or tuple or list,
               values: Tensor or Number = None,
               radius: Tensor or float or int or None = None,
               extrapolation: math.Extrapolation = math.extrapolation.ZERO,
               color: str or Tensor or tuple or list or None = None) -> PointCloud:
        """
        Create a `phi.field.PointCloud` from the given `points`.
        The created field has no channel dimensions and all points carry the value `1`.

        Args:
            points: point locations in physical units
            values: (optional) values of the particles, defaults to 1.
            radius: (optional) size of the particles
            extrapolation: (optional) extrapolation to use, defaults to extrapolation.ZERO
            color: (optional) color used when plotting the points

        Returns:
            `phi.field.PointCloud` object
        """
        extrapolation = extrapolation if isinstance(extrapolation, math.Extrapolation) else self.boundaries[extrapolation]
        if radius is None:
            radius = math.mean(self.bounds.size) * 0.005
        # --- Parse points: tuple / list ---
        if isinstance(points, (tuple, list)):
            if len(points) == 0:  # no points
                points = math.zeros(instance(points=0), channel(vector=1))
            elif isinstance(points[0], Number):  # single point
                points = math.tensor([points], instance('points'), channel('vector'))
            else:
                points = math.tensor(points, instance('points'), channel('vector'))
        elements = Sphere(points, radius)
        if values is None:
            values = math.tensor(1.)
        return PointCloud(elements, values, extrapolation, add_overlapping=False, bounds=self.bounds, color=color)
示例#16
0
 def sample(value, domain, batch_size=None, name=None):
     assert isinstance(domain, Domain)
     if isinstance(value, Field):
         assert_same_rank(
             value.rank, domain.rank,
             'rank of value (%s) does not match domain (%s)' %
             (value.rank, domain.rank))
         if isinstance(value,
                       CenteredGrid) and value.box == domain.box and np.all(
                           value.resolution == domain.resolution):
             data = value.data
         else:
             point_field = CenteredGrid.getpoints(domain.box,
                                                  domain.resolution)
             point_field._batch_size = batch_size
             data = value.at(point_field).data
     else:  # value is constant
         if callable(value):
             x = CenteredGrid.getpoints(
                 domain.box, domain.resolution).copied_with(
                     extrapolation=Material.extrapolation_mode(
                         domain.boundaries),
                     name=name)
             value = value(x)
             return value
         components = math.staticshape(
             value)[-1] if math.ndims(value) > 0 else 1
         data = math.add(
             math.zeros((batch_size, ) + tuple(domain.resolution) +
                        (components, )), value)
     return CenteredGrid(data,
                         box=domain.box,
                         extrapolation=Material.extrapolation_mode(
                             domain.boundaries),
                         name=name)
示例#17
0
 def sample_at(self, points, collapse_dimensions=True):
     if len(self.geometries) == 0:
         return _expand_axes(math.zeros([1,1]), points, collapse_dimensions=collapse_dimensions)
     if len(self.geometries) == 1:
         result = self.geometries[0].value_at(points)
     else:
         result = math.max([geometry.value_at(points) for geometry in self.geometries], axis=0)
     return math.mul(result, self.data)
示例#18
0
 def test_pad_collapsed(self):
     a = math.zeros(b=2, x=10, y=10, batch=10)
     p = math.pad(a, {'x': (1, 2)}, ZERO)
     self.assertIsInstance(p, CollapsedTensor)
     self.assertEqual((10, 2, 13, 10), p.shape.sizes)
     p = math.pad(a, {'x': (1, 2)}, PERIODIC)
     self.assertIsInstance(p, CollapsedTensor)
     self.assertEqual((10, 2, 13, 10), p.shape.sizes)
示例#19
0
 def test_pad(self):
     test_in_func_out = [
         (math.zeros(spatial(x=3, y=4, z=5,
                             a=1)), lambda tensor: ConstantExtrapolation(0).
          pad(tensor, dict(x=[1, 1], y=[1, 0], z=[0, 1], a=[0, 0])),
          math.zeros(spatial(x=5, y=5, z=6, a=1))),
         (math.ones(spatial(x=3, y=4, z=5,
                            a=1)), lambda tensor: ConstantExtrapolation(1).
          pad(tensor, dict(x=[1, 1], y=[1, 0], z=[0, 1], a=[0, 0])),
          math.ones(spatial(x=5, y=5, z=6, a=1))),
         (-math.ones(spatial(x=3, y=4, z=5, a=1)),
          lambda tensor: ConstantExtrapolation(-1).pad(
              tensor, dict(x=[1, 1], y=[1, 0], z=[0, 1], a=[0, 0])),
          -math.ones(spatial(x=5, y=5, z=6, a=1))),
     ]
     for val_in, func, val_out in test_in_func_out:
         math.assert_close(val_out, func(val_in))
示例#20
0
 def test_zeros_nonuniform(self):
     nonuniform = shape_stack(batch('stack'),
                              batch(time=1) & spatial(x=3, y=3),
                              spatial(x=3, y=4), channel())
     self.assertEqual(math.zeros(nonuniform).shape, nonuniform)
     self.assertEqual(math.ones(nonuniform).shape, nonuniform)
     self.assertEqual(math.random_normal(nonuniform).shape, nonuniform)
     self.assertEqual(math.random_uniform(nonuniform).shape, nonuniform)
示例#21
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)
示例#22
0
 def test_staggered_format_conversion(self):
     tensor = math.zeros([1, 5, 5, 2])
     tensor[:, 0, 0, :] = 1
     components = unstack_staggered_tensor(tensor)
     self.assertEqual(len(components), 2)
     np.testing.assert_equal(components[0].shape, [1, 5, 4, 1])
     np.testing.assert_equal(components[1].shape, [1, 4, 5, 1])
     tensor2 = stack_staggered_components(components)
     np.testing.assert_equal(tensor, tensor2)
示例#23
0
 def test_sparse_matrix(self):
     for backend in BACKENDS:
         with backend:
             for f in ['csr', 'csc', 'coo']:
                 matrix = math.jit_compile_linear(
                     math.laplace).sparse_matrix(math.zeros(spatial(x=5)),
                                                 format=f)
                 self.assertEqual(f, matrix.indexing_type)
                 self.assertEqual((5, 5), matrix.shape)
示例#24
0
 def test_struct_initializers(self):
     bounds = box[0:1]  # outside unsafe
     with struct.unsafe():
         obj = ([4], CenteredGrid([1, 4, 1], bounds), ([9], [8, 2]))
     z = math.zeros(obj)
     self.assertIsInstance(z, tuple)
     np.testing.assert_equal(z[0], np.zeros([4]))
     z2 = math.zeros_like(z)
     np.testing.assert_equal(math.shape(z)[0], math.shape(z2)[0])
示例#25
0
 def test_direct_placeholders(self):
     tensorflow.reset_default_graph()
     p = placeholder([4])
     self.assertIsInstance(p, tensorflow.Tensor)
     numpy.testing.assert_equal(p.shape.as_list(), [4])
     self.assertEqual(p.name, 'Placeholder:0')
     v = variable(math.zeros([2, 2]))
     numpy.testing.assert_equal(v.shape.as_list(), [2, 2])
     self.assertIsInstance(v, tensorflow.Variable)
     self.assertEqual(v.name, 'Variable:0')
示例#26
0
 def test_tensor_from_tuple_of_tensor_like(self):
     native = ((1, 2, 3), math.zeros(vector=3))
     for backend in (NUMPY_BACKEND, TORCH_BACKEND, TF_BACKEND):
         with backend:
             tens = math.tensor(native, names=['stack', 'vector'], convert=False)
             self.assertEqual(math.NUMPY_BACKEND, math.choose_backend(tens))
             self.assertEqual(shape(stack=2, vector=3), tens.shape)
             tens = math.tensor(native, names=['stack', 'vector'])
             self.assertEqual(backend, math.choose_backend(tens))
             self.assertEqual(shape(stack=2, vector=3), tens.shape)
示例#27
0
 def test_pad(self):
     test_in_func_out = [
         (math.zeros(x=3, y=4, z=5, a=1),
          lambda tensor: ConstantExtrapolation(0).pad(tensor, dict(x=[1, 1], y=[1, 0], z=[0, 1], a=[0, 0])),
          math.zeros(x=5, y=5, z=6, a=1)),
         (math.ones(x=3, y=4, z=5, a=1),
          lambda tensor: ConstantExtrapolation(1).pad(tensor, dict(x=[1, 1], y=[1, 0], z=[0, 1], a=[0, 0])),
          math.ones(x=5, y=5, z=6, a=1)),
         (-math.ones(x=3, y=4, z=5, a=1),
          lambda tensor: ConstantExtrapolation(-1).pad(tensor, dict(x=[1, 1], y=[1, 0], z=[0, 1], a=[0, 0])),
          - math.ones(x=5, y=5, z=6, a=1)),
     ]
     for val_in, func, val_out in test_in_func_out:
         try:
             math.assert_close(val_out, func(val_in))
             # TypeError('__bool__ should return bool, returned NotImplementedType')
             # self.assertEqual(val_out, func(val_in))
         except Exception as e:
             raise BaseException(AssertionError(e, val_in, func, val_out))
示例#28
0
文件: _grid.py 项目: andresgm/PhiFlow
 def __init__(self, elements: Geometry, values: Tensor, extrapolation: float
              or math.Extrapolation, resolution: Shape, bounds: Box):
     if bounds.size.vector.item_names is None:
         with NUMPY:
             bounds = bounds.shifted(
                 math.zeros(channel(vector=spatial(values).names)))
     SampledField.__init__(self, elements, values, extrapolation, bounds)
     assert values.shape.spatial_rank == elements.spatial_rank, f"Spatial dimensions of values ({values.shape}) do not match elements {elements}"
     assert values.shape.spatial_rank == bounds.spatial_rank, f"Spatial dimensions of values ({values.shape}) do not match elements {elements}"
     assert values.shape.instance_rank == 0, f"Instance dimensions not supported for grids. Got values with shape {values.shape}"
     self._resolution = resolution
示例#29
0
 def test_collapsed_op2(self):
     # Collapsed + Collapsed
     a = math.zeros(vector=4)
     b = math.ones(batch=3)
     c = a + b
     self.assertIsInstance(c, CollapsedTensor)
     self.assertEqual(c.shape.volume, 12)
     self.assertEqual(c._inner.shape.volume, 1)
     # Collapsed + Native
     n = math.ones(vector=3) + (0, 1, 2)
     math.assert_close(n, (1, 2, 3))
示例#30
0
 def test_tensor_from_tuple_of_tensor_like(self):
     native = ([1, 2, 3], math.zeros(channel(vector=3)))
     for backend in BACKENDS:
         with backend:
             tens = wrap(native, batch(stack=2), channel(vector=3))
             self.assertEqual(math.NUMPY, math.choose_backend(tens))
             self.assertEqual(
                 batch(stack=2) & channel(vector=3), tens.shape)
             tens = tensor(native, batch(stack=2), channel(vector=3))
             self.assertEqual(backend, math.choose_backend(tens))
             self.assertEqual(
                 batch(stack=2) & channel(vector=3), tens.shape)