phd-scripts/Unpublished/XFEM2/FESolveX2D.m

270 lines
8.2 KiB
Mathematica
Raw Normal View History

2024-05-13 19:50:21 +00:00
function [] = FESolveX2D()
% MATLAB based 2-D XFEM Solver
% J. Grogan (2012)
clear all
% Define Mesh
NumX=4;
NumY=1;
delX=1.;
delY=1.;
for j=1:NumY+1
for i=1:NumX+1
index=i+(NumX+1)*(j-1);
Node(index,1)=single((i-1.))*delX;
Node(index,2)=single((j-1.))*delY;
end
end
numNodes=(NumX+1)*(NumY+1);
for j=1:NumY
for i=1:NumX
index=i+NumX*(j-1);
Element(index,1)=i+(NumX+1)*(j-1);
Element(index,2)=i+(NumX+1)*(j-1)+1;
Element(index,3)=i+(NumX+1)*(j)+1;
Element(index,4)=i+(NumX+1)*(j);
end
end
numElem=(NumX)*(NumY);
% dofs per node
ndof=2;
% Define Section Properties
rho=1.;
% initial interface position
dpos=0.1;
% Initial temperatures
Tnew=zeros(numNodes*2,1);
Bound=zeros(numNodes*2,1);
stored(1)=dpos;
for e=1:numElem
for n=1:4
crdn=Node(Element(e,n),1);
if crdn<=dpos
Tnew(2*Element(e,n)-1)=1.;
Bound(2*Element(e,n)-1)=1.;
end
end
end
% Define Time Step
dtime=0.1;
tsteps=10;
time=0.;
% penalty term
beta=80.;
% Loop through time steps
for ts=1:tsteps
% Get interface velocity
d(1)=dpos+delX;
d(2)=dpos+3*delX/4;
d(3)=dpos+delX/4;
d(4)=dpos;
for e=1:numElem
crdn1=Node(Element(e,1),1);
crdn2=Node(Element(e,2),1);
for j=1:4
if d(j)>=crdn1 & d(j)<crdn2
elen=abs(crdn2-crdn1);
ajacob=elen/2.;
point=(d(j)-crdn1)/ajacob-1.;
theta(1)=abs(crdn1-dpos)*sign(crdn1-dpos);
theta(2)=abs(crdn2-dpos)*sign(crdn2-dpos);
tmp1a=Tnew(Element(e,1)*2-1);
tmp1b=Tnew(Element(e,1)*2);
tmp2a=Tnew(Element(e,2)*2-1);
tmp2b=Tnew(Element(e,2)*2);
xi=point;
gm(1)=(1.-xi)/2.;
gm(3)=(1.+xi)/2.;
term=theta(1)*gm(1)+theta(2)*gm(3);
gm(2)=gm(1)*(abs(term)-abs(theta(1)));
gm(4)=gm(3)*(abs(term)-abs(theta(2)));
t(j)=gm(1)*tmp1a+gm(2)*tmp1b+gm(3)*tmp2a+gm(4)*tmp2b;
end
end
end
% vel=-0.1*(0.5/delX)*(2*t(1)+t(2)-t(3)-2*t(4))
vel=0.0;
% Update interface position
dpos=dpos+vel*dtime;
stored(ts+1)=dpos;
K=zeros(numNodes*ndof,numNodes*ndof);
M=zeros(numNodes*ndof,numNodes*ndof);
pforce=zeros(numNodes*ndof,1);
% Loop Through Elements
for e=1:numElem
Ke=zeros(4*ndof);
Me=zeros(4*ndof);
for icrd=1:4;
crdnx(icrd)=Node(Element(e,icrd),1);
crdny(icrd)=Node(Element(e,icrd),2);
theta(icrd)=abs(crdnx(icrd)-dpos)*sign(crdnx(icrd)-dpos);
end
if sign(theta(1))~=sign(theta(2))
% enriched element
enr=8;
% get interface position on element
elen=abs(crdnx(2)-crdnx(1));
frac=abs(dpos-crdnx(1))/elen;
len1=2.*frac;
len2=2.*(1.-frac);
% devide element for sub integration
mid1=-1+len1/2.;
mid2=1-len2/2.;
gx(1)=mid1-(len1/2.)/sqrt(3.);
gx(2)=mid1+(len1/2.)/sqrt(3.);
gx(3)=mid1+(len1/2.)/sqrt(3.);
gx(4)=mid1-(len1/2.)/sqrt(3.);
gx(5)=mid2-(len2/2.)/sqrt(3.);
gx(6)=mid2+(len2/2.)/sqrt(3.);
gx(7)=mid2+(len2/2.)/sqrt(3.);
gx(8)=mid2-(len2/2.)/sqrt(3.);
gpos=1/sqrt(3.);
hx(1)=-gpos;
hx(2)=-gpos;
hx(3)=+gpos;
hx(4)=+gpos;
hx(5)=-gpos;
hx(6)=-gpos;
hx(7)=+gpos;
hx(8)=+gpos;
for iw=1:4
w(iw)=frac/2.;
w(iw+4)=(1.-frac)/2.;
end
else
% regular element - fix extra dofs
enr=4;
gpos=1/sqrt(3.);
gx(1)=-gpos;
gx(2)=gpos;
gx(3)=gpos;
gx(4)=-gpos;
hx(1)=-gpos;
hx(2)=-gpos;
hx(3)=gpos;
hx(4)=gpos;
for iw=1:4
w(iw)=1.;
end
end
% Loop Through Int Points
for i=1:enr;
g=gx(i);
h=hx(i);
phi(1)=0.25*(1.-g)*(1.-h);
phi(3)=0.25*(1.+g)*(1.-h);
phi(5)=0.25*(1.+g)*(1.+h);
phi(7)=0.25*(1.-g)*(1.+h);
iLS=theta(1)*phi(1)+theta(2)*phi(3)+theta(3)*phi(5)+theta(4)*phi(7);
if iLS<0.
cond=0.;
spec=0.01;
else
cond=1.;
spec=1.;
end
for iter=1:4
phi(2*iter)=phi(2*iter-1)*(abs(iLS)-abs(theta(iter)));
end
phig(1)=0.25*-(1.-h);
phig(3)=0.25*(1.-h);
phig(5)=0.25*(1.+h);
phig(7)=0.25*-(1.+h);
phih(1)=0.25*-(1.-g);
phih(3)=0.25*-(1.+g);
phih(5)=0.25*(1.+g);
phih(7)=0.25*(1.-g);
diLSg=sign(iLS)*(phig(1)*theta(1)+phig(3)*theta(2)+phig(5)*theta(3)+phig(7)*theta(4));
diLSh=sign(iLS)*(phih(1)*theta(1)+phih(3)*theta(2)+phih(5)*theta(3)+phih(7)*theta(4));
for iter=1:4
phig(2*iter)=phig(2*iter-1)*(abs(iLS)-abs(theta(iter)))+phi(2*iter-1)*diLSg;
phih(2*iter)=phih(2*iter-1)*(abs(iLS)-abs(theta(iter)))+phi(2*iter-1)*diLSh;
end
rjac=zeros(2,2);
for iter=1:4
rjac(1,1)=rjac(1,1)+phig(2*iter-1)*crdnx(iter);
rjac(1,2)=rjac(1,2)+phig(2*iter-1)*crdny(iter);
rjac(2,1)=rjac(2,1)+phih(2*iter-1)*crdnx(iter);
rjac(2,2)=rjac(2,2)+phih(2*iter-1)*crdny(iter);
end
djac=rjac(1,1)*rjac(2,2)-rjac(1,2)*rjac(2,1);
rjaci(1,1)= rjac(2,2)/djac;
rjaci(2,2)= rjac(1,1)/djac;
rjaci(1,2)=-rjac(1,2)/djac;
rjaci(2,1)=-rjac(2,1)/djac ;
for iter=1:8
phix(iter)=rjaci(1,1)*phig(iter)+rjaci(1,2)*phih(iter);
phiy(iter)=rjaci(2,1)*phig(iter)+rjaci(2,2)*phih(iter);
end
we=w(i)*djac;
Ke=Ke+we*cond*(phix'*phix+phiy'*phiy);
Me=Me+(we*rho*spec*phi'*phi)/dtime;
end
% Add penalty term and get temp gradient on interface
if enr==8;
xi=2.*frac-1;
yi=0.;
gm(1)=0.25*(1.-xi)*(1.-yi);
gm(3)=0.25*(1.+xi)*(1.-yi);
gm(5)=0.25*(1.+xi)*(1.+yi);
gm(7)=0.25*(1.-xi)*(1.+yi);
gm(2)=gm(1)*(-abs(theta(1)));
gm(4)=gm(3)*(-abs(theta(2)));
gm(6)=gm(5)*(-abs(theta(3)));
gm(8)=gm(7)*(-abs(theta(4)));
pen=beta*(gm'*gm);
pfL=beta*1.*gm';
Ke=Ke+pen;
else
pen=zeros(8);
pfL=zeros(8,1);
end
% Assemble Global Matrices
gnum(1)=2*Element(e,1)-1;
gnum(2)=2*Element(e,2)-1;
gnum(3)=2*Element(e,3)-1;
gnum(4)=2*Element(e,4)-1;
for i=1:4;
for j=1:4;
K(gnum(j),gnum(i))=K(gnum(j),gnum(i))+Ke(2*j-1,2*i-1);
K(gnum(j)+1,gnum(i)+1)=K(gnum(j)+1,gnum(i)+1)+Ke(2*j,2*i);
M(gnum(j),gnum(i))=M(gnum(j),gnum(i))+Me(2*j-1,2*i-1);
M(gnum(j)+1,gnum(i)+1)=M(gnum(j)+1,gnum(i)+1)+Me(2*j,2*i);
end
end
for i=1:4;
pforce(gnum(i))=pforce(gnum(i))+pfL(2*i-1);
pforce(gnum(i)+1)=pforce(gnum(i)+1)+pfL(2*i);
end
end
%Remove inactive DOFs(Reduce Matrices)
RHS=M*Tnew;
A=K+M;
Sub=A*Bound;
iindex=0;
for i=1:ndof*numNodes;
if Bound(i)==0.;
iindex=iindex+1;
RHSred(iindex)=RHS(i)-Sub(i)+pforce(i);
jindex=0;
for j=1:ndof*numNodes;
if Bound(j)==0.;
jindex=jindex+1;
Ared(iindex,jindex)=A(i,j);
end
end
end
end
%Solve
StiffI=Ared^-1;
Tnewr=(Ared^-1)*RHSred';
iindex=0;
for i=1:ndof*numNodes;
if Bound(i)==0.;
iindex=iindex+1;
Tnew(i)=Tnewr(iindex);
end
end
Tnew
end
stored';