\documentclass[11pt]{article}
\usepackage{fullpage,amsmath,amsfonts,mathpazo,microtype,nicefrac,algorithm2e,graphicx,siunitx}

% Set-up for hypertext references
\usepackage{hyperref,color,textcomp}
\definecolor{webgreen}{rgb}{0,.35,0}
\definecolor{webbrown}{rgb}{.6,0,0}
\definecolor{RoyalBlue}{rgb}{0,0,0.9}
\hypersetup{
   colorlinks=true, linktocpage=true, pdfstartpage=3, pdfstartview=FitV,
   breaklinks=true, pdfpagemode=UseNone, pageanchor=true, pdfpagemode=UseOutlines,
   plainpages=false, bookmarksnumbered, bookmarksopen=true, bookmarksopenlevel=1,
   hypertexnames=true, pdfhighlight=/O,
   urlcolor=webbrown, linkcolor=RoyalBlue, citecolor=webgreen,
   pdfauthor={Chris H. Rycroft},
   pdfsubject={Harvard AM225 (Spring 2021)},
   pdfkeywords={},
   pdfcreator={pdfLaTeX},
   pdfproducer={LaTeX with hyperref}
}
\hypersetup{pdftitle={AM225: Assignment 5}}
\usepackage[font=footnotesize,skip=3pt]{caption}

\graphicspath{ {am225_hw4_files/} {./} }

% Macro definitions
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\p}{\partial}
\renewcommand{\vec}[1]{\mathbf{#1}}
\newcommand{\vx}{\vec{x}}
\newcommand{\vu}{\vec{u}}
\newcommand{\vp}{\vec{p}}
\newcommand{\vc}{\vec{c}}
\newcommand{\bxi}{\boldsymbol\xi}
\newcommand{\rs}{\sqrt{6}}
\newcommand{\calC}{\mathcal{C}}
\newcommand{\calF}{\mathcal{F}}
\newcommand{\Atol}{\textit{Atol}}
\newcommand{\Rtol}{\textit{Rtol}}
\newcommand{\Trans}{\mathsf{T}}
\newcommand{\sep}{\,:\,}

\DeclareMathOperator{\atantwo}{atan2}

\renewcommand{\topfraction}{0.9}
\renewcommand{\bottomfraction}{0.8}
\renewcommand{\textfraction}{0.2}
\renewcommand{\floatpagefraction}{0.7}
\renewcommand{\dblfloatpagefraction}{0.7}

\begin{document}
\section*{AM225: Assignment 5 (due 5~PM, April 30)}
\begin{enumerate}
  \item \textbf{Simulating paper marbling.} Figure~\ref{fig:pmarble} shows
    an example of paper marbling, a type of craft activity for making colorful
    swirling designs. Paper marbling is done by filling a shallow tray with
    water, adding layers of oil-based paints to the surface, and then using the
    water motion to mix and deform the paint colors. After this, a sheet of
    paper is laid on top of the tray, to transfer the paint pattern. Here are
    some Youtube videos showing paper marbling in action:
    \href{https://www.youtube.com/watch?v=NqedBekgLdo}{A},
    \href{https://www.youtube.com/watch?v=T3qcCoJ70XY}{B},
    \href{https://www.youtube.com/watch?v=NOy8X7MR0iI}{C},
    \href{https://www.youtube.com/watch?v=aKDiONucSyg}{D}.
    \textbf{The goal of this question is to produce your own paper marbling
    pattern, by extending the incompressible fluid simulation that was
    introduced in the lectures.} The end result should roughly resemble
    Fig.~\ref{fig:pmarble}.

    As discussed the lectures, the simulation code in
    \texttt{am225\_examples/5a\_fluid\_sim} simulates the Navier--Stokes
    equations
    \begin{equation}
      \rho\left( \frac{\p \vu}{\p t} + (\vu \cdot \nabla) \vu\right) = - \nabla p +
      \mu \nabla^2 \vu \label{eq:ns}
    \end{equation}
    subject to the incompressibility constraint
    \begin{equation}
      \nabla \cdot \vu =0.\label{eq:incomp}
    \end{equation}
    Here $\vu(\vx,t)$ is the velocity, $p(\vx,t)$ is the pressure, $\rho$ is
    fluid density, and $\mu$ is the dynamic viscosity. To begin, extend the
    fluid simulation code so that it also simulates a three-component vector
    field $\vc(\vx,t) = (R(\vx,t),G(\vx,t),B(\vx,t))$ representing the red,
    green, and blue color channels. The field values should be stored
    at the grid cell centers, in the same position as the velocity. Use the
    convention that 1 represents the maximum color channel value.\footnote{For
    example $\vc=(1,1,1)$ corresponds to white.} The vector field should
    satisfy the hyperbolic conservation
    law\footnote{Note that since $\nabla \cdot \vu =0$ this is equivalent to
    $\p c_j/\p t + \nabla\cdot( c_j \vu) = 0$ for each color channel $j$.}
    \begin{equation}
      \frac{\p \vc}{\p t} + (\vu \cdot \nabla) \vc = \vec{0}. \label{eq:col_cons}
    \end{equation}
    You can use any appropriate method to simulate Eq.~\ref{eq:col_cons} but
    one good choice is to use an explicit Euler step for the time derivative,
    and the upwinded ENO method for the spatial derivative. The fluid
    simulation code already contains routines to perform the ENO derivative for
    evaluating the $(\vu \cdot \nabla) \vu$ term appearing in Eq.~\ref{eq:ns},
    and thus you can co-opt this routine for your purposes.

    To visualize your results, an example program \texttt{wpng\_example.cc}
    is provided that will take a three-component 2D array and convert it
    to a PNG image.\footnote{As discussed in the lectures, this requires that
    you have \href{http://www.libpng.org}{libpng} installed. This is usually
    a standard package in package management systems.}
    \begin{enumerate}
      \item As a warm-up and validation of your code, consider the domain
	$[-1,1]^2$ with a $256\times 256$ grid using non-periodic boundary
	conditions and the default initial velocity in the code. Use the
	initial condition
	\begin{equation}
	  \vc(\vx,t) =
	  \begin{cases}
	    (1,1,1) & \qquad \text{if $\lfloor 6x+6\rfloor + \lfloor 6y+6\rfloor)$ is even,} \\
	    (0.2,0.4,0.9) & \qquad \text{otherwise.}
	  \end{cases}
	\end{equation}
	Here $\lfloor \cdot \rfloor$ is the
	\href{https://en.wikipedia.org/wiki/Floor_and_ceiling_functions}{floor
	function}. This corresponds to a blue and white checkerboard pattern.
	Simulate from $t=0$ to $t=0.5$ and make a PNG image of the color
	pattern at $t=0.5$.
      \item Simulate your own paper marbling pattern. Use a computational grid
	of $640\times 640$ or larger, and either periodic or non-periodic
	boundary conditions. A possible approach is as follows:
	\begin{itemize}
	  \item Initialize $\vc(\vx,0)$ to contain a simple pattern using several
	    colors.
	  \item Simulate a small duration of fluid flow to deform the colors.
	  \item Add another layer of colors to the $\vc$ field, such as localized
	    circles or ellipses.
	  \item Simulate a second fluid flow to apply additional mixing.
	\end{itemize}
	Note that you may wish to avoid too much distortion in the fluid flow,
	which will blur the colors into each other. However, some mixing is
	inevitable. Increasing the resolution will reduce the amount of
	blurring. The fluid code is quite efficient, and you only need to run
	for a short simulation duration. Hence it is reasonable that you use a
	$1024\times 1024$ grid or larger if you wish.

	Chris encourages you to think about aesthetically pleasing colors and
	designs. Chris plans to make a montage of the submitted patterns, to be
	posted in the homework solutions.\footnote{The patterns in the montage
	will be anonymous. If for any reason you wish to opt out of this,
	please indicate this on your submitted solutions.}
      \item \textbf{Optional.} If you watch the Youtube videos, you will notice
	that as a droplet of paint is added, it spreads out and pushes the
	surrouding colors away. A way to approximately model the injection of
	paint at a location is to change Eq.~\ref{eq:incomp} to
	\begin{equation}
	  \nabla \cdot \vu = \alpha \delta( \vx-\vx_c) - \frac{\alpha}A \label{eq:modincomp}
	\end{equation}
	where $\delta$ is the Dirac delta function, $\alpha$ is a constant, and
	$A$ is the size of the simulation domain. The second term in
	Eq.~\ref{eq:modincomp} ensures that the total amount of fluid is
	conserved.\footnote{Note that if the total amount of fluid isn't
	conserved, the elliptic problem is unsolvable.} Modify the code to
	simulate this process. Rather than use a perfect delta function, you
	should spread its influence over a small number of grid cells. The
	$\vc$ field should be fixed to the injected paint color on these grid
	cells.
      \item \textbf{Optional.} As mentioned above you will likely observe some
	blurring of color boundaries, even when using the ENO method. Chris and
	his research group have recently been developing a simulation method
    called the \textit{reference map technique},\footnote{C.~H.~Rycroft,
    C.-H.~Wu, Y.~Yu, and K.~Kamrin, J. Fluid Mech. \textbf{898}, A9 (2021).
  \href{https://people.seas.harvard.edu/~chr/publish/JFM2020_Rycroft.pdf}{[Link]}}
    which provides one approach for mitigating this issue. Rather than
	advect the colors directly, we instead introduce a two-component vector
	field $\bxi(\vx,t)$ called the \textit{reference map}. This field is
	initialized to $\bxi(\vx,0)=\vx$, and satisfies the advection equation
	\begin{equation}
	  \frac{\p \bxi}{\p t} + (\vu \cdot \nabla ) \bxi = \vec{0}. \label{eq:refmap}
	\end{equation}
	Consider simulating the deformation of a color pattern from $t=0$ to
	$t=T$. The field $\bxi$ tracks the deformation of the fluid: the value
	of $\bxi(\vx,T)$ gives the position in the fluid at $t=0$ that is
	located at $\vx$ at $t=T$. Modify the code to simulate
	Eq.~\ref{eq:refmap}. instead of Eq.~\ref{eq:col_cons}. You will need to choose
	appropriate boundary conditions for $\bxi$. Once $\bxi(\vx,T)$ is
	known, the color at time $t=T$ can be found using
	\begin{equation}
	  \vc(\vx,T) = \vc(\bxi(\vx,T),0). \label{eq:col_update}
	\end{equation}
	Use Eq.~\ref{eq:col_update} to find the color at time $T$, and compare
	the amount of blurring to just using a direct solve of
	Eq.~\ref{eq:col_cons}.\footnote{At first glance, it may not be obvious
	why Eq.~\ref{eq:refmap} will minimize the blurring when compared to
	Eq.~\ref{eq:col_cons}, since both are transport equations. However,
	since $\bxi$ is a smooth field, it is much less susceptible to blurring
	than $\vc$. Hence, this procedure results in much better defined color
	boundaries.}
    \end{enumerate}
\end{enumerate}

\begin{figure}[b]
  \begin{center}
    \includegraphics[width=0.55\textwidth]{marbled_paper}
  \end{center}
  \caption{An example of a paper marbling pattern taken from a cover to the
  book \textit{Encyclop\ae{}dia Brittanica}. (Image from
  Wikipedia.)\label{fig:pmarble}}
\end{figure}
\end{document}
