# How to Calculate Distance Function

The calculation of the distance of all of the points in a mesh from a set of other points constitutes a useful tool in many contexts.

The Kratos implements a "elementwise" distance calculation based on the article

*Renato N. Elias, Marcos A. D. Martins, Alvaro L. G. A. Coutinho, Simple finite element-based computation of distance functions in unstructured grids*
, International Journal for Numerical Methods in Engineering,72,9,1095-1110,2007

The calculation **needs the knowledge of the NEIGHBOUR_ELEMENTS and NEIGHBOUR_NODES** and works **Only for triangles and tetrahedra**.

## Usage

A few variables are used internally and should be initialized at the beginning of the program:

fluid_model_part.AddNodalSolutionStepVariable(IS_INTERFACE) fluid_model_part.AddNodalSolutionStepVariable(IS_VISITED) fluid_model_part.AddNodalSolutionStepVariable(DISTANCE)

The user has to select a list of nodes FROM WHICH the distance will be computed. The possibility exists to assign a non-zero (even negative) distance to those nodes. The selection is performed by setting IS_VISITED=1 on the nodes. To make an example the python function

def SelectVisited(nodes): for node in nodes: if(node.X<0.0): node.SetValue(IS_VISITED,1.0) node.SetSolutionStepValue(DISTANCE,0,-1.5) else: node.SetValue(IS_VISITED,0.0)

selects as origin all of the nodes with negative abscissa and assigns a value of distance of -1.5 so that the distance starts from -1.5 and grows.