https://mooseframework.inl.gov
ADUtils.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 "ADUtils.h"
11 
12 // We must include this first for the unity build to see some dataStore template specializations
13 // before including DataIO.h through NonlinearSystemBase.h
14 #include "PerfGraphRegistry.h"
15 
16 #include "NonlinearSystemBase.h"
17 #include "SubProblem.h"
18 #include "Assembly.h"
19 #include "MooseError.h"
20 #include "libmesh/system.h"
21 #include "libmesh/dof_map.h"
22 
23 namespace Moose
24 {
25 
26 std::unordered_map<dof_id_type, Real>
28  const SystemBase & sys,
29  const ElementType elem_type /*=ElementType::Element*/,
30  const THREAD_ID tid /*=0*/)
31 {
32  mooseAssert(dynamic_cast<const NonlinearSystemBase *>(&sys),
33  "This must be a nonlinear system base object");
34  const Assembly & assembly = sys.subproblem().assembly(tid, sys.number());
35  const Elem * elem;
36  switch (elem_type)
37  {
39  elem = assembly.elem();
40  break;
41 
43  elem = assembly.neighbor();
44  break;
45 
46  case ElementType::Lower:
47  elem = assembly.lowerDElem();
48  break;
49 
50  default:
51  mooseError("Unrecognized element type");
52  }
53 
54  std::unordered_map<dof_id_type, Real> ret_val;
55 
56  const System & libmesh_sys = sys.system();
57  const DofMap & dof_map = libmesh_sys.get_dof_map();
58 
59  const unsigned int num_vars = libmesh_sys.n_vars();
60 
61  const auto max_dofs_per_elem = sys.getMaxVarNDofsPerElem();
62 
63  for (unsigned int var_num = 0; var_num < num_vars; ++var_num)
64  {
65  std::vector<dof_id_type> global_indices;
66 
67  // Get the global indices corresponding to var_num that exist on elem
68  dof_map.dof_indices(elem, global_indices, var_num);
69 
70  // determine the AD offset for the current var
71  const auto ad_offset = adOffset(var_num, max_dofs_per_elem, elem_type, num_vars);
72 
73  // Map from global index to derivative
74  for (MooseIndex(global_indices) local_index = 0; local_index < global_indices.size();
75  ++local_index)
76  ret_val[global_indices[local_index]] = ad_real.derivatives()[ad_offset + local_index];
77  }
78 
79  return ret_val;
80 }
81 
82 bool
83 doDerivatives(const SubProblem & subproblem, const SystemBase & sys)
84 {
85  return ADReal::do_derivatives && sys.number() == subproblem.currentNlSysNum();
86 }
87 }
const Elem *const & elem() const
Return the current element.
Definition: Assembly.h:375
Keeps track of stuff related to assembling.
Definition: Assembly.h:101
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
virtual unsigned int currentNlSysNum() const =0
virtual libMesh::System & system()=0
Get the reference to the libMesh system.
ElementType
Definition: MooseTypes.h:763
Base class for a system (of equations)
Definition: SystemBase.h:84
DualNumber< Real, DNDerivativeType, true > ADReal
Definition: ADRealForward.h:47
std::size_t adOffset(unsigned int var_num, std::size_t max_dofs_per_elem, ElementType element_type=ElementType::Element, unsigned int num_vars_in_system=0)
Helper function for computing automatic differentiation offset.
Definition: ADUtils.h:79
bool doDerivatives(const SubProblem &subproblem, const SystemBase &sys)
Definition: ADUtils.C:83
SubProblem & subproblem()
Definition: SystemBase.h:101
unsigned int number() const
Gets the number of this system.
Definition: SystemBase.C:1159
std::size_t getMaxVarNDofsPerElem() const
Gets the maximum number of dofs used by any one variable on any one element.
Definition: SystemBase.h:586
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:78
virtual Assembly & assembly(const THREAD_ID tid, const unsigned int sys_num)=0
const Elem *const & neighbor() const
Return the neighbor element.
Definition: Assembly.h:431
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
const Elem *const & lowerDElem() const
Return the lower dimensional element.
Definition: Assembly.h:437
const DofMap & get_dof_map() const
std::unordered_map< dof_id_type, Real > globalDofIndexToDerivative(const ADReal &ad_real, const SystemBase &sys, ElementType elem_type=ElementType::Element, THREAD_ID tid=0)
Generate a map from global dof index to derivative value.
Definition: ADUtils.C:27
unsigned int THREAD_ID
Definition: MooseTypes.h:209