CUNY Academic

This article describes a new numerical solver for the Navier-Stokes equations. The proposed solver is written in Python which is a newly developed language. The Python packages are built to solve the Navier-Stokes equations with existing libraries. We have created discretized coefﬁcient matrices from systems of the Navier-Stokes equations by the ﬁnite difference method. In addition we focus on the preconditioned Krylov subspace iterative methods in the linearized systems. Numerical results of performances for the Preconditioned iterative methods are demonstrated. The comparison between Python and Matlab is discussed at the end of the paper.


Introduction
The numerical solvers for Partial Differential Equations (PDEs) are developed greatly in recent years.
Most of them are based on the traditional languages such as C, C++, or Fortran etc. However, those languages are high performances but lower level languages. In general Languages like C, C++ require relative strong programming backgrounds. Therefore more scientists start to develop the numerical applications for PDEs in Python due to its huge potential advantages in the computational mathematics areas.

Why Python?
Python is an interpreted, high-level, objectoriented, dynamic general-purpose programming language. Since python 1.0 was published in 1994, it has been one of the most popular programming languages (3rd place on PYPL index, Feb 2014).
The advantages of the Python programming language include rapid development, excellent readability, fast learning curves, huge community support and great cross-platform capability.
It is extensively used in desktop application developments, web application developments, scriptings, system managements as well as scientific computations. In the scientific computation area, Python has the following important features compared with Matlab: • Code quality: Compared with the Matlab scripting language, Python is a full stack language supporting multiple programming paradigms including object-oriented, imperative, functional programming, etc.
It is capable of more computational tasks.
With the unique indent block, Python code is more readable. The Python module system is a sophisticated way to organize source code files compared to Matlab which lacks the support on namespace. The zero-based bracket array indexing is much more compatible with most other programming languages.
In addition Python is a signal processing system compared to the Matlab which is a one-based parenthesis system. • Performance: Though performance is a major concern of Python given its interpreted, dynamic nature. This problem can be usually circumvent by delegating most computational tasks to high performance packages written in C, FORTRAN. Cython, the variation of standard Python (CPython), provided a way to take the advantages of both the rapid development capability of Python and the high performance of C language by translating a superset of Python syntax to C code. In many scenarios, it will provide comparable performance to scientific packages.
• Availability of packages: Python and the huge number of extension packages are freely available (41311 on PyPI, a Python package repository on Mar. 2014). There are also high quality packages for all aspects of scientific computing.
Numpy/Scipy are popular Python numerical packages that provide similar functionality and performance to Matlab. Matplotlib is a very useful package for plotting. Numpy/Scipy and Matplotlib together can cover most of the tasks done on Matlab. Moreover, there are many other very useful Python packages for scientific computing including the scientific notebook iPython, the statistic package panda, the cross-platform GUI toolkits pyQt, wxPython and GTK, the all-in-one scientific Python distribution Pythonxy and canopy express (former EPD Python), the test framework unittest and nose.
• Cost: Matlab is very expensive for the researchers while Python and corresponding packages are totally free. It is more favorable for students who would like to learn the numerical analysis with no cost.
With the advanced features of Python, we are able to develop some efficient and easy-touse softwares to solve PDEs. In this paper, we focus on using Python to solve the PDEs arising from the incompressible flow problems, especially the Navier-Stokes equations. We are more interested in the applications of the preconditioned Krylov subspace iterative methods. We will compare the performances between Python and Matlab.

The Naviver-Stokes Equations
The Navier-Stokes equations, which are named after Claude-Louis Navier and George Gabriel Stokes, come from the motion of fluid substances. The equations are important with both academic and economic interests. The Navier-Stokes equations model the movement of the fluid from various types, such as the weather, ocean currents, water flows in a pipe and air flows around a wing. They are also used as the models to study the blood flow, the design of power stations, the analysis of pollution. The incompressible Navier-Stokes equations are given by the following PDEs: Here the variable u = u(x, t) ∈ R d , where d = 2, or d = 3 is a vector-valued function representing the velocity of the fluid, and the scalar function p = p(x, t) ∈ R represents the pressure.
Here B is the boundary condition operator such as the Dirichlet Boundary condition or the Neumann's boundary condition.
Assume Ω ⊂ R 2 . Denoting u = (u, v), and f = (f1, f2), we can write the equations (1.1)-(1.2) in scalar form as follows: The pressure field, p, is determined up to an additive constant. To uniquely determine p we may impose some additional condition, such as ∫ The source function f is given on a d-dimensional domain denoted by Ω ⊂ R d , with ∂Ω the boundary of Ω. Here ν > 0 is a given constant called the kinematic viscosity, which is ν = O(Re −1 ). Here Re is the Reynolds number: where V denotes the mean velocity and L is the diameter of Ω (see [1]).
Numerical algorithms for the computational solutions of the Navier-Stokes equations have been discussed for several decades. This field still remains active in the research area. In this paper, we will implement the efficient solvers for the Navier-Stokes equations using Python.
Though we already have some numerical solvers or softwares for solving PDEs on line, very few of them consider the preconditioned iterative solvers for the Navier-Stokes equations. We are the first to develop a Python package which enables us to solve the Navier-Stokes equations using different preconditioning techniques. We will compare the performances of different precondtioners and the convergence rates of the iterative methods. During our numerical experiences, we have used many mature libraries from the Web in the package, including NumPy (http : //numpy.scipy.org), Scipy (www.scipy.org), and Dolfin (www.f enics.org/dolf in).

Creating Matrices for the Navier-Stokes Equations
The unsteady (time-dependent) case leads to sequences of saddle point systems when fully implicit time-stepping schemes are used. For example, we can discretize the time derivative term ∂u ∂t using backward Euler or Crank-Nicolson schemes; see [2]. For the space discretization, either finite element methods or finite difference methods will work. We first consider the Markerand-Cell (MAC) discretization, which is due to Harlow and Welch (1965). See [3] for more details. The particularity of MAC scheme is the location of the velocity and pressure unknowns.
Pressures are defined at the center of each cell and the velocity components are defined at the cell edges (or cell faces in 3D). Such an arrangement makes the grid suitable for a control volume discretization. We also can use Dolfin to generate the matrices for the Navier-Stokes equations using the finite element method. We obtain the a sequence of linear problems which are also called the Oseen equations: Here α is O(δt), where δt is the time step. And u k is the solution which is obtained from the previous step. The initial guess u0 can be the solution from the Stokes equations which are The following finite difference expressions are utilized (for the two dimensional case) when we discretize the equations with respect to the space: With the above scheme, we obtain the where A1, A2are discrete reaction-convectiondiffusion operators. A1 is the approximation of αu − ν∆u + u ∂u ∂x + v ∂u ∂y , and A2 is the approximation of αv − ν∆v + v ∂v ∂x + v ∂v ∂y . Here B1 ≈ ∂ ∂x and B2 ≈ ∂ ∂y . We denote the matrix A in the block form: and B = [B1 B2].
The right hand side b = [f1, f2, 0] T . The discretized linear system Ax = b is the major system we focus on. Notice here the matrix A is a large sparse matrix. Therefore the computation involving the matrix A should always consider the sparsity and use sparse operations.
Notice here the linear system Ax = b is the linear system we need to solve at each Picard's iteration. Therefore, at each k-iteration in the Picard's iteration, we have the solution x. Under certain conditions, the sequence x inf k=1 will converge to the solution of the nonlinear problem (1.1) to (1.4).

Numerical Solvers
Currently, our most important application is to play with the different numerical solvers for the Saddle point system Ax = b with the coefficient matrix 2.1. We focus on Krylov subspace iterative methods: such as the General Minimum Residual Method (GMRES) [4] and the Biconjugate gradient stabilized method (BICGSTAB) [5]. For both methods, the preconditioning techniques are important for the Navier-Stokes equations. The preconditioner P is a matrix which approximates A in some yet-undefined sense. It is obvious that the linear system has the same solution as However (2.4) may be easier to solve, which means GMRES or other Krylov subspace iterative methods may converge faster. System (2.4) is preconditioned from the left, and we can also preconditioned from the right: Many research work has been published in the area of the preconditioning technique. Unfortunately, there is no universal "ideal" preconditioner for all linear systems. See a thorough discussion of the precondtioning techniques in [6,7], ect. The choice of the preconditioner is strongly case dependent which is the most challenging topic in the iterative solvers. We implemented the GMRES iterative solvers with no preconditioner, and with different choices of preconditions. The performance of the iterative solver and the preconditioners can be explored. At this stage, we have already tested the following different precondtioners with GMRES.

Block diagonal preconditioner
whereÂ is the approximation of A (in most our cases,Â is A or A + αI where α is time variable, and α = O( 1 δt )).
Then the block LU factorization form of block triangular reconditioner is

Hermitian and Skew Hermitian (HSS) Preconditioner
H = H + rIn+m,Ŝ = S + rIn+m. As we can see, H is the symmetric part of the coefficient matrix A, i.e.
and S is the skew-symmetric part of the coefficient matrix A, i.e.
In order to make H and S invertible, we shift both matrices by adding rIn+m, where r ̸ = 0 is an arbitrary parameter, and In+m is a identity matrix of size n + m.
The performances of the above three preconditioners are discussed in both [6,8]. Here we focus on the application of Python in those numerical solvers and the outcomes from the software package.

Numerical Experiments
In this section we report on several numerical experiments meant to illustrate the behavior of the Python applications on the preconditioned GMRES in a wide range of model problems.
We consider Navier-Stokes problems in the two dimensional spaces.

Numerical Solutions
In this session, we will show the example using Python to solve the Navier-Stokes equations and plot the solution. We use a few libraries: numpy and matplotlib. Here numpy is a library that provides matrix operations and matplotlib provides the 2D plotting library that we could use to plot the results. We consider the twodimensional lid-driven cavity problem. This is a classical problem from the Navier-Stokes equations. We take a square cavity with sides of unit length and kinematic viscosity ν = 0.05. The initial condition is u,v,p = 0 everywhere, and the boundary conditions are:  • u = 1 at y = 2 ; • u,v = 0 on the other boundaries; • To satisfy the continuity, we need to correct the pressure term and here are the boundary conditions for the pressure: ∂p ∂y = 0 at y = 0;p = 0 at y = 2; and ∂p ∂x = 0 at x = 0, 2 Figure 1 -4 show the solutions of the above driven cavity problems with viscosity ν = 0.1. Figure 1 is the solution as t = 1, Figure 2 is the solution as t = 200, Figure 3 is the solution as t = 500, and Figure 4 is the solution as t = 2000. We can see as t goes to around 500, the system gradually to stabilize.

2D Equations with Different Preconditioners
In this session, we explore the iteration properties of the 2D Navier-Stokes equations with different preconditioners.
We consider the Oseen equation in this case. We apply the Krylov subspace iterative methods: GMRES with three different preconditioner we introduced in the previous section. We use the library scipy, numpy and matplotlib. In Table 1 we present results for the iteration counts for 2D Oseen problem with different precondtitioners. Here the exact solver is used in each preconditioning action. The mesh size increases from 64 × 64 to 256 × 256.    [1]. This software is written in Matlab and it is a graphical package of the numerical study of the incompressible flows. This is a good way to study and explore the numerical methods of the Navier-Stokes equations. But you need to have Matlab installed which costs around one thousand dollars. It is expensive for the students who would like to explore the numerical solutions for the Navier-Stokes equations. On the other hand, Python is free. In addition, Python is very similar with Matlab in many features. Both of them are script languages and easy to learn. In our numerical experiments, we would like to show that we can use this free high-level language to develop a package. Our package will help to implement the Navier-Stokes equations of finite difference methods, explore a range of preconditioned Krylov subspace solvers, provide a visualization tool and etc.
Notice that with Python alone, the calculation speed is not competitive with the softwares based on C/C++/Fortran, or even Matlab. But with the combination of C/C++/Fortran, the Python script is able to run the meshes that have millions of nodes on desktop computers, see [9].

Conclusions
In this article, we have developed a numerical solver for the Navier-Stokes equations using Python. We use the preconditioned iterative methods to solve the problem and we explore the effectiveness and performances of the different preconditioners for the Krylov subspace methods. The numerical solutions are analyzed for their basic properties. We also compare the performances between Python and Matlab. The major contribution of this project is to develop a free, efficient software and numerical algorithms for the Navier-Stokes equations. It is the first time to study the preconditioning techniques in Python codes. The development of the Python package may benefit both scientists and students to analyze the numerical solvers of the Navier-Stokes equations, especially in observing and developing the preconditioning techniques.