def test_properties_dict(self): world = World() world.add(Fluid(Domain([16, 16])), physics=IncompressibleFlow()) world.add(Inflow(Sphere((8, 8), radius=4))) # world.add(ConstantDensity(box[0:2, 6:10], 1.0)) world.add(Fan(Sphere((10, 8), 5), [-1, 0])) struct.properties_dict(world.state)
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_effects(self): world = World() fluid = world.add(Fluid(Domain([16, 16])), physics=IncompressibleFlow()) 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])), physics=IncompressibleFlow()) 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_precision_16(self): try: math.set_precision(16) fluid = Fluid(Domain([16, 16]), density=math.maximum(0, Noise())) self.assertEqual(fluid.density.data.dtype, numpy.float16) self.assertEqual(fluid.velocity.unstack()[0].data.dtype, numpy.float16) fluid = IncompressibleFlow().step(fluid, dt=1.0) self.assertEqual(fluid.density.data.dtype, numpy.float16) self.assertEqual(fluid.velocity.unstack()[0].data.dtype, numpy.float16) finally: math.set_precision(32) # Reset environment
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, ...]