\chapter{O-C Diagrams and Simulations}
\label{ch:oc_sim}

In this chapter we briefly describe O-C diagrams as a method that we use for
detection of $3^{rd}$ bodies (circumbinary planets or brawn dwarfs in our case) in eclipsing binary systems.
Also we present our simulation of $3^{rd}$ bodies in eclipsing binary systems.

\section{O-C diagrams}
Observed minus Calculated diagrams is a diagnostic tool and interpretation
of the disagreement between the measure of an observable event and its predicted value \citep{Sterken2005basic}.
In astronomy, O-C diagram usually is used when discussing cyclic phenomena where the times of occurrence of a given event is irregular.
The O-C diagram is then constructed by plotting the quantity O-C as a function of time, the correct interpretation
of these deviations leads to a better model (and a new O-C diagram).

In variable-star studies, O-C is sometimes expressed as deviations of phase
in the cycle of variability, whereas the time axis is the cycle number (commonly indicated by $E$).
In such studies, the O-C diagram mostly refers to rather simple C formalisms, viz. linear or quadratic ephemeris formulae, sometimes
combined with a trigonometric periodic term \citep{Sterken2005basic}.

During the building O-C diagram we are dealing with processes that repeat themselves in a more or less regular way.
Any attempt to construct a reliable O-C diagram will fail if a wrong value of period $P$ is used.
But it is not always easy to derive a period from the observational data:
$P$ is not directly observable, it follows from the
determination of at least two moments of time of the same reference phase (epochs).
The observation of two such epochs $T_{1}$ and $T_{2}$ immediately provide us an upper limit for $P$.
When more than two such times are available, the period can be derived by a least-squares solution of a set of
equations
\begin{equation} \label{eq:period}
T_{i} - T_{j} = nP
\end{equation}
where $n$ is an integer, commonly called the cycle number $E$(epoch).

Phase is a position on the cycle of variation, a convenient periodic measure of
elapsed time: $\varphi (t)$ is the fraction of $P$ that elapsed since the occurrence of the
reference time $T_{0}$ and is given by

\begin{equation} \label{eq:phase}
\varphi = \frac{T-T_{0}}{P} ~\bmod~ 1
\end{equation}

The most common approach to the O-C procedure is the one in which one reference
phase is selected, and where the timings of this reference phase are studied
and interpreted.

If $P$ is constant and if its value is known, equation (\ref{eq:period}) leads to

\begin{equation} \label{eq:Tmin2}
T_{min} = T_{0} + P E
\end{equation}

where $T_{min}$ is the time of minimum light, $T_{0}$
is the zero epoch and $E$ is the number of cycles elapsed since the zero epoch. $T_{0}$ and
$P$ are obtained through a least-squares solution. The longer the time interval (in
cycles) over which the data have been collected, the higher will be the accuracy
of the solution for $P$: the uncertainty in $P$ is inversionally proportional to the
number of cycles, and proportional to the r.m.s. scatter of the data.

It is hard to conclude from experimental data that a period change has occurred.
In principle, changes of period could be described by any mathematical formula expressing $P$ as a function of time.
Thus, the time of epoch $T_{m}$ is

\begin{equation} \label{eq:P_var}
T_{m} = T_{0} + \int P(t)dt   ~~~~~\mathrm{or}~~~~~   T_{m} = T_{0} + \int P(E)dE
\end{equation}

In most cases, relation (\ref{eq:P_var}) is restricted to linear variations, cyclic variations, or
a combination of both of them.

Many causes can lead to period variations.
Transfer of matter (between stars in a multiple system) or mass ejection (from a system)
can provoke period changes. And what is most important for our research - periodic variations of O-C can indicate presents of other body in binary system.

After construction of O-C diagram there can be a visible trend present on the residual diagram.
Such a trend, in particular when it repeats itself,
can indicate presence of other body in binary system.
In such case, O-C can be fitted with additional term corresponding to 3rd body orbit parameters:

\begin{equation} \label{eq:P_lin_OC_sin}
T_{m} = T_{0} + P_{0}E + \frac{1}{2} \frac{dP}{dt} \bar{P}E^{2} +
\dfrac{a_{12}\sin i}{c}   \left[  \dfrac{1-e^2}{1+e \cos\nu}   \sin(\nu + \omega)  + e \sin \omega  \right]
\end{equation}

where $a_{12} \sin i$ is the projected semi-major axis, $e$ is the eccentricity,
$\omega$ is the longitude of the periastron, $\nu$ is the true anomaly of the EB orbit around the common centre of the mass of
the whole system and $c$ is the velocity of light \citep{irwin1952}.

\section{Simulations of $3^{rd}$ Body in EB Systems}

In this section, some factors that have an influence on the minima shape, and therefore on O-C diagram too, will be described.
Such factors are:
\begin{itemize}[noitemsep,nolistsep]
\item EB system visibility from observational point;
\item weather conditions;
\item precision of photometry;
\item presents of spots on EB component, or on both components;
\item pulsation
\end{itemize}

The O-C diagram of EB system with $3^{rd}$ body would be presented as a ideal conditions case, when we do not have any of mentioned above distorting factors, and then compared with conditions close to real one, where O-C diagram is distorted.

First three factors are not connected with star types, that's why we will consider them separately.
The influence of other factors on O-C diagrams will be considered in the examples of detached and over-contact systems, because of similarity of minima shape in detached and semi-detached EB systems.

Our simulations are based on a light curves generated by PHOEBE software \citep{Prsa2005} based on well known Wilson \& Devinney code described in \cite{Wilson1971}. These LC will be extended for needed number of cycles, usually 5 or 10 year. Time shifts in LC caused by presence of $3^{rd}$ body added based on computation made in work \cite{irwin1952}. On the final stage other distortion factors mentioned above are also added on LC.

\subsection{EB system visibility}

It is good known that periods of star visibility, for some observational point, depend on star declination.
If we proceed from the assumption that observations are collected from one observatory then this feature greatly complicates the task for the observer. In such case there will be only some seasons of observations and gaps in data for a lot of targets. Only near polar regions of sky can be observable throughout the year for middle latitudes observatories.

For example there is two different O-C diagrams presented on Fig. \ref{fig:oc_1}. Left one is for a case when
target of observation is situated on a equator and right one is for a target near a north pole. Simulations was made for observatory Kolonica AO (Slovakia), duration of observations is 5 years. Parameters of $3^{rd}$ body are presented in second column of table \ref{tab:3rd_body_par}. In a third and fourth columns are orbital parameters obtained by fitting of our O-C diagram with Markov chain Monte Carlo algorithm described in \cite{Gajdos2019}. Same fitting parameters were used for equator and north pole case. We can see, data obtained in case when star is situated on equator are fitted with a worse accuracy and accordingly a star situated near the north pole is fitted better.

\begin{figure}[!h]
    \centering
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{oc_1_5yrs.png}
        \caption{Star on equator.\\RA = 00:43:45.0838, DEC = +00:00:00}
    \end{subfigure}%
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{oc_2_5yrs.png}
        \caption{Star near north pole.\\RA = 00:43:45.0838, DEC = +89:30:00}
    \end{subfigure}
    \caption{O-C diagram for ideal conditions. Red line - fit with MCMC method, correspond to values in table \ref{tab:3rd_body_par}. Filled circles are primary minima, not filled circles - secondary minima.
    Observatory is Kolonica AO \\(Lat = +48.934917, Lon = 22.2738)}
\label{fig:oc_1}
\end{figure}

\begin{table}[!h]
 \caption{Original parameters of the 3$^{rd}$ body orbit and parameters obtained by fitting $O-C$ diagram simulated in ideal condition on equator and near pole. $P$ -- orbital period of eclipsing pair, $T_0$ -- initial minimum, $P_3$ - orbital period of the 3$^{rd}$ body, $t_{03}$ -- pericenter passage, $a\sin i_3$  -- projected semi-major axis of the orbit, $e_3$ -- eccentricity, $\omega_3$ -- the longitude of the periastron, $f(M_3)$ -- the mass function, $\chi^2$ -- sum of squares of the best fit, $\chi^2/n$ -- reduced sum of squares ($n$ -- number of data points), errors are given in parenthesis.}
 \vspace{-6mm}
 \begin{center}
  \begin{tabular}{lccc}
    \hline
	Solution			& Original		    		& Equator				&  near Pole	\\
  \hline\noalign{\smallskip}
 $P$ [days]				& 1.42834       			& fixed					& fixed	\\
 $T_0$ [HJD]			& 2451852.3783  			& fixed  				& fixed	\\
   \hline\noalign{\smallskip}
 $P_3$ [days]   		&   1000 	    			& 997(3)				& 999.90(92)\\
 $t_{03}$ [HJD]			& 2451400       			& 2451407(9) 			& 2451400(1)	\\
$a\sin i_3$ [AU]		&  0.797 	 				& 0.807(15)				& 0.8001(35)\\
 $e_3$     				&  0.54				    	& 0.55(1)		 		& 0.5449(45)\\
$\omega_3$ [\degree] 	&   288 					& 289(3)			 	& 288.16(76)\\
\hline\noalign{\smallskip}
$f(M_3)$  [M$_\odot$]	&  --	  					& 0.070(4)				& 0.06835(91)\\
\hline\noalign{\smallskip}
$\chi^2$ 				&  --						& 8.34		 			& 8.323\\
$\chi^2/n$				&  --						& 0.19					& 0.026\\
\hline\noalign{\smallskip}
\end{tabular}
\end{center}
\label{tab:3rd_body_par}
\vspace{-6mm}
\end{table}

\subsection{Weather and Photometry Precision}
If we will take into account other factors, like weather and precision of photometry, we will get less fit accuracy.
On the Fig.\ref{fig:oc_weather} (a, b) are presented O-C diagrams with same EB system as in previous case but now we take into account only 45\% of good weather, and photometry precision 0.0001. Such weather condition are rather good for our latitudes, photometry precision 0.0001 can be achieved on 400-600 mm telescopes with modern CCD cameras. Obtained parameters of $3^{rd}$ body orbit are presented in table \ref{tab:3rd_body_w}.

As we can se in table \ref{tab:3rd_body_w}, for a case when star is on equator difference with ideal condition is not so big on $\chi^2/n$ but accuracy of individual parameters is worse. It is because there is not much data point at all for this case.
On the other hand situation for a case when star is near the pole, in comparison with ideal conditions, is getting much worse.

If we do a simulation of O-C diagrams with more appropriate for our latitudes 30 \% probability of clear sky, values are getting worse. This results are presented on Fig. \ref{fig:oc_weather} (c,d) and in table \ref{tab:3rd_body_w}.

In real life for research of some EB system light curves from many different observatories are used. If this observatories are situated on different latitudes then we will not have such big gaps in our data as can be seen for a case of star on equator. The more observatories contribute to research the less important is influence of weather conditions and object location.
% Weather and photom precision-----------------------------------------
\begin{figure}[!h]
    \centering
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{oc_w.png}
        \caption{Star on equator. 45\% of clear nights.\\RA = 00:43:45.0838, DEC = +00:00:00}
    \end{subfigure}%
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{oc_eq_w2.png}
        \caption{Star near north pole. 45\% of clear nights\\RA = 00:43:45.0838, DEC = +89:30:00}
    \end{subfigure}

    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{oc_w30_e.png}
        \caption{Star on equator. 30\% of clear nights\\RA = 00:43:45.0838, DEC = +00:00:00}
    \end{subfigure}%
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{oc_w30_p.png}
        \caption{Star near north pole. 30\% of clear nights\\RA = 00:43:45.0838, DEC = +89:30:00}
    \end{subfigure}
    \caption{O-C diagram for 45\% (a,b) and 30\% (c,d) of good weather and photometry accuracy 0.0001.
    Red line - fit with MCMC method, correspond to values in table \ref{tab:3rd_body_w}. Filled circles are primary minima, not filled circles - secondary minima. Observatory is Kolonica AO \\(Lat = +48.934917, Lon = 22.2738)}
\label{fig:oc_weather}
\end{figure}

\begin{table}[!h]
 \caption{Original parameters of the 3$^{rd}$ body orbit and parameters obtained by fitting $O-C$ diagram simulated with influence of weather (45\% and 30\% of clear nights) and photometry precision 0.0001. For description of parameters see Table \ref{tab:3rd_body_par}}
 \vspace{-6mm}
 \begin{center}
  \begin{tabular}{lccccc}
    \hline
    Solution            & Original                  & Equator 45 \%         &  Pole 45\%          & Equator 30\%  &  Pole 30\%        \\
  \hline\noalign{\smallskip}
 $P$ [days]             & 1.42834                   & fixed                 & fixed               & fixed        & fixed             \\
 $T_0$ [HJD]            & 2451852.3783              & fixed                 & fixed               & fixed        & fixed             \\
   \hline\noalign{\smallskip}
 $P_3$ [days]           &   1000                    & 998(4)                & 1000(1)             & 1002(5)      & 999(1)            \\
 $t_{03}$ [HJD]         & 2451400                   & 2451413(11)           & 2451398(3)          & 2451376(16)  & 2451399(3)       \\
$a\sin i_3$ [AU]        &  0.797                    & 0.819(22)             & 0.805(4)            & 0.797(22)    & 0.801(5)          \\
 $e_3$                  &  0.54                     & 0.553(14)             & 0.5588(57)          & 0.569(23)    & 0.558(6)        \\
$\omega_3$ [\degree]    &   288                     & 291(5)                & 287.6(9)            & 280(5)       & 289(1)          \\
\hline\noalign{\smallskip}
$f(M_3)$  [M$_\odot$]   &  --                       & 0.074(6)              & 0.069(1)            & 0.067(6)     & 0.069(1)          \\
\hline\noalign{\smallskip}
$\chi^2$                &  --                       & 4.82                  & 36.46               & 15.00        & 31.29              \\
$\chi^2/n$              &  --                       & 0.18                  & 0.16                & 1.00         & 0.18              \\
\hline\noalign{\smallskip}

\end{tabular}
\end{center}
\label{tab:3rd_body_w}
\vspace{-6mm}
\end{table}

\subsection{Spots in EB Systems}
In previous chapters we was dealing with terrestrial factors that cause inaccuracy in O-C diagrams.
Now we will try to simulate O-C diagrams in ideal terrestrial conditions but under influence of other non-terrestrial factor like star spots.

As we know formation of star spots depends on a age of a host star, that's why we should separate our simulation on detached and over-contact EB systems.
But before we start to simulate we need to know some general spot parameters for this EB systems. Its a radius of star spot, its temperature, and life cycle. Do the spot disappear after some short time or can be present on a host star for a relative long time like 5-10 years? The spots migrate or not?

In \cite{Watson2004} authors said that depressed star-spots also cause a jitter in the residuals of O-C
diagrams, which can also result in the false detection of spurious orbital period changes. Such changes resulting from star-spots
would be distinguishable from other mechanisms that cause period
changes and it should still be possible to determine the orbital period accurately.

In previous work \cite{Kalimeris2002} authors have
shown that intensity variations resulting from star-spots (not taking into account Wilson depressions) can introduce disturbances of
up to $\sim 0.01~day$ in the O-C residuals of contact binaries. Given the
rapid evolutionary time-scales of spots (of the order of days) seen in
recent Doppler images of the contact binary AE Phe \citep{Barnes2004}
this may lead to explaining some of the observed jitter in the
O-C curves of these objects. But in work \cite{Watson2004}, the effects of a
Wilson depression seem to result in a scatter of only a few seconds
in the O-C residuals, it seems unlikely that the Wilson depression
will be a significant source of jitter for contact binaries \citep{Watson2004}.

Several studies have indicated that spot  on close binaries can have angular diameters from $10\degree$ to $40\degree$ which means that they
will cover from 5 to 25\% of the whole photosphere of the active components (e.g. \cite{Hall1990, Guinan1993}).
In most cases, active component can have one or two giant spots. It is also argued that such spots can remain on a surface from few years to even ten years \citep{Kalimeris2002}.

For a estimation of maximum effect caused by the star spot, it is assumed that in our simulations spots are located on equator of each component.
According to \cite{Kalimeris2002} spots with angular diameter less then $10\degree$ cause negligible shifts of the light minimum of the primary eclipse. Therefore we will not consider with spots smaller then $10\degree$ (see fig. \ref{fig:spot_diam}).

Furthermore, the spots on secondary component always remain visible during the primary eclipse,
while the spots on primary do not. As a result, the spots on primary cause a
significantly greater photometric perturbation to the light curve than the spots on secondary component.

\citeauthor{Kalimeris2002} also showed that unlike real orbital period changes, non-migrating star spots cannot
cause permanent slopes in the O-C diagrams.
But the O-C differences caused by long-lived migrating spots can be expected to be periodic.

\begin{figure}[!h]
\vspace{0cm}
\centerline{\includegraphics[width=0.60\textwidth]{spot_diam_2.png}}
\caption{Phase shift $\delta\varphi(\theta)$ of the primary eclipse of a contact
binary similar to AB And. Curves labelled by "P" correspond
to a spot on the primary, while "S" refers to a spot on the
secondary component \cite{Kalimeris2002}.}
\label{fig:spot_diam}
\end{figure}

\subsubsection{Detached EB Systems}
% Pro single stars
%Younger stars rotate faster than older stars and
%have higher activity levels and greater spot coverage (????). As a result, there is an overall trend of the
%degree of photometric variability with age. % \cite article "The Ages of Stars".

Tidal interactions force most of these stars to rotate synchronously with their orbital motions.
The rapid rotation combined with deep convection envelopes produces a variety of magnetic activity phenomena including starspots in
these stars.

Brightness variations due to starspots can be observed in detached EB only during primary total
eclipses, when the luminous hot components are hidden. Therefore, because of synchronised rotation,
only one hemisphere of the cool stars can be observed and the photometric data
collected are less detailed than for other spotted binaries \citep{Berdyugina2005}.

For detached eclipsing binary systems spots with radius from 5\degree~to~25\degree are usually typical, we can find such systems in a many publications like e.g. \cite{Liakos2011} or in publications about spot for RSCVn binaries systems (e.g. \cite{Roettenbacher2011, Kovari2012, Kozhevnikova2015}). Objects with spots migrations are very rarely observed, so we will not consider such case.
Model of detached EB system is presented on figure \ref{fig:eb_det_model}, main parameters of this system is listed in table \ref{tab:detached_params}.

As we mentioned above, spots with radius less then 10\degree~ do not have big influence on O-C diagram. So, we will consider how star spot located on stars equator (colat=90\degree, lon=0\degree) with radius $r_{spot}=15\degree$ and $r_{spot}=25\degree$~ affect the O-C diagram.
There is also a difference in hot and cold spot. Hot spot will have $T_{spot}=1.2$, where $T_{spot}$ is the ratio between the temperature of the
spot and the local temperature of the underlying photosphere, cold spot will be defined as $T_{spot}=0.8$. On figure \ref{fig:detached_spots} we can see how hot and cold spots affect the shape of the minima. Mainly primary minima is changing its shape under the influence of spots.

\begin{figure}[!h]
\vspace{0cm}
\centerline{\includegraphics[width=0.80\textwidth]{detached_pspot_08-25_view.png}}
\caption{Model of detached EB system with spot (colat=90\degree, colon=0\degree) in plane of sky view at phase 0.15.}
\label{fig:eb_det_model}
\end{figure}

\begin{table}[!h]
 \caption{Basic parameters of detached binary system presented on figure~ \ref{fig:eb_det_model}. HJD0 is a origin of the
 ephemeris; P - orbital period of EB system; SMA - semi-major axis; RM - mass ratio; VGA - centre of mass velocity, \\INCL - inclination}
 \begin{center}
 \vspace{-6mm}
  \begin{tabular}{c|c}
    \hline
HJD0(day) & 2451852.3783\\
P(day)    & 1.42834\\
\hline
SMA ($R_\odot$) &  1.000\\
RM              &  0.432\\
VGA (km/s)      &  0\\
INCL (\degree)  & 77.690\\
\hline
\end{tabular}
\end{center}
\label{tab:detached_params}
\vspace{-6mm}
\end{table}

\begin{figure}[!h]
    \centering
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{detached_pspot_08.png}
        \caption{Spot with $T_{spot}=0.8$}
    \end{subfigure}%
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{detached_pspot_12.png}
        \caption{Spot with $T_{spot}=1.2$}
    \end{subfigure}
    \caption{Spot on primary component with different temperature and radius.
    Blue line - spot radius 25\degree, red line - spot radius 15\degree}
\label{fig:detached_spots}
\end{figure}

The final O-C diagrams for all four cases are presented on figure \ref{fig:oc_detached_spot}. Looking only on O-C diagram we can't clearly see the difference. Only when we fit this O-C diagrams by MCMC method results are getting clearer.
Final results of fitting are presented in table \ref{tab:3rd_body_detached_spot}. The $\chi^2$ value of 4 cases are almost the same, except case of spot with $T_{spot}=1.2$ and $R_{spot}=25\degree$, in this case $\chi^2$ is the best.
%But if we take into account Bayesian information criterion (BIC) and Akaike information criterion (AIC), where bigger value corresponds to better fit, the situation changes to the opposite.
Best fit is case with small cold spot, and worst fit is case with hot and big spot.\\
(RECALCULATE!!!!!)

\begin{figure}[!h]
    \centering
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{oc_detached_pspot_R15_T12.png}
        \caption{O-C diagram with $R_{spot}=15, T_{spot}=1.2$}
    \end{subfigure}%
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{oc_detached_pspot_R25_T12.png}
        \caption{O-C diagram with $R_{spot}=25, T_{spot}=1.2$}
    \end{subfigure}

    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{oc_detached_pspot_R15_T08.png}
        \caption{O-C diagram with $R_{spot}=15, T_{spot}=0.8$}
    \end{subfigure}%
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{oc_detached_pspot_R25_T08.png}
        \caption{O-C diagram with $R_{spot}=25, T_{spot}=0.8$}
    \end{subfigure}
    \caption{O-C diagram for EB system with different $R_{spot}$ and $T_{spot}$\\
    Red line - fit with MCMC method, correspond to values in table \ref{tab:3rd_body_detached_spot}.
    Filled circles are primary minima, not filled circles - secondary minima.}
\label{fig:oc_detached_spot}
\end{figure}

\begin{table}[!h]
 \caption{Orbital parameters of $3^{rd}$ body for detached EB system with different spot parameters $R_{spot}$ and $T_{spot}$.
 For description of parameters see Table~\ref{tab:3rd_body_par}}
  \vspace{-6mm}
 \begin{center}
  \begin{tabular}{lccccc}
    \hline
    Solution            & Original       & $R_{spot}$=25\degree&$R_{spot}$=25\degree  &$R_{spot}$=15\degree &$R_{spot}$=15\degree \\
                        &                &    $T_{spot}$=1.2    &  $T_{spot}$=0.8 &  $T_{spot}$=1.2  &  $T_{spot}$=0.8 \\
  \hline\noalign{\smallskip}
 $P$ [days]             & 1.42834        &           fixed     & fixed       & fixed        & fixed       \\
 $T_0$ [HJD]            & 2451852.3783   &           fixed     & fixed       & fixed        & fixed       \\
   \hline\noalign{\smallskip}
 $P_3$ [days]           &   1000         &          999.9(9)   & 1001(1)     &   1000.7(9)  &  999.8(7)   \\
 $t_{03}$ [HJD]         & 2451400        &          2451400(2) & 2451394(2)  &   2451397(2) &  2451400(1) \\
$a\sin i_3$ [AU]        &  0.797         &          0.799(3)   & 0.803(3)    &   0.799(3)   &  0.801(2)   \\
 $e_3$                  &  0.54          &          0.541(4)   & 0.566(5)    &   0.548(3)   &  0.547(3)   \\
$\omega_3$ [\degree]    &   288          &          287.8(7)   & 286.6(8)    &   287.4(8)   &  288.0(5)   \\
\hline\noalign{\smallskip}
$f(M_3)$  [M$_\odot$]   &  --            &          0.0681(8)  & 0.0689(9)   &   0.0681(7)   &  0.0686(7)  \\
\hline\noalign{\smallskip}
$\chi^2$                &  --            &          21.714     & 54.867      &  74.652      &  74.168     \\
$\chi^2/n$              &  --            &           0.0347    & 0.1432      &   0.1161     &   0.1153    \\
% AIC                    &  --            &          31.714     & 64.867      &  84.652      &  84.168     \\
% BIC                    &  --            &          53.942     & 84.672      & 107.022      & 106.538     \\
\hline\noalign{\smallskip}

\end{tabular}
\end{center}
\label{tab:3rd_body_detached_spot}
\vspace{-6mm}
\end{table}

It was mentioned above that spot on secondary component do not have a big influence on minima shape, so we will not consider here this option.

As conclusion we can say that detached EB systems with spot are not sophisticated objects for $3^{rd}$ body detection. In case of bigger then 25\degree ~spots on surface it is advisable to cut off from the top the height of primary (or secondary) minima to get better fit and to determine time of minima more precisely.

\subsubsection{Contact EB Systems}
Contact binary stars occur relatively often among binaries (95\% of eclipsing binary
variables in the solar neighbourhood) \cite{Rucinski98}.
A contact binary system consists of two dwarf stars, most often from the F, G, and K
spectral classes, that are surrounded by a common convective envelope.
The orbital period distribution peaks in the 8 to 12 hour range. Most systems, though not all,
have orbital periods between 0.2 and 1.0 days \citep{Maceroni96, Paczynski2006}.
While the masses of the two component stars of a contact binary
are typically unequal, the two stars usually have approximately equal surface temperatures due to the effects of
mass and energy transfer between the components via a common convective envelope \citep{Lucy68}.
Eclipsing contact binaries are often referred to as W UMa systems in honor of the prototype \citep{Tran2013}.

The components of such a contact binary rotate very rapidly in spite of their old ages
($v~sin~i \sim 100 - 200~ km~s^{-1}$ ) as a result of spin-orbit synchronisation due to strong tidal interactions between the stars.

%Observations reveal that there are two subclasses of W UMa stars: A-type and W-type systems.
%The former have longer periods, are hotter, have larger total mass, and a smaller mass-ratio and
%are in better contact.
Many contact binaries show signs of stellar activity, presumably because the component stars are rapid
rotators with deep convective zones.
A study of the contact binaries with Doppler imaging
technique reveals that both components can be covered by cool starspots, with
a tendency for the primary to be more active than the secondary \citep{Maceroni94, Hendry2000, Barnes2004}.

This makes contact binaries excellent laboratories in
which to investigate the temporal variations and evolution of stellar spots, in part because the timescales of
the variations are shorter than in other types of binary and single stars.
However, the shortest among these timescales can be problematic to study using groundbased observatories because they are comparable to the
length of an Earth night.

\begin{figure}[!h]
\vspace{0cm}
\centerline{\includegraphics[width=0.8\textwidth]{Overcontact_mesh.png}}
\caption{Model of overcontact EB system with spot (colat=45\degree, colon=0\degree) in plane of sky view at phase 0.15.}
\label{fig:eb_overcont_model}
\end{figure}

\begin{table}[!h]
 \caption{Baisic parameters of overcontact binary system presented on figure~ \ref{fig:eb_overcont_model}. HJD0 is a origin of the
 ephemeris; P - orbital period of EB system; SMA - semi-major axis; RM - mass ratio; VGA - centre of mass velocity, \\INCL - inclination}
 \begin{center}
 \vspace{-6mm}
  \begin{tabular}{c|c}
    \hline
HJD0(day) & 2451852.3783\\
P(day)    & 1.42834\\
\hline
SMA ($R_\odot$) &  1.76\\
RM              &  0.67\\
VGA (km/s)      &  4.70\\
INCL (\degree)  & 79.50\\
\hline
\end{tabular}
\end{center}
\label{tab:overcontact_params}
\vspace{-6mm}
\end{table}

\cite{Kalimeris2002} noted that the migration of
starspots on the surface(s) of the constituent stars in
short-period binaries, especially contact binaries, could
affect measurements of eclipse times and thereby mimic
changes in the orbital period. \cite{Kalimeris2002} also
showed that the perturbations to the O-C diagrams would generally have amplitudes smaller than $\sim 0.01$ days, and could appear to
be quasiperiodic on timescales of a few hundred days or so if the spot migration is related to differential rotation
of the host star \citep{Tran2013}.

Model of overcontact EB system with spot is presented on figure \ref{fig:eb_overcont_model}, orbital parameters can be found in table \ref{tab:overcontact_params}.

\begin{figure}[!h]
    \centering
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{Overc_spot_pos_lat.png}
        \caption{LC with spot on different colatitude and \\longitude = 0\degree}
    \end{subfigure}%
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{Overc_spot_pos_lon.png}
        \caption{LC with spot on different longitude and \\colatitude = 45\degree}
    \end{subfigure}
    \caption{LC of overcontact binary system with "cold" spot ($T_{spot}=0.8~T_{surf}$) on primary component at different positions}
\label{fig:overcontact_spot_pos}
\end{figure}

Light curve from such EB system will undergo changes depending on the position of the spot, such changes are partially presented on figure \ref{fig:overcontact_spot_pos}, \ref{fig:overcontact_spotT12_pos}. Analyzing these data we can conclude that the most noticeable changes in LC are caused by changes of position in longitude of a starspot on primary component of contact binary EB system.
If we change spot position only in colatitude and leave longitude = 0\degree, we will observe only variation of primary minima shape and depth. On the other hand if we make colatitude constant (colatitude = 45\degree~ on figures \ref{fig:overcontact_spot_pos}, \ref{fig:overcontact_spotT12_pos}) and change longitude of spot then in addition to the primary minimum part of the LC between two minima will vary too. As it can be expected "hot" spot has a greater contribution to changes in LC than "cold" spot (see fig \ref{fig:overcontact_spot_pos}b vs \ref{fig:overcontact_spotT12_pos}b).

\begin{figure}[!h]
    \centering
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{overc_spotT12_pos_lat.png}
        \caption{LC with spot on different colatitude and \\longitude = 0\degree}
    \end{subfigure}%
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{overc_spotT12_pos_lon.png}
        \caption{LC with spot on different longitude and \\colatitude = 45\degree}
    \end{subfigure}
    \caption{LC of overcontact binary system with "hot" spot ($T_{spot}=1.2~T_{surf}$) on primary component at different positions}
\label{fig:overcontact_spotT12_pos}
\end{figure}

From previous results with detached EB system we already know that bigger and hotter spot adds more uncertainty to definition of orbital elements $3^{rd}$ body available in EB system. We can expect same results with contact EB systems. So let us define a influence of spot migration and presence of spot on secondary EB component. This two aspects can be often found in contact and overcontact systems.

According to \cite{Kalimeris2002} rotation period of starspot differs from $P_{eq}$ by:
\begin{equation}
\Delta P = P_\lambda-P_{eq},  % = \frac{P_{eq} \cdot k \cdot \sin^2\lambda}{1-k \cdot \sin^2 \lambda}
\end{equation}
where $P_{eq}$ is the equatorial period of rotation ($P_{eq}=P_0$), and $P_{\lambda}$ is a period of starspot rotation equal to:

\begin{equation}
P_\lambda = P_{eq} \cdot (1-k \cdot \sin^{2}\lambda)^{-1},
\label{eq:Pl}
\end{equation}

in equation \ref{eq:Pl} $k$ is a coefficient of differential rotation. For close binaries observation have indicated a range between
$k\approx 6 \times10^{-4}$ and 0.18 with mean value $\bar{k}=3 \times10^{-2}$ \citep{Hall1990}. At the end of each orbital cycle, a starspot will shift  in longitude by:
\begin{equation}
\delta \theta = 2\pi \frac{\Delta P}{P}
\end{equation}

If we calculate this value for our EB system with period $P=1.42834$ which has spot on latitude $\lambda=45\degree$ and mean value of $k$ we will get $\delta \theta = 5.4798\degree$ or $\delta \theta \sim 5.5\degree$. So spot with such parameters will do a full revolution in $\sim 65.5$ cycles or in our case this is equal to 93.5 days. To simplify the process of simulation we take an integer number of $\delta \theta = 6\degree$ in such case the spot will do a full revolution in 60 cycles or in 85.7 days.

Large spots causing prominent light curve minima apparently can survive for many years, despite differential rotation, and form centres
of activity, or active longitudes \citep{Berdyugina2005}. Polar spots are found to have lifetimes of over a decade \citep{Hussain2002}.
Our simulation of $3^{rd}$ body is made on 5 years time interval so lets define that our spot lifetime is also 5 years.

Last question that we need to know is a radius of a spot. Lets consider a case with a spot radius $35\degree$ because a bigger spots are more typical for this type of EB systems.

\begin{figure}[!h]
    \centering
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{overc_ideal.png}
        \caption{o-c ideal}
    \end{subfigure}%
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{overc_spot_migr2.png}
        \caption{o-c with spot}
    \end{subfigure}
    \caption{O-C}
\label{fig:overcontact_spot_oc}
\end{figure}

From figure \ref{fig:overcontact_spot_oc} we can clearly see how migrated spot reduce precisions of minima exact time detection and in some cases we even got point that do not fit in our O-C diagram. This means that these point are faults. Migrated spots are the most difficult cases for precise definition time of minima, make O-C diagram and define orbital parameters of $3^{rd}$ body.
Fitted parameters of orbit and $\chi^2$ values of fit are presented in table \ref{tab:3rd_body_overc_spot}.

\begin{table}[!h]
 \caption{Orbital parameters of $3^{rd}$ body for overcontact EB system with migrated spot. Spot parameters $R_{spot}=35\degree$, $T_{spot}=0.8~T_{surf}$. For description of parameters see Table~\ref{tab:3rd_body_par}}
  \vspace{-6mm}
 \begin{center}
  \begin{tabular}{lccc}
    \hline
    Solution            & Original       & no       & migrated   \\
                        &                & spot     & spot  \\
  \hline\noalign{\smallskip}
 $P$ [days]             & 1.42834        &           fixed     & fixed        \\
 $T_0$ [HJD]            & 2451852.3783   &           fixed     & fixed        \\
   \hline\noalign{\smallskip}
 $P_3$ [days]           &   1000         &          999.7(8)   & 1000(1)      \\
 $t_{03}$ [HJD]         & 2451400        &          2451400(2) & 2451398(4)   \\
$a\sin i_3$ [AU]        &  0.797         &          0.799(3)   & 0.764(3)     \\
 $e_3$                  &  0.54          &          0.541(4)   & 0.541(2)     \\
$\omega_3$ [\degree]    &   288          &          288.1(7)   & 287(1)     \\
\hline\noalign{\smallskip}
$f(M_3)$  [M$_\odot$]   &  --            &          0.0681(8)  & 0.0666(7)    \\
\hline\noalign{\smallskip}
$\chi^2$                &  --            &          0.614      & 2478.629      \\
$\chi^2/n$              &  --            &          0.0283     & 6.1200       \\
% AIC                    &  --            &          10.614     & 2488.629       \\
% BIC                    &  --            &          27.628     & 2508.709      \\
\hline\noalign{\smallskip}

\end{tabular}
\end{center}
\label{tab:3rd_body_overc_spot}
\vspace{-6mm}
\end{table}

\subsection{Pulsations in EB Systems}
Pulsation in eclipsing binary systems can be observed in Algol-type binaries, the so-called oEA stars (oscillating EA stars).
The oEA stars are mass-accreting mainsequence A/F-type components in semidetached Algol-type eclipsing binary systems showing $\delta$~Sct - like pulsation \citep{Rodriguez2010}. The period of pulsation in such systems can vary from 20 to 300 minutes (see e.g \cite{Liakos2017}, \cite{Mkrtichian2007}).

In work \cite{Liakos2017} relation for period of pulsation is given as:
\begin{equation}
P_{pul} = 0.031(4) + 0.009(1) P_{orb}.
\end{equation}

This relation is based on observation of $\delta$~Sct stars in all known close binaries systems (Detached, Semi-detached, unclassified). Coefficient of correlation for this relation is $r = 0.62$.

To determine how the pulsations affect the O-C diagram and $3^{rd}$ precision of orbital elements we will consider three cases of detached EB systems with different period of pulsations: 30, 150, 300 minutes.

To determine precise time of minima with pulsation presence it is very important have high rate of observation per some time interval.
Until now we use synthetic LC generated by PHOEBE with sample rate 300 point per period. This rate is not enough for pulsation study.
To determine what sample rate should we use lets see what results we will obtain in detached EB system with added pulsations with period $P_{pus}=30$ min and amplitude $A_{puls} = 0.02$.


\section{Approximating Elasticity Tensors}
\label{sec:62tensors}

\paragraph{Optimization process}

\minitoc{75mm}{7}

During the process of solving \cref{eq:topoOptProblemDiscrete},
optimization algorithms typically
evaluate the objective function $\compliance(\mcp{1}, \dotsc, \mcp{M})$
iteratively at different \term{micro-cell parameter combinations}
$(\mcp{1}, \dotsc, \mcp{M}) \in (\real^d)^M$.
Every evaluation of $\compliance$ corresponds to one solution of a
macro-problem.
However, to solve the macro-problem,
the elasticity tensors $\etensor^{(q)}$ of all $M$ macro-cells
need to be known.
Hence, in every optimization iteration, it is necessary to solve
one macro-problem and $M$ micro-cell problems,
all with the \fem.
This naive approach has two major drawbacks, which we explain in
the following.

\subsection{Drawbacks of the Naive Approach}
\label{sec:621drawbacks}

\paragraph{Drawback 1: Computation time}

First, this approach is computationally infeasible
even for simple micro-cell models and optimization scenarios.
The computation of a single elasticity tensor usually takes seconds to
minutes.
All $M$ micro-cell problems per optimization iteration
can be solved in parallel without any communication.
However, $M$ is typically in the range of thousands and
there are thousands or tens of thousands optimization iterations
(the optimization problem is $(d \cdot M)$-dimensional!).
This implies that the overall computation may still take
several days or even weeks to complete.

\paragraph{Drawback 2: Approximation of gradients}

Second, most optimization algorithms require gradients of the
objective function and of the constraints, i.e.,%
%\footnote{%
%  By convention, the gradient $\gradient{\*x}{f}(\*x)$ of a
%  scalar-valued function $\objfun$ is the column vector of partial derivatives.
%  For vector-valued functions $\*f$, the gradient $\gradient{\*x}{\*f}(\*x)$
%  is defined as the transposed Jacobian of $\*f$, i.e.,
%  the matrix whose columns are the gradients $\gradient{\*x}{f_j}(\*x)$ of
%  single components of $\*f$.
%  If $\*f$ is matrix-valued, then its gradient is defined as the
%  gradient of its vectorization.%
%}
\begin{equation}
  \partialderiv{\partialdiff{} x_t}{\etensor^{(q)}}(\mcp{q}),\quad
  \partialderiv{\partialdiff{} x_t}{\denscell^{(q)}}(\mcp{q}),\qquad
  q = 1, \dotsc, M,\quad
  t = 1, \dotsc, d.
\end{equation}
%where $\etensor^{(q)}$ is the elasticity tensor and
%$\denscell^{(q)}$ is the density of the $q$-th macro-cell.
However, in general, both gradients are unavailable and
have to be approximated by finite differences.
This introduces new error sources and
increases the number of elasticity tensors to be evaluated,
further slowing down the solution process.
Additionally, the number of optimization iterations necessary to
achieve convergence might increase
if there are discontinuities in the objective function
or its gradient.
Such discontinuities can already be caused by the inexact solution of the \fem.
If we need Hessians or other higher-order derivatives,
then the issues even worsen.

\subsection{B-Splines on Sparse Grids for Topology Optimization}
\label{sec:622BSplines}

\paragraph{Elasticity tensor function}

As a remedy, we replace the costly elasticity tensors with cheap surrogates.
If we assume that all macro-cells use the same micro-cell model,
the elasticity tensor $\etensor^{(q)}$ of the $q$-th macro-cell
with the parameter $\mcp{q} \in \clint{\*0, \*1}$
can be written as the value $\etensor(\mcp{q})$ of some function
$\etensor\colon \clint{\*0, \*1} \to \real^{6 \times 6}$
(assuming that $\dimobjdomain = 3$) at the point $\mcp{q}$.
In the following,
$\etensor\colon \clint{\*0, \*1} \to \real^m$
gives $m \in \nat$ values from which the symmetric elasticity tensor
can be uniquely reconstructed,
i.e., $m = 6$ for $\dimobjdomain = 2$ and $m = 21$ for $\dimobjdomain = 3$.
The vector-valued/matrix-valued versions of $\etensor$
will be used interchangeably.

\paragraph{Elasticity tensor surrogate}

The idea is to use B-splines on sparse grids to approximate
the elasticity tensor function $\etensor$.
In contrast to the theoretical framework that we established in
\cref{chap:20sparseGrids,chap:30BSplines,chap:40algorithms},
the function to be interpolated is not scalar-valued, but vector-valued.
This means that we have to construct $m$ sparse grid interpolants
$\etensorentryintp{j}$
for the $m$ components $\etensorentry{j}$ of $\etensor$ ($j = 1, \dotsc, m$).
Note that one could generate different spatially adaptive sparse grids for the
different components $\etensorentryintp{j}$.
However, it is not possible to evaluate only specific entries of $\etensor$
without also evaluating all other entries,
which means that we would waste computational resources by selecting only
a subset of the calculated entries.
Therefore, we use the same grid for all components.

Additionally, we approximate the density $\denscell^{(q)}$
of the $q$-th macro-cell with a surrogate $\denscellintp$ using
B-splines on the same sparse grid as for $\etensorentryintp{j}$
for reasons of implementation,
resulting in $m + 1$ sparse grid interpolants in total.
From a theoretical perspective, this is not necessary,
since the density can be explicitly calculated with simple formulas
for most micro-cell models, independently of evaluations of the
elasticity tensor.

\paragraph{Advantages}

Our approach has multiple obvious advantages:
\begin{itemize}
  \item
  The sparse grid interpolant $\etensorintp$ has to be generated only
  once in an \term{offline step} before the optimization algorithm starts.
  During the optimization \term{(online phase),}
  only inexpensive evaluations of $\etensorintp$ are performed,
  saving much computation time.

  \item
  Sparse grids ease the curse of dimensionality, which prohibits
  conventional full grid interpolation methods if $d > 4$.

  \item
  With spatially adaptive sparse grids and a suitable refinement criterion,
  we can spend more grid points in regions of interest of $\etensor$,
  e.g., regions with large oscillations.

  \item
  By using B-splines as basis functions,
  the interpolant $\etensorintp$ will be more accurate
  than with piecewise linear basis functions.
  In addition, we can calculate its derivatives
  $\tpartialderiv{\partialdiff{} x_t}{\etensorintp}(\mcp{q})$
  fast and explicitly,
  accelerating the speed of convergence of the optimizer.
\end{itemize}

\breakpagebeforenextheadingtrue
\subsection{Cholesky Factor Interpolation}
\label{sec:623cholesky}

\paragraph{Positive definiteness of elasticity tensors}

Unfortunately, just replacing elasticity tensors with
B-spline surrogates often does not lead to correct results in practice.
Experiments show that for only for some sparse grids,
the optimization algorithm converges to an optimal point
\cite{Valentin16Hierarchical}.
The optimization algorithm crashes for most spatially adaptive grids,
not being able to find any meaningful optimum.
%
The root of the problem proves to be that
the interpolated elasticity tensors $\etensorintp(\*x)$ are not
positive definite for specific
micro-cell parameters $\*x \in \clint{\*0, \*1}$.
However, indefinite or even negative definite tensors $\etensorintp$
would mimic unphysical behavior.%
\footnote{%
  In the scalar case, this is analogous to Hooke's law for linear springs,
  where the force $F = kx$ needed to displace the end of a spring
  (fixed at the other end) by $x$ is proportional to $x$.
  The proportionality constant $k$ (which corresponds to the elasticity tensor)
  has to be positive.%
}
Hence, it is imperative for the optimization process that
the interpolated elasticity tensors are \spd.

\paragraph{Positive definiteness of sparse grid interpolants}

Interpolation on sparse grids per se does not preserve
positive definiteness.
A counterexample is shown in \cref{fig:cholesky1},
which displays the minimal eigenvalue of the elasticity tensor surrogate
resulting from interpolation on a regular sparse grid.
As the positivity of the diagonal is a necessary condition
for positive definiteness,
small oscillations of the interpolant of some entries
already make the whole elasticity tensor non-positive-definite.

\begin{figure}
  \subcaptionbox{%
    Minimal eigenvalue of $\etensorintp(\*x)$%
    \label{fig:cholesky1}%
  }[65mm]{%
    \includegraphics{cholesky_1}%
  }%
  \hfill%
  \subcaptionbox{%
    Minimal eigenvalue of $\etensorcholintp(\*x)$%
    \label{fig:cholesky2}%
  }[65mm]{%
    \includegraphics{cholesky_2}%
  }%
  \hfill\hfill%
  \raisebox{2.2mm}{\includegraphics{cholesky_3}}%
  \caption[%
    Minimal eigenvalue of interpolated elasticity tensors%
  ]{%
    Minimal eigenvalue \emph{(colored contour lines)}
    of elasticity tensor surrogates
    for the 2D cross model ($\dimobjdomain = 2$, $d = 2$)
    \vspace{-0.3em}%
    and cubic hierarchical B-splines $\bspl{\*l,\*i}{p}$ ($p = 3$) on
    the regular sparse grid $\coarseregsgset{n}{d}{b}$ \emph{(dots)}
    with $n = 6$ and $b = 4$.
    \emph{Left:} The minimal eigenvalue of $\etensorintp(\*x)$
    becomes negative in some regions \emph{\textcolor{C1}{(red areas)}}
    of the domain $\clint{\*0, \*1}$,
    indicating that $\etensorintp(\*x)$ is not positive definite.
    \emph{Right:} The minimal eigenvalue of $\etensorcholintp(\*x)$
    is non-negative in the whole domain $\clint{\*0, \*1}$.%
  }%
  \label{fig:cholesky}%
\end{figure}

These oscillations are more likely to occur near the boundary of the domain
$\clint{\*0, \*1}$, such that there are larger regions
where the interpolated tensor is not positive definite anymore.
The reason is two-fold:
First, sparse grids without boundary points
are notoriously biased towards the center of the domain,
as they place only few points near the boundary \cite{Pflueger10Spatially}.
This leads to a loss of interpolation accuracy near the boundary
when compared to the center of $\clint{\*0, \*1}$.
Second, both the minimal eigenvalue of $\etensor(\*x)$ and the norm of its
gradient with respect to $\*x$ are small near $x_1 = 0$ or $x_2 = 0$.
Consequently, the ``surface'' of the minimal eigenvalue function
is rather flat in these regions and almost vanishes,
facilitating the existence of negative eigenvalues of
surrogate functions $\etensorintp(\*x)$.

Note that for most micro-cell models,
the optimization algorithm often evaluates the objective function
$\compliance(\mcp{1}, \dotsc, \mcp{M})$ at micro-cell parameter
combinations for which many of the points $\mcp{j}$ are near the boundary
of $\clint{\*0, \*1}$.
This is because many of the macro-cells will either be empty or
fully filled with material, which usually corresponds to micro-cell
parameters near zero or one, respectively.
Thus, $\etensorintp$ is frequently evaluated in the regions of
indefiniteness, which further worsens the issue.

\pagebreak

\paragraph{Positivity-preserving methods}

Even in one dimension, it cannot be guaranteed that the interpolant of
positive data remains positive,
which is a key problem in the estimation of probability densities
\multicite{Pflueger10Spatially,Griebel10Finite,Franzelin16From}.
Just clamping the interpolated values via $\max(\cdot, 0)$ does not help:
In our application, the tensor may still be indefinite;
additionally, the calculated gradients of the interpolants do not match
the actual gradients anymore.
In density estimation, clamping a density-like function changes its
integral, making it necessary to recalculate its normalization constant
\cite{Franzelin17Data}.

One possible workaround is to apply a continuous injective transformation
$T\colon \posreal \to \real$ on the positive values (e.g., $\ln$),
then interpolate the resulting values, and finally
apply the inverse transformation $T^{-1}\colon \real \to \posreal$
on the interpolated values (e.g., $\exp$).%
\footnote{%
  Formally, the inverse function $T^{-1}\colon T(\posreal) \to \posreal$
  is only defined on the image $T(\posreal)$ of $T$,
  which might not be the whole real line.
  However, we assume that $T^{-1}$ can be ``reasonably'' extended to $\real$
  (e.g., $T \ceq \sqrt{\cdot}$ and $T^{-1} = ({\cdot})^2$).%
}
%In density estimation, this method might introduce new unwanted modes into the
%probability distribution.
For the piecewise linear hierarchical basis, another approach has been
developed recently \cite{Franzelin17Data},
maintaining the positivity by inserting additional sparse grid points.
In the context of spline approximation,
positivity-preserving approximation schemes based on so-called
quasi-interpolation are known \cite{Hoellig13Approximation}.
For our application, for which we need to preserve positive definiteness,
it is conceivable that one could apply these positivity-preserving
methods in the eigenspace,
interpolating the positive eigenvalues.

\paragraph{Interpolation of Cholesky factors}

Instead, we pursue a different, more canonical
approach based on Cholesky factorization:

\begin{proposition}[Cholesky factorization]
  For every \spd matrix $\etensor \in \real^{6 \times 6}$,
  there is a unique upper triangular matrix
  $\cholfactor \in \real^{6 \times 6}$
  with positive diagonal entries such that
  \begin{equation}
    \etensor
    = \tr{\cholfactor} \cholfactor.
  \end{equation}
\end{proposition}

\begin{proof}
  See \cite{Benoit24Note} or \cite{Freund07Stoer}.
\end{proof}

%\usenotation{Ëchol}
In one dimension, the Cholesky factorization is equivalent
to the application of a transformation $T$ as above by choosing
$T \ceq \sqrt{\cdot}$ and $T^{-1} = (\cdot)^2$.
Our approach is as follows:
\begin{enumerate}
  \item
  Define $\cholfactor\colon \clint{\*0, \*1} \to \real^{6 \times 6}$
  as the Cholesky factor of
  $\etensor\colon \clint{\*0, \*1} \to \real^{6 \times 6}$, i.e.,
  $\etensor(\*x) = \tr{\cholfactor(\*x)} \cholfactor(\*x)$
  for all $\*x \in \clint{\*0, \*1}$.

  \item
  During the grid generation (offline phase),
  evaluate $\etensor(\gp{\*l,\*i})$ at the grid points $\gp{\*l,\*i}$,
  compute the Cholesky factors $\cholfactor(\gp{\*l,\*i})$ of
  $\etensor(\gp{\*l,\*i})$,
  and interpolate them instead of the elasticity tensors
  to obtain an interpolant
  $\cholfactorintp\colon \clint{\*0, \*1} \to \real^{6 \times 6}$.

  \item
  During the optimization (online phase),
  every time the value $\etensor(\*x)$ of an elasticity tensor is needed,
  the interpolant $\cholfactorintp(\*x)$ is evaluated and we return
  \begin{equation}
    \etensorcholintp(\*x)
    \ceq \tr{\cholfactorintp(\*x)} \cholfactorintp(\*x).
  \end{equation}
\end{enumerate}

\paragraph{Advantages of Cholesky factor interpolation}

As shown in \cref{fig:cholesky2},
the resulting elasticity tensor surrogate $\etensorcholintp$
is positive semidefinite on the whole domain and
positive definite almost everywhere:
The surrogate $\etensorcholintp(\*x)$ is singular if and only if
$\cholfactorintp(\*x)$ is singular, which is in general
only the case on a negligible null set in $\clint{\*0, \*1}$.

Another advantage of this approach is that not only the
positive definiteness, but also the explicit differentiability
of the surrogate $\etensorcholintp$ is preserved.
The gradient can be computed easily and fast with the product rule:
\begin{equation}
  \label{eq:choleskyFactorDerivative}
  \partialderiv{\partialdiff{} x_t}{\etensorcholintp}(\*x)
  = \tr{\cholfactorintp(\*x)} \cdot
  \partialderiv{\partialdiff{} x_t}{\cholfactorintp}(\*x) +
  \tr{\partialderiv{\partialdiff{} x_t}{\cholfactorintp}(\*x)} \cdot
  \cholfactorintp(\*x),\quad
  t = 1, \dotsc, d,
\end{equation}
% e.g. R = [[r11, r12],
%           [r21, r22]]
% ==> E = R^T * R = [[r11^2+r21^2,     r11*r12+r21*r22],
%                    [r11*r12+r21*r22, r12^2+r22^2]]
% ==> dE = [[2*r11*dr11+2*r21*dr21, r11*dr12+r12*dr11+r21*dr22+r22*dr21],
%           [r11*dr12+r12*dr11+r21*dr22+r22*dr21, 2*r12*dr12+2*r22*dr22]]
% dR = [[dr11, dr12],
%       [dr21, dr22]]
% dR^T = [[dr11, dr21],
%         [dr12, dr22]]
% R = [[r11, r12],
%      [r21, r22]]
% R^T = [[r11, r21],
%        [r12, r22]]
% ==> dR^T * R = [[r11*dr11+r21*dr21, r12*dr11+r22*dr21],
%                 [r11*dr12+r21*dr22, r12*dr12+r22*dr22]]
% ==> R^T * dR = [[r11*dr11+r21*dr21, r11*dr12+r21*dr22],
%                 [r12*dr11+r22*dr21, r12*dr12+r22*dr22]]
% ==> R^T * dR + dR^T * R
% = [[2*r11*dr11+2*r21*dr21, r11*dr12+r12*dr11+r21*dr22+r22*dr21],
%    [r11*dr12+r12*dr11+r21*dr22+r22*dr21, 2*r12*dr12+2*r22*dr22]]
% = dE
where both the sparse grid interpolant $\cholfactorintp(\*x)$ and
its derivative $\partialderiv{\partialdiff{} x_t}{\cholfactorintp}(\*x)$
are known.
As discussed above,
this is key to the applicability of gradient-based optimization.


\section{Packet Error Probability}\label{sec:pep}
Now that we are able to simulate the \gls{rssi} when transmitting from one node to another, we need to be able to simulate whether the packet should arrive on the receiving node. With wireless radio communication, there is a chance that a receiving node may not receive the entire packet correctly, which is why we need some way of computing the probability of this happening. For this, we introduce the packet error probability. The computations in this section are derived from \cite{massoud2007digital}, as well as personal communication with the author of \cite{paper:linkmodel}. \medbreak

\subsection{Radio Model}

First we calculate the noise power $P_{N,db} = thermal\_noise + noise\_figure$ in \acrshort{db}. The noise power is calculated with the thermal noise and noise figure, and is the level of background noise affecting the wireless radio communication. For the Reachi devices we assume that the

$thermal\_noise = -119.66$ \acrshort{db} and the $noise\_figure = 4.2$ \acrshort{db}. \medbreak

Next we need to add the noise from interfering transmissions happening at the same time. We do this by adding the sum of the \gls{rssi} from interfering transmitters to the noise power $P_{N,dB}$, giving us $P_{NI,dB}$ on the link between the receiving node $n_r$ and the transmitting node $m_t$. The set of currently transmitting nodes are denoted by $nodes_t$ and the function $RSSI_{dBm}(n, m)$ denotes the RSSI on the link between nodes $n$ and $m$.
\begin{eq}\label{eq:noisepower}
    P_{NI,db}(n_r, m_t, nodes_t) = 10 \log_{10}\left( 10^{\frac{P_{N,dB}}{10}} + \mathlarger{\sum}\limits_{m \in nodes_t - \{m_t\}}  10^{\frac{RSSI_{dBm}(n_{r}, m)}{10}} \right)
\end{eq}

Note that as both the noise power $P_{N,dB}$ and the \gls{rssi} is in \acrshort{db} (a logarithmic scale), we first need to convert the values to linear scale, before computing the sum of the noise and interference power, and then convert the value back into logarithmic scale (\acrshort{db}). \smallbreak

Next we calculate the signal to noise (and interference) ratio $\gamma_{dB}$ in \acrshort{db}. The ratio $\gamma_{dB}$ is the ratio between signal and noise, that compares the level of the signal to the level of the background noise (including the interference from other transmissions), and is computed by subtracting the noise power $P_{NI,db}$:

%We expand upon this further to include the interference of other transmissions happening at the same time. This is done by computing the \gls{rssi} from interfering transmitters, and subtracting this from the \gls{sinr}, which gives us the \acrlong{snir}:

% n receiver, m transmitter

%where we assume that the $thermal\_noise = -119.66$ \acrshort{db} and the $noise\_figure = 4.2$ \acrshort{db}.

%\begin{eq}
%    P_{N,db} = thermal\_noise + noise\_figure
%\end{eq}

%Signal to noise ratio with interference, on a link from $n_r$ (receiver) to $n_t$ (transmitter). The set of currently transmitting nodes are denoted by $nodes_t$. The function $RSSI(n, m)$ denotes the RSSI on the link between nodes $n$ and $m$. We subtract the sum of the RSSI between $n_r$ and any currently transmitting nodes, excluding $m_t$.

\begin{eq}
    \gamma_{dB}(n_r, m_t, nodes_t) = RSSI(n_r, m_t) - P_{NI,dB}(n_r, m_t, nodes_t)
\end{eq}

We use this to compute the bit error probability $P_b$:

%\begin{eq}
%    \gamma = 10^{\frac{\gamma_{dB}}{10}}
%\end{eq}

\begin{eq}
    P_b(n_r, m_t, nodes_t) = \frac{1}{2}erfc \left( \sqrt{ \left( \frac{10^{\frac{\gamma_{dB}(n_r, m_t, nodes_t)}{10}}}{2} \right)} \right)
\end{eq}

which we finally can use to compute the packet error probability $P_p$. The packet error probability is the probability that we experience a bit error for any of the bits in our packet, during transmission.
\begin{eq}
    P_p(n_r, m_t, nodes_t, packetsize) = 1 - \left( 1 - P_b(n_r, m_t, nodes_t) \right) ^{packetsize}
\end{eq}

\subsection{Example}
In \autoref{sec:linkmodel}, we computed the link model for a sample network topology \textbf{G}. \autoref{eq:rssivector} shows a vector containing the \gls{rssi}, in \acrshort{dbm}, for each of the six links in the network, assuming a transmission power of 26 \acrshort{dbm}. For the following, it is assumed that $thermal\_noise = -119.66$ \acrshort{db}, the $noise\_figure = 4.2$ \acrshort{db}, and $packetsize = 160$ bits (which is the size of a header packet for the Reachi devices).

\begin{eq}\label{eq:rssivector}
    \vect{RSSI_{dBm, \textbf{G}}} =
    \begin{bmatrix}
        -67.390 \\
        -61.734 \\
        -72.170 \\
        -52.163 \\
        -74.042 \\
        -72.413
    \end{bmatrix}
\end{eq}

If we assume that $n_2$ is currently listening, and the currently transmitting nodes $nodes_t = \{n_1, n_4\}$. What is the probability for packet error on the link between nodes $n_2$ and $n_1$ with interference from node $n_4$? First, we compute the noise power $P_{NI,db}$, according to \autoref{eq:noisepower}:
\begin{eq}
    P_{NI,db}(n_2, n_1, nodes_t) = 10 \log_{10}\left( 10^{\frac{(-119.66 + 4.2)}{10}} + 10^{\frac{-74.042}{10}} \right) = -74.041
    %\mathlarger{\sum}\limits_{m \in {n_1, n_3}}  10^{\frac{RSSI_{dBm}(n_{2}, m)}{10}} [m \neq n_{4}] \right)
\end{eq}

We subtract the noise power $P_{NI,db}$ from the \gls{rssi} to get the signal to noise (and interference) ratio $\gamma_{dB}$:
\begin{eq}
    \gamma_{dB}(n_2, n_1, nodes_t) = -67.390 - (-74.041) = 6.651
\end{eq}

With which we can compute the bit error probability:
\begin{eq}
    P_b(n_2, n_1, nodes_t) = \frac{1}{2}erfc \left( \sqrt{ \left( \frac{10^{\frac{6.651}{10}}}{2} \right)} \right) = 0.000536
\end{eq}

Finally we can compute the packet error probability using the bit error probability:
\begin{eq}
    P_p(n_2, n_1, nodes_t, packetsize) = 1 - \left( 1 - 0.000536 \right) ^{160} = 0.082
\end{eq}

This gives us a 8.2 \% probability that we will experience packet error on the link from $n_2$ to $n_1$, with a single interfering transmitter, which is a significant difference in relation to the same transmission with no interfering transmitters. To demonstrate this, \autoref{figure:pepegraph} shows the probability for packet error with no interfering transmitters. According to the figure, an \gls{rssi} of approximately $-103.0$ \acrshort{dbm} would have a probability for packet error close to zero, and an \gls{rssi} of approximately $-110.0$ \acrshort{dbm} would have a probability for packet error close to 100 \%. If we look back to the \gls{rssi} vector, \vect{RSSI_{dBm, \textbf{G}}}, we see that the \gls{rssi} on the link between nodes $n_1$ and $n_2$ is $-67.390$ \acrshort{dbm}, which is significantly better than the $-103.0$ \acrshort{dbm} we see in the graph, but with just a single interfering transmitter, the probability for packet error increases to 8.2 \%, which corresponds to what we see in \autoref{figure:pepegraph1}, where an \gls{rssi} of approximately $-66.0$ \acrshort{dbm} is required for a probability for packet error close to zero, with a single interfering transmitter.

%\begin{figure}[H]
%    \centering
%    \includegraphics[width=.8\textwidth]{figures/pep/pep_0_interference.png}
%    \caption{Probability for packet loss on a link with no interfering transmitters.}
%    \label{figure:pepegraph}
%\end{figure}
%\begin{figure}[H]
%    \centering
%    \includegraphics[width=.8\textwidth]{figures/pep/pep_1_interference.png}
%    \caption{Probability for packet loss on a link with a single interfering transmitter.}
%    \label{figure:pepegraph1}
%\end{figure}
\begin{figure}[H]
    \centering
    \begin{subfigure}[t]{.9\textwidth}
        \includegraphics[width=\linewidth]{figures/pep/pep_0_interference.png}
        \caption{Probability for packet error on a link with no interfering transmitters.}
        \label{figure:pepegraph}
    \end{subfigure}
    \begin{subfigure}[t]{.9\textwidth}
        \includegraphics[width=\linewidth]{figures/pep/pep_1_interference.png}
        \caption{Probability for packet error on a link with a single interfering transmitter.}
        \label{figure:pepegraph1}
    \end{subfigure}
    \caption{Probability for packet error with and without interfering transmitters.}
    \label{figure:pepegraphs}
\end{figure}

%Compared to the \gls{rssi} on the link between $n_1$ and $n_2$,

%If we assume that $n_2$ is currently listening, and the currently transmitting nodes are $nodes_t = {n_1, n_3, n_4}$. Now, we want to compute the probability for packet error on the link between nodes $n_2$ and $n_4$.

%\subsection*{Example}
%
%\begin{figure}[H]
%    \centering
%    \begin{tikzpicture}
%        \begin{scope}[xshift=4cm]
%        \node[main node] (1) {$1$};
%        \node[main node] (2) [left = 2cm  of 1]  {$2$};
%        \node[main node] (3) [below = 2cm  of 2] {$3$};
%        \node[main node] (4) [right = 2cm  of 3] {$4$};
%
%        \path[draw,thick]
%        (1) edge node[above] {$l_1$} (2)
%        (1) edge node[above=.8cm, right] {$l_2$} (3)
%        (1) edge node[right] {$l_3$} (4)
%        (2) edge node[left] {$l_4$} (3)
%        (2) edge node[below=.8cm, right] {$l_5$} (4)
%        (3) edge node[below] {$l_6$} (4)
%        ;
%        \end{scope}
%    \end{tikzpicture}
%    \caption{Sample graph \textbf{G} with 4 nodes and 6 links.}
%    \label{figure:lm-sample}
%\end{figure}
%

%The vector \vect{RSSI_{\textbf{G}}} contains the RSSI for all links in \textbf{G}. It is assumed that $thermal\_noise = -119.66$ \acrshort{db}, the $noise\_figure = 4.2$ \acrshort{db}, and $packetsize = 160$. \medbreak

%If we assume that $n_2$ is currently listening, and the currently transmitting nodes are $nodes_t = {n_1, n_3, n_4}$. Now, we want to compute the probability for packet error on the link between nodes $n_2$ and $n_4$.
%(-119.66 + 4.2)
%\begin{eq}
%    \gamma_{dB}(n_2, n_4) = RSSI(n_2, n_4) - P_{N,dB} - \mathlarger{\sum}\limits_{m \in \{n_1, n_3, n_4\}} RSSI(n_2, m)[m \neq n_4]
%\end{eq}
%
%\begin{eq}
%    \gamma_{dB}(n_2, n_4) = -74.042 - (-119.66 + 4.2) - (-64.610 + (-52.163)) = 158.191
%\end{eq}
%
%\begin{eq}
%    P_b(n_2, n_4) = \frac{1}{2}erfc \left( \sqrt{ \left( \frac{10^{\frac{158.191}{10}}}{2} \right)} \right) = 1.406 %\cdot 10^{-36}
%\end{eq}
%
%\begin{eq}
%    P_p(n_2, n_4) = 1 - \left( 1 - P_b(n_2, n_4) \right) ^{160} = 0.0
%\end{eq}

%Which equals to a probability of approximately 10 \% packet loss, with no interference, and an \gls{rssi} of $-105.3$ \acrshort{dbm}.


Special considerations are required for flow to convertible cells that can dry and rewet. When flow is between two cells that are wet, the head difference between them creates the driving force for flow. When the downstream cell is dry,
however, a reference elevation can be used instead of the downstream head to express flow between the nodes. The ``perched'' option in the Node Property Flow (NPF) Package can be used to correct downward flow to a partially saturated cell.

The ``flow correction'' option in the Multi-Aquifer Well (MAW) Package implements the same correction as the NPF package ``perched'' option and corrects the flow between a multi-aquifer well connection and a GWF cell. The approach used in the MAW Package is identical to the ``flow-to-dry-cell'' option available in \mfu~\citep{modflowusg}.

\subsection{Multi-Aquifer Well Flow Correction}

When the ``flow correction'' option is enabled, flow to a well from a GWF cell based on equation 7--50 in \cite{modflow6gwf} is

\begin{align}
	\label{eqn:mawQ}
	\mli{QMAW}_{\mli{MAW},n} = \begin{dcases}
		S_{\mli{MAW},n} C_{\mli{MAW},n}^0 \left( h_n - e_{\mli{MAW},n} \right) &  \mli{HMAW}_i < e_{\mli{MAW},n} \\
		S_{\mli{MAW},n} C_{\mli{MAW},n}^0 \left( h_n - \mli{HMAW}_{i} \right) & \mli{HMAW}_i \ge e_{\mli{MAW},n}
	\end{dcases} ,
\end{align}

\noindent where $S_{\mli{MAW},n}$ is the well screen saturation (unitless), $C_{\mli{MAW},n}^0$ is the saturated well conductance (\ulst), $h_n$ is the head in cell $n$ (\ul), $e_{\mli{MAW},n}$ is the reference elevation for the well connection (\ul), and $\mli{HMAW}_{i}$ is the head in well $i$ (\ul). The reference elevation for the well connection is defined to be

\begin{equation}
	\label{en}
	e_{\mli{MAW},n} = \begin{dcases}
		\mli{BOT}_n &  \mli{BOT}_n > \mli{BOT}_{\mli{MAW},n} \\
		\mli{BOT}_{\mli{MAW},n} &  \mli{BOT}_{\mli{MAW},n} > \mli{BOT}_n
	\end{dcases} ,
\end{equation}

\noindent where $\mli{BOT}_n$ is the bottom of cell $n$ (\ul) and $\mli{BOT}_{\mli{MAW},n}$ is the bottom of the well screen for the connection of well $i$ to cell $n$ (\ul). For the case where the head in cell $n$ is less than $e_{\mli{MAW},n}$, flow from a well to a GWF cell is

\begin{align}
	\label{eqn:mawQc2}
	\mli{QMAW}_{\mli{MAW},n} = \begin{dcases}
		S_{\mli{MAW},n} C_{\mli{MAW},n}^0 \left( e_{\mli{MAW},n} - \mli{HMAW}_{i}  \right) &  h_n < e_{\mli{MAW},n} \\
		S_{\mli{MAW},n} C_{\mli{MAW},n}^0 \left( h_n - \mli{HMAW}_{i} \right) & h_m \ge e_{\mli{MAW},n}
	\end{dcases} .
\end{align}

\subsection{Incorporation of the Flow Correction into the CVFD Groundwater Flow Equation}
Multi-aquifer well flow terms are incorporated into the CVFD flow equation \citep[eq. 6--1]{modflow6gwf} based on whether the standard or Newton-Raphson formulation is being used. The details of how the multi-aquifer well flow correction terms are incorporated into the CVFD flow equation is described below.

\subsubsection{Standard Formulation}

The terms in the standard flow equation for all groundwater cells connected to a multi-aquifer well $i$ based on \citep[eq. 7--55]{modflow6gwf} is

\begin{equation}
	\label{mawStdFlow}
	\sum\limits_{n \in \mli{MAW}_{i}}^{} S_{\mli{MAW},n} C_{\mli{MAW},n}^{0} \left( h_{n} - \mli{HMAW}_{i} \right) = 0 .
\end{equation}

\noindent To implement the flow correction, equation~\ref{eqn:mawQ} is modified to

\begin{align}
	\label{eqn:mawQfdc}
	\mli{QMAW}_{\mli{MAW},n} = S_{\mli{MAW},n} C_{\mli{MAW},n}^0 \left( h_n - \mli{HMAW}_{i} \right) + S_{\mli{MAW},n} C_{\mli{MAW},n}^0 \left( \mli{HMAW}_{i} - \overline{h}_{\mli{MAW},n} \right),
\end{align}

\noindent where $\overline{h}_{\mli{MAW},n}$ is a function that transitions between the head at the downstream node, $\mli{HMAW}_{i}$ in this case, and reference elevation $e_{\mli{MAW},n}$. The downstream head transition function used to correct the multi-aquifer flow for a connection is calculated as

\begin{align}
	\label{eqn:maw-hbar}
	\overline{h}_{\mli{MAW},n} = \begin{dcases}
		e_{\mli{MAW},n} &  h_{ds} < e_{\mli{MAW},n} \\
		h_{ds} & h_{ds} \ge e_{\mli{MAW},n}
	\end{dcases} ,
\end{align}

\noindent where $h_{ds}$ is head in the downstream node (\ul), which is the minimum of $\mli{HMAW}_{i}$ and $h_n$. The relation between the downstream head and $\overline{h}_{\mli{MAW},n}$ is shown in figure~\ref{fig:mawhbar}.

\begin{figure}[!ht]
	\begin{center}
	\includegraphics{./Figures/MAWDischargeCorrection.pdf}
	\caption[Graph showing the downstream head transition functions used to correct the multi-aquifer flow for a connection]{Graph showing the discontinuous (eq.~\ref{eqn:maw-hbar}) and continuous (eq.~\ref{eqn:maw-chbar})  downstream head transition function used to correct the multi-aquifer flow for a connection as it transitions from saturated to partially saturated conditions. The interval over which the smoothing occurs, $\epsilon$, is also shown}
	\label{fig:mawhbar}
	\end{center}
\end{figure}

Similarly, when $h_n$ is the downstream node equation~\ref{eqn:mawQc2} is modified to

\begin{align}
	\label{eqn:mawQfdc2}
	\mli{QMAW}_{\mli{MAW},n} = S_{\mli{MAW},n} C_{\mli{MAW},n}^0 \left( h_n - \mli{HMAW}_{i} \right) + S_{\mli{MAW},n} C_{\mli{MAW},n}^0 \left( \overline{h}_{\mli{MAW},n} - h_n \right),
\end{align}

The first term on the right hand side of equations~\ref{eqn:mawQfdc} and~\ref{eqn:mawQfdc2} ($S_{\mli{MAW},n} C_{\mli{MAW},n}^0$) are added to the coefficient matrix used to solve the coupled groundwater and multi-aquifer well flow equations as part of the standard MAW package formulation \citep[eq. 7--55]{modflow6gwf}. The flow correction is made by subtracting the second term on the right hand side of equations~\ref{eqn:mawQfdc} and~\ref{eqn:mawQfdc2} from the right had side of equation 6--1 in \cite{modflow6gwf}. In the case where $\mli{HMAW}_{i}$ and $h_n$ are both greater than $e_{\mli{MAW},n}$, the second term on the right hand side of equations~\ref{eqn:mawQfdc} and~\ref{eqn:mawQfdc2} is equal to zero, which results in a equation identical to equation~\ref{mawStdFlow} for a single connection.

\subsubsection{Newton-Raphson Formulation}

The modified Newton-Raphson form of equation~\ref{mawStdFlow} solved for a single connection in terms of $h$ instead of $\Delta h$  and incorporated into the Newton-Raphson form of the groundwater flow equation \citep[eq. 2--26]{modflow6gwf} is

\begin{equation}
	\label{eqn:nrMawQ}
	\begin{aligned}
		\frac{\partial \mli{QMAW}_{\mli{MAW},n}}{\partial \mli{HMAW}_i}  \mli{HMAW}_i^k + &\frac{\partial \mli{QMAW}_{\mli{MAW},n}}{\partial h_n}  h^k_n = - \mli{QMAW}_{\mli{MAW},n} + \\
		&\frac{\partial \mli{QMAW}_{\mli{MAW},n}}{\partial \mli{HMAW}_i}  \mli{HMAW}_i^{k-1} +  \frac{\partial \mli{QMAW}_{\mli{MAW},n}}{\partial h_n}  h^{k-1}_n ,
	\end{aligned}
\end{equation}

\noindent where $\mli{HMAW}_i^k$ and $h^k_n$ are the heads at the end of the current non-linear (picard) iteration and $\mli{HMAW}_i^{k-1}$ and $h^{k-1}_n$ are the head at the start of the current non-linear iteration.

When the Newton-Raphson formulation is used, discontinuous derivatives can cause non-convergence in the neighborhood of the discontinuity \citep{doi:10.1029/2006WR005195}. As a result, the discontinuous well screen saturation ($S_{\mli{MAW},n}$) in equations~\ref{eqn:mawQfdc} and~\ref{eqn:mawQfdc2} is replaced by a quadratically smoothed well screen saturation ($S_{\mli{MAW},n}^*$ -- eq. 4--5 in \cite{modflow6gwf}). The discontinuous downstream head transition function ($\overline{h}_{\mli{MAW},n}$, eq.~\ref{eqn:maw-hbar}) in equations~\ref{eqn:mawQfdc} and~\ref{eqn:mawQfdc2} is also replaced by

\begin{align}
	\label{eqn:maw-chbar}
	\overline{h}_{\mli{MAW},n}^* = \begin{dcases}
		e_{\mli{MAW},n} &  h_{ds} - e_{\mli{MAW},n} < -\epsilon \\
		\frac{( h_{ds} - e_{\mli{MAW},n} )^2}{4 \epsilon} + \frac{( h_{ds} - e_{\mli{MAW},n} )}{2} + \frac{\epsilon}{4} + e_{\mli{MAW},n}  & -\epsilon < h_{ds} - e_{\mli{MAW},n} < +\epsilon \\
		h_{ds} & h_{ds} - e_{\mli{MAW},n} \ge +\epsilon
	\end{dcases} ,
\end{align}

\noindent where $\epsilon$ is the interval over which the head transition function is smoothed. The relation between the downstream head and $\overline{h}^*_{\mli{MAW},n}$ is shown in figure~\ref{fig:mawhbar}. Simplification of equations~\ref{eqn:mawQfdc} and~\ref{eqn:mawQfdc2} results in

\begin{align}
	\label{eqn:mawQNRc}
	\mli{QMAW}_{\mli{MAW},n} = S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \left( h_n - \overline{h}^*_{\mli{MAW},n} \right)
\end{align}

\noindent and

\begin{align}
	\label{eqn:mawQNRc2}
	\mli{QMAW}_{\mli{MAW},n} = S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \left( \overline{h}^*_{\mli{MAW},n} - \mli{HMAW}_{i} \right)
\end{align}

\noindent when $\mli{HMAW}_i$ and $h_n$ are the downstream heads, respectively. The derivatives of equation~\ref{eqn:mawQNRc} with respect to $h_n$ and $\mli{HMAW}_i$ are

\begin{equation}
	\label{eqn:nrMawdQdh1}
	\frac{\partial \mli{QMAW}_{\mli{MAW},n}}{\partial \mli{HMAW}_i} =  -S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \frac{\partial \overline{h}^*_{\mli{MAW},n}}{{\partial \mli{HMAW}_i}}
\end{equation}

\noindent and

\begin{equation}
	\label{eqn:nrMawdQdhmaw1}
	\frac{\partial \mli{QMAW}_{\mli{MAW},n}}{\partial h_n} = S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 + \frac{\partial S_{\mli{MAW},n}^*}{\partial h_n} C_{\mli{MAW},n}^0 \left( h_n - \overline{h}^*_{\mli{MAW},n} \right) ,
\end{equation}

\noindent respectively. The derivatives of equation~\ref{eqn:mawQNRc2} with respect to $h_n$ and $\mli{HMAW}_i$ are

\begin{equation}
	\label{eqn:nrMawdQdh2}
	\frac{\partial \mli{QMAW}_{\mli{MAW},n}}{\partial \mli{HMAW}_i} =  -S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 + \frac{\partial S_{\mli{MAW},n}^*}{\partial \mli{HMAW}_i} C_{\mli{MAW},n}^0 \left( \overline{h}^*_{\mli{MAW},n} - \mli{HMAW}_{i} \right)
	\end{equation}

\noindent and

\begin{equation}
	\label{eqn:nrMawdQdhmaw2}
	\frac{\partial \mli{QMAW}_{\mli{MAW},n}}{\partial h_n} =  S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \frac{\partial \overline{h}^*_{\mli{MAW},n}}{\partial h_n} .
\end{equation}

The derivative of $\overline{h}^*_{\mli{MAW},n}$ with respect to the downstream head is

\begin{align}
	\label{eqn:maw-cdhbar}
	\frac{\partial \overline{h}^*_{\mli{MAW},n}}{\partial h_{ds}} = \begin{dcases}
		0 &  h_{ds} - e_{\mli{MAW},n} < -\epsilon \\
		\frac{h_{ds} - e_{\mli{MAW},n}}{2 \epsilon} + \frac{1}{2} & -\epsilon < h_{ds} - e_{\mli{MAW},n} < +\epsilon \\
		1 & h_{ds} - e_{\mli{MAW},n} \ge +\epsilon
	\end{dcases} .
\end{align}

\paragraph{Newton-Raphson formulation when $\mli{HMAW}_i$ is the Downstream Head}

The Newton-Raphson form of the equation~\ref{eqn:mawQfdc}, when $\mli{HMAW}_i$ is the downstream head, is formulated by substituting equations~\ref{eqn:mawQNRc}, ~\ref{eqn:nrMawdQdh1}, and ~\ref{eqn:nrMawdQdhmaw1} into equation~\ref{eqn:nrMawQ}, which results in

\begin{equation}
	\label{eqn:nrMawQmawds01}
	\begin{aligned}
		\biggl[ -S_{\mli{MAW},n}^* &C_{\mli{MAW},n}^0 \frac{\partial \overline{h}^*_{\mli{MAW},n}}{{\partial \mli{HMAW}_i}}
 \biggr] \mli{HMAW}_i^k + \\
 		&\biggl[ S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 + \frac{\partial S_{\mli{MAW},n}^*}{\partial h_n} C_{\mli{MAW},n}^0 \left( h_n^{k-1} - \overline{h}_{\mli{MAW},n}^{*k-1} \right) \biggr]  h^k_n = \\
		&-S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \left( h_n^{k-1} - \overline{h}_{\mli{MAW},n}^{*k-1} \right) + \\
		&\biggl[ -S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \frac{\partial \overline{h}^*_{\mli{MAW},n}}{{\partial \mli{HMAW}_i}}
 \biggr] \mli{HMAW}_i^{k-1} + \\
 		&\biggl[ S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 + \frac{\partial S_{\mli{MAW},n}^*}{\partial h_n} C_{\mli{MAW},n}^0 \left( h_n^{k-1} - \overline{h}_{\mli{MAW},n}^{*k-1} \right) \biggr]  h^{k-1}_n .
	\end{aligned}
\end{equation}

\noindent Simplifying equation~\ref{eqn:nrMawQmawds01} results in

\begin{equation}
	\label{eqn:nrMawQmawds02}
	\begin{aligned}
		\biggl[ -S_{\mli{MAW},n}^* &C_{\mli{MAW},n}^0 \frac{\partial \overline{h}^*_{\mli{MAW},n}}{{\partial \mli{HMAW}_i}}
 \biggr] \mli{HMAW}_i^k + \\
 		&\biggl[ S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 + \frac{\partial S_{\mli{MAW},n}^*}{\partial h_n} C_{\mli{MAW},n}^0 \left( h_n^{k-1} - \overline{h}_{\mli{MAW},n}^{*k-1} \right) \biggr]  h^k_n = \\
		&S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \overline{h}_{\mli{MAW},n}^{*k-1} + \biggl[ -S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \frac{\partial \overline{h}^*_{\mli{MAW},n}}{{\partial \mli{HMAW}_i}}
 \biggr] \mli{HMAW}_i^{k-1} + \\
 		&\biggl[ \frac{\partial S_{\mli{MAW},n}^*}{\partial h_n} C_{\mli{MAW},n}^0 \left( h_n^{k-1} - \overline{h}_{\mli{MAW},n}^{*k-1} \right) \biggr]  h^{k-1}_n .
	\end{aligned}
\end{equation}

\noindent To complete the Newton-Raphson formulation the terms added to the coefficient matrix and right-hand side during the standard formulation step (eq.~\ref{eqn:mawQfdc}) are modified by adding

\begin{equation}
	\label{eqn:nrMawdsdiag}
	S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \left( 1 - \frac{\partial \overline{h}^*_{\mli{MAW},n}}{{\partial \mli{HMAW}_i}} \right)
\end{equation}

\noindent to the diagonal position in the row for multi-aquifer well $i$, adding

\begin{equation}
	\label{eqn:nrMawdsoffd}
	\frac{\partial S_{\mli{MAW},n}^*}{\partial h_n} C_{\mli{MAW},n}^0 \left( h_n - \overline{h}^*_{\mli{MAW},n} \right)
\end{equation}

\noindent to the off-diagonal position for GWF cell $n$ in the row for multi-aquifer well $i$, and adding

\begin{equation}
	\label{eqn:nrMawdsrhs}
	S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \left( 1 - \frac{\partial \overline{h}^*_{\mli{MAW},n}}{{\partial \mli{HMAW}_i}} \right) \mli{HMAW}_i^{k-1} + \frac{\partial S_{\mli{MAW},n}^*}{\partial h_n} C_{\mli{MAW},n}^0 \left( h_n - \overline{h}^*_{\mli{MAW},n} \right) h_n^{k-1}
\end{equation}

\noindent to the right-hand side in the row for multi-aquifer well $i$. The $\left( 1 - \frac{\partial \overline{h}^*_{\mli{MAW},n}}{{\partial \mli{HMAW}_i}} \right)$ term in equation~\ref{eqn:nrMawdsdiag} removes the $-S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0$ term added during standard formulation step and correctly adds the $-S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \frac{\partial \overline{h}^*_{\mli{MAW},n}}{{\partial \mli{HMAW}_i}}$ term. The row for the connected groundwater cell (row $n$) is also modified by subtracting equations~\ref{eqn:nrMawdsdiag}, ~\ref{eqn:nrMawdsoffd}, and~\ref{eqn:nrMawdsrhs} from the off-diagonal (column corresponding to multi-aquifer well $i$), diagonal, and right-hand side, respectively.

\paragraph{Newton-Raphson formulation when $h_n$ is the Downstream Head}

The Newton-Raphson form of the equation~\ref{eqn:mawQfdc2}, when $h_n$ is the downstream head, is formulated by substituting equations ~\ref{eqn:mawQNRc2}, ~\ref{eqn:nrMawdQdh2}, and ~\ref{eqn:nrMawdQdhmaw2} into equation~\ref{eqn:nrMawQ}, which results in

\begin{equation}
	\label{eqn:nrMawQgwfds01}
	\begin{aligned}
		\biggl[ -S_{\mli{MAW},n}^* &C_{\mli{MAW},n}^0 + \frac{\partial S_{\mli{MAW},n}^*}{\partial \mli{HMAW}_i} C_{\mli{MAW},n}^0 \left( \overline{h}_{\mli{MAW},n}^{*k-1} - \mli{HMAW}_{i}^{k-1} \right)  \biggr] \mli{HMAW}_i^k + \\
		&\biggl[ S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \frac{\partial \overline{h}^*_{\mli{MAW},n}}{\partial h_n} \biggr]  h^k_n = - S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \left( \overline{h}_{\mli{MAW},n}^{*k-1} - \mli{HMAW}_{i}^{k-1} \right) + \\
		&\biggl[ -S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 + \frac{\partial S_{\mli{MAW},n}^*}{\partial \mli{HMAW}_i} C_{\mli{MAW},n}^0 \left( \overline{h}_{\mli{MAW},n}^{*k-1} - \mli{HMAW}_{i}^{k-1} \right) \biggr] \mli{HMAW}_i^{k-1} + \\ &
		\biggl[ S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \frac{\partial \overline{h}^*_{\mli{MAW},n}}{\partial h_n} \biggr]  h^{k-1}_n .
	\end{aligned}
\end{equation}

\noindent Simplifying equation~\ref{eqn:nrMawQgwfds01} results in

\begin{equation}
	\label{eqn:nrMawQgwfds02}
	\begin{aligned}
		\biggl[ -S_{\mli{MAW},n}^* &C_{\mli{MAW},n}^0 + \frac{\partial S_{\mli{MAW},n}^*}{\partial \mli{HMAW}_i} C_{\mli{MAW},n}^0 \left( \overline{h}_{\mli{MAW},n}^{*k-1} - \mli{HMAW}_{i}^{k-1} \right)  \biggr] \mli{HMAW}_i^k + \\
		&\biggl[ S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \frac{\partial \overline{h}^*_{\mli{MAW},n}}{\partial h_n} \biggr]  h^k_n = - S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \overline{h}_{\mli{MAW},n}^{*k-1} + \\
		&\biggl[ \frac{\partial S_{\mli{MAW},n}^*}{\partial \mli{HMAW}_i} C_{\mli{MAW},n}^0 \left( \overline{h}_{\mli{MAW},n}^{*k-1} - \mli{HMAW}_{i}^{k-1} \right) \biggr] \mli{HMAW}_i^{k-1} + \\ &
		\biggl[ S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \frac{\partial \overline{h}^*_{\mli{MAW},n}}{\partial h_n} \biggr]  h^{k-1}_n .
	\end{aligned}
\end{equation}

\noindent To complete the Newton-Raphson formulation the terms added to the coefficient matrix and right-hand side during the standard formulation step (eq.~\ref{eqn:mawQfdc2}) are modified by adding

\begin{equation}
	\label{eqn:nrGwfdsdiag}
	\frac{\partial S_{\mli{MAW},n}^*}{\partial \mli{HMAW}_i} C_{\mli{MAW},n}^0 \left( \overline{h}_{\mli{MAW},n}^{*k-1} - \mli{HMAW}_{i}^{k-1} \right)
\end{equation}

\noindent to the diagonal position in the row for multi-aquifer well $i$, adding

\begin{equation}
	\label{eqn:nrGwfdsoffd}
	S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \left( \frac{\partial \overline{h}^*_{\mli{MAW},n}}{\partial h_n} - 1\right)
\end{equation}

\noindent to the off-diagonal position for GWF cell $n$ in the row for multi-aquifer well $i$, and adding

\begin{equation}
	\label{eqn:nrGwfdsrhs}
	\frac{\partial S_{\mli{MAW},n}^*}{\partial \mli{HMAW}_i} C_{\mli{MAW},n}^0 \left( \overline{h}_{\mli{MAW},n}^{*k-1} - \mli{HMAW}_{i}^{k-1} \right) \mli{HMAW}_i^{k-1} + S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \left( \frac{\partial \overline{h}^*_{\mli{MAW},n}}{\partial h_n} - 1 \right) h^{k-1}_n
\end{equation}

\noindent to the right-hand side in the row for multi-aquifer well $i$. The $\left( \frac{\partial \overline{h}^*_{\mli{MAW},n}}{\partial h_n} - 1 \right)$ term in equation~\ref{eqn:nrGwfdsoffd} removes the $S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0$ term added during standard formulation step and correctly adds the $S_{\mli{MAW},n}^* C_{\mli{MAW},n}^0 \frac{\partial \overline{h}^*_{\mli{MAW},n}}{{\partial h_n}}$ term. The row for the connected groundwater cell (row $n$) is also modified by subtracting equations~\ref{eqn:nrGwfdsdiag}, ~\ref{eqn:nrGwfdsoffd}, and~\ref{eqn:nrGwfdsrhs} from the off-diagonal (column corresponding to multi-aquifer well $i$), diagonal, and right-hand side, respectively.