  # Plotting Spatial Derivatives of the Magnetic Field

March 5, 2014

Computing the spatial derivative of the magnetic field or magnetic flux density is useful in areas such as radiology, magnetophoresis, particle accelerators, and geophysics. One of the most important use cases is the design of magnetic resonance imaging (MRI) machines, where it is necessary to analyze not only the field strength but also the spatial variation of the field. Today’s blog post demonstrates how to compute and plot the gradients of the magnetic field in electromagnetic simulations in the COMSOL Multiphysics® software.

Editor’s note: This blog post was updated on 11/24/2020 to reflect new functionality and information.

### Spatial Derivatives: Background and Objective

When you are computing magnetic fields induced by currents, computing electric fields induced by time-varying magnetic fields, or solving 3D electromagnetic wave problems, COMSOL Multiphysics uses so-called curl (vector or Nédélec edge) elements in all three cases. The curl element is also used in 2D and 2D axisymmetric magnetic field simulations that involve in-plane current distributions. Within the AC/DC Module, the curl element is typically used to compute the magnetic vector potential, A. However, the higher-order spatial derivatives are not available for the curl element.

Check out the blog post What Is the Curl Element (and Why Is It Used)? for an elaborate introduction to the curl element.

The magnetic flux density B, magnetic vector potential A, and magnetic field H are related through the magnetic permeability μ, as given by the following:

\textbf{B} = \nabla \times \textbf{A}\\
\textbf{B} = \mu\textbf{H}

The equations show that the magnetic flux density and the magnetic field are functions of the first-order spatial derivative of the magnetic vector potential. Since the second-order spatial derivative is not defined on curl elements, the gradients of B and H cannot be derived by using the differentiation operator (d(f,x)) directly.

In order to compute the second-order spatial derivative, the B or H field defined on curl elements is usually mapped to a field represented by Lagrange elements. Thanks to the operator laginterp(order, expr) introduced in COMSOL Multiphysics version 5.6, it is more convenient to do this. The operator maps an expression to a Lagrange field of a specified order and then evaluates on that field in each mesh element.

When solving for 2D or 2D-axisymmetric magnetic field problems involving out-of-plane currents, or static magnetic field problems without any current flow within the model, the Lagrange elements are used to solve the governing equations, which makes the second-order spatial derivative available. The method shown in this blog post applies only to cases where the curl elements are used to compute the fields.

When solving the magnetic field with the Magnetic Fields, Currents Only interface (available as of version 5.6), which uses the Lagrange element as the shape function for the dependent variable, the d(f,x) operator can be used directly.

### An Example: Helmholtz Coil

Let’s look at an example where we can demonstrate the usage of differentiation operator introduced above. We will show how you can compute and plot the spatial derivatives of the magnetic flux density produced by a Helmholtz coil. A detailed description of this model, along with step-by-step instructions to simulate the coil, can be found in the Application Gallery.

A Helmholtz coil is a pair of parallel circular coils separated by a distance equal to one coil radius where the current through both the coils flows in the same direction. Some known uses of this kind of configuration are canceling Earth’s magnetic field and generating controlled magnetic fields for experiments. The geometry of the Helmholtz coil is shown in the figure below. In this model, each coil has 10 turns and a 0.25-mA current circulating through it. The geometry of the Helmholtz coil. The outermost sphere region is modeled with the Infinite Element.

This model demonstrates two different approaches to compute the magnetic flux density and its spatial derivative. The first approach uses the Magnetic Fields physics interface and the Coil feature to model the two coils. The Magnetic Fields interface uses the curl element to define and represent the magnetic flux density in 3D. For example, to compute the gradient of the y-component of the magnetic flux density (Byy), we use the expression  d(laginterp(2,mf.By),y).

The second approach uses the Magnetic Fields, Currents Only physics interface, as there are no magnetic materials in the model. The coils are modeled with the Conductor feature. Since this interface uses the Lagrange shape function, we can use the expression d(mfco.By,y) to compute Byy. The comparisons of By and Byy along the centerline of the Helmholtz coil are shown in the figures below. As can be seen, the value of  Byy in each mesh element is constant, since both of the interfaces use quadratic elements for the magnetic vector potential A. The y-component of the magnetic flux density along the centerline of the Helmholtz coil. The gradient (with respect to the y direction) of the y-component of the magnetic flux density along the centerline of the Helmholtz coil.

### Next Steps

Download the Magnetic Field of a Helmholtz Coil model by clicking the button below, which will take you to the Application Gallery.