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.");
22 
23  // Element nodes must be sorted to remove duplicates.
24  params.suppressParameter<bool>("auto_sort");
25  params.set<bool>("auto_sort") = true;
26  // Gathered locally, should be broadcast on every process
27  params.set<bool>("auto_broadcast") = true;
28 
29  return params;
30 }
31 
33  : Positions(parameters), BlockRestrictable(this), _mesh(_fe_problem.mesh())
34 {
35  // Mesh is ready at construction
36  initialize();
37  // Trigger synchronization as the initialization is distributed
38  finalize();
39 }
40 
41 void
43 {
45 
46  for (const auto & [node_id, elems_ids] : _fe_problem.mesh().nodeToElemMap())
47  {
48  if (!_mesh.queryNodePtr(node_id))
49  continue;
50 
51  // Check the elements associated with the node to see if they're in the block
52  // we're block-restricting. If so, add the node to the positions vector and move
53  // on to the next node (to minimize duplicates).
54  for (const auto elem_id : elems_ids)
55  {
56  const auto e = _mesh.queryElemPtr(elem_id);
57  if (e && hasBlocks(e->subdomain_id()))
58  {
59  _positions.emplace_back(_mesh.nodeRef(node_id));
60  break;
61  }
62  }
63  }
64 
65  _initialized = true;
66 }
67 
68 void
70 {
71  // Gather up the positions vector on all ranks
72  mooseAssert(initialized(false), "Positions vector has not been initialized.");
73  if (_need_broadcast)
74  // The consumer/producer reporter interface can keep track of whether a reduction is needed
75  // (for example if a consumer needs replicated data, but the producer is distributed) however,
76  // we have currently made the decision that positions should ALWAYS be replicated
77  _communicator.allgather(_positions, /* identical buffer lengths = */ false);
78 
79  // We always need to sort the positions as nodes on the boundary between different mesh partitions
80  // are duplicated. We sort by X, then Y, then Z, and prune the positions list.
81  std::sort(_positions.begin(), _positions.end());
82  _positions.erase(std::unique(_positions.begin(), _positions.end()), _positions.end());
83 
84  // Make a KDTree with the positions
85  _positions_kd_tree = std::make_unique<KDTree>(_positions, 1);
86 
87  // For now at least, we expect positions to be the same on all ranks
88  mooseAssert(comm().verify(_positions), "Positions should be the same across all MPI processes.");
89 }
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
static InputParameters validParams()
Definition: Positions.C:15
Positions from nodes of elements in the mesh.
Definition: NodePositions.h:19
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:857
virtual void initialize() override
In charge of computing / loading the positions.
Definition: NodePositions.C:42
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
const Parallel::Communicator & _communicator
virtual const Node & nodeRef(const dof_id_type i) const
Definition: MooseMesh.C:831
virtual Elem * queryElemPtr(const dof_id_type i)
Definition: MooseMesh.C:3120
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:69
static InputParameters validParams()
Definition: NodePositions.C:17
NodePositions(const InputParameters &parameters)
Definition: NodePositions.C:32
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
std::unique_ptr< KDTree > _positions_kd_tree
Definition: Positions.h:105
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
Definition: UserObject.h:211
const MooseMesh & _mesh
Definition: NodePositions.h:31
registerMooseObject("MooseApp", NodePositions)
An interface that restricts an object to subdomains via the &#39;blocks&#39; input parameter.
virtual MooseMesh & mesh() override
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.
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:1175