Matheart / manim-physics

Physics simulation plugin of Manim that can generate scenes in various branches of Physics.
325 stars 30 forks source link

Homogeneous magnetic field? #11

Open Myzel394 opened 3 years ago

Myzel394 commented 3 years ago

So I'm currently trying to implement a homogeneous magnetic field. However, I can't get it to work. And basically my question is: Is it even possible to create a homogeneous magnetic field using this library?

The closest thing I could get is this (even tough I needed to remove this if-statement in electromagnetism.py, btw, why did you add it?):

from manim import *
from manim_physics import *

class HorseShoeMagnet(VGroup):
    def __init__(
        self,
        width: float = 6,
        height: float = 6,
        bar_width: Optional[float] = None,
        bar_height: Optional[float] = None,
        field_x_padding: Optional[float] = None,
        field_y_padding: Optional[float] = None,
        field_density: float = 2,
        magnets: list[BarMagnet] = None,
        create_bar: bool = True,
        **kwargs
    ):
        magnets = magnets or []
        field_x_padding = field_x_padding or .3
        field_y_padding = field_y_padding or .3
        bar_width = bar_width or 1
        bar_height = bar_height or 1

        field_width = width - bar_width
        field_height = height - (bar_height * 2)

        bar_magnets = [*magnets]
        surrounding_rectangle = Rectangle(width=0, height=height)

        # Ensures that there is a field
        self.bar_magnet = BarMagnet(width=width, height=height * 5)

        if create_bar:
            bar_magnets.insert(0, self.bar_magnet)

        self.field = BarMagneticField(
            *bar_magnets,
            x_range=[
                -(field_width / 2),
                (field_width / 2 - field_x_padding),
                1 / field_density
            ],
            y_range=[
                -(field_height / 2 - field_y_padding),
                (field_height / 2 - field_y_padding),
                1 / field_density
            ]
        )

        self.north_rectangle = Rectangle(
            width=field_width,
            height=bar_height,
            fill_opacity=1,
            color=RED,
            stroke_width=0,
        )\
            .align_to(surrounding_rectangle, DOWN)
        self.north_side_wall = Rectangle(
            width=bar_width,
            height=height / 2,
            fill_opacity=1,
            color=RED,
            stroke_width=0,
        )\
            .align_to(self.north_rectangle, RIGHT + DOWN)\
            .shift(RIGHT)
        self.north_label = Tex("N").next_to(self.north_rectangle, ORIGIN)
        self.north_parts = VGroup(
            self.north_rectangle,
            self.north_side_wall,
        )

        self.south_rectangle = Rectangle(
            width=field_width,
            height=bar_height,
            fill_opacity=1,
            color=BLUE,
            stroke_width=0,
        )\
            .align_to(surrounding_rectangle, UP)
        self.south_side_wall = Rectangle(
            width=bar_width,
            height=height / 2,
            fill_opacity=1,
            color=BLUE,
            stroke_width=0,
        )\
            .align_to(self.south_rectangle, RIGHT + UP)\
            .shift(RIGHT)
        self.south_label = Tex("S").next_to(self.south_rectangle, ORIGIN)
        self.south_parts = VGroup(
            self.south_rectangle,
            self.south_side_wall,
        )

        super().__init__(**kwargs)
        self.add(
            self.field,
            VGroup(
                self.north_parts,
                self.south_parts,
            ),
            self.north_label,
            self.south_label
        )
        self.add(*magnets, self.bar_magnet.set_opacity(.2))

class FirstFormulaExplainedScene(Scene):
    def construct(self):
        magnet = HorseShoeMagnet(field_density=8, magnets=[BarMagnet().scale(.1)])

        self.add(magnet)
Myzel394 commented 3 years ago

You can see the latest updates of this example here

icedcoffeeee commented 2 years ago

So I'm currently trying to implement a homogeneous magnetic field. However, I can't get it to work. And basically my question is: Is it even possible to create a homogeneous magnetic field using this library?

I'm not familiar with the term homogeneous magnetic fields, but I assume you mean a magnetic field of constant magnitude and direction. To be fair, I didn't have such a usage in mind.

I needed to remove this if-statement in electromagnetism.py, btw, why did you add it?)

Honestly, I don't remember. I'll try to keep this issue in mind and refactor most of magnetism objects.