Skip to content

Commit

Permalink
legD, Extrema grid
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrubeck committed Jun 9, 2017
1 parent b2f6b47 commit 6abccfd
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 102 deletions.
12 changes: 12 additions & 0 deletions Integration/gaulob.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function [x, w] = gaulob(a, b, n)
% Returns abscissas and weights for the Gauss-Lobatto n-point quadrature
% over the interval [a, b] using the Golub-Welsch Algorithm.
x=zeros(1,n); x([1,n])=[-1,1];
w=zeros(1,n); w([1,n])=(b-a)/(n*(n-1));

k=1:n-3;
E=sqrt((k.*(k+2))./((2*k+1).*(2*k+3)));
[x(2:n-1),V]=trideigs(zeros(1,n-2), E);
w(2:n-1)=2/3*(b-a)*V(1,:).^2./(1-x(2:n-1).^2);
x=(b-a)/2*x+(a+b)/2;
end
45 changes: 24 additions & 21 deletions Relativity/bh.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
end

% Homotopy Analysis Method
its=30;
tol=1e-6;
maxits=30;
tol=3e-10;
h=-1;

% Simulation parameters
Expand Down Expand Up @@ -45,29 +45,32 @@
eqn=@(uu,F) kd(A1*uu+uu*A2'+E.*(psi+uu).^(-7)-F);

% HAM nonlinear functions
R1=@(um) (psi+um{1}).^(-7);
R2=@(um) -7*(psi+um{1}).^(-8).*(um{2});
R3=@(um) -7*(psi+um{1}).^(-9).*((psi+um{1}).*um{3}-4*um{2}.^2);
R4=@(um) -7*(psi+um{1}).^(-10).*((psi+um{1}).^2.*um{4}-8*(psi+um{1}).*um{2}.*um{3}+12*um{2}.^3);
R{1}=@(um) (psi+um{1}).^(-7);
R{2}=@(um) -7*(psi+um{1}).^(-8).*(um{2});
R{3}=@(um) -7*(psi+um{1}).^(-9).*((psi+um{1}).*um{3}-4*um{2}.^2);
R{4}=@(um) -7*(psi+um{1}).^(-10).*((psi+um{1}).^2.*um{4}-8*(psi+um{1}).*um{2}.*um{3}+12*um{2}.^3);
R{5}=@(um) -7*(psi+um{1}).^(-11).*(-30*um{2}.^4+36*(psi+um{1}).*um{2}.^2.*um{3}+...
-8*(psi+um{1}).^2.*um{2}.*um{4}+(psi+um{1}).^2.*(-4*um{3}.^2+(psi+um{1}).*um{5}));

ub=ps(b1,b2);
um=cell(4,1);
um{1}=ub-green(eqn(ub,F));
uu=ub-green(eqn(ub,F));

i=0;
res=norm(eqn(um{1},F),'inf');
while res>tol && i<its
um{2}=h*green(kd(A1*um{1}+um{1}*A2'+E.*R1(um)));
um{3}=um{2}+h*green(kd(A1*um{2}+um{2}*A2'+E.*R2(um)));
um{4}=um{3}+h*green(kd(A1*um{3}+um{3}*A2'+E.*R3(um)));
um{5}=um{4}+h*green(kd(A1*um{4}+um{4}*A2'+E.*R4(um)));
um{1}=um{1}+um{2}+um{3}+um{4}+um{5};
res=norm(eqn(um{1},F),'inf');
i=i+1;
its=0;
err=1;
um=cell(length(R)+1,1);
while err>tol && its<maxits
um{1}=uu;
for k=1:length(R)
um{k+1}=(k>1)*um{k}+h*green(kd(A1*um{k}+um{k}*A2'+E.*R{k}(um)));
end
uu=sum(cat(3,um{:}),3);
err=norm(1-kd(um{1}./uu),'inf');
its=its+1;
end
display(res);
display(i);
uu=psi+um{1};

display(its);
display(err);
uu=uu+psi;

% Plot
[~,mm]=max(r>=sqrt(2)*L);
Expand Down
45 changes: 24 additions & 21 deletions Relativity/bham.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
end

% Homotopy Analysis Method
its=30;
tol=1e-7;
maxits=30;
tol=3e-10;
h=-1;

% Simulation parameters
Expand Down Expand Up @@ -47,30 +47,33 @@
eqn=@(uu,F) kd(A1*uu+uu*A2'+E.*(psi+uu).^(-7)-F);

% HAM nonlinear functions
R1=@(um) (psi+um{1}).^(-7);
R2=@(um) -7*(psi+um{1}).^(-8).*(um{2});
R3=@(um) -7*(psi+um{1}).^(-9).*((psi+um{1}).*um{3}-4*um{2}.^2);
R4=@(um) -7*(psi+um{1}).^(-10).*((psi+um{1}).^2.*um{4}-8*(psi+um{1}).*um{2}.*um{3}+12*um{2}.^3);
R{1}=@(um) (psi+um{1}).^(-7);
R{2}=@(um) -7*(psi+um{1}).^(-8).*(um{2});
R{3}=@(um) -7*(psi+um{1}).^(-9).*((psi+um{1}).*um{3}-4*um{2}.^2);
R{4}=@(um) -7*(psi+um{1}).^(-10).*((psi+um{1}).^2.*um{4}-8*(psi+um{1}).*um{2}.*um{3}+12*um{2}.^3);
R{5}=@(um) -7*(psi+um{1}).^(-11).*(-30*um{2}.^4+36*(psi+um{1}).*um{2}.^2.*um{3}+...
-8*(psi+um{1}).^2.*um{2}.*um{4}+(psi+um{1}).^2.*(-4*um{3}.^2+(psi+um{1}).*um{5}));

ub=ps(b1,b2);
um=cell(4,1);
um{1}=ub-green(eqn(ub,F));
uu=ub-green(eqn(ub,F));

i=0;
res=norm(eqn(um{1},F),'inf');
while res>tol && i<its
um{2}=h*green(kd(A1*um{1}+um{1}*A2'+E.*R1(um)));
um{3}=um{2}+h*green(kd(A1*um{2}+um{2}*A2'+E.*R2(um)));
um{4}=um{3}+h*green(kd(A1*um{3}+um{3}*A2'+E.*R3(um)));
um{5}=um{4}+h*green(kd(A1*um{4}+um{4}*A2'+E.*R4(um)));
um{1}=um{1}+um{2}+um{3}+um{4}+um{5};
res=norm(eqn(um{1},F),'inf');
i=i+1;
its=0;
err=1;
um=cell(length(R)+1,1);
while err>tol && its<maxits
um{1}=uu;
for k=1:length(R)
um{k+1}=(k>1)*um{k}+h*green(kd(A1*um{k}+um{k}*A2'+E.*R{k}(um)));
end
uu=sum(cat(3,um{:}),3);
err=norm(1-um{1}./uu,'inf');
its=its+1;
end
display(res);
display(i);

uu=1+um{1};
display(its);
display(err);
uu=uu+1;

figure(1);
surf(kron([-1,1],rho), z(:,[end:-1:1,1:end]), uu(:,[end:-1:1,1:end]));

Expand Down
44 changes: 23 additions & 21 deletions Relativity/bham2.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
end

% Homotopy Analysis Method
its=30;
tol=1e-7;
maxits=30;
tol=3e-10;
h=-1;

% Simulation parameters
Expand Down Expand Up @@ -51,30 +51,32 @@
eqn=@(uu,F) kd(A1*uu+uu*A2'+E.*(psi+uu).^(-7)-F);

% HAM nonlinear functions
R1=@(um) (psi+um{1}).^(-7);
R2=@(um) -7*(psi+um{1}).^(-8).*(um{2});
R3=@(um) -7*(psi+um{1}).^(-9).*((psi+um{1}).*um{3}-4*um{2}.^2);
R4=@(um) -7*(psi+um{1}).^(-10).*((psi+um{1}).^2.*um{4}-8*(psi+um{1}).*um{2}.*um{3}+12*um{2}.^3);
R{1}=@(um) (psi+um{1}).^(-7);
R{2}=@(um) -7*(psi+um{1}).^(-8).*(um{2});
R{3}=@(um) -7*(psi+um{1}).^(-9).*((psi+um{1}).*um{3}-4*um{2}.^2);
R{4}=@(um) -7*(psi+um{1}).^(-10).*((psi+um{1}).^2.*um{4}-8*(psi+um{1}).*um{2}.*um{3}+12*um{2}.^3);
R{5}=@(um) -7*(psi+um{1}).^(-11).*(-30*um{2}.^4+36*(psi+um{1}).*um{2}.^2.*um{3}+...
-8*(psi+um{1}).^2.*um{2}.*um{4}+(psi+um{1}).^2.*(-4*um{3}.^2+(psi+um{1}).*um{5}));

ub=ps(b1,b2);
um=cell(4,1);
um{1}=ub-green(eqn(ub,F));
uu=ub-green(eqn(ub,F));

i=0;
res=norm(eqn(um{1},F),'inf');
while res>tol && i<its
um{2}=h*green(kd(A1*um{1}+um{1}*A2'+E.*R1(um)));
um{3}=um{2}+h*green(kd(A1*um{2}+um{2}*A2'+E.*R2(um)));
um{4}=um{3}+h*green(kd(A1*um{3}+um{3}*A2'+E.*R3(um)));
um{5}=um{4}+h*green(kd(A1*um{4}+um{4}*A2'+E.*R4(um)));
um{1}=um{1}+um{2}+um{3}+um{4}+um{5};
res=norm(eqn(um{1},F),'inf');
i=i+1;
its=0;
err=1;
um=cell(length(R)+1,1);
while err>tol && its<maxits
um{1}=uu;
for k=1:length(R)
um{k+1}=(k>1)*um{k}+h*green(kd(A1*um{k}+um{k}*A2'+E.*R{k}(um)));
end
uu=sum(cat(3,um{:}),3);
err=norm(1-um{1}./uu,'inf');
its=its+1;
end
display(res);
display(i);

uu=1+um{1};
display(its);
display(err);
uu=uu+1;

figure(1);
surf([rr;-rr(end:-1:1,:)],zz([1:end,end:-1:1],:),uu([1:end,end:-1:1],:));
Expand Down
39 changes: 9 additions & 30 deletions Relativity/brill.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
end

% Simulation parameters
L=8*(m-1)/(m-2);
L=8;
r0=sqrt(2)*L;
A0=1;

[Dx,x]=chebD(2*m);
A1=diag(x.^2)*Dx*Dx+diag(2*x)*Dx;
[A1,Dx,x]=radial(A1,Dx,x);

[Dy,y]=chebD(n); y=y';
[Dy,y]=legD(n); y=y';
A2=diag(1-y.^2)*Dy*Dy-diag(2*y)*Dy;

a=[1,1;0,0];
Expand All @@ -39,17 +39,19 @@

% Solution
[green,ps,kd,sc,gb]=elliptic(A1,A2,B1,B2,1,[1,n]);
afun=@(uu) sc(uu)+kd(C).*uu;
pfun=@(uu) kd(green(uu));

ub=ps(b1,b2);
rhs=kd(F-A1*ub-ub*A2'-C.*ub);
u0=kd(ub+green(rhs));

[uu,res,its]=precond(afun,pfun,rhs,u0,20,1e-10);
uu=gb(uu)+ub;
display(res);
afun=@(uu) (sc(uu)+kd(C).*uu);
pfun=@(uu) kd(green(uu));

[u1,res,its]=precond(afun,pfun,rhs,u0,20,2e-15);
uu=gb(u1)+ub;

display(its);
display(res);

figure(1);
surf(kron([-1,1],rho), z(:,[end:-1:1,1:end]), uu(:,[end:-1:1,1:end]));
Expand All @@ -67,27 +69,4 @@
ylabel('$z$');
title('$\psi(\rho,z)$');
view(2);

% [rr,zz]=ndgrid(linspace(-L,L,2*m));
% xq=hypot(rr,zz)/r0;
% yq=zz./hypot(rr,zz);
% [xx,yy]=ndgrid(x,y);
% psi=interp2(xx',yy',uu',xq',yq')';
%
% figure(2);
% surf(rr, zz, psi);
%
% colormap(jet(256));
% colorbar;
% shading interp;
% %camlight;
% axis square;
% xlim([-L,L]);
% ylim([-L,L]);
% set(gcf,'DefaultTextInterpreter','latex');
% set(gca,'TickLabelInterpreter','latex','fontsize',14);
% xlabel('$\rho$');
% ylabel('$z$');
% title('$\psi(\rho,z)$');
% view(2);
end
93 changes: 93 additions & 0 deletions Relativity/brillTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
function [] = brillTest(m,n)
n(1:length(m))=n;
err=zeros(size(m));
millis=zeros(numel(m),2);
for i=1:length(m)
[err(i), millis(i,:)]=brillSolve(m(i),n(i));
drawnow;
end
figure(2);
semilogy(m,err);
title('Error |\Delta|_\infty');

figure(3);
plot(m,millis);
title('Time (ms)');
end

function [err,millis] = brillSolve(m,n)
millis=[0,0];
tic;
% Simulation parameters
L=8;
r0=sqrt(2)*L;

[Dx,x]=chebD(2*m);
A1=diag(x.^2)*Dx*Dx+diag(2*x)*Dx;
[A1,Dx,x]=radial(A1,Dx,x);

[Dy,y]=chebD(n); y=y';
A2=diag(1-y.^2)*Dy*Dy-diag(2*y)*Dy;

a=[1,1;0,0];
b=[1,1;1,1];

% Imposition of boundary conditions
E1=eye(m);
E2=eye(n);
B1=a(1,1)*E1(1,:)+b(1,1)*Dx(1,:);
B2=diag(a(2,:))*E2([1,end],:)+diag(b(2,:))*Dy([1,end],:);

b1=0*y+1.000068252008242;
b2=[0*x,0*x];

% Coordinate mapping
r=r0*x;
th=acos(y);
rho=r*sin(th);
z=r*cos(th);

% Test code
C=(1/2)*exp(-rho.^2-z.^2).*(1+2*rho.^2.*(-3+rho.^2+z.^2));
F=(1/20).*(1+rho.^2+z.^2).^(-5/2).*((-6)+exp(-rho.^2-z.^2) ...
.*(1+rho.^2+z.^2).^2.*(1+2.*rho.^2.*((-3)+rho.^2+z.^2)).*( ...
1+10.*(1+rho.^2+z.^2).^(1/2)));
C=diag(r.^2)*C;
F=diag(r.^2)*F;

% Solution
[green,ps,kd,sc,gb]=elliptic(A1,A2,B1,B2,1,[1,n]);

ub=ps(b1,b2);
rhs=kd(F-A1*ub-ub*A2'-C.*ub);
u0=kd(ub+green(rhs));

afun=@(uu) (sc(uu)+kd(C).*uu);
pfun=@(uu) kd(green(uu));

millis(1)=toc;
tic;

[u1,res,its]=precond(afun,pfun,rhs,u0,20,2e-16);
uu=gb(u1)+ub;
millis(2)=1000*toc;

ug=1+(1/10).*(1+rho.^2+z.^2).^(-1/2);
err=norm(kd(uu-ug),'inf');

display(its);
display(res);
display(err);

figure(1);
surf(kron([-1,1],rho), z(:,[end:-1:1,1:end]), uu(:,[end:-1:1,1:end]));

colormap(jet(256));
colorbar;
shading interp;
%camlight;
axis square;
xlim([-L,L]);
ylim([-L,L]);
view(2);
end
5 changes: 3 additions & 2 deletions Relativity/elliptic.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
V1(rd1,:)=G1*V1(kd1,:);
V2(rd2,:)=G2*V2(kd2,:);
[L1,L2]=ndgrid(L1,L2);
LL=L1+L2; LL(abs(LL)<1e-9)=inf;
LL=L1+L2;
W1=inv(V1(kd1,:));
W2=inv(V2(kd2,:));

% Green's function
green=@(rhs) V1*((W1*rhs*W2')./LL)*V2';
green=@(rhs) V1*(((W1*rhs*W2'))./LL)*V2';


% Poincare-Steklov operator
N1=zeros(m,length(rd1)); N1(rd1,:)=inv(B1(:,rd1));
Expand Down
2 changes: 2 additions & 0 deletions Relativity/precond.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
function [uu,res,its] = precond(eqn, green, rhs, u0, maxit, tol)
% bicgstab helper for 2D elliptic equations
afun=@(x) reshape(eqn(reshape(x, size(u0))), [], 1);
pfun=@(x) reshape(green(reshape(x, size(u0))), [], 1);
[x,~,res,its]=bicgstab(afun,rhs(:),tol,maxit,pfun,[],u0(:));
%[x,~,res,its]=gmres(afun,rhs(:),[],tol,maxit,pfun,[],u0(:));
uu=reshape(x,size(u0));
end
Loading

0 comments on commit 6abccfd

Please sign in to comment.