2016-01-07 16:25:59 -08:00
|
|
|
from mobject import Mobject
|
|
|
|
from mobject.image_mobject import MobjectFromRegion
|
|
|
|
from mobject.tex_mobject import TextMobject
|
|
|
|
from region import region_from_polygon_vertices
|
|
|
|
from topics.geometry import Arrow, Dot, Circle
|
|
|
|
from topics.number_line import NumberPlane
|
|
|
|
from scene import Scene
|
2016-01-12 11:51:54 -08:00
|
|
|
from animation.simple_animations import \
|
|
|
|
ShowCreation, Rotating, PhaseFlow, ApplyToCenters
|
2016-01-07 16:25:59 -08:00
|
|
|
from animation.transform import Transform, ApplyMethod, FadeOut
|
|
|
|
|
|
|
|
from helpers import *
|
|
|
|
|
|
|
|
|
2016-01-11 15:54:52 -08:00
|
|
|
class FluidFlow(Scene):
|
2016-01-07 16:25:59 -08:00
|
|
|
DEFAULT_CONFIG = {
|
|
|
|
"arrow_spacing" : 1,
|
|
|
|
"dot_spacing" : 0.5,
|
2016-01-11 15:54:52 -08:00
|
|
|
"dot_color" : BLUE_B,
|
2016-01-07 16:25:59 -08:00
|
|
|
"text_color" : WHITE,
|
2016-01-11 15:54:52 -08:00
|
|
|
"arrow_color" : GREEN_A,
|
2016-01-09 20:07:44 -08:00
|
|
|
"points_height" : SPACE_HEIGHT,
|
|
|
|
"points_width" : SPACE_WIDTH,
|
2016-01-07 16:25:59 -08:00
|
|
|
}
|
|
|
|
def use_function(self, function):
|
|
|
|
# def normalized_func(point):
|
|
|
|
# result = function(point)
|
|
|
|
# length = np.linalg.norm(result)
|
|
|
|
# if length > 0:
|
|
|
|
# result /= length
|
|
|
|
# # result *= self.arrow_spacing/2.
|
|
|
|
# return result
|
|
|
|
self.function = function
|
|
|
|
|
|
|
|
def get_points(self, spacing):
|
|
|
|
x_radius, y_radius = [
|
|
|
|
val-val%spacing
|
2016-01-09 20:07:44 -08:00
|
|
|
for val in self.points_width, self.points_height
|
2016-01-07 16:25:59 -08:00
|
|
|
]
|
|
|
|
return map(np.array, it.product(
|
|
|
|
np.arange(-x_radius, x_radius+spacing, spacing),
|
|
|
|
np.arange(-y_radius, y_radius+spacing, spacing),
|
|
|
|
[0]
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
def add_plane(self):
|
|
|
|
self.add(NumberPlane().fade())
|
|
|
|
|
|
|
|
def add_dots(self):
|
|
|
|
points = self.get_points(self.dot_spacing)
|
|
|
|
self.dots = Mobject(*map(Dot, points))
|
|
|
|
self.dots.highlight(self.dot_color)
|
|
|
|
self.play(ShowCreation(self.dots))
|
|
|
|
self.dither()
|
|
|
|
|
2016-01-11 15:54:52 -08:00
|
|
|
def add_arrows(self, true_length = False):
|
2016-01-07 16:25:59 -08:00
|
|
|
if not hasattr(self, "function"):
|
|
|
|
raise Exception("Must run use_function first")
|
|
|
|
points = self.get_points(self.arrow_spacing)
|
|
|
|
points = filter(
|
|
|
|
lambda p : np.linalg.norm(self.function(p)) > 0.01,
|
|
|
|
points
|
|
|
|
)
|
|
|
|
angles = map(angle_of_vector, map(self.function, points))
|
|
|
|
prototype = Arrow(
|
|
|
|
ORIGIN, RIGHT*self.arrow_spacing/2.,
|
|
|
|
color = self.arrow_color,
|
|
|
|
tip_length = 0.1,
|
|
|
|
buff = 0
|
|
|
|
)
|
2016-01-11 15:54:52 -08:00
|
|
|
arrows = []
|
|
|
|
for point in points:
|
|
|
|
arrow = prototype.copy()
|
|
|
|
output = self.function(point)
|
|
|
|
if true_length:
|
|
|
|
arrow.scale(np.linalg.norm(output))
|
|
|
|
arrow.rotate(angle_of_vector(output))
|
|
|
|
arrow.shift(point)
|
|
|
|
arrows.append(arrow)
|
|
|
|
self.arrows = Mobject(*arrows)
|
2016-01-07 16:25:59 -08:00
|
|
|
|
|
|
|
self.play(ShowCreation(self.arrows))
|
|
|
|
self.dither()
|
|
|
|
|
2016-01-12 11:51:54 -08:00
|
|
|
def add_paddle(self):
|
|
|
|
pass
|
2016-01-07 16:25:59 -08:00
|
|
|
|
2016-01-09 20:07:44 -08:00
|
|
|
def flow(self, **kwargs):
|
2016-01-07 16:25:59 -08:00
|
|
|
if not hasattr(self, "function"):
|
|
|
|
raise Exception("Must run use_function first")
|
2016-01-12 11:51:54 -08:00
|
|
|
self.play(ApplyToCenters(
|
|
|
|
PhaseFlow,
|
|
|
|
self.dots.split(),
|
|
|
|
function = self.function,
|
|
|
|
**kwargs
|
|
|
|
))
|
2016-01-07 16:25:59 -08:00
|
|
|
|
|
|
|
def label(self, text, time = 5):
|
|
|
|
mob = TextMobject(text)
|
|
|
|
mob.scale(1.5)
|
|
|
|
mob.to_edge(UP)
|
|
|
|
rectangle = region_from_polygon_vertices(*[
|
|
|
|
mob.get_corner(vect) + 0.3*vect
|
|
|
|
for vect in [
|
|
|
|
UP+RIGHT,
|
|
|
|
UP+LEFT,
|
|
|
|
DOWN+LEFT,
|
|
|
|
DOWN+RIGHT
|
|
|
|
]
|
|
|
|
])
|
|
|
|
mob.highlight(self.text_color)
|
|
|
|
rectangle = MobjectFromRegion(rectangle, "#111111")
|
|
|
|
rectangle.point_thickness = 3
|
|
|
|
self.add(rectangle, mob)
|
|
|
|
self.dither(time)
|
|
|
|
self.remove(mob, rectangle)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-11 15:54:52 -08:00
|
|
|
class InwardFlow(FluidFlow):
|
2016-01-07 16:25:59 -08:00
|
|
|
def construct(self):
|
|
|
|
circle = Circle(color = YELLOW_C)
|
|
|
|
self.use_function(
|
|
|
|
lambda p : -p/(2*np.linalg.norm(0.5*p)**0.5+0.01)
|
|
|
|
)
|
|
|
|
self.add_plane()
|
|
|
|
self.add_arrows()
|
|
|
|
self.play(ShowCreation(circle))
|
|
|
|
self.label("""
|
|
|
|
Notice that arrows point inward around the origin
|
|
|
|
""")
|
|
|
|
self.label("""
|
|
|
|
Watch what that means as we let particles in \\\\
|
|
|
|
space flow along the arrows
|
|
|
|
""")
|
|
|
|
self.remove(circle)
|
|
|
|
circle.scale(0.5)
|
|
|
|
self.add_dots()
|
|
|
|
self.flow()
|
|
|
|
self.remove(self.arrows)
|
|
|
|
self.play(ShowCreation(circle))
|
|
|
|
self.label("""
|
|
|
|
The density of points around \\\\
|
|
|
|
the origin has become greater
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.label("""
|
|
|
|
This means the divergence of the vector field \\\\
|
|
|
|
is negative at the origin:
|
|
|
|
$\\nabla \\cdot \\vec{\\textbf{v}}(0, 0) < 0$
|
|
|
|
""")
|
2016-01-11 15:54:52 -08:00
|
|
|
self.dither(3)
|
2016-01-07 16:25:59 -08:00
|
|
|
|
|
|
|
|
2016-01-11 15:54:52 -08:00
|
|
|
class OutwardFlow(FluidFlow):
|
2016-01-07 16:25:59 -08:00
|
|
|
def construct(self):
|
|
|
|
circle = Circle(color = YELLOW_C, radius = 2)
|
|
|
|
self.use_function(
|
|
|
|
lambda p : p/(2*np.linalg.norm(0.5*p)**0.5+0.01)
|
|
|
|
)
|
|
|
|
self.add_plane()
|
|
|
|
self.add_arrows()
|
|
|
|
self.play(ShowCreation(circle))
|
|
|
|
self.label("""
|
|
|
|
On the other hand, when arrows \\\\
|
|
|
|
indicate an outward flow\\dots
|
|
|
|
""")
|
|
|
|
self.remove(circle)
|
|
|
|
circle.scale(0.5)
|
|
|
|
self.add_dots()
|
|
|
|
self.flow()
|
|
|
|
self.remove(self.arrows)
|
|
|
|
self.play(ShowCreation(circle))
|
|
|
|
self.label("""
|
|
|
|
The density of points near \\\\
|
|
|
|
the origin becomes smaller
|
|
|
|
""")
|
|
|
|
self.label("""
|
|
|
|
This means the divergence of the vector field \\\\
|
|
|
|
is positive at the origin:
|
|
|
|
$\\nabla \\cdot \\vec{\\textbf{v}}(0, 0) > 0$
|
|
|
|
""")
|
2016-01-11 15:54:52 -08:00
|
|
|
self.dither(3)
|
2016-01-07 16:25:59 -08:00
|
|
|
|
2016-01-12 11:51:54 -08:00
|
|
|
class DivergenceArticleExample(FluidFlow):
|
2016-01-07 16:25:59 -08:00
|
|
|
def construct(self):
|
|
|
|
def raw_function((x, y, z)):
|
2016-01-09 20:07:44 -08:00
|
|
|
return (2*x-y, y*y, 0)
|
2016-01-07 16:25:59 -08:00
|
|
|
def normalized_function(p):
|
|
|
|
result = raw_function(p)
|
|
|
|
return result/(np.linalg.norm(result)+0.01)
|
|
|
|
self.use_function(normalized_function)
|
|
|
|
|
|
|
|
self.add_plane()
|
|
|
|
self.add_arrows()
|
|
|
|
self.add_dots()
|
|
|
|
self.flow()
|
|
|
|
self.remove(self.arrows)
|
2016-01-11 15:54:52 -08:00
|
|
|
self.dither(3)
|
2016-01-07 16:25:59 -08:00
|
|
|
|
|
|
|
|
2016-01-09 20:07:44 -08:00
|
|
|
|
2016-01-11 15:54:52 -08:00
|
|
|
class IncompressibleFluid(FluidFlow):
|
2016-01-12 11:51:54 -08:00
|
|
|
DEFAULT_CONFIG = {
|
|
|
|
"points_width" : 2*SPACE_WIDTH,
|
|
|
|
"points_height" : 1.4*SPACE_HEIGHT
|
|
|
|
}
|
2016-01-09 20:07:44 -08:00
|
|
|
def construct(self):
|
|
|
|
self.use_function(
|
2016-01-12 11:51:54 -08:00
|
|
|
lambda (x, y, z) : RIGHT+np.sin(x)*UP
|
2016-01-09 20:07:44 -08:00
|
|
|
)
|
|
|
|
self.add_plane()
|
|
|
|
self.add_arrows()
|
|
|
|
self.add_dots()
|
2016-01-12 11:51:54 -08:00
|
|
|
for x in range(8):
|
|
|
|
self.flow(
|
|
|
|
run_time = 1,
|
|
|
|
rate_func = None,
|
|
|
|
)
|
2016-01-09 20:07:44 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-11 15:54:52 -08:00
|
|
|
class ConstantInwardFlow(FluidFlow):
|
2016-01-12 11:51:54 -08:00
|
|
|
DEFAULT_CONFIG = {
|
|
|
|
"points_height" : 3*SPACE_HEIGHT,
|
|
|
|
"points_width" : 3*SPACE_WIDTH,
|
|
|
|
}
|
2016-01-09 20:07:44 -08:00
|
|
|
def construct(self):
|
|
|
|
self.use_function(
|
2016-01-12 11:51:54 -08:00
|
|
|
lambda p : -3*p/(np.linalg.norm(p)+0.1)
|
2016-01-09 20:07:44 -08:00
|
|
|
)
|
|
|
|
self.add_plane()
|
|
|
|
self.add_arrows()
|
|
|
|
self.add_dots()
|
|
|
|
for x in range(4):
|
2016-01-12 11:51:54 -08:00
|
|
|
self.flow(
|
|
|
|
run_time = 5,
|
|
|
|
rate_func = None,
|
|
|
|
)
|
2016-01-09 20:07:44 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-11 15:54:52 -08:00
|
|
|
class ConstantOutwardFlow(FluidFlow):
|
2016-01-09 20:07:44 -08:00
|
|
|
def construct(self):
|
|
|
|
self.use_function(
|
|
|
|
lambda p : p/(2*np.linalg.norm(0.5*p)**0.5+0.01)
|
|
|
|
)
|
|
|
|
self.add_plane()
|
|
|
|
self.add_arrows()
|
|
|
|
self.add_dots()
|
|
|
|
for x in range(4):
|
|
|
|
self.flow(rate_func = None)
|
|
|
|
dot = self.dots.split()[0].copy()
|
|
|
|
dot.center()
|
|
|
|
new_dots = [
|
|
|
|
dot.copy().shift(0.5*vect)
|
|
|
|
for vect in [
|
|
|
|
UP, DOWN, LEFT, RIGHT,
|
|
|
|
UP+RIGHT, UP+LEFT, DOWN+RIGHT, DOWN+LEFT
|
|
|
|
]
|
|
|
|
]
|
|
|
|
self.dots.add(*new_dots)
|
|
|
|
|
|
|
|
|
2016-01-11 15:54:52 -08:00
|
|
|
class ConstantPositiveCurl(FluidFlow):
|
2016-01-12 11:51:54 -08:00
|
|
|
DEFAULT_CONFIG = {
|
|
|
|
"points_height" : SPACE_WIDTH,
|
|
|
|
}
|
2016-01-11 15:54:52 -08:00
|
|
|
def construct(self):
|
|
|
|
self.use_function(
|
|
|
|
lambda p : 0.5*(-p[1]*RIGHT+p[0]*UP)
|
|
|
|
)
|
|
|
|
self.add_plane()
|
|
|
|
self.add_arrows(true_length = True)
|
|
|
|
self.add_dots()
|
2016-01-12 11:51:54 -08:00
|
|
|
for x in range(10):
|
|
|
|
self.flow(
|
|
|
|
rate_func = None
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ComplexCurlExample(FluidFlow):
|
|
|
|
def construct(self):
|
|
|
|
self.use_function(
|
|
|
|
lambda (x, y, z) : np.cos(x+y)*RIGHT+np.sin(x*y)*UP
|
|
|
|
)
|
|
|
|
self.add_plane()
|
|
|
|
self.add_arrows(true_length = True)
|
|
|
|
self.add_dots()
|
|
|
|
for x in range(4):
|
|
|
|
self.flow(
|
|
|
|
run_time = 5,
|
|
|
|
rate_func = None,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class FourSwirls(FluidFlow):
|
|
|
|
DEFAULT_CONFIG = {
|
|
|
|
"points_height" :SPACE_WIDTH,
|
|
|
|
"points_width" : SPACE_WIDTH,
|
|
|
|
}
|
|
|
|
def construct(self):
|
|
|
|
circles = [
|
|
|
|
Circle().shift(3*vect)
|
|
|
|
for vect in compass_directions()
|
|
|
|
]
|
|
|
|
self.use_function(
|
|
|
|
lambda (x, y, z) : 0.5*(y**3-9*y)*RIGHT+(x**3-9*x)*UP
|
|
|
|
)
|
|
|
|
self.add_plane()
|
|
|
|
self.add_arrows()
|
|
|
|
|
|
|
|
Mobject(*circles).show()
|
|
|
|
|
|
|
|
for circle in circles:
|
|
|
|
self.play(ShowCreation(circle))
|
|
|
|
self.add_dots()
|
|
|
|
for x in range(4):
|
|
|
|
self.flow(
|
|
|
|
run_time = 5,
|
|
|
|
rate_func = None,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-09 20:07:44 -08:00
|
|
|
|
|
|
|
|
|
|
|
|