phd-scripts/Unpublished/XFEM2/1D_Solver/GetF.m

128 lines
3.6 KiB
Mathematica
Raw Normal View History

2024-05-13 19:50:21 +00:00
function [] = GetF()
% set up grid
gd=0.;
numElem=4;
eLen=0.25;
for i=1:numElem+1
ndCrd(i)=gd;
gd=gd+eLen;
end
for i=1:numElem
elemNod(i,1)=i;
elemNod(i,2)=i+1;
end
% Initial level set
dpos=0.1;
for i=1:numElem+1
lSet(i)=sign(ndCrd(i)-dpos)*abs(dpos-ndCrd(i));
end
lSet'
for tstep=1:1
% Velocity BC
F=zeros(numElem+1,1);
for i=1:numElem
if sign(lSet(elemNod(i,1)))~=sign(lSet(elemNod(i,2)))
F(elemNod(i,1))= 0.0005;
F(elemNod(i,2))= 0.0005;
end
end
% Assemble 'Stiffness' Matrices
A=zeros(numElem+1);
for i=1:numElem
pos(1)=-1/sqrt(3);
pos(2)=1/sqrt(3);
AfL=zeros(2);
AfLGLS=zeros(2);
for j=1:2
shp(1)=(1-pos(j))/2.;
shp(2)=(1+pos(j))/2.;
dshp(1)=-0.5;
dshp(2)=0.5;
rset=shp(1)*lSet(elemNod(i,1))+shp(2)*lSet(elemNod(i,2));
dls=dshp(1)*lSet(elemNod(i,1))+dshp(2)*lSet(elemNod(i,2));
AfL=AfL+shp'*sign(rset)*(dls*dshp);
AfLGLS=AfLGLS+(dshp'*dls)*(0.25/abs(dls))*(dls*dshp);
end
for k=1:2;
for j=1:2;
A(elemNod(i,j),elemNod(i,k))=A(elemNod(i,j),elemNod(i,k))+AfL(j,k)+AfLGLS(j,k)
end
end
end
% Apply BCs
RHS=zeros(numElem+1,1);
Sub=A*F;
iindex=0;
for i=1:numElem+1
if F(i)==0.
iindex=iindex+1;
RHSred(iindex)=RHS(i)-Sub(i);
Fred=0.;
jindex=0;
for j=1:numElem+1
if F(j)==0.
jindex=jindex+1;
Ared(iindex,jindex)=A(i,j);
end
end
end
end
% Solve for Fred
Fred=(Ared^-1)*RHSred';
% Get F
iindex=0;
for i=1:numElem+1
if F(i)==0.
iindex=iindex+1;
F(i)=Fred(iindex);
end
end
% Update level set
mMat=zeros(numElem+1);
mMatGLS=zeros(numElem+1);
f1=zeros(numElem+1,1);
f2=zeros(numElem+1,1);
f3=zeros(numElem+1,1);
h=0.25;
visc=0.000;
for i=1:numElem
pos(1)=-1/sqrt(3);
pos(2)=1/sqrt(3);
mMatL=zeros(2);
mMatGLSL=zeros(2);
f1L=zeros(2,1);
f2L=zeros(2,1);
f3L=zeros(2,1);
for j=1:2
shp(1)=(1-pos(j))/2.;
shp(2)=(1+pos(j))/2.;
dshp(1)=-0.5;
dshp(2)=0.5;
Floc=shp(1)*F(elemNod(i,1))+shp(2)*F(elemNod(i,2));
rset=shp(1)*lSet(elemNod(i,1))+shp(2)*lSet(elemNod(i,2));
dls=dshp(1)*lSet(elemNod(i,1))+dshp(2)*lSet(elemNod(i,2));
mMatL=mMatL+shp'*shp;
mMatGLSL=mMatGLSL+((dshp'*(dls/abs(dls)))*Floc*(h/abs(Floc)))*shp;
f1L=f1L+shp'*Floc*abs(dls);
f2L=f2L+(dshp'*(dls/abs(dls))*Floc)*(h/abs(Floc))*Floc*abs(dls);
vs=h*((abs(visc+Floc*abs(dls)))/(abs(Floc*dls)+h));
f3L=f3L+vs*dshp'*dls;
end
for k=1:2;
for j=1:2;
mMat(elemNod(i,j),elemNod(i,k))=mMat(elemNod(i,j),elemNod(i,k))+mMatL(j,k);
mMatGLS(elemNod(i,j),elemNod(i,k))=mMatGLS(elemNod(i,j),elemNod(i,k))+mMatGLSL(j,k);
end
f1(elemNod(i,k))=f1(elemNod(i,k))+f1L(k);
f2(elemNod(i,k))=f2(elemNod(i,k))+f2L(k);
f3(elemNod(i,k))=f3(elemNod(i,k))+f3L(k);
end
end
dt=0.01;
lSet=lSet-((((mMat+mMatGLS)^-1)/dt)*(f1+f2+f3))';
lSet';
end