def new_scene(self, count=None): if count is None: count = 1 if self.world.batch_size is None else self.world.batch_size self.scene = Scene.create(self.base_dir, self.scene_summary(), count=count, mkdir=True)
def test_tf_worldgraph(self): world = World() fluid = world.add(Fluid(Domain([16, 16]))) tf_bake_graph(world, Session(Scene.create('data', copy_calling_script=False))) world.step() self.assertIsInstance(fluid.state, Fluid) self.assertIsInstance(fluid.state.density.data, numpy.ndarray)
def build_test_database(path='data'): for scene in Scene.list(path): scene.remove() val = 1.0 for _scene_index in range(2): scene = Scene.create(path) for t in range(4): scene.write_sim_frame([np.zeros([1, 4, 4, 1]) + val, np.zeros([1, 5, 5, 2])], ['Density', 'Velocity'], t) val += 1
def test_fluid_tf(self): world = World() fluid = Fluid(Domain([16, 16])) world.add(fluid) world.add(Inflow(Sphere((8, 8), radius=4))) world.add(Obstacle(box[4:16, 0:8])) fluid_in = fluid.copied_with(density=placeholder, velocity=placeholder) fluid_out = world.step(fluid_in) self.assertIsInstance(fluid_out, Fluid) session = Session(Scene.create('data', copy_calling_script=False)) fluid = session.run(fluid_out, {fluid_in: fluid}) fluid = session.run(fluid_out, {fluid_in: fluid}) self.assertIsInstance(fluid, Fluid)
def test_write_batch(self): for scene in Scene.list('data'): scene.remove() scene_batch = Scene.create('data', count=2) self.assertEqual(scene_batch.batch_size, 2) self.assertEqual(len(Scene.list('data')), 2) batched_data = np.zeros([2, 4, 4, 1]) unbatched_data = np.ones([1, 4, 4, 1]) scene_batch.write(batched_data, frame=0) scene_batch.write(unbatched_data, frame=1) self.assertTrue(os.path.exists('data/sim_000000/unnamed_000000.npz')) self.assertTrue(os.path.exists('data/sim_000000/unnamed_000001.npz')) self.assertTrue(os.path.exists('data/sim_000001/unnamed_000000.npz')) self.assertTrue(os.path.exists('data/sim_000001/unnamed_000001.npz'))
def test_read_write_struct(self): for scene in Scene.list('data'): scene.remove() state = Fluid(Domain([4, 4])) scene = Scene.create('data') scene.write(state, frame=0) self.assertTrue(isfile(scene.subpath('density_000000.npz'))) self.assertTrue(isfile(scene.subpath('velocity_000000.npz'))) loaded_state = scene.read(state, frame=0) self.assertIsInstance(loaded_state, Fluid) self.assertIsInstance(loaded_state.velocity, StaggeredGrid) self.assertIsInstance(loaded_state.density, CenteredGrid) _differences = struct.compare([loaded_state.density, state.density]) self.assertEqual(loaded_state.density, state.density) print_differences(loaded_state.velocity.data, state.velocity.data) np.testing.assert_equal(loaded_state.velocity.data[0].data, state.velocity.data[0].data) scene.write(np.ones([1, 4, 4, 1]) * 2, frame=1) self.assertTrue(isfile(scene.subpath('unnamed_000001.npz'))) self.assertEqual(scene.read(None, frame=1)[0, 0, 0, 0], 2) scene.write([np.ones([1, 4, 4, 1])], ['Ones'], frame=2) self.assertTrue(isfile(scene.subpath('Ones_000002.npz'))) mystruct = [{ 'Two': np.ones([1, 4, 4, 1]) * 2, 'Three': np.ones([1, 4, 4, 1]) * 3 }] scene.write(mystruct, frame=3) self.assertTrue(isfile(scene.subpath('0_Three_000003.npz'))) self.assertTrue(isfile(scene.subpath('0_Two_000003.npz'))) loaded_struct = scene.read(mystruct, frame=3) self.assertIsInstance(loaded_struct, list) np.testing.assert_equal(mystruct[0]['Two'][0, 0, 0, 0], loaded_struct[0]['Two'][0, 0, 0, 0]) scene.remove()