\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[fleqn]{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{indentfirst}
%\usepackage[a4paper,includeheadfoot,margin=2.54cm]{geometry}

% Shortcuts
% ---------
\newcommand{\mw}{MetalWalls}
\newcommand{\etal}{~\emph{et al.}}

% Math formatting command
% -----------------------

% Vectors are in bold
\renewcommand{\vec}[1]{\bm{#1}}

% Define erf and erfc as math functions
\DeclareMathOperator\erf{erf}
\DeclareMathOperator\erfc{erfc}
\DeclareMathOperator\sgn{sgn}

\author{Alessandro Coretti}
\title{Metalwalls (part II)}
\begin{document}
%
  \pagenumbering{gobble}
  \maketitle
  \newpage
  \pagenumbering{arabic}
%
  \chapter{Interaction Between Electrode Charges and Dipoles of the Melt}
  The starting point for the derivation is the dipole-charge interaction term given by Allen and Tildesley~\cite{allen:2017-book}, which is the same used by Ishii\etal~\cite{ishii:2015} and by Aguado and Madden~\cite{aguado:2003b}. For a system of $N$ point charges $q_i$ with associated dipoles $\vec{\mu}_i$, this term is given by
  \begin{equation}
  \label{eq:qmelt2mumelt_energy}
  U_{c_{\mathrm{p\mu}}} = -\sum_{i=1}^N\sum_{j>i}^N\biggl[\frac{q_i\vec{r}_{ij}\cdot\vec{\mu}_j}{r^3_{ij}} - \frac{q_j\vec{r}_{ij}\cdot\vec{\mu}_i}{r^3_{ij}}\biggr]
  \end{equation}
  where the damping functions have been dropped and the distance vector is defined --- at contrary with respect to \mw~(part I) by Abel --- as $\vec{r}_{ij} = \vec{r}_i - \vec{r}_j$. The modulus $|\vec{r}_{ij}|$ is indicated by $r_{ij}$.
  
  The system we want to study is different in many ways from the one described by eq.~\eqref{eq:qmelt2mumelt_energy}. Indeed, we want to describe a set of Gaussian-charge distributions which is interacting with the dipoles of a set of point-charge distributions. In particular, the particles involved are of two different kinds, one being particles of the metal walls, while the other being particles of the melt. The above statement encodes two main differences:
  \begin{itemize}
  \item The sums run on two different sets of indexes, one representing the melt particles the other the electrode charges.
  \item The charges are not point charges, but they are distributed as Gaussians.
  \end{itemize}
  The first item of the list imposes a reindexing of the sums as no double counting is involved in this case. This means, in particular, that, indicating by $N_e$ and $N_p$ the number of electrode charges and the number of (dipolar) point particles in the melt, eq.~\eqref{eq:qmelt2mumelt_energy} can be written as
  \begin{equation}
  \label{eq:Qelec2mumelt_energy_intermediate_one}
  \widetilde{U}_{c_{\mathrm{p\mu}}} = -\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\biggl[\frac{q_i\vec{r}_{ij}\cdot\vec{\mu}_j}{r^3_{ij}}\biggr]
  \end{equation}
  where the tilde indicates the different indexing method (indeed $\vec{r}_{ij}\neq-\vec{r}_{ij}$ for this system). We stress the fact that the summations, running on particles of different kinds, do not include the term $i=j$ since no such term actually exists.  
  
  The second item in the list implies the Gaussian shape of the charge distributions, which are given by
  \begin{equation} 
  \label{eq:rho_gaussian}
  \rho_i(\vec{r}) = Q_i \left(\frac{\eta_i^2}{\pi}\right)^{3/2} e^{-\eta_i^2 |\vec{r}-\vec{r}_i|^2} \qquad \text{for} \ i=1,\dots,N_e
  \end{equation}
where $\vec{r}_i$ and $Q_i$ are the position and the integral charge of the atom $i$, respectively and $\eta_i$ is a model parameter which is dependent by the particular site considered. Substituting this expression in eq.~\eqref{eq:Qelec2mumelt_energy_intermediate_one} in place of the point charge $q_i$ and integrating over the whole space we get
  \begin{equation}
  \label{eq:Qelec2mumelt_energy_intermediate_two}
  U_{c_{\mathrm{g\mu}}} = -\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int_{\mathbb{R}^3}d^3r\biggl[Q_i\Bigl(\frac{\eta^2_i}{\pi}\Bigr)^{\frac{3}{2}} e^{-\eta_i^2 |\vec{r}-\vec{r}_i|^2}\frac{(\vec{r} - \vec{r}_{j})\cdot\vec{\mu}_j}{|\vec{r} - \vec{r}_{j}|^3}\biggr]
  \end{equation}
  It is now possible to substitute $\vec{r}' = \vec{r}_i-\vec{r}$ so that $\vec{r} = \vec{r}_i - \vec{r}'$ (the Jacobian is $-1$). The final expression is then given by 
  \begin{equation}
  \label{eq:Qelec2mumelt_energy}
  U_{c_{\mathrm{g\mu}}} = \sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int_{\mathbb{R}^3}d^3r\biggl[Q_i\Bigl(\frac{\eta^2_i}{\pi}\Bigr)^{\frac{3}{2}} e^{-\eta_i^2 |\vec{r}|^2}\frac{(\vec{r}_{ij} - \vec{r})\cdot\vec{\mu}_j}{|\vec{r}_{ij} - \vec{r}|^3}\biggr]
  \end{equation}
  where the prime has been dropped since no ambiguity is produced. The same result would be obtained using Abel's convention $\vec{r}_{ij} = \vec{r}_j - \vec{r}_i$ changing the sign in front of the expression.
  
  Periodic boundary conditions are enforced in the system and we will treat separately the case in which only two or all three dimensions are replicated. For the moment the repeated boxes will be generally labelled through the vector $\vec{n} \in \mathbb{Z}^3$ where $n_z = 0$ when two-dimensional periodic boundary conditions are considered. This means that, for each particle (melt or electrode) at position $\vec{r}_i$, there are an infinite number of other particles at position $\vec{m} = \vec{n}\vec{L}$ (product component by component) where $\vec{n}$ is defined above and $\vec{L} = (L_x,L_y,L_z)$ are the dimensions of the simulation box. The expression of the energy then becomes
  \begin{equation}
  \label{eq:Qelec2mumelt_energy_intermediate_PBC}
  U_{c_{\mathrm{g\mu}}} = \sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{n}}\int_{\mathbb{R}^3}d^3r\biggl[Q_i\Bigl(\frac{\eta^2_i}{\pi}\Bigr)^{\frac{3}{2}} e^{-\eta_i^2 |\vec{r}|^2}\frac{(\vec{r}_{ij} - \vec{r} + \vec{m})\cdot\vec{\mu}_j}{|\vec{r}_{ij} - \vec{r} + \vec{m}|^3}\biggr]
  \end{equation}
  Note that no prime is present on the summation over the boxes, since no term $i=j$ exists and has to be excluded.
  
  The exponential in $\vec{r}$ can be expanded using the property of the Gaussian integral as
  \begin{equation} 
  \label{eq:Gaussian_integral_3D_final}
  \exp(-\eta_i^2|\vec{r}|^2) = (2\pi)^{-3}\Bigl(\frac{\pi}{\eta^2_i}\Bigr)^{\frac{3}{2}}\int_{\mathbb{R}^3}d^3v \exp\biggl[-\frac{|\vec{v}|^2}{4\eta_i^2} + \mathrm{i} \vec{v} \cdot \vec{r} \biggr]
  \end{equation}
  as done in the case of \mw~(part I) by Abel.
  
  The term $|\vec{r}_{ij} - \vec{r} + \vec{m}|^{-3}$ can be expanded using the properties of the Euler's gamma functions
  \begin{equation}
  \int_0^\infty dt \ t^be^{-at} = \frac{\Gamma(b+1)}{a^{b+1}}
  \end{equation}
  which is again the same trick used by Abel. In the case of the inverse function $r^{-1}$ we had $a = r^2$ and $b = -\frac{1}{2}$. In the present case, i.e. $r^{-3}$, we have $a = r^2$ and $b = +\frac{1}{2}$, so that\footnote{Of course the choice $a = r^6$ and $b = -\frac{1}{2}$ is equally possible, but it is much better to work with Gaussian integrals.} 
  \begin{equation}
  \label{eq:Gamma_functions_final}
  \frac{1}{r^3} = \frac{2}{\sqrt{\pi}}\int_0^\infty dt \ t^{\frac{1}{2}}e^{-r^2t}
  \end{equation}
  
  The next relation used by Abel is the so-called ``Jacobi Imaginary Transformation''\footnote{The relation is cited with this name also by Pounds and Gingrich in their PhD and Master thesis, respectively.} to switch from a sum over $\vec{n}$ in real space to a sum over $\vec{k}$ in reciprocal space. I found no trace in the literature of this transformation except that in the context of Elliptic functions which I do not know how to relate to the present case. What I think is the relation sought by Abel and the others is the Poisson Summation Formula~\cite{wiki:poisson}, which reads
  \begin{equation}
  \sum_{\vec{n}\in\mathbb{Z}} f(\vec{n}) = \sum_{\vec{k}\in\mathbb{Z}} \hat{f}(\vec{k})
  \end{equation}
  where $\hat{f}(\vec{k})$ is the Fourier Transform of the function $f(\vec{\vec{r}})$, given by
  \begin{equation}
  \mathcal{F}[f] = \hat{f}(\vec{k}) = \int_{\mathbb{R}^3} d^3r f(\vec{r}) e^{-\mathrm{i}2\pi\vec{r}\cdot\vec{k}}
  \end{equation}
  and $\vec{n}$ and $\vec{k}$ represent periodicity and corresponding wavevectors in real and reciprocal space, respectively. We want to write in reciprocal space an expression of the form
  \begin{equation}
  \label{eq:sum_over_boxes}
  \sum_{\vec{n}}\vec{\mu}\cdot(\vec{r} + \vec{m})\exp\Bigl[-(\vec{r} + \vec{m})^2t\Bigr]
  \end{equation}
  The reciprocal space will be characterized by the wavevectors indexes $\vec{k} \in \mathbb{Z}^3$ where, again, $k_z=0$ when two-dimensional periodic boundary conditions have to be enforced. The size of the system is taken into account in the basis elements of the reciprocal space defining the vectors $\vec{h} = 2\pi\vec{k}/\vec{L}$ (ratio component by component). Expanding the scalar products outside and inside the exponential function in eq.~\eqref{eq:sum_over_boxes} and exploiting the properties of the exponentials, we obtain a sum of products of the following two terms
  \begin{equation}
  \begin{aligned}
  &\exp\Bigl[-(\alpha + n_\alpha L_\alpha)^2t\Bigr] \\
  &\mu_\alpha(\alpha + n_\alpha L_\alpha)\exp\Bigl[-(\alpha + n_\alpha L_\alpha)^2t\Bigr]\\
  \end{aligned}
  \end{equation}
  where $\alpha \in \{x,y,z\}$.
  Fourier transforming these terms we get
  \begin{equation}
  \label{eq:FT_2D-PBC}
  \begin{aligned}
  &\mathcal{F}\biggl[\exp\Bigl[-(\alpha + n_\alpha L_\alpha)^2t\Bigr]\biggr] =  \frac{1}{L_\alpha}\sqrt{\frac{\pi}{t}}\exp\biggl[-\frac{\pi^2k_\alpha^2}{L_\alpha^2t} + \mathrm{i}\frac{2\pi k_\alpha\alpha}{L_\alpha}\biggr] \\
  &\mathcal{F}\biggl[\mu_\alpha(\alpha + n_\alpha L_\alpha)\exp\Bigl[-(\alpha + n_\alpha L_\alpha)^2t\Bigr]\biggr] = -\mathrm{i} \frac{\pi^{\frac{3}{2}}}{L_\alpha^2t^{\frac{3}{2}}}k_\alpha\exp\biggl[-\frac{\pi^2k_\alpha^2}{L_\alpha^2t} + \mathrm{i}\frac{2\pi k_\alpha\alpha}{L_\alpha}\biggr]\\
  \end{aligned}
  \end{equation}
  
  Now a distinction has to be made between the cases in which the periodic boundary conditions are enforced in two or three dimensions.
  
  \section{Periodic Boundary Conditions on the $xy$-plane.}
  When periodic boundary conditions are enforced on the $xy$-plane the vectors $\vec{n}$ and $\vec{k}$ are given by $\vec{n} = (n_x, n_y, 0)$ and $\vec{k} = (k_x, k_y, 0)$, respectively, so that the sum in eq.~\eqref{eq:sum_over_boxes} can actually be expanded as
  \begin{equation}
  \begin{aligned}
  &\sum_{\vec{n}}\vec{\mu}\cdot(\vec{r} + \vec{m})\exp\Bigl[-(\vec{r} + \vec{m})^2t\Bigr] = \\
  &= \sum_{n_x}\sum_{n_y} \bigl[\mu_x(x+n_xL_x) + \mu_y(y+n_yL_y) + \mu_zz\bigr] \times \\
  &\times \exp\Bigl[-(x + n_xL_x)^2t\Bigr]\exp\Bigl[-(y + n_y L_y)^2t\Bigr]\exp\Bigl[-z^2t\Bigr]
  \end{aligned}
  \end{equation}
  The exponential function in $z$ can be taken out from the summation and it will be not involved in the Fourier transform. The $\mathcal{F}$ operator can now be applied on the rest of the expression to get
  \begin{equation}
  \begin{aligned}
  &\exp\Bigl[-z^2t\Bigr]\sum_{n_x}\sum_{n_y} \mathcal{F}\biggl[\bigl[\mu_x(x+n_xL_x) + \mu_y(y+n_yL_y) + \mu_zz\bigr] \times \\
  &\times \exp\Bigl[-(x + n_xL_x)^2t\Bigr]\exp\Bigl[-(y + n_y L_y)^2t\Bigr]\biggr] = \\
  &= \exp\Bigl[-z^2t\Bigr]\sum_{n_x}\sum_{n_y}\Biggl\{\mathcal{F}\biggl[\mu_x(x+n_xL_x)\exp\Bigl[-(x + n_xL_x)^2t\Bigr]\biggr]\mathcal{F}\biggl[\exp\Bigl[-(y + n_y L_y)^2t\Bigr]\biggr] + \\
  &+ \mathcal{F}\biggl[\exp\Bigl[-(x + n_xL_x)^2t\Bigr]\biggr]\mathcal{F}\biggl[\mu_y(y+n_yL_y)\exp\Bigl[-(y + n_y L_y)^2t\Bigr]\biggr] + \\
  &+ \mu_zz\mathcal{F}\biggl[\exp\Bigl[-(x + n_xL_x)^2t\Bigr]\biggr]\mathcal{F}\biggl[\exp\Bigl[-(y + n_y L_y)^2t\Bigr]\biggr]\Biggr\}
  \end{aligned}
  \end{equation}
  where the linearity of the Fourier operator and the factorizability in the $x$ and $y$ variables have been used.
  We can now use the results from eqs.~\eqref{eq:FT_2D-PBC} to write the expression in reciprocal space as
  \begin{equation}
  \begin{aligned}  
  &\exp\Bigl[-z^2t\Bigr]\sum_{k_x}\sum_{k_y}\Biggl\{-\mathrm{i} \frac{\pi^2}{L_xt^2}\frac{k_x}{L_x}\mu_x\exp\biggl[-\frac{\pi^2k_x^2}{L_x^2t} + \mathrm{i}\frac{2\pi k_xx}{L_x}\biggr]\frac{1}{L_y}\exp\biggl[-\frac{\pi^2k_y^2}{L_y^2t} + \mathrm{i}\frac{2\pi k_yy}{L_y}\biggr] + \\
  &+ \frac{1}{L_x}\exp\biggl[-\frac{\pi^2k_x^2}{L_x^2t} + \mathrm{i}\frac{2\pi k_xx}{L_x}\biggr]\biggl(-\mathrm{i} \frac{\pi^2}{L_yt^2}\frac{k_y}{L_y}\mu_y\biggr)\exp\biggl[-\frac{\pi^2k_y^2}{L_y^2t} + \mathrm{i}\frac{2\pi k_yy}{L_y}\biggr] + \\
  &+ \mu_zz\frac{1}{L_x}\sqrt{\frac{\pi}{t}}\exp\biggl[-\frac{\pi^2k_x^2}{L_x^2t} + \mathrm{i}\frac{2\pi k_xx}{L_x}\biggr]\frac{1}{L_y}\sqrt{\frac{\pi}{t}}\exp\biggl[-\frac{\pi^2k_y^2}{L_y^2t} + \mathrm{i}\frac{2\pi k_yy}{L_y}\biggr]\Biggr\} = \\
  &= \frac{1}{L_xL_y}\frac{\pi}{t}\exp\Bigl[-z^2t\Bigr]\sum_{k_x}\sum_{k_y}\Biggl\{\biggl[-\mathrm{i}\frac{\pi}{t}\frac{k_x}{L_x}\mu_x - \mathrm{i}\frac{\pi}{t}\frac{k_y}{L_y}\mu_y + \mu_zz\biggr] \times \\
  &\times \exp\biggl[-\frac{\pi^2k_x^2}{L_x^2t} + \mathrm{i}\frac{2\pi k_xx}{L_x}\biggr]\exp\biggl[-\frac{\pi^2k_y^2}{L_y^2t} + \mathrm{i}\frac{2\pi k_yy}{L_y}\biggr]\Biggl\}
  \end{aligned}
  \end{equation}
  The exponentials can now be factored together to obtain finally 
  \begin{equation}
  \label{eq:Poisson_Summation_Formula_final}
  \begin{aligned}
  &\sum_{n_x}\sum_{n_y} \bigl[\mu_x(x+n_xL_x) + \mu_y(y+n_yL_y) + \mu_zz\bigr] \times \\
  &\times \exp\Bigl[-(x + n_xL_x)^2t\Bigr]\exp\Bigl[-(y + n_y L_y)^2t\Bigr]\exp\Bigl[-z^2t\Bigr] = \\
  &= \frac{1}{L_xL_y}\frac{\pi}{t}\exp\Bigl[-z^2t\Bigr]\sum_{k_x}\sum_{k_y}\Biggl\{\biggl[-\mathrm{i}\frac{\pi}{t}\frac{k_x}{L_x}\mu_x - \mathrm{i}\frac{\pi}{t}\frac{k_y}{L_y}\mu_y + \mu_zz\biggr] \times \\
  &\times \exp\biggl[-\frac{\pi^2k_x^2}{L_x^2t} + \mathrm{i}\frac{2\pi k_xx}{L_x}\biggr]\exp\biggl[-\frac{\pi^2k_y^2}{L_y^2t} + \mathrm{i}\frac{2\pi k_yy}{L_y}\biggr]\Biggl\} = \\
  &= \frac{1}{L_xL_y}\frac{\pi}{t}\exp\Bigl[-z^2t\Bigr]\sum_{\vec{k}}\Biggl\{\biggl[-\mathrm{i}\frac{1}{2t}h_x\mu_x - \mathrm{i}\frac{1}{2t}h_y\mu_y + \mu_zz\biggr] \times \\
  &\times \exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot\vec{r}\biggr]\Biggl\}
  \end{aligned}
  \end{equation}
  recalling that $\vec{h} = 2\pi\vec{k}/\vec{L} = (2\pi k_x/L_x,2\pi k_y/L_y,0)$ for two-dimensional periodic boundary conditions. We can now substitute the three relations eqs.~\eqref{eq:Gaussian_integral_3D_final},~\eqref{eq:Gamma_functions_final} and~\eqref{eq:Poisson_Summation_Formula_final} in the original expression of the energy
  \begin{equation}
  \begin{aligned}
  U_{c_{\mathrm{g\mu}}} &= \sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i\Bigl(\frac{\eta^2_i}{\pi}\Bigr)^{\frac{3}{2}}\sum_{\vec{n}}\int_{\mathbb{R}^3}d^3r\frac{(\vec{r}_{ij} - \vec{r} + \vec{m})\cdot\vec{\mu}_j}{|\vec{r}_{ij} - \vec{r} + \vec{m}|^3}e^{-\eta_i^2 |\vec{r}|^2} = \\
  &= \frac{1}{(2\pi)^{3}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i \sum_{\vec{n}}\int_{\mathbb{R}^3}d^3r\frac{(\vec{r}_{ij} - \vec{r} + \vec{m})\cdot\vec{\mu}_j}{|\vec{r}_{ij} - \vec{r} + \vec{m}|^3} \times \\
  &\times \int_{\mathbb{R}^3}d^3v \exp\biggl[-\frac{|\vec{v}|^2}{4\eta_i^2} + \mathrm{i} \vec{v} \cdot \vec{r} \biggr] = \\
  &= \frac{1}{4\pi^{\frac{7}{2}}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i \int_{\mathbb{R}^3}d^3r\int_0^\infty dt \ t^{\frac{1}{2}}\times \\
  &\times \sum_{\vec{n}}(\vec{r}_{ij} - \vec{r} + \vec{m})\cdot\vec{\mu}_j \times e^{-|\vec{r}_{ij} - \vec{r} + \vec{m}|^2t} \times \\
  &\times \int_{\mathbb{R}^3}d^3v \exp\biggl[-\frac{|\vec{v}|^2}{4\eta_i^2} + \mathrm{i} \vec{v} \cdot \vec{r} \biggr] = \\
  &= \frac{1}{L_xL_y}\frac{1}{4\pi^{\frac{5}{2}}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i \int_{\mathbb{R}^3}d^3r\int_0^\infty dt \ t^{-\frac{1}{2}}\times \\
  &\times \sum_{\vec{k}}\Biggl\{\biggl[-\mathrm{i}\frac{1}{2t}h_x\mu_x - \mathrm{i}\frac{1}{2t}h_y\mu_y + (z_{ij} - z)\mu_z\biggr] \times \\
  &\times \exp\Bigl[-(z_{ij}-z)^2t\Bigr]\exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot(\vec{r}_{ij} - \vec{r})\biggr]\Biggl\} \times \\
  &\times \int_{\mathbb{R}^3}d^3v \exp\biggl[-\frac{|\vec{v}|^2}{4\eta_i^2} + \mathrm{i} \vec{v} \cdot \vec{r} \biggr]
  \end{aligned}
  \end{equation}
  We can now further simplify this expression exploiting again the properties of the Gaussian integrals. In particular we want to expand the exponential $\exp[-(z_{ij}-z)^2t]$ which, contrary to the case treated in \mw~(part I), is now multiplied by the factor $(z_{ij} - z)\mu_z$. This can be done once we recognize that this factor can be written as the derivative of a Gaussian
  \begin{equation}
  x\exp[-x^2t] = -\frac{1}{2t}\frac{\partial}{\partial x}\exp[-x^2t]
  \end{equation}
  The Gaussian integral eq.~\eqref{eq:Gaussian_integral_3D_final} can now be used in its standard one-dimensional form and, taking the derivative with respect to $x$, we obtain the result
  \begin{equation}
  \label{eq:Gaussian_linear_integral_1D}
  \begin{aligned}
  x\exp[-x^2t] &= -\frac{1}{2t}\frac{\partial}{\partial x}\biggl[\frac{1}{\sqrt{4\pi t}}\int^{\infty}_{-\infty}du \exp\Bigl[-\frac{u^2}{4t} + \mathrm{i}ux\Bigr]\biggr] = \\
  &= -\frac{1}{2t}\frac{1}{\sqrt{4\pi t}}\int^{\infty}_{-\infty}du \ (\mathrm{i}u)\exp\Bigl[-\frac{u^2}{4t} + \mathrm{i}ux\Bigr]\biggr] = \\
  &= -\frac{\mathrm{i}}{2t}\frac{1}{\sqrt{4\pi t}}\int^{\infty}_{-\infty}du \ u\exp\Bigl[-\frac{u^2}{4t} + \mathrm{i}ux\Bigr]\biggr]
  \end{aligned}
  \end{equation}
  Since the exponential $\exp[-(z_{ij}-z)^2t]$ is multiplied by the whole scalar product we have to treat consistently all the prefactors. In particular there will be a term $(\sqrt{4\pi t})^{-1}\int du \exp[-u^2/(4t) + \mathrm{i}u(z_{ij} - z)]$ multiplied by the whole scalar product, while the term $-\mathrm{i}u/(2t)$ will multiply only the $z$ component. In this way the energy can be written as
  \begin{equation}
  \begin{aligned}
  U_{c_{\mathrm{g\mu}}} &= \frac{1}{L_xL_y}\frac{1}{4\pi^{\frac{5}{2}}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i \int_{\mathbb{R}^3}d^3r\int_0^\infty dt \ t^{-\frac{1}{2}}\times \\
  &\times \sum_{\vec{k}}\Biggl\{\biggl[-\mathrm{i}\frac{1}{2t}h_x\mu_x - \mathrm{i}\frac{1}{2t}h_y\mu_y + (z_{ij} - z)\mu_z\biggr] \times \\
  &\times \exp\Bigl[-(z_{ij}-z)^2t\Bigr]\exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot(\vec{r}_{ij} - \vec{r})\biggr]\Biggl\} \times \\
  &\times \int_{\mathbb{R}^3}d^3v \exp\biggl[-\frac{|\vec{v}|^2}{4\eta_i^2} + \mathrm{i} \vec{v} \cdot \vec{r} \biggr] = \\
  &= \frac{1}{L_xL_y}\frac{1}{8\pi^3}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i \int^{\infty}_{-\infty}du\int_{\mathbb{R}^3}d^3r\int_0^\infty dt \ t^{-1}\times \\
  &\times \sum_{\vec{k}}\Biggl\{\biggl[-\mathrm{i}\frac{1}{2t}h_x\mu_x - \mathrm{i}\frac{1}{2t}h_y\mu_y -\mathrm{i}\frac{1}{2t}u \mu_z\biggr] \times \\
  &\times \exp\Bigl[-\frac{u^2}{4t} + \mathrm{i}u(z_{ij} - z)\Bigr]\exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot(\vec{r}_{ij} - \vec{r})\biggr]\Biggl\} \times \\
  &\times \int_{\mathbb{R}^3}d^3v \exp\biggl[-\frac{|\vec{v}|^2}{4\eta_i^2} + \mathrm{i} \vec{v} \cdot \vec{r} \biggr]
  \end{aligned}
  \end{equation}
  If we now define $\vec{\kappa} = (2\pi k_x/L_x, 2\pi k_y/L_y, u)=(h_x,h_y,u)$ and reorganize some prefactors, we can write the following expression for the energy
  \begin{equation}
  \label{eq:energy_after_substitution_intermediate_one}
  \begin{aligned}
  U_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{16\pi^3}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i \int^{\infty}_{-\infty}du\int_{\mathbb{R}^3}d^3r\int_0^\infty dt \ t^{-2}\times \\
  &\times\sum_{\vec{k}} (\vec{\mu}_j \cdot \vec{\kappa}) \exp\biggl[-\frac{|\vec{\kappa}|^2}{4t} + \mathrm{i}\vec{\kappa}\cdot(\vec{r}_{ij} - \vec{r})\biggr] \times \\
  &\times\int_{\mathbb{R}^3}d^3v \exp\biggl[-\frac{|\vec{v}|^2}{4\eta_i^2} + \mathrm{i} \vec{v} \cdot \vec{r} \biggr]
  \end{aligned}
  \end{equation}
  At this point we can get rid of the integral in $d^3r$ and $d^3v$ using the integral representation of the three-dimensional Dirac's delta
  \begin{equation}
  \label{eq:Dirac_delta_3D}
  \delta^3(\vec{r} - \vec{r}_0) = \frac{1}{8\pi^3}\int_{\mathbb{R}^3}d^3k \exp[-\mathrm{i}(\vec{r}-\vec{r}_0)\cdot \vec{k}]
  \end{equation}
  Reorganizing factors in eq.~\eqref{eq:energy_after_substitution_intermediate_one} we have
  \begin{equation}
  \begin{aligned}
  U_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{16\pi^3}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i \int^{\infty}_{-\infty}du\int_0^\infty dt \ t^{-2}\times \\
  &\times\sum_{\vec{k}} (\vec{\mu}_j \cdot \vec{\kappa}) \exp\biggl[-\frac{|\vec{\kappa}|^2}{4t} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr] \times \\
  &\times\int_{\mathbb{R}^3}d^3v \exp\biggl[-\frac{|\vec{v}|^2}{4\eta_i^2}\biggr]\int_{\mathbb{R}^3}d^3r\exp\Bigl[-\mathrm{i}(\vec{\kappa} - \vec{v})\cdot\vec{r}\Bigr]
  \end{aligned}
  \end{equation}
  and substituting eq.~\eqref{eq:Dirac_delta_3D}, recalling that $\delta^3(\vec{r}) = \delta^3(-\vec{r})$, we obtain
  \begin{equation}
  \begin{aligned}
  U_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i \int^{\infty}_{-\infty}du\int_0^\infty dt \ t^{-2}\times \\
  &\times\sum_{\vec{k}} (\vec{\mu}_j \cdot \vec{\kappa}) \exp\biggl[-\frac{|\vec{\kappa}|^2}{4t} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr] \times \\
  &\times\int_{\mathbb{R}^3}d^3v \exp\biggl[-\frac{|\vec{v}|^2}{4\eta_i^2}\biggr]\delta^3(\vec{\kappa} - \vec{v}) = \\
  &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i \int^{\infty}_{-\infty}du\int_0^\infty dt \ t^{-2}\times \\
  &\times\sum_{\vec{k}} (\vec{\mu}_j \cdot \vec{\kappa}) \exp\biggl[-\frac{|\vec{\kappa}|^2}{4t} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr] \exp\left[-\frac{|\vec{\kappa}|^2}{4\eta_i^2}\right]  = \\
  &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i \int^{\infty}_{-\infty}du\int_0^\infty dt \ t^{-2}\times \\
  &\times\sum_{\vec{k}} (\vec{\mu}_j \cdot \vec{\kappa}) \exp\biggl[-\frac{|\vec{\kappa}|^2}{4}\Bigl(\frac{1}{t} + \frac{1}{\eta_i^2}\Bigr) + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr]\\
  \end{aligned}
  \end{equation}
  We now consider the integral
  \begin{equation}
  \int_0^\infty dt \ t^{-2}\exp\biggl[-\frac{|\vec{\kappa}|^2}{4}\Bigl(\frac{1}{t} + \frac{1}{\eta_i^2}\Bigr) + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr]
  \end{equation}
  Performing the change of variables
  \begin{equation}
  t' = \biggl[\frac{1}{t} + \frac{1}{\eta_i^2}\biggr]^{-1} = \biggl[\frac{\eta_i^2 + t}{\eta_i^2t}\biggr]^{-1} = \frac{\eta_i^2t}{\eta_i^2 + t}
  \end{equation}
  which implies
  \begin{equation}
  dt' = \frac{d}{dt}\biggl[\frac{\eta_i^2t}{\eta_i^2 + t}\biggr]dt = \biggl[\frac{\eta_i^2(\eta_i^2 + t) - \eta_i^2t}{(\eta_i^2 + t)^2}\biggr]dt = \biggl[\frac{\eta_i^4}{(\eta_i^2 + t)^2}\biggr]dt
  \end{equation}
  and in turn
  \begin{equation}
  dt = \biggl[\frac{\eta_i^4}{(\eta_i^2 + t)^2}\biggr]^{-1}dt' = \biggl[\frac{(\eta_i^2 + t)^2}{\eta_i^4}\biggr]dt'
  \end{equation}
  so that the integral becomes
  \begin{equation}
  \begin{aligned}
  &\int dt \ t^{-2}\exp\biggl[-\frac{|\vec{\kappa}|^2}{4}\Bigl(\frac{1}{t} + \frac{1}{\eta_i^2}\Bigr) + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr] = \\
  &=\int dt'\frac{(\eta_i^2 + t)^2}{\eta_i^4t^2} \exp\biggl[-\frac{|\vec{\kappa}|^2}{4t'} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr] = \\
  &=\int dt'\biggl[\frac{(\eta_i^2 + t)}{\eta_i^2t}\biggr]^2 \exp\biggl[-\frac{|\vec{\kappa}|^2}{4t'} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr] = \\
  &=\int dt'(t')^{-2} \exp\biggl[-\frac{|\vec{\kappa}|^2}{4t'} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr] \\
  \end{aligned}
  \end{equation}
  Finally, the extrema can be obtained as
  \begin{equation}
  \begin{aligned}
  t&=0 \Rightarrow t' = 0 \\
  t&\to\infty \Rightarrow t' = \lim_{t\to\infty}\frac{\eta_i^2t}{\eta_i^2 + t} = \lim_{t\to\infty}\frac{t\eta_i^2}{t(\frac{\eta_i^2}{t} + 1)} = \eta^2_i
  \end{aligned}
  \end{equation}
  So that the integral after the change of variables is given by
  \begin{equation}
  \int_0^{\eta^2_i} dt \ t^{-2} \exp\biggl[-\frac{|\vec{\kappa}|^2}{4t} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr]
  \end{equation}  
  where the prime has been dropped since no ambiguity is produced.
  
  The form of the energy at this point is given by
  \begin{equation}
  \begin{aligned}
  U_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i \int^{\infty}_{-\infty}du\int_0^{\eta^2_i} dt \ t^{-2}\sum_{\vec{k}} (\vec{\mu}_j \cdot \vec{\kappa}) \exp\biggl[-\frac{|\vec{\kappa}|^2}{4t} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr]\\
  \end{aligned}
  \end{equation}
  We now split the contribution to the energy in a long-range and a short-range part, which can be done by dividing the integral in $dt$ introducing the Ewald cut-off parameter $\alpha$
  \begin{equation}
  \begin{aligned}
  U_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}} Q_i(\vec{\mu}_j \cdot \vec{\kappa}) \times \\
  &\times\Biggl\{\int_0^{\alpha^2} dt \ t^{-2}\exp\biggl[-\frac{|\vec{\kappa}|^2}{4t} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr] + \int_{\alpha^2}^{\eta_i^2} dt \ t^{-2}\exp\biggl[-\frac{|\vec{\kappa}|^2}{4t} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr]\Biggr\}\\
  \end{aligned}
  \end{equation}
  where it is easy to identify
  \begin{equation}
  \begin{aligned}
  U^{\mathrm{sr}}_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}} Q_i(\vec{\mu}_j \cdot \vec{\kappa})\int_{\alpha^2}^{\eta_i^2} dt \ t^{-2}\exp\biggl[-\frac{|\vec{\kappa}|^2}{4t} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr]\\
  U^{\mathrm{lr}}_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}} Q_i(\vec{\mu}_j \cdot \vec{\kappa}) \int_0^{\alpha^2} dt \ t^{-2}\exp\biggl[-\frac{|\vec{\kappa}|^2}{4t} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr]\\
  \end{aligned}
  \end{equation}
  To obtain a useful expression for the long-range part it is necessary to treat separately the $\vec{k} = 0$ term and the rest of the sum over the wavevectors in reciprocal space. In this fashion, we write down the long-range part of the energy as $U^{\mathrm{lr}}_{c_{\mathrm{g\mu}}} = U^{\mathrm{lr, *}}_{c_{\mathrm{g\mu}}} +U^{\mathrm{lr, 0}}_{c_{\mathrm{g\mu}}}$ where 
  \begin{equation}
  \begin{aligned}
  U^{\mathrm{lr, *}}_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{\kappa})\int_0^{\alpha^2} dt \ t^{-2}\exp\biggl[-\frac{|\vec{\kappa}|^2}{4t} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr] \\
  U^{\mathrm{lr, 0}}_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \int^{\infty}_{-\infty}du \ Q_i(\mu^z_j u) \int_0^{\alpha^2} dt \ t^{-2}\exp\biggl[-\frac{u^2}{4t} + \mathrm{i}uz_{ij}\biggr] \\
  \end{aligned}
  \end{equation}
  Focusing for the moment on the $\vec{k} = 0$ term we can contract the integral in the $u$ variable using the Gaussian integral formula in eq.~\eqref{eq:Gaussian_linear_integral_1D} and, solving the integral in $dt$, we obtain
  \begin{equation}
  \begin{aligned}
  U^{\mathrm{lr, 0}}_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} Q_i\mu^z_j \int_0^{\alpha^2} dt \ t^{-2}\int^{\infty}_{-\infty}du\ u\exp\biggl[-\frac{u^2}{4t} + \mathrm{i}uz_{ij}\biggr] =\\
  &= \frac{2\sqrt{\pi}}{L_xL_y}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} Q_i\mu^z_jz_{ij} \int_0^{\alpha^2} dt \ t^{-\frac{1}{2}}\exp\bigl[-z^2_{ij}t\bigr] =\\
  &= \frac{2\pi}{L_xL_y}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} Q_i\mu^z_j\sgn[z_{ij}]\erf\bigl[\alpha |z_{ij}|\bigr] = \\
  &=\frac{2\pi}{L_xL_y}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} Q_i\mu^z_j\erf\bigl[\alpha z_{ij}\bigr]
  \end{aligned}
  \end{equation}
  where $\sgn[z_{ij}] = z_{ij}/|z_{ij}|$ and the last equality follows from the oddity of the error function under change of sign of its argument.
  
  The rest of the long-range part is treated just like the case of the point-charge-to-point-charge interaction since the form is the same with the exception of the scalar product $(\vec{\mu}_j \cdot \vec{\kappa})$ in place of the second charge. We solve the integral in $dt$ as a first step, splitting now the contributions directly dependent on $\vec{k}$ and the ones depending on $u$. Recalling that $\vec{h} = (2\pi \frac{k_x}{L_x}, 2\pi \frac{k_y}{L_y}, 0)$ we have that
  \begin{equation}
  \begin{aligned}
  U^{\mathrm{lr, *}}_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\times \\
  &\times\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{ij} + uz_{ij})\bigr]\int_0^{\alpha^2} dt \ t^{-2}\exp\biggl[-\frac{|\vec{h}|^2 + u^2}{4t}\biggr] = \\
  &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\times \\
  &\times\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{ij} + uz_{ij})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} = \\
  &= -\frac{1}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\times \\
  &\times\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{ij} + uz_{ij} + \frac{\pi}{2})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} = \\
  &= -\frac{1}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\times \\
  &\times\bigl[\cos(\vec{h}\cdot\vec{r}_{ij} + uz_{ij} + \frac{\pi}{2}) + \mathrm{i}\sin(\vec{h}\cdot\vec{r}_{ij} + uz_{ij} + \frac{\pi}{2})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} = \\
  &= -\frac{1}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\times \\
  &\times\bigl[\mathrm{i}\cos(\vec{h}\cdot\vec{r}_{ij} + uz_{ij}) - \sin(\vec{h}\cdot\vec{r}_{ij} + uz_{ij})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} = \\
  &= \frac{1}{2L_xL_y}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\times \\
  &\times\bigl[\sin(\vec{h}\cdot\vec{r}_{ij} + uz_{ij})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} \\
  \end{aligned}
  \end{equation}
  where it has been recognized that $\mathrm{i} = \exp[\mathrm{i}\pi/2]$ and, in the last equality, the oddity of the function $(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\mathrm{i}\cos(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)$ with respect to its argument and the symmetry of the domain of integration/summation has been used. In addition, it is possible to recognize that
  \begin{equation}
  \begin{aligned}
  \sin(\vec{h}\cdot\vec{r}_{ij} + uz_{ij}) = &\sin(\vec{h}\cdot\vec{r}_i + uz_i)\cos(\vec{h}\cdot\vec{r}_j + uz_j) +\\
  &- \cos(\vec{h}\cdot\vec{r}_i + uz_i)\sin(\vec{h}\cdot\vec{r}_j + uz_j)
  \end{aligned}
  \end{equation}
  Reorganizing the sums over melt particles and electrode atoms, it is easy to see that the expression for the long-range term of the energy can be written as
  \begin{equation}
  \label{eq:2D_lr_implementation}
  \begin{aligned}
  U^{\mathrm{lr, *}}_{c_{\mathrm{g\mu}}} &= \frac{1}{2L_xL_y}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} \frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2}\times \\
  &\times\biggl[\sum_{i=1}^{N_e}Q_i\sin(\vec{h}\cdot\vec{r}_i + uz_i)\sum_{j=1}^{N_p}(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\cos(\vec{h}\cdot\vec{r}_j + uz_j) +\\
  &- \sum_{i=1}^{N_e}Q_i\cos(\vec{h}\cdot\vec{r}_i + uz_i)\sum_{j=1}^{N_p}(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\sin(\vec{h}\cdot\vec{r}_j + uz_j)\biggr] \\
  \end{aligned}
  \end{equation}
  Since here we exploited the definition of $\vec{r}_{ij}$, there is no sign difference with Abel's expression for this term when written in this form. Nonetheless, while in the actual implementation of the code this form of the expression is very useful, in what follows we will keep using the exponential notation 
    \begin{equation}
  \begin{aligned}
  U^{\mathrm{lr, *}}_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\times \\
  &\times\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{ij} + uz_{ij})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2}
  \end{aligned}
  \end{equation}
  since it is shorter to write and easier to handle for the analytic calculations.
  
  The short-range part
  \begin{equation}
  \begin{aligned}  
  U^{\mathrm{sr}}_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}} Q_i(\vec{\mu}_j \cdot \vec{\kappa})\int_{\alpha^2}^{\eta_i^2} dt \ t^{-2}\exp\biggl[-\frac{|\vec{\kappa}|^2}{4t} + \mathrm{i}\vec{\kappa}\cdot\vec{r}_{ij}\biggr]\\
  \end{aligned}
  \end{equation}
  is a bit more tricky to handle since it is necessary to go back into real space. We first contract the integral over $du$ exploiting the inverse of the transformation in eq.~\eqref{eq:Gaussian_linear_integral_1D}
  \begin{equation}
  \begin{aligned}  
  U^{\mathrm{sr}}_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}} Q_i(\vec{\mu}_j \cdot \vec{h} + \mu_j^zu) \times \\
  &\times \int_{\alpha^2}^{\eta_i^2} dt \ t^{-2}\exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot\vec{r}_{ij}\biggr]\exp\biggl[-\frac{u^2}{4t} + \mathrm{i}uz_{ij}\biggr] = \\
  &= -\frac{\mathrm{i}\sqrt{\pi}}{L_xL_y}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{k}}\int_{\alpha^2}^{\eta_i^2} dt \ t^{-\frac{3}{2}} \exp\bigl[-z_{ij}^2t\bigr]\times \\
  &\times Q_i(\vec{\mu}_j \cdot \vec{h} + 2\mathrm{i}t\mu_j^zz_{ij})\exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot\vec{r}_{ij}\biggr] = \\
  \end{aligned}
  \end{equation}
  and then we go back from a summation in reciprocal space to a summation in real space using eq.~\eqref{eq:Poisson_Summation_Formula_final}
  \begin{equation}
   \label{eq:short_range_final_minusone}
  \begin{aligned}  
  U^{\mathrm{sr}}_{c_{\mathrm{g\mu}}} &= -\frac{\mathrm{i}\sqrt{\pi}}{L_xL_y}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{k}}\int_{\alpha^2}^{\eta_i^2} dt \ t^{-\frac{3}{2}}\exp\bigl[-z_{ij}^2t\bigr] \times \\
  &\times Q_i(\vec{\mu}_j \cdot \vec{h} + 2\mathrm{i}t\mu_j^zz_{ij}) \exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot\vec{r}_{ij}\biggr] = \\
  &= -\frac{\mathrm{i}\sqrt{\pi}}{L_xL_y}\frac{L_xL_y}{\pi}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}}\int_{\alpha^2}^{\eta_i^2} dt \ t^{-\frac{3}{2} + 1} \exp\Bigl[-(\vec{r}_{ij} + \vec{m})^2t\Bigr]\times \\
  &\times Q_i(2\mathrm{i}t\mu_j^x(x_{ij} + n_xL_x) + 2\mathrm{i}t\mu_j^y(y_{ij} + n_yL_y) + 2\mathrm{i}t\mu_j^zz_{ij}) = \\
  &= \frac{2}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}}Q_i[\vec{\mu}_j\cdot(\vec{r}_{ij} + \vec{m})]\int_{\alpha^2}^{\eta_i^2} dt \ t^{\frac{1}{2}} \exp\Bigl[-(\vec{r}_{ij} + \vec{m})^2t\Bigr] \\
  \end{aligned}
  \end{equation}
%  Considering minimum image convention it is possible to drop the summation over the boxes since only the closest image of each particle is included. It is then possible to perform the integral over $dt$ to obtain
%  \begin{equation}
%  \begin{aligned}  
%  U^{\mathrm{sr}}_{c_{\mathrm{g\mu}}} &= -\frac{2}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} Q_i\vec{\mu}_j\cdot\vec{r}_{ij}\int_{\alpha^2}^{\eta_i^2} dt \ t^{\frac{1}{2}} \exp\Bigl[-\vec{r}^2_{ij}t\Bigr] \\
%  \end{aligned}
%  \end{equation}
  To solve the integral we perform the substitution $u^2 = |\vec{r}_{ij}+\vec{m}|^2t$ which implies $2udu = |\vec{r}_{ij}+\vec{m}|^2dt$ for the differential and $t = \alpha^2 \Rightarrow u = \alpha |\vec{r}_{ij}+\vec{m}|$ and $t = \eta_i^2 \Rightarrow u = \eta_i |\vec{r}_{ij}+\vec{m}|$ for the extrema. The integral in $dt$ then becomes
   \begin{equation}
  \label{eq:short_range_integral_final}
   \begin{aligned}
   &\int_{\alpha^2}^{\eta_i^2} dt \ t^{\frac{1}{2}} \exp\Bigl[-|\vec{r}_{ij}+\vec{m}|^2t\Bigr] = \int_{\alpha |\vec{r}_{ij}+\vec{m}|}^{\eta_i |\vec{r}_{ij}+\vec{m}|} du \frac{2u^2\exp[-u^2] }{|\vec{r}_{ij}+\vec{m}|^3} = \\
   &= \frac{2}{|\vec{r}_{ij}+\vec{m}|^3} \int_{\alpha |\vec{r}_{ij}+\vec{m}|}^{\eta_i |\vec{r}_{ij}+\vec{m}|} du \ u^2 \exp[-u^2] = \\
   &= \frac{2}{|\vec{r}_{ij}+\vec{m}|^3} \biggl[\frac{1}{4}\sqrt{\pi}\erf\bigl[u\bigr] - \frac{1}{2}u\exp\bigl[-u^2\bigr]\biggr]_{\alpha |\vec{r}_{ij}+\vec{m}|}^{\eta_i |\vec{r}_{ij}+\vec{m}|} = \\
   &= \frac{1}{2|\vec{r}_{ij}+\vec{m}|^3} \biggl[\sqrt{\pi}\erf\bigl[\eta_i |\vec{r}_{ij}+\vec{m}|\bigr] - 2\eta_i |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ij}+\vec{m}|^2\bigr] + \\
   &-\sqrt{\pi}\erf\bigl[\alpha |\vec{r}_{ij}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ij}+\vec{m}|^2\bigr]\biggr]
   \end{aligned}
   \end{equation}
   For numerical reasons, in the actual implementation, it is better to work with complementary error function instead of error function. Therefore, using the relation $1 - \erfc[x] = \erf[x]$ and substituting eq.~\eqref{eq:short_range_integral_final} into eq.~\eqref{eq:short_range_final_minusone} we obtain
   \begin{equation}
   \begin{aligned}
  U^{\mathrm{sr}}_{c_{\mathrm{g\mu}}} &= \frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}} \frac{Q_i\vec{\mu}_j\cdot(\vec{r}_{ij} + \vec{m})}{|\vec{r}_{ij}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ij}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ij}+\vec{m}|^2 \bigr]+ \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ij}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ij}+\vec{m}|^2\bigr]\Bigr)\biggr]\\
   \end{aligned}
   \end{equation}
   Putting together all the pieces, the final expression for the interaction energy between the dipoles associated to point charges and Gaussian-distributed charges with two-dimensional periodic boundary conditions is written as
   \begin{equation}
   \label{eq:2D_final}
   \begin{aligned}
  U_{c_{\mathrm{g\mu}}} &= \Biggl\{\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}} \frac{Q_i\vec{\mu}_j\cdot(\vec{r}_{ij} + \vec{m})}{|\vec{r}_{ij}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ij}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ij}+\vec{m}|^2 \bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ij}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ij}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\}+ \\
  &+ \Biggl\{ -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\times \\
  &\times\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{ij} + uz_{ij})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} \Biggr\} + \Biggl\{ \frac{2\pi}{L_xL_y}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} Q_i\mu^z_j\erf\bigl[\alpha z_{ij}\bigr] \Biggr\}
   \end{aligned}
   \end{equation}
   
   In \mw, since Abel's convention on $\vec{r}_{ij}$ is followed, the energy is implemented with the opposite sign. See also eq.~\eqref{eq:Qelec2mumelt_energy} and comments below.
  
  \section{Three-dimensional Periodic Boundary Conditions}
When periodic boundary conditions are enforced on the whole space the vectors $\vec{n}$ and $\vec{k}$ are given by $\vec{n} = (n_x, n_y, n_z)$ and $\vec{k} = (k_x, k_y, k_z)$, respectively, so that the sum in eq.~\eqref{eq:sum_over_boxes} can actually be expanded as
  \begin{equation}
  \begin{aligned}
  &\sum_{\vec{n}}\vec{\mu}\cdot(\vec{r} + \vec{m})\exp\Bigl[-(\vec{r} + \vec{m})^2t\Bigr] = \\
  &= \sum_{n_x}\sum_{n_y}\sum_{n_z} \bigl[\mu_x(x+n_xL_x) + \mu_y(y+n_yL_y) + \mu_z(z + n_zL_z)\bigr] \times \\
  &\times \exp\Bigl[-(x + n_xL_x)^2t\Bigr]\exp\Bigl[-(y + n_y L_y)^2t\Bigr]\exp\Bigl[-(z + n_zL_z)^2t\Bigr]
  \end{aligned}
  \end{equation}  
The $\mathcal{F}$ operator can now be applied on the rest of the expression using again linearity of the Fourier transform and factorizability of the expression in the three variables $x$, $y$ and $z$ to get
  \begin{equation}
  \begin{aligned}
  &\mathcal{F}\biggl[\sum_{\vec{n}}\vec{\mu}\cdot(\vec{r} + \vec{m})\exp\Bigl[-(\vec{r} + \vec{m})^2t\Bigr]\biggr] = \\
  &=\sum_{n_x}\sum_{n_y}\sum_{n_z} \mathcal{F}\biggl[\bigl[\mu_x(x+n_xL_x) + \mu_y(y+n_yL_y) + \mu_z(z+n_zL_z)\bigr] \times \\
  &\times \exp\Bigl[-(x + n_xL_x)^2t\Bigr]\exp\Bigl[-(y + n_y L_y)^2t\Bigr]\exp\Bigl[-(z + n_z L_z)^2t\Bigr]\biggr] = \\
  &= -\mathrm{i}\frac{1}{2V}\biggl(\frac{\pi^{3}}{t^{5}}\biggr)^{\frac{1}{2}}\sum_{\vec{k}} (\vec{\mu} \cdot \vec{h})\exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot\vec{r}\biggr]
  \end{aligned}
  \end{equation}
  where $V = L_xL_yL_z$ is the volume of the system and $\vec{h} = 2\pi\vec{k}/\vec{L}$ as before. 
  
  The final result comes from the fact that the application of the $\mathcal{F}$ operator to the expression above results in the sum of three terms of the form
  \begin{equation}
  \begin{aligned}
  &-\mathrm{i}\frac{\pi^{\frac{3}{2}}t^{\frac{1}{2}}}{L^2_\alpha t^2}k_\alpha\mu^\alpha\exp\biggl[-\frac{\pi^2k_\alpha^2}{L_\alpha^2t} + \mathrm{i}\frac{2\pi k_\alpha\alpha}{L_\alpha}\biggr] \times \\
  &\frac{1}{L_\beta}\biggl(\frac{\pi}{t}\biggr)^{\frac{1}{2}}\exp\biggl[-\frac{\pi^2k_\beta^2}{L_\beta^2t} + \mathrm{i}\frac{2\pi k_\beta\beta}{L_\beta}\biggr] \times \\
  &\frac{1}{L_\gamma}\biggl(\frac{\pi}{t}\biggr)^{\frac{1}{2}}\exp\biggl[-\frac{\pi^2k_\gamma^2}{L_\gamma^2t} + \mathrm{i}\frac{2\pi k_\gamma\gamma}{L_\gamma}\biggr]
  \end{aligned}
  \end{equation}
  where the indexes $\{\alpha, \beta, \gamma\}$ are a cyclic permutation of $\{x,y,z\}$. Each of the three terms then contributes as
  \begin{equation}
  \begin{aligned}
  &-\mathrm{i}\frac{1}{2V}\frac{\pi^{\frac{3}{2}}}{t^{\frac{5}{2}}}h_\alpha\mu^\alpha \exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot\vec{r}\biggr] \qquad \alpha\in\{x,y,z\}
  \end{aligned}
  \end{equation}
  
  Using this formula in the expression of the energy, together with eqs.~\eqref{eq:Gaussian_integral_3D_final} and~\eqref{eq:Gamma_functions_final} yields
  \begin{equation}
  \begin{aligned}
  U_{c_{\mathrm{g\mu}}} &= \sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i\Bigl(\frac{\eta^2_i}{\pi}\Bigr)^{\frac{3}{2}}\sum_{\vec{n}}\int_{\mathbb{R}^3}d^3r\frac{(\vec{r}_{ij} - \vec{r} + \vec{m})\cdot\vec{\mu}_j}{|\vec{r}_{ij} - \vec{r} + \vec{m}|^3}e^{-\eta_i^2 |\vec{r}|^2} = \\
&= -\frac{i}{8\pi^{2}V}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}Q_i \int_{\mathbb{R}^3}d^3r\int_0^\infty dt \ t^{-2}\times \\
  &\times \sum_{\vec{k}} (\vec{\mu}_j \cdot \vec{h})\exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot(\vec{r}_{ij} - \vec{r})\biggr] \int_{\mathbb{R}^3}d^3v \exp\biggl[-\frac{|\vec{v}|^2}{4\eta_i^2} + \mathrm{i} \vec{v} \cdot \vec{r} \biggr]
  \end{aligned}
  \end{equation}
  We now use the integral definition of the three-dimensional Dirac's delta function eq.~\eqref{eq:Dirac_delta_3D} to remove the integrals over $d^3r$ and $d^3v$. Proceeding exactly in the same way as the two-dimensional case we obtain
  \begin{equation}
  \begin{aligned}
  U_{c_{\mathrm{g\mu}}} &= -\mathrm{i}\frac{\pi}{V}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{k}} Q_i(\vec{\mu}_j \cdot \vec{h}) \int_0^\infty dt \ t^{-2}\exp\biggl[-\frac{|\vec{h}|^2}{4}\biggl(\frac{1}{t} + \frac{1}{\eta_i^2}\biggr) + \mathrm{i}\vec{h}\cdot\vec{r}_{ij}\biggr]
  \end{aligned}
  \end{equation}
  The same change of variables performed in the two-dimensional case leads to the expression
  \begin{equation}
  \begin{aligned}
  U_{c_{\mathrm{g\mu}}} &= -\mathrm{i}\frac{\pi}{V}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{k}} Q_i(\vec{\mu}_j \cdot \vec{h}) \int_0^{\eta_i^2} dt \ t^{-2}\exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot\vec{r}_{ij}\biggr]
  \end{aligned}
  \end{equation}
  As done before, we split the contribution in long- and short-range parts, introducing the Ewald cut-off parameter $\alpha$ in the integral in $dt$ to obtain
  \begin{equation}
  \begin{aligned}
  U_{c_{\mathrm{g\mu}}} &= \Biggl\{-\mathrm{i}\frac{\pi}{V}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{k}} Q_i(\vec{\mu}_j \cdot \vec{h}) \int_0^{\alpha^2} dt \ t^{-2}\exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot\vec{r}_{ij}\biggr]\Biggr\} + \\
  &+\Biggl\{-\mathrm{i}\frac{\pi}{V}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{k}} Q_i(\vec{\mu}_j \cdot \vec{h}) \int_{\alpha^2}^{\eta_i^2} dt \ t^{-2}\exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot\vec{r}_{ij}\biggr] \Biggr\}
  \end{aligned}
  \end{equation}
  To compute the short-range term we go back in real space and we obtain
  \begin{equation}
  \begin{aligned}
  U^{\mathrm{sr}}_{c_{\mathrm{g\mu}}} &= \frac{2}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{n}} [Q_i\vec{\mu}_j \cdot (\vec{r}_{ij} + \vec{m})] \int_{\alpha^2}^{\eta_i^2} dt \ t^{\frac{1}{2}}\exp\bigl[-(\vec{r}_{ij}+\vec{m})^2t\bigr]
  \end{aligned}
  \end{equation}
  The integral in $dt$ is computed as in the two-dimensional case and, after the change of variables and the substitution $\erf[x] \to \erfc[x]$, we get
   \begin{equation}
   \begin{aligned}
  U^{\mathrm{sr}}_{c_{\mathrm{g\mu}}} &= \frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}} \frac{Q_i\vec{\mu}_j\cdot(\vec{r}_{ij} + \vec{m})}{|\vec{r}_{ij}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ij}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ij}+\vec{m}|^2 \bigr]+ \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ij}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ij}+\vec{m}|^2\bigr]\Bigr)\biggr]\\
   \end{aligned}
   \end{equation}
  which is indeed the same expression obtained for the two-dimensional periodic boundary conditions case.  
  
  As for the long-range part we have that
   \begin{equation}
   \begin{aligned}
  U^{\mathrm{lr}}_{c_{\mathrm{g\mu}}} &= -\mathrm{i}\frac{\pi}{V}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{k}} Q_i(\vec{\mu}_j \cdot \vec{h}) \int_0^{\alpha^2} dt \ t^{-2}\exp\biggl[-\frac{|\vec{h}|^2}{4t} + \mathrm{i}\vec{h}\cdot\vec{r}_{ij}\biggr] = \\
  &= -\mathrm{i}\frac{\pi}{V}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{k}} Q_i(\vec{\mu}_j \cdot \vec{h}) \exp\bigl[\mathrm{i}\vec{h}\cdot\vec{r}_{ij}\bigr]\int_0^{\alpha^2} dt \ t^{-2}\exp\biggl[-\frac{|\vec{h}|^2}{4t}\biggr]
  \end{aligned}
  \end{equation}
  Again the sum over wavevectors should be split in $\vec{k} = 0$ and $\vec{k} \neq 0$ terms, but this time the $\vec{k} = 0$ can be trivially set to zero assuming a globally neutral system. The term $\vec{k}\neq0$ can be written as
  \begin{equation}
  \begin{aligned}
  U^{\mathrm{lr, *}}_{c_{\mathrm{g\mu}}} &= -\mathrm{i}\frac{4\pi}{V}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h}) \exp\bigl[\mathrm{i}\vec{h}\cdot\vec{r}_{ij}\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2}
  \end{aligned}
  \end{equation}
  The same procedure adopted for the two-dimensional case that yielded eq.~\eqref{eq:2D_lr_implementation} can be used here and the result is what will be used in the actual implementation.
  
  Putting together the two pieces, the final expression for the interaction energy between the dipoles associated to point charges and Gaussian-distributed charges with three-dimensional periodic boundary conditions is written as
  \begin{equation}
   \label{eq:3D_final}
  \begin{aligned}
  U_{c_{\mathrm{g\mu}}} &= \Biggl\{\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}} \frac{Q_i\vec{\mu}_j\cdot(\vec{r}_{ij} + \vec{m})}{|\vec{r}_{ij}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ij}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ij}+\vec{m}|^2\bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ij}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ij}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\} +\\
  &+ \Biggl\{-\mathrm{i}\frac{4\pi}{V}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h}) \exp\bigl[\mathrm{i}\vec{h}\cdot\vec{r}_{ij}\bigr]\frac{\exp\bigl[-\frac{|\vec{h}|^2}{4\alpha^2}\bigr]}{|\vec{h}|^2}\Biggr\}
  \end{aligned}
  \end{equation}
  Again, due to the convention adopted by Abel in defining the distance $\vec{r}_{ij}$ the expression implemented in \mw~will have the opposite sign compared to this one.
  
  \chapter{Forces due to Interaction between Electrode Charges and Dipoles}
  As usual the forces are computed as minus the gradient of the potential with respect to the spatial coordinates. This implies that, starting from the expressions in eqs.~\eqref{eq:2D_final} and~\eqref{eq:3D_final}, the forces are computed as
  \begin{equation}
  \vec{F}_k(\vec{r}) = -\vec{\nabla}_{\vec{r}_k} U_{c_{\mathrm{g\mu}}} \qquad k = 1,\dots,N_p
  \end{equation}
  There are four fundamental pieces in the computation of the derivative with respect to the nuclei positions of the energy
  \begin{subequations}
  \label{eq:r_derivatives}
  \begin{align}
  \vec{\nabla}_{\vec{r}}&\biggl[\exp[\mathrm{i}(\vec{h}\cdot\vec{r})]\biggr] = \mathrm{i}\vec{h}\exp[\mathrm{i}(\vec{h}\cdot\vec{r})] \\
  \vec{\nabla}_{\vec{r}}&\biggl[\frac{(\vec{\mu}\cdot\vec{r})}{|\vec{r}|^3}\biggr] = \frac{1}{|\vec{r}|^3}\biggl[\vec{\mu} - \frac{3(\vec{\mu}\cdot\vec{r})}{|\vec{r}|^2}\vec{r}\biggr] \\
  \vec{\nabla}_{\vec{r}}&\biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}|\bigr] + 2\alpha |\vec{r}|\exp\bigl[-\alpha^2 |\vec{r}\|^2\bigr]\biggr] = \nonumber \\
  & = -4\alpha^3|\vec{r}|\exp\bigl[-\alpha^2|\vec{r}|^2\bigr]\vec{r} \\
  \vec{\nabla}_{\vec{r}}&\biggl[\pi\erf[\alpha z]\biggr] = 2\alpha\sqrt{\pi}\exp\bigl[-\alpha^2 z^2\bigr]\hat{z}
  \end{align}
  \end{subequations}
  where $\hat{z} = (0,0,1)$. In the calculations that follow it is crucial to keep in mind the notation used for the distance. Indeed, since here $\vec{r}_{ij} = \vec{r}_i - \vec{r}_j$ and since we are consistently using the index $i$ for the electrode charges and the index $j$ for the particles in the melt, we will have a minus in each expression arising from the fact that we are considering the force acting on the melt particles and not on electrode charges. In particular this means that, writing just for this time $\vec{r}_{ij} = \vec{R}_i - \vec{r}_j$ to highlight the difference, where $\vec{R}_i$ is the (fixed) position of the $i$-th electrode, each function of the distance $\vec{r}_{ij}$ that we derive, will be expressed as
  \begin{equation}
  \vec{\nabla}_{\vec{r}_k} f(\vec{r}_{ij}) = \vec{\nabla}_{\vec{r}_{ij}}f(\vec{r}_{ij})\vec{\nabla}_{\vec{r}_k}(\vec{R}_i - \vec{r}_j) = -1\delta^3_{jk}\vec{\nabla}_{\vec{r}_{ij}}f(\vec{r}_{ij})
  \end{equation}
  
  \section{Periodic Boundary Conditions on the $xy$-plane.}
  Using eqs.~\eqref{eq:r_derivatives} it is possible to compute the contribution to the forces on the melt due to the interaction term between the electrode charges and the dipoles in the system. To lighten the notation we divide the computation in the different contributions as for the computation of the energy so that $U_{c_{\mathrm{g\mu}}} = U^{\mathrm{lr, *}}_{c_{\mathrm{g\mu}}} + U^{\mathrm{lr, 0}}_{c_{\mathrm{g\mu}}} + U^{\mathrm{sr}}_{c_{\mathrm{g\mu}}}$. Taking the gradient of each term we obtain
  \begin{equation}
  \begin{aligned}
  -\vec{\nabla}_{\vec{r}_k}U^{\mathrm{lr, *}}_{c_{\mathrm{g\mu}}} = -\vec{\nabla}_{\vec{r}_k}\Biggl\{ &-\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\times \\
  &\times\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{ij} + uz_{ij})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} \Biggr\} = \\
  =\Biggl\{ &-\frac{(\vec{h} + u\hat{z})}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_k \cdot \vec{h} + u\mu_k^z)\times \\
  &\times\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{ik} + uz_{ik})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} \Biggr\}
  \end{aligned}
  \end{equation}
  \begin{equation}
  \begin{aligned}
  -\vec{\nabla}_{\vec{r}_k}U^{\mathrm{lr, 0}}_{c_{\mathrm{g\mu}}} &= -\vec{\nabla}_{\vec{r}_k} \Biggl\{\frac{2\pi}{L_xL_y}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} Q_i\mu^z_j\erf\bigl[\alpha z_{ij}\bigr] \Biggr\} = \\
  & = \Biggl\{-\frac{4\alpha\sqrt{\pi}}{L_xL_y}\sum_{i=1}^{N_e} Q_i\mu^z_k\exp\bigl[-\alpha^2 z_{ik}^2\bigr]\hat{z} \Biggr\}
  \end{aligned}
  \end{equation}
  The short-range contribution is more complex as it is the product of two functions of the distance. The rule for the derivative of a product yields
  \begin{equation}
  \begin{aligned}
  -\vec{\nabla}_{\vec{r}_k}U^{\mathrm{sr}}_{c_{\mathrm{g\mu}}} &= -\vec{\nabla}_{\vec{r}_k} \Biggl\{\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}} \frac{Q_i\vec{\mu}_j\cdot(\vec{r}_{ij} + \vec{m})}{|\vec{r}_{ij}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ij}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ij}+\vec{m}|^2 \bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ij}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ij}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\} = \\
  &= \Biggl\{-\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}} \vec{\nabla}_{\vec{r}_k}\biggl[\frac{Q_i\vec{\mu}_k\cdot(\vec{r}_{ik} + \vec{m})}{|\vec{r}_{ik}+\vec{m}|^3}\biggr] \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ik}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ik}+\vec{m}|^2 \bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ik}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ik}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\} + \\
  &+ \Biggl\{-\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{n}} \frac{Q_i\vec{\mu}_k\cdot(\vec{r}_{ik} + \vec{m})}{|\vec{r}_{ik}+\vec{m}|^3} \times \\
  &\times \vec{\nabla}_{\vec{r}_k} \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ik}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ik}+\vec{m}|^2 \bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ik}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ik}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\}
  \end{aligned}
  \end{equation}
  Solving separately the two terms in curly brackets we have
  \begin{equation}
  \begin{aligned}
  -\vec{\nabla}_{\vec{r}_k}U^{\mathrm{sr}}_{c_{\mathrm{g\mu}}} &= \Biggl\{\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e} \sum_{\vec{n}} \frac{Q_i}{|\vec{r}_{ik} + \vec{m}|^3}\biggl[\vec{\mu}_k - \frac{3[\vec{\mu}_k\cdot(\vec{r}_{ik} + \vec{m})]}{|\vec{r}_{ik} + \vec{m}|^2}(\vec{r}_{ik} + \vec{m})\biggr] \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ik}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ik}+\vec{m}|^2 \bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ik}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ik}+\vec{m}|^2\bigr]\Bigr)\biggr] + \\
  &+ \frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e} \sum_{\vec{n}}\frac{Q_i\vec{\mu}_k\cdot(\vec{r}_{ik} + \vec{m})}{|\vec{r}_{ik}+\vec{m}|^3} \biggl[4\eta_i^3|\vec{r}_{ik} + \vec{m}|\exp\bigl[-\eta_i^2|\vec{r}_{ik} + \vec{m}|^2\bigr] + \\
  &- 4\alpha^3|\vec{r}_{ik} + \vec{m}|\exp\bigl[-\alpha^2|\vec{r}_{ik} + \vec{m}|^2\bigr]\biggr](\vec{r}_{ik} + \vec{m}) \Biggr\}
  \end{aligned}
  \end{equation}
  So that the final expression for the force when periodic boundary conditions are enforced on the $xy$-plane is given by
   \begin{equation}
   \begin{aligned}
   &\vec{F}_k = -\vec{\nabla}_{\vec{r}_k}U_{c_{\mathrm{g\mu}}} = \Biggl\{\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e} \sum_{\vec{n}} \frac{Q_i}{|\vec{r}_{ik} + \vec{m}|^3}\biggl[\vec{\mu}_k - \frac{3[\vec{\mu}_k\cdot(\vec{r}_{ik} + \vec{m})]}{|\vec{r}_{ik} + \vec{m}|^2}(\vec{r}_{ik} + \vec{m})\biggr] \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ik}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ik}+\vec{m}|^2 \bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ik}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ik}+\vec{m}|^2\bigr]\Bigr)\biggr] + \\
  &+ \frac{4}{\sqrt{\pi}}\sum_{i=1}^{N_e} \sum_{\vec{n}}\frac{Q_i\vec{\mu}_k\cdot(\vec{r}_{ik} + \vec{m})}{|\vec{r}_{ik}+\vec{m}|^2} \biggl[\eta_i^3\exp\bigl[-\eta_i^2|\vec{r}_{ik} + \vec{m}|^2\bigr] - \alpha^3\exp\bigl[-\alpha^2|\vec{r}_{ik} + \vec{m}|^2\bigr]\biggr](\vec{r}_{ik} + \vec{m}) \Biggr\} + \\
  & + \Biggl\{ -\frac{(\vec{h} + u\hat{z})}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_k \cdot \vec{h} + u\mu_k^z)\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{ik} + uz_{ik})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} \Biggr\} + \\
  &+ \Biggl\{-\frac{4\alpha\sqrt{\pi}}{L_xL_y}\sum_{i=1}^{N_e} Q_i\mu^z_k\exp\bigl[-\alpha^2 z_{ik}^2\bigr]\hat{z} \Biggr\}
   \end{aligned}
   \end{equation}
   
   \section{Three-dimensional Periodic Boundary Conditions}
   The same procedure can be applied to the case when three dimensional periodic boundary conditions are enforced. The only differences with respect to two-dimensional case are given by the $\vec{k}=0$ term, which is absent in this case and by the prefactor of the long-range, $\vec{k}\neq0$ terms. The definition of the $\vec{k}$ vector is also different from the case of 2D-PBC and the integral in $du$ is not present. For this system set-up, the forces on the melt are given by
   \begin{equation}
   \begin{aligned}
   &\vec{F}_k = -\vec{\nabla}_{\vec{r}_k}U_{c_{\mathrm{g\mu}}} = \Biggl\{-\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e} \sum_{\vec{n}} \frac{Q_i}{|\vec{r}_{ik} + \vec{m}|^3}\biggl[\vec{\mu}_k - \frac{3[\vec{\mu}_k\cdot(\vec{r}_{ik} + \vec{m})]}{|\vec{r}_{ik} + \vec{m}|^2}(\vec{r}_{ik} + \vec{m})\biggr] \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ik}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ik}+\vec{m}|^2 \bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ik}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ik}+\vec{m}|^2\bigr]\Bigr)\biggr] + \\
  &+ 4\frac{Q_i\vec{\mu}_k\cdot(\vec{r}_{ik} + \vec{m})}{|\vec{r}_{ik}+\vec{m}|^2} \biggl[\eta_i^3\exp\bigl[-\eta_i^2|\vec{r}_{ik} + \vec{m}|^2\bigr] - \alpha^3\exp\bigl[-\alpha^2|\vec{r}_{ik} + \vec{m}|^2\bigr]\biggr](\vec{r}_{ik} + \vec{m}) \Biggr\} + \\
  & + \Biggl\{-\frac{4\pi}{V}\sum_{i=1}^{N_e}\sum_{\vec{k}\neq0} Q_i(\vec{\mu}_k \cdot \vec{h})\vec{h}\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{ik})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2} \Biggr\} + 
   \end{aligned}
   \end{equation} 
   
  \chapter{Electric Fields and Potentials due to Interaction between Electrode Charges and Dipoles}
  The dynamics of the auxiliary variables, i.e. dipole moments $\vec{\mu}$ and (integrated) electrode charges $Q$, is generated enforcing a minimum condition on the energy. In particular we want that the gradient with respect to the auxiliary variables of the energy is equal to 0, which means to find the value of $\vec{\mu}$ and $Q$ such that the energy is at a minimum given the positions of the melt and of the electrodes. In this section we will therefore write the explicit expressions for the quantities $\vec{E}_k = -\vec{\nabla}_{\vec{\mu}_k}U_{c_{\mathrm{g\mu}}}$ with $k = 1,\dots,N_p$ and $V_k = \partial_{Q_k}U_{c_{\mathrm{g\mu}}}$ with $k = 1,\dots,N_e$, which represent the electric field generated by the whole system to the $k$-th atom of the melt and the potential generated by the whole system on the $k$-th electrode atom, respectively. Being the energy quadratic in these variables, the expressions of the derivatives will be much easier to compute compared to the previous ones.
  
  \newpage
  \section{Periodic Boundary Conditions on the $xy$-plane.}
  We now give the expressions for the potential and the electric field generated by a system which is only periodic in the $xy$-plane
  \subsection{Potential}
   \begin{equation}
   \begin{aligned}
  \frac{\partial}{\partial Q_k}U_{c_{\mathrm{g\mu}}} &= \frac{\partial}{\partial Q_k}\Biggl\{\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}} \frac{Q_i\vec{\mu}_j\cdot(\vec{r}_{ij} + \vec{m})}{|\vec{r}_{ij}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ij}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ij}+\vec{m}|^2 \bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ij}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ij}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\}+ \\
  &+ \frac{\partial}{\partial Q_k}\Biggl\{ -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\times \\
  &\times\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{ij} + uz_{ij})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} \Biggr\} + \\
  &+\frac{\partial}{\partial Q_k}\Biggl\{\frac{2\pi}{L_xL_y}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} Q_i\mu^z_j\erf\bigl[\alpha z_{ij}\bigr] \Biggr\} = \\
  &= \Biggl\{\frac{1}{\sqrt{\pi}}\sum_{j=1}^{N_p} \sum_{\vec{n}} \frac{\vec{\mu}_j\cdot(\vec{r}_{kj} + \vec{m})}{|\vec{r}_{kj}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{kj}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{kj}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{kj}+\vec{m}|^2 \bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_k |\vec{r}_{kj}+\vec{m}|\bigr] + 2\eta_k |\vec{r}_{kj}+\vec{m}|\exp\bigl[-\eta^2_k |\vec{r}_{kj}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\}+ \\
  &+ \Biggl\{ -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0}(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\times \\
  &\times\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{kj} + uz_{kj})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} \Biggr\} + \Biggl\{ \frac{2\pi}{L_xL_y}\sum_{j=1}^{N_p} \mu^z_j\erf\bigl[\alpha z_{kj}\bigr] \Biggr\}
  \end{aligned}
   \end{equation}
   
   \subsection{Electric Field}
   \begin{equation}
   \begin{aligned}
  -\vec{\nabla}_{\vec{\mu}_k}U_{c_{\mathrm{g\mu}}} &= -\vec{\nabla}_{\vec{\mu}_k}\Biggl\{\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}} \frac{Q_i\vec{\mu}_j\cdot(\vec{r}_{ij} + \vec{m})}{|\vec{r}_{ij}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ij}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ij}+\vec{m}|^2 \bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ij}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ij}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\}+ \\
  &-\vec{\nabla}_{\vec{\mu}_k}\Biggl\{ -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\times \\
  &\times\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{ij} + uz_{ij})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} \Biggr\} + \\
  &-\vec{\nabla}_{\vec{\mu}_k}\Biggl\{\frac{2\pi}{L_xL_y}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} Q_i\mu^z_j\erf\bigl[\alpha z_{ij}\bigr] \Biggr\} = \\
  & = \Biggl\{-\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e} \sum_{\vec{n}} \frac{Q_i(\vec{r}_{ik} + \vec{m})}{|\vec{r}_{ik}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ik}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ik}+\vec{m}|^2 \bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ik}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ik}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\}+ \\
  &+\Biggl\{ \frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{h} + u\hat{z})\times \\
  &\times\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{ik} + uz_{ik})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} \Biggr\} + \\
  &+\Biggl\{-\frac{2\pi}{L_xL_y}\sum_{i=1}^{N_e}Q_i\erf\bigl[\alpha z_{ik}\bigr]\hat{z} \Biggr\}\\
  \end{aligned}
   \end{equation}
   
   \newpage
  \section{Three-dimensional Periodic Boundary Conditions}
  The same approach can be used when periodic boundary conditions are enforced in the three direction of space.
  \subsection{Potential}
  \begin{equation}
  \begin{aligned}
  \frac{\partial}{\partial Q_k}U_{c_{\mathrm{g\mu}}} &= \frac{\partial}{\partial Q_k}\Biggl\{\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}} \frac{Q_i\vec{\mu}_j\cdot(\vec{r}_{ij} + \vec{m})}{|\vec{r}_{ij}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ij}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ij}+\vec{m}|^2\bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ij}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ij}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\} +\\
  &+ \frac{\partial}{\partial Q_k}\Biggl\{-\mathrm{i}\frac{4\pi}{V}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h}) \exp\bigl[\mathrm{i}\vec{h}\cdot\vec{r}_{ij}\bigr]\frac{\exp\bigl[-\frac{|\vec{h}|^2}{4\alpha^2}\bigr]}{|\vec{h}|^2}\Biggr\} = \\
  &= \Biggl\{\frac{1}{\sqrt{\pi}}\sum_{j=1}^{N_p} \sum_{\vec{n}} \frac{\vec{\mu}_j\cdot(\vec{r}_{kj} + \vec{m})}{|\vec{r}_{kj}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{kj}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{kj}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{kj}+\vec{m}|^2\bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_k |\vec{r}_{kj}+\vec{m}|\bigr] + 2\eta_k |\vec{r}_{kj}+\vec{m}|\exp\bigl[-\eta^2_k |\vec{r}_{kj}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\} +\\
  &+\Biggl\{-\mathrm{i}\frac{4\pi}{V}\sum_{j=1}^{N_p}\sum_{\vec{k}\neq0} (\vec{\mu}_j \cdot \vec{h}) \exp\bigl[\mathrm{i}\vec{h}\cdot\vec{r}_{kj}\bigr]\frac{\exp\bigl[-\frac{|\vec{h}|^2}{4\alpha^2}\bigr]}{|\vec{h}|^2}\Biggr\}
  \end{aligned}
  \end{equation}
  
  \subsection{Electric Field}
  \begin{equation}
  \begin{aligned}
  -\vec{\nabla}_{\vec{\mu}_k}U_{c_{\mathrm{g\mu}}} &= -\vec{\nabla}_{\vec{\mu}_k}\Biggl\{\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}} \frac{Q_i\vec{\mu}_j\cdot(\vec{r}_{ij} + \vec{m})}{|\vec{r}_{ij}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ij}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ij}+\vec{m}|^2\bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ij}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ij}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\} +\\
  &-\vec{\nabla}_{\vec{\mu}_k}\Biggl\{-\mathrm{i}\frac{4\pi}{V}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h}) \exp\bigl[\mathrm{i}\vec{h}\cdot\vec{r}_{ij}\bigr]\frac{\exp\bigl[-\frac{|\vec{h}|^2}{4\alpha^2}\bigr]}{|\vec{h}|^2}\Biggr\} = \\
  &= \Biggl\{-\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e} \sum_{\vec{n}} \frac{Q_i(\vec{r}_{ik} + \vec{m})}{|\vec{r}_{ik}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ik}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ik}+\vec{m}|^2\bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ik}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ik}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ik}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\} +\\
  &+\Biggl\{\mathrm{i}\frac{4\pi}{V}\sum_{i=1}^{N_e}\sum_{\vec{k}\neq0} Q_i\vec{h} \exp\bigl[\mathrm{i}\vec{h}\cdot\vec{r}_{ik}\bigr]\frac{\exp\bigl[-\frac{|\vec{h}|^2}{4\alpha^2}\bigr]}{|\vec{h}|^2}\Biggr\}
  \end{aligned}
  \end{equation}
  
  \chapter{Gradient of the Constraints due to Interaction between Electrode Charges and Dipoles}
  To exploit the massless shake method to compute electrode charges and dipoles, it is necessary to compute the Hessian of the energy. The expression of the tensor due to the interaction between electrode charges and dipoles $\partial_{Q_h}\vec{\nabla}_{\vec{\mu}_k}U_{c_{\mathrm{g\mu}}}$ for $h = 1,\dots,N_e$ and $k = 1,\dots,N_p$ is reported below for the case of periodic boundary conditions on the $xy$ plane and for the three-dimensional case [SCHWARTZ THEOREM].
  \section{Periodic Boundary Conditions on the $xy$-plane.}
  \begin{equation}
  \begin{aligned}
  \frac{\partial}{\partial Q_h}\vec{\nabla}_{\vec{\mu}_k}U_{c_{\mathrm{g\mu}}} &= \frac{\partial}{\partial Q_h}\vec{\nabla}_{\vec{\mu}_k}\Biggl\{\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}} \frac{Q_i\vec{\mu}_j\cdot(\vec{r}_{ij} + \vec{m})}{|\vec{r}_{ij}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ij}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ij}+\vec{m}|^2 \bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ij}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ij}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\}+ \\
  &\frac{\partial}{\partial Q_h}\vec{\nabla}_{\vec{\mu}_k}\Biggl\{- \frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h} + u\mu_j^z)\times \\
  &\times\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{ij} + uz_{ij})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} \Biggr\} + \\
  &\frac{\partial}{\partial Q_h}\vec{\nabla}_{\vec{\mu}_k}\Biggl\{ \frac{2\pi}{L_xL_y}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} Q_i\mu^z_j\erf\bigl[\alpha z_{ij}\bigr] \Biggr\} = \\
  &= \Biggl\{\frac{1}{\sqrt{\pi}}\sum_{\vec{n}} \frac{(\vec{r}_{hk} + \vec{m})}{|\vec{r}_{hk}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{hk}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{hk}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{hk}+\vec{m}|^2 \bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_h |\vec{r}_{hk}+\vec{m}|\bigr] + 2\eta_h |\vec{r}_{hk}+\vec{m}|\exp\bigl[-\eta^2_h |\vec{r}_{hk}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\}+ \\
  &+ \Biggl\{ -\frac{\mathrm{i}}{L_xL_y}\frac{1}{2}\int^{\infty}_{-\infty}du \sum_{\vec{k}\neq0} (\vec{h} + u\hat{z})\times \\
  &\times\exp\bigl[\mathrm{i}(\vec{h}\cdot\vec{r}_{hk} + uz_{hk})\bigr]\frac{\exp\Bigl[-\frac{|\vec{h}|^2 + u^2}{4\alpha^2}\Bigr]}{|\vec{h}|^2 + u^2} \Biggr\} + \Biggl\{ \frac{2\pi}{L_xL_y} \erf\bigl[\alpha z_{hk}\bigr]\hat{z} \Biggr\}
  \end{aligned}
   \end{equation}
%   where
%   \begin{equation}
%   \hat{z}\otimes\hat{z} = 
%   \begin{bmatrix}
%   0 & 0 & 0 \\
%   0 & 0 & 0 \\
%   0 & 0 & 1
%   \end{bmatrix}
%   \end{equation}	

  \section{Three-dimensional Periodic Boundary Conditions}
  \begin{equation}
  \begin{aligned}
  \frac{\partial}{\partial Q_h}\vec{\nabla}_{\vec{\mu}_k}U_{c_{\mathrm{g\mu}}} &= \frac{\partial}{\partial Q_h}\vec{\nabla}_{\vec{\mu}_k} U_{c_{\mathrm{g\mu}}} = \Biggl\{\frac{1}{\sqrt{\pi}}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p} \sum_{\vec{n}} \frac{Q_i\vec{\mu}_j\cdot(\vec{r}_{ij} + \vec{m})}{|\vec{r}_{ij}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{ij}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{ij}+\vec{m}|^2\bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_i |\vec{r}_{ij}+\vec{m}|\bigr] + 2\eta_i |\vec{r}_{ij}+\vec{m}|\exp\bigl[-\eta^2_i |\vec{r}_{ij}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\} +\\
  &+ \frac{\partial}{\partial Q_h}\vec{\nabla}_{\vec{\mu}_k}\Biggl\{-\mathrm{i}\frac{4\pi}{V}\sum_{i=1}^{N_e}\sum_{j=1}^{N_p}\sum_{\vec{k}\neq0} Q_i(\vec{\mu}_j \cdot \vec{h}) \exp\bigl[\mathrm{i}\vec{h}\cdot\vec{r}_{ij}\bigr]\frac{\exp\bigl[-\frac{|\vec{h}|^2}{4\alpha^2}\bigr]}{|\vec{h}|^2}\Biggr\} = \\
  &= \Biggl\{\frac{1}{\sqrt{\pi}} \sum_{\vec{n}} \frac{(\vec{r}_{hk} + \vec{m})}{|\vec{r}_{hk}+\vec{m}|^3} \times \\
  &\times \biggl[\sqrt{\pi}\erfc\bigl[\alpha |\vec{r}_{hk}+\vec{m}|\bigr] + 2\alpha |\vec{r}_{hk}+\vec{m}|\exp\bigl[-\alpha^2 |\vec{r}_{hk}+\vec{m}|^2\bigr] + \\
  &- \Bigl(\sqrt{\pi}\erfc\bigl[\eta_h |\vec{r}_{hk}+\vec{m}|\bigr] + 2\eta_h |\vec{r}_{hk}+\vec{m}|\exp\bigl[-\eta^2_h |\vec{r}_{hk}+\vec{m}|^2\bigr]\Bigr)\biggr] \Biggr\} +\\
  &+ \Biggl\{-\mathrm{i}\frac{4\pi}{V}\sum_{\vec{k}\neq0} \vec{h} \exp\bigl[\mathrm{i}\vec{h}\cdot\vec{r}_{hk}\bigr]\frac{\exp\bigl[-\frac{|\vec{h}|^2}{4\alpha^2}\bigr]}{|\vec{h}|^2}\Biggr\}
  \end{aligned}
  \end{equation}
  
  
\bibliographystyle{unsrt} 
\bibliography{Bibliography/BooksBibliography,Bibliography/PapersBibliography,Bibliography/Bibliography}

\end{document}
