Esempio n. 1
0
 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)
Esempio n. 2
0
 def test_batched_forced_burgers_2d(self):
     world = World(batch_size=3)
     burgers = world.add(Domain([4, 4]).centered_grid(0, batch_size=world.batch_size, name='velocity'))
     k = math.to_float(numpy.random.uniform(3, 6, [world.batch_size, 2]))
     amplitude = numpy.random.uniform(-0.5, 0.5, [world.batch_size])
     force = SinPotential(k, phase_offset=numpy.random.uniform(0, 2 * numpy.pi, [world.batch_size]), data=amplitude)
     physics = ForcingPhysics(numpy.random.uniform(-0.4, 0.4, [world.batch_size]))
     effect = FieldEffect(force, ['velocity'])
     world.add(effect, physics=physics)
     burgers.step()
     burgers.step()
Esempio n. 3
0
 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
Esempio n. 4
0
 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)
Esempio n. 5
0
def tf_bake_subgraph(tracker, session):
    tfworld = World()
    tfworld.add(tracker.state)
    # --- Build placeholder state ---
    dtype = _32_bit(math.types(tracker.state))
    shape = tracker.state.shape
    state_in = placeholder(shape, dtype=dtype)
    dt = placeholder(())
    # --- Build graph ---
    state_out = tracker.world.physics.substep(state_in, tracker.world.state,
                                              dt)
    tracker.physics = BakedPhysics(session, state_in, state_out, dt)
    return tfworld
Esempio n. 6
0
def tf_bake_subgraph(tracker, session):
    tfworld = World()
    tfworld.add(tracker.state)
    # --- Build placeholder state ---
    with VARIABLES:
        state_in = placeholder(tracker.state.staticshape,
                               dtype=tracker.state.dtype)
    dt = placeholder(())
    # --- Build graph ---
    state_out = tracker.world.physics.substep(state_in, tracker.world.state,
                                              dt)
    tracker.physics = BakedPhysics(session, state_in, state_out, dt)
    return tfworld
Esempio n. 7
0
 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)
Esempio n. 8
0
 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)
Esempio n. 9
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'])
Esempio n. 10
0
    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))
Esempio n. 11
0
 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)
Esempio n. 12
0
    def test_names(self):
        c = CollectiveState()
        self.assertEqual(c.states, {})
        c = c.state_added(Fluid(Domain([64])))
        try:
            c = c.state_added(Fluid(Domain([80])))
            self.fail()
        except AssertionError:
            pass
        c = c.state_replaced(Fluid(Domain([80])))
        numpy.testing.assert_equal(c.fluid.density.data.shape, [1, 80, 1])

        world = World(add_default_objects=True)
        assert world.gravity.state is world.state.gravity
Esempio n. 13
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
Esempio n. 14
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, ...]
Esempio n. 15
0
 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()