\documentclass[english]{../thermomemo/thermomemo}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{array}% improves tabular environment.
\usepackage{dcolumn}% also improves tabular environment, with decimal centring.
\usepackage{booktabs}
\usepackage{todonotes}
\usepackage{subcaption,caption}
\usepackage{xspace}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{snakes}
\usepackage{verbatim}
\usepackage{hyperref}
\usepackage{xcolor}
\hypersetup{
  colorlinks,
  linkcolor={red!50!black},
  citecolor={blue!50!black},
  urlcolor={blue!80!black}
}
%
% Egendefinerte
%
% Kolonnetyper for array.sty:
\newcolumntype{C}{>{$}c<{$}}
\newcolumntype{L}{>{$}l<{$}}
%
\newcommand*{\unit}[1]{\ensuremath{\,\mathrm{#1}}}
\newcommand*{\uunit}[1]{\ensuremath{\mathrm{#1}}}
%\newcommand*{\od}[3][]{\frac{\mathrm{d}^{#1}#2}{\mathrm{d}{#3}^{#1}}}% ordinary derivative
\newcommand*{\od}[3][]{\frac{\dif^{#1}#2}{\dif{#3}^{#1}}}% ordinary derivative
\newcommand*{\pd}[3][]{\frac{\partial^{#1}#2}{\partial{#3}^{#1}}}% partial derivative
\newcommand*{\pdc}[3]{\frac{\partial^{2}#1}{\partial{#2}\partial{#3}}}% partial derivative
\newcommand*{\pdt}[3][]{{\partial^{#1}#2}/{\partial{#3}^{#1}}}% partial
                                % derivative for inline use.
\newcommand{\pone}[3]{\frac{\partial #1}{\partial #2}_{#3}}% partial
                                % derivative with information of
                                % constant variables
\newcommand{\ponel}[3]{\frac{\partial #1}{\partial #2}\bigg|_{#3}} % partial derivative with informatio of constant variable. A line is added.
\newcommand{\ptwo}[3]{\frac{\partial^{2} #1}{\partial #2 \partial
    #3}} % partial differential in two different variables
\newcommand{\pdn}[3]{\frac{\partial^{#1}#2}{\partial{#3}^{#1}}}% partial derivative

% Total derivative:
\newcommand*{\ttd}[2]{\frac{\mathrm{D} #1}{\mathrm{D} #2}}
\newcommand*{\td}[2]{\frac{\mathrm{d} #1}{\mathrm{d} #2}}
\newcommand*{\ddt}{\frac{\partial}{\partial t}}
\newcommand*{\ddx}{\frac{\partial}{\partial x}}
% Vectors etc:
% For Computer Modern:

\DeclareMathAlphabet{\mathsfsl}{OT1}{cmss}{m}{sl}
\renewcommand*{\vec}[1]{\boldsymbol{#1}}%
\newcommand*{\vektor}[1]{\boldsymbol{#1}}%
\newcommand*{\tensor}[1]{\mathsfsl{#1}}% 2. order tensor
\newcommand*{\matr}[1]{\tensor{#1}}% matrix
\renewcommand*{\div}{\boldsymbol{\nabla\cdot}}% divergence
\newcommand*{\grad}{\boldsymbol{\nabla}}% gradient
% fancy differential from Claudio Beccari, TUGboat:
% adjusts spacing automatically
\makeatletter
\newcommand*{\dif}{\@ifnextchar^{\DIfF}{\DIfF^{}}}
\def\DIfF^#1{\mathop{\mathrm{\mathstrut d}}\nolimits^{#1}\gobblesp@ce}
\def\gobblesp@ce{\futurelet\diffarg\opsp@ce}
\def\opsp@ce{%
  \let\DiffSpace\!%
  \ifx\diffarg(%
    \let\DiffSpace\relax
  \else
    \ifx\diffarg[%
      \let\DiffSpace\relax
    \else
      \ifx\diffarg\{%
        \let\DiffSpace\relax
      \fi\fi\fi\DiffSpace}
\makeatother
%
\newcommand*{\me}{\mathrm{e}}% e is not a variable (2.718281828...)
%\newcommand*{\mi}{\mathrm{i}}%  nor i (\sqrt{-1})
\newcommand*{\mpi}{\uppi}% nor pi (3.141592...) (works for for Lucida)
%
% lav tekst-indeks/subscript/pedex
\newcommand*{\ped}[1]{\ensuremath{_{\text{#1}}}}
\newcommand*{\ap}[1]{\ensuremath{^{\text{#1}}}}
\newcommand*{\apr}[1]{\ensuremath{^{\mathrm{#1}}}}
\newcommand*{\pedr}[1]{\ensuremath{_{\mathrm{#1}}}}
%
\newcommand*{\volfrac}{\alpha}% volume fraction
\newcommand*{\surften}{\sigma}% coeff. of surface tension
\newcommand*{\curv}{\kappa}% curvature
\newcommand*{\ls}{\phi}% level-set function
\newcommand*{\ep}{\Phi}% electric potential
\newcommand*{\perm}{\varepsilon}% electric permittivity
\newcommand*{\visc}{\mu}% molecular (dymamic) viscosity
\newcommand*{\kvisc}{\nu}% kinematic viscosity
\newcommand*{\cfl}{C}% CFL number

\newcommand*{\cons}{\vec U}
\newcommand*{\flux}{\vec F}
\newcommand*{\dens}{\rho}
\newcommand*{\svol}{\ensuremath v}
\newcommand*{\temp}{\ensuremath T}
\newcommand*{\vel}{\ensuremath u}
\newcommand*{\mom}{\dens\vel}
\newcommand*{\toten}{\ensuremath E}
\newcommand*{\inten}{\ensuremath e}
\newcommand*{\press}{\ensuremath p}
\renewcommand*{\ss}{\ensuremath a}
\newcommand*{\jac}{\matr A}
%
\newcommand*{\abs}[1]{\lvert#1\rvert}
\newcommand*{\bigabs}[1]{\bigl\lvert#1\bigr\rvert}
\newcommand*{\biggabs}[1]{\biggl\lvert#1\biggr\rvert}
\newcommand*{\norm}[1]{\lVert#1\rVert}
%
\newcommand*{\e}[1]{\times 10^{#1}}
\newcommand*{\ex}[1]{\times 10^{#1}}%shorthand -- for use e.g. in tables
\newcommand*{\exi}[1]{10^{#1}}%shorthand -- for use e.g. in tables
\newcommand*{\nondim}[1]{\ensuremath{\mathit{#1}}}% italic iflg. ISO. (???)
\newcommand*{\rey}{\nondim{Re}}
\newcommand*{\acro}[1]{\textsc{\MakeLowercase{#1}}}%acronyms etc.
\newcommand*{\ousum}[2]{\overset{#1}{\underset{#2}{\sum}}}

\newcommand{\nto}{\ensuremath{\mbox{N}_{\mbox{\scriptsize 2}}}}
\newcommand{\chfire}{\ensuremath{\mbox{CH}_{\mbox{\scriptsize 4}}}}
%\newcommand*{\checked}{\ding{51}}
\newcommand{\coto}{\ensuremath{\text{CO}_{\text{\scriptsize 2}}}}
\newcommand{\celsius}{\ensuremath{^\circ\text{C}}}
\newcommand{\clap}{Clapeyron~}
\newcommand{\subl}{\ensuremath{\text{sub}}}
\newcommand{\spec}{\text{spec}}
\newcommand{\sat}{\text{sat}}
\newcommand{\sol}{\text{sol}}
\newcommand{\liq}{\text{liq}}
\newcommand{\vap}{\text{vap}}
\newcommand{\amb}{\text{amb}}
\newcommand{\tr}{\text{tr}}
\newcommand{\crit}{\text{crit}}
\newcommand{\entr}{\ensuremath{\text{s}}}
\newcommand{\fus}{\text{fus}}
\newcommand{\flash}[1]{\ensuremath{#1\text{-flash}}}
\newcommand{\spce}[2]{\ensuremath{#1\, #2\text{ space}}}
\newcommand{\spanwagner}{\text{Span--Wagner}}
\newcommand{\triplepoint}{\text{TP triple point}}
\newcommand{\wrpt}{\text{with respect to}\xspace}
\newcommand{\excess}{\text{E}\xspace}
\newcommand{\comb}{\text{comb}\xspace}
\newcommand{\FH}{\text{FH}\xspace}
\newcommand{\SG}{\text{SG}\xspace}
\newcommand{\NC}{\text{NC}\xspace}
\newcommand{\NGr}{\text{NG}\xspace}
\newcommand{\res}{\text{R}\xspace}

\title{UNIFAC excess gibbs mixing rules}
\author{Morten Hammer}

\graphicspath{{gfx/}}

\begin{document}
\frontmatter
\tableofcontents
\section{Introduction}
The UNIFAC (\textit{UNI}QUAC \textit{F}unctional-group \textit{A}ctivity
\textit{C}oefficients) model \cite{Fredenslund1975}, is a group
contribution model, and a further development of the UNIQUAC model
\cite{Abrams1975}. Being a group contribution model, it accounts for
molecular groups like $\text{C-H}_2$ and $\text{C-H}_3$,
that can be thought upon as monomers in a polymer.

The UNIFAC excess Gibbs mixing rule have found application in the
predictive SRK, PSRK \cite{Holderbaum1991}, model, and VTPR
\cite{Collinet2006}. It is also used as the universal mixing rule
(UMR) \cite{Voutsas2004} togther with t-mPR
\cite{Magoulas1990,Avlonitis1994}. The combined model is denoted
UMR-PR.

\section{UNIFAC model}
The UNIFAC model \cite{Fredenslund1975} is given as follows,
\begin{equation}
  \frac{A^\res}{RT} = \frac{A^\excess}{RT} - \frac{A^\excess}{RT_0} = - \ousum{\NC}{i} n_i \ousum{\NGr}{k}  v_k^i Q_k(\Lambda_k - \Lambda_k^i).
  \label{eq:Ae}
\end{equation}
The symbols and formalism of Michelsen \cite{Michelsen2007} is
used. $A^\excess/(RT_0)$ is the combinatorial term and is described in
a later sub section. It is assumed that $A^\excess = G^\excess$.

The different symbols are defined as follows,
\begin{align}
  \Lambda_k &= \ln \ousum{\NGr}{j}  \Theta_j \tilde{E}_{jk},\label{eq:Lambda_k}\\
  \Lambda_k^i &= \ln \ousum{\NGr}{j}  \Theta_j^i \tilde{E}_{jk}, \label{eq:Lambda_ki}\\
  \tilde{E}_{jk} &= \exp \left(-\frac{\tilde{U}_{jk}}{RT}\right), \label{eq:E_jk}\\
  \Theta_j &= \frac{Q_j \ousum{\NC}{l}n_lv_j^l}{\ousum{\NC}{l}n_l\ousum{\NGr}{m}v_m^lQ_m}, \label{eq:Theta_j}\\
  \Theta_j^i &= \frac{Q_j v_j^i}{\ousum{\NGr}{k}v_k^iQ_k}. \label{eq:Theta_ji}
\end{align}
Here $Q_k$ is the group surface area of group $k$, and $v_k^i$ is the
number of groups $k$ in molecule $i$. Both $Q_k$ and $v_k^i$ are
constants. $\tilde{U}_{jk}$ is the interaction energy per unit surface
area of the $j-k$ group interaction. $\tilde{U}_{jk}$ can be a
constant, or a temperature function.

\subsection{Differentials}
Differentiating \ref{eq:Ae} \wrpt $n_\alpha$ we get,
\begin{equation}
  \frac{1}{RT}\pd{A^\res}{n_\alpha} = \frac{A^\res_\alpha}{RT} = - \ousum{\NGr}{k}  v_k^\alpha Q_k(\Lambda_k - \Lambda_k^\alpha) - \ousum{\NC}{i} n_i \ousum{\NGr}{k}  v_k^i Q_k \pd{\Lambda_k}{n_\alpha}.
  \label{eq:dAedna}
\end{equation}
Michelsen \cite[Chap.~5,Eq.~56]{Michelsen2007} show that
\begin{equation}
  \ousum{\NC}{i} n_i \ousum{\NGr}{k}  v_k^i Q_k \pd{\Lambda_k}{n_\alpha} = \ousum{\NGr}{j}v_j^\alpha Q_j\left(\ousum{\NGr}{k}\frac{\Theta_j \tilde{E}_{jk}}{\ousum{\NGr}{l}\Theta_l \tilde{E}_{lk}} -1\right).
  \label{eq:sumdLambdadna}
\end{equation}
But since second differentials are required, it do not help much for
the compositional differentials. Using
\begin{equation}
  \Lambda_k = \ln \ousum{\NGr}{j}  \Theta_j \tilde{E}_{jk} = \ln \ousum{\NC}{l}n_l \ousum{\NGr}{j} v_j^l Q_j \tilde{E}_{jk} -\ln \ousum{\NC}{l}n_l\ousum{\NGr}{m}v_m^lQ_m,
\end{equation}
we get
\begin{equation}
  \pd{\Lambda_k}{n_\alpha} = \frac{\ousum{\NGr}{j} v_j^\alpha Q_j \tilde{E}_{jk}}{\ousum{\NC}{l}n_l \ousum{\NGr}{j} v_j^l Q_j \tilde{E}_{jk}} - \frac{\ousum{\NGr}{m}v_m^\alpha Q_m}{\ousum{\NC}{l}n_l\ousum{\NGr}{m}v_m^lQ_m}.
  \label{eq:dLambdadna}
\end{equation}
Differentiating \ref{eq:dAedna} further \wrpt $n_\beta$ we get,
\begin{align}
  \frac{A^\res_{\alpha\beta}}{RT} &= - \ousum{\NGr}{k}  Q_k \left(v_k^\alpha  \pd{\Lambda_k}{n_\beta} + v_k^\beta \pd{\Lambda_k}{n_\alpha}\right) - \ousum{\NC}{i} n_i \ousum{\NGr}{k}  v_k^i Q_k \ptwo{\Lambda_k}{n_\alpha}{n_\beta}, \label{eq:d2Aedna2}\\
&= - \ousum{\NGr}{k}  Q_k \left(v_k^\alpha  \pd{\Lambda_k}{n_\beta} + v_k^\beta \pd{\Lambda_k}{n_\alpha}\right) - \ousum{\NGr}{k}  \left(\ousum{\NC}{i} n_i v_k^i\right) Q_k \ptwo{\Lambda_k}{n_\alpha}{n_\beta}.
\end{align}
Differentiating Equation \ref{eq:dLambdadna} we get the second differential of $\Lambda_k$,
\begin{equation}
  \ptwo{\Lambda_k}{n_\alpha}{n_\beta} = -\frac{\left(\ousum{\NGr}{j} v_j^\alpha Q_j \tilde{E}_{jk}\right) \left(\ousum{\NGr}{j} v_j^\beta Q_j \tilde{E}_{jk}\right)}{\left(\ousum{\NC}{l}n_l \ousum{\NGr}{j} v_j^l Q_j \tilde{E}_{jk}\right)^2} + \frac{\left(\ousum{\NGr}{m}v_m^\alpha Q_m\right) \left(\ousum{\NGr}{m}v_m^\beta Q_m\right)}{\left(\ousum{\NC}{l}n_l\ousum{\NGr}{m}v_m^lQ_m\right)^2}.
  \label{eq:ddLambdadnadnb}
\end{equation}
We immediately see that \ref{eq:ddLambdadnadnb} give a symmetric matrix of the second differentials.

Differentiating \ref{eq:Ae} \wrpt $T$ we get,
\begin{align}
  \pd{\left(\frac{A^\res}{RT}\right)}{T} &= - \ousum{\NC}{i} n_i \ousum{\NGr}{k}  v_k^i Q_k\left(\pd{\Lambda_k}{T} - \pd{\Lambda_k^i}{T}\right), \label{eq:dAedT} \\
  \pdn{2}{\left(\frac{A^\res}{RT}\right)}{T} &= - \ousum{\NC}{i} n_i \ousum{\NGr}{k}  v_k^i Q_k\left(\pdn{2}{\Lambda_k}{T} - \pdn{2}{\Lambda_k^i}{T}\right). \label{eq:d2AedT2}
\end{align}
Here,
\begin{align}
  \pd{\Lambda_k}{T} &= \frac{\ousum{\NC}{l}n_l \ousum{\NGr}{j} v_j^l Q_j \pd{\tilde{E}_{jk}}{T}}{\ousum{\NC}{l}n_l \ousum{\NGr}{j} v_j^l Q_j \tilde{E}_{jk}},\label{eq:dLkdT} \\
  \pd{\Lambda_k^i}{T} &= \frac{\ousum{\NGr}{j} Q_j v_j^i \pd{\tilde{E}_{jk}}{T}}{\ousum{\NGr}{j} Q_j v_j^i \tilde{E}_{jk}},\label{eq:dLikdT} \\
  \pdn{2}{\Lambda_k}{T} &= \frac{\ousum{\NC}{l}n_l \ousum{\NGr}{j} v_j^l Q_j \pdn{2}{\tilde{E}_{jk}}{T}}{\ousum{\NC}{l}n_l \ousum{\NGr}{j} v_j^l Q_j \tilde{E}_{jk}} - \left(\pd{\Lambda_k}{T}\right)^2,\label{eq:d2LkdT2} \\
  \pdn{2}{\Lambda_k^i}{T} &= \frac{\ousum{\NGr}{j} Q_j v_j^i \pdn{2}{\tilde{E}_{jk}}{T}}{\ousum{\NGr}{j} Q_j v_j^i \tilde{E}_{jk}} - \left(\pd{\Lambda_k^i}{T}\right)^2.\label{eq:d2LikdT2} \\
\end{align}

Differentiating Equation \ref{eq:dAedna} we get
\begin{equation}
  \pd{\left(\frac{A^\res_\alpha}{RT}\right)}{T} = - \ousum{\NGr}{k}  v_k^\alpha Q_k\left(\pd{\Lambda_k}{T} - \pd{\Lambda_k^\alpha}{T}\right) -  \ousum{\NGr}{k} \left(\ousum{\NC}{i} n_i v_k^i\right) Q_k \ptwo{\Lambda_k}{n_\alpha}{T}. \label{eq:d2AednadT}
\end{equation}
The cross differential of $\Lambda_k$ is found by differentiating Equation \ref{eq:dLambdadna} \wrpt $T$,
\begin{equation}
  \ptwo{\Lambda_k}{n_\alpha}{T} = \frac{\ousum{\NGr}{j} v_j^\alpha Q_j \pd{\tilde{E}_{jk}}{T}}{\ousum{\NC}{l}n_l \ousum{\NGr}{j} v_j^l Q_j \tilde{E}_{jk}} - \frac{\left(\ousum{\NGr}{j} v_j^\alpha Q_j \tilde{E}_{jk}\right) \left(\ousum{\NC}{l}n_l \ousum{\NGr}{j} v_j^l Q_j \pd{\tilde{E}_{jk}}{T}\right)}{\left(\ousum{\NC}{l}n_l \ousum{\NGr}{j} v_j^l Q_j \tilde{E}_{jk}\right)^2} .
  \label{eq:d2LambdadnadT}
\end{equation}
\subsection{The combinatorial term}
The combinatorial term is comprised of a Flory-Huggins (\FH) and a
Staverman-Guggenheim (\SG) contribution,

\begin{align}
  G^{\excess,\comb} &= G^{\excess,\FH} + G^{\excess,\SG},\label{eq:comb}\\
  G^{\excess,\FH} &= \underset{i}{\sum} x_i \ln \frac{\phi_i}{x_i}, \label{eq:fh}\\
  G^{\excess,\SG} &= \frac{z}{2} \underset{i}{\sum} x_i q_i \ln \frac{\theta_i}{\phi_i}\label{eq:sg}.
\end{align}
Where $z=10$,
\begin{align}
  \phi_i &= \frac{x_i r_i}{\underset{j}{\sum} x_j r_j},\label{eq:phii}\\
  \theta_i &= \frac{x_i q_i}{\underset{j}{\sum} x_j q_j}.\label{eq:thetai}\\
\end{align}
$r_i$ and $q_i$ are molecule paramaters and non of the parameters are temperature dependent. $r_i$ is the molecular van der Waals volume and $q_i$ is the molecular van der Waals surface area. They are calculated from the group paramaters as follows,
\begin{align}
  r_i &= \ousum{\NGr}{k}v_k^i R_k,\label{eq:ri}\\
  q_i &= \ousum{\NGr}{k}v_k^i Q_k.\label{eq:qi}\\
\end{align}

\subsubsection{Differentials of the Flory-Huggins combinatorial term}
Writing Equation \ref{eq:fh} as a function of mole numbers, we get,
\begin{align}
  G^{\excess,\FH} &= \ousum{\NC}{i} n_i \left(\ln \phi_i - \ln n_i + \ln \ousum{\NC}{j}n_j\right), \\
  &= \ousum{\NC}{i} n_i \left(\ln n_i r_i - \ln \ousum{\NC}{j} n_j r_j - \ln n_i + \ln \ousum{\NC}{j}n_j\right) = \ousum{\NC}{i} n_i \ln r_i -n\ln \ousum{\NC}{j}n_jr_j  + n\ln n. \label{eq:fhn}
\end{align}
Differentiating $G^{\excess,\FH}$ \wrpt $n_\alpha$ we get,
\begin{align}
  G^{\excess,\FH}_\alpha &= \ln r_\alpha -\ln \ousum{\NC}{j}n_jr_j  + \ln \ousum{\NC}{j}n_j + 1-\frac{r_\alpha\ousum{\NC}{i} n_i}{\ousum{\NC}{j}n_jr_j},\\
  &= \ln r_\alpha -\ln \ousum{\NC}{j}n_jr_j  + \ln n + 1-\frac{n r_\alpha}{\ousum{\NC}{j}n_jr_j}, \label{eq:dfhndna}\\
  &= \ln \left(\frac{n r_\alpha}{\ousum{\NC}{j}n_jr_j}\right) + 1-\frac{n r_\alpha}{\ousum{\NC}{j}n_jr_j}\\
  &= \ln \left(\frac{\phi_\alpha}{x_\alpha}\right) + 1-\frac{\phi_\alpha}{x_\alpha}
\end{align}
Differentiating \ref{eq:dfhndna} \wrpt $n_\beta$ we get,
\begin{align}
  G^{\excess,\FH}_{\alpha\beta} &= -\frac{r_\alpha + r_\beta}{\ousum{\NC}{j}n_jr_j}  + \frac{1}{n} + \frac{n r_\alpha r_\beta}{\left(\ousum{\NC}{j}n_jr_j\right)^2}. \label{eq:d2fhndnadnb}
\end{align}
\subsubsection{Differentials of the Staverman-Guggenheim combinatorial term}
Writing Equation \ref{eq:sg} as a function of mole numbers, we get,
\begin{align}
  G^{\excess,\SG} &= \frac{z}{2} \ousum{\NC}{i} n_i q_i \left(\ln \theta_i - \ln \phi_i\right),\\
  &= \frac{z}{2} \ousum{\NC}{i} n_i q_i \left(\ln \frac{q_i}{r_i} - \ln \ousum{\NC}{j} n_j q_j + \ln \ousum{\NC}{j} n_j r_j \right)\label{eq:sgn}.
\end{align}
Differentiating $G^{\excess,\SG}$ \wrpt $n_\alpha$ we get,
\begin{align}
  G^{\excess,\SG}_\alpha &= \frac{z}{2} q_\alpha \left( \ln \frac{q_\alpha}{r_\alpha} - \ln \ousum{\NC}{j} n_j q_j + \ln \ousum{\NC}{j} n_j r_j - 1 + \frac{r_\alpha \ousum{\NC}{i} n_i q_i}{q_\alpha\ousum{\NC}{j} n_j r_j}\right), \label{eq:dsgnda}\\
  &= \frac{z}{2} q_\alpha \left( - \ln \left(\frac{r_\alpha \ousum{\NC}{j} n_j q_j}{q_\alpha \ousum{\NC}{j} n_j r_j}\right) - 1 + \frac{r_\alpha \ousum{\NC}{i} n_i q_i}{q_\alpha\ousum{\NC}{j} n_j r_j}\right),\\
  &= \frac{z}{2} q_\alpha \left( \ln \left(\frac{\theta_\alpha}{\phi_\alpha}\right) - 1 + \frac{\phi_\alpha}{\theta_\alpha}\right).
\end{align}
Differentiating \ref{eq:dsgnda} \wrpt $n_\beta$ we get,
\begin{align}
  G^{\excess,\SG}_{\alpha\beta} &= \frac{z}{2} \left( - \frac{q_\alpha q_\beta}{\ousum{\NC}{j} n_j q_j} + \frac{q_\alpha r_\beta + q_\beta r_\alpha}{\ousum{\NC}{j} n_j r_j} - \frac{r_\alpha r_\beta \ousum{\NC}{i} n_i q_i}{\left(\ousum{\NC}{j} n_j r_j\right)^2}\right). \label{eq:dssgndadb}
\end{align}
\subsubsection{Comparing to combinatorial activity coefficient of Fredenslund et al.}
Fredenslund et al. \cite{Fredenslund1975} uses the following expression for the activity combinatorial coefficient,
\begin{align}
  \ln \gamma_{\text{c}} &= \ln \frac{\phi_i}{x_i} + \frac{z}{2} q_i \ln \frac{\theta_i}{\phi_i} + l_i - \frac{\phi_i}{x_i}\ousum{\NC}{j} x_j l_j , \label{eq:gamma_c} \\
  l_i &= \frac{z}{2} \left(r_i - q_i\right) - \left(r_i - 1\right) . \label{eq:li}
\end{align}
Inserting for $l_i$ in the last term of Equation \ref{eq:gamma_c}, we get,
\begin{align}
  \frac{\phi_i}{x_i}\ousum{\NC}{j} x_j l_j &= \frac{z\phi_i}{2x_i}\left(\ousum{\NC}{j} x_j r_j - \ousum{\NC}{j}x_jq_j\right) - \frac{\phi_i}{x_i}\left(\ousum{\NC}{j} x_j r_j - 1\right), \\
  &= \frac{z}{2}\left(r_i - \frac{q_i\phi_i}{\theta_i}\right) - r_i + \frac{\phi_i}{x_i}. \label{eq:second_term}
\end{align}
Inserting Equation \ref{eq:second_term} and Equation \ref{eq:li} into Equation \ref{eq:gamma_c} we get,
\begin{align}
  \ln \gamma_{\text{c}} &= \ln \frac{\phi_i}{x_i} + 1 - \frac{\phi_i}{x_i} + \frac{z}{2} q_i \left(\ln \frac{\theta_i}{\phi_i} -1 + \frac{\phi_i}{\theta_i}\right).
\end{align}
We see that
\begin{equation}
  \ln \gamma_{\text{c}} = G^{\excess,\FH}_\alpha + G^{\excess,\SG}_\alpha.
\end{equation}
\section{UMR-PR model}
The UMR-PR model is developed by Voutsas et al \cite{Voutsas2004}, and
uses the UNIFAC mixing rules together with a volume translated
Peng-Robinson EOS, t-mPR \cite{Avlonitis1994}.

UMR-PR applies the following covolume mixing rule,
\begin{align}
  b &= \underset{i}{\sum} \underset{j}{\sum} x_i x_j b_{ij},\\
  b_{ij} &= \left[\frac{b_i^{\frac{1}{s}} + b_j^{\frac{1}{s}}}{2}\right]^s,
  \label{eq:bij}
\end{align}
with $s=2$.

UMR-PR ignores the Flory-Huggins contribution, Equation \ref{eq:fh},
of the combinatorial term, Equation \ref{eq:comb}.

UMR-PR uses the original temperature independent UNIFAC parameters published by Hansen et al \cite{Hansen1991} and Dortmund Data Bank, Wittig et al \cite{Wittig2003}.

Data source:
\url{https://en.wikipedia.org/wiki/UNIFAC}

\url{http://www.ddbst.com/unifacga.html}

\url{http://www.aim.env.uea.ac.uk/aim/info/UNIFACgroups.html}

The volume correction temperature differentials used in UMR is not
continous. This might be a good reason not to use the model.

\subsection{t-mPR model}
t-mPR \cite{Avlonitis1994} is an extension of the t-PR
\cite{Magoulas1990} to mixtures.

The t-mPR model take the following form,
\begin{equation}
  \label{eq:t-mPR}
  P = \frac{RT}{V+t-b} - \frac{a}{(V+t)(V+t+b) + b(V+t-b)},
\end{equation}
where,
\begin{equation}
  \label{eq:tmix}
  t = t(\vektor{x},T) = \underset{i}{\sum} x_i t_i(T).
\end{equation}
We see that by introducing $\tilde{V} = V + t$, the relations for this
equation of state can be related to the standard Peng-Robinson
equation of state. The translation is slightly more complicated than
the P{\'e}neloux \cite{Peneloux1982} volume shift, due to the
temperature dependency, and the lack of correction to the covolume.

\section{PSRK model}
PSRK \cite{Holderbaum1991} uses SRK with Mathias-Copeman
$\alpha$-correlation \cite{Mathias1983}, and a UNIFAC excess Gibbs
energy model.

The zero pressure limit, Equation \ref{eq:zero_limit},
is used when including the mixing rules into the SRK EOS.
$h_{\text{PSRK}}(\beta_0) = 0.64663$ is used.

The zero pressure limit is used when including the excess Gibbs energy into the equation of state,
\begin{equation}
  \label{eq:zero_limit}
  \frac{a}{RTb} = \underset{i}{\sum} x_i \frac{a_i}{RTb_i} - \frac{1}{h(\beta_0)}\left(\underset{i}{\sum} x_i \ln \frac{b}{b_i} + \frac{G^\excess}{RT} \right),
\end{equation}
where $h(\beta_0)$ is a constant that depend on the EOS. We have $h_{\text{PR}}(\beta_0) = 0.53$.

The linear mixing of the covolume is used in PSRK,
\begin{equation}
  \label{eq:linbmix}
  b  = \underset{i}{\sum} x_i b_i.
\end{equation}

\textcolor{red}{Parameters: \cite{Horstmann2005,Holderbaum1991,Horstmann2000,Fischer1996,Gmehling1997}}

\section{VTPR model}
The Volume-Translated-Peng-Robinson (VTPR) EOS \cite{Collinet2006}, uses a constant volume correction for each component. The correction in volume therefore don't depend on temperature. The Twu, Bluck, Cunningham and Coon $\alpha$-correlation \cite{Twu1991} is used.

For the excess Gibbs energy, the UNIFAC model is used without the combinatorial term, Equation \ref{eq:comb}. The infinite pressure limit is used when including the activity coefficient model into the EOS.

Covolume mixing uses Equation \ref{eq:bij} with $s=4/3$.

\textcolor{red}{Parameters: \cite{Schmid2014}}

\section{The general mixing rule for the covolume}
The general mixing rules for the covolume take the following form,
\begin{align}
  nB = n^2 b &= \underset{i}{\sum} \underset{j}{\sum} n_i n_j b_{ij}, \label{eq:B}\\
  b_{ij}^{\frac{1}{s}} &= (1 - l_{ij}) \frac{b_i^{\frac{1}{s}} + b_j^{\frac{1}{s}}}{2}.
  \label{eq:bij_mod}
\end{align}
Where $l_{ij}$ is assumed constant, symmetric, and have a default value of zero.

Differentiating and manipulating Equation \ref{eq:B}, $B_{i}$ and
$B_{ij}$ become,
\begin{align}
  n B_i = &= 2\underset{j}{\sum} n_j b_{ij} -B, \label{eq:Bi}\\
  n B_{ij} = &= 2b_{ij} - B_i - B_j.
  \label{eq:Bij}
\end{align}
\clearpage
\bibliographystyle{plain}
\bibliography{../thermopack}

\end{document}


% --------------------------------------------------------------
% This is all preamble stuff that you don't have to worry about.
% Head down to where it says "Start here"
% --------------------------------------------------------------

\documentclass[12pt]{article}

\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,amssymb}
\usepackage{listings}
\usepackage{bm}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subfigure}
\usepackage{float}
% \usepackage{subcaption}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}

\newenvironment{theorem}[2][Theorem]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}
\newenvironment{lemma}[2][Lemma]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}
\newenvironment{exercise}[2][Exercise]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}
\newenvironment{reflection}[2][Reflection]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}
\newenvironment{proposition}[2][Proposition]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}
\newenvironment{corollary}[2][Corollary]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}

\begin{document}

% --------------------------------------------------------------
%                         Start here
% --------------------------------------------------------------

%\renewcommand{\qedsymbol}{\filledbox}

\title{TalkingData AdTracking Fraud Detection Challenge}%replace X with the appropriate number
\author{Guozhen Li, Hairu Lang \\ %replace with your name
STA 208 - Statistical Machine Learning - Final Project} %if necessary, replace with your course title

\maketitle

\section{Problem Description}
This problem is a data analysis challenge from Kaggle (www.kaggle.com), originally found at  \url{https://www.kaggle.com/c/talkingdata-adtracking-fraud-detection}.
The data provider, TalkingData, tracks a large base of mobile device activity,
and wants to understand which ad clicks end up with app downloads,
and which ones lead to nothing (thus are suspects of fraud clicks).
We are provided with a training data set of over 180 million clicks,
and the objective is to predict whether a click leads to an app download.

For each click, the raw training data set has information about its:
\begin{itemize}
	\item \texttt{ip}: IP address
	\item \texttt{app}: app of marketing
	\item \texttt{device}: device type
	\item \texttt{os}: mobile device operating system type
	\item \texttt{channel}: mobile ad publisher channel
	\item \texttt{click\_time}: click time (date and time, down to seconds)
	\item \texttt{is\_attributed}: whether a download occurs at the end
	\item \texttt{attributed\_time}: download time, if a download occurs
\end{itemize}

This data analysis task poses, at least, the following challenges:
\begin{enumerate}
	\item Extract useful features from the seemingly dispersed information found in the raw training data.
	\item Handling of the extra-large data set (over 180 million samples, as an over 7GB text file), which hardly fits into the memory of a normal personal computer.
	\item Efficient and effective learning method to learn from the data.
\end{enumerate}

\section{Data Exploration}
The training data set has 8 fields, namely ``ip'', ``app'', ``device'', ``os'', ``channel'', ``click\_time'', ``attributed\_time'', and ``is\_attributed''.
Among these, ``ip'' through ``channel'' are categorical features, ``click\_time'' and ``attributed\_time'' are timestamps, and ``is\_attributed'' is the prediction target.
Fortunately, the data is well-cleaned, and there aren't any missing values except in the ``attributed\_time'' field, which is not surprising because not all clicks led to downloads (i.e. attributed).

We performed some exploratory data analysis in notebook \texttt{python/1 Explore.ipynb}.
The exploration reveals several noticeable facts about the data:
\begin{itemize}
    \item There exist a period of time during a day when clicks and downloads are both low (Figure \ref{fig:activity_by_hour}).
    \item Although the minute-of-hour and second-of-minute of a click doesn't appear significantly correlated to download activities, (click\_minute mod 15) and (click\_second mod 5) seems to seems to do. This might have to do with click-bots that are programmed to ``click'' ads at intervals.
    \item Although there are a huge number of unique IPs, apps, devices, etc. appearing in the training data set, a much smaller number of them show up repeatedly (Figure \ref{fig:unique_values_with_more_activity}). The frequently recurring ones are perhaps targets we want to pay more attention to.
    \item The target feature (whether a click leads to download) in the training data set is highly imbalanced. Only about 0.2\% clicks lead to downloads.
\end{itemize}

\begin{figure}[H]
    \centering
    \subfigure{
        \begin{minipage}{0.45\textwidth}
            \centering
            \includegraphics[width=\textwidth]{img/clicks_by_hour.png}
        \end{minipage}
    }
    \subfigure{
        \begin{minipage}{0.45\textwidth}
            \centering
            \includegraphics[width=\textwidth]{img/downloads_by_hour.png}
        \end{minipage}
    }
    \caption{User activity by hour of day}
    \label{fig:activity_by_hour}
\end{figure}

\begin{figure}[H]
    \centering
    \subfigure{
        \begin{minipage}{0.45\textwidth}
            \centering
            \includegraphics[width=\textwidth]{img/unique_values_with_x_clicks.png}
        \end{minipage}
    }
    \subfigure{
        \begin{minipage}{0.45\textwidth}
            \centering
            \includegraphics[width=\textwidth]{img/unique_values_with_x_downloads.png}
        \end{minipage}
    }
    \caption{Number of unique values in a feature when filtered for more activities}\label{fig:unique_values_with_more_activity}
\end{figure}

\section{Feature Engineering and Data Pre-processing}
The feature engineering  and data preprocessing work is demonstrated in notebook \texttt{python/2 Feature Engineering and Preprocessing.ipynb}.
For convenience in later modeling, we also implemented the procedures as a \texttt{Preprocessor} class in \texttt{python/preprocess.py},
which can be instantiated and repeatedly used to preprocess a chunk of the training data.

The key actions we took on data preprocessing include:
\begin{itemize}
    \item Extract hour of day, minute of hour, and second of minute from \texttt{click\_time}
    \item Make \texttt{minute\_mod\_15} and \texttt{second\_mod\_5}
    \item Find number of clicks, number of downloads, and downloads-per-click ratio for each IP, each OS, each device, deach app, and each channel, and match these stats to their respective records in training data
    \item One-hot encode IPs, apps, devices, OSes, and chennels that are recurring in the full training data set. Here ``recurring'' is defined as those who made more than 1 download.
\end{itemize}

We also propose two approaches to deal with the large size of the training data: 1) Randomly select a smaller sample for training, or 2) Feed data in small batches and train the model incrementally (a.k.a. online learning).

\section{Modeling and Predicting}

\subsection{Support Vector Machine with RBF Kernel}
We first apply Support Vector Classifier with RBF kernel and tune the parameter for gammas. The relationship between gammas and the accuracy is given by:
\begin{figure}[H]
    \centering
    \includegraphics[width=8cm]{img/SVC_RBF/RBF_SVM.png}
\end{figure}
We observe that as gammas increase, the accuracy of prediction is getting larger. The accuracy is high.

% \subsection{Gradient Tree Boosting with XGBoost}

\subsection{AdaBoostClassifier}
AdaBoostClassifier is a ensemble classifier that combines weaker classifier algorithm to form strong classifier. It generates the optimal classifier by retraining the algorithm iteratively choosing the training set based on accuracy of previous training. We tune two parameter: one is the number of estimators and the other is the maximum depth of the tree. We plot graphs for the relationship between accuracy and the number of estimators, conditional on the maximum depth of the tree. As we change the max depth of the tree (i.e., Decision Tree), the effect of the number of estimators on the accuracy can change a lot. A further inspection reveals that when the depth is one, the accuracy is close to zero. A higher depth can generate accuracies close to 1. Another observation is that the average accuracy corresponding to each max depth gets the highest when the depth is four. Thus, we pick up MaxDepth = 4. Then, we choose 50 to be the number of estimators in our model. The highest accuracy is about 0.99985.
\begin{figure}[H]
    \centering
    \subfigure[MaxDepth=1]{
        \begin{minipage}{4.5cm}
            \centering
            \includegraphics[width=4.5cm]{img/AdaBoostClassifier/Ada_1.png}
        \end{minipage}
    }
    \subfigure[MaxDepth=2]{
        \begin{minipage}{4.5cm}
            \centering
            \includegraphics[width=4.5cm]{img/AdaBoostClassifier/Ada_max_2.png}
        \end{minipage}
    }
    \subfigure[MaxDepth=3]{
        \begin{minipage}{4.5cm}
            \centering
            \includegraphics[width=4.5cm]{img/AdaBoostClassifier/Ada_10_max_len_3.png}
        \end{minipage}
    }
\end{figure}

\begin{figure}[H]
    \centering
    \subfigure[MaxDepth=4]{
        \begin{minipage}{4.5cm}
            \centering
            \includegraphics[width=4.5cm]{img/AdaBoostClassifier/Ada_4.png}
        \end{minipage}
    }
    \subfigure[MaxDepth=5]{
        \begin{minipage}{4.5cm}
            \centering
            \includegraphics[width=4.5cm]{img/AdaBoostClassifier/Ada_5.png}
        \end{minipage}
    }
    \subfigure[MaxDepth=6]{
        \begin{minipage}{4.5cm}
            \centering
            \includegraphics[width=4.5cm]{img/AdaBoostClassifier/Ada_6.png}
        \end{minipage}
    }
\end{figure}

\subsection{Random Forest}
We also implement Random Forest Classification method and tune the parameter for the number of features to consider when looking for the best split, conditional on the number of trees in the forest. The range for the number of estimators is [10, 60] and the range for the maximum number of features is [1, 20]. The following graphs reflect how the maximum number of features influences the prediction accuracy, conditional on the number of estimators.
\begin{figure}[H]
\centering
\subfigure[Random10]{
\begin{minipage}{4.5cm}
\centering
\includegraphics[width=4.5cm]{img/RandomForest/RandomForest10.png}
\end{minipage}
}
\subfigure[Random20]{
\begin{minipage}{4.5cm}
\centering
\includegraphics[width=4.5cm]{img/RandomForest/RandomForest20.png}
\end{minipage}
}
\subfigure[Random30]{
\begin{minipage}{4.5cm}
\centering
\includegraphics[width=4.5cm]{img/RandomForest/RandomForest30.png}
\end{minipage}
}
\end{figure}

\begin{figure}[H]
\centering
\subfigure[Random40]{
\begin{minipage}{4.5cm}
\centering
\includegraphics[width=4.5cm]{img/RandomForest/RandomForest40.png}
\end{minipage}
}
\subfigure[Random50]{
\begin{minipage}{4.5cm}
\centering
\includegraphics[width=4.5cm]{img/RandomForest/RandomForest1.png}
\end{minipage}
}
\subfigure[Random60]{
\begin{minipage}{4.5cm}
\centering
\includegraphics[width=4.5cm]{img/RandomForest/RandomForest60.png}
\end{minipage}
}
\end{figure}
Based on above results, we can see that as we increase the number of estimators, the accuracy of prediction is decreasing, contrary to the theory. The possible reason is that an accurate forest consists of diverse and accurate models. When the size of the forest is getting larger, it  becomes more difficult to generate a new diverse model. Thus, adding one tree which is not diverse enough would not lead to the improvement of the performance. We would choose 10 for the number of estimators. Based on the first graph, when the number of features is 5, 8, 12, the precision is close to one.

\subsection{LightGBM}
LightGBM is a gradient boosting framework that uses tree-based on learning algorithm. It is relatively new but powerful. Compared with other tree-based algorithms, it grows tree vertically. The main advantanges of LightGBM are that it can handle large size of data, taking lower memory to run and focuses on accuracy of results. We implement LightGBM algorithm to make prediction and tune the values of parameter learning rate, conditional on the number of leaves. The relationships between the accuracy and learning rates are given by:
\begin{figure}[H]
\centering
\subfigure[NumLeaves20]{
\begin{minipage}{4.5cm}
\centering
\includegraphics[width=4.5cm]{img/LightGBM/Light_Num_20.png}
\end{minipage}
}
\subfigure[NumLeaves25]{
\begin{minipage}{4.5cm}
\centering
\includegraphics[width=4.5cm]{img/LightGBM/Light_Num_25.png}
\end{minipage}
}
\subfigure[NumLeaves30]{
\begin{minipage}{4.5cm}
\centering
\includegraphics[width=4.5cm]{img/LightGBM/Light_Num_30.png}
\end{minipage}
}
\end{figure}

\begin{figure}[H]
\centering
\subfigure[NumLeaves35]{
\begin{minipage}{4.5cm}
\centering
\includegraphics[width=4.5cm]{img/LightGBM/Light_Num_35.png}
\end{minipage}
}
\subfigure[NumLeaves40]{
\begin{minipage}{4.5cm}
\centering
\includegraphics[width=4.5cm]{img/LightGBM/Light_Num_40.png}
\end{minipage}
}
\subfigure[NumLeaves45]{
\begin{minipage}{4.5cm}
\centering
\includegraphics[width=4.5cm]{img/LightGBM/Light_Num_45.png}
\end{minipage}
}
\end{figure}
Based on above graphs, the number of leaves does not make big differences. As we tune the number of leaves, the relationship between the accuracy of prediction and learning rates does not change much. The consistent result from those graphs is that as we increase the learning rate to 0.11, the accuracy is higher. We set the value of the number of leaves to be 30 and choose 0.102 as the learning rate. The highest accuracy is also higher than 0.99975.

\section{Model Evaluation and Conclusions}
First, both AdaBoostClassifier and LightGBM can generate high accuracies of prediction. Random Forest classifier and SVM with RBF Kernel, however, can generate relatively low accuraries sometimes. Second, the prediction results from AdaBoostClassifier and Random Forest classifier can be sensitive to the changes in the parameters and the way to split the data. Prediction from LightGBM is not sensitive to the parameter changes or respond to the changes in a matter that can be expected. As for the running time, the implementation of AdaBoostClassifier and SVC with RBF Kernel can be quite time-consuming; however, the implementation of LightGBM can take much shorter time. Overall, we will choose LightGBM as our prediction model and choose the learning rate as 0.1.

% --------------------------------------------------------------
%     You don't have to mess with anything below this line.
% --------------------------------------------------------------

\end{document}


\documentclass[preprint,showkeys,nofootinbib]{revtex4-1}

% linking references
\usepackage{hyperref}
\hypersetup{
  breaklinks=true,
  colorlinks=true,
  linkcolor=blue,
  urlcolor=cyan,
}

% general physics / math packages and commands
\usepackage{physics,amsmath,amssymb,braket,dsfont}
\renewcommand{\t}{\text} % text in math mode
\newcommand{\f}{\dfrac} % shorthand for fractions
\newcommand{\p}[1]{\left(#1\right)} % parenthesis
\renewcommand{\sp}[1]{\left[#1\right]} % square parenthesis
\renewcommand{\set}[1]{\left\{#1\right\}} % curly parenthesis
\newcommand{\bk}{\braket} % shorthand for braket

\renewcommand{\d}{\text{d}}
\newcommand{\g}{\text{g}}
\newcommand{\e}{\text{e}}
\newcommand{\x}{\text{x}}
\newcommand{\y}{\text{y}}
\newcommand{\z}{\text{z}}

\renewcommand{\c}{\hat{c}}
\newcommand{\n}{\hat{n}}

\newcommand{\A}{\mathcal{A}}
\newcommand{\B}{\mathcal{B}}
\newcommand{\D}{\mathcal{D}}
\newcommand{\E}{\mathcal{E}}
\newcommand{\G}{\mathcal{G}}
\renewcommand{\H}{\mathcal{H}}
\newcommand{\I}{\mathcal{I}}
\newcommand{\K}{\mathcal{K}}
\renewcommand{\L}{\mathcal{L}}
\newcommand{\M}{\mathcal{M}}
\newcommand{\N}{\mathcal{N}}
\renewcommand{\O}{\mathcal{O}}
\renewcommand{\P}{\mathcal{P}}
\newcommand{\Q}{\mathcal{Q}}
\renewcommand{\S}{\mathcal{S}}
\newcommand{\U}{\mathcal{U}}

\newcommand{\1}{\mathds{1}}

\newcommand{\mA}{m_{\text{A}}} % symbol for the mass of an atom

% "left vector" arrow; requires tikz package
\newcommand{\lvec}[1]
{\reflectbox{\ensuremath{\vec{\reflectbox{\ensuremath{#1}}}}}}

% figures
\usepackage{graphicx} % for figures
\usepackage{grffile} % help latex properly identify figure extensions
\graphicspath{{./figures/}} % set path for all figures
\usepackage[caption=false]{subfig} % subfigures (via \subfloat[]{})

% inline lists
\usepackage[inline]{enumitem}

% for feynman diagrams
\usepackage{tikz,tikz-feynman}
\tikzset{
  baseline = (current bounding box.center)
}
\tikzfeynmanset{
  compat = 1.1.0,
  every feynman = {/tikzfeynman/small}
}
\newcommand{\shrink}[1]{\scalebox{0.8}{#1}} % for smaller diagrams

% color definitions (used in a figure)
\usepackage{xcolor}
\definecolor{lightblue}{RGB}{31,119,180}
\definecolor{orange}{RGB}{255,127,14}
\definecolor{green}{RGB}{44,160,44}
\definecolor{lightred}{RGB}{214,39,40}

% proper coloring inside math environment
\makeatletter
\def\mathcolor#1#{\@mathcolor{#1}}
\def\@mathcolor#1#2#3{
  \protect\leavevmode
  \begingroup
    \color#1{#2}#3
  \endgroup
}
\makeatother
\newcommand{\bmu}{\mathcolor{lightblue}{\mu}}
\newcommand{\onu}{\mathcolor{orange}{\nu}}
\newcommand{\grho}{\mathcolor{green}{\rho}}
\newcommand{\re}{\mathcolor{lightred}{\text{e}}}

% leave a note in the text, visible in the compiled document
\newcommand{\note}[1]{\textcolor{red}{#1}}

% for strikeout text
% normalem included to prevent underlining titles in the bibliography
\usepackage[normalem]{ulem}

\newcommand{\blue}[1]{\textcolor{blue}{#1}}
\newcommand{\red}[1]{\textcolor{red}{#1}}
\newcommand{\green}[1]{\textcolor{green}{#1}}

\begin{document}

\section*{Referee 2}

We thank the referee for taking the time to read and reconsider our
revised manuscript.  Concerning the comments:

\blue{I read the replies from the authors and other report and
  recommend the publication.}

\blue{There are a few discussions on ``renormalization'' schemes in
  the article and in Referee 1's report.  Let me comment on the scheme
  in the manuscript. The scheme used here (more precisely
  ``regularization'' scheme) is a quite standard and has been commonly
  used in many-body studies since the beginning of many-body
  diagrammatic calculations. It is based on a k-space ``pseudo''
  potential.  From the point of view of low energy few-body physics
  studied here, it is indeed equivalent to the other well known
  standard real space ``differential pseudo'' potential Referee A
  might have in mind (I speculate that what he/she meant by ``pseudo
  potential''). But more importantly, it can be easily and
  straightforwardly generalized to arbitrary dimensions and can be
  integrated into studies of complex/sophisticated collective
  many-body effects associated with Fermi surfaces and/or sound waves
  in symmetry breaking states.}

We thank the referee for their recommendation to publish our
manuscript, and for their input on the regularization scheme in our
manuscript.  In particular, we thank the referee for pointing out that
our approach to regularizing the two-body delta-function interaction
is a standard technique in many-body studies, equivalent to another
common technique based on a regularized real-space ``differential
pseudo-potential''.

\end{document}


\chapter{Comparison with othert ISAs}

\hrulefill

\textbf{IMPORTANT NOTE:} Some of the discussions below refer to an older draft of the
RISC-V Bitmanip extension and are now out-of-date.

\hrulefill

\section{Comparison with x86 Bit Manipulation ISAs}
\label{x86comp}

The following code snippets implement all instructions from the x86 bit manipulation
ISA extensions ABM, BMI1, BMI2, and TBM using RISC-V code that does not spill any
registers and thus could easily be implemented in a single instruction using macro-op
fusion. (Some of them simply map directly to instructions in this spec and so no
macro-op fusion is needed.) Note that shorter RISC-V code sequences are
sometimes possible if we allow spilling to temporary registers.

ABM added x86 encodings for POPCNT, LZCNT, and TZCNT.\footnote{Depending on if
you ask Intel or AMD you will get different opinion regarding whether LZCNT
and/or TZCNT are part of ABM or BMI1.} The difference between LZCNT and the
80386 instruction BSR, and between TZCNT and the 80386 instruction BSF, is that
the new instructions return the operand size when the input operand is zero,
while BSR and BSF were undefined in that case. The ABM instructions map 1:1 to
Bitmanip instructions. Table~\ref{abm-comp} lists ABM instructions and regular
x86 bit manipulation instructions.

\begin{table}[h]
\centering
\begin{tabular}{lrrl}
\multirow{2}{*}{x86 Instruction} & \multicolumn{2}{c}{Bytes} & \multirow{2}{*}{RISC-V Code} \\
& x86 & RV & \\
\hline
POPCNT       &   5 &  4 & {\tt cpop a0, a0} \\
\hline
LZCNT / BSR  &   5 &  4 & {\tt clz a0, a0} \\
\hline
TZCNT / BSF  &   5 &  4 & {\tt ctz a0, a0} \\
\hline
BSWAP        &   3 &  4 & {\tt bswap} \\
\hline
ROL          &   4 &  4 & {\tt roli} \\
\hline
ROR          &   4 &  4 & {\tt rori} \\
\hline
BT           &   5 &  4 & {\tt c.srl a0, N} \\
             &     &    & {\tt c.andi a0, 1} \\
\hline
BTC          &   5 & 16 & {\tt rori a0, N} \\
             &     &    & {\tt andi a1, a0, 1} \\
             &     &    & {\tt xori a0, a0, 1} \\
             &     &    & {\tt rori a0, XLEN-N} \\
\hline
BTR          &   5 & 16 & {\tt rori a0, N} \\
             &     &    & {\tt andi a1, a0, 1} \\
             &     &    & {\tt andi a0, a0, -2} \\
             &     &    & {\tt rori a0, XLEN-N} \\
\hline
BTS          &   5 & 16 & {\tt rori a0, N} \\
             &     &    & {\tt andi a1, a0, 1} \\
             &     &    & {\tt ori a0, a0, 1} \\
             &     &    & {\tt rori a0, XLEN-N} \\
\end{tabular}
\caption{Comparison of x86+ABM with Bitmanip}
\label{abm-comp}
\end{table}

BMI1 adds some instructions for trailing bit manipulations, an add-complement instruction,
and a bit field extract instruction that expects the length and start position packed in one
register operand. Our version expects the length in a0, start position in a1, and source
value in a2. See Table~\ref{bmi1-comp}.

\begin{table}[h]
\centering
\begin{tabular}{lrrl}
\multirow{2}{*}{x86 Instruction} & \multicolumn{2}{c}{Bytes} & \multirow{2}{*}{RISC-V Code} \\
& x86 & RV & \\
\hline
ANDN    & 5 &  4 & {\tt andn a0, a2, a1} \\
\hline
BEXTR (regs)  & 5 & 12 & {\tt c.add a0, a1} \\
              &   &    & {\tt slo a0, zero, a0} \\
              &   &    & {\tt c.and a0, a2} \\
              &   &    & {\tt srl a0, a0, a1} \\
\hline
BLSI          & 5 &  6 & {\tt neg a0, a1} \\
              &   &    & {\tt c.and a0, a1} \\
\hline
BLSMSK        & 5 &  6 & {\tt addi a0, a1, -1} \\
              &   &    & {\tt c.xor a0, a1} \\
\hline
BLSR          & 5 &  6 & {\tt addi a0, a1, -1} \\
              &   &    & {\tt c.and a0, a1} \\
\end{tabular}
\caption{Comparison of x86 BMI1 with Bitmanip}
\label{bmi1-comp}
\end{table}

BMI2 adds a few \texttt{*X} instructions that just perform the indicated
operation without changing any flags. RISC-V does not use flags, so this
instructions trivially just map to their regular RISC-V counterparts. In
addition to those instructions, BMI2 adds bit extract and deposit instructions
and an instruction to clear high bits above a given bit index. See Table~\ref{bmi2-comp}.

\begin{table}[h]
\centering
\begin{tabular}{lrrl}
\multirow{2}{*}{x86 Instruction} & \multicolumn{2}{c}{Bytes} & \multirow{2}{*}{RISC-V Code} \\
& x86 & RV & \\
\hline
BZHI     & 5 &  6 & {\tt slo a0, zero, a2} \\
         &   &    & {\tt c.and a0, a1} \\
\hline
PDEP     & 5 &  4 & {\tt bdep} \\
\hline
PEXT     & 5 &  4 & {\tt bext} \\
\hline
MULX     & 5 &  4 & {\tt mul} \\
\hline
RORX     & 6 &  4 & {\tt rori} \\
\hline
SARX     & 5 &  4 & {\tt sra} \\
\hline
SHRX     & 5 &  4 & {\tt srl} \\
\hline
SHLX     & 5 &  4 & {\tt sll} \\
\end{tabular}
\caption{Comparison of x86 BMI2 with Bitmanip}
\label{bmi2-comp}
\end{table}

Finally, TBM was a short-lived x86 ISA extension introduced by AMD in
Piledriver processors, complementing the trailing bit manipulation instructions
from BMI1. See Table~\ref{tbm-comp}.

\begin{table}[h]
\centering
\begin{tabular}{lrrl}
\multirow{2}{*}{x86 Instruction} & \multicolumn{2}{c}{Bytes} & \multirow{2}{*}{RISC-V Code} \\
& x86 & RV & \\
\hline
BEXTR (imm)  & 7 &  4 & {\tt c.slli a0, (32-START-LEN)} \\
             &   &    & {\tt c.srli a0, (32-LEN)} \\
\hline
BLCFILL      & 5 &  6 & {\tt addi a0, a1, 1} \\
             &   &    & {\tt c.and a0, a1} \\
\hline
BLCI         & 5 &  8 & {\tt addi a0, a1, 1} \\
             &   &    & {\tt c.not a0} \\
             &   &    & {\tt c.or a0, a1} \\
\hline
BLCIC        & 5 & 10 & {\tt addi a0, a1, 1} \\
             &   &    & {\tt andn a0, a1, a0} \\
             &   &    & {\tt c.not a0} \\
\hline
BLCMSK       & 5 &  6 & {\tt addi a0, a1, 1} \\
             &   &    & {\tt c.xor a0, a1} \\
\hline
BLCS         & 5 &  6 & {\tt addi a0, a1, 1} \\
             &   &    & {\tt c.or a0, a1} \\
\hline
BLSFILL      & 5 &  6 & {\tt addi a0, a1, -1} \\
             &   &    & {\tt c.or a0, a1} \\
\hline
BLSIC        & 5 & 10 & {\tt addi a0, a1, -1} \\
             &   &    & {\tt andn a0, a1, a0} \\
             &   &    & {\tt c.not a0} \\
\hline
T1MSKC       & 5 & 10 & {\tt addi a0, a1, +1} \\
             &   &    & {\tt andn a0, a1, a0} \\
             &   &    & {\tt c.not a0} \\
\hline
T1MSK        & 5 &  8 & {\tt addi a0, a1, -1} \\
             &   &    & {\tt andn a0, a0, a1} \\
\end{tabular}
\caption{Comparison of x86 TBM with Bitmanip}
\label{tbm-comp}
\end{table}

\section{Comparison with RI5CY Bit Manipulation ISA}

The following section compares Bitmanip with the RI5CY bit manipulation
instructions as documented in~\cite{Ri5cy}. All RI5CY bit manipulation
instructions (or something very close to their behavior) can be emulated with
Bitmanip using 3 instructions or less.

\subsubsection{RI5CY Instructions {\tt p.extract}, {\tt p.extractu}, {\tt p.extractr}, and {\tt p.extractur}}

These four RI5CY instructions extract bit-fields. The non-{\tt u}-versions sign-extend
the extracted bit-field. This operations can be performed with two shift-immediate
operations. It even fits in a 32-bit word when using compressed instructions (requires
{\tt rd} $=$ {\tt rs}).

\begin{verbatim}
  p.extract rd, rs, len, pos:
    slli rd, rs, (XLEN-pos-len)
    srai rd, rd, (XLEN-len)

  p.extractu rd, rs, len, pos:
    slli rd, rs, (XLEN-pos-len)
    srli rd, rd, (XLEN-len)
\end{verbatim}

The {\tt r}-versions expect the bit-field size in bits 9:5 of the second source
register and the bit-field start in bits 4:0. Instead we use two registers,
$\texttt{rx} = XLEN-pos-len$ and $\texttt{ry} = XLEN-len$.

\begin{verbatim}
  p.extractr:
    sll rd, rs, rx
    sra rd, rd, ry

  p.extractur:
    sll rd, rs, rx
    srl rd, rd, ry
\end{verbatim}

Alternatively, instead of packing length and position into a register, we
can create a mask in a register and then use this mask with {\tt bext}. This
has the advantage over the {\tt sll}+{\tt srl} sequence that the mask only needs
to be generated once and can then be re-used many times, effectively implementing
{\tt p.extractur} in a single instruction.

\begin{verbatim}
  p.extractur:
    slo rMask, zero, rLen
    sll rMask, rMask, rPos
    bext rd, rs, rMask
\end{verbatim}

{\tt p.extractu} can be efficiently emulated with a single XBitfield {\tt bfxp}
instruction (see Chapter~\ref{bfxp}):

\begin{verbatim}
  p.extract rd, rs, len, pos:
    bfxp rd, rs, zero, pos, len, 0
\end{verbatim}

\subsubsection{RI5CY Instructions {\tt p.insert} and {\tt p.insertr}}

These instructions OR the destination register with the {\tt len} LSB bits
from the source register, shifted up by {\tt pos} bits. This can easily
be achieved using three instructions and a temporary register {\tt rt}:

\begin{verbatim}
  p.insert rd, rs, len, pos, rt:
    slli rt, rs, (XLEN-len)
    srli rt, rt, (XLEN-len-pos)
    or rd, rd, rt
\end{verbatim}

The {\tt r}-version of the instruction expects {\tt len} in bits 9:5 of the
second source register and {\tt pos} in bits 4:0. Instead we use two registers,
$\texttt{rx} = XLEN-pos-len$ and $\texttt{ry} = XLEN-len$.

\begin{verbatim}
  p.insertr:
    slli rt, rs, ry
    srli rt, rt, rx
    or rd, rd, rt
\end{verbatim}

Alternatively, instead of packing length and position into a register, we
can create a mask in a register and then use this mask with {\tt bdep}. This
has the advantage over the {\tt sll}+{\tt srl} sequence that the mask only needs
to be generated once and can then be re-used many times.

\begin{verbatim}
  p.extractur:
    slo rMask, zero, rLen
    sll rMask, rMask, rPos
    bdep rt, rs, rMask
    or rd, rd, rt
\end{verbatim}

{\tt p.insert} can be efficiently emulated with a single XBitfield {\tt bfxp}
instruction (see Chapter~\ref{bfxp}), if target region in {\tt rd} contains
only zeros ({\tt bfxp} clears the target region before performing the OR):

\begin{verbatim}
  p.insert rd, rs, len, pos:
    bfxp rd, rs, rd, 0, len, pos
\end{verbatim}

\subsubsection{RI5CY Instructions {\tt p.bclr} and {\tt p.bclrr}}

These instructions clear {\tt len} bits starting with bit {\tt pos}. Using a
temporary register {\tt rt}:

\begin{verbatim}
  p.bclr rd, rs, len, pos, rt:
    sloi rt, zero, len
    slli rt, rt, pos
    andn rd, rs, rt
\end{verbatim}

Or using two registers {\tt rLen} and {\tt rPos}:

\begin{verbatim}
  p.bclrr rd, rs, rLen, rPos, rt:
    slo rt, zero, rLen
    sll rt, rt, rPos
    andn rd, rs, rt
\end{verbatim}

If the mask in {\tt rt} can be pre-computed then a single {\tt andn} instruction
can emulate {\tt p.bclrr}, or a single {\tt and}/{\tt c.and} instruction if the
mask is already inverted.

Or using {\tt bfxp} with $\texttt{rd} = \texttt{rs}$ (see Chapter~\ref{bfxp}):

\begin{verbatim}
  p.bclr rd, len, pos:
    bfxp rd, zero, rd, 0, len, pos
\end{verbatim}

\subsubsection{RI5CY Instructions {\tt p.bset} and {\tt p.bsetr}}

These instructions set {\tt len} bits starting with bit {\tt pos}. They can be
implemented similar to {\tt p.bclr} and {\tt p.bclrr} by replacing {\tt andn}
with {\tt or}:

\begin{verbatim}
  p.bset rd, rs, len, pos, rt:
    sloi rt, zero, len
    slli rt, rt, pos
    or rd, rs, rt

  p.bsetr rd, rs, rLen, rPos, rt:
    slo rt, zero, rLen
    sll rt, rt, rPos
    or rd, rs, rt
\end{verbatim}

If the mask in {\tt rt} can be pre-computed then a single {\tt or}/{\tt c.or} instruction
can emulate {\tt p.bsetr}.

Or using {\tt bfxpc} with $\texttt{rd} = \texttt{rs}$ (see Chapter~\ref{bfxp}):

\begin{verbatim}
  p.bset rd, len, pos:
    bfxpc rd, zero, rd, 0, len, pos
\end{verbatim}

\subsubsection{RI5CY Instructions {\tt p.ff1}, {\tt p.cnt}, and {\tt p.ror}}

These instructions map directly to the Bitmanip instructions {\tt ctz}, {\tt cpop}, and {\tt ror}.

\subsubsection{RI5CY Instructions {\tt p.fl1}}

This instruction returns the index of the last set bit in {\tt rs}. If {\tt rs} is 0, {\tt rd} will be 0.

Using the arguably more useful definition that the operation should return -1 when {\tt rs} is 0:

\begin{verbatim}
  p.fl1 rd, rs:
    clz rd, rs
    neg rd, rd
    addi rd, rd, 31
\end{verbatim}

Converting a -1 result to 0 to match the exact {\tt p.fl1} behavior:

\begin{verbatim}
    slt rt, rd, zero
    add rd, rd, rt
\end{verbatim}

\subsubsection{RI5CY Instructions {\tt p.clb}}

This instruction counts the number of consecutive 1s or 0s from MSB. If {\tt rs} is 0, {\tt rd} will be 0.

Using the arguably more useful definition that the operation should return XLEN when {\tt rs} is 0 or -1,
and assuming {\tt rd} $\ne$ {\tt rs1}:

\begin{verbatim}
  p.clb:
    srai rd, rs, XLEN-1
    xor rd, rd, rs
    clz rd, rd
\end{verbatim}

Simply add {\tt andi rd, rd, XLEN-1} if {\tt rd} should be 0 when {\tt rs} is 0 or -1.

\section{Comparison with Cray XMT bit operations}

Cray XMT is the 3rd generation of the Cray MTA architecture, a supercomputer
using a barrel processor architecture. The Cray XMT instruction set contains
a few bit manipulation instructions~\cite{CrayXMT}. In this section we compare
the Cray XMT bit manipulation instructions with Bitmanip.

\subsubsection{Bitwise boolean operations}

Cray XMT provides the following instructions for bitwise boolean operations:
{\tt BIT\_AND}, {\tt BIT\_IMP}, {\tt BIT\_NAND}, {\tt BIT\_NIMP}, {\tt
BIT\_NOR}, {\tt BIT\_OR}, {\tt BIT\_XNOR}, and {\tt BIT\_XOR}.

These can trivially emulated using basic RISC-V instructions. (Some of those
XMT instructions have a direct RISC-V equivalent. The others can be emulated
by combining the {\tt not} pseudoinstruction with {\tt and}, {\tt or}, or {\tt xor}.)

\subsubsection{Count Leading/Trailing Zeros/Ones}

The Cray XMT instructions {\tt BIT\_LEFT\_ZEORS} and {\tt BIT\_RIGHT\_ZEORS} are
equivalent to the Bitmanip {\tt clz} and {\tt ctz} instructions.

The {\tt BIT\_LEFT\_ONES} and {\tt BIT\_RIGHT\_ONES} instructions can be emulated
by combining the {\tt not} pseudoinstruction with {\tt clz} and {\tt ctz}.

\subsubsection{Mask generation}

The Cray XMT instruction {\tt BIT\_MASK dest top bot} generates a bitmask
that has the bits in the range $[\texttt{bot} \dots \texttt{top}]$ set
and the rest cleared iff $\texttt{bot} \le \texttt{top}$, and those
bits cleared and the rest set otherwise.

Similar masks can be generated using two instructions in Bitmanip,
using the regular shift instructions and the ``shift ones'' instructions.

\subsubsection{Cmix equivalent}

The Cray XMT {\tt BIT\_MERGE} instruction is equivalent to the XTernarybits
{\tt cmix} instruction, or the {\tt and-andn-or} MIX pattern.

\subsubsection{Population count}

The Cray XMT {\tt BIT\_TALLY} instruction and the Bitmanip {\tt cpop}
instruction are equivalent.

\subsubsection{Parity instructions}

The Cray XMT {\tt BIT\_ODD\_AND}, {\tt BIT\_ODD\_NIMP}, {\tt BIT\_ODD\_OR},
and {\tt BIT\_ODD\_XOR} instruction perform the indicated bitwise boolean
operation and then compute the parity of the result.

With Bitmanip the parity can be calculated with {\tt cpop dst, src} followed
by {\tt andi dst, dst, 1}.

\subsubsection{Bit pack/unpack instruction}

The Cray XMT {\tt BIT\_PACK} instruction and the Bitmanip {\tt bext}
instruction are equivalent.

The Cray XMT {\tt BIT\_UNPACK\_1}, {\tt BIT\_UNPACK\_2}, {\tt BIT\_UNPACK\_3},
instruction sequence, when used as intended, is equivalent to the Bitmanip
{\tt bdep} instruction.

\subsubsection{Bit matrix instructions}

The Cray XMT {\tt BIT\_MAT\_} instructions treat a 64-bit value as a 8x8 bit
matrix.

{\tt BIT\_MAT\_TRANSPOSE} is used to transpose such a bit matrix. With
Bitmanip instructions on RV64 this bit permutation can be performed by
applying the {\tt zip} operation three times to the register holding the
matrix (see also~\ref{transposebitboard}).

The Cray XMT instructions {\tt BIT\_MAT\_OR} and {\tt BIT\_MAT\_XOR} perform a
matrix-matrix multiply between such bit matrices, where AND replaces scalar
multiply and OR/XOR replaces scalar addition.

Bitmanip does not provide a similar operation. However, the two example
applications given in the Cray XMT documentation~\cite[p.~81]{CrayXMT}
are reversing the byte order in a word and reversing the bit order in
each byte. In Bitmanip those operations are performed by the {\tt bswap}
and the {\tt brev.b} {\tt grevi}-pseudoinstructions.