Sie sind auf Seite 1von 7

function p_i = discGreen(n_panels)

% function p_i = discGreen(n_panels) computes the contribution of the


% surface panels in a system using the discretised Green's function.
% n_panels is the number of panels per side. (same value for body and far
% field)
% Run in a loop to test convergence.
% Written by Nikita Roseann Pinto oe17s024

disp(n_panels) % Display the number of panels per side

x = linspace(-1,1,n_panels);
y = x;
z = x;
face = [x(1) x(end)];
n = 1;
area_b = (x(2) - x(1)) ^ 2;

%% Body - cube
% Top and bottom face

for k = 1:length(face)
for i = 1:length(x)
for j = 1:length(y)
pos_b(n,:) = [x(i) y(j) face(k)];
nor_b(n,:) = [0 0 k];
u(n,:) = 0;
n = n+1;
end
end
end

% Left and right face

for i = 1:length(face)
for k = 1:length(z)
for j = 1:length(y)
pos_b(n,:) = [face(i) y(j) z(k)];
nor_b(n,:) = [i 0 0];
u(n,:) = i;
n = n+1;
end
end
end

% Front and back


for j = 1:length(face)
for i = 1:length(x)
for k = 1:length(z)
pos_b(n,:) = [x(i) face(j) z(k)];
nor_b(n,:) = [0 j 0];
u(n,:) = 0;
n = n+1;
end
end
end

nor_b(nor_b == 1) = 1;
nor_b(nor_b == 2) = -1;
u(u == 1) = -0.015;
u(u == 2) = 0.01;

%% Free surface

x = linspace(-10,10,n_panels);
y = x;
z = x;
face = [x(1) x(end)];
n = 1;
area_sig = (x(2) - x(1)) ^ 2;

for k = 1:length(face)
for i = 1:length(x)
for j = 1:length(y)
pos_sig(n,:) = [x(i) y(j) face(k)];
nor_sig(n,:) = [0 0 k];
% u(n,:) = 0;
n = n+1;
end
end
end

% Left and right face

for i = 1:length(face)
for k = 1:length(z)
for j = 1:length(y)
pos_sig(n,:) = [face(i) y(j) z(k)];
nor_sig(n,:) = [i 0 0];
% u(n,:) = i;
n = n+1;
end
end
end

% Front and back


for j = 1:length(face)
for i = 1:length(x)
for k = 1:length(z)
pos_sig(n,:) = [x(i) face(j) z(k)];
nor_sig(n,:) = [0 j 0];
% u(n,:) = 0;
n = n+1;
end
end
end

nor_sig(nor_sig == 1) = -1;
nor_sig(nor_sig == 2) = 1;

%% Combine over all panels


pos = [pos_b ; pos_sig];
n_points = size(pos,1);
n_points_b = 1:size(pos_b,1);
n_points_sig = n_points_b(end)+1:n_points_b(end)+size(pos_sig,1);

%% Compute the r and exponent vectors for ALL surface points (b and sig)

s_idx = 1:n_points;
r_idx = s_idx;
r_mat = nan(s_idx(end));
for i = 1:size(s_idx,2)
for j = 1:size(r_idx,2)
r_mat(i,j) = sqrt((pos(i,1)-pos(j,1))^2+(pos(i,2)-pos(j,2))^2 ...
+ (pos(i,3)-pos(j,3))^2);
end
end

k = 2000*pi/1500; % k = sigma/speed_sound

exp_mat = (1j*k*r_mat);
G_mat = exp_mat .* r_mat;
%% LHS - Term 1
source_mat = 2*pi*diag(ones(1,n_points));

term1 = source_mat;

%% LHS - Term 2

% dG1 = nan(n_points); %Idea was to preallocate memory,


% dG2 = nan(n_points); % but this taxes the RAM and increases
% dG3 = nan(n_points); % computational time.
% dG_b = nan(n_points,3);

count = 0;

for j = n_points_b
for i = 1:n_points
count = count + 1;
if j==i
continue;
end

dG_all = -exp_mat(i,j) * (r_mat(i,j)*1j*k - 1)/...


(r_mat(i,j)^3);
dG1(i,j) = dG_all * (pos(j,1) - pos(i,1));
dG2(i,j) = dG_all * (pos(j,2) - pos(i,2));
dG3(i,j) = dG_all * (pos(j,3) - pos(i,3));

end

end

sum_dG1 = sum(dG1,2,'omitnan')*area_b;
sum_dG2 = sum(dG2,2,'omitnan')*area_b;
sum_dG3 = sum(dG3,2,'omitnan')*area_b;
dG_b = [sum_dG1 sum_dG2 sum_dG3];

term2 = dG_b * nor_b';

%% LHS - Term 3
%
% dG1 = nan(n_points);
% dG2 = nan(n_points);
% dG3 = nan(n_points);
% dG_sig = nan(n_points,3);
count = 0;

for j = n_points_sig
for i = 1:n_points
count = count + 1;
if j==i
continue;
end

dG_all = -exp_mat(i,j) * (r_mat(i,j)*1j*k - 1)/...


(r_mat(i,j)^3);
dG1(i,j) = dG_all * (pos(j,1) - pos(i,1));
dG2(i,j) = dG_all * (pos(j,2) - pos(i,2));
dG3(i,j) = dG_all * (pos(j,3) - pos(i,3));

end

end

sum_dG1 = sum(dG1,2,'omitnan')*area_sig;
sum_dG2 = sum(dG2,2,'omitnan')*area_sig;
sum_dG3 = sum(dG3,2,'omitnan')*area_sig;
dG_sig = [sum_dG1 sum_dG2 sum_dG3];

term3 = dG_sig * nor_sig';

%% LHS - Term 4

for j = 1:size(n_points_sig,2)
for i = n_points

if j == i
continue;
end

ikG(i,j) = 1j * k * G_mat(i,j);

end
end
term4 = ikG;

%% Combining across all LHS matrices.

% Choose variable A, because of linear system of equaitons representation

A = term1 + [term2 (term3-term4)];

%% RHS

rho = 1000;
sigma = 2000*pi;
dp_rhs = 1j*rho*sigma*u; %or ones(1,n_points) * uo
G_rhs = G_mat(:,n_points_sig);
term_rhs = G_rhs * dp_rhs * area_b;

% Choose variable b, bc linear system of equations


b = term_rhs;

%% Solution to system of linear equations by simple inverse


% Ax = b; x=A^-1 b;

x = inv(A) * b; % this seems faster than the other option


% x = A\b; % although Matlab suggests A\b for accuracy.
p = x;

%% Select the right index values for results


% Points are (i) [-1, 0.5, 0.5] (ii) [1 0.5 0.5]
% (iii) [-10,0.5,0.5] (iv) [10,0.5,0.5]

P1 = [-1, 0.5, 0.5];


P2 = [1 0.5 0.5];
P3 = [-10,0.5,0.5];
P4 = [10,0.5,0.5];
P = [P1; P2; P3; P4];

% Find the closest panel centroid to the selected points.


for i = 1:size(P,1)
diffr(i,:) = sqrt((P(i,1)-pos(:,1)).^2 +(P(i,2)-pos(:,2)).^2 ...
+(P(i,3)-pos(:,3)).^2);
end

[~,b] = min(diffr,[],2); % Find min along dimension 2 to isolate closest

p_i = p(b);
disp(n_points) % Display the total number of surface panels in the whole
system
end

Das könnte Ihnen auch gefallen