https://mooseframework.inl.gov
ThreadedElementLoop.h
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 #pragma once
11 
12 #include "ParallelUniqueId.h"
13 #include "FEProblemBase.h"
15 #include "ConsoleUtils.h"
16 #include "SwapBackSentinel.h"
17 
18 // Forward declarations
19 class SystemBase;
20 
29 
33 template <typename RangeType>
35 {
36 public:
38 
40 
41  virtual ~ThreadedElementLoop();
42 
43  virtual void caughtMooseException(MooseException & e) override;
44 
45  virtual bool keepGoing() override { return !_fe_problem.hasException(); }
46 
47  virtual void preElement(const Elem * elem) override;
48 
49  virtual void preInternalSide(const Elem * elem, unsigned int side) override;
50 
51  virtual void preBoundary(const Elem * elem,
52  unsigned int side,
53  BoundaryID bnd_id,
54  const Elem * lower_d_elem = nullptr) override;
55 
56  virtual void neighborSubdomainChanged() override;
57 
58 protected:
59  void prepareElement(const Elem * elem);
60  void clearVarsAndMaterials();
61 
63 
75  template <typename T>
76  void printExecutionOrdering(const std::vector<T *> & objs,
77  const bool print_header = true,
78  const std::string & line_prefix = "[DBG]") const;
79  template <typename T>
80  void printExecutionOrdering(const std::vector<std::shared_ptr<T>> & objs_ptrs,
81  const bool print_header = true,
82  const std::string & line_prefix = "[DBG]") const;
83 };
84 
85 template <typename RangeType>
87  : ThreadedElementLoopBase<RangeType>(fe_problem.mesh()), _fe_problem(fe_problem)
88 {
89 }
90 
91 template <typename RangeType>
93  Threads::split /*split*/)
94  : ThreadedElementLoopBase<RangeType>(x), _fe_problem(x._fe_problem)
95 {
96 }
97 
98 template <typename RangeType>
100 {
101 }
102 
103 template <typename RangeType>
104 void
106 {
107  Threads::spin_mutex::scoped_lock lock(threaded_element_mutex);
108 
109  std::string what(e.what());
110  _fe_problem.setException(what);
111 }
112 
113 template <typename RangeType>
114 void
116 {
117  _fe_problem.setCurrentSubdomainID(el, ThreadedElementLoopBase<RangeType>::_tid);
118 }
119 
120 template <typename RangeType>
121 void
122 ThreadedElementLoop<RangeType>::preInternalSide(const Elem * el, unsigned int side)
123 {
124  _fe_problem.setNeighborSubdomainID(el, side, ThreadedElementLoopBase<RangeType>::_tid);
125 }
126 
127 template <typename RangeType>
128 void
130  unsigned int /*side*/,
131  BoundaryID bnd_id,
132  const Elem * /*=nullptr*/)
133 {
134  _fe_problem.setCurrentBoundaryID(bnd_id, ThreadedElementLoopBase<RangeType>::_tid);
135 }
136 
137 template <typename RangeType>
138 void
140 {
141  _fe_problem.neighborSubdomainSetup(ThreadedElementLoopBase<RangeType>::_neighbor_subdomain,
143 }
144 
145 template <typename RangeType>
146 template <typename T>
147 void
149  const bool print_header,
150  const std::string & line_prefix) const
151 {
152  if (!objs.size())
153  return;
154 
155  auto & console = _fe_problem.console();
156  const auto objects_type = MooseUtils::prettyCppType(objs[0]);
157  std::vector<MooseObject *> moose_objs;
158  for (auto obj_ptr : objs)
159  moose_objs.push_back(dynamic_cast<MooseObject *>(obj_ptr));
160  const auto names = ConsoleUtils::mooseObjectVectorToString(moose_objs);
161 
162  // Print string with a DBG prefix and with sufficient line breaks
163  std::string message = print_header ? "Executing " + objects_type + " on " +
164  _fe_problem.getCurrentExecuteOnFlag().name() + "\n"
165  : "";
166  message += (print_header ? "Order of execution:\n" : "") + names;
167  console << ConsoleUtils::formatString(message, line_prefix) << std::endl;
168 }
169 
170 template <typename RangeType>
171 template <typename T>
172 void
174  const std::vector<std::shared_ptr<T>> & objs_ptrs,
175  const bool print_header,
176  const std::string & line_prefix) const
177 {
178  std::vector<T *> regular_ptrs;
179  for (auto shared_ptr : objs_ptrs)
180  regular_ptrs.push_back(shared_ptr.get());
181  printExecutionOrdering<T>(regular_ptrs, print_header, line_prefix);
182 }
183 
184 template <typename RangeType>
185 void
187 {
188  _fe_problem.prepare(elem, this->_tid);
189  _fe_problem.reinitElem(elem, this->_tid);
190  _fe_problem.reinitMaterials(this->_subdomain, this->_tid);
191 }
192 
193 template <typename RangeType>
194 void
196 {
197  _fe_problem.clearActiveElementalMooseVariables(this->_tid);
198  _fe_problem.clearActiveMaterialProperties(this->_tid);
199 }
std::string mooseObjectVectorToString(const std::vector< MooseObject *> &objs, const std::string &sep=" ")
Routine to output the name of MooseObjects in a string.
Definition: ConsoleUtils.C:598
Base class for assembly-like calculations.
virtual const char * what() const
Get out the error message.
FEProblemBase & _fe_problem
virtual void neighborSubdomainChanged() override
Called every time the neighbor subdomain changes (i.e.
MeshBase & mesh
virtual void preInternalSide(const Elem *elem, unsigned int side) override
Called before evaluations on an element internal side.
virtual void preElement(const Elem *elem) override
Called before the element assembly.
Base class for a system (of equations)
Definition: SystemBase.h:84
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual void preBoundary(const Elem *elem, unsigned int side, BoundaryID bnd_id, const Elem *lower_d_elem=nullptr) override
Called before the boundary assembly.
void prepareElement(const Elem *elem)
void printExecutionOrdering(const std::vector< T *> &objs, const bool print_header=true, const std::string &line_prefix="[DBG]") const
Routine to output the ordering of objects within a vector of pointers to these objects.
virtual bool keepGoing() override
Whether or not the loop should continue.
std::string formatString(std::string message, const std::string &prefix)
Add new lines and prefixes to a string for pretty display in output NOTE: This makes a copy of the st...
Definition: ConsoleUtils.C:582
boundary_id_type BoundaryID
ThreadedElementLoop(FEProblemBase &feproblem)
tbb::split split
Provides a way for users to bail out of the current solve.
Base class for assembly-like calculations.
static Threads::spin_mutex threaded_element_mutex
This mutex is used by all derived classes of the ThreadedElementLoop.
virtual void caughtMooseException(MooseException &e) override
Called if a MooseException is caught anywhere during the computation.
virtual bool hasException()
Whether or not an exception has occurred.
std::string prettyCppType(const std::string &cpp_type)
Definition: MooseUtils.C:1246