Further progress on clacks solution

This commit is contained in:
Grant Sanderson 2019-01-15 12:19:34 -08:00
parent 633050c02a
commit 04d5ad2a9f
2 changed files with 632 additions and 96 deletions

View file

@ -3,31 +3,75 @@ import subprocess
from pydub import AudioSegment
class Block(Square):
CONFIG = {
"mass": 1,
"velocity": 0,
"width": None,
"label_text": None,
"label_scale_value": 0.8,
"fill_opacity": 1,
"stroke_width": 3,
"stroke_color": WHITE,
"fill_color": None,
"sheen_direction": UL,
"sheen": 0.5,
"sheen_direction": UL,
}
def __init__(self, **kwargs):
digest_config(self, kwargs)
if self.width is None:
self.width = self.mass_to_width(self.mass)
if self.fill_color is None:
self.fill_color = self.mass_to_color(self.mass)
if self.label_text is None:
self.label_text = self.mass_to_label_text(self.mass)
Square.__init__(self, side_length=self.width, **kwargs)
self.label = self.get_label()
self.add(self.label)
def get_label(self):
label = TextMobject(self.label_text)
label.scale(self.label_scale_value)
label.next_to(self, UP, SMALL_BUFF)
return label
def get_points_defining_boundary(self):
return self.points
def mass_to_color(self, mass):
colors = [
LIGHT_GREY,
BLUE_B,
BLUE_D,
BLUE_E,
BLUE_E,
DARK_GREY,
DARK_GREY,
BLACK,
]
index = min(int(np.log10(mass)), len(colors) - 1)
return colors[index]
def mass_to_width(self, mass):
return 1 + 0.25 * np.log10(mass)
def mass_to_label_text(self, mass):
return "{:,}\\,kg".format(int(mass))
class SlidingBlocks(VGroup):
CONFIG = {
"block1_config": {
"mass": 1,
"velocity": -2,
"distance": 7,
"width": None,
"color": None,
"label_text": None,
"mass": 1e6,
"velocity": -2,
},
"block2_config": {
"distance": 3,
"mass": 1,
"velocity": 0,
"distance": 3,
"width": None,
"color": None,
"label_text": None,
},
"block_style": {
"fill_opacity": 1,
"stroke_width": 3,
"stroke_color": WHITE,
"sheen_direction": UL,
"sheen_factor": 0.5,
"sheen_direction": UL,
},
"collect_clack_data": True,
}
@ -51,30 +95,13 @@ class SlidingBlocks(VGroup):
if self.collect_clack_data:
self.clack_data = self.get_clack_data()
def get_block(self, mass, distance, velocity, width, color, label_text):
if width is None:
width = self.mass_to_width(mass)
if color is None:
color = self.mass_to_color(mass)
if label_text is None:
label_text = "{:,}\\,kg".format(int(mass))
block = Square(side_length=width)
block.mass = mass
block.velocity = velocity
style = dict(self.block_style)
style["fill_color"] = color
block.set_style(**style)
def get_block(self, distance, **kwargs):
block = Block(**kwargs)
block.move_to(
self.floor.get_top()[1] * UP +
(self.wall.get_right()[0] + distance) * RIGHT,
DL,
)
label = block.label = TextMobject(label_text)
label.scale(0.8)
label.next_to(block, UP, SMALL_BUFF)
block.add(label)
return block
def get_phase_space_point_tracker(self):
@ -101,36 +128,6 @@ class SlidingBlocks(VGroup):
)
self.update_blocks_from_phase_space_point_tracker()
def old_update_positions(self, dt):
# Based on velocity diagram bouncing...didn't work for
# large masses, due to frame rate mismatch
blocks = self.submobjects
for block in blocks:
block.shift(block.velocity * dt * RIGHT)
if blocks[0].get_left()[0] < blocks[1].get_right()[0]:
# Two blocks collide
m1 = blocks[0].mass
m2 = blocks[1].mass
v1 = blocks[0].velocity
v2 = blocks[1].velocity
v_phase_space_point = np.array([
np.sqrt(m1) * v1, -np.sqrt(m2) * v2
])
angle = 2 * np.arctan(np.sqrt(m2 / m1))
new_vps_point = rotate_vector(v_phase_space_point, angle)
for block, value in zip(blocks, new_vps_point):
block.velocity = value / np.sqrt(block.mass)
blocks[1].move_to(blocks[0].get_corner(DL), DR)
self.surrounding_scene.clack(blocks[0].get_left())
if blocks[1].get_left()[0] < self.wall.get_right()[0]:
# Second block hits wall
blocks[1].velocity *= -1
blocks[1].move_to(self.wall.get_corner(DR), DL)
if blocks[0].get_left()[0] < blocks[1].get_right()[0]:
blocks[0].move_to(blocks[1].get_corner(DR), DL)
self.surrounding_scene.clack(blocks[1].get_left())
return self
def update_blocks_from_phase_space_point_tracker(self):
block1, block2 = self.block1, self.block2
@ -196,23 +193,6 @@ class SlidingBlocks(VGroup):
clack_data.append((location, time))
return clack_data
def mass_to_color(self, mass):
colors = [
LIGHT_GREY,
BLUE_B,
BLUE_D,
BLUE_E,
BLUE_E,
DARK_GREY,
DARK_GREY,
BLACK,
]
index = min(int(np.log10(mass)), len(colors) - 1)
return colors[index]
def mass_to_width(self, mass):
return 1 + 0.25 * np.log10(mass)
class ClackFlashes(ContinualAnimation):
CONFIG = {
@ -254,6 +234,33 @@ class ClackFlashes(ContinualAnimation):
self.mobject.remove(flash.mobject)
class Wall(Line):
CONFIG = {
"tick_spacing": 0.5,
"tick_length": 0.25,
"tick_style": {
"stroke_width": 1,
"stroke_color": WHITE,
},
}
def __init__(self, height, **kwargs):
Line.__init__(self, ORIGIN, height * UP, **kwargs)
self.height = height
self.ticks = self.get_ticks()
self.add(self.ticks)
def get_ticks(self):
n_lines = int(self.height / self.tick_spacing)
lines = VGroup(*[
Line(ORIGIN, self.tick_length * UR).shift(n * self.tick_spacing * UP)
for n in range(n_lines)
])
lines.set_style(**self.tick_style)
lines.move_to(self, DR)
return lines
class BlocksAndWallScene(Scene):
CONFIG = {
"include_sound": True,
@ -319,16 +326,10 @@ class BlocksAndWallScene(Scene):
self.counter_mob = counter_mob
def get_wall(self):
wall = Line(self.floor_y * UP, FRAME_HEIGHT * UP / 2)
height = (FRAME_HEIGHT / 2) - self.floor_y
wall = Wall(height=height)
wall.shift(self.wall_x * RIGHT)
lines = VGroup(*[
Line(ORIGIN, 0.25 * UR)
for x in range(self.n_wall_ticks)
])
lines.set_stroke(width=1)
lines.arrange_submobjects(UP, buff=MED_SMALL_BUFF)
lines.move_to(wall, DR)
wall.add(lines)
wall.to_edge(UP, buff=0)
return wall
def get_floor(self):
@ -1151,7 +1152,7 @@ class CompareToGalacticMass(Scene):
"velocity": -0.01,
"distance": 4.5,
"label_text": "$100^{(20 - 1)}$ kg",
"color": BLACK,
"fill_color": BLACK,
},
"block2_config": {
"distance": 1,

View file

@ -2,7 +2,6 @@ from big_ol_pile_of_manim_imports import *
from active_projects.clacks import *
# TODO, add solution image
class FromPuzzleToSolution(MovingCameraScene):
def construct(self):
@ -21,7 +20,7 @@ class FromPuzzleToSolution(MovingCameraScene):
images = Group(
ImageMobject("BlocksAndWallExampleMass16"),
ImageMobject("SphereSurfaceProof2"), # TODO
ImageMobject("SphereSurfaceProof2"), # TODO
)
for title, rect, image in zip(titles, rects, images):
title.scale(1.5)
@ -45,7 +44,6 @@ class FromPuzzleToSolution(MovingCameraScene):
self.wait()
class BlocksAndWallExampleMass16(BlocksAndWallExample):
CONFIG = {
"sliding_blocks_config": {
@ -139,3 +137,540 @@ class BlocksAndWallExampleMassTrillion(BlocksAndWallExample):
"min_time_between_sounds": 0.001,
}
# TODO, add sound
class AskAboutFindingNewVelocities(Scene):
CONFIG = {
"sound_file": None,
"floor_y": -3,
"wall_x": -6.5,
"wall_height": 7,
"block1_config": {
"mass": 10,
"fill_color": BLUE_E,
"velocity": -1,
},
"block2_config": {"mass": 1},
"block1_start_x": 7,
"block2_start_x": 3,
"v_arrow_scale_value": 1.0,
"is_halted": False,
}
def construct(self):
self.add_floor()
self.add_wall()
self.add_blocks()
self.add_velocity_labels()
self.ask_about_transfer()
self.show_ms_and_vs()
self.show_value_on_equations()
def add_floor(self):
floor = self.floor = Line(
self.wall_x * RIGHT,
(FRAME_WIDTH / 2) * RIGHT,
)
floor.shift(self.floor_y * UP)
self.add(floor)
def add_wall(self):
wall = self.wall = Wall(height=self.wall_height)
wall.move_to(
self.wall_x * RIGHT + self.floor_y * UP,
DR,
)
self.add(wall)
def add_blocks(self):
block1 = self.block1 = Block(**self.block1_config)
block2 = self.block2 = Block(**self.block2_config)
blocks = self.blocks = VGroup(block1, block2)
block1.move_to(self.block1_start_x * RIGHT + self.floor_y * UP, DOWN)
block2.move_to(self.block2_start_x * RIGHT + self.floor_y * UP, DOWN)
self.add_velocity_phase_space_point()
# Add arrows
for block in blocks:
arrow = Vector(self.block_to_v_vector(block))
arrow.set_color(RED)
arrow.set_stroke(BLACK, 1, background=True)
arrow.move_to(block.get_center(), RIGHT)
block.arrow = arrow
block.add(arrow)
block.v_label = DecimalNumber(
block.velocity,
num_decimal_places=2,
background_stroke_width=2,
)
block.v_label.set_color(RED)
block.add(block.v_label)
# Add updater
blocks.add_updater(self.update_blocks)
self.add(
blocks,
block2.arrow, block1.arrow,
block2.v_label, block1.v_label,
)
def add_velocity_phase_space_point(self):
self.vps_point = VectorizedPoint([
np.sqrt(self.block1.mass) * self.block1.velocity,
np.sqrt(self.block2.mass) * self.block2.velocity,
0
])
def add_velocity_labels(self):
v_labels = self.get_next_velocity_labels()
self.add(v_labels)
def ask_about_transfer(self):
energy_expression, momentum_expression = \
self.get_energy_and_momentum_expressions()
energy_words = TextMobject("Conservation of energy:")
energy_words.move_to(UP)
energy_words.to_edge(LEFT, buff=1.5)
momentum_words = TextMobject("Conservation of momentum:")
momentum_words.next_to(
energy_words, DOWN,
buff=0.7,
)
energy_expression.next_to(energy_words, RIGHT, MED_LARGE_BUFF)
momentum_expression.next_to(energy_expression, DOWN)
momentum_expression.next_to(momentum_words, RIGHT)
velocity_labels = self.all_velocity_labels
randy = Randolph(height=2)
randy.next_to(velocity_labels, DR)
randy.save_state()
randy.fade(1)
# Up to collisions
self.go_through_next_collision()
self.play(
randy.restore,
randy.change, "pondering", velocity_labels[0],
)
self.halt()
self.play(randy.look_at, velocity_labels[-1])
self.play(Blink(randy))
self.play(
FadeInFrom(energy_words, RIGHT),
FadeInFromDown(energy_expression),
FadeOut(randy),
)
self.wait()
self.play(
FadeInFrom(momentum_words, RIGHT),
FadeInFromDown(momentum_expression)
)
self.wait()
self.energy_expression = energy_expression
self.energy_words = energy_words
self.momentum_expression = momentum_expression
self.momentum_words = momentum_words
def show_ms_and_vs(self):
block1 = self.block1
block2 = self.block2
energy_expression = self.energy_expression
momentum_expression = self.momentum_expression
for block in self.blocks:
block.shift_onto_screen()
m1_labels = VGroup(
block1.label,
energy_expression.get_part_by_tex("m_1"),
momentum_expression.get_part_by_tex("m_1"),
)
m2_labels = VGroup(
block2.label,
energy_expression.get_part_by_tex("m_2"),
momentum_expression.get_part_by_tex("m_2"),
)
v1_labels = VGroup(
block1.v_label,
energy_expression.get_part_by_tex("v_1"),
momentum_expression.get_part_by_tex("v_1"),
)
v2_labels = VGroup(
block2.v_label,
energy_expression.get_part_by_tex("v_2"),
momentum_expression.get_part_by_tex("v_2"),
)
label_groups = VGroup(
m1_labels, m2_labels,
v1_labels, v2_labels,
)
for group in label_groups:
group.rects = VGroup(*map(
SurroundingRectangle,
group
))
for group in label_groups:
self.play(LaggedStart(
ShowCreation, group.rects,
lag_ratio=0.8,
run_time=1,
))
self.play(FadeOut(group.rects))
def show_value_on_equations(self):
energy_expression = self.energy_expression
momentum_expression = self.momentum_expression
energy_text = VGroup(energy_expression, self.energy_words)
momentum_text = VGroup(momentum_expression, self.momentum_words)
block1 = self.block1
block2 = self.block2
block1.save_state()
block2.save_state()
v_terms, momentum_v_terms = [
VGroup(*[
expr.get_part_by_tex("v_{}".format(d))
for d in [1, 2]
])
for expr in [energy_expression, momentum_expression]
]
v_braces = VGroup(*[
Brace(term, UP, buff=SMALL_BUFF)
for term in v_terms
])
v_decimals = VGroup(*[DecimalNumber(0) for x in range(2)])
def update_v_decimals(v_decimals):
values = self.get_velocities()
for decimal, value, brace in zip(v_decimals, values, v_braces):
decimal.set_value(value)
decimal.next_to(brace, UP, SMALL_BUFF)
update_v_decimals(v_decimals)
energy_const_brace, momentum_const_brace = [
Brace(
expr.get_part_by_tex("const"), UP,
buff=SMALL_BUFF,
)
for expr in [energy_expression, momentum_expression]
]
sqrt_m_vect = np.array([
np.sqrt(self.block1.mass),
np.sqrt(self.block2.mass),
0
])
def get_energy():
return 0.5 * get_norm(self.vps_point.get_location())**2
def get_momentum():
return np.dot(self.vps_point.get_location(), sqrt_m_vect)
energy_decimal = DecimalNumber(get_energy())
energy_decimal.next_to(energy_const_brace, UP, SMALL_BUFF)
momentum_decimal = DecimalNumber(get_momentum())
momentum_decimal.next_to(momentum_const_brace, UP, SMALL_BUFF)
VGroup(
energy_const_brace, energy_decimal,
momentum_const_brace, momentum_decimal,
).set_color(YELLOW)
self.play(
CircleThenFadeAround(energy_expression),
momentum_text.set_fill, {"opacity": 0.25},
FadeOut(self.all_velocity_labels),
)
self.play(*[
*map(GrowFromCenter, v_braces),
*map(VFadeIn, v_decimals),
GrowFromCenter(energy_const_brace),
FadeIn(energy_decimal),
])
energy_decimal.add_updater(
lambda m: m.set_value(get_energy())
)
v_decimals.add_updater(update_v_decimals)
self.add(v_decimals)
self.unhalt()
for x in range(4):
self.go_through_next_collision(
include_velocity_label_animation=False,
)
energy_decimal.clear_updaters()
momentum_decimal.set_value(get_momentum())
self.halt()
self.play(*[
momentum_text.set_fill, {"opacity": 1},
FadeOut(energy_text),
FadeOut(energy_const_brace),
FadeOut(energy_decimal),
GrowFromCenter(momentum_const_brace),
FadeIn(momentum_decimal),
*[
ApplyMethod(b.next_to, vt, UP, SMALL_BUFF)
for b, vt in zip(v_braces, momentum_v_terms)
],
])
self.unhalt()
momentum_decimal.add_updater(
lambda m: m.set_value(get_momentum())
)
momentum_decimal.add_updater(
lambda m: m.next_to(momentum_const_brace, UP, SMALL_BUFF)
)
for x in range(4):
self.go_through_next_collision(
include_velocity_label_animation=False,
)
self.wait(10)
# Helpers
def get_energy_and_momentum_expressions(self):
tex_to_color_map = {
"v_1": RED_B,
"v_2": RED_B,
"m_1": BLUE_C,
"m_2": BLUE_C,
}
energy_expression = TexMobject(
"\\frac{1}{2} m_1 (v_1)^2 + ",
"\\frac{1}{2} m_2 (v_2)^2 = ",
"\\text{const.}",
tex_to_color_map=tex_to_color_map,
)
momentum_expression = TexMobject(
"m_1 v_1 + m_2 v_2 =", "\\text{const.}",
tex_to_color_map=tex_to_color_map
)
return VGroup(
energy_expression,
momentum_expression,
)
def go_through_next_collision(self, include_velocity_label_animation=True):
block2 = self.block2
if block2.velocity >= 0:
self.wait_until(self.blocks_are_hitting)
self.transfer_momentum()
edge = RIGHT
else:
self.wait_until(self.block2_is_hitting_wall)
self.reflect_block2()
edge = LEFT
anims = [Flash(block2.get_edge_center(edge))]
if include_velocity_label_animation:
anims.append(self.get_next_velocity_labels_animation())
self.play(*anims, run_time=0.5)
def get_next_velocity_labels_animation(self):
return FadeInFrom(
self.get_next_velocity_labels(),
LEFT,
run_time=0.5
)
def get_next_velocity_labels(self, v1=None, v2=None):
new_labels = self.get_velocity_labels(v1, v2)
if hasattr(self, "all_velocity_labels"):
arrow = Vector(RIGHT)
arrow.next_to(self.all_velocity_labels)
new_labels.next_to(arrow, RIGHT)
new_labels.add(arrow)
else:
self.all_velocity_labels = VGroup()
self.all_velocity_labels.add(new_labels)
return new_labels
def get_velocity_labels(self, v1=None, v2=None):
default_vs = self.get_velocities()
v1 = v1 or default_vs[0]
v2 = v2 or default_vs[1]
labels = VGroup(
TexMobject("v_1 = {:.2f}".format(v1)),
TexMobject("v_2 = {:.2f}".format(v2)),
)
labels.arrange_submobjects(
DOWN,
buff=MED_SMALL_BUFF,
aligned_edge=LEFT,
)
labels.scale(0.9)
for label in labels:
label[:2].set_color(RED)
labels.next_to(self.wall, RIGHT)
labels.to_edge(UP, buff=MED_SMALL_BUFF)
return labels
def update_blocks(self, blocks, dt):
for block, velocity in zip(blocks, self.get_velocities()):
block.velocity = velocity
if not self.is_halted:
block.shift(block.velocity * dt * RIGHT)
center = block.get_center()
block.arrow.put_start_and_end_on(
center,
center + self.block_to_v_vector(block),
)
max_height = 0.25
block.v_label.set_value(block.velocity)
if block.v_label.get_height() > max_height:
block.v_label.set_height(max_height)
block.v_label.next_to(
block.arrow.get_start(), DOWN,
buff=SMALL_BUFF,
)
return blocks
def block_to_v_vector(self, block):
return block.velocity * self.v_arrow_scale_value * RIGHT
def blocks_are_hitting(self):
x1 = self.block1.get_left()[0]
x2 = self.block2.get_right()[0]
buff = 0.01
return (x1 < x2 + buff)
def block2_is_hitting_wall(self):
x2 = self.block2.get_left()[0]
buff = 0.01
return (x2 < self.wall_x + buff)
def get_velocities(self):
m1 = self.block1.mass
m2 = self.block2.mass
vps_coords = self.vps_point.get_location()
return [
vps_coords[0] / np.sqrt(m1),
vps_coords[1] / np.sqrt(m2),
]
def transfer_momentum(self):
m1 = self.block1.mass
m2 = self.block2.mass
theta = np.arctan(np.sqrt(m2 / m1))
self.reflect_block2()
self.vps_point.rotate(2 * theta, about_point=ORIGIN)
def reflect_block2(self):
self.vps_point.points[:, 1] *= -1
def halt(self):
self.is_halted = True
def unhalt(self):
self.is_halted = False
class IntroduceVelocityPhaseSpace(AskAboutFindingNewVelocities):
CONFIG = {
"wall_height": 1.5,
"floor_y": -3.5,
"block1_start_x": 5,
"block2_start_x": 0,
}
def construct(self):
self.add_wall_floor_and_blocks()
self.show_two_equations()
self.draw_axes()
self.draw_ellipse()
self.rescale_axes()
self.show_starting_point()
self.show_initial_collide()
self.ask_about_where_to_land()
self.show_conservation_of_momentum_equation()
self.show_momentum_line()
self.reiterate_meaning_of_line_and_circle()
self.show_first_jump()
self.show_bounce_off_wall()
self.show_reflection_about_x()
self.show_remaining_collisions()
def add_wall_floor_and_blocks(self):
self.add_floor()
self.add_wall()
self.add_blocks()
self.halt()
def show_two_equations(self):
equations = self.get_energy_and_momentum_expressions()
equations.arrange_submobjects(DOWN, buff=LARGE_BUFF)
equations.shift(UP)
v1_terms, v2_terms = [
VGroup(*[
expr.get_parts_by_tex(tex)
for expr in equations
])
for tex in ("v_1", "v_2")
]
self.add(equations)
self.play(LaggedStart(
AnimationGroup,
equations.copy().set_stroke(YELLOW, 3).set_fill(opacity=0),
lambda m: (ShowCreation(m), FadeOut(m)),
lag_ratio=0.8,
remover=True,
))
# self.play(*[
# LaggedStart(
# ShowCreationThenDestruction,
# expr.copy().set_stroke(YELLOW, 5),
# remover=True
# )
# for expr in equations
# ])
self.play(*map(Indicate, v1_terms))
self.play(*map(Indicate, v2_terms))
self.wait()
self.equations = equations
def draw_axes(self):
pass
def draw_ellipse(self):
pass
def rescale_axes(self):
pass
def show_starting_point(self):
pass
def show_initial_collide(self):
pass
def ask_about_where_to_land(self):
pass
def show_conservation_of_momentum_equation(self):
pass
def show_momentum_line(self):
pass
def reiterate_meaning_of_line_and_circle(self):
pass
def show_first_jump(self):
pass
def show_bounce_off_wall(self):
pass
def show_reflection_about_x(self):
pass
def show_remaining_collisions(self):
pass