forked from 3b1b/manim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluid_flow.py
407 lines (342 loc) · 10.4 KB
/
fluid_flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
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, Line
from topics.number_line import NumberPlane, XYZAxes
from topics.three_dimensions import Sphere
from scene import Scene
from animation.simple_animations import \
ShowCreation, Rotating, PhaseFlow, ApplyToCenters
from animation.transform import \
Transform, ApplyMethod, FadeOut, ApplyFunction
from helpers import *
class FluidFlow(Scene):
DEFAULT_CONFIG = {
"arrow_spacing" : 1,
"dot_spacing" : 0.5,
"dot_color" : BLUE_B,
"text_color" : WHITE,
"arrow_color" : GREEN_A,
"points_height" : SPACE_HEIGHT,
"points_width" : SPACE_WIDTH,
}
def use_function(self, function):
self.function = function
def get_points(self, spacing):
x_radius, y_radius = [
val-val%spacing
for val in self.points_width, self.points_height
]
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()
def add_arrows(self, true_length = False):
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
)
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)
self.play(ShowCreation(self.arrows))
self.dither()
def add_paddle(self):
pass
def flow(self, **kwargs):
if not hasattr(self, "function"):
raise Exception("Must run use_function first")
self.play(ApplyToCenters(
PhaseFlow,
self.dots.split(),
function = self.function,
**kwargs
))
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)
class NegativeDivergenceExamlpe(FluidFlow):
DEFAULT_CONFIG = {
"points_width" : 2*SPACE_WIDTH,
"points_height" : 2*SPACE_HEIGHT,
}
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(circle)
self.add_arrows()
self.add_dots()
self.flow(run_time = 2, virtual_time = 2)
self.dither(2)
class PositiveDivergenceExample(FluidFlow):
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(circle)
self.add_arrows()
self.add_dots()
self.flow(run_time = 2, virtual_time = 2)
self.dither(2)
class DivergenceArticleExample(FluidFlow):
def construct(self):
def raw_function((x, y, z)):
return (2*x-y, y*y, 0)
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(
virtual_time = 4,
run_time = 5
)
class QuadraticField(FluidFlow):
def construct(self):
self.use_function(
lambda (x, y, z) : 0.25*((x*x-y*y)*RIGHT+x*y*UP)
)
self.add_plane()
self.add_arrows()
self.add_dots()
self.flow(
virtual_time = 10,
run_time = 20,
rate_func = None
)
class IncompressibleFluid(FluidFlow):
DEFAULT_CONFIG = {
"points_width" : 2*SPACE_WIDTH,
"points_height" : 1.4*SPACE_HEIGHT
}
def construct(self):
self.use_function(
lambda (x, y, z) : RIGHT+np.sin(x)*UP
)
self.add_plane()
self.add_arrows()
self.add_dots()
for x in range(8):
self.flow(
run_time = 1,
rate_func = None,
)
class ConstantInwardFlow(FluidFlow):
DEFAULT_CONFIG = {
"points_height" : 3*SPACE_HEIGHT,
"points_width" : 3*SPACE_WIDTH,
}
def construct(self):
self.use_function(
lambda p : -3*p/(np.linalg.norm(p)+0.1)
)
self.add_plane()
self.add_arrows()
self.add_dots()
for x in range(4):
self.flow(
run_time = 5,
rate_func = None,
)
class ConstantOutwardFlow(FluidFlow):
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)
class ConstantPositiveCurl(FluidFlow):
DEFAULT_CONFIG = {
"points_height" : SPACE_WIDTH,
}
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()
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 SingleSwirl(FluidFlow):
DEFAULT_CONFIG = {
"points_height" : SPACE_WIDTH,
}
def construct(self):
self.use_function(
lambda p : (-p[1]*RIGHT+p[0]*UP)/np.linalg.norm(p)
)
self.add_plane()
self.add_arrows()
self.add_dots()
for x in range(10):
self.flow(rate_func = None)
class CurlArticleExample(FluidFlow):
DEFAULT_CONFIG = {
"points_height" : 3*SPACE_HEIGHT,
"points_width" : 3*SPACE_WIDTH
}
def construct(self):
self.use_function(
lambda (x, y, z) : np.cos(0.5*(x+y))*RIGHT + np.sin(0.25*x*y)*UP
)
self.add_plane()
self.add_arrows()
self.add_dots()
self.flow(
rate_func = None,
run_time = 15,
virtual_time = 10
)
class FourSwirlsWithoutCircles(FluidFlow):
DEFAULT_CONFIG = {
"points_height" : 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()
# for circle in circles:
# self.play(ShowCreation(circle))
self.add_dots()
self.add_extra_dots()
self.flow(
virtual_time = 4,
run_time = 20,
rate_func = None
)
def add_extra_dots(self):
dots = self.dots.split()
for vect in UP+LEFT, DOWN+RIGHT:
for n in range(5, 15):
dots.append(
dots[0].copy().center().shift(n*vect)
)
self.dots = Mobject(*dots)
class CopyPlane(Scene):
def construct(self):
def special_rotate(mob):
mob.rotate(0.9*np.pi/2, RIGHT)
mob.rotate(-np.pi/4, UP)
return mob
plane = NumberPlane()
copies = [
special_rotate(plane.copy().shift(u*n*OUT))
for n in range(1, 3)
for u in -1, 1
]
line = Line(4*IN, 4*OUT)
self.add(plane)
self.play(*[
ApplyFunction(special_rotate, mob, run_time = 3)
for mob in plane, line
])
self.dither()
for copy in copies:
self.play(Transform(plane.copy(), copy))
self.dither()
class Test3DMovement(Scene):
def construct(self):
axes = XYZAxes()
axes.highlight(WHITE)
plane = NumberPlane()
vects = [
Arrow(point, point+(3./27)*(3*x**2-3*y**2)*OUT, color = MAROON_D)
for x in range(-4, 5, 2)
for y in range(-5, 5, 2)
for point in [x*RIGHT + y*UP]
]
everybody = Mobject(axes, plane, *vects)
self.play(ApplyMethod(
everybody.rotate, 0.9*np.pi/2, RIGHT
))
self.dither()
self.play(ApplyMethod(
everybody.rotate,
np.pi/2,
run_time = 5
))