Magnetic field map** is commonly used in many fields of science, medicine and technology such as particle accelerators, nuclear storage experiments1,2,3, cardiac beat detection4, magnetic resonance imaging (MRI)5 and magnetic indoor positioning systems (IPS)6,7. For example, in nuclear and particle physics experiments, with one example being the search for the neutron electric dipole moment, it is often crucial to measure and control the magnetic field components in the experimental region, because these experiments are typically sensitive to perturbations in magnetic fields. An undetected disturbance in a magnetic field may result in systematic uncertainties and cause a limitation for the precision of the measured quantities. To minimize systematic uncertainties, magnetic field components should be monitored in real-time and any unwanted field should be compensated during the operation time of the experiment. Real-time measurement of the magnetic field in an experimental region of space is not always practical or feasible. In most cases, the experimental region is not accessible due to a physical enclosure (e.g., a setup placed in a vacuum chamber), or it could be that placing a magnetic field sensor inside the experimental region is too disruptive to the system.

There exist several approaches in the literature that can be utilized to solve the problems stated above. For instance, Solin et al.8 make use of Gaussian processes (GPs) to interpolate/extrapolate ambient magnetic fields. They train the model using a data set collected by a magnetic field sensor at different locations of space and reconstruct the whole ambient magnetic field. Another method is proposed by Nouri et al.9,10. They introduced a non-disruptive magnetic field map** method using exterior measurements at fixed locations and leverage the multipole expansion of the magnetic field vector. Expanding the magnetic field to some finite degree \(n=N\), they provide a systematic way to optimize sensor locations and fit the unknown coefficients of the multipole expansion using the data from those exterior sensor measurements. This method is susceptible to noise in the data and, since the multipole expansion terms need to be picked to a specific field profile, the coefficients of the expansion terms are not regularized.

In this paper, we propose a robust way of predicting the magnetic field vector in the experimental region. In order to accomplish this, we utilize physics-informed neural networks (PINNs)11. PINNs propose a way to incorporate prior physical knowledge about the system in terms of its partial differential equations, into the deep neural networks while still being able to utilize their universal function approximator property. With PINNs, data and mathematical models of physics are combined in a smooth way, even in situations that are only partially understood, uncertain, and have a lot of dimensions. In noisy and high-dimensional situations, physics-informed learning blends data and mathematical models easily and can solve general inverse problems extremely successfully12,13,14. Unlike the method proposed in Refs.9,10, our method does not require prior knowledge of the multiple expansion terms to be fitted. This special type of neural network regularizes the output function (magnetic field prediction) during the training process by requiring the output to satisfy Maxwell’s equations, specifically Ampere’s law and Gauss’ law for magnetism.

Methods

In this work, we are interested in predicting the magnetic field components inside a three dimensional space enclosed by an external surface S by utilizing the knowledge of the magnetic field in some number of locations on the surface S. Assuming there are no free currents, \(\varvec{J}=0\), and no magnetization, \(\varvec{M}=0\), in the region of interest, the partial differential equations that govern the static magnetic field are quite concise. They are

$$\begin{aligned} \varvec{\nabla \cdot B}= 0, \end{aligned}$$
(1)

and

$$\begin{aligned} \varvec{\nabla \times B}= 0. \end{aligned}$$
(2)

Therefore, it is possible to find a magnetic field that satisfies (1) and (2) together with the knowledge of the magnetic field at some number of locations . We choose those locations on the surface of a closed region S of which we are interested in approximating the magnetic field inside (Fig. 1). Of course, according to the electromagnetism uniqueness theorem, having a finite number of data on a surface does not guarantee a unique solution to equations (1) and (2). However, as we demonstrate later in this paper, our results indicate that one can successfully approximate the true solution by having a sufficient number of data scattered on the surface.

Review of the multipole expansion method

In this section, we review the field monitoring method described in Ref.9. Equation (2) indicates that the magnetic field vector can be written as a gradient of a scalar magnetic potential function

$$\begin{aligned} \varvec{B}= -\nabla \Phi _M(\varvec{r}). \end{aligned}$$
(3)

Substituting (3) into Eq. (1) tells us that the magnetic scalar potential satisfies Laplace’s equation

$$\begin{aligned} \nabla ^2\Phi _M=0, \end{aligned}$$
(4)

and the solution of Laplace’s equation in spherical coordinates is given by

$$\begin{aligned} \Phi _M(r,\theta ,\phi ) = \sum _l^\infty \sum _m^l r^l P^m_l(\cos \theta ) [a_{lm}\cos (m\phi )+b_{lm}\sin (m\phi )], \end{aligned}$$
(5)

where \(P_l^m\) are the associated Legendre polynomials, and \(a_{lm}\) and \(b_{lm}\) are expansion coefficients. The magnetic field can be obtained by calculating the gradient of the magnetic scalar potential, \(\varvec{B}= -\nabla \Phi _M(\varvec{r})\). Absorbing \(a_{lm}\) and \(b_{lm}\) in a coefficient \(c_n\), we can write the magnetic field in a compact form

$$\begin{aligned} \varvec{B}(x,y,z) = \sum _n c_n \varvec{f}_n(x,y,z), \end{aligned}$$
(6)

where \(\varvec{f}_n(x,y,z)\) are vector basis functions satisfying \(\nabla \cdot \varvec{f}_n = 0\) and \(\nabla \times \varvec{f}_n = 0\).

Table 1 Table of the xy and z components of the basis vector function \(\varvec{f}_n\) up to order \(n=9\).

To illustrate, the first 10 \(\varvec{f}_n\) basis vector functions are listed in Table 1. The right-hand side of the Eq. (6) is expanded to some finite order \(n=N\) and the magnetic field vector inside the volume can be interpolated using linear regression techniques.

Magnetic field prediction using PINNs

Figure 1
figure 1

Magnetic field sensors (red dots) placed on a surface S to predict the magnetic field \(\varvec{B}\) in the inner region.

The exact values of the partial derivatives in (1) and (2) can be calculated by automatic differentiation11, which is implemented in some well-known machine learning libraries such as TensorFlow15 and PyTorch16. The neural network we train to approximate the magnetic field inside the region will have the structure as shown in Fig. 2. The hyperbolic tangent is used for the activation of each hidden layer. The other tested activation functions have not performed as well as the hyperbolic tangent for this network architecture. The number of hidden layers are chosen to be 4 and 8 with each having 32 or 64 neurons. The performance of these 4 different-sized networks are discussed later.

Figure 2
figure 2

Network takes 3 inputs, (xyz) coordinates, and outputs the magnetic field \(\varvec{B}\). Automatic differentiation is used to calculate the exact derivatives of the output \(\varvec{B}\) with respect to the input parameters.

Then, the network can be trained by a combined loss function of data, curl and divergence losses

$$\begin{aligned} \text {Loss} = \text {Loss}_{\varvec{B}} + \lambda (\text {Loss}_{\varvec{\nabla \cdot B}} + \text {Loss}_{\varvec{\nabla \times B}}), \end{aligned}$$
(7)

where

$$\begin{aligned}&\text {Loss}_{\varvec{B}} := \frac{1}{N_{\varvec{B}}} \sum _{i=1}^{N_{\varvec{B}}} \Vert \varvec{B}(\varvec{r}_{\varvec{B}}^{i})-\varvec{B}_{s}(\varvec{r}_{\varvec{B}}^{i})\Vert ^2, \end{aligned}$$
(8)
$$\begin{aligned}&\text {Loss}_{\varvec{\nabla \cdot B}} := \frac{1}{N_f} \sum _{i=1}^{N_f} |\varvec{\nabla \cdot B}(\varvec{r}_{d}^{i})|^2, \end{aligned}$$
(9)

and

$$\begin{aligned} \text {Loss}_{\varvec{\nabla \times B}} := \frac{1}{N_f} \sum _{i=1}^{N_f} \Vert \varvec{\nabla \times B}(\varvec{r}_{d}^{i})\Vert ^2, \end{aligned}$$
(10)

where the points \(\varvec{r}_{\varvec{B}}^{i}\) and \(\varvec{r}_{d}^{i}\) denote the positions of the magnetic sensors and the collocation points, respectively. \(N_{\varvec{B}}\) is the number of the magnetic field sensors, \(N_f\) is the number of collocation points in the domain and \(\varvec{B}_{s}\) is the measured magnetic field vector at \(\varvec{r}_{d}^{i}\). The parameter \(\lambda\) in Eq. (7) can be adjusted according to the performance of the network. The collocation points, \(\varvec{r}_{d}^{i}\), in Eqs. (9) and (10) are sampled from the volume encapsulated by the surface S (Fig. 1) and can be chosen to be fixed throughout the training process11. However, randomly choosing collocation points in each epoch leads to a quicker convergence as well as more accurate results. This is partly due to being able to choose fewer number of collocation points, and since they are assigned randomly each iteration, they represent the domain better than any fixed collocation points scheme. The ADAM optimizer17, an adaptive method for gradient-based first-order optimization, is what we make use of in order to minimize the loss function 7. The general procedure for training is given in Algorithm 1.

figure a

Experiments

Simulated experiment

In the following example, we will demonstrate the capability of our magnetic field prediction model by placing an arbitrary number of triple-axis magnetic sensors on the surface of a cube. Magnetic field sensors will be placed on the cube randomly and we will generate training and validation data by using Biot–Savart law for circular current loop(s). In the next section, we will give the analytical expression of the three dimensional magnetic field vector of a single circular current loop and then we will construct a higher order asymmetric magnetic field by placing multiple loops with different currents to benchmark our method on.

We begin by demonstrating the ability of our magnetic field reconstruction method by considering the magnetic field of a simple circular current loop (in arbitrary units). The magnetic field components of a circular current loop with radius a are given by18,19

$$\begin{aligned} \begin{aligned} B_x&= C\frac{xz}{2\alpha ^2 \rho ^2 \beta }\left[ (a^2 + r^2)E(k^2) - \alpha ^2 K(k^2)\right] \\ B_y&= \frac{y}{x}B_x\\ B_z&= C\frac{1}{2\alpha ^2 \beta }\left[ (a^2-r^2)E(k^2) + \alpha ^2 K(k^2)\right] \end{aligned} \end{aligned}$$
(11)

with

$$\begin{aligned} k^2 = \frac{4ar\sin \theta }{a^2+r^2+2ar\sin \theta }, \end{aligned}$$

where E(k) and K(k) are elliptic integrals, \(\rho ^2 \equiv x^2 + y^2\) , \(\alpha ^2 \equiv a^2 + r^2 -2a\rho\), \(\beta ^2 \equiv a^2 + r^2 +2 a \rho\) and \(r\equiv \sqrt{x^2+y^2+z^2}\) and \(z = r\cos {\theta }\). In this work, we will work in arbitrary units by setting \(C = 1\).

Figure 3
figure 3

Configuration of the test model. Red circles: current loops, blue cube: cubical sensor array and red dot: triple-axis magnetic field sensors.

We want to show the potential of the network by comparing it to the multipole expansion method for various sensor counts and different types and levels of noises. To create a non-uniform higher order magnetic field, we positioned 8 circular loops of different current values at positions \((x=\pm 1.01, y=\pm 1,z=\pm 4)\) and the triple-axis magnetic sensors are placed randomly on the surface of a cube with side length \(L=2\) centered at the origin. The configuration is illustrated in Fig. 3. Our goal is to predict the magnetic field in the inner region of the surface.

The number of hidden layers and neurons of the network characterizes the complexity of the function it can approximate. Having more hidden layers and neurons should not negatively affect the performance, training larger networks are slower and may require more care with initialization and regularization of the weights20. In this example, larger network sizes resulted in better performance as shown in Table 2 as expected. Models were trained for less than 2 min for all cases on an NVIDIA RTX 3080 GPU.

Table 2 Error between the predicted and exact magnetic field in vector norm for various sensor counts and network structures.
Table 3 Error measure in vector norm for the predicted and the other method’s different orders for various sensor counts.
Figure 4
figure 4

Top: Magnitude plot of the predicted magnetic field along with exact magnetic field and the error at snapshot \(z=0.72\), middle: comparison of the predicted and the other method’s solutions’ xyz directions with 18 sensor data, bottom: comparison of the predicted and the other method’s solutions’ xyz directions with 30 sensor data.

Greater sensor counts gives more information about the magnetic field of the system, and we would expect the network to be able to use that information to predict magnetic field better. As shown in Table 2, having more sensory information has led to a better performance for all network structures. Moreover, fewer sensor counts has not led to a divergence from the exact magnetic field. This is not the case with the multipole expansion method as shown in Table 3. The other method seems to suffer with relatively few sensors and higher order versions overfit the sensor data. Decreasing the order in this case leads to better results but due to lower orders having fewer basis functions, the method is not able to predict the exact magnetic field as well as our network. This can also be seen in Figs. 4 and 5.

Figure 5
figure 5

Top: Comparison of the predicted and the other method’s solutions’ xyz directions with 18 sensor data with Gaussian noise with \(\sigma =1.0\times 10^{-2}\), Bottom: Comparison of the predicted and the other method’s solutions’ xyz directions with 30 sensor data with Gaussian noise with \(\sigma =1.0\times 10^{-2}\).

Performance of the network when Gaussian noise is introduced to the sensory information is given in Table 4. This noise has led to a further deterioration of the performance for the multipole expansion method. Our method has also been affected, however, performed better across various sensor counts.

Table 4 Error measure in vector norm for the predicted and the other method for various sensor counts with Gaussian noise added to the sensor data with standard deviations \(\sigma =5.0\times 10^{-3}\) and \(\sigma =1.0\times 10^{-2}\).

Map** the magnetic field of a square coil system

To demonstrate our methodology using actual data, we conducted an experiment in which a Bartington triple axis magnetic field probe (Mag-13MS1000) was moved to the locations of the training data collection points. In order to generate a non-uniform magnetic field, two rectangular coils are stacked vertically and driven with different current magnitudes in the opposite directions (Fig. 6). Each face of the coils is a printed circuit board (PCB) with dimensions 55 cm \(\times\) 16 cm containing 50 parallel line traces along the long side of the PCB. A current with magnitude 1 A flows counterclockwise and another current with magnitude 0.6 A flows clockwise in the top and bottom coils respectively.

In order to isolate the field generated by the coils, at each measurement location, the data is collected as the difference between the sensor measurements with the coil turned on and off. Then, we trained the network for the magnetic field data collected using the magnetic map** system. The training domain is chosen as a cube with 40 cm side length placed at the center of the coils. Performance benchmarks of our network and the other method on the measurement data is given in Figs. 7 and 8.

Figure 6
figure 6

Setup of the experiment: Left: 3D magnetic field map** system is sitting underneath the rectangular coils. Right: The triple-axis magnetometer and inside the coils.

Figure 7
figure 7

Comparison of the predicted and the other method’s solutions along \(x =10\), \(y=10\) axis. Top: 18 sensor data, bottom: 30 sensor data.

Figure 8
figure 8

Comparison of the predicted and the other method’s solutions along \(y =10\), \(z=10\) axis. Top: 18 sensor data, bottom: 30 sensor data.

Conclusions

In this study, we presented an efficient and practical method for map** the magnetic field of inaccessible locations. We encoded previous knowledge from Maxwell’s equations for magnetostatics into a physics-informed neural network model for magnetic field prediction in regions where direct measurements are not possible.

We provided two experiments that proved the practicability of the proposed method. A simulated experiment proved the value of incorporating extra physics knowledge into the model. Map** the magnetic field of a square coil system illustrated the effectiveness of the approximation technique in real world applications.

Our method compared with the multipole expansion method indicated better performance results across various sensor counts and noise levels both in simulation data and real world measurement data.