\documentclass[
	superscriptaddress,
	twocolumn,
	aps, pre
]{revtex4-1}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage{graphicx}

\input{../common.tex}
\newcommand{\dx}{\Delta x}
\newcommand{\dr}{\Delta r}
\renewcommand{\L}{_\mathrm{L}}
\newcommand{\R}{_\mathrm{R}}
\newcommand{\dom}{\Omega}
\newcommand{\bndry}{\partial\Omega}

\begin{document}

\title{Differential operators and boundary conditions in `py-pde`}
\author{David Zwicker}
\date{\today}

\begin{abstract}
We here document the differential operators and the associated boundary conditions that are used in the finite difference approximation.
We also derive special versions of the Laplace operator that conserves the total mass.
\end{abstract}


\maketitle
\tableofcontents


\section{General considerations}
The main objects in `py-pde` are fields that are expressed on discretized grids.
The package provides many functions to manipulate these fields.
Some functions (e.g., addition, multiplication, integrals) do only use the actual field values, while others (e.g., differential operators and interpolation) might require information on the behavior of the field at the boundary.
This documents specifies details for how boundary conditions are handled.

Generally, boundary conditions are imposed using so-called ghost cells (or virtual points), which specify the value of the fields one support point beyond the boundary.
The idea is that the values of the ghost cells are set such that the boundary condition between the ghost cell and the first real cell is imposed exactly.
While this is quite straight-forward for scalar fields, fields of higher rank (i.e., vector and tensor fields) require more care.
To see problems that can arise, consider a vector field $v_\alpha$ with two components, so that the field has a normal and a tangential component at each boundary point.
How many components need to be specified of the ghost cell? The answer depends on the operator that will be applied to the field: Only the normal components need to be specified to calculate the divergence of the vector field, while all components are required to calculate the gradient tensor or to interpolate the vector field.
Generally, normal boundary conditions are sufficient when the applied operator reduces the rank of the field, e.g., turns a vector field into a scalar field.

\subsection{Scalar fields}
The simplest fields in `py-pde` are scalar fields, which associate a single value to each grid point.
In the continuum, a scalar field $c(\vect r)$ often has the following boundary conditions:
\begin{salign}
	\text{value:} &&	c &= A
\\
	\text{normal derivative:} && n_\alpha \partial_\alpha c &= B
\end{salign}
where $n_\alpha$ is the outward normal of the boundary.
These two conditions are also often known as Dirichlet and Neumann boundary conditions, respectively.
These options are implemented as \texttt{value} and \texttt{derivative} in `py-pde`.

\subsection{Vector fields}
For a vector field $v_\alpha(\vect r)$, which associate a vector with each grid point, possible boundary conditions are
\begin{salign}
	\text{value:} && v_\alpha  &= A_\alpha
\\
	\text{normal component:} && n_\alpha v_\alpha  &= A
\\
	\text{normal derivative:} && n_\beta \partial_\beta v_\alpha &= B_\alpha
\\
	\text{normal comp. of normal deriv.:} && n_\alpha n_\beta \partial_\beta v_\alpha &= B
\end{salign}
There are now many more options, where the first two specify values (Dirichlet conditions) and the last two specify derivatives (Neumann conditions).
Additionally, the conditions differ in that they either specify the full vector (conditions 1 and 3) or just the normal component (conditions 2 and 4).
The latter choice obviously contains less information, but is sufficient for differential operators that reduce the field rank (e.g., divergence).
Conversely, specifying the full vector using the first or third condition is necessary for interpolating vector fields correctly.
Note also that the Dirichlet condition imposes the dot product between the normal vector and the vector field, which in a simple 1d system implies $v_1(x=x\L)=-A$ and $v_1(x=x\R)=A$ for the lower and upper boundary, respectively.
Similarly, the Neumann condition evaluates the directional derivative of the vector field in the normal direction~$n_\beta$ of the boundary.


\section{Boundary conditions for differential operators}
We start with general considerations that are independent of the grid type.
Here, we consider differential operators that act on fields defined in a domain~$\dom$.
In particular, we discuss the boundary conditions that can be enforced with each type of operator.
We here use a component notation with implicit Einstein summation to denote contractions explicitly.
For instance, $\partial_\alpha v_\alpha$ denotes the divergence of a vector field $v_\alpha(\vect r)$, and $t_{\alpha\alpha}(\vect r)$ is the trace of a tensor field $t_{\alpha\beta}(\vect r)$.

\subsection{Laplace operator}
The ordinary Laplace operator $\partial_\alpha^2 c$ is defined to act on scalar fields~$c(\vect r)$.
Possible boundary conditions are
\begin{salign}[eqn:bc_laplace]
	\text{value:} &&	c &= A
\\
	\text{normal derivative:} && n_\alpha \partial_\alpha c &= B
\end{salign}
applied at positions $\vect r \in \bndry$.
Consequently, either the value~$A$ of the field (Dirichlet condition $c(\vect r) = A$ at $\vect r \in \bndry$) or the normal derivative (Neumann condition $n_\alpha \partial_\alpha c = B$) is specified, where $n_\alpha$ is the outwards oriented normal vector at the boundary.


\subsection{Gradient operator}
The gradient operator $\partial_\alpha c$ acts on a scalar field $c(\vect r)$ yielding a vector field.
Possible boundary conditions are
\begin{salign}[eqn:bc_gradient]
	\text{value:} &&	c &= A
\\
	\text{derivative:} && n_\alpha \partial_\alpha c &= B
\end{salign}
where $n_\alpha$ is again the outward normal of the boundary.

\subsection{Divergence operator}
The divergence operator $\partial_\alpha v_\alpha$ acts on a vector field $v_\alpha(\vect r)$ with possible boundary conditions
\begin{salign}[eqn:bc_divergence]
	\text{normal component:} && n_\alpha v_\alpha  &= A
\\
	\text{derivative:} && n_\alpha n_\beta \partial_\beta v_\alpha &= B
\end{salign}
Note that the Dirichlet condition in this case imposes the dot product between the normal vector and the vector field, which in a simple 1d system implies $v_1(x=x\L)=-A$ and $v_1(x=x\R)=A$ for the lower and upper boundary, respectively.
Similarly, the Neumann condition evaluates the directional derivative of the vector field in the normal direction~$n_\beta$ of the boundary.
This implies that the tangential components of the vector field do not affect the boundary condition.


\subsection{Vector Laplacian}
The Vector Laplacian is a simple generalization of the ordinary Laplace operator to a vector field $v_\alpha(\vect r)$.
Possible boundary conditions are
\begin{salign}[eqn:bc_vector_laplace]
	\text{value:} &&	v_\alpha &= A_\alpha
\\
	\text{normal derivative:} && n_\beta \partial_\beta v_\alpha &= B_\alpha
\end{salign}
which are applied at positions $\vect r \in \bndry$.


\subsection{Vector gradient operator}
The vector gradient  operator $\partial_\beta v_\alpha$ acts on a vector field $v_\alpha(\vect r)$ and yields a tensorial field.
This operator can be though of as a gradient applied to each component of $v_\alpha$ and the boundary conditions thus are also applied to each component,
\begin{salign}
	\text{value:} &&	v_\alpha &= A_\alpha
\\
	\text{derivative:} && n_\beta \partial_\beta v_\alpha &= B_\alpha
\end{salign}

\subsection{Tensor divergence operator}
The vector gradient  operator $\partial_\beta t_{\alpha\beta}$ acts on a tensor field $t_{\alpha\beta}(\vect r)$ and yields a vector field.
%This operator can be though of as a divergence applied along the $\beta$-direction to each $\alpha$-component.
The boundary conditions thus are applied to each component,
\begin{subequations}
\begin{align}
	\text{value:} &&	t_{\alpha\beta} n_\beta  &= A_\alpha
\\
	\text{derivative:} &&\partial_\beta t_{\alpha\beta}&= B_\alpha
\end{align}
\end{subequations}

\subsection{Operators not implemented}
The following operators are not yet implemented, but might be useful in the future:
\begin{subequations}
\begin{align}
	\text{Curl:} &&
		\epsilon_{\alpha\beta\gamma} \partial_\beta v_\gamma
\\
	\text{Material derivative (Advection):}&&
		w_\alpha \partial_\alpha v_\beta
\end{align} 
\end{subequations}


%\subsection{Boundary conditions of compound operators}
%The Laplace operator can be written as a gradient operator followed by a divergence operator, $\partial_\alpha^2 c = \partial_\alpha(\partial_\alpha c)$.
%When implementing the involved operators in finite difference, one necessarily needs to determine boundary conditions for all three differential operators.
%Clearly, boundary conditions given in \Eqref{eqn:bc_laplace} for the Laplace operator can also be applied to the gradient operator, which employs the same conditions; see \Eqref{eqn:bc_gradient}.
%Conversely, the boundary conditions on the divergence operator specify 



\section{Differential operators on Cartesian grids}
We consider a linear domain of length $L$ discretized by $N$ support points.
We place these points equidistantly at $x_n = (n+\frac12)\dx$ for $n=0,1,\ldots, N-1$ using the discretization $\dx=L/N$.
Any function $y=f(x)$ is then represented by discretized values $y_n = f(x_n)$.

In the finite difference schemes we discuss, we require differential operators of first and second order.
Targeting second order accuracy, we obtain these as 
\begin{subequations}
\begin{align}
	f'(x_n) &\sim \frac{y_{n+1} - y_{n-1}}{2\dx}
\\	
	f''(x_n) &\sim \frac{y_{n+1} - 2 y_n + y_{n-1}}{\dx^2}
\end{align}
\end{subequations}
where $\sim$ denotes the finite difference approximation.

These derivatives can only be evaluated directly for $n=1,2,\ldots, n-2$, while the boundary points $n=0$ and $n=N-1$ require knowledge of the value at the respective virtual points $x_{-1}$ and $x_N$ outside the domain.
We can derive expressions for the associated function values $y_{-1}$ and $y_N$  taking the boundary conditions in to account, which typically specify the value at the boundary or the (outward) derivative.
A particular simple case are periodic boundary conditions, where we have $y_{-1} = y_{N-1}$ and $y_N = y_0$, allowing the above formula to be used everywhere.
For more complicated boundary conditions, we need to treat both sides separately.

\subsection{Lower boundary}
Let us first consider conditions at the lower boundary, which we place at $x=0$.
We thus need to evaluate the virtual support point $y_{-1}$ from the boundary condition.
Using estimates for the value and the derivative at the boundary,
\begin{align}
	f(0) &\sim \frac{y_0 + y_{-1}}{2}
&
	f'(0) &\sim \frac{y_0 - y_{-1}}{\dx}
\end{align}
we can now handle several boundary conditions.

\paragraph{Dirichlet (Constant value):}
Assuming the boundary condition $f(0) = a$, we obtain $y_{-1} = 2a - y_0$.

\paragraph{Neumann (Constant derivative):}
Here, we consider the boundary condition $-f'(0) = b$, which specifies the outward derivative to equal the value $b$.
We obtain $y_{-1} = y_0 + b \dx$.

\paragraph{Robin (Mixed):}
Assuming the boundary condition $-f'(0) + c f(0) = b $, we obtain $y_{-1} = A - B y_0$
where 
\begin{align}
	A &= \frac{2\dx}{c \dx + 2} b
&
	B &= \frac{c\dx - 2}{c \dx + 2}
\end{align}
%Hence,
%\begin{align}
%	\takenat{\pfrac{^2 f}{x^2}}{x_0} \sim \frac{A - (2 + B) y_0 + y_{1}}{\dx^2}
%\end{align}
As a sanity check, we obtain Dirichlet conditions in the limit $b \rightarrow \infty$ with $b/c = a$, where $A=2a, B=1$.
Likewise, we recover Neumann condition for $c=0$.

\paragraph{Summary:}
Taken together, we can express the value of the support point as
\begin{align}
	y_{-1} &= \alpha\L + \beta\L y_{k\L}
	\;,
\end{align}
where the coefficients $\alpha\L$, $\beta\L$, and $k\L$ are taken from \tabref{tab:cartesian_lower}.


\begin{table}[t]
\caption{\label{tab:cartesian_lower}%
Coefficients for lower cartesian boundary
}
\begin{ruledtabular}
	\begin{tabular}{cccc}
		Boundary condition & Offset $\alpha\L$ & Pre-factor $\beta\L$  & Index $k\L$\\
		\colrule
		Periodic & $0$ & $1$ & $N-1$ \\
		Dirichlet & $2a$ & $-1$ & $0$ \\
		Neumann & $b\dx$ & $1$  & $0$ \\
		Robin & $\frac{2b\dx}{2 + c \dx}$ &
				$\frac{2 - c\dx}{2 + c \dx}$ & $0$ \\
	\end{tabular}
\end{ruledtabular}
\end{table}


\subsection{Upper boundary}
The upper boundary at $x=L$ can be treated analogously to the lower one by introducing a virtual point at $x=x_N$.
Consequently, we find the following conditions for the virtual support point $y_N$:
\paragraph{Dirichlet (Constant value):}
Assuming the boundary condition $f(L) = a$, we obtain $y_N = 2a - y_{N-1}$.

\paragraph{Neumann (Constant derivative):}
Here, we consider the boundary condition $f'(L) = b$, which specifies the outward derivative to equal the value $b$.
We obtain $y_N = y_{N-1} + b \dx$.


Taken together, we express the value at the virtual support point as
\begin{align}
	y_N &= \alpha\R + \beta\R y_{k\R}
	\;,
\end{align}
where the coefficients $\alpha\R$, $\beta\R$, and $k\R$ are taken from \tabref{tab:cartesian_upper}.


\begin{table}[t]
\caption{\label{tab:cartesian_upper}%
Coefficients for upper cartesian boundary
}
\begin{ruledtabular}
	\begin{tabular}{cccc}
		Boundary condition & Offset $\alpha\R$ & Pre-factor $\beta\R$  & Index $k\R$\\
		\colrule
		Periodic & $0$ & $1$ & $0$ \\
		Dirichlet & $2a$ & $-1$ & $N-1$ \\
		Neumann & $b\dx$ & $1$  & $N-1$ \\
		Robin & $\frac{2b\dx}{2 + c \dx}$ &
				$\frac{2 - c\dx}{2 + c \dx}$ & $N-1$ \\
	\end{tabular}
\end{ruledtabular}
\end{table}


\section{Differential operators on cylindrical grids}
We here consider fields in $3$ dimensions that possess angular symmetry and thus only depend on the radial coordinate~$r$ and the axial coordinate~$z$.
Clearly, the axial coordinate behaves exactly as the Cartesian ones described above, so we here only have to deal with the polar symmetry.
Similar to Cartesian grids, we discretize the associated radial coordinate as $r_n = r_0 + (n + \frac12) \dr$, where $r_0$ is the inner radius, which often will be zero.

The radial part of the gradient operator in cylindrical coordinates is simply given by $\partial_r f(r)$ and thus obeys the same discretization as in a Cartesian grid.
The divergence of a vector field $v_\alpha(r) = \rho(r) \vect e_r$ reads
$\partial_\alpha v_\alpha(r) =  \rho'(r) + \frac{\rho(r)}{r}$, implying the discretized version
\begin{align}
	\partial_\alpha v_\alpha \sim
		\frac{y_{n+1} - y_{n-1}}{2\dr}
		+ \frac{y_n}{r_n}
\end{align}
Moreover, the Laplace operator in spherical coordinates reads $\nabla^2 f(r) = f''(r) + \frac{f'(r)}{r}$, which in the discretized version becomes
\begin{align}
	\partial_\alpha^2 f \sim
		\frac{y_{n+1} - 2 y_n + y_{n-1}}{\dr^2}
		+ \frac{y_{n+1} - y_{n-1}}{2 r_n \dr}
\end{align}

\subsection{Boundary condition at the origin}
Due to the symmetry, only vanishing derivatives are allowed at the inner boundary at the origin, implying the virtual support point $y_{-1} = y_0$.
Hence, the differential operators become
\begin{subequations}
\begin{align}
	\takenat{\partial_\alpha v_\alpha(r)}{r=0} &\sim
		\frac{y_1 - y_0}{2\dr}
		+ \frac{2y_0}{\dr}
\\
	\takenat{\partial_\alpha^2 f(r)}{r=0} &\sim
		2\frac{y_1 - y_0}{\dr^2}
%		\frac{y_1 - 2 y_0 + y_0}{\dr^2}
%		+ \frac{2(y_1 - y_0)}{\dr^2}
%		\frac{3y_1 -  3y_0}{\dr^2}
\end{align}
\end{subequations}


\subsection{Boundary condition at the outer side}
At the outer boundary, we can impose boundary conditions similar to the Cartesian grid.

\paragraph{Dirichlet (Constant value):}
Assuming the boundary condition $f(R) = a$, we obtain $y_N = 2a - y_{N-1}$.

\paragraph{Neumann (Constant derivative):}
Here, we consider the boundary condition $f'(L) = b$, which specifies the outward derivative to equal the value $b$.
We obtain $y_n = y_{n-1} + b \dx$.



\section{Differential operators on spherical grid}
We here consider fields that a spherically symmetric in $3$ dimensions and thus only depend on the radial coordinate~$r$.
Similar to Cartesian grids, we discretize the radial coordinate as $r_n = (n + \frac12) \dr$.

The radial part fo the gradient operator in spherical coordinates is simply given by $\partial_r f(r)$ and thus obeys the same discretization as in a Cartesian grid.
The divergence of a vector field $v_\alpha(r) = \rho(r) \vect e_r$ reads
$\partial_\alpha v_\alpha(r) =  \rho'(r) + \frac{2\rho(r)}{r}$, implying the discretized version
\begin{align}
	\partial_\alpha v_\alpha \sim
		\frac{y_{n+1} - y_{n-1}}{2\dr}
		+ \frac{2y_n}{r_n}
\end{align}
Moreover, the naive implementation of Laplace operator in spherical coordinates reads $\nabla^2 f(r) = f''(r) + \frac{2f'(r)}{r}$, which in the discretized version becomes
\begin{align}
	\partial_\alpha^2 f \sim
		\frac{y_{n+1} - 2 y_n + y_{n-1}}{\dr^2}
		+ \frac{y_{n+1} - y_{n-1}}{r_n \dr}
\end{align}
Note that this form of the Laplace operator is not conservative, \ie, the discretized version of the integral $\int \partial_\alpha^2 f \diff r$  does not vanish.


\subsection{Boundary condition at the origin}
Due to the symmetry, only vanishing derivatives are allowed at the inner boundary at the origin, implying the virtual support point $y_{-1} = y_0$.
Hence, the differential operators become
\begin{subequations}
\begin{align}
	\takenat{\partial_\alpha v_\alpha(r)}{r=0} &\sim
		\frac{y_1 - y_0}{2\dr}
		+ \frac{4y_0}{\dr}
\\
	\takenat{\partial_\alpha^2 f(r)}{r=0} &\sim
%		\frac{y_1 - 2 y_0 + y_0}{\dr^2}
%		+ \frac{2(y_1 - y_0)}{\dr^2}
		3\frac{y_1 -  y_0}{\dr^2}
\end{align}
\end{subequations}
Note that the latter expression applies to both the non-conservative and the conservative form of the Laplacian.

\subsection{Boundary condition at the outer side}
At the outer boundary, we can impose boundary conditions similar to the Cartesian grid.

\paragraph{Dirichlet (Constant value):}
Assuming the boundary condition $f(R) = a$, we obtain $y_N = 2a - y_{N-1}$.

\paragraph{Neumann (Constant derivative):}
Here, we consider the boundary condition $f'(L) = b$, which specifies the outward derivative to equal the value $b$.
We obtain $y_n = y_{n-1} + b \dx$.

\subsection{Conservative Laplace operator}

We call a discrete Laplace operator conservative when it preserves the desirable conservation equation
\begin{align}
	\int_\Omega \partial_\alpha^2 f \diff V
		= \oint_{\partial\Omega} \partial_\alpha f n_\alpha \diff S
	\;.
\end{align}
A conservative discrete operator can be derived by integrating the definition of the Laplace operator in spherical coordinates over one spherical shell from $r=r_n - \dr/2$ to $r=r_n  + \dr/2$,
\begin{align}
%	r^2 \partial_\alpha^2 f &= \partial_r\bigl(r^2 \partial_r f(r)\bigr)
%\\
	\int_{r_{n - \frac12}}^{r_{n + \frac12}} r^2 \partial_\alpha^2 f \diff r
		&= \int_{r_{n - \frac12}}^{r_{n + \frac12}} \partial_r\bigl(r^2 \partial_r f(r)\bigr) \diff r
	\;,
\end{align}
where we skipped the factor $4\pi$ stemming from the angle integration.
Assuming that the quantity $\partial_\alpha^2 f$ is constant across the discretization cell, we thus find
\begin{align}
	V_n \partial_\alpha^2 f %\left[ \frac{r^3}{3} \right]_{r_n - \dr/2}^{r_n + \dr/2}
		&\sim \Bigl[r^2 \partial_r f(r)\Bigr]_{r_n - \dr/2}^{r_n + \dr/2}
	\label{eqn:spherical_laplace_conservative_relation}
\end{align}
where we introduced the (scaled) shell volumes
\begin{align}
%	V_n &= \frac{\dr^3}{3}\bigl[(n + 1)^3 - n^3\bigr]
	V_n &= \frac{r_{n + \frac12}^3 - r_{n - \frac12}^3}{3}
	= \dr \left(	r_n^2 + \frac{\dr^2}{12} \right)
	\label{eqn:spherical_shell_volume}
\;,
\end{align}
which also skip the factor $4\pi$.
\Eqref{eqn:spherical_laplace_conservative_relation} provides an expression for the operator, which relies on the values on the adjacent boundaries.
Since integrating the operator over the entire domain corresponds to summing all contributions from individual shells, this formulation ensures that adjacent shells have opposite contributions, so that finally only the outer boundaries contribute, ensuring the conservative property.
Taken together we find
\begin{align}
	\partial_\alpha^2 f
%		&\sim \dr^2 \frac{(n + 1)^2 f'\left(r_n + \frac{\dr}{2}\right) - n^2 f'\left(r_n - \frac{\dr}{2}\right)}{V_n}
		&\sim \frac{r_{n+\frac12}^2 f'\left(r_{n+\frac12}\right) - r_{n-\frac12}^2 f'\left(r_{n-\frac12}\right)}{V_n}
\end{align}
where the derivatives $f'(r)$ are evaluated at the midpoints and can thus be represented as
\begin{subequations}
\begin{align}
	f'\left(r_{n + \frac12}\right) \sim \frac{y_{n+1} - y_n}{\dr}
\\
	f'\left(r_{n - \frac12}\right) \sim \frac{y_n - y_{n-1}}{\dr}
	\;.
\end{align}
\end{subequations}
In the special case where the spherical grid does not have a hole ($r_0=0$), we then arrive at
\begin{align}
	\partial_\alpha^2 f
		&\sim \frac{3}{\dr^2} \, \frac{(n + 1)^2 (y_{n+1} - y_n) - n^2 (y_n - y_{n-1})}{(n + 1)^3 - n^3}
	\;,
\end{align}
which is a conservative discretization of the spherical Laplacian by construction.

Similarly, we can consider a spherical grid where fields depend on the polar angle~$\theta$ while still keeping azimuthal symmetry, i.e., assuming no dependence on $\phi$.
The condition for a conservative Laplace operator then reads
\begin{widetext}
\begin{multline}
	\iint_{\text{cell}(n, m)} r^2 \sin\theta \, \partial_\alpha^2 f  \diff r \diff \theta
=
	\iint_{\text{cell}(n, m)} \biggl(
		\sin\theta  \partial_r\bigl[r^2 \partial_r f(r, \theta)\bigr]
		+ \partial_\theta \bigl[ \sin\theta \partial_\theta  f(r, \theta)\bigr]
	\biggr)\diff r \diff \theta
\\=
	\Bigl(\cos\theta_{m-\frac12} - \cos\theta_{m + \frac12}\Bigr)
		\int_{r_{n - \frac12}}^{r_{n + \frac12}}\left(
			 \partial_r\bigl[r^2 \partial_r f(r, \theta)\bigr]
		\right)  \diff r
%\notag\\&\quad
	+ \Delta r
		\int_{\theta_{m - \frac12}}^{\theta_{m + \frac12}}
			 \partial_\theta \bigl[ \sin\theta \partial_\theta  f(r, \theta)\bigr]	 \diff\theta
\\=
	\Bigl(\cos\theta_{m-\frac12} - \cos\theta_{m + \frac12}\Bigr)
			\bigl[r^2 \partial_r f(r, \theta)\bigr]_{r_{n - \frac12}}^{r_{n + \frac12}}
	+  \Delta r
		\bigl[ \sin\theta \partial_\theta  f(r, \theta)\bigr]_{\theta_{m - \frac12}}^{\theta_{m + \frac12}}
%\\
%	\partial_\alpha^2 f \left[ \frac{r^3}{3} \right]_{r_n - \dr/2}^{r_n + \dr/2}
%		&\sim \Bigl[r^2 \partial_r f(r)\Bigr]_{r_n - \dr/2}^{r_n + \dr/2}
\end{multline}
\end{widetext}
Here we assumed that we can neglect angular dependencies in the radial integral and vice versa.
Using the (scaled) volume of a grid cell,
\begin{align}
	V_{n,m} =
		\left(\cos\theta_{m-\frac12} - \cos\theta_{m + \frac12}\right)
		\frac{r_{n + \frac12}^3 - r_{n - \frac12}^3}{3}
%\notag\\=
%	\left(\cos\theta_{m-\frac12} - \cos\theta_{m + \frac12}\right)
	\;,
\end{align}
we can write this as
\begin{multline}
	V_{n,m} \partial_\alpha^2 f
=\Bigl(\cos\theta_{m-\frac12} - \cos\theta_{m + \frac12}\Bigr)
			\bigl[r^2 \partial_r f(r, \theta)\bigr]_{r_{n - \frac12}}^{r_{n + \frac12}}
\\
	+  \Delta r
		\bigl[ \sin\theta \partial_\theta  f(r, \theta)\bigr]_{\theta_{m - \frac12}}^{\theta_{m + \frac12}}
\end{multline}


\subsection{Conservative divergence operator}
The divergence of a spherically-symmetric vector field reads
\begin{align}
	\partial_\alpha v_\alpha = v_r'(r)+\frac{2 v_r(r)}{r}
\end{align}
where $v_r$ is the radial part of the vector field and primes denote derivatives with respect to $r$.
For a conservative implementation of the operator, we again integrate over a single shell,
\begin{align}
	\int_{r_{n-\frac12}}^{r_{n + \frac12}} (\partial_\alpha v_{\alpha}) r^2 \diff r 
	 &= \left[r^2 v_r(r)\right]_{r_{n-\frac12}}^{r_{n + \frac12}}
\end{align}
Consequently, we find
\begin{align}
	\partial_\alpha v_{\alpha} \sim
	 \frac{r_{n + \frac12}^2 v_r(r_{n + \frac12}) - r_{n - \frac12}^2 v_r(r_{n - \frac12})}{V_n}
\end{align}
where $V_n$ is given by \Eqref{eqn:spherical_shell_volume}. 
Here, we approximate the function values at the midpoints using a simple average,
\begin{align}
	v_r(r_{n + \frac12}) &\approx \frac{v_r(r_{n}) + v_r(r_{n+1})}{2}
	\;,
\end{align}
which might introduced uncontrolled artifacts.


\subsection{Conservative tensor divergence operator}
The radial component of the divergence of a spherically-symmetric tensor field (with $t_{\theta\theta} = t_{\phi\phi}$) reads
\begin{align}
	\partial_\beta t_{r\beta} =
		\frac{2 \left[t_{rr}(r)-t_{\phi \phi }(r)\right]}{r}+t_{rr}'(r)
	\;,
\end{align}
where primes denote derivatives with respect to $r$.
Note that the angular components of the resulting vector field vanish.
The integral over the shell reads
\begin{multline}
	\int_{r_{n-\frac12}}^{r_{n + \frac12}} (\partial_\beta t_{r\beta})  r^2 \diff r 
\\	
	 = \left[r^2 t_{rr}(r)\right]_{r_{n-\frac12}}^{r_{n + \frac12}}
	  - 2 \int_{r_{n-\frac12}}^{r_{n + \frac12}} t_{\phi \phi }(r) \, r \, \diff r 
%\\
%	 \approx \left[r^2 t_{rr}(r)\right]_{r_{n-\frac12}}^{r_{n + \frac12}}
%	  - 2 t_{\phi \phi }(r) \int_{r_{n-\frac12}}^{r_{n + \frac12}}  r \, \diff r 
\\
	 \approx \left[r^2 t_{rr}(r)\right]_{r_{n-\frac12}}^{r_{n + \frac12}}
	  - t_{\phi \phi }(r) \left(
	  	r_{n+\frac12}^2 - r_{n-\frac12}^2
	  \right)
\end{multline}
where we assumed that $t_{\phi\phi}(r)$ is constant over the integration volume in the last approximation.
We thus find
\begin{align}
	\partial_\alpha v_{\alpha} &\sim
	 \frac{r_{n + \frac12}^2 t_{rr}(r_{n + \frac12}) - r_{n - \frac12}^2 t_{rr}(r_{n - \frac12})}{V_n}
\\\notag & \quad - t_{\phi \phi }(r) \frac{r_{n+\frac12}^2 - r_{n-\frac12}^2}{V_n}
\end{align}
where $V_n$ is given by \Eqref{eqn:spherical_shell_volume}. 
We again approximate the function values at the midpoints using a simple average,
\begin{align}
	t_{rr}(r_{n + \frac12}) &\approx \frac{t_{rr}(r_{n}) + t_{rr}(r_{n+1})}{2}
	\;,
\end{align}
which might introduced uncontrolled artifacts.


\subsection{Conservative double divergence operator}
We next consider the double divergence of a rank-$2$ tensor field on spherical coordinates.
In general, we have
\begin{align}
	\partial_\alpha\partial_\beta t_{\alpha\beta} = D_r(t_{\alpha\beta}) - D_\phi(t_{\alpha\beta})
\end{align}
where we defined the two differential operators 
\begin{subequations}
\begin{align}
	D_r(t_{\alpha\beta}) &= 
	 \frac{2 t_{rr}}{r^2} + \frac{4 t_{rr}'}{r} + t_{rr}''
\\
	D_\phi(t_{\alpha\beta}) &= \frac{2 t_{\phi \phi}}{r^2} + \frac{2t_{\phi \phi }'}{r}
\end{align}
\end{subequations}
and used $t_{\phi\phi}=t_{\theta\theta}$, which holds in spherically symmetric systems.
Here, primes denote derivatives with respect to the radial coordinate~$r$.
We next analyze these operators individually.


A conservative operator can be derived by integrating its definition in spherical coordinates over one spherical shell from $r=r_n - \dr/2$ to $r=r_n  + \dr/2$,
\begin{align}
%	r^2 \partial_\alpha^2 f &= \partial_r\bigl(r^2 \partial_r f(r)\bigr)
%\\
	\int_{r_{n - \frac12}}^{r_{n + \frac12}} D_\phi(t_{\alpha\beta}) r^2  \diff r
	&= 2\int_{r_{n - \frac12}}^{r_{n + \frac12}} \left[
			t_{\phi \phi} + rt_{\phi \phi }'
		\right]\diff r
%\notag\\&=
%	2	\left[r t_{\phi \phi}(r)\right]_{r_{n - \frac12}}^{r_{n + \frac12}}
	\;,
\end{align}
where we skipped the factor $4\pi$ stemming from the angle integration.
Assuming that the quantity $D_\phi(t_{\alpha\beta})$ is constant across the discretization cell, we thus find
\begin{align}
	V_n D_\phi(t_{\alpha\beta}) 
		&\sim \Bigl[2r t_{\phi \phi}(r)\Bigr]_{r_n - \dr/2}^{r_n + \dr/2}
\end{align}
where $V_n$ is given by \Eqref{eqn:spherical_shell_volume}. 
Consequently,
\begin{align}
	D_\phi(t_{\alpha\beta}) 
%		&\sim \dr^2 \frac{(n + 1)^2 f'\left(r_n + \frac{\dr}{2}\right) - n^2 f'\left(r_n - \frac{\dr}{2}\right)}{V_n}
		&\sim \frac{2}{V_n}\left[
			r_{n+\frac12} t_{\phi\phi}(r_{n+\frac12}) - r_{n-\frac12}  t_{\phi\phi}(r_{n-\frac12})
		\right]
\end{align}
where the derivatives $t_{\phi\phi}(r)$ are evaluated at the midpoints by the weighted average,
\begin{align}
	t_{\phi\phi}(r_{n+\frac12}) &= \frac{t_{\phi\phi}(r_{n}) + t_{\phi\phi}(r_{n+1})}{2}
	\;.
\end{align}
Taken together, we have a conservative definition of the angular part of the double divergence.

The radial part follows analogously using
\begin{align}
	\int_{r_{n - \frac12}}^{r_{n + \frac12}} D_r(t_{\alpha\beta}) r^2  \diff r
	&= 2\int_{r_{n - \frac12}}^{r_{n + \frac12}} \left[
			 2 t_{rr} + 4 rt_{rr}' + r^2t_{rr}''
		\right]\diff r	
\end{align}
resulting in
\begin{align}
	V_n D_r(t_{\alpha\beta}) \sim
	\Bigl[2 r t_{rr}(r) + r^2 t_{rr}'(r)\Bigr]_{r_n - \dr/2}^{r_n + \dr/2}
\end{align}
where
\begin{align}
	t_{rr}'\bigl(r_{n + \frac12}\bigr) = \frac{t_{rr}(r_{n+1}) - t_{rr}(r_n)}{\dr}
\end{align}
so that $D_r(t_{\alpha\beta})$ can be expressed easily.
Taken together, the double divergence operator reads
\begin{widetext}
\begin{align}
	\partial_\alpha\partial_\beta t_{\alpha\beta} \sim \frac{
	2 r_{n+\frac12} \left[t_{rr}(r_{n+\frac12}) - t_{\phi\phi}(r_{n+\frac12}) \right]
	+ r_{n+\frac12}^2 t_{rr}'(r_{n+\frac12})
	-2 r_{n-\frac12} \left[t_{rr}(r_{n-\frac12})  -  t_{\phi\phi}(r_{n-\frac12})\right]
	- r_{n-\frac12}^2 t_{rr}'(r_{n-\frac12})
	}{V_n}
\end{align}
\end{widetext}


%\bibliographystyle{apsrev4-1}
%\bibliography{bibdesk.bib}

\end{document}  