www.mooseframework.org
ComputeDiracThread.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 "ComputeDiracThread.h"
16 
17 // Moose Includes
18 #include "ParallelUniqueId.h"
19 #include "DiracKernel.h"
20 #include "Problem.h"
21 #include "NonlinearSystem.h"
22 #include "MooseVariable.h"
23 #include "DiracKernel.h"
24 #include "Assembly.h"
25 
26 #include "libmesh/threads.h"
27 
28 ComputeDiracThread::ComputeDiracThread(FEProblemBase & feproblem, SparseMatrix<Number> * jacobian)
29  : ThreadedElementLoop<DistElemRange>(feproblem),
30  _jacobian(jacobian),
31  _nl(feproblem.getNonlinearSystemBase()),
32  _dirac_kernels(_nl.getDiracKernelWarehouse())
33 {
34 }
35 
36 // Splitting Constructor
40  _nl(x._nl),
42 {
43 }
44 
46 
47 void
49 {
50  // Force TID=0 because we run this object _NON THREADED_
51  // Take this out if we ever get Dirac's working with threads!
52  _tid = 0;
53 }
54 
55 void
57 {
59 
60  std::set<MooseVariable *> needed_moose_vars;
61  _dirac_kernels.updateVariableDependency(needed_moose_vars, _tid);
62 
63  // Update material dependencies
64  std::set<unsigned int> needed_mat_props;
65  _dirac_kernels.updateMatPropDependency(needed_mat_props, _tid);
66 
68  _fe_problem.setActiveMaterialProperties(needed_mat_props, _tid);
69 }
70 
71 void
73 {
74  const bool has_dirac_kernels_on_elem = _fe_problem.reinitDirac(elem, _tid);
75  if (!has_dirac_kernels_on_elem)
76  return;
77 
78  std::set<MooseVariable *> needed_moose_vars;
79  const std::vector<std::shared_ptr<DiracKernel>> & dkernels =
81 
82  // Only call reinitMaterials() if one or more DiracKernels has
83  // actually called getMaterialProperty(). Loop over all the
84  // DiracKernels and check whether this is the case.
85  for (const auto & dirac_kernel : dkernels)
86  {
87  // If any of the DiracKernels have had getMaterialProperty()
88  // called, we need to reinit Materials.
89  if (dirac_kernel->getMaterialPropertyCalled())
90  {
91  _fe_problem.reinitMaterials(_subdomain, _tid, /*swap_stateful=*/false);
92  break;
93  }
94  }
95 
96  for (const auto & dirac_kernel : dkernels)
97  {
98  if (!dirac_kernel->hasPointsOnElem(elem))
99  continue;
100  else if (_jacobian == NULL)
101  {
102  dirac_kernel->computeResidual();
103  continue;
104  }
105 
106  // Get a list of coupled variables from the SubProblem
107  std::vector<std::pair<MooseVariable *, MooseVariable *>> & coupling_entries =
108  dirac_kernel->subProblem().assembly(_tid).couplingEntries();
109 
110  // Loop over the list of coupled variable pairs
111  for (auto & it : coupling_entries)
112  {
113  MooseVariable * ivariable = it.first;
114  MooseVariable * jvariable = it.second;
115 
116  // A variant of the check that is in
117  // ComputeFullJacobianThread::computeJacobian(). We
118  // only want to call computeOffDiagJacobian() if both
119  // variables are active on this subdomain, and the
120  // off-diagonal variable actually has dofs.
121  if (dirac_kernel->variable().number() == ivariable->number() &&
122  ivariable->activeOnSubdomain(_subdomain) && jvariable->activeOnSubdomain(_subdomain) &&
123  (jvariable->numberOfDofs() > 0))
124  {
125  dirac_kernel->subProblem().prepareShapes(jvariable->number(), _tid);
126  dirac_kernel->computeOffDiagJacobian(jvariable->number());
127  }
128  }
129  }
130 
131  // Note that we do not call swapBackMaterials() here as they were
132  // never swapped in the first place. This avoids messing up
133  // stored values of stateful material properties.
134 }
135 
136 void
137 ComputeDiracThread::postElement(const Elem * /*elem*/)
138 {
139  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
140  if (_jacobian == NULL)
142  else
144 }
145 
146 void
148 {
151 }
152 
153 void
155 {
156 }
virtual void addResidual(THREAD_ID tid) override
virtual void setActiveElementalMooseVariables(const std::set< MooseVariable * > &moose_vars, THREAD_ID tid) override
Set the MOOSE variables to be reinited on each element.
Base class for assembly-like calculations.
Class for stuff related to variables.
Definition: MooseVariable.h:43
void join(const ComputeDiracThread &)
virtual bool reinitDirac(const Elem *elem, THREAD_ID tid) override
Returns true if the Problem has Dirac kernels it needs to compute on elem.
virtual void pre() override
Called before the element range loop.
unsigned int numberOfDofs()
const MooseObjectWarehouse< DiracKernel > & _dirac_kernels
Storage for DiracKernel objects.
virtual void postElement(const Elem *) override
Called after the element assembly is done (including surface assembling)
static PetscErrorCode Vec x
virtual void reinitMaterials(SubdomainID blk_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)
void updateVariableDependency(std::set< MooseVariable * > &needed_moose_vars, THREAD_ID tid=0) const
Update variable dependency vector.
virtual void clearActiveMaterialProperties(THREAD_ID tid) override
Clear the active material properties.
bool activeOnSubdomain(SubdomainID subdomain) const
Is the variable active on the subdomain?
SparseMatrix< Number > * _jacobian
NonlinearSystemBase & _nl
unsigned int number() const
Get variable number coming from libMesh.
void updateMatPropDependency(std::set< unsigned int > &needed_mat_props, THREAD_ID tid=0) const
Update material property dependency vector.
StoredRange< std::set< const Elem * >::const_iterator, const Elem * > DistElemRange
virtual void subdomainChanged() override
Called every time the current subdomain changes (i.e.
virtual void addJacobian(SparseMatrix< Number > &jacobian, THREAD_ID tid) override
virtual void onElement(const Elem *elem) override
Assembly of the element (not including surface assembly)
virtual void post() override
Called after the element range loop.
SubdomainID _subdomain
The subdomain for the current element.
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...
ComputeDiracThread(FEProblemBase &feproblem, SparseMatrix< Number > *jacobian=NULL)
virtual void clearActiveElementalMooseVariables(THREAD_ID tid) override
Clear the active elemental MooseVariable.