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 1/21/2020 to reflect new functionality and information.*

### 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,

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 *μ*, as given by the following:

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

The technique demonstrated here shows how each component of the magnetic field *Hx, Hy, Hz*] can be mapped to an auxiliary field by adding an extra equation that has three unknown variables (one scalar field for each component of *Lagrange element*. Since both first- and second-order spatial derivatives are defined on this kind of element, it now becomes possible to obtain the spatial gradients of the

Note that 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.

### Step-by-Step Tutorial Using a Helmholtz Coil

Let’s look at an example where we can demonstrate the technique introduced above. We will show how you can compute and plot the spatial derivatives of the magnetic field 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.

This model uses the *Magnetic Fields* physics interface and the *Coil* feature to model the two coils. Each coil has 10 turns and a 0.25-mA current circulating through it.

*The magnetic field norm and the magnetic field direction in the Helmholtz coil.*

#### Plotting the Gradients of the Magnetic Field

We will start with the solved example from the Application Gallery and see how we can determine the derivatives of the magnetic field.

The first step is to map the magnetic field on Lagrange elements. For that, we add the *Coefficient Form PDE* interface with three dependent variables (one per component), which uses the Lagrange element by default. Here, we also set the appropriate units for the dependent variables.

*Settings of the *Coefficient Form PDE* interface.*

The second step is to correctly set up the coefficients of the PDE, in order to get the appropriate expression for mapping the field components.

*Configuration of the *Coefficient Form PDE* coefficients.*

We set all coefficients other than “a” and “

Where a = 1,

As a consequence, when solving for this *Coefficient Form PDE*, the three components of the magnetic field are directly mapped onto the three scalar fields u1, u2, and u3, as follows:

u1 = mf.Hx

u2 = mf.Hy

u3 = mf.Hz

Concerning the study setup, we first compute only the magnetic fields problem in Study 1, and in the second study, we solve for the variables u1, u2, and u3. In order to take the values from the electromagnetic solution, we set the feature *Values of variables not solved for* using the study settings shown below.

*Configuration of Study 2, with user-defined setting for *Values of variables not solved for*.*

It should be noted that the components of the magnetic field do not map perfectly onto the Lagrange elements, causing a small discrepancy between the two (even at the mesh nodes). It is a mapping, not an identity. Therefore, it is a good practice to check abs(

In order to compute the gradients of the magnetic field, we append to the variable name the spatial direction in which we want to compute the partial derivative. These suffixes for the spatial derivatives are available for all degrees of freedom, and come directly from the shape functions. For example, u1x would compute the gradient of the x-component of the magnetic field with respect to the x direction.

*Plot of the gradient (with respect to the x direction) of the x-component of the magnetic field.*

*The y-component of the magnetic field and its gradient (with respect to the y direction) 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. To download the MPH-file, you need to be logged into a COMSOL Access account and have a valid software license.

Learn more about the features and functionality of COMSOL Multiphysics and the add-on AC/DC Module:

## Comments (8)

## Tuan-Anh Le

July 2, 2016But when I checked it, I see u1 and mf.x is different. Plz expain it. Thank you

## Tuan-Anh Le

July 2, 2016But when I checked it, I see u1 and mf.Hx is different. Plz expain it. Thank you

## Rahmetullah Cagil

October 2, 2017I applied a similar procedure for magnetic flux density and just like Tuan-Anh, I see that mf.Bx and u1 are different, they are almost identical in shape but u1 is scaled down by three orders of magnitude. What could be the cause for this error?

## Tijani Aziz Feki

October 10, 2017Hi all

I did all steps about mf.Bx but the results from second study is different from what we can obtain from first study, what is the reason and how should I fix this problem?

thanks.

## Caty Fairclough

October 13, 2017Hi Tuan-Anh, Rahmetullah, and Tijani

Thanks for your comments!

For your questions, please contact our Support team.

Online Support Center: https://www.comsol.com/support

Email: support@comsol.com

## kai zhang

November 9, 2017We alse can select the “dode” interface and set “f=u1-mf.Hx” and so on. It seemed this way will be more accurate for “u1” in the result for the transient study, such as “ecore transformer.mph” in the library.

## Simon Houis

August 7, 2018Hello,

Thank you very much for this interesting post. I managed to compute the gradient of a stationary study (1 single current applied). Now I would like to do the same but for a frequency sweep. I computed a model over 10 frequencies and I ‘d like now to compute spatial gradients for each frequency. The idea is vizualize on a single plot the magn gradient over x for all frequencies. Nonetheless when I applied the method you described, I am only able to vizualise results for the last frequency and not all of them.

Could you tell me how to proceed to compute gradient for all frequencies?

Thanks in advance for your help.

Regards.

## Caty Fairclough

August 7, 2018Hi Simon,

Thanks for your interest in this blog post!

For your questions, I would suggest contacting our Support team.

Online Support Center: https://www.comsol.com/support

Email: support@comsol.com