An object oriented finite element package for Matlab

0 downloads 0 Views 4MB Size Report
We introduce oFEM, an efficient and flexible finite element environment for Matlab. ... make the introduced code a suitable tool for education, application, and ...
Applied Mathematics and Computation 334 (2018) 117–140

Contents lists available at ScienceDirect

Applied Mathematics and Computation journal homepage: www.elsevier.com/locate/amc

oFEM: An object oriented finite element package for Matlab Michael Dudzinski∗, Marco Rozgic`, Marcus Stiemer Department of the Theory of Electrical Engineering, Helmut Schmidt University, Holstenhofweg 85, 22043 Germany

a r t i c l e

i n f o

Keywords: Matlab code vectorization Finite elements Stiffness and mass matrices Waveguide Linear elasticity

a b s t r a c t We introduce oFEM, an efficient and flexible finite element environment for Matlab. The proposed code is fast due to vectorization and scalable in terms of computation time. It further offers interfaces to pre and post processing tools, to import meshes and export computed data. A strict object oriented approach makes it flexible, easily extensible and applicable to a broad range of applications. © 2017 Published by Elsevier Inc.

1. Introduction The mathematical modeling software Matlab has become an indispensable tool in both research and education. It allows for quickly developing and testing algorithms. If used carefully and with strictly vectorial data structures, it can even be an alternative to compiler based languages like C++ or Fortran. Especially in education the effort to learn the Matlab’s language fundamentals is lower. When it comes to modeling and solving problems given by partial differential equations via the finite element method (FEM), several Matlab implementations with different levels of complexity and performance are available. The pioneering work presented in [1] offers a clear and structured introduction to the FEM for Matlab. Later, in [2] and [3], it was modified to improve performance. Another example that offers a broad variety of basis functions and different finite element ansatz spaces, like Nédélec edge elements, is the software package iFEM, presented in [4]. However, a unified modeling tool combining a clear approach, competitive performance, interfaces to both professional computer aided design (CAD) and post processing software tools, and flexibility is still missing. With the presented package oFEM we aim to fill this gap. It uses a strict vectorial programming approach and avoids costly for loops, which leads to reasonably fast FEM code. By adding interfaces to CAD modeling software packages, mesh creation is decoupled from the analysis. Yet, no overhead is created since direct mesh import of the widely used Abaqus format is supported. Results can be visualized and analyzed through post processing tools supporting the unstructured cell data (UCD) format, to which oFEM offers direct export. In oFEM geometry and function space discretizations are separated from each other. This is achieved by exploiting Matlab’s capability for object oriented programming. The flexibility and clear structuring gained by this paradigm make the introduced code a suitable tool for education, application, and research. Environments offering this level of flexibility and aiming for a unifying approach already exist for other programming languages, like Python and C++. Two prominent examples are the FEniCS project [5] and the deal.II library [6]. The paper is structured as follows. In Section 2 we shortly recapitulate the basic ideas of mathematical FEM modeling. This is done in order to present the matrices and vectors that are to be assembled in a way that makes the application of Matlab worthwhile. In principle, we rewrite all quantities as inner products, which can be swiftly evaluated by Matlab. The ∗

Corresponding author. ` [email protected] (M. Stiemer). E-mail addresses: [email protected] (M. Dudzinski), [email protected] (M. Rozgic),

https://doi.org/10.1016/j.amc.2017.11.042 0 096-30 03/© 2017 Published by Elsevier Inc.

118

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

firm reader may skip this part. Section 3 covers the key ingredients behind oFEM as well as its interface. In Section 4 we demonstrate the capabilities of oFEM by solving two examples: one from electrical engineering, a waveguide, and another from linear elasticity. Finally, we compare oFEM to two recent Matlab implementations of the FEM, namely fastFEM, as introduced in [3], and iFEM by Chen [4]. The appendix details the classes and includes the code for the two examples. The oFEM package can be obtained via GitHub [7]. 2. Finite element method This section is intended to introduce basic notions that are heavily used throughout the text and are crucial for understanding the algorithmic ideas. Vectors are always understood as column vectors and are denoted in bold lowercase latin letters, matrices are referenced as bold uppercase latin letters, and scalars are written as lowercase latin letters. The set {1, . . . , n} with n ∈ N is shortly denoted as n. Lastly, the standard inner product between two vectors u, v ∈ Rn is written as u · v. All differential operators appearing in this text differentiate with respect to the spatial variable x ∈ Rd , d ∈ N. If we desire to apply differential operators using different coordinates these will be emphasized. 2.1. General boundary value problem Let d ∈ N denote the spatial dimension and let  ⊆ Rd be a domain with Lipschitz boundary. Furthermore, let L : → C 0 () be a second order linear elliptic differential operator in divergence form, i.e., there exists a continuously differentiable field C 2 ()

A :  → Rd,d ,  = ( ai j )

and continuous fields

b :  → Rd , and c :  → R,  = ( bi )

where A(x) is symmetric and positive definite for all x ∈ , such that

Lu = −

d 

d    ∂i ai j ∂ j u − bi ∂i u + cu = − div (A∇ u ) − b · ∇ u + cu.

i, j=1

(2.1)

i=1

At first, we desire to solve the following problem written in strong form

Lu = f

in ,

u|∂  = u∂ ,

for some Lipschitz function u∂ . Problems with additional boundary conditions of Neumann type will be considered in Section 2.2.5. Using the standard Sobolev spaces and the typical assumptions, cf. [8], the associated weak form reads Problem 2.1 (Dirichlet Boundary Value Problem, Weak Form). Let A, b, and c be measurable and bounded. Given a function u∂ ∈ H1/2 (∂ ) and a function w ∈ H 1 () with w|∂  = u∂ in the sense of traces, find u˜ ∈ H10 (), such that





∇ ϕ · A∇ u˜ + ∇ ϕ · bu˜ + ϕ cu˜ dx =





ϕ ( f − cw ) − ∇ ϕ · A∇ w − ∇ ϕ · bw dx

(2.2)

holds, for all ϕ ∈ H01 (). The weak solution is then given by u := u˜ + w. Next, the standard Galerkin projection is made: we replace all functional spaces by their finite dimensional counterparts. 1 () is assumed to be a basis of the M-dimensional approximation H 1 () to H 1 (). To construct The family {ϕi }i∈M ⊆ HM M 0 1 (), we decompose the domain  into N geometrically sufficiently simple, pairwise disjoint subdomains  , for k ∈ N, HM k

˚k  ˚ j = ∅ for k = j. The collection {k }k ∈ N together with the corresponding edges and vertices, i.e.,  = k∈N k and  constructed via incidence of parts of subdomain boundaries, is called a mesh (see, e.g., [9] for a formally precise defini1 () as the family of all functions in H1 () globally, whose tion). Using this mesh, we define the discrete function space HM restriction to an individual mesh element is a polynomial of degree s. Remark 2.2. For a problem whose solution does globally not possess more than H1 ()-regularity, a simplex mesh with globally continuous, piecewise linear basis functions already yields an asymptotically optimal approximation by the resulting Galerkin method. However, dependent on the particular physical problem the coefficients aij , bi , and c, as well as the right-hand side f and even the boundary ∂  might have a higher regularity than assumed in Problem 2.1. In this case the solutions may also be of higher regularity, at least locally, i.e., on compact subsets of . Therefore, piecewise polynomials of higher degree (s > 1) may improve the performance of the method and will consequently be considered in the following. The restriction of one of these functions to an individual mesh element, i.e., a finite element (here, particularly Ps elements for simplex meshes), is now uniquely defined by the values of a finite number of functionals, the so called degrees

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

119

Fig. 1. Mapping reference triangle to element k .

of freedom (DOFs). It is widely known how to choose the DOFs on each mesh element for a given function space to be projected on. For P1-elements, e.g., which are globally continuous, the DOFs are exactly the vertices of the simplices chosen as elements, whereas for P2-elements, additional points on the faces are included. 1 (), which are algebraically dual to the characterizing degrees of freedom, we Using the basis functions (ϕ i )i ∈ N of HM define the global quantities sij , dij , mij , and fj and their local counterparts ski j , dikj , mki j , and f jk by N 

∇ ϕi · A∇ ϕ j dx,  k=1  k  

si j :=

di j :=

N  k=1

k



ski j := N 

mi j :=

k=1

k



dikj :=

c ϕi ϕ j dx,



∇ϕi · b ϕ j dx,  

fi :=



N  k=1

mki j :=

ϕi f d x .   k   fik :=

The considered basis functions dual to the degrees of freedom are called shape functions. Remark 2.3. We consider only nodal elements in this work. In distinct situations, e.g., in the case of the Maxwell’s equations, the nodal basis is not always appropriate. There, a so called edge basis is preferred: instead of the values on a set of points xj , a set of functionals Lj associated to the set of edges is chosen as DOFs. Any edge based functional Lj has the property L j (ϕi ) = δi j . The corresponding finite elements are known as Nédélec edge elements. We refer to [8] for a general discussion on higher order finite elements and to [10] for an in-depth discussion about Nédélec edge elements and problems involving Maxwell’s equations. 2.2. Transformation to the reference element The standard approach is to perform most of the computations on a reference simplex rather than on each individual element of the mesh. This is achieved by using diffeomorphisms k mapping the reference simplex  to the according element k of the mesh, as shown in Fig. 1. Thus, if the global coordinates of the vertices of k are xk1 , . . . , xkd+1 , the mapping k reads

k :  → k ,

x˜ →





xk2 − xk1 ,

...,



xkd+1 − xk1



x˜ + xk1 .

Jk :=

Using the family {k }k ∈ N we can perform the computations of the integrals in the reference system. To this end we define ϕ˜ i := ϕi ◦ k and ∇˜ := ∇x˜ . 2.2.1. Stiffness matrix For ski j we have



ski j =

k

=

k

=

k

(∇ϕi (x ) ) A(x )∇ϕ j (x ) dx

        ∇ ϕ˜ i ◦ −1 (x ) A(x ) ∇ ϕ˜ j ◦ −1 (x ) d x k k  −        ˜ ϕ˜ i ◦ −1 (x ) A(x ) J− ∇ ˜ ϕ˜ j ◦ −1 (x ) dx Jk ∇ k k k

= |det Jk |



 − Jk

   ∇˜ ϕ˜ i (x˜ ) A(k (x˜ )) J− ∇˜ ϕ˜ j (x˜ ) dx˜. k

120

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

Since A is symmetric, it possesses a Cholesky decomposition A = CC with a lower triangular matrix C. Therefore we can write

ski j = |det Jk |



˜ ϕ˜ i (x˜ )) ((J−1 C(k (x˜ ))) ∇ ˜ ϕ˜ j (x˜ )) dx˜. ((J−1 C(k (x˜ ))) ∇ k k

For Ps-elements we have K :=

s+d

shape functions ϕ˜ i on the reference simplex . Therefore, we set

d

˜ ϕ˜ 1 , . . . , ∇ ˜ ϕ˜ K ] ˜ := [∇ D

d˜ := (ϕ˜ 1 , . . . , ϕ˜ K ) .

and

(2.3)

Since the shape functions are known, the integral can be computed explicitly, however, it is more convenient to use a quadrature rule instead. To this end, let x˜ j for j = 1, . . . , Nq be the nodes of an Nq -points quadrature rule and let w j be the



corresponding weights. By setting Ck := J−1 C(k (x˜ j )) k j

Sk = |det Jk |

Nq 



D(x˜ j ), we compute the per-element stiffness matrix as



w j Ckj Ckj .

(2.4)

j=1

j

j

The product Ck Ck can be computed simultaneously for all elements very efficiently in Matlab, as will be seen in Section 3.1.

2.2.2. Damping matrix For cikj we have

cikj =

(∇ϕi (x ) ) b(x ) ϕ j (x ) dx

k

= | det Jk |



 −1

Jk b(k (x˜ ))





Thus, setting dk := J−1 b(k (x˜ j )) k j

Dk = | det Jk |

Nq 

j





∇˜ ϕ˜ i (x˜ )ϕ˜ j (x˜ ) dx˜ .

˜ (x˜ j ), the per-element damping matrix reads D

j

w j dk dk .

(2.5)

j=1

2.2.3. Mass matrix For mki j we have

mki j =

k

c ϕi (x )ϕ j (x ) dx = | det Jk |



c (k (x˜ ))ϕ˜ i (x˜ )ϕ˜ j (x˜ ) dx˜ .

With d j := d˜ (x˜ j ), the per-element mass matrix reads

Mk = | det Jk |

Nq 

j

w j c (k (x˜ j ))d d . j

(2.6)

j=1

2.2.4. Load vector For fik we have

fik =

k

ϕi (x ) f (x ) dx = | det Jk |



ϕ˜ i (x˜ ) f (k (x˜ ) ) dx˜ .

Consequently, the per-element load vector reads

f = | det Jk | k

Nq  j=1



 

w j f k x˜ j

j

d .

(2.7)

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

121

Using Eqs. (2.4)–(2.7) the respective matrices and vectors can efficiently be computed. Moreover, if the material data b and c are assumed to be constant on the distinct elements, the integrals yielding Eqs. (2.5) and (2.6) can be computed independently of the mesh, since they only rely on information available on the reference structure . 2.2.5. Neumann boundary condition So far, we only assumed Dirichlet boundary conditions. If, additionally, Neumann boundary conditions are to be incorporated, the strong and weak formulations change a little bit and an additional term has to be considered in the latter case. Let  N ⊂ ∂  denote the part of the boundary at which Neumann conditions are prescribed, let g ∈ C 0 (), and let n ∈ Rd be the outward unit normal vector to the boundary. We further assume homogenous Dirichlet boundary conditions on ∂ ࢨ N . Then, the strong form of the boundary value problem reads as:

Lu = f, in ,

∂n u = g, in  N . The weak form results similar to the one from Problem 2.1 by application of Gauss’s theorem, and reads:





∇ ϕ · A∇ u + ∇ ϕ · b u + ϕ c u d x =





ϕ f dx +



N

ϕ g d x,

∀ϕ ∈ H01 ().

In the weak context, g ∈ H −1/2 ( N ) is the suitable regularity assumption for Neumann data. This yields the additional discrete contribution



gki :=

kN

φi (x )g(x ) dx = | det Jk |



N

ϕ˜ i (x˜ )g(k (x˜ ) ) dx˜ ,

with the per-element vector

gk = | det Jk |

Nq 



 

w j g k x˜ j

j

d .

(2.8)

j=1

Here, we denote those portions of the boundary referring to the Neumann condition by kN and N , respectively. This term can be treated exactly the same way as (2.7), where the integration has to be performed over a codimension-one domain. 2.2.6. Quadrature rule Here, the Gaussian quadrature rule is employed. It is known to be exact for univariate polynomials up to degree 2Nq + 1, when using Nq integration points (nodes), see [11]. When applying these quadrature rules on multivariate polynomials, no such results exist for general geometries, as is pointed out by Mousavi et al. [12]. Gaussian quadrature on simplices used in the context of finite elements, however, is well studied and understood, see, e.g., the text book [13]. The number Nq of quadrature points used in the oFEM implementation by default is the smallest one that preserves the order of accuracy of the employed Galerkin method, i.e., for Ps-elements the order is s. To conveniently treat weights and nodes we perform all computations in terms of barycentric coordinates, cf. [14]. For a (d + 1 )-simplex K ⊂ Rd , with vertices v1 , . . . , vd+1 the barycentric coordinates λ1 , . . . , λd+1 ∈ [0, 1] of a point x ∈ K are those numbers, such that

x=

d+1 

λi vi

and

i=1

d+1 

λi = 1.

i=1

As already mentioned, for explicitly computing the integrals in (2.5) and (2.6), the following formula is very useful

d+1  K

d+1 

λki i dx = d!|K | 

i=1

i=1

d+

ki !

d+1  i=1

 ,

(2.9)

ki !

where |K| denotes the (d + 1 )-dimensional volume of the simplex, and k1 , . . . , kd+1 are arbitrarily given positive integers. 3. The oFEM package To make oFEM conveniently employable to solve finite element problems, a tight structuring principle is imposed. The main idea is to split geometry discretization from function space projection as far as possible. Therefore, there exist two main classes ofem.mesh and ofem.finiteelement. The former one is responsible for providing information that is solely related to geometry. These are, in particular, the inverse of the Jacobians of the diffeomorphisms k and their corresponding

122

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

determinants. Furthermore, additional information needed for handling Neumann boundaries can be retrieved here. The whole interface can be seen in A.1. The class ofem.finiteelement encapsulates information coming from the function space projection. These particularly comprise the values of the shape functions and their gradients evaluated at the quadrature points. Although, in a strict manner, discretization has to be performed at three different levels, i.e., the geometry, the function space, and the occurring integrals, up to now, integral computation has not been formally separated from the class for function space discretization, and the weights and quadrature points are still provided by methods of that class. It is part of future work to provide a separate class for integral discretization also. Again, the whole interface is detailed in A.2. These two classes are used to create an object responsible for assembling the desired structures. The following code snippet shows the typical usage.





The class ofem.elliptic corresponds to the operator defined in (2.1) with A := I, b := , and c := 1. To treat 1, . . . , 1 problems with b = 0 and c = 0, appropriate options can be set. In lines 2 and 3, an instance of the ofem.mesh class is created and the mesh saved in the Abaqus file “myAbaqusMesh.inp” is loaded. In line 5 we define piecewise multivariate polynomials of total degree one as bases for the discrete function space. Finally, in lines 7 and 8, an instance of the ofem.elliptic class is created and the stiffness and mass matrices are assembled, as requested through the set options. 3.1. Element-wise operations Implementations of the FEM in programming languages, as C++ or Fortran, perform a large loop over all elements of the mesh. Although current Matlab versions implement a concept of just in-time (JIT) compilation to speed things up, large loops are generally very inefficient in Matlab. If the loop body has no simple structure, the JIT compilation cannot be applied, and, hence, the body is interpreted in each cycle. A further important factor determining the efficiency of the code is the management of sparse data structures. All assembled matrices are sparse. A naive index scheme for sparse matrices requires a frequent resizing of the data tree during assembly, and leads to an extremely poor efficiency. Consequently, it is essential for high performance that the sparsity pattern and the data are defined a priori. Therefore, on the one hand the concept of vectorization, eliminating the loop problem, is applied, and on the other hand the concept of the sparse constructor, resolving the dynamic resizing issue, is used. In addition to avoiding a slow interpreter and permanent memory allocation, these concepts allow for a hardware optimized treatment of the required operations such as implemented in the underlying BLAS (basic linear algebra subprograms). Further, a parallel processing of assembly operations becomes possible with the data structures employed in oFEM. Performing the operations needed during assembly simultaneously for all elements is achieved by a reinterpretation of the matrix multiplication. To retrace the underlying idea, consider exemplarily two families of matrices, {Ak } ⊂ Rn,s and {Bk } ⊂ Rs,m . We are interested in computing the following product

Ck := Ak Bk ∈ Rn,m for each k. The (i, j)th entry in Ck is computed by the standard inner product between the ith row of Ak and the jth column of Bk . To perform the operations for all k simultaneously, assume

A(:, :, k ) := Ak ,

and

B(:, :, k ) := Bk ,

where the usual Matlab notation is used, and note that Matlab’s storage scheme is column-major order. Then

produces a 3D array C with

C ( :, :, k ) = Ck . To encapsulate this idea for performing arbitrary element-wise operations, such as addition or any form of multiplication, the class ofem.matrixarray has been developed. It is derived from Matlab’s built-in class double. It’s constructor is a single

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

123

argument, a 3D Matlab array, which is structured as in the example above. Generally, the third index, i.e., the third dimension, refers to the particular mesh-element or -vertex, or -edge the element-wise (or vertex-wise etc.) data are associated with. For example, the array of all the nodes’ coordinates of a mesh is a 3D array co with size(co)=[d,1,N] and the column vector co(1:d,1,k) containing the coordinates of the kth node. When using tetrahedral elements, the computation of J− and k det Jk , for all k, becomes as easy as:

3.2. Usage of the sparse constructor Next, the use of the sparse constructor is described in the particular case of the assembly of the stiffness matrix. For all other global matrices and vectors (cf. (2.4)–(2.8)) the procedure is practically the same. For this purpose, we consider the following code snippet

In lines 14–18 all non-zero entries in the stiffness matrix are computed, at most K2 N. The K × d × Nq array dphi holds the values of the shape functions’ gradients. The first dimension indexes the shape function, the second the partial derivative, and the last the quadrature point. The loop in lines 15–18 iterates over the quadrature points and needs not to be vectorized, due to the small number of iterations. The d × d × N array DinvT stores the transposed inverse of the Jacobian of the respective diffeomorphism k for each element. In line 16, the operation

 

˜ x˜ j J− D k

is performed for all elements at once. The products

 −    −   ˜ x˜ j Jk D

˜ x˜ j Jk D

are computed in line 17, again for all elements simultaneously. At this point we have nearly computed all entries of the matrices in (2.4); the missing multiplication with | det Jk | is performed in line 20. It remains to create the correct index vectors mapping the local indices to the corresponding global indices of the global matrices, which label the system’s degrees of

124

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

Fig. 2. The typical steps of an oFEM simulation.

freedom. This so called DOF-management is done in lines 22–24. As a last step the sparse constructor is invoked in order to create the (sparse) stiffness matrix. We would like to point out that the sparse constructor is “intelligent” in such a way, that when encountering I (s ) = I (t ) and J (s ) = J (t ) for s = t, it sums the entries S(s) and S(t), and does not just overwrite them, which results in the correct global summation.

3.3. The oFEM interface We will show how the oFEM user interface works by going through the steps that are required to perform a simulation. The corresponding workflow is also depicted in Fig. 2.

3.3.1. Geometry design At the beginning of a finite element analysis (FEA) a model of the problem’s geometry has to be translated into a computer model. For this purpose many computer aided design (CAD) software applications are available. With oFEM we provide an interface to the Abaqus file format, cf. [15]. Our routines, so far, have been used with Abaqus files produced by the meshing and design software Trelis and focus on import of nodes, elements, and sidesets. Other CAD programs also offer the possibility to export into the Abaqus format. The final CAD model of the geometry contains the mesh, i.e., the node and element information, as well as information about specially marked surfaces, areas, and nodes. This information includes Dirichlet boundary sets, Neumann boundary sets, areas of different materials, and other particular parts of the geometry. Constructing the mesh and the geometry by using CAD software offers the advantage of decoupling this process from the actual FEA. It can be cumbersome to produce good meshes, in the sense that the quality of the triangulation is good enough for a reliable computation, cf. [16] for an overview on mesh metrics. Further, when aiming for automatic geometry changes, some commercial CAD programs offer script controls, making it possible to automatically change geometry and mesh aspects, once these are appropriately parameterized.

3.3.2. Solving a problem step by step We demonstrate how the implemented classes can be used to solve a FEM problem. The arrangement and the individual classes are motivated by the actual problem statement and theoretical analysis of the solution method, as described in Section 2.

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

125

1. Load a mesh from a previously designed geometry. After initialization of a mesh object the mesh is loaded by the load_from_inp method:

2. The function space discretization is set, by default these are P1 FEMs.

If higher order elements should be used, the mesh has to be extended first, in the sense that additional nodes have to be introduced for each element.

3. The choice of the equation corresponds to the particular format of the unknown field, i.e., scalar or vectorial, as well as to different choices of A, b, and c in the definition of the operator in (2.1). We provide the following equation objects:

The two equation objects generated above already cover a wide class of physical problems. Since the class ofem.elliptic comprises a Helmholtz equation with complex coefficients, time harmonic problems for Maxwell’s equations can readily be treated: an adequate gauge condition decouples the time harmonic Maxwell equations, both, in the full wave model and in the magneto-quasi-static approximation, into complex vector and scalar Helmholtz equations for the magnetic vector and for the corresponding scalar potential, respectively. For the time being, the vector Helmholtz equation can be solved if additional symmetries allow for the reduction to a scalar problem (see the first example provided in Section 4). Furthermore, the spatial semi-discretization of time-dependent problems typically leads to Helmholtz-type equations independently of the order of the individual discretization steps. Hence, such problems can also be handled by the ofem.elliptic class. For the time being, marching schemes have to be provided by the user. 4. To control which quantities, as discussed in (2.4)–(2.8), should be assembled, we provide an options interface. By setting appropriate options the user can control if, for example, mass matrices should be assembled, or Neumann boundary conditions have to be taken into account. The following options are available:

126

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

5. The actual assembly method then takes the form:

The variable asm is a structure containing the assembled quantities, that read:

Optionally eq.assemble provides some additional output values, which aren’t discussed here. We assemble all quantities, i.e., stiffness and mass matrices, as well as load and pressure terms for every predefined part of the geometric model individually. These have the same overall size as the corresponding global entities. Hence, the part matrices simply need to be added together to obtain the global stiffness or mass matrix. Employing part matrices allows for an efficient handling of inhomogeneous spatial material distributions. Moreover, with this approach we are able to compute solutions only on parts of the geometry, which can be used to apply decoupling strategies, as, for example, the Schwarz iteration method, cf. [17]. This strategy also offers more flexibility, if, for example, different material distributions should be tested. Exemplarily, a computation of a global stiffness matrix for a geometry with two different, but inherently homogeneous parts would look like this:

6. To incorporate Dirichlet boundary conditions, the ofem.mesh class provides a function to retrieve all global indices of nodes corresponding to the specified sideset. This method takes the indices of the sidesets as input:

The returned variable is a cell array with size numel(diriIdx) containing in each of its entries the corresponding global node indices. Since the solution is only sought for so-called free nodes, i.e., those nodes, where the values are not prescribed, the node list can be used to compute the list of free nodes on the one hand, and to set the prescribed values on the other. Suppose the index vector contains two entries, then the steps to perform aforementioned computations are:

7. The linear system can be solved for the free nodes by any solver. For problems involving a moderate number of DOFs, Matlab’s backslash operator is sufficient:

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

127

8. Some applications require the computation of the solution’s gradient. This can be done by calling the corresponding routine:

The algorithm implemented is the one described in [18] and is a modification of the famous approach proposed by Zienkiewicz and Zhu in [19]. The same technique is used to compute the deformation gradient and the stress and strain tensors from the displacement field when using the ofem.elastic class:

9. Finally, oFEM comes with a method to export the solution into the Unstructred Cell Data (UCD) file format. This file format is often used to export solutions of FEM simulations. It can be imported into more sophisticated visualization and post-processing programs like ParaView, cf. [20]. To do so, simply call:

4. Numerical results In this section we demonstrate the use of oFEM by solving two problems. The first one is related to an electromagnetic wave propagating through a waveguide. The second one is set in the framework of linear elasticity. Further, we compare oFEM to two recent FEM implementations in Matlab, namely fastFEM, as introduced in [3] and iFEM, as presented in [4]. It turns out that, although oFEM’s interface has more overhead than fastFEM’s, and although the ofem.matrixarray class does not exploit the stiffness matrix’s symmetry as iFEM does, oFEM is slightly faster. 4.1. Waveguide We consider a long hollow waveguide with congruent, rectangular cross sections and perfect electrical conductor (PEC) boundary and whish to compute its TE-Eigenmodes (transversal electrical eigenmodes), cf. [21]. Starting from the timeharmonic Maxwell equations and assuming a wave propagating into z-direction, the magnetic field Bg can be expressed as





Bg (x, y, z, t ) =  B(x, y )ei(kz−ωt ) . Here k is the wave number and ω the angular frequency. Further, assuming no free charge, i.e., div Bg = 0, and = 0, where ϱ is the electric charge density, we arrive at the eigenvalue problem for the B-field:

  B + μεω2 − k2 B = 0,

(4.1)

with the permeability μ and the permittivity ε . It can be shown, [21], that the x and y components of the B-field can be represented in terms of Bz and the z component of the E-field, i.e.,

Bx ex + By ey =

i

μεω2 − k2

(k∇ Bz + μεωez × ∇ Ez ).

For a TE-wave the z component of the electric field vanishes everywhere. Thus (4.1) reduces to

  Bz + μεω2 − k2 Bz = 0,

with homogeneous Neumann boundary. For an in-depth derivation see, e.g., [21].

(4.2)

128

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

Fig. 3. x component of the H-field in Am−1 .

It is common to write μ = μ0 μr and ε = ε0 εr , with the relative permeability μr and the relative permittivity ε r . In particular we choose μr = εr = 1 and excite the wave guide by a triggering frequency of 2.016 GHz. The eigenvalue problem is solved for the first four wave numbers k1 , . . . , k4 resulting in k1 = 41.7824 m−1 , k2 = 42.0180 m−1 , k3 = 42.1353 m−1 , and k4 = 42.1353 m−1 . The first four H-field eigenmodes can be seen in Figs. 3 and 4. 4.2. Linear elastic gong We consider a gong like structure, with a diameter of 2016 m−1 and with a central thickness of 200 mm. At time t = 0, the gong is hit in an area with a radius of 100 mm in the center of one side. The hit is modeled as a surface pressure of 0.001 Gpa. The whole situation is modeled by a system of linear elastic equations accounting for inertial forces, where Hooke’s law for the Cauchy stress tensor σ is assumed:

div σ = ρ ∂t2 u,

σ = Cε ,  1 ε = Du + (Du ) , 2 ⎡ 2μ + λ λ λ 2μ + λ ⎢ ⎢ λ λ C=⎢ ⎢ 0 0 ⎣ 0 0

0 0

λ λ 2μ + λ 0 0 0

0 0 0

μ 0 0

0 0 0 0

μ 0

0 0 0 0 0

⎤ ⎥ ⎥ ⎥. ⎥ ⎦

(4.3)

μ

Here, ε is Green’s strain tensor, C the fourth order compliance tensor in Voigt notation, and μ and λ are the Lamé constants. Further, ρ ∈ R denotes the density of the respective material, u :  × [0, T ] → R3 the displacement field, and t ∈ R the time.

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

129

Fig. 4. y component of the H-field in Am−1 .

To model the load scenario assumed above we apply Neumann boundary conditions on the particular surface. The surface force acts into inward normal direction. Further, the initial values for u and ∂ t u are assumed to be zero. The initial value for ∂t2 u is determined by solving the system

Ma = f , where M is the mass matrix, f the resulting right hand side and a is the discrete approximate of ∂t2 u at time t = 0. The material parameters of the gong are:

λ = 115.3846GPa, μ = 76.9231GPa, ρ = 7.83E − 06 × 10−6 kgmm−3 . The material taken into account is steel. The data is taken from WebElementsTM (see [22]), which makes use of [23–26]. The total time we take into account is T := 5s. We apply the Newmark-β method, as proposed by [27], for the time integration. The time steps are chosen to be 10 ms, yielding 500 snapshots. The model is constructed and meshed with the CAD program Trelis. The final mesh consists of 110 824 tetrahedra, see Fig. 5. We solve above problem with oFEM, by using the elastic class. Via the specification of the corresponding options the assembly routine is asked to include Neumann boundary conditions and to compute stiffness and mass matrices. The code to realize the solution with oFEM can be found in B.2. We show the Euclidean norm of the displacement field, u, in Fig. 6, the Euclidean norm of the independent components of the strain tensor,

ε = (εxx , εyy , εzz , εyz , εxz , εxy )  in Fig. 8, and the Euclidean norm of the stress tensor,

σ  = (σxx , σyy , σzz , σyz , σxz , σxy ) 

130

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

(a) The CAD model of the gong, in side view.

(b) The mesh of the gong, front view. The shaded area is where the impact happens.

Fig. 5. The CAD model and the mesh of the gong like structure created with Trelis.

Fig. 6. The norm of the displacement field, u.

in Fig. 7, for snapshots with serial numbers contained in {0, 249, 350, 499}. 4.3. Comparison of running times The developed code is compared to two available fast Matlab implementations of the FEM. We compare against fastFEM presented in [3] and iFEM, a versatile FEM package introduced in [4]. For comparison, the time the different codes needed to

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

131

Fig. 7. The norm of the stress tensor, σ .

Table 1 The number of respective elements and nodes for dimensions 2 and 3 and the according finite element degree. dim

# Elements

# DOFs P1

# DOFs P2

2D

226 904 3616 14464 57856 231424 925696 3702784 9797 78376 627008 5016064

134 493 1889 7393 29249 116353 464129 1853953 1998 14471 110033 857937

493 1889 7393 29249 116353 464129 1853953 7410689 14471 110033 857937 (6775393)

3D

assemble the stiffness matrix of a scalar-valued Laplace equation for 2D and 3D and, as far as they were available, for P1 and P2 elements is recorded. The geometries taken into account are a simple square in 2D and a cube in 3D. Meshes and their refinements were created with Trelis. The resulting numbers of elements and respective numbers of nodes, determining the number of matrix entries, are given in Table 1. All computations were performed with Matlab R2015a on a 3.4 GHz Intel® CoreTM i5 with 16 GB RAM. Every run was repeated 9 times and the mean of the respective running times is computed and reported.

132

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

Fig. 8. The norm of the strain tensor, ε.

Fig. 9. Computation times for the three available codes for 2D and 3D P1 elements.

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

133

Fig. 10. Computation times for iFEM and oFEM in the case of P2 elements.

For 2D P1 elements iFEM and oFEM both perform better than fastFEM, where oFEM performs slightly better than iFEM, as Fig. 9 shows. However, in, e.g., iFEM the user has to call different routines depending on the spatial dimension or the degree of ansatz polynomials. In oFEM, the user simply calls the assemble routine that hides all the switch cases from the user and, consequently, these timings are included. The same behavior is observed in the 3D case. In Fig. 10 the assembly time of iFEM is compared against the time oFEM needs for P2 elements. In the third software, fastFEM, no P2 elements are implemented, hence, no comparison is possible. We did not compute and compare the assembly times for the finest 3D mesh in the case of P2 elements, since the test computer had to swap memory during the assembly routines. This makes the computation times less comparable and, consequently, such examples have been excluded from the analysis. Again, we found oFEM being slightly faster than iFEM. The difference between oFEM and iFEM becomes larger when the number of elements increases. The found computation times are in good agreement with the results reported in [28] for various fast Matlab FEM realizations.

5. Conclusion We presented the Matlab FEM package oFEM. By exploiting the inner product structure of the matrices and vectors that are to be assembled, avoiding for loops and using the sparse constructor, oFEM is not slower when compared in terms of speed to other modern FEM implementations in Matlab. In addition to speed, oFEM offers interfaces to CAD modeling software to detach meshing from the actual FEM. Further, results can be exported into UCD files, which can be treated with post processing software tools. The modeling interface of oFEM follows typical paradigms of mathematical modeling in terms of separating geometry discretization from function space discretization. This program structure makes the package easily extensible to implement new finite elements or support different mesh file formats. This is achieved by using object oriented programming. The presented code offers a unified interface to FEA and modeling. It allows for a broad range of improvements, like adaptivity, mixed finite elements, domain decomposition, parallelization, and so on. The oFEM code itself and its latest updates can be downloaded via GitHub [7]. We, the developers, welcome any new contributors to the project. Please feel free to contact us.

Acknowledgment The oFEM package was developed for the summer school 2015 on finite elements at the Helmut Schmidt University, Hamburg (FEM@TET). The authors would like to thank the participants: Othmane Jerhaoui, Julian Gasquez, Dounya Bendahhou, Camille Toulemonde, Elise Couturier, Inès Barbary, Michael Hagel, and Karl Friedrich Rädel. Michael Dudzinski would like to thank the BMBF project MOST under the contract number 03SF0456 for financial support.

134

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

Appendix A. oFEM classes A1. ofem.mesh All mesh depended data is computed in the mesh class. Most important for the matrix and vector assembly is the jacobiandata function. Here, the matrix Dk − and the determinant det Dk are computed for every element. Both are determined for all elements simultaneously and stored in appropriate structures:

This routine is called to export data to a UCD formatted file. The first entry in the cell array meta carries the name of the variable to be exported, the second entry contains the data, and the third entry is the unit. This is exactly the data to be provided for every function in a UCD file as defined by the UCD interface. For now, only lines (1D elements), triangles (2D elements) and tetrahedra (3D elements) are supported. We plan to implement quadrilaterals and hexahedral structures in the future.

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

135

Remark A.1. The method load_from_inp exploits C++ functions to read and process Abaqus ∗ .inp files fast. C++ is considerably faster than Matlab when it comes to ASCII file processing. Both, create_faces and create_edges are preparations for the enhancement of oFEM to edge elements. A2. ofem.finiteelement

Returns the integral ∫ ϕ i ϕ j dx. As before, we have to stress that for the time being only so called Ps, s ∈ {1, 2}, elements are supported. The according classes for Qs elements and Nédélec elements are under development. [29]

136

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

Appendix B. Code for the presented experiments B1. Waveguide

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

B2. Linear elastic gong The main routine looks as follows:

137

138

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

The functions SurfaceForce and Newmark_coefficients read as:

139

140

M. Dudzinski et al. / Applied Mathematics and Computation 334 (2018) 117–140

References [1] J. Alberty, C. Carstensen, S.A. Funken, Remarks around 50 lines of Matlab: short finite element implementation, Numer. Algorithms 20 (2–3) (1999) 117–137. [2] S. Funken, D. Praetorius, P. Wissgott, Efficient implementation of adaptive P1-FEM in MATLAB, Comput. Methods Appl. Math. 11 (4) (2011) 460–490. [3] T. Rahman, J. Valdman, Fast MATLAB assembly of FEM matrices in 2D and 3D: nodal elements, Appl. Math. Comput. 219 (13) (2013) 7151–7158. [4] L. Chen, iFEM: An Innovative Finite Element Methods Package in MATLAB, University of Maryland, 2008. [5] M.S. Alnæs, J. Blechta, J. Hake, A. Johansson, B. Kehlet, A. Logg, C. Richardson, J. Ring, M.E. Rognes, G.N. Wells, The FEniCS project version 1.5, Arch. Numer. Softw. 3 (100) (2015). [6] W. Bangerth, R. Hartmann, G. Kanschat, deal.II – a general purpose object oriented finite element library, ACM Trans. Math. Softw. 33 (4) (2007) 24/1–24/27. [7] M. Dudzinski, M. Rozgic, oFEM Git, 2017. https://github.com/uebermensch2k/oFEM [8] P. Šolín, K. Segeth, I. Doležel, Higher-Order Finite Element Methods, CRC Press, 2003. [9] A. Ern, J.-L. Guermond, Theory and Practice of Finite Elements, vol. 159, Springer Science & Business Media, 2013. [10] P. Monk, Finite Element Methods for Maxwell’s Equations, Oxford University Press, 2003. [11] A.H. Stroud, D. Secrest, Gaussian quadrature formulas, vol. 39, Prentice-Hall, Englewood Cliffs, NJ, 1966. [12] S. Mousavi, H. Xiao, N. Sukumar, Generalized gaussian quadrature rules on arbitrary polygons, Int. J. Numer. Methods Eng. 82 (1) (2010) 99–113. [13] W. Carroll, A Primer for Finite Elements in Elastic Structures, Wiley, 1998. [14] M.S. Floater, K. Hormann, G. Kós, A general construction of barycentric coordinates over convex polygons, Adv. Comput. Math. 24 (1–4) (2006) 311–331. [15] Hibbitt, Karlsson, Sorensen, ABAQUS/Standard User’s Manual, vol. 1, Hibbitt, Karlsson & Sorensen, 2001. [16] P.M. Knupp, Algebraic mesh quality metrics, SIAM J. Sci. Comput. 23 (1) (2001) 193–218. [17] P.-L. Lions, On the Schwarz alternating method I, in: Proceedings of the First International Symposium on Domain Decomposition Methods for Partial Differential Equations, Paris, France, 1988, pp. 1–42. [18] Z. Zhang, A. Naga, A new finite element gradient recovery method: Super convergence property, SIAM J. Sci. Comput. 26 (4) (2005) 1192–1213. [19] O.C. Zienkiewicz, J.Z. Zhu, The super convergent patch recovery and a posteriori error estimates. Part 1: the recovery technique, Int. J. Numer. Methods Eng. 33 (7) (1992) 1331–1364. [20] J. Ahrens, B. Geveci, C. Law, 36 ParaView: an end-user tool for Large-Data visualization, in: The Visualization Handbook, 2005, p. 717. [21] J.D. Jackson, Classical Electromagnetics, Wiley, New York, 1975. [22] M. Winter, Web elements, 2015, (http://www.webelements.com/). [23] D.R. Lide, CRC Handbook of Chemistry and Physics, CRC press, 2004. [24] G. Samsonov, Handbook of the Physicochemical Properties of the Elements, Springer, US, 2012. [25] G. Kaye, T. Laby, Tables of Physical and Chemical Constants and Some Mathematical Functions (1918), Literary Licensing LLC, 2014. [26] A. James, M. Lord, Macmillan’s Chemical and Physical Data, Macmillan, 1992. [27] N.M. Newmark, A method of computation for structural dynamics, in: Proceedings of the American Society of Civil Engineers, ASCE, vol. 85, 1959, pp. 67–94. [28] S. Funken, D. Praetorius, P. Wissgott, Efficient implementation of adaptive P1-FEM in MATLAB, Comput. Methods Appl. Math. 11 (4) (2011) 460–490. [29] P. Keast, Moderate-degree tetrahedral quadrature formulas, Comput. Methods Appl. Mech. Eng. 55 (3) (1986) 339–348.

Suggest Documents