1 


2 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
3 
% 
% 
4 
% Copyright (c) 20032008 by University of Queensland 
% Copyright (c) 20032009 by University of Queensland 
5 
% Earth Systems Science Computational Center (ESSCC) 
% Earth Systems Science Computational Center (ESSCC) 
6 
% http://www.uq.edu.au/esscc 
% http://www.uq.edu.au/esscc 
7 
% 
% 
15 
\section{RayleighTaylor Instability} 
\section{RayleighTaylor Instability} 
16 
\label{LEVELSET CHAP} 
\label{LEVELSET CHAP} 
17 


18 
In this chapter we will implement the Level Set Method in Escript for tracking the interface between two fluids for Computational Fluid Dynamics (CFD). The method is tested with a RayleighTaylor Instability problem, which is an instability of the interface between two fluids with differing densities. \\ 
In this section we will implement the Level Set Method in Escript for tracking the interface between two fluids for Computational Fluid Dynamics (CFD). The method is tested with a RayleighTaylor Instability problem, which is an instability of the interface between two fluids with differing densities. \\ 
19 
Normally in Earth science problems two or more fluids in a system with different properties are of interest. For example, lava dome growth in volcanology, with the contrast of the two mediums as being lava and air. The interface between the two mediums is often referred to as a free surface (free boundary value problem); the problem arises due to the large differences in densities between the lava and air, with their ratio being around 2000, and so the interface between the two fluids move with respect to each other. 
Normally in Earth science problems two or more fluids in a system with different properties are of interest. For example, lava dome growth in volcanology, with the contrast of the two mediums as being lava and air. The interface between the two mediums is often referred to as a free surface (free boundary value problem); the problem arises due to the large differences in densities between the lava and air, with their ratio being around 2000, and so the interface between the two fluids move with respect to each other. 
20 
%and so the lava with the much higher density is able to move independently with respect to the air, and the interface between the two fluids is not constrained. 
%and so the lava with the much higher density is able to move independently with respect to the air, and the interface between the two fluids is not constrained. 
21 
There are a number of numerical techniques to define and track the free surfaces. One of these methods, which is conceptually the simplest, is to construct a Lagrangian grid which moves with the fluid, and so it tracks the free surface. The limitation of this method is that it cannot track surfaces that break apart or intersect. Another limitation is that the elements in the grid can become severely distorted, resulting in numerical instability. The Arbitrary LagrangianEulerian (ALE) method for CFD in moving domains is used to overcome this problem by remeshing, but there is an overhead in computational time, and it results in a loss of accuracy due to the process of mapping the state variables every remesh by interpolation. 
There are a number of numerical techniques to define and track the free surfaces. One of these methods, which is conceptually the simplest, is to construct a Lagrangian grid which moves with the fluid, and so it tracks the free surface. The limitation of this method is that it cannot track surfaces that break apart or intersect. Another limitation is that the elements in the grid can become severely distorted, resulting in numerical instability. The Arbitrary LagrangianEulerian (ALE) method for CFD in moving domains is used to overcome this problem by remeshing, but there is an overhead in computational time, and it results in a loss of accuracy due to the process of mapping the state variables every remesh by interpolation. 
24 
% 
% 
25 
\begin{figure} 
\begin{figure} 
26 
\center 
\center 
27 
\scalebox{0.7}{\includegraphics{figures/unitcircle.eps}} 
\scalebox{0.8}{\includegraphics{figures/unitcircle}} 
28 
\caption{Implicit representation of the curve $x^2 + y^2 = 1$.} 
\caption{Implicit representation of the curve $x^2 + y^2 = 1$.} 
29 
\label{UNITCIRCLE} 
\label{UNITCIRCLE} 
30 
\end{figure} 
\end{figure} 
41 
\label{ADVECTION} 
\label{ADVECTION} 
42 
\end{equation} 
\end{equation} 
43 
% 
% 
44 
where $\vec{v}$ is the velocity field. The advection equation is solved using a midpoint method, which is a two step procedure: 
where $\vec{v}$ is the velocity field. The advection equation is solved using a midpoint, which is a two step procedure: 
45 


46 
Firstly, $\phi^{1/2}$ is calculated solving: 
Firstly, $\phi^{1/2}$ is calculated solving: 
47 
% 
% 
57 
\label{MIDPOINT SECOND} 
\label{MIDPOINT SECOND} 
58 
\end{equation} 
\end{equation} 
59 
% 
% 
60 
For more details on the midpoint procedure see reference \cite{BOURGOUIN2006}. In certain situations the midpoint procedure has been shown to produce artifacts in the numerical solutions. A more robust procedure is to use the TaylorGalerkin scheme with the presence of diffusion, which gives more stable solutions. The expression is derived by either inserting Equation (\ref{MIDPOINT FIST}) into Equation (\ref{MIDPOINT SECOND}), or by expanding $\phi$ into a Taylor series: 
This procedure works provided that the discretization of the lefthand side of Equations (\ref{MIDPOINT FIST}) and (\ref{MIDPOINT SECOND}) is a lumped mass matrix. For more details on the midpoint procedure see reference \cite{BOURGOUIN2006}. In certain situations the midpoint procedure has been shown to produce artifacts in the numerical solutions. A more robust procedure is to use the TaylorGalerkin scheme with the presence of diffusion, which gives more stable solutions. The expression is derived by either inserting Equation (\ref{MIDPOINT FIST}) into Equation (\ref{MIDPOINT SECOND}), or by expanding $\phi$ into a Taylor series: 
61 
% 
% 
62 
\begin{equation} 
\begin{equation} 
63 
\phi^{+} \simeq \phi^{} + dt\frac{\partial \phi^{}}{\partial t} + \frac{dt^2}{2}\frac{\partial^{2}\phi^{}}{\partial t^{2}}, 
\phi^{+} \simeq \phi^{} + dt\frac{\partial \phi^{}}{\partial t} + \frac{dt^2}{2}\frac{\partial^{2}\phi^{}}{\partial t^{2}}, 
86 
\end{equation} 
\end{equation} 
87 


88 


89 
\subsection{Governing Equations for Fluid Flow} 
%\subsection{Governing Equations for Fluid Flow} 
90 


91 
The fluid dynamics is governed by the Stokes equations. In geophysical problems the velocity of fluids are low; that is, the inertial forces are small compared with the viscous forces, therefore the inertial terms in the NavierStokes equations can be ignored. For a body force $f$ the governing equations are given by: 
%The fluid dynamics is governed by the Stokes equations. In geophysical problems the velocity of fluids are low; that is, the inertial forces are small compared with the viscous forces, therefore the inertial terms in the NavierStokes equations can be ignored. For a body force $f$ the governing equations are given by: 
92 

% 
93 

%\begin{equation} 
94 

%\nabla \cdot (\eta(\nabla \vec{v} + \nabla^{T} \vec{v}))  \nabla p = f, 
95 

%\label{GENERAL NAVIER STOKES} 
96 

%\end{equation} 
97 

% 
98 

%with the incompressibility condition 
99 

% 
100 

%\begin{equation} 
101 

%\nabla \cdot \vec{v} = 0. 
102 

%\label{INCOMPRESSIBILITY} 
103 

%\end{equation} 
104 

% 
105 

%where $p$, $\eta$ and $f$ are the pressure, viscosity and body forces, respectively. 
106 

%Alternatively, the Stokes equations can be represented in Einstein summation tensor notation (compact notation): 
107 

% 
108 

%\begin{equation} 
109 

%(\eta(v\hackscore{i,j} + v\hackscore{j,i})),\hackscore{j}  p,\hackscore{i} = f\hackscore{i}, 
110 

%\label{GENERAL NAVIER STOKES COM} 
111 

%\end{equation} 
112 

% 
113 

%with the incompressibility condition 
114 

% 
115 

%\begin{equation} 
116 

%v\hackscore{i,i} = 0. 
117 

%\label{INCOMPRESSIBILITY COM} 
118 

%\end{equation} 
119 

% 
120 

%The subscript comma $i$ denotes the derivative of the function with respect to $x\hackscore{i}$. A linear relationship between the deviatoric stress $\sigma^{'}\hackscore{ij}$ and the stretching $D\hackscore{ij} = \frac{1}{2}(v\hackscore{i,j} + v\hackscore{j,i})$ is defined as \cite{GROSS2006}: 
121 

% 
122 

%\begin{equation} 
123 

%\sigma^{'}\hackscore{ij} = 2\eta D^{'}\hackscore{ij}, 
124 

%\label{STRESS} 
125 

%\end{equation} 
126 

% 
127 

%where the deviatoric stretching $D^{'}\hackscore{ij}$ is defined as 
128 

% 
129 

%\begin{equation} 
130 

%D^{'}\hackscore{ij} = D^{'}\hackscore{ij}  \frac{1}{3}D\hackscore{kk}\delta\hackscore{ij}. 
131 

%\label{DEVIATORIC STRETCHING} 
132 

%\end{equation} 
133 

% 
134 

%where $\delta\hackscore{ij}$ is the Kronecker $\delta$symbol, which is a matrix with ones for its diagonal entries ($i = j$) and zeros for the remaining entries ($i \neq j$). The body force $f$ in Equation (\ref{GENERAL NAVIER STOKES COM}) is the gravity acting in the $x\hackscore{3}$ direction and is given as $f = g \rho \delta\hackscore{i3}$. 
135 

%The Stokes equations is a saddle point problem, and can be solved using a Uzawa scheme. A class called StokesProblemCartesian in Escript can be used to solve for velocity and pressure. 
136 

%In order to keep numerical stability, the timestep size needs to be below a certain value, known as the Courant number. The Courant number is defined as: 
137 

% 
138 

%\begin{equation} 
139 

%C = \frac{v \delta t}{h}. 
140 

%\label{COURANT} 
141 

%\end{equation} 
142 

% 
143 

%where $\delta t$, $v$, and $h$ are the timestep, velocity, and the width of an element in the mesh, respectively. The velocity $v$ may be chosen as the maximum velocity in the domain. In this problem the Courant number is taken to be 0.4 \cite{BOURGOUIN2006}. 
144 


145 


146 

\subsection{Reinitialization of Interface} 
147 


148 

As the computation of the distance function progresses, it becomes distorted, and so it needs to be updated in order to stay regular \cite{SUSSMAN1994}. This process is known as the reinitialization procedure. The aim is to iteratively find a solution to the reinitialization equation: 
149 
% 
% 
150 
\begin{equation} 
\begin{equation} 
151 
\nabla \cdot (\eta(\nabla \vec{v} + \nabla^{T} \vec{v}))  \nabla p = f, 
\frac{\partial \psi}{\partial \tau} + sign(\phi)(1  \nabla \psi) = 0. 
152 
\label{GENERAL NAVIER STOKES} 
\label{REINITIALISATION} 
153 
\end{equation} 
\end{equation} 
154 
% 
% 
155 
with the incompressibility condition 
where $\psi$ shares the same level set with $\phi$, $\tau$ is pseudo time, and $sign(\phi)$ is the smoothed sign function. This equation is solved to meet the definition of the level set function, $\lvert \nabla \psi \rvert = 1$; the normalization condition. Equation (\ref{REINITIALISATION}) can be rewritten in similar form to the advection equation: 
156 
% 
% 
157 
\begin{equation} 
\begin{equation} 
158 
\nabla \cdot \vec{v} = 0. 
\frac{\partial \psi}{\partial \tau} + \vec{w} \cdot \nabla \psi = sign(\phi), 
159 
\label{INCOMPRESSIBILITY} 
\label{REINITIALISATION2} 
160 
\end{equation} 
\end{equation} 
161 
% 
% 
162 
where $p$, $\eta$ and $f$ are the pressure, viscosity and body forces, respectively. 
where 

Alternatively, the Stokes equations can be represented in Einstein summation tensor notation (compact notation): 

163 
% 
% 
164 
\begin{equation} 
\begin{equation} 
165 
(\eta(v\hackscore{i,j} + v\hackscore{j,i})),\hackscore{j}  p,\hackscore{i} = f\hackscore{i}, 
\vec{w} = sign(\phi)\frac{\nabla \psi}{\nabla \psi}. 
166 
\label{GENERAL NAVIER STOKES COM} 
\label{REINITIALISATION3} 
167 
\end{equation} 
\end{equation} 
168 
% 
% 
169 
with the incompressibility condition 
$\vec{w}$ is the characteristic velocity pointing outward from the free surface. Equation (\ref{REINITIALISATION2}) can be solved by a similar technique to what was used in the advection step; either by the midpoint technique \cite{BOURGOUIN2006} or the TaylorGalerkin procedure. For the midpoint technique, the reinitialization technique algorithm is: 
170 


171 

1. Calculate 
172 
% 
% 
173 
\begin{equation} 
\begin{equation} 
174 
v\hackscore{i,i} = 0. 
\vec{w} = sign(\phi)\frac{\nabla \psi}{\nabla \psi}, 
175 
\label{INCOMPRESSIBILITY COM} 
\label{REINITIAL MIDPOINT1} 
176 
\end{equation} 
\end{equation} 
177 
% 
% 
178 
The subscript comma $i$ denotes the derivative of the function with respect to $x\hackscore{i}$. A linear relationship between the deviatoric stress $\sigma^{'}\hackscore{ij}$ and the stretching $D\hackscore{ij} = \frac{1}{2}(v\hackscore{i,j} + v\hackscore{j,i})$ is defined as \cite{GROSS2006}: 

179 

2. Calculate $\psi^{1/2}$ solving 
180 
% 
% 
181 
\begin{equation} 
\begin{equation} 
182 
\sigma^{'}\hackscore{ij} = 2\eta D^{'}\hackscore{ij}, 
\frac{\psi^{1/2}  \psi^{}}{d\tau/2} + \vec{w} \cdot \nabla \psi^{}= sign(\phi), 
183 
\label{STRESS} 
\label{REINITIAL MIDPOINT2} 
184 
\end{equation} 
\end{equation} 
185 
% 
% 
186 
where the deviatoric stretching $D^{'}\hackscore{ij}$ is defined as 

187 

3. using $\psi^{1/2}$, calculate $\psi^{+}$ solving 
188 
% 
% 
189 
\begin{equation} 
\begin{equation} 
190 
D^{'}\hackscore{ij} = D^{'}\hackscore{ij}  \frac{1}{3}D\hackscore{kk}\delta\hackscore{ij}. 
\frac{\psi^{+}  \psi^{}}{d\tau} + \vec{w} \cdot \nabla \psi^{1/2}= sign(\phi), 
191 
\label{DEVIATORIC STRETCHING} 
\label{REINITIAL MIDPOINT3} 
192 
\end{equation} 
\end{equation} 
193 
% 
% 
194 
where $\delta\hackscore{ij}$ is the Kronecker $\delta$symbol, which is a matrix with ones for its diagonal entries ($i = j$) and zeros for the remaining entries ($i \neq j$). The body force $f$ in Equation (\ref{GENERAL NAVIER STOKES COM}) is the gravity acting in the $x\hackscore{3}$ direction and is given as $f = g \rho \delta\hackscore{i3}$. 

195 
The Stokes equations is a saddle point problem, and can be solved using a Uzawa scheme. A class called StokesProblemCartesian in Escript can be used to solve for velocity and pressure. 
4. if the convergence criterion has not been met, go back to step 2. Convergence is declared if 

In order to keep numerical stability, the timestep size needs to be below a certain value, known as the Courant number. The Courant number is defined as: 

196 
% 
% 
197 
\begin{equation} 
\begin{equation} 
198 
C = \frac{v \delta t}{h}. 
\nabla \psi \hackscore{\infty}  1 < \epsilon \hackscore{\psi}. 
199 
\label{COURANT} 
\label{REINITIAL CONVERGE} 
200 
\end{equation} 
\end{equation} 
201 
% 
% 
202 
where $\delta t$, $v$, and $h$ are the timestep, velocity, and the width of an element in the mesh, respectively. The velocity $v$ may be chosen as the maximum velocity in the domain. In this problem the Courant number is taken to be 0.4 \cite{BOURGOUIN2006}. 
where $\epsilon \hackscore{\psi}$ is the convergence tolerance. Normally, the reinitialization procedure is performed every third timestep of solving the Stokes equation. 







\subsection{Reinitialization of Interface} 

203 


204 
As the computation of the distance function progresses, it becomes distorted, and so it needs to be updated in order to stay regular. This process is known as the reinitialization procedure. The aim is to iteratively find a solution to the reinitialization equation: 
The midpoint technique works provided that the lefthand side of Equations (\ref{REINITIAL MIDPOINT2}) and (\ref{REINITIAL MIDPOINT3}) is a lumped mass matrix. Alternatively, for a onestep procedure, the reinitialization equation can be given by: 
205 
% 
% 
206 
\begin{equation} 
\begin{equation} 
207 
\frac{\partial \psi}{\partial \tau} + sign(\psi)(1  \nabla \psi) = 0. 
\psi^{+} = \psi^{}  \tau \vec{w} \cdot \nabla \psi^{} + \frac{d \tau^{2}}{2} \vec{w} \cdot \nabla(\vec{w} \cdot \nabla \psi^{}). 
208 
\label{REINITIALISATION} 
\label{REINITIAL ONESTEP} 
209 
\end{equation} 
\end{equation} 
210 
% 
% 
211 
where $\tau$ is artificial time. This equation is solved to meet the definition of the level set function, $\lvert \nabla \psi \rvert = 1$; the normalization condition. However, it has been shown that in using this reinitialization procedure it is prone to mass loss and inconsistent positioning of the interface \cite{SUCKALE2008}. 
The accuracy of $\phi$ is only needed within the transition zone; and so it can be calculated in a narrow band between the interface of the fluids. 
212 

% 
213 

\begin{figure} 
214 

\center 
215 

\scalebox{0.5}{\includegraphics{figures/LevelSetFlowChart}} 
216 

\caption{Flow chart of Level Set Method procedure \cite{LIN2005}.} 
217 

\label{LEVELSET FLOWCHART} 
218 

\end{figure} 
219 

% 
220 

When the distance function, $\phi$, is calculated, the physical parameters, density and viscosity, are updated using the sign of $\phi$. The jump in material properties between two fluids, such as air and water can be extreme, and so the transition of the properties from one medium to another is smoothed. The region of the interface is assumed to be of finite thickness of $\alpha h$, where $h$ is the size of the elements in the computational mesh and $\alpha$ is a smoothing parameter. The parameters are updated by the following expression: 
221 

% 
222 

\begin{equation} 
223 

P = 
224 

\left \{ \begin{array}{l} 
225 

P\hackscore{1} \hspace{5cm} where \ \ \psi <  \alpha h \\ 
226 

P\hackscore{2} \hspace{5cm} where \ \ \psi > \alpha h \\ 
227 

(P\hackscore{2}  P\hackscore{1}) \psi/2\alpha h + (P\hackscore{1} + P\hackscore{2})/2 \ \ \ \ \ \ where \ \ \psi < \alpha h. 
228 

\end{array} 
229 

\right. 
230 

\label{UPDATE PARAMETERS} 
231 

\end{equation} 
232 

% 
233 

where the subscripts $1$ and $2$ denote the different fluids. The procedure of the level set calculation is shown in Figure \ref{LEVELSET FLOWCHART}. 
234 

Further work is needed in the reinitialization procedure, as it has been shown that it is prone to mass loss and inconsistent positioning of the interface \cite{SUCKALE2008}. 
235 


236 
\subsection{Benchmark Problem} 
\subsection{Benchmark Problem} 
237 


238 
The RayleighTaylor instability problem is used as a benchmark to validate CFD implementations \cite{VANKEKEN1997}. Figure \ref{RT2DSETUP} shows the setup of the problem. A rectangular domain with two different fluids is considered, with the greater density fluid on the top and the lighter density fluid on the bottom. The viscosities of the two fluids are equal (isoviscos). An initial perturbation is given to the interface of $\phi=0.02cos(\frac{\pi x}{\lambda}) + 0.2$. The aspect ratio, $\lambda = L/H = 0.9142$, is chosen such that it gives the greatest disturbance of the fluids. 
The RayleighTaylor instability problem is used as a benchmark to validate CFD implementations \cite{VANKEKEN1997}. Figure \ref{RT2DSETUP} shows the setup of the problem. A rectangular domain with two different fluids is considered, with the greater density fluid on the top and the lighter density fluid on the bottom. The viscosities of the two fluids are equal (isoviscous). An initial perturbation is given to the interface of $\phi=0.02cos(\frac{\pi x}{\lambda}) + 0.2$. The aspect ratio $\lambda = L/H = 0.9142$ is chosen such that it gives the greatest disturbance of the fluids. The fluid properties is chosen such that the compositional Rayleigh number is equal to one: 
239 

% 
240 

\begin{equation} 
241 

R\hackscore{b} = \frac{\Delta \rho H^{3}}{\kappa \eta} = 1. 
242 

\label{RAYLEIGH NUMBER} 
243 

\end{equation} 
244 

% 
245 

where $\Delta \rho$ is the difference in density between the two fluids, $\eta$ is the viscosity and $\kappa$ is the thermal diffusivity; arbitrarily taken equal to 1 for a ``non thermal'' case. 
246 

% 
247 
% 
% 
248 
\begin{figure} 
\begin{figure} 
249 
\center 
\center 
250 
\scalebox{0.7}{\includegraphics{figures/RT2Dsetup.eps}} 
\scalebox{0.7}{\includegraphics{figures/RT2Dsetup}} 
251 
\caption{Parameters, initial interface and boundary conditions for the RayleighTaylor instability problem. The interface is defined as $\phi=0.02cos(\frac{\pi x}{\lambda}) + 0.2$. The fluids have been assigned different densities and equal viscosity (isovisous).} 
\caption{Parameters, initial interface and boundary conditions for the RayleighTaylor instability problem. The interface is defined as $\phi=0.02cos(\frac{\pi x}{\lambda}) + 0.2$. The fluids have been assigned different densities and equal viscosity (isoviscous) \cite{BOURGOUIN2006}.} 
252 
\label{RT2DSETUP} 
\label{RT2DSETUP} 
253 
\end{figure} 
\end{figure} 
254 

% 
255 

% 
256 

The following PYTHON code is for the RayleighTaylor instability problem, which is available in the example directory as 'RT2D.py'. This script uses the 'StokesProblemCartesian' class for solving the Stokes equation, along with the incompressibility condition. A class called 'LevelSet' is also used, which performs the advection and reinitialization procedures to track the movement of the interface of the fluids. The details and use of these classes are described in Chapter \ref{MODELS CHAPTER} (Models Chapter). 
257 


258 

The script starts off by importing the necessary classes. The physical properties of the two fluids are defined, such as density and viscosity. Acceleration due to gravity is taken as 10.0 $ms^{2}$. Solver settings are set for solving the Stokes problem, with the number of timesteps, solver tolerance, maximum solver iterations, and the option to use the Uzawa scheme or not; the default solver is the PCG solver. A regular mesh is defined with 200$\times$200 elements. Level set parameters are set for the reinitialization procedure, such as the convergence tolerance, number of reinitialization steps, the frequency of the reinitialization, for example, every third timestep, and the smoothing parameter to smooth the physical properties across the interface. A noslip boundary condition is set for the top and bottom of the domain, while on the left and righthand sides there is a slip condition. The initial interface between the two fluids is defined as in Figure \ref{RT2DSETUP}. Instances of the StokesProblemCartesian and LevelSet class are created. The iteration throughout the timesteps involves the update of the physical parameters of the fluids; the initialization of the boundary conditions, viscosity, and body forces; the solving of the Stokes problem for velocity and pressure; then the level set procedure. The output of the level set function, velocity and pressure is saved to file. The timestep size is selected based on the Courant condition. Due to the number of elements in the computational mesh, the simulation may take a long time to complete on a desktop computer, so it is recommended to run it on the super computer. At present, the fine mesh is required to capture the details of the fluid motion and for numerical stability. 
259 

% 
260 

\begin{python} 
261 


262 

from esys.escript import * 
263 

import esys.finley 
264 

from esys.escript.models import StokesProblemCartesian 
265 

from esys.finley import finley 
266 

from esys.finley import Rectangle 
267 

from LevelSet import * 
268 


269 

#physical properties 
270 

rho1 = 1000 #fluid density on bottom 
271 

rho2 = 1010 #fluid density on top 
272 

eta1 = 100.0 #fluid viscosity on bottom 
273 

eta2 = 100.0 #fluid viscosity on top 
274 

g=10.0 
275 


276 

#solver settings 
277 

dt = 0.001 
278 

t_step = 0 
279 

t_step_end = 2000 
280 

TOL = 1.0e5 
281 

max_iter=400 
282 

verbose=True 
283 

useUzawa=True 
284 


285 

#define mesh 
286 

l0=0.9142 
287 

l1=1.0 
288 

n0=200 
289 

n1=200 
290 


291 

mesh=Rectangle(l0=l0, l1=l1, order=2, n0=n0, n1=n1) 
292 

#get mesh dimensions 
293 

numDim = mesh.getDim() 
294 

#get element size 
295 

h = Lsup(mesh.getSize()) 
296 


297 

#level set parameters 
298 

tolerance = 1.0e6 
299 

reinit_max = 30 
300 

reinit_each = 3 
301 

alpha = 1 
302 

smooth = alpha*h 
303 


304 

#boundary conditions 
305 

x = mesh.getX() 
306 

#left + bottom + right + top 
307 

b_c = whereZero(x[0])*[1.0,0.0] + whereZero(x[1])*[1.0,1.0] + whereZero(x[0]l0)*[1.0,0.0] \ 
308 

+ whereZero(x[1]l1)*[1.0,1.0] 
309 


310 

velocity = Vector(0.0, ContinuousFunction(mesh)) 
311 

pressure = Scalar(0.0, ContinuousFunction(mesh)) 
312 

Y = Vector(0.0,Function(mesh)) 
313 


314 

#define initial interface between fluids 
315 

xx = mesh.getX()[0] 
316 

yy = mesh.getX()[1] 
317 

func = Scalar(0.0, ContinuousFunction(mesh)) 
318 

h_interface = Scalar(0.0, ContinuousFunction(mesh)) 
319 

h_interface = h_interface + (0.02*cos(math.pi*xx/l0) + 0.2) 
320 

func = yy  h_interface 
321 

func_new = func.interpolate(ReducedSolution(mesh)) 
322 


323 

#Stokes Cartesian 
324 

solution=StokesProblemCartesian(mesh,debug=True) 
325 

solution.setTolerance(TOL) 
326 

solution.setSubProblemTolerance(TOL**2) 
327 


328 

#level set 
329 

levelset = LevelSet(mesh, func_new, reinit_max, reinit_each, tolerance, smooth) 
330 


331 

while t_step <= t_step_end: 
332 

#update density and viscosity 
333 

rho = levelset.update_parameter(rho1, rho2) 
334 

eta = levelset.update_parameter(eta1, eta2) 
335 


336 

#get velocity and pressure of fluid 
337 

Y[1] = rho*g 
338 

solution.initialize(fixed_u_mask=b_c,eta=eta,f=Y) 
339 

velocity,pressure=solution.solve(velocity,pressure,max_iter=max_iter,verbose=verbose, \ 
340 

useUzawa=useUzawa) 
341 


342 

#update the interface 
343 

func = levelset.update_phi(velocity, dt, t_step) 
344 


345 

print "##########################################################" 
346 

print "time step:", t_step, " completed with dt:", dt 
347 

print "Velocity: min =", inf(velocity), "max =", Lsup(velocity) 
348 

print "##########################################################" 
349 


350 

#save interface, velocity and pressure 
351 

saveVTK("phi2D.%2.4i.vtu"%t_step,interface=func,velocity=velocity,pressure=pressure) 
352 

#Courant condition 
353 

dt = 0.4*Lsup(mesh.getSize())/Lsup(velocity) 
354 

t_step += 1 
355 


356 

\end{python} 
357 

% 
358 

% 
359 

The results from the simulation can be viewed by visualization software such as \textit{visIt}. If the software is installed, it can be opened by simply executing the following command: 
360 

% 
361 

\begin{python} 
362 

visit 
363 

\end{python} 
364 

% 
365 

In the visIt main window, vtk/vtu files can be opened from the File menu; contours and vectors can then be displayed by selecting them from the Plots menu and pressing the Draw button. A movie of the simulation can be watched by pressing the Play button. The graphics are displayed in the Vis window. For more information on \textit{visIt} see the website \cite{VisIt}. 
366 


367 

The simulation output is shown in Figures \ref{RT2D OUTPUT1} and \ref{RT2D OUTPUT1} showing the progression of the interface of the two fluids. A diapir can be seen rising on the lefthand side of the domain, and then later on, a second one rises on the righthand side. 
368 

\begin{figure} 
369 

\center 
370 

\subfigure[t=300]{\label{RT OUTPUT300}\includegraphics[scale=0.252]{figures/RT2D200by200t300}} 
371 

\subfigure[t=600]{\label{RT OUTPUT600}\includegraphics[scale=0.252]{figures/RT2D200by200t600}} 
372 

\subfigure[t=900]{\label{RT OUTPUT900}\includegraphics[scale=0.252]{figures/RT2D200by200t900}} 
373 

\subfigure[t=1200]{\label{RT OUTPUT1200}\includegraphics[scale=0.252]{figures/RT2D200by200t1200}} 
374 

\caption{Simulation output of RayleighTaylor instability, showing the movement of the interface of the fluids. The contour line represents the interface between the two fluids; the zero contour of the Level Set function. Velocity vectors are displayed showing the flow field. Computational mesh used was 200$\times$200 elements.} 
375 

\label{RT2D OUTPUT1} 
376 

\end{figure} 
377 

% 
378 

\begin{figure} 
379 

\center 
380 

\subfigure[t=1500]{\label{RT OUTPUT1500}\includegraphics[scale=0.252]{figures/RT2D200by200t1500}} 
381 

\subfigure[t=1800]{\label{RT OUTPUT1800}\includegraphics[scale=0.252]{figures/RT2D200by200t1800}} 
382 

\caption{Simulation output of RayleighTaylor instability.} 
383 

\label{RT2D OUTPUT2} 
384 

\end{figure} 
385 

% 
386 

% 
387 
%The Level Set Method can be applied to many areas of science, for example simulating subduction zones in geophysics, motion of bubbles, and flame propagation. Its also used in image processing. However, the Level Set Method does have limitations. The level set function can still become irregular after reinitialisation, leading to artifacts in the simulations, requiring more thought into the implementation of the reinitialisation step. 
%The Level Set Method can be applied to many areas of science, for example simulating subduction zones in geophysics, motion of bubbles, and flame propagation. Its also used in image processing. However, the Level Set Method does have limitations. The level set function can still become irregular after reinitialisation, leading to artifacts in the simulations, requiring more thought into the implementation of the reinitialisation step. 
388 

% 