1 
ksteube 
1811 

2 
jfenwick 
3989 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
3 
jfenwick 
6651 
% Copyright (c) 20032018 by The University of Queensland 
4 
jfenwick 
3989 
% http://www.uq.edu.au 
5 
lgraham 
1701 
% 
6 
ksteube 
1811 
% Primary Business: Queensland, Australia 
7 
jfenwick 
6112 
% Licensed under the Apache License, version 2.0 
8 


% http://www.apache.org/licenses/LICENSE2.0 
9 
lgraham 
1701 
% 
10 
jfenwick 
3989 
% Development until 2012 by Earth Systems Science Computational Center (ESSCC) 
11 
jfenwick 
4657 
% Development 20122013 by School of Earth Sciences 
12 


% Development from 2014 by Centre for Geoscience Computing (GeoComp) 
13 
jfenwick 
3989 
% 
14 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
15 
lgraham 
1701 

16 


\section{RayleighTaylor Instability} 
17 


\label{LEVELSET CHAP} 
18 



19 
lgraham 
2191 
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. \\ 
20 
lgraham 
1701 
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. 
21 


%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. 
22 


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. 
23 



24 
caltinay 
4285 
There is a technique to overcome these limitations called the Level Set Method, for tracking interfaces between two fluids. The advantages of the method is that CFD can be performed on a fixed Cartesian mesh, and therefore problems with remeshing can be avoided. The field equations for calculating variables such as velocity and pressure are solved on the same mesh. The Level Set Method is based upon the implicit representation of the interface by a continuous function. The function takes the form as a signed distance function, $\phi(x)$, of the interface in a Eulerian coordinate system. For example, the zero isocontour of the unit circle $\phi(x)=x^2 + y^2 1$ is the set of all points where $\phi(x)=0$. Refer to Figure \ref{UNITCIRCLE}. 
25 
lgraham 
1701 
% 
26 


\begin{figure} 
27 


\center 
28 
caltinay 
3279 
\scalebox{0.8}{\includegraphics{unitcircle}} 
29 
lgraham 
1701 
\caption{Implicit representation of the curve $x^2 + y^2 = 1$.} 
30 


\label{UNITCIRCLE} 
31 


\end{figure} 
32 


% 
33 
lgraham 
1973 
The implicit representation can be used to define the interior and exterior of a fluid region. Since the isocontour at $\phi(x)=0$ has been defined as the interface, a point in the domain can be determined if its inside or outside of the interface, by looking at the local sign of $\phi(x)$. For example, a point is inside the interface when $\phi(x)<0$, and outside the interface when $\phi(x)>0$. Parameters values such as density and viscosity can then be defined for two different mediums, depending on which side of the interface they are located. 
34 
lgraham 
1701 

35 
lgraham 
1973 

36 


\subsection{Calculation of the Displacement of the Interface} 
37 



38 


The displacement of the interface at the zero isocontour of $\phi(x)$ is calculated each timestep by using the velocity field. This is achieved my solving the advection equation: 
39 


% 
40 
lgraham 
1701 
\begin{equation} 
41 


\frac{\partial \phi}{\partial t} + \vec{v} \cdot \nabla \phi = 0, 
42 


\label{ADVECTION} 
43 


\end{equation} 
44 
lgraham 
1973 
% 
45 
lgraham 
2115 
where $\vec{v}$ is the velocity field. The advection equation is solved using a midpoint, which is a two step procedure: 
46 
lgraham 
1701 

47 


Firstly, $\phi^{1/2}$ is calculated solving: 
48 
lgraham 
1973 
% 
49 
lgraham 
1701 
\begin{equation} 
50 


\frac{\phi^{1/2}  \phi^{}}{dt/2} + \vec{v} \cdot \nabla \phi^{} = 0. 
51 


\label{MIDPOINT FIST} 
52 


\end{equation} 
53 
lgraham 
1973 
% 
54 
lgraham 
1701 
Secondly, using $\phi^{1/2}$, $\phi^{+}$ is calculated solving: 
55 
lgraham 
1973 
% 
56 
lgraham 
1701 
\begin{equation} 
57 


\frac{\phi^{+}  \phi^{}}{dt} + \vec{v} \cdot \nabla \phi^{1/2} = 0. 
58 


\label{MIDPOINT SECOND} 
59 


\end{equation} 
60 
lgraham 
1973 
% 
61 
lgraham 
2115 
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: 
62 
lgraham 
1973 
% 
63 
lgraham 
1701 
\begin{equation} 
64 


\phi^{+} \simeq \phi^{} + dt\frac{\partial \phi^{}}{\partial t} + \frac{dt^2}{2}\frac{\partial^{2}\phi^{}}{\partial t^{2}}, 
65 


\label{TAYLOR EXPANSION} 
66 


\end{equation} 
67 
lgraham 
1973 
% 
68 
lgraham 
1701 
by inserting 
69 
lgraham 
1973 
% 
70 
lgraham 
1701 
\begin{equation} 
71 


\frac{\partial \phi^{}}{\partial t} =  \vec{v} \cdot \nabla \phi^{}, 
72 


\label{INSERT ADVECTION} 
73 


\end{equation} 
74 
lgraham 
1973 
% 
75 
lgraham 
1701 
and 
76 
lgraham 
1973 
% 
77 
lgraham 
1701 
\begin{equation} 
78 


\frac{\partial^{2} \phi^{}}{\partial t^{2}} = \frac{\partial}{\partial t}(\vec{v} \cdot \nabla \phi^{}) = \vec{v}\cdot \nabla (\vec{v}\cdot \nabla \phi^{}), 
79 


\label{SECOND ORDER} 
80 


\end{equation} 
81 
lgraham 
1973 
% 
82 


into Equation (\ref{TAYLOR EXPANSION}) 
83 


% 
84 
lgraham 
1701 
\begin{equation} 
85 


\phi^{+} = \phi^{}  dt\vec{v}\cdot \nabla \phi^{} + \frac{dt^2}{2}\vec{v}\cdot \nabla (\vec{v}\cdot \nabla \phi^{}). 
86 


\label{TAYLOR GALERKIN} 
87 


\end{equation} 
88 



89 
lgraham 
1973 

90 
lgraham 
2191 
%\subsection{Governing Equations for Fluid Flow} 
91 
lgraham 
1973 

92 
lgraham 
2191 
%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: 
93 
lgraham 
1973 
% 
94 
lgraham 
2191 
%\begin{equation} 
95 


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


%\label{GENERAL NAVIER STOKES} 
97 


%\end{equation} 
98 
lgraham 
1973 
% 
99 
lgraham 
2193 
%with the incompressibility condition 
100 
lgraham 
1973 
% 
101 
lgraham 
2191 
%\begin{equation} 
102 


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


%\label{INCOMPRESSIBILITY} 
104 


%\end{equation} 
105 
lgraham 
1973 
% 
106 
lgraham 
2191 
%where $p$, $\eta$ and $f$ are the pressure, viscosity and body forces, respectively. 
107 


%Alternatively, the Stokes equations can be represented in Einstein summation tensor notation (compact notation): 
108 
lgraham 
1973 
% 
109 
lgraham 
2191 
%\begin{equation} 
110 
jfenwick 
3295 
%(\eta(v_{i,j} + v_{j,i})),_{j}  p,_{i} = f_{i}, 
111 
lgraham 
2191 
%\label{GENERAL NAVIER STOKES COM} 
112 


%\end{equation} 
113 
lgraham 
1973 
% 
114 
lgraham 
2193 
%with the incompressibility condition 
115 
lgraham 
1973 
% 
116 
lgraham 
2191 
%\begin{equation} 
117 
jfenwick 
3295 
%v_{i,i} = 0. 
118 
lgraham 
2191 
%\label{INCOMPRESSIBILITY COM} 
119 


%\end{equation} 
120 
lgraham 
1973 
% 
121 
jfenwick 
3295 
%The subscript comma $i$ denotes the derivative of the function with respect to $x_{i}$. A linear relationship between the deviatoric stress $\sigma^{'}_{ij}$ and the stretching $D_{ij} = \frac{1}{2}(v_{i,j} + v_{j,i})$ is defined as \cite{GROSS2006}: 
122 
lgraham 
1973 
% 
123 
lgraham 
2191 
%\begin{equation} 
124 
jfenwick 
3295 
%\sigma^{'}_{ij} = 2\eta D^{'}_{ij}, 
125 
lgraham 
2191 
%\label{STRESS} 
126 


%\end{equation} 
127 
lgraham 
1973 
% 
128 
jfenwick 
3295 
%where the deviatoric stretching $D^{'}_{ij}$ is defined as 
129 
lgraham 
1973 
% 
130 
lgraham 
2191 
%\begin{equation} 
131 
jfenwick 
3295 
%D^{'}_{ij} = D^{'}_{ij}  \frac{1}{3}D_{kk}\delta_{ij}. 
132 
lgraham 
2191 
%\label{DEVIATORIC STRETCHING} 
133 


%\end{equation} 
134 
lgraham 
1973 
% 
135 
jfenwick 
3295 
%where $\delta_{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_{3}$ direction and is given as $f = g \rho \delta_{i3}$. 
136 
lgraham 
2191 
%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. 
137 
gross 
3379 
%In order to keep numerical stability, the timestep size needs to be below a certain value, known as the Courant number \index{Courant number}\index{CFL condition}. The Courant number is defined as: 
138 
lgraham 
1973 
% 
139 
lgraham 
2191 
%\begin{equation} 
140 


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


%\label{COURANT} 
142 


%\end{equation} 
143 
lgraham 
1973 
% 
144 
lgraham 
2191 
%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}. 
145 
lgraham 
1701 

146 



147 
lgraham 
1973 
\subsection{Reinitialization of Interface} 
148 



149 
lgraham 
2115 
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: 
150 
lgraham 
1973 
% 
151 
lgraham 
1701 
\begin{equation} 
152 
lgraham 
2115 
\frac{\partial \psi}{\partial \tau} + sign(\phi)(1  \nabla \psi) = 0. 
153 
lgraham 
1701 
\label{REINITIALISATION} 
154 


\end{equation} 
155 
lgraham 
1973 
% 
156 
lgraham 
2115 
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: 
157 


% 
158 


\begin{equation} 
159 


\frac{\partial \psi}{\partial \tau} + \vec{w} \cdot \nabla \psi = sign(\phi), 
160 


\label{REINITIALISATION2} 
161 


\end{equation} 
162 


% 
163 


where 
164 


% 
165 


\begin{equation} 
166 


\vec{w} = sign(\phi)\frac{\nabla \psi}{\nabla \psi}. 
167 


\label{REINITIALISATION3} 
168 


\end{equation} 
169 


% 
170 


$\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: 
171 
lgraham 
1701 

172 
lgraham 
2115 
1. Calculate 
173 


% 
174 


\begin{equation} 
175 


\vec{w} = sign(\phi)\frac{\nabla \psi}{\nabla \psi}, 
176 


\label{REINITIAL MIDPOINT1} 
177 


\end{equation} 
178 


% 
179 
lgraham 
1973 

180 
lgraham 
2115 
2. Calculate $\psi^{1/2}$ solving 
181 


% 
182 


\begin{equation} 
183 


\frac{\psi^{1/2}  \psi^{}}{d\tau/2} + \vec{w} \cdot \nabla \psi^{}= sign(\phi), 
184 


\label{REINITIAL MIDPOINT2} 
185 


\end{equation} 
186 


% 
187 



188 


3. using $\psi^{1/2}$, calculate $\psi^{+}$ solving 
189 


% 
190 


\begin{equation} 
191 


\frac{\psi^{+}  \psi^{}}{d\tau} + \vec{w} \cdot \nabla \psi^{1/2}= sign(\phi), 
192 


\label{REINITIAL MIDPOINT3} 
193 


\end{equation} 
194 


% 
195 



196 


4. if the convergence criterion has not been met, go back to step 2. Convergence is declared if 
197 


% 
198 


\begin{equation} 
199 
jfenwick 
3295 
\nabla \psi _{\infty}  1 < \epsilon _{\psi}. 
200 
lgraham 
2115 
\label{REINITIAL CONVERGE} 
201 


\end{equation} 
202 


% 
203 
jfenwick 
3295 
where $\epsilon _{\psi}$ is the convergence tolerance. Normally, the reinitialization procedure is performed every third timestep of solving the Stokes equation. 
204 
lgraham 
2115 

205 


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: 
206 


% 
207 


\begin{equation} 
208 


\psi^{+} = \psi^{}  \tau \vec{w} \cdot \nabla \psi^{} + \frac{d \tau^{2}}{2} \vec{w} \cdot \nabla(\vec{w} \cdot \nabla \psi^{}). 
209 


\label{REINITIAL ONESTEP} 
210 


\end{equation} 
211 


% 
212 


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. 
213 


% 
214 


\begin{figure} 
215 


\center 
216 
caltinay 
3279 
\scalebox{0.5}{\includegraphics{LevelSetFlowChart}} 
217 
lgraham 
2193 
\caption{Flow chart of Level Set Method procedure \cite{LIN2005}.} 
218 
lgraham 
2115 
\label{LEVELSET FLOWCHART} 
219 


\end{figure} 
220 


% 
221 


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: 
222 


% 
223 


\begin{equation} 
224 


P = 
225 


\left \{ \begin{array}{l} 
226 
jfenwick 
3295 
P_{1} \hspace{5cm} where \ \ \psi <  \alpha h \\ 
227 


P_{2} \hspace{5cm} where \ \ \psi > \alpha h \\ 
228 


(P_{2}  P_{1}) \psi/2\alpha h + (P_{1} + P_{2})/2 \ \ \ \ \ \ where \ \ \psi < \alpha h. 
229 
lgraham 
2115 
\end{array} 
230 


\right. 
231 


\label{UPDATE PARAMETERS} 
232 


\end{equation} 
233 


% 
234 


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


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}. 
236 



237 
lgraham 
1973 
\subsection{Benchmark Problem} 
238 



239 
lgraham 
2128 
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: 
240 
lgraham 
1973 
% 
241 
lgraham 
2128 
\begin{equation} 
242 
jfenwick 
3295 
R_{b} = \frac{\Delta \rho H^{3}}{\kappa \eta} = 1. 
243 
lgraham 
2128 
\label{RAYLEIGH NUMBER} 
244 


\end{equation} 
245 


% 
246 


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. 
247 


% 
248 


% 
249 
lgraham 
1701 
\begin{figure} 
250 


\center 
251 
caltinay 
3279 
\scalebox{0.7}{\includegraphics{RT2Dsetup}} 
252 
lgraham 
2120 
\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}.} 
253 
lgraham 
1701 
\label{RT2DSETUP} 
254 


\end{figure} 
255 
lgraham 
2128 
% 
256 


% 
257 
lgraham 
2193 
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). 
258 
lgraham 
1701 

259 
jfenwick 
2738 
The script starts off by importing the necessary classes. The physical properties of the two fluids are defined, such as density and viscosity. 
260 


Acceleration due to gravity is taken as 10.0 $ms^{2}$. 
261 


Solver settings are set for solving the Stokes problem, with the number of timesteps, solver tolerance, maximum solver iterations, 
262 


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. 
263 


Level set parameters are set for the reinitialization procedure, such as the convergence tolerance, number of 
264 


reinitialization steps, the frequency of the reinitialization, for example, every third timestep, and the smoothing 
265 


parameter to smooth the physical properties across the interface. 
266 


A noslip boundary condition is set for the top and bottom of the domain, while on the left and righthand sides 
267 


there is a slip condition. 
268 


The initial interface between the two fluids is defined as in Figure \ref{RT2DSETUP}. Instances of the StokesProblemCartesian and LevelSet class are created. 
269 


The iteration throughout the timesteps involves the update of the physical parameters of the fluids; the initialization of 
270 


the boundary conditions, viscosity, and body forces; the solving of the Stokes problem for velocity and pressure; then the 
271 


level set procedure. 
272 


The output of the level set function, velocity and pressure is saved to file. 
273 
sshaw 
4552 
The timestep size is selected based on the CourantFriedrichsLewy condition (CFL condition)\index{Courant number}\index{CFL condition}. 
274 
jfenwick 
2738 
Due to the number of elements in the computational mesh, the simulation may take a long time to complete on a desktop computer, 
275 


so it is preferable to run it on the super computer. 
276 


At present, the fine mesh is required to capture the details of the fluid motion and for numerical stability. 
277 
lgraham 
2128 
% 
278 
lgraham 
2115 
\begin{python} 
279 



280 
lgraham 
2120 
from esys.escript import * 
281 


import esys.finley 
282 


from esys.escript.models import StokesProblemCartesian 
283 


from esys.finley import finley 
284 
lgraham 
2128 
from esys.finley import Rectangle 
285 
caltinay 
3348 
from esys.weipa import saveVTK 
286 
lgraham 
2120 
from LevelSet import * 
287 
lgraham 
2115 

288 
lgraham 
2120 
#physical properties 
289 


rho1 = 1000 #fluid density on bottom 
290 


rho2 = 1010 #fluid density on top 
291 


eta1 = 100.0 #fluid viscosity on bottom 
292 


eta2 = 100.0 #fluid viscosity on top 
293 


g=10.0 
294 



295 


#solver settings 
296 


dt = 0.001 
297 


t_step = 0 
298 


t_step_end = 2000 
299 


TOL = 1.0e5 
300 


max_iter=400 
301 


verbose=True 
302 


useUzawa=True 
303 



304 


#define mesh 
305 


l0=0.9142 
306 


l1=1.0 
307 
lgraham 
2128 
n0=200 
308 


n1=200 
309 
lgraham 
2120 

310 
lgraham 
2128 
mesh=Rectangle(l0=l0, l1=l1, order=2, n0=n0, n1=n1) 
311 
lgraham 
2120 
#get mesh dimensions 
312 


numDim = mesh.getDim() 
313 


#get element size 
314 


h = Lsup(mesh.getSize()) 
315 



316 


#level set parameters 
317 


tolerance = 1.0e6 
318 


reinit_max = 30 
319 


reinit_each = 3 
320 


alpha = 1 
321 


smooth = alpha*h 
322 



323 


#boundary conditions 
324 


x = mesh.getX() 
325 


#left + bottom + right + top 
326 
lgraham 
2193 
b_c = whereZero(x[0])*[1.0,0.0] + whereZero(x[1])*[1.0,1.0] + whereZero(x[0]l0)*[1.0,0.0] \ 
327 


+ whereZero(x[1]l1)*[1.0,1.0] 
328 
lgraham 
2120 

329 


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


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


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



333 


#define initial interface between fluids 
334 


xx = mesh.getX()[0] 
335 


yy = mesh.getX()[1] 
336 


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


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


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


func = yy  h_interface 
340 


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



342 
lgraham 
2128 
#Stokes Cartesian 
343 
lgraham 
2120 
solution=StokesProblemCartesian(mesh,debug=True) 
344 


solution.setTolerance(TOL) 
345 


solution.setSubProblemTolerance(TOL**2) 
346 



347 


#level set 
348 


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



350 


while t_step <= t_step_end: 
351 


#update density and viscosity 
352 


rho = levelset.update_parameter(rho1, rho2) 
353 


eta = levelset.update_parameter(eta1, eta2) 
354 



355 
lgraham 
2128 
#get velocity and pressure of fluid 
356 
lgraham 
2120 
Y[1] = rho*g 
357 


solution.initialize(fixed_u_mask=b_c,eta=eta,f=Y) 
358 
lgraham 
2193 
velocity,pressure=solution.solve(velocity,pressure,max_iter=max_iter,verbose=verbose, \ 
359 


useUzawa=useUzawa) 
360 
lgraham 
2120 

361 


#update the interface 
362 


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



364 
jfenwick 
4853 
print("##########################################################") 
365 


print("time step:", t_step, " completed with dt:", dt) 
366 


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


print("##########################################################") 
368 
lgraham 
2120 

369 


#save interface, velocity and pressure 
370 


saveVTK("phi2D.%2.4i.vtu"%t_step,interface=func,velocity=velocity,pressure=pressure) 
371 
gross 
3379 
#CFL condition 
372 
lgraham 
2120 
dt = 0.4*Lsup(mesh.getSize())/Lsup(velocity) 
373 


t_step += 1 
374 



375 
lgraham 
2115 
\end{python} 
376 
lgraham 
2128 
% 
377 


% 
378 


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: 
379 


% 
380 


\begin{python} 
381 


visit 
382 


\end{python} 
383 


% 
384 


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}. 
385 



386 


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. 
387 


\begin{figure} 
388 


\center 
389 
caltinay 
3279 
\subfigure[t=300]{\label{RT OUTPUT300}\includegraphics[scale=0.252]{RT2D200by200t300}} 
390 


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


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


\subfigure[t=1200]{\label{RT OUTPUT1200}\includegraphics[scale=0.252]{RT2D200by200t1200}} 
393 
lgraham 
2193 
\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.} 
394 
lgraham 
2128 
\label{RT2D OUTPUT1} 
395 


\end{figure} 
396 


% 
397 


\begin{figure} 
398 


\center 
399 
caltinay 
3279 
\subfigure[t=1500]{\label{RT OUTPUT1500}\includegraphics[scale=0.252]{RT2D200by200t1500}} 
400 


\subfigure[t=1800]{\label{RT OUTPUT1800}\includegraphics[scale=0.252]{RT2D200by200t1800}} 
401 
lgraham 
2128 
\caption{Simulation output of RayleighTaylor instability.} 
402 


\label{RT2D OUTPUT2} 
403 


\end{figure} 
404 


% 
405 


% 
406 


%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. 
407 


% 