<commit_before>/****************************************************************/
/*               DO NOT MODIFY THIS HEADER                      */
/* MOOSE - Multiphysics Object Oriented Simulation Environment  */
/*                                                              */
/*           (c) 2010 Battelle Energy Alliance, LLC             */
/*                   ALL RIGHTS RESERVED                        */
/*                                                              */
/*          Prepared by Battelle Energy Alliance, LLC           */
/*            Under Contract No. DE-AC07-05ID14517              */
/*            With the U. S. Department of Energy               */
/*                                                              */
/*            See COPYRIGHT for full restrictions               */
/****************************************************************/

#include "NearestNodeThread.h"
#include "MooseMesh.h"

// libmesh includes
#include "libmesh/threads.h"

#include <cmath>

NearestNodeThread::NearestNodeThread(
    const MooseMesh & mesh, std::map<dof_id_type, std::vector<dof_id_type>> & neighbor_nodes)
  : _max_patch_percentage(0.0), _mesh(mesh), _neighbor_nodes(neighbor_nodes)
{
}

// Splitting Constructor
NearestNodeThread::NearestNodeThread(NearestNodeThread & x, Threads::split /*split*/)
  : _max_patch_percentage(x._max_patch_percentage),
    _mesh(x._mesh),
    _neighbor_nodes(x._neighbor_nodes)
{
}

/**
 * Save a patch of nodes that are close to each of the slave nodes to speed the search algorithm
 * TODO: This needs to be updated at some point in time.  If the hits into this data structure
 * approach "the end"
 * then it may be time to update
 */
void
NearestNodeThread::operator()(const NodeIdRange & range)
{
  for (const auto & node_id : range)
  {
    const Node & node = _mesh.nodeRef(node_id);

    const Node * closest_node = NULL;
    Real closest_distance = std::numeric_limits<Real>::max();

    const std::vector<dof_id_type> & neighbor_nodes = _neighbor_nodes[node_id];

    unsigned int n_neighbor_nodes = neighbor_nodes.size();

    for (unsigned int k = 0; k < n_neighbor_nodes; k++)
    {
      const Node * cur_node = &_mesh.nodeRef(neighbor_nodes[k]);
      Real distance = ((*cur_node) - node).norm();

      if (distance < closest_distance)
      {
        Real patch_percentage = static_cast<Real>(k) / static_cast<Real>(n_neighbor_nodes);

        // Save off the maximum we had to go through the patch to find the closes node
        if (patch_percentage > _max_patch_percentage)
          _max_patch_percentage = patch_percentage;

        closest_distance = distance;
        closest_node = cur_node;
      }
    }

    if (closest_distance == std::numeric_limits<Real>::max())
    {
      for (unsigned int k = 0; k < n_neighbor_nodes; k++)
      {
        const Node * cur_node = &_mesh.nodeRef(neighbor_nodes[k]);
        if (std::isnan((*cur_node)(0)) || std::isnan((*cur_node)(1)) || std::isnan((*cur_node)(2)))
          mooseError("Failure in NearestNodeThread because solution contans not-a-number entries");
      }
      mooseError("Unable to find nearest node!");
    }

    NearestNodeLocator::NearestNodeInfo & info = _nearest_node_info[node.id()];<commit_msg>Improve error message when factorization fails ref #8200<commit_after>/****************************************************************/
/*               DO NOT MODIFY THIS HEADER                      */
/* MOOSE - Multiphysics Object Oriented Simulation Environment  */
/*                                                              */
/*           (c) 2010 Battelle Energy Alliance, LLC             */
/*                   ALL RIGHTS RESERVED                        */
/*                                                              */
/*          Prepared by Battelle Energy Alliance, LLC           */
/*            Under Contract No. DE-AC07-05ID14517              */
/*            With the U. S. Department of Energy               */
/*                                                              */
/*            See COPYRIGHT for full restrictions               */
/****************************************************************/

#include "NearestNodeThread.h"
#include "MooseMesh.h"

// libmesh includes
#include "libmesh/threads.h"

#include <cmath>

NearestNodeThread::NearestNodeThread(
    const MooseMesh & mesh, std::map<dof_id_type, std::vector<dof_id_type>> & neighbor_nodes)
  : _max_patch_percentage(0.0), _mesh(mesh), _neighbor_nodes(neighbor_nodes)
{
}

// Splitting Constructor
NearestNodeThread::NearestNodeThread(NearestNodeThread & x, Threads::split /*split*/)
  : _max_patch_percentage(x._max_patch_percentage),
    _mesh(x._mesh),
    _neighbor_nodes(x._neighbor_nodes)
{
}

/**
 * Save a patch of nodes that are close to each of the slave nodes to speed the search algorithm
 * TODO: This needs to be updated at some point in time.  If the hits into this data structure
 * approach "the end"
 * then it may be time to update
 */
void
NearestNodeThread::operator()(const NodeIdRange & range)
{
  for (const auto & node_id : range)
  {
    const Node & node = _mesh.nodeRef(node_id);

    const Node * closest_node = NULL;
    Real closest_distance = std::numeric_limits<Real>::max();

    const std::vector<dof_id_type> & neighbor_nodes = _neighbor_nodes[node_id];

    unsigned int n_neighbor_nodes = neighbor_nodes.size();

    for (unsigned int k = 0; k < n_neighbor_nodes; k++)
    {
      const Node * cur_node = &_mesh.nodeRef(neighbor_nodes[k]);
      Real distance = ((*cur_node) - node).norm();

      if (distance < closest_distance)
      {
        Real patch_percentage = static_cast<Real>(k) / static_cast<Real>(n_neighbor_nodes);

        // Save off the maximum we had to go through the patch to find the closes node
        if (patch_percentage > _max_patch_percentage)
          _max_patch_percentage = patch_percentage;

        closest_distance = distance;
        closest_node = cur_node;
      }
    }

    if (closest_distance == std::numeric_limits<Real>::max())
    {
      for (unsigned int k = 0; k < n_neighbor_nodes; k++)
      {
        const Node * cur_node = &_mesh.nodeRef(neighbor_nodes[k]);
        if (std::isnan((*cur_node)(0)) || std::isinf((*cur_node)(0)) ||
            std::isnan((*cur_node)(1)) || std::isinf((*cur_node)(1)) ||
            std::isnan((*cur_node)(2)) || std::isinf((*cur_node)(2)))
          mooseError("Failure in NearestNodeThread because solution contans inf or not-a-number "
                     "entries.  This is likely due to a failed factorization of the Jacobian "
                     "matrix.");
      }
      mooseError("Unable to find nearest node!");
    }

    NearestNodeLocator::NearestNodeInfo & info = _nearest_node_info[node.id()];
{'fraction_non_alphanumeric': 0.14359276151243194, 'fraction_numerical': 0.006914815359717523, 'mean_word_length': 2.337260677466863, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3303568', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1880, 'n_words': 483}