https://mooseframework.inl.gov
ADUtils.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 "MooseError.h"
13 #include "MooseTypes.h"
14 #include "MooseConfig.h"
15 
16 class SubProblem;
17 class SystemBase;
18 namespace libMesh
19 {
20 class Elem;
21 }
22 
23 namespace Moose
24 {
28 inline bool
30 {
31  return true;
32 }
33 
78 inline std::size_t
79 adOffset(unsigned int var_num,
80  std::size_t max_dofs_per_elem,
81  ElementType element_type = ElementType::Element,
82  unsigned int num_vars_in_system = 0)
83 {
84  // If our element type is anything other than ElementType::Element, then the user must
85  // supply num_vars_in_system in order to calculate the offset
86  mooseAssert(element_type == ElementType::Element || num_vars_in_system,
87  "If our element type is anything other than ElementType::Element, then you "
88  "must supply num_vars_in_system in order to calculate the offset");
89 
90  switch (element_type)
91  {
93  return var_num * max_dofs_per_elem;
94 
96  return num_vars_in_system * max_dofs_per_elem + var_num * max_dofs_per_elem;
97 
98  case ElementType::Lower:
99  return 2 * num_vars_in_system * max_dofs_per_elem + var_num * max_dofs_per_elem;
100 
101  default:
102  mooseError(
103  "Unsupported element type ",
104  static_cast<typename std::underlying_type<decltype(element_type)>::type>(element_type));
105  }
106 }
107 
108 inline std::size_t
109 adOffset(unsigned int var_num,
110  std::size_t max_dofs_per_elem,
111  DGJacobianType dg_jacobian_type,
112  unsigned int num_vars_in_system = 0)
113 {
114  if (dg_jacobian_type == DGJacobianType::ElementElement ||
115  dg_jacobian_type == DGJacobianType::NeighborElement)
116  return adOffset(var_num, max_dofs_per_elem, ElementType::Element);
117  else
118  return adOffset(var_num, max_dofs_per_elem, ElementType::Neighbor, num_vars_in_system);
119 }
120 
124 std::unordered_map<dof_id_type, Real>
125 globalDofIndexToDerivative(const ADReal & ad_real,
126  const SystemBase & sys,
127  ElementType elem_type = ElementType::Element,
128  THREAD_ID tid = 0);
129 
134 template <typename T>
135 auto
136 globalDofIndexToDerivative(const T & ad_real_container,
137  const SystemBase & sys,
138  ElementType elem_type = ElementType::Element,
139  THREAD_ID tid = 0)
140  -> std::vector<std::unordered_map<
141  dof_id_type,
142  typename std::enable_if<std::is_same<ADReal, typename T::value_type>::value, Real>::type>>
143 {
144  std::vector<std::unordered_map<dof_id_type, Real>> ret_val(ad_real_container.size());
145 
146  for (MooseIndex(ad_real_container) i = 0; i < ad_real_container.size(); ++i)
147  ret_val[i] = globalDofIndexToDerivative(ad_real_container[i], sys, elem_type, tid);
148 
149  return ret_val;
150 }
151 
155 bool doDerivatives(const SubProblem & subproblem, const SystemBase & sys);
156 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
bool globalADIndexing()
Whether we are using global AD indexing.
Definition: ADUtils.h:29
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
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
DGJacobianType
Definition: MooseTypes.h:749
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:78
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
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
uint8_t dof_id_type