Do you want to win * a US$1,000,000 prize*?

*The Millennium Prize Problems are seven mathematics problems laid out by the Clay Mathematics Institute in 2000. They’re not easy*–

*a correct solution to anyone resulted in a US$1,000,000 prize being awarded by the institute*. One of the problems to be solved is Navier-Stokes Equations. Do you want to learn how to Solve Navier-Stokes Equations? We will try to give an example in C++, compile it and run it using the C++ compiler; Maybe this example can be a beginning, and by using math and C++ you can solve the problem there, and you may earn that price!

Table of Contents

**Navier-Stokes Equations**

The **Navier–Stokes Equations**, also known as **N-S Equations**, **Momentum Equations**, named after French engineer and physicist Claude-Louis Navier and Anglo-Irish physicist and mathematician George Gabriel Stokes. NS Equations describes the physics of many phenomena of scientific and engineering interest. These equations can be used to model the air flow around a wing, airflow around the CPU or GPU, weather, ocean currents, water flow in a pipe and other fluid flows around objects or fluid flow inside objects can be calculated and simulated. These kind of analysis helps to the design of aircraft and cars, the study of blood flow, the design of power stations, the analysis of pollution, design of heat-sinks and many other things. These equations can be used

The N-S Equations are developed mathematically to express the conservation of momentum and the conservation of mass for Newtonian fluids. These equations are good to analyze fluid flows by an equation of state relating velocity, pressure, temperature, viscosity and density. These equations comes from the Isaac Newton’s second law, applied to fluid motion, together with the assumption that the stress in the fluid is the sum of a diffusing viscous and pressure terms.

**The Conservation of Mass**

For the Net Mass Transfer From the Faces, for a control volume in x, y, z cartesian coordinates these equations can be written as below

**The Conservation of Momentum**

The conservation of momentum states the amount of momentum remains constant (conserved); momentum is neither created nor destroyed, but only changed through the action of forces as described by Newton’s Laws of Motion.

Here we would like to summarize basics here. We highly recommend you Fluid Mechanics books to get more information about this theory. The Conversion of Momentum equation for a control volume of a fluid can be written as below;

**Conservation of Momentum = Net Mass Transfer From the Faces + Mass Change in Control Volume by The Time**

In motion, fluid is being under stresses and torsions. For a control volume in x,y,z cartesian coordinates, these equations can be written as below,

## N-S Equations (Momentum Equations) For The Incompressible Flow with Constant Viscosity

For example, for a Constant Viscosity (i.e. water) and if we assume that it is Incompressible, Navier-Stokes also called Momentum Equations can be written as below,

# 2 Dimensional Incompressible Flow Example with Constant Viscosity

## 2D Dimensionless N-S Equations (Momentum Equations) For The Incompressible Flow with Constant Viscosity

If we consider that our problem in 2 Dimension, NS Equations above can be written in dimensionless form if put dimensionless form of each parameter. Thus, for example Momentum Equation in X direction can be written as below,

## 2D Rectangular Channel Flow Model

For a 2 dimensional rectangular channel flow we can model a 2D grid flow as below,

Here, pressure nodes will be on the top of each grid nodes,

and the velocity nodes u and v will be between these pressure nodes as below,

Each Velocity and Pressure nodes can be modeled as below with it’s adjacent nodes.

Finally, with this all models, a N-S Equation can be written as below,

This final formula in rectangle is a 5 diagonal matrix form and it can be solved by the SOR iteration method as we described in this Solve MILLIONS of Unknowns In Equations – We Show You How! post. SOR Iteration Method also can be solved as a Multi-Thread application as given figure below, each lines or group of lines can be threaded.

To solve these kind of matrixes we can use this struct below,

1 2 3 4 5 6 7 8 |
struct matrix { double B,D,E,F,H; unsigned short int b,d,f,h; }; struct matrix m[N]; |

Simply this SOR Iteration Method can be written in C++ as below,

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
int calc_matrix_SORR_5diag(int max_iteration) { unsigned int iteration = 0; const double omega = 0.5; const double min_error = 0.0001; double err; double psol; do { err=0; for(int i=0; i<NN; i++) { psol=sol[i]; sol[i]=(1.00-omega)*sol[i] + omega*(x[i] -mx[i].B*sol[i+mx[i].b]-mx[i].D*sol[i+mx[i].d] -mx[i].F*sol[i+mx[i].f]-mx[i].H*sol[i+mx[i].h] )/mx[i].E; err=max(err, std::fabs( (sol[i]-psol) )); } iteration++; }while(err>min_error && iteration<=max_iteration); for(int i=0; i<NN; i++) u[i]=sol[i]; return iteration; } |

By using these equations methods we can obtain exact u v velocity parameters in x and y directions on each node in that time. Thus, we can display and simulate fluid flow in C++ Builder by drawing into a canvas or canvas of a bitmap. Let’s see how we can draw velocity vectors.

**Visualization of 2D Velocity Vectors with** (u,v) parameters

We can draw velocity vectors by using their dx, dy and dz divergence in X, Y and Z directions. Here below we used u for the dx and and v for the dy.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void draw_vectordata(TCanvas *canvas, float x1, float y1, float u, float v) { canvas->MoveTo(x1, y1); canvas->LineTo(x1+u*scale, y1+v*scale); canvas->LineTo((x1+u*scale)+arrow_scale*cos(atan2(v,u)-M_PI/1.2), (y1+v*scale)+arrow_scale*sin(atan2(v,u)-M_PI/1.2)); canvas->MoveTo(x1+u*scale, y1+v*scale); canvas->LineTo((x1+u*scale)+arrow_scale*cos(atan2(v,u)+M_PI/1.2), (y1+v*scale)+arrow_scale*sin(atan2(v,u)+M_PI/1.2)); } |

Here below is an old application example developed by me for the master thesis, which runs on the latest RAD Studio 10.4 (after 20+ years with few changes) . This shows how those procedures can be used to display velocity of fluid in a rectangular channel flow. This is a uniform fluid flow example in a rectangular duct.

Here is a result that shows a fluid flow in rectangular duck over a rectangular 2D object. After a while, results goes to display vortexes at around the object or at around the spaces at the back.

Now it is your turn 🙂 You can Win $1M! 😀 You can develop your own analyze applications with C++ Builder by using VCL or FMX frameworks and you can display your data. FMX has more options like using alpha colors, using opacity that may result better graphics. You can also add gradient colors these vectors to show their magnitude. **C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. There is a free C++ Builder Community Edition for students, beginners and startups, it can be downloaded from here. Professional developers can use the Professional, Architect or Enterprise versions of C++ Builder which has a trial version and can be downloaded from here.**

Design. Code. Compile. Deploy.

Start Free Trial Upgrade Today

Free Delphi Community Edition Free C++Builder Community Edition