Introduction

In many applications like magnetic sensors, magnetic storage, electric machines and so on, magnetic fields with specific spatial profiles are exploited in order to achieve a specific goal. The properties of magnetic fields are defined by the properties of the involved magnetic structures. Most commonly, in order to obtain a certain magnetic field, the geometry of a magnetic structure is optimized. This can e.g. be the geometry of a permanent magnet producing a well defined magnetic field, or a soft magnetic structure shaping the field possibly generated by an electric current. The process of optimizing the geometry of these structures can be very time consuming. A common approach is called geometry parameterization where the structure’s geometry is parameterized with relatively few parameters and the parameter region is investigated until an optimal solution is found. Geometry parameterization is one representative of a group of techniques summarized as shape optimization1. Another approach is to directly optimize the distribution of material within a given domain. This approach is called topology optimization2. It drastically increases the degrees of freedom (dofs) but allows for possibly better, geometric realizations of the considered system, independent of any introduced parameterizations. Successful applications of topology optimization of magnetic structures include the optimization of magnetic recording write heads3, of rotors of brushless DC motors4 and of electromagnetic actuators5,6.

In this paper a method to optimize the topology of magnetic structures is presented. The presented method is based on a highly efficient hybrid finite element–boundary element method (FEM–BEM) approach7 solving the magnetostatic Maxwell’s problem. In contrast to already presented finite element3,8 and finite differences9 algorithms using a FEM–BEM approach has the advantage that only the regions of interest need to be discretized reducing the dofs dramatically. Furthermore, in order to efficiently calculate gradients during optimization, the adjoint approach is utilized. Our method takes the B/H curve of a hard or soft magnetic material linearized at the working point as input and is able to include external fields generated by electric coils. After optimization, the result is an optimized magnetic structure and its stray field dependent magnetization state.

The algorithm is implemented on the basis of magnum.fe10 which in turn utilizes the finite element library FEniCS11. In particular FEniCS is used for the definition of the objective functionals and to automatically differentiate the involved partial derivatives.

The paper is structured as follows. In the first section the solution of the forward problem–the calculation of the stray field of a magnetic structure—is presented. In the following section the density approach to topology optimization is introduced. After that the gradient necessary to perform the optimization and the adjoint equation are introduced. Thereafter details of the numerical optimization are discussed and numerical experiments showcasing the capabilities of the method are presented. A detailed derivation of the gradient using the adjoint approach within a “first optimize then discretize” scheme is given in the appendix.

Forward problem

The stray field of a magnetic body consisting of a magnetic material with linear magnetic susceptibility \(\chi\) and possibly a remanence magnetization \({\varvec{M}}_\text {r}\) within an external magnetic field \({\varvec{H}}_{\text {ext}}\), can be calculated starting from the magnetostatic Maxwell’s equation. Using the reduced scalar potential formulation the total magnetic field \({\varvec{H}}\) is split into an external part \({\varvec{H}}_{\text {ext}}\) (created via currents outside of \(\Omega _{\text {m}}\)) and the curl-free induced magnetic field \({\varvec{H}}_{\text {d}}\). Expressing the induced field as gradient of a scalar potential u, \({\varvec{H}}_{\text {d}} = -\varvec{\nabla }u\) finally yields

$$\begin{aligned} \varvec{\nabla }\cdot \left[ \left( 1+\chi \right) \left( \varvec{\nabla }u - {\varvec{H}}_{\text {ext}}\right) - {\varvec{M}}_\text {r}\right] = 0. \end{aligned}$$
(1)

In order to use the hybrid FEM–BEM approach, next \({\mathbb {R}}^3\) is divided into a region \(\Omega _{\text {m}}\) containing all magnetic material and a region \({\mathbb {R}}^3 {\backslash }\Omega _\text {m}\). Outside \(\Omega _m\) Eq. (1) then reduces to

$$\begin{aligned} \Delta u = 0 \quad \text {in} \quad {\mathbb {R}}^3 {\backslash }\Omega _m \end{aligned}$$
(2)

and the following boundary and jump conditions apply

$$\begin{aligned} \left[ u\right] _{\partial \Omega _{\text {m}}}&= \,0 \end{aligned}$$
(3)
$$\begin{aligned} \left[ \left( 1+\chi \right) \frac{\partial u}{\partial {\varvec{n}}}\right] _{\partial \Omega _{\text {m}}}&= \,\left[ \chi \right] _{\partial \Omega _{\text {m}}}{\varvec{n}}\cdot {\varvec{H}}_{\text {ext}}+ {\varvec{n}}\cdot {\varvec{M}}_{\text {r}} \end{aligned}$$
(4)
$$\begin{aligned}&u\left( {\varvec{r}}\right) \rightarrow {\mathcal {O}}\left( \frac{1}{\left| {\varvec{r}}\right| }\right) \quad \text {for} \quad \left| {\varvec{r}}\right| \rightarrow \infty \text {,} \end{aligned}$$
(5)

where \({\varvec{n}}\) is the outward pointing normal vector and \(\left[ \cdot \right] _{\partial \Omega }\) denotes the jump over the boundary \(\partial \Omega\). The resulting system is solved using a hybrid FEM–BEM algorithm7.

Topology optimization

Topology optimization tries to find, within a predefined spatial domain \(\Omega _{\text {opt}}\), the optimal material distribution with respect to a design goal. This can be achieved using the density approach for topology optimization12. Here, a scalar indicator function

$$\begin{aligned} \rho \left( {\varvec{x}}\right) \in \left[ 0,1\right] \text {,}\quad \text {where}\quad \rho \left( {\varvec{x}}\right) = {\left\{ \begin{array}{ll} 0\text {: no material}\\ 1\text {: material} \end{array}\right. } \end{aligned}$$
(6)

is introduced, transforming the remanence magnetization \({\varvec{M}}_{\text {r}}\rightarrow {\varvec{M}}_{\text {r}}\left( \rho \right) = \rho ^p {\varvec{M}}_{\text {r}}\) as well as the susceptibility \(\chi \rightarrow \chi \left( \rho \right) = \rho ^p\chi\). Here, p is a parameter introduced in order to penalize intermediate values of \(0 \le \rho \le 1\) that are allowed during optimization12. However, it turns out that the impact of p is more complex as is investigated for each presented numerical experiment (see “Numerical experiments” section).

The forward problem then reads

$$\begin{aligned} F\left( u, \rho \right) =\varvec{\nabla }\cdot \left[ \left( 1+\rho ^p\chi \right) \left( \varvec{\nabla }u -{\varvec{H}}_{\text {ext}}\right) - \rho ^p{\varvec{M}}_\text {r}\right] = 0, \end{aligned}$$
(7)

where u is an at least twice and \(\rho\) an at least once differentiable function. Note, that after transformation into its weak form and shifting of the divergence operator onto the test function by application of partial integration, these conditions are relaxed and after discretization piecewise linear basis function \(\left( {\mathcal {P}}_1\right)\) can be used for the scalar potential u and the scalar indicator function \(\rho\) only has to be constant within each element.

The numerical optimization is performed by casting the design goal into an objective functional \({\hat{J}}\left( \rho \right) = J\left( {\varvec{H}}\left( \rho \right) , \rho \right)\) and minimizing it using an iterative, gradient based optimization algorithm.

Adjoint approach

In order to efficiently minimize the reduced objective functional \({\hat{J}}\left( \rho \right)\) containing the design goal, its gradient with respect to the design variable \(\rho\), \(\varvec{\nabla }_{\rho }{\hat{J}}\) is needed. This is where the adjoint approach comes into play, offering a very efficient method to calculate the gradient. As derived in detail in the appendix (“Appendix” section) within a “first optimize then discretize” scheme, the gradient is given by

$$\begin{aligned} \varvec{\nabla }_{\rho }{\hat{J}} = \left( p\rho ^{p-1}\left[ \chi \left( \varvec{\nabla }u-{\varvec{H}}_{\text {ext}}\right) - {\varvec{M}}_{\text {r}}\right] \right) \cdot \varvec{\nabla }\lambda + {\mathcal {R}}\left( \frac{\partial J}{\partial \rho }\right) \text {,} \end{aligned}$$
(8)

where \(\lambda\) is the adjoint variable that is obtained by solving the adjoint equation

$$\begin{aligned} \varvec{\nabla }\cdot \left( \left( 1+\rho ^p\chi \right) \varvec{\nabla }\lambda -{\mathcal {R}}\left( \frac{\partial J}{\partial {\varvec{H}}}\right) \right) = 0 \end{aligned}$$
(9)

with boundary condition

$$\begin{aligned} \lambda \left( {\varvec{r}}\right) \rightarrow {\mathcal {O}}\left( \frac{1}{\left| {\varvec{r}}\right| }\right) \quad \text {for} \quad \left| {\varvec{r}}\right| \rightarrow \infty \text {.} \end{aligned}$$
(10)

Here \({\mathcal {R}}\) are the Riesz representers of the corresponding functionals as introduced in “Appendix” section. Note, that the adjoint equation has the same form as the forward problem. Therefore, the same algorithm can be utilized to solve the forward as well as the adjoint equation.

Numerical experiments

Figure 1
figure 1

Mesh and geometry used for the numerical experiments. Dimensions are given in µm. The thickness (z-dimension) of the thin film is 12.5 µm. The field box \(\Omega _\text {h}\) consists of 91 cells and the optimization region \(\Omega _{\text {opt}}\) consists of 16505 cells.

In the following, the topology of thin film magnetic flux guide concentrators is optimized towards maximization of the vertical (y-)component of the magnetic field \(H_y\) inside the target domain \(\Omega _{\text {h}}\). The objective functional that is to be minimized reads \({\hat{J}} = -\int _{\Omega _{\text {h}}} H_y\,\text {d}V\). The geometry and the mesh used is depicted in Fig. 1 where also the the optimization domain (\(\Omega _{\text {opt}}\)) and the domain in which the magnetic field is evaluated (\(\Omega _{\text {h}}\)) is specified. Note that \(\Omega _{\text {m}} = \Omega _{\text {opt}} \cup \Omega _{\text {h}}\). Note, that we use an irregular mesh in order to avoid any solution bias introduced by a given regularity. The presented numerical experiments include the usage of hard as well as soft magnetic material.

The potential u and the adjoint variable \(\lambda\) are calculated using piecewise linear basis functions \(\left( {\mathcal {P}}_1\right)\) and the scalar indicator function \(\rho\) as well as \(\chi\), \({\varvec{M}}_{\text {r}}\), \({\varvec{H}}_{\text {ext}}\) and the derived strayfield \(-\varvec{\nabla }u\) and \(\varvec{\nabla }\lambda\) are constant within each element.

The optimizations are performed using the Scipy implementation of the limited-memory Broyden-Fletcher-Goldfarb-Shanno minimization algorithm with bounds (L-BFGS-B)13. Note, that as shown in14, common ready-to-use optimization algorithms such as this, are constructed to only receive coefficient vectors as input and internally use the euclidean (\(\ell ^2\)) inner product to perform all necessary operations (e.g. approximation of the Hessian). Since here an optimization in a Hilbert spaces with the \(L^2\) inner product is performed, this leads to mesh dependent convergence rates. To prevent this, there exists the python package Moola15 that internally uses the correct inner product during optimization. However, in order to perform topology optimization bounds on the optimization variable \(\rho\) have to be enforced since \(0 \le \rho \le 1\) and so far, there is no optimization algorithm offered by Moola enabling bounds on the optimization variable. Furthermore, note that the \(L^2\) Riesz representer of the gradient is independent of the flux concentrator’s geometrical dimensions. However, the objective functional is not since it includes and integration over the volume \(\Omega _{\text {h}}\). For the chosen geometry dimensions the gradient size is relatively large compared to the size of the objective functional. This leads to the L-BFGS-B algorithm terminating prematurely with problems during the line search, simply returning the initial distribution of \(\rho\). This is since the Armijo rule, one of the Wolfe conditions necessary to end the line search is never met. The objective functional value exposed to the optimization algorithm, is therefore scaled to avoid this problem. This scaling does not alter the results since internally, the L-BFGS-B algorithm performs a quasi-Newton method on the gradient and the function value is only used during the line search.

For results of optimizations that terminate with dofs of \(\rho\) at intermediate values \(0\le \rho \le 1\) a brute force regularization is applied where all dofs with \(\rho < 0.5\) are set to 0 and all dofs with \(\rho \ge 0.5\) are set to 1.

Permanent magnet optimization

Figure 2
figure 2

Optimized topology for the ideal permanent magnet and comparison with the analytical solution (green line).

Figure 3
figure 3

Dependence of \(n_{\text {fev}}\) on \(\gamma\) and size of the gradient’s smallest dof value. The black horizontal line represents a value of 1.

First, the topology for an ideal permanent magnet with susceptibility \(\chi = 0\), remanence magnetization \(M_{\text {r}} = 280000\) A/m and the anisotropy axis chosen as the y-axis is optimized towards maximization of the vertical (y-)component of the magnetic field \(H_y\) inside the domain \(\Omega _h\), as introduced above.

As presented in8 for a point like target domain, the optimal topology can be derived analytically by considering the magnetization inside \(\Omega _{\text {opt}}\) as being comprised of single dipoles \(\varvec{\mu }\). A dipole at a vertical (y-)distance produces a positive flux \(B_y\left( {\varvec{x}}\right)\) at a point \({\varvec{x}}\) for an horizontal (x-)distance of

$$\begin{aligned} x < \sqrt{2}y\text {.} \end{aligned}$$
(11)

The surface condition \(x=\sqrt{2}y\) therefore shows that the analytical solution is a cone with an opening angle of \(\alpha \approx 35.26^{\circ }\). Note that the assumption of a point like target domain is still valid, since the changes due to the finite size of \(\Omega _{\text {h}}\) are too small to be resolved by the chosen mesh. The analytical solution is shown in Fig. 2, where it is compared to the optimal numerical solution.

For the ideal permanent magnet, the optimized topology, as shown in Fig. 2, produces a mean field \({\bar{H}}_y = \int _{\Omega _{\text {h}}} H_y\,\text {d}V / V_{\Omega _{\text {h}}}\) of 22.83 mT. This solution is found independently of the initial distribution of the density functional \(\rho _{\text {init}}\) for a penalty parameter of \(p\le 4\). Furthermore, scaling the gradient has no influence on the obtained solution, but increasing the gradient size reduces the number of function evaluations \(n_{\text {fev}}\) needed. This is since the size of the gradient determines how fast the dofs of \(\rho\) are set to 0 or 1. This shows, that the optimization problem in the case of zero susceptibility is linear in \(\rho\) as expected since for \(\chi = 0\) the only source term in the forward problem (7) is given by \(\rho ^p{\varvec{M}}_{\text {r}}\) where \({\varvec{M}}_{\text {r}}\) is constant. In Fig. 3 the value of the smallest dof in the gradient and \(n_{\text {fev}}\) are plotted against the gradient scaling factor \(\gamma\) for \(p=1\). As soon as the smallest dof is larger 1 for \(\gamma = 10^3\), all elements are set to 0 or 1 during the optimization, leaving no dofs with intermediate values of \(\rho\) and \(n_{\text {fev}}\) reaches a minimum of 2.

Realistic hard magnetic material \(\chi = 0.2\)

Figure 4
figure 4

Optimized topology for the optimization with \(\chi = 0.2\).

Figure 5
figure 5

Dependence of the number of necessary function evaluations \(n_{\text {fev}}\) on \(\gamma\) with \(p = 1\) for the optimization with \(\chi = 0.2\).

Using a realistic hard magnetic material with a susceptibility of \(\chi = 0.2\), the optimal solution found has a mean field of \({\bar{H}}_y\) of 22.35 mT. This smaller mean field value compared to the ideal permanent magnet is due the effect of demagnetization. The optimal topology found (Fig. 4) differs only slightly from the topology obtained from the ideal permanent magnet optimization.

The optimal solution is found independently of the initial distribution of the density functional \(\rho\) for \(p=1\). For larger p-values the solution gets worse showing, that the optimization is no longer independent of the parameter p. However, for \(p\le 4\) the difference in the mean field is below 0.5%. Scaling the gradient again reduces the number of necessary function evaluations to perform the optimization \(n_{\text {fev}}\). However, since the optimization problem is no longer linear in \(\rho\), the number of function evaluations \(n_{\text {fev}}\) needed is larger than for the ideal permanent magnet case. In Fig. 5\(n_{\text {fev}}\) in dependence of the gradient scaling factor \(\gamma\) for \(p=1\) is shown. The optimization is fastest for a constant initial distribution of \(\rho _{\text {init}} = 1\) where for \(\gamma \ge 10\) \(n_{\text {fev}} < 250\). Also, the optimization process terminates before all dofs of \(\rho\) are set to 0 or 1, although the number of dofs with an intermediate value of \(\rho\) is negligible for all simulations with \(p=1\). It has been verified that no better solution is found by scaling the gradient down to \(\gamma = 10^{-5}\). Note, that the topology obtained from the optimization using an ideal permanent magnetic material, for a realistic hard magnetic material with \(\chi = 0.2\) produces a magnetic field that is only negligibly smaller (<0.1‰) than the optimal topology obtained using the realistic hard magnetic material during optimization. This confirms, that \(\chi = 0\) is a good assumption for a permanent magnetic structure.

Optimization of a soft magnetic structure

Table 1 Mean field values \(\bar{H}_y\) and increase with respect to the full plate for the optimized topologies.

Next, the topology of a soft magnetic thin film is optimized to act as a magnetic flux guide concentrator maximizing the magnetic flux in vertical (y-)direction inside \(\Omega _{\text {h}}\). Such a structure can be used to e.g. improve the magnetic field detection limits of spin valve sensors16 by ehancing the external field at the sensor position.

The optimization is performed for three

Figure 6
figure 6

Optimized topologies before (top row) and after regularization (bottom row) for (a) \(\chi = 10^2\) (for \(p = 5\)) (b) \(\chi = 10^3\) (for \(p = 14\)) and (c) \(\chi = 10^4\) (for \(p = 17\)) after regularization for \(\gamma = 10^{-4}\).

different values of \(\chi\) of \(10^2\), \(10^3\) and \(10^4\) with an external field of 10 mT applied. The optimized topologies for the three different \(\chi\)-values are shown in Fig. 6. The found structures show the same “tanga” shape as reported in16 as optimal design. While the optimal topology differs only slightly for \(\chi = 10^3\) and \(\chi = 10^4\), for \(\chi = 10^2\) the topology is significantly broader. In fact, switching the optimal topologies of \(\chi = 10^3\) and \(\chi = 10^4\) changes the generated mean field by under \(0.2\%\). The generated mean field and the increase with respect to the full plate (\(\rho =1\) in \(\Omega _{\text {opt}}\)) is shown in Table 1.

As opposed to the optimization of hard magnetic materials, the optimization using a soft magnetic material depends much more on the initial distribution of the density functional \(\rho _{\text {init}}\) as well as on the p-parameter. In Fig. 7 the p-dependence of the mean field \({\bar{H}}_y\) for the regularized solutions for \(\chi =10^3\) is plotted for different initial distributions of \(\rho _{\text {init}}\) and for different values of the gradient scaling factor \(\gamma\).

The solution found for a constant \(\rho _{\text {init}} = 1\) and a randomly initialized \(\rho _{\text {init}}\) are clearly inferior to the solutions found with a constant \(\rho _{\text {init}} = 0.5\). For \(\rho _{\text {init}} = 0.5\) the mean field increases with increasing p until it plateaus. While decreasing \(\gamma\) does not lead to a higher mean field, the p-value at which the mean field plateaus decreases with decreasing \(\gamma\). At \(\gamma = 1\) the plateau starts at \(p = 26\) while for \(\gamma = 10^{-4}\) the plateau value is 14. Further decreasing \(\gamma\) does not alter the plateau value anymore. In fact, as shown in Fig. 8 also for single p-values, the mean field increases with decreasing \(\gamma\) until a maximum is found for \(10^{-4}\). Note, that for higher p-values than the plateau value, the mean field differs well under 1% from the mean field obtained by the optimization performed with the plateau value, except for individual outliers with very small mean field values for large p-values.

Figure 7
figure 7

p-dependency for different initial distributions and gradient scaling factors \(\gamma\) for the optimization performed for \(\chi = 10^3\).

Figure 8
figure 8

Dependency of the mean field \({\bar{H}}_y\) on \(\gamma\) individual p-values.

Figure 9
figure 9

p-dependency of \(n_{\text {fev}}\) and the percentage of dofs with intermediate values of \(\rho\) after optimization for values of \(\gamma\) for the optimization performed for \(\chi = 10^3\).

Figure 10
figure 10

Iteration histories for selected values of p for a gradient scaling factor of \(10^{-4}\).

In Fig. 9, the amount of dofs with intermediate values of \(\rho\) after optimization is plotted in dependence of p. While the solutions with \(p \le 4\) show no dofs with intermediate values of \(\rho\), with increasing p, their number increases and reaches over 60% for the optimal solutions. This is in contrast to the original purpose of the penalization factor p. However, since the changes of the mean field \({\bar{H}}_y\) due to regularization is under 1% for all optimizations, the effect of the regularization is still negligible. This is since the remaining intermediate values are very close to 0 and 1 respectively. In Fig. 6 the optimized topologies are shown before and after regularization for comparison.

The dependence of the number of function evaluations necessary to perform the optimizations \(n_{\text {fev}}\) on p is plotted in Fig. 9 for two different values of the gradient scaling factor. It can be seen \(n_{\text {fev}}\) is lower with a smaller gradient scaling factor. With a gradient scaling factor of \(10^{-4}\), the optimization with \(p=14\) needs 325 \(n_{\text {fev}}\) while with a gradient scaling factor of 1 for the optimization with \(p=26\) \(n_{\text {fev}} = 14631\). In Fig. 10 the iteration histories are plotted for selected values of p for a gradient scaling factor of \(10^{-4}\). Note that the different starting values of the optimizations are due to factorization of \(\rho\) by p.

For \(\chi = 10^4\) the p-dependence of the mean field plateaus in the same way as for \(\chi = 10^3\), although with a different plateau value of \(p=17\) for a gradient scaling factor of \(10^{-4}\). The dependency on the gradient scaling factor is also similar as shown in Fig. 8. For \(\chi = 100\) the situation is slightly different. Here, for gradient scaling factors smaller \(10^{-4}\) the mean field increases with increasing p-value until a maximum is found at \(p=5\) and the mean field decreases when further increasing p.

Conclusion

An algorithm to optimize the topology of magnetic structures based on a hybrid FEM–BEM method and the density approach for topology optimization has been presented. The gradients necessary to efficiently solve the optimization are obtained very efficiently using the adjoint approach. It has been shown that within a “first optimize then discretize” scheme the continuous forward operator is self-adjoint and therefore, the same method can be used to solve the forward as well as the adjoint problem.

The dependence on the p-parameter, originally introduced to penalize intermediate values of the density indicator function \(0\le \rho \le 1\) has been investigated for the presented numerical experiments. For ideal permanent magnetic structures (\(\chi = 0\)) the linearity of the optimization problem can be utilized by enlarging the gradient. Scaling the gradient by a factor \(>10^{3}\), with \(p=1\) the optimization needs only two function evaluations and the found solution is in accordance with analytical expectations and shows no intermediate values of the indicator function \(\rho\). For a realistic permanent magnetic material with \(\chi = 0.2\) the optimized topology produces a magnetic field that differs by \(<0.1\)‰ from the topology found for the ideal permanent magnetic material. This shows that for thin films with negligible demagnetization, \(\chi = 0\) is good approximation for a realistic permanent magnetic material with \(\chi = 0.2\). Furthermore, the number of function evaluation needed using a realistic hard magnetic material is in the hundreds and hence much larger than the number of evaluations needed for the assumption of an ideal permanent magnetic material.

For the optimization of soft magnetic materials the optimal solutions are found for a gradient scaling factor \(\le 10^{-4}\). The solutions found get better with increasing value of the p-parameter. For \(\chi =10^2\) the best solution is found for \(p=5\) and gets worse for larger p-values. For \(\chi = 10^3\) and \(\chi =10^4\) the solutions improve with p until it plateaus for \(p\ge 14\) and \(p\ge 17\) respectively. Note, that in contrast to its original purpose, the p-parameter does not decrease the number of dofs with intermediate values of \(\rho\). On the contrary, the percentage of dofs with intermediate values of \(\rho\) increases with increasing p. However, using a brute force regularization forcing all dofs with \(\rho < 0.5\) to 0 and all dofs with \(\rho \ge 0.5\) to 1 only negligibly impacts the field value created by the structures. The optimized topologies have a shape similar to the optimal shape found experimentally in16.