https://mooseframework.inl.gov
NodePositions.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "NodePositions.h"
11 
12 #include "libmesh/parallel_algebra.h"
13 
15 
18 {
20  params.addClassDescription("Positions of element nodes.");
23 
24  // Element nodes must be sorted to remove duplicates.
25  params.suppressParameter<bool>("auto_sort");
26  params.set<bool>("auto_sort") = true;
27  // Gathered locally, should be broadcast on every process
28  params.set<bool>("auto_broadcast") = true;
29 
30  return params;
31 }
32 
34  : Positions(parameters),
35  BlockRestrictable(this),
36  BoundaryRestrictable(this, true),
37  _mesh(_subproblem.mesh())
38 {
39  // Mesh is ready at construction
40  initialize();
41  // Trigger synchronization as the initialization is distributed
42  finalize();
43 }
44 
45 void
47 {
49 
50  // Only needed for boundary restriction
51  const BoundaryInfo * binfo = nullptr;
52  if (boundaryRestricted())
53  binfo = &_mesh.getMesh().get_boundary_info();
54 
55  for (const auto & [node_id, elems_ids] : _mesh.nodeToElemMap())
56  {
57  const auto * const node = _mesh.queryNodePtr(node_id);
58  if (!node)
59  continue;
60 
61  // Check that node is part of boundary restriction
62  if (binfo)
63  {
64  if (!binfo->get_nodeset_map().count(node))
65  continue;
66  else
67  {
68  for (const auto [_, nodeset_id] : as_range(binfo->get_nodeset_map().equal_range(node)))
69  if (hasBoundary(nodeset_id))
70  goto inRestriction;
71  }
72  continue;
73  }
74  inRestriction:
75  // Check the elements associated with the node to see if they're in the block
76  // we're block-restricting. If so, add the node to the positions vector and move
77  // on to the next node (to minimize duplicates).
78  for (const auto elem_id : elems_ids)
79  {
80  const auto e = _mesh.queryElemPtr(elem_id);
81  if (e && hasBlocks(e->subdomain_id()))
82  {
83  _positions.emplace_back(_mesh.nodeRef(node_id));
84  break;
85  }
86  }
87  }
88 
89  _initialized = true;
90 }
91 
92 void
94 {
95  // Gather up the positions vector on all ranks
96  mooseAssert(initialized(false), "Positions vector has not been initialized.");
97  if (_need_broadcast)
98  // The consumer/producer reporter interface can keep track of whether a reduction is needed
99  // (for example if a consumer needs replicated data, but the producer is distributed) however,
100  // we have currently made the decision that positions should ALWAYS be replicated
101  _communicator.allgather(_positions, /* identical buffer lengths = */ false);
102 
103  // We always need to sort the positions as nodes on the boundary between different mesh partitions
104  // are duplicated. We sort by X, then Y, then Z, and prune the positions list.
105  std::sort(_positions.begin(), _positions.end());
106  _positions.erase(std::unique(_positions.begin(), _positions.end()), _positions.end());
107 
108  // Make a KDTree with the positions
109  _positions_kd_tree = std::make_unique<KDTree>(_positions, 1);
110 
111  // For now at least, we expect positions to be the same on all ranks
112  mooseAssert(comm().verify(_positions), "Positions should be the same across all MPI processes.");
113 }
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
static InputParameters validParams()
Definition: Positions.C:15
virtual bool boundaryRestricted() const
Returns true if this object has been restricted to a boundary.
Positions from nodes of elements in the mesh.
Definition: NodePositions.h:20
void clearPositions()
Clear all positions vectors.
Definition: Positions.C:154
Positions objects are under the hood Reporters.
Definition: Positions.h:20
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
MeshBase & mesh
virtual const Node * queryNodePtr(const dof_id_type i) const
Definition: MooseMesh.C:860
virtual void initialize() override
In charge of computing / loading the positions.
Definition: NodePositions.C:46
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
/class BoundaryRestrictable /brief Provides functionality for limiting the object to certain boundary...
const Parallel::Communicator & _communicator
virtual const Node & nodeRef(const dof_id_type i) const
Definition: MooseMesh.C:834
virtual Elem * queryElemPtr(const dof_id_type i)
Definition: MooseMesh.C:3125
bool _initialized
Whether the positions object has been initialized. This must be set by derived objects.
Definition: Positions.h:116
static InputParameters validParams()
void suppressParameter(const std::string &name)
This method suppresses an inherited parameter so that it isn&#39;t required or valid in the derived class...
virtual void finalize() override
In charge of reduction across all ranks & sorting for consistent output.
Definition: NodePositions.C:93
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:3448
static InputParameters validParams()
Definition: NodePositions.C:17
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
NodePositions(const InputParameters &parameters)
Definition: NodePositions.C:33
std::vector< Point > & _positions
For now, only the 1D vector will be shared across all ranks.
Definition: Positions.h:92
bool initialized(bool initial) const
Whether the positions object has been initialized.
Definition: Positions.C:262
bool _need_broadcast
Whether generation of positions is distributed or not (and therefore needs a broadcast) ...
Definition: Positions.h:108
static InputParameters validParams()
bool hasBoundary(const BoundaryName &name) const
Test if the supplied boundary name is valid for this object.
std::unique_ptr< KDTree > _positions_kd_tree
Definition: Positions.h:105
registerMooseObject("MooseApp", NodePositions)
An interface that restricts an object to subdomains via the &#39;blocks&#39; input parameter.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
bool hasBlocks(const SubdomainName &name) const
Test if the supplied block name is valid for this object.
MooseMesh & _mesh
Definition: NodePositions.h:32
const std::map< dof_id_type, std::vector< dof_id_type > > & nodeToElemMap()
If not already created, creates a map from every node to all elements to which they are connected...
Definition: MooseMesh.C:1178