https://mooseframework.inl.gov
ComputeDiracThread.C
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 #include "ComputeDiracThread.h"
11 
12 // Moose Includes
13 #include "ParallelUniqueId.h"
14 #include "DiracKernel.h"
15 #include "Problem.h"
16 #include "NonlinearSystem.h"
17 #include "MooseVariableFE.h"
18 #include "Assembly.h"
19 #include "ThreadedElementLoop.h"
20 
21 #include "libmesh/threads.h"
22 
24  const std::set<TagID> & tags,
25  bool is_jacobian)
26  : ThreadedElementLoop<DistElemRange>(feproblem),
27  _is_jacobian(is_jacobian),
28  _nl(feproblem.currentNonlinearSystem()),
29  _tags(tags),
30  _dirac_kernels(_nl.getDiracKernelWarehouse())
31 {
32 }
33 
34 // Splitting Constructor
37  _is_jacobian(x._is_jacobian),
38  _nl(x._nl),
39  _tags(x._tags),
40  _dirac_kernels(x._dirac_kernels)
41 {
42 }
43 
45 
46 void
48 {
49  // Force TID=0 because we run this object _NON THREADED_
50  // Take this out if we ever get Dirac's working with threads!
51  _tid = 0;
52 }
53 
54 void
56 {
58 
59  std::set<MooseVariableFEBase *> needed_moose_vars;
60  _dirac_kernels.updateVariableDependency(needed_moose_vars, _tid);
61 
62  // Update material dependencies
63  std::unordered_set<unsigned int> needed_mat_props;
64  _dirac_kernels.updateMatPropDependency(needed_mat_props, _tid);
65 
67  _fe_problem.setActiveMaterialProperties(needed_mat_props, _tid);
68 
69  // If users pass a empty vector or a full size of vector,
70  // we take all kernels
71  if (!_tags.size() || _tags.size() == _fe_problem.numMatrixTags())
73  // If we have one tag only, We call tag based storage
74  else if (_tags.size() == 1)
78  // This one may be expensive, and hopefully we do not use it so often
79  else
82 }
83 
84 void
86 {
87  const bool has_dirac_kernels_on_elem = _fe_problem.reinitDirac(elem, _tid);
88  if (!has_dirac_kernels_on_elem)
89  return;
90 
91  std::set<MooseVariableFEBase *> needed_moose_vars;
92  const auto & dkernels = _dirac_warehouse->getActiveObjects(_tid);
93 
94  // Only call reinitMaterials() if one or more DiracKernels has
95  // actually called getMaterialProperty(). Loop over all the
96  // DiracKernels and check whether this is the case.
97  for (const auto & dirac_kernel : dkernels)
98  {
99  // If any of the DiracKernels have had getMaterialProperty()
100  // called, we need to reinit Materials.
101  if (dirac_kernel->getMaterialPropertyCalled())
102  {
103  _fe_problem.reinitMaterials(_subdomain, _tid, /*swap_stateful=*/false);
104  break;
105  }
106  }
107 
108  for (const auto & dirac_kernel : dkernels)
109  {
110  if (!dirac_kernel->hasPointsOnElem(elem))
111  continue;
112  else if (!_is_jacobian)
113  {
114  dirac_kernel->computeResidual();
115  continue;
116  }
117 
118  // Get a list of coupled variables from the SubProblem
119  const auto & coupling_entries =
120  dirac_kernel->subProblem().assembly(_tid, _nl.number()).couplingEntries();
121 
122  // Loop over the list of coupled variable pairs
123  for (const auto & it : coupling_entries)
124  {
125  const MooseVariableFEBase * const ivariable = it.first;
126  const MooseVariableFEBase * const jvariable = it.second;
127 
128  // A variant of the check that is in
129  // ComputeFullJacobianThread::computeJacobian(). We
130  // only want to call computeOffDiagJacobian() if both
131  // variables are active on this subdomain, and the
132  // off-diagonal variable actually has dofs.
133  if (dirac_kernel->variable().number() == ivariable->number() &&
134  ivariable->activeOnSubdomain(_subdomain) && jvariable->activeOnSubdomain(_subdomain) &&
135  (jvariable->numberOfDofs() > 0))
136  {
137  dirac_kernel->prepareShapes(jvariable->number());
138  dirac_kernel->computeOffDiagJacobian(jvariable->number());
139  }
140  }
141  }
142 
143  // Note that we do not call swapBackMaterials() here as they were
144  // never swapped in the first place. This avoids messing up
145  // stored values of stateful material properties.
146 }
147 
148 void
149 ComputeDiracThread::postElement(const Elem * /*elem*/)
150 {
151  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
152  if (!_is_jacobian)
154  else
156 }
157 
158 void
160 {
163 }
164 
165 void
167 {
168 }
169 
170 void
172 {
174  return;
175  const auto & console = _fe_problem.console();
176  console << "[DBG] Executing Dirac Kernels on " << _fe_problem.getCurrentExecuteOnFlag().name()
177  << std::endl;
178 }
179 
180 void
182 {
185  return;
186 
187  const auto & dkernels = _dirac_warehouse->getActiveBlockObjects(_subdomain, _tid);
188  const auto & console = _fe_problem.console();
189  console << "[DBG] Ordering of DiracKernels on subdomain " << _subdomain << std::endl;
190  printExecutionOrdering<DiracKernelBase>(dkernels, false);
192 }
void updateVariableDependency(std::set< MooseVariableFieldBase *> &needed_moose_vars, THREAD_ID tid=0) const
Update variable dependency vector.
void setActiveMaterialProperties(const std::unordered_set< unsigned int > &mat_prop_ids, const THREAD_ID tid)
Record and set the material properties required by the current computing thread.
Base class for assembly-like calculations.
virtual unsigned int numberOfDofs() const
Get the number of local DoFs.
bool hasActiveBlockObjects(THREAD_ID tid=0) const
const std::string & name() const
Definition: MooseEnumItem.h:35
const std::map< SubdomainID, std::vector< std::shared_ptr< T > > > & getActiveBlockObjects(THREAD_ID tid=0) const
void join(const ComputeDiracThread &)
unsigned int number() const
Get variable number coming from libMesh.
virtual void pre() override
Called before the element range loop.
const ExecFlagType & getCurrentExecuteOnFlag() const
Return/set the current execution flag.
virtual void addJacobian(const THREAD_ID tid) override
MooseObjectWarehouse< T > & getVectorTagsObjectWarehouse(const std::set< TagID > &tags, THREAD_ID tid)
Retrieve a moose object warehouse in which every moose object at least has one of the given vector ta...
virtual void postElement(const Elem *) override
Called after the element assembly is done (including surface assembling)
This class provides an interface for common operations on field variables of both FE and FV types wit...
void clearActiveMaterialProperties(const THREAD_ID tid)
Clear the active material properties.
ComputeDiracThread(FEProblemBase &feproblem, const std::set< TagID > &tags, bool _is_jacobian)
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
void printBlockExecutionInformation() const override
Output the order of execution of objects within the current subdomain.
MooseObjectTagWarehouse< DiracKernelBase > & _dirac_kernels
Storage for DiracKernel objects.
virtual void setActiveElementalMooseVariables(const std::set< MooseVariableFEBase *> &moose_vars, const THREAD_ID tid) override
Set the MOOSE variables to be reinited on each element.
bool shouldPrintExecution(const THREAD_ID tid) const
Check whether the problem should output execution orders at this time.
const std::vector< std::shared_ptr< T > > & getActiveObjects(THREAD_ID tid=0) const
Retrieve complete vector to the active all/block/boundary restricted objects for a given thread...
const std::set< TagID > & _tags
MooseObjectWarehouse< T > & getMatrixTagObjectWarehouse(TagID tag_id, THREAD_ID tid)
Retrieve a moose object warehouse in which every moose object has the given matrix tag...
virtual void addResidual(const THREAD_ID tid) override
unsigned int number() const
Gets the number of this system.
Definition: SystemBase.C:1159
NonlinearSystemBase & _nl
void updateMatPropDependency(std::unordered_set< unsigned int > &needed_mat_props, THREAD_ID tid=0) const
Update material property dependency vector.
void printGeneralExecutionInformation() const override
Output a message indicating execution on this execution flag.
virtual bool reinitDirac(const Elem *elem, const THREAD_ID tid) override
Returns true if the Problem has Dirac kernels it needs to compute on elem.
virtual unsigned int numMatrixTags() const
The total number of tags.
Definition: SubProblem.h:248
std::set< SubdomainID > _blocks_exec_printed
Keep track of which blocks were visited.
tbb::split split
const ConsoleStream & console() const
Return console handle.
Definition: Problem.h:48
MooseObjectWarehouse< DiracKernelBase > * _dirac_warehouse
StoredRange< std::set< const Elem * >::const_iterator, const Elem * > DistElemRange
virtual void subdomainSetup(SubdomainID subdomain, const THREAD_ID tid)
void reinitMaterials(SubdomainID blk_id, const THREAD_ID tid, bool swap_stateful=true)
virtual void subdomainChanged() override
Called every time the current subdomain changes (i.e.
MooseObjectWarehouse< T > & getMatrixTagsObjectWarehouse(const std::set< TagID > &tags, THREAD_ID tid)
Retrieve a moose object warehouse in which every moose object has one of the given matrix tags...
virtual void onElement(const Elem *elem) override
Assembly of the element (not including surface assembly)
MooseObjectWarehouse< T > & getVectorTagObjectWarehouse(TagID tag_id, THREAD_ID tid)
Retrieve a moose object warehouse in which every moose object has the given vector tag...
bool activeOnSubdomain(SubdomainID subdomain) const
Is the variable active on the subdomain?
virtual void post() override
Called after the element range loop.
SubdomainID _subdomain
The subdomain for the current element.
virtual void clearActiveElementalMooseVariables(const THREAD_ID tid) override
Clear the active elemental MooseVariableFEBase.