Пример #1
0
 def poisson_gradient(_op, grad):
     return poisson_solve(CenteredGrid.sample(
         grad, poisson_domain.domain),
                          poisson_domain,
                          solver,
                          None,
                          gradient=gradient)[0].data
Пример #2
0
 def centered_grid(self,
                   data,
                   components=1,
                   dtype=None,
                   name=None,
                   batch_size=None,
                   extrapolation=None):
     warnings.warn(
         "Domain.centered_shape and Domain.centered_grid are deprecated. Use CenteredGrid.sample() instead.",
         DeprecationWarning)
     from phi.physics.field import CenteredGrid
     if callable(data):  # data is an initializer
         shape = self.centered_shape(components,
                                     batch_size=batch_size,
                                     name=name,
                                     extrapolation=extrapolation,
                                     age=())
         try:
             grid = data(shape, dtype=dtype)
         except TypeError:
             grid = data(shape)
         if grid.age == ():
             grid._age = 0.0
     else:
         grid = CenteredGrid.sample(data, self, batch_size=batch_size)
     assert grid.component_count == components, "Field has %d components but %d are required for '%s'" % (
         grid.component_count, components, name)
     if dtype is not None and math.dtype(grid.data) != dtype:
         grid = grid.copied_with(data=math.cast(grid.data, dtype))
     if name is not None:
         grid = grid.copied_with(name=name, tags=(name, ) + grid.tags)
     if extrapolation is not None:
         grid = grid.copied_with(extrapolation=extrapolation)
     return grid
Пример #3
0
 def test_subtraction_centered_grid(self):
     """subtract one field from another"""
     shape = [32, 27]
     for boundary in [CLOSED, PERIODIC, OPEN]:
         domain = Domain(shape, boundaries=(boundary, boundary))
         centered_grid = CenteredGrid.sample(Noise(), domain)
         result_array = (centered_grid - centered_grid).data
         np.testing.assert_array_equal(result_array, 0)
Пример #4
0
 def test_addition_centered_grid(self):
     """add one field to another"""
     shape = [32, 27]
     for boundary in [CLOSED, PERIODIC, OPEN]:
         domain = Domain(shape, boundaries=(boundary, boundary))
         centered_grid = CenteredGrid.sample(1, domain)
         result_array = (centered_grid + centered_grid).data
         np.testing.assert_array_equal(result_array, 2)
Пример #5
0
 def test_division_centered_grid(self):
     """divide one field by another"""
     shape = [32, 27]
     for boundary in [CLOSED, PERIODIC, OPEN]:
         domain = Domain(shape, boundaries=(boundary, boundary))
         centered_grid = CenteredGrid.sample(2, domain)
         result_array = (centered_grid / centered_grid.copied_with(
             data=4 * np.ones([1] + shape + [1]))).data
         np.testing.assert_array_equal(result_array, 1. / 2)
Пример #6
0
 def test_multiplication_centered_grid(self):
     """multiply one field with another"""
     shape = [32, 27]
     for boundary in [CLOSED, PERIODIC, OPEN]:
         domain = Domain(shape, boundaries=(boundary, boundary))
         centered_grid = CenteredGrid.sample(1, domain)
         result_array = (centered_grid * centered_grid.copied_with(
             data=2 * np.ones([1] + shape + [1]))).data
         np.testing.assert_array_equal(result_array, 2)
Пример #7
0
 def test_reconst(self, set_accuracy=1e-5, shape=[40, 40], first_order_tolerance=3, second_order_tolerance=40,
                  boundary_list=[PERIODIC, OPEN, CLOSED]):
     for boundary in boundary_list:
         domain = Domain(shape, boundaries=(boundary, boundary))
         solver_list = [
             ('SparseCG', lambda field: poisson_solve(field, domain, SparseCG(accuracy=set_accuracy)), lambda field: field.laplace()),
             ('GeometricCG', lambda field: poisson_solve(field, domain, GeometricCG(accuracy=set_accuracy)), lambda field: field.laplace()),
             #('SparseSciPy', lambda field: poisson_solve(field, domain, SparseSciPy()), lambda field: field.laplace()),
             # ('Fourier', lambda field: poisson_solve(field, domain, Fourier()))]  # TODO: poisson_solve() causes resolution to be empty
             ('FFT', math.fourier_poisson, math.fourier_laplace)]
         in_data = CenteredGrid.sample(Noise(), domain)
         sloped_data = (np.array([np.arange(shape[1]) for _ in range(shape[0])]).reshape([1] + shape + [1]) / 10 + 1)
         in_data = in_data.copied_with(data=sloped_data)
         for name, solver, laplace in solver_list:
             print('Testing {} boundary with {} solver... '.format(boundary, name)),
             _test_reconstruction_first_order(in_data, solver, laplace, set_accuracy, name, first_order_tolerance=first_order_tolerance)
             _test_reconstruction_second_order(in_data, solver, laplace, set_accuracy, name, second_order_tolerance=second_order_tolerance)
         print('Testing {} boundary with {} solver... '.format(boundary, 'higher order FFT')),
         _run_higher_order_fft_reconstruction(in_data, set_accuracy, order=2, tolerance=second_order_tolerance)
Пример #8
0
 def poisson_gradient(_op, grad):
     return CenteredGrid.sample(
         grad,
         poisson_domain.domain).laplace(physical_units=False).data
Пример #9
0
 def test_staggered_curl2d(self):
     domain = Domain([32, 32])
     pot = CenteredGrid.sample(Noise(), domain)
     vel = staggered_curl_2d(pot)
     div = vel.divergence()
     np.testing.assert_almost_equal(div.data, 0, decimal=5)