Magnetically Modelling MagSafe

MagSafe Magnets? How do they work?

After writing my last post on MagSafe, I wanted to go deeper into the magnet array. What was it that had me so fascinated? It has this property, best described in video:

I’d never seen this property of self alignment before and as my powers of imagination were failing me regarding how this all worked, it was time for a model..

What? No no, wrong model (Photo credit)

I’m (of course) referring to creating a magnetic model of the MagSafe magnet arrays, moving them around and seeing the magnetic fields and forces to infer what is going on. To start, I’d need to find some software to help me and that’s when I came across this wonderful Python package called Magpylib:

There really is a Python package for everything

Setting up the MagSafe Array

Digging once again into Apple’s Accessory Design Guidelines (Release R21, p148), I took the measurements from the drawings, fired up Google Colab and built this model:

3D, fun

Looking at the arrays edge-on, here are some pretty field lines:

White and black boxes show the position of the upper and lower MagSafe arrays and if we zoom into the red box and animate the upper array moving right:

Squiggly wiggly field lines

Now that all these cool visualizations were done, I assumed that it’d be pretty simple to calculate the forces. My assumption was wholly incorrect. There’s no native way in magpylib to calculate forces! Instead of waiting for this issue to be resolved, I looked for another solution. magforce looked promising but I couldn’t get it to work. Time to discard the accurate model..

Maths time, yay

So with no easy way to do it, time to go back to the physics textbook and find an equation:

Oh no, way too complicated

Ah, much better!

If I simplify (using rotational symmetry) from a fancy, three-dimensional model to a 2D model and reduce the magnet dimensions to idealized “point” magnets, I can use that equation to find the forces.

This isn’t the best because it assumes the magnets are now hypothetical “point” magnets called magnetic dipoles. However, the model only needs to be good enough to show the self-centering property I saw in the intro video. This is kavi’s blog after all, not Reviews of Modern Physics!

Skipping over a bunch of Python and a helpful YouTube video, out pops this animation:

Eureka, I can see the forces now! The red arrow is the one to look at. You can clearly see the force on the array pointing right when the upper array is slightly offset to the left and vice versa. That shows the forces responsible for that centering action. The black arrows show the forces on each dipole and the green arrow shows the torque direction.

Enter p5.js

Python was cool and all, but the animation method I used was janky as hell. I wanted something a little more interactive. That’s when I found the awesome p5.js and it’s incredibly cool live editor. After porting over the Python code to Javascript:

The array can be moved with the mouse or the sliders and the force lines are updated dynamically.

I can’t have all the fun myself though..

You can play with this too!

To play around with the p5.js animation, click here (only works on desktop browsers, sorry!)

To play with the Python versions (and all the source code used for making the models and animations) get them from GitHub here.


Clearly, this way of arranging magnets has some cool properties. Modelling the magnet array with visualized forces really shows the interplay between the magnets’ repulsion and attraction and how it leads to the unique MagSafe attach experience:

If you find other interesting properties that the model describes that you can replicate with physical devices, share it with me!

Speaking of the layout of the magnets, it would be incredibly weird if no-one else has come up with this array before. That said, Apple does have this patent that appears to cover the MagSafe array for use in inductive charging. While digging, I also found this Apple patent that allows for “bimodal” devices to be built. Maybe we’ll see devices that can attach to “A” or “B” devices soon! (see Magnet section here for reference). Perhaps a new USB-C MagSafe battery pack that can also be charged using MagSafe? You heard it here first folks.

The Future

With magpylib hopefully getting force calculations in the future, it would be cool to model the forces generated by the full 3D model. It would also be awesome to play around more with p5.js and add in proper physics simulation to the 2D version. Also, why not try modelling a “bimodal” device…

If you have any questions/would like to share your experience with MagSafe, Python, JavaScript or the modelling of magnetic fields, leave a reply below. To receive an email when I publish a post, subscribe here.

(Featured image remixed from this photo)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑