# How to get the node id in a specific coordinate in a finite element mesh?

The following code generates a finite element mesh.

Needs["NDSolveFEM"]
order = 2;
mesh = ToElementMesh[ImplicitRegion[300 < Sqrt[x x + y y], {x, y}], {{0, 1000}, {0, 1000}}, MaxCellMeasure -> 2000, "MaxBoundaryCellMeasure" -> 10,"MeshOrder" -> order, "NodeReordering" -> True]
mesh["Wireframe"]
topol = mesh["MeshElements"][[1, 1]];
nnodes = mesh["Coordinates"];


I need to get the the ids of the nodes that are located in the corners. How can i do that? • What do you need the ids for? – user21 Feb 6 '17 at 6:48

There is Position which almost does what you need out of the box, unfortunately it checks for exact equality while you need only numerical equality. On the other hand the mesh generation doesn't guarantee that the mesh nodes will be close enough to the given coordinates to make Equal consider them equal, so on my computer your solution will fail because:

Nearest[nnodes, {300, 0}]=={300,0}
(* False *)


What I would suggest is a combination of Nearest and Position:

findIds[nodes_, coords_] := Flatten[
Map[Position[nodes, First@#] &, Nearest[nodes, coords]]
]


which I think should be somewhat more robust and also a bit faster. Depending on what you are using that for, you might add some error checking...

Here is a slightly changed version that seems to be somewhat faster, at least if the numbers of nodes to find is not too large:

findIds[nodes_, coords_] := Module[{
nodestofind = Nearest[nodes, coords][[All, 1]]
},
Flatten[Position[nodes, Alternatives @@ nodestofind]]
]


As kglr has mentioned in a comment, since version 11 there is also the possibility to get the index directly from Nearest like this:

Flatten[Nearest[nodes->"Index",coords,1]]


Actually using Automatic instead of "Index" would also work, but I think "Index" is a bit clearer and unlike Automatic is documented. Unsurprisingly this is then also by far the fastet of the given alternatives. Defining a function for it would be trivial but almost seems superfluous...

• @ Albert Retey thank you. I have a similar problem in which i want to find all the ids in a specific line, how would you do that? – Diogo Feb 4 '17 at 17:35
• i have posted another question in the following link, please take a look: mathematica.stackexchange.com/questions/136961/… – Diogo Feb 4 '17 at 17:56
• (+1) Note that Nearest[nodes -> Automatic, coords, 1] returns the position of the nearest element, without the need for Position. – kglr Feb 5 '17 at 13:47
• @kglr: thanks for mentioning that new functionality of Nearest. It is always worth to go back to the documentation of newer versions, I was looking for exactly that functionality a while ago when it was not available yet :-) – Albert Retey Feb 5 '17 at 19:46

I found a solution.

  FindIds[nnodes_, coords_] := Block[{idvec, nodetofind},
idvec = {};
nodetofind = coords;
Table[
Table[
If[nnodes[[i]] == nodetofind[[j]],
AppendTo[idvec, i];
];
, {i, 1, Length[nnodes]}];
, {j, 1, Length[nodetofind]}];
idvec
];
nodetofind = {{300, 0}, {0, 300}};
{hole1, hole2} = FindIds[nnodes, nodetofind]
nodetofind = {{300, 0}, {1000, 0}};
{inferiorline1, inferiorline2} = FindIds[nnodes, nodetofind]
nodetofind = {{0, 300}, {0, 1000}};
{leftline1, leftline2} = FindIds[nnodes, nodetofind]