/*!
 * @param point Vector of real containing the coordinates of the point within the radius
 * @param radius Radius used to find point.
 * @param appliedCurrentVector    Vector epetra containing the applied current.
 * @param valueAppliedCurrent    Value of the current to apply at the specified point.
 * @param fullMesh   Pointer to the mesh.
 * @param Comm   EpetraMpi comunicator.
 */
template<typename Mesh> inline void appliedCurrentClosestPointWithinRadius (std::vector<Real>& point, Real Radius, boost::shared_ptr<VectorEpetra> appliedCurrentVector, Real valueAppliedCurrent,  boost::shared_ptr< Mesh > fullMesh, boost::shared_ptr<Epetra_Comm> Comm )
{
    Int n = appliedCurrentVector -> epetraVector().MyLength();

    Int ids;
    for ( UInt i (0); i < n; i++)
    {
        int iGID = appliedCurrentVector -> blockMap().GID ( static_cast<EpetraInt_Type> (i) );
        Real px = fullMesh -> point ( iGID ).x();
        Real py = fullMesh -> point ( iGID ).y();
        Real pz = fullMesh -> point ( iGID ).z();

        Real distance = std::sqrt ( ( point[0] - px) * (point[0] - px)
                                    + ( point[1] - py) * (point[1] - py)
                                    + ( point[2] - pz) * (point[2] - pz) );
        if (distance <= Radius)
        {
            ids = iGID;
            Radius = distance;
        }


    }
    Real localRadius = Radius;
    Real globalRadius (0);
    Comm->Barrier();
    Comm->MinAll (&localRadius, &globalRadius, 1);

    if (globalRadius == localRadius)
    {
        appliedCurrentVector-> operator [] ( ids ) = valueAppliedCurrent;
    }

}
/*!
 * @param point Vector of real containing the coordinates of the point within the radius
 * @param vec    Vector epetra used to recover Global id.
 * @param fullMesh   Pointer to the mesh.
 * @param Comm   EpetraMpi comunicator.
 * @return The ids of the closest point
 */
template<typename Mesh> inline UInt findClosestPoint (std::vector<Real>& point, boost::shared_ptr<VectorEpetra> vec,  boost::shared_ptr< Mesh > fullMesh, boost::shared_ptr<Epetra_Comm> Comm )
{
    Int n = vec -> epetraVector().MyLength();
    Real Radius = 100000.0;
    Int ids;
    for ( UInt i (0); i < n; i++)
    {
        Int iGID = vec -> blockMap().GID ( static_cast<EpetraInt_Type> (i) );
        Real px = fullMesh -> point ( iGID ).x();
        Real py = fullMesh -> point ( iGID ).y();
        Real pz = fullMesh -> point ( iGID ).z();

        Real distance = std::sqrt ( ( point[0] - px) * (point[0] - px)
                                    + ( point[1] - py) * (point[1] - py)
                                    + ( point[2] - pz) * (point[2] - pz) );
        if (distance <= Radius)
        {
            ids = iGID;
            Radius = distance;
        }


    }
    Real localRadius = Radius;
    Real globalRadius (0);
    Comm->Barrier();
    Comm->MinAll (&localRadius, &globalRadius, 1);
    if (globalRadius == localRadius)
    {
        return ids;
    }
    else
    {
        return -1;
    }

}