Пример #1
0
 def test_domain_grid_from_constant(self):
     domain = Domain(x=4, y=3)
     # int / float
     grid = domain.scalar_grid(1)
     math.assert_close(grid.values, 1)
     # complex
     grid = domain.scalar_grid(1 + 1j)
     math.assert_close(grid.values, 1 + 1j)
     # NumPy
     grid = domain.scalar_grid(numpy.array(1))
     math.assert_close(grid.values, 1)
Пример #2
0
 def test_write_read(self):
     DOMAIN = Domain(x=32, y=32, boundaries=CLOSED)
     smoke = DOMAIN.scalar_grid(1)
     vel = DOMAIN.staggered_grid(2)
     # write
     scene = Scene.create(DIR)
     scene.write(smoke=smoke, vel=vel)
     self.assertEqual(1, len(scene.frames))
     self.assertEqual(1, len(scene.complete_frames))
     self.assertEqual(2, len(scene.fieldnames))
     # read single
     smoke_ = scene.read('smoke')
     vel_ = scene.read('vel')
     field.assert_close(smoke, smoke_)
     field.assert_close(vel, vel_)
     self.assertEqual(smoke.extrapolation, smoke_.extrapolation)
     self.assertEqual(vel.extrapolation, vel_.extrapolation)
     # read multiple
     smoke__, vel__ = scene.read(['smoke', 'vel'])  # deprecated
     field.assert_close(smoke, smoke__)
     field.assert_close(vel, vel__)
     smoke__, vel__ = scene.read('smoke', 'vel')
     field.assert_close(smoke, smoke__)
     field.assert_close(vel, vel__)
     scene.remove()
Пример #3
0
 def test_constant_diffusion(self):
     DOMAIN = Domain(x=4, y=3, boundaries=PERIODIC)
     grid = DOMAIN.scalar_grid(1)
     explicit = diffuse.explicit(grid, 1, 1, substeps=10)
     implicit = diffuse.implicit(grid, 1, 1, order=2)
     fourier = diffuse.fourier(grid, 1, 1)
     math.assert_close(grid.values, explicit.values, implicit.values, fourier.values)
Пример #4
0
def _test_advection(adv):
    domain = Domain(x=4, y=3)
    s = domain.scalar_grid(Noise())
    v = domain.vector_grid(Noise(vector=2))
    field.assert_close(s, adv(s, v, 0), adv(s, v * 0, 1))
    sv = domain.staggered_grid(Noise())
    field.assert_close(s, adv(s, sv, 0), adv(s, sv * 0, 1))
    field.assert_close(sv, adv(sv, sv, 0), adv(sv, sv * 0, 1))
Пример #5
0
 def test_consistency_implicit(self):
     DOMAIN = Domain(x=200, boundaries=PERIODIC)
     DIFFUSIVITY = 0.5
     grid = DOMAIN.scalar_grid((1,) * 100 + (0,) * 100)
     for extrap in (extrapolation.ZERO, extrapolation.BOUNDARY, extrapolation.PERIODIC):
         grid = grid.with_(extrapolation=extrap)
         implicit = diffuse.implicit(grid, DIFFUSIVITY, 1, order=10)
         back_implicit = diffuse.implicit(implicit, DIFFUSIVITY, -1, order=10)
         field.assert_close(grid, back_implicit, rel_tolerance=0, abs_tolerance=0.1)
Пример #6
0
 def _test_make_incompressible_batched(self, grid_type):
     DOMAIN = Domain(x=16, y=16, boundaries=CLOSED, bounds=Box[0:100, 0:100])
     smoke = DOMAIN.scalar_grid(Sphere(center=(math.random_uniform(batch=2) * 100, 10), radius=5))
     velocity = DOMAIN.vector_grid(0, grid_type)
     for _ in range(2):
         velocity += smoke * (0, 0.1) >> velocity
         velocity, pressure, _, _ = fluid.make_incompressible(velocity, DOMAIN)
     math.assert_close(divergence(velocity).values, 0, abs_tolerance=2e-5)
     return velocity.values
Пример #7
0
 def test_implicit_stability(self):
     DOMAIN = Domain(x=6, boundaries=PERIODIC)
     DIFFUSIVITY = 10
     grid = DOMAIN.scalar_grid((1,) * 3 + (0,) * 3)
     try:
         implicit = diffuse.implicit(grid, DIFFUSIVITY, 1, order=10)
         print(implicit.values)
         field.assert_close(0 <= implicit <= 1.0001, True)
     except AssertionError as err:
         print(err)
         pass  # solve did not converge
Пример #8
0
 def test_write_read_batch_batched_files(self):
     DOMAIN = Domain(x=32, y=32, boundaries=CLOSED)
     smoke = DOMAIN.scalar_grid(1) * math.random_uniform(count=2, config=3)
     vel = DOMAIN.staggered_grid(2) * math.random_uniform(count=2, vel=2)
     # write
     scene = Scene.create(DIR, count=2)
     scene.write({'smoke': smoke, 'vel': vel})
     # read batch
     smoke_ = scene.read('smoke')
     vel_ = scene.read('vel')
     field.assert_close(smoke, smoke_)
     field.assert_close(vel, vel_)
     scene.remove()
Пример #9
0
 def test_write_read_batch_duplicate(self):
     DOMAIN = Domain(x=32, y=32, boundaries=CLOSED)
     smoke = DOMAIN.scalar_grid(1) * math.random_uniform(count=2)
     vel = DOMAIN.staggered_grid(2) * math.random_uniform(count=2)
     # write
     scene = Scene.create(DIR, more=2)
     scene.write({'smoke': smoke, 'vel': vel})
     # read batch
     smoke_ = scene.read('smoke')
     vel_ = scene.read('vel')
     self.assertEqual(4, smoke_.shape.batch.volume)
     self.assertEqual(4, vel_.shape.batch.volume)
     field.assert_close(smoke, smoke_)
     field.assert_close(vel, vel_)
     scene.remove()
Пример #10
0
 def test_domain_grid_from_constant(self):
     domain = Domain(x=4, y=3)
     # int / float
     grid = domain.scalar_grid(1)
     math.assert_close(grid.values, 1)
     # complex
     grid = domain.grid(1 + 1j)
     math.assert_close(grid.values, 1 + 1j)
     # NumPy
     grid = domain.grid(numpy.array(1))
     math.assert_close(grid.values, 1)
     # PyTorch
     grid = domain.grid(torch.tensor(1, dtype=torch.float32))
     math.assert_close(grid.values, 1)
     # TensorFlow
     grid = domain.grid(tf.constant(1, tf.float32))
     math.assert_close(grid.values, 1)
Пример #11
0
 def test_equality_1d_periodic(self):
     DOMAIN = Domain(x=200, boundaries=PERIODIC)
     DIFFUSIVITY = 0.5
     grid = DOMAIN.scalar_grid((1,) * 100 + (0,) * 100)
     explicit = diffuse.explicit(grid, DIFFUSIVITY, 1, substeps=1000)
     implicit = diffuse.implicit(grid, DIFFUSIVITY, 1, order=10)
     fourier = diffuse.fourier(grid, DIFFUSIVITY, 1)
     field.assert_close(explicit, implicit, rel_tolerance=0, abs_tolerance=0.01)
     field.assert_close(explicit, implicit, fourier, rel_tolerance=0, abs_tolerance=0.1)
     # print(f"{explicit.values[:6]}  Explicit")
     # print(f"{implicit.values[:6]}  Implicit")
     # print(f"{fourier.values[:6]}  Fourier")
     # print()
     back_explicit = diffuse.explicit(explicit, DIFFUSIVITY, -1, substeps=1000)
     back_implicit = diffuse.implicit(implicit, DIFFUSIVITY, -1, order=10)
     back_fourier = diffuse.fourier(fourier, DIFFUSIVITY, -1)
     # print(f"{back_explicit.values[:6]}  Explicit")
     # print(f"{back_implicit.values[:6]}  Implicit")
     # print(f"{back_fourier.values[:6]}  Fourier")
     field.assert_close(grid, back_explicit, back_implicit, back_fourier, rel_tolerance=0, abs_tolerance=0.1)
Пример #12
0
 def test_custom_spatial_dims(self):
     domain = Domain(a=4, b=3)
     grid = domain.scalar_grid(1)
     self.assertEqual(math.shape(a=4, b=3), grid.shape)
     grid = domain.staggered_grid(1)
     self.assertEqual(math.shape(a=4, b=3, vector=2), grid.shape)
Пример #13
0
 def test_domain_grid_memory_allocation(self):
     domain = Domain(x=10000, y=10000, z=10000, w=10000)
     grid = domain.scalar_grid()
     self.assertEqual((10000, ) * 4, grid.shape.sizes)
     sgrid = domain.staggered_grid()
     self.assertEqual((10000, 10000, 10000, 10000, 4), sgrid.shape.sizes)