⬆️ ⬇️

Finite Element Programming

This article is devoted to the own implementation (Solker Joker FEM ) of the finite element method for systems of diffusion-reaction equations.



It is usually preferable to use ready-made solutions, but if there are specific features in the task, then the task is easier to solve using a simple library.



Introduction



The finite element method [1-4] is a group of effective methods for solving problems of mathematical physics, continuum mechanics. FEM programming issues are devoted to sources [5–9].



Formulation of the problem



Robin’s boundary problem for the system Ndiffusion-reaction equations in a bounded domain  Omega subset mathbbR2with border  Gamma:



βˆ’ai Deltaui(x)+ sumk=1Nqik(x)uk(x)=gi(x),x in Omega,



ai frac partialui(x) partial mathbfn+bi(x)ui(x)=bi(x)wi(x),x in Gamma.



Here i=1,2, ldots,N,  mathbfn- vector of external normal to  Gamma.



Weak language



Multiply each of Nequations for an arbitrary test function v, we integrate the obtained identities by  Omega, apply the Green formula and take into account the boundary conditions. Will get



ai int limits Omega nablaui nablavdx+ int limits Gammabiuivd Gamma+ sumk=1N int limits Omegaqikukvdx= int limits Omegagivdx+ int limits Gammabiwivd Gamma,i=1, ldots,N.



We introduce the Sobolev functional space V=H1( Omega). Functions u1, ldots,uN inVare called a weak solution of the original boundary value problem if the indicated identities are satisfied for an arbitrary test function v inV.



Weak wording can be written as follows: find functions u1, ldots,uN inVsuch that



Ai(ui,v)+ sumk=1NBik(uk,v)=Fi(v) forallv inV,i=1, ldots,N,



Where Ai,Bik- bilinear forms, Fi- linear form:



Ai(u,v)=ai int limits Omega nablau nablavdx+ int limits Gammabiuvd Gamma, quadBik(u,v)= sumk=1N int limits Omegaqikuvdx,



Fi(v)= int limits Omegagivdx+ int limits Gammabiwivd Gamma.



FEM algorithm



We introduce in space Vfinite-dimensional subspace Vm. According to the Galerkin method, we arrive at a discrete problem: find functions u1, ldots,uN inVmsuch that



Ai(ui,v)+ sumk=1NBik(uk,v)=Fi(v) forallv inVm,i=1, ldots,N.



Let be  phi1, ldots, phim- basis Vm. If we put ui(x)= displaystyle sums=1mcis phis(x), v(x)= phij(x), then the task is reduced to SLAE with respect to Nmunknown cks:



 sums=1mAi( phis, phij)cis+ sumk=1N sums=1mBik( phis, phij)cks=Fi( phij),i=1, ldots,N,j=1, ldots,m,



or



 sums=1m left(ai int limits Omega nabla phis cdot nabla phij dx+ int limits Gammabi phis phij right)cis+ sumk=1N sums=1m left( int Omegaqik phis phijdx right)cks=



= int limits Omegagi phijdx+ int limits Gammabiwi phijd Gamma,i=1, ldots,N,j=1, ldots,m.



The simplest example of a subspace Vm- the space of continuous piecewise linear functions. Region  Omegais divided into finite elements (usually triangles), and on each triangle the functions of Vmare linear. In the FEM, the basic functions are chosen so that in most parts of the domain they vanish, then there will be many zeros in the SLAE matrix. As basic functions  phijtake "pyramids", which are equal to 1 in one grid node and 0 in the other nodes. Each node of the grid has its own basic function, and the dimension msubspace is equal to the number of nodes in the grid.



Note that if sth and jnodes are not connected by an edge, then  varphis varphij equiv0,  nabla varphis cdot nabla varphij equiv0so when summing over sit is enough to go through only the indices sfor which between sm and jnodes are edge either s=j.



Thus, the solution of the boundary value problem by the finite element method is reduced to the construction and solution of the corresponding SLAE.



Piecewise linear functions



To set the function u inVm, you need to specify its values ​​in the grid nodes. We denote the coordinates i-th node through (Xi,Yi)and the value of the function uat inode - through Ui=u(Xi,Yi).



To find the value of a function uat an arbitrary point (x,y)belonging to the triangle with ith jth and kvertices, we introduce the barycentric coordinates L1,L2,L3that take values ​​from 0 to 1 and are related to Cartesian coordinates x,ythe following relations (see [3, Section 4.7.1], [4, p. 35]):



 begingatheredx=L1Xi+L2Xj+L3Xk,y=L1Yi+L2Yj+L3Yk,1=L1+L2+L3. endgathered



From here



 begingatheredL1(x,y)= frac12A(ai+bix+ciy),ai=XjYkβˆ’XkYj,bi=Yjβˆ’Yk,ci=Xkβˆ’Xj,L2(x,y)= frac12A(aj+bjx+cjy),aj=XkYiβˆ’XiYk,bj=Ykβˆ’Yi,cj=Xiβˆ’Xk,L3(x,y)= frac12A(ak+bkx+cky),ak=XiYjβˆ’XjYi,bk=Yiβˆ’Yj,ck=Xjβˆ’Xi, endgathered



Where A = \ dfrac {1} {2} \ begin {vmatrix} 1 & X_i & Y_i \\ 1 & X_j & Y_j \\ 1 & X_k & Y_k \ end {vmatrix} - area of ​​a triangle with a sign.



The value of the function can be found by the formula u(x,y)=UiL1(x,y)+UjL2(x,y)+UkL3(x,y).



Numerical integration



Let us reduce the integral over the boundary edge with im and jends of the integral over a segment [βˆ’1,1]:



 int limitsijf(x,y)dl= frac|ij|2 int limitsβˆ’11f(x(t),y(t))dt.



Here |ij|- edge length, x(t)= dfracXi+Xj2+ dfracXjβˆ’Xi2t,

y(t)= dfracYi+Yj2+ dfracYjβˆ’Yi2t.



To calculate the last integral, apply the Gauss quadrature formula:



 int limitsβˆ’11 varphi(s)ds approx sumj=1nwj varphi( xij).



Points  xij in[βˆ’1,1]and weight factors wjtaken from the table [3, sec. 5.9.2].



We will reduce the integral of the triangle with ith jth and kvertices of a triangle integral D = \ {(L_1, L_2) \ in [0,1] \ times [0,1] \ colon L_1 + L_2 \ leq 1 \} :



 int limitsijkf(x,y)dxdy=2|ijk| int limitsDf(x(L1,L2),y(L1,L2))dL1dL2.



Here |ijk|=|A|- area of ​​a triangle, x(L1,L2)=L1Xi+L2Xj+(1βˆ’L1βˆ’L2)Xk,

y(L1,L2)=L1Yi+L2Yj+(1βˆ’L1βˆ’L2)Yk.



To calculate the last integral, we apply the quadrature formula [3, Sec. 5.11].



Software implementation



General considerations



Testing is difficult in this subject domain [10], so it is important that the code be written very clearly. OOP allows you to increase the readability of the code; to use OOP, you need to select a set of concepts and formulate an algorithm in these terms. This makes it possible in the first place to pay attention to the algorithm, and only then to the implementation details.



Triangulation



To build the grid, the FreeFem ++ package is used, the triangulation is saved in the .mesh format.



The library introduces the Mesh class, which contains mesh information that is read from the file.



Build SLAU



To write the algorithm, classes of basic functions and integrands of functions (products of functions) are introduced, which are inherited from the base classes Integrand and BoundaryIntegrand . In these classes there are support (carrier) and boundary_support (boundary carrier) fields, respectively. The carrier is a list of triangles on which the function is not equal to 0. The boundary carrier is a list of boundary edges on which the function is not equal to 0. Also, the Value method is defined for classes of integrand functions, which calculates the value of the function at a given point.



The Integrate and BoundaryIntegrate functions take as input an integrand function, iterate through the carrier list and call the integration function along a given triangle (or boundary edge), which is defined in the Integrand base class ( BoundaryIntegrand ) and calls the virtual function Value .



Thus, the SLAU build code has a simple form:



 for (int i = 0; i < data.N; ++i) { for (int j = 0; j < data.mesh.nodes_num; ++j) { for (auto s : data.mesh.adjacent_nodes[j]) { sys.AddCoeff(i, j, i, s, data.a[i] * Integrate(grad(phi[s]) * grad(phi[j])) + BoundaryIntegrate(data.b[i] * phi[s] * phi[j]) ); } for (int k = 0; k < data.N; ++k) { for (auto s : data.mesh.adjacent_nodes[j]) { sys.AddCoeff(i, j, k, s, Integrate(data.q[i][k] * phi[s] * phi[j]) ); } } sys.AddRhs(i, j, Integrate(data.g[i] * phi[j]) + BoundaryIntegrate(data.b[i] * data.w[i] * phi[j]) ); } } 


Decision SLAU



To solve the SLAE, an iterative method is used, implemented in the MTL4 library.



Access to the result



To find the solution value at a given point, it is necessary in a short time to find the triangle containing the given point. For this you can use a hash table.



Divide the region enclosing rectangle.  Omegaon nmblocks where n=[W/a], m=[H/b], W,H- the length and width of the enclosing rectangle, a,b- the average size of the enclosing triangles of triangles triangles.



For each of nmblocks fill the list of triangles that intersect it. For this, let's iterate through all the triangles and find their enclosing rectangles,

for which it is easy to find the blocks crossing them.



When executing a query for a given point, we find the block to which it belongs, and we look at the list of triangles corresponding to this block, checking which of them the given point belongs to.



Testing



Testing the program was carried out on tasks with a known exact solution. With double grinding of the grid, the error decreases by about 4 times or 2 times, which indicates that the method has the 2nd or 1st order of accuracy. The reduction in the order of convergence to the 1st may be due to the inconsistency of the boundary conditions at the joints of the parts of the boundary on which different values ​​of the function are given. wi.



Conclusion



The advantage of the developed library over large solvers is its simplicity, as well as the ability to take into account the specific features of the problem (piecewise constant coefficient in the boundary condition). In the future, we plan to implement a similar library for 3-dimensional tasks, which will be used to solve optimal control problems for the boundary coefficient in the complex heat exchange model, where this coefficient is piecewise constant [11].



Sources



  1. Alekseev G.V. Numerical methods of mathematical physics. Introduction to the finite element method. Vladivostok: Dal'nauka, 1999.
  2. Dautov R.Z., Karchevsky M.M. Introduction to the theory of the finite element method. Kazan: KSU, 2004.
  3. Zienkiewicz OC, Taylor RL, Zhu JZ The finite element method. Elsevier, 2005.
  4. Segerlind L. Application of the finite element method. M .: Mir, 1979.
  5. The finite element method on the example of the Poisson equation // Habrahabr, 07.27.2015.
  6. Writing the FEM of the calculator in less than 180 lines of code //

    Habrahabr, 12/01/2015.
  7. Practice of using Freefem ++ // Habrahabr, 06.06.2013.
  8. Gockenbach MS Understanding and finite element method. SIAM, 2006.
  9. Smith IM, Griffiths DV, Margetts L. Programming the finite element method. Wiley, 2014.
  10. Why unit tests do not work in scientific applications // Habrahabr, 04/26/2010.
  11. Grenkin G.V. Algorithm for Solving the Boundary Optimal Control Problem in the Model of Complex Heat Exchange, Dal'nevost. Math journals 2016. V. 16. No. 1. P. 24-38.


')

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



All Articles