C++ is a great programming language to calculate engineering problems, it is one of the best for complex operations which require you to get the maximum performance you can from the computer hardware. Delphi is also a truly capable compiled programming language that you can use in engineering problems. In this post we will explain how to write C++ software to solve millions of unknowns in equations by using the Successive Over Relaxation Iteration method.
We engineers mostly face solving numerical equations about the real-life problems. Thermodynamics, Fluid Mechanics, Mechanism Theories, Structure Analysis, and many other engineering areas requires to solve many unknown equations. Sometimes calculations should be in real time, or at the very least it should be simulating time fractions as precisely as possible. Generally, these implicit equations are exposed on a 1D, 2D or 3D grid examples (i.e. compressed on uncompressed fluid flow, thermal distributions) and these grids has nodes with parameters (i.e., Velocity, Pressure, Temperature etc.)
In mathematics, an implicit equation is a relation of the form R(x1,…, xn) = 0, where R is a function of several variables. In example, the implicit equation of the unit circle is is x2 + y2 − 1 = 0. More complex equations can be given as equations about to solve first and second law of thermodynamics, Momentum Equations, Naiver-Stokes Equations etc.
An Implicit Function is a function that is defined by an implicit equation, that relates to one of the variables, considered as the value of the function, with the others considered as the arguments. These Implicit Equations can be solved by using iteration methods.
Solving a 5 Diagonal matrix
One of the most popular methods is SOR Successive Over Relaxation Iteration Method (SOR). This method can be used to solve problems on 1D, 2D and 3D problems. In Numerical Linear Algebra, the Successive Over-Relaxation (SOR) Method is a variant of the Gauss–Seidel method for solving a linear system equation, resulting in faster convergence. A similar method can be used for any slowly converging iterative processes.
If the problem is on a 2D grid (for example heat transfer on a plate is 2D problem) these equations will produce 5 diagonal matrix forms. Because each 2D grid nodes on this plane connected to left, right, up and down nodes (sometimes called East, West, North and South) except for the corners and the edges. That means there will be maximum 5 variables in each line of the 2D matrix.
Simply equations in a matrix form can be written as below.
In this equation;
M is 2D matrix (2 bars shows that it is 2D matrix),
U is 1D matrix form of unknown parameters from the each node,
q is 1D matrix form of right side of the equation.
These A matrixes can be shown as below here,
This matrix can be written as below,
1 2 3 4 5 6 7 8 |
#define NN 100 // number of nodes struct matrix_line { double B,D,E,F,H ; // values in a line of 7 diagonal matrix form to solve 1D, 2D and 3D problems short int b,d,f,h; // X positions (index) of A,B,D,E,F,H,I values }; struct matrix_line mx[NN+1]; |
Linear equations can be solved by Successive Over Relaxation (SOR) Method. Here we developed a SOR Revised Method (SORR) 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 |
void calc_matrix_SORR5(int max_iteration) { unsigned int iterartion = 0; const double omega = 0.5; const double min_error = 0.0001; double err; double psol; do { err=0; for(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,fabs( (sol[i]-psol) )); } iteration++; }while(err>min_error && iteration<=max_iteration); } |
For example, we can calculate unknowns with maximum 1000 iterations by using this function as below,
1 |
void calc_matrix_SORR5(1000); |
C++ is a great programming language to solve these kind of equations.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition