Sei sulla pagina 1di 10

%---- Parameters of the problem ----%

a(1) = 0.5; %Values of a and b


b = 0.05;
circRadius = 5; %Farfield radius
jtot = 160; % Grid resolution required. If 161x33, then jtot = 160, itot = 33
itot = 33;
convergenceCriterion = 0.00001;
%% --------------- ALGEBRAIC GRID ------------------- %%
for j=1:(jtot/2)
theta=((-1*((j-1)/(jtot/2))*180))*pi/180;
%----boundary ellipse
x(1,j)=a(1)*cos(theta);
y(1,j)=sqrt((1-(x(1,j)*x(1,j)/(a(1)*a(1))))*(b*b));
x(1,(j+(jtot/2))) = -a(1)*cos(theta);
y(1,(j+(jtot/2)))=-1*y(1,j);
if(j==(jtot/2))
x(1,(1+j+(jtot/2))) = a(1);
y(1,(1+j+(jtot/2)))=0;
end

%-----far field
x(itot,j) = circRadius*cos(theta);
y(itot,j) = sqrt( (circRadius*circRadius) - ((x(itot,j))*(x(itot,j))) );
x(itot,(j+(jtot/2))) = -circRadius*cos(theta);
y(itot,(j+(jtot/2))) = -1*y(itot,j);
if(j==(jtot/2))
x(itot,(1+j+(jtot/2))) = circRadius;
y(itot,(1+j+(jtot/2)))=0;
end

end
%-----inner points (uniformly distributed)
for i = 2:(itot-1)
a(i) = a((i-1)) + ( ((x(itot,1) - x(1,1))/ (itot-1)) );
for j=1:(jtot/2)
theta=((-1*((j-1)/(jtot/2))*180))*pi/180;
m = (y(itot,j) - y(1,j))/(x(itot,j) - x(1,j));
x(i,j)=a(i)*cos(theta);
y(i,j) = m*( x(i,j) - x(1,j) ) + y(1,j);
x(i,(j+(jtot/2))) = -a(i)*cos(theta);
y(i,(j+(jtot/2)))=-1*y(i,j);
if(j==(jtot/2))
x(i,(1+j+(jtot/2))) = a(i);
y(i,(1+j+(jtot/2)))=0;
end
end
end
%----- To plot algebraic grid
for i=1:itot
plot(x(i,:),y(i,:),'r')
hold on;
end
for j=1:jtot
plot(x(:,j),y(:,j),'r')
hold on;
end
title('Grid 1 - Algebraic Grid');
figure
% -------ELLIPTIC METHOD - points fixed at boundary and farfield ------- %
omega = 1.5;

converged = 0;
convergenceCriterion = 0.00001;
testValue = 1;
LCount=1;
%---- Laplace solver--while(abs(x(4,79)-testValue))>convergenceCriterion

testValue = x(4,79);
for i = 2:(itot-1)
x(i,1) = x(i,(jtot+1)); % for point 1
for j = 2:(jtot) %for points 2 to 160
xg = (x(i+1,j) - x((i-1),j))/2;
yg = (y(i+1,j) - y((i-1),j))/2;
xn = (x(i,(j+1)) - x(i,(j-1)))/2;
yn = (y(i,(j+1)) - y(i,(j-1)))/2;
J = (xg*yn) - (xn*yg);
alpha = (xn*xn) + (yn*yn);
beta = (xg*xg) + (yg*yg);
gamma = (xn*xg) + (yn*yg);
xpredict = ( (alpha*(x(i+1,j) + x(i-1,j))) + (beta*(x(i,j+1)+x(i,j-1))) ) / (2*
(alpha+beta));
ypredict = ( (alpha*(y(i+1,j) + y(i-1,j))) + (beta*(y(i,j+1)+y(i,j-1))) ) / (2*
(alpha+beta));
x(i,j) = x(i,j) + (omega*(xpredict - x(i,j)));
y(i,j) = y(i,j) + (omega*(ypredict - y(i,j)));
end

%For the point 161


j=jtot+1;
xg = (x(i+1,j) - x(i-1,j))/2;
yg = (y(i+1,j) - y(i-1,j))/2;

xn = (x(i,2) - x(i,j-1))/2;
yn = (y(i,2) - y((i),j-1))/2;
J = (xg*yn) - (xn*yg);
alpha = (xn*xn) + (yn*yn);
beta = (xg*xg) + (yg*yg);
gamma = (xn*xg) + (yn*yg);
xpredict = ( (alpha*(x(i+1,j) + x(i-1,j))) + (beta*(x(i,2)+x(i,j-1))) ) / (2*(a
lpha+beta));
x(i,j) = x(i,j) + (omega*(xpredict - x(i,j)));
end

LaplaceConvergence(LCount) = abs(x(4,79)-testValue);
LapIter(LCount) = LCount;
LCount = LCount+1;

end
%%---- end of Laplace solver---semilogy(LapIter,LaplaceConvergence,'-');
title('Convergence Graph');
figure
%%---Checking for quality of grid
qualityCounter = 1;
goodCells = 0;
i=1;
for j = 1:jtot

x1 = (x(i,j) - x(i,j+1));
y1 = (y(i,j) - y(i,j+1));
x2 = (x(i+1,j+1) - x(i,j+1));
y2 = (y(i+1,j+1) - y(i,j+1));
ang(i,j) = acosd(((x1*x2)+(y1*y2))/((sqrt((x1*x1)+(y1*y1)))*(sqrt((x2*x2)+(y2*y

2)))));
if ((ang(i,j) < 91) && (ang(i,j) > 89))
goodCells = goodCells +1;
end
histogrm(qualityCounter) = ang(i,j);
qualityCounter = qualityCounter + 1;
end
hist(histogrm);
title('Quality of cells at boundary - No of cells vs Angle of cell at boundary'
);
figure

%---Plots grid 2---for i=1:itot


plot(x(i,:),y(i,:),'r')
hold on;
end
for j=1:jtot
plot(x(:,j),y(:,j),'r')
hold on;
end
title('Grid 2 - Elliptic Grid with fixed boundary points');
goodCells
figure
%-------------------------------------------------------------------------%v---ELLIPTIC METHOD - points floating at boundary and fixed at farfield--- %
%---------------------------------------------------------------------------%
G3testValue = 1;
BCount=1;
G3convergenceCriterion = 0.00001;
LCount = 1;

while(abs(x(2,50)-G3testValue))>G3convergenceCriterion

G3testValue = x(2,50);
% ----checking for shortest distance bet boundary point and next interior point
---change = 0.0001;
OldDistance =0;
for j = 2:(jtot)
Direction = 1;
DistanceDifference = (sqrt(((x(2,j)-x(1,j))*(x(2,j)-x(1,j))) + ((y(2,j)-y(1,j))
*(y(2,j)-y(1,j))))) - OldDistance;
SignTemp = 1;
count = 0;
while (abs(DistanceDifference) > convergenceCriterion)
count = count+1;
CurrentSign = SignTemp;
Distance = sqrt(((x(2,j)-x(1,j))*(x(2,j)-x(1,j))) + ((y(2,j)-y(1,j))*(y(2,j)-y(
1,j))));
OldDistanceDifference = DistanceDifference;
if x(1,j)==a(1) && CurrentSign==1
Direction = -Direction;
end
x(1,j) = x(1,j) + change*Direction;
if x(1,j) >= a(1)
x(1,j) = a(1);
y(1,j) = sqrt((1-(x(1,j)*x(1,j)/(a(1)*a(1))))*(b*b));
break;
end
if x(1,j) <= -a(1)
x(1,j) = -a(1);
y(1,j) = sqrt((1-(x(1,j)*x(1,j)/(a(1)*a(1))))*(b*b));

break;
end
y(1,j) = sqrt((1-(x(1,j)*x(1,j)/(a(1)*a(1))))*(b*b));
if j>80
y(1,j) = -y(1,j);
end
NewDistance = sqrt(((x(2,j)-x(1,j))*(x(2,j)-x(1,j))) + ((y(2,j)-y(1,j))*(y(2,j)
-y(1,j))));
DistanceDifference = (NewDistance - Distance);
SignTemp = sign(DistanceDifference);
if ((SignTemp==1) && (CurrentSign==1))
Direction = -Direction;
end
if ((SignTemp==1) && (CurrentSign==-1))
Direction = -Direction/2;
end
if (abs(OldDistanceDifference - DistanceDifference) <= convergenceCriterion)
break;
end
OldDistance = NewDistance;
end
end

%%%%----------laplace solver--------------------%%%%
omega = 1.5;
convergenceCriterion = 0.00001;
testValue = 1;
while(abs(x(4,79)-testValue))>convergenceCriterion
testValue = x(4,79);
for i = 2:(itot-1)
x(i,1) = x(i,(jtot+1));

for j = 2:(jtot)
xg = (x(i+1,j)-x(i-1,j))/2;
yg = (y(i+1,j)-y(i-1,j))/2;
xn = (x(i,j+1)-x(i,j-1))/2;
yn = (y(i,j+1)-y(i,j-1))/2;
J = (xg*yn) - (xn*yg);
alpha = (xn*xn) + (yn*yn);
beta = (xg*xg) + (yg*yg);
gamma = (xn*xg) + (yn*yg);
xpredict = ( (alpha*(x(i+1,j) + x(i-1,j))) + (beta*(x(i,j+1)+x(i,j-1))) ) / (2*
(alpha+beta));
ypredict = ( (alpha*(y(i+1,j) + y(i-1,j))) + (beta*(y(i,j+1)+y(i,j-1))) ) / (2*
(alpha+beta));
x(i,j) = x(i,j) + (omega*(xpredict - x(i,j)));
y(i,j) = y(i,j) + (omega*(ypredict - y(i,j)));
end
j=jtot+1;
xg = (x(i+1,j) - x(i-1,j))/2;
yg = (y(i+1,j) - y(i-1,j))/2;
xn = (x(i,2) - x(i,j-1))/2;
yn = (y(i,2) - y((i),j-1))/2;
J = (xg*yn) - (xn*yg);
alpha = (xn*xn) + (yn*yn);
beta = (xg*xg) + (yg*yg);
gamma = (xn*xg) + (yn*yg);
xpredict = ( (alpha*(x(i+1,j) + x(i-1,j))) + (beta*(x(i,2)+x(i,j-1))) ) / (2*(a
lpha+beta));
x(i,j) = x(i,j) + (omega*(xpredict - x(i,j)));
end
if BCount < 2
LaplaceConvergence(LCount) = abs(x(4,79)-testValue);

LapIter(LCount) = LCount;
LCount = LCount+1;
end
end
Grid3Convergence(BCount) = abs(x(2,50)-G3testValue);
G3Iter(BCount) = BCount;
BCount = BCount+1;
end %end main elliptic loop
BCount
%-----------GRID QUALITY CHECKER-------------%
qualityCounter = 1;
goodCells = 0;
i=1;
for j = 1:jtot

x1 = (x(i,j) - x(i,j+1));
y1 = (y(i,j) - y(i,j+1));
x2 = (x(i+1,j+1) - x(i,j+1));
y2 = (y(i+1,j+1) - y(i,j+1));
ang(i,j) = acosd(((x1*x2)+(y1*y2))/((sqrt((x1*x1)+(y1*y1)))*(sqrt((x2*x2)+(y2*y
2)))));
if ((ang(i,j) < 91) && (ang(i,j) > 89))
goodCells = goodCells +1;
end
histogrm(qualityCounter) = ang(i,j);
qualityCounter = qualityCounter + 1;
end
%---------------------------------------------%
hist(histogrm);
title('Quality of cells at boundary - No of cells vs Angle of cell at boundary')
;

goodCells
figure
for i=1:itot
plot(x(i,:),y(i,:),'b')
hold on;
end
for j=1:jtot
plot(x(:,j),y(:,j),'b')
hold on;
end
title('Grid 3 - Elliptic grid with mesh orthogonal at boundary');
%%----------------------END OF GRID 3 CODE -------------------%%%%%