📜 ⬆️ ⬇️

Experience using Freefem ++ and NetGen in an aerodynamic process modeling program

Introduction


The authors of this article had a chance to perform a rather rare project. It was necessary to develop a commercial program for modeling the processes of movement of air in a clean room. A clean room is a production room that meets certain requirements for clean air, temperature and speed of its movement. The main indicator of purity is the class of purity, which is determined by GOST on the concentration of particles in the air. Air flows in a clean room are directed so as to ensure effective removal of dust and aerosols from the room. Requirements may also limit temperature gradients in space and time. The Cleanroom program is designed to be used as a tool for a clean room designer. With its help, the designer must perform the placement of equipment and ventilation elements, and based on the results of modeling processes in air, determine the degree of compliance of the placement option with the purity requirements.

Modeling the concentration of particles and the calculation of the purity class will not be considered here. In the developed program, the cleanliness class is calculated for the room as a whole in accordance with the Gustavsson method, described in the book www.asincom-group.ru/book.htm . There were no fundamental difficulties in its implementation. Implementation of modeling the movement of dust in the space of a room would be very useful, but this task did not fit into the project budget.

Air movement is described by the Navier-Stokes system of equations, to which a large number of literary sources is devoted, and there is a simple exposition of the foundations even here habrahabr.ru/post/171327 . The equations relate the speed of air movement, its temperature and pressure. In a clean room, the parameters of inlet ventilation, exhaust ventilation, and air heating by the equipment constitute the boundary conditions for the flowing aerodynamic processes.

The Navier-Stokes equations are a system of partial differential equations, which, with the exception of a few simple cases, cannot be solved analytically. Therefore, as a rule, this system is solved numerically by the finite element method. To solve such problems, there are universal applications belonging to the CFD (Computational Fluid Dynamics) class. These are extremely complex and expensive software products (Ansys, SolidWorks, Flow Vision, etc.) that allow you to explore the flow of liquids and gases in a variety of conditions. The versatility of these applications leads to the fact that the user must have a very high level of knowledge of physics and mathematics, and mastering these programs requires many months of training and practical work.
')
In our case, it was necessary to develop a specialized program that provides the following functions:
  1. Enter the geometric dimensions of the room and technological components. Enter the temperature of all surfaces and individual regions of the surface.
  2. Enter the geometric dimensions of the regions on the surface corresponding to the elements of supply and exhaust ventilation, setting the parameters of air movement across the border.
  3. Calculation of the air velocity vector field and the scalar temperature field. Graphic display of these fields.

In this program, the Navier-Stokes system of equations was used in the Boussinesq approximation, i.e. with the simplification of the dependence of air density on its temperature. The technological elements of the arrangement and the room are either parallelepipeds or combinations of parallelepipeds. Rectangular regions with their parameters are superimposed on the edges of the placement elements. The faces of the placement elements and the regions superimposed on them set the boundary conditions: temperature and air velocity. The solution of the system of equations by the finite element method gives the desired result: a scalar temperature field and a vector field of air velocity inside the room. Thus, the developed program is a specialized solver of the same Navier-Stokes system of equations, for which only the boundary conditions vary with different configurations of the room and the equipment placed. In fig. 1 shows its window with simulation results. Graphically simulated space and the fields of speed and temperature are displayed as sections in three planes.

Fig. 1. Application window with simulation results

This application, despite its obvious knowledge-intensiveness, was developed in a relatively short time. This has been made possible through the use of third-party open source libraries Freefem ++ and NetGen. The purpose of this article seems to be that our experience will allow other developers to bypass some of the “pitfalls” in the development of similar applications with the involvement of these libraries.

Implementation of the finite element method


The search for a suitable library for solving Navier-Stokes equations was performed both among specialized software tools for solving problems of hydrodynamics, and among universal tools for solving systems of partial differential equations. In this case, we had to take into account the following feature of the project being implemented. The Cleanroom program being developed is a commercial product that should run under Windows. The possibility of acquiring paid libraries was excluded by the terms of reference. Therefore, third-party libraries should have been free and licensed to allow their use in a commercial product.

The latter restriction excluded the possibility of using third-party software products with the GNU GPL license . Therefore, we had to abandon, for example, the well-known and widely used OpenFoam library (the official site www.openfoam.org and a brief description here habrahabr.ru/company/intel/blog/170675 ). Other libraries either have an unsuitable license, or projects are frozen and their use is associated with greater risk. The only library that matched all parameters was Freefem ++.

Freefem ++ (the official website of www.Freefem.org/ff++ ) is a program for the numerical solution of partial differential equations. It is based on the finite element method. This is an application that accepts a file with a program text (script) that describes the solution of the problem in a special C-like language. The input language Freefem ++ includes the basic algorithmic constructs: variable declaration, loop operators, branch operator, C ++ stream input / output style I / O, function declarations, elementary data types, arrays, files, etc. To solve systems of differential equations in the language, special data types and functions are provided. Such types are triangulation grids, the space of finite elements based on these grids. There are special ways to write derivatives of functions and integrals. A special solve construction is provided, with the help of which the variational formulation of the problem is written and the system of equations is solved.

To solve the Navier-Stokes system of equations, two variational formulations are given, which are used sequentially. Using the first one, the system of equations for air velocity and pressure is solved for given temperature values ​​for a given moment of time. The second variation formulation allows one to recalculate the temperature field from the calculated velocity field. This process is repeated cyclically for subsequent time samples. The composition of the first variation formulation includes boundary conditions for air velocity. For impenetrable walls, the speed is set to zero (sticking effect), and for the regions of intake and exhaust ventilation, the direction and speed of air movement is indicated. The second variation formulation includes the boundary conditions specified in the form of wall temperature values.

Geometric elements to which properties should be assigned are presented to the user as the faces of spatial figures and the regions of the surface on them (see Fig. 2). Therefore, for Freefem ++, it is necessary to form a finite element mesh so that the triangles on the boundary of the mesh completely fit into the regions of the surfaces, as shown in Fig. 3. In the mesh3 data type for a three-dimensional finite element mesh for boundary triangles, labels (identifiers) are put down. These labels are used in a variation formulation to define the corresponding function on the boundary.


Fig. 2. Regions of a surface on which boundary conditions should be set


Fig. 3. Boundary triangles of the space of finite elements

Generation of finite element mesh


Unfortunately, Freefem ++ does not have its own 3D grid generator. For this purpose, the authors of Freefem ++ propose to use a third-party module TetGen (http://tetgen.berlios.de). The TetGen module is connected in the script in the standard for Freefem ++ method (the load operator "tetgen"). The language has several predefined functions for generating 3D grids, which are actually implemented by this module. However, the module has a kind of license, which requires coordination with the author of the possibility of using TetGen in commercial applications. Therefore, from this generator of 3D grids had to be abandoned and look for a replacement for it.

Freefem ++ has a 3D mesh import operator from a mesh text file. Therefore, almost any third-party generator can be used. However, in our development, a third-party finite element mesh generator should be a free product with a license that allows it to be used in a commercial application. Ultimately, NetGen was chosen (http://sourceforge.net/projects/netgen-mesher/).

NetGen is designed to generate and display 3D grids and consists of several libraries. The main library nglib directly implements mesh generation. There are two ways to specify the space, which must be filled with tetrahedral elements:
  1. Specifying space using constructive block geometry (CSG) operators. In this technology, the desired space is described by a set of primitive objects, over which such operations as union, intersection, difference are set.
  2. Description of the surface, which is the boundary of the desired space, in the file format STL. In this format, information about the surface is represented as the coordinates of the triangular faces of the surface and their normals.

Despite the attractiveness of using constructive block geometry, it had to be abandoned. According to the statement of work, the surface of a room and any element of placement may contain regions with its own modeling parameters (temperature and air velocity). In the implementation of constructive block geometry, it is possible to set only the properties of the surface of a primitive spatial figure as a whole. And it was not possible to find a reasonable way to set the properties of individual regions.

To solve this problem, only the variant with the formation of finite elements according to the geometric description in the STL format turned out to be suitable. In order to form this file, you must first perform a triangulation of the surfaces that make up the boundary of the space. Moreover, triangulation should be performed taking into account the regions superimposed on the surface.

Generating a surface description in STL format


It was decided to build surface triangulation using Freefem ++, which, unlike the implementation of generating 3D grids, does not use additional third-party modules for these purposes. Since in the developed application all the elements of the placement and the room are represented by parallelepipeds, in order to obtain the triangulation of their surfaces, it was necessary to perform two operations:
  1. Perform 2D triangulation of each face, taking into account the regions imposed on the face.
  2. Assign spatial 3D coordinates to the vertices of the obtained triangles.

The source data required to perform 2D triangulation in Freefem ++ is represented by objects of a special type - border. In such an object, the parametric record of one fragment of the boundary is indicated. To form a triangulation network, the buildmesh function is used, by which all boundaries of the shared surface are transferred to the input. In this case, for each boundary fragment, the number of segments of triangles is indicated, which will be based on the boundary. In this case, this quantity must be indicated equal to one, i.e. each piece of the border will be entirely one edge of a triangle from triangulation.

The input data entered by the user describe the superimposed regions of the surfaces (Fig. 4-a). As a result of such an overlay, a set of borders of arbitrary complexity may form on one face of the placement element. To perform the triangulation procedure, it is necessary to convert the geometric data on the regions into a system of directed segments as shown in Fig. 4-b. The corresponding algorithm is implemented by the authors in Cleanroom. The resulting boundary system is described in a script that is passed to the input of Freefem ++. This script displays the results of triangulation in a file (an example of triangulation in Fig. 4-c). After this, the developed program, reading the triangulation from the file, assigns to each triangle an identifier corresponding to the set of physical properties of the region of the surface to which the triangle belongs. It is obvious that the triangulation of all faces of all placement elements can be performed by one script in one call to Freefem ++.

Fig. 4. Transformation of surface regions into a system of boundaries and surface triangulation

General algorithm for using Freefem ++ and NetGen


Let us summarize the sequence of calls to Freefem ++ and NetGen in the simulation procedure. Recall that the initial data for modeling is an object model of layout elements with the physical properties of surface regions. The following actions are performed:
  1. Formation of the system of regional boundaries in the form of directed segments on each face of all placement elements (see Fig. 4).
  2. Forming a file with a script for Freefem ++ with a description of the boundaries of the surfaces to be triangulated.
  3. Call Freefem ++. At its entrance is a prepared script file. Freefem ++ performs triangulation of specified surfaces. At the output - a 2D triangulation file for the faces of the placement elements.
  4. Convert 2D triangles into 3D triangles. Formation of a file in the STL format, in which the coordinates of the vertices of the triangles and the normals to them are indicated.
  5. Perform a sequence of NetGen function calls. At the entrance - STL-file. NetGen generates a 3D finite element mesh. At the output - a mesh file with a description of the tetrahedra of the grid and boundary triangles.
  6. Preparing a file with a script in the input language Freefem + for solving a system of differential equations. The script imports a finite element mesh from a mesh file. The script writes the boundary conditions for the labels of the boundary triangles.
  7. Launch Freefem ++. At the entrance - a mesh file and a file with a script. Freefem ++ solves the Navier-Stokes equations. At the exit - a file with the values ​​of the air velocity components in the grid nodes and a file with the temperature values ​​in the grid nodes.
  8. Reading a mesh file with a finite element mesh, a file with velocity values, and a file with temperature values. Display of the required fields in graphic form.


An important feature of using NetGen to generate a finite element mesh is as follows. NetGen provides the ability to specify a set of predefined segments (edges in NetGen terminology) on the surface of a space, which are preserved during the formation of a grid of finite elements. In our case, it is necessary to set the edges of rectangular regions specified by the user as edges. Then NetGen generates a finite element mesh so that the boundary triangles densely fill the surface regions, i.e. each boundary triangle of the generated mesh completely falls into one rectangular region. Due to this, the given physical properties (air velocity and temperature) are determined for the boundary triangle, which were specified for the region. If the edges are not specified, then NetGen will not preserve the original triangles from the STL description and the resulting boundary triangles of the network of finite elements will arbitrarily cover the surface of space, crossing the borders of the regions with the specified properties. This means that the triangle cannot be assigned physical properties for the boundary condition.

When implementing the transfer of a finite element mesh generated using NetGen, an undocumented feature of Freefem ++ emerged at the input of Freefem ++, which consists of the following. In a mesh file with a finite element mesh, which is imported into Freefem ++, the tetrahedral elements are described as lines with vertex numbers. The order of the vertices in the documentation for Freefem ++ is not specified. However, it turned out that if the vertices of the tetrahedron are listed in the order NetGen issues them, Freefem ++ shuts down due to an exceptional situation. Analysis of the source code of Freefem ++ showed that the vertices of each tetrahedron should be specified in such a way that, as a result of calculating the volume of the tetrahedron in Freefem ++, a negative number is not obtained. Therefore, the vertices of the tetrahedra from the grid, obtained using NetGen, must be rearranged accordingly. Unfortunately, the clarification of this fact took quite a lot of time during development.

When solving test problems, it turned out that various methods for solving a system of algebraic equations into which the system of differential equations is transformed in the finite element method have undocumented restrictions on the maximum number of finite elements. Only the method, denoted in the input language of Freefem ++ as CG (Conjugate Gradient), allowed to solve real problems without the occurrence of exceptional situations.

Place Freefem ++ and NetGen in the application structure


In Cleanroom, the procedures for the formation of finite elements and the solution of a system of equations are the most resource-intensive parts, and the speed requirements are imposed on them. The speed requirements for all other functions are incomparably lower. Therefore, to reduce development time, it was decided to develop Cleanroom in C #. For seamless use of NetGen, i.e. the inclusion of a library in C ++ in a program in C #, a thin layer was developed in C ++ / CLI. It calls functions from NetGen and converts them to adapt the data to the business logic layer. In this case, the C ++ / CLI assembly uses a data layer developed in C #. Such a connection in one place of two different development tools (C ++ and C #) turned out to be extremely convenient. Since Freefem ++ is a loadable module, it is called as a separate application and communicated with it through files. The developed program structure is shown in Fig. five.

Fig. 5. Structure of the Cleanroom application

Auxiliary tools


For debugging the application, various tools were used that allow you to visually display STL files, mesh files, and the resulting process fields. The GLC-player (http://www.glc-player.net/) was used to view the surface described in the STL format. It is sufficiently resistant to errors in the STL file and sometimes allows these errors to be detected by surface image defects.

The Freefem ++ project includes the ffmedit program, which is an adaptation of the Medit program (http://www.ann.jussieu.fr/frey/software.html) for Freefem ++. This program visualizes the mesh file. With its help, one can consider in detail the space of finite elements. In it, you can even see the surface of equal values ​​of the function calculated on this space. To do this, you need to create a file of field values ​​at the vertices of the grid of elements in accordance with certain rules.

NetGen has a utility that uses the same library, which allows you to view both the surfaces specified in the STL and the files with a finite element mesh. In this program, you can generate a 3D mesh of elements according to STL description, repartition, view individual grid tetrahedra.

Conclusion


The use of Freefem ++ and NetGen allowed in a short time to develop a program that is very complex in its mathematical functionality. At the same time, licenses for Freefem ++ and NetGen do not limit the license of the product under development. There are some problems using Freefem ++ and NetGen, but they are solvable. Therefore, Freefem ++ and NetGen should be used in the development of software products similar to Cleanroom.

Source: https://habr.com/ru/post/181263/


All Articles