me471s03_lec04

Upload: hasen-bebba

Post on 01-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 me471s03_lec04

    1/10

    Assembly Process

    We will now begin to look at the fundamentals of finite element programming. Since the ideas usedin 1D problems (ODEs) are applied to problems in 2D and 3D, we can continue to use our modelproblem as a basis of discussion.

    Review of Model Problem.   Suppose that   a, b , f   are given functions and consider the ordinarydifferential equation: Find  u  ∈  V  p  =  {w ∈  C 2([0, 1]) : w(0) = p}  such that

    −(au) + bu =  f,   0 < x <  1,   = d/dx,   (au + cu)|x=1  =  q 

    where  p, q, c  are given constants. A boundary condition like the one at  x  = 1 with  c  = 0 is calleda natural boundary condition for this ODE (for example, if  u   is the temperature in a wall and  athe wall conductivity  au(1) is the heat flux into the wall). I will present here an approach to theassembly process which starts with the global weak form representation of the solution. Anotherapproach is possible starting from a weak form developed for an individual element.

    Connectivity Matrix.   We assume that the basic interval [0,1] is divided into subintervals or elementsusing the grid (or mesh or node) points   x1   = 0  < x2  < . . . , xne+1   = 1. In our text, these nodesare generally stored in an array called   gcoord. We denote the left and right end points (nodes) of 

    a typical element I e  = [xe1, x

    e2]. The correspondence between element end points and grid points is

    simply xe1  =  xe, xe2  =  xe+1. This can get slightly more complex for elements with interior points so

    the correspondence is often stored in a  ne × k  connectivity matrix, say  C , where n  is the number of elements,  k  is the number of points per element, and  C (e, i) is the global node number the  ith nodeof element e.  For the current problem (using linear elements)

    Connectivity Matrix, 1D linear elements:   C  =

    1 22 3...

    ne   ne + 1

    Our text stores the connectivity matrix in an array generally called  nodes. In two and three dimen-sions the geometry is more complex. For example, if our domain is the rectangle [0, 1] × [0, 1], we

    may create a grid using lines parallel to the axes:   x1   = 0  < x2  < . . . , xn+1   = 1, y1   = 0  < y2   <. . . , ym+1  = 1 . The elements in this case can be either ne  =  n ·m rectangles or ne = 2n ·m triangles.After numbering these elements in a suitable way, the connectivity matrix will have  ne   rows andeither 4 or 3 columns.

    Review of Weak Formulation.   We start from the weak form of the equation. To find the weakform, we proceed in the usual fashion. We define the residual r  =  −(au) + bu − f , and note thatif  u  ∈  V  p  is a solution of the differential equation, and  w  ∈  V 0  =  {w ∈  C 

    2([a, b]) :  w(0) = 0}

       10

    rwdx = 0.

    Noting that (au)w = (auw) − auw,  we find

    A(u, w) =

       1

    0

    (auw + buw) dx =

       1

    0

    fwdx + (q  − cu)w|x=1  = (f, w) + (q  − cu(1))w(1).

    where the right hand boundary condition has been used.

    Numerical Solution.  The above expression holds for all test functions  w  and is equvalent to theoriginal equation for smooth functions. We generate a numerical approximation as follows: Let  V 0,n

    1

  • 8/9/2019 me471s03_lec04

    2/10

    be a linear subspace of  V 0  of finite dimension  n  we seek an approximate (or trial) solution  u  of theform û+v with û ∈  V  p a fixed function satisfying the boundary condition at  x = 0 and v is an elementof  V 0,n. At the same time, we take the test functions to lie in  V 0,n. (This is the Galerkin method. In1D, the dimension  n  of the approximating subspace will be  ne  in cases where the solution functionis specified at one point of the interval. If two natural boundary conditions are given,  n  =  ne + 1.)For the present, we restrict considerations to the case of continuous piecewise linear functions. This

    means that in each element   I e   = [xe1, x

    e2] (where for linear elements   x

    ej   =   xe+j−1, j   = 1, 2), the

    restriction of the trial function to  I e is given by  u(x)|I e  = ue1H 

    e1 (x) + u

    e2H 

    e2 (x), with

    H e1 (x) = (xe2 − x)/he,

    H e2(x) = (x − xe1)/he,

    where he =  xe2−x

    e1 is the length of the element, and the  H 

    es  are called element shape functions. From

    the global point of view, the trial function  u  can be expressed in the form  u = ne+1

    j=1   φjuj, where

    φj  are the tent functions,  φj  = H j1  on element j , φj  = H 

    j−12   on element j − 1, and φj  = 0 otherwise

    (u1  =  p  is given). Also from this point of view, the requirement that the weak form hold for all testfunctions vanishing at  x  = 0 is equivalent the requirement that it hold with  w  =  φi, i  = 2, . . . , n .To make the book keeping somewhat simpler, we will temporalily ignore the fact that w  =  φ1  is notallowable (and that also that  u1  =  p  is already known). Thus, we’ll allow all possible φi   for w. Inthis case, we can write the  ne + 1 linear equations obtained by substituting  w  =  φi  into the weak

    form as follows    10

    aΦ(Φ)T  + bΦΦT  dx

    U  =

       10

    f Φ dx + au(1)Φ(1),

    where Φ = [φ1, φ2, . . . , φne+1]T ,   U   = [u1, u2, . . . , une+1]

    T , and of course, Φ(1) = [0, 0, . . . , 0, 1]T ,and  au(1) =  −cune+1  +  q . This system of equations can be written in the form  KU   =  F   whereK   is called the   (global) stiffness or system matrix   and   F   is called the   (global) load vector.Remember that the system  K U  = F   cannot be inverted to give  U  = K −1F   since the first equation(corresponding to  w  =  φ1) must first be eliminated.

    Element Stiffness and Load Matricies.   Using the fact that 10

      g dx   = ne

    e=1

     I e

    g dx   for anyfunction g , we write the weak form as

    nee=1

     I e

    )T 

    + bΦΦT 

    dx

    U  =

    nee=1

     I e

    f Φ dx + au

    (1)Φ(1)

    When Φ is restricted to element  I e  (we write Φ|I e

    for this matrix) only two of the tent functions arenon-zero:   φe =  H e1   and φe+1  =  H 

    e2 , that is

    Φ|I e =

    0...0

    H e1H e2

    0...

    ...

    ← component  e← component  e  + 1

    ...

    We see that on a single element it is possible to focus on a 2 by 2 submatrix of the global  n × nmatricies.

    The contribution to the stiffness matrix from element  I e  is the 2 by 2 submatrix with components

    K ers  =

       xe2

    xe1

    (a(H er )(H es )

    + bH erH es ) dx, e = 1, . . . , ne, r,s = 1, 2,

    2

  • 8/9/2019 me471s03_lec04

    3/10

    where, remaining consistant with our definition of the global stiffenss matrix  K , we have ignoredthe fact that in element 1 the test function  w   must vanish at  x  =  x1  = 0 so that  w  =  H 

    11   is not

    admissable. The advantage of doing this is clear – all element stiffnesses are the same. At the endof the computation, we will account for our inclusion of this additional term. The contribution tothe load vector from element  e  is

    F e

    r   =   xe2xe1

    f H e

    r dx, e = 1, . . . , ne, r = 1, 2,

    where we have not included the additional contribution  au(1)δe,neδr, 2 so as to have the same formfor all the element load components. This omission will have to be corrected at the conclusion of computation prior to solving for the unknown trial function components  uj .   In matrix form

    K e =

     I e

    aH e(H T e  )

    + bH eH T e   dx

    , F e =

     I e

    fH e dx, e = 1, . . . , ne

    where H e = [H e1 , H 

    e2 ]

    T .

    The element stiffness and load matricies can be computed independently of one another. For exam-ple, if the coefficients  a, b and  f  are all constant, then

    K e

    =

      a

    he   1   −1−1 1

    +

     bhe

    6 2 1

    1 2

    , F e

    = f 

     he

    2 1

    1

    Let us recapitulate the two simplifying modifications we have introduced. First, we have ignored therestriction that the test function is required to vanish at x  =  x1  = 0 in element 1. The motivation forthis was to gain uniformity in the definition of the element stiffness matricies. Second, in element  newe have omitted the additional contribution to the load vector arising from the boundary condition –again to obtain uniformity in the definition of the load vector components. We can summarize thesetwo modifications by saying that we have ignored all boundary conditions. After constructing theglobal matricies K, F , we will introduce the necessary corrections to insure the boundary conditionsare properly imposed.

    Assembly Process and Matrix Inflation.  To compute the global stiffness matrix, we first have toexpand or inflate the 2 by 2 element matricies back size (ne + 1) × (ne + 1) by padding with zeros.

    Then we construct the overall stiffness or system matrix simply by adding together the inflatedmatricies – recall that the global matrices are just the sums of the (expanded) element local stiffnessmatrices. A similar procedure is used with the 2 by 1 load matrices which are inflated to ( ne + 1)×1vectors and summed to obtain the global load vector.

    The process of inflating the element matricies and adding to the global or system matricies is muchsimpler then it sounds. We simply loop through all the elements, determining for each element whatglobal nodes correspond to its local nodes. Determination of how the local stiffness and load vectorsof individual elements contribute to the global matrices can easily be made using the connectivitymatrix. We see, for example, that local node 1 of element 2 corresponds to global node 2 and localnode 2 corresponds to global node 3. This shows that  F 21  should be added to  F 2  and F 

    22   should be

    added to  F 3. Similarily,   K 211   should be added to  K 22,   K 

    212   should be added to  K 23,  K 

    222   to  K 33,

    and K 221  to K 32. Proceding in a similar fashion we find the global or system equations are (we take

    n =  ne + 1 in the equations below).

    K 111u1 + K 112u2   =   F 

    1

    K 121u1 + (K 122 + K 

    211)u2 + K 

    212u3   =   F 

    12  + F 

    21

    K 221u2 + (K 222 + K 

    311)u3 + K 

    312u4   =   F 

    22  + F 

    31

    ... =...

    K n21un−1 + K n22un   =   F 

    n2

    3

  • 8/9/2019 me471s03_lec04

    4/10

    or in matrix form

    K 111   K 112   0 0   · · ·   0

    K 121   (K 122 + K 

    211)   K 

    212   0   · · ·   0

    0   K 221   (K 222 + K 

    311)   K 

    312   · · ·   0

    ...   · · ·...

    0 0 0 0   · · ·   K n22

    u1u2u3...

    un

    =

    F 11F 12   + F 

    21

    F 22   + F 31

    ...

    F n2

    The the loop for assembly of the stiffness and load vectors is programmed in the m-file belowborrowed from our text. Before examining this file, lets look at the computation of the matrixindex  which is used in the assembly loop. Again this is provided in a file from our text. I haveprovided some additional comments intended to clarify the program.

    function [index]=feeldof1(iel,nnel,ndof)

    %----------------------------------------------------------

    % Purpose:

    % Compute system dofs associated with each element in one-

    % dimensional problem

    %

    % Synopsis:% [index]=feeldof1(iel,nnel,ndof)

    %

    % Variable Description:

    % index - system dof vector associated with element "iel"

    % iel - element number whose system dofs are to be determined

    % nnel - number of nodes per element (nnel=2 for linear elements)

    % ndof - number of dofs per node (ndof=1 for scalar problems)

    %-----------------------------------------------------------

    edof = nnel*ndof; % =2 for linear elements, scalar problems

    start = (iel-1)*(nnel-1)*ndof; % =iel-1 for linear elements, scalar problems

    for i=1:edof

    index(i)=start+i;

    end

    % for linear elements, scalar problems: index(1)=iel, index(2)=iel+1

    Now we’re ready for the assembly loop. Again I’ve added some extra comments to the program fromthe text.

    function [kk,ff]=feasmbl2(kk,ff,k,f,index)

    %----------------------------------------------------------

    % Purpose:% Assembly of element matrices into the system matrix &

    % Assembly of element vectors into the system vector

    %

    % Synopsis:

    % [kk,ff]=feasmbl2(kk,ff,k,f,index)

    %

    4

  • 8/9/2019 me471s03_lec04

    5/10

    % Variable Description:

    % kk - system matrix

    % ff - system vector

    % k - element matrix

    % f - element vector

    % index - d.o.f. vector associated with an element

    %-----------------------------------------------------------edof = length(index); % =2 for linear, scalar problems

    for i=1:edof

    ii=index(i); % index=[iel,iel+1] for element iel

    ff(ii)=ff(ii)+f(i); %[ff(iel);ff(iel+1)]=[f(1);f(2)]

    for j=1:edof

    jj=index(j);

    kk(ii,jj)=kk(ii,jj)+k(i,j);

    end

    end

    For the equation we’re considering, the index matrix for element  I e  would be   [e, e+1], so the looplimits would be  1:2.

    Imposition of Boundary Conditions.  The global stiffness and load matrices must now be modifiedso as to account for the boundary conditions which we have neglected up to this point. We will treatthe left and right hand conditions separately.

    Left Boundary. First, we note that the test function  w  =  φ1   should not have been used inK , and this means that the first row of   K   should be deleted. Second, note that   u1   =   p   isgiven so that all the terms  ki1u1  =  ki1 p, i  = 2, . . . , ne  should be moved from the left side of the equation  K U  = F  and added the the ith components of the load vector on the right side.These two modifications would change  K  to an ne × ne  matrix, but would entail modificationof all load components. The same end can be achieved in a much simpler way as follows

    1. Zero out row 1 of  K , then set k11  = 1.

    2. Redefine F 1  =  p.

    3. Now   K   remains (ne  + 1) ×  (ne  + 1), and the first equation becomes  u1   =   p, i.e., thisequation enforces the boundary condition at  x  = 0.

    Right Boundary. Now, it is the final component of the load vector which must be modified.We have omitted the terms  q  − cu(1) =  q  − cun

    efrom  F n

    e. The remedy is straight forward,

    and falls into two categories.

    c = 0 In this case we simply use the replacement  F ne→ F n

    e+ q .

    c = 0 Now we must not only use the modification above  F ne → F ne + q , but also must modifyknene as follows  knene → knene + c

    We are now ready to view some of the Text book files

    function ex351a(n)

    %----------------------------------------------------------------------------

    % EX3.5.1 -- Modified version of Text file

    5

  • 8/9/2019 me471s03_lec04

    6/10

    % to solve the ordinary differential equation given as

    % a u’’ + b u’ + c u = 1, 0 < x < 1

    % u(0) = 0 and u(1) = 0

    % using n linear elements

    %

    % Variable descriptions

    % k = element matrix% f = element vector

    % kk = system matrix

    % ff = system vector

    % index = a vector containing system dofs associated with each element

    % bcdof = a vector containing dofs associated with boundary conditions

    % bcval = a vector containing boundary condition values associated with

    % the dofs in ’bcdof’

    %----------------------------------------------------------------------------

    %------------------------------------

    % input data for control parameters

    %------------------------------------

    nel=n; % number of elements

    nnel=2; % number of nodes per element

    ndof=1; % number of dofs per node

    nnode=nel+1; % total number of nodes in system

    sdof=nnode*ndof; % total system dofs

    %-----------------------------------------

    % input data for nodal coordinate values

    %-----------------------------------------

    gcoord=linspace(0,1,nel+1);

    %-----------------------------------------------------

    % input data for nodal connectivity for each element

    %-----------------------------------------------------

    nodes=[[1:nel]’ [2:nel+1]’];

    %-----------------------------------------

    % input data for coefficients of the ODE

    %-----------------------------------------

    acoef=1; % coefficient ’a’ of the diff eqn

    bcoef=-3; % coefficient ’b’ of the diff eqn

    ccoef=2; % coefficient ’c’ of the diff eqn

    %-------------------------------------

    % input data for boundary conditions

    %-------------------------------------

    bcdof(1)=1; % first node is constrained

    6

  • 8/9/2019 me471s03_lec04

    7/10

    bcval(1)=0; % whose described value is 0

    bcdof(2)=nel+1; % last node is constrained

    bcval(2)=0; % whose described value is 0

    %-----------------------------------------

    % initialization of matrices and vectors

    %-----------------------------------------

    ff=zeros(sdof,1); % initialization of system force vector

    kk=zeros(sdof,sdof); % initialization of system matrix

    index=zeros(nnel*ndof,1); % initialization of index vector

    %-----------------------------------------------------------------

    % computation of element matrices and vectors and their assembly

    %-----------------------------------------------------------------

    for iel=1:nel % loop for the total number of elements

    nl=nodes(iel,1); nr=nodes(iel,2); % extract nodes for (iel)-th element

    xl=gcoord(nl); xr=gcoord(nr);% extract nodal coord values for the elementeleng=xr-xl; % element length

    index=feeldof1(iel,nnel,ndof);% extract system dofs associated with element

    k=feode2l(acoef,bcoef,ccoef,eleng); % compute element matrix

    f=fef1l(xl,xr); % compute element vector

    [kk,ff]=feasmbl2(kk,ff,k,f,index); % assemble element matrices and vectors

    end

    %-----------------------------

    % Global stiffness and load matrices have been assembled

    % now apply boundary conditions

    %-----------------------------

    [kk,ff]=feaplyc2(kk,ff,bcdof,bcval);

    %----------------------------

    % solve the matrix equation

    %----------------------------

    fsol=kk\ff;

    %---------------------

    % analytical solution

    %---------------------

    c1=0.5/exp(1);

    c2=-0.5*(1+1/exp(1));

    for i=1:nnode

    x=gcoord(i);

    esol(i)=c1*exp(2*x)+c2*exp(x)+1/2;

    7

  • 8/9/2019 me471s03_lec04

    8/10

    end

    %------------------------------------

    % print both exact and fem solutions

    %------------------------------------

    fprintf(’%10s %10s %10s %10s\n’,’x’, ’fem soln’,’exac soln’,’error’);fprintf(’%10.2f %10.6f %10.6f %10.6f\n’,[gcoord; fsol’; esol; abs(fsol’-esol)]);

    plot(gcoord,fsol’,’k’, gcoord, esol, ’ko’);

    %---------------------------------------------------------------

    function [index]=feeldof1(iel,nnel,ndof)

    %----------------------------------------------------------

    % Purpose:

    % Compute system dofs associated with each element in one-

    % dimensional problem

    %% Synopsis:

    % [index]=feeldof1(iel,nnel,ndof)

    %

    % Variable Description:

    % index - system dof vector associated with element "iel"

    % iel - element number whose system dofs are to be determined

    % nnel - number of nodes per element

    % ndof - number of dofs per node

    %-----------------------------------------------------------

    edof = nnel*ndof;

    start = (iel-1)*(nnel-1)*ndof;

    for i=1:edof

    index(i)=start+i;

    end

    function [k]=feode2l(acoef,bcoef,ccoef,eleng)

    %-------------------------------------------------------------------

    % Purpose:

    % element matrix for (a u’’ + b u’ + c u)

    % using linear element

    %

    % Synopsis:

    % [k]=feode2l(acoef,bcoef,ccoef,eleng)

    %

    % Variable Description:

    8

  • 8/9/2019 me471s03_lec04

    9/10

    % k - element matrix (size of 2x2)

    % acoef - coefficient of the second order derivative term

    % bcoef - coefficient of the first order derivative term

    % ccoef - coefficient of the zero-th order derivative term

    % eleng - element length

    %-------------------------------------------------------------------

    % element matrix

    a1=-(acoef/eleng); a2=bcoef/2; a3=ccoef*eleng/6;

    % k=a1[1 -1; -1 1]+a2[-1 1;-1 1]+a3[2 1;1 2]

    k=[ a1-a2+2*a3 -a1+a2+a3;...

    -a1-a2+a3 a1+a2+2*a3];

    function [f]=fef1l(xl,xr)

    %-------------------------------------------------------------------

    % Purpose:

    % element vector for f(x)=1% using linear element

    %

    % Synopsis:

    % [f]=fef1l(xl,xr)

    %

    % Variable Description:

    % f - element vector (size of 2x1)

    % xl - coordinate value of the left node

    % xr - coordinate value of the right node

    %-------------------------------------------------------------------

    % element vector

    eleng=xr-xl; % element length

    f=[ eleng/2; eleng/2];

    function [kk,ff]=feasmbl2(kk,ff,k,f,index)

    %----------------------------------------------------------

    % Purpose:

    % Assembly of element matrices into the system matrix &

    % Assembly of element vectors into the system vector

    %

    % Synopsis:

    % [kk,ff]=feasmbl2(kk,ff,k,f,index)

    %

    % Variable Description:

    % kk - system matrix

    % ff - system vector

    % k - element matrix

    % f - element vector

    9

  • 8/9/2019 me471s03_lec04

    10/10

    % index - d.o.f. vector associated with an element

    %-----------------------------------------------------------

    edof = length(index);

    for i=1:edof

    ii=index(i);ff(ii)=ff(ii)+f(i);

    for j=1:edof

    jj=index(j);

    kk(ii,jj)=kk(ii,jj)+k(i,j);

    end

    end

    function [kk,ff]=feaplyc2(kk,ff,bcdof,bcval)

    %----------------------------------------------------------

    % Purpose:

    % Apply constraints to matrix equation [kk]{x}={ff}%

    % Synopsis:

    % [kk,ff]=feaplybc(kk,ff,bcdof,bcval)

    %

    % Variable Description:

    % kk - system matrix before applying constraints

    % ff - system vector before applying constraints

    % bcdof - a vector containing constrained d.o.f

    % bcval - a vector containing contained value

    %

    % For example, for homogeneous BC at x=0, 1,

    % bcdof=[1, ne+1], bcval=[0,0]

    %-----------------------------------------------------------

    n=length(bcdof);

    sdof=size(kk);

    for i=1:n

    c=bcdof(i);

    for j=1:sdof

    kk(c,j)=0;

    end

    kk(c,c)=1;

    ff(c)=bcval(i);

    end

    10