https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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"
14#include "MaterialBase.h"
16#include "ConsoleUtils.h"
17#include "SwapBackSentinel.h"
18
19// Forward declarations
20class SystemBase;
21
30
34template <typename RangeType>
36{
37public:
39
41
43
44 virtual void caughtMooseException(MooseException & e) override;
45
46 virtual bool keepGoing() override { return !_fe_problem.hasException(); }
47
48 virtual void preElement(const Elem * elem) override;
49
50 virtual void preInternalSide(const Elem * elem, unsigned int side) override;
51
52 virtual void preBoundary(const Elem * elem,
53 unsigned int side,
54 BoundaryID bnd_id,
55 const Elem * lower_d_elem = nullptr) override;
56
57 virtual void neighborSubdomainChanged() override;
58
59protected:
60 void prepareElement(const Elem * elem);
64 const std::vector<const MaterialPropertyInterface *> & material_consumers,
65 BoundaryID bnd_id,
66 SubdomainID subdomain_id,
67 std::deque<MaterialBase *> & required_face_materials,
68 std::deque<MaterialBase *> & required_boundary_materials) const;
70
82 template <typename T>
83 void printExecutionOrdering(const std::vector<T *> & objs,
84 const bool print_header = true,
85 const std::string & line_prefix = "[DBG]") const;
86 template <typename T>
87 void printExecutionOrdering(const std::vector<std::shared_ptr<T>> & objs_ptrs,
88 const bool print_header = true,
89 const std::string & line_prefix = "[DBG]") const;
90};
91
92template <typename RangeType>
94 : ThreadedElementLoopBase<RangeType>(fe_problem.mesh()), _fe_problem(fe_problem)
95{
96}
97
98template <typename RangeType>
100 Threads::split /*split*/)
101 : ThreadedElementLoopBase<RangeType>(x), _fe_problem(x._fe_problem)
102{
103}
104
105template <typename RangeType>
109
110template <typename RangeType>
111void
113{
114 Threads::spin_mutex::scoped_lock lock(threaded_element_mutex);
115
116 std::string what(e.what());
117 _fe_problem.setException(what);
118}
119
120template <typename RangeType>
121void
123{
124 _fe_problem.setCurrentSubdomainID(el, ThreadedElementLoopBase<RangeType>::_tid);
125}
126
127template <typename RangeType>
128void
129ThreadedElementLoop<RangeType>::preInternalSide(const Elem * el, unsigned int side)
130{
131 _fe_problem.setNeighborSubdomainID(el, side, ThreadedElementLoopBase<RangeType>::_tid);
132}
133
134template <typename RangeType>
135void
137 unsigned int /*side*/,
138 BoundaryID bnd_id,
139 const Elem * /*=nullptr*/)
140{
141 _fe_problem.setCurrentBoundaryID(bnd_id, ThreadedElementLoopBase<RangeType>::_tid);
142}
143
144template <typename RangeType>
145void
151
152template <typename RangeType>
153template <typename T>
154void
156 const bool print_header,
157 const std::string & line_prefix) const
158{
159 if (!objs.size())
160 return;
161
162 auto & console = _fe_problem.console();
163 const auto objects_type = MooseUtils::prettyCppType(objs[0]);
164 std::vector<MooseObject *> moose_objs;
165 for (auto obj_ptr : objs)
166 moose_objs.push_back(dynamic_cast<MooseObject *>(obj_ptr));
167 const auto names = ConsoleUtils::mooseObjectVectorToString(moose_objs);
168
169 // Print string with a DBG prefix and with sufficient line breaks
170 std::string message = print_header ? "Executing " + objects_type + " on " +
171 _fe_problem.getCurrentExecuteOnFlag().name() + "\n"
172 : "";
173 message += (print_header ? "Order of execution:\n" : "") + names;
174 console << ConsoleUtils::formatString(message, line_prefix) << std::endl;
175}
176
177template <typename RangeType>
178template <typename T>
179void
181 const std::vector<std::shared_ptr<T>> & objs_ptrs,
182 const bool print_header,
183 const std::string & line_prefix) const
184{
185 std::vector<T *> regular_ptrs;
186 for (auto shared_ptr : objs_ptrs)
187 regular_ptrs.push_back(shared_ptr.get());
188 printExecutionOrdering<T>(regular_ptrs, print_header, line_prefix);
189}
190
191template <typename RangeType>
192void
194{
195 _fe_problem.prepare(elem, this->_tid);
196 _fe_problem.reinitElem(elem, this->_tid);
197 _fe_problem.reinitMaterials(this->_subdomain, this->_tid);
198}
199
200template <typename RangeType>
201void
203{
204 _fe_problem.clearActiveElementalMooseVariables(this->_tid);
205 _fe_problem.clearActiveMaterialProperties(this->_tid);
206}
207
208template <typename RangeType>
209void
211 const std::vector<const MaterialPropertyInterface *> & material_consumers,
212 const BoundaryID bnd_id,
213 const SubdomainID subdomain_id,
214 std::deque<MaterialBase *> & required_face_materials,
215 std::deque<MaterialBase *> & required_boundary_materials) const
216{
217 required_face_materials.clear();
218 required_boundary_materials.clear();
219
220 std::unordered_set<unsigned int> needed_face_props;
221 for (const auto * const consumer : material_consumers)
222 {
223 const auto & dependencies = consumer->getMatPropDependencies();
224 needed_face_props.insert(dependencies.begin(), dependencies.end());
225 }
226
227 const auto & materials = _fe_problem.getRegularMaterialsWarehouse();
228 // Resolve boundary materials first. A boundary material can depend on a property supplied by a
229 // face material, while a property supplied by the boundary-material chain does not also need a
230 // face producer.
231 if (!material_consumers.empty() && materials.hasActiveBoundaryObjects(bnd_id, this->_tid))
232 required_boundary_materials = MaterialBase::buildRequiredMaterials(
233 material_consumers, materials.getActiveBoundaryObjects(bnd_id, this->_tid), true);
234 for (const auto * const material : required_boundary_materials)
235 {
236 const auto & dependencies = material->getMatPropDependencies();
237 needed_face_props.insert(dependencies.begin(), dependencies.end());
238 }
239 // The boundary-material chain has already satisfied these properties. Removing them prevents a
240 // face material that happens to declare the same property from being selected unnecessarily.
241 for (const auto * const material : required_boundary_materials)
242 for (const auto supplied_prop : material->getSuppliedPropIDs())
243 needed_face_props.erase(supplied_prop);
244
245 struct MaterialDependencyConsumer
246 {
247 const std::unordered_set<unsigned int> & dependencies;
248 const std::unordered_set<unsigned int> & getMatPropDependencies() const { return dependencies; }
249 };
250 if (!needed_face_props.empty())
251 {
252 const auto & face_materials = materials[Moose::FACE_MATERIAL_DATA];
253 if (face_materials.hasActiveBlockObjects(subdomain_id, this->_tid))
254 {
255 const MaterialDependencyConsumer face_consumer{needed_face_props};
256 const std::vector<const MaterialDependencyConsumer *> face_consumers{&face_consumer};
257 required_face_materials = MaterialBase::buildRequiredMaterials(
258 face_consumers, face_materials.getActiveBlockObjects(subdomain_id, this->_tid), true);
259 }
260 }
261}
boundary_id_type BoundaryID
subdomain_id_type SubdomainID
static Threads::spin_mutex threaded_element_mutex
This mutex is used by all derived classes of the ThreadedElementLoop.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual bool hasException()
Whether or not an exception has occurred.
static std::deque< MaterialBase * > buildRequiredMaterials(const Consumers &mat_consumers, const std::vector< std::shared_ptr< MaterialBase > > &mats, const bool allow_stateful)
Build the materials required by a set of consumer objects.
Provides a way for users to bail out of the current solve.
virtual const char * what() const
Get out the error message.
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
Base class for a system (of equations)
Definition SystemBase.h:87
Base class for assembly-like calculations.
Base class for assembly-like calculations.
virtual void caughtMooseException(MooseException &e) override
Called if a MooseException is caught anywhere during the computation.
ThreadedElementLoop(ThreadedElementLoop &x, Threads::split split)
virtual void preInternalSide(const Elem *elem, unsigned int side) override
Called before evaluations on an element internal side.
virtual void neighborSubdomainChanged() override
Called every time the neighbor subdomain changes (i.e.
ThreadedElementLoop(FEProblemBase &feproblem)
virtual bool keepGoing() override
Whether or not the loop should continue.
void printExecutionOrdering(const std::vector< std::shared_ptr< T > > &objs_ptrs, const bool print_header=true, const std::string &line_prefix="[DBG]") const
void prepareElement(const Elem *elem)
FEProblemBase & _fe_problem
virtual void preElement(const Elem *elem) override
Called before the element assembly.
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.
void getRequiredBoundaryMaterials(const std::vector< const MaterialPropertyInterface * > &material_consumers, BoundaryID bnd_id, SubdomainID subdomain_id, std::deque< MaterialBase * > &required_face_materials, std::deque< MaterialBase * > &required_boundary_materials) const
Determine the face and boundary materials required by material property consumers.
virtual void preBoundary(const Elem *elem, unsigned int side, BoundaryID bnd_id, const Elem *lower_d_elem=nullptr) override
Called before the boundary assembly.
MeshBase & mesh
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...
std::string mooseObjectVectorToString(const std::vector< MooseObject * > &objs, const std::string &sep=" ")
Routine to output the name of MooseObjects in a string.
std::string prettyCppType(const std::string &cpp_type)
@ FACE_MATERIAL_DATA
Definition MooseTypes.h:749