www.mooseframework.org
ThreadedElementLoopBase.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 #pragma once
11 
12 #include "ParallelUniqueId.h"
13 #include "MooseMesh.h"
14 #include "MooseTypes.h"
15 #include "MooseException.h"
16 #include "libmesh/libmesh_exceptions.h"
17 #include "libmesh/elem.h"
18 
22 template <typename RangeType>
24 {
25 public:
27 
29 
30  virtual ~ThreadedElementLoopBase();
31 
32  virtual void operator()(const RangeType & range, bool bypass_threading = false);
33 
37  virtual void pre();
38 
42  virtual void post();
43 
49  virtual void onElement(const Elem * elem);
50 
56  virtual void preElement(const Elem * elem);
57 
63  virtual void postElement(const Elem * elem);
64 
73  virtual void preBoundary(const Elem * elem,
74  unsigned int side,
75  BoundaryID bnd_id,
76  const Elem * lower_d_elem = nullptr);
77 
86  virtual void onBoundary(const Elem * elem,
87  unsigned int side,
88  BoundaryID bnd_id,
89  const Elem * lower_d_elem = nullptr);
90 
97  virtual void preInternalSide(const Elem * elem, unsigned int side);
98 
105  virtual void postInternalSide(const Elem * elem, unsigned int side);
106 
113  virtual void onInternalSide(const Elem * elem, unsigned int side);
114 
122  virtual void onInterface(const Elem * elem, unsigned int side, BoundaryID bnd_id);
123 
130  virtual void subdomainChanged();
131 
138  virtual void neighborSubdomainChanged();
139 
145 
151  virtual bool keepGoing() { return true; }
152 
153 protected:
156 
159 
162 
165 
168 
170  virtual void printGeneralExecutionInformation() const {}
171 
173  virtual void printBlockExecutionInformation() const {}
174 
176  virtual void printBoundaryExecutionInformation(const unsigned int /*bid*/) const {}
177 
179  mutable std::set<SubdomainID> _blocks_exec_printed;
180 
182  mutable std::set<BoundaryID> _boundaries_exec_printed;
183 
185  void resetExecPrintedSets() const;
186 
187 private:
194  virtual bool shouldComputeInternalSide(const Elem & elem, const Elem & neighbor) const;
195 };
196 
197 template <typename RangeType>
199 {
200 }
201 
202 template <typename RangeType>
204  Threads::split /*split*/)
205  : _mesh(x._mesh)
206 {
207 }
208 
209 template <typename RangeType>
211 {
212 }
213 
214 template <typename RangeType>
215 void
216 ThreadedElementLoopBase<RangeType>::operator()(const RangeType & range, bool bypass_threading)
217 {
218  try
219  {
220  try
221  {
222  ParallelUniqueId puid;
223  _tid = bypass_threading ? 0 : puid.id;
224 
225  pre();
226  printGeneralExecutionInformation();
227 
228  _subdomain = Moose::INVALID_BLOCK_ID;
229  _neighbor_subdomain = Moose::INVALID_BLOCK_ID;
230  typename RangeType::const_iterator el = range.begin();
231  for (el = range.begin(); el != range.end(); ++el)
232  {
233  if (!keepGoing())
234  break;
235 
236  const Elem * elem = *el;
237 
238  preElement(elem);
239 
240  _old_subdomain = _subdomain;
241  _subdomain = elem->subdomain_id();
242  if (_subdomain != _old_subdomain)
243  {
244  subdomainChanged();
245  printBlockExecutionInformation();
246  }
247 
248  onElement(elem);
249 
250  if (elem->subdomain_id() == Moose::INTERNAL_SIDE_LOWERD_ID ||
251  elem->subdomain_id() == Moose::BOUNDARY_SIDE_LOWERD_ID)
252  {
253  postElement(elem);
254  continue;
255  }
256 
257  for (unsigned int side = 0; side < elem->n_sides(); side++)
258  {
259  std::vector<BoundaryID> boundary_ids = _mesh.getBoundaryIDs(elem, side);
260  const Elem * lower_d_elem = _mesh.getLowerDElem(elem, side);
261 
262  if (boundary_ids.size() > 0)
263  for (std::vector<BoundaryID>::iterator it = boundary_ids.begin();
264  it != boundary_ids.end();
265  ++it)
266  {
267  preBoundary(elem, side, *it, lower_d_elem);
268  printBoundaryExecutionInformation(*it);
269  onBoundary(elem, side, *it, lower_d_elem);
270  }
271 
272  const Elem * neighbor = elem->neighbor_ptr(side);
273  if (neighbor)
274  {
275  preInternalSide(elem, side);
276 
277  _old_neighbor_subdomain = _neighbor_subdomain;
278  _neighbor_subdomain = neighbor->subdomain_id();
279  if (_neighbor_subdomain != _old_neighbor_subdomain)
280  neighborSubdomainChanged();
281 
282  if (shouldComputeInternalSide(*elem, *neighbor))
283  onInternalSide(elem, side);
284 
285  if (boundary_ids.size() > 0)
286  for (std::vector<BoundaryID>::iterator it = boundary_ids.begin();
287  it != boundary_ids.end();
288  ++it)
289  onInterface(elem, side, *it);
290 
291  postInternalSide(elem, side);
292  }
293  } // sides
294 
295  postElement(elem);
296  } // range
297 
298  post();
299  resetExecPrintedSets();
300  }
301  catch (libMesh::LogicError & e)
302  {
303  mooseException("We caught a libMesh error in ThreadedElementLoopBase:", e.what());
304  }
305  catch (MetaPhysicL::LogicError & e)
306  {
308  }
309  }
310  catch (MooseException & e)
311  {
312  caughtMooseException(e);
313  }
314 }
315 
316 template <typename RangeType>
317 void
319 {
320 }
321 
322 template <typename RangeType>
323 void
325 {
326 }
327 
328 template <typename RangeType>
329 void
331 {
332 }
333 
334 template <typename RangeType>
335 void
337 {
338 }
339 
340 template <typename RangeType>
341 void
343 {
344 }
345 
346 template <typename RangeType>
347 void
349  unsigned int /*side*/,
350  BoundaryID /*bnd_id*/,
351  const Elem * /*lower_d_elem = nullptr*/)
352 {
353 }
354 
355 template <typename RangeType>
356 void
358  unsigned int /*side*/,
359  BoundaryID /*bnd_id*/,
360  const Elem * /*lower_d_elem = nullptr*/)
361 {
362 }
363 
364 template <typename RangeType>
365 void
366 ThreadedElementLoopBase<RangeType>::preInternalSide(const Elem * /*elem*/, unsigned int /*side*/)
367 {
368 }
369 
370 template <typename RangeType>
371 void
372 ThreadedElementLoopBase<RangeType>::postInternalSide(const Elem * /*elem*/, unsigned int /*side*/)
373 {
374 }
375 
376 template <typename RangeType>
377 void
378 ThreadedElementLoopBase<RangeType>::onInternalSide(const Elem * /*elem*/, unsigned int /*side*/)
379 {
380 }
381 
382 template <typename RangeType>
383 void
385  unsigned int /*side*/,
386  BoundaryID /*bnd_id*/)
387 {
388 }
389 
390 template <typename RangeType>
391 void
393 {
394 }
395 
396 template <typename RangeType>
397 void
399 {
400 }
401 
402 template <typename RangeType>
403 bool
405  const Elem & neighbor) const
406 {
407  auto level = [this](const auto & elem_arg)
408  {
409  if (_mesh.doingPRefinement())
410  return elem_arg.p_level();
411  else
412  return elem_arg.level();
413  };
414  const auto elem_id = elem.id(), neighbor_id = neighbor.id();
415  const auto elem_level = level(elem), neighbor_level = level(neighbor);
416 
417  // When looping over elements and then sides, we need to make sure that we do not duplicate
418  // effort, e.g. if a face is shared by element 1 and element 2, then we do not want to do compute
419  // work both when we are visiting element 1 *and* then later when visiting element 2. Our rule is
420  // to only compute when we are visiting the element that has the lower element id when element and
421  // neighbor are of the same adaptivity level, and then if they are not of the same level, then
422  // we only compute when we are visiting the finer element
423  return (neighbor.active() && (neighbor_level == elem_level) && (elem_id < neighbor_id)) ||
424  (neighbor_level < elem_level);
425 }
426 
427 template <typename RangeType>
428 void
430 {
431  _blocks_exec_printed.clear();
432  _boundaries_exec_printed.clear();
433 }
virtual bool keepGoing()
Whether or not the loop should continue.
void resetExecPrintedSets() const
Resets the set of blocks and boundaries visited.
virtual bool shouldComputeInternalSide(const Elem &elem, const Elem &neighbor) const
Whether to compute the internal side for the provided element-neighbor pair.
virtual void onElement(const Elem *elem)
Assembly of the element (not including surface assembly)
void translateMetaPhysicLError(const MetaPhysicL::LogicError &)
emit a relatively clear error message when we catch a MetaPhysicL logic error
Definition: MooseError.C:110
virtual void printBoundaryExecutionInformation(const unsigned int) const
Print information about the particular ordering of objects on each boundary.
virtual void pre()
Called before the element range loop.
const SubdomainID BOUNDARY_SIDE_LOWERD_ID
Definition: MooseTypes.C:21
MeshBase & mesh
virtual void subdomainChanged()
Called every time the current subdomain changes (i.e.
virtual void neighborSubdomainChanged()
Called every time the neighbor subdomain changes (i.e.
virtual void preInternalSide(const Elem *elem, unsigned int side)
Called before evaluations on an element internal side.
virtual void postInternalSide(const Elem *elem, unsigned int side)
Called after evaluations on an element internal side.
const SubdomainID INVALID_BLOCK_ID
Definition: MooseTypes.C:22
virtual void onBoundary(const Elem *elem, unsigned int side, BoundaryID bnd_id, const Elem *lower_d_elem=nullptr)
Called when doing boundary assembling.
virtual void postElement(const Elem *elem)
Called after the element assembly is done (including surface assembling)
virtual void printGeneralExecutionInformation() const
Print information about the loop ordering.
boundary_id_type BoundaryID
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:88
virtual void onInterface(const Elem *elem, unsigned int side, BoundaryID bnd_id)
Called when doing interface assembling.
SubdomainID _old_neighbor_subdomain
The subdomain for the last neighbor.
virtual void operator()(const RangeType &range, bool bypass_threading=false)
virtual void onInternalSide(const Elem *elem, unsigned int side)
Called when doing internal edge assembling.
bool onBoundary(const SubdomainRestrictable &obj, const FaceInfo &fi)
Return whether the supplied face is on a boundary of the object&#39;s execution.
Definition: MathFVUtils.h:729
std::set< SubdomainID > _blocks_exec_printed
Keep track of which blocks were visited.
tbb::split split
Provides a way for users to bail out of the current solve.
std::set< BoundaryID > _boundaries_exec_printed
Keep track of which boundaries were visited.
virtual void caughtMooseException(MooseException &)
Called if a MooseException is caught anywhere during the computation.
const SubdomainID INTERNAL_SIDE_LOWERD_ID
Definition: MooseTypes.C:20
Base class for assembly-like calculations.
SubdomainID _subdomain
The subdomain for the current element.
SubdomainID _old_subdomain
The subdomain for the last element.
virtual void post()
Called after the element range loop.
virtual void printBlockExecutionInformation() const
Print information about the particular ordering of objects on each block.
virtual void preElement(const Elem *elem)
Called before the element assembly.
virtual void preBoundary(const Elem *elem, unsigned int side, BoundaryID bnd_id, const Elem *lower_d_elem=nullptr)
Called before the boundary assembly.
ThreadedElementLoopBase(MooseMesh &mesh)
SubdomainID _neighbor_subdomain
The subdomain for the current neighbor.
unsigned int THREAD_ID
Definition: MooseTypes.h:198