コード例 #1
0
ファイル: GrainTracker.C プロジェクト: fdkong/moose
bool
GrainTracker::attemptGrainRenumber(MooseSharedPointer<FeatureData> grain, unsigned int grain_id, unsigned int depth, unsigned int max)
{
  // End the recursion of our breadth first search
  if (depth > max)
    return false;

  unsigned int curr_var_idx = grain->_var_idx;

  std::map<Node *, CacheValues> cache;

  std::vector<std::list<GrainDistance> > min_distances(_vars.size());

  /**
   * We have two grains that are getting close represented by the same order parameter.
   * We need to map to the variable whose closest grain to this one is furthest away by sphere to sphere distance.
   */
  computeMinDistancesFromGrain(grain, min_distances);

  /**
   * We have a vector of the distances to the closest grains represented by each of our variables.  We just need to pick
   * a suitable grain to replace with.  We will start with the maximum of this this list: (max of the mins), but will settle
   * for next to largest and so forth as we make more attempts at remapping grains.  This is a graph coloring problem so
   * more work will be required to optimize this process.
   *
   * Note: We don't have an explicit check here to avoid remapping a  variable to itself.  This is unnecessary since the
   * min_distance of a variable is explicitly set up above.
   */

  std::sort(min_distances.begin(), min_distances.end(), GrainDistanceSorter());

  _console << "\n********************************************\nDistances list for grain " << grain_id << '\n';
  for (unsigned int i = 0; i < min_distances.size(); ++i)
  {
    for (std::list<GrainDistance>::iterator min_it = min_distances[i].begin(); min_it != min_distances[i].end(); ++min_it)
      _console << min_it->_distance << ": " << min_it->_grain_id << ": " <<  min_it->_var_index << '\n';
    _console << '\n';
  }

  for (unsigned int i = 0; i < min_distances.size(); ++i)
  {
    std::list<GrainDistance>::const_iterator target_it = min_distances[i].begin();
    if (target_it == min_distances[i].end())
      continue;

    // If the distance is positive we can just remap and be done
    if (target_it->_distance > 0)
    {
      Moose::out
        << COLOR_GREEN
        << "- Depth " << depth << ": Remapping grain #" << grain_id << " from variable index " << curr_var_idx
        << " to " << target_it->_var_index << " whose closest grain (#" << target_it->_grain_id
        << ") is at a distance of " << target_it->_distance << "\n"
        << COLOR_DEFAULT;

      swapSolutionValues(grain, target_it->_var_index, cache, BYPASS, depth);
      return true;
    }

    // If the distance isn't positive we just need to make sure that none of the grains represented by the
    // target variable index would intersect this one if we were to remap
    std::list<GrainDistance>::const_iterator next_target_it = target_it;
    bool intersection_hit = false;
    std::ostringstream oss;
    while (!intersection_hit && next_target_it != min_distances[i].end())
    {
      if (next_target_it->_distance > 0)
        break;

      mooseAssert(_unique_grains.find(next_target_it->_grain_id) != _unique_grains.end(), "Error in indexing target grain in attemptGrainRenumber");
      MooseSharedPointer<FeatureData> next_target_grain = _unique_grains[next_target_it->_grain_id];

      // If any grains touch we're done here
      if (setsIntersect(grain->_halo_ids.begin(), grain->_halo_ids.end(),
                        next_target_grain->_halo_ids.begin(), next_target_grain->_halo_ids.end()))
        intersection_hit = true;
      else
        oss << " #" << next_target_it->_grain_id;

      ++next_target_it;
    }

    if (!intersection_hit)
    {
      Moose::out
        << COLOR_GREEN
        << "- Depth " << depth << ": Remapping grain #" << grain_id << " from variable index " << curr_var_idx
        << " to " << target_it->_var_index << " whose closest grain(s):" << oss.str()
        << " are inside our bounding sphere but whose halo(s) are not touching.\n"
        << COLOR_DEFAULT;

      swapSolutionValues(grain, target_it->_var_index, cache, BYPASS, depth);
      return true;
    }

    // If we reach this part of the loop, there is no simple renumbering that can be done.
    mooseAssert(_unique_grains.find(target_it->_grain_id) != _unique_grains.end(), "Error in indexing target grain in attemptGrainRenumber");
    MooseSharedPointer<FeatureData> target_grain = _unique_grains[target_it->_grain_id];

    // Make sure this grain isn't marked. If it is, we can't recurse here
    if (target_grain->_merged)
      return false;

    // Save the solution values in case we overright them during recursion
    swapSolutionValues(grain, target_it->_var_index, cache, FILL, depth);

    // TODO: Make sure this distance is -1 or higher or fine intersections only exist for a single variable
    // Propose a new variable index for the current grain and recurse
    grain->_var_idx = target_it->_var_index;
    grain->_merged = true;
    if (attemptGrainRenumber(target_grain, target_it->_grain_id, depth+1, max))
    {
      // SUCCESS!
      Moose::out
        << COLOR_GREEN
        << "- Depth " << depth << ": Remapping grain #" << grain_id << " from variable index " << curr_var_idx
        << " to " << target_it->_var_index << '\n'
        << COLOR_DEFAULT;

      // NOTE: swapSolutionValues currently reads the current variable index off the grain. We need to set
      //       back here before calling this method.
      grain->_var_idx = curr_var_idx;
      swapSolutionValues(grain, target_it->_var_index, cache, USE, depth);

      return true;
    }
    else
      // Need to set our var index back after failed recursive step
      grain->_var_idx = curr_var_idx;

    // Always "unmark" the grain after the recursion so it can be used by other remap operations
    grain->_merged = false;
  }

  return false;
}
コード例 #2
0
ファイル: GrainTracker.C プロジェクト: rppawlo/moose
void
GrainTracker::swapSolutionValues(std::map<unsigned int, UniqueGrain *>::iterator & grain_it1,
                                 std::map<unsigned int, UniqueGrain *>::iterator & grain_it2,
                                 unsigned int attempt_number)
{
  NumericVector<Real> & solution         =  _nl.solution();
  NumericVector<Real> & solution_old     =  _nl.solutionOld();
  NumericVector<Real> & solution_older   =  _nl.solutionOlder();

  unsigned int curr_var_idx = grain_it1->second->variable_idx;
  /**
   * We have two grains that are getting close represented by the same order parameter.
   * We need to map to the variable whose closest grain to this one is furthest away by sphere to sphere distance.
   */
  std::vector<Real> min_distances(_vars.size(), std::numeric_limits<Real>::max());

  // Make sure that we don't attempt to remap to the same variable
  min_distances[curr_var_idx] = -std::numeric_limits<Real>::max();

  for (std::map<unsigned int, UniqueGrain *>::iterator grain_it3 = _unique_grains.begin();
       grain_it3 != _unique_grains.end(); ++grain_it3)
  {
    if (grain_it3->second->status == INACTIVE || grain_it3->second->variable_idx == curr_var_idx)
      continue;

    unsigned int potential_var_idx = grain_it3->second->variable_idx;

    Real curr_bounding_sphere_diff = boundingRegionDistance(grain_it1->second->sphere_ptrs, grain_it3->second->sphere_ptrs, false);
    if (curr_bounding_sphere_diff < min_distances[potential_var_idx])
      min_distances[potential_var_idx] = curr_bounding_sphere_diff;
  }

  /**
   * We have a vector of the distances to the closest grains represented by each of our variables.  We just need to pick
   * a suitable grain to replace with.  We will start with the maximum of this this list: (max of the mins), but will settle
   * for next to largest and so forth as we make more attempts at remapping grains.  This is a graph coloring problem so
   * more work will be required to optimize this process.
   * Note: We don't have an explicit check here to avoid remapping a  variable to itself.  This is unecessary since the
   * min_distance of a variable is explicitly set up above.
   */
  unsigned int nth_largest_idx = min_distances.size() - attempt_number - 1;

  // nth element destroys the original array so we need to copy it first
  std::vector<Real> min_distances_copy(min_distances);
  std::nth_element(min_distances_copy.begin(), min_distances_copy.end()+nth_largest_idx, min_distances_copy.end());

  // Now find the location of the nth element in the original vector
  unsigned int new_variable_idx = std::distance(min_distances.begin(),
                                                std::find(min_distances.begin(),
                                                          min_distances.end(),
                                                          min_distances_copy[nth_largest_idx]));

  Moose::out
    << COLOR_YELLOW
    << "Grain #: " << grain_it1->first << " intersects Grain #: " << grain_it2->first
    << " (variable index: " << grain_it1->second->variable_idx << ")\n"
    << COLOR_DEFAULT;

  if (min_distances[new_variable_idx] < 0)
  {
    Moose::out
      << COLOR_YELLOW
      << "*****************************************************************************************************\n"
      << "Warning: No suitable variable found for remapping. Will attempt to remap in next loop if necessary...\n"
      << "*****************************************************************************************************\n"
      << COLOR_DEFAULT;
    return;
  }

  Moose::out
    << COLOR_GREEN
    << "Remapping to: " << new_variable_idx << " whose closest grain is at a distance of " << min_distances[new_variable_idx] << "\n"
    << COLOR_DEFAULT;

  MeshBase & mesh = _mesh.getMesh();

  // Remap the grain
  std::set<Node *> updated_nodes_tmp; // Used only in the elemental case
  for (std::set<dof_id_type>::const_iterator entity_it = grain_it1->second->entities_ptr->begin();
       entity_it != grain_it1->second->entities_ptr->end(); ++entity_it)
  {
    if (_is_elemental)
    {
      Elem *elem = mesh.query_elem(*entity_it);
      if (!elem)
        continue;

      for (unsigned int i=0; i < elem->n_nodes(); ++i)
      {
        Node *curr_node = elem->get_node(i);
        if (updated_nodes_tmp.find(curr_node) == updated_nodes_tmp.end())
        {
          updated_nodes_tmp.insert(curr_node);         // cache this node so we don't attempt to remap it again within this loop
          swapSolutionValuesHelper(curr_node, curr_var_idx, new_variable_idx, solution, solution_old, solution_older);
        }
      }
    }
    else
      swapSolutionValuesHelper(mesh.query_node_ptr(*entity_it), curr_var_idx, new_variable_idx, solution, solution_old, solution_older);
  }

  // Update the variable index in the unique grain datastructure
  grain_it1->second->variable_idx = new_variable_idx;

  // Close all of the solution vectors
  solution.close();
  solution_old.close();
  solution_older.close();

  _fe_problem.getNonlinearSystem().sys().update();

}