def test_gradient_batch_independence(self): session = Session(None) # Used to run the TensorFlow graph world = World() fluid = world.add(Fluid(Domain([40, 32], boundaries=CLOSED), buoyancy_factor=0.1, batch_size=2), physics=IncompressibleFlow()) world.add(Inflow(Sphere(center=numpy.array([[5, 4], [5, 8]]), radius=3), rate=0.2)) fluid.velocity = variable(fluid.velocity) # create TensorFlow variable # fluid.velocity *= 0 initial_state = fluid.state # Remember the state at t=0 for later visualization session.initialize_variables() for frame in range(3): world.step(dt=1.5) target = session.run(fluid.density).data[0, ...] loss = tf.nn.l2_loss(fluid.density.data[1, ...] - target) self_loss = tf.nn.l2_loss(fluid.density.data[0, ...] - target) # loss = self_loss optim = tf.train.GradientDescentOptimizer(learning_rate=0.2).minimize(loss) session.initialize_variables() for optim_step in range(3): _, loss_value, sl_value = session.run([optim, loss, self_loss]) staggered_velocity = session.run(initial_state.velocity).staggered_tensor() numpy.testing.assert_equal(staggered_velocity[0, ...], 0) assert numpy.all(~numpy.isnan(staggered_velocity))
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 test_effects(self): world = World() fluid = world.add(Fluid(Domain([16, 16]))) fan = world.add(Fan(Sphere((10, 8), 5), [-1, 0])) obstacle = world.add(Obstacle(box[0:1, 0:1])) world.step(dt=1) world.step(dt=0.5) assert fluid.age == fan.age == obstacle.age == 1.5
def test_simpleplume(self): world = World() world.batch_size = 3 fluid = world.add(Fluid(Domain([16, 16]))) inflow = world.add(Inflow(Sphere((8, 8), radius=4))) world.step() world.step(fluid) self.assertAlmostEqual(fluid.age, 2.0) self.assertAlmostEqual(inflow.age, 1.0)
def test_order(self): world = World() order = [] world.add( Inflow(box[0:0]), physics=CustomPhys('Inflow', order, [StateDependency('d', 'fan', blocking=True)])) world.add(Fan(box[0:0], 0), physics=CustomPhys('Fan', order, [])) world.step() numpy.testing.assert_equal(order, ['Fan', 'Inflow'])
def test_collective_step(self): world = World() obstacle = world.add(Obstacle(geometry_at(0)), physics=GeometryMovement(geometry_at)) inflow = world.add(Inflow(geometry_at(0))) inflow.physics = world.get_physics(obstacle) static_obstacle = world.add(Obstacle(box[0:1])) world.step() self.assertAlmostEqual(obstacle.age, 1.0) self.assertAlmostEqual(obstacle.geometry.center[0], 1.0) self.assertAlmostEqual(inflow.age, 1.0) # self.assertAlmostEqual(inflow.field.geometry.center[0], 1.0) self.assertAlmostEqual(static_obstacle.age, 1.0)
def simulate(centers): world = World() fluid = world.add(Fluid(Domain([5, 4], boundaries=CLOSED, box=AABox(0, [40, 32])), buoyancy_factor=0.1, batch_size=centers.shape[0]), physics=IncompressibleFlow(pressure_solver=SparseCG(max_iterations=3))) world.add(Inflow(Sphere(center=centers, radius=3), rate=0.2)) world.add(Fan(Sphere(center=centers, radius=5), acceleration=[1.0, 0])) world.step(dt=1.5) world.step(dt=1.5) world.step(dt=1.5) print() return fluid.density.data[0, ...], fluid.velocity.unstack()[0].data[0, ...], fluid.velocity.unstack()[1].data[0, ...]
def test_cyclic_dependency(self): world = World() order = [] inflow = world.add(Inflow(box[0:0])) inflow.physics = CustomPhys( 'Inflow', order, [StateDependency('d', 'fan', blocking=True)]) fan = world.add(Fan(box[0:0], 0)) fan.physics = CustomPhys( 'Fan', order, [StateDependency('d', 'inflow', blocking=True)]) try: world.step() self.fail('Cycle not recognized.') except AssertionError: pass
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_effects(self): world = World() world.add(Fluid(Domain([16, 16]))) world.add(Fan(Sphere((10, 8), 5), [-1, 0])) world.step() world.step()