Skip to content

Instantly share code, notes, and snippets.

@behackl
Created September 18, 2021 23:18
Show Gist options
  • Save behackl/a4941f8ca21e8f10fa825251a40f9081 to your computer and use it in GitHub Desktop.
Save behackl/a4941f8ca21e8f10fa825251a40f9081 to your computer and use it in GitHub Desktop.
Interactive manimation worksheet: center of mass of emptying can
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "a231ccc2-9158-41cc-acd5-a32fc7e07491",
"metadata": {
"tags": []
},
"source": [
"Credit: https://github.com/fred-fp/manim/blob/main/cm_can.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1589742f-aad8-4d85-8755-aa7e0bcadd0f",
"metadata": {},
"outputs": [],
"source": [
"from manim import *\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "948e1282-b322-43d6-b497-47b59fc17b95",
"metadata": {},
"outputs": [],
"source": [
"config.media_width = \"80%\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2f0659ac-ee25-4045-a7dd-cfeedf10877c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"%%manim -v WARNING -qm Cena1\n",
"\n",
"class Cena1(Scene):\n",
" def construct(self):\n",
"\n",
" #define initial surface level and the masses of both liquids\n",
" surface_level = ValueTracker(3.0)\n",
" mass_liquid_1 = 1\n",
" mass_liquid_2 = 20\n",
"\n",
" #create components of system 1\n",
" can_1 = Rectangle(color=WHITE, width=3.5, height=6, stroke_width=2).set_x(-4)\n",
" cm_can_1 = Dot(color=YELLOW).scale(0.5).set_x(-4)\n",
" \n",
" liquid_1 = always_redraw(lambda :\n",
" Rectangle(fill_color=BLUE,\n",
" fill_opacity=0.6,\n",
" stroke_width=0,\n",
" width=3.5,\n",
" height=surface_level.get_value()*2).set_y(surface_level.get_value() - 3).set_x(-4)\n",
" )\n",
" cm_liquid_1 = always_redraw(lambda :\n",
" Dot(color=ORANGE).scale(0.5).set_y(surface_level.get_value() - 3).set_x(-4)\n",
" )\n",
"\n",
" cm_total_1 = always_redraw(lambda :\n",
" Dot(color=WHITE).set_y((cm_can_1.get_y() +\n",
" cm_liquid_1.get_y()* (mass_liquid_1 * surface_level.get_value()/3))\n",
" /(1 + (mass_liquid_1 * (surface_level.get_value()/3)))\n",
" ).scale(0.5).set_x(-4)\n",
" )\n",
"\n",
" system_1 = Group(can_1, liquid_1)\n",
" centers_of_mass_1 = Group(cm_can_1, cm_liquid_1, cm_total_1)\n",
"\n",
"\n",
" #create components of system 2\n",
" can_2 = Rectangle(color=WHITE, width=3.5, height=6, stroke_width=2).set_x(4)\n",
" cm_can_2 = Dot(color=YELLOW).scale(0.5).set_x(4)\n",
" \n",
" liquid_2 = always_redraw(lambda :\n",
" Rectangle(fill_color=BLUE,\n",
" fill_opacity=0.6,\n",
" stroke_width=0,\n",
" width=3.5,\n",
" height=surface_level.get_value()*2).set_y(surface_level.get_value() - 3).set_x(4)\n",
" )\n",
" cm_liquid_2 = always_redraw(lambda :\n",
" Dot(color=ORANGE).scale(0.5).set_y(surface_level.get_value() - 3).set_x(4)\n",
" )\n",
"\n",
" cm_total_2 = always_redraw(lambda :\n",
" Dot(color=WHITE).set_y((cm_can_2.get_y() +\n",
" cm_liquid_2.get_y()* (mass_liquid_2 * surface_level.get_value()/3))\n",
" /(1 + (mass_liquid_2 * (surface_level.get_value()/3)))\n",
" ).scale(0.5).set_x(4)\n",
" )\n",
"\n",
" system_2 = Group(can_2, liquid_2)\n",
" centers_of_mass_2 = Group(cm_can_2, cm_liquid_2, cm_total_2)\n",
"\n",
"\n",
"\n",
"\n",
" #text\n",
" desc_cm_can = Tex(\"CM of can\", color=YELLOW).shift(UP)\n",
" desc_cm_liquid = Tex(\"CM of liquid\", color=ORANGE).shift(DOWN)\n",
" desc_cm_total = Tex(\"CM of system\", color=WHITE)\n",
"\n",
" desc = Group(desc_cm_can, desc_cm_liquid, desc_cm_total)\n",
"\n",
" desc_liquid_1 = MathTex(\"m_{liquid} = \", mass_liquid_1, \"m_{can}\").scale(0.7).shift(LEFT*4+UP*3.5)\n",
" desc_liquid_2 = MathTex(\"m_{liquid} = \", mass_liquid_2, \"m_{can}\").scale(0.7).shift(RIGHT*4+UP*3.5)\n",
"\n",
" #animate\n",
" self.wait(0.5)\n",
" self.play(FadeIn(system_1),\n",
" FadeIn(system_2)\n",
" )\n",
" self.play(FadeIn(desc),\n",
" FadeIn(centers_of_mass_1),\n",
" FadeIn(centers_of_mass_2),\n",
" FadeIn(desc_liquid_1),\n",
" FadeIn(desc_liquid_2)\n",
" )\n",
" self.play(surface_level.animate.set_value(0.), run_time = 7)\n",
" self.wait(1)\n",
" self.play(\n",
" *[FadeOut(mob)for mob in self.mobjects]\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "12f0dc3c-ba5f-4456-875c-fea854c1e06f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
FROM manimcommunity/manim:v0.10.0
COPY --chown=manimuser:manimuser . /manim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment