Anda di halaman 1dari 3

Printed by Ales Janka

Oct 06, 10 8:33 program1.m Page 1/2 Oct 06, 10 8:33 program1.m Page 2/2
% defines a scalar field U over a mesh (XY,Elm) quiver(EXY(:,1),EXY(:,2),scale*EGradU0(:,1),scale*EGradU0(:,2),0,b);
% calculates and visualizes the gradient of U % superpose the finite element gradient, in red color:
% hold on
quiver(EXY(:,1),EXY(:,2),scale*EGradU(:,1),scale*EGradU(:,2),0,r);
%*** get the mesh: circle view(2); % < 2D view (from above)
% [XY,Elm,PXY] = getcircle4(15,40,2.1); axis equal;
% nnod = size(XY,1);
% nelm = size(Elm,1); %*** visualize dU/dx (ie. EGrad(:,1)) in Figure 4:
figure(4); clf
%*** get the 2D mesh: square domain (1,1) x (1,1): ts4 = trisurf(Elm,XY(:,1),XY(:,2),0.*XY(:,1),EGradU(:,1));
[XY,Elm] = getdrapeau4(40,40,0); view(2);
nnod = size(XY,1); axis equal
nelm = size(Elm,1);
%*** define the scalar field U(x,y) = sin(pi/2*x)*cos(pi*y):
U = sin(pi/2*XY(:,1)).*cos(pi*XY(:,2));
%*** calculate the gradient on finite elements: constant on triangle:
EGradU = getgrad2D(XY,Elm,U);
%*** barycenters of triangles:
EXY = (XY(Elm(:,1),:)+XY(Elm(:,2),:)+XY(Elm(:,3),:))/3;
%*** to compare: exact gradient (on nodes):
GradU0 = [pi/2*cos(pi/2*XY(:,1)).*cos(pi*XY(:,2)),
pi*sin(pi/2*XY(:,1)).*sin(pi*XY(:,2))];
%*** to compare: exact gradient (on elements):
EGradU0 = [pi/2*cos(pi/2*EXY(:,1)).*cos(pi*EXY(:,2)),
pi*sin(pi/2*EXY(:,1)).*sin(pi*EXY(:,2))];

% average GradU onto element barycenters:


% EGradU0 = (GradU0(Elm(:,1),:)+GradU0(Elm(:,2),:)+GradU0(Elm(:,3),:))/3;

%*** identifies borders of the domain, nodal connectivity matrix G:


[G,Border,bfac] = incidence2(Elm);

%*** visualize the mesh in Figure 1:


figure(1); clf
gplot(G,XY); % < visualization of graphs
axis equal
%*** visualize the scalar field in Figure 2:
figure(2); clf
ts2=trisurf(Elm,XY(:,1),XY(:,2),U,U); % < visualize triangulated surface
view(3); % < predefined 3D view
cb=colorbar; % < add color scale bar
axis equal; % < sets axes of equal length equal
shading interp; % < interpolated colors
set(ts2,EdgeColor,k); % < define color of edges
% set(ts2,EdgeAlpha,0.1); % < edge transparency
% set(ts2,FaceAlpha,0.1); % < face transparency
xl2 = xlabel(X);
yl2 = ylabel(Y);
zl2 = zlabel(U(x,y));
ti2 = title(scalar field U(x,y));
%*** visualize the exact and the finite element gradient vectorfield, Fig 3:
figure(3); clf
% exact gradient averaged on element barycenters:
scale = 0.02;
Wednesday October 06, 2010 program1.m 1/3
Printed by Ales Janka

Sep 19, 10 11:32 getgrad2D.m Page 1/1


function EGradU = getgrad2D(XY,Elm,U);
% function EGradU = getgrad2D(XY,Elm,U);
%
% returns the gradient of a scalar field U(x,y), ie. calculates
% EGradU = grad(U) on 2D linear finite elements,
%*** parameters:
% XY(nnod,2) in nodal coordinates
% Elm(nelm,3) in element connectivity
% U(nnod,1) in scalar field
% EGradU(nelm,2) out gradient of U on every element/triangle
%
% mail: ales.janka@unifr.ch
% http://perso.unifr.ch/ales.janka
%*** no. of nodes and elements:
nnod = size(XY,1);
nelm = size(Elm,1);

%*** allocate the matrix for the resulting gradients:


EGradU = zeros(nelm,2);
t0 = clock;
%*** start loop over all elements:
for el = 1:nelm
if (el==ceil(nelm/100))
t = ceil(100*etime(clock,t0));
fprintf(*** getgrad2D: time to finish: %d min %d s\n,
floor(t/60),rem(t,60));
end;
% select nodal coords of only the current triangle Elm(el,:):
EXY = XY(Elm(el,:),:);
% select nodal values of only the current element Elm(el):
EU = U(Elm(el,:),:);
% dofs: [ui uj uk]: get gradient of U on the current element Elm(el,:):
EGradU(el,:) = elmgrad2D(EXY,EU);
end;
return;

%
% subfunction: elmgrad2D
%*** parameters
% XY(3,2) in nodal coords of 1 triangle, XY(i,:) = [xi yi]
% EU(3,1) in nodal value (scalar field, U(i) = ui)
% EGrad(1,2) out gradient of U on the element, [dU/dx, dU/dy]
%
function EGrad = elmgrad2D(XY,U)
% calculate the basis functions on the 3 nodes:
A = [ones(3,1), XY];
A = inv(A);
% Phi_i(x) = ai + bi*x + ci*y:
ai = A(1,1); aj = A(1,2); ak = A(1,3);
bi = A(2,1); bj = A(2,2); bk = A(2,3);
ci = A(3,1); cj = A(3,2); ck = A(3,3);
% gradient "operator":
Grad = [ bi bj bk % < d/dx
ci cj ck]; % < d/dy
% apply the gradient "operator" on nodal values U:
EGrad = (Grad*U);
return;

Wednesday October 06, 2010 getgrad2D.m 2/3


Printed by Ales Janka

Oct 06, 10 8:29 program2.m Page 1/2 Oct 06, 10 8:29 program2.m Page 2/2
% A rectangular beam (aspect,aspect) x (1,1), stored as a mesh (XY,Elm), figure(3); clf
% is bent into a circular arc of radius (radius), stored in (UXY,Elm). scale = 0.2;
% Based on the resulting displacement vector field U, quiver(XY(:,1),XY(:,2),scale*U(:,1),scale*U(:,2),0,b);
% assemble and visualize Green, Cauchy and/or Almansi strain tensors view(2);
% on every finite element triangle axis equal;
% title(vector field of displacements);
%*** get the mesh: circle
% [XY,Elm,PXY] = getcircle4(15,40,2.1);
% nnod = size(XY,1);
% nelm = size(Elm,1);

%*** get the 2D mesh: rectangular domain (aspect,aspect) x (1,1):


aspect = 5;
refine = 1;
[XY,Elm] = getdrapeau4(aspect*refine*10+1,refine*10+1,0);
XY(:,1) = XY(:,1)*aspect;
nnod = size(XY,1);
nelm = size(Elm,1);
%*** barycenters of triangles:
EXY = (XY(Elm(:,1),:)+XY(Elm(:,2),:)+XY(Elm(:,3),:))/3;
%*** bend the beam along a circle segment of radius 10:
radius = 10;
A = XY(:,1)/radius;
UXY = ((radius+XY(:,2))*ones(1,2)).*[sin(A) cos(A)];
UXY(:,2) = UXY(:,2)radius; % < new (bent) nodal coordinates
%*** define the vector field of displacements:
U = UXYXY;
%*** calculate the deformation tensor EEps(nelm,1:3) = [eps11,eps22,eps12]:
% EEps = getstrain2D(XY,Elm,U);

%*** identifies borders of the domain, nodal connectivity matrix G:


[G,Border,bfac] = incidence2(Elm);

%*** visualize the mesh in Figure 1:


figure(1); clf
gplot(G,XY,b); % < undeformed beam
hold on
gplot(G,UXY,r); % < deformed beam
axis equal
title(undeformed (blue) and deformed (red) beam);
%*** visualize the displacements in X (ie. U(:,1)) in Figure 2:
figure(2); clf
ts2=trisurf(Elm,XY(:,1),XY(:,2),U(:,1));
view(3); % < predefined 3D view
cb=colorbar(horiz); % < horizontal colorbar
axis equal; % < sets axes of equal length equal
shading interp; % < interpolated colors
set(ts2,EdgeColor,k); % < define color of edges
% set(ts2,EdgeAlpha,0.1); % < edge transparency
% set(ts2,FaceAlpha,0.1); % < face transparency
xl2 = xlabel(x^1);
yl2 = ylabel(x^2);
zl2 = zlabel(u^1(x^1,x^2));
ti2 = title(displacements U along X);
%*** visualize the displacements vector field in Figure 3:
Wednesday October 06, 2010 program2.m 3/3

Anda mungkin juga menyukai