% Intended to be compiled with XeLaTeX

\documentclass[11pt,letterpaper]{article}

\usepackage{graphicx}
\usepackage{natbib}
\usepackage{fullpage}
\usepackage{lineno}
\usepackage{multirow}
%\usepackage{wrapfig}
\usepackage{amsmath}
\usepackage{amssymb}
%\usepackage{sidecap}
\usepackage{hyperref}

\begin{document}

\setlength{\parindent}{0mm}
\setlength{\parskip}{0.4cm}

\bibliographystyle{apalike}

%\modulolinenumbers[5]
%\linenumbers

\title{\textbf{GEMINI}:  \textbf{G}eospace \textbf{E}nvironment \textbf{M}odel of \textbf{I}on-\textbf{N}eutral \textbf{I}nteractions}
\author{Matthew D. Zettergren, PhD\\ Associate Professor of Engineering Physics\\ Center for Space and Atmospheric Physics\\ Physical Sciences Department \\Embry-Riddle Aeronautical University\\mattzett@gmail.com}
\maketitle

\tableofcontents

\pagebreak

%\section{TO DO LIST FOR THIS DOCUMENT:}

%Check section on artificial viscosity

%Check section with advection algorithm

%Check section with geomag. to geographic transformations (they differ from \citet{Huba:2000})

%Add advection boundary conditions section

\section{Preface}

\subsection{Purpose of this document}

The \textbf{G}eospace \textbf{E}nvironment \textbf{M}odel of \textbf{I}on-\textbf{N}eutral \textbf{I}nteractions (GEMINI) is a general-purpose, three-dimensional (3D) terrestrial ionospheric model capable of describing most processes relevant to the ionosphere at medium to small spatial scales (200 m to 10000 km).  In the field of ionospheric physics, there are several well-established global models of the terrestrial ionosphere exist; however, there are comparatively fewer ``standard'' models for small-scale phenomena (as of 2018), which was a motivating factor behind developing GEMINI and making it available to our scientific community.

This document outlines the mathematical formulation of GEMINI and is intended to serve as a bridge between published descriptions of the model \citep{Zettergren:2012,Zettergren:2014,Zettergren:2015,Zettergren:2015b}, which are necessarily fairly terse, and the mathematical and (some) implementation details necessary to understand how the model functions.  A separate README describing how to compile and run the model is included on the GitHub site for the GEMINI project, which currently houses some 8 branches of the code, all adapted to different specific problems of interest but each having the same core numerical components.

\subsection{Error reporting}

Please report any apparent errors to me at:  mattzett@gmail.com

\subsection{Design philosophy}

During design of GEMINI top-level clarity and organization have generally been favored over speed and efficiency.  This decision was largely driven by hope that GEMINI would prove useful to others by being easy to adapt to a wide variety of problems; hence, the model has been designed with source code modification in mind.  Undoubtedly optimizations can and will be made in future branches/forks as needs arise.

GEMINI has also been designed to be easily extensible; e.g. it is feasible to add additional ion species into the model or to use a Cartesian or other type of orthogonal curvilinear grid.  In principle, the GEMINI codebase could potentially be used to develop simulations of ionospheres (more generally, collisional plasmas) for many different types of of planets/environments.

As a final, general, note, GEMINI can run with either a 2D or 3D orthogonal, curvilinear grid.  The 2D functionality was included to facilitate very high-resolution runs in two dimensions, which become overly burdensome in GEMINI's parent MATLAB code.  Special cases in the mathematical formulation and numerical implementation arising when using a 2D grid are discussed in detail in Section \ref{sec:2D} of this document.

\subsection{GEMINI development history}

GEMINI is an evolution of the model first used in \citet{Zettergren:2012} to study terrestrial ionospheric plasma density structures generated by two-dimensional auroral current systems.  It was later extended by \citet{Zettergren:2013} to study low-latitude forcing by infrasonic/acoustic wave and then in \citet{Zettergren:2014} to ingest highly processed data products to specify boundary conditions.  These initial model prototypes were coded in MATLAB; \citet{Zettergren:2015b} translated the model into fortran with MPI parallelization and added in the third dimension in Cartesian coordinates.  The present version extends the 3D fortran model further to include general orthogonal curvilinear coordinates (publication TBD).

\subsection{Executive summary}

The 3D ionospheric ``\underline{G}eospace \underline{E}nvironment \underline{M}odel for \underline{I}on-\underline{N}eutral \underline{I}nteractions'' (GEMINI), is based on the existing 2D model developed by \citet{Zettergren:2012} and later extended by \citet{Zettergren:2013,Zettergren:2014,Zettergren:2015b}.  The present form of GEMINI functions in two or three dimensions, and uses general orthogonal curvilinear coordinates, usually either a tilted dipole \citep{Huba:2000} or Cartesian system. The model comprises a fluid system of equations \citep{Schunk:1977,Blelly:1993}, describing dynamics of the ionospheric plasma, self-consistently coupled to an electrostatic treatment of auroral and neutral dynamo currents. The fluid system is a set of three conservation equations (mass, momentum, and energy) for each ionospheric species $s$ relevant to the E-, F-, and topside regions ($s=\mathrm{O^+,NO^+,N_2^+,O_2^+,N^+,H^+}$).
\begin{linenomath*} \begin{equation}
\frac{\partial \rho_s}{\partial t} + \nabla \cdot \left( \rho_s \mathbf{v}_s \right) = m_s P_s - L_s \rho_s \label{continuity}
\end{equation} \end{linenomath*}
\begin{linenomath*} \begin{equation}
\left[ \frac{\partial }{\partial t} \left( \rho_s \mathbf{v}_s \right) + \nabla \cdot \left( \rho_s \mathbf{v}_s \mathbf{v}_s \right) \right] \cdot \hat{\mathbf{e}}_1 = \left[ -\nabla p_s + \rho_s \mathbf{g} + \frac{\rho_s} {m_s} q_s \mathbf{E} + \sum_t \rho_s \nu_{st} \left(\mathbf{v}_t - \mathbf{v}_s \right) \right] \cdot \hat{\mathbf{e}}_1 \label{momentum}
\end{equation} \end{linenomath*}
\begin{linenomath*} \begin{eqnarray}
\frac{\partial}{\partial t} \left( \rho_s \epsilon_s \right) + \nabla \cdot \left( \rho_s \epsilon_s \mathbf{v}_s \right) &=& - p_s (\nabla \cdot \mathbf{v}_s) - \nabla \cdot \mathbf{h}_s - \\
\nonumber&& \frac{1}{(\gamma_s - 1 )}\sum_t \frac{\rho_s k_B \nu_{st}}{m_s + m_t}\left[ 2 (T_s-T_t) - \frac{2}{3} \frac{m_t}{k_B}(\mathbf{v}_s-\mathbf{v}_t)^2 \right] \label{energy}
\end{eqnarray} \end{linenomath*}
The terms on the right-hand side of Equation \ref{continuity} encapsulate chemical production and impact ionization ($P_s$) and chemical loss ($L_s$). Source terms in the continuity equation for photoionization are calculated according the method presented in \citet{Solomon:2005} using solar fluxes from the EUVAC model \citep{Richards:1994}. Impact ionization is computed using the semi-empirical method of \citet[][and references therein]{Fang:2008}. Chemical reactions for the ionospheric model are taken from \citet[][and references therein]{Diloy:1996,StMaurice:1998}. In Equation \ref{momentum} $q_s$ is the charge of each species, $\mathbf{v}_t$ is the drift velocity of species $t$ (which can be either charged or neutral), and $\nu_{st}$ is the collision frequency of charged species $s$ with species $t$. Note that the momentum equation (\ref{momentum}) is solved in a time-dependent form only for the direction parallel to the geomagnetic field (denoted by the unit vector $\hat{\mathbf{e}}_1$). The partial pressure $p_s$ is related to the specific internal energy and temperature by an equation of state (i.e. the ideal gas law).  Heat fluxes in Equation \ref{energy} ($\mathbf{h}_s$) are specified by a simple model of thermal conduction for the ions:
\begin{linenomath*} \begin{equation}
\mathbf{h}_s = - \lambda_s \nabla_{\parallel} T_s,
\end{equation} \end{linenomath*}
where $\lambda_s$ is the thermal conductivity, taken from \citet{Schunk:1974}. %Physical ion stresses are neglected in our model.

For the dimensions perpendicular to the geomagnetic field line, a steady-state momentum approximation is used:
\begin{linenomath*} \begin{equation}
\mathbf{v}_{s \perp} = \boldsymbol{\mu}_{s \perp} \cdot \left( \mathbf{E}_\perp + \frac{m_s \nu_s}{q_s} \mathbf{v}_{n\perp} \right). \label{drifts}
\end{equation} \end{linenomath*}
In this expression, $\boldsymbol{\mu}_{s\perp}$ is the ion mobility tensor and $\nu_s$ is the total ion-neutral collision frequency \citep{Zettergren:2012}. A static geomagnetic field is used to compute the ion mobilities in equation \ref{drifts}. %Note that this steady-state perpendicular drift assumption is consistent with the use of an electrostatic description for the fields, \citep[c.f.][ for a complete discussion]{Zettergren:2012}.

Mass and momentum density state variables for the electron species are solved by invoking quasi-neutrality and the definition of current density \citep[c.f.][equations 25 and 26]{Zettergren:2012}. A full transport equation is solved for the electron energy.
\begin{linenomath*} \begin{eqnarray}
\frac{\partial}{\partial t} \left( \rho_e \epsilon_e \right) + \nabla \cdot \left( \rho_e \epsilon_e \mathbf{v}_e \right) &=& - p_e (\nabla \cdot \mathbf{v}_e) - \nabla \cdot \mathbf{h}_e - \\
\nonumber&& \frac{1}{(\gamma_e - 1 )}\sum_t \frac{\rho_e k_B \nu_{et}}{m_e + m_t}\left[ 2 (T_e-T_t) - \frac{2}{3} \frac{m_t}{k_B}(\mathbf{v}_e-\mathbf{v}_t)^2 \right] + \frac{Q_e}{(\gamma_e - 1 )}\label{energye}
\end{eqnarray} \end{linenomath*}
For electron heat fluxes, both thermoelectric effects \citep{Schunk:1978} and thermal conduction are considered \citep{Banks:1973,Huba:2000}
\begin{linenomath*} \begin{equation}
\mathbf{h}_e = \left( - \lambda_e \nabla_{\parallel} T_e - \beta_e \mathbf{J}_{\parallel} \right).
\end{equation} \end{linenomath*}
The electron energy equation also differs in form from the ion equation above (equation \ref{energy}) in that it includes inelastic cooling terms and heating by photoelectrons, collectively denoted by $Q_e$. For the present work, cooling due to the excitation of rotational and vibrational modes of O$_2$ and N$_2$ are included, as is the excitation of fine structure of O. The photoelectron heating rate is calculated from the photoionization rate according to the method of \citet{Swartz:1972}.

Perpendicular components of the momentum equations are solved with a steady-state force balance approximation, as described in \citet{Zettergren:2012}.  Electric fields are found by enforcing a divergence free current density, where the current is assumed to consist of both conduction currents (directly dependent on the electric field through the usual ionospheric Ohm's law) and polarization currents (from a spatio-temporally varying electric field) \citep[c.f.][ and references therein]{Mitchell:1985,Gondarenko:1999}.  In essence, this approach assumes that the behavior of the ionospheric response currents is, to leading order, electrostatic.  These currents, however, may be modified by a correction which accounts for polarization currents from slowly varying field structures.  Hence, the ionospheric electric field is approximated by $\mathbf{E}=-\nabla \Phi$.  Further assuming that the geomagnetic field lines are equipotentials and employing a field line integration yields an equation that can be solved for the electric potential, hence field:
\begin{equation}
  \nabla_\perp \cdot \left( \boldsymbol{\Sigma}_\perp \cdot \nabla_\perp \Phi \right) + \nabla_\perp \cdot \left[ C_M \left( \frac{\partial}{\partial t}  + \mathbf{v}_\perp \cdot \nabla_\perp \right) \left( \nabla_\perp \Phi \right) \right]  = \nabla_\perp \cdot \left( \boldsymbol{\Sigma}_\perp \cdot \mathbf{E}_{0\perp} \right) \quad \label{eqn:potential}
\end{equation}
In this equation $\Phi$ is the electric potential generated by the ionospheric response to a magnetospherically imposed electric field $\mathbf{E}_{0\perp}$ and $\mathbf{v}_\perp = \left( \mathbf{E}_{0\perp} -\nabla \Phi \right) \times \mathbf{B}/B^2$.  $\boldsymbol{\Sigma}_\perp$ is the field-line integrated conductance tensor, and $C_M$ is the inertial capacitance defined, e.g., in \citet{Mitchell:1985}.  Note that Equation \ref{eqn:potential} implicitly enforces the condition that all current closes within the ionosphere through either conduction currents or polarization currents (viz. no field-aligned current flow through the top boundary of the model).  Past work has generally found that polarization currents suppress GDI growth through nonlocal closure of currents, similar in effect to shorting out of GDI through a strongly conducting E-region.  However, this term is also responsible for destabilizing the KHI mode \citep{Keskinen:1988}, which alternatively may be stabilized by conduction currents.  The potential defined by Equation \ref{eqn:potential} does not include the ambipolar contribution; instead the ambipolar potential is computed during a separate calculation that resolves the parallel momentum equation (the only equation where the ambipolar term is needed).  This ambipolar field evaluted, during this step, directly from the electron pressure.
\begin{linenomath*} \begin{equation}
\mathbf{E}_{a,\parallel} = \frac{1}{n_e q_e} \nabla_\parallel p_e \label{ambipolar}
\end{equation} \end{linenomath*}
This ambipolar field is added to the ``resistive'' part of the parallel field defined by $\mathbf{E}_{r,\parallel} = - \nabla_\parallel \Phi$ to form the total parallel electric field used in the ion momentum equations (c.f. Equation \ref{momentum}).

The most recent detailed descriptions of GEMINI are in recent publications by \citet[][Appendix A]{Zettergren:2015} and \citet{Zettergren:2015b}, though the model has been around for a while in various forms \citep[e.g.][]{Zettergren:2012}.

GEMINI includes capabilities to couple input from neutral dynamics models by way of files and automatically does interpolation, and rotation of neutral wind, density, and temperature perturbations (deviations from MSIS00) onto its curvilinear grid.  These perturbations are then factored into all ionospheric calculations that use neutral parameters (dynamo, collisions, ionization, chemistry, etc.).  Note that the coupling here is one-way (viz. neutrals affecting ions).  Examples of using neutral dynamics information to drive GEMINI have been published by \citet{Zettergren:2013,Zettergren:2015,Zettergren:2017}.

Boundary conditions for electric potential, background electric fields, and field-aligned currents and electron precipitation can be included in GEMINI through input files.  These inputs can be specified independent on a grid different from that used in GEMINI and are automatically interpolated to the simulation grid positions.

A branch of GEMINI exists that uses the suprathermal electron transport code GLOW to specify ionization and heating reates instead of \citet{Fang:2008,Swartz:1972}.  This version has the ability to separate between thermal and suprathermal field-aligned currents and is able to more accurately specify both thermal electron heating by suprathermal electrons and ionization rates of major species for an arbitration input electron precipitation pattern and distribution.

\section{Mathematical formulation}

GEMINI comprises a fluid system of equations \citep{Schunk:1977,Blelly:1993,Huba:2000}, describing dynamics of the ionospheric plasma, self-consistently coupled to an electrostatic treatment of auroral and neutral dynamo currents that is based on a steady-state current continuity equation.  These systems of equations are discussed in detail in this section.

\subsection{Fluid system of equations}

The fluid system is a set of three conservation equations (mass, momentum, and energy) for each ionospheric species $s$ relevant to the E-, F-, and topside regions ($s=\mathrm{O^+,NO^+,N_2^+,O_2^+,N^+,H^+}$):
\begin{linenomath*} \begin{equation}
\frac{\partial \rho_s}{\partial t} + \nabla \cdot \left( \rho_s \mathbf{v}_s \right) = m_s P_s - L_s \rho_s \label{continuity}
\end{equation} \end{linenomath*}
\begin{linenomath*} \begin{equation}
%  \left[ \frac{\partial }{\partial t} \left( \rho_s \mathbf{v}_s \right) + \nabla \cdot \left( \rho_s \mathbf{v}_s \mathbf{v}_s \right) \right] \cdot \hat{\mathbf{e}}_1 = -\nabla p_s + \rho_s \mathbf{g} + \frac{\rho_s} {m_s} q_s \left( \mathbf{E} + \mathbf{v}_s \times \mathbf{B} \right) + \sum_t \rho_s \nu_{st} \left(\mathbf{v}_t - \mathbf{v}_s \right)\label{momentum}
\left[ \frac{\partial }{\partial t} \left( \rho_s \mathbf{v}_s \right) + \nabla \cdot \left( \rho_s \mathbf{v}_s \mathbf{v}_s \right) \right] \cdot \hat{\mathbf{e}}_1 = \left[ -\nabla p_s + \rho_s \mathbf{g} + \frac{\rho_s} {m_s} q_s \mathbf{E} + \sum_t \rho_s \nu_{st} \left(\mathbf{v}_t - \mathbf{v}_s \right) \right] \cdot \hat{\mathbf{e}}_1 \label{momentum}
\end{equation} \end{linenomath*}
\begin{linenomath*} \begin{eqnarray}
\frac{\partial}{\partial t} \left( \rho_s \epsilon_s \right) + \nabla \cdot \left( \rho_s \epsilon_s \mathbf{v}_s \right) &=& - p_s (\nabla \cdot \mathbf{v}_s) - \nabla \cdot \mathbf{h}_s - \\
\nonumber&& \frac{1}{(\gamma_s - 1 )}\sum_t \frac{\rho_s k_B \nu_{st}}{m_s + m_t}\left[ 2 (T_s-T_t) - \frac{2}{3} \frac{m_t}{k_B}(\mathbf{v}_s-\mathbf{v}_t)^2 \right] \label{energy}
\end{eqnarray} \end{linenomath*}
The terms on the right-hand side of Equation \ref{continuity} encapsulate chemical production and impact ionization ($P_s$) and chemical loss ($L_s$). Source terms in the continuity equation for photoionization are calculated according the method presented in \citet{Solomon:2005} using solar fluxes from the EUVAC model \citep{Richards:1994}. Impact ionization is computed by the semi-empircal method described in \citet{Fang:2008}. Chemical reactions for the ionospheric model are taken from \citet[][and references therein]{Diloy:1996,Stmaurice:1998}. In Equation \ref{momentum} $q_s$ is the charge of each species, $\mathbf{v}_t$ is the drift velocity of species $t$ (which can be either charged or neutral), and $\nu_{st}$ is the collision frequency of charged species $s$ with species $t$. Note that the momentum equation (\ref{momentum}) is solved in a time-dependent form only for the direction parallel to the geomagnetic field (denoted by the unit vector $\hat{\mathbf{e}}_1$). The partial pressure $p_s$ is related to the specific internal energy by an equation of state (for each species):
\begin{equation}
\rho_s \epsilon_s = \frac{p_s}{\gamma_s - 1} = \frac{n_s k_B T_s}{\gamma_s - 1}.
\end{equation}
$\gamma_s$ is the adiabatic index for species $s$ and $k_B$ is the Boltzmann constant.  Heat fluxes in Equation \ref{energy} ($\mathbf{h}_s$) are specified by a simple model of thermal conduction for the ions:
\begin{linenomath*} \begin{equation}
\mathbf{h}_s = - \lambda_s \nabla_{\parallel} T_s,
\end{equation} \end{linenomath*}
where $\lambda_s$ is the thermal conducivity, taken from \citet{Schunk:1975}. Note that, as implied in the fluid equations above, physical ion stresses are neglected in our model.  A steady-state momentum balance is used for the perpendicular ion drifts, as described in Section \ref{EM}.

%Ion momentum in the perpendicular direction uses a steady state approximation:
%\begin{linenomath*} \begin{equation}
%\mathbf{v}_{s \perp} = \boldsymbol{\mu}_{s \perp} \cdot \left( \mathbf{E}_\perp + \frac{m_s \nu_s}{q_s} \mathbf{v}_{n\perp} \right). \label{drifts}
%\end{equation} \end{linenomath*}
%In this expression, $\boldsymbol{\mu}_{s\perp}$ is the ion mobility tensor and $\nu_s$ is the total ion-neutral collision frequency \citep{Zettergren:2012}. A static geomagnetic field is used to compute the ion mobilities in equation \ref{drifts}. Note that this steady-state perpendicular drift assumption is consistent with the use of an electrostatic description for the fields.  Note that time-dependence and ion inertia are ignored in Equation \ref{drifts}, although an exception to this is sometimes made in the electrodynamics solutions to accommodate small-scales and fast-varying systems- cf. Section \ref{sec:electrodynamics})

Mass and momentum density state variables for the electron species are solved by invoking quasi-neutrality and the definition of current density \citep[cf. also][equations 25 and 26]{Zettergren:2012}:
\begin{eqnarray}
n_e &=& \sum_{s \ne e} n_s \label{eqn:quasineu} \\
\mathbf{v}_e &=& \mathbf{J} - \frac{1}{n_e q_e} \sum_{s \ne e} n_s \mathbf{v}_s
\end{eqnarray}
A full transport equation is must be solved for the electron energy to include the effects of photoionization heating and heating by suprathermal electron precipitation.
\begin{linenomath*} \begin{eqnarray}
\frac{\partial}{\partial t} \left( \rho_e \epsilon_e \right) + \nabla \cdot \left( \rho_e \epsilon_e \mathbf{v}_e \right) &=& - p_e (\nabla \cdot \mathbf{v}_e) - \nabla \cdot \mathbf{h}_e - \\
\nonumber&& \frac{1}{(\gamma_e - 1 )}\sum_t \frac{\rho_e k_B \nu_{et}}{m_e + m_t}\left[ 2 (T_e-T_t) - \frac{2}{3} \frac{m_t}{k_B}(\mathbf{v}_e-\mathbf{v}_t)^2 \right] + \frac{Q_e}{(\gamma_e - 1 )}\label{energye}
\end{eqnarray} \end{linenomath*}
For electron heat fluxes, both thermoelectric effects \citep{Schunk:1978} and thermal conduction are considered \citep{Banks:1973,Huba:2000}
\begin{linenomath*} \begin{equation}
\mathbf{h}_e = \left( - \lambda_e \nabla_{\parallel} T_e - \beta_e \mathbf{J}_{\parallel} \right).
\end{equation} \end{linenomath*}
The electron energy equation also differs in form from the ion equation above (equation \ref{energy}) in the inclusion of inelastic cooling terms and heating by photoelectrons, collectively denoted by $Q_e$. For the present work, cooling due to the excitation of rotational and vibrational modes of O$_2$ and N$_2$ are included, as is the excitation of fine structure of O. The photoelectron heating rate is calculated according to the method presented in \citet{Swartz:1972} using photoionization rates computed in the model.

%Strictly speaking, these equations must be supplemented by Maxwell's equations for the electromagnetic fields.  However, an electostatic assumption (or quasi-electrostatic assumptions) holds to a reasonable degree of accuracy, so a simpler formulation described in Section \ref{EM}, below, can be used in GEMINI.

\subsection{Electrostatic and quasi-electrostatic approximations} \label{EM}

For a coupled description of transport and electrodynamics in auroras, the fluid equations presented above must be supplemented by Maxwell's equations.  However, the issue of finding the ionospheric electric field that results from a given magnetospheric boundary condition may be treated, to some degree of accuracy, as an electrostatic problem.  The details of this approximation have been covered by several authors \citep[e.g.][and references therein]{Stmaurice:1996}, and the net effect is that the displacement current and electromagnetic induction may be neglected in situations where wave dynamics are not important or have negligible effects.  This approximation precludes modeling of Alfv\'en waves and related inductive M-I coupling effects as discussed, for example in \citet{Lotko:2004}.  Our focus in the following sections on slowly varying current systems supports this use of the electrostatic approximation.  %These constraints reduce the Maxwell equations to the relations:
%\begin{linenomath*} \begin{eqnarray}
%\nabla^2 \Phi = - \frac{\rho_c}{\epsilon_0} &=& - \frac{\sum_s n_s q_s}{\epsilon_0} \\
%\nabla \cdot \mathbf{J} = \nabla \cdot \left( \sum_s n_s q_s \mathbf{v}_s \right)  &=& 0 \label{divJSS}
%\end{eqnarray} \end{linenomath*}

Qausineutrality holds very strongly in the plasma (Equation \ref{eqn:quasineu}), and a steady-state momentum balance holds under certain conditions, which allows the velocities to be expressed in a simple way as a function of the electric field (e.g. as noted above in Equation \ref{drifts}).  These facts facilitate the use of a steady state current continuity equation,
\begin{linenomath*} \begin{equation}
\nabla \cdot \mathbf{J} = \nabla \cdot \left( \sum_s n_s q_s \mathbf{v}_s \right)  = 0, \label{divJSS}
\end{equation} \end{linenomath*}
to directly solve for electric potential as discussed in detail below.

%Several comments are in order regarding the connection of these equations to the fluid formulation of Equations \ref{continuity}, \ref{momentum}, and \ref{energy}.  First, the current continuity equation is linearly dependent on the mass continuity equations (including all ions and electron).  Because of this either all of the mass continuity and momentum equations must be solved, or all but one can be solved provided that the current continuity equation is added in to close the system.  The former choice necessitates the use of Poisson's equation for specifying electric potential, hence electric field.  The latter choice, however, affords a useful shortcut since the plasma is quasi-neutral ($n_e \approx \sum_{s \ne e} n_s$) and the drift velocities can be expressed simply as a function of electric field.  In this case Equation \ref{divJSS} can directly be used to solve for electric potential, as discussed below.

\subsection{Momentum balance approximation and Ohm's law}

Ordinarily one would need to solve a full time dependent equation for ion momenrum perpendicular to the geomagnetic field, similar to Equation \ref{momentum}.  However, in GEMINI, a steady state momentum balance assumption is used and admits a much simpler formulation of ionospheric electric fields.  If the time-derivative and inertial terms of the perpendicular momentum equation are neglected, along with gravitational forces, then the drifts can be solved for explicitly in terms of the force per unit charge:
\begin{linenomath*} \begin{eqnarray}
\mathbf{v}_{s\perp} &=& \boldsymbol{\mu}_{s\perp} \cdot \left( \mathbf{E}_\perp - \frac{1}{n_s q_s} \nabla_\perp p_s + \frac{m_s \nu_s}{q_s} \mathbf{v}_{n\perp} \right) \label{mombalance} \\
\mathbf{v}_{e\parallel} &=& \mu_{e0} \left( \mathbf{E}_\parallel - \frac{1}{n_e q_e} \nabla_\parallel p_e \right) \label{mombalancepar}
\end{eqnarray} \end{linenomath*}
In this expression, $\boldsymbol{\mu}_{s\perp}$ is the ion mobility tensor and $\nu_s$ is the total ion-neutral collision frequency \citep{Zettergren:2012}, and $\mathbf{v}_n$ is the neutral wind velocity. A static geomagnetic field is used to compute the ion mobilities in Equation \ref{mombalance}.  It is again noted that time-dependence and ion inertia are ignored in Equation \ref{mombalance}, although an exception to this is sometimes made in the electrodynamics solutions to accommodate small-scales and fast-varying systems- cf. Section \ref{sec:electrodynamics}).  The ion and electron mobilities are defined using orthogonal coordinates with $\mathbf{B}$ in the $x_1-$direction by:
\begin{linenomath*} \begin{eqnarray}
\boldsymbol{\mu}_{s\perp}
\equiv
\frac{q_s}{m_s \nu_s}  \left[ \begin{array}{cc}
  \frac{\nu_{s}^2}{\nu_{s}^2 + \Omega_s^2} & \frac{\nu_{s} \Omega_s}{\nu_{s}^2+\Omega_s^2} \\
  - \frac{\nu_{s} \Omega_s}{\nu_{s}^2+\Omega_s^2} &  \frac{\nu_{s}^2}{\nu_{s}^2 + \Omega_s^2}
\end{array} \right]
&=&
\left[ \begin{array}{cc}
  \mu_{sP} & -\mu_{sH} \\
  \mu_{sH} &  \mu_{sP}
\end{array} \right]
\label{mus} \\
\mu_{e0} & \equiv & \frac{q_e}{m_e \nu_e'}
\end{eqnarray} \end{linenomath*}
The cyclotron frequency is $\Omega_s \equiv q_s B_z/m_s$, and $\nu_s$ is the \emph{total} collision frequency.  For the mobilities $\mu_{sP}$ and $\mu_{sH}$, the total collision frequency is calculated as a sum over ion-neutral collisions, which are dominant in regions where perpendicular transport is important.
\begin{linenomath*} \begin{equation}
\nu_s = \sum_n \nu_{sn}
\end{equation} \end{linenomath*}
Both electron-neutral and electron-ion collisions are used in computing the parallel mobility of the electrons, which dominates the parallel conductivity.  Coulomb collisions are necessary since the field-aligned currents carried by electrons flow at high altitudes.
\begin{linenomath*} \begin{equation}
\nu_e' = \sum_n \nu_{en} + \sum_j \nu_{ej}
\end{equation} \end{linenomath*}
In this equation the quantity $\nu_{ej}$ refers to collisions of electrons with charged species $j$.  As a final note, neutral and ion drifts are neglected in the computation of the parallel electron drift, which is assumed to be much larger than any other flows on account of the small electron mass (and corresponding very large parallel electron mobility).

The steady-state momentum balance (Equations \ref{mombalance} and \ref{mombalancepar}) is justified in the sense that the principle charge carriers, electrons in the field-aligned direction, ions and electrons perpendicular to $\mathbf{B}$ attain a steady-state quickly.  The ions drift perpendicular to the field in a steady state for time scales $\tau \gg 2 \pi \Omega_s^{-1}$.  Electrons are in a steady-state in field-parallel direction on time-scales much longer than the electron collision time (probably $10^{-2}$ s or less) \citep[e.g.][]{Stmaurice:1996}.

To obtain an Ohm's law expression for use with equation \ref{divJSS} the momentum balance is multiplied by $n_s q_s$ and summed over species:
\begin{linenomath*} \begin{eqnarray}
\mathbf{J}_\perp &=& \boldsymbol{\sigma}_\perp \cdot \mathbf{E}_\perp - \sum_s \boldsymbol{\mu}_{s\perp} \cdot \nabla_\perp p_s + \sum_s n_s m_s \nu_s \boldsymbol{\mu}_{s\perp} \cdot \mathbf{v}_{n\perp} \label{ohm} \\
\mathbf{J}_\parallel &=& \sigma_0 \mathbf{E}_\parallel - \mu_{e0} \nabla_\parallel p_e \label{ohmpar}
\end{eqnarray} \end{linenomath*}
The conductivities are defined by:
\begin{linenomath*} \begin{eqnarray}
\boldsymbol{\sigma}_\perp
\equiv
\sum_s n_s q_s \boldsymbol{\mu}_{s\perp}
&=&
\left[ \begin{array}{ccc}
  \sigma_P & -\sigma_H \\
  \sigma_H &  \sigma_P
\end{array} \right] \\
\sigma_0 &\approx& n_e q_e \mu_{e0}
%=
%\left[ \begin{array}{ccc}
%  \sum_s \frac{q_s^2 n_s}{m_s \nu_{s}} \frac{\nu_{s}^2}{\nu_{s}^2 + \Omega_s^2} & \sum_s \frac{q_s^2 n_s}{m_s \nu_{s}} \frac{\nu_{s} \Omega_s}{\nu_{s}^2+\Omega_s^2} & 0 \\
%  - \sum_s \frac{q_s^2 n_s}{m_s \nu_{s}} \frac{\nu_{s} \Omega_s}{\nu_{s}^2+\Omega_s^2} &  \sum_s \frac{q_s^2 n_s}{m_s \nu_{s}} \frac{\nu_{s}^2}{\nu_{s}^2 + \Omega_s^2} & 0 \\
%0 & 0 & \sum_s \frac{n_s q_s^2}{m_s \nu_s}
%\end{array} \right]
\end{eqnarray} \end{linenomath*}

It is worth emphasizing that GEMINI does not treat the parallel ion drift with a steady state formulation (cf. Equation \ref{momentum}), while Equation \ref{ohmpar} clearly enforces some sort of parallel momentum steady state.  This is justified by the fact that typical time scales for electron momentum variations are much shorter than the time scale for ion momentum variations.  The former is roughly given by the inverse electron-ion collision frequency, while the latter is given by the inverse ion-electron collision frequency. Because of conservation of momentum in collisions \citep[c.f.][]{Schunk:2009} the electron-ion collision frequency is larger than the ion-electron frequency by a factor of roughly the ion to electron mass ratio, and leads to a much smaller momentum relaxation time for electrons as compared to ions.  Hence, one may treat the electrons as in a steady state, while solving a time-dependent equation for ions and not render our current continuity inconsistent, provided that the ions do not contribute much to the parallel current (which, based on simple mobility arguments outlined here, they should not).

Therefore one may use the steady state (for electrons) implied by Equation \ref{ohmpar}, while still treating ion parallel momentum in a time-dependent and nonlinear manner.

\subsection{Handling of ambipolar and large-scale, ``background'' electric fields}

In GEMINI the ambipolar and background parts of the electric field is removed prior to solving for potential, and then added back into the momentum equation when the forces computed.  This is done as a matter of convenience to avoid extra source terms in the numerical solution of our potential equation (described in later sections of this document).  Thus, the electric field is divided into two parts, the ambipolar field $\mathbf{E}_a$ which is present even in the absence of magnetospheric forcing and neutral winds, and the response field $\mathbf{E}_r$ which results from magnetospherically imposed currents, potential patterns, or neutral winds.  The parallel ambipolar field can be found directly by zeroing out $\mathbf{J}$ and $\mathbf{v}_n$ in Equation \ref{ohmpar}.
\begin{linenomath*} \begin{equation}
\mathbf{E}_{a\parallel} \approx \sigma_0^{-1} \mu_{e0} \nabla_\parallel p_e = \frac{1}{n_e q_e} \nabla_\parallel p_e \label{Eambpar}
\end{equation} \end{linenomath*}
GEMINI presently neglects contributions of perpendicular pressure gradients to current, but for simulations with very high resolution these may need to be added.  Ohm's law with $\mathbf{E}=\mathbf{E}_r+\mathbf{E}_a$ becomes an equation in the disturbance quantities only.
\begin{linenomath*} \begin{eqnarray}
\mathbf{J}_\perp &=& \boldsymbol{\sigma}_\perp \cdot \mathbf{E}_{r\perp} + \left( \sum_s n_s m_s \nu_s \boldsymbol{\mu}_{s\perp} \cdot \mathbf{v}_{n\perp} \right) + \boldsymbol{\sigma}_\perp \cdot \mathbf{E}_{0\perp} \label{ohm2} \\
\mathbf{J}_\parallel &=& \sigma_0 \mathbf{E}_{r\parallel}
\end{eqnarray} \end{linenomath*}
The third term on the right-hand side of Equation \ref{ohm2} represents currents generated by large-scale ``background'' electric fields and convection, which is treated as a separate term in GEMINI.  Invoking the electrostatic assumption $\mathbf{E}_r = -\nabla \Phi$ gives an equation that can be solved directly for electric potential.
\begin{linenomath*} \begin{equation}
\nabla_\perp \cdot \left( \boldsymbol{\sigma}_\perp \cdot \nabla_\perp \Phi \right) + \nabla_\parallel \cdot \left( \sigma_0 \nabla_\parallel \Phi \right)  = \nabla_\perp \cdot \left( \sum_s n_s m_s \nu_s \boldsymbol{\mu}_{s\perp} \cdot \mathbf{v}_{n\perp} \right) + \nabla_\perp \cdot \left( \boldsymbol{\sigma}_\perp \cdot \mathbf{E}_{0\perp} \right) \label{divJSS2}
\end{equation} \end{linenomath*}
Note that this problem becomes homogeneous if neutral winds and background electric fields are neglected, while inclusion of the inhomogeneous terms allows for modeling of neutral drag effects on current generation and inclusion of large-scale background ionospheric electric fields and drifts.

%\subsection{Background vs. response electric fields}

%Often we wish to specify a uniform background field, which drives an ionospheric response due to e.g. nonuniform conductivies or other processes.  This can be accommodated easily in the above formulation and slightly altered potential equations to solve:
%\begin{linenomath*} \begin{equation}
%\nabla_\perp \cdot \left( \boldsymbol{\sigma}_\perp \cdot \nabla_\perp \Phi \right) + \nabla_\parallel \cdot \left( \sigma_0 \nabla_\parallel \Phi \right)  = \nabla_\perp \cdot \left( \sum_s n_s m_s \nu_s \boldsymbol{\mu}_{s\perp} \cdot \mathbf{v}_{n\perp} \right)
%\end{equation} \end{linenomath*}

%\subsection{On the mixing of time-dependent and steady state formulations}

%The apparent inconsistency that the reviewer notes can be resolved by carefully considering the assumptions underlying Equations 2, 7, and 10. These were not adequately discussed in the original manuscript so additional text has been added to the revision to correct this oversight.

%The electric field is separated, using superposition, into two parts in the model: (1) the ambipolar electric field Ea and (2) the disturbance response field Er. When solving for the electric potential in equation 7 the ambipolar electric field is not included, so the potential represents the part of the electric field that is due to the ionospheric resistivity (viz. collisions). When solving momentum equations, the ambipolar electric field (computed from electron pressure) is added to the electric field which is a result of driving a field-aligned current through the resistive ionosphere. Hence, both the electron pressure and resistive parts of the electric field are resolved in our model. The separation of different contributions to the electric field (ambipolar vs. resistive) is done merely as a matter of convenience (i.e. the pressure term could be left in the current continuity equation), and tests indicate that this approach does not impact solutions for electric field.

%As indicated above, the conductivities in equation 7 (now equation 8 - listed below for reference) are calculated using all species for the perpendicular conductivities (Pedersen and Hall) and only the electrons for the parallel conductivity.

%Note that this equation encodes assumptions of a perpendicular momentum steady state and a parallel electron momentum steady-state (further-discussed below). The disturbance response portion of the electric field can be calculated from the potential and then added to the ambipolar field to yield an expression for total, parallel electric field. Once the parallel field is calculated it can be used in the parallel momentum equation (which does, as the reviewer notes, include the important inertial term):

%The electron parallel drift is obtained by computing the current density from Ohm?s law and then applying the ?definition? of current density as indicated by equation 10 (now equation 12).

%As indicated above, our current continuity equation assumes steady state ion momentum perpendicular to the geomagnetic field and assumes that electrons carry the parallel current. Under a steady state momentum assumption ion and electron drifts are determined by the mobility and forces on each population. Since the electron mobility is much higher than the ion mobility in the parallel direction (see Figure 1?, not to be confused with the manuscript Figure 1, included below), the parallel ion drift contributions to current are neglected and the electrons are assumed to carry all of the parallel current. Even though the ions are not in a steady state in our model (e.g. equation 2) the assumption that they contribute negligibly to the parallel current is still likely to apply. Hence, we are able to treat the parallel ion momentum in a time-dependent manner (which is necessary for any sensible description of time-dependent ion upflow) while still using a current continuity equation that encodes a steady-state momentum balance.

\subsection{Leading-order electrodynamics} \label{sec:electrodynamics}

Electrodyamic effects on ionospheric electric fields can be encapsulated, to leading order, by include the polarization drifts and current in the current continuity equation, but still retaining the electrostatic assumption, $\nabla \times \mathbf{E}=0$.  The polarization current, due to slow variations in the electric field with time, is assumed to be only in the perpendicular direction and is given by:
\begin{equation}
\mathbf{J}_p = \frac{\sum_s \rho_s}{B^2} \left( \frac{\partial}{\partial t}  + \mathbf{v}_\perp \cdot \nabla_\perp \right) \mathbf{E}_r
\end{equation}
When included in the steady-state current continuity equation, along with electrostatic assumptions we get a time-dependent potential equation:
\begin{eqnarray}
  \nabla_\perp \cdot \left( \boldsymbol{\sigma}_\perp \cdot \nabla_\perp \Phi \right) + \nabla_\parallel \cdot \left( \sigma_0 \nabla_\parallel \Phi \right) + \nabla_\perp \cdot \left[ c_M \left( \frac{\partial}{\partial t}  + \mathbf{v}_\perp \cdot \nabla_\perp \right) \left( \nabla_\perp \Phi \right) \right]  &=& \nonumber \\ \nabla_\perp \cdot \left( \sum_s n_s m_s \nu_s \boldsymbol{\mu}_{s\perp} \cdot \mathbf{v}_{n\perp} \right) + \nabla_\perp \cdot \left( \boldsymbol{\sigma}_\perp \cdot \mathbf{E}_{0\perp} \right) \label{eqn:electrodynamic}
\end{eqnarray}
The parallel component of the polarization current can be neglected for our purposes due to the very small parallel electric field and its extremely slow variations in time.  The inertial capacitance $c_M$ is defined by \citep{Mitchell:1985}:
\begin{equation}
c_M \equiv \frac{\sum_s \rho_s}{B^2}
\end{equation}

The polarization effects can be derived by considering the ion momentum equation, include both inertia and time dependent terms neglected in prior static analysis, which can be written in the form:
\begin{equation}
m_s \left( \frac{\partial }{\partial t}  + \mathbf{v}_s \cdot \nabla \right) \mathbf{v_s} = - \frac{1}{n_s} \nabla p_s + m_s \mathbf{g} +  q_s \mathbf{E} + q_s \mathbf{v}_s \times \mathbf{B} + \sum_t m_s \nu_{st} \left(\mathbf{v}_t - \mathbf{v}_s \right)
\end{equation}
Taking the cross product of both sides of this equation with the magnetic field gives (note that a constant magnetic field is also assumed in the development below):
\begin{equation}
\mathbf{B} \times \left( \mathbf{v}_s \times \mathbf{B} \right) = \left\{ - m_s \left( \frac{\partial }{\partial t}  + \mathbf{v}_s \cdot \nabla \right) \mathbf{v}_s - \frac{1}{n_s} \nabla p_s + m_s \mathbf{g} +  q_s \mathbf{E} + \sum_t m_s \nu_{st} \left(\mathbf{v}_t - \mathbf{v}_s \right) \right \} \times \frac{\mathbf{B}}{q_s}
\end{equation}
\begin{equation}
\mathbf{v}_s (\mathbf{B} \cdot \mathbf{B}) - \mathbf{B}( \mathbf{v}_s \cdot \mathbf{B} ) = \left\{ - m_s \left( \frac{\partial }{\partial t}  + \mathbf{v}_s \cdot \nabla \right) \mathbf{v}_s - \frac{1}{n_s} \nabla p_s + m_s \mathbf{g} +  q_s \mathbf{E} + \sum_t m_s \nu_{st} \left(\mathbf{v}_t - \mathbf{v}_s \right) \right \} \times \frac{\mathbf{B}}{q_s}
\end{equation}
Assuming that the primary drift is perpendicular to the geomagnetic field:
\begin{equation}
\mathbf{v}_s = \left\{ - m_s \left( \frac{\partial }{\partial t}  + \mathbf{v}_s \cdot \nabla \right) \mathbf{v_s} - \frac{1}{n_s} \nabla p_s + m_s \mathbf{g} +  q_s \mathbf{E} + \sum_t m_s \nu_{st} \left(\mathbf{v}_t - \mathbf{v}_s \right) \right \} \times \frac{\mathbf{B}}{B^2 q_s}
\end{equation}
Which can then be rearranged to emphasize the terms of interest:
\begin{equation}
\mathbf{v}_{s\perp} = \frac{\mathbf{E} \times \mathbf{B}}{B^2} - \left\{ m_s \left( \frac{\partial }{\partial t}  + \mathbf{v}_{s\perp} \cdot \nabla \right) \mathbf{v}_{s\perp} \right\} \times \frac{\mathbf{B}}{B^2 q_s} + \left\{ - \frac{1}{n_s m_s} \nabla p_s + \mathbf{g} + \sum_t \nu_{st} \left(\mathbf{v}_t - \mathbf{v}_{s\perp} \right) \right \} \times \frac{\mathbf{B}}{B^2 q_s}
\end{equation}
The first term on the right hand side of this equation is by far the dominant term:
\begin{equation}
\mathbf{v}_{s\perp} \approx \mathbf{v}_{E} = \frac{\mathbf{E} \times \mathbf{B}}{B^2}
\end{equation}
A higher-order correction to the ion drift can then be derived by replacing all instances of $\mathbf{v}_s$ with $\mathbf{v}_E$ on right hand side of the full drift equation:
\begin{equation}
\mathbf{v}_{s\perp} = \frac{\mathbf{E} \times \mathbf{B}}{B^2} - \left\{ m_s \left( \frac{\partial }{\partial t}  + \mathbf{v}_{E} \cdot \nabla \right) \mathbf{v}_{E} \right\} \times \frac{\mathbf{B}}{B^2 q_s} + \left\{ - \frac{1}{n_s m_s} \nabla p_s + \mathbf{g} + \sum_t \nu_{st} \left(\mathbf{v}_t - \mathbf{v}_{E} \right) \right \} \times \frac{\mathbf{B}}{B^2 q_s}
\end{equation}
The time dependent term can be expanded out:
\begin{eqnarray}
- m_s \left\{ \left( \frac{\partial }{\partial t}  + \mathbf{v}_{E} \cdot \nabla \right) \mathbf{v}_{E} \right\} \times \frac{\mathbf{B}}{B^2 q_s} &=& \frac{m_s}{B^2 q_s} \left( \frac{\partial }{\partial t}  + \mathbf{v}_{E} \cdot \nabla \right)  \left( \frac{\mathbf{E} \times \mathbf{B}}{B^2} \right) \times \mathbf{B} \\ &=& -\frac{m_s}{B^2 q_s} \left( \frac{\partial }{\partial t}  + \mathbf{v}_{E} \cdot \nabla \right)  \left( \frac{-1}{B^2} ( \mathbf{E} (B^2) - \mathbf{B} (\mathbf{E} \cdot \mathbf{B}) ) \right) \\ &=& \frac{m_s}{B^2 q_s} \left( \frac{\partial }{\partial t}  + \mathbf{v}_{E} \cdot \nabla \right) \mathbf{E}
\end{eqnarray}
Thus we have, ignoring other terms:
\begin{equation}
\mathbf{v}_{s\perp} \approx \frac{\mathbf{E} \times \mathbf{B}}{B^2} + \frac{m_s}{B^2 q_s} \left( \frac{\partial }{\partial t}  + \mathbf{v}_{E} \cdot \nabla \right) \mathbf{E}
\end{equation}
At lower altitude, obviously collisional terms also matter, as well.  The current density resulting from these drifts is the polarization current density used in the GEMINI electrodynamic equation:
\begin{equation}
\mathbf{J}_p = \sum_s n_s q_s \mathbf{v}_{s\perp} = \frac{\sum n_s m_s }{B^2} \left( \frac{\partial }{\partial t}  + \mathbf{v}_{E} \cdot \nabla \right) \mathbf{E}
\end{equation}

\subsection{Response magnetic fields}

Magnetic fields generated by ionospheric currents from dynamo sources (described above) are calculated from the Biot-Savart Law:
\begin{equation}
  \mathbf{B}(\mathbf{x}) = \frac{\mu_0}{4 \pi} \int \frac{\mathbf{J}(\mathbf{x}') \times (\mathbf{x} - \mathbf{x}')}{|\mathbf{x} - \mathbf{x}'|^3} d^3 x'
\end{equation}
where $\mathbf{x}$ and $\mathbf{x'}$ represent field and source coordinates, respectively.

\subsection{Organization of model}

The GEMINI model consists of a simultaneous solution to Equations \ref{divJSS2}, \ref{continuity}, \ref{momentum}, and \ref{energy} for the unknowns $\Phi$, $n_s$, $\mathbf{v}_s$, and $T_s$.  As outlined above, momentum balance is assumed in the computation of $\Phi$ at any particular instant.  The evolution of potential on longer times scales is captured by evaluating the steady state equation Equation \ref{divJSS2} each time step as the fluid moment variables (densities, etc. hence conductivities) are updated.  The result is a mathematical formulation capable of describing current closure, electric fields, heating, chemistry, plasma structuring, and upwelling processes in the auroral ionosphere.

\section{Use of generalized curvilinear coordinates in GEMINI}

GEMINI makes use of generalized curvilinear coordinates, denoted $x_1,x_2,x_3$, having, respectively, associated differential lengths, $h_1 dx_1,h_2 dx_2,h_3 dx_3$.  Specific forms of the derivative operations needed in the model are documented in detail in this section.

%The master version of the code, while it uses these naming conventions, only functions for Cartesian coordinate (i.e. it assumes all metric factor are equal to one).  The curvilinear branch includes all of the differential geometry described in this document.

\subsection{Gradient and divergence}

The gradient and divergence operations needed for both the fluid and electrostatic equations are given, in curvilinear form, by:
\begin{equation}
\nabla \cdot \mathbf{A} = \frac{1}{h_1 h_2 h_3} \left[ \frac{\partial}{\partial x_1} \left( h_2 h_3 A_1 \right) + \frac{\partial}{\partial x_2} \left( h_1 h_3 A_2 \right) + \frac{\partial}{\partial x_3} \left( h_1 h_2 A_3 \right) \right]
\end{equation}
for a vector field $\mathbf{A}$, and:
\begin{equation}
\nabla \Phi = \frac{1}{h_1} \frac{\partial \Phi}{\partial x_1} \hat{\mathbf{e}}_1 + \frac{1}{h_2} \frac{\partial \Phi}{\partial x_2} \hat{\mathbf{e}}_2 + \frac{1}{h_3} \frac{\partial \Phi}{\partial x_3} \hat{\mathbf{e}}_3
\end{equation}
for a scalar field $\Phi$

\subsection{Tensor derivatives in curvilinear form}

The bulk momentum fluxes occuring in the the parallel ion momentum equations (Equation \ref{momentum}) require careful treatment as they include various geometric terms which encapsulate, e.g. centrifugal forces.  To proceed we specify the 1-coordinate ($x_1$) to be along the geomagnetic field line.  The following identity regarding divergence of a symmetric rank-2 tensor ($\mathbf{T}$) in orthogonal coordinates is useful for paring down the bulk momentum flux term:
\begin{equation}
\left[ \nabla \cdot \mathbf{T} \right]_1 = \frac{1}{h_1 h_2 h_3} \left[ \frac{\partial}{\partial x_1}(h_2 h_3 T_{11}) + \frac{1}{h_1} \frac{\partial}{\partial x_2}(h_1^2 h_3 T_{12}) + \frac{1}{h_1} \frac{\partial}{\partial x_3}(h_1^2 h_2 T_{13}) \right] - \frac{T_{22}}{h_1 h_2} \frac{\partial h_2}{\partial x_1} - \frac{T_{33}}{h_1 h_3} \frac{\partial h_3}{\partial x_1}.  \label{divtens}
\end{equation}
For our specific case this gives:
\begin{equation}
[\nabla \cdot (\rho \mathbf{v} \mathbf{v})]_1 = \frac{1}{h_1 h_2 h_3} \left[ \frac{\partial}{\partial x_1}(h_2 h_3 \rho v_1^2) + \frac{1}{h_1} \frac{\partial}{\partial x_2}(h_1^2 h_3 \rho v_1 v_2) + \frac{1}{h_1} \frac{\partial}{\partial x_3}(h_1^2 h_2 \rho v_1 v_3) \right] - \frac{\rho v_2^2}{h_1 h_2} \frac{\partial h_2}{\partial x_1} - \frac{\rho v_3^2}{h_1 h_3} \frac{\partial h_3}{\partial x_1}.
\end{equation}

In GEMINI an artificial viscosity (yielding an artificial stress) is included in the momentum and energy equations to deal with situations where steep solutions may form.  The stress divergence parallel component (see Section \ref{sec:artvisc} and Equations \ref{eqn:stressmom} and \ref{eqn:stressen}) may also be needed for the momentum equation.  As outlined above, we retain only the $Q_{11}$ element of the stress tensor, termed the parallel stress.  Accordingly we may again invoke the identity of equation \ref{divtens}.
\begin{equation}
(\nabla \cdot \mathbf{Q})_1 = \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1}(h_2 h_3 Q_{11})
\end{equation}
If the parallel stress is given by a Navier-Stokes formula then we have:
\begin{equation}
Q_{11} = \left[-\mu_s \left( \nabla \boldsymbol{\mathrm{v}} + (\nabla \boldsymbol{\mathrm{v}})^T - \frac{2}{3} (\nabla \cdot \boldsymbol{\mathrm{v}}) \mathbf{I} \right) \right]_{11} = -\mu_s \left( 2 [\nabla \boldsymbol{\mathrm{v}}]_{11} - \frac{2}{3} (\nabla \cdot \boldsymbol{\mathrm{v}}) \right)
\end{equation}
In order to pursue this further we need the $\hat{\mathbf{e}}_1 \hat{\mathbf{e}}_1$ component of the gradient of the velocity in generalized orthogonal coordinates.  The required identity is:
\begin{equation}
[\nabla \boldsymbol{\mathrm{v}}]_{11} = \frac{h_1 \frac{\partial v_1}{\partial x_1} - v_1 \frac{\partial h_1}{\partial x_1}}{h_1^2} + \frac{1}{h_1} \left( \frac{v_1}{h_1} \frac{\partial h_1}{\partial x_1} + \frac{v_2}{h_2} \frac{\partial h_1}{\partial x_2} + \frac{v_3}{h_3} \frac{\partial h_1}{\partial x_3} \right)
\end{equation}
In principle the above equations allow us to calculate the effects of stress, though these are presently neglected in the model since past studies have shown that NS stress approximations are invalid at altitudes where stress is significant \citep{Schunk:1975}.

\subsection{Curvilinear form of fluid equations}

The conservation of mass, momentum, and energy equations for the ions can be represented in general curvilinear coordinates as with the magnetic field being aligned with the $x_1-$direction.  The continuity equation reads:
\begin{equation}
\frac{\partial \rho_s}{\partial t} + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( h_2 h_3 \rho_s v_{s1} \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_2} \left( h_1 h_3 \rho_s v_{s2} \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_3} \left( h_1 h_2 \rho_s v_{s3} \right) =  m_s P_s - L_s \rho_s \label{eqn:continuitycoord}
\end{equation}
This equation has the form of an advection equation with local sources and sinks corresponding to chemical and photo-production terms.

The velocity has 3 components, hence, 3 equations.  However, the vertical and horizontal parts are treated differently, as discussed above, with the result that only the time-dependence parallel momentum equation needs to be solved.  %The horizontal drifts are controlled by short time-scale electrodynamic processes and are well approximated by:
%\begin{equation}
%v_{s2} \hat{\mathbf{e}}_2 + v_{s3} \hat{\mathbf{e}}_3 = \boldsymbol{\mu}_{s\perp} \cdot \mathbf{E}_\perp
%\end{equation}
%The field-aligned component of velocity is found by solving the full parallel momentum equation.
\begin{eqnarray}
\frac{\partial}{\partial t} \left( \rho_s v_{s1} \right) &+& \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( h_2 h_3 \rho_s v_{s1}^2 \right) + \frac{1}{h_1^2 h_2 h_3} \frac{\partial}{\partial x_2} \left( h_1^2 h_3 \rho_s v_{s1} v_{s2} \right) + \frac{1}{h_1^2 h_2 h_3} \frac{\partial}{\partial x_3} \left( h_1^2 h_2 \rho_s v_{s1} v_{s3} \right) \nonumber \\
~ &~& - \frac{\rho_s v_{s2}^2}{h_1 h_2} \frac{\partial h_2}{\partial x_1} - \frac{\rho_s v_{s3}^2}{h_1 h_3} \frac{\partial h_3}{\partial x_1}  = \rho_s g_1 - \frac{1}{h_1} \frac{\partial p_s}{\partial x_1} + \frac{\rho_s q_s}{m_s} E_1 + \sum_t \rho_s \nu_{st} \left( v_{t1} - v_{s1} \right) \label{eqn:momentumcoord}
\end{eqnarray}
This equation has the form of an advection equation with local sources and sinks.  The negative, geometric terms on the left-hand side can are treated as source terms for purposes of numerical solution.

The ion energy equation in curvilinear forms is:
\begin{eqnarray}
\frac{\partial}{\partial t} \left( \rho_s \epsilon_s \right) &+& \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( h_2 h_3 \rho_s \epsilon_s v_{s1} \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_2} \left( h_1 h_3 \rho_s \epsilon_s v_{s2} \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_3} \left( h_1 h_2 \rho_s \epsilon_s v_{s3} \right) \nonumber \\
&=& -p_s \frac{1}{h_1 h_2 h_3} \left( \frac{\partial}{\partial x_1} \left( h_2 h_3 v_{s1} \right) + \frac{\partial}{\partial x_2} \left( h_1 h_3 v_{s2} \right) + \frac{\partial}{\partial x_3} \left( h_1 h_2 v_{s3} \right) \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( \frac{h_2 h_3}{h_1} \lambda_s \frac{\partial T_s}{\partial x_1} \right) \nonumber \\
&~& - \frac{1}{\gamma_s - 1} \sum_t \frac{\rho_s k_B \nu_{st}}{m_s + m_t} \left[ 2 \left( T_s - T_t \right) - \frac{2 m_t}{3 k_B} \left( \left( v_{s1} - v_{t1} \right)^2 + \left( v_{s2} - v_{t2} \right)^2 + \left( v_{s3} - v_{t3} \right)^2 \right) \right] \label{eqn:ionenergycoord}
\end{eqnarray}

The electron energy has an extra term corresponding to the divergence of the thermoelectric heat flux.
\begin{equation}
- \nabla \cdot \left( - \beta_e \mathbf{J} \right)
\end{equation}
This term may be expanded using the product rules and simplified by enforcing a divergence free current:
\begin{equation}
- \nabla \cdot \left( - \beta_e \mathbf{J} \right) = \nabla \beta_e \cdot \mathbf{J} + \beta_e \nabla \cdot \mathbf{J} = \nabla \beta_e \cdot \mathbf{J}
\end{equation}
Finally if we consider only the parallel components of the heat flux and use the definition $\beta_e = \frac{5}{2} \frac{k_B T_e}{|q_e|}$, this becomes:
\begin{equation}
\nabla \beta_e \cdot \mathbf{J} = \frac{5}{2} \frac{k_B J_1}{|q_e|} \frac{1}{h_1} \frac{\partial T_e}{\partial x_1}
\end{equation}
The electron energy equation can then be written, in curvilinear coordinates as:
\begin{eqnarray}
\frac{\partial}{\partial t} \left( \rho_e \epsilon_e \right) &+& \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( h_2 h_3 \rho_e \epsilon_e v_{e1} \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_2} \left( h_1 h_3 \rho_e \epsilon_e v_{e2} \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_3} \left( h_1 h_2 \rho_e \epsilon_e v_{e3} \right) \nonumber \\
&=& -p_e \frac{1}{h_1 h_2 h_3} \left( \frac{\partial}{\partial x_1} \left( h_2 h_3 v_{e1} \right) + \frac{\partial}{\partial x_2} \left( h_1 h_3 v_{e2} \right) + \frac{\partial}{\partial x_3} \left( h_1 h_2 v_{e3} \right) \right) \nonumber \\
&~& + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( \frac{h_2 h_3}{h_1} \lambda_e \frac{\partial T_e}{\partial x_1} \right) + \frac{5}{2} \frac{k_B J_1}{|q_e|} \frac{1}{h_1} \frac{\partial T_e}{\partial x_1} \nonumber \\
&~& - \frac{1}{\gamma_e - 1} \sum_t \frac{\rho_e k_B \nu_{et}}{m_e + m_t} \left[ 2 \left( T_e - T_t \right) - \frac{2 m_t}{3 k_B} \left( \left( v_{e1} - v_{t1} \right)^2 + \left( v_{e2} - v_{t2} \right)^2 + \left( v_{e3} - v_{t3} \right)^2 \right) \right] \nonumber \\
&~& + \frac{Q_e}{\gamma_e - 1} \label{eqn:electronenergycoord}
\end{eqnarray}

\subsection{Curvilinear form of electrostatic equation}

\subsubsection{Electrostatic equation}

In GEMINI, the $x_1$-direction is taken to correspond to the field-aligned dimensions of the model grid.  This dimension ordering makes parallelization easier, since the grid is most easily split amongst worker processes along the 3rd dimension or the arrays (this way subdomain portions of full-grid arrays are contiguous in memory). The curvilinear form of Equation \ref{divJSS2} may be written as:
\begin{eqnarray}
\frac{1}{h_1 h_2 h_3} \left[ \frac{\partial}{\partial x_1} \left( \frac{h_2 h_3}{h_1} \sigma_0 \frac{\partial \Phi}{\partial x_1} \right) + \frac{\partial}{\partial x_2} \left( \frac{h_1 h_3}{h_2} \sigma_P \frac{\partial \Phi}{\partial x_2} \right) - \frac{\partial}{\partial x_2} \left( h_1 \sigma_H \frac{\partial \Phi}{\partial x_3} \right) \right. &+& \nonumber \\ \left. \frac{\partial}{\partial x_3} \left( h_1 \sigma_H \frac{\partial \Phi}{\partial x_2} \right) + \frac{\partial}{\partial x_3} \left( \frac{h_1 h_2}{h_3} \sigma_P \frac{\partial \Phi}{\partial x_3} \right)
\right]  = \nabla_\perp \cdot \left( \sum_s n_s m_s \nu_s \boldsymbol{\mu}_{s\perp} \cdot \mathbf{v}_{n\perp} \right) + \nabla_\perp \cdot \left( \boldsymbol{\sigma}_\perp \cdot \mathbf{E}_{0\perp} \right)
\end{eqnarray}
The right-hand side (RHS) of this equation is treated as a source term that is computed with a direct numerical derivative.  This equation can be simplified by noting the equivalence of cross partial derivatives.
\begin{eqnarray}
-\frac{\partial}{\partial x_2} \left( h_1 \sigma_H \frac{\partial \Phi}{\partial x_3} \right) + \frac{\partial}{\partial x_3} \left( h_1 \sigma_H \frac{\partial \Phi}{\partial x_2} \right) &=& -\frac{\partial}{\partial x_2} \left( h_1 \sigma_H \right) \frac{\partial \Phi}{\partial x_3} - h_1 \sigma_H \frac{\partial^2 \Phi}{\partial x_2 \partial x_3} \\ &+& \frac{\partial}{\partial x_3} \left( h_1 \sigma_H \right) \frac{\partial \Phi}{\partial x_2} + h_1 \sigma_H \frac{\partial^2 \Phi}{\partial x_3 \partial x_2} \nonumber \\ &=& -\frac{\partial}{\partial x_2} \left( h_1 \sigma_H \right) \frac{\partial \Phi}{\partial x_3} + \frac{\partial}{\partial x_3} \left( h_1 \sigma_H \right) \frac{\partial \Phi}{\partial x_2},
\end{eqnarray}
which yields:
\begin{eqnarray}
\frac{1}{h_1 h_2 h_3} \left[ \frac{\partial}{\partial x_1} \left( \frac{h_2 h_3}{h_1} \sigma_0 \frac{\partial \Phi}{\partial x_1} \right) + \frac{\partial}{\partial x_2} \left( \frac{h_1 h_3}{h_2} \sigma_P \frac{\partial \Phi}{\partial x_2} \right) -  \right. \frac{\partial}{\partial x_2} \left( h_1 \sigma_H \right) \frac{\partial \Phi}{\partial x_3} &+& \nonumber \\ \left. \frac{\partial}{\partial x_3} \left( h_1 \sigma_H \right) \frac{\partial \Phi}{\partial x_2} + \frac{\partial}{\partial x_3} \left( \frac{h_1 h_2}{h_3} \sigma_P \frac{\partial \Phi}{\partial x_3} \right)
\right]  = \nabla_\perp \cdot \left( \sum_s n_s m_s \nu_s \boldsymbol{\mu}_{s\perp} \cdot \mathbf{v}_{n\perp} \right) + \nabla_\perp \cdot \left( \boldsymbol{\sigma}_\perp \cdot \mathbf{E}_{0\perp} \right) \label{eqn:curvpot}
\end{eqnarray}
This particular form of the potential equation is useful, numerically, since it does not contain any cross-partial derivatives.  It is possible to rewrite the right-hand side of this equation, which is numerically useful as it reduces the message-passing burden in the model by eliminating the need to exchange individual ion mobilities between worker processes.
\begin{equation}
\nabla_\perp \cdot \left( \sum_s n_s m_s \nu_s \boldsymbol{\mu}_{s\perp} \cdot \mathbf{v}_{n\perp} \right) = \nabla_\perp \cdot \left[ \boldsymbol{\sigma}_\perp \cdot \left( \mathbf{v}_{n\perp} \times \mathbf{B} \right) \right]
\end{equation}

\subsubsection{Equipotential field line (EFL) potential formulation}

In many cases it is possible (and useful, from the standpoint of computational efficiency) to solve a field-integrated form of the potential equation, corresponding to an assumption of equipotential field lines (the EFL approximation).  In situations where the scale sizes perpendicular to the geomagnetic field are not too small, the field lines can effectively be treated as equipotentials \citep{Farley:1959,Huba:1988}.  Rerranging and integrating Equation \ref{eqn:curvpot} along the x$_1$-direction) yields:
\begin{eqnarray}
&~& \frac{\partial}{\partial x_2} \left( \Sigma_P^{(2)} \frac{\partial \Phi}{\partial x_2} \right) + \frac{\partial}{\partial x_3} \left( \Sigma_P^{(3)} \frac{\partial \Phi}{\partial x_3} \right) -  \frac{\partial \Sigma_H'}{\partial x_2} \frac{\partial \Phi}{\partial x_3} + \frac{\partial \Sigma_H'}{\partial x_3} \frac{\partial \Phi}{\partial x_2} \nonumber \\ &=& \int h_1 h_2 h_3 \left\{ \nabla_\perp \cdot \left[ \boldsymbol{\sigma}_\perp \cdot \left( \mathbf{v}_{n\perp} \times \mathbf{B} \right) \right] + + \nabla_\perp \cdot \left( \boldsymbol{\sigma}_\perp \cdot \mathbf{E}_{0\perp} \right) \right\} d x_1 + \left[ h_2 h_3 J_1 \right] \left|^{x_{1,max}}_{x_{1,min}} \right. \label{eqn:curvpotEFL}
\end{eqnarray}
where the following definitions have been used:
\begin{eqnarray}
\Sigma_P^{(2)} &\equiv& \int \frac{h_1 h_3}{h_2} \sigma_P dx_1 \\
\Sigma_P^{(3)} &\equiv& \int \frac{h_1 h_2}{h_3} \sigma_P dx_1 \\
\Sigma_H' &\equiv& \int h_1 \sigma_H dx_1
\end{eqnarray}
and the following simplifications were made:
\begin{equation}
\int \frac{\partial}{\partial x_1} \left( \frac{h_2 h_3}{h_1} \sigma_0 \frac{\partial \Phi}{\partial x_1} \right) d x_1= \left[ h_2 h_3 J_1 \right] \left|^{x_{1,max}}_{x_{1,min}} \right.
\end{equation}

Lateral boundaries (corresponding to the two field-perpendicular directions) are always assumed to be at a fixed potential, hence, Equation \ref{eqn:curvpotEFL} is always solved with Dirichlet conditions.  Note also that if the user specifies a topside potential pattern that it is not necessary to solve Equation \ref{eqn:curvpotEFL} since the potential is already known and the fields and current densities may be computed directly.  In contrast, with a specified field aligned current at the highest altitudes of the model, the potential is not known \emph{a priori} and Equation \ref{eqn:curvpotEFL} must to be solved.  This the the mode of operation most typically used in GEMINI, particularly for 3D simulations where a field-resolved potential solution is not practical, especially in high-resolution simulations.  %maybe move to numerical section?

\subsubsection{Electrodynamic equation in EFL form}

Electrodynamic contributions to electric fields and currents are approximated using the polarization current as outlined in Section \ref{sec:electrodynamics}.  Including this current in the field-integrated form of the ionospheric potential equation reads:
\begin{equation}
  \nabla_\perp \cdot \left( \boldsymbol{\Sigma}_\perp \cdot \nabla_\perp \Phi \right) + \nabla_\perp \cdot \left[ C_M \left( \frac{\partial}{\partial t}  + \mathbf{v}_\perp \cdot \nabla_\perp \right) \left( \nabla_\perp \Phi \right) \right]  = \nonumber \\ \nabla_\perp \cdot \left[ \boldsymbol{\sigma}_\perp \cdot \left( \mathbf{v}_{n\perp} \times \mathbf{B} \right) \right] + \nabla_\perp \cdot \left( \boldsymbol{\Sigma}_\perp \cdot \mathbf{E}_{0\perp} \right) \label{eqn:electro}
\end{equation}
Where $C_M$ is the field-line integrated inertial capacitance.  In the current version of GEMINI, solution of this equation is only supported in Cartesian coordinates - attempts to use electrodynamics with other coordinate system will cause GEMINI to terminate the simulation without attempting a solution (an error will also be printed to the screen).

In Cartesian coordinates (the only system supported for electrodynamic simulations) the electrodynamic, EFL form of the polarization current is:
\begin{eqnarray}
- \nabla_\perp \cdot \left[ C_M \frac{\partial}{\partial t} \left( \nabla_\perp \Phi \right) \right]  - \nabla_\perp \cdot \left[ C_M \left( \mathbf{v}_\perp \cdot \nabla_\perp  \right) \left( \nabla_\perp \Phi \right) \right] &=& \nonumber \\
- \left\{ \frac{\partial}{\partial x_2} \left[ C_M \frac{\partial}{\partial t} \left( \frac{\partial \Phi}{\partial x_2} \right) \right] + \frac{\partial}{\partial x_3} \left[ C_M \frac{\partial}{\partial t} \left( \frac{\partial \Phi}{\partial x_3} \right) \right] \right\} &-& \nonumber \\
\frac{\partial}{\partial x_2} \left( C_M v_2 \frac{\partial^2 \Phi}{\partial x_2^2} + C_M v_3 \frac{\partial^2 \Phi}{\partial x_3 \partial x_2} \right) &-& \nonumber \\
\frac{\partial}{\partial x_3} \left( C_M v_2 \frac{\partial^2 \Phi}{\partial x_2 \partial x_3} + C_M v_3 \frac{\partial^2 \Phi}{\partial x_3^2} \right)
\end{eqnarray}
Inserting this into the full EFL potential PDE results in a rather more complex potential equation to be solved as compared to the purely electrostatic form:
\begin{eqnarray}
\frac{\partial}{\partial x_2} \left( \Sigma_P \frac{\partial \Phi}{\partial x_2} \right) + \frac{\partial}{\partial x_3} \left( \Sigma_P \frac{\partial \Phi}{\partial x_3} \right) -  \frac{\partial \Sigma_H}{\partial x_2} \frac{\partial \Phi}{\partial x_3} + \frac{\partial \Sigma_H}{\partial x_3} \frac{\partial \Phi}{\partial x_2} &+& \nonumber \\
\left\{ \frac{\partial}{\partial x_2} \left[ C_M \frac{\partial}{\partial t} \left( \frac{\partial \Phi}{\partial x_2} \right) \right] + \frac{\partial}{\partial x_3} \left[ C_M \frac{\partial}{\partial t} \left( \frac{\partial \Phi}{\partial x_3} \right) \right] \right\} &+& \nonumber \\
\frac{\partial}{\partial x_2} \left( C_M v_2 \frac{\partial^2 \Phi}{\partial x_2^2} + C_M v_3 \frac{\partial^2 \Phi}{\partial x_3 \partial x_2} \right) &+& \nonumber \\
\frac{\partial}{\partial x_3} \left( C_M v_2 \frac{\partial^2 \Phi}{\partial x_2 \partial x_3} + C_M v_3 \frac{\partial^2 \Phi}{\partial x_3^2} \right) &=& \nonumber \\
= \int \left\{ \nabla_\perp \cdot \left[ \boldsymbol{\sigma}_\perp \cdot \left( \mathbf{v}_{n\perp} \times \mathbf{B} \right) \right] + + \nabla_\perp \cdot \left( \boldsymbol{\Sigma}_\perp \cdot \mathbf{E}_{0\perp} \right) \right\} d x_1 + \left[J_1 \right] \left|^{x_{1,max}}_{x_{1,min}} \right. \label{eqn:electrodynamic}
\end{eqnarray}

\subsubsection{Field-aligned current calculations}

When an EFL approximation is used, the field aligned current calculation differs from what it would normally be in the field-resolved formulation, i.e. $\mathbf{J}_\parallel=-\sigma_0 \nabla_\parallel \Phi$, since we explicity assume $\nabla_\parallel \Phi = 0$ in formulating the EFL equations.  The field-aligned currents are instead computed from the divergence of the perpendicular current.  Specifically the parallel current is computed to be whatever current is necessary to enforce the divergence free conditions given the perpendicular currents generated by the solution of Equation \ref{eqn:curvpotEFL}.  Enforcing current continuity with a known perpendicular current density yields:
\begin{equation}
\left. \left[ h_2 h_3 J_1 \right] \right|_{x_1'}^{x_1''} = - \int_{x_1'}^{x_{1}''} h_1 h_2 h_3 \left( \nabla_\perp \cdot \mathbf{J}_\perp \right) d x_1 \label{eqn:FACcalc}
\end{equation}
where $x_1'$ and $x_1''$ represent two different locations on the computational grid.

It is \emph{always} necessary to know the current density at one of the model $x_1$ locations (normally this location would be at a boundary) to produce a solution for parallel current over the entire grid via Equation \ref{eqn:FACcalc}.  Two different types of boundary conditions (i.e. those in the $x_1$-direction) can be used by GEMINI:  (1) a user-specified a potential pattern (``Dirichlet'' conditions) and (2) a user-specified field-aligned current pattern (``Neumann'' conditions).  A field aligned current can be related to Neumann conditions on potential via the equation:
\begin{equation}
  J_1 = - \frac{\sigma_0}{h_1} \frac{\partial \Phi}{\partial x_1}; \qquad \frac{\partial \Phi}{\partial x_1} = -\frac{h_1 J_1}{\sigma_0}
\end{equation}

If Dirichlet boundary conditions are used, the perpendicular current may be directly calculated and used with the assumption that the field-aligned current at the lowest altitude of the model is zero to produce an expression for the parallel current throughout the domain.

For Neumann conditions, if the given field-aligned current boundary condition corresponds to the maximum $x_1$ value we may calculate $J_1$ everywhere on the grid by:
\begin{equation}
\left. \left[ h_2 h_3 J_1 \right] \right|_{x_1} = \left. \left[ h_2 h_3 J_1 \right] \right|_{x_{1,max}} + \int_{x_1}^{x_{1,max}} h_1 h_2 h_3 \left( \nabla_\perp \cdot \mathbf{J}_\perp \right) d x_1
\end{equation}

If the given Neumann boundary condition corresponds to the minimum $x_1$ value, the parallel current over the rest of the grid can be calculated from:
\begin{equation}
\left. \left[ h_2 h_3 J_1 \right] \right|_{x_1} = \left. \left[ h_2 h_3 J_1 \right] \right|_{x_{1,min}} - \int_{x_{1,min}}^{x_1} h_1 h_2 h_3 \left( \nabla_\perp \cdot \mathbf{J}_\perp \right) d x_1
\end{equation}
Note that in either case, we are explicitly using knowledge of the parallel current at one of the boundaries.  On a closed dipole grid, either equation immediately above may be used since the current density is known to go to zero at both ends.  Note that the above formulas may also be used when the user specifies a top boundary current (``Neumann'' conditions) as may be the case when an open grid (i.e. not spanning both hemispheres) is used.

\subsubsection{Magnetic fields}

The particular integral for the magnetic fields is always evaluated in a Cartesian form, irrespective of the coordinate system used in the simulations.  To facilitate this, simulation results are interpolated and rotated onto first into a spherical ECEF coordinate system, then a local ($x_{1,2,3}$ = up, south, east) coordinate system.  The component form of the magnetic field for a 3D simulation reads:
\begin{equation}
\mathbf{B}(\mathbf{x}) = \frac{\mu_0}{4 \pi} \left\{  \hat{\mathbf{e}}_1 \int \frac{J_2 R_3 - J_3 R_2}{R^3} d^3 x' - \hat{\mathbf{e}}_2 \int \frac{J_1 R_3 - J_3 R_1}{R^3} d^3 x' + \hat{\mathbf{e}}_3 \int \frac{J_1 R_2 - J_2 R_1}{R^3} d^3 x' \right\} \label{eqn:magint3D}
\end{equation}
where $\mathbf{R} \equiv \mathbf{x}-\mathbf{x'} = \sum_i R_i \hat{\mathbf{e}}_i$ is the displacement vector between source and field grid points.

\section{Dipole coordinates}

The most commonly used \emph{curvilinear} coordinate system for GEMINI is a dipole coordinate system, defined in \citep{Huba:2000}.  Here we outline metric factors and unit vectors used by GEMINI to describe this coordinate system.  Throughout this section we use $x^i$ to refer to a general curvilinear coordinate (contravariant), and subscripts are used to denote covariant quantities.  The purpose of laying out the transformation process in such detail is to provide a succinct description of how these may be derived for ``new'' coordinate systems.

\subsection{Derivation of dipole metric}

Curvilinear dipole coordinates are typically described using quantities $q,p,\phi$, related to our general curvilinear coordinates by:
\begin{equation}
x^1 = q; \quad x^2 = p; \quad x^3 = \phi
\end{equation}
The $q,p,\phi$ coordinates defined as in \citet{Huba:2000}; $q$ is a coordinate along the geomagnetic field, $p$ is a coordinate perpendicular to the field in the magnetic meridional plane, and $\phi$ is same coordinate as used in a spherical system (positive toward magnetic east).  In terms of spherical coordinates $r,\theta,\phi$ (measured from the center of Earth's tilted dipole, which axes aligned according to Earth magnetic dipole moment) and Cartesian coordinates (similarly defined) the dipole coordinates are:
\begin{eqnarray}
q &=& \frac{R_e^2}{r^2} \cos \theta = \frac{R_e^2 z}{\left( x^2+y^2+z^2\right)^{3/2}}\\
p &=& \frac{r}{R_e} \frac{1}{\sin^2 \theta} = \frac{\left( x^2+y^2+z^2\right)^{3/2}}{R_e \left( x^2 + y^2 \right) } \\
\phi &=& \tan^{-1} \left( \frac{y}{x} \right)
\end{eqnarray}
Inverting $q,p$ coordinates to obtain $r,\theta$ is accomplished via the relations:
\begin{eqnarray}
q^2 \left( \frac{r}{R_e}\right)^4 + \frac{1}{p} \left( \frac{r}{R_e}\right)  &=& 1 \\
\cos \theta &=& q \frac{r^2}{R_e^2}
\end{eqnarray}
Typically the first equation in this set is solved for $r$ given $q,p$ (a numerical root-finding problem), and then the $r$- and $q$ coordinates are using to evaluate the latter equation for $\theta$.

Transformations defined above allow for the basis vectors and metric for dipole coordinates to be derived.  Contravariant basis vectors are defined in terms of coordinates as \citep[][ Chapter 4]{Arfken7th}:
\begin{equation}
\boldsymbol{\varepsilon}^i = \frac{\partial x^i}{\partial x} \hat{\mathbf{e}}_x + \frac{\partial x^i}{\partial y} \hat{\mathbf{e}}_y + \frac{\partial x^i}{\partial z} \hat{\mathbf{e}}_z \label{eqn:conbasis}
\end{equation}
The covariant basis vectors (not used immediately) are defined by:
\begin{equation}
\boldsymbol{\varepsilon}_i = \frac{\partial x}{\partial x^i} \hat{\mathbf{e}}_x + \frac{\partial y}{\partial x^i} \hat{\mathbf{e}}_y + \frac{\partial z}{\partial x^i} \hat{\mathbf{e}}_z \label{eqn:conbasis}
\end{equation}
The necessary derivative for computing the contravariant basis vectors can be computed from the dipole coordinates as functions of the cartesian coordinates $x,y,z$ (listed above) and giving:
\begin{eqnarray}
\frac{\partial x^1}{\partial x} &=& \frac{\partial q}{\partial x} = \frac{-3 R_e \sin \theta \cos \theta \cos \phi}{r^3} \\
\frac{\partial x^1}{\partial y} &=& \frac{\partial q}{\partial y} = \frac{-3 R_e \sin \theta \cos \theta \sin \phi}{r^3} \\
\frac{\partial x^1}{\partial z} &=& \frac{\partial q}{\partial z} = \frac{R_e^2 \left( 1 - 3 \cos^2 \theta \right)}{r^3} \\
\frac{\partial x^2}{\partial x} &=& \frac{\partial p}{\partial x} = \frac{\cos \phi}{ R_e \sin^3 \theta} \left( 1 - 3 \cos^2 \theta \right) \\
\frac{\partial x^2}{\partial y} &=& \frac{\partial p}{\partial y} = \frac{\sin \phi}{ R_e \sin^3 \theta} \left( 1 - 3 \cos^2 \theta \right) \\
\frac{\partial x^2}{\partial z} &=& \frac{\partial p}{\partial z} = \frac{3 \cos \theta}{R_e \sin^2 \theta}\\
\frac{\partial x^3}{\partial x} &=& \frac{\partial \phi}{\partial x} = \frac{- \sin \phi}{r \sin \theta} \\
\frac{\partial x^3}{\partial y} &=& \frac{\partial \phi}{\partial y} = \frac{\cos \phi}{r \sin \theta} \\
\frac{\partial x^3}{\partial z} &=& \frac{\partial \phi}{\partial z} = 0
\end{eqnarray}
The contravariant basis vectors are (from Equation \ref{eqn:conbasis}):
\begin{eqnarray}
\boldsymbol{\varepsilon}^1 &=& \frac{-3 R_e \sin \theta \cos \theta \cos \phi}{r^3} \hat{\mathbf{e}}_x + \frac{-3 R_e \sin \theta \cos \theta \sin \phi}{r^3} \hat{\mathbf{e}}_y + \frac{R_e^2 \left( 1 - 3 \cos^2 \theta \right)}{r^3} \hat{\mathbf{e}}_z \\
\boldsymbol{\varepsilon}^2 &=& \frac{\cos \phi}{ R_e \sin^3 \theta} \left( 1 - 3 \cos^2 \theta \right) \hat{\mathbf{e}}_x +  \frac{\sin \phi}{ R_e \sin^3 \theta} \left( 1 - 3 \cos^2 \theta \right) \hat{\mathbf{e}}_y + \frac{3 \cos \theta}{R_e \sin^2 \theta} \hat{\mathbf{e}}_z \\
\boldsymbol{\varepsilon}^3 &=& \frac{- \sin \phi}{r \sin \theta} \hat{\mathbf{e}}_x + \frac{\cos \phi}{r \sin \theta} \hat{\mathbf{e}}_y
\end{eqnarray}

The metric tensors corresponding to our dipole curvilinear space are related to differential displacements in the new coordinate system.  Based on the definition of the covariant unit vectors a differential displacement in a curvilinear system is (note use of Einstein's summation convention):
\begin{equation}
d \mathbf{r} = d x^i \boldsymbol{\varepsilon}_i \label{eqn:dr}
\end{equation}
Differential distance squared may then be written:
\begin{equation}
dr^2 \equiv d \mathbf{r} \cdot d \mathbf{r} = \left(d x^i \boldsymbol{\varepsilon}_i \right) \cdot \left( d x^j \boldsymbol{\varepsilon}_j \right) = \left( \boldsymbol{\varepsilon}_i \cdot \boldsymbol{\varepsilon}_j \right) dx^i dx^j = g_{ij} dx^i dx^j,
\end{equation}
where
\begin{equation}
g_{ij} \equiv \boldsymbol{\varepsilon}_i \cdot \boldsymbol{\varepsilon}_j
\end{equation}
is defined as the covariant metric \citep{Arfken7th}.  The contravariant metric is:
\begin{equation}
g^{ij} \equiv \boldsymbol{\varepsilon}^i \cdot \boldsymbol{\varepsilon}^j
\end{equation}
Note that, as a consequence of the chain rule applied to sequences of derivatives from the basis vectors, the following relations hold \citep{Arfken7th}:
\begin{equation}
\boldsymbol{\varepsilon}_i \cdot \boldsymbol{\varepsilon}^j = \delta_i^j
\end{equation}
\begin{equation}
g_{ik} g^{kj} = \delta_i^j
\end{equation}
Covariant and contravariant metrics can thus be related via a matrix inverse operation.  One may convert from covariant to contravariant bases by using these two metric tensors:
\begin{equation}
\boldsymbol{\varepsilon}_j = g_{ji} \boldsymbol{\varepsilon}^i
\end{equation}
\begin{equation}
\boldsymbol{\varepsilon}^j = g^{ji} \boldsymbol{\varepsilon}_i
\end{equation}
Components of vectors may similarly be transformed through the metric:
\begin{equation}
A_j = g_{ji} A^i
\end{equation}
\begin{equation}
A^j = g^{ji} A_i
\end{equation}

Because the dipole coordinate system is orthogonal, there are only three non-zero elements of the contravariant metric, $g^{ii}$ (no implied summation), and we have:
\begin{eqnarray}
g^{ij} \rightarrow \left[
\begin{array}{ccc}
\frac{R_e^4}{r^6} \left( 1 + 3 \cos \theta \right) & 0 & 0 \\
0 & \frac{1}{R_e^2} \frac{\left( 1+3 \cos \theta \right)}{\sin^6 \theta} & 0 \\
0 & 0 & \frac{1}{r^2 \sin^2 \theta}
\end{array}
\right]
\end{eqnarray}
The covariant metric is therefore represented by:
\begin{eqnarray}
g_{ij} \rightarrow \left[
\begin{array}{ccc}
\frac{r^6}{R_e^4 \left( 1 + 3 \cos \theta \right)} & 0 & 0 \\
0 & \frac{R_e ^2 \sin^6 \theta}{\left( 1+3 \cos \theta \right)} & 0 \\
0 & 0 & r^2 \sin^2 \theta
\end{array}
\right]
\end{eqnarray}
The metric factors, $h_i$, which we require for computing various derivatives and intergrals in GEMINI are defined by (no summation):
\begin{equation}
h_i \equiv \sqrt{g_{ii}}
\end{equation}
For our specific dipole system we have:
\begin{eqnarray}
h_1 = h_q &=& \frac{r^3}{R_e^2} \frac{1}{\sqrt{1+3 \cos^2 \theta}} \\
h_2 = h_p &=& \frac{R_e \sin^3 \theta}{\sqrt{1+3 \cos^2 \theta}} \\
h_3 = h_\phi &=& r \sin \theta
\end{eqnarray}

Finally, dipole unit vectors (separate quantities from the contravariant or covariant basis because they are unitless) may be derived by noting equivalence of various expressions for differential displacement $d \mathbf{r}$ and differential distance $dr$:
\begin{equation}
dr^2 = g_{ij} dx^i dx^j
\end{equation}
In orthogonal coordinates this simplifies to:
\begin{equation}
dr ^2 = g_{11} dx^1 dx^1 + g_{22} dx^2 dx^2 + g_{33} dx^3 dx^3 = \sum_i h_i^2 (dx_i)^2
\end{equation}
where in the last equality we have reverted to explicit summation and use of pure subscript indices.  It is notable in this equation that the elements of the summation $h_i^2 dx_i^2$ can be understood as differential distance (squared) in the $i^{th}$ direction.  Thus, we arrive at yet another expression for $d \mathbf{r}$:
\begin{equation}
d \mathbf{r} = \sum_i h_i dx^i \hat{\mathbf{e}}_i
\end{equation}
The $\hat{\mathbf{e}}_i$ vectors here are true unit vectors since the differentials accompanying them $h_i dx^i$ have units of distance (viz. meters).  Comparing with Equation \ref{eqn:dr}, we see that (no summation implied):
\begin{equation}
\hat{\mathbf{e}}_i = \frac{1}{h_i} \boldsymbol{\varepsilon}_i
\end{equation}
Alternatively we may write down the units vectors in terms of the contravariant basis (which may be easier to compute depending on the particular form of the coordinate transformation equations) (no sum implied over index $i$ - only $j$):
\begin{equation}
\hat{\mathbf{e}}_i = \frac{g_{ij}}{h_i} \boldsymbol{\varepsilon}^j
\end{equation}
In an orthogonal system only the diagonal elements of the metric are present, so that the unit vectors become (no summation implied):
\begin{equation}
\hat{\mathbf{e}}_i = \frac{g_{ii}}{h_i} \boldsymbol{\varepsilon}^i = h_i \boldsymbol{\varepsilon}^i
\end{equation}
Invoking this result with the contravariant basis vectors for the dipole coordinate system gives:
\begin{eqnarray}
\hat{\mathbf{e}}_1 &=& \frac{-3 \cos \theta \sin \theta \cos \phi}{\sqrt{1+3 \cos^2 \theta } } ~ \hat{\mathbf{e}}_x + \frac{-3 \cos \theta \sin \theta \sin \phi}{\sqrt{1+3 \cos^2 \theta } } ~ \hat{\mathbf{e}}_y + \frac{\left( 1 - 3 \cos^2 \theta \right) }{\sqrt{1+3 \cos^2 \theta } }~ \hat{\mathbf{e}}_z \\
\hat{\mathbf{e}}_2 &=& \frac{\cos \phi \left(1 - 3 \cos^2 \theta \right)}{\sqrt{1 + 3 \cos^2 \theta}} ~ \hat{\mathbf{e}}_x + \frac{\sin \phi \left(1 - 3 \cos^2 \theta \right)}{\sqrt{1 + 3 \cos^2 \theta}}~ \hat{\mathbf{e}}_y + ~  \frac{3 \cos \theta \sin \theta}{\sqrt{1+3 \cos^2 \theta }} \hat{\mathbf{e}}_z \\
\hat{\mathbf{e}}_3 &=& - \sin \phi ~ \hat{\mathbf{e}}_x + \cos \phi ~ \hat{\mathbf{e}}_y
\end{eqnarray}

\subsection{Tilted dipole approximations}

The $r,\theta,\phi$ coordinates of a point in space, are understood to be spherical coordinates measured from the center of the Earth's tilted magnetic dipole.  Unit vectors used for grid-related calculations are given, in tilted dipole, Cartesian coordinates by:
\begin{eqnarray}
\hat{\mathbf{e}}_r &=& \sin \theta \cos \phi ~ \hat{\mathbf{e}}_x + \sin \theta \sin \phi ~ \hat{\mathbf{e}}_y + \cos \theta ~ \hat{\mathbf{e}}_z \\
\hat{\mathbf{e}}_\theta &=& \cos \theta \cos \phi ~ \hat{\mathbf{e}}_x + \cos \theta \sin \phi ~ \hat{\mathbf{e}}_y - \sin \theta ~ \hat{\mathbf{e}}_z \\
\hat{\mathbf{e}}_\phi &=& -\sin \phi ~ \hat{\mathbf{e}}_x + \cos \phi ~ \hat{\mathbf{e}}_y
%\hat{\mathbf{e}}_q &\equiv& \frac{\frac{d \mathbf{x}}{d q}}{\left| \frac{d \mathbf{x}}{d q} \right|} \nonumber \\
% &=& \frac{-3 \cos \theta \sin \theta }{\sqrt{1+3 \cos^2 \theta } } \cos \phi ~ \hat{\mathbf{e}}_x + \frac{-3 \cos \theta \sin \theta }{\sqrt{1+3 \cos^2 \theta } } \sin \phi ~ \hat{\mathbf{e}}_y + \frac{-2 \cos^2 \theta + \sin^2 \theta }{\sqrt{1+3 \cos^2 \theta } } ~ \hat{\mathbf{e}}_z \\
%\hat{\mathbf{e}}_p &=& \hat{\mathbf{e}}_\phi \times \hat{\mathbf{e}}_q
\end{eqnarray}

%In deriving the $\hat{\mathbf{e}}_q$ unit vector use was made of the fact that:
%\begin{equation}
%\frac{d \mathbf{x}}{d q} = \frac{d}{d q} \left( r \sin \theta \cos \phi \hat{\mathbf{e}}_x + r \sin \theta \sin \phi \hat{\mathbf{e}}_y + r \cos \theta \hat{\mathbf{e}}_z \right)
%\end{equation}
%Dervitives needed to evaluate this unit vector are give by \citet{Schunk:2009}:
%\begin{eqnarray}
%\frac{d r}{d q} &=& \frac{-2 r^3 \cos \theta}{R_e^2 \left( 1 + 3 \cos^2 \theta \right)}\\
%\frac{d \theta}{d q} &=& \frac{-r^2 \sin \theta}{R_e^2 \left( 1 + 3 \cos^2 \theta \right)} \\
%\frac{d \phi}{d q} &=& 0 \\
%\end{eqnarray}

The magnetic field is computed from dipole formulas in spherical coordinates, i.e.:
\begin{equation}
\mathbf{B}=\frac{\mu_0 m}{4 \pi r^3} \left(2 \cos \theta ~ \hat{\mathbf{e}}_r + \sin \theta ~ \hat{\mathbf{e}}_\theta \right)
\end{equation}
where $m=7.94 \times 10^{22}$ (A m$^2$) is the magnetic moment of the Earth.  The magnetic field magnitude (and therefore $q$ and $x_1$ component is then given by:
\begin{equation}
B=\frac{\mu_0 m}{4 \pi r^3} \sqrt{1+3 \cos^2 \theta}
\end{equation}

%THE FOLLOWING IS TAKEN DIRECTLY FROM THE SOURCE CODE AND IS NOT THE SAME AS WHAT IS PRESENTED IN HUBA'S PAPER...  NEED TO LOOK INTO THIS...  I've checking consistency of these equations and they work out fine so I'm not worried...

To define a transformation from spherical dipole coordinates (i.e. geomagnetic coordinates) to geographic coordinates one must define the Earth's magnetic dipole tilt angles, taken in GEMINI to be:
\begin{eqnarray}
\theta_0 &=& 11.435^\circ \nonumber \\
\phi_0 &=& 290.24^\circ \nonumber
\end{eqnarray}
These are the zenith and azimuth angles, respectively, measured from the geographic (i.e. spin) axes to the magnetic axes.  The polar angle in the tilted dipole coordinate system (i.e. $\phi$) is then given by:
\begin{equation}
\theta = \cos^{-1} \left( \cos \theta_0 \cos \theta_g + \sin \theta_0 \sin \theta_g \cos \left( \phi_g - \phi_0 \right) \right)
\end{equation}
where $\theta_g,\phi_g$ are the angle of the location of interest in geographic coordinates.  The azimuth angle is then given by:
\begin{equation}
\alpha = \cos^{-1} \left( \frac{ \cos \theta_g - \cos \theta \cos \theta_0 }{\sin \theta \sin \theta_0} \right)
\end{equation}
\begin{equation}
\phi=
\begin{array}{c}
\pi - \alpha \quad (\phi_0 > \phi_g ~\land~ \phi_0-\phi_g > \pi ~~\lor~~ \phi_0 < \phi_g ~\land~ \phi_g - \phi_0 < \pi) \\
\alpha - \pi \quad (\mathrm{otherwise})
\end{array}
\end{equation}

The reverse transformations from geomagnetic to geographic are given by:
\begin{eqnarray}
\theta_g &=& \cos^{-1} \left( \cos \theta \cos \theta_0 - \sin \theta \sin \theta_0 \cos \phi \right) \\
\beta &=& \cos^{-1} \left( \frac{\cos \theta - \cos \theta_0 \cos \theta_g}{\sin \theta_0 \sin \theta_g} \right)
\end{eqnarray}
\begin{equation}
\phi_g=
\begin{array}{c}
\phi_0 - \beta \quad (\phi > \pi) \\
\phi_0 + \beta \quad (\phi \le \pi)
\end{array}
\end{equation}

\section{Numerical solution of fluid equations:  general approach and ordering}

The multi-dimensional fluid equations describing the time-depend evolution of density, drift, and temperature (Equations \ref{eqn:continuitycoord}, \ref{eqn:momentumcoord}, \ref{eqn:ionenergycoord} and \ref{eqn:electronenergycoord}) are solved sequentially through the use of a time-step splitting technique.  Generally speaking, each of these equations may be represented in the form:
\begin{equation}
\frac{\partial f}{\partial t} = \mathcal{L}_a \{f\} + \mathcal{L}_d \{f\} + \mathcal{L}_{ss} \{f\} + \mathcal{L}_{sn} \{f\},
\end{equation}
where $\mathcal{L}_{a,d,ss,sn}$ represent the advection, diffusion, stiff source, and non-stiff source operators, respectively.  These operators are split apart and dealt with sequentially in the model (the time-step splitting, or operator splitting technique).  The ordering used in GEMINI is that the advection operator is solved first.
\begin{equation}
\frac{\partial f}{\partial t} = \mathcal{L}_a \{f\}
\end{equation}
$\mathbf{v}_a$ is the advection velocity, and $\partial f / \partial t$ is an approximation of the time derivative.  The partially updated value of $f$ (which has been advected) is then processed through a non-stiff source step:
\begin{equation}
\frac{\partial f}{\partial t} = \mathcal{L}_{ns} \{f\}
\end{equation}
Then a diffusion-source step is applied to the solution that has already been updated through advection and non-stiff source solutions:
\begin{equation}
\frac{\partial f}{\partial t} = \mathcal{L}_d \{f\}
\end{equation}
Typically the time scales associated with diffusion and stiff source processes require that a implicit scheme (based on backward differences) be used for efficiency.  The advection terms are treated with explicit finite-volume methods since they only need to resolve time scales dictated by the ion sound speed.  The last split step is a solution for the stiff source terms (which is applied to the solution which has been partially updated through the advection, non-stiff source, and diffusion operations):
\begin{equation}
\frac{\partial f}{\partial t} = \mathcal{L}_{ss} \{f\}
\end{equation}
This sequence of substeps approximates the solution to the full transport equation.  Note that only the energy equations include all of these substeps; the continuity and momentum equations are solved in two steps:  an advection substep and then a stiff sources substep.

Along with the time-step splitting one must choose the order in which to resolve the the different substeps.  In principle any ordering will produce an approximate solution to the full equation, but we have found that solving the operators in order of increasing numerical stiffness (viz. the same order presented above) results in the most stable and accurate ordering.  Specifically, first advection is solved, then nonstiff sources, diffusion, and, finally, stiff source terms.  The solutions are organized such that the advection parts of all ion equations are solved together, followed by a solution for the non-stiff terms in each equation, followed by the diffusion terms for each equation.  Finally the Energy stiff source terms are resolved, followed by the momentum equation stiff terms, and then the continuity equation stiff terms.

\subsection{Finite difference approximations used in GEMINI}

The numerical schemes for solving the potential equation and diffusion substep of the fluid equation solutions all rely on finite difference approximations to partial derivatives.  This type of approximation is standard, but is modified slightly to deal with non-uniform grids required for efficient solution of the fluid equations.

A centered first derivative approximation at some point $x$ with neighboring grid points at $x-\Delta x_1$ and $x+\Delta x_2$ may be found from the expansions of some function $f$:
\begin{eqnarray}
f(x-\Delta x_1) &=& f(x) - f'(x)\Delta x_1 + f''(x) \frac{\Delta x_1^2}{2!} - f'''(x) \frac{\Delta x_1^3}{3!} + \dots \nonumber \\
f(x+\Delta x_2) &=& f(x) + f'(x)\Delta x_2 + f''(x) \frac{\Delta x_2^2}{2!} + f'''(x) \frac{\Delta x_2^3}{3!} + \dots \nonumber
\end{eqnarray}
Subtracting the first equation from the second gives an approximate first derivative.
\begin{equation}
f'(x) = \frac{f(x+\Delta x_2) - f(x-\Delta x_1)}{\Delta x_1+\Delta x_2} + \mathcal{O}(\Delta x_1-\Delta x_2) \label{1stderiv}
\end{equation}
Note that this is roughly second order accurate in space provided that $\Delta x_1 \approx \Delta x_2$, i.e. provided that the grid spacing does not change rapidly from one point to the next.  In grid index form:
\begin{equation}
\left[ \frac{\partial f}{\partial x} \right]_i \approx \frac{f_{i+1} - f_{i-1}}{x_{i+1} - x_{i-1}}
\end{equation}

A second derivative approximation is obtained by iteratively applying Equation \ref{1stderiv}.
\begin{equation}
\left[ \frac{\partial^2 f}{\partial x^2} \right]_i \approx \frac{\left( \left[ \frac{\partial f}{\partial x} \right]_{i+1/2} - \left[ \frac{\partial f}{\partial x} \right]_{i-1/2} \right)}{x_{i+1/2} - x_{i-1/2}}
\end{equation}
which makes use of the grid midpoints $x\pm1/2$.  We define, for use hereafter in this document, the forward, backward, and centered difference in $x$ by:
\begin{eqnarray}
\Delta x_{i,f} &=& x_{i+1} - x_i \nonumber \\
\Delta x_{i,b} &=& x_i - x_{i-1} \nonumber \\
\Delta x_{i,1/2} &=& x_{i+1/2} - x_{i-1/2} \nonumber
\end{eqnarray}
Using these definitions a finite difference form of the first derivative is:
\begin{equation}
\left[ \frac{\partial f}{\partial x} \right]_i \approx \frac{f_{i+1} - f_{i-1}}{\Delta x_{i,b}+\Delta x_{i,f}}
\end{equation}
The second derivative is:
\begin{equation}
\left[ \frac{\partial^2 f}{\partial x^2} \right]_i \approx \frac{f_{i+1} - f_i}{\Delta x_{i,f}\Delta x_{i,1/2}} - \frac{f_i - f_{i-1}}{\Delta x_{i,b}\Delta x_{i,1/2}}
\end{equation}

Cross partial derivative terms may be differenced as:
\begin{eqnarray}
\left[ \frac{\partial^2 f}{\partial x \partial y} \right]_{i,j} &\approx& \frac{1}{\Delta x_{i,b} + \Delta x_{i,f}}\left( \left[ \frac{\partial f}{\partial y} \right]_{i+1,j} - \left[ \frac{\partial f}{\partial y} \right]_{i-1,j} \right) \\
 &\approx& \frac{1}{\Delta x_{i,b} + \Delta x_{i,f}} \frac{1}{\Delta y_{j,b} + \Delta y_{j,f}} \left( \left( f_{i+1,j+1} - f_{i+1,j-1} \right) - \left( f_{i-1,j+1} - f_{i-1,j-1} \right) \right)
\end{eqnarray}
where the indices $i$ and $j$ correspond to the $x_1-$ and $x_2-$ dimensions, respectively.  Note that this differencing commutes, as the cross-partials should, e.g.:
\begin{equation}
\left[ \frac{\partial^2 f}{\partial x \partial y} \right]_{i,j} = \left[ \frac{\partial^2 f}{\partial y \partial x} \right]_{i,j}
\end{equation}

In several places in the model (e.g. in potential equation and heat conduction) it is necessary to difference a ``compound'' second order derivative term of the form:
\begin{equation}
\frac{\partial}{\partial x} \left( A \frac{\partial \Phi}{\partial x} \right) \label{eqn:compound}
\end{equation}
Two alternative finite difference approximations to this term may be derived by:  (1) expanding the derivative using the product rule and then generated a finite-difference approximation using the above expressions, and (2) by differencing the equation directly.  These approaches do not yield the same finite difference approximations.  GEMINI uses the latter method for differencing the diffusion part of the energy equaiton as it has been found to produce slightly more stable results under conditions of very strong ion and electron heating.  The FDE for this approach can be generated by sequentially applying first order differences to the expression, i.e:
\begin{equation}
\left[ \frac{\partial}{\partial x} \left( A \frac{\partial \Phi}{\partial x} \right) \right]_i \approx \frac{1}{\Delta x_{i,1/2}} \left( \left[ A \frac{\partial \Phi}{\partial x} \right]_{i+1/2} - \left[ A \frac{\partial \Phi}{\partial x} \right]_{i-1/2} \right)
\end{equation}
The derivative terms that appear in this expression can then be differenced as:
\begin{equation}
\left[ A \frac{\partial \Phi}{\partial x} \right]_{i+1/2} = \frac{A_{i+1/2}}{\Delta x_{i,f}} \left( \Phi_{i+1} - \Phi_{i} \right)
\end{equation}
where:
\begin{equation}
A_{i+1/2} \approx \frac{1}{2} \left( A_{i+1} + A_{i} \right)
\end{equation}
The final expression for the compound derivative of Equation \ref{eqn:compound} is, then, given by:
\begin{equation}
\left[ \frac{\partial}{\partial x} \left( A \frac{\partial \Phi}{\partial x} \right) \right]_i \approx \frac{1}{\Delta x_{i,1/2}} \left( \frac{A_{i+1/2}}{\Delta x_{i,f}} \left( \Phi_{i+1} - \Phi_{i} \right) - \frac{A_{i-1/2}}{\Delta x_{i,b}} \left( \Phi_{i} - \Phi_{i-1} \right) \right)
\label{eqn:compound2}
\end{equation}
Equation \ref{eqn:compound2} is used in differencing the potential and heat flow equations solved in GEMINI.

%An alternative to Equation \ref{eqn:compound2} this can be constructed by expanding the derivative prior to differencing:
%\begin{equation}
%\left[ \frac{\partial}{\partial x} \left( A \frac{\partial \Phi}{\partial x} \right) \right]_i = \left[ \frac{\partial A}{\partial x}\frac{\partial \Phi}{\partial x} + A \frac{\partial^2 \Phi}{\partial x^2} \right]_i \approx \left( \frac{A_{i+1}-A_{i-1}}{\Delta x_{i,b} + \Delta x_{i,f}} \right) \left( \frac{\Phi_{i+1}-\Phi_{i-1}}{\Delta x_{i,b} + \Delta x_{i,f}} \right) + A_i \left( \frac{\Phi_{i+1} - \Phi_i}{\Delta x_{i,f}\Delta x_{i,1/2}} - \frac{\Phi_i - \Phi_{i-1}}{\Delta x_{i,b}\Delta x_{i,1/2}} \right)
%\end{equation}
%%This form of differencing is used in the potential equation as it is most similar to the differencing scheme used for cross partial derivatives that appear in this equation.
%A third possibility would be:
%\begin{equation}
%\left[ \frac{\partial}{\partial x} \left( A \frac{\partial \Phi}{\partial x} \right) \right]_i \approx \frac{1}{\Delta x_{i,1/2}} \left( \left[ A \frac{\partial \Phi}{\partial x} \right]_{i+1} - \left[ A \frac{\partial \Phi}{\partial x} \right]_{i-1} \right) \approx ...
%\end{equation}
%but this form is not used at all in GEMINI.

%Compound derivatives with cross partial terms are slightly more complicated.  For example a term of the form:
%\begin{equation}
%\frac{\partial}{\partial x} \left( A \frac{\partial \Phi}{\partial y} \right) \label{eqn:compoundcross}
%\end{equation}
%can be differenced as:
%\begin{eqnarray}
%\left[ \frac{\partial}{\partial x} \left( A \frac{\partial \Phi}{\partial y} \right) \right]_{i,j} &=& \left[ \frac{\partial A}{\partial x} \frac{\partial \Phi}{\partial y} + A \frac{\partial^2 \Phi}{\partial x \partial y} \right]_i \approx \left( \frac{A_{i+1,j}-A_{i-1,j}}{\Delta x_{i,b} + \Delta x_{i,f}} \right) \left( \frac{\Phi_{i,j+1}-\Phi_{i,j-1}}{\Delta y_{j,b} + \Delta y_{j,f}} \right) \nonumber \\ &+& \frac{A_{i,j}}{\left( \Delta x_{i,b} + \Delta x_{i,f} \right) \left( \Delta y_{j,b} + \Delta y_{j,f} \right)} \left( \left( f_{i+1,j+1} - f_{i+1,j-1} \right) - \left( f_{i-1,j+1} - f_{i-1,j-1} \right) \right) \label{eqn:compoundcross2}
%\end{eqnarray}
%Note that this operation has effectively a six point stencil.  Other differencing possibilities exist for compound cross derivative, but GEMINI uses the form of Equation \ref{eqn:compoundcross2}.  In the potential equation (where this term appears) we take the convention of expanding out all derivatives with the product rule (for consistency) \emph{before} generating the FDE.

\subsection{Mathematical structure of advection terms}

The advection substep for the solution of Equations \ref{eqn:continuitycoord}, \ref{eqn:momentumcoord}, and \ref{eqn:ionenergycoord} consists of three, 2D or 3D advection equations (3D is used here):
\begin{eqnarray}
\frac{\partial \rho_s}{\partial t} + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( h_2 h_3 \rho_s v_{s1} \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_2} \left( h_1 h_3 \rho_s v_{s2} \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_3} \left( h_1 h_2 \rho_s v_{s3} \right) &=& 0 \nonumber \\
\frac{\partial}{\partial t} \left( \rho_s v_{s1} \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( h_2 h_3 \rho_s v_{s1}^2 \right) + \frac{1}{h_1^2 h_2 h_3} \frac{\partial}{\partial x_2} \left( h_1^2 h_3 \rho_s v_{s1} v_{s2} \right) + \frac{1}{h_1^2 h_2 h_3} \frac{\partial}{\partial x_3} \left( h_1^2 h_2 \rho_s v_{s1} v_{s3} \right) &=& 0 \nonumber \\
\frac{\partial}{\partial t} \left( \rho_s \epsilon_s \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( h_2 h_3 \rho_s \epsilon_s v_{s1} \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_2} \left( h_1 h_3 \rho_s \epsilon_s v_{s2} \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_3} \left( h_1 h_2 \rho_s \epsilon_s v_{s3} \right) &=& 0 \nonumber
\end{eqnarray}
The geometric terms that appear in the momentum equation (which are from the tensor derivative) are lumped in with the stiff source terms and not considered here.  The solution to these equations is advanced from time $t^n$ to time $t^{n+1}$ by adopting the previous time step values of drift as the set velocity with which the state variables $\rho_s,\rho_s v_{s1}$, and $\rho_s \epsilon_s$ are advected.  This advection velocity is denoted $\mathbf{u}$ and defined by:
\begin{equation}
\mathbf{u} \equiv [\mathbf{v}_s]^n
\end{equation}
Hence, for a known advection velocity $\mathbf{u}$, our system of advection equations reads:
\begin{eqnarray}
\frac{\partial \rho_s}{\partial t} + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( h_2 h_3 \rho_s u_1 \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_2} \left( h_1 h_3 \rho_s u_2 \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_3} \left( h_1 h_2 \rho_s u_3 \right) &=& 0 \nonumber \\
\frac{\partial}{\partial t} \left( \rho_s v_{s1} \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( h_2 h_3 \rho_s v_{s1} u_1 \right) + \frac{1}{h_1^2 h_2 h_3} \frac{\partial}{\partial x_2} \left( h_1^2 h_3 \rho_s v_{s1} u_2 \right) + \frac{1}{h_1^2 h_2 h_3} \frac{\partial}{\partial x_3} \left( h_1^2 h_2 \rho_s v_{s1} u_3 \right) &=& 0 \nonumber \\
\frac{\partial}{\partial t} \left( \rho_s \epsilon_s \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( h_2 h_3 \rho_s \epsilon_s u_1 \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_2} \left( h_1 h_3 \rho_s \epsilon_s u_2 \right) + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_3} \left( h_1 h_2 \rho_s \epsilon_s u_3 \right) &=& 0 \nonumber
\end{eqnarray}
Which can be solved for partially updated values of the state variables $\rho_s,\rho_s v_{s1}$, and $\rho_s \epsilon_s$ (these still need to be further updated with solutions of the other operators).

\subsection{Numerical solution of the advection terms}

A finite-volume method is used to solve the advection equation listed above.  This method is based on the high-resolution methods commonly used on modern computational fluid dynamics applications.  The update formulas for an advected quantity $f$ in slope-limited form, for a one-dimensional advection problem on a \emph{uniform grid} are given by \citet[][ pg. 113]{Leveque:2002}:
\begin{equation}
f_i^{n+1} = f_i^{n} + \frac{\Delta t}{\Delta z} \left( \varphi_{i+1/2}^n - \varphi_{i-1/2}^n \right)
\end{equation}
This formula advances, by one time step, the solution of the equation:
\begin{equation}
\frac{\partial f}{\partial t} + \frac{\partial}{\partial z} \left( f u \right) = 0
\end{equation}
The fluxes, in slope-limited form, are given by (see Leveque's book for a derivation):
\begin{equation}
\varphi_{i-1/2}^{n+1/2} = \left\{ \begin{array}{c}
u_{i-1/2} f_{i}^n - \frac{1}{2} u_{i-1/2} \left( \Delta z + u_{i-1/2} \Delta t \right) \sigma_i^n \quad (u_{i-1/2}  < 0) \\
u_{i-1/2} f_{i-1}^n + \frac{1}{2} u_{i-1/2} \left( \Delta z - u_{i-1/2} \Delta t \right) \sigma_{i-1}^n \quad (u_{i-1/2}  \ge 0)
\end{array} \right.
\end{equation}
where $\sigma$ is an approximation to the slope of the function $f(z)$, and $u$ is the advection velocity.  For a nonuniform grid the update formulas can be modified as:
\begin{equation}
f_i^{n+1} = f_i^{n} + \frac{\Delta t}{\Delta z_{i,1/2}} \left( \varphi_{i+1/2}^{n+1/2} - \varphi_{i-1/2}^{n+1/2} \right)
\end{equation}
The flux formulas become (THIS NEEDS TO BE CHECKED - IT'S NOT AT ALL CLEAR HOW I ARRIVE AT THESE FROM MY NOTES):
\begin{equation}
\varphi_{i-1/2} = \left\{ \begin{array}{c}
u_{i-1/2} f_{i}^n - \frac{1}{2} u_{i-1/2} \left( z_i - z_{i-1/2} + u_{i-1/2} \Delta t \right) \sigma_i^n \quad (u_{i-1/2}  < 0) \\
u_{i-1/2} f_{i-1}^n + \frac{1}{2} u_{i-1/2} \left( z_{i-1/2} - z_{i-1} - u_{i-1/2} \Delta t \right) \sigma_{i-1}^n \quad (u_{i-1/2}  \ge 0)
\end{array} \right.
\end{equation}
These can also be written in terms of backward differences:
\begin{equation}
\varphi_{i-1/2} = \left\{ \begin{array}{c}
u_{i-1/2} f_{i}^n - \frac{1}{2} u_{i-1/2} \left( \Delta z_{i,b} + u_{i-1/2} \Delta t \right) \sigma_i^n \quad (u_{i-1/2}  < 0) \\
u_{i-1/2} f_{i-1}^n + \frac{1}{2} u_{i-1/2} \left( \Delta z_{i,b} - u_{i-1/2} \Delta t \right) \sigma_{i-1}^n \quad (u_{i-1/2}  \ge 0)
\end{array} \right. \label{eqn:fluxes}
\end{equation}

In generalized curvlinear coordinates we need to solve a slightly different form of advection equation, e.g a one-dimensional curvilinear advection equation might read:
\begin{equation}
\frac{\partial \rho_s}{\partial t} + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( h_2 h_3 \rho_s u_1 \right)  = 0
\end{equation}
Note that the form will be different depending on what coordinate we are advecting with respect to, as well as what type of quantity is being advected (e.g. a scalar vs. rank-1 tensor).  Nonetheless the curvilinear advection equation will alway be of the form:
\begin{equation}
\frac{\partial f}{\partial t} + \frac{1}{h_\alpha^{(1)}} \frac{\partial}{\partial x_\alpha} \left( h_\alpha^{(2)} f u \right)  = 0
\end{equation}
for advection with respect to the $\alpha$ coordinate (denoted $x_\alpha$).  Differencing this general equation yields the update formula:
\begin{equation}
f^{n+1}_i = f^n_i + \frac{\Delta t}{ \left[ h_\alpha^{(1)} \right]_i ~ \Delta z_{i,1/2}} \left( \left[ h_\alpha^{(2)} \right]_{i+1/2} \varphi_{i+1/2}^n - \left[ h_\alpha^{(2)} \right]_{i-1/2} \varphi_{i-1/2}^n \right)
\end{equation}
In addition to this alteration the slope $\sigma$, which is measured with respect to the coordinate of advection needs to also be corrected.  As computed in the model, $\sigma$ has units of [a/b], where a is the units of the variable being advected and b is the units of the coordinate being advected with respect to.  In order for the flux values of Equation \ref{eqn:fluxes} to have the correct units, the velocity which multiplies the slope must be scaled using the metric factor of the dimension being advected, i.e.:
\begin{equation}
\varphi_{i-1/2} = \left\{ \begin{array}{c}
u_{i-1/2} f_{i}^n - \frac{1}{2} u_{i-1/2} \left( \Delta x_{1,i,b} + \frac{u_{1,i-1/2}}{\left[ h_1 \right]_{i-1/2}} \Delta t \right) \sigma_i^n \quad (u_{i-1/2}  < 0) \\
u_{i-1/2} f_{i-1}^n + \frac{1}{2} u_{i-1/2} \left( \Delta x_{1,i,b} - \frac{u_{1,i-1/2}}{\left[ h_1 \right]_{i-1/2}} \Delta t \right) \sigma_{i-1}^n \quad (u_{i-1/2}  \ge 0)
\end{array} \right. \label{eqn:fluxesfinal}
\end{equation}
Note that in Equation \ref{eqn:fluxesfinal}, the quantity $\frac{u_{1,i-1/2}}{\left[ h_1 \right]_{i-1/2} } \Delta t$ has the same units as $\Delta x_{1,i,b}$, as necessary to have a sensible update formula.

Advection in multiple dimensions is achieved by dimensional splitting, i.e. as a sequence of 1D advection problems.  For example the continuity advection substep is composed of the following subsubsteps:
\begin{equation}
\frac{\partial \rho_s}{\partial t} + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( h_2 h_3 \rho_s u_1 \right)  = 0
\end{equation}
\begin{equation}
\frac{\partial \rho_s}{\partial t} + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_2} \left( h_1 h_3 \rho_s u_2 \right)  = 0
\end{equation}
\begin{equation}
\frac{\partial \rho_s}{\partial t} + \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_3} \left( h_1 h_2 \rho_s u_3 \right) = 0
\end{equation}
where each substep update is applied using the previous step results as input.

\subsubsection{Boundary conditions for advection equations}

The boundary conditions for advection equations are implemented through the use of ghost cells \citep{Leveque:2002}.  Generally, values of the plasma fluid state variables in the ghost cells are set to those on the corresponding outermost point of the domain, i.e. free-flow boundaries.  Some exceptions to this exist at the top boundary ghost cells, which are often set to prevent excessive inflow into the simulation domain (see source code for details).

\subsection{Solution of nonstiff source terms}

The compression terms in the energy equation are treated as a nonstiff source substep that corresponds to solving the following equation:
\begin{equation}
\frac{\partial}{\partial t} \left( \rho_s \epsilon_s \right) = -p_s \frac{1}{h_1 h_2 h_3} \left( \frac{\partial}{\partial x_1} \left( h_2 h_3 v_{s1} \right) + \frac{\partial}{\partial x_2} \left( h_1 h_3 v_{s2} \right) + \frac{\partial}{\partial x_3} \left( h_1 h_2 v_{s3} \right) \right)
\end{equation}
These terms are resolved using a two-step Runge-Kutta method for the time integration, while computing the spatial derivatives with centered differences, resulting in the following update algorithm:
\begin{eqnarray}
\left[p_s \right]_{i,j,k}^{n} &=& \left( \gamma_s - 1 \right) \left[ \rho_s \epsilon_s \right]_{i,j,k}^{n} \nonumber \\
\left[ \rho_s \epsilon_s \right]_{i,j,k}^{n+1/2} &=& \left[ \rho_s \epsilon_s \right]_i^{n} - \frac{ \left[ p_s \right]_{i,j,k}^n }{\Delta t / 2} \left[ \frac{1}{h_1 h_2 h_3} \left( \frac{\partial}{\partial x_1} \left( h_2 h_3 v_{s1} \right) + \frac{\partial}{\partial x_2} \left( h_1 h_3 v_{s2} \right) + \frac{\partial}{\partial x_3} \left( h_1 h_2 v_{s3} \right) \right) \right]_{i,j,k}^n \nonumber \\
\left[ p_s \right]_{i,j,k}^{n+1/2} &=& \left( \gamma_s - 1 \right) \left[ \rho_s \epsilon_s \right]_{i,j,k}^{n+1/2} \nonumber \\
\left[ \rho_s \epsilon_s \right]_{i,j,k}^{n+1} &=& \left[ \rho_s \epsilon_s \right]_{i,j,k}^{n} - \frac{ \left[ p_s \right]_{i,j,k}^{n+1/2} }{\Delta t} \left[ \frac{1}{h_1 h_2 h_3} \left( \frac{\partial}{\partial x_1} \left( h_2 h_3 v_{s1} \right) + \frac{\partial}{\partial x_2} \left( h_1 h_3 v_{s2} \right) + \frac{\partial}{\partial x_3} \left( h_1 h_2 v_{s3} \right) \right) \right]_{i,j,k}^n \nonumber
\end{eqnarray}
Note that since this substep follows the advection substep, the partially updated (viz. advected) quantities are used as input to this sequence of steps.  An artifical viscosity (Section \ref{sec:artvisc}) is also included as a nonstiff source term.

\subsection{Solution of diffusion terms}

Since we do not include the effects of physical ion viscosity the energy equation is the only equation with a diffusion term (the heat flux term).  Solving the diffusion terms is best done with the temperature state variable, so the energy equation must first be converted into a temperature form before executing the diffusion time-step split.  The temperature form of the energy equation is given by \citep{Zettergren:2012}:
\begin{equation}
\frac{\partial T_s}{\partial t} + \mathbf{v}_s \cdot \nabla T_s = - \left( \gamma_s - 1 \right) T_s (\nabla \cdot \mathbf{v}_s)  + \frac{\left( \gamma_s - 1 \right)}{ n_s k_B} \nabla \cdot \left( \lambda_s \nabla T_s \right) -\sum_t \frac{n_s m_s \nu_{st}}{m_s + m_t}\left[ 2 (T_s-T_t) - \frac{2}{3} \frac{m_t}{k_B}(\mathbf{v}_s-\mathbf{v}_t)^2 \right].
\end{equation}
In order to solve the equation in this form the partially updated specific internal energy density is first converted into temperature by using the ideal gas law:
\begin{equation}
\rho_s \epsilon_s = \frac{\rho_s k_B T_s}{m_s \left( \gamma_s - 1 \right)}
\end{equation}

The diffusive term in this equation (corresponding to a second derivative in space), which is valid for ions, in curvilinear form is:
\begin{equation}
\frac{\partial T_s}{\partial t}  = \frac{\gamma_s - 1}{n_s k_B} \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( \frac{h_2 h_3}{h_1} \lambda_s \frac{\partial T_s}{\partial x_1} \right).
\end{equation}
For electrons we have an additional terms from the thermoelectric effect:
\begin{equation}
\frac{\partial T_e}{\partial t} = \frac{\gamma_e - 1}{n_e k_B} \frac{1}{h_1 h_2 h_3} \frac{\partial}{\partial x_1} \left( \frac{h_2 h_3}{h_1} \lambda_e \frac{\partial T_e}{\partial x_1} \right) + \frac{\gamma_e - 1}{n_e k_B} \frac{5}{2} \frac{k_B J_1}{|q_e|} \frac{1}{h_1} \frac{\partial T_e}{\partial x_1}
\end{equation}
For purposes of deriving a finite difference approximation to the diffusion equation, we represent ion and electron heat diffusion terms in the general form:
\begin{equation}
\frac{\partial T}{\partial t} =  A T  + B \frac{\partial T}{\partial x_1} + C \frac{\partial}{\partial x_1} \left( D \frac{\partial T}{\partial x_1} \right) + E \label{eqn:generaldiff}
\end{equation}
where the species subscript has been dropped for notational simplicity.  Note that the $A$ and $E$ factors are not present in either the ion or electron energy equation, but are kept in the solvers to preserve their generality.  The remaining factors are:
\begin{eqnarray}
C_i &=& \left[ \frac{\gamma_s - 1}{n_s k_B} \frac{1}{h_1 h_2 h_3} \right]_i \nonumber \\
D_i &=& \left[ \frac{h_2 h_3}{h_1} \lambda_s \right]_i \nonumber \\
D_{i+1/2} &\approx& \frac{1}{2} \left( D_i + D_{i+1} \right) \nonumber
\end{eqnarray}
Those are the only terms need to evaluate the ion equations.  For electron energy diffusion there is the additional term:
\begin{eqnarray}
B_i &=& \left[ \frac{\gamma_e - 1}{n_e k_B} \frac{5}{2} \frac{k_B J_1}{|q_e|} \frac{1}{h_1} \right]_i \nonumber
\end{eqnarray}

For convenience the diffusion PDE is represented in a compact form in the formulas that follow:
\begin{equation}
\frac{\partial T}{\partial t} = f(T)
\end{equation}
where $f(T)$ encapsulates the spatial derivatives.  These spatial derivatives are differenced using the approximations developed above, so that the finite difference form of $-f$ is (note the negative sign since that form is used in most equations developed below):
\begin{eqnarray}
-f(T^{n+1}) &\approx& a_i T^{n+1}_{i-1} + b_i T^{n+1}_{i} + c_i T^{n+1}_{i+1} - E_i\nonumber \\
&=& T^{n+1}_{i-1} \left( - \frac{C_i D_{i-1/2}}{\Delta x_{1,i,1/2} \Delta x_{1,i,b}} + \frac{B_i}{\Delta x_{1,i,b} + \Delta x_{1,i,f}} \right) \nonumber \\
&~&  T^{n+1}_{i} \left( - A_i + \frac{C_i D_{i+1/2}}{\Delta x_{1,i,1/2} \Delta x_{1,i,f}} + \frac{C_i D_{i-1/2}}{\Delta x_{1,i,1/2} \Delta x_{1,i,b}} \right) \nonumber \\
&~&  T^{n+1}_{i+1} \left( - \frac{C_i D_{i+1/2}}{\Delta x_{1,i,1/2} \Delta x_{1,i,f}} - \frac{B_i}{\Delta x_{1,i,b} + \Delta x_{1,i,f}} \right) \nonumber \\
&~& - E_i
\end{eqnarray}

Equation \ref{eqn:generaldiff} is solved using a second-order backward difference method, TRBDF2 \citep{Leveque:2002}.  This two-step implicit scheme allows large time steps, while still maintaining accuracy similar to that of the other numerical methods used in GEMINI.  The update formula for TRBDF2 is given by \citep{Leveque:2002}:
\begin{equation}
T_i^{n+1} = \frac{4}{3} T_i^{n+1/2} - \frac{1}{3} T_i^{n} + \frac{\Delta t}{3} f(T^{n+1})
\end{equation}
This equation can be rearranged to form a system of equations that can be solved to advanced the temperature solution forward a full time step (given $T_i^{n}$ and $T_i^{n+1/2}$).
\begin{equation}
\frac{T_i^{n+1}}{\frac{\Delta t}{3}} - f(T^{n+1})= \frac{\frac{4}{3} T_i^{n+1/2}}{\frac{\Delta t}{3}} - \frac{\frac{1}{3} T_i^{n}}{\frac{\Delta t}{3}} \label{eqn:TRBDF2}
\end{equation}

Equation \ref{eqn:TRBDF2} requires an approximation of the temperature advanced a half step forward in time, i.e. $T_i^{n+1/2}$.  This approximation is obtained from a trapezoidal approximation (i.e. Crank-Nicholson) to the time derivative:
\begin{equation}
\frac{T^{n+1} - T^n}{\Delta t} = \frac{1}{2} \left( f(T^{n+1}) + f(T^n) \right)
\end{equation}
Applied to a half time step update (what is needed to evaluate the TRBDF2 formula in Equation \ref{eqn:TRBDF2}), this approximation reads:
\begin{equation}
\frac{T^{n+1/2} - T^n}{\Delta t/2} = \frac{1}{2} \left( f(T^{n+1/2}) + f(T^n) \right)
\end{equation}
A system of equations for $T^{n+1/2}$ can be obtained by rearranging this equation:
\begin{equation}
\frac{T^{n+1/2}}{\Delta t/2} - \frac{1}{2} f(T^{n+1/2})= \frac{T^n}{\Delta t/2} + \frac{1}{2} f(T^n)
\end{equation}
Plugging in the finite difference form of the function $f(T)$ puts the system in the following form:
\begin{equation}
T^{n+1/2}_{i-1} \left( \frac{1}{2} a_i \right) + T^{n+1/2}_i \left( \frac{1}{\Delta t/2} + \frac{1}{2} b_i \right) + T^{n+1/2}_{i+1} \left( \frac{1}{2} c_i \right)  = \frac{T^n_i}{\Delta t/2} + E_i - T^{n}_{i-1} \left( \frac{1}{2} a_i \right) - T^{n}_i \left( \frac{1}{2} b_i \right) - T^{n}_{i+1} \left( \frac{1}{2} c_i \right)
\end{equation}
This system is solved using LAPACK's banded solver to produce the half time step update $T^{n+1/2}$.

One the half time step update has been computed, there is yet another system of equations to be solved (based on Equation \ref{eqn:TRBDF2}).
\begin{equation}
T^{n+1/2}_{i-1} a_i + T^{n+1}_i \left( \frac{1}{\Delta t/3} + b_i \right) + T^{n+1}_{i+1} c_i = \frac{\frac{4}{3} T_i^{n+1/2}}{\frac{\Delta t}{3}} - \frac{\frac{1}{3} T_i^{n}}{\frac{\Delta t}{3}} \label{eqn:TRBDF2}
\end{equation}

\subsubsection{Boundary conditions for diffusion equations}

The boundary conditions for solution of temperatures are taken to be $T_s=T_n$ at the bottom of the grid (i.e. the lowest altitude) and $T_s=T_\infty$ at the highest altitude (where $T_\infty$ is specified in the input file for the run).

\subsection{Solution for stiff source and sink terms}

The remainder of the continuity and momentum equations, after the advection is solved, can be expressed as a simple local production/loss ODE for each grid point.  These equations are all of the form:
\begin{equation}
\frac{\partial f}{\partial t} = A - B f,
\end{equation}
which for constant values of $A$ and $B$ has the analytical solution:
\begin{equation}
f(t) = \left[ \rho_s \right]_{i,j,k}^n e^{-L_{i,j}^n \Delta t} + \frac{m_s \left[ P_s \right]_{i,j,k}^n}{\left[ L_s \right]_{i,j,k}^n} \left( 1 - e^{-\left[ L_s \right]_{i,j,k}^n \Delta t} \right) \label{conSSsoln}
\end{equation}

\subsubsection{Continuity stiff source terms}

For the continuity equation, neglecting advection terms, we have:
\begin{equation}
\frac{\partial \rho_s }{\partial t} = m_s P_s - L_s \rho_s. \label{conSS}
\end{equation}
Strictly speaking, the production and loss terms for a given species will be dependent on the unknown values of densities for other species.  Thus, the ODE above is really a system of coupled equations - one equation for each species.  It is straighforward to derive an analytical solution to this set of equation, but calculations with this solution are computationally expensive as they require evaluations of exponentials of matrices.  A reasonable and efficient approximation in many cases is to simply take the values of $P_s,L_s$ from the results of the advection+nonstifff source substep calculations.  In this case we may analytically solve Equation \ref{conSS} for the updated value of density:
\begin{equation}
\left[ \rho_s \right]_{i,j,k}^{n+1} = \left[ \rho_s \right]_{i,j,k}^n e^{-L_{i,j}^n \Delta t} + \frac{m_s \left[ P_s \right]_{i,j,k}^n}{\left[ L_s \right]_{i,j,k}^n} \left( 1 - e^{-\left[ L_s \right]_{i,j,k}^n \Delta t} \right) \label{conSSsoln}
\end{equation}

The values for production loss are found using the standard set of reactions rates for the F-region ionosphere for species O$^+$, NO$^+$, N$_2^+$, O$_2^+$, N$^+$, and e$^-$.  An additional production term due to impact ionization can also easily be include with the $P_s$ term in this formulation.  The solution represented by Equation \ref{conSSsoln} is sometimes referred to as an exponential time differencing method.

\subsubsection{Momentum stiff source terms}

The momentum equation sans advection terms may be expressed in the form:
\begin{eqnarray}
\frac{\partial}{\partial t} \left( \rho_s v_{s1} \right) &=& \nonumber \\
~ &~& \frac{\rho_s v_{s2}^2}{h_1 h_2} \frac{\partial h_2}{\partial x_1} + \frac{\rho_s v_{s3}^2}{h_1 h_3} \frac{\partial h_3}{\partial x_1}  + \rho_s g_1 - \frac{1}{h_1} \frac{\partial p_s}{\partial x_1} + \frac{\rho_s q_s}{m_s} E_1 + \sum_t \rho_s \nu_{st} \left( v_{t1} - v_{s1} \right)
\end{eqnarray}
The solution is exactly the same form of Equation \ref{conSSsoln} with the production and loss terms replaced by the terms not directly containing $v_{s1}$ and coefficients of $v_{s1}$, respectively as denoted.
\begin{eqnarray}
P_s &=& \frac{\rho_s v_{s2}^2}{h_1 h_2} \frac{\partial h_2}{\partial x_1} + \frac{\rho_s v_{s3}^2}{h_1 h_3} \frac{\partial h_3}{\partial x_1}  + \rho_s g_1 - \frac{1}{h_1} \frac{\partial p_s}{\partial x_1} + \frac{\rho_s q_s}{m_s} E_1 + \sum_t \rho_s \nu_{st} v_{t1} \nonumber \\
L_s &=& \sum_t \rho_s \nu_{st}
\end{eqnarray}
As with the continuity equation these ``production'' and ``loss'' terms are evaluated with using parameters computed from the advection+nonstiff+diffused steps of the solution.  Note that the geometric terms that resulted from the momentum flux divergence are included with these source terms.  %They could instead have been included as nonstiff source terms in the momentum equation.

\subsubsection{Energy stiff source terms}

The stiff source/loss part of the ion energy equation reads:
\begin{equation}
\frac{\partial}{\partial t} \left( \rho_s \epsilon_s \right) = - \frac{1}{\gamma_s - 1} \sum_t \frac{\rho_s k_B \nu_{st}}{m_s + m_t} \left[ 2 \left( T_s - T_t \right) - \frac{2 m_t}{3 k_B} \left( \left( v_{s1} - v_{t1} \right)^2 + \left( v_{s2} - v_{t2} \right)^2 + \left( v_{s3} - v_{t3} \right)^2 \right) \right]
\end{equation}
Note the lack of compression and diffusion terms which have already been taken care of in previous substeps of the solution.  The collisions terms can be organized into corresponding source and loss terms with some manipulation.  Invoking the ideal gas law, this can be rewritten:
\begin{equation}
\frac{\partial}{\partial t} \left( \rho_s \epsilon_s \right) = \frac{1}{\gamma_s - 1} \sum_t \frac{\rho_s k_B \nu_{st}}{m_s + m_t} \left[ 2  T_t - \frac{2 m_t}{3 k_B} \left( \left( v_{s1} - v_{t1} \right)^2 + \left( v_{s2} - v_{t2} \right)^2 + \left( v_{s3} - v_{t3} \right)^2 \right) \right] - \left( \sum_t \frac{2 \nu_{st} m_s}{m_s + m_t} \right) \left( \rho_s \epsilon_s \right)
\end{equation}
From this form, the production and loss terms (which can be used with the numerical solution of Equation \ref{conSSsoln}) can be determined as:
\begin{eqnarray}
P_s &=& \frac{1}{\gamma_s - 1} \sum_t \frac{\rho_s k_B \nu_{st}}{m_s + m_t} \left[ 2  T_t - \frac{2 m_t}{3 k_B} \left( \left( v_{s1} - v_{t1} \right)^2 + \left( v_{s2} - v_{t2} \right)^2 + \left( v_{s3} - v_{t3} \right)^2 \right) \right] \nonumber \\
L_s &=& \sum_t \frac{2 \nu_{st} m_s}{m_s + m_t}
\end{eqnarray}

The electron energy equation has an additional energy production term corresponding to heat transfer from energetic electrons.  In addition there are various inelastic cooling terms that contribute to both production and loss terms in the numerical solutions.  These are taken from \citet{Swartz:1972} and \citet{Schunk:1978,Schunk:2009}, respectively.

\subsection{Von-Neumann Richtmyer artificial viscosity} \label{sec:artvisc}

An adjustable artificial viscosity is added into simulations to prevent artifacts in steep solutions.  For a single advection equation (or a set of independent advection equations) this is not necessary since the flux limiter is designed to prevent unphysical oscillations.  However, for a coupled system of equations there is more than one characteristic, hence, more than one type of discontinuity that one potentially needs to resolve.  One manner of dealing with this is to employ a characteristics-based method \citep[e.g.][]{Leveque:2002} to transform this system into a set of independent equations which can be reliably solved with a slope-limited method.  However, this is computationally expensive for a large system of equation as one has to repeatedly compute eigenvalue of large matrices.  Thus we do not do this in GEMINI, and, as a consequence, we must use other methods (viz. an artificial viscosity) to prevent artifacts around certain types of steep solutions.  The particular form used is the Von-Neumann Richtmyer artificial viscosity \citep{Richtmyer:1994} which is only significant in regions of strong compression tending to form discontinuities.  The stress for this artificial viscosity may be expressed in the form:
\begin{equation}
Q_{s,11} = \frac{1}{4} \rho_s \xi^2 min(\Delta v_1,0)^2
\end{equation}
where
\begin{equation}
\Delta v_{1,i} = v_{1,i+1/2} - v_{1,i-1/2}
\end{equation}
This stress contributes an additional term to both the momentum and energy equations corresponding to the force of viscosity and energy converted to internal energy by viscosity, respectively.  Hence, these equations, for numerical purposes have the form:
\begin{equation}
\left[ \frac{\partial }{\partial t} \left( \rho_s \mathbf{v}_s \right) + \nabla \cdot \left( \rho_s \mathbf{v}_s \mathbf{v}_s \right) \right] \cdot \hat{\mathbf{e}}_1 = \left[ -\nabla p_s - \nabla \cdot \mathbf{Q}_s + \rho_s \mathbf{g} + \frac{\rho_s} {m_s} q_s \mathbf{E} + \sum_t \rho_s \nu_{st} \left(\mathbf{v}_t - \mathbf{v}_s \right) \right] \cdot \hat{\mathbf{e}}_1 \label{eqn:stressmom}
\end{equation}
\begin{eqnarray}
\frac{\partial}{\partial t} \left( \rho_s \epsilon_s \right) + \nabla \cdot \left( \rho_s \epsilon_s \mathbf{v}_s \right) &=& - p_s (\nabla \cdot \mathbf{v}_s) - \nabla \cdot \mathbf{h}_s - \mathbf{Q}_s : \nabla \mathbf{v}_s - \\
\nonumber&& \frac{1}{(\gamma_s - 1 )}\sum_t \frac{\rho_s k_B \nu_{st}}{m_s + m_t}\left[ 2 (T_s-T_t) - \frac{2}{3} \frac{m_t}{k_B}(\mathbf{v}_s-\mathbf{v}_t)^2 \right]  \label{eqn:stressen}
\end{eqnarray}

%Since only the $x_1$ component...
%I NEED TO CONSULT POTTER HERE TO SEE HOW DIV STRESS CAN BE CONVERTED INTO GRAD PSEUDO-STRESS...  AM I FORGETTING SOME METRIC FACTORS IN THE PRESENT VERSION OF THE CODE?  MAYBE NOT THOUGH BECAUSE THE TENSOR DIVERGENCE CAN BE MANIPULATED INTO A GRADIENT, RIGHT - but this requires isotropy - which may not exist...?

\section{Numerical solution for electric potential}

Equation \ref{eqn:curvpotEFL} is discretized on a 2D, nonuniform grid $\Phi(x_2,x_3) \rightarrow \Phi_{j,k}$.   Substitution of the finite differences into Equation \ref{eqn:curvpotEFL} gives an equation for each grid point of the simulation.
\begin{eqnarray}
 \left( \frac{ \left[ \Sigma_P^{(3)} \right]_{j,k-1/2}}{\Delta x_{3,k,1/2} \Delta x_{3,k,b}} + \frac{ \left[ \frac{\partial \Sigma_H'}{\partial x_2} \right]_{j,k}}{\Delta x_{3,k,b} + \Delta x_{3,k,f}} \right) \Phi_{j,k-1} ~ + &~& \nonumber \\
 \left( \frac{ \left[ \Sigma_P^{(2)} \right]_{j-1/2,k} }{\Delta x_{2,j,1/2} \Delta x_{2,j,b}} - \frac{ \left[ \frac{\partial \Sigma_H'}{\partial x_3} \right]_{j,k}}{\Delta x_{2,j,b} + \Delta x_{2,j,f}}\right) \Phi_{j-1,k} ~ + &~& \nonumber \\
 \left( - \frac{ \left[ \Sigma_P^{(2)} \right]_{j+1/2,k}} {\Delta x_{2,j,1/2} \Delta x_{2,j,f}} - \frac{ \left[ \Sigma_P^{(2)}\right]_{j-1/2,k} }{\Delta x_{2,j,1/2} \Delta x_{2,j,b}} - \frac{ \left[ \Sigma_P^{(3)} \right]_{j,k+1/2}} {\Delta x_{3,k,1/2} \Delta x_{3,k,f}} - \frac{ \left[ \Sigma_P^{(3)}\right]_{j,k-1/2} }{\Delta x_{3,k,1/2} \Delta x_{3,k,b}}  \right) \Phi_{j,k} ~ + &~& \nonumber \\
 \left( \frac{ \left[ \Sigma_P^{(2)} \right]_{j+1/2,k} }{\Delta x_{2,j,1/2} \Delta x_{2,j,f}} + \frac{\left[ \frac{\partial \Sigma_H'}{\partial x_3} \right]_{j,k}}{\Delta x_{2,j,b} + \Delta x_{2,j,f} }\right) \Phi_{j+1,k} + &~& \nonumber \\
 \left( \frac{ \left[ \Sigma_P^{(3)} \right]_{j,k+1/2}}{\Delta x_{3,k,1/2} \Delta x_{3,k,f}} - \frac{ \left[ \frac{\partial \Sigma_H'}{\partial x_2} \right]_{j,k}}{\Delta x_{3,k,b} + \Delta x_{3,k,f}} \right) \Phi_{j,k+1} ~ &~& \nonumber \\
 ~ = \left[ \int h_1 h_2 h_3 \left\{ \nabla_\perp \cdot \left[ \boldsymbol{\sigma}_\perp \cdot \left( \mathbf{v}_{n\perp} \times \mathbf{B} \right) \right] + \nabla_\perp \cdot \left( \boldsymbol{\Sigma}_\perp \cdot \mathbf{E}_{0\perp} \right) \right\} d x_1 + \left[ h_2 h_3 J_1 \right] \left|^{x_{1,max}}_{x_{1,min}} \right. \right]_{i,j}
\label{eqn:FDE}
\end{eqnarray}
This forms a system of equations that can be solved for the interior grid point potential values given some set of boundary conditions.  The size of this system is $l_{x2} \times l_{x3}$, where $l_{x2}$ is the number of grid points in the $x_2$-direction and $l_{x3}$ is the number of grid points in the $x_3$-direction.

\subsection{Boundary conditions for the electric potential}

For solving the elliptic partial differential equation defined by Equation \ref{eqn:curvpotEFL}, boundary conditions are needed.  This equation has been field-line integrated, so we only need boundary conditions for the lateral ($x_2$, $x_3$) sides of the domain.  These are assumed to be equipotentials (i.e. grounded) in the code.

\subsection{Solving the system of potential finite-difference equations}

The system of equations defined by Equation \ref{eqn:FDE} is solved via direct LU factorization using the MUMPS software package \citep{Amestoy:2001,Amestoy:2006}.

\subsection{Solution of ionospheric potential with first-order polarization current}

Note, as indicated previously, that the polarization current terms in the potential equation are not formulated properly for curvilinear grids (the GEMINI simulation will exit automatically with an error) and only work on a Cartesian grid.  All grids other than Cartesian should flag the inertial capacitance as zero in the input file so that the electrodynamic terms are not erroneously used.  Finite difference approximations for the derivative terms corresponding the polarization produces a very different system of equations to be solved.

The time dependent term in Equation \ref{eqn:electrodynamic}, can be differenced as:
\begin{eqnarray}
\left\{ \frac{\partial}{\partial x_2} \left[ C_M \frac{\partial}{\partial t} \left( \frac{\partial \Phi}{\partial x_2} \right) \right] + \frac{\partial}{\partial x_3} \left[ C_M \frac{\partial}{\partial t} \left( \frac{\partial \Phi}{\partial x_3} \right) \right] \right\}_{j,k}^n &\approx& \nonumber \\
\Phi_{j,k+1}^n \left( \frac{[C_M]_{j,k+1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,f}} \right) &+& \nonumber \\
\Phi_{j+1,k}^n \left( \frac{[C_M]_{j+1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,f}} \right) &+& \nonumber \\
\Phi_{j,k}^n \left( -\frac{[C_M]_{j+1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,f}}  - \frac{[C_M]_{j-1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,b}} - \frac{[C_M]_{j,k+1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,f}} - \frac{[C_M]_{j,k-1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,b}} \right) &+& \nonumber \\
\Phi_{j-1,k}^n \left( \frac{[C_M]_{j-1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,b}} \right) &+& \nonumber \\
\Phi_{j,k-1}^n \left( \frac{[C_M]_{j,k-1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,b}} \right) &-& \nonumber \\
\Phi_{j,k+1}^{n-1} \left( \frac{[C_M]_{j,k+1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,f}} \right) &-& \nonumber \\
\Phi_{j+1,k}^{n-1} \left( \frac{[C_M]_{j+1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,f}} \right) &-& \nonumber \\
\Phi_{j,k}^{n-1} \left( -\frac{[C_M]_{j+1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,f}}  - \frac{[C_M]_{j-1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,b}} - \frac{[C_M]_{j,k+1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,f}} - \frac{[C_M]_{j,k-1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,b}} \right) &-& \nonumber \\
\Phi_{j-1,k}^{n-1} \left( \frac{[C_M]_{j-1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,b}} \right) &-& \nonumber \\
\Phi_{j,k-1}^{n-1} \left( \frac{[C_M]_{j,k-1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,b}} \right)
\end{eqnarray}
Note that the time derivative used here is a first-order backward difference in time; this is justified by the relatively small time steps taken compared to the evolution of the electric potential.  The terms that multiple $\Phi^{n-1}$ are included in the right-hand-side of the potential equation (with opposite signs), since the value of the potential from the previous time step is known.  Note also, that the current value of the inertial capacitance $C_M^n$ is used to compute all coefficients; future versions may wish to store the previous time step inertial capacitance to improve accuracy.  However, this is unlikely to have a large effect on the simulations, again because the inertial capacitance changes very slowly compared to typical time steps (which is dictated by CFL conditions in the fluid part of GEMINI).

One piece of the convective term from Equation \ref{eqn:electrodynamic} can be differenced as:
\begin{eqnarray}
\left\{ \frac{\partial}{\partial x_2} \left( C_M v_2 \frac{\partial^2 \Phi }{\partial x_2^2} \right) \right\}_{j,k} &\approx& \nonumber \\
\Phi_{j+2,k} \left( \frac{[C_M]_{j+1,k} [v_{2}]_{j+1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,f} \Delta x_{2,j+1,1/2} \right)} \right) &+& \nonumber \\
\Phi_{j+1,k} \left( \frac{-[C_M]_{j+1,k} [v_{2}]_{j+1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,f} \Delta x_{2,j+1,1/2} \right)} - \frac{[C_M]_{j+1,k} [v_{2}]_{j+1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,b} \Delta x_{2,j+1,1/2} \right)} \right) &+& \nonumber \\
\Phi_{j,k} \left(  \frac{[C_M]_{j+1,k} [v_{2}]_{j+1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,b} \Delta x_{2,j+1,1/2} \right)}  - \frac{[C_M]_{j-1,k} [v_{2}]_{j-1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,b} \Delta x_{2,j-1,1/2} \right)}  \right) &+& \nonumber \\
\Phi_{j-1,k} \left( \frac{[C_M]_{j-1,k} [v_{2}]_{j-1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,f} \Delta x_{2,j-1,1/2} \right)} + \frac{[C_M]_{j-1,k} [v_{2}]_{j-1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,b} \Delta x_{2,j-1,1/2} \right)} \right) &+& \nonumber \\
\Phi_{j-2,k} \left( - \frac{[C_M]_{j-1,k} [v_{2}]_{j-1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,b} \Delta x_{2,j-1,1/2} \right)} \right)
\end{eqnarray}

Similarly the complementary piece of the convective derivative is given by:
\begin{eqnarray}
\left\{ \frac{\partial}{\partial x_3} \left( C_M v_3 \frac{\partial^2 \Phi }{\partial x_3^2} \right) \right\}_{j,k} &\approx& \nonumber \\
\Phi_{j,k+2} \left( \frac{[C_M]_{j,k+1} [v_{3}]_{j,k+1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,f} \Delta x_{3,k+1,1/2} \right)} \right) &+& \nonumber \\
\Phi_{j,k+1} \left( \frac{-[C_M]_{j,k+1} [v_{3}]_{j,k+1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,f} \Delta x_{3,k+1,1/2} \right)} - \frac{[C_M]_{j,k+1} [v_{3}]_{j,k+1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,b} \Delta x_{3,k+1,1/2} \right)} \right) &+& \nonumber \\
\Phi_{j,k} \left(  \frac{[C_M]_{j,k+1} [v_{3}]_{j,k+1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,b} \Delta x_{3,k+1,1/2} \right)}  - \frac{[C_M]_{j,k-1} [v_{3}]_{j,k-1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,b} \Delta x_{3,k-1,1/2} \right)}  \right) &+& \nonumber \\
\Phi_{j,k-1} \left( \frac{[C_M]_{j,k-1} [v_{3}]_{j,k-1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,f} \Delta x_{3,k-1,1/2} \right)} + \frac{[C_M]_{j,k-1} [v_{3}]_{j,k-1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,b} \Delta x_{3,k-1,1/2} \right)} \right) &+& \nonumber \\
\Phi_{j,k-2} \left( - \frac{[C_M]_{j,k-1} [v_{3}]_{j,k-1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,b} \Delta x_{3,k-1,1/2} \right)} \right)
\end{eqnarray}

Another part of the convective derivative term, containing cross-partial derivatives is:
\begin{eqnarray}
\left\{ \frac{\partial}{\partial x_2} \left( C_M v_3 \frac{\partial^2 \Phi }{\partial x_3 \partial x_2} \right) \right\}_{j,k} &\approx& \nonumber \\
\Phi_{j+2,k+1} \left( \frac{[C_M]_{j+1,k} [v_3]_{j+1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,f}+\Delta x_{2,j+1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) } \right) &+& \nonumber \\
\Phi_{j,k+1} \left( \frac{-[C_M]_{j+1,k} [v_3]_{j+1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,f}+\Delta x_{2,j+1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) } \right. + &~& \nonumber \\
\left. \frac{-[C_M]_{j-1,k} [v_3]_{j-1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,f}+\Delta x_{2,j-1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) } \right) &+& \nonumber \\
\Phi_{j-2,k+1} \left( \frac{[C_M]_{j-1,k} [v_3]_{j-1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,f}+\Delta x_{2,j-1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) } \right) &+& \nonumber \\
\Phi_{j+2,k-1} \left( \frac{-[C_M]_{j+1,k} [v_3]_{j+1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,f}+\Delta x_{2,j+1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) } \right) &+& \nonumber \\
\Phi_{j,k-1} \left( \frac{[C_M]_{j+1,k} [v_3]_{j+1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,f}+\Delta x_{2,j+1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) } \right. + &~& \nonumber \\
\left. \frac{[C_M]_{j-1,k} [v_3]_{j-1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,f}+\Delta x_{2,j-1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) } \right) &+& \nonumber \\
\Phi_{j-2,k-1} \left( \frac{[C_M]_{j-1,k} [v_3]_{j-1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,f}+\Delta x_{2,j-1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) } \right)
\end{eqnarray}

The complementary (and final) part of the convective derivative term involving cross-partials is:
\begin{eqnarray}
\left\{ \frac{\partial}{\partial x_3} \left( C_M v_2 \frac{\partial^2 \Phi }{\partial x_2 \partial x_3} \right) \right\}_{j,k} &\approx& \nonumber \\
\Phi_{j+1,k+2} \left( \frac{[C_M]_{j,k+1} [v_2]_{j,k+1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,f}+\Delta x_{3,k+1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) } \right) &+& \nonumber \\
\Phi_{j+1,k} \left( \frac{-[C_M]_{j,k+1} [v_2]_{j,k+1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,f}+\Delta x_{3,k+1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) } \right. + &~& \nonumber \\
\left. \frac{-[C_M]_{j,k-1} [v_2]_{j,k-1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,f}+\Delta x_{3,k-1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) } \right) &+& \nonumber \\
\Phi_{j+1,k-2} \left( \frac{[C_M]_{j,k-1} [v_2]_{j,k-1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,f}+\Delta x_{3,k-1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) } \right) &+& \nonumber \\
\Phi_{j-1,k+2} \left( \frac{-[C_M]_{j,k+1} [v_2]_{j,k+1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,f}+\Delta x_{3,k+1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) } \right) &+& \nonumber \\
\Phi_{j-1,k} \left( \frac{[C_M]_{j,k+1} [v_2]_{j,k+1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,f}+\Delta x_{3,k+1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) } \right. + &~& \nonumber \\
\left. \frac{[C_M]_{j,k-1} [v_2]_{j,k-1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,f}+\Delta x_{3,k-1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) } \right) &+& \nonumber \\
\Phi_{j-1,k-2} \left( \frac{[C_M]_{j,k-1} [v_2]_{j,k-1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,f}+\Delta x_{3,k-1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) } \right)
\end{eqnarray}

As indicated by the relatively length expressions for the finite difference terms above, inclusion of the polarization current causes signficant fill-in in the matrix that needs to be solved; i.e. there are now 17 bands instead of the 5 bands with the static system given in Equation \ref{eqn:FDE}:
\begin{eqnarray}
A \Phi_{j+1,k+2} + B \Phi_{j,k+2} + C \Phi_{j-1,k+2} + D \Phi_{j+2,k+1} +  E \Phi_{j,k+1} + F \Phi_{j-2,k+1} + G \Phi_{j+2,k} &+& \nonumber \\
 H \Phi_{j+1,k} + I \Phi_{j,k} + J \Phi_{j-1,k} + K \Phi_{j-2,k} + L \Phi_{j+2,k-1} + M \Phi_{j,k-1} + N \Phi_{j-2,k-1} &+& \nonumber \\
  O \Phi_{j+1,k-2} + P \Phi_{j,k-2} + Q \Phi_{j-1,k-2} = R
\end{eqnarray}
The coefficients appearing in this equations are given by the following formulas:
\begin{equation}
A = \frac{[C_M]_{j,k+1} [v_2]_{j,k+1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,f}+\Delta x_{3,k+1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) }
\end{equation}
\begin{equation}
B = \frac{[C_M]_{j,k+1} [v_{3}]_{j,k+1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,f} \Delta x_{3,k+1,1/2} \right)}
\end{equation}
\begin{equation}
C = \frac{-[C_M]_{j,k+1} [v_2]_{j,k+1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,f}+\Delta x_{3,k+1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) }
\end{equation}
\begin{equation}
D = \frac{[C_M]_{j+1,k} [v_3]_{j+1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,f}+\Delta x_{2,j+1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) }
\end{equation}
\begin{eqnarray}
E = \frac{[C_M]_{j,k+1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,f}} &+& \nonumber \\
\frac{-[C_M]_{j,k+1} [v_{3}]_{j,k+1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,f} \Delta x_{3,k+1,1/2} \right)} - \frac{[C_M]_{j,k+1} [v_{3}]_{j,k+1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,b} \Delta x_{3,k+1,1/2} \right)} &+& \nonumber \\
\frac{-[C_M]_{j+1,k} [v_3]_{j+1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,f}+\Delta x_{2,j+1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) } + &~& \nonumber \\
 \frac{-[C_M]_{j-1,k} [v_3]_{j-1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,f}+\Delta x_{2,j-1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) } &+& \nonumber \\
\frac{ \left[ \Sigma_P \right]_{j,k+1/2}}{\Delta x_{3,k,1/2} \Delta x_{3,k,f}} - \frac{ \left[ \frac{\partial \Sigma_H}{\partial x_2} \right]_{j,k}}{\Delta x_{3,k,b} + \Delta x_{3,k,f}}
\end{eqnarray}
\begin{equation}
F = \frac{[C_M]_{j-1,k} [v_3]_{j-1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,f}+\Delta x_{2,j-1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) }
\end{equation}
\begin{equation}
G = \frac{[C_M]_{j+1,k} [v_{2}]_{j+1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,f} \Delta x_{2,j+1,1/2} \right)}
\end{equation}
\begin{eqnarray}
H = \frac{[C_M]_{j+1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,f}} &+& \nonumber \\
\frac{-[C_M]_{j+1,k} [v_{2}]_{j+1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,f} \Delta x_{2,j+1,1/2} \right)} - \frac{[C_M]_{j+1,k} [v_{2}]_{j+1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,b} \Delta x_{2,j+1,1/2} \right)} &+& \nonumber \\
\frac{-[C_M]_{j,k+1} [v_2]_{j,k+1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,f}+\Delta x_{3,k+1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) } + &~& \nonumber \\
 \frac{-[C_M]_{j,k-1} [v_2]_{j,k-1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,f}+\Delta x_{3,k-1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) } &+& \nonumber \\
\frac{ \left[ \Sigma_P \right]_{j+1/2,k} }{\Delta x_{2,j,1/2} \Delta x_{2,j,f}} + \frac{\left[ \frac{\partial \Sigma_H}{\partial x_3} \right]_{j,k}}{\Delta x_{2,j,b} + \Delta x_{2,j,f} }
\end{eqnarray}
\begin{eqnarray}
I = -\frac{[C_M]_{j+1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,f}}  - \frac{[C_M]_{j-1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,b}} - \frac{[C_M]_{j,k+1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,f}} - \frac{[C_M]_{j,k-1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,b}} &+& \nonumber \\
\frac{[C_M]_{j+1,k} [v_{2}]_{j+1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,b} \Delta x_{2,j+1,1/2} \right)}  - \frac{[C_M]_{j-1,k} [v_{2}]_{j-1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,b} \Delta x_{2,j-1,1/2} \right)} &+& \nonumber \\
\frac{[C_M]_{j,k+1} [v_{3}]_{j,k+1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,b} \Delta x_{3,k+1,1/2} \right)}  - \frac{[C_M]_{j,k-1} [v_{3}]_{j,k-1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,b} \Delta x_{3,k-1,1/2} \right)} &+& \nonumber \\
- \frac{ \left[ \Sigma_P \right]_{j+1/2,k}} {\Delta x_{2,j,1/2} \Delta x_{2,j,f}} - \frac{ \left[ \Sigma_P \right]_{j-1/2,k} }{\Delta x_{2,j,1/2} \Delta x_{2,j,b}} - \frac{ \left[ \Sigma_P \right]_{j,k+1/2}} {\Delta x_{3,k,1/2} \Delta x_{3,k,f}} - \frac{ \left[ \Sigma_P \right]_{j,k-1/2} }{\Delta x_{3,k,1/2} \Delta x_{3,k,b}}
\end{eqnarray}
\begin{eqnarray}
J = \frac{[C_M]_{j-1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,b}} &+& \nonumber \\
\frac{[C_M]_{j-1,k} [v_{2}]_{j-1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,f} \Delta x_{2,j-1,1/2} \right)} + \frac{[C_M]_{j-1,k} [v_{2}]_{j-1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,b} \Delta x_{2,j-1,1/2} \right)} &+& \nonumber \\
\frac{[C_M]_{j,k+1} [v_2]_{j,k+1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k+1,f}+\Delta x_{3,k+1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) } + &~& \nonumber \\
\frac{[C_M]_{j,k-1} [v_2]_{j,k-1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,f}+\Delta x_{3,k-1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) } &+& \nonumber \\
\frac{ \left[ \Sigma_P \right]_{j-1/2,k} }{\Delta x_{2,j,1/2} \Delta x_{2,j,b}} - \frac{ \left[ \frac{\partial \Sigma_H}{\partial x_3} \right]_{j,k}}{\Delta x_{2,j,b} + \Delta x_{2,j,f}}
\end{eqnarray}
\begin{equation}
K = - \frac{[C_M]_{j-1,k} [v_{2}]_{j-1,k}}{\left( \Delta x_{2,j,f} + \Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,b} \Delta x_{2,j-1,1/2} \right)}
\end{equation}
\begin{equation}
L = \frac{-[C_M]_{j+1,k} [v_3]_{j+1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,f}+\Delta x_{2,j+1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) }
\end{equation}
\begin{eqnarray}
M = \frac{[C_M]_{j,k-1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,b}} &+& \nonumber \\
\frac{[C_M]_{j,k-1} [v_{3}]_{j,k-1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,f} \Delta x_{3,k-1,1/2} \right)} + \frac{[C_M]_{j,k-1} [v_{3}]_{j,k-1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,b} \Delta x_{3,k-1,1/2} \right)} &+& \nonumber \\
\frac{[C_M]_{j+1,k} [v_3]_{j+1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j+1,f}+\Delta x_{2,j+1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) } + &~& \nonumber \\
 \frac{[C_M]_{j-1,k} [v_3]_{j-1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,f}+\Delta x_{2,j-1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) } &+& \nonumber \\
 \frac{ \left[ \Sigma_P \right]_{j,k-1/2}}{\Delta x_{3,k,1/2} \Delta x_{3,k,b}} + \frac{ \left[ \frac{\partial \Sigma_H}{\partial x_2} \right]_{j,k}}{\Delta x_{3,k,b} + \Delta x_{3,k,f}}
\end{eqnarray}
\begin{equation}
N = \frac{[C_M]_{j-1,k} [v_3]_{j-1,k} }{ \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) \left( \Delta x_{2,j-1,f}+\Delta x_{2,j-1,b} \right) \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) }
\end{equation}
\begin{equation}
O = \frac{[C_M]_{j,k-1} [v_2]_{j,k-1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,f}+\Delta x_{3,k-1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) }
\end{equation}
\begin{equation}
P = - \frac{[C_M]_{j,k-1} [v_{3}]_{j,k-1}}{\left( \Delta x_{3,k,f} + \Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,b} \Delta x_{3,k-1,1/2} \right)}
\end{equation}
\begin{equation}
Q = \frac{[C_M]_{j,k-1} [v_2]_{j,k-1} }{ \left( \Delta x_{3,k,f}+\Delta x_{3,k,b} \right) \left( \Delta x_{3,k-1,f}+\Delta x_{3,k-1,b} \right) \left( \Delta x_{2,j,f}+\Delta x_{2,j,b} \right) }
\end{equation}
\begin{eqnarray}
R = \Phi_{j,k+1}^{n-1} \left( \frac{[C_M]_{j,k+1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,f}} \right) &+& \nonumber \\
\Phi_{j+1,k}^{n-1} \left( \frac{[C_M]_{j+1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,f}} \right) &+& \nonumber \\
\Phi_{j,k}^{n-1} \left( -\frac{[C_M]_{j+1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,f}}  - \frac{[C_M]_{j-1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,b}} - \frac{[C_M]_{j,k+1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,f}} - \frac{[C_M]_{j,k-1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,b}} \right) &+& \nonumber \\
\Phi_{j-1,k}^{n-1} \left( \frac{[C_M]_{j-1/2,k}}{\Delta t \Delta x_{2,j,1/2} \Delta x_{2,j,b}} \right) &+& \nonumber \\
\Phi_{j,k-1}^{n-1} \left( \frac{[C_M]_{j,k-1/2}}{\Delta t \Delta x_{3,k,1/2} \Delta x_{3,k,b}} \right)
\end{eqnarray}
The final coefficient ($R$) will also contain terms corresponding to boundary conditions.  Another small complexity to be added to this system of equations is that for grid points that border the boundary we may need to slightly alter the system (since we have $j-2$ and $k-2$ index references in these equations - see source code for details on how these are handled).  Notice also that, since there all metric factors are unity for Cartesian coordinates that we have reverted to standard notation for the field-line-integrated conductances.  Since this equation involves third-order derivatives more stringent boundary conditions are needed - GEMINI assumes that both the electric field and potential go to zero at the boundary (for non-perioidic domains).  For periodic solutions the system of equations above is recast into circulant form (see source code for details) before being passed off to MUMPS.

\section{Summary of empirical models used alongside GEMINI}

\subsection{MSIS}

MSIS is an empirical model of the neutral atmosphere \citep{Picone:2002} that is used in GEMINI to specify background atmospheric state.  Specifically MSIS provides densities of O,N$_2$,O$_2$, and H over the three dimensional GEMINI grid.  It also provide neutral temperature ($T_n$) based on input solar and geomagnetic indices found in the simulation input file.  There is also an option to use MSIS 2.0 at compile time; this is not a runtime option due to the restrictive license.

Depending on how the model is run, perturbations to the neutral atmosphere \emph{may} be applied over this background by specifying a set of neutral perturbation parameters in the input file for the simulations (see example config.dat files included with the source code in the ./initialize directory).  These may be specified in an \emph{ad hoc} fashion or perhaps taken from neutral dynamics modules.

\subsection{Impact ionization model} \label{sec:impact}

Impact ionization from precipitating suprathermal electrons is calculated using the semi-empirical method developed by \citet{Fang:2008} - see this reference for a listing of formulas implemented in GEMINI.  Impact ionization can also be computed using the GLOW model \citep{Solomon:2017}, which can be activated at runtime by specifying appropriate namelists in the config input file.  Using GLOW for impact ionization and thermal electron heating does incur a factor of $~2$ performance penalty - although this can be ameliorated by super-cycling (viz. not running every time step) the GLOW calculations.

\subsection{Thermal electron heating model}

Thermal electron heating rates (i.e. heating due to exchange from suprathermal electrons) are computed according to the parameterization in \citet{Swartz:1972} if the \citet{Fang:2008} semi-empirical model is used for impact ionization.  If GLOW is used for impact ionization it also provides thermal electron heating rates to the GEMINI fluid equations.

\subsection{Photoionization model}

Photoionization is calculated from the empirical model described in \citet{Solomon:2005}.  Vacuum ultraviolet and soft X-ray solar fluxes are taken from the EUVAC model \citep{Richards:1994}.  GLOW is not currently used for photoelectron calculations.

\section{Hooks for data sources and other models}

GEMINI can incorporate potential or FAC boundary condition data as input, as well as background electric fields and neutral perturbations.  The data are read in at a given cadence and then interpolated in space and time on the model grid and/or boundaries as appropriate.  See the project README for more information about how to set up the file input.

\section{Parallelization using message passing libraries}

GEMINI has been developed and tested using openmpi and gfortran, but also works with recent Intel fortran compiler versions.

The model can be run with a single processor, but for 3D simulations almost certainly too slow to be produce results in a useful amount of time; a quad-core system is really the minimum, reasonable hardware requirement.  In order to achieve parallelization with openmpi, a domain decomposition along the $x_2$ and $x_3$ dimensions is done, i.e. each worker process does calculations on a part of the entire grid which corresponds to a slab extending over all $x_1$ coordinates and a contiguous subset of the $x_{2,3}$ coordinates of the full grid.  Some variables in the program need to be stored by the root process for the entire grid (e.g. for input/output, full grid derivatives, electrodynamic solution, etc.), which does incur significant memory overhead for the root process.  Generally speaking the root process can require up to 1.5-4 times the memory of the other workers (depending on the grid size).

\section{Adaptation to Two Dimensions} \label{sec:2D}

A 2D simulation can be run by specifying a grid with an $x_2$ or $x_3$ grid size of 1 (i.e. only 1 point in this dimension).  In the case where the size of $x_3$ is taken to be 1, dimensions two and three in the model are \emph{internally} ``swapped'' so that message passing (which functions only for the $x_3$-direction) still works normally.  The arrays dimensions are swapped again prior to output so that the simulation grid retains the same shape as that specified by input.  A few additional complications arise when the $x_2$ and $x_3$ dimensions are swapped, as all cross products need to have their signs reversed due to the permuting of dimensions.

\subsection{Fluid system}

The fluid part of the model requires no special treatment to function in 2D, except that all derivative operations need to check the size of the $x_2$ dimension and set derivatives in that direction to zero if that dimension has size ``1'' (this is implemented as a low-level check whenever a derivative function is called).

\subsection{Cross products with permuted dimensions}

Because the $x_{2,3}$ dimensions get switched for certain types of 2D runs to accomodate message passing, some changes need to be made to terms involving cross products.  Letting $\mathbf{A} \times \mathbf{B}$ represent the cross product in the original coordinate system and $\mathbf{A}~\bar{\times}~\mathbf{B}$ be the cross product in the permuted coordinate system, it follows that:
\begin{equation}
\left[ \mathbf{A}~\bar{\times}~\mathbf{B} \right]_i = \bar{\epsilon}_{ijk} A_j B_k = - \bar{\epsilon}_{ikj} A_j B_k = - \epsilon_{ikj} A_k B_j = - \left[ \mathbf{A}~\times~\mathbf{B} \right]_i
\end{equation}
The second-to-last step follows from the fact that the Levi-Civita symbols are the same in each system and that the components of vectors are switched in the permuted system.  Hence, the signs of all cross product terms need to be reversed in the simulation when the $x_{2,3}$ dimensions are permuted (in addition to swapping all the $x_{2,3}$ components of vectors).

\subsection{Current in 2D}

To determine how to alter the signs of the current density components in 2D, it is noted that the current may be written, following \citet[][ Ch. 5]{Schunk:2009}, as:
\begin{equation}
\mathbf{J} = \sigma_P \left( \mathbf{E}_\perp + \mathbf{v}_{n,\perp} \times \mathbf{B} \right) - \sigma_H \left( \mathbf{E}_\perp \times \frac{\mathbf{B}}{B} + \left( \mathbf{v}_{n,\perp} \times \mathbf{B} \right) \times \frac{\mathbf{B}}{B} \right)
\end{equation}
Any cross product in this equation will need to have its sign switched.  This includes the Hall terms for the electric field and the Pedersen term for the neutral wind.  Note that the neutral wind Hall terms has two cross products and the sign switches from each of these will cancel out so that \emph{nothing} needs to be done with this term.

\subsection{Electrodynamics}

In addition to the lack of $x_2$ derivatives in the 2D model runs, the potential equation is also reformulation of potential equation using resolved field lines since 2D solves with fully-resolved field lines are not overly burdensome (unlike 3D solves).  This potential equation is:
\begin{eqnarray}
\frac{1}{h_1 h_2 h_3} \left[ \frac{\partial}{\partial x_1} \left( \frac{h_2 h_3}{h_1} \sigma_0 \frac{\partial \Phi}{\partial x_1} \right) + \frac{\partial}{\partial x_3} \left( \frac{h_1 h_2}{h_3} \sigma_P \frac{\partial \Phi}{\partial x_3} \right) \right]  &=& \nonumber \\ \nabla_\perp \cdot \left[ \boldsymbol{\sigma}_\perp \cdot \left( \mathbf{v}_n \times \mathbf{B} \right) \right] + \nabla_\perp \cdot \left( \boldsymbol{\sigma}_\perp \cdot \mathbf{E}_{0\perp} \right) \label{eqn:potnonEFL}
\end{eqnarray}
Note that the terms have been swapped so that the Pedersen term now falls under $x_3$ derivatives (consistent with other coordinate switching done in 2D runs).  Also the neutral wind source term contains a cross product; per discussion above this signs of the Pedersen terms must be switched.

Boundary conditions for Equation \ref{eqn:potnonEFL} can use either topside field-aligned current or potential.  The bottom (i.e. low-altitude) boundary current is always assumed to be zero and the sides of the 2D domain are assumed to be grounded.

\subsection{Magnetic fields in 2D}

In 2D a planar geometry is assumed and the magnetic field integrals from Equation \ref{eqn:magint3D} can be reduced to 2D integrals analytically.  This procedure yields the following integrals to execute to compute magnetic fields:
\begin{eqnarray}
\mathbf{B}(\mathbf{x}) = \frac{\mu_0}{4 \pi} \left\{  \hat{\mathbf{e}}_1 \iint \frac{-2 J_3 R_2}{(x_1-x_1')^2+(x_2-x_2')^2} d x_1' d x_2' \right. &+& \nonumber \\
\hat{\mathbf{e}}_2 \iint \frac{2 J_3 R_1}{(x_1-x_1')^2+(x_2-x_2')^2} d x_1' d x_2' &+& \nonumber \\
 \left. \hat{\mathbf{e}}_3 \iint \frac{2 \left( J_1 R_2 - J_2 R_1 \right) }{(x_1-x_1')^2+(x_2-x_2')^2} d x_1' d x_2' \right\} \label{eqn:magint2D}
\end{eqnarray}
The integrations for this calculations are done over the entire simulation domain (primed coordinates).

\section{Postface}

\subsection{Author's note}

The GEMINI model is the result of years of cycles of tinkering, development, testing, failure, and success.  Work on the model started in the summer of 2011, shortly after the birth of my twin children Karina and Max.  The name ``GEMINI'' is a direct reference to the time during this summer when I was immersed in the initial development and planning for this model.  At the same time, I was similarly immersed in bonding with my children, such that my life during that time had a clearly delineated duality to it - again, motivated the name GEMINI.  I spent my time either working on the model or with my family; both of these endeavors ended up being journeys of discovery.  I was a novice to model development at the time, having only used and made minor alterations to ionospheric codes before, and I was also a new parent with no experience.  My twin children, Karina and Max, and the dual nature of my activities in the summer that followed led me to give the name ``GEMINI'' to the model that I was then developing and that has defined my career in the years 2011-2018.  As a happy coincidence, I also have a late May birthday which falls under the astrological sign of - you guessed it - gemini.

As far as I can remember, the name GEMINI was initially invoked when working on the MICA sounding rocket data with my colleague Kristina Lynch who suggested that I give my model a name because she was tired of referring to our simulations as having been produced by ``Matt's model''.  As far as I can recall this name was first used in an official context at the 2013 Fall AGU meeting during an invited presentation that I gave in a session on ionospheric outflow, and for a several years the name existed without an acronym.  In the following years my PhD student Meghan Burleigh (who has since contributed to the development of GEMINI) and colleagues at other institutions used the name more consistently than I did.  I was compelled to finally define the acronym GEMINI as part of a grant application (acronyms with meaning are far more compelling and fundable) and settled on the Geospace Environment Model of Ion-Neutral Interactions.

After about seven years of developing and improving this model and using it for science, I have decided to open source and document the code.  The motivation for this is to keep myself organized by establishing a version-controlled repository for the code, to maintain transparency, and to encourage the possibility of a larger user-base of scientists that find the code useful and/or want to contribute to the project.

cheers,\\
-MZ

\subsection{Acknowledgments}

Developing a comprehensive model from scratch is a daunting and stressful (but also fun!) task; one at which I would not have succeeded without invaluable experience working with highly qualified modeling teams, specifically Pierre-Louis Blelly's team at CNRS in Toulouse, France.  I've also received an enormous amount of good career advice from my PhD advisor Joshua Semeter (Boston University) and from my colleague Kristina Lynch (Dartmouth College).  I have also my family to thank for being so very patient with me during the many weekend days and late nights that I've spent working on GEMINI.

The \emph{development} of GEMINI has been supported by the National Science Foundation (grants AGS-1000302 and AGS-1339537, and NSF CAREER award AGS-1255181).

Major contributors to GEMINI source code and testing include:  M. Hirsch, G. Grubbs, and M. Burleigh.

\pagebreak
\setcounter{page}{1}

\bibliography{GEMINI.bib}

\end{document}