Entries for November 2017

  1. Portrait of Onur Solmaz
    Onur Solmaz · Post · /2017/11/21

    Vectorial Finite Elements

    $ \newcommand{\BAhat}{\hat{\boldsymbol{A}}} \newcommand{\Buhat}{\hat{\boldsymbol{u}}} \newcommand{\Bbhat}{\hat{\boldsymbol{b}}} $

    Many initial boundary value problems require solving for unknown vector fields, such as solving for displacements in a mechanical problem. Discretization of weak forms of such problems leads to higher-order linear systems which need to be reshaped to be solved by regular linear solvers. There are also more indices involved than a scalar problem, which can be confusing. In this post, I’ll try to elucidate the procedure by deriving for a basic higher-order system and giving an example.

    The weak formulation of a linear vectorial problem reads

    Find uV\Bu\in V such that

    a(u,v)=b(v)\begin{equation} a(\Bu, \Bv) = b(\Bv) \tag{1}\end{equation}

    for all vV\Bv\in V.

    Discretizations of vectorial problems requires the expansion of vectorial quantities as linear combinations of the basis vectors ei\Be_i:

    u=i=1nduiei\begin{equation} \Bu = \suml{i=1}{\ndim} u_i \,\Be_i \htmlId{eq:discrete6}{} \tag{2}\end{equation}

    where {ui}i=1nd\cbr{u_i}_{i=1}^{\ndim} are the components corresponding to the basis vectors and nd=dimV\ndim=\dim V. Here, we chose Cartesian basis vectors for simplicity.

    We can therefore express its discretization as

    uh=I=1nnuINI=I=1nni=1nduiIeiNI.\begin{equation} \Bu_h = \suml{I=1}{\nnode} \Bu^I N^I = \suml{I=1}{\nnode}\suml{i=1}{\ndim} u^I_i \Be_i N^I. \htmlId{eq:discrete7}{} \tag{3}\end{equation}

    Substituting discretized functions in the weak formulation, we obtain

    a(uh,vh)=i=1ndj=1nda(uh,jej,vh,iei)=I=1nnJ=1nni=1ndj=1ndujJviIa(ejNJ,eiNI)\begin{equation} \begin{aligned} a(\Bu_h, \Bv_h) &= \suml{i=1}{\ndim}\suml{j=1}{\ndim} \, a(u_{h,j}\,\Be_j, v_{h,i}\,\Be_i)\\ &= \suml{I=1}{\nnode}\suml{J=1}{\nnode} \suml{i=1}{\ndim}\suml{j=1}{\ndim} u^J_j v^I_i \,a(\Be_j N^J, \Be_i N^I) \end{aligned} \tag{4}\end{equation}

    and

    b(vh)=i=1ndb(vh,iei)=I=1nni=1ndviIb(eiNI).\begin{equation} b(\Bv_h) = \suml{i=1}{\ndim} b(v_{h,i}\,\Be_i) = \suml{I=1}{\nnode} \suml{i=1}{\ndim} v^I_i b(\Be_i N^I). \tag{5}\end{equation}

    We define the following arrays

    AijI ⁣J=a(ejNJ,eiNI)biI=b(eiNI).\begin{equation} \boxed{ \begin{aligned} A^{I\!J}_{ij} &= a(\Be_j N^J, \Be_i N^I) \\ b^{I}_{i} &= b(\Be_i N^I). \end{aligned} } \tag{6}\end{equation}

    Hence we can express the linear system

    a(uh,vh)=b(vh)\begin{equation} a(\Bu_h,\Bv_h) = b(\Bv_h) \tag{7}\end{equation}

    as

    I=1nnJ=1nni=1ndj=1ndujJviIAijI ⁣J=I=1nni=1ndviIbiI.\begin{equation} \suml{I=1}{\nnode}\suml{J=1}{\nnode} \suml{i=1}{\ndim}\suml{j=1}{\ndim} u^J_j v^I_i \,A^{I\!J}_{ij} = \suml{I=1}{\nnode} \suml{i=1}{\ndim} v^I_i b^{I}_{i}. \tag{8}\end{equation}

    For arbitrary vh\Bv_h, this yields the following system of equations

    J=1nnj=1ndAijI ⁣JujJ=biI\begin{equation} \boxed{ \suml{J=1}{\nnode} \suml{j=1}{\ndim} A^{I\!J}_{ij} \,u^J_j = b^{I}_{i} } \htmlId{eq:discrete8}{} \tag{9}\end{equation}

    for i=1,,ndi=1,\dots,\ndim and I=1,,nnI=1,\dots,\nnode.

    We reshape this higher-order system as shown in the previous post Reshaping Higher Order Linear Systems:

    A^u^=b^\begin{equation} \BAhat \Buhat = \Bbhat \tag{10}\end{equation}

    by defining a map idi_d that maps original indices to the reshaped indices

    id:={[1,nn]×[1,nd][1,nnnd](I,i)nd(I1)+i\begin{equation} i_d := \left\{ \begin{array}{rl} [1,\nnode]\times[1,\ndim] & \to [1,\nnode\ndim]\\[1ex] (I,i) & \mapsto \ndim(I-1)+i\\ \end{array} \right. \tag{11}\end{equation}

    where we used 1-based indexing of the arrays. We set

    α:=id(I,i)=nd(I1)+iβ:=id(J,j)=nd(J1)+j\begin{equation} \boxed{ \begin{alignedat}{3} \alpha &:= i_d(I,i) &&= \ndim(I-1) + i \\ \beta &:= i_d(J,j) &&= \ndim(J-1) + j \\ \end{alignedat} } \tag{12}\end{equation}

    and write

    A^αβ=AijI ⁣J,u^β=ujJandb^α=biI\begin{equation} \hat{A}_{\alpha\beta} = A^{I\!J}_{ij} \quad,\quad \hat{u}_{\beta} = u^{J}_{j} \eqand \hat{b}_{\alpha} = b^{I}_{i} \tag{13}\end{equation}

    The inverse index mapping can be obtained as shown in the previous post.

    Example: Linear Elasticity

    Our initial-boundary value problem is

    divσ=ργinΩu=uˉonΩut=tˉonΩt\begin{equation} \begin{alignedat}{4} -\div\Bsigma &= \rho\Bgamma \qquad&& \text{in} \qquad&& \Omega\\ \Bu &= \bar{\Bu} && \text{on} && \del\Omega_u \\ \Bt &= \bar{\Bt} && \text{on} && \del\Omega_t \\ \end{alignedat} \tag{14}\end{equation}

    The weak formulation reads

    Find uV\Bu\in V such that

    Ωdivσvdv=Ωργvdv\begin{equation} -\int_\Omega \div\Bsigma \dtp \Bv \dv = \int_\Omega \rho \Bgamma\dtp\Bv \dv \tag{15}\end{equation}

    for all vV\Bv\in V where V=H1(Ω)V=H^1(\Omega).

    We apply integration by parts on the left-hand side

    Ωdivσvdv=Ωdiv(σv)dvΩσ:vdv\begin{equation} \int_\Omega \div\Bsigma\dtp\Bv \dv = \int_\Omega \div(\Bsigma\Bv) \dv - \int_\Omega \Bsigma : \nabla\Bv \dv \tag{16}\end{equation}

    and apply the divergence theorem to the first resulting term:

    Ωdiv(σv)dv=Ωttˉvda\begin{equation} \int_\Omega \div(\Bsigma\Bv) \dv = \int_{\del\Omega_t} \bar{\Bt}\dtp\Bv \da \tag{17}\end{equation}

    Substituting the linear stress σ=C:ε=C:u\Bsigma=\IC:\Bvareps=\IC:\nabla\Bu, we obtain the following variational forms:

    a(u,v)=Ωv:C:udvb(v)=Ωργvdv+Ωttˉvda\begin{align} \htmlId{eq:linelastdiscretebilinear}{} a(\Bu,\Bv) &= \int_\Omega \nabla\Bv:\IC:\nabla\Bu \dv \tag{18}\\ \htmlId{eq:linelastdiscretelinear}{} b(\Bv) &= \int_\Omega \rho \Bgamma\dtp\Bv \dv + \int_{\del\Omega_t} \bar{\Bt}\dtp\Bv \da \tag{19}\end{align}

    We have the following discretizations of the unknown function and test function

    uh=J=1nnuJNJandvh=I=1nnvINI.\begin{equation} \Bu_h = \suml{J=1}{\nnode} \Bu^J N^J \eqand \Bv_h = \suml{I=1}{\nnode} \Bv^I N^I. \tag{20}\end{equation}

    With the given discretizations, the matrix corresponding to (18) can be calculated as

    AijI ⁣J=a(ejNJ,eiNI)=Ω(eiNI):C:(ejNJ)dv=Ω(eiNI):C:(ejNJ)dv=ΩNIxkCikjlNJxldv,\begin{equation} \begin{aligned} A^{I\!J}_{ij} = a(\Be_j N^J, \Be_i N^I) &= \int_\Omega \nabla(\Be_iN^I) : \IC : \nabla(\Be_jN^J) \dv \\ &= \int_\Omega (\Be_i\dyd \nabla N^I) : \IC : (\Be_j \dyd \nabla N^J) \dv \\ &= \int_\Omega \partd{N^I}{x_k} \, C_{ikjl} \, \partd{N^J}{x_l} \dv, \end{aligned} \tag{21}\end{equation}

    and finally obtain

    AijI ⁣J=ΩBkICikjlBlJdv.\begin{equation} \boxed{ A^{I\!J}_{ij} = \int_\Omega B^I_k \, C_{ikjl} \, B^J_l \dv \,. } \tag{22}\end{equation}

    The vector corresponding to (19) is calculated as

    biI=b(eiNI)=Ωttˉ(eiNI)da+Ωργ(eiNI)dv\begin{equation} b^{I}_{i} = b(\Be_i N^I) = \int_{\del\Omega_t} \bar{\Bt}\dtp(\Be_iN^I) \da + \int_\Omega\rho\Bgamma\dtp(\Be_iN^I) \dv \tag{23}\end{equation}

    which yields

    biI=ΩttˉiNIda+ΩργiNIdv\begin{equation} \boxed{ b^{I}_{i} = \int_{\del\Omega_t} \bar{t}_i N^I \da + \int_\Omega \rho \gamma_i N^I \dv } \tag{24}\end{equation}
  2. Portrait of Onur Solmaz
    Onur Solmaz · Post · /2017/11/20

    Reshaping Higher Order Linear Systems

    $ \newcommand{\BAhat}{\hat{\boldsymbol{A}}} \newcommand{\Buhat}{\hat{\boldsymbol{u}}} \newcommand{\Bbhat}{\hat{\boldsymbol{b}}} $

    In vectorial problems, we end up with linear systems of higher order, such as

    k=1Nl=1MAijklukl=bij\begin{equation} \suml{k=1}{N} \suml{l=1}{M} A_{ijkl} \, u_{kl} = b_{ij} \htmlId{eq:1}{} \tag{1}\end{equation}

    for i=1,,Ni=1,\dots,N and j=1,,Mj=1,\dots,M.

    These systems cannot be solved readily with existing software. In order to be able to solve them with existing software, we need to reshape them by defining a matrix of matrices A^\BAhat and vector of vectors u^\Buhat and b^\Bbhat:

    [A1111A111MA11N1A11NMA1M11A1M1MA1MN1A1MNMAN111AN11MAN1N1AN1NMANM11ANM1MANMN1ANMNM]A^[u11u1MuN1uNM]u^=[b11b1MbN1bNM]b^\begin{equation} \underbrace{ \left[ \begin{array}{ccc|c|ccc} A_{1111} & \cdots & A_{111M} & & A_{11N1} & \cdots & A_{11NM} \\ \vdots & \ddots & \vdots & \cdots & \vdots & \ddots & \vdots \\ A_{1M11} & \cdots & A_{1M1M} & & A_{1MN1} & \cdots & A_{1MNM} \\[1ex] \hline & \vdots & &\ddots & & \vdots & \\ \hline &&&&&& \\[-1.5ex] A_{N111} & \cdots & A_{N11M} & & A_{N1N1} & \cdots & A_{N1NM} \\ \vdots & \ddots & \vdots & \cdots & \vdots & \ddots & \vdots \\ A_{NM11} & \cdots & A_{NM1M} & & A_{NMN1} & \cdots & A_{NMNM} \end{array} \right] }_{\BAhat} \underbrace{ \left[ \begin{array}{c} u_{11}\\ \vdots \\ u_{1M} \\[1ex] \hline \vdots\\ \hline \\[-1.5ex] u_{N1} \\ \vdots \\ u_{NM} \end{array} \right] }_{\Buhat} = \underbrace{ \left[ \begin{array}{c} b_{11}\\ \vdots \\ b_{1M} \\[1ex] \hline \vdots\\ \hline \\[-1.5ex] b_{N1} \\ \vdots \\ b_{NM} \end{array} \right]}_{\Bbhat} \tag{2}\end{equation}

    This allows us to express the linear system as

    A^u^=b^\begin{equation} \BAhat \Buhat = \Bbhat \htmlId{eq:3}{} \tag{3}\end{equation}

    Here, we reshape the system by defining a map idi_d that maps original indices to the reshaped indices

    id:={[1,N]×[1,M][1,NM](i,j)M(i1)+j\begin{equation} i_d := \left\{ \begin{array}{rl} [1,N]\times[1,M] & \to [1,NM]\\[1ex] (i,j) & \mapsto M(i-1)+j\\ \end{array} \right. \tag{4}\end{equation}

    where we used 1-based indexing of the arrays. We set

    α:=id(i,j)=M(i1)+jβ:=id(k,l)=M(k1)+l\begin{equation} \boxed{ \begin{alignedat}{3} \alpha &:= i_d(i,j) &&= M(i-1) + j \\ \beta &:= i_d(k,l) &&= M(k-1) + l \\ \end{alignedat} } \tag{5}\end{equation}

    and write

    A^αβ=Aijkl,u^β=uklandb^α=bij\begin{equation} \hat{A}_{\alpha\beta} = A_{ijkl} \quad,\quad \hat{u}_{\beta} = u_{kl} \eqand \hat{b}_{\alpha} = b_{ij} \tag{6}\end{equation}

    For reference, the inverse of the index mapping reads

    id1:={[1,NM][1,N]×[1,M]α(1+(αmod(α,M))/M  ,  mod(α,M))\begin{equation} i_d^{-1} := \left\{ \begin{array}{rl} [1,NM] & \to [1,N]\times[1,M] \\[1ex] \alpha & \mapsto (1+(\alpha-\modop(\alpha,M))/M\;,\; \modop(\alpha,M)) \end{array} \right. \tag{7}\end{equation}

    Thus, we have for our reshaped indices,

    j=mod(α,M)i=1+(αj)/Mandl=mod(β,M)k=1+(βl)/M\begin{equation} \begin{aligned} j &= \modop(\alpha,M) \\ i &= 1+(\alpha-j)/M \end{aligned} \quad\eqand\quad \begin{aligned} l &= \modop(\beta,M) \\ k &= 1+(\beta-l)/M \end{aligned} \tag{8}\end{equation}

    Expressed as a regular linear system (3), the higher-order system (1) can be solved with a linear solver such as LAPACK.

  3. Portrait of Onur Solmaz
    Onur Solmaz · Post · /2017/11/14

    Linear Finite Elements

    Beginning with this post, I’ll be publishing about the basics of finite element formulations, from personal notes that accumulated over the years. This one is about linear and scalar problems which came to be the “Hello World” for FE. Details regarding spaces and discretization are omitted for the sake of brevity. For those who want to delve into theory, I recommend “The Finite Element Method: Theory, Implementation, and Applications” by Larson and Bengzon.

    The weak formulation of a canonical linear problem reads

    Find uVu\in V such that

    a(u,v)=b(v)\begin{equation} a(u, v) = b(v) \htmlId{eq:femlinear1}{} \tag{1}\end{equation}

    for all vVv \in V where a(,)a(\cdot, \cdot) is a bilinear form and b()b(\cdot) is a linear form.

    We define the discretization of uu as

    uh:=J=1nnuJNJ,uhVhwhereVhV\begin{equation} u_h := \suml{J=1}{\nnode} u^J N^J ,\quad u_h \in V_h \quad\text{where}\quad V_h\subset V \tag{2}\end{equation}

    The discretization uhu_h is a linear combination of basis functions NJN^J and corresponding scalars uJu^J, J=1,,nnJ=1,\dots,\nnode so that VhV_h is a subset of VV. The discretization of (1) then reads

    a(uh,vh)=b(vh)vhVh.\begin{equation} a(u_h, v_h) = b(v_h) \quad \forall v_h \in V_h . \tag{3}\end{equation}

    We then have

    a(J=1nnuJNJ,I=1nnvINI)=b(I=1nnvINI)\begin{equation} a\rbr{\suml{J=1}{\nnode} u^J N^J, \suml{I=1}{\nnode} v^I N^I} = b\rbr{\suml{I=1}{\nnode} v^I N^I} \tag{4}\end{equation}

    Using the linearity properties,

    a(αu,βv)=αβa(u,v)andb(αv)=αb(v)\begin{equation} a(\alpha u, \beta v) = \alpha\beta\, a(u,v) \eqand b(\alpha v) = \alpha b(v) \tag{5}\end{equation}

    we obtain

    I=1nnJ=1nnuJvIa(NJ,NI)=I=1nnvIb(NI).\begin{equation} \suml{I=1}{\nnode} \suml{J=1}{\nnode} u^J v^I a(N^J, N^I) = \suml{I=1}{\nnode} v^I b(N^I) . \htmlId{eq:femlinear2}{} \tag{6}\end{equation}

    For arbitrary test function values vIv^I, we can express (6) as a system of nn\nnode equations

    J=1nnuJa(NJ,NI)=b(NI)\begin{equation} \suml{J=1}{\nnode} u^J a(N^J, N^I) = b(N^I) \htmlId{eq:femlinear3}{} \tag{7}\end{equation}

    for I=1,2,,nnI = 1,2,\dots,\nnode. If we expand the summations as

    a(N1,N1)u1+a(N2,N1)u2++a(Nnn,N1)unn=b(N1)a(N1,N2)u1+a(N2,N2)u2++a(Nnn,N2)unn=b(N2)      a(N1,Nnn)u1+a(N2,Nnn)u2++a(Nnn,Nnn)unn=b(Nnn)\begin{alignat*}{6} & a(N^1, N^1) u^1 &&+ a(N^2, N^1) u^2 &&+ \cdots &&+ a(N^{\nnode}, N^1) u^{\nnode} &&\quad=\quad b(N^1) \\ & a(N^1, N^2) u^1 &&+ a(N^2, N^2) u^2 &&+ \cdots &&+ a(N^{\nnode}, N^2) u^{\nnode} &&\quad=\quad b(N^2) \\ & \qquad\vdots && \qquad\quad\;\vdots && \quad\;\;\vdots && \qquad\qquad\vdots && \qquad\qquad\vdots \\ & a(N^1, N^{\nnode}) u^1 &&+ a(N^2, N^{\nnode}) u^2 &&+ \cdots &&+ a(N^{\nnode}, N^{\nnode}) u^{\nnode} &&\quad=\quad b(N^{\nnode}) \end{alignat*}

    we can see that the terms with aa constitute a matrix A\BA and the terms with bb constitute a vector b\Bb, allowing us to write

    Au=b\begin{equation} \BA\Bu = \Bb \htmlId{eq:discrete9}{} \tag{8}\end{equation}

    where we chose to express the unknown coefficients uIu^I as a vector u=[u1,u2,,unn]T\Bu = [u^1,u^2,\dots,u^{\nnode}]\tra.

    \It can be seen that the components of the A\BA and b\Bb are defined as

    AI ⁣J=a(NJ,NI)andbI=b(NI),\begin{equation} \boxed{ \Aelid{I\!J}{} = a(N^J,N^I) \eqand b^I = b(N^I), } \tag{9}\end{equation}

    we can express the linear system as

    A11u1+A12u2++A1nnunn=b1A21u1+A22u2++A2nnunn=b2            Ann1u1+Ann2u2++Annnnunn=bnn\begin{alignat*}{6} & \Aelid{11}{} u^1 &&+ \Aelid{12}{} u^2 &&+ \cdots &&+ \Aelid{1\nnode}{} u^{\nnode} &&\quad=\quad b^1 \\ & \Aelid{21}{} u^1 &&+ \Aelid{22}{} u^2 &&+ \cdots &&+ \Aelid{2\nnode}{} u^{\nnode} &&\quad=\quad b^2 \\ & \quad\vdots && \qquad\;\vdots && \quad\;\;\vdots && \qquad\;\vdots && \qquad\quad\;\;\vdots \\ & \Aelid{\nnode 1}{} u^1 &&+ \Aelid{\nnode 2}{} u^2 &&+ \cdots &&+ \Aelid{\nnode\nnode}{} u^{\nnode} &&\quad=\quad b^{\nnode} \end{alignat*}

    Note that with the given definitions, (7) becomes

    J=1nnAI ⁣JuJ=bIforI=1,2,nn.\begin{equation} \boxed{ \suml{J=1}{\nnode} \Aelid{I\!J}{} \,u^J = b^I \quad\text{for}\quad I=1,2,\dots\nnode. } \htmlId{eq:discrete10}{} \tag{10}\end{equation}

    Example: Poisson’s Equation

    In the weak form of Poisson’s equation

    Δu=finΩu=0onΩ\begin{equation} \begin{alignedat}{4} - \Var u &= f \quad && \text{in} \quad && \Omega \\ u &= 0 \quad && \text{on} \quad && \del\Omega \end{alignedat} \tag{11}\end{equation}

    The weak formulation reads

    Find uVu\in V such that

    ΩΔ(u)vdv=Ωfvdv\begin{equation} - \int_\Omega \Delta(u) v \dv= \int_\Omega f v \dv \tag{12}\end{equation}

    for all vVv\in V where V=H01(Ω)V=H^1_0(\Omega).

    Applying integration by parts and divergence theorem on the left-hand side

    ΩΔ(u)vdv=Ω((u)v)dvΩuvdv=Ωv(un)dav=0 on ΩΩuvdv\begin{equation} \begin{aligned} \int_\Omega \Delta(u) v \dv &= \int_\Omega \nabla \dtp (\nabla (u) v) \dv - \int_\Omega \nabla u\dtp\nabla v \dv \\ &= \underbrace{\int_{\del\Omega} v (\nabla u\dtp\Bn) \da}_{v = 0 \text{ on } \del\Omega} - \int_\Omega \nabla u\dtp\nabla v \dv \\ \end{aligned} \tag{13}\end{equation}

    We have the following variational forms:

    a(u,v)=Ωuvdvb(v)=Ωfvdv\begin{equation} \begin{aligned} a(u,v) &= \int_{\Omega} \nabla u \dtp \nabla v \dv\\ b(v) &= \int_{\Omega} f \, v \dv\\ \end{aligned} \tag{14}\end{equation}

    Following (7), we can calculate the stiffness matrix A\BA as

    AI ⁣J=a(NJ,NI)=ΩNJNIdv=ΩBJBIdv\begin{equation} \begin{aligned} \Aelid{I\!J}{} = a(N^J, N^I) &= \int_{\Omega} \nabla N^J \dtp \nabla N^I \dv \\ &= \int_{\Omega} \BB^J \dtp \BB^I \dv \end{aligned} \tag{15}\end{equation}

    where we have defined the gradient of the basis functions as

    BI:=NI.\begin{equation} \BB^I := \nabla N^I\,. \tag{16}\end{equation}

    Similarly, we integrate the force term into a vector b\Bb as

    bI=ΩfNIdv\begin{equation} \begin{aligned} b^I &= \int_{\Omega} f N^I \dv \end{aligned} \tag{17}\end{equation}