www.mooseframework.org
FauxGrainTracker.C
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 
8 #include "FauxGrainTracker.h"
9 
10 // MOOSE includes
11 #include "MooseMesh.h"
12 #include "MooseVariable.h"
13 
14 template <>
15 InputParameters
17 {
18  InputParameters params = validParams<GrainTrackerInterface>();
19  params.addClassDescription("Fake grain tracker object for cases where the number of grains is "
20  "equal to the number of order parameters.");
21 
22  return params;
23 }
24 
25 FauxGrainTracker::FauxGrainTracker(const InputParameters & parameters)
26  : FeatureFloodCount(parameters),
28  _grain_count(0),
29  _n_vars(_vars.size()),
30  _tracking_step(getParam<int>("tracking_step"))
31 {
32  // initialize faux data with identity map
33  _op_to_grains.resize(_n_vars);
34  for (auto i = beginIndex(_op_to_grains); i < _op_to_grains.size(); ++i)
35  _op_to_grains[i] = i;
36 
38 }
39 
41 
42 Real
43 FauxGrainTracker::getEntityValue(dof_id_type entity_id,
45  std::size_t var_idx) const
46 {
47  if (var_idx == FeatureFloodCount::invalid_size_t)
48  var_idx = 0;
49 
50  mooseAssert(var_idx < _n_vars, "Index out of range");
51 
52  switch (field_type)
53  {
56  {
57  auto entity_it = _entity_id_to_var_num.find(entity_id);
58 
59  if (entity_it != _entity_id_to_var_num.end())
60  return entity_it->second;
61  else
62  return -1;
63  break;
64  }
65 
67  {
68  if (_periodic_node_map.size())
69  mooseDoOnce(mooseWarning(
70  "Centroids are not correct when using periodic boundaries, contact the MOOSE team"));
71 
72  // If this element contains the centroid of one of features, return it's index
73  const auto * elem_ptr = _mesh.elemPtr(entity_id);
74  for (auto var_num = beginIndex(_vars); var_num < _n_vars; ++var_num)
75  {
76  const auto centroid = _centroid.find(var_num);
77  if (centroid != _centroid.end())
78  if (elem_ptr->contains_point(centroid->second))
79  return 1;
80  }
81 
82  return 0;
83  }
84 
85  // We don't want to error here because this should be a drop in replacement for the real grain
86  // tracker.
87  // Instead we'll just return zero and continue
88  default:
89  return 0;
90  }
91 
92  return 0;
93 }
94 
95 const std::vector<unsigned int> &
96 FauxGrainTracker::getVarToFeatureVector(dof_id_type elem_id) const
97 {
98  const auto pos = _entity_var_to_features.find(elem_id);
99  if (pos != _entity_var_to_features.end())
100  {
101  mooseAssert(pos->second.size() == _n_vars, "Variable to feature vector not sized properly");
102  return pos->second;
103  }
104  else
105  return _empty_var_to_features;
106 }
107 
108 unsigned int
109 FauxGrainTracker::getFeatureVar(unsigned int feature_id) const
110 {
111  return feature_id;
112 }
113 
114 std::size_t
116 {
117  return _variables_used.size();
118 }
119 
120 std::size_t
122 {
123  return _grain_count;
124 }
125 
126 Point
127 FauxGrainTracker::getGrainCentroid(unsigned int grain_index) const
128 {
129  const auto grain_center = _centroid.find(grain_index);
130  mooseAssert(grain_center != _centroid.end(),
131  "Grain " << grain_index << " does not exist in data structure");
132 
133  return grain_center->second;
134 }
135 
136 void
138 {
139  _entity_id_to_var_num.clear();
140  _entity_var_to_features.clear();
141  _variables_used.clear();
142  if (_is_elemental)
143  {
144  _volume.clear();
145  _vol_count.clear();
146  _centroid.clear();
147  }
148 }
149 
150 void
152 {
153  Moose::perf_log.push("execute()", "FauxGrainTracker");
154 
155  const MeshBase::element_iterator end = _mesh.getMesh().active_local_elements_end();
156  for (MeshBase::element_iterator el = _mesh.getMesh().active_local_elements_begin(); el != end;
157  ++el)
158  {
159  const Elem * current_elem = *el;
160 
161  // Loop over elements or nodes and populate the data structure with the first variable with a
162  // value above a threshold
163  if (_is_elemental)
164  {
165  std::vector<Point> centroid(1, current_elem->centroid());
166  _fe_problem.reinitElemPhys(current_elem, centroid, 0);
167 
168  auto entity = current_elem->id();
169  auto insert_pair =
170  moose_try_emplace(_entity_var_to_features,
171  entity,
172  std::vector<unsigned int>(_n_vars, FeatureFloodCount::invalid_id));
173  auto & vec_ref = insert_pair.first->second;
174 
175  for (auto var_num = beginIndex(_vars); var_num < _n_vars; ++var_num)
176  {
177  auto entity_value = _vars[var_num]->sln()[0];
178 
179  if ((_use_less_than_threshold_comparison && (entity_value >= _threshold)) ||
180  (!_use_less_than_threshold_comparison && (entity_value <= _threshold)))
181  {
182  _entity_id_to_var_num[current_elem->id()] = var_num;
183  _variables_used.insert(var_num);
184  _volume[var_num] += current_elem->volume();
185  _vol_count[var_num]++;
186  // Sum the centroid values for now, we'll average them later
187  _centroid[var_num] += current_elem->centroid();
188  vec_ref[var_num] = var_num;
189  break;
190  }
191  }
192  }
193  else
194  {
195  unsigned int n_nodes = current_elem->n_vertices();
196  for (unsigned int i = 0; i < n_nodes; ++i)
197  {
198  const Node * current_node = current_elem->get_node(i);
199 
200  for (auto var_num = beginIndex(_vars); var_num < _n_vars; ++var_num)
201  {
202  auto entity_value = _vars[var_num]->getNodalValue(*current_node);
203  if ((_use_less_than_threshold_comparison && (entity_value >= _threshold)) ||
204  (!_use_less_than_threshold_comparison && (entity_value <= _threshold)))
205  {
206  _entity_id_to_var_num[current_node->id()] = var_num;
207  _variables_used.insert(var_num);
208  break;
209  }
210  }
211  }
212  }
213  }
214 
215  _grain_count = std::max(_grain_count, _variables_used.size());
216 
217  Moose::perf_log.pop("execute()", "FauxGrainTracker");
218 }
219 
220 void
222 {
223  Moose::perf_log.push("finalize()", "FauxGrainTracker");
224 
225  _communicator.set_union(_variables_used);
226  _communicator.set_union(_entity_id_to_var_num);
227 
228  if (_is_elemental)
229  for (auto var_num = beginIndex(_vars); var_num < _n_vars; ++var_num)
230  {
235  unsigned int vol_count;
236  std::vector<Real> grain_data(4);
237 
238  const auto count = _vol_count.find(var_num);
239  if (count != _vol_count.end())
240  vol_count = count->second;
241 
242  const auto vol = _volume.find(var_num);
243  if (vol != _volume.end())
244  grain_data[0] = vol->second;
245 
246  const auto centroid = _centroid.find(var_num);
247  if (centroid != _centroid.end())
248  {
249  grain_data[1] = centroid->second(0);
250  grain_data[2] = centroid->second(1);
251  grain_data[3] = centroid->second(2);
252  }
253  // combine centers & volumes from all MPI ranks
254  gatherSum(vol_count);
255  gatherSum(grain_data);
256  _volume[var_num] = grain_data[0];
257  _centroid[var_num] = {grain_data[1], grain_data[2], grain_data[3]};
258  _centroid[var_num] /= vol_count;
259  }
260 
261  Moose::perf_log.pop("finalize()", "FauxGrainTracker");
262 }
263 
264 Real
266 {
267  return static_cast<Real>(_variables_used.size());
268 }
269 
270 bool
271 FauxGrainTracker::doesFeatureIntersectBoundary(unsigned int /*feature_id*/) const
272 {
273  mooseDoOnce(mooseWarning("FauxGrainTracker::doesFeatureIntersectboundary() is unimplemented"));
274 
275  return false;
276 }
virtual ~FauxGrainTracker()
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 ...
std::vector< unsigned int > _empty_var_to_features
This class defines the interface for the GrainTracking objects.
static const std::size_t invalid_size_t
std::map< dof_id_type, std::vector< unsigned int > > _entity_var_to_features
virtual void finalize() override
virtual std::size_t getNumberActiveGrains() const override
Returns the number of active grains current stored in the GrainTracker.
std::vector< unsigned int > _op_to_grains
Order parameter to grain indices (just a reflexive vector)
FauxGrainTracker(const InputParameters &parameters)
virtual std::size_t getTotalFeatureCount() const override
Returns the total feature count (active and inactive ids, useful for sizing vectors) ...
std::map< unsigned int, unsigned int > _vol_count
The count of entities contributing to the volume calculation.
virtual unsigned int getFeatureVar(unsigned int feature_id) const override
Returns the variable representing the passed in feature.
std::set< unsigned int > _variables_used
Used as the lightweight grain counter.
virtual const std::vector< unsigned int > & getVarToFeatureVector(dof_id_type elem_id) const override
Returns a list of active unique feature ids for a particular element.
std::vector< MooseVariable * > _vars
The vector of coupled in variables.
virtual void initialize() override
bool _is_elemental
Determines if the flood counter is elements or not (nodes)
This object will mark nodes or elements of continuous regions all with a unique number for the purpos...
const bool _use_less_than_threshold_comparison
Use less-than when comparing values against the threshold value.
static const unsigned int invalid_id
InputParameters validParams< GrainTrackerInterface >()
std::map< dof_id_type, unsigned int > _entity_id_to_var_num
The mapping of entities to grains, in this case always the order parameter.
const Real _threshold
The threshold above (or below) where an entity may begin a new region (feature)
virtual void execute() override
virtual Real getEntityValue(dof_id_type entity_id, FeatureFloodCount::FieldType field_type, std::size_t var_idx) const override
virtual Real getValue() override
virtual Point getGrainCentroid(unsigned int grain_id) const override
Returns the centroid for the given grain number.
std::size_t _grain_count
Total Grain Count.
virtual bool doesFeatureIntersectBoundary(unsigned int feature_id) const override
Returns a Boolean indicating whether this feature intersects any boundary.
std::map< unsigned int, Point > _centroid
The centroid of the feature (average of coordinates from entities participating in the volume calcula...
std::map< unsigned int, Real > _volume
The volume of the feature.
InputParameters validParams< FauxGrainTracker >()
MooseMesh & _mesh
A reference to the mesh.
const std::size_t _n_vars