From e30dfb447d69ae91b62f3f06f0dca8ba150f542d Mon Sep 17 00:00:00 2001 From: Dave Marchant Date: Mon, 17 Mar 2014 12:47:36 -0700 Subject: [PATCH] Added points2nodes method to meshutils --- SimPEG/Utils/__init__.py | 2 +- SimPEG/Utils/meshutils.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/SimPEG/Utils/__init__.py b/SimPEG/Utils/__init__.py index 209d1cff..f96ca68a 100644 --- a/SimPEG/Utils/__init__.py +++ b/SimPEG/Utils/__init__.py @@ -1,5 +1,5 @@ from matutils import * -from meshutils import exampleLrmGrid, meshTensors +from meshutils import exampleLrmGrid, meshTensors, points2nodes from lrmutils import volTetra, faceInfo, indexCube from interputils import interpmat from ipythonutils import easyAnimate as animate diff --git a/SimPEG/Utils/meshutils.py b/SimPEG/Utils/meshutils.py index 67d747c3..6b47d690 100644 --- a/SimPEG/Utils/meshutils.py +++ b/SimPEG/Utils/meshutils.py @@ -53,6 +53,27 @@ def meshTensors(*args): return list(tensors) if len(tensors) > 1 else tensors[0] +def points2nodes(mesh, pts): + """ + Move a list of the nearest nodes to a set of points + + :param simpeg.Mesh.TensorMesh mesh: The mesh + :param numpy.ndarray pts: Points to move} + :rtype: numpy.ndarray + :return: nodeInds + """ + + pts = np.atleast_2d(pts) + + assert mesh._meshType == 'TENSOR' + assert pts.shape[1] == mesh.dim + + nodeInds = np.empty(pts.shape[0], dtype=int) + + for i, pt in enumerate(pts): + nodeInds[i] = ((np.tile(pt, (mesh.nN,1)) - mesh.gridN)**2).sum(axis=1).argmin() + + return nodeInds if __name__ == '__main__': from SimPEG import mesh import matplotlib.pyplot as plt