Floating Magnets#
Magpylib’s efficient field and force computations make it ideal for dynamic simulations that require solving equations of motion through time-discretization methods. This example demonstrates how to implement numerical integration schemes to simulate the motion of magnetic objects under electromagnetic forces and torques.
Mathematical Formalism#
The dynamics of magnetic objects are governed by the classical equations of motion: \(\vec{F} = \dot{\vec{p}}\) for translation (with force \(\vec{F}\) and momentum \(\vec{p}\)) and \(\vec{T} = \dot{\vec{L}}\) for rotation (with torque \(\vec{T}\) and angular momentum \(\vec{L}\)).
For numerical integration, we implement a first-order semi-implicit Euler method—a robust algorithm commonly used for planetary motion and other dynamic systems. This method discretizes time into small steps \(\Delta t\) and updates the system state sequentially.
Translation dynamics: For position \(\vec{s}\), velocity \(\vec{v} = \dot{\vec{s}}\), and mass \(m\):
Rotational dynamics: For orientation angle \(\vec{\varphi}\), angular velocity \(\vec{\omega}\), and inertia tensor \(J\):
The semi-implicit nature (velocity updated before position) provides better numerical stability compared to explicit methods, making it well-suited for magnetic dynamics where forces can vary rapidly with distance.
Magnet Accelerated by Coil#
This implementation demonstrates the proposed Euler scheme using a simplified scenario where cubical magnets are accelerated along the z-axis by a current loop, as shown in the following sketch:
A cubical magnet is accelerated by a current loop.#
Due to the axial symmetry of this configuration, the net torque on the magnet is zero, allowing us to solve only the translational equations of motion while ignoring rotational dynamics.
The simulation starts with two magnets (opposite magnetization) at rest, positioned slightly above the current loop center along the z-axis. As time progresses, magnetic forces accelerate the magnets, and their z-positions are computed and visualized to demonstrate the different behaviors based on magnetic moment orientation.
Key observations:
The simulation compares two magnets with opposite polarizations. In the repulsive case (orange), the magnetic moments of the magnet and coil are antiparallel, causing the magnet to be pushed away from the coil in the positive z-direction with monotonic acceleration. In the attractive case (blue), the moments are parallel, initially accelerating the magnet toward the coil center. Due to inertia, the magnet overshoots and emerges on the opposite side, where it is again attracted back toward the center, resulting in oscillatory motion around the equilibrium position.
Warning
This numerical algorithm accumulates discretization errors over time. For higher accuracy or longer simulations, use smaller timesteps or higher-order integration methods.
Two-Body Problem#
In the following example we demonstrate a fully dynamic simulation with two magnetic bodies that rotate around each other, attracted towards each other by the magnetic force, and repelled by the centrifugal force.
Two freely moving magnets rotate around each other.#
Contrary to the simple case above, we apply the Euler scheme also to the rotation degrees of freedom, as the magnets will change their orientation while they circle around each other. Magnet positions and orientations are computed and visualized.
Key observations:
In the figure one can see, that the initial velocity is chosen so that the magnets approach each other in a ringdown-like behavior. The magnets are magnetically locked towards each other - both always show the same orientation. However, given no initial angular velocity, the rotation angle is oscillating several times while circling once.
A video is helpful in this case to understand what is going on. From the computation above, we build the following gif making use of this export-animation tutorial.
Animation of above simulated magnet ringdown.#
Note
Keep in mind that this simulation accounts only for magnetic forces. Time dependent magnetic fields induce eddy currents in conductive media which are not considered here.