$$ \def\ab{\boldsymbol{a}} \def\bb{\boldsymbol{b}} \def\cb{\boldsymbol{c}} \def\db{\boldsymbol{d}} \def\eb{\boldsymbol{e}} \def\fb{\boldsymbol{f}} \def\gb{\boldsymbol{g}} \def\hb{\boldsymbol{h}} \def\kb{\boldsymbol{k}} \def\nb{\boldsymbol{n}} \def\pb{\boldsymbol{p}} \def\qb{\boldsymbol{q}} \def\rb{\boldsymbol{r}} \def\tb{\boldsymbol{t}} \def\ub{\boldsymbol{u}} \def\vb{\boldsymbol{v}} \def\xb{\boldsymbol{x}} \def\yb{\boldsymbol{y}} \def\zb{\boldsymbol{z}} \def\Ab{\boldsymbol{A}} \def\Bb{\boldsymbol{B}} \def\Eb{\boldsymbol{E}} \def\Fb{\boldsymbol{F}} \def\Jb{\boldsymbol{J}} \def\Ub{\boldsymbol{U}} \def\xib{\boldsymbol{\xi}} \def\evx{\boldsymbol{e}_x} \def\evy{\boldsymbol{e}_y} \def\evz{\boldsymbol{e}_z} \def\evr{\boldsymbol{e}_r} \def\evt{\boldsymbol{e}_\theta} \def\evp{\boldsymbol{e}_r} \def\evf{\boldsymbol{e}_\phi} \def\evb{\boldsymbol{e}_\parallel} \def\omb{\boldsymbol{\omega}} \def\dA{\;\mathrm{d}\Ab} \def\dS{\;\mathrm{d}\boldsymbol{S}} \def\dV{\;\mathrm{d}V} \def\dl{\mathrm{d}\boldsymbol{l}} \def\bfzero{\boldsymbol{0}} \def\Rey{\mathrm{Re}} \def\Real{\mathbb{R}} \def\grad{\boldsymbol\nabla} \newcommand{\dds}[1]{\frac{\mathrm{d}{#1}}{\mathrm{d}s}} \newcommand{\ddy}[2]{\frac{\partial{#1}}{\partial{#2}}} \newcommand{\ddt}[1]{\frac{\mathrm{d}{#1}}{\mathrm{d}t}} \newcommand{\DDt}[1]{\frac{\mathrm{D}{#1}}{\mathrm{D}t}} $$
A gauge matching trick
Suppose you are given a 3D magnetic field \({\bf B}(x,y,z)\) and also a “reference field” \({\bf B}^p(x,y,z),\) both defined in the same Cartesian domain \(\{x_0\leq x\leq x_1,\, y_0\leq y\leq y_1,\, z_0\leq z\leq z_1\}.\) By definition, the reference field must satisfy \({\bf B}^p\cdot\hat{\bf n} = {\bf B}\cdot\hat{\bf n}\) on all six boundaries, but is otherwise arbitrary. Moreover, suppose that you know a vector potential \({\bf A}^p\) for the reference field, so that \(\nabla\times{\bf A}^p={\bf B}^p.\) How do you find a vector potential \({\bf A}\) for the original field \({\bf B},\) such that \({\bf A}\times\hat{\bf n} = {\bf A}^p\times\hat{\bf n}\) on all six boundaries? In other words, we want the tangential components of \({\bf A}\) to match those of \({\bf A}^p\) on all of the boundaries.
This is the kind of practical problem that arises when you work with magnetic helicity, and indeed I needed to find such a vector potential today. My collaborator Alex Russell came up with a neat method for this a while back, and this is what I describe here.
First, we compute a vector potential \({\bf A}'\) such that \(\nabla\times{\bf A}'={\bf B} - {\bf B}^p.\) This is easily done using line integrals: we can take the specific solution \[ {\bf A'}(x,y,z) = - \int_{y_0}^y\Big(B_z(x,s,z) - B_z^p(x,s,z)\Big)\,\mathrm{d}s\,\hat{\bf x} + \int_{y_0}^y\Big(B_x(x,s,z) - B_x^p(x,s,z)\Big)\,\mathrm{d}s\,\hat{\bf z}. \] You can easily check that \({\bf A}'\) curls to give \({\bf B} - {\bf B}^p,\) using the fact that both magnetic fields are divergence free.
Now, we need to change the gauge to \({\bf A}''={\bf A}' + \nabla\chi\) so that \({\bf A}''\times\hat{\bf n}=0\) on all six boundaries. In fact, the tangential components of \({\bf A}'\) already vanish on all boundaries except for \(y=y_1\) (you should check this). To find a suitable \({\bf A}'',\) Alex’s clever trick is to choose the gauge function \[ \chi(x,y,z) = -\frac{y - y_0}{y_1 - y_0}\int_{x_0}^xA'_x(s,y_1,z)\,\mathrm{d}s. \] With this choice, and some algebra, we obtain \[\begin{align*} A''_x(x,y,z) &= A'_x(x,y,z) - \frac{y-y_0}{y_1-y_0}A'_x(x,y_1,z),\\ A''_y(x,y,z) &= \frac{-1}{y_1-y_0}\int_{x_0}^xA'_x(s,y_1,z)\,\mathrm{d}s,\\ A''_z(x,y,z) &= A'_z(x,y,z) - \frac{y-y_0}{y_1-y_0}A'_z(x,y_1,z). \end{align*}\] The last component is not immediately obvious, but comes from \[\begin{align*} \frac{\partial}{\partial z}\int_{x_0}^x A'_x(s,y_1,z)\,\mathrm{d}s &= \int_{x_0}^x \frac{\partial A'_x}{\partial z}(s,y_1,z)\,\mathrm{d}s\\ &= \int_{x_0}^x\left(B_y(s,y_1,z) - B^p_y(s,y_1,z) + \frac{\partial A'_z}{\partial x}(s,y_1,z)\right)\,\mathrm{d}s\\ &= \int_{x_0}^x\frac{\partial A'_z}{\partial x}(s,y_1,z)\,\mathrm{d}s = A'_z(x,y_1,z) - A'_z(x_0,y_1,z). \end{align*}\] Using the fact that \(A'_z(x_0, y_1, z)=0,\) we get the quoted result.
It’s quite clear that \(A''_x\) and \(A''_z\) vanish on the required boundaries, using the boundary conditions \({\bf B}^p\cdot\hat{\bf n} = {\bf B}\cdot\hat{\bf n}\) as necessary. Similarly, you can see that \(A''_y\) vanishes on the \(z\)-boundaries and on \(x=x_0.\) However, it is less obvious that it vanishes also on \(x=x_1.\) To see this, substitute for \(A'_x\) to get \[ A''_y(x_1,y,z) = \frac{1}{y_1-y_0}\int_{x_0}^{x_1}\int_{y_0}^{y_1}\Big(B_z(s,t,z) - B_z^p(s,t,z)\Big)\,\mathrm{d}s\,\mathrm{d}t. \] It follows from the boundary conditions that the flux of \({\bf B}\) through any plane of constant z is the same as the flux of \({\bf B}^p,\) so this double-integral indeed vanishes.
Once we have \({\bf A}'',\) it is easy to find our desired \({\bf A}\) by simply setting \({\bf A} = {\bf A}'' + {\bf A}^p.\)
Because it only requires one-dimensional integrals, this method is fast to implement numerically. As I discovered today, it is also easy to parallelize, because all of the integrals are in planes of constant \(z\). Of course, you do have to first find \({\bf B}^p\) and \({\bf A}^p\)…