**Thermodynamics** is one of the engineering areas that we work in, and it is expressed by a mathematical framework of *thermodynamic equations* which relate various thermodynamic quantities and physical properties measured in a laboratory or production process. It is a branch of physics that deals with heat, work, temperature, and their relation to energy, radiation, and the physical properties of matter. Thermodynamic systems are analyzed under the four Laws of Thermodynamics. In this article, you’ll learn What are the laws of thermodynamics, what is heat transfer, using the C++ algorithm for the finite difference method, and using SOR revised method with c++ software.

## What are the laws of thermodynamics?

0th Law: If two systems are both in thermal equilibrium with a third system, then they are in thermal equilibrium with each other

1st Law: The total energy of an isolated system is constant; energy can be transformed from one form to another but can be neither created nor destroyed

2nd Law: Entropy of an isolated system always increases.

3rd Law: A system’s entropy approaches a constant value as its temperature approaches. In other words, a system’s entropy approaches a constant value as its temperature approaches absolute zero.

## What is heat transfer?

**Heat Transfer** is a field of thermal engineering that concerns the generation, use, conversion, and exchange of thermal energy (heat) between physical systems. In the design process of products, mostly heat transfer is neglected but somehow it is always being in deal with working any machines; cars, engines, brake systems, computers, graphics cards, modems, mainboards, CPU and GPU designs, etc. If you do not analyze well all the things with the view of thermodynamics then your fuel filter is being stuck because of heat, your engine doesn’t work efficiently, your modem disconnects due to high temperature on condensers or other circuits, your CPU or GPU can not overlock much or can not work efficiently, etc. These are only a few examples of many problems based on heating or cooling.

## A C++ algorithm for the Finite Difference Method

While some other fields are not related to heating, they are also exposed to this. For example, wind turbines are the product of kinetic energy and its equations. but weather conditions (temperature, pressure) also the pressure of the air on the wings and brake systems, generators, electronic systems are all exposed to heat transfer. Thus, in this post, we think that giving a C++ example to analyze these kinds of system bodies with a Finite Difference Method may help you develop specific apps to design your products more efficiently.

For a 3D object in a xyz cartesian system a heat transfer equation can be written as below,

where T is the temperature of that point (node) in x,y,z cartesian system and here dT/dt shows the change in temperature between nodes. k is thermal conductivity, ro is the density of the rigid body, Cv is a Heat Capacity term, and is the amount of energy absorbed or released per unit mass of a substance in a constant volume. Here, the center node presented with P, east, west, north, and south nodes with E, W, N, S, bottom and top nodes with B and T. Thus this equation can be written as a Finite Difference equation as below,

This can be,

and finally we can simplify these constant coefficients with ‘**a’** as below

This equation above is correct in all nodes inside and can be customized for 2 different materials i.e. copper to aluminum. Outer nodes needs should be adopted to the boundary condition. For example, from the copper rigid body to the air we should add boundary conditions to all outer nodes. Is the outer face is Isolated? Conducted to another material? or Is there convection from the air or to the air or any other gas?

## Boundary conditions

**Boundary Conditions** and the definition of equations by the boundary conditions are also very important. To solve these kinds of equations we should check this formula for each node. In our example, let’s assume that this rigid 3D object in an air (i.e .has heat transfer between the object and the air. Thus, heat transfer at the corners, at the edges where interacts with air and other nodes inside nodes will be different, so we should also consider heat transfer between this rigid body and the air. Thus at these boundary nodes, we can use this formula below,

in this equation, the left side is the **conduction** and **convection** heat transfer on the **node** (corner, edge, or on the plane) which has T temperature, the right side is the **convection** heat transfer from the air where infinitive temperature here shows the environment temperature that can be assumed as a constant.

## Implicit functions

If we consider these 2 equations together for all nodes, this is an **Implicit Function** that is defined by an implicit equation, that relates 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. One of the most popular equations 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 linear system equations, resulting in faster convergence. A similar method can be used for any slowly converging iterative processes. This is a 3D problem and as you see this has 7 parameters that means it will result in 7 diagonal matrix forms. Remember that we post before, that we can calculate 3, 5, and 7 diagonal matrix forms and we can Solve MILLIONS of Unknowns In Equations with the Successive Over Relaxation Iteration Method.

## Equations in matrix form

Simple equations in 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.

Seven diagonal M matrix, U and q matrixes can be shown as below here,

Here A,B,C,D,E,F,H,I are the bottom, south, left, center, right, north, top parameters of the equation, respectively. Lines of this M matrix can be structured as follows to hold all matrix form,

1 2 3 4 5 6 7 8 9 10 |
#define NN 100 // number of nodes struct matrix_line { double A,B,D,E,F,H,I; // values in a line of 7 diagonal matrix form to solve 1D, 2D and 3D problems short int a,b,d,f,h,i; // X positions (index) of A,B,D,E,F,H,I values }; struct matrix_line mx[NN+1]; |

## An example of using the SOR Revised Method in C++

This final equation can be solved with SOR Revised method 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 |
void calc_matrix_SORR(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=1; i<=NN; i++) { psol=sol[i]; sol[i]=(1.00-omega)*sol[i] + omega*(x[i] -mx[i].A*sol[i+mx[i].a]-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].I*sol[i+mx[i].i] )/mx[i].E; err=max(err,fabs( (sol[i]-psol) )); } iteration++; }while(err>min_error && iteration<=max_iteration); } |

Here is an old C++ Builder example showing how these work in an application,

## What is so great about the SOR Revised method?

This SOR Revised Method can be used for the linear equations of most of the convection and the conduction problems. We have explained SOR iteration methods in detailed, 3, 5, and 7 diagonal examples before in this post Solve MILLIONS of Unknowns In Equations – We Show You How! . Note that there is also **Radiation Heat Transfer**, but it can be neglected in low temperatures, if you have higher temperatures you should consider this too.

**RAD Studio**** ****C++ Builder ****is a great programming language to calculate these kinds of problems in C++ and is also powerful enough for all your development needs. Why not ****download**** and try C++ builder today?**

Design. Code. Compile. Deploy.

Start Free Trial Upgrade Today

Free Delphi Community Edition Free C++Builder Community Edition