diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst index 8399f9d..befc604 100644 --- a/docs/source/changelog.rst +++ b/docs/source/changelog.rst @@ -2,6 +2,13 @@ Changelog ========= +**v0.4.1** +========== +Bugfix +------ +- `#34 `_ : Magnetic fields + now accept multiple wires + **v0.4.0** ========== Breaking Changes diff --git a/manim_physics/electromagnetism/magnetostatics.py b/manim_physics/electromagnetism/magnetostatics.py index 2224b05..a27570b 100644 --- a/manim_physics/electromagnetism/magnetostatics.py +++ b/manim_physics/electromagnetism/magnetostatics.py @@ -101,11 +101,12 @@ def _field_func( currents: Iterable[float], ): B_field = np.zeros(3) - for (r0, r1), I in it.product(*dls, currents): - dl = r1 - r0 - r = p - r0 - dist = np.linalg.norm(r) - if dist < 0.1: - return np.zeros(3) - B_field += np.cross(dl, r) * I / dist**4 + for dl in dls: + for (r0, r1), I in it.product(dl, currents): + dr = r1 - r0 + r = p - r0 + dist = np.linalg.norm(r) + if dist < 0.1: + return np.zeros(3) + B_field += np.cross(dr, r) * I / dist**4 return B_field diff --git a/tests/control_data/electromagnetism/magnetic_field_multiple_wires.npz b/tests/control_data/electromagnetism/magnetic_field_multiple_wires.npz new file mode 100644 index 0000000..f19ee3f Binary files /dev/null and b/tests/control_data/electromagnetism/magnetic_field_multiple_wires.npz differ diff --git a/tests/test_electromagnetism.py b/tests/test_electromagnetism.py index 4c6867e..ff8c867 100644 --- a/tests/test_electromagnetism.py +++ b/tests/test_electromagnetism.py @@ -22,3 +22,12 @@ def test_magnetic_field(scene): wire = Wire(Circle(2).rotate(PI / 2, UP)) field = MagneticField(wire) scene.add(field, wire) + + +@frames_comparison(base_scene=ThreeDScene) +def test_magnetic_field_multiple_wires(scene): + wire1 = Wire(Circle(2).rotate(PI / 2, RIGHT).shift(UP * 2)) + wire2 = Wire(Circle(2).rotate(PI / 2, RIGHT).shift(UP * -2)) + mag_field = MagneticField(wire1, wire2) + scene.set_camera_orientation(PI / 3, PI / 4) + scene.add(wire1, wire2, mag_field)