me471s03_lec11a

Upload: hasen-bebba

Post on 13-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/26/2019 me471s03_lec11a

    1/14

    Diffusion Equation Example Using Isoparametric Elements

    LetD = {(x, y) : 0< x, y 0,

    u(x,y, 0) = 0, u(x, 0, t) = 0, u(x, 1, t)/y = x, 0x 1, t > 0 andu(0, y , t) = 0, u(1, y , t)/x =y, 0 y 1, t > 0. The weak form of this problem is

    D

    vu

    tdA+

    D

    v u dA=

    1

    0

    vu(1, y , t)

    x dy+

    1

    0

    vu(x, 1, t)

    y dx

    Using bilinear elements we takeu=n

    j=1uj(t)j(x, y),v = i(x, y), i= 1, . . . , n(We ignore bound-ary conditions for the moment, as usual, these will be imposed near the end of the computation.)This assumption leads to the element mass and stiffness matrices

    m=

    De

    He(He)T dA, k=

    De

    He

    x

    (He)T

    x +

    He

    y

    (He)T

    y

    dA

    where He = [He1 (x, y), . . . , H e4 (x, y)]

    T is the element shape function vector. The two contributionsto the element load vectors are of the form

    ye+1ye

    xe+1xe

    y dy=

    ye+1ye

    00

    He3

    (1, y)He

    4(1, y)

    ydy,

    xe+1xe

    He(x, 1)x dx=

    xe+1xe

    0He

    2(x, 1)

    He3

    (x, 1)0

    x dx

    Because the shape functions are bilinear, the Hek(1, y), Hej (x, 1) are linear functions of their argu-

    ments. In this particular problem the boundary contributions to the element load functions areindependent of time in the general case these contributions can depend on time. We can now builda Matlab file to solve this initial boundary value problem based on our Textbook files from Chapters5 and 6. (All files necessary to compile this example are included below with the exception of the

    utility files gridgen.m, gridplot.m.)

    function heat_eq(nx,ny)

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

    % Based on Textbook Example 6.6.2

    % to solve the two-dimensional Laplaces equation given as

    % u,t-u,xx - u,yy =0, 0 < x < 1, 0 < y < 1

    % u(x,0) = 0, u,y(x,1) = x

    % u(0,y) = 0, u,x(1,y) = y

    % u(x,y,0)=0

    % using isoparametric four-node quadrilateral elements

    %

    % Variable descriptions% k = element matrix

    % f = element vector

    % kk = system matrix

    % ff = system vector

    % gcoord = coordinate values of each node

    % nodes = nodal connectivity of each element

  • 7/26/2019 me471s03_lec11a

    2/14

    % 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

    % point2 - integration (or sampling) points

    % weight2 - weighting coefficients

    % nglx - number of integration points along x-axis% ngly - number of integration points along y-axis

    % xcoord - x coordinate values of nodes

    % ycoord - y coordinate values of nodes

    % jacob2 - jacobian matrix

    % shape - four-node quadrilateral shape functions

    % dhdr - derivatives of shape functions w.r.t. natural coord. r

    % dhds - derivatives of shape functions w.r.t. natural coord. s

    % dhdx - derivatives of shape functions w.r.t. physical coord. x

    % dhdy - derivatives of shape functions w.r.t. physical coord. y

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

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

    % input data for control parameters

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

    bl=[0,0]; ur=[1,1];

    nx1=nx+1; ny1=ny+1;

    nel=nx*ny; % number of elements

    nnel=4; % number of nodes per element

    ndof=1; % number of dofs per node

    nnode=nx1*ny1; % total number of nodes in system

    nglx=2; ngly=2; % use 2x2 integration rule

    sdof=nnode*ndof; % total system dofs

    edof=nnel*ndof; % dofs per element

    deltt=0.04; % time step size for transient analysis

    stime=0.0; % initial time

    ftime=4; % termination time

    ntime=fix((ftime-stime)/deltt); % number of time increment

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

    % input data for nodal coordinate values

    % gcoord(i,j) where i->node no. and j->x or y

    % nodes(i,j) where i-> element no. and j-> connected nodes

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

    [gcoord,nodes]=gridgen(bl,ur, nx, ny, 1);

    gridplot(nx, ny, gcoord, nodes, 1);

    input(press any key to continue);

    close all

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

    % input data for boundary conditions

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

    bcdof=[1:nx1,1+nx1:nx1:nx1*ny1-nx];

  • 7/26/2019 me471s03_lec11a

    3/14

    nbcdof=length(bcdof);

    bcval=zeros(1,nbcdof);

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

    % initialization of matrices and vectors

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

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

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

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

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

    % Note: The only contribution to ff in this problem is from the boundary fluxes

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

    % loop for computation and assembly of element matrices

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

    [point2,weight2]=feglqd2(nglx,ngly); % sampling points & weights

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

    for i=1:nnel

    nd(i)=nodes(iel,i); % extract connected node for (iel)-th element

    xcoord(i)=gcoord(nd(i),1); % extract x value of the node

    ycoord(i)=gcoord(nd(i),2); % extract y value of the node

    end

    k=zeros(edof,edof); % initialization of element matrix to zero

    m=zeros(edof,edof); % element mass matrix

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

    % numerical integration

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

    for intx=1:nglx

    x=point2(intx,1); % sampling point in x-axis

    wtx=weight2(intx,1); % weight in x-axis

    for inty=1:ngly

    y=point2(inty,2); % sampling point in y-axis

    wty=weight2(inty,2) ; % weight in y-axis

    [shape,dhdr,dhds]=feisoq4(x,y); % compute shape functions and

    % derivatives at sampling point

    jacob2=fejacob2(nnel,dhdr,dhds,xcoord,ycoord); % compute Jacobian

    detjacob=det(jacob2); % determinant of Jacobian

    invjacob=inv(jacob2); % inverse of Jacobian matrix

    [dhdx,dhdy]=federiv2(nnel,dhdr,dhds,invjacob); % derivatives w.r.t.

    % physical coordinate

  • 7/26/2019 me471s03_lec11a

    4/14

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

    % compute element matrix

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

    for i=1:edof

    for j=1:edof

    k(i,j)=k(i,j)+(dhdx(i)*dhdx(j)+dhdy(i)*dhdy(j))*wtx*wty*detjacob;m(i,j)=m(i,j)+shape(i)*shape(j)*wtx*wty*detjacob;

    end

    end

    end % y loop

    end % x loop

    index=feeldof(nd,nnel,ndof);% extract system dofs associated with element

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

    % assemble element mass and stiffness matrices

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

    kk=feasmbl1(kk,k,index);

    mm=feasmbl1(mm,m,index);

    end % end of element loops

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

    % Now compute the flux contributions to the load vector.

    % These are independent of time for this problem -- so do not

    % have to be computed in the time integration loop.

    % The loops below could really be moved into the main assembly loop

    % and then the textbook file feasmbl2() could be used to

    % assemble kk and ff at the same time.

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

    [point1, weight1]=feglqd1(ngly); % get integration points for 1D boundary integrals

    for iel=nx:nx:nx*ny % loop over elements with boundary on x=1, 0

  • 7/26/2019 me471s03_lec11a

    5/14

    % pt (y=sum_j H_j(1,s)y_j)

    jacob1=fejacob1(2,dhdr1,ycoord(2:3));

    for i=2:3

    f(i)=f(i)+shape(i)*bfnc*wty*jacob1; % only shape(2),shape(3) nonzero

    end

    end

    for i=2:3 ff(nd(i))=ff(nd(i))+f(i); end % assemble load vectorend

    [point1, weight1]=feglqd1(nglx);

    for iel=nx*ny-nx+1:nx*ny % loop over elements with boundary on y=1,0

  • 7/26/2019 me471s03_lec11a

    6/14

    fsol=zeros(sdof,1); %initial value of solution

    sol(1,1)=fsol((nnode+1)/2); % store time history solution for middle node (assumes nnode is odd

    kn=mm+deltt*kk; % compute effective system matrix (time invariant)

    for it=1:ntime % start loop for time integration

    fn=deltt*ff+mm*fsol; % compute effective column vector

    [kn,fn]=feaplyc2(kn,fn,bcdof,bcval); % apply essential boundary conditions

    fsol=kn\fn; % solve the matrix equation

    sol(1,it+1)=fsol((nnode+1)/2); %

    end

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

    % analytical solution at node middle node

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

    xypt=gcoord((nnode+1)/2,:);

    for it=1:ntime+1

    tt=(it-1)*deltt;

    esol(it)=xypt(1)*xypt(2)-trans(xypt(1),tt,10)*trans(xypt(2),tt,10);

    end

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

    % plot the solution at nodes middle node

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

    time=0:deltt:ntime*deltt;

    plot(time,sol(1,:), time, esol, o);

    xlabel(Time)

    ylabel(Solution at nodes)

    %===================================================================

    % end of main routine

    %===================================================================

    function [point1,weight1]=feglqd1(ngl)

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

    % Purpose:

    % determine the integration points and weighting coefficients

    % of Gauss-Legendre quadrature for one-dimensional integration

    %

  • 7/26/2019 me471s03_lec11a

    7/14

    % Synopsis:

    % [point1,weight1]=feglqd1(ngl)

    %

    % Variable Description:

    % ngl - number of integration points

    % point1 - vector containing integration points

    % weight1 - vector containing weighting coefficients%-------------------------------------------------------------------

    % initialization

    point1=zeros(ngl,1);

    weight1=zeros(ngl,1);

    % find corresponding integration points and weights

    if ngl==1 % 1-point quadrature rule

    point1(1)=0.0;

    weight1(1)=2.0;

    elseif ngl==2 % 2-point quadrature rule

    point1(1)=-0.577350269189626;

    point1(2)=-point1(1);

    weight1(1)=1.0;

    weight1(2)=weight1(1);

    elseif ngl==3 % 3-point quadrature rule

    point1(1)=-0.774596669241483;

    point1(2)=0.0;

    point1(3)=-point1(1);

    weight1(1)=0.555555555555556;

    weight1(2)=0.888888888888889;

    weight1(3)=weight1(1);

    elseif ngl==4 % 4-point quadrature rule

    point1(1)=-0.861136311594053;

    point1(2)=-0.339981043584856;

    point1(3)=-point1(2);

    point1(4)=-point1(1);

    weight1(1)=0.347854845137454;

    weight1(2)=0.652145154862546;

    weight1(3)=weight1(2);

    weight1(4)=weight1(1);

    else % 5-point quadrature rule

    point1(1)=-0.906179845938664;

    point1(2)=-0.538469310105683;

    point1(3)=0.0;

    point1(4)=-point1(2);

    point1(5)=-point1(1);

    weight1(1)=0.236926885056189;

  • 7/26/2019 me471s03_lec11a

    8/14

    weight1(2)=0.478628670499366;

    weight1(3)=0.568888888888889;

    weight1(4)=weight1(2);

    weight1(5)=weight1(1);

    end

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

    function [point2,weight2]=feglqd2(nglx,ngly)

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

    % Purpose:

    % determine the integration points and weighting coefficients

    % of Gauss-Legendre quadrature for two-dimensional integration

    %

    % Synopsis:

    % [point2,weight2]=feglqd2(nglx,ngly)

    %

    % Variable Description:% nglx - number of integration points in the x-axis

    % ngly - number of integration points in the y-axis

    % point2 - vector containing integration points

    % weight2 - vector containing weighting coefficients

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

    % determine the largest one between nglx and ngly

    if nglx > ngly

    ngl=nglx;

    else

    ngl=ngly;

    end

    % initialization

    point2=zeros(ngl,2);

    weight2=zeros(ngl,2);

    % find corresponding integration points and weights

    [pointx,weightx]=feglqd1(nglx); % quadrature rule for x-axis

    [pointy,weighty]=feglqd1(ngly); % quadrature rule for y-axis

    % quadrature for two-dimension

    for intx=1:nglx % quadrature in x-axis

    point2(intx,1)=pointx(intx);

    weight2(intx,1)=weightx(intx);

    end

  • 7/26/2019 me471s03_lec11a

    9/14

    for inty=1:ngly % quadrature in y-axis

    point2(inty,2)=pointy(inty);

    weight2(inty,2)=weighty(inty);

    end

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

    function [shapeq4,dhdrq4,dhdsq4]=feisoq4(rvalue,svalue)

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

    % Purpose:

    % compute isoparametric four-node quadilateral shape functions

    % and their derivatves at the selected (integration) point

    % in terms of the natural coordinate

    %

    % Synopsis:

    % [shapeq4,dhdrq4,dhdsq4]=feisoq4(rvalue,svalue)

    %

    % Variable Description:% shapeq4 - shape functions for four-node element

    % dhdrq4 - derivatives of the shape functions w.r.t. r

    % dhdsq4 - derivatives of the shape functions w.r.t. s

    % rvalue - r coordinate value of the selected point

    % svalue - s coordinate value of the selected point

    %

    % Notes:

    % 1st node at (-1,-1), 2nd node at (1,-1)

    % 3rd node at (1,1), 4th node at (-1,1)

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

    % shape functions

    shapeq4(1)=0.25*(1-rvalue)*(1-svalue);

    shapeq4(2)=0.25*(1+rvalue)*(1-svalue);

    shapeq4(3)=0.25*(1+rvalue)*(1+svalue);

    shapeq4(4)=0.25*(1-rvalue)*(1+svalue);

    % derivatives

    dhdrq4(1)=-0.25*(1-svalue);

    dhdrq4(2)=0.25*(1-svalue);

    dhdrq4(3)=0.25*(1+svalue);

    dhdrq4(4)=-0.25*(1+svalue);

    dhdsq4(1)=-0.25*(1-rvalue);

    dhdsq4(2)=-0.25*(1+rvalue);

    dhdsq4(3)=0.25*(1+rvalue);

    dhdsq4(4)=0.25*(1-rvalue);

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

  • 7/26/2019 me471s03_lec11a

    10/14

    function [dhdx,dhdy]=federiv2(nnel,dhdr,dhds,invjacob)

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

    % Purpose:

    % determine derivatives of 2-D isoparametric shape functions with% respect to physical coordinate system

    %

    % Synopsis:

    % [dhdx,dhdy]=federiv2(nnel,dhdr,dhds,invjacob)

    %

    % Variable Description:

    % dhdx - derivative of shape function w.r.t. physical coordinate x

    % dhdy - derivative of shape function w.r.t. physical coordinate y

    % nnel - number of nodes per element

    % dhdr - derivative of shape functions w.r.t. natural coordinate r

    % dhds - derivative of shape functions w.r.t. natural coordinate s

    % invjacob - inverse of 2-D Jacobian matrix

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

    for i=1:nnel

    dhdx(i)=invjacob(1,1)*dhdr(i)+invjacob(1,2)*dhds(i);

    dhdy(i)=invjacob(2,1)*dhdr(i)+invjacob(2,2)*dhds(i);

    end

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

    function [jacob2]=fejacob2(nnel,dhdr,dhds,xcoord,ycoord)

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

    % Purpose:

    % determine the Jacobian for two-dimensional mapping

    %

    % Synopsis:

    % [jacob2]=fejacob2(nnel,dhdr,dhds,xcoord,ycoord)

    %

    % Variable Description:

    % jacob2 - Jacobian for one-dimension

    % nnel - number of nodes per element

    % dhdr - derivative of shape functions w.r.t. natural coordinate r

    % dhds - derivative of shape functions w.r.t. natural coordinate s

    % xcoord - x axis coordinate values of nodes

    % ycoord - y axis coordinate values of nodes

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

    jacob2=zeros(2,2);

    for i=1:nnel

    jacob2(1,1)=jacob2(1,1)+dhdr(i)*xcoord(i);

  • 7/26/2019 me471s03_lec11a

    11/14

    jacob2(1,2)=jacob2(1,2)+dhdr(i)*ycoord(i);

    jacob2(2,1)=jacob2(2,1)+dhds(i)*xcoord(i);

    jacob2(2,2)=jacob2(2,2)+dhds(i)*ycoord(i);

    end

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

    function [kk]=feasmbl1(kk,k,index)

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

    % Purpose:

    % Assembly of element matrices into the system matrix

    %

    % Synopsis:

    % [kk]=feasmbl1(kk,k,index)

    %

    % Variable Description:

    % kk - system matrix

    % k - element matri

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

    edof = length(index);

    for i=1:edof

    ii=index(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 containging constrained d.o.f

    % bcval - a vector containing contained value

    %

    % For example, there are constraints at d.o.f=2 and 10

  • 7/26/2019 me471s03_lec11a

    12/14

    % and their constrained values are 0.0 and 2.5,

    % respectively. Then, bcdof(1)=2 and bcdof(2)=10; and

    % bcval(1)=1.0 and bcval(2)=2.5.

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

    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

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

    function [index]=feeldof(nd,nnel,ndof)

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

    % Purpose:

    % Compute system dofs associated with each element

    %

    % Synopsis:

    % [index]=feeldof(nd,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;

    k=0;

    for i=1:nnel

    start = (nd(i)-1)*ndof;

    for j=1:ndof

    k=k+1;

    index(k)=start+j;

    end

    end

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

    function [jacob1]=fejacob1(nnel,dhdr,xcoord)

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

  • 7/26/2019 me471s03_lec11a

    13/14

    % Purpose:

    % determine the Jacobian for one-dimensional mapping

    %

    % Synopsis:

    % [jacob1]=fejacob1(nnel,dhdr,xcoord)

    %

    % Variable Description:% jacob1 - Jacobian for one-dimension

    % nnel - number of nodes per element

    % dhdr - derivative of shape functions w.r.t. natural coordinate

    % xcoord - x axis coordinate values of nodes

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

    jacob1=0.0;

    for i=1:nnel

    jacob1=jacob1+dhdr(i)*xcoord(i);

    end

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

    function [shape,dhdr]=feisol2(rvalue)

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

    %Purpose

    % Compute isoparametric 2-node shape functions

    % and their derivatives at the selected

    % point in terms of the natural coordinate.

    %

    %Synopsis:

    % [shape,dhdr]=kwisol2(rvalue)

    %

    %Variable Description:

    % shape - shape functions for the linear element

    % dhdr - derivatives of shape functions

    % rvalue - r coordinate valute of the selected point

    %

    %Notes:

    % 1st node at rvalue=-1

    % 2nd node at rvalue=1

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

    shape(1)=0.5*(1.0-rvalue); % first shape function

    shape(2)=0.5*(1.0+rvalue); % second shape function

    dhdr(1)=-0.5; % derivative of the first shape function

    dhdr(2)=0.5; % derivative of the second shape function

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

    function t=trans(z,t,n)

    sgn=-1;

    sum=0;

    for k=1:n

  • 7/26/2019 me471s03_lec11a

    14/14

    lambda=(2*k-1)*pi/2; lamsq=lambda*lambda;

    sine=sin(lambda*z);

    expfnc=exp(-lamsq*t);

    sum=sum+sgn*sine*expfnc/lamsq;

    sgn=-sgn;

    end;

    t=2*sum;

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