www.mooseframework.org
ComputeResidualThread.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "ComputeResidualThread.h"
16 #include "NonlinearSystem.h"
17 #include "Problem.h"
18 #include "FEProblem.h"
19 #include "KernelBase.h"
20 #include "IntegratedBC.h"
21 #include "DGKernel.h"
22 #include "InterfaceKernel.h"
23 #include "Material.h"
24 #include "TimeKernel.h"
25 #include "KernelWarehouse.h"
26 #include "SwapBackSentinel.h"
27 
28 #include "libmesh/threads.h"
29 
31  : ThreadedElementLoop<ConstElemRange>(fe_problem),
32  _nl(fe_problem.getNonlinearSystemBase()),
33  _kernel_type(type),
34  _num_cached(0),
35  _integrated_bcs(_nl.getIntegratedBCWarehouse()),
36  _dg_kernels(_nl.getDGKernelWarehouse()),
37  _interface_kernels(_nl.getInterfaceKernelWarehouse()),
38  _kernels(_nl.getKernelWarehouse())
39 {
40 }
41 
42 // Splitting Constructor
44  : ThreadedElementLoop<ConstElemRange>(x, split),
45  _nl(x._nl),
47  _num_cached(0),
52 {
53 }
54 
56 
57 void
59 {
61 
62  // Update variable Dependencies
63  std::set<MooseVariable *> needed_moose_vars;
68 
69  // Update material dependencies
70  std::set<unsigned int> needed_mat_props;
75 
77  _fe_problem.setActiveMaterialProperties(needed_mat_props, _tid);
79 }
80 
81 void
83 {
84  _fe_problem.prepare(elem, _tid);
86 
87  // Set up Sentinel class so that, even if reinitMaterials() throws, we
88  // still remember to swap back during stack unwinding.
90 
92 
93  const MooseObjectWarehouse<KernelBase> * warehouse;
94  switch (_kernel_type)
95  {
96  case Moose::KT_ALL:
97  warehouse = &_nl.getKernelWarehouse();
98  break;
99 
100  case Moose::KT_TIME:
101  warehouse = &_nl.getTimeKernelWarehouse();
102  break;
103 
104  case Moose::KT_NONTIME:
105  warehouse = &_nl.getNonTimeKernelWarehouse();
106  break;
107 
108  case Moose::KT_EIGEN:
109  warehouse = &_nl.getEigenKernelWarehouse();
110  break;
111 
112  case Moose::KT_NONEIGEN:
113  warehouse = &_nl.getNonEigenKernelWarehouse();
114  break;
115 
116  default:
117  mooseError("Unknown Kernel Type \n");
118  }
119 
120  if (warehouse->hasActiveBlockObjects(_subdomain, _tid))
121  {
122  const auto & kernels = warehouse->getActiveBlockObjects(_subdomain, _tid);
123  for (const auto & kernel : kernels)
124  kernel->computeResidual();
125  }
126 }
127 
128 void
129 ComputeResidualThread::onBoundary(const Elem * elem, unsigned int side, BoundaryID bnd_id)
130 {
132  {
133  const auto & bcs = _integrated_bcs.getActiveBoundaryObjects(bnd_id, _tid);
134 
135  _fe_problem.reinitElemFace(elem, side, bnd_id, _tid);
136 
137  // Set up Sentinel class so that, even if reinitMaterialsFace() throws, we
138  // still remember to swap back during stack unwinding.
140 
141  _fe_problem.reinitMaterialsFace(elem->subdomain_id(), _tid);
143 
144  for (const auto & bc : bcs)
145  {
146  if (bc->shouldApply())
147  bc->computeResidual();
148  }
149  }
150 }
151 
152 void
153 ComputeResidualThread::onInterface(const Elem * elem, unsigned int side, BoundaryID bnd_id)
154 {
156  {
157 
158  // Pointer to the neighbor we are currently working on.
159  const Elem * neighbor = elem->neighbor_ptr(side);
160 
161  if (!(neighbor->level() == elem->level()))
162  mooseError("Sorry, interface kernels do not work with mesh adaptivity");
163 
164  if (neighbor->active())
165  {
166  _fe_problem.reinitNeighbor(elem, side, _tid);
167 
168  // Set up Sentinels so that, even if one of the reinitMaterialsXXX() calls throws, we
169  // still remember to swap back during stack unwinding.
171  _fe_problem.reinitMaterialsFace(elem->subdomain_id(), _tid);
172 
174  _fe_problem.reinitMaterialsNeighbor(neighbor->subdomain_id(), _tid);
175 
176  const auto & int_ks = _interface_kernels.getActiveBoundaryObjects(bnd_id, _tid);
177  for (const auto & interface_kernel : int_ks)
178  interface_kernel->computeResidual();
179 
180  {
181  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
183  }
184  }
185  }
186 }
187 
188 void
189 ComputeResidualThread::onInternalSide(const Elem * elem, unsigned int side)
190 {
192  {
193  // Pointer to the neighbor we are currently working on.
194  const Elem * neighbor = elem->neighbor_ptr(side);
195 
196  // Get the global id of the element and the neighbor
197  const dof_id_type elem_id = elem->id(), neighbor_id = neighbor->id();
198 
199  if ((neighbor->active() && (neighbor->level() == elem->level()) && (elem_id < neighbor_id)) ||
200  (neighbor->level() < elem->level()))
201  {
202  _fe_problem.reinitNeighbor(elem, side, _tid);
203 
204  // Set up Sentinels so that, even if one of the reinitMaterialsXXX() calls throws, we
205  // still remember to swap back during stack unwinding.
207  _fe_problem.reinitMaterialsFace(elem->subdomain_id(), _tid);
208 
210  _fe_problem.reinitMaterialsNeighbor(neighbor->subdomain_id(), _tid);
211 
212  const auto & dgks = _dg_kernels.getActiveBlockObjects(_subdomain, _tid);
213  for (const auto & dg_kernel : dgks)
214  if (dg_kernel->hasBlocks(neighbor->subdomain_id()))
215  dg_kernel->computeResidual();
216 
217  {
218  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
220  }
221  }
222  }
223 }
224 
225 void
227 {
229  _num_cached++;
230 
231  if (_num_cached % 20 == 0)
232  {
233  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
235  }
236 }
237 
238 void
240 {
243 }
244 
245 void
247 {
248 }
void updateBlockVariableDependency(SubdomainID id, std::set< MooseVariable * > &needed_moose_vars, THREAD_ID tid=0) const
virtual void onBoundary(const Elem *elem, unsigned int side, BoundaryID bnd_id) override
Called when doing boundary assembling.
virtual void setActiveElementalMooseVariables(const std::set< MooseVariable * > &moose_vars, THREAD_ID tid) override
Set the MOOSE variables to be reinited on each element.
virtual void prepare(const Elem *elem, THREAD_ID tid) override
Base class for assembly-like calculations.
virtual void addResidualNeighbor(THREAD_ID tid) override
virtual void prepareMaterials(SubdomainID blk_id, THREAD_ID tid)
Add the MooseVariables that the current materials depend on to the dependency list.
void updateBlockMatPropDependency(SubdomainID id, std::set< unsigned int > &needed_mat_props, THREAD_ID tid=0) const
virtual void postElement(const Elem *) override
Called after the element assembly is done (including surface assembling)
const KernelWarehouse & getNonEigenKernelWarehouse()
const std::map< BoundaryID, std::vector< std::shared_ptr< T > > > & getActiveBoundaryObjects(THREAD_ID tid=0) const
virtual void reinitMaterialsNeighbor(SubdomainID blk_id, THREAD_ID tid, bool swap_stateful=true)
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
const KernelWarehouse & getEigenKernelWarehouse()
ComputeResidualThread(FEProblemBase &fe_problem, Moose::KernelType type)
virtual void subdomainChanged() override
Called every time the current subdomain changes (i.e.
void join(const ComputeResidualThread &)
static PetscErrorCode Vec x
const MooseObjectWarehouse< InterfaceKernel > & _interface_kernels
Reference to interface kernel storage structure.
virtual void reinitElem(const Elem *elem, THREAD_ID tid) override
virtual void reinitMaterials(SubdomainID blk_id, THREAD_ID tid, bool swap_stateful=true)
const MooseObjectWarehouse< IntegratedBC > & _integrated_bcs
Reference to BC storage structures.
virtual void reinitMaterialsBoundary(BoundaryID boundary_id, THREAD_ID tid, bool swap_stateful=true)
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual void setActiveMaterialProperties(const std::set< unsigned int > &mat_prop_ids, THREAD_ID tid) override
Record and set the material properties required by the current computing thread.
virtual void subdomainSetup(SubdomainID subdomain, THREAD_ID tid)
Moose::KernelType _kernel_type
void updateBoundaryVariableDependency(std::set< MooseVariable * > &needed_moose_vars, THREAD_ID tid=0) const
const KernelWarehouse & _kernels
const MooseObjectWarehouse< DGKernel > & _dg_kernels
Reference to DGKernel storage structure.
void updateBoundaryMatPropDependency(std::set< unsigned int > &needed_mat_props, THREAD_ID tid=0) const
virtual void addCachedResidual(THREAD_ID tid) override
bool hasActiveBoundaryObjects(THREAD_ID tid=0) const
virtual void clearActiveMaterialProperties(THREAD_ID tid) override
Clear the active material properties.
virtual void onElement(const Elem *elem) override
Assembly of the element (not including surface assembly)
virtual void swapBackMaterials(THREAD_ID tid)
KernelType
Definition: MooseTypes.h:162
virtual void swapBackMaterialsNeighbor(THREAD_ID tid)
const std::map< SubdomainID, std::vector< std::shared_ptr< T > > > & getActiveBlockObjects(THREAD_ID tid=0) const
MatType type
virtual void reinitElemFace(const Elem *elem, unsigned int side, BoundaryID bnd_id, THREAD_ID tid) override
virtual void post() override
Called after the element range loop.
virtual void onInternalSide(const Elem *elem, unsigned int side) override
Called when doing internal edge assembling.
virtual void reinitMaterialsFace(SubdomainID blk_id, THREAD_ID tid, bool swap_stateful=true)
NonlinearSystemBase & _nl
virtual void reinitNeighbor(const Elem *elem, unsigned int side, THREAD_ID tid) override
virtual void onInterface(const Elem *elem, unsigned int side, BoundaryID bnd_id) override
Called when doing interface assembling.
bool hasActiveBlockObjects(THREAD_ID tid=0) const
SubdomainID _subdomain
The subdomain for the current element.
const KernelWarehouse & getTimeKernelWarehouse()
virtual void cacheResidual(THREAD_ID tid) override
const KernelWarehouse & getNonTimeKernelWarehouse()
virtual void swapBackMaterialsFace(THREAD_ID tid)
virtual void clearActiveElementalMooseVariables(THREAD_ID tid) override
Clear the active elemental MooseVariable.
boundary_id_type BoundaryID
Definition: MooseTypes.h:75
const KernelWarehouse & getKernelWarehouse()
Access functions to Warehouses from outside NonlinearSystemBase.
The "SwapBackSentinel" class&#39;s destructor guarantees that FEProblemBase::swapBackMaterials{Face,Neighbor}() is called even when an exception is thrown from FEProblemBase::reinitMaterials{Face,Neighbor}.