Skip to content
Snippets Groups Projects
Commit a72676a7 authored by Ivar Stefansson's avatar Ivar Stefansson
Browse files

Replace evaluate_norm.m

parent dc411dfa
No related branches found
No related tags found
No related merge requests found
function [ E_abs2 ] = evaluate_norm( Intersectionpoints, intersectionpoints, ... function [ errorSquared ] = evaluate_norm( IntersectionPoints, intersectionPoints, ...
P, p, isvertical,segment) P, p, isVertical,endPoints)
%UNTITLED Summary of this function goes here %evaluate_norm returns L2norm^2 of the pressure solutions along a line.
% Returns L2norm of error and fine pressure, both squared. % The points IntersectionPoints and intersectionPoints all lie on the line.
ishorizontal = abs(Intersectionpoints(1,2) - Intersectionpoints(1,4))<eps; % The length of the line is determined by endPoints.
isp = intersectionpoints;
Isp = Intersectionpoints; isHorizontal = abs(IntersectionPoints(1,2) - IntersectionPoints(1,4))<eps;
%sorting the points to have smallest point first in each isp = intersectionPoints;
%intersectionpointsline Isp = IntersectionPoints;
if isvertical %sorting the points to have smallest point first in each
for c = 1:length(p) %intersectionpointsline
if isp(c,2)>isp(c,4) if isVertical
intersectionpoints(c,:) = [isp(c,3:4),isp(c,1:2)]; for c = 1:length(p)
end if isp(c,2)>isp(c,4)
end intersectionPoints(c,:) = [isp(c,3:4),isp(c,1:2)];
for C = 1:length(P) end
if Isp(C,2)>Isp(C,4) end
Intersectionpoints(C,:) = [Isp(C,3:4),Isp(C,1:2)]; for C = 1:length(P)
end if Isp(C,2)>Isp(C,4)
end IntersectionPoints(C,:) = [Isp(C,3:4),Isp(C,1:2)];
end
else end
for c = 1:length(p)
if isp(c,1)>isp(c,3) else
intersectionpoints(c,:) = [isp(c,3:4),isp(c,1:2)]; for c = 1:length(p)
end if isp(c,1)>isp(c,3)
end intersectionPoints(c,:) = [isp(c,3:4),isp(c,1:2)];
for C = 1:length(P) end
if Isp(C,1)>Isp(C,3) end
Intersectionpoints(C,:) = [Isp(C,3:4),Isp(C,1:2)]; for C = 1:length(P)
end if Isp(C,1)>Isp(C,3)
end IntersectionPoints(C,:) = [Isp(C,3:4),Isp(C,1:2)];
end
end end
ascending = sortrows([intersectionpoints,p]);
Ascending = sortrows([Intersectionpoints,P]); end
if segment(1) ~= segment(2) ascending = sortrows([intersectionPoints,p]);
if isvertical Ascending = sortrows([IntersectionPoints,P]);
first = find(ascending(:,2) >= segment(1),1); if endPoints(1) ~= endPoints(2)
last = find(ascending(:,4) <= segment(2),1,'last'); if isVertical
ascending = ascending(first:last,:); first = find(ascending(:,2) >= endPoints(3),1);
last = find(ascending(:,4) <= endPoints(4),1,'last');
First = find(Ascending(:,2) >= segment(1),1); ascending = ascending(first:last,:);
Last = find(Ascending(:,4) <= segment(2),1,'last');
Ascending = Ascending(First:Last,:); First = find(Ascending(:,2) >= endPoints(3),1);
else Last = find(Ascending(:,4) <= endPoints(4),1,'last');
first = find(ascending(:,1) >= segment(1),1); Ascending = Ascending(First:Last,:);
last = find(ascending(:,3) <= segment(2),1,'last'); else
ascending = ascending(first:last,:); first = find(ascending(:,1) >= endPoints(1),1);
last = find(ascending(:,3) <= endPoints(2),1,'last');
First = find(Ascending(:,1) >= segment(1),1); ascending = ascending(first:last,:);
Last = find(Ascending(:,3) <= segment(2),1,'last');
Ascending = Ascending(First:Last,:); First = find(Ascending(:,1) >= endPoints(1),1);
Last = find(Ascending(:,3) <= endPoints(2),1,'last');
end Ascending = Ascending(First:Last,:);
end
intersectionpoints = ascending(:,1:4); end
p = ascending(:,5); end
Intersectionpoints = Ascending(:,1:4); intersectionPoints = ascending(:,1:4);
P = Ascending(:,5); p = ascending(:,5);
IntersectionPoints = Ascending(:,1:4);
E_abs2 = 0; P = Ascending(:,5);
for C = 1:length(P) errorSquared = 0;
if isvertical
%get the [lower, upper] coarse points for C = 1:length(P)
Yc = sort ([Intersectionpoints(C,2),Intersectionpoints(C,4)]); if isVertical
%get the [lower, upper] coarse points
Yc = sort ([IntersectionPoints(C,2),IntersectionPoints(C,4)]);
larger = intersectionpoints(:,4)>Yc(1);
smaller = intersectionpoints(:,2)<Yc(2);
are_in = find(larger + smaller == 2); % all the relevant fine cells, larger = intersectionPoints(:,4)>Yc(1);
% pointing to location in intersectionpoints (and local "p" = smaller = intersectionPoints(:,2)<Yc(2);
% global "p_on") areIn = find(larger + smaller == 2); % all the relevant fine cells,
% pointing to location in intersectionpoints (and local "p" =
% only one point of the large cell on the line % global "p_on")
if isempty(are_in)
% only one point of the large cell on the line
if isempty(areIn)
% fine cell is larger (on the line) than coarse cell
elseif length(are_in) == 1
larger_length = Yc(2)-Yc(1); % fine cell is larger (on the line) than coarse cell
E_abs2 = E_abs2 + larger_length*(p(are_in)-P(C))^2; elseif length(areIn) == 1
largerLength = Yc(2)-Yc(1);
% at least two fine cells (partly) inside the coarse errorSquared = errorSquared + largerLength*(p(areIn)-P(C))^2;
else
bottom = are_in(1); % at least two fine cells (partly) inside the coarse
top = are_in(end); else
bottom_length = intersectionpoints(bottom,4) - Yc(1); bottom = areIn(1);
top_length = Yc(2) - intersectionpoints(top,2); top = areIn(end);
E_abs2 = E_abs2 + sum([bottom_length,top_length].* ... bottomLength = intersectionPoints(bottom,4) - Yc(1);
([p(bottom),p(top)]-P(C)).^2); topLength = Yc(2) - intersectionPoints(top,2);
errorSquared = errorSquared + sum([bottomLength,topLength].* ...
([p(bottom),p(top)]-P(C)).^2);
completely_in = are_in(2:end-1);
if ~isempty(completely_in) completelyIn = areIn(2:end-1);
completely_length = intersectionpoints(completely_in, 4) ... if ~isempty(completelyIn)
-intersectionpoints(completely_in,2); completelyLength = intersectionPoints(completelyIn, 4) ...
E_abs2 = E_abs2 + sum(completely_length.*(p(completely_in)-P(C)).^2); -intersectionPoints(completelyIn,2);
end errorSquared = errorSquared + sum(completelyLength.*(p(completelyIn)-P(C)).^2);
end end
end
%same cheap length evaluation applys for horizontal case
elseif ishorizontal %same cheap length evaluation applys for horizontal case
%get the [lower, upper] coarse points elseif isHorizontal
Xc = sort ([Intersectionpoints(C,1),Intersectionpoints(C,3)]); %get the [lower, upper] coarse points
Xc = sort ([IntersectionPoints(C,1),IntersectionPoints(C,3)]);
larger = intersectionpoints(:,3)>Xc(1);
smaller = intersectionpoints(:,1)<Xc(2); larger = intersectionPoints(:,3)>Xc(1);
are_in = find(larger + smaller == 2); % all the relevant fine cells, smaller = intersectionPoints(:,1)<Xc(2);
% pointing to location in intersectionpoints (and local "p" = areIn = find(larger + smaller == 2); % all the relevant fine cells,
% global "p_on") % pointing to location in intersectionpoints (and local "p" =
% global "p_on")
% only one point of the large cell on the line
if isempty(are_in) % only one point of the large cell on the line
if isempty(areIn)
% fine cell is larger (on the line) than coarse cell
elseif length(are_in) == 1 % fine cell is larger (on the line) than coarse cell
larger_length = Xc(2)-Xc(1); elseif length(areIn) == 1
E_abs2 = E_abs2 + larger_length*(p(are_in)-P(C))^2; largerLength = Xc(2)-Xc(1);
% at least two fine cells (partly) inside the coarse errorSquared = errorSquared + largerLength*(p(areIn)-P(C))^2;
else % at least two fine cells (partly) inside the coarse
bottom = are_in(1); else
top = are_in(end); bottom = areIn(1);
bottom_length = intersectionpoints(bottom,3) - Xc(1); top = areIn(end);
top_length = Xc(2) - intersectionpoints(top,1) ; bottomLength = intersectionPoints(bottom,3) - Xc(1);
E_abs2 = E_abs2 + sum([bottom_length,top_length].* ... topLength = Xc(2) - intersectionPoints(top,1) ;
([p(bottom),p(top)]-P(C)).^2); errorSquared = errorSquared + sum([bottomLength,topLength].* ...
([p(bottom),p(top)]-P(C)).^2);
completely_in = are_in(2:end-1);
if ~isempty(completely_in) completelyIn = areIn(2:end-1);
completely_length = intersectionpoints(completely_in, 3) ... if ~isempty(completelyIn)
-intersectionpoints(completely_in,1); completelyLength = intersectionPoints(completelyIn, 3) ...
E_abs2 = E_abs2 + sum(completely_length.*(p(completely_in)-P(C)).^2); -intersectionPoints(completelyIn,1);
end errorSquared = errorSquared + sum(completelyLength.*(p(completelyIn)-P(C)).^2);
end end
end
% general, non-constant line. The following code also works on horizontal,
% but not vertical lines. The computation is a bit more expensive, % general, non-constant line. The following code also works on horizontal,
% hence the "elseif ishorizontal" above % but not vertical lines. The computation is a bit more expensive,
else % hence the "elseif ishorizontal" above
%get the [lower, upper] coarse points else
Xc2 = sortrows([Intersectionpoints(C,1:2);Intersectionpoints(C,3:4)]); %get the [lower, upper] coarse points
Xc = [Xc2(1:2)]; Xc2 = sortrows([IntersectionPoints(C,1:2);IntersectionPoints(C,3:4)]);
Yc = [Xc2(3:4)]; Xc = [Xc2(1:2)];
Yc = [Xc2(3:4)];
larger = intersectionpoints(:,3)>Xc(1);
smaller = intersectionpoints(:,1)<Xc(2); larger = intersectionPoints(:,3)>Xc(1);
are_in = find(larger + smaller == 2); % all the relevant fine cells, smaller = intersectionPoints(:,1)<Xc(2);
% pointing to location in intersectionpoints (and local "p" = areIn = find(larger + smaller == 2); % all the relevant fine cells,
% global "p_on") % pointing to location in intersectionpoints (and local "p" =
% global "p_on")
% only one point of the large cell on the line
if isempty(are_in) % only one point of the large cell on the line
if isempty(areIn)
% fine cell is larger (on the line) than coarse cell
elseif length(are_in) == 1 % fine cell is larger (on the line) than coarse cell
larger_length = norm(Intersectionpoints(C,1:2) - ... elseif length(areIn) == 1
Intersectionpoints(C,3:4), 2); largerLength = norm(IntersectionPoints(C,1:2) - ...
E_abs2 = E_abs2 + larger_length*(p(are_in)-P(C))^2; IntersectionPoints(C,3:4), 2);
% at least two fine cells (partly) inside the coarse errorSquared = errorSquared + largerLength*(p(areIn)-P(C))^2;
else % at least two fine cells (partly) inside the coarse
bottom = are_in(1); else
top = are_in(end); bottom = areIn(1);
top = areIn(end);
bottom_length = norm(intersectionpoints(bottom,3:4) - ...
[Xc(1), Yc(1)], 2); bottomLength = norm(intersectionPoints(bottom,3:4) - ...
top_length = norm(intersectionpoints(top,1:2) - ... [Xc(1), Yc(1)], 2);
[Xc(2), Yc(2)], 2); topLength = norm(intersectionPoints(top,1:2) - ...
E_abs2 = E_abs2 + sum([bottom_length,top_length].* ... [Xc(2), Yc(2)], 2);
([p(bottom),p(top)]-P(C)).^2); errorSquared = errorSquared + sum([bottomLength,topLength].* ...
([p(bottom),p(top)]-P(C)).^2);
completely_in = are_in(2:end-1);
if ~isempty(completely_in) completelyIn = areIn(2:end-1);
completely_length = hypot(intersectionpoints(completely_in, 1) ... if ~isempty(completelyIn)
-intersectionpoints(completely_in,3), ... completelyLength = hypot(intersectionPoints(completelyIn, 1) ...
intersectionpoints(completely_in, 2) - ... -intersectionPoints(completelyIn,3), ...
intersectionpoints(completely_in,4)); intersectionPoints(completelyIn, 2) - ...
E_abs2 = E_abs2 + sum(completely_length.*(p(completely_in)-P(C)).^2); intersectionPoints(completelyIn,4));
end errorSquared = errorSquared + sum(completelyLength.*(p(completelyIn)-P(C)).^2);
end end
end end
end
end end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment