www.mooseframework.org
FeatureFloodCount.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
3 /* */
4 /* All contents are licensed under LGPL V2.1 */
5 /* See LICENSE for full restrictions */
6 /****************************************************************/
7 #ifndef FEATUREFLOODCOUNT_H
8 #define FEATUREFLOODCOUNT_H
9 
10 #include "Coupleable.h"
11 #include "GeneralPostprocessor.h"
12 #include "InfixIterator.h"
13 #include "MooseVariableDependencyInterface.h"
14 #include "ZeroInterface.h"
15 
16 #include <iterator>
17 #include <list>
18 #include <set>
19 #include <vector>
20 
21 #include "libmesh/mesh_tools.h"
22 #include "libmesh/periodic_boundaries.h"
23 
24 // External includes
25 #include "bitmask_operators.h"
26 
27 // Forward Declarations
28 class FeatureFloodCount;
29 class MooseMesh;
30 class MooseVariable;
31 
32 template <>
33 InputParameters validParams<FeatureFloodCount>();
34 
43 class FeatureFloodCount : public GeneralPostprocessor,
44  public Coupleable,
45  public MooseVariableDependencyInterface,
46  public ZeroInterface
47 {
48 public:
49  FeatureFloodCount(const InputParameters & parameters);
51 
52  virtual void initialSetup() override;
53  virtual void meshChanged() override;
54 
55  virtual void initialize() override;
56  virtual void execute() override;
57  virtual void finalize() override;
58  virtual Real getValue() override;
59 
61  virtual std::size_t getTotalFeatureCount() const;
62 
64  virtual bool doesFeatureIntersectBoundary(unsigned int feature_id) const;
65 
71  virtual const std::vector<unsigned int> & getVarToFeatureVector(dof_id_type elem_id) const;
72 
74  virtual unsigned int getFeatureVar(unsigned int feature_id) const;
75 
77  std::size_t numCoupledVars() const { return _n_vars; }
78 
81  static const std::size_t invalid_size_t;
82  static const unsigned int invalid_id;
84 
86  const std::vector<MooseVariable *> & getCoupledVars() const { return _vars; }
87 
88  enum class FieldType
89  {
93  HALOS,
94  CENTROID,
96  };
97 
98  // Retrieve field information
99  virtual Real
100  getEntityValue(dof_id_type entity_id, FieldType field_type, std::size_t var_index = 0) const;
101 
102  inline bool isElemental() const { return _is_elemental; }
103 
105  enum class Status : unsigned char
106  {
107  CLEAR = 0x0,
108  MARKED = 0x1,
109  DIRTY = 0x2,
110  INACTIVE = 0x4
111  };
112 
114  {
115  public:
116  FeatureData() : FeatureData(std::numeric_limits<std::size_t>::max(), Status::INACTIVE) {}
117 
118  FeatureData(std::size_t var_index,
119  unsigned int local_index,
120  processor_id_type rank,
121  Status status)
122  : FeatureData(var_index, status)
123  {
124  _orig_ids = {std::make_pair(rank, local_index)};
125  }
126 
127  FeatureData(std::size_t var_index,
128  Status status,
129  unsigned int id = invalid_id,
130  std::vector<MeshTools::BoundingBox> bboxes = {MeshTools::BoundingBox()})
131  : _var_index(var_index),
132  _id(id),
133  _bboxes(bboxes), // Assume at least one bounding box
134  _min_entity_id(DofObject::invalid_id),
135  _vol_count(0),
136  _status(status),
137  _intersects_boundary(false)
138  {
139  }
140 
142  // Default Move constructors
143  FeatureData(FeatureData && f) = default;
144  FeatureData & operator=(FeatureData && f) = default;
146 
148 
152  void updateBBoxExtremes(MeshBase & mesh);
153  void updateBBoxExtremes(MeshTools::BoundingBox & bbox, const MeshTools::BoundingBox & rhs_bbox);
155 
160  bool boundingBoxesIntersect(const FeatureData & rhs) const;
161 
171  bool mergeable(const FeatureData & rhs) const;
172 
174 
178  bool halosIntersect(const FeatureData & rhs) const;
179  bool periodicBoundariesIntersect(const FeatureData & rhs) const;
180  bool ghostedIntersect(const FeatureData & rhs) const;
182 
187  void expandBBox(const FeatureData & rhs);
188 
193  void merge(FeatureData && rhs);
194 
195  // TODO: Doco
196  void clear();
197 
199  bool operator<(const FeatureData & rhs) const
200  {
201  if (_id != invalid_id)
202  {
203  mooseAssert(rhs._id != invalid_id, "Asymmetric setting of ids detected during sort");
204 
205  // Sort based on ids
206  return _id < rhs._id;
207  }
208  else
209  // Sort based on processor independent information (mesh and variable info)
210  return _var_index < rhs._var_index ||
211  (_var_index == rhs._var_index && _min_entity_id < rhs._min_entity_id);
212  }
213 
215  friend std::ostream & operator<<(std::ostream & out, const FeatureData & feature);
216 
218  std::set<dof_id_type> _ghosted_ids;
219 
222  std::set<dof_id_type> _local_ids;
223 
225  std::set<dof_id_type> _halo_ids;
226 
228  std::set<dof_id_type> _disjoint_halo_ids;
229 
231  std::set<dof_id_type> _periodic_nodes;
232 
234  std::size_t _var_index;
235 
237  unsigned int _id;
238 
241  std::vector<MeshTools::BoundingBox> _bboxes;
242 
244  std::list<std::pair<processor_id_type, unsigned int>> _orig_ids;
245 
247  dof_id_type _min_entity_id;
248 
250  std::size_t _vol_count;
251 
254  Point _centroid;
255 
258 
261 
262  FeatureData duplicate() const { return FeatureData(*this); }
263 
264 #ifndef __INTEL_COMPILER
265 
276  private:
277 #endif
278 
284  FeatureData(const FeatureData & f) = default;
285  FeatureData & operator=(const FeatureData & f) = default;
287  };
288 
290  const std::vector<FeatureData> & getFeatures() const { return _feature_sets; }
291 
292 protected:
298  virtual void updateFieldInfo();
299 
308  bool flood(const DofObject * dof_object, std::size_t current_index, FeatureData * feature);
309 
314  virtual Real getThreshold(std::size_t current_index) const;
315 
320  virtual Real getConnectingThreshold(std::size_t current_index) const;
321 
327  bool compareValueWithThreshold(Real entity_value, Real threshold) const;
328 
334  virtual bool isNewFeatureOrConnectedRegion(const DofObject * dof_object,
335  std::size_t & current_index,
336  FeatureData *& feature,
337  Status & status,
338  unsigned int & new_id);
339 
346  void expandPointHalos();
347 
352  void expandEdgeHalos(unsigned int num_layers_to_expand);
353 
355 
360  void visitNodalNeighbors(const Node * node,
361  std::size_t current_index,
362  FeatureData * feature,
363  bool expand_halos_only);
364  void visitElementalNeighbors(const Elem * elem,
365  std::size_t current_index,
366  FeatureData * feature,
367  bool expand_halos_only,
368  bool disjoint_only);
370 
376  template <typename T>
377  void visitNeighborsHelper(const T * curr_entity,
378  std::vector<const T *> neighbor_entities,
379  std::size_t current_index,
380  FeatureData * feature,
381  bool expand_halos_only,
382  bool topological_neighbor,
383  bool disjoint_only);
384 
396  void prepareDataForTransfer();
397 
399 
404  void serialize(std::string & serialized_buffer);
405  void deserialize(std::vector<std::string> & serialized_buffers);
407 
412  void mergeSets();
413 
419  virtual bool areFeaturesMergeable(const FeatureData & f1, const FeatureData & f2) const;
420 
425  void communicateAndMerge();
426 
430  void sortAndLabel();
431 
437  void scatterAndUpdateRanks();
438 
448  virtual void buildLocalToGlobalIndices(std::vector<std::size_t> & local_to_global_all,
449  std::vector<int> & counts) const;
450 
457  void buildFeatureIdToLocalIndices(unsigned int max_id);
458 
463  virtual void clearDataStructures();
464 
469  void appendPeriodicNeighborNodes(FeatureData & feature) const;
470 
475  void updateRegionOffsets();
476 
481  template <class InputIterator>
482  static inline bool setsIntersect(InputIterator first1,
483  InputIterator last1,
484  InputIterator first2,
485  InputIterator last2)
486  {
487  while (first1 != last1 && first2 != last2)
488  {
489  if (*first1 == *first2)
490  return true;
491 
492  if (*first1 < *first2)
493  ++first1;
494  else if (*first1 > *first2)
495  ++first2;
496  }
497  return false;
498  }
499 
500  /*************************************************
501  *************** Data Structures *****************
502  ************************************************/
503 
505  std::vector<MooseVariable *> _vars;
506 
508  const Real _threshold;
510 
515 
517  MooseMesh & _mesh;
518 
524  unsigned long _var_number;
525 
527  const bool _single_map_mode;
528 
529  const bool _condense_map_info;
530 
533  const bool _global_numbering;
534 
537  const bool _var_index_mode;
538 
540  const bool _compute_halo_maps;
541 
544 
551 
552  // Convenience variable holding the number of variables coupled into this object
553  const std::size_t _n_vars;
554 
556  const std::size_t _maps_size;
557 
559  const processor_id_type _n_procs;
560 
567  std::vector<std::set<dof_id_type>> _entities_visited;
568 
575  std::vector<std::map<dof_id_type, int>> _var_index_maps;
576 
578  std::vector<std::vector<const Elem *>> _nodes_to_elem_map;
579 
581  std::vector<unsigned int> _feature_counts_per_map;
582 
584  unsigned int _feature_count;
585 
591  std::vector<std::list<FeatureData>> _partial_feature_sets;
592 
597  std::vector<FeatureData> _feature_sets;
598 
604  std::vector<std::map<dof_id_type, int>> _feature_maps;
605 
607  std::vector<std::size_t> _local_to_global_feature_map;
608 
610  std::vector<std::size_t> _feature_id_to_local_index;
611 
613  PeriodicBoundaries * _pbs;
614 
615  std::unique_ptr<PointLocatorBase> _point_locator;
616 
618  const PostprocessorValue & _element_average_value;
619 
621  std::map<dof_id_type, int> _ghosted_entity_ids;
622 
627  std::vector<std::map<dof_id_type, int>> _halo_ids;
628 
633  std::multimap<dof_id_type, dof_id_type> _periodic_node_map;
634 
637  std::set<dof_id_type> _all_boundary_entity_ids;
638 
639  std::map<dof_id_type, std::vector<unsigned int>> _entity_var_to_features;
640 
641  std::vector<unsigned int> _empty_var_to_features;
642 
645 
648 };
649 
650 template <>
651 void dataStore(std::ostream & stream, FeatureFloodCount::FeatureData & feature, void * context);
652 template <>
653 void dataStore(std::ostream & stream, MeshTools::BoundingBox & bbox, void * context);
654 
655 template <>
656 void dataLoad(std::istream & stream, FeatureFloodCount::FeatureData & feature, void * context);
657 template <>
658 void dataLoad(std::istream & stream, MeshTools::BoundingBox & bbox, void * context);
659 
660 template <>
661 struct enable_bitmask_operators<FeatureFloodCount::Status>
662 {
663  static const bool enable = true;
664 };
665 
666 #endif // FEATUREFLOODCOUNT_H
FeatureFloodCount(const InputParameters &parameters)
void visitNeighborsHelper(const T *curr_entity, std::vector< const T * > neighbor_entities, std::size_t current_index, FeatureData *feature, bool expand_halos_only, bool topological_neighbor, bool disjoint_only)
The actual logic for visiting neighbors is abstracted out here.
virtual Real getEntityValue(dof_id_type entity_id, FieldType field_type, std::size_t var_index=0) const
Point _centroid
The centroid of the feature (average of coordinates from entities participating in the volume calcula...
std::ostream & operator<<(std::ostream &out, const FeatureFloodCount::FeatureData &feature)
std::multimap< dof_id_type, dof_id_type > _periodic_node_map
The data structure which is a list of nodes that are constrained to other nodes based on the imposed ...
const PostprocessorValue & _element_average_value
Average value of the domain which can optionally be used to find features in a field.
void expandEdgeHalos(unsigned int num_layers_to_expand)
This method expands the existing halo set by some width determined by the passed in value...
void expandPointHalos()
This method takes all of the partial features and expands the local, ghosted, and halo sets around th...
virtual Real getValue() override
const std::size_t _n_vars
void visitElementalNeighbors(const Elem *elem, std::size_t current_index, FeatureData *feature, bool expand_halos_only, bool disjoint_only)
static const std::size_t invalid_size_t
virtual void updateFieldInfo()
This method is used to populate any of the data structures used for storing field data (nodal or elem...
void serialize(std::string &serialized_buffer)
These routines packs/unpack the _feature_map data into a structure suitable for parallel communicatio...
std::set< dof_id_type > _local_ids
Holds the local ids in the interior of a feature.
virtual bool areFeaturesMergeable(const FeatureData &f1, const FeatureData &f2) const
Method for determining whether two features are mergeable.
const std::vector< FeatureData > & getFeatures() const
Return a constant reference to the vector of all discovered features.
const bool _condense_map_info
std::set< dof_id_type > _periodic_nodes
Holds the nodes that belong to the feature on a periodic boundary.
Status
This enumeration is used to indicate status of the grains in the _unique_grains data structure...
std::vector< std::set< dof_id_type > > _entities_visited
This variable keeps track of which nodes have been visited during execution.
void deserialize(std::vector< std::string > &serialized_buffers)
This routine takes the vector of byte buffers (one for each processor), deserializes them into a seri...
virtual void finalize() override
std::vector< MeshTools::BoundingBox > _bboxes
The vector of bounding boxes completely enclosing this feature (multiple used with periodic constrain...
std::map< dof_id_type, std::vector< unsigned int > > _entity_var_to_features
std::set< dof_id_type > _disjoint_halo_ids
Holds halo ids that extend onto a non-topologically connected surface.
void communicateAndMerge()
This routine handles all of the serialization, communication and deserialization of the data structur...
InputParameters validParams< FeatureFloodCount >()
virtual void initialize() override
virtual Real getThreshold(std::size_t current_index) const
Return the starting comparison threshold to use when inspecting an entity during the flood stage...
std::size_t numCoupledVars() const
Returns the number of coupled varaibles.
virtual unsigned int getFeatureVar(unsigned int feature_id) const
Returns the variable representing the passed in feature.
void visitNodalNeighbors(const Node *node, std::size_t current_index, FeatureData *feature, bool expand_halos_only)
These two routines are utility routines used by the flood routine and by derived classes for visiting...
std::map< dof_id_type, int > _ghosted_entity_ids
The map for holding reconstructed ghosted element information.
void appendPeriodicNeighborNodes(FeatureData &feature) const
This routine adds the periodic node information to our data structure prior to packing the data this ...
void sortAndLabel()
Sort and assign ids to features based on their position in the container after sorting.
dof_id_type _min_entity_id
The minimum entity seen in the _local_ids, used for sorting features.
std::vector< std::map< dof_id_type, int > > _halo_ids
The data structure for looking up halos around features.
std::vector< MooseVariable * > _vars
The vector of coupled in variables.
const Real _connecting_threshold
The threshold above (or below) which neighboring entities are flooded (where regions can be extended ...
virtual std::size_t getTotalFeatureCount() const
Returns the total feature count (active and inactive ids, useful for sizing vectors) ...
virtual void execute() override
bool _is_elemental
Determines if the flood counter is elements or not (nodes)
bool _is_master
Convenience variable for testing master rank.
std::set< dof_id_type > _all_boundary_entity_ids
The set of entities on the boundary of the domain used for determining if features intersect any boun...
std::size_t _var_index
The Moose variable where this feature was found (often the "order parameter")
bool operator<(const FeatureData &rhs) const
Comparison operator for sorting individual FeatureDatas.
virtual void clearDataStructures()
Helper routine for clearing up data structures during initialize and prior to parallel communication...
std::vector< FeatureData > _feature_sets
The data structure used to hold the globally unique features.
virtual void buildLocalToGlobalIndices(std::vector< std::size_t > &local_to_global_all, std::vector< int > &counts) const
This routine populates a stacked vector of local to global indices per rank and the associated count ...
const std::vector< MooseVariable * > & getCoupledVars() const
Returns a const vector to the coupled variable pointers.
unsigned long _var_number
This variable is used to build the periodic node map.
This object will mark nodes or elements of continuous regions all with a unique number for the purpos...
std::vector< std::vector< const Elem * > > _nodes_to_elem_map
The data structure used to find neighboring elements give a node ID.
const std::size_t _maps_size
Convenience variable holding the size of all the datastructures size by the number of maps...
const bool _use_less_than_threshold_comparison
Use less-than when comparing values against the threshold value.
const bool _global_numbering
This variable is used to indicate whether or not we identify features with unique numbers on multiple...
static const unsigned int invalid_id
void updateRegionOffsets()
This routine updates the _region_offsets variable which is useful for quickly determining the proper ...
FeatureData(std::size_t var_index, unsigned int local_index, processor_id_type rank, Status status)
FeatureData(std::size_t var_index, Status status, unsigned int id=invalid_id, std::vector< MeshTools::BoundingBox > bboxes={MeshTools::BoundingBox()})
std::vector< std::map< dof_id_type, int > > _feature_maps
The feature maps contain the raw flooded node information and eventually the unique grain numbers...
const bool _single_map_mode
This variable is used to indicate whether or not multiple maps are used during flooding.
unsigned int _feature_count
The number of features seen by this object (same as summing _feature_counts_per_map) ...
const Real _threshold
The threshold above (or below) where an entity may begin a new region (feature)
bool isElemental() const
std::unique_ptr< PointLocatorBase > _point_locator
void mergeSets()
This routine is called on the master rank only and stitches together the partial feature pieces seen ...
virtual bool isNewFeatureOrConnectedRegion(const DofObject *dof_object, std::size_t &current_index, FeatureData *&feature, Status &status, unsigned int &new_id)
Method called during the recursive flood routine that should return whether or not the current entity...
std::vector< std::list< FeatureData > > _partial_feature_sets
The data structure used to hold partial and communicated feature data.
const bool _compute_halo_maps
Indicates whether or not to communicate halo map information with all ranks.
PeriodicBoundaries * _pbs
A pointer to the periodic boundary constraints object.
bool compareValueWithThreshold(Real entity_value, Real threshold) const
This method is used to determine whether the current entity value is part of a feature or not...
virtual Real getConnectingThreshold(std::size_t current_index) const
Return the "connecting" comparison threshold to use when inspecting an entity during the flood stage...
virtual const std::vector< unsigned int > & getVarToFeatureVector(dof_id_type elem_id) const
Returns a list of active unique feature ids for a particular element.
std::vector< unsigned int > _empty_var_to_features
const processor_id_type _n_procs
Convenience variable holding the number of processors in this simulation.
void dataStore(std::ostream &stream, FeatureFloodCount::FeatureData &feature, void *context)
virtual void meshChanged() override
std::vector< unsigned int > _feature_counts_per_map
The number of features seen by this object per map.
std::vector< std::size_t > _feature_id_to_local_index
The vector recording the grain_id to local index (several indices will contain invalid_size_t) ...
std::set< dof_id_type > _halo_ids
Holds the ids surrounding the feature.
std::list< std::pair< processor_id_type, unsigned int > > _orig_ids
Original processor/local ids.
virtual bool doesFeatureIntersectBoundary(unsigned int feature_id) const
Returns a Boolean indicating whether this feature intersects any boundary.
virtual void initialSetup() override
Status _status
The status of a feature (used mostly in derived classes like the GrainTracker)
bool flood(const DofObject *dof_object, std::size_t current_index, FeatureData *feature)
This method will "mark" all entities on neighboring elements that are above the supplied threshold...
bool _intersects_boundary
Flag indicating whether this feature intersects a boundary.
MooseMesh & _mesh
A reference to the mesh.
void dataLoad(std::istream &stream, FeatureFloodCount::FeatureData &feature, void *context)
void buildFeatureIdToLocalIndices(unsigned int max_id)
This method builds a lookup map for retrieving the right local feature (by index) given a global inde...
void scatterAndUpdateRanks()
Calls buildLocalToGlobalIndices to build the individual local to global indicies for each rank and sc...
const bool _compute_var_to_feature_map
Indicates whether or not the var to feature map is populated.
std::set< dof_id_type > _ghosted_ids
Holds the ghosted ids for a feature (the ids which will be used for stitching.
const bool _var_index_mode
This variable is used to indicate whether the maps will contain unique region information or just the...
std::size_t _vol_count
The count of entities contributing to the volume calculation.
unsigned int _id
An ID for this feature.
std::vector< std::map< dof_id_type, int > > _var_index_maps
This map keeps track of which variables own which nodes.
std::vector< std::size_t > _local_to_global_feature_map
The vector recording the local to global feature indices.
void prepareDataForTransfer()
This routine uses the local flooded data to build up the local feature data structures (_feature_sets...
static bool setsIntersect(InputIterator first1, InputIterator last1, InputIterator first2, InputIterator last2)
This method detects whether two sets intersect without building a result set.