Magnetic field with periodic boundary conditions, torus and magnetic monopoles Suppose I have a 2D square lattice in the xy-plane, and I apply a uniform magnetic field in the z-direction. To simplify calculations, I would like to assume periodic boundary conditions in both the x and y directions for my system, so that I can perform some kind of Fourier transform. This was done in the following paper Energy Levels and Wave Functions of Bloch Electrons in Rational and Irrational Magnetic Fields. However, is this actually allowed?
My concern is this: When we identify the opposite edges of a rectangle as identical, we end up with a torus. The surface of a torus defines an "inside" and an "outside". The magnetic field being uniform and perpendicular to the torus' surface means that all the field lines travel from inside to outside or vice versa. But by Gauss' law, the magnetic flux of an enclosed surface is always zero. There is a contradiction somewhere.
So the main question is if periodic boundary conditions are allowed in this kind of system with a magnetic field? If, yes, it would be even better if you can further justify it in the context of the paper above.
 A: The same issue arises for any closed, oriented surface, when you try to put a uniform magnetic field through the surface. Strictly speaking Gauss's law does not allow any non-zero total flux through a closed surface. However, at least in theory we can have magnetic monopoles. If that's allowed, then nonzero total flux through a closed surface becomes possible, but there is still a condition: the flux must be an integer multiple of flux quanta ($\hbar c/e$, where $e$ is the fundamental charge of the system). The quantization is necessary to make sure that quantum mechanical wavefunctions are single-valued. One can make very general mathematical argument about why it has to be the case on any closed surface, but we can also do things explicitly on a torus.
Below I set $\hbar=e=c=1$. Suppose we have a $L_x\times L_y$ torus (so identify $x\sim x+L_x, y\sim y+L_y$), and let's choose a Landau gauge:
$$
A_x(x,y)=0, A_y(x,y)=Bx
$$
This is what one would do naively on an infinite plane. Now we have a problem: the periodic boundary condition is imposed, but $A_y$ is not periodic in $x$ ($\mathbf{A}$ has no $y$ dependence so the periodicity along $y$ is fine). In particular, $A_y(x+L_x,y)$ and $A_y(x,y)$ differ by $BL_x$. To rescue this, we should remember that vector potential has gauge ambiguity. Namely, if we change $\mathbf{A}$ by a gauge transformation, the result is physically equivalent. So if we can find a gauge transformation $\mathbf{A}\rightarrow \mathbf{A}+\nabla \chi$ to "patch" the difference between $A_y(x+L_x,y)$ and $A_y(x,y)$, then everything is fine. The gauge transformation we need is one satisfies $\partial_y \chi = BL_x$, so $\chi=BL_x y$.
We are not quite done yet. Gauge transformation changes the wavefunction by $\psi\rightarrow e^{i\chi}\psi$. Since $\psi$ is single-valued, the gauge transformation should not spoil the periodicity. Thus $e^{iB L_xy}$ should be periodic in $y$:
$$ 
e^{iBL_x (y+L_y)}=e^{iBL_xy},
$$
which means $BL_xL_y$ must be $2\pi n$ where $n\mathbb{Z}$.
