Skip to content

Commit

Permalink
fixing transmission helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mrachh committed Oct 18, 2024
1 parent 65ed58f commit 7d53a70
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 73 deletions.
2 changes: 1 addition & 1 deletion chunkie/+chnk/+helm2d/fmm.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
%
% Optional Output:
% pot - potential/neumann data corresponding to the kernel at the target locations
% grad - gradient at target locations
% grad - gradient at target locations
% hess - hessian at target locations

if ( nargout == 0 )
Expand Down
48 changes: 33 additions & 15 deletions chunkie/+chnk/+helm2d/transmission_helper.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
function [kerns,varargout] = transmission_helper(chnkobj,ks,cs,coefs,varargin)
function [kerns, varargout] = transmission_helper2(chnkobj,ks,cs,coefs,varargin)
%CHNK.HELM2D.TRANSMISSION_HELPER builds the matrix of kernels required to call
% chunkermat and generate boundary data which is appropriately scaled for the linear
% system if requested.
%
% Syntax: kerns = transmission_helper(chnkrs,ks,cs,coefs), returns a matrix of kernels
% [kerns,bdrydata] = transmission_helper(chnkrs,ks,cs,coefs,opts), returns
% [kerns, bdrydata] = transmission_helper(chnkrs,ks,cs,coefs,opts), returns
% a matrix of kernels and the boundary data due to point sources or
% waves as prescribed by opts
% [kerns, bdrydata, kerns_eval] =
% transmission_helper(chnkrs,ks,cs,coefs,opts), returns
% a matrix of kernels, the boundary data due to point sources or
% waves as prescribed by opts, and the evaluation matrix of
% kernels
%
% Note: Here we always assume that the transmission problem is either in
% the 'TE' or 'TM' polarization, which implies that the boundary conditions
Expand All @@ -22,9 +27,9 @@
% of the form
% [\beta u] = f.
%
% Input:
% Input arguments:
% chnkrs - cell array of chunker objects describing the geometry (1,ncurve)
% ks - wavenumbers in different regions of teh plane
% ks - wavenumbers in different regions of the plane
% cs - cs(1,i), cs(2,i) denote the region number in the direction of the normal
% and opposite of the direction of the normal for curve chnkrs(i)
% coefs - denotes the scaling parameter for the jump in the neumann data, depends
Expand All @@ -38,6 +43,11 @@
% array of size (1,ncurve), with opts.sources{i}.locs (2,m) locations
% of charges which generate the data for region i, and .charges are the
% corresponding charge strengths
%
% Output arguments:
% kerns - matrix of kernels for solving transmission boundary value problem
% bdrydata - boundary data
% kerns_eval - matrix of kernels for postprocessing transmission problem
%
%

Expand Down Expand Up @@ -66,6 +76,8 @@
error(msg)
end

nreg = length(ks);


bdry_data_type = 'pw';
direction = 0;
Expand Down Expand Up @@ -93,11 +105,6 @@
if(isfield(opts,'exposed_curves'))
exposed_curves = opts.exposed_curves;
end




kerns = cell(ncurve,ncurve);

k1 = ks(cs(1,:));
k2 = ks(cs(2,:));
Expand All @@ -108,7 +115,9 @@

% First build system matrix without any corner corrections
cc1 = zeros(2,2);
cc2 = zeros(2,2);
cc2 = zeros(2,2);

kerns(ncurve, ncurve) = kernel();
for i=1:ncurve

c1 = coefs(cs(1,i));
Expand All @@ -124,10 +133,12 @@

cc1(2,2) = alpha2(i)/c1;
cc2(2,2) = alpha2(i)/c2;

for j=1:ncurve
kerns{i,j} = @(s,t) -(chnk.helm2d.kern(k1(i),s,t,'all',cc1)- ...
chnk.helm2d.kern(k2(i),s,t,'all',cc2));
zks = [k1(i), k2(i)];
cc_use = cat(3, -cc1, -cc2);
kerns(i,j) = kernel('helmdiff', 'all', zks, cc_use);
% kerns{i,j} = @(s,t) -(chnk.helm2d.kern(k1(i),s,t,'all',cc1)- ...
% chnk.helm2d.kern(k2(i),s,t,'all',cc2));
end
end

Expand Down Expand Up @@ -220,8 +231,15 @@

end
varargout{1} = bdry_data;

end


if nargout > 2
kerns_eval(nreg,1) = kernel();
for i=1:nreg
Dk = coefs(i)*kernel('helm', 'd', ks(i));
Sk = kernel('helm', 's', ks(i));
kerns_eval(i) = kernel([Dk, Sk]);
end
varargout{2} = kerns_eval;
end
end
23 changes: 22 additions & 1 deletion chunkie/@kernel/helm2ddiff.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
% COEFS(1)*KERNEL.HELM2D('dp', ZKS(1)) -
% COEFS(2)*KERNEL.HELM2D('dp', ZKS(2))
%
% COEFS is optional. If not given then COEFS is set to [1 1].
% KERNEL.HELM2DDIFF('all', ZKS, coefs)
% constructs the (2x2) matrix of difference kernels of
% [D, S; D' S'] scaled by coefs where coefs is (2x2x2) tensor, i.e.
% D part is scaled as COEFS(1,1,1)*KERNEL.HELM2D('d', zks(1)) -
% COEFS(1,1,2)*KERNEL.HELM2D('d', ZKS(2)), and so on.
%
% COEFS is optional. If not given then COEFS is set to all ones.
%
% See also CHNK.HELM2D.KERN.

Expand All @@ -42,6 +48,9 @@
obj.opdims = [1 1];
if(nargin < 3)
coefs = [1 1];
if strcmpi(type, 'all')
coefs = ones(2,2,2);
end
end
obj.params.coefs = coefs;

Expand Down Expand Up @@ -87,6 +96,18 @@
obj.sing = 'hs';
end

case {'all'}
obj.type = 'all';
obj.opdims = [2,2];
obj.eval = @(s,t) chnk.helm2d.kern(zks(1), s, t, 'all', coefs(:,:,1)) - ...
chnk.helm2d.kern(zks(2), s, t, 'all', coefs(:,:,2));
obj.fmm = [];
if ( abs(coefs(2,1,1)-coefs(2,1,2)) < eps )
obj.sing = 'log';
else
obj.sing = 'hs';
end

otherwise
error('Unknown Helmholtz difference kernel type ''%s''.', type);

Expand Down
71 changes: 15 additions & 56 deletions devtools/test/chunkgrphrcipTransmissionTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
% and tests the Helmholtz transmission problem
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%clear all
clearvars
addpaths_loc();

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a = -1.0;
b = 1.0;

Expand All @@ -37,15 +33,12 @@
verts = exp(1i*2*pi*(0:4)/5);
verts = [real(verts);imag(verts)];

edge2verts = [-1, 1, 0, 0, 0; ...
0,-1, 1, 0, 0; ...
0, 0,-1, 1, 0; ...
0, 0, 0,-1, 1; ...
1, 0, 0, 0,-1];
edge2verts = sparse(edge2verts);
[~, nv] = size(verts);

edgesendverts = [1:nv; [2:nv 1]];
[~, ncurve] = size(edgesendverts);
amp = 0.5;
frq = 6;
ncurve = size(edge2verts,1);

fchnks = cell(1,ncurve);
cparams = cell(ncurve,1);
Expand All @@ -56,7 +49,7 @@
cparams{icurve}.tb = 1;
end

[cgrph] = chunkgraph(verts,edge2verts,fchnks,cparams);
[cgrph] = chunkgraph(verts, edgesendverts, fchnks, cparams);

plot(cgrph); hold on;
quiver(cgrph);
Expand All @@ -82,7 +75,8 @@
opts.charges{1} = charges{1};
opts.charges{2} = charges{2};

[kerns, bdry_data] = chnk.helm2d.transmission_helper(cgrph,ks,cs,coefs,opts);
[kerns, bdry_data, kerns_eval] = chnk.helm2d.transmission_helper(cgrph, ...
ks, cs, coefs, opts);


opts = [];
Expand All @@ -94,7 +88,7 @@

tic, dens = sysmat\bdry_data; toc;

%%
%% Postprocessing

% generate some targets...

Expand All @@ -103,55 +97,27 @@
[X,Y] = meshgrid(xs,ys);
targs = [X(:).';Y(:).'];

srcinfo = [];
srcinfo.sources = cgrph.r(:,:);
w = weights(cgrph);
n = normals(cgrph);

% a quick hack to find the interior points

srcinfo.dipstr = w(:).';
srcinfo.dipvec = n(:,:);
eps = 1E-8;
pg = 0;
pgt = 1;
[U] = lfmm2d(eps,srcinfo,pg,targs,pgt);
U = U.pottarg;

[~,nt] = size(targs);
targdomain = ones(nt,1);
inds = find(abs(U-2*pi)<pi/10);
targdomain(inds) = 2;

utarg = zeros(nt,1);
opts_eval = [];
opts_eval.forcesmooth = true;
[utarg, targdomain] = chunkerkerneval(cgrph, kerns_eval, dens, targs, ...
opts_eval);

nsys = size(sysmat,1);

srcinfo.dipstr = (coefs(1)*w(:).*dens(1:2:nsys)).';
srcinfo.charges = (w(:).*dens(2:2:nsys)).';
srcinfo.dipvec = n(:,:);
[~, nt] = size(targs);
true_sol = zeros(nt,1);

iind1 = (targdomain == 1);
fints = hfmm2d(eps,ks(1),srcinfo,pg,targs(:,iind1),pgt);
utarg(iind1) = fints.pottarg;

true_sol = zeros(nt,1);
t.r = targs(:,iind1);
s = [];
s.r = sources{1};
true_sol(iind1) = charges{1}*chnk.helm2d.kern(ks(1),s,t,'s');
true_sol(iind1) = charges{1}*chnk.helm2d.kern(ks(1), s, t, 's');


srcinfo.dipstr = (coefs(2)*w(:).*dens(1:2:nsys)).';
iind2 = (targdomain == 2);
fints = hfmm2d(eps,ks(2),srcinfo,pg,targs(:,iind2),pgt);
utarg(iind2) = fints.pottarg;
t.r = targs(:,iind2);
s.r = sources{2};
true_sol(iind2) = charges{2}*chnk.helm2d.kern(ks(2),s,t,'s');



uerr = utarg - true_sol;
uerr = reshape(uerr,size(X));
figure
Expand All @@ -160,13 +126,6 @@
plot(cgrph,'w-','LineWidth',2);
caxis([-16,0])
colorbar
%
% utarg = reshape(utarg,size(X));
% figure
% h = pcolor(X,Y,imag(utarg));
% set(h,'EdgeColor','None'); hold on;
% plot(cgrph,'w-','LineWidth',2);
% colorbar

true_sol = reshape(true_sol,size(X));
figure
Expand Down

0 comments on commit 7d53a70

Please sign in to comment.