{'fraction_non_alphanumeric': 0.034447542024345135, 'fraction_numerical': 0.010958072262552099, 'mean_word_length': 4.9209021081876125, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Optimal Inverse Design of Magnetic Field Profiles in a Magnetically Shielded Cylinder

Magnetic shields that use both active and passive components to enable the generation of a tailored low-field environment are required for many applications in science, engineering, and medical imaging. Until now, accurate field nulling, or field generation, has only been possible over a small fraction of the overall volume of the shield. This is due to the interaction between the active field-generating components and the surrounding high-permeability passive shielding material. In this paper, we formulate the interaction between an arbitrary static current flow on a cylinder and an exterior closed high-permeability cylinder. We modify the Green’s function for the magnetic vector potential and match boundary conditions on the shield’s interior surface to calculate the total magnetic field generated by the system. We cast this formulation into an inverse optimization problem to design active–passive magnetic field shaping systems that accurately generate any physical static magnetic field in the interior of a closed cylindrical passive shield. We illustrate this method by designing hybrid systems that generate a range of magnetic field profiles to high accuracy over large interior volumes, and simulate them in real-world shields whose passive components have finite permeability, thickness, and axial entry holes. Our optimization procedure can be adapted to design active–passive magnetic field shaping systems that accurately generate any physical user-specified static magnetic field in the interior of a closed cylindrical shield of any length, enabling the development and miniaturization of systems that require accurate magnetic shielding and control.

Romeo and Hoult first laid the mathematical framework for the design of magnetic fields in free space through the use of current loops and arcs [25]. These simple elements were expanded in a spherical harmonic basis and unwanted field profiles removed by adjusting the relative separations of the discrete coils. Inverse methods based on formulating a current density in terms of simple triangular boundary elements [26][27][28] enabled the design of coils of arbitrary complexity, giving greater flexibility in the design of systems at the cost of computational power and numerical instability. Pseudo-analytical formulations using a Green's function expansion and harmonic minimization methods have been developed to generate accurate user-specified magnetic fields in free space [29][30][31] to facilitate the need for fast and accurate design procedures. However, these methods did not incorporate the interaction of high-permeability passive shielding material, hindering the accurate generation of specified target magnetic field profiles [32] in shielded environments. Consequently, optimization of magnetic field cancellation, or other field shaping, systems in the presence of a material with high magnetic permeability is a long-standing challenge in electromagnetism.
Finite element methods (FEMs) can be used to develop models of hybridized active and passive shielding systems. One method of optimizing active-passive systems using FEMs would be to use genetic algorithms [33,34] to evolve the coil parameters iteratively, including the effect of the passive material on the magnetic field generated actively at each iteration. Due to their computational complexity, however, FEMs can be slow, and, if coupled with forward optimization procedures provide only locally optimal designs since desired magnetic field profiles depend highly non-linearly on the system parameters. Analytical formulations of the magnetic field generated by hybrid active-passive systems have the distinct advantage that optimal solutions can be calculated at a range of target points with minimal computation [35]. Currently, analytical solutions for coils in high-permeability cylindrical magnetic shields have only been formulated in the specific cases of magnetically-shielded solenoids and discrete current loops [36][37][38]. The geometric parameters of the active structure in these systems, such as the separation distances of wire loops, are adapted to account for the interaction between the active and passive systems. Previously, the method of mirror images [39] has been used to formulate the response of a planar material with high magnetic permeability to a magnetic field generated by a current source [40][41][42][43]. In these formulations, Maxwell's equations are solved explicitly by including the reflections of the current source generated by the high-permeability material in order to match the required boundary conditions. More generally, Green's function solutions to boundary value problems have been calculated for an extensive range of electromagnetic systems [44], but have not previously been found generally for the case of a finite closed cylindrical high-permeability shield in the presence of an arbitrary cylindrical coaxial static current source.
To address this, here, we incorporate ab initio the effect of a finite length high-permeability cylinder on the magnetic field generated by an arbitrary static current flow on a conducting cylinder, and use our results to determine the flow required to generate specified static target fields optimally. This enables the geometry of the active components on the surface of a cylinder to be determined entirely from a desired magnetic field profile. Guided by [45], we first derive a Green's function for a hybrid active-passive field-generating system described in cylindrical coordinates. We then utilize a modified Fourier basis to define an adjusted current density distribution which accounts for the effect of the high-permeability material. From this, we determine globally-optimal current density maps using a quadratic optimization procedure, akin to magnetic field design methodologies used previously in Magnetic Resonance Imaging (MRI) [46,47]. To construct practical current sources from these current density maps, we define the streamfunction of the current continuum and discretize it into a set of closed-loop current-carrying wire geometries. Using this formulation, we present two example coil designs optimized in the interior of a closed cylindrical magnetic shield of finite length and high permeability, µ r 1, and show that our analytical model agrees well with FEM simulations performed for the optimized current flow patterns. We then show that our design methodology can be used in a real-world situation where the cylindrical magnetic shield has finite thickness and permeability as well as axial entry holes in the end caps to allow experimental access. Using this formulation, we transform the performance of systems designed to generate user-specified magnetic field profiles in the magnetostatic regime, reducing the amount of high-permeability material required, and find globally-optimal solutions required for practical, low Size, Weight, Power, and Cost (SWaP-C) technologies for the applications listed above.

II. THEORY
The interface conditions for a magnetic field along a boundary, S, between two materials are that the normal component of the magnetic field, B, and tangential component of the magnetic field strength, H, are continuous. Considering the boundary between air and a material, working in the magnetostatic regime, where no eddy currents are induced, and in the case where no surface currents are present, these interface conditions are and wheren is the unit vector normal to the boundary, and µ r is the relative permeability of the material. In free space, the magnetic field is related to the magnetic field strength and the magnetization, M, by Physically, the magnetization of the sub-domains of a material change in response to an applied magnetic field to satisfy the boundary condition, (2), at the material's surface. Here, we choose to formulate this response in terms of a pseudo-current density, J, confined to the surface of the material, which relates to the curl of the magnetization, The magnetic field strength resulting from a current source, J c , can be expressed using Ampère's law Substituting (4) and (5) into the curl of (3), noting B = ∇ ∧ A where A is the vector potential, results in the Poisson equation in free space, As shown in many papers and textbooks [39], the solution to the Poisson equation, for an arbitrary current density, J, is given by where G(r, r ) is the associated Green's function.
Let us now use the boundary condition on the magnetic field, (2), to examine the effect of high-permeability material on the magnetic field generated by a specific current distribution. Consider a hollow high-permeability, µ r → ∞, cylinder of radius, ρ s , length, L s , thickness, d, with high-permeability planar end caps located at z = ±L s /2 that is surrounded by free space (Fig. 1). An arbitrary static current flows over an internal open coaxial cylinder of radius, ρ c < ρ s , and length, materials, such as mumetal, can have µ r values >100,000 times that of air, the magnetic field must abruptly change direction at the boundary between its surface and air to satisfy the boundary conditions (1)- (2). When the shield is of sufficient thickness, the tangential components of the magnetic field at its boundary must be approximately zero. The boundary condition, (2), on the interior surface of the exterior closed cylinder for the example depicted in Fig. 1, where the magnetic field, B = B ρρ + B φφ + B zẑ , is expressed in cylindrical polar coordinates. Previously, it has been found that the response of a high-permeability material deviates from that of a perfect magnetic conductor on the scale of O µ −1 r [48,49]. Therefore, we may assume for high-permeability materials, such as mumetal, the response can be approximated to that of a perfect magnetic conductor without introducing significant errors.
Due to the symmetries of the system it is natural to work in cylindrical coordinates. Following the formulation of Turner [45], we express the vector potential (7) due to an arbitrary current distribution, J = J ρ (r )ρ+J φ (r )φ+J z (r )ẑ, Since the current has been restricted to flow over an interior cylindrical shell centred radially about the origin, its radial components may be set to zero, resulting in the simplified vector potentials Substituting the Green's function solution from (7) in cylindrical coordinates [39], into (11)- (13), the components of the vector potential may be expressed as where ρ is the radius of the cylinder and ρ > , ρ < is either ρ, ρ if ρ > ρ or ρ , ρ if ρ < ρ , respectively. Equations (15)- (17) contain Fourier transforms of the current densities, with their corresponding inverse transforms given by As a result of this formulation, we can now combine methods for matching the boundary conditions at the radial interface, akin to the formulation of passive screening of magnetic field gradients for MRI [50], with the method of mirror images, accounting for the effect of the planar end caps, to determine the effect of the high-permeability material on the overall magnetic field profile. Due to the cylindrical symmetry of the system, the radial boundary condition may be satisfied by matching the azimuthal Fourier modes in the magnetic field, generated by the cylindrical current source, through the use of a pseudo-current density induced on an infinite cylinder. As the planar end caps are spatially orthogonal to the pseudo-current generated by the infinite cylinder, any image current formed by applying the method of mirror images continues to satisfy the radial boundary condition. Consequently, we can decouple the boundary conditions on the high-permeability cylinder and at the planar end cap boundaries. This must be done sequentially to generate mirror images of the pseudo-current density induced on the high-permeability cylindrical shell and, hence, satisfy the boundary conditions over the entire domain of the closed finite high-permeability cylinder. As a result, using (15)- (17) and the usual formulation of the method of mirror images with two infinite parallel planes, depicted in Fig. 2, the vector potential in the region ρ c ≤ ρ ≤ ρ s , including the effect of the high-permeability cylinder, may be written in terms of an infinite summation over the reflected image currents, where are the Fourier transforms of the p th reflected image current and J mp φ,z (k) is the Fourier transform of the p th reflected pseudo-current induced on the high-permeability cylinder. Fig. 2 depicts how azimuthal, J p φ (φ , z ), and axial, J p z (φ , z ), image currents are generated by two parallel planar perfect magnetic conductors.
Schematic diagram showing how azimuthal (left) and axial (right) currents are reflected by two infinite parallel planes (hatched) of infinite extension and permeability (i.e. perfect magnetic conductors) located at z = ±Ls/2. (left) Azimuthal currents located in the z = d plane are translated, by the reflection process, to the z = (−1) p d + pLs planes. (right) Axial currents flowing from z = d1 to z = d2 are reflected to positions z = (−1) p d1 + pLs and z = (−1) p d2 + pLs, respectively, with odd reflections reversing the initial current flow direction. Black circles show cross-sections of wires carrying current into (red crosses) and out of (red circles) the page.
Writing the magnetic field in cylindrical coordinates, the boundary conditions, (8), on the magnetic field at ρ = ρ s are Substituting (22)-(24) into (28)- (29), the Fourier transformed pseudo-current density on the cylindrical wall of the high-permeability cylinder is found to be where I m (z) and K m (z) are the derivatives with respect to z of I m (z) and K m (z), respectively. Using the continuity equation, (20)- (24), and (30), the magnetic field interior to the conducting cylinder, for all points ρ < ρ c , is given by . In order to determine the magnetic field generated by an arbitrary cylindrical current source, we must construct an orthogonal basis defined on a finite cylinder, which accounts for the mirror images. To do this we use a modified Fourier basis, defining the p th reflected azimuthal current to be in which where H(x) is the Heaviside function and (W n0 , W nm , Q nm ) are Fourier coefficients to be determined. Substituting (34) into (31)-(33), we derive a set of governing equations which relate the magnetic field to the set of weighted Fourier coefficients, where the functions F n (ρ, z), G w,q nm (ρ, φ, z), H w,q nm (ρ, φ, z), D n (ρ, z), and S w,q nm (ρ, φ, z) are defined in Appendix A. Having determined the magnetic field produced by an arbitrary cylindrical current, we may now use an inverse method to solve the system of governing equations, (37)- (39), to determine the unknown Fourier coefficients (W n0 , W nm , Q nm ) for a specified target magnetic field. Following work done by Carlson et al. [46], this may be done by a least squares minimization with the addition of a penalty term to regularize the problem. This regularization term may take many forms, with individual contributions to it representing, for example, the curvature of a given wire geometry, the power consumption, or any other physical parameter that depends quadratically on the geometry of the coil. In this work we focus on the overall power dissipated in the cylindrical current flow, but this choice is somewhat arbitrary since all of the regularization parameters act to achieve the same general goal. If the regularization term is large, the result is a well-conditioned inverse problem that yields a simple current flow, but reduced field fidelity. On the other hand, if the regularization term is small, then the result is a less well conditioned inverse problem that yields a more intricate pattern of current flow but a higher-fidelity magnetic field. The power dissipation in the conducting cylinder of thickness, t, and resistivity, , is given by which, when integrated over the surface of the cylinder, using the continuity equation and (34), gives We now construct a cost function using a least squares optimization procedure, evaluated at K target field points, where β is a weighting parameter chosen such that the physical parameters may be adjusted to achieve specified physical constraints. The minimization is achieved by taking the derivative of the cost function with respect to the Fourier coefficients, allowing the optimal Fourier coefficients to be found by matrix inversion for any physically attainable target magnetic field. The current density is then related, through the continuity equation, to the streamfunction on the inner conducting cylindrical surface.
Ideally, the Fourier series would have an infinite number of coefficients. However, truncating the series at a large finite number of terms still provides accurate solutions for sufficiently regularized problems. This is because higher-order terms contribute less at distances much larger than their spatial period. When designing fields in regions close to the conducting surface, if a sufficiently large number of coefficients is chosen, the real-world field fidelity is limited by the accuracy to which the current continuum can be approximated by discretized wire configurations, rather than by the number of Fourier coefficients. Typically, setting N=200 and equating M to the degree of the desired field harmonic is adequate. An approximate solution to the current continuum is then found by discretizing the streamfunction into N ϕ values, where ϕ j = min ϕ + (j − 1/2)∆ϕ, j = 1, ..., N ϕ , separated by ∆ϕ = max ϕ−min ϕ

Nϕ
. The number of contours should be maximized according to the constraints in manufacturing because the accuracy of the model depends on the approximation of the continuum. For situations where only coarse discretization is possible, we recommend that a form of discrete optimization is used instead. When physically manufacturing these structures, the distance of the wires from the high-permeability material is important. Without the passive shield, one can determine how accurately the discrete coil represents the current continuum by using the elemental Biot-Savart law to calculate the error as N ϕ is adjusted.

III. RESULTS
We now analyze our theoretical model by designing and testing hybrid active-passive magnetic field-generating systems. Regarding the validation of our calculations, we first note that, as expected from previous work [36][37][38] and shown in the Supplementary Material, our calculations confirm that the optimal coil design for generating a constant axial field inside a closed cylindrical perfect magnetic shield is a perfect solenoid that runs along the full length of the cylindrical shield.
In Fig. 3 and Fig. 4 respectively, we show active-passive systems for generating a constant transverse field, B x , normal to the axis of the cylinder, and a linear transverse field gradient, B = (zx + xẑ), along the axis of the cylinder. Each of these systems has a cylindrical surface of length L c = 0.95 m and radius ρ c = 0.245 m, which carries the coil current distribution. This current distribution is interior to and centred about the origin of a closed perfect magnetically conducting cylinder of length L s = 1 m and radius ρ s = 0.25 m. The field is optimized over the central cylindrical region spanning half the radius and length of the coil cylinder. Fig. 3a and Fig. 4a show the respective streamfunctions on the surface of the cylinder and their discretization into wire patterns. The magnetic fields shown in Fig. 3b-c and Fig. 4b are calculated in three ways: analytically, using our theoretical model in (37)-(39); numerically, using COMSOL Multiphysics R with the shield treated as a perfect magnetic conductor; and numerically in free space, i.e. excluding the high-permeability material and calculating the magnetic fields by applying the elemental Biot-Savart law directly to the discrete coil geometries in Fig. 3a and Fig. 4a. It is clear from Figs. 3-4 that our design methodology is capable of generating highly accurate user-specified target magnetic fields inside the optimized field region with good agreement between the theoretical model and numerical simulations.
We quantify this agreement by analyzing the deviation from the target fields in the optimization region, ∆B x and ∆dB x /dz, for the constant transverse field and linear transverse field gradient systems, respectively. Specifically, along the z-axis of the optimized region the maximum absolute deviations from the target fields are 0.11% and 0.24%, respectively. Over the same region, the maximum absolute deviations between the numerically-simulated and analytically-calculated field profiles are 0.002% and 0.003%, respectively. We can also see the hybrid nature of our optimization by the improved performance of the active systems when inside, and coupled to, the passive shield. For example, the strength of the B x field is nearly doubled, and its uniformity is improved by a factor of 20, when the high-permeability cylinder is added to the constant transverse field-generating system (see Fig. 3b).
In Fig. 5 we show the numerically-calculated color maps of the B x field in the y-z plane generated by the constant transverse field (Fig. 3a) and linear transverse field gradient (Fig. 4a) systems, using COMSOL Multiphysics R with the shield treated as a perfect magnetic conductor. We summarize the performance of both systems in Table I, calculating the cylindrical shield fractions -the ratio of the radial and axial extent of the central region to that of the passive shield -where the maximum deviations from the target fields are less than 0.01%, 0.05%, 0.1%, 0.5%, 1%, and 5%, respectively.
When physically constructing active-passive structures for real-world experiments, additional limitations must be taken into consideration in order to generate accurate magnetic fields using our design methodology. These limitations originate either from the theoretical model itself or from experimental practicalities. The limitations in the theoretical model are primarily associated with how accurately the high-permeability cylinder approximates a perfect magnetic conductor. This depends on the value of the finite permeability, the thickness of the shielding material, and the required experimental access holes in the shielding system. The errors introduced by these parameters depend on the lengths, radii, and positions of the conducting and high-permeability cylinders relative to the location of the optimization region. The experimental limitations on the field fidelity relate to the stability of the experimental equipment and errors in manufacturing an accurate representation of the current continuum. These errors include coarse discretization of the current continuum, inexact wire placement and construction, and imprecise positioning of the active structure inside the high-permeability shield. In practice, highly stable experimental equipment is available, particularly power supplies and current drivers [51], and it is possible to manufacture structures that approximate the current continuum accurately [45]. Consequently, the error introduced in the formulation of our model must be calculated to determine the accuracy of any future potential designs in a real-world experimental set-up. Our active-passive systems must, therefore, be analyzed for the case of a high-permeability cylinder that is not a closed perfect magnetic conductor. To do this, we use COMSOL Multiphysics R working in the magnetostatic regime, to determine how the uniformity of the B x field generated by the constant transverse field-generating system in Fig. 3a changes when the perfect closed magnetic conductor is replaced by one that is imperfect. To construct an imperfect shield, we define the magnetic permeability µ r = 20000 to match the permeability of industrial standard mumetal regularly used as a passive shield. We then vary the wall thickness, d, of a closed magnetic shield and determine how small d can be while maintaining high field uniformity. Finally, using this minimum thickness, we introduce a circular axial  (37)-(39) (solid red curve); numerically using COMSOL Multiphysics R Version 5.3a and modelling the high-permeability cylinder as a perfect magnetic conductor (blue dotted curve); numerically without the high-permeability cylinder and using the Biot-Savart law with Nϕ = 100 contour levels (dashed green). (c) Enlarged section of (b) emphasizing the high level of field uniformity and the agreement between the numerical and analytical results over the optimization region. entry hole of radius ρ h at the center of each end cap and determine how large the hole can be to preserve field uniformity.
We use the root mean square field deviation, ∆B RMS x , of the attained field from the uniform target field, calculated along the z-axis of the optimized field region, to evaluate the performance of the active-passive system. The light blue crosses in Fig. 6a show that ∆B RMS FIG. 5. Color maps showing the magnitude of the transverse magnetic field, Bx, in the y-z plane inside a closed finite length perfect magnetic conductor generated by two active-passive systems: (a) the constant transverse field-generating system depicted in Fig. 3; (b) the linear transverse gradient field-generating system depicted in Fig. 4. The field profiles were calculated numerically using COMSOL Multiphysics R Version 5.3a. Contours show where the field deviates from the target field by 1% (solid curves), 0.1% (dashed curves), 0.01% (dot-dashed curves), and 0.001% (dotted curves; in (a) only).
this analytical limit and approach the difference O(µ −1 r ) ≈ 0.005% [48,49] that we predicted for our model in Section II. This intrinsic error, resulting from the small difference between thick high-permeability materials and a  Fig. 3a and Fig. 4a, respectively. The magnetic field deviations were calculated numerically using COMSOL Multiphysics R Version 5.3a in two ways: inside a perfect magnetic conductor (perfect case); inside a magnetic shield with finite permeability µr = 20000, thickness d = 1 mm, and a circular entry hole of normalized radius, ρ h = 0.25ρs in both end caps (imperfect case).
perfect magnetic conductor, sets the hard limit on the accuracy of any magnetic field that can by designed using our theoretical model. In reality, however, this limit is so small that for a thick material with a high permeability, such as mumetal, the errors in manufacturing and construction will be much more significant. As technologies advance which reduce these system errors, such as screen-printed foldable PCBs [52] and 3D-printing technologies [53], it may become more relevant to develop a model which accounts ab initio for magnetic shields of finite permeability and thickness.
We see from Fig. 6a that the asymptotic limit is reached at approximately d = 1 mm, where ∆B RMS x = 0.019%. At this point, regarding the accuracy of our model, there is little advantage to increasing d further. Consequently, in Fig. 6b we take d = 1 mm and examine the effect of introducing circular axial entry holes in both end caps of the high-permeability cylinder. Although ∆B RMS x increases as the hole radius, ρ h , increases from no hole, ρ h = 0, to when there is no end cap, ρ h = ρ s , we see that small holes in both end caps allow experimental access without significantly reducing the fidelity of the desired field profile. In particular, for our system, the hole radius can be made as large as ρ h = 0.25ρ s while only increasing ∆B RMS x by 0.0012% when compared to the no hole case (horizontal dashed light blue line).
In Fig. 7 we show the numerically-calculated color maps of the B x field in the y-z plane generated by the constant transverse field (Fig. 3a) and linear transverse field gradient (Fig. 4a) systems. Both of these systems are simulated with the same imperfect high-permeability cylindrical magnetic shield that has had its properties determined in the above analysis: µ r = 20000, d = 1 mm, and ρ h = 0.25ρ s . The performance of both systems in terms of the cylindrical shield fraction is summarized in Table I. Finally, we see from Table I that this imperfect magnetic shield does not introduce significant magnetic field deviations above 0.1% when compared to a perfect shield with the same geometry. In particular, the maximum difference between the perfect and imperfect cylindrical shield fractions for deviations above 0.1% is only 0.028. Large deviations in the field accuracy below 0.1% can be graphically seen when comparing the color maps for the perfect (Fig. 5) and imperfect (Fig. 7) cases. The contours showing field deviations of 0.01% and 0.001% are strongly perturbed, as expected from the analysis in Fig. 6, demonstrating the hard intrinsic limit on our model when generating target field profiles inside real-world magnetic shields. Similar analysis should be applied when designing other active-passive systems using our theoretical model in order to quantify its accuracy for a specific experimental setup. Further analysis could also be performed to determine the low-frequency limit in which a time-dependent current source could be included. However, if the magnitudes of any induced eddy currents are much less than the magnitude of the coil current, such effects will be negligible. Further designs generating more exotic field profiles can be found in the Supplementary Material. All designs can be found in our open access Python code which can be used to design systems to generate specific physical target field using our model (see addendum for more details).  Color maps showing the magnitude of the transverse magnetic field, Bx, in the y-z plane inside a magnetic shield with permeability µr = 20000, thickness d = 1 mm, and a circular axial entry hole of normalized radius, ρ h = 0.25ρs, around the centre of each end cap generated by two active-passive systems: (a) the constant transverse field-generating system depicted in Fig. 3; (b) the linear transverse gradient field-generating system depicted in Fig. 4. The field profiles were calculated numerically using COMSOL Multiphysics R Version 5.3a. Contours show where the field deviates from the target field by 1% (solid curves), 0.1% (dashed curves), 0.01% (dot-dashed curves), and 0.001% (dotted curves; in (a) only).

IV. CONCLUSION
In this paper, we have developed an analytical model to calculate the total magnetic field generated by an arbitrary current flow on a cylinder that is coaxially nested within a finite closed high-permeability cylinder. We modified the Green's function for the magnetic vector potential, matched the radial and planar boundary conditions of the magnetic field through the introduction of a pseudo-current density, and incorporated a harmonic minimization procedure to design optimal user-specified magnetic fields by using a modified cylindrical Fourier basis. We then verified this optimization procedure by designing coils to generate a constant transverse field, B x , across the cylinder, and a linear transverse field gradient, dB x /dz, along the length of the cylinder. Our analytical calculations of these field profiles agreed well with numerical simulations. The optimization procedure generated highly accurate B x and dB x /dz field profiles, inside a high-permeability cylinder, with peak-to-peak deviations from the target profiles below 0.11% and 0.24%, respectively. The analytically-predicted deviations agreed with the numerical simulations to within 0.002% and 0.003% for the constant and linear gradient systems, respectively.
We further investigated the validity of our model by analyzing the behavior of the constant transverse field-generating system inside a magnetic shield of permeability µ r = 20000, finite thickness, and with circular axial entry holes in the end caps. We found a range of parameters where the analytical predictions for a perfect cylindrical magnetic conductor remain close to numerical simulations for a cylindrical shield with finite permeability and thickness that includes entry holes; showing that the designs generated by our model are applicable to real-world magnetic shields. Notably, when the active field-generating systems were enclosed by a passive magnetic shield with realistic experimental parameters (µ r = 20000, thickness 1 mm, and entry holes of radius equal to 25% of the shield's radius), the deviation from the desired constant and linear gradient field profiles were less than 0.1% and 0.5%, respectively, over more than 40% of the central radial and axial extent of this simulated real-world magnetic shield.
Our flexible optimization procedure enables the design of new active-passive magnetic field shaping systems to generate accurately any static magnetic field profile in the interior of a finite closed magnetic shield, subject to satisfying Maxwell's equations in free space and not saturating the shielding material. This facilitates the development and miniaturization of systems and technologies which require such control, including quantum sensors, fundamental physics experiments, and medical technologies. Further investigation could consider an analytical treatment of finite magnetic shield thickness and permeability and interactions with an open magnetic shield topology.

SUPPLEMENTARY MATERIAL A solenoidal coil
As shown in previous work [36][37][38], a solenoid of the same length as the high-permeability cylinder provides the most optimal solution for generating a constant axial field. Due to the image currents, the finite solenoid effectively acts as one of infinite extension, resulting in the most homogeneous possible magnetic field in the z-direction. A perfect finite solenoid of length L c carries a total current of where I is the current density, i.e. the current per unit solenoid length, resulting in the azimuthal current I t /L c . Using (25) the Fourier transform of the current through a finite solenoid given by Substituting the above equation into (33) gives the magnetic field in the z-direction Performing the integral over k by expressing the summation over the infinite pseudo-current reflections through a Fourier series expansion we find This summation can be simplified as the only contributing term is p = 0, which, when evaluated results in This result might seem counter intuitive because the magnetic field is identical to a long solenoid in free space with N turns, i.e. B z (ρ, φ, z) = µ 0 IN/L c , with no field created by the cylindrical surface of the passive shield. This is, however, entirely physical as an infinite solenoid generates zero magnetic field outside of the solenoid itself. Consequently, there is no response due to the surface of the cylindrical wall and, therefore, no magnetic field generated by the cylindrical surface of the perfect magnetic conductor, i.e. the high-permeability cylinder.

Example coil designs
In Figs. 8-9, we present further hybrid active-passive systems, which generate more complex magnetic field landscapes beyond the constant field and field gradient considered in the main body of the paper. The co-ordinate axes and magnetic field plots are labelled in the same way as the systems presented in the main text. The design in Fig. 8 generates uniform axial magnetic fields, B z , with different strengths in two different regions of a magnetic shield. The design in Fig. 9 generates a quadratic gradient field whose spatial variation matches the harmonic B = (2xzx − 2yzŷ + (x 2 − y 2 )ẑ). Further systems can be designed using Python code in the GitHub repository listed in the addendum. (b) Axial magnetic field, Bz, versus axial position, z, calculated from the current continuum in (a) in three ways: analytically using (37)-(39) (solid red curve); numerically using COMSOL Multiphysics R Version 5.5a and modelling the high-permeability cylinder as a perfect magnetic conductor (blue dotted curve, which essentially overlays the red curve); numerically without the high-permeability cylinder and using the Biot-Savart law with Nϕ = 250 contour levels (dashed green curve).