Raw File
find_nearest_node.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This file is part of NS2DDV.                                                      %
%                                                                                   %
% Copyright(C) 2011-2018    C. Calgaro  (caterina.calgaro@math.univ-lille1.fr)      %
%                           E. Creusé   (emmanuel.creuse@math.univ-lille1.fr)       %
%                           T. Goudon   (thierry.goudon@inria.fr)                   %
%                           A. Mouton   (alexandre.mouton@math.univ-lille1.fr)      %
%                                                                                   %
% NS2DDV is free software: you can redistribute it and/or modify it under the terms %
% of the GNU General Public License as published by the Free Software Foundation,   %
% either version 3 of the License, or (at your option) any later version.           %
%                                                                                   %
% NS2DDV is distributed in the hope that it will be useful, but WITHOUT ANY         %
% WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   %
% PARTICULAR PURPOSE. See the GNU General Public License for more details.          %
%                                                                                   %
% You should have received a copy of the GNU General Public License along with      %
% NS2DDV. If not, see <http://www.gnu.org/licenses/>.                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[px,py,ip] = find_nearest_node(x, y, MESH, PARAMETERS)

	% INPUT
	% x				x-coordinate of the studied point
	% y 			y-coordinate of the studied point
	% MESH			Mesh metadata
	% PARAMETERS	List of simulation parameters
	%
	% OUTPUT
	% px			x-coordinate of the nearest P1 node
	% py			y-coordinate of the nearest P1 node
	% ip			Index of the nearest P1 node

	switch PARAMETERS.MESH.GENERATION
		case {'NS2DDV', 'PDET'}
			% We verify if the chosen point is in the domain
			switch PARAMETERS.DOMAIN.GEOMETRY
				case {'RECTANGLE','DIHEDRON','STEP'}
					if ~((x >= PARAMETERS.DOMAIN.XMIN) && (x <= PARAMETERS.DOMAIN.XMAX) && ...
								(y >= PARAMETERS.DOMAIN.YMIN) && (y <= PARAMETERS.DOMAIN.YMAX))
						error(sprintf('The input point must be in the space domain.\n'));
					end
					dist_min = max(PARAMETERS.DOMAIN.XMAX-PARAMETERS.DOMAIN.XMIN, ...
										PARAMETERS.DOMAIN.YMAX-PARAMETERS.DOMAIN.YMIN);
				case {'CIRCLE'}
					if ((x-PARAMETERS.DOMAIN.XCENTER)^2 + (y-PARAMETERS.DOMAIN.YCENTER)^2 > PARAMETERS.DOMAIN.RADIUS^2)
						error(sprintf('The input point must be in the space domain.\n'));
					end
					dist_min = PARAMETERS.DOMAIN.RADIUS;
				otherwise
					error(sprintf('Unknown domain geometry.\n'));
			end
		case {'FROM_FILE'}
			xmax = max(MESH.p1(1,:));
			xmin = min(MESH.p1(1,:));
			ymax = max(MESH.p1(2,:));
			ymin = min(MESH.p1(2,:));
			dist_min = sqrt((xmax-xmin)^2+(ymax-ymin)^2);
		otherwise
			error(sprintf('Unknown mesh generation method.\n'));
	end

	% We look for the closest P1 node to the middle point of the domain
	ip = 1;
	for i=1:MESH.np1
		dist = sqrt((MESH.p1(1,i)-x)^2+(MESH.p1(2,i)-y)^2);
		if (dist < dist_min)
			dist_min = dist;
			ip = i;
		end
	end
	px = MESH.p1(1,ip);
	py = MESH.p1(2,ip);

end
back to top